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

taosdata / TDengine / #4991

17 Mar 2026 07:57AM UTC coverage: 69.756% (+0.4%) from 69.348%
#4991

push

travis-ci

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

14 of 16 new or added lines in 5 files covered. (87.5%)

3928 existing lines in 138 files now uncovered.

192146 of 275455 relevant lines covered (69.76%)

137208686.18 hits per line

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

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

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

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

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

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

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

36
int32_t fillTableColCmpr(SMetaReader *reader, SSchemaExt *pExt, int32_t numOfCol) {
19,794,522✔
37
  int8_t tblType = reader->me.type;
19,794,522✔
38
  if (withExtSchema(tblType)) {
19,794,905✔
39
    SColCmprWrapper *p = &(reader->me.colCmpr);
19,468,909✔
40
    if (numOfCol != p->nCols) {
19,469,554✔
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++) {
234,131,420✔
45
      SColCmpr *pCmpr = &p->pColCmpr[i];
214,664,930✔
46
      pExt[i].colId = pCmpr->id;
214,662,962✔
47
      pExt[i].compress = pCmpr->alg;
214,658,660✔
48
    }
49
  }
50
  return 0;
19,795,288✔
51
}
52

53
void vnodePrintTableMeta(STableMetaRsp *pMeta) {
19,792,587✔
54
  if (!(qDebugFlag & DEBUG_DEBUG)) {
19,792,587✔
55
    return;
400,671✔
56
  }
57

58
  qDebug("tbName:%s", pMeta->tbName);
19,391,916✔
59
  qDebug("stbName:%s", pMeta->stbName);
19,396,935✔
60
  qDebug("dbFName:%s", pMeta->dbFName);
19,396,935✔
61
  qDebug("dbId:%" PRId64, pMeta->dbId);
19,396,935✔
62
  qDebug("numOfTags:%d", pMeta->numOfTags);
19,396,935✔
63
  qDebug("numOfColumns:%d", pMeta->numOfColumns);
19,396,935✔
64
  qDebug("precision:%d", pMeta->precision);
19,396,935✔
65
  qDebug("tableType:%d", pMeta->tableType);
19,396,935✔
66
  qDebug("sversion:%d", pMeta->sversion);
19,396,935✔
67
  qDebug("tversion:%d", pMeta->tversion);
19,396,935✔
68
  qDebug("suid:%" PRIu64, pMeta->suid);
19,396,935✔
69
  qDebug("tuid:%" PRIu64, pMeta->tuid);
19,396,935✔
70
  qDebug("vgId:%d", pMeta->vgId);
19,396,935✔
71
  qDebug("sysInfo:%d", pMeta->sysInfo);
19,396,935✔
72
  if (pMeta->pSchemas) {
19,396,935✔
73
    for (int32_t i = 0; i < (pMeta->numOfColumns + pMeta->numOfTags); ++i) {
270,620,181✔
74
      SSchema *pSchema = pMeta->pSchemas + i;
251,223,246✔
75
      qDebug("%d col/tag: type:%d, flags:%d, colId:%d, bytes:%d, name:%s", i, pSchema->type, pSchema->flags,
251,223,246✔
76
             pSchema->colId, pSchema->bytes, pSchema->name);
77
    }
78
  }
79
}
80

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

103
int32_t vnodeGetTableMeta(SVnode *pVnode, SRpcMsg *pMsg, bool direct) {
28,872,681✔
104
  STableInfoReq  infoReq = {0};
28,872,681✔
105
  STableMetaRsp  metaRsp = {0};
28,872,681✔
106
  SMetaReader    mer1 = {0};
28,873,325✔
107
  SMetaReader    mer2 = {0};
28,873,172✔
108
  char           tableFName[TSDB_TABLE_FNAME_LEN];
28,870,734✔
109
  bool           reqTbUid = false;
28,873,326✔
110
  SRpcMsg        rpcMsg = {0};
28,873,326✔
111
  int32_t        code = 0;
28,873,326✔
112
  int32_t        rspLen = 0;
28,873,326✔
113
  void          *pRsp = NULL;
28,873,326✔
114
  SSchemaWrapper schema = {0};
28,873,326✔
115
  SSchemaWrapper schemaTag = {0};
28,873,326✔
116
  uint8_t        autoCreateCtb = 0;
28,873,326✔
117

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

125
  if (infoReq.option == REQ_OPT_TBUID) reqTbUid = true;
28,874,992✔
126
  metaRsp.dbId = pVnode->config.dbId;
28,874,992✔
127
  tstrncpy(metaRsp.tbName, infoReq.tbName, TSDB_TABLE_NAME_LEN);
28,873,702✔
128
  (void)memcpy(metaRsp.dbFName, infoReq.dbFName, sizeof(metaRsp.dbFName));
28,873,702✔
129

130
  if (!reqTbUid) {
28,873,702✔
131
    (void)snprintf(tableFName, TSDB_TABLE_FNAME_LEN, "%s.%s", infoReq.dbFName, infoReq.tbName);
28,635,918✔
132
    if (pVnode->mounted) tTrimMountPrefix(tableFName);
28,635,918✔
133
    code = vnodeValidateTableHash(pVnode, tableFName);
28,638,395✔
134
    if (code) {
28,637,865✔
135
      goto _exit4;
×
136
    }
137
  }
138

139
  // query meta
140
  metaReaderDoInit(&mer1, pVnode->pMeta, META_READER_LOCK);
28,875,649✔
141
  if (reqTbUid) {
28,873,057✔
142
    SET_ERRNO(0);
235,952✔
143
    uint64_t tbUid = taosStr2UInt64(infoReq.tbName, NULL, 10);
235,952✔
144
    if (ERRNO == ERANGE || tbUid == 0) {
235,952✔
145
      code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
146
      goto _exit3;
×
147
    }
148
    SMetaReader mr3 = {0};
235,952✔
149
    metaReaderDoInit(&mr3, ((SVnode *)pVnode)->pMeta, META_READER_NOLOCK);
235,952✔
150
    if ((code = metaReaderGetTableEntryByUid(&mr3, tbUid)) < 0) {
235,952✔
151
      metaReaderClear(&mr3);
84,728✔
152
      TAOS_CHECK_GOTO(code, NULL, _exit3);
84,728✔
153
    }
154
    tstrncpy(metaRsp.tbName, mr3.me.name, TSDB_TABLE_NAME_LEN);
151,224✔
155
    metaReaderClear(&mr3);
151,224✔
156
    TAOS_CHECK_GOTO(metaGetTableEntryByName(&mer1, metaRsp.tbName), NULL, _exit3);
151,224✔
157
  } else if (metaGetTableEntryByName(&mer1, infoReq.tbName) < 0) {
28,637,105✔
158
    code = terrno;
8,992,282✔
159
    goto _exit3;
8,991,906✔
160
  }
161

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

167
  switch (mer1.me.type) {
19,792,704✔
168
    case TSDB_SUPER_TABLE: {
3,734,726✔
169
      tstrncpy(metaRsp.stbName, mer1.me.name, sizeof(metaRsp.stbName));
3,734,726✔
170
      schema = mer1.me.stbEntry.schemaRow;
3,734,726✔
171
      schemaTag = mer1.me.stbEntry.schemaTag;
3,734,726✔
172
      metaRsp.suid = mer1.me.uid;
3,734,726✔
173
      metaRsp.virtualStb = TABLE_IS_VIRTUAL(mer1.me.flags);
3,734,726✔
174
      metaRsp.ownerId = mer1.me.stbEntry.ownerId;
3,734,726✔
175
      break;
3,734,726✔
176
    }
177
    case TSDB_CHILD_TABLE:
12,625,895✔
178
    case TSDB_VIRTUAL_CHILD_TABLE:{
179
      metaReaderDoInit(&mer2, pVnode->pMeta, META_READER_NOLOCK);
12,625,895✔
180
      if (metaReaderGetTableEntryByUid(&mer2, mer1.me.ctbEntry.suid) < 0) goto _exit2;
12,626,519✔
181

182
      tstrncpy(metaRsp.stbName, mer2.me.name, sizeof(metaRsp.stbName));
12,627,834✔
183
      metaRsp.suid = mer2.me.uid;
12,626,676✔
184
      metaRsp.ownerId = mer2.me.stbEntry.ownerId;  // child table inherits ownerId from stb
12,626,676✔
185
      schema = mer2.me.stbEntry.schemaRow;
12,626,676✔
186
      schemaTag = mer2.me.stbEntry.schemaTag;
12,626,676✔
187
      break;
12,626,676✔
188
    }
189
    case TSDB_NORMAL_TABLE:
3,432,083✔
190
    case TSDB_VIRTUAL_NORMAL_TABLE: {
191
      schema = mer1.me.ntbEntry.schemaRow;
3,432,083✔
192
      metaRsp.ownerId = mer1.me.ntbEntry.ownerId;
3,432,083✔
193
      break;
3,432,083✔
194
    }
195
    default: {
×
196
      vError("vnodeGetTableMeta get invalid table type:%d", mer1.me.type);
×
197
      goto _exit3;
×
198
    }
199
  }
200

201
  metaRsp.numOfTags = schemaTag.nCols;
19,793,485✔
202
  metaRsp.numOfColumns = schema.nCols;
19,793,485✔
203
  metaRsp.precision = pVnode->config.tsdbCfg.precision;
19,793,485✔
204
  metaRsp.sversion = schema.version;
19,794,627✔
205
  metaRsp.tversion = schemaTag.version;
19,794,627✔
206
  metaRsp.pSchemas = (SSchema *)taosMemoryMalloc(sizeof(SSchema) * (metaRsp.numOfColumns + metaRsp.numOfTags));
19,794,627✔
207
  metaRsp.pSchemaExt = (SSchemaExt *)taosMemoryCalloc(metaRsp.numOfColumns, sizeof(SSchemaExt));
19,793,452✔
208
  if (NULL == metaRsp.pSchemas || NULL == metaRsp.pSchemaExt) {
19,794,480✔
209
    code = terrno;
1,526✔
210
    goto _exit;
×
211
  }
212
  (void)memcpy(metaRsp.pSchemas, schema.pSchema, sizeof(SSchema) * schema.nCols);
19,792,954✔
213
  if (schemaTag.nCols) {
19,792,954✔
214
    (void)memcpy(metaRsp.pSchemas + schema.nCols, schemaTag.pSchema, sizeof(SSchema) * schemaTag.nCols);
16,361,254✔
215
  }
216
  if (metaRsp.pSchemaExt) {
19,793,494✔
217
    SMetaReader *pReader = mer1.me.type == TSDB_CHILD_TABLE ? &mer2 : &mer1;
19,793,311✔
218
    code = fillTableColCmpr(pReader, metaRsp.pSchemaExt, metaRsp.numOfColumns);
19,793,311✔
219
    if (code < 0) {
19,794,643✔
220
      goto _exit;
×
221
    }
222
    for (int32_t i = 0; i < metaRsp.numOfColumns && pReader->me.pExtSchemas; i++) {
48,070,385✔
223
      metaRsp.pSchemaExt[i].typeMod = pReader->me.pExtSchemas[i].typeMod;
28,275,742✔
224
    }
225
  } else {
226
    code = TSDB_CODE_OUT_OF_MEMORY;
566✔
227
    goto _exit;
566✔
228
  }
229
  if (hasRefCol(mer1.me.type)) {
19,798,912✔
230
    metaRsp.rversion = mer1.me.colRef.version;
331,822✔
231
    metaRsp.pColRefs = (SColRef*)taosMemoryMalloc(sizeof(SColRef) * metaRsp.numOfColumns);
331,822✔
232
    if (metaRsp.pColRefs) {
325,351✔
233
      code = fillTableColRef(&mer1, metaRsp.pColRefs, metaRsp.numOfColumns);
325,351✔
234
      if (code < 0) {
325,351✔
235
        goto _exit;
×
236
      }
237
    }
238
    metaRsp.numOfColRefs = metaRsp.numOfColumns;
325,351✔
239
  } else {
240
    metaRsp.pColRefs = NULL;
19,464,493✔
241
    metaRsp.numOfColRefs = 0;
19,464,493✔
242
  }
243

244
  vnodePrintTableMeta(&metaRsp);
19,789,844✔
245

246
  // encode and send response
247
  rspLen = tSerializeSTableMetaRsp(NULL, 0, &metaRsp);
19,797,076✔
248
  if (rspLen < 0) {
19,794,521✔
249
    code = terrno;
×
250
    goto _exit;
×
251
  }
252

253
  if (direct) {
19,794,521✔
254
    pRsp = rpcMallocCont(rspLen);
190,188✔
255
  } else {
256
    pRsp = taosMemoryCalloc(1, rspLen);
19,604,333✔
257
  }
258

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

264
  rspLen = tSerializeSTableMetaRsp(pRsp, rspLen, &metaRsp);
19,795,528✔
265
  if (rspLen < 0) {
19,793,085✔
266
    code = terrno;
×
267
    goto _exit;
×
268
  }
269

270
_exit:
19,793,268✔
271
  taosMemoryFree(metaRsp.pColRefs);
19,796,641✔
272
  taosMemoryFree(metaRsp.pSchemas);
19,793,805✔
273
  taosMemoryFree(metaRsp.pSchemaExt);
19,793,678✔
274
_exit2:
19,795,082✔
275
  metaReaderClear(&mer2);
19,795,665✔
276
_exit3:
28,869,855✔
277
  metaReaderClear(&mer1);
28,873,581✔
278
_exit4:
28,872,985✔
279
  rpcMsg.info = pMsg->info;
28,873,581✔
280
  rpcMsg.pCont = pRsp;
28,873,964✔
281
  rpcMsg.contLen = rspLen;
28,873,964✔
282
  rpcMsg.code = code;
28,873,964✔
283
  rpcMsg.msgType = pMsg->msgType;
28,873,964✔
284

285
  if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST && autoCreateCtb == 1) {
28,870,271✔
286
    code = TSDB_CODE_SUCCESS;
5,704,488✔
287
  }
288

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

294
  if (direct) {
28,870,616✔
295
    tmsgSendRsp(&rpcMsg);
918,162✔
296
  } else {
297
    *pMsg = rpcMsg;
27,952,454✔
298
  }
299

300
  return code;
28,869,710✔
301
}
302

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

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

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

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

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

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

340
  cfgRsp.tableType = mer1.me.type;
60,973✔
341
  cfgRsp.isAudit = pVnode->config.isAudit ? 1 : 0;
60,973✔
342
  cfgRsp.secureDelete = pVnode->config.secureDelete;
60,973✔
343

344
  if (mer1.me.type == TSDB_SUPER_TABLE) {
60,973✔
345
    code = TSDB_CODE_VND_HASH_MISMATCH;
×
346
    goto _exit;
×
347
  } else if (mer1.me.type == TSDB_CHILD_TABLE || mer1.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
60,973✔
348
    metaReaderDoInit(&mer2, pVnode->pMeta, META_READER_NOLOCK);
43,260✔
349
    if (metaReaderGetTableEntryByUid(&mer2, mer1.me.ctbEntry.suid) < 0) goto _exit;
43,260✔
350

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

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

397
  if (NULL == cfgRsp.pSchemas || NULL == cfgRsp.pSchemaExt || NULL == cfgRsp.pColRefs) {
60,973✔
398
    code = terrno;
×
399
    goto _exit;
×
400
  }
401
  (void)memcpy(cfgRsp.pSchemas, schema.pSchema, sizeof(SSchema) * schema.nCols);
60,973✔
402
  if (schemaTag.nCols) {
60,973✔
403
    (void)memcpy(cfgRsp.pSchemas + schema.nCols, schemaTag.pSchema, sizeof(SSchema) * schemaTag.nCols);
43,260✔
404
  }
405

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

410
  if (withExtSchema(cfgRsp.tableType)) {
60,973✔
411
    for (int32_t i = 0; i < cfgRsp.numOfColumns; i++) {
222,761✔
412
      SColCmpr   *pCmpr = &pColCmpr->pColCmpr[i];
169,513✔
413
      SSchemaExt *pSchExt = cfgRsp.pSchemaExt + i;
169,513✔
414
      pSchExt->colId = pCmpr->id;
169,513✔
415
      pSchExt->compress = pCmpr->alg;
169,513✔
416
      if (pReader->me.pExtSchemas)
169,513✔
417
        pSchExt->typeMod = pReader->me.pExtSchemas[i].typeMod;
25,212✔
418
      else
419
        pSchExt->typeMod = 0;
144,301✔
420
    }
421
  }
422

423
  cfgRsp.virtualStb = false;
60,973✔
424
  if (hasRefCol(cfgRsp.tableType)) {
60,973✔
425
    for (int32_t i = 0; i < cfgRsp.numOfColumns; i++) {
108,767✔
426
      SColRef *pRef = &pColRef->pColRef[i];
101,042✔
427
      cfgRsp.pColRefs[i].hasRef = pRef->hasRef;
101,042✔
428
      cfgRsp.pColRefs[i].id = pRef->id;
101,042✔
429
      if (cfgRsp.pColRefs[i].hasRef) {
101,042✔
430
        tstrncpy(cfgRsp.pColRefs[i].refDbName, pRef->refDbName, TSDB_DB_NAME_LEN);
52,989✔
431
        tstrncpy(cfgRsp.pColRefs[i].refTableName, pRef->refTableName, TSDB_TABLE_NAME_LEN);
52,989✔
432
        tstrncpy(cfgRsp.pColRefs[i].refColName, pRef->refColName, TSDB_COL_NAME_LEN);
52,989✔
433
      }
434
    }
435
  }
436

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

444
  if (direct) {
60,973✔
445
    pRsp = rpcMallocCont(rspLen);
×
446
  } else {
447
    pRsp = taosMemoryCalloc(1, rspLen);
60,973✔
448
  }
449

450
  if (pRsp == NULL) {
60,973✔
451
    code = terrno;
×
452
    goto _exit;
×
453
  }
454

455
  rspLen = tSerializeSTableCfgRsp(pRsp, rspLen, &cfgRsp);
60,973✔
456
  if (rspLen < 0) {
60,973✔
457
    code = terrno;
×
458
    goto _exit;
×
459
  }
460

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

468
  if (code) {
60,973✔
469
    qError("get table %s cfg failed cause of %s", cfgReq.tbName, tstrerror(code));
×
470
  }
471

472
  if (direct) {
60,973✔
473
    tmsgSendRsp(&rpcMsg);
×
474
  } else {
475
    *pMsg = rpcMsg;
60,973✔
476
  }
477

478
  tFreeSTableCfgRsp(&cfgRsp);
60,973✔
479
  metaReaderClear(&mer2);
60,973✔
480
  metaReaderClear(&mer1);
60,973✔
481
  return code;
60,973✔
482
}
483

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

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

