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

taosdata / TDengine / #4808

16 Oct 2025 11:40AM UTC coverage: 57.938% (-0.6%) from 58.524%
#4808

push

travis-ci

web-flow
fix(tref): increase TSDB_REF_OBJECTS from 100 to 2000 for improved reference handling (#33281)

137662 of 303532 branches covered (45.35%)

Branch coverage included in aggregate %.

209234 of 295200 relevant lines covered (70.88%)

4035326.15 hits per line

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

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

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

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

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

53
void vnodePrintTableMeta(STableMetaRsp *pMeta) {
62,634✔
54
  if (!(qDebugFlag & DEBUG_DEBUG)) {
62,634✔
55
    return;
260✔
56
  }
57

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

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

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

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

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

130
  if (!reqTbUid) {
110,141✔
131
    (void)tsnprintf(tableFName, TSDB_TABLE_FNAME_LEN, "%s.%s", infoReq.dbFName, infoReq.tbName);
108,448✔
132
    if (pVnode->mounted) tTrimMountPrefix(tableFName);
108,448✔
133
    code = vnodeValidateTableHash(pVnode, tableFName);
108,448✔
134
    if (code) {
108,448!
135
      goto _exit4;
×
136
    }
137
  }
138

139
  // query meta
140
  metaReaderDoInit(&mer1, pVnode->pMeta, META_READER_LOCK);
110,141✔
141
  if (reqTbUid) {
110,142✔
142
    SET_ERRNO(0);
1,694✔
143
    uint64_t tbUid = taosStr2UInt64(infoReq.tbName, NULL, 10);
1,694✔
144
    if (ERRNO == ERANGE || tbUid == 0) {
1,694!
145
      code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
146
      goto _exit3;
666✔
147
    }
148
    SMetaReader mr3 = {0};
1,694✔
149
    metaReaderDoInit(&mr3, ((SVnode *)pVnode)->pMeta, META_READER_NOLOCK);
1,694✔
150
    if ((code = metaReaderGetTableEntryByUid(&mr3, tbUid)) < 0) {
1,694✔
151
      metaReaderClear(&mr3);
666✔
152
      TAOS_CHECK_GOTO(code, NULL, _exit3);
666!
153
    }
154
    tstrncpy(metaRsp.tbName, mr3.me.name, TSDB_TABLE_NAME_LEN);
1,028✔
155
    metaReaderClear(&mr3);
1,028✔
156
    TAOS_CHECK_GOTO(metaGetTableEntryByName(&mer1, metaRsp.tbName), NULL, _exit3);
1,028!
157
  } else if (metaGetTableEntryByName(&mer1, infoReq.tbName) < 0) {
108,448✔
158
    code = terrno;
46,842✔
159
    goto _exit3;
46,842✔
160
  }
161

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

166
  switch (mer1.me.type) {
62,634!
167
    case TSDB_SUPER_TABLE: {
27,045✔
168
      (void)strcpy(metaRsp.stbName, mer1.me.name);
27,045✔
169
      schema = mer1.me.stbEntry.schemaRow;
27,045✔
170
      schemaTag = mer1.me.stbEntry.schemaTag;
27,045✔
171
      metaRsp.suid = mer1.me.uid;
27,045✔
172
      break;
27,045✔
173
    }
174
    case TSDB_CHILD_TABLE:
26,162✔
175
    case TSDB_VIRTUAL_CHILD_TABLE:{
176
      metaReaderDoInit(&mer2, pVnode->pMeta, META_READER_NOLOCK);
26,162✔
177
      if (metaReaderGetTableEntryByUid(&mer2, mer1.me.ctbEntry.suid) < 0) goto _exit2;
26,162!
178

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

239
  vnodePrintTableMeta(&metaRsp);
62,634✔
240

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

248
  if (direct) {
62,634✔
249
    pRsp = rpcMallocCont(rspLen);
684✔
250
  } else {
251
    pRsp = taosMemoryCalloc(1, rspLen);
61,950!
252
  }
253

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

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

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

280
  if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST && autoCreateCtb == 1) {
110,142✔
281
    code = TSDB_CODE_SUCCESS;
39,040✔
282
  }
283

284
  if (code) {
110,142✔
285
    qError("get table %s meta with %" PRIu8 " failed cause of %s", infoReq.tbName, infoReq.option, tstrerror(code));
8,468!
286
  }
287

288
  if (direct) {
110,142✔
289
    tmsgSendRsp(&rpcMsg);
2,283✔
290
  } else {
291
    *pMsg = rpcMsg;
107,859✔
292
  }
293

294
  return code;
110,142✔
295
}
296

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

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

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

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

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

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

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

336
  if (mer1.me.type == TSDB_SUPER_TABLE) {
152!
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) {
152✔
340
    metaReaderDoInit(&mer2, pVnode->pMeta, META_READER_NOLOCK);
84✔
341
    if (metaReaderGetTableEntryByUid(&mer2, mer1.me.ctbEntry.suid) < 0) goto _exit;
84!
342

343
    tstrncpy(cfgRsp.stbName, mer2.me.name, TSDB_TABLE_NAME_LEN);
84✔
344
    schema = mer2.me.stbEntry.schemaRow;
84✔
345
    schemaTag = mer2.me.stbEntry.schemaTag;
84✔
346
    cfgRsp.ttl = mer1.me.ctbEntry.ttlDays;
84✔
347
    cfgRsp.commentLen = mer1.me.ctbEntry.commentLen;
84✔
348
    if (mer1.me.ctbEntry.commentLen > 0) {
84!
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;
84✔
356
    cfgRsp.tagsLen = pTag->len;
84✔
357
    cfgRsp.pTags = taosMemoryMalloc(cfgRsp.tagsLen);
84!
358
    if (NULL == cfgRsp.pTags) {
84!
359
      code = terrno;
×
360
      goto _exit;
×
361
    }
362
    (void)memcpy(cfgRsp.pTags, pTag, cfgRsp.tagsLen);
84✔
363
  } else if (mer1.me.type == TSDB_NORMAL_TABLE || mer1.me.type == TSDB_VIRTUAL_NORMAL_TABLE) {
68!
364
    schema = mer1.me.ntbEntry.schemaRow;
68✔
365
    cfgRsp.ttl = mer1.me.ntbEntry.ttlDays;
68✔
366
    cfgRsp.commentLen = mer1.me.ntbEntry.commentLen;
68✔
367
    if (mer1.me.ntbEntry.commentLen > 0) {
68!
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;
152✔
381
  cfgRsp.numOfColumns = schema.nCols;
152✔
382
  cfgRsp.virtualStb = false; // vnode don't have super table, so it's always false
152✔
383
  cfgRsp.pSchemas = (SSchema *)taosMemoryMalloc(sizeof(SSchema) * (cfgRsp.numOfColumns + cfgRsp.numOfTags));
152!
384
  cfgRsp.pSchemaExt = (SSchemaExt *)taosMemoryMalloc(cfgRsp.numOfColumns * sizeof(SSchemaExt));
152!
385
  cfgRsp.pColRefs = (SColRef *)taosMemoryMalloc(sizeof(SColRef) * cfgRsp.numOfColumns);
152!
386

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

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

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

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

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

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

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

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

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

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

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

468
  tFreeSTableCfgRsp(&cfgRsp);
152✔
469
  metaReaderClear(&mer2);
152✔
470
  metaReaderClear(&mer1);
152✔
471
  return code;
152✔
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) {
89,114✔
484
  int32_t      code = 0;
89,114✔
485
  int32_t      rspSize = 0;
89,114✔
486
  SBatchReq    batchReq = {0};
89,114✔
487
  SBatchMsg   *req = NULL;
89,114✔
488
  SBatchRspMsg rsp = {0};
89,114✔
489
  SBatchRsp    batchRsp = {0};
89,114✔
490
  SRpcMsg      reqMsg = *pMsg;
89,114✔
491
  SRpcMsg      rspMsg = {0};
89,114✔
492
  void        *pRsp = NULL;
89,114✔
493

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

500
  int32_t msgNum = taosArrayGetSize(batchReq.pMsgs);
89,111✔
501
  if (msgNum >= MAX_META_MSG_IN_BATCH) {
89,114!
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));
89,114✔
508
  if (NULL == batchRsp.pRsps) {
89,114✔
509
    code = terrno;
1✔
510
    qError("taosArrayInit %d SBatchRspMsg failed", msgNum);
×
511
    goto _exit;
×
512
  }
513

514
  for (int32_t i = 0; i < msgNum; ++i) {
198,744✔
515
    req = taosArrayGet(batchReq.pMsgs, i);
109,631✔
516
    if (req == NULL) {
109,631!
517
      code = terrno;
×
518
      goto _exit;
×
519
    }
520

521
    reqMsg.msgType = req->msgType;
109,631✔
522
    reqMsg.pCont = req->msg;
109,631✔
523
    reqMsg.contLen = req->msgLen;
109,631✔
524

525
    switch (req->msgType) {
109,631!
526
      case TDMT_VND_TABLE_META:
106,165✔
527
        // error code has been set into reqMsg, no need to handle it here.
528
        if (TSDB_CODE_SUCCESS != vnodeGetTableMeta(pVnode, &reqMsg, false)) {
106,165✔
529
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
6,203!
530
        }
531
        break;
106,165✔
532
      case TDMT_VND_TABLE_NAME:
1,694✔
533
        // error code has been set into reqMsg, no need to handle it here.
534
        if (TSDB_CODE_SUCCESS != vnodeGetTableMeta(pVnode, &reqMsg, false)) {
1,694✔
535
          qWarn("vnodeGetBatchName failed, msgType:%d", req->msgType);
666!
536
        }
537
        break;
1,694✔
538
      case TDMT_VND_TABLE_CFG:
152✔
539
        // error code has been set into reqMsg, no need to handle it here.
540
        if (TSDB_CODE_SUCCESS != vnodeGetTableCfg(pVnode, &reqMsg, false)) {
152!
541
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
×
542
        }
543
        break;
152✔
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:
1,620✔
551
        // error code has been set into reqMsg, no need to handle it here.
552
        if (TSDB_CODE_SUCCESS != vnodeGetVStbRefDbs(pVnode, &reqMsg)) {
1,620!
553
          qWarn("vnodeGetVStbRefDbs failed, msgType:%d", req->msgType);
×
554
        }
555
        break;
1,620✔
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;
109,631✔
565
    rsp.reqType = reqMsg.msgType;
109,631✔
566
    rsp.msgLen = reqMsg.contLen;
109,631✔
567
    rsp.rspCode = reqMsg.code;
109,631✔
568
    rsp.msg = reqMsg.pCont;
109,631✔
569

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

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

595
_exit:
89,114✔
596

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

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

607
  taosArrayDestroyEx(batchReq.pMsgs, tFreeSBatchReqMsg);
89,114✔
608
  taosArrayDestroyEx(batchRsp.pRsps, tFreeSBatchRspMsg);
89,113✔
609

610
  tmsgSendRsp(&rspMsg);
89,114✔
611

612
  return code;
89,114✔
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) {
1,620✔
716
  int32_t                    code = TSDB_CODE_SUCCESS;
1,620✔
717
  int32_t                    line = 0;
1,620✔
718
  SMetaReader                mr = {0};
1,620✔
719
  bool                       readerInit = false;
1,620✔
720
  SSHashObj*                 pDbNameHash = NULL;
1,620✔
721
  SArray*                    pList = NULL;
1,620✔
722

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

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

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

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

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

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

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

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

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

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

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

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

776
_return:
39✔
777

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

782
  taosArrayDestroy(pList);
1,620✔
783
  tSimpleHashCleanup(pDbNameHash);
1,620✔
784

785
  if (code) {
1,620!
786
    qError("%s failed since %s", __func__, tstrerror(code));
×
787
  }
788
  return code;
1,620✔
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) {
1,620✔
854
  int32_t        code = 0;
1,620✔
855
  int32_t        rspSize = 0;
1,620✔
856
  SVStbRefDbsReq req = {0};
1,620✔
857
  SVStbRefDbsRsp rsp = {0};
1,620✔
858
  SRpcMsg        rspMsg = {0};
1,620✔
859
  void          *pRsp = NULL;
1,620✔
860
  int32_t        line = 0;
1,620✔
861

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

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

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

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

895
_return:
1,619✔
896

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

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

907
  *pMsg = rspMsg;
1,619✔
908

909
  tDestroySVStbRefDbsRsp(&rsp);
1,619✔
910

911
  return code;
1,620✔
912
}
913

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

921
    SDbSizeStatisInfo info = {0};
13,102✔
922
    if (0 == (code = vnodeGetDBSize(pVnode, &info))) {
13,102!
923
      int64_t compSize =
13,102✔
924
          info.l1Size + info.l2Size + info.l3Size + info.cacheSize + info.walSize + info.metaSize + +info.ssSize;
13,102✔
925
      if (compSize >= 0) {
13,102!
926
        pVnode->config.vndStats.compStorage = compSize;
13,102✔
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));
×
933
    }
934
  }
935
  if (output) *output = pVnode->config.vndStats.compStorage;
236,805!
936
#endif
937
  return code;
236,805✔
938
}
939

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

946
    if (pVnode->inUse) {
236,805✔
947
      *bufferSegmentUsed = pVnode->inUse->size;
236,797✔
948
    }
949
    *bufferSegmentSize = pVnode->config.szBuf / VNODE_BUFPOOL_SEGMENTS;
236,805✔
950

951
    (void)taosThreadMutexUnlock(&pVnode->mutex);
236,805✔
952
  }
953
}
236,805✔
954

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

