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

taosdata / TDengine / #4469

08 Jul 2025 09:38AM UTC coverage: 62.22% (-1.2%) from 63.381%
#4469

push

travis-ci

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

merge: from main to 3.0 branch

153678 of 316510 branches covered (48.55%)

Branch coverage included in aggregate %.

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

5035 existing lines in 221 files now uncovered.

238955 of 314529 relevant lines covered (75.97%)

6273248.0 hits per line

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

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

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

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

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

53
void vnodePrintTableMeta(STableMetaRsp *pMeta) {
66,197✔
54
  if (!(qDebugFlag & DEBUG_DEBUG)) {
66,197✔
55
    return;
33,711✔
56
  }
57

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

81
int32_t fillTableColRef(SMetaReader *reader, SColRef *pRef, int32_t numOfCol) {
20✔
82
  int8_t tblType = reader->me.type;
20✔
83
  if (hasRefCol(tblType)) {
20!
84
    SColRefWrapper *p = &(reader->me.colRef);
20✔
85
    if (numOfCol != p->nCols) {
20!
86
      vError("fillTableColRef table type:%d, col num:%d, col cmpr num:%d mismatch", tblType, numOfCol, p->nCols);
×
87
      return TSDB_CODE_APP_ERROR;
×
88
    }
89
    for (int i = 0; i < p->nCols; i++) {
148✔
90
      SColRef *pColRef = &p->pColRef[i];
128✔
91
      pRef[i].hasRef = pColRef->hasRef;
128✔
92
      pRef[i].id = pColRef->id;
128✔
93
      if(pRef[i].hasRef) {
128✔
94
        tstrncpy(pRef[i].refDbName, pColRef->refDbName, TSDB_DB_NAME_LEN);
62✔
95
        tstrncpy(pRef[i].refTableName, pColRef->refTableName, TSDB_TABLE_NAME_LEN);
62✔
96
        tstrncpy(pRef[i].refColName, pColRef->refColName, TSDB_COL_NAME_LEN);
62✔
97
      }
98
    }
99
  }
100
  return 0;
20✔
101
}
102

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

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

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

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

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

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

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

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

195
  metaRsp.numOfTags = schemaTag.nCols;
66,197✔
196
  metaRsp.numOfColumns = schema.nCols;
66,197✔
197
  metaRsp.precision = pVnode->config.tsdbCfg.precision;
66,197✔
198
  metaRsp.sversion = schema.version;
66,197✔
199
  metaRsp.tversion = schemaTag.version;
66,197✔
200
  metaRsp.pSchemas = (SSchema *)taosMemoryMalloc(sizeof(SSchema) * (metaRsp.numOfColumns + metaRsp.numOfTags));
66,197!
201
  metaRsp.pSchemaExt = (SSchemaExt *)taosMemoryCalloc(metaRsp.numOfColumns, sizeof(SSchemaExt));
66,199!
202
  if (NULL == metaRsp.pSchemas || NULL == metaRsp.pSchemaExt) {
66,198!
203
    code = terrno;
×
204
    goto _exit;
×
205
  }
206
  (void)memcpy(metaRsp.pSchemas, schema.pSchema, sizeof(SSchema) * schema.nCols);
66,198✔
207
  if (schemaTag.nCols) {
66,198✔
208
    (void)memcpy(metaRsp.pSchemas + schema.nCols, schemaTag.pSchema, sizeof(SSchema) * schemaTag.nCols);
32,139✔
209
  }
210
  if (metaRsp.pSchemaExt) {
66,198!
211
    SMetaReader *pReader = mer1.me.type == TSDB_CHILD_TABLE ? &mer2 : &mer1;
66,198✔
212
    code = fillTableColCmpr(pReader, metaRsp.pSchemaExt, metaRsp.numOfColumns);
66,198✔
213
    if (code < 0) {
66,199!
214
      goto _exit;
×
215
    }
216
    for (int32_t i = 0; i < metaRsp.numOfColumns && pReader->me.pExtSchemas; i++) {
444,171✔
217
      metaRsp.pSchemaExt[i].typeMod = pReader->me.pExtSchemas[i].typeMod;
377,972✔
218
    }
219
  } else {
UNCOV
220
    code = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
221
    goto _exit;
×
222
  }
223
  if (hasRefCol(mer1.me.type)) {
66,199✔
224
    metaRsp.rversion = mer1.me.colRef.version;
22✔
225
    metaRsp.pColRefs = (SColRef*)taosMemoryMalloc(sizeof(SColRef) * metaRsp.numOfColumns);
22!
226
    if (metaRsp.pColRefs) {
20!
227
      code = fillTableColRef(&mer1, metaRsp.pColRefs, metaRsp.numOfColumns);
20✔
228
      if (code < 0) {
20!
229
        goto _exit;
×
230
      }
231
    }
232
    metaRsp.numOfColRefs = metaRsp.numOfColumns;
20✔
233
  } else {
234
    metaRsp.pColRefs = NULL;
66,177✔
235
    metaRsp.numOfColRefs = 0;
66,177✔
236
  }
237

238
  vnodePrintTableMeta(&metaRsp);
66,197✔
239

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

247
  if (direct) {
66,196✔
248
    pRsp = rpcMallocCont(rspLen);
128✔
249
  } else {
250
    pRsp = taosMemoryCalloc(1, rspLen);
66,068!
251
  }
252

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

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

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

279
  if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST && autoCreateCtb == 1) {
146,324✔
280
    code = TSDB_CODE_SUCCESS;
61,638✔
281
  }
282

283
  if (code) {
146,324✔
284
    qError("get table %s meta with %" PRIu8 " failed cause of %s", infoReq.tbName, infoReq.option, tstrerror(code));
18,486!
285
  }
286

287
  if (direct) {
146,324✔
288
    tmsgSendRsp(&rpcMsg);
1,706✔
289
  } else {
290
    *pMsg = rpcMsg;
144,618✔
291
  }
292

293
  return code;
146,325✔
294
}
295

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

505
  batchRsp.pRsps = taosArrayInit(msgNum, sizeof(SBatchRspMsg));
110,863✔
506
  if (NULL == batchRsp.pRsps) {
110,865!
UNCOV
507
    code = terrno;
×
508
    qError("taosArrayInit %d SBatchRspMsg failed", msgNum);
×
509
    goto _exit;
×
510
  }
511

512
  for (int32_t i = 0; i < msgNum; ++i) {
259,164✔
513
    req = taosArrayGet(batchReq.pMsgs, i);
148,301✔
514
    if (req == NULL) {
148,301✔
515
      code = terrno;
1✔
516
      goto _exit;
×
517
    }
518

519
    reqMsg.msgType = req->msgType;
148,300✔
520
    reqMsg.pCont = req->msg;
148,300✔
521
    reqMsg.contLen = req->msgLen;
148,300✔
522

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

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

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

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

599
_exit:
110,865✔
600

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

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

611
  taosArrayDestroyEx(batchReq.pMsgs, tFreeSBatchReqMsg);
110,865✔
612
  taosArrayDestroyEx(batchRsp.pRsps, tFreeSBatchRspMsg);
110,865✔
613

614
  tmsgSendRsp(&rspMsg);
110,865✔
615

616
  return code;
110,863✔
617
}
618

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

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

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

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

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

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

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

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

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

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

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

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

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

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

692
      refColsNum++;
×
693
    }
694

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

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

703
_return:
×
704

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

780
_return:
37✔
781

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

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

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

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

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

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

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

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

835
_return:
×
836

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

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

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

851
  //tmsgSendRsp(&rspMsg);
852

853
  return code;
×
854
}
855

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

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

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

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

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

897
_return:
72✔
898

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

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

909
  *pMsg = rspMsg;
72✔
910

911
  tDestroySVStbRefDbsRsp(&rsp);
72✔
912

913
  return code;
72✔
914
}
915

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

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

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

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

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

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

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

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

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

1013
  if (dbname) {
27,605!
1014
    *dbname = pConf->dbname;
27,613✔
1015
  }
1016

1017
  if (vgId) {
27,605✔
1018
    *vgId = TD_VID(pVnodeObj);
26,049✔
1019
  }
1020

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

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

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

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

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

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

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

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

1077
  while (1) {
2,727,788✔
1078
    tb_uid_t id = metaCtbCursorNext(pCur);
3,475,189✔
1079
    if (id == 0) {
3,474,106✔
1080
      break;
747,898✔
1081
    }
1082

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

1090
_exit:
747,898✔
1091
  metaCloseCtbCursor(pCur);
747,898✔
1092
  return code;
748,066✔
1093
}
1094

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

1102
  while (1) {
157,379✔
1103
    tb_uid_t id = metaStbCursorNext(pCur);
293,737✔
1104
    if (id == 0) {
293,699✔
1105
      break;
136,360✔
1106
    }
1107

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

1115
_exit:
136,360✔
1116
  metaCloseStbCursor(pCur);
136,360✔
1117
  return code;
136,368✔
1118
}
1119

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

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

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

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

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

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

1156
  *num = 0;
22,871✔
1157
  while (1) {
19,361✔
1158
    tb_uid_t id = metaCtbCursorNext(pCur);
42,232✔
1159
    if (id == 0) {
42,234✔
1160
      break;
22,873✔
1161
    }
1162

1163
    ++(*num);
19,361✔
1164
  }
1165

1166
  metaCloseCtbCursor(pCur);
22,873✔
1167
  return TSDB_CODE_SUCCESS;
22,873✔
1168
}
1169

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

1179
  return TSDB_CODE_SUCCESS;
22,874✔
1180
}
1181

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

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

1195
  metaReaderClear(&mr);
22,869✔
1196
  return TSDB_CODE_SUCCESS;
22,873✔
1197
}
1198

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

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

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

1255
  return code;
136,433✔
1256
}
1257
#endif
1258

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

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

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

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

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

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

1291
  *num = 0;
136,434✔
1292
  int64_t arrSize = taosArrayGetSize(suidList);
136,434✔
1293
  for (int64_t i = 0; i < arrSize; ++i) {
293,883✔
1294
    tb_uid_t suid = *(tb_uid_t *)taosArrayGet(suidList, i);
157,445✔
1295

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

1308
_exit:
136,438✔
1309
  taosArrayDestroy(suidList);
136,452✔
1310
  return TSDB_CODE_SUCCESS;
136,445✔
1311
}
1312

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

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

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

1333
    *num += ctbNum;
×
1334
  }
1335

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

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

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

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

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

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

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

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

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

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

1396
  code = tsdbGetFsSize(pVnodeObj->pTsdb, pInfo);
17,604✔
1397
_exit:
17,606✔
1398
  return code;
17,606✔
1399
}
1400

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

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

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

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

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

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

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

1491
  return 0;
×
1492
}
1493

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

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

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

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

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

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

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

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

1535
  return 0;
×
1536
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc