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

taosdata / TDengine / #3840

04 Apr 2025 03:35PM UTC coverage: 63.027% (+0.6%) from 62.382%
#3840

push

travis-ci

web-flow
Merge pull request #30653 from taosdata/merge/mainto3.0

merge: from main to 3.0 branch

155471 of 315065 branches covered (49.35%)

Branch coverage included in aggregate %.

241637 of 314991 relevant lines covered (76.71%)

18825079.96 hits per line

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

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

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

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

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

32
void vnodeQueryPreClose(SVnode *pVnode) { qWorkerStopAllTasks((void *)pVnode->pQuery); }
11,803✔
33

34
void vnodeQueryClose(SVnode *pVnode) { qWorkerDestroy((void **)&pVnode->pQuery); }
11,809✔
35

36
int32_t fillTableColCmpr(SMetaReader *reader, SSchemaExt *pExt, int32_t numOfCol) {
2,052,375✔
37
  int8_t tblType = reader->me.type;
2,052,375✔
38
  if (withExtSchema(tblType)) {
2,052,375!
39
    SColCmprWrapper *p = &(reader->me.colCmpr);
2,052,451✔
40
    if (numOfCol != p->nCols) {
2,052,451!
41
      vError("fillTableColCmpr table type:%d, col num:%d, col cmpr num:%d mismatch", tblType, numOfCol, p->nCols);
×
42
      return TSDB_CODE_APP_ERROR;
×
43
    }
44
    for (int i = 0; i < p->nCols; i++) {
51,356,810✔
45
      SColCmpr *pCmpr = &p->pColCmpr[i];
49,304,359✔
46
      pExt[i].colId = pCmpr->id;
49,304,359✔
47
      pExt[i].compress = pCmpr->alg;
49,304,359✔
48
    }
49
  }
50
  return 0;
2,052,402✔
51
}
52

53
void vnodePrintTableMeta(STableMetaRsp *pMeta) {
2,052,407✔
54
  if (!(qDebugFlag & DEBUG_DEBUG)) {
2,052,407✔
55
    return;
2,019,113✔
56
  }
57

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

81
int32_t fillTableColRef(SMetaReader *reader, SColRef *pRef, int32_t numOfCol) {
×
82
  int8_t tblType = reader->me.type;
×
83
  if (hasRefCol(tblType)) {
×
84
    SColRefWrapper *p = &(reader->me.colRef);
×
85
    if (numOfCol != p->nCols) {
×
86
      vError("fillTableColRef table type:%d, col num:%d, col cmpr num:%d mismatch", tblType, numOfCol, p->nCols);
×
87
      return TSDB_CODE_APP_ERROR;
×
88
    }
89
    for (int i = 0; i < p->nCols; i++) {
×
90
      SColRef *pColRef = &p->pColRef[i];
×
91
      pRef[i].hasRef = pColRef->hasRef;
×
92
      pRef[i].id = pColRef->id;
×
93
      if(pRef[i].hasRef) {
×
94
        tstrncpy(pRef[i].refDbName, pColRef->refDbName, TSDB_DB_NAME_LEN);
×
95
        tstrncpy(pRef[i].refTableName, pColRef->refTableName, TSDB_TABLE_NAME_LEN);
×
96
        tstrncpy(pRef[i].refColName, pColRef->refColName, TSDB_COL_NAME_LEN);
×
97
      }
98
    }
99
  }
100
  return 0;
×
101
}
102

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

118
  // decode req
119
  if (tDeserializeSTableInfoReq(pMsg->pCont, pMsg->contLen, &infoReq) != 0) {
2,090,138!
120
    code = terrno;
×
121
    goto _exit4;
×
122
  }
123
  autoCreateCtb = infoReq.autoCreateCtb;
2,091,346✔
124

125
  if (infoReq.option == REQ_OPT_TBUID) reqTbUid = true;
2,091,346✔
126
  metaRsp.dbId = pVnode->config.dbId;
2,091,346✔
127
  tstrncpy(metaRsp.tbName, infoReq.tbName, TSDB_TABLE_NAME_LEN);
2,091,346✔
128
  (void)memcpy(metaRsp.dbFName, infoReq.dbFName, sizeof(metaRsp.dbFName));
2,091,346✔
129

130
  if (!reqTbUid) {
2,091,346✔
131
    (void)tsnprintf(tableFName, TSDB_TABLE_FNAME_LEN, "%s.%s", infoReq.dbFName, infoReq.tbName);
2,089,872✔
132
    code = vnodeValidateTableHash(pVnode, tableFName);
2,090,765✔
133
    if (code) {
2,090,543!
134
      goto _exit4;
×
135
    }
136
  }
137

138
  // query meta
139
  metaReaderDoInit(&mer1, pVnode->pMeta, META_READER_LOCK);
2,092,017✔
140
  if (reqTbUid) {
2,091,206✔
141
    SET_ERRNO(0);
525✔
142
    uint64_t tbUid = taosStr2UInt64(infoReq.tbName, NULL, 10);
525✔
143
    if (ERRNO == ERANGE || tbUid == 0) {
525!
144
      code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
145
      goto _exit3;
125✔
146
    }
147
    SMetaReader mr3 = {0};
526✔
148
    metaReaderDoInit(&mr3, ((SVnode *)pVnode)->pMeta, META_READER_NOLOCK);
526✔
149
    if ((code = metaReaderGetTableEntryByUid(&mr3, tbUid)) < 0) {
526✔
150
      metaReaderClear(&mr3);
127✔
151
      TAOS_CHECK_GOTO(code, NULL, _exit3);
126!
152
    }
153
    tstrncpy(metaRsp.tbName, mr3.me.name, TSDB_TABLE_NAME_LEN);
399✔
154
    metaReaderClear(&mr3);
399✔
155
    TAOS_CHECK_GOTO(metaGetTableEntryByName(&mer1, metaRsp.tbName), NULL, _exit3);
399!
156
  } else if (metaGetTableEntryByName(&mer1, infoReq.tbName) < 0) {
2,090,681✔
157
    code = terrno;
38,150✔
158
    goto _exit3;
38,355✔
159
  }
160

161
  metaRsp.tableType = mer1.me.type;
2,053,105✔
162
  metaRsp.vgId = TD_VID(pVnode);
2,053,105✔
163
  metaRsp.tuid = mer1.me.uid;
2,053,105✔
164

165
  switch (mer1.me.type) {
2,053,105!
166
    case TSDB_SUPER_TABLE: {
1,350,745✔
167
      (void)strcpy(metaRsp.stbName, mer1.me.name);
1,350,745✔
168
      schema = mer1.me.stbEntry.schemaRow;
1,350,745✔
169
      schemaTag = mer1.me.stbEntry.schemaTag;
1,350,745✔
170
      metaRsp.suid = mer1.me.uid;
1,350,745✔
171
      break;
1,350,745✔
172
    }
173
    case TSDB_CHILD_TABLE:
633,135✔
174
    case TSDB_VIRTUAL_CHILD_TABLE:{
175
      metaReaderDoInit(&mer2, pVnode->pMeta, META_READER_NOLOCK);
633,135✔
176
      if (metaReaderGetTableEntryByUid(&mer2, mer1.me.ctbEntry.suid) < 0) goto _exit2;
633,133!
177

178
      (void)strcpy(metaRsp.stbName, mer2.me.name);
633,135✔
179
      metaRsp.suid = mer2.me.uid;
633,135✔
180
      schema = mer2.me.stbEntry.schemaRow;
633,135✔
181
      schemaTag = mer2.me.stbEntry.schemaTag;
633,135✔
182
      break;
633,135✔
183
    }
184
    case TSDB_NORMAL_TABLE:
69,225✔
185
    case TSDB_VIRTUAL_NORMAL_TABLE: {
186
      schema = mer1.me.ntbEntry.schemaRow;
69,225✔
187
      break;
69,225✔
188
    }
189
    default: {
×
190
      vError("vnodeGetTableMeta get invalid table type:%d", mer1.me.type);
×
191
      goto _exit3;
×
192
    }
193
  }
194

195
  metaRsp.numOfTags = schemaTag.nCols;
2,053,105✔
196
  metaRsp.numOfColumns = schema.nCols;
2,053,105✔
197
  metaRsp.precision = pVnode->config.tsdbCfg.precision;
2,053,105✔
198
  metaRsp.sversion = schema.version;
2,053,105✔
199
  metaRsp.tversion = schemaTag.version;
2,053,105✔
200
  metaRsp.pSchemas = (SSchema *)taosMemoryMalloc(sizeof(SSchema) * (metaRsp.numOfColumns + metaRsp.numOfTags));
2,053,105!
201
  metaRsp.pSchemaExt = (SSchemaExt *)taosMemoryCalloc(metaRsp.numOfColumns, sizeof(SSchemaExt));
2,052,869!
202
  if (NULL == metaRsp.pSchemas || NULL == metaRsp.pSchemaExt) {
2,052,427!
203
    code = terrno;
×
204
    goto _exit;
×
205
  }
206
  (void)memcpy(metaRsp.pSchemas, schema.pSchema, sizeof(SSchema) * schema.nCols);
2,052,492✔
207
  if (schemaTag.nCols) {
2,052,492✔
208
    (void)memcpy(metaRsp.pSchemas + schema.nCols, schemaTag.pSchema, sizeof(SSchema) * schemaTag.nCols);
1,982,816✔
209
  }
210
  if (metaRsp.pSchemaExt) {
2,052,492✔
211
    SMetaReader *pReader = mer1.me.type == TSDB_CHILD_TABLE ? &mer2 : &mer1;
2,052,177✔
212
    code = fillTableColCmpr(pReader, metaRsp.pSchemaExt, metaRsp.numOfColumns);
2,052,177✔
213
    if (code < 0) {
2,052,607!
214
      goto _exit;
×
215
    }
216
    for (int32_t i = 0; i < metaRsp.numOfColumns && pReader->me.pExtSchemas; i++) {
2,413,933✔
217
      metaRsp.pSchemaExt[i].typeMod = pReader->me.pExtSchemas[i].typeMod;
361,326✔
218
    }
219
  } else {
220
    code = TSDB_CODE_OUT_OF_MEMORY;
315✔
221
    goto _exit;
315✔
222
  }
223
  if (hasRefCol(mer1.me.type)) {
2,052,607✔
224
    metaRsp.pColRefs = (SColRef*)taosMemoryMalloc(sizeof(SColRef) * metaRsp.numOfColumns);
156!
225
    if (metaRsp.pColRefs) {
×
226
      code = fillTableColRef(&mer1, metaRsp.pColRefs, metaRsp.numOfColumns);
×
227
      if (code < 0) {
×
228
        goto _exit;
×
229
      }
230
    }
231
    metaRsp.numOfColRefs = metaRsp.numOfColumns;
×
232
  } else {
233
    metaRsp.pColRefs = NULL;
2,052,430✔
234
    metaRsp.numOfColRefs = 0;
2,052,430✔
235
  }
236

237
  vnodePrintTableMeta(&metaRsp);
2,052,430✔
238

239
  // encode and send response
240
  rspLen = tSerializeSTableMetaRsp(NULL, 0, &metaRsp);
2,052,385✔
241
  if (rspLen < 0) {
2,052,328!
242
    code = terrno;
×
243
    goto _exit;
×
244
  }
245

246
  if (direct) {
2,052,328✔
247
    pRsp = rpcMallocCont(rspLen);
232✔
248
  } else {
249
    pRsp = taosMemoryCalloc(1, rspLen);
2,052,096!
250
  }
251

252
  if (pRsp == NULL) {
2,051,825!
253
    code = terrno;
×
254
    goto _exit;
×
255
  }
256

257
  rspLen = tSerializeSTableMetaRsp(pRsp, rspLen, &metaRsp);
2,051,825✔
258
  if (rspLen < 0) {
2,052,613!
259
    code = terrno;
×
260
    goto _exit;
×
261
  }
262

263
_exit:
2,052,613✔
264
  taosMemoryFree(metaRsp.pColRefs);
2,052,928!
265
  taosMemoryFree(metaRsp.pSchemas);
2,052,331!
266
  taosMemoryFree(metaRsp.pSchemaExt);
2,052,449!
267
_exit2:
2,052,818✔
268
  metaReaderClear(&mer2);
2,052,818✔
269
_exit3:
2,091,053✔
270
  metaReaderClear(&mer1);
2,091,053✔
271
_exit4:
2,091,223✔
272
  rpcMsg.info = pMsg->info;
2,091,223✔
273
  rpcMsg.pCont = pRsp;
2,091,223✔
274
  rpcMsg.contLen = rspLen;
2,091,223✔
275
  rpcMsg.code = code;
2,091,223✔
276
  rpcMsg.msgType = pMsg->msgType;
2,091,223✔
277

278
  if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST && autoCreateCtb == 1) {
2,091,223✔
279
    code = TSDB_CODE_SUCCESS;
24,379✔
280
  }
281

282
  if (code) {
2,091,223✔
283
    qError("get table %s meta with %" PRIu8 " failed cause of %s", infoReq.tbName, infoReq.option, tstrerror(code));
14,102!
284
  }
285

286
  if (direct) {
2,090,902✔
287
    tmsgSendRsp(&rpcMsg);
1,797✔
288
  } else {
289
    *pMsg = rpcMsg;
2,089,105✔
290
  }
291

292
  return code;
2,090,966✔
293
}
294

295
int32_t vnodeGetTableCfg(SVnode *pVnode, SRpcMsg *pMsg, bool direct) {
101✔
296
  STableCfgReq   cfgReq = {0};
101✔
297
  STableCfgRsp   cfgRsp = {0};
101✔
298
  SMetaReader    mer1 = {0};
101✔
299
  SMetaReader    mer2 = {0};
101✔
300
  char           tableFName[TSDB_TABLE_FNAME_LEN];
301
  SRpcMsg        rpcMsg = {0};
101✔
302
  int32_t        code = 0;
101✔
303
  int32_t        rspLen = 0;
101✔
304
  void          *pRsp = NULL;
101✔
305
  SSchemaWrapper schema = {0};
101✔
306
  SSchemaWrapper schemaTag = {0};
101✔
307

308
  // decode req
309
  if (tDeserializeSTableCfgReq(pMsg->pCont, pMsg->contLen, &cfgReq) != 0) {
101!
310
    code = terrno;
×
311
    goto _exit;
×
312
  }
313

314
  tstrncpy(cfgRsp.tbName, cfgReq.tbName, TSDB_TABLE_NAME_LEN);
101✔
315
  (void)memcpy(cfgRsp.dbFName, cfgReq.dbFName, sizeof(cfgRsp.dbFName));
101✔
316

317
  (void)tsnprintf(tableFName, TSDB_TABLE_FNAME_LEN, "%s.%s", cfgReq.dbFName, cfgReq.tbName);
101✔
318
  code = vnodeValidateTableHash(pVnode, tableFName);
101✔
319
  if (code) {
101!
320
    goto _exit;
×
321
  }
322

323
  // query meta
324
  metaReaderDoInit(&mer1, pVnode->pMeta, META_READER_LOCK);
101✔
325

326
  if (metaGetTableEntryByName(&mer1, cfgReq.tbName) < 0) {
101!
327
    code = terrno;
×
328
    goto _exit;
×
329
  }
330

331
  cfgRsp.tableType = mer1.me.type;
101✔
332

333
  if (mer1.me.type == TSDB_SUPER_TABLE) {
101!
334
    code = TSDB_CODE_VND_HASH_MISMATCH;
×
335
    goto _exit;
×
336
  } else if (mer1.me.type == TSDB_CHILD_TABLE || mer1.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
101!
337
    metaReaderDoInit(&mer2, pVnode->pMeta, META_READER_NOLOCK);
69✔
338
    if (metaReaderGetTableEntryByUid(&mer2, mer1.me.ctbEntry.suid) < 0) goto _exit;
69!
339

340
    tstrncpy(cfgRsp.stbName, mer2.me.name, TSDB_TABLE_NAME_LEN);
69✔
341
    schema = mer2.me.stbEntry.schemaRow;
69✔
342
    schemaTag = mer2.me.stbEntry.schemaTag;
69✔
343
    cfgRsp.ttl = mer1.me.ctbEntry.ttlDays;
69✔
344
    cfgRsp.commentLen = mer1.me.ctbEntry.commentLen;
69✔
345
    if (mer1.me.ctbEntry.commentLen > 0) {
69!
346
      cfgRsp.pComment = taosStrdup(mer1.me.ctbEntry.comment);
×
347
      if (NULL == cfgRsp.pComment) {
×
348
        code = terrno;
×
349
        goto _exit;
×
350
      }
351
    }
352
    STag *pTag = (STag *)mer1.me.ctbEntry.pTags;
69✔
353
    cfgRsp.tagsLen = pTag->len;
69✔
354
    cfgRsp.pTags = taosMemoryMalloc(cfgRsp.tagsLen);
69!
355
    if (NULL == cfgRsp.pTags) {
69!
356
      code = terrno;
×
357
      goto _exit;
×
358
    }
359
    (void)memcpy(cfgRsp.pTags, pTag, cfgRsp.tagsLen);
69✔
360
  } else if (mer1.me.type == TSDB_NORMAL_TABLE || mer1.me.type == TSDB_VIRTUAL_NORMAL_TABLE) {
32!
361
    schema = mer1.me.ntbEntry.schemaRow;
32✔
362
    cfgRsp.ttl = mer1.me.ntbEntry.ttlDays;
32✔
363
    cfgRsp.commentLen = mer1.me.ntbEntry.commentLen;
32✔
364
    if (mer1.me.ntbEntry.commentLen > 0) {
32!
365
      cfgRsp.pComment = taosStrdup(mer1.me.ntbEntry.comment);
×
366
      if (NULL == cfgRsp.pComment) {
×
367
        code = terrno;
×
368
        goto _exit;
×
369
      }
370
    }
371
  } else {
372
    vError("vnodeGetTableCfg get invalid table type:%d", mer1.me.type);
×
373
    code = TSDB_CODE_APP_ERROR;
×
374
    goto _exit;
×
375
  }
376

377
  cfgRsp.numOfTags = schemaTag.nCols;
101✔
378
  cfgRsp.numOfColumns = schema.nCols;
101✔
379
  cfgRsp.virtualStb = false; // vnode don't have super table, so it's always false
101✔
380
  cfgRsp.pSchemas = (SSchema *)taosMemoryMalloc(sizeof(SSchema) * (cfgRsp.numOfColumns + cfgRsp.numOfTags));
101!
381
  cfgRsp.pSchemaExt = (SSchemaExt *)taosMemoryMalloc(cfgRsp.numOfColumns * sizeof(SSchemaExt));
101!
382
  cfgRsp.pColRefs = (SColRef *)taosMemoryMalloc(sizeof(SColRef) * cfgRsp.numOfColumns);
101!
383

384
  if (NULL == cfgRsp.pSchemas || NULL == cfgRsp.pSchemaExt || NULL == cfgRsp.pColRefs) {
101!
385
    code = terrno;
×
386
    goto _exit;
×
387
  }
388
  (void)memcpy(cfgRsp.pSchemas, schema.pSchema, sizeof(SSchema) * schema.nCols);
101✔
389
  if (schemaTag.nCols) {
101✔
390
    (void)memcpy(cfgRsp.pSchemas + schema.nCols, schemaTag.pSchema, sizeof(SSchema) * schemaTag.nCols);
69✔
391
  }
392

393
  SMetaReader     *pReader = (mer1.me.type == TSDB_CHILD_TABLE || mer1.me.type == TSDB_VIRTUAL_CHILD_TABLE) ? &mer2 : &mer1;
101!
394
  SColCmprWrapper *pColCmpr = &pReader->me.colCmpr;
101✔
395
  SColRefWrapper  *pColRef = &mer1.me.colRef;
101✔
396

397
  if (withExtSchema(cfgRsp.tableType)) {
101!
398
    for (int32_t i = 0; i < cfgRsp.numOfColumns; i++) {
923✔
399
      SColCmpr   *pCmpr = &pColCmpr->pColCmpr[i];
822✔
400
      SSchemaExt *pSchExt = cfgRsp.pSchemaExt + i;
822✔
401
      pSchExt->colId = pCmpr->id;
822✔
402
      pSchExt->compress = pCmpr->alg;
822✔
403
      if (pReader->me.pExtSchemas)
822✔
404
        pSchExt->typeMod = pReader->me.pExtSchemas[i].typeMod;
165✔
405
      else
406
        pSchExt->typeMod = 0;
657✔
407
    }
408
  }
409

410
  cfgRsp.virtualStb = false;
101✔
411
  if (hasRefCol(cfgRsp.tableType)) {
101!
412
    for (int32_t i = 0; i < cfgRsp.numOfColumns; i++) {
×
413
      SColRef *pRef = &pColRef->pColRef[i];
×
414
      cfgRsp.pColRefs[i].hasRef = pRef->hasRef;
×
415
      cfgRsp.pColRefs[i].id = pRef->id;
×
416
      if (cfgRsp.pColRefs[i].hasRef) {
×
417
        tstrncpy(cfgRsp.pColRefs[i].refDbName, pRef->refDbName, TSDB_DB_NAME_LEN);
×
418
        tstrncpy(cfgRsp.pColRefs[i].refTableName, pRef->refTableName, TSDB_TABLE_NAME_LEN);
×
419
        tstrncpy(cfgRsp.pColRefs[i].refColName, pRef->refColName, TSDB_COL_NAME_LEN);
×
420
      }
421
    }
422
  }
423

424
  // encode and send response
425
  rspLen = tSerializeSTableCfgRsp(NULL, 0, &cfgRsp);
101✔
426
  if (rspLen < 0) {
101!
427
    code = terrno;
×
428
    goto _exit;
×
429
  }
430

431
  if (direct) {
101!
432
    pRsp = rpcMallocCont(rspLen);
×
433
  } else {
434
    pRsp = taosMemoryCalloc(1, rspLen);
101!
435
  }
436

437
  if (pRsp == NULL) {
101!
438
    code = terrno;
×
439
    goto _exit;
×
440
  }
441

442
  rspLen = tSerializeSTableCfgRsp(pRsp, rspLen, &cfgRsp);
101✔
443
  if (rspLen < 0) {
101!
444
    code = terrno;
×
445
    goto _exit;
×
446
  }
447

448
_exit:
101✔
449
  rpcMsg.info = pMsg->info;
101✔
450
  rpcMsg.pCont = pRsp;
101✔
451
  rpcMsg.contLen = rspLen;
101✔
452
  rpcMsg.code = code;
101✔
453
  rpcMsg.msgType = pMsg->msgType;
101✔
454

455
  if (code) {
101!
456
    qError("get table %s cfg failed cause of %s", cfgReq.tbName, tstrerror(code));
×
457
  }
458

459
  if (direct) {
101!
460
    tmsgSendRsp(&rpcMsg);
×
461
  } else {
462
    *pMsg = rpcMsg;
101✔
463
  }
464

465
  tFreeSTableCfgRsp(&cfgRsp);
101✔
466
  metaReaderClear(&mer2);
101✔
467
  metaReaderClear(&mer1);
101✔
468
  return code;
101✔
469
}
470

471
static FORCE_INLINE void vnodeFreeSBatchRspMsg(void *p) {
472
  if (NULL == p) {
473
    return;
474
  }
475

476
  SBatchRspMsg *pRsp = (SBatchRspMsg *)p;
477
  rpcFreeCont(pRsp->msg);
478
}
479

480
int32_t vnodeGetBatchMeta(SVnode *pVnode, SRpcMsg *pMsg) {
1,196,810✔
481
  int32_t      code = 0;
1,196,810✔
482
  int32_t      rspSize = 0;
1,196,810✔
483
  SBatchReq    batchReq = {0};
1,196,810✔
484
  SBatchMsg   *req = NULL;
1,196,810✔
485
  SBatchRspMsg rsp = {0};
1,196,810✔
486
  SBatchRsp    batchRsp = {0};
1,196,810✔
487
  SRpcMsg      reqMsg = *pMsg;
1,196,810✔
488
  SRpcMsg      rspMsg = {0};
1,196,810✔
489
  void        *pRsp = NULL;
1,196,810✔
490

491
  if (tDeserializeSBatchReq(pMsg->pCont, pMsg->contLen, &batchReq)) {
1,196,810!
492
    code = terrno;
×
493
    qError("tDeserializeSBatchReq failed");
×
494
    goto _exit;
×
495
  }
496

497
  int32_t msgNum = taosArrayGetSize(batchReq.pMsgs);
1,197,735✔
498
  if (msgNum >= MAX_META_MSG_IN_BATCH) {
1,197,481!
499
    code = TSDB_CODE_INVALID_MSG;
×
500
    qError("too many msgs %d in vnode batch meta req", msgNum);
×
501
    goto _exit;
×
502
  }
503

504
  batchRsp.pRsps = taosArrayInit(msgNum, sizeof(SBatchRspMsg));
1,197,481✔
505
  if (NULL == batchRsp.pRsps) {
1,197,506!
506
    code = terrno;
×
507
    qError("taosArrayInit %d SBatchRspMsg failed", msgNum);
×
508
    goto _exit;
×
509
  }
510

511
  for (int32_t i = 0; i < msgNum; ++i) {
3,294,605✔
512
    req = taosArrayGet(batchReq.pMsgs, i);
2,097,777✔
513
    if (req == NULL) {
2,097,240✔
514
      code = terrno;
69✔
515
      goto _exit;
×
516
    }
517

518
    reqMsg.msgType = req->msgType;
2,097,171✔
519
    reqMsg.pCont = req->msg;
2,097,171✔
520
    reqMsg.contLen = req->msgLen;
2,097,171✔
521

522
    switch (req->msgType) {
2,097,171!
523
      case TDMT_VND_TABLE_META:
2,088,065✔
524
        // error code has been set into reqMsg, no need to handle it here.
525
        if (TSDB_CODE_SUCCESS != vnodeGetTableMeta(pVnode, &reqMsg, false)) {
2,088,065✔
526
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
12,411!
527
        }
528
        break;
2,088,543✔
529
      case TDMT_VND_TABLE_NAME:
525✔
530
        // error code has been set into reqMsg, no need to handle it here.
531
        if (TSDB_CODE_SUCCESS != vnodeGetTableMeta(pVnode, &reqMsg, false)) {
525✔
532
          qWarn("vnodeGetBatchName failed, msgType:%d", req->msgType);
127!
533
        }
534
        break;
526✔
535
      case TDMT_VND_TABLE_CFG:
101✔
536
        // error code has been set into reqMsg, no need to handle it here.
537
        if (TSDB_CODE_SUCCESS != vnodeGetTableCfg(pVnode, &reqMsg, false)) {
101!
538
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
×
539
        }
540
        break;
101✔
541
      case TDMT_VND_GET_STREAM_PROGRESS:
8,480✔
542
        // error code has been set into reqMsg, no need to handle it here.
543
        if (TSDB_CODE_SUCCESS != vnodeGetStreamProgress(pVnode, &reqMsg, false)) {
8,480!
544
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
×
545
        }
546
        break;
8,482✔
547
      case TDMT_VND_VSUBTABLES_META:
×
548
        // error code has been set into reqMsg, no need to handle it here.
549
        if (TSDB_CODE_SUCCESS != vnodeGetVSubtablesMeta(pVnode, &reqMsg)) {
×
550
          qWarn("vnodeGetVSubtablesMeta failed, msgType:%d", req->msgType);
×
551
        }
552
        break;
×
553
      case TDMT_VND_VSTB_REF_DBS:
×
554
        // error code has been set into reqMsg, no need to handle it here.
555
        if (TSDB_CODE_SUCCESS != vnodeGetVStbRefDbs(pVnode, &reqMsg)) {
×
556
          qWarn("vnodeGetVStbRefDbs failed, msgType:%d", req->msgType);
×
557
        }
558
        break;
×
559
      default:
×
560
        qError("invalid req msgType %d", req->msgType);
×
561
        reqMsg.code = TSDB_CODE_INVALID_MSG;
×
562
        reqMsg.pCont = NULL;
×
563
        reqMsg.contLen = 0;
×
564
        break;
×
565
    }
566

567
    rsp.msgIdx = req->msgIdx;
2,097,652✔
568
    rsp.reqType = reqMsg.msgType;
2,097,652✔
569
    rsp.msgLen = reqMsg.contLen;
2,097,652✔
570
    rsp.rspCode = reqMsg.code;
2,097,652✔
571
    rsp.msg = reqMsg.pCont;
2,097,652✔
572

573
    if (NULL == taosArrayPush(batchRsp.pRsps, &rsp)) {
4,194,746!
574
      qError("taosArrayPush failed");
×
575
      code = terrno;
×
576
      goto _exit;
×
577
    }
578
  }
579

580
  rspSize = tSerializeSBatchRsp(NULL, 0, &batchRsp);
1,196,828✔
581
  if (rspSize < 0) {
1,197,784!
582
    qError("tSerializeSBatchRsp failed");
×
583
    code = terrno;
×
584
    goto _exit;
×
585
  }
586
  pRsp = rpcMallocCont(rspSize);
1,197,784✔
587
  if (pRsp == NULL) {
1,197,842!
588
    qError("rpcMallocCont %d failed", rspSize);
×
589
    code = terrno;
×
590
    goto _exit;
×
591
  }
592
  if (tSerializeSBatchRsp(pRsp, rspSize, &batchRsp) < 0) {
1,197,842!
593
    qError("tSerializeSBatchRsp %d failed", rspSize);
×
594
    code = terrno;
×
595
    goto _exit;
×
596
  }
597

598
_exit:
1,197,887✔
599

600
  rspMsg.info = pMsg->info;
1,197,887✔
601
  rspMsg.pCont = pRsp;
1,197,887✔
602
  rspMsg.contLen = rspSize;
1,197,887✔
603
  rspMsg.code = code;
1,197,887✔
604
  rspMsg.msgType = pMsg->msgType;
1,197,887✔
605

606
  if (code) {
1,197,887!
607
    qError("vnd get batch meta failed cause of %s", tstrerror(code));
×
608
  }
609

610
  taosArrayDestroyEx(batchReq.pMsgs, tFreeSBatchReqMsg);
1,197,887✔
611
  taosArrayDestroyEx(batchRsp.pRsps, tFreeSBatchRspMsg);
1,197,851✔
612

613
  tmsgSendRsp(&rspMsg);
1,197,839✔
614

615
  return code;
1,197,823✔
616
}
617

618
#define VNODE_DO_META_QUERY(pVnode, cmd)                 \
619
  do {                                                   \
620
    (void)taosThreadRwlockRdlock(&(pVnode)->metaRWLock); \
621
    cmd;                                                 \
622
    (void)taosThreadRwlockUnlock(&(pVnode)->metaRWLock); \
623
  } while (0)
624

625
int32_t vnodeReadVSubtables(SReadHandle* pHandle, int64_t suid, SArray** ppRes) {
×
626
  int32_t                    code = TSDB_CODE_SUCCESS;
×
627
  int32_t                    line = 0;
×
628
  SMetaReader                mr = {0};
×
629
  bool                       readerInit = false;
×
630
  SVCTableRefCols*           pTb = NULL;
×
631
  int32_t                    refColsNum = 0;
×
632
  char                       tbFName[TSDB_TABLE_FNAME_LEN];
633
  SSHashObj*                 pSrcTbls = NULL;
×
634

635
  SArray *pList = taosArrayInit(10, sizeof(uint64_t));
×
636
  QUERY_CHECK_NULL(pList, code, line, _return, terrno);
×
637

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

640
  size_t num = taosArrayGetSize(pList);
×
641
  *ppRes = taosArrayInit(num, POINTER_BYTES);
×
642
  QUERY_CHECK_NULL(*ppRes, code, line, _return, terrno);
×
643
  pSrcTbls = tSimpleHashInit(10, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
×
644
  QUERY_CHECK_NULL(pSrcTbls, code, line, _return, terrno);
×
645

646
  for (int32_t i = 0; i < num; ++i) {
×
647
    uint64_t* id = taosArrayGet(pList, i);
×
648
    QUERY_CHECK_NULL(id, code, line, _return, terrno);
×
649
    pHandle->api.metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pHandle->api.metaFn);
×
650
    QUERY_CHECK_CODE(pHandle->api.metaReaderFn.getTableEntryByUid(&mr, *id), line, _return);
×
651
    readerInit = true;
×
652

653
    refColsNum = 0;
×
654
    for (int32_t j = 0; j < mr.me.colRef.nCols; j++) {
×
655
      if (mr.me.colRef.pColRef[j].hasRef) {
×
656
        refColsNum++;
×
657
      }
658
    }
659

660
    if (refColsNum <= 0) {
×
661
      pHandle->api.metaReaderFn.clearReader(&mr);
×
662
      readerInit = false;
×
663
      continue;
×
664
    }
665

666
    pTb = taosMemoryCalloc(1, refColsNum * sizeof(SRefColInfo) + sizeof(*pTb));
×
667
    QUERY_CHECK_NULL(pTb, code, line, _return, terrno);
×
668

669
    pTb->uid = mr.me.uid;
×
670
    pTb->numOfColRefs = refColsNum;
×
671
    pTb->refCols = (SRefColInfo*)(pTb + 1);
×
672

673
    refColsNum = 0;
×
674
    tSimpleHashClear(pSrcTbls);
×
675
    for (int32_t j = 0; j < mr.me.colRef.nCols; j++) {
×
676
      if (!mr.me.colRef.pColRef[j].hasRef) {
×
677
        continue;
×
678
      }
679

680
      pTb->refCols[refColsNum].colId = mr.me.colRef.pColRef[j].id;
×
681
      tstrncpy(pTb->refCols[refColsNum].refColName, mr.me.colRef.pColRef[j].refColName, TSDB_COL_NAME_LEN);
×
682
      tstrncpy(pTb->refCols[refColsNum].refTableName, mr.me.colRef.pColRef[j].refTableName, TSDB_TABLE_NAME_LEN);
×
683
      tstrncpy(pTb->refCols[refColsNum].refDbName, mr.me.colRef.pColRef[j].refDbName, TSDB_DB_NAME_LEN);
×
684

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

687
      if (NULL == tSimpleHashGet(pSrcTbls, tbFName, strlen(tbFName))) {
×
688
        QUERY_CHECK_CODE(tSimpleHashPut(pSrcTbls, tbFName, strlen(tbFName), &code, sizeof(code)), line, _return);
×
689
      }
690

691
      refColsNum++;
×
692
    }
693

694
    pTb->numOfSrcTbls = tSimpleHashGetSize(pSrcTbls);
×
695
    QUERY_CHECK_NULL(taosArrayPush(*ppRes, &pTb), code, line, _return, terrno);
×
696
    pTb = NULL;
×
697

698
    pHandle->api.metaReaderFn.clearReader(&mr);
×
699
    readerInit = false;
×
700
  }
701

702
_return:
×
703

704
  if (readerInit) {
×
705
    pHandle->api.metaReaderFn.clearReader(&mr);
×
706
  }
707

708
  taosArrayDestroy(pList);
×
709
  taosMemoryFree(pTb);
×
710
  tSimpleHashCleanup(pSrcTbls);
×
711

712
  if (code) {
×
713
    qError("%s failed since %s", __func__, tstrerror(code));
×
714
  }
715
  return code;
×
716
}
717

718
int32_t vnodeReadVStbRefDbs(SReadHandle* pHandle, int64_t suid, SArray** ppRes) {
×
719
  int32_t                    code = TSDB_CODE_SUCCESS;
×
720
  int32_t                    line = 0;
×
721
  SMetaReader                mr = {0};
×
722
  bool                       readerInit = false;
×
723
  SSHashObj*                 pDbNameHash = NULL;
×
724
  SArray*                    pList = NULL;
×
725

726
  pList = taosArrayInit(10, sizeof(uint64_t));
×
727
  QUERY_CHECK_NULL(pList, code, line, _return, terrno);
×
728

729
  *ppRes = taosArrayInit(10, POINTER_BYTES);
×
730
  QUERY_CHECK_NULL(*ppRes, code, line, _return, terrno)
×
731
  
732
  // lookup in cache
733
  code = pHandle->api.metaFn.metaGetCachedRefDbs(pHandle->vnode, suid, *ppRes);
×
734
  QUERY_CHECK_CODE(code, line, _return);
×
735

736
  if (taosArrayGetSize(*ppRes) > 0) {
×
737
    // found in cache
738
    goto _return;
×
739
  } else {
740
    code = pHandle->api.metaFn.getChildTableList(pHandle->vnode, suid, pList);
×
741
    QUERY_CHECK_CODE(code, line, _return);
×
742

743
    size_t num = taosArrayGetSize(pList);
×
744
    pDbNameHash = tSimpleHashInit(10, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
×
745
    QUERY_CHECK_NULL(pDbNameHash, code, line, _return, terrno);
×
746

747
    for (int32_t i = 0; i < num; ++i) {
×
748
      uint64_t* id = taosArrayGet(pList, i);
×
749
      QUERY_CHECK_NULL(id, code, line, _return, terrno);
×
750

751
      pHandle->api.metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pHandle->api.metaFn);
×
752
      readerInit = true;
×
753

754
      code = pHandle->api.metaReaderFn.getTableEntryByUid(&mr, *id);
×
755
      QUERY_CHECK_CODE(code, line, _return);
×
756

757
      for (int32_t j = 0; j < mr.me.colRef.nCols; j++) {
×
758
        if (mr.me.colRef.pColRef[j].hasRef) {
×
759
          if (NULL == tSimpleHashGet(pDbNameHash, mr.me.colRef.pColRef[j].refDbName, strlen(mr.me.colRef.pColRef[j].refDbName))) {
×
760
            char *refDbName = taosStrdup(mr.me.colRef.pColRef[j].refDbName);
×
761
            QUERY_CHECK_NULL(refDbName, code, line, _return, terrno);
×
762

763
            QUERY_CHECK_NULL(taosArrayPush(*ppRes, &refDbName), code, line, _return, terrno);
×
764

765
            code = tSimpleHashPut(pDbNameHash, refDbName, strlen(refDbName), NULL, 0);
×
766
            QUERY_CHECK_CODE(code, line, _return);
×
767
          }
768
        }
769
      }
770

771
      pHandle->api.metaReaderFn.clearReader(&mr);
×
772
      readerInit = false;
×
773
    }
774

775
    code = pHandle->api.metaFn.metaPutRefDbsToCache(pHandle->vnode, suid, *ppRes);
×
776
    QUERY_CHECK_CODE(code, line, _return);
×
777
  }
778

779
_return:
×
780

781
  if (readerInit) {
×
782
    pHandle->api.metaReaderFn.clearReader(&mr);
×
783
  }
784

785
  taosArrayDestroy(pList);
×
786
  tSimpleHashCleanup(pDbNameHash);
×
787

788
  if (code) {
×
789
    qError("%s failed since %s", __func__, tstrerror(code));
×
790
  }
791
  return code;
×
792
}
793

794
int32_t vnodeGetVSubtablesMeta(SVnode *pVnode, SRpcMsg *pMsg) {
×
795
  int32_t        code = 0;
×
796
  int32_t        rspSize = 0;
×
797
  SVSubTablesReq req = {0};
×
798
  SVSubTablesRsp rsp = {0};
×
799
  SRpcMsg      rspMsg = {0};
×
800
  void        *pRsp = NULL;
×
801
  int32_t      line = 0;
×
802

803
  if (tDeserializeSVSubTablesReq(pMsg->pCont, pMsg->contLen, &req)) {
×
804
    code = terrno;
×
805
    qError("tDeserializeSVSubTablesReq failed");
×
806
    goto _return;
×
807
  }
808

809
  SReadHandle handle = {.vnode = pVnode};
×
810
  initStorageAPI(&handle.api);
×
811

812
  QUERY_CHECK_CODE(vnodeReadVSubtables(&handle, req.suid, &rsp.pTables), line, _return);
×
813
  rsp.vgId = TD_VID(pVnode);
×
814

815
  rspSize = tSerializeSVSubTablesRsp(NULL, 0, &rsp);
×
816
  if (rspSize < 0) {
×
817
    code = rspSize;
×
818
    qError("tSerializeSVSubTablesRsp failed, error:%d", rspSize);
×
819
    goto _return;
×
820
  }
821
  pRsp = taosMemoryCalloc(1, rspSize);
×
822
  if (pRsp == NULL) {
×
823
    code = terrno;
×
824
    qError("rpcMallocCont %d failed, error:%d", rspSize, terrno);
×
825
    goto _return;
×
826
  }
827
  rspSize = tSerializeSVSubTablesRsp(pRsp, rspSize, &rsp);
×
828
  if (rspSize < 0) {
×
829
    code = rspSize;
×
830
    qError("tSerializeSVSubTablesRsp failed, error:%d", rspSize);
×
831
    goto _return;
×
832
  }
833

834
_return:
×
835

836
  rspMsg.info = pMsg->info;
×
837
  rspMsg.pCont = pRsp;
×
838
  rspMsg.contLen = rspSize;
×
839
  rspMsg.code = code;
×
840
  rspMsg.msgType = pMsg->msgType;
×
841

842
  if (code) {
×
843
    qError("vnd get virtual subtables failed cause of %s", tstrerror(code));
×
844
  }
845

846
  *pMsg = rspMsg;
×
847
  
848
  tDestroySVSubTablesRsp(&rsp);
×
849

850
  //tmsgSendRsp(&rspMsg);
851

852
  return code;
×
853
}
854

855
int32_t vnodeGetVStbRefDbs(SVnode *pVnode, SRpcMsg *pMsg) {
×
856
  int32_t        code = 0;
×
857
  int32_t        rspSize = 0;
×
858
  SVStbRefDbsReq req = {0};
×
859
  SVStbRefDbsRsp rsp = {0};
×
860
  SRpcMsg        rspMsg = {0};
×
861
  void          *pRsp = NULL;
×
862
  int32_t        line = 0;
×
863

864
  if (tDeserializeSVStbRefDbsReq(pMsg->pCont, pMsg->contLen, &req)) {
×
865
    code = terrno;
×
866
    qError("tDeserializeSVSubTablesReq failed");
×
867
    goto _return;
×
868
  }
869

870
  SReadHandle handle = {.vnode = pVnode};
×
871
  initStorageAPI(&handle.api);
×
872

873
  code = vnodeReadVStbRefDbs(&handle, req.suid, &rsp.pDbs);
×
874
  QUERY_CHECK_CODE(code, line, _return);
×
875
  rsp.vgId = TD_VID(pVnode);
×
876

877
  rspSize = tSerializeSVStbRefDbsRsp(NULL, 0, &rsp);
×
878
  if (rspSize < 0) {
×
879
    code = rspSize;
×
880
    qError("tSerializeSVStbRefDbsRsp failed, error:%d", rspSize);
×
881
    goto _return;
×
882
  }
883
  pRsp = taosMemoryCalloc(1, rspSize);
×
884
  if (pRsp == NULL) {
×
885
    code = terrno;
×
886
    qError("rpcMallocCont %d failed, error:%d", rspSize, terrno);
×
887
    goto _return;
×
888
  }
889
  rspSize = tSerializeSVStbRefDbsRsp(pRsp, rspSize, &rsp);
×
890
  if (rspSize < 0) {
×
891
    code = rspSize;
×
892
    qError("tSerializeSVStbRefDbsRsp failed, error:%d", rspSize);
×
893
    goto _return;
×
894
  }
895

896
_return:
×
897

898
  rspMsg.info = pMsg->info;
×
899
  rspMsg.pCont = pRsp;
×
900
  rspMsg.contLen = rspSize;
×
901
  rspMsg.code = code;
×
902
  rspMsg.msgType = pMsg->msgType;
×
903

904
  if (code) {
×
905
    qError("vnd get virtual stb ref db failed cause of %s", tstrerror(code));
×
906
  }
907

908
  *pMsg = rspMsg;
×
909

910
  tDestroySVStbRefDbsRsp(&rsp);
×
911

912
  return code;
×
913
}
914

915

916
int32_t vnodeGetLoad(SVnode *pVnode, SVnodeLoad *pLoad) {
1,016,730✔
917
  SSyncState state = syncGetState(pVnode->sync);
1,016,730✔
918
  pLoad->syncAppliedIndex = pVnode->state.applied;
1,016,730✔
919
  syncGetCommitIndex(pVnode->sync, &pLoad->syncCommitIndex);
1,016,730✔
920

921
  pLoad->vgId = TD_VID(pVnode);
1,016,730✔
922
  pLoad->syncState = state.state;
1,016,730✔
923
  pLoad->syncRestore = state.restored;
1,016,730✔
924
  pLoad->syncTerm = state.term;
1,016,730✔
925
  pLoad->roleTimeMs = state.roleTimeMs;
1,016,730✔
926
  pLoad->startTimeMs = state.startTimeMs;
1,016,730✔
927
  pLoad->syncCanRead = state.canRead;
1,016,730✔
928
  pLoad->learnerProgress = state.progress;
1,016,730✔
929
  pLoad->cacheUsage = tsdbCacheGetUsage(pVnode);
1,016,730✔
930
  pLoad->numOfCachedTables = tsdbCacheGetElems(pVnode);
1,016,730✔
931
  VNODE_DO_META_QUERY(pVnode, pLoad->numOfTables = metaGetTbNum(pVnode->pMeta));
1,016,730✔
932
  VNODE_DO_META_QUERY(pVnode, pLoad->numOfTimeSeries = metaGetTimeSeriesNum(pVnode->pMeta, 1));
1,016,730✔
933
  pLoad->totalStorage = (int64_t)3 * 1073741824;
1,016,730✔
934
  pLoad->compStorage = (int64_t)2 * 1073741824;
1,016,730✔
935
  pLoad->pointsWritten = 100;
1,016,730✔
936
  pLoad->numOfSelectReqs = 1;
1,016,730✔
937
  pLoad->numOfInsertReqs = atomic_load_64(&pVnode->statis.nInsert);
1,016,730✔
938
  pLoad->numOfInsertSuccessReqs = atomic_load_64(&pVnode->statis.nInsertSuccess);
1,016,730✔
939
  pLoad->numOfBatchInsertReqs = atomic_load_64(&pVnode->statis.nBatchInsert);
1,016,730✔
940
  pLoad->numOfBatchInsertSuccessReqs = atomic_load_64(&pVnode->statis.nBatchInsertSuccess);
1,016,730✔
941
  return 0;
1,016,730✔
942
}
943

944
int32_t vnodeGetLoadLite(SVnode *pVnode, SVnodeLoadLite *pLoad) {
×
945
  SSyncState syncState = syncGetState(pVnode->sync);
×
946
  if (syncState.state == TAOS_SYNC_STATE_LEADER || syncState.state == TAOS_SYNC_STATE_ASSIGNED_LEADER) {
×
947
    pLoad->vgId = TD_VID(pVnode);
×
948
    pLoad->nTimeSeries = metaGetTimeSeriesNum(pVnode->pMeta, 1);
×
949
    return 0;
×
950
  }
951
  return -1;
×
952
}
953
/**
954
 * @brief Reset the statistics value by monitor interval
955
 *
956
 * @param pVnode
957
 * @param pLoad
958
 */
959
void vnodeResetLoad(SVnode *pVnode, SVnodeLoad *pLoad) {
32✔
960
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nInsert, pLoad->numOfInsertReqs, 64, "nInsert");
32!
961
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nInsertSuccess, pLoad->numOfInsertSuccessReqs, 64, "nInsertSuccess");
32!
962
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nBatchInsert, pLoad->numOfBatchInsertReqs, 64, "nBatchInsert");
32!
963
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nBatchInsertSuccess, pLoad->numOfBatchInsertSuccessReqs, 64,
32!
964
                            "nBatchInsertSuccess");
965
}
32✔
966

967
void vnodeGetInfo(void *pVnode, const char **dbname, int32_t *vgId, int64_t *numOfTables, int64_t *numOfNormalTables) {
960,952✔
968
  SVnode    *pVnodeObj = pVnode;
960,952✔
969
  SVnodeCfg *pConf = &pVnodeObj->config;
960,952✔
970

971
  if (dbname) {
960,952!
972
    *dbname = pConf->dbname;
961,833✔
973
  }
974

975
  if (vgId) {
960,952!
976
    *vgId = TD_VID(pVnodeObj);
960,953✔
977
  }
978

979
  if (numOfTables) {
960,952!
980
    *numOfTables = pConf->vndStats.numOfNTables + pConf->vndStats.numOfCTables;
×
981
  }
982

983
  if (numOfNormalTables) {
960,952!
984
    *numOfNormalTables = pConf->vndStats.numOfNTables;
×
985
  }
986
}
960,952✔
987

988
int32_t vnodeGetTableList(void *pVnode, int8_t type, SArray *pList) {
×
989
  if (type == TSDB_SUPER_TABLE) {
×
990
    return vnodeGetStbIdList(pVnode, 0, pList);
×
991
  } else {
992
    return TSDB_CODE_INVALID_PARA;
×
993
  }
994
}
995

996
int32_t vnodeGetAllTableList(SVnode *pVnode, uint64_t uid, SArray *list) {
×
997
  int32_t      code = TSDB_CODE_SUCCESS;
×
998
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, uid, 1);
×
999
  if (NULL == pCur) {
×
1000
    qError("vnode get all table list failed");
×
1001
    return terrno;
×
1002
  }
1003

1004
  while (1) {
×
1005
    tb_uid_t id = metaCtbCursorNext(pCur);
×
1006
    if (id == 0) {
×
1007
      break;
×
1008
    }
1009

1010
    STableKeyInfo info = {uid = id};
×
1011
    if (NULL == taosArrayPush(list, &info)) {
×
1012
      qError("taosArrayPush failed");
×
1013
      code = terrno;
×
1014
      goto _exit;
×
1015
    }
1016
  }
1017
_exit:
×
1018
  metaCloseCtbCursor(pCur);
×
1019
  return code;
×
1020
}
1021

1022
int32_t vnodeGetCtbIdListByFilter(SVnode *pVnode, int64_t suid, SArray *list, bool (*filter)(void *arg), void *arg) {
×
1023
  return 0;
×
1024
}
1025

1026
int32_t vnodeGetCtbIdList(void *pVnode, int64_t suid, SArray *list) {
2,757,139✔
1027
  int32_t      code = TSDB_CODE_SUCCESS;
2,757,139✔
1028
  SVnode      *pVnodeObj = pVnode;
2,757,139✔
1029
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnodeObj, suid, 1);
2,757,139✔
1030
  if (NULL == pCur) {
2,763,319!
1031
    qError("vnode get all table list failed");
×
1032
    return terrno;
×
1033
  }
1034

1035
  while (1) {
9,126,746✔
1036
    tb_uid_t id = metaCtbCursorNext(pCur);
11,890,065✔
1037
    if (id == 0) {
11,882,384✔
1038
      break;
2,765,921✔
1039
    }
1040

1041
    if (NULL == taosArrayPush(list, &id)) {
9,126,746!
1042
      qError("taosArrayPush failed");
×
1043
      code = terrno;
×
1044
      goto _exit;
×
1045
    }
1046
  }
1047

1048
_exit:
2,765,921✔
1049
  metaCloseCtbCursor(pCur);
2,765,921✔
1050
  return code;
2,764,663✔
1051
}
1052

1053
int32_t vnodeGetStbIdList(SVnode *pVnode, int64_t suid, SArray *list) {
153,590✔
1054
  int32_t      code = TSDB_CODE_SUCCESS;
153,590✔
1055
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, suid);
153,590✔
1056
  if (!pCur) {
153,591!
1057
    return TSDB_CODE_OUT_OF_MEMORY;
×
1058
  }
1059

1060
  while (1) {
115,101✔
1061
    tb_uid_t id = metaStbCursorNext(pCur);
268,692✔
1062
    if (id == 0) {
268,688✔
1063
      break;
153,591✔
1064
    }
1065

1066
    if (NULL == taosArrayPush(list, &id)) {
115,101!
1067
      qError("taosArrayPush failed");
×
1068
      code = terrno;
×
1069
      goto _exit;
×
1070
    }
1071
  }
1072

1073
_exit:
153,591✔
1074
  metaCloseStbCursor(pCur);
153,591✔
1075
  return code;
153,591✔
1076
}
1077

1078
int32_t vnodeGetStbIdListByFilter(SVnode *pVnode, int64_t suid, SArray *list, bool (*filter)(void *arg, void *arg1),
×
1079
                                  void *arg) {
1080
  int32_t      code = TSDB_CODE_SUCCESS;
×
1081
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, suid);
×
1082
  if (!pCur) {
×
1083
    return terrno;
×
1084
  }
1085

1086
  while (1) {
×
1087
    tb_uid_t id = metaStbCursorNext(pCur);
×
1088
    if (id == 0) {
×
1089
      break;
×
1090
    }
1091

1092
    if ((*filter) && (*filter)(arg, &id)) {
×
1093
      continue;
×
1094
    }
1095

1096
    if (NULL == taosArrayPush(list, &id)) {
×
1097
      qError("taosArrayPush failed");
×
1098
      code = terrno;
×
1099
      goto _exit;
×
1100
    }
1101
  }
1102

1103
_exit:
×
1104
  metaCloseStbCursor(pCur);
×
1105
  return code;
×
1106
}
1107

1108
int32_t vnodeGetCtbNum(SVnode *pVnode, int64_t suid, int64_t *num) {
21,291✔
1109
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, suid, 0);
21,291✔
1110
  if (!pCur) {
21,299!
1111
    return terrno;
×
1112
  }
1113

1114
  *num = 0;
21,300✔
1115
  while (1) {
20,867✔
1116
    tb_uid_t id = metaCtbCursorNext(pCur);
42,167✔
1117
    if (id == 0) {
42,168✔
1118
      break;
21,301✔
1119
    }
1120

1121
    ++(*num);
20,867✔
1122
  }
1123

1124
  metaCloseCtbCursor(pCur);
21,301✔
1125
  return TSDB_CODE_SUCCESS;
21,301✔
1126
}
1127

1128
int32_t vnodeGetStbColumnNum(SVnode *pVnode, tb_uid_t suid, int *num) {
21,291✔
1129
  SSchemaWrapper *pSW = metaGetTableSchema(pVnode->pMeta, suid, -1, 0, NULL);
21,291✔
1130
  if (pSW) {
21,299!
1131
    *num = pSW->nCols;
21,299✔
1132
    tDeleteSchemaWrapper(pSW);
1133
  } else {
1134
    *num = 2;
×
1135
  }
1136

1137
  return TSDB_CODE_SUCCESS;
21,300✔
1138
}
1139

1140
int32_t vnodeGetStbKeep(SVnode *pVnode, tb_uid_t suid, int64_t *keep) {
21,294✔
1141
  SMetaReader mr = {0};
21,294✔
1142
  metaReaderDoInit(&mr, pVnode->pMeta, META_READER_NOLOCK);
21,294✔
1143

1144
  int32_t code = metaReaderGetTableEntryByUid(&mr, suid);
21,295✔
1145
  if (code == TSDB_CODE_SUCCESS) {
21,300!
1146
    *keep = mr.me.stbEntry.keep;
21,300✔
1147
  } else {
1148
    *keep = 0;  // Default value if not found
×
1149
  }
1150

1151
  metaReaderClear(&mr);
21,300✔
1152
  return TSDB_CODE_SUCCESS;
21,301✔
1153
}
1154

1155
#ifdef TD_ENTERPRISE
1156
const char *tkLogStb[] = {"cluster_info",
1157
                          "data_dir",
1158
                          "dnodes_info",
1159
                          "d_info",
1160
                          "grants_info",
1161
                          "keeper_monitor",
1162
                          "logs",
1163
                          "log_dir",
1164
                          "log_summary",
1165
                          "m_info",
1166
                          "taosadapter_restful_http_request_fail",
1167
                          "taosadapter_restful_http_request_in_flight",
1168
                          "taosadapter_restful_http_request_summary_milliseconds",
1169
                          "taosadapter_restful_http_request_total",
1170
                          "taosadapter_system_cpu_percent",
1171
                          "taosadapter_system_mem_percent",
1172
                          "temp_dir",
1173
                          "vgroups_info",
1174
                          "vnodes_role"};
1175
const char *tkAuditStb[] = {"operations"};
1176
const int   tkLogStbNum = ARRAY_SIZE(tkLogStb);
1177
const int   tkAuditStbNum = ARRAY_SIZE(tkAuditStb);
1178

1179
// exclude stbs of taoskeeper log
1180
static int32_t vnodeGetTimeSeriesBlackList(SVnode *pVnode, int32_t *tbSize) {
153,574✔
1181
  int32_t      code = TSDB_CODE_SUCCESS;
153,574✔
1182
  int32_t      tbNum = 0;
153,574✔
1183
  const char **pTbArr = NULL;
153,574✔
1184
  const char  *dbName = NULL;
153,574✔
1185
  *tbSize = 0;
153,574✔
1186

1187
  if (!(dbName = strchr(pVnode->config.dbname, '.'))) return 0;
153,574!
1188
  if (0 == strncmp(++dbName, "log", TSDB_DB_NAME_LEN)) {
153,574!
1189
    tbNum = tkLogStbNum;
×
1190
    pTbArr = (const char **)&tkLogStb;
×
1191
  } else if (0 == strncmp(dbName, "audit", TSDB_DB_NAME_LEN)) {
153,574!
1192
    tbNum = tkAuditStbNum;
×
1193
    pTbArr = (const char **)&tkAuditStb;
×
1194
  }
1195
  if (tbNum && pTbArr) {
153,574!
1196
    *tbSize = metaSizeOfTbFilterCache(pVnode->pMeta, 0);
×
1197
    if (*tbSize < tbNum) {
×
1198
      for (int32_t i = 0; i < tbNum; ++i) {
×
1199
        tb_uid_t suid = metaGetTableEntryUidByName(pVnode->pMeta, pTbArr[i]);
×
1200
        if (suid != 0) {
×
1201
          code = metaPutTbToFilterCache(pVnode->pMeta, &suid, 0);
×
1202
          if (TSDB_CODE_SUCCESS != code) {
×
1203
            return code;
×
1204
          }
1205
        }
1206
      }
1207
      *tbSize = metaSizeOfTbFilterCache(pVnode->pMeta, 0);
×
1208
    }
1209
  }
1210

1211
  return code;
153,574✔
1212
}
1213
#endif
1214

1215
static bool vnodeTimeSeriesFilter(void *arg1, void *arg2) {
×
1216
  SVnode *pVnode = (SVnode *)arg1;
×
1217

1218
  if (metaTbInFilterCache(pVnode->pMeta, arg2, 0)) {
×
1219
    return true;
×
1220
  }
1221
  return false;
×
1222
}
1223

1224
int32_t vnodeGetTimeSeriesNum(SVnode *pVnode, int64_t *num) {
153,574✔
1225
  SArray *suidList = NULL;
153,574✔
1226

1227
  if (!(suidList = taosArrayInit(1, sizeof(tb_uid_t)))) {
153,574!
1228
    return terrno;
×
1229
  }
1230

1231
  int32_t tbFilterSize = 0;
153,574✔
1232
  int32_t code = TSDB_CODE_SUCCESS;
153,574✔
1233
#ifdef TD_ENTERPRISE
1234
  code = vnodeGetTimeSeriesBlackList(pVnode, &tbFilterSize);
153,574✔
1235
  if (TSDB_CODE_SUCCESS != code) {
153,572!
1236
    goto _exit;
×
1237
  }
1238
#endif
1239

1240
  if ((!tbFilterSize && vnodeGetStbIdList(pVnode, 0, suidList) < 0) ||
153,572!
1241
      (tbFilterSize && vnodeGetStbIdListByFilter(pVnode, 0, suidList, vnodeTimeSeriesFilter, pVnode) < 0)) {
153,573!
1242
    qError("vgId:%d, failed to get stb id list error: %s", TD_VID(pVnode), terrstr());
×
1243
    taosArrayDestroy(suidList);
×
1244
    return terrno;
×
1245
  }
1246

1247
  *num = 0;
153,574✔
1248
  int64_t arrSize = taosArrayGetSize(suidList);
153,574✔
1249
  for (int64_t i = 0; i < arrSize; ++i) {
268,725✔
1250
    tb_uid_t suid = *(tb_uid_t *)taosArrayGet(suidList, i);
115,149✔
1251

1252
    int64_t ctbNum = 0;
115,149✔
1253
    int32_t numOfCols = 0;
115,149✔
1254
    code = metaGetStbStats(pVnode, suid, &ctbNum, &numOfCols);
115,149✔
1255
    if (TSDB_CODE_SUCCESS != code) {
115,152!
1256
      goto _exit;
×
1257
    }
1258
    *num += ctbNum * (numOfCols - 1);
115,152✔
1259
  }
1260

1261
_exit:
153,576✔
1262
  taosArrayDestroy(suidList);
153,576✔
1263
  return TSDB_CODE_SUCCESS;
153,574✔
1264
}
1265

1266
int32_t vnodeGetAllCtbNum(SVnode *pVnode, int64_t *num) {
×
1267
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, 0);
×
1268
  if (!pCur) {
×
1269
    return terrno;
×
1270
  }
1271

1272
  *num = 0;
×
1273
  while (1) {
×
1274
    tb_uid_t id = metaStbCursorNext(pCur);
×
1275
    if (id == 0) {
×
1276
      break;
×
1277
    }
1278

1279
    int64_t ctbNum = 0;
×
1280
    int32_t code = vnodeGetCtbNum(pVnode, id, &ctbNum);
×
1281
    if (TSDB_CODE_SUCCESS != code) {
×
1282
      metaCloseStbCursor(pCur);
×
1283
      return code;
×
1284
    }
1285

1286
    *num += ctbNum;
×
1287
  }
1288

1289
  metaCloseStbCursor(pCur);
×
1290
  return TSDB_CODE_SUCCESS;
×
1291
}
1292

1293
void *vnodeGetIdx(void *pVnode) {
139,267✔
1294
  if (pVnode == NULL) {
139,267!
1295
    return NULL;
×
1296
  }
1297

1298
  return metaGetIdx(((SVnode *)pVnode)->pMeta);
139,267✔
1299
}
1300

1301
void *vnodeGetIvtIdx(void *pVnode) {
138,925✔
1302
  if (pVnode == NULL) {
138,925!
1303
    return NULL;
×
1304
  }
1305
  return metaGetIvtIdx(((SVnode *)pVnode)->pMeta);
138,925✔
1306
}
1307

1308
int32_t vnodeGetTableSchema(void *pVnode, int64_t uid, STSchema **pSchema, int64_t *suid) {
534✔
1309
  return tsdbGetTableSchema(((SVnode *)pVnode)->pMeta, uid, pSchema, suid);
534✔
1310
}
1311

1312
static FORCE_INLINE int32_t vnodeGetDBPrimaryInfo(SVnode *pVnode, SDbSizeStatisInfo *pInfo) {
1313
  int32_t code = 0;
22✔
1314
  char    path[TSDB_FILENAME_LEN] = {0};
22✔
1315

1316
  char   *dirName[] = {VNODE_TSDB_DIR, VNODE_WAL_DIR, VNODE_META_DIR, VNODE_TSDB_CACHE_DIR};
22✔
1317
  int64_t dirSize[4];
1318

1319
  vnodeGetPrimaryDir(pVnode->path, pVnode->diskPrimary, pVnode->pTfs, path, TSDB_FILENAME_LEN);
22✔
1320
  int32_t offset = strlen(path);
22✔
1321

1322
  for (int i = 0; i < sizeof(dirName) / sizeof(dirName[0]); i++) {
110✔
1323
    int64_t size = {0};
88✔
1324
    (void)snprintf(path + offset, TSDB_FILENAME_LEN, "%s%s", TD_DIRSEP, dirName[i]);
88✔
1325
    code = taosGetDirSize(path, &size);
88✔
1326
    if (code != 0) {
88!
1327
      return code;
×
1328
    }
1329
    path[offset] = 0;
88✔
1330
    dirSize[i] = size;
88✔
1331
  }
1332

1333
  pInfo->l1Size = 0;
22✔
1334
  pInfo->walSize = dirSize[1];
22✔
1335
  pInfo->metaSize = dirSize[2];
22✔
1336
  pInfo->cacheSize = dirSize[3];
22✔
1337
  return code;
22✔
1338
}
1339
int32_t vnodeGetDBSize(void *pVnode, SDbSizeStatisInfo *pInfo) {
22✔
1340
  int32_t code = 0;
22✔
1341
  int32_t lino = 0;
22✔
1342
  SVnode *pVnodeObj = pVnode;
22✔
1343
  if (pVnodeObj == NULL) {
22!
1344
    return TSDB_CODE_VND_NOT_EXIST;
×
1345
  }
1346
  code = vnodeGetDBPrimaryInfo(pVnode, pInfo);
22✔
1347
  if (code != 0) goto _exit;
22!
1348

1349
  code = tsdbGetFsSize(pVnodeObj->pTsdb, pInfo);
22✔
1350
_exit:
22✔
1351
  return code;
22✔
1352
}
1353

1354
int32_t vnodeGetStreamProgress(SVnode *pVnode, SRpcMsg *pMsg, bool direct) {
8,479✔
1355
  int32_t            code = 0;
8,479✔
1356
  SStreamProgressReq req;
1357
  SStreamProgressRsp rsp = {0};
8,479✔
1358
  SRpcMsg            rpcMsg = {.info = pMsg->info, .code = 0};
8,479✔
1359
  char              *buf = NULL;
8,479✔
1360
  int32_t            rspLen = 0;
8,479✔
1361
  code = tDeserializeStreamProgressReq(pMsg->pCont, pMsg->contLen, &req);
8,479✔
1362

1363
  if (code == TSDB_CODE_SUCCESS) {
8,481!
1364
    rsp.fetchIdx = req.fetchIdx;
8,481✔
1365
    rsp.subFetchIdx = req.subFetchIdx;
8,481✔
1366
    rsp.vgId = req.vgId;
8,481✔
1367
    rsp.streamId = req.streamId;
8,481✔
1368
    rspLen = tSerializeStreamProgressRsp(0, 0, &rsp);
8,481✔
1369
    if (rspLen < 0) {
8,477!
1370
      code = terrno;
×
1371
      goto _OVER;
×
1372
    }
1373
    if (direct) {
8,477!
1374
      buf = rpcMallocCont(rspLen);
×
1375
    } else {
1376
      buf = taosMemoryCalloc(1, rspLen);
8,477!
1377
    }
1378
    if (!buf) {
8,479!
1379
      code = terrno;
×
1380
      goto _OVER;
×
1381
    }
1382
  }
1383

1384
  if (code == TSDB_CODE_SUCCESS) {
8,479!
1385
    code = tqGetStreamExecInfo(pVnode, req.streamId, &rsp.progressDelay, &rsp.fillHisFinished);
8,479✔
1386
  }
1387
  if (code == TSDB_CODE_SUCCESS) {
8,482!
1388
    rspLen = tSerializeStreamProgressRsp(buf, rspLen, &rsp);
8,482✔
1389
    if (rspLen < 0) {
8,482!
1390
      code = terrno;
×
1391
      goto _OVER;
×
1392
    }
1393
    rpcMsg.pCont = buf;
8,482✔
1394
    buf = NULL;
8,482✔
1395
    rpcMsg.contLen = rspLen;
8,482✔
1396
    rpcMsg.code = code;
8,482✔
1397
    rpcMsg.msgType = pMsg->msgType;
8,482✔
1398
    if (direct) {
8,482!
1399
      tmsgSendRsp(&rpcMsg);
×
1400
    } else {
1401
      *pMsg = rpcMsg;
8,482✔
1402
    }
1403
  }
1404

1405
_OVER:
×
1406
  if (buf) {
8,482!
1407
    taosMemoryFree(buf);
×
1408
  }
1409
  return code;
8,482✔
1410
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc