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

taosdata / TDengine / #5006

29 Mar 2026 04:32AM UTC coverage: 72.274% (+0.1%) from 72.152%
#5006

push

travis-ci

web-flow
refactor: do some internal refactor for TDgpt. (#34955)

253711 of 351039 relevant lines covered (72.27%)

131490495.89 hits per line

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

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

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

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

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

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

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

36
int32_t fillTableColCmpr(SMetaReader *reader, SSchemaExt *pExt, int32_t numOfCol) {
20,720,212✔
37
  int8_t tblType = reader->me.type;
20,720,212✔
38
  if (withExtSchema(tblType)) {
20,720,212✔
39
    SColCmprWrapper *p = &(reader->me.colCmpr);
20,365,627✔
40
    if (numOfCol != p->nCols) {
20,365,627✔
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++) {
239,186,906✔
45
      SColCmpr *pCmpr = &p->pColCmpr[i];
218,822,591✔
46
      pExt[i].colId = pCmpr->id;
218,822,591✔
47
      pExt[i].compress = pCmpr->alg;
218,823,903✔
48
    }
49
  }
50
  return 0;
20,720,212✔
51
}
52

53
void vnodeDebugTableMeta(STableMetaRsp *pMeta) {
20,720,212✔
54
  if (!(qDebugFlag & DEBUG_DEBUG)) {
20,720,212✔
55
    return;
375,647✔
56
  }
57

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

81
int32_t fillTableColRef(SMetaReader *reader, SColRef *pRef, int32_t numOfCol) {
354,585✔
82
  int8_t tblType = reader->me.type;
354,585✔
83
  if (hasRefCol(tblType)) {
354,585✔
84
    SColRefWrapper *p = &(reader->me.colRef);
354,585✔
85
    if (numOfCol != p->nCols) {
354,585✔
86
      vError("fillTableColRef table type:%d, col num:%d, col cmpr num:%d mismatch", tblType, numOfCol, p->nCols);
×
87
      return TSDB_CODE_APP_ERROR;
×
88
    }
89
    for (int i = 0; i < p->nCols; i++) {
2,750,128✔
90
      SColRef *pColRef = &p->pColRef[i];
2,395,543✔
91
      pRef[i].hasRef = pColRef->hasRef;
2,395,543✔
92
      pRef[i].id = pColRef->id;
2,395,543✔
93
      if(pRef[i].hasRef) {
2,395,543✔
94
        tstrncpy(pRef[i].refDbName, pColRef->refDbName, TSDB_DB_NAME_LEN);
1,682,294✔
95
        tstrncpy(pRef[i].refTableName, pColRef->refTableName, TSDB_TABLE_NAME_LEN);
1,682,294✔
96
        tstrncpy(pRef[i].refColName, pColRef->refColName, TSDB_COL_NAME_LEN);
1,682,294✔
97
      }
98
    }
99
  }
100
  return 0;
354,585✔
101
}
102

103
int32_t fillTableTagRef(SMetaReader *reader, SColRef *pRef, int32_t numOfTagRefs) {
×
104
  int8_t tblType = reader->me.type;
×
105
  if (hasRefCol(tblType)) {
×
106
    SColRefWrapper *p = &(reader->me.colRef);
×
107
    if (numOfTagRefs != p->nTagRefs) {
×
108
      vError("fillTableTagRef table type:%d, tag ref num:%d, expected:%d mismatch", tblType, numOfTagRefs, p->nTagRefs);
×
109
      return TSDB_CODE_APP_ERROR;
×
110
    }
111
    for (int i = 0; i < p->nTagRefs; i++) {
×
112
      SColRef *pTagRef = &p->pTagRef[i];
×
113
      pRef[i].hasRef = pTagRef->hasRef;
×
114
      pRef[i].id = pTagRef->id;
×
115
      if (pRef[i].hasRef) {
×
116
        tstrncpy(pRef[i].refDbName, pTagRef->refDbName, TSDB_DB_NAME_LEN);
×
117
        tstrncpy(pRef[i].refTableName, pTagRef->refTableName, TSDB_TABLE_NAME_LEN);
×
118
        tstrncpy(pRef[i].refColName, pTagRef->refColName, TSDB_COL_NAME_LEN);
×
119
      }
120
    }
121
  }
122
  return 0;
×
123
}
124

125
int32_t vnodeGetTableMeta(SVnode *pVnode, SRpcMsg *pMsg, bool direct) {
29,732,356✔
126
  STableInfoReq  infoReq = {0};
29,732,356✔
127
  STableMetaRsp  metaRsp = {0};
29,733,335✔
128
  SMetaReader    mer1 = {0};
29,733,335✔
129
  SMetaReader    mer2 = {0};
29,731,367✔
130
  char           tableFName[TSDB_TABLE_FNAME_LEN];
29,728,382✔
131
  bool           reqTbUid = false;
29,732,679✔
132
  SRpcMsg        rpcMsg = {0};
29,732,679✔
133
  int32_t        code = 0;
29,732,679✔
134
  int32_t        rspLen = 0;
29,732,679✔
135
  void          *pRsp = NULL;
29,732,679✔
136
  SSchemaWrapper schema = {0};
29,732,679✔
137
  SSchemaWrapper schemaTag = {0};
29,732,679✔
138
  uint8_t        autoCreateCtb = 0;
29,732,679✔
139

140
  // decode req
141
  if (tDeserializeSTableInfoReq(pMsg->pCont, pMsg->contLen, &infoReq) != 0) {
29,732,679✔
142
    code = terrno;
×
143
    goto _exit4;
×
144
  }
145
  autoCreateCtb = infoReq.autoCreateCtb;
29,733,991✔
146

147
  if (infoReq.option == REQ_OPT_TBUID) reqTbUid = true;
29,733,991✔
148
  metaRsp.dbId = pVnode->config.dbId;
29,733,991✔
149
  tstrncpy(metaRsp.tbName, infoReq.tbName, TSDB_TABLE_NAME_LEN);
29,733,991✔
150
  (void)memcpy(metaRsp.dbFName, infoReq.dbFName, sizeof(metaRsp.dbFName));
29,733,791✔
151

152
  if (!reqTbUid) {
29,733,791✔
153
    (void)snprintf(tableFName, TSDB_TABLE_FNAME_LEN, "%s.%s", infoReq.dbFName, infoReq.tbName);
29,498,761✔
154
    if (pVnode->mounted) tTrimMountPrefix(tableFName);
29,498,761✔
155
    code = vnodeValidateTableHash(pVnode, tableFName);
29,498,761✔
156
    if (code) {
29,498,220✔
157
      goto _exit4;
×
158
    }
159
  }
160

161
  // query meta
162
  metaReaderDoInit(&mer1, pVnode->pMeta, META_READER_LOCK);
29,733,250✔
163
  if (reqTbUid) {
29,733,450✔
164
    SET_ERRNO(0);
235,230✔
165
    uint64_t tbUid = taosStr2UInt64(infoReq.tbName, NULL, 10);
235,030✔
166
    if (ERRNO == ERANGE || tbUid == 0) {
235,030✔
167
      code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
168
      goto _exit3;
×
169
    }
170
    SMetaReader mr3 = {0};
235,030✔
171
    metaReaderDoInit(&mr3, ((SVnode *)pVnode)->pMeta, META_READER_NOLOCK);
235,030✔
172
    if ((code = metaReaderGetTableEntryByUid(&mr3, tbUid)) < 0) {
235,030✔
173
      metaReaderClear(&mr3);
86,230✔
174
      TAOS_CHECK_GOTO(code, NULL, _exit3);
86,230✔
175
    }
176
    tstrncpy(metaRsp.tbName, mr3.me.name, TSDB_TABLE_NAME_LEN);
148,800✔
177
    metaReaderClear(&mr3);
148,800✔
178
    TAOS_CHECK_GOTO(metaGetTableEntryByName(&mer1, metaRsp.tbName), NULL, _exit3);
148,800✔
179
  } else if (metaGetTableEntryByName(&mer1, infoReq.tbName) < 0) {
29,498,220✔
180
    code = terrno;
8,926,629✔
181
    goto _exit3;
8,926,791✔
182
  }
183

184
  metaRsp.tableType = mer1.me.type;
20,720,212✔
185
  metaRsp.vgId = TD_VID(pVnode);
20,720,212✔
186
  metaRsp.tuid = mer1.me.uid;
20,720,212✔
187
  metaRsp.isAudit = pVnode->config.isAudit ? 1 : 0;
20,720,212✔
188

189
  switch (mer1.me.type) {
20,720,212✔
190
    case TSDB_SUPER_TABLE: {
3,750,801✔
191
      tstrncpy(metaRsp.stbName, mer1.me.name, sizeof(metaRsp.stbName));
3,750,801✔
192
      schema = mer1.me.stbEntry.schemaRow;
3,750,801✔
193
      schemaTag = mer1.me.stbEntry.schemaTag;
3,750,801✔
194
      metaRsp.suid = mer1.me.uid;
3,750,801✔
195
      metaRsp.virtualStb = TABLE_IS_VIRTUAL(mer1.me.flags);
3,750,801✔
196
      metaRsp.ownerId = mer1.me.stbEntry.ownerId;
3,750,801✔
197
      break;
3,750,801✔
198
    }
199
    case TSDB_CHILD_TABLE:
13,333,163✔
200
    case TSDB_VIRTUAL_CHILD_TABLE:{
201
      metaReaderDoInit(&mer2, pVnode->pMeta, META_READER_NOLOCK);
13,333,163✔
202
      if (metaReaderGetTableEntryByUid(&mer2, mer1.me.ctbEntry.suid) < 0) goto _exit2;
13,332,963✔
203

204
      tstrncpy(metaRsp.stbName, mer2.me.name, sizeof(metaRsp.stbName));
13,333,163✔
205
      metaRsp.suid = mer2.me.uid;
13,333,163✔
206
      metaRsp.ownerId = mer2.me.stbEntry.ownerId;  // child table inherits ownerId from stb
13,333,163✔
207
      schema = mer2.me.stbEntry.schemaRow;
13,333,163✔
208
      schemaTag = mer2.me.stbEntry.schemaTag;
13,333,163✔
209
      break;
13,333,163✔
210
    }
211
    case TSDB_NORMAL_TABLE:
3,636,248✔
212
    case TSDB_VIRTUAL_NORMAL_TABLE: {
213
      schema = mer1.me.ntbEntry.schemaRow;
3,636,248✔
214
      metaRsp.ownerId = mer1.me.ntbEntry.ownerId;
3,636,248✔
215
      break;
3,636,248✔
216
    }
217
    default: {
×
218
      vError("vnodeGetTableMeta get invalid table type:%d", mer1.me.type);
×
219
      goto _exit3;
×
220
    }
221
  }
222

223
  metaRsp.numOfTags = schemaTag.nCols;
20,720,212✔
224
  metaRsp.numOfColumns = schema.nCols;
20,720,212✔
225
  metaRsp.precision = pVnode->config.tsdbCfg.precision;
20,720,212✔
226
  metaRsp.sversion = schema.version;
20,720,212✔
227
  metaRsp.tversion = schemaTag.version;
20,720,212✔
228
  metaRsp.pSchemas = (SSchema *)taosMemoryMalloc(sizeof(SSchema) * (metaRsp.numOfColumns + metaRsp.numOfTags));
20,720,212✔
229
  metaRsp.pSchemaExt = (SSchemaExt *)taosMemoryCalloc(metaRsp.numOfColumns, sizeof(SSchemaExt));
20,718,244✔
230
  if (NULL == metaRsp.pSchemas || NULL == metaRsp.pSchemaExt) {
20,718,900✔
231
    code = terrno;
×
232
    goto _exit;
×
233
  }
234
  (void)memcpy(metaRsp.pSchemas, schema.pSchema, sizeof(SSchema) * schema.nCols);
20,720,212✔
235
  if (schemaTag.nCols) {
20,720,212✔
236
    (void)memcpy(metaRsp.pSchemas + schema.nCols, schemaTag.pSchema, sizeof(SSchema) * schemaTag.nCols);
17,083,964✔
237
  }
238
  if (metaRsp.pSchemaExt) {
20,720,212✔
239
    SMetaReader *pReader = mer1.me.type == TSDB_CHILD_TABLE ? &mer2 : &mer1;
20,720,212✔
240
    code = fillTableColCmpr(pReader, metaRsp.pSchemaExt, metaRsp.numOfColumns);
20,720,212✔
241
    if (code < 0) {
20,720,212✔
242
      goto _exit;
×
243
    }
244
    for (int32_t i = 0; i < metaRsp.numOfColumns && pReader->me.pExtSchemas; i++) {
49,315,697✔
245
      metaRsp.pSchemaExt[i].typeMod = pReader->me.pExtSchemas[i].typeMod;
28,595,485✔
246
    }
247
  } else {
248
    code = TSDB_CODE_OUT_OF_MEMORY;
×
249
    goto _exit;
×
250
  }
251
  if (hasRefCol(mer1.me.type)) {
20,720,212✔
252
    metaRsp.rversion = mer1.me.colRef.version;
353,273✔
253
    metaRsp.pColRefs = (SColRef*)taosMemoryMalloc(sizeof(SColRef) * metaRsp.numOfColumns);
353,273✔
254
    if (metaRsp.pColRefs) {
354,585✔
255
      code = fillTableColRef(&mer1, metaRsp.pColRefs, metaRsp.numOfColumns);
354,585✔
256
      if (code < 0) {
354,585✔
257
        goto _exit;
×
258
      }
259
    }
260
    metaRsp.numOfColRefs = metaRsp.numOfColumns;
354,585✔
261

262
    // Fill tag references
263
    if (mer1.me.colRef.nTagRefs > 0) {
354,585✔
264
      metaRsp.pTagRefs = (SColRef*)taosMemoryMalloc(sizeof(SColRef) * mer1.me.colRef.nTagRefs);
×
265
      if (metaRsp.pTagRefs) {
×
266
        code = fillTableTagRef(&mer1, metaRsp.pTagRefs, mer1.me.colRef.nTagRefs);
×
267
        if (code < 0) {
×
268
          taosMemoryFreeClear(metaRsp.pTagRefs);
×
269
          goto _exit;
×
270
        }
271
      } else {
272
        code = terrno;
×
273
        goto _exit;
×
274
      }
275
      metaRsp.numOfTagRefs = mer1.me.colRef.nTagRefs;
×
276
    } else {
277
      metaRsp.pTagRefs = NULL;
354,585✔
278
      metaRsp.numOfTagRefs = 0;
354,585✔
279
    }
280
  } else {
281
    metaRsp.pColRefs = NULL;
20,365,627✔
282
    metaRsp.numOfColRefs = 0;
20,365,627✔
283
    metaRsp.pTagRefs = NULL;
20,365,627✔
284
    metaRsp.numOfTagRefs = 0;
20,365,627✔
285
  }
286

287
  vnodeDebugTableMeta(&metaRsp);
20,720,212✔
288

289
  // encode and send response
290
  rspLen = tSerializeSTableMetaRsp(NULL, 0, &metaRsp);
20,720,212✔
291
  if (rspLen < 0) {
20,719,556✔
292
    code = terrno;
×
293
    goto _exit;
×
294
  }
295

296
  if (direct) {
20,719,556✔
297
    pRsp = rpcMallocCont(rspLen);
487,204✔
298
  } else {
299
    pRsp = taosMemoryCalloc(1, rspLen);
20,232,352✔
300
  }
301

302
  if (pRsp == NULL) {
20,720,012✔
303
    code = terrno;
×
304
    goto _exit;
×
305
  }
306

307
  rspLen = tSerializeSTableMetaRsp(pRsp, rspLen, &metaRsp);
20,720,012✔
308
  if (rspLen < 0) {
20,718,899✔
309
    code = terrno;
×
310
    goto _exit;
×
311
  }
312

313
_exit:
20,718,899✔
314
  taosMemoryFree(metaRsp.pColRefs);
20,719,555✔
315
  taosMemoryFree(metaRsp.pSchemas);
20,718,243✔
316
  taosMemoryFree(metaRsp.pSchemaExt);
20,719,556✔
317
  taosMemoryFree(metaRsp.pTagRefs);
20,718,359✔
318
_exit2:
20,718,243✔
319
  metaReaderClear(&mer2);
20,720,212✔
320
_exit3:
29,730,186✔
321
  metaReaderClear(&mer1);
29,732,909✔
322
_exit4:
29,732,151✔
323
  rpcMsg.info = pMsg->info;
29,733,450✔
324
  rpcMsg.pCont = pRsp;
29,733,991✔
325
  rpcMsg.contLen = rspLen;
29,733,991✔
326
  rpcMsg.code = code;
29,733,991✔
327
  rpcMsg.msgType = pMsg->msgType;
29,733,991✔
328

329
  if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST && autoCreateCtb == 1) {
29,732,022✔
330
    code = TSDB_CODE_SUCCESS;
5,475,158✔
331
  }
332

333
  if (code) {
29,732,022✔
334
    qError("vgId:%d, get table %s meta with %" PRIu8 " failed cause of %s", pVnode->config.vgId, infoReq.tbName,
3,538,242✔
335
           infoReq.option, tstrerror(code));
336
  }
337

338
  if (direct) {
29,733,991✔
339
    tmsgSendRsp(&rpcMsg);
1,253,953✔
340
  } else {
341
    *pMsg = rpcMsg;
28,480,038✔
342
  }
343

344
  return code;
29,733,991✔
345
}
346

347
int32_t vnodeGetTableCfg(SVnode *pVnode, SRpcMsg *pMsg, bool direct) {
61,274✔
348
  STableCfgReq   cfgReq = {0};
61,274✔
349
  STableCfgRsp   cfgRsp = {0};
61,274✔
350
  SMetaReader    mer1 = {0};
61,274✔
351
  SMetaReader    mer2 = {0};
61,274✔
352
  char           tableFName[TSDB_TABLE_FNAME_LEN];
61,274✔
353
  SRpcMsg        rpcMsg = {0};
61,274✔
354
  int32_t        code = 0;
61,274✔
355
  int32_t        rspLen = 0;
61,274✔
356
  void          *pRsp = NULL;
61,274✔
357
  SSchemaWrapper schema = {0};
61,274✔
358
  SSchemaWrapper schemaTag = {0};
61,274✔
359

360
  // decode req
361
  if (tDeserializeSTableCfgReq(pMsg->pCont, pMsg->contLen, &cfgReq) != 0) {
61,274✔
362
    code = terrno;
×
363
    goto _exit;
×
364
  }
365

366
  tstrncpy(cfgRsp.tbName, cfgReq.tbName, TSDB_TABLE_NAME_LEN);
61,274✔
367
  (void)memcpy(cfgRsp.dbFName, cfgReq.dbFName, sizeof(cfgRsp.dbFName));
61,274✔
368

369
  (void)snprintf(tableFName, TSDB_TABLE_FNAME_LEN, "%s.%s", cfgReq.dbFName, cfgReq.tbName);
61,274✔
370
  if (pVnode->mounted) tTrimMountPrefix(tableFName);
61,274✔
371
  code = vnodeValidateTableHash(pVnode, tableFName);
61,274✔
372
  if (code) {
61,274✔
373
    goto _exit;
×
374
  }
375

376
  // query meta
377
  metaReaderDoInit(&mer1, pVnode->pMeta, META_READER_LOCK);
61,274✔
378

379
  if (metaGetTableEntryByName(&mer1, cfgReq.tbName) < 0) {
61,274✔
380
    code = terrno;
×
381
    goto _exit;
×
382
  }
383

384
  cfgRsp.tableType = mer1.me.type;
61,274✔
385
  cfgRsp.isAudit = pVnode->config.isAudit ? 1 : 0;
61,274✔
386
  cfgRsp.secureDelete = pVnode->config.secureDelete;
61,274✔
387

388
  if (mer1.me.type == TSDB_SUPER_TABLE) {
61,274✔
389
    code = TSDB_CODE_VND_HASH_MISMATCH;
×
390
    goto _exit;
×
391
  } else if (mer1.me.type == TSDB_CHILD_TABLE || mer1.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
61,274✔
392
    metaReaderDoInit(&mer2, pVnode->pMeta, META_READER_NOLOCK);
43,591✔
393
    if (metaReaderGetTableEntryByUid(&mer2, mer1.me.ctbEntry.suid) < 0) goto _exit;
43,591✔
394

395
    tstrncpy(cfgRsp.stbName, mer2.me.name, TSDB_TABLE_NAME_LEN);
43,591✔
396
    schema = mer2.me.stbEntry.schemaRow;
43,591✔
397
    schemaTag = mer2.me.stbEntry.schemaTag;
43,591✔
398
    cfgRsp.ownerId = mer2.me.stbEntry.ownerId;  // child table inherits ownerId from stb
43,591✔
399
    cfgRsp.ttl = mer1.me.ctbEntry.ttlDays;
43,591✔
400
    cfgRsp.commentLen = mer1.me.ctbEntry.commentLen;
43,591✔
401
    if (mer1.me.ctbEntry.commentLen > 0) {
43,591✔
402
      cfgRsp.pComment = taosStrdup(mer1.me.ctbEntry.comment);
×
403
      if (NULL == cfgRsp.pComment) {
×
404
        code = terrno;
×
405
        goto _exit;
×
406
      }
407
    }
408
    STag *pTag = (STag *)mer1.me.ctbEntry.pTags;
43,591✔
409
    cfgRsp.tagsLen = pTag->len;
43,591✔
410
    cfgRsp.pTags = taosMemoryMalloc(cfgRsp.tagsLen);
43,591✔
411
    if (NULL == cfgRsp.pTags) {
43,591✔
412
      code = terrno;
×
413
      goto _exit;
×
414
    }
415
    (void)memcpy(cfgRsp.pTags, pTag, cfgRsp.tagsLen);
43,591✔
416
  } else if (mer1.me.type == TSDB_NORMAL_TABLE || mer1.me.type == TSDB_VIRTUAL_NORMAL_TABLE) {
17,683✔
417
    schema = mer1.me.ntbEntry.schemaRow;
17,683✔
418
    cfgRsp.ttl = mer1.me.ntbEntry.ttlDays;
17,683✔
419
    cfgRsp.ownerId = mer1.me.ntbEntry.ownerId;
17,683✔
420
    cfgRsp.commentLen = mer1.me.ntbEntry.commentLen;
17,683✔
421
    if (mer1.me.ntbEntry.commentLen > 0) {
17,683✔
422
      cfgRsp.pComment = taosStrdup(mer1.me.ntbEntry.comment);
×
423
      if (NULL == cfgRsp.pComment) {
×
424
        code = terrno;
×
425
        goto _exit;
×
426
      }
427
    }
428
  } else {
429
    vError("vnodeGetTableCfg get invalid table type:%d", mer1.me.type);
×
430
    code = TSDB_CODE_APP_ERROR;
×
431
    goto _exit;
×
432
  }
433

434
  cfgRsp.numOfTags = schemaTag.nCols;
61,274✔
435
  cfgRsp.numOfColumns = schema.nCols;
61,274✔
436
  cfgRsp.virtualStb = false; // vnode don't have super table, so it's always false
61,274✔
437
  cfgRsp.pSchemas = (SSchema *)taosMemoryMalloc(sizeof(SSchema) * (cfgRsp.numOfColumns + cfgRsp.numOfTags));
61,274✔
438
  cfgRsp.pSchemaExt = (SSchemaExt *)taosMemoryMalloc(cfgRsp.numOfColumns * sizeof(SSchemaExt));
61,274✔
439
  cfgRsp.pColRefs = (SColRef *)taosMemoryMalloc(sizeof(SColRef) * cfgRsp.numOfColumns);
61,274✔
440
  cfgRsp.numOfTagRefs = 0;
61,274✔
441
  cfgRsp.pTagRefs = NULL;
61,274✔
442

443
  if (NULL == cfgRsp.pSchemas || NULL == cfgRsp.pSchemaExt || NULL == cfgRsp.pColRefs) {
61,274✔
444
    code = terrno;
×
445
    goto _exit;
×
446
  }
447
  (void)memcpy(cfgRsp.pSchemas, schema.pSchema, sizeof(SSchema) * schema.nCols);
61,274✔
448
  if (schemaTag.nCols) {
61,274✔
449
    (void)memcpy(cfgRsp.pSchemas + schema.nCols, schemaTag.pSchema, sizeof(SSchema) * schemaTag.nCols);
43,591✔
450
  }
451

452
  SMetaReader     *pReader = (mer1.me.type == TSDB_CHILD_TABLE || mer1.me.type == TSDB_VIRTUAL_CHILD_TABLE) ? &mer2 : &mer1;
61,274✔
453
  SColCmprWrapper *pColCmpr = &pReader->me.colCmpr;
61,274✔
454
  SColRefWrapper  *pColRef = &mer1.me.colRef;
61,274✔
455

456
  if (withExtSchema(cfgRsp.tableType)) {
61,274✔
457
    for (int32_t i = 0; i < cfgRsp.numOfColumns; i++) {
222,815✔
458
      SColCmpr   *pCmpr = &pColCmpr->pColCmpr[i];
169,362✔
459
      SSchemaExt *pSchExt = cfgRsp.pSchemaExt + i;
169,362✔
460
      pSchExt->colId = pCmpr->id;
169,362✔
461
      pSchExt->compress = pCmpr->alg;
169,362✔
462
      if (pReader->me.pExtSchemas)
169,362✔
463
        pSchExt->typeMod = pReader->me.pExtSchemas[i].typeMod;
25,074✔
464
      else
465
        pSchExt->typeMod = 0;
144,288✔
466
    }
467
  }
468

469
  cfgRsp.virtualStb = false;
61,274✔
470
  if (hasRefCol(cfgRsp.tableType)) {
61,274✔
471
    for (int32_t i = 0; i < cfgRsp.numOfColumns; i++) {
110,218✔
472
      SColRef *pRef = &pColRef->pColRef[i];
102,397✔
473
      cfgRsp.pColRefs[i].hasRef = pRef->hasRef;
102,397✔
474
      cfgRsp.pColRefs[i].id = pRef->id;
102,397✔
475
      if (cfgRsp.pColRefs[i].hasRef) {
102,397✔
476
        tstrncpy(cfgRsp.pColRefs[i].refDbName, pRef->refDbName, TSDB_DB_NAME_LEN);
53,686✔
477
        tstrncpy(cfgRsp.pColRefs[i].refTableName, pRef->refTableName, TSDB_TABLE_NAME_LEN);
53,686✔
478
        tstrncpy(cfgRsp.pColRefs[i].refColName, pRef->refColName, TSDB_COL_NAME_LEN);
53,686✔
479
      }
480
    }
481

482
    cfgRsp.numOfTagRefs = pColRef->nTagRefs;
7,821✔
483
    if (cfgRsp.numOfTagRefs > 0) {
7,821✔
484
      if (NULL == pColRef->pTagRef) {
×
485
        code = TSDB_CODE_APP_ERROR;
×
486
        goto _exit;
×
487
      }
488
      cfgRsp.pTagRefs = (SColRef *)taosMemoryMalloc(sizeof(SColRef) * cfgRsp.numOfTagRefs);
×
489
      if (NULL == cfgRsp.pTagRefs) {
×
490
        code = terrno;
×
491
        goto _exit;
×
492
      }
493

494
      for (int32_t i = 0; i < cfgRsp.numOfTagRefs; i++) {
×
495
        SColRef *pRef = &pColRef->pTagRef[i];
×
496
        cfgRsp.pTagRefs[i].hasRef = pRef->hasRef;
×
497
        cfgRsp.pTagRefs[i].id = pRef->id;
×
498
        if (cfgRsp.pTagRefs[i].hasRef) {
×
499
          tstrncpy(cfgRsp.pTagRefs[i].refDbName, pRef->refDbName, TSDB_DB_NAME_LEN);
×
500
          tstrncpy(cfgRsp.pTagRefs[i].refTableName, pRef->refTableName, TSDB_TABLE_NAME_LEN);
×
501
          tstrncpy(cfgRsp.pTagRefs[i].refColName, pRef->refColName, TSDB_COL_NAME_LEN);
×
502
        }
503
      }
504
    }
505
  }
506

507
  // encode and send response
508
  rspLen = tSerializeSTableCfgRsp(NULL, 0, &cfgRsp);
61,274✔
509
  if (rspLen < 0) {
61,274✔
510
    code = terrno;
×
511
    goto _exit;
×
512
  }
513

514
  if (direct) {
61,274✔
515
    pRsp = rpcMallocCont(rspLen);
×
516
  } else {
517
    pRsp = taosMemoryCalloc(1, rspLen);
61,274✔
518
  }
519

520
  if (pRsp == NULL) {
61,274✔
521
    code = terrno;
×
522
    goto _exit;
×
523
  }
524

525
  rspLen = tSerializeSTableCfgRsp(pRsp, rspLen, &cfgRsp);
61,274✔
526
  if (rspLen < 0) {
61,274✔
527
    code = terrno;
×
528
    goto _exit;
×
529
  }
530

531
_exit:
61,274✔
532
  rpcMsg.info = pMsg->info;
61,274✔
533
  rpcMsg.pCont = pRsp;
61,274✔
534
  rpcMsg.contLen = rspLen;
61,274✔
535
  rpcMsg.code = code;
61,274✔
536
  rpcMsg.msgType = pMsg->msgType;
61,274✔
537

538
  if (code) {
61,274✔
539
    qError("get table %s cfg failed cause of %s", cfgReq.tbName, tstrerror(code));
×
540
  }
541

542
  if (direct) {
61,274✔
543
    tmsgSendRsp(&rpcMsg);
×
544
  } else {
545
    *pMsg = rpcMsg;
61,274✔
546
  }
547

548
  tFreeSTableCfgRsp(&cfgRsp);
61,274✔
549
  metaReaderClear(&mer2);
61,274✔
550
  metaReaderClear(&mer1);
61,274✔
551
  return code;
61,274✔
552
}
553

554
static FORCE_INLINE void vnodeFreeSBatchRspMsg(void *p) {
555
  if (NULL == p) {
556
    return;
557
  }
558

559
  SBatchRspMsg *pRsp = (SBatchRspMsg *)p;
560
  rpcFreeCont(pRsp->msg);
561
}
562

563
int32_t vnodeGetBatchMeta(SVnode *pVnode, SRpcMsg *pMsg) {
27,445,627✔
564
  int32_t      code = 0;
27,445,627✔
565
  int32_t      rspSize = 0;
27,445,627✔
566
  SBatchReq    batchReq = {0};
27,445,627✔
567
  SBatchMsg   *req = NULL;
27,445,436✔
568
  SBatchRspMsg rsp = {0};
27,445,436✔
569
  SBatchRsp    batchRsp = {0};
27,432,377✔
570
  SRpcMsg      reqMsg = *pMsg;
27,432,377✔
571
  SRpcMsg      rspMsg = {0};
27,432,377✔
572
  void        *pRsp = NULL;
27,438,632✔
573

574
  if (tDeserializeSBatchReq(pMsg->pCont, pMsg->contLen, &batchReq)) {
27,438,632✔
575
    code = terrno;
×
576
    qError("tDeserializeSBatchReq failed");
×
577
    goto _exit;
×
578
  }
579

580
  int32_t msgNum = taosArrayGetSize(batchReq.pMsgs);
27,444,859✔
581
  if (msgNum >= MAX_META_MSG_IN_BATCH) {
27,439,904✔
582
    code = TSDB_CODE_INVALID_MSG;
×
583
    qError("too many msgs %d in vnode batch meta req", msgNum);
×
584
    goto _exit;
×
585
  }
586

587
  batchRsp.pRsps = taosArrayInit(msgNum, sizeof(SBatchRspMsg));
27,439,904✔
588
  if (NULL == batchRsp.pRsps) {
27,434,922✔
589
    code = terrno;
×
590
    qError("taosArrayInit %d SBatchRspMsg failed", msgNum);
×
591
    goto _exit;
×
592
  }
593

594
  for (int32_t i = 0; i < msgNum; ++i) {
60,096,662✔
595
    req = taosArrayGet(batchReq.pMsgs, i);
32,656,045✔
596
    if (req == NULL) {
32,655,513✔
597
      code = terrno;
×
598
      goto _exit;
×
599
    }
600

601
    reqMsg.msgType = req->msgType;
32,655,513✔
602
    reqMsg.pCont = req->msg;
32,655,503✔
603
    reqMsg.contLen = req->msgLen;
32,652,336✔
604

605
    switch (req->msgType) {
32,648,013✔
606
      case TDMT_VND_TABLE_META:
28,242,184✔
607
        // error code has been set into reqMsg, no need to handle it here.
608
        if (TSDB_CODE_SUCCESS != vnodeGetTableMeta(pVnode, &reqMsg, false)) {
28,242,184✔
609
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
2,685,642✔
610
        }
611
        break;
28,244,808✔
612
      case TDMT_VND_TABLE_NAME:
235,230✔
613
        // error code has been set into reqMsg, no need to handle it here.
614
        if (TSDB_CODE_SUCCESS != vnodeGetTableMeta(pVnode, &reqMsg, false)) {
235,230✔
615
          qWarn("vnodeGetBatchName failed, msgType:%d", req->msgType);
86,230✔
616
        }
617
        break;
235,230✔
618
      case TDMT_VND_TABLE_CFG:
61,274✔
619
        // error code has been set into reqMsg, no need to handle it here.
620
        if (TSDB_CODE_SUCCESS != vnodeGetTableCfg(pVnode, &reqMsg, false)) {
61,274✔
621
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
×
622
        }
623
        break;
61,274✔
624
      case TDMT_VND_VSUBTABLES_META:
×
625
        // error code has been set into reqMsg, no need to handle it here.
626
        if (TSDB_CODE_SUCCESS != vnodeGetVSubtablesMeta(pVnode, &reqMsg)) {
×
627
          qWarn("vnodeGetVSubtablesMeta failed, msgType:%d", req->msgType);
×
628
        }
629
        break;
×
630
      case TDMT_VND_VSTB_REF_DBS:
4,116,637✔
631
        // error code has been set into reqMsg, no need to handle it here.
632
        if (TSDB_CODE_SUCCESS != vnodeGetVStbRefDbs(pVnode, &reqMsg)) {
4,116,637✔
633
          qWarn("vnodeGetVStbRefDbs failed, msgType:%d", req->msgType);
×
634
        }
635
        break;
4,113,461✔
636
      default:
×
637
        qError("invalid req msgType %d", req->msgType);
×
638
        reqMsg.code = TSDB_CODE_INVALID_MSG;
×
639
        reqMsg.pCont = NULL;
×
640
        reqMsg.contLen = 0;
×
641
        break;
×
642
    }
643

644
    rsp.msgIdx = req->msgIdx;
32,654,773✔
645
    rsp.reqType = reqMsg.msgType;
32,655,987✔
646
    rsp.msgLen = reqMsg.contLen;
32,655,987✔
647
    rsp.rspCode = reqMsg.code;
32,655,987✔
648
    rsp.msg = reqMsg.pCont;
32,655,987✔
649

650
    if (NULL == taosArrayPush(batchRsp.pRsps, &rsp)) {
65,309,035✔
651
      qError("taosArrayPush failed");
×
652
      code = terrno;
×
653
      goto _exit;
×
654
    }
655
  }
656

657
  rspSize = tSerializeSBatchRsp(NULL, 0, &batchRsp);
27,440,617✔
658
  if (rspSize < 0) {
27,441,786✔
659
    qError("tSerializeSBatchRsp failed");
×
660
    code = terrno;
×
661
    goto _exit;
×
662
  }
663
  pRsp = rpcMallocCont(rspSize);
27,441,786✔
664
  if (pRsp == NULL) {
27,435,514✔
665
    qError("rpcMallocCont %d failed", rspSize);
×
666
    code = terrno;
×
667
    goto _exit;
×
668
  }
669
  if (tSerializeSBatchRsp(pRsp, rspSize, &batchRsp) < 0) {
27,435,514✔
670
    qError("tSerializeSBatchRsp %d failed", rspSize);
×
671
    code = terrno;
×
672
    goto _exit;
×
673
  }
674

675
_exit:
27,441,626✔
676

677
  rspMsg.info = pMsg->info;
27,442,850✔
678
  rspMsg.pCont = pRsp;
27,441,838✔
679
  rspMsg.contLen = rspSize;
27,441,838✔
680
  rspMsg.code = code;
27,441,838✔
681
  rspMsg.msgType = pMsg->msgType;
27,441,838✔
682

683
  if (code) {
27,433,830✔
684
    qError("vnd get batch meta failed cause of %s", tstrerror(code));
×
685
  }
686

687
  taosArrayDestroyEx(batchReq.pMsgs, tFreeSBatchReqMsg);
27,433,830✔
688
  taosArrayDestroyEx(batchRsp.pRsps, tFreeSBatchRspMsg);
27,436,783✔
689

690
  tmsgSendRsp(&rspMsg);
27,421,220✔
691

692
  return code;
27,446,722✔
693
}
694

695
#define VNODE_DO_META_QUERY(pVnode, cmd)                 \
696
  do {                                                   \
697
    (void)taosThreadRwlockRdlock(&(pVnode)->metaRWLock); \
698
    cmd;                                                 \
699
    (void)taosThreadRwlockUnlock(&(pVnode)->metaRWLock); \
700
  } while (0)
701

702
int32_t vnodeReadVSubtables(SReadHandle* pHandle, int64_t suid, SArray** ppRes) {
×
703
  int32_t                    code = TSDB_CODE_SUCCESS;
×
704
  int32_t                    line = 0;
×
705
  SMetaReader                mr = {0};
×
706
  bool                       readerInit = false;
×
707
  SVCTableRefCols*           pTb = NULL;
×
708
  int32_t                    refColsNum = 0;
×
709
  char                       tbFName[TSDB_TABLE_FNAME_LEN];
×
710
  SSHashObj*                 pSrcTbls = NULL;
×
711

712
  SArray *pList = taosArrayInit(10, sizeof(uint64_t));
×
713
  QUERY_CHECK_NULL(pList, code, line, _return, terrno);
×
714

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

717
  size_t num = taosArrayGetSize(pList);
×
718
  *ppRes = taosArrayInit(num, POINTER_BYTES);
×
719
  QUERY_CHECK_NULL(*ppRes, code, line, _return, terrno);
×
720
  pSrcTbls = tSimpleHashInit(10, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
×
721
  QUERY_CHECK_NULL(pSrcTbls, code, line, _return, terrno);
×
722

723
  for (int32_t i = 0; i < num; ++i) {
×
724
    uint64_t* id = taosArrayGet(pList, i);
×
725
    QUERY_CHECK_NULL(id, code, line, _return, terrno);
×
726
    pHandle->api.metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pHandle->api.metaFn);
×
727
    QUERY_CHECK_CODE(pHandle->api.metaReaderFn.getTableEntryByUid(&mr, *id), line, _return);
×
728
    readerInit = true;
×
729

730
    refColsNum = 0;
×
731
    for (int32_t j = 0; j < mr.me.colRef.nCols; j++) {
×
732
      if (mr.me.colRef.pColRef[j].hasRef) {
×
733
        refColsNum++;
×
734
      }
735
    }
736

737
    if (refColsNum <= 0) {
×
738
      pHandle->api.metaReaderFn.clearReader(&mr);
×
739
      readerInit = false;
×
740
      continue;
×
741
    }
742

743
    pTb = taosMemoryCalloc(1, refColsNum * sizeof(SRefColInfo) + sizeof(*pTb));
×
744
    QUERY_CHECK_NULL(pTb, code, line, _return, terrno);
×
745

746
    pTb->uid = mr.me.uid;
×
747
    pTb->numOfColRefs = refColsNum;
×
748
    pTb->refCols = (SRefColInfo*)(pTb + 1);
×
749

750
    refColsNum = 0;
×
751
    tSimpleHashClear(pSrcTbls);
×
752
    for (int32_t j = 0; j < mr.me.colRef.nCols; j++) {
×
753
      if (!mr.me.colRef.pColRef[j].hasRef) {
×
754
        continue;
×
755
      }
756

757
      pTb->refCols[refColsNum].colId = mr.me.colRef.pColRef[j].id;
×
758
      tstrncpy(pTb->refCols[refColsNum].refColName, mr.me.colRef.pColRef[j].refColName, TSDB_COL_NAME_LEN);
×
759
      tstrncpy(pTb->refCols[refColsNum].refTableName, mr.me.colRef.pColRef[j].refTableName, TSDB_TABLE_NAME_LEN);
×
760
      tstrncpy(pTb->refCols[refColsNum].refDbName, mr.me.colRef.pColRef[j].refDbName, TSDB_DB_NAME_LEN);
×
761

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

764
      if (NULL == tSimpleHashGet(pSrcTbls, tbFName, strlen(tbFName))) {
×
765
        QUERY_CHECK_CODE(tSimpleHashPut(pSrcTbls, tbFName, strlen(tbFName), &code, sizeof(code)), line, _return);
×
766
      }
767

768
      refColsNum++;
×
769
    }
770

771
    pTb->numOfSrcTbls = tSimpleHashGetSize(pSrcTbls);
×
772
    QUERY_CHECK_NULL(taosArrayPush(*ppRes, &pTb), code, line, _return, terrno);
×
773
    pTb = NULL;
×
774

775
    pHandle->api.metaReaderFn.clearReader(&mr);
×
776
    readerInit = false;
×
777
  }
778

779
_return:
×
780

781
  if (readerInit) {
×
782
    pHandle->api.metaReaderFn.clearReader(&mr);
×
783
  }
784

785
  taosArrayDestroy(pList);
×
786
  taosMemoryFree(pTb);
×
787
  tSimpleHashCleanup(pSrcTbls);
×
788

789
  if (code) {
×
790
    qError("%s failed since %s", __func__, tstrerror(code));
×
791
  }
792
  return code;
×
793
}
794

795
int32_t vnodeReadVStbRefDbs(SReadHandle* pHandle, int64_t suid, SArray** ppRes) {
4,111,090✔
796
  int32_t                    code = TSDB_CODE_SUCCESS;
4,111,090✔
797
  int32_t                    line = 0;
4,111,090✔
798
  SMetaReader                mr = {0};
4,111,090✔
799
  bool                       readerInit = false;
4,110,494✔
800
  SSHashObj*                 pDbNameHash = NULL;
4,110,494✔
801
  SArray*                    pList = NULL;
4,110,494✔
802

803
  pList = taosArrayInit(10, sizeof(uint64_t));
4,110,494✔
804
  QUERY_CHECK_NULL(pList, code, line, _return, terrno);
4,106,753✔
805

806
  *ppRes = taosArrayInit(10, POINTER_BYTES);
4,106,753✔
807
  QUERY_CHECK_NULL(*ppRes, code, line, _return, terrno)
4,112,376✔
808
  
809
  // lookup in cache
810
  code = pHandle->api.metaFn.metaGetCachedRefDbs(pHandle->vnode, suid, *ppRes);
4,113,008✔
811
  QUERY_CHECK_CODE(code, line, _return);
4,120,995✔
812

813
  if (taosArrayGetSize(*ppRes) > 0) {
4,120,995✔
814
    // found in cache
815
    goto _return;
4,045,164✔
816
  } else {
817
    code = pHandle->api.metaFn.getChildTableList(pHandle->vnode, suid, pList);
75,831✔
818
    QUERY_CHECK_CODE(code, line, _return);
75,831✔
819

820
    size_t num = taosArrayGetSize(pList);
75,831✔
821
    pDbNameHash = tSimpleHashInit(10, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
75,831✔
822
    QUERY_CHECK_NULL(pDbNameHash, code, line, _return, terrno);
75,831✔
823

824
    for (int32_t i = 0; i < num; ++i) {
448,700✔
825
      uint64_t* id = taosArrayGet(pList, i);
372,869✔
826
      QUERY_CHECK_NULL(id, code, line, _return, terrno);
372,869✔
827

828
      pHandle->api.metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pHandle->api.metaFn);
372,869✔
829
      readerInit = true;
372,869✔
830

831
      code = pHandle->api.metaReaderFn.getTableEntryByUid(&mr, *id);
372,869✔
832
      QUERY_CHECK_CODE(code, line, _return);
372,869✔
833

834
      for (int32_t j = 0; j < mr.me.colRef.nCols; j++) {
39,418,358✔
835
        if (mr.me.colRef.pColRef[j].hasRef) {
39,045,489✔
836
          if (NULL == tSimpleHashGet(pDbNameHash, mr.me.colRef.pColRef[j].refDbName, strlen(mr.me.colRef.pColRef[j].refDbName))) {
38,397,362✔
837
            char *refDbName = taosStrdup(mr.me.colRef.pColRef[j].refDbName);
56,945✔
838
            QUERY_CHECK_NULL(refDbName, code, line, _return, terrno);
56,945✔
839

840
            QUERY_CHECK_NULL(taosArrayPush(*ppRes, &refDbName), code, line, _return, terrno);
113,890✔
841

842
            code = tSimpleHashPut(pDbNameHash, refDbName, strlen(refDbName), NULL, 0);
56,945✔
843
            QUERY_CHECK_CODE(code, line, _return);
56,945✔
844
          }
845
        }
846
      }
847

848
      pHandle->api.metaReaderFn.clearReader(&mr);
372,869✔
849
      readerInit = false;
372,869✔
850
    }
851

852
    code = pHandle->api.metaFn.metaPutRefDbsToCache(pHandle->vnode, suid, *ppRes);
75,831✔
853
    QUERY_CHECK_CODE(code, line, _return);
75,831✔
854
  }
855

856
_return:
4,120,995✔
857

858
  if (readerInit) {
4,120,361✔
859
    pHandle->api.metaReaderFn.clearReader(&mr);
×
860
  }
861

862
  taosArrayDestroy(pList);
4,120,361✔
863
  tSimpleHashCleanup(pDbNameHash);
4,113,593✔
864

865
  if (code) {
4,117,257✔
866
    qError("%s failed since %s", __func__, tstrerror(code));
×
867
  }
868
  return code;
4,117,257✔
869
}
870

871
int32_t vnodeGetVSubtablesMeta(SVnode *pVnode, SRpcMsg *pMsg) {
×
872
  int32_t        code = 0;
×
873
  int32_t        rspSize = 0;
×
874
  SVSubTablesReq req = {0};
×
875
  SVSubTablesRsp rsp = {0};
×
876
  SRpcMsg      rspMsg = {0};
×
877
  void        *pRsp = NULL;
×
878
  int32_t      line = 0;
×
879

880
  if (tDeserializeSVSubTablesReq(pMsg->pCont, pMsg->contLen, &req)) {
×
881
    code = terrno;
×
882
    qError("tDeserializeSVSubTablesReq failed");
×
883
    goto _return;
×
884
  }
885

886
  SReadHandle handle = {0};
×
887
  handle.vnode = pVnode;
×
888
  initStorageAPI(&handle.api);
×
889

890
  QUERY_CHECK_CODE(vnodeReadVSubtables(&handle, req.suid, &rsp.pTables), line, _return);
×
891
  rsp.vgId = TD_VID(pVnode);
×
892

893
  rspSize = tSerializeSVSubTablesRsp(NULL, 0, &rsp);
×
894
  if (rspSize < 0) {
×
895
    code = rspSize;
×
896
    qError("tSerializeSVSubTablesRsp failed, error:%d", rspSize);
×
897
    goto _return;
×
898
  }
899
  pRsp = taosMemoryCalloc(1, rspSize);
×
900
  if (pRsp == NULL) {
×
901
    code = terrno;
×
902
    qError("rpcMallocCont %d failed, error:%d", rspSize, terrno);
×
903
    goto _return;
×
904
  }
905
  rspSize = tSerializeSVSubTablesRsp(pRsp, rspSize, &rsp);
×
906
  if (rspSize < 0) {
×
907
    code = rspSize;
×
908
    qError("tSerializeSVSubTablesRsp failed, error:%d", rspSize);
×
909
    goto _return;
×
910
  }
911

912
_return:
×
913

914
  rspMsg.info = pMsg->info;
×
915
  rspMsg.pCont = pRsp;
×
916
  rspMsg.contLen = rspSize;
×
917
  rspMsg.code = code;
×
918
  rspMsg.msgType = pMsg->msgType;
×
919

920
  if (code) {
×
921
    qError("vnd get virtual subtables failed cause of %s", tstrerror(code));
×
922
  }
923

924
  *pMsg = rspMsg;
×
925
  
926
  tDestroySVSubTablesRsp(&rsp);
×
927

928
  //tmsgSendRsp(&rspMsg);
929

930
  return code;
×
931
}
932

933
int32_t vnodeGetVStbRefDbs(SVnode *pVnode, SRpcMsg *pMsg) {
4,114,735✔
934
  int32_t        code = 0;
4,114,735✔
935
  int32_t        rspSize = 0;
4,114,735✔
936
  SVStbRefDbsReq req = {0};
4,114,735✔
937
  SVStbRefDbsRsp rsp = {0};
4,115,349✔
938
  SRpcMsg        rspMsg = {0};
4,100,554✔
939
  void          *pRsp = NULL;
4,100,554✔
940
  int32_t        line = 0;
4,100,554✔
941

942
  if (tDeserializeSVStbRefDbsReq(pMsg->pCont, pMsg->contLen, &req)) {
4,100,554✔
943
    code = terrno;
×
944
    qError("tDeserializeSVSubTablesReq failed");
×
945
    goto _return;
×
946
  }
947

948
  SReadHandle handle = {0};
4,112,357✔
949
  handle.vnode = pVnode;
4,115,457✔
950
  initStorageAPI(&handle.api);
4,115,457✔
951

952
  code = vnodeReadVStbRefDbs(&handle, req.suid, &rsp.pDbs);
4,116,617✔
953
  QUERY_CHECK_CODE(code, line, _return);
4,115,989✔
954
  rsp.vgId = TD_VID(pVnode);
4,115,989✔
955

956
  rspSize = tSerializeSVStbRefDbsRsp(NULL, 0, &rsp);
4,118,477✔
957
  if (rspSize < 0) {
4,106,658✔
958
    code = rspSize;
×
959
    qError("tSerializeSVStbRefDbsRsp failed, error:%d", rspSize);
×
960
    goto _return;
×
961
  }
962
  pRsp = taosMemoryCalloc(1, rspSize);
4,106,658✔
963
  if (pRsp == NULL) {
4,107,347✔
964
    code = terrno;
×
965
    qError("rpcMallocCont %d failed, error:%d", rspSize, terrno);
×
966
    goto _return;
×
967
  }
968
  rspSize = tSerializeSVStbRefDbsRsp(pRsp, rspSize, &rsp);
4,107,347✔
969
  if (rspSize < 0) {
4,112,868✔
970
    code = rspSize;
×
971
    qError("tSerializeSVStbRefDbsRsp failed, error:%d", rspSize);
×
972
    goto _return;
×
973
  }
974

975
_return:
4,112,868✔
976

977
  rspMsg.info = pMsg->info;
4,117,760✔
978
  rspMsg.pCont = pRsp;
4,118,571✔
979
  rspMsg.contLen = rspSize;
4,118,571✔
980
  rspMsg.code = code;
4,118,571✔
981
  rspMsg.msgType = pMsg->msgType;
4,118,571✔
982

983
  if (code) {
4,115,881✔
984
    qError("vnd get virtual stb ref db failed cause of %s", tstrerror(code));
×
985
  }
986

987
  *pMsg = rspMsg;
4,115,881✔
988

989
  tDestroySVStbRefDbsRsp(&rsp);
4,107,269✔
990

991
  return code;
4,111,701✔
992
}
993

994
static int32_t vnodeGetCompStorage(SVnode *pVnode, int64_t *output) {
136,456,346✔
995
  int32_t code = 0;
136,456,346✔
996
#ifdef TD_ENTERPRISE
997
  int32_t now = taosGetTimestampSec();
136,456,346✔
998
  if (llabs(now - pVnode->config.vndStats.storageLastUpd) >= 30) {
136,456,346✔
999
    pVnode->config.vndStats.storageLastUpd = now;
7,219,122✔
1000

1001
    SDbSizeStatisInfo info = {0};
7,219,122✔
1002
    if (0 == (code = vnodeGetDBSize(pVnode, &info))) {
7,219,122✔
1003
      int64_t compSize =
7,219,122✔
1004
          info.l1Size + info.l2Size + info.l3Size + info.cacheSize + info.walSize + info.metaSize + +info.ssSize;
7,219,122✔
1005
      if (compSize >= 0) {
7,219,122✔
1006
        pVnode->config.vndStats.compStorage = compSize;
7,219,122✔
1007
      } else {
1008
        vError("vnode get comp storage failed since compSize is negative:%" PRIi64, compSize);
×
1009
        code = TSDB_CODE_APP_ERROR;
×
1010
      }
1011
    } else {
1012
      vWarn("vnode get comp storage failed since %s", tstrerror(code));
×
1013
    }
1014
  }
1015
  if (output) *output = pVnode->config.vndStats.compStorage;
136,456,346✔
1016
#endif
1017
  return code;
136,456,346✔
1018
}
1019

1020
static void vnodeGetBufferInfo(SVnode *pVnode, int64_t *bufferSegmentUsed, int64_t *bufferSegmentSize) {
136,456,346✔
1021
  *bufferSegmentUsed = 0;
136,456,346✔
1022
  *bufferSegmentSize = 0;
136,456,346✔
1023
  if (pVnode) {
136,456,346✔
1024
    (void)taosThreadMutexLock(&pVnode->mutex);
136,456,346✔
1025

1026
    if (pVnode->inUse) {
136,456,346✔
1027
      *bufferSegmentUsed = pVnode->inUse->size;
136,447,249✔
1028
    }
1029
    *bufferSegmentSize = pVnode->config.szBuf / VNODE_BUFPOOL_SEGMENTS;
136,456,346✔
1030

1031
    (void)taosThreadMutexUnlock(&pVnode->mutex);
136,456,346✔
1032
  }
1033
}
136,456,346✔
1034

1035
int32_t vnodeGetLoad(SVnode *pVnode, SVnodeLoad *pLoad) {
136,456,346✔
1036
  SSyncState state = syncGetState(pVnode->sync);
136,456,346✔
1037
  pLoad->syncAppliedIndex = pVnode->state.applied;
136,456,346✔
1038
  syncGetCommitIndex(pVnode->sync, &pLoad->syncCommitIndex);
136,456,346✔
1039

1040
  pLoad->vgId = TD_VID(pVnode);
136,456,346✔
1041
  pLoad->syncState = state.state;
136,456,346✔
1042
  pLoad->syncRestore = state.restored;
136,456,346✔
1043
  pLoad->syncTerm = state.term;
136,456,346✔
1044
  pLoad->roleTimeMs = state.roleTimeMs;
136,456,346✔
1045
  pLoad->startTimeMs = state.startTimeMs;
136,456,346✔
1046
  pLoad->syncCanRead = state.canRead;
136,456,346✔
1047
  pLoad->cacheUsage = tsdbCacheGetUsage(pVnode);
136,456,346✔
1048
  pLoad->numOfCachedTables = tsdbCacheGetElems(pVnode);
136,456,346✔
1049
  VNODE_DO_META_QUERY(pVnode, pLoad->numOfTables = metaGetTbNum(pVnode->pMeta));
136,456,346✔
1050
  VNODE_DO_META_QUERY(pVnode, pLoad->numOfTimeSeries = metaGetTimeSeriesNum(pVnode->pMeta, 1));
136,456,346✔
1051
  pLoad->totalStorage = (int64_t)3 * 1073741824;  // TODO
136,456,346✔
1052
  (void)vnodeGetCompStorage(pVnode, &pLoad->compStorage);
136,456,346✔
1053
  pLoad->pointsWritten = 100;
136,456,346✔
1054
  pLoad->numOfSelectReqs = 1;
136,456,346✔
1055
  pLoad->numOfInsertReqs = atomic_load_64(&pVnode->statis.nInsert);
136,456,346✔
1056
  pLoad->numOfInsertSuccessReqs = atomic_load_64(&pVnode->statis.nInsertSuccess);
136,456,346✔
1057
  pLoad->numOfBatchInsertReqs = atomic_load_64(&pVnode->statis.nBatchInsert);
136,456,346✔
1058
  pLoad->numOfBatchInsertSuccessReqs = atomic_load_64(&pVnode->statis.nBatchInsertSuccess);
136,456,346✔
1059
  vnodeGetBufferInfo(pVnode, &pLoad->bufferSegmentUsed, &pLoad->bufferSegmentSize);
136,456,346✔
1060
  vDebug("vgId:%d, get vnode load, state:%s snapSeq:%d, learnerProgress:%d, totalIndex:%" PRId64, TD_VID(pVnode),
136,456,346✔
1061
         syncStr(state.state), state.snapSeq, state.progress, state.totalIndex);
1062
  pLoad->learnerProgress = state.progress;
136,456,346✔
1063
  pLoad->snapSeq = state.snapSeq;
136,456,346✔
1064
  pLoad->syncTotalIndex = state.totalIndex;
136,456,346✔
1065
  return 0;
136,456,346✔
1066
}
1067

1068
int32_t vnodeGetLoadLite(SVnode *pVnode, SVnodeLoadLite *pLoad) {
×
1069
  SSyncState syncState = syncGetState(pVnode->sync);
×
1070
  if (syncState.state == TAOS_SYNC_STATE_LEADER || syncState.state == TAOS_SYNC_STATE_ASSIGNED_LEADER) {
×
1071
    pLoad->vgId = TD_VID(pVnode);
×
1072
    pLoad->nTimeSeries = metaGetTimeSeriesNum(pVnode->pMeta, 1);
×
1073
    return 0;
×
1074
  }
1075
  return -1;
×
1076
}
1077
/**
1078
 * @brief Reset the statistics value by monitor interval
1079
 *
1080
 * @param pVnode
1081
 * @param pLoad
1082
 */
1083
void vnodeResetLoad(SVnode *pVnode, SVnodeLoad *pLoad) {
104✔
1084
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nInsert, pLoad->numOfInsertReqs, 64, "nInsert");
104✔
1085
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nInsertSuccess, pLoad->numOfInsertSuccessReqs, 64, "nInsertSuccess");
104✔
1086
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nBatchInsert, pLoad->numOfBatchInsertReqs, 64, "nBatchInsert");
104✔
1087
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nBatchInsertSuccess, pLoad->numOfBatchInsertSuccessReqs, 64,
104✔
1088
                            "nBatchInsertSuccess");
