• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

taosdata / TDengine / #3544

30 Nov 2024 03:06AM UTC coverage: 60.88% (+0.04%) from 60.842%
#3544

push

travis-ci

web-flow
Merge pull request #28988 from taosdata/main

merge: from main to 3.0 branch

120724 of 253479 branches covered (47.63%)

Branch coverage included in aggregate %.

407 of 489 new or added lines in 21 files covered. (83.23%)

1148 existing lines in 113 files now uncovered.

201919 of 276488 relevant lines covered (73.03%)

18898587.44 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

76.59
/source/libs/stream/src/streamData.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#include "streamInt.h"
17
#include "ttime.h"
18

19
static int32_t streamMergedSubmitNew(SStreamMergedSubmit** pSubmit) {
25,416✔
20
  *pSubmit = NULL;
25,416✔
21

22
  int32_t code = taosAllocateQitem(sizeof(SStreamMergedSubmit), DEF_QITEM, 0, (void**)pSubmit);
25,416✔
23
  if (code) {
25,427!
24
    return TSDB_CODE_OUT_OF_MEMORY;
×
25
  }
26

27
  (*pSubmit)->submits = taosArrayInit(0, sizeof(SPackedData));
25,427✔
28
  if ((*pSubmit)->submits == NULL) {
25,426!
UNCOV
29
    taosFreeQitem(*pSubmit);
×
30
    *pSubmit = NULL;
×
31
    return terrno;
×
32
  }
33

34
  (*pSubmit)->type = STREAM_INPUT__MERGED_SUBMIT;
25,427✔
35
  return TSDB_CODE_SUCCESS;
25,427✔
36
}
37

38
static int32_t streamMergeSubmit(SStreamMergedSubmit* pMerged, SStreamDataSubmit* pSubmit) {
428,313✔
39
  void* p = taosArrayPush(pMerged->submits, &pSubmit->submit);
428,313✔
40
  if (p == NULL) {
428,259!
41
    return terrno;
×
42
  }
43

44
  if (pSubmit->ver > pMerged->ver) {
428,259!
45
    pMerged->ver = pSubmit->ver;
428,263✔
46
  }
47
  return 0;
428,259✔
48
}
49

50
static void freeItems(void* param) {
19,267✔
51
  SSDataBlock* pBlock = param;
19,267✔
52
  taosArrayDestroy(pBlock->pDataBlock);
19,267✔
53
}
19,295✔
54

