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

taosdata / TDengine / #5057

17 May 2026 01:15AM UTC coverage: 73.406% (+0.02%) from 73.384%
#5057

push

travis-ci

web-flow
feat (TDgpt): Dynamic Model Synchronization Enhancements (#35344)

* refactor: do some internal refactor.

* fix: fix multiprocess sync issue.

* feat: add dynamic anomaly detection and forecasting services

* fix: log error message for undeploying model in exception handling

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* fix: handle undeploy when model exists only on disk

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/286aafa0-c3ce-4c27-b803-2707571e9dc1

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* fix: guard dynamic registry concurrent access

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/5e4db858-6458-40f4-ac28-d1b1b7f97c18

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* fix: tighten service list locking scope

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/5e4db858-6458-40f4-ac28-d1b1b7f97c18

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* fix: restore prophet support and update tests per review feedback

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/92298ae1-7da6-4d07-b20e-101c7cd0b26b

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* fix: improve test name and move copy inside lock scope

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/92298ae1-7da6-4d07-b20e-101c7cd0b26b

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* Potential fix for pull request finding

Co-au... (continued)

281727 of 383795 relevant lines covered (73.41%)

136101761.85 hits per line

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

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

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

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

36
int32_t fillTableColCmpr(SMetaReader *reader, SSchemaExt *pExt, int32_t numOfCol) {
23,196,703✔
37
  int8_t tblType = reader->me.type;
23,196,703✔
38
  if (withColCompress(tblType)) {
23,196,945✔
39
    SColCmprWrapper *p = &(reader->me.colCmpr);
23,038,185✔
40
    if (numOfCol != p->nCols) {
23,038,427✔
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++) {
268,377,857✔
45
      SColCmpr *pCmpr = &p->pColCmpr[i];
245,340,838✔
46
      pExt[i].colId = pCmpr->id;
245,342,773✔
47
      pExt[i].compress = pCmpr->alg;
245,342,036✔
48
    }
49
  }
50
  return 0;
23,196,703✔
51
}
52

53
void vnodeDebugTableMeta(STableMetaRsp *pMeta) {
23,121,736✔
54
  if (!(qDebugFlag & DEBUG_DEBUG)) {
23,121,736✔
55
    return;
425,639✔
56
  }
57

58
  qDebug("tbName:%s", pMeta->tbName);
22,696,097✔
59
  qDebug("stbName:%s", pMeta->stbName);
22,698,218✔
60
  qDebug("dbFName:%s", pMeta->dbFName);
22,698,218✔
61
  qDebug("dbId:%" PRId64, pMeta->dbId);
22,698,218✔
62
  qDebug("numOfTags:%d", pMeta->numOfTags);
22,698,218✔
63
  qDebug("numOfColumns:%d", pMeta->numOfColumns);
22,698,218✔
64
  qDebug("precision:%d", pMeta->precision);
22,698,218✔
65
  qDebug("tableType:%d", pMeta->tableType);
22,698,218✔
66
  qDebug("sversion:%d", pMeta->sversion);
22,698,218✔
67
  qDebug("tversion:%d", pMeta->tversion);
22,698,218✔
68
  qDebug("suid:%" PRIu64, pMeta->suid);
22,698,218✔
69
  qDebug("tuid:%" PRIu64, pMeta->tuid);
22,698,218✔
70
  qDebug("vgId:%d", pMeta->vgId);
22,698,218✔
71
  qDebug("sysInfo:%d", pMeta->sysInfo);
22,698,218✔
72
  if (pMeta->pSchemas) {
22,698,218✔
73
    for (int32_t i = 0; i < (pMeta->numOfColumns + pMeta->numOfTags); ++i) {
310,720,395✔
74
      SSchema *pSchema = pMeta->pSchemas + i;
288,022,177✔
75
      qDebug("%d col/tag: type:%d, flags:%d, colId:%d, bytes:%d, name:%s", i, pSchema->type, pSchema->flags,
288,022,177✔
76
             pSchema->colId, pSchema->bytes, pSchema->name);
77
    }
78
  }
79
}
80

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

103
int32_t fillTableTagRef(SMetaReader *reader, SColRef *pRef, int32_t numOfTagRefs) {
×
104
  int8_t tblType = reader->me.type;
×
105
  if (hasRefCol(tblType)) {
×
106
    SColRefWrapper *p = &(reader->me.colRef);
×
107
    if (numOfTagRefs != p->nTagRefs) {
×
108
      vError("fillTableTagRef table type:%d, tag ref num:%d, expected:%d mismatch", tblType, numOfTagRefs, p->nTagRefs);
×
109
      return TSDB_CODE_APP_ERROR;
×
110
    }
111
    for (int i = 0; i < p->nTagRefs; i++) {
×
112
      SColRef *pTagRef = &p->pTagRef[i];
×
113
      pRef[i].hasRef = pTagRef->hasRef;
×
114
      pRef[i].id = pTagRef->id;
×
115
      if (pRef[i].hasRef) {
×
116
        tstrncpy(pRef[i].refDbName, pTagRef->refDbName, TSDB_DB_NAME_LEN);
×
117
        tstrncpy(pRef[i].refTableName, pTagRef->refTableName, TSDB_TABLE_NAME_LEN);
×
118
        tstrncpy(pRef[i].refColName, pTagRef->refColName, TSDB_COL_NAME_LEN);
×
119
      }
120
    }
121
  }
122
  return 0;
×
123
}
124

125
int32_t vnodeGetTableMeta(SVnode *pVnode, SRpcMsg *pMsg, bool direct) {
33,758,979✔
126
  STableInfoReq  infoReq = {0};
33,758,979✔
127
  STableMetaRsp  metaRsp = {0};
33,758,979✔
128
  SMetaReader    mer1 = {0};
33,758,979✔
129
  SMetaReader    mer2 = {0};
33,758,979✔
130
  char           tableFName[TSDB_TABLE_FNAME_LEN];
33,755,735✔
131
  bool           reqTbUid = false;
33,758,554✔
132
  SRpcMsg        rpcMsg = {0};
33,758,554✔
133
  int32_t        code = 0;
33,758,554✔
134
  int32_t        rspLen = 0;
33,758,554✔
135
  void          *pRsp = NULL;
33,758,554✔
136
  SSchemaWrapper schema = {0};
33,758,554✔
137
  SSchemaWrapper schemaTag = {0};
33,758,554✔
138
  uint8_t        autoCreateCtb = 0;
33,758,554✔
139

140
  // decode req
141
  if (tDeserializeSTableInfoReq(pMsg->pCont, pMsg->contLen, &infoReq) != 0) {
33,758,554✔
142
    code = terrno;
×
143
    goto _exit4;
×
144
  }
145
  autoCreateCtb = infoReq.autoCreateCtb;
33,759,736✔
146

147
  if (infoReq.option == REQ_OPT_TBUID) reqTbUid = true;
33,759,736✔
148
  metaRsp.dbId = pVnode->config.dbId;
33,759,736✔
149
  tstrncpy(metaRsp.tbName, infoReq.tbName, TSDB_TABLE_NAME_LEN);
33,759,137✔
150
  (void)memcpy(metaRsp.dbFName, infoReq.dbFName, sizeof(metaRsp.dbFName));
33,758,554✔
151

152
  if (!reqTbUid) {
33,758,554✔
153
    (void)snprintf(tableFName, TSDB_TABLE_FNAME_LEN, "%s.%s", infoReq.dbFName, infoReq.tbName);
33,459,999✔
154
    if (pVnode->mounted) tTrimMountPrefix(tableFName);
33,459,999✔
155
    code = vnodeValidateTableHash(pVnode, tableFName);
33,460,598✔
156
    if (code) {
33,460,475✔
157
      goto _exit4;
×
158
    }
159
  }
160

161
  // query meta
162
  metaReaderDoInit(&mer1, pVnode->pMeta, META_READER_LOCK);
33,759,030✔
163
  if (reqTbUid) {
33,757,827✔
164
    SET_ERRNO(0);
298,683✔
165
    uint64_t tbUid = taosStr2UInt64(infoReq.tbName, NULL, 10);
298,683✔
166
    if (ERRNO == ERANGE || tbUid == 0) {
298,683✔
167
      code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
168
      goto _exit3;
×
169
    }
170
    SMetaReader mr3 = {0};
298,683✔
171
    metaReaderDoInit(&mr3, ((SVnode *)pVnode)->pMeta, META_READER_NOLOCK);
298,683✔
172
    if ((code = metaReaderGetTableEntryByUid(&mr3, tbUid)) < 0) {
298,683✔
173
      metaReaderClear(&mr3);
106,863✔
174
      TAOS_CHECK_GOTO(code, NULL, _exit3);
106,863✔
175
    }
176
    tstrncpy(metaRsp.tbName, mr3.me.name, TSDB_TABLE_NAME_LEN);
191,820✔
177
    metaReaderClear(&mr3);
191,820✔
178
    TAOS_CHECK_GOTO(metaGetTableEntryByName(&mer1, metaRsp.tbName), NULL, _exit3);
191,578✔
179
  } else if (metaGetTableEntryByName(&mer1, infoReq.tbName) < 0) {
33,459,144✔
180
    code = terrno;
10,528,857✔
181
    goto _exit3;
10,528,432✔
182
  }
183

184
  metaRsp.tableType = mer1.me.type;
23,122,161✔
185
  metaRsp.vgId = TD_VID(pVnode);
23,122,161✔
186
  metaRsp.tuid = mer1.me.uid;
23,123,615✔
187
  metaRsp.isAudit = pVnode->config.isAudit ? 1 : 0;
23,123,615✔
188

189
  switch (mer1.me.type) {
23,123,428✔
190
    case TSDB_SUPER_TABLE: {
4,324,556✔
191
      tstrncpy(metaRsp.stbName, mer1.me.name, sizeof(metaRsp.stbName));
4,324,556✔
192
      schema = mer1.me.stbEntry.schemaRow;
4,324,314✔
193
      schemaTag = mer1.me.stbEntry.schemaTag;
4,324,314✔
194
      metaRsp.suid = mer1.me.uid;
4,324,314✔
195
      metaRsp.virtualStb = TABLE_IS_VIRTUAL(mer1.me.flags);
4,324,314✔
196
      metaRsp.ownerId = mer1.me.stbEntry.ownerId;
4,324,314✔
197
      metaRsp.secLvl = mer1.me.stbEntry.securityLevel;
4,324,314✔
198
      break;
4,324,314✔
199
    }
200
    case TSDB_CHILD_TABLE:
14,655,016✔
201
    case TSDB_VIRTUAL_CHILD_TABLE:{
202
      metaReaderDoInit(&mer2, pVnode->pMeta, META_READER_NOLOCK);
14,655,016✔
203
      if (metaReaderGetTableEntryByUid(&mer2, mer1.me.ctbEntry.suid) < 0) goto _exit2;
14,655,016✔
204

205
      tstrncpy(metaRsp.stbName, mer2.me.name, sizeof(metaRsp.stbName));
14,655,016✔
206
      metaRsp.suid = mer2.me.uid;
14,654,154✔
207
      metaRsp.ownerId = mer2.me.stbEntry.ownerId;  // child table inherits ownerId from stb
14,654,154✔
208
      metaRsp.secLvl = mer2.me.stbEntry.securityLevel;  // child table inherits secLvl from stb
14,654,154✔
209
      schema = mer2.me.stbEntry.schemaRow;
14,654,154✔
210
      schemaTag = mer2.me.stbEntry.schemaTag;
14,654,154✔
211
      break;
14,654,154✔
212
    }
213
    case TSDB_NORMAL_TABLE:
3,990,441✔
214
      metaRsp.secLvl = pVnode->config.securityLevel;  // normal table inherits secLvl from vnode config
3,990,441✔
215
    case TSDB_VIRTUAL_NORMAL_TABLE: {
4,143,558✔
216
      schema = mer1.me.ntbEntry.schemaRow;
4,143,558✔
217
      metaRsp.ownerId = mer1.me.ntbEntry.ownerId;
4,143,558✔
218
      break;
4,143,558✔
219
    }
220
    default: {
×
221
      vError("vnodeGetTableMeta get invalid table type:%d", mer1.me.type);
×
222
      goto _exit3;
×
223
    }
224
  }
225

226
  metaRsp.numOfTags = schemaTag.nCols;
23,122,026✔
227
  metaRsp.numOfColumns = schema.nCols;
23,122,026✔
228
  metaRsp.precision = pVnode->config.tsdbCfg.precision;
23,122,026✔
229
  metaRsp.sversion = schema.version;
23,122,161✔
230
  metaRsp.tversion = schemaTag.version;
23,122,161✔
231
  metaRsp.pSchemas = (SSchema *)taosMemoryMalloc(sizeof(SSchema) * (metaRsp.numOfColumns + metaRsp.numOfTags));
23,122,161✔
232
  metaRsp.pSchemaExt = (SSchemaExt *)taosMemoryCalloc(metaRsp.numOfColumns, sizeof(SSchemaExt));
23,122,701✔
233
  if (NULL == metaRsp.pSchemas || NULL == metaRsp.pSchemaExt) {
23,121,732✔
234
    code = terrno;
×
235
    goto _exit;
×
236
  }
237
  (void)memcpy(metaRsp.pSchemas, schema.pSchema, sizeof(SSchema) * schema.nCols);
23,122,888✔
238
  if (schemaTag.nCols) {
23,122,888✔
239
    (void)memcpy(metaRsp.pSchemas + schema.nCols, schemaTag.pSchema, sizeof(SSchema) * schemaTag.nCols);
18,978,901✔
240
  }
241
  if (metaRsp.pSchemaExt) {
23,123,317✔
242
    SMetaReader *pReader =
23,122,701✔
243
        (mer1.me.type == TSDB_CHILD_TABLE || mer1.me.type == TSDB_VIRTUAL_CHILD_TABLE) ? &mer2 : &mer1;
23,122,701✔
244
    code = fillTableColCmpr(pReader, metaRsp.pSchemaExt, metaRsp.numOfColumns);
23,122,701✔
245
    if (code < 0) {
23,122,463✔
246
      goto _exit;
×
247
    }
248
    for (int32_t i = 0; i < metaRsp.numOfColumns; i++) {
269,469,687✔
249
      metaRsp.pSchemaExt[i].colId = schema.pSchema[i].colId;
246,349,685✔
250
      if (pReader->me.pExtSchemas) {
246,350,652✔
251
        metaRsp.pSchemaExt[i].typeMod = pReader->me.pExtSchemas[i].typeMod;
32,207,711✔
252
      }
253
    }
254
  } else {
255
    code = TSDB_CODE_OUT_OF_MEMORY;
616✔
256
    goto _exit;
616✔
257
  }
258
  if (hasRefCol(mer1.me.type)) {
23,120,002✔
259
    metaRsp.rversion = mer1.me.colRef.version;
403,804✔
260
    metaRsp.pColRefs = (SColRef*)taosMemoryMalloc(sizeof(SColRef) * metaRsp.numOfColumns);
403,804✔
261
    if (metaRsp.pColRefs) {
403,812✔
262
      code = fillTableColRef(&mer1, metaRsp.pColRefs, metaRsp.numOfColumns);
403,812✔
263
      if (code < 0) {
403,812✔
264
        goto _exit;
×
265
      }
266
    }
267
    metaRsp.numOfColRefs = metaRsp.numOfColumns;
403,812✔
268

269
    // Fill tag references
270
    if (mer1.me.colRef.nTagRefs > 0) {
403,812✔
271
      metaRsp.pTagRefs = (SColRef*)taosMemoryMalloc(sizeof(SColRef) * mer1.me.colRef.nTagRefs);
×
272
      if (metaRsp.pTagRefs) {
×
273
        code = fillTableTagRef(&mer1, metaRsp.pTagRefs, mer1.me.colRef.nTagRefs);
×
274
        if (code < 0) {
×
275
          taosMemoryFreeClear(metaRsp.pTagRefs);
×
276
          goto _exit;
×
277
        }
278
      } else {
279
        code = terrno;
×
280
        goto _exit;
×
281
      }
282
      metaRsp.numOfTagRefs = mer1.me.colRef.nTagRefs;
×
283
    } else {
284
      metaRsp.pTagRefs = NULL;
403,812✔
285
      metaRsp.numOfTagRefs = 0;
403,812✔
286
    }
287
  } else {
288
    metaRsp.pColRefs = NULL;
22,717,924✔
289
    metaRsp.numOfColRefs = 0;
22,717,924✔
290
    metaRsp.pTagRefs = NULL;
22,717,924✔
291
    metaRsp.numOfTagRefs = 0;
22,717,924✔
292
  }
293

294
  vnodeDebugTableMeta(&metaRsp);
23,121,736✔
295

296
  // encode and send response
297
  rspLen = tSerializeSTableMetaRsp(NULL, 0, &metaRsp);
23,123,857✔
298
  if (rspLen < 0) {
23,122,403✔
299
    code = terrno;
×
300
    goto _exit;
×
301
  }
302

303
  if (direct) {
23,122,403✔
304
    pRsp = rpcMallocCont(rspLen);
574,254✔
305
  } else {
306
    pRsp = taosMemoryCalloc(1, rspLen);
22,548,149✔
307
  }
308

309
  if (pRsp == NULL) {
23,121,804✔
310
    code = terrno;
×
311
    goto _exit;
×
312
  }
313

314
  rspLen = tSerializeSTableMetaRsp(pRsp, rspLen, &metaRsp);
23,121,804✔
315
  if (rspLen < 0) {
23,123,130✔
316
    code = terrno;
×
317
    goto _exit;
×
318
  }
319

320
_exit:
23,123,746✔
321
  taosMemoryFree(metaRsp.pColRefs);
23,122,403✔
322
  taosMemoryFree(metaRsp.pSchemas);
23,120,949✔
323
  taosMemoryFree(metaRsp.pSchemaExt);
23,122,515✔
324
  taosMemoryFree(metaRsp.pTagRefs);
23,122,515✔
325
_exit2:
23,121,227✔
326
  metaReaderClear(&mer2);
23,123,130✔
327
_exit3:
33,755,517✔
328
  metaReaderClear(&mer1);
33,759,706✔
329
_exit4:
33,758,984✔
330
  rpcMsg.info = pMsg->info;
33,760,433✔
331
  rpcMsg.pCont = pRsp;
33,760,433✔
332
  rpcMsg.contLen = rspLen;
33,760,433✔
333
  rpcMsg.code = code;
33,760,433✔
334
  rpcMsg.msgType = pMsg->msgType;
33,760,433✔
335

336
  if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST && autoCreateCtb == 1) {
33,758,856✔
337
    code = TSDB_CODE_SUCCESS;
6,481,888✔
338
  }
339

340
  if (code) {
33,758,856✔
341
    qError("vgId:%d, get table %s meta with %" PRIu8 " failed cause of %s", pVnode->config.vgId, infoReq.tbName,
4,154,257✔
342
           infoReq.option, tstrerror(code));
343
  }
344

345
  if (direct) {
33,758,010✔
346
    tmsgSendRsp(&rpcMsg);
1,434,262✔
347
  } else {
348
    *pMsg = rpcMsg;
32,323,748✔
349
  }
350

351
  return code;
33,758,010✔
352
}
353

354
int32_t vnodeGetTableCfg(SVnode *pVnode, SRpcMsg *pMsg, bool direct) {
73,088✔
355
  STableCfgReq   cfgReq = {0};
73,088✔
356
  STableCfgRsp   cfgRsp = {0};
73,088✔
357
  SMetaReader    mer1 = {0};
73,088✔
358
  SMetaReader    mer2 = {0};
73,088✔
359
  char           tableFName[TSDB_TABLE_FNAME_LEN];
73,088✔
360
  SRpcMsg        rpcMsg = {0};
73,088✔
361
  int32_t        code = 0;
73,088✔
362
  int32_t        rspLen = 0;
73,088✔
363
  void          *pRsp = NULL;
73,088✔
364
  SSchemaWrapper schema = {0};
73,088✔
365
  SSchemaWrapper schemaTag = {0};
73,088✔
366

367
  // decode req
368
  if (tDeserializeSTableCfgReq(pMsg->pCont, pMsg->contLen, &cfgReq) != 0) {
73,088✔
369
    code = terrno;
×
370
    goto _exit;
×
371
  }
372

373
  tstrncpy(cfgRsp.tbName, cfgReq.tbName, TSDB_TABLE_NAME_LEN);
73,088✔
374
  (void)memcpy(cfgRsp.dbFName, cfgReq.dbFName, sizeof(cfgRsp.dbFName));
73,088✔
375

376
  (void)snprintf(tableFName, TSDB_TABLE_FNAME_LEN, "%s.%s", cfgReq.dbFName, cfgReq.tbName);
73,088✔
377
  if (pVnode->mounted) tTrimMountPrefix(tableFName);
73,088✔
378
  code = vnodeValidateTableHash(pVnode, tableFName);
73,088✔
379
  if (code) {
73,088✔
380
    goto _exit;
×
381
  }
382

383
  // query meta
384
  metaReaderDoInit(&mer1, pVnode->pMeta, META_READER_LOCK);
73,088✔
385

386
  if (metaGetTableEntryByName(&mer1, cfgReq.tbName) < 0) {
73,088✔
387
    code = terrno;
×
388
    goto _exit;
×
389
  }
390

391
  cfgRsp.tableType = mer1.me.type;
73,088✔
392
  cfgRsp.isAudit = pVnode->config.isAudit ? 1 : 0;
73,088✔
393
  cfgRsp.secureDelete = pVnode->config.secureDelete;
73,088✔
394

395
  if (mer1.me.type == TSDB_SUPER_TABLE) {
73,088✔
396
    code = TSDB_CODE_VND_HASH_MISMATCH;
×
397
    goto _exit;
×
398
  } else if (mer1.me.type == TSDB_CHILD_TABLE || mer1.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
73,088✔
399
    metaReaderDoInit(&mer2, pVnode->pMeta, META_READER_NOLOCK);
51,177✔
400
    if (metaReaderGetTableEntryByUid(&mer2, mer1.me.ctbEntry.suid) < 0) goto _exit;
51,177✔
401

402
    tstrncpy(cfgRsp.stbName, mer2.me.name, TSDB_TABLE_NAME_LEN);
51,177✔
403
    schema = mer2.me.stbEntry.schemaRow;
51,177✔
404
    schemaTag = mer2.me.stbEntry.schemaTag;
51,177✔
405
    cfgRsp.ownerId = mer2.me.stbEntry.ownerId;        // child table inherits ownerId from stb
51,177✔
406
    cfgRsp.securityLevel = mer2.me.stbEntry.securityLevel;  // child table inherits secLvl from stb
51,177✔
407
    cfgRsp.ttl = mer1.me.ctbEntry.ttlDays;
51,177✔
408
    cfgRsp.commentLen = mer1.me.ctbEntry.commentLen;
51,177✔
409
    if (mer1.me.ctbEntry.commentLen > 0) {
51,177✔
410
      cfgRsp.pComment = taosStrdup(mer1.me.ctbEntry.comment);
×
411
      if (NULL == cfgRsp.pComment) {
×
412
        code = terrno;
×
413
        goto _exit;
×
414
      }
415
    }
416
    STag *pTag = (STag *)mer1.me.ctbEntry.pTags;
51,177✔
417
    cfgRsp.tagsLen = pTag->len;
51,177✔
418
    cfgRsp.pTags = taosMemoryMalloc(cfgRsp.tagsLen);
51,177✔
419
    if (NULL == cfgRsp.pTags) {
51,177✔
420
      code = terrno;
×
421
      goto _exit;
×
422
    }
423
    (void)memcpy(cfgRsp.pTags, pTag, cfgRsp.tagsLen);
51,177✔
424
  } else if (mer1.me.type == TSDB_NORMAL_TABLE || mer1.me.type == TSDB_VIRTUAL_NORMAL_TABLE) {
21,911✔
425
    schema = mer1.me.ntbEntry.schemaRow;
21,911✔
426
    cfgRsp.ttl = mer1.me.ntbEntry.ttlDays;
21,911✔
427
    cfgRsp.ownerId = mer1.me.ntbEntry.ownerId;
21,911✔
428
    cfgRsp.securityLevel = mer1.me.type == TSDB_NORMAL_TABLE ? pVnode->config.securityLevel : 0;
21,911✔
429
    cfgRsp.commentLen = mer1.me.ntbEntry.commentLen;
21,911✔
430
    if (mer1.me.ntbEntry.commentLen > 0) {
21,911✔
431
      cfgRsp.pComment = taosStrdup(mer1.me.ntbEntry.comment);
×
432
      if (NULL == cfgRsp.pComment) {
×
433
        code = terrno;
×
434
        goto _exit;
×
435
      }
436
    }
437
  } else {
438
    vError("vnodeGetTableCfg get invalid table type:%d", mer1.me.type);
×
439
    code = TSDB_CODE_APP_ERROR;
×
440
    goto _exit;
×
441
  }
442

443
  cfgRsp.numOfTags = schemaTag.nCols;
73,088✔
444
  cfgRsp.numOfColumns = schema.nCols;
73,088✔
445
  cfgRsp.virtualStb = false; // vnode don't have super table, so it's always false
73,088✔
446
  cfgRsp.pSchemas = (SSchema *)taosMemoryMalloc(sizeof(SSchema) * (cfgRsp.numOfColumns + cfgRsp.numOfTags));
73,088✔
447
  cfgRsp.pSchemaExt = (SSchemaExt *)taosMemoryCalloc(cfgRsp.numOfColumns, sizeof(SSchemaExt));
73,088✔
448
  cfgRsp.pColRefs = (SColRef *)taosMemoryMalloc(sizeof(SColRef) * cfgRsp.numOfColumns);
73,088✔
449
  cfgRsp.numOfTagRefs = 0;
73,088✔
450
  cfgRsp.pTagRefs = NULL;
73,088✔
451

452
  if (NULL == cfgRsp.pSchemas || NULL == cfgRsp.pSchemaExt || NULL == cfgRsp.pColRefs) {
73,088✔
453
    code = terrno;
×
454
    goto _exit;
×
455
  }
456
  (void)memcpy(cfgRsp.pSchemas, schema.pSchema, sizeof(SSchema) * schema.nCols);
73,088✔
457
  if (schemaTag.nCols) {
73,088✔
458
    (void)memcpy(cfgRsp.pSchemas + schema.nCols, schemaTag.pSchema, sizeof(SSchema) * schemaTag.nCols);
51,177✔
459
  }
460

461
  SMetaReader    *pReader = (mer1.me.type == TSDB_CHILD_TABLE || mer1.me.type == TSDB_VIRTUAL_CHILD_TABLE) ? &mer2 : &mer1;
73,088✔
462
  SColRefWrapper *pColRef = &mer1.me.colRef;
73,088✔
463

464
  if (withExtSchema(cfgRsp.tableType)) {
73,088✔
465
    code = fillTableColCmpr(pReader, cfgRsp.pSchemaExt, cfgRsp.numOfColumns);
73,088✔
466
    if (code < 0) {
73,088✔
467
      goto _exit;
×
468
    }
469

470
    for (int32_t i = 0; i < cfgRsp.numOfColumns; i++) {
391,395✔
471
      SSchemaExt *pSchExt = cfgRsp.pSchemaExt + i;
318,307✔
472
      pSchExt->colId = schema.pSchema[i].colId;
318,307✔
473
      if (pReader->me.pExtSchemas) {
318,307✔
474
        pSchExt->typeMod = pReader->me.pExtSchemas[i].typeMod;
35,435✔
475
      }
476
    }
477
  }
478

479
  cfgRsp.virtualStb = false;
73,088✔
480
  if (hasRefCol(cfgRsp.tableType)) {
73,088✔
481
    for (int32_t i = 0; i < cfgRsp.numOfColumns; i++) {
132,093✔
482
      SColRef *pRef = &pColRef->pColRef[i];
121,349✔
483
      cfgRsp.pColRefs[i].hasRef = pRef->hasRef;
121,349✔
484
      cfgRsp.pColRefs[i].id = pRef->id;
121,349✔
485
      if (cfgRsp.pColRefs[i].hasRef) {
121,349✔
486
        tstrncpy(cfgRsp.pColRefs[i].refDbName, pRef->refDbName, TSDB_DB_NAME_LEN);
65,172✔
487
        tstrncpy(cfgRsp.pColRefs[i].refTableName, pRef->refTableName, TSDB_TABLE_NAME_LEN);
65,172✔
488
        tstrncpy(cfgRsp.pColRefs[i].refColName, pRef->refColName, TSDB_COL_NAME_LEN);
65,172✔
489
      }
490
    }
491

492
    cfgRsp.numOfTagRefs = pColRef->nTagRefs;
10,744✔
493
    if (cfgRsp.numOfTagRefs > 0) {
10,744✔
494
      if (NULL == pColRef->pTagRef) {
×
495
        code = TSDB_CODE_APP_ERROR;
×
496
        goto _exit;
×
497
      }
498
      cfgRsp.pTagRefs = (SColRef *)taosMemoryMalloc(sizeof(SColRef) * cfgRsp.numOfTagRefs);
×
499
      if (NULL == cfgRsp.pTagRefs) {
×
500
        code = terrno;
×
501
        goto _exit;
×
502
      }
503

504
      for (int32_t i = 0; i < cfgRsp.numOfTagRefs; i++) {
×
505
        SColRef *pRef = &pColRef->pTagRef[i];
×
506
        cfgRsp.pTagRefs[i].hasRef = pRef->hasRef;
×
507
        cfgRsp.pTagRefs[i].id = pRef->id;
×
508
        if (cfgRsp.pTagRefs[i].hasRef) {
×
509
          tstrncpy(cfgRsp.pTagRefs[i].refDbName, pRef->refDbName, TSDB_DB_NAME_LEN);
×
510
          tstrncpy(cfgRsp.pTagRefs[i].refTableName, pRef->refTableName, TSDB_TABLE_NAME_LEN);
×
511
          tstrncpy(cfgRsp.pTagRefs[i].refColName, pRef->refColName, TSDB_COL_NAME_LEN);
×
512
        }
513
      }
514
    }
515
  }
516

517
  // encode and send response
518
  rspLen = tSerializeSTableCfgRsp(NULL, 0, &cfgRsp);
73,088✔
519
  if (rspLen < 0) {
73,088✔
520
    code = terrno;
×
521
    goto _exit;
×
522
  }
523

524
  if (direct) {
73,088✔
525
    pRsp = rpcMallocCont(rspLen);
×
526
  } else {
527
    pRsp = taosMemoryCalloc(1, rspLen);
73,088✔
528
  }
529

530
  if (pRsp == NULL) {
73,088✔
531
    code = terrno;
×
532
    goto _exit;
×
533
  }
534

535
  rspLen = tSerializeSTableCfgRsp(pRsp, rspLen, &cfgRsp);
73,088✔
536
  if (rspLen < 0) {
73,088✔
537
    code = terrno;
×
538
    goto _exit;
×
539
  }
540

541
_exit:
73,088✔
542
  rpcMsg.info = pMsg->info;
73,088✔
543
  rpcMsg.pCont = pRsp;
73,088✔
544
  rpcMsg.contLen = rspLen;
73,088✔
545
  rpcMsg.code = code;
73,088✔
546
  rpcMsg.msgType = pMsg->msgType;
73,088✔
547

548
  if (code) {
73,088✔
549
    qError("get table %s cfg failed cause of %s", cfgReq.tbName, tstrerror(code));
×
550
  }
551

552
  if (direct) {
73,088✔
553
    tmsgSendRsp(&rpcMsg);
×
554
  } else {
555
    *pMsg = rpcMsg;
73,088✔
556
  }
557

558
  tFreeSTableCfgRsp(&cfgRsp);
73,088✔
559
  metaReaderClear(&mer2);
73,088✔
560
  metaReaderClear(&mer1);
73,088✔
561
  return code;
73,088✔
562
}
563

564
static FORCE_INLINE void vnodeFreeSBatchRspMsg(void *p) {
565
  if (NULL == p) {
566
    return;
567
  }
568

569
  SBatchRspMsg *pRsp = (SBatchRspMsg *)p;
570
  rpcFreeCont(pRsp->msg);
571
}
572

573
int32_t vnodeGetBatchMeta(SVnode *pVnode, SRpcMsg *pMsg) {
30,951,850✔
574
  int32_t      code = 0;
30,951,850✔
575
  int32_t      rspSize = 0;
30,951,850✔
576
  SBatchReq    batchReq = {0};
30,951,850✔
577
  SBatchMsg   *req = NULL;
30,953,888✔
578
  SBatchRspMsg rsp = {0};
30,953,888✔
579
  SBatchRsp    batchRsp = {0};
30,936,669✔
580
  SRpcMsg      reqMsg = *pMsg;
30,935,927✔
581
  SRpcMsg      rspMsg = {0};
30,935,927✔
582
  void        *pRsp = NULL;
30,942,865✔
583

584
  if (tDeserializeSBatchReq(pMsg->pCont, pMsg->contLen, &batchReq)) {
30,942,865✔
585
    code = terrno;
×
586
    qError("tDeserializeSBatchReq failed");
×
587
    goto _exit;
×
588
  }
589

590
  int32_t msgNum = taosArrayGetSize(batchReq.pMsgs);
30,951,918✔
591
  if (msgNum >= MAX_META_MSG_IN_BATCH) {
30,940,777✔
592
    code = TSDB_CODE_INVALID_MSG;
×
593
    qError("too many msgs %d in vnode batch meta req", msgNum);
×
594
    goto _exit;
×
595
  }
596

597
  batchRsp.pRsps = taosArrayInit(msgNum, sizeof(SBatchRspMsg));
30,940,777✔
598
  if (NULL == batchRsp.pRsps) {
30,942,238✔
599
    code = terrno;
×
600
    qError("taosArrayInit %d SBatchRspMsg failed", msgNum);
×
601
    goto _exit;
×
602
  }
603

604
  for (int32_t i = 0; i < msgNum; ++i) {
67,988,607✔
605
    req = taosArrayGet(batchReq.pMsgs, i);
37,044,172✔
606
    if (req == NULL) {
37,044,149✔
607
      code = terrno;
×
608
      goto _exit;
×
609
    }
610

611
    reqMsg.msgType = req->msgType;
37,044,149✔
612
    reqMsg.pCont = req->msg;
37,041,564✔
613
    reqMsg.contLen = req->msgLen;
37,035,833✔
614

615
    switch (req->msgType) {
37,038,276✔
616
      case TDMT_VND_TABLE_META:
32,026,761✔
617
        // error code has been set into reqMsg, no need to handle it here.
618
        if (TSDB_CODE_SUCCESS != vnodeGetTableMeta(pVnode, &reqMsg, false)) {
32,026,761✔
619
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
3,187,817✔
620
        }
621
        break;
32,024,580✔
622
      case TDMT_VND_TABLE_NAME:
298,683✔
623
        // error code has been set into reqMsg, no need to handle it here.
624
        if (TSDB_CODE_SUCCESS != vnodeGetTableMeta(pVnode, &reqMsg, false)) {
298,683✔
625
          qWarn("vnodeGetBatchName failed, msgType:%d", req->msgType);
106,863✔
626
        }
627
        break;
298,683✔
628
      case TDMT_VND_TABLE_CFG:
73,088✔
629
        // error code has been set into reqMsg, no need to handle it here.
630
        if (TSDB_CODE_SUCCESS != vnodeGetTableCfg(pVnode, &reqMsg, false)) {
73,088✔
631
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
×
632
        }
633
        break;
73,088✔
634
      case TDMT_VND_VSUBTABLES_META:
×
635
        // error code has been set into reqMsg, no need to handle it here.
636
        if (TSDB_CODE_SUCCESS != vnodeGetVSubtablesMeta(pVnode, &reqMsg)) {
×
637
          qWarn("vnodeGetVSubtablesMeta failed, msgType:%d", req->msgType);
×
638
        }
639
        break;
×
640
      case TDMT_VND_VSTB_REF_DBS:
4,647,005✔
641
        // error code has been set into reqMsg, no need to handle it here.
642
        if (TSDB_CODE_SUCCESS != vnodeGetVStbRefDbs(pVnode, &reqMsg)) {
4,647,005✔
643
          qWarn("vnodeGetVStbRefDbs failed, msgType:%d", req->msgType);
×
644
        }
645
        break;
4,644,455✔
646
      default:
×
647
        qError("invalid req msgType %d", req->msgType);
×
648
        reqMsg.code = TSDB_CODE_INVALID_MSG;
×
649
        reqMsg.pCont = NULL;
×
650
        reqMsg.contLen = 0;
×
651
        break;
×
652
    }
653

654
    rsp.msgIdx = req->msgIdx;
37,040,806✔
655
    rsp.reqType = reqMsg.msgType;
37,039,516✔
656
    rsp.msgLen = reqMsg.contLen;
37,039,516✔
657
    rsp.rspCode = reqMsg.code;
37,039,516✔
658
    rsp.msg = reqMsg.pCont;
37,039,516✔
659

660
    if (NULL == taosArrayPush(batchRsp.pRsps, &rsp)) {
74,081,032✔
661
      qError("taosArrayPush failed");
×
662
      code = terrno;
×
663
      goto _exit;
×
664
    }
665
  }
666

667
  rspSize = tSerializeSBatchRsp(NULL, 0, &batchRsp);
30,944,435✔
668
  if (rspSize < 0) {
30,943,000✔
669
    qError("tSerializeSBatchRsp failed");
×
670
    code = terrno;
×
671
    goto _exit;
×
672
  }
673
  pRsp = rpcMallocCont(rspSize);
30,943,000✔
674
  if (pRsp == NULL) {
30,930,119✔
675
    qError("rpcMallocCont %d failed", rspSize);
×
676
    code = terrno;
×
677
    goto _exit;
×
678
  }
679
  if (tSerializeSBatchRsp(pRsp, rspSize, &batchRsp) < 0) {
30,930,119✔
680
    qError("tSerializeSBatchRsp %d failed", rspSize);
×
681
    code = terrno;
×
682
    goto _exit;
×
683
  }
684

685
_exit:
30,940,649✔
686

687
  rspMsg.info = pMsg->info;
30,948,254✔
688
  rspMsg.pCont = pRsp;
30,945,396✔
689
  rspMsg.contLen = rspSize;
30,945,396✔
690
  rspMsg.code = code;
30,945,396✔
691
  rspMsg.msgType = pMsg->msgType;
30,945,396✔
692

693
  if (code) {
30,928,391✔
694
    qError("vnd get batch meta failed cause of %s", tstrerror(code));
×
695
  }
696

697
  taosArrayDestroyEx(batchReq.pMsgs, tFreeSBatchReqMsg);
30,928,391✔
698
  taosArrayDestroyEx(batchRsp.pRsps, tFreeSBatchRspMsg);
30,927,235✔
699

700
  tmsgSendRsp(&rspMsg);
30,916,954✔
701

702
  return code;
30,952,568✔
703
}
704

705
#define VNODE_DO_META_QUERY(pVnode, cmd)                 \
706
  do {                                                   \
707
    (void)taosThreadRwlockRdlock(&(pVnode)->metaRWLock); \
708
    cmd;                                                 \
709
    (void)taosThreadRwlockUnlock(&(pVnode)->metaRWLock); \
710
  } while (0)
711

712
int32_t vnodeReadVSubtables(SReadHandle* pHandle, int64_t suid, SArray** ppRes) {
×
713
  int32_t                    code = TSDB_CODE_SUCCESS;
×
714
  int32_t                    line = 0;
×
715
  SMetaReader                mr = {0};
×
716
  bool                       readerInit = false;
×
717
  SVCTableRefCols*           pTb = NULL;
×
718
  int32_t                    refColsNum = 0;
×
719
  char                       tbFName[TSDB_TABLE_FNAME_LEN];
×
720
  SSHashObj*                 pSrcTbls = NULL;
×
721

722
  SArray *pList = taosArrayInit(10, sizeof(uint64_t));
×
723
  QUERY_CHECK_NULL(pList, code, line, _return, terrno);
×
724

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

727
  size_t num = taosArrayGetSize(pList);
×
728
  *ppRes = taosArrayInit(num, POINTER_BYTES);
×
729
  QUERY_CHECK_NULL(*ppRes, code, line, _return, terrno);
×
730
  pSrcTbls = tSimpleHashInit(10, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
×
731
  QUERY_CHECK_NULL(pSrcTbls, code, line, _return, terrno);
×
732

733
  for (int32_t i = 0; i < num; ++i) {
×
734
    uint64_t* id = taosArrayGet(pList, i);
×
735
    QUERY_CHECK_NULL(id, code, line, _return, terrno);
×
736
    pHandle->api.metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pHandle->api.metaFn);
×
737
    QUERY_CHECK_CODE(pHandle->api.metaReaderFn.getTableEntryByUid(&mr, *id), line, _return);
×
738
    readerInit = true;
×
739

740
    refColsNum = 0;
×
741
    for (int32_t j = 0; j < mr.me.colRef.nCols; j++) {
×
742
      if (mr.me.colRef.pColRef[j].hasRef) {
×
743
        refColsNum++;
×
744
      }
745
    }
746

747
    if (refColsNum <= 0) {
×
748
      pHandle->api.metaReaderFn.clearReader(&mr);
×
749
      readerInit = false;
×
750
      continue;
×
751
    }
752

753
    pTb = taosMemoryCalloc(1, refColsNum * sizeof(SRefColInfo) + sizeof(*pTb));
×
754
    QUERY_CHECK_NULL(pTb, code, line, _return, terrno);
×
755

756
    pTb->uid = mr.me.uid;
×
757
    pTb->numOfColRefs = refColsNum;
×
758
    pTb->refCols = (SRefColInfo*)(pTb + 1);
×
759

760
    refColsNum = 0;
×
761
    tSimpleHashClear(pSrcTbls);
×
762
    for (int32_t j = 0; j < mr.me.colRef.nCols; j++) {
×
763
      if (!mr.me.colRef.pColRef[j].hasRef) {
×
764
        continue;
×
765
      }
766

767
      pTb->refCols[refColsNum].colId = mr.me.colRef.pColRef[j].id;
×
768
      tstrncpy(pTb->refCols[refColsNum].refColName, mr.me.colRef.pColRef[j].refColName, TSDB_COL_NAME_LEN);
×
769
      tstrncpy(pTb->refCols[refColsNum].refTableName, mr.me.colRef.pColRef[j].refTableName, TSDB_TABLE_NAME_LEN);
×
770
      tstrncpy(pTb->refCols[refColsNum].refDbName, mr.me.colRef.pColRef[j].refDbName, TSDB_DB_NAME_LEN);
×
771

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

774
      if (NULL == tSimpleHashGet(pSrcTbls, tbFName, strlen(tbFName))) {
×
775
        QUERY_CHECK_CODE(tSimpleHashPut(pSrcTbls, tbFName, strlen(tbFName), &code, sizeof(code)), line, _return);
×
776
      }
777

778
      refColsNum++;
×
779
    }
780

781
    pTb->numOfSrcTbls = tSimpleHashGetSize(pSrcTbls);
×
782
    QUERY_CHECK_NULL(taosArrayPush(*ppRes, &pTb), code, line, _return, terrno);
×
783
    pTb = NULL;
×
784

785
    pHandle->api.metaReaderFn.clearReader(&mr);
×
786
    readerInit = false;
×
787
  }
788

789
_return:
×
790

791
  if (readerInit) {
×
792
    pHandle->api.metaReaderFn.clearReader(&mr);
×
793
  }
794

795
  taosArrayDestroy(pList);
×
796
  taosMemoryFree(pTb);
×
797
  tSimpleHashCleanup(pSrcTbls);
×
798

799
  if (code) {
×
800
    qError("%s failed since %s", __func__, tstrerror(code));
×
801
  }
802
  return code;
×
803
}
804

805
int32_t vnodeReadVStbRefDbs(SReadHandle* pHandle, int64_t suid, SArray** ppRes) {
4,633,395✔
806
  int32_t                    code = TSDB_CODE_SUCCESS;
4,633,395✔
807
  int32_t                    line = 0;
4,633,395✔
808
  SMetaReader                mr = {0};
4,633,395✔
809
  bool                       readerInit = false;
4,638,144✔
810
  SSHashObj*                 pDbNameHash = NULL;
4,638,144✔
811
  SArray*                    pList = NULL;
4,638,144✔
812

813
  pList = taosArrayInit(10, sizeof(uint64_t));
4,638,144✔
814
  QUERY_CHECK_NULL(pList, code, line, _return, terrno);
4,623,631✔
815

816
  *ppRes = taosArrayInit(10, POINTER_BYTES);
4,623,631✔
817
  QUERY_CHECK_NULL(*ppRes, code, line, _return, terrno)
4,635,324✔
818
  
819
  // lookup in cache
820
  code = pHandle->api.metaFn.metaGetCachedRefDbs(pHandle->vnode, suid, *ppRes);
4,632,612✔
821
  QUERY_CHECK_CODE(code, line, _return);
4,652,528✔
822

823
  if (taosArrayGetSize(*ppRes) > 0) {
4,652,528✔
824
    // found in cache
825
    goto _return;
4,545,598✔
826
  } else {
827
    code = pHandle->api.metaFn.getChildTableList(pHandle->vnode, suid, pList);
105,660✔
828
    QUERY_CHECK_CODE(code, line, _return);
105,660✔
829

830
    size_t num = taosArrayGetSize(pList);
105,660✔
831
    pDbNameHash = tSimpleHashInit(10, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
105,660✔
832
    QUERY_CHECK_NULL(pDbNameHash, code, line, _return, terrno);
105,660✔
833

834
    for (int32_t i = 0; i < num; ++i) {
550,627✔
835
      uint64_t* id = taosArrayGet(pList, i);
444,967✔
836
      QUERY_CHECK_NULL(id, code, line, _return, terrno);
444,967✔
837

838
      pHandle->api.metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pHandle->api.metaFn);
444,967✔
839
      readerInit = true;
444,967✔
840

841
      code = pHandle->api.metaReaderFn.getTableEntryByUid(&mr, *id);
444,967✔
842
      QUERY_CHECK_CODE(code, line, _return);
444,967✔
843

844
      for (int32_t j = 0; j < mr.me.colRef.nCols; j++) {
42,732,725✔
845
        if (mr.me.colRef.pColRef[j].hasRef) {
42,287,758✔
846
          if (NULL == tSimpleHashGet(pDbNameHash, mr.me.colRef.pColRef[j].refDbName, strlen(mr.me.colRef.pColRef[j].refDbName))) {
41,391,682✔
847
            char *refDbName = taosStrdup(mr.me.colRef.pColRef[j].refDbName);
90,561✔
848
            QUERY_CHECK_NULL(refDbName, code, line, _return, terrno);
90,561✔
849

850
            QUERY_CHECK_NULL(taosArrayPush(*ppRes, &refDbName), code, line, _return, terrno);
181,122✔
851

852
            code = tSimpleHashPut(pDbNameHash, refDbName, strlen(refDbName), NULL, 0);
90,561✔
853
            QUERY_CHECK_CODE(code, line, _return);
90,561✔
854
          }
855
        }
856
      }
857

858
      pHandle->api.metaReaderFn.clearReader(&mr);
444,967✔
859
      readerInit = false;
444,967✔
860
    }
861

862
    code = pHandle->api.metaFn.metaPutRefDbsToCache(pHandle->vnode, suid, *ppRes);
105,660✔
863
    QUERY_CHECK_CODE(code, line, _return);
105,660✔
864
  }
865

866
_return:
4,651,258✔
867

868
  if (readerInit) {
4,650,605✔
869
    pHandle->api.metaReaderFn.clearReader(&mr);
×
870
  }
871

872
  taosArrayDestroy(pList);
4,650,605✔
873
  tSimpleHashCleanup(pDbNameHash);
4,647,982✔
874

875
  if (code) {
4,647,811✔
876
    qError("%s failed since %s", __func__, tstrerror(code));
×
877
  }
878
  return code;
4,647,811✔
879
}
880

881
int32_t vnodeGetVSubtablesMeta(SVnode *pVnode, SRpcMsg *pMsg) {
×
882
  int32_t        code = 0;
×
883
  int32_t        rspSize = 0;
×
884
  SVSubTablesReq req = {0};
×
885
  SVSubTablesRsp rsp = {0};
×
886
  SRpcMsg      rspMsg = {0};
×
887
  void        *pRsp = NULL;
×
888
  int32_t      line = 0;
×
889

890
  if (tDeserializeSVSubTablesReq(pMsg->pCont, pMsg->contLen, &req)) {
×
891
    code = terrno;
×
892
    qError("tDeserializeSVSubTablesReq failed");
×
893
    goto _return;
×
894
  }
895

896
  SReadHandle handle = {0};
×
897
  handle.vnode = pVnode;
×
898
  initStorageAPI(&handle.api);
×
899

900
  QUERY_CHECK_CODE(vnodeReadVSubtables(&handle, req.suid, &rsp.pTables), line, _return);
×
901
  rsp.vgId = TD_VID(pVnode);
×
902

903
  rspSize = tSerializeSVSubTablesRsp(NULL, 0, &rsp);
×
904
  if (rspSize < 0) {
×
905
    code = rspSize;
×
906
    qError("tSerializeSVSubTablesRsp failed, error:%d", rspSize);
×
907
    goto _return;
×
908
  }
909
  pRsp = taosMemoryCalloc(1, rspSize);
×
910
  if (pRsp == NULL) {
×
911
    code = terrno;
×
912
    qError("rpcMallocCont %d failed, error:%d", rspSize, terrno);
×
913
    goto _return;
×
914
  }
915
  rspSize = tSerializeSVSubTablesRsp(pRsp, rspSize, &rsp);
×
916
  if (rspSize < 0) {
×
917
    code = rspSize;
×
918
    qError("tSerializeSVSubTablesRsp failed, error:%d", rspSize);
×
919
    goto _return;
×
920
  }
921

922
_return:
×
923

924
  rspMsg.info = pMsg->info;
×
925
  rspMsg.pCont = pRsp;
×
926
  rspMsg.contLen = rspSize;
×
927
  rspMsg.code = code;
×
928
  rspMsg.msgType = pMsg->msgType;
×
929

930
  if (code) {
×
931
    qError("vnd get virtual subtables failed cause of %s", tstrerror(code));
×
932
  }
933

934
  *pMsg = rspMsg;
×
935
  
936
  tDestroySVSubTablesRsp(&rsp);
×
937

938
  //tmsgSendRsp(&rspMsg);
939

940
  return code;
×
941
}
942

943
int32_t vnodeGetVStbRefDbs(SVnode *pVnode, SRpcMsg *pMsg) {
4,642,805✔
944
  int32_t        code = 0;
4,642,805✔
945
  int32_t        rspSize = 0;
4,642,805✔
946
  SVStbRefDbsReq req = {0};
4,642,805✔
947
  SVStbRefDbsRsp rsp = {0};
4,644,205✔
948
  SRpcMsg        rspMsg = {0};
4,617,308✔
949
  void          *pRsp = NULL;
4,617,308✔
950
  int32_t        line = 0;
4,617,308✔
951

952
  if (tDeserializeSVStbRefDbsReq(pMsg->pCont, pMsg->contLen, &req)) {
4,617,308✔
953
    code = terrno;
×
954
    qError("tDeserializeSVSubTablesReq failed");
×
955
    goto _return;
×
956
  }
957

958
  SReadHandle handle = {0};
4,640,218✔
959
  handle.vnode = pVnode;
4,642,971✔
960
  initStorageAPI(&handle.api);
4,642,971✔
961

962
  code = vnodeReadVStbRefDbs(&handle, req.suid, &rsp.pDbs);
4,620,966✔
963
  QUERY_CHECK_CODE(code, line, _return);
4,648,505✔
964
  rsp.vgId = TD_VID(pVnode);
4,648,505✔
965

966
  rspSize = tSerializeSVStbRefDbsRsp(NULL, 0, &rsp);
4,645,820✔
967
  if (rspSize < 0) {
4,639,623✔
968
    code = rspSize;
×
969
    qError("tSerializeSVStbRefDbsRsp failed, error:%d", rspSize);
×
970
    goto _return;
×
971
  }
972
  pRsp = taosMemoryCalloc(1, rspSize);
4,639,623✔
973
  if (pRsp == NULL) {
4,633,708✔
974
    code = terrno;
×
975
    qError("rpcMallocCont %d failed, error:%d", rspSize, terrno);
×
976
    goto _return;
×
977
  }
978
  rspSize = tSerializeSVStbRefDbsRsp(pRsp, rspSize, &rsp);
4,633,708✔
979
  if (rspSize < 0) {
4,641,024✔
980
    code = rspSize;
×
981
    qError("tSerializeSVStbRefDbsRsp failed, error:%d", rspSize);
×
982
    goto _return;
×
983
  }
984

985
_return:
4,641,024✔
986

987
  rspMsg.info = pMsg->info;
4,647,084✔
988
  rspMsg.pCont = pRsp;
4,648,525✔
989
  rspMsg.contLen = rspSize;
4,648,525✔
990
  rspMsg.code = code;
4,648,525✔
991
  rspMsg.msgType = pMsg->msgType;
4,648,525✔
992

993
  if (code) {
4,646,515✔
994
    qError("vnd get virtual stb ref db failed cause of %s", tstrerror(code));
×
995
  }
996

997
  *pMsg = rspMsg;
4,646,515✔
998

999
  tDestroySVStbRefDbsRsp(&rsp);
4,637,645✔
1000

1001
  return code;
4,644,430✔
1002
}
1003

1004
static int32_t vnodeGetCompStorage(SVnode *pVnode, int64_t *output) {
178,674,078✔
1005
  int32_t code = 0;
178,674,078✔
1006
#ifdef TD_ENTERPRISE
1007
  int32_t now = taosGetTimestampSec();
178,674,078✔
1008
  if (llabs(now - pVnode->config.vndStats.storageLastUpd) >= 30) {
178,674,078✔
1009
    pVnode->config.vndStats.storageLastUpd = now;
9,009,860✔
1010

1011
    SDbSizeStatisInfo info = {0};
9,009,860✔
1012
    if (0 == (code = vnodeGetDBSize(pVnode, &info))) {
9,009,860✔
1013
      int64_t compSize =
9,009,860✔
1014
          info.l1Size + info.l2Size + info.l3Size + info.cacheSize + info.walSize + info.metaSize + +info.ssSize;
9,009,860✔
1015
      if (compSize >= 0) {
9,009,860✔
1016
        pVnode->config.vndStats.compStorage = compSize;
9,009,860✔
1017
      } else {
1018
        vError("vnode get comp storage failed since compSize is negative:%" PRIi64, compSize);
×
1019
        code = TSDB_CODE_APP_ERROR;
×
1020
      }
1021
    } else {
1022
      vWarn("vnode get comp storage failed since %s", tstrerror(code));
×
1023
    }
1024
  }
1025
  if (output) *output = pVnode->config.vndStats.compStorage;
178,674,078✔
1026
#endif
1027
  return code;
178,674,078✔
1028
}
1029

1030
static void vnodeGetBufferInfo(SVnode *pVnode, int64_t *bufferSegmentUsed, int64_t *bufferSegmentSize) {
178,674,078✔
1031
  *bufferSegmentUsed = 0;
178,674,078✔
1032
  *bufferSegmentSize = 0;
178,674,078✔
1033
  if (pVnode) {
178,674,078✔
1034
    (void)taosThreadMutexLock(&pVnode->mutex);
178,674,078✔
1035

1036
    if (pVnode->inUse) {
178,674,078✔
1037
      *bufferSegmentUsed = pVnode->inUse->size;
178,658,129✔
1038
    }
1039
    *bufferSegmentSize = pVnode->config.szBuf / VNODE_BUFPOOL_SEGMENTS;
178,674,078✔
1040

1041
    (void)taosThreadMutexUnlock(&pVnode->mutex);
178,674,078✔
1042
  }
1043
}
178,674,078✔
1044

1045
int32_t vnodeGetLoad(SVnode *pVnode, SVnodeLoad *pLoad) {
178,674,078✔
1046
  SSyncState state = syncGetState(pVnode->sync);
178,674,078✔
1047
  pLoad->syncAppliedIndex = pVnode->state.applied;
178,674,078✔
1048
  syncGetCommitIndex(pVnode->sync, &pLoad->syncCommitIndex);
178,674,078✔
1049

1050
  pLoad->vgId = TD_VID(pVnode);
178,674,078✔
1051
  pLoad->syncState = state.state;
178,674,078✔
1052
  pLoad->syncRestore = state.restored;
178,674,078✔
1053
  pLoad->syncTerm = state.term;
178,674,078✔
1054
  pLoad->roleTimeMs = state.roleTimeMs;
178,674,078✔
1055
  pLoad->startTimeMs = state.startTimeMs;
178,674,078✔
1056
  pLoad->syncCanRead = state.canRead;
178,674,078✔
1057
  pLoad->cacheUsage = tsdbCacheGetUsage(pVnode);
178,674,078✔
1058
  pLoad->numOfCachedTables = tsdbCacheGetElems(pVnode);
178,674,078✔
1059
  VNODE_DO_META_QUERY(pVnode, pLoad->numOfTables = metaGetTbNum(pVnode->pMeta));
178,674,078✔
1060
  VNODE_DO_META_QUERY(pVnode, pLoad->numOfTimeSeries = metaGetTimeSeriesNum(pVnode->pMeta, 1));
178,674,078✔
1061
  pLoad->totalStorage = (int64_t)3 * 1073741824;  // TODO
178,674,078✔
1062
  (void)vnodeGetCompStorage(pVnode, &pLoad->compStorage);
178,674,078✔
1063
  pLoad->pointsWritten = 100;
178,674,078✔
1064
  pLoad->numOfSelectReqs = 1;
178,674,078✔
1065
  pLoad->numOfInsertReqs = atomic_load_64(&pVnode->statis.nInsert);
178,674,078✔
1066
  pLoad->numOfInsertSuccessReqs = atomic_load_64(&pVnode->statis.nInsertSuccess);
178,674,078✔
1067
  pLoad->numOfBatchInsertReqs = atomic_load_64(&pVnode->statis.nBatchInsert);
178,674,078✔
1068
  pLoad->numOfBatchInsertSuccessReqs = atomic_load_64(&pVnode->statis.nBatchInsertSuccess);
178,674,078✔
1069
  vnodeGetBufferInfo(pVnode, &pLoad->bufferSegmentUsed, &pLoad->bufferSegmentSize);
178,674,078✔
1070
  vDebug("vgId:%d, get vnode load, state:%s snapSeq:%d, learnerProgress:%d, totalIndex:%" PRId64, TD_VID(pVnode),
178,674,078✔
1071
         syncStr(state.state), state.snapSeq, state.progress, state.totalIndex);
1072
  pLoad->learnerProgress = state.progress;
178,674,078✔
1073
  pLoad->snapSeq = state.snapSeq;
178,674,078✔
1074
  pLoad->syncTotalIndex = state.totalIndex;
178,674,078✔
1075
  return 0;
178,674,078✔
1076
}
1077

1078
int32_t vnodeGetLoadLite(SVnode *pVnode, SVnodeLoadLite *pLoad) {
×
1079
  SSyncState syncState = syncGetState(pVnode->sync);
×
1080
  if (syncState.state == TAOS_SYNC_STATE_LEADER || syncState.state == TAOS_SYNC_STATE_ASSIGNED_LEADER) {
×
1081
    pLoad->vgId = TD_VID(pVnode);
×
1082
    pLoad->nTimeSeries = metaGetTimeSeriesNum(pVnode->pMeta, 1);
×
1083
    return 0;
×
1084
  }
1085
  return -1;
×
1086
}
1087
/**
1088
 * @brief Reset the statistics value by monitor interval
1089
 *
1090
 * @param pVnode
1091
 * @param pLoad
1092
 */
1093
void vnodeResetLoad(SVnode *pVnode, SVnodeLoad *pLoad) {
166✔
1094
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nInsert, pLoad->numOfInsertReqs, 64, "nInsert");
166✔
1095
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nInsertSuccess, pLoad->numOfInsertSuccessReqs, 64, "nInsertSuccess");
166✔
1096
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nBatchInsert, pLoad->numOfBatchInsertReqs, 64, "nBatchInsert");
166✔
1097
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nBatchInsertSuccess, pLoad->numOfBatchInsertSuccessReqs, 64,
166✔
1098
                            "nBatchInsertSuccess");
1099
}
166✔
1100

1101
void vnodeGetInfo(void *pVnode, const char **dbname, int32_t *vgId, int64_t *numOfTables, int64_t *numOfNormalTables) {
22,013,224✔
1102
  SVnode    *pVnodeObj = pVnode;
22,013,224✔
1103
  SVnodeCfg *pConf = &pVnodeObj->config;
22,013,224✔
1104

1105
  if (dbname) {
22,013,911✔
1106
    *dbname = pConf->dbname;
21,305,676✔
1107
  }
1108

1109
  if (vgId) {
22,014,015✔
1110
    *vgId = TD_VID(pVnodeObj);
20,891,576✔
1111
  }
1112

1113
  if (numOfTables) {
22,013,527✔
1114
    *numOfTables = pConf->vndStats.numOfNTables + pConf->vndStats.numOfCTables +
16,884✔
1115
                   pConf->vndStats.numOfVTables + pConf->vndStats.numOfVCTables;
8,442✔
1116
  }
1117

1118
  if (numOfNormalTables) {
22,013,527✔
1119
    *numOfNormalTables = pConf->vndStats.numOfNTables +
19,782✔
1120
                         pConf->vndStats.numOfVTables;
9,891✔
1121
  }
1122
}
22,013,527✔
1123

1124
int8_t vnodeGetSecurityLevel(void *pVnode) {
1,520,146✔
1125
  SVnode *pVnodeObj = pVnode;
1,520,146✔
1126
  return pVnodeObj->config.securityLevel;
1,520,146✔
1127
}
1128

1129
int32_t vnodeGetTableList(void *pVnode, int8_t type, SArray *pList) {
9,891✔
1130
  if (type == TSDB_SUPER_TABLE) {
9,891✔
1131
    return vnodeGetStbIdList(pVnode, 0, pList);
9,891✔
1132
  } else {
1133
    return TSDB_CODE_INVALID_PARA;
×
1134
  }
1135
}
1136

1137
int32_t vnodeGetAllTableList(SVnode *pVnode, uint64_t uid, SArray *list) {
×
1138
  int32_t      code = TSDB_CODE_SUCCESS;
×
1139
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, uid, 1);
×
1140
  if (NULL == pCur) {
×
1141
    qError("vnode get all table list failed");
×
1142
    return terrno;
×
1143
  }
1144

1145
  while (1) {
×
1146
    tb_uid_t id = metaCtbCursorNext(pCur);
×
1147
    if (id == 0) {
×
1148
      break;
×
1149
    }
1150

1151
    STableKeyInfo info = {uid = id};
×
1152
    if (NULL == taosArrayPush(list, &info)) {
×
1153
      qError("taosArrayPush failed");
×
1154
      code = terrno;
×
1155
      goto _exit;
×
1156
    }
1157
  }
1158
_exit:
×
1159
  metaCloseCtbCursor(pCur);
×
1160
  return code;
×
1161
}
1162

1163
int32_t vnodeGetCtbIdListByFilter(SVnode *pVnode, int64_t suid, SArray *list, bool (*filter)(void *arg), void *arg) {
×
1164
  return 0;
×
1165
}
1166

1167
int32_t vnodeGetCtbIdList(void *pVnode, int64_t suid, SArray *list) {
129,123,958✔
1168
  int32_t      code = TSDB_CODE_SUCCESS;
129,123,958✔
1169
  SVnode      *pVnodeObj = pVnode;
129,123,958✔
1170
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnodeObj, suid, 1);
129,123,958✔
1171
  if (NULL == pCur) {
129,082,539✔
1172
    qError("vnode get all table list failed");
×
1173
    return terrno;
×
1174
  }
1175

1176
  while (1) {
509,628,623✔
1177
    tb_uid_t id = metaCtbCursorNext(pCur);
638,711,162✔
1178
    if (id == 0) {
638,798,520✔
1179
      break;
129,152,932✔
1180
    }
1181
    qTrace("vnodeGetCtbIdList: got ctb id %" PRId64 " for suid %" PRId64, id, suid);
509,645,588✔
1182
    if (NULL == taosArrayPush(list, &id)) {
509,627,448✔
1183
      qError("taosArrayPush failed");
×
1184
      code = terrno;
×
1185
      goto _exit;
×
1186
    }
1187
  }
1188

1189
_exit:
129,099,168✔
1190
  metaCloseCtbCursor(pCur);
129,099,168✔
1191
  return code;
129,054,994✔
1192
}
1193

1194
int32_t vnodeGetStbIdList(SVnode *pVnode, int64_t suid, SArray *list) {
80,361,273✔
1195
  int32_t      code = TSDB_CODE_SUCCESS;
80,361,273✔
1196
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, suid);
80,361,273✔
1197
  if (!pCur) {
80,364,177✔
1198
    return TSDB_CODE_OUT_OF_MEMORY;
×
1199
  }
1200

1201
  while (1) {
63,236,825✔
1202
    tb_uid_t id = metaStbCursorNext(pCur);
143,601,002✔
1203
    if (id == 0) {
143,593,567✔
1204
      break;
80,365,494✔
1205
    }
1206

1207
    if (NULL == taosArrayPush(list, &id)) {
63,234,647✔
1208
      qError("taosArrayPush failed");
×
1209
      code = terrno;
×
1210
      goto _exit;
×
1211
    }
1212
  }
1213

1214
_exit:
80,358,275✔
1215
  metaCloseStbCursor(pCur);
80,358,275✔
1216
  return code;
80,355,714✔
1217
}
1218

1219
int32_t vnodeGetStbIdListByFilter(SVnode *pVnode, int64_t suid, SArray *list, bool (*filter)(void *arg, void *arg1),
7,080✔
1220
                                  void *arg) {
1221
  int32_t      code = TSDB_CODE_SUCCESS;
7,080✔
1222
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, suid);
7,080✔
1223
  if (!pCur) {
7,080✔
1224
    return terrno;
×
1225
  }
1226

1227
  while (1) {
8,654✔
1228
    tb_uid_t id = metaStbCursorNext(pCur);
15,734✔
1229
    if (id == 0) {
15,734✔
1230
      break;
7,080✔
1231
    }
1232

1233
    if ((*filter) && (*filter)(arg, &id)) {
8,654✔
1234
      continue;
8,654✔
1235
    }
1236

1237
    if (NULL == taosArrayPush(list, &id)) {
×
1238
      qError("taosArrayPush failed");
×
1239
      code = terrno;
×
1240
      goto _exit;
×
1241
    }
1242
  }
1243

1244
_exit:
7,080✔
1245
  metaCloseStbCursor(pCur);
7,080✔
1246
  return code;
7,080✔
1247
}
1248

1249
int32_t vnodeGetCtbNum(SVnode *pVnode, int64_t suid, int64_t *num) {
5,689,774✔
1250
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, suid, 0);
5,689,774✔
1251
  if (!pCur) {
5,691,766✔
1252
    return terrno;
×
1253
  }
1254

1255
  *num = 0;
5,691,766✔
1256
  while (1) {
3,246,504✔
1257
    tb_uid_t id = metaCtbCursorNext(pCur);
8,938,270✔
1258
    if (id == 0) {
8,937,111✔
1259
      break;
5,691,351✔
1260
    }
1261

1262
    ++(*num);
3,245,760✔
1263
  }
1264

1265
  metaCloseCtbCursor(pCur);
5,691,351✔
1266
  return TSDB_CODE_SUCCESS;
5,692,670✔
1267
}
1268

1269
int32_t vnodeGetStbColumnNum(SVnode *pVnode, tb_uid_t suid, int *num) {
5,691,974✔
1270
  SSchemaWrapper *pSW = metaGetTableSchema(pVnode->pMeta, suid, -1, 0, NULL, 0, false);
5,691,974✔
1271
  if (pSW) {
5,691,950✔
1272
    *num = pSW->nCols;
5,691,950✔
1273
    tDeleteSchemaWrapper(pSW);
1274
  } else {
1275
    *num = 2;
×
1276
  }
1277

1278
  return TSDB_CODE_SUCCESS;
5,691,215✔
1279
}
1280

1281
int32_t vnodeGetStbInfo(SVnode *pVnode, tb_uid_t suid, int64_t *keep, int8_t *flags) {
5,691,690✔
1282
  SMetaReader mr = {0};
5,691,690✔
1283
  metaReaderDoInit(&mr, pVnode->pMeta, META_READER_NOLOCK);
5,691,710✔
1284

1285
  int32_t code = metaReaderGetTableEntryByUid(&mr, suid);
5,690,554✔
1286
  if (code == TSDB_CODE_SUCCESS) {
5,691,703✔
1287
    if (keep) *keep = mr.me.stbEntry.keep;
5,691,703✔
1288
    if (flags) *flags = mr.me.flags;
5,691,703✔
1289
  } else {
1290
    if (keep) *keep = 0;
×
1291
    if (flags) *flags = 0;
×
1292
  }
1293

1294
  metaReaderClear(&mr);
5,691,703✔
1295
  return TSDB_CODE_SUCCESS;
5,691,703✔
1296
}
1297

1298
#ifdef TD_ENTERPRISE
1299
const char *tkLogStb[] = {"cluster_info",
1300
                          "data_dir",
1301
                          "disk_free",
1302
                          "disk_total",
1303
                          "dnodes_info",
1304
                          "d_info",
1305
                          "explorer_sys",
1306
                          "grants_info",
1307
                          "keeper_monitor",
1308
                          "logs",
1309
                          "log_dir",
1310
                          "log_summary",
1311
                          "http_client_requests",
1312
                          "http_server_requests_idmp",
1313
                          "m_info",
1314
                          "taosadapter_restful_http_request_fail",
1315
                          "taosadapter_restful_http_request_in_flight",
1316
                          "taosadapter_restful_http_request_summary_milliseconds",
1317
                          "taosadapter_restful_http_request_total",
1318
                          "taosadapter_system_cpu_percent",
1319
                          "taosadapter_system_mem_percent",
1320
                          "temp_dir",
1321
                          "vgroups_info",
1322
                          "vnodes_role",
1323
                          "taosd_dnodes_status",
1324
                          "adapter_conn_pool",
1325
                          "taosd_vnodes_info",
1326
                          "taosd_dnodes_metrics",
1327
                          "taosd_vgroups_info",
1328
                          "taos_sql_req",
1329
                          "taosd_mnodes_info",
1330
                          "adapter_c_interface",
1331
                          "taosd_cluster_info",
1332
                          "taosd_sql_req",
1333
                          "taosd_dnodes_info",
1334
                          "adapter_requests",
1335
                          "taosd_write_metrics",
1336
                          "adapter_status",
1337
                          "taos_slow_sql",
1338
                          "taos_slow_sql_detail",
1339
                          "taosd_cluster_basic",
1340
                          "taosd_dnodes_data_dirs",
1341
                          "taosd_dnodes_log_dirs",
1342
                          "xnode_agent_activities",
1343
                          "xnode_task_activities",
1344
                          "xnode_task_metrics",
1345
                          "taosx_sys",
1346
                          "taosx_task_csv",
1347
                          "taosx_task_progress",
1348
                          "taosx_task_kinghist",
1349
                          "taosx_task_tdengine2",
1350
                          "taosx_task_tdengine3",
1351
                          "taosx_task_opc_da",
1352
                          "taosx_task_opc_ua",
1353
                          "taosx_task_kafka",
1354
                          "taosx_task_influxdb",
1355
                          "taosx_task_mqtt",
1356
                          "taosx_task_avevahistorian",
1357
                          "taosx_task_opentsdb",
1358
                          "taosx_task_mysql",
1359
                          "taosx_task_postgres",
1360
                          "taosx_task_oracle",
1361
                          "taosx_task_mssql",
1362
                          "taosx_task_mongodb",
1363
                          "taosx_task_sparkplugb",
1364
                          "taosx_task_orc",
1365
                          "taosx_task_pulsar",
1366
                          "taosx_task_pspace"};
1367
const char *tkAuditStb[] = {"operations"};
1368
const int   tkLogStbNum = ARRAY_SIZE(tkLogStb);
1369
const int   tkAuditStbNum = ARRAY_SIZE(tkAuditStb);
1370

1371
// exclude stbs of taoskeeper log
1372
static int32_t vnodeGetTimeSeriesBlackList(SVnode *pVnode, int32_t *tbSize) {
80,358,905✔
1373
  int32_t      code = TSDB_CODE_SUCCESS;
80,358,905✔
1374
  int32_t      tbNum = 0;
80,358,905✔
1375
  const char **pTbArr = NULL;
80,358,905✔
1376
  const char  *dbName = NULL;
80,358,905✔
1377
  *tbSize = 0;
80,358,905✔
1378

1379
  if (!(dbName = strchr(pVnode->config.dbname, '.'))) return 0;
80,359,137✔
1380
  if (0 == strncmp(++dbName, "log", TSDB_DB_NAME_LEN)) {
80,359,957✔
1381
    tbNum = tkLogStbNum;
4,113✔
1382
    pTbArr = (const char **)&tkLogStb;
4,113✔
1383
  } else if (0 == strncmp(dbName, "audit", TSDB_DB_NAME_LEN) || pVnode->config.isAudit) {
80,358,654✔
1384
    tbNum = tkAuditStbNum;
6,913✔
1385
    pTbArr = (const char **)&tkAuditStb;
6,913✔
1386
  }
1387
  if (tbNum && pTbArr) {
80,362,767✔
1388
    *tbSize = metaSizeOfTbFilterCache(pVnode->pMeta, 0);
11,026✔
1389
    if (*tbSize < tbNum) {
11,026✔
1390
      for (int32_t i = 0; i < tbNum; ++i) {
109,411✔
1391
        tb_uid_t suid = metaGetTableEntryUidByName(pVnode->pMeta, pTbArr[i]);
107,552✔
1392
        if (suid != 0) {
107,552✔
1393
          code = metaPutTbToFilterCache(pVnode->pMeta, &suid, 0);
5,333✔
1394
          if (TSDB_CODE_SUCCESS != code) {
5,333✔
1395
            return code;
2,923✔
1396
          }
1397
        }
1398
      }
1399
      *tbSize = metaSizeOfTbFilterCache(pVnode->pMeta, 0);
1,859✔
1400
    }
1401
  }
1402

1403
  return code;
80,357,760✔
1404
}
1405
#endif
1406

1407
static bool vnodeTimeSeriesFilter(void *arg1, void *arg2) {
8,654✔
1408
  SVnode *pVnode = (SVnode *)arg1;
8,654✔
1409

1410
  if (metaTbInFilterCache(pVnode->pMeta, arg2, 0)) {
8,654✔
1411
    return true;
8,654✔
1412
  }
1413
  return false;
×
1414
}
1415

1416
int32_t vnodeGetTimeSeriesNum(SVnode *pVnode, int64_t *num) {
80,360,683✔
1417
  SArray *suidList = NULL;
80,360,683✔
1418

1419
  if (!(suidList = taosArrayInit(1, sizeof(tb_uid_t)))) {
80,360,683✔
1420
    return terrno;
×
1421
  }
1422

1423
  int32_t tbFilterSize = 0;
80,357,702✔
1424
  int32_t code = TSDB_CODE_SUCCESS;
80,350,442✔
1425
#ifdef TD_ENTERPRISE
1426
  code = vnodeGetTimeSeriesBlackList(pVnode, &tbFilterSize);
80,350,442✔
1427
  if (TSDB_CODE_SUCCESS != code) {
80,354,149✔
1428
    goto _exit;
2,923✔
1429
  }
1430
#endif
1431

1432
  if ((!tbFilterSize && vnodeGetStbIdList(pVnode, 0, suidList) < 0) ||
80,351,226✔
1433
      (tbFilterSize && vnodeGetStbIdListByFilter(pVnode, 0, suidList, vnodeTimeSeriesFilter, pVnode) < 0)) {
80,352,584✔
1434
    qError("vgId:%d, failed to get stb id list error: %s", TD_VID(pVnode), terrstr());
×
1435
    taosArrayDestroy(suidList);
×
1436
    return terrno;
×
1437
  }
1438

1439
  *num = 0;
80,346,393✔
1440
  int64_t arrSize = taosArrayGetSize(suidList);
80,356,940✔
1441
  for (int64_t i = 0; i < arrSize; ++i) {
143,576,599✔
1442
    tb_uid_t suid = *(tb_uid_t *)taosArrayGet(suidList, i);
63,213,851✔
1443

1444
    int64_t ctbNum = 0;
63,211,673✔
1445
    int32_t numOfCols = 0;
63,215,303✔
1446
    int8_t  flags = 0;
63,213,125✔
1447
    code = metaGetStbStats(pVnode, suid, &ctbNum, &numOfCols, &flags);
63,207,317✔
1448
    if (TSDB_CODE_SUCCESS != code) {
63,222,563✔
1449
      goto _exit;
×
1450
    }
1451
    if (!TABLE_IS_VIRTUAL(flags)) {
63,222,563✔
1452
      *num += ctbNum * (numOfCols - 1);
57,656,269✔
1453
    }
1454
  }
1455

1456
_exit:
80,355,532✔
1457
  taosArrayDestroy(suidList);
80,364,945✔
1458
  return TSDB_CODE_SUCCESS;
80,355,850✔
1459
}
1460

1461
int32_t vnodeGetAllCtbNum(SVnode *pVnode, int64_t *num) {
×
1462
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, 0);
×
1463
  if (!pCur) {
×
1464
    return terrno;
×
1465
  }
1466

1467
  *num = 0;
×
1468
  while (1) {
×
1469
    tb_uid_t id = metaStbCursorNext(pCur);
×
1470
    if (id == 0) {
×
1471
      break;
×
1472
    }
1473

1474
    int64_t ctbNum = 0;
×
1475
    int32_t code = vnodeGetCtbNum(pVnode, id, &ctbNum);
×
1476
    if (TSDB_CODE_SUCCESS != code) {
×
1477
      metaCloseStbCursor(pCur);
×
1478
      return code;
×
1479
    }
1480

1481
    *num += ctbNum;
×
1482
  }
1483

1484
  metaCloseStbCursor(pCur);
×
1485
  return TSDB_CODE_SUCCESS;
×
1486
}
1487

1488
void *vnodeGetIdx(void *pVnode) {
4,938,018✔
1489
  if (pVnode == NULL) {
4,938,018✔
1490
    return NULL;
×
1491
  }
1492

1493
  return metaGetIdx(((SVnode *)pVnode)->pMeta);
4,938,018✔
1494
}
1495

1496
void *vnodeGetIvtIdx(void *pVnode) {
4,937,587✔
1497
  if (pVnode == NULL) {
4,937,587✔
1498
    return NULL;
×
1499
  }
1500
  return metaGetIvtIdx(((SVnode *)pVnode)->pMeta);
4,937,587✔
1501
}
1502

1503
int32_t vnodeGetTableSchema(void *pVnode, int64_t uid, STSchema **pSchema, int64_t *suid, SSchemaWrapper **pTagSchema) {
529,025✔
1504
  return tsdbGetTableSchema(((SVnode *)pVnode)->pMeta, uid, pSchema, suid, pTagSchema);
529,025✔
1505
}
1506

1507
static FORCE_INLINE int32_t vnodeGetDBPrimaryInfo(SVnode *pVnode, SDbSizeStatisInfo *pInfo) {
1508
  int32_t code = 0;
9,017,496✔
1509
  char    path[TSDB_FILENAME_LEN] = {0};
9,017,496✔
1510

1511
  char   *dirName[] = {VNODE_TSDB_DIR, VNODE_WAL_DIR, VNODE_META_DIR, VNODE_TSDB_CACHE_DIR};
9,017,496✔
1512
  int64_t dirSize[4];
9,015,242✔
1513

1514
  vnodeGetPrimaryPath(pVnode, false, path, TSDB_FILENAME_LEN);
9,017,496✔
1515
  int32_t offset = strlen(path);
9,017,496✔
1516

1517
  for (int i = 0; i < sizeof(dirName) / sizeof(dirName[0]); i++) {
45,087,480✔
1518
    int64_t size = {0};
36,069,984✔
1519
    (void)snprintf(path + offset, TSDB_FILENAME_LEN - offset, "%s%s", TD_DIRSEP, dirName[i]);
36,069,984✔
1520
    code = taosGetDirSize(path, &size);
36,069,984✔
1521
    if (code != 0) {
36,069,984✔
1522
      uWarn("vnode %d get dir %s %s size failed since %s", TD_VID(pVnode), path, dirName[i], tstrerror(code));
×
1523
    }
1524
    path[offset] = 0;
36,069,984✔
1525
    dirSize[i] = size;
36,069,984✔
1526
  }
1527

1528
  pInfo->l1Size = 0;
9,017,496✔
1529
  pInfo->walSize = dirSize[1];
9,017,496✔
1530
  pInfo->metaSize = dirSize[2];
9,017,496✔
1531
  pInfo->cacheSize = dirSize[3];
9,017,496✔
1532
  return code;
9,017,496✔
1533
}
1534
int32_t vnodeGetDBSize(void *pVnode, SDbSizeStatisInfo *pInfo) {
9,017,496✔
1535
  int32_t code = 0;
9,017,496✔
1536
  int32_t lino = 0;
9,017,496✔
1537
  SVnode *pVnodeObj = pVnode;
9,017,496✔
1538
  if (pVnodeObj == NULL) {
9,017,496✔
1539
    return TSDB_CODE_VND_NOT_EXIST;
×
1540
  }
1541
  code = vnodeGetDBPrimaryInfo(pVnode, pInfo);
9,017,496✔
1542
  if (code != 0) goto _exit;
9,017,496✔
1543

1544
  code = tsdbGetFsSize(pVnodeObj->pTsdb, pInfo);
9,017,496✔
1545
_exit:
9,017,496✔
1546
  return code;
9,017,496✔
1547
}
1548

1549
/*
1550
 * Get raw write metrics for a vnode
1551
 */
1552
int32_t vnodeGetRawWriteMetrics(void *pVnode, SRawWriteMetrics *pRawMetrics) {
×
1553
  if (pVnode == NULL || pRawMetrics == NULL) {
×
1554
    return TSDB_CODE_INVALID_PARA;
×
1555
  }
1556

1557
  SVnode      *pVnode1 = (SVnode *)pVnode;
×
1558
  SSyncMetrics syncMetrics = syncGetMetrics(pVnode1->sync);
×
1559

1560
  // Copy values following SRawWriteMetrics structure order
1561
  pRawMetrics->total_requests = atomic_load_64(&pVnode1->writeMetrics.total_requests);
×
1562
  pRawMetrics->total_rows = atomic_load_64(&pVnode1->writeMetrics.total_rows);
×
1563
  pRawMetrics->total_bytes = atomic_load_64(&pVnode1->writeMetrics.total_bytes);
×
1564
  pRawMetrics->fetch_batch_meta_time = atomic_load_64(&pVnode1->writeMetrics.fetch_batch_meta_time);
×
1565
  pRawMetrics->fetch_batch_meta_count = atomic_load_64(&pVnode1->writeMetrics.fetch_batch_meta_count);
×
1566
  pRawMetrics->preprocess_time = atomic_load_64(&pVnode1->writeMetrics.preprocess_time);
×
1567
  pRawMetrics->wal_write_bytes = atomic_load_64(&syncMetrics.wal_write_bytes);
×
1568
  pRawMetrics->wal_write_time = atomic_load_64(&syncMetrics.wal_write_time);
×
1569
  pRawMetrics->apply_bytes = atomic_load_64(&pVnode1->writeMetrics.apply_bytes);
×
1570
  pRawMetrics->apply_time = atomic_load_64(&pVnode1->writeMetrics.apply_time);
×
1571
  pRawMetrics->commit_count = atomic_load_64(&pVnode1->writeMetrics.commit_count);
×
1572
  pRawMetrics->commit_time = atomic_load_64(&pVnode1->writeMetrics.commit_time);
×
1573
  pRawMetrics->memtable_wait_time = atomic_load_64(&pVnode1->writeMetrics.memtable_wait_time);
×
1574
  pRawMetrics->blocked_commit_count = atomic_load_64(&pVnode1->writeMetrics.blocked_commit_count);
×
1575
  pRawMetrics->blocked_commit_time = atomic_load_64(&pVnode1->writeMetrics.block_commit_time);
×
1576
  pRawMetrics->merge_count = atomic_load_64(&pVnode1->writeMetrics.merge_count);
×
1577
  pRawMetrics->merge_time = atomic_load_64(&pVnode1->writeMetrics.merge_time);
×
1578
  pRawMetrics->last_cache_commit_time = atomic_load_64(&pVnode1->writeMetrics.last_cache_commit_time);
×
1579
  pRawMetrics->last_cache_commit_count = atomic_load_64(&pVnode1->writeMetrics.last_cache_commit_count);
×
1580

1581
  return 0;
×
1582
}
1583

1584
/*
1585
 * Reset raw write metrics for a vnode by subtracting old values
1586
 */
1587
int32_t vnodeResetRawWriteMetrics(void *pVnode, const SRawWriteMetrics *pOldMetrics) {
×
1588
  if (pVnode == NULL || pOldMetrics == NULL) {
×
1589
    return TSDB_CODE_INVALID_PARA;
×
1590
  }
1591

1592
  SVnode *pVnode1 = (SVnode *)pVnode;
×
1593

1594
  // Reset vnode write metrics using atomic operations to subtract old values
1595
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.total_requests, pOldMetrics->total_requests);
×
1596
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.total_rows, pOldMetrics->total_rows);
×
1597
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.total_bytes, pOldMetrics->total_bytes);
×
1598

1599
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.fetch_batch_meta_time, pOldMetrics->fetch_batch_meta_time);
×
1600
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.fetch_batch_meta_count, pOldMetrics->fetch_batch_meta_count);
×
1601
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.preprocess_time, pOldMetrics->preprocess_time);
×
1602
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.apply_bytes, pOldMetrics->apply_bytes);
×
1603
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.apply_time, pOldMetrics->apply_time);
×
1604
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.commit_count, pOldMetrics->commit_count);
×
1605

1606
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.commit_time, pOldMetrics->commit_time);
×
1607
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.merge_time, pOldMetrics->merge_time);
×
1608

1609
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.memtable_wait_time, pOldMetrics->memtable_wait_time);
×
1610
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.blocked_commit_count, pOldMetrics->blocked_commit_count);
×
1611
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.block_commit_time, pOldMetrics->blocked_commit_time);
×
1612
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.merge_count, pOldMetrics->merge_count);
×
1613

1614
  // Reset new cache metrics
1615
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.last_cache_commit_time, pOldMetrics->last_cache_commit_time);
×
1616
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.last_cache_commit_count, pOldMetrics->last_cache_commit_count);
×
1617

1618
  // Reset sync metrics
1619
  SSyncMetrics syncMetrics = {
×
1620
      .wal_write_bytes = pOldMetrics->wal_write_bytes,
×
1621
      .wal_write_time = pOldMetrics->wal_write_time,
×
1622
  };
1623
  syncResetMetrics(pVnode1->sync, &syncMetrics);
×
1624

1625
  return 0;
×
1626
}
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