960
  pLoad->vgId = TD_VID(pVnode);
236,805✔
961
  pLoad->syncState = state.state;
236,805✔
962
  pLoad->syncRestore = state.restored;
236,805✔
963
  pLoad->syncTerm = state.term;
236,805✔
964
  pLoad->roleTimeMs = state.roleTimeMs;
236,805✔
965
  pLoad->startTimeMs = state.startTimeMs;
236,805✔
966
  pLoad->syncCanRead = state.canRead;
236,805✔
967
  pLoad->learnerProgress = state.progress;
236,805✔
968
  pLoad->cacheUsage = tsdbCacheGetUsage(pVnode);
236,805✔
969
  pLoad->numOfCachedTables = tsdbCacheGetElems(pVnode);
236,805✔
970
  VNODE_DO_META_QUERY(pVnode, pLoad->numOfTables = metaGetTbNum(pVnode->pMeta));
236,805✔
971
  VNODE_DO_META_QUERY(pVnode, pLoad->numOfTimeSeries = metaGetTimeSeriesNum(pVnode->pMeta, 1));
236,805✔
972
  pLoad->totalStorage = (int64_t)3 * 1073741824;  // TODO
236,805✔
973
  (void)vnodeGetCompStorage(pVnode, &pLoad->compStorage);
236,805✔
974
  pLoad->pointsWritten = 100;
236,805✔
975
  pLoad->numOfSelectReqs = 1;
236,805✔
976
  pLoad->numOfInsertReqs = atomic_load_64(&pVnode->statis.nInsert);
236,805✔
977
  pLoad->numOfInsertSuccessReqs = atomic_load_64(&pVnode->statis.nInsertSuccess);
236,805✔
978
  pLoad->numOfBatchInsertReqs = atomic_load_64(&pVnode->statis.nBatchInsert);
236,805✔
979
  pLoad->numOfBatchInsertSuccessReqs = atomic_load_64(&pVnode->statis.nBatchInsertSuccess);
236,805✔
980
  vnodeGetBufferInfo(pVnode, &pLoad->bufferSegmentUsed, &pLoad->bufferSegmentSize);
236,805✔
981
  return 0;
236,805✔
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) {
6✔
1000
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nInsert, pLoad->numOfInsertReqs, 64, "nInsert");
6!
1001
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nInsertSuccess, pLoad->numOfInsertSuccessReqs, 64, "nInsertSuccess");
6!
1002
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nBatchInsert, pLoad->numOfBatchInsertReqs, 64, "nBatchInsert");
6!
1003
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nBatchInsertSuccess, pLoad->numOfBatchInsertSuccessReqs, 64,
6!
1004
                            "nBatchInsertSuccess");
