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

taosdata / TDengine / #4890

19 Dec 2025 11:37AM UTC coverage: 62.824% (-2.7%) from 65.487%
#4890

push

travis-ci

web-flow
feat: support TOTP code login and password expired tip (#33969)

22 of 26 new or added lines in 3 files covered. (84.62%)

1989 existing lines in 120 files now uncovered.

63068 of 100389 relevant lines covered (62.82%)

286770988.88 hits per line

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

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

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

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

36
int32_t fillTableColCmpr(SMetaReader *reader, SSchemaExt *pExt, int32_t numOfCol) {
20,161,849✔
37
  int8_t tblType = reader->me.type;
20,161,849✔
38
  if (withExtSchema(tblType)) {
20,161,849✔
39
    SColCmprWrapper *p = &(reader->me.colCmpr);
19,966,333✔
40
    if (numOfCol != p->nCols) {
19,966,333✔
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++) {
238,053,980✔
45
      SColCmpr *pCmpr = &p->pColCmpr[i];
218,088,120✔
46
      pExt[i].colId = pCmpr->id;
218,086,450✔
47
      pExt[i].compress = pCmpr->alg;
218,089,056✔
48
    }
49
  }
50
  return 0;
20,161,849✔
51
}
52

53
void vnodePrintTableMeta(STableMetaRsp *pMeta) {
20,161,120✔
54
  if (!(qDebugFlag & DEBUG_DEBUG)) {
20,161,120✔
55
    return;
109,512✔
56
  }
57

58
  qDebug("tbName:%s", pMeta->tbName);
20,051,608✔
59
  qDebug("stbName:%s", pMeta->stbName);
20,052,337✔
60
  qDebug("dbFName:%s", pMeta->dbFName);
20,052,337✔
61
  qDebug("dbId:%" PRId64, pMeta->dbId);
20,052,337✔
62
  qDebug("numOfTags:%d", pMeta->numOfTags);
20,052,337✔
63
  qDebug("numOfColumns:%d", pMeta->numOfColumns);
20,052,337✔
64
  qDebug("precision:%d", pMeta->precision);
20,052,337✔
65
  qDebug("tableType:%d", pMeta->tableType);
20,052,337✔
66
  qDebug("sversion:%d", pMeta->sversion);
20,052,337✔
67
  qDebug("tversion:%d", pMeta->tversion);
20,052,337✔
68
  qDebug("suid:%" PRIu64, pMeta->suid);
20,052,337✔
69
  qDebug("tuid:%" PRIu64, pMeta->tuid);
20,052,337✔
70
  qDebug("vgId:%d", pMeta->vgId);
20,052,337✔
71
  qDebug("sysInfo:%d", pMeta->sysInfo);
20,052,337✔
72
  if (pMeta->pSchemas) {
20,052,337✔
73
    for (int32_t i = 0; i < (pMeta->numOfColumns + pMeta->numOfTags); ++i) {
278,786,348✔
74
      SSchema *pSchema = pMeta->pSchemas + i;
258,734,011✔
75
      qDebug("%d col/tag: type:%d, flags:%d, colId:%d, bytes:%d, name:%s", i, pSchema->type, pSchema->flags,
258,734,011✔
76
             pSchema->colId, pSchema->bytes, pSchema->name);
77
    }
78
  }
79
}
80

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

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

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

125
  if (infoReq.option == REQ_OPT_TBUID) reqTbUid = true;
32,274,999✔
126
  metaRsp.dbId = pVnode->config.dbId;
32,274,999✔
127
  tstrncpy(metaRsp.tbName, infoReq.tbName, TSDB_TABLE_NAME_LEN);
32,275,470✔
128
  (void)memcpy(metaRsp.dbFName, infoReq.dbFName, sizeof(metaRsp.dbFName));
32,275,470✔
129

130
  if (!reqTbUid) {
32,275,470✔
131
    (void)tsnprintf(tableFName, TSDB_TABLE_FNAME_LEN, "%s.%s", infoReq.dbFName, infoReq.tbName);
31,692,833✔
132
    if (pVnode->mounted) tTrimMountPrefix(tableFName);
31,693,243✔
133
    code = vnodeValidateTableHash(pVnode, tableFName);
31,693,243✔
134
    if (code) {
31,692,772✔
135
      goto _exit4;
×
136
    }
137
  }
138

139
  // query meta
140
  metaReaderDoInit(&mer1, pVnode->pMeta, META_READER_LOCK);
32,275,409✔
141
  if (reqTbUid) {
32,275,880✔
142
    SET_ERRNO(0);
582,637✔
143
    uint64_t tbUid = taosStr2UInt64(infoReq.tbName, NULL, 10);
582,637✔
144
    if (ERRNO == ERANGE || tbUid == 0) {
582,637✔
145
      code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
146
      goto _exit3;
×
147
    }
148
    SMetaReader mr3 = {0};
582,637✔
149
    metaReaderDoInit(&mr3, ((SVnode *)pVnode)->pMeta, META_READER_NOLOCK);
582,637✔
150
    if ((code = metaReaderGetTableEntryByUid(&mr3, tbUid)) < 0) {
582,637✔
151
      metaReaderClear(&mr3);
232,271✔
152
      TAOS_CHECK_GOTO(code, NULL, _exit3);
232,271✔
153
    }
154
    tstrncpy(metaRsp.tbName, mr3.me.name, TSDB_TABLE_NAME_LEN);
350,366✔
155
    metaReaderClear(&mr3);
350,366✔
156
    TAOS_CHECK_GOTO(metaGetTableEntryByName(&mer1, metaRsp.tbName), NULL, _exit3);
350,366✔
157
  } else if (metaGetTableEntryByName(&mer1, infoReq.tbName) < 0) {
31,693,243✔
158
    code = terrno;
11,881,760✔
159
    goto _exit3;
11,880,818✔
160
  }
161

162
  metaRsp.tableType = mer1.me.type;
20,161,849✔
163
  metaRsp.vgId = TD_VID(pVnode);
20,161,849✔
164
  metaRsp.tuid = mer1.me.uid;
20,161,849✔
165

166
  switch (mer1.me.type) {
20,161,849✔
167
    case TSDB_SUPER_TABLE: {
3,635,190✔
168
      (void)strcpy(metaRsp.stbName, mer1.me.name);
3,635,190✔
169
      schema = mer1.me.stbEntry.schemaRow;
3,635,190✔
170
      schemaTag = mer1.me.stbEntry.schemaTag;
3,635,190✔
171
      metaRsp.suid = mer1.me.uid;
3,635,190✔
172
      metaRsp.virtualStb = TABLE_IS_VIRTUAL(mer1.me.flags);
3,635,190✔
173
      break;
3,635,190✔
174
    }
175
    case TSDB_CHILD_TABLE:
13,461,659✔
176
    case TSDB_VIRTUAL_CHILD_TABLE:{
177
      metaReaderDoInit(&mer2, pVnode->pMeta, META_READER_NOLOCK);
13,461,659✔
178
      if (metaReaderGetTableEntryByUid(&mer2, mer1.me.ctbEntry.suid) < 0) goto _exit2;
13,461,659✔
179

180
      (void)strcpy(metaRsp.stbName, mer2.me.name);
13,461,659✔
181
      metaRsp.suid = mer2.me.uid;
13,461,659✔
182
      schema = mer2.me.stbEntry.schemaRow;
13,461,659✔
183
      schemaTag = mer2.me.stbEntry.schemaTag;
13,461,659✔
184
      break;
13,461,659✔
185
    }
186
    case TSDB_NORMAL_TABLE:
3,065,000✔
187
    case TSDB_VIRTUAL_NORMAL_TABLE: {
188
      schema = mer1.me.ntbEntry.schemaRow;
3,065,000✔
189
      break;
3,065,000✔
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;
20,161,849✔
198
  metaRsp.numOfColumns = schema.nCols;
20,161,849✔
199
  metaRsp.precision = pVnode->config.tsdbCfg.precision;
20,161,849✔
200
  metaRsp.sversion = schema.version;
20,160,908✔
201
  metaRsp.tversion = schemaTag.version;
20,160,908✔
202
  metaRsp.pSchemas = (SSchema *)taosMemoryMalloc(sizeof(SSchema) * (metaRsp.numOfColumns + metaRsp.numOfTags));
20,160,908✔
203
  metaRsp.pSchemaExt = (SSchemaExt *)taosMemoryCalloc(metaRsp.numOfColumns, sizeof(SSchemaExt));
20,160,908✔
204
  if (NULL == metaRsp.pSchemas || NULL == metaRsp.pSchemaExt) {
20,160,652✔
205
    code = terrno;
×
206
    goto _exit;
×
207
  }
208
  (void)memcpy(metaRsp.pSchemas, schema.pSchema, sizeof(SSchema) * schema.nCols);
20,161,376✔
209
  if (schemaTag.nCols) {
20,161,376✔
210
    (void)memcpy(metaRsp.pSchemas + schema.nCols, schemaTag.pSchema, sizeof(SSchema) * schemaTag.nCols);
17,096,849✔
211
  }
212
  if (metaRsp.pSchemaExt) {
20,160,435✔
213
    SMetaReader *pReader = mer1.me.type == TSDB_CHILD_TABLE ? &mer2 : &mer1;
20,161,849✔
214
    code = fillTableColCmpr(pReader, metaRsp.pSchemaExt, metaRsp.numOfColumns);
20,161,849✔
215
    if (code < 0) {
20,161,381✔
216
      goto _exit;
×
217
    }
218
    for (int32_t i = 0; i < metaRsp.numOfColumns && pReader->me.pExtSchemas; i++) {
52,703,643✔
219
      metaRsp.pSchemaExt[i].typeMod = pReader->me.pExtSchemas[i].typeMod;
32,542,262✔
220
    }
221
  } else {
222
    code = TSDB_CODE_OUT_OF_MEMORY;
×
223
    goto _exit;
×
224
  }
225
  if (hasRefCol(mer1.me.type)) {
20,161,849✔
226
    metaRsp.rversion = mer1.me.colRef.version;
196,713✔
227
    metaRsp.pColRefs = (SColRef*)taosMemoryMalloc(sizeof(SColRef) * metaRsp.numOfColumns);
196,713✔
228
    if (metaRsp.pColRefs) {
195,516✔
229
      code = fillTableColRef(&mer1, metaRsp.pColRefs, metaRsp.numOfColumns);
195,516✔
230
      if (code < 0) {
195,516✔
231
        goto _exit;
×
232
      }
233
    }
234
    metaRsp.numOfColRefs = metaRsp.numOfColumns;
195,516✔
235
  } else {
236
    metaRsp.pColRefs = NULL;
19,965,136✔
237
    metaRsp.numOfColRefs = 0;
19,965,136✔
238
  }
239

240
  vnodePrintTableMeta(&metaRsp);
20,160,652✔
241

242
  // encode and send response
243
  rspLen = tSerializeSTableMetaRsp(NULL, 0, &metaRsp);
20,161,849✔
244
  if (rspLen < 0) {
20,160,699✔
245
    code = terrno;
×
246
    goto _exit;
×
247
  }
248

249
  if (direct) {
20,160,699✔
250
    pRsp = rpcMallocCont(rspLen);
76,987✔
251
  } else {
252
    pRsp = taosMemoryCalloc(1, rspLen);
20,083,712✔
253
  }
254

255
  if (pRsp == NULL) {
20,161,381✔
256
    code = terrno;
×
257
    goto _exit;
×
258
  }
259

260
  rspLen = tSerializeSTableMetaRsp(pRsp, rspLen, &metaRsp);
20,161,381✔
261
  if (rspLen < 0) {
20,160,993✔
262
    code = terrno;
×
263
    goto _exit;
×
264
  }
265

266
_exit:
20,159,579✔
267
  taosMemoryFree(metaRsp.pColRefs);
20,161,073✔
268
  taosMemoryFree(metaRsp.pSchemas);
20,160,993✔
269
  taosMemoryFree(metaRsp.pSchemaExt);
20,158,867✔
270
_exit2:
20,160,779✔
271
  metaReaderClear(&mer2);
20,160,699✔
272
_exit3:
32,273,106✔
273
  metaReaderClear(&mer1);
32,274,941✔
274
_exit4:
32,272,978✔
275
  rpcMsg.info = pMsg->info;
32,274,048✔
276
  rpcMsg.pCont = pRsp;
32,273,577✔
277
  rpcMsg.contLen = rspLen;
32,273,577✔
278
  rpcMsg.code = code;
32,273,577✔
279
  rpcMsg.msgType = pMsg->msgType;
32,273,577✔
280

281
  if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST && autoCreateCtb == 1) {
32,273,189✔
282
    code = TSDB_CODE_SUCCESS;
8,550,151✔
283
  }
284

285
  if (code) {
32,273,189✔
286
    qError("vgId:%d, get table %s meta with %" PRIu8 " failed cause of %s", pVnode->config.vgId, infoReq.tbName,
3,563,880✔
287
           infoReq.option, tstrerror(code));
288
  }
289

290
  if (direct) {
32,273,192✔
291
    tmsgSendRsp(&rpcMsg);
765,060✔
292
  } else {
293
    *pMsg = rpcMsg;
31,508,132✔
294
  }
295

296
  return code;
32,274,342✔
297
}
298

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

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

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

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

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

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

336
  cfgRsp.tableType = mer1.me.type;
72,914✔
337

338
  if (mer1.me.type == TSDB_SUPER_TABLE) {
72,914✔
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) {
72,914✔
342
    metaReaderDoInit(&mer2, pVnode->pMeta, META_READER_NOLOCK);
42,223✔
343
    if (metaReaderGetTableEntryByUid(&mer2, mer1.me.ctbEntry.suid) < 0) goto _exit;
42,223✔
344

345
    tstrncpy(cfgRsp.stbName, mer2.me.name, TSDB_TABLE_NAME_LEN);
42,223✔
346
    schema = mer2.me.stbEntry.schemaRow;
42,223✔
347
    schemaTag = mer2.me.stbEntry.schemaTag;
42,223✔
348
    cfgRsp.ttl = mer1.me.ctbEntry.ttlDays;
42,223✔
349
    cfgRsp.commentLen = mer1.me.ctbEntry.commentLen;
42,223✔
350
    if (mer1.me.ctbEntry.commentLen > 0) {
42,223✔
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;
42,223✔
358
    cfgRsp.tagsLen = pTag->len;
42,223✔
359
    cfgRsp.pTags = taosMemoryMalloc(cfgRsp.tagsLen);
42,223✔
360
    if (NULL == cfgRsp.pTags) {
42,223✔
361
      code = terrno;
×
362
      goto _exit;
×
363
    }
364
    (void)memcpy(cfgRsp.pTags, pTag, cfgRsp.tagsLen);
42,223✔
365
  } else if (mer1.me.type == TSDB_NORMAL_TABLE || mer1.me.type == TSDB_VIRTUAL_NORMAL_TABLE) {
30,691✔
366
    schema = mer1.me.ntbEntry.schemaRow;
30,691✔
367
    cfgRsp.ttl = mer1.me.ntbEntry.ttlDays;
30,691✔
368
    cfgRsp.commentLen = mer1.me.ntbEntry.commentLen;
30,691✔
369
    if (mer1.me.ntbEntry.commentLen > 0) {
30,691✔
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;
72,914✔
383
  cfgRsp.numOfColumns = schema.nCols;
72,914✔
384
  cfgRsp.virtualStb = false; // vnode don't have super table, so it's always false
72,914✔
385
  cfgRsp.pSchemas = (SSchema *)taosMemoryMalloc(sizeof(SSchema) * (cfgRsp.numOfColumns + cfgRsp.numOfTags));
72,914✔
386
  cfgRsp.pSchemaExt = (SSchemaExt *)taosMemoryMalloc(cfgRsp.numOfColumns * sizeof(SSchemaExt));
72,914✔
387
  cfgRsp.pColRefs = (SColRef *)taosMemoryMalloc(sizeof(SColRef) * cfgRsp.numOfColumns);
72,914✔
388

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

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

402
  if (withExtSchema(cfgRsp.tableType)) {
72,914✔
403
    for (int32_t i = 0; i < cfgRsp.numOfColumns; i++) {
320,463✔
404
      SColCmpr   *pCmpr = &pColCmpr->pColCmpr[i];
249,801✔
405
      SSchemaExt *pSchExt = cfgRsp.pSchemaExt + i;
249,801✔
406
      pSchExt->colId = pCmpr->id;
249,801✔
407
      pSchExt->compress = pCmpr->alg;
249,801✔
408
      if (pReader->me.pExtSchemas)
249,801✔
409
        pSchExt->typeMod = pReader->me.pExtSchemas[i].typeMod;
25,674✔
410
      else
411
        pSchExt->typeMod = 0;
224,127✔
412
    }
413
  }
414

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

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

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

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

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

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

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

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

470
  tFreeSTableCfgRsp(&cfgRsp);
72,914✔
471
  metaReaderClear(&mer2);
72,914✔
472
  metaReaderClear(&mer1);
72,914✔
473
  return code;
72,914✔
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) {
26,392,486✔
486
  int32_t      code = 0;
26,392,486✔
487
  int32_t      rspSize = 0;
26,392,486✔
488
  SBatchReq    batchReq = {0};
26,392,486✔
489
  SBatchMsg   *req = NULL;
26,392,486✔
490
  SBatchRspMsg rsp = {0};
26,392,486✔
491
  SBatchRsp    batchRsp = {0};
26,392,486✔
492
  SRpcMsg      reqMsg = *pMsg;
26,392,486✔
493
  SRpcMsg      rspMsg = {0};
26,392,486✔
494
  void        *pRsp = NULL;
26,392,874✔
495

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

502
  int32_t msgNum = taosArrayGetSize(batchReq.pMsgs);
26,393,262✔
503
  if (msgNum >= MAX_META_MSG_IN_BATCH) {
26,393,262✔
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));
26,393,262✔
510
  if (NULL == batchRsp.pRsps) {
26,392,791✔
511
    code = terrno;
×
512
    qError("taosArrayInit %d SBatchRspMsg failed", msgNum);
×
513
    goto _exit;
×
514
  }
515

516
  for (int32_t i = 0; i < msgNum; ++i) {
58,028,373✔
517
    req = taosArrayGet(batchReq.pMsgs, i);
31,637,839✔
518
    if (req == NULL) {
31,637,451✔
519
      code = terrno;
×
520
      goto _exit;
×
521
    }
522

523
    reqMsg.msgType = req->msgType;
31,637,451✔
524
    reqMsg.pCont = req->msg;
31,637,451✔
525
    reqMsg.contLen = req->msgLen;
31,636,980✔
526

527
    switch (req->msgType) {
31,637,451✔
528
      case TDMT_VND_TABLE_META:
30,928,183✔
529
        // error code has been set into reqMsg, no need to handle it here.
530
        if (TSDB_CODE_SUCCESS != vnodeGetTableMeta(pVnode, &reqMsg, false)) {
30,928,183✔
531
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
2,643,536✔
532
        }
533
        break;
30,927,305✔
534
      case TDMT_VND_TABLE_NAME:
582,249✔
535
        // error code has been set into reqMsg, no need to handle it here.
536
        if (TSDB_CODE_SUCCESS != vnodeGetTableMeta(pVnode, &reqMsg, false)) {
582,249✔
537
          qWarn("vnodeGetBatchName failed, msgType:%d", req->msgType);
232,271✔
538
        }
539
        break;
582,249✔
540
      case TDMT_VND_TABLE_CFG:
72,914✔
541
        // error code has been set into reqMsg, no need to handle it here.
542
        if (TSDB_CODE_SUCCESS != vnodeGetTableCfg(pVnode, &reqMsg, false)) {
72,914✔
543
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
×
544
        }
545
        break;
72,914✔
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:
54,105✔
553
        // error code has been set into reqMsg, no need to handle it here.
554
        if (TSDB_CODE_SUCCESS != vnodeGetVStbRefDbs(pVnode, &reqMsg)) {
54,105✔
555
          qWarn("vnodeGetVStbRefDbs failed, msgType:%d", req->msgType);
×
556
        }
557
        break;
54,105✔
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;
31,636,573✔
567
    rsp.reqType = reqMsg.msgType;
31,637,451✔
568
    rsp.msgLen = reqMsg.contLen;
31,637,451✔
569
    rsp.rspCode = reqMsg.code;
31,637,451✔
570
    rsp.msg = reqMsg.pCont;
31,637,451✔
571

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

579
  rspSize = tSerializeSBatchRsp(NULL, 0, &batchRsp);
26,390,534✔
580
  if (rspSize < 0) {
26,392,406✔
581
    qError("tSerializeSBatchRsp failed");
×
582
    code = terrno;
×
583
    goto _exit;
×
584
  }
585
  pRsp = rpcMallocCont(rspSize);
26,392,406✔
586
  if (pRsp == NULL) {
26,392,874✔
587
    qError("rpcMallocCont %d failed", rspSize);
×
588
    code = terrno;
×
589
    goto _exit;
×
590
  }
591
  if (tSerializeSBatchRsp(pRsp, rspSize, &batchRsp) < 0) {
26,392,874✔
592
    qError("tSerializeSBatchRsp %d failed", rspSize);
×
593
    code = terrno;
×
594
    goto _exit;
×
595
  }
596

597
_exit:
26,392,874✔
598

599
  rspMsg.info = pMsg->info;
26,393,262✔
600
  rspMsg.pCont = pRsp;
26,393,262✔
601
  rspMsg.contLen = rspSize;
26,393,262✔
602
  rspMsg.code = code;
26,393,262✔
603
  rspMsg.msgType = pMsg->msgType;
26,393,262✔
604

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

609
  taosArrayDestroyEx(batchReq.pMsgs, tFreeSBatchReqMsg);
26,392,580✔
610
  taosArrayDestroyEx(batchRsp.pRsps, tFreeSBatchRspMsg);
26,392,401✔
611

612
  tmsgSendRsp(&rspMsg);
26,390,485✔
613

614
  return code;
26,392,791✔
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) {
54,105✔
718
  int32_t                    code = TSDB_CODE_SUCCESS;
54,105✔
719
  int32_t                    line = 0;
54,105✔
720
  SMetaReader                mr = {0};
54,105✔
721
  bool                       readerInit = false;
54,105✔
722
  SSHashObj*                 pDbNameHash = NULL;
54,105✔
723
  SArray*                    pList = NULL;
54,105✔
724

725
  pList = taosArrayInit(10, sizeof(uint64_t));
54,105✔
726
  QUERY_CHECK_NULL(pList, code, line, _return, terrno);
54,105✔
727

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

735
  if (taosArrayGetSize(*ppRes) > 0) {
54,105✔
736
    // found in cache
737
    goto _return;
28,709✔
738
  } else {
739
    code = pHandle->api.metaFn.getChildTableList(pHandle->vnode, suid, pList);
25,396✔
740
    QUERY_CHECK_CODE(code, line, _return);
25,396✔
741

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

746
    for (int32_t i = 0; i < num; ++i) {
84,990✔
747
      uint64_t* id = taosArrayGet(pList, i);
59,594✔
748
      QUERY_CHECK_NULL(id, code, line, _return, terrno);
59,594✔
749

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

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

756
      for (int32_t j = 0; j < mr.me.colRef.nCols; j++) {
33,895,949✔
757
        if (mr.me.colRef.pColRef[j].hasRef) {
33,836,355✔
758
          if (NULL == tSimpleHashGet(pDbNameHash, mr.me.colRef.pColRef[j].refDbName, strlen(mr.me.colRef.pColRef[j].refDbName))) {
33,730,185✔
759
            char *refDbName = taosStrdup(mr.me.colRef.pColRef[j].refDbName);
18,876✔
760
            QUERY_CHECK_NULL(refDbName, code, line, _return, terrno);
18,876✔
761

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

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

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

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

778
_return:
54,105✔
779

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

784
  taosArrayDestroy(pList);
54,105✔
785
  tSimpleHashCleanup(pDbNameHash);
54,105✔
786

787
  if (code) {
54,105✔
788
    qError("%s failed since %s", __func__, tstrerror(code));
×
789
  }
790
  return code;
54,105✔
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) {
54,105✔
856
  int32_t        code = 0;
54,105✔
857
  int32_t        rspSize = 0;
54,105✔
858
  SVStbRefDbsReq req = {0};
54,105✔
859
  SVStbRefDbsRsp rsp = {0};
54,105✔
860
  SRpcMsg        rspMsg = {0};
54,105✔
861
  void          *pRsp = NULL;
54,105✔
862
  int32_t        line = 0;
54,105✔
863

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

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

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

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

897
_return:
54,105✔
898

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

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

909
  *pMsg = rspMsg;
54,105✔
910

911
  tDestroySVStbRefDbsRsp(&rsp);
54,105✔
912

913
  return code;
54,105✔
914
}
915

916
static int32_t vnodeGetCompStorage(SVnode *pVnode, int64_t *output) {
105,461,592✔
917
  int32_t code = 0;
105,461,592✔
918
#ifdef TD_ENTERPRISE
919
  int32_t now = taosGetTimestampSec();
105,461,592✔
920
  if (llabs(now - pVnode->config.vndStats.storageLastUpd) >= 30) {
105,461,592✔
921
    pVnode->config.vndStats.storageLastUpd = now;
6,071,902✔
922

923
    SDbSizeStatisInfo info = {0};
6,071,902✔
924
    if (0 == (code = vnodeGetDBSize(pVnode, &info))) {
6,071,902✔
925
      int64_t compSize =
6,071,902✔
926
          info.l1Size + info.l2Size + info.l3Size + info.cacheSize + info.walSize + info.metaSize + +info.ssSize;
6,071,902✔
927
      if (compSize >= 0) {
6,071,902✔
928
        pVnode->config.vndStats.compStorage = compSize;
6,071,902✔
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;
105,461,592✔
938
#endif
939
  return code;
105,461,592✔
940
}
941

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

948
    if (pVnode->inUse) {
105,461,592✔
949
      *bufferSegmentUsed = pVnode->inUse->size;
105,447,535✔
950
    }
951
    *bufferSegmentSize = pVnode->config.szBuf / VNODE_BUFPOOL_SEGMENTS;
105,461,592✔
952

953
    (void)taosThreadMutexUnlock(&pVnode->mutex);
105,461,592✔
954
  }
955
}
105,461,592✔
956

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

962
  pLoad->vgId = TD_VID(pVnode);
105,461,592✔
963
  pLoad->syncState = state.state;
105,461,592✔
964
  pLoad->syncRestore = state.restored;
105,461,592✔
965
  pLoad->syncTerm = state.term;
105,461,592✔
966
  pLoad->roleTimeMs = state.roleTimeMs;
105,461,592✔
967
  pLoad->startTimeMs = state.startTimeMs;
105,461,592✔
968
  pLoad->syncCanRead = state.canRead;
105,461,592✔
969
  pLoad->learnerProgress = state.progress;
105,461,592✔
970
  pLoad->cacheUsage = tsdbCacheGetUsage(pVnode);
105,461,592✔
971
  pLoad->numOfCachedTables = tsdbCacheGetElems(pVnode);
105,461,592✔
972
  VNODE_DO_META_QUERY(pVnode, pLoad->numOfTables = metaGetTbNum(pVnode->pMeta));
105,461,592✔
973
  VNODE_DO_META_QUERY(pVnode, pLoad->numOfTimeSeries = metaGetTimeSeriesNum(pVnode->pMeta, 1));
105,461,592✔
974
  pLoad->totalStorage = (int64_t)3 * 1073741824;  // TODO
105,461,592✔
975
  (void)vnodeGetCompStorage(pVnode, &pLoad->compStorage);
105,461,592✔
976
  pLoad->pointsWritten = 100;
105,461,592✔
977
  pLoad->numOfSelectReqs = 1;
105,461,592✔
978
  pLoad->numOfInsertReqs = atomic_load_64(&pVnode->statis.nInsert);
105,461,592✔
979
  pLoad->numOfInsertSuccessReqs = atomic_load_64(&pVnode->statis.nInsertSuccess);
105,461,592✔
980
  pLoad->numOfBatchInsertReqs = atomic_load_64(&pVnode->statis.nBatchInsert);
105,461,592✔
981
  pLoad->numOfBatchInsertSuccessReqs = atomic_load_64(&pVnode->statis.nBatchInsertSuccess);
105,461,592✔
982
  vnodeGetBufferInfo(pVnode, &pLoad->bufferSegmentUsed, &pLoad->bufferSegmentSize);
105,461,592✔
983
  return 0;
105,461,592✔
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) {
284✔
1002
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nInsert, pLoad->numOfInsertReqs, 64, "nInsert");
284✔
1003
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nInsertSuccess, pLoad->numOfInsertSuccessReqs, 64, "nInsertSuccess");
284✔
1004
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nBatchInsert, pLoad->numOfBatchInsertReqs, 64, "nBatchInsert");
284✔
1005
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nBatchInsertSuccess, pLoad->numOfBatchInsertSuccessReqs, 64,
284✔
1006
                            "nBatchInsertSuccess");
1007
}
284✔
1008

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

1013
  if (dbname) {
5,794,321✔
1014
    *dbname = pConf->dbname;
5,724,262✔
1015
  }
1016

1017
  if (vgId) {
5,794,689✔
1018
    *vgId = TD_VID(pVnodeObj);
4,991,292✔
1019
  }
1020

1021
  if (numOfTables) {
5,794,689✔
1022
    *numOfTables = pConf->vndStats.numOfNTables + pConf->vndStats.numOfCTables +
114,774✔
1023
                   pConf->vndStats.numOfVTables + pConf->vndStats.numOfVCTables;
57,426✔
1024
  }
1025

1026
  if (numOfNormalTables) {
5,794,689✔
1027
    *numOfNormalTables = pConf->vndStats.numOfNTables +
24,478✔
1028
                         pConf->vndStats.numOfVTables;
12,265✔
1029
  }
1030
}
5,794,689✔
1031

1032
int32_t vnodeGetTableList(void *pVnode, int8_t type, SArray *pList) {
12,265✔
1033
  if (type == TSDB_SUPER_TABLE) {
12,265✔
1034
    return vnodeGetStbIdList(pVnode, 0, pList);
12,265✔
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) {
54,744,431✔
1071
  int32_t      code = TSDB_CODE_SUCCESS;
54,744,431✔
1072
  SVnode      *pVnodeObj = pVnode;
54,744,431✔
1073
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnodeObj, suid, 1);
54,744,431✔
1074
  if (NULL == pCur) {
54,734,682✔
1075
    qError("vnode get all table list failed");
×
1076
    return terrno;
×
1077
  }
1078

1079
  while (1) {
264,127,500✔
1080
    tb_uid_t id = metaCtbCursorNext(pCur);
318,862,182✔
1081
    if (id == 0) {
318,862,190✔
1082
      break;
54,748,704✔
1083
    }
1084
    qTrace("vnodeGetCtbIdList: got ctb id %" PRId64 " for suid %" PRId64, id, suid);
264,113,486✔
1085
    if (NULL == taosArrayPush(list, &id)) {
264,138,839✔
1086
      qError("taosArrayPush failed");
×
1087
      code = terrno;
×
1088
      goto _exit;
×
1089
    }
1090
  }
1091

1092
_exit:
54,728,190✔
1093
  metaCloseCtbCursor(pCur);
54,728,190✔
1094
  return code;
54,730,744✔
1095
}
1096

1097
int32_t vnodeGetStbIdList(SVnode *pVnode, int64_t suid, SArray *list) {
73,939,170✔
1098
  int32_t      code = TSDB_CODE_SUCCESS;
73,939,170✔
1099
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, suid);
73,939,170✔
1100
  if (!pCur) {
73,940,534✔
1101
    return TSDB_CODE_OUT_OF_MEMORY;
×
1102
  }
1103

1104
  while (1) {
124,325,170✔
1105
    tb_uid_t id = metaStbCursorNext(pCur);
198,265,704✔
1106
    if (id == 0) {
198,261,455✔
1107
      break;
73,941,741✔
1108
    }
1109

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

1117
_exit:
73,935,646✔
1118
  metaCloseStbCursor(pCur);
73,935,646✔
1119
  return code;
73,930,304✔
1120
}
1121

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

1130
  while (1) {
1,270✔
1131
    tb_uid_t id = metaStbCursorNext(pCur);
1,524✔
1132
    if (id == 0) {
1,524✔
1133
      break;
254✔
1134
    }
1135

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

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

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

1152
int32_t vnodeGetCtbNum(SVnode *pVnode, int64_t suid, int64_t *num) {
7,081,851✔
1153
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, suid, 0);
7,081,851✔
1154
  if (!pCur) {
7,081,867✔
1155
    return terrno;
×
1156
  }
1157

1158
  *num = 0;
7,081,867✔
1159
  while (1) {
2,551,539✔
1160
    tb_uid_t id = metaCtbCursorNext(pCur);
9,633,494✔
1161
    if (id == 0) {
9,633,565✔
1162
      break;
7,082,096✔
1163
    }
1164

1165
    ++(*num);
2,551,469✔
1166
  }
1167

1168
  metaCloseCtbCursor(pCur);
7,082,096✔
1169
  return TSDB_CODE_SUCCESS;
7,082,247✔
1170
}
1171

1172
int32_t vnodeGetStbColumnNum(SVnode *pVnode, tb_uid_t suid, int *num) {
7,082,177✔
1173
  SSchemaWrapper *pSW = metaGetTableSchema(pVnode->pMeta, suid, -1, 0, NULL, 0);
7,082,177✔
1174
  if (pSW) {
7,082,262✔
1175
    *num = pSW->nCols;
7,082,262✔
1176
    tDeleteSchemaWrapper(pSW);
1177
  } else {
UNCOV
1178
    *num = 2;
×
1179
  }
1180

1181
  return TSDB_CODE_SUCCESS;
7,081,973✔
1182
}
1183

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

1188
  int32_t code = metaReaderGetTableEntryByUid(&mr, suid);
7,081,528✔
1189
  if (code == TSDB_CODE_SUCCESS) {
7,082,262✔
1190
    if (keep) *keep = mr.me.stbEntry.keep;
7,082,262✔
1191
    if (flags) *flags = mr.me.flags;
7,082,262✔
1192
  } else {
UNCOV
1193
    if (keep) *keep = 0;
×
UNCOV
1194
    if (flags) *flags = 0;
×
1195
  }
1196

1197
  metaReaderClear(&mr);
7,082,262✔
1198
  return TSDB_CODE_SUCCESS;
7,082,066✔
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) {
73,926,229✔
1227
  int32_t      code = TSDB_CODE_SUCCESS;
73,926,229✔
1228
  int32_t      tbNum = 0;
73,926,229✔
1229
  const char **pTbArr = NULL;
73,926,229✔
1230
  const char  *dbName = NULL;
73,926,229✔
1231
  *tbSize = 0;
73,926,229✔
1232

1233
  if (!(dbName = strchr(pVnode->config.dbname, '.'))) return 0;
73,926,911✔
1234
  if (0 == strncmp(++dbName, "log", TSDB_DB_NAME_LEN)) {
73,923,387✔
1235
    tbNum = tkLogStbNum;
3,048✔
1236
    pTbArr = (const char **)&tkLogStb;
3,048✔
1237
  } else if (0 == strncmp(dbName, "audit", TSDB_DB_NAME_LEN)) {
73,925,909✔
1238
    tbNum = tkAuditStbNum;
1,410✔
1239
    pTbArr = (const char **)&tkAuditStb;
1,410✔
1240
  }
1241
  if (tbNum && pTbArr) {
73,928,957✔
1242
    *tbSize = metaSizeOfTbFilterCache(pVnode->pMeta, 0);
4,458✔
1243
    if (*tbSize < tbNum) {
4,458✔
1244
      for (int32_t i = 0; i < tbNum; ++i) {
47,778✔
1245
        tb_uid_t suid = metaGetTableEntryUidByName(pVnode->pMeta, pTbArr[i]);
45,098✔
1246
        if (suid != 0) {
45,098✔
1247
          code = metaPutTbToFilterCache(pVnode->pMeta, &suid, 0);
3,048✔
1248
          if (TSDB_CODE_SUCCESS != code) {
3,048✔
1249
            return code;
1,778✔
1250
          }
1251
        }
1252
      }
1253
      *tbSize = metaSizeOfTbFilterCache(pVnode->pMeta, 0);
2,680✔
1254
    }
1255
  }
1256

1257
  return code;
73,925,133✔
1258
}
1259
#endif
1260

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

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

1270
int32_t vnodeGetTimeSeriesNum(SVnode *pVnode, int64_t *num) {
73,926,229✔
1271
  SArray *suidList = NULL;
73,926,229✔
1272

1273
  if (!(suidList = taosArrayInit(1, sizeof(tb_uid_t)))) {
73,926,229✔
1274
    return terrno;
×
1275
  }
1276

1277
  int32_t tbFilterSize = 0;
73,922,137✔
1278
  int32_t code = TSDB_CODE_SUCCESS;
73,920,773✔
1279
#ifdef TD_ENTERPRISE
1280
  code = vnodeGetTimeSeriesBlackList(pVnode, &tbFilterSize);
73,920,773✔
1281
  if (TSDB_CODE_SUCCESS != code) {
73,926,911✔
1282
    goto _exit;
1,778✔
1283
  }
1284
#endif
1285

1286
  if ((!tbFilterSize && vnodeGetStbIdList(pVnode, 0, suidList) < 0) ||
73,925,133✔
1287
      (tbFilterSize && vnodeGetStbIdListByFilter(pVnode, 0, suidList, vnodeTimeSeriesFilter, pVnode) < 0)) {
73,920,245✔
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;
73,920,245✔
1294
  int64_t arrSize = taosArrayGetSize(suidList);
73,925,815✔
1295
  for (int64_t i = 0; i < arrSize; ++i) {
198,223,217✔
1296
    tb_uid_t suid = *(tb_uid_t *)taosArrayGet(suidList, i);
124,292,628✔
1297

1298
    int64_t ctbNum = 0;
124,297,402✔
1299
    int32_t numOfCols = 0;
124,300,812✔
1300
    int8_t  flags = 0;
124,302,176✔
1301
    code = metaGetStbStats(pVnode, suid, &ctbNum, &numOfCols, &flags);
124,298,084✔
1302
    if (TSDB_CODE_SUCCESS != code) {
124,313,088✔
1303
      goto _exit;
×
1304
    }
1305
    if (!TABLE_IS_VIRTUAL(flags)) {
124,313,088✔
1306
      *num += ctbNum * (numOfCols - 1);
122,085,264✔
1307
    }
1308
  }
1309

1310
_exit:
73,932,367✔
1311
  taosArrayDestroy(suidList);
73,933,049✔
1312
  return TSDB_CODE_SUCCESS;
73,924,069✔
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) {
4,438,187✔
1343
  if (pVnode == NULL) {
4,438,187✔
1344
    return NULL;
×
1345
  }
1346

1347
  return metaGetIdx(((SVnode *)pVnode)->pMeta);
4,438,187✔
1348
}
1349

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

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

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

1365
  char   *dirName[] = {VNODE_TSDB_DIR, VNODE_WAL_DIR, VNODE_META_DIR, VNODE_TSDB_CACHE_DIR};
6,084,840✔
1366
  int64_t dirSize[4];
6,083,938✔
1367

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

1371
  for (int i = 0; i < sizeof(dirName) / sizeof(dirName[0]); i++) {
30,424,200✔
1372
    int64_t size = {0};
24,339,360✔
1373
    (void)snprintf(path + offset, TSDB_FILENAME_LEN - offset, "%s%s", TD_DIRSEP, dirName[i]);
24,339,360✔
1374
    code = taosGetDirSize(path, &size);
24,338,994✔
1375
    if (code != 0) {
24,339,360✔
1376
      uWarn("vnode %d get dir %s %s size failed since %s", TD_VID(pVnode), path, dirName[i], tstrerror(code));
2,678✔
1377
    }
1378
    path[offset] = 0;
24,339,360✔
1379
    dirSize[i] = size;
24,339,360✔
1380
  }
1381

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

1398
  code = tsdbGetFsSize(pVnodeObj->pTsdb, pInfo);
6,084,840✔
1399
_exit:
6,084,840✔
1400
  return code;
6,084,840✔
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