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

taosdata / TDengine / #4672

14 Aug 2025 01:04PM UTC coverage: 59.715% (+2.2%) from 57.533%
#4672

push

travis-ci

web-flow
fix(query): fix order by column check of union operator (#32524)

137065 of 292055 branches covered (46.93%)

Branch coverage included in aggregate %.

1 of 13 new or added lines in 1 file covered. (7.69%)

20958 existing lines in 205 files now uncovered.

207155 of 284385 relevant lines covered (72.84%)

20501687.34 hits per line

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

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

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

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

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

53
void vnodePrintTableMeta(STableMetaRsp *pMeta) {
3,459,584✔
54
  if (!(qDebugFlag & DEBUG_DEBUG)) {
3,459,584✔
55
    return;
3,385,036✔
56
  }
57

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

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

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

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

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

130
  if (!reqTbUid) {
3,513,574✔
131
    (void)tsnprintf(tableFName, TSDB_TABLE_FNAME_LEN, "%s.%s", infoReq.dbFName, infoReq.tbName);
3,510,262✔
132
    if (pVnode->mounted) tTrimMountPrefix(tableFName);
3,512,007✔
133
    code = vnodeValidateTableHash(pVnode, tableFName);
3,512,007✔
134
    if (code) {
3,510,806!
135
      goto _exit4;
×
136
    }
137
  }
138

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

162
  metaRsp.tableType = mer1.me.type;
3,460,730✔
163
  metaRsp.vgId = TD_VID(pVnode);
3,460,730✔
164
  metaRsp.tuid = mer1.me.uid;
3,460,730✔
165

166
  switch (mer1.me.type) {
3,460,730!
167
    case TSDB_SUPER_TABLE: {
2,348,383✔
168
      (void)strcpy(metaRsp.stbName, mer1.me.name);
2,348,383✔
169
      schema = mer1.me.stbEntry.schemaRow;
2,348,383✔
170
      schemaTag = mer1.me.stbEntry.schemaTag;
2,348,383✔
171
      metaRsp.suid = mer1.me.uid;
2,348,383✔
172
      break;
2,348,383✔
173
    }
174
    case TSDB_CHILD_TABLE:
1,037,617✔
175
    case TSDB_VIRTUAL_CHILD_TABLE:{
176
      metaReaderDoInit(&mer2, pVnode->pMeta, META_READER_NOLOCK);
1,037,617✔
177
      if (metaReaderGetTableEntryByUid(&mer2, mer1.me.ctbEntry.suid) < 0) goto _exit2;
1,037,620!
178

179
      (void)strcpy(metaRsp.stbName, mer2.me.name);
1,037,623✔
180
      metaRsp.suid = mer2.me.uid;
1,037,623✔
181
      schema = mer2.me.stbEntry.schemaRow;
1,037,623✔
182
      schemaTag = mer2.me.stbEntry.schemaTag;
1,037,623✔
183
      break;
1,037,623✔
184
    }
185
    case TSDB_NORMAL_TABLE:
74,730✔
186
    case TSDB_VIRTUAL_NORMAL_TABLE: {
187
      schema = mer1.me.ntbEntry.schemaRow;
74,730✔
188
      break;
74,730✔
189
    }
190
    default: {
×
191
      vError("vnodeGetTableMeta get invalid table type:%d", mer1.me.type);
×
192
      goto _exit3;
×
193
    }
194
  }
195

196
  metaRsp.numOfTags = schemaTag.nCols;
3,460,736✔
197
  metaRsp.numOfColumns = schema.nCols;
3,460,736✔
198
  metaRsp.precision = pVnode->config.tsdbCfg.precision;
3,460,736✔
199
  metaRsp.sversion = schema.version;
3,460,736✔
200
  metaRsp.tversion = schemaTag.version;
3,460,736✔
201
  metaRsp.pSchemas = (SSchema *)taosMemoryMalloc(sizeof(SSchema) * (metaRsp.numOfColumns + metaRsp.numOfTags));
3,460,736!
202
  metaRsp.pSchemaExt = (SSchemaExt *)taosMemoryCalloc(metaRsp.numOfColumns, sizeof(SSchemaExt));
3,460,362!
203
  if (NULL == metaRsp.pSchemas || NULL == metaRsp.pSchemaExt) {
3,459,239!
UNCOV
204
    code = terrno;
×
205
    goto _exit;
×
206
  }
207
  (void)memcpy(metaRsp.pSchemas, schema.pSchema, sizeof(SSchema) * schema.nCols);
3,459,321✔
208
  if (schemaTag.nCols) {
3,459,321✔
209
    (void)memcpy(metaRsp.pSchemas + schema.nCols, schemaTag.pSchema, sizeof(SSchema) * schemaTag.nCols);
3,383,809✔
210
  }
211
  if (metaRsp.pSchemaExt) {
3,459,321✔
212
    SMetaReader *pReader = mer1.me.type == TSDB_CHILD_TABLE ? &mer2 : &mer1;
3,458,648✔
213
    code = fillTableColCmpr(pReader, metaRsp.pSchemaExt, metaRsp.numOfColumns);
3,458,648✔
214
    if (code < 0) {
3,459,846!
215
      goto _exit;
×
216
    }
217
    for (int32_t i = 0; i < metaRsp.numOfColumns && pReader->me.pExtSchemas; i++) {
3,529,995✔
218
      metaRsp.pSchemaExt[i].typeMod = pReader->me.pExtSchemas[i].typeMod;
70,149✔
219
    }
220
  } else {
221
    code = TSDB_CODE_OUT_OF_MEMORY;
673✔
222
    goto _exit;
673✔
223
  }
224
  if (hasRefCol(mer1.me.type)) {
3,459,846✔
225
    metaRsp.rversion = mer1.me.colRef.version;
420✔
226
    metaRsp.pColRefs = (SColRef*)taosMemoryMalloc(sizeof(SColRef) * metaRsp.numOfColumns);
420!
227
    if (metaRsp.pColRefs) {
123!
228
      code = fillTableColRef(&mer1, metaRsp.pColRefs, metaRsp.numOfColumns);
123✔
229
      if (code < 0) {
123!
230
        goto _exit;
×
231
      }
232
    }
233
    metaRsp.numOfColRefs = metaRsp.numOfColumns;
123✔
234
  } else {
235
    metaRsp.pColRefs = NULL;
3,459,418✔
236
    metaRsp.numOfColRefs = 0;
3,459,418✔
237
  }
238

239
  vnodePrintTableMeta(&metaRsp);
3,459,541✔
240

241
  // encode and send response
242
  rspLen = tSerializeSTableMetaRsp(NULL, 0, &metaRsp);
3,459,406✔
243
  if (rspLen < 0) {
3,458,931!
244
    code = terrno;
×
245
    goto _exit;
×
246
  }
247

248
  if (direct) {
3,458,931✔
249
    pRsp = rpcMallocCont(rspLen);
746✔
250
  } else {
251
    pRsp = taosMemoryCalloc(1, rspLen);
3,458,185!
252
  }
253

254
  if (pRsp == NULL) {
3,458,383!
255
    code = terrno;
×
256
    goto _exit;
×
257
  }
258

259
  rspLen = tSerializeSTableMetaRsp(pRsp, rspLen, &metaRsp);
3,458,383✔
260
  if (rspLen < 0) {
3,460,252!
261
    code = terrno;
×
262
    goto _exit;
×
263
  }
264

265
_exit:
3,460,252✔
266
  taosMemoryFree(metaRsp.pColRefs);
3,460,925!
267
  taosMemoryFree(metaRsp.pSchemas);
3,458,664!
268
  taosMemoryFree(metaRsp.pSchemaExt);
3,460,268!
269
_exit2:
3,460,258✔
270
  metaReaderClear(&mer2);
3,460,258✔
271
_exit3:
3,513,078✔
272
  metaReaderClear(&mer1);
3,513,078✔
273
_exit4:
3,513,670✔
274
  rpcMsg.info = pMsg->info;
3,513,670✔
275
  rpcMsg.pCont = pRsp;
3,513,670✔
276
  rpcMsg.contLen = rspLen;
3,513,670✔
277
  rpcMsg.code = code;
3,513,670✔
278
  rpcMsg.msgType = pMsg->msgType;
3,513,670✔
279

280
  if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST && autoCreateCtb == 1) {
3,513,670✔
281
    code = TSDB_CODE_SUCCESS;
39,982✔
282
  }
283

284
  if (code) {
3,513,670✔
285
    qError("get table %s meta with %" PRIu8 " failed cause of %s", infoReq.tbName, infoReq.option, tstrerror(code));
13,354!
286
  }
287

288
  if (direct) {
3,512,975✔
289
    tmsgSendRsp(&rpcMsg);
5,116✔
290
  } else {
291
    *pMsg = rpcMsg;
3,507,859✔
292
  }
293

294
  return code;
3,512,893✔
295
}
296

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

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

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

319
  (void)tsnprintf(tableFName, TSDB_TABLE_FNAME_LEN, "%s.%s", cfgReq.dbFName, cfgReq.tbName);
261✔
320
  if (pVnode->mounted) tTrimMountPrefix(tableFName);
261✔
321
  code = vnodeValidateTableHash(pVnode, tableFName);
261✔
322
  if (code) {
261!
323
    goto _exit;
×
324
  }
325

326
  // query meta
327
  metaReaderDoInit(&mer1, pVnode->pMeta, META_READER_LOCK);
261✔
328

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

334
  cfgRsp.tableType = mer1.me.type;
261✔
335

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

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

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

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

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

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

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

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

434
  if (direct) {
261!
435
    pRsp = rpcMallocCont(rspLen);
×
436
  } else {
437
    pRsp = taosMemoryCalloc(1, rspLen);
261!
438
  }
439

440
  if (pRsp == NULL) {
261!
441
    code = terrno;
×
442
    goto _exit;
×
443
  }
444

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

451
_exit:
261✔
452
  rpcMsg.info = pMsg->info;
261✔
453
  rpcMsg.pCont = pRsp;
261✔
454
  rpcMsg.contLen = rspLen;
261✔
455
  rpcMsg.code = code;
261✔
456
  rpcMsg.msgType = pMsg->msgType;
261✔
457

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

462
  if (direct) {
261!
463
    tmsgSendRsp(&rpcMsg);
×
464
  } else {
465
    *pMsg = rpcMsg;
261✔
466
  }
467

468
  tFreeSTableCfgRsp(&cfgRsp);
261✔
469
  metaReaderClear(&mer2);
261✔
470
  metaReaderClear(&mer1);
261✔
471
  return code;
261✔
472
}
473

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

479
  SBatchRspMsg *pRsp = (SBatchRspMsg *)p;
480
  rpcFreeCont(pRsp->msg);
481
}
482

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

494
  if (tDeserializeSBatchReq(pMsg->pCont, pMsg->contLen, &batchReq)) {
1,964,429!
495
    code = terrno;
×
496
    qError("tDeserializeSBatchReq failed");
×
497
    goto _exit;
×
498
  }
499

500
  int32_t msgNum = taosArrayGetSize(batchReq.pMsgs);
1,965,883✔
501
  if (msgNum >= MAX_META_MSG_IN_BATCH) {
1,964,221!
502
    code = TSDB_CODE_INVALID_MSG;
×
503
    qError("too many msgs %d in vnode batch meta req", msgNum);
×
504
    goto _exit;
×
505
  }
506

507
  batchRsp.pRsps = taosArrayInit(msgNum, sizeof(SBatchRspMsg));
1,964,221✔
508
  if (NULL == batchRsp.pRsps) {
1,965,557✔
509
    code = terrno;
113✔
510
    qError("taosArrayInit %d SBatchRspMsg failed", msgNum);
×
511
    goto _exit;
×
512
  }
513

514
  for (int32_t i = 0; i < msgNum; ++i) {
5,472,390✔
515
    req = taosArrayGet(batchReq.pMsgs, i);
3,507,940✔
516
    if (req == NULL) {
3,504,189!
UNCOV
517
      code = terrno;
×
518
      goto _exit;
×
519
    }
520

521
    reqMsg.msgType = req->msgType;
3,504,313✔
522
    reqMsg.pCont = req->msg;
3,504,313✔
523
    reqMsg.contLen = req->msgLen;
3,504,313✔
524

525
    switch (req->msgType) {
3,504,313!
526
      case TDMT_VND_TABLE_META:
3,502,330✔
527
        // error code has been set into reqMsg, no need to handle it here.
528
        if (TSDB_CODE_SUCCESS != vnodeGetTableMeta(pVnode, &reqMsg, false)) {
3,502,330✔
529
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
8,300!
530
        }
531
        break;
3,506,007✔
532
      case TDMT_VND_TABLE_NAME:
1,692✔
533
        // error code has been set into reqMsg, no need to handle it here.
534
        if (TSDB_CODE_SUCCESS != vnodeGetTableMeta(pVnode, &reqMsg, false)) {
1,692✔
535
          qWarn("vnodeGetBatchName failed, msgType:%d", req->msgType);
686!
536
        }
537
        break;
1,692✔
538
      case TDMT_VND_TABLE_CFG:
261✔
539
        // error code has been set into reqMsg, no need to handle it here.
540
        if (TSDB_CODE_SUCCESS != vnodeGetTableCfg(pVnode, &reqMsg, false)) {
261!
541
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
×
542
        }
543
        break;
261✔
544
      case TDMT_VND_VSUBTABLES_META:
×
545
        // error code has been set into reqMsg, no need to handle it here.
546
        if (TSDB_CODE_SUCCESS != vnodeGetVSubtablesMeta(pVnode, &reqMsg)) {
×
547
          qWarn("vnodeGetVSubtablesMeta failed, msgType:%d", req->msgType);
×
548
        }
549
        break;
×
550
      case TDMT_VND_VSTB_REF_DBS:
30✔
551
        // error code has been set into reqMsg, no need to handle it here.
552
        if (TSDB_CODE_SUCCESS != vnodeGetVStbRefDbs(pVnode, &reqMsg)) {
30!
553
          qWarn("vnodeGetVStbRefDbs failed, msgType:%d", req->msgType);
×
554
        }
555
        break;
30✔
556
      default:
×
557
        qError("invalid req msgType %d", req->msgType);
×
558
        reqMsg.code = TSDB_CODE_INVALID_MSG;
×
559
        reqMsg.pCont = NULL;
×
560
        reqMsg.contLen = 0;
×
561
        break;
×
562
    }
563

564
    rsp.msgIdx = req->msgIdx;
3,507,990✔
565
    rsp.reqType = reqMsg.msgType;
3,507,990✔
566
    rsp.msgLen = reqMsg.contLen;
3,507,990✔
567
    rsp.rspCode = reqMsg.code;
3,507,990✔
568
    rsp.msg = reqMsg.pCont;
3,507,990✔
569

570
    if (NULL == taosArrayPush(batchRsp.pRsps, &rsp)) {
7,014,936!
571
      qError("taosArrayPush failed");
×
572
      code = terrno;
×
573
      goto _exit;
×
574
    }
575
  }
576

577
  rspSize = tSerializeSBatchRsp(NULL, 0, &batchRsp);
1,964,450✔
578
  if (rspSize < 0) {
1,966,118!
579
    qError("tSerializeSBatchRsp failed");
×
580
    code = terrno;
×
581
    goto _exit;
×
582
  }
583
  pRsp = rpcMallocCont(rspSize);
1,966,118✔
584
  if (pRsp == NULL) {
1,966,130!
585
    qError("rpcMallocCont %d failed", rspSize);
×
586
    code = terrno;
×
587
    goto _exit;
×
588
  }
589
  if (tSerializeSBatchRsp(pRsp, rspSize, &batchRsp) < 0) {
1,966,130!
590
    qError("tSerializeSBatchRsp %d failed", rspSize);
×
591
    code = terrno;
×
592
    goto _exit;
×
593
  }
594

595
_exit:
1,966,201✔
596

597
  rspMsg.info = pMsg->info;
1,966,201✔
598
  rspMsg.pCont = pRsp;
1,966,201✔
599
  rspMsg.contLen = rspSize;
1,966,201✔
600
  rspMsg.code = code;
1,966,201✔
601
  rspMsg.msgType = pMsg->msgType;
1,966,201✔
602

603
  if (code) {
1,966,201!
604
    qError("vnd get batch meta failed cause of %s", tstrerror(code));
×
605
  }
606

607
  taosArrayDestroyEx(batchReq.pMsgs, tFreeSBatchReqMsg);
1,966,201✔
608
  taosArrayDestroyEx(batchRsp.pRsps, tFreeSBatchRspMsg);
1,966,127✔
609

610
  tmsgSendRsp(&rspMsg);
1,966,130✔
611

612
  return code;
1,966,227✔
613
}
614

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

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

632
  SArray *pList = taosArrayInit(10, sizeof(uint64_t));
×
633
  QUERY_CHECK_NULL(pList, code, line, _return, terrno);
×
634

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

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

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

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

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

663
    pTb = taosMemoryCalloc(1, refColsNum * sizeof(SRefColInfo) + sizeof(*pTb));
×
664
    QUERY_CHECK_NULL(pTb, code, line, _return, terrno);
×
665

666
    pTb->uid = mr.me.uid;
×
667
    pTb->numOfColRefs = refColsNum;
×
668
    pTb->refCols = (SRefColInfo*)(pTb + 1);
×
669

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

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

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

684
      if (NULL == tSimpleHashGet(pSrcTbls, tbFName, strlen(tbFName))) {
×
685
        QUERY_CHECK_CODE(tSimpleHashPut(pSrcTbls, tbFName, strlen(tbFName), &code, sizeof(code)), line, _return);
×
686
      }
687

688
      refColsNum++;
×
689
    }
690

691
    pTb->numOfSrcTbls = tSimpleHashGetSize(pSrcTbls);
×
692
    QUERY_CHECK_NULL(taosArrayPush(*ppRes, &pTb), code, line, _return, terrno);
×
693
    pTb = NULL;
×
694

695
    pHandle->api.metaReaderFn.clearReader(&mr);
×
696
    readerInit = false;
×
697
  }
698

699
_return:
×
700

701
  if (readerInit) {
×
702
    pHandle->api.metaReaderFn.clearReader(&mr);
×
703
  }
704

705
  taosArrayDestroy(pList);
×
706
  taosMemoryFree(pTb);
×
707
  tSimpleHashCleanup(pSrcTbls);
×
708

709
  if (code) {
×
710
    qError("%s failed since %s", __func__, tstrerror(code));
×
711
  }
712
  return code;
×
713
}
714

715
int32_t vnodeReadVStbRefDbs(SReadHandle* pHandle, int64_t suid, SArray** ppRes) {
30✔
716
  int32_t                    code = TSDB_CODE_SUCCESS;
30✔
717
  int32_t                    line = 0;
30✔
718
  SMetaReader                mr = {0};
30✔
719
  bool                       readerInit = false;
30✔
720
  SSHashObj*                 pDbNameHash = NULL;
30✔
721
  SArray*                    pList = NULL;
30✔
722

723
  pList = taosArrayInit(10, sizeof(uint64_t));
30✔
724
  QUERY_CHECK_NULL(pList, code, line, _return, terrno);
30!
725

726
  *ppRes = taosArrayInit(10, POINTER_BYTES);
30✔
727
  QUERY_CHECK_NULL(*ppRes, code, line, _return, terrno)
30!
728
  
729
  // lookup in cache
730
  code = pHandle->api.metaFn.metaGetCachedRefDbs(pHandle->vnode, suid, *ppRes);
30✔
731
  QUERY_CHECK_CODE(code, line, _return);
30!
732

733
  if (taosArrayGetSize(*ppRes) > 0) {
30✔
734
    // found in cache
735
    goto _return;
6✔
736
  } else {
737
    code = pHandle->api.metaFn.getChildTableList(pHandle->vnode, suid, pList);
24✔
738
    QUERY_CHECK_CODE(code, line, _return);
24!
739

740
    size_t num = taosArrayGetSize(pList);
24✔
741
    pDbNameHash = tSimpleHashInit(10, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
24✔
742
    QUERY_CHECK_NULL(pDbNameHash, code, line, _return, terrno);
24!
743

744
    for (int32_t i = 0; i < num; ++i) {
89✔
745
      uint64_t* id = taosArrayGet(pList, i);
65✔
746
      QUERY_CHECK_NULL(id, code, line, _return, terrno);
65!
747

748
      pHandle->api.metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pHandle->api.metaFn);
65✔
749
      readerInit = true;
65✔
750

751
      code = pHandle->api.metaReaderFn.getTableEntryByUid(&mr, *id);
65✔
752
      QUERY_CHECK_CODE(code, line, _return);
65!
753

754
      for (int32_t j = 0; j < mr.me.colRef.nCols; j++) {
431✔
755
        if (mr.me.colRef.pColRef[j].hasRef) {
366✔
756
          if (NULL == tSimpleHashGet(pDbNameHash, mr.me.colRef.pColRef[j].refDbName, strlen(mr.me.colRef.pColRef[j].refDbName))) {
237✔
757
            char *refDbName = taosStrdup(mr.me.colRef.pColRef[j].refDbName);
19!
758
            QUERY_CHECK_NULL(refDbName, code, line, _return, terrno);
18!
759

760
            QUERY_CHECK_NULL(taosArrayPush(*ppRes, &refDbName), code, line, _return, terrno);
36!
761

762
            code = tSimpleHashPut(pDbNameHash, refDbName, strlen(refDbName), NULL, 0);
18✔
763
            QUERY_CHECK_CODE(code, line, _return);
17!
764
          }
765
        }
766
      }
767

768
      pHandle->api.metaReaderFn.clearReader(&mr);
65✔
769
      readerInit = false;
65✔
770
    }
771

772
    code = pHandle->api.metaFn.metaPutRefDbsToCache(pHandle->vnode, suid, *ppRes);
24✔
773
    QUERY_CHECK_CODE(code, line, _return);
24!
774
  }
775

776
_return:
24✔
777

778
  if (readerInit) {
30!
779
    pHandle->api.metaReaderFn.clearReader(&mr);
×
780
  }
781

782
  taosArrayDestroy(pList);
30✔
783
  tSimpleHashCleanup(pDbNameHash);
30✔
784

785
  if (code) {
30!
786
    qError("%s failed since %s", __func__, tstrerror(code));
×
787
  }
788
  return code;
30✔
789
}
790

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

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

806
  SReadHandle handle = {0};
×
807
  handle.vnode = pVnode;
×
808
  initStorageAPI(&handle.api);
×
809

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

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

832
_return:
×
833

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

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

844
  *pMsg = rspMsg;
×
845
  
846
  tDestroySVSubTablesRsp(&rsp);
×
847

848
  //tmsgSendRsp(&rspMsg);
849

850
  return code;
×
851
}
852

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

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

868
  SReadHandle handle = {0};
30✔
869
  handle.vnode = pVnode;
30✔
870
  initStorageAPI(&handle.api);
30✔
871

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

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

895
_return:
30✔
896

897
  rspMsg.info = pMsg->info;
30✔
898
  rspMsg.pCont = pRsp;
30✔
899
  rspMsg.contLen = rspSize;
30✔
900
  rspMsg.code = code;
30✔
901
  rspMsg.msgType = pMsg->msgType;
30✔
902

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

907
  *pMsg = rspMsg;
30✔
908

909
  tDestroySVStbRefDbsRsp(&rsp);
30✔
910

911
  return code;
30✔
912
}
913

914
static int32_t vnodeGetCompStorage(SVnode *pVnode, int64_t *output) {
1,469,824✔
915
  int32_t code = 0;
1,469,824✔
916
#ifdef TD_ENTERPRISE
917
  int32_t now = taosGetTimestampSec();
1,469,824✔
918
  if (llabs(now - pVnode->config.vndStats.storageLastUpd) >= 30) {
1,469,824✔
919
    pVnode->config.vndStats.storageLastUpd = now;
57,213✔
920

921
    SDbSizeStatisInfo info = {0};
57,213✔
922
    if (0 == (code = vnodeGetDBSize(pVnode, &info))) {
57,213✔
923
      int64_t compSize =
51,056✔
924
          info.l1Size + info.l2Size + info.l3Size + info.cacheSize + info.walSize + info.metaSize + +info.ssSize;
51,056✔
925
      if (compSize >= 0) {
51,056!
926
        pVnode->config.vndStats.compStorage = compSize;
51,056✔
927
      } else {
928
        vError("vnode get comp storage failed since compSize is negative:%" PRIi64, compSize);
×
929
        code = TSDB_CODE_APP_ERROR;
×
930
      }
931
    } else {
932
      vWarn("vnode get comp storage failed since %s", tstrerror(code));
6,157!
933
    }
934
  }
935
  if (output) *output = pVnode->config.vndStats.compStorage;
1,469,824!
936
#endif
937
  return code;
1,469,824✔
938
}
939

940
static void vnodeGetBufferInfo(SVnode *pVnode, int64_t *bufferSegmentUsed, int64_t *bufferSegmentSize) {
1,469,824✔
941
  *bufferSegmentUsed = 0;
1,469,824✔
942
  *bufferSegmentSize = 0;
1,469,824✔
943
  if (pVnode) {
1,469,824!
944
    (void)taosThreadMutexLock(&pVnode->mutex);
1,469,824✔
945

946
    if (pVnode->inUse) {
1,469,824✔
947
      *bufferSegmentUsed = pVnode->inUse->size;
1,469,767✔
948
    }
949
    *bufferSegmentSize = pVnode->config.szBuf / VNODE_BUFPOOL_SEGMENTS;
1,469,824✔
950

951
    (void)taosThreadMutexUnlock(&pVnode->mutex);
1,469,824✔
952
  }
953
}
1,469,824✔
954

955
int32_t vnodeGetLoad(SVnode *pVnode, SVnodeLoad *pLoad) {
1,469,824✔
956
  SSyncState state = syncGetState(pVnode->sync);
1,469,824✔
957
  pLoad->syncAppliedIndex = pVnode->state.applied;
1,469,824✔
958
  syncGetCommitIndex(pVnode->sync, &pLoad->syncCommitIndex);
1,469,824✔
959

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

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

1007
void vnodeGetInfo(void *pVnode, const char **dbname, int32_t *vgId, int64_t *numOfTables, int64_t *numOfNormalTables) {
2,232,742✔
1008
  SVnode    *pVnodeObj = pVnode;
2,232,742✔
1009
  SVnodeCfg *pConf = &pVnodeObj->config;
2,232,742✔
1010

1011
  if (dbname) {
2,232,742!
1012
    *dbname = pConf->dbname;
2,234,425✔
1013
  }
1014

1015
  if (vgId) {
2,232,742!
1016
    *vgId = TD_VID(pVnodeObj);
2,233,512✔
1017
  }
1018

1019
  if (numOfTables) {
2,232,742!
1020
    *numOfTables = pConf->vndStats.numOfNTables + pConf->vndStats.numOfCTables;
×
1021
  }
1022

1023
  if (numOfNormalTables) {
2,232,742!
1024
    *numOfNormalTables = pConf->vndStats.numOfNTables;
×
1025
  }
1026
}
2,232,742✔
1027

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

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

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

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

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

1066
int32_t vnodeGetCtbIdList(void *pVnode, int64_t suid, SArray *list) {
4,219,279✔
1067
  int32_t      code = TSDB_CODE_SUCCESS;
4,219,279✔
1068
  SVnode      *pVnodeObj = pVnode;
4,219,279✔
1069
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnodeObj, suid, 1);
4,219,279✔
1070
  if (NULL == pCur) {
4,228,950!
1071
    qError("vnode get all table list failed");
×
1072
    return terrno;
×
1073
  }
1074

1075
  while (1) {
13,456,140✔
1076
    tb_uid_t id = metaCtbCursorNext(pCur);
17,685,090✔
1077
    if (id == 0) {
17,681,675✔
1078
      break;
4,230,512✔
1079
    }
1080

1081
    if (NULL == taosArrayPush(list, &id)) {
13,456,140!
1082
      qError("taosArrayPush failed");
×
1083
      code = terrno;
×
1084
      goto _exit;
×
1085
    }
1086
  }
1087

1088
_exit:
4,230,512✔
1089
  metaCloseCtbCursor(pCur);
4,230,512✔
1090
  return code;
4,231,234✔
1091
}
1092

1093
int32_t vnodeGetStbIdList(SVnode *pVnode, int64_t suid, SArray *list) {
339,475✔
1094
  int32_t      code = TSDB_CODE_SUCCESS;
339,475✔
1095
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, suid);
339,475✔
1096
  if (!pCur) {
339,474!
1097
    return TSDB_CODE_OUT_OF_MEMORY;
×
1098
  }
1099

1100
  while (1) {
425,713✔
1101
    tb_uid_t id = metaStbCursorNext(pCur);
765,187✔
1102
    if (id == 0) {
765,184✔
1103
      break;
339,475✔
1104
    }
1105

1106
    if (NULL == taosArrayPush(list, &id)) {
425,713!
1107
      qError("taosArrayPush failed");
×
1108
      code = terrno;
×
1109
      goto _exit;
×
1110
    }
1111
  }
1112

1113
_exit:
339,475✔
1114
  metaCloseStbCursor(pCur);
339,475✔
1115
  return code;
339,476✔
1116
}
1117

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

1126
  while (1) {
10✔
1127
    tb_uid_t id = metaStbCursorNext(pCur);
12✔
1128
    if (id == 0) {
12✔
1129
      break;
2✔
1130
    }
1131

1132
    if ((*filter) && (*filter)(arg, &id)) {
10!
1133
      continue;
10✔
1134
    }
1135

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

1143
_exit:
2✔
1144
  metaCloseStbCursor(pCur);
2✔
1145
  return code;
2✔
1146
}
1147

1148
int32_t vnodeGetCtbNum(SVnode *pVnode, int64_t suid, int64_t *num) {
28,523✔
1149
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, suid, 0);
28,523✔
1150
  if (!pCur) {
28,524✔
1151
    return terrno;
1✔
1152
  }
1153

1154
  *num = 0;
28,523✔
1155
  while (1) {
22,563✔
1156
    tb_uid_t id = metaCtbCursorNext(pCur);
51,086✔
1157
    if (id == 0) {
51,088✔
1158
      break;
28,525✔
1159
    }
1160

1161
    ++(*num);
22,563✔
1162
  }
1163

1164
  metaCloseCtbCursor(pCur);
28,525✔
1165
  return TSDB_CODE_SUCCESS;
28,527✔
1166
}
1167

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

1177
  return TSDB_CODE_SUCCESS;
28,526✔
1178
}
1179

1180
int32_t vnodeGetStbInfo(SVnode *pVnode, tb_uid_t suid, int64_t *keep, int8_t *flags) {
28,523✔
1181
  SMetaReader mr = {0};
28,523✔
1182
  metaReaderDoInit(&mr, pVnode->pMeta, META_READER_NOLOCK);
28,523✔
1183

1184
  int32_t code = metaReaderGetTableEntryByUid(&mr, suid);
28,524✔
1185
  if (code == TSDB_CODE_SUCCESS) {
28,522!
1186
    if (keep) *keep = mr.me.stbEntry.keep;
28,522!
1187
    if (flags) *flags = mr.me.flags;
28,522!
1188
  } else {
UNCOV
1189
    if (keep) *keep = 0;
×
UNCOV
1190
    if (flags) *flags = 0;
×
1191
  }
1192

1193
  metaReaderClear(&mr);
28,522✔
1194
  return TSDB_CODE_SUCCESS;
28,527✔
1195
}
1196

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

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

1229
  if (!(dbName = strchr(pVnode->config.dbname, '.'))) return 0;
339,484!
1230
  if (0 == strncmp(++dbName, "log", TSDB_DB_NAME_LEN)) {
339,484✔
1231
    tbNum = tkLogStbNum;
14✔
1232
    pTbArr = (const char **)&tkLogStb;
14✔
1233
  } else if (0 == strncmp(dbName, "audit", TSDB_DB_NAME_LEN)) {
339,470!
1234
    tbNum = tkAuditStbNum;
×
1235
    pTbArr = (const char **)&tkAuditStb;
×
1236
  }
1237
  if (tbNum && pTbArr) {
339,484!
1238
    *tbSize = metaSizeOfTbFilterCache(pVnode->pMeta, 0);
14✔
1239
    if (*tbSize < tbNum) {
14!
1240
      for (int32_t i = 0; i < tbNum; ++i) {
190✔
1241
        tb_uid_t suid = metaGetTableEntryUidByName(pVnode->pMeta, pTbArr[i]);
186✔
1242
        if (suid != 0) {
186✔
1243
          code = metaPutTbToFilterCache(pVnode->pMeta, &suid, 0);
20✔
1244
          if (TSDB_CODE_SUCCESS != code) {
20✔
1245
            return code;
10✔
1246
          }
1247
        }
1248
      }
1249
      *tbSize = metaSizeOfTbFilterCache(pVnode->pMeta, 0);
4✔
1250
    }
1251
  }
1252

1253
  return code;
339,474✔
1254
}
1255
#endif
1256

1257
static bool vnodeTimeSeriesFilter(void *arg1, void *arg2) {
10✔
1258
  SVnode *pVnode = (SVnode *)arg1;
10✔
1259

1260
  if (metaTbInFilterCache(pVnode->pMeta, arg2, 0)) {
10!
1261
    return true;
10✔
1262
  }
1263
  return false;
×
1264
}
1265

1266
int32_t vnodeGetTimeSeriesNum(SVnode *pVnode, int64_t *num) {
339,483✔
1267
  SArray *suidList = NULL;
339,483✔
1268

1269
  if (!(suidList = taosArrayInit(1, sizeof(tb_uid_t)))) {
339,483!
1270
    return terrno;
×
1271
  }
1272

1273
  int32_t tbFilterSize = 0;
339,484✔
1274
  int32_t code = TSDB_CODE_SUCCESS;
339,484✔
1275
#ifdef TD_ENTERPRISE
1276
  code = vnodeGetTimeSeriesBlackList(pVnode, &tbFilterSize);
339,484✔
1277
  if (TSDB_CODE_SUCCESS != code) {
339,483✔
1278
    goto _exit;
10✔
1279
  }
1280
#endif
1281

1282
  if ((!tbFilterSize && vnodeGetStbIdList(pVnode, 0, suidList) < 0) ||
339,473✔
1283
      (tbFilterSize && vnodeGetStbIdListByFilter(pVnode, 0, suidList, vnodeTimeSeriesFilter, pVnode) < 0)) {
339,473!
1284
    qError("vgId:%d, failed to get stb id list error: %s", TD_VID(pVnode), terrstr());
×
1285
    taosArrayDestroy(suidList);
×
1286
    return terrno;
×
1287
  }
1288

1289
  *num = 0;
339,474✔
1290
  int64_t arrSize = taosArrayGetSize(suidList);
339,474✔
1291
  for (int64_t i = 0; i < arrSize; ++i) {
765,190✔
1292
    tb_uid_t suid = *(tb_uid_t *)taosArrayGet(suidList, i);
425,713✔
1293

1294
    int64_t ctbNum = 0;
425,714✔
1295
    int32_t numOfCols = 0;
425,714✔
1296
    int8_t  flags = 0;
425,714✔
1297
    code = metaGetStbStats(pVnode, suid, &ctbNum, &numOfCols, &flags);
425,714✔
1298
    if (TSDB_CODE_SUCCESS != code) {
425,717!
1299
      goto _exit;
×
1300
    }
1301
    if (!TABLE_IS_VIRTUAL(flags)) {
425,717✔
1302
      *num += ctbNum * (numOfCols - 1);
424,922✔
1303
    }
1304
  }
1305

1306
_exit:
339,477✔
1307
  taosArrayDestroy(suidList);
339,487✔
1308
  return TSDB_CODE_SUCCESS;
339,484✔
1309
}
1310

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

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

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

1331
    *num += ctbNum;
×
1332
  }
1333

1334
  metaCloseStbCursor(pCur);
×
1335
  return TSDB_CODE_SUCCESS;
×
1336
}
1337

1338
void *vnodeGetIdx(void *pVnode) {
96,295✔
1339
  if (pVnode == NULL) {
96,295!
1340
    return NULL;
×
1341
  }
1342

1343
  return metaGetIdx(((SVnode *)pVnode)->pMeta);
96,295✔
1344
}
1345

1346
void *vnodeGetIvtIdx(void *pVnode) {
96,160✔
1347
  if (pVnode == NULL) {
96,160!
1348
    return NULL;
×
1349
  }
1350
  return metaGetIvtIdx(((SVnode *)pVnode)->pMeta);
96,160✔
1351
}
1352

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

1357
static FORCE_INLINE int32_t vnodeGetDBPrimaryInfo(SVnode *pVnode, SDbSizeStatisInfo *pInfo) {
1358
  int32_t code = 0;
57,317✔
1359
  char    path[TSDB_FILENAME_LEN] = {0};
57,317✔
1360

1361
  char   *dirName[] = {VNODE_TSDB_DIR, VNODE_WAL_DIR, VNODE_META_DIR, VNODE_TSDB_CACHE_DIR};
57,317✔
1362
  int64_t dirSize[4];
1363

1364
  vnodeGetPrimaryPath(pVnode, false, path, TSDB_FILENAME_LEN);
57,317✔
1365
  int32_t offset = strlen(path);
57,317✔
1366

1367
  for (int i = 0; i < sizeof(dirName) / sizeof(dirName[0]); i++) {
261,959✔
1368
    int64_t size = {0};
210,798✔
1369
    (void)snprintf(path + offset, TSDB_FILENAME_LEN - offset, "%s%s", TD_DIRSEP, dirName[i]);
210,798✔
1370
    code = taosGetDirSize(path, &size);
210,798✔
1371
    if (code != 0) {
210,799✔
1372
      return code;
6,157✔
1373
    }
1374
    path[offset] = 0;
204,642✔
1375
    dirSize[i] = size;
204,642✔
1376
  }
1377

1378
  pInfo->l1Size = 0;
51,161✔
1379
  pInfo->walSize = dirSize[1];
51,161✔
1380
  pInfo->metaSize = dirSize[2];
51,161✔
1381
  pInfo->cacheSize = dirSize[3];
51,161✔
1382
  return code;
51,161✔
1383
}
1384
int32_t vnodeGetDBSize(void *pVnode, SDbSizeStatisInfo *pInfo) {
57,317✔
1385
  int32_t code = 0;
57,317✔
1386
  int32_t lino = 0;
57,317✔
1387
  SVnode *pVnodeObj = pVnode;
57,317✔
1388
  if (pVnodeObj == NULL) {
57,317!
1389
    return TSDB_CODE_VND_NOT_EXIST;
×
1390
  }
1391
  code = vnodeGetDBPrimaryInfo(pVnode, pInfo);
57,318✔
1392
  if (code != 0) goto _exit;
57,318✔
1393

1394
  code = tsdbGetFsSize(pVnodeObj->pTsdb, pInfo);
51,161✔
1395
_exit:
57,317✔
1396
  return code;
57,317✔
1397
}
1398

1399
/*
1400
 * Get raw write metrics for a vnode
1401
 */
1402
int32_t vnodeGetRawWriteMetrics(void *pVnode, SRawWriteMetrics *pRawMetrics) {
×
1403
  if (pVnode == NULL || pRawMetrics == NULL) {
×
1404
    return TSDB_CODE_INVALID_PARA;
×
1405
  }
1406

1407
  SVnode      *pVnode1 = (SVnode *)pVnode;
×
1408
  SSyncMetrics syncMetrics = syncGetMetrics(pVnode1->sync);
×
1409

1410
  // Copy values following SRawWriteMetrics structure order
1411
  pRawMetrics->total_requests = atomic_load_64(&pVnode1->writeMetrics.total_requests);
×
1412
  pRawMetrics->total_rows = atomic_load_64(&pVnode1->writeMetrics.total_rows);
×
1413
  pRawMetrics->total_bytes = atomic_load_64(&pVnode1->writeMetrics.total_bytes);
×
1414
  pRawMetrics->fetch_batch_meta_time = atomic_load_64(&pVnode1->writeMetrics.fetch_batch_meta_time);
×
1415
  pRawMetrics->fetch_batch_meta_count = atomic_load_64(&pVnode1->writeMetrics.fetch_batch_meta_count);
×
1416
  pRawMetrics->preprocess_time = atomic_load_64(&pVnode1->writeMetrics.preprocess_time);
×
1417
  pRawMetrics->wal_write_bytes = atomic_load_64(&syncMetrics.wal_write_bytes);
×
1418
  pRawMetrics->wal_write_time = atomic_load_64(&syncMetrics.wal_write_time);
×
1419
  pRawMetrics->apply_bytes = atomic_load_64(&pVnode1->writeMetrics.apply_bytes);
×
1420
  pRawMetrics->apply_time = atomic_load_64(&pVnode1->writeMetrics.apply_time);
×
1421
  pRawMetrics->commit_count = atomic_load_64(&pVnode1->writeMetrics.commit_count);
×
1422
  pRawMetrics->commit_time = atomic_load_64(&pVnode1->writeMetrics.commit_time);
×
1423
  pRawMetrics->memtable_wait_time = atomic_load_64(&pVnode1->writeMetrics.memtable_wait_time);
×
1424
  pRawMetrics->blocked_commit_count = atomic_load_64(&pVnode1->writeMetrics.blocked_commit_count);
×
1425
  pRawMetrics->blocked_commit_time = atomic_load_64(&pVnode1->writeMetrics.block_commit_time);
×
1426
  pRawMetrics->merge_count = atomic_load_64(&pVnode1->writeMetrics.merge_count);
×
1427
  pRawMetrics->merge_time = atomic_load_64(&pVnode1->writeMetrics.merge_time);
×
1428
  pRawMetrics->last_cache_commit_time = atomic_load_64(&pVnode1->writeMetrics.last_cache_commit_time);
×
1429
  pRawMetrics->last_cache_commit_count = atomic_load_64(&pVnode1->writeMetrics.last_cache_commit_count);
×
1430

1431
  return 0;
×
1432
}
1433

1434
/*
1435
 * Reset raw write metrics for a vnode by subtracting old values
1436
 */
1437
int32_t vnodeResetRawWriteMetrics(void *pVnode, const SRawWriteMetrics *pOldMetrics) {
×
1438
  if (pVnode == NULL || pOldMetrics == NULL) {
×
1439
    return TSDB_CODE_INVALID_PARA;
×
1440
  }
1441

1442
  SVnode *pVnode1 = (SVnode *)pVnode;
×
1443

1444
  // Reset vnode write metrics using atomic operations to subtract old values
1445
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.total_requests, pOldMetrics->total_requests);
×
1446
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.total_rows, pOldMetrics->total_rows);
×
1447
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.total_bytes, pOldMetrics->total_bytes);
×
1448

1449
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.fetch_batch_meta_time, pOldMetrics->fetch_batch_meta_time);
×
1450
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.fetch_batch_meta_count, pOldMetrics->fetch_batch_meta_count);
×
1451
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.preprocess_time, pOldMetrics->preprocess_time);
×
1452
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.apply_bytes, pOldMetrics->apply_bytes);
×
1453
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.apply_time, pOldMetrics->apply_time);
×
1454
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.commit_count, pOldMetrics->commit_count);
×
1455

1456
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.commit_time, pOldMetrics->commit_time);
×
1457
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.merge_time, pOldMetrics->merge_time);
×
1458

1459
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.memtable_wait_time, pOldMetrics->memtable_wait_time);
×
1460
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.blocked_commit_count, pOldMetrics->blocked_commit_count);
×
1461
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.block_commit_time, pOldMetrics->blocked_commit_time);
×
1462
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.merge_count, pOldMetrics->merge_count);
×
1463

1464
  // Reset new cache metrics
1465
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.last_cache_commit_time, pOldMetrics->last_cache_commit_time);
×
1466
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.last_cache_commit_count, pOldMetrics->last_cache_commit_count);
×
1467

1468
  // Reset sync metrics
1469
  SSyncMetrics syncMetrics = {
×
1470
      .wal_write_bytes = pOldMetrics->wal_write_bytes,
×
1471
      .wal_write_time = pOldMetrics->wal_write_time,
×
1472
  };
1473
  syncResetMetrics(pVnode1->sync, &syncMetrics);
×
1474

1475
  return 0;
×
1476
}
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