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

taosdata / TDengine / #4953

09 Feb 2026 01:16AM UTC coverage: 66.877% (+0.008%) from 66.869%
#4953

push

travis-ci

web-flow
docs: add support for recording STMT to CSV files (#34276)

* docs: add support for recording STMT to CSV files

* docs: update version for STMT recording feature in CSV files

205779 of 307696 relevant lines covered (66.88%)

126028030.19 hits per line

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

65.67
/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) {
3,958,380✔
29
  return qWorkerInit(NODE_TYPE_VNODE, TD_VID(pVnode), (void **)&pVnode->pQuery, &pVnode->msgCb);
3,958,380✔
30
}
31

32
void vnodeQueryPreClose(SVnode *pVnode) { qWorkerStopAllTasks((void *)pVnode->pQuery); }
3,958,966✔
33

34
void vnodeQueryClose(SVnode *pVnode) { qWorkerDestroy((void **)&pVnode->pQuery); }
3,962,014✔
35

36
int32_t fillTableColCmpr(SMetaReader *reader, SSchemaExt *pExt, int32_t numOfCol) {
19,230,343✔
37
  int8_t tblType = reader->me.type;
19,230,343✔
38
  if (withExtSchema(tblType)) {
19,230,343✔
39
    SColCmprWrapper *p = &(reader->me.colCmpr);
18,935,752✔
40
    if (numOfCol != p->nCols) {
18,935,752✔
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++) {
222,423,371✔
45
      SColCmpr *pCmpr = &p->pColCmpr[i];
203,488,343✔
46
      pExt[i].colId = pCmpr->id;
203,487,131✔
47
      pExt[i].compress = pCmpr->alg;
203,487,641✔
48
    }
49
  }
50
  return 0;
19,230,397✔
51
}
52

53
void vnodePrintTableMeta(STableMetaRsp *pMeta) {
19,230,548✔
54
  if (!(qDebugFlag & DEBUG_DEBUG)) {
19,230,548✔
55
    return;
286,369✔
56
  }
57

58
  qDebug("tbName:%s", pMeta->tbName);
18,944,179✔
59
  qDebug("stbName:%s", pMeta->stbName);
18,944,607✔
60
  qDebug("dbFName:%s", pMeta->dbFName);
18,944,607✔
61
  qDebug("dbId:%" PRId64, pMeta->dbId);
18,944,607✔
62
  qDebug("numOfTags:%d", pMeta->numOfTags);
18,944,607✔
63
  qDebug("numOfColumns:%d", pMeta->numOfColumns);
18,944,607✔
64
  qDebug("precision:%d", pMeta->precision);
18,944,607✔
65
  qDebug("tableType:%d", pMeta->tableType);
18,944,607✔
66
  qDebug("sversion:%d", pMeta->sversion);
18,944,607✔
67
  qDebug("tversion:%d", pMeta->tversion);
18,944,607✔
68
  qDebug("suid:%" PRIu64, pMeta->suid);
18,944,607✔
69
  qDebug("tuid:%" PRIu64, pMeta->tuid);
18,944,607✔
70
  qDebug("vgId:%d", pMeta->vgId);
18,944,412✔
71
  qDebug("sysInfo:%d", pMeta->sysInfo);
18,944,607✔
72
  if (pMeta->pSchemas) {
18,944,607✔
73
    for (int32_t i = 0; i < (pMeta->numOfColumns + pMeta->numOfTags); ++i) {
258,534,770✔
74
      SSchema *pSchema = pMeta->pSchemas + i;
239,590,163✔
75
      qDebug("%d col/tag: type:%d, flags:%d, colId:%d, bytes:%d, name:%s", i, pSchema->type, pSchema->flags,
239,590,163✔
76
             pSchema->colId, pSchema->bytes, pSchema->name);
77
    }
78
  }
79
}
80

81
int32_t fillTableColRef(SMetaReader *reader, SColRef *pRef, int32_t numOfCol) {
295,224✔
82
  int8_t tblType = reader->me.type;
295,224✔
83
  if (hasRefCol(tblType)) {
295,224✔
84
    SColRefWrapper *p = &(reader->me.colRef);
295,224✔
85
    if (numOfCol != p->nCols) {
295,224✔
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++) {
2,447,272✔
90
      SColRef *pColRef = &p->pColRef[i];
2,152,048✔
91
      pRef[i].hasRef = pColRef->hasRef;
2,152,048✔
92
      pRef[i].id = pColRef->id;
2,152,048✔
93
      if(pRef[i].hasRef) {
2,152,048✔
94
        tstrncpy(pRef[i].refDbName, pColRef->refDbName, TSDB_DB_NAME_LEN);
1,522,355✔
95
        tstrncpy(pRef[i].refTableName, pColRef->refTableName, TSDB_TABLE_NAME_LEN);
1,522,355✔
96
        tstrncpy(pRef[i].refColName, pColRef->refColName, TSDB_COL_NAME_LEN);
1,522,355✔
97
      }
98
    }
99
  }
100
  return 0;
295,224✔
101
}
102

103
int32_t vnodeGetTableMeta(SVnode *pVnode, SRpcMsg *pMsg, bool direct) {
28,285,246✔
104
  STableInfoReq  infoReq = {0};
28,285,246✔
105
  STableMetaRsp  metaRsp = {0};
28,285,246✔
106
  SMetaReader    mer1 = {0};
28,285,246✔
107
  SMetaReader    mer2 = {0};
28,285,243✔
108
  char           tableFName[TSDB_TABLE_FNAME_LEN];
28,282,274✔
109
  bool           reqTbUid = false;
28,285,246✔
110
  SRpcMsg        rpcMsg = {0};
28,285,246✔
111
  int32_t        code = 0;
28,285,246✔
112
  int32_t        rspLen = 0;
28,285,246✔
113
  void          *pRsp = NULL;
28,285,246✔
114
  SSchemaWrapper schema = {0};
28,285,246✔
115
  SSchemaWrapper schemaTag = {0};
28,285,246✔
116
  uint8_t        autoCreateCtb = 0;
28,285,246✔
117

118
  // decode req
119
  if (tDeserializeSTableInfoReq(pMsg->pCont, pMsg->contLen, &infoReq) != 0) {
28,285,246✔
120
    code = terrno;
×
121
    goto _exit4;
×
122
  }
123
  autoCreateCtb = infoReq.autoCreateCtb;
28,285,246✔
124

125
  if (infoReq.option == REQ_OPT_TBUID) reqTbUid = true;
28,285,246✔
126
  metaRsp.dbId = pVnode->config.dbId;
28,285,246✔
127
  tstrncpy(metaRsp.tbName, infoReq.tbName, TSDB_TABLE_NAME_LEN);
28,285,246✔
128
  (void)memcpy(metaRsp.dbFName, infoReq.dbFName, sizeof(metaRsp.dbFName));
28,285,246✔
129

130
  if (!reqTbUid) {
28,285,246✔
131
    (void)tsnprintf(tableFName, TSDB_TABLE_FNAME_LEN, "%s.%s", infoReq.dbFName, infoReq.tbName);
28,020,301✔
132
    if (pVnode->mounted) tTrimMountPrefix(tableFName);
28,020,301✔
133
    code = vnodeValidateTableHash(pVnode, tableFName);
28,020,301✔
134
    if (code) {
28,020,247✔
135
      goto _exit4;
×
136
    }
137
  }
138

139
  // query meta
140
  metaReaderDoInit(&mer1, pVnode->pMeta, META_READER_LOCK);
28,285,192✔
141
  if (reqTbUid) {
28,285,243✔
142
    SET_ERRNO(0);
264,945✔
143
    uint64_t tbUid = taosStr2UInt64(infoReq.tbName, NULL, 10);
264,945✔
144
    if (ERRNO == ERANGE || tbUid == 0) {
264,945✔
145
      code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
146
      goto _exit3;
×
147
    }
148
    SMetaReader mr3 = {0};
264,945✔
149
    metaReaderDoInit(&mr3, ((SVnode *)pVnode)->pMeta, META_READER_NOLOCK);
264,945✔
150
    if ((code = metaReaderGetTableEntryByUid(&mr3, tbUid)) < 0) {
264,945✔
151
      metaReaderClear(&mr3);
95,407✔
152
      TAOS_CHECK_GOTO(code, NULL, _exit3);
95,407✔
153
    }
154
    tstrncpy(metaRsp.tbName, mr3.me.name, TSDB_TABLE_NAME_LEN);
169,343✔
155
    metaReaderClear(&mr3);
169,343✔
156
    TAOS_CHECK_GOTO(metaGetTableEntryByName(&mer1, metaRsp.tbName), NULL, _exit3);
169,343✔
157
  } else if (metaGetTableEntryByName(&mer1, infoReq.tbName) < 0) {
28,020,298✔
158
    code = terrno;
8,958,863✔
159
    goto _exit3;
8,958,863✔
160
  }
161

162
  metaRsp.tableType = mer1.me.type;
19,230,148✔
163
  metaRsp.vgId = TD_VID(pVnode);
19,230,148✔
164
  metaRsp.tuid = mer1.me.uid;
19,230,343✔
165
  metaRsp.isAudit = pVnode->config.isAudit ? 1 : 0;
19,230,343✔
166

167
  switch (mer1.me.type) {
19,230,452✔
168
    case TSDB_SUPER_TABLE: {
3,631,052✔
169
      (void)strcpy(metaRsp.stbName, mer1.me.name);
3,631,052✔
170
      schema = mer1.me.stbEntry.schemaRow;
3,631,052✔
171
      schemaTag = mer1.me.stbEntry.schemaTag;
3,631,052✔
172
      metaRsp.suid = mer1.me.uid;
3,631,052✔
173
      metaRsp.virtualStb = TABLE_IS_VIRTUAL(mer1.me.flags);
3,631,052✔
174
      metaRsp.ownerId = mer1.me.stbEntry.ownerId;
3,631,052✔
175
      break;
3,631,052✔
176
    }
177
    case TSDB_CHILD_TABLE:
12,278,328✔
178
    case TSDB_VIRTUAL_CHILD_TABLE:{
179
      metaReaderDoInit(&mer2, pVnode->pMeta, META_READER_NOLOCK);
12,278,328✔
180
      if (metaReaderGetTableEntryByUid(&mer2, mer1.me.ctbEntry.suid) < 0) goto _exit2;
12,278,852✔
181

182
      (void)strcpy(metaRsp.stbName, mer2.me.name);
12,278,473✔
183
      metaRsp.suid = mer2.me.uid;
12,278,473✔
184
      metaRsp.ownerId = mer2.me.stbEntry.ownerId;  // child table inherits ownerId from stb
12,278,473✔
185
      schema = mer2.me.stbEntry.schemaRow;
12,278,473✔
186
      schemaTag = mer2.me.stbEntry.schemaTag;
12,278,473✔
187
      break;
12,278,473✔
188
    }
189
    case TSDB_NORMAL_TABLE:
3,321,072✔
190
    case TSDB_VIRTUAL_NORMAL_TABLE: {
191
      schema = mer1.me.ntbEntry.schemaRow;
3,321,072✔
192
      metaRsp.ownerId = mer1.me.ntbEntry.ownerId;
3,321,072✔
193
      break;
3,321,072✔
194
    }
195
    default: {
×
196
      vError("vnodeGetTableMeta get invalid table type:%d", mer1.me.type);
×
197
      goto _exit3;
×
198
    }
199
  }
200

201
  metaRsp.numOfTags = schemaTag.nCols;
19,230,597✔
202
  metaRsp.numOfColumns = schema.nCols;
19,230,597✔
203
  metaRsp.precision = pVnode->config.tsdbCfg.precision;
19,230,597✔
204
  metaRsp.sversion = schema.version;
19,230,976✔
205
  metaRsp.tversion = schemaTag.version;
19,230,976✔
206
  metaRsp.pSchemas = (SSchema *)taosMemoryMalloc(sizeof(SSchema) * (metaRsp.numOfColumns + metaRsp.numOfTags));
19,230,976✔
207
  metaRsp.pSchemaExt = (SSchemaExt *)taosMemoryCalloc(metaRsp.numOfColumns, sizeof(SSchemaExt));
19,230,831✔
208
  if (NULL == metaRsp.pSchemas || NULL == metaRsp.pSchemaExt) {
19,230,869✔
209
    code = terrno;
×
210
    goto _exit;
×
211
  }
212
  (void)memcpy(metaRsp.pSchemas, schema.pSchema, sizeof(SSchema) * schema.nCols);
19,230,869✔
213
  if (schemaTag.nCols) {
19,230,869✔
214
    (void)memcpy(metaRsp.pSchemas + schema.nCols, schemaTag.pSchema, sizeof(SSchema) * schemaTag.nCols);
15,909,797✔
215
  }
216
  if (metaRsp.pSchemaExt) {
19,230,869✔
217
    SMetaReader *pReader = mer1.me.type == TSDB_CHILD_TABLE ? &mer2 : &mer1;
19,230,343✔
218
    code = fillTableColCmpr(pReader, metaRsp.pSchemaExt, metaRsp.numOfColumns);
19,230,343✔
219
    if (code < 0) {
19,230,397✔
220
      goto _exit;
×
221
    }
222
    for (int32_t i = 0; i < metaRsp.numOfColumns && pReader->me.pExtSchemas; i++) {
44,132,302✔
223
      metaRsp.pSchemaExt[i].typeMod = pReader->me.pExtSchemas[i].typeMod;
24,901,905✔
224
    }
225
  } else {
226
    code = TSDB_CODE_OUT_OF_MEMORY;
526✔
227
    goto _exit;
526✔
228
  }
229
  if (hasRefCol(mer1.me.type)) {
19,231,068✔
230
    metaRsp.rversion = mer1.me.colRef.version;
295,911✔
231
    metaRsp.pColRefs = (SColRef*)taosMemoryMalloc(sizeof(SColRef) * metaRsp.numOfColumns);
295,911✔
232
    if (metaRsp.pColRefs) {
295,224✔
233
      code = fillTableColRef(&mer1, metaRsp.pColRefs, metaRsp.numOfColumns);
295,224✔
234
      if (code < 0) {
295,224✔
235
        goto _exit;
×
236
      }
237
    }
238
    metaRsp.numOfColRefs = metaRsp.numOfColumns;
295,224✔
239
  } else {
240
    metaRsp.pColRefs = NULL;
18,934,691✔
241
    metaRsp.numOfColRefs = 0;
18,934,691✔
242
  }
243

244
  vnodePrintTableMeta(&metaRsp);
19,229,915✔
245

246
  // encode and send response
247
  rspLen = tSerializeSTableMetaRsp(NULL, 0, &metaRsp);
19,230,976✔
248
  if (rspLen < 0) {
19,230,727✔
249
    code = terrno;
×
250
    goto _exit;
×
251
  }
252

253
  if (direct) {
19,230,727✔
254
    pRsp = rpcMallocCont(rspLen);
178,007✔
255
  } else {
256
    pRsp = taosMemoryCalloc(1, rspLen);
19,052,720✔
257
  }
258

259
  if (pRsp == NULL) {
19,230,548✔
260
    code = terrno;
×
261
    goto _exit;
×
262
  }
263

264
  rspLen = tSerializeSTableMetaRsp(pRsp, rspLen, &metaRsp);
19,230,548✔
265
  if (rspLen < 0) {
19,230,922✔
266
    code = terrno;
×
267
    goto _exit;
×
268
  }
269

270
_exit:
19,231,448✔
271
  taosMemoryFree(metaRsp.pColRefs);
19,230,976✔
272
  taosMemoryFree(metaRsp.pSchemas);
19,230,599✔
273
  taosMemoryFree(metaRsp.pSchemaExt);
19,230,976✔
274
_exit2:
19,230,025✔
275
  metaReaderClear(&mer2);
19,230,922✔
276
_exit3:
28,284,613✔
277
  metaReaderClear(&mer1);
28,285,246✔
278
_exit4:
28,283,928✔
279
  rpcMsg.info = pMsg->info;
28,285,048✔
280
  rpcMsg.pCont = pRsp;
28,285,051✔
281
  rpcMsg.contLen = rspLen;
28,285,051✔
282
  rpcMsg.code = code;
28,285,051✔
283
  rpcMsg.msgType = pMsg->msgType;
28,285,051✔
284

285
  if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST && autoCreateCtb == 1) {
28,283,942✔
286
    code = TSDB_CODE_SUCCESS;
5,529,421✔
287
  }
288

289
  if (code) {
28,283,942✔
290
    qError("vgId:%d, get table %s meta with %" PRIu8 " failed cause of %s", pVnode->config.vgId, infoReq.tbName,
3,524,117✔
291
           infoReq.option, tstrerror(code));
292
  }
293

294
  if (direct) {
28,284,493✔
295
    tmsgSendRsp(&rpcMsg);
893,776✔
296
  } else {
297
    *pMsg = rpcMsg;
27,390,717✔
298
  }
299

300
  return code;
28,285,246✔
301
}
302

303
int32_t vnodeGetTableCfg(SVnode *pVnode, SRpcMsg *pMsg, bool direct) {
56,996✔
304
  STableCfgReq   cfgReq = {0};
56,996✔
305
  STableCfgRsp   cfgRsp = {0};
56,996✔
306
  SMetaReader    mer1 = {0};
56,996✔
307
  SMetaReader    mer2 = {0};
56,996✔
308
  char           tableFName[TSDB_TABLE_FNAME_LEN];
56,996✔
309
  SRpcMsg        rpcMsg = {0};
56,996✔
310
  int32_t        code = 0;
56,996✔
311
  int32_t        rspLen = 0;
56,996✔
312
  void          *pRsp = NULL;
56,996✔
313
  SSchemaWrapper schema = {0};
56,996✔
314
  SSchemaWrapper schemaTag = {0};
56,996✔
315

316
  // decode req
317
  if (tDeserializeSTableCfgReq(pMsg->pCont, pMsg->contLen, &cfgReq) != 0) {
56,996✔
318
    code = terrno;
×
319
    goto _exit;
×
320
  }
321

322
  tstrncpy(cfgRsp.tbName, cfgReq.tbName, TSDB_TABLE_NAME_LEN);
56,996✔
323
  (void)memcpy(cfgRsp.dbFName, cfgReq.dbFName, sizeof(cfgRsp.dbFName));
56,996✔
324

325
  (void)tsnprintf(tableFName, TSDB_TABLE_FNAME_LEN, "%s.%s", cfgReq.dbFName, cfgReq.tbName);
56,996✔
326
  if (pVnode->mounted) tTrimMountPrefix(tableFName);
56,996✔
327
  code = vnodeValidateTableHash(pVnode, tableFName);
56,996✔
328
  if (code) {
56,996✔
329
    goto _exit;
×
330
  }
331

332
  // query meta
333
  metaReaderDoInit(&mer1, pVnode->pMeta, META_READER_LOCK);
56,996✔
334

335
  if (metaGetTableEntryByName(&mer1, cfgReq.tbName) < 0) {
56,996✔
336
    code = terrno;
×
337
    goto _exit;
×
338
  }
339

340
  cfgRsp.tableType = mer1.me.type;
56,996✔
341
  cfgRsp.isAudit = pVnode->config.isAudit ? 1 : 0;
56,996✔
342

343
  if (mer1.me.type == TSDB_SUPER_TABLE) {
56,996✔
344
    code = TSDB_CODE_VND_HASH_MISMATCH;
×
345
    goto _exit;
×
346
  } else if (mer1.me.type == TSDB_CHILD_TABLE || mer1.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
56,996✔
347
    metaReaderDoInit(&mer2, pVnode->pMeta, META_READER_NOLOCK);
39,296✔
348
    if (metaReaderGetTableEntryByUid(&mer2, mer1.me.ctbEntry.suid) < 0) goto _exit;
39,296✔
349

350
    tstrncpy(cfgRsp.stbName, mer2.me.name, TSDB_TABLE_NAME_LEN);
39,296✔
351
    schema = mer2.me.stbEntry.schemaRow;
39,296✔
352
    schemaTag = mer2.me.stbEntry.schemaTag;
39,296✔
353
    cfgRsp.ownerId = mer2.me.stbEntry.ownerId;  // child table inherits ownerId from stb
39,296✔
354
    cfgRsp.ttl = mer1.me.ctbEntry.ttlDays;
39,296✔
355
    cfgRsp.commentLen = mer1.me.ctbEntry.commentLen;
39,296✔
356
    if (mer1.me.ctbEntry.commentLen > 0) {
39,296✔
357
      cfgRsp.pComment = taosStrdup(mer1.me.ctbEntry.comment);
×
358
      if (NULL == cfgRsp.pComment) {
×
359
        code = terrno;
×
360
        goto _exit;
×
361
      }
362
    }
363
    STag *pTag = (STag *)mer1.me.ctbEntry.pTags;
39,296✔
364
    cfgRsp.tagsLen = pTag->len;
39,296✔
365
    cfgRsp.pTags = taosMemoryMalloc(cfgRsp.tagsLen);
39,296✔
366
    if (NULL == cfgRsp.pTags) {
39,296✔
367
      code = terrno;
×
368
      goto _exit;
×
369
    }
370
    (void)memcpy(cfgRsp.pTags, pTag, cfgRsp.tagsLen);
39,296✔
371
  } else if (mer1.me.type == TSDB_NORMAL_TABLE || mer1.me.type == TSDB_VIRTUAL_NORMAL_TABLE) {
17,700✔
372
    schema = mer1.me.ntbEntry.schemaRow;
17,700✔
373
    cfgRsp.ttl = mer1.me.ntbEntry.ttlDays;
17,700✔
374
    cfgRsp.ownerId = mer1.me.ntbEntry.ownerId;
17,700✔
375
    cfgRsp.commentLen = mer1.me.ntbEntry.commentLen;
17,700✔
376
    if (mer1.me.ntbEntry.commentLen > 0) {
17,700✔
377
      cfgRsp.pComment = taosStrdup(mer1.me.ntbEntry.comment);
×
378
      if (NULL == cfgRsp.pComment) {
×
379
        code = terrno;
×
380
        goto _exit;
×
381
      }
382
    }
383
  } else {
384
    vError("vnodeGetTableCfg get invalid table type:%d", mer1.me.type);
×
385
    code = TSDB_CODE_APP_ERROR;
×
386
    goto _exit;
×
387
  }
388

389
  cfgRsp.numOfTags = schemaTag.nCols;
56,996✔
390
  cfgRsp.numOfColumns = schema.nCols;
56,996✔
391
  cfgRsp.virtualStb = false; // vnode don't have super table, so it's always false
56,996✔
392
  cfgRsp.pSchemas = (SSchema *)taosMemoryMalloc(sizeof(SSchema) * (cfgRsp.numOfColumns + cfgRsp.numOfTags));
56,996✔
393
  cfgRsp.pSchemaExt = (SSchemaExt *)taosMemoryMalloc(cfgRsp.numOfColumns * sizeof(SSchemaExt));
56,996✔
394
  cfgRsp.pColRefs = (SColRef *)taosMemoryMalloc(sizeof(SColRef) * cfgRsp.numOfColumns);
56,996✔
395

396
  if (NULL == cfgRsp.pSchemas || NULL == cfgRsp.pSchemaExt || NULL == cfgRsp.pColRefs) {
56,996✔
397
    code = terrno;
×
398
    goto _exit;
×
399
  }
400
  (void)memcpy(cfgRsp.pSchemas, schema.pSchema, sizeof(SSchema) * schema.nCols);
56,996✔
401
  if (schemaTag.nCols) {
56,996✔
402
    (void)memcpy(cfgRsp.pSchemas + schema.nCols, schemaTag.pSchema, sizeof(SSchema) * schemaTag.nCols);
39,296✔
403
  }
404

405
  SMetaReader     *pReader = (mer1.me.type == TSDB_CHILD_TABLE || mer1.me.type == TSDB_VIRTUAL_CHILD_TABLE) ? &mer2 : &mer1;
56,996✔
406
  SColCmprWrapper *pColCmpr = &pReader->me.colCmpr;
56,996✔
407
  SColRefWrapper  *pColRef = &mer1.me.colRef;
56,996✔
408

409
  if (withExtSchema(cfgRsp.tableType)) {
56,996✔
410
    for (int32_t i = 0; i < cfgRsp.numOfColumns; i++) {
207,603✔
411
      SColCmpr   *pCmpr = &pColCmpr->pColCmpr[i];
158,211✔
412
      SSchemaExt *pSchExt = cfgRsp.pSchemaExt + i;
158,211✔
413
      pSchExt->colId = pCmpr->id;
158,211✔
414
      pSchExt->compress = pCmpr->alg;
158,211✔
415
      if (pReader->me.pExtSchemas)
158,211✔
416
        pSchExt->typeMod = pReader->me.pExtSchemas[i].typeMod;
24,068✔
417
      else
418
        pSchExt->typeMod = 0;
134,143✔
419
    }
420
  }
421

422
  cfgRsp.virtualStb = false;
56,996✔
423
  if (hasRefCol(cfgRsp.tableType)) {
56,996✔
424
    for (int32_t i = 0; i < cfgRsp.numOfColumns; i++) {
107,218✔
425
      SColRef *pRef = &pColRef->pColRef[i];
99,614✔
426
      cfgRsp.pColRefs[i].hasRef = pRef->hasRef;
99,614✔
427
      cfgRsp.pColRefs[i].id = pRef->id;
99,614✔
428
      if (cfgRsp.pColRefs[i].hasRef) {
99,614✔
429
        tstrncpy(cfgRsp.pColRefs[i].refDbName, pRef->refDbName, TSDB_DB_NAME_LEN);
52,343✔
430
        tstrncpy(cfgRsp.pColRefs[i].refTableName, pRef->refTableName, TSDB_TABLE_NAME_LEN);
52,343✔
431
        tstrncpy(cfgRsp.pColRefs[i].refColName, pRef->refColName, TSDB_COL_NAME_LEN);
52,343✔
432
      }
433
    }
434
  }
435

436
  // encode and send response
437
  rspLen = tSerializeSTableCfgRsp(NULL, 0, &cfgRsp);
56,996✔
438
  if (rspLen < 0) {
56,996✔
439
    code = terrno;
×
440
    goto _exit;
×
441
  }
442

443
  if (direct) {
56,996✔
444
    pRsp = rpcMallocCont(rspLen);
×
445
  } else {
446
    pRsp = taosMemoryCalloc(1, rspLen);
56,996✔
447
  }
448

449
  if (pRsp == NULL) {
56,996✔
450
    code = terrno;
×
451
    goto _exit;
×
452
  }
453

454
  rspLen = tSerializeSTableCfgRsp(pRsp, rspLen, &cfgRsp);
56,996✔
455
  if (rspLen < 0) {
56,996✔
456
    code = terrno;
×
457
    goto _exit;
×
458
  }
459

460
_exit:
56,996✔
461
  rpcMsg.info = pMsg->info;
56,996✔
462
  rpcMsg.pCont = pRsp;
56,996✔
463
  rpcMsg.contLen = rspLen;
56,996✔
464
  rpcMsg.code = code;
56,996✔
465
  rpcMsg.msgType = pMsg->msgType;
56,996✔
466

467
  if (code) {
56,996✔
468
    qError("get table %s cfg failed cause of %s", cfgReq.tbName, tstrerror(code));
×
469
  }
470

471
  if (direct) {
56,996✔
472
    tmsgSendRsp(&rpcMsg);
×
473
  } else {
474
    *pMsg = rpcMsg;
56,996✔
475
  }
476

477
  tFreeSTableCfgRsp(&cfgRsp);
56,996✔
478
  metaReaderClear(&mer2);
56,996✔
479
  metaReaderClear(&mer1);
56,996✔
480
  return code;
56,996✔
481
}
482

483
static FORCE_INLINE void vnodeFreeSBatchRspMsg(void *p) {
484
  if (NULL == p) {
485
    return;
486
  }
487

488
  SBatchRspMsg *pRsp = (SBatchRspMsg *)p;
489
  rpcFreeCont(pRsp->msg);
490
}
491

492
int32_t vnodeGetBatchMeta(SVnode *pVnode, SRpcMsg *pMsg) {
26,582,532✔
493
  int32_t      code = 0;
26,582,532✔
494
  int32_t      rspSize = 0;
26,582,532✔
495
  SBatchReq    batchReq = {0};
26,582,532✔
496
  SBatchMsg   *req = NULL;
26,583,443✔
497
  SBatchRspMsg rsp = {0};
26,583,443✔
498
  SBatchRsp    batchRsp = {0};
26,569,001✔
499
  SRpcMsg      reqMsg = *pMsg;
26,568,090✔
500
  SRpcMsg      rspMsg = {0};
26,567,578✔
501
  void        *pRsp = NULL;
26,574,864✔
502

503
  if (tDeserializeSBatchReq(pMsg->pCont, pMsg->contLen, &batchReq)) {
26,574,864✔
504
    code = terrno;
×
505
    qError("tDeserializeSBatchReq failed");
×
506
    goto _exit;
×
507
  }
508

509
  int32_t msgNum = taosArrayGetSize(batchReq.pMsgs);
26,572,776✔
510
  if (msgNum >= MAX_META_MSG_IN_BATCH) {
26,578,093✔
511
    code = TSDB_CODE_INVALID_MSG;
×
512
    qError("too many msgs %d in vnode batch meta req", msgNum);
×
513
    goto _exit;
×
514
  }
515

516
  batchRsp.pRsps = taosArrayInit(msgNum, sizeof(SBatchRspMsg));
26,578,093✔
517
  if (NULL == batchRsp.pRsps) {
26,570,865✔
518
    code = terrno;
×
519
    qError("taosArrayInit %d SBatchRspMsg failed", msgNum);
×
520
    goto _exit;
×
521
  }
522

523
  for (int32_t i = 0; i < msgNum; ++i) {
58,187,563✔
524
    req = taosArrayGet(batchReq.pMsgs, i);
31,613,733✔
525
    if (req == NULL) {
31,615,545✔
526
      code = terrno;
×
527
      goto _exit;
×
528
    }
529

530
    reqMsg.msgType = req->msgType;
31,615,545✔
531
    reqMsg.pCont = req->msg;
31,609,759✔
532
    reqMsg.contLen = req->msgLen;
31,608,271✔
533

534
    switch (req->msgType) {
31,609,108✔
535
      case TDMT_VND_TABLE_META:
27,126,525✔
536
        // error code has been set into reqMsg, no need to handle it here.
537
        if (TSDB_CODE_SUCCESS != vnodeGetTableMeta(pVnode, &reqMsg, false)) {
27,126,525✔
538
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
2,713,673✔
539
        }
540
        break;
27,126,525✔
541
      case TDMT_VND_TABLE_NAME:
264,945✔
542
        // error code has been set into reqMsg, no need to handle it here.
543
        if (TSDB_CODE_SUCCESS != vnodeGetTableMeta(pVnode, &reqMsg, false)) {
264,945✔
544
          qWarn("vnodeGetBatchName failed, msgType:%d", req->msgType);
95,407✔
545
        }
546
        break;
264,945✔
547
      case TDMT_VND_TABLE_CFG:
56,996✔
548
        // error code has been set into reqMsg, no need to handle it here.
549
        if (TSDB_CODE_SUCCESS != vnodeGetTableCfg(pVnode, &reqMsg, false)) {
56,996✔
550
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
×
551
        }
552
        break;
56,996✔
553
      case TDMT_VND_VSUBTABLES_META:
×
554
        // error code has been set into reqMsg, no need to handle it here.
555
        if (TSDB_CODE_SUCCESS != vnodeGetVSubtablesMeta(pVnode, &reqMsg)) {
×
556
          qWarn("vnodeGetVSubtablesMeta failed, msgType:%d", req->msgType);
×
557
        }
558
        break;
×
559
      case TDMT_VND_VSTB_REF_DBS:
4,159,780✔
560
        // error code has been set into reqMsg, no need to handle it here.
561
        if (TSDB_CODE_SUCCESS != vnodeGetVStbRefDbs(pVnode, &reqMsg)) {
4,159,780✔
562
          qWarn("vnodeGetVStbRefDbs failed, msgType:%d", req->msgType);
×
563
        }
564
        break;
4,161,561✔
565
      default:
×
566
        qError("invalid req msgType %d", req->msgType);
×
567
        reqMsg.code = TSDB_CODE_INVALID_MSG;
×
568
        reqMsg.pCont = NULL;
×
569
        reqMsg.contLen = 0;
×
570
        break;
×
571
    }
572

573
    rsp.msgIdx = req->msgIdx;
31,610,027✔
574
    rsp.reqType = reqMsg.msgType;
31,613,750✔
575
    rsp.msgLen = reqMsg.contLen;
31,613,750✔
576
    rsp.rspCode = reqMsg.code;
31,613,750✔
577
    rsp.msg = reqMsg.pCont;
31,613,750✔
578

579
    if (NULL == taosArrayPush(batchRsp.pRsps, &rsp)) {
63,222,535✔
580
      qError("taosArrayPush failed");
×
581
      code = terrno;
×
582
      goto _exit;
×
583
    }
584
  }
585

586
  rspSize = tSerializeSBatchRsp(NULL, 0, &batchRsp);
26,573,830✔
587
  if (rspSize < 0) {
26,552,873✔
588
    qError("tSerializeSBatchRsp failed");
×
589
    code = terrno;
×
590
    goto _exit;
×
591
  }
592
  pRsp = rpcMallocCont(rspSize);
26,552,873✔
593
  if (pRsp == NULL) {
26,556,497✔
594
    qError("rpcMallocCont %d failed", rspSize);
×
595
    code = terrno;
×
596
    goto _exit;
×
597
  }
598
  if (tSerializeSBatchRsp(pRsp, rspSize, &batchRsp) < 0) {
26,556,497✔
599
    qError("tSerializeSBatchRsp %d failed", rspSize);
×
600
    code = terrno;
×
601
    goto _exit;
×
602
  }
603

604
_exit:
26,569,574✔
605

606
  rspMsg.info = pMsg->info;
26,574,867✔
607
  rspMsg.pCont = pRsp;
26,572,426✔
608
  rspMsg.contLen = rspSize;
26,572,426✔
609
  rspMsg.code = code;
26,572,426✔
610
  rspMsg.msgType = pMsg->msgType;
26,572,426✔
611

612
  if (code) {
26,565,117✔
613
    qError("vnd get batch meta failed cause of %s", tstrerror(code));
×
614
  }
615

616
  taosArrayDestroyEx(batchReq.pMsgs, tFreeSBatchReqMsg);
26,565,117✔
617
  taosArrayDestroyEx(batchRsp.pRsps, tFreeSBatchRspMsg);
26,562,618✔
618

619
  tmsgSendRsp(&rspMsg);
26,553,661✔
620

621
  return code;
26,580,808✔
622
}
623

624
#define VNODE_DO_META_QUERY(pVnode, cmd)                 \
625
  do {                                                   \
626
    (void)taosThreadRwlockRdlock(&(pVnode)->metaRWLock); \
627
    cmd;                                                 \
628
    (void)taosThreadRwlockUnlock(&(pVnode)->metaRWLock); \
629
  } while (0)
630

631
int32_t vnodeReadVSubtables(SReadHandle* pHandle, int64_t suid, SArray** ppRes) {
×
632
  int32_t                    code = TSDB_CODE_SUCCESS;
×
633
  int32_t                    line = 0;
×
634
  SMetaReader                mr = {0};
×
635
  bool                       readerInit = false;
×
636
  SVCTableRefCols*           pTb = NULL;
×
637
  int32_t                    refColsNum = 0;
×
638
  char                       tbFName[TSDB_TABLE_FNAME_LEN];
×
639
  SSHashObj*                 pSrcTbls = NULL;
×
640

641
  SArray *pList = taosArrayInit(10, sizeof(uint64_t));
×
642
  QUERY_CHECK_NULL(pList, code, line, _return, terrno);
×
643

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

646
  size_t num = taosArrayGetSize(pList);
×
647
  *ppRes = taosArrayInit(num, POINTER_BYTES);
×
648
  QUERY_CHECK_NULL(*ppRes, code, line, _return, terrno);
×
649
  pSrcTbls = tSimpleHashInit(10, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
×
650
  QUERY_CHECK_NULL(pSrcTbls, code, line, _return, terrno);
×
651

652
  for (int32_t i = 0; i < num; ++i) {
×
653
    uint64_t* id = taosArrayGet(pList, i);
×
654
    QUERY_CHECK_NULL(id, code, line, _return, terrno);
×
655
    pHandle->api.metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pHandle->api.metaFn);
×
656
    QUERY_CHECK_CODE(pHandle->api.metaReaderFn.getTableEntryByUid(&mr, *id), line, _return);
×
657
    readerInit = true;
×
658

659
    refColsNum = 0;
×
660
    for (int32_t j = 0; j < mr.me.colRef.nCols; j++) {
×
661
      if (mr.me.colRef.pColRef[j].hasRef) {
×
662
        refColsNum++;
×
663
      }
664
    }
665

666
    if (refColsNum <= 0) {
×
667
      pHandle->api.metaReaderFn.clearReader(&mr);
×
668
      readerInit = false;
×
669
      continue;
×
670
    }
671

672
    pTb = taosMemoryCalloc(1, refColsNum * sizeof(SRefColInfo) + sizeof(*pTb));
×
673
    QUERY_CHECK_NULL(pTb, code, line, _return, terrno);
×
674

675
    pTb->uid = mr.me.uid;
×
676
    pTb->numOfColRefs = refColsNum;
×
677
    pTb->refCols = (SRefColInfo*)(pTb + 1);
×
678

679
    refColsNum = 0;
×
680
    tSimpleHashClear(pSrcTbls);
×
681
    for (int32_t j = 0; j < mr.me.colRef.nCols; j++) {
×
682
      if (!mr.me.colRef.pColRef[j].hasRef) {
×
683
        continue;
×
684
      }
685

686
      pTb->refCols[refColsNum].colId = mr.me.colRef.pColRef[j].id;
×
687
      tstrncpy(pTb->refCols[refColsNum].refColName, mr.me.colRef.pColRef[j].refColName, TSDB_COL_NAME_LEN);
×
688
      tstrncpy(pTb->refCols[refColsNum].refTableName, mr.me.colRef.pColRef[j].refTableName, TSDB_TABLE_NAME_LEN);
×
689
      tstrncpy(pTb->refCols[refColsNum].refDbName, mr.me.colRef.pColRef[j].refDbName, TSDB_DB_NAME_LEN);
×
690

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

693
      if (NULL == tSimpleHashGet(pSrcTbls, tbFName, strlen(tbFName))) {
×
694
        QUERY_CHECK_CODE(tSimpleHashPut(pSrcTbls, tbFName, strlen(tbFName), &code, sizeof(code)), line, _return);
×
695
      }
696

697
      refColsNum++;
×
698
    }
699

700
    pTb->numOfSrcTbls = tSimpleHashGetSize(pSrcTbls);
×
701
    QUERY_CHECK_NULL(taosArrayPush(*ppRes, &pTb), code, line, _return, terrno);
×
702
    pTb = NULL;
×
703

704
    pHandle->api.metaReaderFn.clearReader(&mr);
×
705
    readerInit = false;
×
706
  }
707

708
_return:
×
709

710
  if (readerInit) {
×
711
    pHandle->api.metaReaderFn.clearReader(&mr);
×
712
  }
713

714
  taosArrayDestroy(pList);
×
715
  taosMemoryFree(pTb);
×
716
  tSimpleHashCleanup(pSrcTbls);
×
717

718
  if (code) {
×
719
    qError("%s failed since %s", __func__, tstrerror(code));
×
720
  }
721
  return code;
×
722
}
723

724
int32_t vnodeReadVStbRefDbs(SReadHandle* pHandle, int64_t suid, SArray** ppRes) {
4,168,696✔
725
  int32_t                    code = TSDB_CODE_SUCCESS;
4,168,696✔
726
  int32_t                    line = 0;
4,168,696✔
727
  SMetaReader                mr = {0};
4,168,696✔
728
  bool                       readerInit = false;
4,167,301✔
729
  SSHashObj*                 pDbNameHash = NULL;
4,167,301✔
730
  SArray*                    pList = NULL;
4,167,301✔
731

732
  pList = taosArrayInit(10, sizeof(uint64_t));
4,167,301✔
733
  QUERY_CHECK_NULL(pList, code, line, _return, terrno);
4,160,178✔
734

735
  *ppRes = taosArrayInit(10, POINTER_BYTES);
4,160,178✔
736
  QUERY_CHECK_NULL(*ppRes, code, line, _return, terrno)
4,165,181✔
737
  
738
  // lookup in cache
739
  code = pHandle->api.metaFn.metaGetCachedRefDbs(pHandle->vnode, suid, *ppRes);
4,157,521✔
740
  QUERY_CHECK_CODE(code, line, _return);
4,173,929✔
741

742
  if (taosArrayGetSize(*ppRes) > 0) {
4,173,929✔
743
    // found in cache
744
    goto _return;
4,140,946✔
745
  } else {
746
    code = pHandle->api.metaFn.getChildTableList(pHandle->vnode, suid, pList);
32,983✔
747
    QUERY_CHECK_CODE(code, line, _return);
32,983✔
748

749
    size_t num = taosArrayGetSize(pList);
32,983✔
750
    pDbNameHash = tSimpleHashInit(10, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
32,983✔
751
    QUERY_CHECK_NULL(pDbNameHash, code, line, _return, terrno);
32,983✔
752

753
    for (int32_t i = 0; i < num; ++i) {
107,820✔
754
      uint64_t* id = taosArrayGet(pList, i);
74,837✔
755
      QUERY_CHECK_NULL(id, code, line, _return, terrno);
74,837✔
756

757
      pHandle->api.metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pHandle->api.metaFn);
74,837✔
758
      readerInit = true;
74,837✔
759

760
      code = pHandle->api.metaReaderFn.getTableEntryByUid(&mr, *id);
74,837✔
761
      QUERY_CHECK_CODE(code, line, _return);
74,837✔
762

763
      for (int32_t j = 0; j < mr.me.colRef.nCols; j++) {
35,533,844✔
764
        if (mr.me.colRef.pColRef[j].hasRef) {
35,459,007✔
765
          if (NULL == tSimpleHashGet(pDbNameHash, mr.me.colRef.pColRef[j].refDbName, strlen(mr.me.colRef.pColRef[j].refDbName))) {
35,154,953✔
766
            char *refDbName = taosStrdup(mr.me.colRef.pColRef[j].refDbName);
27,731✔
767
            QUERY_CHECK_NULL(refDbName, code, line, _return, terrno);
27,731✔
768

769
            QUERY_CHECK_NULL(taosArrayPush(*ppRes, &refDbName), code, line, _return, terrno);
55,462✔
770

771
            code = tSimpleHashPut(pDbNameHash, refDbName, strlen(refDbName), NULL, 0);
27,731✔
772
            QUERY_CHECK_CODE(code, line, _return);
27,731✔
773
          }
774
        }
775
      }
776

777
      pHandle->api.metaReaderFn.clearReader(&mr);
74,837✔
778
      readerInit = false;
74,837✔
779
    }
780

781
    code = pHandle->api.metaFn.metaPutRefDbsToCache(pHandle->vnode, suid, *ppRes);
32,983✔
782
    QUERY_CHECK_CODE(code, line, _return);
32,983✔
783
  }
784

785
_return:
4,173,929✔
786

787
  if (readerInit) {
4,173,929✔
788
    pHandle->api.metaReaderFn.clearReader(&mr);
×
789
  }
790

791
  taosArrayDestroy(pList);
4,173,929✔
792
  tSimpleHashCleanup(pDbNameHash);
4,170,303✔
793

794
  if (code) {
4,168,643✔
795
    qError("%s failed since %s", __func__, tstrerror(code));
×
796
  }
797
  return code;
4,168,643✔
798
}
799

800
int32_t vnodeGetVSubtablesMeta(SVnode *pVnode, SRpcMsg *pMsg) {
×
801
  int32_t        code = 0;
×
802
  int32_t        rspSize = 0;
×
803
  SVSubTablesReq req = {0};
×
804
  SVSubTablesRsp rsp = {0};
×
805
  SRpcMsg      rspMsg = {0};
×
806
  void        *pRsp = NULL;
×
807
  int32_t      line = 0;
×
808

809
  if (tDeserializeSVSubTablesReq(pMsg->pCont, pMsg->contLen, &req)) {
×
810
    code = terrno;
×
811
    qError("tDeserializeSVSubTablesReq failed");
×
812
    goto _return;
×
813
  }
814

815
  SReadHandle handle = {0};
×
816
  handle.vnode = pVnode;
×
817
  initStorageAPI(&handle.api);
×
818

819
  QUERY_CHECK_CODE(vnodeReadVSubtables(&handle, req.suid, &rsp.pTables), line, _return);
×
820
  rsp.vgId = TD_VID(pVnode);
×
821

822
  rspSize = tSerializeSVSubTablesRsp(NULL, 0, &rsp);
×
823
  if (rspSize < 0) {
×
824
    code = rspSize;
×
825
    qError("tSerializeSVSubTablesRsp failed, error:%d", rspSize);
×
826
    goto _return;
×
827
  }
828
  pRsp = taosMemoryCalloc(1, rspSize);
×
829
  if (pRsp == NULL) {
×
830
    code = terrno;
×
831
    qError("rpcMallocCont %d failed, error:%d", rspSize, terrno);
×
832
    goto _return;
×
833
  }
834
  rspSize = tSerializeSVSubTablesRsp(pRsp, rspSize, &rsp);
×
835
  if (rspSize < 0) {
×
836
    code = rspSize;
×
837
    qError("tSerializeSVSubTablesRsp failed, error:%d", rspSize);
×
838
    goto _return;
×
839
  }
840

841
_return:
×
842

843
  rspMsg.info = pMsg->info;
×
844
  rspMsg.pCont = pRsp;
×
845
  rspMsg.contLen = rspSize;
×
846
  rspMsg.code = code;
×
847
  rspMsg.msgType = pMsg->msgType;
×
848

849
  if (code) {
×
850
    qError("vnd get virtual subtables failed cause of %s", tstrerror(code));
×
851
  }
852

853
  *pMsg = rspMsg;
×
854
  
855
  tDestroySVSubTablesRsp(&rsp);
×
856

857
  //tmsgSendRsp(&rspMsg);
858

859
  return code;
×
860
}
861

862
int32_t vnodeGetVStbRefDbs(SVnode *pVnode, SRpcMsg *pMsg) {
4,168,160✔
863
  int32_t        code = 0;
4,168,160✔
864
  int32_t        rspSize = 0;
4,168,160✔
865
  SVStbRefDbsReq req = {0};
4,168,160✔
866
  SVStbRefDbsRsp rsp = {0};
4,168,340✔
867
  SRpcMsg        rspMsg = {0};
4,168,719✔
868
  void          *pRsp = NULL;
4,168,719✔
869
  int32_t        line = 0;
4,168,719✔
870

871
  if (tDeserializeSVStbRefDbsReq(pMsg->pCont, pMsg->contLen, &req)) {
4,168,719✔
872
    code = terrno;
×
873
    qError("tDeserializeSVSubTablesReq failed");
×
874
    goto _return;
×
875
  }
876

877
  SReadHandle handle = {0};
4,158,446✔
878
  handle.vnode = pVnode;
4,160,602✔
879
  initStorageAPI(&handle.api);
4,160,602✔
880

881
  code = vnodeReadVStbRefDbs(&handle, req.suid, &rsp.pDbs);
4,158,480✔
882
  QUERY_CHECK_CODE(code, line, _return);
4,170,623✔
883
  rsp.vgId = TD_VID(pVnode);
4,170,623✔
884

885
  rspSize = tSerializeSVStbRefDbsRsp(NULL, 0, &rsp);
4,170,634✔
886
  if (rspSize < 0) {
4,150,368✔
887
    code = rspSize;
×
888
    qError("tSerializeSVStbRefDbsRsp failed, error:%d", rspSize);
×
889
    goto _return;
×
890
  }
891
  pRsp = taosMemoryCalloc(1, rspSize);
4,150,368✔
892
  if (pRsp == NULL) {
4,156,051✔
893
    code = terrno;
×
894
    qError("rpcMallocCont %d failed, error:%d", rspSize, terrno);
×
895
    goto _return;
×
896
  }
897
  rspSize = tSerializeSVStbRefDbsRsp(pRsp, rspSize, &rsp);
4,156,051✔
898
  if (rspSize < 0) {
4,162,916✔
899
    code = rspSize;
×
900
    qError("tSerializeSVStbRefDbsRsp failed, error:%d", rspSize);
×
901
    goto _return;
×
902
  }
903

904
_return:
4,162,916✔
905

906
  rspMsg.info = pMsg->info;
4,166,100✔
907
  rspMsg.pCont = pRsp;
4,164,463✔
908
  rspMsg.contLen = rspSize;
4,164,463✔
909
  rspMsg.code = code;
4,164,463✔
910
  rspMsg.msgType = pMsg->msgType;
4,164,463✔
911

912
  if (code) {
4,154,121✔
913
    qError("vnd get virtual stb ref db failed cause of %s", tstrerror(code));
×
914
  }
915

916
  *pMsg = rspMsg;
4,154,121✔
917

918
  tDestroySVStbRefDbsRsp(&rsp);
4,150,511✔
919

920
  return code;
4,155,489✔
921
}
922

923
static int32_t vnodeGetCompStorage(SVnode *pVnode, int64_t *output) {
122,594,807✔
924
  int32_t code = 0;
122,594,807✔
925
#ifdef TD_ENTERPRISE
926
  int32_t now = taosGetTimestampSec();
122,594,807✔
927
  if (llabs(now - pVnode->config.vndStats.storageLastUpd) >= 30) {
122,594,807✔
928
    pVnode->config.vndStats.storageLastUpd = now;
6,511,907✔
929

930
    SDbSizeStatisInfo info = {0};
6,511,907✔
931
    if (0 == (code = vnodeGetDBSize(pVnode, &info))) {
6,511,907✔
932
      int64_t compSize =
6,511,907✔
933
          info.l1Size + info.l2Size + info.l3Size + info.cacheSize + info.walSize + info.metaSize + +info.ssSize;
6,511,907✔
934
      if (compSize >= 0) {
6,511,907✔
935
        pVnode->config.vndStats.compStorage = compSize;
6,511,907✔
936
      } else {
937
        vError("vnode get comp storage failed since compSize is negative:%" PRIi64, compSize);
×
938
        code = TSDB_CODE_APP_ERROR;
×
939
      }
940
    } else {
941
      vWarn("vnode get comp storage failed since %s", tstrerror(code));
×
942
    }
943
  }
944
  if (output) *output = pVnode->config.vndStats.compStorage;
122,594,807✔
945
#endif
946
  return code;
122,594,807✔
947
}
948

949
static void vnodeGetBufferInfo(SVnode *pVnode, int64_t *bufferSegmentUsed, int64_t *bufferSegmentSize) {
122,594,807✔
950
  *bufferSegmentUsed = 0;
122,594,807✔
951
  *bufferSegmentSize = 0;
122,594,807✔
952
  if (pVnode) {
122,594,807✔
953
    (void)taosThreadMutexLock(&pVnode->mutex);
122,594,807✔
954

955
    if (pVnode->inUse) {
122,594,807✔
956
      *bufferSegmentUsed = pVnode->inUse->size;
122,588,625✔
957
    }
958
    *bufferSegmentSize = pVnode->config.szBuf / VNODE_BUFPOOL_SEGMENTS;
122,594,807✔
959

960
    (void)taosThreadMutexUnlock(&pVnode->mutex);
122,594,807✔
961
  }
962
}
122,594,807✔
963

964
int32_t vnodeGetLoad(SVnode *pVnode, SVnodeLoad *pLoad) {
122,594,807✔
965
  SSyncState state = syncGetState(pVnode->sync);
122,594,807✔
966
  pLoad->syncAppliedIndex = pVnode->state.applied;
122,594,807✔
967
  syncGetCommitIndex(pVnode->sync, &pLoad->syncCommitIndex);
122,594,807✔
968

969
  pLoad->vgId = TD_VID(pVnode);
122,594,807✔
970
  pLoad->syncState = state.state;
122,594,807✔
971
  pLoad->syncRestore = state.restored;
122,594,807✔
972
  pLoad->syncTerm = state.term;
122,594,807✔
973
  pLoad->roleTimeMs = state.roleTimeMs;
122,594,807✔
974
  pLoad->startTimeMs = state.startTimeMs;
122,594,807✔
975
  pLoad->syncCanRead = state.canRead;
122,594,807✔
976
  pLoad->cacheUsage = tsdbCacheGetUsage(pVnode);
122,594,807✔
977
  pLoad->numOfCachedTables = tsdbCacheGetElems(pVnode);
122,594,807✔
978
  VNODE_DO_META_QUERY(pVnode, pLoad->numOfTables = metaGetTbNum(pVnode->pMeta));
122,594,807✔
979
  VNODE_DO_META_QUERY(pVnode, pLoad->numOfTimeSeries = metaGetTimeSeriesNum(pVnode->pMeta, 1));
122,594,807✔
980
  pLoad->totalStorage = (int64_t)3 * 1073741824;  // TODO
122,594,807✔
981
  (void)vnodeGetCompStorage(pVnode, &pLoad->compStorage);
122,594,807✔
982
  pLoad->pointsWritten = 100;
122,594,807✔
983
  pLoad->numOfSelectReqs = 1;
122,594,807✔
984
  pLoad->numOfInsertReqs = atomic_load_64(&pVnode->statis.nInsert);
122,594,807✔
985
  pLoad->numOfInsertSuccessReqs = atomic_load_64(&pVnode->statis.nInsertSuccess);
122,594,807✔
986
  pLoad->numOfBatchInsertReqs = atomic_load_64(&pVnode->statis.nBatchInsert);
122,594,807✔
987
  pLoad->numOfBatchInsertSuccessReqs = atomic_load_64(&pVnode->statis.nBatchInsertSuccess);
122,594,807✔
988
  vnodeGetBufferInfo(pVnode, &pLoad->bufferSegmentUsed, &pLoad->bufferSegmentSize);
122,594,807✔
989
  vDebug("vgId:%d, get vnode load, state:%s snapSeq:%d, learnerProgress:%d, totalIndex:%" PRId64, TD_VID(pVnode),
122,594,807✔
990
         syncStr(state.state), state.snapSeq, state.progress, state.totalIndex);
991
  pLoad->learnerProgress = state.progress;
122,594,807✔
992
  pLoad->snapSeq = state.snapSeq;
122,594,807✔
993
  pLoad->syncTotalIndex = state.totalIndex;
122,594,807✔
994
  return 0;
122,594,807✔
995
}
996

997
int32_t vnodeGetLoadLite(SVnode *pVnode, SVnodeLoadLite *pLoad) {
×
998
  SSyncState syncState = syncGetState(pVnode->sync);
×
999
  if (syncState.state == TAOS_SYNC_STATE_LEADER || syncState.state == TAOS_SYNC_STATE_ASSIGNED_LEADER) {
×
1000
    pLoad->vgId = TD_VID(pVnode);
×
1001
    pLoad->nTimeSeries = metaGetTimeSeriesNum(pVnode->pMeta, 1);
×
1002
    return 0;
×
1003
  }
1004
  return -1;
×
1005
}
1006
/**
1007
 * @brief Reset the statistics value by monitor interval
1008
 *
1009
 * @param pVnode
1010
 * @param pLoad
1011
 */
1012
void vnodeResetLoad(SVnode *pVnode, SVnodeLoad *pLoad) {
162✔
1013
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nInsert, pLoad->numOfInsertReqs, 64, "nInsert");
162✔
1014
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nInsertSuccess, pLoad->numOfInsertSuccessReqs, 64, "nInsertSuccess");
162✔
1015
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nBatchInsert, pLoad->numOfBatchInsertReqs, 64, "nBatchInsert");
162✔
1016
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nBatchInsertSuccess, pLoad->numOfBatchInsertSuccessReqs, 64,
162✔
1017
                            "nBatchInsertSuccess");
1018
}
162✔
1019

1020
void vnodeGetInfo(void *pVnode, const char **dbname, int32_t *vgId, int64_t *numOfTables, int64_t *numOfNormalTables) {
13,454,464✔
1021
  SVnode    *pVnodeObj = pVnode;
13,454,464✔
1022
  SVnodeCfg *pConf = &pVnodeObj->config;
13,454,464✔
1023

1024
  if (dbname) {
13,454,916✔
1025
    *dbname = pConf->dbname;
13,435,025✔
1026
  }
1027

1028
  if (vgId) {
13,455,683✔
1029
    *vgId = TD_VID(pVnodeObj);
12,702,162✔
1030
  }
1031

1032
  if (numOfTables) {
13,455,836✔
1033
    *numOfTables = pConf->vndStats.numOfNTables + pConf->vndStats.numOfCTables +
21,622✔
1034
                   pConf->vndStats.numOfVTables + pConf->vndStats.numOfVCTables;
10,644✔
1035
  }
1036

1037
  if (numOfNormalTables) {
13,456,170✔
1038
    *numOfNormalTables = pConf->vndStats.numOfNTables +
16,590✔
1039
                         pConf->vndStats.numOfVTables;
8,295✔
1040
  }
1041
}
13,456,170✔
1042

1043
int32_t vnodeGetTableList(void *pVnode, int8_t type, SArray *pList) {
8,295✔
1044
  if (type == TSDB_SUPER_TABLE) {
8,295✔
1045
    return vnodeGetStbIdList(pVnode, 0, pList);
8,295✔
1046
  } else {
1047
    return TSDB_CODE_INVALID_PARA;
×
1048
  }
1049
}
1050

1051
int32_t vnodeGetAllTableList(SVnode *pVnode, uint64_t uid, SArray *list) {
×
1052
  int32_t      code = TSDB_CODE_SUCCESS;
×
1053
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, uid, 1);
×
1054
  if (NULL == pCur) {
×
1055
    qError("vnode get all table list failed");
×
1056
    return terrno;
×
1057
  }
1058

1059
  while (1) {
×
1060
    tb_uid_t id = metaCtbCursorNext(pCur);
×
1061
    if (id == 0) {
×
1062
      break;
×
1063
    }
1064

1065
    STableKeyInfo info = {uid = id};
×
1066
    if (NULL == taosArrayPush(list, &info)) {
×
1067
      qError("taosArrayPush failed");
×
1068
      code = terrno;
×
1069
      goto _exit;
×
1070
    }
1071
  }
1072
_exit:
×
1073
  metaCloseCtbCursor(pCur);
×
1074
  return code;
×
1075
}
1076

1077
int32_t vnodeGetCtbIdListByFilter(SVnode *pVnode, int64_t suid, SArray *list, bool (*filter)(void *arg), void *arg) {
×
1078
  return 0;
×
1079
}
1080

1081
int32_t vnodeGetCtbIdList(void *pVnode, int64_t suid, SArray *list) {
96,395,257✔
1082
  int32_t      code = TSDB_CODE_SUCCESS;
96,395,257✔
1083
  SVnode      *pVnodeObj = pVnode;
96,395,257✔
1084
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnodeObj, suid, 1);
96,395,257✔
1085
  if (NULL == pCur) {
96,370,061✔
1086
    qError("vnode get all table list failed");
×
1087
    return terrno;
×
1088
  }
1089

1090
  while (1) {
441,129,410✔
1091
    tb_uid_t id = metaCtbCursorNext(pCur);
537,499,471✔
1092
    if (id == 0) {
537,461,965✔
1093
      break;
96,419,151✔
1094
    }
1095
    qTrace("vnodeGetCtbIdList: got ctb id %" PRId64 " for suid %" PRId64, id, suid);
441,042,814✔
1096
    if (NULL == taosArrayPush(list, &id)) {
441,162,672✔
1097
      qError("taosArrayPush failed");
×
1098
      code = terrno;
×
1099
      goto _exit;
×
1100
    }
1101
  }
1102

1103
_exit:
96,354,937✔
1104
  metaCloseCtbCursor(pCur);
96,354,937✔
1105
  return code;
96,346,815✔
1106
}
1107

1108
int32_t vnodeGetStbIdList(SVnode *pVnode, int64_t suid, SArray *list) {
61,478,059✔
1109
  int32_t      code = TSDB_CODE_SUCCESS;
61,478,059✔
1110
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, suid);
61,478,059✔
1111
  if (!pCur) {
61,479,325✔
1112
    return TSDB_CODE_OUT_OF_MEMORY;
×
1113
  }
1114

1115
  while (1) {
51,000,659✔
1116
    tb_uid_t id = metaStbCursorNext(pCur);
112,479,984✔
1117
    if (id == 0) {
112,473,654✔
1118
      break;
61,479,325✔
1119
    }
1120

1121
    if (NULL == taosArrayPush(list, &id)) {
51,004,806✔
1122
      qError("taosArrayPush failed");
×
1123
      code = terrno;
×
1124
      goto _exit;
×
1125
    }
1126
  }
1127

1128
_exit:
61,474,261✔
1129
  metaCloseStbCursor(pCur);
61,474,261✔
1130
  return code;
61,477,710✔
1131
}
1132

1133
int32_t vnodeGetStbIdListByFilter(SVnode *pVnode, int64_t suid, SArray *list, bool (*filter)(void *arg, void *arg1),
306✔
1134
                                  void *arg) {
1135
  int32_t      code = TSDB_CODE_SUCCESS;
306✔
1136
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, suid);
306✔
1137
  if (!pCur) {
306✔
1138
    return terrno;
×
1139
  }
1140

1141
  while (1) {
1,530✔
1142
    tb_uid_t id = metaStbCursorNext(pCur);
1,836✔
1143
    if (id == 0) {
1,836✔
1144
      break;
306✔
1145
    }
1146

1147
    if ((*filter) && (*filter)(arg, &id)) {
1,530✔
1148
      continue;
1,530✔
1149
    }
1150

1151
    if (NULL == taosArrayPush(list, &id)) {
×
1152
      qError("taosArrayPush failed");
×
1153
      code = terrno;
×
1154
      goto _exit;
×
1155
    }
1156
  }
1157

1158
_exit:
306✔
1159
  metaCloseStbCursor(pCur);
306✔
1160
  return code;
306✔
1161
}
1162

1163
int32_t vnodeGetCtbNum(SVnode *pVnode, int64_t suid, int64_t *num) {
4,655,646✔
1164
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, suid, 0);
4,655,646✔
1165
  if (!pCur) {
4,657,262✔
1166
    return terrno;
×
1167
  }
1168

1169
  *num = 0;
4,657,262✔
1170
  while (1) {
2,588,325✔
1171
    tb_uid_t id = metaCtbCursorNext(pCur);
7,246,173✔
1172
    if (id == 0) {
7,245,362✔
1173
      break;
4,656,685✔
1174
    }
1175

1176
    ++(*num);
2,588,677✔
1177
  }
1178

1179
  metaCloseCtbCursor(pCur);
4,656,685✔
1180
  return TSDB_CODE_SUCCESS;
4,656,166✔
1181
}
1182

1183
int32_t vnodeGetStbColumnNum(SVnode *pVnode, tb_uid_t suid, int *num) {
4,657,031✔
1184
  SSchemaWrapper *pSW = metaGetTableSchema(pVnode->pMeta, suid, -1, 0, NULL, 0);
4,657,031✔
1185
  if (pSW) {
4,657,902✔
1186
    *num = pSW->nCols;
4,657,628✔
1187
    tDeleteSchemaWrapper(pSW);
1188
  } else {
1189
    *num = 2;
274✔
1190
  }
1191

1192
  return TSDB_CODE_SUCCESS;
4,656,676✔
1193
}
1194

1195
int32_t vnodeGetStbInfo(SVnode *pVnode, tb_uid_t suid, int64_t *keep, int8_t *flags) {
4,658,163✔
1196
  SMetaReader mr = {0};
4,658,163✔
1197
  metaReaderDoInit(&mr, pVnode->pMeta, META_READER_NOLOCK);
4,658,163✔
1198

1199
  int32_t code = metaReaderGetTableEntryByUid(&mr, suid);
4,655,867✔
1200
  if (code == TSDB_CODE_SUCCESS) {
4,658,217✔
1201
    if (keep) *keep = mr.me.stbEntry.keep;
4,657,943✔
1202
    if (flags) *flags = mr.me.flags;
4,657,943✔
1203
  } else {
1204
    if (keep) *keep = 0;
274✔
1205
    if (flags) *flags = 0;
274✔
1206
  }
1207

1208
  metaReaderClear(&mr);
4,658,217✔
1209
  return TSDB_CODE_SUCCESS;
4,658,217✔
1210
}
1211

1212
#ifdef TD_ENTERPRISE
1213
const char *tkLogStb[] = {"cluster_info",
1214
                          "data_dir",
1215
                          "dnodes_info",
1216
                          "d_info",
1217
                          "grants_info",
1218
                          "keeper_monitor",
1219
                          "logs",
1220
                          "log_dir",
1221
                          "log_summary",
1222
                          "m_info",
1223
                          "taosadapter_restful_http_request_fail",
1224
                          "taosadapter_restful_http_request_in_flight",
1225
                          "taosadapter_restful_http_request_summary_milliseconds",
1226
                          "taosadapter_restful_http_request_total",
1227
                          "taosadapter_system_cpu_percent",
1228
                          "taosadapter_system_mem_percent",
1229
                          "temp_dir",
1230
                          "vgroups_info",
1231
                          "vnodes_role"};
1232
const char *tkAuditStb[] = {"operations"};
1233
const int   tkLogStbNum = ARRAY_SIZE(tkLogStb);
1234
const int   tkAuditStbNum = ARRAY_SIZE(tkAuditStb);
1235

1236
// exclude stbs of taoskeeper log
1237
static int32_t vnodeGetTimeSeriesBlackList(SVnode *pVnode, int32_t *tbSize) {
61,471,667✔
1238
  int32_t      code = TSDB_CODE_SUCCESS;
61,471,667✔
1239
  int32_t      tbNum = 0;
61,471,667✔
1240
  const char **pTbArr = NULL;
61,471,667✔
1241
  const char  *dbName = NULL;
61,471,667✔
1242
  *tbSize = 0;
61,471,667✔
1243

1244
  if (!(dbName = strchr(pVnode->config.dbname, '.'))) return 0;
61,472,300✔
1245
  if (0 == strncmp(++dbName, "log", TSDB_DB_NAME_LEN)) {
61,472,300✔
1246
    tbNum = tkLogStbNum;
2,754✔
1247
    pTbArr = (const char **)&tkLogStb;
2,754✔
1248
  } else if (0 == strncmp(dbName, "audit", TSDB_DB_NAME_LEN)) {
61,468,913✔
1249
    tbNum = tkAuditStbNum;
2,528✔
1250
    pTbArr = (const char **)&tkAuditStb;
2,528✔
1251
  }
1252
  if (tbNum && pTbArr) {
61,471,667✔
1253
    *tbSize = metaSizeOfTbFilterCache(pVnode->pMeta, 0);
5,282✔
1254
    if (*tbSize < tbNum) {
5,282✔
1255
      for (int32_t i = 0; i < tbNum; ++i) {
46,366✔
1256
        tb_uid_t suid = metaGetTableEntryUidByName(pVnode->pMeta, pTbArr[i]);
42,614✔
1257
        if (suid != 0) {
42,614✔
1258
          code = metaPutTbToFilterCache(pVnode->pMeta, &suid, 0);
3,060✔
1259
          if (TSDB_CODE_SUCCESS != code) {
3,060✔
1260
            return code;
1,530✔
1261
          }
1262
        }
1263
      }
1264
      *tbSize = metaSizeOfTbFilterCache(pVnode->pMeta, 0);
3,752✔
1265
    }
1266
  }
1267

1268
  return code;
61,466,972✔
1269
}
1270
#endif
1271

1272
static bool vnodeTimeSeriesFilter(void *arg1, void *arg2) {
1,530✔
1273
  SVnode *pVnode = (SVnode *)arg1;
1,530✔
1274

1275
  if (metaTbInFilterCache(pVnode->pMeta, arg2, 0)) {
1,530✔
1276
    return true;
1,530✔
1277
  }
1278
  return false;
×
1279
}
1280

1281
int32_t vnodeGetTimeSeriesNum(SVnode *pVnode, int64_t *num) {
61,471,034✔
1282
  SArray *suidList = NULL;
61,471,034✔
1283

1284
  if (!(suidList = taosArrayInit(1, sizeof(tb_uid_t)))) {
61,471,034✔
1285
    return terrno;
×
1286
  }
1287

1288
  int32_t tbFilterSize = 0;
61,467,236✔
1289
  int32_t code = TSDB_CODE_SUCCESS;
61,458,374✔
1290
#ifdef TD_ENTERPRISE
1291
  code = vnodeGetTimeSeriesBlackList(pVnode, &tbFilterSize);
61,458,374✔
1292
  if (TSDB_CODE_SUCCESS != code) {
61,469,135✔
1293
    goto _exit;
1,530✔
1294
  }
1295
#endif
1296

1297
  if ((!tbFilterSize && vnodeGetStbIdList(pVnode, 0, suidList) < 0) ||
61,467,605✔
1298
      (tbFilterSize && vnodeGetStbIdListByFilter(pVnode, 0, suidList, vnodeTimeSeriesFilter, pVnode) < 0)) {
61,461,908✔
1299
    qError("vgId:%d, failed to get stb id list error: %s", TD_VID(pVnode), terrstr());
×
1300
    taosArrayDestroy(suidList);
×
1301
    return terrno;
×
1302
  }
1303

1304
  *num = 0;
61,468,238✔
1305
  int64_t arrSize = taosArrayGetSize(suidList);
61,466,339✔
1306
  for (int64_t i = 0; i < arrSize; ++i) {
112,455,242✔
1307
    tb_uid_t suid = *(tb_uid_t *)taosArrayGet(suidList, i);
50,985,105✔
1308

1309
    int64_t ctbNum = 0;
50,987,004✔
1310
    int32_t numOfCols = 0;
50,988,903✔
1311
    int8_t  flags = 0;
50,985,738✔
1312
    code = metaGetStbStats(pVnode, suid, &ctbNum, &numOfCols, &flags);
50,981,940✔
1313
    if (TSDB_CODE_SUCCESS != code) {
50,998,398✔
1314
      goto _exit;
×
1315
    }
1316
    if (!TABLE_IS_VIRTUAL(flags)) {
50,998,398✔
1317
      *num += ctbNum * (numOfCols - 1);
47,876,389✔
1318
    }
1319
  }
1320

1321
_exit:
61,468,153✔
1322
  taosArrayDestroy(suidList);
61,472,300✔
1323
  return TSDB_CODE_SUCCESS;
61,468,153✔
1324
}
1325

1326
int32_t vnodeGetAllCtbNum(SVnode *pVnode, int64_t *num) {
×
1327
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, 0);
×
1328
  if (!pCur) {
×
1329
    return terrno;
×
1330
  }
1331

1332
  *num = 0;
×
1333
  while (1) {
×
1334
    tb_uid_t id = metaStbCursorNext(pCur);
×
1335
    if (id == 0) {
×
1336
      break;
×
1337
    }
1338

1339
    int64_t ctbNum = 0;
×
1340
    int32_t code = vnodeGetCtbNum(pVnode, id, &ctbNum);
×
1341
    if (TSDB_CODE_SUCCESS != code) {
×
1342
      metaCloseStbCursor(pCur);
×
1343
      return code;
×
1344
    }
1345

1346
    *num += ctbNum;
×
1347
  }
1348

1349
  metaCloseStbCursor(pCur);
×
1350
  return TSDB_CODE_SUCCESS;
×
1351
}
1352

1353
void *vnodeGetIdx(void *pVnode) {
4,030,730✔
1354
  if (pVnode == NULL) {
4,030,730✔
1355
    return NULL;
×
1356
  }
1357

1358
  return metaGetIdx(((SVnode *)pVnode)->pMeta);
4,030,730✔
1359
}
1360

1361
void *vnodeGetIvtIdx(void *pVnode) {
4,030,730✔
1362
  if (pVnode == NULL) {
4,030,730✔
1363
    return NULL;
×
1364
  }
1365
  return metaGetIvtIdx(((SVnode *)pVnode)->pMeta);
4,030,730✔
1366
}
1367

1368
int32_t vnodeGetTableSchema(void *pVnode, int64_t uid, STSchema **pSchema, int64_t *suid, SSchemaWrapper **pTagSchema) {
241,880✔
1369
  return tsdbGetTableSchema(((SVnode *)pVnode)->pMeta, uid, pSchema, suid, pTagSchema);
241,880✔
1370
}
1371

1372
static FORCE_INLINE int32_t vnodeGetDBPrimaryInfo(SVnode *pVnode, SDbSizeStatisInfo *pInfo) {
1373
  int32_t code = 0;
6,517,985✔
1374
  char    path[TSDB_FILENAME_LEN] = {0};
6,517,985✔
1375

1376
  char   *dirName[] = {VNODE_TSDB_DIR, VNODE_WAL_DIR, VNODE_META_DIR, VNODE_TSDB_CACHE_DIR};
6,517,985✔
1377
  int64_t dirSize[4];
6,517,362✔
1378

1379
  vnodeGetPrimaryPath(pVnode, false, path, TSDB_FILENAME_LEN);
6,517,985✔
1380
  int32_t offset = strlen(path);
6,517,985✔
1381

1382
  for (int i = 0; i < sizeof(dirName) / sizeof(dirName[0]); i++) {
32,589,925✔
1383
    int64_t size = {0};
26,071,940✔
1384
    (void)snprintf(path + offset, TSDB_FILENAME_LEN - offset, "%s%s", TD_DIRSEP, dirName[i]);
26,071,940✔
1385
    code = taosGetDirSize(path, &size);
26,071,940✔
1386
    if (code != 0) {
26,071,940✔
1387
      uWarn("vnode %d get dir %s %s size failed since %s", TD_VID(pVnode), path, dirName[i], tstrerror(code));
1,482✔
1388
    }
1389
    path[offset] = 0;
26,071,940✔
1390
    dirSize[i] = size;
26,071,940✔
1391
  }
1392

1393
  pInfo->l1Size = 0;
6,517,985✔
1394
  pInfo->walSize = dirSize[1];
6,517,985✔
1395
  pInfo->metaSize = dirSize[2];
6,517,985✔
1396
  pInfo->cacheSize = dirSize[3];
6,517,985✔
1397
  return code;
6,517,985✔
1398
}
1399
int32_t vnodeGetDBSize(void *pVnode, SDbSizeStatisInfo *pInfo) {
6,517,985✔
1400
  int32_t code = 0;
6,517,985✔
1401
  int32_t lino = 0;
6,517,985✔
1402
  SVnode *pVnodeObj = pVnode;
6,517,985✔
1403
  if (pVnodeObj == NULL) {
6,517,985✔
1404
    return TSDB_CODE_VND_NOT_EXIST;
×
1405
  }
1406
  code = vnodeGetDBPrimaryInfo(pVnode, pInfo);
6,517,985✔
1407
  if (code != 0) goto _exit;
6,517,985✔
1408

1409
  code = tsdbGetFsSize(pVnodeObj->pTsdb, pInfo);
6,517,985✔
1410
_exit:
6,517,985✔
1411
  return code;
6,517,985✔
1412
}
1413

1414
/*
1415
 * Get raw write metrics for a vnode
1416
 */
1417
int32_t vnodeGetRawWriteMetrics(void *pVnode, SRawWriteMetrics *pRawMetrics) {
×
1418
  if (pVnode == NULL || pRawMetrics == NULL) {
×
1419
    return TSDB_CODE_INVALID_PARA;
×
1420
  }
1421

1422
  SVnode      *pVnode1 = (SVnode *)pVnode;
×
1423
  SSyncMetrics syncMetrics = syncGetMetrics(pVnode1->sync);
×
1424

1425
  // Copy values following SRawWriteMetrics structure order
1426
  pRawMetrics->total_requests = atomic_load_64(&pVnode1->writeMetrics.total_requests);
×
1427
  pRawMetrics->total_rows = atomic_load_64(&pVnode1->writeMetrics.total_rows);
×
1428
  pRawMetrics->total_bytes = atomic_load_64(&pVnode1->writeMetrics.total_bytes);
×
1429
  pRawMetrics->fetch_batch_meta_time = atomic_load_64(&pVnode1->writeMetrics.fetch_batch_meta_time);
×
1430
  pRawMetrics->fetch_batch_meta_count = atomic_load_64(&pVnode1->writeMetrics.fetch_batch_meta_count);
×
1431
  pRawMetrics->preprocess_time = atomic_load_64(&pVnode1->writeMetrics.preprocess_time);
×
1432
  pRawMetrics->wal_write_bytes = atomic_load_64(&syncMetrics.wal_write_bytes);
×
1433
  pRawMetrics->wal_write_time = atomic_load_64(&syncMetrics.wal_write_time);
×
1434
  pRawMetrics->apply_bytes = atomic_load_64(&pVnode1->writeMetrics.apply_bytes);
×
1435
  pRawMetrics->apply_time = atomic_load_64(&pVnode1->writeMetrics.apply_time);
×
1436
  pRawMetrics->commit_count = atomic_load_64(&pVnode1->writeMetrics.commit_count);
×
1437
  pRawMetrics->commit_time = atomic_load_64(&pVnode1->writeMetrics.commit_time);
×
1438
  pRawMetrics->memtable_wait_time = atomic_load_64(&pVnode1->writeMetrics.memtable_wait_time);
×
1439
  pRawMetrics->blocked_commit_count = atomic_load_64(&pVnode1->writeMetrics.blocked_commit_count);
×
1440
  pRawMetrics->blocked_commit_time = atomic_load_64(&pVnode1->writeMetrics.block_commit_time);
×
1441
  pRawMetrics->merge_count = atomic_load_64(&pVnode1->writeMetrics.merge_count);
×
1442
  pRawMetrics->merge_time = atomic_load_64(&pVnode1->writeMetrics.merge_time);
×
1443
  pRawMetrics->last_cache_commit_time = atomic_load_64(&pVnode1->writeMetrics.last_cache_commit_time);
×
1444
  pRawMetrics->last_cache_commit_count = atomic_load_64(&pVnode1->writeMetrics.last_cache_commit_count);
×
1445

1446
  return 0;
×
1447
}
1448

1449
/*
1450
 * Reset raw write metrics for a vnode by subtracting old values
1451
 */
1452
int32_t vnodeResetRawWriteMetrics(void *pVnode, const SRawWriteMetrics *pOldMetrics) {
×
1453
  if (pVnode == NULL || pOldMetrics == NULL) {
×
1454
    return TSDB_CODE_INVALID_PARA;
×
1455
  }
1456

1457
  SVnode *pVnode1 = (SVnode *)pVnode;
×
1458

1459
  // Reset vnode write metrics using atomic operations to subtract old values
1460
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.total_requests, pOldMetrics->total_requests);
×
1461
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.total_rows, pOldMetrics->total_rows);
×
1462
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.total_bytes, pOldMetrics->total_bytes);
×
1463

1464
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.fetch_batch_meta_time, pOldMetrics->fetch_batch_meta_time);
×
1465
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.fetch_batch_meta_count, pOldMetrics->fetch_batch_meta_count);
×
1466
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.preprocess_time, pOldMetrics->preprocess_time);
×
1467
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.apply_bytes, pOldMetrics->apply_bytes);
×
1468
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.apply_time, pOldMetrics->apply_time);
×
1469
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.commit_count, pOldMetrics->commit_count);
×
1470

1471
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.commit_time, pOldMetrics->commit_time);
×
1472
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.merge_time, pOldMetrics->merge_time);
×
1473

1474
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.memtable_wait_time, pOldMetrics->memtable_wait_time);
×
1475
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.blocked_commit_count, pOldMetrics->blocked_commit_count);
×
1476
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.block_commit_time, pOldMetrics->blocked_commit_time);
×
1477
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.merge_count, pOldMetrics->merge_count);
×
1478

1479
  // Reset new cache metrics
1480
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.last_cache_commit_time, pOldMetrics->last_cache_commit_time);
×
1481
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.last_cache_commit_count, pOldMetrics->last_cache_commit_count);
×
1482

1483
  // Reset sync metrics
1484
  SSyncMetrics syncMetrics = {
×
1485
      .wal_write_bytes = pOldMetrics->wal_write_bytes,
×
1486
      .wal_write_time = pOldMetrics->wal_write_time,
×
1487
  };
1488
  syncResetMetrics(pVnode1->sync, &syncMetrics);
×
1489

1490
  return 0;
×
1491
}
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