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

taosdata / TDengine / #5071

17 May 2026 01:15AM UTC coverage: 63.054% (-10.3%) from 73.326%
#5071

push

travis-ci

web-flow
feat (TDgpt): Dynamic Model Synchronization Enhancements (#35344)

* refactor: do some internal refactor.

* fix: fix multiprocess sync issue.

* feat: add dynamic anomaly detection and forecasting services

* fix: log error message for undeploying model in exception handling

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* fix: handle undeploy when model exists only on disk

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/286aafa0-c3ce-4c27-b803-2707571e9dc1

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* fix: guard dynamic registry concurrent access

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/5e4db858-6458-40f4-ac28-d1b1b7f97c18

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* fix: tighten service list locking scope

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/5e4db858-6458-40f4-ac28-d1b1b7f97c18

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* fix: restore prophet support and update tests per review feedback

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/92298ae1-7da6-4d07-b20e-101c7cd0b26b

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* fix: improve test name and move copy inside lock scope

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/92298ae1-7da6-4d07-b20e-101c7cd0b26b

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* Potential fix for pull request finding

Co-au... (continued)

238317 of 377957 relevant lines covered (63.05%)

130539817.12 hits per line

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

76.03
/source/dnode/vnode/src/tq/tqScan.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 "taoserror.h"
17
#include "tarray.h"
18
#include "tdef.h"
19
#include "thash.h"
20
#include "tmsg.h"
21
#include "tpriv.h"
22
#include "tq.h"
23

24
static int32_t tqRetrieveCols(STqReader *pReader, SSDataBlock *pRes, SHashObj* pCol2SlotId);
25
static int32_t tqAddTableListForStbSub(STqHandle* pTqHandle, STQ* pTq, const SArray* tbUidList, int64_t version);
26
static void    tqAlterTagForStbSub(SVnode *pVnode, const SArray* tbUidList, const SArray* tags, const SArray* tagsArray, STqHandle* pTqHandle, int64_t version);
27
static int32_t tqSendMetaPollRsp(STqHandle* pHandle, const SRpcMsg* pMsg, const SMqPollReq* pReq,
28
                                 const SMqMetaRsp* pRsp, int32_t vgId);
29
static int32_t tqSendBatchMetaPollRsp(STqHandle* pHandle, const SRpcMsg* pMsg, const SMqPollReq* pReq,
30
                                      const SMqBatchMetaRsp* pRsp, int32_t vgId);
31
                                      
32
static void tqProcessCreateTbMsg(SDecoder* dcoder, SWalCont* pHead, STQ* pTq, STqHandle* pHandle, int64_t* realTbSuid, int64_t tbSuid) {
66,995✔
33
  int32_t code = 0;
66,995✔
34
  int32_t lino = 0;
66,995✔
35
  SVCreateTbReq* pCreateReq = NULL;
66,995✔
36
  SVCreateTbBatchReq reqNew = {0};
66,995✔
37
  void* buf = NULL;
66,995✔
38
  SArray *tbUids = NULL;
66,995✔
39
  SVCreateTbBatchReq req = {0};
66,995✔
40
  code = tDecodeSVCreateTbBatchReq(dcoder, &req);
66,995✔
41
  TSDB_CHECK_CODE(code, lino, end);
66,583✔
42

43
  STqExecHandle* pExec = &pHandle->execHandle;
66,583✔
44
  STqReader* pReader = pExec->pTqReader;
66,583✔
45

46
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
66,583✔
47
  TSDB_CHECK_NULL(tbUids, code, lino, end, terrno);
66,995✔
48
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
146,014✔
49
    pCreateReq = req.pReqs + iReq;
79,019✔
50
    if ((pCreateReq->type == TSDB_CHILD_TABLE || pCreateReq->type == TSDB_VIRTUAL_CHILD_TABLE) && pCreateReq->ctb.suid == tbSuid) {
79,019✔
51
      TSDB_CHECK_NULL(taosArrayPush(tbUids, &pCreateReq->uid), code, lino, end, terrno);
139,610✔
52
    }
53
  }
54
  TSDB_CHECK_CONDITION(taosArrayGetSize(tbUids) != 0, code, lino, end, TSDB_CODE_SUCCESS);
66,995✔
55

56
  taosWLockLatch(&pTq->lock);
57,781✔
57
  tqReaderRemoveTbUidList(pHandle->execHandle.pTqReader, tbUids);
57,781✔
58
  code = tqAddTableListForStbSub(pHandle, pTq, tbUids, pHead->version);
57,781✔
59
  taosWUnLockLatch(&pTq->lock);
57,781✔
60
  TSDB_CHECK_CODE(code, lino, end);
57,781✔
61

62
  reqNew.pArray = taosArrayInit(req.nReqs, sizeof(struct SVCreateTbReq));
57,781✔
63
  TSDB_CHECK_NULL(reqNew.pArray, code, lino, end, terrno);
57,781✔
64
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
127,586✔
65
    pCreateReq = req.pReqs + iReq;
69,805✔
66
    if ((pCreateReq->type == TSDB_CHILD_TABLE || pCreateReq->type == TSDB_VIRTUAL_CHILD_TABLE) &&
69,805✔
67
        pCreateReq->ctb.suid == tbSuid &&
139,610✔
68
        taosHashGet(pReader->tbIdHash, &pCreateReq->uid, sizeof(int64_t)) != NULL) {
69,805✔
69
      TSDB_CHECK_NULL(taosArrayPush(reqNew.pArray, pCreateReq), code, lino, end, terrno);
107,634✔
70
      reqNew.nReqs++;
53,817✔
71
    }
72
  }
73

74
  TSDB_CHECK_CONDITION(reqNew.nReqs != 0, code, lino, end, TSDB_CODE_SUCCESS);
57,781✔
75
  *realTbSuid = tbSuid;
51,101✔
76

77
  int     tlen = 0;
51,101✔
78
  tEncodeSize(tEncodeSVCreateTbBatchReq, &reqNew, tlen, code);
51,101✔
79
  TSDB_CHECK_CODE(code, lino, end);
51,101✔
80

81
  buf = taosMemoryMalloc(tlen);
51,101✔
82
  TSDB_CHECK_NULL(buf, code, lino, end, terrno);
51,101✔
83

84
  SEncoder coderNew = {0};
51,101✔
85
  tEncoderInit(&coderNew, buf, tlen);
51,101✔
86
  code = tEncodeSVCreateTbBatchReq(&coderNew, &reqNew);
51,101✔
87
  tEncoderClear(&coderNew);
51,101✔
88
  TSDB_CHECK_CODE(code, lino, end);
51,101✔
89

90
  if (tlen + sizeof(SMsgHead) > pHead->bodyLen) {
51,101✔
91
    tqError("vgId:%d, %s failed at %s:%d since buffer overflow", TD_VID(pTq->pVnode), __func__, __FILE__, __LINE__);
×
92
  } else {
93
    (void)memcpy(pHead->body + sizeof(SMsgHead), buf, tlen);
51,101✔
94
    pHead->bodyLen = tlen + sizeof(SMsgHead);
51,101✔
95
  }
96

97
end:
66,995✔
98
  taosMemoryFree(buf);
66,995✔
99
  taosArrayDestroy(reqNew.pArray);
66,995✔
100
  tDeleteSVCreateTbBatchReq(&req);
66,995✔
101
  taosArrayDestroy(tbUids);
66,995✔
102
  if (code < 0) {
66,995✔
103
    tqError("tqProcessCreateTbMsg failed, code:%d, line:%d", code, lino);
×
104
  }
105
}
66,995✔
106

107
static int32_t tqGetUidSuid(SMeta* pMeta, const char* tbName, int64_t* uid, int64_t* suid){
12,451✔
108
  SMetaReader mr = {0};
12,451✔
109

110
  metaReaderDoInit(&mr, pMeta, META_READER_LOCK);
12,451✔
111
  int32_t code = metaGetTableEntryByName(&mr, tbName);
12,451✔
112
  if (code == 0) {
12,451✔
113
    *uid = mr.me.uid;
11,641✔
114
    *suid = mr.me.ctbEntry.suid;
11,641✔
115
  } else {
116
    tqError("tqGetUidSuid failed at %s:%d since table %s not found, code:%d", __FILE__, __LINE__, tbName, code);
810✔
117
  }
118

119
  metaReaderClear(&mr);
12,451✔
120
  return code;
12,451✔
121
}
122

123
// Cache entry for uid lookup results, avoiding duplicate tqGetUidSuid calls
124
typedef struct {
125
  int64_t uid;
126
  bool    valid;   // true if tqGetUidSuid succeeded and suid matches tbSuid
127
} SAlterTagUidCache;
128

129
static void tqAlterMultiTag(SVAlterTbReq* req, SWalCont* pHead, STQ* pTq, STqHandle* pHandle, int64_t* realTbSuid, int64_t tbSuid) {
6,047✔
130
  int32_t lino = 0;
6,047✔
131
  int32_t code = 0;
6,047✔
132
  SVAlterTbReq reqNew = {0};
6,047✔
133
  SArray* uidList = NULL;
6,047✔
134
  SArray* tagListArray = NULL;
6,047✔
135
  SAlterTagUidCache* uidCache = NULL;
6,047✔
136
  void* buf = NULL;
6,047✔
137

138
  STqExecHandle* pExec = &pHandle->execHandle;
6,047✔
139
  STqReader* pReader = pExec->pTqReader;
6,047✔
140

141
  int32_t nTables = taosArrayGetSize(req->tables);
6,047✔
142
  uidList = taosArrayInit(nTables, sizeof(tb_uid_t));
6,047✔
143
  TSDB_CHECK_NULL(uidList, code, lino, end, terrno);
6,047✔
144

145
  tagListArray = taosArrayInit(nTables, sizeof(void*));
6,047✔
146
  TSDB_CHECK_NULL(tagListArray, code, lino, end, terrno);
6,047✔
147

148
  // Cache uid lookup results for reuse in the second pass
149
  uidCache = taosMemoryCalloc(nTables, sizeof(SAlterTagUidCache));
6,047✔
150
  TSDB_CHECK_NULL(uidCache, code, lino, end, terrno);
6,047✔
151

152
  // First pass: resolve uid/suid once per table, collect for tag update notification
153
  for (int32_t i = 0; i < nTables; i++) {
13,734✔
154
    SUpdateTableTagVal *pTable = taosArrayGet(req->tables, i);
7,687✔
155
    if (pTable == NULL || pTable->tbName == NULL) {
7,687✔
156
      continue;
×
157
    }
158
    int64_t uid = 0;
7,687✔
159
    int64_t suid = 0;
7,687✔
160
    int32_t ret = tqGetUidSuid(pTq->pVnode->pMeta, pTable->tbName, &uid, &suid);
7,687✔
161
    if (ret != 0) {
7,687✔
162
      tqError("vgId:%d, %s failed at %s:%d since table %s not found", TD_VID(pTq->pVnode), __func__, __FILE__, __LINE__, pTable->tbName);
810✔
163
      continue;
810✔
164
    }
165
    if (suid != tbSuid) continue;
6,877✔
166

167
    uidCache[i].uid = uid;
6,877✔
168
    uidCache[i].valid = true;
6,877✔
169

170
    if (taosArrayPush(uidList, &uid) == NULL) {
6,877✔
171
      tqError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pTq->pVnode), __func__, __FILE__, __LINE__, tstrerror(terrno));
×
172
      continue;
×
173
    }
174
    if (taosArrayPush(tagListArray, &pTable->tags) == NULL){
13,754✔
175
      void* popRet = taosArrayPop(uidList);  // keep uidList and tagListArray in sync
×
176
      tqError("vgId:%d, %s failed at %s:%d since %s, ret:%p", TD_VID(pTq->pVnode), __func__, __FILE__, __LINE__, tstrerror(terrno), popRet);
×
177
      continue;
×
178
    }
179
  }
180

181
  // tqAlterTagForStbSub may modify pReader->tbIdHash, so must run before the second pass
182
  if (taosArrayGetSize(uidList) > 0) {
6,047✔
183
    tqAlterTagForStbSub(pTq->pVnode, uidList, NULL, tagListArray, pHandle, pHead->version);
5,237✔
184
  }
185

186
  // Build filtered message
187
  reqNew.action = req->action;
6,047✔
188
  reqNew.tbName = req->tbName;
6,047✔
189
  reqNew.tables = taosArrayInit(nTables, sizeof(SUpdateTableTagVal));
6,047✔
190
  TSDB_CHECK_NULL(reqNew.tables, code, lino, end, terrno);
6,047✔
191

192
  // Second pass: filter subscribed tables using cached uid (no repeated tqGetUidSuid calls)
193
  for (int32_t i = 0; i < nTables; i++) {
13,734✔
194
    if (!uidCache[i].valid) continue;
7,687✔
195
    SUpdateTableTagVal* pTable = taosArrayGet(req->tables, i);
6,877✔
196
    if (pTable == NULL) continue;
6,877✔
197

198
    if (taosHashGet(pReader->tbIdHash, &uidCache[i].uid, sizeof(int64_t)) != NULL) {
6,877✔
199
      TSDB_CHECK_NULL(taosArrayPush(reqNew.tables, pTable), code, lino, end, terrno);
10,546✔
200
    }
201
  }
202

203
  TSDB_CHECK_CONDITION(taosArrayGetSize(reqNew.tables) != 0, code, lino, end, TSDB_CODE_SUCCESS);
6,047✔
204

205
  *realTbSuid = tbSuid;
4,043✔
206

207
  // Encode filtered message
208
  int tlen = 0;
4,043✔
209
  tEncodeSize(tEncodeSVAlterTbReq, &reqNew, tlen, code);
4,043✔
210
  TSDB_CHECK_CODE(code, lino, end);
4,043✔
211

212
  buf = taosMemoryMalloc(tlen);
4,043✔
213
  TSDB_CHECK_NULL(buf, code, lino, end, terrno);
4,043✔
214

215
  SEncoder coderNew = {0};
4,043✔
216
  tEncoderInit(&coderNew, buf, tlen);
4,043✔
217
  code = tEncodeSVAlterTbReq(&coderNew, &reqNew);
4,043✔
218
  tEncoderClear(&coderNew);
4,043✔
219
  TSDB_CHECK_CODE(code, lino, end);
4,043✔
220

221
  if (tlen + sizeof(SMsgHead) > pHead->bodyLen) {
4,043✔
222
    tqError("vgId:%d, %s failed at %s:%d since buffer overflow", TD_VID(pTq->pVnode), __func__, __FILE__, __LINE__);
3,633✔
223
  } else {
224
    (void)memcpy(pHead->body + sizeof(SMsgHead), buf, tlen);
410✔
225
    pHead->bodyLen = tlen + sizeof(SMsgHead);
410✔
226
  }
227

228
end:
6,047✔
229
  taosArrayDestroy(uidList);
6,047✔
230
  taosArrayDestroy(tagListArray);
6,047✔
231
  taosMemoryFree(uidCache);
6,047✔
232
  taosMemoryFree(buf);
6,047✔
233
  taosArrayDestroy(reqNew.tables);
6,047✔
234
  if (code != 0) {
6,047✔
235
    tqError("%s failed, code:%d, line:%d", __func__, code, lino);
×
236
  }
237
}
6,047✔
238

239
static void tqProcessAlterTbMsg(SDecoder* dcoder, SWalCont* pHead, STQ* pTq, STqHandle* pHandle, int64_t* realTbSuid, int64_t tbSuid) {
11,631✔
240
  SVAlterTbReq req = {0};
11,631✔
241
  SArray* uidList = NULL;
11,631✔
242
  int32_t lino = 0;
11,631✔
243
  int32_t code = tDecodeSVAlterTbReq(dcoder, &req);
11,631✔
244
  TSDB_CHECK_CODE(code, lino, end);
11,631✔
245

246
  if (req.action == TSDB_ALTER_TABLE_UPDATE_MULTI_TABLE_TAG_VAL) {
11,631✔
247
    tqAlterMultiTag(&req, pHead, pTq, pHandle, realTbSuid, tbSuid);
6,047✔
248
  } else if (req.action == TSDB_ALTER_TABLE_UPDATE_CHILD_TABLE_TAG_VAL) {
5,584✔
249
    ETableType tbType = 0;
820✔
250
    uint64_t suid = 0;
820✔
251
    STREAM_CHECK_RET_GOTO(metaGetTableTypeSuidByName(pTq->pVnode, req.tbName, &tbType, &suid));
820✔
252
    if (tbType != TSDB_SUPER_TABLE) {
820✔
253
      tqError("%s failed at line:%d since table %s is not super table, code:%d", __func__, lino, req.tbName, code);
×
254
      goto end;
×
255
    }
256
    SNode* pTagCond = getTagCondNodeForStableTmq(pHandle->execHandle.execTb.node);
820✔
257
    if (pTagCond != NULL) {
820✔
258
      uidList = taosArrayInit(8, sizeof(uint64_t));
×
259
      STREAM_CHECK_NULL_GOTO(uidList, terrno);
×
260
      STREAM_CHECK_RET_GOTO(vnodeGetCtbIdList(pTq->pVnode, suid, uidList));
×
261
      tqAlterTagForStbSub(pTq->pVnode, uidList, req.pMultiTag, NULL, pHandle, pHead->version);
×
262
    }
263
    *realTbSuid = suid;
820✔
264
  } else {
265
    int64_t uid = 0;
4,764✔
266
    int64_t suid = 0;
4,764✔
267
    code = tqGetUidSuid(pTq->pVnode->pMeta, req.tbName, &uid, &suid);
4,764✔
268
    TSDB_CHECK_CODE(code, lino, end);
4,764✔
269

270
    STqExecHandle* pExec = &pHandle->execHandle;
4,764✔
271
    STqReader* pReader = pExec->pTqReader;
4,764✔
272
    if (taosHashGet(pReader->tbIdHash, &uid, sizeof(int64_t)) != NULL) {
4,764✔
273
      *realTbSuid = suid;
1,588✔
274
    }
275
  }
276

277
end:
11,631✔
278
  if (code != 0) {
11,631✔
279
    tqError("%s failed at line:%d, code:%s, table:%s", __func__, lino, tstrerror(code), req.tbName);
×
280
  }
281
  destroyAlterTbReq(&req);
11,631✔
282
  taosArrayDestroy(uidList);
11,631✔
283
} 
11,631✔
284

285
static void tqProcessDropTbMsg(SDecoder* dcoder, SWalCont* pHead, STqHandle* pHandle, int64_t* realTbSuid, int64_t tbSuid) {
810✔
286
  SVDropTbBatchReq req = {0};
810✔
287
  SVDropTbBatchReq reqNew = {0};
810✔
288
  void* buf = NULL;
810✔
289
  int32_t lino = 0;
810✔
290
  int32_t code = tDecodeSVDropTbBatchReq(dcoder, &req);
810✔
291
  TSDB_CHECK_CODE(code, lino, end);
810✔
292

293
  STqExecHandle* pExec = &pHandle->execHandle;
810✔
294
  STqReader* pReader = pExec->pTqReader;
810✔
295

296
  reqNew.pArray = taosArrayInit(req.nReqs, sizeof(SVDropTbReq));
810✔
297
  TSDB_CHECK_NULL(reqNew.pArray, code, lino, end, terrno);
810✔
298
  SVDropTbReq* pDropReq = NULL;
810✔
299
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
1,620✔
300
    pDropReq = req.pReqs + iReq;
810✔
301
    if (pDropReq->suid == tbSuid && taosHashGet(pReader->tbIdHash, &pDropReq->uid, sizeof(int64_t)) != NULL) {
810✔
302
      reqNew.nReqs++;
405✔
303
      TSDB_CHECK_NULL(taosArrayPush(reqNew.pArray, pDropReq), code, lino, end, terrno);
810✔
304
    }
305
  }
306

307
  TSDB_CHECK_CONDITION(taosArrayGetSize(reqNew.pArray) != 0, code, lino, end, TSDB_CODE_SUCCESS);
810✔
308

309
  *realTbSuid = tbSuid;
405✔
310
  int     tlen = 0;
405✔
311
  tEncodeSize(tEncodeSVDropTbBatchReq, &reqNew, tlen, code);
405✔
312
  TSDB_CHECK_CODE(code, lino, end);
405✔
313

314
  buf = taosMemoryMalloc(tlen);
405✔
315
  TSDB_CHECK_NULL(buf, code, lino, end, terrno);
405✔
316

317
  SEncoder coderNew = {0};
405✔
318
  tEncoderInit(&coderNew, buf, tlen);
405✔
319
  code = tEncodeSVDropTbBatchReq(&coderNew, &reqNew);
405✔
320
  tEncoderClear(&coderNew);
405✔
321
  TSDB_CHECK_CODE(code, lino, end);
405✔
322

323
  if (tlen + sizeof(SMsgHead) > pHead->bodyLen) {
405✔
324
    tqError("%s failed at %s:%d since buffer overflow", __func__, __FILE__, __LINE__);
×
325
  } else {
326
    (void)memcpy(pHead->body + sizeof(SMsgHead), buf, tlen);
405✔
327
    pHead->bodyLen = tlen + sizeof(SMsgHead);
405✔
328
  }
329

330
end:
810✔
331
  taosMemoryFree(buf);
810✔
332
  taosArrayDestroy(reqNew.pArray);
810✔
333
  if (code != 0) {
810✔
334
    tqError("%s failed, code:%d, line:%d", __func__, code, lino);
×
335
  }
336
}
810✔
337

338
static bool tqProcessMetaForStbSub(STQ* pTq, STqHandle* pHandle, SWalCont* pHead) {
117,935✔
339
  int32_t code = 0;
117,935✔
340
  int32_t lino = 0;
117,935✔
341
  if (pHandle == NULL || pHead == NULL) {
117,935✔
342
    return false;
×
343
  }
344
  if (pHandle->execHandle.subType != TOPIC_SUB_TYPE__TABLE) {
117,935✔
345
    return true;
16,456✔
346
  }
347

348
  int16_t msgType = pHead->msgType;
101,479✔
349
  char*   body = pHead->body;
101,479✔
350
  int32_t bodyLen = pHead->bodyLen;
101,479✔
351

352
  int64_t  tbSuid = pHandle->execHandle.execTb.suid;
101,479✔
353
  int64_t  realTbSuid = 0;
101,479✔
354
  SDecoder dcoder = {0};
101,479✔
355
  void*    data = POINTER_SHIFT(body, sizeof(SMsgHead));
101,479✔
356
  int32_t  len = bodyLen - sizeof(SMsgHead);
101,479✔
357
  tDecoderInit(&dcoder, data, len);
101,479✔
358

359
  if (msgType == TDMT_VND_CREATE_STB || msgType == TDMT_VND_ALTER_STB) {
123,112✔
360
    SVCreateStbReq req = {0};
21,633✔
361
    if (tDecodeSVCreateStbReq(&dcoder, &req) < 0) {
21,633✔
362
      goto end;
×
363
    }
364
    realTbSuid = req.suid;
21,633✔
365
  } else if (msgType == TDMT_VND_DROP_STB) {
79,846✔
366
    SVDropStbReq req = {0};
410✔
367
    if (tDecodeSVDropStbReq(&dcoder, &req) < 0) {
410✔
368
      goto end;
×
369
    }
370
    realTbSuid = req.suid;
410✔
371
  } else if (msgType == TDMT_VND_CREATE_TABLE) {
79,436✔
372
    tqProcessCreateTbMsg(&dcoder, pHead, pTq, pHandle, &realTbSuid, tbSuid);
66,995✔
373
  } else if (msgType == TDMT_VND_ALTER_TABLE) {
12,441✔
374
    tqProcessAlterTbMsg(&dcoder, pHead, pTq, pHandle, &realTbSuid, tbSuid);
11,631✔
375
  } else if (msgType == TDMT_VND_DROP_TABLE) {
810✔
376
    tqProcessDropTbMsg(&dcoder, pHead, pHandle, &realTbSuid, tbSuid);
810✔
377
  } else if (msgType == TDMT_VND_DELETE) {
×
378
    SDeleteRes req = {0};
×
379
    if (tDecodeDeleteRes(&dcoder, &req) < 0) {
×
380
      goto end;
×
381
    }
382
    realTbSuid = req.suid;
×
383
  }
384

385
end:
101,479✔
386
  tDecoderClear(&dcoder);
101,479✔
387
  bool tmp = tbSuid == realTbSuid;
101,479✔
388
  if (pHandle->fetchMeta == ONLY_DATA){
101,479✔
389
    tmp = false;
76,950✔
390
  }
391
  tqDebug("%s suid:%" PRId64 " realSuid:%" PRId64 " return:%d", __FUNCTION__, tbSuid, realTbSuid, tmp);
101,479✔
392
  return tmp;
101,479✔
393
}
394

395
static int32_t tqFetchLog(STQ* pTq, STqHandle* pHandle, int64_t* fetchOffset, uint64_t reqId) {
152,953,454✔
396
  if (pTq == NULL || pHandle == NULL || fetchOffset == NULL) {
152,953,454✔
397
    return -1;
×
398
  }
399
  int32_t code = -1;
152,982,826✔
400
  int32_t vgId = TD_VID(pTq->pVnode);
152,982,826✔
401
  int64_t id = pHandle->pWalReader->readerId;
152,979,471✔
402

403
  int64_t offset = *fetchOffset;
152,976,387✔
404
  int64_t lastVer = walGetLastVer(pHandle->pWalReader->pWal);
152,970,200✔
405
  int64_t committedVer = walGetCommittedVer(pHandle->pWalReader->pWal);
152,957,760✔
406
  int64_t appliedVer = walGetAppliedVer(pHandle->pWalReader->pWal);
152,955,152✔
407

408
  tqDebug("vgId:%d, start to fetch wal, index:%" PRId64 ", last:%" PRId64 " commit:%" PRId64 ", applied:%" PRId64
152,952,103✔
409
          ", 0x%" PRIx64,
410
          vgId, offset, lastVer, committedVer, appliedVer, id);
411

412
  while (offset <= appliedVer) {
153,934,400✔
413
    if (walFetchHead(pHandle->pWalReader, offset) < 0) {
34,864,275✔
414
      tqDebug("tmq poll: consumer:0x%" PRIx64 ", (epoch %d) vgId:%d offset %" PRId64
×
415
              ", no more log to return, QID:0x%" PRIx64 " 0x%" PRIx64,
416
              pHandle->consumerId, pHandle->epoch, vgId, offset, reqId, id);
417
      goto END;
×
418
    }
419

420
    tqDebug("vgId:%d, consumer:0x%" PRIx64 " taosx get msg ver %" PRId64 ", type:%s, QID:0x%" PRIx64 " 0x%" PRIx64,
34,863,078✔
421
            vgId, pHandle->consumerId, offset, TMSG_INFO(pHandle->pWalReader->pHead->head.msgType), reqId, id);
422

423
    if (pHandle->pWalReader->pHead->head.msgType == TDMT_VND_SUBMIT) {
34,863,074✔
424
      code = walFetchBody(pHandle->pWalReader);
33,895,906✔
425
      goto END;
33,895,082✔
426
    } else {
427
      if (pHandle->fetchMeta != ONLY_DATA || pHandle->execHandle.subType == TOPIC_SUB_TYPE__TABLE) {
968,369✔
428
        SWalCont* pHead = &(pHandle->pWalReader->pHead->head);
333,136✔
429
        if (IS_META_MSG(pHead->msgType) && !(pHead->msgType == TDMT_VND_DELETE && pHandle->fetchMeta == ONLY_META)) {
333,136✔
430
          code = walFetchBody(pHandle->pWalReader);
117,935✔
431
          if (code < 0) {
117,935✔
432
            goto END;
×
433
          }
434

435
          pHead = &(pHandle->pWalReader->pHead->head);
117,935✔
436
          if (tqProcessMetaForStbSub(pTq, pHandle, pHead)) {
117,935✔
437
            code = 0;
28,919✔
438
            goto END;
28,919✔
439
          } else {
440
            offset++;
88,604✔
441
            code = -1;
88,604✔
442
            continue;
88,604✔
443
          }
444
        }
445
      }
446
      code = walSkipFetchBody(pHandle->pWalReader);
850,434✔
447
      if (code < 0) {
850,434✔
448
        goto END;
×
449
      }
450
      offset++;
850,434✔
451
    }
452
    code = -1;
850,434✔
453
  }
454

455
END:
119,070,125✔
456
  *fetchOffset = offset;
152,994,126✔
457
  tqDebug("vgId:%d, end to fetch wal, code:%d , index:%" PRId64 ", last:%" PRId64 " commit:%" PRId64
152,992,890✔
458
          ", applied:%" PRId64 ", 0x%" PRIx64,
459
          vgId, code, offset, lastVer, committedVer, appliedVer, id);
460
  return code;
152,995,754✔
461
}
462

463
bool tqGetTablePrimaryKey(STqReader* pReader) {
188,400✔
464
  if (pReader == NULL) {
188,400✔
465
    return false;
×
466
  }
467
  return pReader->hasPrimaryKey;
188,400✔
468
}
469

470
void tqSetTablePrimaryKey(STqReader* pReader, int64_t uid) {
10,158✔
471
  tqDebug("%s:%p uid:%" PRId64, __FUNCTION__, pReader, uid);
10,158✔
472

473
  if (pReader == NULL) {
10,158✔
474
    return;
×
475
  }
476
  bool            ret = false;
10,158✔
477
  SSchemaWrapper* schema = metaGetTableSchema(pReader->pVnode->pMeta, uid, -1, 1, NULL, 0, false);
10,158✔
478
  if (schema && schema->nCols >= 2 && schema->pSchema[1].flags & COL_IS_KEY) {
10,158✔
479
    ret = true;
768✔
480
  }
481
  tDeleteSchemaWrapper(schema);
482
  pReader->hasPrimaryKey = ret;
10,158✔
483
}
484

485
static void tqFreeTagCache(void* pData){
1,276,756✔
486
  if (pData == NULL) return;
1,276,756✔
487
  SArray* tagCache = *(SArray**)pData;
1,276,756✔
488
  taosArrayDestroyP(tagCache, taosMemFree);
1,277,176✔
489
}
490

491
STqReader* tqReaderOpen(SVnode* pVnode) {
267,865✔
492
  tqDebug("%s:%p", __FUNCTION__, pVnode);
267,865✔
493
  if (pVnode == NULL) {
269,841✔
494
    return NULL;
×
495
  }
496
  STqReader* pReader = taosMemoryCalloc(1, sizeof(STqReader));
269,841✔
497
  if (pReader == NULL) {
269,841✔
498
    return NULL;
×
499
  }
500

501
  pReader->pWalReader = walOpenReader(pVnode->pWal, 0);
269,841✔
502
  if (pReader->pWalReader == NULL) {
269,841✔
503
    taosMemoryFree(pReader);
×
504
    return NULL;
×
505
  }
506

507
  pReader->pVnode = pVnode;
269,841✔
508
  pReader->pSchemaWrapper = NULL;
269,841✔
509
  pReader->tbIdHash = NULL;
269,841✔
510
  pReader->pTableTagCacheForTmq = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_ENTRY_LOCK);
269,841✔
511
  if (pReader->pTableTagCacheForTmq == NULL) {
269,841✔
512
    walCloseReader(pReader->pWalReader);
×
513
    taosMemoryFree(pReader);
×
514
    return NULL;
×
515
  }
516
  taosHashSetFreeFp(pReader->pTableTagCacheForTmq, tqFreeTagCache);
269,841✔
517
  taosInitRWLatch(&pReader->tagCachelock);
269,841✔
518

519
  return pReader;
269,841✔
520
}
521

522
void tqReaderClose(STqReader* pReader) {
269,434✔
523
  tqDebug("%s:%p", __FUNCTION__, pReader);
269,434✔
524
  if (pReader == NULL) return;
269,841✔
525

526
  // close wal reader
527
  walCloseReader(pReader->pWalReader);
269,841✔
528
  taosHashCleanup(pReader->pTableTagCacheForTmq);
269,841✔
529
  tDeleteSchemaWrapper(pReader->pSchemaWrapper);
269,841✔
530
  taosMemoryFree(pReader->pTSchema);
269,841✔
531
  taosMemoryFree(pReader->extSchema);
269,841✔
532

533
  // free hash
534
  taosHashCleanup(pReader->tbIdHash);
269,841✔
535
  tDestroySubmitReq(&pReader->submit, TSDB_MSG_FLG_DECODE);
269,841✔
536

537
  taosMemoryFree(pReader);
269,841✔
538
}
539

540
int32_t tqReaderSeek(STqReader* pReader, int64_t ver, const char* id) {
7,551,281✔
541
  if (pReader == NULL) {
7,551,281✔
542
    return TSDB_CODE_INVALID_PARA;
×
543
  }
544
  if (walReaderSeekVer(pReader->pWalReader, ver) < 0) {
7,551,281✔
545
    return terrno;
7,386,068✔
546
  }
547
  tqDebug("wal reader seek to ver:%" PRId64 " %s", ver, id);
158,473✔
548
  return 0;
158,473✔
549
}
550

551
static int32_t tqGetTableTagCache(STqReader* pReader, SExprInfo* pExprInfo, int32_t numOfExpr, int64_t uid) {
111,361,227✔
552
  int32_t code = 0;
111,361,227✔
553
  int32_t lino = 0;
111,361,227✔
554

555
  void* data = taosHashGet(pReader->pTableTagCacheForTmq, &uid, LONG_BYTES);
111,361,227✔
556
  if (data == NULL) {
111,450,131✔
557
    SStorageAPI api = {0}; 
39,896,767✔
558
    initStorageAPI(&api);
39,896,013✔
559
    code = cacheTag(pReader->pVnode, pReader->pTableTagCacheForTmq, pExprInfo, numOfExpr, &api, uid, 0, &pReader->tagCachelock);
39,881,976✔
560
    TSDB_CHECK_CODE(code, lino, END);
39,896,432✔
561
  }
562

563
  END:
71,553,364✔
564
  if (code != TSDB_CODE_SUCCESS) {
111,404,444✔
565
    tqError("%s failed at %d, failed to add tbName to response:%s, uid:%"PRId64, __FUNCTION__, lino, tstrerror(code), uid);
×
566
  }
567
  
568
  return code;
111,344,879✔
569
}
570

571
void tqUpdateTableTagCache(STqReader* pReader, SExprInfo* pExprInfo, int32_t numOfExpr, int64_t uid, col_id_t colId) {
48,451✔
572
  int32_t code = 0;
48,451✔
573
  int32_t lino = 0;
48,451✔
574

575
  void* data = taosHashGet(pReader->pTableTagCacheForTmq, &uid, LONG_BYTES);
48,451✔
576
  if (data == NULL) {
48,451✔
577
    return;
47,675✔
578
  }
579

580
  SStorageAPI api = {0}; 
776✔
581
  initStorageAPI(&api);
776✔
582
  code = cacheTag(pReader->pVnode, pReader->pTableTagCacheForTmq, pExprInfo, numOfExpr, &api, uid, colId, &pReader->tagCachelock);
776✔
583
  TSDB_CHECK_CODE(code, lino, END);
776✔
584

585
  END:
776✔
586
  if (code != TSDB_CODE_SUCCESS) {
776✔
587
    tqError("%s failed at %d, failed to update tag cache code:%s, uid:%"PRId64, __FUNCTION__, lino, tstrerror(code), uid);
×
588
  }
589
}
590

591
static int32_t tqRetrievePseudoCols(STqReader* pReader, SSDataBlock* pBlock, int32_t numOfRows, int64_t uid, SExprInfo* pPseudoExpr, int32_t numOfPseudoExpr) {
111,362,802✔
592
  if (pReader == NULL || pBlock == NULL) {
111,362,802✔
593
    return TSDB_CODE_INVALID_PARA;
×
594
  }
595
  int32_t code = TSDB_CODE_SUCCESS;
111,395,649✔
596
  int32_t lino = 0;
111,395,649✔
597
  
598
  code = tqGetTableTagCache(pReader, pPseudoExpr, numOfPseudoExpr, uid);
111,395,649✔
599
  TSDB_CHECK_CODE(code, lino, END);
111,399,633✔
600

601
  code = fillTag(pReader->pTableTagCacheForTmq, pPseudoExpr, numOfPseudoExpr, uid, pBlock, numOfRows, pBlock->info.rows - numOfRows, 1, &pReader->tagCachelock);
111,399,633✔
602
  TSDB_CHECK_CODE(code, lino, END);
111,400,635✔
603

604
END:
111,400,635✔
605
  if (code != 0) {
111,400,635✔
606
    tqError("tqRetrievePseudoCols failed, line:%d, msg:%s", lino, tstrerror(code));
×
607
  }
608
  return code;
111,372,314✔
609
}
610

611
int32_t tqNextBlockInWal(STqReader* pReader, SSDataBlock* pRes, SHashObj* pCol2SlotId, SExprInfo* pPseudoExpr, int32_t numOfPseudoExpr,
59,929,442✔
612
                         int sourceExcluded, int32_t minPollRows, int64_t timeout, int8_t enableReplay) {
613
  int32_t code = 0;
59,929,442✔
614
  if (pReader == NULL) {
59,929,442✔
615
    return TSDB_CODE_INVALID_PARA;
×
616
  }
617
  SWalReader* pWalReader = pReader->pWalReader;
59,929,442✔
618

619
  int64_t st = taosGetTimestampMs();
59,927,805✔
620
  while (1) {
113,269,220✔
621
    code = walNextValidMsg(pWalReader, false);
173,197,025✔
622
    if (code != 0) {
173,187,471✔
623
      break;
59,266,731✔
624
    }
625

626
    void*   pBody = POINTER_SHIFT(pWalReader->pHead->head.body, sizeof(SSubmitReq2Msg));
113,920,740✔
627
    int32_t bodyLen = pWalReader->pHead->head.bodyLen - sizeof(SSubmitReq2Msg);
114,008,289✔
628
    int64_t ver = pWalReader->pHead->head.version;
114,026,839✔
629
    SDecoder decoder = {0};
114,004,653✔
630
    code = tqReaderSetSubmitMsg(pReader, pBody, bodyLen, ver, NULL, &decoder);
114,003,391✔
631
    tDecoderClear(&decoder);
113,932,339✔
632
    if (code != 0) {
114,027,362✔
633
      return code;
×
634
    }
635
    pReader->nextBlk = 0;
114,027,362✔
636

637
    int32_t numOfBlocks = taosArrayGetSize(pReader->submit.aSubmitTbData);
114,042,120✔
638
    while (pReader->nextBlk < numOfBlocks) {
227,959,871✔
639
      tqDebug("tq reader next data block %d/%d, len:%d %" PRId64, pReader->nextBlk, numOfBlocks, pReader->msg.msgLen,
113,939,242✔
640
              pReader->msg.ver);
641

642
      SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
114,070,303✔
643
      if (pSubmitTbData == NULL) {
114,068,597✔
644
        tqError("tq reader next data block %d/%d, len:%d %" PRId64, pReader->nextBlk, numOfBlocks, pReader->msg.msgLen,
×
645
                pReader->msg.ver);
646
        return terrno;
×
647
      }
648
      if ((pSubmitTbData->flags & sourceExcluded) != 0) {
114,068,597✔
649
        pReader->nextBlk += 1;
×
650
        continue;
×
651
      }
652
      if (pReader->tbIdHash == NULL || taosHashGet(pReader->tbIdHash, &pSubmitTbData->uid, sizeof(int64_t)) != NULL) {
114,072,997✔
653
        tqDebug("tq reader return submit block, uid:%" PRId64, pSubmitTbData->uid);
111,477,365✔
654
        int32_t numOfRows = pRes->info.rows;
111,470,598✔
655
        code = tqRetrieveCols(pReader, pRes, pCol2SlotId);
111,473,375✔
656
        if (code != TSDB_CODE_SUCCESS) {
111,357,529✔
657
          return code;
×
658
        }
659
        code = tqRetrievePseudoCols(pReader, pRes, numOfRows, pSubmitTbData->uid, pPseudoExpr, numOfPseudoExpr);
111,357,529✔
660
        if (code != TSDB_CODE_SUCCESS) {
111,393,083✔
661
          return code;
×
662
        }
663

664
      }
665
      pReader->nextBlk += 1;
113,996,315✔
666
    }
667

668
    tDestroySubmitReq(&pReader->submit, TSDB_MSG_FLG_DECODE);
114,024,245✔
669
    pReader->msg.msgStr = NULL;
113,986,431✔
670

671
    if (pRes->info.rows >= minPollRows || (enableReplay && pRes->info.rows > 0)){
113,959,813✔
672
      break;
673
    }
674
    int64_t elapsed = taosGetTimestampMs() - st;
113,443,649✔
675
    if (elapsed > timeout || elapsed < 0) {
113,443,649✔
676
      code = TSDB_CODE_TMQ_FETCH_TIMEOUT;
121,178✔
677
      terrno = code;
121,178✔
678
      break;
136,800✔
679
    }
680
  }
681
  return code;
59,925,855✔
682
}
683

684
int32_t tqReaderSetSubmitMsg(STqReader* pReader, void* msgStr, int32_t msgLen, int64_t ver, SArray* rawList, SDecoder* decoder) {
147,826,830✔
685
  if (pReader == NULL) {
147,826,830✔
686
    return TSDB_CODE_INVALID_PARA;
×
687
  }
688
  pReader->msg.msgStr = msgStr;
147,826,830✔
689
  pReader->msg.msgLen = msgLen;
147,892,673✔
690
  pReader->msg.ver = ver;
147,888,144✔
691

692
  tqTrace("tq reader set msg pointer:%p, msg len:%d", msgStr, msgLen);
147,924,037✔
693

694
  tDecoderInit(decoder, pReader->msg.msgStr, pReader->msg.msgLen);
147,924,037✔
695
  int32_t code = tDecodeSubmitReq(decoder, &pReader->submit, rawList);
147,893,710✔
696

697
  if (code != 0) {
147,876,571✔
698
    tqError("DecodeSSubmitReq2 error, msgLen:%d, ver:%" PRId64, msgLen, ver);
×
699
  }
700

701
  return code;
147,880,134✔
702
}
703

704
void tqReaderClearSubmitMsg(STqReader* pReader) {
33,885,626✔
705
  tDestroySubmitReq(&pReader->submit, TSDB_MSG_FLG_DECODE);
33,885,626✔
706
  pReader->nextBlk = 0;
33,886,092✔
707
  pReader->msg.msgStr = NULL;
33,889,360✔
708
}
33,887,706✔
709

710
SWalReader* tqGetWalReader(STqReader* pReader) {
134,622,000✔
711
  if (pReader == NULL) {
134,622,000✔
712
    return NULL;
×
713
  }
714
  return pReader->pWalReader;
134,622,000✔
715
}
716

717
int64_t tqGetResultBlockTime(STqReader* pReader) {
59,922,215✔
718
  if (pReader == NULL) {
59,922,215✔
719
    return 0;
×
720
  }
721
  return pReader->lastTs;
59,922,215✔
722
}
723

724
int32_t tqMaskBlock(SSchemaWrapper* pDst, SSDataBlock* pBlock, const SSchemaWrapper* pSrc, char* mask,
32,512,633✔
725
                    SExtSchema* extSrc) {
726
  if (pDst == NULL || pBlock == NULL || pSrc == NULL || mask == NULL) {
32,512,633✔
727
    return TSDB_CODE_INVALID_PARA;
×
728
  }
729
  int32_t code = 0;
32,520,852✔
730

731
  int32_t cnt = 0;
32,520,852✔
732
  for (int32_t i = 0; i < pSrc->nCols; i++) {
166,977,884✔
733
    cnt += mask[i];
134,455,732✔
734
  }
735

736
  pDst->nCols = cnt;
32,512,935✔
737
  pDst->pSchema = taosMemoryCalloc(cnt, sizeof(SSchema));
32,522,888✔
738
  if (pDst->pSchema == NULL) {
32,511,806✔
739
    return TAOS_GET_TERRNO(terrno);
×
740
  }
741

742
  int32_t j = 0;
32,510,612✔
743
  for (int32_t i = 0; i < pSrc->nCols; i++) {
166,984,620✔
744
    if (mask[i]) {
134,459,553✔
745
      pDst->pSchema[j++] = pSrc->pSchema[i];
134,478,147✔
746
      SColumnInfoData colInfo =
134,474,068✔
747
          createColumnInfoData(pSrc->pSchema[i].type, pSrc->pSchema[i].bytes, pSrc->pSchema[i].colId);
134,476,513✔
748
      if (extSrc != NULL) {
134,480,621✔
749
        decimalFromTypeMod(extSrc[i].typeMod, &colInfo.info.precision, &colInfo.info.scale);
×
750
      }
751
      code = blockDataAppendColInfo(pBlock, &colInfo);
134,480,621✔
752
      if (code != 0) {
134,474,420✔
753
        return code;
×
754
      }
755
    }
756
  }
757
  return 0;
32,525,802✔
758
}
759

760
static int32_t tqDoSetBlobVal(SColumnInfoData* pColumnInfoData, int32_t idx, SColVal* pColVal, SBlobSet* pBlobRow2) {
×
761
  int32_t code = 0;
×
762
  if (pColumnInfoData == NULL || pColVal == NULL || pBlobRow2 == NULL) {
×
763
    return TSDB_CODE_INVALID_PARA;
×
764
  }
765
  // TODO(yhDeng)
766
  if (COL_VAL_IS_VALUE(pColVal)) {
×
767
    char* val = taosMemCalloc(1, pColVal->value.nData + sizeof(BlobDataLenT));
×
768
    if (val == NULL) {
×
769
      return terrno;
×
770
    }
771

772
    uint64_t seq = 0;
×
773
    int32_t  len = 0;
×
774
    if (pColVal->value.pData != NULL) {
×
775
      if (tGetU64(pColVal->value.pData, &seq) < 0){
×
776
        TAOS_CHECK_RETURN(TSDB_CODE_INVALID_PARA);
×
777
      }
778
      SBlobItem item = {0};
×
779
      code = tBlobSetGet(pBlobRow2, seq, &item);
×
780
      if (code != 0) {
×
781
        taosMemoryFree(val);
×
782
        terrno = code;
×
783
        uError("tq set blob val, idx:%d, get blob item failed, seq:%" PRIu64 ", code:%d", idx, seq, code);
×
784
        return code;
×
785
      }
786

787
      val = taosMemRealloc(val, item.len + sizeof(BlobDataLenT));
×
788
      (void)memcpy(blobDataVal(val), item.data, item.len);
×
789
      len = item.len;
×
790
    }
791

792
    blobDataSetLen(val, len);
×
793
    code = colDataSetVal(pColumnInfoData, idx, val, false);
×
794

795
    taosMemoryFree(val);
×
796
  } else {
797
    colDataSetNULL(pColumnInfoData, idx);
×
798
  }
799
  return code;
×
800
}
801
static int32_t tqDoSetVal(SColumnInfoData* pColumnInfoData, int32_t rowIndex, SColVal* pColVal) {
2,147,483,647✔
802
  int32_t code = TSDB_CODE_SUCCESS;
2,147,483,647✔
803

804
  if (IS_VAR_DATA_TYPE(pColVal->value.type)) {
2,147,483,647✔
805
    if (COL_VAL_IS_VALUE(pColVal)) {
2,147,483,647✔
806
      char val[65535 + 2] = {0};
2,147,483,647✔
807
      if (pColVal->value.pData != NULL) {
2,147,483,647✔
808
        (void)memcpy(varDataVal(val), pColVal->value.pData, pColVal->value.nData);
2,147,483,647✔
809
      }
810
      varDataSetLen(val, pColVal->value.nData);
2,147,483,647✔
811
      code = colDataSetVal(pColumnInfoData, rowIndex, val, false);
2,147,483,647✔
812
    } else {
813
      colDataSetNULL(pColumnInfoData, rowIndex);
×
814
    }
815
  } else {
816
    code = colDataSetVal(pColumnInfoData, rowIndex, VALUE_GET_DATUM(&pColVal->value, pColVal->value.type),
2,147,483,647✔
817
                         !COL_VAL_IS_VALUE(pColVal));
2,147,483,647✔
818
  }
819

820
  return code;
2,147,483,647✔
821
}
822

823
static int32_t tqSetBlockData(SSDataBlock* pBlock, int32_t slotId, int32_t rowIndex, SColVal* colVal, SBlobSet* pBlobSet) {
2,147,483,647✔
824
  int32_t        code = 0;
2,147,483,647✔
825
  SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, slotId);
2,147,483,647✔
826
  if (pColData == NULL) {
2,147,483,647✔
827
    return terrno;
×
828
  }
829

830
  uint8_t isBlob = IS_STR_DATA_BLOB(pColData->info.type) ? 1 : 0;
2,147,483,647✔
831
  if (isBlob == 0) {
2,147,483,647✔
832
    code = tqDoSetVal(pColData, rowIndex, colVal);
2,147,483,647✔
833
  } else {
834
    code = tqDoSetBlobVal(pColData, rowIndex, colVal, pBlobSet);
390✔
835
  }
836
  return code;
2,147,483,647✔
837
}
838

839
static int32_t tqProcessSubmitRow(SArray*         pRows,
111,443,482✔
840
                                SSDataBlock*    pBlock,
841
                                SHashObj*       pCol2SlotId,
842
                                STqReader*      pReader,
843
                                SBlobSet*       pBlobSet) {
844
  int32_t        code = 0;
111,443,482✔
845
  int32_t        line = 0;
111,443,482✔
846

847
  SArray* pColArray = taosArrayInit(4, INT_BYTES * 2);
111,443,482✔
848
  TSDB_CHECK_NULL(pColArray, code, line, END, terrno);
111,448,212✔
849

850
  int32_t sourceIdx = -1;
111,448,212✔
851
  int32_t rowIndex = 0;
111,448,212✔
852
  SRow* pRow = taosArrayGetP(pRows, rowIndex);
111,448,212✔
853
  TSDB_CHECK_NULL(pRow, code, line, END, terrno);
111,419,049✔
854
  while (++sourceIdx < pReader->pTSchema->numOfCols) {
986,921,163✔
855
    SColVal colVal = {0};
875,954,908✔
856
    code = tRowGet(pRow, pReader->pTSchema, sourceIdx, &colVal);
875,814,743✔
857
    TSDB_CHECK_CODE(code, line, END);
877,400,630✔
858
    void* pSlotId = taosHashGet(pCol2SlotId, &colVal.cid, sizeof(colVal.cid));
877,400,630✔
859
    if (pSlotId == NULL) {
879,334,512✔
860
      continue;
406,512,981✔
861
    }
862
    int32_t pData[2] = {sourceIdx, *(int16_t*)pSlotId};
472,821,531✔
863
    TSDB_CHECK_NULL(taosArrayPush(pColArray, pData), code, line, END, terrno);
472,093,883✔
864
    code = tqSetBlockData(pBlock, pData[1], pBlock->info.rows + rowIndex, &colVal, pBlobSet);
472,093,883✔
865
    TSDB_CHECK_CODE(code, line, END);
469,227,945✔
866
  }
867
  
868
  for (rowIndex = 1; rowIndex < taosArrayGetSize(pRows); rowIndex++) {
2,147,483,647✔
869
    SRow* pRow = taosArrayGetP(pRows, rowIndex);
2,147,483,647✔
870
    TSDB_CHECK_NULL(pRow, code, line, END, terrno);
2,147,483,647✔
871
    for (int32_t j = 0; j < taosArrayGetSize(pColArray); j++) {
2,147,483,647✔
872
      int32_t* pData = taosArrayGet(pColArray, j);
2,147,483,647✔
873
      TSDB_CHECK_NULL(pData, code, line, END, terrno);
2,147,483,647✔
874

875
      SColVal colVal = {0};
2,147,483,647✔
876
      code = tRowGet(pRow, pReader->pTSchema, pData[0], &colVal);
2,147,483,647✔
877
      TSDB_CHECK_CODE(code, line, END);
2,147,483,647✔
878

879
      code = tqSetBlockData(pBlock, pData[1], pBlock->info.rows + rowIndex, &colVal, pBlobSet);
2,147,483,647✔
880
      TSDB_CHECK_CODE(code, line, END);
2,147,483,647✔
881
    }
882
  }
883

884
  END:
103,331,668✔
885
  taosArrayDestroy(pColArray);
75,082,214✔
886
  return code;
111,392,388✔
887
}
888

889
static int32_t tqProcessSubmitCol(SArray*         pCols,
1,576✔
890
                                SSDataBlock*    pBlock,
891
                                SHashObj*       pCol2SlotId,
892
                                SBlobSet*       pBlobSet) {
893
  int32_t        code = 0;
1,576✔
894
  int32_t        line = 0;
1,576✔
895

896
  for (int32_t i = 0; i < taosArrayGetSize(pCols); i++) {
4,728✔
897
    SColData* pCol = taosArrayGet(pCols, i);
3,152✔
898
    TSDB_CHECK_NULL(pCol, code, line, END, terrno);
3,152✔
899
    void* pSlotId = taosHashGet(pCol2SlotId, &pCol->cid, sizeof(pCol->cid));
3,152✔
900
    if (pSlotId == NULL) {
3,152✔
901
      continue;
1,576✔
902
    }
903
    SColVal colVal = {0};
1,576✔
904
    for (int32_t row = 0; row < pCol->nVal; row++) {
4,728✔
905
      code = tColDataGetValue(pCol, row, &colVal);
3,152✔
906
      TSDB_CHECK_CODE(code, line, END);
3,152✔
907

908
      code = tqSetBlockData(pBlock, *(int16_t*)pSlotId, pBlock->info.rows + row, &colVal, pBlobSet);
3,152✔
909
      TSDB_CHECK_CODE(code, line, END);
3,152✔
910
    }
911
  }
912
  
913
  END:
1,576✔
914
  return code;
1,576✔
915
}
916

917
static int32_t tqCheckSchema(STqReader* pReader, SSubmitTbData* pSubmitTbData) {
111,450,982✔
918
  int32_t vgId = pReader->pWalReader->pWal->cfg.vgId;
111,450,982✔
919
  int32_t sversion = pSubmitTbData->sver;
111,464,588✔
920
  int64_t suid = pSubmitTbData->suid;
111,471,800✔
921
  int64_t uid = pSubmitTbData->uid;
111,468,200✔
922
  if ((suid != 0 && pReader->cachedSchemaSuid != suid) || (suid == 0 && pReader->cachedSchemaUid != uid) ||
111,466,580✔
923
      (pReader->cachedSchemaVer != sversion)) {
111,356,191✔
924
    tDeleteSchemaWrapper(pReader->pSchemaWrapper);
120,015✔
925
    taosMemoryFreeClear(pReader->extSchema);
107,614✔
926
    taosMemoryFreeClear(pReader->pTSchema);
108,387✔
927
    pReader->pSchemaWrapper = metaGetTableSchema(pReader->pVnode->pMeta, uid, sversion, 1, &pReader->extSchema, 0, true);
108,387✔
928
    if (pReader->pSchemaWrapper == NULL) {
108,387✔
929
      tqWarn("vgId:%d, cannot found schema wrapper for table: suid:%" PRId64 ", uid:%" PRId64 ",version %d, possibly dropped table",
×
930
              vgId, suid, uid, pReader->cachedSchemaVer);
931
      return TSDB_CODE_TQ_TABLE_SCHEMA_NOT_FOUND;
×
932
    }
933
    pReader->pTSchema = tBuildTSchema(pReader->pSchemaWrapper->pSchema, pReader->pSchemaWrapper->nCols, pReader->pSchemaWrapper->version);
108,387✔
934
    if (pReader->pTSchema == NULL) {
108,387✔
935
      tqWarn("vgId:%d, cannot build schema for table: suid:%" PRId64 ", uid:%" PRId64 ",version %d",
×
936
              vgId, suid, uid, pReader->cachedSchemaVer);
937
      return terrno;
×
938
    }
939
    pReader->cachedSchemaUid = uid;
108,387✔
940
    pReader->cachedSchemaSuid = suid;
108,387✔
941
    pReader->cachedSchemaVer = sversion;
108,387✔
942
  }
943
  return TSDB_CODE_SUCCESS;
111,449,790✔
944
}
945

946
static int32_t tqRetrieveCols(STqReader* pReader, SSDataBlock* pBlock, SHashObj* pCol2SlotId) {
111,474,175✔
947
  if (pReader == NULL || pBlock == NULL) {
111,474,175✔
948
    return TSDB_CODE_INVALID_PARA;
×
949
  }
950
  tqDebug("tq reader retrieve data block %p, index:%d", pReader->msg.msgStr, pReader->nextBlk);
111,476,175✔
951
  int32_t        code = 0;
111,473,775✔
952
  int32_t        line = 0;
111,473,775✔
953
  SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
111,473,775✔
954
  TSDB_CHECK_NULL(pSubmitTbData, code, line, END, terrno);
111,456,575✔
955
  pReader->lastTs = pSubmitTbData->ctimeMs;
111,456,575✔
956

957
  int32_t numOfRows = 0;
111,474,965✔
958
  if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
111,474,965✔
959
    SColData* pCol = taosArrayGet(pSubmitTbData->aCol, 0);
1,576✔
960
    TSDB_CHECK_NULL(pCol, code, line, END, terrno);
1,576✔
961
    numOfRows = pCol->nVal;
1,576✔
962
  } else {
963
    numOfRows = taosArrayGetSize(pSubmitTbData->aRowP);
111,465,789✔
964
  }
965

966
  code = blockDataEnsureCapacity(pBlock, pBlock->info.rows + numOfRows);
111,464,575✔
967
  TSDB_CHECK_CODE(code, line, END);
111,450,998✔
968

969
  code = tqCheckSchema(pReader, pSubmitTbData);
111,450,998✔
970
  TSDB_CHECK_CODE(code, line, END);
111,455,423✔
971

972
  // convert and scan one block
973
  if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
111,455,423✔
974
    SArray* pCols = pSubmitTbData->aCol;
1,576✔
975
    code = tqProcessSubmitCol(pCols, pBlock, pCol2SlotId, pSubmitTbData->pBlobSet);
1,576✔
976
    TSDB_CHECK_CODE(code, line, END);
1,576✔
977
  } else {
978
    SArray*         pRows = pSubmitTbData->aRowP;
111,454,647✔
979
    code = tqProcessSubmitRow(pRows, pBlock, pCol2SlotId, pReader, pSubmitTbData->pBlobSet);
111,451,817✔
980
    TSDB_CHECK_CODE(code, line, END);
111,358,755✔
981
  }
982
  pBlock->info.rows += numOfRows;
111,360,331✔
983
END:
111,397,461✔
984
  if (code != 0) {
111,397,461✔
985
    tqError("tqRetrieveCols failed, line:%d, msg:%s", line, tstrerror(code));
×
986
  }
987
  return code;
111,382,539✔
988
}
989

990
#define PROCESS_VAL                                      \
991
  if (curRow == 0) {                                     \
992
    assigned[j] = !COL_VAL_IS_NONE(&colVal);             \
993
    buildNew = true;                                     \
994
  } else {                                               \
995
    bool currentRowAssigned = !COL_VAL_IS_NONE(&colVal); \
996
    if (currentRowAssigned != assigned[j]) {             \
997
      assigned[j] = currentRowAssigned;                  \
998
      buildNew = true;                                   \
999
    }                                                    \
1000
  }
1001

1002
#define SET_DATA                                                                                    \
1003
  if (colVal.cid < pColData->info.colId) {                                                          \
1004
    sourceIdx++;                                                                                    \
1005
  } else if (colVal.cid == pColData->info.colId) {                                                  \
1006
    if (IS_STR_DATA_BLOB(pColData->info.type)) {                                                    \
1007
      TQ_ERR_GO_TO_END(tqDoSetBlobVal(pColData, curRow - lastRow, &colVal, pSubmitTbData->pBlobSet)); \
1008
    } else {                                                                                        \
1009
      TQ_ERR_GO_TO_END(tqDoSetVal(pColData, curRow - lastRow, &colVal));                              \
1010
    }                                                                                               \
1011
    sourceIdx++;                                                                                    \
1012
    targetIdx++;                                                                                    \
1013
  } else {                                                                                          \
1014
    colDataSetNULL(pColData, curRow - lastRow);                                                     \
1015
    targetIdx++;                                                                                    \
1016
  }
1017

1018
static int32_t tqProcessBuildNew(STqReader* pReader, SSubmitTbData* pSubmitTbData, SArray* blocks, SArray* schemas,
32,509,710✔
1019
                               char* assigned, int32_t numOfRows, int32_t curRow, int32_t* lastRow) {
1020
  int32_t         code = 0;
32,509,710✔
1021
  SSchemaWrapper* pSW = NULL;
32,509,710✔
1022
  SSDataBlock*    block = NULL;
32,514,636✔
1023
  if (taosArrayGetSize(blocks) > 0) {
32,514,636✔
1024
    SSDataBlock* pLastBlock = taosArrayGetLast(blocks);
×
1025
    TQ_NULL_GO_TO_END(pLastBlock);
×
1026
    pLastBlock->info.rows = curRow - *lastRow;
×
1027
    *lastRow = curRow;
×
1028
  }
1029

1030
  block = taosMemoryCalloc(1, sizeof(SSDataBlock));
32,516,677✔
1031
  TQ_NULL_GO_TO_END(block);
32,508,851✔
1032

1033
  pSW = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
32,508,851✔
1034
  TQ_NULL_GO_TO_END(pSW);
32,511,344✔
1035

1036
  TQ_ERR_GO_TO_END(tqMaskBlock(pSW, block, pReader->pSchemaWrapper, assigned, pReader->extSchema));
32,511,344✔
1037
  tqTrace("vgId:%d, build new block, col %d", pReader->pWalReader->pWal->cfg.vgId,
32,525,802✔
1038
          (int32_t)taosArrayGetSize(block->pDataBlock));
1039

1040
  block->info.id.uid = pSubmitTbData->uid;
32,525,802✔
1041
  block->info.version = pReader->msg.ver;
32,523,335✔
1042
  TQ_ERR_GO_TO_END(blockDataEnsureCapacity(block, numOfRows - curRow));
32,518,808✔
1043
  TQ_NULL_GO_TO_END(taosArrayPush(blocks, block));
32,520,861✔
1044
  TQ_NULL_GO_TO_END(taosArrayPush(schemas, &pSW));
32,520,861✔
1045
  pSW = NULL;
32,520,861✔
1046

1047
  taosMemoryFreeClear(block);
32,520,861✔
1048

1049
END:
32,520,858✔
1050
  if (code != 0) {
32,519,210✔
1051
    tqError("tqProcessBuildNew failed, code:%d", code);
×
1052
  }
1053
  tDeleteSchemaWrapper(pSW);
32,519,210✔
1054
  blockDataFreeRes(block);
32,519,266✔
1055
  taosMemoryFree(block);
32,517,612✔
1056
  return code;
32,519,638✔
1057
}
1058
static int32_t tqProcessColData(STqReader* pReader, SSubmitTbData* pSubmitTbData, SArray* blocks, SArray* schemas) {
845,250✔
1059
  int32_t code = 0;
845,250✔
1060
  int32_t curRow = 0;
845,250✔
1061
  int32_t lastRow = 0;
845,250✔
1062

1063
  SSchemaWrapper* pSchemaWrapper = pReader->pSchemaWrapper;
845,250✔
1064
  char*           assigned = taosMemoryCalloc(1, pSchemaWrapper->nCols);
845,250✔
1065
  TQ_NULL_GO_TO_END(assigned);
845,250✔
1066

1067
  SArray*   pCols = pSubmitTbData->aCol;
845,250✔
1068
  SColData* pCol = taosArrayGet(pCols, 0);
845,250✔
1069
  TQ_NULL_GO_TO_END(pCol);
845,250✔
1070
  int32_t numOfRows = pCol->nVal;
845,250✔
1071
  int32_t numOfCols = taosArrayGetSize(pCols);
845,250✔
1072
  tqTrace("vgId:%d, tqProcessColData start, col num: %d, rows:%d", pReader->pWalReader->pWal->cfg.vgId, numOfCols,
845,250✔
1073
          numOfRows);
1074
  for (int32_t i = 0; i < numOfRows; i++) {
135,134,342✔
1075
    bool buildNew = false;
134,006,570✔
1076

1077
    for (int32_t j = 0; j < pSchemaWrapper->nCols; j++) {
525,964,708✔
1078
      int32_t k = 0;
387,974,956✔
1079
      for (; k < numOfCols; k++) {
773,302,922✔
1080
        pCol = taosArrayGet(pCols, k);
737,289,736✔
1081
        TQ_NULL_GO_TO_END(pCol);
737,366,952✔
1082
        if (pSchemaWrapper->pSchema[j].colId == pCol->cid) {
737,366,952✔
1083
          SColVal colVal = {0};
385,501,416✔
1084
          TQ_ERR_GO_TO_END(tColDataGetValue(pCol, i, &colVal));
388,073,720✔
1085
          PROCESS_VAL
393,285,056✔
1086
          tqTrace("assign[%d] = %d, nCols:%d", j, assigned[j], numOfCols);
393,853,876✔
1087
          break;
392,242,386✔
1088
        }
1089
      }
1090
      if (k >= numOfCols) {
391,958,138✔
1091
        // this column is not in the current row, so we set it to NULL
1092
        assigned[j] = 0;
×
1093
        buildNew = true;
×
1094
      }
1095
    }
1096

1097
    if (buildNew) {
124,707,454✔
1098
      TQ_ERR_GO_TO_END(tqProcessBuildNew(pReader, pSubmitTbData, blocks, schemas, assigned, numOfRows, curRow, &lastRow));
845,250✔
1099
    }
1100

1101
    SSDataBlock* pBlock = taosArrayGetLast(blocks);
124,706,686✔
1102
    TQ_NULL_GO_TO_END(pBlock);
134,155,476✔
1103

1104
    tqTrace("vgId:%d, taosx scan, block num: %d", pReader->pWalReader->pWal->cfg.vgId,
134,155,476✔
1105
            (int32_t)taosArrayGetSize(blocks));
1106

1107
    int32_t targetIdx = 0;
134,155,476✔
1108
    int32_t sourceIdx = 0;
134,155,476✔
1109
    int32_t colActual = blockDataGetNumOfCols(pBlock);
134,155,476✔
1110
    while (targetIdx < colActual && sourceIdx < numOfCols) {
524,438,894✔
1111
      pCol = taosArrayGet(pCols, sourceIdx);
390,149,802✔
1112
      TQ_NULL_GO_TO_END(pCol);
388,213,776✔
1113
      SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, targetIdx);
388,213,776✔
1114
      TQ_NULL_GO_TO_END(pColData);
383,507,464✔
1115
      SColVal colVal = {0};
383,507,464✔
1116
      TQ_ERR_GO_TO_END(tColDataGetValue(pCol, i, &colVal));
385,096,776✔
1117
      SET_DATA
391,548,364✔
1118
      tqTrace("targetIdx:%d sourceIdx:%d colActual:%d", targetIdx, sourceIdx, colActual);
397,925,466✔
1119
    }
1120

1121
    curRow++;
134,289,092✔
1122
  }
1123
  SSDataBlock* pLastBlock = taosArrayGetLast(blocks);
1,127,772✔
1124
  pLastBlock->info.rows = curRow - lastRow;
844,866✔
1125
  tqTrace("vgId:%d, tqProcessColData end, col num: %d, rows:%d, block num:%d", pReader->pWalReader->pWal->cfg.vgId,
844,482✔
1126
          numOfCols, numOfRows, (int)taosArrayGetSize(blocks));
1127
END:
43,162,962✔
1128
  if (code != TSDB_CODE_SUCCESS) {
844,482✔
1129
    tqError("vgId:%d, process col data failed, code:%d", pReader->pWalReader->pWal->cfg.vgId, code);
×
1130
  }
1131
  taosMemoryFree(assigned);
844,866✔
1132
  return code;
845,250✔
1133
}
1134

1135
int32_t tqProcessRowData(STqReader* pReader, SSubmitTbData* pSubmitTbData, SArray* blocks, SArray* schemas) {
31,669,871✔
1136
  int32_t   code = 0;
31,669,871✔
1137
  STSchema* pTSchema = NULL;
31,669,871✔
1138

1139
  SSchemaWrapper* pSchemaWrapper = pReader->pSchemaWrapper;
31,669,871✔
1140
  char*           assigned = taosMemoryCalloc(1, pSchemaWrapper->nCols);
31,674,778✔
1141
  TQ_NULL_GO_TO_END(assigned);
31,669,797✔
1142

1143
  int32_t curRow = 0;
31,669,797✔
1144
  int32_t lastRow = 0;
31,669,797✔
1145
  SArray* pRows = pSubmitTbData->aRowP;
31,668,977✔
1146
  int32_t numOfRows = taosArrayGetSize(pRows);
31,671,441✔
1147
  pTSchema = tBuildTSchema(pSchemaWrapper->pSchema, pSchemaWrapper->nCols, pSchemaWrapper->version);
31,673,894✔
1148
  TQ_NULL_GO_TO_END(pTSchema);
31,669,827✔
1149
  tqTrace("vgId:%d, tqProcessRowData start, rows:%d", pReader->pWalReader->pWal->cfg.vgId, numOfRows);
31,669,827✔
1150

1151
  for (int32_t i = 0; i < numOfRows; i++) {
1,330,407,131✔
1152
    bool  buildNew = false;
1,298,747,693✔
1153
    SRow* pRow = taosArrayGetP(pRows, i);
1,298,747,693✔
1154
    TQ_NULL_GO_TO_END(pRow);
1,297,536,452✔
1155

1156
    for (int32_t j = 0; j < pTSchema->numOfCols; j++) {
2,147,483,647✔
1157
      SColVal colVal = {0};
2,147,483,647✔
1158
      TQ_ERR_GO_TO_END(tRowGet(pRow, pTSchema, j, &colVal));
2,147,483,647✔
1159
      PROCESS_VAL
2,147,483,647✔
1160
      tqTrace("assign[%d] = %d, nCols:%d", j, assigned[j], pTSchema->numOfCols);
2,147,483,647✔
1161
    }
1162

1163
    if (buildNew) {
1,275,283,713✔
1164
      TQ_ERR_GO_TO_END(tqProcessBuildNew(pReader, pSubmitTbData, blocks, schemas, assigned, numOfRows, curRow, &lastRow));
31,677,190✔
1165
    }
1166

1167
    SSDataBlock* pBlock = taosArrayGetLast(blocks);
1,275,278,799✔
1168
    TQ_NULL_GO_TO_END(pBlock);
1,297,711,414✔
1169

1170
    tqTrace("vgId:%d, taosx scan, block num: %d", pReader->pWalReader->pWal->cfg.vgId,
1,297,711,414✔
1171
            (int32_t)taosArrayGetSize(blocks));
1172

1173
    int32_t targetIdx = 0;
1,297,711,414✔
1174
    int32_t sourceIdx = 0;
1,297,711,414✔
1175
    int32_t colActual = blockDataGetNumOfCols(pBlock);
1,297,711,414✔
1176
    while (targetIdx < colActual && sourceIdx < pTSchema->numOfCols) {
2,147,483,647✔
1177
      SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, targetIdx);
2,147,483,647✔
1178
      TQ_NULL_GO_TO_END(pColData);
2,147,483,647✔
1179
      SColVal          colVal = {0};
2,147,483,647✔
1180
      TQ_ERR_GO_TO_END(tRowGet(pRow, pTSchema, sourceIdx, &colVal));
2,147,483,647✔
1181
      SET_DATA
2,147,483,647✔
1182
      tqTrace("targetIdx:%d sourceIdx:%d colActual:%d", targetIdx, sourceIdx, colActual);
2,147,483,647✔
1183
    }
1184

1185
    curRow++;
1,298,740,196✔
1186
  }
1187
  SSDataBlock* pLastBlock = taosArrayGetLast(blocks);
31,659,438✔
1188
  if (pLastBlock != NULL) {
31,680,969✔
1189
    pLastBlock->info.rows = curRow - lastRow;
31,680,969✔
1190
  }
1191

1192
  tqTrace("vgId:%d, tqProcessRowData end, rows:%d, block num:%d", pReader->pWalReader->pWal->cfg.vgId, numOfRows,
31,680,144✔
1193
          (int)taosArrayGetSize(blocks));
1194
END:
42,026,807✔
1195
  if (code != TSDB_CODE_SUCCESS) {
31,678,486✔
1196
    tqError("vgId:%d, process row data failed, code:%d", pReader->pWalReader->pWal->cfg.vgId, code);
×
1197
  }
1198
  taosMemoryFreeClear(pTSchema);
31,676,018✔
1199
  taosMemoryFree(assigned);
31,673,948✔
1200
  return code;
31,675,582✔
1201
}
1202

1203
static int32_t tqBuildCreateTbInfo(SMqDataRsp* pRsp, SVCreateTbReq* pCreateTbReq) {
2,724✔
1204
  int32_t code = 0;
2,724✔
1205
  int32_t lino = 0;
2,724✔
1206
  void*   createReq = NULL;
2,724✔
1207
  TSDB_CHECK_NULL(pRsp, code, lino, END, TSDB_CODE_INVALID_PARA);
2,724✔
1208
  TSDB_CHECK_NULL(pCreateTbReq, code, lino, END, TSDB_CODE_INVALID_PARA);
2,724✔
1209

1210
  if (pRsp->createTableNum == 0) {
2,724✔
1211
    pRsp->createTableLen = taosArrayInit(0, sizeof(int32_t));
2,327✔
1212
    TSDB_CHECK_NULL(pRsp->createTableLen, code, lino, END, terrno);
2,327✔
1213
    pRsp->createTableReq = taosArrayInit(0, sizeof(void*));
2,327✔
1214
    TSDB_CHECK_NULL(pRsp->createTableReq, code, lino, END, terrno);
2,327✔
1215
  }
1216

1217
  uint32_t len = 0;
2,724✔
1218
  tEncodeSize(tEncodeSVCreateTbReq, pCreateTbReq, len, code);
2,724✔
1219
  TSDB_CHECK_CODE(code, lino, END);
2,724✔
1220
  createReq = taosMemoryCalloc(1, len);
2,724✔
1221
  TSDB_CHECK_NULL(createReq, code, lino, END, terrno);
2,724✔
1222

1223
  SEncoder encoder = {0};
2,724✔
1224
  tEncoderInit(&encoder, createReq, len);
2,724✔
1225
  code = tEncodeSVCreateTbReq(&encoder, pCreateTbReq);
2,724✔
1226
  tEncoderClear(&encoder);
2,724✔
1227
  TSDB_CHECK_CODE(code, lino, END);
2,724✔
1228
  TSDB_CHECK_NULL(taosArrayPush(pRsp->createTableLen, &len), code, lino, END, terrno);
5,448✔
1229
  TSDB_CHECK_NULL(taosArrayPush(pRsp->createTableReq, &createReq), code, lino, END, terrno);
5,448✔
1230
  pRsp->createTableNum++;
2,724✔
1231
  tqTrace("build create table info msg success");
2,724✔
1232

1233
END:
2,724✔
1234
  if (code != 0) {
2,724✔
1235
    tqError("%s failed at %d, failed to build create table info msg:%s", __FUNCTION__, lino, tstrerror(code));
×
1236
    taosMemoryFree(createReq);
×
1237
  }
1238
  return code;
2,724✔
1239
}
1240

1241

1242

1243
int32_t tqReaderSetTbUidList(STqReader* pReader, const SArray* tbUidList, const char* id) {
199,177✔
1244
  if (pReader == NULL || tbUidList == NULL) {
199,177✔
1245
    return TSDB_CODE_SUCCESS;
×
1246
  }
1247
  if (pReader->tbIdHash) {
199,177✔
1248
    taosHashClear(pReader->tbIdHash);
×
1249
  } else {
1250
    pReader->tbIdHash = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_ENTRY_LOCK);
199,177✔
1251
    if (pReader->tbIdHash == NULL) {
199,177✔
1252
      tqError("s-task:%s failed to init hash table", id);
×
1253
      return terrno;
×
1254
    }
1255
  }
1256

1257
  for (int i = 0; i < taosArrayGetSize(tbUidList); i++) {
2,462,106✔
1258
    int64_t* pKey = (int64_t*)taosArrayGet(tbUidList, i);
2,262,089✔
1259
    if (pKey && taosHashPut(pReader->tbIdHash, pKey, sizeof(int64_t), NULL, 0) != 0) {
2,261,249✔
1260
      tqError("s-task:%s failed to add table uid:%" PRId64 " to hash", id, *pKey);
×
1261
      continue;
×
1262
    }
1263
  }
1264

1265
  tqDebug("s-task:%s %d tables are set to be queried target table", id, (int32_t)taosArrayGetSize(tbUidList));
199,177✔
1266
  return TSDB_CODE_SUCCESS;
199,177✔
1267
}
1268

1269
void tqReaderAddTbUidList(STqReader* pReader, const SArray* pTableUidList) {
118,019✔
1270
  if (pReader == NULL || pTableUidList == NULL) {
118,019✔
1271
    return;
×
1272
  }
1273
  if (pReader->tbIdHash == NULL) {
118,019✔
1274
    pReader->tbIdHash = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_ENTRY_LOCK);
×
1275
    if (pReader->tbIdHash == NULL) {
×
1276
      tqError("failed to init hash table");
×
1277
      return;
×
1278
    }
1279
  }
1280

1281
  int32_t numOfTables = taosArrayGetSize(pTableUidList);
118,019✔
1282
  for (int i = 0; i < numOfTables; i++) {
223,996✔
1283
    int64_t* pKey = (int64_t*)taosArrayGet(pTableUidList, i);
105,977✔
1284
    if (taosHashPut(pReader->tbIdHash, pKey, sizeof(int64_t), NULL, 0) != 0) {
105,977✔
1285
      tqError("failed to add table uid:%" PRId64 " to hash", *pKey);
×
1286
      continue;
×
1287
    }
1288
    tqDebug("%s add table uid:%" PRId64 " to hash:%p %p", __func__, *pKey, pReader, pReader->tbIdHash);
105,977✔
1289
  }
1290
}
1291

1292
bool tqReaderIsQueriedTable(STqReader* pReader, uint64_t uid) {
×
1293
  if (pReader == NULL) {
×
1294
    return false;
×
1295
  }
1296
  return taosHashGet(pReader->tbIdHash, &uid, sizeof(uint64_t)) != NULL;
×
1297
}
1298

1299
bool tqCurrentBlockConsumed(const STqReader* pReader) {
×
1300
  if (pReader == NULL) {
×
1301
    return false;
×
1302
  }
1303
  return pReader->msg.msgStr == NULL;
×
1304
}
1305

1306
void tqReaderRemoveTbUidList(STqReader* pReader, const SArray* tbUidList) {
71,964✔
1307
  if (pReader == NULL || tbUidList == NULL) {
71,964✔
1308
    return;
×
1309
  }
1310
  for (int32_t i = 0; i < taosArrayGetSize(tbUidList); i++) {
156,362✔
1311
    int64_t* pKey = (int64_t*)taosArrayGet(tbUidList, i);
84,398✔
1312
    int32_t code = taosHashRemove(pReader->tbIdHash, pKey, sizeof(int64_t));
84,398✔
1313
    if (code != 0) {
84,398✔
1314
      tqWarn("%s failed to remove table uid:%" PRId64 " from hash:%p %p, msg:%s", __func__, pKey != NULL ? *pKey : 0, pReader, pReader->tbIdHash, tstrerror(code));
24,176✔
1315
    }
1316
  }
1317
}
1318

1319
int32_t tqDeleteTbUidList(STQ* pTq, SArray* tbUidList) {
2,130,122✔
1320
  if (pTq == NULL) {
2,130,122✔
1321
    return 0;  // mounted vnode may have no tq
×
1322
  }
1323
  if (tbUidList == NULL) {
2,130,122✔
1324
    return TSDB_CODE_INVALID_PARA;
×
1325
  }
1326
  void*   pIter = NULL;
2,130,122✔
1327
  int32_t vgId = TD_VID(pTq->pVnode);
2,130,122✔
1328

1329
  // update the table list for each consumer handle
1330
  taosWLockLatch(&pTq->lock);
2,130,122✔
1331
  while (1) {
4,046✔
1332
    pIter = taosHashIterate(pTq->pHandle, pIter);
2,134,168✔
1333
    if (pIter == NULL) {
2,134,168✔
1334
      break;
2,130,122✔
1335
    }
1336

1337
    STqHandle* pTqHandle = (STqHandle*)pIter;
4,046✔
1338
    tqDebug("%s subKey:%s, consumer:0x%" PRIx64 " delete table list", __func__, pTqHandle->subKey, pTqHandle->consumerId);
4,046✔
1339
    if (pTqHandle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
4,046✔
1340
      int32_t code = qDeleteTableListForQuerySub(pTqHandle->execHandle.task, tbUidList);
1,620✔
1341
      if (code != 0) {
1,620✔
1342
        tqError("update qualified table error for %s", pTqHandle->subKey);
×
1343
        continue;
×
1344
      }
1345
    }
1346
  }
1347
  taosWUnLockLatch(&pTq->lock);
2,130,122✔
1348
  return 0;
2,130,122✔
1349
}
1350

1351
static SArray* tqCopyUidList(const SArray* tbUidList) {
70,344✔
1352
  SArray* tbUidListCopy = taosArrayInit(4, sizeof(int64_t));
70,344✔
1353
  if (tbUidListCopy == NULL) {
70,344✔
1354
    return NULL;
×
1355
  }
1356

1357
  if (taosArrayAddAll(tbUidListCopy, tbUidList) == NULL) {
70,344✔
1358
    taosArrayDestroy(tbUidListCopy);
×
1359
    tqError("copy table uid list failed");
×
1360
    return NULL;
×
1361
  }
1362
  return tbUidListCopy;
69,932✔
1363
}
1364

1365
static int32_t tqAddTableListForStbSub(STqHandle* pTqHandle, STQ* pTq, const SArray* tbUidList, int64_t version) {
70,344✔
1366
  int32_t code = 0;
70,344✔
1367
  SArray* tbUidListCopy = tqCopyUidList(tbUidList);
70,344✔
1368
  if (tbUidListCopy == NULL) {
69,932✔
1369
    code = terrno;
×
1370
    goto END;
×
1371
  }
1372
  code = qFilterTableList(pTq->pVnode, tbUidListCopy, version, pTqHandle->execHandle.execTb.node,
69,932✔
1373
                      pTqHandle->execHandle.task, pTqHandle->execHandle.execTb.suid);
69,932✔
1374
  if (code != TDB_CODE_SUCCESS) {
70,344✔
1375
    tqError("%s error:%d handle %s consumer:0x%" PRIx64, __func__, code, pTqHandle->subKey,
×
1376
            pTqHandle->consumerId);
1377
    goto END;
×
1378
  }
1379
  tqDebug("%s handle %s consumer:0x%" PRIx64 " add %d tables to tqReader", __func__, pTqHandle->subKey,
70,344✔
1380
          pTqHandle->consumerId, (int32_t)taosArrayGetSize(tbUidListCopy));
1381
  tqReaderAddTbUidList(pTqHandle->execHandle.pTqReader, tbUidListCopy);
70,344✔
1382

1383
END:
70,344✔
1384
  taosArrayDestroy(tbUidListCopy);
70,344✔
1385
  return code;
70,344✔
1386
}
1387

1388
int32_t tqAddTbUidListForQuerySub(STQ* pTq, const SArray* tbUidList) {
57,035,220✔
1389
  if (pTq == NULL) {
57,035,220✔
1390
    return 0;  // mounted vnode may have no tq
×
1391
  }
1392
  if (tbUidList == NULL) {
57,035,220✔
1393
    return TSDB_CODE_INVALID_PARA;
×
1394
  }
1395
  void*   pIter = NULL;
57,035,220✔
1396
  int32_t vgId = TD_VID(pTq->pVnode);
57,035,220✔
1397
  int32_t code = 0;
57,035,939✔
1398

1399
  // update the table list for each consumer handle
1400
  taosWLockLatch(&pTq->lock);
57,035,939✔
1401
  while (1) {
95,301✔
1402
    pIter = taosHashIterate(pTq->pHandle, pIter);
57,131,240✔
1403
    if (pIter == NULL) {
57,131,240✔
1404
      break;
57,035,939✔
1405
    }
1406

1407
    STqHandle* pTqHandle = (STqHandle*)pIter;
95,301✔
1408
    tqDebug("%s subKey:%s, consumer:0x%" PRIx64 " add table list", __func__, pTqHandle->subKey, pTqHandle->consumerId);
95,301✔
1409
    if (pTqHandle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
95,301✔
1410
      code = qAddTableListForQuerySub(pTqHandle->execHandle.task, tbUidList);
47,675✔
1411
      if (code != 0) {
47,675✔
1412
        tqError("add table list for query tmq error for %s, msg:%s", pTqHandle->subKey, tstrerror(code));
×
1413
        break;
×
1414
      }
1415
    }
1416
  }
1417
  taosHashCancelIterate(pTq->pHandle, pIter);
57,035,939✔
1418
  taosWUnLockLatch(&pTq->lock);
57,034,846✔
1419

1420
  return code;
57,035,939✔
1421
}
1422

1423
int32_t tqUpdateTbUidListForQuerySub(STQ* pTq, const SArray* tbUidList, SArray* cidList, SArray* cidListArray) {
8,660,619✔
1424
  if (pTq == NULL) {
8,660,619✔
1425
    return 0;  // mounted vnode may have no tq
×
1426
  }
1427
  if (tbUidList == NULL) {
8,660,619✔
1428
    return TSDB_CODE_INVALID_PARA;
×
1429
  }
1430
  void*   pIter = NULL;
8,660,619✔
1431
  int32_t vgId = TD_VID(pTq->pVnode);
8,660,619✔
1432
  int32_t code = 0;
8,660,619✔
1433
  // update the table list for each consumer handle
1434
  taosWLockLatch(&pTq->lock);
8,660,619✔
1435
  while (1) {
2,396✔
1436
    pIter = taosHashIterate(pTq->pHandle, pIter);
8,663,015✔
1437
    if (pIter == NULL) {
8,663,015✔
1438
      break;
8,660,619✔
1439
    }
1440

1441
    STqHandle* pTqHandle = (STqHandle*)pIter;
2,396✔
1442
    tqDebug("%s subKey:%s, consumer:0x%" PRIx64 " update table list", __func__, pTqHandle->subKey, pTqHandle->consumerId);
2,396✔
1443
    if (pTqHandle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
2,396✔
1444
      SNode* pTagCond = getTagCondNodeForQueryTmq(pTqHandle->execHandle.task);
776✔
1445
      bool ret = checkCidInTagCondition(pTagCond, cidList);
776✔
1446
      if (ret){
776✔
1447
        code = qUpdateTableListForQuerySub(pTqHandle->execHandle.task, tbUidList);
×
1448
        if (code != 0) {
×
1449
          tqError("update table list for query tmq error for %s, msg:%s", pTqHandle->subKey, tstrerror(code));
×
1450
          break;
×
1451
        }
1452
      }
1453
      qUpdateTableTagCacheForQuerySub(pTqHandle->execHandle.task, tbUidList, cidList, cidListArray);
776✔
1454
    }
1455
  }
1456

1457
  taosHashCancelIterate(pTq->pHandle, pIter);
8,660,619✔
1458
  taosWUnLockLatch(&pTq->lock);
8,660,619✔
1459

1460
  return code;
8,660,619✔
1461
}
1462

1463
static int32_t tqUpdateTableListForStbSub(STQ* pTq, const SArray* tbUidList, SArray* cidList, SArray* cidListArray, STqHandle* pTqHandle, int64_t version) {
5,237✔
1464
  if (pTq == NULL) {
5,237✔
1465
    return 0;  // mounted vnode may have no tq
×
1466
  }
1467
  if (tbUidList == NULL) {
5,237✔
1468
    return TSDB_CODE_INVALID_PARA;
×
1469
  }
1470
  void*   pIter = NULL;
5,237✔
1471
  int32_t vgId = TD_VID(pTq->pVnode);
5,237✔
1472
  int32_t code = 0;
5,237✔
1473
  taosWLockLatch(&pTq->lock);
5,237✔
1474
  tqDebug("%s subKey:%s, consumer:0x%" PRIx64 " update table list", __func__, pTqHandle->subKey, pTqHandle->consumerId);
5,237✔
1475
  SNode* pTagCond = getTagCondNodeForStableTmq(pTqHandle->execHandle.execTb.node);
5,237✔
1476
  bool ret = checkCidInTagCondition(pTagCond, cidList);
5,237✔
1477
  if (ret){
5,237✔
1478
    tqReaderRemoveTbUidList(pTqHandle->execHandle.pTqReader, tbUidList);
2,777✔
1479
    code = tqAddTableListForStbSub(pTqHandle, pTq, tbUidList, version);
2,777✔
1480
    if (code != 0) {
2,777✔
1481
      tqError("update table list for stable tmq error for %s, msg:%s", pTqHandle->subKey, tstrerror(code));
×
1482
    }
1483
  }
1484
  taosWUnLockLatch(&pTq->lock);
5,237✔
1485

1486
  return code;
5,237✔
1487
}
1488

1489
static void tqAlterTagForStbSub(SVnode *pVnode, const SArray* tbUidList, const SArray* tags, const SArray* tagsArray, STqHandle* pTqHandle, int64_t version) {
5,237✔
1490
  int32_t       code = 0;
5,237✔
1491
  int32_t       lino = 0;
5,237✔
1492
  SArray*       cidList = NULL;
5,237✔
1493
  SArray*       cidListArray = NULL;
5,237✔
1494

1495
  code = getCidInfo(tags, tagsArray, &cidList, &cidListArray);
5,237✔
1496
  QUERY_CHECK_CODE(code, lino, end);
5,237✔
1497

1498
  tqDebug("vgId:%d, try to add %d tables in query table list, cidList size:%"PRIzu,
5,237✔
1499
         TD_VID(pVnode), (int32_t)taosArrayGetSize(tbUidList), taosArrayGetSize(cidList));
1500
  code = tqUpdateTableListForStbSub(pVnode->pTq, tbUidList, cidList, cidListArray, pTqHandle, version);
5,237✔
1501
  QUERY_CHECK_CODE(code, lino, end);
5,237✔
1502

1503
end:
5,237✔
1504
  if (code != 0) {
5,237✔
1505
    qError("vgId:%d, failed to alter tags for %d tables since %s",
×
1506
           TD_VID(pVnode), (int32_t)taosArrayGetSize(tbUidList), tstrerror(code));
1507
  }
1508
  taosArrayDestroy(cidList);
5,237✔
1509
  taosArrayDestroyP(cidListArray, (FDelete)taosArrayDestroy);
5,237✔
1510
}
5,237✔
1511

1512
static void tqDestroySourceScanTables(void* ptr) {
×
1513
  SArray** pTables = ptr;
×
1514
  if (pTables && *pTables) {
×
1515
    taosArrayDestroy(*pTables);
×
1516
    *pTables = NULL;
×
1517
  }
1518
}
×
1519

1520
static int32_t tqCompareSVTColInfo(const void* p1, const void* p2) {
×
1521
  SVTColInfo* pCol1 = (SVTColInfo*)p1;
×
1522
  SVTColInfo* pCol2 = (SVTColInfo*)p2;
×
1523
  if (pCol1->vColId == pCol2->vColId) {
×
1524
    return 0;
×
1525
  } else if (pCol1->vColId < pCol2->vColId) {
×
1526
    return -1;
×
1527
  } else {
1528
    return 1;
×
1529
  }
1530
}
1531

1532
static void tqFreeTableSchemaCache(const void* key, size_t keyLen, void* value, void* ud) {
×
1533
  if (value) {
×
1534
    SSchemaWrapper* pSchemaWrapper = value;
×
1535
    tDeleteSchemaWrapper(pSchemaWrapper);
1536
  }
1537
}
×
1538

1539
static int32_t tqAddRawDataToRsp(const void* rawData, SMqDataRsp* pRsp, int8_t precision) {
×
1540
  int32_t    code = TDB_CODE_SUCCESS;
×
1541
  int32_t    lino = 0;
×
1542
  void*      buf = NULL;
×
1543

1544
  int32_t dataStrLen = sizeof(SRetrieveTableRspForTmq) + *(uint32_t *)rawData + INT_BYTES;
×
1545
  buf = taosMemoryCalloc(1, dataStrLen);
×
1546
  TSDB_CHECK_NULL(buf, code, lino, END, terrno);
×
1547

1548
  SRetrieveTableRspForTmq* pRetrieve = (SRetrieveTableRspForTmq*)buf;
×
1549
  pRetrieve->version = RETRIEVE_TABLE_RSP_TMQ_RAW_VERSION;
×
1550
  pRetrieve->precision = precision;
×
1551
  pRetrieve->compressed = 0;
×
1552

1553
  memcpy(pRetrieve->data, rawData, *(uint32_t *)rawData + INT_BYTES);
×
1554
  TSDB_CHECK_NULL(taosArrayPush(pRsp->blockDataLen, &dataStrLen), code, lino, END, terrno);
×
1555
  TSDB_CHECK_NULL(taosArrayPush(pRsp->blockData, &buf), code, lino, END, terrno);
×
1556
  pRsp->blockDataElementFree = true;
×
1557

1558
  tqTrace("tqAddRawDataToRsp add block data to block array, blockDataLen:%d, blockData:%p", dataStrLen, buf);
×
1559
  END:
×
1560
  if (code != TSDB_CODE_SUCCESS) {
×
1561
    taosMemoryFree(buf);
×
1562
    tqError("%s failed at %d, failed to add block data to response:%s", __FUNCTION__, lino, tstrerror(code));
×
1563
  }
1564
  return code;
×
1565
}
1566

1567
static int32_t tqAddBlockDataToRsp(const SSDataBlock* pBlock, SMqDataRsp* pRsp, const SSchemaWrapper* pSW, int8_t precision) {
42,863,830✔
1568
  int32_t code = 0;
42,863,830✔
1569
  int32_t lino = 0;
42,863,830✔
1570
  SSchemaWrapper* pSchema = NULL;
42,863,830✔
1571
  
1572
  size_t dataEncodeBufSize = blockGetEncodeSize(pBlock);
42,865,900✔
1573
  int32_t dataStrLen = sizeof(SRetrieveTableRspForTmq) + dataEncodeBufSize;
42,873,659✔
1574
  void*   buf = taosMemoryCalloc(1, dataStrLen);
42,873,659✔
1575
  TSDB_CHECK_NULL(buf, code, lino, END, terrno);
42,866,615✔
1576

1577
  SRetrieveTableRspForTmq* pRetrieve = (SRetrieveTableRspForTmq*)buf;
42,866,615✔
1578
  pRetrieve->version = RETRIEVE_TABLE_RSP_TMQ_VERSION;
42,866,615✔
1579
  pRetrieve->precision = precision;
42,867,023✔
1580
  pRetrieve->compressed = 0;
42,867,023✔
1581
  pRetrieve->numOfRows = htobe64((int64_t)pBlock->info.rows);
42,866,635✔
1582

1583
  int32_t actualLen = blockEncode(pBlock, pRetrieve->data, dataEncodeBufSize, pSW->nCols);
42,872,395✔
1584
  TSDB_CHECK_CONDITION(actualLen >= 0, code, lino, END, terrno);
42,863,885✔
1585

1586
  actualLen += sizeof(SRetrieveTableRspForTmq);
42,863,885✔
1587
  TSDB_CHECK_NULL(taosArrayPush(pRsp->blockDataLen, &actualLen), code, lino, END, terrno);
85,732,600✔
1588
  TSDB_CHECK_NULL(taosArrayPush(pRsp->blockData, &buf), code, lino, END, terrno);
85,739,892✔
1589
  pSchema = tCloneSSchemaWrapper(pSW);
42,864,643✔
1590
  TSDB_CHECK_NULL(pSchema, code, lino, END, terrno);
42,864,643✔
1591
  TSDB_CHECK_NULL(taosArrayPush(pRsp->blockSchema, &pSchema), code, lino, END, terrno);
85,733,790✔
1592
  pSchema = NULL;
42,869,147✔
1593
  pRsp->blockDataElementFree = true;
42,869,147✔
1594
  tqTrace("tqAddBlockDataToRsp add block data to block array, blockDataLen:%d, blockData:%p", dataStrLen, buf);
42,864,621✔
1595

1596
END:
42,864,621✔
1597
  tDeleteSchemaWrapper(pSchema);
42,862,583✔
1598
  if (code != TSDB_CODE_SUCCESS){
42,858,186✔
1599
    taosMemoryFree(buf);
×
1600
    tqError("%s failed at line %d with msg:%s", __func__, lino, tstrerror(code));
×
1601
  }
1602
  return code;
42,858,186✔
1603
}
1604

1605
static int32_t tqAddTbNameToRsp(const STQ* pTq, int64_t uid, SMqDataRsp* pRsp, int32_t n) {
32,519,247✔
1606
  int32_t    code = TDB_CODE_SUCCESS;
32,519,247✔
1607
  int32_t    lino = 0;
32,519,247✔
1608
  SMetaReader mr = {0};
32,519,247✔
1609

1610
  TSDB_CHECK_NULL(pTq, code, lino, END, TSDB_CODE_INVALID_PARA);
32,523,358✔
1611
  TSDB_CHECK_NULL(pRsp, code, lino, END, TSDB_CODE_INVALID_PARA);
32,523,358✔
1612

1613
  metaReaderDoInit(&mr, pTq->pVnode->pMeta, META_READER_LOCK);
32,523,358✔
1614

1615
  code = metaReaderGetTableEntryByUidCache(&mr, uid);
32,523,732✔
1616
  if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST){
32,511,537✔
1617
    char tbname[TSDB_TABLE_NAME_LEN] = {0};
193,221✔
1618
    code = metaGetTbnameByIdIfTableNotExist(pTq->pVnode->pMeta, uid, tbname);
193,221✔
1619
    TSDB_CHECK_CODE(code, lino, END);
191,985✔
1620

1621
    for (int32_t i = 0; i < n; i++) {
384,382✔
1622
      char* tbName = taosStrdup(tbname);
191,985✔
1623
      TSDB_CHECK_NULL(tbName, code, lino, END, terrno);
191,985✔
1624
      if(taosArrayPush(pRsp->blockTbName, &tbName) == NULL){
384,382✔
1625
        tqError("failed to push tbName to blockTbName:%s, uid:%"PRId64, tbName, uid);
×
1626
        continue;
×
1627
      }
1628
      tqTrace("add tbName to response success tbname:%s, uid:%"PRId64, tbName, uid);
192,397✔
1629
    }
1630
    goto END;
192,397✔
1631
  }
1632
  TSDB_CHECK_CODE(code, lino, END);
32,318,316✔
1633

1634
  for (int32_t i = 0; i < n; i++) {
64,643,465✔
1635
    char* tbName = taosStrdup(mr.me.name);
32,317,492✔
1636
    TSDB_CHECK_NULL(tbName, code, lino, END, terrno);
32,324,064✔
1637
    if(taosArrayPush(pRsp->blockTbName, &tbName) == NULL){
64,653,333✔
1638
      tqError("failed to push tbName to blockTbName:%s, uid:%"PRId64, tbName, uid);
×
1639
      continue;
×
1640
    }
1641
    tqTrace("add tbName to response success tbname:%s, uid:%"PRId64, tbName, uid);
32,329,269✔
1642
  }
1643

1644
END:
32,518,370✔
1645
  if (code != TSDB_CODE_SUCCESS) {
32,518,024✔
1646
    tqError("%s failed at %d, failed to add tbName to response:%s, uid:%"PRId64, __FUNCTION__, lino, tstrerror(code), uid);
×
1647
  }
1648
  metaReaderClear(&mr);
32,518,024✔
1649
  return code;
32,517,257✔
1650
}
1651

1652
int32_t tqGetDataBlock(qTaskInfo_t task, const STqHandle* pHandle, int32_t vgId, SSDataBlock** res) {
59,853,833✔
1653
  if (task == NULL || pHandle == NULL || res == NULL) {
59,853,833✔
1654
    return TSDB_CODE_INVALID_PARA;
×
1655
  }
1656
  uint64_t ts = 0;
59,871,533✔
1657
  qStreamSetOpen(task);
59,867,607✔
1658

1659
  tqDebug("consumer:0x%" PRIx64 " vgId:%d, tmq one task start execute", pHandle->consumerId, vgId);
59,836,028✔
1660
  int32_t code = qExecTask(task, res, &ts);
59,888,003✔
1661
  if (code != TSDB_CODE_SUCCESS) {
59,892,178✔
1662
    tqError("consumer:0x%" PRIx64 " vgId:%d, task exec error since %s", pHandle->consumerId, vgId, tstrerror(code));
×
1663
  }
1664

1665
  tqDebug("consumer:0x%" PRIx64 " vgId:%d tmq one task end executed, pDataBlock:%p", pHandle->consumerId, vgId, *res);
59,892,171✔
1666
  return code;
59,892,175✔
1667
}
1668

1669
static int32_t tqProcessReplayRsp(STQ* pTq, STqHandle* pHandle, SMqDataRsp* pRsp, const SMqPollReq* pRequest, SSDataBlock* pDataBlock, qTaskInfo_t task){
×
1670
  int32_t code = 0;
×
1671
  int32_t lino = 0;
×
1672

1673
  if (IS_OFFSET_RESET_TYPE(pRequest->reqOffset.type) && pHandle->block != NULL) {
×
1674
    blockDataDestroy(pHandle->block);
×
1675
    pHandle->block = NULL;
×
1676
  }
1677
  if (pHandle->block == NULL) {
×
1678
    if (pDataBlock == NULL) {
×
1679
      goto END;
×
1680
    }
1681

1682
    STqOffsetVal offset = {0};
×
1683
    code = qStreamExtractOffset(task, &offset);
×
1684
    TSDB_CHECK_CODE(code, lino, END);
×
1685

1686
    pHandle->block = NULL;
×
1687

1688
    code = createOneDataBlock(pDataBlock, true, &pHandle->block);
×
1689
    TSDB_CHECK_CODE(code, lino, END);
×
1690

1691
    pHandle->blockTime = offset.ts;
×
1692
    tOffsetDestroy(&offset);
×
1693
    int32_t vgId = TD_VID(pTq->pVnode);
×
1694
    code = tqGetDataBlock(task, pHandle, vgId, &pDataBlock);
×
1695
    TSDB_CHECK_CODE(code, lino, END);
×
1696
  }
1697

1698
  const STqExecHandle* pExec = &pHandle->execHandle;
×
1699
  code = tqAddBlockDataToRsp(pHandle->block, pRsp, &pExec->execCol.pSW, pTq->pVnode->config.tsdbCfg.precision);
×
1700
  TSDB_CHECK_CODE(code, lino, END);
×
1701

1702
  pRsp->blockNum++;
×
1703
  if (pDataBlock == NULL) {
×
1704
    blockDataDestroy(pHandle->block);
×
1705
    pHandle->block = NULL;
×
1706
  } else {
1707
    code = copyDataBlock(pHandle->block, pDataBlock);
×
1708
    TSDB_CHECK_CODE(code, lino, END);
×
1709

1710
    STqOffsetVal offset = {0};
×
1711
    code = qStreamExtractOffset(task, &offset);
×
1712
    TSDB_CHECK_CODE(code, lino, END);
×
1713

1714
    pRsp->sleepTime = offset.ts - pHandle->blockTime;
×
1715
    pHandle->blockTime = offset.ts;
×
1716
    tOffsetDestroy(&offset);
×
1717
  }
1718

1719
END:
×
1720
  if (code != TSDB_CODE_SUCCESS) {
×
1721
    tqError("%s failed at %d, failed to process replay response:%s", __FUNCTION__, lino, tstrerror(code));
×
1722
  }
1723
  return code;
×
1724
}
1725

1726
static int32_t tqScanData(STQ* pTq, STqHandle* pHandle, SMqDataRsp* pRsp, STqOffsetVal* pOffset, const SMqPollReq* pRequest) {
68,473,629✔
1727
  int32_t code = 0;
68,473,629✔
1728
  int32_t lino = 0;
68,473,629✔
1729
  TSDB_CHECK_NULL(pRsp, code, lino, END, TSDB_CODE_INVALID_PARA);
68,473,629✔
1730
  TSDB_CHECK_NULL(pTq, code, lino, END, TSDB_CODE_INVALID_PARA);
68,473,629✔
1731
  TSDB_CHECK_NULL(pHandle, code, lino, END, TSDB_CODE_INVALID_PARA);
68,473,629✔
1732
  TSDB_CHECK_NULL(pOffset, code, lino, END, TSDB_CODE_INVALID_PARA);
68,473,629✔
1733
  TSDB_CHECK_NULL(pRequest, code, lino, END, TSDB_CODE_INVALID_PARA);
68,473,629✔
1734

1735
  int32_t vgId = TD_VID(pTq->pVnode);
68,473,629✔
1736
  int32_t totalRows = 0;
68,471,585✔
1737

1738
  const STqExecHandle* pExec = &pHandle->execHandle;
68,471,585✔
1739
  qTaskInfo_t          task = pExec->task;
68,480,378✔
1740

1741
  code = qStreamPrepareScan(task, pOffset, pHandle->execHandle.subType);
68,476,023✔
1742
  TSDB_CHECK_CODE(code, lino, END);
68,460,366✔
1743

1744
  qStreamSetParams(task, pRequest->sourceExcluded, pRequest->minPollRows, pRequest->timeout, pRequest->enableReplay);
59,859,194✔
1745
  do {
1746
    SSDataBlock* pDataBlock = NULL;
59,818,639✔
1747
    code = tqGetDataBlock(task, pHandle, vgId, &pDataBlock);
59,825,728✔
1748
    TSDB_CHECK_CODE(code, lino, END);
59,892,967✔
1749

1750
    if (pRequest->enableReplay) {
59,892,967✔
1751
      code = tqProcessReplayRsp(pTq, pHandle, pRsp, pRequest, pDataBlock, task);
×
1752
      TSDB_CHECK_CODE(code, lino, END);
×
1753
      break;
×
1754
    }
1755
    if (pDataBlock == NULL) {
59,891,766✔
1756
      break;
49,572,993✔
1757
    }
1758
    code = tqAddBlockDataToRsp(pDataBlock, pRsp, &pExec->execCol.pSW, pTq->pVnode->config.tsdbCfg.precision);
10,318,773✔
1759
    TSDB_CHECK_CODE(code, lino, END);
10,317,629✔
1760

1761
    pRsp->blockNum++;
10,317,629✔
1762
    totalRows += pDataBlock->info.rows;
10,317,548✔
1763
  } while(0);
1764

1765
  tqDebug("consumer:0x%" PRIx64 " vgId:%d tmq task executed finished, total blocks:%d, totalRows:%d", pHandle->consumerId, vgId, pRsp->blockNum, totalRows);
59,888,980✔
1766
  code = qStreamExtractOffset(task, &pRsp->rspOffset);
59,891,385✔
1767

1768
END:
68,492,959✔
1769
  if (code != 0) {
68,492,959✔
1770
    tqError("%s failed at %d, tmq task executed error msg:%s", __FUNCTION__, lino, tstrerror(code));
8,601,172✔
1771
  }
1772
  return code;
68,492,939✔
1773
}
1774

1775
static int32_t tqScanTaosx(STQ* pTq, const STqHandle* pHandle, SMqDataRsp* pRsp, SMqBatchMetaRsp* pBatchMetaRsp, STqOffsetVal* pOffset, const SMqPollReq* pRequest) {
23,080✔
1776
  int32_t code = 0;
23,080✔
1777
  int32_t lino = 0;
23,080✔
1778
  char* tbName = NULL;
23,080✔
1779
  const STqExecHandle* pExec = &pHandle->execHandle;
23,080✔
1780
  qTaskInfo_t          task = pExec->task;
23,080✔
1781
  code = qStreamPrepareScan(task, pOffset, pHandle->execHandle.subType);
23,080✔
1782
  TSDB_CHECK_CODE(code, lino, END);
23,080✔
1783

1784
  qStreamSetParams(task, pRequest->sourceExcluded, pRequest->minPollRows, pRequest->timeout, false);
23,080✔
1785

1786
  int32_t rowCnt = 0;
23,080✔
1787
  int64_t st = taosGetTimestampMs();
23,080✔
1788
  while (1) {
26,023✔
1789
    SSDataBlock* pDataBlock = NULL;
49,103✔
1790
    uint64_t     ts = 0;
49,103✔
1791
    tqDebug("tmqsnap task start to execute");
49,103✔
1792
    code = qExecTask(task, &pDataBlock, &ts);
49,103✔
1793
    TSDB_CHECK_CODE(code, lino, END);
49,103✔
1794
    tqDebug("tmqsnap task execute end, get %p", pDataBlock);
49,103✔
1795

1796
    if (pDataBlock != NULL && pDataBlock->info.rows > 0) {
49,103✔
1797
      if (pRsp->withTbName) {
30,724✔
1798
        tbName = taosStrdup(qExtractTbnameFromTask(task));
30,724✔
1799
        TSDB_CHECK_NULL(tbName, code, lino, END, terrno);
30,724✔
1800
        TSDB_CHECK_NULL(taosArrayPush(pRsp->blockTbName, &tbName), code, lino, END, terrno);
61,448✔
1801
        tqDebug("vgId:%d, add tbname:%s to rsp msg", pTq->pVnode->config.vgId, tbName);
30,724✔
1802
        tbName = NULL;
30,724✔
1803
      }
1804

1805
      code = tqAddBlockDataToRsp(pDataBlock, pRsp, qExtractSchemaFromTask(task), pTq->pVnode->config.tsdbCfg.precision);
30,724✔
1806
      TSDB_CHECK_CODE(code, lino, END);
30,724✔
1807

1808
      pRsp->blockNum++;
30,724✔
1809
      rowCnt += pDataBlock->info.rows;
30,724✔
1810
      if (rowCnt <= pRequest->minPollRows && (taosGetTimestampMs() - st <= pRequest->timeout)) {
46,124✔
1811
        continue;
15,400✔
1812
      }
1813
    }
1814

1815
    // get meta
1816
    SMqBatchMetaRsp* tmp = qStreamExtractMetaMsg(task);
33,703✔
1817
    if (taosArrayGetSize(tmp->batchMetaReq) > 0) {
33,703✔
1818
      code = qStreamExtractOffset(task, &tmp->rspOffset);
3,482✔
1819
      TSDB_CHECK_CODE(code, lino, END);
3,482✔
1820
      *pBatchMetaRsp = *tmp;
3,482✔
1821
      tqDebug("tmqsnap task get meta");
3,482✔
1822
      break;
3,482✔
1823
    }
1824

1825
    if (pDataBlock == NULL) {
30,221✔
1826
      code = qStreamExtractOffset(task, pOffset);
14,897✔
1827
      TSDB_CHECK_CODE(code, lino, END);
14,897✔
1828

1829
      if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
14,897✔
1830
        continue;
10,623✔
1831
      }
1832

1833
      tqDebug("tmqsnap vgId: %d, tsdb consume over, switch to wal, ver %" PRId64, TD_VID(pTq->pVnode), pHandle->snapshotVer + 1);
4,274✔
1834
      code = qStreamExtractOffset(task, &pRsp->rspOffset);
4,274✔
1835
      break;
4,274✔
1836
    }
1837

1838
    if (pRsp->blockNum > 0) {
15,324✔
1839
      tqDebug("tmqsnap task exec exited, get data");
15,324✔
1840
      code = qStreamExtractOffset(task, &pRsp->rspOffset);
15,324✔
1841
      break;
15,324✔
1842
    }
1843
  }
1844
  tqDebug("%s:%d success", __FUNCTION__, lino);
23,080✔
1845
END:
23,080✔
1846
  if (code != 0){
23,080✔
1847
    tqError("%s failed at %d, vgId:%d, task exec error since %s", __FUNCTION__ , lino, pTq->pVnode->config.vgId, tstrerror(code));
×
1848
  }
1849
  taosMemoryFree(tbName);
23,080✔
1850
  return code;
23,080✔
1851
}
1852

1853
static int32_t tqRetrieveTaosxBlock(STqReader* pReader, SMqDataRsp* pRsp, SArray* blocks, SArray* schemas,
32,523,400✔
1854
                             SSubmitTbData* pSubmitTbData, SArray* rawList, int8_t fetchMeta) {
1855
  tqTrace("tq reader retrieve data block msg pointer:%p, index:%d", pReader->msg.msgStr, pReader->nextBlk);
32,523,400✔
1856
  if (fetchMeta == ONLY_META) {
32,522,566✔
1857
    if (pSubmitTbData->pCreateTbReq != NULL) {
×
1858
      if (pRsp->createTableReq == NULL) {
×
1859
        pRsp->createTableReq = taosArrayInit(0, POINTER_BYTES);
×
1860
        if (pRsp->createTableReq == NULL) {
×
1861
          return terrno;
×
1862
        }
1863
      }
1864
      if (taosArrayPush(pRsp->createTableReq, &pSubmitTbData->pCreateTbReq) == NULL) {
×
1865
        return terrno;
×
1866
      }
1867
      pSubmitTbData->pCreateTbReq = NULL;
×
1868
    }
1869
    return 0;
×
1870
  }
1871

1872
  int32_t sversion = pSubmitTbData->sver;
32,522,566✔
1873
  int64_t uid = pSubmitTbData->uid;
32,523,390✔
1874
  pReader->lastBlkUid = uid;
32,523,398✔
1875

1876
  tDeleteSchemaWrapper(pReader->pSchemaWrapper);
32,524,156✔
1877
  taosMemoryFreeClear(pReader->extSchema);
32,522,100✔
1878
  pReader->pSchemaWrapper = metaGetTableSchema(pReader->pVnode->pMeta, uid, sversion, 1, &pReader->extSchema, 0, true);
32,523,308✔
1879
  if (pReader->pSchemaWrapper == NULL) {
32,519,616✔
1880
    tqWarn("vgId:%d, cannot found schema wrapper for table: suid:%" PRId64 ", version %d, possibly dropped table",
×
1881
           pReader->pWalReader->pWal->cfg.vgId, uid, sversion);
1882
    pReader->cachedSchemaSuid = 0;
×
1883
    return TSDB_CODE_TQ_TABLE_SCHEMA_NOT_FOUND;
×
1884
  }
1885

1886
  if (pSubmitTbData->pCreateTbReq != NULL && fetchMeta != ONLY_DATA) {
32,514,524✔
1887
    int32_t code = tqBuildCreateTbInfo(pRsp, pSubmitTbData->pCreateTbReq);
2,724✔
1888
    if (code != 0) {
2,724✔
1889
      return code;
×
1890
    }
1891
  } else if (rawList != NULL) {
32,511,936✔
1892
    if (taosArrayPush(schemas, &pReader->pSchemaWrapper) == NULL) {
×
1893
      return terrno;
×
1894
    }
1895
    pReader->pSchemaWrapper = NULL;
×
1896
    return 0;
×
1897
  }
1898

1899
  if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
32,514,660✔
1900
    return tqProcessColData(pReader, pSubmitTbData, blocks, schemas);
845,250✔
1901
  } else {
1902
    return tqProcessRowData(pReader, pSubmitTbData, blocks, schemas);
31,669,004✔
1903
  }
1904
}
1905

1906
static bool tqFilterForStbSub(STQ* pTq, STqHandle* pHandle, SSubmitTbData* pSubmitTbData, int64_t version) {
33,893,026✔
1907
  bool ret = false;
33,893,026✔
1908
  SArray* tbUids = NULL;
33,893,026✔
1909
  if (pHandle->execHandle.subType != TOPIC_SUB_TYPE__TABLE) {
33,893,026✔
1910
    goto end;
21,893,490✔
1911
  }
1912
  if (pSubmitTbData->pCreateTbReq != NULL && (pSubmitTbData->pCreateTbReq->type == TSDB_CHILD_TABLE || pSubmitTbData->pCreateTbReq->type == TSDB_VIRTUAL_CHILD_TABLE)
12,003,196✔
1913
        && pSubmitTbData->pCreateTbReq->ctb.suid == pHandle->execHandle.execTb.suid) {
12,168✔
1914
    tbUids = taosArrayInit(1, sizeof(int64_t));
9,786✔
1915
    if (tbUids == NULL) {
9,786✔
1916
      goto end;
×
1917
    }
1918
    if (taosArrayPush(tbUids, &pSubmitTbData->uid) == NULL) {
19,572✔
1919
      goto end;
×
1920
    }
1921
    
1922
    taosWLockLatch(&pTq->lock);
9,786✔
1923
    tqReaderRemoveTbUidList(pHandle->execHandle.pTqReader, tbUids);
9,786✔
1924
    int32_t code = tqAddTableListForStbSub(pHandle, pTq, tbUids, version);
9,786✔
1925
    taosWUnLockLatch(&pTq->lock);
9,786✔
1926
    if (code != 0) {
9,786✔
1927
      goto end;
×
1928
    }
1929
  }
1930
  
1931
  STqExecHandle* pExec = &pHandle->execHandle;
12,003,196✔
1932
  STqReader* pReader = pExec->pTqReader;
12,003,196✔
1933
  if (taosHashGet(pReader->tbIdHash, &pSubmitTbData->uid, sizeof(int64_t)) == NULL) {
12,003,196✔
1934
    tqInfo("iterator submit block in hash continue for stb sub, progress:%d/%d, total queried tables:%d, uid:%" PRId64,
1,372,122✔
1935
            pReader->nextBlk, (int32_t)taosArrayGetSize(pReader->submit.aSubmitTbData), (int32_t)taosHashGetSize(pReader->tbIdHash), pSubmitTbData->uid);
1936
    ret = true;
1,376,390✔
1937
  }
1938

1939
end:
10,630,686✔
1940
  taosArrayDestroy(tbUids);
33,900,566✔
1941
  return ret;
33,898,125✔
1942
}
1943

1944
static void tqProcessSubData(STQ* pTq, STqHandle* pHandle, SMqDataRsp* pRsp, int32_t* totalRows,
33,893,478✔
1945
                             const SMqPollReq* pRequest, SArray* rawList, int64_t version){
1946
  int32_t code = 0;
33,893,478✔
1947
  int32_t lino = 0;
33,893,478✔
1948
  SArray* pBlocks = NULL;
33,893,478✔
1949
  SArray* pSchemas = NULL;
33,893,478✔
1950

1951
  STqExecHandle* pExec = &pHandle->execHandle;
33,893,478✔
1952
  STqReader* pReader = pExec->pTqReader;
33,896,738✔
1953

1954
  SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
33,894,706✔
1955
  TSDB_CHECK_NULL(pSubmitTbData, code, lino, END, terrno);
33,894,650✔
1956

1957
  if (tqFilterForStbSub(pTq, pHandle, pSubmitTbData, version)) {
33,894,650✔
1958
    goto END;
1,376,390✔
1959
  }
1960

1961
  pBlocks = taosArrayInit(0, sizeof(SSDataBlock));
32,521,753✔
1962
  TSDB_CHECK_NULL(pBlocks, code, lino, END, terrno);
32,523,750✔
1963
  pSchemas = taosArrayInit(0, sizeof(void*));
32,523,750✔
1964
  TSDB_CHECK_NULL(pSchemas, code, lino, END, terrno);
32,524,608✔
1965
  
1966
  code = tqRetrieveTaosxBlock(pReader, pRsp, pBlocks, pSchemas, pSubmitTbData, rawList, pHandle->fetchMeta);
32,524,608✔
1967
  TSDB_CHECK_CODE(code, lino, END);
32,519,649✔
1968
  bool tmp = (pSubmitTbData->flags & pRequest->sourceExcluded) != 0;
32,519,649✔
1969
  TSDB_CHECK_CONDITION(!tmp, code, lino, END, TSDB_CODE_SUCCESS);
32,522,524✔
1970

1971
  if (pHandle->fetchMeta == ONLY_META){
32,522,524✔
1972
    goto END;
×
1973
  }
1974

1975
  int32_t blockNum = taosArrayGetSize(pBlocks) == 0 ? 1 : taosArrayGetSize(pBlocks);
32,525,792✔
1976
  if (pRsp->withTbName) {
32,525,792✔
1977
    int64_t uid = pExec->pTqReader->lastBlkUid;
32,525,380✔
1978
    code = tqAddTbNameToRsp(pTq, uid, pRsp, blockNum);
32,525,380✔
1979
    TSDB_CHECK_CODE(code, lino, END);
32,512,760✔
1980
  }
1981

1982
  TSDB_CHECK_CONDITION(!tmp, code, lino, END, TSDB_CODE_SUCCESS);
32,512,760✔
1983
  for (int32_t i = 0; i < blockNum; i++) {
65,028,310✔
1984
    if (taosArrayGetSize(pBlocks) == 0){
32,510,695✔
1985
      void* rawData = taosArrayGetP(rawList, pReader->nextBlk);
×
1986
      if (rawData == NULL) {
×
1987
        continue;
×
1988
      }
1989
      if (tqAddRawDataToRsp(rawData, pRsp, pTq->pVnode->config.tsdbCfg.precision) != 0){
×
1990
        tqError("vgId:%d, failed to add block to rsp msg", pTq->pVnode->config.vgId);
×
1991
        continue;
×
1992
      }
1993
      *totalRows += *(uint32_t *)rawData + INT_BYTES; // bytes actually
×
1994
    } else {
1995
      SSDataBlock* pBlock = taosArrayGet(pBlocks, i);
32,516,423✔
1996
      if (pBlock == NULL) {
32,515,120✔
1997
        continue;
×
1998
      }
1999

2000
      SSchemaWrapper* pSW = (SSchemaWrapper*)taosArrayGetP(pSchemas, i);
32,515,120✔
2001
      if (tqAddBlockDataToRsp(pBlock, pRsp, pSW, pTq->pVnode->config.tsdbCfg.precision) != 0){
32,519,653✔
2002
        tqError("vgId:%d, failed to add block to rsp msg", pTq->pVnode->config.vgId);
×
2003
        continue;
×
2004
      }
2005
      *totalRows += pBlock->info.rows;
32,513,522✔
2006
    }
2007

2008
    pRsp->blockNum++;
32,518,824✔
2009
  }
2010
  tqTrace("vgId:%d, process sub data success, response blocknum:%d, rows:%d", pTq->pVnode->config.vgId, pRsp->blockNum, *totalRows);
32,517,615✔
2011
END:
32,517,615✔
2012
  if (code != 0) {
33,888,264✔
2013
    tqError("%s failed at %d, failed to process sub data:%s", __FUNCTION__, lino, tstrerror(code));
×
2014
  }
2015
  taosArrayDestroyEx(pBlocks, (FDelete)blockDataFreeRes);
33,888,264✔
2016
  taosArrayDestroyP(pSchemas, (FDelete)tDeleteSchemaWrapper);
33,889,454✔
2017
}
33,889,053✔
2018

2019
static void tqPreProcessSubmitMsg(STqHandle* pHandle, const SMqPollReq* pRequest, SArray** rawList){
33,891,112✔
2020
  STqExecHandle* pExec = &pHandle->execHandle;
33,891,112✔
2021
  STqReader* pReader = pExec->pTqReader;
33,892,330✔
2022
  int32_t blockSz = taosArrayGetSize(pReader->submit.aSubmitTbData);
33,892,742✔
2023
  for (int32_t i = 0; i < blockSz; i++){
67,780,547✔
2024
    SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, i);
33,895,089✔
2025
    if (pSubmitTbData== NULL){
33,894,988✔
2026
      taosArrayDestroy(*rawList);
×
2027
      *rawList = NULL;
×
2028
      return;
×
2029
    }
2030

2031
    int64_t uid = pSubmitTbData->uid;
33,894,988✔
2032
    if (pRequest->rawData) {
33,895,400✔
2033
      if (taosHashGet(pRequest->uidHash, &uid, LONG_BYTES) != NULL) {
×
2034
        tqDebug("poll rawdata split,uid:%" PRId64 " is already exists", uid);
×
2035
        terrno = TSDB_CODE_TMQ_RAW_DATA_SPLIT;
×
2036
        return;
×
2037
      } else {
2038
        int32_t code = taosHashPut(pRequest->uidHash, &uid, LONG_BYTES, &uid, LONG_BYTES);
×
2039
        if (code != 0) {
×
2040
          tqError("failed to add table uid to hash, code:%d, uid:%" PRId64, code, uid);
×
2041
        }
2042
      }
2043
    }
2044

2045
    if (pSubmitTbData->pCreateTbReq == NULL){
33,894,576✔
2046
      continue;
33,000,860✔
2047
    }
2048

2049
    int64_t createTime = INT64_MAX;
892,162✔
2050
    int64_t *cTime = (int64_t*)taosHashGet(pHandle->tableCreateTimeHash, &uid, LONG_BYTES);
892,546✔
2051
    if (cTime != NULL){
894,082✔
2052
      createTime = *cTime;
778✔
2053
    } else{
2054
      createTime = metaGetTableCreateTime(pReader->pVnode->pMeta, uid, 1);
893,304✔
2055
      if (createTime != INT64_MAX){
891,768✔
2056
        int32_t code = taosHashPut(pHandle->tableCreateTimeHash, &uid, LONG_BYTES, &createTime, LONG_BYTES);
892,110✔
2057
        if (code != 0){
892,494✔
2058
          tqError("failed to add table create time to hash,code:%d, uid:%"PRId64, code, uid);
×
2059
        }
2060
      }
2061
    }
2062
    if (pSubmitTbData->ctimeMs > createTime){
892,930✔
2063
      tDestroySVSubmitCreateTbReq(pSubmitTbData->pCreateTbReq, TSDB_MSG_FLG_DECODE);
394✔
2064
      taosMemoryFreeClear(pSubmitTbData->pCreateTbReq);
394✔
2065
    } else if (pHandle->fetchMeta != ONLY_DATA){
893,688✔
2066
      taosArrayDestroy(*rawList);
4,696✔
2067
      *rawList = NULL;
4,696✔
2068
    }
2069
  }
2070
}
2071

2072
static int32_t tqTaosxScanLog(STQ* pTq, STqHandle* pHandle, SPackedData submit, SMqDataRsp* pRsp, int32_t* totalRows, const SMqPollReq* pRequest) {
33,895,906✔
2073
  int32_t code = 0;
33,895,906✔
2074
  int32_t lino = 0;
33,895,906✔
2075
  SDecoder decoder = {0};
33,895,906✔
2076
  STqExecHandle* pExec = &pHandle->execHandle;
33,895,906✔
2077
  STqReader* pReader = pExec->pTqReader;
33,895,906✔
2078
  SArray *rawList = NULL;
33,895,518✔
2079
  if (pRequest->rawData){
33,895,518✔
2080
    rawList = taosArrayInit(0, POINTER_BYTES);
×
2081
    TSDB_CHECK_NULL(rawList, code, lino, END, terrno);
×
2082
  }
2083
  code = tqReaderSetSubmitMsg(pReader, submit.msgStr, submit.msgLen, submit.ver, rawList, &decoder);
33,895,518✔
2084
  TSDB_CHECK_CODE(code, lino, END);
33,891,895✔
2085
  tqPreProcessSubmitMsg(pHandle, pRequest, &rawList);
33,891,895✔
2086
  // data could not contains same uid data in rawdata mode
2087
  if (pRequest->rawData != 0 && terrno == TSDB_CODE_TMQ_RAW_DATA_SPLIT){
33,884,728✔
2088
    goto END;
×
2089
  }
2090

2091
  // this submit data is metadata and previous data is rawdata
2092
  if (pRequest->rawData != 0 && *totalRows > 0 && pRsp->createTableNum == 0 && rawList == NULL){
33,886,292✔
2093
    tqDebug("poll rawdata split,vgId:%d, this wal submit data contains metadata and previous data is data", pTq->pVnode->config.vgId);
×
2094
    terrno = TSDB_CODE_TMQ_RAW_DATA_SPLIT;
×
2095
    goto END;
×
2096
  }
2097

2098
  // this submit data is rawdata and previous data is metadata
2099
  if (pRequest->rawData != 0 && pRsp->createTableNum > 0 && rawList != NULL){
33,885,084✔
2100
    tqDebug("poll rawdata split,vgId:%d, this wal submit data is data and previous data is metadata", pTq->pVnode->config.vgId);
×
2101
    terrno = TSDB_CODE_TMQ_RAW_DATA_SPLIT;
×
2102
    goto END;
×
2103
  }
2104

2105
  int32_t blockSz = taosArrayGetSize(pReader->submit.aSubmitTbData);
33,885,586✔
2106
  while (pReader->nextBlk < blockSz) {
67,786,002✔
2107
    tqProcessSubData(pTq, pHandle, pRsp, totalRows, pRequest, rawList, submit.ver);
33,895,037✔
2108
    pReader->nextBlk++;
33,887,871✔
2109
  }
2110

2111
END:
33,889,794✔
2112
  tDecoderClear(&decoder);
33,892,202✔
2113
  tqReaderClearSubmitMsg(pReader);
33,891,808✔
2114
  taosArrayDestroy(rawList);
33,884,462✔
2115
  if (code != 0){
33,887,328✔
2116
    tqError("%s failed at %d, failed to scan log:%s", __FUNCTION__, lino, tstrerror(code));
×
2117
  }
2118
  return code;
33,887,328✔
2119
}
2120

2121
static int32_t tqInitTaosxRsp(SMqDataRsp* pRsp, STqOffsetVal pOffset) {
119,305,160✔
2122
  int32_t code = TDB_CODE_SUCCESS;
119,305,160✔
2123
  int32_t lino = 0;
119,305,160✔
2124
  tqDebug("%s called", __FUNCTION__);
119,305,160✔
2125
  TSDB_CHECK_NULL(pRsp, code, lino, END, TSDB_CODE_INVALID_PARA);
119,305,561✔
2126
  tOffsetCopy(&pRsp->reqOffset, &pOffset);
119,305,561✔
2127
  tOffsetCopy(&pRsp->rspOffset, &pOffset);
119,304,356✔
2128

2129
  pRsp->withTbName = 1;
119,306,386✔
2130
  pRsp->withSchema = 1;
119,306,793✔
2131
  pRsp->blockData = taosArrayInit(0, sizeof(void*));
119,305,979✔
2132
  TSDB_CHECK_NULL(pRsp->blockData, code, lino, END, terrno);
119,302,328✔
2133

2134
  pRsp->blockDataLen = taosArrayInit(0, sizeof(int32_t));
119,301,096✔
2135
  TSDB_CHECK_NULL(pRsp->blockDataLen, code, lino, END, terrno);
119,303,544✔
2136

2137
  pRsp->blockTbName = taosArrayInit(0, sizeof(void*));
119,299,047✔
2138
  TSDB_CHECK_NULL(pRsp->blockTbName, code, lino, END, terrno);
119,295,863✔
2139

2140
  pRsp->blockSchema = taosArrayInit(0, sizeof(void*));
119,297,528✔
2141
  TSDB_CHECK_NULL(pRsp->blockSchema, code, lino, END, terrno);
119,294,638✔
2142

2143
END:
119,297,864✔
2144
  if (code != 0) {
119,297,864✔
2145
    tqError("%s failed at:%d, code:%s", __FUNCTION__, lino, tstrerror(code));
×
2146
    taosArrayDestroy(pRsp->blockData);
×
2147
    taosArrayDestroy(pRsp->blockDataLen);
×
2148
    taosArrayDestroy(pRsp->blockTbName);
×
2149
    taosArrayDestroy(pRsp->blockSchema);
×
2150
  }
2151
  return code;
119,294,256✔
2152
}
2153

2154
static int32_t tqExtractResetOffsetVal(STqOffsetVal* pOffsetVal, STQ* pTq, STqHandle* pHandle, const SMqPollReq* pRequest,
1,562,755✔
2155
                                     SRpcMsg* pMsg, bool* pBlockReturned) {
2156
  if (pOffsetVal == NULL || pTq == NULL || pHandle == NULL || pRequest == NULL || pMsg == NULL || pBlockReturned == NULL) {
1,562,755✔
2157
    return TSDB_CODE_INVALID_PARA;
×
2158
  }
2159
  uint64_t   consumerId = pRequest->consumerId;
1,564,695✔
2160
  STqOffset* pOffset = NULL;
1,564,695✔
2161
  int32_t    code = tqMetaGetOffset(pTq, pRequest->subKey, &pOffset);
1,564,695✔
2162
  int32_t    vgId = TD_VID(pTq->pVnode);
1,560,467✔
2163

2164
  *pBlockReturned = false;
1,560,879✔
2165
  // In this vnode, data has been polled by consumer for this topic, so let's continue from the last offset value.
2166
  if (code == 0) {
1,554,757✔
2167
    tOffsetCopy(pOffsetVal, &pOffset->val);
131,758✔
2168

2169
    char formatBuf[TSDB_OFFSET_LEN] = {0};
131,758✔
2170
    tFormatOffset(formatBuf, TSDB_OFFSET_LEN, pOffsetVal);
131,758✔
2171
    tqDebug("tmq poll: consumer:0x%" PRIx64
131,758✔
2172
                ", subkey %s, vgId:%d, existed offset found, offset reset to %s and continue.QID:0x%" PRIx64,
2173
            consumerId, pHandle->subKey, vgId, formatBuf, pRequest->reqId);
2174
    return 0;
131,758✔
2175
  } else {
2176
    // no poll occurs in this vnode for this topic, let's seek to the right offset value.
2177
    if (pRequest->reqOffset.type == TMQ_OFFSET__RESET_EARLIEST) {
1,422,999✔
2178
      if (pRequest->useSnapshot) {
1,401,142✔
2179
        tqDebug("tmq poll: consumer:0x%" PRIx64 ", subkey:%s, vgId:%d, (earliest) set offset to be snapshot",
1,220,888✔
2180
                consumerId, pHandle->subKey, vgId);
2181
        if (pHandle->fetchMeta) {
1,223,156✔
2182
          tqOffsetResetToMeta(pOffsetVal, 0);
2183
        } else {
2184
          SValue val = {0};
1,219,292✔
2185
          tqOffsetResetToData(pOffsetVal, 0, 0, val);
×
2186
        }
2187
      } else {
2188
        walRefFirstVer(pTq->pVnode->pWal, pHandle->pRef);
177,581✔
2189
        tqOffsetResetToLog(pOffsetVal, pHandle->pRef->refVer);
179,547✔
2190
      }
2191
    } else if (pRequest->reqOffset.type == TMQ_OFFSET__RESET_LATEST) {
29,831✔
2192
      walRefLastVer(pTq->pVnode->pWal, pHandle->pRef);
19,120✔
2193
      SMqDataRsp dataRsp = {0};
18,743✔
2194
      tqOffsetResetToLog(pOffsetVal, pHandle->pRef->refVer + 1);
19,120✔
2195

2196
      code = tqInitDataRsp(&dataRsp, *pOffsetVal);
18,743✔
2197
      if (code != 0) {
19,120✔
2198
        return code;
×
2199
      }
2200
      tqDebug("tmq poll: consumer:0x%" PRIx64 ", subkey %s, vgId:%d, (latest) offset reset to %" PRId64, consumerId,
19,120✔
2201
              pHandle->subKey, vgId, dataRsp.rspOffset.version);
2202
      code = tqSendDataRsp(pHandle, pMsg, pRequest, &dataRsp, (pRequest->rawData == 1) ? TMQ_MSG_TYPE__POLL_RAW_DATA_RSP : TMQ_MSG_TYPE__POLL_DATA_RSP, vgId);
19,120✔
2203
      tDeleteMqDataRsp(&dataRsp);
19,120✔
2204

2205
      *pBlockReturned = true;
19,120✔
2206
      return code;
19,120✔
2207
    } else if (pRequest->reqOffset.type == TMQ_OFFSET__RESET_NONE) {
11,088✔
2208
      tqError("tmq poll: subkey:%s, no offset committed for consumer:0x%" PRIx64
11,088✔
2209
                  " in vg %d, subkey %s, reset none failed",
2210
              pHandle->subKey, consumerId, vgId, pRequest->subKey);
2211
      return TSDB_CODE_TQ_NO_COMMITTED_OFFSET;
11,088✔
2212
    }
2213
  }
2214

2215
  return 0;
1,400,332✔
2216
}
2217

2218
static int32_t tqExtractDataAndRspForNormalSubscribe(STQ* pTq, STqHandle* pHandle, const SMqPollReq* pRequest,
68,488,932✔
2219
                                                   SRpcMsg* pMsg, STqOffsetVal* pOffset) {
2220
  int32_t    code = TDB_CODE_SUCCESS;
68,488,932✔
2221
  int32_t    lino = 0;
68,488,932✔
2222
  tqDebug("%s called", __FUNCTION__ );
68,488,932✔
2223
  uint64_t consumerId = pRequest->consumerId;
68,490,932✔
2224
  int32_t  vgId = TD_VID(pTq->pVnode);
68,493,315✔
2225
  terrno = 0;
68,494,517✔
2226

2227
  SMqDataRsp dataRsp = {0};
68,492,545✔
2228
  code = tqInitDataRsp(&dataRsp, *pOffset);
68,492,545✔
2229
  TSDB_CHECK_CODE(code, lino, end);
68,484,728✔
2230

2231
  code = qSetTaskId(pHandle->execHandle.task, consumerId, pRequest->reqId);
68,484,728✔
2232
  TSDB_CHECK_CODE(code, lino, end);
68,481,547✔
2233

2234
  code = tqScanData(pTq, pHandle, &dataRsp, pOffset, pRequest);
68,481,547✔
2235
  if (code != 0 && terrno != TSDB_CODE_WAL_LOG_NOT_EXIST) {
68,492,519✔
2236
    goto end;
1,208,330✔
2237
  }
2238

2239
  if (terrno == TSDB_CODE_TMQ_FETCH_TIMEOUT && dataRsp.blockNum == 0) {
67,284,189✔
2240
    dataRsp.timeout = true;
×
2241
  }
2242
  
2243
  // reqOffset represents the current date offset, may be changed if wal not exists
2244
  tOffsetCopy(&dataRsp.reqOffset, pOffset);
67,283,761✔
2245
  code = tqSendDataRsp(pHandle, pMsg, pRequest, &dataRsp, TMQ_MSG_TYPE__POLL_DATA_RSP, vgId);
67,285,009✔
2246

2247
end:
68,478,266✔
2248
  {
2249
    char buf[TSDB_OFFSET_LEN] = {0};
68,480,229✔
2250
    tFormatOffset(buf, TSDB_OFFSET_LEN, &dataRsp.rspOffset);
68,482,603✔
2251
    if (code != 0){
68,466,861✔
2252
      tqError("tmq poll: consumer:0x%" PRIx64 ", subkey %s, vgId:%d, rsp block:%d, rsp offset type:%s, QID:0x%" PRIx64 " error msg:%s, line:%d",
1,208,330✔
2253
              consumerId, pHandle->subKey, vgId, dataRsp.blockNum, buf, pRequest->reqId, tstrerror(code), lino);
2254
    } else {
2255
      tqDebug("tmq poll: consumer:0x%" PRIx64 ", subkey %s, vgId:%d, rsp block:%d, rsp offset type:%s, QID:0x%" PRIx64 " success",
67,258,531✔
2256
              consumerId, pHandle->subKey, vgId, dataRsp.blockNum, buf, pRequest->reqId);
2257
    }
2258

2259
    tDeleteMqDataRsp(&dataRsp);
68,487,347✔
2260
    return code;
68,488,581✔
2261
  }
2262
}
2263

2264
#define PROCESS_EXCLUDED_MSG(TYPE, DECODE_FUNC, DELETE_FUNC)                                               \
2265
  SDecoder decoder = {0};                                                                                  \
2266
  TYPE     req = {0};                                                                                      \
2267
  void*    data = POINTER_SHIFT(pHead->body, sizeof(SMsgHead));                                            \
2268
  int32_t  len = pHead->bodyLen - sizeof(SMsgHead);                                                        \
2269
  tDecoderInit(&decoder, data, len);                                                                       \
2270
  if (DECODE_FUNC(&decoder, &req) == 0 && (req.source & TD_REQ_FROM_TAOX) != 0) {                          \
2271
    tqDebug("tmq poll: consumer:0x%" PRIx64 " (epoch %d) iter log, jump meta for, vgId:%d offset %" PRId64 \
2272
            " msgType %d",                                                                                 \
2273
            pRequest->consumerId, pRequest->epoch, vgId, fetchVer, pHead->msgType);                        \
2274
    fetchVer++;                                                                                            \
2275
    DELETE_FUNC(&req);                                                                                     \
2276
    tDecoderClear(&decoder);                                                                               \
2277
    continue;                                                                                              \
2278
  }                                                                                                        \
2279
  DELETE_FUNC(&req);                                                                                       \
2280
  tDecoderClear(&decoder);
2281

2282
static void tqDeleteCommon(void* parm) {}
1,536✔
2283

2284
#define POLL_RSP_TYPE(pRequest,taosxRsp) \
2285
taosxRsp.createTableNum > 0 ? TMQ_MSG_TYPE__POLL_DATA_META_RSP : \
2286
(pRequest->rawData == 1 ? TMQ_MSG_TYPE__POLL_RAW_DATA_RSP : TMQ_MSG_TYPE__POLL_DATA_RSP)
2287

2288
static int32_t tqBuildBatchMeta(SMqBatchMetaRsp *btMetaRsp, int16_t type, int32_t bodyLen, void* body){
×
2289
  int32_t         code = 0;
×
2290

2291
  if (!btMetaRsp->batchMetaReq) {
×
2292
    btMetaRsp->batchMetaReq = taosArrayInit(4, POINTER_BYTES);
×
2293
    TQ_NULL_GO_TO_END(btMetaRsp->batchMetaReq);
×
2294
    btMetaRsp->batchMetaLen = taosArrayInit(4, sizeof(int32_t));
×
2295
    TQ_NULL_GO_TO_END(btMetaRsp->batchMetaLen);
×
2296
  }
2297

2298
  SMqMetaRsp tmpMetaRsp = {0};
×
2299
  tmpMetaRsp.resMsgType = type;
×
2300
  tmpMetaRsp.metaRspLen = bodyLen;
×
2301
  tmpMetaRsp.metaRsp = body;
×
2302
  uint32_t len = 0;
×
2303
  tEncodeSize(tEncodeMqMetaRsp, &tmpMetaRsp, len, code);
×
2304
  if (TSDB_CODE_SUCCESS != code) {
×
2305
    tqError("tmq extract meta from log, tEncodeMqMetaRsp error");
×
2306
    goto END;
×
2307
  }
2308
  int32_t tLen = sizeof(SMqRspHead) + len;
×
2309
  void*   tBuf = taosMemoryCalloc(1, tLen);
×
2310
  TQ_NULL_GO_TO_END(tBuf);
×
2311
  void*    metaBuff = POINTER_SHIFT(tBuf, sizeof(SMqRspHead));
×
2312
  SEncoder encoder = {0};
×
2313
  tEncoderInit(&encoder, metaBuff, len);
×
2314
  code = tEncodeMqMetaRsp(&encoder, &tmpMetaRsp);
×
2315
  tEncoderClear(&encoder);
×
2316

2317
  if (code < 0) {
×
2318
    tqError("tmq extract meta from log, tEncodeMqMetaRsp error");
×
2319
    goto END;
×
2320
  }
2321
  TQ_NULL_GO_TO_END (taosArrayPush(btMetaRsp->batchMetaReq, &tBuf));
×
2322
  TQ_NULL_GO_TO_END (taosArrayPush(btMetaRsp->batchMetaLen, &tLen));
×
2323

2324
END:
×
2325
  return code;
×
2326
}
2327

2328
static int32_t tqBuildCreateTbBatchReqBinary(SMqDataRsp *taosxRsp, void** pBuf, int32_t *len){
×
2329
  int32_t code = 0;
×
2330
  SVCreateTbBatchReq pReq = {0};
×
2331
  pReq.nReqs = taosArrayGetSize(taosxRsp->createTableReq);
×
2332
  pReq.pArray = taosArrayInit(1, sizeof(struct SVCreateTbReq));
×
2333
  TQ_NULL_GO_TO_END(pReq.pArray);
×
2334
  for (int i = 0; i < taosArrayGetSize(taosxRsp->createTableReq); i++){
×
2335
    void   *createTableReq = taosArrayGetP(taosxRsp->createTableReq, i);
×
2336
    TQ_NULL_GO_TO_END(taosArrayPush(pReq.pArray, createTableReq));
×
2337
  }
2338
  tEncodeSize(tEncodeSVCreateTbBatchReq, &pReq, *len, code);
×
2339
  if (code < 0) {
×
2340
    goto END;
×
2341
  }
2342
  *len += sizeof(SMsgHead);
×
2343
  *pBuf = taosMemoryMalloc(*len);
×
2344
  TQ_NULL_GO_TO_END(*pBuf);
×
2345
  SEncoder coder = {0};
×
2346
  tEncoderInit(&coder, POINTER_SHIFT(*pBuf, sizeof(SMsgHead)), *len);
×
2347
  code = tEncodeSVCreateTbBatchReq(&coder, &pReq);
×
2348
  tEncoderClear(&coder);
×
2349

2350
END:
×
2351
  taosArrayDestroy(pReq.pArray);
×
2352
  return code;
×
2353
}
2354

2355
#define SEND_BATCH_META_RSP \
2356
tqOffsetResetToLog(&btMetaRsp.rspOffset, fetchVer);\
2357
code = tqSendBatchMetaPollRsp(pHandle, pMsg, pRequest, &btMetaRsp, vgId);\
2358
goto END;
2359

2360
#define SEND_DATA_RSP \
2361
tqOffsetResetToLog(&taosxRsp.rspOffset, fetchVer);\
2362
code = tqSendDataRsp(pHandle, pMsg, pRequest, &taosxRsp, POLL_RSP_TYPE(pRequest, taosxRsp), vgId);\
2363
goto END;
2364

2365
static int32_t tqExtractDataAndRspForDbStbSubscribe(STQ* pTq, STqHandle* pHandle, const SMqPollReq* pRequest,
119,303,130✔
2366
                                                  SRpcMsg* pMsg, STqOffsetVal* offset) {
2367
  int32_t         vgId = TD_VID(pTq->pVnode);
119,303,130✔
2368
  SMqDataRsp      taosxRsp = {0};
119,304,777✔
2369
  SMqBatchMetaRsp btMetaRsp = {0};
119,301,937✔
2370
  int32_t         code = 0;
119,305,193✔
2371

2372
  TQ_ERR_GO_TO_END(tqInitTaosxRsp(&taosxRsp, *offset));
119,305,193✔
2373
  if (offset->type != TMQ_OFFSET__LOG) {
119,293,805✔
2374
    TQ_ERR_GO_TO_END(tqScanTaosx(pTq, pHandle, &taosxRsp, &btMetaRsp, offset, pRequest));
23,080✔
2375

2376
    if (taosArrayGetSize(btMetaRsp.batchMetaReq) > 0) {
23,080✔
2377
      code = tqSendBatchMetaPollRsp(pHandle, pMsg, pRequest, &btMetaRsp, vgId);
3,482✔
2378
      tqDebug("tmq poll: consumer:0x%" PRIx64 " subkey:%s vgId:%d, send meta offset type:%d,uid:%" PRId64 ",ts:%" PRId64,
3,482✔
2379
              pRequest->consumerId, pHandle->subKey, vgId, btMetaRsp.rspOffset.type, btMetaRsp.rspOffset.uid,btMetaRsp.rspOffset.ts);
2380
      goto END;
3,482✔
2381
    }
2382

2383
    tqDebug("taosx poll: consumer:0x%" PRIx64 " subkey:%s vgId:%d, send data blockNum:%d, offset type:%d,uid:%" PRId64",ts:%" PRId64,
19,598✔
2384
            pRequest->consumerId, pHandle->subKey, vgId, taosxRsp.blockNum, taosxRsp.rspOffset.type, taosxRsp.rspOffset.uid, taosxRsp.rspOffset.ts);
2385
    if (taosxRsp.blockNum > 0) {
19,598✔
2386
      code = tqSendDataRsp(pHandle, pMsg, pRequest, &taosxRsp, TMQ_MSG_TYPE__POLL_DATA_RSP, vgId);
17,281✔
2387
      goto END;
17,281✔
2388
    } else {
2389
      tOffsetCopy(offset, &taosxRsp.rspOffset);
2,317✔
2390
    }
2391
  }
2392

2393
  if (offset->type == TMQ_OFFSET__LOG) {
119,274,669✔
2394
    walReaderVerifyOffset(pHandle->pWalReader, offset);
119,284,016✔
2395
    int64_t fetchVer = offset->version;
119,273,473✔
2396

2397
    uint64_t st = taosGetTimestampMs();
119,269,876✔
2398
    int      totalRows = 0;
119,269,876✔
2399
    int32_t  totalMetaRows = 0;
119,267,073✔
2400
    while (1) {
33,695,783✔
2401
      int32_t savedEpoch = atomic_load_32(&pHandle->epoch);
152,962,856✔
2402
      if (savedEpoch > pRequest->epoch) {
152,952,746✔
2403
        tqError("tmq poll: consumer:0x%" PRIx64 " (epoch %d) iter log, savedEpoch error, vgId:%d offset %" PRId64,
×
2404
                pRequest->consumerId, pRequest->epoch, vgId, fetchVer);
2405
        code = TSDB_CODE_TQ_INTERNAL_ERROR;
×
2406
        goto END;
×
2407
      }
2408

2409
      if (tqFetchLog(pTq, pHandle, &fetchVer, pRequest->reqId) < 0) {
152,947,009✔
2410
        if (totalMetaRows > 0) {
119,072,170✔
2411
          SEND_BATCH_META_RSP
×
2412
        }
2413
        SEND_DATA_RSP
238,145,154✔
2414
      }
2415

2416
      SWalCont* pHead = &pHandle->pWalReader->pHead->head;
33,924,413✔
2417
      tqDebug("tmq poll: consumer:0x%" PRIx64 " (epoch %d) iter log, vgId:%d offset %" PRId64 " msgType %s",
33,924,825✔
2418
              pRequest->consumerId, pRequest->epoch, vgId, fetchVer, TMSG_INFO(pHead->msgType));
2419

2420
      // process meta
2421
      if (pHead->msgType != TDMT_VND_SUBMIT) {
33,925,237✔
2422
        if (totalRows > 0) {
28,919✔
2423
          SEND_DATA_RSP
12,196✔
2424
        }
2425

2426
        if ((pRequest->sourceExcluded & TD_REQ_FROM_TAOX) != 0) {
22,821✔
2427
          if (pHead->msgType == TDMT_VND_CREATE_TABLE) {
1,536✔
2428
            PROCESS_EXCLUDED_MSG(SVCreateTbBatchReq, tDecodeSVCreateTbBatchReq, tDeleteSVCreateTbBatchReq)
×
2429
          } else if (pHead->msgType == TDMT_VND_ALTER_TABLE) {
1,536✔
2430
            PROCESS_EXCLUDED_MSG(SVAlterTbReq, tDecodeSVAlterTbReq, destroyAlterTbReq)
×
2431
          } else if (pHead->msgType == TDMT_VND_CREATE_STB || pHead->msgType == TDMT_VND_ALTER_STB) {
3,072✔
2432
            PROCESS_EXCLUDED_MSG(SVCreateStbReq, tDecodeSVCreateStbReq, tqDeleteCommon)
1,536✔
2433
          } else if (pHead->msgType == TDMT_VND_DELETE) {
×
2434
            PROCESS_EXCLUDED_MSG(SDeleteRes, tDecodeDeleteRes, tqDeleteCommon)
×
2435
          }
2436
        }
2437

2438
        tqDebug("fetch meta msg, ver:%" PRId64 ", vgId:%d, type:%s, enable batch meta:%d", pHead->version, vgId,
22,821✔
2439
                TMSG_INFO(pHead->msgType), pRequest->enableBatchMeta);
2440
        if (!pRequest->enableBatchMeta && !pRequest->useSnapshot) {
22,821✔
2441
          SMqMetaRsp metaRsp = {0};
22,821✔
2442
          tqOffsetResetToLog(&metaRsp.rspOffset, fetchVer + 1);
22,821✔
2443
          metaRsp.resMsgType = pHead->msgType;
22,821✔
2444
          metaRsp.metaRspLen = pHead->bodyLen;
22,821✔
2445
          metaRsp.metaRsp = pHead->body;
22,821✔
2446
          code = tqSendMetaPollRsp(pHandle, pMsg, pRequest, &metaRsp, vgId);
22,821✔
2447
          goto END;
22,821✔
2448
        }
2449
        code = tqBuildBatchMeta(&btMetaRsp, pHead->msgType, pHead->bodyLen, pHead->body);
×
2450
        fetchVer++;
×
2451
        if (code != 0){
×
2452
          goto END;
×
2453
        }
2454
        totalMetaRows++;
×
2455
        if ((taosArrayGetSize(btMetaRsp.batchMetaReq) >= pRequest->minPollRows) || (taosGetTimestampMs() - st > pRequest->timeout)) {
×
2456
          SEND_BATCH_META_RSP
×
2457
        }
2458
        continue;
×
2459
      }
2460

2461
      if (totalMetaRows > 0 && pHandle->fetchMeta != ONLY_META) {
33,895,906✔
2462
        SEND_BATCH_META_RSP
×
2463
      }
2464

2465
      // process data
2466
      SPackedData submit = {
101,686,530✔
2467
          .msgStr = POINTER_SHIFT(pHead->body, sizeof(SSubmitReq2Msg)),
33,895,906✔
2468
          .msgLen = pHead->bodyLen - sizeof(SSubmitReq2Msg),
33,895,906✔
2469
          .ver = pHead->version,
33,895,106✔
2470
      };
2471

2472
      TQ_ERR_GO_TO_END(tqTaosxScanLog(pTq, pHandle, submit, &taosxRsp, &totalRows, pRequest));
33,895,518✔
2473

2474
      if (pHandle->fetchMeta == ONLY_META && taosArrayGetSize(taosxRsp.createTableReq) > 0){
33,881,198✔
2475
        int32_t len = 0;
×
2476
        void *pBuf = NULL;
×
2477
        code = tqBuildCreateTbBatchReqBinary(&taosxRsp, &pBuf, &len);
×
2478
        if (code == 0){
×
2479
          code = tqBuildBatchMeta(&btMetaRsp, TDMT_VND_CREATE_TABLE, len, pBuf);
×
2480
        }
2481
        taosMemoryFree(pBuf);
×
2482
        for (int i = 0; i < taosArrayGetSize(taosxRsp.createTableReq); i++) {
×
2483
          void* pCreateTbReq = taosArrayGetP(taosxRsp.createTableReq, i);
×
2484
          if (pCreateTbReq != NULL) {
×
2485
            tDestroySVSubmitCreateTbReq(pCreateTbReq, TSDB_MSG_FLG_DECODE);
×
2486
          }
2487
          taosMemoryFree(pCreateTbReq);
×
2488
        }
2489
        taosArrayDestroy(taosxRsp.createTableReq);
×
2490
        taosxRsp.createTableReq = NULL;
×
2491
        fetchVer++;
×
2492
        if (code != 0){
×
2493
          goto END;
×
2494
        }
2495
        totalMetaRows++;
×
2496
        if ((taosArrayGetSize(btMetaRsp.batchMetaReq) >= pRequest->minPollRows) ||
×
2497
            (taosGetTimestampMs() - st > pRequest->timeout) ||
×
2498
            (!pRequest->enableBatchMeta && !pRequest->useSnapshot)) {
×
2499
          SEND_BATCH_META_RSP
×
2500
        }
2501
        continue;
×
2502
      }
2503

2504
      if ((pRequest->rawData == 0 && totalRows >= pRequest->minPollRows) ||
33,891,856✔
2505
          (taosGetTimestampMs() - st > pRequest->timeout) ||
33,699,869✔
2506
          (pRequest->rawData != 0 && (taosArrayGetSize(taosxRsp.blockData) > pRequest->minPollRows ||
33,699,889✔
2507
                                      terrno == TSDB_CODE_TMQ_RAW_DATA_SPLIT))) {
×
2508
        if (terrno == TSDB_CODE_TMQ_RAW_DATA_SPLIT){
192,300✔
2509
          terrno = 0;
×
2510
        } else{
2511
          fetchVer++;
184,529✔
2512
        }
2513
        SEND_DATA_RSP
369,470✔
2514
      } else {
2515
        fetchVer++;
33,695,783✔
2516
      }
2517
    }
2518
  }
2519

2520
END:
119,262,019✔
2521
  if (code != 0){
119,270,153✔
2522
    tqError("tmq poll: tqTaosxScanLog error. consumerId:0x%" PRIx64 ", in vgId:%d, subkey %s", pRequest->consumerId, vgId,
×
2523
            pRequest->subKey);
2524
  }
2525
  tDeleteMqBatchMetaRsp(&btMetaRsp);
119,270,153✔
2526
  tDeleteSTaosxRsp(&taosxRsp);
119,246,304✔
2527
  return code;
119,226,741✔
2528
}
2529

2530
int32_t tqExtractDataForMq(STQ* pTq, STqHandle* pHandle, const SMqPollReq* pRequest, SRpcMsg* pMsg) {
187,827,154✔
2531
  if (pTq == NULL || pHandle == NULL || pRequest == NULL || pMsg == NULL) {
187,827,154✔
2532
    return TSDB_CODE_TMQ_INVALID_MSG;
×
2533
  }
2534
  int32_t      code = 0;
187,829,132✔
2535
  STqOffsetVal reqOffset = {0};
187,829,132✔
2536
  tOffsetCopy(&reqOffset, &pRequest->reqOffset);
187,829,132✔
2537

2538
  // reset the offset if needed
2539
  if (IS_OFFSET_RESET_TYPE(pRequest->reqOffset.type)) {
187,824,612✔
2540
    bool blockReturned = false;
1,564,695✔
2541
    code = tqExtractResetOffsetVal(&reqOffset, pTq, pHandle, pRequest, pMsg, &blockReturned);
1,564,695✔
2542
    if (code != 0) {
1,563,137✔
2543
      goto END;
11,088✔
2544
    }
2545

2546
    // empty block returned, quit
2547
    if (blockReturned) {
1,552,049✔
2548
      goto END;
19,120✔
2549
    }
2550
  } else if (reqOffset.type == 0) {  // use the consumer specified offset
186,253,521✔
2551
    uError("req offset type is 0");
×
2552
    code = TSDB_CODE_TMQ_INVALID_MSG;
×
2553
    goto END;
×
2554
  }
2555

2556
  if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
187,787,224✔
2557
    code = tqExtractDataAndRspForNormalSubscribe(pTq, pHandle, pRequest, pMsg, &reqOffset);
68,491,753✔
2558
  } else {
2559
    code = tqExtractDataAndRspForDbStbSubscribe(pTq, pHandle, pRequest, pMsg, &reqOffset);
119,305,983✔
2560
  }
2561

2562
END:
187,728,038✔
2563
  if (code != 0){
187,745,711✔
2564
    uError("failed to extract data for mq, msg:%s", tstrerror(code));
1,219,418✔
2565
  }
2566
  tOffsetDestroy(&reqOffset);
187,745,711✔
2567
  return code;
187,799,303✔
2568
}
2569

2570
static int32_t tqSendBatchMetaPollRsp(STqHandle* pHandle, const SRpcMsg* pMsg, const SMqPollReq* pReq,
3,482✔
2571
                               const SMqBatchMetaRsp* pRsp, int32_t vgId) {
2572
  if (pHandle == NULL || pMsg == NULL || pReq == NULL || pRsp == NULL) {
3,482✔
2573
    return TSDB_CODE_TMQ_INVALID_MSG;
×
2574
  }
2575
  int32_t len = 0;
3,482✔
2576
  int32_t code = 0;
3,482✔
2577
  tEncodeSize(tEncodeMqBatchMetaRsp, pRsp, len, code);
3,482✔
2578
  if (code < 0) {
3,482✔
2579
    return TAOS_GET_TERRNO(code);
×
2580
  }
2581
  int32_t tlen = sizeof(SMqRspHead) + len;
3,482✔
2582
  void*   buf = rpcMallocCont(tlen);
3,482✔
2583
  if (buf == NULL) {
3,482✔
2584
    return TAOS_GET_TERRNO(terrno);
×
2585
  }
2586

2587
  int64_t sver = 0, ever = 0;
3,482✔
2588
  walReaderValidVersionRange(pHandle->execHandle.pTqReader->pWalReader, &sver, &ever);
3,482✔
2589
  tqInitMqRspHead(buf, TMQ_MSG_TYPE__POLL_BATCH_META_RSP, pReq->epoch, pReq->consumerId, sver, ever);
3,482✔
2590

2591
  void* abuf = POINTER_SHIFT(buf, sizeof(SMqRspHead));
3,482✔
2592

2593
  SEncoder encoder = {0};
3,482✔
2594
  tEncoderInit(&encoder, abuf, len);
3,482✔
2595
  code = tEncodeMqBatchMetaRsp(&encoder, pRsp);
3,482✔
2596
  tEncoderClear(&encoder);
3,482✔
2597
  if (code < 0) {
3,482✔
2598
    rpcFreeCont(buf);
×
2599
    return TAOS_GET_TERRNO(code);
×
2600
  }
2601
  SRpcMsg resp = {.info = pMsg->info, .pCont = buf, .contLen = tlen, .code = 0};
3,482✔
2602

2603
  tmsgSendRsp(&resp);
3,482✔
2604
  tqDebug("vgId:%d, from consumer:0x%" PRIx64 " (epoch %d) send rsp, res msg type: batch meta, size:%ld offset type:%d",
3,482✔
2605
          vgId, pReq->consumerId, pReq->epoch, taosArrayGetSize(pRsp->batchMetaReq), pRsp->rspOffset.type);
2606

2607
  return 0;
3,482✔
2608
}
2609

2610
static int32_t tqSendMetaPollRsp(STqHandle* pHandle, const SRpcMsg* pMsg, const SMqPollReq* pReq, const SMqMetaRsp* pRsp,
22,821✔
2611
                          int32_t vgId) {
2612
  if (pHandle == NULL || pMsg == NULL || pReq == NULL || pRsp == NULL) {
22,821✔
2613
    return TSDB_CODE_TMQ_INVALID_MSG;
×
2614
  }
2615
  int32_t len = 0;
22,821✔
2616
  int32_t code = 0;
22,821✔
2617
  tEncodeSize(tEncodeMqMetaRsp, pRsp, len, code);
22,821✔
2618
  if (code < 0) {
22,821✔
2619
    return TAOS_GET_TERRNO(code);
×
2620
  }
2621
  int32_t tlen = sizeof(SMqRspHead) + len;
22,821✔
2622
  void*   buf = rpcMallocCont(tlen);
22,821✔
2623
  if (buf == NULL) {
22,821✔
2624
    return TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
2625
  }
2626

2627
  int64_t sver = 0, ever = 0;
22,821✔
2628
  walReaderValidVersionRange(pHandle->execHandle.pTqReader->pWalReader, &sver, &ever);
22,821✔
2629
  tqInitMqRspHead(buf, TMQ_MSG_TYPE__POLL_META_RSP, pReq->epoch, pReq->consumerId, sver, ever);
22,821✔
2630

2631
  void* abuf = POINTER_SHIFT(buf, sizeof(SMqRspHead));
22,821✔
2632

2633
  SEncoder encoder = {0};
22,821✔
2634
  tEncoderInit(&encoder, abuf, len);
22,821✔
2635
  code = tEncodeMqMetaRsp(&encoder, pRsp);
22,821✔
2636
  tEncoderClear(&encoder);
22,821✔
2637
  if (code < 0) {
22,821✔
2638
    rpcFreeCont(buf);
×
2639
    return TAOS_GET_TERRNO(code);
×
2640
  }
2641

2642
  SRpcMsg resp = {.info = pMsg->info, .pCont = buf, .contLen = tlen, .code = 0};
22,821✔
2643

2644
  tmsgSendRsp(&resp);
22,821✔
2645
  tqDebug("vgId:%d, from consumer:0x%" PRIx64 " (epoch %d) send rsp, res msg type %d, offset type:%d", vgId,
22,821✔
2646
          pReq->consumerId, pReq->epoch, pRsp->resMsgType, pRsp->rspOffset.type);
2647

2648
  return 0;
22,821✔
2649
}
2650

2651

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