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

taosdata / TDengine / #4945

30 Jan 2026 06:19AM UTC coverage: 66.87% (+0.02%) from 66.849%
#4945

push

travis-ci

web-flow
merge: from main to 3.0 #34453

1126 of 2018 new or added lines in 72 files covered. (55.8%)

13708 existing lines in 159 files now uncovered.

205277 of 306978 relevant lines covered (66.87%)

126353544.65 hits per line

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

65.15
/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) {
3,782,648✔
29
  return qWorkerInit(NODE_TYPE_VNODE, TD_VID(pVnode), (void **)&pVnode->pQuery, &pVnode->msgCb);
3,782,648✔
30
}
31

32
void vnodeQueryPreClose(SVnode *pVnode) { qWorkerStopAllTasks((void *)pVnode->pQuery); }
3,783,032✔
33

34
void vnodeQueryClose(SVnode *pVnode) { qWorkerDestroy((void **)&pVnode->pQuery); }
3,783,462✔
35

36
int32_t fillTableColCmpr(SMetaReader *reader, SSchemaExt *pExt, int32_t numOfCol) {
19,170,933✔
37
  int8_t tblType = reader->me.type;
19,170,933✔
38
  if (withExtSchema(tblType)) {
19,170,933✔
39
    SColCmprWrapper *p = &(reader->me.colCmpr);
18,884,880✔
40
    if (numOfCol != p->nCols) {
18,884,880✔
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++) {
223,586,971✔
45
      SColCmpr *pCmpr = &p->pColCmpr[i];
204,702,091✔
46
      pExt[i].colId = pCmpr->id;
204,701,344✔
47
      pExt[i].compress = pCmpr->alg;
204,703,523✔
48
    }
49
  }
50
  return 0;
19,171,115✔
51
}
52

53
void vnodePrintTableMeta(STableMetaRsp *pMeta) {
19,170,933✔
54
  if (!(qDebugFlag & DEBUG_DEBUG)) {
19,170,933✔
55
    return;
282,197✔
56
  }
57

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

81
int32_t fillTableColRef(SMetaReader *reader, SColRef *pRef, int32_t numOfCol) {
286,053✔
82
  int8_t tblType = reader->me.type;
286,053✔
83
  if (hasRefCol(tblType)) {
286,053✔
84
    SColRefWrapper *p = &(reader->me.colRef);
286,053✔
85
    if (numOfCol != p->nCols) {
286,053✔
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,364,957✔
90
      SColRef *pColRef = &p->pColRef[i];
2,078,904✔
91
      pRef[i].hasRef = pColRef->hasRef;
2,078,904✔
92
      pRef[i].id = pColRef->id;
2,078,904✔
93
      if(pRef[i].hasRef) {
2,078,904✔
94
        tstrncpy(pRef[i].refDbName, pColRef->refDbName, TSDB_DB_NAME_LEN);
1,467,410✔
95
        tstrncpy(pRef[i].refTableName, pColRef->refTableName, TSDB_TABLE_NAME_LEN);
1,467,410✔
96
        tstrncpy(pRef[i].refColName, pColRef->refColName, TSDB_COL_NAME_LEN);
1,467,410✔
97
      }
98
    }
99
  }
100
  return 0;
286,053✔
101
}
102

103
int32_t vnodeGetTableMeta(SVnode *pVnode, SRpcMsg *pMsg, bool direct) {
27,338,903✔
104
  STableInfoReq  infoReq = {0};
27,338,903✔
105
  STableMetaRsp  metaRsp = {0};
27,338,903✔
106
  SMetaReader    mer1 = {0};
27,339,085✔
107
  SMetaReader    mer2 = {0};
27,339,480✔
108
  char           tableFName[TSDB_TABLE_FNAME_LEN];
27,337,346✔
109
  bool           reqTbUid = false;
27,339,298✔
110
  SRpcMsg        rpcMsg = {0};
27,339,298✔
111
  int32_t        code = 0;
27,339,298✔
112
  int32_t        rspLen = 0;
27,339,298✔
113
  void          *pRsp = NULL;
27,339,298✔
114
  SSchemaWrapper schema = {0};
27,339,298✔
115
  SSchemaWrapper schemaTag = {0};
27,339,298✔
116
  uint8_t        autoCreateCtb = 0;
27,339,298✔
117

118
  // decode req
119
  if (tDeserializeSTableInfoReq(pMsg->pCont, pMsg->contLen, &infoReq) != 0) {
27,339,298✔
120
    code = terrno;
×
121
    goto _exit4;
×
122
  }
123
  autoCreateCtb = infoReq.autoCreateCtb;
27,339,732✔
124

125
  if (infoReq.option == REQ_OPT_TBUID) reqTbUid = true;
27,339,732✔
126
  metaRsp.dbId = pVnode->config.dbId;
27,339,732✔
127
  tstrncpy(metaRsp.tbName, infoReq.tbName, TSDB_TABLE_NAME_LEN);
27,339,116✔
128
  (void)memcpy(metaRsp.dbFName, infoReq.dbFName, sizeof(metaRsp.dbFName));
27,339,116✔
129

130
  if (!reqTbUid) {
27,339,116✔
131
    (void)tsnprintf(tableFName, TSDB_TABLE_FNAME_LEN, "%s.%s", infoReq.dbFName, infoReq.tbName);
27,099,084✔
132
    if (pVnode->mounted) tTrimMountPrefix(tableFName);
27,098,727✔
133
    code = vnodeValidateTableHash(pVnode, tableFName);
27,099,084✔
134
    if (code) {
27,099,084✔
135
      goto _exit4;
×
136
    }
137
  }
138

139
  // query meta
140
  metaReaderDoInit(&mer1, pVnode->pMeta, META_READER_LOCK);
27,339,116✔
141
  if (reqTbUid) {
27,340,096✔
142
    SET_ERRNO(0);
241,012✔
143
    uint64_t tbUid = taosStr2UInt64(infoReq.tbName, NULL, 10);
240,830✔
144
    if (ERRNO == ERANGE || tbUid == 0) {
241,012✔
145
      code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
146
      goto _exit3;
×
147
    }
148
    SMetaReader mr3 = {0};
241,012✔
149
    metaReaderDoInit(&mr3, ((SVnode *)pVnode)->pMeta, META_READER_NOLOCK);
241,012✔
150
    if ((code = metaReaderGetTableEntryByUid(&mr3, tbUid)) < 0) {
240,830✔
151
      metaReaderClear(&mr3);
88,100✔
152
      TAOS_CHECK_GOTO(code, NULL, _exit3);
87,918✔
153
    }
154
    tstrncpy(metaRsp.tbName, mr3.me.name, TSDB_TABLE_NAME_LEN);
152,548✔
155
    metaReaderClear(&mr3);
152,548✔
156
    TAOS_CHECK_GOTO(metaGetTableEntryByName(&mer1, metaRsp.tbName), NULL, _exit3);
152,730✔
157
  } else if (metaGetTableEntryByName(&mer1, infoReq.tbName) < 0) {
27,099,084✔
158
    code = terrno;
8,080,881✔
159
    goto _exit3;
8,080,524✔
160
  }
161

162
  metaRsp.tableType = mer1.me.type;
19,171,115✔
163
  metaRsp.vgId = TD_VID(pVnode);
19,171,115✔
164
  metaRsp.tuid = mer1.me.uid;
19,171,115✔
165
  metaRsp.isAudit = pVnode->config.isAudit ? 1 : 0;
19,171,115✔
166

167
  switch (mer1.me.type) {
19,170,217✔
168
    case TSDB_SUPER_TABLE: {
3,476,657✔
169
      (void)strcpy(metaRsp.stbName, mer1.me.name);
3,476,657✔
170
      schema = mer1.me.stbEntry.schemaRow;
3,476,657✔
171
      schemaTag = mer1.me.stbEntry.schemaTag;
3,476,657✔
172
      metaRsp.suid = mer1.me.uid;
3,476,657✔
173
      metaRsp.virtualStb = TABLE_IS_VIRTUAL(mer1.me.flags);
3,476,657✔
174
      metaRsp.ownerId = mer1.me.stbEntry.ownerId;
3,476,657✔
175
      break;
3,476,657✔
176
    }
177
    case TSDB_CHILD_TABLE:
11,906,912✔
178
    case TSDB_VIRTUAL_CHILD_TABLE:{
179
      metaReaderDoInit(&mer2, pVnode->pMeta, META_READER_NOLOCK);
11,906,912✔
180
      if (metaReaderGetTableEntryByUid(&mer2, mer1.me.ctbEntry.suid) < 0) goto _exit2;
11,907,628✔
181

182
      (void)strcpy(metaRsp.stbName, mer2.me.name);
11,907,628✔
183
      metaRsp.suid = mer2.me.uid;
11,907,628✔
184
      metaRsp.ownerId = mer2.me.stbEntry.ownerId;  // child table inherits ownerId from stb
11,907,628✔
185
      schema = mer2.me.stbEntry.schemaRow;
11,907,628✔
186
      schemaTag = mer2.me.stbEntry.schemaTag;
11,907,628✔
187
      break;
11,907,628✔
188
    }
189
    case TSDB_NORMAL_TABLE:
3,786,648✔
190
    case TSDB_VIRTUAL_NORMAL_TABLE: {
191
      schema = mer1.me.ntbEntry.schemaRow;
3,786,648✔
192
      metaRsp.ownerId = mer1.me.ntbEntry.ownerId;
3,786,648✔
193
      break;
3,786,648✔
194
    }
UNCOV
195
    default: {
×
UNCOV
196
      vError("vnodeGetTableMeta get invalid table type:%d", mer1.me.type);
×
197
      goto _exit3;
×
198
    }
199
  }
200

201
  metaRsp.numOfTags = schemaTag.nCols;
19,170,933✔
202
  metaRsp.numOfColumns = schema.nCols;
19,170,933✔
203
  metaRsp.precision = pVnode->config.tsdbCfg.precision;
19,170,933✔
204
  metaRsp.sversion = schema.version;
19,170,933✔
205
  metaRsp.tversion = schemaTag.version;
19,170,933✔
206
  metaRsp.pSchemas = (SSchema *)taosMemoryMalloc(sizeof(SSchema) * (metaRsp.numOfColumns + metaRsp.numOfTags));
19,170,933✔
207
  metaRsp.pSchemaExt = (SSchemaExt *)taosMemoryCalloc(metaRsp.numOfColumns, sizeof(SSchemaExt));
19,169,501✔
208
  if (NULL == metaRsp.pSchemas || NULL == metaRsp.pSchemaExt) {
19,169,501✔
UNCOV
209
    code = terrno;
×
UNCOV
210
    goto _exit;
×
211
  }
212
  (void)memcpy(metaRsp.pSchemas, schema.pSchema, sizeof(SSchema) * schema.nCols);
19,170,041✔
213
  if (schemaTag.nCols) {
19,170,041✔
214
    (void)memcpy(metaRsp.pSchemas + schema.nCols, schemaTag.pSchema, sizeof(SSchema) * schemaTag.nCols);
15,384,285✔
215
  }
216
  if (metaRsp.pSchemaExt) {
19,169,865✔
217
    SMetaReader *pReader = mer1.me.type == TSDB_CHILD_TABLE ? &mer2 : &mer1;
19,170,933✔
218
    code = fillTableColCmpr(pReader, metaRsp.pSchemaExt, metaRsp.numOfColumns);
19,170,933✔
219
    if (code < 0) {
19,170,933✔
UNCOV
220
      goto _exit;
×
221
    }
222
    for (int32_t i = 0; i < metaRsp.numOfColumns && pReader->me.pExtSchemas; i++) {
50,651,563✔
223
      metaRsp.pSchemaExt[i].typeMod = pReader->me.pExtSchemas[i].typeMod;
31,480,630✔
224
    }
225
  } else {
UNCOV
226
    code = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
227
    goto _exit;
×
228
  }
229
  if (hasRefCol(mer1.me.type)) {
19,171,115✔
230
    metaRsp.rversion = mer1.me.colRef.version;
286,235✔
231
    metaRsp.pColRefs = (SColRef*)taosMemoryMalloc(sizeof(SColRef) * metaRsp.numOfColumns);
286,235✔
232
    if (metaRsp.pColRefs) {
286,053✔
233
      code = fillTableColRef(&mer1, metaRsp.pColRefs, metaRsp.numOfColumns);
286,053✔
234
      if (code < 0) {
286,053✔
UNCOV
235
        goto _exit;
×
236
      }
237
    }
238
    metaRsp.numOfColRefs = metaRsp.numOfColumns;
286,053✔
239
  } else {
240
    metaRsp.pColRefs = NULL;
18,884,880✔
241
    metaRsp.numOfColRefs = 0;
18,884,880✔
242
  }
243

244
  vnodePrintTableMeta(&metaRsp);
19,170,933✔
245

246
  // encode and send response
247
  rspLen = tSerializeSTableMetaRsp(NULL, 0, &metaRsp);
19,171,115✔
248
  if (rspLen < 0) {
19,169,784✔
UNCOV
249
    code = terrno;
×
UNCOV
250
    goto _exit;
×
251
  }
252

253
  if (direct) {
19,169,784✔
254
    pRsp = rpcMallocCont(rspLen);
173,402✔
255
  } else {
256
    pRsp = taosMemoryCalloc(1, rspLen);
18,996,382✔
257
  }
258

259
  if (pRsp == NULL) {
19,169,784✔
UNCOV
260
    code = terrno;
×
UNCOV
261
    goto _exit;
×
262
  }
263

264
  rspLen = tSerializeSTableMetaRsp(pRsp, rspLen, &metaRsp);
19,169,784✔
265
  if (rspLen < 0) {
19,170,757✔
UNCOV
266
    code = terrno;
×
UNCOV
267
    goto _exit;
×
268
  }
269

270
_exit:
19,169,689✔
271
  taosMemoryFree(metaRsp.pColRefs);
19,170,569✔
272
  taosMemoryFree(metaRsp.pSchemas);
19,170,318✔
273
  taosMemoryFree(metaRsp.pSchemaExt);
19,170,933✔
274
_exit2:
19,170,393✔
275
  metaReaderClear(&mer2);
19,170,576✔
276
_exit3:
27,339,011✔
277
  metaReaderClear(&mer1);
27,339,375✔
278
_exit4:
27,339,914✔
279
  rpcMsg.info = pMsg->info;
27,339,914✔
280
  rpcMsg.pCont = pRsp;
27,339,914✔
281
  rpcMsg.contLen = rspLen;
27,339,914✔
282
  rpcMsg.code = code;
27,339,914✔
283
  rpcMsg.msgType = pMsg->msgType;
27,339,914✔
284

285
  if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST && autoCreateCtb == 1) {
27,339,017✔
286
    code = TSDB_CODE_SUCCESS;
4,875,435✔
287
  }
288

289
  if (code) {
27,339,017✔
290
    qError("vgId:%d, get table %s meta with %" PRIu8 " failed cause of %s", pVnode->config.vgId, infoReq.tbName,
3,293,364✔
291
           infoReq.option, tstrerror(code));
292
  }
293

294
  if (direct) {
27,339,550✔
295
    tmsgSendRsp(&rpcMsg);
867,556✔
296
  } else {
297
    *pMsg = rpcMsg;
26,471,994✔
298
  }
299

300
  return code;
27,339,732✔
301
}
302

303
int32_t vnodeGetTableCfg(SVnode *pVnode, SRpcMsg *pMsg, bool direct) {
54,870✔
304
  STableCfgReq   cfgReq = {0};
54,870✔
305
  STableCfgRsp   cfgRsp = {0};
54,870✔
306
  SMetaReader    mer1 = {0};
54,870✔
307
  SMetaReader    mer2 = {0};
54,870✔
308
  char           tableFName[TSDB_TABLE_FNAME_LEN];
54,870✔
309
  SRpcMsg        rpcMsg = {0};
54,870✔
310
  int32_t        code = 0;
54,870✔
311
  int32_t        rspLen = 0;
54,870✔
312
  void          *pRsp = NULL;
54,870✔
313
  SSchemaWrapper schema = {0};
54,870✔
314
  SSchemaWrapper schemaTag = {0};
54,870✔
315

316
  // decode req
317
  if (tDeserializeSTableCfgReq(pMsg->pCont, pMsg->contLen, &cfgReq) != 0) {
54,870✔
UNCOV
318
    code = terrno;
×
UNCOV
319
    goto _exit;
×
320
  }
321

322
  tstrncpy(cfgRsp.tbName, cfgReq.tbName, TSDB_TABLE_NAME_LEN);
54,870✔
323
  (void)memcpy(cfgRsp.dbFName, cfgReq.dbFName, sizeof(cfgRsp.dbFName));
54,870✔
324

325
  (void)tsnprintf(tableFName, TSDB_TABLE_FNAME_LEN, "%s.%s", cfgReq.dbFName, cfgReq.tbName);
54,870✔
326
  if (pVnode->mounted) tTrimMountPrefix(tableFName);
54,870✔
327
  code = vnodeValidateTableHash(pVnode, tableFName);
54,870✔
328
  if (code) {
54,870✔
UNCOV
329
    goto _exit;
×
330
  }
331

332
  // query meta
333
  metaReaderDoInit(&mer1, pVnode->pMeta, META_READER_LOCK);
54,870✔
334

335
  if (metaGetTableEntryByName(&mer1, cfgReq.tbName) < 0) {
54,870✔
UNCOV
336
    code = terrno;
×
UNCOV
337
    goto _exit;
×
338
  }
339

340
  cfgRsp.tableType = mer1.me.type;
54,870✔
341
  cfgRsp.isAudit = pVnode->config.isAudit ? 1 : 0;
54,870✔
342

343
  if (mer1.me.type == TSDB_SUPER_TABLE) {
54,870✔
UNCOV
344
    code = TSDB_CODE_VND_HASH_MISMATCH;
×
UNCOV
345
    goto _exit;
×
346
  } else if (mer1.me.type == TSDB_CHILD_TABLE || mer1.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
54,870✔
347
    metaReaderDoInit(&mer2, pVnode->pMeta, META_READER_NOLOCK);
37,759✔
348
    if (metaReaderGetTableEntryByUid(&mer2, mer1.me.ctbEntry.suid) < 0) goto _exit;
37,759✔
349

350
    tstrncpy(cfgRsp.stbName, mer2.me.name, TSDB_TABLE_NAME_LEN);
37,759✔
351
    schema = mer2.me.stbEntry.schemaRow;
37,759✔
352
    schemaTag = mer2.me.stbEntry.schemaTag;
37,759✔
353
    cfgRsp.ownerId = mer2.me.stbEntry.ownerId;  // child table inherits ownerId from stb
37,759✔
354
    cfgRsp.ttl = mer1.me.ctbEntry.ttlDays;
37,759✔
355
    cfgRsp.commentLen = mer1.me.ctbEntry.commentLen;
37,759✔
356
    if (mer1.me.ctbEntry.commentLen > 0) {
37,759✔
UNCOV
357
      cfgRsp.pComment = taosStrdup(mer1.me.ctbEntry.comment);
×
UNCOV
358
      if (NULL == cfgRsp.pComment) {
×
UNCOV
359
        code = terrno;
×
UNCOV
360
        goto _exit;
×
361
      }
362
    }
363
    STag *pTag = (STag *)mer1.me.ctbEntry.pTags;
37,759✔
364
    cfgRsp.tagsLen = pTag->len;
37,759✔
365
    cfgRsp.pTags = taosMemoryMalloc(cfgRsp.tagsLen);
37,759✔
366
    if (NULL == cfgRsp.pTags) {
37,759✔
UNCOV
367
      code = terrno;
×
UNCOV
368
      goto _exit;
×
369
    }
370
    (void)memcpy(cfgRsp.pTags, pTag, cfgRsp.tagsLen);
37,759✔
371
  } else if (mer1.me.type == TSDB_NORMAL_TABLE || mer1.me.type == TSDB_VIRTUAL_NORMAL_TABLE) {
17,111✔
372
    schema = mer1.me.ntbEntry.schemaRow;
17,111✔
373
    cfgRsp.ttl = mer1.me.ntbEntry.ttlDays;
17,111✔
374
    cfgRsp.ownerId = mer1.me.ntbEntry.ownerId;
17,111✔
375
    cfgRsp.commentLen = mer1.me.ntbEntry.commentLen;
17,111✔
376
    if (mer1.me.ntbEntry.commentLen > 0) {
17,111✔
UNCOV
377
      cfgRsp.pComment = taosStrdup(mer1.me.ntbEntry.comment);
×
UNCOV
378
      if (NULL == cfgRsp.pComment) {
×
UNCOV
379
        code = terrno;
×
UNCOV
380
        goto _exit;
×
381
      }
382
    }
383
  } else {
384
    vError("vnodeGetTableCfg get invalid table type:%d", mer1.me.type);
×
UNCOV
385
    code = TSDB_CODE_APP_ERROR;
×
UNCOV
386
    goto _exit;
×
387
  }
388

389
  cfgRsp.numOfTags = schemaTag.nCols;
54,870✔
390
  cfgRsp.numOfColumns = schema.nCols;
54,870✔
391
  cfgRsp.virtualStb = false; // vnode don't have super table, so it's always false
54,870✔
392
  cfgRsp.pSchemas = (SSchema *)taosMemoryMalloc(sizeof(SSchema) * (cfgRsp.numOfColumns + cfgRsp.numOfTags));
54,870✔
393
  cfgRsp.pSchemaExt = (SSchemaExt *)taosMemoryMalloc(cfgRsp.numOfColumns * sizeof(SSchemaExt));
54,870✔
394
  cfgRsp.pColRefs = (SColRef *)taosMemoryMalloc(sizeof(SColRef) * cfgRsp.numOfColumns);
54,870✔
395

396
  if (NULL == cfgRsp.pSchemas || NULL == cfgRsp.pSchemaExt || NULL == cfgRsp.pColRefs) {
54,870✔
UNCOV
397
    code = terrno;
×
UNCOV
398
    goto _exit;
×
399
  }
400
  (void)memcpy(cfgRsp.pSchemas, schema.pSchema, sizeof(SSchema) * schema.nCols);
54,870✔
401
  if (schemaTag.nCols) {
54,870✔
402
    (void)memcpy(cfgRsp.pSchemas + schema.nCols, schemaTag.pSchema, sizeof(SSchema) * schemaTag.nCols);
37,759✔
403
  }
404

405
  SMetaReader     *pReader = (mer1.me.type == TSDB_CHILD_TABLE || mer1.me.type == TSDB_VIRTUAL_CHILD_TABLE) ? &mer2 : &mer1;
54,870✔
406
  SColCmprWrapper *pColCmpr = &pReader->me.colCmpr;
54,870✔
407
  SColRefWrapper  *pColRef = &mer1.me.colRef;
54,870✔
408

409
  if (withExtSchema(cfgRsp.tableType)) {
54,870✔
410
    for (int32_t i = 0; i < cfgRsp.numOfColumns; i++) {
203,404✔
411
      SColCmpr   *pCmpr = &pColCmpr->pColCmpr[i];
155,830✔
412
      SSchemaExt *pSchExt = cfgRsp.pSchemaExt + i;
155,830✔
413
      pSchExt->colId = pCmpr->id;
155,830✔
414
      pSchExt->compress = pCmpr->alg;
155,830✔
415
      if (pReader->me.pExtSchemas)
155,830✔
416
        pSchExt->typeMod = pReader->me.pExtSchemas[i].typeMod;
30,206✔
417
      else
418
        pSchExt->typeMod = 0;
125,624✔
419
    }
420
  }
421

422
  cfgRsp.virtualStb = false;
54,870✔
423
  if (hasRefCol(cfgRsp.tableType)) {
54,870✔
424
    for (int32_t i = 0; i < cfgRsp.numOfColumns; i++) {
102,808✔
425
      SColRef *pRef = &pColRef->pColRef[i];
95,512✔
426
      cfgRsp.pColRefs[i].hasRef = pRef->hasRef;
95,512✔
427
      cfgRsp.pColRefs[i].id = pRef->id;
95,512✔
428
      if (cfgRsp.pColRefs[i].hasRef) {
95,512✔
429
        tstrncpy(cfgRsp.pColRefs[i].refDbName, pRef->refDbName, TSDB_DB_NAME_LEN);
49,873✔
430
        tstrncpy(cfgRsp.pColRefs[i].refTableName, pRef->refTableName, TSDB_TABLE_NAME_LEN);
49,873✔
431
        tstrncpy(cfgRsp.pColRefs[i].refColName, pRef->refColName, TSDB_COL_NAME_LEN);
49,873✔
432
      }
433
    }
434
  }
435

436
  // encode and send response
437
  rspLen = tSerializeSTableCfgRsp(NULL, 0, &cfgRsp);
54,870✔
438
  if (rspLen < 0) {
54,870✔
UNCOV
439
    code = terrno;
×
UNCOV
440
    goto _exit;
×
441
  }
442

443
  if (direct) {
54,870✔
444
    pRsp = rpcMallocCont(rspLen);
×
445
  } else {
446
    pRsp = taosMemoryCalloc(1, rspLen);
54,870✔
447
  }
448

449
  if (pRsp == NULL) {
54,870✔
UNCOV
450
    code = terrno;
×
UNCOV
451
    goto _exit;
×
452
  }
453

454
  rspLen = tSerializeSTableCfgRsp(pRsp, rspLen, &cfgRsp);
54,870✔
455
  if (rspLen < 0) {
54,870✔
UNCOV
456
    code = terrno;
×
UNCOV
457
    goto _exit;
×
458
  }
459

460
_exit:
54,870✔
461
  rpcMsg.info = pMsg->info;
54,870✔
462
  rpcMsg.pCont = pRsp;
54,870✔
463
  rpcMsg.contLen = rspLen;
54,870✔
464
  rpcMsg.code = code;
54,870✔
465
  rpcMsg.msgType = pMsg->msgType;
54,870✔
466

467
  if (code) {
54,870✔
UNCOV
468
    qError("get table %s cfg failed cause of %s", cfgReq.tbName, tstrerror(code));
×
469
  }
470

471
  if (direct) {
54,870✔
472
    tmsgSendRsp(&rpcMsg);
×
473
  } else {
474
    *pMsg = rpcMsg;
54,870✔
475
  }
476

477
  tFreeSTableCfgRsp(&cfgRsp);
54,870✔
478
  metaReaderClear(&mer2);
54,870✔
479
  metaReaderClear(&mer1);
54,870✔
480
  return code;
54,870✔
481
}
482

483
static FORCE_INLINE void vnodeFreeSBatchRspMsg(void *p) {
484
  if (NULL == p) {
485
    return;
486
  }
487

488
  SBatchRspMsg *pRsp = (SBatchRspMsg *)p;
489
  rpcFreeCont(pRsp->msg);
490
}
491

492
int32_t vnodeGetBatchMeta(SVnode *pVnode, SRpcMsg *pMsg) {
25,823,516✔
493
  int32_t      code = 0;
25,823,516✔
494
  int32_t      rspSize = 0;
25,823,516✔
495
  SBatchReq    batchReq = {0};
25,823,516✔
496
  SBatchMsg   *req = NULL;
25,824,882✔
497
  SBatchRspMsg rsp = {0};
25,824,882✔
498
  SBatchRsp    batchRsp = {0};
25,811,450✔
499
  SRpcMsg      reqMsg = *pMsg;
25,810,559✔
500
  SRpcMsg      rspMsg = {0};
25,810,559✔
501
  void        *pRsp = NULL;
25,818,982✔
502

503
  if (tDeserializeSBatchReq(pMsg->pCont, pMsg->contLen, &batchReq)) {
25,818,982✔
UNCOV
504
    code = terrno;
×
UNCOV
505
    qError("tDeserializeSBatchReq failed");
×
UNCOV
506
    goto _exit;
×
507
  }
508

509
  int32_t msgNum = taosArrayGetSize(batchReq.pMsgs);
25,815,472✔
510
  if (msgNum >= MAX_META_MSG_IN_BATCH) {
25,823,672✔
UNCOV
511
    code = TSDB_CODE_INVALID_MSG;
×
UNCOV
512
    qError("too many msgs %d in vnode batch meta req", msgNum);
×
UNCOV
513
    goto _exit;
×
514
  }
515

516
  batchRsp.pRsps = taosArrayInit(msgNum, sizeof(SBatchRspMsg));
25,823,672✔
517
  if (NULL == batchRsp.pRsps) {
25,818,164✔
UNCOV
518
    code = terrno;
×
UNCOV
519
    qError("taosArrayInit %d SBatchRspMsg failed", msgNum);
×
UNCOV
520
    goto _exit;
×
521
  }
522

523
  for (int32_t i = 0; i < msgNum; ++i) {
56,724,584✔
524
    req = taosArrayGet(batchReq.pMsgs, i);
30,903,606✔
525
    if (req == NULL) {
30,904,779✔
UNCOV
526
      code = terrno;
×
UNCOV
527
      goto _exit;
×
528
    }
529

530
    reqMsg.msgType = req->msgType;
30,904,779✔
531
    reqMsg.pCont = req->msg;
30,900,408✔
532
    reqMsg.contLen = req->msgLen;
30,901,088✔
533

534
    switch (req->msgType) {
30,900,184✔
535
      case TDMT_VND_TABLE_META:
26,231,528✔
536
        // error code has been set into reqMsg, no need to handle it here.
537
        if (TSDB_CODE_SUCCESS != vnodeGetTableMeta(pVnode, &reqMsg, false)) {
26,231,528✔
538
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
2,511,292✔
539
        }
540
        break;
26,231,528✔
541
      case TDMT_VND_TABLE_NAME:
240,830✔
542
        // error code has been set into reqMsg, no need to handle it here.
543
        if (TSDB_CODE_SUCCESS != vnodeGetTableMeta(pVnode, &reqMsg, false)) {
240,830✔
544
          qWarn("vnodeGetBatchName failed, msgType:%d", req->msgType);
88,100✔
545
        }
546
        break;
240,466✔
547
      case TDMT_VND_TABLE_CFG:
54,870✔
548
        // error code has been set into reqMsg, no need to handle it here.
549
        if (TSDB_CODE_SUCCESS != vnodeGetTableCfg(pVnode, &reqMsg, false)) {
54,870✔
UNCOV
550
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
×
551
        }
552
        break;
54,870✔
UNCOV
553
      case TDMT_VND_VSUBTABLES_META:
×
554
        // error code has been set into reqMsg, no need to handle it here.
UNCOV
555
        if (TSDB_CODE_SUCCESS != vnodeGetVSubtablesMeta(pVnode, &reqMsg)) {
×
UNCOV
556
          qWarn("vnodeGetVSubtablesMeta failed, msgType:%d", req->msgType);
×
557
        }
UNCOV
558
        break;
×
559
      case TDMT_VND_VSTB_REF_DBS:
4,376,984✔
560
        // error code has been set into reqMsg, no need to handle it here.
561
        if (TSDB_CODE_SUCCESS != vnodeGetVStbRefDbs(pVnode, &reqMsg)) {
4,376,984✔
562
          qWarn("vnodeGetVStbRefDbs failed, msgType:%d", req->msgType);
×
563
        }
564
        break;
4,370,150✔
UNCOV
565
      default:
×
566
        qError("invalid req msgType %d", req->msgType);
×
UNCOV
567
        reqMsg.code = TSDB_CODE_INVALID_MSG;
×
UNCOV
568
        reqMsg.pCont = NULL;
×
569
        reqMsg.contLen = 0;
×
570
        break;
×
571
    }
572

573
    rsp.msgIdx = req->msgIdx;
30,897,014✔
574
    rsp.reqType = reqMsg.msgType;
30,901,214✔
575
    rsp.msgLen = reqMsg.contLen;
30,901,214✔
576
    rsp.rspCode = reqMsg.code;
30,901,214✔
577
    rsp.msg = reqMsg.pCont;
30,901,214✔
578

579
    if (NULL == taosArrayPush(batchRsp.pRsps, &rsp)) {
61,804,016✔
UNCOV
580
      qError("taosArrayPush failed");
×
UNCOV
581
      code = terrno;
×
UNCOV
582
      goto _exit;
×
583
    }
584
  }
585

586
  rspSize = tSerializeSBatchRsp(NULL, 0, &batchRsp);
25,820,978✔
587
  if (rspSize < 0) {
25,798,552✔
UNCOV
588
    qError("tSerializeSBatchRsp failed");
×
UNCOV
589
    code = terrno;
×
UNCOV
590
    goto _exit;
×
591
  }
592
  pRsp = rpcMallocCont(rspSize);
25,798,552✔
593
  if (pRsp == NULL) {
25,795,284✔
594
    qError("rpcMallocCont %d failed", rspSize);
×
UNCOV
595
    code = terrno;
×
UNCOV
596
    goto _exit;
×
597
  }
598
  if (tSerializeSBatchRsp(pRsp, rspSize, &batchRsp) < 0) {
25,795,284✔
599
    qError("tSerializeSBatchRsp %d failed", rspSize);
×
600
    code = terrno;
×
UNCOV
601
    goto _exit;
×
602
  }
603

604
_exit:
25,812,886✔
605

606
  rspMsg.info = pMsg->info;
25,821,539✔
607
  rspMsg.pCont = pRsp;
25,815,855✔
608
  rspMsg.contLen = rspSize;
25,815,855✔
609
  rspMsg.code = code;
25,815,855✔
610
  rspMsg.msgType = pMsg->msgType;
25,815,855✔
611

612
  if (code) {
25,800,476✔
UNCOV
613
    qError("vnd get batch meta failed cause of %s", tstrerror(code));
×
614
  }
615

616
  taosArrayDestroyEx(batchReq.pMsgs, tFreeSBatchReqMsg);
25,800,476✔
617
  taosArrayDestroyEx(batchRsp.pRsps, tFreeSBatchRspMsg);
25,798,478✔
618

619
  tmsgSendRsp(&rspMsg);
25,787,820✔
620

621
  return code;
25,821,416✔
622
}
623

624
#define VNODE_DO_META_QUERY(pVnode, cmd)                 \
625
  do {                                                   \
626
    (void)taosThreadRwlockRdlock(&(pVnode)->metaRWLock); \
627
    cmd;                                                 \
628
    (void)taosThreadRwlockUnlock(&(pVnode)->metaRWLock); \
629
  } while (0)
630

UNCOV
631
int32_t vnodeReadVSubtables(SReadHandle* pHandle, int64_t suid, SArray** ppRes) {
×
UNCOV
632
  int32_t                    code = TSDB_CODE_SUCCESS;
×
UNCOV
633
  int32_t                    line = 0;
×
UNCOV
634
  SMetaReader                mr = {0};
×
635
  bool                       readerInit = false;
×
636
  SVCTableRefCols*           pTb = NULL;
×
637
  int32_t                    refColsNum = 0;
×
638
  char                       tbFName[TSDB_TABLE_FNAME_LEN];
×
639
  SSHashObj*                 pSrcTbls = NULL;
×
640

641
  SArray *pList = taosArrayInit(10, sizeof(uint64_t));
×
642
  QUERY_CHECK_NULL(pList, code, line, _return, terrno);
×
643

UNCOV
644
  QUERY_CHECK_CODE(pHandle->api.metaFn.getChildTableList(pHandle->vnode, suid, pList), line, _return);
×
645

646
  size_t num = taosArrayGetSize(pList);
×
UNCOV
647
  *ppRes = taosArrayInit(num, POINTER_BYTES);
×
648
  QUERY_CHECK_NULL(*ppRes, code, line, _return, terrno);
×
UNCOV
649
  pSrcTbls = tSimpleHashInit(10, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
×
650
  QUERY_CHECK_NULL(pSrcTbls, code, line, _return, terrno);
×
651

652
  for (int32_t i = 0; i < num; ++i) {
×
653
    uint64_t* id = taosArrayGet(pList, i);
×
654
    QUERY_CHECK_NULL(id, code, line, _return, terrno);
×
UNCOV
655
    pHandle->api.metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pHandle->api.metaFn);
×
656
    QUERY_CHECK_CODE(pHandle->api.metaReaderFn.getTableEntryByUid(&mr, *id), line, _return);
×
657
    readerInit = true;
×
658

659
    refColsNum = 0;
×
660
    for (int32_t j = 0; j < mr.me.colRef.nCols; j++) {
×
661
      if (mr.me.colRef.pColRef[j].hasRef) {
×
UNCOV
662
        refColsNum++;
×
663
      }
664
    }
665

666
    if (refColsNum <= 0) {
×
UNCOV
667
      pHandle->api.metaReaderFn.clearReader(&mr);
×
UNCOV
668
      readerInit = false;
×
UNCOV
669
      continue;
×
670
    }
671

672
    pTb = taosMemoryCalloc(1, refColsNum * sizeof(SRefColInfo) + sizeof(*pTb));
×
673
    QUERY_CHECK_NULL(pTb, code, line, _return, terrno);
×
674

UNCOV
675
    pTb->uid = mr.me.uid;
×
676
    pTb->numOfColRefs = refColsNum;
×
677
    pTb->refCols = (SRefColInfo*)(pTb + 1);
×
678

679
    refColsNum = 0;
×
680
    tSimpleHashClear(pSrcTbls);
×
681
    for (int32_t j = 0; j < mr.me.colRef.nCols; j++) {
×
UNCOV
682
      if (!mr.me.colRef.pColRef[j].hasRef) {
×
683
        continue;
×
684
      }
685

686
      pTb->refCols[refColsNum].colId = mr.me.colRef.pColRef[j].id;
×
687
      tstrncpy(pTb->refCols[refColsNum].refColName, mr.me.colRef.pColRef[j].refColName, TSDB_COL_NAME_LEN);
×
UNCOV
688
      tstrncpy(pTb->refCols[refColsNum].refTableName, mr.me.colRef.pColRef[j].refTableName, TSDB_TABLE_NAME_LEN);
×
UNCOV
689
      tstrncpy(pTb->refCols[refColsNum].refDbName, mr.me.colRef.pColRef[j].refDbName, TSDB_DB_NAME_LEN);
×
690

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

693
      if (NULL == tSimpleHashGet(pSrcTbls, tbFName, strlen(tbFName))) {
×
UNCOV
694
        QUERY_CHECK_CODE(tSimpleHashPut(pSrcTbls, tbFName, strlen(tbFName), &code, sizeof(code)), line, _return);
×
695
      }
696

697
      refColsNum++;
×
698
    }
699

UNCOV
700
    pTb->numOfSrcTbls = tSimpleHashGetSize(pSrcTbls);
×
701
    QUERY_CHECK_NULL(taosArrayPush(*ppRes, &pTb), code, line, _return, terrno);
×
UNCOV
702
    pTb = NULL;
×
703

704
    pHandle->api.metaReaderFn.clearReader(&mr);
×
705
    readerInit = false;
×
706
  }
707

708
_return:
×
709

UNCOV
710
  if (readerInit) {
×
UNCOV
711
    pHandle->api.metaReaderFn.clearReader(&mr);
×
712
  }
713

714
  taosArrayDestroy(pList);
×
715
  taosMemoryFree(pTb);
×
UNCOV
716
  tSimpleHashCleanup(pSrcTbls);
×
717

718
  if (code) {
×
719
    qError("%s failed since %s", __func__, tstrerror(code));
×
720
  }
UNCOV
721
  return code;
×
722
}
723

724
int32_t vnodeReadVStbRefDbs(SReadHandle* pHandle, int64_t suid, SArray** ppRes) {
4,374,950✔
725
  int32_t                    code = TSDB_CODE_SUCCESS;
4,374,950✔
726
  int32_t                    line = 0;
4,374,950✔
727
  SMetaReader                mr = {0};
4,374,950✔
728
  bool                       readerInit = false;
4,370,644✔
729
  SSHashObj*                 pDbNameHash = NULL;
4,370,644✔
730
  SArray*                    pList = NULL;
4,370,644✔
731

732
  pList = taosArrayInit(10, sizeof(uint64_t));
4,370,644✔
733
  QUERY_CHECK_NULL(pList, code, line, _return, terrno);
4,363,295✔
734

735
  *ppRes = taosArrayInit(10, POINTER_BYTES);
4,363,295✔
736
  QUERY_CHECK_NULL(*ppRes, code, line, _return, terrno)
4,366,770✔
737
  
738
  // lookup in cache
739
  code = pHandle->api.metaFn.metaGetCachedRefDbs(pHandle->vnode, suid, *ppRes);
4,367,906✔
740
  QUERY_CHECK_CODE(code, line, _return);
4,382,603✔
741

742
  if (taosArrayGetSize(*ppRes) > 0) {
4,382,603✔
743
    // found in cache
744
    goto _return;
4,350,253✔
745
  } else {
746
    code = pHandle->api.metaFn.getChildTableList(pHandle->vnode, suid, pList);
32,350✔
747
    QUERY_CHECK_CODE(code, line, _return);
32,350✔
748

749
    size_t num = taosArrayGetSize(pList);
32,350✔
750
    pDbNameHash = tSimpleHashInit(10, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
32,350✔
751
    QUERY_CHECK_NULL(pDbNameHash, code, line, _return, terrno);
32,350✔
752

753
    for (int32_t i = 0; i < num; ++i) {
106,332✔
754
      uint64_t* id = taosArrayGet(pList, i);
73,982✔
755
      QUERY_CHECK_NULL(id, code, line, _return, terrno);
73,982✔
756

757
      pHandle->api.metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pHandle->api.metaFn);
73,982✔
758
      readerInit = true;
73,982✔
759

760
      code = pHandle->api.metaReaderFn.getTableEntryByUid(&mr, *id);
73,982✔
761
      QUERY_CHECK_CODE(code, line, _return);
73,982✔
762

763
      for (int32_t j = 0; j < mr.me.colRef.nCols; j++) {
34,881,139✔
764
        if (mr.me.colRef.pColRef[j].hasRef) {
34,807,356✔
765
          if (NULL == tSimpleHashGet(pDbNameHash, mr.me.colRef.pColRef[j].refDbName, strlen(mr.me.colRef.pColRef[j].refDbName))) {
34,498,676✔
766
            char *refDbName = taosStrdup(mr.me.colRef.pColRef[j].refDbName);
27,855✔
767
            QUERY_CHECK_NULL(refDbName, code, line, _return, terrno);
27,855✔
768

769
            QUERY_CHECK_NULL(taosArrayPush(*ppRes, &refDbName), code, line, _return, terrno);
55,710✔
770

771
            code = tSimpleHashPut(pDbNameHash, refDbName, strlen(refDbName), NULL, 0);
27,855✔
772
            QUERY_CHECK_CODE(code, line, _return);
27,855✔
773
          }
774
        }
775
      }
776

777
      pHandle->api.metaReaderFn.clearReader(&mr);
73,783✔
778
      readerInit = false;
73,982✔
779
    }
780

781
    code = pHandle->api.metaFn.metaPutRefDbsToCache(pHandle->vnode, suid, *ppRes);
32,350✔
782
    QUERY_CHECK_CODE(code, line, _return);
32,350✔
783
  }
784

785
_return:
4,383,001✔
786

787
  if (readerInit) {
4,382,603✔
UNCOV
788
    pHandle->api.metaReaderFn.clearReader(&mr);
×
789
  }
790

791
  taosArrayDestroy(pList);
4,382,603✔
792
  tSimpleHashCleanup(pDbNameHash);
4,380,485✔
793

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

UNCOV
800
int32_t vnodeGetVSubtablesMeta(SVnode *pVnode, SRpcMsg *pMsg) {
×
UNCOV
801
  int32_t        code = 0;
×
UNCOV
802
  int32_t        rspSize = 0;
×
UNCOV
803
  SVSubTablesReq req = {0};
×
804
  SVSubTablesRsp rsp = {0};
×
805
  SRpcMsg      rspMsg = {0};
×
806
  void        *pRsp = NULL;
×
807
  int32_t      line = 0;
×
808

809
  if (tDeserializeSVSubTablesReq(pMsg->pCont, pMsg->contLen, &req)) {
×
810
    code = terrno;
×
811
    qError("tDeserializeSVSubTablesReq failed");
×
UNCOV
812
    goto _return;
×
813
  }
814

815
  SReadHandle handle = {0};
×
816
  handle.vnode = pVnode;
×
UNCOV
817
  initStorageAPI(&handle.api);
×
818

819
  QUERY_CHECK_CODE(vnodeReadVSubtables(&handle, req.suid, &rsp.pTables), line, _return);
×
820
  rsp.vgId = TD_VID(pVnode);
×
821

UNCOV
822
  rspSize = tSerializeSVSubTablesRsp(NULL, 0, &rsp);
×
823
  if (rspSize < 0) {
×
824
    code = rspSize;
×
UNCOV
825
    qError("tSerializeSVSubTablesRsp failed, error:%d", rspSize);
×
826
    goto _return;
×
827
  }
828
  pRsp = taosMemoryCalloc(1, rspSize);
×
829
  if (pRsp == NULL) {
×
830
    code = terrno;
×
UNCOV
831
    qError("rpcMallocCont %d failed, error:%d", rspSize, terrno);
×
832
    goto _return;
×
833
  }
834
  rspSize = tSerializeSVSubTablesRsp(pRsp, rspSize, &rsp);
×
835
  if (rspSize < 0) {
×
836
    code = rspSize;
×
UNCOV
837
    qError("tSerializeSVSubTablesRsp failed, error:%d", rspSize);
×
838
    goto _return;
×
839
  }
840

841
_return:
×
842

UNCOV
843
  rspMsg.info = pMsg->info;
×
UNCOV
844
  rspMsg.pCont = pRsp;
×
845
  rspMsg.contLen = rspSize;
×
UNCOV
846
  rspMsg.code = code;
×
847
  rspMsg.msgType = pMsg->msgType;
×
848

849
  if (code) {
×
850
    qError("vnd get virtual subtables failed cause of %s", tstrerror(code));
×
851
  }
852

853
  *pMsg = rspMsg;
×
854
  
UNCOV
855
  tDestroySVSubTablesRsp(&rsp);
×
856

857
  //tmsgSendRsp(&rspMsg);
858

859
  return code;
×
860
}
861

862
int32_t vnodeGetVStbRefDbs(SVnode *pVnode, SRpcMsg *pMsg) {
4,365,229✔
863
  int32_t        code = 0;
4,365,229✔
864
  int32_t        rspSize = 0;
4,365,229✔
865
  SVStbRefDbsReq req = {0};
4,365,229✔
866
  SVStbRefDbsRsp rsp = {0};
4,367,283✔
867
  SRpcMsg        rspMsg = {0};
4,367,772✔
868
  void          *pRsp = NULL;
4,367,772✔
869
  int32_t        line = 0;
4,367,772✔
870

871
  if (tDeserializeSVStbRefDbsReq(pMsg->pCont, pMsg->contLen, &req)) {
4,367,772✔
UNCOV
872
    code = terrno;
×
UNCOV
873
    qError("tDeserializeSVSubTablesReq failed");
×
UNCOV
874
    goto _return;
×
875
  }
876

877
  SReadHandle handle = {0};
4,367,000✔
878
  handle.vnode = pVnode;
4,374,367✔
879
  initStorageAPI(&handle.api);
4,374,367✔
880

881
  code = vnodeReadVStbRefDbs(&handle, req.suid, &rsp.pDbs);
4,376,534✔
882
  QUERY_CHECK_CODE(code, line, _return);
4,379,254✔
883
  rsp.vgId = TD_VID(pVnode);
4,379,254✔
884

885
  rspSize = tSerializeSVStbRefDbsRsp(NULL, 0, &rsp);
4,380,413✔
886
  if (rspSize < 0) {
4,366,723✔
UNCOV
887
    code = rspSize;
×
UNCOV
888
    qError("tSerializeSVStbRefDbsRsp failed, error:%d", rspSize);
×
UNCOV
889
    goto _return;
×
890
  }
891
  pRsp = taosMemoryCalloc(1, rspSize);
4,366,723✔
892
  if (pRsp == NULL) {
4,368,035✔
893
    code = terrno;
×
UNCOV
894
    qError("rpcMallocCont %d failed, error:%d", rspSize, terrno);
×
UNCOV
895
    goto _return;
×
896
  }
897
  rspSize = tSerializeSVStbRefDbsRsp(pRsp, rspSize, &rsp);
4,368,035✔
898
  if (rspSize < 0) {
4,375,454✔
899
    code = rspSize;
×
UNCOV
900
    qError("tSerializeSVStbRefDbsRsp failed, error:%d", rspSize);
×
UNCOV
901
    goto _return;
×
902
  }
903

904
_return:
4,375,454✔
905

906
  rspMsg.info = pMsg->info;
4,381,583✔
907
  rspMsg.pCont = pRsp;
4,378,353✔
908
  rspMsg.contLen = rspSize;
4,378,353✔
909
  rspMsg.code = code;
4,378,353✔
910
  rspMsg.msgType = pMsg->msgType;
4,378,353✔
911

912
  if (code) {
4,376,460✔
UNCOV
913
    qError("vnd get virtual stb ref db failed cause of %s", tstrerror(code));
×
914
  }
915

916
  *pMsg = rspMsg;
4,376,460✔
917

918
  tDestroySVStbRefDbsRsp(&rsp);
4,369,384✔
919

920
  return code;
4,371,196✔
921
}
922

923
static int32_t vnodeGetCompStorage(SVnode *pVnode, int64_t *output) {
119,534,262✔
924
  int32_t code = 0;
119,534,262✔
925
#ifdef TD_ENTERPRISE
926
  int32_t now = taosGetTimestampSec();
119,534,262✔
927
  if (llabs(now - pVnode->config.vndStats.storageLastUpd) >= 30) {
119,534,262✔
928
    pVnode->config.vndStats.storageLastUpd = now;
6,304,987✔
929

930
    SDbSizeStatisInfo info = {0};
6,304,987✔
931
    if (0 == (code = vnodeGetDBSize(pVnode, &info))) {
6,304,987✔
932
      int64_t compSize =
6,304,987✔
933
          info.l1Size + info.l2Size + info.l3Size + info.cacheSize + info.walSize + info.metaSize + +info.ssSize;
6,304,987✔
934
      if (compSize >= 0) {
6,304,987✔
935
        pVnode->config.vndStats.compStorage = compSize;
6,304,987✔
936
      } else {
UNCOV
937
        vError("vnode get comp storage failed since compSize is negative:%" PRIi64, compSize);
×
UNCOV
938
        code = TSDB_CODE_APP_ERROR;
×
939
      }
940
    } else {
941
      vWarn("vnode get comp storage failed since %s", tstrerror(code));
×
942
    }
943
  }
944
  if (output) *output = pVnode->config.vndStats.compStorage;
119,534,262✔
945
#endif
946
  return code;
119,534,262✔
947
}
948

949
static void vnodeGetBufferInfo(SVnode *pVnode, int64_t *bufferSegmentUsed, int64_t *bufferSegmentSize) {
119,534,262✔
950
  *bufferSegmentUsed = 0;
119,534,262✔
951
  *bufferSegmentSize = 0;
119,534,262✔
952
  if (pVnode) {
119,534,262✔
953
    (void)taosThreadMutexLock(&pVnode->mutex);
119,534,262✔
954

955
    if (pVnode->inUse) {
119,534,262✔
956
      *bufferSegmentUsed = pVnode->inUse->size;
119,522,685✔
957
    }
958
    *bufferSegmentSize = pVnode->config.szBuf / VNODE_BUFPOOL_SEGMENTS;
119,534,262✔
959

960
    (void)taosThreadMutexUnlock(&pVnode->mutex);
119,534,262✔
961
  }
962
}
119,534,262✔
963

964
int32_t vnodeGetLoad(SVnode *pVnode, SVnodeLoad *pLoad) {
119,534,262✔
965
  SSyncState state = syncGetState(pVnode->sync);
119,534,262✔
966
  pLoad->syncAppliedIndex = pVnode->state.applied;
119,534,262✔
967
  syncGetCommitIndex(pVnode->sync, &pLoad->syncCommitIndex);
119,534,262✔
968

969
  pLoad->vgId = TD_VID(pVnode);
119,534,262✔
970
  pLoad->syncState = state.state;
119,534,262✔
971
  pLoad->syncRestore = state.restored;
119,534,262✔
972
  pLoad->syncTerm = state.term;
119,534,262✔
973
  pLoad->roleTimeMs = state.roleTimeMs;
119,534,262✔
974
  pLoad->startTimeMs = state.startTimeMs;
119,534,262✔
975
  pLoad->syncCanRead = state.canRead;
119,534,262✔
976
  pLoad->cacheUsage = tsdbCacheGetUsage(pVnode);
119,534,262✔
977
  pLoad->numOfCachedTables = tsdbCacheGetElems(pVnode);
119,534,262✔
978
  VNODE_DO_META_QUERY(pVnode, pLoad->numOfTables = metaGetTbNum(pVnode->pMeta));
119,534,262✔
979
  VNODE_DO_META_QUERY(pVnode, pLoad->numOfTimeSeries = metaGetTimeSeriesNum(pVnode->pMeta, 1));
119,534,262✔
980
  pLoad->totalStorage = (int64_t)3 * 1073741824;  // TODO
119,534,262✔
981
  (void)vnodeGetCompStorage(pVnode, &pLoad->compStorage);
119,534,262✔
982
  pLoad->pointsWritten = 100;
119,534,262✔
983
  pLoad->numOfSelectReqs = 1;
119,534,262✔
984
  pLoad->numOfInsertReqs = atomic_load_64(&pVnode->statis.nInsert);
119,534,262✔
985
  pLoad->numOfInsertSuccessReqs = atomic_load_64(&pVnode->statis.nInsertSuccess);
119,534,262✔
986
  pLoad->numOfBatchInsertReqs = atomic_load_64(&pVnode->statis.nBatchInsert);
119,534,262✔
987
  pLoad->numOfBatchInsertSuccessReqs = atomic_load_64(&pVnode->statis.nBatchInsertSuccess);
119,534,262✔
988
  vnodeGetBufferInfo(pVnode, &pLoad->bufferSegmentUsed, &pLoad->bufferSegmentSize);
119,534,262✔
989
  vDebug("vgId:%d, get vnode load, state:%s snapSeq:%d, learnerProgress:%d, totalIndex:%" PRId64, TD_VID(pVnode),
119,534,262✔
990
         syncStr(state.state), state.snapSeq, state.progress, state.totalIndex);
991
  pLoad->learnerProgress = state.progress;
119,534,262✔
992
  pLoad->snapSeq = state.snapSeq;
119,534,262✔
993
  pLoad->syncTotalIndex = state.totalIndex;
119,534,262✔
994
  return 0;
119,534,262✔
995
}
996

UNCOV
997
int32_t vnodeGetLoadLite(SVnode *pVnode, SVnodeLoadLite *pLoad) {
×
UNCOV
998
  SSyncState syncState = syncGetState(pVnode->sync);
×
UNCOV
999
  if (syncState.state == TAOS_SYNC_STATE_LEADER || syncState.state == TAOS_SYNC_STATE_ASSIGNED_LEADER) {
×
UNCOV
1000
    pLoad->vgId = TD_VID(pVnode);
×
1001
    pLoad->nTimeSeries = metaGetTimeSeriesNum(pVnode->pMeta, 1);
×
1002
    return 0;
×
1003
  }
1004
  return -1;
×
1005
}
1006
/**
1007
 * @brief Reset the statistics value by monitor interval
1008
 *
1009
 * @param pVnode
1010
 * @param pLoad
1011
 */
1012
void vnodeResetLoad(SVnode *pVnode, SVnodeLoad *pLoad) {
130✔
1013
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nInsert, pLoad->numOfInsertReqs, 64, "nInsert");
130✔
1014
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nInsertSuccess, pLoad->numOfInsertSuccessReqs, 64, "nInsertSuccess");
130✔
1015
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nBatchInsert, pLoad->numOfBatchInsertReqs, 64, "nBatchInsert");
130✔
1016
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nBatchInsertSuccess, pLoad->numOfBatchInsertSuccessReqs, 64,
130✔
1017
                            "nBatchInsertSuccess");
1018
}
130✔
1019

1020
void vnodeGetInfo(void *pVnode, const char **dbname, int32_t *vgId, int64_t *numOfTables, int64_t *numOfNormalTables) {
14,500,375✔
1021
  SVnode    *pVnodeObj = pVnode;
14,500,375✔
1022
  SVnodeCfg *pConf = &pVnodeObj->config;
14,500,375✔
1023

1024
  if (dbname) {
14,500,975✔
1025
    *dbname = pConf->dbname;
14,484,756✔
1026
  }
1027

1028
  if (vgId) {
14,501,967✔
1029
    *vgId = TD_VID(pVnodeObj);
13,779,696✔
1030
  }
1031

1032
  if (numOfTables) {
14,500,846✔
1033
    *numOfTables = pConf->vndStats.numOfNTables + pConf->vndStats.numOfCTables +
16,688✔
1034
                   pConf->vndStats.numOfVTables + pConf->vndStats.numOfVCTables;
8,344✔
1035
  }
1036

1037
  if (numOfNormalTables) {
14,500,846✔
1038
    *numOfNormalTables = pConf->vndStats.numOfNTables +
14,772✔
1039
                         pConf->vndStats.numOfVTables;
7,386✔
1040
  }
1041
}
14,500,846✔
1042

1043
int32_t vnodeGetTableList(void *pVnode, int8_t type, SArray *pList) {
7,386✔
1044
  if (type == TSDB_SUPER_TABLE) {
7,386✔
1045
    return vnodeGetStbIdList(pVnode, 0, pList);
7,386✔
1046
  } else {
UNCOV
1047
    return TSDB_CODE_INVALID_PARA;
×
1048
  }
1049
}
1050

1051
int32_t vnodeGetAllTableList(SVnode *pVnode, uint64_t uid, SArray *list) {
×
UNCOV
1052
  int32_t      code = TSDB_CODE_SUCCESS;
×
UNCOV
1053
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, uid, 1);
×
UNCOV
1054
  if (NULL == pCur) {
×
1055
    qError("vnode get all table list failed");
×
1056
    return terrno;
×
1057
  }
1058

1059
  while (1) {
×
1060
    tb_uid_t id = metaCtbCursorNext(pCur);
×
UNCOV
1061
    if (id == 0) {
×
UNCOV
1062
      break;
×
1063
    }
1064

1065
    STableKeyInfo info = {uid = id};
×
1066
    if (NULL == taosArrayPush(list, &info)) {
×
UNCOV
1067
      qError("taosArrayPush failed");
×
UNCOV
1068
      code = terrno;
×
1069
      goto _exit;
×
1070
    }
1071
  }
1072
_exit:
×
1073
  metaCloseCtbCursor(pCur);
×
UNCOV
1074
  return code;
×
1075
}
1076

1077
int32_t vnodeGetCtbIdListByFilter(SVnode *pVnode, int64_t suid, SArray *list, bool (*filter)(void *arg), void *arg) {
×
1078
  return 0;
×
1079
}
1080

1081
int32_t vnodeGetCtbIdList(void *pVnode, int64_t suid, SArray *list) {
96,304,425✔
1082
  int32_t      code = TSDB_CODE_SUCCESS;
96,304,425✔
1083
  SVnode      *pVnodeObj = pVnode;
96,304,425✔
1084
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnodeObj, suid, 1);
96,304,425✔
1085
  if (NULL == pCur) {
96,296,315✔
UNCOV
1086
    qError("vnode get all table list failed");
×
UNCOV
1087
    return terrno;
×
1088
  }
1089

1090
  while (1) {
428,393,351✔
1091
    tb_uid_t id = metaCtbCursorNext(pCur);
524,689,666✔
1092
    if (id == 0) {
524,675,709✔
1093
      break;
96,335,671✔
1094
    }
1095
    qTrace("vnodeGetCtbIdList: got ctb id %" PRId64 " for suid %" PRId64, id, suid);
428,340,038✔
1096
    if (NULL == taosArrayPush(list, &id)) {
428,407,305✔
UNCOV
1097
      qError("taosArrayPush failed");
×
UNCOV
1098
      code = terrno;
×
UNCOV
1099
      goto _exit;
×
1100
    }
1101
  }
1102

1103
_exit:
96,282,232✔
1104
  metaCloseCtbCursor(pCur);
96,282,232✔
1105
  return code;
96,233,079✔
1106
}
1107

1108
int32_t vnodeGetStbIdList(SVnode *pVnode, int64_t suid, SArray *list) {
57,617,355✔
1109
  int32_t      code = TSDB_CODE_SUCCESS;
57,617,355✔
1110
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, suid);
57,617,355✔
1111
  if (!pCur) {
57,617,972✔
UNCOV
1112
    return TSDB_CODE_OUT_OF_MEMORY;
×
1113
  }
1114

1115
  while (1) {
43,671,904✔
1116
    tb_uid_t id = metaStbCursorNext(pCur);
101,289,876✔
1117
    if (id == 0) {
101,286,038✔
1118
      break;
57,621,235✔
1119
    }
1120

1121
    if (NULL == taosArrayPush(list, &id)) {
43,670,206✔
UNCOV
1122
      qError("taosArrayPush failed");
×
UNCOV
1123
      code = terrno;
×
UNCOV
1124
      goto _exit;
×
1125
    }
1126
  }
1127

1128
_exit:
57,616,916✔
1129
  metaCloseStbCursor(pCur);
57,616,916✔
1130
  return code;
57,614,763✔
1131
}
1132

1133
int32_t vnodeGetStbIdListByFilter(SVnode *pVnode, int64_t suid, SArray *list, bool (*filter)(void *arg, void *arg1),
270✔
1134
                                  void *arg) {
1135
  int32_t      code = TSDB_CODE_SUCCESS;
270✔
1136
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, suid);
270✔
1137
  if (!pCur) {
270✔
UNCOV
1138
    return terrno;
×
1139
  }
1140

1141
  while (1) {
1,350✔
1142
    tb_uid_t id = metaStbCursorNext(pCur);
1,620✔
1143
    if (id == 0) {
1,620✔
1144
      break;
270✔
1145
    }
1146

1147
    if ((*filter) && (*filter)(arg, &id)) {
1,350✔
1148
      continue;
1,350✔
1149
    }
1150

UNCOV
1151
    if (NULL == taosArrayPush(list, &id)) {
×
UNCOV
1152
      qError("taosArrayPush failed");
×
UNCOV
1153
      code = terrno;
×
UNCOV
1154
      goto _exit;
×
1155
    }
1156
  }
1157

1158
_exit:
270✔
1159
  metaCloseStbCursor(pCur);
270✔
1160
  return code;
270✔
1161
}
1162

1163
int32_t vnodeGetCtbNum(SVnode *pVnode, int64_t suid, int64_t *num) {
4,324,397✔
1164
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, suid, 0);
4,324,397✔
1165
  if (!pCur) {
4,325,336✔
UNCOV
1166
    return terrno;
×
1167
  }
1168

1169
  *num = 0;
4,325,336✔
1170
  while (1) {
2,492,373✔
1171
    tb_uid_t id = metaCtbCursorNext(pCur);
6,817,519✔
1172
    if (id == 0) {
6,818,364✔
1173
      break;
4,325,991✔
1174
    }
1175

1176
    ++(*num);
2,492,373✔
1177
  }
1178

1179
  metaCloseCtbCursor(pCur);
4,325,991✔
1180
  return TSDB_CODE_SUCCESS;
4,324,725✔
1181
}
1182

1183
int32_t vnodeGetStbColumnNum(SVnode *pVnode, tb_uid_t suid, int *num) {
4,325,991✔
1184
  SSchemaWrapper *pSW = metaGetTableSchema(pVnode->pMeta, suid, -1, 0, NULL, 0);
4,325,991✔
1185
  if (pSW) {
4,325,991✔
1186
    *num = pSW->nCols;
4,325,991✔
1187
    tDeleteSchemaWrapper(pSW);
1188
  } else {
UNCOV
1189
    *num = 2;
×
1190
  }
1191

1192
  return TSDB_CODE_SUCCESS;
4,325,094✔
1193
}
1194

1195
int32_t vnodeGetStbInfo(SVnode *pVnode, tb_uid_t suid, int64_t *keep, int8_t *flags) {
4,325,697✔
1196
  SMetaReader mr = {0};
4,325,697✔
1197
  metaReaderDoInit(&mr, pVnode->pMeta, META_READER_NOLOCK);
4,325,991✔
1198

1199
  int32_t code = metaReaderGetTableEntryByUid(&mr, suid);
4,324,412✔
1200
  if (code == TSDB_CODE_SUCCESS) {
4,325,651✔
1201
    if (keep) *keep = mr.me.stbEntry.keep;
4,325,651✔
1202
    if (flags) *flags = mr.me.flags;
4,325,651✔
1203
  } else {
UNCOV
1204
    if (keep) *keep = 0;
×
UNCOV
1205
    if (flags) *flags = 0;
×
1206
  }
1207

1208
  metaReaderClear(&mr);
4,325,651✔
1209
  return TSDB_CODE_SUCCESS;
4,325,651✔
1210
}
1211

1212
#ifdef TD_ENTERPRISE
1213
const char *tkLogStb[] = {"cluster_info",
1214
                          "data_dir",
1215
                          "dnodes_info",
1216
                          "d_info",
1217
                          "grants_info",
1218
                          "keeper_monitor",
1219
                          "logs",
1220
                          "log_dir",
1221
                          "log_summary",
1222
                          "m_info",
1223
                          "taosadapter_restful_http_request_fail",
1224
                          "taosadapter_restful_http_request_in_flight",
1225
                          "taosadapter_restful_http_request_summary_milliseconds",
1226
                          "taosadapter_restful_http_request_total",
1227
                          "taosadapter_system_cpu_percent",
1228
                          "taosadapter_system_mem_percent",
1229
                          "temp_dir",
1230
                          "vgroups_info",
1231
                          "vnodes_role"};
1232
const char *tkAuditStb[] = {"operations"};
1233
const int   tkLogStbNum = ARRAY_SIZE(tkLogStb);
1234
const int   tkAuditStbNum = ARRAY_SIZE(tkAuditStb);
1235

1236
// exclude stbs of taoskeeper log
1237
static int32_t vnodeGetTimeSeriesBlackList(SVnode *pVnode, int32_t *tbSize) {
57,611,087✔
1238
  int32_t      code = TSDB_CODE_SUCCESS;
57,611,087✔
1239
  int32_t      tbNum = 0;
57,611,087✔
1240
  const char **pTbArr = NULL;
57,611,087✔
1241
  const char  *dbName = NULL;
57,611,087✔
1242
  *tbSize = 0;
57,611,087✔
1243

1244
  if (!(dbName = strchr(pVnode->config.dbname, '.'))) return 0;
57,612,321✔
1245
  if (0 == strncmp(++dbName, "log", TSDB_DB_NAME_LEN)) {
57,610,801✔
1246
    tbNum = tkLogStbNum;
2,160✔
1247
    pTbArr = (const char **)&tkLogStb;
2,160✔
1248
  } else if (0 == strncmp(dbName, "audit", TSDB_DB_NAME_LEN)) {
57,610,492✔
1249
    tbNum = tkAuditStbNum;
2,080✔
1250
    pTbArr = (const char **)&tkAuditStb;
2,080✔
1251
  }
1252
  if (tbNum && pTbArr) {
57,612,652✔
1253
    *tbSize = metaSizeOfTbFilterCache(pVnode->pMeta, 0);
4,240✔
1254
    if (*tbSize < tbNum) {
4,240✔
1255
      for (int32_t i = 0; i < tbNum; ++i) {
35,210✔
1256
        tb_uid_t suid = metaGetTableEntryUidByName(pVnode->pMeta, pTbArr[i]);
32,320✔
1257
        if (suid != 0) {
32,320✔
1258
          code = metaPutTbToFilterCache(pVnode->pMeta, &suid, 0);
2,700✔
1259
          if (TSDB_CODE_SUCCESS != code) {
2,700✔
1260
            return code;
1,350✔
1261
          }
1262
        }
1263
      }
1264
      *tbSize = metaSizeOfTbFilterCache(pVnode->pMeta, 0);
2,890✔
1265
    }
1266
  }
1267

1268
  return code;
57,610,354✔
1269
}
1270
#endif
1271

1272
static bool vnodeTimeSeriesFilter(void *arg1, void *arg2) {
1,350✔
1273
  SVnode *pVnode = (SVnode *)arg1;
1,350✔
1274

1275
  if (metaTbInFilterCache(pVnode->pMeta, arg2, 0)) {
1,350✔
1276
    return true;
1,350✔
1277
  }
UNCOV
1278
  return false;
×
1279
}
1280

1281
int32_t vnodeGetTimeSeriesNum(SVnode *pVnode, int64_t *num) {
57,610,470✔
1282
  SArray *suidList = NULL;
57,610,470✔
1283

1284
  if (!(suidList = taosArrayInit(1, sizeof(tb_uid_t)))) {
57,610,470✔
UNCOV
1285
    return terrno;
×
1286
  }
1287

1288
  int32_t tbFilterSize = 0;
57,612,035✔
1289
  int32_t code = TSDB_CODE_SUCCESS;
57,612,035✔
1290
#ifdef TD_ENTERPRISE
1291
  code = vnodeGetTimeSeriesBlackList(pVnode, &tbFilterSize);
57,612,035✔
1292
  if (TSDB_CODE_SUCCESS != code) {
57,610,470✔
1293
    goto _exit;
1,350✔
1294
  }
1295
#endif
1296

1297
  if ((!tbFilterSize && vnodeGetStbIdList(pVnode, 0, suidList) < 0) ||
57,609,120✔
1298
      (tbFilterSize && vnodeGetStbIdListByFilter(pVnode, 0, suidList, vnodeTimeSeriesFilter, pVnode) < 0)) {
57,610,068✔
UNCOV
1299
    qError("vgId:%d, failed to get stb id list error: %s", TD_VID(pVnode), terrstr());
×
UNCOV
1300
    taosArrayDestroy(suidList);
×
UNCOV
1301
    return terrno;
×
1302
  }
1303

1304
  *num = 0;
57,606,528✔
1305
  int64_t arrSize = taosArrayGetSize(suidList);
57,609,120✔
1306
  for (int64_t i = 0; i < arrSize; ++i) {
101,267,078✔
1307
    tb_uid_t suid = *(tb_uid_t *)taosArrayGet(suidList, i);
43,654,570✔
1308

1309
    int64_t ctbNum = 0;
43,661,071✔
1310
    int32_t numOfCols = 0;
43,662,305✔
1311
    int8_t  flags = 0;
43,656,438✔
1312
    code = metaGetStbStats(pVnode, suid, &ctbNum, &numOfCols, &flags);
43,657,986✔
1313
    if (TSDB_CODE_SUCCESS != code) {
43,667,858✔
UNCOV
1314
      goto _exit;
×
1315
    }
1316
    if (!TABLE_IS_VIRTUAL(flags)) {
43,667,858✔
1317
      *num += ctbNum * (numOfCols - 1);
40,630,012✔
1318
    }
1319
  }
1320

1321
_exit:
57,613,858✔
1322
  taosArrayDestroy(suidList);
57,615,092✔
1323
  return TSDB_CODE_SUCCESS;
57,608,546✔
1324
}
1325

UNCOV
1326
int32_t vnodeGetAllCtbNum(SVnode *pVnode, int64_t *num) {
×
UNCOV
1327
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, 0);
×
UNCOV
1328
  if (!pCur) {
×
UNCOV
1329
    return terrno;
×
1330
  }
1331

1332
  *num = 0;
×
1333
  while (1) {
×
UNCOV
1334
    tb_uid_t id = metaStbCursorNext(pCur);
×
UNCOV
1335
    if (id == 0) {
×
1336
      break;
×
1337
    }
1338

1339
    int64_t ctbNum = 0;
×
1340
    int32_t code = vnodeGetCtbNum(pVnode, id, &ctbNum);
×
UNCOV
1341
    if (TSDB_CODE_SUCCESS != code) {
×
UNCOV
1342
      metaCloseStbCursor(pCur);
×
1343
      return code;
×
1344
    }
1345

1346
    *num += ctbNum;
×
1347
  }
1348

UNCOV
1349
  metaCloseStbCursor(pCur);
×
1350
  return TSDB_CODE_SUCCESS;
×
1351
}
1352

1353
void *vnodeGetIdx(void *pVnode) {
3,889,871✔
1354
  if (pVnode == NULL) {
3,889,871✔
UNCOV
1355
    return NULL;
×
1356
  }
1357

1358
  return metaGetIdx(((SVnode *)pVnode)->pMeta);
3,889,871✔
1359
}
1360

1361
void *vnodeGetIvtIdx(void *pVnode) {
3,889,871✔
1362
  if (pVnode == NULL) {
3,889,871✔
UNCOV
1363
    return NULL;
×
1364
  }
1365
  return metaGetIvtIdx(((SVnode *)pVnode)->pMeta);
3,889,871✔
1366
}
1367

1368
int32_t vnodeGetTableSchema(void *pVnode, int64_t uid, STSchema **pSchema, int64_t *suid, SSchemaWrapper **pTagSchema) {
247,703✔
1369
  return tsdbGetTableSchema(((SVnode *)pVnode)->pMeta, uid, pSchema, suid, pTagSchema);
247,703✔
1370
}
1371

1372
static FORCE_INLINE int32_t vnodeGetDBPrimaryInfo(SVnode *pVnode, SDbSizeStatisInfo *pInfo) {
1373
  int32_t code = 0;
6,310,629✔
1374
  char    path[TSDB_FILENAME_LEN] = {0};
6,310,629✔
1375

1376
  char   *dirName[] = {VNODE_TSDB_DIR, VNODE_WAL_DIR, VNODE_META_DIR, VNODE_TSDB_CACHE_DIR};
6,310,629✔
1377
  int64_t dirSize[4];
6,310,181✔
1378

1379
  vnodeGetPrimaryPath(pVnode, false, path, TSDB_FILENAME_LEN);
6,310,629✔
1380
  int32_t offset = strlen(path);
6,310,629✔
1381

1382
  for (int i = 0; i < sizeof(dirName) / sizeof(dirName[0]); i++) {
31,553,145✔
1383
    int64_t size = {0};
25,242,516✔
1384
    (void)snprintf(path + offset, TSDB_FILENAME_LEN - offset, "%s%s", TD_DIRSEP, dirName[i]);
25,242,516✔
1385
    code = taosGetDirSize(path, &size);
25,242,516✔
1386
    if (code != 0) {
25,242,516✔
1387
      uWarn("vnode %d get dir %s %s size failed since %s", TD_VID(pVnode), path, dirName[i], tstrerror(code));
530✔
1388
    }
1389
    path[offset] = 0;
25,242,516✔
1390
    dirSize[i] = size;
25,242,516✔
1391
  }
1392

1393
  pInfo->l1Size = 0;
6,310,629✔
1394
  pInfo->walSize = dirSize[1];
6,310,629✔
1395
  pInfo->metaSize = dirSize[2];
6,310,629✔
1396
  pInfo->cacheSize = dirSize[3];
6,310,629✔
1397
  return code;
6,310,629✔
1398
}
1399
int32_t vnodeGetDBSize(void *pVnode, SDbSizeStatisInfo *pInfo) {
6,310,629✔
1400
  int32_t code = 0;
6,310,629✔
1401
  int32_t lino = 0;
6,310,629✔
1402
  SVnode *pVnodeObj = pVnode;
6,310,629✔
1403
  if (pVnodeObj == NULL) {
6,310,629✔
UNCOV
1404
    return TSDB_CODE_VND_NOT_EXIST;
×
1405
  }
1406
  code = vnodeGetDBPrimaryInfo(pVnode, pInfo);
6,310,629✔
1407
  if (code != 0) goto _exit;
6,310,629✔
1408

1409
  code = tsdbGetFsSize(pVnodeObj->pTsdb, pInfo);
6,310,629✔
1410
_exit:
6,310,629✔
1411
  return code;
6,310,629✔
1412
}
1413

1414
/*
1415
 * Get raw write metrics for a vnode
1416
 */
UNCOV
1417
int32_t vnodeGetRawWriteMetrics(void *pVnode, SRawWriteMetrics *pRawMetrics) {
×
UNCOV
1418
  if (pVnode == NULL || pRawMetrics == NULL) {
×
UNCOV
1419
    return TSDB_CODE_INVALID_PARA;
×
1420
  }
1421

1422
  SVnode      *pVnode1 = (SVnode *)pVnode;
×
1423
  SSyncMetrics syncMetrics = syncGetMetrics(pVnode1->sync);
×
1424

1425
  // Copy values following SRawWriteMetrics structure order
1426
  pRawMetrics->total_requests = atomic_load_64(&pVnode1->writeMetrics.total_requests);
×
1427
  pRawMetrics->total_rows = atomic_load_64(&pVnode1->writeMetrics.total_rows);
×
UNCOV
1428
  pRawMetrics->total_bytes = atomic_load_64(&pVnode1->writeMetrics.total_bytes);
×
UNCOV
1429
  pRawMetrics->fetch_batch_meta_time = atomic_load_64(&pVnode1->writeMetrics.fetch_batch_meta_time);
×
1430
  pRawMetrics->fetch_batch_meta_count = atomic_load_64(&pVnode1->writeMetrics.fetch_batch_meta_count);
×
1431
  pRawMetrics->preprocess_time = atomic_load_64(&pVnode1->writeMetrics.preprocess_time);
×
1432
  pRawMetrics->wal_write_bytes = atomic_load_64(&syncMetrics.wal_write_bytes);
×
1433
  pRawMetrics->wal_write_time = atomic_load_64(&syncMetrics.wal_write_time);
×
1434
  pRawMetrics->apply_bytes = atomic_load_64(&pVnode1->writeMetrics.apply_bytes);
×
1435
  pRawMetrics->apply_time = atomic_load_64(&pVnode1->writeMetrics.apply_time);
×
1436
  pRawMetrics->commit_count = atomic_load_64(&pVnode1->writeMetrics.commit_count);
×
1437
  pRawMetrics->commit_time = atomic_load_64(&pVnode1->writeMetrics.commit_time);
×
1438
  pRawMetrics->memtable_wait_time = atomic_load_64(&pVnode1->writeMetrics.memtable_wait_time);
×
1439
  pRawMetrics->blocked_commit_count = atomic_load_64(&pVnode1->writeMetrics.blocked_commit_count);
×
1440
  pRawMetrics->blocked_commit_time = atomic_load_64(&pVnode1->writeMetrics.block_commit_time);
×
1441
  pRawMetrics->merge_count = atomic_load_64(&pVnode1->writeMetrics.merge_count);
×
1442
  pRawMetrics->merge_time = atomic_load_64(&pVnode1->writeMetrics.merge_time);
×
1443
  pRawMetrics->last_cache_commit_time = atomic_load_64(&pVnode1->writeMetrics.last_cache_commit_time);
×
1444
  pRawMetrics->last_cache_commit_count = atomic_load_64(&pVnode1->writeMetrics.last_cache_commit_count);
×
1445

1446
  return 0;
×
1447
}
1448

1449
/*
1450
 * Reset raw write metrics for a vnode by subtracting old values
1451
 */
UNCOV
1452
int32_t vnodeResetRawWriteMetrics(void *pVnode, const SRawWriteMetrics *pOldMetrics) {
×
UNCOV
1453
  if (pVnode == NULL || pOldMetrics == NULL) {
×
UNCOV
1454
    return TSDB_CODE_INVALID_PARA;
×
1455
  }
1456

1457
  SVnode *pVnode1 = (SVnode *)pVnode;
×
1458

1459
  // Reset vnode write metrics using atomic operations to subtract old values
UNCOV
1460
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.total_requests, pOldMetrics->total_requests);
×
1461
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.total_rows, pOldMetrics->total_rows);
×
UNCOV
1462
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.total_bytes, pOldMetrics->total_bytes);
×
1463

1464
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.fetch_batch_meta_time, pOldMetrics->fetch_batch_meta_time);
×
1465
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.fetch_batch_meta_count, pOldMetrics->fetch_batch_meta_count);
×
1466
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.preprocess_time, pOldMetrics->preprocess_time);
×
UNCOV
1467
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.apply_bytes, pOldMetrics->apply_bytes);
×
1468
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.apply_time, pOldMetrics->apply_time);
×
1469
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.commit_count, pOldMetrics->commit_count);
×
1470

1471
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.commit_time, pOldMetrics->commit_time);
×
1472
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.merge_time, pOldMetrics->merge_time);
×
1473

UNCOV
1474
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.memtable_wait_time, pOldMetrics->memtable_wait_time);
×
1475
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.blocked_commit_count, pOldMetrics->blocked_commit_count);
×
1476
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.block_commit_time, pOldMetrics->blocked_commit_time);
×
UNCOV
1477
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.merge_count, pOldMetrics->merge_count);
×
1478

1479
  // Reset new cache metrics
1480
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.last_cache_commit_time, pOldMetrics->last_cache_commit_time);
×
1481
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.last_cache_commit_count, pOldMetrics->last_cache_commit_count);
×
1482

1483
  // Reset sync metrics
1484
  SSyncMetrics syncMetrics = {
×
1485
      .wal_write_bytes = pOldMetrics->wal_write_bytes,
×
UNCOV
1486
      .wal_write_time = pOldMetrics->wal_write_time,
×
1487
  };
1488
  syncResetMetrics(pVnode1->sync, &syncMetrics);
×
1489

1490
  return 0;
×
1491
}
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