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

taosdata / TDengine / #3814

01 Apr 2025 05:36AM UTC coverage: 30.169% (-3.6%) from 33.798%
#3814

push

travis-ci

happyguoxy
test:add build cmake

129680 of 592945 branches covered (21.87%)

Branch coverage included in aggregate %.

196213 of 487270 relevant lines covered (40.27%)

555639.42 hits per line

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

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

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

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

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

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

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

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

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

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

138
  // query meta
139
  metaReaderDoInit(&mer1, pVnode->pMeta, META_READER_LOCK);
41✔
140
  if (reqTbUid) {
41!
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) {
41✔
157
    code = terrno;
29✔
158
    goto _exit3;
29✔
159
  }
160

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

165
  switch (mer1.me.type) {
12!
166
    case TSDB_SUPER_TABLE: {
12✔
167
      (void)strcpy(metaRsp.stbName, mer1.me.name);
12✔
168
      schema = mer1.me.stbEntry.schemaRow;
12✔
169
      schemaTag = mer1.me.stbEntry.schemaTag;
12✔
170
      metaRsp.suid = mer1.me.uid;
12✔
171
      break;
12✔
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;
12✔
196
  metaRsp.numOfColumns = schema.nCols;
12✔
197
  metaRsp.precision = pVnode->config.tsdbCfg.precision;
12✔
198
  metaRsp.sversion = schema.version;
12✔
199
  metaRsp.tversion = schemaTag.version;
12✔
200
  metaRsp.pSchemas = (SSchema *)taosMemoryMalloc(sizeof(SSchema) * (metaRsp.numOfColumns + metaRsp.numOfTags));
12!
201
  metaRsp.pSchemaExt = (SSchemaExt *)taosMemoryCalloc(metaRsp.numOfColumns, sizeof(SSchemaExt));
12!
202
  if (NULL == metaRsp.pSchemas || NULL == metaRsp.pSchemaExt) {
12!
203
    code = terrno;
×
204
    goto _exit;
×
205
  }
206
  (void)memcpy(metaRsp.pSchemas, schema.pSchema, sizeof(SSchema) * schema.nCols);
12✔
207
  if (schemaTag.nCols) {
12!
208
    (void)memcpy(metaRsp.pSchemas + schema.nCols, schemaTag.pSchema, sizeof(SSchema) * schemaTag.nCols);
12✔
209
  }
210
  if (metaRsp.pSchemaExt) {
12!
211
    SMetaReader *pReader = mer1.me.type == TSDB_CHILD_TABLE ? &mer2 : &mer1;
12!
212
    code = fillTableColCmpr(pReader, metaRsp.pSchemaExt, metaRsp.numOfColumns);
12✔
213
    if (code < 0) {
12!
214
      goto _exit;
×
215
    }
216
    for (int32_t i = 0; i < metaRsp.numOfColumns && pReader->me.pExtSchemas; i++) {
12!
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)) {
12!
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;
12✔
234
    metaRsp.numOfColRefs = 0;
12✔
235
  }
236

237
  vnodePrintTableMeta(&metaRsp);
12✔
238

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

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

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

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

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

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

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

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

292
  return code;
41✔
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) {
31✔
481
  int32_t      code = 0;
31✔
482
  int32_t      rspSize = 0;
31✔
483
  SBatchReq    batchReq = {0};
31✔
484
  SBatchMsg   *req = NULL;
31✔
485
  SBatchRspMsg rsp = {0};
31✔
486
  SBatchRsp    batchRsp = {0};
31✔
487
  SRpcMsg      reqMsg = *pMsg;
31✔
488
  SRpcMsg      rspMsg = {0};
31✔
489
  void        *pRsp = NULL;
31✔
490

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

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

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

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

522
    switch (req->msgType) {
41!
523
      case TDMT_VND_TABLE_META:
41✔
524
        // error code has been set into reqMsg, no need to handle it here.
525
        if (TSDB_CODE_SUCCESS != vnodeGetTableMeta(pVnode, &reqMsg, false)) {
41✔
526
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
29!
527
        }
528
        break;
41✔
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
      case TDMT_VND_VSTB_REF_DBS:
×
554
        // error code has been set into reqMsg, no need to handle it here.
555
        if (TSDB_CODE_SUCCESS != vnodeGetVStbRefDbs(pVnode, &reqMsg)) {
×
556
          qWarn("vnodeGetVStbRefDbs failed, msgType:%d", req->msgType);
×
557
        }
558
        break;
×
559
      default:
×
560
        qError("invalid req msgType %d", req->msgType);
×
561
        reqMsg.code = TSDB_CODE_INVALID_MSG;
×
562
        reqMsg.pCont = NULL;
×
563
        reqMsg.contLen = 0;
×
564
        break;
×
565
    }
566

567
    rsp.msgIdx = req->msgIdx;
41✔
568
    rsp.reqType = reqMsg.msgType;
41✔
569
    rsp.msgLen = reqMsg.contLen;
41✔
570
    rsp.rspCode = reqMsg.code;
41✔
571
    rsp.msg = reqMsg.pCont;
41✔
572

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

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

598
_exit:
31✔
599

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

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

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

613
  tmsgSendRsp(&rspMsg);
31✔
614

615
  return code;
31✔
616
}
617

618
#define VNODE_DO_META_QUERY(pVnode, cmd)                 \
619
  do {                                                   \
620
    (void)taosThreadRwlockRdlock(&(pVnode)->metaRWLock); \
621
    cmd;                                                 \
622
    (void)taosThreadRwlockUnlock(&(pVnode)->metaRWLock); \
623
  } while (0)
624

625
int32_t vnodeReadVSubtables(SReadHandle* pHandle, int64_t suid, SArray** ppRes) {
×
626
  int32_t                    code = TSDB_CODE_SUCCESS;
×
627
  int32_t                    line = 0;
×
628
  SMetaReader                mr = {0};
×
629
  bool                       readerInit = false;
×
630
  SVCTableRefCols*           pTb = NULL;
×
631
  int32_t                    refColsNum = 0;
×
632
  char                       tbFName[TSDB_TABLE_FNAME_LEN];
633
  SSHashObj*                 pSrcTbls = NULL;
×
634

635
  SArray *pList = taosArrayInit(10, sizeof(uint64_t));
×
636
  QUERY_CHECK_NULL(pList, code, line, _return, terrno);
×
637

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

640
  size_t num = taosArrayGetSize(pList);
×
641
  *ppRes = taosArrayInit(num, POINTER_BYTES);
×
642
  QUERY_CHECK_NULL(*ppRes, code, line, _return, terrno);
×
643
  pSrcTbls = tSimpleHashInit(10, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
×
644
  QUERY_CHECK_NULL(pSrcTbls, code, line, _return, terrno);
×
645

646
  for (int32_t i = 0; i < num; ++i) {
×
647
    uint64_t* id = taosArrayGet(pList, i);
×
648
    QUERY_CHECK_NULL(id, code, line, _return, terrno);
×
649
    pHandle->api.metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pHandle->api.metaFn);
×
650
    QUERY_CHECK_CODE(pHandle->api.metaReaderFn.getTableEntryByUid(&mr, *id), line, _return);
×
651
    readerInit = true;
×
652

653
    refColsNum = 0;
×
654
    for (int32_t j = 0; j < mr.me.colRef.nCols; j++) {
×
655
      if (mr.me.colRef.pColRef[j].hasRef) {
×
656
        refColsNum++;
×
657
      }
658
    }
659

660
    if (refColsNum <= 0) {
×
661
      pHandle->api.metaReaderFn.clearReader(&mr);
×
662
      readerInit = false;
×
663
      continue;
×
664
    }
665

666
    pTb = taosMemoryCalloc(1, refColsNum * sizeof(SRefColInfo) + sizeof(*pTb));
×
667
    QUERY_CHECK_NULL(pTb, code, line, _return, terrno);
×
668

669
    pTb->uid = mr.me.uid;
×
670
    pTb->numOfColRefs = refColsNum;
×
671
    pTb->refCols = (SRefColInfo*)(pTb + 1);
×
672

673
    refColsNum = 0;
×
674
    tSimpleHashClear(pSrcTbls);
×
675
    for (int32_t j = 0; j < mr.me.colRef.nCols; j++) {
×
676
      if (!mr.me.colRef.pColRef[j].hasRef) {
×
677
        continue;
×
678
      }
679

680
      pTb->refCols[refColsNum].colId = mr.me.colRef.pColRef[j].id;
×
681
      tstrncpy(pTb->refCols[refColsNum].refColName, mr.me.colRef.pColRef[j].refColName, TSDB_COL_NAME_LEN);
×
682
      tstrncpy(pTb->refCols[refColsNum].refTableName, mr.me.colRef.pColRef[j].refTableName, TSDB_TABLE_NAME_LEN);
×
683
      tstrncpy(pTb->refCols[refColsNum].refDbName, mr.me.colRef.pColRef[j].refDbName, TSDB_DB_NAME_LEN);
×
684

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

687
      if (NULL == tSimpleHashGet(pSrcTbls, tbFName, strlen(tbFName))) {
×
688
        QUERY_CHECK_CODE(tSimpleHashPut(pSrcTbls, tbFName, strlen(tbFName), &code, sizeof(code)), line, _return);
×
689
      }
690

691
      refColsNum++;
×
692
    }
693

694
    pTb->numOfSrcTbls = tSimpleHashGetSize(pSrcTbls);
×
695
    QUERY_CHECK_NULL(taosArrayPush(*ppRes, &pTb), code, line, _return, terrno);
×
696
    pTb = NULL;
×
697

698
    pHandle->api.metaReaderFn.clearReader(&mr);
×
699
    readerInit = false;
×
700
  }
701

702
_return:
×
703

704
  if (readerInit) {
×
705
    pHandle->api.metaReaderFn.clearReader(&mr);
×
706
  }
707

708
  taosArrayDestroy(pList);
×
709
  taosMemoryFree(pTb);
×
710
  tSimpleHashCleanup(pSrcTbls);
×
711

712
  if (code) {
×
713
    qError("%s failed since %s", __func__, tstrerror(code));
×
714
  }
715
  return code;
×
716
}
717

718
int32_t vnodeReadVStbRefDbs(SReadHandle* pHandle, int64_t suid, SArray** ppRes) {
×
719
  int32_t                    code = TSDB_CODE_SUCCESS;
×
720
  int32_t                    line = 0;
×
721
  SMetaReader                mr = {0};
×
722
  bool                       readerInit = false;
×
723
  SSHashObj*                 pDbNameHash = NULL;
×
724
  SArray*                    pList = NULL;
×
725

726
  pList = taosArrayInit(10, sizeof(uint64_t));
×
727
  QUERY_CHECK_NULL(pList, code, line, _return, terrno);
×
728

729
  *ppRes = taosArrayInit(10, POINTER_BYTES);
×
730
  QUERY_CHECK_NULL(*ppRes, code, line, _return, terrno)
×
731
  
732
  // lookup in cache
733
  code = pHandle->api.metaFn.metaGetCachedRefDbs(pHandle->vnode, suid, *ppRes);
×
734
  QUERY_CHECK_CODE(code, line, _return);
×
735

736
  if (taosArrayGetSize(*ppRes) > 0) {
×
737
    // found in cache
738
    goto _return;
×
739
  } else {
740
    code = pHandle->api.metaFn.getChildTableList(pHandle->vnode, suid, pList);
×
741
    QUERY_CHECK_CODE(code, line, _return);
×
742

743
    size_t num = taosArrayGetSize(pList);
×
744
    pDbNameHash = tSimpleHashInit(10, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
×
745
    QUERY_CHECK_NULL(pDbNameHash, code, line, _return, terrno);
×
746

747
    for (int32_t i = 0; i < num; ++i) {
×
748
      uint64_t* id = taosArrayGet(pList, i);
×
749
      QUERY_CHECK_NULL(id, code, line, _return, terrno);
×
750

751
      pHandle->api.metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pHandle->api.metaFn);
×
752
      readerInit = true;
×
753

754
      code = pHandle->api.metaReaderFn.getTableEntryByUid(&mr, *id);
×
755
      QUERY_CHECK_CODE(code, line, _return);
×
756

757
      for (int32_t j = 0; j < mr.me.colRef.nCols; j++) {
×
758
        if (mr.me.colRef.pColRef[j].hasRef) {
×
759
          if (NULL == tSimpleHashGet(pDbNameHash, mr.me.colRef.pColRef[j].refDbName, strlen(mr.me.colRef.pColRef[j].refDbName))) {
×
760
            char *refDbName = taosStrdup(mr.me.colRef.pColRef[j].refDbName);
×
761
            QUERY_CHECK_NULL(refDbName, code, line, _return, terrno);
×
762

763
            QUERY_CHECK_NULL(taosArrayPush(*ppRes, &refDbName), code, line, _return, terrno);
×
764

765
            code = tSimpleHashPut(pDbNameHash, refDbName, strlen(refDbName), NULL, 0);
×
766
            QUERY_CHECK_CODE(code, line, _return);
×
767
          }
768
        }
769
      }
770

771
      pHandle->api.metaReaderFn.clearReader(&mr);
×
772
      readerInit = false;
×
773
    }
774

775
    code = pHandle->api.metaFn.metaPutRefDbsToCache(pHandle->vnode, suid, *ppRes);
×
776
    QUERY_CHECK_CODE(code, line, _return);
×
777
  }
778

779
_return:
×
780

781
  if (readerInit) {
×
782
    pHandle->api.metaReaderFn.clearReader(&mr);
×
783
  }
784

785
  taosArrayDestroy(pList);
×
786
  tSimpleHashCleanup(pDbNameHash);
×
787

788
  if (code) {
×
789
    qError("%s failed since %s", __func__, tstrerror(code));
×
790
  }
791
  return code;
×
792
}
793

794
int32_t vnodeGetVSubtablesMeta(SVnode *pVnode, SRpcMsg *pMsg) {
×
795
  int32_t        code = 0;
×
796
  int32_t        rspSize = 0;
×
797
  SVSubTablesReq req = {0};
×
798
  SVSubTablesRsp rsp = {0};
×
799
  SRpcMsg      rspMsg = {0};
×
800
  void        *pRsp = NULL;
×
801
  int32_t      line = 0;
×
802

803
  if (tDeserializeSVSubTablesReq(pMsg->pCont, pMsg->contLen, &req)) {
×
804
    code = terrno;
×
805
    qError("tDeserializeSVSubTablesReq failed");
×
806
    goto _return;
×
807
  }
808

809
  SReadHandle handle = {.vnode = pVnode};
×
810
  initStorageAPI(&handle.api);
×
811

812
  QUERY_CHECK_CODE(vnodeReadVSubtables(&handle, req.suid, &rsp.pTables), line, _return);
×
813
  rsp.vgId = TD_VID(pVnode);
×
814

815
  rspSize = tSerializeSVSubTablesRsp(NULL, 0, &rsp);
×
816
  if (rspSize < 0) {
×
817
    code = rspSize;
×
818
    qError("tSerializeSVSubTablesRsp failed, error:%d", rspSize);
×
819
    goto _return;
×
820
  }
821
  pRsp = taosMemoryCalloc(1, rspSize);
×
822
  if (pRsp == NULL) {
×
823
    code = terrno;
×
824
    qError("rpcMallocCont %d failed, error:%d", rspSize, terrno);
×
825
    goto _return;
×
826
  }
827
  rspSize = tSerializeSVSubTablesRsp(pRsp, rspSize, &rsp);
×
828
  if (rspSize < 0) {
×
829
    code = rspSize;
×
830
    qError("tSerializeSVSubTablesRsp failed, error:%d", rspSize);
×
831
    goto _return;
×
832
  }
833

834
_return:
×
835

836
  rspMsg.info = pMsg->info;
×
837
  rspMsg.pCont = pRsp;
×
838
  rspMsg.contLen = rspSize;
×
839
  rspMsg.code = code;
×
840
  rspMsg.msgType = pMsg->msgType;
×
841

842
  if (code) {
×
843
    qError("vnd get virtual subtables failed cause of %s", tstrerror(code));
×
844
  }
845

846
  *pMsg = rspMsg;
×
847
  
848
  tDestroySVSubTablesRsp(&rsp);
×
849

850
  //tmsgSendRsp(&rspMsg);
851

852
  return code;
×
853
}
854

855
int32_t vnodeGetVStbRefDbs(SVnode *pVnode, SRpcMsg *pMsg) {
×
856
  int32_t        code = 0;
×
857
  int32_t        rspSize = 0;
×
858
  SVStbRefDbsReq req = {0};
×
859
  SVStbRefDbsRsp rsp = {0};
×
860
  SRpcMsg        rspMsg = {0};
×
861
  void          *pRsp = NULL;
×
862
  int32_t        line = 0;
×
863

864
  if (tDeserializeSVStbRefDbsReq(pMsg->pCont, pMsg->contLen, &req)) {
×
865
    code = terrno;
×
866
    qError("tDeserializeSVSubTablesReq failed");
×
867
    goto _return;
×
868
  }
869

870
  SReadHandle handle = {.vnode = pVnode};
×
871
  initStorageAPI(&handle.api);
×
872

873
  code = vnodeReadVStbRefDbs(&handle, req.suid, &rsp.pDbs);
×
874
  QUERY_CHECK_CODE(code, line, _return);
×
875
  rsp.vgId = TD_VID(pVnode);
×
876

877
  rspSize = tSerializeSVStbRefDbsRsp(NULL, 0, &rsp);
×
878
  if (rspSize < 0) {
×
879
    code = rspSize;
×
880
    qError("tSerializeSVStbRefDbsRsp failed, error:%d", rspSize);
×
881
    goto _return;
×
882
  }
883
  pRsp = taosMemoryCalloc(1, rspSize);
×
884
  if (pRsp == NULL) {
×
885
    code = terrno;
×
886
    qError("rpcMallocCont %d failed, error:%d", rspSize, terrno);
×
887
    goto _return;
×
888
  }
889
  rspSize = tSerializeSVStbRefDbsRsp(pRsp, rspSize, &rsp);
×
890
  if (rspSize < 0) {
×
891
    code = rspSize;
×
892
    qError("tSerializeSVStbRefDbsRsp failed, error:%d", rspSize);
×
893
    goto _return;
×
894
  }
895

896
_return:
×
897

898
  rspMsg.info = pMsg->info;
×
899
  rspMsg.pCont = pRsp;
×
900
  rspMsg.contLen = rspSize;
×
901
  rspMsg.code = code;
×
902
  rspMsg.msgType = pMsg->msgType;
×
903

904
  if (code) {
×
905
    qError("vnd get virtual stb ref db failed cause of %s", tstrerror(code));
×
906
  }
907

908
  *pMsg = rspMsg;
×
909

910
  tDestroySVStbRefDbsRsp(&rsp);
×
911

912
  return code;
×
913
}
914

915

916
int32_t vnodeGetLoad(SVnode *pVnode, SVnodeLoad *pLoad) {
607✔
917
  SSyncState state = syncGetState(pVnode->sync);
607✔
918
  pLoad->syncAppliedIndex = pVnode->state.applied;
607✔
919
  syncGetCommitIndex(pVnode->sync, &pLoad->syncCommitIndex);
607✔
920

921
  pLoad->vgId = TD_VID(pVnode);
607✔
922
  pLoad->syncState = state.state;
607✔
923
  pLoad->syncRestore = state.restored;
607✔
924
  pLoad->syncTerm = state.term;
607✔
925
  pLoad->roleTimeMs = state.roleTimeMs;
607✔
926
  pLoad->startTimeMs = state.startTimeMs;
607✔
927
  pLoad->syncCanRead = state.canRead;
607✔
928
  pLoad->learnerProgress = state.progress;
607✔
929
  pLoad->cacheUsage = tsdbCacheGetUsage(pVnode);
607✔
930
  pLoad->numOfCachedTables = tsdbCacheGetElems(pVnode);
607✔
931
  VNODE_DO_META_QUERY(pVnode, pLoad->numOfTables = metaGetTbNum(pVnode->pMeta));
607✔
932
  VNODE_DO_META_QUERY(pVnode, pLoad->numOfTimeSeries = metaGetTimeSeriesNum(pVnode->pMeta, 1));
607✔
933
  pLoad->totalStorage = (int64_t)3 * 1073741824;
607✔
934
  pLoad->compStorage = (int64_t)2 * 1073741824;
607✔
935
  pLoad->pointsWritten = 100;
607✔
936
  pLoad->numOfSelectReqs = 1;
607✔
937
  pLoad->numOfInsertReqs = atomic_load_64(&pVnode->statis.nInsert);
607✔
938
  pLoad->numOfInsertSuccessReqs = atomic_load_64(&pVnode->statis.nInsertSuccess);
607✔
939
  pLoad->numOfBatchInsertReqs = atomic_load_64(&pVnode->statis.nBatchInsert);
607✔
940
  pLoad->numOfBatchInsertSuccessReqs = atomic_load_64(&pVnode->statis.nBatchInsertSuccess);
607✔
941
  return 0;
607✔
942
}
943

944
int32_t vnodeGetLoadLite(SVnode *pVnode, SVnodeLoadLite *pLoad) {
×
945
  SSyncState syncState = syncGetState(pVnode->sync);
×
946
  if (syncState.state == TAOS_SYNC_STATE_LEADER || syncState.state == TAOS_SYNC_STATE_ASSIGNED_LEADER) {
×
947
    pLoad->vgId = TD_VID(pVnode);
×
948
    pLoad->nTimeSeries = metaGetTimeSeriesNum(pVnode->pMeta, 1);
×
949
    return 0;
×
950
  }
951
  return -1;
×
952
}
953
/**
954
 * @brief Reset the statistics value by monitor interval
955
 *
956
 * @param pVnode
957
 * @param pLoad
958
 */
959
void vnodeResetLoad(SVnode *pVnode, SVnodeLoad *pLoad) {
×
960
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nInsert, pLoad->numOfInsertReqs, 64, "nInsert");
×
961
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nInsertSuccess, pLoad->numOfInsertSuccessReqs, 64, "nInsertSuccess");
×
962
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nBatchInsert, pLoad->numOfBatchInsertReqs, 64, "nBatchInsert");
×
963
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nBatchInsertSuccess, pLoad->numOfBatchInsertSuccessReqs, 64,
×
964
                            "nBatchInsertSuccess");
