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

taosdata / TDengine / #4886

16 Dec 2025 01:13AM UTC coverage: 65.292% (+0.03%) from 65.258%
#4886

push

travis-ci

web-flow
fix: compile error (#33938)

178718 of 273721 relevant lines covered (65.29%)

103311111.65 hits per line

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

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

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

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

36
int32_t fillTableColCmpr(SMetaReader *reader, SSchemaExt *pExt, int32_t numOfCol) {
19,920,332✔
37
  int8_t tblType = reader->me.type;
19,920,332✔
38
  if (withExtSchema(tblType)) {
19,920,085✔
39
    SColCmprWrapper *p = &(reader->me.colCmpr);
19,721,199✔
40
    if (numOfCol != p->nCols) {
19,721,199✔
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++) {
241,902,409✔
45
      SColCmpr *pCmpr = &p->pColCmpr[i];
222,178,038✔
46
      pExt[i].colId = pCmpr->id;
222,177,127✔
47
      pExt[i].compress = pCmpr->alg;
222,173,695✔
48
    }
49
  }
50
  return 0;
19,920,332✔
51
}
52

53
void vnodePrintTableMeta(STableMetaRsp *pMeta) {
19,920,332✔
54
  if (!(qDebugFlag & DEBUG_DEBUG)) {
19,920,332✔
55
    return;
121,450✔
56
  }
57

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

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

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

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

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

130
  if (!reqTbUid) {
32,028,286✔
131
    (void)tsnprintf(tableFName, TSDB_TABLE_FNAME_LEN, "%s.%s", infoReq.dbFName, infoReq.tbName);
31,461,406✔
132
    if (pVnode->mounted) tTrimMountPrefix(tableFName);
31,462,319✔
133
    code = vnodeValidateTableHash(pVnode, tableFName);
31,462,541✔
134
    if (code) {
31,461,865✔
135
      goto _exit4;
×
136
    }
137
  }
138

139
  // query meta
140
  metaReaderDoInit(&mer1, pVnode->pMeta, META_READER_LOCK);
32,028,745✔
141
  if (reqTbUid) {
32,027,613✔
142
    SET_ERRNO(0);
566,880✔
143
    uint64_t tbUid = taosStr2UInt64(infoReq.tbName, NULL, 10);
566,880✔
144
    if (ERRNO == ERANGE || tbUid == 0) {
567,255✔
145
      code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
146
      goto _exit3;
×
147
    }
148
    SMetaReader mr3 = {0};
567,255✔
149
    metaReaderDoInit(&mr3, ((SVnode *)pVnode)->pMeta, META_READER_NOLOCK);
567,255✔
150
    if ((code = metaReaderGetTableEntryByUid(&mr3, tbUid)) < 0) {
566,880✔
151
      metaReaderClear(&mr3);
225,567✔
152
      TAOS_CHECK_GOTO(code, NULL, _exit3);
225,567✔
153
    }
154
    tstrncpy(metaRsp.tbName, mr3.me.name, TSDB_TABLE_NAME_LEN);
341,313✔
155
    metaReaderClear(&mr3);
341,313✔
156
    TAOS_CHECK_GOTO(metaGetTableEntryByName(&mer1, metaRsp.tbName), NULL, _exit3);
341,688✔
157
  } else if (metaGetTableEntryByName(&mer1, infoReq.tbName) < 0) {
31,460,733✔
158
    code = terrno;
11,883,443✔
159
    goto _exit3;
11,883,443✔
160
  }
161

162
  metaRsp.tableType = mer1.me.type;
19,920,085✔
163
  metaRsp.vgId = TD_VID(pVnode);
19,920,085✔
164
  metaRsp.tuid = mer1.me.uid;
19,919,501✔
165

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

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

197
  metaRsp.numOfTags = schemaTag.nCols;
19,919,710✔
198
  metaRsp.numOfColumns = schema.nCols;
19,919,710✔
199
  metaRsp.precision = pVnode->config.tsdbCfg.precision;
19,919,710✔
200
  metaRsp.sversion = schema.version;
19,920,085✔
201
  metaRsp.tversion = schemaTag.version;
19,920,085✔
202
  metaRsp.pSchemas = (SSchema *)taosMemoryMalloc(sizeof(SSchema) * (metaRsp.numOfColumns + metaRsp.numOfTags));
19,920,085✔
203
  metaRsp.pSchemaExt = (SSchemaExt *)taosMemoryCalloc(metaRsp.numOfColumns, sizeof(SSchemaExt));
19,918,744✔
204
  if (NULL == metaRsp.pSchemas || NULL == metaRsp.pSchemaExt) {
19,918,281✔
205
    code = terrno;
×
206
    goto _exit;
×
207
  }
208
  (void)memcpy(metaRsp.pSchemas, schema.pSchema, sizeof(SSchema) * schema.nCols);
19,919,204✔
209
  if (schemaTag.nCols) {
19,919,204✔
210
    (void)memcpy(metaRsp.pSchemas + schema.nCols, schemaTag.pSchema, sizeof(SSchema) * schemaTag.nCols);
16,926,782✔
211
  }
212
  if (metaRsp.pSchemaExt) {
19,919,204✔
213
    SMetaReader *pReader = mer1.me.type == TSDB_CHILD_TABLE ? &mer2 : &mer1;
19,920,085✔
214
    code = fillTableColCmpr(pReader, metaRsp.pSchemaExt, metaRsp.numOfColumns);
19,920,085✔
215
    if (code < 0) {
19,920,332✔
216
      goto _exit;
×
217
    }
218
    for (int32_t i = 0; i < metaRsp.numOfColumns && pReader->me.pExtSchemas; i++) {
56,264,374✔
219
      metaRsp.pSchemaExt[i].typeMod = pReader->me.pExtSchemas[i].typeMod;
36,344,042✔
220
    }
221
  } else {
222
    code = TSDB_CODE_OUT_OF_MEMORY;
×
223
    goto _exit;
×
224
  }
225
  if (hasRefCol(mer1.me.type)) {
19,920,552✔
226
    metaRsp.rversion = mer1.me.colRef.version;
195,065✔
227
    metaRsp.pColRefs = (SColRef*)taosMemoryMalloc(sizeof(SColRef) * metaRsp.numOfColumns);
195,065✔
228
    if (metaRsp.pColRefs) {
197,545✔
229
      code = fillTableColRef(&mer1, metaRsp.pColRefs, metaRsp.numOfColumns);
197,545✔
230
      if (code < 0) {
197,545✔
231
        goto _exit;
×
232
      }
233
    }
234
    metaRsp.numOfColRefs = metaRsp.numOfColumns;
197,545✔
235
  } else {
236
    metaRsp.pColRefs = NULL;
19,722,787✔
237
    metaRsp.numOfColRefs = 0;
19,722,787✔
238
  }
239

240
  vnodePrintTableMeta(&metaRsp);
19,920,332✔
241

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

249
  if (direct) {
19,920,332✔
250
    pRsp = rpcMallocCont(rspLen);
76,697✔
251
  } else {
252
    pRsp = taosMemoryCalloc(1, rspLen);
19,843,635✔
253
  }
254

255
  if (pRsp == NULL) {
19,918,524✔
256
    code = terrno;
×
257
    goto _exit;
×
258
  }
259

260
  rspLen = tSerializeSTableMetaRsp(pRsp, rspLen, &metaRsp);
19,918,524✔
261
  if (rspLen < 0) {
19,920,332✔
262
    code = terrno;
×
263
    goto _exit;
×
264
  }
265

266
_exit:
19,919,451✔
267
  taosMemoryFree(metaRsp.pColRefs);
19,920,332✔
268
  taosMemoryFree(metaRsp.pSchemas);
19,920,332✔
269
  taosMemoryFree(metaRsp.pSchemaExt);
19,919,656✔
270
_exit2:
19,919,876✔
271
  metaReaderClear(&mer2);
19,919,656✔
272
_exit3:
32,029,342✔
273
  metaReaderClear(&mer1);
32,029,342✔
274
_exit4:
32,029,337✔
275
  rpcMsg.info = pMsg->info;
32,030,250✔
276
  rpcMsg.pCont = pRsp;
32,029,796✔
277
  rpcMsg.contLen = rspLen;
32,029,796✔
278
  rpcMsg.code = code;
32,029,796✔
279
  rpcMsg.msgType = pMsg->msgType;
32,029,796✔
280

281
  if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST && autoCreateCtb == 1) {
32,027,157✔
282
    code = TSDB_CODE_SUCCESS;
8,234,565✔
283
  }
284

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

290
  if (direct) {
32,030,250✔
291
    tmsgSendRsp(&rpcMsg);
752,978✔
292
  } else {
293
    *pMsg = rpcMsg;
31,277,272✔
294
  }
295

296
  return code;
32,030,250✔
297
}
298

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

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

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

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

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

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

336
  cfgRsp.tableType = mer1.me.type;
71,456✔
337

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

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

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

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

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

402
  if (withExtSchema(cfgRsp.tableType)) {
71,456✔
403
    for (int32_t i = 0; i < cfgRsp.numOfColumns; i++) {
313,213✔
404
      SColCmpr   *pCmpr = &pColCmpr->pColCmpr[i];
243,949✔
405
      SSchemaExt *pSchExt = cfgRsp.pSchemaExt + i;
243,949✔
406
      pSchExt->colId = pCmpr->id;
243,949✔
407
      pSchExt->compress = pCmpr->alg;
243,949✔
408
      if (pReader->me.pExtSchemas)
243,949✔
409
        pSchExt->typeMod = pReader->me.pExtSchemas[i].typeMod;
25,025✔
410
      else
411
        pSchExt->typeMod = 0;
218,924✔
412
    }
413
  }
414

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

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

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

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

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

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

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

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

470
  tFreeSTableCfgRsp(&cfgRsp);
71,456✔
471
  metaReaderClear(&mer2);
71,456✔
472
  metaReaderClear(&mer1);
71,456✔
473
  return code;
71,456✔
474
}
475

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

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

485
int32_t vnodeGetBatchMeta(SVnode *pVnode, SRpcMsg *pMsg) {
26,791,505✔
486
  int32_t      code = 0;
26,791,505✔
487
  int32_t      rspSize = 0;
26,791,505✔
488
  SBatchReq    batchReq = {0};
26,791,505✔
489
  SBatchMsg   *req = NULL;
26,791,505✔
490
  SBatchRspMsg rsp = {0};
26,791,505✔
491
  SBatchRsp    batchRsp = {0};
26,787,076✔
492
  SRpcMsg      reqMsg = *pMsg;
26,787,076✔
493
  SRpcMsg      rspMsg = {0};
26,787,076✔
494
  void        *pRsp = NULL;
26,788,077✔
495

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

502
  int32_t msgNum = taosArrayGetSize(batchReq.pMsgs);
26,790,665✔
503
  if (msgNum >= MAX_META_MSG_IN_BATCH) {
26,789,674✔
504
    code = TSDB_CODE_INVALID_MSG;
×
505
    qError("too many msgs %d in vnode batch meta req", msgNum);
×
506
    goto _exit;
×
507
  }
508

509
  batchRsp.pRsps = taosArrayInit(msgNum, sizeof(SBatchRspMsg));
26,789,674✔
510
  if (NULL == batchRsp.pRsps) {
26,788,552✔
511
    code = terrno;
×
512
    qError("taosArrayInit %d SBatchRspMsg failed", msgNum);
×
513
    goto _exit;
×
514
  }
515

516
  for (int32_t i = 0; i < msgNum; ++i) {
58,902,173✔
517
    req = taosArrayGet(batchReq.pMsgs, i);
32,112,337✔
518
    if (req == NULL) {
32,110,971✔
519
      code = terrno;
×
520
      goto _exit;
×
521
    }
522

523
    reqMsg.msgType = req->msgType;
32,110,971✔
524
    reqMsg.pCont = req->msg;
32,112,963✔
525
    reqMsg.contLen = req->msgLen;
32,112,347✔
526

527
    switch (req->msgType) {
32,111,128✔
528
      case TDMT_VND_TABLE_META:
30,709,563✔
529
        // error code has been set into reqMsg, no need to handle it here.
530
        if (TSDB_CODE_SUCCESS != vnodeGetTableMeta(pVnode, &reqMsg, false)) {
30,709,563✔
531
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
2,973,505✔
532
        }
533
        break;
30,710,017✔
534
      case TDMT_VND_TABLE_NAME:
567,255✔
535
        // error code has been set into reqMsg, no need to handle it here.
536
        if (TSDB_CODE_SUCCESS != vnodeGetTableMeta(pVnode, &reqMsg, false)) {
567,255✔
537
          qWarn("vnodeGetBatchName failed, msgType:%d", req->msgType);
225,567✔
538
        }
539
        break;
567,255✔
540
      case TDMT_VND_TABLE_CFG:
71,456✔
541
        // error code has been set into reqMsg, no need to handle it here.
542
        if (TSDB_CODE_SUCCESS != vnodeGetTableCfg(pVnode, &reqMsg, false)) {
71,456✔
543
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
×
544
        }
545
        break;
71,456✔
546
      case TDMT_VND_VSUBTABLES_META:
×
547
        // error code has been set into reqMsg, no need to handle it here.
548
        if (TSDB_CODE_SUCCESS != vnodeGetVSubtablesMeta(pVnode, &reqMsg)) {
×
549
          qWarn("vnodeGetVSubtablesMeta failed, msgType:%d", req->msgType);
×
550
        }
551
        break;
×
552
      case TDMT_VND_VSTB_REF_DBS:
764,689✔
553
        // error code has been set into reqMsg, no need to handle it here.
554
        if (TSDB_CODE_SUCCESS != vnodeGetVStbRefDbs(pVnode, &reqMsg)) {
764,689✔
555
          qWarn("vnodeGetVStbRefDbs failed, msgType:%d", req->msgType);
×
556
        }
557
        break;
764,689✔
558
      default:
×
559
        qError("invalid req msgType %d", req->msgType);
×
560
        reqMsg.code = TSDB_CODE_INVALID_MSG;
×
561
        reqMsg.pCont = NULL;
×
562
        reqMsg.contLen = 0;
×
563
        break;
×
564
    }
565

566
    rsp.msgIdx = req->msgIdx;
32,113,417✔
567
    rsp.reqType = reqMsg.msgType;
32,112,801✔
568
    rsp.msgLen = reqMsg.contLen;
32,112,801✔
569
    rsp.rspCode = reqMsg.code;
32,112,801✔
570
    rsp.msg = reqMsg.pCont;
32,112,801✔
571

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

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

597
_exit:
26,790,504✔
598

599
  rspMsg.info = pMsg->info;
26,791,130✔
600
  rspMsg.pCont = pRsp;
26,789,838✔
601
  rspMsg.contLen = rspSize;
26,789,838✔
602
  rspMsg.code = code;
26,789,838✔
603
  rspMsg.msgType = pMsg->msgType;
26,789,838✔
604

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

609
  taosArrayDestroyEx(batchReq.pMsgs, tFreeSBatchReqMsg);
26,785,477✔
610
  taosArrayDestroyEx(batchRsp.pRsps, tFreeSBatchRspMsg);
26,787,144✔
611

612
  tmsgSendRsp(&rspMsg);
26,785,100✔
613

614
  return code;
26,791,505✔
615
}
616

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

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

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

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

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

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

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

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

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

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

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

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

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

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

690
      refColsNum++;
×
691
    }
692

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

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

701
_return:
×
702

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

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

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

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

725
  pList = taosArrayInit(10, sizeof(uint64_t));
764,689✔
726
  QUERY_CHECK_NULL(pList, code, line, _return, terrno);
762,215✔
727

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

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

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

746
    for (int32_t i = 0; i < num; ++i) {
90,947✔
747
      uint64_t* id = taosArrayGet(pList, i);
63,551✔
748
      QUERY_CHECK_NULL(id, code, line, _return, terrno);
63,551✔
749

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

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

756
      for (int32_t j = 0; j < mr.me.colRef.nCols; j++) {
32,267,482✔
757
        if (mr.me.colRef.pColRef[j].hasRef) {
32,203,931✔
758
          if (NULL == tSimpleHashGet(pDbNameHash, mr.me.colRef.pColRef[j].refDbName, strlen(mr.me.colRef.pColRef[j].refDbName))) {
32,069,246✔
759
            char *refDbName = taosStrdup(mr.me.colRef.pColRef[j].refDbName);
21,052✔
760
            QUERY_CHECK_NULL(refDbName, code, line, _return, terrno);
21,052✔
761

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

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

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

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

778
_return:
764,689✔
779

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

784
  taosArrayDestroy(pList);
764,689✔
785
  tSimpleHashCleanup(pDbNameHash);
764,689✔
786

787
  if (code) {
764,689✔
788
    qError("%s failed since %s", __func__, tstrerror(code));
×
789
  }
790
  return code;
764,689✔
791
}
792

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

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

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

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

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

834
_return:
×
835

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

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

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

850
  //tmsgSendRsp(&rspMsg);
851

852
  return code;
×
853
}
854

855
int32_t vnodeGetVStbRefDbs(SVnode *pVnode, SRpcMsg *pMsg) {
764,689✔
856
  int32_t        code = 0;
764,689✔
857
  int32_t        rspSize = 0;
764,689✔
858
  SVStbRefDbsReq req = {0};
764,689✔
859
  SVStbRefDbsRsp rsp = {0};
764,689✔
860
  SRpcMsg        rspMsg = {0};
764,689✔
861
  void          *pRsp = NULL;
764,689✔
862
  int32_t        line = 0;
764,689✔
863

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

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

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

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

897
_return:
764,689✔
898

899
  rspMsg.info = pMsg->info;
764,689✔
900
  rspMsg.pCont = pRsp;
763,447✔
901
  rspMsg.contLen = rspSize;
763,447✔
902
  rspMsg.code = code;
763,447✔
903
  rspMsg.msgType = pMsg->msgType;
763,447✔
904

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

909
  *pMsg = rspMsg;
760,983✔
910

911
  tDestroySVStbRefDbsRsp(&rsp);
764,689✔
912

913
  return code;
764,063✔
914
}
915

916
static int32_t vnodeGetCompStorage(SVnode *pVnode, int64_t *output) {
104,697,471✔
917
  int32_t code = 0;
104,697,471✔
918
#ifdef TD_ENTERPRISE
919
  int32_t now = taosGetTimestampSec();
104,697,471✔
920
  if (llabs(now - pVnode->config.vndStats.storageLastUpd) >= 30) {
104,697,471✔
921
    pVnode->config.vndStats.storageLastUpd = now;
5,977,874✔
922

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

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

948
    if (pVnode->inUse) {
104,697,471✔
949
      *bufferSegmentUsed = pVnode->inUse->size;
104,683,969✔
950
    }
951
    *bufferSegmentSize = pVnode->config.szBuf / VNODE_BUFPOOL_SEGMENTS;
104,697,471✔
952

953
    (void)taosThreadMutexUnlock(&pVnode->mutex);
104,697,471✔
954
  }
955
}
104,697,471✔
956

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

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

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

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

1013
  if (dbname) {
6,293,706✔
1014
    *dbname = pConf->dbname;
6,226,033✔
1015
  }
1016

1017
  if (vgId) {
6,294,340✔
1018
    *vgId = TD_VID(pVnodeObj);
5,508,008✔
1019
  }
1020

1021
  if (numOfTables) {
6,294,340✔
1022
    *numOfTables = pConf->vndStats.numOfNTables + pConf->vndStats.numOfCTables +
110,320✔
1023
                   pConf->vndStats.numOfVTables + pConf->vndStats.numOfVCTables;
55,196✔
1024
  }
1025

1026
  if (numOfNormalTables) {
6,294,340✔
1027
    *numOfNormalTables = pConf->vndStats.numOfNTables +
23,638✔
1028
                         pConf->vndStats.numOfVTables;
11,843✔
1029
  }
1030
}
6,294,340✔
1031

1032
int32_t vnodeGetTableList(void *pVnode, int8_t type, SArray *pList) {
11,843✔
1033
  if (type == TSDB_SUPER_TABLE) {
11,843✔
1034
    return vnodeGetStbIdList(pVnode, 0, pList);
11,843✔
1035
  } else {
1036
    return TSDB_CODE_INVALID_PARA;
×
1037
  }
1038
}
1039

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

1048
  while (1) {
×
1049
    tb_uid_t id = metaCtbCursorNext(pCur);
×
1050
    if (id == 0) {
×
1051
      break;
×
1052
    }
1053

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

1066
int32_t vnodeGetCtbIdListByFilter(SVnode *pVnode, int64_t suid, SArray *list, bool (*filter)(void *arg), void *arg) {
×
1067
  return 0;
×
1068
}
1069

1070
int32_t vnodeGetCtbIdList(void *pVnode, int64_t suid, SArray *list) {
52,630,612✔
1071
  int32_t      code = TSDB_CODE_SUCCESS;
52,630,612✔
1072
  SVnode      *pVnodeObj = pVnode;
52,630,612✔
1073
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnodeObj, suid, 1);
52,630,612✔
1074
  if (NULL == pCur) {
52,622,774✔
1075
    qError("vnode get all table list failed");
×
1076
    return terrno;
×
1077
  }
1078

1079
  while (1) {
255,270,282✔
1080
    tb_uid_t id = metaCtbCursorNext(pCur);
307,893,056✔
1081
    if (id == 0) {
307,893,236✔
1082
      break;
52,633,533✔
1083
    }
1084
    qTrace("vnodeGetCtbIdList: got ctb id %" PRId64 " for suid %" PRId64, id, suid);
255,259,703✔
1085
    if (NULL == taosArrayPush(list, &id)) {
255,272,719✔
1086
      qError("taosArrayPush failed");
×
1087
      code = terrno;
×
1088
      goto _exit;
×
1089
    }
1090
  }
1091

1092
_exit:
52,617,537✔
1093
  metaCloseCtbCursor(pCur);
52,617,537✔
1094
  return code;
52,614,176✔
1095
}
1096

1097
int32_t vnodeGetStbIdList(SVnode *pVnode, int64_t suid, SArray *list) {
71,591,400✔
1098
  int32_t      code = TSDB_CODE_SUCCESS;
71,591,400✔
1099
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, suid);
71,591,400✔
1100
  if (!pCur) {
71,592,750✔
1101
    return TSDB_CODE_OUT_OF_MEMORY;
×
1102
  }
1103

1104
  while (1) {
116,484,639✔
1105
    tb_uid_t id = metaStbCursorNext(pCur);
188,077,389✔
1106
    if (id == 0) {
188,086,644✔
1107
      break;
71,599,466✔
1108
    }
1109

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

1117
_exit:
71,595,967✔
1118
  metaCloseStbCursor(pCur);
71,595,967✔
1119
  return code;
71,588,025✔
1120
}
1121

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

1130
  while (1) {
1,140✔
1131
    tb_uid_t id = metaStbCursorNext(pCur);
1,368✔
1132
    if (id == 0) {
1,368✔
1133
      break;
228✔
1134
    }
1135

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

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

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

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

1158
  *num = 0;
7,043,410✔
1159
  while (1) {
2,454,509✔
1160
    tb_uid_t id = metaCtbCursorNext(pCur);
9,497,179✔
1161
    if (id == 0) {
9,496,381✔
1162
      break;
7,042,670✔
1163
    }
1164

1165
    ++(*num);
2,453,711✔
1166
  }
1167

1168
  metaCloseCtbCursor(pCur);
7,042,670✔
1169
  return TSDB_CODE_SUCCESS;
7,043,410✔
1170
}
1171

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

1181
  return TSDB_CODE_SUCCESS;
7,043,100✔
1182
}
1183

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

1188
  int32_t code = metaReaderGetTableEntryByUid(&mr, suid);
7,042,670✔
1189
  if (code == TSDB_CODE_SUCCESS) {
7,043,410✔
1190
    if (keep) *keep = mr.me.stbEntry.keep;
7,043,410✔
1191
    if (flags) *flags = mr.me.flags;
7,043,410✔
1192
  } else {
1193
    if (keep) *keep = 0;
×
1194
    if (flags) *flags = 0;
×
1195
  }
1196

1197
  metaReaderClear(&mr);
7,043,678✔
1198
  return TSDB_CODE_SUCCESS;
7,043,389✔
1199
}
1200

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

1225
// exclude stbs of taoskeeper log
1226
static int32_t vnodeGetTimeSeriesBlackList(SVnode *pVnode, int32_t *tbSize) {
71,579,412✔
1227
  int32_t      code = TSDB_CODE_SUCCESS;
71,579,412✔
1228
  int32_t      tbNum = 0;
71,579,412✔
1229
  const char **pTbArr = NULL;
71,579,412✔
1230
  const char  *dbName = NULL;
71,579,412✔
1231
  *tbSize = 0;
71,579,412✔
1232

1233
  if (!(dbName = strchr(pVnode->config.dbname, '.'))) return 0;
71,582,112✔
1234
  if (0 == strncmp(++dbName, "log", TSDB_DB_NAME_LEN)) {
71,579,412✔
1235
    tbNum = tkLogStbNum;
2,964✔
1236
    pTbArr = (const char **)&tkLogStb;
2,964✔
1237
  } else if (0 == strncmp(dbName, "audit", TSDB_DB_NAME_LEN)) {
71,579,148✔
1238
    tbNum = tkAuditStbNum;
×
1239
    pTbArr = (const char **)&tkAuditStb;
×
1240
  }
1241
  if (tbNum && pTbArr) {
71,582,112✔
1242
    *tbSize = metaSizeOfTbFilterCache(pVnode->pMeta, 0);
2,964✔
1243
    if (*tbSize < tbNum) {
2,964✔
1244
      for (int32_t i = 0; i < tbNum; ++i) {
44,916✔
1245
        tb_uid_t suid = metaGetTableEntryUidByName(pVnode->pMeta, pTbArr[i]);
43,548✔
1246
        if (suid != 0) {
43,548✔
1247
          code = metaPutTbToFilterCache(pVnode->pMeta, &suid, 0);
2,736✔
1248
          if (TSDB_CODE_SUCCESS != code) {
2,736✔
1249
            return code;
1,596✔
1250
          }
1251
        }
1252
      }
1253
      *tbSize = metaSizeOfTbFilterCache(pVnode->pMeta, 0);
1,368✔
1254
    }
1255
  }
1256

1257
  return code;
71,577,816✔
1258
}
1259
#endif
1260

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

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

1270
int32_t vnodeGetTimeSeriesNum(SVnode *pVnode, int64_t *num) {
71,580,762✔
1271
  SArray *suidList = NULL;
71,580,762✔
1272

1273
  if (!(suidList = taosArrayInit(1, sizeof(tb_uid_t)))) {
71,580,762✔
1274
    return terrno;
×
1275
  }
1276

1277
  int32_t tbFilterSize = 0;
71,572,662✔
1278
  int32_t code = TSDB_CODE_SUCCESS;
71,575,362✔
1279
#ifdef TD_ENTERPRISE
1280
  code = vnodeGetTimeSeriesBlackList(pVnode, &tbFilterSize);
71,575,362✔
1281
  if (TSDB_CODE_SUCCESS != code) {
71,577,291✔
1282
    goto _exit;
1,596✔
1283
  }
1284
#endif
1285

1286
  if ((!tbFilterSize && vnodeGetStbIdList(pVnode, 0, suidList) < 0) ||
71,575,695✔
1287
      (tbFilterSize && vnodeGetStbIdListByFilter(pVnode, 0, suidList, vnodeTimeSeriesFilter, pVnode) < 0)) {
71,576,466✔
1288
    qError("vgId:%d, failed to get stb id list error: %s", TD_VID(pVnode), terrstr());
×
1289
    taosArrayDestroy(suidList);
×
1290
    return terrno;
×
1291
  }
1292

1293
  *num = 0;
71,575,116✔
1294
  int64_t arrSize = taosArrayGetSize(suidList);
71,580,516✔
1295
  for (int64_t i = 0; i < arrSize; ++i) {
188,050,985✔
1296
    tb_uid_t suid = *(tb_uid_t *)taosArrayGet(suidList, i);
116,463,085✔
1297

1298
    int64_t ctbNum = 0;
116,467,135✔
1299
    int32_t numOfCols = 0;
116,471,860✔
1300
    int8_t  flags = 0;
116,463,085✔
1301
    code = metaGetStbStats(pVnode, suid, &ctbNum, &numOfCols, &flags);
116,468,485✔
1302
    if (TSDB_CODE_SUCCESS != code) {
116,477,935✔
1303
      goto _exit;
×
1304
    }
1305
    if (!TABLE_IS_VIRTUAL(flags)) {
116,477,935✔
1306
      *num += ctbNum * (numOfCols - 1);
114,422,755✔
1307
    }
1308
  }
1309

1310
_exit:
71,587,471✔
1311
  taosArrayDestroy(suidList);
71,589,496✔
1312
  return TSDB_CODE_SUCCESS;
71,580,683✔
1313
}
1314

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

1321
  *num = 0;
×
1322
  while (1) {
×
1323
    tb_uid_t id = metaStbCursorNext(pCur);
×
1324
    if (id == 0) {
×
1325
      break;
×
1326
    }
1327

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

1335
    *num += ctbNum;
×
1336
  }
1337

1338
  metaCloseStbCursor(pCur);
×
1339
  return TSDB_CODE_SUCCESS;
×
1340
}
1341

1342
void *vnodeGetIdx(void *pVnode) {
4,333,561✔
1343
  if (pVnode == NULL) {
4,333,561✔
1344
    return NULL;
×
1345
  }
1346

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

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

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

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

1365
  char   *dirName[] = {VNODE_TSDB_DIR, VNODE_WAL_DIR, VNODE_META_DIR, VNODE_TSDB_CACHE_DIR};
5,990,616✔
1366
  int64_t dirSize[4];
5,989,759✔
1367

1368
  vnodeGetPrimaryPath(pVnode, false, path, TSDB_FILENAME_LEN);
5,990,616✔
1369
  int32_t offset = strlen(path);
5,990,616✔
1370

1371
  for (int i = 0; i < sizeof(dirName) / sizeof(dirName[0]); i++) {
29,953,080✔
1372
    int64_t size = {0};
23,962,464✔
1373
    (void)snprintf(path + offset, TSDB_FILENAME_LEN - offset, "%s%s", TD_DIRSEP, dirName[i]);
23,962,464✔
1374
    code = taosGetDirSize(path, &size);
23,962,464✔
1375
    if (code != 0) {
23,962,464✔
1376
      uWarn("vnode %d get dir %s %s size failed since %s", TD_VID(pVnode), path, dirName[i], tstrerror(code));
604✔
1377
    }
1378
    path[offset] = 0;
23,962,464✔
1379
    dirSize[i] = size;
23,962,464✔
1380
  }
1381

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

1398
  code = tsdbGetFsSize(pVnodeObj->pTsdb, pInfo);
5,990,616✔
1399
_exit:
5,990,616✔
1400
  return code;
5,990,616✔
1401
}
1402

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

1411
  SVnode      *pVnode1 = (SVnode *)pVnode;
×
1412
  SSyncMetrics syncMetrics = syncGetMetrics(pVnode1->sync);
×
1413

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

1435
  return 0;
×
1436
}
1437

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

1446
  SVnode *pVnode1 = (SVnode *)pVnode;
×
1447

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

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

1460
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.commit_time, pOldMetrics->commit_time);
×
1461
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.merge_time, pOldMetrics->merge_time);
×
1462

1463
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.memtable_wait_time, pOldMetrics->memtable_wait_time);
×
1464
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.blocked_commit_count, pOldMetrics->blocked_commit_count);
×
1465
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.block_commit_time, pOldMetrics->blocked_commit_time);
×
1466
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.merge_count, pOldMetrics->merge_count);
×
1467

1468
  // Reset new cache metrics
1469
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.last_cache_commit_time, pOldMetrics->last_cache_commit_time);
×
1470
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.last_cache_commit_count, pOldMetrics->last_cache_commit_count);
×
1471

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

1479
  return 0;
×
1480
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc