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

taosdata / TDengine / #3599

08 Feb 2025 11:23AM UTC coverage: 1.77% (-61.6%) from 63.396%
#3599

push

travis-ci

web-flow
Merge pull request #29712 from taosdata/fix/TD-33652-3.0

fix: reduce write rows from 30w to 3w

3776 of 278949 branches covered (1.35%)

Branch coverage included in aggregate %.

6012 of 274147 relevant lines covered (2.19%)

1642.73 hits per line

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

0.0
/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 "vnd.h"
18

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

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

31
void vnodeQueryPreClose(SVnode *pVnode) { qWorkerStopAllTasks((void *)pVnode->pQuery); }
×
32

33
void vnodeQueryClose(SVnode *pVnode) { qWorkerDestroy((void **)&pVnode->pQuery); }
×
34

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

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

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

78
}
79

80

81
int32_t vnodeGetTableMeta(SVnode *pVnode, SRpcMsg *pMsg, bool direct) {
×
82
  STableInfoReq  infoReq = {0};
×
83
  STableMetaRsp  metaRsp = {0};
×
84
  SMetaReader    mer1 = {0};
×
85
  SMetaReader    mer2 = {0};
×
86
  char           tableFName[TSDB_TABLE_FNAME_LEN];
87
  bool           reqTbUid = false;
×
88
  SRpcMsg        rpcMsg = {0};
×
89
  int32_t        code = 0;
×
90
  int32_t        rspLen = 0;
×
91
  void          *pRsp = NULL;
×
92
  SSchemaWrapper schema = {0};
×
93
  SSchemaWrapper schemaTag = {0};
×
94

95
  // decode req
96
  if (tDeserializeSTableInfoReq(pMsg->pCont, pMsg->contLen, &infoReq) != 0) {
×
97
    code = terrno;
×
98
    goto _exit4;
×
99
  }
100

101
  if (infoReq.option == REQ_OPT_TBUID) reqTbUid = true;
×
102
  metaRsp.dbId = pVnode->config.dbId;
×
103
  tstrncpy(metaRsp.tbName, infoReq.tbName, TSDB_TABLE_NAME_LEN);
×
104
  (void)memcpy(metaRsp.dbFName, infoReq.dbFName, sizeof(metaRsp.dbFName));
×
105

106
  if (!reqTbUid) {
×
107
    (void)tsnprintf(tableFName, TSDB_TABLE_FNAME_LEN, "%s.%s", infoReq.dbFName, infoReq.tbName);
×
108
    code = vnodeValidateTableHash(pVnode, tableFName);
×
109
    if (code) {
×
110
      goto _exit4;
×
111
    }
112
  }
113

114
  // query meta
115
  metaReaderDoInit(&mer1, pVnode->pMeta, META_READER_LOCK);
×
116
  if (reqTbUid) {
×
117
    errno = 0;
×
118
    uint64_t tbUid = taosStr2UInt64(infoReq.tbName, NULL, 10);
×
119
    if (errno == ERANGE || tbUid == 0) {
×
120
      code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
121
      goto _exit3;
×
122
    }
123
    SMetaReader mr3 = {0};
×
124
    metaReaderDoInit(&mr3, ((SVnode *)pVnode)->pMeta, META_READER_NOLOCK);
×
125
    if ((code = metaReaderGetTableEntryByUid(&mr3, tbUid)) < 0) {
×
126
      metaReaderClear(&mr3);
×
127
      TAOS_CHECK_GOTO(code, NULL, _exit3);
×
128
    }
129
    tstrncpy(metaRsp.tbName, mr3.me.name, TSDB_TABLE_NAME_LEN);
×
130
    metaReaderClear(&mr3);
×
131
    TAOS_CHECK_GOTO(metaGetTableEntryByName(&mer1, metaRsp.tbName), NULL, _exit3);
×
132
  } else if (metaGetTableEntryByName(&mer1, infoReq.tbName) < 0) {
×
133
    code = terrno;
×
134
    goto _exit3;
×
135
  }
136

137
  metaRsp.tableType = mer1.me.type;
×
138
  metaRsp.vgId = TD_VID(pVnode);
×
139
  metaRsp.tuid = mer1.me.uid;
×
140

141
  if (mer1.me.type == TSDB_SUPER_TABLE) {
×
142
    tstrncpy(metaRsp.stbName, mer1.me.name, TSDB_TABLE_NAME_LEN);
×
143
    schema = mer1.me.stbEntry.schemaRow;
×
144
    schemaTag = mer1.me.stbEntry.schemaTag;
×
145
    metaRsp.suid = mer1.me.uid;
×
146
  } else if (mer1.me.type == TSDB_CHILD_TABLE) {
×
147
    metaReaderDoInit(&mer2, pVnode->pMeta, META_READER_NOLOCK);
×
148
    if (metaReaderGetTableEntryByUid(&mer2, mer1.me.ctbEntry.suid) < 0) goto _exit2;
×
149

150
    tstrncpy(metaRsp.stbName, mer2.me.name, TSDB_TABLE_NAME_LEN);
×
151
    metaRsp.suid = mer2.me.uid;
×
152
    schema = mer2.me.stbEntry.schemaRow;
×
153
    schemaTag = mer2.me.stbEntry.schemaTag;
×
154
  } else if (mer1.me.type == TSDB_NORMAL_TABLE) {
×
155
    schema = mer1.me.ntbEntry.schemaRow;
×
156
  } else {
157
    vError("vnodeGetTableMeta get invalid table type:%d", mer1.me.type);
×
158
    goto _exit3;
×
159
  }
160

161
  metaRsp.numOfTags = schemaTag.nCols;
×
162
  metaRsp.numOfColumns = schema.nCols;
×
163
  metaRsp.precision = pVnode->config.tsdbCfg.precision;
×
164
  metaRsp.sversion = schema.version;
×
165
  metaRsp.tversion = schemaTag.version;
×
166
  metaRsp.pSchemas = (SSchema *)taosMemoryMalloc(sizeof(SSchema) * (metaRsp.numOfColumns + metaRsp.numOfTags));
×
167
  metaRsp.pSchemaExt = (SSchemaExt *)taosMemoryCalloc(metaRsp.numOfColumns, sizeof(SSchemaExt));
×
168
  if (NULL == metaRsp.pSchemas || NULL == metaRsp.pSchemaExt) {
×
169
    code = terrno;
×
170
    goto _exit;
×
171
  }
172
  (void)memcpy(metaRsp.pSchemas, schema.pSchema, sizeof(SSchema) * schema.nCols);
×
173
  if (schemaTag.nCols) {
×
174
    (void)memcpy(metaRsp.pSchemas + schema.nCols, schemaTag.pSchema, sizeof(SSchema) * schemaTag.nCols);
×
175
  }
176
  if (metaRsp.pSchemaExt) {
×
177
    SMetaReader *pReader = mer1.me.type == TSDB_CHILD_TABLE ? &mer2 : &mer1;
×
178
    code = fillTableColCmpr(pReader, metaRsp.pSchemaExt, metaRsp.numOfColumns);
×
179
    if (code < 0) {
×
180
      goto _exit;
×
181
    }
182
  } else {
183
    code = TSDB_CODE_OUT_OF_MEMORY;
×
184
    goto _exit;
×
185
  }
186

187
  vnodePrintTableMeta(&metaRsp);
×
188

189
  // encode and send response
190
  rspLen = tSerializeSTableMetaRsp(NULL, 0, &metaRsp);
×
191
  if (rspLen < 0) {
×
192
    code = terrno;
×
193
    goto _exit;
×
194
  }
195

196
  if (direct) {
×
197
    pRsp = rpcMallocCont(rspLen);
×
198
  } else {
199
    pRsp = taosMemoryCalloc(1, rspLen);
×
200
  }
201

202
  if (pRsp == NULL) {
×
203
    code = terrno;
×
204
    goto _exit;
×
205
  }
206

207
  rspLen = tSerializeSTableMetaRsp(pRsp, rspLen, &metaRsp);
×
208
  if (rspLen < 0) {
×
209
    code = terrno;
×
210
    goto _exit;
×
211
  }
212

213
_exit:
×
214
  taosMemoryFree(metaRsp.pSchemas);
×
215
  taosMemoryFree(metaRsp.pSchemaExt);
×
216
_exit2:
×
217
  metaReaderClear(&mer2);
×
218
_exit3:
×
219
  metaReaderClear(&mer1);
×
220
_exit4:
×
221
  rpcMsg.info = pMsg->info;
×
222
  rpcMsg.pCont = pRsp;
×
223
  rpcMsg.contLen = rspLen;
×
224
  rpcMsg.code = code;
×
225
  rpcMsg.msgType = pMsg->msgType;
×
226

227
  if (code) {
×
228
    qError("get table %s meta with %" PRIu8 " failed cause of %s", infoReq.tbName, infoReq.option, tstrerror(code));
×
229
  }
230

231
  if (direct) {
×
232
    tmsgSendRsp(&rpcMsg);
×
233
  } else {
234
    *pMsg = rpcMsg;
×
235
  }
236

237
  return code;
×
238
}
239

