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

taosdata / TDengine / #4871

04 Dec 2025 01:55AM UTC coverage: 64.654% (+0.1%) from 64.545%
#4871

push

travis-ci

guanshengliang
Merge branch '3.0' into cover/3.0

869 of 2219 new or added lines in 36 files covered. (39.16%)

441 existing lines in 120 files now uncovered.

159620 of 246882 relevant lines covered (64.65%)

110922946.31 hits per line

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

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

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

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

36
int32_t fillTableColCmpr(SMetaReader *reader, SSchemaExt *pExt, int32_t numOfCol) {
20,355,800✔
37
  int8_t tblType = reader->me.type;
20,355,800✔
38
  if (withExtSchema(tblType)) {
20,356,034✔
39
    SColCmprWrapper *p = &(reader->me.colCmpr);
20,162,425✔
40
    if (numOfCol != p->nCols) {
20,162,425✔
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++) {
246,787,231✔
45
      SColCmpr *pCmpr = &p->pColCmpr[i];
226,625,231✔
46
      pExt[i].colId = pCmpr->id;
226,623,402✔
47
      pExt[i].compress = pCmpr->alg;
226,625,465✔
48
    }
49
  }
50
  return 0;
20,355,375✔
51
}
52

53
void vnodePrintTableMeta(STableMetaRsp *pMeta) {
20,355,800✔
54
  if (!(qDebugFlag & DEBUG_DEBUG)) {
20,355,800✔
55
    return;
122,332✔
56
  }
57

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

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

103
int32_t vnodeGetTableMeta(SVnode *pVnode, SRpcMsg *pMsg, bool direct) {
31,863,184✔
104
  STableInfoReq  infoReq = {0};
31,863,184✔
105
  STableMetaRsp  metaRsp = {0};
31,863,184✔
106
  SMetaReader    mer1 = {0};
31,861,205✔
107
  SMetaReader    mer2 = {0};
31,862,525✔
108
  char           tableFName[TSDB_TABLE_FNAME_LEN];
31,831,711✔
109
  bool           reqTbUid = false;
31,861,864✔
110
  SRpcMsg        rpcMsg = {0};
31,861,864✔
111
  int32_t        code = 0;
31,862,260✔
112
  int32_t        rspLen = 0;
31,862,260✔
113
  void          *pRsp = NULL;
31,862,260✔
114
  SSchemaWrapper schema = {0};
31,862,260✔
115
  SSchemaWrapper schemaTag = {0};
31,862,260✔
116
  uint8_t        autoCreateCtb = 0;
31,862,260✔
117

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

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

130
  if (!reqTbUid) {
31,863,184✔
131
    (void)tsnprintf(tableFName, TSDB_TABLE_FNAME_LEN, "%s.%s", infoReq.dbFName, infoReq.tbName);
31,315,400✔
132
    if (pVnode->mounted) tTrimMountPrefix(tableFName);
31,316,718✔
133
    code = vnodeValidateTableHash(pVnode, tableFName);
31,316,718✔
134
    if (code) {
31,315,002✔
135
      goto _exit4;
×
136
    }
137
  }
138

139
  // query meta
140
  metaReaderDoInit(&mer1, pVnode->pMeta, META_READER_LOCK);
31,862,786✔
141
  if (reqTbUid) {
31,861,864✔
142
    SET_ERRNO(0);
546,203✔
143
    uint64_t tbUid = taosStr2UInt64(infoReq.tbName, NULL, 10);
546,203✔
144
    if (ERRNO == ERANGE || tbUid == 0) {
546,203✔
145
      code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
146
      goto _exit3;
×
147
    }
148
    SMetaReader mr3 = {0};
546,203✔
149
    metaReaderDoInit(&mr3, ((SVnode *)pVnode)->pMeta, META_READER_NOLOCK);
546,203✔
150
    if ((code = metaReaderGetTableEntryByUid(&mr3, tbUid)) < 0) {
545,839✔
151
      metaReaderClear(&mr3);
211,519✔
152
      TAOS_CHECK_GOTO(code, NULL, _exit3);
211,519✔
153
    }
154
    tstrncpy(metaRsp.tbName, mr3.me.name, TSDB_TABLE_NAME_LEN);
334,684✔
155
    metaReaderClear(&mr3);
334,684✔
156
    TAOS_CHECK_GOTO(metaGetTableEntryByName(&mer1, metaRsp.tbName), NULL, _exit3);
334,684✔
157
  } else if (metaGetTableEntryByName(&mer1, infoReq.tbName) < 0) {
31,315,661✔
158
    code = terrno;
11,296,027✔
159
    goto _exit3;
11,296,027✔
160
  }
161

162
  metaRsp.tableType = mer1.me.type;
20,355,800✔
163
  metaRsp.vgId = TD_VID(pVnode);
20,355,800✔
164
  metaRsp.tuid = mer1.me.uid;
20,355,566✔
165

166
  switch (mer1.me.type) {
20,355,566✔
167
    case TSDB_SUPER_TABLE: {
3,486,123✔
168
      (void)strcpy(metaRsp.stbName, mer1.me.name);
3,486,123✔
169
      schema = mer1.me.stbEntry.schemaRow;
3,486,123✔
170
      schemaTag = mer1.me.stbEntry.schemaTag;
3,486,123✔
171
      metaRsp.suid = mer1.me.uid;
3,486,123✔
172
      break;
3,486,123✔
173
    }
174
    case TSDB_CHILD_TABLE:
13,104,706✔
175
    case TSDB_VIRTUAL_CHILD_TABLE:{
176
      metaReaderDoInit(&mer2, pVnode->pMeta, META_READER_NOLOCK);
13,104,706✔
177
      if (metaReaderGetTableEntryByUid(&mer2, mer1.me.ctbEntry.suid) < 0) goto _exit2;
13,104,940✔
178

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

196
  metaRsp.numOfTags = schemaTag.nCols;
20,355,566✔
197
  metaRsp.numOfColumns = schema.nCols;
20,355,566✔
198
  metaRsp.precision = pVnode->config.tsdbCfg.precision;
20,355,566✔
199
  metaRsp.sversion = schema.version;
20,354,057✔
200
  metaRsp.tversion = schemaTag.version;
20,354,057✔
201
  metaRsp.pSchemas = (SSchema *)taosMemoryMalloc(sizeof(SSchema) * (metaRsp.numOfColumns + metaRsp.numOfTags));
20,354,057✔
202
  metaRsp.pSchemaExt = (SSchemaExt *)taosMemoryCalloc(metaRsp.numOfColumns, sizeof(SSchemaExt));
20,355,566✔
203
  if (NULL == metaRsp.pSchemas || NULL == metaRsp.pSchemaExt) {
20,354,248✔
204
    code = terrno;
×
205
    goto _exit;
×
206
  }
207
  (void)memcpy(metaRsp.pSchemas, schema.pSchema, sizeof(SSchema) * schema.nCols);
20,355,141✔
208
  if (schemaTag.nCols) {
20,355,141✔
209
    (void)memcpy(metaRsp.pSchemas + schema.nCols, schemaTag.pSchema, sizeof(SSchema) * schemaTag.nCols);
16,590,831✔
210
  }
211
  if (metaRsp.pSchemaExt) {
20,355,373✔
212
    SMetaReader *pReader = mer1.me.type == TSDB_CHILD_TABLE ? &mer2 : &mer1;
20,355,800✔
213
    code = fillTableColCmpr(pReader, metaRsp.pSchemaExt, metaRsp.numOfColumns);
20,355,800✔
214
    if (code < 0) {
20,355,375✔
215
      goto _exit;
×
216
    }
217
    for (int32_t i = 0; i < metaRsp.numOfColumns && pReader->me.pExtSchemas; i++) {
66,847,125✔
218
      metaRsp.pSchemaExt[i].typeMod = pReader->me.pExtSchemas[i].typeMod;
46,491,750✔
219
    }
220
  } else {
221
    code = TSDB_CODE_OUT_OF_MEMORY;
×
222
    goto _exit;
×
223
  }
224
  if (hasRefCol(mer1.me.type)) {
20,354,057✔
225
    metaRsp.rversion = mer1.me.colRef.version;
194,268✔
226
    metaRsp.pColRefs = (SColRef*)taosMemoryMalloc(sizeof(SColRef) * metaRsp.numOfColumns);
194,268✔
227
    if (metaRsp.pColRefs) {
193,609✔
228
      code = fillTableColRef(&mer1, metaRsp.pColRefs, metaRsp.numOfColumns);
193,609✔
229
      if (code < 0) {
193,609✔
230
        goto _exit;
×
231
      }
232
    }
233
    metaRsp.numOfColRefs = metaRsp.numOfColumns;
193,609✔
234
  } else {
235
    metaRsp.pColRefs = NULL;
20,161,532✔
236
    metaRsp.numOfColRefs = 0;
20,161,532✔
237
  }
238

239
  vnodePrintTableMeta(&metaRsp);
20,355,141✔
240

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

248
  if (direct) {
20,355,584✔
249
    pRsp = rpcMallocCont(rspLen);
75,538✔
250
  } else {
251
    pRsp = taosMemoryCalloc(1, rspLen);
20,280,046✔
252
  }
253

254
  if (pRsp == NULL) {
20,356,034✔
255
    code = terrno;
×
256
    goto _exit;
×
257
  }
258

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

265
_exit:
20,355,607✔
266
  taosMemoryFree(metaRsp.pColRefs);
20,355,585✔
267
  taosMemoryFree(metaRsp.pSchemas);
20,355,585✔
268
  taosMemoryFree(metaRsp.pSchemaExt);
20,356,034✔
269
_exit2:
20,356,034✔
270
  metaReaderClear(&mer2);
20,356,034✔
271
_exit3:
31,863,580✔
272
  metaReaderClear(&mer1);
31,863,580✔
273
_exit4:
31,863,132✔
274
  rpcMsg.info = pMsg->info;
31,863,580✔
275
  rpcMsg.pCont = pRsp;
31,863,580✔
276
  rpcMsg.contLen = rspLen;
31,863,580✔
277
  rpcMsg.code = code;
31,863,580✔
278
  rpcMsg.msgType = pMsg->msgType;
31,863,580✔
279

280
  if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST && autoCreateCtb == 1) {
31,863,132✔
281
    code = TSDB_CODE_SUCCESS;
8,299,129✔
282
  }
283

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

289
  if (direct) {
31,863,580✔
290
    tmsgSendRsp(&rpcMsg);
735,881✔
291
  } else {
292
    *pMsg = rpcMsg;
31,127,699✔
293
  }
294

295
  return code;
31,863,580✔
296
}
297

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

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

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

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

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

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

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

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

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

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

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

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

401
  if (withExtSchema(cfgRsp.tableType)) {
68,899✔
402
    for (int32_t i = 0; i < cfgRsp.numOfColumns; i++) {
304,236✔
403
      SColCmpr   *pCmpr = &pColCmpr->pColCmpr[i];
237,465✔
404
      SSchemaExt *pSchExt = cfgRsp.pSchemaExt + i;
237,465✔
405
      pSchExt->colId = pCmpr->id;
237,465✔
406
      pSchExt->compress = pCmpr->alg;
237,465✔
407
      if (pReader->me.pExtSchemas)
237,465✔
408
        pSchExt->typeMod = pReader->me.pExtSchemas[i].typeMod;
32,703✔
409
      else
410
        pSchExt->typeMod = 0;
204,762✔
411
    }
412
  }
413

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

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

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

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

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

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

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

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

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

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

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

484
int32_t vnodeGetBatchMeta(SVnode *pVnode, SRpcMsg *pMsg) {
26,811,654✔
485
  int32_t      code = 0;
26,811,654✔
486
  int32_t      rspSize = 0;
26,811,654✔
487
  SBatchReq    batchReq = {0};
26,811,654✔
488
  SBatchMsg   *req = NULL;
26,811,654✔
489
  SBatchRspMsg rsp = {0};
26,811,654✔
490
  SBatchRsp    batchRsp = {0};
26,809,279✔
491
  SRpcMsg      reqMsg = *pMsg;
26,809,279✔
492
  SRpcMsg      rspMsg = {0};
26,809,279✔
493
  void        *pRsp = NULL;
26,809,940✔
494

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

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

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

515
  for (int32_t i = 0; i < msgNum; ++i) {
58,971,608✔
516
    req = taosArrayGet(batchReq.pMsgs, i);
32,159,968✔
517
    if (req == NULL) {
32,159,297✔
518
      code = terrno;
×
519
      goto _exit;
×
520
    }
521

522
    reqMsg.msgType = req->msgType;
32,159,297✔
523
    reqMsg.pCont = req->msg;
32,159,297✔
524
    reqMsg.contLen = req->msgLen;
32,159,968✔
525

526
    switch (req->msgType) {
32,158,700✔
527
      case TDMT_VND_TABLE_META:
30,580,837✔
528
        // error code has been set into reqMsg, no need to handle it here.
529
        if (TSDB_CODE_SUCCESS != vnodeGetTableMeta(pVnode, &reqMsg, false)) {
30,580,837✔
530
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
2,336,105✔
531
        }
532
        break;
30,581,496✔
533
      case TDMT_VND_TABLE_NAME:
546,203✔
534
        // error code has been set into reqMsg, no need to handle it here.
535
        if (TSDB_CODE_SUCCESS != vnodeGetTableMeta(pVnode, &reqMsg, false)) {
546,203✔
536
          qWarn("vnodeGetBatchName failed, msgType:%d", req->msgType);
211,519✔
537
        }
538
        break;
546,203✔
539
      case TDMT_VND_TABLE_CFG:
68,899✔
540
        // error code has been set into reqMsg, no need to handle it here.
541
        if (TSDB_CODE_SUCCESS != vnodeGetTableCfg(pVnode, &reqMsg, false)) {
68,899✔
542
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
×
543
        }
544
        break;
68,899✔
545
      case TDMT_VND_VSUBTABLES_META:
×
546
        // error code has been set into reqMsg, no need to handle it here.
547
        if (TSDB_CODE_SUCCESS != vnodeGetVSubtablesMeta(pVnode, &reqMsg)) {
×
548
          qWarn("vnodeGetVSubtablesMeta failed, msgType:%d", req->msgType);
×
549
        }
550
        break;
×
551
      case TDMT_VND_VSTB_REF_DBS:
963,370✔
552
        // error code has been set into reqMsg, no need to handle it here.
553
        if (TSDB_CODE_SUCCESS != vnodeGetVStbRefDbs(pVnode, &reqMsg)) {
963,370✔
554
          qWarn("vnodeGetVStbRefDbs failed, msgType:%d", req->msgType);
×
555
        }
556
        break;
963,969✔
557
      default:
×
558
        qError("invalid req msgType %d", req->msgType);
×
559
        reqMsg.code = TSDB_CODE_INVALID_MSG;
×
560
        reqMsg.pCont = NULL;
×
561
        reqMsg.contLen = 0;
×
562
        break;
×
563
    }
564

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

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

578
  rspSize = tSerializeSBatchRsp(NULL, 0, &batchRsp);
26,811,640✔
579
  if (rspSize < 0) {
26,810,082✔
580
    qError("tSerializeSBatchRsp failed");
×
581
    code = terrno;
×
582
    goto _exit;
×
583
  }
584
  pRsp = rpcMallocCont(rspSize);
26,810,082✔
585
  if (pRsp == NULL) {
26,808,860✔
586
    qError("rpcMallocCont %d failed", rspSize);
×
587
    code = terrno;
×
588
    goto _exit;
×
589
  }
590
  if (tSerializeSBatchRsp(pRsp, rspSize, &batchRsp) < 0) {
26,808,860✔
591
    qError("tSerializeSBatchRsp %d failed", rspSize);
×
592
    code = terrno;
×
593
    goto _exit;
×
594
  }
595

596
_exit:
26,811,654✔
597

598
  rspMsg.info = pMsg->info;
26,811,644✔
599
  rspMsg.pCont = pRsp;
26,812,253✔
600
  rspMsg.contLen = rspSize;
26,812,253✔
601
  rspMsg.code = code;
26,812,253✔
602
  rspMsg.msgType = pMsg->msgType;
26,812,253✔
603

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

608
  taosArrayDestroyEx(batchReq.pMsgs, tFreeSBatchReqMsg);
26,808,401✔
609
  taosArrayDestroyEx(batchRsp.pRsps, tFreeSBatchRspMsg);
26,809,009✔
610

611
  tmsgSendRsp(&rspMsg);
26,806,594✔
612

613
  return code;
26,811,654✔
614
}
615

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

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

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

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

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

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

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

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

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

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

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

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

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

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

689
      refColsNum++;
×
690
    }
691

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

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

700
_return:
×
701

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

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

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

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

724
  pList = taosArrayInit(10, sizeof(uint64_t));
963,969✔
725
  QUERY_CHECK_NULL(pList, code, line, _return, terrno);
963,360✔
726

727
  *ppRes = taosArrayInit(10, POINTER_BYTES);
963,360✔
728
  QUERY_CHECK_NULL(*ppRes, code, line, _return, terrno)
962,152✔
729
  
730
  // lookup in cache
731
  code = pHandle->api.metaFn.metaGetCachedRefDbs(pHandle->vnode, suid, *ppRes);
962,761✔
732
  QUERY_CHECK_CODE(code, line, _return);
963,969✔
733

734
  if (taosArrayGetSize(*ppRes) > 0) {
963,969✔
735
    // found in cache
736
    goto _return;
938,161✔
737
  } else {
738
    code = pHandle->api.metaFn.getChildTableList(pHandle->vnode, suid, pList);
25,808✔
739
    QUERY_CHECK_CODE(code, line, _return);
25,808✔
740

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

745
    for (int32_t i = 0; i < num; ++i) {
87,190✔
746
      uint64_t* id = taosArrayGet(pList, i);
61,382✔
747
      QUERY_CHECK_NULL(id, code, line, _return, terrno);
61,382✔
748

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

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

755
      for (int32_t j = 0; j < mr.me.colRef.nCols; j++) {
494,217✔
756
        if (mr.me.colRef.pColRef[j].hasRef) {
432,835✔
757
          if (NULL == tSimpleHashGet(pDbNameHash, mr.me.colRef.pColRef[j].refDbName, strlen(mr.me.colRef.pColRef[j].refDbName))) {
289,300✔
758
            char *refDbName = taosStrdup(mr.me.colRef.pColRef[j].refDbName);
20,093✔
759
            QUERY_CHECK_NULL(refDbName, code, line, _return, terrno);
20,093✔
760

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

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

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

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

777
_return:
964,735✔
778

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

783
  taosArrayDestroy(pList);
963,969✔
784
  tSimpleHashCleanup(pDbNameHash);
963,360✔
785

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

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

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

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

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

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

833
_return:
×
834

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

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

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

849
  //tmsgSendRsp(&rspMsg);
850

851
  return code;
×
852
}
853

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

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

869
  SReadHandle handle = {0};
962,761✔
870
  handle.vnode = pVnode;
963,370✔
871
  initStorageAPI(&handle.api);
963,370✔
872

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

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

896
_return:
963,969✔
897

898
  rspMsg.info = pMsg->info;
963,969✔
899
  rspMsg.pCont = pRsp;
963,969✔
900
  rspMsg.contLen = rspSize;
963,969✔
901
  rspMsg.code = code;
963,969✔
902
  rspMsg.msgType = pMsg->msgType;
963,969✔
903

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

908
  *pMsg = rspMsg;
963,969✔
909

910
  tDestroySVStbRefDbsRsp(&rsp);
963,360✔
911

912
  return code;
963,969✔
913
}
914

915
static int32_t vnodeGetCompStorage(SVnode *pVnode, int64_t *output) {
107,967,198✔
916
  int32_t code = 0;
107,967,198✔
917
#ifdef TD_ENTERPRISE
918
  int32_t now = taosGetTimestampSec();
107,967,198✔
919
  if (llabs(now - pVnode->config.vndStats.storageLastUpd) >= 30) {
107,967,198✔
920
    pVnode->config.vndStats.storageLastUpd = now;
6,097,545✔
921

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

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

947
    if (pVnode->inUse) {
107,967,198✔
948
      *bufferSegmentUsed = pVnode->inUse->size;
107,952,562✔
949
    }
950
    *bufferSegmentSize = pVnode->config.szBuf / VNODE_BUFPOOL_SEGMENTS;
107,967,198✔
951

952
    (void)taosThreadMutexUnlock(&pVnode->mutex);
107,967,198✔
953
  }
954
}
107,967,198✔
955

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

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

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

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

1012
  if (dbname) {
6,470,140✔
1013
    *dbname = pConf->dbname;
6,407,021✔
1014
  }
1015

1016
  if (vgId) {
6,469,938✔
1017
    *vgId = TD_VID(pVnodeObj);
5,710,254✔
1018
  }
1019

1020
  if (numOfTables) {
6,469,250✔
1021
    *numOfTables = pConf->vndStats.numOfNTables + pConf->vndStats.numOfCTables +
103,620✔
1022
                   pConf->vndStats.numOfVTables + pConf->vndStats.numOfVCTables;
52,410✔
1023
  }
1024

1025
  if (numOfNormalTables) {
6,469,250✔
1026
    *numOfNormalTables = pConf->vndStats.numOfNTables +
21,492✔
1027
                         pConf->vndStats.numOfVTables;
11,146✔
1028
  }
1029
}
6,469,250✔
1030

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

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

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

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

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

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

1078
  while (1) {
248,761,317✔
1079
    tb_uid_t id = metaCtbCursorNext(pCur);
301,816,091✔
1080
    if (id == 0) {
301,826,624✔
1081
      break;
53,062,085✔
1082
    }
1083
    qTrace("vnodeGetCtbIdList: got ctb id %" PRId64 " for suid %" PRId64, id, suid);
248,764,539✔
1084
    if (NULL == taosArrayPush(list, &id)) {
248,768,521✔
1085
      qError("taosArrayPush failed");
×
1086
      code = terrno;
×
1087
      goto _exit;
×
1088
    }
1089
  }
1090

1091
_exit:
53,056,469✔
1092
  metaCloseCtbCursor(pCur);
53,056,469✔
1093
  return code;
53,044,977✔
1094
}
1095

1096
int32_t vnodeGetStbIdList(SVnode *pVnode, int64_t suid, SArray *list) {
55,340,748✔
1097
  int32_t      code = TSDB_CODE_SUCCESS;
55,340,748✔
1098
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, suid);
55,340,748✔
1099
  if (!pCur) {
55,341,985✔
1100
    return TSDB_CODE_OUT_OF_MEMORY;
×
1101
  }
1102

1103
  while (1) {
122,026,594✔
1104
    tb_uid_t id = metaStbCursorNext(pCur);
177,368,579✔
1105
    if (id == 0) {
177,371,894✔
1106
      break;
55,342,737✔
1107
    }
1108

1109
    if (NULL == taosArrayPush(list, &id)) {
122,026,594✔
1110
      qError("taosArrayPush failed");
×
1111
      code = terrno;
×
1112
      goto _exit;
×
1113
    }
1114
  }
1115

1116
_exit:
55,338,096✔
1117
  metaCloseStbCursor(pCur);
55,338,096✔
1118
  return code;
55,339,333✔
1119
}
1120

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

1129
  while (1) {
1,070✔
1130
    tb_uid_t id = metaStbCursorNext(pCur);
1,284✔
1131
    if (id == 0) {
1,284✔
1132
      break;
214✔
1133
    }
1134

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

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

1146
_exit:
214✔
1147
  metaCloseStbCursor(pCur);
214✔
1148
  return code;
214✔
1149
}
1150

1151
int32_t vnodeGetCtbNum(SVnode *pVnode, int64_t suid, int64_t *num) {
6,965,640✔
1152
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, suid, 0);
6,965,640✔
1153
  if (!pCur) {
6,967,057✔
1154
    return terrno;
×
1155
  }
1156

1157
  *num = 0;
6,967,057✔
1158
  while (1) {
2,398,615✔
1159
    tb_uid_t id = metaCtbCursorNext(pCur);
9,365,212✔
1160
    if (id == 0) {
9,365,679✔
1161
      break;
6,967,323✔
1162
    }
1163

1164
    ++(*num);
2,398,356✔
1165
  }
1166

1167
  metaCloseCtbCursor(pCur);
6,967,323✔
1168
  return TSDB_CODE_SUCCESS;
6,967,212✔
1169
}
1170

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

1180
  return TSDB_CODE_SUCCESS;
6,967,134✔
1181
}
1182

1183
int32_t vnodeGetStbInfo(SVnode *pVnode, tb_uid_t suid, int64_t *keep, int8_t *flags) {
6,967,206✔
1184
  SMetaReader mr = {0};
6,967,206✔
1185
  metaReaderDoInit(&mr, pVnode->pMeta, META_READER_NOLOCK);
6,967,295✔
1186

1187
  int32_t code = metaReaderGetTableEntryByUid(&mr, suid);
6,966,804✔
1188
  if (code == TSDB_CODE_SUCCESS) {
6,967,206✔
1189
    if (keep) *keep = mr.me.stbEntry.keep;
6,967,206✔
1190
    if (flags) *flags = mr.me.flags;
6,966,916✔
1191
  } else {
UNCOV
1192
    if (keep) *keep = 0;
×
UNCOV
1193
    if (flags) *flags = 0;
×
1194
  }
1195

1196
  metaReaderClear(&mr);
6,966,715✔
1197
  return TSDB_CODE_SUCCESS;
6,967,206✔
1198
}
1199

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

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

1232
  if (!(dbName = strchr(pVnode->config.dbname, '.'))) return 0;
55,328,722✔
1233
  if (0 == strncmp(++dbName, "log", TSDB_DB_NAME_LEN)) {
55,331,374✔
1234
    tbNum = tkLogStbNum;
2,568✔
1235
    pTbArr = (const char **)&tkLogStb;
2,568✔
1236
  } else if (0 == strncmp(dbName, "audit", TSDB_DB_NAME_LEN)) {
55,328,806✔
1237
    tbNum = tkAuditStbNum;
×
1238
    pTbArr = (const char **)&tkAuditStb;
×
1239
  }
1240
  if (tbNum && pTbArr) {
55,331,374✔
1241
    *tbSize = metaSizeOfTbFilterCache(pVnode->pMeta, 0);
2,568✔
1242
    if (*tbSize < tbNum) {
2,568✔
1243
      for (int32_t i = 0; i < tbNum; ++i) {
37,878✔
1244
        tb_uid_t suid = metaGetTableEntryUidByName(pVnode->pMeta, pTbArr[i]);
36,808✔
1245
        if (suid != 0) {
36,808✔
1246
          code = metaPutTbToFilterCache(pVnode->pMeta, &suid, 0);
2,568✔
1247
          if (TSDB_CODE_SUCCESS != code) {
2,568✔
1248
            return code;
1,498✔
1249
          }
1250
        }
1251
      }
1252
      *tbSize = metaSizeOfTbFilterCache(pVnode->pMeta, 0);
1,070✔
1253
    }
1254
  }
1255

1256
  return code;
55,328,550✔
1257
}
1258
#endif
1259

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

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

1269
int32_t vnodeGetTimeSeriesNum(SVnode *pVnode, int64_t *num) {
55,330,048✔
1270
  SArray *suidList = NULL;
55,330,048✔
1271

1272
  if (!(suidList = taosArrayInit(1, sizeof(tb_uid_t)))) {
55,330,048✔
1273
    return terrno;
×
1274
  }
1275

1276
  int32_t tbFilterSize = 0;
55,328,722✔
1277
  int32_t code = TSDB_CODE_SUCCESS;
55,329,385✔
1278
#ifdef TD_ENTERPRISE
1279
  code = vnodeGetTimeSeriesBlackList(pVnode, &tbFilterSize);
55,329,385✔
1280
  if (TSDB_CODE_SUCCESS != code) {
55,330,048✔
1281
    goto _exit;
1,498✔
1282
  }
1283
#endif
1284

1285
  if ((!tbFilterSize && vnodeGetStbIdList(pVnode, 0, suidList) < 0) ||
55,328,550✔
1286
      (tbFilterSize && vnodeGetStbIdListByFilter(pVnode, 0, suidList, vnodeTimeSeriesFilter, pVnode) < 0)) {
55,327,798✔
1287
    qError("vgId:%d, failed to get stb id list error: %s", TD_VID(pVnode), terrstr());
×
1288
    taosArrayDestroy(suidList);
×
1289
    return terrno;
×
1290
  }
1291

1292
  *num = 0;
55,329,124✔
1293
  int64_t arrSize = taosArrayGetSize(suidList);
55,327,887✔
1294
  for (int64_t i = 0; i < arrSize; ++i) {
177,336,912✔
1295
    tb_uid_t suid = *(tb_uid_t *)taosArrayGet(suidList, i);
122,006,373✔
1296

1297
    int64_t ctbNum = 0;
122,006,373✔
1298
    int32_t numOfCols = 0;
122,007,036✔
1299
    int8_t  flags = 0;
122,007,699✔
1300
    code = metaGetStbStats(pVnode, suid, &ctbNum, &numOfCols, &flags);
122,007,610✔
1301
    if (TSDB_CODE_SUCCESS != code) {
122,011,014✔
1302
      goto _exit;
×
1303
    }
1304
    if (!TABLE_IS_VIRTUAL(flags)) {
122,011,014✔
1305
      *num += ctbNum * (numOfCols - 1);
121,752,129✔
1306
    }
1307
  }
1308

1309
_exit:
55,332,037✔
1310
  taosArrayDestroy(suidList);
55,332,037✔
1311
  return TSDB_CODE_SUCCESS;
55,330,622✔
1312
}
1313

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

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

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

