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

taosdata / TDengine / #5118

16 Sep 2026 01:31AM UTC coverage: 73.739% (+0.9%) from 72.885%
#5118

push

travis-ci

jbjia
test(coverage): sync from gitlab

312365 of 423608 relevant lines covered (73.74%)

70479229.47 hits per line

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

69.53
/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 "meta.h"
17
#include "tsdb.h"
18
#include "tutil.h"
19
#include "vnd.h"
20

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

29
int vnodeQueryOpen(SVnode *pVnode) {
1,510,178✔
30
  return qWorkerInit(NODE_TYPE_VNODE, TD_VID(pVnode), (void **)&pVnode->pQuery, &pVnode->msgCb);
1,510,178✔
31
}
32

33
void vnodeQueryPreClose(SVnode *pVnode) { qWorkerStopAllTasks((void *)pVnode->pQuery); }
1,508,977✔
34

35
void vnodeQueryClose(SVnode *pVnode) { qWorkerDestroy((void **)&pVnode->pQuery); }
1,510,006✔
36

37
int32_t fillTableColCmpr(SMetaReader *reader, SSchemaExt *pExt, int32_t numOfCol) {
65,586,727✔
38
  int8_t tblType = reader->me.type;
65,586,727✔
39
  if (withColCompress(tblType)) {
65,586,727✔
40
    SColCmprWrapper *p = &(reader->me.colCmpr);
51,655,975✔
41
    if (numOfCol != p->nCols) {
51,655,975✔
42
      vError("fillTableColCmpr table type:%d, col num:%d, col cmpr num:%d mismatch", tblType, numOfCol, p->nCols);
×
43
      return TSDB_CODE_APP_ERROR;
×
44
    }
45
    for (int i = 0; i < p->nCols; i++) {
545,709,594✔
46
      SColCmpr *pCmpr = &p->pColCmpr[i];
494,053,619✔
47
      pExt[i].colId = pCmpr->id;
494,053,619✔
48
      pExt[i].compress = pCmpr->alg;
494,053,619✔
49
    }
50
  }
51
  return 0;
65,023,372✔
52
}
53

54
void vnodeDebugTableMeta(STableMetaRsp *pMeta) {
28,290,062✔
55
  if (!(qDebugFlag & DEBUG_DEBUG)) {
28,290,062✔
56
    return;
18,410,696✔
57
  }
58

59
  qDebug("tbName:%s", pMeta->tbName);
9,879,364✔
60
  qDebug("stbName:%s", pMeta->stbName);
9,881,135✔
61
  qDebug("dbFName:%s", pMeta->dbFName);
9,881,135✔
62
  qDebug("dbId:%" PRId64, pMeta->dbId);
9,881,135✔
63
  qDebug("numOfTags:%d", pMeta->numOfTags);
9,881,135✔
64
  qDebug("numOfColumns:%d", pMeta->numOfColumns);
9,881,135✔
65
  qDebug("precision:%d", pMeta->precision);
9,881,135✔
66
  qDebug("tableType:%d", pMeta->tableType);
9,881,135✔
67
  qDebug("sversion:%d", pMeta->sversion);
9,881,135✔
68
  qDebug("tversion:%d", pMeta->tversion);
9,881,135✔
69
  qDebug("suid:%" PRIu64, pMeta->suid);
9,881,135✔
70
  qDebug("tuid:%" PRIu64, pMeta->tuid);
9,881,017✔
71
  qDebug("vgId:%d", pMeta->vgId);
9,881,017✔
72
  qDebug("sysInfo:%d", pMeta->sysInfo);
9,881,017✔
73
  if (pMeta->pSchemas) {
9,881,017✔
74
    for (int32_t i = 0; i < (pMeta->numOfColumns + pMeta->numOfTags); ++i) {
115,117,355✔
75
      SSchema *pSchema = pMeta->pSchemas + i;
105,237,188✔
76
      qDebug("%d col/tag: type:%d, flags:%d, colId:%d, bytes:%d, name:%s", i, pSchema->type, pSchema->flags,
105,237,188✔
77
             pSchema->colId, pSchema->bytes, pSchema->name);
78
    }
79
  }
80
}
81

82
int32_t fillTableColRef(SMetaReader *reader, SColRef *pRef, int32_t numOfCol) {
15,380,761✔
83
  int8_t tblType = reader->me.type;
15,380,761✔
84
  if (hasColRef(tblType)) {
15,380,761✔
85
    SColRefWrapper *p = &(reader->me.colRef);
15,380,580✔
86
    if (numOfCol != p->nCols) {
15,380,580✔
87
      vError("fillTableColRef table type:%d, col num:%d, col cmpr num:%d mismatch", tblType, numOfCol, p->nCols);
×
88
      return TSDB_CODE_APP_ERROR;
×
89
    }
90
    for (int i = 0; i < p->nCols; i++) {
231,753,189✔
91
      SColRef *pColRef = &p->pColRef[i];
216,372,609✔
92
      pRef[i].hasRef = pColRef->hasRef;
216,372,609✔
93
      pRef[i].id = pColRef->id;
216,372,609✔
94
      if(pRef[i].hasRef) {
216,372,609✔
95
        pRef[i].refType = pColRef->refType;
176,416,984✔
96
        tstrncpy(pRef[i].refSourceName, pColRef->refSourceName, TSDB_EXT_SOURCE_NAME_LEN);
176,416,984✔
97
        tstrncpy(pRef[i].refSchemaName, pColRef->refSchemaName, TSDB_EXT_SOURCE_SCHEMA_LEN);
176,416,984✔
98
        tstrncpy(pRef[i].refDbName, pColRef->refDbName, TSDB_DB_NAME_LEN);
176,416,984✔
99
        tstrncpy(pRef[i].refTableName, pColRef->refTableName, TSDB_TABLE_NAME_LEN);
176,416,984✔
100
        tstrncpy(pRef[i].refColName, pColRef->refColName, TSDB_COL_NAME_LEN);
176,416,984✔
101
        if (pColRef->tagCondLen > 0 && pColRef->tagCondJson) {
176,416,984✔
102
          pRef[i].tagCondLen = pColRef->tagCondLen;
214,838✔
103
          pRef[i].tagCondJson = taosStrdup(pColRef->tagCondJson);
214,838✔
104
        } else {
105
          pRef[i].tagCondLen = 0;
176,202,146✔
106
          pRef[i].tagCondJson = NULL;
176,202,146✔
107
        }
108
      }
109
    }
110
  }
111
  return 0;
15,381,073✔
112
}
113

114
int32_t fillTableTagRef(SMetaReader *reader, SColRef *pRef, int32_t numOfTagRefs) {
629,277✔
115
  int8_t tblType = reader->me.type;
629,277✔
116
  if (hasTagRef(tblType)) {
629,277✔
117
    SColRefWrapper *p = &(reader->me.colRef);
629,277✔
118
    if (numOfTagRefs != p->nTagRefs) {
629,277✔
119
      vError("fillTableTagRef table type:%d, tag ref num:%d, expected:%d mismatch", tblType, numOfTagRefs, p->nTagRefs);
×
120
      return TSDB_CODE_APP_ERROR;
×
121
    }
122
    for (int i = 0; i < p->nTagRefs; i++) {
2,095,342✔
123
      SColRef *pTagRef = &p->pTagRef[i];
1,466,065✔
124
      pRef[i].hasRef = pTagRef->hasRef;
1,466,065✔
125
      pRef[i].id = pTagRef->id;
1,466,065✔
126
      if (pRef[i].hasRef) {
1,466,065✔
127
        tstrncpy(pRef[i].refDbName, pTagRef->refDbName, TSDB_DB_NAME_LEN);
1,165,194✔
128
        tstrncpy(pRef[i].refTableName, pTagRef->refTableName, TSDB_TABLE_NAME_LEN);
1,165,194✔
129
        tstrncpy(pRef[i].refColName, pTagRef->refColName, TSDB_COL_NAME_LEN);
1,165,194✔
130
      }
131
    }
132
  }
133
  return 0;
629,277✔
134
}
135

136
int32_t vnodeGetTableMeta(SVnode *pVnode, SRpcMsg *pMsg, bool direct) {
31,924,952✔
137
  STableInfoReq  infoReq = {0};
31,924,952✔
138
  STableMetaRsp  metaRsp = {0};
31,924,952✔
139
  SMetaReader    mer1 = {0};
31,923,248✔
140
  SMetaReader    mer2 = {0};
31,923,703✔
141
  char           tableFName[TSDB_TABLE_FNAME_LEN];
31,922,763✔
142
  bool           reqTbUid = false;
31,922,912✔
143
  SRpcMsg        rpcMsg = {0};
31,922,912✔
144
  int32_t        code = 0;
31,922,739✔
145
  int32_t        rspLen = 0;
31,922,739✔
146
  void          *pRsp = NULL;
31,922,739✔
147
  SSchemaWrapper schema = {0};
31,922,739✔
148
  SSchemaWrapper schemaTag = {0};
31,922,739✔
149
  uint8_t        autoCreateCtb = 0;
31,922,739✔
150

151
  // decode req
152
  if (tDeserializeSTableInfoReq(pMsg->pCont, pMsg->contLen, &infoReq) != 0) {
31,922,739✔
153
    code = terrno;
×
154
    goto _exit4;
×
155
  }
156
  autoCreateCtb = infoReq.autoCreateCtb;
31,925,383✔
157

158
  if (infoReq.option == REQ_OPT_TBUID) reqTbUid = true;
31,925,383✔
159
  metaRsp.dbId = pVnode->config.dbId;
31,925,383✔
160
  tstrncpy(metaRsp.tbName, infoReq.tbName, TSDB_TABLE_NAME_LEN);
31,925,383✔
161
  (void)memcpy(metaRsp.dbFName, infoReq.dbFName, sizeof(metaRsp.dbFName));
31,925,383✔
162

163
  if (!reqTbUid) {
31,925,383✔
164
    (void)snprintf(tableFName, TSDB_TABLE_FNAME_LEN, "%s.%s", infoReq.dbFName, infoReq.tbName);
31,850,323✔
165
    if (pVnode->mounted) tTrimMountPrefix(tableFName);
31,850,323✔
166
    code = vnodeValidateTableHash(pVnode, tableFName);
31,850,323✔
167
    if (code) {
31,851,817✔
168
      goto _exit4;
×
169
    }
170
  }
171

172
  // query meta
173
  metaReaderDoInit(&mer1, pVnode->pMeta, META_READER_LOCK, infoReq.txnId);
31,926,877✔
174
  if (reqTbUid) {
31,924,857✔
175
    SET_ERRNO(0);
73,930✔
176
    uint64_t tbUid = taosStr2UInt64(infoReq.tbName, NULL, 10);
73,930✔
177
    if (ERRNO == ERANGE || tbUid == 0) {
73,930✔
178
      code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
960✔
179
      goto _exit3;
960✔
180
    }
181
    SMetaReader mr3 = {0};
72,970✔
182
    metaReaderDoInit(&mr3, ((SVnode *)pVnode)->pMeta, META_READER_NOLOCK, infoReq.txnId);
72,970✔
183
    if ((code = metaReaderGetTableEntryByUid(&mr3, tbUid)) < 0) {
72,970✔
184
      metaReaderClear(&mr3);
25,577✔
185
      TAOS_CHECK_GOTO(code, NULL, _exit3);
25,577✔
186
    }
187
    tstrncpy(metaRsp.tbName, mr3.me.name, TSDB_TABLE_NAME_LEN);
47,157✔
188
    metaReaderClear(&mr3);
47,157✔
189
    TAOS_CHECK_GOTO(metaGetTableEntryByName(&mer1, metaRsp.tbName), NULL, _exit3);
47,216✔
190
  } else if (metaGetTableEntryByName(&mer1, infoReq.tbName) < 0) {
31,850,927✔
191
    code = terrno;
3,606,808✔
192
    goto _exit3;
3,607,444✔
193
  }
194

195
  metaRsp.tableType = mer1.me.type;
28,290,035✔
196
  metaRsp.vgId = TD_VID(pVnode);
28,290,035✔
197
  metaRsp.tuid = mer1.me.uid;
28,290,035✔
198
  metaRsp.isAudit = pVnode->config.isAudit ? 1 : 0;
28,290,035✔
199

200
  switch (mer1.me.type) {
28,290,035✔
201
    case TSDB_SUPER_TABLE: {
1,472,295✔
202
      tstrncpy(metaRsp.stbName, mer1.me.name, sizeof(metaRsp.stbName));
1,472,295✔
203
      schema = mer1.me.stbEntry.schemaRow;
1,472,295✔
204
      schemaTag = mer1.me.stbEntry.schemaTag;
1,472,295✔
205
      metaRsp.suid = mer1.me.uid;
1,472,295✔
206
      metaRsp.virtualStb = TABLE_IS_VIRTUAL(mer1.me.flags);
1,472,295✔
207
      metaRsp.ownerId = mer1.me.stbEntry.ownerId;
1,472,295✔
208
      metaRsp.secLvl = mer1.me.stbEntry.securityLevel;
1,472,295✔
209
      break;
1,472,295✔
210
    }
211
    case TSDB_CHILD_TABLE:
6,702,588✔
212
    case TSDB_VIRTUAL_CHILD_TABLE:{
213
      metaReaderDoInit(&mer2, pVnode->pMeta, META_READER_NOLOCK, infoReq.txnId);
6,702,588✔
214
      if (metaReaderGetTableEntryByUid(&mer2, mer1.me.ctbEntry.suid) < 0) goto _exit2;
6,703,231✔
215

216
      tstrncpy(metaRsp.stbName, mer2.me.name, sizeof(metaRsp.stbName));
6,703,279✔
217
      metaRsp.suid = mer2.me.uid;
6,703,279✔
218
      metaRsp.ownerId = mer2.me.stbEntry.ownerId;  // child table inherits ownerId from stb
6,703,279✔
219
      metaRsp.secLvl = mer2.me.stbEntry.securityLevel;  // child table inherits secLvl from stb
6,703,279✔
220
      schema = mer2.me.stbEntry.schemaRow;
6,703,279✔
221
      schemaTag = mer2.me.stbEntry.schemaTag;
6,703,279✔
222
      break;
6,703,279✔
223
    }
224
    case TSDB_NORMAL_TABLE:
6,205,356✔
225
      metaRsp.secLvl = pVnode->config.securityLevel;  // normal table inherits secLvl from vnode config
6,205,356✔
226
    case TSDB_VIRTUAL_NORMAL_TABLE: {
20,115,152✔
227
      schema = mer1.me.ntbEntry.schemaRow;
20,115,152✔
228
      schemaTag = mer1.me.ntbEntry.schemaTag;  // normal/virtual-normal tables may own tags
20,115,152✔
229
      metaRsp.ownerId = mer1.me.ntbEntry.ownerId;
20,115,152✔
230
      break;
20,115,152✔
231
    }
232
    default: {
×
233
      vError("vnodeGetTableMeta get invalid table type:%d", mer1.me.type);
×
234
      goto _exit3;
×
235
    }
236
  }
237

238
  metaRsp.numOfTags = schemaTag.nCols;
28,290,726✔
239
  metaRsp.numOfColumns = schema.nCols;
28,290,726✔
240
  metaRsp.precision = pVnode->config.tsdbCfg.precision;
28,290,726✔
241
  metaRsp.sversion = schema.version;
28,290,726✔
242
  metaRsp.tversion = schemaTag.version;
28,290,726✔
243
  metaRsp.pSchemas = (SSchema *)taosMemoryMalloc(sizeof(SSchema) * (metaRsp.numOfColumns + metaRsp.numOfTags));
28,290,726✔
244
  metaRsp.pSchemaExt = (SSchemaExt *)taosMemoryCalloc(metaRsp.numOfColumns, sizeof(SSchemaExt));
28,287,889✔
245
  if (NULL == metaRsp.pSchemas || NULL == metaRsp.pSchemaExt) {
28,288,983✔
246
    code = terrno;
×
247
    goto _exit;
×
248
  }
249
  (void)memcpy(metaRsp.pSchemas, schema.pSchema, sizeof(SSchema) * schema.nCols);
28,289,348✔
250
  if (schemaTag.nCols) {
28,289,348✔
251
    (void)memcpy(metaRsp.pSchemas + schema.nCols, schemaTag.pSchema, sizeof(SSchema) * schemaTag.nCols);
8,186,130✔
252
  }
253
  if (metaRsp.pSchemaExt) {
28,289,348✔
254
    SMetaReader *pReader =
28,296,676✔
255
        (mer1.me.type == TSDB_CHILD_TABLE || mer1.me.type == TSDB_VIRTUAL_CHILD_TABLE) ? &mer2 : &mer1;
28,291,052✔
256
    code = fillTableColCmpr(pReader, metaRsp.pSchemaExt, metaRsp.numOfColumns);
28,291,052✔
257
    if (code < 0) {
28,289,448✔
258
      goto _exit;
×
259
    }
260
    for (int32_t i = 0; i < metaRsp.numOfColumns; i++) {
410,152,437✔
261
      metaRsp.pSchemaExt[i].colId = schema.pSchema[i].colId;
381,862,989✔
262
      if (pReader->me.pExtSchemas) {
381,862,989✔
263
        metaRsp.pSchemaExt[i].typeMod = pReader->me.pExtSchemas[i].typeMod;
9,131,012✔
264
      }
265
    }
266
  } else {
267
    code = TSDB_CODE_OUT_OF_MEMORY;
×
268
    goto _exit;
×
269
  }
270
  if (hasColRef(mer1.me.type)) {
28,289,448✔
271
    metaRsp.rversion = mer1.me.colRef.version;
15,379,670✔
272
    metaRsp.pColRefs = (SColRef*)taosMemoryCalloc(metaRsp.numOfColumns, sizeof(SColRef));
15,379,670✔
273
    if (metaRsp.pColRefs) {
15,379,844✔
274
      code = fillTableColRef(&mer1, metaRsp.pColRefs, metaRsp.numOfColumns);
15,380,190✔
275
      if (code < 0) {
15,381,437✔
276
        goto _exit;
×
277
      }
278
    }
279
    metaRsp.numOfColRefs = metaRsp.numOfColumns;
15,381,091✔
280

281
    // Fill tag references
282
    if (mer1.me.colRef.nTagRefs > 0) {
15,381,091✔
283
      metaRsp.pTagRefs = (SColRef*)taosMemoryCalloc(mer1.me.colRef.nTagRefs, sizeof(SColRef));
629,277✔
284
      if (metaRsp.pTagRefs) {
629,277✔
285
        code = fillTableTagRef(&mer1, metaRsp.pTagRefs, mer1.me.colRef.nTagRefs);
629,277✔
286
        if (code < 0) {
629,277✔
287
          taosMemoryFreeClear(metaRsp.pTagRefs);
×
288
          goto _exit;
×
289
        }
290
      } else {
291
        code = terrno;
×
292
        goto _exit;
×
293
      }
294
      metaRsp.numOfTagRefs = mer1.me.colRef.nTagRefs;
629,277✔
295
    } else {
296
      metaRsp.pTagRefs = NULL;
14,751,814✔
297
      metaRsp.numOfTagRefs = 0;
14,751,814✔
298
    }
299
  } else {
300
    metaRsp.pColRefs = NULL;
12,909,180✔
301
    metaRsp.numOfColRefs = 0;
12,909,180✔
302
    metaRsp.pTagRefs = NULL;
12,909,180✔
303
    metaRsp.numOfTagRefs = 0;
12,909,180✔
304
  }
305

306
  // Fill series
307
  if (mer1.me.series.nSeries > 0) {
28,290,271✔
308
    metaRsp.numOfSeries = mer1.me.series.nSeries;
21,468✔
309
    metaRsp.pSeries = taosMemoryCalloc(mer1.me.series.nSeries, sizeof(SSeriesEntry));
21,468✔
310
    if (metaRsp.pSeries) {
21,468✔
311
      for (int32_t i = 0; i < mer1.me.series.nSeries; i++) {
114,398✔
312
        SSeriesEntry *src = &mer1.me.series.pSeries[i];
92,930✔
313
        SSeriesEntry *dst = &metaRsp.pSeries[i];
92,930✔
314
        tstrncpy(dst->alias, src->alias, TSDB_COL_NAME_LEN);
92,930✔
315
        tstrncpy(dst->sourceName, src->sourceName, TSDB_EXT_SOURCE_NAME_LEN);
92,930✔
316
        tstrncpy(dst->dbName, src->dbName, TSDB_DB_NAME_LEN);
92,930✔
317
        tstrncpy(dst->measurementName, src->measurementName, TSDB_TABLE_NAME_LEN);
92,930✔
318
        dst->tagCondLen = src->tagCondLen;
92,930✔
319
        dst->tagCondJson = src->tagCondJson ? taosStrdup(src->tagCondJson) : NULL;
92,930✔
320
      }
321
    } else {
322
      code = terrno;
×
323
      goto _exit;
×
324
    }
325
  } else {
326
    metaRsp.numOfSeries = 0;
28,268,803✔
327
    metaRsp.pSeries = NULL;
28,268,803✔
328
  }
329

330
  vnodeDebugTableMeta(&metaRsp);
28,290,271✔
331

332
  // encode and send response
333
  rspLen = tSerializeSTableMetaRsp(NULL, 0, &metaRsp);
28,291,485✔
334
  if (rspLen < 0) {
28,289,533✔
335
    code = terrno;
×
336
    goto _exit;
×
337
  }
338

339
  if (direct) {
28,289,533✔
340
    pRsp = rpcMallocCont(rspLen);
20,561,218✔
341
  } else {
342
    pRsp = taosMemoryCalloc(1, rspLen);
7,728,315✔
343
  }
344

345
  if (pRsp == NULL) {
28,286,402✔
346
    code = terrno;
×
347
    goto _exit;
×
348
  }
349

350
  rspLen = tSerializeSTableMetaRsp(pRsp, rspLen, &metaRsp);
28,286,402✔
351
  if (rspLen < 0) {
28,290,548✔
352
    code = terrno;
×
353
    goto _exit;
×
354
  }
355

356
_exit:
28,288,844✔
357
  tFreeSTableMetaRsp(&metaRsp);
28,290,293✔
358
_exit2:
28,288,799✔
359
  metaReaderClear(&mer2);
28,290,223✔
360
_exit3:
31,923,763✔
361
  metaReaderClear(&mer1);
31,925,029✔
362
_exit4:
31,924,725✔
363
  rpcMsg.info = pMsg->info;
31,925,634✔
364
  rpcMsg.pCont = pRsp;
31,925,634✔
365
  rpcMsg.contLen = rspLen;
31,925,634✔
366
  rpcMsg.code = code;
31,925,634✔
367
  rpcMsg.msgType = direct ? pMsg->msgType + 1 : pMsg->msgType;
31,925,634✔
368

369
  if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST && autoCreateCtb == 1) {
31,925,634✔
370
    code = TSDB_CODE_SUCCESS;
1,428,301✔
371
  }
372

373
  if (code) {
31,925,582✔
374
    qError("vgId:%d, get table %s meta with %" PRIu8 " failed cause of %s", pVnode->config.vgId, infoReq.tbName,
2,205,310✔
375
           infoReq.option, tstrerror(code));
376
  }
377

378
  if (direct) {
31,923,905✔
379
    tmsgSendRsp(&rpcMsg);
21,134,542✔
380
  } else {
381
    *pMsg = rpcMsg;
10,789,363✔
382
  }
383

384
  return code;
31,923,905✔
385
}
386

387
int32_t vnodeGetTableCfg(SVnode *pVnode, SRpcMsg *pMsg, bool direct) {
37,303,088✔
388
  STableCfgReq   cfgReq = {0};
37,303,088✔
389
  STableCfgRsp   cfgRsp = {0};
37,303,088✔
390
  SMetaReader    mer1 = {0};
37,303,088✔
391
  SMetaReader    mer2 = {0};
37,303,088✔
392
  char           tableFName[TSDB_TABLE_FNAME_LEN];
37,303,088✔
393
  SRpcMsg        rpcMsg = {0};
37,303,088✔
394
  int32_t        code = 0;
37,303,088✔
395
  int32_t        rspLen = 0;
37,303,088✔
396
  void          *pRsp = NULL;
37,303,088✔
397
  SSchemaWrapper schema = {0};
37,303,088✔
398
  SSchemaWrapper schemaTag = {0};
37,303,088✔
399

400
  // decode req
401
  if (tDeserializeSTableCfgReq(pMsg->pCont, pMsg->contLen, &cfgReq) != 0) {
37,303,088✔
402
    code = terrno;
×
403
    goto _exit;
×
404
  }
405

406
  tstrncpy(cfgRsp.tbName, cfgReq.tbName, TSDB_TABLE_NAME_LEN);
37,303,088✔
407
  (void)memcpy(cfgRsp.dbFName, cfgReq.dbFName, sizeof(cfgRsp.dbFName));
37,303,088✔
408

409
  (void)snprintf(tableFName, TSDB_TABLE_FNAME_LEN, "%s.%s", cfgReq.dbFName, cfgReq.tbName);
37,303,088✔
410
  if (pVnode->mounted) tTrimMountPrefix(tableFName);
37,303,088✔
411
  code = vnodeValidateTableHash(pVnode, tableFName);
37,303,088✔
412
  if (code) {
37,303,088✔
413
    goto _exit;
×
414
  }
415

416
  // query meta
417
  metaReaderDoInit(&mer1, pVnode->pMeta, META_READER_LOCK, cfgReq.txnId);
37,303,088✔
418

419
  if (metaGetTableEntryByName(&mer1, cfgReq.tbName) < 0) {
37,303,088✔
420
    code = terrno;
6,594✔
421
    goto _exit;
6,594✔
422
  }
423

424
  cfgRsp.tableType = mer1.me.type;
37,296,494✔
425
  cfgRsp.isAudit = pVnode->config.isAudit ? 1 : 0;
37,296,494✔
426
  cfgRsp.secureDelete = pVnode->config.secureDelete;
37,296,494✔
427

428
  if (mer1.me.type == TSDB_SUPER_TABLE) {
37,296,494✔
429
    code = TSDB_CODE_VND_HASH_MISMATCH;
×
430
    goto _exit;
×
431
  } else if (mer1.me.type == TSDB_CHILD_TABLE || mer1.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
36,738,286✔
432
    metaReaderDoInit(&mer2, pVnode->pMeta, META_READER_NOLOCK, cfgReq.txnId);
37,270,458✔
433
    if (metaReaderGetTableEntryByUid(&mer2, mer1.me.ctbEntry.suid) < 0) goto _exit;
37,270,458✔
434

435
    tstrncpy(cfgRsp.stbName, mer2.me.name, TSDB_TABLE_NAME_LEN);
37,270,458✔
436
    schema = mer2.me.stbEntry.schemaRow;
37,270,458✔
437
    schemaTag = mer2.me.stbEntry.schemaTag;
37,270,458✔
438
    cfgRsp.ownerId = mer2.me.stbEntry.ownerId;        // child table inherits ownerId from stb
37,270,458✔
439
    cfgRsp.securityLevel = mer2.me.stbEntry.securityLevel;  // child table inherits secLvl from stb
37,270,458✔
440
    cfgRsp.ttl = mer1.me.ctbEntry.ttlDays;
37,270,458✔
441
    cfgRsp.commentLen = mer1.me.ctbEntry.commentLen;
37,270,458✔
442
    if (mer1.me.ctbEntry.commentLen > 0) {
37,270,458✔
443
      cfgRsp.pComment = taosStrdup(mer1.me.ctbEntry.comment);
×
444
      if (NULL == cfgRsp.pComment) {
×
445
        code = terrno;
×
446
        goto _exit;
×
447
      }
448
    }
449
    STag *pTag = (STag *)mer1.me.ctbEntry.pTags;
37,270,458✔
450
    cfgRsp.tagsLen = pTag->len;
37,270,458✔
451
    cfgRsp.pTags = taosMemoryMalloc(cfgRsp.tagsLen);
37,270,458✔
452
    if (NULL == cfgRsp.pTags) {
37,270,458✔
453
      code = terrno;
×
454
      goto _exit;
×
455
    }
456
    (void)memcpy(cfgRsp.pTags, pTag, cfgRsp.tagsLen);
37,270,458✔
457
  } else if (mer1.me.type == TSDB_NORMAL_TABLE || mer1.me.type == TSDB_VIRTUAL_NORMAL_TABLE) {
26,036✔
458
    schema = mer1.me.ntbEntry.schemaRow;
26,036✔
459
    schemaTag = mer1.me.ntbEntry.schemaTag;  // normal/virtual-normal tables may own tags
26,036✔
460
    cfgRsp.ttl = mer1.me.ntbEntry.ttlDays;
26,036✔
461
    cfgRsp.ownerId = mer1.me.ntbEntry.ownerId;
26,036✔
462
    cfgRsp.securityLevel = mer1.me.type == TSDB_NORMAL_TABLE ? pVnode->config.securityLevel : 0;
26,036✔
463
    cfgRsp.commentLen = mer1.me.ntbEntry.commentLen;
26,036✔
464
    if (mer1.me.ntbEntry.commentLen > 0) {
26,036✔
465
      cfgRsp.pComment = taosStrdup(mer1.me.ntbEntry.comment);
×
466
      if (NULL == cfgRsp.pComment) {
×
467
        code = terrno;
×
468
        goto _exit;
×
469
      }
470
    }
471
    // owned tag values: copy ntbEntry.pTags so the executor's local-tag reading path
472
    // (which reads cfgRsp.pTags) can project them. Mirrors the child-table branch above.
473
    if (mer1.me.ntbEntry.schemaTag.nCols > 0 && mer1.me.ntbEntry.pTags != NULL) {
26,036✔
474
      STag *pTag = (STag *)mer1.me.ntbEntry.pTags;
6,579✔
475
      cfgRsp.tagsLen = pTag->len;
6,579✔
476
      cfgRsp.pTags = taosMemoryMalloc(cfgRsp.tagsLen);
6,579✔
477
      if (NULL == cfgRsp.pTags) {
6,579✔
478
        code = terrno;
×
479
        goto _exit;
×
480
      }
481
      (void)memcpy(cfgRsp.pTags, pTag, cfgRsp.tagsLen);
6,579✔
482
    }
483
  } else {
484
    vError("vnodeGetTableCfg get invalid table type:%d", mer1.me.type);
×
485
    code = TSDB_CODE_APP_ERROR;
×
486
    goto _exit;
×
487
  }
488

489
  cfgRsp.numOfTags = schemaTag.nCols;
37,296,494✔
490
  cfgRsp.numOfColumns = schema.nCols;
37,296,494✔
491
  cfgRsp.virtualStb = false; // vnode don't have super table, so it's always false
37,296,494✔
492
  cfgRsp.pSchemas = (SSchema *)taosMemoryMalloc(sizeof(SSchema) * (cfgRsp.numOfColumns + cfgRsp.numOfTags));
37,296,494✔
493
  cfgRsp.pSchemaExt = (SSchemaExt *)taosMemoryCalloc(cfgRsp.numOfColumns, sizeof(SSchemaExt));
37,296,494✔
494
  cfgRsp.pColRefs = (SColRef *)taosMemoryCalloc(cfgRsp.numOfColumns, sizeof(SColRef));
37,296,494✔
495
  cfgRsp.numOfTagRefs = 0;
37,296,494✔
496
  cfgRsp.pTagRefs = NULL;
37,296,494✔
497

498
  if (NULL == cfgRsp.pSchemas || NULL == cfgRsp.pSchemaExt || NULL == cfgRsp.pColRefs) {
37,296,494✔
499
    code = terrno;
×
500
    goto _exit;
×
501
  }
502
  (void)memcpy(cfgRsp.pSchemas, schema.pSchema, sizeof(SSchema) * schema.nCols);
37,296,494✔
503
  if (schemaTag.nCols) {
37,296,494✔
504
    (void)memcpy(cfgRsp.pSchemas + schema.nCols, schemaTag.pSchema, sizeof(SSchema) * schemaTag.nCols);
37,277,037✔
505
  }
506

507
  SMetaReader    *pReader = (mer1.me.type == TSDB_CHILD_TABLE || mer1.me.type == TSDB_VIRTUAL_CHILD_TABLE) ? &mer2 : &mer1;
37,296,494✔
508
  SColRefWrapper *pColRef = &mer1.me.colRef;
37,296,494✔
509

510
  if (withExtSchema(cfgRsp.tableType)) {
37,296,494✔
511
    code = fillTableColCmpr(pReader, cfgRsp.pSchemaExt, cfgRsp.numOfColumns);
37,296,494✔
512
    if (code < 0) {
37,296,494✔
513
      goto _exit;
×
514
    }
515

516
    for (int32_t i = 0; i < cfgRsp.numOfColumns; i++) {
361,581,614✔
517
      SSchemaExt *pSchExt = cfgRsp.pSchemaExt + i;
324,285,120✔
518
      pSchExt->colId = schema.pSchema[i].colId;
324,285,120✔
519
      if (pReader->me.pExtSchemas) {
324,285,120✔
520
        pSchExt->typeMod = pReader->me.pExtSchemas[i].typeMod;
61,107✔
521
      }
522
    }
523
  }
524

525
  cfgRsp.virtualStb = false;
37,296,494✔
526
  if (hasColRef(cfgRsp.tableType)) {
37,296,494✔
527
    for (int32_t i = 0; i < cfgRsp.numOfColumns; i++) {
262,257,438✔
528
      SColRef *pRef = &pColRef->pColRef[i];
236,484,242✔
529
      cfgRsp.pColRefs[i].hasRef = pRef->hasRef;
236,484,242✔
530
      cfgRsp.pColRefs[i].id = pRef->id;
236,484,242✔
531
      if (cfgRsp.pColRefs[i].hasRef) {
236,484,242✔
532
        cfgRsp.pColRefs[i].refType = pRef->refType;
148,575,015✔
533
        tstrncpy(cfgRsp.pColRefs[i].refSourceName, pRef->refSourceName, TSDB_EXT_SOURCE_NAME_LEN);
148,575,015✔
534
        tstrncpy(cfgRsp.pColRefs[i].refSchemaName, pRef->refSchemaName, TSDB_EXT_SOURCE_SCHEMA_LEN);
148,575,015✔
535
        tstrncpy(cfgRsp.pColRefs[i].refDbName, pRef->refDbName, TSDB_DB_NAME_LEN);
148,575,015✔
536
        tstrncpy(cfgRsp.pColRefs[i].refTableName, pRef->refTableName, TSDB_TABLE_NAME_LEN);
148,575,015✔
537
        tstrncpy(cfgRsp.pColRefs[i].refColName, pRef->refColName, TSDB_COL_NAME_LEN);
148,575,015✔
538
        if (pRef->tagCondJson) {
148,575,015✔
539
          cfgRsp.pColRefs[i].tagCondLen = pRef->tagCondLen;
807,400✔
540
          cfgRsp.pColRefs[i].tagCondJson = taosStrdup(pRef->tagCondJson);
807,400✔
541
        } else {
542
          cfgRsp.pColRefs[i].tagCondLen = 0;
147,767,615✔
543
          cfgRsp.pColRefs[i].tagCondJson = NULL;
147,767,615✔
544
        }
545
      }
546
    }
547

548
    cfgRsp.numOfTagRefs = pColRef->nTagRefs;
25,773,196✔
549
    if (cfgRsp.numOfTagRefs > 0) {
25,773,196✔
550
      if (NULL == pColRef->pTagRef) {
12,760,840✔
551
        code = TSDB_CODE_APP_ERROR;
×
552
        goto _exit;
×
553
      }
554
      cfgRsp.pTagRefs = (SColRef *)taosMemoryCalloc(cfgRsp.numOfTagRefs, sizeof(SColRef));
12,760,840✔
555
      if (NULL == cfgRsp.pTagRefs) {
12,760,840✔
556
        code = terrno;
×
557
        goto _exit;
×
558
      }
559

560
      for (int32_t i = 0; i < cfgRsp.numOfTagRefs; i++) {
50,852,835✔
561
        SColRef *pRef = &pColRef->pTagRef[i];
38,091,995✔
562
        cfgRsp.pTagRefs[i].hasRef = pRef->hasRef;
38,091,995✔
563
        cfgRsp.pTagRefs[i].id = pRef->id;
38,091,995✔
564
        if (cfgRsp.pTagRefs[i].hasRef) {
38,091,995✔
565
          cfgRsp.pTagRefs[i].refType = pRef->refType;
25,317,642✔
566
          tstrncpy(cfgRsp.pTagRefs[i].refSourceName, pRef->refSourceName, TSDB_EXT_SOURCE_NAME_LEN);
25,317,642✔
567
          tstrncpy(cfgRsp.pTagRefs[i].refSchemaName, pRef->refSchemaName, TSDB_EXT_SOURCE_SCHEMA_LEN);
25,317,642✔
568
          tstrncpy(cfgRsp.pTagRefs[i].refDbName, pRef->refDbName, TSDB_DB_NAME_LEN);
25,317,642✔
569
          tstrncpy(cfgRsp.pTagRefs[i].refTableName, pRef->refTableName, TSDB_TABLE_NAME_LEN);
25,317,642✔
570
          tstrncpy(cfgRsp.pTagRefs[i].refColName, pRef->refColName, TSDB_COL_NAME_LEN);
25,317,642✔
571
          if (pRef->tagCondJson) {
25,317,642✔
572
            cfgRsp.pTagRefs[i].tagCondLen = pRef->tagCondLen;
×
573
            cfgRsp.pTagRefs[i].tagCondJson = taosStrdup(pRef->tagCondJson);
×
574
            if (NULL == cfgRsp.pTagRefs[i].tagCondJson) {
×
575
              code = terrno;
×
576
              goto _exit;
×
577
            }
578
          }
579
        }
580
      }
581
    }
582
  }
583

584
  // populate series
585
  if (mer1.me.series.nSeries > 0) {
37,296,494✔
586
    cfgRsp.numOfSeries = mer1.me.series.nSeries;
58,715✔
587
    cfgRsp.pSeries = taosMemoryCalloc(mer1.me.series.nSeries, sizeof(SSeriesEntry));
58,715✔
588
    if (cfgRsp.pSeries) {
58,715✔
589
      for (int32_t i = 0; i < mer1.me.series.nSeries; i++) {
388,431✔
590
        SSeriesEntry *src = &mer1.me.series.pSeries[i];
329,716✔
591
        SSeriesEntry *dst = &cfgRsp.pSeries[i];
329,716✔
592
        tstrncpy(dst->alias, src->alias, TSDB_COL_NAME_LEN);
329,716✔
593
        tstrncpy(dst->sourceName, src->sourceName, TSDB_EXT_SOURCE_NAME_LEN);
329,716✔
594
        tstrncpy(dst->dbName, src->dbName, TSDB_DB_NAME_LEN);
329,716✔
595
        tstrncpy(dst->measurementName, src->measurementName, TSDB_TABLE_NAME_LEN);
329,716✔
596
        if (src->tagCondLen > 0) {
329,716✔
597
          dst->tagCondJson = taosStrdup(src->tagCondJson);
329,716✔
598
          dst->tagCondLen = src->tagCondLen;
329,716✔
599
        } else {
600
          dst->tagCondJson = NULL;
×
601
          dst->tagCondLen = 0;
×
602
        }
603
      }
604
    }
605
  }
606

607
  // encode and send response
608
  rspLen = tSerializeSTableCfgRsp(NULL, 0, &cfgRsp);
37,296,494✔
609
  if (rspLen < 0) {
37,296,494✔
610
    code = terrno;
×
611
    goto _exit;
×
612
  }
613

614
  if (direct) {
37,296,494✔
615
    pRsp = rpcMallocCont(rspLen);
37,258,999✔
616
  } else {
617
    pRsp = taosMemoryCalloc(1, rspLen);
37,495✔
618
  }
619

620
  if (pRsp == NULL) {
37,296,494✔
621
    code = terrno;
×
622
    goto _exit;
×
623
  }
624

625
  rspLen = tSerializeSTableCfgRsp(pRsp, rspLen, &cfgRsp);
37,296,494✔
626
  if (rspLen < 0) {
37,296,494✔
627
    code = terrno;
×
628
    goto _exit;
×
629
  }
630

631
_exit:
37,303,088✔
632
  rpcMsg.info = pMsg->info;
37,303,088✔
633
  rpcMsg.pCont = pRsp;
37,303,088✔
634
  rpcMsg.contLen = rspLen;
37,303,088✔
635
  rpcMsg.code = code;
37,303,088✔
636
  rpcMsg.msgType = pMsg->msgType;
37,303,088✔
637

638
  if (code) {
37,303,088✔
639
    qError("get table %s cfg failed cause of %s", cfgReq.tbName, tstrerror(code));
6,594✔
640
  }
641

642
  if (direct) {
37,303,088✔
643
    tmsgSendRsp(&rpcMsg);
37,265,593✔
644
  } else {
645
    *pMsg = rpcMsg;
37,495✔
646
  }
647

648
  tFreeSTableCfgRsp(&cfgRsp);
37,303,088✔
649
  metaReaderClear(&mer2);
37,303,088✔
650
  metaReaderClear(&mer1);
37,303,088✔
651
  return code;
37,303,088✔
652
}
653

654
static FORCE_INLINE void vnodeFreeSBatchRspMsg(void *p) {
655
  if (NULL == p) {
656
    return;
657
  }
658

659
  SBatchRspMsg *pRsp = (SBatchRspMsg *)p;
660
  rpcFreeCont(pRsp->msg);
661
}
662

663
int32_t vnodeGetBatchMeta(SVnode *pVnode, SRpcMsg *pMsg) {
17,541,629✔
664
  int32_t      code = 0;
17,541,629✔
665
  int32_t      rspSize = 0;
17,541,629✔
666
  SBatchReq    batchReq = {0};
17,541,629✔
667
  SBatchMsg   *req = NULL;
17,545,066✔
668
  SBatchRspMsg rsp = {0};
17,545,066✔
669
  SBatchRsp    batchRsp = {0};
17,526,659✔
670
  SRpcMsg      reqMsg = *pMsg;
17,527,135✔
671
  SRpcMsg      rspMsg = {0};
17,528,341✔
672
  void        *pRsp = NULL;
17,535,471✔
673

674
  if (tDeserializeSBatchReq(pMsg->pCont, pMsg->contLen, &batchReq)) {
17,535,471✔
675
    code = terrno;
×
676
    qError("tDeserializeSBatchReq failed");
×
677
    goto _exit;
×
678
  }
679

680
  int32_t msgNum = taosArrayGetSize(batchReq.pMsgs);
17,530,583✔
681
  if (msgNum >= MAX_META_MSG_IN_BATCH) {
17,538,019✔
682
    code = TSDB_CODE_INVALID_MSG;
×
683
    qError("too many msgs %d in vnode batch meta req", msgNum);
×
684
    goto _exit;
×
685
  }
686

687
  batchRsp.pRsps = taosArrayInit(msgNum, sizeof(SBatchRspMsg));
17,538,019✔
688
  if (NULL == batchRsp.pRsps) {
17,536,657✔
689
    code = terrno;
1,368✔
690
    qError("taosArrayInit %d SBatchRspMsg failed", msgNum);
×
691
    goto _exit;
×
692
  }
693

694
  for (int32_t i = 0; i < msgNum; ++i) {
37,823,312✔
695
    req = taosArrayGet(batchReq.pMsgs, i);
20,277,619✔
696
    if (req == NULL) {
20,272,233✔
697
      code = terrno;
×
698
      goto _exit;
×
699
    }
700

701
    reqMsg.msgType = req->msgType;
20,276,248✔
702
    reqMsg.pCont = req->msg;
20,276,248✔
703
    reqMsg.contLen = req->msgLen;
20,276,248✔
704

705
    switch (req->msgType) {
20,276,248✔
706
      case TDMT_VND_TABLE_META:
10,715,474✔
707
        // error code has been set into reqMsg, no need to handle it here.
708
        if (TSDB_CODE_SUCCESS != vnodeGetTableMeta(pVnode, &reqMsg, false)) {
10,715,474✔
709
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
1,605,979✔
710
        }
711
        break;
10,705,497✔
712
      case TDMT_VND_TABLE_NAME:
73,928✔
713
        // error code has been set into reqMsg, no need to handle it here.
714
        if (TSDB_CODE_SUCCESS != vnodeGetTableMeta(pVnode, &reqMsg, false)) {
73,928✔
715
          qWarn("vnodeGetBatchName failed, msgType:%d", req->msgType);
26,537✔
716
        }
717
        break;
73,928✔
718
      case TDMT_VND_TABLE_CFG:
37,495✔
719
        // error code has been set into reqMsg, no need to handle it here.
720
        if (TSDB_CODE_SUCCESS != vnodeGetTableCfg(pVnode, &reqMsg, false)) {
37,495✔
721
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
×
722
        }
723
        break;
37,495✔
724
      case TDMT_VND_VSUBTABLES_META:
9,449,351✔
725
        // error code has been set into reqMsg, no need to handle it here.
726
        if (TSDB_CODE_SUCCESS != vnodeGetVSubtablesMeta(pVnode, &reqMsg)) {
9,449,351✔
727
          qWarn("vnodeGetVSubtablesMeta failed, msgType:%d", req->msgType);
×
728
        }
729
        break;
9,322,917✔
730
      case TDMT_VND_VSTB_REF_DBS:
×
731
        // error code has been set into reqMsg, no need to handle it here.
732
        if (TSDB_CODE_SUCCESS != vnodeGetVStbRefDbs(pVnode, &reqMsg)) {
×
733
          qWarn("vnodeGetVStbRefDbs failed, msgType:%d", req->msgType);
×
734
        }
735
        break;
×
736
      case TDMT_VND_VTB_TAG_COND:
×
737
        // error code has been set into reqMsg, no need to handle it here.
738
        if (TSDB_CODE_SUCCESS != vnodeGetVTbTagCond(pVnode, &reqMsg, false)) {
×
739
          qWarn("vnodeGetVTbTagCond failed, msgType:%d", req->msgType);
×
740
        }
741
        break;
×
742
      default:
×
743
        qError("invalid req msgType %d", req->msgType);
×
744
        reqMsg.code = TSDB_CODE_INVALID_MSG;
×
745
        reqMsg.pCont = NULL;
×
746
        reqMsg.contLen = 0;
×
747
        break;
×
748
    }
749

750
    rsp.msgIdx = req->msgIdx;
20,284,472✔
751
    rsp.reqType = reqMsg.msgType;
20,284,472✔
752
    rsp.msgLen = reqMsg.contLen;
20,284,472✔
753
    rsp.rspCode = reqMsg.code;
20,284,472✔
754
    rsp.msg = reqMsg.pCont;
20,284,472✔
755

756
    if (NULL == taosArrayPush(batchRsp.pRsps, &rsp)) {
40,427,860✔
757
      qError("taosArrayPush failed");
×
758
      code = terrno;
×
759
      goto _exit;
×
760
    }
761
  }
762

763
  rspSize = tSerializeSBatchRsp(NULL, 0, &batchRsp);
17,545,693✔
764
  if (rspSize < 0) {
17,543,824✔
765
    qError("tSerializeSBatchRsp failed");
×
766
    code = terrno;
×
767
    goto _exit;
×
768
  }
769
  pRsp = rpcMallocCont(rspSize);
17,543,824✔
770
  if (pRsp == NULL) {
17,543,144✔
771
    qError("rpcMallocCont %d failed", rspSize);
×
772
    code = terrno;
×
773
    goto _exit;
×
774
  }
775
  if (tSerializeSBatchRsp(pRsp, rspSize, &batchRsp) < 0) {
17,543,144✔
776
    qError("tSerializeSBatchRsp %d failed", rspSize);
×
777
    code = terrno;
×
778
    goto _exit;
×
779
  }
780

781
_exit:
17,547,613✔
782

783
  rspMsg.info = pMsg->info;
17,548,620✔
784
  rspMsg.pCont = pRsp;
17,548,620✔
785
  rspMsg.contLen = rspSize;
17,548,620✔
786
  rspMsg.code = code;
17,548,620✔
787
  rspMsg.msgType = pMsg->msgType;
17,548,620✔
788

789
  if (code) {
17,548,620✔
790
    qError("vnd get batch meta failed cause of %s", tstrerror(code));
×
791
  }
792

793
  taosArrayDestroyEx(batchReq.pMsgs, tFreeSBatchReqMsg);
17,548,620✔
794
  taosArrayDestroyEx(batchRsp.pRsps, tFreeSBatchRspMsg);
17,546,494✔
795

796
  tmsgSendRsp(&rspMsg);
17,544,497✔
797

798
  return code;
17,548,164✔
799
}
800

801
#define VNODE_DO_META_QUERY(pVnode, cmd)                 \
802
  do {                                                   \
803
    (void)taosThreadRwlockRdlock(&(pVnode)->metaRWLock); \
804
    cmd;                                                 \
805
    (void)taosThreadRwlockUnlock(&(pVnode)->metaRWLock); \
806
  } while (0)
807

808
int32_t vnodeReadVSubtables(SReadHandle* pHandle, int64_t suid, SArray** ppRes) {
9,443,375✔
809
  int32_t                    code = TSDB_CODE_SUCCESS;
9,443,375✔
810
  int32_t                    line = 0;
9,446,319✔
811
  SMetaReader                mr = {0};
9,446,319✔
812
  bool                       readerInit = false;
9,415,746✔
813
  SVCTableRefCols*           pTb = NULL;
9,415,746✔
814
  int32_t                    refColsNum = 0;
9,417,988✔
815
  char                       tbFName[TSDB_TABLE_FNAME_LEN];
9,417,988✔
816
  SSHashObj*                 pSrcTbls = NULL;
9,429,657✔
817

818
  SArray *pList = taosArrayInit(10, sizeof(uint64_t));
9,429,657✔
819
  QUERY_CHECK_NULL(pList, code, line, _return, terrno);
9,436,448✔
820

821
  QUERY_CHECK_CODE(pHandle->api.metaFn.getChildTableList(pHandle->vnode, suid, pList), line, _return);
9,436,448✔
822

823
  size_t num = taosArrayGetSize(pList);
9,450,332✔
824
  *ppRes = taosArrayInit(num, POINTER_BYTES);
9,443,531✔
825
  QUERY_CHECK_NULL(*ppRes, code, line, _return, terrno);
9,452,351✔
826
  pSrcTbls = tSimpleHashInit(10, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
9,452,351✔
827
  QUERY_CHECK_NULL(pSrcTbls, code, line, _return, terrno);
9,442,325✔
828

829
  for (int32_t i = 0; i < num; ++i) {
56,089,910✔
830
    uint64_t* id = taosArrayGet(pList, i);
46,629,455✔
831
    QUERY_CHECK_NULL(id, code, line, _return, terrno);
46,585,114✔
832
    pHandle->api.metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pHandle->api.metaFn, pHandle->txnId);
46,585,114✔
833
    QUERY_CHECK_CODE(pHandle->api.metaReaderFn.getTableEntryByUid(&mr, *id), line, _return);
46,682,969✔
834
    readerInit = true;
56,399,428✔
835

836
    refColsNum = 0;
46,013,649✔
837
    for (int32_t j = 0; j < mr.me.colRef.nCols; j++) {
546,302,314✔
838
      if (mr.me.colRef.pColRef[j].hasRef) {
499,675,122✔
839
        refColsNum++;
286,752,741✔
840
      }
841
    }
842

843
    int32_t tagRefColsNum = 0;
46,013,649✔
844
    for (int32_t j = 0; j < mr.me.colRef.nTagRefs; j++) {
84,116,622✔
845
      if (mr.me.colRef.pTagRef[j].hasRef) {
37,489,430✔
846
        tagRefColsNum++;
24,996,249✔
847
      }
848
    }
849

850
    if (refColsNum <= 0 && tagRefColsNum <= 0) {
46,627,192✔
851
      pHandle->api.metaReaderFn.clearReader(&mr);
7,931,134✔
852
      readerInit = false;
7,931,134✔
853
      continue;
7,931,134✔
854
    }
855

856
    pTb = taosMemoryCalloc(1, (refColsNum + tagRefColsNum) * sizeof(SRefColInfo) + sizeof(*pTb));
38,696,058✔
857
    QUERY_CHECK_NULL(pTb, code, line, _return, terrno);
38,552,239✔
858

859
    pTb->uid = mr.me.uid;
38,552,239✔
860
    pTb->numOfColRefs = refColsNum;
38,552,239✔
861
    pTb->refCols = (SRefColInfo*)(pTb + 1);
38,552,239✔
862
    pTb->numOfTagRefs = tagRefColsNum;
38,552,239✔
863
    pTb->tagRefCols = pTb->refCols + refColsNum;
38,552,239✔
864

865
    refColsNum = 0;
38,552,239✔
866
    tSimpleHashClear(pSrcTbls);
38,552,239✔
867
    for (int32_t j = 0; j < mr.me.colRef.nCols; j++) {
412,109,129✔
868
      if (!mr.me.colRef.pColRef[j].hasRef) {
373,360,804✔
869
        continue;
86,376,013✔
870
      }
871

872
      pTb->refCols[refColsNum].colId = mr.me.colRef.pColRef[j].id;
286,984,791✔
873
      tstrncpy(pTb->refCols[refColsNum].refColName, mr.me.colRef.pColRef[j].refColName, TSDB_COL_NAME_LEN);
286,984,791✔
874
      tstrncpy(pTb->refCols[refColsNum].refTableName, mr.me.colRef.pColRef[j].refTableName, TSDB_TABLE_NAME_LEN);
286,984,791✔
875
      tstrncpy(pTb->refCols[refColsNum].refDbName, mr.me.colRef.pColRef[j].refDbName, TSDB_DB_NAME_LEN);
286,984,791✔
876
      tstrncpy(pTb->refCols[refColsNum].refSourceName, mr.me.colRef.pColRef[j].refSourceName, TSDB_EXT_SOURCE_NAME_LEN);
286,984,791✔
877

878
      snprintf(tbFName, sizeof(tbFName), "%s.%s", pTb->refCols[refColsNum].refDbName, pTb->refCols[refColsNum].refTableName);
286,984,791✔
879

880
      if (NULL == tSimpleHashGet(pSrcTbls, tbFName, strlen(tbFName))) {
286,984,791✔
881
        QUERY_CHECK_CODE(tSimpleHashPut(pSrcTbls, tbFName, strlen(tbFName), &code, sizeof(code)), line, _return);
59,318,465✔
882
      }
883

884
      refColsNum++;
286,945,357✔
885
    }
886

887
    // Fill tag refs
888
    tagRefColsNum = 0;
38,338,576✔
889
    for (int32_t j = 0; j < mr.me.colRef.nTagRefs; j++) {
76,330,152✔
890
      if (!mr.me.colRef.pTagRef[j].hasRef) {
37,534,387✔
891
        continue;
12,513,524✔
892
      }
893

894
      pTb->tagRefCols[tagRefColsNum].colId = mr.me.colRef.pTagRef[j].id;
25,020,863✔
895
      tstrncpy(pTb->tagRefCols[tagRefColsNum].refColName, mr.me.colRef.pTagRef[j].refColName, TSDB_COL_NAME_LEN);
25,020,863✔
896
      tstrncpy(pTb->tagRefCols[tagRefColsNum].refTableName, mr.me.colRef.pTagRef[j].refTableName, TSDB_TABLE_NAME_LEN);
25,020,863✔
897
      tstrncpy(pTb->tagRefCols[tagRefColsNum].refDbName, mr.me.colRef.pTagRef[j].refDbName, TSDB_DB_NAME_LEN);
25,020,863✔
898
      tstrncpy(pTb->tagRefCols[tagRefColsNum].refSourceName, mr.me.colRef.pTagRef[j].refSourceName, TSDB_EXT_SOURCE_NAME_LEN);
25,020,863✔
899

900
      snprintf(tbFName, sizeof(tbFName), "%s.%s", pTb->tagRefCols[tagRefColsNum].refDbName, pTb->tagRefCols[tagRefColsNum].refTableName);
25,020,863✔
901

902
      if (NULL == tSimpleHashGet(pSrcTbls, tbFName, strlen(tbFName))) {
25,020,863✔
903
        QUERY_CHECK_CODE(tSimpleHashPut(pSrcTbls, tbFName, strlen(tbFName), &code, sizeof(code)), line, _return);
560,180✔
904
      }
905

906
      tagRefColsNum++;
25,068,117✔
907
    }
908

909
    pTb->numOfSrcTbls = tSimpleHashGetSize(pSrcTbls);
38,795,765✔
910
    QUERY_CHECK_NULL(taosArrayPush(*ppRes, &pTb), code, line, _return, terrno);
77,163,840✔
911
    pTb = NULL;
38,751,105✔
912

913
    pHandle->api.metaReaderFn.clearReader(&mr);
38,751,105✔
914
    readerInit = false;
38,716,327✔
915
  }
916

917
_return:
9,460,455✔
918

919
  if (readerInit) {
9,322,735✔
920
    pHandle->api.metaReaderFn.clearReader(&mr);
×
921
  }
922

923
  taosArrayDestroy(pList);
9,461,265✔
924
  taosMemoryFree(pTb);
9,461,447✔
925
  tSimpleHashCleanup(pSrcTbls);
9,461,601✔
926

927
  if (code) {
9,460,316✔
928
    qError("%s failed since %s", __func__, tstrerror(code));
×
929
  }
930
  return code;
9,460,316✔
931
}
932

933
int32_t vnodeReadVStbRefDbs(SReadHandle* pHandle, int64_t suid, SArray** ppDbs, SArray** ppExtSources) {
×
934
  int32_t                    code = TSDB_CODE_SUCCESS;
×
935
  int32_t                    line = 0;
×
936
  SMetaReader                mr = {0};
×
937
  bool                       readerInit = false;
×
938
  SSHashObj*                 pDbNameHash = NULL;
×
939
  SSHashObj*                 pExtSourceHash = NULL;
×
940
  SArray*                    pList = NULL;
×
941

942
  pList = taosArrayInit(10, sizeof(uint64_t));
×
943
  QUERY_CHECK_NULL(pList, code, line, _return, terrno);
×
944

945
  *ppDbs = taosArrayInit(10, POINTER_BYTES);
×
946
  QUERY_CHECK_NULL(*ppDbs, code, line, _return, terrno)
×
947

948
  *ppExtSources = taosArrayInit(4, POINTER_BYTES);
×
949
  QUERY_CHECK_NULL(*ppExtSources, code, line, _return, terrno)
×
950
  
951
  // lookup in cache
952
  code = pHandle->api.metaFn.metaGetCachedRefDbs(pHandle->vnode, suid, *ppDbs);
×
953
  QUERY_CHECK_CODE(code, line, _return);
×
954

955
  if (taosArrayGetSize(*ppDbs) > 0) {
×
956
    // found in cache, also get ext sources from cache
957
    code = pHandle->api.metaFn.metaGetCachedExtSources(pHandle->vnode, suid, *ppExtSources);
×
958
    QUERY_CHECK_CODE(code, line, _return);
×
959
    goto _return;
×
960
  } else {
961
    code = pHandle->api.metaFn.getChildTableList(pHandle->vnode, suid, pList);
×
962
    QUERY_CHECK_CODE(code, line, _return);
×
963

964
    size_t num = taosArrayGetSize(pList);
×
965
    pDbNameHash = tSimpleHashInit(10, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
×
966
    QUERY_CHECK_NULL(pDbNameHash, code, line, _return, terrno);
×
967

968
    pExtSourceHash = tSimpleHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
×
969
    QUERY_CHECK_NULL(pExtSourceHash, code, line, _return, terrno);
×
970

971
    for (int32_t i = 0; i < num; ++i) {
×
972
      uint64_t* id = taosArrayGet(pList, i);
×
973
      QUERY_CHECK_NULL(id, code, line, _return, terrno);
×
974

975
      pHandle->api.metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pHandle->api.metaFn, pHandle->txnId);
×
976
      readerInit = true;
×
977

978
      code = pHandle->api.metaReaderFn.getTableEntryByUid(&mr, *id);
×
979
      QUERY_CHECK_CODE(code, line, _return);
×
980

981
      for (int32_t j = 0; j < mr.me.colRef.nCols; j++) {
×
982
        if (mr.me.colRef.pColRef[j].hasRef) {
×
983
          if (mr.me.colRef.pColRef[j].refSourceName[0] != '\0') {
×
984
            const char* srcName = mr.me.colRef.pColRef[j].refSourceName;
×
985
            if (NULL == tSimpleHashGet(pExtSourceHash, srcName, strlen(srcName))) {
×
986
              char *dup = taosStrdup(srcName);
×
987
              QUERY_CHECK_NULL(dup, code, line, _return, terrno);
×
988
              QUERY_CHECK_NULL(taosArrayPush(*ppExtSources, &dup), code, line, _return, terrno);
×
989
              code = tSimpleHashPut(pExtSourceHash, srcName, strlen(srcName), NULL, 0);
×
990
              QUERY_CHECK_CODE(code, line, _return);
×
991
            }
992
          } else {
993
            if (NULL == tSimpleHashGet(pDbNameHash, mr.me.colRef.pColRef[j].refDbName, strlen(mr.me.colRef.pColRef[j].refDbName))) {
×
994
              char *refDbName = taosStrdup(mr.me.colRef.pColRef[j].refDbName);
×
995
              QUERY_CHECK_NULL(refDbName, code, line, _return, terrno);
×
996
              QUERY_CHECK_NULL(taosArrayPush(*ppDbs, &refDbName), code, line, _return, terrno);
×
997
              code = tSimpleHashPut(pDbNameHash, refDbName, strlen(refDbName), NULL, 0);
×
998
              QUERY_CHECK_CODE(code, line, _return);
×
999
            }
1000
          }
1001
        }
1002
      }
1003

1004
      pHandle->api.metaReaderFn.clearReader(&mr);
×
1005
      readerInit = false;
×
1006
    }
1007

1008
    code = pHandle->api.metaFn.metaPutRefDbsToCache(pHandle->vnode, suid, *ppDbs);
×
1009
    QUERY_CHECK_CODE(code, line, _return);
×
1010

1011
    code = pHandle->api.metaFn.metaPutExtSourcesToCache(pHandle->vnode, suid, *ppExtSources);
×
1012
    QUERY_CHECK_CODE(code, line, _return);
×
1013
  }
1014

1015
_return:
×
1016

1017
  if (readerInit) {
×
1018
    pHandle->api.metaReaderFn.clearReader(&mr);
×
1019
  }
1020

1021
  taosArrayDestroy(pList);
×
1022
  tSimpleHashCleanup(pDbNameHash);
×
1023
  tSimpleHashCleanup(pExtSourceHash);
×
1024

1025
  if (code) {
×
1026
    qError("%s failed since %s", __func__, tstrerror(code));
×
1027
  }
1028
  return code;
×
1029
}
1030

1031
int32_t vnodeGetVSubtablesMeta(SVnode *pVnode, SRpcMsg *pMsg) {
9,445,228✔
1032
  int32_t        code = 0;
9,445,228✔
1033
  int32_t        rspSize = 0;
9,445,228✔
1034
  SVSubTablesReq req = {0};
9,445,228✔
1035
  SVSubTablesRsp rsp = {0};
9,421,996✔
1036
  SRpcMsg      rspMsg = {0};
9,413,032✔
1037
  void        *pRsp = NULL;
9,413,032✔
1038
  int32_t      line = 0;
9,413,032✔
1039

1040
  if (tDeserializeSVSubTablesReq(pMsg->pCont, pMsg->contLen, &req)) {
9,413,032✔
1041
    code = terrno;
×
1042
    qError("tDeserializeSVSubTablesReq failed");
×
1043
    goto _return;
×
1044
  }
1045

1046
  SReadHandle handle = {0};
9,448,562✔
1047
  handle.vnode = pVnode;
9,449,136✔
1048
  initStorageAPI(&handle.api);
9,449,136✔
1049

1050
  QUERY_CHECK_CODE(vnodeReadVSubtables(&handle, req.suid, &rsp.pTables), line, _return);
9,428,099✔
1051
  rsp.vgId = TD_VID(pVnode);
9,461,447✔
1052

1053
  rspSize = tSerializeSVSubTablesRsp(NULL, 0, &rsp);
9,461,447✔
1054
  if (rspSize < 0) {
9,459,993✔
1055
    code = rspSize;
×
1056
    qError("tSerializeSVSubTablesRsp failed, error:%d", rspSize);
×
1057
    goto _return;
×
1058
  }
1059
  pRsp = taosMemoryCalloc(1, rspSize);
9,459,993✔
1060
  if (pRsp == NULL) {
9,457,738✔
1061
    code = terrno;
×
1062
    qError("rpcMallocCont %d failed, error:%d", rspSize, terrno);
×
1063
    goto _return;
×
1064
  }
1065
  rspSize = tSerializeSVSubTablesRsp(pRsp, rspSize, &rsp);
9,457,738✔
1066
  if (rspSize < 0) {
9,461,573✔
1067
    code = rspSize;
×
1068
    qError("tSerializeSVSubTablesRsp failed, error:%d", rspSize);
×
1069
    goto _return;
×
1070
  }
1071

1072
_return:
9,461,573✔
1073

1074
  rspMsg.info = pMsg->info;
9,460,599✔
1075
  rspMsg.pCont = pRsp;
9,460,599✔
1076
  rspMsg.contLen = rspSize;
9,460,599✔
1077
  rspMsg.code = code;
9,460,599✔
1078
  rspMsg.msgType = pMsg->msgType;
9,460,599✔
1079

1080
  if (code) {
9,460,599✔
1081
    qError("vnd get virtual subtables failed cause of %s", tstrerror(code));
×
1082
  }
1083

1084
  *pMsg = rspMsg;
9,460,599✔
1085
  
1086
  tDestroySVSubTablesRsp(&rsp);
9,460,599✔
1087

1088
  //tmsgSendRsp(&rspMsg);
1089

1090
  return code;
9,460,304✔
1091
}
1092

1093
int32_t vnodeProcessCheckHasCtbReq(SVnode *pVnode, SRpcMsg *pMsg) {
49,529✔
1094
  SVCheckHasCtbReq req = {0};
49,529✔
1095
  int32_t          code = 0;
49,529✔
1096
  void            *pBuf = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
49,529✔
1097
  int32_t          bufLen = pMsg->contLen - sizeof(SMsgHead);
49,529✔
1098

1099
  code = tDeserializeSVCheckHasCtbReq(pBuf, bufLen, &req);
49,529✔
1100
  if (code != 0) {
49,529✔
1101
    vError("vgId:%d, failed to deserialize check-has-ctb req", TD_VID(pVnode));
×
1102
    goto _exit;
×
1103
  }
1104

1105
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, req.suid, 0, 0);
49,529✔
1106
  if (pCur == NULL) {
49,529✔
1107
    code = (terrno != 0) ? terrno : TSDB_CODE_OUT_OF_MEMORY;
×
1108
    vError("vgId:%d, failed to open ctb cursor for suid:%" PRId64 ", code:0x%x", TD_VID(pVnode), req.suid, code);
×
1109
  } else {
1110
    tb_uid_t id = metaCtbCursorNext(pCur);
49,529✔
1111
    metaCloseCtbCursor(pCur);
49,529✔
1112
    if (id != 0) {
49,529✔
1113
      code = TSDB_CODE_MND_VST_PARENT_HAS_VCT;
775✔
1114
    }
1115
  }
1116

1117
_exit:;
49,529✔
1118
  SRpcMsg rspMsg = {.info = pMsg->info, .code = code};
49,529✔
1119
  tmsgSendRsp(&rspMsg);
49,529✔
1120
  return 0;  // return 0 so fetch worker doesn't double-send response
49,529✔
1121
}
1122

1123
int32_t vnodeGetVStbRefDbs(SVnode *pVnode, SRpcMsg *pMsg) {
×
1124
  int32_t        code = 0;
×
1125
  int32_t        rspSize = 0;
×
1126
  SVStbRefDbsReq req = {0};
×
1127
  SVStbRefDbsRsp rsp = {0};
×
1128
  SRpcMsg        rspMsg = {0};
×
1129
  void          *pRsp = NULL;
×
1130
  int32_t        line = 0;
×
1131

1132
  if (tDeserializeSVStbRefDbsReq(pMsg->pCont, pMsg->contLen, &req)) {
×
1133
    code = terrno;
×
1134
    qError("tDeserializeSVSubTablesReq failed");
×
1135
    goto _return;
×
1136
  }
1137

1138
  SReadHandle handle = {0};
×
1139
  handle.vnode = pVnode;
×
1140
  initStorageAPI(&handle.api);
×
1141

1142
  code = vnodeReadVStbRefDbs(&handle, req.suid, &rsp.pDbs, &rsp.pExtSources);
×
1143
  QUERY_CHECK_CODE(code, line, _return);
×
1144
  rsp.vgId = TD_VID(pVnode);
×
1145

1146
  rspSize = tSerializeSVStbRefDbsRsp(NULL, 0, &rsp);
×
1147
  if (rspSize < 0) {
×
1148
    code = rspSize;
×
1149
    qError("tSerializeSVStbRefDbsRsp failed, error:%d", rspSize);
×
1150
    goto _return;
×
1151
  }
1152
  pRsp = taosMemoryCalloc(1, rspSize);
×
1153
  if (pRsp == NULL) {
×
1154
    code = terrno;
×
1155
    qError("rpcMallocCont %d failed, error:%d", rspSize, terrno);
×
1156
    goto _return;
×
1157
  }
1158
  rspSize = tSerializeSVStbRefDbsRsp(pRsp, rspSize, &rsp);
×
1159
  if (rspSize < 0) {
×
1160
    code = rspSize;
×
1161
    qError("tSerializeSVStbRefDbsRsp failed, error:%d", rspSize);
×
1162
    goto _return;
×
1163
  }
1164

1165
_return:
×
1166

1167
  rspMsg.info = pMsg->info;
×
1168
  rspMsg.pCont = pRsp;
×
1169
  rspMsg.contLen = rspSize;
×
1170
  rspMsg.code = code;
×
1171
  rspMsg.msgType = pMsg->msgType;
×
1172

1173
  if (code) {
×
1174
    qError("vnd get virtual stb ref db failed cause of %s", tstrerror(code));
×
1175
  }
1176

1177
  *pMsg = rspMsg;
×
1178

1179
  tDestroySVStbRefDbsRsp(&rsp);
×
1180

1181
  return code;
×
1182
}
1183

1184
int32_t vnodeGetVTbTagCond(SVnode *pVnode, SRpcMsg *pMsg, bool direct) {
2,175,601✔
1185
  int32_t      code = 0;
2,175,601✔
1186
  int32_t      line = 0;
2,175,601✔
1187
  int32_t      rspSize = 0;
2,175,601✔
1188
  SVTagCondReq req = {0};
2,175,601✔
1189
  SVTagCondRsp rsp = {0};
2,175,601✔
1190
  SRpcMsg      rspMsg = {0};
2,175,601✔
1191
  void        *pRsp = NULL;
2,175,601✔
1192
  SMetaReader  mer = {0};
2,175,601✔
1193

1194
  if (tDeserializeSVTagCondReq(pMsg->pCont, pMsg->contLen, &req)) {
2,175,601✔
1195
    code = terrno;
×
1196
    qError("tDeserializeSVTagCondReq failed");
×
1197
    goto _return;
×
1198
  }
1199

1200
  metaReaderDoInit(&mer, pVnode->pMeta, META_READER_LOCK, 0);
2,175,601✔
1201
  code = metaReaderGetTableEntryByUid(&mer, req.uid);
2,175,601✔
1202
  QUERY_CHECK_CODE(code, line, _return);
2,175,601✔
1203

1204
  rsp.pEntries = taosArrayInit(8, sizeof(SVTagCondEntry));
2,175,601✔
1205
  QUERY_CHECK_NULL(rsp.pEntries, code, line, _return, terrno);
2,175,601✔
1206

1207
  if (hasColRef(mer.me.type)) {
2,175,601✔
1208
    SColRefWrapper *p = &(mer.me.colRef);
1,799,509✔
1209
    for (int32_t i = 0; i < p->nCols; ++i) {
36,933,528✔
1210
      SColRef *pColRef = &p->pColRef[i];
34,757,927✔
1211
      if (pColRef->hasRef && pColRef->tagCondLen > 0 && pColRef->tagCondJson != NULL) {
34,757,927✔
1212
        SVTagCondEntry entry = {0};
1,859✔
1213
        entry.colId = pColRef->id;
1,859✔
1214
        entry.tagCondLen = pColRef->tagCondLen;
1,859✔
1215
        entry.tagCondJson = taosStrdup(pColRef->tagCondJson);
1,859✔
1216
        QUERY_CHECK_NULL(entry.tagCondJson, code, line, _return, terrno);
1,859✔
1217
        if (taosArrayPush(rsp.pEntries, &entry) == NULL) {
3,718✔
1218
          taosMemoryFree(entry.tagCondJson);
×
1219
          code = terrno;
×
1220
          QUERY_CHECK_CODE(code, line, _return);
×
1221
        }
1222
      }
1223
    }
1224
  }
1225
  rsp.numOfRefs = taosArrayGetSize(rsp.pEntries);
2,175,601✔
1226

1227
  rspSize = tSerializeSVTagCondRsp(NULL, 0, &rsp);
2,175,601✔
1228
  if (rspSize < 0) {
2,175,601✔
1229
    code = rspSize;
×
1230
    qError("tSerializeSVTagCondRsp failed, error:%d", rspSize);
×
1231
    goto _return;
×
1232
  }
1233
  if (direct) {
2,175,601✔
1234
    pRsp = rpcMallocCont(rspSize);
2,175,601✔
1235
  } else {
1236
    pRsp = taosMemoryCalloc(1, rspSize);
×
1237
  }
1238
  QUERY_CHECK_NULL(pRsp, code, line, _return, terrno);
2,175,601✔
1239
  rspSize = tSerializeSVTagCondRsp(pRsp, rspSize, &rsp);
2,175,601✔
1240
  if (rspSize < 0) {
2,175,601✔
1241
    code = rspSize;
×
1242
    qError("tSerializeSVTagCondRsp failed, error:%d", rspSize);
×
1243
    goto _return;
×
1244
  }
1245

1246
_return:
2,175,601✔
1247
  metaReaderClear(&mer);
2,175,601✔
1248
  rspMsg.info = pMsg->info;
2,175,601✔
1249
  rspMsg.pCont = pRsp;
2,175,601✔
1250
  rspMsg.contLen = (code == 0) ? rspSize : 0;
2,175,601✔
1251
  rspMsg.code = code;
2,175,601✔
1252
  rspMsg.msgType = pMsg->msgType;
2,175,601✔
1253
  if (code) {
2,175,601✔
1254
    qError("vnodeGetVTbTagCond failed since %s", tstrerror(code));
×
1255
  }
1256
  if (direct) {
2,175,601✔
1257
    tmsgSendRsp(&rspMsg);
2,175,601✔
1258
  } else {
1259
    *pMsg = rspMsg;
×
1260
  }
1261
  tDestroySVTagCondRsp(&rsp);
2,175,601✔
1262
  return code;
2,175,601✔
1263
}
1264

1265
static int32_t vnodeGetCompStorage(SVnode *pVnode, int64_t *output) {
35,538,193✔
1266
  int32_t code = 0;
35,538,193✔
1267
#ifdef TD_ENTERPRISE
1268
  int32_t now = taosGetTimestampSec();
35,538,193✔
1269
  if (llabs(now - pVnode->config.vndStats.storageLastUpd) >= 30) {
35,538,193✔
1270
    pVnode->config.vndStats.storageLastUpd = now;
2,019,468✔
1271

1272
    SDbSizeStatisInfo info = {0};
2,019,468✔
1273
    if (0 == (code = vnodeGetDBSize(pVnode, &info))) {
2,019,468✔
1274
      int64_t compSize =
2,019,468✔
1275
          info.l1Size + info.l2Size + info.l3Size + info.cacheSize + info.walSize + info.metaSize + +info.ssSize;
2,019,468✔
1276
      if (compSize >= 0) {
2,019,468✔
1277
        pVnode->config.vndStats.compStorage = compSize;
2,019,468✔
1278
      } else {
1279
        vError("vnode get comp storage failed since compSize is negative:%" PRIi64, compSize);
×
1280
        code = TSDB_CODE_APP_ERROR;
×
1281
      }
1282
    } else {
1283
      vWarn("vnode get comp storage failed since %s", tstrerror(code));
2,660✔
1284
    }
1285
  }
1286
  if (output) *output = pVnode->config.vndStats.compStorage;
35,538,193✔
1287
#endif
1288
  return code;
35,538,193✔
1289
}
1290

1291
static void vnodeGetBufferInfo(SVnode *pVnode, int64_t *bufferSegmentUsed, int64_t *bufferSegmentSize) {
35,538,193✔
1292
  *bufferSegmentUsed = 0;
35,538,193✔
1293
  *bufferSegmentSize = 0;
35,538,193✔
1294
  if (pVnode) {
35,538,193✔
1295
    (void)taosThreadMutexLock(&pVnode->mutex);
35,538,193✔
1296

1297
    if (pVnode->inUse) {
35,538,193✔
1298
      *bufferSegmentUsed = pVnode->inUse->size;
35,530,145✔
1299
    }
1300
    *bufferSegmentSize = pVnode->config.szBuf / VNODE_BUFPOOL_SEGMENTS;
35,538,193✔
1301

1302
    (void)taosThreadMutexUnlock(&pVnode->mutex);
35,538,193✔
1303
  }
1304
}
35,538,193✔
1305

1306
int32_t vnodeGetLoad(SVnode *pVnode, SVnodeLoad *pLoad) {
35,538,193✔
1307
  SSyncState state = syncGetState(pVnode->sync);
35,538,193✔
1308
  pLoad->syncAppliedIndex = pVnode->state.applied;
35,538,193✔
1309
  syncGetCommitIndex(pVnode->sync, &pLoad->syncCommitIndex);
35,538,193✔
1310

1311
  pLoad->vgId = TD_VID(pVnode);
35,538,193✔
1312
  pLoad->syncState = state.state;
35,538,193✔
1313
  pLoad->syncRestore = state.restored;
35,538,193✔
1314
  pLoad->syncTerm = state.term;
35,538,193✔
1315
  pLoad->roleTimeMs = state.roleTimeMs;
35,538,193✔
1316
  pLoad->startTimeMs = state.startTimeMs;
35,538,193✔
1317
  pLoad->syncCanRead = state.canRead;
35,538,193✔
1318
  pLoad->cacheUsage = tsdbCacheGetUsage(pVnode);
35,538,193✔
1319
  pLoad->numOfCachedTables = tsdbCacheGetElems(pVnode);
35,538,193✔
1320
  VNODE_DO_META_QUERY(pVnode, pLoad->numOfTables = metaGetTbNum(pVnode->pMeta));
35,538,193✔
1321
  VNODE_DO_META_QUERY(pVnode, pLoad->numOfTimeSeries = metaGetTimeSeriesNum(pVnode->pMeta, 1));
35,538,193✔
1322
  pLoad->totalStorage = (int64_t)3 * 1073741824;  // TODO
35,538,193✔
1323
  (void)vnodeGetCompStorage(pVnode, &pLoad->compStorage);
35,538,193✔
1324
  pLoad->pointsWritten = 100;
35,538,193✔
1325
  pLoad->numOfSelectReqs = 1;
35,538,193✔
1326
  pLoad->numOfInsertReqs = atomic_load_64(&pVnode->statis.nInsert);
35,538,193✔
1327
  pLoad->numOfInsertSuccessReqs = atomic_load_64(&pVnode->statis.nInsertSuccess);
35,538,193✔
1328
  pLoad->numOfBatchInsertReqs = atomic_load_64(&pVnode->statis.nBatchInsert);
35,538,193✔
1329
  pLoad->numOfBatchInsertSuccessReqs = atomic_load_64(&pVnode->statis.nBatchInsertSuccess);
35,538,193✔
1330
  vnodeGetBufferInfo(pVnode, &pLoad->bufferSegmentUsed, &pLoad->bufferSegmentSize);
35,538,193✔
1331
  vDebug("vgId:%d, get vnode load, state:%s snapSeq:%d, learnerProgress:%d, totalIndex:%" PRId64, TD_VID(pVnode),
35,538,193✔
1332
         syncStr(state.state), state.snapSeq, state.progress, state.totalIndex);
1333
  pLoad->learnerProgress = state.progress;
35,538,193✔
1334
  pLoad->snapSeq = state.snapSeq;
35,538,193✔
1335
  pLoad->syncTotalIndex = state.totalIndex;
35,538,193✔
1336
  pLoad->snapshotSending = syncSnapshotSending(pVnode->sync) ? 1 : 0;
35,538,193✔
1337
  // Report per-target snapshot-send progress, so the mnode can display progress and compute the rate per target follower dnodeId.
1338
  // Fetch the byte summary grouped by destDnodeId from tsdb, and convert each entry into an SVnodeSnapProgress filled into pLoad->pSnapProgress.
1339
  pLoad->pSnapProgress = NULL;
35,538,193✔
1340
  SArray *pSnapGroups = taosArrayInit(4, sizeof(STsdbSnapSendGroup));
35,538,193✔
1341
  if (pSnapGroups != NULL) {
35,538,193✔
1342
    tsdbGetSnapSendSummary(pVnode->pTsdb, pSnapGroups);
35,538,193✔
1343
    int32_t numGroups = (int32_t)taosArrayGetSize(pSnapGroups);
35,538,193✔
1344
    if (numGroups > 0) {
35,538,193✔
1345
      pLoad->pSnapProgress = taosArrayInit(numGroups, sizeof(SVnodeSnapProgress));
1,894✔
1346
      if (pLoad->pSnapProgress != NULL) {
1,894✔
1347
        for (int32_t g = 0; g < numGroups; g++) {
3,788✔
1348
          STsdbSnapSendGroup *pGroup = (STsdbSnapSendGroup *)taosArrayGet(pSnapGroups, g);
1,894✔
1349
          if (pGroup == NULL) continue;
1,894✔
1350
          SVnodeSnapProgress prog = {.destDnodeId = pGroup->destDnodeId,
1,894✔
1351
                                     .snapTotalSize = pGroup->total,
1,894✔
1352
                                     .snapTransferredSize = pGroup->transferred};
1,894✔
1353
          if (taosArrayPush(pLoad->pSnapProgress, &prog) == NULL) {
3,788✔
1354
            vError("vgId:%d, failed to push snap progress for destDnodeId:%d", TD_VID(pVnode), pGroup->destDnodeId);
×
1355
          }
1356
        }
1357
        vDebug("vgId:%d, report snap send progress, numGroups:%d", TD_VID(pVnode), numGroups);
1,894✔
1358
      } else {
1359
        vError("vgId:%d, failed to alloc pSnapProgress array for vnode load", TD_VID(pVnode));
×
1360
      }
1361
    }
1362
    taosArrayDestroy(pSnapGroups);
35,538,193✔
1363
  } else {
1364
    vError("vgId:%d, failed to alloc snap send groups array for vnode load", TD_VID(pVnode));
×
1365
  }
1366
  return 0;
35,538,193✔
1367
}
1368

1369
int32_t vnodeGetLoadLite(SVnode *pVnode, SVnodeLoadLite *pLoad) {
×
1370
  SSyncState syncState = syncGetState(pVnode->sync);
×
1371
  if (syncState.state == TAOS_SYNC_STATE_LEADER || syncState.state == TAOS_SYNC_STATE_ASSIGNED_LEADER) {
×
1372
    pLoad->vgId = TD_VID(pVnode);
×
1373
    pLoad->nTimeSeries = metaGetTimeSeriesNum(pVnode->pMeta, 1);
×
1374
    return 0;
×
1375
  }
1376
  return -1;
×
1377
}
1378
/**
1379
 * @brief Reset the statistics value by monitor interval
1380
 *
1381
 * @param pVnode
1382
 * @param pLoad
1383
 */
1384
void vnodeResetLoad(SVnode *pVnode, SVnodeLoad *pLoad) {
138✔
1385
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nInsert, pLoad->numOfInsertReqs, 64, "nInsert");
138✔
1386
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nInsertSuccess, pLoad->numOfInsertSuccessReqs, 64, "nInsertSuccess");
138✔
1387
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nBatchInsert, pLoad->numOfBatchInsertReqs, 64, "nBatchInsert");
138✔
1388
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nBatchInsertSuccess, pLoad->numOfBatchInsertSuccessReqs, 64,
138✔
1389
                            "nBatchInsertSuccess");
1390
}
138✔
1391

1392
void vnodeGetInfo(void *pVnode, const char **dbname, int32_t *vgId, int64_t *numOfTables, int64_t *numOfNormalTables) {
29,832,693✔
1393
  SVnode    *pVnodeObj = pVnode;
29,832,693✔
1394
  SVnodeCfg *pConf = &pVnodeObj->config;
29,832,693✔
1395

1396
  if (dbname) {
29,832,693✔
1397
    *dbname = pConf->dbname;
28,537,253✔
1398
  }
1399

1400
  if (vgId) {
29,832,693✔
1401
    *vgId = TD_VID(pVnodeObj);
29,244,045✔
1402
  }
1403

1404
  if (numOfTables) {
29,832,693✔
1405
    *numOfTables = pConf->vndStats.numOfNTables + pConf->vndStats.numOfCTables +
3,936✔
1406
                   pConf->vndStats.numOfVTables + pConf->vndStats.numOfVCTables;
3,936✔
1407
  }
1408

1409
  if (numOfNormalTables) {
29,832,693✔
1410
    *numOfNormalTables = pConf->vndStats.numOfNTables +
2,505✔
1411
                         pConf->vndStats.numOfVTables;
2,505✔
1412
  }
1413
}
29,832,693✔
1414

1415
int8_t vnodeGetSecurityLevel(void *pVnode) {
429,864✔
1416
  SVnode *pVnodeObj = pVnode;
429,864✔
1417
  return pVnodeObj->config.securityLevel;
429,864✔
1418
}
1419

1420
bool vnodeHasPendingTxnEntries(void *pVnode) {
×
1421
  SVnode *pVnodeObj = pVnode;
×
1422
  return metaHasPendingTxnEntries(pVnodeObj->pMeta);
×
1423
}
1424

1425
int32_t vnodeGetTableList(void *pVnode, int8_t type, SArray *pList) {
2,505✔
1426
  if (type == TSDB_SUPER_TABLE) {
2,505✔
1427
    return vnodeGetStbIdList(pVnode, 0, pList);
2,505✔
1428
  } else {
1429
    return TSDB_CODE_INVALID_PARA;
×
1430
  }
1431
}
1432

1433
int32_t vnodeGetAllTableList(SVnode *pVnode, uint64_t uid, SArray *list) {
×
1434
  int32_t      code = TSDB_CODE_SUCCESS;
×
1435
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, uid, 1, 0);
×
1436
  if (NULL == pCur) {
×
1437
    qError("vnode get all table list failed");
×
1438
    return terrno;
×
1439
  }
1440

1441
  while (1) {
×
1442
    tb_uid_t id = metaCtbCursorNext(pCur);
×
1443
    if (id == 0) {
×
1444
      break;
×
1445
    }
1446

1447
    STableKeyInfo info = {uid = id};
×
1448
    if (NULL == taosArrayPush(list, &info)) {
×
1449
      qError("taosArrayPush failed");
×
1450
      code = terrno;
×
1451
      goto _exit;
×
1452
    }
1453
  }
1454
_exit:
×
1455
  metaCloseCtbCursor(pCur);
×
1456
  return code;
×
1457
}
1458

1459
int32_t vnodeGetCtbIdListByFilter(SVnode *pVnode, int64_t suid, SArray *list, bool (*filter)(void *arg), void *arg) {
×
1460
  return 0;
×
1461
}
1462

1463
int32_t vnodeGetCtbIdList(void *pVnode, int64_t suid, SArray *list) {
87,296,454✔
1464
  int32_t      code = TSDB_CODE_SUCCESS;
87,296,454✔
1465
  SVnode      *pVnodeObj = pVnode;
87,296,454✔
1466
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnodeObj, suid, 1, 0);
87,296,454✔
1467
  if (NULL == pCur) {
87,257,946✔
1468
    qError("vnode get ctb id list failed, suid:%" PRId64, suid);
×
1469
    return terrno;
×
1470
  }
1471

1472
  while (1) {
250,201,430✔
1473
    tb_uid_t id = metaCtbCursorNext(pCur);
337,459,376✔
1474
    if (id == 0) {
336,725,551✔
1475
      break;
86,986,663✔
1476
    }
1477
    qTrace("vnodeGetCtbIdList: got ctb id %" PRId64 " for suid %" PRId64, id, suid);
249,392,650✔
1478
    if (NULL == taosArrayPush(list, &id)) {
250,117,448✔
1479
      qError("taosArrayPush failed");
×
1480
      code = terrno;
×
1481
      goto _exit;
×
1482
    }
1483
  }
1484

1485
_exit:
87,176,955✔
1486
  metaCloseCtbCursor(pCur);
87,176,955✔
1487
  return code;
87,255,943✔
1488
}
1489

1490
int32_t vnodeGetStbIdList(SVnode *pVnode, int64_t suid, SArray *list) {
29,344,719✔
1491
  int32_t      code = TSDB_CODE_SUCCESS;
29,344,719✔
1492
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, suid);
29,344,719✔
1493
  if (!pCur) {
29,345,598✔
1494
    return TSDB_CODE_OUT_OF_MEMORY;
×
1495
  }
1496

1497
  while (1) {
14,822,071✔
1498
    tb_uid_t id = metaStbCursorNext(pCur);
44,167,669✔
1499
    if (id == 0) {
44,168,552✔
1500
      break;
29,284,947✔
1501
    }
1502

1503
    if (NULL == taosArrayPush(list, &id)) {
14,822,214✔
1504
      qError("taosArrayPush failed");
×
1505
      code = terrno;
×
1506
      goto _exit;
×
1507
    }
1508
  }
1509

1510
_exit:
29,343,842✔
1511
  metaCloseStbCursor(pCur);
29,343,842✔
1512
  return code;
29,345,204✔
1513
}
1514

1515
int32_t vnodeGetStbIdListByFilter(SVnode *pVnode, int64_t suid, SArray *list, bool (*filter)(void *arg, void *arg1),
1,475✔
1516
                                  void *arg) {
1517
  int32_t      code = TSDB_CODE_SUCCESS;
1,475✔
1518
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, suid);
1,475✔
1519
  if (!pCur) {
1,475✔
1520
    return terrno;
×
1521
  }
1522

1523
  while (1) {
2,072✔
1524
    tb_uid_t id = metaStbCursorNext(pCur);
3,364✔
1525
    if (id == 0) {
3,364✔
1526
      break;
1,292✔
1527
    }
1528

1529
    if ((*filter) && (*filter)(arg, &id)) {
1,889✔
1530
      continue;
1,889✔
1531
    }
1532

1533
    if (NULL == taosArrayPush(list, &id)) {
×
1534
      qError("taosArrayPush failed");
×
1535
      code = terrno;
×
1536
      goto _exit;
×
1537
    }
1538
  }
1539

1540
_exit:
1,475✔
1541
  metaCloseStbCursor(pCur);
1,475✔
1542
  return code;
1,475✔
1543
}
1544

1545
int32_t vnodeGetCtbNum(SVnode *pVnode, int64_t suid, int64_t *num) {
1,539,132✔
1546
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, suid, 0, 0);
1,539,132✔
1547
  if (!pCur) {
1,539,501✔
1548
    return terrno;
×
1549
  }
1550

1551
  *num = 0;
1,539,589✔
1552
  while (1) {
1,017,527✔
1553
    tb_uid_t id = metaCtbCursorNext(pCur);
2,550,978✔
1554
    if (id == 0) {
2,550,608✔
1555
      break;
1,537,808✔
1556
    }
1557

1558
    ++(*num);
1,011,389✔
1559
  }
1560

1561
  metaCloseCtbCursor(pCur);
1,539,219✔
1562
  return TSDB_CODE_SUCCESS;
1,539,479✔
1563
}
1564

1565
int32_t vnodeGetStbColumnNum(SVnode *pVnode, tb_uid_t suid, int *num) {
1,538,880✔
1566
  SSchemaWrapper *pSW = metaGetTableSchema(pVnode->pMeta, suid, -1, 0, NULL, 0, false);
1,538,880✔
1567
  if (pSW) {
1,539,420✔
1568
    *num = pSW->nCols;
1,537,790✔
1569
    tDeleteSchemaWrapper(pSW);
1,411✔
1570
  } else {
1571
    *num = 2;
1,630✔
1572
  }
1573

1574
  return TSDB_CODE_SUCCESS;
1,539,376✔
1575
}
1576

1577
int32_t vnodeGetStbInfo(SVnode *pVnode, tb_uid_t suid, int64_t *keep, int8_t *flags) {
1,539,652✔
1578
  SMetaReader mr = {0};
1,539,652✔
1579
  metaReaderDoInit(&mr, pVnode->pMeta, META_READER_NOLOCK, 0);
1,539,652✔
1580

1581
  int32_t code = metaReaderGetTableEntryByUid(&mr, suid);
1,539,546✔
1582
  if (code == TSDB_CODE_SUCCESS) {
1,539,567✔
1583
    if (keep) *keep = mr.me.stbEntry.keep;
1,537,937✔
1584
    if (flags) *flags = mr.me.flags;
1,537,937✔
1585
  } else {
1586
    if (keep) *keep = 0;
1,630✔
1587
    if (flags) *flags = 0;
1,630✔
1588
  }
1589

1590
  metaReaderClear(&mr);
1,539,567✔
1591
  return TSDB_CODE_SUCCESS;
1,539,652✔
1592
}
1593

1594
#ifdef TD_ENTERPRISE
1595
const char *tkLogStb[] = {"cluster_info",
1596
                          "data_dir",
1597
                          "disk_free",
1598
                          "disk_total",
1599
                          "dnodes_info",
1600
                          "d_info",
1601
                          "explorer_sys",
1602
                          "grants_info",
1603
                          "keeper_monitor",
1604
                          "logs",
1605
                          "log_dir",
1606
                          "log_summary",
1607
                          "http_client_requests",
1608
                          "http_server_requests_idmp",
1609
                          "m_info",
1610
                          "taosadapter_restful_http_request_fail",
1611
                          "taosadapter_restful_http_request_in_flight",
1612
                          "taosadapter_restful_http_request_summary_milliseconds",
1613
                          "taosadapter_restful_http_request_total",
1614
                          "taosadapter_system_cpu_percent",
1615
                          "taosadapter_system_mem_percent",
1616
                          "temp_dir",
1617
                          "vgroups_info",
1618
                          "vnodes_role",
1619
                          "taosd_dnodes_status",
1620
                          "adapter_conn_pool",
1621
                          "taosd_vnodes_info",
1622
                          "taosd_dnodes_metrics",
1623
                          "taosd_vgroups_info",
1624
                          "taos_sql_req",
1625
                          "taosd_mnodes_info",
1626
                          "adapter_c_interface",
1627
                          "taosd_cluster_info",
1628
                          "taosd_sql_req",
1629
                          "taosd_dnodes_info",
1630
                          "adapter_requests",
1631
                          "taosd_write_metrics",
1632
                          "adapter_status",
1633
                          "taos_slow_sql",
1634
                          "taos_slow_sql_detail",
1635
                          "taosd_cluster_basic",
1636
                          "taosd_dnodes_data_dirs",
1637
                          "taosd_dnodes_log_dirs",
1638
                          "xnode_agent_activities",
1639
                          "xnode_task_activities",
1640
                          "xnode_task_metrics",
1641
                          "taosx_sys",
1642
                          "taosx_task_csv",
1643
                          "taosx_task_progress",
1644
                          "taosx_task_kinghist",
1645
                          "taosx_task_tdengine2",
1646
                          "taosx_task_tdengine3",
1647
                          "taosx_task_opc_da",
1648
                          "taosx_task_opc_ua",
1649
                          "taosx_task_kafka",
1650
                          "taosx_task_influxdb",
1651
                          "taosx_task_mqtt",
1652
                          "taosx_task_avevahistorian",
1653
                          "taosx_task_opentsdb",
1654
                          "taosx_task_mysql",
1655
                          "taosx_task_postgres",
1656
                          "taosx_task_oracle",
1657
                          "taosx_task_mssql",
1658
                          "taosx_task_mongodb",
1659
                          "taosx_task_sparkplugb",
1660
                          "taosx_task_orc",
1661
                          "taosx_task_pulsar",
1662
                          "taosx_task_pspace"};
1663
const char *tkAuditStb[] = {"operations"};
1664
const int   tkLogStbNum = ARRAY_SIZE(tkLogStb);
1665
const int   tkAuditStbNum = ARRAY_SIZE(tkAuditStb);
1666

1667
// exclude stbs of taoskeeper log
1668
static int32_t vnodeGetTimeSeriesBlackList(SVnode *pVnode, int32_t *tbSize) {
29,344,033✔
1669
  int32_t      code = TSDB_CODE_SUCCESS;
29,344,033✔
1670
  int32_t      tbNum = 0;
29,344,033✔
1671
  const char **pTbArr = NULL;
29,344,033✔
1672
  const char  *dbName = NULL;
29,344,033✔
1673
  *tbSize = 0;
29,344,033✔
1674

1675
  if (!(dbName = strchr(pVnode->config.dbname, '.'))) return 0;
29,344,033✔
1676
  if (0 == strncmp(++dbName, "log", TSDB_DB_NAME_LEN)) {
29,344,029✔
1677
    tbNum = tkLogStbNum;
510✔
1678
    pTbArr = (const char **)&tkLogStb;
510✔
1679
  } else if (0 == strncmp(dbName, "audit", TSDB_DB_NAME_LEN) || pVnode->config.isAudit) {
29,343,309✔
1680
    tbNum = tkAuditStbNum;
1,461✔
1681
    pTbArr = (const char **)&tkAuditStb;
1,461✔
1682
  }
1683
  if (tbNum && pTbArr) {
29,344,029✔
1684
    *tbSize = metaSizeOfTbFilterCache(pVnode->pMeta, 0);
2,181✔
1685
    if (*tbSize < tbNum) {
2,181✔
1686
      for (int32_t i = 0; i < tbNum; ++i) {
19,992✔
1687
        tb_uid_t suid = metaGetTableEntryUidByName(pVnode->pMeta, pTbArr[i]);
19,581✔
1688
        if (suid != 0) {
19,581✔
1689
          code = metaPutTbToFilterCache(pVnode->pMeta, &suid, 0);
1,139✔
1690
          if (TSDB_CODE_SUCCESS != code) {
1,139✔
1691
            return code;
510✔
1692
          }
1693
        }
1694
      }
1695
      *tbSize = metaSizeOfTbFilterCache(pVnode->pMeta, 0);
411✔
1696
    }
1697
  }
1698

1699
  return code;
29,282,828✔
1700
}
1701
#endif
1702

1703
static bool vnodeTimeSeriesFilter(void *arg1, void *arg2) {
1,889✔
1704
  SVnode *pVnode = (SVnode *)arg1;
1,889✔
1705

1706
  if (metaTbInFilterCache(pVnode->pMeta, arg2, 0)) {
1,889✔
1707
    return true;
1,700✔
1708
  }
1709
  return false;
×
1710
}
1711

1712
int32_t vnodeGetTimeSeriesNum(SVnode *pVnode, int64_t *num) {
29,343,982✔
1713
  SArray *suidList = NULL;
29,343,982✔
1714

1715
  if (!(suidList = taosArrayInit(1, sizeof(tb_uid_t)))) {
29,343,982✔
1716
    return terrno;
×
1717
  }
1718

1719
  int32_t tbFilterSize = 0;
29,344,198✔
1720
  int32_t code = TSDB_CODE_SUCCESS;
29,344,185✔
1721
#ifdef TD_ENTERPRISE
1722
  code = vnodeGetTimeSeriesBlackList(pVnode, &tbFilterSize);
29,344,185✔
1723
  if (TSDB_CODE_SUCCESS != code) {
29,344,629✔
1724
    goto _exit;
510✔
1725
  }
1726
#endif
1727

1728
  if ((!tbFilterSize && vnodeGetStbIdList(pVnode, 0, suidList) < 0) ||
29,344,119✔
1729
      (tbFilterSize && vnodeGetStbIdListByFilter(pVnode, 0, suidList, vnodeTimeSeriesFilter, pVnode) < 0)) {
29,283,639✔
1730
    qError("vgId:%d, failed to get stb id list error: %s", TD_VID(pVnode), terrstr());
×
1731
    taosArrayDestroy(suidList);
×
1732
    return terrno;
×
1733
  }
1734

1735
  *num = 0;
29,344,048✔
1736
  int64_t arrSize = taosArrayGetSize(suidList);
29,344,048✔
1737
  for (int64_t i = 0; i < arrSize; ++i) {
44,164,217✔
1738
    tb_uid_t suid = *(tb_uid_t *)taosArrayGet(suidList, i);
14,819,027✔
1739

1740
    int64_t ctbNum = 0;
14,819,170✔
1741
    int32_t numOfCols = 0;
14,819,367✔
1742
    int8_t  flags = 0;
14,819,564✔
1743
    code = metaGetStbStats(pVnode, suid, &ctbNum, &numOfCols, &flags);
14,819,815✔
1744
    if (TSDB_CODE_SUCCESS != code) {
14,820,603✔
1745
      goto _exit;
×
1746
    }
1747
    if (!TABLE_IS_VIRTUAL(flags)) {
14,820,603✔
1748
      *num += ctbNum * (numOfCols - 1);
10,904,944✔
1749
    }
1750
  }
1751

1752
_exit:
29,345,693✔
1753
  taosArrayDestroy(suidList);
29,344,912✔
1754
  return TSDB_CODE_SUCCESS;
29,344,715✔
1755
}
1756

1757
int32_t vnodeGetAllCtbNum(SVnode *pVnode, int64_t *num) {
×
1758
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, 0);
×
1759
  if (!pCur) {
×
1760
    return terrno;
×
1761
  }
1762

1763
  *num = 0;
×
1764
  while (1) {
×
1765
    tb_uid_t id = metaStbCursorNext(pCur);
×
1766
    if (id == 0) {
×
1767
      break;
×
1768
    }
1769

1770
    int64_t ctbNum = 0;
×
1771
    int32_t code = vnodeGetCtbNum(pVnode, id, &ctbNum);
×
1772
    if (TSDB_CODE_SUCCESS != code) {
×
1773
      metaCloseStbCursor(pCur);
×
1774
      return code;
×
1775
    }
1776

1777
    *num += ctbNum;
×
1778
  }
1779

1780
  metaCloseStbCursor(pCur);
×
1781
  return TSDB_CODE_SUCCESS;
×
1782
}
1783

1784
void *vnodeGetIdx(void *pVnode) {
1,062,107✔
1785
  if (pVnode == NULL) {
1,062,107✔
1786
    return NULL;
×
1787
  }
1788

1789
  return metaGetIdx(((SVnode *)pVnode)->pMeta);
1,062,107✔
1790
}
1791

1792
void *vnodeGetIvtIdx(void *pVnode) {
1,061,780✔
1793
  if (pVnode == NULL) {
1,061,780✔
1794
    return NULL;
×
1795
  }
1796
  return metaGetIvtIdx(((SVnode *)pVnode)->pMeta);
1,061,780✔
1797
}
1798

1799
int32_t vnodeGetTableSchema(void *pVnode, int64_t uid, STSchema **pSchema, int64_t *suid, SSchemaWrapper **pTagSchema) {
406,842✔
1800
  return tsdbGetTableSchema(((SVnode *)pVnode)->pMeta, uid, pSchema, suid, pTagSchema);
406,842✔
1801
}
1802

1803
static FORCE_INLINE int32_t vnodeGetDBPrimaryInfo(SVnode *pVnode, SDbSizeStatisInfo *pInfo) {
2,660✔
1804
  int32_t code = 0;
2,021,410✔
1805
  char    path[TSDB_FILENAME_LEN] = {0};
2,021,410✔
1806

1807
  char   *dirName[] = {VNODE_TSDB_DIR, VNODE_WAL_DIR, VNODE_META_DIR, VNODE_TSDB_CACHE_DIR};
2,021,410✔
1808
  int64_t dirSize[4];
2,021,362✔
1809

1810
  vnodeGetPrimaryPath(pVnode, false, path, TSDB_FILENAME_LEN);
2,021,410✔
1811
  int32_t offset = strlen(path);
2,021,410✔
1812

1813
  for (int i = 0; i < sizeof(dirName) / sizeof(dirName[0]); i++) {
10,107,242✔
1814
    int64_t size = {0};
8,085,784✔
1815
    (void)snprintf(path + offset, TSDB_FILENAME_LEN - offset, "%s%s", TD_DIRSEP, dirName[i]);
8,085,784✔
1816
    code = taosGetDirSize(path, &size);
8,085,784✔
1817
    if (code != 0) {
8,085,832✔
1818
      uWarn("vnode %d get dir %s %s size failed since %s", TD_VID(pVnode), path, dirName[i], tstrerror(code));
422✔
1819
    }
1820
    path[offset] = 0;
8,085,832✔
1821
    dirSize[i] = size;
8,085,832✔
1822
  }
1823

1824
  pInfo->l1Size = 0;
2,021,458✔
1825
  pInfo->walSize = dirSize[1];
2,021,458✔
1826
  pInfo->metaSize = dirSize[2];
2,021,458✔
1827
  pInfo->cacheSize = dirSize[3];
2,021,458✔
1828
  return code;
2,021,458✔
1829
}
1830
int32_t vnodeGetDBSize(void *pVnode, SDbSizeStatisInfo *pInfo) {
2,021,410✔
1831
  int32_t code = 0;
2,021,410✔
1832
  int32_t lino = 0;
2,021,410✔
1833
  SVnode *pVnodeObj = pVnode;
2,021,410✔
1834
  if (pVnodeObj == NULL) {
2,021,410✔
1835
    return TSDB_CODE_VND_NOT_EXIST;
×
1836
  }
1837
  code = vnodeGetDBPrimaryInfo(pVnode, pInfo);
2,021,458✔
1838
  if (code != 0) goto _exit;
2,021,458✔
1839

1840
  code = tsdbGetFsSize(pVnodeObj->pTsdb, pInfo);
2,021,458✔
1841
_exit:
2,018,798✔
1842
  return code;
2,018,798✔
1843
}
1844

1845
/*
1846
 * Get raw write metrics for a vnode
1847
 */
1848
int32_t vnodeGetRawWriteMetrics(void *pVnode, SRawWriteMetrics *pRawMetrics) {
×
1849
  if (pVnode == NULL || pRawMetrics == NULL) {
×
1850
    return TSDB_CODE_INVALID_PARA;
×
1851
  }
1852

1853
  SVnode      *pVnode1 = (SVnode *)pVnode;
×
1854
  SSyncMetrics syncMetrics = syncGetMetrics(pVnode1->sync);
×
1855

1856
  // Copy values following SRawWriteMetrics structure order
1857
  pRawMetrics->total_requests = atomic_load_64(&pVnode1->writeMetrics.total_requests);
×
1858
  pRawMetrics->total_rows = atomic_load_64(&pVnode1->writeMetrics.total_rows);
×
1859
  pRawMetrics->total_bytes = atomic_load_64(&pVnode1->writeMetrics.total_bytes);
×
1860
  pRawMetrics->fetch_batch_meta_time = atomic_load_64(&pVnode1->writeMetrics.fetch_batch_meta_time);
×
1861
  pRawMetrics->fetch_batch_meta_count = atomic_load_64(&pVnode1->writeMetrics.fetch_batch_meta_count);
×
1862
  pRawMetrics->preprocess_time = atomic_load_64(&pVnode1->writeMetrics.preprocess_time);
×
1863
  pRawMetrics->wal_write_bytes = atomic_load_64(&syncMetrics.wal_write_bytes);
×
1864
  pRawMetrics->wal_write_time = atomic_load_64(&syncMetrics.wal_write_time);
×
1865
  pRawMetrics->apply_bytes = atomic_load_64(&pVnode1->writeMetrics.apply_bytes);
×
1866
  pRawMetrics->apply_time = atomic_load_64(&pVnode1->writeMetrics.apply_time);
×
1867
  pRawMetrics->commit_count = atomic_load_64(&pVnode1->writeMetrics.commit_count);
×
1868
  pRawMetrics->commit_time = atomic_load_64(&pVnode1->writeMetrics.commit_time);
×
1869
  pRawMetrics->memtable_wait_time = atomic_load_64(&pVnode1->writeMetrics.memtable_wait_time);
×
1870
  pRawMetrics->blocked_commit_count = atomic_load_64(&pVnode1->writeMetrics.blocked_commit_count);
×
1871
  pRawMetrics->blocked_commit_time = atomic_load_64(&pVnode1->writeMetrics.block_commit_time);
×
1872
  pRawMetrics->merge_count = atomic_load_64(&pVnode1->writeMetrics.merge_count);
×
1873
  pRawMetrics->merge_time = atomic_load_64(&pVnode1->writeMetrics.merge_time);
×
1874
  pRawMetrics->last_cache_commit_time = atomic_load_64(&pVnode1->writeMetrics.last_cache_commit_time);
×
1875
  pRawMetrics->last_cache_commit_count = atomic_load_64(&pVnode1->writeMetrics.last_cache_commit_count);
×
1876

1877
  return 0;
×
1878
}
1879

1880
/*
1881
 * Reset raw write metrics for a vnode by subtracting old values
1882
 */
1883
int32_t vnodeResetRawWriteMetrics(void *pVnode, const SRawWriteMetrics *pOldMetrics) {
×
1884
  if (pVnode == NULL || pOldMetrics == NULL) {
×
1885
    return TSDB_CODE_INVALID_PARA;
×
1886
  }
1887

1888
  SVnode *pVnode1 = (SVnode *)pVnode;
×
1889

1890
  // Reset vnode write metrics using atomic operations to subtract old values
1891
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.total_requests, pOldMetrics->total_requests);
×
1892
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.total_rows, pOldMetrics->total_rows);
×
1893
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.total_bytes, pOldMetrics->total_bytes);
×
1894

1895
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.fetch_batch_meta_time, pOldMetrics->fetch_batch_meta_time);
×
1896
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.fetch_batch_meta_count, pOldMetrics->fetch_batch_meta_count);
×
1897
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.preprocess_time, pOldMetrics->preprocess_time);
×
1898
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.apply_bytes, pOldMetrics->apply_bytes);
×
1899
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.apply_time, pOldMetrics->apply_time);
×
1900
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.commit_count, pOldMetrics->commit_count);
×
1901

1902
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.commit_time, pOldMetrics->commit_time);
×
1903
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.merge_time, pOldMetrics->merge_time);
×
1904

1905
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.memtable_wait_time, pOldMetrics->memtable_wait_time);
×
1906
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.blocked_commit_count, pOldMetrics->blocked_commit_count);
×
1907
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.block_commit_time, pOldMetrics->blocked_commit_time);
×
1908
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.merge_count, pOldMetrics->merge_count);
×
1909

1910
  // Reset new cache metrics
1911
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.last_cache_commit_time, pOldMetrics->last_cache_commit_time);
×
1912
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.last_cache_commit_count, pOldMetrics->last_cache_commit_count);
×
1913

1914
  // Reset sync metrics
1915
  SSyncMetrics syncMetrics = {
×
1916
      .wal_write_bytes = pOldMetrics->wal_write_bytes,
×
1917
      .wal_write_time = pOldMetrics->wal_write_time,
×
1918
  };
1919
  syncResetMetrics(pVnode1->sync, &syncMetrics);
×
1920

1921
  return 0;
×
1922
}
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