55
int32_t createStreamBlockFromDispatchMsg(const SStreamDispatchReq* pReq, int32_t blockType, int32_t srcVg, SStreamDataBlock** pRes) {
62,401✔
56
  SStreamDataBlock* pData = NULL;
62,401✔
57
  int32_t code = taosAllocateQitem(sizeof(SStreamDataBlock), DEF_QITEM, pReq->totalLen, (void**)&pData);
62,401✔
58
  if (code) {
62,438!
59
    return terrno = code;
×
60
  }
61

62
  pData->type = blockType;
62,438✔
63
  pData->srcVgId = srcVg;
62,438✔
64
  pData->srcTaskId = pReq->upstreamTaskId;
62,438✔
65

66
  int32_t blockNum = pReq->blockNum;
62,438✔
67
  SArray* pArray = taosArrayInit_s(sizeof(SSDataBlock), blockNum);
62,438✔
68
  if (pArray == NULL) {
62,446!
UNCOV
69
    taosFreeQitem(pData);
×
70
    return code;
×
71
  }
72

73
  for (int32_t i = 0; i < blockNum; i++) {
226,360✔
74
    SRetrieveTableRsp* pRetrieve = (SRetrieveTableRsp*)taosArrayGetP(pReq->data, i);
163,920✔
75
    SSDataBlock* pDataBlock = taosArrayGet(pArray, i);
163,925✔
76
    if (pDataBlock == NULL || pRetrieve == NULL) {
163,894!
77
      return terrno;
×
78
    }
79

80
    int32_t compLen = *(int32_t*)pRetrieve->data;
163,902✔
81
    int32_t fullLen = *(int32_t*)(pRetrieve->data + sizeof(int32_t));
163,902✔
82

83
    char* pInput = pRetrieve->data + PAYLOAD_PREFIX_LEN;
163,902✔
84
    if (pRetrieve->compressed && compLen < fullLen) {
163,902!
85
      char* p = taosMemoryMalloc(fullLen);
×
86
      if (p == NULL) {
×
87
        return terrno;
×
88
      }
89

90
      int32_t len = tsDecompressString(pInput, compLen, 1, p, fullLen, ONE_STAGE_COMP, NULL, 0);
×
91
      pInput = p;
×
92
    }
93

94
    const char* pDummy = NULL;
163,902✔
95
    code = blockDecode(pDataBlock, pInput, &pDummy);
163,902✔
96
    if (code) {
163,882!
97
      return code;
×
98
    }
99

100
    if (pRetrieve->compressed && compLen < fullLen) {
163,882!
101
      taosMemoryFree(pInput);
×
102
    }
103

104
    // TODO: refactor
105
    pDataBlock->info.window.skey = be64toh(pRetrieve->skey);
163,882✔
106
    pDataBlock->info.window.ekey = be64toh(pRetrieve->ekey);
163,864✔
107
    pDataBlock->info.version = be64toh(pRetrieve->version);
163,878✔
108
    pDataBlock->info.watermark = be64toh(pRetrieve->watermark);
163,880✔
109
    memcpy(pDataBlock->info.parTbName, pRetrieve->parTbName, TSDB_TABLE_NAME_LEN);
163,913✔
110

111
    pDataBlock->info.type = pRetrieve->streamBlockType;
163,913✔
112
    pDataBlock->info.childId = pReq->upstreamChildId;
163,913✔
113
  }
114

115
  pData->blocks = pArray;
62,440✔
116
  *pRes = pData;
62,440✔
117

118
  return code;
62,440✔
119
}
120

121
int32_t createStreamBlockFromResults(SStreamQueueItem* pItem, SStreamTask* pTask, int64_t resultSize, SArray* pRes,
26,506✔
122
                                     SStreamDataBlock** pBlock) {
123
  int32_t code = taosAllocateQitem(sizeof(SStreamDataBlock), DEF_QITEM, resultSize, (void**)pBlock);
26,506✔
124
  if (code) {
26,505!
125
    taosArrayClearEx(pRes, (FDelete)blockDataFreeRes);
×
126
    return terrno = code;
×
127
  }
128

129
  (*pBlock)->srcTaskId = pTask->id.taskId;
26,505✔
130
  (*pBlock)->type = STREAM_INPUT__DATA_BLOCK;
26,505✔
131
  (*pBlock)->blocks = pRes;
26,505✔
132

133
  if (pItem == NULL) {
26,505✔
134
    return code;
1,916✔
135
  }
136

137
  if (pItem->type == STREAM_INPUT__DATA_SUBMIT) {
24,589✔
138
    SStreamDataSubmit* pSubmit = (SStreamDataSubmit*)pItem;
4,382✔
139
    (*pBlock)->sourceVer = pSubmit->ver;
4,382✔
140
  } else if (pItem->type == STREAM_INPUT__MERGED_SUBMIT) {
20,207✔
141
    SStreamMergedSubmit* pMerged = (SStreamMergedSubmit*)pItem;
15,439✔
142
    (*pBlock)->sourceVer = pMerged->ver;
15,439✔
143
  }
144

145
  return code;
24,589✔
146
}
147

148
void destroyStreamDataBlock(SStreamDataBlock* pBlock) {
53,424✔
149
  if (pBlock == NULL) {
53,424!
150
    return;
×
151
  }
152

153
  taosArrayDestroyEx(pBlock->blocks, (FDelete)blockDataFreeRes);
53,424✔
154
  taosFreeQitem(pBlock);
53,431✔
155
}
156

157
int32_t streamRetrieveReqToData(const SStreamRetrieveReq* pReq, SStreamDataBlock* pData, const char* id) {
516✔
158
  const char*        pDummy = NULL;
516✔
159
  SRetrieveTableRsp* pRetrieve = pReq->pRetrieve;
516✔
160
  SArray*            pArray = taosArrayInit(1, sizeof(SSDataBlock));
516✔
161
  if (pArray == NULL) {
520!
162
    stError("failed to prepare retrieve block, %s", id);
×
163
    return terrno;
×
164
  }
165

166
  void* px = taosArrayPush(pArray, &(SSDataBlock){0});
520✔
167
  if (px == NULL) {
520!
168
    taosArrayDestroy(pArray);
×
169
    return terrno;
×
170
  }
171

172
  SSDataBlock* pDataBlock = taosArrayGet(pArray, 0);
520✔
173
  if (pDataBlock == NULL) {
520!
174
    taosArrayDestroy(pArray);
×
175
    return terrno;
×
176
  }
177

178
  int32_t code = blockDecode(pDataBlock, pRetrieve->data + PAYLOAD_PREFIX_LEN, &pDummy);
520✔
179
  if (code) {
518!
180
    taosArrayDestroy(pArray);
×
181
    return code;
×
182
  }
183

184
  // TODO: refactor
185
  pDataBlock->info.window.skey = be64toh(pRetrieve->skey);
518✔
186
  pDataBlock->info.window.ekey = be64toh(pRetrieve->ekey);
518✔
187
  pDataBlock->info.version = be64toh(pRetrieve->version);
518✔
188

189
  pDataBlock->info.type = pRetrieve->streamBlockType;
518✔
190

191
  pData->reqId = pReq->reqId;
518✔
192
  pData->blocks = pArray;
518✔
193

194
  return code;
518✔
195
}
196

197
int32_t streamDataSubmitNew(SPackedData* pData, int32_t type, SStreamDataSubmit** pSubmit) {
445,382✔
198
  SStreamDataSubmit* pDataSubmit = NULL;
445,382✔
199
  int32_t code = taosAllocateQitem(sizeof(SStreamDataSubmit), DEF_QITEM, pData->msgLen, (void**)&pDataSubmit);
445,382✔
200
  if (code) {
445,397!
201
    return code;
×
202
  }
203

204
  pDataSubmit->ver = pData->ver;
445,397✔
205
  pDataSubmit->submit = *pData;
445,397✔
206
  pDataSubmit->type = type;
445,397✔
207

208
  *pSubmit = pDataSubmit;
445,397✔
209
  return TSDB_CODE_SUCCESS;
445,397✔
210
}
211

212
void streamDataSubmitDestroy(SStreamDataSubmit* pDataSubmit) {
17,018✔
213
  if (pDataSubmit != NULL && pDataSubmit->type == STREAM_INPUT__DATA_SUBMIT) {
17,018!
214
    taosMemoryFree(pDataSubmit->submit.msgStr);
17,020✔
215
    taosFreeQitem(pDataSubmit);
17,020✔
216
  }
217
}
17,019✔
218

219
// todo handle memory error
220
int32_t streamQueueMergeQueueItem(SStreamQueueItem* dst, SStreamQueueItem* pElem, SStreamQueueItem** pRes) {
428,893✔
221
  *pRes = NULL;
428,893✔
222
  int32_t code = 0;
428,893✔
223

224
  if (dst->type == STREAM_INPUT__DATA_BLOCK && pElem->type == STREAM_INPUT__DATA_BLOCK) {
428,893✔
225
    SStreamDataBlock* pBlock = (SStreamDataBlock*)dst;
25,891✔
226
    SStreamDataBlock* pBlockSrc = (SStreamDataBlock*)pElem;
25,891✔
227
    void* px = taosArrayAddAll(pBlock->blocks, pBlockSrc->blocks);
25,891✔
228
    if (px == NULL) {
25,887!
229
      return terrno;
×
230
    }
231

232
    taosArrayDestroy(pBlockSrc->blocks);
25,887✔
233
    streamQueueItemIncSize(dst, streamQueueItemGetSize(pElem));
25,891✔
234

235
    taosFreeQitem(pElem);
25,888✔
236
    *pRes = dst;
25,893✔
237
    return code;
25,893✔
238
  } else if (dst->type == STREAM_INPUT__MERGED_SUBMIT && pElem->type == STREAM_INPUT__DATA_SUBMIT) {
403,002✔
239
    SStreamMergedSubmit* pMerged = (SStreamMergedSubmit*)dst;
377,514✔
240
    SStreamDataSubmit*   pBlockSrc = (SStreamDataSubmit*)pElem;
377,514✔
241

242
    code = streamMergeSubmit(pMerged, pBlockSrc);
377,514✔
243
    streamQueueItemIncSize(dst, streamQueueItemGetSize(pElem));
377,462✔
244

245
    taosFreeQitem(pElem);
377,443✔
246
    *pRes = dst;
377,524✔
247
    *pRes = dst;
377,524✔
248
    return code;
377,524✔
249
  } else if (dst->type == STREAM_INPUT__DATA_SUBMIT && pElem->type == STREAM_INPUT__DATA_SUBMIT) {
25,488✔
250
    SStreamMergedSubmit* pMerged = NULL;
25,420✔
251
    code = streamMergedSubmitNew(&pMerged);
25,420✔
252
    if (code != 0) {
25,426!
253
      return code;
×
254
    }
255

256
    streamQueueItemIncSize((SStreamQueueItem*)pMerged, streamQueueItemGetSize(pElem));
25,426✔
257

258
    code = streamMergeSubmit(pMerged, (SStreamDataSubmit*)dst);
25,425✔
259
    if (code == 0) {
25,420!
260
      code = streamMergeSubmit(pMerged, (SStreamDataSubmit*)pElem);
25,421✔
261
    }
262

263
    taosFreeQitem(dst);
25,419✔
264
    taosFreeQitem(pElem);
25,430✔
265

266
    *pRes = (SStreamQueueItem*)pMerged;
25,430✔
267
    return code;
25,430✔
268
  } else {
269
    code = TSDB_CODE_FAILED;
68✔
270
    stDebug("block type:%s not merged with existed blocks list, type:%d", streamQueueItemGetTypeStr(pElem->type),
68✔
271
            dst->type);
272
    return code;
72✔
273
  }
274
}
275

276
void streamFreeQitem(SStreamQueueItem* data) {
70,183✔
277
  int8_t type = data->type;
70,183✔
278
  if (type == STREAM_INPUT__GET_RES) {
70,183✔
279
    blockDataDestroy(((SStreamTrigger*)data)->pBlock);
2,941✔
280
    taosFreeQitem(data);
2,942✔
281
  } else if (type == STREAM_INPUT__DATA_BLOCK || type == STREAM_INPUT__DATA_RETRIEVE) {
67,242✔
282
    destroyStreamDataBlock((SStreamDataBlock*)data);
3,054✔
283
  } else if (type == STREAM_INPUT__DATA_SUBMIT) {
64,188✔
284
    streamDataSubmitDestroy((SStreamDataSubmit*)data);
17,017✔
285
  } else if (type == STREAM_INPUT__MERGED_SUBMIT) {
47,171✔
286
    SStreamMergedSubmit* pMerge = (SStreamMergedSubmit*)data;
25,433✔
287

288
    int32_t sz = taosArrayGetSize(pMerge->submits);
25,433✔
289
    for (int32_t i = 0; i < sz; i++) {
453,809✔
290
      SPackedData* pSubmit = (SPackedData*)taosArrayGet(pMerge->submits, i);
428,372✔
291
      if (pSubmit == NULL) {
428,362!
292
        continue;
×
293
      }
294
      taosMemoryFree(pSubmit->msgStr);
428,362✔
295
    }
296

297
    taosArrayDestroy(pMerge->submits);
25,437✔
298
    taosFreeQitem(pMerge);
25,433✔
299
  } else if (type == STREAM_INPUT__REF_DATA_BLOCK) {
21,738✔
300
    SStreamRefDataBlock* pRefBlock = (SStreamRefDataBlock*)data;
2,451✔
301
    blockDataDestroy(pRefBlock->pBlock);
2,451✔
302
    taosFreeQitem(pRefBlock);
2,452✔
303
  } else if (type == STREAM_INPUT__CHECKPOINT || type == STREAM_INPUT__CHECKPOINT_TRIGGER ||
19,287!
304
             type == STREAM_INPUT__TRANS_STATE) {
305
    SStreamDataBlock* pBlock = (SStreamDataBlock*)data;
19,293✔
306
    taosArrayDestroyEx(pBlock->blocks, freeItems);
19,293✔
307
    taosFreeQitem(pBlock);
19,306✔
308
  }
309
}
70,211✔
310

311
int32_t streamCreateForcewindowTrigger(SStreamTrigger** pTrigger, int32_t interval, SInterval* pInterval,
1,662✔
312
                                       STimeWindow* pLatestWindow, const char* id) {
313
  QRY_PARAM_CHECK(pTrigger);
1,662!
314

315
  SStreamTrigger* p = NULL;
1,662✔
316
  int64_t         ts = taosGetTimestamp(pInterval->precision);
1,662!
317
  int64_t         skey = pLatestWindow->skey + interval;
1,662✔
318

319
  int32_t code = taosAllocateQitem(sizeof(SStreamTrigger), DEF_QITEM, 0, (void**)&p);
1,662✔
320
  if (code) {
1,662!
NEW
321
    stError("s-task:%s failed to create force_window trigger, code:%s", id, tstrerror(code));
×
UNCOV
322
    return code;
×
323
  }
324

325
  p->type = STREAM_INPUT__GET_RES;
1,662✔
326
  p->pBlock = taosMemoryCalloc(1, sizeof(SSDataBlock));
1,662✔
327
  if (p->pBlock == NULL) {
1,662!
328
    taosFreeQitem(p);
×
329
    return terrno;
×
330
  }
331

332
  p->pBlock->info.window.skey = skey;
1,662✔
333
  p->pBlock->info.window.ekey = TMAX(ts, skey + interval);
1,662✔
334
  p->pBlock->info.type = STREAM_GET_RESULT;
1,662✔
335

336
  stDebug("s-task:%s force_window_close trigger block generated, window range:%" PRId64 "-%" PRId64, id,
1,662!
337
          p->pBlock->info.window.skey, p->pBlock->info.window.ekey);
338

339
  *pTrigger = p;
1,662✔
340
  return code;
1,662✔
341
}
342

343
int32_t streamCreateSinkResTrigger(SStreamTrigger** pTrigger) {
1,280✔
344
  QRY_PARAM_CHECK(pTrigger);
1,280!
345
  SStreamTrigger* p = NULL;
1,280✔
346

347
  int32_t code = taosAllocateQitem(sizeof(SStreamTrigger), DEF_QITEM, 0, (void**)&p);
1,280✔
348
  if (code) {
1,280!
349
    return code;
×
350
  }
351

352
  p->type = STREAM_INPUT__GET_RES;
1,280✔
353
  p->pBlock = taosMemoryCalloc(1, sizeof(SSDataBlock));
1,280✔
354
  if (p->pBlock == NULL) {
1,280!
355
    taosFreeQitem(p);
×
356
    return terrno;
×
357
  }
358

359
  p->pBlock->info.type = STREAM_GET_ALL;
1,280✔
360
  *pTrigger = p;
1,280✔
361
  return code;
1,280✔
362
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc