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

taosdata / TDengine / #5005

26 Mar 2026 12:51PM UTC coverage: 72.152% (-0.2%) from 72.338%
#5005

push

travis-ci

web-flow
merge: from main to 3.0 branch #34951

512 of 851 new or added lines in 47 files covered. (60.16%)

6189 existing lines in 147 files now uncovered.

253282 of 351039 relevant lines covered (72.15%)

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

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

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

36
int32_t fillTableColCmpr(SMetaReader *reader, SSchemaExt *pExt, int32_t numOfCol) {
20,733,055✔
37
  int8_t tblType = reader->me.type;
20,733,055✔
38
  if (withExtSchema(tblType)) {
20,733,258✔
39
    SColCmprWrapper *p = &(reader->me.colCmpr);
20,375,426✔
40
    if (numOfCol != p->nCols) {
20,375,629✔
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++) {
238,191,637✔
45
      SColCmpr *pCmpr = &p->pColCmpr[i];
217,816,610✔
46
      pExt[i].colId = pCmpr->id;
217,815,892✔
47
      pExt[i].compress = pCmpr->alg;
217,818,348✔
48
    }
49
  }
50
  return 0;
20,733,258✔
51
}
52

53
void vnodeDebugTableMeta(STableMetaRsp *pMeta) {
20,733,055✔
54
  if (!(qDebugFlag & DEBUG_DEBUG)) {
20,733,055✔
55
    return;
378,283✔
56
  }
57

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

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

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

147
  if (infoReq.option == REQ_OPT_TBUID) reqTbUid = true;
30,054,343✔
148
  metaRsp.dbId = pVnode->config.dbId;
30,054,343✔
149
  tstrncpy(metaRsp.tbName, infoReq.tbName, TSDB_TABLE_NAME_LEN);
30,053,880✔
150
  (void)memcpy(metaRsp.dbFName, infoReq.dbFName, sizeof(metaRsp.dbFName));
30,054,343✔
151

152
  if (!reqTbUid) {
30,054,343✔
153
    (void)snprintf(tableFName, TSDB_TABLE_FNAME_LEN, "%s.%s", infoReq.dbFName, infoReq.tbName);
29,810,063✔
154
    if (pVnode->mounted) tTrimMountPrefix(tableFName);
29,810,063✔
155
    code = vnodeValidateTableHash(pVnode, tableFName);
29,811,243✔
156
    if (code) {
29,811,409✔
157
      goto _exit4;
×
158
    }
159
  }
160

161
  // query meta
162
  metaReaderDoInit(&mer1, pVnode->pMeta, META_READER_LOCK);
30,055,689✔
163
  if (reqTbUid) {
30,054,004✔
164
    SET_ERRNO(0);
242,761✔
165
    uint64_t tbUid = taosStr2UInt64(infoReq.tbName, NULL, 10);
242,761✔
166
    if (ERRNO == ERANGE || tbUid == 0) {
242,558✔
167
      code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
168
      goto _exit3;
×
169
    }
170
    SMetaReader mr3 = {0};
242,558✔
171
    metaReaderDoInit(&mr3, ((SVnode *)pVnode)->pMeta, META_READER_NOLOCK);
242,558✔
172
    if ((code = metaReaderGetTableEntryByUid(&mr3, tbUid)) < 0) {
242,761✔
173
      metaReaderClear(&mr3);
83,968✔
174
      TAOS_CHECK_GOTO(code, NULL, _exit3);
83,968✔
175
    }
176
    tstrncpy(metaRsp.tbName, mr3.me.name, TSDB_TABLE_NAME_LEN);
158,590✔
177
    metaReaderClear(&mr3);
158,590✔
178
    TAOS_CHECK_GOTO(metaGetTableEntryByName(&mer1, metaRsp.tbName), NULL, _exit3);
158,793✔
179
  } else if (metaGetTableEntryByName(&mer1, infoReq.tbName) < 0) {
29,811,243✔
180
    code = terrno;
9,237,320✔
181
    goto _exit3;
9,236,944✔
182
  }
183

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

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

204
      tstrncpy(metaRsp.stbName, mer2.me.name, sizeof(metaRsp.stbName));
13,305,344✔
205
      metaRsp.suid = mer2.me.uid;
13,304,953✔
206
      metaRsp.ownerId = mer2.me.stbEntry.ownerId;  // child table inherits ownerId from stb
13,304,953✔
207
      schema = mer2.me.stbEntry.schemaRow;
13,304,953✔
208
      schemaTag = mer2.me.stbEntry.schemaTag;
13,304,953✔
209
      break;
13,304,953✔
210
    }
211
    case TSDB_NORMAL_TABLE:
3,658,836✔
212
    case TSDB_VIRTUAL_NORMAL_TABLE: {
213
      schema = mer1.me.ntbEntry.schemaRow;
3,658,836✔
214
      metaRsp.ownerId = mer1.me.ntbEntry.ownerId;
3,658,836✔
215
      break;
3,658,836✔
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,732,201✔
224
  metaRsp.numOfColumns = schema.nCols;
20,732,201✔
225
  metaRsp.precision = pVnode->config.tsdbCfg.precision;
20,732,201✔
226
  metaRsp.sversion = schema.version;
20,732,389✔
227
  metaRsp.tversion = schemaTag.version;
20,732,389✔
228
  metaRsp.pSchemas = (SSchema *)taosMemoryMalloc(sizeof(SSchema) * (metaRsp.numOfColumns + metaRsp.numOfTags));
20,732,389✔
229
  metaRsp.pSchemaExt = (SSchemaExt *)taosMemoryCalloc(metaRsp.numOfColumns, sizeof(SSchemaExt));
20,730,252✔
230
  if (NULL == metaRsp.pSchemas || NULL == metaRsp.pSchemaExt) {
20,731,121✔
UNCOV
231
    code = terrno;
×
232
    goto _exit;
×
233
  }
234
  (void)memcpy(metaRsp.pSchemas, schema.pSchema, sizeof(SSchema) * schema.nCols);
20,732,641✔
235
  if (schemaTag.nCols) {
20,732,641✔
236
    (void)memcpy(metaRsp.pSchemas + schema.nCols, schemaTag.pSchema, sizeof(SSchema) * schemaTag.nCols);
17,073,756✔
237
  }
238
  if (metaRsp.pSchemaExt) {
20,732,641✔
239
    SMetaReader *pReader = mer1.me.type == TSDB_CHILD_TABLE ? &mer2 : &mer1;
20,731,535✔
240
    code = fillTableColCmpr(pReader, metaRsp.pSchemaExt, metaRsp.numOfColumns);
20,731,535✔
241
    if (code < 0) {
20,733,055✔
242
      goto _exit;
×
243
    }
244
    for (int32_t i = 0; i < metaRsp.numOfColumns && pReader->me.pExtSchemas; i++) {
48,780,418✔
245
      metaRsp.pSchemaExt[i].typeMod = pReader->me.pExtSchemas[i].typeMod;
28,047,363✔
246
    }
247
  } else {
248
    code = TSDB_CODE_OUT_OF_MEMORY;
1,106✔
249
    goto _exit;
1,106✔
250
  }
251
  if (hasRefCol(mer1.me.type)) {
20,732,389✔
252
    metaRsp.rversion = mer1.me.colRef.version;
357,166✔
253
    metaRsp.pColRefs = (SColRef*)taosMemoryMalloc(sizeof(SColRef) * metaRsp.numOfColumns);
357,166✔
254
    if (metaRsp.pColRefs) {
357,629✔
255
      code = fillTableColRef(&mer1, metaRsp.pColRefs, metaRsp.numOfColumns);
357,629✔
256
      if (code < 0) {
357,629✔
257
        goto _exit;
×
258
      }
259
    }
260
    metaRsp.numOfColRefs = metaRsp.numOfColumns;
357,629✔
261

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

287
  vnodeDebugTableMeta(&metaRsp);
20,733,055✔
288

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

296
  if (direct) {
20,731,520✔
297
    pRsp = rpcMallocCont(rspLen);
491,785✔
298
  } else {
299
    pRsp = taosMemoryCalloc(1, rspLen);
20,239,735✔
300
  }
301

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

307
  rspLen = tSerializeSTableMetaRsp(pRsp, rspLen, &metaRsp);
20,731,998✔
308
  if (rspLen < 0) {
20,733,258✔
309
    code = terrno;
×
310
    goto _exit;
×
311
  }
312

313
_exit:
20,734,364✔
314
  taosMemoryFree(metaRsp.pColRefs);
20,732,852✔
315
  taosMemoryFree(metaRsp.pSchemas);
20,731,253✔
316
  taosMemoryFree(metaRsp.pSchemaExt);
20,731,998✔
317
  taosMemoryFree(metaRsp.pTagRefs);
20,731,216✔
318
_exit2:
20,729,545✔
319
  metaReaderClear(&mer2);
20,733,055✔
320
_exit3:
30,052,256✔
321
  metaReaderClear(&mer1);
30,054,343✔
322
_exit4:
30,052,895✔
323
  rpcMsg.info = pMsg->info;
30,054,140✔
324
  rpcMsg.pCont = pRsp;
30,054,343✔
325
  rpcMsg.contLen = rspLen;
30,054,343✔
326
  rpcMsg.code = code;
30,054,343✔
327
  rpcMsg.msgType = pMsg->msgType;
30,054,343✔
328

329
  if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST && autoCreateCtb == 1) {
30,052,129✔
330
    code = TSDB_CODE_SUCCESS;
5,446,901✔
331
  }
332

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

338
  if (direct) {
30,054,140✔
339
    tmsgSendRsp(&rpcMsg);
1,267,936✔
340
  } else {
341
    *pMsg = rpcMsg;
28,786,204✔
342
  }
343

344
  return code;
30,053,749✔
345
}
346

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

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

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

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

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

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

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

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

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

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

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

456
  if (withExtSchema(cfgRsp.tableType)) {
62,224✔
457
    for (int32_t i = 0; i < cfgRsp.numOfColumns; i++) {
227,015✔
458
      SColCmpr   *pCmpr = &pColCmpr->pColCmpr[i];
172,690✔
459
      SSchemaExt *pSchExt = cfgRsp.pSchemaExt + i;
172,690✔
460
      pSchExt->colId = pCmpr->id;
172,690✔
461
      pSchExt->compress = pCmpr->alg;
172,690✔
462
      if (pReader->me.pExtSchemas)
172,690✔
463
        pSchExt->typeMod = pReader->me.pExtSchemas[i].typeMod;
25,203✔
464
      else
465
        pSchExt->typeMod = 0;
147,487✔
466
    }
467
  }
468

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

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

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

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

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

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

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

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

548
  tFreeSTableCfgRsp(&cfgRsp);
62,224✔
549
  metaReaderClear(&mer2);
62,224✔
550
  metaReaderClear(&mer1);
62,224✔
551
  return code;
62,224✔
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,293,486✔
564
  int32_t      code = 0;
28,293,486✔
565
  int32_t      rspSize = 0;
28,293,486✔
566
  SBatchReq    batchReq = {0};
28,293,486✔
567
  SBatchMsg   *req = NULL;
28,294,751✔
568
  SBatchRspMsg rsp = {0};
28,294,751✔
569
  SBatchRsp    batchRsp = {0};
28,284,864✔
570
  SRpcMsg      reqMsg = *pMsg;
28,283,647✔
571
  SRpcMsg      rspMsg = {0};
28,283,048✔
572
  void        *pRsp = NULL;
28,290,362✔
573

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

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

594
  for (int32_t i = 0; i < msgNum; ++i) {
61,952,534✔
595
    req = taosArrayGet(batchReq.pMsgs, i);
33,664,378✔
596
    if (req == NULL) {
33,661,476✔
597
      code = terrno;
×
598
      goto _exit;
×
599
    }
600

601
    reqMsg.msgType = req->msgType;
33,661,476✔
602
    reqMsg.pCont = req->msg;
33,660,724✔
603
    reqMsg.contLen = req->msgLen;
33,654,629✔
604

605
    switch (req->msgType) {
33,660,434✔
606
      case TDMT_VND_TABLE_META:
28,542,517✔
607
        // error code has been set into reqMsg, no need to handle it here.
608
        if (TSDB_CODE_SUCCESS != vnodeGetTableMeta(pVnode, &reqMsg, false)) {
28,542,517✔
609
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
3,014,268✔
610
        }
611
        break;
28,543,849✔
612
      case TDMT_VND_TABLE_NAME:
242,761✔
613
        // error code has been set into reqMsg, no need to handle it here.
614
        if (TSDB_CODE_SUCCESS != vnodeGetTableMeta(pVnode, &reqMsg, false)) {
242,761✔
615
          qWarn("vnodeGetBatchName failed, msgType:%d", req->msgType);
83,968✔
616
        }
617
        break;
242,355✔
618
      case TDMT_VND_TABLE_CFG:
62,224✔
619
        // error code has been set into reqMsg, no need to handle it here.
620
        if (TSDB_CODE_SUCCESS != vnodeGetTableCfg(pVnode, &reqMsg, false)) {
62,224✔
621
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
×
622
        }
623
        break;
62,224✔
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,816,894✔
631
        // error code has been set into reqMsg, no need to handle it here.
632
        if (TSDB_CODE_SUCCESS != vnodeGetVStbRefDbs(pVnode, &reqMsg)) {
4,816,894✔
633
          qWarn("vnodeGetVStbRefDbs failed, msgType:%d", req->msgType);
×
634
        }
635
        break;
4,810,591✔
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;
33,659,019✔
645
    rsp.reqType = reqMsg.msgType;
33,661,525✔
646
    rsp.msgLen = reqMsg.contLen;
33,661,525✔
647
    rsp.rspCode = reqMsg.code;
33,661,525✔
648
    rsp.msg = reqMsg.pCont;
33,661,525✔
649

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

657
  rspSize = tSerializeSBatchRsp(NULL, 0, &batchRsp);
28,288,156✔
658
  if (rspSize < 0) {
28,286,375✔
659
    qError("tSerializeSBatchRsp failed");
×
660
    code = terrno;
×
661
    goto _exit;
×
662
  }
663
  pRsp = rpcMallocCont(rspSize);
28,286,375✔
664
  if (pRsp == NULL) {
28,273,401✔
665
    qError("rpcMallocCont %d failed", rspSize);
×
666
    code = terrno;
×
667
    goto _exit;
×
668
  }
669
  if (tSerializeSBatchRsp(pRsp, rspSize, &batchRsp) < 0) {
28,273,401✔
670
    qError("tSerializeSBatchRsp %d failed", rspSize);
×
671
    code = terrno;
×
672
    goto _exit;
×
673
  }
674

675
_exit:
28,287,768✔
676

677
  rspMsg.info = pMsg->info;
28,292,536✔
678
  rspMsg.pCont = pRsp;
28,289,587✔
679
  rspMsg.contLen = rspSize;
28,289,587✔
680
  rspMsg.code = code;
28,289,587✔
681
  rspMsg.msgType = pMsg->msgType;
28,289,587✔
682

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

687
  taosArrayDestroyEx(batchReq.pMsgs, tFreeSBatchReqMsg);
28,280,579✔
688
  taosArrayDestroyEx(batchRsp.pRsps, tFreeSBatchRspMsg);
28,283,823✔
689

690
  tmsgSendRsp(&rspMsg);
28,272,414✔
691

692
  return code;
28,291,939✔
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,806,928✔
796
  int32_t                    code = TSDB_CODE_SUCCESS;
4,806,928✔
797
  int32_t                    line = 0;
4,806,928✔
798
  SMetaReader                mr = {0};
4,806,928✔
799
  bool                       readerInit = false;
4,805,675✔
800
  SSHashObj*                 pDbNameHash = NULL;
4,805,675✔
801
  SArray*                    pList = NULL;
4,805,675✔
802

803
  pList = taosArrayInit(10, sizeof(uint64_t));
4,805,675✔
804
  QUERY_CHECK_NULL(pList, code, line, _return, terrno);
4,807,130✔
805

806
  *ppRes = taosArrayInit(10, POINTER_BYTES);
4,807,130✔
807
  QUERY_CHECK_NULL(*ppRes, code, line, _return, terrno)
4,807,767✔
808
  
809
  // lookup in cache
810
  code = pHandle->api.metaFn.metaGetCachedRefDbs(pHandle->vnode, suid, *ppRes);
4,809,689✔
811
  QUERY_CHECK_CODE(code, line, _return);
4,818,478✔
812

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

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

824
    for (int32_t i = 0; i < num; ++i) {
454,576✔
825
      uint64_t* id = taosArrayGet(pList, i);
377,569✔
826
      QUERY_CHECK_NULL(id, code, line, _return, terrno);
376,952✔
827

828
      pHandle->api.metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pHandle->api.metaFn);
376,952✔
829
      readerInit = true;
378,186✔
830

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

834
      for (int32_t j = 0; j < mr.me.colRef.nCols; j++) {
38,304,538✔
835
        if (mr.me.colRef.pColRef[j].hasRef) {
37,926,969✔
836
          if (NULL == tSimpleHashGet(pDbNameHash, mr.me.colRef.pColRef[j].refDbName, strlen(mr.me.colRef.pColRef[j].refDbName))) {
37,249,408✔
837
            char *refDbName = taosStrdup(mr.me.colRef.pColRef[j].refDbName);
59,506✔
838
            QUERY_CHECK_NULL(refDbName, code, line, _return, terrno);
58,889✔
839

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

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

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

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

856
_return:
4,818,496✔
857

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

862
  taosArrayDestroy(pList);
4,818,478✔
863
  tSimpleHashCleanup(pDbNameHash);
4,815,081✔
864

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

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

948
  SReadHandle handle = {0};
4,811,015✔
949
  handle.vnode = pVnode;
4,814,671✔
950
  initStorageAPI(&handle.api);
4,814,671✔
951

952
  code = vnodeReadVStbRefDbs(&handle, req.suid, &rsp.pDbs);
4,813,979✔
953
  QUERY_CHECK_CODE(code, line, _return);
4,813,688✔
954
  rsp.vgId = TD_VID(pVnode);
4,813,688✔
955

956
  rspSize = tSerializeSVStbRefDbsRsp(NULL, 0, &rsp);
4,815,674✔
957
  if (rspSize < 0) {
4,801,338✔
958
    code = rspSize;
×
959
    qError("tSerializeSVStbRefDbsRsp failed, error:%d", rspSize);
×
960
    goto _return;
×
961
  }
962
  pRsp = taosMemoryCalloc(1, rspSize);
4,801,338✔
963
  if (pRsp == NULL) {
4,804,391✔
964
    code = terrno;
×
965
    qError("rpcMallocCont %d failed, error:%d", rspSize, terrno);
×
966
    goto _return;
×
967
  }
968
  rspSize = tSerializeSVStbRefDbsRsp(pRsp, rspSize, &rsp);
4,804,391✔
969
  if (rspSize < 0) {
4,810,704✔
970
    code = rspSize;
×
971
    qError("tSerializeSVStbRefDbsRsp failed, error:%d", rspSize);
×
972
    goto _return;
×
973
  }
974

975
_return:
4,810,704✔
976

977
  rspMsg.info = pMsg->info;
4,817,089✔
978
  rspMsg.pCont = pRsp;
4,816,874✔
979
  rspMsg.contLen = rspSize;
4,816,874✔
980
  rspMsg.code = code;
4,816,874✔
981
  rspMsg.msgType = pMsg->msgType;
4,816,874✔
982

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

987
  *pMsg = rspMsg;
4,813,461✔
988

989
  tDestroySVStbRefDbsRsp(&rsp);
4,811,078✔
990

991
  return code;
4,808,765✔
992
}
993

994
static int32_t vnodeGetCompStorage(SVnode *pVnode, int64_t *output) {
134,124,255✔
995
  int32_t code = 0;
134,124,255✔
996
#ifdef TD_ENTERPRISE
997
  int32_t now = taosGetTimestampSec();
134,124,255✔
998
  if (llabs(now - pVnode->config.vndStats.storageLastUpd) >= 30) {
134,124,255✔
999
    pVnode->config.vndStats.storageLastUpd = now;
7,114,132✔
1000

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

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

1026
    if (pVnode->inUse) {
134,124,255✔
1027
      *bufferSegmentUsed = pVnode->inUse->size;
134,110,005✔
1028
    }
1029
    *bufferSegmentSize = pVnode->config.szBuf / VNODE_BUFPOOL_SEGMENTS;
134,124,255✔
1030

1031
    (void)taosThreadMutexUnlock(&pVnode->mutex);
134,124,255✔
1032
  }
1033
}
134,124,255✔
1034

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

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

1095
  if (dbname) {
16,550,848✔
1096
    *dbname = pConf->dbname;
15,906,905✔
1097
  }
1098

1099
  if (vgId) {
16,550,848✔
1100
    *vgId = TD_VID(pVnodeObj);
15,676,374✔
1101
  }
1102

1103
  if (numOfTables) {
16,552,905✔
1104
    *numOfTables = pConf->vndStats.numOfNTables + pConf->vndStats.numOfCTables +
19,048✔
1105
                   pConf->vndStats.numOfVTables + pConf->vndStats.numOfVCTables;
9,690✔
1106
  }
1107

1108
  if (numOfNormalTables) {
16,552,905✔
1109
    *numOfNormalTables = pConf->vndStats.numOfNTables +
16,456✔
1110
                         pConf->vndStats.numOfVTables;
8,228✔
1111
  }
1112
}
16,552,905✔
1113

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

1161
  while (1) {
470,653,454✔
1162
    tb_uid_t id = metaCtbCursorNext(pCur);
572,877,462✔
1163
    if (id == 0) {
572,897,114✔
1164
      break;
102,280,167✔
1165
    }
1166
    qTrace("vnodeGetCtbIdList: got ctb id %" PRId64 " for suid %" PRId64, id, suid);
470,616,947✔
1167
    if (NULL == taosArrayPush(list, &id)) {
470,654,526✔
1168
      qError("taosArrayPush failed");
×
1169
      code = terrno;
×
1170
      goto _exit;
×
1171
    }
1172
  }
1173

1174
_exit:
102,228,348✔
1175
  metaCloseCtbCursor(pCur);
102,228,348✔
1176
  return code;
102,206,767✔
1177
}
1178

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

1186
  while (1) {
47,566,942✔
1187
    tb_uid_t id = metaStbCursorNext(pCur);
112,432,436✔
1188
    if (id == 0) {
112,427,124✔
1189
      break;
64,866,158✔
1190
    }
1191

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

1199
_exit:
64,862,273✔
1200
  metaCloseStbCursor(pCur);
64,862,273✔
1201
  return code;
64,860,945✔
1202
}
1203

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

1212
  while (1) {
3,940✔
1213
    tb_uid_t id = metaStbCursorNext(pCur);
7,224✔
1214
    if (id == 0) {
7,224✔
1215
      break;
3,284✔
1216
    }
1217

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

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

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

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

1240
  *num = 0;
4,610,022✔
1241
  while (1) {
2,656,891✔
1242
    tb_uid_t id = metaCtbCursorNext(pCur);
7,266,648✔
1243
    if (id == 0) {
7,265,333✔
1244
      break;
4,609,326✔
1245
    }
1246

1247
    ++(*num);
2,656,007✔
1248
  }
1249

1250
  metaCloseCtbCursor(pCur);
4,609,326✔
1251
  return TSDB_CODE_SUCCESS;
4,609,083✔
1252
}
1253

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

1263
  return TSDB_CODE_SUCCESS;
4,608,966✔
1264
}
1265

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

1270
  int32_t code = metaReaderGetTableEntryByUid(&mr, suid);
4,608,705✔
1271
  if (code == TSDB_CODE_SUCCESS) {
4,610,962✔
1272
    if (keep) *keep = mr.me.stbEntry.keep;
4,610,962✔
1273
    if (flags) *flags = mr.me.flags;
4,610,851✔
1274
  } else {
UNCOV
1275
    if (keep) *keep = 0;
×
UNCOV
1276
    if (flags) *flags = 0;
×
1277
  }
1278

1279
  metaReaderClear(&mr);
4,611,565✔
1280
  return TSDB_CODE_SUCCESS;
4,610,995✔
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,859,416✔
1358
  int32_t      code = TSDB_CODE_SUCCESS;
64,859,416✔
1359
  int32_t      tbNum = 0;
64,859,416✔
1360
  const char **pTbArr = NULL;
64,859,416✔
1361
  const char  *dbName = NULL;
64,859,416✔
1362
  *tbSize = 0;
64,859,416✔
1363

1364
  if (!(dbName = strchr(pVnode->config.dbname, '.'))) return 0;
64,860,080✔
1365
  if (0 == strncmp(++dbName, "log", TSDB_DB_NAME_LEN)) {
64,860,744✔
1366
    tbNum = tkLogStbNum;
3,412✔
1367
    pTbArr = (const char **)&tkLogStb;
3,412✔
1368
  } else if (0 == strncmp(dbName, "audit", TSDB_DB_NAME_LEN) || pVnode->config.isAudit) {
64,859,988✔
1369
    tbNum = tkAuditStbNum;
4,042✔
1370
    pTbArr = (const char **)&tkAuditStb;
4,042✔
1371
  }
1372
  if (tbNum && pTbArr) {
64,863,400✔
1373
    *tbSize = metaSizeOfTbFilterCache(pVnode->pMeta, 0);
7,454✔
1374
    if (*tbSize < tbNum) {
7,454✔
1375
      for (int32_t i = 0; i < tbNum; ++i) {
74,378✔
1376
        tb_uid_t suid = metaGetTableEntryUidByName(pVnode->pMeta, pTbArr[i]);
72,478✔
1377
        if (suid != 0) {
72,478✔
1378
          code = metaPutTbToFilterCache(pVnode->pMeta, &suid, 0);
4,208✔
1379
          if (TSDB_CODE_SUCCESS != code) {
4,208✔
1380
            return code;
2,756✔
1381
          }
1382
        }
1383
      }
1384
      *tbSize = metaSizeOfTbFilterCache(pVnode->pMeta, 0);
1,900✔
1385
    }
1386
  }
1387

1388
  return code;
64,856,660✔
1389
}
1390
#endif
1391

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

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

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

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

1408
  int32_t tbFilterSize = 0;
64,858,088✔
1409
  int32_t code = TSDB_CODE_SUCCESS;
64,856,096✔
1410
#ifdef TD_ENTERPRISE
1411
  code = vnodeGetTimeSeriesBlackList(pVnode, &tbFilterSize);
64,856,096✔
1412
  if (TSDB_CODE_SUCCESS != code) {
64,858,752✔
1413
    goto _exit;
2,756✔
1414
  }
1415
#endif
1416

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

1424
  *num = 0;
64,855,996✔
1425
  int64_t arrSize = taosArrayGetSize(suidList);
64,857,324✔
1426
  for (int64_t i = 0; i < arrSize; ++i) {
112,412,840✔
1427
    tb_uid_t suid = *(tb_uid_t *)taosArrayGet(suidList, i);
47,552,860✔
1428

1429
    int64_t ctbNum = 0;
47,552,196✔
1430
    int32_t numOfCols = 0;
47,553,524✔
1431
    int8_t  flags = 0;
47,551,532✔
1432
    code = metaGetStbStats(pVnode, suid, &ctbNum, &numOfCols, &flags);
47,549,540✔
1433
    if (TSDB_CODE_SUCCESS != code) {
47,560,164✔
UNCOV
1434
      goto _exit;
×
1435
    }
1436
    if (!TABLE_IS_VIRTUAL(flags)) {
47,560,164✔
1437
      *num += ctbNum * (numOfCols - 1);
43,988,787✔
1438
    }
1439
  }
1440

1441
_exit:
64,860,866✔
1442
  taosArrayDestroy(suidList);
64,863,400✔
1443
  return TSDB_CODE_SUCCESS;
64,860,843✔
1444
}
1445

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

1452
  *num = 0;
×
1453
  while (1) {
×
1454
    tb_uid_t id = metaStbCursorNext(pCur);
×
1455
    if (id == 0) {
×
UNCOV
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);
×
UNCOV
1463
      return code;
×
1464
    }
1465

1466
    *num += ctbNum;
×
1467
  }
1468

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

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

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

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

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

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

1496
  char   *dirName[] = {VNODE_TSDB_DIR, VNODE_WAL_DIR, VNODE_META_DIR, VNODE_TSDB_CACHE_DIR};
7,120,430✔
1497
  int64_t dirSize[4];
7,118,015✔
1498

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

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

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

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

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

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

1545
  // Copy values following SRawWriteMetrics structure order
UNCOV
1546
  pRawMetrics->total_requests = atomic_load_64(&pVnode1->writeMetrics.total_requests);
×
UNCOV
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);
×
UNCOV
1550
  pRawMetrics->fetch_batch_meta_count = atomic_load_64(&pVnode1->writeMetrics.fetch_batch_meta_count);
×
UNCOV
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) {
×
UNCOV
1573
  if (pVnode == NULL || pOldMetrics == NULL) {
×
UNCOV
1574
    return TSDB_CODE_INVALID_PARA;
×
1575
  }
1576

UNCOV
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);
×
UNCOV
1581
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.total_rows, pOldMetrics->total_rows);
×
UNCOV
1582
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.total_bytes, pOldMetrics->total_bytes);
×
1583

UNCOV
1584
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.fetch_batch_meta_time, pOldMetrics->fetch_batch_meta_time);
×
UNCOV
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);
×
UNCOV
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);
×
UNCOV
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
UNCOV
1604
  SSyncMetrics syncMetrics = {
×
UNCOV
1605
      .wal_write_bytes = pOldMetrics->wal_write_bytes,
×
1606
      .wal_write_time = pOldMetrics->wal_write_time,
×
1607
  };
UNCOV
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