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

taosdata / TDengine / #3561

19 Dec 2024 03:15AM UTC coverage: 58.812% (-1.3%) from 60.124%
#3561

push

travis-ci

web-flow
Merge pull request #29213 from taosdata/merge/mainto3.0

merge: from main to 3.0 branch

130770 of 287658 branches covered (45.46%)

Branch coverage included in aggregate %.

32 of 78 new or added lines in 4 files covered. (41.03%)

7347 existing lines in 166 files now uncovered.

205356 of 283866 relevant lines covered (72.34%)

7187865.64 hits per line

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

63.98
/source/dnode/vnode/src/tq/tqSink.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 "tcommon.h"
17
#include "tq.h"
18

19
#define IS_NEW_SUBTB_RULE(_t) (((_t)->ver >= SSTREAM_TASK_SUBTABLE_CHANGED_VER) && ((_t)->subtableWithoutMd5 != 1))
20

21
typedef struct STableSinkInfo {
22
  uint64_t uid;
23
  tstr     name;
24
} STableSinkInfo;
25

26
static bool    hasOnlySubmitData(const SArray* pBlocks, int32_t numOfBlocks);
27
static int32_t tsAscendingSortFn(const void* p1, const void* p2);
28
static int32_t setDstTableDataUid(SVnode* pVnode, SStreamTask* pTask, SSDataBlock* pDataBlock, char* stbFullName,
29
                                  SSubmitTbData* pTableData);
30
static int32_t doBuildAndSendDeleteMsg(SVnode* pVnode, char* stbFullName, SSDataBlock* pDataBlock, SStreamTask* pTask,
31
                                       int64_t suid);
32
static int32_t doBuildAndSendSubmitMsg(SVnode* pVnode, SStreamTask* pTask, SSubmitReq2* pReq, int32_t numOfBlocks);
33
static int32_t buildSubmitMsgImpl(SSubmitReq2* pSubmitReq, int32_t vgId, void** pMsg, int32_t* msgLen);
34
static int32_t doConvertRows(SSubmitTbData* pTableData, const STSchema* pTSchema, SSDataBlock* pDataBlock,
35
                             int64_t earlyTs, const char* id);
36
static int32_t doWaitForDstTableCreated(SVnode* pVnode, SStreamTask* pTask, STableSinkInfo* pTableSinkInfo,
37
                                        const char* dstTableName, int64_t* uid);
38

39
static bool    isValidDstChildTable(SMetaReader* pReader, int32_t vgId, const char* ctbName, int64_t suid);
40
static int32_t initCreateTableMsg(SVCreateTbReq* pCreateTableReq, uint64_t suid, const char* stbFullName,
41
                                  int32_t numOfTags);
42
static int32_t createDefaultTagColName(SArray** pColNameList);
43
static int32_t setCreateTableMsgTableName(SVCreateTbReq* pCreateTableReq, SSDataBlock* pDataBlock,
44
                                          const char* stbFullName, int64_t gid, bool newSubTableRule);
45
static int32_t doCreateSinkTableInfo(const char* pDstTableName, STableSinkInfo** pInfo);
46
static int32_t doPutSinkTableInfoIntoCache(SSHashObj* pSinkTableMap, STableSinkInfo* pTableSinkInfo, uint64_t groupId,
47
                                           const char* id);
48
static bool    doGetSinkTableInfoFromCache(SSHashObj* pTableInfoMap, uint64_t groupId, STableSinkInfo** pInfo);
49
static int32_t doRemoveSinkTableInfoInCache(SSHashObj* pSinkTableMap, uint64_t groupId, const char* id);
50
static int32_t checkTagSchema(SStreamTask* pTask, SVnode* pVnode);
51
static void    rebuildAndSendMultiResBlock(SStreamTask* pTask, const SArray* pBlocks, SVnode* pVnode, int64_t earlyTs);
52
static int32_t handleResultBlockMsg(SStreamTask* pTask, SSDataBlock* pDataBlock, int32_t index, SVnode* pVnode,
53
                                    int64_t earlyTs);
54
static int32_t doWaitForDstTableDropped(SVnode* pVnode, SStreamTask* pTask, const char* dstTableName);
55

56
int32_t tqBuildDeleteReq(STQ* pTq, const char* stbFullName, const SSDataBlock* pDataBlock, SBatchDeleteReq* deleteReq,
2,603✔
57
                         const char* pIdStr, bool newSubTableRule) {
58
  int32_t          totalRows = pDataBlock->info.rows;
2,603✔
59
  SColumnInfoData* pStartTsCol = taosArrayGet(pDataBlock->pDataBlock, START_TS_COLUMN_INDEX);
2,603✔
60
  SColumnInfoData* pEndTsCol = taosArrayGet(pDataBlock->pDataBlock, END_TS_COLUMN_INDEX);
2,602✔
61
  SColumnInfoData* pGidCol = taosArrayGet(pDataBlock->pDataBlock, GROUPID_COLUMN_INDEX);
2,602✔
62
  SColumnInfoData* pTbNameCol = taosArrayGet(pDataBlock->pDataBlock, TABLE_NAME_COLUMN_INDEX);
2,601✔
63

64
  if (pStartTsCol == NULL || pEndTsCol == NULL || pGidCol == NULL || pTbNameCol == NULL) {
2,601!
65
    return terrno;
×
66
  }
67

68
  tqDebug("s-task:%s build %d rows delete msg for table:%s", pIdStr, totalRows, stbFullName);
2,601✔
69

70
  for (int32_t row = 0; row < totalRows; row++) {
5,992✔
71
    int64_t skey = *(int64_t*)colDataGetData(pStartTsCol, row);
3,392!
72
    int64_t ekey = *(int64_t*)colDataGetData(pEndTsCol, row);
3,392!
73
    int64_t groupId = *(int64_t*)colDataGetData(pGidCol, row);
3,392!
74

75
    char* name = NULL;
3,392✔
76
    char* originName = NULL;
3,392✔
77
    void* varTbName = NULL;
3,392✔
78
    if (!colDataIsNull(pTbNameCol, totalRows, row, NULL)) {
6,784!
79
      varTbName = colDataGetVarData(pTbNameCol, row);
2,050✔
80
    }
81

82
    if (varTbName != NULL && varTbName != (void*)-1) {
5,444!
83
      size_t cap = TMAX(TSDB_TABLE_NAME_LEN, varDataLen(varTbName) + 1);
2,050✔
84
      name = taosMemoryMalloc(cap);
2,050!
85
      if (name == NULL) {
2,054!
86
        return terrno;
×
87
      }
88

89
      memcpy(name, varDataVal(varTbName), varDataLen(varTbName));
2,054✔
90
      name[varDataLen(varTbName)] = '\0';
2,054✔
91
      if (newSubTableRule && !isAutoTableName(name) && !alreadyAddGroupId(name, groupId) && groupId != 0 &&
2,054!
92
          stbFullName) {
93
        int32_t code = buildCtbNameAddGroupId(stbFullName, name, groupId, cap);
1,935✔
94
        if (code != TSDB_CODE_SUCCESS) {
1,934!
95
          return code;
×
96
        }
97
      }
98
    } else if (stbFullName) {
1,342✔
99
      int32_t code = buildCtbNameByGroupId(stbFullName, groupId, &name);
723✔
100
      if (code) {
723!
101
        return code;
×
102
      }
103
    } else {
104
      originName = taosMemoryCalloc(1, TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE);
619!
105
      if (originName == NULL) {
616!
106
        return terrno;
×
107
      }
108

109
      if (metaGetTableNameByUid(pTq->pVnode, groupId, originName) == 0) {
616!
110
        name = varDataVal(originName);
616✔
111
      }
112
    }
113

114
    if (!name || *name == '\0') {
3,391!
115
      tqWarn("s-task:%s failed to build delete msg groupId:%" PRId64 ", skey:%" PRId64 " ekey:%" PRId64
×
116
             " since invalid tbname:%s",
117
             pIdStr, groupId, skey, ekey, name ? name : "NULL");
118
    } else {
119
      tqDebug("s-task:%s build delete msg groupId:%" PRId64 ", name:%s, skey:%" PRId64 " ekey:%" PRId64, pIdStr,
3,391✔
120
              groupId, name, skey, ekey);
121

122
      SSingleDeleteReq req = {.startTs = skey, .endTs = ekey};
3,392✔
123
      tstrncpy(req.tbname, name, TSDB_TABLE_NAME_LEN);
3,392✔
124
      void* p = taosArrayPush(deleteReq->deleteReqs, &req);
3,392✔
125
      if (p == NULL) {
3,393!
126
        return terrno;
×
127
      }
128
    }
129

130
    if (originName) {
3,393✔
131
      name = originName;
616✔
132
    }
133

134
    taosMemoryFreeClear(name);
3,393!
135
  }
136

137
  return 0;
2,600✔
138
}
139

140
static int32_t encodeCreateChildTableForRPC(void* pReqs, int32_t vgId, void** pBuf, int32_t* contLen) {
6,662✔
141
  int32_t ret = 0;
6,662✔
142

143
  tEncodeSize(tEncodeSVCreateTbBatchReq, pReqs, *contLen, ret);
6,662!
144
  if (ret < 0) {
6,654!
145
    ret = -1;
×
146
    goto end;
×
147
  }
148
  *contLen += sizeof(SMsgHead);
6,654✔
149
  *pBuf = rpcMallocCont(*contLen);
6,654✔
150
  if (NULL == *pBuf) {
6,663!
151
    ret = -1;
×
152
    goto end;
×
153
  }
154
  ((SMsgHead*)(*pBuf))->vgId = vgId;
6,663✔
155
  ((SMsgHead*)(*pBuf))->contLen = htonl(*contLen);
6,663✔
156
  SEncoder coder = {0};
6,663✔
157
  tEncoderInit(&coder, POINTER_SHIFT(*pBuf, sizeof(SMsgHead)), (*contLen) - sizeof(SMsgHead));
6,663✔
158
  if (tEncodeSVCreateTbBatchReq(&coder, pReqs) < 0) {
6,661!
159
    rpcFreeCont(*pBuf);
×
160
    *pBuf = NULL;
×
161
    *contLen = 0;
×
162
    tEncoderClear(&coder);
×
163
    ret = -1;
×
164
    goto end;
×
165
  }
166
  tEncoderClear(&coder);
6,663✔
167

168
end:
6,664✔
169
  return ret;
6,664✔
170
}
171

172
static int32_t encodeDropChildTableForRPC(void* pReqs, int32_t vgId, void** ppBuf, int32_t *contLen) {
55✔
173
  int32_t  code = 0;
55✔
174
  SEncoder ec = {0};
55✔
175
  tEncodeSize(tEncodeSVDropTbBatchReq, pReqs, *contLen, code);
55!
176
  if (code < 0) {
55!
177
    code = TSDB_CODE_INVALID_MSG;
×
178
    goto end;
×
179
  }
180
  *contLen += sizeof(SMsgHead);
55✔
181
  *ppBuf = rpcMallocCont(*contLen);
55✔
182

183
  if (!*ppBuf) {
55!
184
    code = terrno;
×
185
    goto end;
×
186
  }
187

188
  ((SMsgHead*)(*ppBuf))->vgId = vgId;
55✔
189
  ((SMsgHead*)(*ppBuf))->contLen = htonl(*contLen);
55✔
190

191
  tEncoderInit(&ec, POINTER_SHIFT(*ppBuf, sizeof(SMsgHead)), (*contLen) - sizeof(SMsgHead));
55✔
192
  code = tEncodeSVDropTbBatchReq(&ec, pReqs);
55✔
193
  tEncoderClear(&ec);
55✔
194
  if (code < 0) {
55!
195
    rpcFreeCont(*ppBuf);
×
196
    *ppBuf = NULL;
×
197
    *contLen = 0;
×
198
    code = TSDB_CODE_INVALID_MSG;
×
199
    goto end;
×
200
  }
201
end:
55✔
202
  return code;
55✔
203
}
204

205
static int32_t tqPutReqToQueue(SVnode* pVnode, void* pReqs, int32_t(*encoder)(void* pReqs, int32_t vgId, void** ppBuf, int32_t *contLen), tmsg_t msgType) {
6,717✔
206
  void*   buf = NULL;
6,717✔
207
  int32_t tlen = 0;
6,717✔
208

209
  int32_t code = encoder(pReqs, TD_VID(pVnode), &buf, &tlen);
6,717✔
210
  if (code) {
6,719!
211
    tqError("vgId:%d failed to encode create table msg, create table failed, code:%s", TD_VID(pVnode), tstrerror(code));
×
212
    return code;
×
213
  }
214

215
  SRpcMsg msg = {.msgType = msgType, .pCont = buf, .contLen = tlen};
6,719✔
216
  code = tmsgPutToQueue(&pVnode->msgCb, WRITE_QUEUE, &msg);
6,719✔
217
  if (code) {
6,719!
218
    tqError("failed to put into write-queue since %s", terrstr());
×
219
  }
220

221
  return code;
6,718✔
222
}
223

224
int32_t initCreateTableMsg(SVCreateTbReq* pCreateTableReq, uint64_t suid, const char* stbFullName, int32_t numOfTags) {
17,579✔
225
  pCreateTableReq->flags = 0;
17,579✔
226
  pCreateTableReq->type = TSDB_CHILD_TABLE;
17,579✔
227
  pCreateTableReq->ctb.suid = suid;
17,579✔
228

229
  // set super table name
230
  SName name = {0};
17,579✔
231

232
  int32_t code = tNameFromString(&name, stbFullName, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
17,579✔
233
  if (code == 0) {
17,580!
234
    pCreateTableReq->ctb.stbName = taosStrdup((char*)tNameGetTableName(&name));
17,580!
235
    if (pCreateTableReq->ctb.stbName == NULL) {  // ignore this error code
17,578!
236
      tqError("failed to duplicate the stb name:%s, failed to init create-table msg and create req table", stbFullName);
×
237
      code = terrno;
×
238
    }
239
  }
240

241
  pCreateTableReq->ctb.tagNum = numOfTags;
17,578✔
242
  return code;
17,578✔
243
}
244

245
int32_t createDefaultTagColName(SArray** pColNameList) {
13,260✔
246
  *pColNameList = NULL;
13,260✔
247

248
  SArray* pTagColNameList = taosArrayInit(1, TSDB_COL_NAME_LEN);
13,260✔
249
  if (pTagColNameList == NULL) {
13,261!
250
    return terrno;
×
251
  }
252

253
  char  tagNameStr[TSDB_COL_NAME_LEN] = "group_id";
13,261✔
254
  void* p = taosArrayPush(pTagColNameList, tagNameStr);
13,260✔
255
  if (p == NULL) {
13,260!
256
    taosArrayDestroy(pTagColNameList);
×
257
    return terrno;
×
258
  }
259

260
  *pColNameList = pTagColNameList;
13,260✔
261
  return TSDB_CODE_SUCCESS;
13,260✔
262
}
263

264
int32_t setCreateTableMsgTableName(SVCreateTbReq* pCreateTableReq, SSDataBlock* pDataBlock, const char* stbFullName,
17,579✔
265
                                   int64_t gid, bool newSubTableRule) {
266
  if (pDataBlock->info.parTbName[0]) {
17,579✔
267
    if (newSubTableRule && !isAutoTableName(pDataBlock->info.parTbName) &&
17,558✔
268
        !alreadyAddGroupId(pDataBlock->info.parTbName, gid) && gid != 0 && stbFullName) {
2,973!
269
      pCreateTableReq->name = taosMemoryCalloc(1, TSDB_TABLE_NAME_LEN);
16!
270
      if (pCreateTableReq->name == NULL) {
16!
271
        return terrno;
×
272
      }
273

274
      tstrncpy(pCreateTableReq->name, pDataBlock->info.parTbName, TSDB_TABLE_NAME_LEN);
16✔
275
      int32_t code = buildCtbNameAddGroupId(stbFullName, pCreateTableReq->name, gid, TSDB_TABLE_NAME_LEN);
16✔
276
      if (code != TSDB_CODE_SUCCESS) {
16!
277
        return code;
×
278
      }
279
      //      tqDebug("gen name from:%s", pDataBlock->info.parTbName);
280
    } else {
281
      pCreateTableReq->name = taosStrdup(pDataBlock->info.parTbName);
17,543!
282
      if (pCreateTableReq->name == NULL) {
17,540!
283
        return terrno;
×
284
      }
285
      //      tqDebug("copy name:%s", pDataBlock->info.parTbName);
286
    }
287
  } else {
288
    int32_t code = buildCtbNameByGroupId(stbFullName, gid, &pCreateTableReq->name);
21✔
289
    return code;
22✔
290
  }
291

292
  return 0;
17,556✔
293
}
294

295
static int32_t doBuildAndSendCreateTableMsg(SVnode* pVnode, char* stbFullName, SSDataBlock* pDataBlock,
6,661✔
296
                                            SStreamTask* pTask, int64_t suid) {
297
  STSchema*          pTSchema = pTask->outputInfo.tbSink.pTSchema;
6,661✔
298
  int32_t            rows = pDataBlock->info.rows;
6,661✔
299
  SArray*            tagArray = NULL;
6,661✔
300
  const char*        id = pTask->id.idStr;
6,661✔
301
  int32_t            vgId = pTask->pMeta->vgId;
6,661✔
302
  int32_t            code = 0;
6,661✔
303
  STableSinkInfo*    pInfo = NULL;
6,661✔
304
  SVCreateTbBatchReq reqs = {0};
6,661✔
305
  SArray*            crTblArray = NULL;
6,661✔
306

307
  tqDebug("s-task:%s build create %d table(s) msg", id, rows);
6,661✔
308

309
  tagArray = taosArrayInit(4, sizeof(STagVal));
6,661✔
310
  crTblArray = reqs.pArray = taosArrayInit(1, sizeof(SVCreateTbReq));
6,664✔
311
  if ((NULL == reqs.pArray) || (tagArray == NULL)) {
6,664!
312
    tqError("s-task:%s failed to init create table msg, code:%s", id, tstrerror(terrno));
1!
313
    code = terrno;
1✔
314
    goto _end;
×
315
  }
316

317
  for (int32_t rowId = 0; rowId < rows; rowId++) {
13,326✔
318
    SVCreateTbReq* pCreateTbReq = &((SVCreateTbReq){0});
6,663✔
319

320
    int32_t size = taosArrayGetSize(pDataBlock->pDataBlock);
6,663✔
321
    int32_t numOfTags = TMAX(size - UD_TAG_COLUMN_INDEX, 1);
6,664✔
322

323
    code = initCreateTableMsg(pCreateTbReq, suid, stbFullName, numOfTags);
6,664✔
324
    if (code) {
6,661!
325
      tqError("s-task:%s vgId:%d failed to init create table msg", id, vgId);
×
326
      continue;
×
327
    }
328

329
    taosArrayClear(tagArray);
6,661✔
330

331
    if (size == 2) {
6,661✔
332
      STagVal tagVal = {
2,342✔
333
          .cid = pTSchema->numOfCols + 1, .type = TSDB_DATA_TYPE_UBIGINT, .i64 = pDataBlock->info.id.groupId};
2,342✔
334

335
      void* p = taosArrayPush(tagArray, &tagVal);
2,344✔
336
      if (p == NULL) {
2,344!
337
        return terrno;
×
338
      }
339

340
      code = createDefaultTagColName(&pCreateTbReq->ctb.tagName);
2,344✔
341
      if (code) {
2,343!
342
        return code;
×
343
      }
344
    } else {
345
      for (int32_t tagId = UD_TAG_COLUMN_INDEX, step = 1; tagId < size; tagId++, step++) {
46,830✔
346
        SColumnInfoData* pTagData = taosArrayGet(pDataBlock->pDataBlock, tagId);
42,513✔
347
        if (pTagData == NULL) {
42,510!
348
          continue;
18,332✔
349
        }
350

351
        STagVal tagVal = {.cid = pTSchema->numOfCols + step, .type = pTagData->info.type};
42,510✔
352
        void*   pData = colDataGetData(pTagData, rowId);
42,510!
353
        if (colDataIsNull_s(pTagData, rowId)) {
85,020!
354
          continue;
18,332✔
355
        } else if (IS_VAR_DATA_TYPE(pTagData->info.type)) {
24,178!
356
          tagVal.nData = varDataLen(pData);
7,660✔
357
          tagVal.pData = (uint8_t*)varDataVal(pData);
7,660✔
358
        } else {
359
          memcpy(&tagVal.i64, pData, pTagData->info.bytes);
16,518✔
360
        }
361
        void* p = taosArrayPush(tagArray, &tagVal);
24,179✔
362
        if (p == NULL) {
24,179!
363
          code = terrno;
×
364
          goto _end;
×
365
        }
366
      }
367
    }
368

369
    code = tTagNew(tagArray, 1, false, (STag**)&pCreateTbReq->ctb.pTag);
6,660✔
370
    taosArrayDestroy(tagArray);
6,663✔
371
    tagArray = NULL;
6,662✔
372

373
    if (pCreateTbReq->ctb.pTag == NULL || (code != 0)) {
6,662!
374
      tdDestroySVCreateTbReq(pCreateTbReq);
375
      code = TSDB_CODE_OUT_OF_MEMORY;
×
376
      goto _end;
×
377
    }
378

379
    uint64_t gid = pDataBlock->info.id.groupId;
6,662✔
380
    if (taosArrayGetSize(pDataBlock->pDataBlock) > UD_GROUPID_COLUMN_INDEX) {
6,662!
381
      SColumnInfoData* pGpIdColInfo = taosArrayGet(pDataBlock->pDataBlock, UD_GROUPID_COLUMN_INDEX);
6,662✔
382
      if (pGpIdColInfo == NULL) {
6,662!
383
        continue;
×
384
      }
385

386
      // todo remove this
387
      void* pGpIdData = colDataGetData(pGpIdColInfo, rowId);
6,662!
388
      if (gid != *(int64_t*)pGpIdData) {
6,662!
389
        tqError("s-task:%s vgId:%d invalid groupId:%" PRId64 " actual:%" PRId64 " in sink task", id, vgId, gid,
×
390
                *(int64_t*)pGpIdData);
391
      }
392
    }
393

394
    code = setCreateTableMsgTableName(pCreateTbReq, pDataBlock, stbFullName, gid, IS_NEW_SUBTB_RULE(pTask));
6,663!
395
    if (code) {
6,661!
396
      goto _end;
×
397
    }
398

399
    void* p = taosArrayPush(reqs.pArray, pCreateTbReq);
6,661✔
400
    if (p == NULL) {
6,662!
401
      code = terrno;
×
402
      goto _end;
×
403
    }
404

405
    bool alreadyCached = doGetSinkTableInfoFromCache(pTask->outputInfo.tbSink.pTbInfo, gid, &pInfo);
6,662✔
406
    if (!alreadyCached) {
6,662✔
407
      code = doCreateSinkTableInfo(pCreateTbReq->name, &pInfo);
5,564✔
408
      if (code) {
5,564!
409
        tqError("vgId:%d failed to create sink tableInfo for table:%s, s-task:%s", vgId, pCreateTbReq->name, id);
×
410
        continue;
×
411
      }
412

413
      code = doPutSinkTableInfoIntoCache(pTask->outputInfo.tbSink.pTbInfo, pInfo, gid, id);
5,564✔
414
      if (code) {
5,565!
415
        tqError("vgId:%d failed to put sink tableInfo:%s into cache, s-task:%s", vgId, pCreateTbReq->name, id);
×
416
      }
417
    }
418

419
    tqDebug("s-task:%s build create table:%s msg complete", id, pCreateTbReq->name);
6,663✔
420
  }
421

422
  reqs.nReqs = taosArrayGetSize(reqs.pArray);
6,663✔
423
  code = tqPutReqToQueue(pVnode, &reqs, encodeCreateChildTableForRPC, TDMT_VND_CREATE_TABLE);
6,662✔
424
  if (code != TSDB_CODE_SUCCESS) {
6,663!
425
    tqError("s-task:%s failed to send create table msg", id);
×
426
  }
427

428
_end:
6,663✔
429
  taosArrayDestroy(tagArray);
6,663✔
430
  taosArrayDestroyEx(crTblArray, (FDelete)tdDestroySVCreateTbReq);
6,663✔
431
  return code;
6,664✔
432
}
433

434
static int32_t doBuildAndSendDropTableMsg(SVnode* pVnode, char* pStbFullname, SSDataBlock* pDataBlock,
55✔
435
                                          SStreamTask* pTask, int64_t suid) {
436
  int32_t          lino = 0;
55✔
437
  int32_t          code = 0;
55✔
438
  int32_t          rows = pDataBlock->info.rows;
55✔
439
  const char*      id = pTask->id.idStr;
55✔
440
  SVDropTbBatchReq batchReq = {0};
55✔
441
  SVDropTbReq      req = {0};
55✔
442

443
  if (rows <= 0 || rows > 1 || pTask->subtableWithoutMd5 == 0) return TSDB_CODE_SUCCESS;
55!
444

445
  batchReq.pArray = taosArrayInit(rows, sizeof(SVDropTbReq));
55✔
446
  if (!batchReq.pArray) return terrno;
55!
447
  batchReq.nReqs = rows;
55✔
448
  req.suid = suid;
55✔
449
  req.igNotExists = true;
55✔
450

451
  SColumnInfoData* pTbNameCol = taosArrayGet(pDataBlock->pDataBlock, TABLE_NAME_COLUMN_INDEX);
55✔
452
  char             tbName[TSDB_TABLE_NAME_LEN + 1] = {0};
55✔
453
  int32_t          i = 0;
55✔
454
  void*            pData = colDataGetVarData(pTbNameCol, i);
55✔
455
  memcpy(tbName, varDataVal(pData), varDataLen(pData));
55✔
456
  tbName[varDataLen(pData) + 1] = 0;
55✔
457
  req.name = tbName;
55✔
458
  if (taosArrayPush(batchReq.pArray, &req) == NULL) {
110!
459
    TSDB_CHECK_CODE(terrno, lino, _exit);
×
460
  }
461

462
  SMetaReader mr = {0};
55✔
463
  metaReaderDoInit(&mr, pVnode->pMeta, META_READER_LOCK);
55✔
464

465
  code = metaGetTableEntryByName(&mr, tbName);
55✔
466
  if (TSDB_CODE_SUCCESS == code && isValidDstChildTable(&mr, TD_VID(pVnode), tbName, pTask->outputInfo.tbSink.stbUid)) {
55!
467
    STableSinkInfo* pTableSinkInfo = NULL;
55✔
468
    bool alreadyCached = doGetSinkTableInfoFromCache(pTask->outputInfo.tbSink.pTbInfo, pDataBlock->info.id.groupId, &pTableSinkInfo);
55✔
469
    if (alreadyCached) {
55✔
470
      pTableSinkInfo->uid = mr.me.uid;
26✔
471
    }
472
  }
473
  metaReaderClear(&mr);
55✔
474
  tqDebug("s-task:%s build drop %d table(s) msg", id, rows);
55!
475
  code = tqPutReqToQueue(pVnode, &batchReq, encodeDropChildTableForRPC, TDMT_VND_DROP_TABLE);
55✔
476
  TSDB_CHECK_CODE(code, lino, _exit);
55!
477

478

479
  code = doWaitForDstTableDropped(pVnode, pTask, tbName);
55✔
480
  TSDB_CHECK_CODE(code, lino, _exit);
55!
481

482
_exit:
55✔
483
  if (batchReq.pArray) {
55!
484
    taosArrayDestroy(batchReq.pArray);
55✔
485
  }
486
  return code;
55✔
487
}
488

489
int32_t doBuildAndSendSubmitMsg(SVnode* pVnode, SStreamTask* pTask, SSubmitReq2* pReq, int32_t numOfBlocks) {
19,366✔
490
  const char* id = pTask->id.idStr;
19,366✔
491
  int32_t     vgId = TD_VID(pVnode);
19,366✔
492
  int32_t     len = 0;
19,366✔
493
  void*       pBuf = NULL;
19,366✔
494
  int32_t     numOfFinalBlocks = taosArrayGetSize(pReq->aSubmitTbData);
19,366✔
495

496
  int32_t code = buildSubmitMsgImpl(pReq, vgId, &pBuf, &len);
19,367✔
497
  if (code != TSDB_CODE_SUCCESS) {
19,370!
498
    tqError("s-task:%s build submit msg failed, vgId:%d, code:%s", id, vgId, tstrerror(code));
×
499
    return code;
×
500
  }
501

502
  SRpcMsg msg = {.msgType = TDMT_VND_SUBMIT, .pCont = pBuf, .contLen = len};
19,370✔
503
  code = tmsgPutToQueue(&pVnode->msgCb, WRITE_QUEUE, &msg);
19,370✔
504
  if (code == TSDB_CODE_SUCCESS) {
19,369!
505
    tqDebug("s-task:%s vgId:%d comp %d blocks into %d and send to dstTable(s) completed", id, vgId, numOfBlocks,
19,369✔
506
            numOfFinalBlocks);
507
  } else {
UNCOV
508
    tqError("s-task:%s failed to put into write-queue since %s", id, terrstr());
×
509
  }
510

511
  SSinkRecorder* pRec = &pTask->execInfo.sink;
19,370✔
512

513
  pRec->numOfSubmit += 1;
19,370✔
514
  if ((pRec->numOfSubmit % 1000) == 0) {
19,370✔
515
    double el = (taosGetTimestampMs() - pTask->execInfo.readyTs) / 1000.0;
3✔
516
    tqInfo("s-task:%s vgId:%d write %" PRId64 " blocks (%" PRId64 " rows) in %" PRId64
3!
517
           " submit into dst table, %.2fMiB duration:%.2f Sec.",
518
           id, vgId, pRec->numOfBlocks, pRec->numOfRows, pRec->numOfSubmit, SIZE_IN_MiB(pRec->dataSize), el);
519
  }
520

521
  return TSDB_CODE_SUCCESS;
19,370✔
522
}
523

524
// merge the new submit table block with the existed blocks
525
// if ts in the new data block overlap with existed one, replace it
526
int32_t doMergeExistedRows(SSubmitTbData* pExisted, const SSubmitTbData* pNew, const char* id) {
42,672✔
527
  int32_t oldLen = taosArrayGetSize(pExisted->aRowP);
42,672✔
528
  int32_t newLen = taosArrayGetSize(pNew->aRowP);
42,672✔
529
  int32_t numOfPk = 0;
42,672✔
530

531
  int32_t j = 0, k = 0;
42,672✔
532
  SArray* pFinal = taosArrayInit(oldLen + newLen, POINTER_BYTES);
42,672✔
533
  if (pFinal == NULL) {
42,673!
534
    tqError("s-task:%s failed to prepare merge result datablock, code:%s", id, tstrerror(terrno));
×
535
    return terrno;
×
536
  }
537

538
  while (j < newLen && k < oldLen) {
516,151✔
539
    SRow* pNewRow = *(SRow**)TARRAY_GET_ELEM(pNew->aRowP, j);
473,478✔
540
    SRow* pOldRow = *(SRow**)TARRAY_GET_ELEM(pExisted->aRowP, k);
473,478✔
541

542
    if (pNewRow->ts < pOldRow->ts) {
473,478✔
543
      void* p = taosArrayPush(pFinal, &pNewRow);
43✔
544
      if (p == NULL) {
43!
545
        return terrno;
×
546
      }
547
      j += 1;
43✔
548
    } else if (pNewRow->ts > pOldRow->ts) {
473,435✔
549
      void* p = taosArrayPush(pFinal, &pOldRow);
431,246✔
550
      if (p == NULL) {
431,246!
551
        return terrno;
×
552
      }
553

554
      k += 1;
431,246✔
555
    } else {
556
      // check for the existance of primary key
557
      if (pNewRow->numOfPKs == 0) {
42,189!
558
        void* p = taosArrayPush(pFinal, &pNewRow);
42,189✔
559
        if (p == NULL) {
42,189!
560
          return terrno;
×
561
        }
562

563
        k += 1;
42,189✔
564
        j += 1;
42,189✔
565
        tRowDestroy(pOldRow);
42,189✔
566
      } else {
567
        numOfPk = pNewRow->numOfPKs;
×
568

569
        SRowKey kNew, kOld;
570
        tRowGetKey(pNewRow, &kNew);
×
571
        tRowGetKey(pOldRow, &kOld);
×
572

573
        int32_t ret = tRowKeyCompare(&kNew, &kOld);
×
574
        if (ret <= 0) {
×
575
          void* p = taosArrayPush(pFinal, &pNewRow);
×
576
          if (p == NULL) {
×
577
            return terrno;
×
578
          }
579

580
          j += 1;
×
581

582
          if (ret == 0) {
×
583
            k += 1;
×
584
            tRowDestroy(pOldRow);
×
585
          }
586
        } else {
587
          void* p = taosArrayPush(pFinal, &pOldRow);
×
588
          if (p == NULL) {
×
589
            return terrno;
×
590
          }
591

592
          k += 1;
×
593
        }
594
      }
595
    }
596
  }
597

598
  while (j < newLen) {
212,022✔
599
    SRow* pRow = *(SRow**)TARRAY_GET_ELEM(pNew->aRowP, j++);
169,350✔
600
    void* p = taosArrayPush(pFinal, &pRow);
169,349✔
601
    if (p == NULL) {
169,349!
602
      return terrno;
×
603
    }
604
  }
605

606
  while (k < oldLen) {
43,429✔
607
    SRow* pRow = *(SRow**)TARRAY_GET_ELEM(pExisted->aRowP, k++);
758✔
608
    void* p = taosArrayPush(pFinal, &pRow);
757✔
609
    if (p == NULL) {
757!
610
      return terrno;
×
611
    }
612
  }
613

614
  taosArrayDestroy(pNew->aRowP);
42,671✔
615
  taosArrayDestroy(pExisted->aRowP);
42,672✔
616
  pExisted->aRowP = pFinal;
42,673✔
617

618
  tqTrace("s-task:%s rows merged, final rows:%d, pk:%d uid:%" PRId64 ", existed auto-create table:%d, new-block:%d", id,
42,673!
619
          (int32_t)taosArrayGetSize(pFinal), numOfPk, pExisted->uid, (pExisted->pCreateTbReq != NULL),
620
          (pNew->pCreateTbReq != NULL));
621

622
  tdDestroySVCreateTbReq(pNew->pCreateTbReq);
42,673!
623
  taosMemoryFree(pNew->pCreateTbReq);
42,673!
624
  return TSDB_CODE_SUCCESS;
42,673✔
625
}
626

627
bool isValidDstChildTable(SMetaReader* pReader, int32_t vgId, const char* ctbName, int64_t suid) {
30,926✔
628
  if (pReader->me.type != TSDB_CHILD_TABLE) {
30,926!
629
    tqError("vgId:%d, failed to write into %s, since table type:%d incorrect", vgId, ctbName, pReader->me.type);
×
630
    terrno = TSDB_CODE_TDB_INVALID_TABLE_TYPE;
×
631
    return false;
×
632
  }
633

634
  if (pReader->me.ctbEntry.suid != suid) {
30,926!
635
    tqError("vgId:%d, failed to write into %s, since suid mismatch, expect suid:%" PRId64 ", actual:%" PRId64, vgId,
×
636
            ctbName, suid, pReader->me.ctbEntry.suid);
637
    terrno = TSDB_CODE_TDB_TABLE_IN_OTHER_STABLE;
×
638
    return false;
×
639
  }
640

641
  terrno = 0;
30,926✔
642
  return true;
30,926✔
643
}
644

645
int32_t buildAutoCreateTableReq(const char* stbFullName, int64_t suid, int32_t numOfCols, SSDataBlock* pDataBlock,
10,917✔
646
                                SArray* pTagArray, bool newSubTableRule, SVCreateTbReq** pReq) {
647
  *pReq = NULL;
10,917✔
648

649
  SVCreateTbReq* pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
10,917!
650
  if (pCreateTbReq == NULL) {
10,917!
651
    return terrno;
×
652
  }
653

654
  taosArrayClear(pTagArray);
10,917✔
655
  int32_t code = initCreateTableMsg(pCreateTbReq, suid, stbFullName, 1);
10,917✔
656
  if (code != 0) {
10,917!
657
    return code;
×
658
  }
659

660
  STagVal tagVal = {.cid = numOfCols, .type = TSDB_DATA_TYPE_UBIGINT, .i64 = pDataBlock->info.id.groupId};
10,917✔
661
  void* p = taosArrayPush(pTagArray, &tagVal);
10,917✔
662
  if (p == NULL) {
10,917!
663
    return terrno;
×
664
  }
665

666
  code = tTagNew(pTagArray, 1, false, (STag**)&pCreateTbReq->ctb.pTag);
10,917✔
667
  if (pCreateTbReq->ctb.pTag == NULL || (code != 0)) {
10,917!
668
    tdDestroySVCreateTbReq(pCreateTbReq);
UNCOV
669
    taosMemoryFreeClear(pCreateTbReq);
×
UNCOV
670
    return code;
×
671
  }
672

673
  code = createDefaultTagColName(&pCreateTbReq->ctb.tagName);
10,917✔
674
  if (code) {
10,917!
675
    return code;
×
676
  }
677

678
  // set table name
679
  code = setCreateTableMsgTableName(pCreateTbReq, pDataBlock, stbFullName, pDataBlock->info.id.groupId, newSubTableRule);
10,917✔
680
  if (code) {
10,917!
681
    return code;
×
682
  }
683

684
  *pReq = pCreateTbReq;
10,917✔
685
  return code;
10,917✔
686
}
687

688
int32_t buildSubmitMsgImpl(SSubmitReq2* pSubmitReq, int32_t vgId, void** pMsg, int32_t* msgLen) {
19,366✔
689
  int32_t code = 0;
19,366✔
690
  void*   pBuf = NULL;
19,366✔
691
  *msgLen = 0;
19,366✔
692

693
  // encode
694
  int32_t len = 0;
19,366✔
695
  tEncodeSize(tEncodeSubmitReq, pSubmitReq, len, code);
19,366!
696

697
  SEncoder encoder = {0};
19,367✔
698
  len += sizeof(SSubmitReq2Msg);
19,367✔
699

700
  pBuf = rpcMallocCont(len);
19,367✔
701
  if (NULL == pBuf) {
19,362!
702
    tDestroySubmitReq(pSubmitReq, TSDB_MSG_FLG_ENCODE);
×
703
    return terrno;
×
704
  }
705

706
  ((SSubmitReq2Msg*)pBuf)->header.vgId = vgId;
19,362✔
707
  ((SSubmitReq2Msg*)pBuf)->header.contLen = htonl(len);
19,362✔
708
  ((SSubmitReq2Msg*)pBuf)->version = htobe64(1);
19,362✔
709

710
  tEncoderInit(&encoder, POINTER_SHIFT(pBuf, sizeof(SSubmitReq2Msg)), len - sizeof(SSubmitReq2Msg));
19,366✔
711
  if (tEncodeSubmitReq(&encoder, pSubmitReq) < 0) {
19,366!
712
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
713
    tqError("failed to encode submit req, code:%s, ignore and continue", terrstr());
×
714
    tEncoderClear(&encoder);
×
715
    rpcFreeCont(pBuf);
×
716
    tDestroySubmitReq(pSubmitReq, TSDB_MSG_FLG_ENCODE);
×
717
    return code;
×
718
  }
719

720
  tEncoderClear(&encoder);
19,367✔
721
  tDestroySubmitReq(pSubmitReq, TSDB_MSG_FLG_ENCODE);
19,368✔
722

723
  *msgLen = len;
19,370✔
724
  *pMsg = pBuf;
19,370✔
725
  return TSDB_CODE_SUCCESS;
19,370✔
726
}
727

728
int32_t tsAscendingSortFn(const void* p1, const void* p2) {
42,684,318✔
729
  SRow* pRow1 = *(SRow**)p1;
42,684,318✔
730
  SRow* pRow2 = *(SRow**)p2;
42,684,318✔
731

732
  if (pRow1->ts == pRow2->ts) {
42,684,318!
733
    return 0;
×
734
  } else {
735
    return pRow1->ts > pRow2->ts ? 1 : -1;
42,684,318!
736
  }
737
}
738

739
int32_t doConvertRows(SSubmitTbData* pTableData, const STSchema* pTSchema, SSDataBlock* pDataBlock, int64_t earlyTs,
106,259✔
740
                      const char* id) {
741
  int32_t numOfRows = pDataBlock->info.rows;
106,259✔
742
  int32_t code = TSDB_CODE_SUCCESS;
106,259✔
743

744
  SArray* pVals = taosArrayInit(pTSchema->numOfCols, sizeof(SColVal));
106,259✔
745
  pTableData->aRowP = taosArrayInit(numOfRows, sizeof(SRow*));
106,261✔
746

747
  if (pTableData->aRowP == NULL || pVals == NULL) {
106,262!
UNCOV
748
    taosArrayDestroy(pTableData->aRowP);
×
749
    pTableData->aRowP = NULL;
×
750
    taosArrayDestroy(pVals);
×
751
    code = terrno;
×
752
    tqError("s-task:%s failed to prepare write stream res blocks, code:%s", id, tstrerror(code));
×
753
    return code;
×
754
  }
755

756
  for (int32_t j = 0; j < numOfRows; j++) {
8,938,263✔
757
    taosArrayClear(pVals);
8,831,732✔
758

759
    int32_t dataIndex = 0;
8,831,605✔
760
    int64_t ts = 0;
8,831,605✔
761

762
    for (int32_t k = 0; k < pTSchema->numOfCols; k++) {
70,374,016✔
763
      const STColumn* pCol = &pTSchema->columns[k];
61,532,090✔
764

765
      // primary timestamp column, for debug purpose
766
      if (k == 0) {
61,532,090✔
767
        SColumnInfoData* pColData = taosArrayGet(pDataBlock->pDataBlock, dataIndex);
8,831,093✔
768
        if (pColData == NULL) {
8,829,737!
769
          continue;
×
770
        }
771

772
        ts = *(int64_t*)colDataGetData(pColData, j);
8,829,737!
773
        tqTrace("s-task:%s sink row %d, col %d ts %" PRId64, id, j, k, ts);
8,829,737✔
774

775
        if (ts < earlyTs) {
8,827,467!
776
          tqError("s-task:%s ts:%" PRId64 " of generated results out of valid time range %" PRId64 " , discarded", id,
×
777
                  ts, earlyTs);
778
          taosArrayDestroy(pTableData->aRowP);
×
779
          pTableData->aRowP = NULL;
×
780
          taosArrayDestroy(pVals);
×
781
          return TSDB_CODE_SUCCESS;
×
782
        }
783
      }
784

785
      if (IS_SET_NULL(pCol)) {
61,528,464✔
786
        if (pCol->flags & COL_IS_KEY) {
3,707!
787
          qError("ts:%" PRId64 " primary key column should not be null, colId:%" PRIi16 ", colType:%" PRIi8, ts,
×
788
                 pCol->colId, pCol->type);
789
          break;
×
790
        }
791
        SColVal cv = COL_VAL_NULL(pCol->colId, pCol->type);
3,707✔
792
        void* p = taosArrayPush(pVals, &cv);
3,707✔
793
        if (p == NULL) {
3,707!
794
          return terrno;
×
795
        }
796
      } else {
797
        SColumnInfoData* pColData = taosArrayGet(pDataBlock->pDataBlock, dataIndex);
61,524,757✔
798
        if (pColData == NULL) {
61,522,269!
799
          continue;
×
800
        }
801

802
        if (colDataIsNull_s(pColData, j)) {
123,044,538✔
803
          if (pCol->flags & COL_IS_KEY) {
5,833,918!
804
            qError("ts:%" PRId64 " primary key column should not be null, colId:%" PRIi16 ", colType:%" PRIi8,
×
805
                   ts, pCol->colId, pCol->type);
806
            break;
×
807
          }
808

809
          SColVal cv = COL_VAL_NULL(pCol->colId, pCol->type);
5,833,918✔
810
          void* p = taosArrayPush(pVals, &cv);
5,833,941✔
811
          if (p == NULL) {
5,833,941!
812
            return terrno;
×
813
          }
814

815
          dataIndex++;
5,833,941✔
816
        } else {
817
          void* colData = colDataGetData(pColData, j);
55,688,351!
818
          if (IS_VAR_DATA_TYPE(pCol->type)) {  // address copy, no value
61,523,005!
819
            SValue sv =
5,842,986✔
820
                (SValue){.type = pCol->type, .nData = varDataLen(colData), .pData = (uint8_t*)varDataVal(colData)};
5,842,986✔
821
            SColVal cv = COL_VAL_VALUE(pCol->colId, sv);
5,842,986✔
822
            void* p = taosArrayPush(pVals, &cv);
5,834,654✔
823
            if (p == NULL) {
5,834,654!
824
              return terrno;
×
825
            }
826
          } else {
827
            SValue sv = {.type = pCol->type};
49,845,365✔
828
            memcpy(&sv.val, colData, tDataTypes[pCol->type].bytes);
49,845,365✔
829
            SColVal cv = COL_VAL_VALUE(pCol->colId, sv);
49,845,365✔
830
            void* p = taosArrayPush(pVals, &cv);
49,870,109✔
831
            if (p == NULL) {
49,870,109!
832
              return terrno;
×
833
            }
834
          }
835
          dataIndex++;
55,704,763✔
836
        }
837
      }
838
    }
839

840
    SRow* pRow = NULL;
8,841,926✔
841
    code = tRowBuild(pVals, (STSchema*)pTSchema, &pRow);
8,841,926✔
842
    if (code != TSDB_CODE_SUCCESS) {
8,833,654!
843
      tDestroySubmitTbData(pTableData, TSDB_MSG_FLG_ENCODE);
×
844
      taosArrayDestroy(pVals);
×
845
      tqError("s-task:%s build rows for submit failed, ts:%"PRId64, id, ts);
×
846
      return code;
×
847
    }
848

849
    void* p = taosArrayPush(pTableData->aRowP, &pRow);
8,833,654✔
850
    if (p == NULL) {
8,832,001!
851
      return terrno;
×
852
    }
853
  }
854

855
  taosArrayDestroy(pVals);
106,531✔
856
  return TSDB_CODE_SUCCESS;
106,257✔
857
}
858

859
int32_t doWaitForDstTableCreated(SVnode* pVnode, SStreamTask* pTask, STableSinkInfo* pTableSinkInfo,
6,328✔
860
                                 const char* dstTableName, int64_t* uid) {
861
  int32_t     vgId = TD_VID(pVnode);
6,328✔
862
  int64_t     suid = pTask->outputInfo.tbSink.stbUid;
6,328✔
863
  const char* id = pTask->id.idStr;
6,328✔
864

865
  while (pTableSinkInfo->uid == 0) {
9,880!
866
    if (streamTaskShouldStop(pTask)) {
9,880✔
867
      tqDebug("s-task:%s task will stop, quit from waiting for table:%s create", id, dstTableName);
7!
868
      return TSDB_CODE_STREAM_EXEC_CANCELLED;
6,328✔
869
    }
870

871
    // wait for the table to be created
872
    SMetaReader mr = {0};
9,873✔
873
    metaReaderDoInit(&mr, pVnode->pMeta, META_READER_LOCK);
9,873✔
874

875
    int32_t code = metaGetTableEntryByName(&mr, dstTableName);
9,873✔
876
    if (code == 0) {  // table already exists, check its type and uid
9,873✔
877
      bool isValid = isValidDstChildTable(&mr, vgId, dstTableName, suid);
6,320✔
878
      if (isValid) {  // not valid table, ignore it
6,320!
879
        tqDebug("s-task:%s set uid:%" PRIu64 " for dstTable:%s from meta", id, mr.me.uid, pTableSinkInfo->name.data);
6,320✔
880
        // set the destination table uid
881
        (*uid) = mr.me.uid;
6,319✔
882
        pTableSinkInfo->uid = mr.me.uid;
6,319✔
883
      }
884

885
      metaReaderClear(&mr);
6,319✔
886
      return terrno;
6,321✔
887
    } else {  // not exist, wait and retry
888
      metaReaderClear(&mr);
3,553✔
889
      taosMsleep(100);
3,552✔
890
      tqDebug("s-task:%s wait 100ms for the table:%s ready before insert data", id, dstTableName);
3,552✔
891
    }
892
  }
893

UNCOV
894
  return TSDB_CODE_SUCCESS;
×
895
}
896

897
static int32_t doWaitForDstTableDropped(SVnode* pVnode, SStreamTask* pTask, const char* dstTableName) {
55✔
898
  int32_t vgId = TD_VID(pVnode);
55✔
899
  int64_t suid = pTask->outputInfo.tbSink.stbUid;
55✔
900
  const char* id = pTask->id.idStr;
55✔
901

902
  while (1) {
55✔
903
    if (streamTaskShouldStop(pTask)) {
110!
904
      tqDebug("s-task:%s task will stop, quit from waiting for table:%s drop", id, dstTableName);
×
905
      return TSDB_CODE_STREAM_EXEC_CANCELLED;
×
906
    }
907
    SMetaReader mr = {0};
110✔
908
    metaReaderDoInit(&mr, pVnode->pMeta, META_READER_LOCK);
110✔
909
    int32_t code = metaGetTableEntryByName(&mr, dstTableName);
110✔
910
    if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST) {
110✔
911
      metaReaderClear(&mr);
55✔
912
      break;
55✔
913
    } else if (TSDB_CODE_SUCCESS == code) {
55!
914
      if (isValidDstChildTable(&mr, vgId, dstTableName, suid)) {
55!
915
        metaReaderClear(&mr);
55✔
916
        taosMsleep(100);
55✔
917
        tqDebug("s-task:%s wait 100ms for table:%s drop", id, dstTableName);
55!
918
      } else {
919
        metaReaderClear(&mr);
×
920
        break;
×
921
      }
922
    } else {
923
      tqError("s-task:%s failed to wait for table:%s drop", id, dstTableName);
×
924
      metaReaderClear(&mr);
×
925
      return terrno;
×
926
    }
927
  }
928
  return TSDB_CODE_SUCCESS;
55✔
929
}
930

