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

taosdata / TDengine / #4820

20 Oct 2025 09:08AM UTC coverage: 61.353% (-0.04%) from 61.392%
#4820

push

travis-ci

happyguoxy
add alarm result print

156500 of 324369 branches covered (48.25%)

Branch coverage included in aggregate %.

207878 of 269535 relevant lines covered (77.12%)

242602011.17 hits per line

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

52.57
/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) {
12,583,560✔
29
  return qWorkerInit(NODE_TYPE_VNODE, TD_VID(pVnode), (void **)&pVnode->pQuery, &pVnode->msgCb);
12,583,560✔
30
}
31

32
void vnodeQueryPreClose(SVnode *pVnode) { qWorkerStopAllTasks((void *)pVnode->pQuery); }
12,595,019✔
33

34
void vnodeQueryClose(SVnode *pVnode) { qWorkerDestroy((void **)&pVnode->pQuery); }
12,597,139✔
35

36
int32_t fillTableColCmpr(SMetaReader *reader, SSchemaExt *pExt, int32_t numOfCol) {
81,208,828✔
37
  int8_t tblType = reader->me.type;
81,208,828✔
38
  if (withExtSchema(tblType)) {
81,208,828✔
39
    SColCmprWrapper *p = &(reader->me.colCmpr);
80,576,222✔
40
    if (numOfCol != p->nCols) {
80,576,222!
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++) {
805,159,342✔
45
      SColCmpr *pCmpr = &p->pColCmpr[i];
724,587,597✔
46
      pExt[i].colId = pCmpr->id;
724,579,306✔
47
      pExt[i].compress = pCmpr->alg;
724,568,403✔
48
    }
49
  }
50
  return 0;
81,207,696✔
51
}
52

53
void vnodePrintTableMeta(STableMetaRsp *pMeta) {
81,206,381✔
54
  if (!(qDebugFlag & DEBUG_DEBUG)) {
81,206,381✔
55
    return;
395,215✔
56
  }
57

58
  qDebug("tbName:%s", pMeta->tbName);
80,811,166✔
59
  qDebug("stbName:%s", pMeta->stbName);
80,815,175!
60
  qDebug("dbFName:%s", pMeta->dbFName);
80,815,175!
61
  qDebug("dbId:%" PRId64, pMeta->dbId);
80,815,175!
62
  qDebug("numOfTags:%d", pMeta->numOfTags);
80,815,175!
63
  qDebug("numOfColumns:%d", pMeta->numOfColumns);
80,815,175!
64
  qDebug("precision:%d", pMeta->precision);
80,815,175!
65
  qDebug("tableType:%d", pMeta->tableType);
80,815,175!
66
  qDebug("sversion:%d", pMeta->sversion);
80,815,175!
67
  qDebug("tversion:%d", pMeta->tversion);
80,815,175!
68
  qDebug("suid:%" PRIu64, pMeta->suid);
80,815,175!
69
  qDebug("tuid:%" PRIu64, pMeta->tuid);
80,815,175!
70
  qDebug("vgId:%d", pMeta->vgId);
80,815,175!
71
  qDebug("sysInfo:%d", pMeta->sysInfo);
80,815,175!
72
  if (pMeta->pSchemas) {
80,815,175!
73
    for (int32_t i = 0; i < (pMeta->numOfColumns + pMeta->numOfTags); ++i) {
1,014,877,856✔
74
      SSchema *pSchema = pMeta->pSchemas + i;
934,062,681✔
75
      qDebug("%d col/tag: type:%d, flags:%d, colId:%d, bytes:%d, name:%s", i, pSchema->type, pSchema->flags,
934,062,681!
76
             pSchema->colId, pSchema->bytes, pSchema->name);
77
    }
78
  }
79
}
80

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

103
int32_t vnodeGetTableMeta(SVnode *pVnode, SRpcMsg *pMsg, bool direct) {
146,500,388✔
104
  STableInfoReq  infoReq = {0};
146,500,388✔
105
  STableMetaRsp  metaRsp = {0};
146,501,685✔
106
  SMetaReader    mer1 = {0};
146,495,653✔
107
  SMetaReader    mer2 = {0};
146,494,546✔
108
  char           tableFName[TSDB_TABLE_FNAME_LEN];
146,486,996✔
109
  bool           reqTbUid = false;
146,494,270✔
110
  SRpcMsg        rpcMsg = {0};
146,494,270✔
111
  int32_t        code = 0;
146,494,270✔
112
  int32_t        rspLen = 0;
146,494,270✔
113
  void          *pRsp = NULL;
146,494,270✔
114
  SSchemaWrapper schema = {0};
146,494,270✔
115
  SSchemaWrapper schemaTag = {0};
146,494,270✔
116
  uint8_t        autoCreateCtb = 0;
146,494,270✔
117

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

125
  if (infoReq.option == REQ_OPT_TBUID) reqTbUid = true;
146,501,685✔
126
  metaRsp.dbId = pVnode->config.dbId;
146,501,685✔
127
  tstrncpy(metaRsp.tbName, infoReq.tbName, TSDB_TABLE_NAME_LEN);
146,500,303✔
128
  (void)memcpy(metaRsp.dbFName, infoReq.dbFName, sizeof(metaRsp.dbFName));
146,500,303✔
129

130
  if (!reqTbUid) {
146,500,303✔
131
    (void)tsnprintf(tableFName, TSDB_TABLE_FNAME_LEN, "%s.%s", infoReq.dbFName, infoReq.tbName);
144,363,881✔
132
    if (pVnode->mounted) tTrimMountPrefix(tableFName);
144,367,981!
133
    code = vnodeValidateTableHash(pVnode, tableFName);
144,366,419✔
134
    if (code) {
144,363,013!
135
      goto _exit4;
×
136
    }
137
  }
138

139
  // query meta
140
  metaReaderDoInit(&mer1, pVnode->pMeta, META_READER_LOCK);
146,499,435✔
141
  if (reqTbUid) {
146,502,947✔
142
    SET_ERRNO(0);
2,135,266✔
143
    uint64_t tbUid = taosStr2UInt64(infoReq.tbName, NULL, 10);
2,135,266✔
144
    if (ERRNO == ERANGE || tbUid == 0) {
2,135,266!
145
      code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
146
      goto _exit3;
×
147
    }
148
    SMetaReader mr3 = {0};
2,135,266✔
149
    metaReaderDoInit(&mr3, ((SVnode *)pVnode)->pMeta, META_READER_NOLOCK);
2,135,266✔
150
    if ((code = metaReaderGetTableEntryByUid(&mr3, tbUid)) < 0) {
2,135,266✔
151
      metaReaderClear(&mr3);
868,136✔
152
      TAOS_CHECK_GOTO(code, NULL, _exit3);
868,136!
153
    }
154
    tstrncpy(metaRsp.tbName, mr3.me.name, TSDB_TABLE_NAME_LEN);
1,267,130!
155
    metaReaderClear(&mr3);
1,267,130✔
156
    TAOS_CHECK_GOTO(metaGetTableEntryByName(&mer1, metaRsp.tbName), NULL, _exit3);
1,267,130!
157
  } else if (metaGetTableEntryByName(&mer1, infoReq.tbName) < 0) {
144,367,681✔
158
    code = terrno;
64,424,721✔
159
    goto _exit3;
64,424,721✔
160
  }
161

162
  metaRsp.tableType = mer1.me.type;
81,208,260✔
163
  metaRsp.vgId = TD_VID(pVnode);
81,208,260✔
164
  metaRsp.tuid = mer1.me.uid;
81,200,663✔
165

166
  switch (mer1.me.type) {
81,200,663!
167
    case TSDB_SUPER_TABLE: {
39,581,515✔
168
      (void)strcpy(metaRsp.stbName, mer1.me.name);
39,581,515!
169
      schema = mer1.me.stbEntry.schemaRow;
39,581,515✔
170
      schemaTag = mer1.me.stbEntry.schemaTag;
39,581,515✔
171
      metaRsp.suid = mer1.me.uid;
39,581,515✔
172
      break;
39,581,515✔
173
    }
174
    case TSDB_CHILD_TABLE:
32,628,928✔
175
    case TSDB_VIRTUAL_CHILD_TABLE:{
176
      metaReaderDoInit(&mer2, pVnode->pMeta, META_READER_NOLOCK);
32,628,928✔
177
      if (metaReaderGetTableEntryByUid(&mer2, mer1.me.ctbEntry.suid) < 0) goto _exit2;
32,631,548!
178

179
      (void)strcpy(metaRsp.stbName, mer2.me.name);
32,630,990!
180
      metaRsp.suid = mer2.me.uid;
32,630,990✔
181
      schema = mer2.me.stbEntry.schemaRow;
32,630,990✔
182
      schemaTag = mer2.me.stbEntry.schemaTag;
32,630,990✔
183
      break;
32,630,990✔
184
    }
185
    case TSDB_NORMAL_TABLE:
8,990,220✔
186
    case TSDB_VIRTUAL_NORMAL_TABLE: {
187
      schema = mer1.me.ntbEntry.schemaRow;
8,990,220✔
188
      break;
8,990,220✔
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;
81,202,725✔
197
  metaRsp.numOfColumns = schema.nCols;
81,202,725✔
198
  metaRsp.precision = pVnode->config.tsdbCfg.precision;
81,202,725✔
199
  metaRsp.sversion = schema.version;
81,203,243✔
200
  metaRsp.tversion = schemaTag.version;
81,203,243✔
201
  metaRsp.pSchemas = (SSchema *)taosMemoryMalloc(sizeof(SSchema) * (metaRsp.numOfColumns + metaRsp.numOfTags));
81,203,243!
202
  metaRsp.pSchemaExt = (SSchemaExt *)taosMemoryCalloc(metaRsp.numOfColumns, sizeof(SSchemaExt));
81,202,040!
203
  if (NULL == metaRsp.pSchemas || NULL == metaRsp.pSchemaExt) {
81,204,143!
204
    code = terrno;
1,107✔
205
    goto _exit;
×
206
  }
207
  (void)memcpy(metaRsp.pSchemas, schema.pSchema, sizeof(SSchema) * schema.nCols);
81,203,400!
208
  if (schemaTag.nCols) {
81,203,400✔
209
    (void)memcpy(metaRsp.pSchemas + schema.nCols, schemaTag.pSchema, sizeof(SSchema) * schemaTag.nCols);
72,215,380!
210
  }
211
  if (metaRsp.pSchemaExt) {
81,203,827!
212
    SMetaReader *pReader = mer1.me.type == TSDB_CHILD_TABLE ? &mer2 : &mer1;
81,207,366✔
213
    code = fillTableColCmpr(pReader, metaRsp.pSchemaExt, metaRsp.numOfColumns);
81,207,366✔
214
    if (code < 0) {
81,207,696!
215
      goto _exit;
×
216
    }
217
    for (int32_t i = 0; i < metaRsp.numOfColumns && pReader->me.pExtSchemas; i++) {
167,864,268✔
218
      metaRsp.pSchemaExt[i].typeMod = pReader->me.pExtSchemas[i].typeMod;
86,656,572✔
219
    }
220
  } else {
221
    code = TSDB_CODE_OUT_OF_MEMORY;
×
222
    goto _exit;
×
223
  }
224
  if (hasRefCol(mer1.me.type)) {
81,206,498✔
225
    metaRsp.rversion = mer1.me.colRef.version;
630,172✔
226
    metaRsp.pColRefs = (SColRef*)taosMemoryMalloc(sizeof(SColRef) * metaRsp.numOfColumns);
630,172!
227
    if (metaRsp.pColRefs) {
634,168!
228
      code = fillTableColRef(&mer1, metaRsp.pColRefs, metaRsp.numOfColumns);
634,168✔
229
      if (code < 0) {
634,168!
230
        goto _exit;
×
231
      }
232
    }
233
    metaRsp.numOfColRefs = metaRsp.numOfColumns;
634,168✔
234
  } else {
235
    metaRsp.pColRefs = NULL;
80,573,528✔
236
    metaRsp.numOfColRefs = 0;
80,573,528✔
237
  }
238

239
  vnodePrintTableMeta(&metaRsp);
81,207,696✔
240

241
  // encode and send response
242
  rspLen = tSerializeSTableMetaRsp(NULL, 0, &metaRsp);
81,210,390✔
243
  if (rspLen < 0) {
81,208,828!
244
    code = terrno;
×
245
    goto _exit;
×
246
  }
247

248
  if (direct) {
81,208,828✔
249
    pRsp = rpcMallocCont(rspLen);
914,196✔
250
  } else {
251
    pRsp = taosMemoryCalloc(1, rspLen);
80,294,632!
252
  }
253

254
  if (pRsp == NULL) {
81,206,315!
255
    code = terrno;
×
256
    goto _exit;
×
257
  }
258

259
  rspLen = tSerializeSTableMetaRsp(pRsp, rspLen, &metaRsp);
81,206,315✔
260
  if (rspLen < 0) {
81,209,075!
261
    code = terrno;
×
262
    goto _exit;
×
263
  }
264

265
_exit:
81,205,536✔
266
  taosMemoryFree(metaRsp.pColRefs);
81,206,495!
267
  taosMemoryFree(metaRsp.pSchemas);
81,201,759!
268
  taosMemoryFree(metaRsp.pSchemaExt);
81,208,828!
269
_exit2:
81,203,739✔
270
  metaReaderClear(&mer2);
81,207,721✔
271
_exit3:
146,497,688✔
272
  metaReaderClear(&mer1);
146,499,547✔
273
_exit4:
146,496,924✔
274
  rpcMsg.info = pMsg->info;
146,501,685✔
275
  rpcMsg.pCont = pRsp;
146,501,280✔
276
  rpcMsg.contLen = rspLen;
146,501,280✔
277
  rpcMsg.code = code;
146,501,280✔
278
  rpcMsg.msgType = pMsg->msgType;
146,501,280✔
279

280
  if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST && autoCreateCtb == 1) {
146,495,063✔
281
    code = TSDB_CODE_SUCCESS;
54,325,779✔
282
  }
283

284
  if (code) {
146,495,063✔
285
    qError("get table %s meta with %" PRIu8 " failed cause of %s", infoReq.tbName, infoReq.option, tstrerror(code));
10,967,078✔
286
  }
287

288
  if (direct) {
146,501,280✔
289
    tmsgSendRsp(&rpcMsg);
3,233,941✔
290
  } else {
291
    *pMsg = rpcMsg;
143,267,339✔
292
  }
293

294
  return code;
146,501,280✔
295
}
296

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

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

316
  tstrncpy(cfgRsp.tbName, cfgReq.tbName, TSDB_TABLE_NAME_LEN);
237,288!
317
  (void)memcpy(cfgRsp.dbFName, cfgReq.dbFName, sizeof(cfgRsp.dbFName));
237,288✔
318

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

326
  // query meta
327
  metaReaderDoInit(&mer1, pVnode->pMeta, META_READER_LOCK);
237,288✔
328

329
  if (metaGetTableEntryByName(&mer1, cfgReq.tbName) < 0) {
237,288!
330
    code = terrno;
×
331
    goto _exit;
×
332
  }
333

334
  cfgRsp.tableType = mer1.me.type;
237,288✔
335

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

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

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

387
  if (NULL == cfgRsp.pSchemas || NULL == cfgRsp.pSchemaExt || NULL == cfgRsp.pColRefs) {
237,288!
388
    code = terrno;
×
389
    goto _exit;
×
390
  }
391
  (void)memcpy(cfgRsp.pSchemas, schema.pSchema, sizeof(SSchema) * schema.nCols);
237,288!
392
  if (schemaTag.nCols) {
237,288✔
393
    (void)memcpy(cfgRsp.pSchemas + schema.nCols, schemaTag.pSchema, sizeof(SSchema) * schemaTag.nCols);
142,923!
394
  }
395

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

400
  if (withExtSchema(cfgRsp.tableType)) {
237,288✔
401
    for (int32_t i = 0; i < cfgRsp.numOfColumns; i++) {
902,904✔
402
      SColCmpr   *pCmpr = &pColCmpr->pColCmpr[i];
684,914✔
403
      SSchemaExt *pSchExt = cfgRsp.pSchemaExt + i;
684,914✔
404
      pSchExt->colId = pCmpr->id;
684,914✔
405
      pSchExt->compress = pCmpr->alg;
684,914✔
406
      if (pReader->me.pExtSchemas)
684,914✔
407
        pSchExt->typeMod = pReader->me.pExtSchemas[i].typeMod;
19,756✔
408
      else
409
        pSchExt->typeMod = 0;
665,158✔
410
    }
411
  }
412

413
  cfgRsp.virtualStb = false;
237,288✔
414
  if (hasRefCol(cfgRsp.tableType)) {
237,288✔
415
    for (int32_t i = 0; i < cfgRsp.numOfColumns; i++) {
130,266✔
416
      SColRef *pRef = &pColRef->pColRef[i];
110,968✔
417
      cfgRsp.pColRefs[i].hasRef = pRef->hasRef;
110,968!
418
      cfgRsp.pColRefs[i].id = pRef->id;
110,968✔
419
      if (cfgRsp.pColRefs[i].hasRef) {
110,968!
420
        tstrncpy(cfgRsp.pColRefs[i].refDbName, pRef->refDbName, TSDB_DB_NAME_LEN);
55,767!
421
        tstrncpy(cfgRsp.pColRefs[i].refTableName, pRef->refTableName, TSDB_TABLE_NAME_LEN);
55,767!
422
        tstrncpy(cfgRsp.pColRefs[i].refColName, pRef->refColName, TSDB_COL_NAME_LEN);
55,767!
423
      }
424
    }
425
  }
426

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

434
  if (direct) {
237,288!
435
    pRsp = rpcMallocCont(rspLen);
×
436
  } else {
437
    pRsp = taosMemoryCalloc(1, rspLen);
237,288!
438
  }
439

440
  if (pRsp == NULL) {
237,288!
441
    code = terrno;
×
442
    goto _exit;
×
443
  }
444

445
  rspLen = tSerializeSTableCfgRsp(pRsp, rspLen, &cfgRsp);
237,288✔
446
  if (rspLen < 0) {
237,288!
447
    code = terrno;
×
448
    goto _exit;
×
449
  }
450

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

458
  if (code) {
237,288!
459
    qError("get table %s cfg failed cause of %s", cfgReq.tbName, tstrerror(code));
×
460
  }
461

462
  if (direct) {
237,288!
463
    tmsgSendRsp(&rpcMsg);
×
464
  } else {
465
    *pMsg = rpcMsg;
237,288✔
466
  }
467

468
  tFreeSTableCfgRsp(&cfgRsp);
237,288✔
469
  metaReaderClear(&mer2);
237,288✔
470
  metaReaderClear(&mer1);
237,288✔
471
  return code;
237,288✔
472
}
473

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

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

483
int32_t vnodeGetBatchMeta(SVnode *pVnode, SRpcMsg *pMsg) {
119,579,913✔
484
  int32_t      code = 0;
119,579,913✔
485
  int32_t      rspSize = 0;
119,579,913✔
486
  SBatchReq    batchReq = {0};
119,579,913✔
487
  SBatchMsg   *req = NULL;
119,581,930✔
488
  SBatchRspMsg rsp = {0};
119,581,930✔
489
  SBatchRsp    batchRsp = {0};
119,570,960✔
490
  SRpcMsg      reqMsg = *pMsg;
119,570,137✔
491
  SRpcMsg      rspMsg = {0};
119,570,137✔
492
  void        *pRsp = NULL;
119,573,215✔
493

494
  if (tDeserializeSBatchReq(pMsg->pCont, pMsg->contLen, &batchReq)) {
119,573,215!
495
    code = terrno;
×
496
    qError("tDeserializeSBatchReq failed");
×
497
    goto _exit;
×
498
  }
499

500
  int32_t msgNum = taosArrayGetSize(batchReq.pMsgs);
119,579,993✔
501
  if (msgNum >= MAX_META_MSG_IN_BATCH) {
119,579,918!
502
    code = TSDB_CODE_INVALID_MSG;
×
503
    qError("too many msgs %d in vnode batch meta req", msgNum);
×
504
    goto _exit;
×
505
  }
506

507
  batchRsp.pRsps = taosArrayInit(msgNum, sizeof(SBatchRspMsg));
119,579,918✔
508
  if (NULL == batchRsp.pRsps) {
119,579,678!
509
    code = terrno;
×
510
    qError("taosArrayInit %d SBatchRspMsg failed", msgNum);
×
511
    goto _exit;
×
512
  }
513

514
  for (int32_t i = 0; i < msgNum; ++i) {
266,306,379✔
515
    req = taosArrayGet(batchReq.pMsgs, i);
146,727,480✔
516
    if (req == NULL) {
146,727,925!
517
      code = terrno;
×
518
      goto _exit;
×
519
    }
520

521
    reqMsg.msgType = req->msgType;
146,727,925✔
522
    reqMsg.pCont = req->msg;
146,727,576✔
523
    reqMsg.contLen = req->msgLen;
146,723,285✔
524

525
    switch (req->msgType) {
146,720,349!
526
      case TDMT_VND_TABLE_META:
141,131,371✔
527
        // error code has been set into reqMsg, no need to handle it here.
528
        if (TSDB_CODE_SUCCESS != vnodeGetTableMeta(pVnode, &reqMsg, false)) {
141,131,371✔
529
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
7,779,197!
530
        }
531
        break;
141,130,299✔
532
      case TDMT_VND_TABLE_NAME:
2,135,266✔
533
        // error code has been set into reqMsg, no need to handle it here.
534
        if (TSDB_CODE_SUCCESS != vnodeGetTableMeta(pVnode, &reqMsg, false)) {
2,135,266✔
535
          qWarn("vnodeGetBatchName failed, msgType:%d", req->msgType);
868,136!
536
        }
537
        break;
2,135,266✔
538
      case TDMT_VND_TABLE_CFG:
237,288✔
539
        // error code has been set into reqMsg, no need to handle it here.
540
        if (TSDB_CODE_SUCCESS != vnodeGetTableCfg(pVnode, &reqMsg, false)) {
237,288!
541
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
×
542
        }
543
        break;
237,288✔
544
      case TDMT_VND_VSUBTABLES_META:
×
545
        // error code has been set into reqMsg, no need to handle it here.
546
        if (TSDB_CODE_SUCCESS != vnodeGetVSubtablesMeta(pVnode, &reqMsg)) {
×
547
          qWarn("vnodeGetVSubtablesMeta failed, msgType:%d", req->msgType);
×
548
        }
549
        break;
×
550
      case TDMT_VND_VSTB_REF_DBS:
3,223,555✔
551
        // error code has been set into reqMsg, no need to handle it here.
552
        if (TSDB_CODE_SUCCESS != vnodeGetVStbRefDbs(pVnode, &reqMsg)) {
3,223,555!
553
          qWarn("vnodeGetVStbRefDbs failed, msgType:%d", req->msgType);
×
554
        }
555
        break;
3,225,060✔
556
      default:
×
557
        qError("invalid req msgType %d", req->msgType);
×
558
        reqMsg.code = TSDB_CODE_INVALID_MSG;
×
559
        reqMsg.pCont = NULL;
×
560
        reqMsg.contLen = 0;
×
561
        break;
×
562
    }
563

564
    rsp.msgIdx = req->msgIdx;
146,727,913✔
565
    rsp.reqType = reqMsg.msgType;
146,729,111✔
566
    rsp.msgLen = reqMsg.contLen;
146,729,111✔
567
    rsp.rspCode = reqMsg.code;
146,729,111✔
568
    rsp.msg = reqMsg.pCont;
146,729,111✔
569

570
    if (NULL == taosArrayPush(batchRsp.pRsps, &rsp)) {
293,454,497!
571
      qError("taosArrayPush failed");
×
572
      code = terrno;
×
573
      goto _exit;
×
574
    }
575
  }
576

577
  rspSize = tSerializeSBatchRsp(NULL, 0, &batchRsp);
119,578,899✔
578
  if (rspSize < 0) {
119,571,452!
579
    qError("tSerializeSBatchRsp failed");
×
580
    code = terrno;
×
581
    goto _exit;
×
582
  }
583
  pRsp = rpcMallocCont(rspSize);
119,571,452✔
584
  if (pRsp == NULL) {
119,572,585!
585
    qError("rpcMallocCont %d failed", rspSize);
×
586
    code = terrno;
×
587
    goto _exit;
×
588
  }
589
  if (tSerializeSBatchRsp(pRsp, rspSize, &batchRsp) < 0) {
119,572,585!
590
    qError("tSerializeSBatchRsp %d failed", rspSize);
×
591
    code = terrno;
×
592
    goto _exit;
×
593
  }
594

595
_exit:
119,579,567✔
596

597
  rspMsg.info = pMsg->info;
119,583,964✔
598
  rspMsg.pCont = pRsp;
119,583,760✔
599
  rspMsg.contLen = rspSize;
119,583,760✔
600
  rspMsg.code = code;
119,583,760✔
601
  rspMsg.msgType = pMsg->msgType;
119,583,760✔
602

603
  if (code) {
119,562,367!
604
    qError("vnd get batch meta failed cause of %s", tstrerror(code));
×
605
  }
606

607
  taosArrayDestroyEx(batchReq.pMsgs, tFreeSBatchReqMsg);
119,562,367✔
608
  taosArrayDestroyEx(batchRsp.pRsps, tFreeSBatchRspMsg);
119,577,496✔
609

610
  tmsgSendRsp(&rspMsg);
119,568,360✔
611

612
  return code;
119,585,526✔
613
}
614

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

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

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

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

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

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

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

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

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

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

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

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

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

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

688
      refColsNum++;
×
689
    }
690

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

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

699
_return:
×
700

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

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

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

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

723
  pList = taosArrayInit(10, sizeof(uint64_t));
3,223,555✔
724
  QUERY_CHECK_NULL(pList, code, line, _return, terrno);
3,220,585!
725

726
  *ppRes = taosArrayInit(10, POINTER_BYTES);
3,220,585✔
727
  QUERY_CHECK_NULL(*ppRes, code, line, _return, terrno)
3,219,119!
728
  
729
  // lookup in cache
730
  code = pHandle->api.metaFn.metaGetCachedRefDbs(pHandle->vnode, suid, *ppRes);
3,222,089✔
731
  QUERY_CHECK_CODE(code, line, _return);
3,226,526!
732

733
  if (taosArrayGetSize(*ppRes) > 0) {
3,226,526✔
734
    // found in cache
735
    goto _return;
3,171,635✔
736
  } else {
737
    code = pHandle->api.metaFn.getChildTableList(pHandle->vnode, suid, pList);
54,891✔
738
    QUERY_CHECK_CODE(code, line, _return);
54,891!
739

740
    size_t num = taosArrayGetSize(pList);
54,891✔
741
    pDbNameHash = tSimpleHashInit(10, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
54,891✔
742
    QUERY_CHECK_NULL(pDbNameHash, code, line, _return, terrno);
54,891!
743

744
    for (int32_t i = 0; i < num; ++i) {
192,841✔
745
      uint64_t* id = taosArrayGet(pList, i);
137,950✔
746
      QUERY_CHECK_NULL(id, code, line, _return, terrno);
137,950!
747

748
      pHandle->api.metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pHandle->api.metaFn);
137,950✔
749
      readerInit = true;
137,950✔
750

751
      code = pHandle->api.metaReaderFn.getTableEntryByUid(&mr, *id);
137,950✔
752
      QUERY_CHECK_CODE(code, line, _return);
137,950!
753

754
      for (int32_t j = 0; j < mr.me.colRef.nCols; j++) {
1,147,946✔
755
        if (mr.me.colRef.pColRef[j].hasRef) {
1,009,996!
756
          if (NULL == tSimpleHashGet(pDbNameHash, mr.me.colRef.pColRef[j].refDbName, strlen(mr.me.colRef.pColRef[j].refDbName))) {
640,743!
757
            char *refDbName = taosStrdup(mr.me.colRef.pColRef[j].refDbName);
72,261!
758
            QUERY_CHECK_NULL(refDbName, code, line, _return, terrno);
72,261!
759

760
            QUERY_CHECK_NULL(taosArrayPush(*ppRes, &refDbName), code, line, _return, terrno);
144,522!
761

762
            code = tSimpleHashPut(pDbNameHash, refDbName, strlen(refDbName), NULL, 0);
72,261!
763
            QUERY_CHECK_CODE(code, line, _return);
72,261!
764
          }
765
        }
766
      }
767

768
      pHandle->api.metaReaderFn.clearReader(&mr);
137,950✔
769
      readerInit = false;
137,950✔
770
    }
771

772
    code = pHandle->api.metaFn.metaPutRefDbsToCache(pHandle->vnode, suid, *ppRes);
54,891✔
773
    QUERY_CHECK_CODE(code, line, _return);
54,891!
774
  }
775

776
_return:
3,226,526✔
777

778
  if (readerInit) {
3,225,021!
779
    pHandle->api.metaReaderFn.clearReader(&mr);
×
780
  }
781

782
  taosArrayDestroy(pList);
3,225,021✔
783
  tSimpleHashCleanup(pDbNameHash);
3,225,021✔
784

785
  if (code) {
3,225,021!
786
    qError("%s failed since %s", __func__, tstrerror(code));
×
787
  }
788
  return code;
3,225,021✔
789
}
790

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

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

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

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

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

832
_return:
×
833

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

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

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

848
  //tmsgSendRsp(&rspMsg);
849

850
  return code;
×
851
}
852

853
int32_t vnodeGetVStbRefDbs(SVnode *pVnode, SRpcMsg *pMsg) {
3,223,555✔
854
  int32_t        code = 0;
3,223,555✔
855
  int32_t        rspSize = 0;
3,223,555✔
856
  SVStbRefDbsReq req = {0};
3,223,555✔
857
  SVStbRefDbsRsp rsp = {0};
3,223,555✔
858
  SRpcMsg        rspMsg = {0};
3,225,060✔
859
  void          *pRsp = NULL;
3,225,060✔
860
  int32_t        line = 0;
3,225,060✔
861

862
  if (tDeserializeSVStbRefDbsReq(pMsg->pCont, pMsg->contLen, &req)) {
3,225,060!
863
    code = terrno;
×
864
    qError("tDeserializeSVSubTablesReq failed");
×
865
    goto _return;
×
866
  }
867

868
  SReadHandle handle = {0};
3,223,555✔
869
  handle.vnode = pVnode;
3,223,555✔
870
  initStorageAPI(&handle.api);
3,223,555✔
871

872
  code = vnodeReadVStbRefDbs(&handle, req.suid, &rsp.pDbs);
3,223,555✔
873
  QUERY_CHECK_CODE(code, line, _return);
3,225,021!
874
  rsp.vgId = TD_VID(pVnode);
3,225,021✔
875

876
  rspSize = tSerializeSVStbRefDbsRsp(NULL, 0, &rsp);
3,225,021✔
877
  if (rspSize < 0) {
3,222,051!
878
    code = rspSize;
×
879
    qError("tSerializeSVStbRefDbsRsp failed, error:%d", rspSize);
×
880
    goto _return;
×
881
  }
882
  pRsp = taosMemoryCalloc(1, rspSize);
3,222,051!
883
  if (pRsp == NULL) {
3,225,021!
884
    code = terrno;
×
885
    qError("rpcMallocCont %d failed, error:%d", rspSize, terrno);
×
886
    goto _return;
×
887
  }
888
  rspSize = tSerializeSVStbRefDbsRsp(pRsp, rspSize, &rsp);
3,225,021✔
889
  if (rspSize < 0) {
3,222,050!
890
    code = rspSize;
×
891
    qError("tSerializeSVStbRefDbsRsp failed, error:%d", rspSize);
×
892
    goto _return;
×
893
  }
894

895
_return:
3,222,050✔
896

897
  rspMsg.info = pMsg->info;
3,225,021✔
898
  rspMsg.pCont = pRsp;
3,226,526✔
899
  rspMsg.contLen = rspSize;
3,226,526✔
900
  rspMsg.code = code;
3,226,526✔
901
  rspMsg.msgType = pMsg->msgType;
3,226,526✔
902

903
  if (code) {
3,222,180!
904
    qError("vnd get virtual stb ref db failed cause of %s", tstrerror(code));
×
905
  }
906

907
  *pMsg = rspMsg;
3,222,180✔
908

909
  tDestroySVStbRefDbsRsp(&rsp);
3,223,555✔
910

911
  return code;
3,225,060✔
912
}
913

914
static int32_t vnodeGetCompStorage(SVnode *pVnode, int64_t *output) {
436,360,885✔
915
  int32_t code = 0;
436,360,885✔
916
#ifdef TD_ENTERPRISE
917
  int32_t now = taosGetTimestampSec();
436,360,885✔
918
  if (llabs(now - pVnode->config.vndStats.storageLastUpd) >= 30) {
436,360,885✔
919
    pVnode->config.vndStats.storageLastUpd = now;
22,197,572✔
920

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

940
static void vnodeGetBufferInfo(SVnode *pVnode, int64_t *bufferSegmentUsed, int64_t *bufferSegmentSize) {
436,360,885✔
941
  *bufferSegmentUsed = 0;
436,360,885✔
942
  *bufferSegmentSize = 0;
436,360,885✔
943
  if (pVnode) {
436,360,885!
944
    (void)taosThreadMutexLock(&pVnode->mutex);
436,360,885✔
945

946
    if (pVnode->inUse) {
436,360,885✔
947
      *bufferSegmentUsed = pVnode->inUse->size;
436,311,482✔
948
    }
949
    *bufferSegmentSize = pVnode->config.szBuf / VNODE_BUFPOOL_SEGMENTS;
436,360,885✔
950

951
    (void)taosThreadMutexUnlock(&pVnode->mutex);
436,360,885✔
952
  }
953
}
436,360,885✔
954

955
int32_t vnodeGetLoad(SVnode *pVnode, SVnodeLoad *pLoad) {
436,360,885✔
956
  SSyncState state = syncGetState(pVnode->sync);
436,360,885✔
957
  pLoad->syncAppliedIndex = pVnode->state.applied;
436,360,885✔
958
  syncGetCommitIndex(pVnode->sync, &pLoad->syncCommitIndex);
436,360,885✔
959

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

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

1007
void vnodeGetInfo(void *pVnode, const char **dbname, int32_t *vgId, int64_t *numOfTables, int64_t *numOfNormalTables) {
32,538,644✔
1008
  SVnode    *pVnodeObj = pVnode;
32,538,644✔
1009
  SVnodeCfg *pConf = &pVnodeObj->config;
32,538,644✔
1010

1011
  if (dbname) {
32,545,735✔
1012
    *dbname = pConf->dbname;
32,544,692✔
1013
  }
1014

1015
  if (vgId) {
32,545,731✔
1016
    *vgId = TD_VID(pVnodeObj);
30,829,914✔
1017
  }
1018

1019
  if (numOfTables) {
32,544,124!
1020
    *numOfTables = pConf->vndStats.numOfNTables + pConf->vndStats.numOfCTables;
×
1021
  }
1022

1023
  if (numOfNormalTables) {
32,544,124!
1024
    *numOfNormalTables = pConf->vndStats.numOfNTables;
×
1025
  }
1026
}
32,544,124✔
1027

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

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

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

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

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

1066
int32_t vnodeGetCtbIdList(void *pVnode, int64_t suid, SArray *list) {
475,472,559✔
1067
  int32_t      code = TSDB_CODE_SUCCESS;
475,472,559✔
1068
  SVnode      *pVnodeObj = pVnode;
475,472,559✔
1069
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnodeObj, suid, 1);
475,472,559✔
1070
  if (NULL == pCur) {
475,745,304!
1071
    qError("vnode get all table list failed");
×
1072
    return terrno;
×
1073
  }
1074

1075
  while (1) {
2,147,483,647✔
1076
    tb_uid_t id = metaCtbCursorNext(pCur);
2,147,483,647✔
1077
    if (id == 0) {
2,147,483,647✔
1078
      break;
475,954,045✔
1079
    }
1080

1081
    if (NULL == taosArrayPush(list, &id)) {
2,147,483,647!
1082
      qError("taosArrayPush failed");
×
1083
      code = terrno;
×
1084
      goto _exit;
×
1085
    }
1086
  }
1087

1088
_exit:
475,575,583✔
1089
  metaCloseCtbCursor(pCur);
475,575,583✔
1090
  return code;
475,573,815✔
1091
}
1092

1093
int32_t vnodeGetStbIdList(SVnode *pVnode, int64_t suid, SArray *list) {
193,341,158✔
1094
  int32_t      code = TSDB_CODE_SUCCESS;
193,341,158✔
1095
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, suid);
193,341,158✔
1096
  if (!pCur) {
193,342,055!
1097
    return TSDB_CODE_OUT_OF_MEMORY;
×
1098
  }
1099

1100
  while (1) {
313,931,632✔
1101
    tb_uid_t id = metaStbCursorNext(pCur);
507,273,687✔
1102
    if (id == 0) {
507,275,999✔
1103
      break;
193,355,428✔
1104
    }
1105

1106
    if (NULL == taosArrayPush(list, &id)) {
313,935,530!
1107
      qError("taosArrayPush failed");
×
1108
      code = terrno;
×
1109
      goto _exit;
×
1110
    }
1111
  }
1112

1113
_exit:
193,342,829✔
1114
  metaCloseStbCursor(pCur);
193,342,829✔
1115
  return code;
193,331,018✔
1116
}
1117

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

1126
  while (1) {
9,190✔
1127
    tb_uid_t id = metaStbCursorNext(pCur);
11,028✔
1128
    if (id == 0) {
11,028✔
1129
      break;
1,838✔
1130
    }
1131

1132
    if ((*filter) && (*filter)(arg, &id)) {
9,190!
1133
      continue;
9,190✔
1134
    }
1135

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

1143
_exit:
1,838✔
1144
  metaCloseStbCursor(pCur);
1,838✔
1145
  return code;
1,838✔
1146
}
1147

1148
int32_t vnodeGetCtbNum(SVnode *pVnode, int64_t suid, int64_t *num) {
22,896,028✔
1149
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, suid, 0);
22,896,028✔
1150
  if (!pCur) {
22,901,927!
1151
    return terrno;
×
1152
  }
1153

1154
  *num = 0;
22,901,927✔
1155
  while (1) {
8,537,582✔
1156
    tb_uid_t id = metaCtbCursorNext(pCur);
31,440,385✔
1157
    if (id == 0) {
31,440,856✔
1158
      break;
22,905,034✔
1159
    }
1160

1161
    ++(*num);
8,535,822✔
1162
  }
1163

1164
  metaCloseCtbCursor(pCur);
22,905,034✔
1165
  return TSDB_CODE_SUCCESS;
22,905,399✔
1166
}
1167

1168
int32_t vnodeGetStbColumnNum(SVnode *pVnode, tb_uid_t suid, int *num) {
22,900,292✔
1169
  SSchemaWrapper *pSW = metaGetTableSchema(pVnode->pMeta, suid, -1, 0, NULL, 0);
22,900,292✔
1170
  if (pSW) {
22,906,275!
1171
    *num = pSW->nCols;
22,906,275✔
1172
    tDeleteSchemaWrapper(pSW);
1173
  } else {
1174
    *num = 2;
×
1175
  }
1176

1177
  return TSDB_CODE_SUCCESS;
22,904,527✔
1178
}
1179

1180
int32_t vnodeGetStbInfo(SVnode *pVnode, tb_uid_t suid, int64_t *keep, int8_t *flags) {
22,903,666✔
1181
  SMetaReader mr = {0};
22,903,666✔
1182
  metaReaderDoInit(&mr, pVnode->pMeta, META_READER_NOLOCK);
22,904,677✔
1183

1184
  int32_t code = metaReaderGetTableEntryByUid(&mr, suid);
22,904,423✔
1185
  if (code == TSDB_CODE_SUCCESS) {
22,906,422!
1186
    if (keep) *keep = mr.me.stbEntry.keep;
22,906,422!
1187
    if (flags) *flags = mr.me.flags;
22,906,422!
1188
  } else {
1189
    if (keep) *keep = 0;
×
1190
    if (flags) *flags = 0;
×
1191
  }
1192

1193
  metaReaderClear(&mr);
22,904,674✔
1194
  return TSDB_CODE_SUCCESS;
22,903,483✔
1195
}
1196

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

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

1229
  if (!(dbName = strchr(pVnode->config.dbname, '.'))) return 0;
193,349,535!
1230
  if (0 == strncmp(++dbName, "log", TSDB_DB_NAME_LEN)) {
193,336,185!
1231
    tbNum = tkLogStbNum;
16,542✔
1232
    pTbArr = (const char **)&tkLogStb;
16,542✔
1233
  } else if (0 == strncmp(dbName, "audit", TSDB_DB_NAME_LEN)) {
193,333,736!
1234
    tbNum = tkAuditStbNum;
×
1235
    pTbArr = (const char **)&tkAuditStb;
×
1236
  }
1237
  if (tbNum && pTbArr) {
193,350,278!
1238
    *tbSize = metaSizeOfTbFilterCache(pVnode->pMeta, 0);
16,542✔
1239
    if (*tbSize < tbNum) {
16,542!
1240
      for (int32_t i = 0; i < tbNum; ++i) {
231,588✔
1241
        tb_uid_t suid = metaGetTableEntryUidByName(pVnode->pMeta, pTbArr[i]);
226,074✔
1242
        if (suid != 0) {
226,074✔
1243
          code = metaPutTbToFilterCache(pVnode->pMeta, &suid, 0);
20,218✔
1244
          if (TSDB_CODE_SUCCESS != code) {
20,218✔
1245
            return code;
11,028✔
1246
          }
1247
        }
1248
      }
1249
      *tbSize = metaSizeOfTbFilterCache(pVnode->pMeta, 0);
5,514✔
1250
    }
1251
  }
1252

1253
  return code;
193,340,069✔
1254
}
1255
#endif
1256

1257
static bool vnodeTimeSeriesFilter(void *arg1, void *arg2) {
9,190✔
1258
  SVnode *pVnode = (SVnode *)arg1;
9,190✔
1259

1260
  if (metaTbInFilterCache(pVnode->pMeta, arg2, 0)) {
9,190!
1261
    return true;
9,190✔
1262
  }
1263
  return false;
×
1264
}
1265

1266
int32_t vnodeGetTimeSeriesNum(SVnode *pVnode, int64_t *num) {
193,349,535✔
1267
  SArray *suidList = NULL;
193,349,535✔
1268

1269
  if (!(suidList = taosArrayInit(1, sizeof(tb_uid_t)))) {
193,349,535!
1270
    return terrno;
×
1271
  }
1272

1273
  int32_t tbFilterSize = 0;
193,356,557✔
1274
  int32_t code = TSDB_CODE_SUCCESS;
193,342,499✔
1275
#ifdef TD_ENTERPRISE
1276
  code = vnodeGetTimeSeriesBlackList(pVnode, &tbFilterSize);
193,342,499✔
1277
  if (TSDB_CODE_SUCCESS != code) {
193,344,814✔
1278
    goto _exit;
11,028✔
1279
  }
1280
#endif
1281

1282
  if ((!tbFilterSize && vnodeGetStbIdList(pVnode, 0, suidList) < 0) ||
193,333,786✔
1283
      (tbFilterSize && vnodeGetStbIdListByFilter(pVnode, 0, suidList, vnodeTimeSeriesFilter, pVnode) < 0)) {
193,328,316✔
1284
    qError("vgId:%d, failed to get stb id list error: %s", TD_VID(pVnode), terrstr());
×
1285
    taosArrayDestroy(suidList);
×
1286
    return terrno;
×
1287
  }
1288

1289
  *num = 0;
193,329,909✔
1290
  int64_t arrSize = taosArrayGetSize(suidList);
193,341,631✔
1291
  for (int64_t i = 0; i < arrSize; ++i) {
507,266,408✔
1292
    tb_uid_t suid = *(tb_uid_t *)taosArrayGet(suidList, i);
313,913,912✔
1293

1294
    int64_t ctbNum = 0;
313,917,652✔
1295
    int32_t numOfCols = 0;
313,923,215✔
1296
    int8_t  flags = 0;
313,910,788✔
1297
    code = metaGetStbStats(pVnode, suid, &ctbNum, &numOfCols, &flags);
313,903,728✔
1298
    if (TSDB_CODE_SUCCESS != code) {
313,939,781!
1299
      goto _exit;
×
1300
    }
1301
    if (!TABLE_IS_VIRTUAL(flags)) {
313,939,781✔
1302
      *num += ctbNum * (numOfCols - 1);
307,618,199✔
1303
    }
1304
  }
1305

1306
_exit:
193,361,859✔
1307
  taosArrayDestroy(suidList);
193,363,524✔
1308
  return TSDB_CODE_SUCCESS;
193,348,613✔
1309
}
1310

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

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

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

1331
    *num += ctbNum;
×
1332
  }
1333

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

1338
void *vnodeGetIdx(void *pVnode) {
12,853,644✔
1339
  if (pVnode == NULL) {
12,853,644!
1340
    return NULL;
×
1341
  }
1342

1343
  return metaGetIdx(((SVnode *)pVnode)->pMeta);
12,853,644✔
1344
}
1345

1346
void *vnodeGetIvtIdx(void *pVnode) {
12,857,644✔
1347
  if (pVnode == NULL) {
12,857,644!
1348
    return NULL;
×
1349
  }
1350
  return metaGetIvtIdx(((SVnode *)pVnode)->pMeta);
12,857,644✔
1351
}
1352

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

1357
static FORCE_INLINE int32_t vnodeGetDBPrimaryInfo(SVnode *pVnode, SDbSizeStatisInfo *pInfo) {
1358
  int32_t code = 0;
22,280,142✔
1359
  char    path[TSDB_FILENAME_LEN] = {0};
22,280,142✔
1360

1361
  char   *dirName[] = {VNODE_TSDB_DIR, VNODE_WAL_DIR, VNODE_META_DIR, VNODE_TSDB_CACHE_DIR};
22,280,142✔
1362
  int64_t dirSize[4];
22,276,633✔
1363

1364
  vnodeGetPrimaryPath(pVnode, false, path, TSDB_FILENAME_LEN);
22,278,841✔
1365
  int32_t offset = strlen(path);
22,280,142✔
1366

1367
  for (int i = 0; i < sizeof(dirName) / sizeof(dirName[0]); i++) {
111,400,710✔
1368
    int64_t size = {0};
89,120,568✔
1369
    (void)snprintf(path + offset, TSDB_FILENAME_LEN - offset, "%s%s", TD_DIRSEP, dirName[i]);
89,120,568✔
1370
    code = taosGetDirSize(path, &size);
89,120,568✔
1371
    if (code != 0) {
89,120,568✔
1372
      uWarn("vnode %d get dir %s %s size failed since %s", TD_VID(pVnode), path, dirName[i], tstrerror(code));
7,797!
1373
    }
1374
    path[offset] = 0;
89,120,568✔
1375
    dirSize[i] = size;
89,120,568✔
1376
  }
1377

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

1394
  code = tsdbGetFsSize(pVnodeObj->pTsdb, pInfo);
22,280,142✔
1395
_exit:
22,280,142✔
1396
  return code;
22,280,142✔
1397
}
1398

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

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

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

1431
  return 0;
×
1432
}
1433

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

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

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

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

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

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

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

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

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