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

taosdata / TDengine / #4469

08 Jul 2025 09:38AM UTC coverage: 62.22% (-1.2%) from 63.381%
#4469

push

travis-ci

web-flow
Merge pull request #31712 from taosdata/merge/mainto3.0

merge: from main to 3.0 branch

153678 of 316510 branches covered (48.55%)

Branch coverage included in aggregate %.

56 of 60 new or added lines in 13 files covered. (93.33%)

5035 existing lines in 221 files now uncovered.

238955 of 314529 relevant lines covered (75.97%)

6273248.0 hits per line

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

63.57
/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) {
5,763✔
20
  *pSubmit = NULL;
5,763✔
21

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

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

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

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

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

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

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

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

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

73
  for (int32_t i = 0; i < blockNum; i++) {
30,591✔
74
    SRetrieveTableRsp* pRetrieve = (SRetrieveTableRsp*)taosArrayGetP(pReq->data, i);
18,356✔
75
    SSDataBlock* pDataBlock = taosArrayGet(pArray, i);
18,356✔
76
    if (pDataBlock == NULL || pRetrieve == NULL) {
18,357!
77
      return terrno;
×
78
    }
79

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

83
    char* pInput = pRetrieve->data + PAYLOAD_PREFIX_LEN;
18,357✔
84
    if (pRetrieve->compressed && compLen < fullLen) {
18,357!
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;
18,357✔
95
    code = blockDecode(pDataBlock, pInput, &pDummy);
18,357✔
96
    if (code) {
18,358!
97
      return code;
×
98
    }
99

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

104
    // TODO: refactor
105
    pDataBlock->info.window.skey = be64toh(pRetrieve->skey);
18,358✔
106
    pDataBlock->info.window.ekey = be64toh(pRetrieve->ekey);
18,358✔
107
    pDataBlock->info.version = be64toh(pRetrieve->version);
18,357✔
108
    pDataBlock->info.watermark = be64toh(pRetrieve->watermark);
18,357✔
109
    memcpy(pDataBlock->info.parTbName, pRetrieve->parTbName, TSDB_TABLE_NAME_LEN);
18,357✔
110

111
    pDataBlock->info.type = pRetrieve->streamBlockType;
18,357✔
112
    pDataBlock->info.childId = pReq->upstreamChildId;
18,357✔
113
    pDataBlock->info.id.uid = be64toh(pRetrieve->useconds);
18,357✔
114
  }
115

116
  pData->blocks = pArray;
12,235✔
117
  *pRes = pData;
12,235✔
118

119
  return code;
12,235✔
120
}
121

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

130
  (*pBlock)->srcTaskId = pTask->id.taskId;
7,664✔
131
  (*pBlock)->type = STREAM_INPUT__DATA_BLOCK;
7,664✔
132
  (*pBlock)->blocks = pRes;
7,664✔
133

134
  if (pItem == NULL) {
7,664✔
135
    return code;
984✔
136
  }
137

138
  if (pItem->type == STREAM_INPUT__DATA_SUBMIT) {
6,680✔
139
    SStreamDataSubmit* pSubmit = (SStreamDataSubmit*)pItem;
809✔
140
    (*pBlock)->sourceVer = pSubmit->ver;
809✔
141
  } else if (pItem->type == STREAM_INPUT__MERGED_SUBMIT) {
5,871✔
142
    SStreamMergedSubmit* pMerged = (SStreamMergedSubmit*)pItem;
2,696✔
143
    (*pBlock)->sourceVer = pMerged->ver;
2,696✔
144
  }
145

146
  return code;
6,680✔
147
}
148

149
void destroyStreamDataBlock(SStreamDataBlock* pBlock) {
15,699✔
150
  if (pBlock == NULL) {
15,699!
151
    return;
×
152
  }
153

154
  taosArrayDestroyEx(pBlock->blocks, (FDelete)blockDataFreeRes);
15,699✔
155
  taosFreeQitem(pBlock);
15,699✔
156
}
157

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

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

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

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

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

190
  pDataBlock->info.type = pRetrieve->streamBlockType;
422✔
191

192
  pData->reqId = pReq->reqId;
422✔
193
  pData->blocks = pArray;
422✔
194

195
  return code;
422✔
196
}
197

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

205
  pDataSubmit->ver = pData->ver;
56,573✔
206
  pDataSubmit->submit = *pData;
56,573✔
207
  pDataSubmit->type = type;
56,573✔
208

209
  *pSubmit = pDataSubmit;
56,573✔
210
  return TSDB_CODE_SUCCESS;
56,573✔
211
}
212

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

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

225
  if (dst->type == STREAM_INPUT__DATA_BLOCK && pElem->type == STREAM_INPUT__DATA_BLOCK) {
48,908!
226
    SStreamDataBlock* pBlock = (SStreamDataBlock*)dst;
1,437✔
227
    SStreamDataBlock* pBlockSrc = (SStreamDataBlock*)pElem;
1,437✔
228
    void* px = taosArrayAddAll(pBlock->blocks, pBlockSrc->blocks);
1,437✔
229
    if (px == NULL) {
1,437!
230
      return terrno;
×
231
    }
232

233
    taosArrayDestroy(pBlockSrc->blocks);
1,437✔
234
    streamQueueItemIncSize(dst, streamQueueItemGetSize(pElem));
1,437✔
235

236
    taosFreeQitem(pElem);
1,437✔
237
    *pRes = dst;
1,437✔
238
    return code;
1,437✔
239
  } else if (dst->type == STREAM_INPUT__MERGED_SUBMIT && pElem->type == STREAM_INPUT__DATA_SUBMIT) {
47,471✔
240
    SStreamMergedSubmit* pMerged = (SStreamMergedSubmit*)dst;
41,528✔
241
    SStreamDataSubmit*   pBlockSrc = (SStreamDataSubmit*)pElem;
41,528✔
242

243
    code = streamMergeSubmit(pMerged, pBlockSrc);
41,528✔
244
    streamQueueItemIncSize(dst, streamQueueItemGetSize(pElem));
41,528✔
245

246
    taosFreeQitem(pElem);
41,528✔
247
    *pRes = dst;
41,528✔
248
    *pRes = dst;
41,528✔
249
    return code;
41,528✔
250
  } else if (dst->type == STREAM_INPUT__DATA_SUBMIT && pElem->type == STREAM_INPUT__DATA_SUBMIT) {
5,943✔
251
    SStreamMergedSubmit* pMerged = NULL;
5,763✔
252
    code = streamMergedSubmitNew(&pMerged);
5,763✔
253
    if (code != 0) {
5,763!
254
      return code;
×
255
    }
256

257
    streamQueueItemIncSize((SStreamQueueItem*)pMerged, streamQueueItemGetSize(pElem));
5,763✔
258

259
    code = streamMergeSubmit(pMerged, (SStreamDataSubmit*)dst);
5,763✔
260
    if (code == 0) {
5,763!
261
      code = streamMergeSubmit(pMerged, (SStreamDataSubmit*)pElem);
5,763✔
262
    }
263

264
    taosFreeQitem(dst);
5,763✔
265
    taosFreeQitem(pElem);
5,763✔
266

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

277
void streamFreeQitem(SStreamQueueItem* data) {
22,781✔
278
  if (data == NULL) {
22,781!
279
    return;
×
280
  }
281

282
  int8_t type = data->type;
22,781✔
283
  if (type == STREAM_INPUT__GET_RES) {
22,781✔
284
    blockDataDestroy(((SStreamTrigger*)data)->pBlock);
5,393✔
285
    taosFreeQitem(data);
5,393✔
286
  } else if (type == STREAM_INPUT__DATA_BLOCK || type == STREAM_INPUT__DATA_RETRIEVE) {
17,388✔
287
    destroyStreamDataBlock((SStreamDataBlock*)data);
1,407✔
288
  } else if (type == STREAM_INPUT__DATA_SUBMIT) {
15,981✔
289
    streamDataSubmitDestroy((SStreamDataSubmit*)data);
3,520✔
290
  } else if (type == STREAM_INPUT__MERGED_SUBMIT) {
12,461✔
291
    SStreamMergedSubmit* pMerge = (SStreamMergedSubmit*)data;
5,763✔
292

293
    int32_t sz = taosArrayGetSize(pMerge->submits);
5,763✔
294
    for (int32_t i = 0; i < sz; i++) {
58,817✔
295
      SPackedData* pSubmit = (SPackedData*)taosArrayGet(pMerge->submits, i);
53,054✔
296
      if (pSubmit == NULL) {
53,054!
297
        continue;
×
298
      }
299
      taosMemoryFree(pSubmit->msgStr);
53,054!
300
    }
301

302
    taosArrayDestroy(pMerge->submits);
5,763✔
303
    taosFreeQitem(pMerge);
5,763✔
304
  } else if (type == STREAM_INPUT__REF_DATA_BLOCK) {
6,698✔
305
    SStreamRefDataBlock* pRefBlock = (SStreamRefDataBlock*)data;
963✔
306
    blockDataDestroy(pRefBlock->pBlock);
963✔
307
    taosFreeQitem(pRefBlock);
963✔
308
  } else if (type == STREAM_INPUT__CHECKPOINT || type == STREAM_INPUT__CHECKPOINT_TRIGGER ||
5,735!
309
             type == STREAM_INPUT__TRANS_STATE || type == STREAM_INPUT__RECALCULATE) {
×
310
    SStreamDataBlock* pBlock = (SStreamDataBlock*)data;
5,735✔
311
    taosArrayDestroyEx(pBlock->blocks, freeItems);
5,735✔
312
    taosFreeQitem(pBlock);
5,735✔
313
  }
314
}
315

316
int32_t streamCreateForcewindowTrigger(SStreamTrigger** pTrigger, int32_t interval, SInterval* pInterval,
4,408✔
317
                                       STimeWindow* pLatestWindow, const char* id) {
318
  QRY_PARAM_CHECK(pTrigger);
4,408!
319

320
  SStreamTrigger* p = NULL;
4,408✔
321
  int64_t         ts = taosGetTimestamp(pInterval->precision);
4,408✔
322
  int64_t         skey = pLatestWindow->skey + pInterval->sliding;
4,408✔
323

324
  int32_t code = taosAllocateQitem(sizeof(SStreamTrigger), DEF_QITEM, 0, (void**)&p);
4,408✔
325
  if (code) {
4,408!
326
    stError("s-task:%s failed to create force_window trigger, code:%s", id, tstrerror(code));
×
327
    return code;
×
328
  }
329

330
  p->type = STREAM_INPUT__GET_RES;
4,408✔
331
  p->pBlock = taosMemoryCalloc(1, sizeof(SSDataBlock));
4,408!
332
  if (p->pBlock == NULL) {
4,408!
333
    taosFreeQitem(p);
×
334
    return terrno;
×
335
  }
336

337
  p->pBlock->info.window.skey = skey;
4,408✔
338
  p->pBlock->info.window.ekey = TMAX(ts, skey + pInterval->interval);
4,408✔
339
  p->pBlock->info.type = STREAM_GET_RESULT;
4,408✔
340

341
  stDebug("s-task:%s force_window_close trigger block generated, window range:%" PRId64 "-%" PRId64, id,
4,408!
342
          p->pBlock->info.window.skey, p->pBlock->info.window.ekey);
343

344
  *pTrigger = p;
4,408✔
345
  return code;
4,408✔
346
}
347

348
int32_t streamCreateTriggerBlock(SStreamTrigger** pTrigger, int32_t type, int32_t blockType) {
985✔
349
  QRY_PARAM_CHECK(pTrigger);
985!
350

351
  SStreamTrigger* p = NULL;
985✔
352
  int32_t         code = taosAllocateQitem(sizeof(SStreamTrigger), DEF_QITEM, 0, (void**)&p);
985✔
353
  if (code) {
985!
354
    return code;
×
355
  }
356

357
  p->type = (int8_t) type;
985✔
358
  p->pBlock = taosMemoryCalloc(1, sizeof(SSDataBlock));
985!
359
  if (p->pBlock == NULL) {
985!
360
    taosFreeQitem(p);
×
361
    return terrno;
×
362
  }
363

364
  p->pBlock->info.type = blockType;
985✔
365
  *pTrigger = p;
985✔
366
  return code;
985✔
367
}
368

369
int32_t streamCreateRecalculateBlock(SStreamTask* pTask, SStreamDataBlock** pBlock, int32_t type) {
×
370
  int32_t           code = 0;
×
371
  SSDataBlock*      p = NULL;
×
372
  SStreamDataBlock* pRecalc = NULL;
×
373

374
  if (pBlock != NULL) {
×
375
    *pBlock = NULL;
×
376
  }
377

378
  code = taosAllocateQitem(sizeof(SStreamDataBlock), DEF_QITEM, sizeof(SSDataBlock), (void**)&pRecalc);
×
379
  if (code) {
×
380
    return code;
×
381
  }
382

383
  p = taosMemoryCalloc(1, sizeof(SSDataBlock));
×
384
  if (p == NULL) {
×
385
    code = terrno;
×
386
    goto _err;
×
387
  }
388

389
  pRecalc->type = STREAM_INPUT__RECALCULATE;
×
390

391
  p->info.type = type;
×
392
  p->info.rows = 1;
×
393
  p->info.childId = pTask->info.selfChildId;
×
394

395
  pRecalc->blocks = taosArrayInit(4, sizeof(SSDataBlock));  // pBlock;
×
396
  if (pRecalc->blocks == NULL) {
×
397
    code = terrno;
×
398
    goto _err;
×
399
  }
400

401
  void* px = taosArrayPush(pRecalc->blocks, p);
×
402
  if (px == NULL) {
×
403
    code = terrno;
×
404
    goto _err;
×
405
  }
406

407
  taosMemoryFree(p);
×
408
  *pBlock = pRecalc;
×
409

410
  return code;
×
411

412
_err:
×
413
  taosMemoryFree(p);
×
414
  taosFreeQitem(pRecalc);
×
415
  return code;
×
416
}
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