931
int32_t doCreateSinkTableInfo(const char* pDstTableName, STableSinkInfo** pInfo) {
40,971✔
932
  int32_t nameLen = strlen(pDstTableName);
40,971✔
933
  (*pInfo) = taosMemoryCalloc(1, sizeof(STableSinkInfo) + nameLen + 1);
40,971!
934
  if (*pInfo == NULL) {
40,974!
935
    return terrno;
×
936
  }
937

938
  (*pInfo)->name.len = nameLen;
40,974✔
939
  memcpy((*pInfo)->name.data, pDstTableName, nameLen);
40,974✔
940
  return TSDB_CODE_SUCCESS;
40,974✔
941
}
942

943
int32_t setDstTableDataUid(SVnode* pVnode, SStreamTask* pTask, SSDataBlock* pDataBlock, char* stbFullName,
63,587✔
944
                           SSubmitTbData* pTableData) {
945
  uint64_t        groupId = pDataBlock->info.id.groupId;
63,587✔
946
  char*           dstTableName = pDataBlock->info.parTbName;
63,587✔
947
  int32_t         numOfRows = pDataBlock->info.rows;
63,587✔
948
  const char*     id = pTask->id.idStr;
63,587✔
949
  int64_t         suid = pTask->outputInfo.tbSink.stbUid;
63,587✔
950
  STSchema*       pTSchema = pTask->outputInfo.tbSink.pTSchema;
63,587✔
951
  int32_t         vgId = TD_VID(pVnode);
63,587✔
952
  STableSinkInfo* pTableSinkInfo = NULL;
63,587✔
953
  int32_t         code = 0;
63,587✔
954

955
  bool alreadyCached = doGetSinkTableInfoFromCache(pTask->outputInfo.tbSink.pTbInfo, groupId, &pTableSinkInfo);
63,587✔
956

957
  if (alreadyCached) {
63,590✔
958
    if (dstTableName[0] == 0) {  // data block does not set the destination table name
28,181✔
959
      tstrncpy(dstTableName, pTableSinkInfo->name.data, pTableSinkInfo->name.len + 1);
1,029✔
960
      tqDebug("s-task:%s vgId:%d, gropuId:%" PRIu64 " datablock table name is null, set name:%s", id, vgId, groupId,
1,029!
961
              dstTableName);
962
    } else {
963
      tstrncpy(dstTableName, pTableSinkInfo->name.data, pTableSinkInfo->name.len + 1);
27,152✔
964
      if (pTableSinkInfo->uid != 0) {
27,152✔
965
        tqDebug("s-task:%s write %d rows into groupId:%" PRIu64 " dstTable:%s(uid:%" PRIu64 ")", id, numOfRows, groupId,
21,029✔
966
                dstTableName, pTableSinkInfo->uid);
967
      } else {
968
        tqDebug("s-task:%s write %d rows into groupId:%" PRIu64 " dstTable:%s(not set uid yet for the secondary block)",
6,123✔
969
                id, numOfRows, groupId, dstTableName);
970
      }
971
    }
972
  } else {  // this groupId has not been kept in cache yet
973
    if (dstTableName[0] == 0) {
35,409✔
974
      memset(dstTableName, 0, TSDB_TABLE_NAME_LEN);
248✔
975
      code = buildCtbNameByGroupIdImpl(stbFullName, groupId, dstTableName);
248✔
976
      if (code) {
248!
977
        tqDebug("s-task:%s failed to build auto create table-name:%s, groupId:0x%" PRId64, id, dstTableName, groupId);
×
978
        return code;
×
979
      }
980
    } else {
981
      if (pTask->subtableWithoutMd5 != 1 && !isAutoTableName(dstTableName) &&
35,161!
UNCOV
982
          !alreadyAddGroupId(dstTableName, groupId) && groupId != 0) {
×
983
        tqDebug("s-task:%s append groupId:%" PRId64 " for generated dstTable:%s", id, groupId, dstTableName);
×
984
        if (pTask->ver == SSTREAM_TASK_SUBTABLE_CHANGED_VER) {
×
985
          code = buildCtbNameAddGroupId(NULL, dstTableName, groupId, sizeof(pDataBlock->info.parTbName));
×
986
        } else if (pTask->ver > SSTREAM_TASK_SUBTABLE_CHANGED_VER && stbFullName) {
×
987
          code = buildCtbNameAddGroupId(stbFullName, dstTableName, groupId, sizeof(pDataBlock->info.parTbName));
×
988
        }
989
        if (code != TSDB_CODE_SUCCESS) {
×
990
          return code;
×
991
        }
992
      }
993
    }
994

995
    code = doCreateSinkTableInfo(dstTableName, &pTableSinkInfo);
35,409✔
996
    if (code == 0) {
35,409!
997
      tqDebug("s-task:%s build new sinkTableInfo to add cache, dstTable:%s", id, dstTableName);
35,409✔
998
    } else {
999
      tqDebug("s-task:%s failed to build new sinkTableInfo, dstTable:%s", id, dstTableName);
×
1000
      return code;
×
1001
    }
1002
  }
1003

1004
  if (alreadyCached) {
63,591✔
1005
    pTableData->uid = pTableSinkInfo->uid;
28,182✔
1006

1007
    if (pTableData->uid == 0) {
28,182✔
1008
      tqTrace("s-task:%s cached tableInfo:%s uid is invalid, acquire it from meta", id, pTableSinkInfo->name.data);
6,328✔
1009
      return doWaitForDstTableCreated(pVnode, pTask, pTableSinkInfo, dstTableName, &pTableData->uid);
6,328✔
1010
    } else {
1011
      tqTrace("s-task:%s set the dstTable uid from cache:%" PRId64, id, pTableData->uid);
21,854✔
1012
    }
1013
  } else {
1014
    // The auto-create option will always set to be open for those submit messages, which arrives during the period
1015
    // the creating of the destination table, due to the absence of the user-specified table in TSDB. When scanning
1016
    // data from WAL, those submit messages, with auto-created table option, will be discarded expect the first, for
1017
    // those mismatched table uids. Only the FIRST table has the correct table uid, and those remain all have
1018
    // randomly generated, but false table uid in the WAL.
1019
    SMetaReader mr = {0};
35,409✔
1020
    metaReaderDoInit(&mr, pVnode->pMeta, META_READER_LOCK);
35,409✔
1021

1022
    // table not in cache, let's try to extract it from tsdb meta
1023
    if (metaGetTableEntryByName(&mr, dstTableName) < 0) {
35,410✔
1024
      metaReaderClear(&mr);
10,914✔
1025

1026
      if (pTask->outputInfo.tbSink.autoCreateCtb) {
10,914!
1027
        tqDebug("s-task:%s stream write into table:%s, table auto created", id, dstTableName);
10,914✔
1028

1029
        SArray* pTagArray = taosArrayInit(pTSchema->numOfCols + 1, sizeof(STagVal));
10,914✔
1030
        if (pTagArray == NULL) {
10,914!
1031
          tqError("s-task:%s failed to build auto create submit msg in sink, vgId:%d, due to %s", id, vgId,
×
1032
                  tstrerror(terrno));
1033
          return terrno;
×
1034
        }
1035

1036
        pTableData->flags = SUBMIT_REQ_AUTO_CREATE_TABLE;
10,914✔
1037
        code = buildAutoCreateTableReq(stbFullName, suid, pTSchema->numOfCols + 1, pDataBlock, pTagArray,
21,828✔
1038
                                       IS_NEW_SUBTB_RULE(pTask), &pTableData->pCreateTbReq);
10,914!
1039
        taosArrayDestroy(pTagArray);
10,914✔
1040

1041
        if (code) {
10,914!
1042
          tqError("s-task:%s failed to build auto create dst-table req:%s, code:%s", id, dstTableName, tstrerror(code));
×
1043
          taosMemoryFree(pTableSinkInfo);
×
1044
          return code;
×
1045
        }
1046

1047
        pTableSinkInfo->uid = 0;
10,914✔
1048
        code = doPutSinkTableInfoIntoCache(pTask->outputInfo.tbSink.pTbInfo, pTableSinkInfo, groupId, id);
10,914✔
1049
      } else {
1050
        metaReaderClear(&mr);
×
1051

1052
        tqError("s-task:%s vgId:%d dst-table:%s not auto-created, and not create in tsdb, discard data", id, vgId,
×
1053
                dstTableName);
1054
        return TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
1055
      }
1056
    } else {
1057
      bool isValid = isValidDstChildTable(&mr, vgId, dstTableName, suid);
24,496✔
1058
      if (!isValid) {
24,496!
1059
        metaReaderClear(&mr);
×
1060
        taosMemoryFree(pTableSinkInfo);
×
1061
        tqError("s-task:%s vgId:%d table:%s already exists, but not child table, stream results is discarded", id, vgId,
×
1062
                dstTableName);
1063
        return terrno;
×
1064
      } else {
1065
        pTableData->uid = mr.me.uid;
24,496✔
1066
        pTableSinkInfo->uid = mr.me.uid;
24,496✔
1067

1068
        metaReaderClear(&mr);
24,496✔
1069
        code = doPutSinkTableInfoIntoCache(pTask->outputInfo.tbSink.pTbInfo, pTableSinkInfo, groupId, id);
24,495✔
1070
      }
1071
    }
1072
  }
1073

1074
  return code;
57,264✔
1075
}
1076

1077
int32_t tqSetDstTableDataPayload(uint64_t suid, const STSchema* pTSchema, int32_t blockIndex, SSDataBlock* pDataBlock,
106,261✔
1078
                                 SSubmitTbData* pTableData, int64_t earlyTs, const char* id) {
1079
  int32_t numOfRows = pDataBlock->info.rows;
106,261✔
1080
  char*   dstTableName = pDataBlock->info.parTbName;
106,261✔
1081

1082
  tqDebug("s-task:%s sink data pipeline, build submit msg from %dth resBlock, including %d rows, dst suid:%" PRId64, id,
106,261✔
1083
          blockIndex + 1, numOfRows, suid);
1084

1085
  // convert all rows
1086
  int32_t code = doConvertRows(pTableData, pTSchema, pDataBlock, earlyTs, id);
106,261✔
1087
  if (code != TSDB_CODE_SUCCESS) {
106,256!
1088
    tqError("s-task:%s failed to convert rows from result block, code:%s", id, tstrerror(terrno));
×
1089
    return code;
×
1090
  }
1091

1092
  if (pTableData->aRowP != NULL) {
106,256!
1093
    taosArraySort(pTableData->aRowP, tsAscendingSortFn);
106,257✔
1094
    tqTrace("s-task:%s build submit msg for dstTable:%s, numOfRows:%d", id, dstTableName, numOfRows);
106,258✔
1095
  }
1096

1097
  return code;
106,258✔
1098
}
1099