493
int32_t vnodeGetBatchMeta(SVnode *pVnode, SRpcMsg *pMsg) {
28,495,204✔
494
  int32_t      code = 0;
28,495,204✔
495
  int32_t      rspSize = 0;
28,495,204✔
496
  SBatchReq    batchReq = {0};
28,495,204✔
497
  SBatchMsg   *req = NULL;
28,495,794✔
498
  SBatchRspMsg rsp = {0};
28,495,794✔
499
  SBatchRsp    batchRsp = {0};
28,474,976✔
500
  SRpcMsg      reqMsg = *pMsg;
28,474,518✔
501
  SRpcMsg      rspMsg = {0};
28,474,518✔
502
  void        *pRsp = NULL;
28,492,308✔
503

504
  if (tDeserializeSBatchReq(pMsg->pCont, pMsg->contLen, &batchReq)) {
28,492,308✔
505
    code = terrno;
×
506
    qError("tDeserializeSBatchReq failed");
×
507
    goto _exit;
×
508
  }
509

510
  int32_t msgNum = taosArrayGetSize(batchReq.pMsgs);
28,479,002✔
511
  if (msgNum >= MAX_META_MSG_IN_BATCH) {
28,482,478✔
512
    code = TSDB_CODE_INVALID_MSG;
×
513
    qError("too many msgs %d in vnode batch meta req", msgNum);
×
514
    goto _exit;
×
515
  }
516

517
  batchRsp.pRsps = taosArrayInit(msgNum, sizeof(SBatchRspMsg));
28,482,478✔
518
  if (NULL == batchRsp.pRsps) {
28,479,159✔
519
    code = terrno;
×
520
    qError("taosArrayInit %d SBatchRspMsg failed", msgNum);
×
521
    goto _exit;
×
522
  }
523

524
  for (int32_t i = 0; i < msgNum; ++i) {
62,068,892✔
525
    req = taosArrayGet(batchReq.pMsgs, i);
33,597,790✔
526
    if (req == NULL) {
33,591,358✔
527
      code = terrno;
×
528
      goto _exit;
×
529
    }
530

531
    reqMsg.msgType = req->msgType;
33,591,358✔
532
    reqMsg.pCont = req->msg;
33,590,381✔
533
    reqMsg.contLen = req->msgLen;
33,584,035✔
534

535
    switch (req->msgType) {
33,587,004✔
536
      case TDMT_VND_TABLE_META:
27,718,298✔
537
        // error code has been set into reqMsg, no need to handle it here.
538
        if (TSDB_CODE_SUCCESS != vnodeGetTableMeta(pVnode, &reqMsg, false)) {
27,718,298✔
539
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
2,560,196✔
540
        }
541
        break;
27,719,084✔
542
      case TDMT_VND_TABLE_NAME:
235,952✔
543
        // error code has been set into reqMsg, no need to handle it here.
544
        if (TSDB_CODE_SUCCESS != vnodeGetTableMeta(pVnode, &reqMsg, false)) {
235,952✔
545
          qWarn("vnodeGetBatchName failed, msgType:%d", req->msgType);
84,728✔
546
        }
547
        break;
235,952✔
548
      case TDMT_VND_TABLE_CFG:
60,973✔
549
        // error code has been set into reqMsg, no need to handle it here.
550
        if (TSDB_CODE_SUCCESS != vnodeGetTableCfg(pVnode, &reqMsg, false)) {
60,973✔
551
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
×
552
        }
553
        break;
60,973✔
554
      case TDMT_VND_VSUBTABLES_META:
×
555
        // error code has been set into reqMsg, no need to handle it here.
556
        if (TSDB_CODE_SUCCESS != vnodeGetVSubtablesMeta(pVnode, &reqMsg)) {
×
557
          qWarn("vnodeGetVSubtablesMeta failed, msgType:%d", req->msgType);
×
558
        }
559
        break;
×
560
      case TDMT_VND_VSTB_REF_DBS:
5,564,034✔
561
        // error code has been set into reqMsg, no need to handle it here.
562
        if (TSDB_CODE_SUCCESS != vnodeGetVStbRefDbs(pVnode, &reqMsg)) {
5,564,034✔
563
          qWarn("vnodeGetVStbRefDbs failed, msgType:%d", req->msgType);
×
564
        }
565
        break;
5,571,442✔
566
      default:
×
567
        qError("invalid req msgType %d", req->msgType);
×
568
        reqMsg.code = TSDB_CODE_INVALID_MSG;
×
569
        reqMsg.pCont = NULL;
×
570
        reqMsg.contLen = 0;
×
571
        break;
×
572
    }
573

574
    rsp.msgIdx = req->msgIdx;
33,587,451✔
575
    rsp.reqType = reqMsg.msgType;
33,592,466✔
576
    rsp.msgLen = reqMsg.contLen;
33,592,466✔
577
    rsp.rspCode = reqMsg.code;
33,592,466✔
578
    rsp.msg = reqMsg.pCont;
33,592,466✔
579

580
    if (NULL == taosArrayPush(batchRsp.pRsps, &rsp)) {
67,171,318✔
581
      qError("taosArrayPush failed");
×
582
      code = terrno;
×
583
      goto _exit;
×
584
    }
585
  }
586

587
  rspSize = tSerializeSBatchRsp(NULL, 0, &batchRsp);
28,471,102✔
588
  if (rspSize < 0) {
28,426,740✔
589
    qError("tSerializeSBatchRsp failed");
×
590
    code = terrno;
×
591
    goto _exit;
×
592
  }
593
  pRsp = rpcMallocCont(rspSize);
28,426,740✔
594
  if (pRsp == NULL) {
28,457,842✔
595
    qError("rpcMallocCont %d failed", rspSize);
×
596
    code = terrno;
×
597
    goto _exit;
×
598
  }
599
  if (tSerializeSBatchRsp(pRsp, rspSize, &batchRsp) < 0) {
28,457,842✔
600
    qError("tSerializeSBatchRsp %d failed", rspSize);
×
601
    code = terrno;
×
602
    goto _exit;
×
603
  }
604

605
_exit:
28,476,789✔
606

607
  rspMsg.info = pMsg->info;
28,481,539✔
608
  rspMsg.pCont = pRsp;
28,481,044✔
609
  rspMsg.contLen = rspSize;
28,481,044✔
610
  rspMsg.code = code;
28,481,044✔
611
  rspMsg.msgType = pMsg->msgType;
28,481,044✔
612

613
  if (code) {
28,434,138✔
614
    qError("vnd get batch meta failed cause of %s", tstrerror(code));
×
615
  }
616

617
  taosArrayDestroyEx(batchReq.pMsgs, tFreeSBatchReqMsg);
28,434,138✔
618
  taosArrayDestroyEx(batchRsp.pRsps, tFreeSBatchRspMsg);
28,437,261✔
619

620
  tmsgSendRsp(&rspMsg);
28,434,236✔
621

622
  return code;
28,491,686✔
623
}
624

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

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

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

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

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

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

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

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

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

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

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

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

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

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

698
      refColsNum++;
×
699
    }
700

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

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

709
_return:
×
710

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

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

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

725
int32_t vnodeReadVStbRefDbs(SReadHandle* pHandle, int64_t suid, SArray** ppRes) {
5,556,015✔
726
  int32_t                    code = TSDB_CODE_SUCCESS;
5,556,015✔
727
  int32_t                    line = 0;
5,556,015✔
728
  SMetaReader                mr = {0};
5,556,015✔
729
  bool                       readerInit = false;
5,553,105✔
730
  SSHashObj*                 pDbNameHash = NULL;
5,553,105✔
731
  SArray*                    pList = NULL;
5,553,105✔
732

733
  pList = taosArrayInit(10, sizeof(uint64_t));
5,553,105✔
734
  QUERY_CHECK_NULL(pList, code, line, _return, terrno);
5,546,504✔
735

736
  *ppRes = taosArrayInit(10, POINTER_BYTES);
5,546,504✔
737
  QUERY_CHECK_NULL(*ppRes, code, line, _return, terrno)
5,557,299✔
738
  
739
  // lookup in cache
740
  code = pHandle->api.metaFn.metaGetCachedRefDbs(pHandle->vnode, suid, *ppRes);
5,557,828✔
741
  QUERY_CHECK_CODE(code, line, _return);
5,585,882✔
742

743
  if (taosArrayGetSize(*ppRes) > 0) {
5,585,882✔
744
    // found in cache
745
    goto _return;
5,542,493✔
746
  } else {
747
    code = pHandle->api.metaFn.getChildTableList(pHandle->vnode, suid, pList);
43,389✔
748
    QUERY_CHECK_CODE(code, line, _return);
43,389✔
749

750
    size_t num = taosArrayGetSize(pList);
43,389✔
751
    pDbNameHash = tSimpleHashInit(10, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
43,389✔
752
    QUERY_CHECK_NULL(pDbNameHash, code, line, _return, terrno);
43,389✔
753

754
    for (int32_t i = 0; i < num; ++i) {
138,892✔
755
      uint64_t* id = taosArrayGet(pList, i);
95,503✔
756
      QUERY_CHECK_NULL(id, code, line, _return, terrno);
95,503✔
757

758
      pHandle->api.metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pHandle->api.metaFn);
95,503✔
759
      readerInit = true;
95,503✔
760

761
      code = pHandle->api.metaReaderFn.getTableEntryByUid(&mr, *id);
95,503✔
762
      QUERY_CHECK_CODE(code, line, _return);
95,503✔
763

764
      for (int32_t j = 0; j < mr.me.colRef.nCols; j++) {
38,190,389✔
765
        if (mr.me.colRef.pColRef[j].hasRef) {
38,094,886✔
766
          if (NULL == tSimpleHashGet(pDbNameHash, mr.me.colRef.pColRef[j].refDbName, strlen(mr.me.colRef.pColRef[j].refDbName))) {
37,658,334✔
767
            char *refDbName = taosStrdup(mr.me.colRef.pColRef[j].refDbName);
38,038✔
768
            QUERY_CHECK_NULL(refDbName, code, line, _return, terrno);
38,038✔
769

770
            QUERY_CHECK_NULL(taosArrayPush(*ppRes, &refDbName), code, line, _return, terrno);
76,076✔
771

772
            code = tSimpleHashPut(pDbNameHash, refDbName, strlen(refDbName), NULL, 0);
38,038✔
773
            QUERY_CHECK_CODE(code, line, _return);
38,038✔
774
          }
775
        }
776
      }
777

778
      pHandle->api.metaReaderFn.clearReader(&mr);
95,503✔
779
      readerInit = false;
95,503✔
780
    }
781

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

786
_return:
5,587,110✔
787

788
  if (readerInit) {
5,584,759✔
789
    pHandle->api.metaReaderFn.clearReader(&mr);
×
790
  }
791

792
  taosArrayDestroy(pList);
5,584,759✔
793
  tSimpleHashCleanup(pDbNameHash);
5,575,096✔
794

795
  if (code) {
5,571,501✔
796
    qError("%s failed since %s", __func__, tstrerror(code));
×
797
  }
798
  return code;
5,571,501✔
799
}
800

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

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

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

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

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

842
_return:
×
843

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

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

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

858
  //tmsgSendRsp(&rspMsg);
859

860
  return code;
×
861
}
862

