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

taosdata / TDengine / #5048

10 May 2026 03:11AM UTC coverage: 73.222% (+0.07%) from 73.152%
#5048

push

travis-ci

web-flow
merge: from main to 3.0 branch #35290

353 of 452 new or added lines in 9 files covered. (78.1%)

587 existing lines in 140 files now uncovered.

278189 of 379928 relevant lines covered (73.22%)

135206397.85 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,781,088✔
29
  return qWorkerInit(NODE_TYPE_VNODE, TD_VID(pVnode), (void **)&pVnode->pQuery, &pVnode->msgCb);
4,781,088✔
30
}
31

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

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

36
int32_t fillTableColCmpr(SMetaReader *reader, SSchemaExt *pExt, int32_t numOfCol) {
22,737,250✔
37
  int8_t tblType = reader->me.type;
22,737,250✔
38
  if (withColCompress(tblType)) {
22,737,670✔
39
    SColCmprWrapper *p = &(reader->me.colCmpr);
22,582,636✔
40
    if (numOfCol != p->nCols) {
22,582,636✔
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++) {
258,336,020✔
45
      SColCmpr *pCmpr = &p->pColCmpr[i];
235,753,384✔
46
      pExt[i].colId = pCmpr->id;
235,753,611✔
47
      pExt[i].compress = pCmpr->alg;
235,754,031✔
48
    }
49
  }
50
  return 0;
22,737,250✔
51
}
52

