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

taosdata / TDengine / #3951

28 Apr 2025 05:42AM UTC coverage: 62.445% (-0.4%) from 62.853%
#3951

push

travis-ci

web-flow
fix: mnode-status-case (#30871)

155256 of 317429 branches covered (48.91%)

Branch coverage included in aggregate %.

240626 of 316539 relevant lines covered (76.02%)

6221630.89 hits per line

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

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

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

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

36
int32_t fillTableColCmpr(SMetaReader *reader, SSchemaExt *pExt, int32_t numOfCol) {
63,476✔
37
  int8_t tblType = reader->me.type;
63,476✔
38
  if (withExtSchema(tblType)) {
63,476!
39
    SColCmprWrapper *p = &(reader->me.colCmpr);
63,476✔
40
    if (numOfCol != p->nCols) {
63,476!
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++) {
819,599✔
45
      SColCmpr *pCmpr = &p->pColCmpr[i];
756,123✔
46
      pExt[i].colId = pCmpr->id;
756,123✔
47
      pExt[i].compress = pCmpr->alg;
756,123✔
48
    }
49
  }
50
  return 0;
63,476✔
51
}
52

53
void vnodePrintTableMeta(STableMetaRsp *pMeta) {
63,476✔
54
  if (!(qDebugFlag & DEBUG_DEBUG)) {
63,476✔
55
    return;
31,058✔
56
  }
57

58
  qDebug("tbName:%s", pMeta->tbName);
32,418!
59
  qDebug("stbName:%s", pMeta->stbName);
32,418!
60
  qDebug("dbFName:%s", pMeta->dbFName);
32,418!
61
  qDebug("dbId:%" PRId64, pMeta->dbId);
32,418!
62
  qDebug("numOfTags:%d", pMeta->numOfTags);
32,418!
63
  qDebug("numOfColumns:%d", pMeta->numOfColumns);
32,418!
64
  qDebug("precision:%d", pMeta->precision);
32,418!
65
  qDebug("tableType:%d", pMeta->tableType);
32,418!
66
  qDebug("sversion:%d", pMeta->sversion);
32,418!
67
  qDebug("tversion:%d", pMeta->tversion);
32,418!
68
  qDebug("suid:%" PRIu64, pMeta->suid);
32,418!
69
  qDebug("tuid:%" PRIu64, pMeta->tuid);
32,418!
70
  qDebug("vgId:%d", pMeta->vgId);
32,418!
71
  qDebug("sysInfo:%d", pMeta->sysInfo);
32,418!
72
  if (pMeta->pSchemas) {
32,418!
73
    for (int32_t i = 0; i < (pMeta->numOfColumns + pMeta->numOfTags); ++i) {
464,380✔
74
      SSchema *pSchema = pMeta->pSchemas + i;
431,962✔
75
      qDebug("%d col/tag: type:%d, flags:%d, colId:%d, bytes:%d, name:%s", i, pSchema->type, pSchema->flags,
431,962!
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) {
104,476✔
104
  STableInfoReq  infoReq = {0};
104,476✔
105
  STableMetaRsp  metaRsp = {0};
104,476✔
106
  SMetaReader    mer1 = {0};
104,476✔
107
  SMetaReader    mer2 = {0};
104,476✔
108
  char           tableFName[TSDB_TABLE_FNAME_LEN];
109
  bool           reqTbUid = false;
104,476✔
110
  SRpcMsg        rpcMsg = {0};
104,476✔
111
  int32_t        code = 0;
104,476✔
112
  int32_t        rspLen = 0;
104,476✔
113
  void          *pRsp = NULL;
104,476✔
114
  SSchemaWrapper schema = {0};
104,476✔
115
  SSchemaWrapper schemaTag = {0};
104,476✔
116
  uint8_t        autoCreateCtb = 0;
104,476✔
117

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

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

130
  if (!reqTbUid) {
104,476✔
131
    (void)tsnprintf(tableFName, TSDB_TABLE_FNAME_LEN, "%s.%s", infoReq.dbFName, infoReq.tbName);
103,942✔
132
    code = vnodeValidateTableHash(pVnode, tableFName);
103,942✔
133
    if (code) {
103,942!
134
      goto _exit4;
×
135
    }
136
  }
137

138
  // query meta
139
  metaReaderDoInit(&mer1, pVnode->pMeta, META_READER_LOCK);
104,476✔
140
  if (reqTbUid) {
104,475✔
141
    SET_ERRNO(0);
534✔
142
    uint64_t tbUid = taosStr2UInt64(infoReq.tbName, NULL, 10);
534✔
143
    if (ERRNO == ERANGE || tbUid == 0) {
534!
144
      code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
145
      goto _exit3;
133✔
146
    }
147
    SMetaReader mr3 = {0};
534✔
148
    metaReaderDoInit(&mr3, ((SVnode *)pVnode)->pMeta, META_READER_NOLOCK);
534✔
149
    if ((code = metaReaderGetTableEntryByUid(&mr3, tbUid)) < 0) {
534✔
150
      metaReaderClear(&mr3);
133✔
151
      TAOS_CHECK_GOTO(code, NULL, _exit3);
133!
152
    }
153
    tstrncpy(metaRsp.tbName, mr3.me.name, TSDB_TABLE_NAME_LEN);
401✔
154
    metaReaderClear(&mr3);
401✔
155
    TAOS_CHECK_GOTO(metaGetTableEntryByName(&mer1, metaRsp.tbName), NULL, _exit3);
400!
156
  } else if (metaGetTableEntryByName(&mer1, infoReq.tbName) < 0) {
103,941✔
157
    code = terrno;
40,865✔
158
    goto _exit3;
40,866✔
159
  }
160

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

165
  switch (mer1.me.type) {
63,477!
166
    case TSDB_SUPER_TABLE: {
14,275✔
167
      (void)strcpy(metaRsp.stbName, mer1.me.name);
14,275✔
168
      schema = mer1.me.stbEntry.schemaRow;
14,275✔
169
      schemaTag = mer1.me.stbEntry.schemaTag;
14,275✔
170
      metaRsp.suid = mer1.me.uid;
14,275✔
171
      break;
14,275✔
172
    }
173
    case TSDB_CHILD_TABLE:
15,677✔
174
    case TSDB_VIRTUAL_CHILD_TABLE:{
175
      metaReaderDoInit(&mer2, pVnode->pMeta, META_READER_NOLOCK);
15,677✔
176
      if (metaReaderGetTableEntryByUid(&mer2, mer1.me.ctbEntry.suid) < 0) goto _exit2;
15,677!
177

178
      (void)strcpy(metaRsp.stbName, mer2.me.name);
15,676✔
179
      metaRsp.suid = mer2.me.uid;
15,676✔
180
      schema = mer2.me.stbEntry.schemaRow;
15,676✔
181
      schemaTag = mer2.me.stbEntry.schemaTag;
15,676✔
182
      break;
15,676✔
183
    }
184
    case TSDB_NORMAL_TABLE:
33,525✔
185
    case TSDB_VIRTUAL_NORMAL_TABLE: {
186
      schema = mer1.me.ntbEntry.schemaRow;
33,525✔
187
      break;
33,525✔
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;
63,476✔
196
  metaRsp.numOfColumns = schema.nCols;
63,476✔
197
  metaRsp.precision = pVnode->config.tsdbCfg.precision;
63,476✔
198
  metaRsp.sversion = schema.version;
63,476✔
199
  metaRsp.tversion = schemaTag.version;
63,476✔
200
  metaRsp.pSchemas = (SSchema *)taosMemoryMalloc(sizeof(SSchema) * (metaRsp.numOfColumns + metaRsp.numOfTags));
63,476!
201
  metaRsp.pSchemaExt = (SSchemaExt *)taosMemoryCalloc(metaRsp.numOfColumns, sizeof(SSchemaExt));
63,477!
202
  if (NULL == metaRsp.pSchemas || NULL == metaRsp.pSchemaExt) {
63,477!
203
    code = terrno;
×
204
    goto _exit;
×
205
  }
206
  (void)memcpy(metaRsp.pSchemas, schema.pSchema, sizeof(SSchema) * schema.nCols);
63,477✔
207
  if (schemaTag.nCols) {
63,477✔
208
    (void)memcpy(metaRsp.pSchemas + schema.nCols, schemaTag.pSchema, sizeof(SSchema) * schemaTag.nCols);
29,951✔
209
  }
210
  if (metaRsp.pSchemaExt) {
63,477✔
211
    SMetaReader *pReader = mer1.me.type == TSDB_CHILD_TABLE ? &mer2 : &mer1;
63,476✔
212
    code = fillTableColCmpr(pReader, metaRsp.pSchemaExt, metaRsp.numOfColumns);
63,476✔
213
    if (code < 0) {
63,476!
214
      goto _exit;
×
215
    }
216
    for (int32_t i = 0; i < metaRsp.numOfColumns && pReader->me.pExtSchemas; i++) {
425,033✔
217
      metaRsp.pSchemaExt[i].typeMod = pReader->me.pExtSchemas[i].typeMod;
361,557✔
218
    }
219
  } else {
220
    code = TSDB_CODE_OUT_OF_MEMORY;
1✔
221
    goto _exit;
1✔
222
  }
223
  if (hasRefCol(mer1.me.type)) {
63,476!
224
    metaRsp.pColRefs = (SColRef*)taosMemoryMalloc(sizeof(SColRef) * metaRsp.numOfColumns);
×
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;
63,476✔
234
    metaRsp.numOfColRefs = 0;
63,476✔
235
  }
236

237
  vnodePrintTableMeta(&metaRsp);
63,476✔
238

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

246
  if (direct) {
63,477✔
247
    pRsp = rpcMallocCont(rspLen);
141✔
248
  } else {
249
    pRsp = taosMemoryCalloc(1, rspLen);
63,336!
250
  }
251

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

257
  rspLen = tSerializeSTableMetaRsp(pRsp, rspLen, &metaRsp);
63,476✔
258
  if (rspLen < 0) {
63,477!
259
    code = terrno;
×
260
    goto _exit;
×
261
  }
262

263
_exit:
63,477✔
264
  taosMemoryFree(metaRsp.pColRefs);
63,478!
265
  taosMemoryFree(metaRsp.pSchemas);
63,477!
266
  taosMemoryFree(metaRsp.pSchemaExt);
63,477!
267
_exit2:
63,477✔
268
  metaReaderClear(&mer2);
63,477✔
269
_exit3:
104,476✔
270
  metaReaderClear(&mer1);
104,476✔
271
_exit4:
104,475✔
272
  rpcMsg.info = pMsg->info;
104,475✔
273
  rpcMsg.pCont = pRsp;
104,475✔
274
  rpcMsg.contLen = rspLen;
104,475✔
275
  rpcMsg.code = code;
104,475✔
276
  rpcMsg.msgType = pMsg->msgType;
104,475✔
277

278
  if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST && autoCreateCtb == 1) {
104,475✔
279
    code = TSDB_CODE_SUCCESS;
24,540✔
280
  }
281

282
  if (code) {
104,475✔
283
    qError("get table %s meta with %" PRIu8 " failed cause of %s", infoReq.tbName, infoReq.option, tstrerror(code));
16,459!
284
  }
285

286
  if (direct) {
104,476✔
287
    tmsgSendRsp(&rpcMsg);
699✔
288
  } else {
289
    *pMsg = rpcMsg;
103,777✔
290
  }
291

292
  return code;
104,476✔
293
}
294

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

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

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

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

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

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

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

333
  if (mer1.me.type == TSDB_SUPER_TABLE) {
82!
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) {
82!
337
    metaReaderDoInit(&mer2, pVnode->pMeta, META_READER_NOLOCK);
50✔
338
    if (metaReaderGetTableEntryByUid(&mer2, mer1.me.ctbEntry.suid) < 0) goto _exit;
50!
339

340
    tstrncpy(cfgRsp.stbName, mer2.me.name, TSDB_TABLE_NAME_LEN);
50✔
341
    schema = mer2.me.stbEntry.schemaRow;
50✔
342
    schemaTag = mer2.me.stbEntry.schemaTag;
50✔
343
    cfgRsp.ttl = mer1.me.ctbEntry.ttlDays;
50✔
344
    cfgRsp.commentLen = mer1.me.ctbEntry.commentLen;
50✔
345
    if (mer1.me.ctbEntry.commentLen > 0) {
50!
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;
50✔
353
    cfgRsp.tagsLen = pTag->len;
50✔
354
    cfgRsp.pTags = taosMemoryMalloc(cfgRsp.tagsLen);
50!
355
    if (NULL == cfgRsp.pTags) {
50!
356
      code = terrno;
×
357
      goto _exit;
×
358
    }
359
    (void)memcpy(cfgRsp.pTags, pTag, cfgRsp.tagsLen);
50✔
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;
82✔
378
  cfgRsp.numOfColumns = schema.nCols;
82✔
379
  cfgRsp.virtualStb = false; // vnode don't have super table, so it's always false
82✔
380
  cfgRsp.pSchemas = (SSchema *)taosMemoryMalloc(sizeof(SSchema) * (cfgRsp.numOfColumns + cfgRsp.numOfTags));
82!
381
  cfgRsp.pSchemaExt = (SSchemaExt *)taosMemoryMalloc(cfgRsp.numOfColumns * sizeof(SSchemaExt));
82!
382
  cfgRsp.pColRefs = (SColRef *)taosMemoryMalloc(sizeof(SColRef) * cfgRsp.numOfColumns);
82!
383

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

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

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

410
  cfgRsp.virtualStb = false;
82✔
411
  if (hasRefCol(cfgRsp.tableType)) {
82!
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);
82✔
426
  if (rspLen < 0) {
82!
427
    code = terrno;
×
428
    goto _exit;
×
429
  }
430

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

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

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

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

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

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

465
  tFreeSTableCfgRsp(&cfgRsp);
82✔
466
  metaReaderClear(&mer2);
82✔
467
  metaReaderClear(&mer1);
82✔
468
  return code;
82✔
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) {
70,704✔
481
  int32_t      code = 0;
70,704✔
482
  int32_t      rspSize = 0;
70,704✔
483
  SBatchReq    batchReq = {0};
70,704✔
484
  SBatchMsg   *req = NULL;
70,704✔
485
  SBatchRspMsg rsp = {0};
70,704✔
486
  SBatchRsp    batchRsp = {0};
70,704✔
487
  SRpcMsg      reqMsg = *pMsg;
70,704✔
488
  SRpcMsg      rspMsg = {0};
70,704✔
489
  void        *pRsp = NULL;
70,704✔
490

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

497
  int32_t msgNum = taosArrayGetSize(batchReq.pMsgs);
70,704✔
498
  if (msgNum >= MAX_META_MSG_IN_BATCH) {
70,704!
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));
70,704✔
505
  if (NULL == batchRsp.pRsps) {
70,705!
506
    code = terrno;
×
507
    qError("taosArrayInit %d SBatchRspMsg failed", msgNum);
×
508
    goto _exit;
×
509
  }
510

511
  for (int32_t i = 0; i < msgNum; ++i) {
177,888✔
512
    req = taosArrayGet(batchReq.pMsgs, i);
107,183✔
513
    if (req == NULL) {
107,183!
514
      code = terrno;
×
515
      goto _exit;
×
516
    }
517

518
    reqMsg.msgType = req->msgType;
107,183✔
519
    reqMsg.pCont = req->msg;
107,183✔
520
    reqMsg.contLen = req->msgLen;
107,183✔
521

522
    switch (req->msgType) {
107,183!
523
      case TDMT_VND_TABLE_META:
103,243✔
524
        // error code has been set into reqMsg, no need to handle it here.
525
        if (TSDB_CODE_SUCCESS != vnodeGetTableMeta(pVnode, &reqMsg, false)) {
103,243✔
526
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
15,768!
527
        }
528
        break;
103,243✔
529
      case TDMT_VND_TABLE_NAME:
534✔
530
        // error code has been set into reqMsg, no need to handle it here.
531
        if (TSDB_CODE_SUCCESS != vnodeGetTableMeta(pVnode, &reqMsg, false)) {
534✔
532
          qWarn("vnodeGetBatchName failed, msgType:%d", req->msgType);
133!
533
        }
534
        break;
534✔
535
      case TDMT_VND_TABLE_CFG:
82✔
536
        // error code has been set into reqMsg, no need to handle it here.
537
        if (TSDB_CODE_SUCCESS != vnodeGetTableCfg(pVnode, &reqMsg, false)) {
82!
538
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
×
539
        }
540
        break;
82✔
541
      case TDMT_VND_GET_STREAM_PROGRESS:
3,324✔
542
        // error code has been set into reqMsg, no need to handle it here.
543
        if (TSDB_CODE_SUCCESS != vnodeGetStreamProgress(pVnode, &reqMsg, false)) {
3,324!
544
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
×
545
        }
546
        break;
3,324✔
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;
107,183✔
568
    rsp.reqType = reqMsg.msgType;
107,183✔
569
    rsp.msgLen = reqMsg.contLen;
107,183✔
570
    rsp.rspCode = reqMsg.code;
107,183✔
571
    rsp.msg = reqMsg.pCont;
107,183✔
572

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

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

598
_exit:
70,705✔
599

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

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

610
  taosArrayDestroyEx(batchReq.pMsgs, tFreeSBatchReqMsg);
70,705✔
611
  taosArrayDestroyEx(batchRsp.pRsps, tFreeSBatchRspMsg);
70,705✔
612

613
  tmsgSendRsp(&rspMsg);
70,705✔
614

615
  return code;
70,705✔
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) {
206,184✔
917
  SSyncState state = syncGetState(pVnode->sync);
206,184✔
918
  pLoad->syncAppliedIndex = pVnode->state.applied;
206,184✔
919
  syncGetCommitIndex(pVnode->sync, &pLoad->syncCommitIndex);
206,184✔
920

921
  pLoad->vgId = TD_VID(pVnode);
206,184✔
922
  pLoad->syncState = state.state;
206,184✔
923
  pLoad->syncRestore = state.restored;
206,184✔
924
  pLoad->syncTerm = state.term;
206,184✔
925
  pLoad->roleTimeMs = state.roleTimeMs;
206,184✔
926
  pLoad->startTimeMs = state.startTimeMs;
206,184✔
927
  pLoad->syncCanRead = state.canRead;
206,184✔
928
  pLoad->learnerProgress = state.progress;
206,184✔
929
  pLoad->cacheUsage = tsdbCacheGetUsage(pVnode);
206,184✔
930
  pLoad->numOfCachedTables = tsdbCacheGetElems(pVnode);
206,184✔
931
  VNODE_DO_META_QUERY(pVnode, pLoad->numOfTables = metaGetTbNum(pVnode->pMeta));
206,184✔
932
  VNODE_DO_META_QUERY(pVnode, pLoad->numOfTimeSeries = metaGetTimeSeriesNum(pVnode->pMeta, 1));
206,184✔
933
  pLoad->totalStorage = (int64_t)3 * 1073741824;
206,184✔
934
  pLoad->compStorage = (int64_t)2 * 1073741824;
206,184✔
935
  pLoad->pointsWritten = 100;
206,184✔
936
  pLoad->numOfSelectReqs = 1;
206,184✔
937
  pLoad->numOfInsertReqs = atomic_load_64(&pVnode->statis.nInsert);
206,184✔
938
  pLoad->numOfInsertSuccessReqs = atomic_load_64(&pVnode->statis.nInsertSuccess);
206,184✔
939
  pLoad->numOfBatchInsertReqs = atomic_load_64(&pVnode->statis.nBatchInsert);
206,184✔
940
  pLoad->numOfBatchInsertSuccessReqs = atomic_load_64(&pVnode->statis.nBatchInsertSuccess);
206,184✔
941
  return 0;
206,184✔
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) {
24,714✔
968
  SVnode    *pVnodeObj = pVnode;
24,714✔
969
  SVnodeCfg *pConf = &pVnodeObj->config;
24,714✔
970

971
  if (dbname) {
24,714!
972
    *dbname = pConf->dbname;
24,718✔
973
  }
974

975
  if (vgId) {
24,714✔
976
    *vgId = TD_VID(pVnodeObj);
23,231✔
977
  }
978

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

983
  if (numOfNormalTables) {
24,714!
984
    *numOfNormalTables = pConf->vndStats.numOfNTables;
×
985
  }
986
}
24,714✔
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) {
707,869✔
1027
  int32_t      code = TSDB_CODE_SUCCESS;
707,869✔
1028
  SVnode      *pVnodeObj = pVnode;
707,869✔
1029
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnodeObj, suid, 1);
707,869✔
1030
  if (NULL == pCur) {
708,981!
1031
    qError("vnode get all table list failed");
×
1032
    return terrno;
×
1033
  }
1034

1035
  while (1) {
2,667,742✔
1036
    tb_uid_t id = metaCtbCursorNext(pCur);
3,376,723✔
1037
    if (id == 0) {
3,376,769✔
1038
      break;
709,471✔
1039
    }
1040

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

1048
_exit:
709,471✔
1049
  metaCloseCtbCursor(pCur);
709,471✔
1050
  return code;
709,756✔
1051
}
1052

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

1060
  while (1) {
105,486✔
1061
    tb_uid_t id = metaStbCursorNext(pCur);
204,616✔
1062
    if (id == 0) {
204,616✔
1063
      break;
99,130✔
1064
    }
1065

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

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

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

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

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

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

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

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

1114
  *num = 0;
20,385✔
1115
  while (1) {
17,600✔
1116
    tb_uid_t id = metaCtbCursorNext(pCur);
37,985✔
1117
    if (id == 0) {
37,983✔
1118
      break;
20,383✔
1119
    }
1120

1121
    ++(*num);
17,600✔
1122
  }
1123

1124
  metaCloseCtbCursor(pCur);
20,383✔
1125
  return TSDB_CODE_SUCCESS;
20,387✔
1126
}
1127

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

1137
  return TSDB_CODE_SUCCESS;
20,387✔
1138
}
1139

1140
int32_t vnodeGetStbInfo(SVnode *pVnode, tb_uid_t suid, int64_t *keep, int8_t *flags) {
20,385✔
1141
  SMetaReader mr = {0};
20,385✔
1142
  metaReaderDoInit(&mr, pVnode->pMeta, META_READER_NOLOCK);
20,385✔
1143

1144
  int32_t code = metaReaderGetTableEntryByUid(&mr, suid);
20,386✔
1145
  if (code == TSDB_CODE_SUCCESS) {
20,388!
1146
    if (keep) *keep = mr.me.stbEntry.keep;
20,388✔
1147
    if (flags) *flags = mr.me.flags;
20,388✔
1148
  } else {
1149
    if (keep) *keep = 0;
×
1150
    if (flags) *flags = 0;
×
1151
  }
1152

1153
  metaReaderClear(&mr);
20,388✔
1154
  return TSDB_CODE_SUCCESS;
20,388✔
1155
}
1156

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

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

1189
  if (!(dbName = strchr(pVnode->config.dbname, '.'))) return 0;
99,188!
1190
  if (0 == strncmp(++dbName, "log", TSDB_DB_NAME_LEN)) {
99,188✔
1191
    tbNum = tkLogStbNum;
18✔
1192
    pTbArr = (const char **)&tkLogStb;
18✔
1193
  } else if (0 == strncmp(dbName, "audit", TSDB_DB_NAME_LEN)) {
99,170✔
1194
    tbNum = tkAuditStbNum;
60✔
1195
    pTbArr = (const char **)&tkAuditStb;
60✔
1196
  }
1197
  if (tbNum && pTbArr) {
99,188!
1198
    *tbSize = metaSizeOfTbFilterCache(pVnode->pMeta, 0);
78✔
1199
    if (*tbSize < tbNum) {
78✔
1200
      for (int32_t i = 0; i < tbNum; ++i) {
240✔
1201
        tb_uid_t suid = metaGetTableEntryUidByName(pVnode->pMeta, pTbArr[i]);
233✔
1202
        if (suid != 0) {
233✔
1203
          code = metaPutTbToFilterCache(pVnode->pMeta, &suid, 0);
26✔
1204
          if (TSDB_CODE_SUCCESS != code) {
26✔
1205
            return code;
14✔
1206
          }
1207
        }
1208
      }
1209
      *tbSize = metaSizeOfTbFilterCache(pVnode->pMeta, 0);
7✔
1210
    }
1211
  }
1212

1213
  return code;
99,174✔
1214
}
1215
#endif
1216

1217
static bool vnodeTimeSeriesFilter(void *arg1, void *arg2) {
69✔
1218
  SVnode *pVnode = (SVnode *)arg1;
69✔
1219

1220
  if (metaTbInFilterCache(pVnode->pMeta, arg2, 0)) {
69!
1221
    return true;
69✔
1222
  }
1223
  return false;
×
1224
}
1225

1226
int32_t vnodeGetTimeSeriesNum(SVnode *pVnode, int64_t *num) {
99,188✔
1227
  SArray *suidList = NULL;
99,188✔
1228

1229
  if (!(suidList = taosArrayInit(1, sizeof(tb_uid_t)))) {
99,188!
1230
    return terrno;
×
1231
  }
1232

1233
  int32_t tbFilterSize = 0;
99,188✔
1234
  int32_t code = TSDB_CODE_SUCCESS;
99,188✔
1235
#ifdef TD_ENTERPRISE
1236
  code = vnodeGetTimeSeriesBlackList(pVnode, &tbFilterSize);
99,188✔
1237
  if (TSDB_CODE_SUCCESS != code) {
99,188✔
1238
    goto _exit;
14✔
1239
  }
1240
#endif
1241

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

1249
  *num = 0;
99,174✔
1250
  int64_t arrSize = taosArrayGetSize(suidList);
99,174✔
1251
  for (int64_t i = 0; i < arrSize; ++i) {
204,650✔
1252
    tb_uid_t suid = *(tb_uid_t *)taosArrayGet(suidList, i);
105,476✔
1253

1254
    int64_t ctbNum = 0;
105,476✔
1255
    int32_t numOfCols = 0;
105,476✔
1256
    int8_t  flags = 0;
105,476✔
1257
    code = metaGetStbStats(pVnode, suid, &ctbNum, &numOfCols, &flags);
105,476✔
1258
    if (TSDB_CODE_SUCCESS != code) {
105,476!
1259
      goto _exit;
×
1260
    }
1261
    if (!TABLE_IS_VIRTUAL(flags)) {
105,476✔
1262
      *num += ctbNum * (numOfCols - 1);
105,466✔
1263
    }
1264
  }
1265

1266
_exit:
99,174✔
1267
  taosArrayDestroy(suidList);
99,188✔
1268
  return TSDB_CODE_SUCCESS;
99,188✔
1269
}
1270

1271
int32_t vnodeGetAllCtbNum(SVnode *pVnode, int64_t *num) {
×
1272
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, 0);
×
1273
  if (!pCur) {
×
1274
    return terrno;
×
1275
  }
1276

1277
  *num = 0;
×
1278
  while (1) {
×
1279
    tb_uid_t id = metaStbCursorNext(pCur);
×
1280
    if (id == 0) {
×
1281
      break;
×
1282
    }
1283

1284
    int64_t ctbNum = 0;
×
1285
    int32_t code = vnodeGetCtbNum(pVnode, id, &ctbNum);
×
1286
    if (TSDB_CODE_SUCCESS != code) {
×
1287
      metaCloseStbCursor(pCur);
×
1288
      return code;
×
1289
    }
1290

1291
    *num += ctbNum;
×
1292
  }
1293

1294
  metaCloseStbCursor(pCur);
×
1295
  return TSDB_CODE_SUCCESS;
×
1296
}
1297

1298
void *vnodeGetIdx(void *pVnode) {
8,010✔
1299
  if (pVnode == NULL) {
8,010!
1300
    return NULL;
×
1301
  }
1302

1303
  return metaGetIdx(((SVnode *)pVnode)->pMeta);
8,010✔
1304
}
1305

1306
void *vnodeGetIvtIdx(void *pVnode) {
8,006✔
1307
  if (pVnode == NULL) {
8,006!
1308
    return NULL;
×
1309
  }
1310
  return metaGetIvtIdx(((SVnode *)pVnode)->pMeta);
8,006✔
1311
}
1312

1313
int32_t vnodeGetTableSchema(void *pVnode, int64_t uid, STSchema **pSchema, int64_t *suid) {
129✔
1314
  return tsdbGetTableSchema(((SVnode *)pVnode)->pMeta, uid, pSchema, suid);
129✔
1315
}
1316

1317
static FORCE_INLINE int32_t vnodeGetDBPrimaryInfo(SVnode *pVnode, SDbSizeStatisInfo *pInfo) {
1318
  int32_t code = 0;
26✔
1319
  char    path[TSDB_FILENAME_LEN] = {0};
26✔
1320

1321
  char   *dirName[] = {VNODE_TSDB_DIR, VNODE_WAL_DIR, VNODE_META_DIR, VNODE_TSDB_CACHE_DIR};
26✔
1322
  int64_t dirSize[4];
1323

1324
  vnodeGetPrimaryDir(pVnode->path, pVnode->diskPrimary, pVnode->pTfs, path, TSDB_FILENAME_LEN);
26✔
1325
  int32_t offset = strlen(path);
26✔
1326

1327
  for (int i = 0; i < sizeof(dirName) / sizeof(dirName[0]); i++) {
130✔
1328
    int64_t size = {0};
104✔
1329
    (void)snprintf(path + offset, TSDB_FILENAME_LEN, "%s%s", TD_DIRSEP, dirName[i]);
104✔
1330
    code = taosGetDirSize(path, &size);
104✔
1331
    if (code != 0) {
104!
1332
      return code;
×
1333
    }
1334
    path[offset] = 0;
104✔
1335
    dirSize[i] = size;
104✔
1336
  }
1337

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

1354
  code = tsdbGetFsSize(pVnodeObj->pTsdb, pInfo);
26✔
1355
_exit:
26✔
1356
  return code;
26✔
1357
}
1358

1359
int32_t vnodeGetStreamProgress(SVnode *pVnode, SRpcMsg *pMsg, bool direct) {
3,324✔
1360
  int32_t            code = 0;
3,324✔
1361
  SStreamProgressReq req;
1362
  SStreamProgressRsp rsp = {0};
3,324✔
1363
  SRpcMsg            rpcMsg = {.info = pMsg->info, .code = 0};
3,324✔
1364
  char              *buf = NULL;
3,324✔
1365
  int32_t            rspLen = 0;
3,324✔
1366
  code = tDeserializeStreamProgressReq(pMsg->pCont, pMsg->contLen, &req);
3,324✔
1367

1368
  if (code == TSDB_CODE_SUCCESS) {
3,324!
1369
    rsp.fetchIdx = req.fetchIdx;
3,324✔
1370
    rsp.subFetchIdx = req.subFetchIdx;
3,324✔
1371
    rsp.vgId = req.vgId;
3,324✔
1372
    rsp.streamId = req.streamId;
3,324✔
1373
    rspLen = tSerializeStreamProgressRsp(0, 0, &rsp);
3,324✔
1374
    if (rspLen < 0) {
3,324!
1375
      code = terrno;
×
1376
      goto _OVER;
×
1377
    }
1378
    if (direct) {
3,324!
1379
      buf = rpcMallocCont(rspLen);
×
1380
    } else {
1381
      buf = taosMemoryCalloc(1, rspLen);
3,324!
1382
    }
1383
    if (!buf) {
3,323!
1384
      code = terrno;
×
1385
      goto _OVER;
×
1386
    }
1387
  }
1388

1389
  if (code == TSDB_CODE_SUCCESS) {
3,323!
1390
    code = tqGetStreamExecInfo(pVnode, req.streamId, &rsp.progressDelay, &rsp.fillHisFinished);
3,323✔
1391
  }
1392
  if (code == TSDB_CODE_SUCCESS) {
3,324!
1393
    rspLen = tSerializeStreamProgressRsp(buf, rspLen, &rsp);
3,324✔
1394
    if (rspLen < 0) {
3,324!
1395
      code = terrno;
×
1396
      goto _OVER;
×
1397
    }
1398
    rpcMsg.pCont = buf;
3,324✔
1399
    buf = NULL;
3,324✔
1400
    rpcMsg.contLen = rspLen;
3,324✔
1401
    rpcMsg.code = code;
3,324✔
1402
    rpcMsg.msgType = pMsg->msgType;
3,324✔
1403
    if (direct) {
3,324!
1404
      tmsgSendRsp(&rpcMsg);
×
1405
    } else {
1406
      *pMsg = rpcMsg;
3,324✔
1407
    }
1408
  }
1409

1410
_OVER:
×
1411
  if (buf) {
3,324!
1412
    taosMemoryFree(buf);
×
1413
  }
1414
  return code;
3,324✔
1415
}
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