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

taosdata / TDengine / #4868

26 Nov 2025 05:46AM UTC coverage: 64.629% (+0.2%) from 64.473%
#4868

push

travis-ci

guanshengliang
Merge branch '3.0' into cover/3.0

770 of 945 new or added lines in 33 files covered. (81.48%)

3010 existing lines in 120 files now uncovered.

158425 of 245129 relevant lines covered (64.63%)

113048242.66 hits per line

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

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

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

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

36
int32_t fillTableColCmpr(SMetaReader *reader, SSchemaExt *pExt, int32_t numOfCol) {
19,454,049✔
37
  int8_t tblType = reader->me.type;
19,454,049✔
38
  if (withExtSchema(tblType)) {
19,454,049✔
39
    SColCmprWrapper *p = &(reader->me.colCmpr);
19,196,743✔
40
    if (numOfCol != p->nCols) {
19,196,743✔
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++) {
237,378,594✔
45
      SColCmpr *pCmpr = &p->pColCmpr[i];
218,181,195✔
46
      pExt[i].colId = pCmpr->id;
218,181,851✔
47
      pExt[i].compress = pCmpr->alg;
218,181,172✔
48
    }
49
  }
50
  return 0;
19,454,049✔
51
}
52

53
void vnodePrintTableMeta(STableMetaRsp *pMeta) {
19,453,601✔
54
  if (!(qDebugFlag & DEBUG_DEBUG)) {
19,453,601✔
55
    return;
108,845✔
56
  }
57

58
  qDebug("tbName:%s", pMeta->tbName);
19,344,756✔
59
  qDebug("stbName:%s", pMeta->stbName);
19,345,204✔
60
  qDebug("dbFName:%s", pMeta->dbFName);
19,345,204✔
61
  qDebug("dbId:%" PRId64, pMeta->dbId);
19,345,204✔
62
  qDebug("numOfTags:%d", pMeta->numOfTags);
19,345,204✔
63
  qDebug("numOfColumns:%d", pMeta->numOfColumns);
19,345,204✔
64
  qDebug("precision:%d", pMeta->precision);
19,345,204✔
65
  qDebug("tableType:%d", pMeta->tableType);
19,345,204✔
66
  qDebug("sversion:%d", pMeta->sversion);
19,345,204✔
67
  qDebug("tversion:%d", pMeta->tversion);
19,345,204✔
68
  qDebug("suid:%" PRIu64, pMeta->suid);
19,345,204✔
69
  qDebug("tuid:%" PRIu64, pMeta->tuid);
19,345,204✔
70
  qDebug("vgId:%d", pMeta->vgId);
19,345,204✔
71
  qDebug("sysInfo:%d", pMeta->sysInfo);
19,345,204✔
72
  if (pMeta->pSchemas) {
19,345,204✔
73
    for (int32_t i = 0; i < (pMeta->numOfColumns + pMeta->numOfTags); ++i) {
276,983,810✔
74
      SSchema *pSchema = pMeta->pSchemas + i;
257,638,606✔
75
      qDebug("%d col/tag: type:%d, flags:%d, colId:%d, bytes:%d, name:%s", i, pSchema->type, pSchema->flags,
257,638,606✔
76
             pSchema->colId, pSchema->bytes, pSchema->name);
77
    }
78
  }
79
}
80

81
int32_t fillTableColRef(SMetaReader *reader, SColRef *pRef, int32_t numOfCol) {
257,306✔
82
  int8_t tblType = reader->me.type;
257,306✔
83
  if (hasRefCol(tblType)) {
257,306✔
84
    SColRefWrapper *p = &(reader->me.colRef);
257,306✔
85
    if (numOfCol != p->nCols) {
257,306✔
86
      vError("fillTableColRef table type:%d, col num:%d, col cmpr num:%d mismatch", tblType, numOfCol, p->nCols);
×
87
      return TSDB_CODE_APP_ERROR;
×
88
    }
89
    for (int i = 0; i < p->nCols; i++) {
2,076,354✔
90
      SColRef *pColRef = &p->pColRef[i];
1,819,048✔
91
      pRef[i].hasRef = pColRef->hasRef;
1,819,048✔
92
      pRef[i].id = pColRef->id;
1,819,048✔
93
      if(pRef[i].hasRef) {
1,819,048✔
94
        tstrncpy(pRef[i].refDbName, pColRef->refDbName, TSDB_DB_NAME_LEN);
1,294,018✔
95
        tstrncpy(pRef[i].refTableName, pColRef->refTableName, TSDB_TABLE_NAME_LEN);
1,294,018✔
96
        tstrncpy(pRef[i].refColName, pColRef->refColName, TSDB_COL_NAME_LEN);
1,294,018✔
97
      }
98
    }
99
  }
100
  return 0;
257,306✔
101
}
102

103
int32_t vnodeGetTableMeta(SVnode *pVnode, SRpcMsg *pMsg, bool direct) {
31,104,132✔
104
  STableInfoReq  infoReq = {0};
31,104,132✔
105
  STableMetaRsp  metaRsp = {0};
31,104,579✔
106
  SMetaReader    mer1 = {0};
31,102,820✔
107
  SMetaReader    mer2 = {0};
31,103,523✔
108
  char           tableFName[TSDB_TABLE_FNAME_LEN];
31,078,756✔
109
  bool           reqTbUid = false;
31,103,076✔
110
  SRpcMsg        rpcMsg = {0};
31,103,076✔
111
  int32_t        code = 0;
31,103,076✔
112
  int32_t        rspLen = 0;
31,103,076✔
113
  void          *pRsp = NULL;
31,103,076✔
114
  SSchemaWrapper schema = {0};
31,103,076✔
115
  SSchemaWrapper schemaTag = {0};
31,103,076✔
116
  uint8_t        autoCreateCtb = 0;
31,103,076✔
117

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

125
  if (infoReq.option == REQ_OPT_TBUID) reqTbUid = true;
31,103,684✔
126
  metaRsp.dbId = pVnode->config.dbId;
31,103,684✔
127
  tstrncpy(metaRsp.tbName, infoReq.tbName, TSDB_TABLE_NAME_LEN);
31,102,628✔
128
  (void)memcpy(metaRsp.dbFName, infoReq.dbFName, sizeof(metaRsp.dbFName));
31,102,628✔
129

130
  if (!reqTbUid) {
31,102,628✔
131
    (void)tsnprintf(tableFName, TSDB_TABLE_FNAME_LEN, "%s.%s", infoReq.dbFName, infoReq.tbName);
30,547,026✔
132
    if (pVnode->mounted) tTrimMountPrefix(tableFName);
30,547,921✔
133
    code = vnodeValidateTableHash(pVnode, tableFName);
30,548,129✔
134
    if (code) {
30,547,682✔
135
      goto _exit4;
×
136
    }
137
  }
138

139
  // query meta
140
  metaReaderDoInit(&mer1, pVnode->pMeta, META_READER_LOCK);
31,103,284✔
141
  if (reqTbUid) {
31,104,579✔
142
    SET_ERRNO(0);
556,002✔
143
    uint64_t tbUid = taosStr2UInt64(infoReq.tbName, NULL, 10);
556,002✔
144
    if (ERRNO == ERANGE || tbUid == 0) {
556,002✔
145
      code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
146
      goto _exit3;
×
147
    }
148
    SMetaReader mr3 = {0};
555,634✔
149
    metaReaderDoInit(&mr3, ((SVnode *)pVnode)->pMeta, META_READER_NOLOCK);
555,634✔
150
    if ((code = metaReaderGetTableEntryByUid(&mr3, tbUid)) < 0) {
556,002✔
151
      metaReaderClear(&mr3);
217,810✔
152
      TAOS_CHECK_GOTO(code, NULL, _exit3);
217,810✔
153
    }
154
    tstrncpy(metaRsp.tbName, mr3.me.name, TSDB_TABLE_NAME_LEN);
338,192✔
155
    metaReaderClear(&mr3);
338,192✔
156
    TAOS_CHECK_GOTO(metaGetTableEntryByName(&mer1, metaRsp.tbName), NULL, _exit3);
338,192✔
157
  } else if (metaGetTableEntryByName(&mer1, infoReq.tbName) < 0) {
30,548,577✔
158
    code = terrno;
11,432,720✔
159
    goto _exit3;
11,432,720✔
160
  }
161

162
  metaRsp.tableType = mer1.me.type;
19,453,601✔
163
  metaRsp.vgId = TD_VID(pVnode);
19,453,601✔
164
  metaRsp.tuid = mer1.me.uid;
19,453,601✔
165

166
  switch (mer1.me.type) {
19,453,601✔
167
    case TSDB_SUPER_TABLE: {
3,512,797✔
168
      (void)strcpy(metaRsp.stbName, mer1.me.name);
3,512,797✔
169
      schema = mer1.me.stbEntry.schemaRow;
3,512,797✔
170
      schemaTag = mer1.me.stbEntry.schemaTag;
3,512,797✔
171
      metaRsp.suid = mer1.me.uid;
3,512,797✔
172
      break;
3,512,797✔
173
    }
174
    case TSDB_CHILD_TABLE:
12,765,057✔
175
    case TSDB_VIRTUAL_CHILD_TABLE:{
176
      metaReaderDoInit(&mer2, pVnode->pMeta, META_READER_NOLOCK);
12,765,057✔
177
      if (metaReaderGetTableEntryByUid(&mer2, mer1.me.ctbEntry.suid) < 0) goto _exit2;
12,765,057✔
178

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

196
  metaRsp.numOfTags = schemaTag.nCols;
19,454,049✔
197
  metaRsp.numOfColumns = schema.nCols;
19,454,049✔
198
  metaRsp.precision = pVnode->config.tsdbCfg.precision;
19,454,049✔
199
  metaRsp.sversion = schema.version;
19,454,049✔
200
  metaRsp.tversion = schemaTag.version;
19,454,049✔
201
  metaRsp.pSchemas = (SSchema *)taosMemoryMalloc(sizeof(SSchema) * (metaRsp.numOfColumns + metaRsp.numOfTags));
19,454,049✔
202
  metaRsp.pSchemaExt = (SSchemaExt *)taosMemoryCalloc(metaRsp.numOfColumns, sizeof(SSchemaExt));
19,454,049✔
203
  if (NULL == metaRsp.pSchemas || NULL == metaRsp.pSchemaExt) {
19,453,604✔
204
    code = terrno;
×
205
    goto _exit;
×
206
  }
207
  (void)memcpy(metaRsp.pSchemas, schema.pSchema, sizeof(SSchema) * schema.nCols);
19,453,604✔
208
  if (schemaTag.nCols) {
19,453,604✔
209
    (void)memcpy(metaRsp.pSchemas + schema.nCols, schemaTag.pSchema, sizeof(SSchema) * schemaTag.nCols);
16,278,302✔
210
  }
211
  if (metaRsp.pSchemaExt) {
19,453,159✔
212
    SMetaReader *pReader = mer1.me.type == TSDB_CHILD_TABLE ? &mer2 : &mer1;
19,453,604✔
213
    code = fillTableColCmpr(pReader, metaRsp.pSchemaExt, metaRsp.numOfColumns);
19,453,604✔
214
    if (code < 0) {
19,454,049✔
215
      goto _exit;
×
216
    }
217
    for (int32_t i = 0; i < metaRsp.numOfColumns && pReader->me.pExtSchemas; i++) {
55,402,343✔
218
      metaRsp.pSchemaExt[i].typeMod = pReader->me.pExtSchemas[i].typeMod;
35,948,294✔
219
    }
220
  } else {
221
    code = TSDB_CODE_OUT_OF_MEMORY;
×
222
    goto _exit;
×
223
  }
224
  if (hasRefCol(mer1.me.type)) {
19,454,049✔
225
    metaRsp.rversion = mer1.me.colRef.version;
257,098✔
226
    metaRsp.pColRefs = (SColRef*)taosMemoryMalloc(sizeof(SColRef) * metaRsp.numOfColumns);
257,098✔
227
    if (metaRsp.pColRefs) {
257,306✔
228
      code = fillTableColRef(&mer1, metaRsp.pColRefs, metaRsp.numOfColumns);
257,306✔
229
      if (code < 0) {
257,306✔
230
        goto _exit;
×
231
      }
232
    }
233
    metaRsp.numOfColRefs = metaRsp.numOfColumns;
257,306✔
234
  } else {
235
    metaRsp.pColRefs = NULL;
19,196,295✔
236
    metaRsp.numOfColRefs = 0;
19,196,295✔
237
  }
238

239
  vnodePrintTableMeta(&metaRsp);
19,453,601✔
240

241
  // encode and send response
242
  rspLen = tSerializeSTableMetaRsp(NULL, 0, &metaRsp);
19,454,049✔
243
  if (rspLen < 0) {
19,454,049✔
244
    code = terrno;
×
245
    goto _exit;
×
246
  }
247

248
  if (direct) {
19,454,049✔
249
    pRsp = rpcMallocCont(rspLen);
268,155✔
250
  } else {
251
    pRsp = taosMemoryCalloc(1, rspLen);
19,185,894✔
252
  }
253

254
  if (pRsp == NULL) {
19,454,049✔
255
    code = terrno;
×
256
    goto _exit;
×
257
  }
258

259
  rspLen = tSerializeSTableMetaRsp(pRsp, rspLen, &metaRsp);
19,454,049✔
260
  if (rspLen < 0) {
19,454,049✔
261
    code = terrno;
×
262
    goto _exit;
×
263
  }
264

265
_exit:
19,453,604✔
266
  taosMemoryFree(metaRsp.pColRefs);
19,454,049✔
267
  taosMemoryFree(metaRsp.pSchemas);
19,453,434✔
268
  taosMemoryFree(metaRsp.pSchemaExt);
19,454,049✔
269
_exit2:
19,453,025✔
270
  metaReaderClear(&mer2);
19,453,681✔
271
_exit3:
31,103,923✔
272
  metaReaderClear(&mer1);
31,104,132✔
273
_exit4:
31,104,132✔
274
  rpcMsg.info = pMsg->info;
31,104,579✔
275
  rpcMsg.pCont = pRsp;
31,104,579✔
276
  rpcMsg.contLen = rspLen;
31,104,579✔
277
  rpcMsg.code = code;
31,104,579✔
278
  rpcMsg.msgType = pMsg->msgType;
31,104,579✔
279

280
  if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST && autoCreateCtb == 1) {
31,103,438✔
281
    code = TSDB_CODE_SUCCESS;
8,236,298✔
282
  }
283

284
  if (code) {
31,103,438✔
285
    qError("vgId:%d, get table %s meta with %" PRIu8 " failed cause of %s", pVnode->config.vgId, infoReq.tbName,
3,413,338✔
286
           infoReq.option, tstrerror(code));
287
  }
288

289
  if (direct) {
31,104,579✔
290
    tmsgSendRsp(&rpcMsg);
939,879✔
291
  } else {
292
    *pMsg = rpcMsg;
30,164,700✔
293
  }
294

295
  return code;
31,104,211✔
296
}
297

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

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

317
  tstrncpy(cfgRsp.tbName, cfgReq.tbName, TSDB_TABLE_NAME_LEN);
68,620✔
318
  (void)memcpy(cfgRsp.dbFName, cfgReq.dbFName, sizeof(cfgRsp.dbFName));
68,620✔
319

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

327
  // query meta
328
  metaReaderDoInit(&mer1, pVnode->pMeta, META_READER_LOCK);
68,620✔
329

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

335
  cfgRsp.tableType = mer1.me.type;
68,620✔
336

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

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

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

388
  if (NULL == cfgRsp.pSchemas || NULL == cfgRsp.pSchemaExt || NULL == cfgRsp.pColRefs) {
68,620✔
389
    code = terrno;
×
390
    goto _exit;
×
391
  }
392
  (void)memcpy(cfgRsp.pSchemas, schema.pSchema, sizeof(SSchema) * schema.nCols);
68,620✔
393
  if (schemaTag.nCols) {
68,620✔
394
    (void)memcpy(cfgRsp.pSchemas + schema.nCols, schemaTag.pSchema, sizeof(SSchema) * schemaTag.nCols);
38,932✔
395
  }
396

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

401
  if (withExtSchema(cfgRsp.tableType)) {
68,620✔
402
    for (int32_t i = 0; i < cfgRsp.numOfColumns; i++) {
297,647✔
403
      SColCmpr   *pCmpr = &pColCmpr->pColCmpr[i];
231,161✔
404
      SSchemaExt *pSchExt = cfgRsp.pSchemaExt + i;
231,161✔
405
      pSchExt->colId = pCmpr->id;
231,161✔
406
      pSchExt->compress = pCmpr->alg;
231,161✔
407
      if (pReader->me.pExtSchemas)
231,161✔
408
        pSchExt->typeMod = pReader->me.pExtSchemas[i].typeMod;
24,794✔
409
      else
410
        pSchExt->typeMod = 0;
206,367✔
411
    }
412
  }
413

414
  cfgRsp.virtualStb = false;
68,620✔
415
  if (hasRefCol(cfgRsp.tableType)) {
68,620✔
416
    for (int32_t i = 0; i < cfgRsp.numOfColumns; i++) {
30,670✔
417
      SColRef *pRef = &pColRef->pColRef[i];
28,536✔
418
      cfgRsp.pColRefs[i].hasRef = pRef->hasRef;
28,536✔
419
      cfgRsp.pColRefs[i].id = pRef->id;
28,536✔
420
      if (cfgRsp.pColRefs[i].hasRef) {
28,536✔
421
        tstrncpy(cfgRsp.pColRefs[i].refDbName, pRef->refDbName, TSDB_DB_NAME_LEN);
11,429✔
422
        tstrncpy(cfgRsp.pColRefs[i].refTableName, pRef->refTableName, TSDB_TABLE_NAME_LEN);
11,429✔
423
        tstrncpy(cfgRsp.pColRefs[i].refColName, pRef->refColName, TSDB_COL_NAME_LEN);
11,429✔
424
      }
425
    }
426
  }
427

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

435
  if (direct) {
68,620✔
436
    pRsp = rpcMallocCont(rspLen);
×
437
  } else {
438
    pRsp = taosMemoryCalloc(1, rspLen);
68,620✔
439
  }
440

441
  if (pRsp == NULL) {
68,620✔
442
    code = terrno;
×
443
    goto _exit;
×
444
  }
445

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

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

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

463
  if (direct) {
68,620✔
464
    tmsgSendRsp(&rpcMsg);
×
465
  } else {
466
    *pMsg = rpcMsg;
68,620✔
467
  }
468

469
  tFreeSTableCfgRsp(&cfgRsp);
68,620✔
470
  metaReaderClear(&mer2);
68,620✔
471
  metaReaderClear(&mer1);
68,620✔
472
  return code;
68,620✔
473
}
474

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

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

484
int32_t vnodeGetBatchMeta(SVnode *pVnode, SRpcMsg *pMsg) {
27,738,052✔
485
  int32_t      code = 0;
27,738,052✔
486
  int32_t      rspSize = 0;
27,738,052✔
487
  SBatchReq    batchReq = {0};
27,738,052✔
488
  SBatchMsg   *req = NULL;
27,737,590✔
489
  SBatchRspMsg rsp = {0};
27,737,590✔
490
  SBatchRsp    batchRsp = {0};
27,731,537✔
491
  SRpcMsg      reqMsg = *pMsg;
27,731,537✔
492
  SRpcMsg      rspMsg = {0};
27,732,106✔
493
  void        *pRsp = NULL;
27,733,420✔
494

495
  if (tDeserializeSBatchReq(pMsg->pCont, pMsg->contLen, &batchReq)) {
27,733,420✔
496
    code = terrno;
×
497
    qError("tDeserializeSBatchReq failed");
×
498
    goto _exit;
×
499
  }
500

501
  int32_t msgNum = taosArrayGetSize(batchReq.pMsgs);
27,737,021✔
502
  if (msgNum >= MAX_META_MSG_IN_BATCH) {
27,734,156✔
503
    code = TSDB_CODE_INVALID_MSG;
×
504
    qError("too many msgs %d in vnode batch meta req", msgNum);
×
505
    goto _exit;
×
506
  }
507

508
  batchRsp.pRsps = taosArrayInit(msgNum, sizeof(SBatchRspMsg));
27,734,156✔
509
  if (NULL == batchRsp.pRsps) {
27,732,049✔
510
    code = terrno;
×
511
    qError("taosArrayInit %d SBatchRspMsg failed", msgNum);
×
512
    goto _exit;
×
513
  }
514

515
  for (int32_t i = 0; i < msgNum; ++i) {
60,536,516✔
516
    req = taosArrayGet(batchReq.pMsgs, i);
32,800,604✔
517
    if (req == NULL) {
32,799,853✔
518
      code = terrno;
×
519
      goto _exit;
×
520
    }
521

522
    reqMsg.msgType = req->msgType;
32,799,853✔
523
    reqMsg.pCont = req->msg;
32,799,290✔
524
    reqMsg.contLen = req->msgLen;
32,795,222✔
525

526
    switch (req->msgType) {
32,798,360✔
527
      case TDMT_VND_TABLE_META:
29,608,251✔
528
        // error code has been set into reqMsg, no need to handle it here.
529
        if (TSDB_CODE_SUCCESS != vnodeGetTableMeta(pVnode, &reqMsg, false)) {
29,608,251✔
530
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
2,524,698✔
531
        }
532
        break;
29,608,698✔
533
      case TDMT_VND_TABLE_NAME:
556,002✔
534
        // error code has been set into reqMsg, no need to handle it here.
535
        if (TSDB_CODE_SUCCESS != vnodeGetTableMeta(pVnode, &reqMsg, false)) {
556,002✔
536
          qWarn("vnodeGetBatchName failed, msgType:%d", req->msgType);
217,810✔
537
        }
538
        break;
556,002✔
539
      case TDMT_VND_TABLE_CFG:
68,620✔
540
        // error code has been set into reqMsg, no need to handle it here.
541
        if (TSDB_CODE_SUCCESS != vnodeGetTableCfg(pVnode, &reqMsg, false)) {
68,620✔
542
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
×
543
        }
544
        break;
68,620✔
545
      case TDMT_VND_VSUBTABLES_META:
×
546
        // error code has been set into reqMsg, no need to handle it here.
547
        if (TSDB_CODE_SUCCESS != vnodeGetVSubtablesMeta(pVnode, &reqMsg)) {
×
548
          qWarn("vnodeGetVSubtablesMeta failed, msgType:%d", req->msgType);
×
549
        }
550
        break;
×
551
      case TDMT_VND_VSTB_REF_DBS:
2,567,284✔
552
        // error code has been set into reqMsg, no need to handle it here.
553
        if (TSDB_CODE_SUCCESS != vnodeGetVStbRefDbs(pVnode, &reqMsg)) {
2,567,284✔
554
          qWarn("vnodeGetVStbRefDbs failed, msgType:%d", req->msgType);
×
555
        }
556
        break;
2,564,128✔
557
      default:
×
558
        qError("invalid req msgType %d", req->msgType);
×
559
        reqMsg.code = TSDB_CODE_INVALID_MSG;
×
560
        reqMsg.pCont = NULL;
×
561
        reqMsg.contLen = 0;
×
562
        break;
×
563
    }
564

565
    rsp.msgIdx = req->msgIdx;
32,797,448✔
566
    rsp.reqType = reqMsg.msgType;
32,799,428✔
567
    rsp.msgLen = reqMsg.contLen;
32,799,428✔
568
    rsp.rspCode = reqMsg.code;
32,799,428✔
569
    rsp.msg = reqMsg.pCont;
32,799,428✔
570

571
    if (NULL == taosArrayPush(batchRsp.pRsps, &rsp)) {
65,599,892✔
572
      qError("taosArrayPush failed");
×
573
      code = terrno;
×
574
      goto _exit;
×
575
    }
576
  }
577

578
  rspSize = tSerializeSBatchRsp(NULL, 0, &batchRsp);
27,735,912✔
579
  if (rspSize < 0) {
27,729,886✔
580
    qError("tSerializeSBatchRsp failed");
×
581
    code = terrno;
×
582
    goto _exit;
×
583
  }
584
  pRsp = rpcMallocCont(rspSize);
27,729,886✔
585
  if (pRsp == NULL) {
27,728,028✔
586
    qError("rpcMallocCont %d failed", rspSize);
×
587
    code = terrno;
×
588
    goto _exit;
×
589
  }
590
  if (tSerializeSBatchRsp(pRsp, rspSize, &batchRsp) < 0) {
27,728,028✔
591
    qError("tSerializeSBatchRsp %d failed", rspSize);
×
592
    code = terrno;
×
593
    goto _exit;
×
594
  }
595

596
_exit:
27,734,795✔
597

598
  rspMsg.info = pMsg->info;
27,736,666✔
599
  rspMsg.pCont = pRsp;
27,734,193✔
600
  rspMsg.contLen = rspSize;
27,734,193✔
601
  rspMsg.code = code;
27,734,193✔
602
  rspMsg.msgType = pMsg->msgType;
27,734,193✔
603

604
  if (code) {
27,722,847✔
605
    qError("vnd get batch meta failed cause of %s", tstrerror(code));
×
606
  }
607

608
  taosArrayDestroyEx(batchReq.pMsgs, tFreeSBatchReqMsg);
27,722,847✔
609
  taosArrayDestroyEx(batchRsp.pRsps, tFreeSBatchRspMsg);
27,725,868✔
610

611
  tmsgSendRsp(&rspMsg);
27,724,097✔
612

613
  return code;
27,738,052✔
614
}
615

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

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

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

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

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

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

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

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

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

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

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

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

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

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

689
      refColsNum++;
×
690
    }
691

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

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

700
_return:
×
701

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

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

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

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

724
  pList = taosArrayInit(10, sizeof(uint64_t));
2,563,158✔
725
  QUERY_CHECK_NULL(pList, code, line, _return, terrno);
2,559,815✔
726

727
  *ppRes = taosArrayInit(10, POINTER_BYTES);
2,559,815✔
728
  QUERY_CHECK_NULL(*ppRes, code, line, _return, terrno)
2,561,756✔
729
  
730
  // lookup in cache
731
  code = pHandle->api.metaFn.metaGetCachedRefDbs(pHandle->vnode, suid, *ppRes);
2,565,083✔
732
  QUERY_CHECK_CODE(code, line, _return);
2,567,853✔
733

734
  if (taosArrayGetSize(*ppRes) > 0) {
2,567,853✔
735
    // found in cache
736
    goto _return;
2,537,043✔
737
  } else {
738
    code = pHandle->api.metaFn.getChildTableList(pHandle->vnode, suid, pList);
31,272✔
739
    QUERY_CHECK_CODE(code, line, _return);
31,272✔
740

741
    size_t num = taosArrayGetSize(pList);
31,272✔
742
    pDbNameHash = tSimpleHashInit(10, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
31,272✔
743
    QUERY_CHECK_NULL(pDbNameHash, code, line, _return, terrno);
31,272✔
744

745
    for (int32_t i = 0; i < num; ++i) {
102,884✔
746
      uint64_t* id = taosArrayGet(pList, i);
71,612✔
747
      QUERY_CHECK_NULL(id, code, line, _return, terrno);
71,612✔
748

749
      pHandle->api.metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pHandle->api.metaFn);
71,612✔
750
      readerInit = true;
71,612✔
751

752
      code = pHandle->api.metaReaderFn.getTableEntryByUid(&mr, *id);
71,612✔
753
      QUERY_CHECK_CODE(code, line, _return);
71,612✔
754

755
      for (int32_t j = 0; j < mr.me.colRef.nCols; j++) {
677,183✔
756
        if (mr.me.colRef.pColRef[j].hasRef) {
605,571✔
757
          if (NULL == tSimpleHashGet(pDbNameHash, mr.me.colRef.pColRef[j].refDbName, strlen(mr.me.colRef.pColRef[j].refDbName))) {
392,676✔
758
            char *refDbName = taosStrdup(mr.me.colRef.pColRef[j].refDbName);
42,585✔
759
            QUERY_CHECK_NULL(refDbName, code, line, _return, terrno);
42,585✔
760

761
            QUERY_CHECK_NULL(taosArrayPush(*ppRes, &refDbName), code, line, _return, terrno);
85,170✔
762

763
            code = tSimpleHashPut(pDbNameHash, refDbName, strlen(refDbName), NULL, 0);
42,585✔
764
            QUERY_CHECK_CODE(code, line, _return);
42,585✔
765
          }
766
        }
767
      }
768

769
      pHandle->api.metaReaderFn.clearReader(&mr);
71,612✔
770
      readerInit = false;
71,612✔
771
    }
772

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

777
_return:
2,568,315✔
778

779
  if (readerInit) {
2,568,315✔
780
    pHandle->api.metaReaderFn.clearReader(&mr);
×
781
  }
782

783
  taosArrayDestroy(pList);
2,568,315✔
784
  tSimpleHashCleanup(pDbNameHash);
2,567,713✔
785

786
  if (code) {
2,567,111✔
787
    qError("%s failed since %s", __func__, tstrerror(code));
×
788
  }
789
  return code;
2,567,111✔
790
}
791

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

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

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

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

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

833
_return:
×
834

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

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

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

849
  //tmsgSendRsp(&rspMsg);
850

851
  return code;
×
852
}
853

854
int32_t vnodeGetVStbRefDbs(SVnode *pVnode, SRpcMsg *pMsg) {
2,566,107✔
855
  int32_t        code = 0;
2,566,107✔
856
  int32_t        rspSize = 0;
2,566,107✔
857
  SVStbRefDbsReq req = {0};
2,566,107✔
858
  SVStbRefDbsRsp rsp = {0};
2,567,177✔
859
  SRpcMsg        rspMsg = {0};
2,567,177✔
860
  void          *pRsp = NULL;
2,567,177✔
861
  int32_t        line = 0;
2,567,177✔
862

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

869
  SReadHandle handle = {0};
2,566,822✔
870
  handle.vnode = pVnode;
2,566,822✔
871
  initStorageAPI(&handle.api);
2,566,822✔
872

873
  code = vnodeReadVStbRefDbs(&handle, req.suid, &rsp.pDbs);
2,565,090✔
874
  QUERY_CHECK_CODE(code, line, _return);
2,567,713✔
875
  rsp.vgId = TD_VID(pVnode);
2,567,713✔
876

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

896
_return:
2,566,075✔
897

898
  rspMsg.info = pMsg->info;
2,566,677✔
899
  rspMsg.pCont = pRsp;
2,566,069✔
900
  rspMsg.contLen = rspSize;
2,566,069✔
901
  rspMsg.code = code;
2,566,069✔
902
  rspMsg.msgType = pMsg->msgType;
2,566,069✔
903

904
  if (code) {
2,559,513✔
905
    qError("vnd get virtual stb ref db failed cause of %s", tstrerror(code));
×
906
  }
907

908
  *pMsg = rspMsg;
2,559,513✔
909

910
  tDestroySVStbRefDbsRsp(&rsp);
2,565,506✔
911

912
  return code;
2,565,640✔
913
}
914

915
static int32_t vnodeGetCompStorage(SVnode *pVnode, int64_t *output) {
130,211,353✔
916
  int32_t code = 0;
130,211,353✔
917
#ifdef TD_ENTERPRISE
918
  int32_t now = taosGetTimestampSec();
130,211,353✔
919
  if (llabs(now - pVnode->config.vndStats.storageLastUpd) >= 30) {
130,211,353✔
920
    pVnode->config.vndStats.storageLastUpd = now;
6,760,241✔
921

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

941
static void vnodeGetBufferInfo(SVnode *pVnode, int64_t *bufferSegmentUsed, int64_t *bufferSegmentSize) {
130,211,353✔
942
  *bufferSegmentUsed = 0;
130,211,353✔
943
  *bufferSegmentSize = 0;
130,211,353✔
944
  if (pVnode) {
130,211,353✔
945
    (void)taosThreadMutexLock(&pVnode->mutex);
130,211,353✔
946

947
    if (pVnode->inUse) {
130,211,353✔
948
      *bufferSegmentUsed = pVnode->inUse->size;
130,191,551✔
949
    }
950
    *bufferSegmentSize = pVnode->config.szBuf / VNODE_BUFPOOL_SEGMENTS;
130,211,353✔
951

952
    (void)taosThreadMutexUnlock(&pVnode->mutex);
130,211,353✔
953
  }
954
}
130,211,353✔
955

956
int32_t vnodeGetLoad(SVnode *pVnode, SVnodeLoad *pLoad) {
130,211,353✔
957
  SSyncState state = syncGetState(pVnode->sync);
130,211,353✔
958
  pLoad->syncAppliedIndex = pVnode->state.applied;
130,211,353✔
959
  syncGetCommitIndex(pVnode->sync, &pLoad->syncCommitIndex);
130,211,353✔
960

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

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

1008
void vnodeGetInfo(void *pVnode, const char **dbname, int32_t *vgId, int64_t *numOfTables, int64_t *numOfNormalTables) {
13,569,186✔
1009
  SVnode    *pVnodeObj = pVnode;
13,569,186✔
1010
  SVnodeCfg *pConf = &pVnodeObj->config;
13,569,186✔
1011

1012
  if (dbname) {
13,570,379✔
1013
    *dbname = pConf->dbname;
13,570,183✔
1014
  }
1015

1016
  if (vgId) {
13,571,030✔
1017
    *vgId = TD_VID(pVnodeObj);
12,871,966✔
1018
  }
1019

1020
  if (numOfTables) {
13,571,509✔
1021
    *numOfTables = pConf->vndStats.numOfNTables + pConf->vndStats.numOfCTables;
×
1022
  }
1023

1024
  if (numOfNormalTables) {
13,571,509✔
1025
    *numOfNormalTables = pConf->vndStats.numOfNTables;
×
1026
  }
1027
}
13,571,509✔
1028

1029
int32_t vnodeGetTableList(void *pVnode, int8_t type, SArray *pList) {
×
1030
  if (type == TSDB_SUPER_TABLE) {
×
1031
    return vnodeGetStbIdList(pVnode, 0, pList);
×
1032
  } else {
1033
    return TSDB_CODE_INVALID_PARA;
×
1034
  }
1035
}
1036

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

1045
  while (1) {
×
1046
    tb_uid_t id = metaCtbCursorNext(pCur);
×
1047
    if (id == 0) {
×
1048
      break;
×
1049
    }
1050

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

1063
int32_t vnodeGetCtbIdListByFilter(SVnode *pVnode, int64_t suid, SArray *list, bool (*filter)(void *arg), void *arg) {
×
1064
  return 0;
×
1065
}
1066

1067
int32_t vnodeGetCtbIdList(void *pVnode, int64_t suid, SArray *list) {
58,539,465✔
1068
  int32_t      code = TSDB_CODE_SUCCESS;
58,539,465✔
1069
  SVnode      *pVnodeObj = pVnode;
58,539,465✔
1070
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnodeObj, suid, 1);
58,539,465✔
1071
  if (NULL == pCur) {
58,534,220✔
1072
    qError("vnode get all table list failed");
×
1073
    return terrno;
×
1074
  }
1075

1076
  while (1) {
253,952,862✔
1077
    tb_uid_t id = metaCtbCursorNext(pCur);
312,487,082✔
1078
    if (id == 0) {
312,484,108✔
1079
      break;
58,539,852✔
1080
    }
1081
    qTrace("vnodeGetCtbIdList: got ctb id %" PRId64 " for suid %" PRId64, id, suid);
253,944,256✔
1082
    if (NULL == taosArrayPush(list, &id)) {
253,949,662✔
1083
      qError("taosArrayPush failed");
×
1084
      code = terrno;
×
1085
      goto _exit;
×
1086
    }
1087
  }
1088

1089
_exit:
58,520,381✔
1090
  metaCloseCtbCursor(pCur);
58,520,381✔
1091
  return code;
58,497,501✔
1092
}
1093

1094
int32_t vnodeGetStbIdList(SVnode *pVnode, int64_t suid, SArray *list) {
62,324,223✔
1095
  int32_t      code = TSDB_CODE_SUCCESS;
62,324,223✔
1096
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, suid);
62,324,223✔
1097
  if (!pCur) {
62,324,223✔
1098
    return TSDB_CODE_OUT_OF_MEMORY;
×
1099
  }
1100

1101
  while (1) {
107,656,121✔
1102
    tb_uid_t id = metaStbCursorNext(pCur);
169,980,344✔
1103
    if (id == 0) {
169,980,996✔
1104
      break;
62,327,483✔
1105
    }
1106

1107
    if (NULL == taosArrayPush(list, &id)) {
107,654,817✔
1108
      qError("taosArrayPush failed");
×
1109
      code = terrno;
×
1110
      goto _exit;
×
1111
    }
1112
  }
1113

1114
_exit:
62,324,223✔
1115
  metaCloseStbCursor(pCur);
62,324,223✔
1116
  return code;
62,323,571✔
1117
}
1118

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

1127
  while (1) {
1,120✔
1128
    tb_uid_t id = metaStbCursorNext(pCur);
1,344✔
1129
    if (id == 0) {
1,344✔
1130
      break;
224✔
1131
    }
1132

1133
    if ((*filter) && (*filter)(arg, &id)) {
1,120✔
1134
      continue;
1,120✔
1135
    }
1136

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

1144
_exit:
224✔
1145
  metaCloseStbCursor(pCur);
224✔
1146
  return code;
224✔
1147
}
1148

1149
int32_t vnodeGetCtbNum(SVnode *pVnode, int64_t suid, int64_t *num) {
6,880,213✔
1150
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, suid, 0);
6,880,213✔
1151
  if (!pCur) {
6,881,071✔
1152
    return terrno;
×
1153
  }
1154

1155
  *num = 0;
6,881,071✔
1156
  while (1) {
2,393,451✔
1157
    tb_uid_t id = metaCtbCursorNext(pCur);
9,274,522✔
1158
    if (id == 0) {
9,275,177✔
1159
      break;
6,881,508✔
1160
    }
1161

1162
    ++(*num);
2,393,669✔
1163
  }
1164

1165
  metaCloseCtbCursor(pCur);
6,881,508✔
1166
  return TSDB_CODE_SUCCESS;
6,881,490✔
1167
}
1168

1169
int32_t vnodeGetStbColumnNum(SVnode *pVnode, tb_uid_t suid, int *num) {
6,881,246✔
1170
  SSchemaWrapper *pSW = metaGetTableSchema(pVnode->pMeta, suid, -1, 0, NULL, 0);
6,881,246✔
1171
  if (pSW) {
6,881,416✔
1172
    *num = pSW->nCols;
6,881,416✔
1173
    tDeleteSchemaWrapper(pSW);
1174
  } else {
UNCOV
1175
    *num = 2;
×
1176
  }
1177

1178
  return TSDB_CODE_SUCCESS;
6,881,017✔
1179
}
1180

1181
int32_t vnodeGetStbInfo(SVnode *pVnode, tb_uid_t suid, int64_t *keep, int8_t *flags) {
6,880,986✔
1182
  SMetaReader mr = {0};
6,880,986✔
1183
  metaReaderDoInit(&mr, pVnode->pMeta, META_READER_NOLOCK);
6,880,986✔
1184

1185
  int32_t code = metaReaderGetTableEntryByUid(&mr, suid);
6,880,720✔
1186
  if (code == TSDB_CODE_SUCCESS) {
6,881,490✔
1187
    if (keep) *keep = mr.me.stbEntry.keep;
6,881,490✔
1188
    if (flags) *flags = mr.me.flags;
6,881,490✔
1189
  } else {
UNCOV
1190
    if (keep) *keep = 0;
×
UNCOV
1191
    if (flags) *flags = 0;
×
1192
  }
1193

1194
  metaReaderClear(&mr);
6,881,472✔
1195
  return TSDB_CODE_SUCCESS;
6,881,490✔
1196
}
1197

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

1222
// exclude stbs of taoskeeper log
1223
static int32_t vnodeGetTimeSeriesBlackList(SVnode *pVnode, int32_t *tbSize) {
62,324,965✔
1224
  int32_t      code = TSDB_CODE_SUCCESS;
62,324,965✔
1225
  int32_t      tbNum = 0;
62,324,965✔
1226
  const char **pTbArr = NULL;
62,324,965✔
1227
  const char  *dbName = NULL;
62,324,965✔
1228
  *tbSize = 0;
62,324,965✔
1229

1230
  if (!(dbName = strchr(pVnode->config.dbname, '.'))) return 0;
62,323,009✔
1231
  if (0 == strncmp(++dbName, "log", TSDB_DB_NAME_LEN)) {
62,324,965✔
1232
    tbNum = tkLogStbNum;
3,136✔
1233
    pTbArr = (const char **)&tkLogStb;
3,136✔
1234
  } else if (0 == strncmp(dbName, "audit", TSDB_DB_NAME_LEN)) {
62,323,133✔
1235
    tbNum = tkAuditStbNum;
×
1236
    pTbArr = (const char **)&tkAuditStb;
×
1237
  }
1238
  if (tbNum && pTbArr) {
62,326,269✔
1239
    *tbSize = metaSizeOfTbFilterCache(pVnode->pMeta, 0);
3,136✔
1240
    if (*tbSize < tbNum) {
3,136✔
1241
      for (int32_t i = 0; i < tbNum; ++i) {
46,592✔
1242
        tb_uid_t suid = metaGetTableEntryUidByName(pVnode->pMeta, pTbArr[i]);
45,248✔
1243
        if (suid != 0) {
45,248✔
1244
          code = metaPutTbToFilterCache(pVnode->pMeta, &suid, 0);
2,912✔
1245
          if (TSDB_CODE_SUCCESS != code) {
2,912✔
1246
            return code;
1,792✔
1247
          }
1248
        }
1249
      }
1250
      *tbSize = metaSizeOfTbFilterCache(pVnode->pMeta, 0);
1,344✔
1251
    }
1252
  }
1253

1254
  return code;
62,323,173✔
1255
}
1256
#endif
1257

1258
static bool vnodeTimeSeriesFilter(void *arg1, void *arg2) {
1,120✔
1259
  SVnode *pVnode = (SVnode *)arg1;
1,120✔
1260

1261
  if (metaTbInFilterCache(pVnode->pMeta, arg2, 0)) {
1,120✔
1262
    return true;
1,120✔
1263
  }
1264
  return false;
×
1265
}
1266

1267
int32_t vnodeGetTimeSeriesNum(SVnode *pVnode, int64_t *num) {
62,324,965✔
1268
  SArray *suidList = NULL;
62,324,965✔
1269

1270
  if (!(suidList = taosArrayInit(1, sizeof(tb_uid_t)))) {
62,324,965✔
1271
    return terrno;
×
1272
  }
1273

1274
  int32_t tbFilterSize = 0;
62,323,661✔
1275
  int32_t code = TSDB_CODE_SUCCESS;
62,323,661✔
1276
#ifdef TD_ENTERPRISE
1277
  code = vnodeGetTimeSeriesBlackList(pVnode, &tbFilterSize);
62,323,661✔
1278
  if (TSDB_CODE_SUCCESS != code) {
62,324,965✔
1279
    goto _exit;
1,792✔
1280
  }
1281
#endif
1282

1283
  if ((!tbFilterSize && vnodeGetStbIdList(pVnode, 0, suidList) < 0) ||
62,323,173✔
1284
      (tbFilterSize && vnodeGetStbIdListByFilter(pVnode, 0, suidList, vnodeTimeSeriesFilter, pVnode) < 0)) {
62,322,521✔
1285
    qError("vgId:%d, failed to get stb id list error: %s", TD_VID(pVnode), terrstr());
×
1286
    taosArrayDestroy(suidList);
×
1287
    return terrno;
×
1288
  }
1289

1290
  *num = 0;
62,321,869✔
1291
  int64_t arrSize = taosArrayGetSize(suidList);
62,323,173✔
1292
  for (int64_t i = 0; i < arrSize; ++i) {
169,974,820✔
1293
    tb_uid_t suid = *(tb_uid_t *)taosArrayGet(suidList, i);
107,649,039✔
1294

1295
    int64_t ctbNum = 0;
107,652,951✔
1296
    int32_t numOfCols = 0;
107,652,299✔
1297
    int8_t  flags = 0;
107,651,647✔
1298
    code = metaGetStbStats(pVnode, suid, &ctbNum, &numOfCols, &flags);
107,651,647✔
1299
    if (TSDB_CODE_SUCCESS != code) {
107,656,211✔
1300
      goto _exit;
×
1301
    }
1302
    if (!TABLE_IS_VIRTUAL(flags)) {
107,656,211✔
1303
      *num += ctbNum * (numOfCols - 1);
104,187,235✔
1304
    }
1305
  }
1306

1307
_exit:
62,328,225✔
1308
  taosArrayDestroy(suidList);
62,327,573✔
1309
  return TSDB_CODE_SUCCESS;
62,325,617✔
1310
}
1311

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

1318
  *num = 0;
×
1319
  while (1) {
×
1320
    tb_uid_t id = metaStbCursorNext(pCur);
×
1321
    if (id == 0) {
×
1322
      break;
×
1323
    }
1324

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

1332
    *num += ctbNum;
×
1333
  }
1334

1335
  metaCloseStbCursor(pCur);
×
1336
  return TSDB_CODE_SUCCESS;
×
1337
}
1338

1339
void *vnodeGetIdx(void *pVnode) {
4,313,757✔
1340
  if (pVnode == NULL) {
4,313,757✔
1341
    return NULL;
×
1342
  }
1343

1344
  return metaGetIdx(((SVnode *)pVnode)->pMeta);
4,313,757✔
1345
}
1346

1347
void *vnodeGetIvtIdx(void *pVnode) {
4,313,757✔
1348
  if (pVnode == NULL) {
4,313,757✔
1349
    return NULL;
×
1350
  }
1351
  return metaGetIvtIdx(((SVnode *)pVnode)->pMeta);
4,313,757✔
1352
}
1353

1354
int32_t vnodeGetTableSchema(void *pVnode, int64_t uid, STSchema **pSchema, int64_t *suid, SSchemaWrapper **pTagSchema) {
65,287✔
1355
  return tsdbGetTableSchema(((SVnode *)pVnode)->pMeta, uid, pSchema, suid, pTagSchema);
65,287✔
1356
}
1357

1358
static FORCE_INLINE int32_t vnodeGetDBPrimaryInfo(SVnode *pVnode, SDbSizeStatisInfo *pInfo) {
1359
  int32_t code = 0;
6,772,913✔
1360
  char    path[TSDB_FILENAME_LEN] = {0};
6,772,913✔
1361

1362
  char   *dirName[] = {VNODE_TSDB_DIR, VNODE_WAL_DIR, VNODE_META_DIR, VNODE_TSDB_CACHE_DIR};
6,772,913✔
1363
  int64_t dirSize[4];
6,767,218✔
1364

1365
  vnodeGetPrimaryPath(pVnode, false, path, TSDB_FILENAME_LEN);
6,772,913✔
1366
  int32_t offset = strlen(path);
6,772,913✔
1367

1368
  for (int i = 0; i < sizeof(dirName) / sizeof(dirName[0]); i++) {
33,864,565✔
1369
    int64_t size = {0};
27,091,652✔
1370
    (void)snprintf(path + offset, TSDB_FILENAME_LEN - offset, "%s%s", TD_DIRSEP, dirName[i]);
27,091,652✔
1371
    code = taosGetDirSize(path, &size);
27,091,293✔
1372
    if (code != 0) {
27,091,652✔
1373
      uWarn("vnode %d get dir %s %s size failed since %s", TD_VID(pVnode), path, dirName[i], tstrerror(code));
2,409✔
1374
    }
1375
    path[offset] = 0;
27,091,652✔
1376
    dirSize[i] = size;
27,091,652✔
1377
  }
1378

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

1395
  code = tsdbGetFsSize(pVnodeObj->pTsdb, pInfo);
6,772,913✔
1396
_exit:
6,772,913✔
1397
  return code;
6,772,913✔
1398
}
1399

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

1408
  SVnode      *pVnode1 = (SVnode *)pVnode;
×
1409
  SSyncMetrics syncMetrics = syncGetMetrics(pVnode1->sync);
×
1410

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

1432
  return 0;
×
1433
}
1434

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

1443
  SVnode *pVnode1 = (SVnode *)pVnode;
×
1444

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

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

1457
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.commit_time, pOldMetrics->commit_time);
×
1458
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.merge_time, pOldMetrics->merge_time);
×
1459

1460
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.memtable_wait_time, pOldMetrics->memtable_wait_time);
×
1461
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.blocked_commit_count, pOldMetrics->blocked_commit_count);
×
1462
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.block_commit_time, pOldMetrics->blocked_commit_time);
×
1463
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.merge_count, pOldMetrics->merge_count);
×
1464

1465
  // Reset new cache metrics
1466
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.last_cache_commit_time, pOldMetrics->last_cache_commit_time);
×
1467
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.last_cache_commit_count, pOldMetrics->last_cache_commit_count);
×
1468

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

1476
  return 0;
×
1477
}
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