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

H
Hongze Cheng 已提交
16
#include "vnd.h"
D
dapan1121 已提交
17

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

S
Shengliang Guan 已提交
27
int vnodeQueryOpen(SVnode *pVnode) {
D
dapan1121 已提交
28
  return qWorkerInit(NODE_TYPE_VNODE, TD_VID(pVnode), (void **)&pVnode->pQuery, &pVnode->msgCb);
S
Shengliang Guan 已提交
29
}
D
dapan1121 已提交
30

D
dapan1121 已提交
31
void vnodeQueryPreClose(SVnode *pVnode) { qWorkerStopAllTasks((void *)pVnode->pQuery); }
D
dapan1121 已提交
32

L
Liu Jicong 已提交
33
void vnodeQueryClose(SVnode *pVnode) { qWorkerDestroy((void **)&pVnode->pQuery); }
D
dapan1121 已提交
34

D
dapan1121 已提交
35
int vnodeGetTableMeta(SVnode *pVnode, SRpcMsg *pMsg, bool direct) {
H
Hongze Cheng 已提交
36 37 38 39 40
  STableInfoReq  infoReq = {0};
  STableMetaRsp  metaRsp = {0};
  SMetaReader    mer1 = {0};
  SMetaReader    mer2 = {0};
  char           tableFName[TSDB_TABLE_FNAME_LEN];
dengyihao's avatar
dengyihao 已提交
41
  SRpcMsg        rpcMsg = {0};
H
Hongze Cheng 已提交
42 43
  int32_t        code = 0;
  int32_t        rspLen = 0;
44
  void          *pRsp = NULL;
H
Hongze Cheng 已提交
45 46
  SSchemaWrapper schema = {0};
  SSchemaWrapper schemaTag = {0};
H
Hongze Cheng 已提交
47 48 49 50 51 52 53

  // decode req
  if (tDeserializeSTableInfoReq(pMsg->pCont, pMsg->contLen, &infoReq) != 0) {
    code = TSDB_CODE_INVALID_MSG;
    goto _exit;
  }

H
Hongze Cheng 已提交
54
  metaRsp.dbId = pVnode->config.dbId;
D
dapan1121 已提交
55
  strcpy(metaRsp.tbName, infoReq.tbName);
H
Hongze Cheng 已提交
56
  memcpy(metaRsp.dbFName, infoReq.dbFName, sizeof(metaRsp.dbFName));
H
Hongze Cheng 已提交
57

D
dapan1121 已提交
58
  sprintf(tableFName, "%s.%s", infoReq.dbFName, infoReq.tbName);
H
Hongze Cheng 已提交
59
  code = vnodeValidateTableHash(pVnode, tableFName);
D
dapan1121 已提交
60 61 62 63
  if (code) {
    goto _exit;
  }

H
Hongze Cheng 已提交
64
  // query meta
65
  metaReaderDoInit(&mer1, pVnode->pMeta, 0);
H
more  
Hongze Cheng 已提交
66

H
Hongze Cheng 已提交
67
  if (metaGetTableEntryByName(&mer1, infoReq.tbName) < 0) {
H
Hongze Cheng 已提交
68
    code = terrno;
H
more  
Hongze Cheng 已提交
69
    goto _exit;
H
more  
Hongze Cheng 已提交
70 71
  }

H
Hongze Cheng 已提交
72
  metaRsp.tableType = mer1.me.type;
H
Hongze Cheng 已提交
73
  metaRsp.vgId = TD_VID(pVnode);
H
Hongze Cheng 已提交
74 75 76
  metaRsp.tuid = mer1.me.uid;

  if (mer1.me.type == TSDB_SUPER_TABLE) {
H
Hongze Cheng 已提交
77
    strcpy(metaRsp.stbName, mer1.me.name);
78
    schema = mer1.me.stbEntry.schemaRow;
H
Hongze Cheng 已提交
79 80 81
    schemaTag = mer1.me.stbEntry.schemaTag;
    metaRsp.suid = mer1.me.uid;
  } else if (mer1.me.type == TSDB_CHILD_TABLE) {
82
    metaReaderDoInit(&mer2, pVnode->pMeta, META_READER_NOLOCK);
83
    if (metaReaderGetTableEntryByUid(&mer2, mer1.me.ctbEntry.suid) < 0) goto _exit;
H
Hongze Cheng 已提交
84

H
Hongze Cheng 已提交
85
    strcpy(metaRsp.stbName, mer2.me.name);
H
Hongze Cheng 已提交
86
    metaRsp.suid = mer2.me.uid;
87
    schema = mer2.me.stbEntry.schemaRow;
H
Hongze Cheng 已提交
88 89
    schemaTag = mer2.me.stbEntry.schemaTag;
  } else if (mer1.me.type == TSDB_NORMAL_TABLE) {
90
    schema = mer1.me.ntbEntry.schemaRow;
H
Hongze Cheng 已提交
91 92
  } else {
    ASSERT(0);
H
Hongze Cheng 已提交
93 94
  }

H
Hongze Cheng 已提交
95 96 97
  metaRsp.numOfTags = schemaTag.nCols;
  metaRsp.numOfColumns = schema.nCols;
  metaRsp.precision = pVnode->config.tsdbCfg.precision;
98
  metaRsp.sversion = schema.version;
dengyihao's avatar
dengyihao 已提交
99
  metaRsp.tversion = schemaTag.version;
H
Hongze Cheng 已提交
100 101 102 103 104 105 106
  metaRsp.pSchemas = (SSchema *)taosMemoryMalloc(sizeof(SSchema) * (metaRsp.numOfColumns + metaRsp.numOfTags));

  memcpy(metaRsp.pSchemas, schema.pSchema, sizeof(SSchema) * schema.nCols);
  if (schemaTag.nCols) {
    memcpy(metaRsp.pSchemas + schema.nCols, schemaTag.pSchema, sizeof(SSchema) * schemaTag.nCols);
  }

H
Hongze Cheng 已提交
107
  // encode and send response
D
dapan1121 已提交
108
  rspLen = tSerializeSTableMetaRsp(NULL, 0, &metaRsp);
S
Shengliang Guan 已提交
109 110 111 112 113
  if (rspLen < 0) {
    code = TSDB_CODE_INVALID_MSG;
    goto _exit;
  }

D
dapan1121 已提交
114 115 116 117 118
  if (direct) {
    pRsp = rpcMallocCont(rspLen);
  } else {
    pRsp = taosMemoryCalloc(1, rspLen);
  }
119

S
Shengliang Guan 已提交
120 121 122
  if (pRsp == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
H
Hongze Cheng 已提交
123
  }
S
Shengliang Guan 已提交
124
  tSerializeSTableMetaRsp(pRsp, rspLen, &metaRsp);
H
more  
Hongze Cheng 已提交
125

H
Hongze Cheng 已提交
126
_exit:
S
Shengliang Guan 已提交
127
  rpcMsg.info = pMsg->info;
S
Shengliang Guan 已提交
128 129
  rpcMsg.pCont = pRsp;
  rpcMsg.contLen = rspLen;
D
dapan1121 已提交
130
  rpcMsg.code = code;
dengyihao's avatar
dengyihao 已提交
131
  rpcMsg.msgType = pMsg->msgType;
D
dapan1121 已提交
132

D
dapan1121 已提交
133 134 135 136
  if (code) {
    qError("get table %s meta failed cause of %s", infoReq.tbName, tstrerror(code));
  }

D
dapan1121 已提交
137 138
  if (direct) {
    tmsgSendRsp(&rpcMsg);
139 140
  } else {
    *pMsg = rpcMsg;
D
dapan1121 已提交
141
  }
142

H
Hongze Cheng 已提交
143
  taosMemoryFree(metaRsp.pSchemas);
H
Hongze Cheng 已提交
144 145
  metaReaderClear(&mer2);
  metaReaderClear(&mer1);
D
dapan 已提交
146
  return TSDB_CODE_SUCCESS;
H
Haojun Liao 已提交
147
}
H
Hongze Cheng 已提交
148

