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

taosdata / TDengine / #4908

30 Dec 2025 10:52AM UTC coverage: 65.386% (-0.2%) from 65.541%
#4908

push

travis-ci

web-flow
enh: drop multi-stream (#33962)

60 of 106 new or added lines in 4 files covered. (56.6%)

1330 existing lines in 113 files now uncovered.

193461 of 295877 relevant lines covered (65.39%)

115765274.47 hits per line

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

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

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

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

36
int32_t fillTableColCmpr(SMetaReader *reader, SSchemaExt *pExt, int32_t numOfCol) {
18,020,461✔
37
  int8_t tblType = reader->me.type;
18,020,461✔
38
  if (withExtSchema(tblType)) {
18,021,685✔
39
    SColCmprWrapper *p = &(reader->me.colCmpr);
17,877,657✔
40
    if (numOfCol != p->nCols) {
17,878,269✔
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++) {
213,513,916✔
45
      SColCmpr *pCmpr = &p->pColCmpr[i];
195,639,167✔
46
      pExt[i].colId = pCmpr->id;
195,635,928✔
47
      pExt[i].compress = pCmpr->alg;
195,638,095✔
48
    }
49
  }
50
  return 0;
18,018,516✔
51
}
52

53
void vnodePrintTableMeta(STableMetaRsp *pMeta) {
18,019,237✔
54
  if (!(qDebugFlag & DEBUG_DEBUG)) {
18,019,237✔
55
    return;
114,011✔
56
  }
57

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

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

103
int32_t vnodeGetTableMeta(SVnode *pVnode, SRpcMsg *pMsg, bool direct) {
24,881,972✔
104
  STableInfoReq  infoReq = {0};
24,881,972✔
105
  STableMetaRsp  metaRsp = {0};
24,882,584✔
106
  SMetaReader    mer1 = {0};
24,882,029✔
107
  SMetaReader    mer2 = {0};
24,882,641✔
108
  char           tableFName[TSDB_TABLE_FNAME_LEN];
24,879,399✔
109
  bool           reqTbUid = false;
24,882,982✔
110
  SRpcMsg        rpcMsg = {0};
24,882,982✔
111
  int32_t        code = 0;
24,882,982✔
112
  int32_t        rspLen = 0;
24,882,982✔
113
  void          *pRsp = NULL;
24,882,982✔
114
  SSchemaWrapper schema = {0};
24,882,982✔
115
  SSchemaWrapper schemaTag = {0};
24,882,982✔
116
  uint8_t        autoCreateCtb = 0;
24,882,982✔
117

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

125
  if (infoReq.option == REQ_OPT_TBUID) reqTbUid = true;
24,882,801✔
126
  metaRsp.dbId = pVnode->config.dbId;
24,882,801✔
127
  tstrncpy(metaRsp.tbName, infoReq.tbName, TSDB_TABLE_NAME_LEN);
24,883,304✔
128
  (void)memcpy(metaRsp.dbFName, infoReq.dbFName, sizeof(metaRsp.dbFName));
24,883,304✔
129

130
  if (!reqTbUid) {
24,883,304✔
131
    (void)tsnprintf(tableFName, TSDB_TABLE_FNAME_LEN, "%s.%s", infoReq.dbFName, infoReq.tbName);
24,635,436✔
132
    if (pVnode->mounted) tTrimMountPrefix(tableFName);
24,635,650✔
133
    code = vnodeValidateTableHash(pVnode, tableFName);
24,636,262✔
134
    if (code) {
24,635,222✔
135
      goto _exit4;
×
136
    }
137
  }
138

139
  // query meta
140
  metaReaderDoInit(&mer1, pVnode->pMeta, META_READER_LOCK);
24,883,090✔
141
  if (reqTbUid) {
24,881,363✔
142
    SET_ERRNO(0);
246,141✔
143
    uint64_t tbUid = taosStr2UInt64(infoReq.tbName, NULL, 10);
246,322✔
144
    if (ERRNO == ERANGE || tbUid == 0) {
246,322✔
145
      code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
146
      goto _exit3;
×
147
    }
148
    SMetaReader mr3 = {0};
246,322✔
149
    metaReaderDoInit(&mr3, ((SVnode *)pVnode)->pMeta, META_READER_NOLOCK);
246,322✔
150
    if ((code = metaReaderGetTableEntryByUid(&mr3, tbUid)) < 0) {
246,322✔
151
      metaReaderClear(&mr3);
91,451✔
152
      TAOS_CHECK_GOTO(code, NULL, _exit3);
91,451✔
153
    }
154
    tstrncpy(metaRsp.tbName, mr3.me.name, TSDB_TABLE_NAME_LEN);
154,871✔
155
    metaReaderClear(&mr3);
154,871✔
156
    TAOS_CHECK_GOTO(metaGetTableEntryByName(&mer1, metaRsp.tbName), NULL, _exit3);
154,871✔
157
  } else if (metaGetTableEntryByName(&mer1, infoReq.tbName) < 0) {
24,635,222✔
158
    code = terrno;
6,770,672✔
159
    goto _exit3;
6,770,672✔
160
  }
161

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

166
  switch (mer1.me.type) {
18,019,128✔
167
    case TSDB_SUPER_TABLE: {
3,360,021✔
168
      (void)strcpy(metaRsp.stbName, mer1.me.name);
3,360,021✔
169
      schema = mer1.me.stbEntry.schemaRow;
3,360,021✔
170
      schemaTag = mer1.me.stbEntry.schemaTag;
3,360,021✔
171
      metaRsp.suid = mer1.me.uid;
3,360,021✔
172
      metaRsp.virtualStb = TABLE_IS_VIRTUAL(mer1.me.flags);
3,360,021✔
173
      metaRsp.ownerId = mer1.me.stbEntry.ownerId;
3,360,021✔
174
      break;
3,360,021✔
175
    }
176
    case TSDB_CHILD_TABLE:
11,843,795✔
177
    case TSDB_VIRTUAL_CHILD_TABLE:{
178
      metaReaderDoInit(&mer2, pVnode->pMeta, META_READER_NOLOCK);
11,843,795✔
179
      if (metaReaderGetTableEntryByUid(&mer2, mer1.me.ctbEntry.suid) < 0) goto _exit2;
11,844,516✔
180

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

199
  metaRsp.numOfTags = schemaTag.nCols;
18,019,849✔
200
  metaRsp.numOfColumns = schema.nCols;
18,019,849✔
201
  metaRsp.precision = pVnode->config.tsdbCfg.precision;
18,019,849✔
202
  metaRsp.sversion = schema.version;
18,019,128✔
203
  metaRsp.tversion = schemaTag.version;
18,019,128✔
204
  metaRsp.pSchemas = (SSchema *)taosMemoryMalloc(sizeof(SSchema) * (metaRsp.numOfColumns + metaRsp.numOfTags));
18,019,128✔
205
  metaRsp.pSchemaExt = (SSchemaExt *)taosMemoryCalloc(metaRsp.numOfColumns, sizeof(SSchemaExt));
18,016,951✔
206
  if (NULL == metaRsp.pSchemas || NULL == metaRsp.pSchemaExt) {
18,019,740✔
207
    code = terrno;
×
208
    goto _exit;
×
209
  }
210
  (void)memcpy(metaRsp.pSchemas, schema.pSchema, sizeof(SSchema) * schema.nCols);
18,019,740✔
211
  if (schemaTag.nCols) {
18,019,740✔
212
    (void)memcpy(metaRsp.pSchemas + schema.nCols, schemaTag.pSchema, sizeof(SSchema) * schemaTag.nCols);
15,204,537✔
213
  }
214
  if (metaRsp.pSchemaExt) {
18,019,740✔
215
    SMetaReader *pReader = mer1.me.type == TSDB_CHILD_TABLE ? &mer2 : &mer1;
18,019,740✔
216
    code = fillTableColCmpr(pReader, metaRsp.pSchemaExt, metaRsp.numOfColumns);
18,019,740✔
217
    if (code < 0) {
18,019,128✔
218
      goto _exit;
×
219
    }
220
    for (int32_t i = 0; i < metaRsp.numOfColumns && pReader->me.pExtSchemas; i++) {
42,671,904✔
221
      metaRsp.pSchemaExt[i].typeMod = pReader->me.pExtSchemas[i].typeMod;
24,652,776✔
222
    }
223
  } else {
UNCOV
224
    code = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
225
    goto _exit;
×
226
  }
227
  if (hasRefCol(mer1.me.type)) {
18,020,461✔
228
    metaRsp.rversion = mer1.me.colRef.version;
143,145✔
229
    metaRsp.pColRefs = (SColRef*)taosMemoryMalloc(sizeof(SColRef) * metaRsp.numOfColumns);
143,145✔
230
    if (metaRsp.pColRefs) {
142,804✔
231
      code = fillTableColRef(&mer1, metaRsp.pColRefs, metaRsp.numOfColumns);
142,804✔
232
      if (code < 0) {
142,804✔
233
        goto _exit;
×
234
      }
235
    }
236
    metaRsp.numOfColRefs = metaRsp.numOfColumns;
142,804✔
237
  } else {
238
    metaRsp.pColRefs = NULL;
17,876,087✔
239
    metaRsp.numOfColRefs = 0;
17,876,087✔
240
  }
241

242
  vnodePrintTableMeta(&metaRsp);
18,018,891✔
243

244
  // encode and send response
245
  rspLen = tSerializeSTableMetaRsp(NULL, 0, &metaRsp);
18,021,344✔
246
  if (rspLen < 0) {
18,021,068✔
247
    code = terrno;
×
248
    goto _exit;
×
249
  }
250

251
  if (direct) {
18,021,068✔
252
    pRsp = rpcMallocCont(rspLen);
72,612✔
253
  } else {
254
    pRsp = taosMemoryCalloc(1, rspLen);
17,948,456✔
255
  }
256

257
  if (pRsp == NULL) {
18,019,237✔
258
    code = terrno;
×
259
    goto _exit;
×
260
  }
261

262
  rspLen = tSerializeSTableMetaRsp(pRsp, rspLen, &metaRsp);
18,019,237✔
263
  if (rspLen < 0) {
18,018,896✔
264
    code = terrno;
×
265
    goto _exit;
×
266
  }
267

268
_exit:
18,018,896✔
269
  taosMemoryFree(metaRsp.pColRefs);
18,021,685✔
270
  taosMemoryFree(metaRsp.pSchemas);
18,019,764✔
271
  taosMemoryFree(metaRsp.pSchemaExt);
18,019,493✔
272
_exit2:
18,019,759✔
273
  metaReaderClear(&mer2);
18,020,105✔
274
_exit3:
24,882,403✔
275
  metaReaderClear(&mer1);
24,881,435✔
276
_exit4:
24,883,191✔
277
  rpcMsg.info = pMsg->info;
24,883,271✔
278
  rpcMsg.pCont = pRsp;
24,883,271✔
279
  rpcMsg.contLen = rspLen;
24,883,271✔
280
  rpcMsg.code = code;
24,883,271✔
281
  rpcMsg.msgType = pMsg->msgType;
24,883,271✔
282

283
  if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST && autoCreateCtb == 1) {
24,882,584✔
284
    code = TSDB_CODE_SUCCESS;
4,765,143✔
285
  }
286

287
  if (code) {
24,882,584✔
288
    qError("vgId:%d, get table %s meta with %" PRIu8 " failed cause of %s", pVnode->config.vgId, infoReq.tbName,
2,096,980✔
289
           infoReq.option, tstrerror(code));
290
  }
291

292
  if (direct) {
24,879,163✔
293
    tmsgSendRsp(&rpcMsg);
755,490✔
294
  } else {
295
    *pMsg = rpcMsg;
24,123,673✔
296
  }
297

298
  return code;
24,880,532✔
299
}
300

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

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

320
  tstrncpy(cfgRsp.tbName, cfgReq.tbName, TSDB_TABLE_NAME_LEN);
48,484✔
321
  (void)memcpy(cfgRsp.dbFName, cfgReq.dbFName, sizeof(cfgRsp.dbFName));
48,484✔
322

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

330
  // query meta
331
  metaReaderDoInit(&mer1, pVnode->pMeta, META_READER_LOCK);
48,484✔
332

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

338
  cfgRsp.tableType = mer1.me.type;
48,484✔
339

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

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

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

392
  if (NULL == cfgRsp.pSchemas || NULL == cfgRsp.pSchemaExt || NULL == cfgRsp.pColRefs) {
48,484✔
393
    code = terrno;
×
394
    goto _exit;
×
395
  }
396
  (void)memcpy(cfgRsp.pSchemas, schema.pSchema, sizeof(SSchema) * schema.nCols);
48,484✔
397
  if (schemaTag.nCols) {
48,484✔
398
    (void)memcpy(cfgRsp.pSchemas + schema.nCols, schemaTag.pSchema, sizeof(SSchema) * schemaTag.nCols);
33,353✔
399
  }
400

401
  SMetaReader     *pReader = (mer1.me.type == TSDB_CHILD_TABLE || mer1.me.type == TSDB_VIRTUAL_CHILD_TABLE) ? &mer2 : &mer1;
48,484✔
402
  SColCmprWrapper *pColCmpr = &pReader->me.colCmpr;
48,484✔
403
  SColRefWrapper  *pColRef = &mer1.me.colRef;
48,484✔
404

405
  if (withExtSchema(cfgRsp.tableType)) {
48,484✔
406
    for (int32_t i = 0; i < cfgRsp.numOfColumns; i++) {
191,898✔
407
      SColCmpr   *pCmpr = &pColCmpr->pColCmpr[i];
146,610✔
408
      SSchemaExt *pSchExt = cfgRsp.pSchemaExt + i;
146,610✔
409
      pSchExt->colId = pCmpr->id;
146,610✔
410
      pSchExt->compress = pCmpr->alg;
146,610✔
411
      if (pReader->me.pExtSchemas)
146,610✔
412
        pSchExt->typeMod = pReader->me.pExtSchemas[i].typeMod;
23,397✔
413
      else
414
        pSchExt->typeMod = 0;
123,213✔
415
    }
416
  }
417

418
  cfgRsp.virtualStb = false;
48,484✔
419
  if (hasRefCol(cfgRsp.tableType)) {
48,484✔
420
    for (int32_t i = 0; i < cfgRsp.numOfColumns; i++) {
33,456✔
421
      SColRef *pRef = &pColRef->pColRef[i];
30,260✔
422
      cfgRsp.pColRefs[i].hasRef = pRef->hasRef;
30,260✔
423
      cfgRsp.pColRefs[i].id = pRef->id;
30,260✔
424
      if (cfgRsp.pColRefs[i].hasRef) {
30,260✔
425
        tstrncpy(cfgRsp.pColRefs[i].refDbName, pRef->refDbName, TSDB_DB_NAME_LEN);
13,080✔
426
        tstrncpy(cfgRsp.pColRefs[i].refTableName, pRef->refTableName, TSDB_TABLE_NAME_LEN);
13,080✔
427
        tstrncpy(cfgRsp.pColRefs[i].refColName, pRef->refColName, TSDB_COL_NAME_LEN);
13,080✔
428
      }
429
    }
430
  }
431

432
  // encode and send response
433
  rspLen = tSerializeSTableCfgRsp(NULL, 0, &cfgRsp);
48,484✔
434
  if (rspLen < 0) {
48,484✔
435
    code = terrno;
×
436
    goto _exit;
×
437
  }
438

439
  if (direct) {
48,484✔
440
    pRsp = rpcMallocCont(rspLen);
×
441
  } else {
442
    pRsp = taosMemoryCalloc(1, rspLen);
48,484✔
443
  }
444

445
  if (pRsp == NULL) {
48,484✔
446
    code = terrno;
×
447
    goto _exit;
×
448
  }
449

450
  rspLen = tSerializeSTableCfgRsp(pRsp, rspLen, &cfgRsp);
48,484✔
451
  if (rspLen < 0) {
48,484✔
452
    code = terrno;
×
453
    goto _exit;
×
454
  }
455

456
_exit:
48,484✔
457
  rpcMsg.info = pMsg->info;
48,484✔
458
  rpcMsg.pCont = pRsp;
48,484✔
459
  rpcMsg.contLen = rspLen;
48,484✔
460
  rpcMsg.code = code;
48,484✔
461
  rpcMsg.msgType = pMsg->msgType;
48,484✔
462

463
  if (code) {
48,484✔
464
    qError("get table %s cfg failed cause of %s", cfgReq.tbName, tstrerror(code));
×
465
  }
466

467
  if (direct) {
48,484✔
468
    tmsgSendRsp(&rpcMsg);
×
469
  } else {
470
    *pMsg = rpcMsg;
48,484✔
471
  }
472

473
  tFreeSTableCfgRsp(&cfgRsp);
48,484✔
474
  metaReaderClear(&mer2);
48,484✔
475
  metaReaderClear(&mer1);
48,484✔
476
  return code;
48,484✔
477
}
478

479
static FORCE_INLINE void vnodeFreeSBatchRspMsg(void *p) {
480
  if (NULL == p) {
481
    return;
482
  }
483

484
  SBatchRspMsg *pRsp = (SBatchRspMsg *)p;
485
  rpcFreeCont(pRsp->msg);
486
}
487

488
int32_t vnodeGetBatchMeta(SVnode *pVnode, SRpcMsg *pMsg) {
20,146,622✔
489
  int32_t      code = 0;
20,146,622✔
490
  int32_t      rspSize = 0;
20,146,622✔
491
  SBatchReq    batchReq = {0};
20,146,622✔
492
  SBatchMsg   *req = NULL;
20,146,622✔
493
  SBatchRspMsg rsp = {0};
20,146,622✔
494
  SBatchRsp    batchRsp = {0};
20,144,457✔
495
  SRpcMsg      reqMsg = *pMsg;
20,143,845✔
496
  SRpcMsg      rspMsg = {0};
20,143,845✔
497
  void        *pRsp = NULL;
20,147,020✔
498

499
  if (tDeserializeSBatchReq(pMsg->pCont, pMsg->contLen, &batchReq)) {
20,147,020✔
500
    code = terrno;
×
501
    qError("tDeserializeSBatchReq failed");
×
502
    goto _exit;
×
503
  }
504

505
  int32_t msgNum = taosArrayGetSize(batchReq.pMsgs);
20,146,622✔
506
  if (msgNum >= MAX_META_MSG_IN_BATCH) {
20,147,234✔
507
    code = TSDB_CODE_INVALID_MSG;
×
508
    qError("too many msgs %d in vnode batch meta req", msgNum);
×
509
    goto _exit;
×
510
  }
511

512
  batchRsp.pRsps = taosArrayInit(msgNum, sizeof(SBatchRspMsg));
20,147,234✔
513
  if (NULL == batchRsp.pRsps) {
20,146,806✔
514
    code = terrno;
398✔
515
    qError("taosArrayInit %d SBatchRspMsg failed", msgNum);
×
516
    goto _exit;
×
517
  }
518

519
  for (int32_t i = 0; i < msgNum; ++i) {
44,368,859✔
520
    req = taosArrayGet(batchReq.pMsgs, i);
24,221,008✔
521
    if (req == NULL) {
24,221,625✔
522
      code = terrno;
×
523
      goto _exit;
×
524
    }
525

526
    reqMsg.msgType = req->msgType;
24,221,625✔
527
    reqMsg.pCont = req->msg;
24,221,411✔
528
    reqMsg.contLen = req->msgLen;
24,220,898✔
529

530
    switch (req->msgType) {
24,221,625✔
531
      case TDMT_VND_TABLE_META:
23,880,003✔
532
        // error code has been set into reqMsg, no need to handle it here.
533
        if (TSDB_CODE_SUCCESS != vnodeGetTableMeta(pVnode, &reqMsg, false)) {
23,880,003✔
534
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
1,322,651✔
535
        }
536
        break;
23,879,192✔
537
      case TDMT_VND_TABLE_NAME:
246,322✔
538
        // error code has been set into reqMsg, no need to handle it here.
539
        if (TSDB_CODE_SUCCESS != vnodeGetTableMeta(pVnode, &reqMsg, false)) {
246,322✔
540
          qWarn("vnodeGetBatchName failed, msgType:%d", req->msgType);
91,451✔
541
        }
542
        break;
246,322✔
543
      case TDMT_VND_TABLE_CFG:
48,484✔
544
        // error code has been set into reqMsg, no need to handle it here.
545
        if (TSDB_CODE_SUCCESS != vnodeGetTableCfg(pVnode, &reqMsg, false)) {
48,484✔
546
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
×
547
        }
548
        break;
48,484✔
549
      case TDMT_VND_VSUBTABLES_META:
×
550
        // error code has been set into reqMsg, no need to handle it here.
551
        if (TSDB_CODE_SUCCESS != vnodeGetVSubtablesMeta(pVnode, &reqMsg)) {
×
552
          qWarn("vnodeGetVSubtablesMeta failed, msgType:%d", req->msgType);
×
553
        }
554
        break;
×
555
      case TDMT_VND_VSTB_REF_DBS:
46,261✔
556
        // error code has been set into reqMsg, no need to handle it here.
557
        if (TSDB_CODE_SUCCESS != vnodeGetVStbRefDbs(pVnode, &reqMsg)) {
46,261✔
558
          qWarn("vnodeGetVStbRefDbs failed, msgType:%d", req->msgType);
×
559
        }
560
        break;
45,665✔
561
      default:
×
562
        qError("invalid req msgType %d", req->msgType);
×
563
        reqMsg.code = TSDB_CODE_INVALID_MSG;
×
564
        reqMsg.pCont = NULL;
×
565
        reqMsg.contLen = 0;
×
566
        break;
×
567
    }
568

569
    rsp.msgIdx = req->msgIdx;
24,219,663✔
570
    rsp.reqType = reqMsg.msgType;
24,219,663✔
571
    rsp.msgLen = reqMsg.contLen;
24,219,663✔
572
    rsp.rspCode = reqMsg.code;
24,219,663✔
573
    rsp.msg = reqMsg.pCont;
24,219,663✔
574

575
    if (NULL == taosArrayPush(batchRsp.pRsps, &rsp)) {
48,442,114✔
576
      qError("taosArrayPush failed");
×
577
      code = terrno;
×
578
      goto _exit;
×
579
    }
580
  }
581

582
  rspSize = tSerializeSBatchRsp(NULL, 0, &batchRsp);
20,147,851✔
583
  if (rspSize < 0) {
20,145,794✔
584
    qError("tSerializeSBatchRsp failed");
×
585
    code = terrno;
×
586
    goto _exit;
×
587
  }
588
  pRsp = rpcMallocCont(rspSize);
20,145,794✔
589
  if (pRsp == NULL) {
20,145,414✔
590
    qError("rpcMallocCont %d failed", rspSize);
×
591
    code = terrno;
×
592
    goto _exit;
×
593
  }
594
  if (tSerializeSBatchRsp(pRsp, rspSize, &batchRsp) < 0) {
20,145,414✔
595
    qError("tSerializeSBatchRsp %d failed", rspSize);
×
596
    code = terrno;
×
597
    goto _exit;
×
598
  }
599

600
_exit:
20,146,622✔
601

602
  rspMsg.info = pMsg->info;
20,146,026✔
603
  rspMsg.pCont = pRsp;
20,146,026✔
604
  rspMsg.contLen = rspSize;
20,146,026✔
605
  rspMsg.code = code;
20,146,026✔
606
  rspMsg.msgType = pMsg->msgType;
20,146,026✔
607

608
  if (code) {
20,146,026✔
609
    qError("vnd get batch meta failed cause of %s", tstrerror(code));
×
610
  }
611

612
  taosArrayDestroyEx(batchReq.pMsgs, tFreeSBatchReqMsg);
20,146,026✔
613
  taosArrayDestroyEx(batchRsp.pRsps, tFreeSBatchRspMsg);
20,145,398✔
614

615
  tmsgSendRsp(&rspMsg);
20,145,669✔
616

617
  return code;
20,147,234✔
618
}
619

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

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

637
  SArray *pList = taosArrayInit(10, sizeof(uint64_t));
×
638
  QUERY_CHECK_NULL(pList, code, line, _return, terrno);
×
639

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

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

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

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

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

668
    pTb = taosMemoryCalloc(1, refColsNum * sizeof(SRefColInfo) + sizeof(*pTb));
×
669
    QUERY_CHECK_NULL(pTb, code, line, _return, terrno);
×
670

671
    pTb->uid = mr.me.uid;
×
672
    pTb->numOfColRefs = refColsNum;
×
673
    pTb->refCols = (SRefColInfo*)(pTb + 1);
×
674

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

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

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

689
      if (NULL == tSimpleHashGet(pSrcTbls, tbFName, strlen(tbFName))) {
×
690
        QUERY_CHECK_CODE(tSimpleHashPut(pSrcTbls, tbFName, strlen(tbFName), &code, sizeof(code)), line, _return);
×
691
      }
692

693
      refColsNum++;
×
694
    }
695

696
    pTb->numOfSrcTbls = tSimpleHashGetSize(pSrcTbls);
×
697
    QUERY_CHECK_NULL(taosArrayPush(*ppRes, &pTb), code, line, _return, terrno);
×
698
    pTb = NULL;
×
699

700
    pHandle->api.metaReaderFn.clearReader(&mr);
×
701
    readerInit = false;
×
702
  }
703

704
_return:
×
705

706
  if (readerInit) {
×
707
    pHandle->api.metaReaderFn.clearReader(&mr);
×
708
  }
709

710
  taosArrayDestroy(pList);
×
711
  taosMemoryFree(pTb);
×
712
  tSimpleHashCleanup(pSrcTbls);
×
713

714
  if (code) {
×
715
    qError("%s failed since %s", __func__, tstrerror(code));
×
716
  }
717
  return code;
×
718
}
719

720
int32_t vnodeReadVStbRefDbs(SReadHandle* pHandle, int64_t suid, SArray** ppRes) {
45,665✔
721
  int32_t                    code = TSDB_CODE_SUCCESS;
45,665✔
722
  int32_t                    line = 0;
45,665✔
723
  SMetaReader                mr = {0};
45,665✔
724
  bool                       readerInit = false;
45,665✔
725
  SSHashObj*                 pDbNameHash = NULL;
45,665✔
726
  SArray*                    pList = NULL;
45,665✔
727

728
  pList = taosArrayInit(10, sizeof(uint64_t));
45,665✔
729
  QUERY_CHECK_NULL(pList, code, line, _return, terrno);
45,665✔
730

731
  *ppRes = taosArrayInit(10, POINTER_BYTES);
45,665✔
732
  QUERY_CHECK_NULL(*ppRes, code, line, _return, terrno)
45,665✔
733
  
734
  // lookup in cache
735
  code = pHandle->api.metaFn.metaGetCachedRefDbs(pHandle->vnode, suid, *ppRes);
45,665✔
736
  QUERY_CHECK_CODE(code, line, _return);
46,261✔
737

738
  if (taosArrayGetSize(*ppRes) > 0) {
46,261✔
739
    // found in cache
740
    goto _return;
26,373✔
741
  } else {
742
    code = pHandle->api.metaFn.getChildTableList(pHandle->vnode, suid, pList);
19,888✔
743
    QUERY_CHECK_CODE(code, line, _return);
19,888✔
744

745
    size_t num = taosArrayGetSize(pList);
19,888✔
746
    pDbNameHash = tSimpleHashInit(10, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
19,888✔
747
    QUERY_CHECK_NULL(pDbNameHash, code, line, _return, terrno);
19,888✔
748

749
    for (int32_t i = 0; i < num; ++i) {
61,349✔
750
      uint64_t* id = taosArrayGet(pList, i);
41,461✔
751
      QUERY_CHECK_NULL(id, code, line, _return, terrno);
41,461✔
752

753
      pHandle->api.metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pHandle->api.metaFn);
41,461✔
754
      readerInit = true;
41,461✔
755

756
      code = pHandle->api.metaReaderFn.getTableEntryByUid(&mr, *id);
41,461✔
757
      QUERY_CHECK_CODE(code, line, _return);
41,461✔
758

759
      for (int32_t j = 0; j < mr.me.colRef.nCols; j++) {
30,790,930✔
760
        if (mr.me.colRef.pColRef[j].hasRef) {
30,749,469✔
761
          if (NULL == tSimpleHashGet(pDbNameHash, mr.me.colRef.pColRef[j].refDbName, strlen(mr.me.colRef.pColRef[j].refDbName))) {
30,668,984✔
762
            char *refDbName = taosStrdup(mr.me.colRef.pColRef[j].refDbName);
13,564✔
763
            QUERY_CHECK_NULL(refDbName, code, line, _return, terrno);
13,564✔
764

765
            QUERY_CHECK_NULL(taosArrayPush(*ppRes, &refDbName), code, line, _return, terrno);
27,128✔
766

767
            code = tSimpleHashPut(pDbNameHash, refDbName, strlen(refDbName), NULL, 0);
13,564✔
768
            QUERY_CHECK_CODE(code, line, _return);
13,564✔
769
          }
770
        }
771
      }
772

773
      pHandle->api.metaReaderFn.clearReader(&mr);
41,461✔
774
      readerInit = false;
41,461✔
775
    }
776

777
    code = pHandle->api.metaFn.metaPutRefDbsToCache(pHandle->vnode, suid, *ppRes);
19,888✔
778
    QUERY_CHECK_CODE(code, line, _return);
19,888✔
779
  }
780

781
_return:
46,261✔
782

783
  if (readerInit) {
46,261✔
784
    pHandle->api.metaReaderFn.clearReader(&mr);
×
785
  }
786

787
  taosArrayDestroy(pList);
46,261✔
788
  tSimpleHashCleanup(pDbNameHash);
45,665✔
789

790
  if (code) {
46,261✔
791
    qError("%s failed since %s", __func__, tstrerror(code));
×
792
  }
793
  return code;
46,261✔
794
}
795

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

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

811
  SReadHandle handle = {0};
×
812
  handle.vnode = pVnode;
×
813
  initStorageAPI(&handle.api);
×
814

815
  QUERY_CHECK_CODE(vnodeReadVSubtables(&handle, req.suid, &rsp.pTables), line, _return);
×
816
  rsp.vgId = TD_VID(pVnode);
×
817

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

837
_return:
×
838

839
  rspMsg.info = pMsg->info;
×
840
  rspMsg.pCont = pRsp;
×
841
  rspMsg.contLen = rspSize;
×
842
  rspMsg.code = code;
×
843
  rspMsg.msgType = pMsg->msgType;
×
844

845
  if (code) {
×
846
    qError("vnd get virtual subtables failed cause of %s", tstrerror(code));
×
847
  }
848

849
  *pMsg = rspMsg;
×
850
  
851
  tDestroySVSubTablesRsp(&rsp);
×
852

853
  //tmsgSendRsp(&rspMsg);
854

855
  return code;
×
856
}
857

858
int32_t vnodeGetVStbRefDbs(SVnode *pVnode, SRpcMsg *pMsg) {
45,534✔
859
  int32_t        code = 0;
45,534✔
860
  int32_t        rspSize = 0;
45,534✔
861
  SVStbRefDbsReq req = {0};
45,534✔
862
  SVStbRefDbsRsp rsp = {0};
45,534✔
863
  SRpcMsg        rspMsg = {0};
45,534✔
864
  void          *pRsp = NULL;
45,534✔
865
  int32_t        line = 0;
45,534✔
866

867
  if (tDeserializeSVStbRefDbsReq(pMsg->pCont, pMsg->contLen, &req)) {
45,534✔
868
    code = terrno;
×
869
    qError("tDeserializeSVSubTablesReq failed");
×
870
    goto _return;
×
871
  }
872

873
  SReadHandle handle = {0};
46,261✔
874
  handle.vnode = pVnode;
46,261✔
875
  initStorageAPI(&handle.api);
46,261✔
876

877
  code = vnodeReadVStbRefDbs(&handle, req.suid, &rsp.pDbs);
46,261✔
878
  QUERY_CHECK_CODE(code, line, _return);
45,665✔
879
  rsp.vgId = TD_VID(pVnode);
45,665✔
880

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

900
_return:
46,261✔
901

902
  rspMsg.info = pMsg->info;
46,261✔
903
  rspMsg.pCont = pRsp;
46,261✔
904
  rspMsg.contLen = rspSize;
46,261✔
905
  rspMsg.code = code;
46,261✔
906
  rspMsg.msgType = pMsg->msgType;
46,261✔
907

908
  if (code) {
45,665✔
909
    qError("vnd get virtual stb ref db failed cause of %s", tstrerror(code));
×
910
  }
911

912
  *pMsg = rspMsg;
45,665✔
913

914
  tDestroySVStbRefDbsRsp(&rsp);
45,665✔
915

916
  return code;
45,665✔
917
}
918

919
static int32_t vnodeGetCompStorage(SVnode *pVnode, int64_t *output) {
110,474,650✔
920
  int32_t code = 0;
110,474,650✔
921
#ifdef TD_ENTERPRISE
922
  int32_t now = taosGetTimestampSec();
110,474,650✔
923
  if (llabs(now - pVnode->config.vndStats.storageLastUpd) >= 30) {
110,474,650✔
924
    pVnode->config.vndStats.storageLastUpd = now;
5,930,526✔
925

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

945
static void vnodeGetBufferInfo(SVnode *pVnode, int64_t *bufferSegmentUsed, int64_t *bufferSegmentSize) {
110,474,650✔
946
  *bufferSegmentUsed = 0;
110,474,650✔
947
  *bufferSegmentSize = 0;
110,474,650✔
948
  if (pVnode) {
110,474,650✔
949
    (void)taosThreadMutexLock(&pVnode->mutex);
110,474,650✔
950

951
    if (pVnode->inUse) {
110,474,650✔
952
      *bufferSegmentUsed = pVnode->inUse->size;
110,465,458✔
953
    }
954
    *bufferSegmentSize = pVnode->config.szBuf / VNODE_BUFPOOL_SEGMENTS;
110,474,650✔
955

956
    (void)taosThreadMutexUnlock(&pVnode->mutex);
110,474,650✔
957
  }
958
}
110,474,650✔
959

960
int32_t vnodeGetLoad(SVnode *pVnode, SVnodeLoad *pLoad) {
110,474,650✔
961
  SSyncState state = syncGetState(pVnode->sync);
110,474,650✔
962
  pLoad->syncAppliedIndex = pVnode->state.applied;
110,474,650✔
963
  syncGetCommitIndex(pVnode->sync, &pLoad->syncCommitIndex);
110,474,650✔
964

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

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

1012
void vnodeGetInfo(void *pVnode, const char **dbname, int32_t *vgId, int64_t *numOfTables, int64_t *numOfNormalTables) {
6,562,353✔
1013
  SVnode    *pVnodeObj = pVnode;
6,562,353✔
1014
  SVnodeCfg *pConf = &pVnodeObj->config;
6,562,353✔
1015

1016
  if (dbname) {
6,563,698✔
1017
    *dbname = pConf->dbname;
6,547,646✔
1018
  }
1019

1020
  if (vgId) {
6,563,086✔
1021
    *vgId = TD_VID(pVnodeObj);
5,967,272✔
1022
  }
1023

1024
  if (numOfTables) {
6,563,698✔
1025
    *numOfTables = pConf->vndStats.numOfNTables + pConf->vndStats.numOfCTables +
18,788✔
1026
                   pConf->vndStats.numOfVTables + pConf->vndStats.numOfVCTables;
9,394✔
1027
  }
1028

1029
  if (numOfNormalTables) {
6,563,698✔
1030
    *numOfNormalTables = pConf->vndStats.numOfNTables +
13,316✔
1031
                         pConf->vndStats.numOfVTables;
6,658✔
1032
  }
1033
}
6,563,698✔
1034

1035
int32_t vnodeGetTableList(void *pVnode, int8_t type, SArray *pList) {
6,658✔
1036
  if (type == TSDB_SUPER_TABLE) {
6,658✔
1037
    return vnodeGetStbIdList(pVnode, 0, pList);
6,658✔
1038
  } else {
1039
    return TSDB_CODE_INVALID_PARA;
×
1040
  }
1041
}
1042

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

1051
  while (1) {
×
1052
    tb_uid_t id = metaCtbCursorNext(pCur);
×
1053
    if (id == 0) {
×
1054
      break;
×
1055
    }
1056

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

1069
int32_t vnodeGetCtbIdListByFilter(SVnode *pVnode, int64_t suid, SArray *list, bool (*filter)(void *arg), void *arg) {
×
1070
  return 0;
×
1071
}
1072

1073
int32_t vnodeGetCtbIdList(void *pVnode, int64_t suid, SArray *list) {
78,750,802✔
1074
  int32_t      code = TSDB_CODE_SUCCESS;
78,750,802✔
1075
  SVnode      *pVnodeObj = pVnode;
78,750,802✔
1076
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnodeObj, suid, 1);
78,750,802✔
1077
  if (NULL == pCur) {
78,741,116✔
1078
    qError("vnode get all table list failed");
×
1079
    return terrno;
×
1080
  }
1081

1082
  while (1) {
400,707,270✔
1083
    tb_uid_t id = metaCtbCursorNext(pCur);
479,448,386✔
1084
    if (id == 0) {
479,343,827✔
1085
      break;
78,778,407✔
1086
    }
1087
    qTrace("vnodeGetCtbIdList: got ctb id %" PRId64 " for suid %" PRId64, id, suid);
400,565,420✔
1088
    if (NULL == taosArrayPush(list, &id)) {
400,693,067✔
1089
      qError("taosArrayPush failed");
×
1090
      code = terrno;
×
1091
      goto _exit;
×
1092
    }
1093
  }
1094

1095
_exit:
78,694,232✔
1096
  metaCloseCtbCursor(pCur);
78,694,232✔
1097
  return code;
78,747,426✔
1098
}
1099

1100
int32_t vnodeGetStbIdList(SVnode *pVnode, int64_t suid, SArray *list) {
72,141,799✔
1101
  int32_t      code = TSDB_CODE_SUCCESS;
72,141,799✔
1102
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, suid);
72,141,799✔
1103
  if (!pCur) {
72,146,666✔
1104
    return TSDB_CODE_OUT_OF_MEMORY;
×
1105
  }
1106

1107
  while (1) {
52,555,459✔
1108
    tb_uid_t id = metaStbCursorNext(pCur);
124,702,125✔
1109
    if (id == 0) {
124,681,036✔
1110
      break;
72,152,174✔
1111
    }
1112

1113
    if (NULL == taosArrayPush(list, &id)) {
52,547,834✔
1114
      qError("taosArrayPush failed");
×
1115
      code = terrno;
×
1116
      goto _exit;
×
1117
    }
1118
  }
1119

1120
_exit:
72,149,726✔
1121
  metaCloseStbCursor(pCur);
72,149,726✔
1122
  return code;
72,142,341✔
1123
}
1124

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

1133
  while (1) {
×
1134
    tb_uid_t id = metaStbCursorNext(pCur);
×
1135
    if (id == 0) {
×
1136
      break;
×
1137
    }
1138

1139
    if ((*filter) && (*filter)(arg, &id)) {
×
1140
      continue;
×
1141
    }
1142

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

1150
_exit:
×
1151
  metaCloseStbCursor(pCur);
×
1152
  return code;
×
1153
}
1154

1155
int32_t vnodeGetCtbNum(SVnode *pVnode, int64_t suid, int64_t *num) {
4,182,213✔
1156
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, suid, 0);
4,182,213✔
1157
  if (!pCur) {
4,182,167✔
1158
    return terrno;
×
1159
  }
1160

1161
  *num = 0;
4,182,167✔
1162
  while (1) {
2,375,792✔
1163
    tb_uid_t id = metaCtbCursorNext(pCur);
6,558,277✔
1164
    if (id == 0) {
6,557,578✔
1165
      break;
4,182,352✔
1166
    }
1167

1168
    ++(*num);
2,375,226✔
1169
  }
1170

1171
  metaCloseCtbCursor(pCur);
4,182,352✔
1172
  return TSDB_CODE_SUCCESS;
4,182,612✔
1173
}
1174

1175
int32_t vnodeGetStbColumnNum(SVnode *pVnode, tb_uid_t suid, int *num) {
4,182,313✔
1176
  SSchemaWrapper *pSW = metaGetTableSchema(pVnode->pMeta, suid, -1, 0, NULL, 0);
4,182,313✔
1177
  if (pSW) {
4,181,958✔
1178
    *num = pSW->nCols;
4,181,579✔
1179
    tDeleteSchemaWrapper(pSW);
1180
  } else {
1181
    *num = 2;
379✔
1182
  }
1183

1184
  return TSDB_CODE_SUCCESS;
4,181,681✔
1185
}
1186

1187
int32_t vnodeGetStbInfo(SVnode *pVnode, tb_uid_t suid, int64_t *keep, int8_t *flags) {
4,181,958✔
1188
  SMetaReader mr = {0};
4,181,958✔
1189
  metaReaderDoInit(&mr, pVnode->pMeta, META_READER_NOLOCK);
4,181,958✔
1190

1191
  int32_t code = metaReaderGetTableEntryByUid(&mr, suid);
4,181,661✔
1192
  if (code == TSDB_CODE_SUCCESS) {
4,182,213✔
1193
    if (keep) *keep = mr.me.stbEntry.keep;
4,181,834✔
1194
    if (flags) *flags = mr.me.flags;
4,181,834✔
1195
  } else {
1196
    if (keep) *keep = 0;
379✔
1197
    if (flags) *flags = 0;
379✔
1198
  }
1199

1200
  metaReaderClear(&mr);
4,183,170✔
1201
  return TSDB_CODE_SUCCESS;
4,183,165✔
1202
}
1203

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

1228
// exclude stbs of taoskeeper log
1229
static int32_t vnodeGetTimeSeriesBlackList(SVnode *pVnode, int32_t *tbSize) {
72,136,725✔
1230
  int32_t      code = TSDB_CODE_SUCCESS;
72,136,725✔
1231
  int32_t      tbNum = 0;
72,136,725✔
1232
  const char **pTbArr = NULL;
72,136,725✔
1233
  const char  *dbName = NULL;
72,136,725✔
1234
  *tbSize = 0;
72,136,725✔
1235

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

1260
  return code;
72,136,725✔
1261
}
1262
#endif
1263

1264
static bool vnodeTimeSeriesFilter(void *arg1, void *arg2) {
×
1265
  SVnode *pVnode = (SVnode *)arg1;
×
1266

1267
  if (metaTbInFilterCache(pVnode->pMeta, arg2, 0)) {
×
1268
    return true;
×
1269
  }
1270
  return false;
×
1271
}
1272

1273
int32_t vnodeGetTimeSeriesNum(SVnode *pVnode, int64_t *num) {
72,135,501✔
1274
  SArray *suidList = NULL;
72,135,501✔
1275

1276
  if (!(suidList = taosArrayInit(1, sizeof(tb_uid_t)))) {
72,135,501✔
1277
    return terrno;
×
1278
  }
1279

1280
  int32_t tbFilterSize = 0;
72,129,952✔
1281
  int32_t code = TSDB_CODE_SUCCESS;
72,133,665✔
1282
#ifdef TD_ENTERPRISE
1283
  code = vnodeGetTimeSeriesBlackList(pVnode, &tbFilterSize);
72,133,665✔
1284
  if (TSDB_CODE_SUCCESS != code) {
72,132,681✔
1285
    goto _exit;
×
1286
  }
1287
#endif
1288

1289
  if ((!tbFilterSize && vnodeGetStbIdList(pVnode, 0, suidList) < 0) ||
72,132,681✔
1290
      (tbFilterSize && vnodeGetStbIdListByFilter(pVnode, 0, suidList, vnodeTimeSeriesFilter, pVnode) < 0)) {
72,130,564✔
1291
    qError("vgId:%d, failed to get stb id list error: %s", TD_VID(pVnode), terrstr());
×
1292
    taosArrayDestroy(suidList);
×
1293
    return terrno;
×
1294
  }
1295

1296
  *num = 0;
72,129,059✔
1297
  int64_t arrSize = taosArrayGetSize(suidList);
72,137,296✔
1298
  for (int64_t i = 0; i < arrSize; ++i) {
124,694,895✔
1299
    tb_uid_t suid = *(tb_uid_t *)taosArrayGet(suidList, i);
52,548,047✔
1300

1301
    int64_t ctbNum = 0;
52,557,227✔
1302
    int32_t numOfCols = 0;
52,559,675✔
1303
    int8_t  flags = 0;
52,551,107✔
1304
    code = metaGetStbStats(pVnode, suid, &ctbNum, &numOfCols, &flags);
52,557,558✔
1305
    if (TSDB_CODE_SUCCESS != code) {
52,569,186✔
1306
      goto _exit;
×
1307
    }
1308
    if (!TABLE_IS_VIRTUAL(flags)) {
52,569,186✔
1309
      *num += ctbNum * (numOfCols - 1);
50,744,397✔
1310
    }
1311
  }
1312

1313
_exit:
72,149,908✔
1314
  taosArrayDestroy(suidList);
72,146,848✔
1315
  return TSDB_CODE_SUCCESS;
72,136,364✔
1316
}
1317

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

1324
  *num = 0;
×
1325
  while (1) {
×
1326
    tb_uid_t id = metaStbCursorNext(pCur);
×
1327
    if (id == 0) {
×
1328
      break;
×
1329
    }
1330

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

1338
    *num += ctbNum;
×
1339
  }
1340

1341
  metaCloseStbCursor(pCur);
×
1342
  return TSDB_CODE_SUCCESS;
×
1343
}
1344

1345
void *vnodeGetIdx(void *pVnode) {
3,897,383✔
1346
  if (pVnode == NULL) {
3,897,383✔
1347
    return NULL;
×
1348
  }
1349

1350
  return metaGetIdx(((SVnode *)pVnode)->pMeta);
3,897,383✔
1351
}
1352

1353
void *vnodeGetIvtIdx(void *pVnode) {
3,897,383✔
1354
  if (pVnode == NULL) {
3,897,383✔
1355
    return NULL;
×
1356
  }
1357
  return metaGetIvtIdx(((SVnode *)pVnode)->pMeta);
3,897,383✔
1358
}
1359

1360
int32_t vnodeGetTableSchema(void *pVnode, int64_t uid, STSchema **pSchema, int64_t *suid, SSchemaWrapper **pTagSchema) {
68,066✔
1361
  return tsdbGetTableSchema(((SVnode *)pVnode)->pMeta, uid, pSchema, suid, pTagSchema);
68,066✔
1362
}
1363

1364
static FORCE_INLINE int32_t vnodeGetDBPrimaryInfo(SVnode *pVnode, SDbSizeStatisInfo *pInfo) {
1365
  int32_t code = 0;
5,936,024✔
1366
  char    path[TSDB_FILENAME_LEN] = {0};
5,936,024✔
1367

1368
  char   *dirName[] = {VNODE_TSDB_DIR, VNODE_WAL_DIR, VNODE_META_DIR, VNODE_TSDB_CACHE_DIR};
5,936,024✔
1369
  int64_t dirSize[4];
5,935,512✔
1370

1371
  vnodeGetPrimaryPath(pVnode, false, path, TSDB_FILENAME_LEN);
5,936,024✔
1372
  int32_t offset = strlen(path);
5,936,024✔
1373

1374
  for (int i = 0; i < sizeof(dirName) / sizeof(dirName[0]); i++) {
29,680,120✔
1375
    int64_t size = {0};
23,744,096✔
1376
    (void)snprintf(path + offset, TSDB_FILENAME_LEN - offset, "%s%s", TD_DIRSEP, dirName[i]);
23,744,096✔
1377
    code = taosGetDirSize(path, &size);
23,744,096✔
1378
    if (code != 0) {
23,744,096✔
1379
      uWarn("vnode %d get dir %s %s size failed since %s", TD_VID(pVnode), path, dirName[i], tstrerror(code));
3,230✔
1380
    }
1381
    path[offset] = 0;
23,744,096✔
1382
    dirSize[i] = size;
23,744,096✔
1383
  }
1384

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

1401
  code = tsdbGetFsSize(pVnodeObj->pTsdb, pInfo);
5,936,024✔
1402
_exit:
5,936,024✔
1403
  return code;
5,936,024✔
1404
}
1405

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

1414
  SVnode      *pVnode1 = (SVnode *)pVnode;
×
1415
  SSyncMetrics syncMetrics = syncGetMetrics(pVnode1->sync);
×
1416

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

1438
  return 0;
×
1439
}
1440

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

1449
  SVnode *pVnode1 = (SVnode *)pVnode;
×
1450

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

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

1463
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.commit_time, pOldMetrics->commit_time);
×
1464
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.merge_time, pOldMetrics->merge_time);
×
1465

1466
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.memtable_wait_time, pOldMetrics->memtable_wait_time);
×
1467
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.blocked_commit_count, pOldMetrics->blocked_commit_count);
×
1468
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.block_commit_time, pOldMetrics->blocked_commit_time);
×
1469
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.merge_count, pOldMetrics->merge_count);
×
1470

1471
  // Reset new cache metrics
1472
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.last_cache_commit_time, pOldMetrics->last_cache_commit_time);
×
1473
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.last_cache_commit_count, pOldMetrics->last_cache_commit_count);
×
1474

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

1482
  return 0;
×
1483
}
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