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

taosdata / TDengine / #3633

11 Mar 2025 12:59PM UTC coverage: 0.0% (-60.7%) from 60.719%
#3633

push

travis-ci

web-flow
Merge pull request #30118 from taosdata/wl30

udpate ci workflow

0 of 280412 branches covered (0.0%)

Branch coverage included in aggregate %.

0 of 275582 relevant lines covered (0.0%)

0.0 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,
×
75
             pSchema->colId, pSchema->bytes, pSchema->name);
76
    }
77
  }
78
}
79

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

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

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

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

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

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

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

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

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

188
  vnodePrintTableMeta(&metaRsp);
×
189

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

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

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

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

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

228
  if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST && autoCreateCtb == 1) {
×
229
    code = TSDB_CODE_SUCCESS;
×
230
  }
231

232
  if (code) {
×
233
    qError("get table %s meta with %" PRIu8 " failed cause of %s", infoReq.tbName, infoReq.option, tstrerror(code));
×
234
  }
235

236
  if (direct) {
×
237
    tmsgSendRsp(&rpcMsg);
×
238
  } else {
239
    *pMsg = rpcMsg;
×
240
  }
241

242
  return code;
×
243
}
244

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

258
  // decode req
259
  if (tDeserializeSTableCfgReq(pMsg->pCont, pMsg->contLen, &cfgReq) != 0) {
×
260
    code = terrno;
×
261
    goto _exit;
×
262
  }
263

264
  tstrncpy(cfgRsp.tbName, cfgReq.tbName, TSDB_TABLE_NAME_LEN);
×
265
  (void)memcpy(cfgRsp.dbFName, cfgReq.dbFName, sizeof(cfgRsp.dbFName));
×
266

267
  (void)tsnprintf(tableFName, TSDB_TABLE_FNAME_LEN, "%s.%s", cfgReq.dbFName, cfgReq.tbName);
×
268
  code = vnodeValidateTableHash(pVnode, tableFName);
×
269
  if (code) {
×
270
    goto _exit;
×
271
  }
272

273
  // query meta
274
  metaReaderDoInit(&mer1, pVnode->pMeta, META_READER_LOCK);
×
275

276
  if (metaGetTableEntryByName(&mer1, cfgReq.tbName) < 0) {
×
277
    code = terrno;
×
278
    goto _exit;
×
279
  }
280

281
  cfgRsp.tableType = mer1.me.type;
×
282