D
dapan1121 已提交
149
int vnodeGetTableCfg(SVnode *pVnode, SRpcMsg *pMsg, bool direct) {
D
dapan1121 已提交
150 151 152 153 154
  STableCfgReq   cfgReq = {0};
  STableCfgRsp   cfgRsp = {0};
  SMetaReader    mer1 = {0};
  SMetaReader    mer2 = {0};
  char           tableFName[TSDB_TABLE_FNAME_LEN];
dengyihao's avatar
dengyihao 已提交
155
  SRpcMsg        rpcMsg = {0};
D
dapan1121 已提交
156 157
  int32_t        code = 0;
  int32_t        rspLen = 0;
158
  void          *pRsp = NULL;
D
dapan1121 已提交
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
  SSchemaWrapper schema = {0};
  SSchemaWrapper schemaTag = {0};

  // decode req
  if (tDeserializeSTableCfgReq(pMsg->pCont, pMsg->contLen, &cfgReq) != 0) {
    code = TSDB_CODE_INVALID_MSG;
    goto _exit;
  }

  strcpy(cfgRsp.tbName, cfgReq.tbName);
  memcpy(cfgRsp.dbFName, cfgReq.dbFName, sizeof(cfgRsp.dbFName));

  sprintf(tableFName, "%s.%s", cfgReq.dbFName, cfgReq.tbName);
  code = vnodeValidateTableHash(pVnode, tableFName);
  if (code) {
    goto _exit;
  }

  // query meta
178
  metaReaderDoInit(&mer1, pVnode->pMeta, 0);
D
dapan1121 已提交
179 180 181 182 183 184 185 186 187

  if (metaGetTableEntryByName(&mer1, cfgReq.tbName) < 0) {
    code = terrno;
    goto _exit;
  }

  cfgRsp.tableType = mer1.me.type;

  if (mer1.me.type == TSDB_SUPER_TABLE) {
D
dapan1121 已提交
188 189
    code = TSDB_CODE_VND_HASH_MISMATCH;
    goto _exit;
D
dapan1121 已提交
190
  } else if (mer1.me.type == TSDB_CHILD_TABLE) {
191
    metaReaderDoInit(&mer2, pVnode->pMeta, 0);
192
    if (metaReaderGetTableEntryByUid(&mer2, mer1.me.ctbEntry.suid) < 0) goto _exit;
D
dapan1121 已提交
193 194 195 196

    strcpy(cfgRsp.stbName, mer2.me.name);
    schema = mer2.me.stbEntry.schemaRow;
    schemaTag = mer2.me.stbEntry.schemaTag;
D
dapan1121 已提交
197 198 199
    cfgRsp.ttl = mer1.me.ctbEntry.ttlDays;
    cfgRsp.commentLen = mer1.me.ctbEntry.commentLen;
    if (mer1.me.ctbEntry.commentLen > 0) {
200
      cfgRsp.pComment = taosStrdup(mer1.me.ctbEntry.comment);
D
dapan1121 已提交
201 202 203 204 205
    }
    STag *pTag = (STag *)mer1.me.ctbEntry.pTags;
    cfgRsp.tagsLen = pTag->len;
    cfgRsp.pTags = taosMemoryMalloc(cfgRsp.tagsLen);
    memcpy(cfgRsp.pTags, pTag, cfgRsp.tagsLen);
D
dapan1121 已提交
206 207
  } else if (mer1.me.type == TSDB_NORMAL_TABLE) {
    schema = mer1.me.ntbEntry.schemaRow;
D
dapan1121 已提交
208 209 210
    cfgRsp.ttl = mer1.me.ntbEntry.ttlDays;
    cfgRsp.commentLen = mer1.me.ntbEntry.commentLen;
    if (mer1.me.ntbEntry.commentLen > 0) {
211
      cfgRsp.pComment = taosStrdup(mer1.me.ntbEntry.comment);
D
dapan1121 已提交
212
    }
D
dapan1121 已提交
213 214 215 216 217 218
  } else {
    ASSERT(0);
  }

  cfgRsp.numOfTags = schemaTag.nCols;
  cfgRsp.numOfColumns = schema.nCols;
219
  cfgRsp.pSchemas = (SSchema *)taosMemoryCalloc(cfgRsp.numOfColumns + cfgRsp.numOfTags, sizeof(SSchema));
D
dapan1121 已提交
220 221 222 223 224 225 226 227 228 229 230 231 232

  memcpy(cfgRsp.pSchemas, schema.pSchema, sizeof(SSchema) * schema.nCols);
  if (schemaTag.nCols) {
    memcpy(cfgRsp.pSchemas + schema.nCols, schemaTag.pSchema, sizeof(SSchema) * schemaTag.nCols);
  }

  // encode and send response
  rspLen = tSerializeSTableCfgRsp(NULL, 0, &cfgRsp);
  if (rspLen < 0) {
    code = TSDB_CODE_INVALID_MSG;
    goto _exit;
  }

D
dapan1121 已提交
233 234 235 236 237 238
  if (direct) {
    pRsp = rpcMallocCont(rspLen);
  } else {
    pRsp = taosMemoryCalloc(1, rspLen);
  }

D
dapan1121 已提交
239 240 241 242 243 244 245 246 247 248 249
  if (pRsp == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
  }
  tSerializeSTableCfgRsp(pRsp, rspLen, &cfgRsp);

_exit:
  rpcMsg.info = pMsg->info;
  rpcMsg.pCont = pRsp;
  rpcMsg.contLen = rspLen;
  rpcMsg.code = code;
dengyihao's avatar
dengyihao 已提交
250
  rpcMsg.msgType = pMsg->msgType;
D
dapan1121 已提交
251 252 253 254 255

  if (code) {
    qError("get table %s cfg failed cause of %s", cfgReq.tbName, tstrerror(code));
  }

D
dapan1121 已提交
256 257
  if (direct) {
    tmsgSendRsp(&rpcMsg);
258 259
  } else {
    *pMsg = rpcMsg;
D
dapan1121 已提交
260
  }
261

D
dapan1121 已提交
262
  tFreeSTableCfgRsp(&cfgRsp);
D
dapan1121 已提交
263 264
  metaReaderClear(&mer2);
  metaReaderClear(&mer1);
D
dapan 已提交
265
  return TSDB_CODE_SUCCESS;
H
Haojun Liao 已提交
266
}
H
Hongze Cheng 已提交
267

dengyihao's avatar
dengyihao 已提交
268
static FORCE_INLINE void vnodeFreeSBatchRspMsg(void *p) {
D
dapan1121 已提交
269 270 271 272
  if (NULL == p) {
    return;
  }

dengyihao's avatar
dengyihao 已提交
273
  SBatchRspMsg *pRsp = (SBatchRspMsg *)p;
D
dapan1121 已提交
274 275 276
  rpcFreeCont(pRsp->msg);
}

D
dapan1121 已提交
277
int32_t vnodeGetBatchMeta(SVnode *pVnode, SRpcMsg *pMsg) {
dengyihao's avatar
dengyihao 已提交
278 279 280 281
  int32_t      code = 0;
  int32_t      rspSize = 0;
  SBatchReq    batchReq = {0};
  SBatchMsg   *req = NULL;
D
dapan1121 已提交
282
  SBatchRspMsg rsp = {0};
dengyihao's avatar
dengyihao 已提交
283 284 285 286
  SBatchRsp    batchRsp = {0};
  SRpcMsg      reqMsg = *pMsg;
  SRpcMsg      rspMsg = {0};
  void        *pRsp = NULL;
D
dapan1121 已提交
287

D
dapan1121 已提交
288 289 290 291 292 293
  if (tDeserializeSBatchReq(pMsg->pCont, pMsg->contLen, &batchReq)) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    qError("tDeserializeSBatchReq failed");
    goto _exit;
  }

dengyihao's avatar
dengyihao 已提交
294
  int32_t msgNum = taosArrayGetSize(batchReq.pMsgs);
D
dapan1121 已提交
295 296 297 298 299 300
  if (msgNum >= MAX_META_MSG_IN_BATCH) {
    code = TSDB_CODE_INVALID_MSG;
    qError("too many msgs %d in vnode batch meta req", msgNum);
    goto _exit;
  }

D
dapan1121 已提交
301 302
  batchRsp.pRsps = taosArrayInit(msgNum, sizeof(SBatchRspMsg));
  if (NULL == batchRsp.pRsps) {
D
dapan1121 已提交
303
    code = TSDB_CODE_OUT_OF_MEMORY;
D
dapan1121 已提交
304
    qError("taosArrayInit %d SBatchRspMsg failed", msgNum);
D
dapan1121 已提交
305 306
    goto _exit;
  }
307

D
dapan1121 已提交
308
  for (int32_t i = 0; i < msgNum; ++i) {
D
dapan1121 已提交
309 310 311 312 313 314 315
    req = taosArrayGet(batchReq.pMsgs, i);

    reqMsg.msgType = req->msgType;
    reqMsg.pCont = req->msg;
    reqMsg.contLen = req->msgLen;

    switch (req->msgType) {
D
dapan1121 已提交
316 317 318 319 320 321 322
      case TDMT_VND_TABLE_META:
        vnodeGetTableMeta(pVnode, &reqMsg, false);
        break;
      case TDMT_VND_TABLE_CFG:
        vnodeGetTableCfg(pVnode, &reqMsg, false);
        break;
      default:
D
dapan1121 已提交
323
        qError("invalid req msgType %d", req->msgType);
D
dapan1121 已提交
324 325 326 327 328 329
        reqMsg.code = TSDB_CODE_INVALID_MSG;
        reqMsg.pCont = NULL;
        reqMsg.contLen = 0;
        break;
    }

D
dapan1121 已提交
330
    rsp.msgIdx = req->msgIdx;
D
dapan1121 已提交
331 332 333 334
    rsp.reqType = reqMsg.msgType;
    rsp.msgLen = reqMsg.contLen;
    rsp.rspCode = reqMsg.code;
    rsp.msg = reqMsg.pCont;
335

D
dapan1121 已提交
336
    taosArrayPush(batchRsp.pRsps, &rsp);
D
dapan1121 已提交
337 338
  }

D
dapan1121 已提交
339 340
  rspSize = tSerializeSBatchRsp(NULL, 0, &batchRsp);
  if (rspSize < 0) {
D
dapan1121 已提交
341
    qError("tSerializeSBatchRsp failed");
D
dapan1121 已提交
342
    code = TSDB_CODE_OUT_OF_MEMORY;
D
dapan1121 已提交
343 344
    goto _exit;
  }
D
dapan1121 已提交
345 346
  pRsp = rpcMallocCont(rspSize);
  if (pRsp == NULL) {
D
dapan1121 已提交
347
    qError("rpcMallocCont %d failed", rspSize);
D
dapan1121 已提交
348 349 350
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
  }
D
dapan1121 已提交
351 352
  if (tSerializeSBatchRsp(pRsp, rspSize, &batchRsp) < 0) {
    qError("tSerializeSBatchRsp %d failed", rspSize);
D
dapan1121 已提交
353 354
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
D
dapan1121 已提交
355 356 357 358 359 360 361 362 363 364 365
  }

_exit:

  rspMsg.info = pMsg->info;
  rspMsg.pCont = pRsp;
  rspMsg.contLen = rspSize;
  rspMsg.code = code;
  rspMsg.msgType = pMsg->msgType;

  if (code) {
366
    qError("vnd get batch meta failed cause of %s", tstrerror(code));
D
dapan1121 已提交
367 368
  }

D
dapan1121 已提交
369
  taosArrayDestroyEx(batchReq.pMsgs, tFreeSBatchReqMsg);
D
dapan1121 已提交
370
  taosArrayDestroyEx(batchRsp.pRsps, tFreeSBatchRspMsg);
D
dapan1121 已提交
371 372 373

  tmsgSendRsp(&rspMsg);

374
  return code;
D
dapan1121 已提交
375 376
}

C
Cary Xu 已提交
377
int32_t vnodeGetLoad(SVnode *pVnode, SVnodeLoad *pLoad) {
378 379
  SSyncState state = syncGetState(pVnode->sync);

C
Cary Xu 已提交
380
  pLoad->vgId = TD_VID(pVnode);
381 382
  pLoad->syncState = state.state;
  pLoad->syncRestore = state.restored;
383 384 385
  pLoad->syncTerm = state.term;
  pLoad->roleTimeMs = state.roleTimeMs;
  pLoad->startTimeMs = state.startTimeMs;
386
  pLoad->syncCanRead = state.canRead;
C
Cary Xu 已提交
387
  pLoad->cacheUsage = tsdbCacheGetUsage(pVnode);
D
dapan1121 已提交
388
  pLoad->numOfCachedTables = tsdbCacheGetElems(pVnode);
C
Cary Xu 已提交
389 390 391 392 393 394 395 396 397 398 399 400 401
  pLoad->numOfTables = metaGetTbNum(pVnode->pMeta);
  pLoad->numOfTimeSeries = metaGetTimeSeriesNum(pVnode->pMeta);
  pLoad->totalStorage = (int64_t)3 * 1073741824;
  pLoad->compStorage = (int64_t)2 * 1073741824;
  pLoad->pointsWritten = 100;
  pLoad->numOfSelectReqs = 1;
  pLoad->numOfInsertReqs = atomic_load_64(&pVnode->statis.nInsert);
  pLoad->numOfInsertSuccessReqs = atomic_load_64(&pVnode->statis.nInsertSuccess);
  pLoad->numOfBatchInsertReqs = atomic_load_64(&pVnode->statis.nBatchInsert);
  pLoad->numOfBatchInsertSuccessReqs = atomic_load_64(&pVnode->statis.nBatchInsertSuccess);
  return 0;
}

