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

taosdata / TDengine / #5053

13 May 2026 12:00PM UTC coverage: 73.397% (+0.06%) from 73.338%
#5053

push

travis-ci

web-flow
feat: taosdump support stream backup/restore (#35326)

139 of 170 new or added lines in 3 files covered. (81.76%)

627 existing lines in 131 files now uncovered.

281694 of 383795 relevant lines covered (73.4%)

132505311.38 hits per line

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

63.28
/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 "tsdb.h"
17
#include "tutil.h"
18
#include "vnd.h"
19

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

28
int vnodeQueryOpen(SVnode *pVnode) {
4,901,123✔
29
  return qWorkerInit(NODE_TYPE_VNODE, TD_VID(pVnode), (void **)&pVnode->pQuery, &pVnode->msgCb);
4,901,123✔
30
}
31

32
void vnodeQueryPreClose(SVnode *pVnode) { qWorkerStopAllTasks((void *)pVnode->pQuery); }
4,900,608✔
33

34
void vnodeQueryClose(SVnode *pVnode) { qWorkerDestroy((void **)&pVnode->pQuery); }
4,901,205✔
35

36
int32_t fillTableColCmpr(SMetaReader *reader, SSchemaExt *pExt, int32_t numOfCol) {
23,246,640✔
37
  int8_t tblType = reader->me.type;
23,246,640✔
38
  if (withColCompress(tblType)) {
23,246,640✔
39
    SColCmprWrapper *p = &(reader->me.colCmpr);
23,089,688✔
40
    if (numOfCol != p->nCols) {
23,090,288✔
41
      vError("fillTableColCmpr table type:%d, col num:%d, col cmpr num:%d mismatch", tblType, numOfCol, p->nCols);
×
42
      return TSDB_CODE_APP_ERROR;
×
43
    }
44
    for (int i = 0; i < p->nCols; i++) {
272,536,446✔
45
      SColCmpr *pCmpr = &p->pColCmpr[i];
249,446,806✔
46
      pExt[i].colId = pCmpr->id;
249,446,888✔
47
      pExt[i].compress = pCmpr->alg;
249,444,826✔
48
    }
49
  }
50
  return 0;
23,247,840✔
51
}
52

53
void vnodeDebugTableMeta(STableMetaRsp *pMeta) {
23,173,443✔
54
  if (!(qDebugFlag & DEBUG_DEBUG)) {
23,173,443✔
55
    return;
423,308✔
56
  }
57

58
  qDebug("tbName:%s", pMeta->tbName);
22,750,135✔
59
  qDebug("stbName:%s", pMeta->stbName);
22,751,099✔
60
  qDebug("dbFName:%s", pMeta->dbFName);
22,751,099✔
61
  qDebug("dbId:%" PRId64, pMeta->dbId);
22,751,099✔
62
  qDebug("numOfTags:%d", pMeta->numOfTags);
22,751,099✔
63
  qDebug("numOfColumns:%d", pMeta->numOfColumns);
22,751,099✔
64
  qDebug("precision:%d", pMeta->precision);
22,751,099✔
65
  qDebug("tableType:%d", pMeta->tableType);
22,751,099✔
66
  qDebug("sversion:%d", pMeta->sversion);
22,751,099✔
67
  qDebug("tversion:%d", pMeta->tversion);
22,751,099✔
68
  qDebug("suid:%" PRIu64, pMeta->suid);
22,751,099✔
69
  qDebug("tuid:%" PRIu64, pMeta->tuid);
22,751,099✔
70
  qDebug("vgId:%d", pMeta->vgId);
22,751,099✔
71
  qDebug("sysInfo:%d", pMeta->sysInfo);
22,751,099✔
72
  if (pMeta->pSchemas) {
22,751,099✔
73
    for (int32_t i = 0; i < (pMeta->numOfColumns + pMeta->numOfTags); ++i) {
314,941,425✔
74
      SSchema *pSchema = pMeta->pSchemas + i;
292,190,326✔
75
      qDebug("%d col/tag: type:%d, flags:%d, colId:%d, bytes:%d, name:%s", i, pSchema->type, pSchema->flags,
292,190,326✔
76
             pSchema->colId, pSchema->bytes, pSchema->name);
77
    }
78
  }
79
}
80

81
int32_t fillTableColRef(SMetaReader *reader, SColRef *pRef, int32_t numOfCol) {
401,969✔
82
  int8_t tblType = reader->me.type;
401,969✔
83
  if (hasRefCol(tblType)) {
401,969✔
84
    SColRefWrapper *p = &(reader->me.colRef);
401,969✔
85
    if (numOfCol != p->nCols) {
401,969✔
86
      vError("fillTableColRef table type:%d, col num:%d, col cmpr num:%d mismatch", tblType, numOfCol, p->nCols);
×
87
      return TSDB_CODE_APP_ERROR;
×
88
    }
89
    for (int i = 0; i < p->nCols; i++) {
3,127,358✔
90
      SColRef *pColRef = &p->pColRef[i];
2,725,389✔
91
      pRef[i].hasRef = pColRef->hasRef;
2,725,389✔
92
      pRef[i].id = pColRef->id;
2,725,389✔
93
      if(pRef[i].hasRef) {
2,725,389✔
94
        tstrncpy(pRef[i].refDbName, pColRef->refDbName, TSDB_DB_NAME_LEN);
1,922,357✔
95
        tstrncpy(pRef[i].refTableName, pColRef->refTableName, TSDB_TABLE_NAME_LEN);
1,922,357✔
96
        tstrncpy(pRef[i].refColName, pColRef->refColName, TSDB_COL_NAME_LEN);
1,922,357✔
97
      }
98
    }
99
  }
100
  return 0;
401,969✔
101
}
102

103
int32_t fillTableTagRef(SMetaReader *reader, SColRef *pRef, int32_t numOfTagRefs) {
×
104
  int8_t tblType = reader->me.type;
×
105
  if (hasRefCol(tblType)) {
×
106
    SColRefWrapper *p = &(reader->me.colRef);
×
107
    if (numOfTagRefs != p->nTagRefs) {
×
108
      vError("fillTableTagRef table type:%d, tag ref num:%d, expected:%d mismatch", tblType, numOfTagRefs, p->nTagRefs);
×
109
      return TSDB_CODE_APP_ERROR;
×
110
    }
111
    for (int i = 0; i < p->nTagRefs; i++) {
×
112
      SColRef *pTagRef = &p->pTagRef[i];
×
113
      pRef[i].hasRef = pTagRef->hasRef;
×
114
      pRef[i].id = pTagRef->id;
×
115
      if (pRef[i].hasRef) {
×
116
        tstrncpy(pRef[i].refDbName, pTagRef->refDbName, TSDB_DB_NAME_LEN);
×
117
        tstrncpy(pRef[i].refTableName, pTagRef->refTableName, TSDB_TABLE_NAME_LEN);
×
118
        tstrncpy(pRef[i].refColName, pTagRef->refColName, TSDB_COL_NAME_LEN);
×
119
      }
120
    }
121
  }
122
  return 0;
×
123
}
124

125
int32_t vnodeGetTableMeta(SVnode *pVnode, SRpcMsg *pMsg, bool direct) {
34,076,694✔
126
  STableInfoReq  infoReq = {0};
34,076,694✔
127
  STableMetaRsp  metaRsp = {0};
34,076,694✔
128
  SMetaReader    mer1 = {0};
34,076,694✔
129
  SMetaReader    mer2 = {0};
34,076,694✔
130
  char           tableFName[TSDB_TABLE_FNAME_LEN];
34,072,016✔
131
  bool           reqTbUid = false;
34,075,541✔
132
  SRpcMsg        rpcMsg = {0};
34,075,541✔
133
  int32_t        code = 0;
34,075,541✔
134
  int32_t        rspLen = 0;
34,075,541✔
135
  void          *pRsp = NULL;
34,075,541✔
136
  SSchemaWrapper schema = {0};
34,075,541✔
137
  SSchemaWrapper schemaTag = {0};
34,075,541✔
138
  uint8_t        autoCreateCtb = 0;
34,075,541✔
139

140
  // decode req
141
  if (tDeserializeSTableInfoReq(pMsg->pCont, pMsg->contLen, &infoReq) != 0) {
34,075,541✔
142
    code = terrno;
×
143
    goto _exit4;
×
144
  }
145
  autoCreateCtb = infoReq.autoCreateCtb;
34,077,294✔
146

147
  if (infoReq.option == REQ_OPT_TBUID) reqTbUid = true;
34,077,294✔
148
  metaRsp.dbId = pVnode->config.dbId;
34,077,294✔
149
  tstrncpy(metaRsp.tbName, infoReq.tbName, TSDB_TABLE_NAME_LEN);
34,076,139✔
150
  (void)memcpy(metaRsp.dbFName, infoReq.dbFName, sizeof(metaRsp.dbFName));
34,076,265✔
151

152
  if (!reqTbUid) {
34,076,265✔
153
    (void)snprintf(tableFName, TSDB_TABLE_FNAME_LEN, "%s.%s", infoReq.dbFName, infoReq.tbName);
33,784,899✔
154
    if (pVnode->mounted) tTrimMountPrefix(tableFName);
33,784,899✔
155
    code = vnodeValidateTableHash(pVnode, tableFName);
33,786,223✔
156
    if (code) {
33,785,930✔
157
      goto _exit4;
×
158
    }
159
  }
160

161
  // query meta
162
  metaReaderDoInit(&mer1, pVnode->pMeta, META_READER_LOCK);
34,077,296✔
163
  if (reqTbUid) {
34,076,863✔
164
    SET_ERRNO(0);
290,640✔
165
    uint64_t tbUid = taosStr2UInt64(infoReq.tbName, NULL, 10);
290,640✔
166
    if (ERRNO == ERANGE || tbUid == 0) {
290,400✔
167
      code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
168
      goto _exit3;
×
169
    }
170
    SMetaReader mr3 = {0};
290,640✔
171
    metaReaderDoInit(&mr3, ((SVnode *)pVnode)->pMeta, META_READER_NOLOCK);
290,400✔
172
    if ((code = metaReaderGetTableEntryByUid(&mr3, tbUid)) < 0) {
290,640✔
173
      metaReaderClear(&mr3);
106,080✔
174
      TAOS_CHECK_GOTO(code, NULL, _exit3);
106,080✔
175
    }
176
    tstrncpy(metaRsp.tbName, mr3.me.name, TSDB_TABLE_NAME_LEN);
184,560✔
177
    metaReaderClear(&mr3);
184,560✔
178
    TAOS_CHECK_GOTO(metaGetTableEntryByName(&mer1, metaRsp.tbName), NULL, _exit3);
184,560✔
179
  } else if (metaGetTableEntryByName(&mer1, infoReq.tbName) < 0) {
33,786,223✔
180
    code = terrno;
10,795,778✔
181
    goto _exit3;
10,794,489✔
182
  }
183

184
  metaRsp.tableType = mer1.me.type;
23,172,359✔
185
  metaRsp.vgId = TD_VID(pVnode);
23,172,359✔
186
  metaRsp.tuid = mer1.me.uid;
23,173,648✔
187
  metaRsp.isAudit = pVnode->config.isAudit ? 1 : 0;
23,173,648✔
188

189
  switch (mer1.me.type) {
23,174,248✔
190
    case TSDB_SUPER_TABLE: {
4,359,003✔
191
      tstrncpy(metaRsp.stbName, mer1.me.name, sizeof(metaRsp.stbName));
4,359,003✔
192
      schema = mer1.me.stbEntry.schemaRow;
4,359,003✔
193
      schemaTag = mer1.me.stbEntry.schemaTag;
4,359,003✔
194
      metaRsp.suid = mer1.me.uid;
4,359,003✔
195
      metaRsp.virtualStb = TABLE_IS_VIRTUAL(mer1.me.flags);
4,359,003✔
196
      metaRsp.ownerId = mer1.me.stbEntry.ownerId;
4,359,003✔
197
      metaRsp.secLvl = mer1.me.stbEntry.securityLevel;
4,359,003✔
198
      break;
4,359,003✔
199
    }
200
    case TSDB_CHILD_TABLE:
14,679,101✔
201
    case TSDB_VIRTUAL_CHILD_TABLE:{
202
      metaReaderDoInit(&mer2, pVnode->pMeta, META_READER_NOLOCK);
14,679,101✔
203
      if (metaReaderGetTableEntryByUid(&mer2, mer1.me.ctbEntry.suid) < 0) goto _exit2;
14,680,301✔
204

205
      tstrncpy(metaRsp.stbName, mer2.me.name, sizeof(metaRsp.stbName));
14,679,701✔
206
      metaRsp.suid = mer2.me.uid;
14,679,271✔
207
      metaRsp.ownerId = mer2.me.stbEntry.ownerId;  // child table inherits ownerId from stb
14,679,271✔
208
      metaRsp.secLvl = mer2.me.stbEntry.securityLevel;  // child table inherits secLvl from stb
14,679,271✔
209
      schema = mer2.me.stbEntry.schemaRow;
14,679,271✔
210
      schemaTag = mer2.me.stbEntry.schemaTag;
14,679,271✔
211
      break;
14,679,271✔
212
    }
213
    case TSDB_NORMAL_TABLE:
3,980,899✔
214
      metaRsp.secLvl = pVnode->config.securityLevel;  // normal table inherits secLvl from vnode config
3,980,899✔
215
    case TSDB_VIRTUAL_NORMAL_TABLE: {
4,134,739✔
216
      schema = mer1.me.ntbEntry.schemaRow;
4,134,739✔
217
      metaRsp.ownerId = mer1.me.ntbEntry.ownerId;
4,134,739✔
218
      break;
4,134,739✔
219
    }
220
    default: {
×
221
      vError("vnodeGetTableMeta get invalid table type:%d", mer1.me.type);
×
222
      goto _exit3;
×
223
    }
224
  }
225

226
  metaRsp.numOfTags = schemaTag.nCols;
23,173,013✔
227
  metaRsp.numOfColumns = schema.nCols;
23,173,013✔
228
  metaRsp.precision = pVnode->config.tsdbCfg.precision;
23,173,013✔
229
  metaRsp.sversion = schema.version;
23,172,843✔
230
  metaRsp.tversion = schemaTag.version;
23,172,843✔
231
  metaRsp.pSchemas = (SSchema *)taosMemoryMalloc(sizeof(SSchema) * (metaRsp.numOfColumns + metaRsp.numOfTags));
23,172,843✔
232
  metaRsp.pSchemaExt = (SSchemaExt *)taosMemoryCalloc(metaRsp.numOfColumns, sizeof(SSchemaExt));
23,172,397✔
233
  if (NULL == metaRsp.pSchemas || NULL == metaRsp.pSchemaExt) {
23,172,821✔
234
    code = terrno;
×
235
    goto _exit;
×
236
  }
237
  (void)memcpy(metaRsp.pSchemas, schema.pSchema, sizeof(SSchema) * schema.nCols);
23,173,421✔
238
  if (schemaTag.nCols) {
23,173,421✔
239
    (void)memcpy(metaRsp.pSchemas + schema.nCols, schemaTag.pSchema, sizeof(SSchema) * schemaTag.nCols);
19,038,704✔
240
  }
241
  if (metaRsp.pSchemaExt) {
23,173,217✔
242
    SMetaReader *pReader =
23,175,007✔
243
        (mer1.me.type == TSDB_CHILD_TABLE || mer1.me.type == TSDB_VIRTUAL_CHILD_TABLE) ? &mer2 : &mer1;
23,175,007✔
244
    code = fillTableColCmpr(pReader, metaRsp.pSchemaExt, metaRsp.numOfColumns);
23,175,007✔
245
    if (code < 0) {
23,173,567✔
246
      goto _exit;
×
247
    }
248
    for (int32_t i = 0; i < metaRsp.numOfColumns; i++) {
273,627,164✔
249
      metaRsp.pSchemaExt[i].colId = schema.pSchema[i].colId;
250,454,055✔
250
      if (pReader->me.pExtSchemas) {
250,454,509✔
251
        metaRsp.pSchemaExt[i].typeMod = pReader->me.pExtSchemas[i].typeMod;
32,462,625✔
252
      }
253
    }
254
  } else {
UNCOV
255
    code = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
256
    goto _exit;
×
257
  }
258
  if (hasRefCol(mer1.me.type)) {
23,173,109✔
259
    metaRsp.rversion = mer1.me.colRef.version;
402,379✔
260
    metaRsp.pColRefs = (SColRef*)taosMemoryMalloc(sizeof(SColRef) * metaRsp.numOfColumns);
402,379✔
261
    if (metaRsp.pColRefs) {
401,289✔
262
      code = fillTableColRef(&mer1, metaRsp.pColRefs, metaRsp.numOfColumns);
401,969✔
263
      if (code < 0) {
401,969✔
264
        goto _exit;
×
265
      }
266
    }
267
    metaRsp.numOfColRefs = metaRsp.numOfColumns;
401,289✔
268

269
    // Fill tag references
270
    if (mer1.me.colRef.nTagRefs > 0) {
401,289✔
271
      metaRsp.pTagRefs = (SColRef*)taosMemoryMalloc(sizeof(SColRef) * mer1.me.colRef.nTagRefs);
×
272
      if (metaRsp.pTagRefs) {
×
273
        code = fillTableTagRef(&mer1, metaRsp.pTagRefs, mer1.me.colRef.nTagRefs);
×
274
        if (code < 0) {
×
275
          taosMemoryFreeClear(metaRsp.pTagRefs);
×
276
          goto _exit;
×
277
        }
278
      } else {
279
        code = terrno;
×
280
        goto _exit;
×
281
      }
282
      metaRsp.numOfTagRefs = mer1.me.colRef.nTagRefs;
×
283
    } else {
284
      metaRsp.pTagRefs = NULL;
401,289✔
285
      metaRsp.numOfTagRefs = 0;
401,289✔
286
    }
287
  } else {
288
    metaRsp.pColRefs = NULL;
22,771,598✔
289
    metaRsp.numOfColRefs = 0;
22,771,598✔
290
    metaRsp.pTagRefs = NULL;
22,771,598✔
291
    metaRsp.numOfTagRefs = 0;
22,771,598✔
292
  }
293

294
  vnodeDebugTableMeta(&metaRsp);
23,172,887✔
295

296
  // encode and send response
297
  rspLen = tSerializeSTableMetaRsp(NULL, 0, &metaRsp);
23,174,407✔
298
  if (rspLen < 0) {
23,173,807✔
299
    code = terrno;
×
300
    goto _exit;
×
301
  }
302

303
  if (direct) {
23,173,807✔
304
    pRsp = rpcMallocCont(rspLen);
608,986✔
305
  } else {
306
    pRsp = taosMemoryCalloc(1, rspLen);
22,564,821✔
307
  }
308

309
  if (pRsp == NULL) {
23,173,551✔
310
    code = terrno;
×
311
    goto _exit;
×
312
  }
313

314
  rspLen = tSerializeSTableMetaRsp(pRsp, rspLen, &metaRsp);
23,173,551✔
315
  if (rspLen < 0) {
23,175,007✔
316
    code = terrno;
×
317
    goto _exit;
×
318
  }
319

320
_exit:
23,173,217✔
321
  taosMemoryFree(metaRsp.pColRefs);
23,175,007✔
322
  taosMemoryFree(metaRsp.pSchemas);
23,171,783✔
323
  taosMemoryFree(metaRsp.pSchemaExt);
23,172,799✔
324
  taosMemoryFree(metaRsp.pTagRefs);
23,172,942✔
325
_exit2:
23,172,552✔
326
  metaReaderClear(&mer2);
23,174,043✔
327
_exit3:
34,073,373✔
328
  metaReaderClear(&mer1);
34,075,191✔
329
_exit4:
34,075,514✔
330
  rpcMsg.info = pMsg->info;
34,075,156✔
331
  rpcMsg.pCont = pRsp;
34,075,665✔
332
  rpcMsg.contLen = rspLen;
34,075,665✔
333
  rpcMsg.code = code;
34,075,665✔
334
  rpcMsg.msgType = pMsg->msgType;
34,075,665✔
335

336
  if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST && autoCreateCtb == 1) {
34,075,425✔
337
    code = TSDB_CODE_SUCCESS;
6,522,089✔
338
  }
339

340
  if (code) {
34,075,425✔
341
    qError("vgId:%d, get table %s meta with %" PRIu8 " failed cause of %s", pVnode->config.vgId, infoReq.tbName,
4,379,338✔
342
           infoReq.option, tstrerror(code));
343
  }
344

345
  if (direct) {
34,074,194✔
346
    tmsgSendRsp(&rpcMsg);
1,466,459✔
347
  } else {
348
    *pMsg = rpcMsg;
32,607,735✔
349
  }
350

351
  return code;
34,075,730✔
352
}
353

354
int32_t vnodeGetTableCfg(SVnode *pVnode, SRpcMsg *pMsg, bool direct) {
73,073✔
355
  STableCfgReq   cfgReq = {0};
73,073✔
356
  STableCfgRsp   cfgRsp = {0};
73,073✔
357
  SMetaReader    mer1 = {0};
73,073✔
358
  SMetaReader    mer2 = {0};
73,073✔
359
  char           tableFName[TSDB_TABLE_FNAME_LEN];
73,073✔
360
  SRpcMsg        rpcMsg = {0};
73,073✔
361
  int32_t        code = 0;
73,073✔
362
  int32_t        rspLen = 0;
73,073✔
363
  void          *pRsp = NULL;
73,073✔
364
  SSchemaWrapper schema = {0};
73,073✔
365
  SSchemaWrapper schemaTag = {0};
73,073✔
366

367
  // decode req
368
  if (tDeserializeSTableCfgReq(pMsg->pCont, pMsg->contLen, &cfgReq) != 0) {
73,073✔
369
    code = terrno;
×
370
    goto _exit;
×
371
  }
372

373
  tstrncpy(cfgRsp.tbName, cfgReq.tbName, TSDB_TABLE_NAME_LEN);
73,073✔
374
  (void)memcpy(cfgRsp.dbFName, cfgReq.dbFName, sizeof(cfgRsp.dbFName));
73,073✔
375

376
  (void)snprintf(tableFName, TSDB_TABLE_FNAME_LEN, "%s.%s", cfgReq.dbFName, cfgReq.tbName);
73,073✔
377
  if (pVnode->mounted) tTrimMountPrefix(tableFName);
73,073✔
378
  code = vnodeValidateTableHash(pVnode, tableFName);
73,073✔
379
  if (code) {
73,073✔
380
    goto _exit;
×
381
  }
382

383
  // query meta
384
  metaReaderDoInit(&mer1, pVnode->pMeta, META_READER_LOCK);
73,073✔
385

386
  if (metaGetTableEntryByName(&mer1, cfgReq.tbName) < 0) {
73,073✔
387
    code = terrno;
×
388
    goto _exit;
×
389
  }
390

391
  cfgRsp.tableType = mer1.me.type;
73,073✔
392
  cfgRsp.isAudit = pVnode->config.isAudit ? 1 : 0;
73,073✔
393
  cfgRsp.secureDelete = pVnode->config.secureDelete;
73,073✔
394

395
  if (mer1.me.type == TSDB_SUPER_TABLE) {
73,073✔
396
    code = TSDB_CODE_VND_HASH_MISMATCH;
×
397
    goto _exit;
×
398
  } else if (mer1.me.type == TSDB_CHILD_TABLE || mer1.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
73,073✔
399
    metaReaderDoInit(&mer2, pVnode->pMeta, META_READER_NOLOCK);
51,234✔
400
    if (metaReaderGetTableEntryByUid(&mer2, mer1.me.ctbEntry.suid) < 0) goto _exit;
51,234✔
401

402
    tstrncpy(cfgRsp.stbName, mer2.me.name, TSDB_TABLE_NAME_LEN);
51,234✔
403
    schema = mer2.me.stbEntry.schemaRow;
51,234✔
404
    schemaTag = mer2.me.stbEntry.schemaTag;
51,234✔
405
    cfgRsp.ownerId = mer2.me.stbEntry.ownerId;        // child table inherits ownerId from stb
51,234✔
406
    cfgRsp.securityLevel = mer2.me.stbEntry.securityLevel;  // child table inherits secLvl from stb
51,234✔
407
    cfgRsp.ttl = mer1.me.ctbEntry.ttlDays;
51,234✔
408
    cfgRsp.commentLen = mer1.me.ctbEntry.commentLen;
51,234✔
409
    if (mer1.me.ctbEntry.commentLen > 0) {
51,234✔
410
      cfgRsp.pComment = taosStrdup(mer1.me.ctbEntry.comment);
×
411
      if (NULL == cfgRsp.pComment) {
×
412
        code = terrno;
×
413
        goto _exit;
×
414
      }
415
    }
416
    STag *pTag = (STag *)mer1.me.ctbEntry.pTags;
51,234✔
417
    cfgRsp.tagsLen = pTag->len;
51,234✔
418
    cfgRsp.pTags = taosMemoryMalloc(cfgRsp.tagsLen);
51,234✔
419
    if (NULL == cfgRsp.pTags) {
51,234✔
420
      code = terrno;
×
421
      goto _exit;
×
422
    }
423
    (void)memcpy(cfgRsp.pTags, pTag, cfgRsp.tagsLen);
51,234✔
424
  } else if (mer1.me.type == TSDB_NORMAL_TABLE || mer1.me.type == TSDB_VIRTUAL_NORMAL_TABLE) {
21,839✔
425
    schema = mer1.me.ntbEntry.schemaRow;
21,839✔
426
    cfgRsp.ttl = mer1.me.ntbEntry.ttlDays;
21,839✔
427
    cfgRsp.ownerId = mer1.me.ntbEntry.ownerId;
21,839✔
428
    cfgRsp.securityLevel = mer1.me.type == TSDB_NORMAL_TABLE ? pVnode->config.securityLevel : 0;
21,839✔
429
    cfgRsp.commentLen = mer1.me.ntbEntry.commentLen;
21,839✔
430
    if (mer1.me.ntbEntry.commentLen > 0) {
21,839✔
431
      cfgRsp.pComment = taosStrdup(mer1.me.ntbEntry.comment);
×
432
      if (NULL == cfgRsp.pComment) {
×
433
        code = terrno;
×
434
        goto _exit;
×
435
      }
436
    }
437
  } else {
438
    vError("vnodeGetTableCfg get invalid table type:%d", mer1.me.type);
×
439
    code = TSDB_CODE_APP_ERROR;
×
440
    goto _exit;
×
441
  }
442

443
  cfgRsp.numOfTags = schemaTag.nCols;
73,073✔
444
  cfgRsp.numOfColumns = schema.nCols;
73,073✔
445
  cfgRsp.virtualStb = false; // vnode don't have super table, so it's always false
73,073✔
446
  cfgRsp.pSchemas = (SSchema *)taosMemoryMalloc(sizeof(SSchema) * (cfgRsp.numOfColumns + cfgRsp.numOfTags));
73,073✔
447
  cfgRsp.pSchemaExt = (SSchemaExt *)taosMemoryCalloc(cfgRsp.numOfColumns, sizeof(SSchemaExt));
73,073✔
448
  cfgRsp.pColRefs = (SColRef *)taosMemoryMalloc(sizeof(SColRef) * cfgRsp.numOfColumns);
73,073✔
449
  cfgRsp.numOfTagRefs = 0;
73,073✔
450
  cfgRsp.pTagRefs = NULL;
73,073✔
451

452
  if (NULL == cfgRsp.pSchemas || NULL == cfgRsp.pSchemaExt || NULL == cfgRsp.pColRefs) {
73,073✔
453
    code = terrno;
×
454
    goto _exit;
×
455
  }
456
  (void)memcpy(cfgRsp.pSchemas, schema.pSchema, sizeof(SSchema) * schema.nCols);
73,073✔
457
  if (schemaTag.nCols) {
73,073✔
458
    (void)memcpy(cfgRsp.pSchemas + schema.nCols, schemaTag.pSchema, sizeof(SSchema) * schemaTag.nCols);
51,234✔
459
  }
460

461
  SMetaReader    *pReader = (mer1.me.type == TSDB_CHILD_TABLE || mer1.me.type == TSDB_VIRTUAL_CHILD_TABLE) ? &mer2 : &mer1;
73,073✔
462
  SColRefWrapper *pColRef = &mer1.me.colRef;
73,073✔
463

464
  if (withExtSchema(cfgRsp.tableType)) {
73,073✔
465
    code = fillTableColCmpr(pReader, cfgRsp.pSchemaExt, cfgRsp.numOfColumns);
73,073✔
466
    if (code < 0) {
73,073✔
467
      goto _exit;
×
468
    }
469

470
    for (int32_t i = 0; i < cfgRsp.numOfColumns; i++) {
391,141✔
471
      SSchemaExt *pSchExt = cfgRsp.pSchemaExt + i;
318,068✔
472
      pSchExt->colId = schema.pSchema[i].colId;
318,068✔
473
      if (pReader->me.pExtSchemas) {
318,068✔
474
        pSchExt->typeMod = pReader->me.pExtSchemas[i].typeMod;
35,316✔
475
      }
476
    }
477
  }
478

479
  cfgRsp.virtualStb = false;
73,073✔
480
  if (hasRefCol(cfgRsp.tableType)) {
73,073✔
481
    for (int32_t i = 0; i < cfgRsp.numOfColumns; i++) {
132,121✔
482
      SColRef *pRef = &pColRef->pColRef[i];
121,370✔
483
      cfgRsp.pColRefs[i].hasRef = pRef->hasRef;
121,370✔
484
      cfgRsp.pColRefs[i].id = pRef->id;
121,370✔
485
      if (cfgRsp.pColRefs[i].hasRef) {
121,370✔
486
        tstrncpy(cfgRsp.pColRefs[i].refDbName, pRef->refDbName, TSDB_DB_NAME_LEN);
65,244✔
487
        tstrncpy(cfgRsp.pColRefs[i].refTableName, pRef->refTableName, TSDB_TABLE_NAME_LEN);
65,244✔
488
        tstrncpy(cfgRsp.pColRefs[i].refColName, pRef->refColName, TSDB_COL_NAME_LEN);
65,244✔
489
      }
490
    }
491

492
    cfgRsp.numOfTagRefs = pColRef->nTagRefs;
10,751✔
493
    if (cfgRsp.numOfTagRefs > 0) {
10,751✔
494
      if (NULL == pColRef->pTagRef) {
×
495
        code = TSDB_CODE_APP_ERROR;
×
496
        goto _exit;
×
497
      }
498
      cfgRsp.pTagRefs = (SColRef *)taosMemoryMalloc(sizeof(SColRef) * cfgRsp.numOfTagRefs);
×
499
      if (NULL == cfgRsp.pTagRefs) {
×
500
        code = terrno;
×
501
        goto _exit;
×
502
      }
503

504
      for (int32_t i = 0; i < cfgRsp.numOfTagRefs; i++) {
×
505
        SColRef *pRef = &pColRef->pTagRef[i];
×
506
        cfgRsp.pTagRefs[i].hasRef = pRef->hasRef;
×
507
        cfgRsp.pTagRefs[i].id = pRef->id;
×
508
        if (cfgRsp.pTagRefs[i].hasRef) {
×
509
          tstrncpy(cfgRsp.pTagRefs[i].refDbName, pRef->refDbName, TSDB_DB_NAME_LEN);
×
510
          tstrncpy(cfgRsp.pTagRefs[i].refTableName, pRef->refTableName, TSDB_TABLE_NAME_LEN);
×
511
          tstrncpy(cfgRsp.pTagRefs[i].refColName, pRef->refColName, TSDB_COL_NAME_LEN);
×
512
        }
513
      }
514
    }
515
  }
516

517
  // encode and send response
518
  rspLen = tSerializeSTableCfgRsp(NULL, 0, &cfgRsp);
73,073✔
519
  if (rspLen < 0) {
73,073✔
520
    code = terrno;
×
521
    goto _exit;
×
522
  }
523

524
  if (direct) {
73,073✔
525
    pRsp = rpcMallocCont(rspLen);
×
526
  } else {
527
    pRsp = taosMemoryCalloc(1, rspLen);
73,073✔
528
  }
529

530
  if (pRsp == NULL) {
73,073✔
531
    code = terrno;
×
532
    goto _exit;
×
533
  }
534

535
  rspLen = tSerializeSTableCfgRsp(pRsp, rspLen, &cfgRsp);
73,073✔
536
  if (rspLen < 0) {
73,073✔
537
    code = terrno;
×
538
    goto _exit;
×
539
  }
540

541
_exit:
73,073✔
542
  rpcMsg.info = pMsg->info;
73,073✔
543
  rpcMsg.pCont = pRsp;
73,073✔
544
  rpcMsg.contLen = rspLen;
73,073✔
545
  rpcMsg.code = code;
73,073✔
546
  rpcMsg.msgType = pMsg->msgType;
73,073✔
547

548
  if (code) {
73,073✔
549
    qError("get table %s cfg failed cause of %s", cfgReq.tbName, tstrerror(code));
×
550
  }
551

552
  if (direct) {
73,073✔
553
    tmsgSendRsp(&rpcMsg);
×
554
  } else {
555
    *pMsg = rpcMsg;
73,073✔
556
  }
557

558
  tFreeSTableCfgRsp(&cfgRsp);
73,073✔
559
  metaReaderClear(&mer2);
73,073✔
560
  metaReaderClear(&mer1);
73,073✔
561
  return code;
73,073✔
562
}
563

564
static FORCE_INLINE void vnodeFreeSBatchRspMsg(void *p) {
565
  if (NULL == p) {
566
    return;
567
  }
568

569
  SBatchRspMsg *pRsp = (SBatchRspMsg *)p;
570
  rpcFreeCont(pRsp->msg);
571
}
572

573
int32_t vnodeGetBatchMeta(SVnode *pVnode, SRpcMsg *pMsg) {
31,202,473✔
574
  int32_t      code = 0;
31,202,473✔
575
  int32_t      rspSize = 0;
31,202,473✔
576
  SBatchReq    batchReq = {0};
31,202,473✔
577
  SBatchMsg   *req = NULL;
31,203,111✔
578
  SBatchRspMsg rsp = {0};
31,203,111✔
579
  SBatchRsp    batchRsp = {0};
31,191,505✔
580
  SRpcMsg      reqMsg = *pMsg;
31,190,168✔
581
  SRpcMsg      rspMsg = {0};
31,190,818✔
582
  void        *pRsp = NULL;
31,195,557✔
583

584
  if (tDeserializeSBatchReq(pMsg->pCont, pMsg->contLen, &batchReq)) {
31,195,557✔
585
    code = terrno;
×
586
    qError("tDeserializeSBatchReq failed");
×
587
    goto _exit;
×
588
  }
589

590
  int32_t msgNum = taosArrayGetSize(batchReq.pMsgs);
31,204,441✔
591
  if (msgNum >= MAX_META_MSG_IN_BATCH) {
31,198,476✔
592
    code = TSDB_CODE_INVALID_MSG;
×
593
    qError("too many msgs %d in vnode batch meta req", msgNum);
×
594
    goto _exit;
×
595
  }
596

597
  batchRsp.pRsps = taosArrayInit(msgNum, sizeof(SBatchRspMsg));
31,198,476✔
598
  if (NULL == batchRsp.pRsps) {
31,193,289✔
599
    code = terrno;
×
600
    qError("taosArrayInit %d SBatchRspMsg failed", msgNum);
×
601
    goto _exit;
×
602
  }
603

604
  for (int32_t i = 0; i < msgNum; ++i) {
68,508,765✔
605
    req = taosArrayGet(batchReq.pMsgs, i);
37,308,761✔
606
    if (req == NULL) {
37,307,638✔
607
      code = terrno;
×
608
      goto _exit;
×
609
    }
610

611
    reqMsg.msgType = req->msgType;
37,307,638✔
612
    reqMsg.pCont = req->msg;
37,303,924✔
613
    reqMsg.contLen = req->msgLen;
37,300,620✔
614

615
    switch (req->msgType) {
37,304,850✔
616
      case TDMT_VND_TABLE_META:
32,320,195✔
617
        // error code has been set into reqMsg, no need to handle it here.
618
        if (TSDB_CODE_SUCCESS != vnodeGetTableMeta(pVnode, &reqMsg, false)) {
32,320,195✔
619
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
3,416,645✔
620
        }
621
        break;
32,318,255✔
622
      case TDMT_VND_TABLE_NAME:
290,640✔
623
        // error code has been set into reqMsg, no need to handle it here.
624
        if (TSDB_CODE_SUCCESS != vnodeGetTableMeta(pVnode, &reqMsg, false)) {
290,640✔
625
          qWarn("vnodeGetBatchName failed, msgType:%d", req->msgType);
106,080✔
626
        }
627
        break;
290,160✔
628
      case TDMT_VND_TABLE_CFG:
73,073✔
629
        // error code has been set into reqMsg, no need to handle it here.
630
        if (TSDB_CODE_SUCCESS != vnodeGetTableCfg(pVnode, &reqMsg, false)) {
73,073✔
631
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
×
632
        }
633
        break;
73,073✔
634
      case TDMT_VND_VSUBTABLES_META:
×
635
        // error code has been set into reqMsg, no need to handle it here.
636
        if (TSDB_CODE_SUCCESS != vnodeGetVSubtablesMeta(pVnode, &reqMsg)) {
×
637
          qWarn("vnodeGetVSubtablesMeta failed, msgType:%d", req->msgType);
×
638
        }
639
        break;
×
640
      case TDMT_VND_VSTB_REF_DBS:
4,626,177✔
641
        // error code has been set into reqMsg, no need to handle it here.
642
        if (TSDB_CODE_SUCCESS != vnodeGetVStbRefDbs(pVnode, &reqMsg)) {
4,626,177✔
643
          qWarn("vnodeGetVStbRefDbs failed, msgType:%d", req->msgType);
×
644
        }
645
        break;
4,622,210✔
646
      default:
×
647
        qError("invalid req msgType %d", req->msgType);
×
648
        reqMsg.code = TSDB_CODE_INVALID_MSG;
×
649
        reqMsg.pCont = NULL;
×
650
        reqMsg.contLen = 0;
×
651
        break;
×
652
    }
653

654
    rsp.msgIdx = req->msgIdx;
37,303,698✔
655
    rsp.reqType = reqMsg.msgType;
37,305,317✔
656
    rsp.msgLen = reqMsg.contLen;
37,305,317✔
657
    rsp.rspCode = reqMsg.code;
37,305,317✔
658
    rsp.msg = reqMsg.pCont;
37,305,317✔
659

660
    if (NULL == taosArrayPush(batchRsp.pRsps, &rsp)) {
74,612,265✔
661
      qError("taosArrayPush failed");
×
662
      code = terrno;
×
663
      goto _exit;
×
664
    }
665
  }
666

667
  rspSize = tSerializeSBatchRsp(NULL, 0, &batchRsp);
31,200,004✔
668
  if (rspSize < 0) {
31,193,272✔
669
    qError("tSerializeSBatchRsp failed");
×
670
    code = terrno;
×
671
    goto _exit;
×
672
  }
673
  pRsp = rpcMallocCont(rspSize);
31,193,272✔
674
  if (pRsp == NULL) {
31,188,060✔
675
    qError("rpcMallocCont %d failed", rspSize);
×
676
    code = terrno;
×
677
    goto _exit;
×
678
  }
679
  if (tSerializeSBatchRsp(pRsp, rspSize, &batchRsp) < 0) {
31,188,060✔
680
    qError("tSerializeSBatchRsp %d failed", rspSize);
×
681
    code = terrno;
×
682
    goto _exit;
×
683
  }
684

685
_exit:
31,191,028✔
686

687
  rspMsg.info = pMsg->info;
31,199,745✔
688
  rspMsg.pCont = pRsp;
31,198,557✔
689
  rspMsg.contLen = rspSize;
31,198,557✔
690
  rspMsg.code = code;
31,198,557✔
691
  rspMsg.msgType = pMsg->msgType;
31,198,557✔
692

693
  if (code) {
31,187,802✔
694
    qError("vnd get batch meta failed cause of %s", tstrerror(code));
×
695
  }
696

697
  taosArrayDestroyEx(batchReq.pMsgs, tFreeSBatchReqMsg);
31,187,802✔
698
  taosArrayDestroyEx(batchRsp.pRsps, tFreeSBatchRspMsg);
31,185,679✔
699

700
  tmsgSendRsp(&rspMsg);
31,177,593✔
701

702
  return code;
31,202,862✔
703
}
704

705
#define VNODE_DO_META_QUERY(pVnode, cmd)                 \
706
  do {                                                   \
707
    (void)taosThreadRwlockRdlock(&(pVnode)->metaRWLock); \
708
    cmd;                                                 \
709
    (void)taosThreadRwlockUnlock(&(pVnode)->metaRWLock); \
710
  } while (0)
711

712
int32_t vnodeReadVSubtables(SReadHandle* pHandle, int64_t suid, SArray** ppRes) {
×
713
  int32_t                    code = TSDB_CODE_SUCCESS;
×
714
  int32_t                    line = 0;
×
715
  SMetaReader                mr = {0};
×
716
  bool                       readerInit = false;
×
717
  SVCTableRefCols*           pTb = NULL;
×
718
  int32_t                    refColsNum = 0;
×
719
  char                       tbFName[TSDB_TABLE_FNAME_LEN];
×
720
  SSHashObj*                 pSrcTbls = NULL;
×
721

722
  SArray *pList = taosArrayInit(10, sizeof(uint64_t));
×
723
  QUERY_CHECK_NULL(pList, code, line, _return, terrno);
×
724

725
  QUERY_CHECK_CODE(pHandle->api.metaFn.getChildTableList(pHandle->vnode, suid, pList), line, _return);
×
726

727
  size_t num = taosArrayGetSize(pList);
×
728
  *ppRes = taosArrayInit(num, POINTER_BYTES);
×
729
  QUERY_CHECK_NULL(*ppRes, code, line, _return, terrno);
×
730
  pSrcTbls = tSimpleHashInit(10, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
×
731
  QUERY_CHECK_NULL(pSrcTbls, code, line, _return, terrno);
×
732

733
  for (int32_t i = 0; i < num; ++i) {
×
734
    uint64_t* id = taosArrayGet(pList, i);
×
735
    QUERY_CHECK_NULL(id, code, line, _return, terrno);
×
736
    pHandle->api.metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pHandle->api.metaFn);
×
737
    QUERY_CHECK_CODE(pHandle->api.metaReaderFn.getTableEntryByUid(&mr, *id), line, _return);
×
738
    readerInit = true;
×
739

740
    refColsNum = 0;
×
741
    for (int32_t j = 0; j < mr.me.colRef.nCols; j++) {
×
742
      if (mr.me.colRef.pColRef[j].hasRef) {
×
743
        refColsNum++;
×
744
      }
745
    }
746

747
    if (refColsNum <= 0) {
×
748
      pHandle->api.metaReaderFn.clearReader(&mr);
×
749
      readerInit = false;
×
750
      continue;
×
751
    }
752

753
    pTb = taosMemoryCalloc(1, refColsNum * sizeof(SRefColInfo) + sizeof(*pTb));
×
754
    QUERY_CHECK_NULL(pTb, code, line, _return, terrno);
×
755

756
    pTb->uid = mr.me.uid;
×
757
    pTb->numOfColRefs = refColsNum;
×
758
    pTb->refCols = (SRefColInfo*)(pTb + 1);
×
759

760
    refColsNum = 0;
×
761
    tSimpleHashClear(pSrcTbls);
×
762
    for (int32_t j = 0; j < mr.me.colRef.nCols; j++) {
×
763
      if (!mr.me.colRef.pColRef[j].hasRef) {
×
764
        continue;
×
765
      }
766

767
      pTb->refCols[refColsNum].colId = mr.me.colRef.pColRef[j].id;
×
768
      tstrncpy(pTb->refCols[refColsNum].refColName, mr.me.colRef.pColRef[j].refColName, TSDB_COL_NAME_LEN);
×
769
      tstrncpy(pTb->refCols[refColsNum].refTableName, mr.me.colRef.pColRef[j].refTableName, TSDB_TABLE_NAME_LEN);
×
770
      tstrncpy(pTb->refCols[refColsNum].refDbName, mr.me.colRef.pColRef[j].refDbName, TSDB_DB_NAME_LEN);
×
771

772
      snprintf(tbFName, sizeof(tbFName), "%s.%s", pTb->refCols[refColsNum].refDbName, pTb->refCols[refColsNum].refTableName);
×
773

774
      if (NULL == tSimpleHashGet(pSrcTbls, tbFName, strlen(tbFName))) {
×
775
        QUERY_CHECK_CODE(tSimpleHashPut(pSrcTbls, tbFName, strlen(tbFName), &code, sizeof(code)), line, _return);
×
776
      }
777

778
      refColsNum++;
×
779
    }
780

781
    pTb->numOfSrcTbls = tSimpleHashGetSize(pSrcTbls);
×
782
    QUERY_CHECK_NULL(taosArrayPush(*ppRes, &pTb), code, line, _return, terrno);
×
783
    pTb = NULL;
×
784

785
    pHandle->api.metaReaderFn.clearReader(&mr);
×
786
    readerInit = false;
×
787
  }
788

789
_return:
×
790

791
  if (readerInit) {
×
792
    pHandle->api.metaReaderFn.clearReader(&mr);
×
793
  }
794

795
  taosArrayDestroy(pList);
×
796
  taosMemoryFree(pTb);
×
797
  tSimpleHashCleanup(pSrcTbls);
×
798

799
  if (code) {
×
800
    qError("%s failed since %s", __func__, tstrerror(code));
×
801
  }
802
  return code;
×
803
}
804

805
int32_t vnodeReadVStbRefDbs(SReadHandle* pHandle, int64_t suid, SArray** ppRes) {
4,620,799✔
806
  int32_t                    code = TSDB_CODE_SUCCESS;
4,620,799✔
807
  int32_t                    line = 0;
4,620,799✔
808
  SMetaReader                mr = {0};
4,620,799✔
809
  bool                       readerInit = false;
4,620,103✔
810
  SSHashObj*                 pDbNameHash = NULL;
4,620,103✔
811
  SArray*                    pList = NULL;
4,620,103✔
812

813
  pList = taosArrayInit(10, sizeof(uint64_t));
4,620,103✔
814
  QUERY_CHECK_NULL(pList, code, line, _return, terrno);
4,612,803✔
815

816
  *ppRes = taosArrayInit(10, POINTER_BYTES);
4,612,803✔
817
  QUERY_CHECK_NULL(*ppRes, code, line, _return, terrno)
4,619,076✔
818
  
819
  // lookup in cache
820
  code = pHandle->api.metaFn.metaGetCachedRefDbs(pHandle->vnode, suid, *ppRes);
4,618,395✔
821
  QUERY_CHECK_CODE(code, line, _return);
4,629,501✔
822

823
  if (taosArrayGetSize(*ppRes) > 0) {
4,629,501✔
824
    // found in cache
825
    goto _return;
4,524,133✔
826
  } else {
827
    code = pHandle->api.metaFn.getChildTableList(pHandle->vnode, suid, pList);
105,368✔
828
    QUERY_CHECK_CODE(code, line, _return);
105,368✔
829

830
    size_t num = taosArrayGetSize(pList);
105,368✔
831
    pDbNameHash = tSimpleHashInit(10, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
105,368✔
832
    QUERY_CHECK_NULL(pDbNameHash, code, line, _return, terrno);
105,368✔
833

834
    for (int32_t i = 0; i < num; ++i) {
548,146✔
835
      uint64_t* id = taosArrayGet(pList, i);
442,778✔
836
      QUERY_CHECK_NULL(id, code, line, _return, terrno);
442,778✔
837

838
      pHandle->api.metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pHandle->api.metaFn);
442,778✔
839
      readerInit = true;
442,778✔
840

841
      code = pHandle->api.metaReaderFn.getTableEntryByUid(&mr, *id);
442,778✔
842
      QUERY_CHECK_CODE(code, line, _return);
442,778✔
843

844
      for (int32_t j = 0; j < mr.me.colRef.nCols; j++) {
42,326,906✔
845
        if (mr.me.colRef.pColRef[j].hasRef) {
41,884,128✔
846
          if (NULL == tSimpleHashGet(pDbNameHash, mr.me.colRef.pColRef[j].refDbName, strlen(mr.me.colRef.pColRef[j].refDbName))) {
40,991,162✔
847
            char *refDbName = taosStrdup(mr.me.colRef.pColRef[j].refDbName);
90,120✔
848
            QUERY_CHECK_NULL(refDbName, code, line, _return, terrno);
90,120✔
849

850
            QUERY_CHECK_NULL(taosArrayPush(*ppRes, &refDbName), code, line, _return, terrno);
180,240✔
851

852
            code = tSimpleHashPut(pDbNameHash, refDbName, strlen(refDbName), NULL, 0);
90,120✔
853
            QUERY_CHECK_CODE(code, line, _return);
90,120✔
854
          }
855
        }
856
      }
857

858
      pHandle->api.metaReaderFn.clearReader(&mr);
442,778✔
859
      readerInit = false;
442,778✔
860
    }
861

862
    code = pHandle->api.metaFn.metaPutRefDbsToCache(pHandle->vnode, suid, *ppRes);
105,368✔
863
    QUERY_CHECK_CODE(code, line, _return);
105,368✔
864
  }
865

866
_return:
4,629,501✔
867

868
  if (readerInit) {
4,629,501✔
869
    pHandle->api.metaReaderFn.clearReader(&mr);
×
870
  }
871

872
  taosArrayDestroy(pList);
4,629,501✔
873
  tSimpleHashCleanup(pDbNameHash);
4,629,530✔
874

875
  if (code) {
4,628,194✔
876
    qError("%s failed since %s", __func__, tstrerror(code));
×
877
  }
878
  return code;
4,628,194✔
879
}
880

881
int32_t vnodeGetVSubtablesMeta(SVnode *pVnode, SRpcMsg *pMsg) {
×
882
  int32_t        code = 0;
×
883
  int32_t        rspSize = 0;
×
884
  SVSubTablesReq req = {0};
×
885
  SVSubTablesRsp rsp = {0};
×
886
  SRpcMsg      rspMsg = {0};
×
887
  void        *pRsp = NULL;
×
888
  int32_t      line = 0;
×
889

890
  if (tDeserializeSVSubTablesReq(pMsg->pCont, pMsg->contLen, &req)) {
×
891
    code = terrno;
×
892
    qError("tDeserializeSVSubTablesReq failed");
×
893
    goto _return;
×
894
  }
895

896
  SReadHandle handle = {0};
×
897
  handle.vnode = pVnode;
×
898
  initStorageAPI(&handle.api);
×
899

900
  QUERY_CHECK_CODE(vnodeReadVSubtables(&handle, req.suid, &rsp.pTables), line, _return);
×
901
  rsp.vgId = TD_VID(pVnode);
×
902

903
  rspSize = tSerializeSVSubTablesRsp(NULL, 0, &rsp);
×
904
  if (rspSize < 0) {
×
905
    code = rspSize;
×
906
    qError("tSerializeSVSubTablesRsp failed, error:%d", rspSize);
×
907
    goto _return;
×
908
  }
909
  pRsp = taosMemoryCalloc(1, rspSize);
×
910
  if (pRsp == NULL) {
×
911
    code = terrno;
×
912
    qError("rpcMallocCont %d failed, error:%d", rspSize, terrno);
×
913
    goto _return;
×
914
  }
915
  rspSize = tSerializeSVSubTablesRsp(pRsp, rspSize, &rsp);
×
916
  if (rspSize < 0) {
×
917
    code = rspSize;
×
918
    qError("tSerializeSVSubTablesRsp failed, error:%d", rspSize);
×
919
    goto _return;
×
920
  }
921

922
_return:
×
923

924
  rspMsg.info = pMsg->info;
×
925
  rspMsg.pCont = pRsp;
×
926
  rspMsg.contLen = rspSize;
×
927
  rspMsg.code = code;
×
928
  rspMsg.msgType = pMsg->msgType;
×
929

930
  if (code) {
×
931
    qError("vnd get virtual subtables failed cause of %s", tstrerror(code));
×
932
  }
933

934
  *pMsg = rspMsg;
×
935
  
936
  tDestroySVSubTablesRsp(&rsp);
×
937

938
  //tmsgSendRsp(&rspMsg);
939

940
  return code;
×
941
}
942

943
int32_t vnodeGetVStbRefDbs(SVnode *pVnode, SRpcMsg *pMsg) {
4,624,791✔
944
  int32_t        code = 0;
4,624,791✔
945
  int32_t        rspSize = 0;
4,624,791✔
946
  SVStbRefDbsReq req = {0};
4,624,791✔
947
  SVStbRefDbsRsp rsp = {0};
4,624,791✔
948
  SRpcMsg        rspMsg = {0};
4,606,736✔
949
  void          *pRsp = NULL;
4,606,736✔
950
  int32_t        line = 0;
4,606,736✔
951

952
  if (tDeserializeSVStbRefDbsReq(pMsg->pCont, pMsg->contLen, &req)) {
4,606,736✔
953
    code = terrno;
×
954
    qError("tDeserializeSVSubTablesReq failed");
×
955
    goto _return;
×
956
  }
957

958
  SReadHandle handle = {0};
4,617,371✔
959
  handle.vnode = pVnode;
4,624,168✔
960
  initStorageAPI(&handle.api);
4,624,168✔
961

962
  code = vnodeReadVStbRefDbs(&handle, req.suid, &rsp.pDbs);
4,609,975✔
963
  QUERY_CHECK_CODE(code, line, _return);
4,626,919✔
964
  rsp.vgId = TD_VID(pVnode);
4,626,919✔
965

966
  rspSize = tSerializeSVStbRefDbsRsp(NULL, 0, &rsp);
4,628,877✔
967
  if (rspSize < 0) {
4,621,479✔
968
    code = rspSize;
×
969
    qError("tSerializeSVStbRefDbsRsp failed, error:%d", rspSize);
×
970
    goto _return;
×
971
  }
972
  pRsp = taosMemoryCalloc(1, rspSize);
4,621,479✔
973
  if (pRsp == NULL) {
4,615,547✔
974
    code = terrno;
×
975
    qError("rpcMallocCont %d failed, error:%d", rspSize, terrno);
×
976
    goto _return;
×
977
  }
978
  rspSize = tSerializeSVStbRefDbsRsp(pRsp, rspSize, &rsp);
4,615,547✔
979
  if (rspSize < 0) {
4,622,995✔
980
    code = rspSize;
×
981
    qError("tSerializeSVStbRefDbsRsp failed, error:%d", rspSize);
×
982
    goto _return;
×
983
  }
984

985
_return:
4,622,995✔
986

987
  rspMsg.info = pMsg->info;
4,626,272✔
988
  rspMsg.pCont = pRsp;
4,626,268✔
989
  rspMsg.contLen = rspSize;
4,626,268✔
990
  rspMsg.code = code;
4,626,268✔
991
  rspMsg.msgType = pMsg->msgType;
4,626,268✔
992

993
  if (code) {
4,626,223✔
994
    qError("vnd get virtual stb ref db failed cause of %s", tstrerror(code));
×
995
  }
996

997
  *pMsg = rspMsg;
4,626,223✔
998

999
  tDestroySVStbRefDbsRsp(&rsp);
4,620,214✔
1000

1001
  return code;
4,619,491✔
1002
}
1003

1004
static int32_t vnodeGetCompStorage(SVnode *pVnode, int64_t *output) {
173,927,092✔
1005
  int32_t code = 0;
173,927,092✔
1006
#ifdef TD_ENTERPRISE
1007
  int32_t now = taosGetTimestampSec();
173,927,092✔
1008
  if (llabs(now - pVnode->config.vndStats.storageLastUpd) >= 30) {
173,927,092✔
1009
    pVnode->config.vndStats.storageLastUpd = now;
8,819,487✔
1010

1011
    SDbSizeStatisInfo info = {0};
8,819,487✔
1012
    if (0 == (code = vnodeGetDBSize(pVnode, &info))) {
8,819,487✔
1013
      int64_t compSize =
8,819,487✔
1014
          info.l1Size + info.l2Size + info.l3Size + info.cacheSize + info.walSize + info.metaSize + +info.ssSize;
8,819,487✔
1015
      if (compSize >= 0) {
8,819,487✔
1016
        pVnode->config.vndStats.compStorage = compSize;
8,819,487✔
1017
      } else {
1018
        vError("vnode get comp storage failed since compSize is negative:%" PRIi64, compSize);
×
1019
        code = TSDB_CODE_APP_ERROR;
×
1020
      }
1021
    } else {
1022
      vWarn("vnode get comp storage failed since %s", tstrerror(code));
×
1023
    }
1024
  }
1025
  if (output) *output = pVnode->config.vndStats.compStorage;
173,927,092✔
1026
#endif
1027
  return code;
173,927,092✔
1028
}
1029

1030
static void vnodeGetBufferInfo(SVnode *pVnode, int64_t *bufferSegmentUsed, int64_t *bufferSegmentSize) {
173,927,092✔
1031
  *bufferSegmentUsed = 0;
173,927,092✔
1032
  *bufferSegmentSize = 0;
173,927,092✔
1033
  if (pVnode) {
173,927,092✔
1034
    (void)taosThreadMutexLock(&pVnode->mutex);
173,927,092✔
1035

1036
    if (pVnode->inUse) {
173,927,092✔
1037
      *bufferSegmentUsed = pVnode->inUse->size;
173,916,931✔
1038
    }
1039
    *bufferSegmentSize = pVnode->config.szBuf / VNODE_BUFPOOL_SEGMENTS;
173,927,092✔
1040

1041
    (void)taosThreadMutexUnlock(&pVnode->mutex);
173,927,092✔
1042
  }
1043
}
173,927,092✔
1044

1045
int32_t vnodeGetLoad(SVnode *pVnode, SVnodeLoad *pLoad) {
173,927,092✔
1046
  SSyncState state = syncGetState(pVnode->sync);
173,927,092✔
1047
  pLoad->syncAppliedIndex = pVnode->state.applied;
173,927,092✔
1048
  syncGetCommitIndex(pVnode->sync, &pLoad->syncCommitIndex);
173,927,092✔
1049

1050
  pLoad->vgId = TD_VID(pVnode);
173,927,092✔
1051
  pLoad->syncState = state.state;
173,927,092✔
1052
  pLoad->syncRestore = state.restored;
173,927,092✔
1053
  pLoad->syncTerm = state.term;
173,927,092✔
1054
  pLoad->roleTimeMs = state.roleTimeMs;
173,927,092✔
1055
  pLoad->startTimeMs = state.startTimeMs;
173,927,092✔
1056
  pLoad->syncCanRead = state.canRead;
173,927,092✔
1057
  pLoad->cacheUsage = tsdbCacheGetUsage(pVnode);
173,927,092✔
1058
  pLoad->numOfCachedTables = tsdbCacheGetElems(pVnode);
173,927,092✔
1059
  VNODE_DO_META_QUERY(pVnode, pLoad->numOfTables = metaGetTbNum(pVnode->pMeta));
173,927,092✔
1060
  VNODE_DO_META_QUERY(pVnode, pLoad->numOfTimeSeries = metaGetTimeSeriesNum(pVnode->pMeta, 1));
173,927,092✔
1061
  pLoad->totalStorage = (int64_t)3 * 1073741824;  // TODO
173,927,092✔
1062
  (void)vnodeGetCompStorage(pVnode, &pLoad->compStorage);
173,927,092✔
1063
  pLoad->pointsWritten = 100;
173,927,092✔
1064
  pLoad->numOfSelectReqs = 1;
173,927,092✔
1065
  pLoad->numOfInsertReqs = atomic_load_64(&pVnode->statis.nInsert);
173,927,092✔
1066
  pLoad->numOfInsertSuccessReqs = atomic_load_64(&pVnode->statis.nInsertSuccess);
173,927,092✔
1067
  pLoad->numOfBatchInsertReqs = atomic_load_64(&pVnode->statis.nBatchInsert);
173,927,092✔
1068
  pLoad->numOfBatchInsertSuccessReqs = atomic_load_64(&pVnode->statis.nBatchInsertSuccess);
173,927,092✔
1069
  vnodeGetBufferInfo(pVnode, &pLoad->bufferSegmentUsed, &pLoad->bufferSegmentSize);
173,927,092✔
1070
  vDebug("vgId:%d, get vnode load, state:%s snapSeq:%d, learnerProgress:%d, totalIndex:%" PRId64, TD_VID(pVnode),
173,927,092✔
1071
         syncStr(state.state), state.snapSeq, state.progress, state.totalIndex);
1072
  pLoad->learnerProgress = state.progress;
173,927,092✔
1073
  pLoad->snapSeq = state.snapSeq;
173,927,092✔
1074
  pLoad->syncTotalIndex = state.totalIndex;
173,927,092✔
1075
  return 0;
173,927,092✔
1076
}
1077

1078
int32_t vnodeGetLoadLite(SVnode *pVnode, SVnodeLoadLite *pLoad) {
×
1079
  SSyncState syncState = syncGetState(pVnode->sync);
×
1080
  if (syncState.state == TAOS_SYNC_STATE_LEADER || syncState.state == TAOS_SYNC_STATE_ASSIGNED_LEADER) {
×
1081
    pLoad->vgId = TD_VID(pVnode);
×
1082
    pLoad->nTimeSeries = metaGetTimeSeriesNum(pVnode->pMeta, 1);
×
1083
    return 0;
×
1084
  }
1085
  return -1;
×
1086
}
1087
/**
1088
 * @brief Reset the statistics value by monitor interval
1089
 *
1090
 * @param pVnode
1091
 * @param pLoad
1092
 */
1093
void vnodeResetLoad(SVnode *pVnode, SVnodeLoad *pLoad) {
255✔
1094
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nInsert, pLoad->numOfInsertReqs, 64, "nInsert");
255✔
1095
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nInsertSuccess, pLoad->numOfInsertSuccessReqs, 64, "nInsertSuccess");
255✔
1096
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nBatchInsert, pLoad->numOfBatchInsertReqs, 64, "nBatchInsert");
255✔
1097
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nBatchInsertSuccess, pLoad->numOfBatchInsertSuccessReqs, 64,
255✔
1098
                            "nBatchInsertSuccess");
1099
}
255✔
1100

1101
void vnodeGetInfo(void *pVnode, const char **dbname, int32_t *vgId, int64_t *numOfTables, int64_t *numOfNormalTables) {
21,762,022✔
1102
  SVnode    *pVnodeObj = pVnode;
21,762,022✔
1103
  SVnodeCfg *pConf = &pVnodeObj->config;
21,762,022✔
1104

1105
  if (dbname) {
21,762,603✔
1106
    *dbname = pConf->dbname;
21,053,692✔
1107
  }
1108

1109
  if (vgId) {
21,763,981✔
1110
    *vgId = TD_VID(pVnodeObj);
20,593,035✔
1111
  }
1112

1113
  if (numOfTables) {
21,763,277✔
1114
    *numOfTables = pConf->vndStats.numOfNTables + pConf->vndStats.numOfCTables +
17,000✔
1115
                   pConf->vndStats.numOfVTables + pConf->vndStats.numOfVCTables;
8,500✔
1116
  }
1117

1118
  if (numOfNormalTables) {
21,763,277✔
1119
    *numOfNormalTables = pConf->vndStats.numOfNTables +
19,954✔
1120
                         pConf->vndStats.numOfVTables;
9,977✔
1121
  }
1122
}
21,763,277✔
1123

1124
int8_t vnodeGetSecurityLevel(void *pVnode) {
1,584,207✔
1125
  SVnode *pVnodeObj = pVnode;
1,584,207✔
1126
  return pVnodeObj->config.securityLevel;
1,584,207✔
1127
}
1128

1129
int32_t vnodeGetTableList(void *pVnode, int8_t type, SArray *pList) {
9,977✔
1130
  if (type == TSDB_SUPER_TABLE) {
9,977✔
1131
    return vnodeGetStbIdList(pVnode, 0, pList);
9,977✔
1132
  } else {
1133
    return TSDB_CODE_INVALID_PARA;
×
1134
  }
1135
}
1136

1137
int32_t vnodeGetAllTableList(SVnode *pVnode, uint64_t uid, SArray *list) {
×
1138
  int32_t      code = TSDB_CODE_SUCCESS;
×
1139
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, uid, 1);
×
1140
  if (NULL == pCur) {
×
1141
    qError("vnode get all table list failed");
×
1142
    return terrno;
×
1143
  }
1144

1145
  while (1) {
×
1146
    tb_uid_t id = metaCtbCursorNext(pCur);
×
1147
    if (id == 0) {
×
1148
      break;
×
1149
    }
1150

1151
    STableKeyInfo info = {uid = id};
×
1152
    if (NULL == taosArrayPush(list, &info)) {
×
1153
      qError("taosArrayPush failed");
×
1154
      code = terrno;
×
1155
      goto _exit;
×
1156
    }
1157
  }
1158
_exit:
×
1159
  metaCloseCtbCursor(pCur);
×
1160
  return code;
×
1161
}
1162

1163
int32_t vnodeGetCtbIdListByFilter(SVnode *pVnode, int64_t suid, SArray *list, bool (*filter)(void *arg), void *arg) {
×
1164
  return 0;
×
1165
}
1166

1167
int32_t vnodeGetCtbIdList(void *pVnode, int64_t suid, SArray *list) {
114,594,964✔
1168
  int32_t      code = TSDB_CODE_SUCCESS;
114,594,964✔
1169
  SVnode      *pVnodeObj = pVnode;
114,594,964✔
1170
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnodeObj, suid, 1);
114,594,964✔
1171
  if (NULL == pCur) {
114,589,102✔
1172
    qError("vnode get all table list failed");
×
1173
    return terrno;
×
1174
  }
1175

1176
  while (1) {
511,916,334✔
1177
    tb_uid_t id = metaCtbCursorNext(pCur);
626,505,436✔
1178
    if (id == 0) {
626,528,423✔
1179
      break;
114,624,856✔
1180
    }
1181
    qTrace("vnodeGetCtbIdList: got ctb id %" PRId64 " for suid %" PRId64, id, suid);
511,903,567✔
1182
    if (NULL == taosArrayPush(list, &id)) {
511,912,760✔
1183
      qError("taosArrayPush failed");
×
1184
      code = terrno;
×
1185
      goto _exit;
×
1186
    }
1187
  }
1188

1189
_exit:
114,578,821✔
1190
  metaCloseCtbCursor(pCur);
114,578,821✔
1191
  return code;
114,555,880✔
1192
}
1193

1194
int32_t vnodeGetStbIdList(SVnode *pVnode, int64_t suid, SArray *list) {
80,695,109✔
1195
  int32_t      code = TSDB_CODE_SUCCESS;
80,695,109✔
1196
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, suid);
80,695,109✔
1197
  if (!pCur) {
80,695,618✔
1198
    return TSDB_CODE_OUT_OF_MEMORY;
×
1199
  }
1200

1201
  while (1) {
62,467,790✔
1202
    tb_uid_t id = metaStbCursorNext(pCur);
143,163,408✔
1203
    if (id == 0) {
143,163,597✔
1204
      break;
80,696,538✔
1205
    }
1206

1207
    if (NULL == taosArrayPush(list, &id)) {
62,464,644✔
1208
      qError("taosArrayPush failed");
×
1209
      code = terrno;
×
1210
      goto _exit;
×
1211
    }
1212
  }
1213

1214
_exit:
80,697,080✔
1215
  metaCloseStbCursor(pCur);
80,697,080✔
1216
  return code;
80,694,156✔
1217
}
1218

1219
int32_t vnodeGetStbIdListByFilter(SVnode *pVnode, int64_t suid, SArray *list, bool (*filter)(void *arg, void *arg1),
7,206✔
1220
                                  void *arg) {
1221
  int32_t      code = TSDB_CODE_SUCCESS;
7,206✔
1222
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, suid);
7,206✔
1223
  if (!pCur) {
7,206✔
1224
    return terrno;
×
1225
  }
1226

1227
  while (1) {
8,798✔
1228
    tb_uid_t id = metaStbCursorNext(pCur);
16,004✔
1229
    if (id == 0) {
16,004✔
1230
      break;
7,206✔
1231
    }
1232

1233
    if ((*filter) && (*filter)(arg, &id)) {
8,798✔
1234
      continue;
8,798✔
1235
    }
1236

1237
    if (NULL == taosArrayPush(list, &id)) {
×
1238
      qError("taosArrayPush failed");
×
1239
      code = terrno;
×
1240
      goto _exit;
×
1241
    }
1242
  }
1243

1244
_exit:
7,206✔
1245
  metaCloseStbCursor(pCur);
7,206✔
1246
  return code;
7,206✔
1247
}
1248

1249
int32_t vnodeGetCtbNum(SVnode *pVnode, int64_t suid, int64_t *num) {
5,667,674✔
1250
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, suid, 0);
5,667,674✔
1251
  if (!pCur) {
5,670,281✔
1252
    return terrno;
×
1253
  }
1254

1255
  *num = 0;
5,670,281✔
1256
  while (1) {
3,190,532✔
1257
    tb_uid_t id = metaCtbCursorNext(pCur);
8,860,813✔
1258
    if (id == 0) {
8,860,790✔
1259
      break;
5,670,281✔
1260
    }
1261

1262
    ++(*num);
3,190,509✔
1263
  }
1264

1265
  metaCloseCtbCursor(pCur);
5,670,281✔
1266
  return TSDB_CODE_SUCCESS;
5,670,277✔
1267
}
1268

1269
int32_t vnodeGetStbColumnNum(SVnode *pVnode, tb_uid_t suid, int *num) {
5,670,277✔
1270
  SSchemaWrapper *pSW = metaGetTableSchema(pVnode->pMeta, suid, -1, 0, NULL, 0, false);
5,670,277✔
1271
  if (pSW) {
5,670,281✔
1272
    *num = pSW->nCols;
5,670,281✔
1273
    tDeleteSchemaWrapper(pSW);
1274
  } else {
UNCOV
1275
    *num = 2;
×
1276
  }
1277

1278
  return TSDB_CODE_SUCCESS;
5,669,630✔
1279
}
1280

1281
int32_t vnodeGetStbInfo(SVnode *pVnode, tb_uid_t suid, int64_t *keep, int8_t *flags) {
5,670,281✔
1282
  SMetaReader mr = {0};
5,670,281✔
1283
  metaReaderDoInit(&mr, pVnode->pMeta, META_READER_NOLOCK);
5,669,630✔
1284

1285
  int32_t code = metaReaderGetTableEntryByUid(&mr, suid);
5,669,607✔
1286
  if (code == TSDB_CODE_SUCCESS) {
5,670,281✔
1287
    if (keep) *keep = mr.me.stbEntry.keep;
5,670,281✔
1288
    if (flags) *flags = mr.me.flags;
5,670,281✔
1289
  } else {
UNCOV
1290
    if (keep) *keep = 0;
×
UNCOV
1291
    if (flags) *flags = 0;
×
1292
  }
1293

1294
  metaReaderClear(&mr);
5,670,281✔
1295
  return TSDB_CODE_SUCCESS;
5,670,281✔
1296
}
1297

1298
#ifdef TD_ENTERPRISE
1299
const char *tkLogStb[] = {"cluster_info",
1300
                          "data_dir",
1301
                          "disk_free",
1302
                          "disk_total",
1303
                          "dnodes_info",
1304
                          "d_info",
1305
                          "explorer_sys",
1306
                          "grants_info",
1307
                          "keeper_monitor",
1308
                          "logs",
1309
                          "log_dir",
1310
                          "log_summary",
1311
                          "http_client_requests",
1312
                          "http_server_requests_idmp",
1313
                          "m_info",
1314
                          "taosadapter_restful_http_request_fail",
1315
                          "taosadapter_restful_http_request_in_flight",
1316
                          "taosadapter_restful_http_request_summary_milliseconds",
1317
                          "taosadapter_restful_http_request_total",
1318
                          "taosadapter_system_cpu_percent",
1319
                          "taosadapter_system_mem_percent",
1320
                          "temp_dir",
1321
                          "vgroups_info",
1322
                          "vnodes_role",
1323
                          "taosd_dnodes_status",
1324
                          "adapter_conn_pool",
1325
                          "taosd_vnodes_info",
1326
                          "taosd_dnodes_metrics",
1327
                          "taosd_vgroups_info",
1328
                          "taos_sql_req",
1329
                          "taosd_mnodes_info",
1330
                          "adapter_c_interface",
1331
                          "taosd_cluster_info",
1332
                          "taosd_sql_req",
1333
                          "taosd_dnodes_info",
1334
                          "adapter_requests",
1335
                          "taosd_write_metrics",
1336
                          "adapter_status",
1337
                          "taos_slow_sql",
1338
                          "taos_slow_sql_detail",
1339
                          "taosd_cluster_basic",
1340
                          "taosd_dnodes_data_dirs",
1341
                          "taosd_dnodes_log_dirs",
1342
                          "xnode_agent_activities",
1343
                          "xnode_task_activities",
1344
                          "xnode_task_metrics",
1345
                          "taosx_sys",
1346
                          "taosx_task_csv",
1347
                          "taosx_task_progress",
1348
                          "taosx_task_kinghist",
1349
                          "taosx_task_tdengine2",
1350
                          "taosx_task_tdengine3",
1351
                          "taosx_task_opc_da",
1352
                          "taosx_task_opc_ua",
1353
                          "taosx_task_kafka",
1354
                          "taosx_task_influxdb",
1355
                          "taosx_task_mqtt",
1356
                          "taosx_task_avevahistorian",
1357
                          "taosx_task_opentsdb",
1358
                          "taosx_task_mysql",
1359
                          "taosx_task_postgres",
1360
                          "taosx_task_oracle",
1361
                          "taosx_task_mssql",
1362
                          "taosx_task_mongodb",
1363
                          "taosx_task_sparkplugb",
1364
                          "taosx_task_orc",
1365
                          "taosx_task_pulsar",
1366
                          "taosx_task_pspace"};
1367
const char *tkAuditStb[] = {"operations"};
1368
const int   tkLogStbNum = ARRAY_SIZE(tkLogStb);
1369
const int   tkAuditStbNum = ARRAY_SIZE(tkAuditStb);
1370

1371
// exclude stbs of taoskeeper log
1372
static int32_t vnodeGetTimeSeriesBlackList(SVnode *pVnode, int32_t *tbSize) {
80,695,729✔
1373
  int32_t      code = TSDB_CODE_SUCCESS;
80,695,729✔
1374
  int32_t      tbNum = 0;
80,695,729✔
1375
  const char **pTbArr = NULL;
80,695,729✔
1376
  const char  *dbName = NULL;
80,695,729✔
1377
  *tbSize = 0;
80,695,729✔
1378

1379
  if (!(dbName = strchr(pVnode->config.dbname, '.'))) return 0;
80,693,536✔
1380
  if (0 == strncmp(++dbName, "log", TSDB_DB_NAME_LEN)) {
80,696,460✔
1381
    tbNum = tkLogStbNum;
4,952✔
1382
    pTbArr = (const char **)&tkLogStb;
4,952✔
1383
  } else if (0 == strncmp(dbName, "audit", TSDB_DB_NAME_LEN) || pVnode->config.isAudit) {
80,692,239✔
1384
    tbNum = tkAuditStbNum;
7,203✔
1385
    pTbArr = (const char **)&tkAuditStb;
7,203✔
1386
  }
1387
  if (tbNum && pTbArr) {
80,698,320✔
1388
    *tbSize = metaSizeOfTbFilterCache(pVnode->pMeta, 0);
12,155✔
1389
    if (*tbSize < tbNum) {
12,155✔
1390
      for (int32_t i = 0; i < tbNum; ++i) {
143,996✔
1391
        tb_uid_t suid = metaGetTableEntryUidByName(pVnode->pMeta, pTbArr[i]);
141,560✔
1392
        if (suid != 0) {
141,560✔
1393
          code = metaPutTbToFilterCache(pVnode->pMeta, &suid, 0);
5,799✔
1394
          if (TSDB_CODE_SUCCESS != code) {
5,799✔
1395
            return code;
3,360✔
1396
          }
1397
        }
1398
      }
1399
      *tbSize = metaSizeOfTbFilterCache(pVnode->pMeta, 0);
2,436✔
1400
    }
1401
  }
1402

1403
  return code;
80,693,100✔
1404
}
1405
#endif
1406

1407
static bool vnodeTimeSeriesFilter(void *arg1, void *arg2) {
8,798✔
1408
  SVnode *pVnode = (SVnode *)arg1;
8,798✔
1409

1410
  if (metaTbInFilterCache(pVnode->pMeta, arg2, 0)) {
8,798✔
1411
    return true;
8,798✔
1412
  }
1413
  return false;
×
1414
}
1415

1416
int32_t vnodeGetTimeSeriesNum(SVnode *pVnode, int64_t *num) {
80,694,998✔
1417
  SArray *suidList = NULL;
80,694,998✔
1418

1419
  if (!(suidList = taosArrayInit(1, sizeof(tb_uid_t)))) {
80,694,998✔
1420
    return terrno;
×
1421
  }
1422

1423
  int32_t tbFilterSize = 0;
80,696,460✔
1424
  int32_t code = TSDB_CODE_SUCCESS;
80,694,267✔
1425
#ifdef TD_ENTERPRISE
1426
  code = vnodeGetTimeSeriesBlackList(pVnode, &tbFilterSize);
80,694,267✔
1427
  if (TSDB_CODE_SUCCESS != code) {
80,695,331✔
1428
    goto _exit;
3,360✔
1429
  }
1430
#endif
1431

1432
  if ((!tbFilterSize && vnodeGetStbIdList(pVnode, 0, suidList) < 0) ||
80,691,971✔
1433
      (tbFilterSize && vnodeGetStbIdListByFilter(pVnode, 0, suidList, vnodeTimeSeriesFilter, pVnode) < 0)) {
80,690,907✔
1434
    qError("vgId:%d, failed to get stb id list error: %s", TD_VID(pVnode), terrstr());
×
1435
    taosArrayDestroy(suidList);
×
1436
    return terrno;
×
1437
  }
1438

1439
  *num = 0;
80,690,907✔
1440
  int64_t arrSize = taosArrayGetSize(suidList);
80,692,369✔
1441
  for (int64_t i = 0; i < arrSize; ++i) {
143,143,581✔
1442
    tb_uid_t suid = *(tb_uid_t *)taosArrayGet(suidList, i);
62,449,750✔
1443

1444
    int64_t ctbNum = 0;
62,447,159✔
1445
    int32_t numOfCols = 0;
62,447,159✔
1446
    int8_t  flags = 0;
62,446,428✔
1447
    code = metaGetStbStats(pVnode, suid, &ctbNum, &numOfCols, &flags);
62,449,019✔
1448
    if (TSDB_CODE_SUCCESS != code) {
62,450,399✔
1449
      goto _exit;
×
1450
    }
1451
    if (!TABLE_IS_VIRTUAL(flags)) {
62,450,399✔
1452
      *num += ctbNum * (numOfCols - 1);
56,850,017✔
1453
    }
1454
  }
1455

1456
_exit:
80,691,385✔
1457
  taosArrayDestroy(suidList);
80,697,191✔
1458
  return TSDB_CODE_SUCCESS;
80,693,869✔
1459
}
1460

1461
int32_t vnodeGetAllCtbNum(SVnode *pVnode, int64_t *num) {
×
1462
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, 0);
×
1463
  if (!pCur) {
×
1464
    return terrno;
×
1465
  }
1466

1467
  *num = 0;
×
1468
  while (1) {
×
1469
    tb_uid_t id = metaStbCursorNext(pCur);
×
1470
    if (id == 0) {
×
1471
      break;
×
1472
    }
1473

1474
    int64_t ctbNum = 0;
×
1475
    int32_t code = vnodeGetCtbNum(pVnode, id, &ctbNum);
×
1476
    if (TSDB_CODE_SUCCESS != code) {
×
1477
      metaCloseStbCursor(pCur);
×
1478
      return code;
×
1479
    }
1480

1481
    *num += ctbNum;
×
1482
  }
1483

1484
  metaCloseStbCursor(pCur);
×
1485
  return TSDB_CODE_SUCCESS;
×
1486
}
1487

1488
void *vnodeGetIdx(void *pVnode) {
4,902,839✔
1489
  if (pVnode == NULL) {
4,902,839✔
1490
    return NULL;
×
1491
  }
1492

1493
  return metaGetIdx(((SVnode *)pVnode)->pMeta);
4,902,839✔
1494
}
1495

1496
void *vnodeGetIvtIdx(void *pVnode) {
4,902,839✔
1497
  if (pVnode == NULL) {
4,902,839✔
1498
    return NULL;
×
1499
  }
1500
  return metaGetIvtIdx(((SVnode *)pVnode)->pMeta);
4,902,839✔
1501
}
1502

1503
int32_t vnodeGetTableSchema(void *pVnode, int64_t uid, STSchema **pSchema, int64_t *suid, SSchemaWrapper **pTagSchema) {
527,795✔
1504
  return tsdbGetTableSchema(((SVnode *)pVnode)->pMeta, uid, pSchema, suid, pTagSchema);
527,795✔
1505
}
1506

1507
static FORCE_INLINE int32_t vnodeGetDBPrimaryInfo(SVnode *pVnode, SDbSizeStatisInfo *pInfo) {
1508
  int32_t code = 0;
8,827,119✔
1509
  char    path[TSDB_FILENAME_LEN] = {0};
8,827,119✔
1510

1511
  char   *dirName[] = {VNODE_TSDB_DIR, VNODE_WAL_DIR, VNODE_META_DIR, VNODE_TSDB_CACHE_DIR};
8,827,119✔
1512
  int64_t dirSize[4];
8,824,988✔
1513

1514
  vnodeGetPrimaryPath(pVnode, false, path, TSDB_FILENAME_LEN);
8,827,119✔
1515
  int32_t offset = strlen(path);
8,827,119✔
1516

1517
  for (int i = 0; i < sizeof(dirName) / sizeof(dirName[0]); i++) {
44,135,595✔
1518
    int64_t size = {0};
35,308,476✔
1519
    (void)snprintf(path + offset, TSDB_FILENAME_LEN - offset, "%s%s", TD_DIRSEP, dirName[i]);
35,308,476✔
1520
    code = taosGetDirSize(path, &size);
35,308,476✔
1521
    if (code != 0) {
35,308,476✔
1522
      uWarn("vnode %d get dir %s %s size failed since %s", TD_VID(pVnode), path, dirName[i], tstrerror(code));
638✔
1523
    }
1524
    path[offset] = 0;
35,308,476✔
1525
    dirSize[i] = size;
35,308,476✔
1526
  }
1527

1528
  pInfo->l1Size = 0;
8,827,119✔
1529
  pInfo->walSize = dirSize[1];
8,827,119✔
1530
  pInfo->metaSize = dirSize[2];
8,827,119✔
1531
  pInfo->cacheSize = dirSize[3];
8,827,119✔
1532
  return code;
8,827,119✔
1533
}
1534
int32_t vnodeGetDBSize(void *pVnode, SDbSizeStatisInfo *pInfo) {
8,827,119✔
1535
  int32_t code = 0;
8,827,119✔
1536
  int32_t lino = 0;
8,827,119✔
1537
  SVnode *pVnodeObj = pVnode;
8,827,119✔
1538
  if (pVnodeObj == NULL) {
8,827,119✔
1539
    return TSDB_CODE_VND_NOT_EXIST;
×
1540
  }
1541
  code = vnodeGetDBPrimaryInfo(pVnode, pInfo);
8,827,119✔
1542
  if (code != 0) goto _exit;
8,827,119✔
1543

1544
  code = tsdbGetFsSize(pVnodeObj->pTsdb, pInfo);
8,827,119✔
1545
_exit:
8,827,119✔
1546
  return code;
8,827,119✔
1547
}
1548

1549
/*
1550
 * Get raw write metrics for a vnode
1551
 */
1552
int32_t vnodeGetRawWriteMetrics(void *pVnode, SRawWriteMetrics *pRawMetrics) {
×
1553
  if (pVnode == NULL || pRawMetrics == NULL) {
×
1554
    return TSDB_CODE_INVALID_PARA;
×
1555
  }
1556

1557
  SVnode      *pVnode1 = (SVnode *)pVnode;
×
1558
  SSyncMetrics syncMetrics = syncGetMetrics(pVnode1->sync);
×
1559

1560
  // Copy values following SRawWriteMetrics structure order
1561
  pRawMetrics->total_requests = atomic_load_64(&pVnode1->writeMetrics.total_requests);
×
1562
  pRawMetrics->total_rows = atomic_load_64(&pVnode1->writeMetrics.total_rows);
×
1563
  pRawMetrics->total_bytes = atomic_load_64(&pVnode1->writeMetrics.total_bytes);
×
1564
  pRawMetrics->fetch_batch_meta_time = atomic_load_64(&pVnode1->writeMetrics.fetch_batch_meta_time);
×
1565
  pRawMetrics->fetch_batch_meta_count = atomic_load_64(&pVnode1->writeMetrics.fetch_batch_meta_count);
×
1566
  pRawMetrics->preprocess_time = atomic_load_64(&pVnode1->writeMetrics.preprocess_time);
×
1567
  pRawMetrics->wal_write_bytes = atomic_load_64(&syncMetrics.wal_write_bytes);
×
1568
  pRawMetrics->wal_write_time = atomic_load_64(&syncMetrics.wal_write_time);
×
1569
  pRawMetrics->apply_bytes = atomic_load_64(&pVnode1->writeMetrics.apply_bytes);
×
1570
  pRawMetrics->apply_time = atomic_load_64(&pVnode1->writeMetrics.apply_time);
×
1571
  pRawMetrics->commit_count = atomic_load_64(&pVnode1->writeMetrics.commit_count);
×
1572
  pRawMetrics->commit_time = atomic_load_64(&pVnode1->writeMetrics.commit_time);
×
1573
  pRawMetrics->memtable_wait_time = atomic_load_64(&pVnode1->writeMetrics.memtable_wait_time);
×
1574
  pRawMetrics->blocked_commit_count = atomic_load_64(&pVnode1->writeMetrics.blocked_commit_count);
×
1575
  pRawMetrics->blocked_commit_time = atomic_load_64(&pVnode1->writeMetrics.block_commit_time);
×
1576
  pRawMetrics->merge_count = atomic_load_64(&pVnode1->writeMetrics.merge_count);
×
1577
  pRawMetrics->merge_time = atomic_load_64(&pVnode1->writeMetrics.merge_time);
×
1578
  pRawMetrics->last_cache_commit_time = atomic_load_64(&pVnode1->writeMetrics.last_cache_commit_time);
×
1579
  pRawMetrics->last_cache_commit_count = atomic_load_64(&pVnode1->writeMetrics.last_cache_commit_count);
×
1580

1581
  return 0;
×
1582
}
1583

1584
/*
1585
 * Reset raw write metrics for a vnode by subtracting old values
1586
 */
1587
int32_t vnodeResetRawWriteMetrics(void *pVnode, const SRawWriteMetrics *pOldMetrics) {
×
1588
  if (pVnode == NULL || pOldMetrics == NULL) {
×
1589
    return TSDB_CODE_INVALID_PARA;
×
1590
  }
1591

1592
  SVnode *pVnode1 = (SVnode *)pVnode;
×
1593

1594
  // Reset vnode write metrics using atomic operations to subtract old values
1595
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.total_requests, pOldMetrics->total_requests);
×
1596
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.total_rows, pOldMetrics->total_rows);
×
1597
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.total_bytes, pOldMetrics->total_bytes);
×
1598

1599
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.fetch_batch_meta_time, pOldMetrics->fetch_batch_meta_time);
×
1600
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.fetch_batch_meta_count, pOldMetrics->fetch_batch_meta_count);
×
1601
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.preprocess_time, pOldMetrics->preprocess_time);
×
1602
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.apply_bytes, pOldMetrics->apply_bytes);
×
1603
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.apply_time, pOldMetrics->apply_time);
×
1604
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.commit_count, pOldMetrics->commit_count);
×
1605

1606
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.commit_time, pOldMetrics->commit_time);
×
1607
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.merge_time, pOldMetrics->merge_time);
×
1608

1609
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.memtable_wait_time, pOldMetrics->memtable_wait_time);
×
1610
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.blocked_commit_count, pOldMetrics->blocked_commit_count);
×
1611
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.block_commit_time, pOldMetrics->blocked_commit_time);
×
1612
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.merge_count, pOldMetrics->merge_count);
×
1613

1614
  // Reset new cache metrics
1615
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.last_cache_commit_time, pOldMetrics->last_cache_commit_time);
×
1616
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.last_cache_commit_count, pOldMetrics->last_cache_commit_count);
×
1617

1618
  // Reset sync metrics
1619
  SSyncMetrics syncMetrics = {
×
1620
      .wal_write_bytes = pOldMetrics->wal_write_bytes,
×
1621
      .wal_write_time = pOldMetrics->wal_write_time,
×
1622
  };
1623
  syncResetMetrics(pVnode1->sync, &syncMetrics);
×
1624

1625
  return 0;
×
1626
}
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