965
}
×
966

967
void vnodeGetInfo(void *pVnode, const char **dbname, int32_t *vgId, int64_t *numOfTables, int64_t *numOfNormalTables) {
×
968
  SVnode    *pVnodeObj = pVnode;
×
969
  SVnodeCfg *pConf = &pVnodeObj->config;
×
970

971
  if (dbname) {
×
972
    *dbname = pConf->dbname;
×
973
  }
974

975
  if (vgId) {
×
976
    *vgId = TD_VID(pVnodeObj);
×
977
  }
978

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

983
  if (numOfNormalTables) {
×
984
    *numOfNormalTables = pConf->vndStats.numOfNTables;
×
985
  }
986
}
×
987

988
int32_t vnodeGetTableList(void *pVnode, int8_t type, SArray *pList) {
×
989
  if (type == TSDB_SUPER_TABLE) {
×
990
    return vnodeGetStbIdList(pVnode, 0, pList);
×
991
  } else {
992
    return TSDB_CODE_INVALID_PARA;
×
993
  }
994
}
995

996
int32_t vnodeGetAllTableList(SVnode *pVnode, uint64_t uid, SArray *list) {
×
997
  int32_t      code = TSDB_CODE_SUCCESS;
×
998
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, uid, 1);
×
999
  if (NULL == pCur) {
×
1000
    qError("vnode get all table list failed");
×
1001
    return terrno;
×
1002
  }
1003

1004
  while (1) {
×
1005
    tb_uid_t id = metaCtbCursorNext(pCur);
×
1006
    if (id == 0) {
×
1007
      break;
×
1008
    }
1009

1010
    STableKeyInfo info = {uid = id};
×
1011
    if (NULL == taosArrayPush(list, &info)) {
×
1012
      qError("taosArrayPush failed");
×
1013
      code = terrno;
×
1014
      goto _exit;
×
1015
    }
1016
  }
1017
_exit:
×
1018
  metaCloseCtbCursor(pCur);
×
1019
  return code;
×
1020
}
1021

1022
int32_t vnodeGetCtbIdListByFilter(SVnode *pVnode, int64_t suid, SArray *list, bool (*filter)(void *arg), void *arg) {
×
1023
  return 0;
×
1024
}
1025

1026
int32_t vnodeGetCtbIdList(void *pVnode, int64_t suid, SArray *list) {
387✔
1027
  int32_t      code = TSDB_CODE_SUCCESS;
387✔
1028
  SVnode      *pVnodeObj = pVnode;
387✔
1029
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnodeObj, suid, 1);
387✔
1030
  if (NULL == pCur) {
387!
1031
    qError("vnode get all table list failed");
×
1032
    return terrno;
×
1033
  }
1034

1035
  while (1) {
1,782✔
1036
    tb_uid_t id = metaCtbCursorNext(pCur);
2,169✔
1037
    if (id == 0) {
2,168✔
1038
      break;
386✔
1039
    }
1040

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

1048
_exit:
386✔
1049
  metaCloseCtbCursor(pCur);
386✔
1050
  return code;
386✔
1051
}
1052

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

1060
  while (1) {
73✔
1061
    tb_uid_t id = metaStbCursorNext(pCur);
267✔
1062
    if (id == 0) {
267✔
1063
      break;
194✔
1064
    }
1065

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

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

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

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

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

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

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

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

1114
  *num = 0;
41✔
1115
  while (1) {
23✔
1116
    tb_uid_t id = metaCtbCursorNext(pCur);
64✔
1117
    if (id == 0) {
64✔
1118
      break;
41✔
1119
    }
1120

1121
    ++(*num);
23✔
1122
  }
1123

1124
  metaCloseCtbCursor(pCur);
41✔
1125
  return TSDB_CODE_SUCCESS;
41✔
1126
}
1127

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

1137
  return TSDB_CODE_SUCCESS;
41✔
1138
}
1139

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

1144
  int32_t code = metaReaderGetTableEntryByUid(&mr, suid);
41✔
1145
  if (code == TSDB_CODE_SUCCESS) {
41!
1146
    *keep = mr.me.stbEntry.keep;
41✔
1147
  } else {
1148
    *keep = 0;  // Default value if not found
×
1149
  }
1150

1151
  metaReaderClear(&mr);
41✔
1152
  return TSDB_CODE_SUCCESS;
41✔
1153
}
1154

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

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

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

1211
  return code;
194✔
1212
}
1213
#endif
1214

1215
static bool vnodeTimeSeriesFilter(void *arg1, void *arg2) {
×
1216
  SVnode *pVnode = (SVnode *)arg1;
×
1217

1218
  if (metaTbInFilterCache(pVnode->pMeta, arg2, 0)) {
×
1219
    return true;
×
1220
  }
1221
  return false;
×
1222
}
1223

1224
int32_t vnodeGetTimeSeriesNum(SVnode *pVnode, int64_t *num) {
194✔
1225
  SArray *suidList = NULL;
194✔
1226

1227
  if (!(suidList = taosArrayInit(1, sizeof(tb_uid_t)))) {
194!
1228
    return terrno;
×
1229
  }
1230

1231
  int32_t tbFilterSize = 0;
194✔
1232
  int32_t code = TSDB_CODE_SUCCESS;
194✔
1233
#ifdef TD_ENTERPRISE
1234
  code = vnodeGetTimeSeriesBlackList(pVnode, &tbFilterSize);
194✔
1235
  if (TSDB_CODE_SUCCESS != code) {
194!
1236
    goto _exit;
×
1237
  }
1238
#endif
1239

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

1247
  *num = 0;
194✔
1248
  int64_t arrSize = taosArrayGetSize(suidList);
194✔
1249
  for (int64_t i = 0; i < arrSize; ++i) {
267✔
1250
    tb_uid_t suid = *(tb_uid_t *)taosArrayGet(suidList, i);
73✔
1251

1252
    int64_t ctbNum = 0;
73✔
1253
    int32_t numOfCols = 0;
73✔
1254
    code = metaGetStbStats(pVnode, suid, &ctbNum, &numOfCols);
73✔
1255
    if (TSDB_CODE_SUCCESS != code) {
73!
1256
      goto _exit;
×
1257
    }
1258
    *num += ctbNum * (numOfCols - 1);
73✔
1259
  }
1260

1261
_exit:
194✔
1262
  taosArrayDestroy(suidList);
194✔
1263
  return TSDB_CODE_SUCCESS;
194✔
1264
}
1265

1266
int32_t vnodeGetAllCtbNum(SVnode *pVnode, int64_t *num) {
×
1267
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, 0);
×
1268
  if (!pCur) {
×
1269
    return terrno;
×
1270
  }
1271

1272
  *num = 0;
×
1273
  while (1) {
×
1274
    tb_uid_t id = metaStbCursorNext(pCur);
×
1275
    if (id == 0) {
×
1276
      break;
×
1277
    }
1278

1279
    int64_t ctbNum = 0;
×
1280
    int32_t code = vnodeGetCtbNum(pVnode, id, &ctbNum);
×
1281
    if (TSDB_CODE_SUCCESS != code) {
×
1282
      metaCloseStbCursor(pCur);
×
1283
      return code;
×
1284
    }
1285

1286
    *num += ctbNum;
×
1287
  }
1288

1289
  metaCloseStbCursor(pCur);
×
1290
  return TSDB_CODE_SUCCESS;
×
1291
}
1292

1293
void *vnodeGetIdx(void *pVnode) {
22✔
1294
  if (pVnode == NULL) {
22!
1295
    return NULL;
×
1296
  }
1297

1298
  return metaGetIdx(((SVnode *)pVnode)->pMeta);
22✔
1299
}
1300

1301
void *vnodeGetIvtIdx(void *pVnode) {
22✔
1302
  if (pVnode == NULL) {
22!
1303
    return NULL;
×
1304
  }
1305
  return metaGetIvtIdx(((SVnode *)pVnode)->pMeta);
22✔
1306
}
1307

1308
int32_t vnodeGetTableSchema(void *pVnode, int64_t uid, STSchema **pSchema, int64_t *suid) {
×
1309
  return tsdbGetTableSchema(((SVnode *)pVnode)->pMeta, uid, pSchema, suid);
×
1310
}
1311

1312
static FORCE_INLINE int32_t vnodeGetDBPrimaryInfo(SVnode *pVnode, SDbSizeStatisInfo *pInfo) {
1313
  int32_t code = 0;
×
1314
  char    path[TSDB_FILENAME_LEN] = {0};
×
1315

1316
  char   *dirName[] = {VNODE_TSDB_DIR, VNODE_WAL_DIR, VNODE_META_DIR, VNODE_TSDB_CACHE_DIR};
×
1317
  int64_t dirSize[4];
1318

1319
  vnodeGetPrimaryDir(pVnode->path, pVnode->diskPrimary, pVnode->pTfs, path, TSDB_FILENAME_LEN);
×
1320
  int32_t offset = strlen(path);
×
1321

1322
  for (int i = 0; i < sizeof(dirName) / sizeof(dirName[0]); i++) {
×
1323
    int64_t size = {0};
×
1324
    (void)snprintf(path + offset, TSDB_FILENAME_LEN, "%s%s", TD_DIRSEP, dirName[i]);
×
1325
    code = taosGetDirSize(path, &size);
×
1326
    if (code != 0) {
×
1327
      return code;
×
1328
    }
1329
    path[offset] = 0;
×
1330
    dirSize[i] = size;
×
1331
  }
1332

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

1349
  code = tsdbGetFsSize(pVnodeObj->pTsdb, pInfo);
×
1350
_exit:
×
1351
  return code;
×
1352
}
1353

1354
int32_t vnodeGetStreamProgress(SVnode *pVnode, SRpcMsg *pMsg, bool direct) {
×
1355
  int32_t            code = 0;
×
1356
  SStreamProgressReq req;
1357
  SStreamProgressRsp rsp = {0};
×
1358
  SRpcMsg            rpcMsg = {.info = pMsg->info, .code = 0};
×
1359
  char              *buf = NULL;
×
1360
  int32_t            rspLen = 0;
×
1361
  code = tDeserializeStreamProgressReq(pMsg->pCont, pMsg->contLen, &req);
×
1362

1363
  if (code == TSDB_CODE_SUCCESS) {
×
1364
    rsp.fetchIdx = req.fetchIdx;
×
1365
    rsp.subFetchIdx = req.subFetchIdx;
×
1366
    rsp.vgId = req.vgId;
×
1367
    rsp.streamId = req.streamId;
×
1368
    rspLen = tSerializeStreamProgressRsp(0, 0, &rsp);
×
1369
    if (rspLen < 0) {
×
1370
      code = terrno;
×
1371
      goto _OVER;
×
1372
    }
1373
    if (direct) {
×
1374
      buf = rpcMallocCont(rspLen);
×
1375
    } else {
1376
      buf = taosMemoryCalloc(1, rspLen);
×
1377
    }
1378
    if (!buf) {
×
1379
      code = terrno;
×
1380
      goto _OVER;
×
1381
    }
1382
  }
1383

1384
  if (code == TSDB_CODE_SUCCESS) {
×
1385
    code = tqGetStreamExecInfo(pVnode, req.streamId, &rsp.progressDelay, &rsp.fillHisFinished);
×
1386
  }
1387
  if (code == TSDB_CODE_SUCCESS) {
×
1388
    rspLen = tSerializeStreamProgressRsp(buf, rspLen, &rsp);
×
1389
    if (rspLen < 0) {
×
1390
      code = terrno;
×
1391
      goto _OVER;
×
1392
    }
1393
    rpcMsg.pCont = buf;
×
1394
    buf = NULL;
×
1395
    rpcMsg.contLen = rspLen;
×
1396
    rpcMsg.code = code;
×
1397
    rpcMsg.msgType = pMsg->msgType;
×
1398
    if (direct) {
×
1399
      tmsgSendRsp(&rpcMsg);
×
1400
    } else {
1401
      *pMsg = rpcMsg;
×
1402
    }
1403
  }
1404

1405
_OVER:
×
1406
  if (buf) {
×
1407
    taosMemoryFree(buf);
×
1408
  }
1409
  return code;
×
1410
}
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