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

taosdata / TDengine / #5115

12 Sep 2026 01:55PM UTC coverage: 73.107% (+0.03%) from 73.077%
#5115

push

travis-ci

jbjia
fix(ci): restore parTranslater.c in coverage exclude list

Re-add parTranslater.c to exclude.txt to align Coveralls reporting with the 3.0 branch baseline (~73%).

Co-authored-by: Cursor <cursoragent@cursor.com>

309688 of 423608 relevant lines covered (73.11%)

69352327.63 hits per line

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

69.13
/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,422,498✔
30
  return qWorkerInit(NODE_TYPE_VNODE, TD_VID(pVnode), (void **)&pVnode->pQuery, &pVnode->msgCb);
1,422,498✔
31
}
32

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

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

37
int32_t fillTableColCmpr(SMetaReader *reader, SSchemaExt *pExt, int32_t numOfCol) {
65,732,751✔
38
  int8_t tblType = reader->me.type;
65,732,751✔
39
  if (withColCompress(tblType)) {
65,732,751✔
40
    SColCmprWrapper *p = &(reader->me.colCmpr);
51,291,175✔
41
    if (numOfCol != p->nCols) {
51,291,175✔
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++) {
525,032,185✔
46
      SColCmpr *pCmpr = &p->pColCmpr[i];
473,741,010✔
47
      pExt[i].colId = pCmpr->id;
473,741,010✔
48
      pExt[i].compress = pCmpr->alg;
473,741,010✔
49
    }
50
  }
51
  return 0;
65,207,111✔
52
}
53

54
void vnodeDebugTableMeta(STableMetaRsp *pMeta) {
28,905,612✔
55
  if (!(qDebugFlag & DEBUG_DEBUG)) {
28,905,612✔
56
    return;
19,464,565✔
57
  }
58

59
  qDebug("tbName:%s", pMeta->tbName);
9,441,045✔
60
  qDebug("stbName:%s", pMeta->stbName);
9,442,032✔
61
  qDebug("dbFName:%s", pMeta->dbFName);
9,442,032✔
62
  qDebug("dbId:%" PRId64, pMeta->dbId);
9,442,032✔
63
  qDebug("numOfTags:%d", pMeta->numOfTags);
9,442,032✔
64
  qDebug("numOfColumns:%d", pMeta->numOfColumns);
9,442,032✔
65
  qDebug("precision:%d", pMeta->precision);
9,442,032✔
66
  qDebug("tableType:%d", pMeta->tableType);
9,442,032✔
67
  qDebug("sversion:%d", pMeta->sversion);
9,442,032✔
68
  qDebug("tversion:%d", pMeta->tversion);
9,442,032✔
69
  qDebug("suid:%" PRIu64, pMeta->suid);
9,442,032✔
70
  qDebug("tuid:%" PRIu64, pMeta->tuid);
9,442,032✔
71
  qDebug("vgId:%d", pMeta->vgId);
9,442,032✔
72
  qDebug("sysInfo:%d", pMeta->sysInfo);
9,442,032✔
73
  if (pMeta->pSchemas) {
9,442,032✔
74
    for (int32_t i = 0; i < (pMeta->numOfColumns + pMeta->numOfTags); ++i) {
94,840,841✔
75
      SSchema *pSchema = pMeta->pSchemas + i;
85,399,072✔
76
      qDebug("%d col/tag: type:%d, flags:%d, colId:%d, bytes:%d, name:%s", i, pSchema->type, pSchema->flags,
85,399,072✔
77
             pSchema->colId, pSchema->bytes, pSchema->name);
78
    }
79
  }
80
}
81

82
int32_t fillTableColRef(SMetaReader *reader, SColRef *pRef, int32_t numOfCol) {
15,946,432✔
83
  int8_t tblType = reader->me.type;
15,946,432✔
84
  if (hasColRef(tblType)) {
15,946,432✔
85
    SColRefWrapper *p = &(reader->me.colRef);
15,946,432✔
86
    if (numOfCol != p->nCols) {
15,946,432✔
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++) {
239,941,973✔
91
      SColRef *pColRef = &p->pColRef[i];
223,995,541✔
92
      pRef[i].hasRef = pColRef->hasRef;
223,995,541✔
93
      pRef[i].id = pColRef->id;
223,995,541✔
94
      if(pRef[i].hasRef) {
223,995,541✔
95
        pRef[i].refType = pColRef->refType;
182,653,104✔
96
        tstrncpy(pRef[i].refSourceName, pColRef->refSourceName, TSDB_EXT_SOURCE_NAME_LEN);
182,653,104✔
97
        tstrncpy(pRef[i].refSchemaName, pColRef->refSchemaName, TSDB_EXT_SOURCE_SCHEMA_LEN);
182,653,104✔
98
        tstrncpy(pRef[i].refDbName, pColRef->refDbName, TSDB_DB_NAME_LEN);
182,653,104✔
99
        tstrncpy(pRef[i].refTableName, pColRef->refTableName, TSDB_TABLE_NAME_LEN);
182,653,104✔
100
        tstrncpy(pRef[i].refColName, pColRef->refColName, TSDB_COL_NAME_LEN);
182,653,104✔
101
        if (pColRef->tagCondLen > 0 && pColRef->tagCondJson) {
182,653,104✔
102
          pRef[i].tagCondLen = pColRef->tagCondLen;
114,312✔
103
          pRef[i].tagCondJson = taosStrdup(pColRef->tagCondJson);
114,312✔
104
        } else {
105
          pRef[i].tagCondLen = 0;
182,538,792✔
106
          pRef[i].tagCondJson = NULL;
182,538,792✔
107
        }
108
      }
109
    }
110
  }
111
  return 0;
15,946,406✔
112
}
113

114
int32_t fillTableTagRef(SMetaReader *reader, SColRef *pRef, int32_t numOfTagRefs) {
642,340✔
115
  int8_t tblType = reader->me.type;
642,340✔
116
  if (hasTagRef(tblType)) {
642,340✔
117
    SColRefWrapper *p = &(reader->me.colRef);
642,340✔
118
    if (numOfTagRefs != p->nTagRefs) {
642,340✔
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,139,418✔
123
      SColRef *pTagRef = &p->pTagRef[i];
1,497,078✔
124
      pRef[i].hasRef = pTagRef->hasRef;
1,497,078✔
125
      pRef[i].id = pTagRef->id;
1,497,078✔
126
      if (pRef[i].hasRef) {
1,497,078✔
127
        tstrncpy(pRef[i].refDbName, pTagRef->refDbName, TSDB_DB_NAME_LEN);
1,189,209✔
128
        tstrncpy(pRef[i].refTableName, pTagRef->refTableName, TSDB_TABLE_NAME_LEN);
1,189,209✔
129
        tstrncpy(pRef[i].refColName, pTagRef->refColName, TSDB_COL_NAME_LEN);
1,189,209✔
130
      }
131
    }
132
  }
133
  return 0;
642,340✔
134
}
135

136
int32_t vnodeGetTableMeta(SVnode *pVnode, SRpcMsg *pMsg, bool direct) {
32,346,494✔
137
  STableInfoReq  infoReq = {0};
32,346,494✔
138
  STableMetaRsp  metaRsp = {0};
32,346,494✔
139
  SMetaReader    mer1 = {0};
32,345,959✔
140
  SMetaReader    mer2 = {0};
32,346,220✔
141
  char           tableFName[TSDB_TABLE_FNAME_LEN];
32,346,220✔
142
  bool           reqTbUid = false;
32,345,914✔
143
  SRpcMsg        rpcMsg = {0};
32,345,914✔
144
  int32_t        code = 0;
32,346,018✔
145
  int32_t        rspLen = 0;
32,346,018✔
146
  void          *pRsp = NULL;
32,346,018✔
147
  SSchemaWrapper schema = {0};
32,346,018✔
148
  SSchemaWrapper schemaTag = {0};
32,346,018✔
149
  uint8_t        autoCreateCtb = 0;
32,346,018✔
150

151
  // decode req
152
  if (tDeserializeSTableInfoReq(pMsg->pCont, pMsg->contLen, &infoReq) != 0) {
32,346,018✔
153
    code = terrno;
×
154
    goto _exit4;
×
155
  }
156
  autoCreateCtb = infoReq.autoCreateCtb;
32,347,223✔
157

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

163
  if (!reqTbUid) {
32,347,223✔
164
    (void)snprintf(tableFName, TSDB_TABLE_FNAME_LEN, "%s.%s", infoReq.dbFName, infoReq.tbName);
32,278,028✔
165
    if (pVnode->mounted) tTrimMountPrefix(tableFName);
32,278,028✔
166
    code = vnodeValidateTableHash(pVnode, tableFName);
32,278,028✔
167
    if (code) {
32,278,609✔
168
      goto _exit4;
×
169
    }
170
  }
171

172
  // query meta
173
  metaReaderDoInit(&mer1, pVnode->pMeta, META_READER_LOCK, infoReq.txnId);
32,347,804✔
174
  if (reqTbUid) {
32,345,719✔
175
    SET_ERRNO(0);
68,145✔
176
    uint64_t tbUid = taosStr2UInt64(infoReq.tbName, NULL, 10);
68,145✔
177
    if (ERRNO == ERANGE || tbUid == 0) {
68,145✔
178
      code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
980✔
179
      goto _exit3;
980✔
180
    }
181
    SMetaReader mr3 = {0};
67,165✔
182
    metaReaderDoInit(&mr3, ((SVnode *)pVnode)->pMeta, META_READER_NOLOCK, infoReq.txnId);
67,165✔
183
    if ((code = metaReaderGetTableEntryByUid(&mr3, tbUid)) < 0) {
67,165✔
184
      metaReaderClear(&mr3);
24,399✔
185
      TAOS_CHECK_GOTO(code, NULL, _exit3);
24,399✔
186
    }
187
    tstrncpy(metaRsp.tbName, mr3.me.name, TSDB_TABLE_NAME_LEN);
42,766✔
188
    metaReaderClear(&mr3);
42,766✔
189
    TAOS_CHECK_GOTO(metaGetTableEntryByName(&mer1, metaRsp.tbName), NULL, _exit3);
42,709✔
190
  } else if (metaGetTableEntryByName(&mer1, infoReq.tbName) < 0) {
32,277,574✔
191
    code = terrno;
3,414,935✔
192
    goto _exit3;
3,415,035✔
193
  }
194

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

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

216
      tstrncpy(metaRsp.stbName, mer2.me.name, sizeof(metaRsp.stbName));
6,745,320✔
217
      metaRsp.suid = mer2.me.uid;
6,745,320✔
218
      metaRsp.ownerId = mer2.me.stbEntry.ownerId;  // child table inherits ownerId from stb
6,745,320✔
219
      metaRsp.secLvl = mer2.me.stbEntry.securityLevel;  // child table inherits secLvl from stb
6,745,320✔
220
      schema = mer2.me.stbEntry.schemaRow;
6,745,320✔
221
      schemaTag = mer2.me.stbEntry.schemaTag;
6,745,320✔
222
      break;
6,745,320✔
223
    }
224
    case TSDB_NORMAL_TABLE:
6,332,969✔
225
      metaRsp.secLvl = pVnode->config.securityLevel;  // normal table inherits secLvl from vnode config
6,332,969✔
226
    case TSDB_VIRTUAL_NORMAL_TABLE: {
20,760,166✔
227
      schema = mer1.me.ntbEntry.schemaRow;
20,760,166✔
228
      schemaTag = mer1.me.ntbEntry.schemaTag;  // normal/virtual-normal tables may own tags
20,760,166✔
229
      metaRsp.ownerId = mer1.me.ntbEntry.ownerId;
20,760,166✔
230
      break;
20,760,166✔
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,905,670✔
239
  metaRsp.numOfColumns = schema.nCols;
28,905,670✔
240
  metaRsp.precision = pVnode->config.tsdbCfg.precision;
28,905,670✔
241
  metaRsp.sversion = schema.version;
28,905,670✔
242
  metaRsp.tversion = schemaTag.version;
28,905,670✔
243
  metaRsp.pSchemas = (SSchema *)taosMemoryMalloc(sizeof(SSchema) * (metaRsp.numOfColumns + metaRsp.numOfTags));
28,905,670✔
244
  metaRsp.pSchemaExt = (SSchemaExt *)taosMemoryCalloc(metaRsp.numOfColumns, sizeof(SSchemaExt));
28,903,968✔
245
  if (NULL == metaRsp.pSchemas || NULL == metaRsp.pSchemaExt) {
28,904,194✔
246
    code = terrno;
3✔
247
    goto _exit;
×
248
  }
249
  (void)memcpy(metaRsp.pSchemas, schema.pSchema, sizeof(SSchema) * schema.nCols);
28,904,531✔
250
  if (schemaTag.nCols) {
28,904,531✔
251
    (void)memcpy(metaRsp.pSchemas + schema.nCols, schemaTag.pSchema, sizeof(SSchema) * schemaTag.nCols);
8,156,406✔
252
  }
253
  if (metaRsp.pSchemaExt) {
28,904,531✔
254
    SMetaReader *pReader =
28,911,944✔
255
        (mer1.me.type == TSDB_CHILD_TABLE || mer1.me.type == TSDB_VIRTUAL_CHILD_TABLE) ? &mer2 : &mer1;
28,906,663✔
256
    code = fillTableColCmpr(pReader, metaRsp.pSchemaExt, metaRsp.numOfColumns);
28,906,663✔
257
    if (code < 0) {
28,906,521✔
258
      goto _exit;
×
259
    }
260
    for (int32_t i = 0; i < metaRsp.numOfColumns; i++) {
409,295,041✔
261
      metaRsp.pSchemaExt[i].colId = schema.pSchema[i].colId;
380,388,520✔
262
      if (pReader->me.pExtSchemas) {
380,388,520✔
263
        metaRsp.pSchemaExt[i].typeMod = pReader->me.pExtSchemas[i].typeMod;
8,006,708✔
264
      }
265
    }
266
  } else {
267
    code = TSDB_CODE_OUT_OF_MEMORY;
×
268
    goto _exit;
×
269
  }
270
  if (hasColRef(mer1.me.type)) {
28,906,521✔
271
    metaRsp.rversion = mer1.me.colRef.version;
15,946,903✔
272
    metaRsp.pColRefs = (SColRef*)taosMemoryCalloc(metaRsp.numOfColumns, sizeof(SColRef));
15,946,903✔
273
    if (metaRsp.pColRefs) {
15,945,911✔
274
      code = fillTableColRef(&mer1, metaRsp.pColRefs, metaRsp.numOfColumns);
15,946,253✔
275
      if (code < 0) {
15,946,613✔
276
        goto _exit;
×
277
      }
278
    }
279
    metaRsp.numOfColRefs = metaRsp.numOfColumns;
15,946,271✔
280

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

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

330
  vnodeDebugTableMeta(&metaRsp);
28,905,629✔
331

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

339
  if (direct) {
28,906,260✔
340
    pRsp = rpcMallocCont(rspLen);
21,483,997✔
341
  } else {
342
    pRsp = taosMemoryCalloc(1, rspLen);
7,422,263✔
343
  }
344

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

350
  rspLen = tSerializeSTableMetaRsp(pRsp, rspLen, &metaRsp);
28,903,925✔
351
  if (rspLen < 0) {
28,905,636✔
352
    code = terrno;
×
353
    goto _exit;
×
354
  }
355

356
_exit:
28,903,504✔
357
  tFreeSTableMetaRsp(&metaRsp);
28,907,231✔
358
_exit2:
28,904,843✔
359
  metaReaderClear(&mer2);
28,905,139✔
360
_exit3:
32,346,236✔
361
  metaReaderClear(&mer1);
32,347,300✔
362
_exit4:
32,346,642✔
363
  rpcMsg.info = pMsg->info;
32,347,563✔
364
  rpcMsg.pCont = pRsp;
32,347,563✔
365
  rpcMsg.contLen = rspLen;
32,347,563✔
366
  rpcMsg.code = code;
32,347,563✔
367
  rpcMsg.msgType = direct ? pMsg->msgType + 1 : pMsg->msgType;
32,347,563✔
368

369
  if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST && autoCreateCtb == 1) {
32,347,563✔
370
    code = TSDB_CODE_SUCCESS;
1,356,304✔
371
  }
372

373
  if (code) {
32,347,515✔
374
    qError("vgId:%d, get table %s meta with %" PRIu8 " failed cause of %s", pVnode->config.vgId, infoReq.tbName,
2,084,062✔
375
           infoReq.option, tstrerror(code));
376
  }
377

378
  if (direct) {
32,346,432✔
379
    tmsgSendRsp(&rpcMsg);
22,113,985✔
380
  } else {
381
    *pMsg = rpcMsg;
10,232,447✔
382
  }
383

384
  return code;
32,346,432✔
385
}
386

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

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

406
  tstrncpy(cfgRsp.tbName, cfgReq.tbName, TSDB_TABLE_NAME_LEN);
36,833,183✔
407
  (void)memcpy(cfgRsp.dbFName, cfgReq.dbFName, sizeof(cfgRsp.dbFName));
36,833,183✔
408

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

416
  // query meta
417
  metaReaderDoInit(&mer1, pVnode->pMeta, META_READER_LOCK, cfgReq.txnId);
36,833,183✔
418

419
  if (metaGetTableEntryByName(&mer1, cfgReq.tbName) < 0) {
36,833,183✔
420
    code = terrno;
6,678✔
421
    goto _exit;
6,678✔
422
  }
423

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

428
  if (mer1.me.type == TSDB_SUPER_TABLE) {
36,826,505✔
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,306,278✔
432
    metaReaderDoInit(&mer2, pVnode->pMeta, META_READER_NOLOCK, cfgReq.txnId);
36,807,750✔
433
    if (metaReaderGetTableEntryByUid(&mer2, mer1.me.ctbEntry.suid) < 0) goto _exit;
36,807,750✔
434

435
    tstrncpy(cfgRsp.stbName, mer2.me.name, TSDB_TABLE_NAME_LEN);
36,807,750✔
436
    schema = mer2.me.stbEntry.schemaRow;
36,807,750✔
437
    schemaTag = mer2.me.stbEntry.schemaTag;
36,807,750✔
438
    cfgRsp.ownerId = mer2.me.stbEntry.ownerId;        // child table inherits ownerId from stb
36,807,750✔
439
    cfgRsp.securityLevel = mer2.me.stbEntry.securityLevel;  // child table inherits secLvl from stb
36,807,750✔
440
    cfgRsp.ttl = mer1.me.ctbEntry.ttlDays;
36,807,750✔
441
    cfgRsp.commentLen = mer1.me.ctbEntry.commentLen;
36,807,750✔
442
    if (mer1.me.ctbEntry.commentLen > 0) {
36,807,750✔
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;
36,807,750✔
450
    cfgRsp.tagsLen = pTag->len;
36,807,750✔
451
    cfgRsp.pTags = taosMemoryMalloc(cfgRsp.tagsLen);
36,807,750✔
452
    if (NULL == cfgRsp.pTags) {
36,807,750✔
453
      code = terrno;
×
454
      goto _exit;
×
455
    }
456
    (void)memcpy(cfgRsp.pTags, pTag, cfgRsp.tagsLen);
36,807,750✔
457
  } else if (mer1.me.type == TSDB_NORMAL_TABLE || mer1.me.type == TSDB_VIRTUAL_NORMAL_TABLE) {
18,755✔
458
    schema = mer1.me.ntbEntry.schemaRow;
18,755✔
459
    schemaTag = mer1.me.ntbEntry.schemaTag;  // normal/virtual-normal tables may own tags
18,755✔
460
    cfgRsp.ttl = mer1.me.ntbEntry.ttlDays;
18,755✔
461
    cfgRsp.ownerId = mer1.me.ntbEntry.ownerId;
18,755✔
462
    cfgRsp.securityLevel = mer1.me.type == TSDB_NORMAL_TABLE ? pVnode->config.securityLevel : 0;
18,755✔
463
    cfgRsp.commentLen = mer1.me.ntbEntry.commentLen;
18,755✔
464
    if (mer1.me.ntbEntry.commentLen > 0) {
18,755✔
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) {
18,755✔
474
      STag *pTag = (STag *)mer1.me.ntbEntry.pTags;
6,794✔
475
      cfgRsp.tagsLen = pTag->len;
6,794✔
476
      cfgRsp.pTags = taosMemoryMalloc(cfgRsp.tagsLen);
6,794✔
477
      if (NULL == cfgRsp.pTags) {
6,794✔
478
        code = terrno;
×
479
        goto _exit;
×
480
      }
481
      (void)memcpy(cfgRsp.pTags, pTag, cfgRsp.tagsLen);
6,794✔
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;
36,826,505✔
490
  cfgRsp.numOfColumns = schema.nCols;
36,826,505✔
491
  cfgRsp.virtualStb = false; // vnode don't have super table, so it's always false
36,826,505✔
492
  cfgRsp.pSchemas = (SSchema *)taosMemoryMalloc(sizeof(SSchema) * (cfgRsp.numOfColumns + cfgRsp.numOfTags));
36,826,505✔
493
  cfgRsp.pSchemaExt = (SSchemaExt *)taosMemoryCalloc(cfgRsp.numOfColumns, sizeof(SSchemaExt));
36,826,505✔
494
  cfgRsp.pColRefs = (SColRef *)taosMemoryCalloc(cfgRsp.numOfColumns, sizeof(SColRef));
36,826,505✔
495
  cfgRsp.numOfTagRefs = 0;
36,826,505✔
496
  cfgRsp.pTagRefs = NULL;
36,826,505✔
497

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

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

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

516
    for (int32_t i = 0; i < cfgRsp.numOfColumns; i++) {
349,673,017✔
517
      SSchemaExt *pSchExt = cfgRsp.pSchemaExt + i;
312,846,512✔
518
      pSchExt->colId = schema.pSchema[i].colId;
312,846,512✔
519
      if (pReader->me.pExtSchemas) {
312,846,512✔
520
        pSchExt->typeMod = pReader->me.pExtSchemas[i].typeMod;
61,399✔
521
      }
522
    }
523
  }
524

525
  cfgRsp.virtualStb = false;
36,826,505✔
526
  if (hasColRef(cfgRsp.tableType)) {
36,826,505✔
527
    for (int32_t i = 0; i < cfgRsp.numOfColumns; i++) {
248,334,725✔
528
      SColRef *pRef = &pColRef->pColRef[i];
223,266,903✔
529
      cfgRsp.pColRefs[i].hasRef = pRef->hasRef;
223,266,903✔
530
      cfgRsp.pColRefs[i].id = pRef->id;
223,266,903✔
531
      if (cfgRsp.pColRefs[i].hasRef) {
223,266,903✔
532
        cfgRsp.pColRefs[i].refType = pRef->refType;
140,862,904✔
533
        tstrncpy(cfgRsp.pColRefs[i].refSourceName, pRef->refSourceName, TSDB_EXT_SOURCE_NAME_LEN);
140,862,904✔
534
        tstrncpy(cfgRsp.pColRefs[i].refSchemaName, pRef->refSchemaName, TSDB_EXT_SOURCE_SCHEMA_LEN);
140,862,904✔
535
        tstrncpy(cfgRsp.pColRefs[i].refDbName, pRef->refDbName, TSDB_DB_NAME_LEN);
140,862,904✔
536
        tstrncpy(cfgRsp.pColRefs[i].refTableName, pRef->refTableName, TSDB_TABLE_NAME_LEN);
140,862,904✔
537
        tstrncpy(cfgRsp.pColRefs[i].refColName, pRef->refColName, TSDB_COL_NAME_LEN);
140,862,904✔
538
        if (pRef->tagCondJson) {
140,862,904✔
539
          cfgRsp.pColRefs[i].tagCondLen = pRef->tagCondLen;
1,622,098✔
540
          cfgRsp.pColRefs[i].tagCondJson = taosStrdup(pRef->tagCondJson);
1,622,098✔
541
        } else {
542
          cfgRsp.pColRefs[i].tagCondLen = 0;
139,240,806✔
543
          cfgRsp.pColRefs[i].tagCondJson = NULL;
139,240,806✔
544
        }
545
      }
546
    }
547

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

560
      for (int32_t i = 0; i < cfgRsp.numOfTagRefs; i++) {
51,916,528✔
561
        SColRef *pRef = &pColRef->pTagRef[i];
38,888,881✔
562
        cfgRsp.pTagRefs[i].hasRef = pRef->hasRef;
38,888,881✔
563
        cfgRsp.pTagRefs[i].id = pRef->id;
38,888,881✔
564
        if (cfgRsp.pTagRefs[i].hasRef) {
38,888,881✔
565
          cfgRsp.pTagRefs[i].refType = pRef->refType;
25,854,163✔
566
          tstrncpy(cfgRsp.pTagRefs[i].refSourceName, pRef->refSourceName, TSDB_EXT_SOURCE_NAME_LEN);
25,854,163✔
567
          tstrncpy(cfgRsp.pTagRefs[i].refSchemaName, pRef->refSchemaName, TSDB_EXT_SOURCE_SCHEMA_LEN);
25,854,163✔
568
          tstrncpy(cfgRsp.pTagRefs[i].refDbName, pRef->refDbName, TSDB_DB_NAME_LEN);
25,854,163✔
569
          tstrncpy(cfgRsp.pTagRefs[i].refTableName, pRef->refTableName, TSDB_TABLE_NAME_LEN);
25,854,163✔
570
          tstrncpy(cfgRsp.pTagRefs[i].refColName, pRef->refColName, TSDB_COL_NAME_LEN);
25,854,163✔
571
          if (pRef->tagCondJson) {
25,854,163✔
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) {
36,826,505✔
586
    cfgRsp.numOfSeries = mer1.me.series.nSeries;
127,231✔
587
    cfgRsp.pSeries = taosMemoryCalloc(mer1.me.series.nSeries, sizeof(SSeriesEntry));
127,231✔
588
    if (cfgRsp.pSeries) {
127,231✔
589
      for (int32_t i = 0; i < mer1.me.series.nSeries; i++) {
869,757✔
590
        SSeriesEntry *src = &mer1.me.series.pSeries[i];
742,526✔
591
        SSeriesEntry *dst = &cfgRsp.pSeries[i];
742,526✔
592
        tstrncpy(dst->alias, src->alias, TSDB_COL_NAME_LEN);
742,526✔
593
        tstrncpy(dst->sourceName, src->sourceName, TSDB_EXT_SOURCE_NAME_LEN);
742,526✔
594
        tstrncpy(dst->dbName, src->dbName, TSDB_DB_NAME_LEN);
742,526✔
595
        tstrncpy(dst->measurementName, src->measurementName, TSDB_TABLE_NAME_LEN);
742,526✔
596
        if (src->tagCondLen > 0) {
742,526✔
597
          dst->tagCondJson = taosStrdup(src->tagCondJson);
742,526✔
598
          dst->tagCondLen = src->tagCondLen;
742,526✔
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);
36,826,505✔
609
  if (rspLen < 0) {
36,826,505✔
610
    code = terrno;
×
611
    goto _exit;
×
612
  }
613

614
  if (direct) {
36,826,505✔
615
    pRsp = rpcMallocCont(rspLen);
36,792,305✔
616
  } else {
617
    pRsp = taosMemoryCalloc(1, rspLen);
34,200✔
618
  }
619

620
  if (pRsp == NULL) {
36,826,505✔
621
    code = terrno;
×
622
    goto _exit;
×
623
  }
624

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

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

638
  if (code) {
36,833,183✔
639
    qError("get table %s cfg failed cause of %s", cfgReq.tbName, tstrerror(code));
6,678✔
640
  }
641

642
  if (direct) {
36,833,183✔
643
    tmsgSendRsp(&rpcMsg);
36,798,983✔
644
  } else {
645
    *pMsg = rpcMsg;
34,200✔
646
  }
647

648
  tFreeSTableCfgRsp(&cfgRsp);
36,833,183✔
649
  metaReaderClear(&mer2);
36,833,183✔
650
  metaReaderClear(&mer1);
36,833,183✔
651
  return code;
36,833,183✔
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) {
16,764,540✔
664
  int32_t      code = 0;
16,764,540✔
665
  int32_t      rspSize = 0;
16,764,540✔
666
  SBatchReq    batchReq = {0};
16,764,540✔
667
  SBatchMsg   *req = NULL;
16,767,111✔
668
  SBatchRspMsg rsp = {0};
16,767,111✔
669
  SBatchRsp    batchRsp = {0};
16,752,963✔
670
  SRpcMsg      reqMsg = *pMsg;
16,753,691✔
671
  SRpcMsg      rspMsg = {0};
16,754,363✔
672
  void        *pRsp = NULL;
16,760,795✔
673

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

680
  int32_t msgNum = taosArrayGetSize(batchReq.pMsgs);
16,756,876✔
681
  if (msgNum >= MAX_META_MSG_IN_BATCH) {
16,759,894✔
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));
16,759,894✔
688
  if (NULL == batchRsp.pRsps) {
16,759,279✔
689
    code = terrno;
314✔
690
    qError("taosArrayInit %d SBatchRspMsg failed", msgNum);
×
691
    goto _exit;
×
692
  }
693

694
  for (int32_t i = 0; i < msgNum; ++i) {
36,116,277✔
695
    req = taosArrayGet(batchReq.pMsgs, i);
19,346,666✔
696
    if (req == NULL) {
19,342,641✔
697
      code = terrno;
95✔
698
      goto _exit;
×
699
    }
700

701
    reqMsg.msgType = req->msgType;
19,343,971✔
702
    reqMsg.pCont = req->msg;
19,343,971✔
703
    reqMsg.contLen = req->msgLen;
19,343,971✔
704

705
    switch (req->msgType) {
19,343,971✔
706
      case TDMT_VND_TABLE_META:
10,164,147✔
707
        // error code has been set into reqMsg, no need to handle it here.
708
        if (TSDB_CODE_SUCCESS != vnodeGetTableMeta(pVnode, &reqMsg, false)) {
10,164,147✔
709
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
1,428,795✔
710
        }
711
        break;
10,157,585✔
712
      case TDMT_VND_TABLE_NAME:
68,086✔
713
        // error code has been set into reqMsg, no need to handle it here.
714
        if (TSDB_CODE_SUCCESS != vnodeGetTableMeta(pVnode, &reqMsg, false)) {
68,086✔
715
          qWarn("vnodeGetBatchName failed, msgType:%d", req->msgType);
25,379✔
716
        }
717
        break;
67,858✔
718
      case TDMT_VND_TABLE_CFG:
34,200✔
719
        // error code has been set into reqMsg, no need to handle it here.
720
        if (TSDB_CODE_SUCCESS != vnodeGetTableCfg(pVnode, &reqMsg, false)) {
34,200✔
721
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
×
722
        }
723
        break;
34,200✔
724
      case TDMT_VND_VSUBTABLES_META:
9,077,538✔
725
        // error code has been set into reqMsg, no need to handle it here.
726
        if (TSDB_CODE_SUCCESS != vnodeGetVSubtablesMeta(pVnode, &reqMsg)) {
9,077,538✔
727
          qWarn("vnodeGetVSubtablesMeta failed, msgType:%d", req->msgType);
×
728
        }
729
        break;
8,953,295✔
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;
19,356,427✔
751
    rsp.reqType = reqMsg.msgType;
19,356,427✔
752
    rsp.msgLen = reqMsg.contLen;
19,356,427✔
753
    rsp.rspCode = reqMsg.code;
19,356,427✔
754
    rsp.msg = reqMsg.pCont;
19,356,427✔
755

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

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

781
_exit:
16,772,216✔
782

783
  rspMsg.info = pMsg->info;
16,773,031✔
784
  rspMsg.pCont = pRsp;
16,773,031✔
785
  rspMsg.contLen = rspSize;
16,773,031✔
786
  rspMsg.code = code;
16,773,031✔
787
  rspMsg.msgType = pMsg->msgType;
16,773,031✔
788

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

793
  taosArrayDestroyEx(batchReq.pMsgs, tFreeSBatchReqMsg);
16,773,031✔
794
  taosArrayDestroyEx(batchRsp.pRsps, tFreeSBatchRspMsg);
16,770,894✔
795

796
  tmsgSendRsp(&rspMsg);
16,770,472✔
797

798
  return code;
16,772,416✔
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,076,258✔
809
  int32_t                    code = TSDB_CODE_SUCCESS;
9,076,258✔
810
  int32_t                    line = 0;
9,077,151✔
811
  SMetaReader                mr = {0};
9,077,151✔
812
  bool                       readerInit = false;
9,056,324✔
813
  SVCTableRefCols*           pTb = NULL;
9,056,324✔
814
  int32_t                    refColsNum = 0;
9,056,839✔
815
  char                       tbFName[TSDB_TABLE_FNAME_LEN];
9,056,839✔
816
  SSHashObj*                 pSrcTbls = NULL;
9,063,829✔
817

818
  SArray *pList = taosArrayInit(10, sizeof(uint64_t));
9,063,829✔
819
  QUERY_CHECK_NULL(pList, code, line, _return, terrno);
9,070,989✔
820

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

823
  size_t num = taosArrayGetSize(pList);
9,081,192✔
824
  *ppRes = taosArrayInit(num, POINTER_BYTES);
9,077,558✔
825
  QUERY_CHECK_NULL(*ppRes, code, line, _return, terrno);
9,082,889✔
826
  pSrcTbls = tSimpleHashInit(10, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
9,082,889✔
827
  QUERY_CHECK_NULL(pSrcTbls, code, line, _return, terrno);
9,073,466✔
828

829
  for (int32_t i = 0; i < num; ++i) {
56,222,078✔
830
    uint64_t* id = taosArrayGet(pList, i);
47,131,637✔
831
    QUERY_CHECK_NULL(id, code, line, _return, terrno);
47,101,284✔
832
    pHandle->api.metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pHandle->api.metaFn, pHandle->txnId);
47,101,284✔
833
    QUERY_CHECK_CODE(pHandle->api.metaReaderFn.getTableEntryByUid(&mr, *id), line, _return);
47,181,300✔
834
    readerInit = true;
56,903,720✔
835

836
    refColsNum = 0;
46,510,018✔
837
    for (int32_t j = 0; j < mr.me.colRef.nCols; j++) {
550,300,880✔
838
      if (mr.me.colRef.pColRef[j].hasRef) {
503,177,684✔
839
        refColsNum++;
286,664,275✔
840
      }
841
    }
842

843
    int32_t tagRefColsNum = 0;
46,510,018✔
844
    for (int32_t j = 0; j < mr.me.colRef.nTagRefs; j++) {
85,371,349✔
845
      if (mr.me.colRef.pTagRef[j].hasRef) {
38,248,153✔
846
        tagRefColsNum++;
25,511,632✔
847
      }
848
    }
849

850
    if (refColsNum <= 0 && tagRefColsNum <= 0) {
47,123,196✔
851
      pHandle->api.metaReaderFn.clearReader(&mr);
8,076,075✔
852
      readerInit = false;
8,076,075✔
853
      continue;
8,076,075✔
854
    }
855

856
    pTb = taosMemoryCalloc(1, (refColsNum + tagRefColsNum) * sizeof(SRefColInfo) + sizeof(*pTb));
39,047,121✔
857
    QUERY_CHECK_NULL(pTb, code, line, _return, terrno);
38,857,410✔
858

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

865
    refColsNum = 0;
38,857,410✔
866
    tSimpleHashClear(pSrcTbls);
38,857,410✔
867
    for (int32_t j = 0; j < mr.me.colRef.nCols; j++) {
413,623,162✔
868
      if (!mr.me.colRef.pColRef[j].hasRef) {
374,509,848✔
869
        continue;
87,599,991✔
870
      }
871

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

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

880
      if (NULL == tSimpleHashGet(pSrcTbls, tbFName, strlen(tbFName))) {
286,909,857✔
881
        QUERY_CHECK_CODE(tSimpleHashPut(pSrcTbls, tbFName, strlen(tbFName), &code, sizeof(code)), line, _return);
59,968,664✔
882
      }
883

884
      refColsNum++;
286,883,544✔
885
    }
886

887
    // Fill tag refs
888
    tagRefColsNum = 0;
38,704,087✔
889
    for (int32_t j = 0; j < mr.me.colRef.nTagRefs; j++) {
77,483,929✔
890
      if (!mr.me.colRef.pTagRef[j].hasRef) {
38,343,687✔
891
        continue;
12,768,187✔
892
      }
893

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

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

902
      if (NULL == tSimpleHashGet(pSrcTbls, tbFName, strlen(tbFName))) {
25,575,500✔
903
        QUERY_CHECK_CODE(tSimpleHashPut(pSrcTbls, tbFName, strlen(tbFName), &code, sizeof(code)), line, _return);
604,864✔
904
      }
905

906
      tagRefColsNum++;
25,602,143✔
907
    }
908

909
    pTb->numOfSrcTbls = tSimpleHashGetSize(pSrcTbls);
39,140,242✔
910
    QUERY_CHECK_NULL(taosArrayPush(*ppRes, &pTb), code, line, _return, terrno);
77,879,994✔
911
    pTb = NULL;
39,115,400✔
912

913
    pHandle->api.metaReaderFn.clearReader(&mr);
39,115,400✔
914
    readerInit = false;
39,072,512✔
915
  }
916

917
_return:
9,090,441✔
918

919
  if (readerInit) {
8,953,628✔
920
    pHandle->api.metaReaderFn.clearReader(&mr);
×
921
  }
922

923
  taosArrayDestroy(pList);
9,091,370✔
924
  taosMemoryFree(pTb);
9,091,376✔
925
  tSimpleHashCleanup(pSrcTbls);
9,090,770✔
926

927
  if (code) {
9,089,463✔
928
    qError("%s failed since %s", __func__, tstrerror(code));
×
929
  }
930
  return code;
9,089,463✔
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,077,067✔
1032
  int32_t        code = 0;
9,077,067✔
1033
  int32_t        rspSize = 0;
9,077,067✔
1034
  SVSubTablesReq req = {0};
9,077,067✔
1035
  SVSubTablesRsp rsp = {0};
9,056,936✔
1036
  SRpcMsg      rspMsg = {0};
9,047,415✔
1037
  void        *pRsp = NULL;
9,047,415✔
1038
  int32_t      line = 0;
9,047,415✔
1039

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

1046
  SReadHandle handle = {0};
9,079,588✔
1047
  handle.vnode = pVnode;
9,079,523✔
1048
  initStorageAPI(&handle.api);
9,079,523✔
1049

1050
  QUERY_CHECK_CODE(vnodeReadVSubtables(&handle, req.suid, &rsp.pTables), line, _return);
9,059,410✔
1051
  rsp.vgId = TD_VID(pVnode);
9,091,109✔
1052

1053
  rspSize = tSerializeSVSubTablesRsp(NULL, 0, &rsp);
9,091,109✔
1054
  if (rspSize < 0) {
9,090,383✔
1055
    code = rspSize;
×
1056
    qError("tSerializeSVSubTablesRsp failed, error:%d", rspSize);
×
1057
    goto _return;
×
1058
  }
1059
  pRsp = taosMemoryCalloc(1, rspSize);
9,090,383✔
1060
  if (pRsp == NULL) {
9,088,684✔
1061
    code = terrno;
×
1062
    qError("rpcMallocCont %d failed, error:%d", rspSize, terrno);
×
1063
    goto _return;
×
1064
  }
1065
  rspSize = tSerializeSVSubTablesRsp(pRsp, rspSize, &rsp);
9,088,684✔
1066
  if (rspSize < 0) {
9,091,217✔
1067
    code = rspSize;
×
1068
    qError("tSerializeSVSubTablesRsp failed, error:%d", rspSize);
×
1069
    goto _return;
×
1070
  }
1071

1072
_return:
9,091,217✔
1073

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

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

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

1088
  //tmsgSendRsp(&rspMsg);
1089

1090
  return code;
9,089,949✔
1091
}
1092

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

1099
  code = tDeserializeSVCheckHasCtbReq(pBuf, bufLen, &req);
51,021✔
1100
  if (code != 0) {
51,021✔
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);
51,021✔
1106
  if (pCur == NULL) {
51,021✔
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);
51,021✔
1111
    metaCloseCtbCursor(pCur);
51,021✔
1112
    if (id != 0) {
51,021✔
1113
      code = TSDB_CODE_MND_VST_PARENT_HAS_VCT;
795✔
1114
    }
1115
  }
1116

1117
_exit:;
51,021✔
1118
  SRpcMsg rspMsg = {.info = pMsg->info, .code = code};
51,021✔
1119
  tmsgSendRsp(&rspMsg);
51,021✔
1120
  return 0;  // return 0 so fetch worker doesn't double-send response
51,021✔
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,254,470✔
1185
  int32_t      code = 0;
2,254,470✔
1186
  int32_t      line = 0;
2,254,470✔
1187
  int32_t      rspSize = 0;
2,254,470✔
1188
  SVTagCondReq req = {0};
2,254,470✔
1189
  SVTagCondRsp rsp = {0};
2,254,470✔
1190
  SRpcMsg      rspMsg = {0};
2,254,470✔
1191
  void        *pRsp = NULL;
2,254,470✔
1192
  SMetaReader  mer = {0};
2,254,470✔
1193

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

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

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

1207
  if (hasColRef(mer.me.type)) {
2,254,470✔
1208
    SColRefWrapper *p = &(mer.me.colRef);
1,899,192✔
1209
    for (int32_t i = 0; i < p->nCols; ++i) {
38,290,115✔
1210
      SColRef *pColRef = &p->pColRef[i];
36,035,645✔
1211
      if (pColRef->hasRef && pColRef->tagCondLen > 0 && pColRef->tagCondJson != NULL) {
36,035,645✔
1212
        SVTagCondEntry entry = {0};
806,853✔
1213
        entry.colId = pColRef->id;
806,853✔
1214
        entry.tagCondLen = pColRef->tagCondLen;
806,853✔
1215
        entry.tagCondJson = taosStrdup(pColRef->tagCondJson);
806,853✔
1216
        QUERY_CHECK_NULL(entry.tagCondJson, code, line, _return, terrno);
806,853✔
1217
        if (taosArrayPush(rsp.pEntries, &entry) == NULL) {
808,746✔
1218
          taosMemoryFree(entry.tagCondJson);
×
1219
          code = terrno;
×
1220
          QUERY_CHECK_CODE(code, line, _return);
804,960✔
1221
        }
1222
      }
1223
    }
1224
  }
1225
  rsp.numOfRefs = taosArrayGetSize(rsp.pEntries);
2,254,470✔
1226

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

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

1265
static int32_t vnodeGetCompStorage(SVnode *pVnode, int64_t *output) {
39,699,472✔
1266
  int32_t code = 0;
39,699,472✔
1267
#ifdef TD_ENTERPRISE
1268
  int32_t now = taosGetTimestampSec();
39,699,472✔
1269
  if (llabs(now - pVnode->config.vndStats.storageLastUpd) >= 30) {
39,699,472✔
1270
    pVnode->config.vndStats.storageLastUpd = now;
2,107,526✔
1271

1272
    SDbSizeStatisInfo info = {0};
2,107,526✔
1273
    if (0 == (code = vnodeGetDBSize(pVnode, &info))) {
2,107,526✔
1274
      int64_t compSize =
2,107,526✔
1275
          info.l1Size + info.l2Size + info.l3Size + info.cacheSize + info.walSize + info.metaSize + +info.ssSize;
2,107,526✔
1276
      if (compSize >= 0) {
2,107,526✔
1277
        pVnode->config.vndStats.compStorage = compSize;
2,107,526✔
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));
5,581✔
1284
    }
1285
  }
1286
  if (output) *output = pVnode->config.vndStats.compStorage;
39,699,472✔
1287
#endif
1288
  return code;
39,699,472✔
1289
}
1290

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

1297
    if (pVnode->inUse) {
39,699,472✔
1298
      *bufferSegmentUsed = pVnode->inUse->size;
39,685,882✔
1299
    }
1300
    *bufferSegmentSize = pVnode->config.szBuf / VNODE_BUFPOOL_SEGMENTS;
39,699,472✔
1301

1302
    (void)taosThreadMutexUnlock(&pVnode->mutex);
39,699,472✔
1303
  }
1304
}
39,699,472✔
1305

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

1311
  pLoad->vgId = TD_VID(pVnode);
39,699,472✔
1312
  pLoad->syncState = state.state;
39,699,472✔
1313
  pLoad->syncRestore = state.restored;
39,699,472✔
1314
  pLoad->syncTerm = state.term;
39,699,472✔
1315
  pLoad->roleTimeMs = state.roleTimeMs;
39,699,472✔
1316
  pLoad->startTimeMs = state.startTimeMs;
39,699,472✔
1317
  pLoad->syncCanRead = state.canRead;
39,699,472✔
1318
  pLoad->cacheUsage = tsdbCacheGetUsage(pVnode);
39,699,472✔
1319
  pLoad->numOfCachedTables = tsdbCacheGetElems(pVnode);
39,699,472✔
1320
  VNODE_DO_META_QUERY(pVnode, pLoad->numOfTables = metaGetTbNum(pVnode->pMeta));
39,699,472✔
1321
  VNODE_DO_META_QUERY(pVnode, pLoad->numOfTimeSeries = metaGetTimeSeriesNum(pVnode->pMeta, 1));
39,699,472✔
1322
  pLoad->totalStorage = (int64_t)3 * 1073741824;  // TODO
39,699,472✔
1323
  (void)vnodeGetCompStorage(pVnode, &pLoad->compStorage);
39,699,472✔
1324
  pLoad->pointsWritten = 100;
39,699,472✔
1325
  pLoad->numOfSelectReqs = 1;
39,699,472✔
1326
  pLoad->numOfInsertReqs = atomic_load_64(&pVnode->statis.nInsert);
39,699,472✔
1327
  pLoad->numOfInsertSuccessReqs = atomic_load_64(&pVnode->statis.nInsertSuccess);
39,699,472✔
1328
  pLoad->numOfBatchInsertReqs = atomic_load_64(&pVnode->statis.nBatchInsert);
39,699,472✔
1329
  pLoad->numOfBatchInsertSuccessReqs = atomic_load_64(&pVnode->statis.nBatchInsertSuccess);
39,699,472✔
1330
  vnodeGetBufferInfo(pVnode, &pLoad->bufferSegmentUsed, &pLoad->bufferSegmentSize);
39,699,472✔
1331
  vDebug("vgId:%d, get vnode load, state:%s snapSeq:%d, learnerProgress:%d, totalIndex:%" PRId64, TD_VID(pVnode),
39,699,472✔
1332
         syncStr(state.state), state.snapSeq, state.progress, state.totalIndex);
1333
  pLoad->learnerProgress = state.progress;
39,699,472✔
1334
  pLoad->snapSeq = state.snapSeq;
39,699,472✔
1335
  pLoad->syncTotalIndex = state.totalIndex;
39,699,472✔
1336
  pLoad->snapshotSending = syncSnapshotSending(pVnode->sync) ? 1 : 0;
39,699,472✔
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;
39,699,472✔
1340
  SArray *pSnapGroups = taosArrayInit(4, sizeof(STsdbSnapSendGroup));
39,699,472✔
1341
  if (pSnapGroups != NULL) {
39,699,472✔
1342
    tsdbGetSnapSendSummary(pVnode->pTsdb, pSnapGroups);
39,699,472✔
1343
    int32_t numGroups = (int32_t)taosArrayGetSize(pSnapGroups);
39,699,472✔
1344
    if (numGroups > 0) {
39,699,472✔
1345
      pLoad->pSnapProgress = taosArrayInit(numGroups, sizeof(SVnodeSnapProgress));
2,501✔
1346
      if (pLoad->pSnapProgress != NULL) {
2,501✔
1347
        for (int32_t g = 0; g < numGroups; g++) {
5,002✔
1348
          STsdbSnapSendGroup *pGroup = (STsdbSnapSendGroup *)taosArrayGet(pSnapGroups, g);
2,501✔
1349
          if (pGroup == NULL) continue;
2,501✔
1350
          SVnodeSnapProgress prog = {.destDnodeId = pGroup->destDnodeId,
2,501✔
1351
                                     .snapTotalSize = pGroup->total,
2,501✔
1352
                                     .snapTransferredSize = pGroup->transferred};
2,501✔
1353
          if (taosArrayPush(pLoad->pSnapProgress, &prog) == NULL) {
5,002✔
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);
2,501✔
1358
      } else {
1359
        vError("vgId:%d, failed to alloc pSnapProgress array for vnode load", TD_VID(pVnode));
×
1360
      }
1361
    }
1362
    taosArrayDestroy(pSnapGroups);
39,699,472✔
1363
  } else {
1364
    vError("vgId:%d, failed to alloc snap send groups array for vnode load", TD_VID(pVnode));
×
1365
  }
1366
  return 0;
39,699,472✔
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) {
×
1385
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nInsert, pLoad->numOfInsertReqs, 64, "nInsert");
×
1386
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nInsertSuccess, pLoad->numOfInsertSuccessReqs, 64, "nInsertSuccess");
×
1387
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nBatchInsert, pLoad->numOfBatchInsertReqs, 64, "nBatchInsert");
×
1388
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nBatchInsertSuccess, pLoad->numOfBatchInsertSuccessReqs, 64,
×
1389
                            "nBatchInsertSuccess");
1390
}
×
1391

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

1396
  if (dbname) {
31,110,457✔
1397
    *dbname = pConf->dbname;
29,713,929✔
1398
  }
1399

1400
  if (vgId) {
31,110,457✔
1401
    *vgId = TD_VID(pVnodeObj);
30,525,147✔
1402
  }
1403

1404
  if (numOfTables) {
31,110,457✔
1405
    *numOfTables = pConf->vndStats.numOfNTables + pConf->vndStats.numOfCTables +
3,563✔
1406
                   pConf->vndStats.numOfVTables + pConf->vndStats.numOfVCTables;
3,563✔
1407
  }
1408

1409
  if (numOfNormalTables) {
31,110,457✔
1410
    *numOfNormalTables = pConf->vndStats.numOfNTables +
2,491✔
1411
                         pConf->vndStats.numOfVTables;
2,491✔
1412
  }
1413
}
31,110,457✔
1414

1415
int8_t vnodeGetSecurityLevel(void *pVnode) {
346,926✔
1416
  SVnode *pVnodeObj = pVnode;
346,926✔
1417
  return pVnodeObj->config.securityLevel;
346,926✔
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,491✔
1426
  if (type == TSDB_SUPER_TABLE) {
2,491✔
1427
    return vnodeGetStbIdList(pVnode, 0, pList);
2,491✔
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) {
79,558,894✔
1464
  int32_t      code = TSDB_CODE_SUCCESS;
79,558,894✔
1465
  SVnode      *pVnodeObj = pVnode;
79,558,894✔
1466
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnodeObj, suid, 1, 0);
79,558,894✔
1467
  if (NULL == pCur) {
79,536,065✔
1468
    qError("vnode get ctb id list failed, suid:%" PRId64, suid);
×
1469
    return terrno;
×
1470
  }
1471

1472
  while (1) {
215,383,855✔
1473
    tb_uid_t id = metaCtbCursorNext(pCur);
294,919,920✔
1474
    if (id == 0) {
294,243,308✔
1475
      break;
79,251,451✔
1476
    }
1477
    qTrace("vnodeGetCtbIdList: got ctb id %" PRId64 " for suid %" PRId64, id, suid);
214,671,249✔
1478
    if (NULL == taosArrayPush(list, &id)) {
215,319,850✔
1479
      qError("taosArrayPush failed");
×
1480
      code = terrno;
×
1481
      goto _exit;
×
1482
    }
1483
  }
1484

1485
_exit:
79,465,415✔
1486
  metaCloseCtbCursor(pCur);
79,465,415✔
1487
  return code;
79,543,828✔
1488
}
1489

1490
int32_t vnodeGetStbIdList(SVnode *pVnode, int64_t suid, SArray *list) {
30,058,371✔
1491
  int32_t      code = TSDB_CODE_SUCCESS;
30,058,371✔
1492
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, suid);
30,058,371✔
1493
  if (!pCur) {
30,060,767✔
1494
    return TSDB_CODE_OUT_OF_MEMORY;
×
1495
  }
1496

1497
  while (1) {
13,422,441✔
1498
    tb_uid_t id = metaStbCursorNext(pCur);
43,483,208✔
1499
    if (id == 0) {
43,486,541✔
1500
      break;
29,921,018✔
1501
    }
1502

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

1510
_exit:
30,059,766✔
1511
  metaCloseStbCursor(pCur);
30,059,766✔
1512
  return code;
30,058,797✔
1513
}
1514

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

1523
  while (1) {
2,415✔
1524
    tb_uid_t id = metaStbCursorNext(pCur);
2,613✔
1525
    if (id == 0) {
2,613✔
1526
      break;
198✔
1527
    }
1528

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

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

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

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

1551
  *num = 0;
1,329,835✔
1552
  while (1) {
955,838✔
1553
    tb_uid_t id = metaCtbCursorNext(pCur);
2,280,260✔
1554
    if (id == 0) {
2,280,083✔
1555
      break;
1,328,260✔
1556
    }
1557

1558
    ++(*num);
950,425✔
1559
  }
1560

1561
  metaCloseCtbCursor(pCur);
1,329,658✔
1562
  return TSDB_CODE_SUCCESS;
1,329,705✔
1563
}
1564

1565
int32_t vnodeGetStbColumnNum(SVnode *pVnode, tb_uid_t suid, int *num) {
1,329,368✔
1566
  SSchemaWrapper *pSW = metaGetTableSchema(pVnode->pMeta, suid, -1, 0, NULL, 0, false);
1,329,368✔
1567
  if (pSW) {
1,329,918✔
1568
    *num = pSW->nCols;
1,328,417✔
1569
    tDeleteSchemaWrapper(pSW);
1,398✔
1570
  } else {
1571
    *num = 2;
1,501✔
1572
  }
1573

1574
  return TSDB_CODE_SUCCESS;
1,329,944✔
1575
}
1576

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

1581
  int32_t code = metaReaderGetTableEntryByUid(&mr, suid);
1,329,393✔
1582
  if (code == TSDB_CODE_SUCCESS) {
1,330,020✔
1583
    if (keep) *keep = mr.me.stbEntry.keep;
1,328,519✔
1584
    if (flags) *flags = mr.me.flags;
1,328,519✔
1585
  } else {
1586
    if (keep) *keep = 0;
1,501✔
1587
    if (flags) *flags = 0;
1,501✔
1588
  }
1589

1590
  metaReaderClear(&mr);
1,330,020✔
1591
  return TSDB_CODE_SUCCESS;
1,330,020✔
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) {
30,057,760✔
1669
  int32_t      code = TSDB_CODE_SUCCESS;
30,057,760✔
1670
  int32_t      tbNum = 0;
30,057,760✔
1671
  const char **pTbArr = NULL;
30,057,760✔
1672
  const char  *dbName = NULL;
30,057,760✔
1673
  *tbSize = 0;
30,057,760✔
1674

1675
  if (!(dbName = strchr(pVnode->config.dbname, '.'))) return 0;
30,057,760✔
1676
  if (0 == strncmp(++dbName, "log", TSDB_DB_NAME_LEN)) {
30,057,756✔
1677
    tbNum = tkLogStbNum;
×
1678
    pTbArr = (const char **)&tkLogStb;
×
1679
  } else if (0 == strncmp(dbName, "audit", TSDB_DB_NAME_LEN) || pVnode->config.isAudit) {
30,057,453✔
1680
    tbNum = tkAuditStbNum;
1,347✔
1681
    pTbArr = (const char **)&tkAuditStb;
1,347✔
1682
  }
1683
  if (tbNum && pTbArr) {
30,057,756✔
1684
    *tbSize = metaSizeOfTbFilterCache(pVnode->pMeta, 0);
1,650✔
1685
    if (*tbSize < tbNum) {
1,650✔
1686
      for (int32_t i = 0; i < tbNum; ++i) {
1,683✔
1687
        tb_uid_t suid = metaGetTableEntryUidByName(pVnode->pMeta, pTbArr[i]);
1,587✔
1688
        if (suid != 0) {
1,587✔
1689
          code = metaPutTbToFilterCache(pVnode->pMeta, &suid, 0);
324✔
1690
          if (TSDB_CODE_SUCCESS != code) {
324✔
1691
            return code;
285✔
1692
          }
1693
        }
1694
      }
1695
      *tbSize = metaSizeOfTbFilterCache(pVnode->pMeta, 0);
96✔
1696
    }
1697
  }
1698

1699
  return code;
29,916,422✔
1700
}
1701
#endif
1702

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

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

1712
int32_t vnodeGetTimeSeriesNum(SVnode *pVnode, int64_t *num) {
30,057,744✔
1713
  SArray *suidList = NULL;
30,057,744✔
1714

1715
  if (!(suidList = taosArrayInit(1, sizeof(tb_uid_t)))) {
30,057,744✔
1716
    return terrno;
×
1717
  }
1718

1719
  int32_t tbFilterSize = 0;
30,057,941✔
1720
  int32_t code = TSDB_CODE_SUCCESS;
30,057,744✔
1721
#ifdef TD_ENTERPRISE
1722
  code = vnodeGetTimeSeriesBlackList(pVnode, &tbFilterSize);
30,057,744✔
1723
  if (TSDB_CODE_SUCCESS != code) {
30,059,336✔
1724
    goto _exit;
285✔
1725
  }
1726
#endif
1727

1728
  if ((!tbFilterSize && vnodeGetStbIdList(pVnode, 0, suidList) < 0) ||
30,059,051✔
1729
      (tbFilterSize && vnodeGetStbIdListByFilter(pVnode, 0, suidList, vnodeTimeSeriesFilter, pVnode) < 0)) {
29,918,892✔
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;
30,058,050✔
1736
  int64_t arrSize = taosArrayGetSize(suidList);
30,058,050✔
1737
  for (int64_t i = 0; i < arrSize; ++i) {
43,481,438✔
1738
    tb_uid_t suid = *(tb_uid_t *)taosArrayGet(suidList, i);
13,421,599✔
1739

1740
    int64_t ctbNum = 0;
13,421,008✔
1741
    int32_t numOfCols = 0;
13,421,796✔
1742
    int8_t  flags = 0;
13,422,387✔
1743
    code = metaGetStbStats(pVnode, suid, &ctbNum, &numOfCols, &flags);
13,422,797✔
1744
    if (TSDB_CODE_SUCCESS != code) {
13,422,600✔
1745
      goto _exit;
×
1746
    }
1747
    if (!TABLE_IS_VIRTUAL(flags)) {
13,422,600✔
1748
      *num += ctbNum * (numOfCols - 1);
9,862,108✔
1749
    }
1750
  }
1751

1752
_exit:
30,059,839✔
1753
  taosArrayDestroy(suidList);
30,059,336✔
1754
  return TSDB_CODE_SUCCESS;
30,058,532✔
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) {
764,323✔
1785
  if (pVnode == NULL) {
764,323✔
1786
    return NULL;
×
1787
  }
1788

1789
  return metaGetIdx(((SVnode *)pVnode)->pMeta);
764,323✔
1790
}
1791

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

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

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

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

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

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

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

1840
  code = tsdbGetFsSize(pVnodeObj->pTsdb, pInfo);
2,109,482✔
1841
_exit:
2,103,901✔
1842
  return code;
2,103,901✔
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