283
  if (mer1.me.type == TSDB_SUPER_TABLE) {
×
284
    code = TSDB_CODE_VND_HASH_MISMATCH;
×
285
    goto _exit;
×
286
  } else if (mer1.me.type == TSDB_CHILD_TABLE) {
×
287
    metaReaderDoInit(&mer2, pVnode->pMeta, META_READER_NOLOCK);
×
288
    if (metaReaderGetTableEntryByUid(&mer2, mer1.me.ctbEntry.suid) < 0) goto _exit;
×
289

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

327
  cfgRsp.numOfTags = schemaTag.nCols;
×
328
  cfgRsp.numOfColumns = schema.nCols;
×
329
  cfgRsp.pSchemas = (SSchema *)taosMemoryMalloc(sizeof(SSchema) * (cfgRsp.numOfColumns + cfgRsp.numOfTags));
×
330
  cfgRsp.pSchemaExt = (SSchemaExt *)taosMemoryMalloc(cfgRsp.numOfColumns * sizeof(SSchemaExt));
×
331

332
  if (NULL == cfgRsp.pSchemas || NULL == cfgRsp.pSchemaExt) {
×
333
    code = terrno;
×
334
    goto _exit;
×
335
  }
336
  (void)memcpy(cfgRsp.pSchemas, schema.pSchema, sizeof(SSchema) * schema.nCols);
×
337
  if (schemaTag.nCols) {
×
338
    (void)memcpy(cfgRsp.pSchemas + schema.nCols, schemaTag.pSchema, sizeof(SSchema) * schemaTag.nCols);
×
339
  }
340

341
  // if (useCompress(cfgRsp.tableType)) {
342

343
  SMetaReader     *pReader = mer1.me.type == TSDB_CHILD_TABLE ? &mer2 : &mer1;
×
344
  SColCmprWrapper *pColCmpr = &pReader->me.colCmpr;
×
345

346
  for (int32_t i = 0; i < cfgRsp.numOfColumns; i++) {
×
347
    SColCmpr   *pCmpr = &pColCmpr->pColCmpr[i];
×
348
    SSchemaExt *pSchExt = cfgRsp.pSchemaExt + i;
×
349
    pSchExt->colId = pCmpr->id;
×
350
    pSchExt->compress = pCmpr->alg;
×
351
  }
352
  //}
353

354
  // encode and send response
355
  rspLen = tSerializeSTableCfgRsp(NULL, 0, &cfgRsp);
×
356
  if (rspLen < 0) {
×
357
    code = terrno;
×
358
    goto _exit;
×
359
  }
360

361
  if (direct) {
×
362
    pRsp = rpcMallocCont(rspLen);
×
363
  } else {
364
    pRsp = taosMemoryCalloc(1, rspLen);
×
365
  }
366

367
  if (pRsp == NULL) {
×
368
    code = terrno;
×
369
    goto _exit;
×
370
  }
371

372
  rspLen = tSerializeSTableCfgRsp(pRsp, rspLen, &cfgRsp);
×
373
  if (rspLen < 0) {
×
374
    code = terrno;
×
375
    goto _exit;
×
376
  }
377

378
_exit:
×
379
  rpcMsg.info = pMsg->info;
×
380
  rpcMsg.pCont = pRsp;
×
381
  rpcMsg.contLen = rspLen;
×
382
  rpcMsg.code = code;
×
383
  rpcMsg.msgType = pMsg->msgType;
×
384

385
  if (code) {
×
386
    qError("get table %s cfg failed cause of %s", cfgReq.tbName, tstrerror(code));
×
387
  }
388

389
  if (direct) {
×
390
    tmsgSendRsp(&rpcMsg);
×
391
  } else {
392
    *pMsg = rpcMsg;
×
393
  }
394

395
  tFreeSTableCfgRsp(&cfgRsp);
×
396
  metaReaderClear(&mer2);
×
397
  metaReaderClear(&mer1);
×
398
  return code;
×
399
}
400

401
static FORCE_INLINE void vnodeFreeSBatchRspMsg(void *p) {
402
  if (NULL == p) {
403
    return;
404
  }
405

406
  SBatchRspMsg *pRsp = (SBatchRspMsg *)p;
407
  rpcFreeCont(pRsp->msg);
408
}
409

410
int32_t vnodeGetBatchMeta(SVnode *pVnode, SRpcMsg *pMsg) {
×
411
  int32_t      code = 0;
×
412
  int32_t      rspSize = 0;
×
413
  SBatchReq    batchReq = {0};
×
414
  SBatchMsg   *req = NULL;
×
415
  SBatchRspMsg rsp = {0};
×
416
  SBatchRsp    batchRsp = {0};
×
417
  SRpcMsg      reqMsg = *pMsg;
×
418
  SRpcMsg      rspMsg = {0};
×
419
  void        *pRsp = NULL;
×
420

421
  if (tDeserializeSBatchReq(pMsg->pCont, pMsg->contLen, &batchReq)) {
×
422
    code = terrno;
×
423
    qError("tDeserializeSBatchReq failed");
×
424
    goto _exit;
×
425
  }
426

427
  int32_t msgNum = taosArrayGetSize(batchReq.pMsgs);
×
428
  if (msgNum >= MAX_META_MSG_IN_BATCH) {
×
429
    code = TSDB_CODE_INVALID_MSG;
×
430
    qError("too many msgs %d in vnode batch meta req", msgNum);
×
431
    goto _exit;
×
432
  }
433

434
  batchRsp.pRsps = taosArrayInit(msgNum, sizeof(SBatchRspMsg));
×
435
  if (NULL == batchRsp.pRsps) {
×
436
    code = terrno;
×
437
    qError("taosArrayInit %d SBatchRspMsg failed", msgNum);
×
438
    goto _exit;
×
439
  }
440

441
  for (int32_t i = 0; i < msgNum; ++i) {
×
442
    req = taosArrayGet(batchReq.pMsgs, i);
×
443
    if (req == NULL) {
×
444
      code = terrno;
×
445
      goto _exit;
×
446
    }
447

448
    reqMsg.msgType = req->msgType;
×
449
    reqMsg.pCont = req->msg;
×
450
    reqMsg.contLen = req->msgLen;
×
451

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

485
    rsp.msgIdx = req->msgIdx;
×
486
    rsp.reqType = reqMsg.msgType;
×
487
    rsp.msgLen = reqMsg.contLen;
×
488
    rsp.rspCode = reqMsg.code;
×
489
    rsp.msg = reqMsg.pCont;
×
490

491
    if (NULL == taosArrayPush(batchRsp.pRsps, &rsp)) {
×
492
      qError("taosArrayPush failed");
×
493
      code = terrno;
×
494
      goto _exit;
×
495
    }
496
  }
497

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

516
_exit:
×
517

518
  rspMsg.info = pMsg->info;
×
519
  rspMsg.pCont = pRsp;
×
520
  rspMsg.contLen = rspSize;
×
521
  rspMsg.code = code;
×
522
  rspMsg.msgType = pMsg->msgType;
×
523

524
  if (code) {
×
525
    qError("vnd get batch meta failed cause of %s", tstrerror(code));
×
526
  }
527

528
  taosArrayDestroyEx(batchReq.pMsgs, tFreeSBatchReqMsg);
×
529
  taosArrayDestroyEx(batchRsp.pRsps, tFreeSBatchRspMsg);
×
530

531
  tmsgSendRsp(&rspMsg);
×
532

533
  return code;
×
534
}
535

536
#define VNODE_DO_META_QUERY(pVnode, cmd)                 \
537
  do {                                                   \
538
    (void)taosThreadRwlockRdlock(&(pVnode)->metaRWLock); \
539
    cmd;                                                 \
540
    (void)taosThreadRwlockUnlock(&(pVnode)->metaRWLock); \
541
  } while (0)
542

543
int32_t vnodeGetLoad(SVnode *pVnode, SVnodeLoad *pLoad) {
×
544
  SSyncState state = syncGetState(pVnode->sync);
×
545
  pLoad->syncAppliedIndex = pVnode->state.applied;
×
546
  syncGetCommitIndex(pVnode->sync, &pLoad->syncCommitIndex);
×
547

548
  pLoad->vgId = TD_VID(pVnode);
×
549
  pLoad->syncState = state.state;
×
550
  pLoad->syncRestore = state.restored;
×
551
  pLoad->syncTerm = state.term;
×
552
  pLoad->roleTimeMs = state.roleTimeMs;
×
553
  pLoad->startTimeMs = state.startTimeMs;
×
554
  pLoad->syncCanRead = state.canRead;
×
555
  pLoad->learnerProgress = state.progress;
×
556
  pLoad->cacheUsage = tsdbCacheGetUsage(pVnode);
×
557
  pLoad->numOfCachedTables = tsdbCacheGetElems(pVnode);
×
558
  VNODE_DO_META_QUERY(pVnode, pLoad->numOfTables = metaGetTbNum(pVnode->pMeta));
×
559
  VNODE_DO_META_QUERY(pVnode, pLoad->numOfTimeSeries = metaGetTimeSeriesNum(pVnode->pMeta, 1));
×
560
  pLoad->totalStorage = (int64_t)3 * 1073741824;
×
561
  pLoad->compStorage = (int64_t)2 * 1073741824;
×
562
  pLoad->pointsWritten = 100;
×
563
  pLoad->numOfSelectReqs = 1;
×
564
  pLoad->numOfInsertReqs = atomic_load_64(&pVnode->statis.nInsert);
×
565
  pLoad->numOfInsertSuccessReqs = atomic_load_64(&pVnode->statis.nInsertSuccess);
×
566
  pLoad->numOfBatchInsertReqs = atomic_load_64(&pVnode->statis.nBatchInsert);
×
567
  pLoad->numOfBatchInsertSuccessReqs = atomic_load_64(&pVnode->statis.nBatchInsertSuccess);
×
568
  return 0;
×
569
}
570

571
int32_t vnodeGetLoadLite(SVnode *pVnode, SVnodeLoadLite *pLoad) {
×
572
  SSyncState syncState = syncGetState(pVnode->sync);
×
573
  if (syncState.state == TAOS_SYNC_STATE_LEADER || syncState.state == TAOS_SYNC_STATE_ASSIGNED_LEADER) {
×
574
    pLoad->vgId = TD_VID(pVnode);
×
575
    pLoad->nTimeSeries = metaGetTimeSeriesNum(pVnode->pMeta, 1);
×
576
    return 0;
×
577
  }
578
  return -1;
×
579
}
580
/**
581
 * @brief Reset the statistics value by monitor interval
582
 *
583
 * @param pVnode
584
 * @param pLoad
585
 */
586
void vnodeResetLoad(SVnode *pVnode, SVnodeLoad *pLoad) {
×
587
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nInsert, pLoad->numOfInsertReqs, 64, "nInsert");
×
588
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nInsertSuccess, pLoad->numOfInsertSuccessReqs, 64, "nInsertSuccess");
×
589
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nBatchInsert, pLoad->numOfBatchInsertReqs, 64, "nBatchInsert");
×
590
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nBatchInsertSuccess, pLoad->numOfBatchInsertSuccessReqs, 64,
×
591
                            "nBatchInsertSuccess");
592
}
×
593

594
void vnodeGetInfo(void *pVnode, const char **dbname, int32_t *vgId, int64_t *numOfTables, int64_t *numOfNormalTables) {
×
595
  SVnode    *pVnodeObj = pVnode;
×
596
  SVnodeCfg *pConf = &pVnodeObj->config;
×
597

598
  if (dbname) {
×
599
    *dbname = pConf->dbname;
×
600
  }
601

602
  if (vgId) {
×
603
    *vgId = TD_VID(pVnodeObj);
×
604
  }
605

606
  if (numOfTables) {
×
607
    *numOfTables = pConf->vndStats.numOfNTables + pConf->vndStats.numOfCTables;
×
608
  }
609

610
  if (numOfNormalTables) {
×
611
    *numOfNormalTables = pConf->vndStats.numOfNTables;
×
612
  }
613
}
×
614

615
int32_t vnodeGetTableList(void *pVnode, int8_t type, SArray *pList) {
×
616
  if (type == TSDB_SUPER_TABLE) {
×
617
    return vnodeGetStbIdList(pVnode, 0, pList);
×
618
  } else {
619
    return TSDB_CODE_INVALID_PARA;
×
620
  }
621
}
622

623
int32_t vnodeGetAllTableList(SVnode *pVnode, uint64_t uid, SArray *list) {
×
624
  int32_t      code = TSDB_CODE_SUCCESS;
×
625
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, uid, 1);
×
626
  if (NULL == pCur) {
×
627
    qError("vnode get all table list failed");
×
628
    return terrno;
×
629
  }
630

631
  while (1) {
×
632
    tb_uid_t id = metaCtbCursorNext(pCur);
×
633
    if (id == 0) {
×
634
      break;
×
635
    }
636

637
    STableKeyInfo info = {uid = id};
×
638
    if (NULL == taosArrayPush(list, &info)) {
×
639
      qError("taosArrayPush failed");
×
640
      code = terrno;
×
641
      goto _exit;
×
642
    }
643
  }