1100
int32_t checkTagSchema(SStreamTask* pTask, SVnode* pVnode) {
17,204✔
1101
  int32_t          code = TSDB_CODE_SUCCESS;
17,204✔
1102
  const char*      id = pTask->id.idStr;
17,204✔
1103
  STaskOutputInfo* pOutputInfo = &pTask->outputInfo;
17,204✔
1104
  int32_t          vgId = pTask->pMeta->vgId;
17,204✔
1105

1106
  if (pTask->outputInfo.tbSink.pTagSchema == NULL) {
17,204✔
1107
    SMetaReader mer1 = {0};
3,262✔
1108
    metaReaderDoInit(&mer1, pVnode->pMeta, META_READER_LOCK);
3,262✔
1109

1110
    code = metaReaderGetTableEntryByUid(&mer1, pOutputInfo->tbSink.stbUid);
3,262✔
1111
    if (code != TSDB_CODE_SUCCESS) {
3,262!
1112
      tqError("s-task:%s vgId:%d failed to get the dst stable, failed to sink results", id, vgId);
×
1113
      metaReaderClear(&mer1);
×
1114
      return code;
×
1115
    }
1116

1117
    pOutputInfo->tbSink.pTagSchema = tCloneSSchemaWrapper(&mer1.me.stbEntry.schemaTag);
3,261✔
1118
    metaReaderClear(&mer1);
3,261✔
1119

1120
    if (pOutputInfo->tbSink.pTagSchema == NULL) {
3,261✔
1121
      tqError("s-task:%s failed to clone tag schema, code:%s, failed to sink results", id, tstrerror(terrno));
1!
1122
      return terrno;
1✔
1123
    }
1124

1125
    SSchemaWrapper* pTagSchema = pOutputInfo->tbSink.pTagSchema;
3,260✔
1126
    SSchema*        pCol1 = &pTagSchema->pSchema[0];
3,260✔
1127
    if (pTagSchema->nCols == 1 && pCol1->type == TSDB_DATA_TYPE_UBIGINT && strcmp(pCol1->name, "group_id") == 0) {
3,260!
1128
      pOutputInfo->tbSink.autoCreateCtb = true;
1,975✔
1129
    } else {
1130
      pOutputInfo->tbSink.autoCreateCtb = false;
1,285✔
1131
    }
1132
  }
1133

1134
  return code;
17,202✔
1135
}
1136

