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

taosdata / TDengine / #4118

17 May 2025 06:43AM UTC coverage: 62.797% (+0.7%) from 62.054%
#4118

push

travis-ci

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

merge: from main to 3.0 branch

156841 of 318088 branches covered (49.31%)

Branch coverage included in aggregate %.

176 of 225 new or added lines in 20 files covered. (78.22%)

2989 existing lines in 163 files now uncovered.

242067 of 317143 relevant lines covered (76.33%)

6956088.31 hits per line

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

57.13
/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
typedef struct STableSinkInfo {
20
  uint64_t uid;
21
  tstr     name;
22
} STableSinkInfo;
23

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

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

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

62
  if (pStartTsCol == NULL || pEndTsCol == NULL || pGidCol == NULL || pTbNameCol == NULL) {
2,499!
63
    return terrno;
×
64
  }
65

66
  tqDebug("s-task:%s build %d rows delete msg for table:%s", pIdStr, totalRows, stbFullName);
2,499✔
67

68
  for (int32_t row = 0; row < totalRows; row++) {
5,956✔
69
    int64_t skey = *(int64_t*)colDataGetData(pStartTsCol, row);
3,456!
70
    int64_t ekey = *(int64_t*)colDataGetData(pEndTsCol, row);
3,456!
71
    int64_t groupId = *(int64_t*)colDataGetData(pGidCol, row);
3,456!
72

73
    char* name = NULL;
3,456✔
74
    char* originName = NULL;
3,456✔
75
    void* varTbName = NULL;
3,456✔
76
    if (!colDataIsNull(pTbNameCol, totalRows, row, NULL)) {
6,912✔
77
      varTbName = colDataGetVarData(pTbNameCol, row);
1,869✔
78
    }
79

80
    if (varTbName != NULL && varTbName != (void*)-1) {
5,311!
81
      size_t cap = TMAX(TSDB_TABLE_NAME_LEN, varDataLen(varTbName) + 1);
1,854✔
82
      name = taosMemoryMalloc(cap);
1,854!
83
      if (name == NULL) {
1,853!
84
        return terrno;
×
85
      }
86

87
      memcpy(name, varDataVal(varTbName), varDataLen(varTbName));
1,853✔
88
      name[varDataLen(varTbName)] = '\0';
1,853✔
89
      if (newSubTableRule && !isAutoTableName(name) && !alreadyAddGroupId(name, groupId) && groupId != 0 &&
1,853!
90
          stbFullName) {
91
        int32_t code = buildCtbNameAddGroupId(stbFullName, name, groupId, cap);
1,749✔
92
        if (code != TSDB_CODE_SUCCESS) {
1,749!
93
          return code;
×
94
        }
95
      }
96
    } else if (stbFullName) {
1,602✔
97
      int32_t code = buildCtbNameByGroupId(stbFullName, groupId, &name);
988✔
98
      if (code) {
987!
99
        return code;
×
100
      }
101
    } else {
102
      originName = taosMemoryCalloc(1, TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE);
614!
103
      if (originName == NULL) {
616!
104
        return terrno;
×
105
      }
106

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

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

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

128
    if (originName) {
3,457✔
129
      name = originName;
616✔
130
    }
131

132
    taosMemoryFreeClear(name);
3,457!
133
  }
134

135
  return 0;
2,500✔
136
}
137

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

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

166
end:
6,142✔
167
  return ret;
6,142✔
168
}
169

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

181
  if (!*ppBuf) {
×
182
    code = terrno;
×
183
    goto end;
×
184
  }
185

186
  ((SMsgHead*)(*ppBuf))->vgId = vgId;
×
187
  ((SMsgHead*)(*ppBuf))->contLen = htonl(*contLen);
×
188

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

203
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,142✔
204
  void*   buf = NULL;
6,142✔
205
  int32_t tlen = 0;
6,142✔
206

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

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

219
  return code;
6,144✔
220
}
221

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

227
  // set super table name
228
  SName name = {0};
7,226✔
229

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

239
  pCreateTableReq->ctb.tagNum = numOfTags;
7,227✔
240
  return code;
7,227✔
241
}
242

243
int32_t createDefaultTagColName(SArray** pColNameList) {
3,903✔
244
  *pColNameList = NULL;
3,903✔
245

246
  SArray* pTagColNameList = taosArrayInit(1, TSDB_COL_NAME_LEN);
3,903✔
247
  if (pTagColNameList == NULL) {
3,903!
248
    return terrno;
×
249
  }
250

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

258
  *pColNameList = pTagColNameList;
3,903✔
259
  return TSDB_CODE_SUCCESS;
3,903✔
260
}
261

262
int32_t setCreateTableMsgTableName(SVCreateTbReq* pCreateTableReq, SSDataBlock* pDataBlock, const char* stbFullName,
7,224✔
263
                                   int64_t gid, bool newSubTableRule, const char* id) {
264
  if (pDataBlock->info.parTbName[0]) {
7,224✔
265
    if (newSubTableRule && !isAutoTableName(pDataBlock->info.parTbName) &&
7,205✔
266
        !alreadyAddGroupId(pDataBlock->info.parTbName, gid) && gid != 0 && stbFullName) {
3,486!
267
      pCreateTableReq->name = taosMemoryCalloc(1, TSDB_TABLE_NAME_LEN);
22!
268
      if (pCreateTableReq->name == NULL) {
22!
269
        return terrno;
×
270
      }
271

272
      tstrncpy(pCreateTableReq->name, pDataBlock->info.parTbName, TSDB_TABLE_NAME_LEN);
22✔
273
      int32_t code = buildCtbNameAddGroupId(stbFullName, pCreateTableReq->name, gid, TSDB_TABLE_NAME_LEN);
22✔
274
      if (code != TSDB_CODE_SUCCESS) {
22!
275
        return code;
×
276
      }
277
      tqDebug("s-task:%s gen name from:%s blockdata", id, pDataBlock->info.parTbName);
22!
278
    } else {
279
      pCreateTableReq->name = taosStrdup(pDataBlock->info.parTbName);
7,186!
280
      if (pCreateTableReq->name == NULL) {
7,185!
281
        return terrno;
×
282
      }
283
      tqDebug("s-task:%s copy name:%s from blockdata", id, pDataBlock->info.parTbName);
7,185✔
284
    }
285
  } else {
286
    int32_t code = buildCtbNameByGroupId(stbFullName, gid, &pCreateTableReq->name);
19✔
287
    tqDebug("s-task:%s no name in blockdata, auto-created table name:%s", id, pCreateTableReq->name);
19!
288
    return code;
19✔
289
  }
290

291
  return 0;
7,207✔
292
}
293

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

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

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

