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

taosdata / TDengine / #3796

31 Mar 2025 10:39AM UTC coverage: 30.372% (-7.1%) from 37.443%
#3796

push

travis-ci

happyguoxy
test:add test cases

69287 of 309062 branches covered (22.42%)

Branch coverage included in aggregate %.

118044 of 307720 relevant lines covered (38.36%)

278592.15 hits per line

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

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

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

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

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

53
void vnodePrintTableMeta(STableMetaRsp *pMeta) {
×
54
  if (!(qDebugFlag & DEBUG_DEBUG)) {
×
55
    return;
×
56
  }
57

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

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

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

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

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

161
  metaRsp.tableType = mer1.me.type;
×
162
  metaRsp.vgId = TD_VID(pVnode);
×
163
  metaRsp.tuid = mer1.me.uid;
×
164

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

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

237
  vnodePrintTableMeta(&metaRsp);
×
238

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

246
  if (direct) {
×
247
    pRsp = rpcMallocCont(rspLen);
×
248
  } else {
249
    pRsp = taosMemoryCalloc(1, rspLen);
×
250
  }
251

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

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

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

278
  if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST && autoCreateCtb == 1) {
×
279
    code = TSDB_CODE_SUCCESS;
×
280
  }
281

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

286
  if (direct) {
×
287
    tmsgSendRsp(&rpcMsg);
×
288
  } else {
289
    *pMsg = rpcMsg;
×
290
  }
291

292
  return code;
×
293
}
294

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

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

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

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

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

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

331
  cfgRsp.tableType = mer1.me.type;
×
332

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

518
    reqMsg.msgType = req->msgType;
×
519
    reqMsg.pCont = req->msg;
×
520
    reqMsg.contLen = req->msgLen;
×
521

522
    switch (req->msgType) {
×
523
      case TDMT_VND_TABLE_META:
×
524
        // error code has been set into reqMsg, no need to handle it here.
525
        if (TSDB_CODE_SUCCESS != vnodeGetTableMeta(pVnode, &reqMsg, false)) {
×
526
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
×
527
        }
528
        break;
×
529
      case TDMT_VND_TABLE_NAME:
×
530
        // error code has been set into reqMsg, no need to handle it here.
531
        if (TSDB_CODE_SUCCESS != vnodeGetTableMeta(pVnode, &reqMsg, false)) {
×
532
          qWarn("vnodeGetBatchName failed, msgType:%d", req->msgType);
×
533
        }
534
        break;
×
535
      case TDMT_VND_TABLE_CFG:
×
536
        // error code has been set into reqMsg, no need to handle it here.
537
        if (TSDB_CODE_SUCCESS != vnodeGetTableCfg(pVnode, &reqMsg, false)) {
×
538
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
×
539
        }
540
        break;
×
541
      case TDMT_VND_GET_STREAM_PROGRESS:
×
542
        // error code has been set into reqMsg, no need to handle it here.
543
        if (TSDB_CODE_SUCCESS != vnodeGetStreamProgress(pVnode, &reqMsg, false)) {
×
544
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
×
545
        }
546
        break;
×
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
      default:
×
554
        qError("invalid req msgType %d", req->msgType);
×
555
        reqMsg.code = TSDB_CODE_INVALID_MSG;
×
556
        reqMsg.pCont = NULL;
×
557
        reqMsg.contLen = 0;
×
558
        break;
×
559
    }
560

561
    rsp.msgIdx = req->msgIdx;
×
562
    rsp.reqType = reqMsg.msgType;
×
563
    rsp.msgLen = reqMsg.contLen;
×
564
    rsp.rspCode = reqMsg.code;
×
565
    rsp.msg = reqMsg.pCont;
×
566

567
    if (NULL == taosArrayPush(batchRsp.pRsps, &rsp)) {
×
568
      qError("taosArrayPush failed");
×
569
      code = terrno;
×
570
      goto _exit;
×
571
    }
572
  }
573

574
  rspSize = tSerializeSBatchRsp(NULL, 0, &batchRsp);
×
575
  if (rspSize < 0) {
×
576
    qError("tSerializeSBatchRsp failed");
×
577
    code = terrno;
×
578
    goto _exit;
×
579
  }
580
  pRsp = rpcMallocCont(rspSize);
×
581
  if (pRsp == NULL) {
×
582
    qError("rpcMallocCont %d failed", rspSize);
×
583
    code = terrno;
×
584
    goto _exit;
×
585
  }
586
  if (tSerializeSBatchRsp(pRsp, rspSize, &batchRsp) < 0) {
×
587
    qError("tSerializeSBatchRsp %d failed", rspSize);
×
588
    code = terrno;
×
589
    goto _exit;
×
590
  }
591

592
_exit:
×
593

594
  rspMsg.info = pMsg->info;
×
595
  rspMsg.pCont = pRsp;
×
596
  rspMsg.contLen = rspSize;
×
597
  rspMsg.code = code;
×
598
  rspMsg.msgType = pMsg->msgType;
×
599

600
  if (code) {
×
601
    qError("vnd get batch meta failed cause of %s", tstrerror(code));
×
602
  }
603

604
  taosArrayDestroyEx(batchReq.pMsgs, tFreeSBatchReqMsg);
×
605
  taosArrayDestroyEx(batchRsp.pRsps, tFreeSBatchRspMsg);
×
606

607
  tmsgSendRsp(&rspMsg);
×
608

609
  return code;
×
610
}
611

612
#define VNODE_DO_META_QUERY(pVnode, cmd)                 \
613
  do {                                                   \
614
    (void)taosThreadRwlockRdlock(&(pVnode)->metaRWLock); \
615
    cmd;                                                 \
616
    (void)taosThreadRwlockUnlock(&(pVnode)->metaRWLock); \
617
  } while (0)
618

619
int32_t vnodeReadVSubtables(SReadHandle* pHandle, int64_t suid, SArray** ppRes) {
×
620
  int32_t                    code = TSDB_CODE_SUCCESS;
×
621
  int32_t                    line = 0;
×
622
  SMetaReader                mr = {0};
×
623
  bool                       readerInit = false;
×
624
  SVCTableRefCols*           pTb = NULL;
×
625
  int32_t                    refColsNum = 0;
×
626
  char                       tbFName[TSDB_TABLE_FNAME_LEN];
627

628
  SArray *pList = taosArrayInit(10, sizeof(uint64_t));
×
629
  QUERY_CHECK_NULL(pList, code, line, _return, terrno);
×
630

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

633
  size_t num = taosArrayGetSize(pList);
×
634
  *ppRes = taosArrayInit(num, POINTER_BYTES);
×
635
  QUERY_CHECK_NULL(*ppRes, code, line, _return, terrno);
×
636
  SSHashObj* pSrcTbls = tSimpleHashInit(10, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
×
637
  QUERY_CHECK_NULL(pSrcTbls, code, line, _return, terrno);
×
638

639
  for (int32_t i = 0; i < num; ++i) {
×
640
    uint64_t* id = taosArrayGet(pList, i);
×
641
    QUERY_CHECK_NULL(id, code, line, _return, terrno);
×
642
    pHandle->api.metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pHandle->api.metaFn);
×
643
    QUERY_CHECK_CODE(pHandle->api.metaReaderFn.getTableEntryByUid(&mr, *id), line, _return);
×
644
    readerInit = true;
×
645

646
    refColsNum = 0;
×
647
    for (int32_t j = 0; j < mr.me.colRef.nCols; j++) {
×
648
      if (mr.me.colRef.pColRef[j].hasRef) {
×
649
        refColsNum++;
×
650
      }
651
    }
652

653
    if (refColsNum <= 0) {
×
654
      pHandle->api.metaReaderFn.clearReader(&mr);
×
655
      readerInit = false;
×
656
      continue;
×
657
    }
658

659
    pTb = taosMemoryCalloc(1, refColsNum * sizeof(SRefColInfo) + sizeof(*pTb));
×
660
    QUERY_CHECK_NULL(pTb, code, line, _return, terrno);
×
661

662
    pTb->uid = mr.me.uid;
×
663
    pTb->numOfColRefs = refColsNum;
×
664
    pTb->refCols = (SRefColInfo*)(pTb + 1);
×
665

666
    refColsNum = 0;
×
667
    tSimpleHashClear(pSrcTbls);
×
668
    for (int32_t j = 0; j < mr.me.colRef.nCols; j++) {
×
669
      if (!mr.me.colRef.pColRef[j].hasRef) {
×
670
        continue;
×
671
      }
672

673
      pTb->refCols[refColsNum].colId = mr.me.colRef.pColRef[j].id;
×
674
      tstrncpy(pTb->refCols[refColsNum].refColName, mr.me.colRef.pColRef[j].refColName, TSDB_COL_NAME_LEN);
×
675
      tstrncpy(pTb->refCols[refColsNum].refTableName, mr.me.colRef.pColRef[j].refTableName, TSDB_TABLE_NAME_LEN);
×
676
      tstrncpy(pTb->refCols[refColsNum].refDbName, mr.me.colRef.pColRef[j].refDbName, TSDB_DB_NAME_LEN);
×
677

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

680
      if (NULL == tSimpleHashGet(pSrcTbls, tbFName, strlen(tbFName))) {
×
681
        QUERY_CHECK_CODE(tSimpleHashPut(pSrcTbls, tbFName, strlen(tbFName), &code, sizeof(code)), line, _return);
×
682
      }
683

684
      refColsNum++;
×
685
    }
686

687
    pTb->numOfSrcTbls = tSimpleHashGetSize(pSrcTbls);
×
688
    QUERY_CHECK_NULL(taosArrayPush(*ppRes, &pTb), code, line, _return, terrno);
×
689
    pTb = NULL;
×
690

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

695
_return:
×
696

697
  if (readerInit) {
×
698
    pHandle->api.metaReaderFn.clearReader(&mr);
×
699
  }
700

701
  taosArrayDestroy(pList);
×
702
  taosMemoryFree(pTb);
×
703
  tSimpleHashCleanup(pSrcTbls);
×
704

705
  if (code) {
×
706
    qError("%s failed since %s", __func__, tstrerror(code));
×
707
  }
708
  return code;
×
709
}
710

711

712
int32_t vnodeGetVSubtablesMeta(SVnode *pVnode, SRpcMsg *pMsg) {
×
713
  int32_t        code = 0;
×
714
  int32_t        rspSize = 0;
×
715
  SVSubTablesReq req = {0};
×
716
  SVSubTablesRsp rsp = {0};
×
717
  SRpcMsg      rspMsg = {0};
×
718
  void        *pRsp = NULL;
×
719
  int32_t      line = 0;
×
720

721
  if (tDeserializeSVSubTablesReq(pMsg->pCont, pMsg->contLen, &req)) {
×
722
    code = terrno;
×
723
    qError("tDeserializeSVSubTablesReq failed");
×
724
    goto _return;
×
725
  }
726

727
  SReadHandle handle = {.vnode = pVnode};
×
728
  initStorageAPI(&handle.api);
×
729

730
  QUERY_CHECK_CODE(vnodeReadVSubtables(&handle, req.suid, &rsp.pTables), line, _return);
×
731
  rsp.vgId = TD_VID(pVnode);
×
732

733
  rspSize = tSerializeSVSubTablesRsp(NULL, 0, &rsp);
×
734
  if (rspSize < 0) {
×
735
    code = rspSize;
×
736
    qError("tSerializeSVSubTablesRsp failed, error:%d", rspSize);
×
737
    goto _return;
×
738
  }
739
  pRsp = taosMemoryCalloc(1, rspSize);
×
740
  if (pRsp == NULL) {
×
741
    code = terrno;
×
742
    qError("rpcMallocCont %d failed, error:%d", rspSize, terrno);
×
743
    goto _return;
×
744
  }
745
  rspSize = tSerializeSVSubTablesRsp(pRsp, rspSize, &rsp);
×
746
  if (rspSize < 0) {
×
747
    code = rspSize;
×
748
    qError("tSerializeSVSubTablesRsp failed, error:%d", rspSize);
×
749
    goto _return;
×
750
  }
751

752
_return:
×
753

754
  rspMsg.info = pMsg->info;
×
755
  rspMsg.pCont = pRsp;
×
756
  rspMsg.contLen = rspSize;
×
757
  rspMsg.code = code;
×
758
  rspMsg.msgType = pMsg->msgType;
×
759

760
  if (code) {
×
761
    qError("vnd get virtual subtables failed cause of %s", tstrerror(code));
×
762
  }
763

764
  *pMsg = rspMsg;
×
765
  
766
  tDestroySVSubTablesRsp(&rsp);
×
767

768
  //tmsgSendRsp(&rspMsg);
769

770
  return code;
×
771
}
772

773

774
int32_t vnodeGetLoad(SVnode *pVnode, SVnodeLoad *pLoad) {
8✔
775
  SSyncState state = syncGetState(pVnode->sync);
8✔
776
  pLoad->syncAppliedIndex = pVnode->state.applied;
8✔
777
  syncGetCommitIndex(pVnode->sync, &pLoad->syncCommitIndex);
8✔
778

779
  pLoad->vgId = TD_VID(pVnode);
8✔
780
  pLoad->syncState = state.state;
8✔
781
  pLoad->syncRestore = state.restored;
8✔
782
  pLoad->syncTerm = state.term;
8✔
783
  pLoad->roleTimeMs = state.roleTimeMs;
8✔
784
  pLoad->startTimeMs = state.startTimeMs;
8✔
785
  pLoad->syncCanRead = state.canRead;
8✔
786
  pLoad->learnerProgress = state.progress;
8✔
787
  pLoad->cacheUsage = tsdbCacheGetUsage(pVnode);
8✔
788
  pLoad->numOfCachedTables = tsdbCacheGetElems(pVnode);
8✔
789
  VNODE_DO_META_QUERY(pVnode, pLoad->numOfTables = metaGetTbNum(pVnode->pMeta));
8✔
790
  VNODE_DO_META_QUERY(pVnode, pLoad->numOfTimeSeries = metaGetTimeSeriesNum(pVnode->pMeta, 1));
8✔
791
  pLoad->totalStorage = (int64_t)3 * 1073741824;
8✔
792
  pLoad->compStorage = (int64_t)2 * 1073741824;
8✔
793
  pLoad->pointsWritten = 100;
8✔
794
  pLoad->numOfSelectReqs = 1;
8✔
795
  pLoad->numOfInsertReqs = atomic_load_64(&pVnode->statis.nInsert);
8✔
796
  pLoad->numOfInsertSuccessReqs = atomic_load_64(&pVnode->statis.nInsertSuccess);
8✔
797
  pLoad->numOfBatchInsertReqs = atomic_load_64(&pVnode->statis.nBatchInsert);
8✔
798
  pLoad->numOfBatchInsertSuccessReqs = atomic_load_64(&pVnode->statis.nBatchInsertSuccess);
8✔
799
  return 0;
8✔
800
}
801

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

825
void vnodeGetInfo(void *pVnode, const char **dbname, int32_t *vgId, int64_t *numOfTables, int64_t *numOfNormalTables) {
×
826
  SVnode    *pVnodeObj = pVnode;
×
827
  SVnodeCfg *pConf = &pVnodeObj->config;
×
828

829
  if (dbname) {
×
830
    *dbname = pConf->dbname;
×
831
  }
832

833
  if (vgId) {
×
834
    *vgId = TD_VID(pVnodeObj);
×
835
  }
836

837
  if (numOfTables) {
×
838
    *numOfTables = pConf->vndStats.numOfNTables + pConf->vndStats.numOfCTables;
×
839
  }
840

841
  if (numOfNormalTables) {
×
842
    *numOfNormalTables = pConf->vndStats.numOfNTables;
×
843
  }
844
}
×
845

846
int32_t vnodeGetTableList(void *pVnode, int8_t type, SArray *pList) {
×
847
  if (type == TSDB_SUPER_TABLE) {
×
848
    return vnodeGetStbIdList(pVnode, 0, pList);
×
849
  } else {
850
    return TSDB_CODE_INVALID_PARA;
×
851
  }
852
}
853

854
int32_t vnodeGetAllTableList(SVnode *pVnode, uint64_t uid, SArray *list) {
×
855
  int32_t      code = TSDB_CODE_SUCCESS;
×
856
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, uid, 1);
×
857
  if (NULL == pCur) {
×
858
    qError("vnode get all table list failed");
×
859
    return terrno;
×
860
  }
861

862
  while (1) {
×
863
    tb_uid_t id = metaCtbCursorNext(pCur);
×
864
    if (id == 0) {
×
865
      break;
×
866
    }
867

868
    STableKeyInfo info = {uid = id};
×
869
    if (NULL == taosArrayPush(list, &info)) {
×
870
      qError("taosArrayPush failed");
×
871
      code = terrno;
×
872
      goto _exit;
×
873
    }
874
  }
875
_exit:
×
876
  metaCloseCtbCursor(pCur);
×
877
  return code;
×
878
}
879

880
int32_t vnodeGetCtbIdListByFilter(SVnode *pVnode, int64_t suid, SArray *list, bool (*filter)(void *arg), void *arg) {
×
881
  return 0;
×
882
}
883

884
int32_t vnodeGetCtbIdList(void *pVnode, int64_t suid, SArray *list) {
×
885
  int32_t      code = TSDB_CODE_SUCCESS;
×
886
  SVnode      *pVnodeObj = pVnode;
×
887
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnodeObj, suid, 1);
×
888
  if (NULL == pCur) {
×
889
    qError("vnode get all table list failed");
×
890
    return terrno;
×
891
  }
892

893
  while (1) {
×
894
    tb_uid_t id = metaCtbCursorNext(pCur);
×
895
    if (id == 0) {
×
896
      break;
×
897
    }
898

899
    if (NULL == taosArrayPush(list, &id)) {
×
900
      qError("taosArrayPush failed");
×
901
      code = terrno;
×
902
      goto _exit;
×
903
    }
904
  }
905

906
_exit:
×
907
  metaCloseCtbCursor(pCur);
×
908
  return code;
×
909
}
910

911
int32_t vnodeGetStbIdList(SVnode *pVnode, int64_t suid, SArray *list) {
14✔
912
  int32_t      code = TSDB_CODE_SUCCESS;
14✔
913
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, suid);
14✔
914
  if (!pCur) {
14!
915
    return TSDB_CODE_OUT_OF_MEMORY;
×
916
  }
917

918
  while (1) {
9✔
919
    tb_uid_t id = metaStbCursorNext(pCur);
23✔
920
    if (id == 0) {
23✔
921
      break;
14✔
922
    }
923

924
    if (NULL == taosArrayPush(list, &id)) {
9!
925
      qError("taosArrayPush failed");
×
926
      code = terrno;
×
927
      goto _exit;
×
928
    }
929
  }
930

931
_exit:
14✔
932
  metaCloseStbCursor(pCur);
14✔
933
  return code;
14✔
934
}
935

936
int32_t vnodeGetStbIdListByFilter(SVnode *pVnode, int64_t suid, SArray *list, bool (*filter)(void *arg, void *arg1),
×
937
                                  void *arg) {
938
  int32_t      code = TSDB_CODE_SUCCESS;
×
939
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, suid);
×
940
  if (!pCur) {
×
941
    return terrno;
×
942
  }
943

944
  while (1) {
×
945
    tb_uid_t id = metaStbCursorNext(pCur);
×
946
    if (id == 0) {
×
947
      break;
×
948
    }
949

950
    if ((*filter) && (*filter)(arg, &id)) {
×
951
      continue;
×
952
    }
953

954
    if (NULL == taosArrayPush(list, &id)) {
×
955
      qError("taosArrayPush failed");
×
956
      code = terrno;
×
957
      goto _exit;
×
958
    }
959
  }
960

961
_exit:
×
962
  metaCloseStbCursor(pCur);
×
963
  return code;
×
964
}
965

966
int32_t vnodeGetCtbNum(SVnode *pVnode, int64_t suid, int64_t *num) {
9✔
967
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, suid, 0);
9✔
968
  if (!pCur) {
9!
969
    return terrno;
×
970
  }
971

972
  *num = 0;
9✔
973
  while (1) {
1✔
974
    tb_uid_t id = metaCtbCursorNext(pCur);
10✔
975
    if (id == 0) {
10✔
976
      break;
9✔
977
    }
978

979
    ++(*num);
1✔
980
  }
981

982
  metaCloseCtbCursor(pCur);
9✔
983
  return TSDB_CODE_SUCCESS;
9✔
984
}
985

986
int32_t vnodeGetStbColumnNum(SVnode *pVnode, tb_uid_t suid, int *num) {
9✔
987
  SSchemaWrapper *pSW = metaGetTableSchema(pVnode->pMeta, suid, -1, 0, NULL);
9✔
988
  if (pSW) {
9!
989
    *num = pSW->nCols;
9!
990
    tDeleteSchemaWrapper(pSW);
991
  } else {
992
    *num = 2;
×
993
  }
994

995
  return TSDB_CODE_SUCCESS;
9✔
996
}
997