644
_exit:
×
645
  metaCloseCtbCursor(pCur);
×
646
  return code;
×
647
}
648

649
int32_t vnodeGetCtbIdListByFilter(SVnode *pVnode, int64_t suid, SArray *list, bool (*filter)(void *arg), void *arg) {
×
650
  return 0;
×
651
}
652

653
int32_t vnodeGetCtbIdList(void *pVnode, int64_t suid, SArray *list) {
×
654
  int32_t      code = TSDB_CODE_SUCCESS;
×
655
  SVnode      *pVnodeObj = pVnode;
×
656
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnodeObj, suid, 1);
×
657
  if (NULL == pCur) {
×
658
    qError("vnode get all table list failed");
×
659
    return terrno;
×
660
  }
661

662
  while (1) {
×
663
    tb_uid_t id = metaCtbCursorNext(pCur);
×
664
    if (id == 0) {
×
665
      break;
×
666
    }
667

668
    if (NULL == taosArrayPush(list, &id)) {
×
669
      qError("taosArrayPush failed");
×
670
      code = terrno;
×
671
      goto _exit;
×
672
    }
673
  }
674

675
_exit:
×
676
  metaCloseCtbCursor(pCur);
×
677
  return code;
×
678
}
679

680
int32_t vnodeGetStbIdList(SVnode *pVnode, int64_t suid, SArray *list) {
×
681
  int32_t      code = TSDB_CODE_SUCCESS;
×
682
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, suid);
×
683
  if (!pCur) {
×
684
    return TSDB_CODE_OUT_OF_MEMORY;
×
685
  }
686

687
  while (1) {
×
688
    tb_uid_t id = metaStbCursorNext(pCur);
×
689
    if (id == 0) {
×
690
      break;
×
691
    }
692

693
    if (NULL == taosArrayPush(list, &id)) {
×
694
      qError("taosArrayPush failed");
×
695
      code = terrno;
×
696
      goto _exit;
×
697
    }
698
  }
699

700
_exit:
×
701
  metaCloseStbCursor(pCur);
×
702
  return code;
×
703
}
704

705
int32_t vnodeGetStbIdListByFilter(SVnode *pVnode, int64_t suid, SArray *list, bool (*filter)(void *arg, void *arg1),
×
706
                                  void *arg) {
707
  int32_t      code = TSDB_CODE_SUCCESS;
×
708
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, suid);
×
709
  if (!pCur) {
×
710
    return terrno;
×
711
  }
712

713
  while (1) {
×
714
    tb_uid_t id = metaStbCursorNext(pCur);
×
715
    if (id == 0) {
×
716
      break;
×
717
    }
718

719
    if ((*filter) && (*filter)(arg, &id)) {
×
720
      continue;
×
721
    }
722

723
    if (NULL == taosArrayPush(list, &id)) {
×
724
      qError("taosArrayPush failed");
×
725
      code = terrno;
×
726
      goto _exit;
×
727
    }
728
  }
729

730
_exit:
×
731
  metaCloseStbCursor(pCur);
×
732
  return code;
×
733
}
734

735
int32_t vnodeGetCtbNum(SVnode *pVnode, int64_t suid, int64_t *num) {
×
736
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, suid, 0);
×
737
  if (!pCur) {
×
738
    return terrno;
×
739
  }
740

741
  *num = 0;
×
742
  while (1) {
×
743
    tb_uid_t id = metaCtbCursorNext(pCur);
×
744
    if (id == 0) {
×
745
      break;
×
746
    }
747

748
    ++(*num);
×
749
  }
750

751
  metaCloseCtbCursor(pCur);
×
752
  return TSDB_CODE_SUCCESS;
×
753
}
754

