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

taosdata / TDengine / #4131

20 May 2025 07:22AM UTC coverage: 63.096% (+0.7%) from 62.384%
#4131

push

travis-ci

web-flow
docs(datain): add topic meta options docs in tmq (#31147)

157751 of 318088 branches covered (49.59%)

Branch coverage included in aggregate %.

243052 of 317143 relevant lines covered (76.64%)

18743283.33 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) {
13,717✔
20
  *pSubmit = NULL;
13,717✔
21

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

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

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

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

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

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

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

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

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

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

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

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

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

104
    // TODO: refactor
105
    pDataBlock->info.window.skey = be64toh(pRetrieve->skey);
50,286✔
106
    pDataBlock->info.window.ekey = be64toh(pRetrieve->ekey);
50,274✔
107
    pDataBlock->info.version = be64toh(pRetrieve->version);
50,285✔
108
    pDataBlock->info.watermark = be64toh(pRetrieve->watermark);
50,295✔
109
    memcpy(pDataBlock->info.parTbName, pRetrieve->parTbName, TSDB_TABLE_NAME_LEN);
50,292✔
110

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

116
  pData->blocks = pArray;
29,638✔
117
  *pRes = pData;
29,638✔
118

119
  return code;
29,638✔
120
}
121

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

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

134
  if (pItem == NULL) {
14,387✔
135
    return code;
1,465✔
136
  }
137

138
  if (pItem->type == STREAM_INPUT__DATA_SUBMIT) {
12,922✔
139
    SStreamDataSubmit* pSubmit = (SStreamDataSubmit*)pItem;
1,669✔
140
    (*pBlock)->sourceVer = pSubmit->ver;
1,669✔
141
  } else if (pItem->type == STREAM_INPUT__MERGED_SUBMIT) {
11,253✔
142
    SStreamMergedSubmit* pMerged = (SStreamMergedSubmit*)pItem;
6,195✔
143
    (*pBlock)->sourceVer = pMerged->ver;
6,195✔
144
  }
145

146
  return code;
12,922✔
147
}
148

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

154
  taosArrayDestroyEx(pBlock->blocks, (FDelete)blockDataFreeRes);
32,744✔
155
  taosFreeQitem(pBlock);
32,751✔
156
}
157

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

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

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

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

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

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

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

195
  return code;
540✔
196
}
197

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

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

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

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

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

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

233
    taosArrayDestroy(pBlockSrc->blocks);
3,801✔
234
    streamQueueItemIncSize(dst, streamQueueItemGetSize(pElem));
3,803✔
235

236
    taosFreeQitem(pElem);
3,803✔
237
    *pRes = dst;
3,804✔
238
    return code;
3,804✔
239
  } else if (dst->type == STREAM_INPUT__MERGED_SUBMIT && pElem->type == STREAM_INPUT__DATA_SUBMIT) {
116,441!
240
    SStreamMergedSubmit* pMerged = (SStreamMergedSubmit*)dst;
102,539✔
241
    SStreamDataSubmit*   pBlockSrc = (SStreamDataSubmit*)pElem;
102,539✔
242

243
    code = streamMergeSubmit(pMerged, pBlockSrc);
102,539✔
244
    streamQueueItemIncSize(dst, streamQueueItemGetSize(pElem));
102,517✔
245

246
    taosFreeQitem(pElem);
102,511✔
247
    *pRes = dst;
102,545✔
248
    *pRes = dst;
102,545✔
249
    return code;
102,545✔
250
  } else if (dst->type == STREAM_INPUT__DATA_SUBMIT && pElem->type == STREAM_INPUT__DATA_SUBMIT) {
13,902✔
251
    SStreamMergedSubmit* pMerged = NULL;
13,714✔
252
    code = streamMergedSubmitNew(&pMerged);
13,714✔
253
    if (code != 0) {
13,713!
254
      return code;
×
255
    }
256

257
    streamQueueItemIncSize((SStreamQueueItem*)pMerged, streamQueueItemGetSize(pElem));
13,713✔
258

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

264
    taosFreeQitem(dst);
13,710✔
265
    taosFreeQitem(pElem);
13,719✔
266

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

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

282
  int8_t type = data->type;
47,118✔
283
  if (type == STREAM_INPUT__GET_RES) {
47,118✔
284
    blockDataDestroy(((SStreamTrigger*)data)->pBlock);
5,395✔
285
    taosFreeQitem(data);
5,395✔
286
  } else if (type == STREAM_INPUT__DATA_BLOCK || type == STREAM_INPUT__DATA_RETRIEVE) {
41,723✔
287
    destroyStreamDataBlock((SStreamDataBlock*)data);
2,566✔
288
  } else if (type == STREAM_INPUT__DATA_SUBMIT) {
39,157✔
289
    streamDataSubmitDestroy((SStreamDataSubmit*)data);
7,183✔
290
  } else if (type == STREAM_INPUT__MERGED_SUBMIT) {
31,974✔
291
    SStreamMergedSubmit* pMerge = (SStreamMergedSubmit*)data;
13,715✔
292

293
    int32_t sz = taosArrayGetSize(pMerge->submits);
13,715✔
294
    for (int32_t i = 0; i < sz; i++) {
143,686✔
295
      SPackedData* pSubmit = (SPackedData*)taosArrayGet(pMerge->submits, i);
129,963✔
296
      if (pSubmit == NULL) {
129,955!
297
        continue;
×
298
      }
299
      taosMemoryFree(pSubmit->msgStr);
129,955!
300
    }
301

302
    taosArrayDestroy(pMerge->submits);
13,723✔
303
    taosFreeQitem(pMerge);
13,718✔
304
  } else if (type == STREAM_INPUT__REF_DATA_BLOCK) {
18,259✔
305
    SStreamRefDataBlock* pRefBlock = (SStreamRefDataBlock*)data;
2,686✔
306
    blockDataDestroy(pRefBlock->pBlock);
2,686✔
307
    taosFreeQitem(pRefBlock);
2,686✔
308
  } else if (type == STREAM_INPUT__CHECKPOINT || type == STREAM_INPUT__CHECKPOINT_TRIGGER ||
15,573!
309
             type == STREAM_INPUT__TRANS_STATE || type == STREAM_INPUT__RECALCULATE) {
×
310
    SStreamDataBlock* pBlock = (SStreamDataBlock*)data;
15,573✔
311
    taosArrayDestroyEx(pBlock->blocks, freeItems);
15,573✔
312
    taosFreeQitem(pBlock);
15,583✔
313
  }
314
}
315

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

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

324
  int32_t code = taosAllocateQitem(sizeof(SStreamTrigger), DEF_QITEM, 0, (void**)&p);
4,378✔
325
  if (code) {
4,378!
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,378✔
331
  p->pBlock = taosMemoryCalloc(1, sizeof(SSDataBlock));
4,378!
332
  if (p->pBlock == NULL) {
4,378!
333
    taosFreeQitem(p);
×
334
    return terrno;
×
335
  }
336

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

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

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

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

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

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

364
  p->pBlock->info.type = blockType;
1,017✔
365
  *pTrigger = p;
1,017✔
366
  return code;
1,017✔
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