1005
}
6✔
1006

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

1011
  if (dbname) {
17,547!
1012
    *dbname = pConf->dbname;
17,559✔
1013
  }
1014

1015
  if (vgId) {
17,547✔
1016
    *vgId = TD_VID(pVnodeObj);
16,227✔
1017
  }
1018

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

1023
  if (numOfNormalTables) {
17,547!
1024
    *numOfNormalTables = pConf->vndStats.numOfNTables;
×
1025
  }
1026
}
17,547✔
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) {
184,388✔
1067
  int32_t      code = TSDB_CODE_SUCCESS;
184,388✔
1068
  SVnode      *pVnodeObj = pVnode;
184,388✔
1069
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnodeObj, suid, 1);
184,388✔
1070
  if (NULL == pCur) {
184,509!
1071
    qError("vnode get all table list failed");
×
1072
    return terrno;
×
1073
  }
1074

1075
  while (1) {
826,328✔
1076
    tb_uid_t id = metaCtbCursorNext(pCur);
1,010,837✔
1077
    if (id == 0) {
1,010,523✔
1078
      break;
184,549✔
1079
    }
1080

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

1088
_exit:
184,549✔
1089
  metaCloseCtbCursor(pCur);
184,549✔
1090
  return code;
184,605✔
1091
}
1092

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

1100
  while (1) {
589,206✔
1101
    tb_uid_t id = metaStbCursorNext(pCur);
735,244✔
1102
    if (id == 0) {
735,246✔
1103
      break;
146,038✔
1104
    }
1105

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

1113
_exit:
146,038✔
1114
  metaCloseStbCursor(pCur);
146,038✔
1115
  return code;
146,038✔
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) {
20,208✔
1149
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, suid, 0);
20,208✔
1150
  if (!pCur) {
20,210!
1151
    return terrno;
×
1152
  }
1153

1154
  *num = 0;
20,210✔
1155
  while (1) {
6,603✔
1156
    tb_uid_t id = metaCtbCursorNext(pCur);
26,813✔
1157
    if (id == 0) {
26,813✔
1158
      break;
20,210✔
1159
    }
1160

1161
    ++(*num);
6,603✔
1162
  }
1163

1164
  metaCloseCtbCursor(pCur);
20,210✔
1165
  return TSDB_CODE_SUCCESS;
20,213✔
1166
}
1167

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

1177
  return TSDB_CODE_SUCCESS;
20,212✔
1178
}
1179

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

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

1193
  metaReaderClear(&mr);
20,211✔
1194
  return TSDB_CODE_SUCCESS;
20,211✔
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) {
146,045✔
1223
  int32_t      code = TSDB_CODE_SUCCESS;
146,045✔
1224
  int32_t      tbNum = 0;
146,045✔
1225
  const char **pTbArr = NULL;
146,045✔
1226
  const char  *dbName = NULL;
146,045✔
1227
  *tbSize = 0;
146,045✔
1228

1229
  if (!(dbName = strchr(pVnode->config.dbname, '.'))) return 0;
146,045!
1230
  if (0 == strncmp(++dbName, "log", TSDB_DB_NAME_LEN)) {
146,045✔
1231
    tbNum = tkLogStbNum;
14✔
1232
    pTbArr = (const char **)&tkLogStb;
14✔
1233
  } else if (0 == strncmp(dbName, "audit", TSDB_DB_NAME_LEN)) {
146,031!
1234
    tbNum = tkAuditStbNum;
×
1235
    pTbArr = (const char **)&tkAuditStb;
×
1236
  }
1237
  if (tbNum && pTbArr) {
146,045!
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;
146,035✔
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) {
146,046✔
1267
  SArray *suidList = NULL;
146,046✔
1268

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

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

1282
  if ((!tbFilterSize && vnodeGetStbIdList(pVnode, 0, suidList) < 0) ||
146,035!
1283
      (tbFilterSize && vnodeGetStbIdListByFilter(pVnode, 0, suidList, vnodeTimeSeriesFilter, pVnode) < 0)) {
146,036✔
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;
146,035✔
1290
  int64_t arrSize = taosArrayGetSize(suidList);
146,035✔
1291
  for (int64_t i = 0; i < arrSize; ++i) {
735,237✔
1292
    tb_uid_t suid = *(tb_uid_t *)taosArrayGet(suidList, i);
589,202✔
1293

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

1306
_exit:
146,035✔
1307
  taosArrayDestroy(suidList);
146,045✔
1308
  return TSDB_CODE_SUCCESS;
146,046✔
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) {
8,950✔
1339
  if (pVnode == NULL) {
8,950!
1340
    return NULL;
×
1341
  }
1342

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

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

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

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

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

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

1367
  for (int i = 0; i < sizeof(dirName) / sizeof(dirName[0]); i++) {
65,900✔
1368
    int64_t size = {0};
52,720✔
1369
    (void)snprintf(path + offset, TSDB_FILENAME_LEN - offset, "%s%s", TD_DIRSEP, dirName[i]);
52,720✔
1370
    code = taosGetDirSize(path, &size);
52,720✔
1371
    if (code != 0) {
52,720✔
1372
      uWarn("vnode %d get dir %s %s size failed since %s", TD_VID(pVnode), path, dirName[i], tstrerror(code));
3!
1373
    }
1374
    path[offset] = 0;
52,720✔
1375
    dirSize[i] = size;
52,720✔
1376
  }
1377

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

1394
  code = tsdbGetFsSize(pVnodeObj->pTsdb, pInfo);
13,180✔
1395
_exit:
13,180✔
1396
  return code;
13,180✔
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