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

taosdata / TDengine / #3646

12 Mar 2025 12:34PM UTC coverage: 28.375% (-27.8%) from 56.156%
#3646

push

travis-ci

web-flow
Merge pull request #30119 from taosdata/ciup30

ci: Update workflow to fix param issue of run_tdgpt_test

59085 of 286935 branches covered (20.59%)

Branch coverage included in aggregate %.

102775 of 283490 relevant lines covered (36.25%)

55149.72 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
  }
114

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

118
  return code;
×
119
}
120

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

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

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

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

145
  return code;
×
146
}
147

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

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

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

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

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

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

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

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

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

194
  return code;
×
195
}
196

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

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

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

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

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

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

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

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

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

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

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

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

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

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

276
void streamFreeQitem(SStreamQueueItem* data) {
×
277
  int8_t type = data->type;
×
278
  if (type == STREAM_INPUT__GET_RES) {
×
279
    blockDataDestroy(((SStreamTrigger*)data)->pBlock);
×
280
    taosFreeQitem(data);
×
281
  } else if (type == STREAM_INPUT__DATA_BLOCK || type == STREAM_INPUT__DATA_RETRIEVE) {
×
282
    destroyStreamDataBlock((SStreamDataBlock*)data);
×
283
  } else if (type == STREAM_INPUT__DATA_SUBMIT) {
×
284
    streamDataSubmitDestroy((SStreamDataSubmit*)data);
×
285
  } else if (type == STREAM_INPUT__MERGED_SUBMIT) {
×
286
    SStreamMergedSubmit* pMerge = (SStreamMergedSubmit*)data;
×
287

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

297
    taosArrayDestroy(pMerge->submits);
×
298
    taosFreeQitem(pMerge);
×
299
  } else if (type == STREAM_INPUT__REF_DATA_BLOCK) {
×
300
    SStreamRefDataBlock* pRefBlock = (SStreamRefDataBlock*)data;
×
301
    blockDataDestroy(pRefBlock->pBlock);
×
302
    taosFreeQitem(pRefBlock);
×
303
  } else if (type == STREAM_INPUT__CHECKPOINT || type == STREAM_INPUT__CHECKPOINT_TRIGGER ||
×
304
             type == STREAM_INPUT__TRANS_STATE) {
305
    SStreamDataBlock* pBlock = (SStreamDataBlock*)data;
×
306
    taosArrayDestroyEx(pBlock->blocks, freeItems);
×
307
    taosFreeQitem(pBlock);
×
308
  }
309
}
×
310

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

315
  SStreamTrigger* p = NULL;
×
316
  int64_t         ts = taosGetTimestamp(pInterval->precision);
×
317
  int64_t         skey = pLatestWindow->skey + interval;
×
318

319
  int32_t code = taosAllocateQitem(sizeof(SStreamTrigger), DEF_QITEM, 0, (void**)&p);
×
320
  if (code) {
×
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;
×
326
  p->pBlock = taosMemoryCalloc(1, sizeof(SSDataBlock));
×
327
  if (p->pBlock == NULL) {
×
328
    taosFreeQitem(p);
×
329
    return terrno;
×
330
  }
331

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

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

339
  *pTrigger = p;
×
340
  return code;
×
341
}
342

343
int32_t streamCreateSinkResTrigger(SStreamTrigger** pTrigger) {
×
344
  QRY_PARAM_CHECK(pTrigger);
×
345
  SStreamTrigger* p = NULL;
×
346

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

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

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