316
  for (int32_t rowId = 0; rowId < rows; rowId++) {
12,285✔
317
    SVCreateTbReq* pCreateTbReq = &((SVCreateTbReq){0});
6,142✔
318

319
    int32_t size = taosArrayGetSize(pDataBlock->pDataBlock);
6,142✔
320
    int32_t numOfTags = TMAX(size - UD_TAG_COLUMN_INDEX, 1);
6,143✔
321

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

328
    taosArrayClear(tagArray);
6,144✔
329

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

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

339
      code = createDefaultTagColName(&pCreateTbReq->ctb.tagName);
2,820✔
340
      if (code) {
2,820!
341
        return code;
×
342
      }
343
    } else {
344
      for (int32_t tagId = UD_TAG_COLUMN_INDEX, step = 1; tagId < size; tagId++, step++) {
38,606✔
345
        SColumnInfoData* pTagData = taosArrayGet(pDataBlock->pDataBlock, tagId);
35,281✔
346
        if (pTagData == NULL) {
35,281!
347
          continue;
18,676✔
348
        }
349

350
        STagVal tagVal = {.cid = pTSchema->numOfCols + step, .type = pTagData->info.type};
35,281✔
351
        void*   pData = colDataGetData(pTagData, rowId);
35,281!
352
        if (colDataIsNull_s(pTagData, rowId)) {
70,562!
353
          continue;
18,676✔
354
        } else if (IS_VAR_DATA_TYPE(pTagData->info.type)) {
16,605!
355
          tagVal.nData = varDataLen(pData);
4,456✔
356
          tagVal.pData = (uint8_t*)varDataVal(pData);
4,456✔
357
        } else {
358
          memcpy(&tagVal.i64, pData, pTagData->info.bytes);
12,149✔
359
        }
360
        void* p = taosArrayPush(tagArray, &tagVal);
16,606✔
361
        if (p == NULL) {
16,606!
362
          code = terrno;
×
363
          goto _end;
×
364
        }
365
      }
366
    }
367

368
    code = tTagNew(tagArray, 1, false, (STag**)&pCreateTbReq->ctb.pTag);
6,145✔
369
    taosArrayDestroy(tagArray);
6,141✔
370
    tagArray = NULL;
6,142✔
371

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

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

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

393
    code = setCreateTableMsgTableName(pCreateTbReq, pDataBlock, stbFullName, gid, IS_NEW_SUBTB_RULE(pTask),
6,141!
394
                                      pTask->id.idStr);
395
    if (code) {
6,143!
396
      goto _end;
×
397
    }
398

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

405
    bool alreadyCached = doGetSinkTableInfoFromCache(pTask->outputInfo.tbSink.pTbInfo, gid, &pInfo);
6,143✔
406
    if (!alreadyCached) {
6,141✔
407
      code = doCreateSinkTableInfo(pCreateTbReq->name, &pInfo);
4,999✔
408
      if (code) {
5,001!
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,001✔
414
      if (code) {
5,000!
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,142✔
420
  }
421

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

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

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

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

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

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

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

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

478

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

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

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

496
  int32_t code = buildSubmitMsgImpl(pReq, vgId, &pBuf, &len);
13,503✔
497
  if (code != TSDB_CODE_SUCCESS) {
13,505!
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};
13,505✔
503
  code = tmsgPutToQueue(&pVnode->msgCb, WRITE_QUEUE, &msg);
13,505✔
504
  if (code == TSDB_CODE_SUCCESS) {
13,505!
505
    tqDebug("s-task:%s vgId:%d comp %d blocks into %d and send to dstTable(s) completed", id, vgId, numOfBlocks,
13,505✔
506
            numOfFinalBlocks);
507
  } else {
508
    tqError("s-task:%s failed to put into write-queue since %s", id, terrstr());
×
509
  }
510

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

513
  pRec->numOfSubmit += 1;
13,505✔
514
  if ((pRec->numOfSubmit % 1000) == 0) {
13,505!
515
    double el = (taosGetTimestampMs() - pTask->execInfo.readyTs) / 1000.0;
×
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;
13,505✔
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) {
943✔
527
  int32_t oldLen = taosArrayGetSize(pExisted->aRowP);
943✔
528
  int32_t newLen = taosArrayGetSize(pNew->aRowP);
943✔
529
  int32_t numOfPk = 0;
943✔
530

531
  int32_t j = 0, k = 0;
943✔
532
  SArray* pFinal = taosArrayInit(oldLen + newLen, POINTER_BYTES);
943✔
533
  if (pFinal == NULL) {
944!
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,645,698✔
539
    SRow* pNewRow = *(SRow**)TARRAY_GET_ELEM(pNew->aRowP, j);
1,644,758✔
540
    SRow* pOldRow = *(SRow**)TARRAY_GET_ELEM(pExisted->aRowP, k);
1,644,758✔
541

542
    if (pNewRow->ts < pOldRow->ts) {
1,644,758✔
543
      void* p = taosArrayPush(pFinal, &pNewRow);
54✔
544
      if (p == NULL) {
54!
545
        return terrno;
×
546
      }
547
      j += 1;
54✔
548
    } else if (pNewRow->ts > pOldRow->ts) {
1,644,704✔
549
      void* p = taosArrayPush(pFinal, &pOldRow);
1,643,656✔
550
      if (p == NULL) {
1,643,656!
551
        return terrno;
×
552
      }
553

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

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

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

614
  taosArrayDestroy(pNew->aRowP);
940✔
615
  taosArrayDestroy(pExisted->aRowP);
945✔
616
  pExisted->aRowP = pFinal;
945✔
617

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

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

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

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

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

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

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

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

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

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

685
  *pReq = pCreateTbReq;
1,083✔
686
  return code;
1,083✔
687
}
688

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

694
  // encode
695
  int32_t len = 0;
13,503✔
696
  tEncodeSize(tEncodeSubmitReq, pSubmitReq, len, code);
13,503!
697

698
  SEncoder encoder = {0};
13,501✔
699
  len += sizeof(SSubmitReq2Msg);
13,501✔
700

701
  pBuf = rpcMallocCont(len);
13,501✔
702
  if (NULL == pBuf) {
13,503!
703
    tDestroySubmitReq(pSubmitReq, TSDB_MSG_FLG_ENCODE);
×
704
    return terrno;
×
705
  }
706

707
  ((SSubmitReq2Msg*)pBuf)->header.vgId = vgId;
13,503✔
708
  ((SSubmitReq2Msg*)pBuf)->header.contLen = htonl(len);
13,503✔
709
  ((SSubmitReq2Msg*)pBuf)->version = htobe64(1);
13,503✔
710

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

721
  tEncoderClear(&encoder);
13,503✔
722
  tDestroySubmitReq(pSubmitReq, TSDB_MSG_FLG_ENCODE);
13,504✔
723

724
  *msgLen = len;
13,505✔
725
  *pMsg = pBuf;
13,505✔
726
  return TSDB_CODE_SUCCESS;
13,505✔
727
}
728

729
int32_t tsAscendingSortFn(const void* p1, const void* p2) {
35,990,474✔
730
  SRow* pRow1 = *(SRow**)p1;
35,990,474✔
731
  SRow* pRow2 = *(SRow**)p2;
35,990,474✔
732

733
  if (pRow1->ts == pRow2->ts) {
35,990,474!
734
    return 0;
×
735
  } else {
736
    return pRow1->ts > pRow2->ts ? 1 : -1;
35,990,474!
737
  }
738
}
739

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

745
  SArray* pVals = taosArrayInit(pTSchema->numOfCols, sizeof(SColVal));
15,086✔
746
  pTableData->aRowP = taosArrayInit(numOfRows, sizeof(SRow*));
15,085✔
747

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

757
  for (int32_t j = 0; j < numOfRows; j++) {
6,245,896✔
758
    taosArrayClear(pVals);
6,230,801✔
759

760
    int32_t dataIndex = 0;
6,230,795✔
761
    int64_t ts = 0;
6,230,795✔
762

763
    for (int32_t k = 0; k < pTSchema->numOfCols; k++) {
59,838,685✔
764
      const STColumn* pCol = &pTSchema->columns[k];
53,586,054✔
765

766
      // primary timestamp column, for debug purpose
767
      if (k == 0) {
53,586,054✔
768
        SColumnInfoData* pColData = taosArrayGet(pDataBlock->pDataBlock, dataIndex);
6,230,685✔
769
        if (pColData == NULL) {
6,230,485!
770
          continue;
×
771
        }
772

773
        ts = *(int64_t*)colDataGetData(pColData, j);
6,230,485!
774
        tqTrace("s-task:%s sink row %d, col %d ts %" PRId64, id, j, k, ts);
6,230,485✔
775

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

786
      if (IS_SET_NULL(pCol)) {
53,603,046✔
787
        if (pCol->flags & COL_IS_KEY) {
3,845!
788
          qError("ts:%" PRId64 " primary key column should not be null, colId:%" PRIi16 ", colType:%" PRIi8, ts,
×
789
                 pCol->colId, pCol->type);
790
          break;
×
791
        }
792
        SColVal cv = COL_VAL_NULL(pCol->colId, pCol->type);
3,845✔
793
        void* p = taosArrayPush(pVals, &cv);
3,848✔
794
        if (p == NULL) {
3,848!
795
          return terrno;
×
796
        }
797
      } else {
798
        SColumnInfoData* pColData = taosArrayGet(pDataBlock->pDataBlock, dataIndex);
53,599,201✔
799
        if (pColData == NULL) {
53,598,005!
800
          continue;
×
801
        }
802

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

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

816
          dataIndex++;
5,913,539✔
817
        } else {
818
          void* colData = colDataGetData(pColData, j);
47,684,467!
819
          if (IS_VAR_DATA_TYPE(pCol->type)) {  // address copy, no value
53,504,065!
820
            SValue sv =
5,817,160✔
821
                (SValue){.type = pCol->type, .nData = varDataLen(colData), .pData = (uint8_t*)varDataVal(colData)};
5,817,160✔
822
            SColVal cv = COL_VAL_VALUE(pCol->colId, sv);
5,817,160✔
823
            void* p = taosArrayPush(pVals, &cv);
5,819,598✔
824
            if (p == NULL) {
5,819,598!
825
              return terrno;
×
826
            }
827
          } else {
828
            SValue sv = {.type = pCol->type};
41,867,307✔
829
            valueSetDatum(&sv, pCol->type, colData, tDataTypes[pCol->type].bytes);
41,867,307✔
830
            SColVal cv = COL_VAL_VALUE(pCol->colId, sv);
41,865,920✔
831
            void* p = taosArrayPush(pVals, &cv);
41,870,905✔
832
            if (p == NULL) {
41,870,905!
833
              return terrno;
×
834
            }
835
          }
836
          dataIndex++;
47,690,503✔
837
        }
838
      }
839
    }
840

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

850
    void* p = taosArrayPush(pTableData->aRowP, &pRow);
6,230,897✔
851
    if (p == NULL) {
6,230,809!
852
      return terrno;
×
853
    }
854
  }
855

856
  taosArrayDestroy(pVals);
15,095✔
857
  return TSDB_CODE_SUCCESS;
15,089✔
858
}
859

860
int32_t doWaitForDstTableCreated(SVnode* pVnode, SStreamTask* pTask, STableSinkInfo* pTableSinkInfo,
5,472✔
861
                                 const char* dstTableName, int64_t* uid) {
862
  int32_t     vgId = TD_VID(pVnode);
5,472✔
863
  int64_t     suid = pTask->outputInfo.tbSink.stbUid;
5,472✔
864
  const char* id = pTask->id.idStr;
5,472✔
865
  int32_t     timeout = 60;  // 1min
5,472✔
866
  int64_t     start = taosGetTimestampSec();
5,472✔
867

868
  while (pTableSinkInfo->uid == 0) {
8,275!
869
    if (streamTaskShouldStop(pTask)) {
8,276✔
870
      tqDebug("s-task:%s task will stop, quit from waiting for table:%s create", id, dstTableName);
1!
871
      return TSDB_CODE_STREAM_EXEC_CANCELLED;
5,474✔
872
    }
873

874
    int64_t waitingDuration = taosGetTimestampSec() - start;
8,275✔
875
    if (waitingDuration > timeout) {
8,276!
876
      tqError("s-task:%s wait for table-creating:%s more than %dsec, failed", id, dstTableName, timeout);
×
877
      return  TSDB_CODE_PAR_TABLE_NOT_EXIST;
×
878
    }
879

880
    // wait for the table to be created
881
    SMetaReader mr = {0};
8,276✔
882
    metaReaderDoInit(&mr, pVnode->pMeta, META_READER_LOCK);
8,276✔
883

884
    int32_t code = metaGetTableEntryByName(&mr, dstTableName);
8,277✔
885
    if (code == 0) {  // table already exists, check its type and uid
8,275✔
886
      bool isValid = isValidDstChildTable(&mr, vgId, dstTableName, suid);
5,471✔
887
      if (isValid) {  // not valid table, ignore it
5,471!
888
        tqDebug("s-task:%s set uid:%" PRIu64 " for dstTable:%s from meta", id, mr.me.uid, pTableSinkInfo->name.data);
5,472✔
889
        // set the destination table uid
890
        (*uid) = mr.me.uid;
5,472✔
891
        pTableSinkInfo->uid = mr.me.uid;
5,472✔
892
      }
893

894
      metaReaderClear(&mr);
5,471✔
895
      return terrno;
5,473✔
896
    } else {  // not exist, wait and retry
897
      metaReaderClear(&mr);
2,804✔
898
      taosMsleep(100);
2,804✔
899
      tqDebug("s-task:%s wait 100ms for the table:%s ready before insert data", id, dstTableName);
2,803✔
900
    }
901
  }
902

903
  return TSDB_CODE_SUCCESS;
×
904
}
905

906
static int32_t doWaitForDstTableDropped(SVnode* pVnode, SStreamTask* pTask, const char* dstTableName) {
×
907
  int32_t vgId = TD_VID(pVnode);
×
908
  int64_t suid = pTask->outputInfo.tbSink.stbUid;
×
909
  const char* id = pTask->id.idStr;
×
910

911
  while (1) {
×
912
    if (streamTaskShouldStop(pTask)) {
×
913
      tqDebug("s-task:%s task will stop, quit from waiting for table:%s drop", id, dstTableName);
×
914
      return TSDB_CODE_STREAM_EXEC_CANCELLED;
×
915
    }
916
    SMetaReader mr = {0};
×
917
    metaReaderDoInit(&mr, pVnode->pMeta, META_READER_LOCK);
×
918
    int32_t code = metaGetTableEntryByName(&mr, dstTableName);
×
919
    if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST) {
×
920
      metaReaderClear(&mr);
×
921
      break;
×
922
    } else if (TSDB_CODE_SUCCESS == code) {
×
923
      if (isValidDstChildTable(&mr, vgId, dstTableName, suid)) {
×
924
        metaReaderClear(&mr);
×
925
        taosMsleep(100);
×
926
        tqDebug("s-task:%s wait 100ms for table:%s drop", id, dstTableName);
×
927
      } else {
928
        metaReaderClear(&mr);
×
929
        break;
×
930
      }
931
    } else {
932
      tqError("s-task:%s failed to wait for table:%s drop", id, dstTableName);
×
933
      metaReaderClear(&mr);
×
934
      return terrno;
×
935
    }
936
  }
937
  return TSDB_CODE_SUCCESS;
×
938
}
939

940
int32_t doCreateSinkTableInfo(const char* pDstTableName, STableSinkInfo** pInfo) {
6,155✔
941
  int32_t nameLen = strlen(pDstTableName);
6,155✔
942
  (*pInfo) = taosMemoryCalloc(1, sizeof(STableSinkInfo) + nameLen + 1);
6,155!
943
  if (*pInfo == NULL) {
6,155!
944
    return terrno;
×
945
  }
946

947
  (*pInfo)->name.len = nameLen;
6,155✔
948
  memcpy((*pInfo)->name.data, pDstTableName, nameLen);
6,155✔
949
  return TSDB_CODE_SUCCESS;
6,155✔
950
}
951

952
int32_t setDstTableDataUid(SVnode* pVnode, SStreamTask* pTask, SSDataBlock* pDataBlock, char* stbFullName,
14,145✔
953
                           SSubmitTbData* pTableData) {
954
  uint64_t        groupId = pDataBlock->info.id.groupId;
14,145✔
955
  char*           dstTableName = pDataBlock->info.parTbName;
14,145✔
956
  int32_t         numOfRows = pDataBlock->info.rows;
14,145✔
957
  const char*     id = pTask->id.idStr;
14,145✔
958
  int64_t         suid = pTask->outputInfo.tbSink.stbUid;
14,145✔
959
  STSchema*       pTSchema = pTask->outputInfo.tbSink.pTSchema;
14,145✔
960
  int32_t         vgId = TD_VID(pVnode);
14,145✔
961
  STableSinkInfo* pTableSinkInfo = NULL;
14,145✔
962
  int32_t         code = 0;
14,145✔
963

964
  bool alreadyCached = doGetSinkTableInfoFromCache(pTask->outputInfo.tbSink.pTbInfo, groupId, &pTableSinkInfo);
14,145✔
965

966
  if (alreadyCached) {
14,146✔
967
    if (dstTableName[0] == 0) {  // data block does not set the destination table name
12,990✔
968
      tstrncpy(dstTableName, pTableSinkInfo->name.data, pTableSinkInfo->name.len + 1);
992✔
969
      tqDebug("s-task:%s vgId:%d, gropuId:%" PRIu64 " datablock table name is null, set name:%s", id, vgId, groupId,
992!
970
              dstTableName);
971
    } else {
972
      tstrncpy(dstTableName, pTableSinkInfo->name.data, pTableSinkInfo->name.len + 1);
11,998✔
973
      if (pTableSinkInfo->uid != 0) {
11,998✔
974
        tqDebug("s-task:%s write %d rows into groupId:%" PRIu64 " dstTable:%s(uid:%" PRIu64 ")", id, numOfRows, groupId,
6,724✔
975
                dstTableName, pTableSinkInfo->uid);
976
      } else {
977
        tqDebug("s-task:%s write %d rows into groupId:%" PRIu64 " dstTable:%s(not set uid yet for the secondary block)",
5,274✔
978
                id, numOfRows, groupId, dstTableName);
979
      }
980
    }
981
  } else {  // this groupId has not been kept in cache yet
982
    if (dstTableName[0] == 0) {
1,156✔
983
      memset(dstTableName, 0, TSDB_TABLE_NAME_LEN);
312✔
984
      code = buildCtbNameByGroupIdImpl(stbFullName, groupId, dstTableName);
312✔
985
      if (code) {
312!
986
        tqDebug("s-task:%s failed to build auto create table-name:%s, groupId:0x%" PRId64, id, dstTableName, groupId);
×
987
        return code;
×
988
      } else {
989
        tqDebug("s-task:%s no table name given, generated sub-table-name:%s, groupId:0x%" PRId64, id, dstTableName, groupId);
312!
990
      }
991
    } else {
992
      if (pTask->subtableWithoutMd5 != 1 && !isAutoTableName(dstTableName) &&
844!
993
          !alreadyAddGroupId(dstTableName, groupId) && groupId != 0) {
×
994
        tqDebug("s-task:%s append groupId:%" PRId64 " for generated dstTable:%s", id, groupId, dstTableName);
×
995
        if (pTask->ver == SSTREAM_TASK_SUBTABLE_CHANGED_VER) {
×
996
          code = buildCtbNameAddGroupId(NULL, dstTableName, groupId, sizeof(pDataBlock->info.parTbName));
×
997
        } else if (pTask->ver >= SSTREAM_TASK_APPEND_STABLE_NAME_VER && stbFullName) {
×
998
          code = buildCtbNameAddGroupId(stbFullName, dstTableName, groupId, sizeof(pDataBlock->info.parTbName));
×
999
        }
1000
        if (code != TSDB_CODE_SUCCESS) {
×
1001
          return code;
×
1002
        }
1003
      }
1004
    }
1005

1006
    code = doCreateSinkTableInfo(dstTableName, &pTableSinkInfo);
1,157✔
1007
    if (code == 0) {
1,156!
1008
      tqDebug("s-task:%s build new sinkTableInfo to add cache, dstTable:%s, groupId:%" PRId64, id, dstTableName, groupId);
1,156✔
1009
    } else {
1010
      tqDebug("s-task:%s failed to build new sinkTableInfo, dstTable:%s, groupId:%" PRId64, id, dstTableName, groupId);
×
1011
      return code;
×
1012
    }
1013
  }
1014

1015
  if (alreadyCached) {
14,144✔
1016
    pTableData->uid = pTableSinkInfo->uid;
12,988✔
1017

1018
    if (pTableData->uid == 0) {
12,988✔
1019
      tqTrace("s-task:%s cached tableInfo:%s uid is invalid, acquire it from meta", id, pTableSinkInfo->name.data);
5,473✔
1020
      return doWaitForDstTableCreated(pVnode, pTask, pTableSinkInfo, dstTableName, &pTableData->uid);
5,473✔
1021
    } else {
1022
      tqTrace("s-task:%s set the dstTable uid from cache:%" PRId64, id, pTableData->uid);
7,515✔
1023
    }
1024
  } else {
1025
    // The auto-create option will always set to be open for those submit messages, which arrives during the period
1026
    // the creating of the destination table, due to the absence of the user-specified table in TSDB. When scanning
1027
    // data from WAL, those submit messages, with auto-created table option, will be discarded expect the first, for
1028
    // those mismatched table uids. Only the FIRST table has the correct table uid, and those remain all have
1029
    // randomly generated, but false table uid in the WAL.
1030
    SMetaReader mr = {0};
1,156✔
1031
    metaReaderDoInit(&mr, pVnode->pMeta, META_READER_LOCK);
1,156✔
1032

1033
    // table not in cache, let's try to extract it from tsdb meta
1034
    if (metaGetTableEntryByName(&mr, dstTableName) < 0) {
1,156✔
1035
      metaReaderClear(&mr);
1,083✔
1036

1037
      if (pTask->outputInfo.tbSink.autoCreateCtb) {
1,083!
1038
        tqDebug("s-task:%s stream write into table:%s, table auto created", id, dstTableName);
1,083✔
1039

1040
        SArray* pTagArray = taosArrayInit(pTSchema->numOfCols + 1, sizeof(STagVal));
1,083✔
1041
        if (pTagArray == NULL) {
1,083!
1042
          tqError("s-task:%s failed to build auto create submit msg in sink, vgId:%d, due to %s", id, vgId,
×
1043
                  tstrerror(terrno));
1044
          return terrno;
×
1045
        }
1046

1047
        pTableData->flags = SUBMIT_REQ_AUTO_CREATE_TABLE;
1,083✔
1048
        code = buildAutoCreateTableReq(stbFullName, suid, pTSchema->numOfCols + 1, pDataBlock, pTagArray,
2,166✔
1049
                                       IS_NEW_SUBTB_RULE(pTask), &pTableData->pCreateTbReq, id);
1,083!
1050
        taosArrayDestroy(pTagArray);
1,083✔
1051

1052
        if (code) {
1,083!
1053
          tqError("s-task:%s failed to build auto create dst-table req:%s, code:%s", id, dstTableName, tstrerror(code));
×
1054
          taosMemoryFree(pTableSinkInfo);
×
1055
          return code;
×
1056
        }
1057

1058
        pTableSinkInfo->uid = 0;
1,083✔
1059
        code = doPutSinkTableInfoIntoCache(pTask->outputInfo.tbSink.pTbInfo, pTableSinkInfo, groupId, id);
1,083✔
1060
      } else {
1061
        tqError("s-task:%s vgId:%d dst-table:%s not auto-created, and not create in tsdb, discard data", id, vgId,
×
1062
                dstTableName);
1063
        return TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
1064
      }
1065
    } else {
1066
      bool isValid = isValidDstChildTable(&mr, vgId, dstTableName, suid);
73✔
1067
      if (!isValid) {
73!
1068
        metaReaderClear(&mr);
×
1069
        taosMemoryFree(pTableSinkInfo);
×
1070
        tqError("s-task:%s vgId:%d table:%s already exists, but not child table, stream results is discarded", id, vgId,
×
1071
                dstTableName);
1072
        return terrno;
×
1073
      } else {
1074
        pTableData->uid = mr.me.uid;
73✔
1075
        pTableSinkInfo->uid = mr.me.uid;
73✔
1076

1077
        metaReaderClear(&mr);
73✔
1078
        code = doPutSinkTableInfoIntoCache(pTask->outputInfo.tbSink.pTbInfo, pTableSinkInfo, groupId, id);
73✔
1079
      }
1080
    }
1081
  }
1082

1083
  return code;
8,671✔
1084
}
1085

1086
int32_t tqSetDstTableDataPayload(uint64_t suid, const STSchema* pTSchema, int32_t blockIndex, SSDataBlock* pDataBlock,
15,086✔
1087
                                 SSubmitTbData* pTableData, int64_t earlyTs, const char* id) {
1088
  int32_t numOfRows = pDataBlock->info.rows;
15,086✔
1089
  char*   dstTableName = pDataBlock->info.parTbName;
15,086✔
1090

1091
  tqDebug("s-task:%s sink data pipeline, build submit msg from %dth resBlock, including %d rows, dst suid:%" PRId64, id,
15,086✔
1092
          blockIndex + 1, numOfRows, suid);
1093

1094
  // convert all rows
1095
  int32_t code = doConvertRows(pTableData, pTSchema, pDataBlock, earlyTs, id);
15,086✔
1096
  if (code != TSDB_CODE_SUCCESS) {
15,089!
1097
    tqError("s-task:%s failed to convert rows from result block, code:%s", id, tstrerror(terrno));
×
1098
    return code;
×
1099
  }
1100

1101
  if (pTableData->aRowP != NULL) {
15,089!
1102
    taosArraySort(pTableData->aRowP, tsAscendingSortFn);
15,089✔
1103
    tqTrace("s-task:%s build submit msg for dstTable:%s, numOfRows:%d", id, dstTableName, numOfRows);
15,085✔
1104
  }
1105

1106
  return code;
15,085✔
1107
}
1108

1109
int32_t checkTagSchema(SStreamTask* pTask, SVnode* pVnode) {
10,809✔
1110
  int32_t          code = TSDB_CODE_SUCCESS;
10,809✔
1111
  const char*      id = pTask->id.idStr;
10,809✔
1112
  STaskOutputInfo* pOutputInfo = &pTask->outputInfo;
10,809✔
1113
  int32_t          vgId = pTask->pMeta->vgId;
10,809✔
1114

1115
  if (pTask->outputInfo.tbSink.pTagSchema == NULL) {
10,809✔
1116
    SMetaReader mer1 = {0};
2,931✔
1117
    metaReaderDoInit(&mer1, pVnode->pMeta, META_READER_LOCK);
2,931✔
1118

1119
    code = metaReaderGetTableEntryByUid(&mer1, pOutputInfo->tbSink.stbUid);
2,931✔
1120
    if (code != TSDB_CODE_SUCCESS) {
2,931!
1121
      tqError("s-task:%s vgId:%d failed to get the dst stable, failed to sink results", id, vgId);
×
1122
      metaReaderClear(&mer1);
×
1123
      return code;
×
1124
    }
1125

1126
    pOutputInfo->tbSink.pTagSchema = tCloneSSchemaWrapper(&mer1.me.stbEntry.schemaTag);
2,931✔
1127
    metaReaderClear(&mer1);
2,931✔
1128

1129
    if (pOutputInfo->tbSink.pTagSchema == NULL) {
2,931!
1130
      tqError("s-task:%s failed to clone tag schema, code:%s, failed to sink results", id, tstrerror(terrno));
×
1131
      return terrno;
×
1132
    }
1133

1134
    SSchemaWrapper* pTagSchema = pOutputInfo->tbSink.pTagSchema;
2,931✔
1135
    SSchema*        pCol1 = &pTagSchema->pSchema[0];
2,931✔
1136
    if (pTagSchema->nCols == 1 && pCol1->type == TSDB_DATA_TYPE_UBIGINT && strcmp(pCol1->name, "group_id") == 0) {
2,931!
1137
      pOutputInfo->tbSink.autoCreateCtb = true;
2,058✔
1138
    } else {
1139
      pOutputInfo->tbSink.autoCreateCtb = false;
873✔
1140
    }
1141
  }
1142

1143
  return code;
10,809✔
1144
}
1145

1146
void tqSinkDataIntoDstTable(SStreamTask* pTask, void* vnode, void* data) {
10,804✔
1147
  const SArray*    pBlocks = (const SArray*)data;
10,804✔
1148
  SVnode*          pVnode = (SVnode*)vnode;
10,804✔
1149
  int64_t          suid = pTask->outputInfo.tbSink.stbUid;
10,804✔
1150
  char*            stbFullName = pTask->outputInfo.tbSink.stbFullName;
10,804✔
1151
  STSchema*        pTSchema = pTask->outputInfo.tbSink.pTSchema;
10,804✔
1152
  int32_t          vgId = TD_VID(pVnode);
10,804✔
1153
  int32_t          numOfBlocks = taosArrayGetSize(pBlocks);
10,804✔
1154
  int32_t          code = TSDB_CODE_SUCCESS;
10,810✔
1155
  const char*      id = pTask->id.idStr;
10,810✔
1156
  int64_t          earlyTs = tsdbGetEarliestTs(pVnode->pTsdb);
10,810✔
1157
  STaskOutputInfo* pOutputInfo = &pTask->outputInfo;
10,810✔
1158

1159
  code = checkTagSchema(pTask, pVnode);
10,810✔
1160
  if (code != TSDB_CODE_SUCCESS) {
10,810!
1161
    return;
×
1162
  }
1163

1164
  code = tqSendAllNotifyEvents(pBlocks, pTask, pVnode);
10,810✔
1165
  if (code != TSDB_CODE_SUCCESS) {
10,810!
1166
    tqError("vgId: %d, s-task:%s failed to send all event notifications", vgId, id);
×
1167
    // continue processing even if notification fails
1168
  }
1169

1170
  bool onlySubmitData = hasOnlySubmitData(pBlocks, numOfBlocks);
10,810✔
1171
  if (!onlySubmitData || pTask->subtableWithoutMd5 == 1) {
10,811✔
1172
    tqDebug("vgId:%d, s-task:%s write %d stream resBlock(s) into table, has other type block, submit one-by-one", vgId,
5,566✔
1173
            id, numOfBlocks);
1174

1175
    for (int32_t i = 0; i < numOfBlocks; ++i) {
22,450✔
1176
      if (streamTaskShouldStop(pTask)) {
16,881!
UNCOV
1177
        return;
×
1178
      }
1179

1180
      SSDataBlock* pDataBlock = taosArrayGet(pBlocks, i);
16,882✔
1181
      if (pDataBlock == NULL) {
16,881!
1182
        continue;
×
1183
      }
1184

1185
      if (pDataBlock->info.type == STREAM_DELETE_RESULT) {
16,881✔
1186
        code = doBuildAndSendDeleteMsg(pVnode, stbFullName, pDataBlock, pTask, suid);
2,484✔
1187
      } else if (pDataBlock->info.type == STREAM_CREATE_CHILD_TABLE) {
14,397✔
1188
        code = doBuildAndSendCreateTableMsg(pVnode, stbFullName, pDataBlock, pTask, suid);
6,142✔
1189
      } else if (pDataBlock->info.type == STREAM_CHECKPOINT) {
8,255!
1190
        continue;
×
1191
      } else if (pDataBlock->info.type == STREAM_DROP_CHILD_TABLE && pTask->subtableWithoutMd5) {
8,255!
1192
        code = doBuildAndSendDropTableMsg(pVnode, stbFullName, pDataBlock, pTask, suid);
×
1193
      } else if (pDataBlock->info.type == STREAM_NOTIFY_EVENT) {
8,255!
1194
        continue;
×
1195
      } else {
1196
        code = handleResultBlockMsg(pTask, pDataBlock, i, pVnode, earlyTs);
8,255✔
1197
      }
1198
    }
1199
  } else {
1200
    tqDebug("vgId:%d, s-task:%s write %d stream resBlock(s) into table, merge submit msg", vgId, id, numOfBlocks);
5,245✔
1201
    if (streamTaskShouldStop(pTask)) {
5,245!
1202
      return;
×
1203
    }
1204

1205
    rebuildAndSendMultiResBlock(pTask, pBlocks, pVnode, earlyTs);
5,245✔
1206
  }
1207
}
1208

1209
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
1210
bool hasOnlySubmitData(const SArray* pBlocks, int32_t numOfBlocks) {
10,809✔
1211
  for (int32_t i = 0; i < numOfBlocks; ++i) {
18,735✔
1212
    SSDataBlock* p = taosArrayGet(pBlocks, i);
13,162✔
1213
    if (p == NULL) {
13,162!
1214
      continue;
×
1215
    }
1216

1217
    if (p->info.type == STREAM_DELETE_RESULT || p->info.type == STREAM_CREATE_CHILD_TABLE) {
13,162✔
1218
      return false;
5,236✔
1219
    }
1220
  }
1221

1222
  return true;
5,573✔
1223
}
1224

1225
int32_t doPutSinkTableInfoIntoCache(SSHashObj* pSinkTableMap, STableSinkInfo* pTableSinkInfo, uint64_t groupId, const char* id) {
6,156✔
1226
  int32_t code = tSimpleHashPut(pSinkTableMap, &groupId, sizeof(uint64_t), &pTableSinkInfo, POINTER_BYTES);
6,156✔
1227
  if (code != TSDB_CODE_SUCCESS) {
6,156!
1228
    taosMemoryFreeClear(pTableSinkInfo);
×
1229
  } else {
1230
    tqDebug("s-task:%s new dst table:%s(uid:%" PRIu64 ") added into cache, total:%d", id, pTableSinkInfo->name.data,
6,156✔
1231
            pTableSinkInfo->uid, tSimpleHashGetSize(pSinkTableMap));
1232
  }
1233

1234
  return code;
6,156✔
1235
}
1236

1237
bool doGetSinkTableInfoFromCache(SSHashObj* pTableInfoMap, uint64_t groupId, STableSinkInfo** pInfo) {
20,288✔
1238
  void* pVal = tSimpleHashGet(pTableInfoMap, &groupId, sizeof(uint64_t));
20,288✔
1239
  if (pVal) {
20,287✔
1240
    *pInfo = *(STableSinkInfo**)pVal;
14,132✔
1241
    return true;
14,132✔
1242
  }
1243

1244
  return false;
6,155✔
1245
}
1246

1247
int32_t doRemoveSinkTableInfoInCache(SSHashObj* pSinkTableMap, uint64_t groupId, const char* id) {
×
1248
  if (tSimpleHashGetSize(pSinkTableMap) == 0) {
×
1249
    return TSDB_CODE_SUCCESS;
×
1250
  }
1251

1252
  int32_t code = tSimpleHashRemove(pSinkTableMap, &groupId, sizeof(groupId));
×
1253
  if (code == 0) {
×
1254
    tqDebug("s-task:%s remove cached table meta for groupId:%" PRId64, id, groupId);
×
1255
  } else {
1256
    tqError("s-task:%s failed to remove table meta from hashmap, groupId:%" PRId64, id, groupId);
×
1257
  }
1258
  return code;
×
1259
}
1260

1261
int32_t doBuildAndSendDeleteMsg(SVnode* pVnode, char* stbFullName, SSDataBlock* pDataBlock, SStreamTask* pTask,
2,483✔
1262
                                int64_t suid) {
1263
  SBatchDeleteReq deleteReq = {.suid = suid, .deleteReqs = taosArrayInit(0, sizeof(SSingleDeleteReq))};
2,483✔
1264
  if (deleteReq.deleteReqs == NULL) {
2,484!
1265
    return terrno;
×
1266
  }
1267

1268
  int32_t code =
1269
      tqBuildDeleteReq(pVnode->pTq, stbFullName, pDataBlock, &deleteReq, pTask->id.idStr, IS_NEW_SUBTB_RULE(pTask));
2,484!
1270
  if (code != TSDB_CODE_SUCCESS) {
2,483!
1271
    return code;
×
1272
  }
1273

1274
  if (taosArrayGetSize(deleteReq.deleteReqs) == 0) {
2,483!
1275
    taosArrayDestroy(deleteReq.deleteReqs);
×
1276
    return TSDB_CODE_SUCCESS;
×
1277
  }
1278

1279
  int32_t len;
1280
  tEncodeSize(tEncodeSBatchDeleteReq, &deleteReq, len, code);
2,483!
1281
  if (code != TSDB_CODE_SUCCESS) {
2,480!
1282
    qError("s-task:%s failed to encode delete request", pTask->id.idStr);
×
1283
    return code;
×
1284
  }
1285

1286
  SEncoder encoder = {0};
2,480✔
1287
  void*    serializedDeleteReq = rpcMallocCont(len + sizeof(SMsgHead));
2,480✔
1288
  void*    abuf = POINTER_SHIFT(serializedDeleteReq, sizeof(SMsgHead));
2,483✔
1289
  tEncoderInit(&encoder, abuf, len);
2,483✔
1290
  code = tEncodeSBatchDeleteReq(&encoder, &deleteReq);
2,484✔
1291
  tEncoderClear(&encoder);
2,484✔
1292
  taosArrayDestroy(deleteReq.deleteReqs);
2,484✔
1293

1294
  if (code) {
2,485!
1295
    return code;
×
1296
  }
1297

1298
  ((SMsgHead*)serializedDeleteReq)->vgId = TD_VID(pVnode);
2,485✔
1299

1300
  SRpcMsg msg = {.msgType = TDMT_VND_BATCH_DEL, .pCont = serializedDeleteReq, .contLen = len + sizeof(SMsgHead)};
2,485✔
1301
  if (tmsgPutToQueue(&pVnode->msgCb, WRITE_QUEUE, &msg) != 0) {
2,485!
1302
    tqDebug("failed to put delete req into write-queue since %s", terrstr());
×
1303
  }
1304

1305
  return TSDB_CODE_SUCCESS;
2,485✔
1306
}
1307

1308
void rebuildAndSendMultiResBlock(SStreamTask* pTask, const SArray* pBlocks, SVnode* pVnode, int64_t earlyTs) {
5,244✔
1309
  int32_t     code = 0;
5,244✔
1310
  const char* id = pTask->id.idStr;
5,244✔
1311
  int32_t     vgId = pTask->pMeta->vgId;
5,244✔
1312
  int32_t     numOfBlocks = taosArrayGetSize(pBlocks);
5,244✔
1313
  int64_t     suid = pTask->outputInfo.tbSink.stbUid;
5,244✔
1314
  STSchema*   pTSchema = pTask->outputInfo.tbSink.pTSchema;
5,244✔
1315
  char*       stbFullName = pTask->outputInfo.tbSink.stbFullName;
5,244✔
1316

1317
  SHashObj* pTableIndexMap =
1318
      taosHashInit(numOfBlocks, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
5,244✔
1319

1320
  SSubmitReq2 submitReq = {.aSubmitTbData = taosArrayInit(1, sizeof(SSubmitTbData))};
5,246✔
1321
  if (submitReq.aSubmitTbData == NULL) {
5,247!
1322
    code = terrno;
×
1323
    tqError("s-task:%s vgId:%d failed to prepare submit msg in sink task, code:%s", id, vgId, tstrerror(code));
×
1324
    taosHashCleanup(pTableIndexMap);
×
1325
    return;
×
1326
  }
1327

1328
  bool hasSubmit = false;
5,247✔
1329
  for (int32_t i = 0; i < numOfBlocks; i++) {
12,077✔
1330
    SSDataBlock* pDataBlock = taosArrayGet(pBlocks, i);
6,831✔
1331
    if (pDataBlock == NULL) {
6,831!
1332
      continue;
×
1333
    }
1334

1335
    if (pDataBlock->info.type == STREAM_CHECKPOINT) {
6,831!
1336
      continue;
×
1337
    }
1338

1339
    if (pDataBlock->info.type == STREAM_NOTIFY_EVENT) {
6,831!
1340
      continue;
×
1341
    }
1342

1343
    hasSubmit = true;
6,831✔
1344
    pTask->execInfo.sink.numOfBlocks += 1;
6,831✔
1345
    uint64_t groupId = pDataBlock->info.id.groupId;
6,831✔
1346

1347
    SSubmitTbData tbData = {.suid = suid, .uid = 0, .sver = pTSchema->version, .flags = TD_REQ_FROM_APP};
6,831✔
1348

1349
    int32_t* index = taosHashGet(pTableIndexMap, &groupId, sizeof(groupId));
6,831✔
1350
    if (index == NULL) {  // no data yet, append it
6,831✔
1351
      code = setDstTableDataUid(pVnode, pTask, pDataBlock, stbFullName, &tbData);
5,886✔
1352
      if (code != TSDB_CODE_SUCCESS) {
5,885!
1353
        tqError("vgId:%d dst-table gid:%" PRId64 " not exist, discard stream results", vgId, groupId);
×
1354
        continue;
×
1355
      }
1356

1357
      code = tqSetDstTableDataPayload(suid, pTSchema, i, pDataBlock, &tbData, earlyTs, id);
5,885✔
1358
      if (code != TSDB_CODE_SUCCESS || tbData.aRowP == NULL) {
5,886!
1359
        if (tbData.pCreateTbReq != NULL) {
×
1360
          tdDestroySVCreateTbReq(tbData.pCreateTbReq);
×
1361
          (void)doRemoveSinkTableInfoInCache(pTask->outputInfo.tbSink.pTbInfo, groupId, id);
×
1362
          tbData.pCreateTbReq = NULL;
×
1363
        }
1364
        continue;
×
1365
      }
1366

1367
      void* p = taosArrayPush(submitReq.aSubmitTbData, &tbData);
5,886✔
1368
      if (p == NULL) {
5,885!
1369
        tqError("vgId:%d, s-task:%s failed to build submit msg, data lost", vgId, id);
×
1370
        continue;
×
1371
      }
1372

1373
      int32_t size = (int32_t)taosArrayGetSize(submitReq.aSubmitTbData) - 1;
5,885✔
1374
      code = taosHashPut(pTableIndexMap, &groupId, sizeof(groupId), &size, sizeof(size));
5,885✔
1375
      if (code) {
5,885!
1376
        tqError("vgId:%d, s-task:%s failed to put group into index map, code:%s", vgId, id, tstrerror(code));
×
1377
        continue;
×
1378
      }
1379
    } else {
1380
      code = tqSetDstTableDataPayload(suid, pTSchema, i, pDataBlock, &tbData, earlyTs, id);
945✔
1381
      if (code != TSDB_CODE_SUCCESS || tbData.aRowP == NULL) {
943!
1382
        if (tbData.pCreateTbReq != NULL) {
×
1383
          tdDestroySVCreateTbReq(tbData.pCreateTbReq);
×
1384
          tbData.pCreateTbReq = NULL;
×
1385
        }
1386
        continue;
×
1387
      }
1388

1389
      SSubmitTbData* pExisted = taosArrayGet(submitReq.aSubmitTbData, *index);
943✔
1390
      if (pExisted == NULL) {
943!
1391
        continue;
×
1392
      }
1393

1394
      code = doMergeExistedRows(pExisted, &tbData, id);
943✔
1395
      if (code != TSDB_CODE_SUCCESS) {
945!
1396
        continue;
×
1397
      }
1398
    }
1399

1400
    pTask->execInfo.sink.numOfRows += pDataBlock->info.rows;
6,830✔
1401
  }
1402

1403
  taosHashCleanup(pTableIndexMap);
5,246✔
1404

1405
  if (hasSubmit) {
5,247!
1406
    code = doBuildAndSendSubmitMsg(pVnode, pTask, &submitReq, numOfBlocks);
5,247✔
1407
    if (code) {  // failed and continue
5,247!
1408
      tqError("vgId:%d failed to build and send submit msg", vgId);
×
1409
    }
1410
  } else {
1411
    tDestroySubmitReq(&submitReq, TSDB_MSG_FLG_ENCODE);
×
1412
    tqDebug("vgId:%d, s-task:%s write results completed", vgId, id);
×
1413
  }
1414
}
1415

1416
int32_t handleResultBlockMsg(SStreamTask* pTask, SSDataBlock* pDataBlock, int32_t index, SVnode* pVnode, int64_t earlyTs) {
8,259✔
1417
  int32_t     code = 0;
8,259✔
1418
  STSchema*   pTSchema = pTask->outputInfo.tbSink.pTSchema;
8,259✔
1419
  int64_t     suid = pTask->outputInfo.tbSink.stbUid;
8,259✔
1420
  const char* id = pTask->id.idStr;
8,259✔
1421
  int32_t     vgId = TD_VID(pVnode);
8,259✔
1422
  char*       stbFullName = pTask->outputInfo.tbSink.stbFullName;
8,259✔
1423

1424
  pTask->execInfo.sink.numOfBlocks += 1;
8,259✔
1425

1426
  SSubmitReq2 submitReq = {.aSubmitTbData = taosArrayInit(1, sizeof(SSubmitTbData))};
8,259✔
1427
  if (submitReq.aSubmitTbData == NULL) {
8,259!
1428
    tqError("s-task:%s vgId:%d failed to prepare submit msg in sink task, code:%s", id, vgId, tstrerror(terrno));
×
1429
    return terrno;
×
1430
  }
1431

1432
  SSubmitTbData tbData = {.suid = suid, .uid = 0, .sver = pTSchema->version, .flags = TD_REQ_FROM_APP};
8,259✔
1433
  code = setDstTableDataUid(pVnode, pTask, pDataBlock, stbFullName, &tbData);
8,259✔
1434
  if (code != TSDB_CODE_SUCCESS) {
8,257✔
1435
    tqError("vgId:%d s-task:%s dst-table not exist, stb:%s discard stream results", vgId, id, stbFullName);
1!
1436
    tDestroySubmitReq(&submitReq, TSDB_MSG_FLG_ENCODE);
1✔
1437
    return code;
1✔
1438
  }
1439

1440
  code = tqSetDstTableDataPayload(suid, pTSchema, index, pDataBlock, &tbData, earlyTs, id);
8,256✔
1441
  if (code != TSDB_CODE_SUCCESS || tbData.aRowP == NULL) {
8,257!
1442
    if (tbData.pCreateTbReq != NULL) {
×
1443
      tdDestroySVCreateTbReq(tbData.pCreateTbReq);
×
1444
      (void)doRemoveSinkTableInfoInCache(pTask->outputInfo.tbSink.pTbInfo, pDataBlock->info.id.groupId, id);
×
1445
      tbData.pCreateTbReq = NULL;
×
1446
    }
1447

1448
    tDestroySubmitReq(&submitReq, TSDB_MSG_FLG_ENCODE);
×
1449
    return code;
×
1450
  }
1451

1452
  void* p = taosArrayPush(submitReq.aSubmitTbData, &tbData);
8,257✔
1453
  if (p == NULL) {
8,257!
1454
    tqDebug("vgId:%d, s-task:%s failed to build submit msg, code:%s, data lost", vgId, id, tstrerror(terrno));
×
1455
    tDestroySubmitReq(&submitReq, TSDB_MSG_FLG_ENCODE);
×
1456
    return terrno;
×
1457
  }
1458

1459
  code = doBuildAndSendSubmitMsg(pVnode, pTask, &submitReq, 1);
8,257✔
1460
  if (code) {  // failed and continue
8,258!
1461
    tqDebug("vgId:%d, s-task:%s submit msg failed, code:%s data lost", vgId, id, tstrerror(code));
×
1462
  }
1463

1464
  return code;
8,258✔
1465
}
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