998
int32_t vnodeGetStbKeep(SVnode *pVnode, tb_uid_t suid, int64_t *keep) {
9✔
999
  SMetaReader mr = {0};
9✔
1000
  metaReaderDoInit(&mr, pVnode->pMeta, META_READER_NOLOCK);
9✔
1001

1002
  int32_t code = metaReaderGetTableEntryByUid(&mr, suid);
9✔
1003
  if (code == TSDB_CODE_SUCCESS) {
9!
1004
    *keep = mr.me.stbEntry.keep;
9✔
1005
  } else {
1006
    *keep = 0;  // Default value if not found
×
1007
  }
1008

1009
  metaReaderClear(&mr);
9✔
1010
  return TSDB_CODE_SUCCESS;
9✔
1011
}
1012

1013
#ifdef TD_ENTERPRISE
1014
const char *tkLogStb[] = {"cluster_info",
1015
                          "data_dir",
1016
                          "dnodes_info",
1017
                          "d_info",
1018
                          "grants_info",
1019
                          "keeper_monitor",
1020
                          "logs",
1021
                          "log_dir",
1022
                          "log_summary",
1023
                          "m_info",
1024
                          "taosadapter_restful_http_request_fail",
1025
                          "taosadapter_restful_http_request_in_flight",
1026
                          "taosadapter_restful_http_request_summary_milliseconds",
1027
                          "taosadapter_restful_http_request_total",
1028
                          "taosadapter_system_cpu_percent",
1029
                          "taosadapter_system_mem_percent",
1030
                          "temp_dir",
1031
                          "vgroups_info",
1032
                          "vnodes_role"};
1033
const char *tkAuditStb[] = {"operations"};
1034
const int   tkLogStbNum = ARRAY_SIZE(tkLogStb);
1035
const int   tkAuditStbNum = ARRAY_SIZE(tkAuditStb);
1036

1037
// exclude stbs of taoskeeper log
1038
static int32_t vnodeGetTimeSeriesBlackList(SVnode *pVnode, int32_t *tbSize) {
14✔
1039
  int32_t      code = TSDB_CODE_SUCCESS;
14✔
1040
  int32_t      tbNum = 0;
14✔
1041
  const char **pTbArr = NULL;
14✔
1042
  const char  *dbName = NULL;
14✔
1043
  *tbSize = 0;
14✔
1044

1045
  if (!(dbName = strchr(pVnode->config.dbname, '.'))) return 0;
14!
1046
  if (0 == strncmp(++dbName, "log", TSDB_DB_NAME_LEN)) {
14!
1047
    tbNum = tkLogStbNum;
×
1048
    pTbArr = (const char **)&tkLogStb;
×
1049
  } else if (0 == strncmp(dbName, "audit", TSDB_DB_NAME_LEN)) {
14!
1050
    tbNum = tkAuditStbNum;
×
1051
    pTbArr = (const char **)&tkAuditStb;
×
1052
  }
1053
  if (tbNum && pTbArr) {
14!
1054
    *tbSize = metaSizeOfTbFilterCache(pVnode->pMeta, 0);
×
1055
    if (*tbSize < tbNum) {
×
1056
      for (int32_t i = 0; i < tbNum; ++i) {
×
1057
        tb_uid_t suid = metaGetTableEntryUidByName(pVnode->pMeta, pTbArr[i]);
×
1058
        if (suid != 0) {
×
1059
          code = metaPutTbToFilterCache(pVnode->pMeta, &suid, 0);
×
1060
          if (TSDB_CODE_SUCCESS != code) {
×
1061
            return code;
×
1062
          }
1063
        }
1064
      }
1065
      *tbSize = metaSizeOfTbFilterCache(pVnode->pMeta, 0);
×
1066
    }
1067
  }
1068

1069
  return code;
14✔
1070
}
1071
#endif
1072

1073
static bool vnodeTimeSeriesFilter(void *arg1, void *arg2) {
×
1074
  SVnode *pVnode = (SVnode *)arg1;
×
1075

1076
  if (metaTbInFilterCache(pVnode->pMeta, arg2, 0)) {
×
1077
    return true;
×
1078
  }
1079
  return false;
×
1080
}
1081

1082
int32_t vnodeGetTimeSeriesNum(SVnode *pVnode, int64_t *num) {
14✔
1083
  SArray *suidList = NULL;
14✔
1084

1085
  if (!(suidList = taosArrayInit(1, sizeof(tb_uid_t)))) {
14!
1086
    return terrno;
×
1087
  }
1088

1089
  int32_t tbFilterSize = 0;
14✔
1090
  int32_t code = TSDB_CODE_SUCCESS;
14✔
1091
#ifdef TD_ENTERPRISE
1092
  code = vnodeGetTimeSeriesBlackList(pVnode, &tbFilterSize);
14✔
1093
  if (TSDB_CODE_SUCCESS != code) {
14!
1094
    goto _exit;
×
1095
  }
1096
#endif
1097

1098
  if ((!tbFilterSize && vnodeGetStbIdList(pVnode, 0, suidList) < 0) ||
14!
1099
      (tbFilterSize && vnodeGetStbIdListByFilter(pVnode, 0, suidList, vnodeTimeSeriesFilter, pVnode) < 0)) {
14!
1100
    qError("vgId:%d, failed to get stb id list error: %s", TD_VID(pVnode), terrstr());
×
1101
    taosArrayDestroy(suidList);
×
1102
    return terrno;
×
1103
  }
1104

1105
  *num = 0;
14✔
1106
  int64_t arrSize = taosArrayGetSize(suidList);
14✔
1107
  for (int64_t i = 0; i < arrSize; ++i) {
23✔
1108
    tb_uid_t suid = *(tb_uid_t *)taosArrayGet(suidList, i);
9✔
1109

1110
    int64_t ctbNum = 0;
9✔
1111
    int32_t numOfCols = 0;
9✔
1112
    code = metaGetStbStats(pVnode, suid, &ctbNum, &numOfCols);
9✔
1113
    if (TSDB_CODE_SUCCESS != code) {
9!
1114
      goto _exit;
×
1115
    }
1116
    *num += ctbNum * (numOfCols - 1);
9✔
1117
  }
1118

1119
_exit:
14✔
1120
  taosArrayDestroy(suidList);
14✔
1121
  return TSDB_CODE_SUCCESS;
14✔
1122
}
1123

1124
int32_t vnodeGetAllCtbNum(SVnode *pVnode, int64_t *num) {
×
1125
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, 0);
×
1126
  if (!pCur) {
×
1127
    return terrno;
×
1128
  }
1129

1130
  *num = 0;
×
1131
  while (1) {
×
1132
    tb_uid_t id = metaStbCursorNext(pCur);
×
1133
    if (id == 0) {
×
1134
      break;
×
1135
    }
1136

1137
    int64_t ctbNum = 0;
×
1138
    int32_t code = vnodeGetCtbNum(pVnode, id, &ctbNum);
×
1139
    if (TSDB_CODE_SUCCESS != code) {
×
1140
      metaCloseStbCursor(pCur);
×
1141
      return code;
×
1142
    }
1143

1144
    *num += ctbNum;
×
1145
  }
1146

1147
  metaCloseStbCursor(pCur);
×
1148
  return TSDB_CODE_SUCCESS;
×
1149
}
1150

1151
void *vnodeGetIdx(void *pVnode) {
×
1152
  if (pVnode == NULL) {
×
1153
    return NULL;
×
1154
  }
1155

1156
  return metaGetIdx(((SVnode *)pVnode)->pMeta);
×
1157
}
1158

1159
void *vnodeGetIvtIdx(void *pVnode) {
×
1160
  if (pVnode == NULL) {
×
1161
    return NULL;
×
1162
  }
1163
  return metaGetIvtIdx(((SVnode *)pVnode)->pMeta);
×
1164
}
1165

1166
int32_t vnodeGetTableSchema(void *pVnode, int64_t uid, STSchema **pSchema, int64_t *suid) {
×
1167
  return tsdbGetTableSchema(((SVnode *)pVnode)->pMeta, uid, pSchema, suid);
×
1168
}
1169

1170
static FORCE_INLINE int32_t vnodeGetDBPrimaryInfo(SVnode *pVnode, SDbSizeStatisInfo *pInfo) {
1171
  int32_t code = 0;
×
1172
  char    path[TSDB_FILENAME_LEN] = {0};
×
1173

1174
  char   *dirName[] = {VNODE_TSDB_DIR, VNODE_WAL_DIR, VNODE_META_DIR, VNODE_TSDB_CACHE_DIR};
×
1175
  int64_t dirSize[4];
1176

1177
  vnodeGetPrimaryDir(pVnode->path, pVnode->diskPrimary, pVnode->pTfs, path, TSDB_FILENAME_LEN);
×
1178
  int32_t offset = strlen(path);
×
1179

1180
  for (int i = 0; i < sizeof(dirName) / sizeof(dirName[0]); i++) {
×
1181
    int64_t size = {0};
×
1182
    (void)snprintf(path + offset, TSDB_FILENAME_LEN, "%s%s", TD_DIRSEP, dirName[i]);
×
1183
    code = taosGetDirSize(path, &size);
×
1184
    if (code != 0) {
×
1185
      return code;
×
1186
    }
1187
    path[offset] = 0;
×
1188
    dirSize[i] = size;
×
1189
  }
1190

1191
  pInfo->l1Size = 0;
×
1192
  pInfo->walSize = dirSize[1];
×
1193
  pInfo->metaSize = dirSize[2];
×
1194
  pInfo->cacheSize = dirSize[3];
×
1195
  return code;
×
1196
}
1197
int32_t vnodeGetDBSize(void *pVnode, SDbSizeStatisInfo *pInfo) {
×
1198
  int32_t code = 0;
×
1199
  int32_t lino = 0;
×
1200
  SVnode *pVnodeObj = pVnode;
×
1201
  if (pVnodeObj == NULL) {
×
1202
    return TSDB_CODE_VND_NOT_EXIST;
×
1203
  }
1204
  code = vnodeGetDBPrimaryInfo(pVnode, pInfo);
×
1205
  if (code != 0) goto _exit;
×
1206

1207
  code = tsdbGetFsSize(pVnodeObj->pTsdb, pInfo);
×
1208
_exit:
×
1209
  return code;
×
1210
}
1211

1212
int32_t vnodeGetStreamProgress(SVnode *pVnode, SRpcMsg *pMsg, bool direct) {
×
1213
  int32_t            code = 0;
×
1214
  SStreamProgressReq req;
1215
  SStreamProgressRsp rsp = {0};
×
1216
  SRpcMsg            rpcMsg = {.info = pMsg->info, .code = 0};
×
1217
  char              *buf = NULL;
×
1218
  int32_t            rspLen = 0;
×
1219
  code = tDeserializeStreamProgressReq(pMsg->pCont, pMsg->contLen, &req);
×
1220

1221
  if (code == TSDB_CODE_SUCCESS) {
×
1222
    rsp.fetchIdx = req.fetchIdx;
×
1223
    rsp.subFetchIdx = req.subFetchIdx;
×
1224
    rsp.vgId = req.vgId;
×
1225
    rsp.streamId = req.streamId;
×
1226
    rspLen = tSerializeStreamProgressRsp(0, 0, &rsp);
×
1227
    if (rspLen < 0) {
×
1228
      code = terrno;
×
1229
      goto _OVER;
×
1230
    }
1231
    if (direct) {
×
1232
      buf = rpcMallocCont(rspLen);
×
1233
    } else {
1234
      buf = taosMemoryCalloc(1, rspLen);
×
1235
    }
1236
    if (!buf) {
×
1237
      code = terrno;
×
1238
      goto _OVER;
×
1239
    }
1240
  }
1241

1242
  if (code == TSDB_CODE_SUCCESS) {
×
1243
    code = tqGetStreamExecInfo(pVnode, req.streamId, &rsp.progressDelay, &rsp.fillHisFinished);
×
1244
  }
1245
  if (code == TSDB_CODE_SUCCESS) {
×
1246
    rspLen = tSerializeStreamProgressRsp(buf, rspLen, &rsp);
×
1247
    if (rspLen < 0) {
×
1248
      code = terrno;
×
1249
      goto _OVER;
×
1250
    }
1251
    rpcMsg.pCont = buf;
×
1252
    buf = NULL;
×
1253
    rpcMsg.contLen = rspLen;
×
1254
    rpcMsg.code = code;
×
1255
    rpcMsg.msgType = pMsg->msgType;
×
1256
    if (direct) {
×
1257
      tmsgSendRsp(&rpcMsg);
×
1258
    } else {
1259
      *pMsg = rpcMsg;
×
1260
    }
1261
  }
1262

1263
_OVER:
×
1264
  if (buf) {
×
1265
    taosMemoryFree(buf);
×
1266
  }
1267
  return code;
×
1268
}
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