1334
    *num += ctbNum;
×
1335
  }
1336

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

1341
void *vnodeGetIdx(void *pVnode) {
4,297,963✔
1342
  if (pVnode == NULL) {
4,297,963✔
1343
    return NULL;
×
1344
  }
1345

1346
  return metaGetIdx(((SVnode *)pVnode)->pMeta);
4,297,963✔
1347
}
1348

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

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

1360
static FORCE_INLINE int32_t vnodeGetDBPrimaryInfo(SVnode *pVnode, SDbSizeStatisInfo *pInfo) {
1361
  int32_t code = 0;
6,110,055✔
1362
  char    path[TSDB_FILENAME_LEN] = {0};
6,110,055✔
1363

1364
  char   *dirName[] = {VNODE_TSDB_DIR, VNODE_WAL_DIR, VNODE_META_DIR, VNODE_TSDB_CACHE_DIR};
6,110,055✔
1365
  int64_t dirSize[4];
6,104,182✔
1366

1367
  vnodeGetPrimaryPath(pVnode, false, path, TSDB_FILENAME_LEN);
6,110,055✔
1368
  int32_t offset = strlen(path);
6,110,055✔
1369

1370
  for (int i = 0; i < sizeof(dirName) / sizeof(dirName[0]); i++) {
30,550,275✔
1371
    int64_t size = {0};
24,440,220✔
1372
    (void)snprintf(path + offset, TSDB_FILENAME_LEN - offset, "%s%s", TD_DIRSEP, dirName[i]);
24,440,220✔
1373
    code = taosGetDirSize(path, &size);
24,440,220✔
1374
    if (code != 0) {
24,440,220✔
1375
      uWarn("vnode %d get dir %s %s size failed since %s", TD_VID(pVnode), path, dirName[i], tstrerror(code));
1,936✔
1376
    }
1377
    path[offset] = 0;
24,440,220✔
1378
    dirSize[i] = size;
24,440,220✔
1379
  }
1380

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

1397
  code = tsdbGetFsSize(pVnodeObj->pTsdb, pInfo);
6,110,055✔
1398
_exit:
6,110,055✔
1399
  return code;
6,110,055✔
1400
}
1401

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

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

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

1434
  return 0;
×
1435
}
1436

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

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

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

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

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

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

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

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

1478
  return 0;
×
1479
}
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