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

taosdata / TDengine / #3608

12 Feb 2025 05:57AM UTC coverage: 63.066% (+1.4%) from 61.715%
#3608

push

travis-ci

web-flow
Merge pull request #29746 from taosdata/merge/mainto3.02

merge: from main to 3.0 branch

140199 of 286257 branches covered (48.98%)

Branch coverage included in aggregate %.

89 of 161 new or added lines in 18 files covered. (55.28%)

3211 existing lines in 190 files now uncovered.

218998 of 283298 relevant lines covered (77.3%)

5949310.66 hits per line

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

73.24
/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) {
6,551✔
20
  *pSubmit = NULL;
6,551✔
21

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

115
  pData->blocks = pArray;
18,464✔
116
  *pRes = pData;
18,464✔
117

118
  return code;
18,464✔
119
}
120

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

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

133
  if (pItem == NULL) {
9,136✔
134
    return code;
1,431✔
135
  }
136

137
  if (pItem->type == STREAM_INPUT__DATA_SUBMIT) {
7,705✔
138
    SStreamDataSubmit* pSubmit = (SStreamDataSubmit*)pItem;
1,460✔
139
    (*pBlock)->sourceVer = pSubmit->ver;
1,460✔
140
  } else if (pItem->type == STREAM_INPUT__MERGED_SUBMIT) {
6,245✔
141
    SStreamMergedSubmit* pMerged = (SStreamMergedSubmit*)pItem;
2,794✔
142
    (*pBlock)->sourceVer = pMerged->ver;
2,794✔
143
  }
144

145
  return code;
7,705✔
146
}
147

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

153
  taosArrayDestroyEx(pBlock->blocks, (FDelete)blockDataFreeRes);
20,078✔
154
  taosFreeQitem(pBlock);
20,078✔
155
}
156

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

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

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

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

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

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

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

194
  return code;
447✔
195
}
196

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

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

208
  *pSubmit = pDataSubmit;
56,061✔
209
  return TSDB_CODE_SUCCESS;
56,061✔
210
}
211

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

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

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

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

235
    taosFreeQitem(pElem);
1,896✔
236
    *pRes = dst;
1,896✔
237
    return code;
1,896✔
238
  } else if (dst->type == STREAM_INPUT__MERGED_SUBMIT && pElem->type == STREAM_INPUT__DATA_SUBMIT) {
44,821✔
239
    SStreamMergedSubmit* pMerged = (SStreamMergedSubmit*)dst;
38,134✔
240
    SStreamDataSubmit*   pBlockSrc = (SStreamDataSubmit*)pElem;
38,134✔
241

242
    code = streamMergeSubmit(pMerged, pBlockSrc);
38,134✔
243
    streamQueueItemIncSize(dst, streamQueueItemGetSize(pElem));
38,134✔
244

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

256
    streamQueueItemIncSize((SStreamQueueItem*)pMerged, streamQueueItemGetSize(pElem));
6,551✔
257

258
    code = streamMergeSubmit(pMerged, (SStreamDataSubmit*)dst);
6,551✔
259
    if (code == 0) {
6,551!
260
      code = streamMergeSubmit(pMerged, (SStreamDataSubmit*)pElem);
6,551✔
261
    }
262

263
    taosFreeQitem(dst);
6,551✔
264
    taosFreeQitem(pElem);
6,551✔
265

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

276
void streamFreeQitem(SStreamQueueItem* data) {
29,097✔
277
  int8_t type = data->type;
29,097✔
278
  if (type == STREAM_INPUT__GET_RES) {
29,097✔
279
    blockDataDestroy(((SStreamTrigger*)data)->pBlock);
4,231✔
280
    taosFreeQitem(data);
4,231✔
281
  } else if (type == STREAM_INPUT__DATA_BLOCK || type == STREAM_INPUT__DATA_RETRIEVE) {
24,866✔
282
    destroyStreamDataBlock((SStreamDataBlock*)data);
1,799✔
283
  } else if (type == STREAM_INPUT__DATA_SUBMIT) {
23,067✔
284
    streamDataSubmitDestroy((SStreamDataSubmit*)data);
4,825✔
285
  } else if (type == STREAM_INPUT__MERGED_SUBMIT) {
18,242✔
286
    SStreamMergedSubmit* pMerge = (SStreamMergedSubmit*)data;
6,551✔
287

288
    int32_t sz = taosArrayGetSize(pMerge->submits);
6,551✔
289
    for (int32_t i = 0; i < sz; i++) {
57,787✔
290
      SPackedData* pSubmit = (SPackedData*)taosArrayGet(pMerge->submits, i);
51,236✔
291
      if (pSubmit == NULL) {
51,236!
292
        continue;
×
293
      }
294
      taosMemoryFree(pSubmit->msgStr);
51,236!
295
    }
296

297
    taosArrayDestroy(pMerge->submits);
6,551✔
298
    taosFreeQitem(pMerge);
6,551✔
299
  } else if (type == STREAM_INPUT__REF_DATA_BLOCK) {
11,691✔
300
    SStreamRefDataBlock* pRefBlock = (SStreamRefDataBlock*)data;
1,404✔
301
    blockDataDestroy(pRefBlock->pBlock);
1,404✔
302
    taosFreeQitem(pRefBlock);
1,404✔
303
  } else if (type == STREAM_INPUT__CHECKPOINT || type == STREAM_INPUT__CHECKPOINT_TRIGGER ||
10,287!
304
             type == STREAM_INPUT__TRANS_STATE) {
305
    SStreamDataBlock* pBlock = (SStreamDataBlock*)data;
10,287✔
306
    taosArrayDestroyEx(pBlock->blocks, freeItems);
10,287✔
307
    taosFreeQitem(pBlock);
10,287✔
308
  }
309
}
29,097✔
310

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

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

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

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

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

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

339
  *pTrigger = p;
3,182✔
340
  return code;
3,182✔
341
}
342

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

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

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

359
  p->pBlock->info.type = STREAM_GET_ALL;
1,049✔
360
  *pTrigger = p;
1,049✔
361
  return code;
1,049✔
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