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

taosdata / TDengine / #3663

19 Mar 2025 09:21AM UTC coverage: 61.664% (-0.6%) from 62.28%
#3663

push

travis-ci

web-flow
docs: add defination of tmq_config_res_t & fix spell error (#30271)

153169 of 318241 branches covered (48.13%)

Branch coverage included in aggregate %.

239405 of 318390 relevant lines covered (75.19%)

5762846.6 hits per line

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

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

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

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

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

53
void vnodePrintTableMeta(STableMetaRsp *pMeta) {
35,495✔
54
  if (!(qDebugFlag & DEBUG_DEBUG)) {
35,495✔
55
    return;
17,677✔
56
  }
57

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

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

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

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

138
  // query meta
139
  metaReaderDoInit(&mer1, pVnode->pMeta, META_READER_LOCK);
68,465✔
140
  if (reqTbUid) {
68,464✔
141
    SET_ERRNO(0);
529✔
142
    uint64_t tbUid = taosStr2UInt64(infoReq.tbName, NULL, 10);
529✔
143
    if (ERRNO == ERANGE || tbUid == 0) {
529!
144
      code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
145
      goto _exit3;
130✔
146
    }
147
    SMetaReader mr3 = {0};
529✔
148
    metaReaderDoInit(&mr3, ((SVnode *)pVnode)->pMeta, META_READER_NOLOCK);
529✔
149
    if ((code = metaReaderGetTableEntryByUid(&mr3, tbUid)) < 0) {
530✔
150
      metaReaderClear(&mr3);
130✔
151
      TAOS_CHECK_GOTO(code, NULL, _exit3);
130!
152
    }
153
    tstrncpy(metaRsp.tbName, mr3.me.name, TSDB_TABLE_NAME_LEN);
400✔
154
    metaReaderClear(&mr3);
400✔
155
    TAOS_CHECK_GOTO(metaGetTableEntryByName(&mer1, metaRsp.tbName), NULL, _exit3);
400!
156
  } else if (metaGetTableEntryByName(&mer1, infoReq.tbName) < 0) {
67,935✔
157
    code = terrno;
32,840✔
158
    goto _exit3;
32,840✔
159
  }
160

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

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

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

237
  vnodePrintTableMeta(&metaRsp);
35,495✔
238

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

246
  if (direct) {
35,495✔
247
    pRsp = rpcMallocCont(rspLen);
167✔
248
  } else {
249
    pRsp = taosMemoryCalloc(1, rspLen);
35,328!
250
  }
251

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

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

263
_exit:
35,496✔
264
  taosMemoryFree(metaRsp.pColRefs);
35,497!
265
  taosMemoryFree(metaRsp.pSchemas);
35,495!
266
  taosMemoryFree(metaRsp.pSchemaExt);
35,496!
267
_exit2:
35,495✔
268
  metaReaderClear(&mer2);
35,495✔
269
_exit3:
68,465✔
270
  metaReaderClear(&mer1);
68,465✔
271
_exit4:
68,466✔
272
  rpcMsg.info = pMsg->info;
68,466✔
273
  rpcMsg.pCont = pRsp;
68,466✔
274
  rpcMsg.contLen = rspLen;
68,466✔
275
  rpcMsg.code = code;
68,466✔
276
  rpcMsg.msgType = pMsg->msgType;
68,466✔
277

278
  if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST && autoCreateCtb == 1) {
68,466✔
279
    code = TSDB_CODE_SUCCESS;
24,385✔
280
  }
281

282
  if (code) {
68,466✔
283
    qError("get table %s meta with %" PRIu8 " failed cause of %s", infoReq.tbName, infoReq.option, tstrerror(code));
8,586!
284
  }
285

286
  if (direct) {
68,466✔
287
    tmsgSendRsp(&rpcMsg);
491✔
288
  } else {
289
    *pMsg = rpcMsg;
67,975✔
290
  }
291

292
  return code;
68,466✔
293
}
294

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

522
    switch (req->msgType) {
74,122!
523
      case TDMT_VND_TABLE_META:
67,446✔
524
        // error code has been set into reqMsg, no need to handle it here.
525
        if (TSDB_CODE_SUCCESS != vnodeGetTableMeta(pVnode, &reqMsg, false)) {
67,446✔
526
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
8,132!
527
        }
528
        break;
67,446✔
529
      case TDMT_VND_TABLE_NAME:
530✔
530
        // error code has been set into reqMsg, no need to handle it here.
531
        if (TSDB_CODE_SUCCESS != vnodeGetTableMeta(pVnode, &reqMsg, false)) {
530✔
532
          qWarn("vnodeGetBatchName failed, msgType:%d", req->msgType);
130!
533
        }
534
        break;
530✔
535
      case TDMT_VND_TABLE_CFG:
81✔
536
        // error code has been set into reqMsg, no need to handle it here.
537
        if (TSDB_CODE_SUCCESS != vnodeGetTableCfg(pVnode, &reqMsg, false)) {
81!
538
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
×
539
        }
540
        break;
81✔
541
      case TDMT_VND_GET_STREAM_PROGRESS:
6,065✔
542
        // error code has been set into reqMsg, no need to handle it here.
543
        if (TSDB_CODE_SUCCESS != vnodeGetStreamProgress(pVnode, &reqMsg, false)) {
6,065!
544
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
×
545
        }
546
        break;
6,066✔
547
      default:
×
548
        qError("invalid req msgType %d", req->msgType);
×
549
        reqMsg.code = TSDB_CODE_INVALID_MSG;
×
550
        reqMsg.pCont = NULL;
×
551
        reqMsg.contLen = 0;
×
552
        break;
×
553
    }
554

555
    rsp.msgIdx = req->msgIdx;
74,123✔
556
    rsp.reqType = reqMsg.msgType;
74,123✔
557
    rsp.msgLen = reqMsg.contLen;
74,123✔
558
    rsp.rspCode = reqMsg.code;
74,123✔
559
    rsp.msg = reqMsg.pCont;
74,123✔
560

561
    if (NULL == taosArrayPush(batchRsp.pRsps, &rsp)) {
148,244!
562
      qError("taosArrayPush failed");
×
563
      code = terrno;
×
564
      goto _exit;
×
565
    }
566
  }
567

568
  rspSize = tSerializeSBatchRsp(NULL, 0, &batchRsp);
70,540✔
569
  if (rspSize < 0) {
70,542!
570
    qError("tSerializeSBatchRsp failed");
×
571
    code = terrno;
×
572
    goto _exit;
×
573
  }
574
  pRsp = rpcMallocCont(rspSize);
70,542✔
575
  if (pRsp == NULL) {
70,542!
576
    qError("rpcMallocCont %d failed", rspSize);
×
577
    code = terrno;
×
578
    goto _exit;
×
579
  }
580
  if (tSerializeSBatchRsp(pRsp, rspSize, &batchRsp) < 0) {
70,542!
581
    qError("tSerializeSBatchRsp %d failed", rspSize);
×
582
    code = terrno;
×
583
    goto _exit;
×
584
  }
585

586
_exit:
70,542✔
587

588
  rspMsg.info = pMsg->info;
70,542✔
589
  rspMsg.pCont = pRsp;
70,542✔
590
  rspMsg.contLen = rspSize;
70,542✔
591
  rspMsg.code = code;
70,542✔
592
  rspMsg.msgType = pMsg->msgType;
70,542✔
593

594
  if (code) {
70,542!
595
    qError("vnd get batch meta failed cause of %s", tstrerror(code));
×
596
  }
597

598
  taosArrayDestroyEx(batchReq.pMsgs, tFreeSBatchReqMsg);
70,542✔
599
  taosArrayDestroyEx(batchRsp.pRsps, tFreeSBatchRspMsg);
70,541✔
600

601
  tmsgSendRsp(&rspMsg);
70,541✔
602

603
  return code;
70,542✔
604
}
605

606
#define VNODE_DO_META_QUERY(pVnode, cmd)                 \
607
  do {                                                   \
608
    (void)taosThreadRwlockRdlock(&(pVnode)->metaRWLock); \
609
    cmd;                                                 \
610
    (void)taosThreadRwlockUnlock(&(pVnode)->metaRWLock); \
611
  } while (0)
612

613
int32_t vnodeReadVSubtables(SReadHandle* pHandle, int64_t suid, SArray** ppRes) {
×
614
  int32_t                    code = TSDB_CODE_SUCCESS;
×
615
  int32_t                    line = 0;
×
616
  SMetaReader                mr = {0};
×
617
  bool                       readerInit = false;
×
618
  SVCTableRefCols*           pTb = NULL;
×
619
  int32_t                    refColsNum = 0;
×
620
  char                       tbFName[TSDB_TABLE_FNAME_LEN];
621

622
  SArray *pList = taosArrayInit(10, sizeof(uint64_t));
×
623
  QUERY_CHECK_NULL(pList, code, line, _return, terrno);
×
624

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

627
  size_t num = taosArrayGetSize(pList);
×
628
  *ppRes = taosArrayInit(num, POINTER_BYTES);
×
629
  QUERY_CHECK_NULL(*ppRes, code, line, _return, terrno);
×
630
  SSHashObj* pSrcTbls = tSimpleHashInit(10, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
×
631
  QUERY_CHECK_NULL(pSrcTbls, code, line, _return, terrno);
×
632

633
  for (int32_t i = 0; i < num; ++i) {
×
634
    uint64_t* id = taosArrayGet(pList, i);
×
635
    QUERY_CHECK_NULL(id, code, line, _return, terrno);
×
636
    pHandle->api.metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pHandle->api.metaFn);
×
637
    QUERY_CHECK_CODE(pHandle->api.metaReaderFn.getTableEntryByUid(&mr, *id), line, _return);
×
638
    readerInit = true;
×
639

640
    refColsNum = 0;
×
641
    for (int32_t j = 0; j < mr.me.colRef.nCols; j++) {
×
642
      if (mr.me.colRef.pColRef[j].hasRef) {
×
643
        refColsNum++;
×
644
      }
645
    }
646

647
    if (refColsNum <= 0) {
×
648
      pHandle->api.metaReaderFn.clearReader(&mr);
×
649
      readerInit = false;
×
650
      continue;
×
651
    }
652

653
    pTb = taosMemoryCalloc(1, refColsNum * sizeof(SRefColInfo) + sizeof(*pTb));
×
654
    QUERY_CHECK_NULL(pTb, code, line, _return, terrno);
×
655

656
    pTb->uid = mr.me.uid;
×
657
    pTb->numOfColRefs = refColsNum;
×
658
    pTb->refCols = (SRefColInfo*)(pTb + 1);
×
659

660
    refColsNum = 0;
×
661
    tSimpleHashClear(pSrcTbls);
×
662
    for (int32_t j = 0; j < mr.me.colRef.nCols; j++) {
×
663
      if (!mr.me.colRef.pColRef[j].hasRef) {
×
664
        continue;
×
665
      }
666

667
      pTb->refCols[refColsNum].colId = mr.me.colRef.pColRef[j].id;
×
668
      tstrncpy(pTb->refCols[refColsNum].refColName, mr.me.colRef.pColRef[j].refColName, TSDB_COL_NAME_LEN);
×
669
      tstrncpy(pTb->refCols[refColsNum].refTableName, mr.me.colRef.pColRef[j].refTableName, TSDB_TABLE_NAME_LEN);
×
670
      tstrncpy(pTb->refCols[refColsNum].refDbName, mr.me.colRef.pColRef[j].refDbName, TSDB_DB_NAME_LEN);
×
671

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

674
      if (NULL == tSimpleHashGet(pSrcTbls, tbFName, strlen(tbFName))) {
×
675
        QUERY_CHECK_CODE(tSimpleHashPut(pSrcTbls, tbFName, strlen(tbFName), &code, sizeof(code)), line, _return);
×
676
      }
677

678
      refColsNum++;
×
679
    }
680

681
    pTb->numOfSrcTbls = tSimpleHashGetSize(pSrcTbls);
×
682
    QUERY_CHECK_NULL(taosArrayPush(*ppRes, &pTb), code, line, _return, terrno);
×
683
    pTb = NULL;
×
684

685
    pHandle->api.metaReaderFn.clearReader(&mr);
×
686
    readerInit = false;
×
687
  }
688

689
_return:
×
690

691
  if (readerInit) {
×
692
    pHandle->api.metaReaderFn.clearReader(&mr);
×
693
  }
694

695
  taosArrayDestroy(pList);
×
696
  taosMemoryFree(pTb);
×
697
  tSimpleHashCleanup(pSrcTbls);
×
698

699
  if (code) {
×
700
    qError("%s failed since %s", __func__, tstrerror(code));
×
701
  }
702
  return code;
×
703
}
704

705

706
int32_t vnodeGetVSubtablesMeta(SVnode *pVnode, SRpcMsg *pMsg) {
×
707
  int32_t        code = 0;
×
708
  int32_t        rspSize = 0;
×
709
  SVSubTablesReq req = {0};
×
710
  SVSubTablesRsp rsp = {0};
×
711
  SRpcMsg      rspMsg = {0};
×
712
  void        *pRsp = NULL;
×
713
  int32_t      line = 0;
×
714

715
  if (tDeserializeSVSubTablesReq(pMsg->pCont, pMsg->contLen, &req)) {
×
716
    code = terrno;
×
717
    qError("tDeserializeSVSubTablesReq failed");
×
718
    goto _return;
×
719
  }
720

721
  SReadHandle handle = {.vnode = pVnode};
×
722
  initStorageAPI(&handle.api);
×
723

724
  QUERY_CHECK_CODE(vnodeReadVSubtables(&handle, req.suid, &rsp.pTables), line, _return);
×
725
  rsp.vgId = TD_VID(pVnode);
×
726

727
  rspSize = tSerializeSVSubTablesRsp(NULL, 0, &rsp);
×
728
  if (rspSize < 0) {
×
729
    code = rspSize;
×
730
    qError("tSerializeSVSubTablesRsp failed, error:%d", rspSize);
×
731
    goto _return;
×
732
  }
733
  pRsp = rpcMallocCont(rspSize);
×
734
  if (pRsp == NULL) {
×
735
    code = terrno;
×
736
    qError("rpcMallocCont %d failed, error:%d", rspSize, terrno);
×
737
    goto _return;
×
738
  }
739
  rspSize = tSerializeSVSubTablesRsp(pRsp, rspSize, &rsp);
×
740
  if (rspSize < 0) {
×
741
    code = rspSize;
×
742
    qError("tSerializeSVSubTablesRsp failed, error:%d", rspSize);
×
743
    goto _return;
×
744
  }
745

746
_return:
×
747

748
  rspMsg.info = pMsg->info;
×
749
  rspMsg.pCont = pRsp;
×
750
  rspMsg.contLen = rspSize;
×
751
  rspMsg.code = code;
×
752
  rspMsg.msgType = pMsg->msgType;
×
753

754
  if (code) {
×
755
    qError("vnd get virtual subtables failed cause of %s", tstrerror(code));
×
756
  }
757

758
  tDestroySVSubTablesRsp(&rsp);
×
759

760
  tmsgSendRsp(&rspMsg);
×
761

762
  return code;
×
763
}
764

765

766
int32_t vnodeGetLoad(SVnode *pVnode, SVnodeLoad *pLoad) {
200,220✔
767
  SSyncState state = syncGetState(pVnode->sync);
200,220✔
768
  pLoad->syncAppliedIndex = pVnode->state.applied;
200,220✔
769
  syncGetCommitIndex(pVnode->sync, &pLoad->syncCommitIndex);
200,220✔
770

771
  pLoad->vgId = TD_VID(pVnode);
200,220✔
772
  pLoad->syncState = state.state;
200,220✔
773
  pLoad->syncRestore = state.restored;
200,220✔
774
  pLoad->syncTerm = state.term;
200,220✔
775
  pLoad->roleTimeMs = state.roleTimeMs;
200,220✔
776
  pLoad->startTimeMs = state.startTimeMs;
200,220✔
777
  pLoad->syncCanRead = state.canRead;
200,220✔
778
  pLoad->learnerProgress = state.progress;
200,220✔
779
  pLoad->cacheUsage = tsdbCacheGetUsage(pVnode);
200,220✔
780
  pLoad->numOfCachedTables = tsdbCacheGetElems(pVnode);
200,220✔
781
  VNODE_DO_META_QUERY(pVnode, pLoad->numOfTables = metaGetTbNum(pVnode->pMeta));
200,220✔
782
  VNODE_DO_META_QUERY(pVnode, pLoad->numOfTimeSeries = metaGetTimeSeriesNum(pVnode->pMeta, 1));
200,220✔
783
  pLoad->totalStorage = (int64_t)3 * 1073741824;
200,220✔
784
  pLoad->compStorage = (int64_t)2 * 1073741824;
200,220✔
785
  pLoad->pointsWritten = 100;
200,220✔
786
  pLoad->numOfSelectReqs = 1;
200,220✔
787
  pLoad->numOfInsertReqs = atomic_load_64(&pVnode->statis.nInsert);
200,220✔
788
  pLoad->numOfInsertSuccessReqs = atomic_load_64(&pVnode->statis.nInsertSuccess);
200,220✔
789
  pLoad->numOfBatchInsertReqs = atomic_load_64(&pVnode->statis.nBatchInsert);
200,220✔
790
  pLoad->numOfBatchInsertSuccessReqs = atomic_load_64(&pVnode->statis.nBatchInsertSuccess);
200,220✔
791
  return 0;
200,220✔
792
}
793

794
int32_t vnodeGetLoadLite(SVnode *pVnode, SVnodeLoadLite *pLoad) {
×
795
  SSyncState syncState = syncGetState(pVnode->sync);
×
796
  if (syncState.state == TAOS_SYNC_STATE_LEADER || syncState.state == TAOS_SYNC_STATE_ASSIGNED_LEADER) {
×
797
    pLoad->vgId = TD_VID(pVnode);
×
798
    pLoad->nTimeSeries = metaGetTimeSeriesNum(pVnode->pMeta, 1);
×
799
    return 0;
×
800
  }
801
  return -1;
×
802
}
803
/**
804
 * @brief Reset the statistics value by monitor interval
805
 *
806
 * @param pVnode
807
 * @param pLoad
808
 */
809
void vnodeResetLoad(SVnode *pVnode, SVnodeLoad *pLoad) {
32✔
810
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nInsert, pLoad->numOfInsertReqs, 64, "nInsert");
32!
811
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nInsertSuccess, pLoad->numOfInsertSuccessReqs, 64, "nInsertSuccess");
32!
812
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nBatchInsert, pLoad->numOfBatchInsertReqs, 64, "nBatchInsert");
32!
813
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nBatchInsertSuccess, pLoad->numOfBatchInsertSuccessReqs, 64,
32!
814
                            "nBatchInsertSuccess");
815
}
32✔
816

817
void vnodeGetInfo(void *pVnode, const char **dbname, int32_t *vgId, int64_t *numOfTables, int64_t *numOfNormalTables) {
34,664✔
818
  SVnode    *pVnodeObj = pVnode;
34,664✔
819
  SVnodeCfg *pConf = &pVnodeObj->config;
34,664✔
820

821
  if (dbname) {
34,664!
822
    *dbname = pConf->dbname;
34,703✔
823
  }
824

825
  if (vgId) {
34,664✔
826
    *vgId = TD_VID(pVnodeObj);
33,051✔
827
  }
828

829
  if (numOfTables) {
34,664!
830
    *numOfTables = pConf->vndStats.numOfNTables + pConf->vndStats.numOfCTables;
×
831
  }
832

833
  if (numOfNormalTables) {
34,664!
834
    *numOfNormalTables = pConf->vndStats.numOfNTables;
×
835
  }
836
}
34,664✔
837

838
int32_t vnodeGetTableList(void *pVnode, int8_t type, SArray *pList) {
×
839
  if (type == TSDB_SUPER_TABLE) {
×
840
    return vnodeGetStbIdList(pVnode, 0, pList);
×
841
  } else {
842
    return TSDB_CODE_INVALID_PARA;
×
843
  }
844
}
845

846
int32_t vnodeGetAllTableList(SVnode *pVnode, uint64_t uid, SArray *list) {
×
847
  int32_t      code = TSDB_CODE_SUCCESS;
×
848
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, uid, 1);
×
849
  if (NULL == pCur) {
×
850
    qError("vnode get all table list failed");
×
851
    return terrno;
×
852
  }
853

854
  while (1) {
×
855
    tb_uid_t id = metaCtbCursorNext(pCur);
×
856
    if (id == 0) {
×
857
      break;
×
858
    }
859

860
    STableKeyInfo info = {uid = id};
×
861
    if (NULL == taosArrayPush(list, &info)) {
×
862
      qError("taosArrayPush failed");
×
863
      code = terrno;
×
864
      goto _exit;
×
865
    }
866
  }
867
_exit:
×
868
  metaCloseCtbCursor(pCur);
×
869
  return code;
×
870
}
871

872
int32_t vnodeGetCtbIdListByFilter(SVnode *pVnode, int64_t suid, SArray *list, bool (*filter)(void *arg), void *arg) {
×
873
  return 0;
×
874
}
875

876
int32_t vnodeGetCtbIdList(void *pVnode, int64_t suid, SArray *list) {
607,572✔
877
  int32_t      code = TSDB_CODE_SUCCESS;
607,572✔
878
  SVnode      *pVnodeObj = pVnode;
607,572✔
879
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnodeObj, suid, 1);
607,572✔
880
  if (NULL == pCur) {
608,695!
881
    qError("vnode get all table list failed");
×
882
    return terrno;
×
883
  }
884

885
  while (1) {
2,435,687✔
886
    tb_uid_t id = metaCtbCursorNext(pCur);
3,044,382✔
887
    if (id == 0) {
3,043,898✔
888
      break;
609,203✔
889
    }
890

891
    if (NULL == taosArrayPush(list, &id)) {
2,435,687!
892
      qError("taosArrayPush failed");
×
893
      code = terrno;
×
894
      goto _exit;
×
895
    }
896
  }
897

898
_exit:
609,203✔
899
  metaCloseCtbCursor(pCur);
609,203✔
900
  return code;
609,575✔
901
}
902

903
int32_t vnodeGetStbIdList(SVnode *pVnode, int64_t suid, SArray *list) {
95,945✔
904
  int32_t      code = TSDB_CODE_SUCCESS;
95,945✔
905
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, suid);
95,945✔
906
  if (!pCur) {
95,944!
907
    return TSDB_CODE_OUT_OF_MEMORY;
×
908
  }
909

910
  while (1) {
95,442✔
911
    tb_uid_t id = metaStbCursorNext(pCur);
191,386✔
912
    if (id == 0) {
191,385✔
913
      break;
95,943✔
914
    }
915

916
    if (NULL == taosArrayPush(list, &id)) {
95,442!
917
      qError("taosArrayPush failed");
×
918
      code = terrno;
×
919
      goto _exit;
×
920
    }
921
  }
922

923
_exit:
95,943✔
924
  metaCloseStbCursor(pCur);
95,943✔
925
  return code;
95,945✔
926
}
927

928
int32_t vnodeGetStbIdListByFilter(SVnode *pVnode, int64_t suid, SArray *list, bool (*filter)(void *arg, void *arg1),
×
929
                                  void *arg) {
930
  int32_t      code = TSDB_CODE_SUCCESS;
×
931
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, suid);
×
932
  if (!pCur) {
×
933
    return terrno;
×
934
  }
935

936
  while (1) {
×
937
    tb_uid_t id = metaStbCursorNext(pCur);
×
938
    if (id == 0) {
×
939
      break;
×
940
    }
941

942
    if ((*filter) && (*filter)(arg, &id)) {
×
943
      continue;
×
944
    }
945

946
    if (NULL == taosArrayPush(list, &id)) {
×
947
      qError("taosArrayPush failed");
×
948
      code = terrno;
×
949
      goto _exit;
×
950
    }
951
  }
952

953
_exit:
×
954
  metaCloseStbCursor(pCur);
×
955
  return code;
×
956
}
957

958
int32_t vnodeGetCtbNum(SVnode *pVnode, int64_t suid, int64_t *num) {
20,039✔
959
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, suid, 0);
20,039✔
960
  if (!pCur) {
20,045!
961
    return terrno;
×
962
  }
963

964
  *num = 0;
20,048✔
965
  while (1) {
18,735✔
966
    tb_uid_t id = metaCtbCursorNext(pCur);
38,783✔
967
    if (id == 0) {
38,780✔
968
      break;
20,045✔
969
    }
970

971
    ++(*num);
18,735✔
972
  }
973

974
  metaCloseCtbCursor(pCur);
20,045✔
975
  return TSDB_CODE_SUCCESS;
20,048✔
976
}
977

978
int32_t vnodeGetStbColumnNum(SVnode *pVnode, tb_uid_t suid, int *num) {
20,040✔
979
  SSchemaWrapper *pSW = metaGetTableSchema(pVnode->pMeta, suid, -1, 0, NULL);
20,040✔
980
  if (pSW) {
20,048!
981
    *num = pSW->nCols;
20,048!
982
    tDeleteSchemaWrapper(pSW);
983
  } else {
984
    *num = 2;
×
985
  }
986

987
  return TSDB_CODE_SUCCESS;
20,047✔
988
}
989

990
int32_t vnodeGetStbKeep(SVnode *pVnode, tb_uid_t suid, int64_t *keep) {
20,044✔
991
  SMetaReader mr = {0};
20,044✔
992
  metaReaderDoInit(&mr, pVnode->pMeta, META_READER_NOLOCK);
20,044✔
993

994
  int32_t code = metaReaderGetTableEntryByUid(&mr, suid);
20,046✔
995
  if (code == TSDB_CODE_SUCCESS) {
20,046!
996
    *keep = mr.me.stbEntry.keep;
20,046✔
997
  } else {
998
    *keep = 0;  // Default value if not found
×
999
  }
1000

1001
  metaReaderClear(&mr);
20,046✔
1002
  return TSDB_CODE_SUCCESS;
20,050✔
1003
}
1004

1005
#ifdef TD_ENTERPRISE
1006
const char *tkLogStb[] = {"cluster_info",
1007
                          "data_dir",
1008
                          "dnodes_info",
1009
                          "d_info",
1010
                          "grants_info",
1011
                          "keeper_monitor",
1012
                          "logs",
1013
                          "log_dir",
1014
                          "log_summary",
1015
                          "m_info",
1016
                          "taosadapter_restful_http_request_fail",
1017
                          "taosadapter_restful_http_request_in_flight",
1018
                          "taosadapter_restful_http_request_summary_milliseconds",
1019
                          "taosadapter_restful_http_request_total",
1020
                          "taosadapter_system_cpu_percent",
1021
                          "taosadapter_system_mem_percent",
1022
                          "temp_dir",
1023
                          "vgroups_info",
1024
                          "vnodes_role"};
1025
const char *tkAuditStb[] = {"operations"};
1026
const int   tkLogStbNum = ARRAY_SIZE(tkLogStb);
1027
const int   tkAuditStbNum = ARRAY_SIZE(tkAuditStb);
1028

1029
// exclude stbs of taoskeeper log
1030
static int32_t vnodeGetTimeSeriesBlackList(SVnode *pVnode, int32_t *tbSize) {
95,928✔
1031
  int32_t      code = TSDB_CODE_SUCCESS;
95,928✔
1032
  int32_t      tbNum = 0;
95,928✔
1033
  const char **pTbArr = NULL;
95,928✔
1034
  const char  *dbName = NULL;
95,928✔
1035
  *tbSize = 0;
95,928✔
1036

1037
  if (!(dbName = strchr(pVnode->config.dbname, '.'))) return 0;
95,928!
1038
  if (0 == strncmp(++dbName, "log", TSDB_DB_NAME_LEN)) {
95,928!
1039
    tbNum = tkLogStbNum;
×
1040
    pTbArr = (const char **)&tkLogStb;
×
1041
  } else if (0 == strncmp(dbName, "audit", TSDB_DB_NAME_LEN)) {
95,928!
1042
    tbNum = tkAuditStbNum;
×
1043
    pTbArr = (const char **)&tkAuditStb;
×
1044
  }
1045
  if (tbNum && pTbArr) {
95,928!
1046
    *tbSize = metaSizeOfTbFilterCache(pVnode->pMeta, 0);
×
1047
    if (*tbSize < tbNum) {
×
1048
      for (int32_t i = 0; i < tbNum; ++i) {
×
1049
        tb_uid_t suid = metaGetTableEntryUidByName(pVnode->pMeta, pTbArr[i]);
×
1050
        if (suid != 0) {
×
1051
          code = metaPutTbToFilterCache(pVnode->pMeta, &suid, 0);
×
1052
          if (TSDB_CODE_SUCCESS != code) {
×
1053
            return code;
×
1054
          }
1055
        }
1056
      }
1057
      *tbSize = metaSizeOfTbFilterCache(pVnode->pMeta, 0);
×
1058
    }
1059
  }
1060

1061
  return code;
95,928✔
1062
}
1063
#endif
1064

1065
static bool vnodeTimeSeriesFilter(void *arg1, void *arg2) {
×
1066
  SVnode *pVnode = (SVnode *)arg1;
×
1067

1068
  if (metaTbInFilterCache(pVnode->pMeta, arg2, 0)) {
×
1069
    return true;
×
1070
  }
1071
  return false;
×
1072
}
1073

1074
int32_t vnodeGetTimeSeriesNum(SVnode *pVnode, int64_t *num) {
95,927✔
1075
  SArray *suidList = NULL;
95,927✔
1076

1077
  if (!(suidList = taosArrayInit(1, sizeof(tb_uid_t)))) {
95,927!
1078
    return terrno;
×
1079
  }
1080

1081
  int32_t tbFilterSize = 0;
95,928✔
1082
  int32_t code = TSDB_CODE_SUCCESS;
95,928✔
1083
#ifdef TD_ENTERPRISE
1084
  code = vnodeGetTimeSeriesBlackList(pVnode, &tbFilterSize);
95,928✔
1085
  if (TSDB_CODE_SUCCESS != code) {
95,927!
1086
    goto _exit;
×
1087
  }
1088
#endif
1089

1090
  if ((!tbFilterSize && vnodeGetStbIdList(pVnode, 0, suidList) < 0) ||
95,927!
1091
      (tbFilterSize && vnodeGetStbIdListByFilter(pVnode, 0, suidList, vnodeTimeSeriesFilter, pVnode) < 0)) {
95,928!
1092
    qError("vgId:%d, failed to get stb id list error: %s", TD_VID(pVnode), terrstr());
×
1093
    taosArrayDestroy(suidList);
×
1094
    return terrno;
×
1095
  }
1096

1097
  *num = 0;
95,928✔
1098
  int64_t arrSize = taosArrayGetSize(suidList);
95,928✔
1099
  for (int64_t i = 0; i < arrSize; ++i) {
191,360✔
1100
    tb_uid_t suid = *(tb_uid_t *)taosArrayGet(suidList, i);
95,432✔
1101

1102
    int64_t ctbNum = 0;
95,432✔
1103
    int32_t numOfCols = 0;
95,432✔
1104
    code = metaGetStbStats(pVnode, suid, &ctbNum, &numOfCols);
95,432✔
1105
    if (TSDB_CODE_SUCCESS != code) {
95,432!
1106
      goto _exit;
×
1107
    }
1108
    *num += ctbNum * (numOfCols - 1);
95,432✔
1109
  }
1110

1111
_exit:
95,928✔
1112
  taosArrayDestroy(suidList);
95,928✔
1113
  return TSDB_CODE_SUCCESS;
95,927✔
1114
}
1115

1116
int32_t vnodeGetAllCtbNum(SVnode *pVnode, int64_t *num) {
×
1117
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, 0);
×
1118
  if (!pCur) {
×
1119
    return terrno;
×
1120
  }
1121

1122
  *num = 0;
×
1123
  while (1) {
×
1124
    tb_uid_t id = metaStbCursorNext(pCur);
×
1125
    if (id == 0) {
×
1126
      break;
×
1127
    }
1128

1129
    int64_t ctbNum = 0;
×
1130
    int32_t code = vnodeGetCtbNum(pVnode, id, &ctbNum);
×
1131
    if (TSDB_CODE_SUCCESS != code) {
×
1132
      metaCloseStbCursor(pCur);
×
1133
      return code;
×
1134
    }
1135

1136
    *num += ctbNum;
×
1137
  }
1138

1139
  metaCloseStbCursor(pCur);
×
1140
  return TSDB_CODE_SUCCESS;
×
1141
}
1142

1143
void *vnodeGetIdx(void *pVnode) {
8,075✔
1144
  if (pVnode == NULL) {
8,075!
1145
    return NULL;
×
1146
  }
1147

1148
  return metaGetIdx(((SVnode *)pVnode)->pMeta);
8,075✔
1149
}
1150

1151
void *vnodeGetIvtIdx(void *pVnode) {
8,068✔
1152
  if (pVnode == NULL) {
8,068!
1153
    return NULL;
×
1154
  }
1155
  return metaGetIvtIdx(((SVnode *)pVnode)->pMeta);
8,068✔
1156
}
1157

1158
int32_t vnodeGetTableSchema(void *pVnode, int64_t uid, STSchema **pSchema, int64_t *suid) {
123✔
1159
  return tsdbGetTableSchema(((SVnode *)pVnode)->pMeta, uid, pSchema, suid);
123✔
1160
}
1161

1162
static FORCE_INLINE int32_t vnodeGetDBPrimaryInfo(SVnode *pVnode, SDbSizeStatisInfo *pInfo) {
1163
  int32_t code = 0;
22✔
1164
  char    path[TSDB_FILENAME_LEN] = {0};
22✔
1165

1166
  char   *dirName[] = {VNODE_TSDB_DIR, VNODE_WAL_DIR, VNODE_META_DIR, VNODE_TSDB_CACHE_DIR};
22✔
1167
  int64_t dirSize[4];
1168

1169
  vnodeGetPrimaryDir(pVnode->path, pVnode->diskPrimary, pVnode->pTfs, path, TSDB_FILENAME_LEN);
22✔
1170
  int32_t offset = strlen(path);
22✔
1171

1172
  for (int i = 0; i < sizeof(dirName) / sizeof(dirName[0]); i++) {
110✔
1173
    int64_t size = {0};
88✔
1174
    (void)snprintf(path + offset, TSDB_FILENAME_LEN, "%s%s", TD_DIRSEP, dirName[i]);
88✔
1175
    code = taosGetDirSize(path, &size);
88✔
1176
    if (code != 0) {
88!
1177
      return code;
×
1178
    }
1179
    path[offset] = 0;
88✔
1180
    dirSize[i] = size;
88✔
1181
  }
1182

1183
  pInfo->l1Size = 0;
22✔
1184
  pInfo->walSize = dirSize[1];
22✔
1185
  pInfo->metaSize = dirSize[2];
22✔
1186
  pInfo->cacheSize = dirSize[3];
22✔
1187
  return code;
22✔
1188
}
1189
int32_t vnodeGetDBSize(void *pVnode, SDbSizeStatisInfo *pInfo) {
22✔
1190
  int32_t code = 0;
22✔
1191
  int32_t lino = 0;
22✔
1192
  SVnode *pVnodeObj = pVnode;
22✔
1193
  if (pVnodeObj == NULL) {
22!
1194
    return TSDB_CODE_VND_NOT_EXIST;
×
1195
  }
1196
  code = vnodeGetDBPrimaryInfo(pVnode, pInfo);
22✔
1197
  if (code != 0) goto _exit;
22!
1198

1199
  code = tsdbGetFsSize(pVnodeObj->pTsdb, pInfo);
22✔
1200
_exit:
22✔
1201
  return code;
22✔
1202
}
1203

1204
int32_t vnodeGetStreamProgress(SVnode *pVnode, SRpcMsg *pMsg, bool direct) {
6,065✔
1205
  int32_t            code = 0;
6,065✔
1206
  SStreamProgressReq req;
1207
  SStreamProgressRsp rsp = {0};
6,065✔
1208
  SRpcMsg            rpcMsg = {.info = pMsg->info, .code = 0};
6,065✔
1209
  char              *buf = NULL;
6,065✔
1210
  int32_t            rspLen = 0;
6,065✔
1211
  code = tDeserializeStreamProgressReq(pMsg->pCont, pMsg->contLen, &req);
6,065✔
1212

1213
  if (code == TSDB_CODE_SUCCESS) {
6,065!
1214
    rsp.fetchIdx = req.fetchIdx;
6,065✔
1215
    rsp.subFetchIdx = req.subFetchIdx;
6,065✔
1216
    rsp.vgId = req.vgId;
6,065✔
1217
    rsp.streamId = req.streamId;
6,065✔
1218
    rspLen = tSerializeStreamProgressRsp(0, 0, &rsp);
6,065✔
1219
    if (rspLen < 0) {
6,065!
1220
      code = terrno;
×
1221
      goto _OVER;
×
1222
    }
1223
    if (direct) {
6,065!
1224
      buf = rpcMallocCont(rspLen);
×
1225
    } else {
1226
      buf = taosMemoryCalloc(1, rspLen);
6,065!
1227
    }
1228
    if (!buf) {
6,066!
1229
      code = terrno;
×
1230
      goto _OVER;
×
1231
    }
1232
  }
1233

1234
  if (code == TSDB_CODE_SUCCESS) {
6,066!
1235
    code = tqGetStreamExecInfo(pVnode, req.streamId, &rsp.progressDelay, &rsp.fillHisFinished);
6,066✔
1236
  }
1237
  if (code == TSDB_CODE_SUCCESS) {
6,066!
1238
    rspLen = tSerializeStreamProgressRsp(buf, rspLen, &rsp);
6,066✔
1239
    if (rspLen < 0) {
6,066!
1240
      code = terrno;
×
1241
      goto _OVER;
×
1242
    }
1243
    rpcMsg.pCont = buf;
6,066✔
1244
    buf = NULL;
6,066✔
1245
    rpcMsg.contLen = rspLen;
6,066✔
1246
    rpcMsg.code = code;
6,066✔
1247
    rpcMsg.msgType = pMsg->msgType;
6,066✔
1248
    if (direct) {
6,066!
1249
      tmsgSendRsp(&rpcMsg);
×
1250
    } else {
1251
      *pMsg = rpcMsg;
6,066✔
1252
    }
1253
  }
1254

1255
_OVER:
×
1256
  if (buf) {
6,066!
1257
    taosMemoryFree(buf);
×
1258
  }
1259
  return code;
6,066✔
1260
}
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