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

taosdata / TDengine / #4944

30 Jan 2026 06:19AM UTC coverage: 66.849% (+0.1%) from 66.718%
#4944

push

travis-ci

web-flow
merge: from main to 3.0 #34453

1124 of 2018 new or added lines in 72 files covered. (55.7%)

13677 existing lines in 155 files now uncovered.

205211 of 306978 relevant lines covered (66.85%)

125657591.7 hits per line

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

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

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

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

36
int32_t fillTableColCmpr(SMetaReader *reader, SSchemaExt *pExt, int32_t numOfCol) {
18,920,212✔
37
  int8_t tblType = reader->me.type;
18,920,212✔
38
  if (withExtSchema(tblType)) {
18,921,466✔
39
    SColCmprWrapper *p = &(reader->me.colCmpr);
18,630,109✔
40
    if (numOfCol != p->nCols) {
18,630,736✔
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++) {
219,168,473✔
45
      SColCmpr *pCmpr = &p->pColCmpr[i];
200,536,115✔
46
      pExt[i].colId = pCmpr->id;
200,537,005✔
47
      pExt[i].compress = pCmpr->alg;
200,537,373✔
48
    }
49
  }
50
  return 0;
18,921,834✔
51
}
52

53
void vnodePrintTableMeta(STableMetaRsp *pMeta) {
18,920,835✔
54
  if (!(qDebugFlag & DEBUG_DEBUG)) {
18,920,835✔
55
    return;
282,374✔
56
  }
57

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

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

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

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

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

130
  if (!reqTbUid) {
27,569,213✔
131
    (void)tsnprintf(tableFName, TSDB_TABLE_FNAME_LEN, "%s.%s", infoReq.dbFName, infoReq.tbName);
27,309,598✔
132
    if (pVnode->mounted) tTrimMountPrefix(tableFName);
27,310,591✔
133
    code = vnodeValidateTableHash(pVnode, tableFName);
27,310,591✔
134
    if (code) {
27,309,964✔
135
      goto _exit4;
×
136
    }
137
  }
138

139
  // query meta
140
  metaReaderDoInit(&mer1, pVnode->pMeta, META_READER_LOCK);
27,569,579✔
141
  if (reqTbUid) {
27,568,586✔
142
    SET_ERRNO(0);
258,622✔
143
    uint64_t tbUid = taosStr2UInt64(infoReq.tbName, NULL, 10);
258,622✔
144
    if (ERRNO == ERANGE || tbUid == 0) {
258,622✔
145
      code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
146
      goto _exit3;
×
147
    }
148
    SMetaReader mr3 = {0};
258,622✔
149
    metaReaderDoInit(&mr3, ((SVnode *)pVnode)->pMeta, META_READER_NOLOCK);
258,622✔
150
    if ((code = metaReaderGetTableEntryByUid(&mr3, tbUid)) < 0) {
258,622✔
151
      metaReaderClear(&mr3);
94,592✔
152
      TAOS_CHECK_GOTO(code, NULL, _exit3);
94,592✔
153
    }
154
    tstrncpy(metaRsp.tbName, mr3.me.name, TSDB_TABLE_NAME_LEN);
164,030✔
155
    metaReaderClear(&mr3);
164,030✔
156
    TAOS_CHECK_GOTO(metaGetTableEntryByName(&mer1, metaRsp.tbName), NULL, _exit3);
164,030✔
157
  } else if (metaGetTableEntryByName(&mer1, infoReq.tbName) < 0) {
27,309,964✔
158
    code = terrno;
8,552,419✔
159
    goto _exit3;
8,552,419✔
160
  }
161

162
  metaRsp.tableType = mer1.me.type;
18,920,841✔
163
  metaRsp.vgId = TD_VID(pVnode);
18,920,841✔
164
  metaRsp.tuid = mer1.me.uid;
18,921,207✔
165
  metaRsp.isAudit = pVnode->config.isAudit ? 1 : 0;
18,921,207✔
166

167
  switch (mer1.me.type) {
18,921,207✔
168
    case TSDB_SUPER_TABLE: {
3,556,812✔
169
      (void)strcpy(metaRsp.stbName, mer1.me.name);
3,556,812✔
170
      schema = mer1.me.stbEntry.schemaRow;
3,556,812✔
171
      schemaTag = mer1.me.stbEntry.schemaTag;
3,556,812✔
172
      metaRsp.suid = mer1.me.uid;
3,556,812✔
173
      metaRsp.virtualStb = TABLE_IS_VIRTUAL(mer1.me.flags);
3,556,812✔
174
      metaRsp.ownerId = mer1.me.stbEntry.ownerId;
3,556,812✔
175
      break;
3,556,812✔
176
    }
177
    case TSDB_CHILD_TABLE:
12,107,597✔
178
    case TSDB_VIRTUAL_CHILD_TABLE:{
179
      metaReaderDoInit(&mer2, pVnode->pMeta, META_READER_NOLOCK);
12,107,597✔
180
      if (metaReaderGetTableEntryByUid(&mer2, mer1.me.ctbEntry.suid) < 0) goto _exit2;
12,107,597✔
181

182
      (void)strcpy(metaRsp.stbName, mer2.me.name);
12,107,229✔
183
      metaRsp.suid = mer2.me.uid;
12,107,229✔
184
      metaRsp.ownerId = mer2.me.stbEntry.ownerId;  // child table inherits ownerId from stb
12,107,229✔
185
      schema = mer2.me.stbEntry.schemaRow;
12,107,229✔
186
      schemaTag = mer2.me.stbEntry.schemaTag;
12,107,229✔
187
      break;
12,107,229✔
188
    }
189
    case TSDB_NORMAL_TABLE:
3,256,798✔
190
    case TSDB_VIRTUAL_NORMAL_TABLE: {
191
      schema = mer1.me.ntbEntry.schemaRow;
3,256,798✔
192
      metaRsp.ownerId = mer1.me.ntbEntry.ownerId;
3,256,798✔
193
      break;
3,256,798✔
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;
18,920,839✔
202
  metaRsp.numOfColumns = schema.nCols;
18,920,839✔
203
  metaRsp.precision = pVnode->config.tsdbCfg.precision;
18,920,839✔
204
  metaRsp.sversion = schema.version;
18,921,466✔
205
  metaRsp.tversion = schemaTag.version;
18,921,466✔
206
  metaRsp.pSchemas = (SSchema *)taosMemoryMalloc(sizeof(SSchema) * (metaRsp.numOfColumns + metaRsp.numOfTags));
18,921,466✔
207
  metaRsp.pSchemaExt = (SSchemaExt *)taosMemoryCalloc(metaRsp.numOfColumns, sizeof(SSchemaExt));
18,919,217✔
208
  if (NULL == metaRsp.pSchemas || NULL == metaRsp.pSchemaExt) {
18,920,839✔
UNCOV
209
    code = terrno;
×
UNCOV
210
    goto _exit;
×
211
  }
212
  (void)memcpy(metaRsp.pSchemas, schema.pSchema, sizeof(SSchema) * schema.nCols);
18,920,839✔
213
  if (schemaTag.nCols) {
18,920,839✔
214
    (void)memcpy(metaRsp.pSchemas + schema.nCols, schemaTag.pSchema, sizeof(SSchema) * schemaTag.nCols);
15,664,041✔
215
  }
216
  if (metaRsp.pSchemaExt) {
18,920,839✔
217
    SMetaReader *pReader = mer1.me.type == TSDB_CHILD_TABLE ? &mer2 : &mer1;
18,921,207✔
218
    code = fillTableColCmpr(pReader, metaRsp.pSchemaExt, metaRsp.numOfColumns);
18,921,207✔
219
    if (code < 0) {
18,921,207✔
UNCOV
220
      goto _exit;
×
221
    }
222
    for (int32_t i = 0; i < metaRsp.numOfColumns && pReader->me.pExtSchemas; i++) {
43,358,141✔
223
      metaRsp.pSchemaExt[i].typeMod = pReader->me.pExtSchemas[i].typeMod;
24,436,934✔
224
    }
225
  } else {
UNCOV
226
    code = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
227
    goto _exit;
×
228
  }
229
  if (hasRefCol(mer1.me.type)) {
18,922,202✔
230
    metaRsp.rversion = mer1.me.colRef.version;
289,844✔
231
    metaRsp.pColRefs = (SColRef*)taosMemoryMalloc(sizeof(SColRef) * metaRsp.numOfColumns);
289,844✔
232
    if (metaRsp.pColRefs) {
291,098✔
233
      code = fillTableColRef(&mer1, metaRsp.pColRefs, metaRsp.numOfColumns);
291,098✔
234
      if (code < 0) {
291,098✔
UNCOV
235
        goto _exit;
×
236
      }
237
    }
238
    metaRsp.numOfColRefs = metaRsp.numOfColumns;
291,098✔
239
  } else {
240
    metaRsp.pColRefs = NULL;
18,630,364✔
241
    metaRsp.numOfColRefs = 0;
18,630,364✔
242
  }
243

244
  vnodePrintTableMeta(&metaRsp);
18,921,462✔
245

246
  // encode and send response
247
  rspLen = tSerializeSTableMetaRsp(NULL, 0, &metaRsp);
18,921,834✔
248
  if (rspLen < 0) {
18,920,839✔
UNCOV
249
    code = terrno;
×
UNCOV
250
    goto _exit;
×
251
  }
252

253
  if (direct) {
18,920,839✔
254
    pRsp = rpcMallocCont(rspLen);
176,644✔
255
  } else {
256
    pRsp = taosMemoryCalloc(1, rspLen);
18,744,195✔
257
  }
258

259
  if (pRsp == NULL) {
18,919,217✔
UNCOV
260
    code = terrno;
×
UNCOV
261
    goto _exit;
×
262
  }
263

264
  rspLen = tSerializeSTableMetaRsp(pRsp, rspLen, &metaRsp);
18,919,217✔
265
  if (rspLen < 0) {
18,921,098✔
UNCOV
266
    code = terrno;
×
UNCOV
267
    goto _exit;
×
268
  }
269

270
_exit:
18,920,730✔
271
  taosMemoryFree(metaRsp.pColRefs);
18,921,017✔
272
  taosMemoryFree(metaRsp.pSchemas);
18,919,725✔
273
  taosMemoryFree(metaRsp.pSchemaExt);
18,921,013✔
274
_exit2:
18,920,580✔
275
  metaReaderClear(&mer2);
18,921,834✔
276
_exit3:
27,566,218✔
277
  metaReaderClear(&mer1);
27,567,591✔
278
_exit4:
27,567,296✔
279
  rpcMsg.info = pMsg->info;
27,567,035✔
280
  rpcMsg.pCont = pRsp;
27,567,662✔
281
  rpcMsg.contLen = rspLen;
27,567,662✔
282
  rpcMsg.code = code;
27,567,662✔
283
  rpcMsg.msgType = pMsg->msgType;
27,567,662✔
284

285
  if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST && autoCreateCtb == 1) {
27,564,940✔
286
    code = TSDB_CODE_SUCCESS;
5,304,244✔
287
  }
288

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

294
  if (direct) {
27,566,024✔
295
    tmsgSendRsp(&rpcMsg);
885,834✔
296
  } else {
297
    *pMsg = rpcMsg;
26,680,190✔
298
  }
299

300
  return code;
27,568,028✔
301
}
302

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

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

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

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

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

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

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

343
  if (mer1.me.type == TSDB_SUPER_TABLE) {
55,601✔
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) {
55,601✔
347
    metaReaderDoInit(&mer2, pVnode->pMeta, META_READER_NOLOCK);
38,338✔
348
    if (metaReaderGetTableEntryByUid(&mer2, mer1.me.ctbEntry.suid) < 0) goto _exit;
38,338✔
349

350
    tstrncpy(cfgRsp.stbName, mer2.me.name, TSDB_TABLE_NAME_LEN);
38,338✔
351
    schema = mer2.me.stbEntry.schemaRow;
38,338✔
352
    schemaTag = mer2.me.stbEntry.schemaTag;
38,338✔
353
    cfgRsp.ownerId = mer2.me.stbEntry.ownerId;  // child table inherits ownerId from stb
38,338✔
354
    cfgRsp.ttl = mer1.me.ctbEntry.ttlDays;
38,338✔
355
    cfgRsp.commentLen = mer1.me.ctbEntry.commentLen;
38,338✔
356
    if (mer1.me.ctbEntry.commentLen > 0) {
38,338✔
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;
38,338✔
364
    cfgRsp.tagsLen = pTag->len;
38,338✔
365
    cfgRsp.pTags = taosMemoryMalloc(cfgRsp.tagsLen);
38,338✔
366
    if (NULL == cfgRsp.pTags) {
38,338✔
UNCOV
367
      code = terrno;
×
UNCOV
368
      goto _exit;
×
369
    }
370
    (void)memcpy(cfgRsp.pTags, pTag, cfgRsp.tagsLen);
38,338✔
371
  } else if (mer1.me.type == TSDB_NORMAL_TABLE || mer1.me.type == TSDB_VIRTUAL_NORMAL_TABLE) {
17,263✔
372
    schema = mer1.me.ntbEntry.schemaRow;
17,263✔
373
    cfgRsp.ttl = mer1.me.ntbEntry.ttlDays;
17,263✔
374
    cfgRsp.ownerId = mer1.me.ntbEntry.ownerId;
17,263✔
375
    cfgRsp.commentLen = mer1.me.ntbEntry.commentLen;
17,263✔
376
    if (mer1.me.ntbEntry.commentLen > 0) {
17,263✔
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;
55,601✔
390
  cfgRsp.numOfColumns = schema.nCols;
55,601✔
391
  cfgRsp.virtualStb = false; // vnode don't have super table, so it's always false
55,601✔
392
  cfgRsp.pSchemas = (SSchema *)taosMemoryMalloc(sizeof(SSchema) * (cfgRsp.numOfColumns + cfgRsp.numOfTags));
55,601✔
393
  cfgRsp.pSchemaExt = (SSchemaExt *)taosMemoryMalloc(cfgRsp.numOfColumns * sizeof(SSchemaExt));
55,601✔
394
  cfgRsp.pColRefs = (SColRef *)taosMemoryMalloc(sizeof(SColRef) * cfgRsp.numOfColumns);
55,601✔
395

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

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

409
  if (withExtSchema(cfgRsp.tableType)) {
55,601✔
410
    for (int32_t i = 0; i < cfgRsp.numOfColumns; i++) {
202,506✔
411
      SColCmpr   *pCmpr = &pColCmpr->pColCmpr[i];
154,304✔
412
      SSchemaExt *pSchExt = cfgRsp.pSchemaExt + i;
154,304✔
413
      pSchExt->colId = pCmpr->id;
154,304✔
414
      pSchExt->compress = pCmpr->alg;
154,304✔
415
      if (pReader->me.pExtSchemas)
154,304✔
416
        pSchExt->typeMod = pReader->me.pExtSchemas[i].typeMod;
23,804✔
417
      else
418
        pSchExt->typeMod = 0;
130,500✔
419
    }
420
  }
421

422
  cfgRsp.virtualStb = false;
55,601✔
423
  if (hasRefCol(cfgRsp.tableType)) {
55,601✔
424
    for (int32_t i = 0; i < cfgRsp.numOfColumns; i++) {
104,503✔
425
      SColRef *pRef = &pColRef->pColRef[i];
97,104✔
426
      cfgRsp.pColRefs[i].hasRef = pRef->hasRef;
97,104✔
427
      cfgRsp.pColRefs[i].id = pRef->id;
97,104✔
428
      if (cfgRsp.pColRefs[i].hasRef) {
97,104✔
429
        tstrncpy(cfgRsp.pColRefs[i].refDbName, pRef->refDbName, TSDB_DB_NAME_LEN);
50,631✔
430
        tstrncpy(cfgRsp.pColRefs[i].refTableName, pRef->refTableName, TSDB_TABLE_NAME_LEN);
50,631✔
431
        tstrncpy(cfgRsp.pColRefs[i].refColName, pRef->refColName, TSDB_COL_NAME_LEN);
50,631✔
432
      }
433
    }
434
  }
435

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

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

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

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

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

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

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

477
  tFreeSTableCfgRsp(&cfgRsp);
55,601✔
478
  metaReaderClear(&mer2);
55,601✔
479
  metaReaderClear(&mer1);
55,601✔
480
  return code;
55,601✔
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) {
26,288,986✔
493
  int32_t      code = 0;
26,288,986✔
494
  int32_t      rspSize = 0;
26,288,986✔
495
  SBatchReq    batchReq = {0};
26,288,986✔
496
  SBatchMsg   *req = NULL;
26,289,438✔
497
  SBatchRspMsg rsp = {0};
26,289,438✔
498
  SBatchRsp    batchRsp = {0};
26,271,425✔
499
  SRpcMsg      reqMsg = *pMsg;
26,269,435✔
500
  SRpcMsg      rspMsg = {0};
26,270,425✔
501
  void        *pRsp = NULL;
26,275,951✔
502

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

509
  int32_t msgNum = taosArrayGetSize(batchReq.pMsgs);
26,282,209✔
510
  if (msgNum >= MAX_META_MSG_IN_BATCH) {
26,289,862✔
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));
26,289,862✔
517
  if (NULL == batchRsp.pRsps) {
26,281,968✔
518
    code = terrno;
1✔
UNCOV
519
    qError("taosArrayInit %d SBatchRspMsg failed", msgNum);
×
UNCOV
520
    goto _exit;
×
521
  }
522

523
  for (int32_t i = 0; i < msgNum; ++i) {
57,446,137✔
524
    req = taosArrayGet(batchReq.pMsgs, i);
31,162,038✔
525
    if (req == NULL) {
31,160,155✔
UNCOV
526
      code = terrno;
×
UNCOV
527
      goto _exit;
×
528
    }
529

530
    reqMsg.msgType = req->msgType;
31,160,155✔
531
    reqMsg.pCont = req->msg;
31,159,116✔
532
    reqMsg.contLen = req->msgLen;
31,153,469✔
533

534
    switch (req->msgType) {
31,157,637✔
535
      case TDMT_VND_TABLE_META:
26,424,130✔
536
        // error code has been set into reqMsg, no need to handle it here.
537
        if (TSDB_CODE_SUCCESS != vnodeGetTableMeta(pVnode, &reqMsg, false)) {
26,424,130✔
538
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
2,539,353✔
539
        }
540
        break;
26,423,503✔
541
      case TDMT_VND_TABLE_NAME:
258,622✔
542
        // error code has been set into reqMsg, no need to handle it here.
543
        if (TSDB_CODE_SUCCESS != vnodeGetTableMeta(pVnode, &reqMsg, false)) {
258,622✔
544
          qWarn("vnodeGetBatchName failed, msgType:%d", req->msgType);
94,592✔
545
        }
546
        break;
258,432✔
547
      case TDMT_VND_TABLE_CFG:
55,601✔
548
        // error code has been set into reqMsg, no need to handle it here.
549
        if (TSDB_CODE_SUCCESS != vnodeGetTableCfg(pVnode, &reqMsg, false)) {
55,601✔
UNCOV
550
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
×
551
        }
552
        break;
55,601✔
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,412,063✔
560
        // error code has been set into reqMsg, no need to handle it here.
561
        if (TSDB_CODE_SUCCESS != vnodeGetVStbRefDbs(pVnode, &reqMsg)) {
4,412,063✔
562
          qWarn("vnodeGetVStbRefDbs failed, msgType:%d", req->msgType);
×
563
        }
564
        break;
4,414,775✔
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;
31,152,311✔
574
    rsp.reqType = reqMsg.msgType;
31,151,948✔
575
    rsp.msgLen = reqMsg.contLen;
31,151,948✔
576
    rsp.rspCode = reqMsg.code;
31,151,948✔
577
    rsp.msg = reqMsg.pCont;
31,151,948✔
578

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

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

604
_exit:
26,280,034✔
605

606
  rspMsg.info = pMsg->info;
26,284,986✔
607
  rspMsg.pCont = pRsp;
26,282,923✔
608
  rspMsg.contLen = rspSize;
26,282,923✔
609
  rspMsg.code = code;
26,282,923✔
610
  rspMsg.msgType = pMsg->msgType;
26,282,923✔
611

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

616
  taosArrayDestroyEx(batchReq.pMsgs, tFreeSBatchReqMsg);
26,257,208✔
617
  taosArrayDestroyEx(batchRsp.pRsps, tFreeSBatchRspMsg);
26,260,386✔
618

619
  tmsgSendRsp(&rspMsg);
26,243,155✔
620

621
  return code;
26,286,229✔
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,425,492✔
725
  int32_t                    code = TSDB_CODE_SUCCESS;
4,425,492✔
726
  int32_t                    line = 0;
4,425,492✔
727
  SMetaReader                mr = {0};
4,425,492✔
728
  bool                       readerInit = false;
4,424,370✔
729
  SSHashObj*                 pDbNameHash = NULL;
4,424,370✔
730
  SArray*                    pList = NULL;
4,424,370✔
731

732
  pList = taosArrayInit(10, sizeof(uint64_t));
4,424,370✔
733
  QUERY_CHECK_NULL(pList, code, line, _return, terrno);
4,405,374✔
734

735
  *ppRes = taosArrayInit(10, POINTER_BYTES);
4,405,374✔
736
  QUERY_CHECK_NULL(*ppRes, code, line, _return, terrno)
4,414,134✔
737
  
738
  // lookup in cache
739
  code = pHandle->api.metaFn.metaGetCachedRefDbs(pHandle->vnode, suid, *ppRes);
4,415,855✔
740
  QUERY_CHECK_CODE(code, line, _return);
4,431,350✔
741

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

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

753
    for (int32_t i = 0; i < num; ++i) {
108,224✔
754
      uint64_t* id = taosArrayGet(pList, i);
75,348✔
755
      QUERY_CHECK_NULL(id, code, line, _return, terrno);
75,348✔
756

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

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

763
      for (int32_t j = 0; j < mr.me.colRef.nCols; j++) {
35,287,420✔
764
        if (mr.me.colRef.pColRef[j].hasRef) {
35,212,072✔
765
          if (NULL == tSimpleHashGet(pDbNameHash, mr.me.colRef.pColRef[j].refDbName, strlen(mr.me.colRef.pColRef[j].refDbName))) {
34,899,656✔
766
            char *refDbName = taosStrdup(mr.me.colRef.pColRef[j].refDbName);
28,341✔
767
            QUERY_CHECK_NULL(refDbName, code, line, _return, terrno);
28,341✔
768

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

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

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

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

785
_return:
4,431,350✔
786

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

791
  taosArrayDestroy(pList);
4,431,349✔
792
  tSimpleHashCleanup(pDbNameHash);
4,420,766✔
793

794
  if (code) {
4,420,893✔
UNCOV
795
    qError("%s failed since %s", __func__, tstrerror(code));
×
796
  }
797
  return code;
4,420,893✔
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,425,547✔
863
  int32_t        code = 0;
4,425,547✔
864
  int32_t        rspSize = 0;
4,425,547✔
865
  SVStbRefDbsReq req = {0};
4,425,547✔
866
  SVStbRefDbsRsp rsp = {0};
4,427,718✔
867
  SRpcMsg        rspMsg = {0};
4,428,197✔
868
  void          *pRsp = NULL;
4,428,197✔
869
  int32_t        line = 0;
4,428,197✔
870

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

877
  SReadHandle handle = {0};
4,421,058✔
878
  handle.vnode = pVnode;
4,421,159✔
879
  initStorageAPI(&handle.api);
4,421,159✔
880

881
  code = vnodeReadVStbRefDbs(&handle, req.suid, &rsp.pDbs);
4,402,894✔
882
  QUERY_CHECK_CODE(code, line, _return);
4,423,915✔
883
  rsp.vgId = TD_VID(pVnode);
4,423,915✔
884

885
  rspSize = tSerializeSVStbRefDbsRsp(NULL, 0, &rsp);
4,426,112✔
886
  if (rspSize < 0) {
4,404,947✔
UNCOV
887
    code = rspSize;
×
UNCOV
888
    qError("tSerializeSVStbRefDbsRsp failed, error:%d", rspSize);
×
UNCOV
889
    goto _return;
×
890
  }
891
  pRsp = taosMemoryCalloc(1, rspSize);
4,404,947✔
892
  if (pRsp == NULL) {
4,408,959✔
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,408,959✔
898
  if (rspSize < 0) {
4,421,347✔
899
    code = rspSize;
×
UNCOV
900
    qError("tSerializeSVStbRefDbsRsp failed, error:%d", rspSize);
×
UNCOV
901
    goto _return;
×
902
  }
903

904
_return:
4,421,347✔
905

906
  rspMsg.info = pMsg->info;
4,417,258✔
907
  rspMsg.pCont = pRsp;
4,421,395✔
908
  rspMsg.contLen = rspSize;
4,421,395✔
909
  rspMsg.code = code;
4,421,395✔
910
  rspMsg.msgType = pMsg->msgType;
4,421,395✔
911

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

916
  *pMsg = rspMsg;
4,409,486✔
917

918
  tDestroySVStbRefDbsRsp(&rsp);
4,410,345✔
919

920
  return code;
4,407,079✔
921
}
922

923
static int32_t vnodeGetCompStorage(SVnode *pVnode, int64_t *output) {
124,724,002✔
924
  int32_t code = 0;
124,724,002✔
925
#ifdef TD_ENTERPRISE
926
  int32_t now = taosGetTimestampSec();
124,724,002✔
927
  if (llabs(now - pVnode->config.vndStats.storageLastUpd) >= 30) {
124,724,002✔
928
    pVnode->config.vndStats.storageLastUpd = now;
6,570,881✔
929

930
    SDbSizeStatisInfo info = {0};
6,570,881✔
931
    if (0 == (code = vnodeGetDBSize(pVnode, &info))) {
6,570,881✔
932
      int64_t compSize =
6,570,881✔
933
          info.l1Size + info.l2Size + info.l3Size + info.cacheSize + info.walSize + info.metaSize + +info.ssSize;
6,570,881✔
934
      if (compSize >= 0) {
6,570,881✔
935
        pVnode->config.vndStats.compStorage = compSize;
6,570,881✔
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;
124,724,002✔
945
#endif
946
  return code;
124,724,002✔
947
}
948

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

955
    if (pVnode->inUse) {
124,724,002✔
956
      *bufferSegmentUsed = pVnode->inUse->size;
124,717,041✔
957
    }
958
    *bufferSegmentSize = pVnode->config.szBuf / VNODE_BUFPOOL_SEGMENTS;
124,724,002✔
959

960
    (void)taosThreadMutexUnlock(&pVnode->mutex);
124,724,002✔
961
  }
962
}
124,724,002✔
963

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

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

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

1024
  if (dbname) {
14,877,181✔
1025
    *dbname = pConf->dbname;
14,858,914✔
1026
  }
1027

1028
  if (vgId) {
14,877,181✔
1029
    *vgId = TD_VID(pVnodeObj);
14,116,984✔
1030
  }
1031

1032
  if (numOfTables) {
14,876,556✔
1033
    *numOfTables = pConf->vndStats.numOfNTables + pConf->vndStats.numOfCTables +
21,824✔
1034
                   pConf->vndStats.numOfVTables + pConf->vndStats.numOfVCTables;
10,912✔
1035
  }
1036

1037
  if (numOfNormalTables) {
14,876,556✔
1038
    *numOfNormalTables = pConf->vndStats.numOfNTables +
15,960✔
1039
                         pConf->vndStats.numOfVTables;
7,980✔
1040
  }
1041
}
14,876,556✔
1042

1043
int32_t vnodeGetTableList(void *pVnode, int8_t type, SArray *pList) {
7,980✔
1044
  if (type == TSDB_SUPER_TABLE) {
7,980✔
1045
    return vnodeGetStbIdList(pVnode, 0, pList);
7,980✔
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) {
99,124,733✔
1082
  int32_t      code = TSDB_CODE_SUCCESS;
99,124,733✔
1083
  SVnode      *pVnodeObj = pVnode;
99,124,733✔
1084
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnodeObj, suid, 1);
99,124,733✔
1085
  if (NULL == pCur) {
99,103,230✔
UNCOV
1086
    qError("vnode get all table list failed");
×
UNCOV
1087
    return terrno;
×
1088
  }
1089

1090
  while (1) {
435,109,101✔
1091
    tb_uid_t id = metaCtbCursorNext(pCur);
534,212,331✔
1092
    if (id == 0) {
534,127,565✔
1093
      break;
99,160,102✔
1094
    }
1095
    qTrace("vnodeGetCtbIdList: got ctb id %" PRId64 " for suid %" PRId64, id, suid);
434,967,463✔
1096
    if (NULL == taosArrayPush(list, &id)) {
435,126,257✔
UNCOV
1097
      qError("taosArrayPush failed");
×
UNCOV
1098
      code = terrno;
×
UNCOV
1099
      goto _exit;
×
1100
    }
1101
  }
1102

1103
_exit:
99,095,173✔
1104
  metaCloseCtbCursor(pCur);
99,095,173✔
1105
  return code;
99,067,148✔
1106
}
1107

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

1115
  while (1) {
47,899,732✔
1116
    tb_uid_t id = metaStbCursorNext(pCur);
108,877,445✔
1117
    if (id == 0) {
108,874,062✔
1118
      break;
60,982,168✔
1119
    }
1120

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

1128
_exit:
60,972,348✔
1129
  metaCloseStbCursor(pCur);
60,972,348✔
1130
  return code;
60,979,330✔
1131
}
1132

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

1141
  while (1) {
1,490✔
1142
    tb_uid_t id = metaStbCursorNext(pCur);
1,788✔
1143
    if (id == 0) {
1,788✔
1144
      break;
298✔
1145
    }
1146

1147
    if ((*filter) && (*filter)(arg, &id)) {
1,490✔
1148
      continue;
1,490✔
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:
298✔
1159
  metaCloseStbCursor(pCur);
298✔
1160
  return code;
298✔
1161
}
1162

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

1169
  *num = 0;
4,585,308✔
1170
  while (1) {
2,516,044✔
1171
    tb_uid_t id = metaCtbCursorNext(pCur);
7,101,665✔
1172
    if (id == 0) {
7,102,120✔
1173
      break;
4,586,874✔
1174
    }
1175

1176
    ++(*num);
2,515,246✔
1177
  }
1178

1179
  metaCloseCtbCursor(pCur);
4,586,874✔
1180
  return TSDB_CODE_SUCCESS;
4,586,659✔
1181
}
1182

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

1192
  return TSDB_CODE_SUCCESS;
4,586,062✔
1193
}
1194

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

1199
  int32_t code = metaReaderGetTableEntryByUid(&mr, suid);
4,584,724✔
1200
  if (code == TSDB_CODE_SUCCESS) {
4,587,545✔
1201
    if (keep) *keep = mr.me.stbEntry.keep;
4,587,411✔
1202
    if (flags) *flags = mr.me.flags;
4,587,411✔
1203
  } else {
1204
    if (keep) *keep = 0;
134✔
1205
    if (flags) *flags = 0;
134✔
1206
  }
1207

1208
  metaReaderClear(&mr);
4,588,261✔
1209
  return TSDB_CODE_SUCCESS;
4,588,114✔
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) {
60,969,152✔
1238
  int32_t      code = TSDB_CODE_SUCCESS;
60,969,152✔
1239
  int32_t      tbNum = 0;
60,969,152✔
1240
  const char **pTbArr = NULL;
60,969,152✔
1241
  const char  *dbName = NULL;
60,969,152✔
1242
  *tbSize = 0;
60,969,152✔
1243

1244
  if (!(dbName = strchr(pVnode->config.dbname, '.'))) return 0;
60,972,233✔
1245
  if (0 == strncmp(++dbName, "log", TSDB_DB_NAME_LEN)) {
60,968,825✔
1246
    tbNum = tkLogStbNum;
2,980✔
1247
    pTbArr = (const char **)&tkLogStb;
2,980✔
1248
  } else if (0 == strncmp(dbName, "audit", TSDB_DB_NAME_LEN)) {
60,967,101✔
1249
    tbNum = tkAuditStbNum;
2,400✔
1250
    pTbArr = (const char **)&tkAuditStb;
2,400✔
1251
  }
1252
  if (tbNum && pTbArr) {
60,970,081✔
1253
    *tbSize = metaSizeOfTbFilterCache(pVnode->pMeta, 0);
5,380✔
1254
    if (*tbSize < tbNum) {
5,380✔
1255
      for (int32_t i = 0; i < tbNum; ++i) {
50,990✔
1256
        tb_uid_t suid = metaGetTableEntryUidByName(pVnode->pMeta, pTbArr[i]);
47,100✔
1257
        if (suid != 0) {
47,100✔
1258
          code = metaPutTbToFilterCache(pVnode->pMeta, &suid, 0);
2,980✔
1259
          if (TSDB_CODE_SUCCESS != code) {
2,980✔
1260
            return code;
1,490✔
1261
          }
1262
        }
1263
      }
1264
      *tbSize = metaSizeOfTbFilterCache(pVnode->pMeta, 0);
3,890✔
1265
    }
1266
  }
1267

1268
  return code;
60,966,406✔
1269
}
1270
#endif
1271

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

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

1281
int32_t vnodeGetTimeSeriesNum(SVnode *pVnode, int64_t *num) {
60,968,524✔
1282
  SArray *suidList = NULL;
60,968,524✔
1283

1284
  if (!(suidList = taosArrayInit(1, sizeof(tb_uid_t)))) {
60,968,524✔
UNCOV
1285
    return terrno;
×
1286
  }
1287

1288
  int32_t tbFilterSize = 0;
60,966,581✔
1289
  int32_t code = TSDB_CODE_SUCCESS;
60,963,500✔
1290
#ifdef TD_ENTERPRISE
1291
  code = vnodeGetTimeSeriesBlackList(pVnode, &tbFilterSize);
60,963,500✔
1292
  if (TSDB_CODE_SUCCESS != code) {
60,967,268✔
1293
    goto _exit;
1,490✔
1294
  }
1295
#endif
1296

1297
  if ((!tbFilterSize && vnodeGetStbIdList(pVnode, 0, suidList) < 0) ||
60,965,778✔
1298
      (tbFilterSize && vnodeGetStbIdListByFilter(pVnode, 0, suidList, vnodeTimeSeriesFilter, pVnode) < 0)) {
60,958,242✔
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;
60,967,277✔
1305
  int64_t arrSize = taosArrayGetSize(suidList);
60,966,406✔
1306
  for (int64_t i = 0; i < arrSize; ++i) {
108,859,644✔
1307
    tb_uid_t suid = *(tb_uid_t *)taosArrayGet(suidList, i);
47,886,330✔
1308

1309
    int64_t ctbNum = 0;
47,883,759✔
1310
    int32_t numOfCols = 0;
47,886,271✔
1311
    int8_t  flags = 0;
47,873,770✔
1312
    code = metaGetStbStats(pVnode, suid, &ctbNum, &numOfCols, &flags);
47,881,306✔
1313
    if (TSDB_CODE_SUCCESS != code) {
47,900,087✔
UNCOV
1314
      goto _exit;
×
1315
    }
1316
    if (!TABLE_IS_VIRTUAL(flags)) {
47,900,087✔
1317
      *num += ctbNum * (numOfCols - 1);
44,957,175✔
1318
    }
1319
  }
1320

1321
_exit:
60,974,444✔
1322
  taosArrayDestroy(suidList);
60,974,804✔
1323
  return TSDB_CODE_SUCCESS;
60,966,274✔
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,970,739✔
1354
  if (pVnode == NULL) {
3,970,739✔
UNCOV
1355
    return NULL;
×
1356
  }
1357

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

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

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

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

1376
  char   *dirName[] = {VNODE_TSDB_DIR, VNODE_WAL_DIR, VNODE_META_DIR, VNODE_TSDB_CACHE_DIR};
6,576,779✔
1377
  int64_t dirSize[4];
6,576,305✔
1378

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

1382
  for (int i = 0; i < sizeof(dirName) / sizeof(dirName[0]); i++) {
32,883,895✔
1383
    int64_t size = {0};
26,307,116✔
1384
    (void)snprintf(path + offset, TSDB_FILENAME_LEN - offset, "%s%s", TD_DIRSEP, dirName[i]);
26,307,116✔
1385
    code = taosGetDirSize(path, &size);
26,307,116✔
1386
    if (code != 0) {
26,307,116✔
1387
      uWarn("vnode %d get dir %s %s size failed since %s", TD_VID(pVnode), path, dirName[i], tstrerror(code));
2,332✔
1388
    }
1389
    path[offset] = 0;
26,307,116✔
1390
    dirSize[i] = size;
26,307,116✔
1391
  }
1392

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

1409
  code = tsdbGetFsSize(pVnodeObj->pTsdb, pInfo);
6,576,779✔
1410
_exit:
6,576,779✔
1411
  return code;
6,576,779✔
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