1137
void tqSinkDataIntoDstTable(SStreamTask* pTask, void* vnode, void* data) {
17,205✔
1138
  const SArray*    pBlocks = (const SArray*)data;
17,205✔
1139
  SVnode*          pVnode = (SVnode*)vnode;
17,205✔
1140
  int64_t          suid = pTask->outputInfo.tbSink.stbUid;
17,205✔
1141
  char*            stbFullName = pTask->outputInfo.tbSink.stbFullName;
17,205✔
1142
  STSchema*        pTSchema = pTask->outputInfo.tbSink.pTSchema;
17,205✔
1143
  int32_t          vgId = TD_VID(pVnode);
17,205✔
1144
  int32_t          numOfBlocks = taosArrayGetSize(pBlocks);
17,205✔
1145
  int32_t          code = TSDB_CODE_SUCCESS;
17,205✔
1146
  const char*      id = pTask->id.idStr;
17,205✔
1147
  int64_t          earlyTs = tsdbGetEarliestTs(pVnode->pTsdb);
17,205✔
1148
  STaskOutputInfo* pOutputInfo = &pTask->outputInfo;
17,207✔
1149

1150
  code = checkTagSchema(pTask, pVnode);
17,207✔
1151
  if (code != TSDB_CODE_SUCCESS) {
17,205!
1152
    return;
×
1153
  }
1154

1155
  bool onlySubmitData = hasOnlySubmitData(pBlocks, numOfBlocks);
17,205✔
1156
  if (!onlySubmitData || pTask->subtableWithoutMd5 == 1) {
17,205✔
1157
    tqDebug("vgId:%d, s-task:%s write %d stream resBlock(s) into table, has delete block, submit one-by-one", vgId, id,
6,646✔
1158
            numOfBlocks);
1159

1160
    for (int32_t i = 0; i < numOfBlocks; ++i) {
24,764✔
1161
      if (streamTaskShouldStop(pTask)) {
18,114!
1162
        return;
×
1163
      }
1164

1165
      SSDataBlock* pDataBlock = taosArrayGet(pBlocks, i);
18,115✔
1166
      if (pDataBlock == NULL) {
18,114!
1167
        continue;
×
1168
      }
1169

1170
      if (pDataBlock->info.type == STREAM_DELETE_RESULT) {
18,114✔
1171
        code = doBuildAndSendDeleteMsg(pVnode, stbFullName, pDataBlock, pTask, suid);
2,582✔
1172
      } else if (pDataBlock->info.type == STREAM_CREATE_CHILD_TABLE) {
15,532✔
1173
        code = doBuildAndSendCreateTableMsg(pVnode, stbFullName, pDataBlock, pTask, suid);
6,662✔
1174
      } else if (pDataBlock->info.type == STREAM_CHECKPOINT) {
8,870!
1175
        continue;
×
1176
      } else if (pDataBlock->info.type == STREAM_DROP_CHILD_TABLE && pTask->subtableWithoutMd5) {
8,870!
1177
        code = doBuildAndSendDropTableMsg(pVnode, stbFullName, pDataBlock, pTask, suid);
55✔
1178
      } else {
1179
        code = handleResultBlockMsg(pTask, pDataBlock, i, pVnode, earlyTs);
8,815✔
1180
      }
1181
    }
1182
  } else {
1183
    tqDebug("vgId:%d, s-task:%s write %d stream resBlock(s) into table, merge submit msg", vgId, id, numOfBlocks);
10,559✔
1184
    if (streamTaskShouldStop(pTask)) {
10,559!
1185
      return;
×
1186
    }
1187

1188
    rebuildAndSendMultiResBlock(pTask, pBlocks, pVnode, earlyTs);
10,559✔
1189
  }
1190
}
1191

1192
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
1193
bool hasOnlySubmitData(const SArray* pBlocks, int32_t numOfBlocks) {
17,202✔
1194
  for (int32_t i = 0; i < numOfBlocks; ++i) {
115,874✔
1195
    SSDataBlock* p = taosArrayGet(pBlocks, i);
104,926✔
1196
    if (p == NULL) {
104,927!
1197
      continue;
×
1198
    }
1199

1200
    if (p->info.type == STREAM_DELETE_RESULT || p->info.type == STREAM_CREATE_CHILD_TABLE) {
104,927✔
1201
      return false;
6,255✔
1202
    }
1203
  }
1204

1205
  return true;
10,948✔
1206
}
1207

1208
int32_t doPutSinkTableInfoIntoCache(SSHashObj* pSinkTableMap, STableSinkInfo* pTableSinkInfo, uint64_t groupId, const char* id) {
40,973✔
1209
  int32_t code = tSimpleHashPut(pSinkTableMap, &groupId, sizeof(uint64_t), &pTableSinkInfo, POINTER_BYTES);
40,973✔
1210
  if (code != TSDB_CODE_SUCCESS) {
40,975!
1211
    taosMemoryFreeClear(pTableSinkInfo);
×
1212
  } else {
1213
    tqDebug("s-task:%s new dst table:%s(uid:%" PRIu64 ") added into cache, total:%d", id, pTableSinkInfo->name.data,
40,975✔
1214
            pTableSinkInfo->uid, tSimpleHashGetSize(pSinkTableMap));
1215
  }
1216

1217
  return code;
40,975✔
1218
}
1219

1220
bool doGetSinkTableInfoFromCache(SSHashObj* pTableInfoMap, uint64_t groupId, STableSinkInfo** pInfo) {
70,303✔
1221
  void* pVal = tSimpleHashGet(pTableInfoMap, &groupId, sizeof(uint64_t));
70,303✔
1222
  if (pVal) {
70,306✔
1223
    *pInfo = *(STableSinkInfo**)pVal;
29,307✔
1224
    return true;
29,307✔
1225
  }
1226

1227
  return false;
40,999✔
1228
}
1229

1230
int32_t doRemoveSinkTableInfoInCache(SSHashObj* pSinkTableMap, uint64_t groupId, const char* id) {
×
1231
  if (tSimpleHashGetSize(pSinkTableMap) == 0) {
×
1232
    return TSDB_CODE_SUCCESS;
×
1233
  }
1234

1235
  int32_t code = tSimpleHashRemove(pSinkTableMap, &groupId, sizeof(groupId));
×
1236
  if (code == 0) {
×
1237
    tqDebug("s-task:%s remove cached table meta for groupId:%" PRId64, id, groupId);
×
1238
  } else {
1239
    tqError("s-task:%s failed to remove table meta from hashmap, groupId:%" PRId64, id, groupId);
×
1240
  }
1241
  return code;
×
1242
}
1243

1244
int32_t doBuildAndSendDeleteMsg(SVnode* pVnode, char* stbFullName, SSDataBlock* pDataBlock, SStreamTask* pTask,
2,580✔
1245
                                int64_t suid) {
1246
  SBatchDeleteReq deleteReq = {.suid = suid, .deleteReqs = taosArrayInit(0, sizeof(SSingleDeleteReq))};
2,580✔
1247
  if (deleteReq.deleteReqs == NULL) {
2,586!
1248
    return terrno;
×
1249
  }
1250

1251
  int32_t code =
1252
      tqBuildDeleteReq(pVnode->pTq, stbFullName, pDataBlock, &deleteReq, pTask->id.idStr, IS_NEW_SUBTB_RULE(pTask));
2,586!
1253
  if (code != TSDB_CODE_SUCCESS) {
2,583!
1254
    return code;
×
1255
  }
1256

1257
  if (taosArrayGetSize(deleteReq.deleteReqs) == 0) {
2,583!
1258
    taosArrayDestroy(deleteReq.deleteReqs);
×
1259
    return TSDB_CODE_SUCCESS;
×
1260
  }
1261

1262
  int32_t len;
1263
  tEncodeSize(tEncodeSBatchDeleteReq, &deleteReq, len, code);
2,583!
1264
  if (code != TSDB_CODE_SUCCESS) {
2,584!
1265
    qError("s-task:%s failed to encode delete request", pTask->id.idStr);
×
1266
    return code;
×
1267
  }
1268

1269
  SEncoder encoder = {0};
2,584✔
1270
  void*    serializedDeleteReq = rpcMallocCont(len + sizeof(SMsgHead));
2,584✔
1271
  void*    abuf = POINTER_SHIFT(serializedDeleteReq, sizeof(SMsgHead));
2,585✔
1272
  tEncoderInit(&encoder, abuf, len);
2,585✔
1273
  code = tEncodeSBatchDeleteReq(&encoder, &deleteReq);
2,585✔
1274
  tEncoderClear(&encoder);
2,582✔
1275
  taosArrayDestroy(deleteReq.deleteReqs);
2,582✔
1276

1277
  if (code) {
2,585!
1278
    return code;
×
1279
  }
1280

1281
  ((SMsgHead*)serializedDeleteReq)->vgId = TD_VID(pVnode);
2,585✔
1282

1283
  SRpcMsg msg = {.msgType = TDMT_VND_BATCH_DEL, .pCont = serializedDeleteReq, .contLen = len + sizeof(SMsgHead)};
2,585✔
1284
  if (tmsgPutToQueue(&pVnode->msgCb, WRITE_QUEUE, &msg) != 0) {
2,585!
1285
    tqDebug("failed to put delete req into write-queue since %s", terrstr());
×
1286
  }
1287

1288
  return TSDB_CODE_SUCCESS;
2,585✔
1289
}
1290