863
int32_t vnodeGetVStbRefDbs(SVnode *pVnode, SRpcMsg *pMsg) {
5,579,529✔
864
  int32_t        code = 0;
5,579,529✔
865
  int32_t        rspSize = 0;
5,579,529✔
866
  SVStbRefDbsReq req = {0};
5,579,529✔
867
  SVStbRefDbsRsp rsp = {0};
5,580,062✔
868
  SRpcMsg        rspMsg = {0};
5,557,879✔
869
  void          *pRsp = NULL;
5,557,879✔
870
  int32_t        line = 0;
5,557,879✔
871

872
  if (tDeserializeSVStbRefDbsReq(pMsg->pCont, pMsg->contLen, &req)) {
5,557,879✔
873
    code = terrno;
×
874
    qError("tDeserializeSVSubTablesReq failed");
×
875
    goto _return;
×
876
  }
877

878
  SReadHandle handle = {0};
5,564,063✔
879
  handle.vnode = pVnode;
5,566,964✔
880
  initStorageAPI(&handle.api);
5,566,964✔
881

882
  code = vnodeReadVStbRefDbs(&handle, req.suid, &rsp.pDbs);
5,551,813✔
883
  QUERY_CHECK_CODE(code, line, _return);
5,578,064✔
884
  rsp.vgId = TD_VID(pVnode);
5,578,064✔
885

886
  rspSize = tSerializeSVStbRefDbsRsp(NULL, 0, &rsp);
5,579,301✔
887
  if (rspSize < 0) {
5,556,321✔
888
    code = rspSize;
×
889
    qError("tSerializeSVStbRefDbsRsp failed, error:%d", rspSize);
×
890
    goto _return;
×
891
  }
892
  pRsp = taosMemoryCalloc(1, rspSize);
5,556,321✔
893
  if (pRsp == NULL) {
5,545,960✔
894
    code = terrno;
×
895
    qError("rpcMallocCont %d failed, error:%d", rspSize, terrno);
×
896
    goto _return;
×
897
  }
898
  rspSize = tSerializeSVStbRefDbsRsp(pRsp, rspSize, &rsp);
5,545,960✔
899
  if (rspSize < 0) {
5,559,087✔
900
    code = rspSize;
×
901
    qError("tSerializeSVStbRefDbsRsp failed, error:%d", rspSize);
×
902
    goto _return;
×
903
  }
904

905
_return:
5,559,087✔
906

907
  rspMsg.info = pMsg->info;
5,559,347✔
908
  rspMsg.pCont = pRsp;
5,562,332✔
909
  rspMsg.contLen = rspSize;
5,562,332✔
910
  rspMsg.code = code;
5,562,332✔
911
  rspMsg.msgType = pMsg->msgType;
5,562,332✔
912

913
  if (code) {
5,549,588✔
914
    qError("vnd get virtual stb ref db failed cause of %s", tstrerror(code));
×
915
  }
916

917
  *pMsg = rspMsg;
5,549,588✔
918

919
  tDestroySVStbRefDbsRsp(&rsp);
5,542,815✔
920

921
  return code;
5,559,880✔
922
}
923

924
static int32_t vnodeGetCompStorage(SVnode *pVnode, int64_t *output) {
115,344,410✔
925
  int32_t code = 0;
115,344,410✔
926
#ifdef TD_ENTERPRISE
927
  int32_t now = taosGetTimestampSec();
115,344,410✔
928
  if (llabs(now - pVnode->config.vndStats.storageLastUpd) >= 30) {
115,344,410✔
929
    pVnode->config.vndStats.storageLastUpd = now;
6,356,116✔
930

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

950
static void vnodeGetBufferInfo(SVnode *pVnode, int64_t *bufferSegmentUsed, int64_t *bufferSegmentSize) {
115,344,410✔
951
  *bufferSegmentUsed = 0;
115,344,410✔
952
  *bufferSegmentSize = 0;
115,344,410✔
953
  if (pVnode) {
115,344,410✔
954
    (void)taosThreadMutexLock(&pVnode->mutex);
115,344,410✔
955

956
    if (pVnode->inUse) {
115,344,410✔
957
      *bufferSegmentUsed = pVnode->inUse->size;
115,338,627✔
958
    }
959
    *bufferSegmentSize = pVnode->config.szBuf / VNODE_BUFPOOL_SEGMENTS;
115,344,410✔
960

961
    (void)taosThreadMutexUnlock(&pVnode->mutex);
115,344,410✔
962
  }
963
}
115,344,410✔
964

965
int32_t vnodeGetLoad(SVnode *pVnode, SVnodeLoad *pLoad) {
115,344,410✔
966
  SSyncState state = syncGetState(pVnode->sync);
115,344,410✔
967
  pLoad->syncAppliedIndex = pVnode->state.applied;
115,344,410✔
968
  syncGetCommitIndex(pVnode->sync, &pLoad->syncCommitIndex);
115,344,410✔
969

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

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

1021
void vnodeGetInfo(void *pVnode, const char **dbname, int32_t *vgId, int64_t *numOfTables, int64_t *numOfNormalTables) {
17,799,638✔
1022
  SVnode    *pVnodeObj = pVnode;
17,799,638✔
1023
  SVnodeCfg *pConf = &pVnodeObj->config;
17,799,638✔
1024

1025
  if (dbname) {
17,803,795✔
1026
    *dbname = pConf->dbname;
17,782,578✔
1027
  }
1028

1029
  if (vgId) {
17,806,188✔
1030
    *vgId = TD_VID(pVnodeObj);
16,896,463✔
1031
  }
1032

1033
  if (numOfTables) {
17,806,140✔
1034
    *numOfTables = pConf->vndStats.numOfNTables + pConf->vndStats.numOfCTables +
19,843✔
1035
                   pConf->vndStats.numOfVTables + pConf->vndStats.numOfVCTables;
9,930✔
1036
  }
1037

1038
  if (numOfNormalTables) {
17,806,079✔
1039
    *numOfNormalTables = pConf->vndStats.numOfNTables +
16,942✔
1040
                         pConf->vndStats.numOfVTables;
8,557✔
1041
  }
1042
}
17,806,251✔
1043

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

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

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

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

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

1082
int32_t vnodeGetCtbIdList(void *pVnode, int64_t suid, SArray *list) {
104,237,202✔
1083
  int32_t      code = TSDB_CODE_SUCCESS;
104,237,202✔
1084
  SVnode      *pVnodeObj = pVnode;
104,237,202✔
1085
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnodeObj, suid, 1);
104,237,202✔
1086
  if (NULL == pCur) {
104,165,164✔
1087
    qError("vnode get all table list failed");
×
1088
    return terrno;
×
1089
  }
1090

1091
  while (1) {
465,881,973✔
1092
    tb_uid_t id = metaCtbCursorNext(pCur);
570,047,137✔
1093
    if (id == 0) {
569,899,723✔
1094
      break;
104,287,016✔
1095
    }
1096
    qTrace("vnodeGetCtbIdList: got ctb id %" PRId64 " for suid %" PRId64, id, suid);
465,612,707✔
1097
    if (NULL == taosArrayPush(list, &id)) {
465,970,365✔
1098
      qError("taosArrayPush failed");
×
1099
      code = terrno;
×
1100
      goto _exit;
×
1101
    }
1102
  }
1103

1104
_exit:
104,084,841✔
1105
  metaCloseCtbCursor(pCur);
104,084,841✔
1106
  return code;
104,068,035✔
1107
}
1108

1109
int32_t vnodeGetStbIdList(SVnode *pVnode, int64_t suid, SArray *list) {
59,459,275✔
1110
  int32_t      code = TSDB_CODE_SUCCESS;
59,459,275✔
1111
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, suid);
59,459,275✔
1112
  if (!pCur) {
59,459,275✔
1113
    return TSDB_CODE_OUT_OF_MEMORY;
×
1114
  }
1115

1116
  while (1) {
39,473,159✔
1117
    tb_uid_t id = metaStbCursorNext(pCur);
98,932,434✔
1118
    if (id == 0) {
98,929,385✔
1119
      break;
59,465,845✔
1120
    }
1121

1122
    if (NULL == taosArrayPush(list, &id)) {
39,476,444✔
1123
      qError("taosArrayPush failed");
×
1124
      code = terrno;
×
1125
      goto _exit;
×
1126
    }
1127
  }
1128

1129
_exit:
59,459,275✔
1130
  metaCloseStbCursor(pCur);
59,459,275✔
1131
  return code;
59,456,625✔
1132
}
1133

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

1142
  while (1) {
3,738✔
1143
    tb_uid_t id = metaStbCursorNext(pCur);
6,112✔
1144
    if (id == 0) {
6,112✔
1145
      break;
2,374✔
1146
    }
1147

1148
    if ((*filter) && (*filter)(arg, &id)) {
3,738✔
1149
      continue;
3,738✔
1150
    }
1151

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

1159
_exit:
2,374✔
1160
  metaCloseStbCursor(pCur);
2,374✔
1161
  return code;
2,374✔
1162
}
1163

1164
int32_t vnodeGetCtbNum(SVnode *pVnode, int64_t suid, int64_t *num) {
4,775,874✔
1165
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, suid, 0);
4,775,874✔
1166
  if (!pCur) {
4,780,397✔
1167
    return terrno;
×
1168
  }
1169

1170
  *num = 0;
4,780,397✔
1171
  while (1) {
2,865,626✔
1172
    tb_uid_t id = metaCtbCursorNext(pCur);
7,645,674✔
1173
    if (id == 0) {
7,646,186✔
1174
      break;
4,781,310✔
1175
    }
1176

1177
    ++(*num);
2,864,876✔
1178
  }
1179

1180
  metaCloseCtbCursor(pCur);
4,781,310✔
1181
  return TSDB_CODE_SUCCESS;
4,779,658✔
1182
}
1183

1184
int32_t vnodeGetStbColumnNum(SVnode *pVnode, tb_uid_t suid, int *num) {
4,779,443✔
1185
  SSchemaWrapper *pSW = metaGetTableSchema(pVnode->pMeta, suid, -1, 0, NULL, 0);
4,779,443✔
1186
  if (pSW) {
4,781,934✔
1187
    *num = pSW->nCols;
4,781,345✔
1188
    tDeleteSchemaWrapper(pSW);
1189
  } else {
1190
    *num = 2;
589✔
1191
  }
1192

1193
  return TSDB_CODE_SUCCESS;
4,781,228✔
1194
}
1195

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

1200
  int32_t code = metaReaderGetTableEntryByUid(&mr, suid);
4,778,995✔
1201
  if (code == TSDB_CODE_SUCCESS) {
4,780,758✔
1202
    if (keep) *keep = mr.me.stbEntry.keep;
4,780,169✔
1203
    if (flags) *flags = mr.me.flags;
4,780,169✔
1204
  } else {
1205
    if (keep) *keep = 0;
589✔
1206
    if (flags) *flags = 0;
589✔
1207
  }
1208

1209
  metaReaderClear(&mr);
4,781,141✔
1210
  return TSDB_CODE_SUCCESS;
4,782,248✔
1211
}
1212

1213
#ifdef TD_ENTERPRISE
1214
const char *tkLogStb[] = {"cluster_info",
1215
                          "data_dir",
1216
                          "dnodes_info",
1217
                          "d_info",
1218
                          "grants_info",
1219
                          "keeper_monitor",
1220
                          "logs",
1221
                          "log_dir",
1222
                          "log_summary",
1223
                          "m_info",
1224
                          "taosadapter_restful_http_request_fail",
1225
                          "taosadapter_restful_http_request_in_flight",
1226
                          "taosadapter_restful_http_request_summary_milliseconds",
1227
                          "taosadapter_restful_http_request_total",
1228
                          "taosadapter_system_cpu_percent",
1229
                          "taosadapter_system_mem_percent",
1230
                          "temp_dir",
1231
                          "vgroups_info",
1232
                          "vnodes_role",
1233
                          "taosd_dnodes_status",
1234
                          "adapter_conn_pool",
1235
                          "taosd_vnodes_info",
1236
                          "taosd_dnodes_metrics",
1237
                          "taosd_vgroups_info",
1238
                          "taos_sql_req",
1239
                          "taosd_mnodes_info",
1240
                          "adapter_c_interface",
1241
                          "taosd_cluster_info",
1242
                          "taosd_sql_req",
1243
                          "taosd_dnodes_info",
1244
                          "adapter_requests",
1245
                          "taosd_write_metrics",
1246
                          "adapter_status",
1247
                          "taos_slow_sql",
1248
                          "taos_slow_sql_detail",
1249
                          "taosd_cluster_basic",
1250
                          "taosd_dnodes_data_dirs",
1251
                          "taosd_dnodes_log_dirs",
1252
                          "xnode_agent_activities",
1253
                          "xnode_task_activities",
1254
                          "xnode_task_metrics",
1255
                          "taosx_task_csv",
1256
                          "taosx_task_progress",
1257
                          "taosx_task_kinghist",
1258
                          "taosx_task_tdengine2",
1259
                          "taosx_task_tdengine3",
1260
                          "taosx_task_opc_da",
1261
                          "taosx_task_opc_ua",
1262
                          "taosx_task_kafka",
1263
                          "taosx_task_influxdb",
1264
                          "taosx_task_mqtt",
1265
                          "taosx_task_avevahistorian",
1266
                          "taosx_task_opentsdb",
1267
                          "taosx_task_mysql",
1268
                          "taosx_task_postgres",
1269
                          "taosx_task_oracle",
1270
                          "taosx_task_mssql",
1271
                          "taosx_task_mongodb",
1272
                          "taosx_task_sparkplugb",
1273
                          "taosx_task_orc",
1274
                          "taosx_task_pulsar",
1275
                          "taosx_task_pspace"};
1276
const char *tkAuditStb[] = {"operations"};
1277
const int   tkLogStbNum = ARRAY_SIZE(tkLogStb);
1278
const int   tkAuditStbNum = ARRAY_SIZE(tkAuditStb);
1279

1280
// exclude stbs of taoskeeper log
1281
static int32_t vnodeGetTimeSeriesBlackList(SVnode *pVnode, int32_t *tbSize) {
59,453,981✔
1282
  int32_t      code = TSDB_CODE_SUCCESS;
59,453,981✔
1283
  int32_t      tbNum = 0;
59,453,981✔
1284
  const char **pTbArr = NULL;
59,453,981✔
1285
  const char  *dbName = NULL;
59,453,981✔
1286
  *tbSize = 0;
59,453,981✔
1287

1288
  if (!(dbName = strchr(pVnode->config.dbname, '.'))) return 0;
59,453,668✔
1289
  if (0 == strncmp(++dbName, "log", TSDB_DB_NAME_LEN)) {
59,453,011✔
1290
    tbNum = tkLogStbNum;
4,140✔
1291
    pTbArr = (const char **)&tkLogStb;
4,140✔
1292
  } else if (0 == strncmp(dbName, "audit", TSDB_DB_NAME_LEN) || pVnode->config.isAudit) {
59,451,499✔
1293
    tbNum = tkAuditStbNum;
2,846✔
1294
    pTbArr = (const char **)&tkAuditStb;
2,846✔
1295
  }
1296
  if (tbNum && pTbArr) {
59,458,602✔
1297
    *tbSize = metaSizeOfTbFilterCache(pVnode->pMeta, 0);
6,986✔
1298
    if (*tbSize < tbNum) {
6,986✔
1299
      for (int32_t i = 0; i < tbNum; ++i) {
124,514✔
1300
        tb_uid_t suid = metaGetTableEntryUidByName(pVnode->pMeta, pTbArr[i]);
121,847✔
1301
        if (suid != 0) {
121,847✔
1302
          code = metaPutTbToFilterCache(pVnode->pMeta, &suid, 0);
4,307✔
1303
          if (TSDB_CODE_SUCCESS != code) {
4,307✔
1304
            return code;
2,444✔
1305
          }
1306
        }
1307
      }
1308
      *tbSize = metaSizeOfTbFilterCache(pVnode->pMeta, 0);
2,667✔
1309
    }
1310
  }
1311

1312
  return code;
59,450,232✔
1313
}
1314
#endif
1315

1316
static bool vnodeTimeSeriesFilter(void *arg1, void *arg2) {
3,738✔
1317
  SVnode *pVnode = (SVnode *)arg1;
3,738✔
1318

1319
  if (metaTbInFilterCache(pVnode->pMeta, arg2, 0)) {
3,738✔
1320
    return true;
3,738✔
1321
  }
1322
  return false;
×
1323
}
1324

1325
int32_t vnodeGetTimeSeriesNum(SVnode *pVnode, int64_t *num) {
59,454,982✔
1326
  SArray *suidList = NULL;
59,454,982✔
1327

1328
  if (!(suidList = taosArrayInit(1, sizeof(tb_uid_t)))) {
59,454,982✔
1329
    return terrno;
×
1330
  }
1331

1332
  int32_t tbFilterSize = 0;
59,451,666✔
1333
  int32_t code = TSDB_CODE_SUCCESS;
59,445,418✔
1334
#ifdef TD_ENTERPRISE
1335
  code = vnodeGetTimeSeriesBlackList(pVnode, &tbFilterSize);
59,445,418✔
1336
  if (TSDB_CODE_SUCCESS != code) {
59,446,763✔
1337
    goto _exit;
2,444✔
1338
  }
1339
#endif
1340

1341
  if ((!tbFilterSize && vnodeGetStbIdList(pVnode, 0, suidList) < 0) ||
59,444,319✔
1342
      (tbFilterSize && vnodeGetStbIdListByFilter(pVnode, 0, suidList, vnodeTimeSeriesFilter, pVnode) < 0)) {
59,449,231✔
1343
    qError("vgId:%d, failed to get stb id list error: %s", TD_VID(pVnode), terrstr());
×
1344
    taosArrayDestroy(suidList);
×
1345
    return terrno;
×
1346
  }
1347

1348
  *num = 0;
59,443,662✔
1349
  int64_t arrSize = taosArrayGetSize(suidList);
59,452,538✔
1350
  for (int64_t i = 0; i < arrSize; ++i) {
98,920,118✔
1351
    tb_uid_t suid = *(tb_uid_t *)taosArrayGet(suidList, i);
39,464,760✔
1352

1353
    int64_t ctbNum = 0;
39,456,845✔
1354
    int32_t numOfCols = 0;
39,461,444✔
1355
    int8_t  flags = 0;
39,462,959✔
1356
    code = metaGetStbStats(pVnode, suid, &ctbNum, &numOfCols, &flags);
39,463,737✔
1357
    if (TSDB_CODE_SUCCESS != code) {
39,478,557✔
1358
      goto _exit;
×
1359
    }
1360
    if (!TABLE_IS_VIRTUAL(flags)) {
39,478,557✔
1361
      *num += ctbNum * (numOfCols - 1);
36,339,009✔
1362
    }
1363
  }
1364

1365
_exit:
59,452,731✔
1366
  taosArrayDestroy(suidList);
59,457,802✔
1367
  return TSDB_CODE_SUCCESS;
59,450,705✔
1368
}
1369

1370
int32_t vnodeGetAllCtbNum(SVnode *pVnode, int64_t *num) {
×
1371
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, 0);
×
1372
  if (!pCur) {
×
1373
    return terrno;
×
1374
  }
1375

1376
  *num = 0;
×
1377
  while (1) {
×
1378
    tb_uid_t id = metaStbCursorNext(pCur);
×
1379
    if (id == 0) {
×
1380
      break;
×
1381
    }
1382

1383
    int64_t ctbNum = 0;
×
1384
    int32_t code = vnodeGetCtbNum(pVnode, id, &ctbNum);
×
1385
    if (TSDB_CODE_SUCCESS != code) {
×
1386
      metaCloseStbCursor(pCur);
×
1387
      return code;
×
1388
    }
1389

1390
    *num += ctbNum;
×
1391
  }
1392

1393
  metaCloseStbCursor(pCur);
×
1394
  return TSDB_CODE_SUCCESS;
×
1395
}
1396

1397
void *vnodeGetIdx(void *pVnode) {
4,221,063✔
1398
  if (pVnode == NULL) {
4,221,063✔
1399
    return NULL;
×
1400
  }
1401

1402
  return metaGetIdx(((SVnode *)pVnode)->pMeta);
4,221,063✔
1403
}
1404

1405
void *vnodeGetIvtIdx(void *pVnode) {
4,221,440✔
1406
  if (pVnode == NULL) {
4,221,440✔
1407
    return NULL;
×
1408
  }
1409
  return metaGetIvtIdx(((SVnode *)pVnode)->pMeta);
4,221,440✔
1410
}
1411

1412
int32_t vnodeGetTableSchema(void *pVnode, int64_t uid, STSchema **pSchema, int64_t *suid, SSchemaWrapper **pTagSchema) {
332,367✔
1413
  return tsdbGetTableSchema(((SVnode *)pVnode)->pMeta, uid, pSchema, suid, pTagSchema);
332,367✔
1414
}
1415

1416
static FORCE_INLINE int32_t vnodeGetDBPrimaryInfo(SVnode *pVnode, SDbSizeStatisInfo *pInfo) {
1417
  int32_t code = 0;
6,362,356✔
1418
  char    path[TSDB_FILENAME_LEN] = {0};
6,362,356✔
1419

1420
  char   *dirName[] = {VNODE_TSDB_DIR, VNODE_WAL_DIR, VNODE_META_DIR, VNODE_TSDB_CACHE_DIR};
6,361,996✔
1421
  int64_t dirSize[4];
6,361,308✔
1422

1423
  vnodeGetPrimaryPath(pVnode, false, path, TSDB_FILENAME_LEN);
6,362,176✔
1424
  int32_t offset = strlen(path);
6,362,356✔
1425

1426
  for (int i = 0; i < sizeof(dirName) / sizeof(dirName[0]); i++) {
31,811,780✔
1427
    int64_t size = {0};
25,449,424✔
1428
    (void)snprintf(path + offset, TSDB_FILENAME_LEN - offset, "%s%s", TD_DIRSEP, dirName[i]);
25,449,424✔
1429
    code = taosGetDirSize(path, &size);
25,449,244✔
1430
    if (code != 0) {
25,449,424✔
UNCOV
1431
      uWarn("vnode %d get dir %s %s size failed since %s", TD_VID(pVnode), path, dirName[i], tstrerror(code));
×
1432
    }
1433
    path[offset] = 0;
25,449,424✔
1434
    dirSize[i] = size;
25,449,424✔
1435
  }
1436

1437
  pInfo->l1Size = 0;
6,362,356✔
1438
  pInfo->walSize = dirSize[1];
6,362,356✔
1439
  pInfo->metaSize = dirSize[2];
6,362,356✔
1440
  pInfo->cacheSize = dirSize[3];
6,362,356✔
1441
  return code;
6,362,356✔
1442
}
1443
int32_t vnodeGetDBSize(void *pVnode, SDbSizeStatisInfo *pInfo) {
6,362,356✔
1444
  int32_t code = 0;
6,362,356✔
1445
  int32_t lino = 0;
6,362,356✔
1446
  SVnode *pVnodeObj = pVnode;
6,362,356✔
1447
  if (pVnodeObj == NULL) {
6,362,356✔
1448
    return TSDB_CODE_VND_NOT_EXIST;
×
1449
  }
1450
  code = vnodeGetDBPrimaryInfo(pVnode, pInfo);
6,362,356✔
1451
  if (code != 0) goto _exit;
6,362,356✔
1452

1453
  code = tsdbGetFsSize(pVnodeObj->pTsdb, pInfo);
6,362,356✔
1454
_exit:
6,362,356✔
1455
  return code;
6,362,356✔
1456
}
1457

1458
/*
1459
 * Get raw write metrics for a vnode
1460
 */
1461
int32_t vnodeGetRawWriteMetrics(void *pVnode, SRawWriteMetrics *pRawMetrics) {
×
1462
  if (pVnode == NULL || pRawMetrics == NULL) {
×
1463
    return TSDB_CODE_INVALID_PARA;
×
1464
  }
1465

1466
  SVnode      *pVnode1 = (SVnode *)pVnode;
×
1467
  SSyncMetrics syncMetrics = syncGetMetrics(pVnode1->sync);
×
1468

1469
  // Copy values following SRawWriteMetrics structure order
1470
  pRawMetrics->total_requests = atomic_load_64(&pVnode1->writeMetrics.total_requests);
×
1471
  pRawMetrics->total_rows = atomic_load_64(&pVnode1->writeMetrics.total_rows);
×
1472
  pRawMetrics->total_bytes = atomic_load_64(&pVnode1->writeMetrics.total_bytes);
×
1473
  pRawMetrics->fetch_batch_meta_time = atomic_load_64(&pVnode1->writeMetrics.fetch_batch_meta_time);
×
1474
  pRawMetrics->fetch_batch_meta_count = atomic_load_64(&pVnode1->writeMetrics.fetch_batch_meta_count);
×
1475
  pRawMetrics->preprocess_time = atomic_load_64(&pVnode1->writeMetrics.preprocess_time);
×
1476
  pRawMetrics->wal_write_bytes = atomic_load_64(&syncMetrics.wal_write_bytes);
×
1477
  pRawMetrics->wal_write_time = atomic_load_64(&syncMetrics.wal_write_time);
×
1478
  pRawMetrics->apply_bytes = atomic_load_64(&pVnode1->writeMetrics.apply_bytes);
×
1479
  pRawMetrics->apply_time = atomic_load_64(&pVnode1->writeMetrics.apply_time);
×
1480
  pRawMetrics->commit_count = atomic_load_64(&pVnode1->writeMetrics.commit_count);
×
1481
  pRawMetrics->commit_time = atomic_load_64(&pVnode1->writeMetrics.commit_time);
×
1482
  pRawMetrics->memtable_wait_time = atomic_load_64(&pVnode1->writeMetrics.memtable_wait_time);
×
1483
  pRawMetrics->blocked_commit_count = atomic_load_64(&pVnode1->writeMetrics.blocked_commit_count);
×
1484
  pRawMetrics->blocked_commit_time = atomic_load_64(&pVnode1->writeMetrics.block_commit_time);
×
1485
  pRawMetrics->merge_count = atomic_load_64(&pVnode1->writeMetrics.merge_count);
×
1486
  pRawMetrics->merge_time = atomic_load_64(&pVnode1->writeMetrics.merge_time);
×
1487
  pRawMetrics->last_cache_commit_time = atomic_load_64(&pVnode1->writeMetrics.last_cache_commit_time);
×
1488
  pRawMetrics->last_cache_commit_count = atomic_load_64(&pVnode1->writeMetrics.last_cache_commit_count);
×
1489

1490
  return 0;
×
1491
}
1492

1493
/*
1494
 * Reset raw write metrics for a vnode by subtracting old values
1495
 */
1496
int32_t vnodeResetRawWriteMetrics(void *pVnode, const SRawWriteMetrics *pOldMetrics) {
×
1497
  if (pVnode == NULL || pOldMetrics == NULL) {
×
1498
    return TSDB_CODE_INVALID_PARA;
×
1499
  }
1500

1501
  SVnode *pVnode1 = (SVnode *)pVnode;
×
1502

1503
  // Reset vnode write metrics using atomic operations to subtract old values
1504
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.total_requests, pOldMetrics->total_requests);
×
1505
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.total_rows, pOldMetrics->total_rows);
×
1506
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.total_bytes, pOldMetrics->total_bytes);
×
1507

1508
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.fetch_batch_meta_time, pOldMetrics->fetch_batch_meta_time);
×
1509
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.fetch_batch_meta_count, pOldMetrics->fetch_batch_meta_count);
×
1510
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.preprocess_time, pOldMetrics->preprocess_time);
×
1511
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.apply_bytes, pOldMetrics->apply_bytes);
×
1512
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.apply_time, pOldMetrics->apply_time);
×
1513
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.commit_count, pOldMetrics->commit_count);
×
1514

1515
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.commit_time, pOldMetrics->commit_time);
×
1516
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.merge_time, pOldMetrics->merge_time);
×
1517

1518
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.memtable_wait_time, pOldMetrics->memtable_wait_time);
×
1519
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.blocked_commit_count, pOldMetrics->blocked_commit_count);
×
1520
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.block_commit_time, pOldMetrics->blocked_commit_time);
×
1521
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.merge_count, pOldMetrics->merge_count);
×
1522

1523
  // Reset new cache metrics
1524
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.last_cache_commit_time, pOldMetrics->last_cache_commit_time);
×
1525
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.last_cache_commit_count, pOldMetrics->last_cache_commit_count);
×
1526

1527
  // Reset sync metrics
1528
  SSyncMetrics syncMetrics = {
×
1529
      .wal_write_bytes = pOldMetrics->wal_write_bytes,
×
1530
      .wal_write_time = pOldMetrics->wal_write_time,
×
1531
  };
1532
  syncResetMetrics(pVnode1->sync, &syncMetrics);
×
1533

1534
  return 0;
×
1535
}
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