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

taosdata / TDengine / #4473

08 Jul 2025 09:38AM UTC coverage: 62.922% (+0.7%) from 62.22%
#4473

push

travis-ci

web-flow
Merge pull request #31712 from taosdata/merge/mainto3.0

merge: from main to 3.0 branch

158525 of 321496 branches covered (49.31%)

Branch coverage included in aggregate %.

56 of 60 new or added lines in 13 files covered. (93.33%)

1333 existing lines in 67 files now uncovered.

245526 of 320647 relevant lines covered (76.57%)

17689640.25 hits per line

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

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

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

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

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

53
void vnodePrintTableMeta(STableMetaRsp *pMeta) {
2,006,964✔
54
  if (!(qDebugFlag & DEBUG_DEBUG)) {
2,006,964✔
55
    return;
1,974,510✔
56
  }
57

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

81
int32_t fillTableColRef(SMetaReader *reader, SColRef *pRef, int32_t numOfCol) {
20✔
82
  int8_t tblType = reader->me.type;
20✔
83
  if (hasRefCol(tblType)) {
20!
84
    SColRefWrapper *p = &(reader->me.colRef);
20✔
85
    if (numOfCol != p->nCols) {
20!
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++) {
148✔
90
      SColRef *pColRef = &p->pColRef[i];
128✔
91
      pRef[i].hasRef = pColRef->hasRef;
128✔
92
      pRef[i].id = pColRef->id;
128✔
93
      if(pRef[i].hasRef) {
128✔
94
        tstrncpy(pRef[i].refDbName, pColRef->refDbName, TSDB_DB_NAME_LEN);
62✔
95
        tstrncpy(pRef[i].refTableName, pColRef->refTableName, TSDB_TABLE_NAME_LEN);
62✔
96
        tstrncpy(pRef[i].refColName, pColRef->refColName, TSDB_COL_NAME_LEN);
62✔
97
      }
98
    }
99
  }
100
  return 0;
20✔
101
}
102

103
int32_t vnodeGetTableMeta(SVnode *pVnode, SRpcMsg *pMsg, bool direct) {
2,086,343✔
104
  STableInfoReq  infoReq = {0};
2,086,343✔
105
  STableMetaRsp  metaRsp = {0};
2,086,343✔
106
  SMetaReader    mer1 = {0};
2,086,343✔
107
  SMetaReader    mer2 = {0};
2,086,343✔
108
  char           tableFName[TSDB_TABLE_FNAME_LEN];
109
  bool           reqTbUid = false;
2,086,343✔
110
  SRpcMsg        rpcMsg = {0};
2,086,343✔
111
  int32_t        code = 0;
2,086,343✔
112
  int32_t        rspLen = 0;
2,086,343✔
113
  void          *pRsp = NULL;
2,086,343✔
114
  SSchemaWrapper schema = {0};
2,086,343✔
115
  SSchemaWrapper schemaTag = {0};
2,086,343✔
116
  uint8_t        autoCreateCtb = 0;
2,086,343✔
117

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

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

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

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

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

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

178
      (void)strcpy(metaRsp.stbName, mer2.me.name);
587,242✔
179
      metaRsp.suid = mer2.me.uid;
587,242✔
180
      schema = mer2.me.stbEntry.schemaRow;
587,242✔
181
      schemaTag = mer2.me.stbEntry.schemaTag;
587,242✔
182
      break;
587,242✔
183
    }
184
    case TSDB_NORMAL_TABLE:
69,712✔
185
    case TSDB_VIRTUAL_NORMAL_TABLE: {
186
      schema = mer1.me.ntbEntry.schemaRow;
69,712✔
187
      break;
69,712✔
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;
2,007,630✔
196
  metaRsp.numOfColumns = schema.nCols;
2,007,630✔
197
  metaRsp.precision = pVnode->config.tsdbCfg.precision;
2,007,630✔
198
  metaRsp.sversion = schema.version;
2,007,630✔
199
  metaRsp.tversion = schemaTag.version;
2,007,630✔
200
  metaRsp.pSchemas = (SSchema *)taosMemoryMalloc(sizeof(SSchema) * (metaRsp.numOfColumns + metaRsp.numOfTags));
2,007,630!
201
  metaRsp.pSchemaExt = (SSchemaExt *)taosMemoryCalloc(metaRsp.numOfColumns, sizeof(SSchemaExt));
2,007,458!
202
  if (NULL == metaRsp.pSchemas || NULL == metaRsp.pSchemaExt) {
2,006,960!
203
    code = terrno;
×
204
    goto _exit;
×
205
  }
206
  (void)memcpy(metaRsp.pSchemas, schema.pSchema, sizeof(SSchema) * schema.nCols);
2,007,006✔
207
  if (schemaTag.nCols) {
2,007,006✔
208
    (void)memcpy(metaRsp.pSchemas + schema.nCols, schemaTag.pSchema, sizeof(SSchema) * schemaTag.nCols);
1,936,885✔
209
  }
210
  if (metaRsp.pSchemaExt) {
2,007,006✔
211
    SMetaReader *pReader = mer1.me.type == TSDB_CHILD_TABLE ? &mer2 : &mer1;
2,006,633✔
212
    code = fillTableColCmpr(pReader, metaRsp.pSchemaExt, metaRsp.numOfColumns);
2,006,633✔
213
    if (code < 0) {
2,007,205!
214
      goto _exit;
×
215
    }
216
    for (int32_t i = 0; i < metaRsp.numOfColumns && pReader->me.pExtSchemas; i++) {
2,385,177✔
217
      metaRsp.pSchemaExt[i].typeMod = pReader->me.pExtSchemas[i].typeMod;
377,972✔
218
    }
219
  } else {
220
    code = TSDB_CODE_OUT_OF_MEMORY;
373✔
221
    goto _exit;
373✔
222
  }
223
  if (hasRefCol(mer1.me.type)) {
2,007,205✔
224
    metaRsp.rversion = mer1.me.colRef.version;
179✔
225
    metaRsp.pColRefs = (SColRef*)taosMemoryMalloc(sizeof(SColRef) * metaRsp.numOfColumns);
179!
226
    if (metaRsp.pColRefs) {
20!
227
      code = fillTableColRef(&mer1, metaRsp.pColRefs, metaRsp.numOfColumns);
20✔
228
      if (code < 0) {
20!
229
        goto _exit;
×
230
      }
231
    }
232
    metaRsp.numOfColRefs = metaRsp.numOfColumns;
20✔
233
  } else {
234
    metaRsp.pColRefs = NULL;
2,006,958✔
235
    metaRsp.numOfColRefs = 0;
2,006,958✔
236
  }
237

238
  vnodePrintTableMeta(&metaRsp);
2,006,978✔
239

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

247
  if (direct) {
2,006,439✔
248
    pRsp = rpcMallocCont(rspLen);
190✔
249
  } else {
250
    pRsp = taosMemoryCalloc(1, rspLen);
2,006,249!
251
  }
252

253
  if (pRsp == NULL) {
2,006,319!
254
    code = terrno;
×
255
    goto _exit;
×
256
  }
257

258
  rspLen = tSerializeSTableMetaRsp(pRsp, rspLen, &metaRsp);
2,006,319✔
259
  if (rspLen < 0) {
2,007,192!
260
    code = terrno;
×
261
    goto _exit;
×
262
  }
263

264
_exit:
2,007,192✔
265
  taosMemoryFree(metaRsp.pColRefs);
2,007,565!
266
  taosMemoryFree(metaRsp.pSchemas);
2,006,838!
267
  taosMemoryFree(metaRsp.pSchemaExt);
2,007,129!
268
_exit2:
2,007,395✔
269
  metaReaderClear(&mer2);
2,007,395✔
270
_exit3:
2,089,109✔
271
  metaReaderClear(&mer1);
2,089,109✔
272
_exit4:
2,089,265✔
273
  rpcMsg.info = pMsg->info;
2,089,265✔
274
  rpcMsg.pCont = pRsp;
2,089,265✔
275
  rpcMsg.contLen = rspLen;
2,089,265✔
276
  rpcMsg.code = code;
2,089,265✔
277
  rpcMsg.msgType = pMsg->msgType;
2,089,265✔
278

279
  if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST && autoCreateCtb == 1) {
2,089,265✔
280
    code = TSDB_CODE_SUCCESS;
61,660✔
281
  }
282

283
  if (code) {
2,089,265✔
284
    qError("get table %s meta with %" PRIu8 " failed cause of %s", infoReq.tbName, infoReq.option, tstrerror(code));
20,267!
285
  }
286

287
  if (direct) {
2,088,842✔
288
    tmsgSendRsp(&rpcMsg);
3,000✔
289
  } else {
290
    *pMsg = rpcMsg;
2,085,842✔
291
  }
292

293
  return code;
2,088,832✔
294
}
295

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

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

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

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

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

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

