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

taosdata / TDengine / #4987

16 Mar 2026 12:26PM UTC coverage: 73.883% (+36.6%) from 37.305%
#4987

push

travis-ci

web-flow
feat: support secure delete option. (#34591)

209 of 391 new or added lines in 24 files covered. (53.45%)

3062 existing lines in 140 files now uncovered.

261133 of 353439 relevant lines covered (73.88%)

121262425.02 hits per line

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

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

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

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

36
int32_t fillTableColCmpr(SMetaReader *reader, SSchemaExt *pExt, int32_t numOfCol) {
20,324,707✔
37
  int8_t tblType = reader->me.type;
20,324,707✔
38
  if (withExtSchema(tblType)) {
20,326,033✔
39
    SColCmprWrapper *p = &(reader->me.colCmpr);
19,992,600✔
40
    if (numOfCol != p->nCols) {
19,992,600✔
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,246,225✔
45
      SColCmpr *pCmpr = &p->pColCmpr[i];
217,252,838✔
46
      pExt[i].colId = pCmpr->id;
217,258,142✔
47
      pExt[i].compress = pCmpr->alg;
217,254,703✔
48
    }
49
  }
50
  return 0;
20,325,370✔
51
}
52

53
void vnodePrintTableMeta(STableMetaRsp *pMeta) {
20,322,842✔
54
  if (!(qDebugFlag & DEBUG_DEBUG)) {
20,322,842✔
55
    return;
406,064✔
56
  }
57

58
  qDebug("tbName:%s", pMeta->tbName);
19,916,778✔
59
  qDebug("stbName:%s", pMeta->stbName);
19,919,969✔
60
  qDebug("dbFName:%s", pMeta->dbFName);
19,919,969✔
61
  qDebug("dbId:%" PRId64, pMeta->dbId);
19,919,969✔
62
  qDebug("numOfTags:%d", pMeta->numOfTags);
19,919,969✔
63
  qDebug("numOfColumns:%d", pMeta->numOfColumns);
19,919,969✔
64
  qDebug("precision:%d", pMeta->precision);
19,919,969✔
65
  qDebug("tableType:%d", pMeta->tableType);
19,919,969✔
66
  qDebug("sversion:%d", pMeta->sversion);
19,919,969✔
67
  qDebug("tversion:%d", pMeta->tversion);
19,919,969✔
68
  qDebug("suid:%" PRIu64, pMeta->suid);
19,919,969✔
69
  qDebug("tuid:%" PRIu64, pMeta->tuid);
19,919,969✔
70
  qDebug("vgId:%d", pMeta->vgId);
19,919,969✔
71
  qDebug("sysInfo:%d", pMeta->sysInfo);
19,919,969✔
72
  if (pMeta->pSchemas) {
19,919,969✔
73
    for (int32_t i = 0; i < (pMeta->numOfColumns + pMeta->numOfTags); ++i) {
275,456,384✔
74
      SSchema *pSchema = pMeta->pSchemas + i;
255,536,621✔
75
      qDebug("%d col/tag: type:%d, flags:%d, colId:%d, bytes:%d, name:%s", i, pSchema->type, pSchema->flags,
255,536,621✔
76
             pSchema->colId, pSchema->bytes, pSchema->name);
77
    }
78
  }
79
}
80

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

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

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

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

130
  if (!reqTbUid) {
29,802,254✔
131
    (void)tsnprintf(tableFName, TSDB_TABLE_FNAME_LEN, "%s.%s", infoReq.dbFName, infoReq.tbName);
29,539,422✔
132
    if (pVnode->mounted) tTrimMountPrefix(tableFName);
29,540,748✔
133
    code = vnodeValidateTableHash(pVnode, tableFName);
29,540,748✔
134
    if (code) {
29,538,887✔
135
      goto _exit4;
×
136
    }
137
  }
138

139
  // query meta
140
  metaReaderDoInit(&mer1, pVnode->pMeta, META_READER_LOCK);
29,801,719✔
141
  if (reqTbUid) {
29,801,060✔
142
    SET_ERRNO(0);
262,169✔
143
    uint64_t tbUid = taosStr2UInt64(infoReq.tbName, NULL, 10);
262,169✔
144
    if (ERRNO == ERANGE || tbUid == 0) {
262,169✔
145
      code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
146
      goto _exit3;
×
147
    }
148
    SMetaReader mr3 = {0};
262,169✔
149
    metaReaderDoInit(&mr3, ((SVnode *)pVnode)->pMeta, META_READER_NOLOCK);
262,169✔
150
    if ((code = metaReaderGetTableEntryByUid(&mr3, tbUid)) < 0) {
262,169✔
151
      metaReaderClear(&mr3);
92,554✔
152
      TAOS_CHECK_GOTO(code, NULL, _exit3);
92,554✔
153
    }
154
    tstrncpy(metaRsp.tbName, mr3.me.name, TSDB_TABLE_NAME_LEN);
169,615✔
155
    metaReaderClear(&mr3);
169,615✔
156
    TAOS_CHECK_GOTO(metaGetTableEntryByName(&mer1, metaRsp.tbName), NULL, _exit3);
169,615✔
157
  } else if (metaGetTableEntryByName(&mer1, infoReq.tbName) < 0) {
29,538,891✔
158
    code = terrno;
9,384,330✔
159
    goto _exit3;
9,383,955✔
160
  }
161

162
  metaRsp.tableType = mer1.me.type;
20,323,966✔
163
  metaRsp.vgId = TD_VID(pVnode);
20,323,966✔
164
  metaRsp.tuid = mer1.me.uid;
20,324,633✔
165
  metaRsp.isAudit = pVnode->config.isAudit ? 1 : 0;
20,324,633✔
166

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

182
      (void)strcpy(metaRsp.stbName, mer2.me.name);
12,958,492✔
183
      metaRsp.suid = mer2.me.uid;
12,958,492✔
184
      metaRsp.ownerId = mer2.me.stbEntry.ownerId;  // child table inherits ownerId from stb
12,958,492✔
185
      schema = mer2.me.stbEntry.schemaRow;
12,958,492✔
186
      schemaTag = mer2.me.stbEntry.schemaTag;
12,958,492✔
187
      break;
12,958,492✔
188
    }
189
    case TSDB_NORMAL_TABLE:
3,532,156✔
190
    case TSDB_VIRTUAL_NORMAL_TABLE: {
191
      schema = mer1.me.ntbEntry.schemaRow;
3,532,156✔
192
      metaRsp.ownerId = mer1.me.ntbEntry.ownerId;
3,532,156✔
193
      break;
3,532,156✔
194
    }
195
    default: {
×
196
      vError("vnodeGetTableMeta get invalid table type:%d", mer1.me.type);
×
197
      goto _exit3;
×
198
    }
199
  }
200

201
  metaRsp.numOfTags = schemaTag.nCols;
20,324,176✔
202
  metaRsp.numOfColumns = schema.nCols;
20,324,176✔
203
  metaRsp.precision = pVnode->config.tsdbCfg.precision;
20,324,176✔
204
  metaRsp.sversion = schema.version;
20,322,311✔
205
  metaRsp.tversion = schemaTag.version;
20,322,311✔
206
  metaRsp.pSchemas = (SSchema *)taosMemoryMalloc(sizeof(SSchema) * (metaRsp.numOfColumns + metaRsp.numOfTags));
20,322,311✔
207
  metaRsp.pSchemaExt = (SSchemaExt *)taosMemoryCalloc(metaRsp.numOfColumns, sizeof(SSchemaExt));
20,322,842✔
208
  if (NULL == metaRsp.pSchemas || NULL == metaRsp.pSchemaExt) {
20,324,831✔
209
    code = terrno;
×
210
    goto _exit;
×
211
  }
212
  (void)memcpy(metaRsp.pSchemas, schema.pSchema, sizeof(SSchema) * schema.nCols);
20,326,033✔
213
  if (schemaTag.nCols) {
20,326,033✔
214
    (void)memcpy(metaRsp.pSchemas + schema.nCols, schemaTag.pSchema, sizeof(SSchema) * schemaTag.nCols);
16,792,551✔
215
  }
216
  if (metaRsp.pSchemaExt) {
20,325,494✔
217
    SMetaReader *pReader = mer1.me.type == TSDB_CHILD_TABLE ? &mer2 : &mer1;
20,324,831✔
218
    code = fillTableColCmpr(pReader, metaRsp.pSchemaExt, metaRsp.numOfColumns);
20,324,831✔
219
    if (code < 0) {
20,325,370✔
220
      goto _exit;
×
221
    }
222
    for (int32_t i = 0; i < metaRsp.numOfColumns && pReader->me.pExtSchemas; i++) {
48,474,282✔
223
      metaRsp.pSchemaExt[i].typeMod = pReader->me.pExtSchemas[i].typeMod;
28,148,912✔
224
    }
225
  } else {
226
    code = TSDB_CODE_OUT_OF_MEMORY;
663✔
227
    goto _exit;
663✔
228
  }
