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

taosdata / TDengine / #3526

10 Nov 2024 03:50AM UTC coverage: 60.225% (-0.6%) from 60.818%
#3526

push

travis-ci

web-flow
Merge pull request #28709 from taosdata/main

merge: from main to 3.0 branch

117031 of 249004 branches covered (47.0%)

Branch coverage included in aggregate %.

130 of 169 new or added lines in 23 files covered. (76.92%)

4149 existing lines in 176 files now uncovered.

197577 of 273386 relevant lines covered (72.27%)

5840219.36 hits per line

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

77.49
/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

18
static int32_t streamMergedSubmitNew(SStreamMergedSubmit** pSubmit) {
6,556✔
19
  *pSubmit = NULL;
6,556✔
20

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

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

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

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

43
  if (pSubmit->ver > pMerged->ver) {
50,239!
44
    pMerged->ver = pSubmit->ver;
50,239✔
45
  }
46
  return 0;
50,239✔
47
}
48

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

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

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

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

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

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

82
    char* pInput = pRetrieve->data + PAYLOAD_PREFIX_LEN;
26,220✔
83
    if (pRetrieve->compressed && compLen < fullLen) {
26,220!
84
      char* p = taosMemoryMalloc(fullLen);
×
85
      if (p == NULL) {
×
86
        return terrno;
×
87
      }
88

89
      int32_t len = tsDecompressString(pInput, compLen, 1, p, fullLen, ONE_STAGE_COMP, NULL, 0);
×
90
      pInput = p;
×
91
    }
92

93
    const char* pDummy = NULL;
26,220✔
94
    code = blockDecode(pDataBlock, pInput, &pDummy);
26,220✔
95
    if (code) {
26,220!
96
      return code;
×
97
    }
98

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

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

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

114
  pData->blocks = pArray;
18,646✔
115
  *pRes = pData;
18,646✔
116

117
  return code;
18,646✔
118
}
119

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

128
  (*pBlock)->srcTaskId = pTask->id.taskId;
8,971✔
129
  (*pBlock)->type = STREAM_INPUT__DATA_BLOCK;
8,971✔
130
  (*pBlock)->blocks = pRes;
8,971✔
131

132
  if (pItem == NULL) {
8,971✔
133
    return code;
1,424✔
134
  }
135

136
  if (pItem->type == STREAM_INPUT__DATA_SUBMIT) {
7,547✔
137
    SStreamDataSubmit* pSubmit = (SStreamDataSubmit*)pItem;
2,100✔
138
    (*pBlock)->sourceVer = pSubmit->ver;
2,100✔
139
  } else if (pItem->type == STREAM_INPUT__MERGED_SUBMIT) {
5,447✔
140
    SStreamMergedSubmit* pMerged = (SStreamMergedSubmit*)pItem;
2,572✔
141
    (*pBlock)->sourceVer = pMerged->ver;
2,572✔
142
  }
143

144
  return code;
7,547✔
145
}
146

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

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

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

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

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

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

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

188
  pDataBlock->info.type = pRetrieve->streamBlockType;
432✔
189

190
  pData->reqId = pReq->reqId;
432✔
191
  pData->blocks = pArray;
432✔
192

193
  return code;
432✔
194
}
195

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

203
  pDataSubmit->ver = pData->ver;
58,317✔
204
  pDataSubmit->submit = *pData;
58,317✔
205
  pDataSubmit->type = type;
58,317✔
206

207
  *pSubmit = pDataSubmit;
58,317✔
208
  return TSDB_CODE_SUCCESS;
58,317✔
209
}
210

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

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

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

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

234
    taosFreeQitem(pElem);
1,698✔
235
    *pRes = dst;
1,698✔
236
    return code;
1,698✔
237
  } else if (dst->type == STREAM_INPUT__MERGED_SUBMIT && pElem->type == STREAM_INPUT__DATA_SUBMIT) {
43,762✔
238
    SStreamMergedSubmit* pMerged = (SStreamMergedSubmit*)dst;
37,132✔
239
    SStreamDataSubmit*   pBlockSrc = (SStreamDataSubmit*)pElem;
37,132✔
240

241
    code = streamMergeSubmit(pMerged, pBlockSrc);
37,132✔
242
    streamQueueItemIncSize(dst, streamQueueItemGetSize(pElem));
37,128✔
243

244
    taosFreeQitem(pElem);
37,127✔
245
    *pRes = dst;
37,132✔
246
    *pRes = dst;
37,132✔
247
    return code;
37,132✔
248
  } else if (dst->type == STREAM_INPUT__DATA_SUBMIT && pElem->type == STREAM_INPUT__DATA_SUBMIT) {
6,630✔
249
    SStreamMergedSubmit* pMerged = NULL;
6,556✔
250
    code = streamMergedSubmitNew(&pMerged);
6,556✔
251
    if (code != 0) {
6,556!
252
      return code;
×
253
    }
254

255
    streamQueueItemIncSize((SStreamQueueItem*)pMerged, streamQueueItemGetSize(pElem));
6,556✔
256

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

262
    taosFreeQitem(dst);
6,556✔
263
    taosFreeQitem(pElem);
6,556✔
264

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

275
void streamFreeQitem(SStreamQueueItem* data) {
30,203✔
276
  int8_t type = data->type;
30,203✔
277
  if (type == STREAM_INPUT__GET_RES) {
30,203✔
278
    blockDataDestroy(((SStreamTrigger*)data)->pBlock);
1,756✔
279
    taosFreeQitem(data);
1,756✔
280
  } else if (type == STREAM_INPUT__DATA_BLOCK || type == STREAM_INPUT__DATA_RETRIEVE) {
28,447✔
281
    destroyStreamDataBlock((SStreamDataBlock*)data);
1,839✔
282
  } else if (type == STREAM_INPUT__DATA_SUBMIT) {
26,608✔
283
    streamDataSubmitDestroy((SStreamDataSubmit*)data);
8,073✔
284
  } else if (type == STREAM_INPUT__MERGED_SUBMIT) {
18,535✔
285
    SStreamMergedSubmit* pMerge = (SStreamMergedSubmit*)data;
6,556✔
286

287
    int32_t sz = taosArrayGetSize(pMerge->submits);
6,556✔
288
    for (int32_t i = 0; i < sz; i++) {
56,800✔
289
      SPackedData* pSubmit = (SPackedData*)taosArrayGet(pMerge->submits, i);
50,244✔
290
      if (pSubmit == NULL) {
50,244!
291
        continue;
×
292
      }
293
      taosMemoryFree(pSubmit->msgStr);
50,244✔
294
    }
295

296
    taosArrayDestroy(pMerge->submits);
6,556✔
297
    taosFreeQitem(pMerge);
6,556✔
298
  } else if (type == STREAM_INPUT__REF_DATA_BLOCK) {
11,979✔
299
    SStreamRefDataBlock* pRefBlock = (SStreamRefDataBlock*)data;
1,399✔
300
    blockDataDestroy(pRefBlock->pBlock);
1,399✔
301
    taosFreeQitem(pRefBlock);
1,399✔
302
  } else if (type == STREAM_INPUT__CHECKPOINT || type == STREAM_INPUT__CHECKPOINT_TRIGGER ||
10,580!
303
             type == STREAM_INPUT__TRANS_STATE) {
304
    SStreamDataBlock* pBlock = (SStreamDataBlock*)data;
10,580✔
305
    taosArrayDestroyEx(pBlock->blocks, freeItems);
10,580✔
306
    taosFreeQitem(pBlock);
10,580✔
307
  }
308
}
30,203✔
309

310
int32_t streamCreateForcewindowTrigger(SStreamTrigger** pTrigger, int32_t trigger, SInterval* pInterval, STimeWindow* pLatestWindow, const char* id) {
676✔
311
  QRY_PARAM_CHECK(pTrigger);
676!
312
  int64_t         ts = INT64_MIN;
676✔
313
  SStreamTrigger* p = NULL;
676✔
314

315
  int32_t code = taosAllocateQitem(sizeof(SStreamTrigger), DEF_QITEM, 0, (void**)&p);
676✔
316
  if (code) {
676!
317
    return code;
×
318
  }
319

320
  p->type = STREAM_INPUT__GET_RES;
676✔
321
  p->pBlock = taosMemoryCalloc(1, sizeof(SSDataBlock));
676✔
322
  if (p->pBlock == NULL) {
676!
323
    taosFreeQitem(p);
×
324
    return terrno;
×
325
  }
326

327
  // let's calculate the previous time window
328
  SInterval interval = {.interval = trigger,
676✔
329
                        .sliding = trigger,
330
                        .intervalUnit = pInterval->intervalUnit,
676✔
331
                        .slidingUnit = pInterval->slidingUnit};
676✔
332

333
  ts = taosGetTimestampMs();
676✔
334

335
  if (pLatestWindow->skey == INT64_MIN) {
676✔
336
    STimeWindow window = getAlignQueryTimeWindow(&interval, ts - trigger);
35✔
337

338
    p->pBlock->info.window.skey = window.skey;
35✔
339
    p->pBlock->info.window.ekey = TMAX(ts, window.ekey);
35✔
340
  } else {
341
    int64_t skey = pLatestWindow->skey + trigger;
641✔
342
    p->pBlock->info.window.skey = skey;
641✔
343
    p->pBlock->info.window.ekey = TMAX(ts, skey + trigger);
641✔
344
  }
345

346
  p->pBlock->info.type = STREAM_GET_RESULT;
676✔
347
  stDebug("s-task:%s force_window_close trigger block generated, window range:%" PRId64 "-%" PRId64, id,
676!
348
          p->pBlock->info.window.skey, p->pBlock->info.window.ekey);
349

350
  *pTrigger = p;
676✔
351
  return code;
676✔
352
}
353

354
int32_t streamCreateSinkResTrigger(SStreamTrigger** pTrigger) {
1,080✔
355
  QRY_PARAM_CHECK(pTrigger);
1,080!
356
  SStreamTrigger* p = NULL;
1,080✔
357

358
  int32_t code = taosAllocateQitem(sizeof(SStreamTrigger), DEF_QITEM, 0, (void**)&p);
1,080✔
359
  if (code) {
1,080!
360
    return code;
×
361
  }
362

363
  p->type = STREAM_INPUT__GET_RES;
1,080✔
364
  p->pBlock = taosMemoryCalloc(1, sizeof(SSDataBlock));
1,080✔
365
  if (p->pBlock == NULL) {
1,080!
366
    taosFreeQitem(p);
×
367
    return terrno;
×
368
  }
369

370
  p->pBlock->info.type = STREAM_GET_ALL;
1,080✔
371
  *pTrigger = p;
1,080✔
372
  return code;
1,080✔
373
}
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