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

taosdata / TDengine / #4899

27 Dec 2025 07:32AM UTC coverage: 65.534% (+0.5%) from 65.061%
#4899

push

travis-ci

web-flow
test: remove semaphore test (#34071)

189567 of 289265 relevant lines covered (65.53%)

114701701.06 hits per line

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

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

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

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

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

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

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

36
int32_t fillTableColCmpr(SMetaReader *reader, SSchemaExt *pExt, int32_t numOfCol) {
17,880,780✔
37
  int8_t tblType = reader->me.type;
17,880,780✔
38
  if (withExtSchema(tblType)) {
17,881,732✔
39
    SColCmprWrapper *p = &(reader->me.colCmpr);
17,705,038✔
40
    if (numOfCol != p->nCols) {
17,705,038✔
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++) {
207,156,178✔
45
      SColCmpr *pCmpr = &p->pColCmpr[i];
189,451,654✔
46
      pExt[i].colId = pCmpr->id;
189,452,170✔
47
      pExt[i].compress = pCmpr->alg;
189,450,692✔
48
    }
49
  }
50
  return 0;
17,881,385✔
51
}
52

53
void vnodePrintTableMeta(STableMetaRsp *pMeta) {
17,880,780✔
54
  if (!(qDebugFlag & DEBUG_DEBUG)) {
17,880,780✔
55
    return;
95,158✔
56
  }
57

58
  qDebug("tbName:%s", pMeta->tbName);
17,785,622✔
59
  qDebug("stbName:%s", pMeta->stbName);
17,786,574✔
60
  qDebug("dbFName:%s", pMeta->dbFName);
17,786,574✔
61
  qDebug("dbId:%" PRId64, pMeta->dbId);
17,786,574✔
62
  qDebug("numOfTags:%d", pMeta->numOfTags);
17,786,406✔
63
  qDebug("numOfColumns:%d", pMeta->numOfColumns);
17,786,574✔
64
  qDebug("precision:%d", pMeta->precision);
17,786,574✔
65
  qDebug("tableType:%d", pMeta->tableType);
17,786,574✔
66
  qDebug("sversion:%d", pMeta->sversion);
17,786,574✔
67
  qDebug("tversion:%d", pMeta->tversion);
17,786,574✔
68
  qDebug("suid:%" PRIu64, pMeta->suid);
17,786,574✔
69
  qDebug("tuid:%" PRIu64, pMeta->tuid);
17,786,574✔
70
  qDebug("vgId:%d", pMeta->vgId);
17,786,574✔
71
  qDebug("sysInfo:%d", pMeta->sysInfo);
17,786,574✔
72
  if (pMeta->pSchemas) {
17,786,574✔
73
    for (int32_t i = 0; i < (pMeta->numOfColumns + pMeta->numOfTags); ++i) {
243,388,094✔
74
      SSchema *pSchema = pMeta->pSchemas + i;
225,601,520✔
75
      qDebug("%d col/tag: type:%d, flags:%d, colId:%d, bytes:%d, name:%s", i, pSchema->type, pSchema->flags,
225,601,520✔
76
             pSchema->colId, pSchema->bytes, pSchema->name);
77
    }
78
  }
79
}
80

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

103
int32_t vnodeGetTableMeta(SVnode *pVnode, SRpcMsg *pMsg, bool direct) {
25,408,744✔
104
  STableInfoReq  infoReq = {0};
25,408,744✔
105
  STableMetaRsp  metaRsp = {0};
25,408,565✔
106
  SMetaReader    mer1 = {0};
25,407,445✔
107
  SMetaReader    mer2 = {0};
25,408,651✔
108
  char           tableFName[TSDB_TABLE_FNAME_LEN];
25,405,999✔
109
  bool           reqTbUid = false;
25,409,256✔
110
  SRpcMsg        rpcMsg = {0};
25,409,256✔
111
  int32_t        code = 0;
25,409,603✔
112
  int32_t        rspLen = 0;
25,409,603✔
113
  void          *pRsp = NULL;
25,409,603✔
114
  SSchemaWrapper schema = {0};
25,409,603✔
115
  SSchemaWrapper schemaTag = {0};
25,409,603✔
116
  uint8_t        autoCreateCtb = 0;
25,409,603✔
117

118
  // decode req
119
  if (tDeserializeSTableInfoReq(pMsg->pCont, pMsg->contLen, &infoReq) != 0) {
25,409,603✔
120
    code = terrno;
×
121
    goto _exit4;
×
122
  }
123
  autoCreateCtb = infoReq.autoCreateCtb;
25,408,744✔
124

125
  if (infoReq.option == REQ_OPT_TBUID) reqTbUid = true;
25,408,744✔
126
  metaRsp.dbId = pVnode->config.dbId;
25,408,744✔
127
  tstrncpy(metaRsp.tbName, infoReq.tbName, TSDB_TABLE_NAME_LEN);
25,407,960✔
128
  (void)memcpy(metaRsp.dbFName, infoReq.dbFName, sizeof(metaRsp.dbFName));
25,407,960✔
129

130
  if (!reqTbUid) {
25,407,960✔
131
    (void)tsnprintf(tableFName, TSDB_TABLE_FNAME_LEN, "%s.%s", infoReq.dbFName, infoReq.tbName);
25,189,966✔
132
    if (pVnode->mounted) tTrimMountPrefix(tableFName);
25,190,657✔
133
    code = vnodeValidateTableHash(pVnode, tableFName);
25,190,313✔
134
    if (code) {
25,189,969✔
135
      goto _exit4;
×
136
    }
137
  }
138

139
  // query meta
140
  metaReaderDoInit(&mer1, pVnode->pMeta, META_READER_LOCK);
25,407,963✔
141
  if (reqTbUid) {
25,409,603✔
142
    SET_ERRNO(0);
218,943✔
143
    uint64_t tbUid = taosStr2UInt64(infoReq.tbName, NULL, 10);
218,943✔
144
    if (ERRNO == ERANGE || tbUid == 0) {
218,775✔
145
      code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
146
      goto _exit3;
×
147
    }
148
    SMetaReader mr3 = {0};
218,775✔
149
    metaReaderDoInit(&mr3, ((SVnode *)pVnode)->pMeta, META_READER_NOLOCK);
218,775✔
150
    if ((code = metaReaderGetTableEntryByUid(&mr3, tbUid)) < 0) {
218,943✔
151
      metaReaderClear(&mr3);
83,757✔
152
      TAOS_CHECK_GOTO(code, NULL, _exit3);
83,757✔
153
    }
154
    tstrncpy(metaRsp.tbName, mr3.me.name, TSDB_TABLE_NAME_LEN);
135,186✔
155
    metaReaderClear(&mr3);
135,186✔
156
    TAOS_CHECK_GOTO(metaGetTableEntryByName(&mer1, metaRsp.tbName), NULL, _exit3);
135,018✔
157
  } else if (metaGetTableEntryByName(&mer1, infoReq.tbName) < 0) {
25,190,660✔
158
    code = terrno;
7,444,114✔
159
    goto _exit3;
7,443,770✔
160
  }
161

162
  metaRsp.tableType = mer1.me.type;
17,880,873✔
163
  metaRsp.vgId = TD_VID(pVnode);
17,880,873✔
164
  metaRsp.tuid = mer1.me.uid;
17,880,264✔
165

166
  switch (mer1.me.type) {
17,880,264✔
167
    case TSDB_SUPER_TABLE: {
3,334,010✔
168
      (void)strcpy(metaRsp.stbName, mer1.me.name);
3,334,010✔
169
      schema = mer1.me.stbEntry.schemaRow;
3,334,010✔
170
      schemaTag = mer1.me.stbEntry.schemaTag;
3,334,010✔
171
      metaRsp.suid = mer1.me.uid;
3,334,010✔
172
      metaRsp.virtualStb = TABLE_IS_VIRTUAL(mer1.me.flags);
3,334,010✔
173
      break;
3,334,010✔
174
    }
175
    case TSDB_CHILD_TABLE:
11,759,520✔
176
    case TSDB_VIRTUAL_CHILD_TABLE:{
177
      metaReaderDoInit(&mer2, pVnode->pMeta, META_READER_NOLOCK);
11,759,520✔
178
      if (metaReaderGetTableEntryByUid(&mer2, mer1.me.ctbEntry.suid) < 0) goto _exit2;
11,759,868✔
179

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

197
  metaRsp.numOfTags = schemaTag.nCols;
17,880,265✔
198
  metaRsp.numOfColumns = schema.nCols;
17,880,265✔
199
  metaRsp.precision = pVnode->config.tsdbCfg.precision;
17,880,265✔
200
  metaRsp.sversion = schema.version;
17,880,265✔
201
  metaRsp.tversion = schemaTag.version;
17,880,265✔
202
  metaRsp.pSchemas = (SSchema *)taosMemoryMalloc(sizeof(SSchema) * (metaRsp.numOfColumns + metaRsp.numOfTags));
17,880,265✔
203
  metaRsp.pSchemaExt = (SSchemaExt *)taosMemoryCalloc(metaRsp.numOfColumns, sizeof(SSchemaExt));
17,879,136✔
204
  if (NULL == metaRsp.pSchemas || NULL == metaRsp.pSchemaExt) {
17,880,780✔
205
    code = terrno;
605✔
206
    goto _exit;
×
207
  }
208
  (void)memcpy(metaRsp.pSchemas, schema.pSchema, sizeof(SSchema) * schema.nCols);
17,880,175✔
209
  if (schemaTag.nCols) {
17,880,175✔
210
    (void)memcpy(metaRsp.pSchemas + schema.nCols, schemaTag.pSchema, sizeof(SSchema) * schemaTag.nCols);
15,094,046✔
211
  }
212
  if (metaRsp.pSchemaExt) {
17,880,175✔
213
    SMetaReader *pReader = mer1.me.type == TSDB_CHILD_TABLE ? &mer2 : &mer1;
17,881,127✔
214
    code = fillTableColCmpr(pReader, metaRsp.pSchemaExt, metaRsp.numOfColumns);
17,881,127✔
215
    if (code < 0) {
17,881,732✔
216
      goto _exit;
×
217
    }
218
    for (int32_t i = 0; i < metaRsp.numOfColumns && pReader->me.pExtSchemas; i++) {
42,633,754✔
219
      metaRsp.pSchemaExt[i].typeMod = pReader->me.pExtSchemas[i].typeMod;
24,752,022✔
220
    }
221
  } else {
222
    code = TSDB_CODE_OUT_OF_MEMORY;
×
223
    goto _exit;
×
224
  }
225
  if (hasRefCol(mer1.me.type)) {
17,882,337✔
226
    metaRsp.rversion = mer1.me.colRef.version;
175,137✔
227
    metaRsp.pColRefs = (SColRef*)taosMemoryMalloc(sizeof(SColRef) * metaRsp.numOfColumns);
175,137✔
228
    if (metaRsp.pColRefs) {
176,694✔
229
      code = fillTableColRef(&mer1, metaRsp.pColRefs, metaRsp.numOfColumns);
176,694✔
230
      if (code < 0) {
176,694✔
231
        goto _exit;
×
232
      }
233
    }
234
    metaRsp.numOfColRefs = metaRsp.numOfColumns;
176,694✔
235
  } else {
236
    metaRsp.pColRefs = NULL;
17,704,086✔
237
    metaRsp.numOfColRefs = 0;
17,704,086✔
238
  }
239

240
  vnodePrintTableMeta(&metaRsp);
17,880,780✔
241

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

249
  if (direct) {
17,881,732✔
250
    pRsp = rpcMallocCont(rspLen);
82,012✔
251
  } else {
252
    pRsp = taosMemoryCalloc(1, rspLen);
17,799,720✔
253
  }
254

255
  if (pRsp == NULL) {
17,880,519✔
256
    code = terrno;
×
257
    goto _exit;
×
258
  }
259

260
  rspLen = tSerializeSTableMetaRsp(pRsp, rspLen, &metaRsp);
17,880,519✔
261
  if (rspLen < 0) {
17,880,522✔
262
    code = terrno;
×
263
    goto _exit;
×
264
  }
265

266
_exit:
17,879,570✔
267
  taosMemoryFree(metaRsp.pColRefs);
17,881,729✔
268
  taosMemoryFree(metaRsp.pSchemas);
17,880,519✔
269
  taosMemoryFree(metaRsp.pSchemaExt);
17,881,127✔
270
_exit2:
17,879,309✔
271
  metaReaderClear(&mer2);
17,881,729✔
272
_exit3:
25,407,612✔
273
  metaReaderClear(&mer1);
25,409,603✔
274
_exit4:
25,408,651✔
275
  rpcMsg.info = pMsg->info;
25,408,654✔
276
  rpcMsg.pCont = pRsp;
25,409,259✔
277
  rpcMsg.contLen = rspLen;
25,409,259✔
278
  rpcMsg.code = code;
25,409,259✔
279
  rpcMsg.msgType = pMsg->msgType;
25,409,259✔
280

281
  if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST && autoCreateCtb == 1) {
25,406,234✔
282
    code = TSDB_CODE_SUCCESS;
4,622,886✔
283
  }
284

285
  if (code) {
25,406,234✔
286
    qError("vgId:%d, get table %s meta with %" PRIu8 " failed cause of %s", pVnode->config.vgId, infoReq.tbName,
2,904,641✔
287
           infoReq.option, tstrerror(code));
288
  }
289

290
  if (direct) {
25,408,998✔
291
    tmsgSendRsp(&rpcMsg);
764,451✔
292
  } else {
293
    *pMsg = rpcMsg;
24,644,547✔
294
  }
295

296
  return code;
25,408,650✔
297
}
298

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

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

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

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

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

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

336
  cfgRsp.tableType = mer1.me.type;
48,362✔
337

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

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

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

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

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

402
  if (withExtSchema(cfgRsp.tableType)) {
48,362✔
403
    for (int32_t i = 0; i < cfgRsp.numOfColumns; i++) {
189,919✔
404
      SColCmpr   *pCmpr = &pColCmpr->pColCmpr[i];
144,719✔
405
      SSchemaExt *pSchExt = cfgRsp.pSchemaExt + i;
144,719✔
406
      pSchExt->colId = pCmpr->id;
144,719✔
407
      pSchExt->compress = pCmpr->alg;
144,719✔
408
      if (pReader->me.pExtSchemas)
144,719✔
409
        pSchExt->typeMod = pReader->me.pExtSchemas[i].typeMod;
23,089✔
410
      else
411
        pSchExt->typeMod = 0;
121,630✔
412
    }
413
  }
414

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

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

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

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

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

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

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

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

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

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

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

485
int32_t vnodeGetBatchMeta(SVnode *pVnode, SRpcMsg *pMsg) {
20,429,114✔
486
  int32_t      code = 0;
20,429,114✔
487
  int32_t      rspSize = 0;
20,429,114✔
488
  SBatchReq    batchReq = {0};
20,429,114✔
489
  SBatchMsg   *req = NULL;
20,429,461✔
490
  SBatchRspMsg rsp = {0};
20,429,461✔
491
  SBatchRsp    batchRsp = {0};
20,428,420✔
492
  SRpcMsg      reqMsg = *pMsg;
20,427,815✔
493
  SRpcMsg      rspMsg = {0};
20,427,815✔
494
  void        *pRsp = NULL;
20,429,458✔
495

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

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

509
  batchRsp.pRsps = taosArrayInit(msgNum, sizeof(SBatchRspMsg));
20,430,149✔
510
  if (NULL == batchRsp.pRsps) {
20,429,290✔
511
    code = terrno;
×
512
    qError("taosArrayInit %d SBatchRspMsg failed", msgNum);
×
513
    goto _exit;
×
514
  }
515

516
  for (int32_t i = 0; i < msgNum; ++i) {
45,162,128✔
517
    req = taosArrayGet(batchReq.pMsgs, i);
24,733,544✔
518
    if (req == NULL) {
24,734,056✔
519
      code = terrno;
×
520
      goto _exit;
×
521
    }
522

523
    reqMsg.msgType = req->msgType;
24,734,056✔
524
    reqMsg.pCont = req->msg;
24,734,637✔
525
    reqMsg.contLen = req->msgLen;
24,733,712✔
526

527
    switch (req->msgType) {
24,733,197✔
528
      case TDMT_VND_TABLE_META:
24,425,518✔
529
        // error code has been set into reqMsg, no need to handle it here.
530
        if (TSDB_CODE_SUCCESS != vnodeGetTableMeta(pVnode, &reqMsg, false)) {
24,425,518✔
531
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
2,138,789✔
532
        }
533
        break;
24,425,259✔
534
      case TDMT_VND_TABLE_NAME:
218,775✔
535
        // error code has been set into reqMsg, no need to handle it here.
536
        if (TSDB_CODE_SUCCESS != vnodeGetTableMeta(pVnode, &reqMsg, false)) {
218,775✔
537
          qWarn("vnodeGetBatchName failed, msgType:%d", req->msgType);
83,757✔
538
        }
539
        break;
218,943✔
540
      case TDMT_VND_TABLE_CFG:
48,362✔
541
        // error code has been set into reqMsg, no need to handle it here.
542
        if (TSDB_CODE_SUCCESS != vnodeGetTableCfg(pVnode, &reqMsg, false)) {
48,362✔
543
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
×
544
        }
545
        break;
48,362✔
546
      case TDMT_VND_VSUBTABLES_META:
×
547
        // error code has been set into reqMsg, no need to handle it here.
548
        if (TSDB_CODE_SUCCESS != vnodeGetVSubtablesMeta(pVnode, &reqMsg)) {
×
549
          qWarn("vnodeGetVSubtablesMeta failed, msgType:%d", req->msgType);
×
550
        }
551
        break;
×
552
      case TDMT_VND_VSTB_REF_DBS:
40,889✔
553
        // error code has been set into reqMsg, no need to handle it here.
554
        if (TSDB_CODE_SUCCESS != vnodeGetVStbRefDbs(pVnode, &reqMsg)) {
40,889✔
555
          qWarn("vnodeGetVStbRefDbs failed, msgType:%d", req->msgType);
×
556
        }
557
        break;
41,467✔
558
      default:
×
559
        qError("invalid req msgType %d", req->msgType);
×
560
        reqMsg.code = TSDB_CODE_INVALID_MSG;
×
561
        reqMsg.pCont = NULL;
×
562
        reqMsg.contLen = 0;
×
563
        break;
×
564
    }
565

566
    rsp.msgIdx = req->msgIdx;
24,734,031✔
567
    rsp.reqType = reqMsg.msgType;
24,734,031✔
568
    rsp.msgLen = reqMsg.contLen;
24,734,031✔
569
    rsp.rspCode = reqMsg.code;
24,734,031✔
570
    rsp.msg = reqMsg.pCont;
24,734,031✔
571

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

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

597
_exit:
20,429,376✔
598

599
  rspMsg.info = pMsg->info;
20,429,981✔
600
  rspMsg.pCont = pRsp;
20,430,149✔
601
  rspMsg.contLen = rspSize;
20,430,149✔
602
  rspMsg.code = code;
20,430,149✔
603
  rspMsg.msgType = pMsg->msgType;
20,430,149✔
604

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

609
  taosArrayDestroyEx(batchReq.pMsgs, tFreeSBatchReqMsg);
20,428,939✔
610
  taosArrayDestroyEx(batchRsp.pRsps, tFreeSBatchRspMsg);
20,429,032✔
611

612
  tmsgSendRsp(&rspMsg);
20,428,939✔
613

614
  return code;
20,429,805✔
615
}
616

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

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

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

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

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

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

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

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

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

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

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

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

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

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

690
      refColsNum++;
×
691
    }
692

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

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

701
_return:
×
702

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

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

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

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

725
  pList = taosArrayInit(10, sizeof(uint64_t));
40,889✔
726
  QUERY_CHECK_NULL(pList, code, line, _return, terrno);
41,467✔
727

728
  *ppRes = taosArrayInit(10, POINTER_BYTES);
41,467✔
729
  QUERY_CHECK_NULL(*ppRes, code, line, _return, terrno)
40,882✔
730
  
731
  // lookup in cache
732
  code = pHandle->api.metaFn.metaGetCachedRefDbs(pHandle->vnode, suid, *ppRes);
41,467✔
733
  QUERY_CHECK_CODE(code, line, _return);
40,889✔
734

735
  if (taosArrayGetSize(*ppRes) > 0) {
40,889✔
736
    // found in cache
737
    goto _return;
23,138✔
738
  } else {
739
    code = pHandle->api.metaFn.getChildTableList(pHandle->vnode, suid, pList);
17,751✔
740
    QUERY_CHECK_CODE(code, line, _return);
18,329✔
741

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

746
    for (int32_t i = 0; i < num; ++i) {
56,186✔
747
      uint64_t* id = taosArrayGet(pList, i);
37,857✔
748
      QUERY_CHECK_NULL(id, code, line, _return, terrno);
37,857✔
749

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

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

756
      for (int32_t j = 0; j < mr.me.colRef.nCols; j++) {
303,231✔
757
        if (mr.me.colRef.pColRef[j].hasRef) {
265,374✔
758
          if (NULL == tSimpleHashGet(pDbNameHash, mr.me.colRef.pColRef[j].refDbName, strlen(mr.me.colRef.pColRef[j].refDbName))) {
188,961✔
759
            char *refDbName = taosStrdup(mr.me.colRef.pColRef[j].refDbName);
12,533✔
760
            QUERY_CHECK_NULL(refDbName, code, line, _return, terrno);
12,533✔
761

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

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

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

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

778
_return:
41,467✔
779

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

784
  taosArrayDestroy(pList);
41,467✔
785
  tSimpleHashCleanup(pDbNameHash);
41,467✔
786

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

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

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

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

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

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

834
_return:
×
835

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

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

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

850
  //tmsgSendRsp(&rspMsg);
851

852
  return code;
×
853
}
854

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

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

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

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

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

897
_return:
41,467✔
898

899
  rspMsg.info = pMsg->info;
41,467✔
900
  rspMsg.pCont = pRsp;
41,467✔
901
  rspMsg.contLen = rspSize;
41,467✔
902
  rspMsg.code = code;
41,467✔
903
  rspMsg.msgType = pMsg->msgType;
41,467✔
904

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

909
  *pMsg = rspMsg;
40,882✔
910

911
  tDestroySVStbRefDbsRsp(&rsp);
41,467✔
912

913
  return code;
41,467✔
914
}
915

916
static int32_t vnodeGetCompStorage(SVnode *pVnode, int64_t *output) {
107,517,763✔
917
  int32_t code = 0;
107,517,763✔
918
#ifdef TD_ENTERPRISE
919
  int32_t now = taosGetTimestampSec();
107,517,763✔
920
  if (llabs(now - pVnode->config.vndStats.storageLastUpd) >= 30) {
107,517,763✔
921
    pVnode->config.vndStats.storageLastUpd = now;
5,823,121✔
922

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

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

948
    if (pVnode->inUse) {
107,517,763✔
949
      *bufferSegmentUsed = pVnode->inUse->size;
107,505,435✔
950
    }
951
    *bufferSegmentSize = pVnode->config.szBuf / VNODE_BUFPOOL_SEGMENTS;
107,517,763✔
952

953
    (void)taosThreadMutexUnlock(&pVnode->mutex);
107,517,763✔
954
  }
955
}
107,517,763✔
956

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

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

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

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

1013
  if (dbname) {
6,396,153✔
1014
    *dbname = pConf->dbname;
6,381,307✔
1015
  }
1016

1017
  if (vgId) {
6,396,153✔
1018
    *vgId = TD_VID(pVnodeObj);
5,831,015✔
1019
  }
1020

1021
  if (numOfTables) {
6,396,758✔
1022
    *numOfTables = pConf->vndStats.numOfNTables + pConf->vndStats.numOfCTables +
16,719✔
1023
                   pConf->vndStats.numOfVTables + pConf->vndStats.numOfVCTables;
8,374✔
1024
  }
1025

1026
  if (numOfNormalTables) {
6,396,787✔
1027
    *numOfNormalTables = pConf->vndStats.numOfNTables +
13,002✔
1028
                         pConf->vndStats.numOfVTables;
6,501✔
1029
  }
1030
}
6,396,787✔
1031

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

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

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

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

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

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

1079
  while (1) {
390,885,670✔
1080
    tb_uid_t id = metaCtbCursorNext(pCur);
470,733,285✔
1081
    if (id == 0) {
470,727,800✔
1082
      break;
79,869,280✔
1083
    }
1084
    qTrace("vnodeGetCtbIdList: got ctb id %" PRId64 " for suid %" PRId64, id, suid);
390,858,520✔
1085
    if (NULL == taosArrayPush(list, &id)) {
390,857,102✔
1086
      qError("taosArrayPush failed");
×
1087
      code = terrno;
×
1088
      goto _exit;
×
1089
    }
1090
  }
1091

1092
_exit:
79,829,237✔
1093
  metaCloseCtbCursor(pCur);
79,829,237✔
1094
  return code;
79,827,017✔
1095
}
1096

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

1104
  while (1) {
47,402,586✔
1105
    tb_uid_t id = metaStbCursorNext(pCur);
101,441,934✔
1106
    if (id == 0) {
101,440,235✔
1107
      break;
54,043,778✔
1108
    }
1109

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

1117
_exit:
54,041,671✔
1118
  metaCloseStbCursor(pCur);
54,041,671✔
1119
  return code;
54,035,938✔
1120
}
1121

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

1130
  while (1) {
×
1131
    tb_uid_t id = metaStbCursorNext(pCur);
×
1132
    if (id == 0) {
×
1133
      break;
×
1134
    }
1135

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

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

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

1152
int32_t vnodeGetCtbNum(SVnode *pVnode, int64_t suid, int64_t *num) {
4,074,817✔
1153
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, suid, 0);
4,074,817✔
1154
  if (!pCur) {
4,075,191✔
1155
    return terrno;
×
1156
  }
1157

1158
  *num = 0;
4,075,191✔
1159
  while (1) {
2,393,263✔
1160
    tb_uid_t id = metaCtbCursorNext(pCur);
6,468,454✔
1161
    if (id == 0) {
6,468,120✔
1162
      break;
4,075,792✔
1163
    }
1164

1165
    ++(*num);
2,392,328✔
1166
  }
1167

1168
  metaCloseCtbCursor(pCur);
4,075,792✔
1169
  return TSDB_CODE_SUCCESS;
4,075,792✔
1170
}
1171

1172
int32_t vnodeGetStbColumnNum(SVnode *pVnode, tb_uid_t suid, int *num) {
4,075,505✔
1173
  SSchemaWrapper *pSW = metaGetTableSchema(pVnode->pMeta, suid, -1, 0, NULL, 0);
4,075,505✔
1174
  if (pSW) {
4,075,170✔
1175
    *num = pSW->nCols;
4,075,047✔
1176
    tDeleteSchemaWrapper(pSW);
1177
  } else {
1178
    *num = 2;
123✔
1179
  }
1180

1181
  return TSDB_CODE_SUCCESS;
4,075,505✔
1182
}
1183

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

1188
  int32_t code = metaReaderGetTableEntryByUid(&mr, suid);
4,075,761✔
1189
  if (code == TSDB_CODE_SUCCESS) {
4,075,742✔
1190
    if (keep) *keep = mr.me.stbEntry.keep;
4,075,619✔
1191
    if (flags) *flags = mr.me.flags;
4,075,863✔
1192
  } else {
1193
    if (keep) *keep = 0;
123✔
1194
    if (flags) *flags = 0;
123✔
1195
  }
1196

1197
  metaReaderClear(&mr);
4,075,456✔
1198
  return TSDB_CODE_SUCCESS;
4,075,731✔
1199
}
1200

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

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

1233
  if (!(dbName = strchr(pVnode->config.dbname, '.'))) return 0;
54,034,198✔
1234
  if (0 == strncmp(++dbName, "log", TSDB_DB_NAME_LEN)) {
54,032,006✔
1235
    tbNum = tkLogStbNum;
×
1236
    pTbArr = (const char **)&tkLogStb;
×
1237
  } else if (0 == strncmp(dbName, "audit", TSDB_DB_NAME_LEN)) {
54,033,224✔
1238
    tbNum = tkAuditStbNum;
×
1239
    pTbArr = (const char **)&tkAuditStb;
×
1240
  }
1241
  if (tbNum && pTbArr) {
54,033,224✔
1242
    *tbSize = metaSizeOfTbFilterCache(pVnode->pMeta, 0);
×
1243
    if (*tbSize < tbNum) {
×
1244
      for (int32_t i = 0; i < tbNum; ++i) {
×
1245
        tb_uid_t suid = metaGetTableEntryUidByName(pVnode->pMeta, pTbArr[i]);
×
1246
        if (suid != 0) {
×
1247
          code = metaPutTbToFilterCache(pVnode->pMeta, &suid, 0);
×
1248
          if (TSDB_CODE_SUCCESS != code) {
×
1249
            return code;
×
1250
          }
1251
        }
1252
      }
1253
      *tbSize = metaSizeOfTbFilterCache(pVnode->pMeta, 0);
×
1254
    }
1255
  }
1256

1257
  return code;
54,031,397✔
1258
}
1259
#endif
1260

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

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

1270
int32_t vnodeGetTimeSeriesNum(SVnode *pVnode, int64_t *num) {
54,032,615✔
1271
  SArray *suidList = NULL;
54,032,615✔
1272

1273
  if (!(suidList = taosArrayInit(1, sizeof(tb_uid_t)))) {
54,032,615✔
1274
    return terrno;
×
1275
  }
1276

1277
  int32_t tbFilterSize = 0;
54,032,615✔
1278
  int32_t code = TSDB_CODE_SUCCESS;
54,032,615✔
1279
#ifdef TD_ENTERPRISE
1280
  code = vnodeGetTimeSeriesBlackList(pVnode, &tbFilterSize);
54,032,615✔
1281
  if (TSDB_CODE_SUCCESS != code) {
54,031,397✔
1282
    goto _exit;
×
1283
  }
1284
#endif
1285

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

1293
  *num = 0;
54,030,179✔
1294
  int64_t arrSize = taosArrayGetSize(suidList);
54,031,397✔
1295
  for (int64_t i = 0; i < arrSize; ++i) {
101,426,923✔
1296
    tb_uid_t suid = *(tb_uid_t *)taosArrayGet(suidList, i);
47,388,218✔
1297

1298
    int64_t ctbNum = 0;
47,391,263✔
1299
    int32_t numOfCols = 0;
47,391,263✔
1300
    int8_t  flags = 0;
47,388,218✔
1301
    code = metaGetStbStats(pVnode, suid, &ctbNum, &numOfCols, &flags);
47,392,846✔
1302
    if (TSDB_CODE_SUCCESS != code) {
47,400,763✔
1303
      goto _exit;
×
1304
    }
1305
    if (!TABLE_IS_VIRTUAL(flags)) {
47,400,763✔
1306
      *num += ctbNum * (numOfCols - 1);
47,157,895✔
1307
    }
1308
  }
1309

1310
_exit:
54,039,314✔
1311
  taosArrayDestroy(suidList);
54,038,705✔
1312
  return TSDB_CODE_SUCCESS;
54,034,442✔
1313
}
1314

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

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

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

1335
    *num += ctbNum;
×
1336
  }
1337

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

1342
void *vnodeGetIdx(void *pVnode) {
3,778,401✔
1343
  if (pVnode == NULL) {
3,778,401✔
1344
    return NULL;
×
1345
  }
1346

1347
  return metaGetIdx(((SVnode *)pVnode)->pMeta);
3,778,401✔
1348
}
1349

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

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

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

1365
  char   *dirName[] = {VNODE_TSDB_DIR, VNODE_WAL_DIR, VNODE_META_DIR, VNODE_TSDB_CACHE_DIR};
5,828,553✔
1366
  int64_t dirSize[4];
5,828,095✔
1367

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

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

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

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

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

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

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

1435
  return 0;
×
1436
}
1437

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

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

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

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

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

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

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

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

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

© 2026 Coveralls, Inc