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

taosdata / TDengine / #3541

26 Nov 2024 03:56AM UTC coverage: 60.776% (-0.07%) from 60.846%
#3541

push

travis-ci

web-flow
Merge pull request #28920 from taosdata/fix/TD-33008-3.0

fix(query)[TD-33008]. fix error handling in tsdbCacheRead

120076 of 252763 branches covered (47.51%)

Branch coverage included in aggregate %.

0 of 2 new or added lines in 1 file covered. (0.0%)

1395 existing lines in 154 files now uncovered.

200995 of 275526 relevant lines covered (72.95%)

19612328.37 hits per line

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

63.29
/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 "vnd.h"
18

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

27
int vnodeQueryOpen(SVnode *pVnode) {
13,702✔
28
  return qWorkerInit(NODE_TYPE_VNODE, TD_VID(pVnode), (void **)&pVnode->pQuery, &pVnode->msgCb);
13,702✔
29
}
30

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

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

35
int32_t fillTableColCmpr(SMetaReader *reader, SSchemaExt *pExt, int32_t numOfCol) {
1,318,673✔
36
  int8_t tblType = reader->me.type;
1,318,673✔
37
  if (useCompress(tblType)) {
1,318,673!
38
    SColCmprWrapper *p = &(reader->me.colCmpr);
1,318,699✔
39
    if (numOfCol != p->nCols) {
1,318,699!
40
      vError("fillTableColCmpr table type:%d, col num:%d, col cmpr num:%d mismatch", tblType, numOfCol, p->nCols);
×
41
      return TSDB_CODE_APP_ERROR;
×
42
    }
43
    for (int i = 0; i < p->nCols; i++) {
33,771,979✔
44
      SColCmpr *pCmpr = &p->pColCmpr[i];
32,453,280✔
45
      pExt[i].colId = pCmpr->id;
32,453,280✔
46
      pExt[i].compress = pCmpr->alg;
32,453,280✔
47
    }
48
  }
49
  return 0;
1,318,676✔
50
}
51

52
int32_t vnodeGetTableMeta(SVnode *pVnode, SRpcMsg *pMsg, bool direct) {
1,331,792✔
53
  STableInfoReq  infoReq = {0};
1,331,792✔
54
  STableMetaRsp  metaRsp = {0};
1,331,792✔
55
  SMetaReader    mer1 = {0};
1,331,792✔
56
  SMetaReader    mer2 = {0};
1,331,792✔
57
  char           tableFName[TSDB_TABLE_FNAME_LEN];
58
  bool           reqTbUid = false;
1,331,792✔
59
  SRpcMsg        rpcMsg = {0};
1,331,792✔
60
  int32_t        code = 0;
1,331,792✔
61
  int32_t        rspLen = 0;
1,331,792✔
62
  void          *pRsp = NULL;
1,331,792✔
63
  SSchemaWrapper schema = {0};
1,331,792✔
64
  SSchemaWrapper schemaTag = {0};
1,331,792✔
65

66
  // decode req
67
  if (tDeserializeSTableInfoReq(pMsg->pCont, pMsg->contLen, &infoReq) != 0) {
1,331,792!
68
    code = terrno;
×
69
    goto _exit4;
×
70
  }
71

72
  if (infoReq.option == REQ_OPT_TBUID) reqTbUid = true;
1,332,340✔
73
  metaRsp.dbId = pVnode->config.dbId;
1,332,340✔
74
  (void)strcpy(metaRsp.tbName, infoReq.tbName);
1,332,340✔
75
  (void)memcpy(metaRsp.dbFName, infoReq.dbFName, sizeof(metaRsp.dbFName));
1,332,340✔
76

77
  if (!reqTbUid) {
1,332,340✔
78
    TAOS_UNUSED(sprintf(tableFName, "%s.%s", infoReq.dbFName, infoReq.tbName));
1,331,715✔
79
    code = vnodeValidateTableHash(pVnode, tableFName);
1,331,715✔
80
    if (code) {
1,331,716!
81
      goto _exit4;
×
82
    }
83
  }
84

85
  // query meta
86
  metaReaderDoInit(&mer1, pVnode->pMeta, META_READER_LOCK);
1,332,341✔
87
  if (reqTbUid) {
1,332,232✔
88
    errno = 0;
534✔
89
    uint64_t tbUid = taosStr2UInt64(infoReq.tbName, NULL, 10);
534✔
90
    if (errno == ERANGE || tbUid == 0) {
534!
91
      code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
92
      goto _exit3;
131✔
93
    }
94
    char tbName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
534✔
95
    TAOS_CHECK_GOTO(metaGetTableNameByUid(pVnode, tbUid, tbName), NULL, _exit3);
534✔
96
    tstrncpy(metaRsp.tbName, varDataVal(tbName), TSDB_TABLE_NAME_LEN);
403✔
97
    TAOS_CHECK_GOTO(metaGetTableEntryByName(&mer1, varDataVal(tbName)), NULL, _exit3);
403!
98
  } else if (metaGetTableEntryByName(&mer1, infoReq.tbName) < 0) {
1,331,698✔
99
    code = terrno;
13,235✔
100
    goto _exit3;
13,329✔
101
  }
102

103
  metaRsp.tableType = mer1.me.type;
1,319,017✔
104
  metaRsp.vgId = TD_VID(pVnode);
1,319,017✔
105
  metaRsp.tuid = mer1.me.uid;
1,319,017✔
106

107
  if (mer1.me.type == TSDB_SUPER_TABLE) {
1,319,017✔
108
    (void)strcpy(metaRsp.stbName, mer1.me.name);
701,280✔
109
    schema = mer1.me.stbEntry.schemaRow;
701,280✔
110
    schemaTag = mer1.me.stbEntry.schemaTag;
701,280✔
111
    metaRsp.suid = mer1.me.uid;
701,280✔
112
  } else if (mer1.me.type == TSDB_CHILD_TABLE) {
617,737✔
113
    metaReaderDoInit(&mer2, pVnode->pMeta, META_READER_NOLOCK);
593,016✔
114
    if (metaReaderGetTableEntryByUid(&mer2, mer1.me.ctbEntry.suid) < 0) goto _exit2;
593,016!
115

116
    (void)strcpy(metaRsp.stbName, mer2.me.name);
593,020✔
117
    metaRsp.suid = mer2.me.uid;
593,020✔
118
    schema = mer2.me.stbEntry.schemaRow;
593,020✔
119
    schemaTag = mer2.me.stbEntry.schemaTag;
593,020✔
120
  } else if (mer1.me.type == TSDB_NORMAL_TABLE) {
24,721!
121
    schema = mer1.me.ntbEntry.schemaRow;
24,721✔
122
  } else {
123
    vError("vnodeGetTableMeta get invalid table type:%d", mer1.me.type);
×
124
    goto _exit3;
×
125
  }
126

127
  metaRsp.numOfTags = schemaTag.nCols;
1,319,021✔
128
  metaRsp.numOfColumns = schema.nCols;
1,319,021✔
129
  metaRsp.precision = pVnode->config.tsdbCfg.precision;
1,319,021✔
130
  metaRsp.sversion = schema.version;
1,319,021✔
131
  metaRsp.tversion = schemaTag.version;
1,319,021✔
132
  metaRsp.pSchemas = (SSchema *)taosMemoryMalloc(sizeof(SSchema) * (metaRsp.numOfColumns + metaRsp.numOfTags));
1,319,021✔
133
  metaRsp.pSchemaExt = (SSchemaExt *)taosMemoryCalloc(metaRsp.numOfColumns, sizeof(SSchemaExt));
1,318,954✔
134
  if (NULL == metaRsp.pSchemas || NULL == metaRsp.pSchemaExt) {
1,318,843!
135
    code = terrno;
×
136
    goto _exit;
×
137
  }
138
  (void)memcpy(metaRsp.pSchemas, schema.pSchema, sizeof(SSchema) * schema.nCols);
1,318,858✔
139
  if (schemaTag.nCols) {
1,318,858✔
140
    (void)memcpy(metaRsp.pSchemas + schema.nCols, schemaTag.pSchema, sizeof(SSchema) * schemaTag.nCols);
1,293,890✔
141
  }
142
  if (metaRsp.pSchemaExt) {
1,318,858✔
143
    SMetaReader *pReader = mer1.me.type == TSDB_CHILD_TABLE ? &mer2 : &mer1;
1,318,606✔
144
    code = fillTableColCmpr(pReader, metaRsp.pSchemaExt, metaRsp.numOfColumns);
1,318,606✔
145
    if (code < 0) {
1,318,851!
146
      goto _exit;
×
147
    }
148
  } else {
149
    code = TSDB_CODE_OUT_OF_MEMORY;
252✔
150
    goto _exit;
252✔
151
  }
152

153
  // encode and send response
154
  rspLen = tSerializeSTableMetaRsp(NULL, 0, &metaRsp);
1,318,851✔
155
  if (rspLen < 0) {
1,318,571✔
156
    code = terrno;
48✔
157
    goto _exit;
×
158
  }
159

160
  if (direct) {
1,318,523✔
161
    pRsp = rpcMallocCont(rspLen);
269✔
162
  } else {
163
    pRsp = taosMemoryCalloc(1, rspLen);
1,318,254✔
164
  }
165

166
  if (pRsp == NULL) {
1,318,408!
167
    code = terrno;
×
168
    goto _exit;
×
169
  }
170

171
  rspLen = tSerializeSTableMetaRsp(pRsp, rspLen, &metaRsp);
1,318,408✔
172
  if (rspLen < 0) {
1,318,876!
173
    code = terrno;
×
174
    goto _exit;
×
175
  }
176

177
_exit:
1,318,876✔
178
  taosMemoryFree(metaRsp.pSchemas);
1,319,128✔
179
  taosMemoryFree(metaRsp.pSchemaExt);
1,318,693✔
180
_exit2:
1,318,923✔
181
  metaReaderClear(&mer2);
1,318,923✔
182
_exit3:
1,332,210✔
183
  metaReaderClear(&mer1);
1,332,210✔
184
_exit4:
1,332,384✔
185
  rpcMsg.info = pMsg->info;
1,332,384✔
186
  rpcMsg.pCont = pRsp;
1,332,384✔
187
  rpcMsg.contLen = rspLen;
1,332,384✔
188
  rpcMsg.code = code;
1,332,384✔
189
  rpcMsg.msgType = pMsg->msgType;
1,332,384✔
190

191
  if (code) {
1,332,384✔
192
    qError("get table %s meta with %" PRIu8 " failed cause of %s", infoReq.tbName, infoReq.option, tstrerror(code));
13,460!
193
  }
194

195
  if (direct) {
1,332,251✔
196
    tmsgSendRsp(&rpcMsg);
3,072✔
197
  } else {
198
    *pMsg = rpcMsg;
1,329,179✔
199
  }
200

201
  return code;
1,332,114✔
202
}
203

204
int32_t vnodeGetTableCfg(SVnode *pVnode, SRpcMsg *pMsg, bool direct) {
94✔
205
  STableCfgReq   cfgReq = {0};
94✔
206
  STableCfgRsp   cfgRsp = {0};
94✔
207
  SMetaReader    mer1 = {0};
94✔
208
  SMetaReader    mer2 = {0};
94✔
209
  char           tableFName[TSDB_TABLE_FNAME_LEN];
210
  SRpcMsg        rpcMsg = {0};
94✔
211
  int32_t        code = 0;
94✔
212
  int32_t        rspLen = 0;
94✔
213
  void          *pRsp = NULL;
94✔
214
  SSchemaWrapper schema = {0};
94✔
215
  SSchemaWrapper schemaTag = {0};
94✔
216

217
  // decode req
218
  if (tDeserializeSTableCfgReq(pMsg->pCont, pMsg->contLen, &cfgReq) != 0) {
94!
219
    code = terrno;
×
220
    goto _exit;
×
221
  }
222

223
  (void)strcpy(cfgRsp.tbName, cfgReq.tbName);
94✔
224
  (void)memcpy(cfgRsp.dbFName, cfgReq.dbFName, sizeof(cfgRsp.dbFName));
94✔
225

226
  (void)sprintf(tableFName, "%s.%s", cfgReq.dbFName, cfgReq.tbName);
94✔
227
  code = vnodeValidateTableHash(pVnode, tableFName);
94✔
228
  if (code) {
94!
229
    goto _exit;
×
230
  }
231

232
  // query meta
233
  metaReaderDoInit(&mer1, pVnode->pMeta, META_READER_LOCK);
94✔
234

235
  if (metaGetTableEntryByName(&mer1, cfgReq.tbName) < 0) {
94!
236
    code = terrno;
×
237
    goto _exit;
×
238
  }
239

240
  cfgRsp.tableType = mer1.me.type;
94✔
241

242
  if (mer1.me.type == TSDB_SUPER_TABLE) {
94!
243
    code = TSDB_CODE_VND_HASH_MISMATCH;
×
244
    goto _exit;
×
245
  } else if (mer1.me.type == TSDB_CHILD_TABLE) {
94✔
246
    metaReaderDoInit(&mer2, pVnode->pMeta, META_READER_LOCK);
70✔
247
    if (metaReaderGetTableEntryByUid(&mer2, mer1.me.ctbEntry.suid) < 0) goto _exit;
70!
248

249
    (void)strcpy(cfgRsp.stbName, mer2.me.name);
70✔
250
    schema = mer2.me.stbEntry.schemaRow;
70✔
251
    schemaTag = mer2.me.stbEntry.schemaTag;
70✔
252
    cfgRsp.ttl = mer1.me.ctbEntry.ttlDays;
70✔
253
    cfgRsp.commentLen = mer1.me.ctbEntry.commentLen;
70✔
254
    if (mer1.me.ctbEntry.commentLen > 0) {
70!
255
      cfgRsp.pComment = taosStrdup(mer1.me.ctbEntry.comment);
×
256
      if (NULL == cfgRsp.pComment) {
×
257
        code = terrno;
×
258
        goto _exit;
×
259
      }
260
    }
261
    STag *pTag = (STag *)mer1.me.ctbEntry.pTags;
70✔
262
    cfgRsp.tagsLen = pTag->len;
70✔
263
    cfgRsp.pTags = taosMemoryMalloc(cfgRsp.tagsLen);
70✔
264
    if (NULL == cfgRsp.pTags) {
70!
265
      code = terrno;
×
266
      goto _exit;
×
267
    }
268
    (void)memcpy(cfgRsp.pTags, pTag, cfgRsp.tagsLen);
70✔
269
  } else if (mer1.me.type == TSDB_NORMAL_TABLE) {
24!
270
    schema = mer1.me.ntbEntry.schemaRow;
24✔
271
    cfgRsp.ttl = mer1.me.ntbEntry.ttlDays;
24✔
272
    cfgRsp.commentLen = mer1.me.ntbEntry.commentLen;
24✔
273
    if (mer1.me.ntbEntry.commentLen > 0) {
24!
274
      cfgRsp.pComment = taosStrdup(mer1.me.ntbEntry.comment);
×
275
      if (NULL == cfgRsp.pComment) {
×
276
        code = terrno;
×
277
        goto _exit;
×
278
      }
279
    }
280
  } else {
281
    vError("vnodeGetTableCfg get invalid table type:%d", mer1.me.type);
×
282
    return TSDB_CODE_APP_ERROR;
×
283
  }
284

285
  cfgRsp.numOfTags = schemaTag.nCols;
94✔
286
  cfgRsp.numOfColumns = schema.nCols;
94✔
287
  cfgRsp.pSchemas = (SSchema *)taosMemoryMalloc(sizeof(SSchema) * (cfgRsp.numOfColumns + cfgRsp.numOfTags));
94✔
288
  cfgRsp.pSchemaExt = (SSchemaExt *)taosMemoryMalloc(cfgRsp.numOfColumns * sizeof(SSchemaExt));
94✔
289

290
  if (NULL == cfgRsp.pSchemas || NULL == cfgRsp.pSchemaExt) {
94!
291
    code = terrno;
×
292
    goto _exit;
×
293
  }
294
  (void)memcpy(cfgRsp.pSchemas, schema.pSchema, sizeof(SSchema) * schema.nCols);
94✔
295
  if (schemaTag.nCols) {
94✔
296
    (void)memcpy(cfgRsp.pSchemas + schema.nCols, schemaTag.pSchema, sizeof(SSchema) * schemaTag.nCols);
70✔
297
  }
298

299
  // if (useCompress(cfgRsp.tableType)) {
300

301
  SMetaReader     *pReader = mer1.me.type == TSDB_CHILD_TABLE ? &mer2 : &mer1;
94✔
302
  SColCmprWrapper *pColCmpr = &pReader->me.colCmpr;
94✔
303

304
  for (int32_t i = 0; i < cfgRsp.numOfColumns; i++) {
788✔
305
    SColCmpr   *pCmpr = &pColCmpr->pColCmpr[i];
694✔
306
    SSchemaExt *pSchExt = cfgRsp.pSchemaExt + i;
694✔
307
    pSchExt->colId = pCmpr->id;
694✔
308
    pSchExt->compress = pCmpr->alg;
694✔
309
  }
310
  //}
311

312
  // encode and send response
313
  rspLen = tSerializeSTableCfgRsp(NULL, 0, &cfgRsp);
94✔
314
  if (rspLen < 0) {
94!
315
    code = terrno;
×
316
    goto _exit;
×
317
  }
318

319
  if (direct) {
94!
320
    pRsp = rpcMallocCont(rspLen);
×
321
  } else {
322
    pRsp = taosMemoryCalloc(1, rspLen);
94✔
323
  }
324

325
  if (pRsp == NULL) {
94!
326
    code = terrno;
×
327
    goto _exit;
×
328
  }
329

330
  rspLen = tSerializeSTableCfgRsp(pRsp, rspLen, &cfgRsp);
94✔
331
  if (rspLen < 0) {
94!
332
    code = terrno;
×
333
    goto _exit;
×
334
  }
335

336
_exit:
94✔
337
  rpcMsg.info = pMsg->info;
94✔
338
  rpcMsg.pCont = pRsp;
94✔
339
  rpcMsg.contLen = rspLen;
94✔
340
  rpcMsg.code = code;
94✔
341
  rpcMsg.msgType = pMsg->msgType;
94✔
342

343
  if (code) {
94!
344
    qError("get table %s cfg failed cause of %s", cfgReq.tbName, tstrerror(code));
×
345
  }
346

347
  if (direct) {
94!
348
    tmsgSendRsp(&rpcMsg);
×
349
  } else {
350
    *pMsg = rpcMsg;
94✔
351
  }
352

353
  tFreeSTableCfgRsp(&cfgRsp);
94✔
354
  metaReaderClear(&mer2);
94✔
355
  metaReaderClear(&mer1);
94✔
356
  return code;
94✔
357
}
358

359
static FORCE_INLINE void vnodeFreeSBatchRspMsg(void *p) {
360
  if (NULL == p) {
361
    return;
362
  }
363

364
  SBatchRspMsg *pRsp = (SBatchRspMsg *)p;
365
  rpcFreeCont(pRsp->msg);
366
}
367

368
int32_t vnodeGetBatchMeta(SVnode *pVnode, SRpcMsg *pMsg) {
1,113,063✔
369
  int32_t      code = 0;
1,113,063✔
370
  int32_t      rspSize = 0;
1,113,063✔
371
  SBatchReq    batchReq = {0};
1,113,063✔
372
  SBatchMsg   *req = NULL;
1,113,063✔
373
  SBatchRspMsg rsp = {0};
1,113,063✔
374
  SBatchRsp    batchRsp = {0};
1,113,063✔
375
  SRpcMsg      reqMsg = *pMsg;
1,113,063✔
376
  SRpcMsg      rspMsg = {0};
1,113,063✔
377
  void        *pRsp = NULL;
1,113,063✔
378

379
  if (tDeserializeSBatchReq(pMsg->pCont, pMsg->contLen, &batchReq)) {
1,113,063!
380
    code = terrno;
×
381
    qError("tDeserializeSBatchReq failed");
×
382
    goto _exit;
×
383
  }
384

385
  int32_t msgNum = taosArrayGetSize(batchReq.pMsgs);
1,113,300✔
386
  if (msgNum >= MAX_META_MSG_IN_BATCH) {
1,113,031!
387
    code = TSDB_CODE_INVALID_MSG;
×
388
    qError("too many msgs %d in vnode batch meta req", msgNum);
×
389
    goto _exit;
×
390
  }
391

392
  batchRsp.pRsps = taosArrayInit(msgNum, sizeof(SBatchRspMsg));
1,113,031✔
393
  if (NULL == batchRsp.pRsps) {
1,113,223✔
394
    code = terrno;
31✔
395
    qError("taosArrayInit %d SBatchRspMsg failed", msgNum);
×
396
    goto _exit;
×
397
  }
398

399
  for (int32_t i = 0; i < msgNum; ++i) {
2,447,807✔
400
    req = taosArrayGet(batchReq.pMsgs, i);
1,335,013✔
401
    if (req == NULL) {
1,334,811!
UNCOV
402
      code = terrno;
×
403
      goto _exit;
×
404
    }
405

406
    reqMsg.msgType = req->msgType;
1,334,832✔
407
    reqMsg.pCont = req->msg;
1,334,832✔
408
    reqMsg.contLen = req->msgLen;
1,334,832✔
409

410
    switch (req->msgType) {
1,334,832!
411
      case TDMT_VND_TABLE_META:
1,328,348✔
412
        // error code has been set into reqMsg, no need to handle it here.
413
        if (TSDB_CODE_SUCCESS != vnodeGetTableMeta(pVnode, &reqMsg, false)) {
1,328,348✔
414
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
10,526!
415
        }
416
        break;
1,328,464✔
417
      case TDMT_VND_TABLE_NAME:
534✔
418
        // error code has been set into reqMsg, no need to handle it here.
419
        if (TSDB_CODE_SUCCESS != vnodeGetTableMeta(pVnode, &reqMsg, false)) {
534✔
420
          qWarn("vnodeGetBatchName failed, msgType:%d", req->msgType);
131!
421
        }
422
        break;
534✔
423
      case TDMT_VND_TABLE_CFG:
94✔
424
        // error code has been set into reqMsg, no need to handle it here.
425
        if (TSDB_CODE_SUCCESS != vnodeGetTableCfg(pVnode, &reqMsg, false)) {
94!
426
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
×
427
        }
428
        break;
94✔
429
      case TDMT_VND_GET_STREAM_PROGRESS:
5,856✔
430
        // error code has been set into reqMsg, no need to handle it here.
431
        if (TSDB_CODE_SUCCESS != vnodeGetStreamProgress(pVnode, &reqMsg, false)) {
5,856!
432
          qWarn("vnodeGetBatchMeta failed, msgType:%d", req->msgType);
×
433
        }
434
        break;
5,857✔
435
      default:
×
436
        qError("invalid req msgType %d", req->msgType);
×
437
        reqMsg.code = TSDB_CODE_INVALID_MSG;
×
438
        reqMsg.pCont = NULL;
×
439
        reqMsg.contLen = 0;
×
440
        break;
×
441
    }
442

443
    rsp.msgIdx = req->msgIdx;
1,334,949✔
444
    rsp.reqType = reqMsg.msgType;
1,334,949✔
445
    rsp.msgLen = reqMsg.contLen;
1,334,949✔
446
    rsp.rspCode = reqMsg.code;
1,334,949✔
447
    rsp.msg = reqMsg.pCont;
1,334,949✔
448

449
    if (NULL == taosArrayPush(batchRsp.pRsps, &rsp)) {
2,669,564!
450
      qError("taosArrayPush failed");
×
451
      code = terrno;
×
452
      goto _exit;
×
453
    }
454
  }
455

456
  rspSize = tSerializeSBatchRsp(NULL, 0, &batchRsp);
1,112,794✔
457
  if (rspSize < 0) {
1,112,977!
458
    qError("tSerializeSBatchRsp failed");
×
459
    code = terrno;
×
460
    goto _exit;
×
461
  }
462
  pRsp = rpcMallocCont(rspSize);
1,112,977✔
463
  if (pRsp == NULL) {
1,113,025!
464
    qError("rpcMallocCont %d failed", rspSize);
×
465
    code = terrno;
×
466
    goto _exit;
×
467
  }
468
  if (tSerializeSBatchRsp(pRsp, rspSize, &batchRsp) < 0) {
1,113,025!
469
    qError("tSerializeSBatchRsp %d failed", rspSize);
×
470
    code = terrno;
×
471
    goto _exit;
×
472
  }
473

474
_exit:
1,113,005✔
475

476
  rspMsg.info = pMsg->info;
1,113,005✔
477
  rspMsg.pCont = pRsp;
1,113,005✔
478
  rspMsg.contLen = rspSize;
1,113,005✔
479
  rspMsg.code = code;
1,113,005✔
480
  rspMsg.msgType = pMsg->msgType;
1,113,005✔
481

482
  if (code) {
1,113,005!
483
    qError("vnd get batch meta failed cause of %s", tstrerror(code));
×
484
  }
485

486
  taosArrayDestroyEx(batchReq.pMsgs, tFreeSBatchReqMsg);
1,113,005✔
487
  taosArrayDestroyEx(batchRsp.pRsps, tFreeSBatchRspMsg);
1,113,098✔
488

489
  tmsgSendRsp(&rspMsg);
1,113,126✔
490

491
  return code;
1,113,087✔
492
}
493

494
int32_t vnodeGetLoad(SVnode *pVnode, SVnodeLoad *pLoad) {
957,395✔
495
  SSyncState state = syncGetState(pVnode->sync);
957,395✔
496

497
  pLoad->vgId = TD_VID(pVnode);
957,395✔
498
  pLoad->syncState = state.state;
957,395✔
499
  pLoad->syncRestore = state.restored;
957,395✔
500
  pLoad->syncTerm = state.term;
957,395✔
501
  pLoad->roleTimeMs = state.roleTimeMs;
957,395✔
502
  pLoad->startTimeMs = state.startTimeMs;
957,395✔
503
  pLoad->syncCanRead = state.canRead;
957,395✔
504
  pLoad->learnerProgress = state.progress;
957,395✔
505
  pLoad->cacheUsage = tsdbCacheGetUsage(pVnode);
957,395✔
506
  pLoad->numOfCachedTables = tsdbCacheGetElems(pVnode);
957,395✔
507
  pLoad->numOfTables = metaGetTbNum(pVnode->pMeta);
957,395✔
508
  pLoad->numOfTimeSeries = metaGetTimeSeriesNum(pVnode->pMeta, 1);
957,395✔
509
  pLoad->totalStorage = (int64_t)3 * 1073741824;
957,395✔
510
  pLoad->compStorage = (int64_t)2 * 1073741824;
957,395✔
511
  pLoad->pointsWritten = 100;
957,395✔
512
  pLoad->numOfSelectReqs = 1;
957,395✔
513
  pLoad->numOfInsertReqs = atomic_load_64(&pVnode->statis.nInsert);
957,395✔
514
  pLoad->numOfInsertSuccessReqs = atomic_load_64(&pVnode->statis.nInsertSuccess);
957,395✔
515
  pLoad->numOfBatchInsertReqs = atomic_load_64(&pVnode->statis.nBatchInsert);
957,395✔
516
  pLoad->numOfBatchInsertSuccessReqs = atomic_load_64(&pVnode->statis.nBatchInsertSuccess);
957,395✔
517
  return 0;
957,395✔
518
}
519

520
int32_t vnodeGetLoadLite(SVnode *pVnode, SVnodeLoadLite *pLoad) {
×
521
  SSyncState syncState = syncGetState(pVnode->sync);
×
522
  if (syncState.state == TAOS_SYNC_STATE_LEADER || syncState.state == TAOS_SYNC_STATE_ASSIGNED_LEADER) {
×
523
    pLoad->vgId = TD_VID(pVnode);
×
524
    pLoad->nTimeSeries = metaGetTimeSeriesNum(pVnode->pMeta, 1);
×
525
    return 0;
×
526
  }
527
  return -1;
×
528
}
529
/**
530
 * @brief Reset the statistics value by monitor interval
531
 *
532
 * @param pVnode
533
 * @param pLoad
534
 */
535
void vnodeResetLoad(SVnode *pVnode, SVnodeLoad *pLoad) {
26✔
536
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nInsert, pLoad->numOfInsertReqs, 64, "nInsert");
26!
537
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nInsertSuccess, pLoad->numOfInsertSuccessReqs, 64, "nInsertSuccess");
26!
538
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nBatchInsert, pLoad->numOfBatchInsertReqs, 64, "nBatchInsert");
26!
539
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nBatchInsertSuccess, pLoad->numOfBatchInsertSuccessReqs, 64,
26!
540
                            "nBatchInsertSuccess");
541
}
26✔
542

543
void vnodeGetInfo(void *pVnode, const char **dbname, int32_t *vgId, int64_t *numOfTables, int64_t *numOfNormalTables) {
1,208,221✔
544
  SVnode    *pVnodeObj = pVnode;
1,208,221✔
545
  SVnodeCfg *pConf = &pVnodeObj->config;
1,208,221✔
546

547
  if (dbname) {
1,208,221✔
548
    *dbname = pConf->dbname;
923,254✔
549
  }
550

551
  if (vgId) {
1,208,221✔
552
    *vgId = TD_VID(pVnodeObj);
922,204✔
553
  }
554

555
  if (numOfTables) {
1,208,221✔
556
    *numOfTables = pConf->vndStats.numOfNTables + pConf->vndStats.numOfCTables;
286,080✔
557
  }
558

559
  if (numOfNormalTables) {
1,208,221✔
560
    *numOfNormalTables = pConf->vndStats.numOfNTables;
35✔
561
  }
562
}
1,208,221✔
563

564
int32_t vnodeGetTableList(void *pVnode, int8_t type, SArray *pList) {
35✔
565
  if (type == TSDB_SUPER_TABLE) {
35!
566
    return vnodeGetStbIdList(pVnode, 0, pList);
35✔
567
  } else {
568
    return TSDB_CODE_INVALID_PARA;
×
569
  }
570
}
571

572
int32_t vnodeGetAllTableList(SVnode *pVnode, uint64_t uid, SArray *list) {
×
573
  int32_t      code = TSDB_CODE_SUCCESS;
×
574
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, uid, 1);
×
575
  if (NULL == pCur) {
×
576
    qError("vnode get all table list failed");
×
577
    return terrno;
×
578
  }
579

580
  while (1) {
×
581
    tb_uid_t id = metaCtbCursorNext(pCur);
×
582
    if (id == 0) {
×
583
      break;
×
584
    }
585

586
    STableKeyInfo info = {uid = id};
×
587
    if (NULL == taosArrayPush(list, &info)) {
×
588
      qError("taosArrayPush failed");
×
589
      code = terrno;
×
590
      goto _exit;
×
591
    }
592
  }
593
_exit:
×
594
  metaCloseCtbCursor(pCur);
×
595
  return code;
×
596
}
597

598
int32_t vnodeGetCtbIdListByFilter(SVnode *pVnode, int64_t suid, SArray *list, bool (*filter)(void *arg), void *arg) {
×
599
  return 0;
×
600
}
601

602
int32_t vnodeGetCtbIdList(void *pVnode, int64_t suid, SArray *list) {
2,932,356✔
603
  int32_t      code = TSDB_CODE_SUCCESS;
2,932,356✔
604
  SVnode      *pVnodeObj = pVnode;
2,932,356✔
605
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnodeObj, suid, 1);
2,932,356✔
606
  if (NULL == pCur) {
2,935,405!
607
    qError("vnode get all table list failed");
×
608
    return terrno;
×
609
  }
610

611
  while (1) {
9,373,420✔
612
    tb_uid_t id = metaCtbCursorNext(pCur);
12,308,825✔
613
    if (id == 0) {
12,304,865✔
614
      break;
2,936,575✔
615
    }
616

617
    if (NULL == taosArrayPush(list, &id)) {
9,373,420!
618
      qError("taosArrayPush failed");
×
619
      code = terrno;
×
620
      goto _exit;
×
621
    }
622
  }
623

624
_exit:
2,936,575✔
625
  metaCloseCtbCursor(pCur);
2,936,575✔
626
  return code;
2,936,875✔
627
}
628

629
int32_t vnodeGetStbIdList(SVnode *pVnode, int64_t suid, SArray *list) {
163,985✔
630
  int32_t      code = TSDB_CODE_SUCCESS;
163,985✔
631
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, suid);
163,985✔
632
  if (!pCur) {
163,985!
633
    return TSDB_CODE_OUT_OF_MEMORY;
×
634
  }
635

636
  while (1) {
167,351✔
637
    tb_uid_t id = metaStbCursorNext(pCur);
331,336✔
638
    if (id == 0) {
331,319✔
639
      break;
163,984✔
640
    }
641

642
    if (NULL == taosArrayPush(list, &id)) {
167,351!
643
      qError("taosArrayPush failed");
×
644
      code = terrno;
×
645
      goto _exit;
×
646
    }
647
  }
648

649
_exit:
163,984✔
650
  metaCloseStbCursor(pCur);
163,984✔
651
  return code;
163,985✔
652
}
653

654
int32_t vnodeGetStbIdListByFilter(SVnode *pVnode, int64_t suid, SArray *list, bool (*filter)(void *arg, void *arg1),
2✔
655
                                  void *arg) {
656
  int32_t      code = TSDB_CODE_SUCCESS;
2✔
657
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, suid);
2✔
658
  if (!pCur) {
2!
659
    return terrno;
×
660
  }
661

662
  while (1) {
10✔
663
    tb_uid_t id = metaStbCursorNext(pCur);
12✔
664
    if (id == 0) {
12✔
665
      break;
2✔
666
    }
667

668
    if ((*filter) && (*filter)(arg, &id)) {
10!
669
      continue;
10✔
670
    }
671

672
    if (NULL == taosArrayPush(list, &id)) {
×
673
      qError("taosArrayPush failed");
×
674
      code = terrno;
×
675
      goto _exit;
×
676
    }
677
  }
678

679
_exit:
2✔
680
  metaCloseStbCursor(pCur);
2✔
681
  return code;
2✔
682
}
683

684
int32_t vnodeGetCtbNum(SVnode *pVnode, int64_t suid, int64_t *num) {
24,839✔
685
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, suid, 0);
24,839✔
686
  if (!pCur) {
24,843!
687
    return terrno;
×
688
  }
689

690
  *num = 0;
24,843✔
691
  while (1) {
41,907✔
692
    tb_uid_t id = metaCtbCursorNext(pCur);
66,750✔
693
    if (id == 0) {
66,750✔
694
      break;
24,843✔
695
    }
696

697
    ++(*num);
41,907✔
698
  }
699

700
  metaCloseCtbCursor(pCur);
24,843✔
701
  return TSDB_CODE_SUCCESS;
24,845✔
702
}
703

704
int32_t vnodeGetStbColumnNum(SVnode *pVnode, tb_uid_t suid, int *num) {
24,845✔
705
  SSchemaWrapper *pSW = metaGetTableSchema(pVnode->pMeta, suid, -1, 0, NULL);
24,845✔
706
  if (pSW) {
24,844✔
707
    *num = pSW->nCols;
24,843!
708
    tDeleteSchemaWrapper(pSW);
709
  } else {
710
    *num = 2;
1✔
711
  }
712

713
  return TSDB_CODE_SUCCESS;
24,846✔
714
}
715

716
#ifdef TD_ENTERPRISE
717
const char *tkLogStb[] = {"cluster_info",
718
                          "data_dir",
719
                          "dnodes_info",
720
                          "d_info",
721
                          "grants_info",
722
                          "keeper_monitor",
723
                          "logs",
724
                          "log_dir",
725
                          "log_summary",
726
                          "m_info",
727
                          "taosadapter_restful_http_request_fail",
728
                          "taosadapter_restful_http_request_in_flight",
729
                          "taosadapter_restful_http_request_summary_milliseconds",
730
                          "taosadapter_restful_http_request_total",
731
                          "taosadapter_system_cpu_percent",
732
                          "taosadapter_system_mem_percent",
733
                          "temp_dir",
734
                          "vgroups_info",
735
                          "vnodes_role"};
736
const char *tkAuditStb[] = {"operations"};
737
const int   tkLogStbNum = ARRAY_SIZE(tkLogStb);
738
const int   tkAuditStbNum = ARRAY_SIZE(tkAuditStb);
739

740
// exclude stbs of taoskeeper log
741
static int32_t vnodeGetTimeSeriesBlackList(SVnode *pVnode, int32_t *tbSize) {
163,949✔
742
  int32_t      code = TSDB_CODE_SUCCESS;
163,949✔
743
  int32_t      tbNum = 0;
163,949✔
744
  const char **pTbArr = NULL;
163,949✔
745
  const char  *dbName = NULL;
163,949✔
746
  *tbSize = 0;
163,949✔
747

748
  if (!(dbName = strchr(pVnode->config.dbname, '.'))) return 0;
163,949!
749
  if (0 == strncmp(++dbName, "log", TSDB_DB_NAME_LEN)) {
163,949✔
750
    tbNum = tkLogStbNum;
18✔
751
    pTbArr = (const char **)&tkLogStb;
18✔
752
  } else if (0 == strncmp(dbName, "audit", TSDB_DB_NAME_LEN)) {
163,931!
753
    tbNum = tkAuditStbNum;
×
754
    pTbArr = (const char **)&tkAuditStb;
×
755
  }
756
  if (tbNum && pTbArr) {
163,949!
757
    *tbSize = metaSizeOfTbFilterCache(pVnode->pMeta, 0);
18✔
758
    if (*tbSize < tbNum) {
18!
759
      for (int32_t i = 0; i < tbNum; ++i) {
234✔
760
        tb_uid_t suid = metaGetTableEntryUidByName(pVnode->pMeta, pTbArr[i]);
230✔
761
        if (suid != 0) {
230✔
762
          code = metaPutTbToFilterCache(pVnode->pMeta, &suid, 0);
24✔
763
          if (TSDB_CODE_SUCCESS != code) {
24✔
764
            return code;
14✔
765
          }
766
        }
767
      }
768
      *tbSize = metaSizeOfTbFilterCache(pVnode->pMeta, 0);
4✔
769
    }
770
  }
771

772
  return code;
163,935✔
773
}
774
#endif
775

776
static bool vnodeTimeSeriesFilter(void *arg1, void *arg2) {
10✔
777
  SVnode *pVnode = (SVnode *)arg1;
10✔
778

779
  if (metaTbInFilterCache(pVnode->pMeta, arg2, 0)) {
10!
780
    return true;
10✔
781
  }
782
  return false;
×
783
}
784

785
int32_t vnodeGetTimeSeriesNum(SVnode *pVnode, int64_t *num) {
163,949✔
786
  SArray *suidList = NULL;
163,949✔
787

788
  if (!(suidList = taosArrayInit(1, sizeof(tb_uid_t)))) {
163,949!
789
    return terrno;
×
790
  }
791

792
  int32_t tbFilterSize = 0;
163,950✔
793
  int32_t code = TSDB_CODE_SUCCESS;
163,950✔
794
#ifdef TD_ENTERPRISE
795
  code = vnodeGetTimeSeriesBlackList(pVnode, &tbFilterSize);
163,950✔
796
  if (TSDB_CODE_SUCCESS != code) {
163,950✔
797
    goto _exit;
14✔
798
  }
799
#endif
800

801
  if ((!tbFilterSize && vnodeGetStbIdList(pVnode, 0, suidList) < 0) ||
163,936✔
802
      (tbFilterSize && vnodeGetStbIdListByFilter(pVnode, 0, suidList, vnodeTimeSeriesFilter, pVnode) < 0)) {
163,934!
803
    qError("vgId:%d, failed to get stb id list error: %s", TD_VID(pVnode), terrstr());
×
804
    taosArrayDestroy(suidList);
×
805
    return terrno;
×
806
  }
807

808
  *num = 0;
163,935✔
809
  int64_t arrSize = taosArrayGetSize(suidList);
163,935✔
810
  for (int64_t i = 0; i < arrSize; ++i) {
331,251✔
811
    tb_uid_t suid = *(tb_uid_t *)taosArrayGet(suidList, i);
167,311✔
812

813
    int64_t ctbNum = 0;
167,307✔
814
    int32_t numOfCols = 0;
167,307✔
815
    code = metaGetStbStats(pVnode, suid, &ctbNum, &numOfCols);
167,307✔
816
    if (TSDB_CODE_SUCCESS != code) {
167,317!
817
      goto _exit;
×
818
    }
819
    *num += ctbNum * (numOfCols - 1);
167,317✔
820
  }
821

822
_exit:
163,940✔
823
  taosArrayDestroy(suidList);
163,954✔
824
  return TSDB_CODE_SUCCESS;
163,950✔
825
}
826

827
int32_t vnodeGetAllCtbNum(SVnode *pVnode, int64_t *num) {
×
828
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, 0);
×
829
  if (!pCur) {
×
830
    return terrno;
×
831
  }
832

833
  *num = 0;
×
834
  while (1) {
×
835
    tb_uid_t id = metaStbCursorNext(pCur);
×
836
    if (id == 0) {
×
837
      break;
×
838
    }
839

840
    int64_t ctbNum = 0;
×
841
    int32_t code = vnodeGetCtbNum(pVnode, id, &ctbNum);
×
842
    if (TSDB_CODE_SUCCESS != code) {
×
843
      metaCloseStbCursor(pCur);
×
844
      return code;
×
845
    }
846

847
    *num += ctbNum;
×
848
  }
849

850
  metaCloseStbCursor(pCur);
×
851
  return TSDB_CODE_SUCCESS;
×
852
}
853

854
void *vnodeGetIdx(void *pVnode) {
47,413✔
855
  if (pVnode == NULL) {
47,413!
856
    return NULL;
×
857
  }
858

859
  return metaGetIdx(((SVnode *)pVnode)->pMeta);
47,413✔
860
}
861

862
void *vnodeGetIvtIdx(void *pVnode) {
47,383✔
863
  if (pVnode == NULL) {
47,383!
864
    return NULL;
×
865
  }
866
  return metaGetIvtIdx(((SVnode *)pVnode)->pMeta);
47,383✔
867
}
868

869
int32_t vnodeGetTableSchema(void *pVnode, int64_t uid, STSchema **pSchema, int64_t *suid) {
839✔
870
  return tsdbGetTableSchema(((SVnode *)pVnode)->pMeta, uid, pSchema, suid);
839✔
871
}
872

873
int32_t vnodeGetStreamProgress(SVnode *pVnode, SRpcMsg *pMsg, bool direct) {
5,857✔
874
  int32_t            code = 0;
5,857✔
875
  SStreamProgressReq req;
876
  SStreamProgressRsp rsp = {0};
5,857✔
877
  SRpcMsg            rpcMsg = {.info = pMsg->info, .code = 0};
5,857✔
878
  char              *buf = NULL;
5,857✔
879
  int32_t            rspLen = 0;
5,857✔
880
  code = tDeserializeStreamProgressReq(pMsg->pCont, pMsg->contLen, &req);
5,857✔
881

882
  if (code == TSDB_CODE_SUCCESS) {
5,858!
883
    rsp.fetchIdx = req.fetchIdx;
5,858✔
884
    rsp.subFetchIdx = req.subFetchIdx;
5,858✔
885
    rsp.vgId = req.vgId;
5,858✔
886
    rsp.streamId = req.streamId;
5,858✔
887
    rspLen = tSerializeStreamProgressRsp(0, 0, &rsp);
5,858✔
888
    if (rspLen < 0) {
5,858✔
889
      code = terrno;
1✔
890
      goto _OVER;
×
891
    }
892
    if (direct) {
5,857!
893
      buf = rpcMallocCont(rspLen);
×
894
    } else {
895
      buf = taosMemoryCalloc(1, rspLen);
5,857✔
896
    }
897
    if (!buf) {
5,857!
898
      code = terrno;
×
899
      goto _OVER;
×
900
    }
901
  }
902

903
  if (code == TSDB_CODE_SUCCESS) {
5,857!
904
    code = tqGetStreamExecInfo(pVnode, req.streamId, &rsp.progressDelay, &rsp.fillHisFinished);
5,857✔
905
  }
906
  if (code == TSDB_CODE_SUCCESS) {
5,858!
907
    rspLen = tSerializeStreamProgressRsp(buf, rspLen, &rsp);
5,858✔
908
    if (rspLen < 0) {
5,858!
UNCOV
909
      code = terrno;
×
910
      goto _OVER;
×
911
    }
912
    rpcMsg.pCont = buf;
5,858✔
913
    buf = NULL;
5,858✔
914
    rpcMsg.contLen = rspLen;
5,858✔
915
    rpcMsg.code = code;
5,858✔
916
    rpcMsg.msgType = pMsg->msgType;
5,858✔
917
    if (direct) {
5,858!
918
      tmsgSendRsp(&rpcMsg);
×
919
    } else {
920
      *pMsg = rpcMsg;
5,858✔
921
    }
922
  }
923

924
_OVER:
×
925
  if (buf) {
5,858!
926
    taosMemoryFree(buf);
×
927
  }
928
  return code;
5,858✔
929
}
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

© 2025 Coveralls, Inc