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

taosdata / TDengine / #5029

21 Apr 2026 10:00AM UTC coverage: 73.003% (+0.02%) from 72.986%
#5029

push

travis-ci

web-flow
fix(tmq): balance vgroup error (#35183)

273843 of 375111 relevant lines covered (73.0%)

134074995.07 hits per line

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

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

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

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

36
int32_t fillTableColCmpr(SMetaReader *reader, SSchemaExt *pExt, int32_t numOfCol) {
22,445,721✔
37
  int8_t tblType = reader->me.type;
22,445,721✔
38
  if (withColCompress(tblType)) {
22,446,392✔
39
    SColCmprWrapper *p = &(reader->me.colCmpr);
22,300,663✔
40
    if (numOfCol != p->nCols) {
22,300,663✔
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++) {
257,102,116✔
45
      SColCmpr *pCmpr = &p->pColCmpr[i];
234,801,453✔
46
      pExt[i].colId = pCmpr->id;
234,799,202✔
47
      pExt[i].compress = pCmpr->alg;
234,800,149✔
48
    }
49
  }
50
  return 0;
22,445,721✔
51
}
52

53
void vnodeDebugTableMeta(STableMetaRsp *pMeta) {
22,380,016✔
54
  if (!(qDebugFlag & DEBUG_DEBUG)) {
22,380,016✔
55
    return;
394,057✔
56
  }
57

58
  qDebug("tbName:%s", pMeta->tbName);
21,985,959✔
59
  qDebug("stbName:%s", pMeta->stbName);
21,986,630✔
60
  qDebug("dbFName:%s", pMeta->dbFName);
21,986,630✔
61
  qDebug("dbId:%" PRId64, pMeta->dbId);
21,986,630✔
62
  qDebug("numOfTags:%d", pMeta->numOfTags);
21,986,630✔
63
  qDebug("numOfColumns:%d", pMeta->numOfColumns);
21,986,630✔
64
  qDebug("precision:%d", pMeta->precision);
21,986,630✔
65
  qDebug("tableType:%d", pMeta->tableType);
21,986,630✔
66
  qDebug("sversion:%d", pMeta->sversion);
21,986,630✔
67
  qDebug("tversion:%d", pMeta->tversion);
21,986,630✔
68
  qDebug("suid:%" PRIu64, pMeta->suid);
21,986,630✔
69
  qDebug("tuid:%" PRIu64, pMeta->tuid);
21,986,630✔
70
  qDebug("vgId:%d", pMeta->vgId);
21,986,630✔
71
  qDebug("sysInfo:%d", pMeta->sysInfo);
21,986,630✔
72
  if (pMeta->pSchemas) {
21,986,630✔
73
    for (int32_t i = 0; i < (pMeta->numOfColumns + pMeta->numOfTags); ++i) {
297,438,790✔
74
      SSchema *pSchema = pMeta->pSchemas + i;
275,452,160✔
75
      qDebug("%d col/tag: type:%d, flags:%d, colId:%d, bytes:%d, name:%s", i, pSchema->type, pSchema->flags,
275,452,160✔
76
             pSchema->colId, pSchema->bytes, pSchema->name);
77
    }
78
  }
79
}
80

81
int32_t fillTableColRef(SMetaReader *reader, SColRef *pRef, int32_t numOfCol) {
370,169✔
82
  int8_t tblType = reader->me.type;
370,169✔
83
  if (hasRefCol(tblType)) {
370,169✔
84
    SColRefWrapper *p = &(reader->me.colRef);
370,169✔
85
    if (numOfCol != p->nCols) {
370,169✔
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,864,751✔
90
      SColRef *pColRef = &p->pColRef[i];
2,494,582✔
91
      pRef[i].hasRef = pColRef->hasRef;
2,494,582✔
92
      pRef[i].id = pColRef->id;
2,494,582✔
93
      if(pRef[i].hasRef) {
2,494,582✔
94
        tstrncpy(pRef[i].refDbName, pColRef->refDbName, TSDB_DB_NAME_LEN);
1,751,599✔
95
        tstrncpy(pRef[i].refTableName, pColRef->refTableName, TSDB_TABLE_NAME_LEN);
1,751,599✔
96
        tstrncpy(pRef[i].refColName, pColRef->refColName, TSDB_COL_NAME_LEN);
1,751,599✔
97
      }
98
    }
99
  }
100
  return 0;
370,169✔
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) {
32,103,222✔
126
  STableInfoReq  infoReq = {0};
32,103,222✔
127
  STableMetaRsp  metaRsp = {0};
32,103,222✔
128
  SMetaReader    mer1 = {0};
32,103,222✔
129
  SMetaReader    mer2 = {0};
32,103,222✔
130
  char           tableFName[TSDB_TABLE_FNAME_LEN];
32,100,337✔
131
  bool           reqTbUid = false;
32,103,222✔
132
  SRpcMsg        rpcMsg = {0};
32,103,222✔
133
  int32_t        code = 0;
32,103,222✔
134
  int32_t        rspLen = 0;
32,103,222✔
135
  void          *pRsp = NULL;
32,103,222✔
136
  SSchemaWrapper schema = {0};
32,103,222✔
137
  SSchemaWrapper schemaTag = {0};
32,103,222✔
138
  uint8_t        autoCreateCtb = 0;
32,103,222✔
139

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

147
  if (infoReq.option == REQ_OPT_TBUID) reqTbUid = true;
32,103,893✔
148
  metaRsp.dbId = pVnode->config.dbId;
32,103,893✔
149
  tstrncpy(metaRsp.tbName, infoReq.tbName, TSDB_TABLE_NAME_LEN);
32,103,222✔
150
  (void)memcpy(metaRsp.dbFName, infoReq.dbFName, sizeof(metaRsp.dbFName));
32,103,222✔
151

152
  if (!reqTbUid) {
32,103,222✔
153
    (void)snprintf(tableFName, TSDB_TABLE_FNAME_LEN, "%s.%s", infoReq.dbFName, infoReq.tbName);
31,860,707✔
154
    if (pVnode->mounted) tTrimMountPrefix(tableFName);
31,860,707✔
155
    code = vnodeValidateTableHash(pVnode, tableFName);
31,861,378✔
156
    if (code) {
31,861,378✔
157
      goto _exit4;
×
158
    }
159
  }
160

161
  // query meta
162
  metaReaderDoInit(&mer1, pVnode->pMeta, META_READER_LOCK);
32,103,893✔
163
  if (reqTbUid) {
32,103,222✔
164
    SET_ERRNO(0);
242,515✔
165
    uint64_t tbUid = taosStr2UInt64(infoReq.tbName, NULL, 10);
242,515✔
166
    if (ERRNO == ERANGE || tbUid == 0) {
242,515✔
167
      code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
168
      goto _exit3;
×
169
    }
170
    SMetaReader mr3 = {0};
242,515✔
171
    metaReaderDoInit(&mr3, ((SVnode *)pVnode)->pMeta, META_READER_NOLOCK);
242,515✔
172
    if ((code = metaReaderGetTableEntryByUid(&mr3, tbUid)) < 0) {
242,515✔
173
      metaReaderClear(&mr3);
87,269✔
174
      TAOS_CHECK_GOTO(code, NULL, _exit3);
87,269✔
175
    }
176
    tstrncpy(metaRsp.tbName, mr3.me.name, TSDB_TABLE_NAME_LEN);
155,246✔
177
    metaReaderClear(&mr3);
155,246✔
178
    TAOS_CHECK_GOTO(metaGetTableEntryByName(&mer1, metaRsp.tbName), NULL, _exit3);
155,246✔
179
  } else if (metaGetTableEntryByName(&mer1, infoReq.tbName) < 0) {
31,860,707✔
180
    code = terrno;
9,635,937✔
181
    goto _exit3;
9,635,937✔
182
  }
183

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

189
  switch (mer1.me.type) {
22,379,912✔
190
    case TSDB_SUPER_TABLE: {
3,981,620✔
191
      tstrncpy(metaRsp.stbName, mer1.me.name, sizeof(metaRsp.stbName));
3,981,620✔
192
      schema = mer1.me.stbEntry.schemaRow;
3,981,620✔
193
      schemaTag = mer1.me.stbEntry.schemaTag;
3,981,620✔
194
      metaRsp.suid = mer1.me.uid;
3,981,620✔
195
      metaRsp.virtualStb = TABLE_IS_VIRTUAL(mer1.me.flags);
3,981,620✔
196
      metaRsp.ownerId = mer1.me.stbEntry.ownerId;
3,981,620✔
197
      break;
3,981,620✔
198
    }
199
    case TSDB_CHILD_TABLE:
13,858,341✔
200
    case TSDB_VIRTUAL_CHILD_TABLE:{
201
      metaReaderDoInit(&mer2, pVnode->pMeta, META_READER_NOLOCK);
13,858,341✔
202
      if (metaReaderGetTableEntryByUid(&mer2, mer1.me.ctbEntry.suid) < 0) goto _exit2;
13,858,445✔
203

204
      tstrncpy(metaRsp.stbName, mer2.me.name, sizeof(metaRsp.stbName));
13,858,445✔
205
      metaRsp.suid = mer2.me.uid;
13,858,445✔
206
      metaRsp.ownerId = mer2.me.stbEntry.ownerId;  // child table inherits ownerId from stb
13,858,445✔
207
      schema = mer2.me.stbEntry.schemaRow;
13,858,445✔
208
      schemaTag = mer2.me.stbEntry.schemaTag;
13,858,445✔
209
      break;
13,858,445✔
210
    }
211
    case TSDB_NORMAL_TABLE:
4,539,951✔
212
    case TSDB_VIRTUAL_NORMAL_TABLE: {
213
      schema = mer1.me.ntbEntry.schemaRow;
4,539,951✔
214
      metaRsp.ownerId = mer1.me.ntbEntry.ownerId;
4,539,951✔
215
      break;
4,539,951✔
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;
22,380,016✔
224
  metaRsp.numOfColumns = schema.nCols;
22,380,016✔
225
  metaRsp.precision = pVnode->config.tsdbCfg.precision;
22,380,016✔
226
  metaRsp.sversion = schema.version;
22,380,016✔
227
  metaRsp.tversion = schemaTag.version;
22,380,016✔
228
  metaRsp.pSchemas = (SSchema *)taosMemoryMalloc(sizeof(SSchema) * (metaRsp.numOfColumns + metaRsp.numOfTags));
22,380,016✔
229
  metaRsp.pSchemaExt = (SSchemaExt *)taosMemoryCalloc(metaRsp.numOfColumns, sizeof(SSchemaExt));
22,379,345✔
230
  if (NULL == metaRsp.pSchemas || NULL == metaRsp.pSchemaExt) {
22,380,687✔
231
    code = terrno;
671✔
232
    goto _exit;
×
233
  }
234
  (void)memcpy(metaRsp.pSchemas, schema.pSchema, sizeof(SSchema) * schema.nCols);
22,380,016✔
235
  if (schemaTag.nCols) {
22,380,016✔
236
    (void)memcpy(metaRsp.pSchemas + schema.nCols, schemaTag.pSchema, sizeof(SSchema) * schemaTag.nCols);
17,840,065✔
237
  }
238
  if (metaRsp.pSchemaExt) {
22,380,016✔
239
    SMetaReader *pReader =
22,380,687✔
240
        (mer1.me.type == TSDB_CHILD_TABLE || mer1.me.type == TSDB_VIRTUAL_CHILD_TABLE) ? &mer2 : &mer1;
22,380,687✔
241
    code = fillTableColCmpr(pReader, metaRsp.pSchemaExt, metaRsp.numOfColumns);
22,380,687✔
242
    if (code < 0) {
22,380,016✔
243
      goto _exit;
×
244
    }
245
    for (int32_t i = 0; i < metaRsp.numOfColumns; i++) {
258,096,186✔
246
      metaRsp.pSchemaExt[i].colId = schema.pSchema[i].colId;
235,717,907✔
247
      if (pReader->me.pExtSchemas) {
235,717,907✔
248
        metaRsp.pSchemaExt[i].typeMod = pReader->me.pExtSchemas[i].typeMod;
35,597,439✔
249
      }
250
    }
251
  } else {
252
    code = TSDB_CODE_OUT_OF_MEMORY;
×
253
    goto _exit;
×
254
  }
255
  if (hasRefCol(mer1.me.type)) {
22,378,279✔
256
    metaRsp.rversion = mer1.me.colRef.version;
370,840✔
257
    metaRsp.pColRefs = (SColRef*)taosMemoryMalloc(sizeof(SColRef) * metaRsp.numOfColumns);
370,840✔
258
    if (metaRsp.pColRefs) {
370,169✔
259
      code = fillTableColRef(&mer1, metaRsp.pColRefs, metaRsp.numOfColumns);
370,169✔
260
      if (code < 0) {
370,169✔
261
        goto _exit;
×
262
      }
263
    }
264
    metaRsp.numOfColRefs = metaRsp.numOfColumns;
370,169✔
265

266
    // Fill tag references
267
    if (mer1.me.colRef.nTagRefs > 0) {
370,169✔
268
      metaRsp.pTagRefs = (SColRef*)taosMemoryMalloc(sizeof(SColRef) * mer1.me.colRef.nTagRefs);
×
269
      if (metaRsp.pTagRefs) {
×
270
        code = fillTableTagRef(&mer1, metaRsp.pTagRefs, mer1.me.colRef.nTagRefs);
×
271
        if (code < 0) {
×
272
          taosMemoryFreeClear(metaRsp.pTagRefs);
×
273
          goto _exit;
×
274
        }
275
      } else {
276
        code = terrno;
×
277
        goto _exit;
×
278
      }
279
      metaRsp.numOfTagRefs = mer1.me.colRef.nTagRefs;
×
280
    } else {
281
      metaRsp.pTagRefs = NULL;
370,169✔
282
      metaRsp.numOfTagRefs = 0;
370,169✔
283
    }
284
  } else {
285
    metaRsp.pColRefs = NULL;
22,009,847✔
286
    metaRsp.numOfColRefs = 0;
22,009,847✔
287
    metaRsp.pTagRefs = NULL;
22,009,847✔
288
    metaRsp.numOfTagRefs = 0;
22,009,847✔
289
  }
290

291
  vnodeDebugTableMeta(&metaRsp);
22,380,016✔
292

293
  // encode and send response
294
  rspLen = tSerializeSTableMetaRsp(NULL, 0, &metaRsp);
22,380,687✔
295
  if (rspLen < 0) {
22,380,016✔
296
    code = terrno;
×
297
    goto _exit;
×
298
  }
299

300
  if (direct) {
22,380,016✔
301
    pRsp = rpcMallocCont(rspLen);
540,019✔
302
  } else {
303
    pRsp = taosMemoryCalloc(1, rspLen);
21,839,997✔
304
  }
305

306
  if (pRsp == NULL) {
22,379,345✔
307
    code = terrno;
×
308
    goto _exit;
×
309
  }
310

311
  rspLen = tSerializeSTableMetaRsp(pRsp, rspLen, &metaRsp);
22,379,345✔
312
  if (rspLen < 0) {
22,380,016✔
313
    code = terrno;
×
314
    goto _exit;
×
315
  }
316

317
_exit:
22,379,345✔
318
  taosMemoryFree(metaRsp.pColRefs);
22,380,016✔
319
  taosMemoryFree(metaRsp.pSchemas);
22,380,016✔
320
  taosMemoryFree(metaRsp.pSchemaExt);
22,380,016✔
321
  taosMemoryFree(metaRsp.pTagRefs);
22,380,016✔
322
_exit2:
22,380,016✔
323
  metaReaderClear(&mer2);
22,380,687✔
324
_exit3:
32,103,893✔
325
  metaReaderClear(&mer1);
32,103,893✔
326
_exit4:
32,103,893✔
327
  rpcMsg.info = pMsg->info;
32,103,893✔
328
  rpcMsg.pCont = pRsp;
32,103,893✔
329
  rpcMsg.contLen = rspLen;
32,103,893✔
330
  rpcMsg.code = code;
32,103,893✔
331
  rpcMsg.msgType = pMsg->msgType;
32,103,893✔
332

333
  if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST && autoCreateCtb == 1) {
32,103,222✔
334
    code = TSDB_CODE_SUCCESS;
5,669,005✔
335
  }
336

337
  if (code) {
32,103,222✔
338
    qError("vgId:%d, get table %s meta with %" PRIu8 " failed cause of %s", pVnode->config.vgId, infoReq.tbName,
4,054,201✔
339
           infoReq.option, tstrerror(code));
340
  }
341

342
  if (direct) {
32,103,222✔
343
    tmsgSendRsp(&rpcMsg);
1,329,286✔
344
  } else {
345
    *pMsg = rpcMsg;
30,773,936✔
346
  }
347

348
  return code;
32,103,222✔
349
}
350

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

364
  // decode req
365
  if (tDeserializeSTableCfgReq(pMsg->pCont, pMsg->contLen, &cfgReq) != 0) {
65,705✔
366
    code = terrno;
×
367
    goto _exit;
×
368
  }
369

370
  tstrncpy(cfgRsp.tbName, cfgReq.tbName, TSDB_TABLE_NAME_LEN);
65,705✔
371
  (void)memcpy(cfgRsp.dbFName, cfgReq.dbFName, sizeof(cfgRsp.dbFName));
65,705✔
372

373
  (void)snprintf(tableFName, TSDB_TABLE_FNAME_LEN, "%s.%s", cfgReq.dbFName, cfgReq.tbName);
65,705✔
374
  if (pVnode->mounted) tTrimMountPrefix(tableFName);
65,705✔
375
  code = vnodeValidateTableHash(pVnode, tableFName);
65,705✔
376
  if (code) {
65,705✔
377
    goto _exit;
×
378
  }
379

380
  // query meta
381
  metaReaderDoInit(&mer1, pVnode->pMeta, META_READER_LOCK);
65,705✔
382

383
  if (metaGetTableEntryByName(&mer1, cfgReq.tbName) < 0) {
65,705✔
384
    code = terrno;
×
385
    goto _exit;
×
386
  }
387

388
  cfgRsp.tableType = mer1.me.type;
65,705✔
389
  cfgRsp.isAudit = pVnode->config.isAudit ? 1 : 0;
65,705✔
390
  cfgRsp.secureDelete = pVnode->config.secureDelete;
65,705✔
391

392
  if (mer1.me.type == TSDB_SUPER_TABLE) {
65,705✔
393
    code = TSDB_CODE_VND_HASH_MISMATCH;
×
394
    goto _exit;
×
395
  } else if (mer1.me.type == TSDB_CHILD_TABLE || mer1.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
65,705✔
396
    metaReaderDoInit(&mer2, pVnode->pMeta, META_READER_NOLOCK);
46,109✔
397
    if (metaReaderGetTableEntryByUid(&mer2, mer1.me.ctbEntry.suid) < 0) goto _exit;
46,109✔
398

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

438
  cfgRsp.numOfTags = schemaTag.nCols;
65,705✔
439
  cfgRsp.numOfColumns = schema.nCols;
65,705✔
440
  cfgRsp.virtualStb = false; // vnode don't have super table, so it's always false
65,705✔
441
  cfgRsp.pSchemas = (SSchema *)taosMemoryMalloc(sizeof(SSchema) * (cfgRsp.numOfColumns + cfgRsp.numOfTags));
65,705✔
442
  cfgRsp.pSchemaExt = (SSchemaExt *)taosMemoryCalloc(cfgRsp.numOfColumns, sizeof(SSchemaExt));
65,705✔
443
  cfgRsp.pColRefs = (SColRef *)taosMemoryMalloc(sizeof(SColRef) * cfgRsp.numOfColumns);
65,705✔
444
  cfgRsp.numOfTagRefs = 0;
65,705✔
445
  cfgRsp.pTagRefs = NULL;
65,705✔
446

447
  if (NULL == cfgRsp.pSchemas || NULL == cfgRsp.pSchemaExt || NULL == cfgRsp.pColRefs) {
65,705✔
448
    code = terrno;
×
449
    goto _exit;
×
450
  }
451
  (void)memcpy(cfgRsp.pSchemas, schema.pSchema, sizeof(SSchema) * schema.nCols);
65,705✔
452
  if (schemaTag.nCols) {
65,705✔
453
    (void)memcpy(cfgRsp.pSchemas + schema.nCols, schemaTag.pSchema, sizeof(SSchema) * schemaTag.nCols);
46,109✔
454
  }
455

456
  SMetaReader    *pReader = (mer1.me.type == TSDB_CHILD_TABLE || mer1.me.type == TSDB_VIRTUAL_CHILD_TABLE) ? &mer2 : &mer1;
65,705✔
457
  SColRefWrapper *pColRef = &mer1.me.colRef;
65,705✔
458

459
  if (withExtSchema(cfgRsp.tableType)) {
65,705✔
460
    code = fillTableColCmpr(pReader, cfgRsp.pSchemaExt, cfgRsp.numOfColumns);
65,705✔
461
    if (code < 0) {
65,705✔
462
      goto _exit;
×
463
    }
464

465
    for (int32_t i = 0; i < cfgRsp.numOfColumns; i++) {
361,103✔
466
      SSchemaExt *pSchExt = cfgRsp.pSchemaExt + i;
295,398✔
467
      pSchExt->colId = schema.pSchema[i].colId;
295,398✔
468
      if (pReader->me.pExtSchemas) {
295,398✔
469
        pSchExt->typeMod = pReader->me.pExtSchemas[i].typeMod;
41,241✔
470
      }
471
    }
472
  }
473

474
  cfgRsp.virtualStb = false;
65,705✔
475
  if (hasRefCol(cfgRsp.tableType)) {
65,705✔
476
    for (int32_t i = 0; i < cfgRsp.numOfColumns; i++) {
123,205✔
477
      SColRef *pRef = &pColRef->pColRef[i];
113,220✔
478
      cfgRsp.pColRefs[i].hasRef = pRef->hasRef;
113,220✔
479
      cfgRsp.pColRefs[i].id = pRef->id;
113,220✔
480
      if (cfgRsp.pColRefs[i].hasRef) {
113,220✔
481
        tstrncpy(cfgRsp.pColRefs[i].refDbName, pRef->refDbName, TSDB_DB_NAME_LEN);
61,091✔
482
        tstrncpy(cfgRsp.pColRefs[i].refTableName, pRef->refTableName, TSDB_TABLE_NAME_LEN);
61,091✔
483
        tstrncpy(cfgRsp.pColRefs[i].refColName, pRef->refColName, TSDB_COL_NAME_LEN);
61,091✔
484
      }
485
    }
486

487
    cfgRsp.numOfTagRefs = pColRef->nTagRefs;
9,985✔
488
    if (cfgRsp.numOfTagRefs > 0) {
9,985✔
489
      if (NULL == pColRef->pTagRef) {
×
490
        code = TSDB_CODE_APP_ERROR;
×
491
        goto _exit;
×
492
      }
493
      cfgRsp.pTagRefs = (SColRef *)taosMemoryMalloc(sizeof(SColRef) * cfgRsp.numOfTagRefs);
×
494
      if (NULL == cfgRsp.pTagRefs) {
×
495
        code = terrno;
×
496
        goto _exit;
×
497
      }
498

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

512
  // encode and send response
513
  rspLen = tSerializeSTableCfgRsp(NULL, 0, &cfgRsp);
65,705✔
514
  if (rspLen < 0) {
65,705✔
515
    code = terrno;
×
516
    goto _exit;
×
517
  }
518

519
  if (direct) {
65,705✔
520
    pRsp = rpcMallocCont(rspLen);
×
521
  } else {
522
    pRsp = taosMemoryCalloc(1, rspLen);
65,705✔
523
  }
524

525
  if (pRsp == NULL) {
65,705✔
526
    code = terrno;
×
527
    goto _exit;
×
528
  }
529

530
  rspLen = tSerializeSTableCfgRsp(pRsp, rspLen, &cfgRsp);
65,705✔
531
  if (rspLen < 0) {
65,705✔
532
    code = terrno;
×
533
    goto _exit;
×
534
  }
535

536
_exit:
65,705✔
537
  rpcMsg.info = pMsg->info;
65,705✔
538
  rpcMsg.pCont = pRsp;
65,705✔
539
  rpcMsg.contLen = rspLen;
65,705✔
540
  rpcMsg.code = code;
65,705✔
541
  rpcMsg.msgType = pMsg->msgType;
65,705✔
542

543
  if (code) {
65,705✔
544
    qError("get table %s cfg failed cause of %s", cfgReq.tbName, tstrerror(code));
×
545
  }
546

547
  if (direct) {
65,705✔
548
    tmsgSendRsp(&rpcMsg);
×
549
  } else {
550
    *pMsg = rpcMsg;
65,705✔
551
  }
552

553
  tFreeSTableCfgRsp(&cfgRsp);
65,705✔
554
  metaReaderClear(&mer2);
65,705✔
555
  metaReaderClear(&mer1);
65,705✔
556
  return code;
65,705✔
557
}
558

559
static FORCE_INLINE void vnodeFreeSBatchRspMsg(void *p) {
560
  if (NULL == p) {
561
    return;
562
  }
563

564
  SBatchRspMsg *pRsp = (SBatchRspMsg *)p;
565
  rpcFreeCont(pRsp->msg);
566
}
567

568
int32_t vnodeGetBatchMeta(SVnode *pVnode, SRpcMsg *pMsg) {
29,470,591✔
569
  int32_t      code = 0;
29,470,591✔
570
  int32_t      rspSize = 0;
29,470,591✔
571
  SBatchReq    batchReq = {0};
29,470,591✔
572
  SBatchMsg   *req = NULL;
29,472,359✔
573
  SBatchRspMsg rsp = {0};
29,472,359✔
574
  SBatchRsp    batchRsp = {0};
29,456,715✔
575
  SRpcMsg      reqMsg = *pMsg;
29,456,709✔
576
  SRpcMsg      rspMsg = {0};
29,458,013✔
577
  void        *pRsp = NULL;
29,462,241✔
578

579
  if (tDeserializeSBatchReq(pMsg->pCont, pMsg->contLen, &batchReq)) {
29,462,241✔
580
    code = terrno;
×
581
    qError("tDeserializeSBatchReq failed");
×
582
    goto _exit;
×
583
  }
584

585
  int32_t msgNum = taosArrayGetSize(batchReq.pMsgs);
29,471,224✔
586
  if (msgNum >= MAX_META_MSG_IN_BATCH) {
29,466,853✔
587
    code = TSDB_CODE_INVALID_MSG;
×
588
    qError("too many msgs %d in vnode batch meta req", msgNum);
×
589
    goto _exit;
×
590
  }
591

592
  batchRsp.pRsps = taosArrayInit(msgNum, sizeof(SBatchRspMsg));
29,466,853✔
593
  if (NULL == batchRsp.pRsps) {
29,457,581✔
594
    code = terrno;
×
595
    qError("taosArrayInit %d SBatchRspMsg failed", msgNum);
×
596
    goto _exit;
×
597
  }
598

599
  for (int32_t i = 0; i < msgNum; ++i) {
64,931,754✔
600
    req = taosArrayGet(batchReq.pMsgs, i);
35,466,913✔
601
    if (req == NULL) {
35,461,642✔
602
      code = terrno;
×
603
      goto _exit;
×
604
    }
605

606
    reqMsg.msgType = req->msgType;
35,461,642✔
607
    reqMsg.pCont = req->msg;
35,461,119✔
608
    reqMsg.contLen = req->msgLen;
35,456,896✔
609

610
    switch (req->msgType) {
35,457,561✔
611
      case TDMT_VND_TABLE_META:
30,532,071✔
612
        // error code has been set into reqMsg, no need to handle it here.
613
        if (TSDB_CODE_SUCCESS != vnodeGetTableMeta(pVnode, &reqMsg, false)) {
30,532,071✔
614
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
3,177,665✔
615
        }
616
        break;
30,531,421✔
617
      case TDMT_VND_TABLE_NAME:
242,515✔
618
        // error code has been set into reqMsg, no need to handle it here.
619
        if (TSDB_CODE_SUCCESS != vnodeGetTableMeta(pVnode, &reqMsg, false)) {
242,515✔
620
          qWarn("vnodeGetBatchName failed, msgType:%d", req->msgType);
87,269✔
621
        }
622
        break;
242,515✔
623
      case TDMT_VND_TABLE_CFG:
65,705✔
624
        // error code has been set into reqMsg, no need to handle it here.
625
        if (TSDB_CODE_SUCCESS != vnodeGetTableCfg(pVnode, &reqMsg, false)) {
65,705✔
626
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
×
627
        }
628
        break;
65,705✔
629
      case TDMT_VND_VSUBTABLES_META:
×
630
        // error code has been set into reqMsg, no need to handle it here.
631
        if (TSDB_CODE_SUCCESS != vnodeGetVSubtablesMeta(pVnode, &reqMsg)) {
×
632
          qWarn("vnodeGetVSubtablesMeta failed, msgType:%d", req->msgType);
×
633
        }
634
        break;
×
635
      case TDMT_VND_VSTB_REF_DBS:
4,626,620✔
636
        // error code has been set into reqMsg, no need to handle it here.
637
        if (TSDB_CODE_SUCCESS != vnodeGetVStbRefDbs(pVnode, &reqMsg)) {
4,626,620✔
638
          qWarn("vnodeGetVStbRefDbs failed, msgType:%d", req->msgType);
×
639
        }
640
        break;
4,621,334✔
641
      default:
×
642
        qError("invalid req msgType %d", req->msgType);
×
643
        reqMsg.code = TSDB_CODE_INVALID_MSG;
×
644
        reqMsg.pCont = NULL;
×
645
        reqMsg.contLen = 0;
×
646
        break;
×
647
    }
648

649
    rsp.msgIdx = req->msgIdx;
35,460,975✔
650
    rsp.reqType = reqMsg.msgType;
35,465,483✔
651
    rsp.msgLen = reqMsg.contLen;
35,465,483✔
652
    rsp.rspCode = reqMsg.code;
35,465,483✔
653
    rsp.msg = reqMsg.pCont;
35,465,483✔
654

655
    if (NULL == taosArrayPush(batchRsp.pRsps, &rsp)) {
70,926,520✔
656
      qError("taosArrayPush failed");
×
657
      code = terrno;
×
658
      goto _exit;
×
659
    }
660
  }
661

662
  rspSize = tSerializeSBatchRsp(NULL, 0, &batchRsp);
29,464,841✔
663
  if (rspSize < 0) {
29,467,507✔
664
    qError("tSerializeSBatchRsp failed");
×
665
    code = terrno;
×
666
    goto _exit;
×
667
  }
668
  pRsp = rpcMallocCont(rspSize);
29,467,507✔
669
  if (pRsp == NULL) {
29,457,055✔
670
    qError("rpcMallocCont %d failed", rspSize);
×
671
    code = terrno;
×
672
    goto _exit;
×
673
  }
674
  if (tSerializeSBatchRsp(pRsp, rspSize, &batchRsp) < 0) {
29,457,055✔
675
    qError("tSerializeSBatchRsp %d failed", rspSize);
×
676
    code = terrno;
×
677
    goto _exit;
×
678
  }
679

680
_exit:
29,469,321✔
681

682
  rspMsg.info = pMsg->info;
29,471,032✔
683
  rspMsg.pCont = pRsp;
29,468,421✔
684
  rspMsg.contLen = rspSize;
29,468,421✔
685
  rspMsg.code = code;
29,468,421✔
686
  rspMsg.msgType = pMsg->msgType;
29,468,421✔
687

688
  if (code) {
29,453,257✔
689
    qError("vnd get batch meta failed cause of %s", tstrerror(code));
×
690
  }
691

692
  taosArrayDestroyEx(batchReq.pMsgs, tFreeSBatchReqMsg);
29,453,257✔
693
  taosArrayDestroyEx(batchRsp.pRsps, tFreeSBatchRspMsg);
29,461,303✔
694

695
  tmsgSendRsp(&rspMsg);
29,442,358✔
696

697
  return code;
29,470,139✔
698
}
699

700
#define VNODE_DO_META_QUERY(pVnode, cmd)                 \
701
  do {                                                   \
702
    (void)taosThreadRwlockRdlock(&(pVnode)->metaRWLock); \
703
    cmd;                                                 \
704
    (void)taosThreadRwlockUnlock(&(pVnode)->metaRWLock); \
705
  } while (0)
706

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

717
  SArray *pList = taosArrayInit(10, sizeof(uint64_t));
×
718
  QUERY_CHECK_NULL(pList, code, line, _return, terrno);
×
719

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

722
  size_t num = taosArrayGetSize(pList);
×
723
  *ppRes = taosArrayInit(num, POINTER_BYTES);
×
724
  QUERY_CHECK_NULL(*ppRes, code, line, _return, terrno);
×
725
  pSrcTbls = tSimpleHashInit(10, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
×
726
  QUERY_CHECK_NULL(pSrcTbls, code, line, _return, terrno);
×
727

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

735
    refColsNum = 0;
×
736
    for (int32_t j = 0; j < mr.me.colRef.nCols; j++) {
×
737
      if (mr.me.colRef.pColRef[j].hasRef) {
×
738
        refColsNum++;
×
739
      }
740
    }
741

742
    if (refColsNum <= 0) {
×
743
      pHandle->api.metaReaderFn.clearReader(&mr);
×
744
      readerInit = false;
×
745
      continue;
×
746
    }
747

748
    pTb = taosMemoryCalloc(1, refColsNum * sizeof(SRefColInfo) + sizeof(*pTb));
×
749
    QUERY_CHECK_NULL(pTb, code, line, _return, terrno);
×
750

751
    pTb->uid = mr.me.uid;
×
752
    pTb->numOfColRefs = refColsNum;
×
753
    pTb->refCols = (SRefColInfo*)(pTb + 1);
×
754

755
    refColsNum = 0;
×
756
    tSimpleHashClear(pSrcTbls);
×
757
    for (int32_t j = 0; j < mr.me.colRef.nCols; j++) {
×
758
      if (!mr.me.colRef.pColRef[j].hasRef) {
×
759
        continue;
×
760
      }
761

762
      pTb->refCols[refColsNum].colId = mr.me.colRef.pColRef[j].id;
×
763
      tstrncpy(pTb->refCols[refColsNum].refColName, mr.me.colRef.pColRef[j].refColName, TSDB_COL_NAME_LEN);
×
764
      tstrncpy(pTb->refCols[refColsNum].refTableName, mr.me.colRef.pColRef[j].refTableName, TSDB_TABLE_NAME_LEN);
×
765
      tstrncpy(pTb->refCols[refColsNum].refDbName, mr.me.colRef.pColRef[j].refDbName, TSDB_DB_NAME_LEN);
×
766

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

769
      if (NULL == tSimpleHashGet(pSrcTbls, tbFName, strlen(tbFName))) {
×
770
        QUERY_CHECK_CODE(tSimpleHashPut(pSrcTbls, tbFName, strlen(tbFName), &code, sizeof(code)), line, _return);
×
771
      }
772

773
      refColsNum++;
×
774
    }
775

776
    pTb->numOfSrcTbls = tSimpleHashGetSize(pSrcTbls);
×
777
    QUERY_CHECK_NULL(taosArrayPush(*ppRes, &pTb), code, line, _return, terrno);
×
778
    pTb = NULL;
×
779

780
    pHandle->api.metaReaderFn.clearReader(&mr);
×
781
    readerInit = false;
×
782
  }
783

784
_return:
×
785

786
  if (readerInit) {
×
787
    pHandle->api.metaReaderFn.clearReader(&mr);
×
788
  }
789

790
  taosArrayDestroy(pList);
×
791
  taosMemoryFree(pTb);
×
792
  tSimpleHashCleanup(pSrcTbls);
×
793

794
  if (code) {
×
795
    qError("%s failed since %s", __func__, tstrerror(code));
×
796
  }
797
  return code;
×
798
}
799

800
int32_t vnodeReadVStbRefDbs(SReadHandle* pHandle, int64_t suid, SArray** ppRes) {
4,623,493✔
801
  int32_t                    code = TSDB_CODE_SUCCESS;
4,623,493✔
802
  int32_t                    line = 0;
4,623,493✔
803
  SMetaReader                mr = {0};
4,623,493✔
804
  bool                       readerInit = false;
4,622,296✔
805
  SSHashObj*                 pDbNameHash = NULL;
4,622,296✔
806
  SArray*                    pList = NULL;
4,622,296✔
807

808
  pList = taosArrayInit(10, sizeof(uint64_t));
4,622,296✔
809
  QUERY_CHECK_NULL(pList, code, line, _return, terrno);
4,617,631✔
810

811
  *ppRes = taosArrayInit(10, POINTER_BYTES);
4,617,631✔
812
  QUERY_CHECK_NULL(*ppRes, code, line, _return, terrno)
4,617,289✔
813
  
814
  // lookup in cache
815
  code = pHandle->api.metaFn.metaGetCachedRefDbs(pHandle->vnode, suid, *ppRes);
4,616,544✔
816
  QUERY_CHECK_CODE(code, line, _return);
4,630,199✔
817

818
  if (taosArrayGetSize(*ppRes) > 0) {
4,630,199✔
819
    // found in cache
820
    goto _return;
4,531,031✔
821
  } else {
822
    code = pHandle->api.metaFn.getChildTableList(pHandle->vnode, suid, pList);
98,559✔
823
    QUERY_CHECK_CODE(code, line, _return);
98,559✔
824

825
    size_t num = taosArrayGetSize(pList);
98,559✔
826
    pDbNameHash = tSimpleHashInit(10, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
98,559✔
827
    QUERY_CHECK_NULL(pDbNameHash, code, line, _return, terrno);
98,559✔
828

829
    for (int32_t i = 0; i < num; ++i) {
512,528✔
830
      uint64_t* id = taosArrayGet(pList, i);
413,969✔
831
      QUERY_CHECK_NULL(id, code, line, _return, terrno);
413,969✔
832

833
      pHandle->api.metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pHandle->api.metaFn);
413,969✔
834
      readerInit = true;
413,336✔
835

836
      code = pHandle->api.metaReaderFn.getTableEntryByUid(&mr, *id);
413,336✔
837
      QUERY_CHECK_CODE(code, line, _return);
413,969✔
838

839
      for (int32_t j = 0; j < mr.me.colRef.nCols; j++) {
40,494,225✔
840
        if (mr.me.colRef.pColRef[j].hasRef) {
40,079,623✔
841
          if (NULL == tSimpleHashGet(pDbNameHash, mr.me.colRef.pColRef[j].refDbName, strlen(mr.me.colRef.pColRef[j].refDbName))) {
39,233,764✔
842
            char *refDbName = taosStrdup(mr.me.colRef.pColRef[j].refDbName);
88,212✔
843
            QUERY_CHECK_NULL(refDbName, code, line, _return, terrno);
88,212✔
844

845
            QUERY_CHECK_NULL(taosArrayPush(*ppRes, &refDbName), code, line, _return, terrno);
176,424✔
846

847
            code = tSimpleHashPut(pDbNameHash, refDbName, strlen(refDbName), NULL, 0);
88,212✔
848
            QUERY_CHECK_CODE(code, line, _return);
88,212✔
849
          }
850
        }
851
      }
852

853
      pHandle->api.metaReaderFn.clearReader(&mr);
414,602✔
854
      readerInit = false;
413,969✔
855
    }
856

857
    code = pHandle->api.metaFn.metaPutRefDbsToCache(pHandle->vnode, suid, *ppRes);
98,559✔
858
    QUERY_CHECK_CODE(code, line, _return);
98,559✔
859
  }
860

861
_return:
4,629,590✔
862

863
  if (readerInit) {
4,629,045✔
864
    pHandle->api.metaReaderFn.clearReader(&mr);
×
865
  }
866

867
  taosArrayDestroy(pList);
4,629,045✔
868
  tSimpleHashCleanup(pDbNameHash);
4,627,256✔
869

870
  if (code) {
4,627,882✔
871
    qError("%s failed since %s", __func__, tstrerror(code));
×
872
  }
873
  return code;
4,627,882✔
874
}
875

876
int32_t vnodeGetVSubtablesMeta(SVnode *pVnode, SRpcMsg *pMsg) {
×
877
  int32_t        code = 0;
×
878
  int32_t        rspSize = 0;
×
879
  SVSubTablesReq req = {0};
×
880
  SVSubTablesRsp rsp = {0};
×
881
  SRpcMsg      rspMsg = {0};
×
882
  void        *pRsp = NULL;
×
883
  int32_t      line = 0;
×
884

885
  if (tDeserializeSVSubTablesReq(pMsg->pCont, pMsg->contLen, &req)) {
×
886
    code = terrno;
×
887
    qError("tDeserializeSVSubTablesReq failed");
×
888
    goto _return;
×
889
  }
890

891
  SReadHandle handle = {0};
×
892
  handle.vnode = pVnode;
×
893
  initStorageAPI(&handle.api);
×
894

895
  QUERY_CHECK_CODE(vnodeReadVSubtables(&handle, req.suid, &rsp.pTables), line, _return);
×
896
  rsp.vgId = TD_VID(pVnode);
×
897

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

917
_return:
×
918

919
  rspMsg.info = pMsg->info;
×
920
  rspMsg.pCont = pRsp;
×
921
  rspMsg.contLen = rspSize;
×
922
  rspMsg.code = code;
×
923
  rspMsg.msgType = pMsg->msgType;
×
924

925
  if (code) {
×
926
    qError("vnd get virtual subtables failed cause of %s", tstrerror(code));
×
927
  }
928

929
  *pMsg = rspMsg;
×
930
  
931
  tDestroySVSubTablesRsp(&rsp);
×
932

933
  //tmsgSendRsp(&rspMsg);
934

935
  return code;
×
936
}
937

938
int32_t vnodeGetVStbRefDbs(SVnode *pVnode, SRpcMsg *pMsg) {
4,625,313✔
939
  int32_t        code = 0;
4,625,313✔
940
  int32_t        rspSize = 0;
4,625,313✔
941
  SVStbRefDbsReq req = {0};
4,625,313✔
942
  SVStbRefDbsRsp rsp = {0};
4,624,618✔
943
  SRpcMsg        rspMsg = {0};
4,603,473✔
944
  void          *pRsp = NULL;
4,603,473✔
945
  int32_t        line = 0;
4,603,473✔
946

947
  if (tDeserializeSVStbRefDbsReq(pMsg->pCont, pMsg->contLen, &req)) {
4,603,473✔
948
    code = terrno;
×
949
    qError("tDeserializeSVSubTablesReq failed");
×
950
    goto _return;
×
951
  }
952

953
  SReadHandle handle = {0};
4,619,569✔
954
  handle.vnode = pVnode;
4,624,675✔
955
  initStorageAPI(&handle.api);
4,624,675✔
956

957
  code = vnodeReadVStbRefDbs(&handle, req.suid, &rsp.pDbs);
4,615,901✔
958
  QUERY_CHECK_CODE(code, line, _return);
4,627,213✔
959
  rsp.vgId = TD_VID(pVnode);
4,627,213✔
960

961
  rspSize = tSerializeSVStbRefDbsRsp(NULL, 0, &rsp);
4,628,500✔
962
  if (rspSize < 0) {
4,617,939✔
963
    code = rspSize;
×
964
    qError("tSerializeSVStbRefDbsRsp failed, error:%d", rspSize);
×
965
    goto _return;
×
966
  }
967
  pRsp = taosMemoryCalloc(1, rspSize);
4,617,939✔
968
  if (pRsp == NULL) {
4,613,479✔
969
    code = terrno;
×
970
    qError("rpcMallocCont %d failed, error:%d", rspSize, terrno);
×
971
    goto _return;
×
972
  }
973
  rspSize = tSerializeSVStbRefDbsRsp(pRsp, rspSize, &rsp);
4,613,479✔
974
  if (rspSize < 0) {
4,625,195✔
975
    code = rspSize;
×
976
    qError("tSerializeSVStbRefDbsRsp failed, error:%d", rspSize);
×
977
    goto _return;
×
978
  }
979

980
_return:
4,625,195✔
981

982
  rspMsg.info = pMsg->info;
4,623,962✔
983
  rspMsg.pCont = pRsp;
4,623,324✔
984
  rspMsg.contLen = rspSize;
4,623,324✔
985
  rspMsg.code = code;
4,623,324✔
986
  rspMsg.msgType = pMsg->msgType;
4,623,324✔
987

988
  if (code) {
4,625,890✔
989
    qError("vnd get virtual stb ref db failed cause of %s", tstrerror(code));
×
990
  }
991

992
  *pMsg = rspMsg;
4,625,890✔
993

994
  tDestroySVStbRefDbsRsp(&rsp);
4,616,035✔
995

996
  return code;
4,622,076✔
997
}
998

999
static int32_t vnodeGetCompStorage(SVnode *pVnode, int64_t *output) {
154,831,573✔
1000
  int32_t code = 0;
154,831,573✔
1001
#ifdef TD_ENTERPRISE
1002
  int32_t now = taosGetTimestampSec();
154,831,573✔
1003
  if (llabs(now - pVnode->config.vndStats.storageLastUpd) >= 30) {
154,831,573✔
1004
    pVnode->config.vndStats.storageLastUpd = now;
7,970,948✔
1005

1006
    SDbSizeStatisInfo info = {0};
7,970,948✔
1007
    if (0 == (code = vnodeGetDBSize(pVnode, &info))) {
7,970,948✔
1008
      int64_t compSize =
7,970,948✔
1009
          info.l1Size + info.l2Size + info.l3Size + info.cacheSize + info.walSize + info.metaSize + +info.ssSize;
7,970,948✔
1010
      if (compSize >= 0) {
7,970,948✔
1011
        pVnode->config.vndStats.compStorage = compSize;
7,970,948✔
1012
      } else {
1013
        vError("vnode get comp storage failed since compSize is negative:%" PRIi64, compSize);
×
1014
        code = TSDB_CODE_APP_ERROR;
×
1015
      }
1016
    } else {
1017
      vWarn("vnode get comp storage failed since %s", tstrerror(code));
×
1018
    }
1019
  }
1020
  if (output) *output = pVnode->config.vndStats.compStorage;
154,831,573✔
1021
#endif
1022
  return code;
154,831,573✔
1023
}
1024

1025
static void vnodeGetBufferInfo(SVnode *pVnode, int64_t *bufferSegmentUsed, int64_t *bufferSegmentSize) {
154,831,573✔
1026
  *bufferSegmentUsed = 0;
154,831,573✔
1027
  *bufferSegmentSize = 0;
154,831,573✔
1028
  if (pVnode) {
154,831,573✔
1029
    (void)taosThreadMutexLock(&pVnode->mutex);
154,831,573✔
1030

1031
    if (pVnode->inUse) {
154,831,573✔
1032
      *bufferSegmentUsed = pVnode->inUse->size;
154,821,835✔
1033
    }
1034
    *bufferSegmentSize = pVnode->config.szBuf / VNODE_BUFPOOL_SEGMENTS;
154,831,573✔
1035

1036
    (void)taosThreadMutexUnlock(&pVnode->mutex);
154,831,573✔
1037
  }
1038
}
154,831,573✔
1039

1040
int32_t vnodeGetLoad(SVnode *pVnode, SVnodeLoad *pLoad) {
154,831,573✔
1041
  SSyncState state = syncGetState(pVnode->sync);
154,831,573✔
1042
  pLoad->syncAppliedIndex = pVnode->state.applied;
154,831,573✔
1043
  syncGetCommitIndex(pVnode->sync, &pLoad->syncCommitIndex);
154,831,573✔
1044

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

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

1096
void vnodeGetInfo(void *pVnode, const char **dbname, int32_t *vgId, int64_t *numOfTables, int64_t *numOfNormalTables) {
22,894,170✔
1097
  SVnode    *pVnodeObj = pVnode;
22,894,170✔
1098
  SVnodeCfg *pConf = &pVnodeObj->config;
22,894,170✔
1099

1100
  if (dbname) {
22,895,427✔
1101
    *dbname = pConf->dbname;
22,236,412✔
1102
  }
1103

1104
  if (vgId) {
22,895,427✔
1105
    *vgId = TD_VID(pVnodeObj);
21,821,233✔
1106
  }
1107

1108
  if (numOfTables) {
22,894,229✔
1109
    *numOfTables = pConf->vndStats.numOfNTables + pConf->vndStats.numOfCTables +
18,584✔
1110
                   pConf->vndStats.numOfVTables + pConf->vndStats.numOfVCTables;
9,292✔
1111
  }
1112

1113
  if (numOfNormalTables) {
22,894,229✔
1114
    *numOfNormalTables = pConf->vndStats.numOfNTables +
16,988✔
1115
                         pConf->vndStats.numOfVTables;
8,494✔
1116
  }
1117
}
22,894,229✔
1118

1119
int32_t vnodeGetTableList(void *pVnode, int8_t type, SArray *pList) {
8,494✔
1120
  if (type == TSDB_SUPER_TABLE) {
8,494✔
1121
    return vnodeGetStbIdList(pVnode, 0, pList);
8,494✔
1122
  } else {
1123
    return TSDB_CODE_INVALID_PARA;
×
1124
  }
1125
}
1126

1127
int32_t vnodeGetAllTableList(SVnode *pVnode, uint64_t uid, SArray *list) {
×
1128
  int32_t      code = TSDB_CODE_SUCCESS;
×
1129
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, uid, 1);
×
1130
  if (NULL == pCur) {
×
1131
    qError("vnode get all table list failed");
×
1132
    return terrno;
×
1133
  }
1134

1135
  while (1) {
×
1136
    tb_uid_t id = metaCtbCursorNext(pCur);
×
1137
    if (id == 0) {
×
1138
      break;
×
1139
    }
1140

1141
    STableKeyInfo info = {uid = id};
×
1142
    if (NULL == taosArrayPush(list, &info)) {
×
1143
      qError("taosArrayPush failed");
×
1144
      code = terrno;
×
1145
      goto _exit;
×
1146
    }
1147
  }
1148
_exit:
×
1149
  metaCloseCtbCursor(pCur);
×
1150
  return code;
×
1151
}
1152

1153
int32_t vnodeGetCtbIdListByFilter(SVnode *pVnode, int64_t suid, SArray *list, bool (*filter)(void *arg), void *arg) {
×
1154
  return 0;
×
1155
}
1156

1157
int32_t vnodeGetCtbIdList(void *pVnode, int64_t suid, SArray *list) {
110,024,509✔
1158
  int32_t      code = TSDB_CODE_SUCCESS;
110,024,509✔
1159
  SVnode      *pVnodeObj = pVnode;
110,024,509✔
1160
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnodeObj, suid, 1);
110,024,509✔
1161
  if (NULL == pCur) {
110,015,940✔
1162
    qError("vnode get all table list failed");
×
1163
    return terrno;
×
1164
  }
1165

1166
  while (1) {
471,618,017✔
1167
    tb_uid_t id = metaCtbCursorNext(pCur);
581,633,957✔
1168
    if (id == 0) {
581,642,519✔
1169
      break;
110,032,615✔
1170
    }
1171
    qTrace("vnodeGetCtbIdList: got ctb id %" PRId64 " for suid %" PRId64, id, suid);
471,609,904✔
1172
    if (NULL == taosArrayPush(list, &id)) {
471,632,920✔
1173
      qError("taosArrayPush failed");
×
1174
      code = terrno;
×
1175
      goto _exit;
×
1176
    }
1177
  }
1178

1179
_exit:
110,018,086✔
1180
  metaCloseCtbCursor(pCur);
110,018,086✔
1181
  return code;
109,978,253✔
1182
}
1183

1184
int32_t vnodeGetStbIdList(SVnode *pVnode, int64_t suid, SArray *list) {
70,697,482✔
1185
  int32_t      code = TSDB_CODE_SUCCESS;
70,697,482✔
1186
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, suid);
70,697,482✔
1187
  if (!pCur) {
70,698,848✔
1188
    return TSDB_CODE_OUT_OF_MEMORY;
×
1189
  }
1190

1191
  while (1) {
53,360,289✔
1192
    tb_uid_t id = metaStbCursorNext(pCur);
124,059,137✔
1193
    if (id == 0) {
124,056,405✔
1194
      break;
70,699,531✔
1195
    }
1196

1197
    if (NULL == taosArrayPush(list, &id)) {
53,357,370✔
1198
      qError("taosArrayPush failed");
×
1199
      code = terrno;
×
1200
      goto _exit;
×
1201
    }
1202
  }
1203

1204
_exit:
70,692,575✔
1205
  metaCloseStbCursor(pCur);
70,692,575✔
1206
  return code;
70,694,750✔
1207
}
1208

1209
int32_t vnodeGetStbIdListByFilter(SVnode *pVnode, int64_t suid, SArray *list, bool (*filter)(void *arg, void *arg1),
5,341✔
1210
                                  void *arg) {
1211
  int32_t      code = TSDB_CODE_SUCCESS;
5,341✔
1212
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, suid);
5,341✔
1213
  if (!pCur) {
5,341✔
1214
    return terrno;
×
1215
  }
1216

1217
  while (1) {
6,659✔
1218
    tb_uid_t id = metaStbCursorNext(pCur);
12,000✔
1219
    if (id == 0) {
12,000✔
1220
      break;
5,341✔
1221
    }
1222

1223
    if ((*filter) && (*filter)(arg, &id)) {
6,659✔
1224
      continue;
6,659✔
1225
    }
1226

1227
    if (NULL == taosArrayPush(list, &id)) {
×
1228
      qError("taosArrayPush failed");
×
1229
      code = terrno;
×
1230
      goto _exit;
×
1231
    }
1232
  }
1233

1234
_exit:
5,341✔
1235
  metaCloseStbCursor(pCur);
5,341✔
1236
  return code;
5,341✔
1237
}
1238

1239
int32_t vnodeGetCtbNum(SVnode *pVnode, int64_t suid, int64_t *num) {
4,893,534✔
1240
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, suid, 0);
4,893,534✔
1241
  if (!pCur) {
4,894,824✔
1242
    return terrno;
×
1243
  }
1244

1245
  *num = 0;
4,894,824✔
1246
  while (1) {
2,938,976✔
1247
    tb_uid_t id = metaCtbCursorNext(pCur);
7,834,090✔
1248
    if (id == 0) {
7,834,039✔
1249
      break;
4,895,103✔
1250
    }
1251

1252
    ++(*num);
2,938,936✔
1253
  }
1254

1255
  metaCloseCtbCursor(pCur);
4,895,103✔
1256
  return TSDB_CODE_SUCCESS;
4,895,114✔
1257
}
1258

1259
int32_t vnodeGetStbColumnNum(SVnode *pVnode, tb_uid_t suid, int *num) {
4,895,114✔
1260
  SSchemaWrapper *pSW = metaGetTableSchema(pVnode->pMeta, suid, -1, 0, NULL, 0, false);
4,895,114✔
1261
  if (pSW) {
4,895,154✔
1262
    *num = pSW->nCols;
4,895,154✔
1263
    tDeleteSchemaWrapper(pSW);
1264
  } else {
1265
    *num = 2;
×
1266
  }
1267

1268
  return TSDB_CODE_SUCCESS;
4,895,484✔
1269
}
1270

1271
int32_t vnodeGetStbInfo(SVnode *pVnode, tb_uid_t suid, int64_t *keep, int8_t *flags) {
4,895,484✔
1272
  SMetaReader mr = {0};
4,895,484✔
1273
  metaReaderDoInit(&mr, pVnode->pMeta, META_READER_NOLOCK);
4,895,484✔
1274

1275
  int32_t code = metaReaderGetTableEntryByUid(&mr, suid);
4,895,804✔
1276
  if (code == TSDB_CODE_SUCCESS) {
4,895,107✔
1277
    if (keep) *keep = mr.me.stbEntry.keep;
4,895,107✔
1278
    if (flags) *flags = mr.me.flags;
4,895,107✔
1279
  } else {
1280
    if (keep) *keep = 0;
×
1281
    if (flags) *flags = 0;
×
1282
  }
1283

1284
  metaReaderClear(&mr);
4,895,107✔
1285
  return TSDB_CODE_SUCCESS;
4,895,107✔
1286
}
1287

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

1361
// exclude stbs of taoskeeper log
1362
static int32_t vnodeGetTimeSeriesBlackList(SVnode *pVnode, int32_t *tbSize) {
70,697,394✔
1363
  int32_t      code = TSDB_CODE_SUCCESS;
70,697,394✔
1364
  int32_t      tbNum = 0;
70,697,394✔
1365
  const char **pTbArr = NULL;
70,697,394✔
1366
  const char  *dbName = NULL;
70,697,394✔
1367
  *tbSize = 0;
70,697,394✔
1368

1369
  if (!(dbName = strchr(pVnode->config.dbname, '.'))) return 0;
70,698,760✔
1370
  if (0 == strncmp(++dbName, "log", TSDB_DB_NAME_LEN)) {
70,698,077✔
1371
    tbNum = tkLogStbNum;
3,928✔
1372
    pTbArr = (const char **)&tkLogStb;
3,928✔
1373
  } else if (0 == strncmp(dbName, "audit", TSDB_DB_NAME_LEN) || pVnode->config.isAudit) {
70,694,832✔
1374
    tbNum = tkAuditStbNum;
5,272✔
1375
    pTbArr = (const char **)&tkAuditStb;
5,272✔
1376
  }
1377
  if (tbNum && pTbArr) {
70,699,443✔
1378
    *tbSize = metaSizeOfTbFilterCache(pVnode->pMeta, 0);
9,200✔
1379
    if (*tbSize < tbNum) {
9,200✔
1380
      for (int32_t i = 0; i < tbNum; ++i) {
96,982✔
1381
        tb_uid_t suid = metaGetTableEntryUidByName(pVnode->pMeta, pTbArr[i]);
95,389✔
1382
        if (suid != 0) {
95,389✔
1383
          code = metaPutTbToFilterCache(pVnode->pMeta, &suid, 0);
4,912✔
1384
          if (TSDB_CODE_SUCCESS != code) {
4,912✔
1385
            return code;
2,930✔
1386
          }
1387
        }
1388
      }
1389
      *tbSize = metaSizeOfTbFilterCache(pVnode->pMeta, 0);
1,593✔
1390
    }
1391
  }
1392

1393
  return code;
70,693,781✔
1394
}
1395
#endif
1396

1397
static bool vnodeTimeSeriesFilter(void *arg1, void *arg2) {
6,659✔
1398
  SVnode *pVnode = (SVnode *)arg1;
6,659✔
1399

1400
  if (metaTbInFilterCache(pVnode->pMeta, arg2, 0)) {
6,659✔
1401
    return true;
6,659✔
1402
  }
1403
  return false;
×
1404
}
1405

1406
int32_t vnodeGetTimeSeriesNum(SVnode *pVnode, int64_t *num) {
70,697,394✔
1407
  SArray *suidList = NULL;
70,697,394✔
1408

1409
  if (!(suidList = taosArrayInit(1, sizeof(tb_uid_t)))) {
70,697,394✔
1410
    return terrno;
×
1411
  }
1412

1413
  int32_t tbFilterSize = 0;
70,697,394✔
1414
  int32_t code = TSDB_CODE_SUCCESS;
70,697,394✔
1415
#ifdef TD_ENTERPRISE
1416
  code = vnodeGetTimeSeriesBlackList(pVnode, &tbFilterSize);
70,697,394✔
1417
  if (TSDB_CODE_SUCCESS != code) {
70,695,345✔
1418
    goto _exit;
2,930✔
1419
  }
1420
#endif
1421

1422
  if ((!tbFilterSize && vnodeGetStbIdList(pVnode, 0, suidList) < 0) ||
70,692,415✔
1423
      (tbFilterSize && vnodeGetStbIdListByFilter(pVnode, 0, suidList, vnodeTimeSeriesFilter, pVnode) < 0)) {
70,690,366✔
1424
    qError("vgId:%d, failed to get stb id list error: %s", TD_VID(pVnode), terrstr());
×
1425
    taosArrayDestroy(suidList);
×
1426
    return terrno;
×
1427
  }
1428

1429
  *num = 0;
70,691,049✔
1430
  int64_t arrSize = taosArrayGetSize(suidList);
70,694,464✔
1431
  for (int64_t i = 0; i < arrSize; ++i) {
124,040,180✔
1432
    tb_uid_t suid = *(tb_uid_t *)taosArrayGet(suidList, i);
53,344,476✔
1433

1434
    int64_t ctbNum = 0;
53,345,842✔
1435
    int32_t numOfCols = 0;
53,345,159✔
1436
    int8_t  flags = 0;
53,342,984✔
1437
    code = metaGetStbStats(pVnode, suid, &ctbNum, &numOfCols, &flags);
53,343,110✔
1438
    if (TSDB_CODE_SUCCESS != code) {
53,350,623✔
1439
      goto _exit;
×
1440
    }
1441
    if (!TABLE_IS_VIRTUAL(flags)) {
53,350,623✔
1442
      *num += ctbNum * (numOfCols - 1);
47,612,601✔
1443
    }
1444
  }
1445

1446
_exit:
70,697,304✔
1447
  taosArrayDestroy(suidList);
70,699,443✔
1448
  return TSDB_CODE_SUCCESS;
70,696,711✔
1449
}
1450

1451
int32_t vnodeGetAllCtbNum(SVnode *pVnode, int64_t *num) {
×
1452
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, 0);
×
1453
  if (!pCur) {
×
1454
    return terrno;
×
1455
  }
1456

1457
  *num = 0;
×
1458
  while (1) {
×
1459
    tb_uid_t id = metaStbCursorNext(pCur);
×
1460
    if (id == 0) {
×
1461
      break;
×
1462
    }
1463

1464
    int64_t ctbNum = 0;
×
1465
    int32_t code = vnodeGetCtbNum(pVnode, id, &ctbNum);
×
1466
    if (TSDB_CODE_SUCCESS != code) {
×
1467
      metaCloseStbCursor(pCur);
×
1468
      return code;
×
1469
    }
1470

1471
    *num += ctbNum;
×
1472
  }
1473

1474
  metaCloseStbCursor(pCur);
×
1475
  return TSDB_CODE_SUCCESS;
×
1476
}
1477

1478
void *vnodeGetIdx(void *pVnode) {
4,437,332✔
1479
  if (pVnode == NULL) {
4,437,332✔
1480
    return NULL;
×
1481
  }
1482

1483
  return metaGetIdx(((SVnode *)pVnode)->pMeta);
4,437,332✔
1484
}
1485

1486
void *vnodeGetIvtIdx(void *pVnode) {
4,437,332✔
1487
  if (pVnode == NULL) {
4,437,332✔
1488
    return NULL;
×
1489
  }
1490
  return metaGetIvtIdx(((SVnode *)pVnode)->pMeta);
4,437,332✔
1491
}
1492

1493
int32_t vnodeGetTableSchema(void *pVnode, int64_t uid, STSchema **pSchema, int64_t *suid, SSchemaWrapper **pTagSchema) {
531,948✔
1494
  return tsdbGetTableSchema(((SVnode *)pVnode)->pMeta, uid, pSchema, suid, pTagSchema);
531,948✔
1495
}
1496

1497
static FORCE_INLINE int32_t vnodeGetDBPrimaryInfo(SVnode *pVnode, SDbSizeStatisInfo *pInfo) {
1498
  int32_t code = 0;
7,977,386✔
1499
  char    path[TSDB_FILENAME_LEN] = {0};
7,977,386✔
1500

1501
  char   *dirName[] = {VNODE_TSDB_DIR, VNODE_WAL_DIR, VNODE_META_DIR, VNODE_TSDB_CACHE_DIR};
7,977,386✔
1502
  int64_t dirSize[4];
7,975,399✔
1503

1504
  vnodeGetPrimaryPath(pVnode, false, path, TSDB_FILENAME_LEN);
7,977,386✔
1505
  int32_t offset = strlen(path);
7,977,386✔
1506

1507
  for (int i = 0; i < sizeof(dirName) / sizeof(dirName[0]); i++) {
39,886,930✔
1508
    int64_t size = {0};
31,909,544✔
1509
    (void)snprintf(path + offset, TSDB_FILENAME_LEN - offset, "%s%s", TD_DIRSEP, dirName[i]);
31,909,544✔
1510
    code = taosGetDirSize(path, &size);
31,909,544✔
1511
    if (code != 0) {
31,909,544✔
1512
      uWarn("vnode %d get dir %s %s size failed since %s", TD_VID(pVnode), path, dirName[i], tstrerror(code));
1,300✔
1513
    }
1514
    path[offset] = 0;
31,909,544✔
1515
    dirSize[i] = size;
31,909,544✔
1516
  }
1517

1518
  pInfo->l1Size = 0;
7,977,386✔
1519
  pInfo->walSize = dirSize[1];
7,977,386✔
1520
  pInfo->metaSize = dirSize[2];
7,977,386✔
1521
  pInfo->cacheSize = dirSize[3];
7,977,386✔
1522
  return code;
7,977,386✔
1523
}
1524
int32_t vnodeGetDBSize(void *pVnode, SDbSizeStatisInfo *pInfo) {
7,977,386✔
1525
  int32_t code = 0;
7,977,386✔
1526
  int32_t lino = 0;
7,977,386✔
1527
  SVnode *pVnodeObj = pVnode;
7,977,386✔
1528
  if (pVnodeObj == NULL) {
7,977,386✔
1529
    return TSDB_CODE_VND_NOT_EXIST;
×
1530
  }
1531
  code = vnodeGetDBPrimaryInfo(pVnode, pInfo);
7,977,386✔
1532
  if (code != 0) goto _exit;
7,977,386✔
1533

1534
  code = tsdbGetFsSize(pVnodeObj->pTsdb, pInfo);
7,977,386✔
1535
_exit:
7,977,386✔
1536
  return code;
7,977,386✔
1537
}
1538

1539
/*
1540
 * Get raw write metrics for a vnode
1541
 */
1542
int32_t vnodeGetRawWriteMetrics(void *pVnode, SRawWriteMetrics *pRawMetrics) {
×
1543
  if (pVnode == NULL || pRawMetrics == NULL) {
×
1544
    return TSDB_CODE_INVALID_PARA;
×
1545
  }
1546

1547
  SVnode      *pVnode1 = (SVnode *)pVnode;
×
1548
  SSyncMetrics syncMetrics = syncGetMetrics(pVnode1->sync);
×
1549

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

1571
  return 0;
×
1572
}
1573

1574
/*
1575
 * Reset raw write metrics for a vnode by subtracting old values
1576
 */
1577
int32_t vnodeResetRawWriteMetrics(void *pVnode, const SRawWriteMetrics *pOldMetrics) {
×
1578
  if (pVnode == NULL || pOldMetrics == NULL) {
×
1579
    return TSDB_CODE_INVALID_PARA;
×
1580
  }
1581

1582
  SVnode *pVnode1 = (SVnode *)pVnode;
×
1583

1584
  // Reset vnode write metrics using atomic operations to subtract old values
1585
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.total_requests, pOldMetrics->total_requests);
×
1586
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.total_rows, pOldMetrics->total_rows);
×
1587
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.total_bytes, pOldMetrics->total_bytes);
×
1588

1589
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.fetch_batch_meta_time, pOldMetrics->fetch_batch_meta_time);
×
1590
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.fetch_batch_meta_count, pOldMetrics->fetch_batch_meta_count);
×
1591
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.preprocess_time, pOldMetrics->preprocess_time);
×
1592
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.apply_bytes, pOldMetrics->apply_bytes);
×
1593
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.apply_time, pOldMetrics->apply_time);
×
1594
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.commit_count, pOldMetrics->commit_count);
×
1595

1596
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.commit_time, pOldMetrics->commit_time);
×
1597
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.merge_time, pOldMetrics->merge_time);
×
1598

1599
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.memtable_wait_time, pOldMetrics->memtable_wait_time);
×
1600
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.blocked_commit_count, pOldMetrics->blocked_commit_count);
×
1601
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.block_commit_time, pOldMetrics->blocked_commit_time);
×
1602
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.merge_count, pOldMetrics->merge_count);
×
1603

1604
  // Reset new cache metrics
1605
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.last_cache_commit_time, pOldMetrics->last_cache_commit_time);
×
1606
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.last_cache_commit_count, pOldMetrics->last_cache_commit_count);
×
1607

1608
  // Reset sync metrics
1609
  SSyncMetrics syncMetrics = {
×
1610
      .wal_write_bytes = pOldMetrics->wal_write_bytes,
×
1611
      .wal_write_time = pOldMetrics->wal_write_time,
×
1612
  };
1613
  syncResetMetrics(pVnode1->sync, &syncMetrics);
×
1614

1615
  return 0;
×
1616
}
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