240
int32_t vnodeGetTableCfg(SVnode *pVnode, SRpcMsg *pMsg, bool direct) {
×
241
  STableCfgReq   cfgReq = {0};
×
242
  STableCfgRsp   cfgRsp = {0};
×
243
  SMetaReader    mer1 = {0};
×
244
  SMetaReader    mer2 = {0};
×
245
  char           tableFName[TSDB_TABLE_FNAME_LEN];
246
  SRpcMsg        rpcMsg = {0};
×
247
  int32_t        code = 0;
×
248
  int32_t        rspLen = 0;
×
249
  void          *pRsp = NULL;
×
250
  SSchemaWrapper schema = {0};
×
251
  SSchemaWrapper schemaTag = {0};
×
252

253
  // decode req
254
  if (tDeserializeSTableCfgReq(pMsg->pCont, pMsg->contLen, &cfgReq) != 0) {
×
255
    code = terrno;
×
256
    goto _exit;
×
257
  }
258

259
  tstrncpy(cfgRsp.tbName, cfgReq.tbName, TSDB_TABLE_NAME_LEN);
×
260
  (void)memcpy(cfgRsp.dbFName, cfgReq.dbFName, sizeof(cfgRsp.dbFName));
×
261

262
  (void)tsnprintf(tableFName, TSDB_TABLE_FNAME_LEN, "%s.%s", cfgReq.dbFName, cfgReq.tbName);
×
263
  code = vnodeValidateTableHash(pVnode, tableFName);
×
264
  if (code) {
×
265
    goto _exit;
×
266
  }
267

268
  // query meta
269
  metaReaderDoInit(&mer1, pVnode->pMeta, META_READER_LOCK);
×
270

271
  if (metaGetTableEntryByName(&mer1, cfgReq.tbName) < 0) {
×
272
    code = terrno;
×
273
    goto _exit;
×
274
  }
275

276
  cfgRsp.tableType = mer1.me.type;
×
277

278
  if (mer1.me.type == TSDB_SUPER_TABLE) {
×
279
    code = TSDB_CODE_VND_HASH_MISMATCH;
×
280
    goto _exit;
×
281
  } else if (mer1.me.type == TSDB_CHILD_TABLE) {
×
282
    metaReaderDoInit(&mer2, pVnode->pMeta, META_READER_NOLOCK);
×
283
    if (metaReaderGetTableEntryByUid(&mer2, mer1.me.ctbEntry.suid) < 0) goto _exit;
×
284

285
    tstrncpy(cfgRsp.stbName, mer2.me.name, TSDB_TABLE_NAME_LEN);
×
286
    schema = mer2.me.stbEntry.schemaRow;
×
287
    schemaTag = mer2.me.stbEntry.schemaTag;
×
288
    cfgRsp.ttl = mer1.me.ctbEntry.ttlDays;
×
289
    cfgRsp.commentLen = mer1.me.ctbEntry.commentLen;
×
290
    if (mer1.me.ctbEntry.commentLen > 0) {
×
291
      cfgRsp.pComment = taosStrdup(mer1.me.ctbEntry.comment);
×
292
      if (NULL == cfgRsp.pComment) {
×
293
        code = terrno;
×
294
        goto _exit;
×
295
      }
296
    }
297
    STag *pTag = (STag *)mer1.me.ctbEntry.pTags;
×
298
    cfgRsp.tagsLen = pTag->len;
×
299
    cfgRsp.pTags = taosMemoryMalloc(cfgRsp.tagsLen);
×
300
    if (NULL == cfgRsp.pTags) {
×
301
      code = terrno;
×
302
      goto _exit;
×
303
    }
304
    (void)memcpy(cfgRsp.pTags, pTag, cfgRsp.tagsLen);
×
305
  } else if (mer1.me.type == TSDB_NORMAL_TABLE) {
×
306
    schema = mer1.me.ntbEntry.schemaRow;
×
307
    cfgRsp.ttl = mer1.me.ntbEntry.ttlDays;
×
308
    cfgRsp.commentLen = mer1.me.ntbEntry.commentLen;
×
309
    if (mer1.me.ntbEntry.commentLen > 0) {
×
310
      cfgRsp.pComment = taosStrdup(mer1.me.ntbEntry.comment);
×
311
      if (NULL == cfgRsp.pComment) {
×
312
        code = terrno;
×
313
        goto _exit;
×
314
      }
315
    }
316
  } else {
317
    vError("vnodeGetTableCfg get invalid table type:%d", mer1.me.type);
×
318
    code = TSDB_CODE_APP_ERROR;
×
319
    goto _exit;
×
320
  }
321

322
  cfgRsp.numOfTags = schemaTag.nCols;
×
323
  cfgRsp.numOfColumns = schema.nCols;
×
324
  cfgRsp.pSchemas = (SSchema *)taosMemoryMalloc(sizeof(SSchema) * (cfgRsp.numOfColumns + cfgRsp.numOfTags));
×
325
  cfgRsp.pSchemaExt = (SSchemaExt *)taosMemoryMalloc(cfgRsp.numOfColumns * sizeof(SSchemaExt));
×
326

327
  if (NULL == cfgRsp.pSchemas || NULL == cfgRsp.pSchemaExt) {
×
328
    code = terrno;
×
329
    goto _exit;
×
330
  }
331
  (void)memcpy(cfgRsp.pSchemas, schema.pSchema, sizeof(SSchema) * schema.nCols);
×
332
  if (schemaTag.nCols) {
×
333
    (void)memcpy(cfgRsp.pSchemas + schema.nCols, schemaTag.pSchema, sizeof(SSchema) * schemaTag.nCols);
×
334
  }
335

336
  // if (useCompress(cfgRsp.tableType)) {
337

338
  SMetaReader     *pReader = mer1.me.type == TSDB_CHILD_TABLE ? &mer2 : &mer1;
×
339
  SColCmprWrapper *pColCmpr = &pReader->me.colCmpr;
×
340

341
  for (int32_t i = 0; i < cfgRsp.numOfColumns; i++) {
×
342
    SColCmpr   *pCmpr = &pColCmpr->pColCmpr[i];
×
343
    SSchemaExt *pSchExt = cfgRsp.pSchemaExt + i;
×
344
    pSchExt->colId = pCmpr->id;
×
345
    pSchExt->compress = pCmpr->alg;
×
346
  }
347
  //}
348

349
  // encode and send response
350
  rspLen = tSerializeSTableCfgRsp(NULL, 0, &cfgRsp);
×
351
  if (rspLen < 0) {
×
352
    code = terrno;
×
353
    goto _exit;
×
354
  }
355

356
  if (direct) {
×
357
    pRsp = rpcMallocCont(rspLen);
×
358
  } else {
359
    pRsp = taosMemoryCalloc(1, rspLen);
×
360
  }
361

362
  if (pRsp == NULL) {
×
363
    code = terrno;
×
364
    goto _exit;
×
365
  }
366

367
  rspLen = tSerializeSTableCfgRsp(pRsp, rspLen, &cfgRsp);
×
368
  if (rspLen < 0) {
×
369
    code = terrno;
×
370
    goto _exit;
×
371
  }
372

373
_exit:
×
374
  rpcMsg.info = pMsg->info;
×
375
  rpcMsg.pCont = pRsp;
×
376
  rpcMsg.contLen = rspLen;
×
377
  rpcMsg.code = code;
×
378
  rpcMsg.msgType = pMsg->msgType;
×
379

380
  if (code) {
×
381
    qError("get table %s cfg failed cause of %s", cfgReq.tbName, tstrerror(code));
×
382
  }
383

384
  if (direct) {
×
385
    tmsgSendRsp(&rpcMsg);
×
386
  } else {
387
    *pMsg = rpcMsg;
×
388
  }
389

390
  tFreeSTableCfgRsp(&cfgRsp);
×
391
  metaReaderClear(&mer2);
×
392
  metaReaderClear(&mer1);
×
393
  return code;
×
394
}
395

396
static FORCE_INLINE void vnodeFreeSBatchRspMsg(void *p) {
397
  if (NULL == p) {
398
    return;
399
  }
400

401
  SBatchRspMsg *pRsp = (SBatchRspMsg *)p;
402
  rpcFreeCont(pRsp->msg);
403
}
404

405
int32_t vnodeGetBatchMeta(SVnode *pVnode, SRpcMsg *pMsg) {
×
406
  int32_t      code = 0;
×
407
  int32_t      rspSize = 0;
×
408
  SBatchReq    batchReq = {0};
×
409
  SBatchMsg   *req = NULL;
×
410
  SBatchRspMsg rsp = {0};
×
411
  SBatchRsp    batchRsp = {0};
×
412
  SRpcMsg      reqMsg = *pMsg;
×
413
  SRpcMsg      rspMsg = {0};
×
414
  void        *pRsp = NULL;
×
415

416
  if (tDeserializeSBatchReq(pMsg->pCont, pMsg->contLen, &batchReq)) {
×
417
    code = terrno;
×
418
    qError("tDeserializeSBatchReq failed");
×
419
    goto _exit;
×
420
  }
421

422
  int32_t msgNum = taosArrayGetSize(batchReq.pMsgs);
×
423
  if (msgNum >= MAX_META_MSG_IN_BATCH) {
×
424
    code = TSDB_CODE_INVALID_MSG;
×
425
    qError("too many msgs %d in vnode batch meta req", msgNum);
×
426
    goto _exit;
×
427
  }
428

429
  batchRsp.pRsps = taosArrayInit(msgNum, sizeof(SBatchRspMsg));
×
430
  if (NULL == batchRsp.pRsps) {
×
431
    code = terrno;
×
432
    qError("taosArrayInit %d SBatchRspMsg failed", msgNum);
×
433
    goto _exit;
×
434
  }
435

436
  for (int32_t i = 0; i < msgNum; ++i) {
×
437
    req = taosArrayGet(batchReq.pMsgs, i);
×
438
    if (req == NULL) {
×
439
      code = terrno;
×
440
      goto _exit;
×
441
    }
442

443
    reqMsg.msgType = req->msgType;
×
444
    reqMsg.pCont = req->msg;
×
445
    reqMsg.contLen = req->msgLen;
×
446

447
    switch (req->msgType) {
×
448
      case TDMT_VND_TABLE_META:
×
449
        // error code has been set into reqMsg, no need to handle it here.
450
        if (TSDB_CODE_SUCCESS != vnodeGetTableMeta(pVnode, &reqMsg, false)) {
×
451
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
×
452
        }
453
        break;
×
454
      case TDMT_VND_TABLE_NAME:
×
455
        // error code has been set into reqMsg, no need to handle it here.
456
        if (TSDB_CODE_SUCCESS != vnodeGetTableMeta(pVnode, &reqMsg, false)) {
×
457
          qWarn("vnodeGetBatchName failed, msgType:%d", req->msgType);
×
458
        }
459
        break;
×
460
      case TDMT_VND_TABLE_CFG:
×
461
        // error code has been set into reqMsg, no need to handle it here.
462
        if (TSDB_CODE_SUCCESS != vnodeGetTableCfg(pVnode, &reqMsg, false)) {
×
463
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
×
464
        }
465
        break;
×
466
      case TDMT_VND_GET_STREAM_PROGRESS:
×
467
        // error code has been set into reqMsg, no need to handle it here.
468
        if (TSDB_CODE_SUCCESS != vnodeGetStreamProgress(pVnode, &reqMsg, false)) {
×
469
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
×
470
        }
471
        break;
×
472
      default:
×
473
        qError("invalid req msgType %d", req->msgType);
×
474
        reqMsg.code = TSDB_CODE_INVALID_MSG;
×
475
        reqMsg.pCont = NULL;
×
476
        reqMsg.contLen = 0;
×
477
        break;
×
478
    }
479

480
    rsp.msgIdx = req->msgIdx;
×
481
    rsp.reqType = reqMsg.msgType;
×
482
    rsp.msgLen = reqMsg.contLen;
×
483
    rsp.rspCode = reqMsg.code;
×
484
    rsp.msg = reqMsg.pCont;
×
485

486
    if (NULL == taosArrayPush(batchRsp.pRsps, &rsp)) {
×
487
      qError("taosArrayPush failed");
×
488
      code = terrno;
×
489
      goto _exit;
×
490
    }
491
  }
492

493
  rspSize = tSerializeSBatchRsp(NULL, 0, &batchRsp);
×
494
  if (rspSize < 0) {
×
495
    qError("tSerializeSBatchRsp failed");
×
496
    code = terrno;
×
497
    goto _exit;
×
498
  }
499
  pRsp = rpcMallocCont(rspSize);
×
500
  if (pRsp == NULL) {
×
501
    qError("rpcMallocCont %d failed", rspSize);
×
502
    code = terrno;
×
503
    goto _exit;
×
504
  }
505
  if (tSerializeSBatchRsp(pRsp, rspSize, &batchRsp) < 0) {
×
506
    qError("tSerializeSBatchRsp %d failed", rspSize);
×
507
    code = terrno;
×
508
    goto _exit;
×
509
  }
510

511
_exit:
×
512

513
  rspMsg.info = pMsg->info;
×
514
  rspMsg.pCont = pRsp;
×
515
  rspMsg.contLen = rspSize;
×
516
  rspMsg.code = code;
×
517
  rspMsg.msgType = pMsg->msgType;
×
518

519
  if (code) {
×
520
    qError("vnd get batch meta failed cause of %s", tstrerror(code));
×
521
  }
522

523
  taosArrayDestroyEx(batchReq.pMsgs, tFreeSBatchReqMsg);
×
524
  taosArrayDestroyEx(batchRsp.pRsps, tFreeSBatchRspMsg);
×
525

526
  tmsgSendRsp(&rspMsg);
×
527

528
  return code;
×
529
}
530

