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

taosdata / TDengine / #4892

20 Dec 2025 01:15PM UTC coverage: 65.571% (+0.02%) from 65.549%
#4892

push

travis-ci

web-flow
feat: support taos_connect_with func (#33952)

* feat: support taos_connect_with

* refactor: enhance connection options and add tests for taos_set_option and taos_connect_with

* fix: handle NULL keys and values in taos_connect_with options

* fix: revert TAOSWS_GIT_TAG to default value "main"

* docs: add TLS configuration options for WebSocket connections in documentation

* docs: modify zh docs and add en docs

* chore: update taos.cfg

* docs: add examples

* docs: add error handling for connection failure in example code

2 of 82 new or added lines in 3 files covered. (2.44%)

527 existing lines in 120 files now uncovered.

182859 of 278870 relevant lines covered (65.57%)

104634355.9 hits per line

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

65.1
/source/dnode/vnode/src/vnd/vnodeQuery.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#include "tsdb.h"
17
#include "tutil.h"
18
#include "vnd.h"
19

20
#define VNODE_GET_LOAD_RESET_VALS(pVar, oVal, vType, tags)                                                    \
21
  do {                                                                                                        \
22
    int##vType##_t newVal = atomic_sub_fetch_##vType(&(pVar), (oVal));                                        \
23
    if (newVal < 0) {                                                                                         \
24
      vWarn("vgId:%d, %s, abnormal val:%" PRIi64 ", old val:%" PRIi64, TD_VID(pVnode), tags, newVal, (oVal)); \
25
    }                                                                                                         \
26
  } while (0)
27

28
int vnodeQueryOpen(SVnode *pVnode) {
4,147,986✔
29
  return qWorkerInit(NODE_TYPE_VNODE, TD_VID(pVnode), (void **)&pVnode->pQuery, &pVnode->msgCb);
4,147,986✔
30
}
31

32
void vnodeQueryPreClose(SVnode *pVnode) { qWorkerStopAllTasks((void *)pVnode->pQuery); }
4,154,135✔
33

34
void vnodeQueryClose(SVnode *pVnode) { qWorkerDestroy((void **)&pVnode->pQuery); }
4,155,042✔
35

36
int32_t fillTableColCmpr(SMetaReader *reader, SSchemaExt *pExt, int32_t numOfCol) {
19,855,394✔
37
  int8_t tblType = reader->me.type;
19,855,394✔
38
  if (withExtSchema(tblType)) {
19,855,706✔
39
    SColCmprWrapper *p = &(reader->me.colCmpr);
19,656,245✔
40
    if (numOfCol != p->nCols) {
19,656,245✔
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++) {
230,345,632✔
45
      SColCmpr *pCmpr = &p->pColCmpr[i];
210,689,181✔
46
      pExt[i].colId = pCmpr->id;
210,688,475✔
47
      pExt[i].compress = pCmpr->alg;
210,688,663✔
48
    }
49
  }
50
  return 0;
19,855,956✔
51
}
52

53
void vnodePrintTableMeta(STableMetaRsp *pMeta) {
19,854,719✔
54
  if (!(qDebugFlag & DEBUG_DEBUG)) {
19,854,719✔
55
    return;
126,807✔
56
  }
57

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

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

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

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

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

130
  if (!reqTbUid) {
31,792,239✔
131
    (void)tsnprintf(tableFName, TSDB_TABLE_FNAME_LEN, "%s.%s", infoReq.dbFName, infoReq.tbName);
31,224,901✔
132
    if (pVnode->mounted) tTrimMountPrefix(tableFName);
31,225,451✔
133
    code = vnodeValidateTableHash(pVnode, tableFName);
31,226,375✔
134
    if (code) {
31,224,433✔
135
      goto _exit4;
×
136
    }
137
  }
138

139
  // query meta
140
  metaReaderDoInit(&mer1, pVnode->pMeta, META_READER_LOCK);
31,791,771✔
141
  if (reqTbUid) {
31,791,999✔
142
    SET_ERRNO(0);
566,788✔
143
    uint64_t tbUid = taosStr2UInt64(infoReq.tbName, NULL, 10);
566,788✔
144
    if (ERRNO == ERANGE || tbUid == 0) {
566,788✔
145
      code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
146
      goto _exit3;
×
147
    }
148
    SMetaReader mr3 = {0};
566,788✔
149
    metaReaderDoInit(&mr3, ((SVnode *)pVnode)->pMeta, META_READER_NOLOCK);
566,788✔
150
    if ((code = metaReaderGetTableEntryByUid(&mr3, tbUid)) < 0) {
566,788✔
151
      metaReaderClear(&mr3);
219,801✔
152
      TAOS_CHECK_GOTO(code, NULL, _exit3);
219,801✔
153
    }
154
    tstrncpy(metaRsp.tbName, mr3.me.name, TSDB_TABLE_NAME_LEN);
346,987✔
155
    metaReaderClear(&mr3);
346,987✔
156
    TAOS_CHECK_GOTO(metaGetTableEntryByName(&mer1, metaRsp.tbName), NULL, _exit3);
346,987✔
157
  } else if (metaGetTableEntryByName(&mer1, infoReq.tbName) < 0) {
31,225,211✔
158
    code = terrno;
11,717,625✔
159
    goto _exit3;
11,717,625✔
160
  }
161

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

166
  switch (mer1.me.type) {
19,854,937✔
167
    case TSDB_SUPER_TABLE: {
3,578,935✔
168
      (void)strcpy(metaRsp.stbName, mer1.me.name);
3,578,935✔
169
      schema = mer1.me.stbEntry.schemaRow;
3,578,935✔
170
      schemaTag = mer1.me.stbEntry.schemaTag;
3,578,935✔
171
      metaRsp.suid = mer1.me.uid;
3,578,935✔
172
      metaRsp.virtualStb = TABLE_IS_VIRTUAL(mer1.me.flags);
3,578,935✔
173
      break;
3,578,935✔
174
    }
175
    case TSDB_CHILD_TABLE:
13,249,116✔
176
    case TSDB_VIRTUAL_CHILD_TABLE:{
177
      metaReaderDoInit(&mer2, pVnode->pMeta, META_READER_NOLOCK);
13,249,116✔
178
      if (metaReaderGetTableEntryByUid(&mer2, mer1.me.ctbEntry.suid) < 0) goto _exit2;
13,249,428✔
179

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

197
  metaRsp.numOfTags = schemaTag.nCols;
19,855,249✔
198
  metaRsp.numOfColumns = schema.nCols;
19,855,249✔
199
  metaRsp.precision = pVnode->config.tsdbCfg.precision;
19,855,249✔
200
  metaRsp.sversion = schema.version;
19,855,499✔
201
  metaRsp.tversion = schemaTag.version;
19,855,499✔
202
  metaRsp.pSchemas = (SSchema *)taosMemoryMalloc(sizeof(SSchema) * (metaRsp.numOfColumns + metaRsp.numOfTags));
19,855,499✔
203
  metaRsp.pSchemaExt = (SSchemaExt *)taosMemoryCalloc(metaRsp.numOfColumns, sizeof(SSchemaExt));
19,855,144✔
204
  if (NULL == metaRsp.pSchemas || NULL == metaRsp.pSchemaExt) {
19,855,394✔
205
    code = terrno;
×
206
    goto _exit;
×
207
  }
208
  (void)memcpy(metaRsp.pSchemas, schema.pSchema, sizeof(SSchema) * schema.nCols);
19,855,394✔
209
  if (schemaTag.nCols) {
19,855,394✔
210
    (void)memcpy(metaRsp.pSchemas + schema.nCols, schemaTag.pSchema, sizeof(SSchema) * schemaTag.nCols);
16,828,820✔
211
  }
212
  if (metaRsp.pSchemaExt) {
19,855,082✔
213
    SMetaReader *pReader = mer1.me.type == TSDB_CHILD_TABLE ? &mer2 : &mer1;
19,855,706✔
214
    code = fillTableColCmpr(pReader, metaRsp.pSchemaExt, metaRsp.numOfColumns);
19,855,706✔
215
    if (code < 0) {
19,855,706✔
216
      goto _exit;
×
217
    }
218
    for (int32_t i = 0; i < metaRsp.numOfColumns && pReader->me.pExtSchemas; i++) {
48,352,778✔
219
      metaRsp.pSchemaExt[i].typeMod = pReader->me.pExtSchemas[i].typeMod;
28,497,072✔
220
    }
221
  } else {
222
    code = TSDB_CODE_OUT_OF_MEMORY;
×
223
    goto _exit;
×
224
  }
225
  if (hasRefCol(mer1.me.type)) {
19,856,943✔
226
    metaRsp.rversion = mer1.me.colRef.version;
200,136✔
227
    metaRsp.pColRefs = (SColRef*)taosMemoryMalloc(sizeof(SColRef) * metaRsp.numOfColumns);
200,136✔
228
    if (metaRsp.pColRefs) {
199,399✔
229
      code = fillTableColRef(&mer1, metaRsp.pColRefs, metaRsp.numOfColumns);
199,399✔
230
      if (code < 0) {
199,399✔
231
        goto _exit;
×
232
      }
233
    }
234
    metaRsp.numOfColRefs = metaRsp.numOfColumns;
199,399✔
235
  } else {
236
    metaRsp.pColRefs = NULL;
19,655,320✔
237
    metaRsp.numOfColRefs = 0;
19,655,320✔
238
  }
239

240
  vnodePrintTableMeta(&metaRsp);
19,854,719✔
241

242
  // encode and send response
243
  rspLen = tSerializeSTableMetaRsp(NULL, 0, &metaRsp);
19,855,956✔
244
  if (rspLen < 0) {
19,855,281✔
245
    code = terrno;
×
246
    goto _exit;
×
247
  }
248

249
  if (direct) {
19,855,281✔
250
    pRsp = rpcMallocCont(rspLen);
75,584✔
251
  } else {
252
    pRsp = taosMemoryCalloc(1, rspLen);
19,779,697✔
253
  }
254

255
  if (pRsp == NULL) {
19,855,281✔
256
    code = terrno;
×
257
    goto _exit;
×
258
  }
259

260
  rspLen = tSerializeSTableMetaRsp(pRsp, rspLen, &metaRsp);
19,855,281✔
261
  if (rspLen < 0) {
19,855,281✔
262
    code = terrno;
×
263
    goto _exit;
×
264
  }
265

266
_exit:
19,854,657✔
267
  taosMemoryFree(metaRsp.pColRefs);
19,855,956✔
268
  taosMemoryFree(metaRsp.pSchemas);
19,855,281✔
269
  taosMemoryFree(metaRsp.pSchemaExt);
19,855,956✔
270
_exit2:
19,855,281✔
271
  metaReaderClear(&mer2);
19,855,281✔
272
_exit3:
31,792,707✔
273
  metaReaderClear(&mer1);
31,792,251✔
274
_exit4:
31,793,838✔
275
  rpcMsg.info = pMsg->info;
31,793,381✔
276
  rpcMsg.pCont = pRsp;
31,793,132✔
277
  rpcMsg.contLen = rspLen;
31,793,132✔
278
  rpcMsg.code = code;
31,793,132✔
279
  rpcMsg.msgType = pMsg->msgType;
31,793,132✔
280

281
  if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST && autoCreateCtb == 1) {
31,792,001✔
282
    code = TSDB_CODE_SUCCESS;
8,377,906✔
283
  }
284

285
  if (code) {
31,792,001✔
286
    qError("vgId:%d, get table %s meta with %" PRIu8 " failed cause of %s", pVnode->config.vgId, infoReq.tbName,
3,559,727✔
287
           infoReq.option, tstrerror(code));
288
  }
289

290
  if (direct) {
31,792,250✔
291
    tmsgSendRsp(&rpcMsg);
749,281✔
292
  } else {
293
    *pMsg = rpcMsg;
31,042,969✔
294
  }
295

296
  return code;
31,792,706✔
297
}
298

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

312
  // decode req
313
  if (tDeserializeSTableCfgReq(pMsg->pCont, pMsg->contLen, &cfgReq) != 0) {
71,716✔
314
    code = terrno;
×
315
    goto _exit;
×
316
  }
317

318
  tstrncpy(cfgRsp.tbName, cfgReq.tbName, TSDB_TABLE_NAME_LEN);
71,716✔
319
  (void)memcpy(cfgRsp.dbFName, cfgReq.dbFName, sizeof(cfgRsp.dbFName));
71,716✔
320

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

328
  // query meta
329
  metaReaderDoInit(&mer1, pVnode->pMeta, META_READER_LOCK);
71,716✔
330

331
  if (metaGetTableEntryByName(&mer1, cfgReq.tbName) < 0) {
71,716✔
332
    code = terrno;
×
333
    goto _exit;
×
334
  }
335

336
  cfgRsp.tableType = mer1.me.type;
71,716✔
337

338
  if (mer1.me.type == TSDB_SUPER_TABLE) {
71,716✔
339
    code = TSDB_CODE_VND_HASH_MISMATCH;
×
340
    goto _exit;
×
341
  } else if (mer1.me.type == TSDB_CHILD_TABLE || mer1.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
71,716✔
342
    metaReaderDoInit(&mer2, pVnode->pMeta, META_READER_NOLOCK);
41,604✔
343
    if (metaReaderGetTableEntryByUid(&mer2, mer1.me.ctbEntry.suid) < 0) goto _exit;
41,604✔
344

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

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

389
  if (NULL == cfgRsp.pSchemas || NULL == cfgRsp.pSchemaExt || NULL == cfgRsp.pColRefs) {
71,716✔
390
    code = terrno;
×
391
    goto _exit;
×
392
  }
393
  (void)memcpy(cfgRsp.pSchemas, schema.pSchema, sizeof(SSchema) * schema.nCols);
71,716✔
394
  if (schemaTag.nCols) {
71,716✔
395
    (void)memcpy(cfgRsp.pSchemas + schema.nCols, schemaTag.pSchema, sizeof(SSchema) * schemaTag.nCols);
41,604✔
396
  }
397

398
  SMetaReader     *pReader = (mer1.me.type == TSDB_CHILD_TABLE || mer1.me.type == TSDB_VIRTUAL_CHILD_TABLE) ? &mer2 : &mer1;
71,716✔
399
  SColCmprWrapper *pColCmpr = &pReader->me.colCmpr;
71,716✔
400
  SColRefWrapper  *pColRef = &mer1.me.colRef;
71,716✔
401

402
  if (withExtSchema(cfgRsp.tableType)) {
71,716✔
403
    for (int32_t i = 0; i < cfgRsp.numOfColumns; i++) {
314,978✔
404
      SColCmpr   *pCmpr = &pColCmpr->pColCmpr[i];
245,418✔
405
      SSchemaExt *pSchExt = cfgRsp.pSchemaExt + i;
245,418✔
406
      pSchExt->colId = pCmpr->id;
245,418✔
407
      pSchExt->compress = pCmpr->alg;
245,418✔
408
      if (pReader->me.pExtSchemas)
245,418✔
409
        pSchExt->typeMod = pReader->me.pExtSchemas[i].typeMod;
25,366✔
410
      else
411
        pSchExt->typeMod = 0;
220,052✔
412
    }
413
  }
414

415
  cfgRsp.virtualStb = false;
71,716✔
416
  if (hasRefCol(cfgRsp.tableType)) {
71,716✔
417
    for (int32_t i = 0; i < cfgRsp.numOfColumns; i++) {
30,792✔
418
      SColRef *pRef = &pColRef->pColRef[i];
28,636✔
419
      cfgRsp.pColRefs[i].hasRef = pRef->hasRef;
28,636✔
420
      cfgRsp.pColRefs[i].id = pRef->id;
28,636✔
421
      if (cfgRsp.pColRefs[i].hasRef) {
28,636✔
422
        tstrncpy(cfgRsp.pColRefs[i].refDbName, pRef->refDbName, TSDB_DB_NAME_LEN);
11,484✔
423
        tstrncpy(cfgRsp.pColRefs[i].refTableName, pRef->refTableName, TSDB_TABLE_NAME_LEN);
11,484✔
424
        tstrncpy(cfgRsp.pColRefs[i].refColName, pRef->refColName, TSDB_COL_NAME_LEN);
11,484✔
425
      }
426
    }
427
  }
428

429
  // encode and send response
430
  rspLen = tSerializeSTableCfgRsp(NULL, 0, &cfgRsp);
71,716✔
431
  if (rspLen < 0) {
71,716✔
432
    code = terrno;
×
433
    goto _exit;
×
434
  }
435

436
  if (direct) {
71,716✔
437
    pRsp = rpcMallocCont(rspLen);
×
438
  } else {
439
    pRsp = taosMemoryCalloc(1, rspLen);
71,716✔
440
  }
441

442
  if (pRsp == NULL) {
71,716✔
443
    code = terrno;
×
444
    goto _exit;
×
445
  }
446

447
  rspLen = tSerializeSTableCfgRsp(pRsp, rspLen, &cfgRsp);
71,716✔
448
  if (rspLen < 0) {
71,716✔
449
    code = terrno;
×
450
    goto _exit;
×
451
  }
452

453
_exit:
71,716✔
454
  rpcMsg.info = pMsg->info;
71,716✔
455
  rpcMsg.pCont = pRsp;
71,716✔
456
  rpcMsg.contLen = rspLen;
71,716✔
457
  rpcMsg.code = code;
71,716✔
458
  rpcMsg.msgType = pMsg->msgType;
71,716✔
459

460
  if (code) {
71,716✔
461
    qError("get table %s cfg failed cause of %s", cfgReq.tbName, tstrerror(code));
×
462
  }
463

464
  if (direct) {
71,716✔
465
    tmsgSendRsp(&rpcMsg);
×
466
  } else {
467
    *pMsg = rpcMsg;
71,716✔
468
  }
469

470
  tFreeSTableCfgRsp(&cfgRsp);
71,716✔
471
  metaReaderClear(&mer2);
71,716✔
472
  metaReaderClear(&mer1);
71,716✔
473
  return code;
71,716✔
474
}
475

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

481
  SBatchRspMsg *pRsp = (SBatchRspMsg *)p;
482
  rpcFreeCont(pRsp->msg);
483
}
484

485
int32_t vnodeGetBatchMeta(SVnode *pVnode, SRpcMsg *pMsg) {
26,837,320✔
486
  int32_t      code = 0;
26,837,320✔
487
  int32_t      rspSize = 0;
26,837,320✔
488
  SBatchReq    batchReq = {0};
26,837,320✔
489
  SBatchMsg   *req = NULL;
26,837,569✔
490
  SBatchRspMsg rsp = {0};
26,837,569✔
491
  SBatchRsp    batchRsp = {0};
26,835,415✔
492
  SRpcMsg      reqMsg = *pMsg;
26,835,415✔
493
  SRpcMsg      rspMsg = {0};
26,835,415✔
494
  void        *pRsp = NULL;
26,837,320✔
495

496
  if (tDeserializeSBatchReq(pMsg->pCont, pMsg->contLen, &batchReq)) {
26,837,320✔
497
    code = terrno;
×
498
    qError("tDeserializeSBatchReq failed");
×
499
    goto _exit;
×
500
  }
501

502
  int32_t msgNum = taosArrayGetSize(batchReq.pMsgs);
26,837,581✔
503
  if (msgNum >= MAX_META_MSG_IN_BATCH) {
26,838,213✔
504
    code = TSDB_CODE_INVALID_MSG;
×
505
    qError("too many msgs %d in vnode batch meta req", msgNum);
×
506
    goto _exit;
×
507
  }
508

509
  batchRsp.pRsps = taosArrayInit(msgNum, sizeof(SBatchRspMsg));
26,838,213✔
510
  if (NULL == batchRsp.pRsps) {
26,836,282✔
UNCOV
511
    code = terrno;
×
512
    qError("taosArrayInit %d SBatchRspMsg failed", msgNum);
×
513
    goto _exit;
×
514
  }
515

516
  for (int32_t i = 0; i < msgNum; ++i) {
58,906,244✔
517
    req = taosArrayGet(batchReq.pMsgs, i);
32,069,821✔
518
    if (req == NULL) {
32,069,945✔
519
      code = terrno;
×
520
      goto _exit;
×
521
    }
522

523
    reqMsg.msgType = req->msgType;
32,069,945✔
524
    reqMsg.pCont = req->msg;
32,070,329✔
525
    reqMsg.contLen = req->msgLen;
32,069,189✔
526

527
    switch (req->msgType) {
32,069,197✔
528
      case TDMT_VND_TABLE_META:
30,476,388✔
529
        // error code has been set into reqMsg, no need to handle it here.
530
        if (TSDB_CODE_SUCCESS != vnodeGetTableMeta(pVnode, &reqMsg, false)) {
30,476,388✔
531
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
2,666,478✔
532
        }
533
        break;
30,477,094✔
534
      case TDMT_VND_TABLE_NAME:
566,788✔
535
        // error code has been set into reqMsg, no need to handle it here.
536
        if (TSDB_CODE_SUCCESS != vnodeGetTableMeta(pVnode, &reqMsg, false)) {
566,788✔
537
          qWarn("vnodeGetBatchName failed, msgType:%d", req->msgType);
219,801✔
538
        }
539
        break;
566,788✔
540
      case TDMT_VND_TABLE_CFG:
71,716✔
541
        // error code has been set into reqMsg, no need to handle it here.
542
        if (TSDB_CODE_SUCCESS != vnodeGetTableCfg(pVnode, &reqMsg, false)) {
71,716✔
543
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
×
544
        }
545
        break;
71,716✔
546
      case TDMT_VND_VSUBTABLES_META:
×
547
        // error code has been set into reqMsg, no need to handle it here.
548
        if (TSDB_CODE_SUCCESS != vnodeGetVSubtablesMeta(pVnode, &reqMsg)) {
×
549
          qWarn("vnodeGetVSubtablesMeta failed, msgType:%d", req->msgType);
×
550
        }
551
        break;
×
552
      case TDMT_VND_VSTB_REF_DBS:
952,416✔
553
        // error code has been set into reqMsg, no need to handle it here.
554
        if (TSDB_CODE_SUCCESS != vnodeGetVStbRefDbs(pVnode, &reqMsg)) {
952,416✔
555
          qWarn("vnodeGetVStbRefDbs failed, msgType:%d", req->msgType);
×
556
        }
557
        break;
953,057✔
558
      default:
×
559
        qError("invalid req msgType %d", req->msgType);
×
560
        reqMsg.code = TSDB_CODE_INVALID_MSG;
×
561
        reqMsg.pCont = NULL;
×
562
        reqMsg.contLen = 0;
×
563
        break;
×
564
    }
565

566
    rsp.msgIdx = req->msgIdx;
32,068,655✔
567
    rsp.reqType = reqMsg.msgType;
32,069,903✔
568
    rsp.msgLen = reqMsg.contLen;
32,069,903✔
569
    rsp.rspCode = reqMsg.code;
32,069,903✔
570
    rsp.msg = reqMsg.pCont;
32,069,903✔
571

572
    if (NULL == taosArrayPush(batchRsp.pRsps, &rsp)) {
64,139,233✔
573
      qError("taosArrayPush failed");
×
574
      code = terrno;
×
575
      goto _exit;
×
576
    }
577
  }
578

579
  rspSize = tSerializeSBatchRsp(NULL, 0, &batchRsp);
26,836,423✔
580
  if (rspSize < 0) {
26,835,333✔
581
    qError("tSerializeSBatchRsp failed");
×
582
    code = terrno;
×
583
    goto _exit;
×
584
  }
585
  pRsp = rpcMallocCont(rspSize);
26,835,333✔
586
  if (pRsp == NULL) {
26,837,662✔
587
    qError("rpcMallocCont %d failed", rspSize);
×
588
    code = terrno;
×
589
    goto _exit;
×
590
  }
591
  if (tSerializeSBatchRsp(pRsp, rspSize, &batchRsp) < 0) {
26,837,662✔
592
    qError("tSerializeSBatchRsp %d failed", rspSize);
×
593
    code = terrno;
×
594
    goto _exit;
×
595
  }
596

597
_exit:
26,838,286✔
598

599
  rspMsg.info = pMsg->info;
26,838,286✔
600
  rspMsg.pCont = pRsp;
26,838,286✔
601
  rspMsg.contLen = rspSize;
26,838,286✔
602
  rspMsg.code = code;
26,838,286✔
603
  rspMsg.msgType = pMsg->msgType;
26,838,286✔
604

605
  if (code) {
26,835,790✔
606
    qError("vnd get batch meta failed cause of %s", tstrerror(code));
×
607
  }
608

609
  taosArrayDestroyEx(batchReq.pMsgs, tFreeSBatchReqMsg);
26,835,790✔
610
  taosArrayDestroyEx(batchRsp.pRsps, tFreeSBatchRspMsg);
26,837,047✔
611

612
  tmsgSendRsp(&rspMsg);
26,836,590✔
613

614
  return code;
26,838,919✔
615
}
616

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

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

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

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

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

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

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

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

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

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

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

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

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

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

690
      refColsNum++;
×
691
    }
692

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

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

701
_return:
×
702

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

707
  taosArrayDestroy(pList);
×
708
  taosMemoryFree(pTb);
×
709
  tSimpleHashCleanup(pSrcTbls);
×
710

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

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

725
  pList = taosArrayInit(10, sizeof(uint64_t));
954,929✔
726
  QUERY_CHECK_NULL(pList, code, line, _return, terrno);
953,672✔
727

728
  *ppRes = taosArrayInit(10, POINTER_BYTES);
953,672✔
729
  QUERY_CHECK_NULL(*ppRes, code, line, _return, terrno)
953,681✔
730
  
731
  // lookup in cache
732
  code = pHandle->api.metaFn.metaGetCachedRefDbs(pHandle->vnode, suid, *ppRes);
953,681✔
733
  QUERY_CHECK_CODE(code, line, _return);
954,929✔
734

735
  if (taosArrayGetSize(*ppRes) > 0) {
954,929✔
736
    // found in cache
737
    goto _return;
925,803✔
738
  } else {
739
    code = pHandle->api.metaFn.getChildTableList(pHandle->vnode, suid, pList);
29,126✔
740
    QUERY_CHECK_CODE(code, line, _return);
29,126✔
741

742
    size_t num = taosArrayGetSize(pList);
29,126✔
743
    pDbNameHash = tSimpleHashInit(10, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
29,126✔
744
    QUERY_CHECK_NULL(pDbNameHash, code, line, _return, terrno);
29,126✔
745

746
    for (int32_t i = 0; i < num; ++i) {
95,278✔
747
      uint64_t* id = taosArrayGet(pList, i);
66,152✔
748
      QUERY_CHECK_NULL(id, code, line, _return, terrno);
66,152✔
749

750
      pHandle->api.metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pHandle->api.metaFn);
66,152✔
751
      readerInit = true;
66,152✔
752

753
      code = pHandle->api.metaReaderFn.getTableEntryByUid(&mr, *id);
66,152✔
754
      QUERY_CHECK_CODE(code, line, _return);
66,152✔
755

756
      for (int32_t j = 0; j < mr.me.colRef.nCols; j++) {
545,068✔
757
        if (mr.me.colRef.pColRef[j].hasRef) {
478,916✔
758
          if (NULL == tSimpleHashGet(pDbNameHash, mr.me.colRef.pColRef[j].refDbName, strlen(mr.me.colRef.pColRef[j].refDbName))) {
328,207✔
759
            char *refDbName = taosStrdup(mr.me.colRef.pColRef[j].refDbName);
22,596✔
760
            QUERY_CHECK_NULL(refDbName, code, line, _return, terrno);
22,596✔
761

762
            QUERY_CHECK_NULL(taosArrayPush(*ppRes, &refDbName), code, line, _return, terrno);
45,192✔
763

764
            code = tSimpleHashPut(pDbNameHash, refDbName, strlen(refDbName), NULL, 0);
22,596✔
765
            QUERY_CHECK_CODE(code, line, _return);
22,596✔
766
          }
767
        }
768
      }
769

770
      pHandle->api.metaReaderFn.clearReader(&mr);
66,152✔
771
      readerInit = false;
66,152✔
772
    }
773

774
    code = pHandle->api.metaFn.metaPutRefDbsToCache(pHandle->vnode, suid, *ppRes);
29,126✔
775
    QUERY_CHECK_CODE(code, line, _return);
29,126✔
776
  }
