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

taosdata / TDengine / #3858

17 Apr 2025 01:40PM UTC coverage: 62.968% (+0.5%) from 62.513%
#3858

push

travis-ci

web-flow
docs(opc): add perssit data support (#30783)

156194 of 316378 branches covered (49.37%)

Branch coverage included in aggregate %.

242021 of 316027 relevant lines covered (76.58%)

19473613.85 hits per line

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

72.16
/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) {
23,327✔
20
  *pSubmit = NULL;
23,327✔
21

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

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

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

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

44
  if (pSubmit->ver > pMerged->ver) {
445,039✔
45
    pMerged->ver = pSubmit->ver;
445,037✔
46
  }
47
  return 0;
445,039✔
48
}
49

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

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

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

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

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

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

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

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

104
    // TODO: refactor
105
    pDataBlock->info.window.skey = be64toh(pRetrieve->skey);
135,869✔
106
    pDataBlock->info.window.ekey = be64toh(pRetrieve->ekey);
135,870✔
107
    pDataBlock->info.version = be64toh(pRetrieve->version);
135,886✔
108
    pDataBlock->info.watermark = be64toh(pRetrieve->watermark);
135,904✔
109
    memcpy(pDataBlock->info.parTbName, pRetrieve->parTbName, TSDB_TABLE_NAME_LEN);
135,919✔
110

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

116
  pData->blocks = pArray;
57,937✔
117
  *pRes = pData;
57,937✔
118

119
  return code;
57,937✔
120
}
121

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

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

134
  if (pItem == NULL) {
23,042✔
135
    return code;
1,803✔
136
  }
137

138
  if (pItem->type == STREAM_INPUT__DATA_SUBMIT) {
21,239✔
139
    SStreamDataSubmit* pSubmit = (SStreamDataSubmit*)pItem;
1,398✔
140
    (*pBlock)->sourceVer = pSubmit->ver;
1,398✔
141
  } else if (pItem->type == STREAM_INPUT__MERGED_SUBMIT) {
19,841✔
142
    SStreamMergedSubmit* pMerged = (SStreamMergedSubmit*)pItem;
15,273✔
143
    (*pBlock)->sourceVer = pMerged->ver;
15,273✔
144
  }
145

146
  return code;
21,239✔
147
}
148

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

154
  taosArrayDestroyEx(pBlock->blocks, (FDelete)blockDataFreeRes);
46,340✔
155
  taosFreeQitem(pBlock);
46,343✔
156
}
157

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

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

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

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

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

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

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

195
  return code;
513✔
196
}
197

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

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

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

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

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

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

233
    taosArrayDestroy(pBlockSrc->blocks);
25,655✔
234
    streamQueueItemIncSize(dst, streamQueueItemGetSize(pElem));
25,679✔
235

236
    taosFreeQitem(pElem);
25,675✔
237
    *pRes = dst;
25,678✔
238
    return code;
25,678✔
239
  } else if (dst->type == STREAM_INPUT__MERGED_SUBMIT && pElem->type == STREAM_INPUT__DATA_SUBMIT) {
421,938✔
240
    SStreamMergedSubmit* pMerged = (SStreamMergedSubmit*)dst;
398,446✔
241
    SStreamDataSubmit*   pBlockSrc = (SStreamDataSubmit*)pElem;
398,446✔
242

243
    code = streamMergeSubmit(pMerged, pBlockSrc);
398,446✔
244
    streamQueueItemIncSize(dst, streamQueueItemGetSize(pElem));
398,409✔
245

246
    taosFreeQitem(pElem);
398,394✔
247
    *pRes = dst;
398,453✔
248
    *pRes = dst;
398,453✔
249
    return code;
398,453✔
250
  } else if (dst->type == STREAM_INPUT__DATA_SUBMIT && pElem->type == STREAM_INPUT__DATA_SUBMIT) {
23,492!
251
    SStreamMergedSubmit* pMerged = NULL;
23,328✔
252
    code = streamMergedSubmitNew(&pMerged);
23,328✔
253
    if (code != 0) {
23,331!
254
      return code;
×
255
    }
256

257
    streamQueueItemIncSize((SStreamQueueItem*)pMerged, streamQueueItemGetSize(pElem));
23,331✔
258

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

264
    taosFreeQitem(dst);
23,331✔
265
    taosFreeQitem(pElem);
23,340✔
266

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

277
void streamFreeQitem(SStreamQueueItem* data) {
58,657✔
278
  if (data == NULL) {
58,657✔
279
    return;
4✔
280
  }
281

282
  int8_t type = data->type;
58,653✔
283
  if (type == STREAM_INPUT__GET_RES) {
58,653✔
284
    blockDataDestroy(((SStreamTrigger*)data)->pBlock);
5,957✔
285
    taosFreeQitem(data);
5,957✔
286
  } else if (type == STREAM_INPUT__DATA_BLOCK || type == STREAM_INPUT__DATA_RETRIEVE) {
52,696✔
287
    destroyStreamDataBlock((SStreamDataBlock*)data);
2,248✔
288
  } else if (type == STREAM_INPUT__DATA_SUBMIT) {
50,448✔
289
    streamDataSubmitDestroy((SStreamDataSubmit*)data);
6,725✔
290
  } else if (type == STREAM_INPUT__MERGED_SUBMIT) {
43,723✔
291
    SStreamMergedSubmit* pMerge = (SStreamMergedSubmit*)data;
23,342✔
292

293
    int32_t sz = taosArrayGetSize(pMerge->submits);
23,342✔
294
    for (int32_t i = 0; i < sz; i++) {
468,490✔
295
      SPackedData* pSubmit = (SPackedData*)taosArrayGet(pMerge->submits, i);
445,147✔
296
      if (pSubmit == NULL) {
445,138!
297
        continue;
×
298
      }
299
      taosMemoryFree(pSubmit->msgStr);
445,138!
300
    }
301

302
    taosArrayDestroy(pMerge->submits);
23,343✔
303
    taosFreeQitem(pMerge);
23,342✔
304
  } else if (type == STREAM_INPUT__REF_DATA_BLOCK) {
20,381✔
305
    SStreamRefDataBlock* pRefBlock = (SStreamRefDataBlock*)data;
2,408✔
306
    blockDataDestroy(pRefBlock->pBlock);
2,408✔
307
    taosFreeQitem(pRefBlock);
2,408✔
308
  } else if (type == STREAM_INPUT__CHECKPOINT || type == STREAM_INPUT__CHECKPOINT_TRIGGER ||
17,973✔
309
             type == STREAM_INPUT__TRANS_STATE || type == STREAM_INPUT__RECALCULATE) {
14!
310
    SStreamDataBlock* pBlock = (SStreamDataBlock*)data;
17,973✔
311
    taosArrayDestroyEx(pBlock->blocks, freeItems);
17,973✔
312
    taosFreeQitem(pBlock);
17,978✔
313
  }
314
}
315

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

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

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

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

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

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

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

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

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

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

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

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

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

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

389
  pRecalc->type = STREAM_INPUT__RECALCULATE;
14✔
390

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

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

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

407
  taosMemoryFree(p);
14!
408
  *pBlock = pRecalc;
14✔
409

410
  return code;
14✔
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