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

taosdata / TDengine / #5101

17 May 2026 01:15AM UTC coverage: 73.286% (+0.1%) from 73.166%
#5101

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)

281268 of 383795 relevant lines covered (73.29%)

132379931.91 hits per line

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

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

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

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

36
int32_t fillTableColCmpr(SMetaReader *reader, SSchemaExt *pExt, int32_t numOfCol) {
23,415,394 ✔
37
  int8_t tblType = reader->me.type;
23,415,394 ✔
38
  if (withColCompress(tblType)) {
23,415,394 ✔
39
    SColCmprWrapper *p = &(reader->me.colCmpr);
23,258,933 ✔
40
    if (numOfCol != p->nCols) {
23,258,933 ✔
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++) {
277,603,066 ✔
45
      SColCmpr *pCmpr = &p->pColCmpr[i];
254,344,791 ✔
46
      pExt[i].colId = pCmpr->id;
254,344,364 ✔
47
      pExt[i].compress = pCmpr->alg;
254,342,682 ✔
48
    }
49
  }
50
  return 0;
23,415,172 ✔
51
}
52

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

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

81
int32_t fillTableColRef(SMetaReader *reader, SColRef *pRef, int32_t numOfCol) {
399,850 ✔
82
  int8_t tblType = reader->me.type;
399,850 ✔
83
  if (hasRefCol(tblType)) {
399,850 ✔
84
    SColRefWrapper *p = &(reader->me.colRef);
399,850 ✔
85
    if (numOfCol != p->nCols) {
399,850 ✔
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,112,791 ✔
90
      SColRef *pColRef = &p->pColRef[i];
2,712,941 ✔
91
      pRef[i].hasRef = pColRef->hasRef;
2,712,941 ✔
92
      pRef[i].id = pColRef->id;
2,712,941 ✔
93
      if(pRef[i].hasRef) {
2,712,941 ✔
94
        tstrncpy(pRef[i].refDbName, pColRef->refDbName, TSDB_DB_NAME_LEN);
1,914,227 ✔
95
        tstrncpy(pRef[i].refTableName, pColRef->refTableName, TSDB_TABLE_NAME_LEN);
1,914,227 ✔
96
        tstrncpy(pRef[i].refColName, pColRef->refColName, TSDB_COL_NAME_LEN);
1,914,227 ✔
97
      }
98
    }
99
  }
100
  return 0;
399,850 ✔
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) {
35,249,285 ✔
126
  STableInfoReq  infoReq = {0};
35,249,285 ✔
127
  STableMetaRsp  metaRsp = {0};
35,249,285 ✔
128
  SMetaReader    mer1 = {0};
35,249,285 ✔
129
  SMetaReader    mer2 = {0};
35,249,285 ✔
130
  char           tableFName[TSDB_TABLE_FNAME_LEN];
35,246,403 ✔
131
  bool           reqTbUid = false;
35,249,285 ✔
132
  SRpcMsg        rpcMsg = {0};
35,249,285 ✔
133
  int32_t        code = 0;
35,249,285 ✔
134
  int32_t        rspLen = 0;
35,249,285 ✔
135
  void          *pRsp = NULL;
35,249,285 ✔
136
  SSchemaWrapper schema = {0};
35,249,285 ✔
137
  SSchemaWrapper schemaTag = {0};
35,249,285 ✔
138
  uint8_t        autoCreateCtb = 0;
35,249,285 ✔
139

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

147
  if (infoReq.option == REQ_OPT_TBUID) reqTbUid = true;
35,249,285 ✔
148
  metaRsp.dbId = pVnode->config.dbId;
35,249,285 ✔
149
  tstrncpy(metaRsp.tbName, infoReq.tbName, TSDB_TABLE_NAME_LEN);
35,249,285 ✔
150
  (void)memcpy(metaRsp.dbFName, infoReq.dbFName, sizeof(metaRsp.dbFName));
35,249,285 ✔
151

152
  if (!reqTbUid) {
35,249,285 ✔
153
    (void)snprintf(tableFName, TSDB_TABLE_FNAME_LEN, "%s.%s", infoReq.dbFName, infoReq.tbName);
34,968,849 ✔
154
    if (pVnode->mounted) tTrimMountPrefix(tableFName);
34,968,849 ✔
155
    code = vnodeValidateTableHash(pVnode, tableFName);
34,968,849 ✔
156
    if (code) {
34,967,998 ✔
157
      goto _exit4;
×
158
    }
159
  }
160

161
  // query meta
162
  metaReaderDoInit(&mer1, pVnode->pMeta, META_READER_LOCK);
35,248,434 ✔
163
  if (reqTbUid) {
35,248,858 ✔
164
    SET_ERRNO(0);
280,436 ✔
165
    uint64_t tbUid = taosStr2UInt64(infoReq.tbName, NULL, 10);
280,436 ✔
166
    if (ERRNO == ERANGE || tbUid == 0) {
280,436 ✔
167
      code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
168
      goto _exit3;
×
169
    }
170
    SMetaReader mr3 = {0};
280,436 ✔
171
    metaReaderDoInit(&mr3, ((SVnode *)pVnode)->pMeta, META_READER_NOLOCK);
280,436 ✔
172
    if ((code = metaReaderGetTableEntryByUid(&mr3, tbUid)) < 0) {
280,436 ✔
173
      metaReaderClear(&mr3);
101,880 ✔
174
      TAOS_CHECK_GOTO(code, NULL, _exit3);
101,880 ✔
175
    }
176
    tstrncpy(metaRsp.tbName, mr3.me.name, TSDB_TABLE_NAME_LEN);
178,328 ✔
177
    metaReaderClear(&mr3);
178,556 ✔
178
    TAOS_CHECK_GOTO(metaGetTableEntryByName(&mer1, metaRsp.tbName), NULL, _exit3);
178,556 ✔
179
  } else if (metaGetTableEntryByName(&mer1, infoReq.tbName) < 0) {
34,968,422 ✔
180
    code = terrno;
11,803,227 ✔
181
    goto _exit3;
11,802,803 ✔
182
  }
183

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

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

205
      tstrncpy(metaRsp.stbName, mer2.me.name, sizeof(metaRsp.stbName));
14,904,906 ✔
206
      metaRsp.suid = mer2.me.uid;
14,904,900 ✔
207
      metaRsp.ownerId = mer2.me.stbEntry.ownerId;  // child table inherits ownerId from stb
14,904,900 ✔
208
      metaRsp.secLvl = mer2.me.stbEntry.securityLevel;  // child table inherits secLvl from stb
14,904,900 ✔
209
      schema = mer2.me.stbEntry.schemaRow;
14,904,900 ✔
210
      schemaTag = mer2.me.stbEntry.schemaTag;
14,904,900 ✔
211
      break;
14,904,900 ✔
212
    }
