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

taosdata / TDengine / #4958

09 Feb 2026 01:16AM UTC coverage: 66.842% (+0.005%) from 66.837%
#4958

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

205670 of 307696 relevant lines covered (66.84%)

127690353.27 hits per line

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

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

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

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

36
int32_t fillTableColCmpr(SMetaReader *reader, SSchemaExt *pExt, int32_t numOfCol) {
19,980,821✔
37
  int8_t tblType = reader->me.type;
19,980,821✔
38
  if (withExtSchema(tblType)) {
19,981,019✔
39
    SColCmprWrapper *p = &(reader->me.colCmpr);
19,685,593✔
40
    if (numOfCol != p->nCols) {
19,685,593✔
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++) {
234,672,105✔
45
      SColCmpr *pCmpr = &p->pColCmpr[i];
214,985,879✔
46
      pExt[i].colId = pCmpr->id;
214,986,512✔
47
      pExt[i].compress = pCmpr->alg;
214,987,778✔
48
    }
49
  }
50
  return 0;
19,981,019✔
51
}
52

53
void vnodePrintTableMeta(STableMetaRsp *pMeta) {
19,980,386✔
54
  if (!(qDebugFlag & DEBUG_DEBUG)) {
19,980,386✔
55
    return;
304,697✔
56
  }
57

58
  qDebug("tbName:%s", pMeta->tbName);
19,675,689✔
59
  qDebug("stbName:%s", pMeta->stbName);
19,676,322✔
60
  qDebug("dbFName:%s", pMeta->dbFName);
19,676,322✔
61
  qDebug("dbId:%" PRId64, pMeta->dbId);
19,676,322✔
62
  qDebug("numOfTags:%d", pMeta->numOfTags);
19,676,322✔
63
  qDebug("numOfColumns:%d", pMeta->numOfColumns);
19,676,322✔
64
  qDebug("precision:%d", pMeta->precision);
19,676,322✔
65
  qDebug("tableType:%d", pMeta->tableType);
19,676,322✔
66
  qDebug("sversion:%d", pMeta->sversion);
19,676,322✔
67
  qDebug("tversion:%d", pMeta->tversion);
19,676,322✔
68
  qDebug("suid:%" PRIu64, pMeta->suid);
19,676,322✔
69
  qDebug("tuid:%" PRIu64, pMeta->tuid);
19,676,322✔
70
  qDebug("vgId:%d", pMeta->vgId);
19,676,322✔
71
  qDebug("sysInfo:%d", pMeta->sysInfo);
19,676,322✔
72
  if (pMeta->pSchemas) {
19,676,322✔
73
    for (int32_t i = 0; i < (pMeta->numOfColumns + pMeta->numOfTags); ++i) {
271,515,482✔
74
      SSchema *pSchema = pMeta->pSchemas + i;
251,839,160✔
75
      qDebug("%d col/tag: type:%d, flags:%d, colId:%d, bytes:%d, name:%s", i, pSchema->type, pSchema->flags,
251,839,160✔
76
             pSchema->colId, pSchema->bytes, pSchema->name);
77
    }
78
  }
79
}
80

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

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

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

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

130
  if (!reqTbUid) {
28,949,072✔
131
    (void)tsnprintf(tableFName, TSDB_TABLE_FNAME_LEN, "%s.%s", infoReq.dbFName, infoReq.tbName);
28,685,457✔
132
    if (pVnode->mounted) tTrimMountPrefix(tableFName);
28,686,839✔
133
    code = vnodeValidateTableHash(pVnode, tableFName);
28,686,206✔
134
    if (code) {
28,685,200✔
135
      goto _exit4;
×
136
    }
137
  }
138

139
  // query meta
140
  metaReaderDoInit(&mer1, pVnode->pMeta, META_READER_LOCK);
28,948,815✔
141
  if (reqTbUid) {
28,949,643✔
142
    SET_ERRNO(0);
264,186✔
143
    uint64_t tbUid = taosStr2UInt64(infoReq.tbName, NULL, 10);
263,988✔
144
    if (ERRNO == ERANGE || tbUid == 0) {
264,186✔
145
      code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
146
      goto _exit3;
×
147
    }
148
    SMetaReader mr3 = {0};
264,186✔
149
    metaReaderDoInit(&mr3, ((SVnode *)pVnode)->pMeta, META_READER_NOLOCK);
264,186✔
150
    if ((code = metaReaderGetTableEntryByUid(&mr3, tbUid)) < 0) {
264,186✔
151
      metaReaderClear(&mr3);
96,963✔
152
      TAOS_CHECK_GOTO(code, NULL, _exit3);
96,963✔
153
    }
154
    tstrncpy(metaRsp.tbName, mr3.me.name, TSDB_TABLE_NAME_LEN);
167,223✔
155
    metaReaderClear(&mr3);
167,223✔
156
    TAOS_CHECK_GOTO(metaGetTableEntryByName(&mer1, metaRsp.tbName), NULL, _exit3);
167,025✔
157
  } else if (metaGetTableEntryByName(&mer1, infoReq.tbName) < 0) {
28,685,457✔
158
    code = terrno;
8,872,297✔
159
    goto _exit3;
8,872,297✔
160
  }
161

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

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

182
      (void)strcpy(metaRsp.stbName, mer2.me.name);
12,334,370✔
183
      metaRsp.suid = mer2.me.uid;
12,334,370✔
184
      metaRsp.ownerId = mer2.me.stbEntry.ownerId;  // child table inherits ownerId from stb
12,334,370✔
185
      schema = mer2.me.stbEntry.schemaRow;
12,334,370✔
186
      schemaTag = mer2.me.stbEntry.schemaTag;
12,334,370✔
187
      break;
12,334,370✔
188
    }
189
    case TSDB_NORMAL_TABLE:
4,019,209✔
190
    case TSDB_VIRTUAL_NORMAL_TABLE: {
191
      schema = mer1.me.ntbEntry.schemaRow;
4,019,209✔
192
      metaRsp.ownerId = mer1.me.ntbEntry.ownerId;
4,019,209✔
193
      break;
4,019,209✔
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,980,386✔
202
  metaRsp.numOfColumns = schema.nCols;
19,980,386✔
203
  metaRsp.precision = pVnode->config.tsdbCfg.precision;
19,980,386✔
204
  metaRsp.sversion = schema.version;
19,981,019✔
205
  metaRsp.tversion = schemaTag.version;
19,981,019✔
206
  metaRsp.pSchemas = (SSchema *)taosMemoryMalloc(sizeof(SSchema) * (metaRsp.numOfColumns + metaRsp.numOfTags));
19,981,019✔
207
  metaRsp.pSchemaExt = (SSchemaExt *)taosMemoryCalloc(metaRsp.numOfColumns, sizeof(SSchemaExt));
19,979,753✔
208
  if (NULL == metaRsp.pSchemas || NULL == metaRsp.pSchemaExt) {
19,979,753✔
209
    code = terrno;
×
210
    goto _exit;
×
211
  }
212
  (void)memcpy(metaRsp.pSchemas, schema.pSchema, sizeof(SSchema) * schema.nCols);
19,980,386✔
213
  if (schemaTag.nCols) {
19,980,386✔
214
    (void)memcpy(metaRsp.pSchemas + schema.nCols, schemaTag.pSchema, sizeof(SSchema) * schemaTag.nCols);
15,961,810✔
215
  }
216
  if (metaRsp.pSchemaExt) {
19,980,386✔
217
    SMetaReader *pReader = mer1.me.type == TSDB_CHILD_TABLE ? &mer2 : &mer1;
19,981,019✔
218
    code = fillTableColCmpr(pReader, metaRsp.pSchemaExt, metaRsp.numOfColumns);
19,981,019✔
219
    if (code < 0) {
19,981,019✔
220
      goto _exit;
×
221
    }
222
    for (int32_t i = 0; i < metaRsp.numOfColumns && pReader->me.pExtSchemas; i++) {
55,477,754✔
223
      metaRsp.pSchemaExt[i].typeMod = pReader->me.pExtSchemas[i].typeMod;
35,496,735✔
224
    }
225
  } else {
226
    code = TSDB_CODE_OUT_OF_MEMORY;
×
227
    goto _exit;
×
228
  }
229
  if (hasRefCol(mer1.me.type)) {
19,981,217✔
230
    metaRsp.rversion = mer1.me.colRef.version;
296,059✔
231
    metaRsp.pColRefs = (SColRef*)taosMemoryMalloc(sizeof(SColRef) * metaRsp.numOfColumns);
296,059✔
232
    if (metaRsp.pColRefs) {
295,426✔
233
      code = fillTableColRef(&mer1, metaRsp.pColRefs, metaRsp.numOfColumns);
295,426✔
234
      if (code < 0) {
295,426✔
235
        goto _exit;
×
236
      }
237
    }
238
    metaRsp.numOfColRefs = metaRsp.numOfColumns;
295,426✔
239
  } else {
240
    metaRsp.pColRefs = NULL;
19,684,960✔
241
    metaRsp.numOfColRefs = 0;
19,684,960✔
242
  }
243

244
  vnodePrintTableMeta(&metaRsp);
19,980,386✔
245

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

253
  if (direct) {
19,979,749✔
254
    pRsp = rpcMallocCont(rspLen);
173,634✔
255
  } else {
256
    pRsp = taosMemoryCalloc(1, rspLen);
19,806,115✔
257
  }
258

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

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

270
_exit:
19,979,753✔
271
  taosMemoryFree(metaRsp.pColRefs);
19,979,753✔
272
  taosMemoryFree(metaRsp.pSchemas);
19,979,551✔
273
  taosMemoryFree(metaRsp.pSchemaExt);
19,979,551✔
274
_exit2:
19,979,116✔
275
  metaReaderClear(&mer2);
19,979,749✔
276
_exit3:
28,948,376✔
277
  metaReaderClear(&mer1);
28,951,021✔
278
_exit4:
28,949,708✔
279
  rpcMsg.info = pMsg->info;
28,950,648✔
280
  rpcMsg.pCont = pRsp;
28,950,593✔
281
  rpcMsg.contLen = rspLen;
28,950,593✔
282
  rpcMsg.code = code;
28,950,593✔
283
  rpcMsg.msgType = pMsg->msgType;
28,950,593✔
284

285
  if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST && autoCreateCtb == 1) {
28,946,920✔
286
    code = TSDB_CODE_SUCCESS;
5,437,883✔
287
  }
288

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

294
  if (direct) {
28,948,295✔
295
    tmsgSendRsp(&rpcMsg);
888,388✔
296
  } else {
297
    *pMsg = rpcMsg;
28,059,907✔
298
  }
299

300
  return code;
28,948,928✔
301
}
302

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

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

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

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

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

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

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

343
  if (mer1.me.type == TSDB_SUPER_TABLE) {
57,648✔
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) {
57,648✔
347
    metaReaderDoInit(&mer2, pVnode->pMeta, META_READER_NOLOCK);
39,299✔
348
    if (metaReaderGetTableEntryByUid(&mer2, mer1.me.ctbEntry.suid) < 0) goto _exit;
39,299✔
349

350
    tstrncpy(cfgRsp.stbName, mer2.me.name, TSDB_TABLE_NAME_LEN);
39,299✔
351
    schema = mer2.me.stbEntry.schemaRow;
39,299✔
352
    schemaTag = mer2.me.stbEntry.schemaTag;
39,299✔
353
    cfgRsp.ownerId = mer2.me.stbEntry.ownerId;  // child table inherits ownerId from stb
39,299✔
354
    cfgRsp.ttl = mer1.me.ctbEntry.ttlDays;
39,299✔
355
    cfgRsp.commentLen = mer1.me.ctbEntry.commentLen;
39,299✔
356
    if (mer1.me.ctbEntry.commentLen > 0) {
39,299✔
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,299✔
364
    cfgRsp.tagsLen = pTag->len;
39,299✔
365
    cfgRsp.pTags = taosMemoryMalloc(cfgRsp.tagsLen);
39,299✔
366
    if (NULL == cfgRsp.pTags) {
39,299✔
367
      code = terrno;
×
368
      goto _exit;
×
369
    }
370
    (void)memcpy(cfgRsp.pTags, pTag, cfgRsp.tagsLen);
39,299✔
371
  } else if (mer1.me.type == TSDB_NORMAL_TABLE || mer1.me.type == TSDB_VIRTUAL_NORMAL_TABLE) {
18,349✔
372
    schema = mer1.me.ntbEntry.schemaRow;
18,349✔
373
    cfgRsp.ttl = mer1.me.ntbEntry.ttlDays;
18,349✔
374
    cfgRsp.ownerId = mer1.me.ntbEntry.ownerId;
18,349✔
375
    cfgRsp.commentLen = mer1.me.ntbEntry.commentLen;
18,349✔
376
    if (mer1.me.ntbEntry.commentLen > 0) {
18,349✔
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;
57,648✔
390
  cfgRsp.numOfColumns = schema.nCols;
57,648✔
391
  cfgRsp.virtualStb = false; // vnode don't have super table, so it's always false
57,648✔
392
  cfgRsp.pSchemas = (SSchema *)taosMemoryMalloc(sizeof(SSchema) * (cfgRsp.numOfColumns + cfgRsp.numOfTags));
57,648✔
393
  cfgRsp.pSchemaExt = (SSchemaExt *)taosMemoryMalloc(cfgRsp.numOfColumns * sizeof(SSchemaExt));
57,648✔
394
  cfgRsp.pColRefs = (SColRef *)taosMemoryMalloc(sizeof(SColRef) * cfgRsp.numOfColumns);
57,648✔
395

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

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

409
  if (withExtSchema(cfgRsp.tableType)) {
57,648✔
410
    for (int32_t i = 0; i < cfgRsp.numOfColumns; i++) {
215,515✔
411
      SColCmpr   *pCmpr = &pColCmpr->pColCmpr[i];
165,484✔
412
      SSchemaExt *pSchExt = cfgRsp.pSchemaExt + i;
165,484✔
413
      pSchExt->colId = pCmpr->id;
165,484✔
414
      pSchExt->compress = pCmpr->alg;
165,484✔
415
      if (pReader->me.pExtSchemas)
165,484✔
416
        pSchExt->typeMod = pReader->me.pExtSchemas[i].typeMod;
31,361✔
417
      else
418
        pSchExt->typeMod = 0;
134,123✔
419
    }
420
  }
421

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

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

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

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

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

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

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

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

477
  tFreeSTableCfgRsp(&cfgRsp);
57,648✔
478
  metaReaderClear(&mer2);
57,648✔
479
  metaReaderClear(&mer1);
57,648✔
480
  return code;
57,648✔
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,894,165✔
493
  int32_t      code = 0;
26,894,165✔
494
  int32_t      rspSize = 0;
26,894,165✔
495
  SBatchReq    batchReq = {0};
26,894,165✔
496
  SBatchMsg   *req = NULL;
26,895,903✔
497
  SBatchRspMsg rsp = {0};
26,895,903✔
498
  SBatchRsp    batchRsp = {0};
26,880,548✔
499
  SRpcMsg      reqMsg = *pMsg;
26,879,457✔
500
  SRpcMsg      rspMsg = {0};
26,879,457✔
501
  void        *pRsp = NULL;
26,888,487✔
502

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

509
  int32_t msgNum = taosArrayGetSize(batchReq.pMsgs);
26,883,993✔
510
  if (msgNum >= MAX_META_MSG_IN_BATCH) {
26,895,407✔
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,895,407✔
517
  if (NULL == batchRsp.pRsps) {
26,884,516✔
518
    code = terrno;
×
519
    qError("taosArrayInit %d SBatchRspMsg failed", msgNum);
×
520
    goto _exit;
×
521
  }
522

523
  for (int32_t i = 0; i < msgNum; ++i) {
59,165,254✔
524
    req = taosArrayGet(batchReq.pMsgs, i);
32,283,135✔
525
    if (req == NULL) {
32,281,292✔
526
      code = terrno;
×
527
      goto _exit;
×
528
    }
529

530
    reqMsg.msgType = req->msgType;
32,281,292✔
531
    reqMsg.pCont = req->msg;
32,278,890✔
532
    reqMsg.contLen = req->msgLen;
32,277,164✔
533

534
    switch (req->msgType) {
32,281,428✔
535
      case TDMT_VND_TABLE_META:
27,797,818✔
536
        // error code has been set into reqMsg, no need to handle it here.
537
        if (TSDB_CODE_SUCCESS != vnodeGetTableMeta(pVnode, &reqMsg, false)) {
27,797,818✔
538
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
2,720,406✔
539
        }
540
        break;
27,795,919✔
541
      case TDMT_VND_TABLE_NAME:
263,988✔
542
        // error code has been set into reqMsg, no need to handle it here.
543
        if (TSDB_CODE_SUCCESS != vnodeGetTableMeta(pVnode, &reqMsg, false)) {
263,988✔
544
          qWarn("vnodeGetBatchName failed, msgType:%d", req->msgType);
96,963✔
545
        }
546
        break;
264,186✔
547
      case TDMT_VND_TABLE_CFG:
57,648✔
548
        // error code has been set into reqMsg, no need to handle it here.
549
        if (TSDB_CODE_SUCCESS != vnodeGetTableCfg(pVnode, &reqMsg, false)) {
57,648✔
550
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
×
551
        }
552
        break;
57,648✔
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,154,262✔
560
        // error code has been set into reqMsg, no need to handle it here.
561
        if (TSDB_CODE_SUCCESS != vnodeGetVStbRefDbs(pVnode, &reqMsg)) {
4,154,262✔
562
          qWarn("vnodeGetVStbRefDbs failed, msgType:%d", req->msgType);
×
563
        }
564
        break;
4,159,727✔
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;
32,277,480✔
574
    rsp.reqType = reqMsg.msgType;
32,277,181✔
575
    rsp.msgLen = reqMsg.contLen;
32,277,181✔
576
    rsp.rspCode = reqMsg.code;
32,277,181✔
577
    rsp.msg = reqMsg.pCont;
32,277,181✔
578

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

586
  rspSize = tSerializeSBatchRsp(NULL, 0, &batchRsp);
26,882,119✔
587
  if (rspSize < 0) {
26,868,253✔
588
    qError("tSerializeSBatchRsp failed");
×
589
    code = terrno;
×
590
    goto _exit;
×
591
  }
592
  pRsp = rpcMallocCont(rspSize);
26,868,253✔
593
  if (pRsp == NULL) {
26,878,381✔
594
    qError("rpcMallocCont %d failed", rspSize);
×
595
    code = terrno;
×
596
    goto _exit;
×
597
  }
598
  if (tSerializeSBatchRsp(pRsp, rspSize, &batchRsp) < 0) {
26,878,381✔
599
    qError("tSerializeSBatchRsp %d failed", rspSize);
×
600
    code = terrno;
×
601
    goto _exit;
×
602
  }
603

604
_exit:
26,886,076✔
605

606
  rspMsg.info = pMsg->info;
26,887,855✔
607
  rspMsg.pCont = pRsp;
26,887,387✔
608
  rspMsg.contLen = rspSize;
26,887,387✔
609
  rspMsg.code = code;
26,887,387✔
610
  rspMsg.msgType = pMsg->msgType;
26,887,387✔
611

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

616
  taosArrayDestroyEx(batchReq.pMsgs, tFreeSBatchReqMsg);
26,871,529✔
617
  taosArrayDestroyEx(batchRsp.pRsps, tFreeSBatchRspMsg);
26,876,391✔
618

619
  tmsgSendRsp(&rspMsg);
26,872,897✔
620

621
  return code;
26,893,631✔
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,165,449✔
725
  int32_t                    code = TSDB_CODE_SUCCESS;
4,165,449✔
726
  int32_t                    line = 0;
4,165,449✔
727
  SMetaReader                mr = {0};
4,165,449✔
728
  bool                       readerInit = false;
4,160,190✔
729
  SSHashObj*                 pDbNameHash = NULL;
4,160,190✔
730
  SArray*                    pList = NULL;
4,160,190✔
731

732
  pList = taosArrayInit(10, sizeof(uint64_t));
4,160,190✔
733
  QUERY_CHECK_NULL(pList, code, line, _return, terrno);
4,157,698✔
734

735
  *ppRes = taosArrayInit(10, POINTER_BYTES);
4,157,698✔
736
  QUERY_CHECK_NULL(*ppRes, code, line, _return, terrno)
4,166,191✔
737
  
738
  // lookup in cache
739
  code = pHandle->api.metaFn.metaGetCachedRefDbs(pHandle->vnode, suid, *ppRes);
4,164,328✔
740
  QUERY_CHECK_CODE(code, line, _return);
4,169,489✔
741

742
  if (taosArrayGetSize(*ppRes) > 0) {
4,169,489✔
743
    // found in cache
744
    goto _return;
4,137,015✔
745
  } else {
746
    code = pHandle->api.metaFn.getChildTableList(pHandle->vnode, suid, pList);
33,079✔
747
    QUERY_CHECK_CODE(code, line, _return);
33,079✔
748

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

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

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

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

763
      for (int32_t j = 0; j < mr.me.colRef.nCols; j++) {
35,338,749✔
764
        if (mr.me.colRef.pColRef[j].hasRef) {
35,263,673✔
765
          if (NULL == tSimpleHashGet(pDbNameHash, mr.me.colRef.pColRef[j].refDbName, strlen(mr.me.colRef.pColRef[j].refDbName))) {
34,959,414✔
766
            char *refDbName = taosStrdup(mr.me.colRef.pColRef[j].refDbName);
28,495✔
767
            QUERY_CHECK_NULL(refDbName, code, line, _return, terrno);
28,495✔
768

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

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

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

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

785
_return:
4,170,311✔
786

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

791
  taosArrayDestroy(pList);
4,170,361✔
792
  tSimpleHashCleanup(pDbNameHash);
4,164,751✔
793

794
  if (code) {
4,165,494✔
795
    qError("%s failed since %s", __func__, tstrerror(code));
×
796
  }
797
  return code;
4,165,494✔
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,164,449✔
863
  int32_t        code = 0;
4,164,449✔
864
  int32_t        rspSize = 0;
4,164,449✔
865
  SVStbRefDbsReq req = {0};
4,164,449✔
866
  SVStbRefDbsRsp rsp = {0};
4,164,110✔
867
  SRpcMsg        rspMsg = {0};
4,164,264✔
868
  void          *pRsp = NULL;
4,164,264✔
869
  int32_t        line = 0;
4,164,264✔
870

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

877
  SReadHandle handle = {0};
4,155,098✔
878
  handle.vnode = pVnode;
4,156,056✔
879
  initStorageAPI(&handle.api);
4,156,056✔
880

881
  code = vnodeReadVStbRefDbs(&handle, req.suid, &rsp.pDbs);
4,151,876✔
882
  QUERY_CHECK_CODE(code, line, _return);
4,166,192✔
883
  rsp.vgId = TD_VID(pVnode);
4,166,192✔
884

885
  rspSize = tSerializeSVStbRefDbsRsp(NULL, 0, &rsp);
4,167,296✔
886
  if (rspSize < 0) {
4,149,935✔
887
    code = rspSize;
×
888
    qError("tSerializeSVStbRefDbsRsp failed, error:%d", rspSize);
×
889
    goto _return;
×
890
  }
891
  pRsp = taosMemoryCalloc(1, rspSize);
4,149,935✔
892
  if (pRsp == NULL) {
4,151,381✔
893
    code = terrno;
×
894
    qError("rpcMallocCont %d failed, error:%d", rspSize, terrno);
×
895
    goto _return;
×
896
  }
897
  rspSize = tSerializeSVStbRefDbsRsp(pRsp, rspSize, &rsp);
4,151,381✔
898
  if (rspSize < 0) {
4,159,319✔
899
    code = rspSize;
×
900
    qError("tSerializeSVStbRefDbsRsp failed, error:%d", rspSize);
×
901
    goto _return;
×
902
  }
903

904
_return:
4,159,319✔
905

906
  rspMsg.info = pMsg->info;
4,158,381✔
907
  rspMsg.pCont = pRsp;
4,162,694✔
908
  rspMsg.contLen = rspSize;
4,162,694✔
909
  rspMsg.code = code;
4,162,694✔
910
  rspMsg.msgType = pMsg->msgType;
4,162,694✔
911

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

916
  *pMsg = rspMsg;
4,155,716✔
917

918
  tDestroySVStbRefDbsRsp(&rsp);
4,148,015✔
919

920
  return code;
4,151,926✔
921
}
922

923
static int32_t vnodeGetCompStorage(SVnode *pVnode, int64_t *output) {
130,777,119✔
924
  int32_t code = 0;
130,777,119✔
925
#ifdef TD_ENTERPRISE
926
  int32_t now = taosGetTimestampSec();
130,777,119✔
927
  if (llabs(now - pVnode->config.vndStats.storageLastUpd) >= 30) {
130,777,119✔
928
    pVnode->config.vndStats.storageLastUpd = now;
6,854,725✔
929

930
    SDbSizeStatisInfo info = {0};
6,854,725✔
931
    if (0 == (code = vnodeGetDBSize(pVnode, &info))) {
6,854,725✔
932
      int64_t compSize =
6,854,725✔
933
          info.l1Size + info.l2Size + info.l3Size + info.cacheSize + info.walSize + info.metaSize + +info.ssSize;
6,854,725✔
934
      if (compSize >= 0) {
6,854,725✔
935
        pVnode->config.vndStats.compStorage = compSize;
6,854,725✔
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;
130,777,119✔
945
#endif
946
  return code;
130,777,119✔
947
}
948

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

955
    if (pVnode->inUse) {
130,777,119✔
956
      *bufferSegmentUsed = pVnode->inUse->size;
130,770,906✔
957
    }
958
    *bufferSegmentSize = pVnode->config.szBuf / VNODE_BUFPOOL_SEGMENTS;
130,777,119✔
959

960
    (void)taosThreadMutexUnlock(&pVnode->mutex);
130,777,119✔
961
  }
962
}
130,777,119✔
963

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

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

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

1024
  if (dbname) {
14,852,161✔
1025
    *dbname = pConf->dbname;
14,832,883✔
1026
  }
1027

1028
  if (vgId) {
14,852,794✔
1029
    *vgId = TD_VID(pVnodeObj);
14,058,570✔
1030
  }
1031

1032
  if (numOfTables) {
14,849,704✔
1033
    *numOfTables = pConf->vndStats.numOfNTables + pConf->vndStats.numOfCTables +
21,708✔
1034
                   pConf->vndStats.numOfVTables + pConf->vndStats.numOfVCTables;
10,854✔
1035
  }
1036

1037
  if (numOfNormalTables) {
14,849,704✔
1038
    *numOfNormalTables = pConf->vndStats.numOfNTables +
16,122✔
1039
                         pConf->vndStats.numOfVTables;
8,150✔
1040
  }
1041
}
14,849,882✔
1042

1043
int32_t vnodeGetTableList(void *pVnode, int8_t type, SArray *pList) {
8,150✔
1044
  if (type == TSDB_SUPER_TABLE) {
8,150✔
1045
    return vnodeGetStbIdList(pVnode, 0, pList);
8,150✔
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) {
108,644,776✔
1082
  int32_t      code = TSDB_CODE_SUCCESS;
108,644,776✔
1083
  SVnode      *pVnodeObj = pVnode;
108,644,776✔
1084
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnodeObj, suid, 1);
108,644,776✔
1085
  if (NULL == pCur) {
108,633,345✔
1086
    qError("vnode get all table list failed");
×
1087
    return terrno;
×
1088
  }
1089

1090
  while (1) {
442,025,919✔
1091
    tb_uid_t id = metaCtbCursorNext(pCur);
550,659,264✔
1092
    if (id == 0) {
550,593,376✔
1093
      break;
108,667,717✔
1094
    }
1095
    qTrace("vnodeGetCtbIdList: got ctb id %" PRId64 " for suid %" PRId64, id, suid);
441,925,659✔
1096
    if (NULL == taosArrayPush(list, &id)) {
441,911,025✔
1097
      qError("taosArrayPush failed");
×
1098
      code = terrno;
×
1099
      goto _exit;
×
1100
    }
1101
  }
1102

1103
_exit:
108,615,936✔
1104
  metaCloseCtbCursor(pCur);
108,615,936✔
1105
  return code;
108,586,107✔
1106
}
1107

1108
int32_t vnodeGetStbIdList(SVnode *pVnode, int64_t suid, SArray *list) {
63,896,234✔
1109
  int32_t      code = TSDB_CODE_SUCCESS;
63,896,234✔
1110
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, suid);
63,896,234✔
1111
  if (!pCur) {
63,897,500✔
1112
    return TSDB_CODE_OUT_OF_MEMORY;
×
1113
  }
1114

1115
  while (1) {
50,239,240✔
1116
    tb_uid_t id = metaStbCursorNext(pCur);
114,136,740✔
1117
    if (id == 0) {
114,138,566✔
1118
      break;
63,903,197✔
1119
    }
1120

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

1128
_exit:
63,895,903✔
1129
  metaCloseStbCursor(pCur);
63,895,903✔
1130
  return code;
63,894,004✔
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,675,249✔
1164
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, suid, 0);
4,675,249✔
1165
  if (!pCur) {
4,675,124✔
1166
    return terrno;
×
1167
  }
1168

1169
  *num = 0;
4,675,124✔
1170
  while (1) {
2,629,206✔
1171
    tb_uid_t id = metaCtbCursorNext(pCur);
7,304,334✔
1172
    if (id == 0) {
7,304,570✔
1173
      break;
4,675,448✔
1174
    }
1175

1176
    ++(*num);
2,629,122✔
1177
  }
1178

1179
  metaCloseCtbCursor(pCur);
4,675,448✔
1180
  return TSDB_CODE_SUCCESS;
4,675,412✔
1181
}
1182

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

1192
  return TSDB_CODE_SUCCESS;
4,674,628✔
1193
}
1194

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

1199
  int32_t code = metaReaderGetTableEntryByUid(&mr, suid);
4,674,309✔
1200
  if (code == TSDB_CODE_SUCCESS) {
4,675,006✔
1201
    if (keep) *keep = mr.me.stbEntry.keep;
4,675,006✔
1202
    if (flags) *flags = mr.me.flags;
4,675,006✔
1203
  } else {
1204
    if (keep) *keep = 0;
×
1205
    if (flags) *flags = 0;
×
1206
  }
1207

1208
  metaReaderClear(&mr);
4,675,006✔
1209
  return TSDB_CODE_SUCCESS;
4,675,412✔
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) {
63,890,291✔
1238
  int32_t      code = TSDB_CODE_SUCCESS;
63,890,291✔
1239
  int32_t      tbNum = 0;
63,890,291✔
1240
  const char **pTbArr = NULL;
63,890,291✔
1241
  const char  *dbName = NULL;
63,890,291✔
1242
  *tbSize = 0;
63,890,291✔
1243

1244
  if (!(dbName = strchr(pVnode->config.dbname, '.'))) return 0;
63,890,924✔
1245
  if (0 == strncmp(++dbName, "log", TSDB_DB_NAME_LEN)) {
63,889,327✔
1246
    tbNum = tkLogStbNum;
2,754✔
1247
    pTbArr = (const char **)&tkLogStb;
2,754✔
1248
  } else if (0 == strncmp(dbName, "audit", TSDB_DB_NAME_LEN)) {
63,888,803✔
1249
    tbNum = tkAuditStbNum;
2,496✔
1250
    pTbArr = (const char **)&tkAuditStb;
2,496✔
1251
  }
1252
  if (tbNum && pTbArr) {
63,891,557✔
1253
    *tbSize = metaSizeOfTbFilterCache(pVnode->pMeta, 0);
5,250✔
1254
    if (*tbSize < tbNum) {
5,250✔
1255
      for (int32_t i = 0; i < tbNum; ++i) {
43,548✔
1256
        tb_uid_t suid = metaGetTableEntryUidByName(pVnode->pMeta, pTbArr[i]);
40,134✔
1257
        if (suid != 0) {
40,134✔
1258
          code = metaPutTbToFilterCache(pVnode->pMeta, &suid, 0);
3,366✔
1259
          if (TSDB_CODE_SUCCESS != code) {
3,366✔
1260
            return code;
1,836✔
1261
          }
1262
        }
1263
      }
1264
      *tbSize = metaSizeOfTbFilterCache(pVnode->pMeta, 0);
3,414✔
1265
    }
1266
  }
1267

1268
  return code;
63,888,455✔
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) {
63,890,924✔
1282
  SArray *suidList = NULL;
63,890,924✔
1283

1284
  if (!(suidList = taosArrayInit(1, sizeof(tb_uid_t)))) {
63,890,924✔
1285
    return terrno;
×
1286
  }
1287

1288
  int32_t tbFilterSize = 0;
63,889,327✔
1289
  int32_t code = TSDB_CODE_SUCCESS;
63,881,400✔
1290
#ifdef TD_ENTERPRISE
1291
  code = vnodeGetTimeSeriesBlackList(pVnode, &tbFilterSize);
63,881,400✔
1292
  if (TSDB_CODE_SUCCESS != code) {
63,890,291✔
1293
    goto _exit;
1,836✔
1294
  }
1295
#endif
1296

1297
  if ((!tbFilterSize && vnodeGetStbIdList(pVnode, 0, suidList) < 0) ||
63,888,455✔
1298
      (tbFilterSize && vnodeGetStbIdListByFilter(pVnode, 0, suidList, vnodeTimeSeriesFilter, pVnode) < 0)) {
63,880,528✔
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;
63,886,556✔
1305
  int64_t arrSize = taosArrayGetSize(suidList);
63,884,326✔
1306
  for (int64_t i = 0; i < arrSize; ++i) {
114,114,230✔
1307
    tb_uid_t suid = *(tb_uid_t *)taosArrayGet(suidList, i);
50,222,941✔
1308

1309
    int64_t ctbNum = 0;
50,227,070✔
1310
    int32_t numOfCols = 0;
50,228,336✔
1311
    int8_t  flags = 0;
50,225,804✔
1312
    code = metaGetStbStats(pVnode, suid, &ctbNum, &numOfCols, &flags);
50,218,208✔
1313
    if (TSDB_CODE_SUCCESS != code) {
50,239,428✔
1314
      goto _exit;
×
1315
    }
1316
    if (!TABLE_IS_VIRTUAL(flags)) {
50,239,428✔
1317
      *num += ctbNum * (numOfCols - 1);
46,933,759✔
1318
    }
1319
  }
1320

1321
_exit:
63,888,363✔
1322
  taosArrayDestroy(suidList);
63,894,391✔
1323
  return TSDB_CODE_SUCCESS;
63,886,420✔
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,040,293✔
1354
  if (pVnode == NULL) {
4,040,293✔
1355
    return NULL;
×
1356
  }
1357

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

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

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

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

1376
  char   *dirName[] = {VNODE_TSDB_DIR, VNODE_WAL_DIR, VNODE_META_DIR, VNODE_TSDB_CACHE_DIR};
6,860,839✔
1377
  int64_t dirSize[4];
6,860,372✔
1378

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

1382
  for (int i = 0; i < sizeof(dirName) / sizeof(dirName[0]); i++) {
34,304,195✔
1383
    int64_t size = {0};
27,443,356✔
1384
    (void)snprintf(path + offset, TSDB_FILENAME_LEN - offset, "%s%s", TD_DIRSEP, dirName[i]);
27,443,356✔
1385
    code = taosGetDirSize(path, &size);
27,443,356✔
1386
    if (code != 0) {
27,443,356✔
1387
      uWarn("vnode %d get dir %s %s size failed since %s", TD_VID(pVnode), path, dirName[i], tstrerror(code));
531✔
1388
    }
1389
    path[offset] = 0;
27,443,356✔
1390
    dirSize[i] = size;
27,443,356✔
1391
  }
1392

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

1409
  code = tsdbGetFsSize(pVnodeObj->pTsdb, pInfo);
6,860,839✔
1410
_exit:
6,860,839✔
1411
  return code;
6,860,839✔
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