755
int32_t vnodeGetStbColumnNum(SVnode *pVnode, tb_uid_t suid, int *num) {
×
756
  SSchemaWrapper *pSW = metaGetTableSchema(pVnode->pMeta, suid, -1, 0);
×
757
  if (pSW) {
×
758
    *num = pSW->nCols;
×
759
    tDeleteSchemaWrapper(pSW);
760
  } else {
761
    *num = 2;
×
762
  }
763

764
  return TSDB_CODE_SUCCESS;
×
765
}
766

767
#ifdef TD_ENTERPRISE
768
const char *tkLogStb[] = {"cluster_info",
769
                          "data_dir",
770
                          "dnodes_info",
771
                          "d_info",
772
                          "grants_info",
773
                          "keeper_monitor",
774
                          "logs",
775
                          "log_dir",
776
                          "log_summary",
777
                          "m_info",
778
                          "taosadapter_restful_http_request_fail",
779
                          "taosadapter_restful_http_request_in_flight",
780
                          "taosadapter_restful_http_request_summary_milliseconds",
781
                          "taosadapter_restful_http_request_total",
782
                          "taosadapter_system_cpu_percent",
783
                          "taosadapter_system_mem_percent",
784
                          "temp_dir",
785
                          "vgroups_info",
786
                          "vnodes_role"};
787
const char *tkAuditStb[] = {"operations"};
788
const int   tkLogStbNum = ARRAY_SIZE(tkLogStb);
789
const int   tkAuditStbNum = ARRAY_SIZE(tkAuditStb);
790

791
// exclude stbs of taoskeeper log
792
static int32_t vnodeGetTimeSeriesBlackList(SVnode *pVnode, int32_t *tbSize) {
×
793
  int32_t      code = TSDB_CODE_SUCCESS;
×
794
  int32_t      tbNum = 0;
×
795
  const char **pTbArr = NULL;
×
796
  const char  *dbName = NULL;
×
797
  *tbSize = 0;
×
798

799
  if (!(dbName = strchr(pVnode->config.dbname, '.'))) return 0;
×
800
  if (0 == strncmp(++dbName, "log", TSDB_DB_NAME_LEN)) {
×
801
    tbNum = tkLogStbNum;
×
802
    pTbArr = (const char **)&tkLogStb;
×
803
  } else if (0 == strncmp(dbName, "audit", TSDB_DB_NAME_LEN)) {
×
804
    tbNum = tkAuditStbNum;
×
805
    pTbArr = (const char **)&tkAuditStb;
×
806
  }
807
  if (tbNum && pTbArr) {
×
808
    *tbSize = metaSizeOfTbFilterCache(pVnode->pMeta, 0);
×
809
    if (*tbSize < tbNum) {
×
810
      for (int32_t i = 0; i < tbNum; ++i) {
×
811
        tb_uid_t suid = metaGetTableEntryUidByName(pVnode->pMeta, pTbArr[i]);
×
812
        if (suid != 0) {
×
813
          code = metaPutTbToFilterCache(pVnode->pMeta, &suid, 0);
×
814
          if (TSDB_CODE_SUCCESS != code) {
×
815
            return code;
×
816
          }
817
        }
818
      }
819
      *tbSize = metaSizeOfTbFilterCache(pVnode->pMeta, 0);
×
820
    }
821
  }
822

823
  return code;
×
824
}
825
#endif
826

827
static bool vnodeTimeSeriesFilter(void *arg1, void *arg2) {
×
828
  SVnode *pVnode = (SVnode *)arg1;
×
829

830
  if (metaTbInFilterCache(pVnode->pMeta, arg2, 0)) {
×
831
    return true;
×
832
  }
833
  return false;
×
834
}
835

836
int32_t vnodeGetTimeSeriesNum(SVnode *pVnode, int64_t *num) {
×
837
  SArray *suidList = NULL;
×
838

839
  if (!(suidList = taosArrayInit(1, sizeof(tb_uid_t)))) {
×
840
    return terrno;
×
841
  }
842

843
  int32_t tbFilterSize = 0;
×
844
  int32_t code = TSDB_CODE_SUCCESS;
×
845
#ifdef TD_ENTERPRISE
846
  code = vnodeGetTimeSeriesBlackList(pVnode, &tbFilterSize);
×
847
  if (TSDB_CODE_SUCCESS != code) {
×
848
    goto _exit;
×
849
  }
850
#endif
851

852
  if ((!tbFilterSize && vnodeGetStbIdList(pVnode, 0, suidList) < 0) ||
×
853
      (tbFilterSize && vnodeGetStbIdListByFilter(pVnode, 0, suidList, vnodeTimeSeriesFilter, pVnode) < 0)) {
×
854
    qError("vgId:%d, failed to get stb id list error: %s", TD_VID(pVnode), terrstr());
×
855
    taosArrayDestroy(suidList);
×
856
    return terrno;
×
857
  }
858

859
  *num = 0;
×
860
  int64_t arrSize = taosArrayGetSize(suidList);
×
861
  for (int64_t i = 0; i < arrSize; ++i) {
×
862
    tb_uid_t suid = *(tb_uid_t *)taosArrayGet(suidList, i);
×
863

864
    int64_t ctbNum = 0;
×
865
    int32_t numOfCols = 0;
×
866
    code = metaGetStbStats(pVnode, suid, &ctbNum, &numOfCols);
×
867
    if (TSDB_CODE_SUCCESS != code) {
×
868
      goto _exit;
×
869
    }
870
    *num += ctbNum * (numOfCols - 1);
×
871
  }
872

873
_exit:
×
874
  taosArrayDestroy(suidList);
×
875
  return TSDB_CODE_SUCCESS;
×
876
}
877

878
int32_t vnodeGetAllCtbNum(SVnode *pVnode, int64_t *num) {
×
879
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, 0);
×
880
  if (!pCur) {
×
881
    return terrno;
×
882
  }
883

884
  *num = 0;
×
885
  while (1) {
×
886
    tb_uid_t id = metaStbCursorNext(pCur);
×
887
    if (id == 0) {
×
888
      break;
×
889
    }
890

891
    int64_t ctbNum = 0;
×
892
    int32_t code = vnodeGetCtbNum(pVnode, id, &ctbNum);
×
893
    if (TSDB_CODE_SUCCESS != code) {
×
894
      metaCloseStbCursor(pCur);
×
895
      return code;
×
896
    }
897

898
    *num += ctbNum;
×
899
  }
900

901
  metaCloseStbCursor(pCur);
×
902
  return TSDB_CODE_SUCCESS;
×
903
}
904

905
void *vnodeGetIdx(void *pVnode) {
×
906
  if (pVnode == NULL) {
×
907
    return NULL;
×
908
  }
909

910
  return metaGetIdx(((SVnode *)pVnode)->pMeta);
×
911
}
912

913
void *vnodeGetIvtIdx(void *pVnode) {
×
914
  if (pVnode == NULL) {
×
915
    return NULL;
×
916
  }
917
  return metaGetIvtIdx(((SVnode *)pVnode)->pMeta);
×
918
}
919

920
int32_t vnodeGetTableSchema(void *pVnode, int64_t uid, STSchema **pSchema, int64_t *suid) {
×
921
  return tsdbGetTableSchema(((SVnode *)pVnode)->pMeta, uid, pSchema, suid);
×
922
}
923

924
int32_t vnodeGetDBSize(void *pVnode, SDbSizeStatisInfo *pInfo) {
×
925
  SVnode *pVnodeObj = pVnode;
×
926
  if (pVnodeObj == NULL) {
×
927
    return TSDB_CODE_VND_NOT_EXIST;
×
928
  }
929
  int32_t code = 0;
×
930
  char    path[TSDB_FILENAME_LEN] = {0};
×
931

932
  char   *dirName[] = {VNODE_TSDB_DIR, VNODE_WAL_DIR, VNODE_META_DIR, VNODE_TSDB_CACHE_DIR};
×
933
  int64_t dirSize[4];
934

935
  vnodeGetPrimaryDir(pVnodeObj->path, pVnodeObj->diskPrimary, pVnodeObj->pTfs, path, TSDB_FILENAME_LEN);
×
936
  int32_t offset = strlen(path);
×
937

938
  for (int i = 0; i < sizeof(dirName) / sizeof(dirName[0]); i++) {
×
939
    int64_t size = {0};
×
940
    (void)snprintf(path + offset, TSDB_FILENAME_LEN, "%s%s", TD_DIRSEP, dirName[i]);
×
941
    code = taosGetDirSize(path, &size);
×
942
    if (code != 0) {
×
943
      return code;
×
944
    }
945
    path[offset] = 0;
×
946
    dirSize[i] = size;
×
947
  }
948

949
  pInfo->l1Size = dirSize[0] - dirSize[3];
×
950
  pInfo->walSize = dirSize[1];
×
951
  pInfo->metaSize = dirSize[2];
×
952
  pInfo->cacheSize = dirSize[3];
×
953

954
  code = tsdbGetS3Size(pVnodeObj->pTsdb, &pInfo->s3Size);
×
955

956
  return code;
×
957
}
958

959
int32_t vnodeGetStreamProgress(SVnode *pVnode, SRpcMsg *pMsg, bool direct) {
×
960
  int32_t            code = 0;
×
961
  SStreamProgressReq req;
962
  SStreamProgressRsp rsp = {0};
×
963
  SRpcMsg            rpcMsg = {.info = pMsg->info, .code = 0};
×
964
  char              *buf = NULL;
×
965
  int32_t            rspLen = 0;
×
966
  code = tDeserializeStreamProgressReq(pMsg->pCont, pMsg->contLen, &req);
×
967

968
  if (code == TSDB_CODE_SUCCESS) {
×
969
    rsp.fetchIdx = req.fetchIdx;
×
970
    rsp.subFetchIdx = req.subFetchIdx;
×
971
    rsp.vgId = req.vgId;
×
972
    rsp.streamId = req.streamId;
×
973
    rspLen = tSerializeStreamProgressRsp(0, 0, &rsp);
×
974
    if (rspLen < 0) {
×
975
      code = terrno;
×
976
      goto _OVER;
×
977
    }
978
    if (direct) {
×
979
      buf = rpcMallocCont(rspLen);
×
980
    } else {
981
      buf = taosMemoryCalloc(1, rspLen);
×
982
    }
983
    if (!buf) {
×
984
      code = terrno;
×
985
      goto _OVER;
×
986
    }
987
  }
988

989
  if (code == TSDB_CODE_SUCCESS) {
×
990
    code = tqGetStreamExecInfo(pVnode, req.streamId, &rsp.progressDelay, &rsp.fillHisFinished);
×
991
  }
992
  if (code == TSDB_CODE_SUCCESS) {
×
993
    rspLen = tSerializeStreamProgressRsp(buf, rspLen, &rsp);
×
994
    if (rspLen < 0) {
×
995
      code = terrno;
×
996
      goto _OVER;
×
997
    }
998
    rpcMsg.pCont = buf;
×
999
    buf = NULL;
×
1000
    rpcMsg.contLen = rspLen;
×
1001
    rpcMsg.code = code;
×
1002
    rpcMsg.msgType = pMsg->msgType;
×
1003
    if (direct) {
×
1004
      tmsgSendRsp(&rpcMsg);
×
1005
    } else {
1006
      *pMsg = rpcMsg;
×
1007
    }
1008
  }
1009

1010
_OVER:
×
1011
  if (buf) {
×
1012
    taosMemoryFree(buf);
×
1013
  }
1014
  return code;
×
1015
}
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