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

taosdata / TDengine / #4941

27 Jan 2026 10:23AM UTC coverage: 66.868% (+0.04%) from 66.832%
#4941

push

travis-ci

web-flow
fix: asan invalid write issue (#34400)

7 of 8 new or added lines in 2 files covered. (87.5%)

560 existing lines in 126 files now uncovered.

204401 of 305680 relevant lines covered (66.87%)

126915843.15 hits per line

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

65.11
/source/dnode/vnode/src/vnd/vnodeQuery.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#include "tsdb.h"
17
#include "tutil.h"
18
#include "vnd.h"
19

20
#define VNODE_GET_LOAD_RESET_VALS(pVar, oVal, vType, tags)                                                    \
21
  do {                                                                                                        \
22
    int##vType##_t newVal = atomic_sub_fetch_##vType(&(pVar), (oVal));                                        \
23
    if (newVal < 0) {                                                                                         \
24
      vWarn("vgId:%d, %s, abnormal val:%" PRIi64 ", old val:%" PRIi64, TD_VID(pVnode), tags, newVal, (oVal)); \
25
    }                                                                                                         \
26
  } while (0)
27

28
int vnodeQueryOpen(SVnode *pVnode) {
3,706,275✔
29
  return qWorkerInit(NODE_TYPE_VNODE, TD_VID(pVnode), (void **)&pVnode->pQuery, &pVnode->msgCb);
3,706,275✔
30
}
31

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

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

36
int32_t fillTableColCmpr(SMetaReader *reader, SSchemaExt *pExt, int32_t numOfCol) {
19,154,474✔
37
  int8_t tblType = reader->me.type;
19,154,474✔
38
  if (withExtSchema(tblType)) {
19,154,975✔
39
    SColCmprWrapper *p = &(reader->me.colCmpr);
18,937,588✔
40
    if (numOfCol != p->nCols) {
18,938,089✔
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++) {
219,866,427✔
45
      SColCmpr *pCmpr = &p->pColCmpr[i];
200,927,736✔
46
      pExt[i].colId = pCmpr->id;
200,928,232✔
47
      pExt[i].compress = pCmpr->alg;
200,926,042✔
48
    }
49
  }
50
  return 0;
19,154,975✔
51
}
52

53
void vnodePrintTableMeta(STableMetaRsp *pMeta) {
19,154,474✔
54
  if (!(qDebugFlag & DEBUG_DEBUG)) {
19,154,474✔
55
    return;
618,006✔
56
  }
57

58
  qDebug("tbName:%s", pMeta->tbName);
18,536,468✔
59
  qDebug("stbName:%s", pMeta->stbName);
18,537,571✔
60
  qDebug("dbFName:%s", pMeta->dbFName);
18,537,571✔
61
  qDebug("dbId:%" PRId64, pMeta->dbId);
18,537,571✔
62
  qDebug("numOfTags:%d", pMeta->numOfTags);
18,537,571✔
63
  qDebug("numOfColumns:%d", pMeta->numOfColumns);
18,537,571✔
64
  qDebug("precision:%d", pMeta->precision);
18,537,571✔
65
  qDebug("tableType:%d", pMeta->tableType);
18,537,571✔
66
  qDebug("sversion:%d", pMeta->sversion);
18,537,571✔
67
  qDebug("tversion:%d", pMeta->tversion);
18,537,571✔
68
  qDebug("suid:%" PRIu64, pMeta->suid);
18,537,571✔
69
  qDebug("tuid:%" PRIu64, pMeta->tuid);
18,537,571✔
70
  qDebug("vgId:%d", pMeta->vgId);
18,537,571✔
71
  qDebug("sysInfo:%d", pMeta->sysInfo);
18,537,571✔
72
  if (pMeta->pSchemas) {
18,537,571✔
73
    for (int32_t i = 0; i < (pMeta->numOfColumns + pMeta->numOfTags); ++i) {
253,501,613✔
74
      SSchema *pSchema = pMeta->pSchemas + i;
234,964,042✔
75
      qDebug("%d col/tag: type:%d, flags:%d, colId:%d, bytes:%d, name:%s", i, pSchema->type, pSchema->flags,
234,964,042✔
76
             pSchema->colId, pSchema->bytes, pSchema->name);
77
    }
78
  }
79
}
80

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

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

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

125
  if (infoReq.option == REQ_OPT_TBUID) reqTbUid = true;
26,563,547✔
126
  metaRsp.dbId = pVnode->config.dbId;
26,563,547✔
127
  tstrncpy(metaRsp.tbName, infoReq.tbName, TSDB_TABLE_NAME_LEN);
26,563,547✔
128
  (void)memcpy(metaRsp.dbFName, infoReq.dbFName, sizeof(metaRsp.dbFName));
26,563,547✔
129

130
  if (!reqTbUid) {
26,563,547✔
131
    (void)tsnprintf(tableFName, TSDB_TABLE_FNAME_LEN, "%s.%s", infoReq.dbFName, infoReq.tbName);
26,339,545✔
132
    if (pVnode->mounted) tTrimMountPrefix(tableFName);
26,340,997✔
133
    code = vnodeValidateTableHash(pVnode, tableFName);
26,340,046✔
134
    if (code) {
26,339,444✔
135
      goto _exit4;
×
136
    }
137
  }
138

139
  // query meta
140
  metaReaderDoInit(&mer1, pVnode->pMeta, META_READER_LOCK);
26,563,446✔
141
  if (reqTbUid) {
26,564,999✔
142
    SET_ERRNO(0);
224,002✔
143
    uint64_t tbUid = taosStr2UInt64(infoReq.tbName, NULL, 10);
224,002✔
144
    if (ERRNO == ERANGE || tbUid == 0) {
224,002✔
145
      code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
146
      goto _exit3;
×
147
    }
148
    SMetaReader mr3 = {0};
224,002✔
149
    metaReaderDoInit(&mr3, ((SVnode *)pVnode)->pMeta, META_READER_NOLOCK);
224,002✔
150
    if ((code = metaReaderGetTableEntryByUid(&mr3, tbUid)) < 0) {
224,002✔
151
      metaReaderClear(&mr3);
80,222✔
152
      TAOS_CHECK_GOTO(code, NULL, _exit3);
80,222✔
153
    }
154
    tstrncpy(metaRsp.tbName, mr3.me.name, TSDB_TABLE_NAME_LEN);
143,780✔
155
    metaReaderClear(&mr3);
143,780✔
156
    TAOS_CHECK_GOTO(metaGetTableEntryByName(&mer1, metaRsp.tbName), NULL, _exit3);
143,780✔
157
  } else if (metaGetTableEntryByName(&mer1, infoReq.tbName) < 0) {
26,340,997✔
158
    code = terrno;
7,329,200✔
159
    goto _exit3;
7,329,200✔
160
  }
161

162
  metaRsp.tableType = mer1.me.type;
19,154,125✔
163
  metaRsp.vgId = TD_VID(pVnode);
19,154,125✔
164
  metaRsp.tuid = mer1.me.uid;
19,153,624✔
165

166
  switch (mer1.me.type) {
19,153,624✔
167
    case TSDB_SUPER_TABLE: {
3,453,371✔
168
      (void)strcpy(metaRsp.stbName, mer1.me.name);
3,453,371✔
169
      schema = mer1.me.stbEntry.schemaRow;
3,453,371✔
170
      schemaTag = mer1.me.stbEntry.schemaTag;
3,453,371✔
171
      metaRsp.suid = mer1.me.uid;
3,453,371✔
172
      metaRsp.virtualStb = TABLE_IS_VIRTUAL(mer1.me.flags);
3,453,371✔
173
      metaRsp.ownerId = mer1.me.stbEntry.ownerId;
3,453,371✔
174
      break;
3,453,371✔
175
    }
176
    case TSDB_CHILD_TABLE:
12,050,468✔
177
    case TSDB_VIRTUAL_CHILD_TABLE:{
178
      metaReaderDoInit(&mer2, pVnode->pMeta, META_READER_NOLOCK);
12,050,468✔
179
      if (metaReaderGetTableEntryByUid(&mer2, mer1.me.ctbEntry.suid) < 0) goto _exit2;
12,051,318✔
180

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

199
  metaRsp.numOfTags = schemaTag.nCols;
19,154,474✔
200
  metaRsp.numOfColumns = schema.nCols;
19,154,474✔
201
  metaRsp.precision = pVnode->config.tsdbCfg.precision;
19,154,474✔
202
  metaRsp.sversion = schema.version;
19,154,474✔
203
  metaRsp.tversion = schemaTag.version;
19,154,474✔
204
  metaRsp.pSchemas = (SSchema *)taosMemoryMalloc(sizeof(SSchema) * (metaRsp.numOfColumns + metaRsp.numOfTags));
19,154,474✔
205
  metaRsp.pSchemaExt = (SSchemaExt *)taosMemoryCalloc(metaRsp.numOfColumns, sizeof(SSchemaExt));
19,151,019✔
206
  if (NULL == metaRsp.pSchemas || NULL == metaRsp.pSchemaExt) {
19,153,427✔
207
    code = terrno;
1,356✔
208
    goto _exit;
×
209
  }
210
  (void)memcpy(metaRsp.pSchemas, schema.pSchema, sizeof(SSchema) * schema.nCols);
19,152,071✔
211
  if (schemaTag.nCols) {
19,152,071✔
212
    (void)memcpy(metaRsp.pSchemas + schema.nCols, schemaTag.pSchema, sizeof(SSchema) * schemaTag.nCols);
15,504,689✔
213
  }
214
  if (metaRsp.pSchemaExt) {
19,151,373✔
215
    SMetaReader *pReader = mer1.me.type == TSDB_CHILD_TABLE ? &mer2 : &mer1;
19,153,270✔
216
    code = fillTableColCmpr(pReader, metaRsp.pSchemaExt, metaRsp.numOfColumns);
19,153,270✔
217
    if (code < 0) {
19,155,577✔
218
      goto _exit;
×
219
    }
220
    for (int32_t i = 0; i < metaRsp.numOfColumns && pReader->me.pExtSchemas; i++) {
48,342,352✔
221
      metaRsp.pSchemaExt[i].typeMod = pReader->me.pExtSchemas[i].typeMod;
29,186,775✔
222
    }
223
  } else {
UNCOV
224
    code = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
225
    goto _exit;
×
226
  }
227
  if (hasRefCol(mer1.me.type)) {
19,154,975✔
228
    metaRsp.rversion = mer1.me.colRef.version;
218,242✔
229
    metaRsp.pColRefs = (SColRef*)taosMemoryMalloc(sizeof(SColRef) * metaRsp.numOfColumns);
218,242✔
230
    if (metaRsp.pColRefs) {
217,488✔
231
      code = fillTableColRef(&mer1, metaRsp.pColRefs, metaRsp.numOfColumns);
217,488✔
232
      if (code < 0) {
217,488✔
233
        goto _exit;
×
234
      }
235
    }
236
    metaRsp.numOfColRefs = metaRsp.numOfColumns;
217,488✔
237
  } else {
238
    metaRsp.pColRefs = NULL;
18,936,986✔
239
    metaRsp.numOfColRefs = 0;
18,936,986✔
240
  }
241

242
  vnodePrintTableMeta(&metaRsp);
19,154,474✔
243

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

251
  if (direct) {
19,155,228✔
252
    pRsp = rpcMallocCont(rspLen);
167,879✔
253
  } else {
254
    pRsp = taosMemoryCalloc(1, rspLen);
18,987,349✔
255
  }
256

257
  if (pRsp == NULL) {
19,154,626✔
258
    code = terrno;
×
259
    goto _exit;
×
260
  }
261

262
  rspLen = tSerializeSTableMetaRsp(pRsp, rspLen, &metaRsp);
19,154,626✔
263
  if (rspLen < 0) {
19,155,577✔
264
    code = terrno;
×
265
    goto _exit;
×
266
  }
267

268
_exit:
19,153,680✔
269
  taosMemoryFree(metaRsp.pColRefs);
19,155,228✔
270
  taosMemoryFree(metaRsp.pSchemas);
19,153,817✔
271
  taosMemoryFree(metaRsp.pSchemaExt);
19,155,228✔
272
_exit2:
19,154,023✔
273
  metaReaderClear(&mer2);
19,154,372✔
274
_exit3:
26,563,271✔
275
  metaReaderClear(&mer1);
26,564,999✔
276
_exit4:
26,564,999✔
277
  rpcMsg.info = pMsg->info;
26,564,476✔
278
  rpcMsg.pCont = pRsp;
26,564,825✔
279
  rpcMsg.contLen = rspLen;
26,564,825✔
280
  rpcMsg.code = code;
26,564,825✔
281
  rpcMsg.msgType = pMsg->msgType;
26,564,825✔
282

283
  if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST && autoCreateCtb == 1) {
26,563,271✔
284
    code = TSDB_CODE_SUCCESS;
5,024,073✔
285
  }
286

287
  if (code) {
26,563,271✔
288
    qError("vgId:%d, get table %s meta with %" PRIu8 " failed cause of %s", pVnode->config.vgId, infoReq.tbName,
2,385,349✔
289
           infoReq.option, tstrerror(code));
290
  }
291

292
  if (direct) {
26,563,874✔
293
    tmsgSendRsp(&rpcMsg);
838,725✔
294
  } else {
295
    *pMsg = rpcMsg;
25,725,149✔
296
  }
297

298
  return code;
26,563,874✔
299
}
300

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

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

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

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

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

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

338
  cfgRsp.tableType = mer1.me.type;
51,968✔
339

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

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

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

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

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

405
  if (withExtSchema(cfgRsp.tableType)) {
51,968✔
406
    for (int32_t i = 0; i < cfgRsp.numOfColumns; i++) {
193,108✔
407
      SColCmpr   *pCmpr = &pColCmpr->pColCmpr[i];
148,338✔
408
      SSchemaExt *pSchExt = cfgRsp.pSchemaExt + i;
148,338✔
409
      pSchExt->colId = pCmpr->id;
148,338✔
410
      pSchExt->compress = pCmpr->alg;
148,338✔
411
      if (pReader->me.pExtSchemas)
148,338✔
412
        pSchExt->typeMod = pReader->me.pExtSchemas[i].typeMod;
29,975✔
413
      else
414
        pSchExt->typeMod = 0;
118,363✔
415
    }
416
  }
417

418
  cfgRsp.virtualStb = false;
51,968✔
419
  if (hasRefCol(cfgRsp.tableType)) {
51,968✔
420
    for (int32_t i = 0; i < cfgRsp.numOfColumns; i++) {
101,518✔
421
      SColRef *pRef = &pColRef->pColRef[i];
94,320✔
422
      cfgRsp.pColRefs[i].hasRef = pRef->hasRef;
94,320✔
423
      cfgRsp.pColRefs[i].id = pRef->id;
94,320✔
424
      if (cfgRsp.pColRefs[i].hasRef) {
94,320✔
425
        tstrncpy(cfgRsp.pColRefs[i].refDbName, pRef->refDbName, TSDB_DB_NAME_LEN);
49,230✔
426
        tstrncpy(cfgRsp.pColRefs[i].refTableName, pRef->refTableName, TSDB_TABLE_NAME_LEN);
49,230✔
427
        tstrncpy(cfgRsp.pColRefs[i].refColName, pRef->refColName, TSDB_COL_NAME_LEN);
49,230✔
428
      }
429
    }
430
  }
431

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

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

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

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

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

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

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

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

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

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

488
int32_t vnodeGetBatchMeta(SVnode *pVnode, SRpcMsg *pMsg) {
25,174,094✔
489
  int32_t      code = 0;
25,174,094✔
490
  int32_t      rspSize = 0;
25,174,094✔
491
  SBatchReq    batchReq = {0};
25,174,094✔
492
  SBatchMsg   *req = NULL;
25,174,094✔
493
  SBatchRspMsg rsp = {0};
25,174,094✔
494
  SBatchRsp    batchRsp = {0};
25,169,195✔
495
  SRpcMsg      reqMsg = *pMsg;
25,167,705✔
496
  SRpcMsg      rspMsg = {0};
25,168,259✔
497
  void        *pRsp = NULL;
25,171,423✔
498

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

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

512
  batchRsp.pRsps = taosArrayInit(msgNum, sizeof(SBatchRspMsg));
25,168,787✔
513
  if (NULL == batchRsp.pRsps) {
25,170,258✔
514
    code = terrno;
×
515
    qError("taosArrayInit %d SBatchRspMsg failed", msgNum);
×
516
    goto _exit;
×
517
  }
518

519
  for (int32_t i = 0; i < msgNum; ++i) {
55,068,985✔
520
    req = taosArrayGet(batchReq.pMsgs, i);
29,902,727✔
521
    if (req == NULL) {
29,902,133✔
522
      code = terrno;
×
523
      goto _exit;
×
524
    }
525

526
    reqMsg.msgType = req->msgType;
29,902,133✔
527
    reqMsg.pCont = req->msg;
29,902,727✔
528
    reqMsg.contLen = req->msgLen;
29,898,318✔
529

530
    switch (req->msgType) {
29,896,868✔
531
      case TDMT_VND_TABLE_META:
25,501,670✔
532
        // error code has been set into reqMsg, no need to handle it here.
533
        if (TSDB_CODE_SUCCESS != vnodeGetTableMeta(pVnode, &reqMsg, false)) {
25,501,670✔
534
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
1,634,281✔
535
        }
536
        break;
25,501,321✔
537
      case TDMT_VND_TABLE_NAME:
224,002✔
538
        // error code has been set into reqMsg, no need to handle it here.
539
        if (TSDB_CODE_SUCCESS != vnodeGetTableMeta(pVnode, &reqMsg, false)) {
224,002✔
540
          qWarn("vnodeGetBatchName failed, msgType:%d", req->msgType);
80,222✔
541
        }
542
        break;
223,828✔
543
      case TDMT_VND_TABLE_CFG:
51,968✔
544
        // error code has been set into reqMsg, no need to handle it here.
545
        if (TSDB_CODE_SUCCESS != vnodeGetTableCfg(pVnode, &reqMsg, false)) {
51,968✔
546
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
×
547
        }
548
        break;
51,968✔
549
      case TDMT_VND_VSUBTABLES_META:
×
550
        // error code has been set into reqMsg, no need to handle it here.
551
        if (TSDB_CODE_SUCCESS != vnodeGetVSubtablesMeta(pVnode, &reqMsg)) {
×
552
          qWarn("vnodeGetVSubtablesMeta failed, msgType:%d", req->msgType);
×
553
        }
554
        break;
×
555
      case TDMT_VND_VSTB_REF_DBS:
4,125,087✔
556
        // error code has been set into reqMsg, no need to handle it here.
557
        if (TSDB_CODE_SUCCESS != vnodeGetVStbRefDbs(pVnode, &reqMsg)) {
4,125,087✔
558
          qWarn("vnodeGetVStbRefDbs failed, msgType:%d", req->msgType);
×
559
        }
560
        break;
4,120,421✔
561
      default:
×
562
        qError("invalid req msgType %d", req->msgType);
×
563
        reqMsg.code = TSDB_CODE_INVALID_MSG;
×
564
        reqMsg.pCont = NULL;
×
565
        reqMsg.contLen = 0;
×
566
        break;
×
567
    }
568

569
    rsp.msgIdx = req->msgIdx;
29,897,538✔
570
    rsp.reqType = reqMsg.msgType;
29,898,599✔
571
    rsp.msgLen = reqMsg.contLen;
29,898,599✔
572
    rsp.rspCode = reqMsg.code;
29,898,599✔
573
    rsp.msg = reqMsg.pCont;
29,898,599✔
574

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

582
  rspSize = tSerializeSBatchRsp(NULL, 0, &batchRsp);
25,166,258✔
583
  if (rspSize < 0) {
25,157,732✔
584
    qError("tSerializeSBatchRsp failed");
×
585
    code = terrno;
×
586
    goto _exit;
×
587
  }
588
  pRsp = rpcMallocCont(rspSize);
25,157,732✔
589
  if (pRsp == NULL) {
25,157,411✔
590
    qError("rpcMallocCont %d failed", rspSize);
×
591
    code = terrno;
×
592
    goto _exit;
×
593
  }
594
  if (tSerializeSBatchRsp(pRsp, rspSize, &batchRsp) < 0) {
25,157,411✔
595
    qError("tSerializeSBatchRsp %d failed", rspSize);
×
596
    code = terrno;
×
597
    goto _exit;
×
598
  }
599

600
_exit:
25,169,857✔
601

602
  rspMsg.info = pMsg->info;
25,171,810✔
603
  rspMsg.pCont = pRsp;
25,170,896✔
604
  rspMsg.contLen = rspSize;
25,170,896✔
605
  rspMsg.code = code;
25,170,896✔
606
  rspMsg.msgType = pMsg->msgType;
25,170,896✔
607

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

612
  taosArrayDestroyEx(batchReq.pMsgs, tFreeSBatchReqMsg);
25,160,249✔
613
  taosArrayDestroyEx(batchRsp.pRsps, tFreeSBatchRspMsg);
25,165,374✔
614

615
  tmsgSendRsp(&rspMsg);
25,154,466✔
616

617
  return code;
25,172,002✔
618
}
619

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

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

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

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

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

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

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

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

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

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

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

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

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

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

693
      refColsNum++;
×
694
    }
695

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

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

704
_return:
×
705

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

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

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

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

728
  pList = taosArrayInit(10, sizeof(uint64_t));
4,123,597✔
729
  QUERY_CHECK_NULL(pList, code, line, _return, terrno);
4,118,060✔
730

731
  *ppRes = taosArrayInit(10, POINTER_BYTES);
4,118,060✔
732
  QUERY_CHECK_NULL(*ppRes, code, line, _return, terrno)
4,117,975✔
733
  
734
  // lookup in cache
735
  code = pHandle->api.metaFn.metaGetCachedRefDbs(pHandle->vnode, suid, *ppRes);
4,122,305✔
736
  QUERY_CHECK_CODE(code, line, _return);
4,125,207✔
737

738
  if (taosArrayGetSize(*ppRes) > 0) {
4,125,207✔
739
    // found in cache
740
    goto _return;
4,093,636✔
741
  } else {
742
    code = pHandle->api.metaFn.getChildTableList(pHandle->vnode, suid, pList);
31,571✔
743
    QUERY_CHECK_CODE(code, line, _return);
31,571✔
744

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

749
    for (int32_t i = 0; i < num; ++i) {
103,248✔
750
      uint64_t* id = taosArrayGet(pList, i);
71,677✔
751
      QUERY_CHECK_NULL(id, code, line, _return, terrno);
71,677✔
752

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

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

759
      for (int32_t j = 0; j < mr.me.colRef.nCols; j++) {
34,782,080✔
760
        if (mr.me.colRef.pColRef[j].hasRef) {
34,709,903✔
761
          if (NULL == tSimpleHashGet(pDbNameHash, mr.me.colRef.pColRef[j].refDbName, strlen(mr.me.colRef.pColRef[j].refDbName))) {
34,416,735✔
762
            char *refDbName = taosStrdup(mr.me.colRef.pColRef[j].refDbName);
26,749✔
763
            QUERY_CHECK_NULL(refDbName, code, line, _return, terrno);
26,749✔
764

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

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

773
      pHandle->api.metaReaderFn.clearReader(&mr);
72,177✔
774
      readerInit = false;
71,677✔
775
    }
776

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

781
_return:
4,124,707✔
782

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

787
  taosArrayDestroy(pList);
4,125,087✔
788
  tSimpleHashCleanup(pDbNameHash);
4,124,153✔
789

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

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

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

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

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

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

837
_return:
×
838

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

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

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

853
  //tmsgSendRsp(&rspMsg);
854

855
  return code;
×
856
}
857

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

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

873
  SReadHandle handle = {0};
4,124,153✔
874
  handle.vnode = pVnode;
4,125,087✔
875
  initStorageAPI(&handle.api);
4,125,087✔
876

877
  code = vnodeReadVStbRefDbs(&handle, req.suid, &rsp.pDbs);
4,123,977✔
878
  QUERY_CHECK_CODE(code, line, _return);
4,123,827✔
879
  rsp.vgId = TD_VID(pVnode);
4,123,827✔
880

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

900
_return:
4,121,046✔
901

902
  rspMsg.info = pMsg->info;
4,122,536✔
903
  rspMsg.pCont = pRsp;
4,123,393✔
904
  rspMsg.contLen = rspSize;
4,123,393✔
905
  rspMsg.code = code;
4,123,393✔
906
  rspMsg.msgType = pMsg->msgType;
4,123,393✔
907

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

912
  *pMsg = rspMsg;
4,112,924✔
913

914
  tDestroySVStbRefDbsRsp(&rsp);
4,122,482✔
915

916
  return code;
4,123,099✔
917
}
918

919
static int32_t vnodeGetCompStorage(SVnode *pVnode, int64_t *output) {
114,690,894✔
920
  int32_t code = 0;
114,690,894✔
921
#ifdef TD_ENTERPRISE
922
  int32_t now = taosGetTimestampSec();
114,690,894✔
923
  if (llabs(now - pVnode->config.vndStats.storageLastUpd) >= 30) {
114,690,894✔
924
    pVnode->config.vndStats.storageLastUpd = now;
6,069,566✔
925

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

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

951
    if (pVnode->inUse) {
114,690,894✔
952
      *bufferSegmentUsed = pVnode->inUse->size;
114,684,365✔
953
    }
954
    *bufferSegmentSize = pVnode->config.szBuf / VNODE_BUFPOOL_SEGMENTS;
114,690,894✔
955

956
    (void)taosThreadMutexUnlock(&pVnode->mutex);
114,690,894✔
957
  }
958
}
114,690,894✔
959

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

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

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

1016
void vnodeGetInfo(void *pVnode, const char **dbname, int32_t *vgId, int64_t *numOfTables, int64_t *numOfNormalTables) {
13,478,843✔
1017
  SVnode    *pVnodeObj = pVnode;
13,478,843✔
1018
  SVnodeCfg *pConf = &pVnodeObj->config;
13,478,843✔
1019

1020
  if (dbname) {
13,478,843✔
1021
    *dbname = pConf->dbname;
13,462,781✔
1022
  }
1023

1024
  if (vgId) {
13,479,223✔
1025
    *vgId = TD_VID(pVnodeObj);
12,727,136✔
1026
  }
1027

1028
  if (numOfTables) {
13,479,214✔
1029
    *numOfTables = pConf->vndStats.numOfNTables + pConf->vndStats.numOfCTables +
16,964✔
1030
                   pConf->vndStats.numOfVTables + pConf->vndStats.numOfVCTables;
8,482✔
1031
  }
1032

1033
  if (numOfNormalTables) {
13,479,214✔
1034
    *numOfNormalTables = pConf->vndStats.numOfNTables +
15,160✔
1035
                         pConf->vndStats.numOfVTables;
7,580✔
1036
  }
1037
}
13,479,214✔
1038

1039
int32_t vnodeGetTableList(void *pVnode, int8_t type, SArray *pList) {
7,580✔
1040
  if (type == TSDB_SUPER_TABLE) {
7,580✔
1041
    return vnodeGetStbIdList(pVnode, 0, pList);
7,580✔
1042
  } else {
1043
    return TSDB_CODE_INVALID_PARA;
×
1044
  }
1045
}
1046

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

1055
  while (1) {
×
1056
    tb_uid_t id = metaCtbCursorNext(pCur);
×
1057
    if (id == 0) {
×
1058
      break;
×
1059
    }
1060

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

1073
int32_t vnodeGetCtbIdListByFilter(SVnode *pVnode, int64_t suid, SArray *list, bool (*filter)(void *arg), void *arg) {
×
1074
  return 0;
×
1075
}
1076

1077
int32_t vnodeGetCtbIdList(void *pVnode, int64_t suid, SArray *list) {
102,369,210✔
1078
  int32_t      code = TSDB_CODE_SUCCESS;
102,369,210✔
1079
  SVnode      *pVnodeObj = pVnode;
102,369,210✔
1080
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnodeObj, suid, 1);
102,369,210✔
1081
  if (NULL == pCur) {
102,341,894✔
1082
    qError("vnode get all table list failed");
×
1083
    return terrno;
×
1084
  }
1085

1086
  while (1) {
444,005,818✔
1087
    tb_uid_t id = metaCtbCursorNext(pCur);
546,347,712✔
1088
    if (id == 0) {
546,304,468✔
1089
      break;
102,444,093✔
1090
    }
1091
    qTrace("vnodeGetCtbIdList: got ctb id %" PRId64 " for suid %" PRId64, id, suid);
443,860,375✔
1092
    if (NULL == taosArrayPush(list, &id)) {
444,013,069✔
1093
      qError("taosArrayPush failed");
×
1094
      code = terrno;
×
1095
      goto _exit;
×
1096
    }
1097
  }
1098

1099
_exit:
102,347,114✔
1100
  metaCloseCtbCursor(pCur);
102,347,114✔
1101
  return code;
102,314,520✔
1102
}
1103

1104
int32_t vnodeGetStbIdList(SVnode *pVnode, int64_t suid, SArray *list) {
56,068,604✔
1105
  int32_t      code = TSDB_CODE_SUCCESS;
56,068,604✔
1106
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, suid);
56,068,604✔
1107
  if (!pCur) {
56,071,357✔
1108
    return TSDB_CODE_OUT_OF_MEMORY;
×
1109
  }
1110

1111
  while (1) {
44,333,462✔
1112
    tb_uid_t id = metaStbCursorNext(pCur);
100,404,819✔
1113
    if (id == 0) {
100,403,288✔
1114
      break;
56,073,124✔
1115
    }
1116

1117
    if (NULL == taosArrayPush(list, &id)) {
44,334,618✔
1118
      qError("taosArrayPush failed");
×
1119
      code = terrno;
×
1120
      goto _exit;
×
1121
    }
1122
  }
1123

1124
_exit:
56,071,133✔
1125
  metaCloseStbCursor(pCur);
56,071,133✔
1126
  return code;
56,068,611✔
1127
}
1128

1129
int32_t vnodeGetStbIdListByFilter(SVnode *pVnode, int64_t suid, SArray *list, bool (*filter)(void *arg, void *arg1),
286✔
1130
                                  void *arg) {
1131
  int32_t      code = TSDB_CODE_SUCCESS;
286✔
1132
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, suid);
286✔
1133
  if (!pCur) {
286✔
1134
    return terrno;
×
1135
  }
1136

1137
  while (1) {
1,430✔
1138
    tb_uid_t id = metaStbCursorNext(pCur);
1,716✔
1139
    if (id == 0) {
1,716✔
1140
      break;
286✔
1141
    }
1142

1143
    if ((*filter) && (*filter)(arg, &id)) {
1,430✔
1144
      continue;
1,430✔
1145
    }
1146

1147
    if (NULL == taosArrayPush(list, &id)) {
×
1148
      qError("taosArrayPush failed");
×
1149
      code = terrno;
×
1150
      goto _exit;
×
1151
    }
1152
  }
1153

1154
_exit:
286✔
1155
  metaCloseStbCursor(pCur);
286✔
1156
  return code;
286✔
1157
}
1158

1159
int32_t vnodeGetCtbNum(SVnode *pVnode, int64_t suid, int64_t *num) {
4,143,043✔
1160
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, suid, 0);
4,143,043✔
1161
  if (!pCur) {
4,143,680✔
1162
    return terrno;
×
1163
  }
1164

1165
  *num = 0;
4,143,680✔
1166
  while (1) {
2,361,809✔
1167
    tb_uid_t id = metaCtbCursorNext(pCur);
6,505,946✔
1168
    if (id == 0) {
6,505,793✔
1169
      break;
4,144,544✔
1170
    }
1171

1172
    ++(*num);
2,361,249✔
1173
  }
1174

1175
  metaCloseCtbCursor(pCur);
4,144,544✔
1176
  return TSDB_CODE_SUCCESS;
4,144,259✔
1177
}
1178

1179
int32_t vnodeGetStbColumnNum(SVnode *pVnode, tb_uid_t suid, int *num) {
4,143,311✔
1180
  SSchemaWrapper *pSW = metaGetTableSchema(pVnode->pMeta, suid, -1, 0, NULL, 0);
4,143,311✔
1181
  if (pSW) {
4,144,733✔
1182
    *num = pSW->nCols;
4,144,733✔
1183
    tDeleteSchemaWrapper(pSW);
1184
  } else {
UNCOV
1185
    *num = 2;
×
1186
  }
1187

1188
  return TSDB_CODE_SUCCESS;
4,144,448✔
1189
}
1190

1191
int32_t vnodeGetStbInfo(SVnode *pVnode, tb_uid_t suid, int64_t *keep, int8_t *flags) {
4,144,150✔
1192
  SMetaReader mr = {0};
4,144,150✔
1193
  metaReaderDoInit(&mr, pVnode->pMeta, META_READER_NOLOCK);
4,144,150✔
1194

1195
  int32_t code = metaReaderGetTableEntryByUid(&mr, suid);
4,144,150✔
1196
  if (code == TSDB_CODE_SUCCESS) {
4,144,733✔
1197
    if (keep) *keep = mr.me.stbEntry.keep;
4,144,733✔
1198
    if (flags) *flags = mr.me.flags;
4,145,067✔
1199
  } else {
UNCOV
1200
    if (keep) *keep = 0;
×
UNCOV
1201
    if (flags) *flags = 0;
×
1202
  }
1203

1204
  metaReaderClear(&mr);
4,145,067✔
1205
  return TSDB_CODE_SUCCESS;
4,144,733✔
1206
}
1207

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

