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

taosdata / TDengine / #5120

16 Sep 2026 01:31AM UTC coverage: 73.895% (+0.8%) from 73.081%
#5120

push

travis-ci

jbjia
test(coverage): sync from gitlab

313025 of 423608 relevant lines covered (73.89%)

71072916.02 hits per line

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

69.61
/source/dnode/vnode/src/vnd/vnodeQuery.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 "meta.h"
17
#include "tsdb.h"
18
#include "tutil.h"
19
#include "vnd.h"
20

21
#define VNODE_GET_LOAD_RESET_VALS(pVar, oVal, vType, tags)                                                    \
22
  do {                                                                                                        \
23
    int##vType##_t newVal = atomic_sub_fetch_##vType(&(pVar), (oVal));                                        \
24
    if (newVal < 0) {                                                                                         \
25
      vWarn("vgId:%d, %s, abnormal val:%" PRIi64 ", old val:%" PRIi64, TD_VID(pVnode), tags, newVal, (oVal)); \
26
    }                                                                                                         \
27
  } while (0)
28

29
int vnodeQueryOpen(SVnode *pVnode) {
1,555,154✔
30
  return qWorkerInit(NODE_TYPE_VNODE, TD_VID(pVnode), (void **)&pVnode->pQuery, &pVnode->msgCb);
1,555,154✔
31
}
32

33
void vnodeQueryPreClose(SVnode *pVnode) { qWorkerStopAllTasks((void *)pVnode->pQuery); }
1,555,084✔
34

35
void vnodeQueryClose(SVnode *pVnode) { qWorkerDestroy((void **)&pVnode->pQuery); }
1,555,520✔
36

37
int32_t fillTableColCmpr(SMetaReader *reader, SSchemaExt *pExt, int32_t numOfCol) {
65,991,695✔
38
  int8_t tblType = reader->me.type;
65,991,695✔
39
  if (withColCompress(tblType)) {
65,991,695✔
40
    SColCmprWrapper *p = &(reader->me.colCmpr);
51,519,884✔
41
    if (numOfCol != p->nCols) {
51,519,884✔
42
      vError("fillTableColCmpr table type:%d, col num:%d, col cmpr num:%d mismatch", tblType, numOfCol, p->nCols);
×
43
      return TSDB_CODE_APP_ERROR;
×
44
    }
45
    for (int i = 0; i < p->nCols; i++) {
531,848,384✔
46
      SColCmpr *pCmpr = &p->pColCmpr[i];
480,328,500✔
47
      pExt[i].colId = pCmpr->id;
480,328,500✔
48
      pExt[i].compress = pCmpr->alg;
480,328,500✔
49
    }
50
  }
51
  return 0;
65,608,417✔
52
}
53

54
void vnodeDebugTableMeta(STableMetaRsp *pMeta) {
29,018,067✔
55
  if (!(qDebugFlag & DEBUG_DEBUG)) {
29,018,067✔
56
    return;
18,957,244✔
57
  }
58

59
  qDebug("tbName:%s", pMeta->tbName);
10,060,820✔
60
  qDebug("stbName:%s", pMeta->stbName);
10,062,371✔
61
  qDebug("dbFName:%s", pMeta->dbFName);
10,062,371✔
62
  qDebug("dbId:%" PRId64, pMeta->dbId);
10,062,371✔
63
  qDebug("numOfTags:%d", pMeta->numOfTags);
10,062,371✔
64
  qDebug("numOfColumns:%d", pMeta->numOfColumns);
10,062,371✔
65
  qDebug("precision:%d", pMeta->precision);
10,062,371✔
66
  qDebug("tableType:%d", pMeta->tableType);
10,062,371✔
67
  qDebug("sversion:%d", pMeta->sversion);
10,062,371✔
68
  qDebug("tversion:%d", pMeta->tversion);
10,062,371✔
69
  qDebug("suid:%" PRIu64, pMeta->suid);
10,062,371✔
70
  qDebug("tuid:%" PRIu64, pMeta->tuid);
10,062,371✔
71
  qDebug("vgId:%d", pMeta->vgId);
10,062,371✔
72
  qDebug("sysInfo:%d", pMeta->sysInfo);
10,062,371✔
73
  if (pMeta->pSchemas) {
10,062,371✔
74
    for (int32_t i = 0; i < (pMeta->numOfColumns + pMeta->numOfTags); ++i) {
104,702,424✔
75
      SSchema *pSchema = pMeta->pSchemas + i;
94,639,198✔
76
      qDebug("%d col/tag: type:%d, flags:%d, colId:%d, bytes:%d, name:%s", i, pSchema->type, pSchema->flags,
94,639,198✔
77
             pSchema->colId, pSchema->bytes, pSchema->name);
78
    }
79
  }
80
}
81

82
int32_t fillTableColRef(SMetaReader *reader, SColRef *pRef, int32_t numOfCol) {
15,914,954✔
83
  int8_t tblType = reader->me.type;
15,914,954✔
84
  if (hasColRef(tblType)) {
15,914,954✔
85
    SColRefWrapper *p = &(reader->me.colRef);
15,915,117✔
86
    if (numOfCol != p->nCols) {
15,915,117✔
87
      vError("fillTableColRef table type:%d, col num:%d, col cmpr num:%d mismatch", tblType, numOfCol, p->nCols);
×
88
      return TSDB_CODE_APP_ERROR;
×
89
    }
90
    for (int i = 0; i < p->nCols; i++) {
240,532,728✔
91
      SColRef *pColRef = &p->pColRef[i];
224,617,611✔
92
      pRef[i].hasRef = pColRef->hasRef;
224,617,611✔
93
      pRef[i].id = pColRef->id;
224,617,611✔
94
      if(pRef[i].hasRef) {
224,617,611✔
95
        pRef[i].refType = pColRef->refType;
183,217,669✔
96
        tstrncpy(pRef[i].refSourceName, pColRef->refSourceName, TSDB_EXT_SOURCE_NAME_LEN);
183,217,669✔
97
        tstrncpy(pRef[i].refSchemaName, pColRef->refSchemaName, TSDB_EXT_SOURCE_SCHEMA_LEN);
183,217,669✔
98
        tstrncpy(pRef[i].refDbName, pColRef->refDbName, TSDB_DB_NAME_LEN);
183,217,669✔
99
        tstrncpy(pRef[i].refTableName, pColRef->refTableName, TSDB_TABLE_NAME_LEN);
183,217,669✔
100
        tstrncpy(pRef[i].refColName, pColRef->refColName, TSDB_COL_NAME_LEN);
183,217,669✔
101
        if (pColRef->tagCondLen > 0 && pColRef->tagCondJson) {
183,217,669✔
102
          pRef[i].tagCondLen = pColRef->tagCondLen;
219,152✔
103
          pRef[i].tagCondJson = taosStrdup(pColRef->tagCondJson);
219,152✔
104
        } else {
105
          pRef[i].tagCondLen = 0;
182,998,517✔
106
          pRef[i].tagCondJson = NULL;
182,998,517✔
107
        }
108
      }
109
    }
110
  }
111
  return 0;
15,914,918✔
112
}
113

114
int32_t fillTableTagRef(SMetaReader *reader, SColRef *pRef, int32_t numOfTagRefs) {
631,996✔
115
  int8_t tblType = reader->me.type;
631,996✔
116
  if (hasTagRef(tblType)) {
631,996✔
117
    SColRefWrapper *p = &(reader->me.colRef);
631,996✔
118
    if (numOfTagRefs != p->nTagRefs) {
631,996✔
119
      vError("fillTableTagRef table type:%d, tag ref num:%d, expected:%d mismatch", tblType, numOfTagRefs, p->nTagRefs);
×
120
      return TSDB_CODE_APP_ERROR;
×
121
    }
122
    for (int i = 0; i < p->nTagRefs; i++) {
2,107,286✔
123
      SColRef *pTagRef = &p->pTagRef[i];
1,475,290✔
124
      pRef[i].hasRef = pTagRef->hasRef;
1,475,290✔
125
      pRef[i].id = pTagRef->id;
1,475,290✔
126
      if (pRef[i].hasRef) {
1,475,290✔
127
        tstrncpy(pRef[i].refDbName, pTagRef->refDbName, TSDB_DB_NAME_LEN);
1,171,504✔
128
        tstrncpy(pRef[i].refTableName, pTagRef->refTableName, TSDB_TABLE_NAME_LEN);
1,171,504✔
129
        tstrncpy(pRef[i].refColName, pTagRef->refColName, TSDB_COL_NAME_LEN);
1,171,504✔
130
      }
131
    }
132
  }
133
  return 0;
631,996✔
134
}
135

136
int32_t vnodeGetTableMeta(SVnode *pVnode, SRpcMsg *pMsg, bool direct) {
32,727,979✔
137
  STableInfoReq  infoReq = {0};
32,727,979✔
138
  STableMetaRsp  metaRsp = {0};
32,728,261✔
139
  SMetaReader    mer1 = {0};
32,727,813✔
140
  SMetaReader    mer2 = {0};
32,727,755✔
141
  char           tableFName[TSDB_TABLE_FNAME_LEN];
32,727,291✔
142
  bool           reqTbUid = false;
32,728,149✔
143
  SRpcMsg        rpcMsg = {0};
32,728,149✔
144
  int32_t        code = 0;
32,728,149✔
145
  int32_t        rspLen = 0;
32,728,149✔
146
  void          *pRsp = NULL;
32,728,149✔
147
  SSchemaWrapper schema = {0};
32,728,149✔
148
  SSchemaWrapper schemaTag = {0};
32,728,149✔
149
  uint8_t        autoCreateCtb = 0;
32,728,149✔
150

151
  // decode req
152
  if (tDeserializeSTableInfoReq(pMsg->pCont, pMsg->contLen, &infoReq) != 0) {
32,728,149✔
153
    code = terrno;
×
154
    goto _exit4;
×
155
  }
156
  autoCreateCtb = infoReq.autoCreateCtb;
32,727,537✔
157

158
  if (infoReq.option == REQ_OPT_TBUID) reqTbUid = true;
32,727,537✔
159
  metaRsp.dbId = pVnode->config.dbId;
32,727,537✔
160
  tstrncpy(metaRsp.tbName, infoReq.tbName, TSDB_TABLE_NAME_LEN);
32,727,537✔
161
  (void)memcpy(metaRsp.dbFName, infoReq.dbFName, sizeof(metaRsp.dbFName));
32,727,537✔
162

163
  if (!reqTbUid) {
32,727,537✔
164
    (void)snprintf(tableFName, TSDB_TABLE_FNAME_LEN, "%s.%s", infoReq.dbFName, infoReq.tbName);
32,661,925✔
165
    if (pVnode->mounted) tTrimMountPrefix(tableFName);
32,661,925✔
166
    code = vnodeValidateTableHash(pVnode, tableFName);
32,661,925✔
167
    if (code) {
32,662,401✔
168
      goto _exit4;
×
169
    }
170
  }
171

172
  // query meta
173
  metaReaderDoInit(&mer1, pVnode->pMeta, META_READER_LOCK, infoReq.txnId);
32,728,013✔
174
  if (reqTbUid) {
32,726,932✔
175
    SET_ERRNO(0);
64,852✔
176
    uint64_t tbUid = taosStr2UInt64(infoReq.tbName, NULL, 10);
64,852✔
177
    if (ERRNO == ERANGE || tbUid == 0) {
64,852✔
178
      code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
965✔
179
      goto _exit3;
965✔
180
    }
181
    SMetaReader mr3 = {0};
63,887✔
182
    metaReaderDoInit(&mr3, ((SVnode *)pVnode)->pMeta, META_READER_NOLOCK, infoReq.txnId);
63,887✔
183
    if ((code = metaReaderGetTableEntryByUid(&mr3, tbUid)) < 0) {
63,887✔
184
      metaReaderClear(&mr3);
22,761✔
185
      TAOS_CHECK_GOTO(code, NULL, _exit3);
22,761✔
186
    }
187
    tstrncpy(metaRsp.tbName, mr3.me.name, TSDB_TABLE_NAME_LEN);
41,126✔
188
    metaReaderClear(&mr3);
41,126✔
189
    TAOS_CHECK_GOTO(metaGetTableEntryByName(&mer1, metaRsp.tbName), NULL, _exit3);
41,126✔
190
  } else if (metaGetTableEntryByName(&mer1, infoReq.tbName) < 0) {
32,662,080✔
191
    code = terrno;
3,684,033✔
192
    goto _exit3;
3,684,135✔
193
  }
194

195
  metaRsp.tableType = mer1.me.type;
29,017,710✔
196
  metaRsp.vgId = TD_VID(pVnode);
29,017,710✔
197
  metaRsp.tuid = mer1.me.uid;
29,017,710✔
198
  metaRsp.isAudit = pVnode->config.isAudit ? 1 : 0;
29,017,710✔
199

200
  switch (mer1.me.type) {
29,017,710✔
201
    case TSDB_SUPER_TABLE: {
1,466,938✔
202
      tstrncpy(metaRsp.stbName, mer1.me.name, sizeof(metaRsp.stbName));
1,466,938✔
203
      schema = mer1.me.stbEntry.schemaRow;
1,466,938✔
204
      schemaTag = mer1.me.stbEntry.schemaTag;
1,466,938✔
205
      metaRsp.suid = mer1.me.uid;
1,466,938✔
206
      metaRsp.virtualStb = TABLE_IS_VIRTUAL(mer1.me.flags);
1,466,938✔
207
      metaRsp.ownerId = mer1.me.stbEntry.ownerId;
1,466,938✔
208
      metaRsp.secLvl = mer1.me.stbEntry.securityLevel;
1,466,938✔
209
      break;
1,466,938✔
210
    }
211
    case TSDB_CHILD_TABLE:
6,718,773✔
212
    case TSDB_VIRTUAL_CHILD_TABLE:{
213
      metaReaderDoInit(&mer2, pVnode->pMeta, META_READER_NOLOCK, infoReq.txnId);
6,718,773✔
214
      if (metaReaderGetTableEntryByUid(&mer2, mer1.me.ctbEntry.suid) < 0) goto _exit2;
6,719,001✔
215

216
      tstrncpy(metaRsp.stbName, mer2.me.name, sizeof(metaRsp.stbName));
6,719,195✔
217
      metaRsp.suid = mer2.me.uid;
6,719,195✔
218
      metaRsp.ownerId = mer2.me.stbEntry.ownerId;  // child table inherits ownerId from stb
6,719,195✔
219
      metaRsp.secLvl = mer2.me.stbEntry.securityLevel;  // child table inherits secLvl from stb
6,719,195✔
220
      schema = mer2.me.stbEntry.schemaRow;
6,719,195✔
221
      schemaTag = mer2.me.stbEntry.schemaTag;
6,719,195✔
222
      break;
6,719,195✔
223
    }
224
    case TSDB_NORMAL_TABLE:
6,388,247✔
225
      metaRsp.secLvl = pVnode->config.securityLevel;  // normal table inherits secLvl from vnode config
6,388,247✔
226
    case TSDB_VIRTUAL_NORMAL_TABLE: {
20,831,999✔
227
      schema = mer1.me.ntbEntry.schemaRow;
20,831,999✔
228
      schemaTag = mer1.me.ntbEntry.schemaTag;  // normal/virtual-normal tables may own tags
20,831,999✔
229
      metaRsp.ownerId = mer1.me.ntbEntry.ownerId;
20,831,999✔
230
      break;
20,831,999✔
231
    }
232
    default: {
×
233
      vError("vnodeGetTableMeta get invalid table type:%d", mer1.me.type);
×
234
      goto _exit3;
×
235
    }
236
  }
237

238
  metaRsp.numOfTags = schemaTag.nCols;
29,018,132✔
239
  metaRsp.numOfColumns = schema.nCols;
29,018,132✔
240
  metaRsp.precision = pVnode->config.tsdbCfg.precision;
29,018,132✔
241
  metaRsp.sversion = schema.version;
29,018,132✔
242
  metaRsp.tversion = schemaTag.version;
29,018,132✔
243
  metaRsp.pSchemas = (SSchema *)taosMemoryMalloc(sizeof(SSchema) * (metaRsp.numOfColumns + metaRsp.numOfTags));
29,018,132✔
244
  metaRsp.pSchemaExt = (SSchemaExt *)taosMemoryCalloc(metaRsp.numOfColumns, sizeof(SSchemaExt));
29,016,872✔
245
  if (NULL == metaRsp.pSchemas || NULL == metaRsp.pSchemaExt) {
29,017,141✔
246
    code = terrno;
×
247
    goto _exit;
×
248
  }
249
  (void)memcpy(metaRsp.pSchemas, schema.pSchema, sizeof(SSchema) * schema.nCols);
29,017,588✔
250
  if (schemaTag.nCols) {
29,017,588✔
251
    (void)memcpy(metaRsp.pSchemas + schema.nCols, schemaTag.pSchema, sizeof(SSchema) * schemaTag.nCols);
8,197,118✔
252
  }
253
  if (metaRsp.pSchemaExt) {
29,017,588✔
254
    SMetaReader *pReader =
29,021,508✔
255
        (mer1.me.type == TSDB_CHILD_TABLE || mer1.me.type == TSDB_VIRTUAL_CHILD_TABLE) ? &mer2 : &mer1;
29,017,929✔
256
    code = fillTableColCmpr(pReader, metaRsp.pSchemaExt, metaRsp.numOfColumns);
29,017,929✔
257
    if (code < 0) {
29,018,160✔
258
      goto _exit;
×
259
    }
260
    for (int32_t i = 0; i < metaRsp.numOfColumns; i++) {
410,128,081✔
261
      metaRsp.pSchemaExt[i].colId = schema.pSchema[i].colId;
381,109,921✔
262
      if (pReader->me.pExtSchemas) {
381,109,921✔
263
        metaRsp.pSchemaExt[i].typeMod = pReader->me.pExtSchemas[i].typeMod;
7,772,822✔
264
      }
265
    }
266
  } else {
267
    code = TSDB_CODE_OUT_OF_MEMORY;
×
268
    goto _exit;
×
269
  }
270
  if (hasColRef(mer1.me.type)) {
29,018,160✔
271
    metaRsp.rversion = mer1.me.colRef.version;
15,914,807✔
272
    metaRsp.pColRefs = (SColRef*)taosMemoryCalloc(metaRsp.numOfColumns, sizeof(SColRef));
15,914,807✔
273
    if (metaRsp.pColRefs) {
15,914,954✔
274
      code = fillTableColRef(&mer1, metaRsp.pColRefs, metaRsp.numOfColumns);
15,914,954✔
275
      if (code < 0) {
15,915,276✔
276
        goto _exit;
×
277
      }
278
    }
279
    metaRsp.numOfColRefs = metaRsp.numOfColumns;
15,915,276✔
280

281
    // Fill tag references
282
    if (mer1.me.colRef.nTagRefs > 0) {
15,915,276✔
283
      metaRsp.pTagRefs = (SColRef*)taosMemoryCalloc(mer1.me.colRef.nTagRefs, sizeof(SColRef));
631,996✔
284
      if (metaRsp.pTagRefs) {
631,996✔
285
        code = fillTableTagRef(&mer1, metaRsp.pTagRefs, mer1.me.colRef.nTagRefs);
631,996✔
286
        if (code < 0) {
631,996✔
287
          taosMemoryFreeClear(metaRsp.pTagRefs);
×
288
          goto _exit;
×
289
        }
290
      } else {
291
        code = terrno;
×
292
        goto _exit;
×
293
      }
294
      metaRsp.numOfTagRefs = mer1.me.colRef.nTagRefs;
631,996✔
295
    } else {
296
      metaRsp.pTagRefs = NULL;
15,283,280✔
297
      metaRsp.numOfTagRefs = 0;
15,283,280✔
298
    }
299
  } else {
300
    metaRsp.pColRefs = NULL;
13,103,005✔
301
    metaRsp.numOfColRefs = 0;
13,103,005✔
302
    metaRsp.pTagRefs = NULL;
13,103,005✔
303
    metaRsp.numOfTagRefs = 0;
13,103,005✔
304
  }
305

306
  // Fill series
307
  if (mer1.me.series.nSeries > 0) {
29,018,281✔
308
    metaRsp.numOfSeries = mer1.me.series.nSeries;
21,720✔
309
    metaRsp.pSeries = taosMemoryCalloc(mer1.me.series.nSeries, sizeof(SSeriesEntry));
21,720✔
310
    if (metaRsp.pSeries) {
21,720✔
311
      for (int32_t i = 0; i < mer1.me.series.nSeries; i++) {
116,580✔
312
        SSeriesEntry *src = &mer1.me.series.pSeries[i];
94,860✔
313
        SSeriesEntry *dst = &metaRsp.pSeries[i];
94,860✔
314
        tstrncpy(dst->alias, src->alias, TSDB_COL_NAME_LEN);
94,860✔
315
        tstrncpy(dst->sourceName, src->sourceName, TSDB_EXT_SOURCE_NAME_LEN);
94,860✔
316
        tstrncpy(dst->dbName, src->dbName, TSDB_DB_NAME_LEN);
94,860✔
317
        tstrncpy(dst->measurementName, src->measurementName, TSDB_TABLE_NAME_LEN);
94,860✔
318
        dst->tagCondLen = src->tagCondLen;
94,860✔
319
        dst->tagCondJson = src->tagCondJson ? taosStrdup(src->tagCondJson) : NULL;
94,860✔
320
      }
321
    } else {
322
      code = terrno;
×
323
      goto _exit;
×
324
    }
325
  } else {
326
    metaRsp.numOfSeries = 0;
28,996,561✔
327
    metaRsp.pSeries = NULL;
28,996,561✔
328
  }
329

330
  vnodeDebugTableMeta(&metaRsp);
29,018,281✔
331

332
  // encode and send response
333
  rspLen = tSerializeSTableMetaRsp(NULL, 0, &metaRsp);
29,020,121✔
334
  if (rspLen < 0) {
29,017,886✔
335
    code = terrno;
×
336
    goto _exit;
×
337
  }
338

339
  if (direct) {
29,017,886✔
340
    pRsp = rpcMallocCont(rspLen);
21,266,724✔
341
  } else {
342
    pRsp = taosMemoryCalloc(1, rspLen);
7,751,162✔
343
  }
344

345
  if (pRsp == NULL) {
29,018,959✔
346
    code = terrno;
×
347
    goto _exit;
×
348
  }
349

350
  rspLen = tSerializeSTableMetaRsp(pRsp, rspLen, &metaRsp);
29,018,959✔
351
  if (rspLen < 0) {
29,019,006✔
352
    code = terrno;
×
353
    goto _exit;
×
354
  }
355

356
_exit:
29,018,665✔
357
  tFreeSTableMetaRsp(&metaRsp);
29,019,712✔
358
_exit2:
29,017,815✔
359
  metaReaderClear(&mer2);
29,018,825✔
360
_exit3:
32,727,220✔
361
  metaReaderClear(&mer1);
32,727,497✔
362
_exit4:
32,727,236✔
363
  rpcMsg.info = pMsg->info;
32,728,525✔
364
  rpcMsg.pCont = pRsp;
32,728,525✔
365
  rpcMsg.contLen = rspLen;
32,728,525✔
366
  rpcMsg.code = code;
32,728,525✔
367
  rpcMsg.msgType = direct ? pMsg->msgType + 1 : pMsg->msgType;
32,728,525✔
368

369
  if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST && autoCreateCtb == 1) {
32,728,525✔
370
    code = TSDB_CODE_SUCCESS;
1,517,029✔
371
  }
372

373
  if (code) {
32,728,477✔
374
    qError("vgId:%d, get table %s meta with %" PRIu8 " failed cause of %s", pVnode->config.vgId, infoReq.tbName,
2,190,979✔
375
           infoReq.option, tstrerror(code));
376
  }
377

378
  if (direct) {
32,726,066✔
379
    tmsgSendRsp(&rpcMsg);
21,848,883✔
380
  } else {
381
    *pMsg = rpcMsg;
10,877,183✔
382
  }
383

384
  return code;
32,726,066✔
385
}
386

387
int32_t vnodeGetTableCfg(SVnode *pVnode, SRpcMsg *pMsg, bool direct) {
36,980,000✔
388
  STableCfgReq   cfgReq = {0};
36,980,000✔
389
  STableCfgRsp   cfgRsp = {0};
36,980,000✔
390
  SMetaReader    mer1 = {0};
36,980,000✔
391
  SMetaReader    mer2 = {0};
36,980,000✔
392
  char           tableFName[TSDB_TABLE_FNAME_LEN];
36,980,000✔
393
  SRpcMsg        rpcMsg = {0};
36,980,000✔
394
  int32_t        code = 0;
36,980,000✔
395
  int32_t        rspLen = 0;
36,980,000✔
396
  void          *pRsp = NULL;
36,980,000✔
397
  SSchemaWrapper schema = {0};
36,980,000✔
398
  SSchemaWrapper schemaTag = {0};
36,980,000✔
399

400
  // decode req
401
  if (tDeserializeSTableCfgReq(pMsg->pCont, pMsg->contLen, &cfgReq) != 0) {
36,980,000✔
402
    code = terrno;
×
403
    goto _exit;
×
404
  }
405

406
  tstrncpy(cfgRsp.tbName, cfgReq.tbName, TSDB_TABLE_NAME_LEN);
36,980,000✔
407
  (void)memcpy(cfgRsp.dbFName, cfgReq.dbFName, sizeof(cfgRsp.dbFName));
36,980,000✔
408

409
  (void)snprintf(tableFName, TSDB_TABLE_FNAME_LEN, "%s.%s", cfgReq.dbFName, cfgReq.tbName);
36,980,000✔
410
  if (pVnode->mounted) tTrimMountPrefix(tableFName);
36,980,000✔
411
  code = vnodeValidateTableHash(pVnode, tableFName);
36,980,000✔
412
  if (code) {
36,980,000✔
413
    goto _exit;
×
414
  }
415

416
  // query meta
417
  metaReaderDoInit(&mer1, pVnode->pMeta, META_READER_LOCK, cfgReq.txnId);
36,980,000✔
418

419
  if (metaGetTableEntryByName(&mer1, cfgReq.tbName) < 0) {
36,980,000✔
420
    code = terrno;
6,594✔
421
    goto _exit;
6,594✔
422
  }
423

424
  cfgRsp.tableType = mer1.me.type;
36,973,406✔
425
  cfgRsp.isAudit = pVnode->config.isAudit ? 1 : 0;
36,973,406✔
426
  cfgRsp.secureDelete = pVnode->config.secureDelete;
36,973,406✔
427

428
  if (mer1.me.type == TSDB_SUPER_TABLE) {
36,973,406✔
429
    code = TSDB_CODE_VND_HASH_MISMATCH;
×
430
    goto _exit;
×
431
  } else if (mer1.me.type == TSDB_CHILD_TABLE || mer1.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
36,593,714✔
432
    metaReaderDoInit(&mer2, pVnode->pMeta, META_READER_NOLOCK, cfgReq.txnId);
36,940,360✔
433
    if (metaReaderGetTableEntryByUid(&mer2, mer1.me.ctbEntry.suid) < 0) goto _exit;
36,940,360✔
434

435
    tstrncpy(cfgRsp.stbName, mer2.me.name, TSDB_TABLE_NAME_LEN);
36,940,360✔
436
    schema = mer2.me.stbEntry.schemaRow;
36,940,360✔
437
    schemaTag = mer2.me.stbEntry.schemaTag;
36,940,360✔
438
    cfgRsp.ownerId = mer2.me.stbEntry.ownerId;        // child table inherits ownerId from stb
36,940,360✔
439
    cfgRsp.securityLevel = mer2.me.stbEntry.securityLevel;  // child table inherits secLvl from stb
36,940,360✔
440
    cfgRsp.ttl = mer1.me.ctbEntry.ttlDays;
36,940,360✔
441
    cfgRsp.commentLen = mer1.me.ctbEntry.commentLen;
36,940,360✔
442
    if (mer1.me.ctbEntry.commentLen > 0) {
36,940,360✔
443
      cfgRsp.pComment = taosStrdup(mer1.me.ctbEntry.comment);
×
444
      if (NULL == cfgRsp.pComment) {
×
445
        code = terrno;
×
446
        goto _exit;
×
447
      }
448
    }
449
    STag *pTag = (STag *)mer1.me.ctbEntry.pTags;
36,940,360✔
450
    cfgRsp.tagsLen = pTag->len;
36,940,360✔
451
    cfgRsp.pTags = taosMemoryMalloc(cfgRsp.tagsLen);
36,940,360✔
452
    if (NULL == cfgRsp.pTags) {
36,940,360✔
453
      code = terrno;
×
454
      goto _exit;
×
455
    }
456
    (void)memcpy(cfgRsp.pTags, pTag, cfgRsp.tagsLen);
36,940,360✔
457
  } else if (mer1.me.type == TSDB_NORMAL_TABLE || mer1.me.type == TSDB_VIRTUAL_NORMAL_TABLE) {
33,046✔
458
    schema = mer1.me.ntbEntry.schemaRow;
33,046✔
459
    schemaTag = mer1.me.ntbEntry.schemaTag;  // normal/virtual-normal tables may own tags
33,046✔
460
    cfgRsp.ttl = mer1.me.ntbEntry.ttlDays;
33,046✔
461
    cfgRsp.ownerId = mer1.me.ntbEntry.ownerId;
33,046✔
462
    cfgRsp.securityLevel = mer1.me.type == TSDB_NORMAL_TABLE ? pVnode->config.securityLevel : 0;
33,046✔
463
    cfgRsp.commentLen = mer1.me.ntbEntry.commentLen;
33,046✔
464
    if (mer1.me.ntbEntry.commentLen > 0) {
33,046✔
465
      cfgRsp.pComment = taosStrdup(mer1.me.ntbEntry.comment);
×
466
      if (NULL == cfgRsp.pComment) {
×
467
        code = terrno;
×
468
        goto _exit;
×
469
      }
470
    }
471
    // owned tag values: copy ntbEntry.pTags so the executor's local-tag reading path
472
    // (which reads cfgRsp.pTags) can project them. Mirrors the child-table branch above.
473
    if (mer1.me.ntbEntry.schemaTag.nCols > 0 && mer1.me.ntbEntry.pTags != NULL) {
33,046✔
474
      STag *pTag = (STag *)mer1.me.ntbEntry.pTags;
6,579✔
475
      cfgRsp.tagsLen = pTag->len;
6,579✔
476
      cfgRsp.pTags = taosMemoryMalloc(cfgRsp.tagsLen);
6,579✔
477
      if (NULL == cfgRsp.pTags) {
6,579✔
478
        code = terrno;
×
479
        goto _exit;
×
480
      }
481
      (void)memcpy(cfgRsp.pTags, pTag, cfgRsp.tagsLen);
6,579✔
482
    }
483
  } else {
484
    vError("vnodeGetTableCfg get invalid table type:%d", mer1.me.type);
×
485
    code = TSDB_CODE_APP_ERROR;
×
486
    goto _exit;
×
487
  }
488

489
  cfgRsp.numOfTags = schemaTag.nCols;
36,973,406✔
490
  cfgRsp.numOfColumns = schema.nCols;
36,973,406✔
491
  cfgRsp.virtualStb = false; // vnode don't have super table, so it's always false
36,973,406✔
492
  cfgRsp.pSchemas = (SSchema *)taosMemoryMalloc(sizeof(SSchema) * (cfgRsp.numOfColumns + cfgRsp.numOfTags));
36,973,406✔
493
  cfgRsp.pSchemaExt = (SSchemaExt *)taosMemoryCalloc(cfgRsp.numOfColumns, sizeof(SSchemaExt));
36,973,406✔
494
  cfgRsp.pColRefs = (SColRef *)taosMemoryCalloc(cfgRsp.numOfColumns, sizeof(SColRef));
36,973,406✔
495
  cfgRsp.numOfTagRefs = 0;
36,973,406✔
496
  cfgRsp.pTagRefs = NULL;
36,973,406✔
497

498
  if (NULL == cfgRsp.pSchemas || NULL == cfgRsp.pSchemaExt || NULL == cfgRsp.pColRefs) {
36,973,406✔
499
    code = terrno;
×
500
    goto _exit;
×
501
  }
502
  (void)memcpy(cfgRsp.pSchemas, schema.pSchema, sizeof(SSchema) * schema.nCols);
36,973,406✔
503
  if (schemaTag.nCols) {
36,973,406✔
504
    (void)memcpy(cfgRsp.pSchemas + schema.nCols, schemaTag.pSchema, sizeof(SSchema) * schemaTag.nCols);
36,946,939✔
505
  }
506

507
  SMetaReader    *pReader = (mer1.me.type == TSDB_CHILD_TABLE || mer1.me.type == TSDB_VIRTUAL_CHILD_TABLE) ? &mer2 : &mer1;
36,973,406✔
508
  SColRefWrapper *pColRef = &mer1.me.colRef;
36,973,406✔
509

510
  if (withExtSchema(cfgRsp.tableType)) {
36,973,406✔
511
    code = fillTableColCmpr(pReader, cfgRsp.pSchemaExt, cfgRsp.numOfColumns);
36,973,406✔
512
    if (code < 0) {
36,973,406✔
513
      goto _exit;
×
514
    }
515

516
    for (int32_t i = 0; i < cfgRsp.numOfColumns; i++) {
356,568,327✔
517
      SSchemaExt *pSchExt = cfgRsp.pSchemaExt + i;
319,594,921✔
518
      pSchExt->colId = schema.pSchema[i].colId;
319,594,921✔
519
      if (pReader->me.pExtSchemas) {
319,594,921✔
520
        pSchExt->typeMod = pReader->me.pExtSchemas[i].typeMod;
56,526✔
521
      }
522
    }
523
  }
524

525
  cfgRsp.virtualStb = false;
36,973,406✔
526
  if (hasColRef(cfgRsp.tableType)) {
36,973,406✔
527
    for (int32_t i = 0; i < cfgRsp.numOfColumns; i++) {
257,394,613✔
528
      SColRef *pRef = &pColRef->pColRef[i];
231,928,393✔
529
      cfgRsp.pColRefs[i].hasRef = pRef->hasRef;
231,928,393✔
530
      cfgRsp.pColRefs[i].id = pRef->id;
231,928,393✔
531
      if (cfgRsp.pColRefs[i].hasRef) {
231,928,393✔
532
        cfgRsp.pColRefs[i].refType = pRef->refType;
145,872,931✔
533
        tstrncpy(cfgRsp.pColRefs[i].refSourceName, pRef->refSourceName, TSDB_EXT_SOURCE_NAME_LEN);
145,872,931✔
534
        tstrncpy(cfgRsp.pColRefs[i].refSchemaName, pRef->refSchemaName, TSDB_EXT_SOURCE_SCHEMA_LEN);
145,872,931✔
535
        tstrncpy(cfgRsp.pColRefs[i].refDbName, pRef->refDbName, TSDB_DB_NAME_LEN);
145,872,931✔
536
        tstrncpy(cfgRsp.pColRefs[i].refTableName, pRef->refTableName, TSDB_TABLE_NAME_LEN);
145,872,931✔
537
        tstrncpy(cfgRsp.pColRefs[i].refColName, pRef->refColName, TSDB_COL_NAME_LEN);
145,872,931✔
538
        if (pRef->tagCondJson) {
145,872,931✔
539
          cfgRsp.pColRefs[i].tagCondLen = pRef->tagCondLen;
1,704,483✔
540
          cfgRsp.pColRefs[i].tagCondJson = taosStrdup(pRef->tagCondJson);
1,704,483✔
541
        } else {
542
          cfgRsp.pColRefs[i].tagCondLen = 0;
144,168,448✔
543
          cfgRsp.pColRefs[i].tagCondJson = NULL;
144,168,448✔
544
        }
545
      }
546
    }
547

548
    cfgRsp.numOfTagRefs = pColRef->nTagRefs;
25,466,220✔
549
    if (cfgRsp.numOfTagRefs > 0) {
25,466,220✔
550
      if (NULL == pColRef->pTagRef) {
12,743,242✔
551
        code = TSDB_CODE_APP_ERROR;
×
552
        goto _exit;
×
553
      }
554
      cfgRsp.pTagRefs = (SColRef *)taosMemoryCalloc(cfgRsp.numOfTagRefs, sizeof(SColRef));
12,743,242✔
555
      if (NULL == cfgRsp.pTagRefs) {
12,743,242✔
556
        code = terrno;
×
557
        goto _exit;
×
558
      }
559

560
      for (int32_t i = 0; i < cfgRsp.numOfTagRefs; i++) {
50,783,229✔
561
        SColRef *pRef = &pColRef->pTagRef[i];
38,039,987✔
562
        cfgRsp.pTagRefs[i].hasRef = pRef->hasRef;
38,039,987✔
563
        cfgRsp.pTagRefs[i].id = pRef->id;
38,039,987✔
564
        if (cfgRsp.pTagRefs[i].hasRef) {
38,039,987✔
565
          cfgRsp.pTagRefs[i].refType = pRef->refType;
25,285,653✔
566
          tstrncpy(cfgRsp.pTagRefs[i].refSourceName, pRef->refSourceName, TSDB_EXT_SOURCE_NAME_LEN);
25,285,653✔
567
          tstrncpy(cfgRsp.pTagRefs[i].refSchemaName, pRef->refSchemaName, TSDB_EXT_SOURCE_SCHEMA_LEN);
25,285,653✔
568
          tstrncpy(cfgRsp.pTagRefs[i].refDbName, pRef->refDbName, TSDB_DB_NAME_LEN);
25,285,653✔
569
          tstrncpy(cfgRsp.pTagRefs[i].refTableName, pRef->refTableName, TSDB_TABLE_NAME_LEN);
25,285,653✔
570
          tstrncpy(cfgRsp.pTagRefs[i].refColName, pRef->refColName, TSDB_COL_NAME_LEN);
25,285,653✔
571
          if (pRef->tagCondJson) {
25,285,653✔
572
            cfgRsp.pTagRefs[i].tagCondLen = pRef->tagCondLen;
×
573
            cfgRsp.pTagRefs[i].tagCondJson = taosStrdup(pRef->tagCondJson);
×
574
            if (NULL == cfgRsp.pTagRefs[i].tagCondJson) {
×
575
              code = terrno;
×
576
              goto _exit;
×
577
            }
578
          }
579
        }
580
      }
581
    }
582
  }
583

584
  // populate series
585
  if (mer1.me.series.nSeries > 0) {
36,973,406✔
586
    cfgRsp.numOfSeries = mer1.me.series.nSeries;
134,421✔
587
    cfgRsp.pSeries = taosMemoryCalloc(mer1.me.series.nSeries, sizeof(SSeriesEntry));
134,421✔
588
    if (cfgRsp.pSeries) {
134,421✔
589
      for (int32_t i = 0; i < mer1.me.series.nSeries; i++) {
917,788✔
590
        SSeriesEntry *src = &mer1.me.series.pSeries[i];
783,367✔
591
        SSeriesEntry *dst = &cfgRsp.pSeries[i];
783,367✔
592
        tstrncpy(dst->alias, src->alias, TSDB_COL_NAME_LEN);
783,367✔
593
        tstrncpy(dst->sourceName, src->sourceName, TSDB_EXT_SOURCE_NAME_LEN);
783,367✔
594
        tstrncpy(dst->dbName, src->dbName, TSDB_DB_NAME_LEN);
783,367✔
595
        tstrncpy(dst->measurementName, src->measurementName, TSDB_TABLE_NAME_LEN);
783,367✔
596
        if (src->tagCondLen > 0) {
783,367✔
597
          dst->tagCondJson = taosStrdup(src->tagCondJson);
783,367✔
598
          dst->tagCondLen = src->tagCondLen;
783,367✔
599
        } else {
600
          dst->tagCondJson = NULL;
×
601
          dst->tagCondLen = 0;
×
602
        }
603
      }
604
    }
605
  }
606

607
  // encode and send response
608
  rspLen = tSerializeSTableCfgRsp(NULL, 0, &cfgRsp);
36,973,406✔
609
  if (rspLen < 0) {
36,973,406✔
610
    code = terrno;
×
611
    goto _exit;
×
612
  }
613

614
  if (direct) {
36,973,406✔
615
    pRsp = rpcMallocCont(rspLen);
36,937,490✔
616
  } else {
617
    pRsp = taosMemoryCalloc(1, rspLen);
35,916✔
618
  }
619

620
  if (pRsp == NULL) {
36,973,406✔
621
    code = terrno;
×
622
    goto _exit;
×
623
  }
624

625
  rspLen = tSerializeSTableCfgRsp(pRsp, rspLen, &cfgRsp);
36,973,406✔
626
  if (rspLen < 0) {
36,973,406✔
627
    code = terrno;
×
628
    goto _exit;
×
629
  }
630

631
_exit:
36,980,000✔
632
  rpcMsg.info = pMsg->info;
36,980,000✔
633
  rpcMsg.pCont = pRsp;
36,980,000✔
634
  rpcMsg.contLen = rspLen;
36,980,000✔
635
  rpcMsg.code = code;
36,980,000✔
636
  rpcMsg.msgType = pMsg->msgType;
36,980,000✔
637

638
  if (code) {
36,980,000✔
639
    qError("get table %s cfg failed cause of %s", cfgReq.tbName, tstrerror(code));
6,594✔
640
  }
641

642
  if (direct) {
36,980,000✔
643
    tmsgSendRsp(&rpcMsg);
36,944,084✔
644
  } else {
645
    *pMsg = rpcMsg;
35,916✔
646
  }
647

648
  tFreeSTableCfgRsp(&cfgRsp);
36,980,000✔
649
  metaReaderClear(&mer2);
36,980,000✔
650
  metaReaderClear(&mer1);
36,980,000✔
651
  return code;
36,980,000✔
652
}
653

654
static FORCE_INLINE void vnodeFreeSBatchRspMsg(void *p) {
655
  if (NULL == p) {
656
    return;
657
  }
658

659
  SBatchRspMsg *pRsp = (SBatchRspMsg *)p;
660
  rpcFreeCont(pRsp->msg);
661
}
662

663
int32_t vnodeGetBatchMeta(SVnode *pVnode, SRpcMsg *pMsg) {
17,655,192✔
664
  int32_t      code = 0;
17,655,192✔
665
  int32_t      rspSize = 0;
17,655,192✔
666
  SBatchReq    batchReq = {0};
17,655,192✔
667
  SBatchMsg   *req = NULL;
17,655,445✔
668
  SBatchRspMsg rsp = {0};
17,655,445✔
669
  SBatchRsp    batchRsp = {0};
17,645,451✔
670
  SRpcMsg      reqMsg = *pMsg;
17,646,004✔
671
  SRpcMsg      rspMsg = {0};
17,646,004✔
672
  void        *pRsp = NULL;
17,651,446✔
673

674
  if (tDeserializeSBatchReq(pMsg->pCont, pMsg->contLen, &batchReq)) {
17,651,446✔
675
    code = terrno;
×
676
    qError("tDeserializeSBatchReq failed");
×
677
    goto _exit;
×
678
  }
679

680
  int32_t msgNum = taosArrayGetSize(batchReq.pMsgs);
17,649,546✔
681
  if (msgNum >= MAX_META_MSG_IN_BATCH) {
17,651,857✔
682
    code = TSDB_CODE_INVALID_MSG;
×
683
    qError("too many msgs %d in vnode batch meta req", msgNum);
×
684
    goto _exit;
×
685
  }
686

687
  batchRsp.pRsps = taosArrayInit(msgNum, sizeof(SBatchRspMsg));
17,651,857✔
688
  if (NULL == batchRsp.pRsps) {
17,648,171✔
689
    code = terrno;
441✔
690
    qError("taosArrayInit %d SBatchRspMsg failed", msgNum);
×
691
    goto _exit;
×
692
  }
693

694
  for (int32_t i = 0; i < msgNum; ++i) {
38,051,929✔
695
    req = taosArrayGet(batchReq.pMsgs, i);
20,398,014✔
696
    if (req == NULL) {
20,390,583✔
697
      code = terrno;
×
698
      goto _exit;
×
699
    }
700

701
    reqMsg.msgType = req->msgType;
20,396,343✔
702
    reqMsg.pCont = req->msg;
20,396,343✔
703
    reqMsg.contLen = req->msgLen;
20,396,343✔
704

705
    switch (req->msgType) {
20,396,343✔
706
      case TDMT_VND_TABLE_META:
10,812,342✔
707
        // error code has been set into reqMsg, no need to handle it here.
708
        if (TSDB_CODE_SUCCESS != vnodeGetTableMeta(pVnode, &reqMsg, false)) {
10,812,342✔
709
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
1,585,194✔
710
        }
711
        break;
10,807,712✔
712
      case TDMT_VND_TABLE_NAME:
64,849✔
713
        // error code has been set into reqMsg, no need to handle it here.
714
        if (TSDB_CODE_SUCCESS != vnodeGetTableMeta(pVnode, &reqMsg, false)) {
64,849✔
715
          qWarn("vnodeGetBatchName failed, msgType:%d", req->msgType);
23,726✔
716
        }
717
        break;
64,849✔
718
      case TDMT_VND_TABLE_CFG:
35,916✔
719
        // error code has been set into reqMsg, no need to handle it here.
720
        if (TSDB_CODE_SUCCESS != vnodeGetTableCfg(pVnode, &reqMsg, false)) {
35,916✔
721
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
×
722
        }
723
        break;
35,916✔
724
      case TDMT_VND_VSUBTABLES_META:
9,483,236✔
725
        // error code has been set into reqMsg, no need to handle it here.
726
        if (TSDB_CODE_SUCCESS != vnodeGetVSubtablesMeta(pVnode, &reqMsg)) {
9,483,236✔
727
          qWarn("vnodeGetVSubtablesMeta failed, msgType:%d", req->msgType);
×
728
        }
729
        break;
9,388,977✔
730
      case TDMT_VND_VSTB_REF_DBS:
×
731
        // error code has been set into reqMsg, no need to handle it here.
732
        if (TSDB_CODE_SUCCESS != vnodeGetVStbRefDbs(pVnode, &reqMsg)) {
×
733
          qWarn("vnodeGetVStbRefDbs failed, msgType:%d", req->msgType);
×
734
        }
735
        break;
×
736
      case TDMT_VND_VTB_TAG_COND:
×
737
        // error code has been set into reqMsg, no need to handle it here.
738
        if (TSDB_CODE_SUCCESS != vnodeGetVTbTagCond(pVnode, &reqMsg, false)) {
×
739
          qWarn("vnodeGetVTbTagCond failed, msgType:%d", req->msgType);
×
740
        }
741
        break;
×
742
      default:
×
743
        qError("invalid req msgType %d", req->msgType);
×
744
        reqMsg.code = TSDB_CODE_INVALID_MSG;
×
745
        reqMsg.pCont = NULL;
×
746
        reqMsg.contLen = 0;
×
747
        break;
×
748
    }
749

750
    rsp.msgIdx = req->msgIdx;
20,402,171✔
751
    rsp.reqType = reqMsg.msgType;
20,402,171✔
752
    rsp.msgLen = reqMsg.contLen;
20,402,171✔
753
    rsp.rspCode = reqMsg.code;
20,402,171✔
754
    rsp.msg = reqMsg.pCont;
20,402,171✔
755

756
    if (NULL == taosArrayPush(batchRsp.pRsps, &rsp)) {
40,701,645✔
757
      qError("taosArrayPush failed");
×
758
      code = terrno;
×
759
      goto _exit;
×
760
    }
761
  }
762

763
  rspSize = tSerializeSBatchRsp(NULL, 0, &batchRsp);
17,653,915✔
764
  if (rspSize < 0) {
17,651,607✔
765
    qError("tSerializeSBatchRsp failed");
×
766
    code = terrno;
×
767
    goto _exit;
×
768
  }
769
  pRsp = rpcMallocCont(rspSize);
17,651,607✔
770
  if (pRsp == NULL) {
17,653,726✔
771
    qError("rpcMallocCont %d failed", rspSize);
×
772
    code = terrno;
×
773
    goto _exit;
×
774
  }
775
  if (tSerializeSBatchRsp(pRsp, rspSize, &batchRsp) < 0) {
17,653,726✔
776
    qError("tSerializeSBatchRsp %d failed", rspSize);
×
777
    code = terrno;
×
778
    goto _exit;
×
779
  }
780

781
_exit:
17,656,878✔
782

783
  rspMsg.info = pMsg->info;
17,656,728✔
784
  rspMsg.pCont = pRsp;
17,656,728✔
785
  rspMsg.contLen = rspSize;
17,656,728✔
786
  rspMsg.code = code;
17,656,728✔
787
  rspMsg.msgType = pMsg->msgType;
17,656,728✔
788

789
  if (code) {
17,656,728✔
790
    qError("vnd get batch meta failed cause of %s", tstrerror(code));
×
791
  }
792

793
  taosArrayDestroyEx(batchReq.pMsgs, tFreeSBatchReqMsg);
17,656,728✔
794
  taosArrayDestroyEx(batchRsp.pRsps, tFreeSBatchRspMsg);
17,655,619✔
795

796
  tmsgSendRsp(&rspMsg);
17,654,637✔
797

798
  return code;
17,657,390✔
799
}
800

801
#define VNODE_DO_META_QUERY(pVnode, cmd)                 \
802
  do {                                                   \
803
    (void)taosThreadRwlockRdlock(&(pVnode)->metaRWLock); \
804
    cmd;                                                 \
805
    (void)taosThreadRwlockUnlock(&(pVnode)->metaRWLock); \
806
  } while (0)
807

808
int32_t vnodeReadVSubtables(SReadHandle* pHandle, int64_t suid, SArray** ppRes) {
9,479,152✔
809
  int32_t                    code = TSDB_CODE_SUCCESS;
9,479,152✔
810
  int32_t                    line = 0;
9,481,284✔
811
  SMetaReader                mr = {0};
9,481,284✔
812
  bool                       readerInit = false;
9,466,201✔
813
  SVCTableRefCols*           pTb = NULL;
9,466,201✔
814
  int32_t                    refColsNum = 0;
9,466,323✔
815
  char                       tbFName[TSDB_TABLE_FNAME_LEN];
9,466,323✔
816
  SSHashObj*                 pSrcTbls = NULL;
9,470,666✔
817

818
  SArray *pList = taosArrayInit(10, sizeof(uint64_t));
9,470,666✔
819
  QUERY_CHECK_NULL(pList, code, line, _return, terrno);
9,474,447✔
820

821
  QUERY_CHECK_CODE(pHandle->api.metaFn.getChildTableList(pHandle->vnode, suid, pList), line, _return);
9,474,447✔
822

823
  size_t num = taosArrayGetSize(pList);
9,482,200✔
824
  *ppRes = taosArrayInit(num, POINTER_BYTES);
9,477,902✔
825
  QUERY_CHECK_NULL(*ppRes, code, line, _return, terrno);
9,484,087✔
826
  pSrcTbls = tSimpleHashInit(10, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
9,484,087✔
827
  QUERY_CHECK_NULL(pSrcTbls, code, line, _return, terrno);
9,479,529✔
828

829
  for (int32_t i = 0; i < num; ++i) {
56,704,054✔
830
    uint64_t* id = taosArrayGet(pList, i);
47,219,489✔
831
    QUERY_CHECK_NULL(id, code, line, _return, terrno);
47,180,287✔
832
    pHandle->api.metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pHandle->api.metaFn, pHandle->txnId);
47,180,287✔
833
    QUERY_CHECK_CODE(pHandle->api.metaReaderFn.getTableEntryByUid(&mr, *id), line, _return);
47,294,401✔
834
    readerInit = true;
54,296,814✔
835

836
    refColsNum = 0;
46,784,586✔
837
    for (int32_t j = 0; j < mr.me.colRef.nCols; j++) {
556,864,824✔
838
      if (mr.me.colRef.pColRef[j].hasRef) {
509,636,226✔
839
        refColsNum++;
291,153,239✔
840
      }
841
    }
842

843
    int32_t tagRefColsNum = 0;
46,784,586✔
844
    for (int32_t j = 0; j < mr.me.colRef.nTagRefs; j++) {
84,668,508✔
845
      if (mr.me.colRef.pTagRef[j].hasRef) {
37,439,910✔
846
        tagRefColsNum++;
24,969,148✔
847
      }
848
    }
849

850
    if (refColsNum <= 0 && tagRefColsNum <= 0) {
47,228,598✔
851
      pHandle->api.metaReaderFn.clearReader(&mr);
8,164,542✔
852
      readerInit = false;
8,164,542✔
853
      continue;
8,164,542✔
854
    }
855

856
    pTb = taosMemoryCalloc(1, (refColsNum + tagRefColsNum) * sizeof(SRefColInfo) + sizeof(*pTb));
39,064,056✔
857
    QUERY_CHECK_NULL(pTb, code, line, _return, terrno);
38,888,530✔
858

859
    pTb->uid = mr.me.uid;
38,888,530✔
860
    pTb->numOfColRefs = refColsNum;
38,888,530✔
861
    pTb->refCols = (SRefColInfo*)(pTb + 1);
38,888,530✔
862
    pTb->numOfTagRefs = tagRefColsNum;
38,888,530✔
863
    pTb->tagRefCols = pTb->refCols + refColsNum;
38,888,530✔
864

865
    refColsNum = 0;
38,888,530✔
866
    tSimpleHashClear(pSrcTbls);
38,888,530✔
867
    for (int32_t j = 0; j < mr.me.colRef.nCols; j++) {
418,586,946✔
868
      if (!mr.me.colRef.pColRef[j].hasRef) {
379,483,119✔
869
        continue;
88,146,012✔
870
      }
871

872
      pTb->refCols[refColsNum].colId = mr.me.colRef.pColRef[j].id;
291,337,107✔
873
      tstrncpy(pTb->refCols[refColsNum].refColName, mr.me.colRef.pColRef[j].refColName, TSDB_COL_NAME_LEN);
291,337,107✔
874
      tstrncpy(pTb->refCols[refColsNum].refTableName, mr.me.colRef.pColRef[j].refTableName, TSDB_TABLE_NAME_LEN);
291,337,107✔
875
      tstrncpy(pTb->refCols[refColsNum].refDbName, mr.me.colRef.pColRef[j].refDbName, TSDB_DB_NAME_LEN);
291,337,107✔
876
      tstrncpy(pTb->refCols[refColsNum].refSourceName, mr.me.colRef.pColRef[j].refSourceName, TSDB_EXT_SOURCE_NAME_LEN);
291,337,107✔
877

878
      snprintf(tbFName, sizeof(tbFName), "%s.%s", pTb->refCols[refColsNum].refDbName, pTb->refCols[refColsNum].refTableName);
291,337,107✔
879

880
      if (NULL == tSimpleHashGet(pSrcTbls, tbFName, strlen(tbFName))) {
291,337,107✔
881
        QUERY_CHECK_CODE(tSimpleHashPut(pSrcTbls, tbFName, strlen(tbFName), &code, sizeof(code)), line, _return);
55,949,240✔
882
      }
883

884
      refColsNum++;
291,285,650✔
885
    }
886

887
    // Fill tag refs
888
    tagRefColsNum = 0;
38,807,055✔
889
    for (int32_t j = 0; j < mr.me.colRef.nTagRefs; j++) {
76,635,964✔
890
      if (!mr.me.colRef.pTagRef[j].hasRef) {
37,498,007✔
891
        continue;
12,491,624✔
892
      }
893

894
      pTb->tagRefCols[tagRefColsNum].colId = mr.me.colRef.pTagRef[j].id;
25,006,383✔
895
      tstrncpy(pTb->tagRefCols[tagRefColsNum].refColName, mr.me.colRef.pTagRef[j].refColName, TSDB_COL_NAME_LEN);
25,006,383✔
896
      tstrncpy(pTb->tagRefCols[tagRefColsNum].refTableName, mr.me.colRef.pTagRef[j].refTableName, TSDB_TABLE_NAME_LEN);
25,006,383✔
897
      tstrncpy(pTb->tagRefCols[tagRefColsNum].refDbName, mr.me.colRef.pTagRef[j].refDbName, TSDB_DB_NAME_LEN);
25,006,383✔
898
      tstrncpy(pTb->tagRefCols[tagRefColsNum].refSourceName, mr.me.colRef.pTagRef[j].refSourceName, TSDB_EXT_SOURCE_NAME_LEN);
25,006,383✔
899

900
      snprintf(tbFName, sizeof(tbFName), "%s.%s", pTb->tagRefCols[tagRefColsNum].refDbName, pTb->tagRefCols[tagRefColsNum].refTableName);
25,006,383✔
901

902
      if (NULL == tSimpleHashGet(pSrcTbls, tbFName, strlen(tbFName))) {
25,006,383✔
903
        QUERY_CHECK_CODE(tSimpleHashPut(pSrcTbls, tbFName, strlen(tbFName), &code, sizeof(code)), line, _return);
576,063✔
904
      }
905

906
      tagRefColsNum++;
25,040,393✔
907
    }
908

909
    pTb->numOfSrcTbls = tSimpleHashGetSize(pSrcTbls);
39,137,957✔
910
    QUERY_CHECK_NULL(taosArrayPush(*ppRes, &pTb), code, line, _return, terrno);
77,944,035✔
911
    pTb = NULL;
39,073,744✔
912

913
    pHandle->api.metaReaderFn.clearReader(&mr);
39,073,744✔
914
    readerInit = false;
39,060,055✔
915
  }
916

917
_return:
9,484,565✔
918

919
  if (readerInit) {
9,389,995✔
920
    pHandle->api.metaReaderFn.clearReader(&mr);
×
921
  }
922

923
  taosArrayDestroy(pList);
9,490,471✔
924
  taosMemoryFree(pTb);
9,490,320✔
925
  tSimpleHashCleanup(pSrcTbls);
9,490,138✔
926

927
  if (code) {
9,489,865✔
928
    qError("%s failed since %s", __func__, tstrerror(code));
×
929
  }
930
  return code;
9,489,865✔
931
}
932

933
int32_t vnodeReadVStbRefDbs(SReadHandle* pHandle, int64_t suid, SArray** ppDbs, SArray** ppExtSources) {
×
934
  int32_t                    code = TSDB_CODE_SUCCESS;
×
935
  int32_t                    line = 0;
×
936
  SMetaReader                mr = {0};
×
937
  bool                       readerInit = false;
×
938
  SSHashObj*                 pDbNameHash = NULL;
×
939
  SSHashObj*                 pExtSourceHash = NULL;
×
940
  SArray*                    pList = NULL;
×
941

942
  pList = taosArrayInit(10, sizeof(uint64_t));
×
943
  QUERY_CHECK_NULL(pList, code, line, _return, terrno);
×
944

945
  *ppDbs = taosArrayInit(10, POINTER_BYTES);
×
946
  QUERY_CHECK_NULL(*ppDbs, code, line, _return, terrno)
×
947

948
  *ppExtSources = taosArrayInit(4, POINTER_BYTES);
×
949
  QUERY_CHECK_NULL(*ppExtSources, code, line, _return, terrno)
×
950
  
951
  // lookup in cache
952
  code = pHandle->api.metaFn.metaGetCachedRefDbs(pHandle->vnode, suid, *ppDbs);
×
953
  QUERY_CHECK_CODE(code, line, _return);
×
954

955
  if (taosArrayGetSize(*ppDbs) > 0) {
×
956
    // found in cache, also get ext sources from cache
957
    code = pHandle->api.metaFn.metaGetCachedExtSources(pHandle->vnode, suid, *ppExtSources);
×
958
    QUERY_CHECK_CODE(code, line, _return);
×
959
    goto _return;
×
960
  } else {
961
    code = pHandle->api.metaFn.getChildTableList(pHandle->vnode, suid, pList);
×
962
    QUERY_CHECK_CODE(code, line, _return);
×
963

964
    size_t num = taosArrayGetSize(pList);
×
965
    pDbNameHash = tSimpleHashInit(10, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
×
966
    QUERY_CHECK_NULL(pDbNameHash, code, line, _return, terrno);
×
967

968
    pExtSourceHash = tSimpleHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
×
969
    QUERY_CHECK_NULL(pExtSourceHash, code, line, _return, terrno);
×
970

971
    for (int32_t i = 0; i < num; ++i) {
×
972
      uint64_t* id = taosArrayGet(pList, i);
×
973
      QUERY_CHECK_NULL(id, code, line, _return, terrno);
×
974

975
      pHandle->api.metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pHandle->api.metaFn, pHandle->txnId);
×
976
      readerInit = true;
×
977

978
      code = pHandle->api.metaReaderFn.getTableEntryByUid(&mr, *id);
×
979
      QUERY_CHECK_CODE(code, line, _return);
×
980

981
      for (int32_t j = 0; j < mr.me.colRef.nCols; j++) {
×
982
        if (mr.me.colRef.pColRef[j].hasRef) {
×
983
          if (mr.me.colRef.pColRef[j].refSourceName[0] != '\0') {
×
984
            const char* srcName = mr.me.colRef.pColRef[j].refSourceName;
×
985
            if (NULL == tSimpleHashGet(pExtSourceHash, srcName, strlen(srcName))) {
×
986
              char *dup = taosStrdup(srcName);
×
987
              QUERY_CHECK_NULL(dup, code, line, _return, terrno);
×
988
              QUERY_CHECK_NULL(taosArrayPush(*ppExtSources, &dup), code, line, _return, terrno);
×
989
              code = tSimpleHashPut(pExtSourceHash, srcName, strlen(srcName), NULL, 0);
×
990
              QUERY_CHECK_CODE(code, line, _return);
×
991
            }
992
          } else {
993
            if (NULL == tSimpleHashGet(pDbNameHash, mr.me.colRef.pColRef[j].refDbName, strlen(mr.me.colRef.pColRef[j].refDbName))) {
×
994
              char *refDbName = taosStrdup(mr.me.colRef.pColRef[j].refDbName);
×
995
              QUERY_CHECK_NULL(refDbName, code, line, _return, terrno);
×
996
              QUERY_CHECK_NULL(taosArrayPush(*ppDbs, &refDbName), code, line, _return, terrno);
×
997
              code = tSimpleHashPut(pDbNameHash, refDbName, strlen(refDbName), NULL, 0);
×
998
              QUERY_CHECK_CODE(code, line, _return);
×
999
            }
1000
          }
1001
        }
1002
      }
1003

1004
      pHandle->api.metaReaderFn.clearReader(&mr);
×
1005
      readerInit = false;
×
1006
    }
1007

1008
    code = pHandle->api.metaFn.metaPutRefDbsToCache(pHandle->vnode, suid, *ppDbs);
×
1009
    QUERY_CHECK_CODE(code, line, _return);
×
1010

1011
    code = pHandle->api.metaFn.metaPutExtSourcesToCache(pHandle->vnode, suid, *ppExtSources);
×
1012
    QUERY_CHECK_CODE(code, line, _return);
×
1013
  }
1014

1015
_return:
×
1016

1017
  if (readerInit) {
×
1018
    pHandle->api.metaReaderFn.clearReader(&mr);
×
1019
  }
1020

1021
  taosArrayDestroy(pList);
×
1022
  tSimpleHashCleanup(pDbNameHash);
×
1023
  tSimpleHashCleanup(pExtSourceHash);
×
1024

1025
  if (code) {
×
1026
    qError("%s failed since %s", __func__, tstrerror(code));
×
1027
  }
1028
  return code;
×
1029
}
1030

1031
int32_t vnodeGetVSubtablesMeta(SVnode *pVnode, SRpcMsg *pMsg) {
9,481,326✔
1032
  int32_t        code = 0;
9,481,326✔
1033
  int32_t        rspSize = 0;
9,481,326✔
1034
  SVSubTablesReq req = {0};
9,481,326✔
1035
  SVSubTablesRsp rsp = {0};
9,468,555✔
1036
  SRpcMsg      rspMsg = {0};
9,461,167✔
1037
  void        *pRsp = NULL;
9,461,167✔
1038
  int32_t      line = 0;
9,461,167✔
1039

1040
  if (tDeserializeSVSubTablesReq(pMsg->pCont, pMsg->contLen, &req)) {
9,461,167✔
1041
    code = terrno;
×
1042
    qError("tDeserializeSVSubTablesReq failed");
×
1043
    goto _return;
×
1044
  }
1045

1046
  SReadHandle handle = {0};
9,483,181✔
1047
  handle.vnode = pVnode;
9,483,873✔
1048
  initStorageAPI(&handle.api);
9,483,873✔
1049

1050
  QUERY_CHECK_CODE(vnodeReadVSubtables(&handle, req.suid, &rsp.pTables), line, _return);
9,470,092✔
1051
  rsp.vgId = TD_VID(pVnode);
9,490,338✔
1052

1053
  rspSize = tSerializeSVSubTablesRsp(NULL, 0, &rsp);
9,490,338✔
1054
  if (rspSize < 0) {
9,487,506✔
1055
    code = rspSize;
×
1056
    qError("tSerializeSVSubTablesRsp failed, error:%d", rspSize);
×
1057
    goto _return;
×
1058
  }
1059
  pRsp = taosMemoryCalloc(1, rspSize);
9,487,506✔
1060
  if (pRsp == NULL) {
9,486,358✔
1061
    code = terrno;
×
1062
    qError("rpcMallocCont %d failed, error:%d", rspSize, terrno);
×
1063
    goto _return;
×
1064
  }
1065
  rspSize = tSerializeSVSubTablesRsp(pRsp, rspSize, &rsp);
9,486,358✔
1066
  if (rspSize < 0) {
9,490,651✔
1067
    code = rspSize;
×
1068
    qError("tSerializeSVSubTablesRsp failed, error:%d", rspSize);
×
1069
    goto _return;
×
1070
  }
1071

1072
_return:
9,490,651✔
1073

1074
  rspMsg.info = pMsg->info;
9,490,052✔
1075
  rspMsg.pCont = pRsp;
9,490,052✔
1076
  rspMsg.contLen = rspSize;
9,490,052✔
1077
  rspMsg.code = code;
9,490,052✔
1078
  rspMsg.msgType = pMsg->msgType;
9,490,052✔
1079

1080
  if (code) {
9,490,052✔
1081
    qError("vnd get virtual subtables failed cause of %s", tstrerror(code));
×
1082
  }
1083

1084
  *pMsg = rspMsg;
9,490,052✔
1085
  
1086
  tDestroySVSubTablesRsp(&rsp);
9,490,052✔
1087

1088
  //tmsgSendRsp(&rspMsg);
1089

1090
  return code;
9,488,896✔
1091
}
1092

1093
int32_t vnodeProcessCheckHasCtbReq(SVnode *pVnode, SRpcMsg *pMsg) {
49,416✔
1094
  SVCheckHasCtbReq req = {0};
49,416✔
1095
  int32_t          code = 0;
49,416✔
1096
  void            *pBuf = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
49,416✔
1097
  int32_t          bufLen = pMsg->contLen - sizeof(SMsgHead);
49,416✔
1098

1099
  code = tDeserializeSVCheckHasCtbReq(pBuf, bufLen, &req);
49,416✔
1100
  if (code != 0) {
49,416✔
1101
    vError("vgId:%d, failed to deserialize check-has-ctb req", TD_VID(pVnode));
×
1102
    goto _exit;
×
1103
  }
1104

1105
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, req.suid, 0, 0);
49,416✔
1106
  if (pCur == NULL) {
49,416✔
1107
    code = (terrno != 0) ? terrno : TSDB_CODE_OUT_OF_MEMORY;
×
1108
    vError("vgId:%d, failed to open ctb cursor for suid:%" PRId64 ", code:0x%x", TD_VID(pVnode), req.suid, code);
×
1109
  } else {
1110
    tb_uid_t id = metaCtbCursorNext(pCur);
49,416✔
1111
    metaCloseCtbCursor(pCur);
49,416✔
1112
    if (id != 0) {
49,416✔
1113
      code = TSDB_CODE_MND_VST_PARENT_HAS_VCT;
780✔
1114
    }
1115
  }
1116

1117
_exit:;
49,416✔
1118
  SRpcMsg rspMsg = {.info = pMsg->info, .code = code};
49,416✔
1119
  tmsgSendRsp(&rspMsg);
49,416✔
1120
  return 0;  // return 0 so fetch worker doesn't double-send response
49,416✔
1121
}
1122

1123
int32_t vnodeGetVStbRefDbs(SVnode *pVnode, SRpcMsg *pMsg) {
×
1124
  int32_t        code = 0;
×
1125
  int32_t        rspSize = 0;
×
1126
  SVStbRefDbsReq req = {0};
×
1127
  SVStbRefDbsRsp rsp = {0};
×
1128
  SRpcMsg        rspMsg = {0};
×
1129
  void          *pRsp = NULL;
×
1130
  int32_t        line = 0;
×
1131

1132
  if (tDeserializeSVStbRefDbsReq(pMsg->pCont, pMsg->contLen, &req)) {
×
1133
    code = terrno;
×
1134
    qError("tDeserializeSVSubTablesReq failed");
×
1135
    goto _return;
×
1136
  }
1137

1138
  SReadHandle handle = {0};
×
1139
  handle.vnode = pVnode;
×
1140
  initStorageAPI(&handle.api);
×
1141

1142
  code = vnodeReadVStbRefDbs(&handle, req.suid, &rsp.pDbs, &rsp.pExtSources);
×
1143
  QUERY_CHECK_CODE(code, line, _return);
×
1144
  rsp.vgId = TD_VID(pVnode);
×
1145

1146
  rspSize = tSerializeSVStbRefDbsRsp(NULL, 0, &rsp);
×
1147
  if (rspSize < 0) {
×
1148
    code = rspSize;
×
1149
    qError("tSerializeSVStbRefDbsRsp failed, error:%d", rspSize);
×
1150
    goto _return;
×
1151
  }
1152
  pRsp = taosMemoryCalloc(1, rspSize);
×
1153
  if (pRsp == NULL) {
×
1154
    code = terrno;
×
1155
    qError("rpcMallocCont %d failed, error:%d", rspSize, terrno);
×
1156
    goto _return;
×
1157
  }
1158
  rspSize = tSerializeSVStbRefDbsRsp(pRsp, rspSize, &rsp);
×
1159
  if (rspSize < 0) {
×
1160
    code = rspSize;
×
1161
    qError("tSerializeSVStbRefDbsRsp failed, error:%d", rspSize);
×
1162
    goto _return;
×
1163
  }
1164

1165
_return:
×
1166

1167
  rspMsg.info = pMsg->info;
×
1168
  rspMsg.pCont = pRsp;
×
1169
  rspMsg.contLen = rspSize;
×
1170
  rspMsg.code = code;
×
1171
  rspMsg.msgType = pMsg->msgType;
×
1172

1173
  if (code) {
×
1174
    qError("vnd get virtual stb ref db failed cause of %s", tstrerror(code));
×
1175
  }
1176

1177
  *pMsg = rspMsg;
×
1178

1179
  tDestroySVStbRefDbsRsp(&rsp);
×
1180

1181
  return code;
×
1182
}
1183

1184
int32_t vnodeGetVTbTagCond(SVnode *pVnode, SRpcMsg *pMsg, bool direct) {
1,516,429✔
1185
  int32_t      code = 0;
1,516,429✔
1186
  int32_t      line = 0;
1,516,429✔
1187
  int32_t      rspSize = 0;
1,516,429✔
1188
  SVTagCondReq req = {0};
1,516,429✔
1189
  SVTagCondRsp rsp = {0};
1,516,429✔
1190
  SRpcMsg      rspMsg = {0};
1,516,429✔
1191
  void        *pRsp = NULL;
1,516,429✔
1192
  SMetaReader  mer = {0};
1,516,429✔
1193

1194
  if (tDeserializeSVTagCondReq(pMsg->pCont, pMsg->contLen, &req)) {
1,516,429✔
1195
    code = terrno;
×
1196
    qError("tDeserializeSVTagCondReq failed");
×
1197
    goto _return;
×
1198
  }
1199

1200
  metaReaderDoInit(&mer, pVnode->pMeta, META_READER_LOCK, 0);
1,516,429✔
1201
  code = metaReaderGetTableEntryByUid(&mer, req.uid);
1,516,429✔
1202
  QUERY_CHECK_CODE(code, line, _return);
1,516,429✔
1203

1204
  rsp.pEntries = taosArrayInit(8, sizeof(SVTagCondEntry));
1,516,429✔
1205
  QUERY_CHECK_NULL(rsp.pEntries, code, line, _return, terrno);
1,516,429✔
1206

1207
  if (hasColRef(mer.me.type)) {
1,516,429✔
1208
    SColRefWrapper *p = &(mer.me.colRef);
1,258,093✔
1209
    for (int32_t i = 0; i < p->nCols; ++i) {
25,726,237✔
1210
      SColRef *pColRef = &p->pColRef[i];
24,209,808✔
1211
      if (pColRef->hasRef && pColRef->tagCondLen > 0 && pColRef->tagCondJson != NULL) {
24,209,808✔
1212
        SVTagCondEntry entry = {0};
888,363✔
1213
        entry.colId = pColRef->id;
888,363✔
1214
        entry.tagCondLen = pColRef->tagCondLen;
888,363✔
1215
        entry.tagCondJson = taosStrdup(pColRef->tagCondJson);
888,363✔
1216
        QUERY_CHECK_NULL(entry.tagCondJson, code, line, _return, terrno);
888,363✔
1217
        if (taosArrayPush(rsp.pEntries, &entry) == NULL) {
890,262✔
1218
          taosMemoryFree(entry.tagCondJson);
×
1219
          code = terrno;
×
1220
          QUERY_CHECK_CODE(code, line, _return);
886,464✔
1221
        }
1222
      }
1223
    }
1224
  }
1225
  rsp.numOfRefs = taosArrayGetSize(rsp.pEntries);
1,516,429✔
1226

1227
  rspSize = tSerializeSVTagCondRsp(NULL, 0, &rsp);
1,516,429✔
1228
  if (rspSize < 0) {
1,516,429✔
1229
    code = rspSize;
×
1230
    qError("tSerializeSVTagCondRsp failed, error:%d", rspSize);
×
1231
    goto _return;
×
1232
  }
1233
  if (direct) {
1,516,429✔
1234
    pRsp = rpcMallocCont(rspSize);
1,516,429✔
1235
  } else {
1236
    pRsp = taosMemoryCalloc(1, rspSize);
×
1237
  }
1238
  QUERY_CHECK_NULL(pRsp, code, line, _return, terrno);
1,516,429✔
1239
  rspSize = tSerializeSVTagCondRsp(pRsp, rspSize, &rsp);
1,516,429✔
1240
  if (rspSize < 0) {
1,516,429✔
1241
    code = rspSize;
×
1242
    qError("tSerializeSVTagCondRsp failed, error:%d", rspSize);
×
1243
    goto _return;
×
1244
  }
1245

1246
_return:
1,516,429✔
1247
  metaReaderClear(&mer);
1,516,429✔
1248
  rspMsg.info = pMsg->info;
1,516,429✔
1249
  rspMsg.pCont = pRsp;
1,516,429✔
1250
  rspMsg.contLen = (code == 0) ? rspSize : 0;
1,516,429✔
1251
  rspMsg.code = code;
1,516,429✔
1252
  rspMsg.msgType = pMsg->msgType;
1,516,429✔
1253
  if (code) {
1,516,429✔
1254
    qError("vnodeGetVTbTagCond failed since %s", tstrerror(code));
×
1255
  }
1256
  if (direct) {
1,516,429✔
1257
    tmsgSendRsp(&rspMsg);
1,516,429✔
1258
  } else {
1259
    *pMsg = rspMsg;
×
1260
  }
1261
  tDestroySVTagCondRsp(&rsp);
1,516,429✔
1262
  return code;
1,516,429✔
1263
}
1264

1265
static int32_t vnodeGetCompStorage(SVnode *pVnode, int64_t *output) {
36,858,528✔
1266
  int32_t code = 0;
36,858,528✔
1267
#ifdef TD_ENTERPRISE
1268
  int32_t now = taosGetTimestampSec();
36,858,528✔
1269
  if (llabs(now - pVnode->config.vndStats.storageLastUpd) >= 30) {
36,858,528✔
1270
    pVnode->config.vndStats.storageLastUpd = now;
2,090,619✔
1271

1272
    SDbSizeStatisInfo info = {0};
2,090,619✔
1273
    if (0 == (code = vnodeGetDBSize(pVnode, &info))) {
2,090,619✔
1274
      int64_t compSize =
2,090,619✔
1275
          info.l1Size + info.l2Size + info.l3Size + info.cacheSize + info.walSize + info.metaSize + +info.ssSize;
2,090,619✔
1276
      if (compSize >= 0) {
2,090,619✔
1277
        pVnode->config.vndStats.compStorage = compSize;
2,090,619✔
1278
      } else {
1279
        vError("vnode get comp storage failed since compSize is negative:%" PRIi64, compSize);
×
1280
        code = TSDB_CODE_APP_ERROR;
×
1281
      }
1282
    } else {
1283
      vWarn("vnode get comp storage failed since %s", tstrerror(code));
3,222✔
1284
    }
1285
  }
1286
  if (output) *output = pVnode->config.vndStats.compStorage;
36,858,528✔
1287
#endif
1288
  return code;
36,858,528✔
1289
}
1290

1291
static void vnodeGetBufferInfo(SVnode *pVnode, int64_t *bufferSegmentUsed, int64_t *bufferSegmentSize) {
36,858,528✔
1292
  *bufferSegmentUsed = 0;
36,858,528✔
1293
  *bufferSegmentSize = 0;
36,858,528✔
1294
  if (pVnode) {
36,858,528✔
1295
    (void)taosThreadMutexLock(&pVnode->mutex);
36,858,528✔
1296

1297
    if (pVnode->inUse) {
36,858,528✔
1298
      *bufferSegmentUsed = pVnode->inUse->size;
36,855,856✔
1299
    }
1300
    *bufferSegmentSize = pVnode->config.szBuf / VNODE_BUFPOOL_SEGMENTS;
36,858,528✔
1301

1302
    (void)taosThreadMutexUnlock(&pVnode->mutex);
36,858,528✔
1303
  }
1304
}
36,858,528✔
1305

1306
int32_t vnodeGetLoad(SVnode *pVnode, SVnodeLoad *pLoad) {
36,858,528✔
1307
  SSyncState state = syncGetState(pVnode->sync);
36,858,528✔
1308
  pLoad->syncAppliedIndex = pVnode->state.applied;
36,858,528✔
1309
  syncGetCommitIndex(pVnode->sync, &pLoad->syncCommitIndex);
36,858,528✔
1310

1311
  pLoad->vgId = TD_VID(pVnode);
36,858,528✔
1312
  pLoad->syncState = state.state;
36,858,528✔
1313
  pLoad->syncRestore = state.restored;
36,858,528✔
1314
  pLoad->syncTerm = state.term;
36,858,528✔
1315
  pLoad->roleTimeMs = state.roleTimeMs;
36,858,528✔
1316
  pLoad->startTimeMs = state.startTimeMs;
36,858,528✔
1317
  pLoad->syncCanRead = state.canRead;
36,858,528✔
1318
  pLoad->cacheUsage = tsdbCacheGetUsage(pVnode);
36,858,528✔
1319
  pLoad->numOfCachedTables = tsdbCacheGetElems(pVnode);
36,858,528✔
1320
  VNODE_DO_META_QUERY(pVnode, pLoad->numOfTables = metaGetTbNum(pVnode->pMeta));
36,858,528✔
1321
  VNODE_DO_META_QUERY(pVnode, pLoad->numOfTimeSeries = metaGetTimeSeriesNum(pVnode->pMeta, 1));
36,858,528✔
1322
  pLoad->totalStorage = (int64_t)3 * 1073741824;  // TODO
36,858,528✔
1323
  (void)vnodeGetCompStorage(pVnode, &pLoad->compStorage);
36,858,528✔
1324
  pLoad->pointsWritten = 100;
36,858,528✔
1325
  pLoad->numOfSelectReqs = 1;
36,858,528✔
1326
  pLoad->numOfInsertReqs = atomic_load_64(&pVnode->statis.nInsert);
36,858,528✔
1327
  pLoad->numOfInsertSuccessReqs = atomic_load_64(&pVnode->statis.nInsertSuccess);
36,858,528✔
1328
  pLoad->numOfBatchInsertReqs = atomic_load_64(&pVnode->statis.nBatchInsert);
36,858,528✔
1329
  pLoad->numOfBatchInsertSuccessReqs = atomic_load_64(&pVnode->statis.nBatchInsertSuccess);
36,858,528✔
1330
  vnodeGetBufferInfo(pVnode, &pLoad->bufferSegmentUsed, &pLoad->bufferSegmentSize);
36,858,528✔
1331
  vDebug("vgId:%d, get vnode load, state:%s snapSeq:%d, learnerProgress:%d, totalIndex:%" PRId64, TD_VID(pVnode),
36,858,528✔
1332
         syncStr(state.state), state.snapSeq, state.progress, state.totalIndex);
1333
  pLoad->learnerProgress = state.progress;
36,858,528✔
1334
  pLoad->snapSeq = state.snapSeq;
36,858,528✔
1335
  pLoad->syncTotalIndex = state.totalIndex;
36,858,528✔
1336
  pLoad->snapshotSending = syncSnapshotSending(pVnode->sync) ? 1 : 0;
36,858,528✔
1337
  // Report per-target snapshot-send progress, so the mnode can display progress and compute the rate per target follower dnodeId.
1338
  // Fetch the byte summary grouped by destDnodeId from tsdb, and convert each entry into an SVnodeSnapProgress filled into pLoad->pSnapProgress.
1339
  pLoad->pSnapProgress = NULL;
36,858,528✔
1340
  SArray *pSnapGroups = taosArrayInit(4, sizeof(STsdbSnapSendGroup));
36,858,528✔
1341
  if (pSnapGroups != NULL) {
36,858,528✔
1342
    tsdbGetSnapSendSummary(pVnode->pTsdb, pSnapGroups);
36,858,528✔
1343
    int32_t numGroups = (int32_t)taosArrayGetSize(pSnapGroups);
36,858,528✔
1344
    if (numGroups > 0) {
36,858,528✔
1345
      pLoad->pSnapProgress = taosArrayInit(numGroups, sizeof(SVnodeSnapProgress));
751✔
1346
      if (pLoad->pSnapProgress != NULL) {
751✔
1347
        for (int32_t g = 0; g < numGroups; g++) {
1,502✔
1348
          STsdbSnapSendGroup *pGroup = (STsdbSnapSendGroup *)taosArrayGet(pSnapGroups, g);
751✔
1349
          if (pGroup == NULL) continue;
751✔
1350
          SVnodeSnapProgress prog = {.destDnodeId = pGroup->destDnodeId,
751✔
1351
                                     .snapTotalSize = pGroup->total,
751✔
1352
                                     .snapTransferredSize = pGroup->transferred};
751✔
1353
          if (taosArrayPush(pLoad->pSnapProgress, &prog) == NULL) {
1,502✔
1354
            vError("vgId:%d, failed to push snap progress for destDnodeId:%d", TD_VID(pVnode), pGroup->destDnodeId);
×
1355
          }
1356
        }
1357
        vDebug("vgId:%d, report snap send progress, numGroups:%d", TD_VID(pVnode), numGroups);
751✔
1358
      } else {
1359
        vError("vgId:%d, failed to alloc pSnapProgress array for vnode load", TD_VID(pVnode));
×
1360
      }
1361
    }
1362
    taosArrayDestroy(pSnapGroups);
36,858,528✔
1363
  } else {
1364
    vError("vgId:%d, failed to alloc snap send groups array for vnode load", TD_VID(pVnode));
×
1365
  }
1366
  return 0;
36,858,528✔
1367
}
1368

1369
int32_t vnodeGetLoadLite(SVnode *pVnode, SVnodeLoadLite *pLoad) {
×
1370
  SSyncState syncState = syncGetState(pVnode->sync);
×
1371
  if (syncState.state == TAOS_SYNC_STATE_LEADER || syncState.state == TAOS_SYNC_STATE_ASSIGNED_LEADER) {
×
1372
    pLoad->vgId = TD_VID(pVnode);
×
1373
    pLoad->nTimeSeries = metaGetTimeSeriesNum(pVnode->pMeta, 1);
×
1374
    return 0;
×
1375
  }
1376
  return -1;
×
1377
}
1378
/**
1379
 * @brief Reset the statistics value by monitor interval
1380
 *
1381
 * @param pVnode
1382
 * @param pLoad
1383
 */
1384
void vnodeResetLoad(SVnode *pVnode, SVnodeLoad *pLoad) {
114✔
1385
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nInsert, pLoad->numOfInsertReqs, 64, "nInsert");
114✔
1386
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nInsertSuccess, pLoad->numOfInsertSuccessReqs, 64, "nInsertSuccess");
114✔
1387
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nBatchInsert, pLoad->numOfBatchInsertReqs, 64, "nBatchInsert");
114✔
1388
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nBatchInsertSuccess, pLoad->numOfBatchInsertSuccessReqs, 64,
114✔
1389
                            "nBatchInsertSuccess");
1390
}
114✔
1391

1392
void vnodeGetInfo(void *pVnode, const char **dbname, int32_t *vgId, int64_t *numOfTables, int64_t *numOfNormalTables) {
31,404,974✔
1393
  SVnode    *pVnodeObj = pVnode;
31,404,974✔
1394
  SVnodeCfg *pConf = &pVnodeObj->config;
31,404,974✔
1395

1396
  if (dbname) {
31,404,974✔
1397
    *dbname = pConf->dbname;
30,080,952✔
1398
  }
1399

1400
  if (vgId) {
31,404,974✔
1401
    *vgId = TD_VID(pVnodeObj);
30,800,164✔
1402
  }
1403

1404
  if (numOfTables) {
31,404,974✔
1405
    *numOfTables = pConf->vndStats.numOfNTables + pConf->vndStats.numOfCTables +
3,308✔
1406
                   pConf->vndStats.numOfVTables + pConf->vndStats.numOfVCTables;
3,308✔
1407
  }
1408

1409
  if (numOfNormalTables) {
31,404,974✔
1410
    *numOfNormalTables = pConf->vndStats.numOfNTables +
2,281✔
1411
                         pConf->vndStats.numOfVTables;
2,281✔
1412
  }
1413
}
31,404,974✔
1414

1415
int8_t vnodeGetSecurityLevel(void *pVnode) {
444,091✔
1416
  SVnode *pVnodeObj = pVnode;
444,091✔
1417
  return pVnodeObj->config.securityLevel;
444,091✔
1418
}
1419

1420
bool vnodeHasPendingTxnEntries(void *pVnode) {
×
1421
  SVnode *pVnodeObj = pVnode;
×
1422
  return metaHasPendingTxnEntries(pVnodeObj->pMeta);
×
1423
}
1424

1425
int32_t vnodeGetTableList(void *pVnode, int8_t type, SArray *pList) {
2,281✔
1426
  if (type == TSDB_SUPER_TABLE) {
2,281✔
1427
    return vnodeGetStbIdList(pVnode, 0, pList);
2,281✔
1428
  } else {
1429
    return TSDB_CODE_INVALID_PARA;
×
1430
  }
1431
}
1432

1433
int32_t vnodeGetAllTableList(SVnode *pVnode, uint64_t uid, SArray *list) {
×
1434
  int32_t      code = TSDB_CODE_SUCCESS;
×
1435
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, uid, 1, 0);
×
1436
  if (NULL == pCur) {
×
1437
    qError("vnode get all table list failed");
×
1438
    return terrno;
×
1439
  }
1440

1441
  while (1) {
×
1442
    tb_uid_t id = metaCtbCursorNext(pCur);
×
1443
    if (id == 0) {
×
1444
      break;
×
1445
    }
1446

1447
    STableKeyInfo info = {uid = id};
×
1448
    if (NULL == taosArrayPush(list, &info)) {
×
1449
      qError("taosArrayPush failed");
×
1450
      code = terrno;
×
1451
      goto _exit;
×
1452
    }
1453
  }
1454
_exit:
×
1455
  metaCloseCtbCursor(pCur);
×
1456
  return code;
×
1457
}
1458

1459
int32_t vnodeGetCtbIdListByFilter(SVnode *pVnode, int64_t suid, SArray *list, bool (*filter)(void *arg), void *arg) {
×
1460
  return 0;
×
1461
}
1462

1463
int32_t vnodeGetCtbIdList(void *pVnode, int64_t suid, SArray *list) {
90,873,680✔
1464
  int32_t      code = TSDB_CODE_SUCCESS;
90,873,680✔
1465
  SVnode      *pVnodeObj = pVnode;
90,873,680✔
1466
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnodeObj, suid, 1, 0);
90,873,680✔
1467
  if (NULL == pCur) {
90,856,215✔
1468
    qError("vnode get ctb id list failed, suid:%" PRId64, suid);
×
1469
    return terrno;
×
1470
  }
1471

1472
  while (1) {
256,865,838✔
1473
    tb_uid_t id = metaCtbCursorNext(pCur);
347,722,053✔
1474
    if (id == 0) {
346,948,118✔
1475
      break;
90,655,376✔
1476
    }
1477
    qTrace("vnodeGetCtbIdList: got ctb id %" PRId64 " for suid %" PRId64, id, suid);
256,060,824✔
1478
    if (NULL == taosArrayPush(list, &id)) {
256,744,045✔
1479
      qError("taosArrayPush failed");
×
1480
      code = terrno;
×
1481
      goto _exit;
×
1482
    }
1483
  }
1484

1485
_exit:
90,718,843✔
1486
  metaCloseCtbCursor(pCur);
90,718,843✔
1487
  return code;
90,842,995✔
1488
}
1489

1490
int32_t vnodeGetStbIdList(SVnode *pVnode, int64_t suid, SArray *list) {
28,057,247✔
1491
  int32_t      code = TSDB_CODE_SUCCESS;
28,057,247✔
1492
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, suid);
28,057,247✔
1493
  if (!pCur) {
28,057,443✔
1494
    return TSDB_CODE_OUT_OF_MEMORY;
×
1495
  }
1496

1497
  while (1) {
13,162,942✔
1498
    tb_uid_t id = metaStbCursorNext(pCur);
41,220,385✔
1499
    if (id == 0) {
41,220,893✔
1500
      break;
27,983,674✔
1501
    }
1502

1503
    if (NULL == taosArrayPush(list, &id)) {
13,163,138✔
1504
      qError("taosArrayPush failed");
×
1505
      code = terrno;
×
1506
      goto _exit;
×
1507
    }
1508
  }
1509

1510
_exit:
28,057,074✔
1511
  metaCloseStbCursor(pCur);
28,057,074✔
1512
  return code;
28,057,443✔
1513
}
1514

1515
int32_t vnodeGetStbIdListByFilter(SVnode *pVnode, int64_t suid, SArray *list, bool (*filter)(void *arg, void *arg1),
1,459✔
1516
                                  void *arg) {
1517
  int32_t      code = TSDB_CODE_SUCCESS;
1,459✔
1518
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, suid);
1,459✔
1519
  if (!pCur) {
1,459✔
1520
    return terrno;
×
1521
  }
1522

1523
  while (1) {
2,142✔
1524
    tb_uid_t id = metaStbCursorNext(pCur);
3,296✔
1525
    if (id == 0) {
3,296✔
1526
      break;
1,154✔
1527
    }
1528

1529
    if ((*filter) && (*filter)(arg, &id)) {
1,837✔
1530
      continue;
1,837✔
1531
    }
1532

1533
    if (NULL == taosArrayPush(list, &id)) {
×
1534
      qError("taosArrayPush failed");
×
1535
      code = terrno;
×
1536
      goto _exit;
×
1537
    }
1538
  }
1539

1540
_exit:
1,459✔
1541
  metaCloseStbCursor(pCur);
1,459✔
1542
  return code;
1,459✔
1543
}
1544

1545
int32_t vnodeGetCtbNum(SVnode *pVnode, int64_t suid, int64_t *num) {
1,474,620✔
1546
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, suid, 0, 0);
1,474,620✔
1547
  if (!pCur) {
1,474,801✔
1548
    return terrno;
×
1549
  }
1550

1551
  *num = 0;
1,474,801✔
1552
  while (1) {
1,075,122✔
1553
    tb_uid_t id = metaCtbCursorNext(pCur);
2,545,472✔
1554
    if (id == 0) {
2,545,540✔
1555
      break;
1,473,710✔
1556
    }
1557

1558
    ++(*num);
1,070,671✔
1559
  }
1560

1561
  metaCloseCtbCursor(pCur);
1,474,869✔
1562
  return TSDB_CODE_SUCCESS;
1,474,869✔
1563
}
1564

1565
int32_t vnodeGetStbColumnNum(SVnode *pVnode, tb_uid_t suid, int *num) {
1,474,724✔
1566
  SSchemaWrapper *pSW = metaGetTableSchema(pVnode->pMeta, suid, -1, 0, NULL, 0, false);
1,474,724✔
1567
  if (pSW) {
1,474,796✔
1568
    *num = pSW->nCols;
1,473,360✔
1569
    tDeleteSchemaWrapper(pSW);
1,159✔
1570
  } else {
1571
    *num = 2;
1,436✔
1572
  }
1573

1574
  return TSDB_CODE_SUCCESS;
1,474,719✔
1575
}
1576

1577
int32_t vnodeGetStbInfo(SVnode *pVnode, tb_uid_t suid, int64_t *keep, int8_t *flags) {
1,474,946✔
1578
  SMetaReader mr = {0};
1,474,946✔
1579
  metaReaderDoInit(&mr, pVnode->pMeta, META_READER_NOLOCK, 0);
1,474,946✔
1580

1581
  int32_t code = metaReaderGetTableEntryByUid(&mr, suid);
1,474,848✔
1582
  if (code == TSDB_CODE_SUCCESS) {
1,474,797✔
1583
    if (keep) *keep = mr.me.stbEntry.keep;
1,473,361✔
1584
    if (flags) *flags = mr.me.flags;
1,473,361✔
1585
  } else {
1586
    if (keep) *keep = 0;
1,436✔
1587
    if (flags) *flags = 0;
1,436✔
1588
  }
1589

1590
  metaReaderClear(&mr);
1,474,797✔
1591
  return TSDB_CODE_SUCCESS;
1,474,941✔
1592
}
1593

1594
#ifdef TD_ENTERPRISE
1595
const char *tkLogStb[] = {"cluster_info",
1596
                          "data_dir",
1597
                          "disk_free",
1598
                          "disk_total",
1599
                          "dnodes_info",
1600
                          "d_info",
1601
                          "explorer_sys",
1602
                          "grants_info",
1603
                          "keeper_monitor",
1604
                          "logs",
1605
                          "log_dir",
1606
                          "log_summary",
1607
                          "http_client_requests",
1608
                          "http_server_requests_idmp",
1609
                          "m_info",
1610
                          "taosadapter_restful_http_request_fail",
1611
                          "taosadapter_restful_http_request_in_flight",
1612
                          "taosadapter_restful_http_request_summary_milliseconds",
1613
                          "taosadapter_restful_http_request_total",
1614
                          "taosadapter_system_cpu_percent",
1615
                          "taosadapter_system_mem_percent",
1616
                          "temp_dir",
1617
                          "vgroups_info",
1618
                          "vnodes_role",
1619
                          "taosd_dnodes_status",
1620
                          "adapter_conn_pool",
1621
                          "taosd_vnodes_info",
1622
                          "taosd_dnodes_metrics",
1623
                          "taosd_vgroups_info",
1624
                          "taos_sql_req",
1625
                          "taosd_mnodes_info",
1626
                          "adapter_c_interface",
1627
                          "taosd_cluster_info",
1628
                          "taosd_sql_req",
1629
                          "taosd_dnodes_info",
1630
                          "adapter_requests",
1631
                          "taosd_write_metrics",
1632
                          "adapter_status",
1633
                          "taos_slow_sql",
1634
                          "taos_slow_sql_detail",
1635
                          "taosd_cluster_basic",
1636
                          "taosd_dnodes_data_dirs",
1637
                          "taosd_dnodes_log_dirs",
1638
                          "xnode_agent_activities",
1639
                          "xnode_task_activities",
1640
                          "xnode_task_metrics",
1641
                          "taosx_sys",
1642
                          "taosx_task_csv",
1643
                          "taosx_task_progress",
1644
                          "taosx_task_kinghist",
1645
                          "taosx_task_tdengine2",
1646
                          "taosx_task_tdengine3",
1647
                          "taosx_task_opc_da",
1648
                          "taosx_task_opc_ua",
1649
                          "taosx_task_kafka",
1650
                          "taosx_task_influxdb",
1651
                          "taosx_task_mqtt",
1652
                          "taosx_task_avevahistorian",
1653
                          "taosx_task_opentsdb",
1654
                          "taosx_task_mysql",
1655
                          "taosx_task_postgres",
1656
                          "taosx_task_oracle",
1657
                          "taosx_task_mssql",
1658
                          "taosx_task_mongodb",
1659
                          "taosx_task_sparkplugb",
1660
                          "taosx_task_orc",
1661
                          "taosx_task_pulsar",
1662
                          "taosx_task_pspace"};
1663
const char *tkAuditStb[] = {"operations"};
1664
const int   tkLogStbNum = ARRAY_SIZE(tkLogStb);
1665
const int   tkAuditStbNum = ARRAY_SIZE(tkAuditStb);
1666

1667
// exclude stbs of taoskeeper log
1668
static int32_t vnodeGetTimeSeriesBlackList(SVnode *pVnode, int32_t *tbSize) {
28,056,410✔
1669
  int32_t      code = TSDB_CODE_SUCCESS;
28,056,410✔
1670
  int32_t      tbNum = 0;
28,056,410✔
1671
  const char **pTbArr = NULL;
28,056,410✔
1672
  const char  *dbName = NULL;
28,056,410✔
1673
  *tbSize = 0;
28,056,410✔
1674

1675
  if (!(dbName = strchr(pVnode->config.dbname, '.'))) return 0;
28,056,410✔
1676
  if (0 == strncmp(++dbName, "log", TSDB_DB_NAME_LEN)) {
28,056,404✔
1677
    tbNum = tkLogStbNum;
460✔
1678
    pTbArr = (const char **)&tkLogStb;
460✔
1679
  } else if (0 == strncmp(dbName, "audit", TSDB_DB_NAME_LEN) || pVnode->config.isAudit) {
28,055,599✔
1680
    tbNum = tkAuditStbNum;
1,396✔
1681
    pTbArr = (const char **)&tkAuditStb;
1,396✔
1682
  }
1683
  if (tbNum && pTbArr) {
28,056,404✔
1684
    *tbSize = metaSizeOfTbFilterCache(pVnode->pMeta, 0);
2,201✔
1685
    if (*tbSize < tbNum) {
2,201✔
1686
      for (int32_t i = 0; i < tbNum; ++i) {
23,293✔
1687
        tb_uid_t suid = metaGetTableEntryUidByName(pVnode->pMeta, pTbArr[i]);
22,867✔
1688
        if (suid != 0) {
22,867✔
1689
          code = metaPutTbToFilterCache(pVnode->pMeta, &suid, 0);
1,100✔
1690
          if (TSDB_CODE_SUCCESS != code) {
1,100✔
1691
            return code;
519✔
1692
          }
1693
        }
1694
      }
1695
      *tbSize = metaSizeOfTbFilterCache(pVnode->pMeta, 0);
426✔
1696
    }
1697
  }
1698

1699
  return code;
27,981,701✔
1700
}
1701
#endif
1702

1703
static bool vnodeTimeSeriesFilter(void *arg1, void *arg2) {
1,837✔
1704
  SVnode *pVnode = (SVnode *)arg1;
1,837✔
1705

1706
  if (metaTbInFilterCache(pVnode->pMeta, arg2, 0)) {
1,837✔
1707
    return true;
1,522✔
1708
  }
1709
  return false;
×
1710
}
1711

1712
int32_t vnodeGetTimeSeriesNum(SVnode *pVnode, int64_t *num) {
28,056,606✔
1713
  SArray *suidList = NULL;
28,056,606✔
1714

1715
  if (!(suidList = taosArrayInit(1, sizeof(tb_uid_t)))) {
28,056,606✔
1716
    return terrno;
×
1717
  }
1718

1719
  int32_t tbFilterSize = 0;
28,056,754✔
1720
  int32_t code = TSDB_CODE_SUCCESS;
28,056,754✔
1721
#ifdef TD_ENTERPRISE
1722
  code = vnodeGetTimeSeriesBlackList(pVnode, &tbFilterSize);
28,056,754✔
1723
  if (TSDB_CODE_SUCCESS != code) {
28,057,194✔
1724
    goto _exit;
519✔
1725
  }
1726
#endif
1727

1728
  if ((!tbFilterSize && vnodeGetStbIdList(pVnode, 0, suidList) < 0) ||
28,056,675✔
1729
      (tbFilterSize && vnodeGetStbIdListByFilter(pVnode, 0, suidList, vnodeTimeSeriesFilter, pVnode) < 0)) {
27,982,549✔
1730
    qError("vgId:%d, failed to get stb id list error: %s", TD_VID(pVnode), terrstr());
×
1731
    taosArrayDestroy(suidList);
×
1732
    return terrno;
×
1733
  }
1734

1735
  *num = 0;
28,056,527✔
1736
  int64_t arrSize = taosArrayGetSize(suidList);
28,056,527✔
1737
  for (int64_t i = 0; i < arrSize; ++i) {
41,218,917✔
1738
    tb_uid_t suid = *(tb_uid_t *)taosArrayGet(suidList, i);
13,162,438✔
1739

1740
    int64_t ctbNum = 0;
13,162,438✔
1741
    int32_t numOfCols = 0;
13,162,438✔
1742
    int8_t  flags = 0;
13,162,438✔
1743
    code = metaGetStbStats(pVnode, suid, &ctbNum, &numOfCols, &flags);
13,162,554✔
1744
    if (TSDB_CODE_SUCCESS != code) {
13,162,438✔
1745
      goto _exit;
×
1746
    }
1747
    if (!TABLE_IS_VIRTUAL(flags)) {
13,162,438✔
1748
      *num += ctbNum * (numOfCols - 1);
9,708,880✔
1749
    }
1750
  }
1751

1752
_exit:
28,056,859✔
1753
  taosArrayDestroy(suidList);
28,057,194✔
1754
  return TSDB_CODE_SUCCESS;
28,056,998✔
1755
}
1756

1757
int32_t vnodeGetAllCtbNum(SVnode *pVnode, int64_t *num) {
×
1758
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, 0);
×
1759
  if (!pCur) {
×
1760
    return terrno;
×
1761
  }
1762

1763
  *num = 0;
×
1764
  while (1) {
×
1765
    tb_uid_t id = metaStbCursorNext(pCur);
×
1766
    if (id == 0) {
×
1767
      break;
×
1768
    }
1769

1770
    int64_t ctbNum = 0;
×
1771
    int32_t code = vnodeGetCtbNum(pVnode, id, &ctbNum);
×
1772
    if (TSDB_CODE_SUCCESS != code) {
×
1773
      metaCloseStbCursor(pCur);
×
1774
      return code;
×
1775
    }
1776

1777
    *num += ctbNum;
×
1778
  }
1779

1780
  metaCloseStbCursor(pCur);
×
1781
  return TSDB_CODE_SUCCESS;
×
1782
}
1783

1784
void *vnodeGetIdx(void *pVnode) {
1,117,719✔
1785
  if (pVnode == NULL) {
1,117,719✔
1786
    return NULL;
×
1787
  }
1788

1789
  return metaGetIdx(((SVnode *)pVnode)->pMeta);
1,117,719✔
1790
}
1791

1792
void *vnodeGetIvtIdx(void *pVnode) {
1,117,719✔
1793
  if (pVnode == NULL) {
1,117,719✔
1794
    return NULL;
×
1795
  }
1796
  return metaGetIvtIdx(((SVnode *)pVnode)->pMeta);
1,117,719✔
1797
}
1798

1799
int32_t vnodeGetTableSchema(void *pVnode, int64_t uid, STSchema **pSchema, int64_t *suid, SSchemaWrapper **pTagSchema) {
423,487✔
1800
  return tsdbGetTableSchema(((SVnode *)pVnode)->pMeta, uid, pSchema, suid, pTagSchema);
423,487✔
1801
}
1802

1803
static FORCE_INLINE int32_t vnodeGetDBPrimaryInfo(SVnode *pVnode, SDbSizeStatisInfo *pInfo) {
3,222✔
1804
  int32_t code = 0;
2,092,401✔
1805
  char    path[TSDB_FILENAME_LEN] = {0};
2,092,401✔
1806

1807
  char   *dirName[] = {VNODE_TSDB_DIR, VNODE_WAL_DIR, VNODE_META_DIR, VNODE_TSDB_CACHE_DIR};
2,092,401✔
1808
  int64_t dirSize[4];
2,092,401✔
1809

1810
  vnodeGetPrimaryPath(pVnode, false, path, TSDB_FILENAME_LEN);
2,092,401✔
1811
  int32_t offset = strlen(path);
2,092,401✔
1812

1813
  for (int i = 0; i < sizeof(dirName) / sizeof(dirName[0]); i++) {
10,462,005✔
1814
    int64_t size = {0};
8,369,604✔
1815
    (void)snprintf(path + offset, TSDB_FILENAME_LEN - offset, "%s%s", TD_DIRSEP, dirName[i]);
8,369,604✔
1816
    code = taosGetDirSize(path, &size);
8,369,604✔
1817
    if (code != 0) {
8,369,604✔
1818
      uWarn("vnode %d get dir %s %s size failed since %s", TD_VID(pVnode), path, dirName[i], tstrerror(code));
529✔
1819
    }
1820
    path[offset] = 0;
8,369,604✔
1821
    dirSize[i] = size;
8,369,604✔
1822
  }
1823

1824
  pInfo->l1Size = 0;
2,092,401✔
1825
  pInfo->walSize = dirSize[1];
2,092,401✔
1826
  pInfo->metaSize = dirSize[2];
2,092,401✔
1827
  pInfo->cacheSize = dirSize[3];
2,092,401✔
1828
  return code;
2,092,401✔
1829
}
1830
int32_t vnodeGetDBSize(void *pVnode, SDbSizeStatisInfo *pInfo) {
2,092,401✔
1831
  int32_t code = 0;
2,092,401✔
1832
  int32_t lino = 0;
2,092,401✔
1833
  SVnode *pVnodeObj = pVnode;
2,092,401✔
1834
  if (pVnodeObj == NULL) {
2,092,401✔
1835
    return TSDB_CODE_VND_NOT_EXIST;
×
1836
  }
1837
  code = vnodeGetDBPrimaryInfo(pVnode, pInfo);
2,092,401✔
1838
  if (code != 0) goto _exit;
2,092,401✔
1839

1840
  code = tsdbGetFsSize(pVnodeObj->pTsdb, pInfo);
2,092,401✔
1841
_exit:
2,089,179✔
1842
  return code;
2,089,179✔
1843
}
1844

1845
/*
1846
 * Get raw write metrics for a vnode
1847
 */
1848
int32_t vnodeGetRawWriteMetrics(void *pVnode, SRawWriteMetrics *pRawMetrics) {
×
1849
  if (pVnode == NULL || pRawMetrics == NULL) {
×
1850
    return TSDB_CODE_INVALID_PARA;
×
1851
  }
1852

1853
  SVnode      *pVnode1 = (SVnode *)pVnode;
×
1854
  SSyncMetrics syncMetrics = syncGetMetrics(pVnode1->sync);
×
1855

1856
  // Copy values following SRawWriteMetrics structure order
1857
  pRawMetrics->total_requests = atomic_load_64(&pVnode1->writeMetrics.total_requests);
×
1858
  pRawMetrics->total_rows = atomic_load_64(&pVnode1->writeMetrics.total_rows);
×
1859
  pRawMetrics->total_bytes = atomic_load_64(&pVnode1->writeMetrics.total_bytes);
×
1860
  pRawMetrics->fetch_batch_meta_time = atomic_load_64(&pVnode1->writeMetrics.fetch_batch_meta_time);
×
1861
  pRawMetrics->fetch_batch_meta_count = atomic_load_64(&pVnode1->writeMetrics.fetch_batch_meta_count);
×
1862
  pRawMetrics->preprocess_time = atomic_load_64(&pVnode1->writeMetrics.preprocess_time);
×
1863
  pRawMetrics->wal_write_bytes = atomic_load_64(&syncMetrics.wal_write_bytes);
×
1864
  pRawMetrics->wal_write_time = atomic_load_64(&syncMetrics.wal_write_time);
×
1865
  pRawMetrics->apply_bytes = atomic_load_64(&pVnode1->writeMetrics.apply_bytes);
×
1866
  pRawMetrics->apply_time = atomic_load_64(&pVnode1->writeMetrics.apply_time);
×
1867
  pRawMetrics->commit_count = atomic_load_64(&pVnode1->writeMetrics.commit_count);
×
1868
  pRawMetrics->commit_time = atomic_load_64(&pVnode1->writeMetrics.commit_time);
×
1869
  pRawMetrics->memtable_wait_time = atomic_load_64(&pVnode1->writeMetrics.memtable_wait_time);
×
1870
  pRawMetrics->blocked_commit_count = atomic_load_64(&pVnode1->writeMetrics.blocked_commit_count);
×
1871
  pRawMetrics->blocked_commit_time = atomic_load_64(&pVnode1->writeMetrics.block_commit_time);
×
1872
  pRawMetrics->merge_count = atomic_load_64(&pVnode1->writeMetrics.merge_count);
×
1873
  pRawMetrics->merge_time = atomic_load_64(&pVnode1->writeMetrics.merge_time);
×
1874
  pRawMetrics->last_cache_commit_time = atomic_load_64(&pVnode1->writeMetrics.last_cache_commit_time);
×
1875
  pRawMetrics->last_cache_commit_count = atomic_load_64(&pVnode1->writeMetrics.last_cache_commit_count);
×
1876

1877
  return 0;
×
1878
}
1879

1880
/*
1881
 * Reset raw write metrics for a vnode by subtracting old values
1882
 */
1883
int32_t vnodeResetRawWriteMetrics(void *pVnode, const SRawWriteMetrics *pOldMetrics) {
×
1884
  if (pVnode == NULL || pOldMetrics == NULL) {
×
1885
    return TSDB_CODE_INVALID_PARA;
×
1886
  }
1887

1888
  SVnode *pVnode1 = (SVnode *)pVnode;
×
1889

1890
  // Reset vnode write metrics using atomic operations to subtract old values
1891
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.total_requests, pOldMetrics->total_requests);
×
1892
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.total_rows, pOldMetrics->total_rows);
×
1893
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.total_bytes, pOldMetrics->total_bytes);
×
1894

1895
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.fetch_batch_meta_time, pOldMetrics->fetch_batch_meta_time);
×
1896
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.fetch_batch_meta_count, pOldMetrics->fetch_batch_meta_count);
×
1897
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.preprocess_time, pOldMetrics->preprocess_time);
×
1898
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.apply_bytes, pOldMetrics->apply_bytes);
×
1899
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.apply_time, pOldMetrics->apply_time);
×
1900
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.commit_count, pOldMetrics->commit_count);
×
1901

1902
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.commit_time, pOldMetrics->commit_time);
×
1903
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.merge_time, pOldMetrics->merge_time);
×
1904

1905
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.memtable_wait_time, pOldMetrics->memtable_wait_time);
×
1906
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.blocked_commit_count, pOldMetrics->blocked_commit_count);
×
1907
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.block_commit_time, pOldMetrics->blocked_commit_time);
×
1908
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.merge_count, pOldMetrics->merge_count);
×
1909

1910
  // Reset new cache metrics
1911
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.last_cache_commit_time, pOldMetrics->last_cache_commit_time);
×
1912
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.last_cache_commit_count, pOldMetrics->last_cache_commit_count);
×
1913

1914
  // Reset sync metrics
1915
  SSyncMetrics syncMetrics = {
×
1916
      .wal_write_bytes = pOldMetrics->wal_write_bytes,
×
1917
      .wal_write_time = pOldMetrics->wal_write_time,
×
1918
  };
1919
  syncResetMetrics(pVnode1->sync, &syncMetrics);
×
1920

1921
  return 0;
×
1922
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc