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

taosdata / TDengine / #3583

17 Jan 2025 07:28AM UTC coverage: 63.876% (+0.07%) from 63.803%
#3583

push

travis-ci

web-flow
Merge pull request #29594 from taosdata/fix/insert-when-2-replicas

fix/insert-when-2-replicas

141608 of 284535 branches covered (49.77%)

Branch coverage included in aggregate %.

0 of 2 new or added lines in 1 file covered. (0.0%)

396 existing lines in 105 files now uncovered.

220075 of 281695 relevant lines covered (78.13%)

19864675.58 hits per line

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

64.14
/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,655✔
57
                         const char* pIdStr, bool newSubTableRule) {
58
  int32_t          totalRows = pDataBlock->info.rows;
2,655✔
59
  SColumnInfoData* pStartTsCol = taosArrayGet(pDataBlock->pDataBlock, START_TS_COLUMN_INDEX);
2,655✔
60
  SColumnInfoData* pEndTsCol = taosArrayGet(pDataBlock->pDataBlock, END_TS_COLUMN_INDEX);
2,654✔
61
  SColumnInfoData* pGidCol = taosArrayGet(pDataBlock->pDataBlock, GROUPID_COLUMN_INDEX);
2,655✔
62
  SColumnInfoData* pTbNameCol = taosArrayGet(pDataBlock->pDataBlock, TABLE_NAME_COLUMN_INDEX);
2,654✔
63

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

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

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

75
    char* name = NULL;
3,548✔
76
    char* originName = NULL;
3,548✔
77
    void* varTbName = NULL;
3,548✔
78
    if (!colDataIsNull(pTbNameCol, totalRows, row, NULL)) {
7,096✔
79
      varTbName = colDataGetVarData(pTbNameCol, row);
2,113✔
80
    }
81

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

89
      memcpy(name, varDataVal(varTbName), varDataLen(varTbName));
2,097✔
90
      name[varDataLen(varTbName)] = '\0';
2,097✔
91
      if (newSubTableRule && !isAutoTableName(name) && !alreadyAddGroupId(name, groupId) && groupId != 0 &&
2,097!
92
          stbFullName) {
93
        int32_t code = buildCtbNameAddGroupId(stbFullName, name, groupId, cap);
1,929✔
94
        if (code != TSDB_CODE_SUCCESS) {
1,926!
95
          return code;
×
96
        }
97
      }
98
    } else if (stbFullName) {
1,450✔
99
      int32_t code = buildCtbNameByGroupId(stbFullName, groupId, &name);
834✔
100
      if (code) {
834!
101
        return code;
×
102
      }
103
    } else {
104
      originName = taosMemoryCalloc(1, TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE);
616!
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,546!
115
      tqWarn("s-task:%s failed to build delete msg groupId:%" PRId64 ", skey:%" PRId64 " ekey:%" PRId64
1!
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,545✔
120
              groupId, name, skey, ekey);
121

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

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

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

137
  return 0;
2,654✔
138
}
139

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

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

168
end:
6,818✔
169
  return ret;
6,818✔
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,871✔
206
  void*   buf = NULL;
6,871✔
207
  int32_t tlen = 0;
6,871✔
208

209
  int32_t code = encoder(pReqs, TD_VID(pVnode), &buf, &tlen);
6,871✔
210
  if (code) {
6,873!
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,873✔
216
  code = tmsgPutToQueue(&pVnode->msgCb, WRITE_QUEUE, &msg);
6,873✔
217
  if (code) {
6,873!
218
    tqError("failed to put into write-queue since %s", terrstr());
×
219
  }
220

221
  return code;
6,872✔
222
}
223

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

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

232
  int32_t code = tNameFromString(&name, stbFullName, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
17,836✔
233
  if (code == 0) {
17,837!
234
    pCreateTableReq->ctb.stbName = taosStrdup((char*)tNameGetTableName(&name));
17,838!
235
    if (pCreateTableReq->ctb.stbName == NULL) {  // ignore this error code
17,838!
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,837✔
242
  return code;
17,837✔
243
}
244

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

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

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

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

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

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

292
  return 0;
17,814✔
293
}
294

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

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

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

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

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

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

329
    taosArrayClear(tagArray);
6,819✔
330

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

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

340
      code = createDefaultTagColName(&pCreateTbReq->ctb.tagName);
2,386✔
341
      if (code) {
2,387!
342
        return code;
×
343
      }
344
    } else {
345
      for (int32_t tagId = UD_TAG_COLUMN_INDEX, step = 1; tagId < size; tagId++, step++) {
47,619✔
346
        SColumnInfoData* pTagData = taosArrayGet(pDataBlock->pDataBlock, tagId);
43,186✔
347
        if (pTagData == NULL) {
43,180!
348
          continue;
18,767✔
349
        }
350

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

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

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

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

386
      // todo remove this
387
      void* pGpIdData = colDataGetData(pGpIdColInfo, rowId);
6,819!
388
      if (gid != *(int64_t*)pGpIdData) {
6,819!
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,819!
395
    if (code) {
6,816!
396
      goto _end;
×
397
    }
398

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

405
    bool alreadyCached = doGetSinkTableInfoFromCache(pTask->outputInfo.tbSink.pTbInfo, gid, &pInfo);
6,817✔
406
    if (!alreadyCached) {
6,815✔
407
      code = doCreateSinkTableInfo(pCreateTbReq->name, &pInfo);
5,656✔
408
      if (code) {
5,658!
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,658✔
414
      if (code) {
5,658!
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,817✔
420
  }
421

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

428
_end:
6,816✔
429
  taosArrayDestroy(tagArray);
6,816✔
430
  taosArrayDestroyEx(crTblArray, (FDelete)tdDestroySVCreateTbReq);
6,818✔
431
  return code;
6,818✔
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;
28✔
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) {
18,093✔
490
  const char* id = pTask->id.idStr;
18,093✔
491
  int32_t     vgId = TD_VID(pVnode);
18,093✔
492
  int32_t     len = 0;
18,093✔
493
  void*       pBuf = NULL;
18,093✔
494
  int32_t     numOfFinalBlocks = taosArrayGetSize(pReq->aSubmitTbData);
18,093✔
495

496
  int32_t code = buildSubmitMsgImpl(pReq, vgId, &pBuf, &len);
18,095✔
497
  if (code != TSDB_CODE_SUCCESS) {
18,096!
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};
18,096✔
503
  code = tmsgPutToQueue(&pVnode->msgCb, WRITE_QUEUE, &msg);
18,096✔
504
  if (code == TSDB_CODE_SUCCESS) {
18,096!
505
    tqDebug("s-task:%s vgId:%d comp %d blocks into %d and send to dstTable(s) completed", id, vgId, numOfBlocks,
18,097✔
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;
18,097✔
512

513
  pRec->numOfSubmit += 1;
18,097✔
514
  if ((pRec->numOfSubmit % 1000) == 0) {
18,097!
UNCOV
515
    double el = (taosGetTimestampMs() - pTask->execInfo.readyTs) / 1000.0;
×
UNCOV
516
    tqInfo("s-task:%s vgId:%d write %" PRId64 " blocks (%" PRId64 " rows) in %" PRId64
×
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;
18,096✔
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) {
31,443✔
527
  int32_t oldLen = taosArrayGetSize(pExisted->aRowP);
31,443✔
528
  int32_t newLen = taosArrayGetSize(pNew->aRowP);
31,443✔
529
  int32_t numOfPk = 0;
31,443✔
530

531
  int32_t j = 0, k = 0;
31,443✔
532
  SArray* pFinal = taosArrayInit(oldLen + newLen, POINTER_BYTES);
31,443✔
533
  if (pFinal == NULL) {
31,444!
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) {
1,848,082✔
539
    SRow* pNewRow = *(SRow**)TARRAY_GET_ELEM(pNew->aRowP, j);
1,816,638✔
540
    SRow* pOldRow = *(SRow**)TARRAY_GET_ELEM(pExisted->aRowP, k);
1,816,638✔
541

542
    if (pNewRow->ts < pOldRow->ts) {
1,816,638✔
543
      void* p = taosArrayPush(pFinal, &pNewRow);
36✔
544
      if (p == NULL) {
36!
545
        return terrno;
×
546
      }
547
      j += 1;
36✔
548
    } else if (pNewRow->ts > pOldRow->ts) {
1,816,602✔
549
      void* p = taosArrayPush(pFinal, &pOldRow);
1,785,573✔
550
      if (p == NULL) {
1,785,573!
551
        return terrno;
×
552
      }
553

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

563
        k += 1;
31,028✔
564
        j += 1;
31,028✔
565
        tRowDestroy(pOldRow);
31,028✔
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) {
403,497✔
599
    SRow* pRow = *(SRow**)TARRAY_GET_ELEM(pNew->aRowP, j++);
372,053✔
600
    void* p = taosArrayPush(pFinal, &pRow);
372,053✔
601
    if (p == NULL) {
372,053!
602
      return terrno;
×
603
    }
604
  }
605

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

614
  taosArrayDestroy(pNew->aRowP);
31,444✔
615
  taosArrayDestroy(pExisted->aRowP);
31,444✔
616
  pExisted->aRowP = pFinal;
31,444✔
617

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

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

627
bool isValidDstChildTable(SMetaReader* pReader, int32_t vgId, const char* ctbName, int64_t suid) {
49,602✔
628
  if (pReader->me.type != TSDB_CHILD_TABLE) {
49,602!
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) {
49,602!
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;
49,602✔
642
  return true;
49,604✔
643
}
644

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

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

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

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

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

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

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

684
  *pReq = pCreateTbReq;
11,018✔
685
  return code;
11,018✔
686
}
687

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

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

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

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

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

710
  tEncoderInit(&encoder, POINTER_SHIFT(pBuf, sizeof(SSubmitReq2Msg)), len - sizeof(SSubmitReq2Msg));
18,089✔
711
  if (tEncodeSubmitReq(&encoder, pSubmitReq) < 0) {
18,091!
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);
18,088✔
721
  tDestroySubmitReq(pSubmitReq, TSDB_MSG_FLG_ENCODE);
18,095✔
722

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

728
int32_t tsAscendingSortFn(const void* p1, const void* p2) {
50,668,506✔
729
  SRow* pRow1 = *(SRow**)p1;
50,668,506✔
730
  SRow* pRow2 = *(SRow**)p2;
50,668,506✔
731

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

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

744
  SArray* pVals = taosArrayInit(pTSchema->numOfCols, sizeof(SColVal));
109,787✔
745
  pTableData->aRowP = taosArrayInit(numOfRows, sizeof(SRow*));
109,792✔
746

747
  if (pTableData->aRowP == NULL || pVals == NULL) {
109,796!
748
    taosArrayDestroy(pTableData->aRowP);
2✔
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++) {
11,142,030✔
757
    taosArrayClear(pVals);
11,032,058✔
758

759
    int32_t dataIndex = 0;
11,032,333✔
760
    int64_t ts = 0;
11,032,333✔
761

762
    for (int32_t k = 0; k < pTSchema->numOfCols; k++) {
79,262,558✔
763
      const STColumn* pCol = &pTSchema->columns[k];
68,158,957✔
764

765
      // primary timestamp column, for debug purpose
766
      if (k == 0) {
68,158,957✔
767
        SColumnInfoData* pColData = taosArrayGet(pDataBlock->pDataBlock, dataIndex);
11,030,428✔
768
        if (pColData == NULL) {
11,027,212!
769
          continue;
×
770
        }
771

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

775
        if (ts < earlyTs) {
11,020,082!
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)) {
68,148,611✔
786
        if (pCol->flags & COL_IS_KEY) {
4,010!
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);
4,010✔
792
        void* p = taosArrayPush(pVals, &cv);
4,010✔
793
        if (p == NULL) {
4,010!
794
          return terrno;
×
795
        }
796
      } else {
797
        SColumnInfoData* pColData = taosArrayGet(pDataBlock->pDataBlock, dataIndex);
68,144,601✔
798
        if (pColData == NULL) {
68,118,648!
799
          continue;
×
800
        }
801

802
        if (colDataIsNull_s(pColData, j)) {
136,237,296✔
803
          if (pCol->flags & COL_IS_KEY) {
5,914,387!
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,914,387✔
810
          void* p = taosArrayPush(pVals, &cv);
5,914,401✔
811
          if (p == NULL) {
5,914,401!
812
            return terrno;
×
813
          }
814

815
          dataIndex++;
5,914,401✔
816
        } else {
817
          void* colData = colDataGetData(pColData, j);
62,204,261!
818
          if (IS_VAR_DATA_TYPE(pCol->type)) {  // address copy, no value
68,038,947!
819
            SValue sv =
5,922,477✔
820
                (SValue){.type = pCol->type, .nData = varDataLen(colData), .pData = (uint8_t*)varDataVal(colData)};
5,922,477✔
821
            SColVal cv = COL_VAL_VALUE(pCol->colId, sv);
5,922,477✔
822
            void* p = taosArrayPush(pVals, &cv);
5,834,686✔
823
            if (p == NULL) {
5,834,686!
824
              return terrno;
×
825
            }
826
          } else {
827
            SValue sv = {.type = pCol->type};
56,281,784✔
828
            memcpy(&sv.val, colData, tDataTypes[pCol->type].bytes);
56,281,784✔
829
            SColVal cv = COL_VAL_VALUE(pCol->colId, sv);
56,281,784✔
830
            void* p = taosArrayPush(pVals, &cv);
56,477,128✔
831
            if (p == NULL) {
56,477,128!
832
              return terrno;
×
833
            }
834
          }
835
          dataIndex++;
62,311,814✔
836
        }
837
      }
838
    }
839

840
    SRow* pRow = NULL;
11,103,601✔
841
    code = tRowBuild(pVals, (STSchema*)pTSchema, &pRow);
11,103,601✔
842
    if (code != TSDB_CODE_SUCCESS) {
11,038,857!
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);
11,038,857✔
850
    if (p == NULL) {
11,032,236!
851
      return terrno;
×
852
    }
853
  }
854

855
  taosArrayDestroy(pVals);
109,972✔
856
  return TSDB_CODE_SUCCESS;
109,796✔
857
}
858

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

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

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

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

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

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) {
59,836✔
932
  int32_t nameLen = strlen(pDstTableName);
59,836✔
933
  (*pInfo) = taosMemoryCalloc(1, sizeof(STableSinkInfo) + nameLen + 1);
59,836!
934
  if (*pInfo == NULL) {
59,840!
935
    return terrno;
×
936
  }
937

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

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

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

957
  if (alreadyCached) {
78,361✔
958
    if (dstTableName[0] == 0) {  // data block does not set the destination table name
24,178✔
959
      tstrncpy(dstTableName, pTableSinkInfo->name.data, pTableSinkInfo->name.len + 1);
1,002✔
960
      tqDebug("s-task:%s vgId:%d, gropuId:%" PRIu64 " datablock table name is null, set name:%s", id, vgId, groupId,
1,002!
961
              dstTableName);
962
    } else {
963
      tstrncpy(dstTableName, pTableSinkInfo->name.data, pTableSinkInfo->name.len + 1);
23,176✔
964
      if (pTableSinkInfo->uid != 0) {
23,176✔
965
        tqDebug("s-task:%s write %d rows into groupId:%" PRIu64 " dstTable:%s(uid:%" PRIu64 ")", id, numOfRows, groupId,
17,070✔
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,106✔
969
                id, numOfRows, groupId, dstTableName);
970
      }
971
    }
972
  } else {  // this groupId has not been kept in cache yet
973
    if (dstTableName[0] == 0) {
54,183✔
974
      memset(dstTableName, 0, TSDB_TABLE_NAME_LEN);
313✔
975
      code = buildCtbNameByGroupIdImpl(stbFullName, groupId, dstTableName);
313✔
976
      if (code) {
313!
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) &&
53,870!
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);
54,184✔
996
    if (code == 0) {
54,182!
997
      tqDebug("s-task:%s build new sinkTableInfo to add cache, dstTable:%s", id, dstTableName);
54,182✔
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) {
78,358✔
1005
    pTableData->uid = pTableSinkInfo->uid;
24,176✔
1006

1007
    if (pTableData->uid == 0) {
24,176✔
1008
      tqTrace("s-task:%s cached tableInfo:%s uid is invalid, acquire it from meta", id, pTableSinkInfo->name.data);
6,340✔
1009
      return doWaitForDstTableCreated(pVnode, pTask, pTableSinkInfo, dstTableName, &pTableData->uid);
6,340✔
1010
    } else {
1011
      tqTrace("s-task:%s set the dstTable uid from cache:%" PRId64, id, pTableData->uid);
17,836✔
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};
54,182✔
1020
    metaReaderDoInit(&mr, pVnode->pMeta, META_READER_LOCK);
54,182✔
1021

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

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

1029
        SArray* pTagArray = taosArrayInit(pTSchema->numOfCols + 1, sizeof(STagVal));
11,017✔
1030
        if (pTagArray == NULL) {
11,018!
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;
11,018✔
1037
        code = buildAutoCreateTableReq(stbFullName, suid, pTSchema->numOfCols + 1, pDataBlock, pTagArray,
22,036✔
1038
                                       IS_NEW_SUBTB_RULE(pTask), &pTableData->pCreateTbReq);
11,018!
1039
        taosArrayDestroy(pTagArray);
11,016✔
1040

1041
        if (code) {
11,017!
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;
11,017✔
1048
        code = doPutSinkTableInfoIntoCache(pTask->outputInfo.tbSink.pTbInfo, pTableSinkInfo, groupId, id);
11,017✔
1049
      } else {
1050
        tqError("s-task:%s vgId:%d dst-table:%s not auto-created, and not create in tsdb, discard data", id, vgId,
×
1051
                dstTableName);
1052
        return TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
1053
      }
1054
    } else {
1055
      bool isValid = isValidDstChildTable(&mr, vgId, dstTableName, suid);
43,165✔
1056
      if (!isValid) {
43,164!
1057
        metaReaderClear(&mr);
×
1058
        taosMemoryFree(pTableSinkInfo);
×
1059
        tqError("s-task:%s vgId:%d table:%s already exists, but not child table, stream results is discarded", id, vgId,
×
1060
                dstTableName);
1061
        return terrno;
×
1062
      } else {
1063
        pTableData->uid = mr.me.uid;
43,164✔
1064
        pTableSinkInfo->uid = mr.me.uid;
43,164✔
1065

1066
        metaReaderClear(&mr);
43,164✔
1067
        code = doPutSinkTableInfoIntoCache(pTask->outputInfo.tbSink.pTbInfo, pTableSinkInfo, groupId, id);
43,165✔
1068
      }
1069
    }
1070
  }
1071

1072
  return code;
72,018✔
1073
}
1074

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

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

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

1090
  if (pTableData->aRowP != NULL) {
109,795!
1091
    taosArraySort(pTableData->aRowP, tsAscendingSortFn);
109,795✔
1092
    tqTrace("s-task:%s build submit msg for dstTable:%s, numOfRows:%d", id, dstTableName, numOfRows);
109,795✔
1093
  }
1094

1095
  return code;
109,795✔
1096
}
1097

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

1104
  if (pTask->outputInfo.tbSink.pTagSchema == NULL) {
16,101✔
1105
    SMetaReader mer1 = {0};
3,408✔
1106
    metaReaderDoInit(&mer1, pVnode->pMeta, META_READER_LOCK);
3,408✔
1107

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

1115
    pOutputInfo->tbSink.pTagSchema = tCloneSSchemaWrapper(&mer1.me.stbEntry.schemaTag);
3,407✔
1116
    metaReaderClear(&mer1);
3,407✔
1117

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

1123
    SSchemaWrapper* pTagSchema = pOutputInfo->tbSink.pTagSchema;
3,405✔
1124
    SSchema*        pCol1 = &pTagSchema->pSchema[0];
3,405✔
1125
    if (pTagSchema->nCols == 1 && pCol1->type == TSDB_DATA_TYPE_UBIGINT && strcmp(pCol1->name, "group_id") == 0) {
3,405✔
1126
      pOutputInfo->tbSink.autoCreateCtb = true;
2,070✔
1127
    } else {
1128
      pOutputInfo->tbSink.autoCreateCtb = false;
1,335✔
1129
    }
1130
  }
1131

1132
  return code;
16,098✔
1133
}
1134

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

1148
  code = checkTagSchema(pTask, pVnode);
16,101✔
1149
  if (code != TSDB_CODE_SUCCESS) {
16,100!
1150
    return;
×
1151
  }
1152

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

1158
    for (int32_t i = 0; i < numOfBlocks; ++i) {
25,287✔
1159
      if (streamTaskShouldStop(pTask)) {
18,407✔
1160
        return;
5✔
1161
      }
1162

1163
      SSDataBlock* pDataBlock = taosArrayGet(pBlocks, i);
18,408✔
1164
      if (pDataBlock == NULL) {
18,406!
1165
        continue;
×
1166
      }
1167

1168
      if (pDataBlock->info.type == STREAM_DELETE_RESULT) {
18,406✔
1169
        code = doBuildAndSendDeleteMsg(pVnode, stbFullName, pDataBlock, pTask, suid);
2,638✔
1170
      } else if (pDataBlock->info.type == STREAM_CREATE_CHILD_TABLE) {
15,768✔
1171
        code = doBuildAndSendCreateTableMsg(pVnode, stbFullName, pDataBlock, pTask, suid);
6,819✔
1172
      } else if (pDataBlock->info.type == STREAM_CHECKPOINT) {
8,949!
1173
        continue;
×
1174
      } else if (pDataBlock->info.type == STREAM_DROP_CHILD_TABLE && pTask->subtableWithoutMd5) {
8,949!
1175
        code = doBuildAndSendDropTableMsg(pVnode, stbFullName, pDataBlock, pTask, suid);
55✔
1176
      } else {
1177
        code = handleResultBlockMsg(pTask, pDataBlock, i, pVnode, earlyTs);
8,894✔
1178
      }
1179
    }
1180
  } else {
1181
    tqDebug("vgId:%d, s-task:%s write %d stream resBlock(s) into table, merge submit msg", vgId, id, numOfBlocks);
9,214✔
1182
    if (streamTaskShouldStop(pTask)) {
9,214!
1183
      return;
×
1184
    }
1185

1186
    rebuildAndSendMultiResBlock(pTask, pBlocks, pVnode, earlyTs);
9,214✔
1187
  }
1188
}
1189

1190
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
1191
bool hasOnlySubmitData(const SArray* pBlocks, int32_t numOfBlocks) {
16,097✔
1192
  for (int32_t i = 0; i < numOfBlocks; ++i) {
118,234✔
1193
    SSDataBlock* p = taosArrayGet(pBlocks, i);
108,633✔
1194
    if (p == NULL) {
108,634!
1195
      continue;
×
1196
    }
1197

1198
    if (p->info.type == STREAM_DELETE_RESULT || p->info.type == STREAM_CREATE_CHILD_TABLE) {
108,634✔
1199
      return false;
6,497✔
1200
    }
1201
  }
1202

1203
  return true;
9,601✔
1204
}
1205

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

1215
  return code;
59,840✔
1216
}
1217

1218
bool doGetSinkTableInfoFromCache(SSHashObj* pTableInfoMap, uint64_t groupId, STableSinkInfo** pInfo) {
85,226✔
1219
  void* pVal = tSimpleHashGet(pTableInfoMap, &groupId, sizeof(uint64_t));
85,226✔
1220
  if (pVal) {
85,230✔
1221
    *pInfo = *(STableSinkInfo**)pVal;
25,366✔
1222
    return true;
25,366✔
1223
  }
1224

1225
  return false;
59,864✔
1226
}
1227

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

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

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

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

1255
  if (taosArrayGetSize(deleteReq.deleteReqs) == 0) {
2,637!
1256
    taosArrayDestroy(deleteReq.deleteReqs);
×
1257
    return TSDB_CODE_SUCCESS;
×
1258
  }
1259

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

1267
  SEncoder encoder = {0};
2,635✔
1268
  void*    serializedDeleteReq = rpcMallocCont(len + sizeof(SMsgHead));
2,635✔
1269
  void*    abuf = POINTER_SHIFT(serializedDeleteReq, sizeof(SMsgHead));
2,640✔
1270
  tEncoderInit(&encoder, abuf, len);
2,640✔
1271
  code = tEncodeSBatchDeleteReq(&encoder, &deleteReq);
2,638✔
1272
  tEncoderClear(&encoder);
2,638✔
1273
  taosArrayDestroy(deleteReq.deleteReqs);
2,635✔
1274

1275
  if (code) {
2,637!
1276
    return code;
×
1277
  }
1278

1279
  ((SMsgHead*)serializedDeleteReq)->vgId = TD_VID(pVnode);
2,637✔
1280

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

1286
  return TSDB_CODE_SUCCESS;
2,639✔
1287
}
1288

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

1298
  SHashObj* pTableIndexMap =
1299
      taosHashInit(numOfBlocks, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
9,213✔
1300

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

1309
  bool hasSubmit = false;
9,211✔
1310
  for (int32_t i = 0; i < numOfBlocks; i++) {
110,123✔
1311
    SSDataBlock* pDataBlock = taosArrayGet(pBlocks, i);
100,909✔
1312
    if (pDataBlock == NULL) {
100,910!
1313
      continue;
×
1314
    }
1315

1316
    if (pDataBlock->info.type == STREAM_CHECKPOINT) {
100,910!
1317
      continue;
×
1318
    }
1319

1320
    hasSubmit = true;
100,910✔
1321
    pTask->execInfo.sink.numOfBlocks += 1;
100,910✔
1322
    uint64_t groupId = pDataBlock->info.id.groupId;
100,910✔
1323

1324
    SSubmitTbData tbData = {.suid = suid, .uid = 0, .sver = pTSchema->version, .flags = TD_REQ_FROM_APP};
100,910✔
1325

1326
    int32_t* index = taosHashGet(pTableIndexMap, &groupId, sizeof(groupId));
100,910✔
1327
    if (index == NULL) {  // no data yet, append it
100,910✔
1328
      code = setDstTableDataUid(pVnode, pTask, pDataBlock, stbFullName, &tbData);
69,467✔
1329
      if (code != TSDB_CODE_SUCCESS) {
69,466!
1330
        tqError("vgId:%d dst-table gid:%" PRId64 " not exist, discard stream results", vgId, groupId);
×
1331
        continue;
×
1332
      }
1333

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

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

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

1366
      SSubmitTbData* pExisted = taosArrayGet(submitReq.aSubmitTbData, *index);
31,444✔
1367
      if (pExisted == NULL) {
31,444!
1368
        continue;
×
1369
      }
1370

1371
      code = doMergeExistedRows(pExisted, &tbData, id);
31,444✔
1372
      if (code != TSDB_CODE_SUCCESS) {
31,444!
1373
        continue;
×
1374
      }
1375
    }
1376

1377
    pTask->execInfo.sink.numOfRows += pDataBlock->info.rows;
100,912✔
1378
  }
1379

1380
  taosHashCleanup(pTableIndexMap);
9,214✔
1381

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

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

1401
  pTask->execInfo.sink.numOfBlocks += 1;
8,893✔
1402

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

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

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

1424
    return code;
×
1425
  }
1426

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

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

1438
  return code;
8,883✔
1439
}
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