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

taosdata / TDengine / #3796

31 Mar 2025 10:39AM UTC coverage: 30.372% (-7.1%) from 37.443%
#3796

push

travis-ci

happyguoxy
test:add test cases

69287 of 309062 branches covered (22.42%)

Branch coverage included in aggregate %.

118044 of 307720 relevant lines covered (38.36%)

278592.15 hits per line

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

0.0
/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) {
×
20
  *pSubmit = NULL;
×
21

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

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

34
  (*pSubmit)->type = STREAM_INPUT__MERGED_SUBMIT;
×
35
  return TSDB_CODE_SUCCESS;
×
36
}
37

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

44
  if (pSubmit->ver > pMerged->ver) {
×
45
    pMerged->ver = pSubmit->ver;
×
46
  }
47
  return 0;
×
48
}
49

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

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

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

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

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

80
    int32_t compLen = *(int32_t*)pRetrieve->data;
×
81
    int32_t fullLen = *(int32_t*)(pRetrieve->data + sizeof(int32_t));
×
82

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

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

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

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

116
  pData->blocks = pArray;
×
117
  *pRes = pData;
×
118

119
  return code;
×
120
}
121

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

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

134
  if (pItem == NULL) {
×
135
    return code;
×
136
  }
137

138
  if (pItem->type == STREAM_INPUT__DATA_SUBMIT) {
×
139
    SStreamDataSubmit* pSubmit = (SStreamDataSubmit*)pItem;
×
140
    (*pBlock)->sourceVer = pSubmit->ver;
×
141
  } else if (pItem->type == STREAM_INPUT__MERGED_SUBMIT) {
×
142
    SStreamMergedSubmit* pMerged = (SStreamMergedSubmit*)pItem;
×
143
    (*pBlock)->sourceVer = pMerged->ver;
×
144
  }
145

146
  return code;
×
147
}
148

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

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

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

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

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

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

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

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

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

195
  return code;
×
196
}
197

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

205
  pDataSubmit->ver = pData->ver;
×
206
  pDataSubmit->submit = *pData;
×
207
  pDataSubmit->type = type;
×
208

209
  *pSubmit = pDataSubmit;
×
210
  return TSDB_CODE_SUCCESS;
×
211
}
212

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

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

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

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

236
    taosFreeQitem(pElem);
×
237
    *pRes = dst;
×
238
    return code;
×
239
  } else if (dst->type == STREAM_INPUT__MERGED_SUBMIT && pElem->type == STREAM_INPUT__DATA_SUBMIT) {
×
240
    SStreamMergedSubmit* pMerged = (SStreamMergedSubmit*)dst;
×
241
    SStreamDataSubmit*   pBlockSrc = (SStreamDataSubmit*)pElem;
×
242

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

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

257
    streamQueueItemIncSize((SStreamQueueItem*)pMerged, streamQueueItemGetSize(pElem));
×
258

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

264
    taosFreeQitem(dst);
×
265
    taosFreeQitem(pElem);
×
266

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

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

282
  int8_t type = data->type;
×
283
  if (type == STREAM_INPUT__GET_RES) {
×
284
    blockDataDestroy(((SStreamTrigger*)data)->pBlock);
×
285
    taosFreeQitem(data);
×
286
  } else if (type == STREAM_INPUT__DATA_BLOCK || type == STREAM_INPUT__DATA_RETRIEVE) {
×
287
    destroyStreamDataBlock((SStreamDataBlock*)data);
×
288
  } else if (type == STREAM_INPUT__DATA_SUBMIT) {
×
289
    streamDataSubmitDestroy((SStreamDataSubmit*)data);
×
290
  } else if (type == STREAM_INPUT__MERGED_SUBMIT) {
×
291
    SStreamMergedSubmit* pMerge = (SStreamMergedSubmit*)data;
×
292

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

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

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

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

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

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

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

344
  *pTrigger = p;
×
345
  return code;
×
346
}
347

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

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

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

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