1291
void rebuildAndSendMultiResBlock(SStreamTask* pTask, const SArray* pBlocks, SVnode* pVnode, int64_t earlyTs) {
10,559✔
1292
  int32_t     code = 0;
10,559✔
1293
  const char* id = pTask->id.idStr;
10,559✔
1294
  int32_t     vgId = pTask->pMeta->vgId;
10,559✔
1295
  int32_t     numOfBlocks = taosArrayGetSize(pBlocks);
10,559✔
1296
  int64_t     suid = pTask->outputInfo.tbSink.stbUid;
10,559✔
1297
  STSchema*   pTSchema = pTask->outputInfo.tbSink.pTSchema;
10,559✔
1298
  char*       stbFullName = pTask->outputInfo.tbSink.stbFullName;
10,559✔
1299

1300
  SHashObj* pTableIndexMap =
1301
      taosHashInit(numOfBlocks, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
10,559✔
1302

1303
  SSubmitReq2 submitReq = {.aSubmitTbData = taosArrayInit(1, sizeof(SSubmitTbData))};
10,558✔
1304
  if (submitReq.aSubmitTbData == NULL) {
10,557!
1305
    code = terrno;
×
1306
    tqError("s-task:%s vgId:%d failed to prepare submit msg in sink task, code:%s", id, vgId, tstrerror(code));
×
1307
    taosHashCleanup(pTableIndexMap);
×
1308
    return;
×
1309
  }
1310

1311
  bool hasSubmit = false;
10,557✔
1312
  for (int32_t i = 0; i < numOfBlocks; i++) {
108,005✔
1313
    SSDataBlock* pDataBlock = taosArrayGet(pBlocks, i);
97,447✔
1314
    if (pDataBlock == NULL) {
97,446!
UNCOV
1315
      continue;
×
1316
    }
1317

1318
    if (pDataBlock->info.type == STREAM_CHECKPOINT) {
97,446!
1319
      continue;
×
1320
    }
1321

1322
    hasSubmit = true;
97,446✔
1323
    pTask->execInfo.sink.numOfBlocks += 1;
97,446✔
1324
    uint64_t groupId = pDataBlock->info.id.groupId;
97,446✔
1325

1326
    SSubmitTbData tbData = {.suid = suid, .uid = 0, .sver = pTSchema->version, .flags = TD_REQ_FROM_APP};
97,446✔
1327

1328
    int32_t* index = taosHashGet(pTableIndexMap, &groupId, sizeof(groupId));
97,446✔
1329
    if (index == NULL) {  // no data yet, append it
97,441✔
1330
      code = setDstTableDataUid(pVnode, pTask, pDataBlock, stbFullName, &tbData);
54,770✔
1331
      if (code != TSDB_CODE_SUCCESS) {
54,775!
1332
        tqError("vgId:%d dst-table gid:%" PRId64 " not exist, discard stream results", vgId, groupId);
×
UNCOV
1333
        continue;
×
1334
      }
1335

1336
      code = tqSetDstTableDataPayload(suid, pTSchema, i, pDataBlock, &tbData, earlyTs, id);
54,775✔
1337
      if (code != TSDB_CODE_SUCCESS || tbData.aRowP == NULL) {
54,774!
UNCOV
1338
        if (tbData.pCreateTbReq != NULL) {
×
1339
          tdDestroySVCreateTbReq(tbData.pCreateTbReq);
×
1340
          (void)doRemoveSinkTableInfoInCache(pTask->outputInfo.tbSink.pTbInfo, groupId, id);
×
1341
          tbData.pCreateTbReq = NULL;
×
1342
        }
UNCOV
1343
        continue;
×
1344
      }
1345

1346
      void* p = taosArrayPush(submitReq.aSubmitTbData, &tbData);
54,774✔
1347
      if (p == NULL) {
54,774!
1348
        tqError("vgId:%d, s-task:%s failed to build submit msg, data lost", vgId, id);
×
1349
        continue;
×
1350
      }
1351

1352
      int32_t size = (int32_t)taosArrayGetSize(submitReq.aSubmitTbData) - 1;
54,774✔
1353
      code = taosHashPut(pTableIndexMap, &groupId, sizeof(groupId), &size, sizeof(size));
54,774✔
1354
      if (code) {
54,775!
1355
        tqError("vgId:%d, s-task:%s failed to put group into index map, code:%s", vgId, id, tstrerror(code));
×
1356
        continue;
×
1357
      }
1358
    } else {
1359
      code = tqSetDstTableDataPayload(suid, pTSchema, i, pDataBlock, &tbData, earlyTs, id);
42,671✔
1360
      if (code != TSDB_CODE_SUCCESS || tbData.aRowP == NULL) {
42,672!
1361
        if (tbData.pCreateTbReq != NULL) {
×
1362
          tdDestroySVCreateTbReq(tbData.pCreateTbReq);
×
1363
          tbData.pCreateTbReq = NULL;
×
1364
        }
1365
        continue;
×
1366
      }
1367

1368
      SSubmitTbData* pExisted = taosArrayGet(submitReq.aSubmitTbData, *index);
42,672✔
1369
      if (pExisted == NULL) {
42,672!
1370
        continue;
×
1371
      }
1372

1373
      code = doMergeExistedRows(pExisted, &tbData, id);
42,672✔
1374
      if (code != TSDB_CODE_SUCCESS) {
42,673!
1375
        continue;
×
1376
      }
1377
    }
1378

1379
    pTask->execInfo.sink.numOfRows += pDataBlock->info.rows;
97,448✔
1380
  }
1381

1382
  taosHashCleanup(pTableIndexMap);
10,558✔
1383

1384
  if (hasSubmit) {
10,559!
1385
    code = doBuildAndSendSubmitMsg(pVnode, pTask, &submitReq, numOfBlocks);
10,559✔
1386
    if (code) {  // failed and continue
10,559!
1387
      tqError("vgId:%d failed to build and send submit msg", vgId);
×
1388
    }
1389
  } else {
1390
    tDestroySubmitReq(&submitReq, TSDB_MSG_FLG_ENCODE);
×
1391
    tqDebug("vgId:%d, s-task:%s write results completed", vgId, id);
×
1392
  }
1393
}
1394

1395
int32_t handleResultBlockMsg(SStreamTask* pTask, SSDataBlock* pDataBlock, int32_t index, SVnode* pVnode, int64_t earlyTs) {
8,817✔
1396
  int32_t     code = 0;
8,817✔
1397
  STSchema*   pTSchema = pTask->outputInfo.tbSink.pTSchema;
8,817✔
1398
  int64_t     suid = pTask->outputInfo.tbSink.stbUid;
8,817✔
1399
  const char* id = pTask->id.idStr;
8,817✔
1400
  int32_t     vgId = TD_VID(pVnode);
8,817✔
1401
  char*       stbFullName = pTask->outputInfo.tbSink.stbFullName;
8,817✔
1402

1403
  pTask->execInfo.sink.numOfBlocks += 1;
8,817✔
1404

1405
  SSubmitReq2 submitReq = {.aSubmitTbData = taosArrayInit(1, sizeof(SSubmitTbData))};
8,817✔
1406
  if (submitReq.aSubmitTbData == NULL) {
8,817!
1407
    tqError("s-task:%s vgId:%d failed to prepare submit msg in sink task, code:%s", id, vgId, tstrerror(terrno));
×
1408
    return terrno;
×
1409
  }
1410

1411
  SSubmitTbData tbData = {.suid = suid, .uid = 0, .sver = pTSchema->version, .flags = TD_REQ_FROM_APP};
8,817✔
1412
  code = setDstTableDataUid(pVnode, pTask, pDataBlock, stbFullName, &tbData);
8,817✔
1413
  if (code != TSDB_CODE_SUCCESS) {
8,817✔
1414
    tqError("vgId:%d s-task:%s dst-table not exist, stb:%s discard stream results", vgId, id, stbFullName);
7!
1415
    return code;
7✔
1416
  }
1417

1418
  code = tqSetDstTableDataPayload(suid, pTSchema, index, pDataBlock, &tbData, earlyTs, id);
8,810✔
1419
  if (code != TSDB_CODE_SUCCESS || tbData.aRowP == NULL) {
8,810!
1420
    if (tbData.pCreateTbReq != NULL) {
1!
1421
      tdDestroySVCreateTbReq(tbData.pCreateTbReq);
×
1422
      (void)doRemoveSinkTableInfoInCache(pTask->outputInfo.tbSink.pTbInfo, pDataBlock->info.id.groupId, id);
×
1423
      tbData.pCreateTbReq = NULL;
×
1424
    }
1425

1426
    return code;
1✔
1427
  }
1428

1429
  void* p = taosArrayPush(submitReq.aSubmitTbData, &tbData);
8,809✔
1430
  if (p == NULL) {
8,808!
1431
    tqDebug("vgId:%d, s-task:%s failed to build submit msg, code:%s, data lost", vgId, id, tstrerror(terrno));
×
1432
    return terrno;
×
1433
  }
1434

1435
  code = doBuildAndSendSubmitMsg(pVnode, pTask, &submitReq, 1);
8,808✔
1436
  if (code) {  // failed and continue
8,812!
1437
    tqDebug("vgId:%d, s-task:%s submit msg failed, code:%s data lost", vgId, id, tstrerror(code));
×
1438
  }
1439

1440
  return code;
8,812✔
1441
}
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