531
int32_t vnodeGetLoad(SVnode *pVnode, SVnodeLoad *pLoad) {
×
532
  SSyncState state = syncGetState(pVnode->sync);
×
533

534
  pLoad->vgId = TD_VID(pVnode);
×
535
  pLoad->syncState = state.state;
×
536
  pLoad->syncRestore = state.restored;
×
537
  pLoad->syncTerm = state.term;
×
538
  pLoad->roleTimeMs = state.roleTimeMs;
×
539
  pLoad->startTimeMs = state.startTimeMs;
×
540
  pLoad->syncCanRead = state.canRead;
×
541
  pLoad->learnerProgress = state.progress;
×
542
  pLoad->cacheUsage = tsdbCacheGetUsage(pVnode);
×
543
  pLoad->numOfCachedTables = tsdbCacheGetElems(pVnode);
×
544
  pLoad->numOfTables = metaGetTbNum(pVnode->pMeta);
×
545
  pLoad->numOfTimeSeries = metaGetTimeSeriesNum(pVnode->pMeta, 1);
×
546
  pLoad->totalStorage = (int64_t)3 * 1073741824;
×
547
  pLoad->compStorage = (int64_t)2 * 1073741824;
×
548
  pLoad->pointsWritten = 100;
×
549
  pLoad->numOfSelectReqs = 1;
×
550
  pLoad->numOfInsertReqs = atomic_load_64(&pVnode->statis.nInsert);
×
551
  pLoad->numOfInsertSuccessReqs = atomic_load_64(&pVnode->statis.nInsertSuccess);
×
552
  pLoad->numOfBatchInsertReqs = atomic_load_64(&pVnode->statis.nBatchInsert);
×
553
  pLoad->numOfBatchInsertSuccessReqs = atomic_load_64(&pVnode->statis.nBatchInsertSuccess);
×
554
  return 0;
×
555
}
556

557
int32_t vnodeGetLoadLite(SVnode *pVnode, SVnodeLoadLite *pLoad) {
×
558
  SSyncState syncState = syncGetState(pVnode->sync);
×
559
  if (syncState.state == TAOS_SYNC_STATE_LEADER || syncState.state == TAOS_SYNC_STATE_ASSIGNED_LEADER) {
×
560
    pLoad->vgId = TD_VID(pVnode);
×
561
    pLoad->nTimeSeries = metaGetTimeSeriesNum(pVnode->pMeta, 1);
×
562
    return 0;
×
563
  }
564
  return -1;
×
565
}
566
/**
567
 * @brief Reset the statistics value by monitor interval
568
 *
569
 * @param pVnode
570
 * @param pLoad
571
 */
572
void vnodeResetLoad(SVnode *pVnode, SVnodeLoad *pLoad) {
×
573
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nInsert, pLoad->numOfInsertReqs, 64, "nInsert");
×
574
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nInsertSuccess, pLoad->numOfInsertSuccessReqs, 64, "nInsertSuccess");
×
575
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nBatchInsert, pLoad->numOfBatchInsertReqs, 64, "nBatchInsert");
×
576
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nBatchInsertSuccess, pLoad->numOfBatchInsertSuccessReqs, 64,
×
577
                            "nBatchInsertSuccess");
578
}
×
579

580
void vnodeGetInfo(void *pVnode, const char **dbname, int32_t *vgId, int64_t *numOfTables, int64_t *numOfNormalTables) {
×
581
  SVnode    *pVnodeObj = pVnode;
×
582
  SVnodeCfg *pConf = &pVnodeObj->config;
×
583

584
  if (dbname) {
×
585
    *dbname = pConf->dbname;
×
586
  }
587

588
  if (vgId) {
×
589
    *vgId = TD_VID(pVnodeObj);
×
590
  }
591

592
  if (numOfTables) {
×
593
    *numOfTables = pConf->vndStats.numOfNTables + pConf->vndStats.numOfCTables;
×
594
  }
595

596
  if (numOfNormalTables) {
×
597
    *numOfNormalTables = pConf->vndStats.numOfNTables;
×
598
  }
599
}
×
600

601
int32_t vnodeGetTableList(void *pVnode, int8_t type, SArray *pList) {
×
602
  if (type == TSDB_SUPER_TABLE) {
×
603
    return vnodeGetStbIdList(pVnode, 0, pList);
×
604
  } else {
605
    return TSDB_CODE_INVALID_PARA;
×
606
  }
607
}
608

609
int32_t vnodeGetAllTableList(SVnode *pVnode, uint64_t uid, SArray *list) {
×
610
  int32_t      code = TSDB_CODE_SUCCESS;
×
611
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, uid, 1);
×
612
  if (NULL == pCur) {
×
613
    qError("vnode get all table list failed");
×
614
    return terrno;
×
615
  }
616

617
  while (1) {
×
618
    tb_uid_t id = metaCtbCursorNext(pCur);
×
619
    if (id == 0) {
×
620
      break;
×
621
    }
622

623
    STableKeyInfo info = {uid = id};
×
624
    if (NULL == taosArrayPush(list, &info)) {
×
625
      qError("taosArrayPush failed");
×
626
      code = terrno;
×
627
      goto _exit;
×
628
    }
629
  }
630
_exit:
×
631
  metaCloseCtbCursor(pCur);
×
632
  return code;
×
633
}
634

635
int32_t vnodeGetCtbIdListByFilter(SVnode *pVnode, int64_t suid, SArray *list, bool (*filter)(void *arg), void *arg) {
×
636
  return 0;
×
637
}
638

639
int32_t vnodeGetCtbIdList(void *pVnode, int64_t suid, SArray *list) {
×
640
  int32_t      code = TSDB_CODE_SUCCESS;
×
641
  SVnode      *pVnodeObj = pVnode;
×
642
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnodeObj, suid, 1);
×
643
  if (NULL == pCur) {
×
644
    qError("vnode get all table list failed");
×
645
    return terrno;
×
646
  }
647

648
  while (1) {
×
649
    tb_uid_t id = metaCtbCursorNext(pCur);
×
650
    if (id == 0) {
×
651
      break;
×
652
    }
653

654
    if (NULL == taosArrayPush(list, &id)) {
×
655
      qError("taosArrayPush failed");
×
656
      code = terrno;
×
657
      goto _exit;
×
658
    }
659
  }
660

661
_exit:
×
662
  metaCloseCtbCursor(pCur);
×
663
  return code;
×
664
}
665

666
int32_t vnodeGetStbIdList(SVnode *pVnode, int64_t suid, SArray *list) {
×
667
  int32_t      code = TSDB_CODE_SUCCESS;
×
668
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, suid);
×
669
  if (!pCur) {
×
670
    return TSDB_CODE_OUT_OF_MEMORY;
×
671
  }
672

673
  while (1) {
×
674
    tb_uid_t id = metaStbCursorNext(pCur);
×
675
    if (id == 0) {
×
676
      break;
×
677
    }
678

679
    if (NULL == taosArrayPush(list, &id)) {
×
680
      qError("taosArrayPush failed");
×
681
      code = terrno;
×
682
      goto _exit;
×
683
    }
684
  }
685

686
_exit:
×
687
  metaCloseStbCursor(pCur);
×
688
  return code;
×
689
}
690

691
int32_t vnodeGetStbIdListByFilter(SVnode *pVnode, int64_t suid, SArray *list, bool (*filter)(void *arg, void *arg1),
×
692
                                  void *arg) {
693
  int32_t      code = TSDB_CODE_SUCCESS;
×
694
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, suid);
×
695
  if (!pCur) {
×
696
    return terrno;
×
697
  }
698

699
  while (1) {
×
700
    tb_uid_t id = metaStbCursorNext(pCur);
×
701
    if (id == 0) {
×
702
      break;
×
703
    }
704

705
    if ((*filter) && (*filter)(arg, &id)) {
×
706
      continue;
×
707
    }
708

709
    if (NULL == taosArrayPush(list, &id)) {
×
710
      qError("taosArrayPush failed");
×
711
      code = terrno;
×
712
      goto _exit;
×
713
    }
714
  }
715

716
_exit:
×
717
  metaCloseStbCursor(pCur);
×
718
  return code;
×
719
}
720

721
int32_t vnodeGetCtbNum(SVnode *pVnode, int64_t suid, int64_t *num) {
×
722
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, suid, 0);
×
723
  if (!pCur) {
×
724
    return terrno;
×
725
  }
726

727
  *num = 0;
×
728
  while (1) {
×
729
    tb_uid_t id = metaCtbCursorNext(pCur);
×
730
    if (id == 0) {
×
731
      break;
×
732
    }
733

734
    ++(*num);
×
735
  }
736

737
  metaCloseCtbCursor(pCur);
×
738
  return TSDB_CODE_SUCCESS;
×
739
}
740

741
int32_t vnodeGetStbColumnNum(SVnode *pVnode, tb_uid_t suid, int *num) {
×
742
  SSchemaWrapper *pSW = metaGetTableSchema(pVnode->pMeta, suid, -1, 0, NULL);
×
743
  if (pSW) {
×
744
    *num = pSW->nCols;
×
745
    tDeleteSchemaWrapper(pSW);
746
  } else {
747
    *num = 2;
×
748
  }
749

750
  return TSDB_CODE_SUCCESS;
×
751
}
752

753
#ifdef TD_ENTERPRISE
754
const char *tkLogStb[] = {"cluster_info",
755
                          "data_dir",
756
                          "dnodes_info",
757
                          "d_info",
758
                          "grants_info",
759
                          "keeper_monitor",
760
                          "logs",
761
                          "log_dir",
762
                          "log_summary",
763
                          "m_info",
764
                          "taosadapter_restful_http_request_fail",
765
                          "taosadapter_restful_http_request_in_flight",
766
                          "taosadapter_restful_http_request_summary_milliseconds",
767
                          "taosadapter_restful_http_request_total",
768
                          "taosadapter_system_cpu_percent",
769
                          "taosadapter_system_mem_percent",
770
                          "temp_dir",
771
                          "vgroups_info",
772
                          "vnodes_role"};
773
const char *tkAuditStb[] = {"operations"};
774
const int   tkLogStbNum = ARRAY_SIZE(tkLogStb);
775
const int   tkAuditStbNum = ARRAY_SIZE(tkAuditStb);
776

777
// exclude stbs of taoskeeper log
778
static int32_t vnodeGetTimeSeriesBlackList(SVnode *pVnode, int32_t *tbSize) {
×
779
  int32_t      code = TSDB_CODE_SUCCESS;
×
780
  int32_t      tbNum = 0;
×
781
  const char **pTbArr = NULL;
×
782
  const char  *dbName = NULL;
×
783
  *tbSize = 0;
×
784

785
  if (!(dbName = strchr(pVnode->config.dbname, '.'))) return 0;
×
786
  if (0 == strncmp(++dbName, "log", TSDB_DB_NAME_LEN)) {
×
787
    tbNum = tkLogStbNum;
×
788
    pTbArr = (const char **)&tkLogStb;
×
789
  } else if (0 == strncmp(dbName, "audit", TSDB_DB_NAME_LEN)) {
×
790
    tbNum = tkAuditStbNum;
×
791
    pTbArr = (const char **)&tkAuditStb;
×
792
  }
793
  if (tbNum && pTbArr) {
×
794
    *tbSize = metaSizeOfTbFilterCache(pVnode->pMeta, 0);
×
795
    if (*tbSize < tbNum) {
×
796
      for (int32_t i = 0; i < tbNum; ++i) {
×
797
        tb_uid_t suid = metaGetTableEntryUidByName(pVnode->pMeta, pTbArr[i]);
×
798
        if (suid != 0) {
×
799
          code = metaPutTbToFilterCache(pVnode->pMeta, &suid, 0);
×
800
          if (TSDB_CODE_SUCCESS != code) {
×
801
            return code;
×
802
          }
803
        }
804
      }
805
      *tbSize = metaSizeOfTbFilterCache(pVnode->pMeta, 0);
×
806
    }
807
  }
808

809
  return code;
×
810
}
811
#endif
812

813
static bool vnodeTimeSeriesFilter(void *arg1, void *arg2) {
×
814
  SVnode *pVnode = (SVnode *)arg1;
×
815

816
  if (metaTbInFilterCache(pVnode->pMeta, arg2, 0)) {
×
817
    return true;
×
818
  }
819
  return false;
×
820
}
821

822
int32_t vnodeGetTimeSeriesNum(SVnode *pVnode, int64_t *num) {
×
823
  SArray *suidList = NULL;
×
824

825
  if (!(suidList = taosArrayInit(1, sizeof(tb_uid_t)))) {
×
826
    return terrno;
×
827
  }
828

829
  int32_t tbFilterSize = 0;
×
830
  int32_t code = TSDB_CODE_SUCCESS;
×
831
#ifdef TD_ENTERPRISE
832
  code = vnodeGetTimeSeriesBlackList(pVnode, &tbFilterSize);
×
833
  if (TSDB_CODE_SUCCESS != code) {
×
834
    goto _exit;
×
835
  }
836
#endif
837

838
  if ((!tbFilterSize && vnodeGetStbIdList(pVnode, 0, suidList) < 0) ||
×
839
      (tbFilterSize && vnodeGetStbIdListByFilter(pVnode, 0, suidList, vnodeTimeSeriesFilter, pVnode) < 0)) {
×
840
    qError("vgId:%d, failed to get stb id list error: %s", TD_VID(pVnode), terrstr());
×
841
    taosArrayDestroy(suidList);
×
842
    return terrno;
×
843
  }
844

845
  *num = 0;
×
846
  int64_t arrSize = taosArrayGetSize(suidList);
×
847
  for (int64_t i = 0; i < arrSize; ++i) {
×
848
    tb_uid_t suid = *(tb_uid_t *)taosArrayGet(suidList, i);
×
849

850
    int64_t ctbNum = 0;
×
851
    int32_t numOfCols = 0;
×
852
    code = metaGetStbStats(pVnode, suid, &ctbNum, &numOfCols);
×
853
    if (TSDB_CODE_SUCCESS != code) {
×
854
      goto _exit;
×
855
    }
856
    *num += ctbNum * (numOfCols - 1);
×
857
  }
858

859
_exit:
×
860
  taosArrayDestroy(suidList);
×
861
  return TSDB_CODE_SUCCESS;
×
862
}
863

864
int32_t vnodeGetAllCtbNum(SVnode *pVnode, int64_t *num) {
×
865
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, 0);
×
866
  if (!pCur) {
×
867
    return terrno;
×
868
  }
869

870
  *num = 0;
×
871
  while (1) {
×
872
    tb_uid_t id = metaStbCursorNext(pCur);
×
873
    if (id == 0) {
×
874
      break;
×
875
    }
876

877
    int64_t ctbNum = 0;
×
878
    int32_t code = vnodeGetCtbNum(pVnode, id, &ctbNum);
×
879
    if (TSDB_CODE_SUCCESS != code) {
×
880
      metaCloseStbCursor(pCur);
×
881
      return code;
×
882
    }
883

884
    *num += ctbNum;
×
885
  }
886

887
  metaCloseStbCursor(pCur);
×
888
  return TSDB_CODE_SUCCESS;
×
889
}
890

891
void *vnodeGetIdx(void *pVnode) {
×
892
  if (pVnode == NULL) {
×
893
    return NULL;
×
894
  }
895

896
  return metaGetIdx(((SVnode *)pVnode)->pMeta);
×
897
}
898

899
void *vnodeGetIvtIdx(void *pVnode) {
×
900
  if (pVnode == NULL) {
×
901
    return NULL;
×
902
  }
903
  return metaGetIvtIdx(((SVnode *)pVnode)->pMeta);
×
904
}
905

906
int32_t vnodeGetTableSchema(void *pVnode, int64_t uid, STSchema **pSchema, int64_t *suid) {
×
907
  return tsdbGetTableSchema(((SVnode *)pVnode)->pMeta, uid, pSchema, suid);
×
908
}
909

910
int32_t vnodeGetDBSize(void *pVnode, SDbSizeStatisInfo *pInfo) {
×
911
  SVnode *pVnodeObj = pVnode;
×
912
  if (pVnodeObj == NULL) {
×
913
    return TSDB_CODE_VND_NOT_EXIST;
×
914
  }
915
  int32_t code = 0;
×
916
  char    path[TSDB_FILENAME_LEN] = {0};
×
917

918
  char   *dirName[] = {VNODE_TSDB_DIR, VNODE_WAL_DIR, VNODE_META_DIR, VNODE_TSDB_CACHE_DIR};
×
919
  int64_t dirSize[4];
920

921
  vnodeGetPrimaryDir(pVnodeObj->path, pVnodeObj->diskPrimary, pVnodeObj->pTfs, path, TSDB_FILENAME_LEN);
×
922
  int32_t offset = strlen(path);
×
923

924
  for (int i = 0; i < sizeof(dirName) / sizeof(dirName[0]); i++) {
×
925
    int64_t size = {0};
×
926
    (void)snprintf(path + offset, TSDB_FILENAME_LEN, "%s%s", TD_DIRSEP, dirName[i]);
×
927
    code = taosGetDirSize(path, &size);
×
928
    if (code != 0) {
×
929
      return code;
×
930
    }
931
    path[offset] = 0;
×
932
    dirSize[i] = size;
×
933
  }
934

935
  pInfo->l1Size = dirSize[0] - dirSize[3];
×
936
  pInfo->walSize = dirSize[1];
×
937
  pInfo->metaSize = dirSize[2];
×
938
  pInfo->cacheSize = dirSize[3];
×
939

940
  code = tsdbGetS3Size(pVnodeObj->pTsdb, &pInfo->s3Size);
×
941

942
  return code;
×
943
}
944

945
int32_t vnodeGetStreamProgress(SVnode *pVnode, SRpcMsg *pMsg, bool direct) {
×
946
  int32_t            code = 0;
×
947
  SStreamProgressReq req;
948
  SStreamProgressRsp rsp = {0};
×
949
  SRpcMsg            rpcMsg = {.info = pMsg->info, .code = 0};
×
950
  char              *buf = NULL;
×
951
  int32_t            rspLen = 0;
×
952
  code = tDeserializeStreamProgressReq(pMsg->pCont, pMsg->contLen, &req);
×
953

954
  if (code == TSDB_CODE_SUCCESS) {
×
955
    rsp.fetchIdx = req.fetchIdx;
×
956
    rsp.subFetchIdx = req.subFetchIdx;
×
957
    rsp.vgId = req.vgId;
×
958
    rsp.streamId = req.streamId;
×
959
    rspLen = tSerializeStreamProgressRsp(0, 0, &rsp);
×
960
    if (rspLen < 0) {
×
961
      code = terrno;
×
962
      goto _OVER;
×
963
    }
964
    if (direct) {
×
965
      buf = rpcMallocCont(rspLen);
×
966
    } else {
967
      buf = taosMemoryCalloc(1, rspLen);
×
968
    }
969
    if (!buf) {
×
970
      code = terrno;
×
971
      goto _OVER;
×
972
    }
973
  }
974

975
  if (code == TSDB_CODE_SUCCESS) {
×
976
    code = tqGetStreamExecInfo(pVnode, req.streamId, &rsp.progressDelay, &rsp.fillHisFinished);
×
977
  }
978
  if (code == TSDB_CODE_SUCCESS) {
×
979
    rspLen = tSerializeStreamProgressRsp(buf, rspLen, &rsp);
×
980
    if (rspLen < 0) {
×
981
      code = terrno;
×
982
      goto _OVER;
×
983
    }
984
    rpcMsg.pCont = buf;
×
985
    buf = NULL;
×
986
    rpcMsg.contLen = rspLen;
×
987
    rpcMsg.code = code;
×
988
    rpcMsg.msgType = pMsg->msgType;
×
989
    if (direct) {
×
990
      tmsgSendRsp(&rpcMsg);
×
991
    } else {
992
      *pMsg = rpcMsg;
×
993
    }
994
  }
995

996
_OVER:
×
997
  if (buf) {
×
998
    taosMemoryFree(buf);
×
999
  }
1000
  return code;
×
1001
}
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