777

778
_return:
954,929✔
779

780
  if (readerInit) {
954,929✔
781
    pHandle->api.metaReaderFn.clearReader(&mr);
×
782
  }
783

784
  taosArrayDestroy(pList);
954,929✔
785
  tSimpleHashCleanup(pDbNameHash);
954,305✔
786

787
  if (code) {
954,929✔
788
    qError("%s failed since %s", __func__, tstrerror(code));
×
789
  }
790
  return code;
954,929✔
791
}
792

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

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

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

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

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

834
_return:
×
835

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

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

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

850
  //tmsgSendRsp(&rspMsg);
851

852
  return code;
×
853
}
854

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

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

870
  SReadHandle handle = {0};
953,672✔
871
  handle.vnode = pVnode;
953,672✔
872
  initStorageAPI(&handle.api);
953,672✔
873

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

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

897
_return:
954,305✔
898

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

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

909
  *pMsg = rspMsg;
953,057✔
910

911
  tDestroySVStbRefDbsRsp(&rsp);
952,433✔
912

913
  return code;
951,809✔
914
}
915

916
static int32_t vnodeGetCompStorage(SVnode *pVnode, int64_t *output) {
147,496,526✔
917
  int32_t code = 0;
147,496,526✔
918
#ifdef TD_ENTERPRISE
919
  int32_t now = taosGetTimestampSec();
147,496,526✔
920
  if (llabs(now - pVnode->config.vndStats.storageLastUpd) >= 30) {
147,496,526✔
921
    pVnode->config.vndStats.storageLastUpd = now;
7,510,365✔
922

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

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

948
    if (pVnode->inUse) {
147,496,526✔
949
      *bufferSegmentUsed = pVnode->inUse->size;
147,473,714✔
950
    }
951
    *bufferSegmentSize = pVnode->config.szBuf / VNODE_BUFPOOL_SEGMENTS;
147,496,526✔
952

953
    (void)taosThreadMutexUnlock(&pVnode->mutex);
147,496,526✔
954
  }
955
}
147,496,526✔
956

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

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

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

1013
  if (dbname) {
6,354,152✔
1014
    *dbname = pConf->dbname;
6,284,357✔
1015
  }
1016

1017
  if (vgId) {
6,354,833✔
1018
    *vgId = TD_VID(pVnodeObj);
5,602,247✔
1019
  }
1020

1021
  if (numOfTables) {
6,355,457✔
1022
    *numOfTables = pConf->vndStats.numOfNTables + pConf->vndStats.numOfCTables +
112,565✔
1023
                   pConf->vndStats.numOfVTables + pConf->vndStats.numOfVCTables;
56,446✔
1024
  }
1025

1026
  if (numOfNormalTables) {
6,355,208✔
1027
    *numOfNormalTables = pConf->vndStats.numOfNTables +
24,036✔
1028
                         pConf->vndStats.numOfVTables;
12,044✔
1029
  }
1030
}
6,355,208✔
1031

1032
int32_t vnodeGetTableList(void *pVnode, int8_t type, SArray *pList) {
12,044✔
1033
  if (type == TSDB_SUPER_TABLE) {
12,044✔
1034
    return vnodeGetStbIdList(pVnode, 0, pList);
12,044✔
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) {
54,027,393✔
1071
  int32_t      code = TSDB_CODE_SUCCESS;
54,027,393✔
1072
  SVnode      *pVnodeObj = pVnode;
54,027,393✔
1073
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnodeObj, suid, 1);
54,027,393✔
1074
  if (NULL == pCur) {
54,019,101✔
1075
    qError("vnode get all table list failed");
×
1076
    return terrno;
×
1077
  }
1078

1079
  while (1) {
261,421,306✔
1080
    tb_uid_t id = metaCtbCursorNext(pCur);
315,440,407✔
1081
    if (id == 0) {
315,204,878✔
1082
      break;
54,022,739✔
1083
    }
1084
    qTrace("vnodeGetCtbIdList: got ctb id %" PRId64 " for suid %" PRId64, id, suid);
261,182,139✔
1085
    if (NULL == taosArrayPush(list, &id)) {
261,434,995✔
1086
      qError("taosArrayPush failed");
×
1087
      code = terrno;
×
1088
      goto _exit;
×
1089
    }
1090
  }
1091

1092
_exit:
53,988,755✔
1093
  metaCloseCtbCursor(pCur);
53,988,755✔
1094
  return code;
54,000,992✔
1095
}
1096

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

1104
  while (1) {
145,345,837✔
1105
    tb_uid_t id = metaStbCursorNext(pCur);
216,983,135✔
1106
    if (id == 0) {
216,983,812✔
1107
      break;
71,640,006✔
1108
    }
1109

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

1117
_exit:
71,636,621✔
1118
  metaCloseStbCursor(pCur);
71,636,621✔
1119
  return code;
71,636,589✔
1120
}
1121

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

1130
  while (1) {
1,170✔
1131
    tb_uid_t id = metaStbCursorNext(pCur);
1,404✔
1132
    if (id == 0) {
1,404✔
1133
      break;
234✔
1134
    }
1135

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

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

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

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

1158
  *num = 0;
7,252,699✔
1159
  while (1) {
2,392,399✔
1160
    tb_uid_t id = metaCtbCursorNext(pCur);
9,645,540✔
1161
    if (id == 0) {
9,644,933✔
1162
      break;
7,252,605✔
1163
    }
1164

1165
    ++(*num);
2,392,328✔
1166
  }
1167

1168
  metaCloseCtbCursor(pCur);
7,252,605✔
1169
  return TSDB_CODE_SUCCESS;
7,253,259✔
1170
}
1171

1172
int32_t vnodeGetStbColumnNum(SVnode *pVnode, tb_uid_t suid, int *num) {
7,253,259✔
1173
  SSchemaWrapper *pSW = metaGetTableSchema(pVnode->pMeta, suid, -1, 0, NULL, 0);
7,253,259✔
1174
  if (pSW) {
7,253,282✔
1175
    *num = pSW->nCols;
7,252,955✔
1176
    tDeleteSchemaWrapper(pSW);
1177
  } else {
1178
    *num = 2;
327✔
1179
  }
1180

1181
  return TSDB_CODE_SUCCESS;
7,253,212✔
1182
}
1183

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

1188
  int32_t code = metaReaderGetTableEntryByUid(&mr, suid);
7,252,698✔
1189
  if (code == TSDB_CODE_SUCCESS) {
7,253,282✔
1190
    if (keep) *keep = mr.me.stbEntry.keep;
7,252,955✔
1191
    if (flags) *flags = mr.me.flags;
7,252,955✔
1192
  } else {
1193
    if (keep) *keep = 0;
327✔
1194
    if (flags) *flags = 0;
327✔
1195
  }
1196

1197
  metaReaderClear(&mr);
7,253,282✔
1198
  return TSDB_CODE_SUCCESS;
7,253,282✔
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) {
71,626,300✔
1227
  int32_t      code = TSDB_CODE_SUCCESS;
71,626,300✔
1228
  int32_t      tbNum = 0;
71,626,300✔
1229
  const char **pTbArr = NULL;
71,626,300✔
1230
  const char  *dbName = NULL;
71,626,300✔
1231
  *tbSize = 0;
71,626,300✔
1232

1233
  if (!(dbName = strchr(pVnode->config.dbname, '.'))) return 0;
71,626,300✔
1234
  if (0 == strncmp(++dbName, "log", TSDB_DB_NAME_LEN)) {
71,625,623✔
1235
    tbNum = tkLogStbNum;
4,212✔
1236
    pTbArr = (const char **)&tkLogStb;
4,212✔
1237
  } else if (0 == strncmp(dbName, "audit", TSDB_DB_NAME_LEN)) {
71,622,088✔
1238
    tbNum = tkAuditStbNum;
1,736✔
1239
    pTbArr = (const char **)&tkAuditStb;
1,736✔
1240
  }
1241
  if (tbNum && pTbArr) {
71,626,300✔
1242
    *tbSize = metaSizeOfTbFilterCache(pVnode->pMeta, 0);
5,948✔
1243
    if (*tbSize < tbNum) {
5,948✔
1244
      for (int32_t i = 0; i < tbNum; ++i) {
68,758✔
1245
        tb_uid_t suid = metaGetTableEntryUidByName(pVnode->pMeta, pTbArr[i]);
64,916✔
1246
        if (suid != 0) {
64,916✔
1247
          code = metaPutTbToFilterCache(pVnode->pMeta, &suid, 0);
3,276✔
1248
          if (TSDB_CODE_SUCCESS != code) {
3,276✔
1249
            return code;
2,106✔
1250
          }
1251
        }
1252
      }
1253
      *tbSize = metaSizeOfTbFilterCache(pVnode->pMeta, 0);
3,842✔
1254
    }
1255
  }
1256

1257
  return code;
71,624,194✔
1258
}
1259
#endif
1260

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

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

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

1273
  if (!(suidList = taosArrayInit(1, sizeof(tb_uid_t)))) {
71,626,977✔
1274
    return terrno;
×
1275
  }
1276

1277
  int32_t tbFilterSize = 0;
71,626,977✔
1278
  int32_t code = TSDB_CODE_SUCCESS;
71,626,977✔
1279
#ifdef TD_ENTERPRISE
1280
  code = vnodeGetTimeSeriesBlackList(pVnode, &tbFilterSize);
71,626,977✔
1281
  if (TSDB_CODE_SUCCESS != code) {
71,626,300✔
1282
    goto _exit;
2,106✔
1283
  }
1284
#endif
1285

1286
  if ((!tbFilterSize && vnodeGetStbIdList(pVnode, 0, suidList) < 0) ||
71,624,194✔
1287
      (tbFilterSize && vnodeGetStbIdListByFilter(pVnode, 0, suidList, vnodeTimeSeriesFilter, pVnode) < 0)) {
71,622,163✔
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;
71,622,808✔
1294
  int64_t arrSize = taosArrayGetSize(suidList);
71,622,840✔
1295
  for (int64_t i = 0; i < arrSize; ++i) {
216,946,745✔
1296
    tb_uid_t suid = *(tb_uid_t *)taosArrayGet(suidList, i);
145,321,197✔
1297

1298
    int64_t ctbNum = 0;
145,323,228✔
1299
    int32_t numOfCols = 0;
145,324,582✔
1300
    int8_t  flags = 0;
145,325,259✔
1301
    code = metaGetStbStats(pVnode, suid, &ctbNum, &numOfCols, &flags);
145,323,228✔
1302
    if (TSDB_CODE_SUCCESS != code) {
145,325,936✔
1303
      goto _exit;
×
1304
    }
1305
    if (!TABLE_IS_VIRTUAL(flags)) {
145,325,936✔
1306
      *num += ctbNum * (numOfCols - 1);
145,023,795✔
1307
    }
1308
  }
1309

1310
_exit:
71,627,578✔
1311
  taosArrayDestroy(suidList);
71,627,654✔
1312
  return TSDB_CODE_SUCCESS;
71,624,914✔
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) {
4,335,665✔
1343
  if (pVnode == NULL) {
4,335,665✔
1344
    return NULL;
×
1345
  }
1346

1347
  return metaGetIdx(((SVnode *)pVnode)->pMeta);
4,335,665✔
1348
}
1349

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

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

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

1365
  char   *dirName[] = {VNODE_TSDB_DIR, VNODE_WAL_DIR, VNODE_META_DIR, VNODE_TSDB_CACHE_DIR};
7,523,069✔
1366
  int64_t dirSize[4];
7,522,150✔
1367

1368
  vnodeGetPrimaryPath(pVnode, false, path, TSDB_FILENAME_LEN);
7,523,069✔
1369
  int32_t offset = strlen(path);
7,523,069✔
1370

1371
  for (int i = 0; i < sizeof(dirName) / sizeof(dirName[0]); i++) {
37,615,345✔
1372
    int64_t size = {0};
30,092,276✔
1373
    (void)snprintf(path + offset, TSDB_FILENAME_LEN - offset, "%s%s", TD_DIRSEP, dirName[i]);
30,092,276✔
1374
    code = taosGetDirSize(path, &size);
30,092,276✔
1375
    if (code != 0) {
30,092,276✔
1376
      uWarn("vnode %d get dir %s %s size failed since %s", TD_VID(pVnode), path, dirName[i], tstrerror(code));
5,966✔
1377
    }
1378
    path[offset] = 0;
30,092,276✔
1379
    dirSize[i] = size;
30,092,276✔
1380
  }
1381

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

1398
  code = tsdbGetFsSize(pVnodeObj->pTsdb, pInfo);
7,523,069✔
1399
_exit:
7,523,069✔
1400
  return code;
7,523,069✔
1401
}
1402

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

1411
  SVnode      *pVnode1 = (SVnode *)pVnode;
×
1412
  SSyncMetrics syncMetrics = syncGetMetrics(pVnode1->sync);
×
1413

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

1435
  return 0;
×
1436
}
1437

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

1446
  SVnode *pVnode1 = (SVnode *)pVnode;
×
1447

1448
  // Reset vnode write metrics using atomic operations to subtract old values
1449
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.total_requests, pOldMetrics->total_requests);
×
1450
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.total_rows, pOldMetrics->total_rows);
×
1451
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.total_bytes, pOldMetrics->total_bytes);
×
1452

1453
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.fetch_batch_meta_time, pOldMetrics->fetch_batch_meta_time);
×
1454
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.fetch_batch_meta_count, pOldMetrics->fetch_batch_meta_count);
×
1455
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.preprocess_time, pOldMetrics->preprocess_time);
×
1456
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.apply_bytes, pOldMetrics->apply_bytes);
×
1457
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.apply_time, pOldMetrics->apply_time);
×
1458
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.commit_count, pOldMetrics->commit_count);
×
1459

1460
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.commit_time, pOldMetrics->commit_time);
×
1461
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.merge_time, pOldMetrics->merge_time);
×
1462

1463
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.memtable_wait_time, pOldMetrics->memtable_wait_time);
×
1464
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.blocked_commit_count, pOldMetrics->blocked_commit_count);
×
1465
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.block_commit_time, pOldMetrics->blocked_commit_time);
×
1466
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.merge_count, pOldMetrics->merge_count);
×
1467

1468
  // Reset new cache metrics
1469
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.last_cache_commit_time, pOldMetrics->last_cache_commit_time);
×
1470
  (void)atomic_sub_fetch_64(&pVnode1->writeMetrics.last_cache_commit_count, pOldMetrics->last_cache_commit_count);
×
1471

1472
  // Reset sync metrics
1473
  SSyncMetrics syncMetrics = {
×
1474
      .wal_write_bytes = pOldMetrics->wal_write_bytes,
×
1475
      .wal_write_time = pOldMetrics->wal_write_time,
×
1476
  };
1477
  syncResetMetrics(pVnode1->sync, &syncMetrics);
×
1478

1479
  return 0;
×
1480
}
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