213
    case TSDB_NORMAL_TABLE:
3,964,457 ✔
214
      metaRsp.secLvl = pVnode->config.securityLevel;  // normal table inherits secLvl from vnode config
3,964,457 ✔
215
    case TSDB_VIRTUAL_NORMAL_TABLE: {
4,116,302 ✔
216
      schema = mer1.me.ntbEntry.schemaRow;
4,116,302 ✔
217
      metaRsp.ownerId = mer1.me.ntbEntry.ownerId;
4,116,302 ✔
218
      break;
4,116,302 ✔
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,343,274 ✔
227
  metaRsp.numOfColumns = schema.nCols;
23,343,274 ✔
228
  metaRsp.precision = pVnode->config.tsdbCfg.precision;
23,343,274 ✔
229
  metaRsp.sversion = schema.version;
23,343,324 ✔
230
  metaRsp.tversion = schemaTag.version;
23,343,324 ✔
231
  metaRsp.pSchemas = (SSchema *)taosMemoryMalloc(sizeof(SSchema) * (metaRsp.numOfColumns + metaRsp.numOfTags));
23,343,324 ✔
232
  metaRsp.pSchemaExt = (SSchemaExt *)taosMemoryCalloc(metaRsp.numOfColumns, sizeof(SSchemaExt));
23,343,324 ✔
233
  if (NULL == metaRsp.pSchemas || NULL == metaRsp.pSchemaExt) {
23,342,887 ✔
234
    code = terrno;
×
235
    goto _exit;
×
236
  }
237
  (void)memcpy(metaRsp.pSchemas, schema.pSchema, sizeof(SSchema) * schema.nCols);
23,343,324 ✔
238
  if (schemaTag.nCols) {
23,343,324 ✔
239
    (void)memcpy(metaRsp.pSchemas + schema.nCols, schemaTag.pSchema, sizeof(SSchema) * schemaTag.nCols);
19,227,028 ✔
240
  }
241
  if (metaRsp.pSchemaExt) {
23,343,312 ✔
242
    SMetaReader *pReader =
23,343,330 ✔
243
        (mer1.me.type == TSDB_CHILD_TABLE || mer1.me.type == TSDB_VIRTUAL_CHILD_TABLE) ? &mer2 : &mer1;
23,343,330 ✔
244
    code = fillTableColCmpr(pReader, metaRsp.pSchemaExt, metaRsp.numOfColumns);
23,343,330 ✔
245
    if (code < 0) {
23,343,102 ✔
246
      goto _exit;
×
247
    }
248
    for (int32_t i = 0; i < metaRsp.numOfColumns; i++) {
278,691,104 ✔
249
      metaRsp.pSchemaExt[i].colId = schema.pSchema[i].colId;
255,348,709 ✔
250
      if (pReader->me.pExtSchemas) {
255,348,865 ✔
251
        metaRsp.pSchemaExt[i].typeMod = pReader->me.pExtSchemas[i].typeMod;
34,659,141 ✔
252
      }
253
    }
254
  } else {
255
    code = TSDB_CODE_OUT_OF_MEMORY;
×
256
    goto _exit;
×
257
  }
258
  if (hasRefCol(mer1.me.type)) {
23,342,395 ✔
259
    metaRsp.rversion = mer1.me.colRef.version;
399,641 ✔
260
    metaRsp.pColRefs = (SColRef*)taosMemoryMalloc(sizeof(SColRef) * metaRsp.numOfColumns);
399,641 ✔
261
    if (metaRsp.pColRefs) {
399,850 ✔
262
      code = fillTableColRef(&mer1, metaRsp.pColRefs, metaRsp.numOfColumns);
399,850 ✔
263
      if (code < 0) {
399,850 ✔
264
        goto _exit;
×
265
      }
266
    }
267
    metaRsp.numOfColRefs = metaRsp.numOfColumns;
399,850 ✔
268

269
    // Fill tag references
270
    if (mer1.me.colRef.nTagRefs > 0) {
399,850 ✔
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;
399,850 ✔
285
      metaRsp.numOfTagRefs = 0;
399,850 ✔
286
    }
287
  } else {
288
    metaRsp.pColRefs = NULL;
22,943,237 ✔
289
    metaRsp.numOfColRefs = 0;
22,943,237 ✔
290
    metaRsp.pTagRefs = NULL;
22,943,237 ✔
291
    metaRsp.numOfTagRefs = 0;
22,943,237 ✔
292
  }
293

294
  vnodeDebugTableMeta(&metaRsp);
23,343,087 ✔
295

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

303
  if (direct) {
23,343,330 ✔
304
    pRsp = rpcMallocCont(rspLen);
603,050 ✔
305
  } else {
306
    pRsp = taosMemoryCalloc(1, rspLen);
22,740,280 ✔
307
  }
308

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

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

320
_exit:
23,343,312 ✔
321
  taosMemoryFree(metaRsp.pColRefs);
23,343,330 ✔
322
  taosMemoryFree(metaRsp.pSchemas);
23,343,330 ✔
323
  taosMemoryFree(metaRsp.pSchemaExt);
23,342,732 ✔
324
  taosMemoryFree(metaRsp.pTagRefs);
23,342,732 ✔
325
_exit2:
23,343,330 ✔
326
  metaReaderClear(&mer2);
23,342,732 ✔
327
_exit3:
35,247,415 ✔
328
  metaReaderClear(&mer1);
35,248,263 ✔
329
_exit4:
35,248,859 ✔
330
  rpcMsg.info = pMsg->info;
35,248,435 ✔
331
  rpcMsg.pCont = pRsp;
35,248,435 ✔
332
  rpcMsg.contLen = rspLen;
35,248,435 ✔
333
  rpcMsg.code = code;
35,248,435 ✔
334
  rpcMsg.msgType = pMsg->msgType;
35,248,435 ✔
335

336
  if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST && autoCreateCtb == 1) {
35,249,285 ✔
337
    code = TSDB_CODE_SUCCESS;
6,621,174 ✔
338
  }
339

340
  if (code) {
35,249,285 ✔
341
    qError("vgId:%d, get table %s meta with %" PRIu8 " failed cause of %s", pVnode->config.vgId, infoReq.tbName,
5,284,357 ✔
342
           infoReq.option, tstrerror(code));
343
  }
344

345
  if (direct) {
35,249,285 ✔
346
    tmsgSendRsp(&rpcMsg);
1,454,592 ✔
347
  } else {
348
    *pMsg = rpcMsg;
33,794,693 ✔
349
  }
350

351
  return code;
35,249,285 ✔
352
}
353

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

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

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

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

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

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

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

395
  if (mer1.me.type == TSDB_SUPER_TABLE) {
72,070 ✔
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) {
72,070 ✔
399
    metaReaderDoInit(&mer2, pVnode->pMeta, META_READER_NOLOCK);
50,530 ✔
400
    if (metaReaderGetTableEntryByUid(&mer2, mer1.me.ctbEntry.suid) < 0) goto _exit;
50,530 ✔
401

402
    tstrncpy(cfgRsp.stbName, mer2.me.name, TSDB_TABLE_NAME_LEN);
50,530 ✔
403
    schema = mer2.me.stbEntry.schemaRow;
50,530 ✔
404
    schemaTag = mer2.me.stbEntry.schemaTag;
50,530 ✔
405
    cfgRsp.ownerId = mer2.me.stbEntry.ownerId;        // child table inherits ownerId from stb
50,530 ✔
406
    cfgRsp.securityLevel = mer2.me.stbEntry.securityLevel;  // child table inherits secLvl from stb
50,530 ✔
407
    cfgRsp.ttl = mer1.me.ctbEntry.ttlDays;
50,530 ✔
408
    cfgRsp.commentLen = mer1.me.ctbEntry.commentLen;
50,530 ✔
409
    if (mer1.me.ctbEntry.commentLen > 0) {
50,530 ✔
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;
50,530 ✔
417
    cfgRsp.tagsLen = pTag->len;
50,530 ✔
418
    cfgRsp.pTags = taosMemoryMalloc(cfgRsp.tagsLen);
50,530 ✔
419
    if (NULL == cfgRsp.pTags) {
50,530 ✔
420
      code = terrno;
×
421
      goto _exit;
×
422
    }
423
    (void)memcpy(cfgRsp.pTags, pTag, cfgRsp.tagsLen);
50,530 ✔
424
  } else if (mer1.me.type == TSDB_NORMAL_TABLE || mer1.me.type == TSDB_VIRTUAL_NORMAL_TABLE) {
21,540 ✔
425
    schema = mer1.me.ntbEntry.schemaRow;
21,540 ✔
426
    cfgRsp.ttl = mer1.me.ntbEntry.ttlDays;
21,540 ✔
427
    cfgRsp.ownerId = mer1.me.ntbEntry.ownerId;
21,540 ✔
428
    cfgRsp.securityLevel = mer1.me.type == TSDB_NORMAL_TABLE ? pVnode->config.securityLevel : 0;
21,540 ✔
429
    cfgRsp.commentLen = mer1.me.ntbEntry.commentLen;
21,540 ✔
430
    if (mer1.me.ntbEntry.commentLen > 0) {
21,540 ✔
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;
72,070 ✔
444
  cfgRsp.numOfColumns = schema.nCols;
72,070 ✔
445
  cfgRsp.virtualStb = false; // vnode don't have super table, so it's always false
72,070 ✔
446
  cfgRsp.pSchemas = (SSchema *)taosMemoryMalloc(sizeof(SSchema) * (cfgRsp.numOfColumns + cfgRsp.numOfTags));
72,070 ✔
447
  cfgRsp.pSchemaExt = (SSchemaExt *)taosMemoryCalloc(cfgRsp.numOfColumns, sizeof(SSchemaExt));
72,070 ✔
448
  cfgRsp.pColRefs = (SColRef *)taosMemoryMalloc(sizeof(SColRef) * cfgRsp.numOfColumns);
72,070 ✔
449
  cfgRsp.numOfTagRefs = 0;
72,070 ✔
450
  cfgRsp.pTagRefs = NULL;
72,070 ✔
451

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

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

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

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

479
  cfgRsp.virtualStb = false;
72,070 ✔
480
  if (hasRefCol(cfgRsp.tableType)) {
72,070 ✔
481
    for (int32_t i = 0; i < cfgRsp.numOfColumns; i++) {
130,438 ✔
482
      SColRef *pRef = &pColRef->pColRef[i];
119,815 ✔
483
      cfgRsp.pColRefs[i].hasRef = pRef->hasRef;
119,815 ✔
484
      cfgRsp.pColRefs[i].id = pRef->id;
119,815 ✔
485
      if (cfgRsp.pColRefs[i].hasRef) {
119,815 ✔
486
        tstrncpy(cfgRsp.pColRefs[i].refDbName, pRef->refDbName, TSDB_DB_NAME_LEN);
64,398 ✔
487
        tstrncpy(cfgRsp.pColRefs[i].refTableName, pRef->refTableName, TSDB_TABLE_NAME_LEN);
64,398 ✔
488
        tstrncpy(cfgRsp.pColRefs[i].refColName, pRef->refColName, TSDB_COL_NAME_LEN);
64,398 ✔
489
      }
490
    }
491

492
    cfgRsp.numOfTagRefs = pColRef->nTagRefs;
10,623 ✔
493
    if (cfgRsp.numOfTagRefs > 0) {
10,623 ✔
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);
72,070 ✔
519
  if (rspLen < 0) {
72,070 ✔
520
    code = terrno;
×
521
    goto _exit;
×
522
  }
523

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

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

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

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

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

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

558
  tFreeSTableCfgRsp(&cfgRsp);
72,070 ✔
559
  metaReaderClear(&mer2);
72,070 ✔
560
  metaReaderClear(&mer1);
72,070 ✔
561
  return code;
72,070 ✔
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) {
31,861,748 ✔
574
  int32_t      code = 0;
31,861,748 ✔
575
  int32_t      rspSize = 0;
31,861,748 ✔
576
  SBatchReq    batchReq = {0};
31,861,748 ✔
577
  SBatchMsg   *req = NULL;
31,862,995 ✔
578
  SBatchRspMsg rsp = {0};
31,862,995 ✔
579
  SBatchRsp    batchRsp = {0};
31,852,744 ✔
580
  SRpcMsg      reqMsg = *pMsg;
31,852,093 ✔
581
  SRpcMsg      rspMsg = {0};
31,852,093 ✔
582
  void        *pRsp = NULL;
31,859,622 ✔
583

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

590
  int32_t msgNum = taosArrayGetSize(batchReq.pMsgs);
31,862,406 ✔
591
  if (msgNum >= MAX_META_MSG_IN_BATCH) {
31,859,033 ✔
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));
31,859,033 ✔
598
  if (NULL == batchRsp.pRsps) {
31,856,351 ✔
599
    code = terrno;
×
600
    qError("taosArrayInit %d SBatchRspMsg failed", msgNum);
×
601
    goto _exit;
×
602
  }
603

604
  for (int32_t i = 0; i < msgNum; ++i) {
70,361,215 ✔
605
    req = taosArrayGet(batchReq.pMsgs, i);
38,502,099 ✔
606
    if (req == NULL) {
38,497,965 ✔
607
      code = terrno;
×
608
      goto _exit;
×
609
    }
610

611
    reqMsg.msgType = req->msgType;
38,497,965 ✔
612
    reqMsg.pCont = req->msg;
38,497,270 ✔
613
    reqMsg.contLen = req->msgLen;
38,497,314 ✔
614

615
    switch (req->msgType) {
38,494,592 ✔
616
      case TDMT_VND_TABLE_META:
33,514,257 ✔
617
        // error code has been set into reqMsg, no need to handle it here.
618
        if (TSDB_CODE_SUCCESS != vnodeGetTableMeta(pVnode, &reqMsg, false)) {
33,514,257 ✔
619
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
4,331,359 ✔
620
        }
621
        break;
33,514,257 ✔
622
      case TDMT_VND_TABLE_NAME:
280,436 ✔
623
        // error code has been set into reqMsg, no need to handle it here.
624
        if (TSDB_CODE_SUCCESS != vnodeGetTableMeta(pVnode, &reqMsg, false)) {
280,436 ✔
625
          qWarn("vnodeGetBatchName failed, msgType:%d", req->msgType);
101,880 ✔
626
        }
627
        break;
280,436 ✔
628
      case TDMT_VND_TABLE_CFG:
72,070 ✔
629
        // error code has been set into reqMsg, no need to handle it here.
630
        if (TSDB_CODE_SUCCESS != vnodeGetTableCfg(pVnode, &reqMsg, false)) {
72,070 ✔
631
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
×
632
        }
633
        break;
72,070 ✔
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,634,641 ✔
641
        // error code has been set into reqMsg, no need to handle it here.
642
        if (TSDB_CODE_SUCCESS != vnodeGetVStbRefDbs(pVnode, &reqMsg)) {
4,634,641 ✔
643
          qWarn("vnodeGetVStbRefDbs failed, msgType:%d", req->msgType);
×
644
        }
645
        break;
4,629,157 ✔
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;
38,495,920 ✔
655
    rsp.reqType = reqMsg.msgType;
38,499,315 ✔
656
    rsp.msgLen = reqMsg.contLen;
38,499,315 ✔
657
    rsp.rspCode = reqMsg.code;
38,499,315 ✔
658
    rsp.msg = reqMsg.pCont;
38,499,315 ✔
659

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

667
  rspSize = tSerializeSBatchRsp(NULL, 0, &batchRsp);
31,859,116 ✔
668
  if (rspSize < 0) {
31,854,262 ✔
669
    qError("tSerializeSBatchRsp failed");
×
670
    code = terrno;
×
671
    goto _exit;
×
672
  }
673
  pRsp = rpcMallocCont(rspSize);
31,854,262 ✔
674
  if (pRsp == NULL) {
31,850,930 ✔
675
    qError("rpcMallocCont %d failed", rspSize);
×
676
    code = terrno;
×
677
    goto _exit;
×
678
  }
679
  if (tSerializeSBatchRsp(pRsp, rspSize, &batchRsp) < 0) {
31,850,930 ✔
680
    qError("tSerializeSBatchRsp %d failed", rspSize);
×
681
    code = terrno;
×
682
    goto _exit;
×
683
  }
684

685
_exit:
31,850,759 ✔
686

687
  rspMsg.info = pMsg->info;
31,855,527 ✔
688
  rspMsg.pCont = pRsp;
31,852,112 ✔
689
  rspMsg.contLen = rspSize;
31,852,112 ✔
690
  rspMsg.code = code;
31,852,112 ✔
691
  rspMsg.msgType = pMsg->msgType;
31,852,112 ✔
692

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

697
  taosArrayDestroyEx(batchReq.pMsgs, tFreeSBatchReqMsg);
31,851,786 ✔
698
  taosArrayDestroyEx(batchRsp.pRsps, tFreeSBatchRspMsg);
31,846,843 ✔
699

700
  tmsgSendRsp(&rspMsg);
31,845,613 ✔
701

702
  return code;
31,860,994 ✔
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,629,799 ✔
806
  int32_t                    code = TSDB_CODE_SUCCESS;
4,629,799 ✔
807
  int32_t                    line = 0;
4,629,799 ✔
808
  SMetaReader                mr = {0};
4,629,799 ✔
809
  bool                       readerInit = false;
4,629,104 ✔
810
  SSHashObj*                 pDbNameHash = NULL;
4,629,104 ✔
811
  SArray*                    pList = NULL;
4,629,104 ✔
812

813
  pList = taosArrayInit(10, sizeof(uint64_t));
4,629,104 ✔
814
  QUERY_CHECK_NULL(pList, code, line, _return, terrno);
4,625,833 ✔
815

816
  *ppRes = taosArrayInit(10, POINTER_BYTES);
4,625,833 ✔
817
  QUERY_CHECK_NULL(*ppRes, code, line, _return, terrno)
4,625,836 ✔
818
  
819
  // lookup in cache
820
  code = pHandle->api.metaFn.metaGetCachedRefDbs(pHandle->vnode, suid, *ppRes);
4,625,141 ✔
821
  QUERY_CHECK_CODE(code, line, _return);
4,637,315 ✔
822

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

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

834
    for (int32_t i = 0; i < num; ++i) {
547,180 ✔
835
      uint64_t* id = taosArrayGet(pList, i);
442,161 ✔
836
      QUERY_CHECK_NULL(id, code, line, _return, terrno);
442,161 ✔
837

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

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

844
      for (int32_t j = 0; j < mr.me.colRef.nCols; j++) {
43,767,378 ✔
845
        if (mr.me.colRef.pColRef[j].hasRef) {
43,325,217 ✔
846
          if (NULL == tSimpleHashGet(pDbNameHash, mr.me.colRef.pColRef[j].refDbName, strlen(mr.me.colRef.pColRef[j].refDbName))) {
42,433,942 ✔
847
            char *refDbName = taosStrdup(mr.me.colRef.pColRef[j].refDbName);
90,072 ✔
848
            QUERY_CHECK_NULL(refDbName, code, line, _return, terrno);
90,072 ✔
849

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

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

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

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

866
_return:
4,636,656 ✔
867

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

872
  taosArrayDestroy(pList);
4,636,642 ✔
873
  tSimpleHashCleanup(pDbNameHash);
4,634,603 ✔
874

875
  if (code) {
4,635,244 ✔
876
    qError("%s failed since %s", __func__, tstrerror(code));
×
877
  }
878
  return code;
4,635,244 ✔
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,634,641 ✔
944
  int32_t        code = 0;
4,634,641 ✔
945
  int32_t        rspSize = 0;
4,634,641 ✔
946
  SVStbRefDbsReq req = {0};
4,634,641 ✔
947
  SVStbRefDbsRsp rsp = {0};
4,634,641 ✔
948
  SRpcMsg        rspMsg = {0};
4,620,298 ✔
949
  void          *pRsp = NULL;
4,620,298 ✔
950
  int32_t        line = 0;
4,620,298 ✔
951

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

958
  SReadHandle handle = {0};
4,630,537 ✔
959
  handle.vnode = pVnode;
4,631,905 ✔
960
  initStorageAPI(&handle.api);
4,631,905 ✔
961

962
  code = vnodeReadVStbRefDbs(&handle, req.suid, &rsp.pDbs);
4,625,816 ✔
963
  QUERY_CHECK_CODE(code, line, _return);
4,636,660 ✔
964
  rsp.vgId = TD_VID(pVnode);
4,636,660 ✔
965

966
  rspSize = tSerializeSVStbRefDbsRsp(NULL, 0, &rsp);
4,635,284 ✔
967
  if (rspSize < 0) {
4,628,489 ✔
968
    code = rspSize;
×
969
    qError("tSerializeSVStbRefDbsRsp failed, error:%d", rspSize);
×
970
    goto _return;
×
971
  }
972
  pRsp = taosMemoryCalloc(1, rspSize);
4,628,489 ✔
973
  if (pRsp == NULL) {
4,629,908 ✔
974
    code = terrno;
×
975
    qError("rpcMallocCont %d failed, error:%d", rspSize, terrno);
×
976
    goto _return;
×
977
  }
978
  rspSize = tSerializeSVStbRefDbsRsp(pRsp, rspSize, &rsp);
4,629,908 ✔
979
  if (rspSize < 0) {
4,631,879 ✔
980
    code = rspSize;
×
981
    qError("tSerializeSVStbRefDbsRsp failed, error:%d", rspSize);
×
982
    goto _return;
×
983
  }
984

985
_return:
4,631,879 ✔
986

987
  rspMsg.info = pMsg->info;
4,633,920 ✔
988
  rspMsg.pCont = pRsp;
4,630,569 ✔
989
  rspMsg.contLen = rspSize;
4,630,569 ✔
990
  rspMsg.code = code;
4,630,569 ✔
991
  rspMsg.msgType = pMsg->msgType;
4,630,569 ✔
992

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

997
  *pMsg = rspMsg;
4,633,229 ✔
998

999
  tDestroySVStbRefDbsRsp(&rsp);
4,624,451 ✔
1000

1001
  return code;
4,632,538 ✔
1002
}
1003

1004
static int32_t vnodeGetCompStorage(SVnode *pVnode, int64_t *output) {
189,434,968 ✔
1005
  int32_t code = 0;
189,434,968 ✔
1006
#ifdef TD_ENTERPRISE
1007
  int32_t now = taosGetTimestampSec();
189,434,968 ✔
1008
  if (llabs(now - pVnode->config.vndStats.storageLastUpd) >= 30) {
189,434,968 ✔
1009
    pVnode->config.vndStats.storageLastUpd = now;
9,354,988 ✔
1010

1011
    SDbSizeStatisInfo info = {0};
9,354,988 ✔
1012
    if (0 == (code = vnodeGetDBSize(pVnode, &info))) {
9,354,988 ✔
1013
      int64_t compSize =
9,354,988 ✔
1014
          info.l1Size + info.l2Size + info.l3Size + info.cacheSize + info.walSize + info.metaSize + +info.ssSize;
9,354,988 ✔
1015
      if (compSize >= 0) {
9,354,988 ✔
1016
        pVnode->config.vndStats.compStorage = compSize;
9,354,988 ✔
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;
189,434,968 ✔
1026
#endif
1027
  return code;
189,434,968 ✔
1028
}
1029

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

1036
    if (pVnode->inUse) {
189,434,968 ✔
1037
      *bufferSegmentUsed = pVnode->inUse->size;
189,423,873 ✔
1038
    }
1039
    *bufferSegmentSize = pVnode->config.szBuf / VNODE_BUFPOOL_SEGMENTS;
189,434,968 ✔
1040

1041
    (void)taosThreadMutexUnlock(&pVnode->mutex);
189,434,968 ✔
1042
  }
1043
}
189,434,968 ✔
1044

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

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

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

1105
  if (dbname) {
22,302,972 ✔
1106
    *dbname = pConf->dbname;
21,598,560 ✔
1107
  }
1108

1109
  if (vgId) {
22,303,622 ✔
1110
    *vgId = TD_VID(pVnodeObj);
21,159,260 ✔
1111
  }
1112

1113
  if (numOfTables) {
22,303,623 ✔
1114
    *numOfTables = pConf->vndStats.numOfNTables + pConf->vndStats.numOfCTables +
17,056 ✔
1115
                   pConf->vndStats.numOfVTables + pConf->vndStats.numOfVCTables;
8,528 ✔
1116
  }
1117

1118
  if (numOfNormalTables) {
22,303,623 ✔
1119
    *numOfNormalTables = pConf->vndStats.numOfNTables +
20,166 ✔
1120
                         pConf->vndStats.numOfVTables;
10,083 ✔
1121
  }
1122
}
22,303,623 ✔
1123

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

1129
int32_t vnodeGetTableList(void *pVnode, int8_t type, SArray *pList) {
10,083 ✔
1130
  if (type == TSDB_SUPER_TABLE) {
10,083 ✔
1131
    return vnodeGetStbIdList(pVnode, 0, pList);
10,083 ✔
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) {
120,923,878 ✔
1168
  int32_t      code = TSDB_CODE_SUCCESS;
120,923,878 ✔
1169
  SVnode      *pVnodeObj = pVnode;
120,923,878 ✔
1170
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnodeObj, suid, 1);
120,923,878 ✔
1171
  if (NULL == pCur) {
120,911,823 ✔
1172
    qError("vnode get all table list failed");
×
1173
    return terrno;
×
1174
  }
1175

1176
  while (1) {
513,930,867 ✔
1177
    tb_uid_t id = metaCtbCursorNext(pCur);
634,842,690 ✔
1178
    if (id == 0) {
634,878,489 ✔
1179
      break;
120,945,275 ✔
1180
    }
1181
    qTrace("vnodeGetCtbIdList: got ctb id %" PRId64 " for suid %" PRId64, id, suid);
513,933,214 ✔
1182
    if (NULL == taosArrayPush(list, &id)) {
513,934,758 ✔
1183
      qError("taosArrayPush failed");
×
1184
      code = terrno;
×
1185
      goto _exit;
×
1186
    }
1187
  }
1188

1189
_exit:
120,773,194 ✔
1190
  metaCloseCtbCursor(pCur);
120,773,194 ✔
1191
  return code;
120,878,264 ✔
1192
}
1193

1194
int32_t vnodeGetStbIdList(SVnode *pVnode, int64_t suid, SArray *list) {
83,103,833 ✔
1195
  int32_t      code = TSDB_CODE_SUCCESS;
83,103,833 ✔
1196
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, suid);
83,103,833 ✔
1197
  if (!pCur) {
83,104,560 ✔
1198
    return TSDB_CODE_OUT_OF_MEMORY;
×
1199
  }
1200

1201
  while (1) {
66,384,771 ✔
1202
    tb_uid_t id = metaStbCursorNext(pCur);
149,489,331 ✔
1203
    if (id == 0) {
149,487,115 ✔
1204
      break;
83,105,979 ✔
1205
    }
1206

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

1214
_exit:
83,104,560 ✔
1215
  metaCloseStbCursor(pCur);
83,104,560 ✔
1216
  return code;
83,102,379 ✔
1217
}
1218

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

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

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

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

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

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

1255
  *num = 0;
5,478,892 ✔
1256
  while (1) {
3,050,496 ✔
1257
    tb_uid_t id = metaCtbCursorNext(pCur);
8,529,388 ✔
1258
    if (id == 0) {
8,529,388 ✔
1259
      break;
5,478,892 ✔
1260
    }
1261

1262
    ++(*num);
3,050,496 ✔
1263
  }
1264

1265
  metaCloseCtbCursor(pCur);
5,478,892 ✔
1266
  return TSDB_CODE_SUCCESS;
5,478,876 ✔
1267
}
1268

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

1278
  return TSDB_CODE_SUCCESS;
5,478,889 ✔
1279
}
1280

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

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

1294
  metaReaderClear(&mr);
5,478,892 ✔
1295
  return TSDB_CODE_SUCCESS;
5,478,892 ✔
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) {
83,104,635 ✔
1373
  int32_t      code = TSDB_CODE_SUCCESS;
83,104,635 ✔
1374
  int32_t      tbNum = 0;
83,104,635 ✔
1375
  const char **pTbArr = NULL;
83,104,635 ✔
1376
  const char  *dbName = NULL;
83,104,635 ✔
1377
  *tbSize = 0;
83,104,635 ✔
1378

1379
  if (!(dbName = strchr(pVnode->config.dbname, '.'))) return 0;
83,104,635 ✔
1380
  if (0 == strncmp(++dbName, "log", TSDB_DB_NAME_LEN)) {
83,104,635 ✔
1381
    tbNum = tkLogStbNum;
6,931 ✔
1382
    pTbArr = (const char **)&tkLogStb;
6,931 ✔
1383
  } else if (0 == strncmp(dbName, "audit", TSDB_DB_NAME_LEN) || pVnode->config.isAudit) {
83,098,431 ✔
1384
    tbNum = tkAuditStbNum;
6,982 ✔
1385
    pTbArr = (const char **)&tkAuditStb;
6,982 ✔
1386
  }
1387
  if (tbNum && pTbArr) {
83,106,089 ✔
1388
    *tbSize = metaSizeOfTbFilterCache(pVnode->pMeta, 0);
13,913 ✔
1389
    if (*tbSize < tbNum) {
13,913 ✔
1390
      for (int32_t i = 0; i < tbNum; ++i) {
220,129 ✔
1391
        tb_uid_t suid = metaGetTableEntryUidByName(pVnode->pMeta, pTbArr[i]);
216,952 ✔
1392
        if (suid != 0) {
216,952 ✔
1393
          code = metaPutTbToFilterCache(pVnode->pMeta, &suid, 0);
6,954 ✔
1394
          if (TSDB_CODE_SUCCESS != code) {
6,954 ✔
1395
            return code;
4,517 ✔
1396
          }
1397
        }
1398
      }
1399
      *tbSize = metaSizeOfTbFilterCache(pVnode->pMeta, 0);
3,177 ✔
1400
    }
1401
  }
1402

1403
  return code;
83,100,845 ✔
1404
}
1405
#endif
1406

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

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

1416
int32_t vnodeGetTimeSeriesNum(SVnode *pVnode, int64_t *num) {
83,104,635 ✔
1417
  SArray *suidList = NULL;
83,104,635 ✔
1418

1419
  if (!(suidList = taosArrayInit(1, sizeof(tb_uid_t)))) {
83,104,635 ✔
1420
    return terrno;
×
1421
  }
1422

1423
  int32_t tbFilterSize = 0;
83,106,816 ✔
1424
  int32_t code = TSDB_CODE_SUCCESS;
83,103,181 ✔
1425
#ifdef TD_ENTERPRISE
1426
  code = vnodeGetTimeSeriesBlackList(pVnode, &tbFilterSize);
83,103,181 ✔
1427
  if (TSDB_CODE_SUCCESS != code) {
83,106,089 ✔
1428
    goto _exit;
4,517 ✔
1429
  }
1430
#endif
1431

1432
  if ((!tbFilterSize && vnodeGetStbIdList(pVnode, 0, suidList) < 0) ||
83,101,572 ✔
1433
      (tbFilterSize && vnodeGetStbIdListByFilter(pVnode, 0, suidList, vnodeTimeSeriesFilter, pVnode) < 0)) {
83,097,210 ✔
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;
83,100,845 ✔
1440
  int64_t arrSize = taosArrayGetSize(suidList);
83,100,118 ✔
1441
  for (int64_t i = 0; i < arrSize; ++i) {
149,467,458 ✔
1442
    tb_uid_t suid = *(tb_uid_t *)taosArrayGet(suidList, i);
66,365,159 ✔
1443

1444
    int64_t ctbNum = 0;
66,367,340 ✔
1445
    int32_t numOfCols = 0;
66,366,613 ✔
1446
    int8_t  flags = 0;
66,365,159 ✔
1447
    code = metaGetStbStats(pVnode, suid, &ctbNum, &numOfCols, &flags);
66,364,432 ✔
1448
    if (TSDB_CODE_SUCCESS != code) {
66,370,975 ✔
1449
      goto _exit;
×
1450
    }
1451
    if (!TABLE_IS_VIRTUAL(flags)) {
66,370,975 ✔
1452
      *num += ctbNum * (numOfCols - 1);
60,103,033 ✔
1453
    }
1454
  }
1455

1456
_exit:
83,104,013 ✔
1457
  taosArrayDestroy(suidList);
83,107,543 ✔
1458
  return TSDB_CODE_SUCCESS;
83,103,181 ✔
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,787,753 ✔
1489
  if (pVnode == NULL) {
4,787,753 ✔
1490
    return NULL;
×
1491
  }
1492

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

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

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

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

1511
  char   *dirName[] = {VNODE_TSDB_DIR, VNODE_WAL_DIR, VNODE_META_DIR, VNODE_TSDB_CACHE_DIR};
9,362,516 ✔
1512
  int64_t dirSize[4];
9,360,953 ✔
1513

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

1517
  for (int i = 0; i < sizeof(dirName) / sizeof(dirName[0]); i++) {
46,812,580 ✔
1518
    int64_t size = {0};
37,450,064 ✔
1519
    (void)snprintf(path + offset, TSDB_FILENAME_LEN - offset, "%s%s", TD_DIRSEP, dirName[i]);
37,450,064 ✔
1520
    code = taosGetDirSize(path, &size);
37,450,064 ✔
1521
    if (code != 0) {
37,450,064 ✔
1522
      uWarn("vnode %d get dir %s %s size failed since %s", TD_VID(pVnode), path, dirName[i], tstrerror(code));
4,314 ✔
1523
    }
1524
    path[offset] = 0;
37,450,064 ✔
1525
    dirSize[i] = size;
37,450,064 ✔
1526
  }
1527

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

1544
  code = tsdbGetFsSize(pVnodeObj->pTsdb, pInfo);
9,362,516 ✔
1545
_exit:
9,362,516 ✔
1546
  return code;
9,362,516 ✔
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 TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc