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

taosdata / TDengine / #4506

15 Jul 2025 12:33AM UTC coverage: 62.026% (-0.7%) from 62.706%
#4506

push

travis-ci

web-flow
docs: update stream docs (#31874)

155391 of 320094 branches covered (48.55%)

Branch coverage included in aggregate %.

240721 of 318525 relevant lines covered (75.57%)

6529048.03 hits per line

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

239
  vnodePrintTableMeta(&metaRsp);
66,282✔
240

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

248
  if (direct) {
66,283✔
249
    pRsp = rpcMallocCont(rspLen);
193✔
250
  } else {
251
    pRsp = taosMemoryCalloc(1, rspLen);
66,090!
252
  }
253

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

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

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

280
  if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST && autoCreateCtb == 1) {
146,380✔
281
    code = TSDB_CODE_SUCCESS;
61,654✔
282
  }
283

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

288
  if (direct) {
146,381✔
289
    tmsgSendRsp(&rpcMsg);
1,771✔
290
  } else {
291
    *pMsg = rpcMsg;
144,610✔
292
  }
293

294
  return code;
146,381✔
295
}
296

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

514
  for (int32_t i = 0; i < msgNum; ++i) {
264,024✔
515
    req = taosArrayGet(batchReq.pMsgs, i);
150,709✔
516
    if (req == NULL) {
150,708✔
517
      code = terrno;
1✔
518
      goto _exit;
×
519
    }
520

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

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

570
    rsp.msgIdx = req->msgIdx;
150,708✔
571
    rsp.reqType = reqMsg.msgType;
150,708✔
572
    rsp.msgLen = reqMsg.contLen;
150,708✔
573
    rsp.rspCode = reqMsg.code;
150,708✔
574
    rsp.msg = reqMsg.pCont;
150,708✔
575

576
    if (NULL == taosArrayPush(batchRsp.pRsps, &rsp)) {
301,420!
577
      qError("taosArrayPush failed");
×
578
      code = terrno;
×
579
      goto _exit;
×
580
    }
581
  }
582

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

601
_exit:
113,312✔
602

603
  rspMsg.info = pMsg->info;
113,312✔
604
  rspMsg.pCont = pRsp;
113,312✔
605
  rspMsg.contLen = rspSize;
113,312✔
606
  rspMsg.code = code;
113,312✔
607
  rspMsg.msgType = pMsg->msgType;
113,312✔
608

609
  if (code) {
113,312!
610
    qError("vnd get batch meta failed cause of %s", tstrerror(code));
×
611
  }
612

613
  taosArrayDestroyEx(batchReq.pMsgs, tFreeSBatchReqMsg);
113,312✔
614
  taosArrayDestroyEx(batchRsp.pRsps, tFreeSBatchRspMsg);
113,312✔
615

616
  tmsgSendRsp(&rspMsg);
113,311✔
617

618
  return code;
113,309✔
619
}
620

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

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

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

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

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

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

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

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

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

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

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

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

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

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

694
      refColsNum++;
×
695
    }
696

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

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

705
_return:
×
706

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

711
  taosArrayDestroy(pList);
×
712
  taosMemoryFree(pTb);
×
713
  tSimpleHashCleanup(pSrcTbls);
×
714

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

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

729
  pList = taosArrayInit(10, sizeof(uint64_t));
2,495✔
730
  QUERY_CHECK_NULL(pList, code, line, _return, terrno);
2,500!
731

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

739
  if (taosArrayGetSize(*ppRes) > 0) {
2,500✔
740
    // found in cache
741
    goto _return;
2,461✔
742
  } else {
743
    code = pHandle->api.metaFn.getChildTableList(pHandle->vnode, suid, pList);
39✔
744
    QUERY_CHECK_CODE(code, line, _return);
39!
745

746
    size_t num = taosArrayGetSize(pList);
39✔
747
    pDbNameHash = tSimpleHashInit(10, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
39✔
748
    QUERY_CHECK_NULL(pDbNameHash, code, line, _return, terrno);
39!
749

750
    for (int32_t i = 0; i < num; ++i) {
44✔
751
      uint64_t* id = taosArrayGet(pList, i);
5✔
752
      QUERY_CHECK_NULL(id, code, line, _return, terrno);
5!
753

754
      pHandle->api.metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pHandle->api.metaFn);
5✔
755
      readerInit = true;
5✔
756

757
      code = pHandle->api.metaReaderFn.getTableEntryByUid(&mr, *id);
5✔
758
      QUERY_CHECK_CODE(code, line, _return);
5!
759

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

766
            QUERY_CHECK_NULL(taosArrayPush(*ppRes, &refDbName), code, line, _return, terrno);
6!
767

768
            code = tSimpleHashPut(pDbNameHash, refDbName, strlen(refDbName), NULL, 0);
3✔
769
            QUERY_CHECK_CODE(code, line, _return);
3!
770
          }
771
        }
772
      }
773

774
      pHandle->api.metaReaderFn.clearReader(&mr);
5✔
775
      readerInit = false;
5✔
776
    }
777

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

782
_return:
39✔
783

784
  if (readerInit) {
2,500!
785
    pHandle->api.metaReaderFn.clearReader(&mr);
×
786
  }
787

788
  taosArrayDestroy(pList);
2,500✔
789
  tSimpleHashCleanup(pDbNameHash);
2,498✔
790

791
  if (code) {
2,498!
792
    qError("%s failed since %s", __func__, tstrerror(code));
×
793
  }
794
  return code;
2,496✔
795
}
796

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

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

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

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

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

837
_return:
×
838

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

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

849
  *pMsg = rspMsg;
×
850
  
851
  tDestroySVSubTablesRsp(&rsp);
×
852

853
  //tmsgSendRsp(&rspMsg);
854

855
  return code;
×
856
}
857

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

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

873
  SReadHandle handle = {.vnode = pVnode};
2,499✔
874
  initStorageAPI(&handle.api);
2,499✔
875

876
  code = vnodeReadVStbRefDbs(&handle, req.suid, &rsp.pDbs);
2,498✔
877
  QUERY_CHECK_CODE(code, line, _return);
2,496!
878
  rsp.vgId = TD_VID(pVnode);
2,496✔
879

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

899
_return:
2,497✔
900

901
  rspMsg.info = pMsg->info;
2,497✔
902
  rspMsg.pCont = pRsp;
2,497✔
903
  rspMsg.contLen = rspSize;
2,497✔
904
  rspMsg.code = code;
2,497✔
905
  rspMsg.msgType = pMsg->msgType;
2,497✔
906

907
  if (code) {
2,497!
908
    qError("vnd get virtual stb ref db failed cause of %s", tstrerror(code));
×
909
  }
910

911
  *pMsg = rspMsg;
2,497✔
912

913
  tDestroySVStbRefDbsRsp(&rsp);
2,497✔
914

915
  return code;
2,499✔
916
}
917

918
static int32_t vnodeGetCompStorage(SVnode *pVnode, int64_t *output) {
260,863✔
919
  int32_t code = 0;
260,863✔
920
#ifdef TD_ENTERPRISE
921
  int32_t now = taosGetTimestampSec();
260,863✔
922
  if (llabs(now - pVnode->config.vndStats.storageLastUpd) >= 30) {
260,863✔
923
    pVnode->config.vndStats.storageLastUpd = now;
17,751✔
924

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

944
static void vnodeGetBufferInfo(SVnode *pVnode, int64_t *bufferSegmentUsed, int64_t *bufferSegmentSize) {
260,863✔
945
  *bufferSegmentUsed = 0;
260,863✔
946
  *bufferSegmentSize = 0;
260,863✔
947
  if (pVnode) {
260,863!
948
    (void)taosThreadMutexLock(&pVnode->mutex);
260,863✔
949

950
    if (pVnode->inUse) {
260,863✔
951
      *bufferSegmentUsed = pVnode->inUse->size;
260,861✔
952
    }
953
    *bufferSegmentSize = pVnode->config.szBuf / VNODE_BUFPOOL_SEGMENTS;
260,863✔
954

955
    (void)taosThreadMutexUnlock(&pVnode->mutex);
260,863✔
956
  }
957
}
260,863✔
958

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

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

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

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

1015
  if (dbname) {
27,395!
1016
    *dbname = pConf->dbname;
27,404✔
1017
  }
1018

1019
  if (vgId) {
27,395✔
1020
    *vgId = TD_VID(pVnodeObj);
25,741✔
1021
  }
1022

1023
  if (numOfTables) {
27,395!
1024
    *numOfTables = pConf->vndStats.numOfNTables + pConf->vndStats.numOfCTables;
×
1025
  }
1026

1027
  if (numOfNormalTables) {
27,395!
1028
    *numOfNormalTables = pConf->vndStats.numOfNTables;
×
1029
  }
1030
}
27,395✔
1031

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

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

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

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

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

1070
int32_t vnodeGetCtbIdList(void *pVnode, int64_t suid, SArray *list) {
764,647✔
1071
  int32_t      code = TSDB_CODE_SUCCESS;
764,647✔
1072
  SVnode      *pVnodeObj = pVnode;
764,647✔
1073
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnodeObj, suid, 1);
764,647✔
1074
  if (NULL == pCur) {
765,824!
1075
    qError("vnode get all table list failed");
×
1076
    return terrno;
×
1077
  }
1078

1079
  while (1) {
2,764,877✔
1080
    tb_uid_t id = metaCtbCursorNext(pCur);
3,530,701✔
1081
    if (id == 0) {
3,530,694✔
1082
      break;
766,310✔
1083
    }
1084

1085
    if (NULL == taosArrayPush(list, &id)) {
2,764,877!
1086
      qError("taosArrayPush failed");
×
1087
      code = terrno;
×
1088
      goto _exit;
×
1089
    }
1090
  }
1091

1092
_exit:
766,310✔
1093
  metaCloseCtbCursor(pCur);
766,310✔
1094
  return code;
766,607✔
1095
}
1096

1097
int32_t vnodeGetStbIdList(SVnode *pVnode, int64_t suid, SArray *list) {
140,524✔
1098
  int32_t      code = TSDB_CODE_SUCCESS;
140,524✔
1099
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, suid);
140,524✔
1100
  if (!pCur) {
140,535!
1101
    return TSDB_CODE_OUT_OF_MEMORY;
×
1102
  }
1103

1104
  while (1) {
130,731✔
1105
    tb_uid_t id = metaStbCursorNext(pCur);
271,266✔
1106
    if (id == 0) {
271,269✔
1107
      break;
140,539✔
1108
    }
1109

1110
    if (NULL == taosArrayPush(list, &id)) {
130,731!
1111
      qError("taosArrayPush failed");
×
1112
      code = terrno;
×
1113
      goto _exit;
×
1114
    }
1115
  }
1116

1117
_exit:
140,539✔
1118
  metaCloseStbCursor(pCur);
140,539✔
1119
  return code;
140,541✔
1120
}
1121

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

1130
  while (1) {
94✔
1131
    tb_uid_t id = metaStbCursorNext(pCur);
180✔
1132
    if (id == 0) {
180✔
1133
      break;
86✔
1134
    }
1135

1136
    if ((*filter) && (*filter)(arg, &id)) {
94!
1137
      continue;
94✔
1138
    }
1139

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

1147
_exit:
86✔
1148
  metaCloseStbCursor(pCur);
86✔
1149
  return code;
86✔
1150
}
1151

1152
int32_t vnodeGetCtbNum(SVnode *pVnode, int64_t suid, int64_t *num) {
22,757✔
1153
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, suid, 0);
22,757✔
1154
  if (!pCur) {
22,762!
1155
    return terrno;
×
1156
  }
1157

1158
  *num = 0;
22,762✔
1159
  while (1) {
19,716✔
1160
    tb_uid_t id = metaCtbCursorNext(pCur);
42,478✔
1161
    if (id == 0) {
42,481✔
1162
      break;
22,765✔
1163
    }
1164

1165
    ++(*num);
19,716✔
1166
  }
1167

1168
  metaCloseCtbCursor(pCur);
22,765✔
1169
  return TSDB_CODE_SUCCESS;
22,768✔
1170
}
1171

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

1181
  return TSDB_CODE_SUCCESS;
22,767✔
1182
}
1183

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

1188
  int32_t code = metaReaderGetTableEntryByUid(&mr, suid);
22,762✔
1189
  if (code == TSDB_CODE_SUCCESS) {
22,759✔
1190
    if (keep) *keep = mr.me.stbEntry.keep;
22,758✔
1191
    if (flags) *flags = mr.me.flags;
22,758✔
1192
  } else {
1193
    if (keep) *keep = 0;
1!
1194
    if (flags) *flags = 0;
1!
1195
  }
1196

1197
  metaReaderClear(&mr);
22,759✔
1198
  return TSDB_CODE_SUCCESS;
22,767✔
1199
}
1200

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

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

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

1257
  return code;
140,590✔
1258
}
1259
#endif
1260

1261
static bool vnodeTimeSeriesFilter(void *arg1, void *arg2) {
94✔
1262
  SVnode *pVnode = (SVnode *)arg1;
94✔
1263

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

1270
int32_t vnodeGetTimeSeriesNum(SVnode *pVnode, int64_t *num) {
140,614✔
1271
  SArray *suidList = NULL;
140,614✔
1272

1273
  if (!(suidList = taosArrayInit(1, sizeof(tb_uid_t)))) {
140,614!
1274
    return terrno;
×
1275
  }
1276

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

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

1293
  *num = 0;
140,585✔
1294
  int64_t arrSize = taosArrayGetSize(suidList);
140,585✔
1295
  for (int64_t i = 0; i < arrSize; ++i) {
271,340✔
1296
    tb_uid_t suid = *(tb_uid_t *)taosArrayGet(suidList, i);
130,727✔
1297

1298
    int64_t ctbNum = 0;
130,734✔
1299
    int32_t numOfCols = 0;
130,734✔
1300
    int8_t  flags = 0;
130,734✔
1301
    code = metaGetStbStats(pVnode, suid, &ctbNum, &numOfCols, &flags);
130,734✔
1302
    if (TSDB_CODE_SUCCESS != code) {
130,754!
1303
      goto _exit;
×
1304
    }
1305
    if (!TABLE_IS_VIRTUAL(flags)) {
130,754✔
1306
      *num += ctbNum * (numOfCols - 1);
130,495✔
1307
    }
1308
  }
1309

1310
_exit:
140,613✔
1311
  taosArrayDestroy(suidList);
140,627✔
1312
  return TSDB_CODE_SUCCESS;
140,619✔
1313
}
1314

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

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

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

1335
    *num += ctbNum;
×
1336
  }
1337

1338
  metaCloseStbCursor(pCur);
×
1339
  return TSDB_CODE_SUCCESS;
×
1340
}
1341

1342
void *vnodeGetIdx(void *pVnode) {
13,850✔
1343
  if (pVnode == NULL) {
13,850!
1344
    return NULL;
×
1345
  }
1346

1347
  return metaGetIdx(((SVnode *)pVnode)->pMeta);
13,850✔
1348
}
1349

1350
void *vnodeGetIvtIdx(void *pVnode) {
13,843✔
1351
  if (pVnode == NULL) {
13,843!
1352
    return NULL;
×
1353
  }
1354
  return metaGetIvtIdx(((SVnode *)pVnode)->pMeta);
13,843✔
1355
}
1356

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

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

1365
  char   *dirName[] = {VNODE_TSDB_DIR, VNODE_WAL_DIR, VNODE_META_DIR, VNODE_TSDB_CACHE_DIR};
17,777✔
1366
  int64_t dirSize[4];
1367

1368
  vnodeGetPrimaryPath(pVnode, false, path, TSDB_FILENAME_LEN);
17,777✔
1369
  int32_t offset = strlen(path);
17,777✔
1370

1371
  for (int i = 0; i < sizeof(dirName) / sizeof(dirName[0]); i++) {
88,879✔
1372
    int64_t size = {0};
71,104✔
1373
    (void)snprintf(path + offset, TSDB_FILENAME_LEN, "%s%s", TD_DIRSEP, dirName[i]);
71,104✔
1374
    code = taosGetDirSize(path, &size);
71,104✔
1375
    if (code != 0) {
71,104✔
1376
      return code;
2✔
1377
    }
1378
    path[offset] = 0;
71,102✔
1379
    dirSize[i] = size;
71,102✔
1380
  }
1381

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

1398
  code = tsdbGetFsSize(pVnodeObj->pTsdb, pInfo);
17,775✔
1399
_exit:
17,777✔
1400
  return code;
17,777✔
1401
}
1402

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

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

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

1454
_OVER:
×
1455
  if (buf) {
3,432!
1456
    taosMemoryFree(buf);
×
1457
  }
1458
  return code;
3,432✔
1459
}
1460

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

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

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

1493
  return 0;
×
1494
}
1495

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

1504
  SVnode *pVnode1 = (SVnode *)pVnode;
×
1505

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

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

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

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

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

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

1537
  return 0;
×
1538
}
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