C
Cary Xu 已提交
402 403 404 405 406 407 408
/**
 * @brief Reset the statistics value by monitor interval
 *
 * @param pVnode
 * @param pLoad
 */
void vnodeResetLoad(SVnode *pVnode, SVnodeLoad *pLoad) {
409 410 411
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nInsert, pLoad->numOfInsertReqs, 64, "nInsert");
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nInsertSuccess, pLoad->numOfInsertSuccessReqs, 64, "nInsertSuccess");
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nBatchInsert, pLoad->numOfBatchInsertReqs, 64, "nBatchInsert");
dengyihao's avatar
dengyihao 已提交
412 413
  VNODE_GET_LOAD_RESET_VALS(pVnode->statis.nBatchInsertSuccess, pLoad->numOfBatchInsertSuccessReqs, 64,
                            "nBatchInsertSuccess");
C
Cary Xu 已提交
414 415
}

K
kailixu 已提交
416 417 418
void vnodeGetInfo(void *pVnode, const char **dbname, int32_t *vgId, int64_t* numOfTables, int64_t* numOfNormalTables) {
  SVnode* pVnodeObj = pVnode;
  SVnodeCfg* pConf = &pVnodeObj->config;
419

420
  if (dbname) {
421
    *dbname = pConf->dbname;
422 423 424
  }

  if (vgId) {
425 426 427 428 429 430 431 432 433
    *vgId = TD_VID(pVnodeObj);
  }

  if (numOfTables) {
    *numOfTables = pConf->vndStats.numOfNTables + pConf->vndStats.numOfCTables;
  }

  if (numOfNormalTables) {
    *numOfNormalTables = pConf->vndStats.numOfNTables;
434
  }
H
Hongze Cheng 已提交
435 436
}

K
kailixu 已提交
437
int32_t vnodeGetTableList(void* pVnode, int8_t type, SArray* pList) {
H
Haojun Liao 已提交
438 439 440 441 442 443 444
  if (type == TSDB_SUPER_TABLE) {
    return vnodeGetStbIdList(pVnode, 0, pList);
  } else {
    return TSDB_CODE_INVALID_PARA;
  }
}

H
Hongze Cheng 已提交
445
int32_t vnodeGetAllTableList(SVnode *pVnode, uint64_t uid, SArray *list) {
S
slzhou 已提交
446
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, uid, 1);
H
Hongze Cheng 已提交
447 448 449 450 451 452 453

  while (1) {
    tb_uid_t id = metaCtbCursorNext(pCur);
    if (id == 0) {
      break;
    }

454
    STableKeyInfo info = {uid = id};
H
Hongze Cheng 已提交
455 456 457
    taosArrayPush(list, &info);
  }

458
  metaCloseCtbCursor(pCur, 1);
H
Hongze Cheng 已提交
459 460 461
  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
462 463 464
int32_t vnodeGetCtbIdListByFilter(SVnode *pVnode, int64_t suid, SArray *list, bool (*filter)(void *arg), void *arg) {
  return 0;
}
465 466 467

int32_t vnodeGetCtbIdList(void *pVnode, int64_t suid, SArray *list) {
  SVnode      *pVnodeObj = pVnode;
S
slzhou 已提交
468
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnodeObj, suid, 1);
H
Hongze Cheng 已提交
469 470 471 472 473 474 475 476 477 478

  while (1) {
    tb_uid_t id = metaCtbCursorNext(pCur);
    if (id == 0) {
      break;
    }

    taosArrayPush(list, &id);
  }

479
  metaCloseCtbCursor(pCur, 1);
H
Hongze Cheng 已提交
480 481 482
  return TSDB_CODE_SUCCESS;
}

483 484
int32_t vnodeGetStbIdList(SVnode *pVnode, int64_t suid, SArray *list) {
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, suid);
C
Cary Xu 已提交
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
  if (!pCur) {
    return TSDB_CODE_FAILED;
  }

  while (1) {
    tb_uid_t id = metaStbCursorNext(pCur);
    if (id == 0) {
      break;
    }

    taosArrayPush(list, &id);
  }

  metaCloseStbCursor(pCur);
  return TSDB_CODE_SUCCESS;
}

K
kailixu 已提交
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
int32_t vnodeGetStbIdListByFilter(SVnode *pVnode, int64_t suid, SArray *list, bool (*filter)(void *arg, void *arg1),
                                  void *arg) {
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, suid);
  if (!pCur) {
    return TSDB_CODE_FAILED;
  }

  while (1) {
    tb_uid_t id = metaStbCursorNext(pCur);
    if (id == 0) {
      break;
    }

    if ((*filter) && (*filter)(arg, &id)) {
      continue;
    }

    taosArrayPush(list, &id);
  }

  metaCloseStbCursor(pCur);
  return TSDB_CODE_SUCCESS;
}

526
int32_t vnodeGetCtbNum(SVnode *pVnode, int64_t suid, int64_t *num) {
S
slzhou 已提交
527
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, suid, 0);
528 529 530 531 532 533 534 535 536 537 538 539 540 541
  if (!pCur) {
    return TSDB_CODE_FAILED;
  }

  *num = 0;
  while (1) {
    tb_uid_t id = metaCtbCursorNext(pCur);
    if (id == 0) {
      break;
    }

    ++(*num);
  }

542
  metaCloseCtbCursor(pCur, 0);
543 544 545 546
  return TSDB_CODE_SUCCESS;
}

static int32_t vnodeGetStbColumnNum(SVnode *pVnode, tb_uid_t suid, int *num) {
547
  STSchema *pTSchema = metaGetTbTSchema(pVnode->pMeta, suid, -1, 1);
548 549
  // metaGetTbTSchemaEx(pVnode->pMeta, suid, suid, -1, &pTSchema);

550 551
  if (pTSchema) {
    *num = pTSchema->numOfCols;
552

553 554 555 556
    taosMemoryFree(pTSchema);
  } else {
    *num = 2;
  }
557 558 559 560

  return TSDB_CODE_SUCCESS;
}

K
kailixu 已提交
561
#ifdef TD_ENTERPRISE
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
#define TK_LOG_STB_NUM 19
static const char *tkLogStb[TK_LOG_STB_NUM] = {"cluster_info",
                                               "data_dir",
                                               "dnodes_info",
                                               "d_info",
                                               "grants_info",
                                               "keeper_monitor",
                                               "logs",
                                               "log_dir",
                                               "log_summary",
                                               "m_info",
                                               "taosadapter_restful_http_request_fail",
                                               "taosadapter_restful_http_request_in_flight",
                                               "taosadapter_restful_http_request_summary_milliseconds",
                                               "taosadapter_restful_http_request_total",
                                               "taosadapter_system_cpu_percent",
                                               "taosadapter_system_mem_percent",
                                               "temp_dir",
                                               "vgroups_info",
                                               "vnodes_role"};

// exclude stbs of taoskeeper log
584
static int32_t vnodeGetTimeSeriesBlackList(SVnode *pVnode) {
585
  char *dbName = strchr(pVnode->config.dbname, '.');
K
kailixu 已提交
586
  if (!dbName || 0 != strncmp(++dbName, "log", TSDB_DB_NAME_LEN)) {
K
kailixu 已提交
587
    return 0;
588 589 590 591 592 593 594 595 596
  }
  int32_t tbSize = metaSizeOfTbFilterCache(pVnode, 0);
  if (tbSize < TK_LOG_STB_NUM) {
    for (int32_t i = 0; i < TK_LOG_STB_NUM; ++i) {
      tb_uid_t suid = metaGetTableEntryUidByName(pVnode->pMeta, tkLogStb[i]);
      if (suid != 0) {
        metaPutTbToFilterCache(pVnode, suid, 0);
      }
    }
597
    tbSize = metaSizeOfTbFilterCache(pVnode, 0);
598 599
  }

600
  return tbSize;
601
}
602
#endif
603

K
kailixu 已提交
604
static bool vnodeTimeSeriesFilter(void *arg1, void *arg2) {
K
kailixu 已提交
605 606 607 608 609 610 611 612
  SVnode *pVnode = (SVnode *)arg1;

  if (metaTbInFilterCache(pVnode, *(tb_uid_t *)(arg2), 0)) {
    return true;
  }
  return false;
}

613
int32_t vnodeGetTimeSeriesNum(SVnode *pVnode, int64_t *num) {
614 615 616
  SArray *suidList = NULL;

  if (!(suidList = taosArrayInit(1, sizeof(tb_uid_t)))) {
K
kailixu 已提交
617 618
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return TSDB_CODE_FAILED;
619 620
  }

621 622 623 624
  int32_t tbFilterSize = 0;
  #ifdef TD_ENTERPRISE
  tbFilterSize = vnodeGetTimeSeriesBlackList(pVnode);
  #endif
625

626
  if ((!tbFilterSize && vnodeGetStbIdList(pVnode, 0, suidList) < 0) ||
K
kailixu 已提交
627
      (tbFilterSize && vnodeGetStbIdListByFilter(pVnode, 0, suidList, vnodeTimeSeriesFilter, pVnode) < 0)) {
K
kailixu 已提交
628 629 630 631
    qError("vgId:%d, failed to get stb id list error: %s", TD_VID(pVnode), terrstr());
    taosArrayDestroy(suidList);
    return TSDB_CODE_FAILED;
  }
632

633
  *num = 0;
634 635
  int64_t arrSize = taosArrayGetSize(suidList);
  for (int64_t i = 0; i < arrSize; ++i) {
K
kailixu 已提交
636
    tb_uid_t suid = *(tb_uid_t *)taosArrayGet(suidList, i);
637

K
kailixu 已提交
638 639
    int64_t ctbNum = 0;
    metaGetStbStats(pVnode, suid, &ctbNum);
640

K
kailixu 已提交
641 642
    int numOfCols = 0;
    vnodeGetStbColumnNum(pVnode, suid, &numOfCols);
643

K
kailixu 已提交
644
    *num += ctbNum * (numOfCols - 1);
645 646
  }

647
  taosArrayDestroy(suidList);
648 649 650 651 652 653
  return TSDB_CODE_SUCCESS;
}

int32_t vnodeGetAllCtbNum(SVnode *pVnode, int64_t *num) {
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, 0);
  if (!pCur) {
K
kailixu 已提交
654
    return TSDB_CODE_FAILED;
655 656 657 658
  }

  *num = 0;
  while (1) {
K
kailixu 已提交
659 660 661 662
    tb_uid_t id = metaStbCursorNext(pCur);
    if (id == 0) {
      break;
    }
663

K
kailixu 已提交
664 665
    int64_t ctbNum = 0;
    vnodeGetCtbNum(pVnode, id, &ctbNum);
666

K
kailixu 已提交
667
    *num += ctbNum;
668 669 670 671 672 673
  }

  metaCloseStbCursor(pCur);
  return TSDB_CODE_SUCCESS;
}

674
void *vnodeGetIdx(void *pVnode) {
H
Hongze Cheng 已提交
675
  if (pVnode == NULL) {
K
kailixu 已提交
676
    return NULL;
H
Hongze Cheng 已提交
677
  }
678

K
kailixu 已提交
679
  return metaGetIdx(((SVnode*)pVnode)->pMeta);
H
Hongze Cheng 已提交
680 681
}

682
void *vnodeGetIvtIdx(void *pVnode) {
H
Hongze Cheng 已提交
683
  if (pVnode == NULL) {
K
kailixu 已提交
684
    return NULL;
H
Hongze Cheng 已提交
685
  }
K
kailixu 已提交
686
  return metaGetIvtIdx(((SVnode*)pVnode)->pMeta);
H
Hongze Cheng 已提交
687
}