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

taosdata / TDengine / #5007

29 Mar 2026 04:32AM UTC coverage: 72.25% (-0.02%) from 72.274%
#5007

push

travis-ci

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

253624 of 351039 relevant lines covered (72.25%)

132531546.97 hits per line

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

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

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

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

36
int32_t fillTableColCmpr(SMetaReader *reader, SSchemaExt *pExt, int32_t numOfCol) {
21,533,596✔
37
  int8_t tblType = reader->me.type;
21,533,596✔
38
  if (withExtSchema(tblType)) {
21,535,969✔
39
    SColCmprWrapper *p = &(reader->me.colCmpr);
21,176,977✔
40
    if (numOfCol != p->nCols) {
21,178,026✔
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++) {
247,523,975✔
45
      SColCmpr *pCmpr = &p->pColCmpr[i];
226,344,625✔
46
      pExt[i].colId = pCmpr->id;
226,346,101✔
47
      pExt[i].compress = pCmpr->alg;
226,346,336✔
48
    }
49
  }
50
  return 0;
21,535,969✔
51
}
52

53
void vnodeDebugTableMeta(STableMetaRsp *pMeta) {
21,532,934✔
54
  if (!(qDebugFlag & DEBUG_DEBUG)) {
21,532,934✔
55
    return;
380,428✔
56
  }
57

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

81
int32_t fillTableColRef(SMetaReader *reader, SColRef *pRef, int32_t numOfCol) {
357,281✔
82
  int8_t tblType = reader->me.type;
357,281✔
83
  if (hasRefCol(tblType)) {
357,281✔
84
    SColRefWrapper *p = &(reader->me.colRef);
357,281✔
85
    if (numOfCol != p->nCols) {
357,281✔
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,768,257✔
90
      SColRef *pColRef = &p->pColRef[i];
2,410,976✔
91
      pRef[i].hasRef = pColRef->hasRef;
2,410,976✔
92
      pRef[i].id = pColRef->id;
2,410,976✔
93
      if(pRef[i].hasRef) {
2,410,976✔
94
        tstrncpy(pRef[i].refDbName, pColRef->refDbName, TSDB_DB_NAME_LEN);
1,691,704✔
95
        tstrncpy(pRef[i].refTableName, pColRef->refTableName, TSDB_TABLE_NAME_LEN);
1,691,704✔
96
        tstrncpy(pRef[i].refColName, pColRef->refColName, TSDB_COL_NAME_LEN);
1,691,704✔
97
      }
98
    }
99
  }
100
  return 0;
357,281✔
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) {
30,957,023✔
126
  STableInfoReq  infoReq = {0};
30,957,023✔
127
  STableMetaRsp  metaRsp = {0};
30,958,734✔
128
  SMetaReader    mer1 = {0};
30,958,307✔
129
  SMetaReader    mer2 = {0};
30,957,645✔
130
  char           tableFName[TSDB_TABLE_FNAME_LEN];
30,953,828✔
131
  bool           reqTbUid = false;
30,957,920✔
132
  SRpcMsg        rpcMsg = {0};
30,957,920✔
133
  int32_t        code = 0;
30,957,920✔
134
  int32_t        rspLen = 0;
30,957,920✔
135
  void          *pRsp = NULL;
30,957,920✔
136
  SSchemaWrapper schema = {0};
30,957,920✔
137
  SSchemaWrapper schemaTag = {0};
30,957,920✔
138
  uint8_t        autoCreateCtb = 0;
30,957,920✔
139

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

147
  if (infoReq.option == REQ_OPT_TBUID) reqTbUid = true;
30,959,631✔
148
  metaRsp.dbId = pVnode->config.dbId;
30,959,631✔
149
  tstrncpy(metaRsp.tbName, infoReq.tbName, TSDB_TABLE_NAME_LEN);
30,959,244✔
150
  (void)memcpy(metaRsp.dbFName, infoReq.dbFName, sizeof(metaRsp.dbFName));
30,956,869✔
151

152
  if (!reqTbUid) {
30,956,869✔
153
    (void)snprintf(tableFName, TSDB_TABLE_FNAME_LEN, "%s.%s", infoReq.dbFName, infoReq.tbName);
30,712,577✔
154
    if (pVnode->mounted) tTrimMountPrefix(tableFName);
30,712,577✔
155
    code = vnodeValidateTableHash(pVnode, tableFName);
30,714,561✔
156
    if (code) {
30,712,577✔
157
      goto _exit4;
×
158
    }
159
  }
160

161
  // query meta
162
  metaReaderDoInit(&mer1, pVnode->pMeta, META_READER_LOCK);
30,956,869✔
163
  if (reqTbUid) {
30,957,258✔
164
    SET_ERRNO(0);
244,681✔
165
    uint64_t tbUid = taosStr2UInt64(infoReq.tbName, NULL, 10);
244,681✔
166
    if (ERRNO == ERANGE || tbUid == 0) {
244,681✔
167
      code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
168
      goto _exit3;
×
169
    }
170
    SMetaReader mr3 = {0};
244,681✔
171
    metaReaderDoInit(&mr3, ((SVnode *)pVnode)->pMeta, META_READER_NOLOCK);
244,681✔
172
    if ((code = metaReaderGetTableEntryByUid(&mr3, tbUid)) < 0) {
244,681✔
173
      metaReaderClear(&mr3);
84,819✔
174
      TAOS_CHECK_GOTO(code, NULL, _exit3);
84,819✔
175
    }
176
    tstrncpy(metaRsp.tbName, mr3.me.name, TSDB_TABLE_NAME_LEN);
159,862✔
177
    metaReaderClear(&mr3);
159,862✔
178
    TAOS_CHECK_GOTO(metaGetTableEntryByName(&mer1, metaRsp.tbName), NULL, _exit3);
159,862✔
179
  } else if (metaGetTableEntryByName(&mer1, infoReq.tbName) < 0) {
30,712,577✔
180
    code = terrno;
9,338,843✔
181
    goto _exit3;
9,338,081✔
182
  }
183

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

189
  switch (mer1.me.type) {
21,534,229✔
190
    case TSDB_SUPER_TABLE: {
3,806,454✔
191
      tstrncpy(metaRsp.stbName, mer1.me.name, sizeof(metaRsp.stbName));
3,806,454✔
192
      schema = mer1.me.stbEntry.schemaRow;
3,805,915✔
193
      schemaTag = mer1.me.stbEntry.schemaTag;
3,805,915✔
194
      metaRsp.suid = mer1.me.uid;
3,805,915✔
195
      metaRsp.virtualStb = TABLE_IS_VIRTUAL(mer1.me.flags);
3,805,915✔
196
      metaRsp.ownerId = mer1.me.stbEntry.ownerId;
3,805,915✔
197
      break;
3,805,915✔
198
    }
199
    case TSDB_CHILD_TABLE:
13,345,903✔
200
    case TSDB_VIRTUAL_CHILD_TABLE:{
201
      metaReaderDoInit(&mer2, pVnode->pMeta, META_READER_NOLOCK);
13,345,903✔
202
      if (metaReaderGetTableEntryByUid(&mer2, mer1.me.ctbEntry.suid) < 0) goto _exit2;
13,346,319✔
203

204
      tstrncpy(metaRsp.stbName, mer2.me.name, sizeof(metaRsp.stbName));
13,346,319✔
205
      metaRsp.suid = mer2.me.uid;
13,345,932✔
206
      metaRsp.ownerId = mer2.me.stbEntry.ownerId;  // child table inherits ownerId from stb
13,345,932✔
207
      schema = mer2.me.stbEntry.schemaRow;
13,345,932✔
208
      schemaTag = mer2.me.stbEntry.schemaTag;
13,345,932✔
209
      break;
13,345,932✔
210
    }
211
    case TSDB_NORMAL_TABLE:
4,381,872✔
212
    case TSDB_VIRTUAL_NORMAL_TABLE: {
213
      schema = mer1.me.ntbEntry.schemaRow;
4,381,872✔
214
      metaRsp.ownerId = mer1.me.ntbEntry.ownerId;
4,381,872✔
215
      break;
4,381,872✔
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;
21,533,719✔
224
  metaRsp.numOfColumns = schema.nCols;
21,533,719✔
225
  metaRsp.precision = pVnode->config.tsdbCfg.precision;
21,533,719✔
226
  metaRsp.sversion = schema.version;
21,534,920✔
227
  metaRsp.tversion = schemaTag.version;
21,534,920✔
228
  metaRsp.pSchemas = (SSchema *)taosMemoryMalloc(sizeof(SSchema) * (metaRsp.numOfColumns + metaRsp.numOfTags));
21,534,920✔
229
  metaRsp.pSchemaExt = (SSchemaExt *)taosMemoryCalloc(metaRsp.numOfColumns, sizeof(SSchemaExt));
21,533,719✔
230
  if (NULL == metaRsp.pSchemas || NULL == metaRsp.pSchemaExt) {
21,534,381✔
231
    code = terrno;
×
232
    goto _exit;
×
233
  }
234
  (void)memcpy(metaRsp.pSchemas, schema.pSchema, sizeof(SSchema) * schema.nCols);
21,534,381✔
235
  if (schemaTag.nCols) {
21,534,381✔
236
    (void)memcpy(metaRsp.pSchemas + schema.nCols, schemaTag.pSchema, sizeof(SSchema) * schemaTag.nCols);
17,152,386✔
237
  }
238
  if (metaRsp.pSchemaExt) {
21,533,842✔
239
    SMetaReader *pReader = mer1.me.type == TSDB_CHILD_TABLE ? &mer2 : &mer1;
21,534,645✔
240
    code = fillTableColCmpr(pReader, metaRsp.pSchemaExt, metaRsp.numOfColumns);
21,534,645✔
241
    if (code < 0) {
21,534,920✔
242
      goto _exit;
×
243
    }
244
    for (int32_t i = 0; i < metaRsp.numOfColumns && pReader->me.pExtSchemas; i++) {
58,620,034✔
245
      metaRsp.pSchemaExt[i].typeMod = pReader->me.pExtSchemas[i].typeMod;
37,085,114✔
246
    }
247
  } else {
248
    code = TSDB_CODE_OUT_OF_MEMORY;
123✔
249
    goto _exit;
123✔
250
  }
251
  if (hasRefCol(mer1.me.type)) {
21,536,631✔
252
    metaRsp.rversion = mer1.me.colRef.version;
358,992✔
253
    metaRsp.pColRefs = (SColRef*)taosMemoryMalloc(sizeof(SColRef) * metaRsp.numOfColumns);
358,992✔
254
    if (metaRsp.pColRefs) {
357,281✔
255
      code = fillTableColRef(&mer1, metaRsp.pColRefs, metaRsp.numOfColumns);
357,281✔
256
      if (code < 0) {
357,281✔
257
        goto _exit;
×
258
      }
259
    }
260
    metaRsp.numOfColRefs = metaRsp.numOfColumns;
357,281✔
261

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

287
  vnodeDebugTableMeta(&metaRsp);
21,532,934✔
288

289
  // encode and send response
290
  rspLen = tSerializeSTableMetaRsp(NULL, 0, &metaRsp);
21,535,969✔
291
  if (rspLen < 0) {
21,534,258✔
292
    code = terrno;
×
293
    goto _exit;
×
294
  }
295

296
  if (direct) {
21,534,258✔
297
    pRsp = rpcMallocCont(rspLen);
491,712✔
298
  } else {
299
    pRsp = taosMemoryCalloc(1, rspLen);
21,042,546✔
300
  }
301

302
  if (pRsp == NULL) {
21,534,256✔
303
    code = terrno;
×
304
    goto _exit;
×
305
  }
306

307
  rspLen = tSerializeSTableMetaRsp(pRsp, rspLen, &metaRsp);
21,534,256✔
308
  if (rspLen < 0) {
21,535,307✔
309
    code = terrno;
×
310
    goto _exit;
×
311
  }
312

313
_exit:
21,534,504✔
314
  taosMemoryFree(metaRsp.pColRefs);
21,533,596✔
315
  taosMemoryFree(metaRsp.pSchemas);
21,532,934✔
316
  taosMemoryFree(metaRsp.pSchemaExt);
21,533,596✔
317
  taosMemoryFree(metaRsp.pTagRefs);
21,533,983✔
318
_exit2:
21,530,948✔
319
  metaReaderClear(&mer2);
21,535,307✔
320
_exit3:
30,955,170✔
321
  metaReaderClear(&mer1);
30,957,258✔
322
_exit4:
30,957,643✔
323
  rpcMsg.info = pMsg->info;
30,957,258✔
324
  rpcMsg.pCont = pRsp;
30,959,631✔
325
  rpcMsg.contLen = rspLen;
30,959,631✔
326
  rpcMsg.code = code;
30,959,631✔
327
  rpcMsg.msgType = pMsg->msgType;
30,959,631✔
328

329
  if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST && autoCreateCtb == 1) {
30,956,639✔
330
    code = TSDB_CODE_SUCCESS;
5,493,772✔
331
  }
332

333
  if (code) {
30,956,639✔
334
    qError("vgId:%d, get table %s meta with %" PRIu8 " failed cause of %s", pVnode->config.vgId, infoReq.tbName,
3,929,128✔
335
           infoReq.option, tstrerror(code));
336
  }
337

338
  if (direct) {
30,956,596✔
339
    tmsgSendRsp(&rpcMsg);
1,262,253✔
340
  } else {
341
    *pMsg = rpcMsg;
29,694,343✔
342
  }
343

344
  return code;
30,956,596✔
345
}
346

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

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

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

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

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

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

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

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

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

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

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

456
  if (withExtSchema(cfgRsp.tableType)) {
63,263✔
457
    for (int32_t i = 0; i < cfgRsp.numOfColumns; i++) {
236,892✔
458
      SColCmpr   *pCmpr = &pColCmpr->pColCmpr[i];
181,550✔
459
      SSchemaExt *pSchExt = cfgRsp.pSchemaExt + i;
181,550✔
460
      pSchExt->colId = pCmpr->id;
181,550✔
461
      pSchExt->compress = pCmpr->alg;
181,550✔
462
      if (pReader->me.pExtSchemas)
181,550✔
463
        pSchExt->typeMod = pReader->me.pExtSchemas[i].typeMod;
32,792✔
464
      else
465
        pSchExt->typeMod = 0;
148,758✔
466
    }
467
  }
468

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

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

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

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

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

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

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

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

548
  tFreeSTableCfgRsp(&cfgRsp);
63,263✔
549
  metaReaderClear(&mer2);
63,263✔
550
  metaReaderClear(&mer1);
63,263✔
551
  return code;
63,263✔
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) {
28,822,477✔
564
  int32_t      code = 0;
28,822,477✔
565
  int32_t      rspSize = 0;
28,822,477✔
566
  SBatchReq    batchReq = {0};
28,822,477✔
567
  SBatchMsg   *req = NULL;
28,822,477✔
568
  SBatchRspMsg rsp = {0};
28,822,477✔
569
  SBatchRsp    batchRsp = {0};
28,808,490✔
570
  SRpcMsg      reqMsg = *pMsg;
28,807,870✔
571
  SRpcMsg      rspMsg = {0};
28,809,686✔
572
  void        *pRsp = NULL;
28,817,095✔
573

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

580
  int32_t msgNum = taosArrayGetSize(batchReq.pMsgs);
28,822,389✔
581
  if (msgNum >= MAX_META_MSG_IN_BATCH) {
28,822,557✔
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));
28,822,557✔
588
  if (NULL == batchRsp.pRsps) {
28,814,098✔
589
    code = terrno;
×
590
    qError("taosArrayInit %d SBatchRspMsg failed", msgNum);
×
591
    goto _exit;
×
592
  }
593

594
  for (int32_t i = 0; i < msgNum; ++i) {
63,432,025✔
595
    req = taosArrayGet(batchReq.pMsgs, i);
34,614,089✔
596
    if (req == NULL) {
34,615,180✔
597
      code = terrno;
×
598
      goto _exit;
×
599
    }
600

601
    reqMsg.msgType = req->msgType;
34,615,180✔
602
    reqMsg.pCont = req->msg;
34,615,205✔
603
    reqMsg.contLen = req->msgLen;
34,607,766✔
604

605
    switch (req->msgType) {
34,607,521✔
606
      case TDMT_VND_TABLE_META:
29,448,763✔
607
        // error code has been set into reqMsg, no need to handle it here.
608
        if (TSDB_CODE_SUCCESS != vnodeGetTableMeta(pVnode, &reqMsg, false)) {
29,448,763✔
609
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
3,074,530✔
610
        }
611
        break;
29,450,324✔
612
      case TDMT_VND_TABLE_NAME:
244,681✔
613
        // error code has been set into reqMsg, no need to handle it here.
614
        if (TSDB_CODE_SUCCESS != vnodeGetTableMeta(pVnode, &reqMsg, false)) {
244,681✔
615
          qWarn("vnodeGetBatchName failed, msgType:%d", req->msgType);
84,819✔
616
        }
617
        break;
244,681✔
618
      case TDMT_VND_TABLE_CFG:
63,263✔
619
        // error code has been set into reqMsg, no need to handle it here.
620
        if (TSDB_CODE_SUCCESS != vnodeGetTableCfg(pVnode, &reqMsg, false)) {
63,263✔
621
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
×
622
        }
623
        break;
63,263✔
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,856,998✔
631
        // error code has been set into reqMsg, no need to handle it here.
632
        if (TSDB_CODE_SUCCESS != vnodeGetVStbRefDbs(pVnode, &reqMsg)) {
4,856,998✔
633
          qWarn("vnodeGetVStbRefDbs failed, msgType:%d", req->msgType);
×
634
        }
635
        break;
4,852,713✔
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;
34,610,981✔
645
    rsp.reqType = reqMsg.msgType;
34,611,777✔
646
    rsp.msgLen = reqMsg.contLen;
34,611,777✔
647
    rsp.rspCode = reqMsg.code;
34,611,777✔
648
    rsp.msg = reqMsg.pCont;
34,611,777✔
649

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

657
  rspSize = tSerializeSBatchRsp(NULL, 0, &batchRsp);
28,817,936✔
658
  if (rspSize < 0) {
28,815,232✔
659
    qError("tSerializeSBatchRsp failed");
×
660
    code = terrno;
×
661
    goto _exit;
×
662
  }
663
  pRsp = rpcMallocCont(rspSize);
28,815,232✔
664
  if (pRsp == NULL) {
28,794,735✔
665
    qError("rpcMallocCont %d failed", rspSize);
×
666
    code = terrno;
×
667
    goto _exit;
×
668
  }
669
  if (tSerializeSBatchRsp(pRsp, rspSize, &batchRsp) < 0) {
28,794,735✔
670
    qError("tSerializeSBatchRsp %d failed", rspSize);
×
671
    code = terrno;
×
672
    goto _exit;
×
673
  }
674

675
_exit:
28,812,824✔
676

677
  rspMsg.info = pMsg->info;
28,818,302✔
678
  rspMsg.pCont = pRsp;
28,818,298✔
679
  rspMsg.contLen = rspSize;
28,818,298✔
680
  rspMsg.code = code;
28,818,298✔
681
  rspMsg.msgType = pMsg->msgType;
28,818,298✔
682

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

687
  taosArrayDestroyEx(batchReq.pMsgs, tFreeSBatchReqMsg);
28,804,683✔
688
  taosArrayDestroyEx(batchRsp.pRsps, tFreeSBatchRspMsg);
28,803,522✔
689

690
  tmsgSendRsp(&rspMsg);
28,789,696✔
691

692
  return code;
28,821,053✔
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,848,672✔
796
  int32_t                    code = TSDB_CODE_SUCCESS;
4,848,672✔
797
  int32_t                    line = 0;
4,848,672✔
798
  SMetaReader                mr = {0};
4,848,672✔
799
  bool                       readerInit = false;
4,848,649✔
800
  SSHashObj*                 pDbNameHash = NULL;
4,848,649✔
801
  SArray*                    pList = NULL;
4,848,649✔
802

803
  pList = taosArrayInit(10, sizeof(uint64_t));
4,848,649✔
804
  QUERY_CHECK_NULL(pList, code, line, _return, terrno);
4,843,808✔
805

806
  *ppRes = taosArrayInit(10, POINTER_BYTES);
4,843,808✔
807
  QUERY_CHECK_NULL(*ppRes, code, line, _return, terrno)
4,845,526✔
808
  
809
  // lookup in cache
810
  code = pHandle->api.metaFn.metaGetCachedRefDbs(pHandle->vnode, suid, *ppRes);
4,846,292✔
811
  QUERY_CHECK_CODE(code, line, _return);
4,861,144✔
812

813
  if (taosArrayGetSize(*ppRes) > 0) {
4,861,144✔
814
    // found in cache
815
    goto _return;
4,782,689✔
816
  } else {
817
    code = pHandle->api.metaFn.getChildTableList(pHandle->vnode, suid, pList);
77,185✔
818
    QUERY_CHECK_CODE(code, line, _return);
77,185✔
819

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

824
    for (int32_t i = 0; i < num; ++i) {
456,947✔
825
      uint64_t* id = taosArrayGet(pList, i);
379,762✔
826
      QUERY_CHECK_NULL(id, code, line, _return, terrno);
379,762✔
827

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

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

834
      for (int32_t j = 0; j < mr.me.colRef.nCols; j++) {
37,985,172✔
835
        if (mr.me.colRef.pColRef[j].hasRef) {
37,605,624✔
836
          if (NULL == tSimpleHashGet(pDbNameHash, mr.me.colRef.pColRef[j].refDbName, strlen(mr.me.colRef.pColRef[j].refDbName))) {
36,924,470✔
837
            char *refDbName = taosStrdup(mr.me.colRef.pColRef[j].refDbName);
59,733✔
838
            QUERY_CHECK_NULL(refDbName, code, line, _return, terrno);
59,733✔
839

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

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

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

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

856
_return:
4,859,874✔
857

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

862
  taosArrayDestroy(pList);
4,859,874✔
863
  tSimpleHashCleanup(pDbNameHash);
4,855,189✔
864

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

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

948
  SReadHandle handle = {0};
4,849,828✔
949
  handle.vnode = pVnode;
4,856,200✔
950
  initStorageAPI(&handle.api);
4,856,200✔
951

952
  code = vnodeReadVStbRefDbs(&handle, req.suid, &rsp.pDbs);
4,857,875✔
953
  QUERY_CHECK_CODE(code, line, _return);
4,855,566✔
954
  rsp.vgId = TD_VID(pVnode);
4,855,566✔
955

956
  rspSize = tSerializeSVStbRefDbsRsp(NULL, 0, &rsp);
4,855,777✔
957
  if (rspSize < 0) {
4,843,164✔
958
    code = rspSize;
×
959
    qError("tSerializeSVStbRefDbsRsp failed, error:%d", rspSize);
×
960
    goto _return;
×
961
  }
962
  pRsp = taosMemoryCalloc(1, rspSize);
4,843,164✔
963
  if (pRsp == NULL) {
4,846,145✔
964
    code = terrno;
×
965
    qError("rpcMallocCont %d failed, error:%d", rspSize, terrno);
×
966
    goto _return;
×
967
  }
968
  rspSize = tSerializeSVStbRefDbsRsp(pRsp, rspSize, &rsp);
4,846,145✔
969
  if (rspSize < 0) {
4,852,319✔
970
    code = rspSize;
×
971
    qError("tSerializeSVStbRefDbsRsp failed, error:%d", rspSize);
×
972
    goto _return;
×
973
  }
974

975
_return:
4,852,319✔
976

977
  rspMsg.info = pMsg->info;
4,859,490✔
978
  rspMsg.pCont = pRsp;
4,856,634✔
979
  rspMsg.contLen = rspSize;
4,856,634✔
980
  rspMsg.code = code;
4,856,634✔
981
  rspMsg.msgType = pMsg->msgType;
4,856,634✔
982

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

987
  *pMsg = rspMsg;
4,854,570✔
988

989
  tDestroySVStbRefDbsRsp(&rsp);
4,843,682✔
990

991
  return code;
4,852,175✔
992
}
993

994
static int32_t vnodeGetCompStorage(SVnode *pVnode, int64_t *output) {
138,168,966✔
995
  int32_t code = 0;
138,168,966✔
996
#ifdef TD_ENTERPRISE
997
  int32_t now = taosGetTimestampSec();
138,168,966✔
998
  if (llabs(now - pVnode->config.vndStats.storageLastUpd) >= 30) {
138,168,966✔
999
    pVnode->config.vndStats.storageLastUpd = now;
7,289,453✔
1000

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

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

1026
    if (pVnode->inUse) {
138,168,966✔
1027
      *bufferSegmentUsed = pVnode->inUse->size;
138,158,449✔
1028
    }
1029
    *bufferSegmentSize = pVnode->config.szBuf / VNODE_BUFPOOL_SEGMENTS;
138,168,966✔
1030

1031
    (void)taosThreadMutexUnlock(&pVnode->mutex);
138,168,966✔
1032
  }
1033
}
138,168,966✔
1034

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

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

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

1095
  if (dbname) {
16,696,776✔
1096
    *dbname = pConf->dbname;
16,047,374✔
1097
  }
1098

1099
  if (vgId) {
16,697,045✔
1100
    *vgId = TD_VID(pVnodeObj);
15,855,257✔
1101
  }
1102

1103
  if (numOfTables) {
16,696,526✔
1104
    *numOfTables = pConf->vndStats.numOfNTables + pConf->vndStats.numOfCTables +
18,696✔
1105
                   pConf->vndStats.numOfVTables + pConf->vndStats.numOfVCTables;
9,348✔
1106
  }
1107

1108
  if (numOfNormalTables) {
16,696,526✔
1109
    *numOfNormalTables = pConf->vndStats.numOfNTables +
16,574✔
1110
                         pConf->vndStats.numOfVTables;
8,287✔
1111
  }
1112
}
16,696,526✔
1113

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

1161
  while (1) {
472,225,688✔
1162
    tb_uid_t id = metaCtbCursorNext(pCur);
576,561,025✔
1163
    if (id == 0) {
576,534,013✔
1164
      break;
104,372,295✔
1165
    }
1166
    qTrace("vnodeGetCtbIdList: got ctb id %" PRId64 " for suid %" PRId64, id, suid);
472,161,718✔
1167
    if (NULL == taosArrayPush(list, &id)) {
472,227,587✔
1168
      qError("taosArrayPush failed");
×
1169
      code = terrno;
×
1170
      goto _exit;
×
1171
    }
1172
  }
1173

1174
_exit:
104,334,995✔
1175
  metaCloseCtbCursor(pCur);
104,334,995✔
1176
  return code;
104,305,957✔
1177
}
1178

1179
int32_t vnodeGetStbIdList(SVnode *pVnode, int64_t suid, SArray *list) {
65,956,543✔
1180
  int32_t      code = TSDB_CODE_SUCCESS;
65,956,543✔
1181
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, suid);
65,956,543✔
1182
  if (!pCur) {
65,959,873✔
1183
    return TSDB_CODE_OUT_OF_MEMORY;
×
1184
  }
1185

1186
  while (1) {
48,952,179✔
1187
    tb_uid_t id = metaStbCursorNext(pCur);
114,912,052✔
1188
    if (id == 0) {
114,903,729✔
1189
      break;
65,963,869✔
1190
    }
1191

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

1199
_exit:
65,955,211✔
1200
  metaCloseStbCursor(pCur);
65,955,211✔
1201
  return code;
65,959,873✔
1202
}
1203

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

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

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

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

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

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

1240
  *num = 0;
4,677,552✔
1241
  while (1) {
2,772,237✔
1242
    tb_uid_t id = metaCtbCursorNext(pCur);
7,449,789✔
1243
    if (id == 0) {
7,449,093✔
1244
      break;
4,677,806✔
1245
    }
1246

1247
    ++(*num);
2,771,287✔
1248
  }
1249

1250
  metaCloseCtbCursor(pCur);
4,677,806✔
1251
  return TSDB_CODE_SUCCESS;
4,677,552✔
1252
}
1253

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

1263
  return TSDB_CODE_SUCCESS;
4,677,518✔
1264
}
1265

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

1270
  int32_t code = metaReaderGetTableEntryByUid(&mr, suid);
4,676,847✔
1271
  if (code == TSDB_CODE_SUCCESS) {
4,677,844✔
1272
    if (keep) *keep = mr.me.stbEntry.keep;
4,677,844✔
1273
    if (flags) *flags = mr.me.flags;
4,677,844✔
1274
  } else {
1275
    if (keep) *keep = 0;
×
1276
    if (flags) *flags = 0;
×
1277
  }
1278

1279
  metaReaderClear(&mr);
4,678,136✔
1280
  return TSDB_CODE_SUCCESS;
4,677,844✔
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) {
65,953,034✔
1358
  int32_t      code = TSDB_CODE_SUCCESS;
65,953,034✔
1359
  int32_t      tbNum = 0;
65,953,034✔
1360
  const char **pTbArr = NULL;
65,953,034✔
1361
  const char  *dbName = NULL;
65,953,034✔
1362
  *tbSize = 0;
65,953,034✔
1363

1364
  if (!(dbName = strchr(pVnode->config.dbname, '.'))) return 0;
65,955,032✔
1365
  if (0 == strncmp(++dbName, "log", TSDB_DB_NAME_LEN)) {
65,955,698✔
1366
    tbNum = tkLogStbNum;
4,436✔
1367
    pTbArr = (const char **)&tkLogStb;
4,436✔
1368
  } else if (0 == strncmp(dbName, "audit", TSDB_DB_NAME_LEN) || pVnode->config.isAudit) {
65,953,260✔
1369
    tbNum = tkAuditStbNum;
3,274✔
1370
    pTbArr = (const char **)&tkAuditStb;
3,274✔
1371
  }
1372
  if (tbNum && pTbArr) {
65,957,696✔
1373
    *tbSize = metaSizeOfTbFilterCache(pVnode->pMeta, 0);
7,710✔
1374
    if (*tbSize < tbNum) {
7,710✔
1375
      for (int32_t i = 0; i < tbNum; ++i) {
139,632✔
1376
        tb_uid_t suid = metaGetTableEntryUidByName(pVnode->pMeta, pTbArr[i]);
136,807✔
1377
        if (suid != 0) {
136,807✔
1378
          code = metaPutTbToFilterCache(pVnode->pMeta, &suid, 0);
4,603✔
1379
          if (TSDB_CODE_SUCCESS != code) {
4,603✔
1380
            return code;
2,830✔
1381
          }
1382
        }
1383
      }
1384
      *tbSize = metaSizeOfTbFilterCache(pVnode->pMeta, 0);
2,825✔
1385
    }
1386
  }
1387

1388
  return code;
65,948,872✔
1389
}
1390
#endif
1391

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

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

1401
int32_t vnodeGetTimeSeriesNum(SVnode *pVnode, int64_t *num) {
65,954,366✔
1402
  SArray *suidList = NULL;
65,954,366✔
1403

1404
  if (!(suidList = taosArrayInit(1, sizeof(tb_uid_t)))) {
65,954,366✔
1405
    return terrno;
×
1406
  }
1407

1408
  int32_t tbFilterSize = 0;
65,955,032✔
1409
  int32_t code = TSDB_CODE_SUCCESS;
65,955,698✔
1410
#ifdef TD_ENTERPRISE
1411
  code = vnodeGetTimeSeriesBlackList(pVnode, &tbFilterSize);
65,955,698✔
1412
  if (TSDB_CODE_SUCCESS != code) {
65,951,036✔
1413
    goto _exit;
2,830✔
1414
  }
1415
#endif
1416

1417
  if ((!tbFilterSize && vnodeGetStbIdList(pVnode, 0, suidList) < 0) ||
65,948,206✔
1418
      (tbFilterSize && vnodeGetStbIdListByFilter(pVnode, 0, suidList, vnodeTimeSeriesFilter, pVnode) < 0)) {
65,946,874✔
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;
65,953,534✔
1425
  int64_t arrSize = taosArrayGetSize(suidList);
65,951,536✔
1426
  for (int64_t i = 0; i < arrSize; ++i) {
114,891,473✔
1427
    tb_uid_t suid = *(tb_uid_t *)taosArrayGet(suidList, i);
48,933,943✔
1428

1429
    int64_t ctbNum = 0;
48,937,273✔
1430
    int32_t numOfCols = 0;
48,939,937✔
1431
    int8_t  flags = 0;
48,937,939✔
1432
    code = metaGetStbStats(pVnode, suid, &ctbNum, &numOfCols, &flags);
48,932,611✔
1433
    if (TSDB_CODE_SUCCESS != code) {
48,948,595✔
1434
      goto _exit;
×
1435
    }
1436
    if (!TABLE_IS_VIRTUAL(flags)) {
48,948,595✔
1437
      *num += ctbNum * (numOfCols - 1);
45,222,413✔
1438
    }
1439
  }
1440

1441
_exit:
65,959,766✔
1442
  taosArrayDestroy(suidList);
65,960,360✔
1443
  return TSDB_CODE_SUCCESS;
65,956,674✔
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,223,968✔
1474
  if (pVnode == NULL) {
4,223,968✔
1475
    return NULL;
×
1476
  }
1477

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

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

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

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

1496
  char   *dirName[] = {VNODE_TSDB_DIR, VNODE_WAL_DIR, VNODE_META_DIR, VNODE_TSDB_CACHE_DIR};
7,295,783✔
1497
  int64_t dirSize[4];
7,294,492✔
1498

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

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

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

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