332
  cfgRsp.tableType = mer1.me.type;
193✔
333

334
  if (mer1.me.type == TSDB_SUPER_TABLE) {
193!
335
    code = TSDB_CODE_VND_HASH_MISMATCH;
×
336
    goto _exit;
×
337
  } else if (mer1.me.type == TSDB_CHILD_TABLE || mer1.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
193✔
338
    metaReaderDoInit(&mer2, pVnode->pMeta, META_READER_NOLOCK);
141✔
339
    if (metaReaderGetTableEntryByUid(&mer2, mer1.me.ctbEntry.suid) < 0) goto _exit;
141!
340

341
    tstrncpy(cfgRsp.stbName, mer2.me.name, TSDB_TABLE_NAME_LEN);
141✔
342
    schema = mer2.me.stbEntry.schemaRow;
141✔
343
    schemaTag = mer2.me.stbEntry.schemaTag;
141✔
344
    cfgRsp.ttl = mer1.me.ctbEntry.ttlDays;
141✔
345
    cfgRsp.commentLen = mer1.me.ctbEntry.commentLen;
141✔
346
    if (mer1.me.ctbEntry.commentLen > 0) {
141!
347
      cfgRsp.pComment = taosStrdup(mer1.me.ctbEntry.comment);
×
348
      if (NULL == cfgRsp.pComment) {
×
349
        code = terrno;
×
350
        goto _exit;
×
351
      }
352
    }
353
    STag *pTag = (STag *)mer1.me.ctbEntry.pTags;
141✔
354
    cfgRsp.tagsLen = pTag->len;
141✔
355
    cfgRsp.pTags = taosMemoryMalloc(cfgRsp.tagsLen);
141!
356
    if (NULL == cfgRsp.pTags) {
141!
357
      code = terrno;
×
358
      goto _exit;
×
359
    }
360
    (void)memcpy(cfgRsp.pTags, pTag, cfgRsp.tagsLen);
141✔
361
  } else if (mer1.me.type == TSDB_NORMAL_TABLE || mer1.me.type == TSDB_VIRTUAL_NORMAL_TABLE) {
52!
362
    schema = mer1.me.ntbEntry.schemaRow;
52✔
363
    cfgRsp.ttl = mer1.me.ntbEntry.ttlDays;
52✔
364
    cfgRsp.commentLen = mer1.me.ntbEntry.commentLen;
52✔
365
    if (mer1.me.ntbEntry.commentLen > 0) {
52!
366
      cfgRsp.pComment = taosStrdup(mer1.me.ntbEntry.comment);
×
367
      if (NULL == cfgRsp.pComment) {
×
368
        code = terrno;
×
369
        goto _exit;
×
370
      }
371
    }
372
  } else {
373
    vError("vnodeGetTableCfg get invalid table type:%d", mer1.me.type);
×
374
    code = TSDB_CODE_APP_ERROR;
×
375
    goto _exit;
×
376
  }
377

378
  cfgRsp.numOfTags = schemaTag.nCols;
193✔
379
  cfgRsp.numOfColumns = schema.nCols;
193✔
380
  cfgRsp.virtualStb = false; // vnode don't have super table, so it's always false
193✔
381
  cfgRsp.pSchemas = (SSchema *)taosMemoryMalloc(sizeof(SSchema) * (cfgRsp.numOfColumns + cfgRsp.numOfTags));
193!
382
  cfgRsp.pSchemaExt = (SSchemaExt *)taosMemoryMalloc(cfgRsp.numOfColumns * sizeof(SSchemaExt));
193!
383
  cfgRsp.pColRefs = (SColRef *)taosMemoryMalloc(sizeof(SColRef) * cfgRsp.numOfColumns);
193!
384

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

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

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

411
  cfgRsp.virtualStb = false;
193✔
412
  if (hasRefCol(cfgRsp.tableType)) {
193✔
413
    for (int32_t i = 0; i < cfgRsp.numOfColumns; i++) {
106✔
414
      SColRef *pRef = &pColRef->pColRef[i];
88✔
415
      cfgRsp.pColRefs[i].hasRef = pRef->hasRef;
88✔
416
      cfgRsp.pColRefs[i].id = pRef->id;
88✔
417
      if (cfgRsp.pColRefs[i].hasRef) {
88✔
418
        tstrncpy(cfgRsp.pColRefs[i].refDbName, pRef->refDbName, TSDB_DB_NAME_LEN);
47✔
419
        tstrncpy(cfgRsp.pColRefs[i].refTableName, pRef->refTableName, TSDB_TABLE_NAME_LEN);
47✔
420
        tstrncpy(cfgRsp.pColRefs[i].refColName, pRef->refColName, TSDB_COL_NAME_LEN);
47✔
421
      }
422
    }
423
  }
424

425
  // encode and send response
426
  rspLen = tSerializeSTableCfgRsp(NULL, 0, &cfgRsp);
193✔
427
  if (rspLen < 0) {
193!
428
    code = terrno;
×
429
    goto _exit;
×
430
  }
431

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

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

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

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

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

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

466
  tFreeSTableCfgRsp(&cfgRsp);
193✔
467
  metaReaderClear(&mer2);
193✔
468
  metaReaderClear(&mer1);
193✔
469
  return code;
193✔
470
}
471

472
static FORCE_INLINE void vnodeFreeSBatchRspMsg(void *p) {
473
  if (NULL == p) {
474
    return;
475
  }
476

477
  SBatchRspMsg *pRsp = (SBatchRspMsg *)p;
478
  rpcFreeCont(pRsp->msg);
479
}
480

481
int32_t vnodeGetBatchMeta(SVnode *pVnode, SRpcMsg *pMsg) {
1,171,532✔
482
  int32_t      code = 0;
1,171,532✔
483
  int32_t      rspSize = 0;
1,171,532✔
484
  SBatchReq    batchReq = {0};
1,171,532✔
485
  SBatchMsg   *req = NULL;
1,171,532✔
486
  SBatchRspMsg rsp = {0};
1,171,532✔
487
  SBatchRsp    batchRsp = {0};
1,171,532✔
488
  SRpcMsg      reqMsg = *pMsg;
1,171,532✔
489
  SRpcMsg      rspMsg = {0};
1,171,532✔
490
  void        *pRsp = NULL;
1,171,532✔
491

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

498
  int32_t msgNum = taosArrayGetSize(batchReq.pMsgs);
1,172,415✔
499
  if (msgNum >= MAX_META_MSG_IN_BATCH) {
1,171,424!
500
    code = TSDB_CODE_INVALID_MSG;
×
501
    qError("too many msgs %d in vnode batch meta req", msgNum);
×
502
    goto _exit;
×
503
  }
504

505
  batchRsp.pRsps = taosArrayInit(msgNum, sizeof(SBatchRspMsg));
1,171,424✔
506
  if (NULL == batchRsp.pRsps) {
1,172,268✔
507
    code = terrno;
12✔
508
    qError("taosArrayInit %d SBatchRspMsg failed", msgNum);
×
509
    goto _exit;
×
510
  }
511

512
  for (int32_t i = 0; i < msgNum; ++i) {
3,261,480✔
513
    req = taosArrayGet(batchReq.pMsgs, i);
2,089,634✔
514
    if (req == NULL) {
2,087,435✔
515
      code = terrno;
34✔
516
      goto _exit;
×
517
    }
518

519
    reqMsg.msgType = req->msgType;
2,087,401✔
520
    reqMsg.pCont = req->msg;
2,087,401✔
521
    reqMsg.contLen = req->msgLen;
2,087,401✔
522

523
    switch (req->msgType) {
2,087,401!
524
      case TDMT_VND_TABLE_META:
2,083,162✔
525
        // error code has been set into reqMsg, no need to handle it here.
526
        if (TSDB_CODE_SUCCESS != vnodeGetTableMeta(pVnode, &reqMsg, false)) {
2,083,162✔
527
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
17,324!
528
        }
529
        break;
2,085,150✔
530
      case TDMT_VND_TABLE_NAME:
538✔
531
        // error code has been set into reqMsg, no need to handle it here.
532
        if (TSDB_CODE_SUCCESS != vnodeGetTableMeta(pVnode, &reqMsg, false)) {
538✔
533
          qWarn("vnodeGetBatchName failed, msgType:%d", req->msgType);
136!
534
        }
535
        break;
538✔
536
      case TDMT_VND_TABLE_CFG:
193✔
537
        // error code has been set into reqMsg, no need to handle it here.
538
        if (TSDB_CODE_SUCCESS != vnodeGetTableCfg(pVnode, &reqMsg, false)) {
193!
539
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
×
540
        }
541
        break;
193✔
542
      case TDMT_VND_GET_STREAM_PROGRESS:
3,436✔
543
        // error code has been set into reqMsg, no need to handle it here.
544
        if (TSDB_CODE_SUCCESS != vnodeGetStreamProgress(pVnode, &reqMsg, false)) {
3,436!
545
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
×
546
        }
547
        break;
3,436✔
548
      case TDMT_VND_VSUBTABLES_META:
×
549
        // error code has been set into reqMsg, no need to handle it here.
550
        if (TSDB_CODE_SUCCESS != vnodeGetVSubtablesMeta(pVnode, &reqMsg)) {
×
551
          qWarn("vnodeGetVSubtablesMeta failed, msgType:%d", req->msgType);
×
552
        }
553
        break;
×
554
      case TDMT_VND_VSTB_REF_DBS:
72✔
555
        // error code has been set into reqMsg, no need to handle it here.
556
        if (TSDB_CODE_SUCCESS != vnodeGetVStbRefDbs(pVnode, &reqMsg)) {
72!
557
          qWarn("vnodeGetVStbRefDbs failed, msgType:%d", req->msgType);
×
558
        }
559
        break;
72✔
560
      default:
×
561
        qError("invalid req msgType %d", req->msgType);
×
562
        reqMsg.code = TSDB_CODE_INVALID_MSG;
×
563
        reqMsg.pCont = NULL;
×
564
        reqMsg.contLen = 0;
×
565
        break;
×
566
    }
567

568
    rsp.msgIdx = req->msgIdx;
2,089,389✔
569
    rsp.reqType = reqMsg.msgType;
2,089,389✔
570
    rsp.msgLen = reqMsg.contLen;
2,089,389✔
571
    rsp.rspCode = reqMsg.code;
2,089,389✔
572
    rsp.msg = reqMsg.pCont;
2,089,389✔
573

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

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

599
_exit:
1,172,598✔
600

601
  rspMsg.info = pMsg->info;
1,172,598✔
602
  rspMsg.pCont = pRsp;
1,172,598✔
603
  rspMsg.contLen = rspSize;
1,172,598✔
604
  rspMsg.code = code;
1,172,598✔
605
  rspMsg.msgType = pMsg->msgType;
1,172,598✔
606

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

611
  taosArrayDestroyEx(batchReq.pMsgs, tFreeSBatchReqMsg);
1,172,598✔
612
  taosArrayDestroyEx(batchRsp.pRsps, tFreeSBatchRspMsg);
1,172,544✔
613

614
  tmsgSendRsp(&rspMsg);
1,172,548✔
615

616
  return code;
1,172,549✔
617
}
618

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

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

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

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

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

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

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

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

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

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

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

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

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

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

692
      refColsNum++;
×
693
    }
694

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

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

703
_return:
×
704

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

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

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

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

727
  pList = taosArrayInit(10, sizeof(uint64_t));
72✔
728
  QUERY_CHECK_NULL(pList, code, line, _return, terrno);
72!
729

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

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

744
    size_t num = taosArrayGetSize(pList);
37✔
745
    pDbNameHash = tSimpleHashInit(10, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
37✔
746
    QUERY_CHECK_NULL(pDbNameHash, code, line, _return, terrno);
37!
747

748
    for (int32_t i = 0; i < num; ++i) {
38✔
749
      uint64_t* id = taosArrayGet(pList, i);
1✔
750
      QUERY_CHECK_NULL(id, code, line, _return, terrno);
1!
751

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

755
      code = pHandle->api.metaReaderFn.getTableEntryByUid(&mr, *id);
1✔
756
      QUERY_CHECK_CODE(code, line, _return);
1!
757

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

764
            QUERY_CHECK_NULL(taosArrayPush(*ppRes, &refDbName), code, line, _return, terrno);
2!
765

766
            code = tSimpleHashPut(pDbNameHash, refDbName, strlen(refDbName), NULL, 0);
1✔
767
            QUERY_CHECK_CODE(code, line, _return);
1!
768
          }
769
        }
770
      }
771

772
      pHandle->api.metaReaderFn.clearReader(&mr);
1✔
773
      readerInit = false;
1✔
774
    }
775

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

780
_return:
37✔
781

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

786
  taosArrayDestroy(pList);
72✔
787
  tSimpleHashCleanup(pDbNameHash);
72✔
788

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

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

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

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

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

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

835
_return:
×
836

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

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

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

851
  //tmsgSendRsp(&rspMsg);
852

853
  return code;
×
854
}
855

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

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

871
  SReadHandle handle = {.vnode = pVnode};
72✔
872
  initStorageAPI(&handle.api);
72✔
873

874
  code = vnodeReadVStbRefDbs(&handle, req.suid, &rsp.pDbs);
72✔
875
  QUERY_CHECK_CODE(code, line, _return);
72!
876
  rsp.vgId = TD_VID(pVnode);
72✔
877

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

897
_return:
72✔
898

899
  rspMsg.info = pMsg->info;
72✔
900
  rspMsg.pCont = pRsp;
72✔
901
  rspMsg.contLen = rspSize;
72✔
902
  rspMsg.code = code;
72✔
903
  rspMsg.msgType = pMsg->msgType;
72✔
904

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

909
  *pMsg = rspMsg;
72✔
910

911
  tDestroySVStbRefDbsRsp(&rsp);
72✔
912

913
  return code;
72✔
914
}
915

916
static int32_t vnodeGetCompStorage(SVnode *pVnode, int64_t *output) {
732,280✔
917
  int32_t code = 0;
732,280✔
918
#ifdef TD_ENTERPRISE
919
  int32_t now = taosGetTimestampSec();
732,280✔
920
  if (llabs(now - pVnode->config.vndStats.storageLastUpd) >= 30) {
732,280✔
921
    pVnode->config.vndStats.storageLastUpd = now;
34,091✔
922

923
    SDbSizeStatisInfo info = {0};
34,091✔
924
    if (0 == (code = vnodeGetDBSize(pVnode, &info))) {
34,091✔
925
      int64_t compSize =
34,088✔
926
          info.l1Size + info.l2Size + info.l3Size + info.cacheSize + info.walSize + info.metaSize + +info.s3Size;
34,088✔
927
      if (compSize >= 0) {
34,088!
928
        pVnode->config.vndStats.compStorage = compSize;
34,088✔
929
      } else {
NEW
930
        vError("vnode get comp storage failed since compSize is negative:%" PRIi64, compSize);
×
NEW
931
        code = TSDB_CODE_APP_ERROR;
×
932
      }
933
    } else {
934
      vWarn("vnode get comp storage failed since %s", tstrerror(code));
3!
935
    }
936
  }
937
  if (output) *output = pVnode->config.vndStats.compStorage;
732,280!
938
#endif
939
  return code;
732,280✔
940
}
941

942
static void vnodeGetBufferInfo(SVnode *pVnode, int64_t *bufferSegmentUsed, int64_t *bufferSegmentSize) {
732,280✔
943
  *bufferSegmentUsed = 0;
732,280✔
944
  *bufferSegmentSize = 0;
732,280✔
945
  if (pVnode) {
732,280!
946
    (void)taosThreadMutexLock(&pVnode->mutex);
732,280✔
947

948
    if (pVnode->inUse) {
732,280✔
949
      *bufferSegmentUsed = pVnode->inUse->size;
732,252✔
950
    }
951
    *bufferSegmentSize = pVnode->config.szBuf / VNODE_BUFPOOL_SEGMENTS;
732,280✔
952

953
    (void)taosThreadMutexUnlock(&pVnode->mutex);
732,280✔
954
  }
955
}
732,280✔
956

957
int32_t vnodeGetLoad(SVnode *pVnode, SVnodeLoad *pLoad) {
732,280✔
958
  SSyncState state = syncGetState(pVnode->sync);
732,280✔
959
  pLoad->syncAppliedIndex = pVnode->state.applied;
732,280✔
960
  syncGetCommitIndex(pVnode->sync, &pLoad->syncCommitIndex);
732,280✔
961

962
  pLoad->vgId = TD_VID(pVnode);
732,280✔
963
  pLoad->syncState = state.state;
732,280✔
964
  pLoad->syncRestore = state.restored;
732,280✔
965
  pLoad->syncTerm = state.term;
732,280✔
966
  pLoad->roleTimeMs = state.roleTimeMs;
732,280✔
967
  pLoad->startTimeMs = state.startTimeMs;
732,280✔
968
  pLoad->syncCanRead = state.canRead;
732,280✔
969
  pLoad->learnerProgress = state.progress;
732,280✔
970
  pLoad->cacheUsage = tsdbCacheGetUsage(pVnode);
732,280✔
971
  pLoad->numOfCachedTables = tsdbCacheGetElems(pVnode);
732,280✔
972
  VNODE_DO_META_QUERY(pVnode, pLoad->numOfTables = metaGetTbNum(pVnode->pMeta));
732,280✔
973
  VNODE_DO_META_QUERY(pVnode, pLoad->numOfTimeSeries = metaGetTimeSeriesNum(pVnode->pMeta, 1));
732,280✔
974
  pLoad->totalStorage = (int64_t)3 * 1073741824;  // TODO
732,280✔
975
  (void)vnodeGetCompStorage(pVnode, &pLoad->compStorage);
732,280✔
976
  pLoad->pointsWritten = 100;
732,280✔
977
  pLoad->numOfSelectReqs = 1;
732,280✔
978
  pLoad->numOfInsertReqs = atomic_load_64(&pVnode->statis.nInsert);
732,280✔
979
  pLoad->numOfInsertSuccessReqs = atomic_load_64(&pVnode->statis.nInsertSuccess);
732,280✔
980
  pLoad->numOfBatchInsertReqs = atomic_load_64(&pVnode->statis.nBatchInsert);
732,280✔
981
  pLoad->numOfBatchInsertSuccessReqs = atomic_load_64(&pVnode->statis.nBatchInsertSuccess);
732,280✔
982
  vnodeGetBufferInfo(pVnode, &pLoad->bufferSegmentUsed, &pLoad->bufferSegmentSize);
732,280✔
983
  return 0;
732,280✔
984
}
985

UNCOV
986
int32_t vnodeGetLoadLite(SVnode *pVnode, SVnodeLoadLite *pLoad) {
×
UNCOV
987
  SSyncState syncState = syncGetState(pVnode->sync);
×
UNCOV
988
  if (syncState.state == TAOS_SYNC_STATE_LEADER || syncState.state == TAOS_SYNC_STATE_ASSIGNED_LEADER) {
×
UNCOV
989
    pLoad->vgId = TD_VID(pVnode);
×
UNCOV
990
    pLoad->nTimeSeries = metaGetTimeSeriesNum(pVnode->pMeta, 1);
×
UNCOV
991
    return 0;
×
992
  }
UNCOV
993
  return -1;
×
994
}
995
/**
996
 * @brief Reset the statistics value by monitor interval
997
 *
998
 * @param pVnode
999
 * @param pLoad
1000
 */
1001
void vnodeResetLoad(SVnode *pVnode, SVnodeLoad *pLoad) {
38✔
1002
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nInsert, pLoad->numOfInsertReqs, 64, "nInsert");
38!
1003
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nInsertSuccess, pLoad->numOfInsertSuccessReqs, 64, "nInsertSuccess");
38!
1004
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nBatchInsert, pLoad->numOfBatchInsertReqs, 64, "nBatchInsert");
38!
1005
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nBatchInsertSuccess, pLoad->numOfBatchInsertSuccessReqs, 64,
38!
1006
                            "nBatchInsertSuccess");
1007
}
38✔
1008

1009
void vnodeGetInfo(void *pVnode, const char **dbname, int32_t *vgId, int64_t *numOfTables, int64_t *numOfNormalTables) {
1,055,648✔
1010
  SVnode    *pVnodeObj = pVnode;
1,055,648✔
1011
  SVnodeCfg *pConf = &pVnodeObj->config;
1,055,648✔
1012

1013
  if (dbname) {
1,055,648!
1014
    *dbname = pConf->dbname;
1,056,252✔
1015
  }
1016

1017
  if (vgId) {
1,055,648✔
1018
    *vgId = TD_VID(pVnodeObj);
1,055,068✔
1019
  }
1020

1021
  if (numOfTables) {
1,055,648!
UNCOV
1022
    *numOfTables = pConf->vndStats.numOfNTables + pConf->vndStats.numOfCTables;
×
1023
  }
1024

1025
  if (numOfNormalTables) {
1,055,648!
UNCOV
1026
    *numOfNormalTables = pConf->vndStats.numOfNTables;
×
1027
  }
1028
}
1,055,648✔
1029

UNCOV
1030
int32_t vnodeGetTableList(void *pVnode, int8_t type, SArray *pList) {
×
UNCOV
1031
  if (type == TSDB_SUPER_TABLE) {
×
UNCOV
1032
    return vnodeGetStbIdList(pVnode, 0, pList);
×
1033
  } else {
UNCOV
1034
    return TSDB_CODE_INVALID_PARA;
×
1035
  }
1036
}
1037

UNCOV
1038
int32_t vnodeGetAllTableList(SVnode *pVnode, uint64_t uid, SArray *list) {
×
UNCOV
1039
  int32_t      code = TSDB_CODE_SUCCESS;
×
UNCOV
1040
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, uid, 1);
×
UNCOV
1041
  if (NULL == pCur) {
×
UNCOV
1042
    qError("vnode get all table list failed");
×
UNCOV
1043
    return terrno;
×
1044
  }
1045

UNCOV
1046
  while (1) {
×
UNCOV
1047
    tb_uid_t id = metaCtbCursorNext(pCur);
×
1048
    if (id == 0) {
×
UNCOV
1049
      break;
×
1050
    }
1051

1052
    STableKeyInfo info = {uid = id};
×
UNCOV
1053
    if (NULL == taosArrayPush(list, &info)) {
×
UNCOV
1054
      qError("taosArrayPush failed");
×
UNCOV
1055
      code = terrno;
×
1056
      goto _exit;
×
1057
    }
1058
  }
UNCOV
1059
_exit:
×
1060
  metaCloseCtbCursor(pCur);
×
UNCOV
1061
  return code;
×
1062
}
1063

1064
int32_t vnodeGetCtbIdListByFilter(SVnode *pVnode, int64_t suid, SArray *list, bool (*filter)(void *arg), void *arg) {
×
1065
  return 0;
×
1066
}
1067

1068
int32_t vnodeGetCtbIdList(void *pVnode, int64_t suid, SArray *list) {
2,905,507✔
1069
  int32_t      code = TSDB_CODE_SUCCESS;
2,905,507✔
1070
  SVnode      *pVnodeObj = pVnode;
2,905,507✔
1071
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnodeObj, suid, 1);
2,905,507✔
1072
  if (NULL == pCur) {
2,911,417!
1073
    qError("vnode get all table list failed");
×
1074
    return terrno;
×
1075
  }
1076

1077
  while (1) {
9,371,915✔
1078
    tb_uid_t id = metaCtbCursorNext(pCur);
12,283,332✔
1079
    if (id == 0) {
12,278,433✔
1080
      break;
2,912,544✔
1081
    }
1082

1083
    if (NULL == taosArrayPush(list, &id)) {
9,371,915!
UNCOV
1084
      qError("taosArrayPush failed");
×
1085
      code = terrno;
×
1086
      goto _exit;
×
1087
    }
1088
  }
1089

1090
_exit:
2,912,544✔
1091
  metaCloseCtbCursor(pCur);
2,912,544✔
1092
  return code;
2,911,903✔
1093
}
1094

1095
int32_t vnodeGetStbIdList(SVnode *pVnode, int64_t suid, SArray *list) {
184,519✔
1096
  int32_t      code = TSDB_CODE_SUCCESS;
184,519✔
1097
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, suid);
184,519✔
1098
  if (!pCur) {
184,510!
1099
    return TSDB_CODE_OUT_OF_MEMORY;
×
1100
  }
1101

1102
  while (1) {
186,073✔
1103
    tb_uid_t id = metaStbCursorNext(pCur);
370,583✔
1104
    if (id == 0) {
370,545✔
1105
      break;
184,512✔
1106
    }
1107

1108
    if (NULL == taosArrayPush(list, &id)) {
186,073!
UNCOV
1109
      qError("taosArrayPush failed");
×
1110
      code = terrno;
×
1111
      goto _exit;
×
1112
    }
1113
  }
1114

1115
_exit:
184,512✔
1116
  metaCloseStbCursor(pCur);
184,512✔
1117
  return code;
184,520✔
1118
}
1119

1120
int32_t vnodeGetStbIdListByFilter(SVnode *pVnode, int64_t suid, SArray *list, bool (*filter)(void *arg, void *arg1),
89✔
1121
                                  void *arg) {
1122
  int32_t      code = TSDB_CODE_SUCCESS;
89✔
1123
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, suid);
89✔
1124
  if (!pCur) {
89!
1125
    return terrno;
×
1126
  }
1127

1128
  while (1) {
97✔
1129
    tb_uid_t id = metaStbCursorNext(pCur);
186✔
1130
    if (id == 0) {
186✔
1131
      break;
89✔
1132
    }
1133

1134
    if ((*filter) && (*filter)(arg, &id)) {
97!
1135
      continue;
97✔
1136
    }
1137

UNCOV
1138
    if (NULL == taosArrayPush(list, &id)) {
×
UNCOV
1139
      qError("taosArrayPush failed");
×
UNCOV
1140
      code = terrno;
×
UNCOV
1141
      goto _exit;
×
1142
    }
1143
  }
1144

1145
_exit:
89✔
1146
  metaCloseStbCursor(pCur);
89✔
1147
  return code;
89✔
1148
}
1149

1150
int32_t vnodeGetCtbNum(SVnode *pVnode, int64_t suid, int64_t *num) {
26,219✔
1151
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, suid, 0);
26,219✔
1152
  if (!pCur) {
26,224!
UNCOV
1153
    return terrno;
×
1154
  }
1155

1156
  *num = 0;
26,224✔
1157
  while (1) {
29,048✔
1158
    tb_uid_t id = metaCtbCursorNext(pCur);
55,272✔
1159
    if (id == 0) {
55,272✔
1160
      break;
26,224✔
1161
    }
1162

1163
    ++(*num);
29,048✔
1164
  }
1165

1166
  metaCloseCtbCursor(pCur);
26,224✔
1167
  return TSDB_CODE_SUCCESS;
26,225✔
1168
}
1169

1170
int32_t vnodeGetStbColumnNum(SVnode *pVnode, tb_uid_t suid, int *num) {
26,223✔
1171
  SSchemaWrapper *pSW = metaGetTableSchema(pVnode->pMeta, suid, -1, 0, NULL);
26,223✔
1172
  if (pSW) {
26,228!
1173
    *num = pSW->nCols;
26,228!
1174
    tDeleteSchemaWrapper(pSW);
1175
  } else {
UNCOV
1176
    *num = 2;
×
1177
  }
1178

1179
  return TSDB_CODE_SUCCESS;
26,228✔
1180
}
1181

1182
int32_t vnodeGetStbInfo(SVnode *pVnode, tb_uid_t suid, int64_t *keep, int8_t *flags) {
26,228✔
1183
  SMetaReader mr = {0};
26,228✔
1184
  metaReaderDoInit(&mr, pVnode->pMeta, META_READER_NOLOCK);
26,228✔
1185

1186
  int32_t code = metaReaderGetTableEntryByUid(&mr, suid);
26,228✔
1187
  if (code == TSDB_CODE_SUCCESS) {
26,220!
1188
    if (keep) *keep = mr.me.stbEntry.keep;
26,220!
1189
    if (flags) *flags = mr.me.flags;
26,220!
1190
  } else {
UNCOV
1191
    if (keep) *keep = 0;
×
UNCOV
1192
    if (flags) *flags = 0;
×
1193
  }
1194

1195
  metaReaderClear(&mr);
26,220✔
1196
  return TSDB_CODE_SUCCESS;
26,226✔
1197
}
1198

1199
#ifdef TD_ENTERPRISE
1200
const char *tkLogStb[] = {"cluster_info",
1201
                          "data_dir",
1202
                          "dnodes_info",
1203
                          "d_info",
1204
                          "grants_info",
1205
                          "keeper_monitor",
1206
                          "logs",
1207
                          "log_dir",
1208
                          "log_summary",
1209
                          "m_info",
1210
                          "taosadapter_restful_http_request_fail",
1211
                          "taosadapter_restful_http_request_in_flight",
1212
                          "taosadapter_restful_http_request_summary_milliseconds",
1213
                          "taosadapter_restful_http_request_total",
1214
                          "taosadapter_system_cpu_percent",
1215
                          "taosadapter_system_mem_percent",
1216
                          "temp_dir",
1217
                          "vgroups_info",
1218
                          "vnodes_role"};
1219
const char *tkAuditStb[] = {"operations"};
1220
const int   tkLogStbNum = ARRAY_SIZE(tkLogStb);
1221
const int   tkAuditStbNum = ARRAY_SIZE(tkAuditStb);
1222

1223
// exclude stbs of taoskeeper log
1224
static int32_t vnodeGetTimeSeriesBlackList(SVnode *pVnode, int32_t *tbSize) {
184,599✔
1225
  int32_t      code = TSDB_CODE_SUCCESS;
184,599✔
1226
  int32_t      tbNum = 0;
184,599✔
1227
  const char **pTbArr = NULL;
184,599✔
1228
  const char  *dbName = NULL;
184,599✔
1229
  *tbSize = 0;
184,599✔
1230

1231
  if (!(dbName = strchr(pVnode->config.dbname, '.'))) return 0;
184,599!
1232
  if (0 == strncmp(++dbName, "log", TSDB_DB_NAME_LEN)) {
184,599✔
1233
    tbNum = tkLogStbNum;
18✔
1234
    pTbArr = (const char **)&tkLogStb;
18✔
1235
  } else if (0 == strncmp(dbName, "audit", TSDB_DB_NAME_LEN)) {
184,581✔
1236
    tbNum = tkAuditStbNum;
87✔
1237
    pTbArr = (const char **)&tkAuditStb;
87✔
1238
  }
1239
  if (tbNum && pTbArr) {
184,599!
1240
    *tbSize = metaSizeOfTbFilterCache(pVnode->pMeta, 0);
105✔
1241
    if (*tbSize < tbNum) {
105✔
1242
      for (int32_t i = 0; i < tbNum; ++i) {
238✔
1243
        tb_uid_t suid = metaGetTableEntryUidByName(pVnode->pMeta, pTbArr[i]);
232✔
1244
        if (suid != 0) {
232✔
1245
          code = metaPutTbToFilterCache(pVnode->pMeta, &suid, 0);
26✔
1246
          if (TSDB_CODE_SUCCESS != code) {
26✔
1247
            return code;
14✔
1248
          }
1249
        }
1250
      }
1251
      *tbSize = metaSizeOfTbFilterCache(pVnode->pMeta, 0);
6✔
1252
    }
1253
  }
1254

1255
  return code;
184,585✔
1256
}
1257
#endif
1258

1259
static bool vnodeTimeSeriesFilter(void *arg1, void *arg2) {
97✔
1260
  SVnode *pVnode = (SVnode *)arg1;
97✔
1261

1262
  if (metaTbInFilterCache(pVnode->pMeta, arg2, 0)) {
97!
1263
    return true;
97✔
1264
  }
UNCOV
1265
  return false;
×
1266
}
1267

1268
int32_t vnodeGetTimeSeriesNum(SVnode *pVnode, int64_t *num) {
184,597✔
1269
  SArray *suidList = NULL;
184,597✔
1270

1271
  if (!(suidList = taosArrayInit(1, sizeof(tb_uid_t)))) {
184,597!
UNCOV
1272
    return terrno;
×
1273
  }
1274

1275
  int32_t tbFilterSize = 0;
184,600✔
1276
  int32_t code = TSDB_CODE_SUCCESS;
184,600✔
1277
#ifdef TD_ENTERPRISE
1278
  code = vnodeGetTimeSeriesBlackList(pVnode, &tbFilterSize);
184,600✔
1279
  if (TSDB_CODE_SUCCESS != code) {
184,599✔
1280
    goto _exit;
14✔
1281
  }
1282
#endif
1283

1284
  if ((!tbFilterSize && vnodeGetStbIdList(pVnode, 0, suidList) < 0) ||
184,585!
1285
      (tbFilterSize && vnodeGetStbIdListByFilter(pVnode, 0, suidList, vnodeTimeSeriesFilter, pVnode) < 0)) {
184,586!
UNCOV
1286
    qError("vgId:%d, failed to get stb id list error: %s", TD_VID(pVnode), terrstr());
×
UNCOV
1287
    taosArrayDestroy(suidList);
×
UNCOV
1288
    return terrno;
×
1289
  }
1290

1291
  *num = 0;
184,586✔
1292
  int64_t arrSize = taosArrayGetSize(suidList);
184,586✔
1293
  for (int64_t i = 0; i < arrSize; ++i) {
370,729✔
1294
    tb_uid_t suid = *(tb_uid_t *)taosArrayGet(suidList, i);
186,139✔
1295

1296
    int64_t ctbNum = 0;
186,134✔
1297
    int32_t numOfCols = 0;
186,134✔
1298
    int8_t  flags = 0;
186,134✔
1299
    code = metaGetStbStats(pVnode, suid, &ctbNum, &numOfCols, &flags);
186,134✔
1300
    if (TSDB_CODE_SUCCESS != code) {
186,143!
UNCOV
1301
      goto _exit;
×
1302
    }
1303
    if (!TABLE_IS_VIRTUAL(flags)) {
186,143✔
1304
      *num += ctbNum * (numOfCols - 1);
185,885✔
1305
    }
1306
  }
1307

1308
_exit:
184,590✔
1309
  taosArrayDestroy(suidList);
184,604✔
1310
  return TSDB_CODE_SUCCESS;
184,597✔
1311
}
1312

1313
int32_t vnodeGetAllCtbNum(SVnode *pVnode, int64_t *num) {
×
1314
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, 0);
×
UNCOV
1315
  if (!pCur) {
×
UNCOV
1316
    return terrno;
×
1317
  }
1318

UNCOV
1319
  *num = 0;
×
UNCOV
1320
  while (1) {
×
UNCOV
1321
    tb_uid_t id = metaStbCursorNext(pCur);
×
UNCOV
1322
    if (id == 0) {
×
UNCOV
1323
      break;
×
1324
    }
1325

UNCOV
1326
    int64_t ctbNum = 0;
×
1327
    int32_t code = vnodeGetCtbNum(pVnode, id, &ctbNum);
×
UNCOV
1328
    if (TSDB_CODE_SUCCESS != code) {
×
UNCOV
1329
      metaCloseStbCursor(pCur);
×
UNCOV
1330
      return code;
×
1331
    }
1332

UNCOV
1333
    *num += ctbNum;
×
1334
  }
1335

UNCOV
1336
  metaCloseStbCursor(pCur);
×
UNCOV
1337
  return TSDB_CODE_SUCCESS;
×
1338
}
1339

1340
void *vnodeGetIdx(void *pVnode) {
47,807✔
1341
  if (pVnode == NULL) {
47,807!
1342
    return NULL;
×
1343
  }
1344

1345
  return metaGetIdx(((SVnode *)pVnode)->pMeta);
47,807✔
1346
}
1347

1348
void *vnodeGetIvtIdx(void *pVnode) {
47,758✔
1349
  if (pVnode == NULL) {
47,758!
UNCOV
1350
    return NULL;
×
1351
  }
1352
  return metaGetIvtIdx(((SVnode *)pVnode)->pMeta);
47,758✔
1353
}
1354

1355
int32_t vnodeGetTableSchema(void *pVnode, int64_t uid, STSchema **pSchema, int64_t *suid, SSchemaWrapper **pTagSchema) {
130✔
1356
  return tsdbGetTableSchema(((SVnode *)pVnode)->pMeta, uid, pSchema, suid, pTagSchema);
130✔
1357
}
1358

1359
static FORCE_INLINE int32_t vnodeGetDBPrimaryInfo(SVnode *pVnode, SDbSizeStatisInfo *pInfo) {
1360
  int32_t code = 0;
34,138✔
1361
  char    path[TSDB_FILENAME_LEN] = {0};
34,138✔
1362

1363
  char   *dirName[] = {VNODE_TSDB_DIR, VNODE_WAL_DIR, VNODE_META_DIR, VNODE_TSDB_CACHE_DIR};
34,138✔
1364
  int64_t dirSize[4];
1365

1366
  vnodeGetPrimaryDir(pVnode->path, pVnode->diskPrimary, pVnode->pTfs, path, TSDB_FILENAME_LEN);
34,138✔
1367
  int32_t offset = strlen(path);
34,138✔
1368

1369
  for (int i = 0; i < sizeof(dirName) / sizeof(dirName[0]); i++) {
170,685✔
1370
    int64_t size = {0};
136,548✔
1371
    (void)snprintf(path + offset, TSDB_FILENAME_LEN, "%s%s", TD_DIRSEP, dirName[i]);
136,548✔
1372
    code = taosGetDirSize(path, &size);
136,548✔
1373
    if (code != 0) {
136,550✔
1374
      return code;
3✔
1375
    }
1376
    path[offset] = 0;
136,547✔
1377
    dirSize[i] = size;
136,547✔
1378
  }
1379

1380
  pInfo->l1Size = 0;
34,137✔
1381
  pInfo->walSize = dirSize[1];
34,137✔
1382
  pInfo->metaSize = dirSize[2];
34,137✔
1383
  pInfo->cacheSize = dirSize[3];
34,137✔
1384
  return code;
34,137✔
1385
}
1386
int32_t vnodeGetDBSize(void *pVnode, SDbSizeStatisInfo *pInfo) {
34,138✔
1387
  int32_t code = 0;
34,138✔
1388
  int32_t lino = 0;
34,138✔
1389
  SVnode *pVnodeObj = pVnode;
34,138✔
1390
  if (pVnodeObj == NULL) {
34,138!
UNCOV
1391
    return TSDB_CODE_VND_NOT_EXIST;
×
1392
  }
1393
  code = vnodeGetDBPrimaryInfo(pVnode, pInfo);
34,140✔
1394
  if (code != 0) goto _exit;
34,140✔
1395

1396
  code = tsdbGetFsSize(pVnodeObj->pTsdb, pInfo);
34,137✔
1397
_exit:
34,139✔
1398
  return code;
34,139✔
1399
}
1400

1401
int32_t vnodeGetStreamProgress(SVnode *pVnode, SRpcMsg *pMsg, bool direct) {
3,436✔
1402
  int32_t            code = 0;
3,436✔
1403
  SStreamProgressReq req;
1404
  SStreamProgressRsp rsp = {0};
3,436✔
1405
  SRpcMsg            rpcMsg = {.info = pMsg->info, .code = 0};
3,436✔
1406
  char              *buf = NULL;
3,436✔
1407
  int32_t            rspLen = 0;
3,436✔
1408
  code = tDeserializeStreamProgressReq(pMsg->pCont, pMsg->contLen, &req);
3,436✔
1409

1410
  if (code == TSDB_CODE_SUCCESS) {
3,436!
1411
    rsp.fetchIdx = req.fetchIdx;
3,436✔
1412
    rsp.subFetchIdx = req.subFetchIdx;
3,436✔
1413
    rsp.vgId = req.vgId;
3,436✔
1414
    rsp.streamId = req.streamId;
3,436✔
1415
    rspLen = tSerializeStreamProgressRsp(0, 0, &rsp);
3,436✔
1416
    if (rspLen < 0) {
3,436!
1417
      code = terrno;
×
UNCOV
1418
      goto _OVER;
×
1419
    }
1420
    if (direct) {
3,436!
UNCOV
1421
      buf = rpcMallocCont(rspLen);
×
1422
    } else {
1423
      buf = taosMemoryCalloc(1, rspLen);
3,436!
1424
    }
1425
    if (!buf) {
3,436!
UNCOV
1426
      code = terrno;
×
UNCOV
1427
      goto _OVER;
×
1428
    }
1429
  }
1430

1431
  if (code == TSDB_CODE_SUCCESS) {
3,436!
1432
    code = tqGetStreamExecInfo(pVnode, req.streamId, &rsp.progressDelay, &rsp.fillHisFinished);
3,436✔
1433
  }
1434
  if (code == TSDB_CODE_SUCCESS) {
3,436!
1435
    rspLen = tSerializeStreamProgressRsp(buf, rspLen, &rsp);
3,436✔
1436
    if (rspLen < 0) {
3,436!
UNCOV
1437
      code = terrno;
×
UNCOV
1438
      goto _OVER;
×
1439
    }
1440
    rpcMsg.pCont = buf;
3,436✔
1441
    buf = NULL;
3,436✔
1442
    rpcMsg.contLen = rspLen;
3,436✔
1443
    rpcMsg.code = code;
3,436✔
1444
    rpcMsg.msgType = pMsg->msgType;
3,436✔
1445
    if (direct) {
3,436!
UNCOV
1446
      tmsgSendRsp(&rpcMsg);
×
1447
    } else {
1448
      *pMsg = rpcMsg;
3,436✔
1449
    }
1450
  }
1451

1452
_OVER:
×
1453
  if (buf) {
3,436!
UNCOV
1454
    taosMemoryFree(buf);
×
1455
  }
1456
  return code;
3,436✔
1457
}
1458

1459
/*
1460
 * Get raw write metrics for a vnode
1461
 */
UNCOV
1462
int32_t vnodeGetRawWriteMetrics(void *pVnode, SRawWriteMetrics *pRawMetrics) {
×
1463
  if (pVnode == NULL || pRawMetrics == NULL) {
×
1464
    return TSDB_CODE_INVALID_PARA;
×
1465
  }
1466

UNCOV
1467
  SVnode      *pVnode1 = (SVnode *)pVnode;
×
UNCOV
1468
  SSyncMetrics syncMetrics = syncGetMetrics(pVnode1->sync);
×
1469

1470
  // Copy values following SRawWriteMetrics structure order
UNCOV
1471
  pRawMetrics->total_requests = atomic_load_64(&pVnode1->writeMetrics.total_requests);
×
1472
  pRawMetrics->total_rows = atomic_load_64(&pVnode1->writeMetrics.total_rows);
×
UNCOV
1473
  pRawMetrics->total_bytes = atomic_load_64(&pVnode1->writeMetrics.total_bytes);
×
UNCOV
1474
  pRawMetrics->fetch_batch_meta_time = atomic_load_64(&pVnode1->writeMetrics.fetch_batch_meta_time);
×
UNCOV
1475
  pRawMetrics->fetch_batch_meta_count = atomic_load_64(&pVnode1->writeMetrics.fetch_batch_meta_count);
×
UNCOV
1476
  pRawMetrics->preprocess_time = atomic_load_64(&pVnode1->writeMetrics.preprocess_time);
×
UNCOV
1477
  pRawMetrics->wal_write_bytes = atomic_load_64(&syncMetrics.wal_write_bytes);
×
1478
  pRawMetrics->wal_write_time = atomic_load_64(&syncMetrics.wal_write_time);
×
UNCOV
1479
  pRawMetrics->apply_bytes = atomic_load_64(&pVnode1->writeMetrics.apply_bytes);
×
1480
  pRawMetrics->apply_time = atomic_load_64(&pVnode1->writeMetrics.apply_time);
×
UNCOV
1481
  pRawMetrics->commit_count = atomic_load_64(&pVnode1->writeMetrics.commit_count);
×
UNCOV
1482
  pRawMetrics->commit_time = atomic_load_64(&pVnode1->writeMetrics.commit_time);
×
UNCOV
1483
  pRawMetrics->memtable_wait_time = atomic_load_64(&pVnode1->writeMetrics.memtable_wait_time);
×
UNCOV
1484
  pRawMetrics->blocked_commit_count = atomic_load_64(&pVnode1->writeMetrics.blocked_commit_count);
×
UNCOV
1485
  pRawMetrics->blocked_commit_time = atomic_load_64(&pVnode1->writeMetrics.block_commit_time);
×
UNCOV
1486
  pRawMetrics->merge_count = atomic_load_64(&pVnode1->writeMetrics.merge_count);
×
UNCOV
1487
  pRawMetrics->merge_time = atomic_load_64(&pVnode1->writeMetrics.merge_time);
×
1488
  pRawMetrics->last_cache_commit_time = atomic_load_64(&pVnode1->writeMetrics.last_cache_commit_time);
×
1489
  pRawMetrics->last_cache_commit_count = atomic_load_64(&pVnode1->writeMetrics.last_cache_commit_count);
×
1490

UNCOV
1491
  return 0;
×
1492
}
1493

1494
/*
1495
 * Reset raw write metrics for a vnode by subtracting old values
1496
 */
1497
int32_t vnodeResetRawWriteMetrics(void *pVnode, const SRawWriteMetrics *pOldMetrics) {
×
1498
  if (pVnode == NULL || pOldMetrics == NULL) {
×
1499
    return TSDB_CODE_INVALID_PARA;
×
1500
  }
1501

1502
  SVnode *pVnode1 = (SVnode *)pVnode;
×
1503

1504
  // Reset vnode write metrics using atomic operations to subtract old values
1505
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.total_requests, pOldMetrics->total_requests);
×
1506
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.total_rows, pOldMetrics->total_rows);
×
1507
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.total_bytes, pOldMetrics->total_bytes);
×
1508

1509
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.fetch_batch_meta_time, pOldMetrics->fetch_batch_meta_time);
×
1510
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.fetch_batch_meta_count, pOldMetrics->fetch_batch_meta_count);
×
1511
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.preprocess_time, pOldMetrics->preprocess_time);
×
1512
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.apply_bytes, pOldMetrics->apply_bytes);
×
1513
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.apply_time, pOldMetrics->apply_time);
×
1514
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.commit_count, pOldMetrics->commit_count);
×
1515

UNCOV
1516
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.commit_time, pOldMetrics->commit_time);
×
1517
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.merge_time, pOldMetrics->merge_time);
×
1518

UNCOV
1519
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.memtable_wait_time, pOldMetrics->memtable_wait_time);
×
UNCOV
1520
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.blocked_commit_count, pOldMetrics->blocked_commit_count);
×
UNCOV
1521
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.block_commit_time, pOldMetrics->blocked_commit_time);
×
UNCOV
1522
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.merge_count, pOldMetrics->merge_count);
×
1523

1524
  // Reset new cache metrics
1525
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.last_cache_commit_time, pOldMetrics->last_cache_commit_time);
×
UNCOV
1526
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.last_cache_commit_count, pOldMetrics->last_cache_commit_count);
×
1527

1528
  // Reset sync metrics
UNCOV
1529
  SSyncMetrics syncMetrics = {
×
UNCOV
1530
      .wal_write_bytes = pOldMetrics->wal_write_bytes,
×
1531
      .wal_write_time = pOldMetrics->wal_write_time,
×
1532
  };
1533
  syncResetMetrics(pVnode1->sync, &syncMetrics);
×
1534

1535
  return 0;
×
1536
}
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