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

taosdata / TDengine / #4903

27 Dec 2025 02:36PM UTC coverage: 65.73% (+0.09%) from 65.642%
#4903

push

travis-ci

web-flow
fix: ci errors (#34079)

192966 of 293572 relevant lines covered (65.73%)

118504973.46 hits per line

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

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

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

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

36
int32_t fillTableColCmpr(SMetaReader *reader, SSchemaExt *pExt, int32_t numOfCol) {
17,879,337✔
37
  int8_t tblType = reader->me.type;
17,879,337✔
38
  if (withExtSchema(tblType)) {
17,878,457✔
39
    SColCmprWrapper *p = &(reader->me.colCmpr);
17,711,855✔
40
    if (numOfCol != p->nCols) {
17,712,026✔
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++) {
209,203,400✔
45
      SColCmpr *pCmpr = &p->pColCmpr[i];
191,491,985✔
46
      pExt[i].colId = pCmpr->id;
191,490,934✔
47
      pExt[i].compress = pCmpr->alg;
191,491,374✔
48
    }
49
  }
50
  return 0;
17,878,726✔
51
}
52

53
void vnodePrintTableMeta(STableMetaRsp *pMeta) {
17,879,337✔
54
  if (!(qDebugFlag & DEBUG_DEBUG)) {
17,879,337✔
55
    return;
103,114✔
56
  }
57

58
  qDebug("tbName:%s", pMeta->tbName);
17,776,223✔
59
  qDebug("stbName:%s", pMeta->stbName);
17,776,565✔
60
  qDebug("dbFName:%s", pMeta->dbFName);
17,776,565✔
61
  qDebug("dbId:%" PRId64, pMeta->dbId);
17,776,565✔
62
  qDebug("numOfTags:%d", pMeta->numOfTags);
17,776,565✔
63
  qDebug("numOfColumns:%d", pMeta->numOfColumns);
17,776,565✔
64
  qDebug("precision:%d", pMeta->precision);
17,776,565✔
65
  qDebug("tableType:%d", pMeta->tableType);
17,776,565✔
66
  qDebug("sversion:%d", pMeta->sversion);
17,776,565✔
67
  qDebug("tversion:%d", pMeta->tversion);
17,776,565✔
68
  qDebug("suid:%" PRIu64, pMeta->suid);
17,776,565✔
69
  qDebug("tuid:%" PRIu64, pMeta->tuid);
17,776,565✔
70
  qDebug("vgId:%d", pMeta->vgId);
17,776,565✔
71
  qDebug("sysInfo:%d", pMeta->sysInfo);
17,776,565✔
72
  if (pMeta->pSchemas) {
17,776,565✔
73
    for (int32_t i = 0; i < (pMeta->numOfColumns + pMeta->numOfTags); ++i) {
245,110,407✔
74
      SSchema *pSchema = pMeta->pSchemas + i;
227,333,842✔
75
      qDebug("%d col/tag: type:%d, flags:%d, colId:%d, bytes:%d, name:%s", i, pSchema->type, pSchema->flags,
227,333,842✔
76
             pSchema->colId, pSchema->bytes, pSchema->name);
77
    }
78
  }
79
}
80

81
int32_t fillTableColRef(SMetaReader *reader, SColRef *pRef, int32_t numOfCol) {
167,482✔
82
  int8_t tblType = reader->me.type;
167,482✔
83
  if (hasRefCol(tblType)) {
167,482✔
84
    SColRefWrapper *p = &(reader->me.colRef);
167,482✔
85
    if (numOfCol != p->nCols) {
167,482✔
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++) {
833,646✔
90
      SColRef *pColRef = &p->pColRef[i];
666,164✔
91
      pRef[i].hasRef = pColRef->hasRef;
666,164✔
92
      pRef[i].id = pColRef->id;
666,164✔
93
      if(pRef[i].hasRef) {
666,164✔
94
        tstrncpy(pRef[i].refDbName, pColRef->refDbName, TSDB_DB_NAME_LEN);
347,180✔
95
        tstrncpy(pRef[i].refTableName, pColRef->refTableName, TSDB_TABLE_NAME_LEN);
347,180✔
96
        tstrncpy(pRef[i].refColName, pColRef->refColName, TSDB_COL_NAME_LEN);
347,180✔
97
      }
98
    }
99
  }
100
  return 0;
167,482✔
101
}
102

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

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

125
  if (infoReq.option == REQ_OPT_TBUID) reqTbUid = true;
25,318,380✔
126
  metaRsp.dbId = pVnode->config.dbId;
25,318,380✔
127
  tstrncpy(metaRsp.tbName, infoReq.tbName, TSDB_TABLE_NAME_LEN);
25,315,936✔
128
  (void)memcpy(metaRsp.dbFName, infoReq.dbFName, sizeof(metaRsp.dbFName));
25,315,936✔
129

130
  if (!reqTbUid) {
25,315,936✔
131
    (void)tsnprintf(tableFName, TSDB_TABLE_FNAME_LEN, "%s.%s", infoReq.dbFName, infoReq.tbName);
25,097,071✔
132
    if (pVnode->mounted) tTrimMountPrefix(tableFName);
25,097,682✔
133
    code = vnodeValidateTableHash(pVnode, tableFName);
25,098,293✔
134
    if (code) {
25,098,293✔
135
      goto _exit4;
×
136
    }
137
  }
138

139
  // query meta
140
  metaReaderDoInit(&mer1, pVnode->pMeta, META_READER_LOCK);
25,317,158✔
141
  if (reqTbUid) {
25,318,991✔
142
    SET_ERRNO(0);
220,698✔
143
    uint64_t tbUid = taosStr2UInt64(infoReq.tbName, NULL, 10);
220,527✔
144
    if (ERRNO == ERANGE || tbUid == 0) {
220,527✔
145
      code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
146
      goto _exit3;
×
147
    }
148
    SMetaReader mr3 = {0};
220,698✔
149
    metaReaderDoInit(&mr3, ((SVnode *)pVnode)->pMeta, META_READER_NOLOCK);
220,698✔
150
    if ((code = metaReaderGetTableEntryByUid(&mr3, tbUid)) < 0) {
220,698✔
151
      metaReaderClear(&mr3);
85,098✔
152
      TAOS_CHECK_GOTO(code, NULL, _exit3);
85,098✔
153
    }
154
    tstrncpy(metaRsp.tbName, mr3.me.name, TSDB_TABLE_NAME_LEN);
135,429✔
155
    metaReaderClear(&mr3);
135,429✔
156
    TAOS_CHECK_GOTO(metaGetTableEntryByName(&mer1, metaRsp.tbName), NULL, _exit3);
135,429✔
157
  } else if (metaGetTableEntryByName(&mer1, infoReq.tbName) < 0) {
25,098,293✔
158
    code = terrno;
7,354,214✔
159
    goto _exit3;
7,353,868✔
160
  }
161

162
  metaRsp.tableType = mer1.me.type;
17,878,991✔
163
  metaRsp.vgId = TD_VID(pVnode);
17,878,991✔
164
  metaRsp.tuid = mer1.me.uid;
17,878,890✔
165

166
  switch (mer1.me.type) {
17,878,890✔
167
    case TSDB_SUPER_TABLE: {
3,348,562✔
168
      (void)strcpy(metaRsp.stbName, mer1.me.name);
3,348,562✔
169
      schema = mer1.me.stbEntry.schemaRow;
3,348,562✔
170
      schemaTag = mer1.me.stbEntry.schemaTag;
3,348,562✔
171
      metaRsp.suid = mer1.me.uid;
3,348,562✔
172
      metaRsp.virtualStb = TABLE_IS_VIRTUAL(mer1.me.flags);
3,348,562✔
173
      break;
3,348,562✔
174
    }
175
    case TSDB_CHILD_TABLE:
11,737,665✔
176
    case TSDB_VIRTUAL_CHILD_TABLE:{
177
      metaReaderDoInit(&mer2, pVnode->pMeta, META_READER_NOLOCK);
11,737,665✔
178
      if (metaReaderGetTableEntryByUid(&mer2, mer1.me.ctbEntry.suid) < 0) goto _exit2;
11,738,283✔
179

180
      (void)strcpy(metaRsp.stbName, mer2.me.name);
11,738,283✔
181
      metaRsp.suid = mer2.me.uid;
11,738,283✔
182
      schema = mer2.me.stbEntry.schemaRow;
11,738,283✔
183
      schemaTag = mer2.me.stbEntry.schemaTag;
11,738,283✔
184
      break;
11,738,283✔
185
    }
186
    case TSDB_NORMAL_TABLE:
2,792,663✔
187
    case TSDB_VIRTUAL_NORMAL_TABLE: {
188
      schema = mer1.me.ntbEntry.schemaRow;
2,792,663✔
189
      break;
2,792,663✔
190
    }
191
    default: {
×
192
      vError("vnodeGetTableMeta get invalid table type:%d", mer1.me.type);
×
193
      goto _exit3;
×
194
    }
195
  }
196

197
  metaRsp.numOfTags = schemaTag.nCols;
17,879,508✔
198
  metaRsp.numOfColumns = schema.nCols;
17,879,508✔
199
  metaRsp.precision = pVnode->config.tsdbCfg.precision;
17,879,508✔
200
  metaRsp.sversion = schema.version;
17,879,337✔
201
  metaRsp.tversion = schemaTag.version;
17,879,337✔
202
  metaRsp.pSchemas = (SSchema *)taosMemoryMalloc(sizeof(SSchema) * (metaRsp.numOfColumns + metaRsp.numOfTags));
17,879,337✔
203
  metaRsp.pSchemaExt = (SSchemaExt *)taosMemoryCalloc(metaRsp.numOfColumns, sizeof(SSchemaExt));
17,878,457✔
204
  if (NULL == metaRsp.pSchemas || NULL == metaRsp.pSchemaExt) {
17,878,457✔
205
    code = terrno;
1,222✔
206
    goto _exit;
×
207
  }
208
  (void)memcpy(metaRsp.pSchemas, schema.pSchema, sizeof(SSchema) * schema.nCols);
17,877,235✔
209
  if (schemaTag.nCols) {
17,877,235✔
210
    (void)memcpy(metaRsp.pSchemas + schema.nCols, schemaTag.pSchema, sizeof(SSchema) * schemaTag.nCols);
15,086,674✔
211
  }
212
  if (metaRsp.pSchemaExt) {
17,877,235✔
213
    SMetaReader *pReader = mer1.me.type == TSDB_CHILD_TABLE ? &mer2 : &mer1;
17,879,679✔
214
    code = fillTableColCmpr(pReader, metaRsp.pSchemaExt, metaRsp.numOfColumns);
17,879,679✔
215
    if (code < 0) {
17,878,726✔
216
      goto _exit;
×
217
    }
218
    for (int32_t i = 0; i < metaRsp.numOfColumns && pReader->me.pExtSchemas; i++) {
42,289,035✔
219
      metaRsp.pSchemaExt[i].typeMod = pReader->me.pExtSchemas[i].typeMod;
24,410,309✔
220
    }
221
  } else {
222
    code = TSDB_CODE_OUT_OF_MEMORY;
×
223
    goto _exit;
×
224
  }
225
  if (hasRefCol(mer1.me.type)) {
17,878,726✔
226
    metaRsp.rversion = mer1.me.colRef.version;
167,653✔
227
    metaRsp.pColRefs = (SColRef*)taosMemoryMalloc(sizeof(SColRef) * metaRsp.numOfColumns);
167,653✔
228
    if (metaRsp.pColRefs) {
167,482✔
229
      code = fillTableColRef(&mer1, metaRsp.pColRefs, metaRsp.numOfColumns);
167,482✔
230
      if (code < 0) {
167,482✔
231
        goto _exit;
×
232
      }
233
    }
234
    metaRsp.numOfColRefs = metaRsp.numOfColumns;
167,482✔
235
  } else {
236
    metaRsp.pColRefs = NULL;
17,711,855✔
237
    metaRsp.numOfColRefs = 0;
17,711,855✔
238
  }
239

240
  vnodePrintTableMeta(&metaRsp);
17,879,337✔
241

242
  // encode and send response
243
  rspLen = tSerializeSTableMetaRsp(NULL, 0, &metaRsp);
17,879,679✔
244
  if (rspLen < 0) {
17,879,068✔
245
    code = terrno;
×
246
    goto _exit;
×
247
  }
248

249
  if (direct) {
17,879,068✔
250
    pRsp = rpcMallocCont(rspLen);
83,967✔
251
  } else {
252
    pRsp = taosMemoryCalloc(1, rspLen);
17,795,101✔
253
  }
254

255
  if (pRsp == NULL) {
17,878,759✔
256
    code = terrno;
×
257
    goto _exit;
×
258
  }
259

260
  rspLen = tSerializeSTableMetaRsp(pRsp, rspLen, &metaRsp);
17,878,759✔
261
  if (rspLen < 0) {
17,879,679✔
262
    code = terrno;
×
263
    goto _exit;
×
264
  }
265

266
_exit:
17,877,235✔
267
  taosMemoryFree(metaRsp.pColRefs);
17,879,679✔
268
  taosMemoryFree(metaRsp.pSchemas);
17,879,068✔
269
  taosMemoryFree(metaRsp.pSchemaExt);
17,879,068✔
270
_exit2:
17,878,457✔
271
  metaReaderClear(&mer2);
17,879,679✔
272
_exit3:
25,316,983✔
273
  metaReaderClear(&mer1);
25,318,380✔
274
_exit4:
25,318,380✔
275
  rpcMsg.info = pMsg->info;
25,318,645✔
276
  rpcMsg.pCont = pRsp;
25,318,991✔
277
  rpcMsg.contLen = rspLen;
25,318,991✔
278
  rpcMsg.code = code;
25,318,991✔
279
  rpcMsg.msgType = pMsg->msgType;
25,318,991✔
280

281
  if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST && autoCreateCtb == 1) {
25,318,071✔
282
    code = TSDB_CODE_SUCCESS;
4,684,159✔
283
  }
284

285
  if (code) {
25,318,071✔
286
    qError("vgId:%d, get table %s meta with %" PRIu8 " failed cause of %s", pVnode->config.vgId, infoReq.tbName,
2,754,807✔
287
           infoReq.option, tstrerror(code));
288
  }
289

290
  if (direct) {
25,318,380✔
291
    tmsgSendRsp(&rpcMsg);
763,954✔
292
  } else {
293
    *pMsg = rpcMsg;
24,554,426✔
294
  }
295

296
  return code;
25,316,849✔
297
}
298

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

312
  // decode req
313
  if (tDeserializeSTableCfgReq(pMsg->pCont, pMsg->contLen, &cfgReq) != 0) {
48,744✔
314
    code = terrno;
×
315
    goto _exit;
×
316
  }
317

318
  tstrncpy(cfgRsp.tbName, cfgReq.tbName, TSDB_TABLE_NAME_LEN);
48,744✔
319
  (void)memcpy(cfgRsp.dbFName, cfgReq.dbFName, sizeof(cfgRsp.dbFName));
48,744✔
320

321
  (void)tsnprintf(tableFName, TSDB_TABLE_FNAME_LEN, "%s.%s", cfgReq.dbFName, cfgReq.tbName);
48,744✔
322
  if (pVnode->mounted) tTrimMountPrefix(tableFName);
48,744✔
323
  code = vnodeValidateTableHash(pVnode, tableFName);
48,744✔
324
  if (code) {
48,744✔
325
    goto _exit;
×
326
  }
327

328
  // query meta
329
  metaReaderDoInit(&mer1, pVnode->pMeta, META_READER_LOCK);
48,744✔
330

331
  if (metaGetTableEntryByName(&mer1, cfgReq.tbName) < 0) {
48,744✔
332
    code = terrno;
×
333
    goto _exit;
×
334
  }
335

336
  cfgRsp.tableType = mer1.me.type;
48,744✔
337

338
  if (mer1.me.type == TSDB_SUPER_TABLE) {
48,744✔
339
    code = TSDB_CODE_VND_HASH_MISMATCH;
×
340
    goto _exit;
×
341
  } else if (mer1.me.type == TSDB_CHILD_TABLE || mer1.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
48,744✔
342
    metaReaderDoInit(&mer2, pVnode->pMeta, META_READER_NOLOCK);
33,623✔
343
    if (metaReaderGetTableEntryByUid(&mer2, mer1.me.ctbEntry.suid) < 0) goto _exit;
33,623✔
344

345
    tstrncpy(cfgRsp.stbName, mer2.me.name, TSDB_TABLE_NAME_LEN);
33,623✔
346
    schema = mer2.me.stbEntry.schemaRow;
33,623✔
347
    schemaTag = mer2.me.stbEntry.schemaTag;
33,623✔
348
    cfgRsp.ttl = mer1.me.ctbEntry.ttlDays;
33,623✔
349
    cfgRsp.commentLen = mer1.me.ctbEntry.commentLen;
33,623✔
350
    if (mer1.me.ctbEntry.commentLen > 0) {
33,623✔
351
      cfgRsp.pComment = taosStrdup(mer1.me.ctbEntry.comment);
×
352
      if (NULL == cfgRsp.pComment) {
×
353
        code = terrno;
×
354
        goto _exit;
×
355
      }
356
    }
357
    STag *pTag = (STag *)mer1.me.ctbEntry.pTags;
33,623✔
358
    cfgRsp.tagsLen = pTag->len;
33,623✔
359
    cfgRsp.pTags = taosMemoryMalloc(cfgRsp.tagsLen);
33,623✔
360
    if (NULL == cfgRsp.pTags) {
33,623✔
361
      code = terrno;
×
362
      goto _exit;
×
363
    }
364
    (void)memcpy(cfgRsp.pTags, pTag, cfgRsp.tagsLen);
33,623✔
365
  } else if (mer1.me.type == TSDB_NORMAL_TABLE || mer1.me.type == TSDB_VIRTUAL_NORMAL_TABLE) {
15,121✔
366
    schema = mer1.me.ntbEntry.schemaRow;
15,121✔
367
    cfgRsp.ttl = mer1.me.ntbEntry.ttlDays;
15,121✔
368
    cfgRsp.commentLen = mer1.me.ntbEntry.commentLen;
15,121✔
369
    if (mer1.me.ntbEntry.commentLen > 0) {
15,121✔
370
      cfgRsp.pComment = taosStrdup(mer1.me.ntbEntry.comment);
×
371
      if (NULL == cfgRsp.pComment) {
×
372
        code = terrno;
×
373
        goto _exit;
×
374
      }
375
    }
376
  } else {
377
    vError("vnodeGetTableCfg get invalid table type:%d", mer1.me.type);
×
378
    code = TSDB_CODE_APP_ERROR;
×
379
    goto _exit;
×
380
  }
381

382
  cfgRsp.numOfTags = schemaTag.nCols;
48,744✔
383
  cfgRsp.numOfColumns = schema.nCols;
48,744✔
384
  cfgRsp.virtualStb = false; // vnode don't have super table, so it's always false
48,744✔
385
  cfgRsp.pSchemas = (SSchema *)taosMemoryMalloc(sizeof(SSchema) * (cfgRsp.numOfColumns + cfgRsp.numOfTags));
48,744✔
386
  cfgRsp.pSchemaExt = (SSchemaExt *)taosMemoryMalloc(cfgRsp.numOfColumns * sizeof(SSchemaExt));
48,744✔
387
  cfgRsp.pColRefs = (SColRef *)taosMemoryMalloc(sizeof(SColRef) * cfgRsp.numOfColumns);
48,744✔
388

389
  if (NULL == cfgRsp.pSchemas || NULL == cfgRsp.pSchemaExt || NULL == cfgRsp.pColRefs) {
48,744✔
390
    code = terrno;
×
391
    goto _exit;
×
392
  }
393
  (void)memcpy(cfgRsp.pSchemas, schema.pSchema, sizeof(SSchema) * schema.nCols);
48,744✔
394
  if (schemaTag.nCols) {
48,744✔
395
    (void)memcpy(cfgRsp.pSchemas + schema.nCols, schemaTag.pSchema, sizeof(SSchema) * schemaTag.nCols);
33,623✔
396
  }
397

398
  SMetaReader     *pReader = (mer1.me.type == TSDB_CHILD_TABLE || mer1.me.type == TSDB_VIRTUAL_CHILD_TABLE) ? &mer2 : &mer1;
48,744✔
399
  SColCmprWrapper *pColCmpr = &pReader->me.colCmpr;
48,744✔
400
  SColRefWrapper  *pColRef = &mer1.me.colRef;
48,744✔
401

402
  if (withExtSchema(cfgRsp.tableType)) {
48,744✔
403
    for (int32_t i = 0; i < cfgRsp.numOfColumns; i++) {
191,425✔
404
      SColCmpr   *pCmpr = &pColCmpr->pColCmpr[i];
145,843✔
405
      SSchemaExt *pSchExt = cfgRsp.pSchemaExt + i;
145,843✔
406
      pSchExt->colId = pCmpr->id;
145,843✔
407
      pSchExt->compress = pCmpr->alg;
145,843✔
408
      if (pReader->me.pExtSchemas)
145,843✔
409
        pSchExt->typeMod = pReader->me.pExtSchemas[i].typeMod;
23,287✔
410
      else
411
        pSchExt->typeMod = 0;
122,556✔
412
    }
413
  }
414

415
  cfgRsp.virtualStb = false;
48,744✔
416
  if (hasRefCol(cfgRsp.tableType)) {
48,744✔
417
    for (int32_t i = 0; i < cfgRsp.numOfColumns; i++) {
32,810✔
418
      SColRef *pRef = &pColRef->pColRef[i];
29,648✔
419
      cfgRsp.pColRefs[i].hasRef = pRef->hasRef;
29,648✔
420
      cfgRsp.pColRefs[i].id = pRef->id;
29,648✔
421
      if (cfgRsp.pColRefs[i].hasRef) {
29,648✔
422
        tstrncpy(cfgRsp.pColRefs[i].refDbName, pRef->refDbName, TSDB_DB_NAME_LEN);
12,847✔
423
        tstrncpy(cfgRsp.pColRefs[i].refTableName, pRef->refTableName, TSDB_TABLE_NAME_LEN);
12,847✔
424
        tstrncpy(cfgRsp.pColRefs[i].refColName, pRef->refColName, TSDB_COL_NAME_LEN);
12,847✔
425
      }
426
    }
427
  }
428

429
  // encode and send response
430
  rspLen = tSerializeSTableCfgRsp(NULL, 0, &cfgRsp);
48,744✔
431
  if (rspLen < 0) {
48,744✔
432
    code = terrno;
×
433
    goto _exit;
×
434
  }
435

436
  if (direct) {
48,744✔
437
    pRsp = rpcMallocCont(rspLen);
×
438
  } else {
439
    pRsp = taosMemoryCalloc(1, rspLen);
48,744✔
440
  }
441

442
  if (pRsp == NULL) {
48,744✔
443
    code = terrno;
×
444
    goto _exit;
×
445
  }
446

447
  rspLen = tSerializeSTableCfgRsp(pRsp, rspLen, &cfgRsp);
48,744✔
448
  if (rspLen < 0) {
48,744✔
449
    code = terrno;
×
450
    goto _exit;
×
451
  }
452

453
_exit:
48,744✔
454
  rpcMsg.info = pMsg->info;
48,744✔
455
  rpcMsg.pCont = pRsp;
48,744✔
456
  rpcMsg.contLen = rspLen;
48,744✔
457
  rpcMsg.code = code;
48,744✔
458
  rpcMsg.msgType = pMsg->msgType;
48,744✔
459

460
  if (code) {
48,744✔
461
    qError("get table %s cfg failed cause of %s", cfgReq.tbName, tstrerror(code));
×
462
  }
463

464
  if (direct) {
48,744✔
465
    tmsgSendRsp(&rpcMsg);
×
466
  } else {
467
    *pMsg = rpcMsg;
48,744✔
468
  }
469

470
  tFreeSTableCfgRsp(&cfgRsp);
48,744✔
471
  metaReaderClear(&mer2);
48,744✔
472
  metaReaderClear(&mer1);
48,744✔
473
  return code;
48,744✔
474
}
475

476
static FORCE_INLINE void vnodeFreeSBatchRspMsg(void *p) {
477
  if (NULL == p) {
478
    return;
479
  }
480

481
  SBatchRspMsg *pRsp = (SBatchRspMsg *)p;
482
  rpcFreeCont(pRsp->msg);
483
}
484

485
int32_t vnodeGetBatchMeta(SVnode *pVnode, SRpcMsg *pMsg) {
20,572,051✔
486
  int32_t      code = 0;
20,572,051✔
487
  int32_t      rspSize = 0;
20,572,051✔
488
  SBatchReq    batchReq = {0};
20,572,051✔
489
  SBatchMsg   *req = NULL;
20,572,603✔
490
  SBatchRspMsg rsp = {0};
20,572,603✔
491
  SBatchRsp    batchRsp = {0};
20,570,829✔
492
  SRpcMsg      reqMsg = *pMsg;
20,570,829✔
493
  SRpcMsg      rspMsg = {0};
20,571,381✔
494
  void        *pRsp = NULL;
20,572,774✔
495

496
  if (tDeserializeSBatchReq(pMsg->pCont, pMsg->contLen, &batchReq)) {
20,572,774✔
497
    code = terrno;
×
498
    qError("tDeserializeSBatchReq failed");
×
499
    goto _exit;
×
500
  }
501

502
  int32_t msgNum = taosArrayGetSize(batchReq.pMsgs);
20,572,568✔
503
  if (msgNum >= MAX_META_MSG_IN_BATCH) {
20,571,405✔
504
    code = TSDB_CODE_INVALID_MSG;
×
505
    qError("too many msgs %d in vnode batch meta req", msgNum);
×
506
    goto _exit;
×
507
  }
508

509
  batchRsp.pRsps = taosArrayInit(msgNum, sizeof(SBatchRspMsg));
20,571,405✔
510
  if (NULL == batchRsp.pRsps) {
20,570,242✔
511
    code = terrno;
×
512
    qError("taosArrayInit %d SBatchRspMsg failed", msgNum);
×
513
    goto _exit;
×
514
  }
515

516
  for (int32_t i = 0; i < msgNum; ++i) {
45,376,259✔
517
    req = taosArrayGet(batchReq.pMsgs, i);
24,805,465✔
518
    if (req == NULL) {
24,806,076✔
519
      code = terrno;
×
520
      goto _exit;
×
521
    }
522

523
    reqMsg.msgType = req->msgType;
24,806,076✔
524
    reqMsg.pCont = req->msg;
24,806,628✔
525
    reqMsg.contLen = req->msgLen;
24,805,406✔
526

527
    switch (req->msgType) {
24,805,119✔
528
      case TDMT_VND_TABLE_META:
24,334,336✔
529
        // error code has been set into reqMsg, no need to handle it here.
530
        if (TSDB_CODE_SUCCESS != vnodeGetTableMeta(pVnode, &reqMsg, false)) {
24,334,336✔
531
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
1,990,068✔
532
        }
533
        break;
24,333,728✔
534
      case TDMT_VND_TABLE_NAME:
220,698✔
535
        // error code has been set into reqMsg, no need to handle it here.
536
        if (TSDB_CODE_SUCCESS != vnodeGetTableMeta(pVnode, &reqMsg, false)) {
220,698✔
537
          qWarn("vnodeGetBatchName failed, msgType:%d", req->msgType);
85,098✔
538
        }
539
        break;
220,527✔
540
      case TDMT_VND_TABLE_CFG:
48,744✔
541
        // error code has been set into reqMsg, no need to handle it here.
542
        if (TSDB_CODE_SUCCESS != vnodeGetTableCfg(pVnode, &reqMsg, false)) {
48,744✔
543
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
×
544
        }
545
        break;
48,744✔
546
      case TDMT_VND_VSUBTABLES_META:
×
547
        // error code has been set into reqMsg, no need to handle it here.
548
        if (TSDB_CODE_SUCCESS != vnodeGetVSubtablesMeta(pVnode, &reqMsg)) {
×
549
          qWarn("vnodeGetVSubtablesMeta failed, msgType:%d", req->msgType);
×
550
        }
551
        break;
×
552
      case TDMT_VND_VSTB_REF_DBS:
202,295✔
553
        // error code has been set into reqMsg, no need to handle it here.
554
        if (TSDB_CODE_SUCCESS != vnodeGetVStbRefDbs(pVnode, &reqMsg)) {
202,295✔
555
          qWarn("vnodeGetVStbRefDbs failed, msgType:%d", req->msgType);
×
556
        }
557
        break;
201,743✔
558
      default:
×
559
        qError("invalid req msgType %d", req->msgType);
×
560
        reqMsg.code = TSDB_CODE_INVALID_MSG;
×
561
        reqMsg.pCont = NULL;
×
562
        reqMsg.contLen = 0;
×
563
        break;
×
564
    }
565

566
    rsp.msgIdx = req->msgIdx;
24,804,742✔
567
    rsp.reqType = reqMsg.msgType;
24,805,294✔
568
    rsp.msgLen = reqMsg.contLen;
24,805,294✔
569
    rsp.rspCode = reqMsg.code;
24,805,294✔
570
    rsp.msg = reqMsg.pCont;
24,805,294✔
571

572
    if (NULL == taosArrayPush(batchRsp.pRsps, &rsp)) {
49,609,596✔
573
      qError("taosArrayPush failed");
×
574
      code = terrno;
×
575
      goto _exit;
×
576
    }
577
  }
578

579
  rspSize = tSerializeSBatchRsp(NULL, 0, &batchRsp);
20,570,794✔
580
  if (rspSize < 0) {
20,570,846✔
581
    qError("tSerializeSBatchRsp failed");
×
582
    code = terrno;
×
583
    goto _exit;
×
584
  }
585
  pRsp = rpcMallocCont(rspSize);
20,570,846✔
586
  if (pRsp == NULL) {
20,569,578✔
587
    qError("rpcMallocCont %d failed", rspSize);
×
588
    code = terrno;
×
589
    goto _exit;
×
590
  }
591
  if (tSerializeSBatchRsp(pRsp, rspSize, &batchRsp) < 0) {
20,569,578✔
592
    qError("tSerializeSBatchRsp %d failed", rspSize);
×
593
    code = terrno;
×
594
    goto _exit;
×
595
  }
596

597
_exit:
20,571,234✔
598

599
  rspMsg.info = pMsg->info;
20,572,568✔
600
  rspMsg.pCont = pRsp;
20,572,568✔
601
  rspMsg.contLen = rspSize;
20,572,568✔
602
  rspMsg.code = code;
20,572,568✔
603
  rspMsg.msgType = pMsg->msgType;
20,572,568✔
604

605
  if (code) {
20,569,138✔
606
    qError("vnd get batch meta failed cause of %s", tstrerror(code));
×
607
  }
608

609
  taosArrayDestroyEx(batchReq.pMsgs, tFreeSBatchReqMsg);
20,569,138✔
610
  taosArrayDestroyEx(batchRsp.pRsps, tFreeSBatchRspMsg);
20,569,166✔
611

612
  tmsgSendRsp(&rspMsg);
20,566,524✔
613

614
  return code;
20,573,120✔
615
}
616

617
#define VNODE_DO_META_QUERY(pVnode, cmd)                 \
618
  do {                                                   \
619
    (void)taosThreadRwlockRdlock(&(pVnode)->metaRWLock); \
620
    cmd;                                                 \
621
    (void)taosThreadRwlockUnlock(&(pVnode)->metaRWLock); \
622
  } while (0)
623

624
int32_t vnodeReadVSubtables(SReadHandle* pHandle, int64_t suid, SArray** ppRes) {
×
625
  int32_t                    code = TSDB_CODE_SUCCESS;
×
626
  int32_t                    line = 0;
×
627
  SMetaReader                mr = {0};
×
628
  bool                       readerInit = false;
×
629
  SVCTableRefCols*           pTb = NULL;
×
630
  int32_t                    refColsNum = 0;
×
631
  char                       tbFName[TSDB_TABLE_FNAME_LEN];
×
632
  SSHashObj*                 pSrcTbls = NULL;
×
633

634
  SArray *pList = taosArrayInit(10, sizeof(uint64_t));
×
635
  QUERY_CHECK_NULL(pList, code, line, _return, terrno);
×
636

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

639
  size_t num = taosArrayGetSize(pList);
×
640
  *ppRes = taosArrayInit(num, POINTER_BYTES);
×
641
  QUERY_CHECK_NULL(*ppRes, code, line, _return, terrno);
×
642
  pSrcTbls = tSimpleHashInit(10, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
×
643
  QUERY_CHECK_NULL(pSrcTbls, code, line, _return, terrno);
×
644

645
  for (int32_t i = 0; i < num; ++i) {
×
646
    uint64_t* id = taosArrayGet(pList, i);
×
647
    QUERY_CHECK_NULL(id, code, line, _return, terrno);
×
648
    pHandle->api.metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pHandle->api.metaFn);
×
649
    QUERY_CHECK_CODE(pHandle->api.metaReaderFn.getTableEntryByUid(&mr, *id), line, _return);
×
650
    readerInit = true;
×
651

652
    refColsNum = 0;
×
653
    for (int32_t j = 0; j < mr.me.colRef.nCols; j++) {
×
654
      if (mr.me.colRef.pColRef[j].hasRef) {
×
655
        refColsNum++;
×
656
      }
657
    }
658

659
    if (refColsNum <= 0) {
×
660
      pHandle->api.metaReaderFn.clearReader(&mr);
×
661
      readerInit = false;
×
662
      continue;
×
663
    }
664

665
    pTb = taosMemoryCalloc(1, refColsNum * sizeof(SRefColInfo) + sizeof(*pTb));
×
666
    QUERY_CHECK_NULL(pTb, code, line, _return, terrno);
×
667

668
    pTb->uid = mr.me.uid;
×
669
    pTb->numOfColRefs = refColsNum;
×
670
    pTb->refCols = (SRefColInfo*)(pTb + 1);
×
671

672
    refColsNum = 0;
×
673
    tSimpleHashClear(pSrcTbls);
×
674
    for (int32_t j = 0; j < mr.me.colRef.nCols; j++) {
×
675
      if (!mr.me.colRef.pColRef[j].hasRef) {
×
676
        continue;
×
677
      }
678

679
      pTb->refCols[refColsNum].colId = mr.me.colRef.pColRef[j].id;
×
680
      tstrncpy(pTb->refCols[refColsNum].refColName, mr.me.colRef.pColRef[j].refColName, TSDB_COL_NAME_LEN);
×
681
      tstrncpy(pTb->refCols[refColsNum].refTableName, mr.me.colRef.pColRef[j].refTableName, TSDB_TABLE_NAME_LEN);
×
682
      tstrncpy(pTb->refCols[refColsNum].refDbName, mr.me.colRef.pColRef[j].refDbName, TSDB_DB_NAME_LEN);
×
683

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

686
      if (NULL == tSimpleHashGet(pSrcTbls, tbFName, strlen(tbFName))) {
×
687
        QUERY_CHECK_CODE(tSimpleHashPut(pSrcTbls, tbFName, strlen(tbFName), &code, sizeof(code)), line, _return);
×
688
      }
689

690
      refColsNum++;
×
691
    }
692

693
    pTb->numOfSrcTbls = tSimpleHashGetSize(pSrcTbls);
×
694
    QUERY_CHECK_NULL(taosArrayPush(*ppRes, &pTb), code, line, _return, terrno);
×
695
    pTb = NULL;
×
696

697
    pHandle->api.metaReaderFn.clearReader(&mr);
×
698
    readerInit = false;
×
699
  }
700

701
_return:
×
702

703
  if (readerInit) {
×
704
    pHandle->api.metaReaderFn.clearReader(&mr);
×
705
  }
706

707
  taosArrayDestroy(pList);
×
708
  taosMemoryFree(pTb);
×
709
  tSimpleHashCleanup(pSrcTbls);
×
710

711
  if (code) {
×
712
    qError("%s failed since %s", __func__, tstrerror(code));
×
713
  }
714
  return code;
×
715
}
716

717
int32_t vnodeReadVStbRefDbs(SReadHandle* pHandle, int64_t suid, SArray** ppRes) {
202,295✔
718
  int32_t                    code = TSDB_CODE_SUCCESS;
202,295✔
719
  int32_t                    line = 0;
202,295✔
720
  SMetaReader                mr = {0};
202,295✔
721
  bool                       readerInit = false;
202,295✔
722
  SSHashObj*                 pDbNameHash = NULL;
202,295✔
723
  SArray*                    pList = NULL;
202,295✔
724

725
  pList = taosArrayInit(10, sizeof(uint64_t));
202,295✔
726
  QUERY_CHECK_NULL(pList, code, line, _return, terrno);
201,743✔
727

728
  *ppRes = taosArrayInit(10, POINTER_BYTES);
201,743✔
729
  QUERY_CHECK_NULL(*ppRes, code, line, _return, terrno)
202,295✔
730
  
731
  // lookup in cache
732
  code = pHandle->api.metaFn.metaGetCachedRefDbs(pHandle->vnode, suid, *ppRes);
202,847✔
733
  QUERY_CHECK_CODE(code, line, _return);
202,847✔
734

735
  if (taosArrayGetSize(*ppRes) > 0) {
202,847✔
736
    // found in cache
737
    goto _return;
182,567✔
738
  } else {
739
    code = pHandle->api.metaFn.getChildTableList(pHandle->vnode, suid, pList);
20,280✔
740
    QUERY_CHECK_CODE(code, line, _return);
20,280✔
741

742
    size_t num = taosArrayGetSize(pList);
20,280✔
743
    pDbNameHash = tSimpleHashInit(10, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
20,280✔
744
    QUERY_CHECK_NULL(pDbNameHash, code, line, _return, terrno);
20,280✔
745

746
    for (int32_t i = 0; i < num; ++i) {
62,502✔
747
      uint64_t* id = taosArrayGet(pList, i);
42,222✔
748
      QUERY_CHECK_NULL(id, code, line, _return, terrno);
42,222✔
749

750
      pHandle->api.metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pHandle->api.metaFn);
42,222✔
751
      readerInit = true;
42,222✔
752

753
      code = pHandle->api.metaReaderFn.getTableEntryByUid(&mr, *id);
42,222✔
754
      QUERY_CHECK_CODE(code, line, _return);
42,222✔
755

756
      for (int32_t j = 0; j < mr.me.colRef.nCols; j++) {
19,158,310✔
757
        if (mr.me.colRef.pColRef[j].hasRef) {
19,116,088✔
758
          if (NULL == tSimpleHashGet(pDbNameHash, mr.me.colRef.pColRef[j].refDbName, strlen(mr.me.colRef.pColRef[j].refDbName))) {
19,023,490✔
759
            char *refDbName = taosStrdup(mr.me.colRef.pColRef[j].refDbName);
14,158✔
760
            QUERY_CHECK_NULL(refDbName, code, line, _return, terrno);
14,158✔
761

762
            QUERY_CHECK_NULL(taosArrayPush(*ppRes, &refDbName), code, line, _return, terrno);
28,316✔
763

764
            code = tSimpleHashPut(pDbNameHash, refDbName, strlen(refDbName), NULL, 0);
14,158✔
765
            QUERY_CHECK_CODE(code, line, _return);
14,158✔
766
          }
767
        }
768
      }
769

770
      pHandle->api.metaReaderFn.clearReader(&mr);
42,222✔
771
      readerInit = false;
42,222✔
772
    }
773

774
    code = pHandle->api.metaFn.metaPutRefDbsToCache(pHandle->vnode, suid, *ppRes);
20,280✔
775
    QUERY_CHECK_CODE(code, line, _return);
20,280✔
776
  }
777

778
_return:
202,847✔
779

780
  if (readerInit) {
202,847✔
781
    pHandle->api.metaReaderFn.clearReader(&mr);
×
782
  }
783

784
  taosArrayDestroy(pList);
202,847✔
785
  tSimpleHashCleanup(pDbNameHash);
202,847✔
786

787
  if (code) {
202,847✔
788
    qError("%s failed since %s", __func__, tstrerror(code));
×
789
  }
790
  return code;
202,847✔
791
}
792

793
int32_t vnodeGetVSubtablesMeta(SVnode *pVnode, SRpcMsg *pMsg) {
×
794
  int32_t        code = 0;
×
795
  int32_t        rspSize = 0;
×
796
  SVSubTablesReq req = {0};
×
797
  SVSubTablesRsp rsp = {0};
×
798
  SRpcMsg      rspMsg = {0};
×
799
  void        *pRsp = NULL;
×
800
  int32_t      line = 0;
×
801

802
  if (tDeserializeSVSubTablesReq(pMsg->pCont, pMsg->contLen, &req)) {
×
803
    code = terrno;
×
804
    qError("tDeserializeSVSubTablesReq failed");
×
805
    goto _return;
×
806
  }
807

808
  SReadHandle handle = {0};
×
809
  handle.vnode = pVnode;
×
810
  initStorageAPI(&handle.api);
×
811

812
  QUERY_CHECK_CODE(vnodeReadVSubtables(&handle, req.suid, &rsp.pTables), line, _return);
×
813
  rsp.vgId = TD_VID(pVnode);
×
814

815
  rspSize = tSerializeSVSubTablesRsp(NULL, 0, &rsp);
×
816
  if (rspSize < 0) {
×
817
    code = rspSize;
×
818
    qError("tSerializeSVSubTablesRsp failed, error:%d", rspSize);
×
819
    goto _return;
×
820
  }
821
  pRsp = taosMemoryCalloc(1, rspSize);
×
822
  if (pRsp == NULL) {
×
823
    code = terrno;
×
824
    qError("rpcMallocCont %d failed, error:%d", rspSize, terrno);
×
825
    goto _return;
×
826
  }
827
  rspSize = tSerializeSVSubTablesRsp(pRsp, rspSize, &rsp);
×
828
  if (rspSize < 0) {
×
829
    code = rspSize;
×
830
    qError("tSerializeSVSubTablesRsp failed, error:%d", rspSize);
×
831
    goto _return;
×
832
  }
833

834
_return:
×
835

836
  rspMsg.info = pMsg->info;
×
837
  rspMsg.pCont = pRsp;
×
838
  rspMsg.contLen = rspSize;
×
839
  rspMsg.code = code;
×
840
  rspMsg.msgType = pMsg->msgType;
×
841

842
  if (code) {
×
843
    qError("vnd get virtual subtables failed cause of %s", tstrerror(code));
×
844
  }
845

846
  *pMsg = rspMsg;
×
847
  
848
  tDestroySVSubTablesRsp(&rsp);
×
849

850
  //tmsgSendRsp(&rspMsg);
851

852
  return code;
×
853
}
854

855
int32_t vnodeGetVStbRefDbs(SVnode *pVnode, SRpcMsg *pMsg) {
202,295✔
856
  int32_t        code = 0;
202,295✔
857
  int32_t        rspSize = 0;
202,295✔
858
  SVStbRefDbsReq req = {0};
202,295✔
859
  SVStbRefDbsRsp rsp = {0};
201,743✔
860
  SRpcMsg        rspMsg = {0};
202,295✔
861
  void          *pRsp = NULL;
202,295✔
862
  int32_t        line = 0;
202,295✔
863

864
  if (tDeserializeSVStbRefDbsReq(pMsg->pCont, pMsg->contLen, &req)) {
202,295✔
865
    code = terrno;
×
866
    qError("tDeserializeSVSubTablesReq failed");
×
867
    goto _return;
×
868
  }
869

870
  SReadHandle handle = {0};
202,295✔
871
  handle.vnode = pVnode;
202,295✔
872
  initStorageAPI(&handle.api);
202,295✔
873

874
  code = vnodeReadVStbRefDbs(&handle, req.suid, &rsp.pDbs);
202,295✔
875
  QUERY_CHECK_CODE(code, line, _return);
202,847✔
876
  rsp.vgId = TD_VID(pVnode);
202,847✔
877

878
  rspSize = tSerializeSVStbRefDbsRsp(NULL, 0, &rsp);
202,847✔
879
  if (rspSize < 0) {
202,295✔
880
    code = rspSize;
×
881
    qError("tSerializeSVStbRefDbsRsp failed, error:%d", rspSize);
×
882
    goto _return;
×
883
  }
884
  pRsp = taosMemoryCalloc(1, rspSize);
202,295✔
885
  if (pRsp == NULL) {
202,847✔
886
    code = terrno;
×
887
    qError("rpcMallocCont %d failed, error:%d", rspSize, terrno);
×
888
    goto _return;
×
889
  }
890
  rspSize = tSerializeSVStbRefDbsRsp(pRsp, rspSize, &rsp);
202,847✔
891
  if (rspSize < 0) {
202,295✔
892
    code = rspSize;
×
893
    qError("tSerializeSVStbRefDbsRsp failed, error:%d", rspSize);
×
894
    goto _return;
×
895
  }
896

897
_return:
202,295✔
898

899
  rspMsg.info = pMsg->info;
202,295✔
900
  rspMsg.pCont = pRsp;
202,295✔
901
  rspMsg.contLen = rspSize;
202,295✔
902
  rspMsg.code = code;
202,295✔
903
  rspMsg.msgType = pMsg->msgType;
202,295✔
904

905
  if (code) {
202,847✔
906
    qError("vnd get virtual stb ref db failed cause of %s", tstrerror(code));
×
907
  }
908

909
  *pMsg = rspMsg;
202,847✔
910

911
  tDestroySVStbRefDbsRsp(&rsp);
202,295✔
912

913
  return code;
201,743✔
914
}
915

916
static int32_t vnodeGetCompStorage(SVnode *pVnode, int64_t *output) {
118,481,751✔
917
  int32_t code = 0;
118,481,751✔
918
#ifdef TD_ENTERPRISE
919
  int32_t now = taosGetTimestampSec();
118,481,751✔
920
  if (llabs(now - pVnode->config.vndStats.storageLastUpd) >= 30) {
118,481,751✔
921
    pVnode->config.vndStats.storageLastUpd = now;
6,154,837✔
922

923
    SDbSizeStatisInfo info = {0};
6,154,837✔
924
    if (0 == (code = vnodeGetDBSize(pVnode, &info))) {
6,154,837✔
925
      int64_t compSize =
6,154,837✔
926
          info.l1Size + info.l2Size + info.l3Size + info.cacheSize + info.walSize + info.metaSize + +info.ssSize;
6,154,837✔
927
      if (compSize >= 0) {
6,154,837✔
928
        pVnode->config.vndStats.compStorage = compSize;
6,154,837✔
929
      } else {
930
        vError("vnode get comp storage failed since compSize is negative:%" PRIi64, compSize);
×
931
        code = TSDB_CODE_APP_ERROR;
×
932
      }
933
    } else {
934
      vWarn("vnode get comp storage failed since %s", tstrerror(code));
×
935
    }
936
  }
937
  if (output) *output = pVnode->config.vndStats.compStorage;
118,481,751✔
938
#endif
939
  return code;
118,481,751✔
940
}
941

942
static void vnodeGetBufferInfo(SVnode *pVnode, int64_t *bufferSegmentUsed, int64_t *bufferSegmentSize) {
118,481,751✔
943
  *bufferSegmentUsed = 0;
118,481,751✔
944
  *bufferSegmentSize = 0;
118,481,751✔
945
  if (pVnode) {
118,481,751✔
946
    (void)taosThreadMutexLock(&pVnode->mutex);
118,481,751✔
947

948
    if (pVnode->inUse) {
118,481,751✔
949
      *bufferSegmentUsed = pVnode->inUse->size;
118,452,565✔
950
    }
951
    *bufferSegmentSize = pVnode->config.szBuf / VNODE_BUFPOOL_SEGMENTS;
118,481,751✔
952

953
    (void)taosThreadMutexUnlock(&pVnode->mutex);
118,481,751✔
954
  }
955
}
118,481,751✔
956

957
int32_t vnodeGetLoad(SVnode *pVnode, SVnodeLoad *pLoad) {
118,481,751✔
958
  SSyncState state = syncGetState(pVnode->sync);
118,481,751✔
959
  pLoad->syncAppliedIndex = pVnode->state.applied;
118,481,751✔
960
  syncGetCommitIndex(pVnode->sync, &pLoad->syncCommitIndex);
118,481,751✔
961

962
  pLoad->vgId = TD_VID(pVnode);
118,481,751✔
963
  pLoad->syncState = state.state;
118,481,751✔
964
  pLoad->syncRestore = state.restored;
118,481,751✔
965
  pLoad->syncTerm = state.term;
118,481,751✔
966
  pLoad->roleTimeMs = state.roleTimeMs;
118,481,751✔
967
  pLoad->startTimeMs = state.startTimeMs;
118,481,751✔
968
  pLoad->syncCanRead = state.canRead;
118,481,751✔
969
  pLoad->learnerProgress = state.progress;
118,481,751✔
970
  pLoad->cacheUsage = tsdbCacheGetUsage(pVnode);
118,481,751✔
971
  pLoad->numOfCachedTables = tsdbCacheGetElems(pVnode);
118,481,751✔
972
  VNODE_DO_META_QUERY(pVnode, pLoad->numOfTables = metaGetTbNum(pVnode->pMeta));
118,481,751✔
973
  VNODE_DO_META_QUERY(pVnode, pLoad->numOfTimeSeries = metaGetTimeSeriesNum(pVnode->pMeta, 1));
118,481,751✔
974
  pLoad->totalStorage = (int64_t)3 * 1073741824;  // TODO
118,481,751✔
975
  (void)vnodeGetCompStorage(pVnode, &pLoad->compStorage);
118,481,751✔
976
  pLoad->pointsWritten = 100;
118,481,751✔
977
  pLoad->numOfSelectReqs = 1;
118,481,751✔
978
  pLoad->numOfInsertReqs = atomic_load_64(&pVnode->statis.nInsert);
118,481,751✔
979
  pLoad->numOfInsertSuccessReqs = atomic_load_64(&pVnode->statis.nInsertSuccess);
118,481,751✔
980
  pLoad->numOfBatchInsertReqs = atomic_load_64(&pVnode->statis.nBatchInsert);
118,481,751✔
981
  pLoad->numOfBatchInsertSuccessReqs = atomic_load_64(&pVnode->statis.nBatchInsertSuccess);
118,481,751✔
982
  vnodeGetBufferInfo(pVnode, &pLoad->bufferSegmentUsed, &pLoad->bufferSegmentSize);
118,481,751✔
983
  return 0;
118,481,751✔
984
}
985

986
int32_t vnodeGetLoadLite(SVnode *pVnode, SVnodeLoadLite *pLoad) {
×
987
  SSyncState syncState = syncGetState(pVnode->sync);
×
988
  if (syncState.state == TAOS_SYNC_STATE_LEADER || syncState.state == TAOS_SYNC_STATE_ASSIGNED_LEADER) {
×
989
    pLoad->vgId = TD_VID(pVnode);
×
990
    pLoad->nTimeSeries = metaGetTimeSeriesNum(pVnode->pMeta, 1);
×
991
    return 0;
×
992
  }
993
  return -1;
×
994
}
995
/**
996
 * @brief Reset the statistics value by monitor interval
997
 *
998
 * @param pVnode
999
 * @param pLoad
1000
 */
1001
void vnodeResetLoad(SVnode *pVnode, SVnodeLoad *pLoad) {
330✔
1002
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nInsert, pLoad->numOfInsertReqs, 64, "nInsert");
330✔
1003
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nInsertSuccess, pLoad->numOfInsertSuccessReqs, 64, "nInsertSuccess");
330✔
1004
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nBatchInsert, pLoad->numOfBatchInsertReqs, 64, "nBatchInsert");
330✔
1005
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nBatchInsertSuccess, pLoad->numOfBatchInsertSuccessReqs, 64,
330✔
1006
                            "nBatchInsertSuccess");
1007
}
330✔
1008

1009
void vnodeGetInfo(void *pVnode, const char **dbname, int32_t *vgId, int64_t *numOfTables, int64_t *numOfNormalTables) {
6,661,318✔
1010
  SVnode    *pVnodeObj = pVnode;
6,661,318✔
1011
  SVnodeCfg *pConf = &pVnodeObj->config;
6,661,318✔
1012

1013
  if (dbname) {
6,662,572✔
1014
    *dbname = pConf->dbname;
6,647,380✔
1015
  }
1016

1017
  if (vgId) {
6,662,411✔
1018
    *vgId = TD_VID(pVnodeObj);
6,072,323✔
1019
  }
1020

1021
  if (numOfTables) {
6,661,936✔
1022
    *numOfTables = pConf->vndStats.numOfNTables + pConf->vndStats.numOfCTables +
17,041✔
1023
                   pConf->vndStats.numOfVTables + pConf->vndStats.numOfVCTables;
8,616✔
1024
  }
1025

1026
  if (numOfNormalTables) {
6,662,067✔
1027
    *numOfNormalTables = pConf->vndStats.numOfNTables +
13,152✔
1028
                         pConf->vndStats.numOfVTables;
6,576✔
1029
  }
1030
}
6,662,067✔
1031

1032
int32_t vnodeGetTableList(void *pVnode, int8_t type, SArray *pList) {
6,576✔
1033
  if (type == TSDB_SUPER_TABLE) {
6,576✔
1034
    return vnodeGetStbIdList(pVnode, 0, pList);
6,576✔
1035
  } else {
1036
    return TSDB_CODE_INVALID_PARA;
×
1037
  }
1038
}
1039

1040
int32_t vnodeGetAllTableList(SVnode *pVnode, uint64_t uid, SArray *list) {
×
1041
  int32_t      code = TSDB_CODE_SUCCESS;
×
1042
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, uid, 1);
×
1043
  if (NULL == pCur) {
×
1044
    qError("vnode get all table list failed");
×
1045
    return terrno;
×
1046
  }
1047

1048
  while (1) {
×
1049
    tb_uid_t id = metaCtbCursorNext(pCur);
×
1050
    if (id == 0) {
×
1051
      break;
×
1052
    }
1053

1054
    STableKeyInfo info = {uid = id};
×
1055
    if (NULL == taosArrayPush(list, &info)) {
×
1056
      qError("taosArrayPush failed");
×
1057
      code = terrno;
×
1058
      goto _exit;
×
1059
    }
1060
  }
1061
_exit:
×
1062
  metaCloseCtbCursor(pCur);
×
1063
  return code;
×
1064
}
1065

1066
int32_t vnodeGetCtbIdListByFilter(SVnode *pVnode, int64_t suid, SArray *list, bool (*filter)(void *arg), void *arg) {
×
1067
  return 0;
×
1068
}
1069

1070
int32_t vnodeGetCtbIdList(void *pVnode, int64_t suid, SArray *list) {
108,923,353✔
1071
  int32_t      code = TSDB_CODE_SUCCESS;
108,923,353✔
1072
  SVnode      *pVnodeObj = pVnode;
108,923,353✔
1073
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnodeObj, suid, 1);
108,923,353✔
1074
  if (NULL == pCur) {
108,936,902✔
1075
    qError("vnode get all table list failed");
×
1076
    return terrno;
×
1077
  }
1078

1079
  while (1) {
428,502,272✔
1080
    tb_uid_t id = metaCtbCursorNext(pCur);
537,439,174✔
1081
    if (id == 0) {
537,626,973✔
1082
      break;
109,232,524✔
1083
    }
1084
    qTrace("vnodeGetCtbIdList: got ctb id %" PRId64 " for suid %" PRId64, id, suid);
428,394,449✔
1085
    if (NULL == taosArrayPush(list, &id)) {
428,325,862✔
1086
      qError("taosArrayPush failed");
×
1087
      code = terrno;
×
1088
      goto _exit;
×
1089
    }
1090
  }
1091

1092
_exit:
109,025,980✔
1093
  metaCloseCtbCursor(pCur);
109,025,980✔
1094
  return code;
108,999,814✔
1095
}
1096

1097
int32_t vnodeGetStbIdList(SVnode *pVnode, int64_t suid, SArray *list) {
68,438,637✔
1098
  int32_t      code = TSDB_CODE_SUCCESS;
68,438,637✔
1099
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, suid);
68,438,637✔
1100
  if (!pCur) {
68,442,900✔
1101
    return TSDB_CODE_OUT_OF_MEMORY;
×
1102
  }
1103

1104
  while (1) {
48,095,279✔
1105
    tb_uid_t id = metaStbCursorNext(pCur);
116,538,179✔
1106
    if (id == 0) {
116,530,970✔
1107
      break;
68,447,068✔
1108
    }
1109

1110
    if (NULL == taosArrayPush(list, &id)) {
48,089,250✔
1111
      qError("taosArrayPush failed");
×
1112
      code = terrno;
×
1113
      goto _exit;
×
1114
    }
1115
  }
1116

1117
_exit:
68,441,073✔
1118
  metaCloseStbCursor(pCur);
68,441,073✔
1119
  return code;
68,441,298✔
1120
}
1121

1122
int32_t vnodeGetStbIdListByFilter(SVnode *pVnode, int64_t suid, SArray *list, bool (*filter)(void *arg, void *arg1),
×
1123
                                  void *arg) {
1124
  int32_t      code = TSDB_CODE_SUCCESS;
×
1125
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, suid);
×
1126
  if (!pCur) {
×
1127
    return terrno;
×
1128
  }
1129

1130
  while (1) {
×
1131
    tb_uid_t id = metaStbCursorNext(pCur);
×
1132
    if (id == 0) {
×
1133
      break;
×
1134
    }
1135

1136
    if ((*filter) && (*filter)(arg, &id)) {
×
1137
      continue;
×
1138
    }
1139

1140
    if (NULL == taosArrayPush(list, &id)) {
×
1141
      qError("taosArrayPush failed");
×
1142
      code = terrno;
×
1143
      goto _exit;
×
1144
    }
1145
  }
1146

1147
_exit:
×
1148
  metaCloseStbCursor(pCur);
×
1149
  return code;
×
1150
}
1151

1152
int32_t vnodeGetCtbNum(SVnode *pVnode, int64_t suid, int64_t *num) {
4,048,895✔
1153
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, suid, 0);
4,048,895✔
1154
  if (!pCur) {
4,050,862✔
1155
    return terrno;
×
1156
  }
1157

1158
  *num = 0;
4,050,862✔
1159
  while (1) {
2,303,227✔
1160
    tb_uid_t id = metaCtbCursorNext(pCur);
6,354,376✔
1161
    if (id == 0) {
6,352,718✔
1162
      break;
4,050,030✔
1163
    }
1164

1165
    ++(*num);
2,302,688✔
1166
  }
1167

1168
  metaCloseCtbCursor(pCur);
4,050,030✔
1169
  return TSDB_CODE_SUCCESS;
4,051,986✔
1170
}
1171

1172
int32_t vnodeGetStbColumnNum(SVnode *pVnode, tb_uid_t suid, int *num) {
4,051,688✔
1173
  SSchemaWrapper *pSW = metaGetTableSchema(pVnode->pMeta, suid, -1, 0, NULL, 0);
4,051,688✔
1174
  if (pSW) {
4,051,482✔
1175
    *num = pSW->nCols;
4,051,238✔
1176
    tDeleteSchemaWrapper(pSW);
1177
  } else {
1178
    *num = 2;
244✔
1179
  }
1180

1181
  return TSDB_CODE_SUCCESS;
4,050,406✔
1182
}
1183

1184
int32_t vnodeGetStbInfo(SVnode *pVnode, tb_uid_t suid, int64_t *keep, int8_t *flags) {
4,050,863✔
1185
  SMetaReader mr = {0};
4,050,863✔
1186
  metaReaderDoInit(&mr, pVnode->pMeta, META_READER_NOLOCK);
4,050,863✔
1187

1188
  int32_t code = metaReaderGetTableEntryByUid(&mr, suid);
4,050,659✔
1189
  if (code == TSDB_CODE_SUCCESS) {
4,051,655✔
1190
    if (keep) *keep = mr.me.stbEntry.keep;
4,051,411✔
1191
    if (flags) *flags = mr.me.flags;
4,051,411✔
1192
  } else {
1193
    if (keep) *keep = 0;
244✔
1194
    if (flags) *flags = 0;
244✔
1195
  }
1196

1197
  metaReaderClear(&mr);
4,051,655✔
1198
  return TSDB_CODE_SUCCESS;
4,051,655✔
1199
}
1200

1201
#ifdef TD_ENTERPRISE
1202
const char *tkLogStb[] = {"cluster_info",
1203
                          "data_dir",
1204
                          "dnodes_info",
1205
                          "d_info",
1206
                          "grants_info",
1207
                          "keeper_monitor",
1208
                          "logs",
1209
                          "log_dir",
1210
                          "log_summary",
1211
                          "m_info",
1212
                          "taosadapter_restful_http_request_fail",
1213
                          "taosadapter_restful_http_request_in_flight",
1214
                          "taosadapter_restful_http_request_summary_milliseconds",
1215
                          "taosadapter_restful_http_request_total",
1216
                          "taosadapter_system_cpu_percent",
1217
                          "taosadapter_system_mem_percent",
1218
                          "temp_dir",
1219
                          "vgroups_info",
1220
                          "vnodes_role"};
1221
const char *tkAuditStb[] = {"operations"};
1222
const int   tkLogStbNum = ARRAY_SIZE(tkLogStb);
1223
const int   tkAuditStbNum = ARRAY_SIZE(tkAuditStb);
1224

1225
// exclude stbs of taoskeeper log
1226
static int32_t vnodeGetTimeSeriesBlackList(SVnode *pVnode, int32_t *tbSize) {
68,433,351✔
1227
  int32_t      code = TSDB_CODE_SUCCESS;
68,433,351✔
1228
  int32_t      tbNum = 0;
68,433,351✔
1229
  const char **pTbArr = NULL;
68,433,351✔
1230
  const char  *dbName = NULL;
68,433,351✔
1231
  *tbSize = 0;
68,433,351✔
1232

1233
  if (!(dbName = strchr(pVnode->config.dbname, '.'))) return 0;
68,437,672✔
1234
  if (0 == strncmp(++dbName, "log", TSDB_DB_NAME_LEN)) {
68,431,637✔
1235
    tbNum = tkLogStbNum;
×
1236
    pTbArr = (const char **)&tkLogStb;
×
1237
  } else if (0 == strncmp(dbName, "audit", TSDB_DB_NAME_LEN)) {
68,432,246✔
1238
    tbNum = tkAuditStbNum;
×
1239
    pTbArr = (const char **)&tkAuditStb;
×
1240
  }
1241
  if (tbNum && pTbArr) {
68,432,246✔
1242
    *tbSize = metaSizeOfTbFilterCache(pVnode->pMeta, 0);
×
1243
    if (*tbSize < tbNum) {
×
1244
      for (int32_t i = 0; i < tbNum; ++i) {
×
1245
        tb_uid_t suid = metaGetTableEntryUidByName(pVnode->pMeta, pTbArr[i]);
×
1246
        if (suid != 0) {
×
1247
          code = metaPutTbToFilterCache(pVnode->pMeta, &suid, 0);
×
1248
          if (TSDB_CODE_SUCCESS != code) {
×
1249
            return code;
×
1250
          }
1251
        }
1252
      }
1253
      *tbSize = metaSizeOfTbFilterCache(pVnode->pMeta, 0);
×
1254
    }
1255
  }
1256

1257
  return code;
68,432,803✔
1258
}
1259
#endif
1260

1261
static bool vnodeTimeSeriesFilter(void *arg1, void *arg2) {
×
1262
  SVnode *pVnode = (SVnode *)arg1;
×
1263

1264
  if (metaTbInFilterCache(pVnode->pMeta, arg2, 0)) {
×
1265
    return true;
×
1266
  }
1267
  return false;
×
1268
}
1269

1270
int32_t vnodeGetTimeSeriesNum(SVnode *pVnode, int64_t *num) {
68,433,960✔
1271
  SArray *suidList = NULL;
68,433,960✔
1272

1273
  if (!(suidList = taosArrayInit(1, sizeof(tb_uid_t)))) {
68,433,960✔
1274
    return terrno;
×
1275
  }
1276

1277
  int32_t tbFilterSize = 0;
68,435,787✔
1278
  int32_t code = TSDB_CODE_SUCCESS;
68,432,133✔
1279
#ifdef TD_ENTERPRISE
1280
  code = vnodeGetTimeSeriesBlackList(pVnode, &tbFilterSize);
68,432,133✔
1281
  if (TSDB_CODE_SUCCESS != code) {
68,432,252✔
1282
    goto _exit;
×
1283
  }
1284
#endif
1285

1286
  if ((!tbFilterSize && vnodeGetStbIdList(pVnode, 0, suidList) < 0) ||
68,432,252✔
1287
      (tbFilterSize && vnodeGetStbIdListByFilter(pVnode, 0, suidList, vnodeTimeSeriesFilter, pVnode) < 0)) {
68,427,076✔
1288
    qError("vgId:%d, failed to get stb id list error: %s", TD_VID(pVnode), terrstr());
×
1289
    taosArrayDestroy(suidList);
×
1290
    return terrno;
×
1291
  }
1292

1293
  *num = 0;
68,431,259✔
1294
  int64_t arrSize = taosArrayGetSize(suidList);
68,432,861✔
1295
  for (int64_t i = 0; i < arrSize; ++i) {
116,524,380✔
1296
    tb_uid_t suid = *(tb_uid_t *)taosArrayGet(suidList, i);
48,084,559✔
1297

1298
    int64_t ctbNum = 0;
48,085,774✔
1299
    int32_t numOfCols = 0;
48,088,761✔
1300
    int8_t  flags = 0;
48,087,604✔
1301
    code = metaGetStbStats(pVnode, suid, &ctbNum, &numOfCols, &flags);
48,092,153✔
1302
    if (TSDB_CODE_SUCCESS != code) {
48,091,806✔
1303
      goto _exit;
×
1304
    }
1305
    if (!TABLE_IS_VIRTUAL(flags)) {
48,091,806✔
1306
      *num += ctbNum * (numOfCols - 1);
46,838,200✔
1307
    }
1308
  }
1309

1310
_exit:
68,438,603✔
1311
  taosArrayDestroy(suidList);
68,439,821✔
1312
  return TSDB_CODE_SUCCESS;
68,431,295✔
1313
}
1314

1315
int32_t vnodeGetAllCtbNum(SVnode *pVnode, int64_t *num) {
×
1316
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, 0);
×
1317
  if (!pCur) {
×
1318
    return terrno;
×
1319
  }
1320

1321
  *num = 0;
×
1322
  while (1) {
×
1323
    tb_uid_t id = metaStbCursorNext(pCur);
×
1324
    if (id == 0) {
×
1325
      break;
×
1326
    }
1327

1328
    int64_t ctbNum = 0;
×
1329
    int32_t code = vnodeGetCtbNum(pVnode, id, &ctbNum);
×
1330
    if (TSDB_CODE_SUCCESS != code) {
×
1331
      metaCloseStbCursor(pCur);
×
1332
      return code;
×
1333
    }
1334

1335
    *num += ctbNum;
×
1336
  }
1337

1338
  metaCloseStbCursor(pCur);
×
1339
  return TSDB_CODE_SUCCESS;
×
1340
}
1341

1342
void *vnodeGetIdx(void *pVnode) {
3,787,903✔
1343
  if (pVnode == NULL) {
3,787,903✔
1344
    return NULL;
×
1345
  }
1346

1347
  return metaGetIdx(((SVnode *)pVnode)->pMeta);
3,787,903✔
1348
}
1349

1350
void *vnodeGetIvtIdx(void *pVnode) {
3,787,903✔
1351
  if (pVnode == NULL) {
3,787,903✔
1352
    return NULL;
×
1353
  }
1354
  return metaGetIvtIdx(((SVnode *)pVnode)->pMeta);
3,787,903✔
1355
}
1356

1357
int32_t vnodeGetTableSchema(void *pVnode, int64_t uid, STSchema **pSchema, int64_t *suid, SSchemaWrapper **pTagSchema) {
78,254✔
1358
  return tsdbGetTableSchema(((SVnode *)pVnode)->pMeta, uid, pSchema, suid, pTagSchema);
78,254✔
1359
}
1360

1361
static FORCE_INLINE int32_t vnodeGetDBPrimaryInfo(SVnode *pVnode, SDbSizeStatisInfo *pInfo) {
1362
  int32_t code = 0;
6,160,287✔
1363
  char    path[TSDB_FILENAME_LEN] = {0};
6,160,287✔
1364

1365
  char   *dirName[] = {VNODE_TSDB_DIR, VNODE_WAL_DIR, VNODE_META_DIR, VNODE_TSDB_CACHE_DIR};
6,160,287✔
1366
  int64_t dirSize[4];
6,159,507✔
1367

1368
  vnodeGetPrimaryPath(pVnode, false, path, TSDB_FILENAME_LEN);
6,160,287✔
1369
  int32_t offset = strlen(path);
6,160,287✔
1370

1371
  for (int i = 0; i < sizeof(dirName) / sizeof(dirName[0]); i++) {
30,801,435✔
1372
    int64_t size = {0};
24,641,148✔
1373
    (void)snprintf(path + offset, TSDB_FILENAME_LEN - offset, "%s%s", TD_DIRSEP, dirName[i]);
24,640,988✔
1374
    code = taosGetDirSize(path, &size);
24,641,148✔
1375
    if (code != 0) {
24,641,148✔
1376
      uWarn("vnode %d get dir %s %s size failed since %s", TD_VID(pVnode), path, dirName[i], tstrerror(code));
1,194✔
1377
    }
1378
    path[offset] = 0;
24,641,148✔
1379
    dirSize[i] = size;
24,641,148✔
1380
  }
1381

1382
  pInfo->l1Size = 0;
6,160,287✔
1383
  pInfo->walSize = dirSize[1];
6,160,287✔
1384
  pInfo->metaSize = dirSize[2];
6,160,287✔
1385
  pInfo->cacheSize = dirSize[3];
6,160,287✔
1386
  return code;
6,160,287✔
1387
}
1388
int32_t vnodeGetDBSize(void *pVnode, SDbSizeStatisInfo *pInfo) {
6,160,287✔
1389
  int32_t code = 0;
6,160,287✔
1390
  int32_t lino = 0;
6,160,287✔
1391
  SVnode *pVnodeObj = pVnode;
6,160,287✔
1392
  if (pVnodeObj == NULL) {
6,160,287✔
1393
    return TSDB_CODE_VND_NOT_EXIST;
×
1394
  }
1395
  code = vnodeGetDBPrimaryInfo(pVnode, pInfo);
6,160,287✔
1396
  if (code != 0) goto _exit;
6,160,287✔
1397

1398
  code = tsdbGetFsSize(pVnodeObj->pTsdb, pInfo);
6,160,287✔
1399
_exit:
6,160,287✔
1400
  return code;
6,160,287✔
1401
}
1402

1403
/*
1404
 * Get raw write metrics for a vnode
1405
 */
1406
int32_t vnodeGetRawWriteMetrics(void *pVnode, SRawWriteMetrics *pRawMetrics) {
×
1407
  if (pVnode == NULL || pRawMetrics == NULL) {
×
1408
    return TSDB_CODE_INVALID_PARA;
×
1409
  }
1410

1411
  SVnode      *pVnode1 = (SVnode *)pVnode;
×
1412
  SSyncMetrics syncMetrics = syncGetMetrics(pVnode1->sync);
×
1413

1414
  // Copy values following SRawWriteMetrics structure order
1415
  pRawMetrics->total_requests = atomic_load_64(&pVnode1->writeMetrics.total_requests);
×
1416
  pRawMetrics->total_rows = atomic_load_64(&pVnode1->writeMetrics.total_rows);
×
1417
  pRawMetrics->total_bytes = atomic_load_64(&pVnode1->writeMetrics.total_bytes);
×
1418
  pRawMetrics->fetch_batch_meta_time = atomic_load_64(&pVnode1->writeMetrics.fetch_batch_meta_time);
×
1419
  pRawMetrics->fetch_batch_meta_count = atomic_load_64(&pVnode1->writeMetrics.fetch_batch_meta_count);
×
1420
  pRawMetrics->preprocess_time = atomic_load_64(&pVnode1->writeMetrics.preprocess_time);
×
1421
  pRawMetrics->wal_write_bytes = atomic_load_64(&syncMetrics.wal_write_bytes);
×
1422
  pRawMetrics->wal_write_time = atomic_load_64(&syncMetrics.wal_write_time);
×
1423
  pRawMetrics->apply_bytes = atomic_load_64(&pVnode1->writeMetrics.apply_bytes);
×
1424
  pRawMetrics->apply_time = atomic_load_64(&pVnode1->writeMetrics.apply_time);
×
1425
  pRawMetrics->commit_count = atomic_load_64(&pVnode1->writeMetrics.commit_count);
×
1426
  pRawMetrics->commit_time = atomic_load_64(&pVnode1->writeMetrics.commit_time);
×
1427
  pRawMetrics->memtable_wait_time = atomic_load_64(&pVnode1->writeMetrics.memtable_wait_time);
×
1428
  pRawMetrics->blocked_commit_count = atomic_load_64(&pVnode1->writeMetrics.blocked_commit_count);
×
1429
  pRawMetrics->blocked_commit_time = atomic_load_64(&pVnode1->writeMetrics.block_commit_time);
×
1430
  pRawMetrics->merge_count = atomic_load_64(&pVnode1->writeMetrics.merge_count);
×
1431
  pRawMetrics->merge_time = atomic_load_64(&pVnode1->writeMetrics.merge_time);
×
1432
  pRawMetrics->last_cache_commit_time = atomic_load_64(&pVnode1->writeMetrics.last_cache_commit_time);
×
1433
  pRawMetrics->last_cache_commit_count = atomic_load_64(&pVnode1->writeMetrics.last_cache_commit_count);
×
1434

1435
  return 0;
×
1436
}
1437

1438
/*
1439
 * Reset raw write metrics for a vnode by subtracting old values
1440
 */
1441
int32_t vnodeResetRawWriteMetrics(void *pVnode, const SRawWriteMetrics *pOldMetrics) {
×
1442
  if (pVnode == NULL || pOldMetrics == NULL) {
×
1443
    return TSDB_CODE_INVALID_PARA;
×
1444
  }
1445

1446
  SVnode *pVnode1 = (SVnode *)pVnode;
×
1447

1448
  // Reset vnode write metrics using atomic operations to subtract old values
1449
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.total_requests, pOldMetrics->total_requests);
×
1450
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.total_rows, pOldMetrics->total_rows);
×
1451
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.total_bytes, pOldMetrics->total_bytes);
×
1452

1453
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.fetch_batch_meta_time, pOldMetrics->fetch_batch_meta_time);
×
1454
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.fetch_batch_meta_count, pOldMetrics->fetch_batch_meta_count);
×
1455
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.preprocess_time, pOldMetrics->preprocess_time);
×
1456
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.apply_bytes, pOldMetrics->apply_bytes);
×
1457
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.apply_time, pOldMetrics->apply_time);
×
1458
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.commit_count, pOldMetrics->commit_count);
×
1459

1460
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.commit_time, pOldMetrics->commit_time);
×
1461
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.merge_time, pOldMetrics->merge_time);
×
1462

1463
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.memtable_wait_time, pOldMetrics->memtable_wait_time);
×
1464
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.blocked_commit_count, pOldMetrics->blocked_commit_count);
×
1465
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.block_commit_time, pOldMetrics->blocked_commit_time);
×
1466
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.merge_count, pOldMetrics->merge_count);
×
1467

1468
  // Reset new cache metrics
1469
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.last_cache_commit_time, pOldMetrics->last_cache_commit_time);
×
1470
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.last_cache_commit_count, pOldMetrics->last_cache_commit_count);
×
1471

1472
  // Reset sync metrics
1473
  SSyncMetrics syncMetrics = {
×
1474
      .wal_write_bytes = pOldMetrics->wal_write_bytes,
×
1475
      .wal_write_time = pOldMetrics->wal_write_time,
×
1476
  };
1477
  syncResetMetrics(pVnode1->sync, &syncMetrics);
×
1478

1479
  return 0;
×
1480
}
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