53
void vnodeDebugTableMeta(STableMetaRsp *pMeta) {
22,666,950✔
54
  if (!(qDebugFlag & DEBUG_DEBUG)) {
22,666,950✔
55
    return;
429,573✔
56
  }
57

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

81
int32_t fillTableColRef(SMetaReader *reader, SColRef *pRef, int32_t numOfCol) {
395,179✔
82
  int8_t tblType = reader->me.type;
395,179✔
83
  if (hasRefCol(tblType)) {
395,179✔
84
    SColRefWrapper *p = &(reader->me.colRef);
395,179✔
85
    if (numOfCol != p->nCols) {
395,179✔
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,072,925✔
90
      SColRef *pColRef = &p->pColRef[i];
2,677,746✔
91
      pRef[i].hasRef = pColRef->hasRef;
2,677,746✔
92
      pRef[i].id = pColRef->id;
2,677,746✔
93
      if(pRef[i].hasRef) {
2,677,746✔
94
        tstrncpy(pRef[i].refDbName, pColRef->refDbName, TSDB_DB_NAME_LEN);
1,888,303✔
95
        tstrncpy(pRef[i].refTableName, pColRef->refTableName, TSDB_TABLE_NAME_LEN);
1,888,303✔
96
        tstrncpy(pRef[i].refColName, pColRef->refColName, TSDB_COL_NAME_LEN);
1,888,303✔
97
      }
98
    }
99
  }
100
  return 0;
395,179✔
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) {
33,207,867✔
126
  STableInfoReq  infoReq = {0};
33,207,867✔
127
  STableMetaRsp  metaRsp = {0};
33,207,867✔
128
  SMetaReader    mer1 = {0};
33,207,867✔
129
  SMetaReader    mer2 = {0};
33,207,867✔
130
  char           tableFName[TSDB_TABLE_FNAME_LEN];
33,204,677✔
131
  bool           reqTbUid = false;
33,207,867✔
132
  SRpcMsg        rpcMsg = {0};
33,207,867✔
133
  int32_t        code = 0;
33,207,867✔
134
  int32_t        rspLen = 0;
33,207,867✔
135
  void          *pRsp = NULL;
33,207,867✔
136
  SSchemaWrapper schema = {0};
33,207,867✔
137
  SSchemaWrapper schemaTag = {0};
33,207,867✔
138
  uint8_t        autoCreateCtb = 0;
33,207,867✔
139

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

147
  if (infoReq.option == REQ_OPT_TBUID) reqTbUid = true;
33,207,153✔
148
  metaRsp.dbId = pVnode->config.dbId;
33,207,153✔
149
  tstrncpy(metaRsp.tbName, infoReq.tbName, TSDB_TABLE_NAME_LEN);
33,207,153✔
150
  (void)memcpy(metaRsp.dbFName, infoReq.dbFName, sizeof(metaRsp.dbFName));
33,207,867✔
151

152
  if (!reqTbUid) {
33,207,867✔
153
    (void)snprintf(tableFName, TSDB_TABLE_FNAME_LEN, "%s.%s", infoReq.dbFName, infoReq.tbName);
32,931,133✔
154
    if (pVnode->mounted) tTrimMountPrefix(tableFName);
32,931,133✔
155
    code = vnodeValidateTableHash(pVnode, tableFName);
32,930,587✔
156
    if (code) {
32,931,096✔
157
      goto _exit4;
×
158
    }
159
  }
160

161
  // query meta
162
  metaReaderDoInit(&mer1, pVnode->pMeta, META_READER_LOCK);
33,207,830✔
163
  if (reqTbUid) {
33,207,867✔
164
    SET_ERRNO(0);
276,734✔
165
    uint64_t tbUid = taosStr2UInt64(infoReq.tbName, NULL, 10);
276,734✔
166
    if (ERRNO == ERANGE || tbUid == 0) {
276,734✔
167
      code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
168
      goto _exit3;
×
169
    }
170
    SMetaReader mr3 = {0};
276,734✔
171
    metaReaderDoInit(&mr3, ((SVnode *)pVnode)->pMeta, META_READER_NOLOCK);
276,734✔
172
    if ((code = metaReaderGetTableEntryByUid(&mr3, tbUid)) < 0) {
276,734✔
173
      metaReaderClear(&mr3);
99,840✔
174
      TAOS_CHECK_GOTO(code, NULL, _exit3);
99,840✔
175
    }
176
    tstrncpy(metaRsp.tbName, mr3.me.name, TSDB_TABLE_NAME_LEN);
176,894✔
177
    metaReaderClear(&mr3);
176,667✔
178
    TAOS_CHECK_GOTO(metaGetTableEntryByName(&mer1, metaRsp.tbName), NULL, _exit3);
176,894✔
179
  } else if (metaGetTableEntryByName(&mer1, infoReq.tbName) < 0) {
32,931,133✔
180
    code = terrno;
10,440,694✔
181
    goto _exit3;
10,440,286✔
182
  }
183

184
  metaRsp.tableType = mer1.me.type;
22,666,947✔
185
  metaRsp.vgId = TD_VID(pVnode);
22,666,947✔
186
  metaRsp.tuid = mer1.me.uid;
22,666,947✔
187
  metaRsp.isAudit = pVnode->config.isAudit ? 1 : 0;
22,666,947✔
188

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

205
      tstrncpy(metaRsp.stbName, mer2.me.name, sizeof(metaRsp.stbName));
14,383,487✔
206
      metaRsp.suid = mer2.me.uid;
14,382,837✔
207
      metaRsp.ownerId = mer2.me.stbEntry.ownerId;  // child table inherits ownerId from stb
14,382,837✔
208
      metaRsp.secLvl = mer2.me.stbEntry.securityLevel;  // child table inherits secLvl from stb
14,382,837✔
209
      schema = mer2.me.stbEntry.schemaRow;
14,382,837✔
210
      schemaTag = mer2.me.stbEntry.schemaTag;
14,382,837✔
211
      break;
14,382,837✔
212
    }
213
    case TSDB_NORMAL_TABLE:
3,904,027✔
214
      metaRsp.secLvl = pVnode->config.securityLevel;  // normal table inherits secLvl from vnode config
3,904,027✔
215
    case TSDB_VIRTUAL_NORMAL_TABLE: {
4,054,477✔
216
      schema = mer1.me.ntbEntry.schemaRow;
4,054,477✔
217
      metaRsp.ownerId = mer1.me.ntbEntry.ownerId;
4,054,477✔
218
      break;
4,054,477✔
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;
22,666,261✔
227
  metaRsp.numOfColumns = schema.nCols;
22,666,261✔
228
  metaRsp.precision = pVnode->config.tsdbCfg.precision;
22,666,261✔
229
  metaRsp.sversion = schema.version;
22,666,488✔
230
  metaRsp.tversion = schemaTag.version;
22,666,488✔
231
  metaRsp.pSchemas = (SSchema *)taosMemoryMalloc(sizeof(SSchema) * (metaRsp.numOfColumns + metaRsp.numOfTags));
22,666,488✔
232
  metaRsp.pSchemaExt = (SSchemaExt *)taosMemoryCalloc(metaRsp.numOfColumns, sizeof(SSchemaExt));
22,666,908✔
233
  if (NULL == metaRsp.pSchemas || NULL == metaRsp.pSchemaExt) {
22,666,950✔
UNCOV
234
    code = terrno;
×
235
    goto _exit;
×
236
  }
237
  (void)memcpy(metaRsp.pSchemas, schema.pSchema, sizeof(SSchema) * schema.nCols);
22,666,950✔
238
  if (schemaTag.nCols) {
22,666,950✔
239
    (void)memcpy(metaRsp.pSchemas + schema.nCols, schemaTag.pSchema, sizeof(SSchema) * schemaTag.nCols);
18,612,246✔
240
  }
241
  if (metaRsp.pSchemaExt) {
22,666,950✔
242
    SMetaReader *pReader =
22,667,331✔
243
        (mer1.me.type == TSDB_CHILD_TABLE || mer1.me.type == TSDB_VIRTUAL_CHILD_TABLE) ? &mer2 : &mer1;
22,667,331✔
244
    code = fillTableColCmpr(pReader, metaRsp.pSchemaExt, metaRsp.numOfColumns);
22,667,331✔
245
    if (code < 0) {
22,666,950✔
246
      goto _exit;
×
247
    }
248
    for (int32_t i = 0; i < metaRsp.numOfColumns; i++) {
259,411,854✔
249
      metaRsp.pSchemaExt[i].colId = schema.pSchema[i].colId;
236,744,484✔
250
      if (pReader->me.pExtSchemas) {
236,745,884✔
251
        metaRsp.pSchemaExt[i].typeMod = pReader->me.pExtSchemas[i].typeMod;
27,061,283✔
252
      }
253
    }
254
  } else {
255
    code = TSDB_CODE_OUT_OF_MEMORY;
×
256
    goto _exit;
×
257
  }
258
  if (hasRefCol(mer1.me.type)) {
22,667,370✔
259
    metaRsp.rversion = mer1.me.colRef.version;
396,274✔
260
    metaRsp.pColRefs = (SColRef*)taosMemoryMalloc(sizeof(SColRef) * metaRsp.numOfColumns);
396,274✔
261
    if (metaRsp.pColRefs) {
395,179✔
262
      code = fillTableColRef(&mer1, metaRsp.pColRefs, metaRsp.numOfColumns);
395,179✔
263
      if (code < 0) {
395,179✔
264
        goto _exit;
×
265
      }
266
    }
267
    metaRsp.numOfColRefs = metaRsp.numOfColumns;
395,179✔
268

269
    // Fill tag references
270
    if (mer1.me.colRef.nTagRefs > 0) {
395,179✔
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;
395,179✔
285
      metaRsp.numOfTagRefs = 0;
395,179✔
286
    }
287
  } else {
288
    metaRsp.pColRefs = NULL;
22,271,057✔
289
    metaRsp.numOfColRefs = 0;
22,271,057✔
290
    metaRsp.pTagRefs = NULL;
22,271,057✔
291
    metaRsp.numOfTagRefs = 0;
22,271,057✔
292
  }
293

294
  vnodeDebugTableMeta(&metaRsp);
22,666,236✔
295

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

303
  if (direct) {
22,667,370✔
304
    pRsp = rpcMallocCont(rspLen);
565,977✔
305
  } else {
306
    pRsp = taosMemoryCalloc(1, rspLen);
22,101,393✔
307
  }
308

309
  if (pRsp == NULL) {
22,666,233✔
310
    code = terrno;
×
311
    goto _exit;
×
312
  }
313

314
  rspLen = tSerializeSTableMetaRsp(pRsp, rspLen, &metaRsp);
22,666,233✔
315
  if (rspLen < 0) {
22,666,364✔
316
    code = terrno;
×
317
    goto _exit;
×
318
  }
319

320
_exit:
22,665,983✔
321
  taosMemoryFree(metaRsp.pColRefs);
22,666,619✔
322
  taosMemoryFree(metaRsp.pSchemas);
22,665,613✔
323
  taosMemoryFree(metaRsp.pSchemaExt);
22,666,488✔
324
  taosMemoryFree(metaRsp.pTagRefs);
22,665,390✔
325
_exit2:
22,665,485✔
326
  metaReaderClear(&mer2);
22,666,490✔
327
_exit3:
33,207,496✔
328
  metaReaderClear(&mer1);
33,207,481✔
329
_exit4:
33,207,058✔
330
  rpcMsg.info = pMsg->info;
33,207,481✔
331
  rpcMsg.pCont = pRsp;
33,207,058✔
332
  rpcMsg.contLen = rspLen;
33,207,058✔
333
  rpcMsg.code = code;
33,207,058✔
334
  rpcMsg.msgType = pMsg->msgType;
33,207,058✔
335

336
  if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST && autoCreateCtb == 1) {
33,205,764✔
337
    code = TSDB_CODE_SUCCESS;
6,242,382✔
338
  }
339

340
  if (code) {
33,205,764✔
341
    qError("vgId:%d, get table %s meta with %" PRIu8 " failed cause of %s", pVnode->config.vgId, infoReq.tbName,
4,297,324✔
342
           infoReq.option, tstrerror(code));
343
  }
344

345
  if (direct) {
33,205,756✔
346
    tmsgSendRsp(&rpcMsg);
1,413,987✔
347
  } else {
348
    *pMsg = rpcMsg;
31,791,769✔
349
  }
350

351
  return code;
33,206,730✔
352
}
353

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

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

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

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

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

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

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

395
  if (mer1.me.type == TSDB_SUPER_TABLE) {
70,300✔
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) {
70,300✔
399
    metaReaderDoInit(&mer2, pVnode->pMeta, META_READER_NOLOCK);
49,685✔
400
    if (metaReaderGetTableEntryByUid(&mer2, mer1.me.ctbEntry.suid) < 0) goto _exit;
49,685✔
401

402
    tstrncpy(cfgRsp.stbName, mer2.me.name, TSDB_TABLE_NAME_LEN);
49,685✔
403
    schema = mer2.me.stbEntry.schemaRow;
49,685✔
404
    schemaTag = mer2.me.stbEntry.schemaTag;
49,685✔
405
    cfgRsp.ownerId = mer2.me.stbEntry.ownerId;        // child table inherits ownerId from stb
49,685✔
406
    cfgRsp.securityLevel = mer2.me.stbEntry.securityLevel;  // child table inherits secLvl from stb
49,685✔
407
    cfgRsp.ttl = mer1.me.ctbEntry.ttlDays;
49,685✔
408
    cfgRsp.commentLen = mer1.me.ctbEntry.commentLen;
49,685✔
409
    if (mer1.me.ctbEntry.commentLen > 0) {
49,685✔
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;
49,685✔
417
    cfgRsp.tagsLen = pTag->len;
49,685✔
418
    cfgRsp.pTags = taosMemoryMalloc(cfgRsp.tagsLen);
49,685✔
419
    if (NULL == cfgRsp.pTags) {
49,685✔
420
      code = terrno;
×
421
      goto _exit;
×
422
    }
423
    (void)memcpy(cfgRsp.pTags, pTag, cfgRsp.tagsLen);
49,685✔
424
  } else if (mer1.me.type == TSDB_NORMAL_TABLE || mer1.me.type == TSDB_VIRTUAL_NORMAL_TABLE) {
20,615✔
425
    schema = mer1.me.ntbEntry.schemaRow;
20,615✔
426
    cfgRsp.ttl = mer1.me.ntbEntry.ttlDays;
20,615✔
427
    cfgRsp.ownerId = mer1.me.ntbEntry.ownerId;
20,615✔
428
    cfgRsp.securityLevel = mer1.me.type == TSDB_NORMAL_TABLE ? pVnode->config.securityLevel : 0;
20,615✔
429
    cfgRsp.commentLen = mer1.me.ntbEntry.commentLen;
20,615✔
430
    if (mer1.me.ntbEntry.commentLen > 0) {
20,615✔
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;
70,300✔
444
  cfgRsp.numOfColumns = schema.nCols;
70,300✔
445
  cfgRsp.virtualStb = false; // vnode don't have super table, so it's always false
70,300✔
446
  cfgRsp.pSchemas = (SSchema *)taosMemoryMalloc(sizeof(SSchema) * (cfgRsp.numOfColumns + cfgRsp.numOfTags));
70,300✔
447
  cfgRsp.pSchemaExt = (SSchemaExt *)taosMemoryCalloc(cfgRsp.numOfColumns, sizeof(SSchemaExt));
70,300✔
448
  cfgRsp.pColRefs = (SColRef *)taosMemoryMalloc(sizeof(SColRef) * cfgRsp.numOfColumns);
70,300✔
449
  cfgRsp.numOfTagRefs = 0;
70,300✔
450
  cfgRsp.pTagRefs = NULL;
70,300✔
451

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

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

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

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

479
  cfgRsp.virtualStb = false;
70,300✔
480
  if (hasRefCol(cfgRsp.tableType)) {
70,300✔
481
    for (int32_t i = 0; i < cfgRsp.numOfColumns; i++) {
130,337✔
482
      SColRef *pRef = &pColRef->pColRef[i];
119,787✔
483
      cfgRsp.pColRefs[i].hasRef = pRef->hasRef;
119,787✔
484
      cfgRsp.pColRefs[i].id = pRef->id;
119,787✔
485
      if (cfgRsp.pColRefs[i].hasRef) {
119,787✔
486
        tstrncpy(cfgRsp.pColRefs[i].refDbName, pRef->refDbName, TSDB_DB_NAME_LEN);
64,768✔
487
        tstrncpy(cfgRsp.pColRefs[i].refTableName, pRef->refTableName, TSDB_TABLE_NAME_LEN);
64,768✔
488
        tstrncpy(cfgRsp.pColRefs[i].refColName, pRef->refColName, TSDB_COL_NAME_LEN);
64,768✔
489
      }
490
    }
491

492
    cfgRsp.numOfTagRefs = pColRef->nTagRefs;
10,550✔
493
    if (cfgRsp.numOfTagRefs > 0) {
10,550✔
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);
70,300✔
519
  if (rspLen < 0) {
70,300✔
520
    code = terrno;
×
521
    goto _exit;
×
522
  }
523

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

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

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

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

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

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

558
  tFreeSTableCfgRsp(&cfgRsp);
70,300✔
559
  metaReaderClear(&mer2);
70,300✔
560
  metaReaderClear(&mer1);
70,300✔
561
  return code;
70,300✔
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) {
30,447,454✔
574
  int32_t      code = 0;
30,447,454✔
575
  int32_t      rspSize = 0;
30,447,454✔
576
  SBatchReq    batchReq = {0};
30,447,454✔
577
  SBatchMsg   *req = NULL;
30,448,747✔
578
  SBatchRspMsg rsp = {0};
30,448,747✔
579
  SBatchRsp    batchRsp = {0};
30,435,046✔
580
  SRpcMsg      reqMsg = *pMsg;
30,434,360✔
581
  SRpcMsg      rspMsg = {0};
30,434,978✔
582
  void        *pRsp = NULL;
30,442,798✔
583

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

590
  int32_t msgNum = taosArrayGetSize(batchReq.pMsgs);
30,440,396✔
591
  if (msgNum >= MAX_META_MSG_IN_BATCH) {
30,442,267✔
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));
30,442,267✔
598
  if (NULL == batchRsp.pRsps) {
30,438,360✔
599
    code = terrno;
×
600
    qError("taosArrayInit %d SBatchRspMsg failed", msgNum);
×
601
    goto _exit;
×
602
  }
603

604
  for (int32_t i = 0; i < msgNum; ++i) {
66,849,809✔
605
    req = taosArrayGet(batchReq.pMsgs, i);
36,409,310✔
606
    if (req == NULL) {
36,406,716✔
607
      code = terrno;
×
608
      goto _exit;
×
609
    }
610

611
    reqMsg.msgType = req->msgType;
36,406,716✔
612
    reqMsg.pCont = req->msg;
36,410,026✔
613
    reqMsg.contLen = req->msgLen;
36,402,880✔
614

615
    switch (req->msgType) {
36,404,167✔
616
      case TDMT_VND_TABLE_META:
31,516,432✔
617
        // error code has been set into reqMsg, no need to handle it here.
618
        if (TSDB_CODE_SUCCESS != vnodeGetTableMeta(pVnode, &reqMsg, false)) {
31,516,432✔
619
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
3,350,302✔
620
        }
621
        break;
31,517,146✔
622
      case TDMT_VND_TABLE_NAME:
276,734✔
623
        // error code has been set into reqMsg, no need to handle it here.
624
        if (TSDB_CODE_SUCCESS != vnodeGetTableMeta(pVnode, &reqMsg, false)) {
276,734✔
625
          qWarn("vnodeGetBatchName failed, msgType:%d", req->msgType);
99,840✔
626
        }
627
        break;
276,734✔
628
      case TDMT_VND_TABLE_CFG:
70,300✔
629
        // error code has been set into reqMsg, no need to handle it here.
630
        if (TSDB_CODE_SUCCESS != vnodeGetTableCfg(pVnode, &reqMsg, false)) {
70,300✔
631
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
×
632
        }
633
        break;
70,300✔
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,537,358✔
641
        // error code has been set into reqMsg, no need to handle it here.
642
        if (TSDB_CODE_SUCCESS != vnodeGetVStbRefDbs(pVnode, &reqMsg)) {
4,537,358✔
643
          qWarn("vnodeGetVStbRefDbs failed, msgType:%d", req->msgType);
×
644
        }
645
        break;
4,538,699✔
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;
36,402,879✔
655
    rsp.reqType = reqMsg.msgType;
36,406,063✔
656
    rsp.msgLen = reqMsg.contLen;
36,406,063✔
657
    rsp.rspCode = reqMsg.code;
36,406,063✔
658
    rsp.msg = reqMsg.pCont;
36,406,063✔
659

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

667
  rspSize = tSerializeSBatchRsp(NULL, 0, &batchRsp);
30,440,499✔
668
  if (rspSize < 0) {
30,421,763✔
669
    qError("tSerializeSBatchRsp failed");
×
670
    code = terrno;
×
671
    goto _exit;
×
672
  }
673
  pRsp = rpcMallocCont(rspSize);
30,421,763✔
674
  if (pRsp == NULL) {
30,423,962✔
675
    qError("rpcMallocCont %d failed", rspSize);
×
676
    code = terrno;
×
677
    goto _exit;
×
678
  }
679
  if (tSerializeSBatchRsp(pRsp, rspSize, &batchRsp) < 0) {
30,423,962✔
680
    qError("tSerializeSBatchRsp %d failed", rspSize);
×
681
    code = terrno;
×
682
    goto _exit;
×
683
  }
684

685
_exit:
30,439,390✔
686

687
  rspMsg.info = pMsg->info;
30,442,047✔
688
  rspMsg.pCont = pRsp;
30,439,435✔
689
  rspMsg.contLen = rspSize;
30,439,435✔
690
  rspMsg.code = code;
30,439,435✔
691
  rspMsg.msgType = pMsg->msgType;
30,439,435✔
692

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

697
  taosArrayDestroyEx(batchReq.pMsgs, tFreeSBatchReqMsg);
30,421,892✔
698
  taosArrayDestroyEx(batchRsp.pRsps, tFreeSBatchRspMsg);
30,421,091✔
699

700
  tmsgSendRsp(&rspMsg);
30,425,034✔
701

702
  return code;
30,446,115✔
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,540,660✔
806
  int32_t                    code = TSDB_CODE_SUCCESS;
4,540,660✔
807
  int32_t                    line = 0;
4,540,660✔
808
  SMetaReader                mr = {0};
4,540,660✔
809
  bool                       readerInit = false;
4,541,965✔
810
  SSHashObj*                 pDbNameHash = NULL;
4,541,965✔
811
  SArray*                    pList = NULL;
4,541,965✔
812

813
  pList = taosArrayInit(10, sizeof(uint64_t));
4,541,965✔
814
  QUERY_CHECK_NULL(pList, code, line, _return, terrno);
4,534,211✔
815

816
  *ppRes = taosArrayInit(10, POINTER_BYTES);
4,534,211✔
817
  QUERY_CHECK_NULL(*ppRes, code, line, _return, terrno)
4,538,709✔
818
  
819
  // lookup in cache
820
  code = pHandle->api.metaFn.metaGetCachedRefDbs(pHandle->vnode, suid, *ppRes);
4,540,591✔
821
  QUERY_CHECK_CODE(code, line, _return);
4,547,846✔
822

823
  if (taosArrayGetSize(*ppRes) > 0) {
4,547,846✔
824
    // found in cache
825
    goto _return;
4,445,158✔
826
  } else {
827
    code = pHandle->api.metaFn.getChildTableList(pHandle->vnode, suid, pList);
102,688✔
828
    QUERY_CHECK_CODE(code, line, _return);
103,255✔
829

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

834
    for (int32_t i = 0; i < num; ++i) {
538,481✔
835
      uint64_t* id = taosArrayGet(pList, i);
435,226✔
836
      QUERY_CHECK_NULL(id, code, line, _return, terrno);
435,226✔
837

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

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

844
      for (int32_t j = 0; j < mr.me.colRef.nCols; j++) {
42,218,350✔
845
        if (mr.me.colRef.pColRef[j].hasRef) {
41,783,124✔
846
          if (NULL == tSimpleHashGet(pDbNameHash, mr.me.colRef.pColRef[j].refDbName, strlen(mr.me.colRef.pColRef[j].refDbName))) {
40,906,584✔
847
            char *refDbName = taosStrdup(mr.me.colRef.pColRef[j].refDbName);
88,415✔
848
            QUERY_CHECK_NULL(refDbName, code, line, _return, terrno);
88,415✔
849

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

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

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

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

866
_return:
4,548,413✔
867

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

872
  taosArrayDestroy(pList);
4,548,413✔
873
  tSimpleHashCleanup(pDbNameHash);
4,544,519✔
874

875
  if (code) {
4,545,787✔
876
    qError("%s failed since %s", __func__, tstrerror(code));
×
877
  }
878
  return code;
4,545,787✔
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,543,102✔
944
  int32_t        code = 0;
4,543,102✔
945
  int32_t        rspSize = 0;
4,543,102✔
946
  SVStbRefDbsReq req = {0};
4,543,102✔
947
  SVStbRefDbsRsp rsp = {0};
4,543,102✔
948
  SRpcMsg        rspMsg = {0};
4,532,261✔
949
  void          *pRsp = NULL;
4,532,261✔
950
  int32_t        line = 0;
4,532,261✔
951

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

958
  SReadHandle handle = {0};
4,539,300✔
959
  handle.vnode = pVnode;
4,540,620✔
960
  initStorageAPI(&handle.api);
4,540,620✔
961

962
  code = vnodeReadVStbRefDbs(&handle, req.suid, &rsp.pDbs);
4,532,807✔
963
  QUERY_CHECK_CODE(code, line, _return);
4,546,485✔
964
  rsp.vgId = TD_VID(pVnode);
4,546,485✔
965

966
  rspSize = tSerializeSVStbRefDbsRsp(NULL, 0, &rsp);
4,546,445✔
967
  if (rspSize < 0) {
4,536,085✔
968
    code = rspSize;
×
969
    qError("tSerializeSVStbRefDbsRsp failed, error:%d", rspSize);
×
970
    goto _return;
×
971
  }
972
  pRsp = taosMemoryCalloc(1, rspSize);
4,536,085✔
973
  if (pRsp == NULL) {
4,535,549✔
974
    code = terrno;
×
975
    qError("rpcMallocCont %d failed, error:%d", rspSize, terrno);
×
976
    goto _return;
×
977
  }
978
  rspSize = tSerializeSVStbRefDbsRsp(pRsp, rspSize, &rsp);
4,535,549✔
979
  if (rspSize < 0) {
4,536,718✔
980
    code = rspSize;
×
981
    qError("tSerializeSVStbRefDbsRsp failed, error:%d", rspSize);
×
982
    goto _return;
×
983
  }
984

985
_return:
4,536,718✔
986

987
  rspMsg.info = pMsg->info;
4,542,557✔
988
  rspMsg.pCont = pRsp;
4,536,770✔
989
  rspMsg.contLen = rspSize;
4,536,770✔
990
  rspMsg.code = code;
4,536,770✔
991
  rspMsg.msgType = pMsg->msgType;
4,536,770✔
992

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

997
  *pMsg = rspMsg;
4,529,581✔
998

999
  tDestroySVStbRefDbsRsp(&rsp);
4,527,012✔
1000

1001
  return code;
4,536,084✔
1002
}
1003

1004
static int32_t vnodeGetCompStorage(SVnode *pVnode, int64_t *output) {
175,203,439✔
1005
  int32_t code = 0;
175,203,439✔
1006
#ifdef TD_ENTERPRISE
1007
  int32_t now = taosGetTimestampSec();
175,203,439✔
1008
  if (llabs(now - pVnode->config.vndStats.storageLastUpd) >= 30) {
175,203,439✔
1009
    pVnode->config.vndStats.storageLastUpd = now;
8,831,513✔
1010

1011
    SDbSizeStatisInfo info = {0};
8,831,513✔
1012
    if (0 == (code = vnodeGetDBSize(pVnode, &info))) {
8,831,513✔
1013
      int64_t compSize =
8,831,513✔
1014
          info.l1Size + info.l2Size + info.l3Size + info.cacheSize + info.walSize + info.metaSize + +info.ssSize;
8,831,513✔
1015
      if (compSize >= 0) {
8,831,513✔
1016
        pVnode->config.vndStats.compStorage = compSize;
8,831,513✔
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;
175,203,439✔
1026
#endif
1027
  return code;
175,203,439✔
1028
}
1029

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

1036
    if (pVnode->inUse) {
175,203,439✔
1037
      *bufferSegmentUsed = pVnode->inUse->size;
175,185,135✔
1038
    }
1039
    *bufferSegmentSize = pVnode->config.szBuf / VNODE_BUFPOOL_SEGMENTS;
175,203,439✔
1040

1041
    (void)taosThreadMutexUnlock(&pVnode->mutex);
175,203,439✔
1042
  }
1043
}
175,203,439✔
1044

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

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

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

1105
  if (dbname) {
21,192,860✔
1106
    *dbname = pConf->dbname;
20,494,742✔
1107
  }
1108

1109
  if (vgId) {
21,191,392✔
1110
    *vgId = TD_VID(pVnodeObj);
20,095,533✔
1111
  }
1112

1113
  if (numOfTables) {
21,190,241✔
1114
    *numOfTables = pConf->vndStats.numOfNTables + pConf->vndStats.numOfCTables +
23,623✔
1115
                   pConf->vndStats.numOfVTables + pConf->vndStats.numOfVCTables;
11,831✔
1116
  }
1117

1118
  if (numOfNormalTables) {
21,190,280✔
1119
    *numOfNormalTables = pConf->vndStats.numOfNTables +
18,834✔
1120
                         pConf->vndStats.numOfVTables;
9,417✔
1121
  }
1122
}
21,190,280✔
1123

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

1129
int32_t vnodeGetTableList(void *pVnode, int8_t type, SArray *pList) {
9,417✔
1130
  if (type == TSDB_SUPER_TABLE) {
9,417✔
1131
    return vnodeGetStbIdList(pVnode, 0, pList);
9,417✔
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) {
126,050,397✔
1168
  int32_t      code = TSDB_CODE_SUCCESS;
126,050,397✔
1169
  SVnode      *pVnodeObj = pVnode;
126,050,397✔
1170
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnodeObj, suid, 1);
126,050,397✔
1171
  if (NULL == pCur) {
126,021,904✔
1172
    qError("vnode get all table list failed");
×
1173
    return terrno;
×
1174
  }
1175

1176
  while (1) {
494,353,882✔
1177
    tb_uid_t id = metaCtbCursorNext(pCur);
620,375,786✔
1178
    if (id == 0) {
620,337,270✔
1179
      break;
126,078,987✔
1180
    }
1181
    qTrace("vnodeGetCtbIdList: got ctb id %" PRId64 " for suid %" PRId64, id, suid);
494,258,283✔
1182
    if (NULL == taosArrayPush(list, &id)) {
494,347,380✔
1183
      qError("taosArrayPush failed");
×
1184
      code = terrno;
×
1185
      goto _exit;
×
1186
    }
1187
  }
1188

1189
_exit:
126,020,070✔
1190
  metaCloseCtbCursor(pCur);
126,020,070✔
1191
  return code;
126,020,475✔
1192
}
1193

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

1201
  while (1) {
57,460,847✔
1202
    tb_uid_t id = metaStbCursorNext(pCur);
135,917,173✔
1203
    if (id == 0) {
135,923,599✔
1204
      break;
78,461,512✔
1205
    }
1206

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

1214
_exit:
78,457,942✔
1215
  metaCloseStbCursor(pCur);
78,457,942✔
1216
  return code;
78,452,944✔
1217
}
1218

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

1227
  while (1) {
7,716✔
1228
    tb_uid_t id = metaStbCursorNext(pCur);
13,946✔
1229
    if (id == 0) {
13,946✔
1230
      break;
6,230✔
1231
    }
1232

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

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

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

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

1255
  *num = 0;
5,410,148✔
1256
  while (1) {
3,165,822✔
1257
    tb_uid_t id = metaCtbCursorNext(pCur);
8,575,649✔
1258
    if (id == 0) {
8,574,480✔
1259
      break;
5,409,465✔
1260
    }
1261

1262
    ++(*num);
3,165,015✔
1263
  }
1264

1265
  metaCloseCtbCursor(pCur);
5,409,465✔
1266
  return TSDB_CODE_SUCCESS;
5,411,180✔
1267
}
1268

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

1278
  return TSDB_CODE_SUCCESS;
5,410,410✔
1279
}
1280

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

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

1294
  metaReaderClear(&mr);
5,410,719✔
1295
  return TSDB_CODE_SUCCESS;
5,411,144✔
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) {
78,456,503✔
1373
  int32_t      code = TSDB_CODE_SUCCESS;
78,456,503✔
1374
  int32_t      tbNum = 0;
78,456,503✔
1375
  const char **pTbArr = NULL;
78,456,503✔
1376
  const char  *dbName = NULL;
78,456,503✔
1377
  *tbSize = 0;
78,456,503✔
1378

1379
  if (!(dbName = strchr(pVnode->config.dbname, '.'))) return 0;
78,454,361✔
1380
  if (0 == strncmp(++dbName, "log", TSDB_DB_NAME_LEN)) {
78,453,647✔
1381
    tbNum = tkLogStbNum;
4,264✔
1382
    pTbArr = (const char **)&tkLogStb;
4,264✔
1383
  } else if (0 == strncmp(dbName, "audit", TSDB_DB_NAME_LEN) || pVnode->config.isAudit) {
78,452,953✔
1384
    tbNum = tkAuditStbNum;
6,216✔
1385
    pTbArr = (const char **)&tkAuditStb;
6,216✔
1386
  }
1387
  if (tbNum && pTbArr) {
78,457,859✔
1388
    *tbSize = metaSizeOfTbFilterCache(pVnode->pMeta, 0);
10,480✔
1389
    if (*tbSize < tbNum) {
10,480✔
1390
      for (int32_t i = 0; i < tbNum; ++i) {
109,384✔
1391
        tb_uid_t suid = metaGetTableEntryUidByName(pVnode->pMeta, pTbArr[i]);
107,491✔
1392
        if (suid != 0) {
107,491✔
1393
          code = metaPutTbToFilterCache(pVnode->pMeta, &suid, 0);
5,409✔
1394
          if (TSDB_CODE_SUCCESS != code) {
5,409✔
1395
            return code;
3,140✔
1396
          }
1397
        }
1398
      }
1399
      *tbSize = metaSizeOfTbFilterCache(pVnode->pMeta, 0);
1,893✔
1400
    }
1401
  }
1402

1403
  return code;
78,452,649✔
1404
}
1405
#endif
1406

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

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

1416
int32_t vnodeGetTimeSeriesNum(SVnode *pVnode, int64_t *num) {
78,455,789✔
1417
  SArray *suidList = NULL;
78,455,789✔
1418

1419
  if (!(suidList = taosArrayInit(1, sizeof(tb_uid_t)))) {
78,455,789✔
1420
    return terrno;
×
1421
  }
1422

1423
  int32_t tbFilterSize = 0;
78,451,833✔
1424
  int32_t code = TSDB_CODE_SUCCESS;
78,452,547✔
1425
#ifdef TD_ENTERPRISE
1426
  code = vnodeGetTimeSeriesBlackList(pVnode, &tbFilterSize);
78,452,547✔
1427
  if (TSDB_CODE_SUCCESS != code) {
78,454,361✔
1428
    goto _exit;
3,140✔
1429
  }
1430
#endif
1431

1432
  if ((!tbFilterSize && vnodeGetStbIdList(pVnode, 0, suidList) < 0) ||
78,451,221✔
1433
      (tbFilterSize && vnodeGetStbIdListByFilter(pVnode, 0, suidList, vnodeTimeSeriesFilter, pVnode) < 0)) {
78,446,937✔
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;
78,449,793✔
1440
  int64_t arrSize = taosArrayGetSize(suidList);
78,453,363✔
1441
  for (int64_t i = 0; i < arrSize; ++i) {
135,899,800✔
1442
    tb_uid_t suid = *(tb_uid_t *)taosArrayGet(suidList, i);
57,445,395✔
1443

1444
    int64_t ctbNum = 0;
57,441,111✔
1445
    int32_t numOfCols = 0;
57,444,681✔
1446
    int8_t  flags = 0;
57,435,399✔
1447
    code = metaGetStbStats(pVnode, suid, &ctbNum, &numOfCols, &flags);
57,445,395✔
1448
    if (TSDB_CODE_SUCCESS != code) {
57,458,961✔
1449
      goto _exit;
×
1450
    }
1451
    if (!TABLE_IS_VIRTUAL(flags)) {
57,458,961✔
1452
      *num += ctbNum * (numOfCols - 1);
52,128,058✔
1453
    }
1454
  }
1455

1456
_exit:
78,455,429✔
1457
  taosArrayDestroy(suidList);
78,457,545✔
1458
  return TSDB_CODE_SUCCESS;
78,449,049✔
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,736,677✔
1489
  if (pVnode == NULL) {
4,736,677✔
1490
    return NULL;
×
1491
  }
1492

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

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

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

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

1511
  char   *dirName[] = {VNODE_TSDB_DIR, VNODE_WAL_DIR, VNODE_META_DIR, VNODE_TSDB_CACHE_DIR};
8,838,679✔
1512
  int64_t dirSize[4];
8,837,392✔
1513

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

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

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

1544
  code = tsdbGetFsSize(pVnodeObj->pTsdb, pInfo);
8,838,679✔
1545
_exit:
8,838,679✔
1546
  return code;
8,838,679✔
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