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

taosdata / TDengine / #5009

29 Mar 2026 04:32AM UTC coverage: 72.26% (+0.02%) from 72.241%
#5009

push

travis-ci

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

253662 of 351039 relevant lines covered (72.26%)

131649114.75 hits per line

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

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

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

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

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

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

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

36
int32_t fillTableColCmpr(SMetaReader *reader, SSchemaExt *pExt, int32_t numOfCol) {
20,837,612✔
37
  int8_t tblType = reader->me.type;
20,837,612✔
38
  if (withExtSchema(tblType)) {
20,837,612✔
39
    SColCmprWrapper *p = &(reader->me.colCmpr);
20,473,040✔
40
    if (numOfCol != p->nCols) {
20,473,431✔
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,243,644✔
45
      SColCmpr *pCmpr = &p->pColCmpr[i];
218,770,213✔
46
      pExt[i].colId = pCmpr->id;
218,769,433✔
47
      pExt[i].compress = pCmpr->alg;
218,769,976✔
48
    }
49
  }
50
  return 0;
20,838,003✔
51
}
52

53
void vnodeDebugTableMeta(STableMetaRsp *pMeta) {
20,837,612✔
54
  if (!(qDebugFlag & DEBUG_DEBUG)) {
20,837,612✔
55
    return;
416,802✔
56
  }
57

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

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

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

147
  if (infoReq.option == REQ_OPT_TBUID) reqTbUid = true;
29,966,176✔
148
  metaRsp.dbId = pVnode->config.dbId;
29,966,176✔
149
  tstrncpy(metaRsp.tbName, infoReq.tbName, TSDB_TABLE_NAME_LEN);
29,965,785✔
150
  (void)memcpy(metaRsp.dbFName, infoReq.dbFName, sizeof(metaRsp.dbFName));
29,964,854✔
151

152
  if (!reqTbUid) {
29,964,854✔
153
    (void)snprintf(tableFName, TSDB_TABLE_FNAME_LEN, "%s.%s", infoReq.dbFName, infoReq.tbName);
29,720,047✔
154
    if (pVnode->mounted) tTrimMountPrefix(tableFName);
29,720,047✔
155
    code = vnodeValidateTableHash(pVnode, tableFName);
29,721,101✔
156
    if (code) {
29,720,047✔
157
      goto _exit4;
×
158
    }
159
  }
160

161
  // query meta
162
  metaReaderDoInit(&mer1, pVnode->pMeta, META_READER_LOCK);
29,964,854✔
163
  if (reqTbUid) {
29,965,788✔
164
    SET_ERRNO(0);
245,738✔
165
    uint64_t tbUid = taosStr2UInt64(infoReq.tbName, NULL, 10);
245,738✔
166
    if (ERRNO == ERANGE || tbUid == 0) {
245,738✔
167
      code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
168
      goto _exit3;
×
169
    }
170
    SMetaReader mr3 = {0};
245,738✔
171
    metaReaderDoInit(&mr3, ((SVnode *)pVnode)->pMeta, META_READER_NOLOCK);
245,738✔
172
    if ((code = metaReaderGetTableEntryByUid(&mr3, tbUid)) < 0) {
245,738✔
173
      metaReaderClear(&mr3);
86,957✔
174
      TAOS_CHECK_GOTO(code, NULL, _exit3);
86,957✔
175
    }
176
    tstrncpy(metaRsp.tbName, mr3.me.name, TSDB_TABLE_NAME_LEN);
158,576✔
177
    metaReaderClear(&mr3);
158,576✔
178
    TAOS_CHECK_GOTO(metaGetTableEntryByName(&mer1, metaRsp.tbName), NULL, _exit3);
158,781✔
179
  } else if (metaGetTableEntryByName(&mer1, infoReq.tbName) < 0) {
29,720,050✔
180
    code = terrno;
9,041,879✔
181
    goto _exit3;
9,041,879✔
182
  }
183

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

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

204
      tstrncpy(metaRsp.stbName, mer2.me.name, sizeof(metaRsp.stbName));
13,390,801✔
205
      metaRsp.suid = mer2.me.uid;
13,390,408✔
206
      metaRsp.ownerId = mer2.me.stbEntry.ownerId;  // child table inherits ownerId from stb
13,390,408✔
207
      schema = mer2.me.stbEntry.schemaRow;
13,390,408✔
208
      schemaTag = mer2.me.stbEntry.schemaTag;
13,390,408✔
209
      break;
13,390,408✔
210
    }
211
    case TSDB_NORMAL_TABLE:
3,677,985✔
212
    case TSDB_VIRTUAL_NORMAL_TABLE: {
213
      schema = mer1.me.ntbEntry.schemaRow;
3,677,985✔
214
      metaRsp.ownerId = mer1.me.ntbEntry.ownerId;
3,677,985✔
215
      break;
3,677,985✔
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,837,067✔
224
  metaRsp.numOfColumns = schema.nCols;
20,837,067✔
225
  metaRsp.precision = pVnode->config.tsdbCfg.precision;
20,837,067✔
226
  metaRsp.sversion = schema.version;
20,837,612✔
227
  metaRsp.tversion = schemaTag.version;
20,837,612✔
228
  metaRsp.pSchemas = (SSchema *)taosMemoryMalloc(sizeof(SSchema) * (metaRsp.numOfColumns + metaRsp.numOfTags));
20,837,612✔
229
  metaRsp.pSchemaExt = (SSchemaExt *)taosMemoryCalloc(metaRsp.numOfColumns, sizeof(SSchemaExt));
20,836,406✔
230
  if (NULL == metaRsp.pSchemas || NULL == metaRsp.pSchemaExt) {
20,835,738✔
231
    code = terrno;
×
232
    goto _exit;
×
233
  }
234
  (void)memcpy(metaRsp.pSchemas, schema.pSchema, sizeof(SSchema) * schema.nCols);
20,837,612✔
235
  if (schemaTag.nCols) {
20,837,612✔
236
    (void)memcpy(metaRsp.pSchemas + schema.nCols, schemaTag.pSchema, sizeof(SSchema) * schemaTag.nCols);
17,159,475✔
237
  }
238
  if (metaRsp.pSchemaExt) {
20,837,221✔
239
    SMetaReader *pReader = mer1.me.type == TSDB_CHILD_TABLE ? &mer2 : &mer1;
20,838,003✔
240
    code = fillTableColCmpr(pReader, metaRsp.pSchemaExt, metaRsp.numOfColumns);
20,838,003✔
241
    if (code < 0) {
20,837,612✔
242
      goto _exit;
×
243
    }
244
    for (int32_t i = 0; i < metaRsp.numOfColumns && pReader->me.pExtSchemas; i++) {
48,873,307✔
245
      metaRsp.pSchemaExt[i].typeMod = pReader->me.pExtSchemas[i].typeMod;
28,035,695✔
246
    }
247
  } else {
248
    code = TSDB_CODE_OUT_OF_MEMORY;
×
249
    goto _exit;
×
250
  }
251
  if (hasRefCol(mer1.me.type)) {
20,836,949✔
252
    metaRsp.rversion = mer1.me.colRef.version;
364,183✔
253
    metaRsp.pColRefs = (SColRef*)taosMemoryMalloc(sizeof(SColRef) * metaRsp.numOfColumns);
364,183✔
254
    if (metaRsp.pColRefs) {
363,792✔
255
      code = fillTableColRef(&mer1, metaRsp.pColRefs, metaRsp.numOfColumns);
363,792✔
256
      if (code < 0) {
363,792✔
257
        goto _exit;
×
258
      }
259
    }
260
    metaRsp.numOfColRefs = metaRsp.numOfColumns;
363,792✔
261

262
    // Fill tag references
263
    if (mer1.me.colRef.nTagRefs > 0) {
363,792✔
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;
363,792✔
278
      metaRsp.numOfTagRefs = 0;
363,792✔
279
    }
280
  } else {
281
    metaRsp.pColRefs = NULL;
20,473,820✔
282
    metaRsp.numOfColRefs = 0;
20,473,820✔
283
    metaRsp.pTagRefs = NULL;
20,473,820✔
284
    metaRsp.numOfTagRefs = 0;
20,473,820✔
285
  }
286

287
  vnodeDebugTableMeta(&metaRsp);
20,837,612✔
288

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

296
  if (direct) {
20,837,458✔
297
    pRsp = rpcMallocCont(rspLen);
506,843✔
298
  } else {
299
    pRsp = taosMemoryCalloc(1, rspLen);
20,330,615✔
300
  }
301

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

307
  rspLen = tSerializeSTableMetaRsp(pRsp, rspLen, &metaRsp);
20,837,338✔
308
  if (rspLen < 0) {
20,838,003✔
309
    code = terrno;
×
310
    goto _exit;
×
311
  }
312

313
_exit:
20,837,221✔
314
  taosMemoryFree(metaRsp.pColRefs);
20,837,798✔
315
  taosMemoryFree(metaRsp.pSchemas);
20,836,795✔
316
  taosMemoryFree(metaRsp.pSchemaExt);
20,837,255✔
317
  taosMemoryFree(metaRsp.pTagRefs);
20,836,590✔
318
_exit2:
20,836,590✔
319
  metaReaderClear(&mer2);
20,838,003✔
320
_exit3:
29,964,848✔
321
  metaReaderClear(&mer1);
29,966,634✔
322
_exit4:
29,964,835✔
323
  rpcMsg.info = pMsg->info;
29,966,634✔
324
  rpcMsg.pCont = pRsp;
29,966,634✔
325
  rpcMsg.contLen = rspLen;
29,966,634✔
326
  rpcMsg.code = code;
29,966,634✔
327
  rpcMsg.msgType = pMsg->msgType;
29,966,634✔
328

329
  if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST && autoCreateCtb == 1) {
29,965,703✔
330
    code = TSDB_CODE_SUCCESS;
5,552,475✔
331
  }
332

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

338
  if (direct) {
29,966,634✔
339
    tmsgSendRsp(&rpcMsg);
1,281,009✔
340
  } else {
341
    *pMsg = rpcMsg;
28,685,625✔
342
  }
343

344
  return code;
29,966,427✔
345
}
346

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

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

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

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

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

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

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

388
  if (mer1.me.type == TSDB_SUPER_TABLE) {
62,504✔
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) {
62,504✔
392
    metaReaderDoInit(&mer2, pVnode->pMeta, META_READER_NOLOCK);
44,348✔
393
    if (metaReaderGetTableEntryByUid(&mer2, mer1.me.ctbEntry.suid) < 0) goto _exit;
44,348✔
394

395
    tstrncpy(cfgRsp.stbName, mer2.me.name, TSDB_TABLE_NAME_LEN);
44,348✔
396
    schema = mer2.me.stbEntry.schemaRow;
44,348✔
397
    schemaTag = mer2.me.stbEntry.schemaTag;
44,348✔
398
    cfgRsp.ownerId = mer2.me.stbEntry.ownerId;  // child table inherits ownerId from stb
44,348✔
399
    cfgRsp.ttl = mer1.me.ctbEntry.ttlDays;
44,348✔
400
    cfgRsp.commentLen = mer1.me.ctbEntry.commentLen;
44,348✔
401
    if (mer1.me.ctbEntry.commentLen > 0) {
44,348✔
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;
44,348✔
409
    cfgRsp.tagsLen = pTag->len;
44,348✔
410
    cfgRsp.pTags = taosMemoryMalloc(cfgRsp.tagsLen);
44,348✔
411
    if (NULL == cfgRsp.pTags) {
44,348✔
412
      code = terrno;
×
413
      goto _exit;
×
414
    }
415
    (void)memcpy(cfgRsp.pTags, pTag, cfgRsp.tagsLen);
44,348✔
416
  } else if (mer1.me.type == TSDB_NORMAL_TABLE || mer1.me.type == TSDB_VIRTUAL_NORMAL_TABLE) {
18,156✔
417
    schema = mer1.me.ntbEntry.schemaRow;
18,156✔
418
    cfgRsp.ttl = mer1.me.ntbEntry.ttlDays;
18,156✔
419
    cfgRsp.ownerId = mer1.me.ntbEntry.ownerId;
18,156✔
420
    cfgRsp.commentLen = mer1.me.ntbEntry.commentLen;
18,156✔
421
    if (mer1.me.ntbEntry.commentLen > 0) {
18,156✔
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;
62,504✔
435
  cfgRsp.numOfColumns = schema.nCols;
62,504✔
436
  cfgRsp.virtualStb = false; // vnode don't have super table, so it's always false
62,504✔
437
  cfgRsp.pSchemas = (SSchema *)taosMemoryMalloc(sizeof(SSchema) * (cfgRsp.numOfColumns + cfgRsp.numOfTags));
62,504✔
438
  cfgRsp.pSchemaExt = (SSchemaExt *)taosMemoryMalloc(cfgRsp.numOfColumns * sizeof(SSchemaExt));
62,504✔
439
  cfgRsp.pColRefs = (SColRef *)taosMemoryMalloc(sizeof(SColRef) * cfgRsp.numOfColumns);
62,504✔
440
  cfgRsp.numOfTagRefs = 0;
62,504✔
441
  cfgRsp.pTagRefs = NULL;
62,504✔
442

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

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

456
  if (withExtSchema(cfgRsp.tableType)) {
62,504✔
457
    for (int32_t i = 0; i < cfgRsp.numOfColumns; i++) {
228,135✔
458
      SColCmpr   *pCmpr = &pColCmpr->pColCmpr[i];
173,546✔
459
      SSchemaExt *pSchExt = cfgRsp.pSchemaExt + i;
173,546✔
460
      pSchExt->colId = pCmpr->id;
173,546✔
461
      pSchExt->compress = pCmpr->alg;
173,546✔
462
      if (pReader->me.pExtSchemas)
173,546✔
463
        pSchExt->typeMod = pReader->me.pExtSchemas[i].typeMod;
25,080✔
464
      else
465
        pSchExt->typeMod = 0;
148,466✔
466
    }
467
  }
468

469
  cfgRsp.virtualStb = false;
62,504✔
470
  if (hasRefCol(cfgRsp.tableType)) {
62,504✔
471
    for (int32_t i = 0; i < cfgRsp.numOfColumns; i++) {
111,425✔
472
      SColRef *pRef = &pColRef->pColRef[i];
103,510✔
473
      cfgRsp.pColRefs[i].hasRef = pRef->hasRef;
103,510✔
474
      cfgRsp.pColRefs[i].id = pRef->id;
103,510✔
475
      if (cfgRsp.pColRefs[i].hasRef) {
103,510✔
476
        tstrncpy(cfgRsp.pColRefs[i].refDbName, pRef->refDbName, TSDB_DB_NAME_LEN);
54,296✔
477
        tstrncpy(cfgRsp.pColRefs[i].refTableName, pRef->refTableName, TSDB_TABLE_NAME_LEN);
54,296✔
478
        tstrncpy(cfgRsp.pColRefs[i].refColName, pRef->refColName, TSDB_COL_NAME_LEN);
54,296✔
479
      }
480
    }
481

482
    cfgRsp.numOfTagRefs = pColRef->nTagRefs;
7,915✔
483
    if (cfgRsp.numOfTagRefs > 0) {
7,915✔
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);
62,504✔
509
  if (rspLen < 0) {
62,504✔
510
    code = terrno;
×
511
    goto _exit;
×
512
  }
513

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

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

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

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

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

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

548
  tFreeSTableCfgRsp(&cfgRsp);
62,504✔
549
  metaReaderClear(&mer2);
62,504✔
550
  metaReaderClear(&mer1);
62,504✔
551
  return code;
62,504✔
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,642,072✔
564
  int32_t      code = 0;
27,642,072✔
565
  int32_t      rspSize = 0;
27,642,072✔
566
  SBatchReq    batchReq = {0};
27,642,072✔
567
  SBatchMsg   *req = NULL;
27,642,072✔
568
  SBatchRspMsg rsp = {0};
27,642,072✔
569
  SBatchRsp    batchRsp = {0};
27,634,696✔
570
  SRpcMsg      reqMsg = *pMsg;
27,634,153✔
571
  SRpcMsg      rspMsg = {0};
27,634,749✔
572
  void        *pRsp = NULL;
27,638,372✔
573

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

580
  int32_t msgNum = taosArrayGetSize(batchReq.pMsgs);
27,641,462✔
581
  if (msgNum >= MAX_META_MSG_IN_BATCH) {
27,638,315✔
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,638,315✔
588
  if (NULL == batchRsp.pRsps) {
27,635,951✔
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,513,604✔
595
    req = taosArrayGet(batchReq.pMsgs, i);
32,875,586✔
596
    if (req == NULL) {
32,874,247✔
597
      code = terrno;
×
598
      goto _exit;
×
599
    }
600

601
    reqMsg.msgType = req->msgType;
32,874,247✔
602
    reqMsg.pCont = req->msg;
32,874,915✔
603
    reqMsg.contLen = req->msgLen;
32,871,191✔
604

605
    switch (req->msgType) {
32,868,202✔
606
      case TDMT_VND_TABLE_META:
28,440,092✔
607
        // error code has been set into reqMsg, no need to handle it here.
608
        if (TSDB_CODE_SUCCESS != vnodeGetTableMeta(pVnode, &reqMsg, false)) {
28,440,092✔
609
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
2,715,238✔
610
        }
611
        break;
28,439,429✔
612
      case TDMT_VND_TABLE_NAME:
245,738✔
613
        // error code has been set into reqMsg, no need to handle it here.
614
        if (TSDB_CODE_SUCCESS != vnodeGetTableMeta(pVnode, &reqMsg, false)) {
245,738✔
615
          qWarn("vnodeGetBatchName failed, msgType:%d", req->msgType);
86,957✔
616
        }
617
        break;
245,533✔
618
      case TDMT_VND_TABLE_CFG:
62,504✔
619
        // error code has been set into reqMsg, no need to handle it here.
620
        if (TSDB_CODE_SUCCESS != vnodeGetTableCfg(pVnode, &reqMsg, false)) {
62,504✔
621
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
×
622
        }
623
        break;
62,504✔
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,127,190✔
631
        // error code has been set into reqMsg, no need to handle it here.
632
        if (TSDB_CODE_SUCCESS != vnodeGetVStbRefDbs(pVnode, &reqMsg)) {
4,127,190✔
633
          qWarn("vnodeGetVStbRefDbs failed, msgType:%d", req->msgType);
×
634
        }
635
        break;
4,124,128✔
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,871,594✔
645
    rsp.reqType = reqMsg.msgType;
32,872,228✔
646
    rsp.msgLen = reqMsg.contLen;
32,872,228✔
647
    rsp.rspCode = reqMsg.code;
32,872,228✔
648
    rsp.msg = reqMsg.pCont;
32,872,228✔
649

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

657
  rspSize = tSerializeSBatchRsp(NULL, 0, &batchRsp);
27,638,018✔
658
  if (rspSize < 0) {
27,632,747✔
659
    qError("tSerializeSBatchRsp failed");
×
660
    code = terrno;
×
661
    goto _exit;
×
662
  }
663
  pRsp = rpcMallocCont(rspSize);
27,632,747✔
664
  if (pRsp == NULL) {
27,626,408✔
665
    qError("rpcMallocCont %d failed", rspSize);
×
666
    code = terrno;
×
667
    goto _exit;
×
668
  }
669
  if (tSerializeSBatchRsp(pRsp, rspSize, &batchRsp) < 0) {
27,626,408✔
670
    qError("tSerializeSBatchRsp %d failed", rspSize);
×
671
    code = terrno;
×
672
    goto _exit;
×
673
  }
674

675
_exit:
27,637,007✔
676

677
  rspMsg.info = pMsg->info;
27,639,552✔
678
  rspMsg.pCont = pRsp;
27,641,433✔
679
  rspMsg.contLen = rspSize;
27,641,433✔
680
  rspMsg.code = code;
27,641,433✔
681
  rspMsg.msgType = pMsg->msgType;
27,641,433✔
682

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

687
  taosArrayDestroyEx(batchReq.pMsgs, tFreeSBatchReqMsg);
27,632,274✔
688
  taosArrayDestroyEx(batchRsp.pRsps, tFreeSBatchRspMsg);
27,628,461✔
689

690
  tmsgSendRsp(&rspMsg);
27,621,819✔
691

692
  return code;
27,640,823✔
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,120,538✔
796
  int32_t                    code = TSDB_CODE_SUCCESS;
4,120,538✔
797
  int32_t                    line = 0;
4,120,538✔
798
  SMetaReader                mr = {0};
4,120,538✔
799
  bool                       readerInit = false;
4,120,564✔
800
  SSHashObj*                 pDbNameHash = NULL;
4,120,564✔
801
  SArray*                    pList = NULL;
4,120,564✔
802

803
  pList = taosArrayInit(10, sizeof(uint64_t));
4,120,564✔
804
  QUERY_CHECK_NULL(pList, code, line, _return, terrno);
4,121,706✔
805

806
  *ppRes = taosArrayInit(10, POINTER_BYTES);
4,121,706✔
807
  QUERY_CHECK_NULL(*ppRes, code, line, _return, terrno)
4,121,683✔
808
  
809
  // lookup in cache
810
  code = pHandle->api.metaFn.metaGetCachedRefDbs(pHandle->vnode, suid, *ppRes);
4,119,875✔
811
  QUERY_CHECK_CODE(code, line, _return);
4,133,301✔
812

813
  if (taosArrayGetSize(*ppRes) > 0) {
4,133,301✔
814
    // found in cache
815
    goto _return;
4,056,777✔
816
  } else {
817
    code = pHandle->api.metaFn.getChildTableList(pHandle->vnode, suid, pList);
75,930✔
818
    QUERY_CHECK_CODE(code, line, _return);
75,312✔
819

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

824
    for (int32_t i = 0; i < num; ++i) {
449,483✔
825
      uint64_t* id = taosArrayGet(pList, i);
373,553✔
826
      QUERY_CHECK_NULL(id, code, line, _return, terrno);
373,553✔
827

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

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

834
      for (int32_t j = 0; j < mr.me.colRef.nCols; j++) {
38,240,131✔
835
        if (mr.me.colRef.pColRef[j].hasRef) {
37,864,724✔
836
          if (NULL == tSimpleHashGet(pDbNameHash, mr.me.colRef.pColRef[j].refDbName, strlen(mr.me.colRef.pColRef[j].refDbName))) {
37,218,925✔
837
            char *refDbName = taosStrdup(mr.me.colRef.pColRef[j].refDbName);
56,891✔
838
            QUERY_CHECK_NULL(refDbName, code, line, _return, terrno);
56,891✔
839

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

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

848
      pHandle->api.metaReaderFn.clearReader(&mr);
375,407✔
849
      readerInit = false;
374,171✔
850
    }
851

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

856
_return:
4,130,860✔
857

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

862
  taosArrayDestroy(pList);
4,131,506✔
863
  tSimpleHashCleanup(pDbNameHash);
4,129,676✔
864

865
  if (code) {
4,129,051✔
866
    qError("%s failed since %s", __func__, tstrerror(code));
×
867
  }
868
  return code;
4,129,051✔
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,125,427✔
934
  int32_t        code = 0;
4,125,427✔
935
  int32_t        rspSize = 0;
4,125,427✔
936
  SVStbRefDbsReq req = {0};
4,125,427✔
937
  SVStbRefDbsRsp rsp = {0};
4,127,816✔
938
  SRpcMsg        rspMsg = {0};
4,113,100✔
939
  void          *pRsp = NULL;
4,113,100✔
940
  int32_t        line = 0;
4,113,100✔
941

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

948
  SReadHandle handle = {0};
4,122,935✔
949
  handle.vnode = pVnode;
4,127,244✔
950
  initStorageAPI(&handle.api);
4,127,244✔
951

952
  code = vnodeReadVStbRefDbs(&handle, req.suid, &rsp.pDbs);
4,127,222✔
953
  QUERY_CHECK_CODE(code, line, _return);
4,129,640✔
954
  rsp.vgId = TD_VID(pVnode);
4,129,640✔
955

956
  rspSize = tSerializeSVStbRefDbsRsp(NULL, 0, &rsp);
4,129,600✔
957
  if (rspSize < 0) {
4,122,294✔
958
    code = rspSize;
×
959
    qError("tSerializeSVStbRefDbsRsp failed, error:%d", rspSize);
×
960
    goto _return;
×
961
  }
962
  pRsp = taosMemoryCalloc(1, rspSize);
4,122,294✔
963
  if (pRsp == NULL) {
4,121,710✔
964
    code = terrno;
×
965
    qError("rpcMallocCont %d failed, error:%d", rspSize, terrno);
×
966
    goto _return;
×
967
  }
968
  rspSize = tSerializeSVStbRefDbsRsp(pRsp, rspSize, &rsp);
4,121,710✔
969
  if (rspSize < 0) {
4,127,201✔
970
    code = rspSize;
×
971
    qError("tSerializeSVStbRefDbsRsp failed, error:%d", rspSize);
×
972
    goto _return;
×
973
  }
974

975
_return:
4,127,201✔
976

977
  rspMsg.info = pMsg->info;
4,129,637✔
978
  rspMsg.pCont = pRsp;
4,129,000✔
979
  rspMsg.contLen = rspSize;
4,129,000✔
980
  rspMsg.code = code;
4,129,000✔
981
  rspMsg.msgType = pMsg->msgType;
4,129,000✔
982

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

987
  *pMsg = rspMsg;
4,126,605✔
988

989
  tDestroySVStbRefDbsRsp(&rsp);
4,126,054✔
990

991
  return code;
4,125,958✔
992
}
993

994
static int32_t vnodeGetCompStorage(SVnode *pVnode, int64_t *output) {
135,641,079✔
995
  int32_t code = 0;
135,641,079✔
996
#ifdef TD_ENTERPRISE
997
  int32_t now = taosGetTimestampSec();
135,641,079✔
998
  if (llabs(now - pVnode->config.vndStats.storageLastUpd) >= 30) {
135,641,079✔
999
    pVnode->config.vndStats.storageLastUpd = now;
7,175,734✔
1000

1001
    SDbSizeStatisInfo info = {0};
7,175,734✔
1002
    if (0 == (code = vnodeGetDBSize(pVnode, &info))) {
7,175,734✔
1003
      int64_t compSize =
7,175,734✔
1004
          info.l1Size + info.l2Size + info.l3Size + info.cacheSize + info.walSize + info.metaSize + +info.ssSize;
7,175,734✔
1005
      if (compSize >= 0) {
7,175,734✔
1006
        pVnode->config.vndStats.compStorage = compSize;
7,175,734✔
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;
135,641,079✔
1016
#endif
1017
  return code;
135,641,079✔
1018
}
1019

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

1026
    if (pVnode->inUse) {
135,641,079✔
1027
      *bufferSegmentUsed = pVnode->inUse->size;
135,631,216✔
1028
    }
1029
    *bufferSegmentSize = pVnode->config.szBuf / VNODE_BUFPOOL_SEGMENTS;
135,641,079✔
1030

1031
    (void)taosThreadMutexUnlock(&pVnode->mutex);
135,641,079✔
1032
  }
1033
}
135,641,079✔
1034

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

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

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

1095
  if (dbname) {
12,819,396✔
1096
    *dbname = pConf->dbname;
12,172,065✔
1097
  }
1098

1099
  if (vgId) {
12,820,735✔
1100
    *vgId = TD_VID(pVnodeObj);
12,000,671✔
1101
  }
1102

1103
  if (numOfTables) {
12,821,842✔
1104
    *numOfTables = pConf->vndStats.numOfNTables + pConf->vndStats.numOfCTables +
18,911✔
1105
                   pConf->vndStats.numOfVTables + pConf->vndStats.numOfVCTables;
9,534✔
1106
  }
1107

1108
  if (numOfNormalTables) {
12,821,685✔
1109
    *numOfNormalTables = pConf->vndStats.numOfNTables +
16,646✔
1110
                         pConf->vndStats.numOfVTables;
8,323✔
1111
  }
1112
}
12,821,685✔
1113

1114
int32_t vnodeGetTableList(void *pVnode, int8_t type, SArray *pList) {
8,323✔
1115
  if (type == TSDB_SUPER_TABLE) {
8,323✔
1116
    return vnodeGetStbIdList(pVnode, 0, pList);
8,323✔
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) {
109,584,909✔
1153
  int32_t      code = TSDB_CODE_SUCCESS;
109,584,909✔
1154
  SVnode      *pVnodeObj = pVnode;
109,584,909✔
1155
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnodeObj, suid, 1);
109,584,909✔
1156
  if (NULL == pCur) {
109,565,333✔
1157
    qError("vnode get all table list failed");
×
1158
    return terrno;
×
1159
  }
1160

1161
  while (1) {
463,923,937✔
1162
    tb_uid_t id = metaCtbCursorNext(pCur);
573,489,270✔
1163
    if (id == 0) {
573,485,913✔
1164
      break;
109,605,323✔
1165
    }
1166
    qTrace("vnodeGetCtbIdList: got ctb id %" PRId64 " for suid %" PRId64, id, suid);
463,880,590✔
1167
    if (NULL == taosArrayPush(list, &id)) {
463,930,838✔
1168
      qError("taosArrayPush failed");
×
1169
      code = terrno;
×
1170
      goto _exit;
×
1171
    }
1172
  }
1173

1174
_exit:
109,560,843✔
1175
  metaCloseCtbCursor(pCur);
109,560,843✔
1176
  return code;
109,545,289✔
1177
}
1178

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

1186
  while (1) {
49,244,918✔
1187
    tb_uid_t id = metaStbCursorNext(pCur);
113,576,048✔
1188
    if (id == 0) {
113,572,441✔
1189
      break;
64,332,156✔
1190
    }
1191

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

1199
_exit:
64,329,140✔
1200
  metaCloseStbCursor(pCur);
64,329,140✔
1201
  return code;
64,331,488✔
1202
}
1203

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

1212
  while (1) {
3,752✔
1213
    tb_uid_t id = metaStbCursorNext(pCur);
6,212✔
1214
    if (id == 0) {
6,212✔
1215
      break;
2,460✔
1216
    }
1217

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

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

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

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

1240
  *num = 0;
4,714,893✔
1241
  while (1) {
2,740,159✔
1242
    tb_uid_t id = metaCtbCursorNext(pCur);
7,454,710✔
1243
    if (id == 0) {
7,453,548✔
1244
      break;
4,714,730✔
1245
    }
1246

1247
    ++(*num);
2,738,818✔
1248
  }
1249

1250
  metaCloseCtbCursor(pCur);
4,714,730✔
1251
  return TSDB_CODE_SUCCESS;
4,713,591✔
1252
}
1253

1254
int32_t vnodeGetStbColumnNum(SVnode *pVnode, tb_uid_t suid, int *num) {
4,713,887✔
1255
  SSchemaWrapper *pSW = metaGetTableSchema(pVnode->pMeta, suid, -1, 0, NULL, 0);
4,713,887✔
1256
  if (pSW) {
4,714,404✔
1257
    *num = pSW->nCols;
4,714,404✔
1258
    tDeleteSchemaWrapper(pSW);
1259
  } else {
1260
    *num = 2;
×
1261
  }
1262

1263
  return TSDB_CODE_SUCCESS;
4,714,151✔
1264
}
1265

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

1270
  int32_t code = metaReaderGetTableEntryByUid(&mr, suid);
4,714,358✔
1271
  if (code == TSDB_CODE_SUCCESS) {
4,715,040✔
1272
    if (keep) *keep = mr.me.stbEntry.keep;
4,715,040✔
1273
    if (flags) *flags = mr.me.flags;
4,714,018✔
1274
  } else {
1275
    if (keep) *keep = 0;
×
1276
    if (flags) *flags = 0;
×
1277
  }
1278

1279
  metaReaderClear(&mr);
4,713,672✔
1280
  return TSDB_CODE_SUCCESS;
4,714,410✔
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,326,153✔
1358
  int32_t      code = TSDB_CODE_SUCCESS;
64,326,153✔
1359
  int32_t      tbNum = 0;
64,326,153✔
1360
  const char **pTbArr = NULL;
64,326,153✔
1361
  const char  *dbName = NULL;
64,326,153✔
1362
  *tbSize = 0;
64,326,153✔
1363

1364
  if (!(dbName = strchr(pVnode->config.dbname, '.'))) return 0;
64,325,485✔
1365
  if (0 == strncmp(++dbName, "log", TSDB_DB_NAME_LEN)) {
64,326,821✔
1366
    tbNum = tkLogStbNum;
4,086✔
1367
    pTbArr = (const char **)&tkLogStb;
4,086✔
1368
  } else if (0 == strncmp(dbName, "audit", TSDB_DB_NAME_LEN) || pVnode->config.isAudit) {
64,323,403✔
1369
    tbNum = tkAuditStbNum;
3,070✔
1370
    pTbArr = (const char **)&tkAuditStb;
3,070✔
1371
  }
1372
  if (tbNum && pTbArr) {
64,325,408✔
1373
    *tbSize = metaSizeOfTbFilterCache(pVnode->pMeta, 0);
7,156✔
1374
    if (*tbSize < tbNum) {
7,156✔
1375
      for (int32_t i = 0; i < tbNum; ++i) {
101,036✔
1376
        tb_uid_t suid = metaGetTableEntryUidByName(pVnode->pMeta, pTbArr[i]);
98,965✔
1377
        if (suid != 0) {
98,965✔
1378
          code = metaPutTbToFilterCache(pVnode->pMeta, &suid, 0);
4,883✔
1379
          if (TSDB_CODE_SUCCESS != code) {
4,883✔
1380
            return code;
3,108✔
1381
          }
1382
        }
1383
      }
1384
      *tbSize = metaSizeOfTbFilterCache(pVnode->pMeta, 0);
2,071✔
1385
    }
1386
  }
1387

1388
  return code;
64,323,713✔
1389
}
1390
#endif
1391

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

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

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

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

1408
  int32_t tbFilterSize = 0;
64,327,131✔
1409
  int32_t code = TSDB_CODE_SUCCESS;
64,327,131✔
1410
#ifdef TD_ENTERPRISE
1411
  code = vnodeGetTimeSeriesBlackList(pVnode, &tbFilterSize);
64,327,131✔
1412
  if (TSDB_CODE_SUCCESS != code) {
64,326,153✔
1413
    goto _exit;
3,108✔
1414
  }
1415
#endif
1416

1417
  if ((!tbFilterSize && vnodeGetStbIdList(pVnode, 0, suidList) < 0) ||
64,323,045✔
1418
      (tbFilterSize && vnodeGetStbIdListByFilter(pVnode, 0, suidList, vnodeTimeSeriesFilter, pVnode) < 0)) {
64,323,355✔
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,324,725✔
1425
  int64_t arrSize = taosArrayGetSize(suidList);
64,323,355✔
1426
  for (int64_t i = 0; i < arrSize; ++i) {
113,553,033✔
1427
    tb_uid_t suid = *(tb_uid_t *)taosArrayGet(suidList, i);
49,227,984✔
1428

1429
    int64_t ctbNum = 0;
49,228,729✔
1430
    int32_t numOfCols = 0;
49,228,729✔
1431
    int8_t  flags = 0;
49,228,061✔
1432
    code = metaGetStbStats(pVnode, suid, &ctbNum, &numOfCols, &flags);
49,226,958✔
1433
    if (TSDB_CODE_SUCCESS != code) {
49,230,656✔
1434
      goto _exit;
×
1435
    }
1436
    if (!TABLE_IS_VIRTUAL(flags)) {
49,230,656✔
1437
      *num += ctbNum * (numOfCols - 1);
46,348,056✔
1438
    }
1439
  }
1440

1441
_exit:
64,326,933✔
1442
  taosArrayDestroy(suidList);
64,328,157✔
1443
  return TSDB_CODE_SUCCESS;
64,327,455✔
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,282,857✔
1474
  if (pVnode == NULL) {
4,282,857✔
1475
    return NULL;
×
1476
  }
1477

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

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

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

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

1496
  char   *dirName[] = {VNODE_TSDB_DIR, VNODE_WAL_DIR, VNODE_META_DIR, VNODE_TSDB_CACHE_DIR};
7,182,080✔
1497
  int64_t dirSize[4];
7,180,711✔
1498

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

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

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

1529
  code = tsdbGetFsSize(pVnodeObj->pTsdb, pInfo);
7,182,080✔
1530
_exit:
7,182,080✔
1531
  return code;
7,182,080✔
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