1232
// exclude stbs of taoskeeper log
1233
static int32_t vnodeGetTimeSeriesBlackList(SVnode *pVnode, int32_t *tbSize) {
56,062,544✔
1234
  int32_t      code = TSDB_CODE_SUCCESS;
56,062,544✔
1235
  int32_t      tbNum = 0;
56,062,544✔
1236
  const char **pTbArr = NULL;
56,062,544✔
1237
  const char  *dbName = NULL;
56,062,544✔
1238
  *tbSize = 0;
56,062,544✔
1239

1240
  if (!(dbName = strchr(pVnode->config.dbname, '.'))) return 0;
56,064,988✔
1241
  if (0 == strncmp(++dbName, "log", TSDB_DB_NAME_LEN)) {
56,063,391✔
1242
    tbNum = tkLogStbNum;
3,432✔
1243
    pTbArr = (const char **)&tkLogStb;
3,432✔
1244
  } else if (0 == strncmp(dbName, "audit", TSDB_DB_NAME_LEN)) {
56,061,490✔
1245
    tbNum = tkAuditStbNum;
1,800✔
1246
    pTbArr = (const char **)&tkAuditStb;
1,800✔
1247
  }
1248
  if (tbNum && pTbArr) {
56,064,922✔
1249
    *tbSize = metaSizeOfTbFilterCache(pVnode->pMeta, 0);
5,232✔
1250
    if (*tbSize < tbNum) {
5,232✔
1251
      for (int32_t i = 0; i < tbNum; ++i) {
54,222✔
1252
        tb_uid_t suid = metaGetTableEntryUidByName(pVnode->pMeta, pTbArr[i]);
50,992✔
1253
        if (suid != 0) {
50,992✔
1254
          code = metaPutTbToFilterCache(pVnode->pMeta, &suid, 0);
3,432✔
1255
          if (TSDB_CODE_SUCCESS != code) {
3,432✔
1256
            return code;
2,002✔
1257
          }
1258
        }
1259
      }
1260
      *tbSize = metaSizeOfTbFilterCache(pVnode->pMeta, 0);
3,230✔
1261
    }
1262
  }
1263

1264
  return code;
56,060,233✔
1265
}
1266
#endif
1267

1268
static bool vnodeTimeSeriesFilter(void *arg1, void *arg2) {
1,430✔
1269
  SVnode *pVnode = (SVnode *)arg1;
1,430✔
1270

1271
  if (metaTbInFilterCache(pVnode->pMeta, arg2, 0)) {
1,430✔
1272
    return true;
1,430✔
1273
  }
1274
  return false;
×
1275
}
1276

1277
int32_t vnodeGetTimeSeriesNum(SVnode *pVnode, int64_t *num) {
56,063,457✔
1278
  SArray *suidList = NULL;
56,063,457✔
1279

1280
  if (!(suidList = taosArrayInit(1, sizeof(tb_uid_t)))) {
56,063,457✔
1281
    return terrno;
×
1282
  }
1283

1284
  int32_t tbFilterSize = 0;
56,064,075✔
1285
  int32_t code = TSDB_CODE_SUCCESS;
56,061,933✔
1286
#ifdef TD_ENTERPRISE
1287
  code = vnodeGetTimeSeriesBlackList(pVnode, &tbFilterSize);
56,061,933✔
1288
  if (TSDB_CODE_SUCCESS != code) {
56,061,013✔
1289
    goto _exit;
2,002✔
1290
  }
1291
#endif
1292

1293
  if ((!tbFilterSize && vnodeGetStbIdList(pVnode, 0, suidList) < 0) ||
56,059,011✔
1294
      (tbFilterSize && vnodeGetStbIdListByFilter(pVnode, 0, suidList, vnodeTimeSeriesFilter, pVnode) < 0)) {
56,062,139✔
1295
    qError("vgId:%d, failed to get stb id list error: %s", TD_VID(pVnode), terrstr());
×
1296
    taosArrayDestroy(suidList);
×
1297
    return terrno;
×
1298
  }
1299

1300
  *num = 0;
56,059,018✔
1301
  int64_t arrSize = taosArrayGetSize(suidList);
56,062,073✔
1302
  for (int64_t i = 0; i < arrSize; ++i) {
100,380,680✔
1303
    tb_uid_t suid = *(tb_uid_t *)taosArrayGet(suidList, i);
44,315,440✔
1304

1305
    int64_t ctbNum = 0;
44,316,360✔
1306
    int32_t numOfCols = 0;
44,320,637✔
1307
    int8_t  flags = 0;
44,316,971✔
1308
    code = metaGetStbStats(pVnode, suid, &ctbNum, &numOfCols, &flags);
44,321,182✔
1309
    if (TSDB_CODE_SUCCESS != code) {
44,322,404✔
1310
      goto _exit;
×
1311
    }
1312
    if (!TABLE_IS_VIRTUAL(flags)) {
44,322,404✔
1313
      *num += ctbNum * (numOfCols - 1);
41,529,527✔
1314
    }
1315
  }
1316

1317
_exit:
56,067,551✔
1318
  taosArrayDestroy(suidList);
56,067,242✔
1319
  return TSDB_CODE_SUCCESS;
56,062,184✔
1320
}
1321

1322
int32_t vnodeGetAllCtbNum(SVnode *pVnode, int64_t *num) {
×
1323
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, 0);
×
1324
  if (!pCur) {
×
1325
    return terrno;
×
1326
  }
1327

1328
  *num = 0;
×
1329
  while (1) {
×
1330
    tb_uid_t id = metaStbCursorNext(pCur);
×
1331
    if (id == 0) {
×
1332
      break;
×
1333
    }
1334

1335
    int64_t ctbNum = 0;
×
1336
    int32_t code = vnodeGetCtbNum(pVnode, id, &ctbNum);
×
1337
    if (TSDB_CODE_SUCCESS != code) {
×
1338
      metaCloseStbCursor(pCur);
×
1339
      return code;
×
1340
    }
1341

1342
    *num += ctbNum;
×
1343
  }
1344

1345
  metaCloseStbCursor(pCur);
×
1346
  return TSDB_CODE_SUCCESS;
×
1347
}
1348

1349
void *vnodeGetIdx(void *pVnode) {
3,758,542✔
1350
  if (pVnode == NULL) {
3,758,542✔
1351
    return NULL;
×
1352
  }
1353

1354
  return metaGetIdx(((SVnode *)pVnode)->pMeta);
3,758,542✔
1355
}
1356

1357
void *vnodeGetIvtIdx(void *pVnode) {
3,758,542✔
1358
  if (pVnode == NULL) {
3,758,542✔
1359
    return NULL;
×
1360
  }
1361
  return metaGetIvtIdx(((SVnode *)pVnode)->pMeta);
3,758,542✔
1362
}
1363

1364
int32_t vnodeGetTableSchema(void *pVnode, int64_t uid, STSchema **pSchema, int64_t *suid, SSchemaWrapper **pTagSchema) {
234,993✔
1365
  return tsdbGetTableSchema(((SVnode *)pVnode)->pMeta, uid, pSchema, suid, pTagSchema);
234,993✔
1366
}
1367

1368
static FORCE_INLINE int32_t vnodeGetDBPrimaryInfo(SVnode *pVnode, SDbSizeStatisInfo *pInfo) {
1369
  int32_t code = 0;
6,074,960✔
1370
  char    path[TSDB_FILENAME_LEN] = {0};
6,074,960✔
1371

1372
  char   *dirName[] = {VNODE_TSDB_DIR, VNODE_WAL_DIR, VNODE_META_DIR, VNODE_TSDB_CACHE_DIR};
6,074,960✔
1373
  int64_t dirSize[4];
6,046,944✔
1374

1375
  vnodeGetPrimaryPath(pVnode, false, path, TSDB_FILENAME_LEN);
6,074,960✔
1376
  int32_t offset = strlen(path);
6,074,960✔
1377

1378
  for (int i = 0; i < sizeof(dirName) / sizeof(dirName[0]); i++) {
30,374,800✔
1379
    int64_t size = {0};
24,299,840✔
1380
    (void)snprintf(path + offset, TSDB_FILENAME_LEN - offset, "%s%s", TD_DIRSEP, dirName[i]);
24,299,840✔
1381
    code = taosGetDirSize(path, &size);
24,299,840✔
1382
    if (code != 0) {
24,299,840✔
1383
      uWarn("vnode %d get dir %s %s size failed since %s", TD_VID(pVnode), path, dirName[i], tstrerror(code));
2,092✔
1384
    }
1385
    path[offset] = 0;
24,299,840✔
1386
    dirSize[i] = size;
24,299,840✔
1387
  }
1388

1389
  pInfo->l1Size = 0;
6,074,960✔
1390
  pInfo->walSize = dirSize[1];
6,074,960✔
1391
  pInfo->metaSize = dirSize[2];
6,074,960✔
1392
  pInfo->cacheSize = dirSize[3];
6,074,960✔
1393
  return code;
6,074,960✔
1394
}
1395
int32_t vnodeGetDBSize(void *pVnode, SDbSizeStatisInfo *pInfo) {
6,074,960✔
1396
  int32_t code = 0;
6,074,960✔
1397
  int32_t lino = 0;
6,074,960✔
1398
  SVnode *pVnodeObj = pVnode;
6,074,960✔
1399
  if (pVnodeObj == NULL) {
6,074,960✔
1400
    return TSDB_CODE_VND_NOT_EXIST;
×
1401
  }
1402
  code = vnodeGetDBPrimaryInfo(pVnode, pInfo);
6,074,960✔
1403
  if (code != 0) goto _exit;
6,074,960✔
1404

1405
  code = tsdbGetFsSize(pVnodeObj->pTsdb, pInfo);
6,074,960✔
1406
_exit:
6,074,960✔
1407
  return code;
6,074,960✔
1408
}
1409

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

1418
  SVnode      *pVnode1 = (SVnode *)pVnode;
×
1419
  SSyncMetrics syncMetrics = syncGetMetrics(pVnode1->sync);
×
1420

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

1442
  return 0;
×
1443
}
1444

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

1453
  SVnode *pVnode1 = (SVnode *)pVnode;
×
1454

1455
  // Reset vnode write metrics using atomic operations to subtract old values
1456
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.total_requests, pOldMetrics->total_requests);
×
1457
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.total_rows, pOldMetrics->total_rows);
×
1458
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.total_bytes, pOldMetrics->total_bytes);
×
1459

1460
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.fetch_batch_meta_time, pOldMetrics->fetch_batch_meta_time);
×
1461
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.fetch_batch_meta_count, pOldMetrics->fetch_batch_meta_count);
×
1462
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.preprocess_time, pOldMetrics->preprocess_time);
×
1463
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.apply_bytes, pOldMetrics->apply_bytes);
×
1464
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.apply_time, pOldMetrics->apply_time);
×
1465
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.commit_count, pOldMetrics->commit_count);
×
1466

1467
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.commit_time, pOldMetrics->commit_time);
×
1468
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.merge_time, pOldMetrics->merge_time);
×
1469

1470
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.memtable_wait_time, pOldMetrics->memtable_wait_time);
×
1471
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.blocked_commit_count, pOldMetrics->blocked_commit_count);
×
1472
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.block_commit_time, pOldMetrics->blocked_commit_time);
×
1473
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.merge_count, pOldMetrics->merge_count);
×
1474

1475
  // Reset new cache metrics
1476
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.last_cache_commit_time, pOldMetrics->last_cache_commit_time);
×
1477
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.last_cache_commit_count, pOldMetrics->last_cache_commit_count);
×
1478

1479
  // Reset sync metrics
1480
  SSyncMetrics syncMetrics = {
×
1481
      .wal_write_bytes = pOldMetrics->wal_write_bytes,
×
1482
      .wal_write_time = pOldMetrics->wal_write_time,
×
1483
  };
1484
  syncResetMetrics(pVnode1->sync, &syncMetrics);
×
1485

1486
  return 0;
×
1487
}
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