1089
}
104✔
1090

1091
void vnodeGetInfo(void *pVnode, const char **dbname, int32_t *vgId, int64_t *numOfTables, int64_t *numOfNormalTables) {
12,977,871✔
1092
  SVnode    *pVnodeObj = pVnode;
12,977,871✔
1093
  SVnodeCfg *pConf = &pVnodeObj->config;
12,977,871✔
1094

1095
  if (dbname) {
12,979,161✔
1096
    *dbname = pConf->dbname;
12,338,708✔
1097
  }
1098

1099
  if (vgId) {
12,978,751✔
1100
    *vgId = TD_VID(pVnodeObj);
12,141,886✔
1101
  }
1102

1103
  if (numOfTables) {
12,977,443✔
1104
    *numOfTables = pConf->vndStats.numOfNTables + pConf->vndStats.numOfCTables +
18,028✔
1105
                   pConf->vndStats.numOfVTables + pConf->vndStats.numOfVCTables;
9,014✔
1106
  }
1107

1108
  if (numOfNormalTables) {
12,977,443✔
1109
    *numOfNormalTables = pConf->vndStats.numOfNTables +
16,524✔
1110
                         pConf->vndStats.numOfVTables;
8,262✔
1111
  }
1112
}
12,977,443✔
1113

1114
int32_t vnodeGetTableList(void *pVnode, int8_t type, SArray *pList) {
8,262✔
1115
  if (type == TSDB_SUPER_TABLE) {
8,262✔
1116
    return vnodeGetStbIdList(pVnode, 0, pList);
8,262✔
1117
  } else {
1118
    return TSDB_CODE_INVALID_PARA;
×
1119
  }
1120
}
1121

1122
int32_t vnodeGetAllTableList(SVnode *pVnode, uint64_t uid, SArray *list) {
×
1123
  int32_t      code = TSDB_CODE_SUCCESS;
×
1124
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, uid, 1);
×
1125
  if (NULL == pCur) {
×
1126
    qError("vnode get all table list failed");
×
1127
    return terrno;
×
1128
  }
1129

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

1136
    STableKeyInfo info = {uid = id};
×
1137
    if (NULL == taosArrayPush(list, &info)) {
×
1138
      qError("taosArrayPush failed");
×
1139
      code = terrno;
×
1140
      goto _exit;
×
1141
    }
1142
  }
1143
_exit:
×
1144
  metaCloseCtbCursor(pCur);
×
1145
  return code;
×
1146
}
1147

1148
int32_t vnodeGetCtbIdListByFilter(SVnode *pVnode, int64_t suid, SArray *list, bool (*filter)(void *arg), void *arg) {
×
1149
  return 0;
×
1150
}
1151

1152
int32_t vnodeGetCtbIdList(void *pVnode, int64_t suid, SArray *list) {
110,692,136✔
1153
  int32_t      code = TSDB_CODE_SUCCESS;
110,692,136✔
1154
  SVnode      *pVnodeObj = pVnode;
110,692,136✔
1155
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnodeObj, suid, 1);
110,692,136✔
1156
  if (NULL == pCur) {
110,668,531✔
1157
    qError("vnode get all table list failed");
×
1158
    return terrno;
×
1159
  }
1160

1161
  while (1) {
465,377,684✔
1162
    tb_uid_t id = metaCtbCursorNext(pCur);
576,046,215✔
1163
    if (id == 0) {
576,047,386✔
1164
      break;
110,703,567✔
1165
    }
1166
    qTrace("vnodeGetCtbIdList: got ctb id %" PRId64 " for suid %" PRId64, id, suid);
465,343,819✔
1167
    if (NULL == taosArrayPush(list, &id)) {
465,386,187✔
1168
      qError("taosArrayPush failed");
×
1169
      code = terrno;
×
1170
      goto _exit;
×
1171
    }
1172
  }
1173

1174
_exit:
110,677,174✔
1175
  metaCloseCtbCursor(pCur);
110,677,174✔
1176
  return code;
110,663,635✔
1177
}
1178

1179
int32_t vnodeGetStbIdList(SVnode *pVnode, int64_t suid, SArray *list) {
64,645,257✔
1180
  int32_t      code = TSDB_CODE_SUCCESS;
64,645,257✔
1181
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, suid);
64,645,257✔
1182
  if (!pCur) {
64,646,489✔
1183
    return TSDB_CODE_OUT_OF_MEMORY;
×
1184
  }
1185

1186
  while (1) {
48,168,218✔
1187
    tb_uid_t id = metaStbCursorNext(pCur);
112,814,707✔
1188
    if (id == 0) {
112,812,008✔
1189
      break;
64,651,809✔
1190
    }
1191

1192
    if (NULL == taosArrayPush(list, &id)) {
48,168,218✔
1193
      qError("taosArrayPush failed");
×
1194
      code = terrno;
×
1195
      goto _exit;
×
1196
    }
1197
  }
1198

1199
_exit:
64,649,111✔
1200
  metaCloseStbCursor(pCur);
64,649,111✔
1201
  return code;
64,648,484✔
1202
}
1203

1204
int32_t vnodeGetStbIdListByFilter(SVnode *pVnode, int64_t suid, SArray *list, bool (*filter)(void *arg, void *arg1),
2,314✔
1205
                                  void *arg) {
1206
  int32_t      code = TSDB_CODE_SUCCESS;
2,314✔
1207
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, suid);
2,314✔
1208
  if (!pCur) {
2,314✔
1209
    return terrno;
×
1210
  }
1211

1212
  while (1) {
3,598✔
1213
    tb_uid_t id = metaStbCursorNext(pCur);
5,912✔
1214
    if (id == 0) {
5,912✔
1215
      break;
2,314✔
1216
    }
1217

1218
    if ((*filter) && (*filter)(arg, &id)) {
3,598✔
1219
      continue;
3,598✔
1220
    }
1221

1222
    if (NULL == taosArrayPush(list, &id)) {
×
1223
      qError("taosArrayPush failed");
×
1224
      code = terrno;
×
1225
      goto _exit;
×
1226
    }
1227
  }
1228

1229
_exit:
2,314✔
1230
  metaCloseStbCursor(pCur);
2,314✔
1231
  return code;
2,314✔
1232
}
1233

1234
int32_t vnodeGetCtbNum(SVnode *pVnode, int64_t suid, int64_t *num) {
4,546,302✔
1235
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, suid, 0);
4,546,302✔
1236
  if (!pCur) {
4,546,684✔
1237
    return terrno;
×
1238
  }
1239

1240
  *num = 0;
4,546,684✔
1241
  while (1) {
2,712,823✔
1242
    tb_uid_t id = metaCtbCursorNext(pCur);
7,259,157✔
1243
    if (id == 0) {
7,259,226✔
1244
      break;
4,546,925✔
1245
    }
1246

1247
    ++(*num);
2,712,301✔
1248
  }
1249

1250
  metaCloseCtbCursor(pCur);
4,546,925✔
1251
  return TSDB_CODE_SUCCESS;
4,546,373✔
1252
}
1253

1254
int32_t vnodeGetStbColumnNum(SVnode *pVnode, tb_uid_t suid, int *num) {
4,546,335✔
1255
  SSchemaWrapper *pSW = metaGetTableSchema(pVnode->pMeta, suid, -1, 0, NULL, 0);
4,546,335✔
1256
  if (pSW) {
4,545,970✔
1257
    *num = pSW->nCols;
4,545,855✔
1258
    tDeleteSchemaWrapper(pSW);
1259
  } else {
1260
    *num = 2;
115✔
1261
  }
1262

1263
  return TSDB_CODE_SUCCESS;
4,545,993✔
1264
}
1265

1266
int32_t vnodeGetStbInfo(SVnode *pVnode, tb_uid_t suid, int64_t *keep, int8_t *flags) {
4,546,302✔
1267
  SMetaReader mr = {0};
4,546,302✔
1268
  metaReaderDoInit(&mr, pVnode->pMeta, META_READER_NOLOCK);
4,547,590✔
1269

1270
  int32_t code = metaReaderGetTableEntryByUid(&mr, suid);
4,545,993✔
1271
  if (code == TSDB_CODE_SUCCESS) {
4,546,658✔
1272
    if (keep) *keep = mr.me.stbEntry.keep;
4,546,543✔
1273
    if (flags) *flags = mr.me.flags;
4,546,187✔
1274
  } else {
1275
    if (keep) *keep = 0;
115✔
1276
    if (flags) *flags = 0;
115✔
1277
  }
1278

1279
  metaReaderClear(&mr);
4,546,293✔
1280
  return TSDB_CODE_SUCCESS;
4,547,023✔
1281
}
1282

1283
#ifdef TD_ENTERPRISE
1284
const char *tkLogStb[] = {"cluster_info",
1285
                          "data_dir",
1286
                          "disk_free",
1287
                          "disk_total",
1288
                          "dnodes_info",
1289
                          "d_info",
1290
                          "explorer_sys",
1291
                          "grants_info",
1292
                          "keeper_monitor",
1293
                          "logs",
1294
                          "log_dir",
1295
                          "log_summary",
1296
                          "http_client_requests",
1297
                          "http_server_requests_idmp",
1298
                          "m_info",
1299
                          "taosadapter_restful_http_request_fail",
1300
                          "taosadapter_restful_http_request_in_flight",
1301
                          "taosadapter_restful_http_request_summary_milliseconds",
1302
                          "taosadapter_restful_http_request_total",
1303
                          "taosadapter_system_cpu_percent",
1304
                          "taosadapter_system_mem_percent",
1305
                          "temp_dir",
1306
                          "vgroups_info",
1307
                          "vnodes_role",
1308
                          "taosd_dnodes_status",
1309
                          "adapter_conn_pool",
1310
                          "taosd_vnodes_info",
1311
                          "taosd_dnodes_metrics",
1312
                          "taosd_vgroups_info",
1313
                          "taos_sql_req",
1314
                          "taosd_mnodes_info",
1315
                          "adapter_c_interface",
1316
                          "taosd_cluster_info",
1317
                          "taosd_sql_req",
1318
                          "taosd_dnodes_info",
1319
                          "adapter_requests",
1320
                          "taosd_write_metrics",
1321
                          "adapter_status",
1322
                          "taos_slow_sql",
1323
                          "taos_slow_sql_detail",
1324
                          "taosd_cluster_basic",
1325
                          "taosd_dnodes_data_dirs",
1326
                          "taosd_dnodes_log_dirs",
1327
                          "xnode_agent_activities",
1328
                          "xnode_task_activities",
1329
                          "xnode_task_metrics",
1330
                          "taosx_sys",
1331
                          "taosx_task_csv",
1332
                          "taosx_task_progress",
1333
                          "taosx_task_kinghist",
1334
                          "taosx_task_tdengine2",
1335
                          "taosx_task_tdengine3",
1336
                          "taosx_task_opc_da",
1337
                          "taosx_task_opc_ua",
1338
                          "taosx_task_kafka",
1339
                          "taosx_task_influxdb",
1340
                          "taosx_task_mqtt",
1341
                          "taosx_task_avevahistorian",
1342
                          "taosx_task_opentsdb",
1343
                          "taosx_task_mysql",
1344
                          "taosx_task_postgres",
1345
                          "taosx_task_oracle",
1346
                          "taosx_task_mssql",
1347
                          "taosx_task_mongodb",
1348
                          "taosx_task_sparkplugb",
1349
                          "taosx_task_orc",
1350
                          "taosx_task_pulsar",
1351
                          "taosx_task_pspace"};
1352
const char *tkAuditStb[] = {"operations"};
1353
const int   tkLogStbNum = ARRAY_SIZE(tkLogStb);
1354
const int   tkAuditStbNum = ARRAY_SIZE(tkAuditStb);
1355

1356
// exclude stbs of taoskeeper log
1357
static int32_t vnodeGetTimeSeriesBlackList(SVnode *pVnode, int32_t *tbSize) {
64,642,493✔
1358
  int32_t      code = TSDB_CODE_SUCCESS;
64,642,493✔
1359
  int32_t      tbNum = 0;
64,642,493✔
1360
  const char **pTbArr = NULL;
64,642,493✔
1361
  const char  *dbName = NULL;
64,642,493✔
1362
  *tbSize = 0;
64,642,493✔
1363

1364
  if (!(dbName = strchr(pVnode->config.dbname, '.'))) return 0;
64,641,163✔
1365
  if (0 == strncmp(++dbName, "log", TSDB_DB_NAME_LEN)) {
64,641,926✔
1366
    tbNum = tkLogStbNum;
3,498✔
1367
    pTbArr = (const char **)&tkLogStb;
3,498✔
1368
  } else if (0 == strncmp(dbName, "audit", TSDB_DB_NAME_LEN) || pVnode->config.isAudit) {
64,639,093✔
1369
    tbNum = tkAuditStbNum;
2,761✔
1370
    pTbArr = (const char **)&tkAuditStb;
2,761✔
1371
  }
1372
  if (tbNum && pTbArr) {
64,642,591✔
1373
    *tbSize = metaSizeOfTbFilterCache(pVnode->pMeta, 0);
6,259✔
1374
    if (*tbSize < tbNum) {
6,259✔
1375
      for (int32_t i = 0; i < tbNum; ++i) {
92,600✔
1376
        tb_uid_t suid = metaGetTableEntryUidByName(pVnode->pMeta, pTbArr[i]);
90,673✔
1377
        if (suid != 0) {
90,673✔
1378
          code = metaPutTbToFilterCache(pVnode->pMeta, &suid, 0);
4,246✔
1379
          if (TSDB_CODE_SUCCESS != code) {
4,246✔
1380
            return code;
2,490✔
1381
          }
1382
        }
1383
      }
1384
      *tbSize = metaSizeOfTbFilterCache(pVnode->pMeta, 0);
1,927✔
1385
    }
1386
  }
1387

1388
  return code;
64,638,106✔
1389
}
1390
#endif
1391

1392
static bool vnodeTimeSeriesFilter(void *arg1, void *arg2) {
3,598✔
1393
  SVnode *pVnode = (SVnode *)arg1;
3,598✔
1394

1395
  if (metaTbInFilterCache(pVnode->pMeta, arg2, 0)) {
3,598✔
1396
    return true;
3,598✔
1397
  }
1398
  return false;
×
1399
}
1400

1401
int32_t vnodeGetTimeSeriesNum(SVnode *pVnode, int64_t *num) {
64,642,493✔
1402
  SArray *suidList = NULL;
64,642,493✔
1403

1404
  if (!(suidList = taosArrayInit(1, sizeof(tb_uid_t)))) {
64,642,493✔
1405
    return terrno;
×
1406
  }
1407

1408
  int32_t tbFilterSize = 0;
64,638,503✔
1409
  int32_t code = TSDB_CODE_SUCCESS;
64,639,833✔
1410
#ifdef TD_ENTERPRISE
1411
  code = vnodeGetTimeSeriesBlackList(pVnode, &tbFilterSize);
64,639,833✔
1412
  if (TSDB_CODE_SUCCESS != code) {
64,640,350✔
1413
    goto _exit;
2,490✔
1414
  }
1415
#endif
1416

1417
  if ((!tbFilterSize && vnodeGetStbIdList(pVnode, 0, suidList) < 0) ||
64,637,860✔
1418
      (tbFilterSize && vnodeGetStbIdListByFilter(pVnode, 0, suidList, vnodeTimeSeriesFilter, pVnode) < 0)) {
64,638,108✔
1419
    qError("vgId:%d, failed to get stb id list error: %s", TD_VID(pVnode), terrstr());
×
1420
    taosArrayDestroy(suidList);
×
1421
    return terrno;
×
1422
  }
1423

1424
  *num = 0;
64,641,087✔
1425
  int64_t arrSize = taosArrayGetSize(suidList);
64,640,668✔
1426
  for (int64_t i = 0; i < arrSize; ++i) {
112,802,811✔
1427
    tb_uid_t suid = *(tb_uid_t *)taosArrayGet(suidList, i);
48,157,488✔
1428

1429
    int64_t ctbNum = 0;
48,155,693✔
1430
    int32_t numOfCols = 0;
48,160,148✔
1431
    int8_t  flags = 0;
48,159,483✔
1432
    code = metaGetStbStats(pVnode, suid, &ctbNum, &numOfCols, &flags);
48,156,158✔
1433
    if (TSDB_CODE_SUCCESS != code) {
48,167,463✔
1434
      goto _exit;
×
1435
    }
1436
    if (!TABLE_IS_VIRTUAL(flags)) {
48,167,463✔
1437
      *num += ctbNum * (numOfCols - 1);
45,422,058✔
1438
    }
1439
  }
1440

1441
_exit:
64,646,571✔
1442
  taosArrayDestroy(suidList);
64,647,813✔
1443
  return TSDB_CODE_SUCCESS;
64,643,158✔
1444
}
1445

1446
int32_t vnodeGetAllCtbNum(SVnode *pVnode, int64_t *num) {
×
1447
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, 0);
×
1448
  if (!pCur) {
×
1449
    return terrno;
×
1450
  }
1451

1452
  *num = 0;
×
1453
  while (1) {
×
1454
    tb_uid_t id = metaStbCursorNext(pCur);
×
1455
    if (id == 0) {
×
1456
      break;
×
1457
    }
1458

1459
    int64_t ctbNum = 0;
×
1460
    int32_t code = vnodeGetCtbNum(pVnode, id, &ctbNum);
×
1461
    if (TSDB_CODE_SUCCESS != code) {
×
1462
      metaCloseStbCursor(pCur);
×
1463
      return code;
×
1464
    }
1465

1466
    *num += ctbNum;
×
1467
  }
1468

1469
  metaCloseStbCursor(pCur);
×
1470
  return TSDB_CODE_SUCCESS;
×
1471
}
1472

1473
void *vnodeGetIdx(void *pVnode) {
4,338,321✔
1474
  if (pVnode == NULL) {
4,338,321✔
1475
    return NULL;
×
1476
  }
1477

1478
  return metaGetIdx(((SVnode *)pVnode)->pMeta);
4,338,321✔
1479
}
1480

1481
void *vnodeGetIvtIdx(void *pVnode) {
4,338,321✔
1482
  if (pVnode == NULL) {
4,338,321✔
1483
    return NULL;
×
1484
  }
1485
  return metaGetIvtIdx(((SVnode *)pVnode)->pMeta);
4,338,321✔
1486
}
1487

1488
int32_t vnodeGetTableSchema(void *pVnode, int64_t uid, STSchema **pSchema, int64_t *suid, SSchemaWrapper **pTagSchema) {
283,505✔
1489
  return tsdbGetTableSchema(((SVnode *)pVnode)->pMeta, uid, pSchema, suid, pTagSchema);
283,505✔
1490
}
1491

1492
static FORCE_INLINE int32_t vnodeGetDBPrimaryInfo(SVnode *pVnode, SDbSizeStatisInfo *pInfo) {
1493
  int32_t code = 0;
7,225,186✔
1494
  char    path[TSDB_FILENAME_LEN] = {0};
7,225,186✔
1495

1496
  char   *dirName[] = {VNODE_TSDB_DIR, VNODE_WAL_DIR, VNODE_META_DIR, VNODE_TSDB_CACHE_DIR};
7,225,186✔
1497
  int64_t dirSize[4];
7,223,064✔
1498

1499
  vnodeGetPrimaryPath(pVnode, false, path, TSDB_FILENAME_LEN);
7,225,186✔
1500
  int32_t offset = strlen(path);
7,225,186✔
1501

1502
  for (int i = 0; i < sizeof(dirName) / sizeof(dirName[0]); i++) {
36,126,476✔
1503
    int64_t size = {0};
28,901,108✔
1504
    (void)snprintf(path + offset, TSDB_FILENAME_LEN - offset, "%s%s", TD_DIRSEP, dirName[i]);
28,901,290✔
1505
    code = taosGetDirSize(path, &size);
28,901,290✔
1506
    if (code != 0) {
28,901,472✔
1507
      uWarn("vnode %d get dir %s %s size failed since %s", TD_VID(pVnode), path, dirName[i], tstrerror(code));
1,690✔
1508
    }
1509
    path[offset] = 0;
28,901,472✔
1510
    dirSize[i] = size;
28,901,290✔
1511
  }
1512

1513
  pInfo->l1Size = 0;
7,225,368✔
1514
  pInfo->walSize = dirSize[1];
7,225,368✔
1515
  pInfo->metaSize = dirSize[2];
7,225,368✔
1516
  pInfo->cacheSize = dirSize[3];
7,225,368✔
1517
  return code;
7,225,368✔
1518
}
1519
int32_t vnodeGetDBSize(void *pVnode, SDbSizeStatisInfo *pInfo) {
7,225,186✔
1520
  int32_t code = 0;
7,225,186✔
1521
  int32_t lino = 0;
7,225,186✔
1522
  SVnode *pVnodeObj = pVnode;
7,225,186✔
1523
  if (pVnodeObj == NULL) {
7,225,186✔
1524
    return TSDB_CODE_VND_NOT_EXIST;
×
1525
  }
1526
  code = vnodeGetDBPrimaryInfo(pVnode, pInfo);
7,225,368✔
1527
  if (code != 0) goto _exit;
7,225,368✔
1528

1529
  code = tsdbGetFsSize(pVnodeObj->pTsdb, pInfo);
7,225,368✔
1530
_exit:
7,225,368✔
1531
  return code;
7,225,368✔
1532
}
1533

1534
/*
1535
 * Get raw write metrics for a vnode
1536
 */
1537
int32_t vnodeGetRawWriteMetrics(void *pVnode, SRawWriteMetrics *pRawMetrics) {
×
1538
  if (pVnode == NULL || pRawMetrics == NULL) {
×
1539
    return TSDB_CODE_INVALID_PARA;
×
1540
  }
1541

1542
  SVnode      *pVnode1 = (SVnode *)pVnode;
×
1543
  SSyncMetrics syncMetrics = syncGetMetrics(pVnode1->sync);
×
1544

1545
  // Copy values following SRawWriteMetrics structure order
1546
  pRawMetrics->total_requests = atomic_load_64(&pVnode1->writeMetrics.total_requests);
×
1547
  pRawMetrics->total_rows = atomic_load_64(&pVnode1->writeMetrics.total_rows);
×
1548
  pRawMetrics->total_bytes = atomic_load_64(&pVnode1->writeMetrics.total_bytes);
×
1549
  pRawMetrics->fetch_batch_meta_time = atomic_load_64(&pVnode1->writeMetrics.fetch_batch_meta_time);
×
1550
  pRawMetrics->fetch_batch_meta_count = atomic_load_64(&pVnode1->writeMetrics.fetch_batch_meta_count);
×
1551
  pRawMetrics->preprocess_time = atomic_load_64(&pVnode1->writeMetrics.preprocess_time);
×
1552
  pRawMetrics->wal_write_bytes = atomic_load_64(&syncMetrics.wal_write_bytes);
×
1553
  pRawMetrics->wal_write_time = atomic_load_64(&syncMetrics.wal_write_time);
×
1554
  pRawMetrics->apply_bytes = atomic_load_64(&pVnode1->writeMetrics.apply_bytes);
×
1555
  pRawMetrics->apply_time = atomic_load_64(&pVnode1->writeMetrics.apply_time);
×
1556
  pRawMetrics->commit_count = atomic_load_64(&pVnode1->writeMetrics.commit_count);
×
1557
  pRawMetrics->commit_time = atomic_load_64(&pVnode1->writeMetrics.commit_time);
×
1558
  pRawMetrics->memtable_wait_time = atomic_load_64(&pVnode1->writeMetrics.memtable_wait_time);
×
1559
  pRawMetrics->blocked_commit_count = atomic_load_64(&pVnode1->writeMetrics.blocked_commit_count);
×
1560
  pRawMetrics->blocked_commit_time = atomic_load_64(&pVnode1->writeMetrics.block_commit_time);
×
1561
  pRawMetrics->merge_count = atomic_load_64(&pVnode1->writeMetrics.merge_count);
×
1562
  pRawMetrics->merge_time = atomic_load_64(&pVnode1->writeMetrics.merge_time);
×
1563
  pRawMetrics->last_cache_commit_time = atomic_load_64(&pVnode1->writeMetrics.last_cache_commit_time);
×
1564
  pRawMetrics->last_cache_commit_count = atomic_load_64(&pVnode1->writeMetrics.last_cache_commit_count);
×
1565

1566
  return 0;
×
1567
}
1568

1569
/*
1570
 * Reset raw write metrics for a vnode by subtracting old values
1571
 */
1572
int32_t vnodeResetRawWriteMetrics(void *pVnode, const SRawWriteMetrics *pOldMetrics) {
×
1573
  if (pVnode == NULL || pOldMetrics == NULL) {
×
1574
    return TSDB_CODE_INVALID_PARA;
×
1575
  }
1576

1577
  SVnode *pVnode1 = (SVnode *)pVnode;
×
1578

1579
  // Reset vnode write metrics using atomic operations to subtract old values
1580
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.total_requests, pOldMetrics->total_requests);
×
1581
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.total_rows, pOldMetrics->total_rows);
×
1582
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.total_bytes, pOldMetrics->total_bytes);
×
1583

1584
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.fetch_batch_meta_time, pOldMetrics->fetch_batch_meta_time);
×
1585
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.fetch_batch_meta_count, pOldMetrics->fetch_batch_meta_count);
×
1586
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.preprocess_time, pOldMetrics->preprocess_time);
×
1587
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.apply_bytes, pOldMetrics->apply_bytes);
×
1588
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.apply_time, pOldMetrics->apply_time);
×
1589
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.commit_count, pOldMetrics->commit_count);
×
1590

1591
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.commit_time, pOldMetrics->commit_time);
×
1592
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.merge_time, pOldMetrics->merge_time);
×
1593

1594
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.memtable_wait_time, pOldMetrics->memtable_wait_time);
×
1595
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.blocked_commit_count, pOldMetrics->blocked_commit_count);
×
1596
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.block_commit_time, pOldMetrics->blocked_commit_time);
×
1597
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.merge_count, pOldMetrics->merge_count);
×
1598

1599
  // Reset new cache metrics
1600
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.last_cache_commit_time, pOldMetrics->last_cache_commit_time);
×
1601
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.last_cache_commit_count, pOldMetrics->last_cache_commit_count);
×
1602

1603
  // Reset sync metrics
1604
  SSyncMetrics syncMetrics = {
×
1605
      .wal_write_bytes = pOldMetrics->wal_write_bytes,
×
1606
      .wal_write_time = pOldMetrics->wal_write_time,
×
1607
  };
1608
  syncResetMetrics(pVnode1->sync, &syncMetrics);
×
1609

1610
  return 0;
×
1611
}
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