229
  if (hasRefCol(mer1.me.type)) {
20,324,831✔
230
    metaRsp.rversion = mer1.me.colRef.version;
333,972✔
231
    metaRsp.pColRefs = (SColRef*)taosMemoryMalloc(sizeof(SColRef) * metaRsp.numOfColumns);
333,972✔
232
    if (metaRsp.pColRefs) {
333,433✔
233
      code = fillTableColRef(&mer1, metaRsp.pColRefs, metaRsp.numOfColumns);
333,433✔
234
      if (code < 0) {
333,433✔
235
        goto _exit;
×
236
      }
237
    }
238
    metaRsp.numOfColRefs = metaRsp.numOfColumns;
333,433✔
239
  } else {
240
    metaRsp.pColRefs = NULL;
19,988,746✔
241
    metaRsp.numOfColRefs = 0;
19,988,746✔
242
  }
243

244
  vnodePrintTableMeta(&metaRsp);
20,322,179✔
245

246
  // encode and send response
247
  rspLen = tSerializeSTableMetaRsp(NULL, 0, &metaRsp);
20,325,494✔
248
  if (rspLen < 0) {
20,324,044✔
249
    code = terrno;
×
250
    goto _exit;
×
251
  }
252

253
  if (direct) {
20,324,044✔
254
    pRsp = rpcMallocCont(rspLen);
205,802✔
255
  } else {
256
    pRsp = taosMemoryCalloc(1, rspLen);
20,118,242✔
257
  }
258

259
  if (pRsp == NULL) {
20,322,055✔
260
    code = terrno;
×
261
    goto _exit;
×
262
  }
263

264
  rspLen = tSerializeSTableMetaRsp(pRsp, rspLen, &metaRsp);
20,322,055✔
265
  if (rspLen < 0) {
20,324,501✔
266
    code = terrno;
×
267
    goto _exit;
×
268
  }
269

270
_exit:
20,325,164✔
271
  taosMemoryFree(metaRsp.pColRefs);
20,324,707✔
272
  taosMemoryFree(metaRsp.pSchemas);
20,320,612✔
273
  taosMemoryFree(metaRsp.pSchemaExt);
20,320,155✔
274
_exit2:
20,322,512✔
275
  metaReaderClear(&mer2);
20,322,512✔
276
_exit3:
29,798,358✔
277
  metaReaderClear(&mer1);
29,798,404✔
278
_exit4:
29,798,952✔
279
  rpcMsg.info = pMsg->info;
29,800,187✔
280
  rpcMsg.pCont = pRsp;
29,799,615✔
281
  rpcMsg.contLen = rspLen;
29,799,615✔
282
  rpcMsg.code = code;
29,799,615✔
283
  rpcMsg.msgType = pMsg->msgType;
29,799,615✔
284

285
  if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST && autoCreateCtb == 1) {
29,797,343✔
286
    code = TSDB_CODE_SUCCESS;
5,615,175✔
287
  }
288

289
  if (code) {
29,797,343✔
290
    qError("vgId:%d, get table %s meta with %" PRIu8 " failed cause of %s", pVnode->config.vgId, infoReq.tbName,
3,861,319✔
291
           infoReq.option, tstrerror(code));
292
  }
293

294
  if (direct) {
29,797,613✔
295
    tmsgSendRsp(&rpcMsg);
948,585✔
296
  } else {
297
    *pMsg = rpcMsg;
28,849,028✔
298
  }
299

300
  return code;
29,800,265✔
301
}
302

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

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

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

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

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

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

340
  cfgRsp.tableType = mer1.me.type;
63,582✔
341
  cfgRsp.isAudit = pVnode->config.isAudit ? 1 : 0;
63,582✔
342
  cfgRsp.secureDelete = pVnode->config.secureDelete;
63,582✔
343

344
  if (mer1.me.type == TSDB_SUPER_TABLE) {
63,582✔
345
    code = TSDB_CODE_VND_HASH_MISMATCH;
×
346
    goto _exit;
×
347
  } else if (mer1.me.type == TSDB_CHILD_TABLE || mer1.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
63,582✔
348
    metaReaderDoInit(&mer2, pVnode->pMeta, META_READER_NOLOCK);
44,731✔
349
    if (metaReaderGetTableEntryByUid(&mer2, mer1.me.ctbEntry.suid) < 0) goto _exit;
44,731✔
350

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

390
  cfgRsp.numOfTags = schemaTag.nCols;
63,582✔
391
  cfgRsp.numOfColumns = schema.nCols;
63,582✔
392
  cfgRsp.virtualStb = false; // vnode don't have super table, so it's always false
63,582✔
393
  cfgRsp.pSchemas = (SSchema *)taosMemoryMalloc(sizeof(SSchema) * (cfgRsp.numOfColumns + cfgRsp.numOfTags));
63,582✔
394
  cfgRsp.pSchemaExt = (SSchemaExt *)taosMemoryMalloc(cfgRsp.numOfColumns * sizeof(SSchemaExt));
63,582✔
395
  cfgRsp.pColRefs = (SColRef *)taosMemoryMalloc(sizeof(SColRef) * cfgRsp.numOfColumns);
63,582✔
396

397
  if (NULL == cfgRsp.pSchemas || NULL == cfgRsp.pSchemaExt || NULL == cfgRsp.pColRefs) {
63,582✔
398
    code = terrno;
×
399
    goto _exit;
×
400
  }
401
  (void)memcpy(cfgRsp.pSchemas, schema.pSchema, sizeof(SSchema) * schema.nCols);
63,582✔
402
  if (schemaTag.nCols) {
63,582✔
403
    (void)memcpy(cfgRsp.pSchemas + schema.nCols, schemaTag.pSchema, sizeof(SSchema) * schemaTag.nCols);
44,731✔
404
  }
405

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

410
  if (withExtSchema(cfgRsp.tableType)) {
63,582✔
411
    for (int32_t i = 0; i < cfgRsp.numOfColumns; i++) {
234,102✔
412
      SColCmpr   *pCmpr = &pColCmpr->pColCmpr[i];
178,421✔
413
      SSchemaExt *pSchExt = cfgRsp.pSchemaExt + i;
178,421✔
414
      pSchExt->colId = pCmpr->id;
178,421✔
415
      pSchExt->compress = pCmpr->alg;
178,421✔
416
      if (pReader->me.pExtSchemas)
178,421✔
417
        pSchExt->typeMod = pReader->me.pExtSchemas[i].typeMod;
25,704✔
418
      else
419
        pSchExt->typeMod = 0;
152,717✔
420
    }
421
  }
422

423
  cfgRsp.virtualStb = false;
63,582✔
424
  if (hasRefCol(cfgRsp.tableType)) {
63,582✔
425
    for (int32_t i = 0; i < cfgRsp.numOfColumns; i++) {
111,411✔
426
      SColRef *pRef = &pColRef->pColRef[i];
103,510✔
427
      cfgRsp.pColRefs[i].hasRef = pRef->hasRef;
103,510✔
428
      cfgRsp.pColRefs[i].id = pRef->id;
103,510✔
429
      if (cfgRsp.pColRefs[i].hasRef) {
103,510✔
430
        tstrncpy(cfgRsp.pColRefs[i].refDbName, pRef->refDbName, TSDB_DB_NAME_LEN);
54,424✔
431
        tstrncpy(cfgRsp.pColRefs[i].refTableName, pRef->refTableName, TSDB_TABLE_NAME_LEN);
54,424✔
432
        tstrncpy(cfgRsp.pColRefs[i].refColName, pRef->refColName, TSDB_COL_NAME_LEN);
54,424✔
433
      }
434
    }
435
  }
436

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

444
  if (direct) {
63,582✔
445
    pRsp = rpcMallocCont(rspLen);
×
446
  } else {
447
    pRsp = taosMemoryCalloc(1, rspLen);
63,582✔
448
  }
449

450
  if (pRsp == NULL) {
63,582✔
451
    code = terrno;
×
452
    goto _exit;
×
453
  }
454

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

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

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

472
  if (direct) {
63,582✔
473
    tmsgSendRsp(&rpcMsg);
×
474
  } else {
475
    *pMsg = rpcMsg;
63,582✔
476
  }
477

478
  tFreeSTableCfgRsp(&cfgRsp);
63,582✔
479
  metaReaderClear(&mer2);
63,582✔
480
  metaReaderClear(&mer1);
63,582✔
481
  return code;
63,582✔
482
}
483

484
static FORCE_INLINE void vnodeFreeSBatchRspMsg(void *p) {
485
  if (NULL == p) {
486
    return;
487
  }
488

489
  SBatchRspMsg *pRsp = (SBatchRspMsg *)p;
490
  rpcFreeCont(pRsp->msg);
491
}
492

493
int32_t vnodeGetBatchMeta(SVnode *pVnode, SRpcMsg *pMsg) {
28,758,307✔
494
  int32_t      code = 0;
28,758,307✔
495
  int32_t      rspSize = 0;
28,758,307✔
496
  SBatchReq    batchReq = {0};
28,758,307✔
497
  SBatchMsg   *req = NULL;
28,758,891✔
498
  SBatchRspMsg rsp = {0};
28,758,891✔
499
  SBatchRsp    batchRsp = {0};
28,746,242✔
500
  SRpcMsg      reqMsg = *pMsg;
28,745,127✔
501
  SRpcMsg      rspMsg = {0};
28,745,127✔
502
  void        *pRsp = NULL;
28,754,158✔
503

504
  if (tDeserializeSBatchReq(pMsg->pCont, pMsg->contLen, &batchReq)) {
28,754,158✔
505
    code = terrno;
×
506
    qError("tDeserializeSBatchReq failed");
×
507
    goto _exit;
×
508
  }
509

510
  int32_t msgNum = taosArrayGetSize(batchReq.pMsgs);
28,750,465✔
511
  if (msgNum >= MAX_META_MSG_IN_BATCH) {
28,753,352✔
512
    code = TSDB_CODE_INVALID_MSG;
×
513
    qError("too many msgs %d in vnode batch meta req", msgNum);
×
514
    goto _exit;
×
515
  }
516

517
  batchRsp.pRsps = taosArrayInit(msgNum, sizeof(SBatchRspMsg));
28,753,352✔
518
  if (NULL == batchRsp.pRsps) {
28,749,234✔
519
    code = terrno;
×
520
    qError("taosArrayInit %d SBatchRspMsg failed", msgNum);
×
521
    goto _exit;
×
522
  }
523

524
  for (int32_t i = 0; i < msgNum; ++i) {
62,934,341✔
525
    req = taosArrayGet(batchReq.pMsgs, i);
34,182,976✔
526
    if (req == NULL) {
34,182,987✔
527
      code = terrno;
×
528
      goto _exit;
×
529
    }
530

531
    reqMsg.msgType = req->msgType;
34,182,987✔
532
    reqMsg.pCont = req->msg;
34,185,973✔
533
    reqMsg.contLen = req->msgLen;
34,183,063✔
534

535
    switch (req->msgType) {
34,174,103✔
536
      case TDMT_VND_TABLE_META:
28,592,163✔
537
        // error code has been set into reqMsg, no need to handle it here.
538
        if (TSDB_CODE_SUCCESS != vnodeGetTableMeta(pVnode, &reqMsg, false)) {
28,592,163✔
539
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
3,026,372✔
540
        }
541
        break;
28,589,511✔
542
      case TDMT_VND_TABLE_NAME:
262,169✔
543
        // error code has been set into reqMsg, no need to handle it here.
544
        if (TSDB_CODE_SUCCESS != vnodeGetTableMeta(pVnode, &reqMsg, false)) {
262,169✔
545
          qWarn("vnodeGetBatchName failed, msgType:%d", req->msgType);
92,554✔
546
        }
547
        break;
261,963✔
548
      case TDMT_VND_TABLE_CFG:
63,582✔
549
        // error code has been set into reqMsg, no need to handle it here.
550
        if (TSDB_CODE_SUCCESS != vnodeGetTableCfg(pVnode, &reqMsg, false)) {
63,582✔
551
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
×
552
        }
553
        break;
63,582✔
554
      case TDMT_VND_VSUBTABLES_META:
×
555
        // error code has been set into reqMsg, no need to handle it here.
556
        if (TSDB_CODE_SUCCESS != vnodeGetVSubtablesMeta(pVnode, &reqMsg)) {
×
557
          qWarn("vnodeGetVSubtablesMeta failed, msgType:%d", req->msgType);
×
558
        }
559
        break;
×
560
      case TDMT_VND_VSTB_REF_DBS:
5,256,451✔
561
        // error code has been set into reqMsg, no need to handle it here.
562
        if (TSDB_CODE_SUCCESS != vnodeGetVStbRefDbs(pVnode, &reqMsg)) {
5,256,451✔
563
          qWarn("vnodeGetVStbRefDbs failed, msgType:%d", req->msgType);
×
564
        }
565
        break;
5,261,601✔
566
      default:
×
567
        qError("invalid req msgType %d", req->msgType);
×
568
        reqMsg.code = TSDB_CODE_INVALID_MSG;
×
569
        reqMsg.pCont = NULL;
×
570
        reqMsg.contLen = 0;
×
571
        break;
×
572
    }
573

574
    rsp.msgIdx = req->msgIdx;
34,176,657✔
575
    rsp.reqType = reqMsg.msgType;
34,178,412✔
576
    rsp.msgLen = reqMsg.contLen;
34,178,412✔
577
    rsp.rspCode = reqMsg.code;
34,178,412✔
578
    rsp.msg = reqMsg.pCont;
34,178,412✔
579

580
    if (NULL == taosArrayPush(batchRsp.pRsps, &rsp)) {
68,355,618✔
581
      qError("taosArrayPush failed");
×
582
      code = terrno;
×
583
      goto _exit;
×
584
    }
585
  }
586

587
  rspSize = tSerializeSBatchRsp(NULL, 0, &batchRsp);
28,751,365✔
588
  if (rspSize < 0) {
28,724,700✔
589
    qError("tSerializeSBatchRsp failed");
×
590
    code = terrno;
×
591
    goto _exit;
×
592
  }
593
  pRsp = rpcMallocCont(rspSize);
28,724,700✔
594
  if (pRsp == NULL) {
28,736,309✔
595
    qError("rpcMallocCont %d failed", rspSize);
×
596
    code = terrno;
×
597
    goto _exit;
×
598
  }
599
  if (tSerializeSBatchRsp(pRsp, rspSize, &batchRsp) < 0) {
28,736,309✔
600
    qError("tSerializeSBatchRsp %d failed", rspSize);
×
601
    code = terrno;
×
602
    goto _exit;
×
603
  }
604

605
_exit:
28,747,113✔
606

607
  rspMsg.info = pMsg->info;
28,749,360✔
608
  rspMsg.pCont = pRsp;
28,751,329✔
609
  rspMsg.contLen = rspSize;
28,751,329✔
610
  rspMsg.code = code;
28,751,329✔
611
  rspMsg.msgType = pMsg->msgType;
28,751,329✔
612

613
  if (code) {
28,733,901✔
614
    qError("vnd get batch meta failed cause of %s", tstrerror(code));
×
615
  }
616

617
  taosArrayDestroyEx(batchReq.pMsgs, tFreeSBatchReqMsg);
28,733,901✔
618
  taosArrayDestroyEx(batchRsp.pRsps, tFreeSBatchRspMsg);
28,735,920✔
619

620
  tmsgSendRsp(&rspMsg);
28,727,782✔
621

622
  return code;
28,754,340✔
623
}
624

625
#define VNODE_DO_META_QUERY(pVnode, cmd)                 \
626
  do {                                                   \
627
    (void)taosThreadRwlockRdlock(&(pVnode)->metaRWLock); \
628
    cmd;                                                 \
629
    (void)taosThreadRwlockUnlock(&(pVnode)->metaRWLock); \
630
  } while (0)
631

632
int32_t vnodeReadVSubtables(SReadHandle* pHandle, int64_t suid, SArray** ppRes) {
×
633
  int32_t                    code = TSDB_CODE_SUCCESS;
×
634
  int32_t                    line = 0;
×
635
  SMetaReader                mr = {0};
×
636
  bool                       readerInit = false;
×
637
  SVCTableRefCols*           pTb = NULL;
×
638
  int32_t                    refColsNum = 0;
×
UNCOV
639
  char                       tbFName[TSDB_TABLE_FNAME_LEN];
×
640
  SSHashObj*                 pSrcTbls = NULL;
×
641

642
  SArray *pList = taosArrayInit(10, sizeof(uint64_t));
×
643
  QUERY_CHECK_NULL(pList, code, line, _return, terrno);
×
644

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

647
  size_t num = taosArrayGetSize(pList);
×
648
  *ppRes = taosArrayInit(num, POINTER_BYTES);
×
649
  QUERY_CHECK_NULL(*ppRes, code, line, _return, terrno);
×
650
  pSrcTbls = tSimpleHashInit(10, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
×
651
  QUERY_CHECK_NULL(pSrcTbls, code, line, _return, terrno);
×
652

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

660
    refColsNum = 0;
×
661
    for (int32_t j = 0; j < mr.me.colRef.nCols; j++) {
×
662
      if (mr.me.colRef.pColRef[j].hasRef) {
×
663
        refColsNum++;
×
664
      }
665
    }
666

667
    if (refColsNum <= 0) {
×
668
      pHandle->api.metaReaderFn.clearReader(&mr);
×
669
      readerInit = false;
×
670
      continue;
×
671
    }
672

673
    pTb = taosMemoryCalloc(1, refColsNum * sizeof(SRefColInfo) + sizeof(*pTb));
×
674
    QUERY_CHECK_NULL(pTb, code, line, _return, terrno);
×
675

676
    pTb->uid = mr.me.uid;
×
677
    pTb->numOfColRefs = refColsNum;
×
678
    pTb->refCols = (SRefColInfo*)(pTb + 1);
×
679

680
    refColsNum = 0;
×
681
    tSimpleHashClear(pSrcTbls);
×
682
    for (int32_t j = 0; j < mr.me.colRef.nCols; j++) {
×
683
      if (!mr.me.colRef.pColRef[j].hasRef) {
×
684
        continue;
×
685
      }
686

687
      pTb->refCols[refColsNum].colId = mr.me.colRef.pColRef[j].id;
×
688
      tstrncpy(pTb->refCols[refColsNum].refColName, mr.me.colRef.pColRef[j].refColName, TSDB_COL_NAME_LEN);
×
689
      tstrncpy(pTb->refCols[refColsNum].refTableName, mr.me.colRef.pColRef[j].refTableName, TSDB_TABLE_NAME_LEN);
×
690
      tstrncpy(pTb->refCols[refColsNum].refDbName, mr.me.colRef.pColRef[j].refDbName, TSDB_DB_NAME_LEN);
×
691

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

694
      if (NULL == tSimpleHashGet(pSrcTbls, tbFName, strlen(tbFName))) {
×
695
        QUERY_CHECK_CODE(tSimpleHashPut(pSrcTbls, tbFName, strlen(tbFName), &code, sizeof(code)), line, _return);
×
696
      }
697

698
      refColsNum++;
×
699
    }
700

701
    pTb->numOfSrcTbls = tSimpleHashGetSize(pSrcTbls);
×
702
    QUERY_CHECK_NULL(taosArrayPush(*ppRes, &pTb), code, line, _return, terrno);
×
703
    pTb = NULL;
×
704

705
    pHandle->api.metaReaderFn.clearReader(&mr);
×
706
    readerInit = false;
×
707
  }
708

709
_return:
×
710

711
  if (readerInit) {
×
712
    pHandle->api.metaReaderFn.clearReader(&mr);
×
713
  }
714

715
  taosArrayDestroy(pList);
×
716
  taosMemoryFree(pTb);
×
717
  tSimpleHashCleanup(pSrcTbls);
×
718

719
  if (code) {
×
720
    qError("%s failed since %s", __func__, tstrerror(code));
×
721
  }
722
  return code;
×
723
}
724

725
int32_t vnodeReadVStbRefDbs(SReadHandle* pHandle, int64_t suid, SArray** ppRes) {
5,257,562✔
726
  int32_t                    code = TSDB_CODE_SUCCESS;
5,257,562✔
727
  int32_t                    line = 0;
5,257,562✔
728
  SMetaReader                mr = {0};
5,257,562✔
729
  bool                       readerInit = false;
5,258,046✔
730
  SSHashObj*                 pDbNameHash = NULL;
5,258,046✔
731
  SArray*                    pList = NULL;
5,258,046✔
732

733
  pList = taosArrayInit(10, sizeof(uint64_t));
5,258,046✔
734
  QUERY_CHECK_NULL(pList, code, line, _return, terrno);
5,250,953✔
735

736
  *ppRes = taosArrayInit(10, POINTER_BYTES);
5,250,953✔
737
  QUERY_CHECK_NULL(*ppRes, code, line, _return, terrno)
5,257,840✔
738
  
739
  // lookup in cache
740
  code = pHandle->api.metaFn.metaGetCachedRefDbs(pHandle->vnode, suid, *ppRes);
5,261,433✔
741
  QUERY_CHECK_CODE(code, line, _return);
5,271,619✔
742

743
  if (taosArrayGetSize(*ppRes) > 0) {
5,271,619✔
744
    // found in cache
745
    goto _return;
5,228,452✔
746
  } else {
747
    code = pHandle->api.metaFn.getChildTableList(pHandle->vnode, suid, pList);
43,764✔
748
    QUERY_CHECK_CODE(code, line, _return);
43,764✔
749

750
    size_t num = taosArrayGetSize(pList);
43,764✔
751
    pDbNameHash = tSimpleHashInit(10, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
43,764✔
752
    QUERY_CHECK_NULL(pDbNameHash, code, line, _return, terrno);
43,764✔
753

754
    for (int32_t i = 0; i < num; ++i) {
139,116✔
755
      uint64_t* id = taosArrayGet(pList, i);
95,352✔
756
      QUERY_CHECK_NULL(id, code, line, _return, terrno);
94,744✔
757

758
      pHandle->api.metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pHandle->api.metaFn);
94,744✔
759
      readerInit = true;
94,744✔
760

761
      code = pHandle->api.metaReaderFn.getTableEntryByUid(&mr, *id);
94,744✔
762
      QUERY_CHECK_CODE(code, line, _return);
95,352✔
763

764
      for (int32_t j = 0; j < mr.me.colRef.nCols; j++) {
37,651,422✔
765
        if (mr.me.colRef.pColRef[j].hasRef) {
37,556,070✔
766
          if (NULL == tSimpleHashGet(pDbNameHash, mr.me.colRef.pColRef[j].refDbName, strlen(mr.me.colRef.pColRef[j].refDbName))) {
37,128,519✔
767
            char *refDbName = taosStrdup(mr.me.colRef.pColRef[j].refDbName);
37,594✔
768
            QUERY_CHECK_NULL(refDbName, code, line, _return, terrno);
37,594✔
769

770
            QUERY_CHECK_NULL(taosArrayPush(*ppRes, &refDbName), code, line, _return, terrno);
75,188✔
771

772
            code = tSimpleHashPut(pDbNameHash, refDbName, strlen(refDbName), NULL, 0);
37,594✔
773
            QUERY_CHECK_CODE(code, line, _return);
37,594✔
774
          }
775
        }
776
      }
777

778
      pHandle->api.metaReaderFn.clearReader(&mr);
95,352✔
779
      readerInit = false;
95,352✔
780
    }
781

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

786
_return:
5,272,216✔
787

788
  if (readerInit) {
5,269,228✔
789
    pHandle->api.metaReaderFn.clearReader(&mr);
×
790
  }
791

792
  taosArrayDestroy(pList);
5,269,228✔
793
  tSimpleHashCleanup(pDbNameHash);
5,264,104✔
794

795
  if (code) {
5,265,144✔
796
    qError("%s failed since %s", __func__, tstrerror(code));
×
797
  }
798
  return code;
5,265,144✔
799
}
800

801
int32_t vnodeGetVSubtablesMeta(SVnode *pVnode, SRpcMsg *pMsg) {
×
802
  int32_t        code = 0;
×
803
  int32_t        rspSize = 0;
×
804
  SVSubTablesReq req = {0};
×
805
  SVSubTablesRsp rsp = {0};
×
806
  SRpcMsg      rspMsg = {0};
×
807
  void        *pRsp = NULL;
×
808
  int32_t      line = 0;
×
809

810
  if (tDeserializeSVSubTablesReq(pMsg->pCont, pMsg->contLen, &req)) {
×
811
    code = terrno;
×
812
    qError("tDeserializeSVSubTablesReq failed");
×
813
    goto _return;
×
814
  }
815

816
  SReadHandle handle = {0};
×
817
  handle.vnode = pVnode;
×
818
  initStorageAPI(&handle.api);
×
819

820
  QUERY_CHECK_CODE(vnodeReadVSubtables(&handle, req.suid, &rsp.pTables), line, _return);
×
821
  rsp.vgId = TD_VID(pVnode);
×
822

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

842
_return:
×
843

844
  rspMsg.info = pMsg->info;
×
845
  rspMsg.pCont = pRsp;
×
846
  rspMsg.contLen = rspSize;
×
847
  rspMsg.code = code;
×
848
  rspMsg.msgType = pMsg->msgType;
×
849

850
  if (code) {
×
851
    qError("vnd get virtual subtables failed cause of %s", tstrerror(code));
×
852
  }
853

854
  *pMsg = rspMsg;
×
855
  
856
  tDestroySVSubTablesRsp(&rsp);
×
857

858
  //tmsgSendRsp(&rspMsg);
859

860
  return code;
×
861
}
862

863
int32_t vnodeGetVStbRefDbs(SVnode *pVnode, SRpcMsg *pMsg) {
5,266,583✔
864
  int32_t        code = 0;
5,266,583✔
865
  int32_t        rspSize = 0;
5,266,583✔
866
  SVStbRefDbsReq req = {0};
5,266,583✔
867
  SVStbRefDbsRsp rsp = {0};
5,266,608✔
868
  SRpcMsg        rspMsg = {0};
5,253,999✔
869
  void          *pRsp = NULL;
5,253,999✔
870
  int32_t        line = 0;
5,253,999✔
871

872
  if (tDeserializeSVStbRefDbsReq(pMsg->pCont, pMsg->contLen, &req)) {
5,253,999✔
873
    code = terrno;
×
874
    qError("tDeserializeSVSubTablesReq failed");
×
875
    goto _return;
×
876
  }
877

878
  SReadHandle handle = {0};
5,257,949✔
879
  handle.vnode = pVnode;
5,263,863✔
880
  initStorageAPI(&handle.api);
5,263,863✔
881

882
  code = vnodeReadVStbRefDbs(&handle, req.suid, &rsp.pDbs);
5,263,212✔
883
  QUERY_CHECK_CODE(code, line, _return);
5,265,013✔
884
  rsp.vgId = TD_VID(pVnode);
5,265,013✔
885

886
  rspSize = tSerializeSVStbRefDbsRsp(NULL, 0, &rsp);
5,266,492✔
887
  if (rspSize < 0) {
5,249,008✔
888
    code = rspSize;
×
889
    qError("tSerializeSVStbRefDbsRsp failed, error:%d", rspSize);
×
890
    goto _return;
×
891
  }
892
  pRsp = taosMemoryCalloc(1, rspSize);
5,249,008✔
893
  if (pRsp == NULL) {
5,262,106✔
894
    code = terrno;
×
895
    qError("rpcMallocCont %d failed, error:%d", rspSize, terrno);
×
896
    goto _return;
×
897
  }
898
  rspSize = tSerializeSVStbRefDbsRsp(pRsp, rspSize, &rsp);
5,262,106✔
899
  if (rspSize < 0) {
5,261,138✔
900
    code = rspSize;
×
901
    qError("tSerializeSVStbRefDbsRsp failed, error:%d", rspSize);
×
902
    goto _return;
×
903
  }
904

905
_return:
5,261,138✔
906

907
  rspMsg.info = pMsg->info;
5,257,734✔
908
  rspMsg.pCont = pRsp;
5,260,170✔
909
  rspMsg.contLen = rspSize;
5,260,170✔
910
  rspMsg.code = code;
5,260,170✔
911
  rspMsg.msgType = pMsg->msgType;
5,260,170✔
912

913
  if (code) {
5,245,973✔
914
    qError("vnd get virtual stb ref db failed cause of %s", tstrerror(code));
×
915
  }
916

917
  *pMsg = rspMsg;
5,245,973✔
918

919
  tDestroySVStbRefDbsRsp(&rsp);
5,247,038✔
920

921
  return code;
5,254,201✔
922
}
923

924
static int32_t vnodeGetCompStorage(SVnode *pVnode, int64_t *output) {
140,209,887✔
925
  int32_t code = 0;
140,209,887✔
926
#ifdef TD_ENTERPRISE
927
  int32_t now = taosGetTimestampSec();
140,209,887✔
928
  if (llabs(now - pVnode->config.vndStats.storageLastUpd) >= 30) {
140,209,887✔
929
    pVnode->config.vndStats.storageLastUpd = now;
7,300,763✔
930

931
    SDbSizeStatisInfo info = {0};
7,300,763✔
932
    if (0 == (code = vnodeGetDBSize(pVnode, &info))) {
7,300,763✔
933
      int64_t compSize =
7,300,763✔
934
          info.l1Size + info.l2Size + info.l3Size + info.cacheSize + info.walSize + info.metaSize + +info.ssSize;
7,300,763✔
935
      if (compSize >= 0) {
7,300,763✔
936
        pVnode->config.vndStats.compStorage = compSize;
7,300,763✔
937
      } else {
938
        vError("vnode get comp storage failed since compSize is negative:%" PRIi64, compSize);
×
939
        code = TSDB_CODE_APP_ERROR;
×
940
      }
941
    } else {
942
      vWarn("vnode get comp storage failed since %s", tstrerror(code));
×
943
    }
944
  }
945
  if (output) *output = pVnode->config.vndStats.compStorage;
140,209,887✔
946
#endif
947
  return code;
140,209,887✔
948
}
949

950
static void vnodeGetBufferInfo(SVnode *pVnode, int64_t *bufferSegmentUsed, int64_t *bufferSegmentSize) {
140,209,887✔
951
  *bufferSegmentUsed = 0;
140,209,887✔
952
  *bufferSegmentSize = 0;
140,209,887✔
953
  if (pVnode) {
140,209,887✔
954
    (void)taosThreadMutexLock(&pVnode->mutex);
140,209,887✔
955

956
    if (pVnode->inUse) {
140,209,887✔
957
      *bufferSegmentUsed = pVnode->inUse->size;
140,199,437✔
958
    }
959
    *bufferSegmentSize = pVnode->config.szBuf / VNODE_BUFPOOL_SEGMENTS;
140,209,887✔
960

961
    (void)taosThreadMutexUnlock(&pVnode->mutex);
140,209,887✔
962
  }
963
}
140,209,887✔
964

965
int32_t vnodeGetLoad(SVnode *pVnode, SVnodeLoad *pLoad) {
140,209,887✔
966
  SSyncState state = syncGetState(pVnode->sync);
140,209,887✔
967
  pLoad->syncAppliedIndex = pVnode->state.applied;
140,209,887✔
968
  syncGetCommitIndex(pVnode->sync, &pLoad->syncCommitIndex);
140,209,887✔
969

970
  pLoad->vgId = TD_VID(pVnode);
140,209,887✔
971
  pLoad->syncState = state.state;
140,209,887✔
972
  pLoad->syncRestore = state.restored;
140,209,887✔
973
  pLoad->syncTerm = state.term;
140,209,887✔
974
  pLoad->roleTimeMs = state.roleTimeMs;
140,209,887✔
975
  pLoad->startTimeMs = state.startTimeMs;
140,209,887✔
976
  pLoad->syncCanRead = state.canRead;
140,209,887✔
977
  pLoad->cacheUsage = tsdbCacheGetUsage(pVnode);
140,209,887✔
978
  pLoad->numOfCachedTables = tsdbCacheGetElems(pVnode);
140,209,887✔
979
  VNODE_DO_META_QUERY(pVnode, pLoad->numOfTables = metaGetTbNum(pVnode->pMeta));
140,209,887✔
980
  VNODE_DO_META_QUERY(pVnode, pLoad->numOfTimeSeries = metaGetTimeSeriesNum(pVnode->pMeta, 1));
140,209,887✔
981
  pLoad->totalStorage = (int64_t)3 * 1073741824;  // TODO
140,209,887✔
982
  (void)vnodeGetCompStorage(pVnode, &pLoad->compStorage);
140,209,887✔
983
  pLoad->pointsWritten = 100;
140,209,887✔
984
  pLoad->numOfSelectReqs = 1;
140,209,887✔
985
  pLoad->numOfInsertReqs = atomic_load_64(&pVnode->statis.nInsert);
140,209,887✔
986
  pLoad->numOfInsertSuccessReqs = atomic_load_64(&pVnode->statis.nInsertSuccess);
140,209,887✔
987
  pLoad->numOfBatchInsertReqs = atomic_load_64(&pVnode->statis.nBatchInsert);
140,209,887✔
988
  pLoad->numOfBatchInsertSuccessReqs = atomic_load_64(&pVnode->statis.nBatchInsertSuccess);
140,209,887✔
989
  vnodeGetBufferInfo(pVnode, &pLoad->bufferSegmentUsed, &pLoad->bufferSegmentSize);
140,209,887✔
990
  vDebug("vgId:%d, get vnode load, state:%s snapSeq:%d, learnerProgress:%d, totalIndex:%" PRId64, TD_VID(pVnode),
140,209,887✔
991
         syncStr(state.state), state.snapSeq, state.progress, state.totalIndex);
992
  pLoad->learnerProgress = state.progress;
140,209,887✔
993
  pLoad->snapSeq = state.snapSeq;
140,209,887✔
994
  pLoad->syncTotalIndex = state.totalIndex;
140,209,887✔
995
  return 0;
140,209,887✔
996
}
997

998
int32_t vnodeGetLoadLite(SVnode *pVnode, SVnodeLoadLite *pLoad) {
×
999
  SSyncState syncState = syncGetState(pVnode->sync);
×
1000
  if (syncState.state == TAOS_SYNC_STATE_LEADER || syncState.state == TAOS_SYNC_STATE_ASSIGNED_LEADER) {
×
1001
    pLoad->vgId = TD_VID(pVnode);
×
1002
    pLoad->nTimeSeries = metaGetTimeSeriesNum(pVnode->pMeta, 1);
×
1003
    return 0;
×
1004
  }
1005
  return -1;
×
1006
}
1007
/**
1008
 * @brief Reset the statistics value by monitor interval
1009
 *
1010
 * @param pVnode
1011
 * @param pLoad
1012
 */
1013
void vnodeResetLoad(SVnode *pVnode, SVnodeLoad *pLoad) {
462✔
1014
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nInsert, pLoad->numOfInsertReqs, 64, "nInsert");
462✔
1015
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nInsertSuccess, pLoad->numOfInsertSuccessReqs, 64, "nInsertSuccess");
462✔
1016
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nBatchInsert, pLoad->numOfBatchInsertReqs, 64, "nBatchInsert");
462✔
1017
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nBatchInsertSuccess, pLoad->numOfBatchInsertSuccessReqs, 64,
462✔
1018
                            "nBatchInsertSuccess");
1019
}
462✔
1020

1021
void vnodeGetInfo(void *pVnode, const char **dbname, int32_t *vgId, int64_t *numOfTables, int64_t *numOfNormalTables) {
16,303,813✔
1022
  SVnode    *pVnodeObj = pVnode;
16,303,813✔
1023
  SVnodeCfg *pConf = &pVnodeObj->config;
16,303,813✔
1024

1025
  if (dbname) {
16,305,312✔
1026
    *dbname = pConf->dbname;
16,285,483✔
1027
  }
1028

1029
  if (vgId) {
16,303,875✔
1030
    *vgId = TD_VID(pVnodeObj);
15,426,174✔
1031
  }
1032

1033
  if (numOfTables) {
16,305,146✔
1034
    *numOfTables = pConf->vndStats.numOfNTables + pConf->vndStats.numOfCTables +
22,736✔
1035
                   pConf->vndStats.numOfVTables + pConf->vndStats.numOfVCTables;
11,368✔
1036
  }
1037

1038
  if (numOfNormalTables) {
16,305,146✔
1039
    *numOfNormalTables = pConf->vndStats.numOfNTables +
16,812✔
1040
                         pConf->vndStats.numOfVTables;
8,406✔
1041
  }
1042
}
16,305,146✔
1043

1044
int32_t vnodeGetTableList(void *pVnode, int8_t type, SArray *pList) {
8,406✔
1045
  if (type == TSDB_SUPER_TABLE) {
8,406✔
1046
    return vnodeGetStbIdList(pVnode, 0, pList);
8,406✔
1047
  } else {
1048
    return TSDB_CODE_INVALID_PARA;
×
1049
  }
1050
}
1051

1052
int32_t vnodeGetAllTableList(SVnode *pVnode, uint64_t uid, SArray *list) {
×
1053
  int32_t      code = TSDB_CODE_SUCCESS;
×
1054
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, uid, 1);
×
1055
  if (NULL == pCur) {
×
1056
    qError("vnode get all table list failed");
×
1057
    return terrno;
×
1058
  }
1059

1060
  while (1) {
×
1061
    tb_uid_t id = metaCtbCursorNext(pCur);
×
1062
    if (id == 0) {
×
1063
      break;
×
1064
    }
1065

1066
    STableKeyInfo info = {uid = id};
×
1067
    if (NULL == taosArrayPush(list, &info)) {
×
1068
      qError("taosArrayPush failed");
×
1069
      code = terrno;
×
1070
      goto _exit;
×
1071
    }
1072
  }
1073
_exit:
×
1074
  metaCloseCtbCursor(pCur);
×
1075
  return code;
×
1076
}
1077

1078
int32_t vnodeGetCtbIdListByFilter(SVnode *pVnode, int64_t suid, SArray *list, bool (*filter)(void *arg), void *arg) {
×
1079
  return 0;
×
1080
}
1081

1082
int32_t vnodeGetCtbIdList(void *pVnode, int64_t suid, SArray *list) {
105,629,539✔
1083
  int32_t      code = TSDB_CODE_SUCCESS;
105,629,539✔
1084
  SVnode      *pVnodeObj = pVnode;
105,629,539✔
1085
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnodeObj, suid, 1);
105,629,539✔
1086
  if (NULL == pCur) {
105,595,510✔
1087
    qError("vnode get all table list failed");
×
1088
    return terrno;
×
1089
  }
1090

1091
  while (1) {
468,815,070✔
1092
    tb_uid_t id = metaCtbCursorNext(pCur);
574,410,580✔
1093
    if (id == 0) {
574,367,685✔
1094
      break;
105,649,480✔
1095
    }
1096
    qTrace("vnodeGetCtbIdList: got ctb id %" PRId64 " for suid %" PRId64, id, suid);
468,718,205✔
1097
    if (NULL == taosArrayPush(list, &id)) {
468,810,645✔
1098
      qError("taosArrayPush failed");
×
1099
      code = terrno;
×
1100
      goto _exit;
×
1101
    }
1102
  }
1103

1104
_exit:
105,594,360✔
1105
  metaCloseCtbCursor(pCur);
105,594,360✔
1106
  return code;
105,612,548✔
1107
}
1108

1109
int32_t vnodeGetStbIdList(SVnode *pVnode, int64_t suid, SArray *list) {
66,603,482✔
1110
  int32_t      code = TSDB_CODE_SUCCESS;
66,603,482✔
1111
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, suid);
66,603,482✔
1112
  if (!pCur) {
66,600,830✔
1113
    return TSDB_CODE_OUT_OF_MEMORY;
×
1114
  }
1115

1116
  while (1) {
48,673,879✔
1117
    tb_uid_t id = metaStbCursorNext(pCur);
115,274,709✔
1118
    if (id == 0) {
115,274,046✔
1119
      break;
66,608,123✔
1120
    }
1121

1122
    if (NULL == taosArrayPush(list, &id)) {
48,680,509✔
1123
      qError("taosArrayPush failed");
×
1124
      code = terrno;
×
1125
      goto _exit;
×
1126
    }
1127
  }
1128

1129
_exit:
66,603,482✔
1130
  metaCloseStbCursor(pCur);
66,603,482✔
1131
  return code;
66,602,819✔
1132
}
1133

1134
int32_t vnodeGetStbIdListByFilter(SVnode *pVnode, int64_t suid, SArray *list, bool (*filter)(void *arg, void *arg1),
3,246✔
1135
                                  void *arg) {
1136
  int32_t      code = TSDB_CODE_SUCCESS;
3,246✔
1137
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, suid);
3,246✔
1138
  if (!pCur) {
3,246✔
1139
    return terrno;
×
1140
  }
1141

1142
  while (1) {
4,562✔
1143
    tb_uid_t id = metaStbCursorNext(pCur);
7,808✔
1144
    if (id == 0) {
7,808✔
1145
      break;
3,246✔
1146
    }
1147

1148
    if ((*filter) && (*filter)(arg, &id)) {
4,562✔
1149
      continue;
4,562✔
1150
    }
1151

1152
    if (NULL == taosArrayPush(list, &id)) {
×
1153
      qError("taosArrayPush failed");
×
1154
      code = terrno;
×
1155
      goto _exit;
×
1156
    }
1157
  }
1158

1159
_exit:
3,246✔
1160
  metaCloseStbCursor(pCur);
3,246✔
1161
  return code;
3,246✔
1162
}
1163

1164
int32_t vnodeGetCtbNum(SVnode *pVnode, int64_t suid, int64_t *num) {
4,836,179✔
1165
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, suid, 0);
4,836,179✔
1166
  if (!pCur) {
4,835,904✔
1167
    return terrno;
×
1168
  }
1169

1170
  *num = 0;
4,835,904✔
1171
  while (1) {
2,772,839✔
1172
    tb_uid_t id = metaCtbCursorNext(pCur);
7,609,109✔
1173
    if (id == 0) {
7,608,128✔
1174
      break;
4,836,303✔
1175
    }
1176

1177
    ++(*num);
2,771,825✔
1178
  }
1179

1180
  metaCloseCtbCursor(pCur);
4,836,303✔
1181
  return TSDB_CODE_SUCCESS;
4,836,003✔
1182
}
1183

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

1193
  return TSDB_CODE_SUCCESS;
4,836,633✔
1194
}
1195

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

1200
  int32_t code = metaReaderGetTableEntryByUid(&mr, suid);
4,835,645✔
1201
  if (code == TSDB_CODE_SUCCESS) {
4,836,613✔
1202
    if (keep) *keep = mr.me.stbEntry.keep;
4,836,613✔
1203
    if (flags) *flags = mr.me.flags;
4,836,613✔
1204
  } else {
1205
    if (keep) *keep = 0;
×
1206
    if (flags) *flags = 0;
×
1207
  }
1208

1209
  metaReaderClear(&mr);
4,836,242✔
1210
  return TSDB_CODE_SUCCESS;
4,836,979✔
1211
}
1212

1213
#ifdef TD_ENTERPRISE
1214
const char *tkLogStb[] = {"cluster_info",
1215
                          "data_dir",
1216
                          "dnodes_info",
1217
                          "d_info",
1218
                          "grants_info",
1219
                          "keeper_monitor",
1220
                          "logs",
1221
                          "log_dir",
1222
                          "log_summary",
1223
                          "m_info",
1224
                          "taosadapter_restful_http_request_fail",
1225
                          "taosadapter_restful_http_request_in_flight",
1226
                          "taosadapter_restful_http_request_summary_milliseconds",
1227
                          "taosadapter_restful_http_request_total",
1228
                          "taosadapter_system_cpu_percent",
1229
                          "taosadapter_system_mem_percent",
1230
                          "temp_dir",
1231
                          "vgroups_info",
1232
                          "vnodes_role",
1233
                          "taosd_dnodes_status",
1234
                          "adapter_conn_pool",
1235
                          "taosd_vnodes_info",
1236
                          "taosd_dnodes_metrics",
1237
                          "taosd_vgroups_info",
1238
                          "taos_sql_req",
1239
                          "taosd_mnodes_info",
1240
                          "adapter_c_interface",
1241
                          "taosd_cluster_info",
1242
                          "taosd_sql_req",
1243
                          "taosd_dnodes_info",
1244
                          "adapter_requests",
1245
                          "taosd_write_metrics",
1246
                          "adapter_status",
1247
                          "taos_slow_sql",
1248
                          "taos_slow_sql_detail",
1249
                          "taosd_cluster_basic",
1250
                          "taosd_dnodes_data_dirs",
1251
                          "taosd_dnodes_log_dirs",
1252
                          "xnode_agent_activities",
1253
                          "xnode_task_activities",
1254
                          "xnode_task_metrics",
1255
                          "taosx_task_csv",
1256
                          "taosx_task_progress",
1257
                          "taosx_task_kinghist",
1258
                          "taosx_task_tdengine2",
1259
                          "taosx_task_tdengine3",
1260
                          "taosx_task_opc_da",
1261
                          "taosx_task_opc_ua",
1262
                          "taosx_task_kafka",
1263
                          "taosx_task_influxdb",
1264
                          "taosx_task_mqtt",
1265
                          "taosx_task_avevahistorian",
1266
                          "taosx_task_opentsdb",
1267
                          "taosx_task_mysql",
1268
                          "taosx_task_postgres",
1269
                          "taosx_task_oracle",
1270
                          "taosx_task_mssql",
1271
                          "taosx_task_mongodb",
1272
                          "taosx_task_sparkplugb",
1273
                          "taosx_task_orc",
1274
                          "taosx_task_pulsar",
1275
                          "taosx_task_pspace"};
1276
const char *tkAuditStb[] = {"operations"};
1277
const int   tkLogStbNum = ARRAY_SIZE(tkLogStb);
1278
const int   tkAuditStbNum = ARRAY_SIZE(tkAuditStb);
1279

1280
// exclude stbs of taoskeeper log
1281
static int32_t vnodeGetTimeSeriesBlackList(SVnode *pVnode, int32_t *tbSize) {
66,600,438✔
1282
  int32_t      code = TSDB_CODE_SUCCESS;
66,600,438✔
1283
  int32_t      tbNum = 0;
66,600,438✔
1284
  const char **pTbArr = NULL;
66,600,438✔
1285
  const char  *dbName = NULL;
66,600,438✔
1286
  *tbSize = 0;
66,600,438✔
1287

1288
  if (!(dbName = strchr(pVnode->config.dbname, '.'))) return 0;
66,597,786✔
1289
  if (0 == strncmp(++dbName, "log", TSDB_DB_NAME_LEN)) {
66,598,084✔
1290
    tbNum = tkLogStbNum;
3,410✔
1291
    pTbArr = (const char **)&tkLogStb;
3,410✔
1292
  } else if (0 == strncmp(dbName, "audit", TSDB_DB_NAME_LEN) || pVnode->config.isAudit) {
66,594,674✔
1293
    tbNum = tkAuditStbNum;
4,151✔
1294
    pTbArr = (const char **)&tkAuditStb;
4,151✔
1295
  }
1296
  if (tbNum && pTbArr) {
66,601,399✔
1297
    *tbSize = metaSizeOfTbFilterCache(pVnode->pMeta, 0);
7,561✔
1298
    if (*tbSize < tbNum) {
7,561✔
1299
      for (int32_t i = 0; i < tbNum; ++i) {
64,140✔
1300
        tb_uid_t suid = metaGetTableEntryUidByName(pVnode->pMeta, pTbArr[i]);
62,055✔
1301
        if (suid != 0) {
62,055✔
1302
          code = metaPutTbToFilterCache(pVnode->pMeta, &suid, 0);
4,554✔
1303
          if (TSDB_CODE_SUCCESS != code) {
4,554✔
1304
            return code;
2,734✔
1305
          }
1306
        }
1307
      }
1308
      *tbSize = metaSizeOfTbFilterCache(pVnode->pMeta, 0);
2,085✔
1309
    }
1310
  }
1311

1312
  return code;
66,597,041✔
1313
}
1314
#endif
1315

1316
static bool vnodeTimeSeriesFilter(void *arg1, void *arg2) {
4,562✔
1317
  SVnode *pVnode = (SVnode *)arg1;
4,562✔
1318

1319
  if (metaTbInFilterCache(pVnode->pMeta, arg2, 0)) {
4,562✔
1320
    return true;
4,562✔
1321
  }
UNCOV
1322
  return false;
×
1323
}
1324

1325
int32_t vnodeGetTimeSeriesNum(SVnode *pVnode, int64_t *num) {
66,600,438✔
1326
  SArray *suidList = NULL;
66,600,438✔
1327

1328
  if (!(suidList = taosArrayInit(1, sizeof(tb_uid_t)))) {
66,600,438✔
1329
    return terrno;
×
1330
  }
1331

1332
  int32_t tbFilterSize = 0;
66,594,471✔
1333
  int32_t code = TSDB_CODE_SUCCESS;
66,599,775✔
1334
#ifdef TD_ENTERPRISE
1335
  code = vnodeGetTimeSeriesBlackList(pVnode, &tbFilterSize);
66,599,775✔
1336
  if (TSDB_CODE_SUCCESS != code) {
66,594,471✔
1337
    goto _exit;
2,734✔
1338
  }
1339
#endif
1340

1341
  if ((!tbFilterSize && vnodeGetStbIdList(pVnode, 0, suidList) < 0) ||
66,591,737✔
1342
      (tbFilterSize && vnodeGetStbIdListByFilter(pVnode, 0, suidList, vnodeTimeSeriesFilter, pVnode) < 0)) {
66,595,715✔
1343
    qError("vgId:%d, failed to get stb id list error: %s", TD_VID(pVnode), terrstr());
×
1344
    taosArrayDestroy(suidList);
×
UNCOV
1345
    return terrno;
×
1346
  }
1347

1348
  *num = 0;
66,597,041✔
1349
  int64_t arrSize = taosArrayGetSize(suidList);
66,597,041✔
1350
  for (int64_t i = 0; i < arrSize; ++i) {
115,262,330✔
1351
    tb_uid_t suid = *(tb_uid_t *)taosArrayGet(suidList, i);
48,659,985✔
1352

1353
    int64_t ctbNum = 0;
48,662,637✔
1354
    int32_t numOfCols = 0;
48,663,300✔
1355
    int8_t  flags = 0;
48,658,659✔
1356
    code = metaGetStbStats(pVnode, suid, &ctbNum, &numOfCols, &flags);
48,658,659✔
1357
    if (TSDB_CODE_SUCCESS != code) {
48,671,919✔
UNCOV
1358
      goto _exit;
×
1359
    }
1360
    if (!TABLE_IS_VIRTUAL(flags)) {
48,671,919✔
1361
      *num += ctbNum * (numOfCols - 1);
45,364,078✔
1362
    }
1363
  }
1364

1365
_exit:
66,605,271✔
1366
  taosArrayDestroy(suidList);
66,605,079✔
1367
  return TSDB_CODE_SUCCESS;
66,598,449✔
1368
}
1369

1370
int32_t vnodeGetAllCtbNum(SVnode *pVnode, int64_t *num) {
×
UNCOV
1371
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, 0);
×
UNCOV
1372
  if (!pCur) {
×
UNCOV
1373
    return terrno;
×
1374
  }
1375

UNCOV
1376
  *num = 0;
×
UNCOV
1377
  while (1) {
×
UNCOV
1378
    tb_uid_t id = metaStbCursorNext(pCur);
×
UNCOV
1379
    if (id == 0) {
×
UNCOV
1380
      break;
×
1381
    }
1382

UNCOV
1383
    int64_t ctbNum = 0;
×
UNCOV
1384
    int32_t code = vnodeGetCtbNum(pVnode, id, &ctbNum);
×
UNCOV
1385
    if (TSDB_CODE_SUCCESS != code) {
×
UNCOV
1386
      metaCloseStbCursor(pCur);
×
UNCOV
1387
      return code;
×
1388
    }
1389

UNCOV
1390
    *num += ctbNum;
×
1391
  }
1392

UNCOV
1393
  metaCloseStbCursor(pCur);
×
UNCOV
1394
  return TSDB_CODE_SUCCESS;
×
1395
}
1396

1397
void *vnodeGetIdx(void *pVnode) {
4,198,956✔
1398
  if (pVnode == NULL) {
4,198,956✔
UNCOV
1399
    return NULL;
×
1400
  }
1401

1402
  return metaGetIdx(((SVnode *)pVnode)->pMeta);
4,198,956✔
1403
}
1404

1405
void *vnodeGetIvtIdx(void *pVnode) {
4,198,956✔
1406
  if (pVnode == NULL) {
4,198,956✔
UNCOV
1407
    return NULL;
×
1408
  }
1409
  return metaGetIvtIdx(((SVnode *)pVnode)->pMeta);
4,198,956✔
1410
}
1411

1412
int32_t vnodeGetTableSchema(void *pVnode, int64_t uid, STSchema **pSchema, int64_t *suid, SSchemaWrapper **pTagSchema) {
322,883✔
1413
  return tsdbGetTableSchema(((SVnode *)pVnode)->pMeta, uid, pSchema, suid, pTagSchema);
322,883✔
1414
}
1415

1416
static FORCE_INLINE int32_t vnodeGetDBPrimaryInfo(SVnode *pVnode, SDbSizeStatisInfo *pInfo) {
1417
  int32_t code = 0;
7,307,179✔
1418
  char    path[TSDB_FILENAME_LEN] = {0};
7,307,179✔
1419

1420
  char   *dirName[] = {VNODE_TSDB_DIR, VNODE_WAL_DIR, VNODE_META_DIR, VNODE_TSDB_CACHE_DIR};
7,307,015✔
1421
  int64_t dirSize[4];
7,305,520✔
1422

1423
  vnodeGetPrimaryPath(pVnode, false, path, TSDB_FILENAME_LEN);
7,307,015✔
1424
  int32_t offset = strlen(path);
7,307,179✔
1425

1426
  for (int i = 0; i < sizeof(dirName) / sizeof(dirName[0]); i++) {
36,535,895✔
1427
    int64_t size = {0};
29,228,716✔
1428
    (void)snprintf(path + offset, TSDB_FILENAME_LEN - offset, "%s%s", TD_DIRSEP, dirName[i]);
29,228,716✔
1429
    code = taosGetDirSize(path, &size);
29,228,716✔
1430
    if (code != 0) {
29,228,716✔
1431
      uWarn("vnode %d get dir %s %s size failed since %s", TD_VID(pVnode), path, dirName[i], tstrerror(code));
2,802✔
1432
    }
1433
    path[offset] = 0;
29,228,716✔
1434
    dirSize[i] = size;
29,228,716✔
1435
  }
1436

1437
  pInfo->l1Size = 0;
7,307,179✔
1438
  pInfo->walSize = dirSize[1];
7,307,179✔
1439
  pInfo->metaSize = dirSize[2];
7,307,179✔
1440
  pInfo->cacheSize = dirSize[3];
7,307,179✔
1441
  return code;
7,307,179✔
1442
}
1443
int32_t vnodeGetDBSize(void *pVnode, SDbSizeStatisInfo *pInfo) {
7,307,179✔
1444
  int32_t code = 0;
7,307,179✔
1445
  int32_t lino = 0;
7,307,179✔
1446
  SVnode *pVnodeObj = pVnode;
7,307,179✔
1447
  if (pVnodeObj == NULL) {
7,307,179✔
UNCOV
1448
    return TSDB_CODE_VND_NOT_EXIST;
×
1449
  }
1450
  code = vnodeGetDBPrimaryInfo(pVnode, pInfo);
7,307,179✔
1451
  if (code != 0) goto _exit;
7,307,179✔
1452

1453
  code = tsdbGetFsSize(pVnodeObj->pTsdb, pInfo);
7,307,179✔
1454
_exit:
7,307,179✔
1455
  return code;
7,307,179✔
1456
}
1457

1458
/*
1459
 * Get raw write metrics for a vnode
1460
 */
1461
int32_t vnodeGetRawWriteMetrics(void *pVnode, SRawWriteMetrics *pRawMetrics) {
×
1462
  if (pVnode == NULL || pRawMetrics == NULL) {
×
1463
    return TSDB_CODE_INVALID_PARA;
×
1464
  }
1465

1466
  SVnode      *pVnode1 = (SVnode *)pVnode;
×
1467
  SSyncMetrics syncMetrics = syncGetMetrics(pVnode1->sync);
×
1468

1469
  // Copy values following SRawWriteMetrics structure order
1470
  pRawMetrics->total_requests = atomic_load_64(&pVnode1->writeMetrics.total_requests);
×
UNCOV
1471
  pRawMetrics->total_rows = atomic_load_64(&pVnode1->writeMetrics.total_rows);
×
1472
  pRawMetrics->total_bytes = atomic_load_64(&pVnode1->writeMetrics.total_bytes);
×
1473
  pRawMetrics->fetch_batch_meta_time = atomic_load_64(&pVnode1->writeMetrics.fetch_batch_meta_time);
×
UNCOV
1474
  pRawMetrics->fetch_batch_meta_count = atomic_load_64(&pVnode1->writeMetrics.fetch_batch_meta_count);
×
1475
  pRawMetrics->preprocess_time = atomic_load_64(&pVnode1->writeMetrics.preprocess_time);
×
1476
  pRawMetrics->wal_write_bytes = atomic_load_64(&syncMetrics.wal_write_bytes);
×
1477
  pRawMetrics->wal_write_time = atomic_load_64(&syncMetrics.wal_write_time);
×
1478
  pRawMetrics->apply_bytes = atomic_load_64(&pVnode1->writeMetrics.apply_bytes);
×
UNCOV
1479
  pRawMetrics->apply_time = atomic_load_64(&pVnode1->writeMetrics.apply_time);
×
UNCOV
1480
  pRawMetrics->commit_count = atomic_load_64(&pVnode1->writeMetrics.commit_count);
×
1481
  pRawMetrics->commit_time = atomic_load_64(&pVnode1->writeMetrics.commit_time);
×
1482
  pRawMetrics->memtable_wait_time = atomic_load_64(&pVnode1->writeMetrics.memtable_wait_time);
×
UNCOV
1483
  pRawMetrics->blocked_commit_count = atomic_load_64(&pVnode1->writeMetrics.blocked_commit_count);
×
UNCOV
1484
  pRawMetrics->blocked_commit_time = atomic_load_64(&pVnode1->writeMetrics.block_commit_time);
×
1485
  pRawMetrics->merge_count = atomic_load_64(&pVnode1->writeMetrics.merge_count);
×
1486
  pRawMetrics->merge_time = atomic_load_64(&pVnode1->writeMetrics.merge_time);
×
1487
  pRawMetrics->last_cache_commit_time = atomic_load_64(&pVnode1->writeMetrics.last_cache_commit_time);
×
UNCOV
1488
  pRawMetrics->last_cache_commit_count = atomic_load_64(&pVnode1->writeMetrics.last_cache_commit_count);
×
1489

UNCOV
1490
  return 0;
×
1491
}
1492

1493
/*
1494
 * Reset raw write metrics for a vnode by subtracting old values
1495
 */
UNCOV
1496
int32_t vnodeResetRawWriteMetrics(void *pVnode, const SRawWriteMetrics *pOldMetrics) {
×
UNCOV
1497
  if (pVnode == NULL || pOldMetrics == NULL) {
×
UNCOV
1498
    return TSDB_CODE_INVALID_PARA;
×
1499
  }
1500

UNCOV
1501
  SVnode *pVnode1 = (SVnode *)pVnode;
×
1502

1503
  // Reset vnode write metrics using atomic operations to subtract old values
UNCOV
1504
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.total_requests, pOldMetrics->total_requests);
×
UNCOV
1505
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.total_rows, pOldMetrics->total_rows);
×
UNCOV
1506
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.total_bytes, pOldMetrics->total_bytes);
×
1507

UNCOV
1508
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.fetch_batch_meta_time, pOldMetrics->fetch_batch_meta_time);
×
UNCOV
1509
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.fetch_batch_meta_count, pOldMetrics->fetch_batch_meta_count);
×
UNCOV
1510
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.preprocess_time, pOldMetrics->preprocess_time);
×
UNCOV
1511
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.apply_bytes, pOldMetrics->apply_bytes);
×
UNCOV
1512
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.apply_time, pOldMetrics->apply_time);
×
UNCOV
1513
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.commit_count, pOldMetrics->commit_count);
×
1514

UNCOV
1515
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.commit_time, pOldMetrics->commit_time);
×
UNCOV
1516
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.merge_time, pOldMetrics->merge_time);
×
1517

UNCOV
1518
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.memtable_wait_time, pOldMetrics->memtable_wait_time);
×
UNCOV
1519
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.blocked_commit_count, pOldMetrics->blocked_commit_count);
×
UNCOV
1520
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.block_commit_time, pOldMetrics->blocked_commit_time);
×
UNCOV
1521
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.merge_count, pOldMetrics->merge_count);
×
1522

1523
  // Reset new cache metrics
UNCOV
1524
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.last_cache_commit_time, pOldMetrics->last_cache_commit_time);
×
UNCOV
1525
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.last_cache_commit_count, pOldMetrics->last_cache_commit_count);
×
1526

1527
  // Reset sync metrics
UNCOV
1528
  SSyncMetrics syncMetrics = {
×
UNCOV
1529
      .wal_write_bytes = pOldMetrics->wal_write_bytes,
×
UNCOV
1530
      .wal_write_time = pOldMetrics->wal_write_time,
×
1531
  };
UNCOV
1532
  syncResetMetrics(pVnode1->sync, &syncMetrics);
×
1533

UNCOV
1534
  return 0;
×
1535
}
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