mndShow.c 15.0 KB
Newer Older
H
refact  
Hongze Cheng 已提交
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/>.
 */

S
Shengliang Guan 已提交
16
#define _DEFAULT_SOURCE
S
Shengliang Guan 已提交
17
#include "mndShow.h"
S
Shengliang Guan 已提交
18

S
Shengliang Guan 已提交
19 20
#define SHOW_STEP_SIZE 100

S
Shengliang Guan 已提交
21
static SShowObj *mndCreateShowObj(SMnode *pMnode, SShowReq *pReq);
S
Shengliang Guan 已提交
22
static void      mndFreeShowObj(SShowObj *pShow);
23
static SShowObj *mndAcquireShowObj(SMnode *pMnode, int64_t showId);
S
Shengliang Guan 已提交
24
static void      mndReleaseShowObj(SShowObj *pShow, bool forceRemove);
S
Shengliang Guan 已提交
25 26
static int32_t   mndProcessShowReq(SMnodeMsg *pReq);
static int32_t   mndProcessRetrieveReq(SMnodeMsg *pReq);
S
Shengliang Guan 已提交
27
static bool      mndCheckRetrieveFinished(SShowObj *pShow);
28
static int32_t   mndProcessRetrieveSysTableReq(SMnodeMsg *pReq);
S
Shengliang Guan 已提交
29 30 31 32

int32_t mndInitShow(SMnode *pMnode) {
  SShowMgmt *pMgmt = &pMnode->showMgmt;

S
Shengliang Guan 已提交
33
  pMgmt->cache = taosCacheInit(TSDB_DATA_TYPE_INT, 5, true, (__cache_free_fn_t)mndFreeShowObj, "show");
S
Shengliang Guan 已提交
34 35 36 37 38 39
  if (pMgmt->cache == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    mError("failed to alloc show cache since %s", terrstr());
    return -1;
  }

S
Shengliang Guan 已提交
40 41
  mndSetMsgHandle(pMnode, TDMT_MND_SHOW, mndProcessShowReq);
  mndSetMsgHandle(pMnode, TDMT_MND_SHOW_RETRIEVE, mndProcessRetrieveReq);
42
  mndSetMsgHandle(pMnode, TDMT_MND_SYSTABLE_RETRIEVE, mndProcessRetrieveSysTableReq);
S
Shengliang Guan 已提交
43 44 45
  return 0;
}

S
Shengliang Guan 已提交
46 47 48 49 50 51 52 53
void mndCleanupShow(SMnode *pMnode) {
  SShowMgmt *pMgmt = &pMnode->showMgmt;
  if (pMgmt->cache != NULL) {
    taosCacheCleanup(pMgmt->cache);
    pMgmt->cache = NULL;
  }
}

S
Shengliang Guan 已提交
54
static SShowObj *mndCreateShowObj(SMnode *pMnode, SShowReq *pReq) {
S
Shengliang Guan 已提交
55 56
  SShowMgmt *pMgmt = &pMnode->showMgmt;

57 58
  int64_t showId = atomic_add_fetch_64(&pMgmt->showId, 1);
  if (showId == 0) atomic_add_fetch_64(&pMgmt->showId, 1);
S
Shengliang Guan 已提交
59

S
Shengliang Guan 已提交
60
  int32_t  size = sizeof(SShowObj) + pReq->payloadLen;
S
Shengliang Guan 已提交
61 62 63
  SShowObj showObj = {0};
  showObj.id = showId;
  showObj.pMnode = pMnode;
S
Shengliang Guan 已提交
64 65 66 67
  showObj.type = pReq->type;
  showObj.payloadLen = pReq->payloadLen;
  memcpy(showObj.db, pReq->db, TSDB_DB_FNAME_LEN);
  memcpy(showObj.payload, pReq->payload, pReq->payloadLen);
S
Shengliang Guan 已提交
68

S
Shengliang Guan 已提交
69
  int32_t   keepTime = tsShellActivityTimer * 6 * 1000;
70
  SShowObj *pShow = taosCachePut(pMgmt->cache, &showId, sizeof(int64_t), &showObj, size, keepTime);
S
Shengliang Guan 已提交
71
  if (pShow == NULL) {
S
Shengliang Guan 已提交
72
    terrno = TSDB_CODE_OUT_OF_MEMORY;
S
Shengliang Guan 已提交
73
    mError("show:0x%" PRIx64 ", failed to put into cache since %s", showId, terrstr());
S
Shengliang Guan 已提交
74 75
    return NULL;
  }
S
Shengliang Guan 已提交
76

S
Shengliang Guan 已提交
77
  mTrace("show:0x%" PRIx64 ", is created, data:%p", showId, pShow);
S
Shengliang Guan 已提交
78
  return pShow;
S
Shengliang Guan 已提交
79 80
}

S
Shengliang Guan 已提交
81
static void mndFreeShowObj(SShowObj *pShow) {
S
Shengliang Guan 已提交
82 83 84 85 86 87 88 89 90 91
  SMnode    *pMnode = pShow->pMnode;
  SShowMgmt *pMgmt = &pMnode->showMgmt;

  ShowFreeIterFp freeFp = pMgmt->freeIterFps[pShow->type];
  if (freeFp != NULL) {
    if (pShow->pIter != NULL) {
      (*freeFp)(pMnode, pShow->pIter);
    }
  }

S
Shengliang Guan 已提交
92
  mTrace("show:0x%" PRIx64 ", is destroyed, data:%p", pShow->id, pShow);
S
Shengliang Guan 已提交
93 94
}

95
static SShowObj *mndAcquireShowObj(SMnode *pMnode, int64_t showId) {
S
Shengliang Guan 已提交
96 97
  SShowMgmt *pMgmt = &pMnode->showMgmt;

98
  SShowObj *pShow = taosCacheAcquireByKey(pMgmt->cache, &showId, sizeof(showId));
S
Shengliang Guan 已提交
99
  if (pShow == NULL) {
S
Shengliang Guan 已提交
100
    mError("show:0x%" PRIx64 ", already destroyed", showId);
S
Shengliang Guan 已提交
101 102 103
    return NULL;
  }

S
Shengliang Guan 已提交
104
  mTrace("show:0x%" PRIx64 ", acquired from cache, data:%p", pShow->id, pShow);
S
Shengliang Guan 已提交
105 106 107 108 109
  return pShow;
}

static void mndReleaseShowObj(SShowObj *pShow, bool forceRemove) {
  if (pShow == NULL) return;
S
Shengliang Guan 已提交
110
  mTrace("show:0x%" PRIx64 ", released from cache, data:%p force:%d", pShow->id, pShow, forceRemove);
S
Shengliang Guan 已提交
111

S
Shengliang Guan 已提交
112 113
  // A bug in tcache.c
  forceRemove = 0;
S
Shengliang Guan 已提交
114 115 116 117

  SMnode    *pMnode = pShow->pMnode;
  SShowMgmt *pMgmt = &pMnode->showMgmt;
  taosCacheRelease(pMgmt->cache, (void **)(&pShow), forceRemove);
S
Shengliang Guan 已提交
118
}
S
Shengliang Guan 已提交
119

S
Shengliang Guan 已提交
120 121
static int32_t mndProcessShowReq(SMnodeMsg *pReq) {
  SMnode    *pMnode = pReq->pMnode;
S
Shengliang Guan 已提交
122
  SShowMgmt *pMgmt = &pMnode->showMgmt;
S
Shengliang Guan 已提交
123 124
  int32_t    code = -1;
  SShowReq   showReq = {0};
S
Shengliang Guan 已提交
125
  SShowRsp   showRsp = {0};
S
Shengliang Guan 已提交
126

S
Shengliang Guan 已提交
127 128 129 130 131 132
  if (tDeserializeSShowReq(pReq->rpcMsg.pCont, pReq->rpcMsg.contLen, &showReq) != 0) {
    terrno = TSDB_CODE_INVALID_MSG;
    goto SHOW_OVER;
  }

  if (showReq.type <= TSDB_MGMT_TABLE_START || showReq.type >= TSDB_MGMT_TABLE_MAX) {
S
Shengliang Guan 已提交
133
    terrno = TSDB_CODE_MND_INVALID_MSG_TYPE;
S
Shengliang Guan 已提交
134
    goto SHOW_OVER;
S
Shengliang Guan 已提交
135 136
  }

S
Shengliang Guan 已提交
137
  ShowMetaFp metaFp = pMgmt->metaFps[showReq.type];
S
Shengliang Guan 已提交
138 139
  if (metaFp == NULL) {
    terrno = TSDB_CODE_MND_INVALID_MSG_TYPE;
S
Shengliang Guan 已提交
140
    goto SHOW_OVER;
S
Shengliang Guan 已提交
141 142
  }

S
Shengliang Guan 已提交
143
  SShowObj *pShow = mndCreateShowObj(pMnode, &showReq);
S
Shengliang Guan 已提交
144
  if (pShow == NULL) {
S
Shengliang Guan 已提交
145
    goto SHOW_OVER;
S
Shengliang Guan 已提交
146 147
  }

S
Shengliang Guan 已提交
148 149 150
  showRsp.showId = pShow->id;
  showRsp.tableMeta.pSchemas = calloc(TSDB_MAX_COLUMNS, sizeof(SSchema));
  if (showRsp.tableMeta.pSchemas == NULL) {
S
Shengliang Guan 已提交
151 152
    mndReleaseShowObj(pShow, true);
    terrno = TSDB_CODE_OUT_OF_MEMORY;
S
Shengliang Guan 已提交
153
    goto SHOW_OVER;
S
Shengliang Guan 已提交
154 155
  }

S
Shengliang Guan 已提交
156
  code = (*metaFp)(pReq, pShow, &showRsp.tableMeta);
S
Shengliang Guan 已提交
157 158
  mDebug("show:0x%" PRIx64 ", get meta finished, numOfRows:%d cols:%d showReq.type:%s, result:%s", pShow->id,
         pShow->numOfRows, pShow->numOfColumns, mndShowStr(showReq.type), tstrerror(code));
S
Shengliang Guan 已提交
159

S
Shengliang Guan 已提交
160 161 162 163 164 165
  if (code == 0) {
    int32_t bufLen = tSerializeSShowRsp(NULL, 0, &showRsp);
    void   *pBuf = rpcMallocCont(bufLen);
    tSerializeSShowRsp(pBuf, bufLen, &showRsp);
    pReq->contLen = bufLen;
    pReq->pCont = pBuf;
S
Shengliang Guan 已提交
166 167 168 169
    mndReleaseShowObj(pShow, false);
  } else {
    mndReleaseShowObj(pShow, true);
  }
S
Shengliang Guan 已提交
170 171 172 173 174 175 176

SHOW_OVER:
  if (code != 0) {
    mError("failed to process show-meta req since %s", terrstr());
  }

  tFreeSShowReq(&showReq);
S
Shengliang Guan 已提交
177
  tFreeSShowRsp(&showRsp);
S
Shengliang Guan 已提交
178
  return code;
S
Shengliang Guan 已提交
179 180
}

S
Shengliang Guan 已提交
181 182
static int32_t mndProcessRetrieveReq(SMnodeMsg *pReq) {
  SMnode    *pMnode = pReq->pMnode;
S
Shengliang Guan 已提交
183 184 185 186 187
  SShowMgmt *pMgmt = &pMnode->showMgmt;
  int32_t    rowsToRead = 0;
  int32_t    size = 0;
  int32_t    rowsRead = 0;

S
Shengliang Guan 已提交
188 189 190 191 192
  SRetrieveTableReq retrieveReq = {0};
  if (tDeserializeSRetrieveTableReq(pReq->rpcMsg.pCont, pReq->rpcMsg.contLen, &retrieveReq) != 0) {
    terrno = TSDB_CODE_INVALID_MSG;
    return -1;
  }
S
Shengliang Guan 已提交
193

S
Shengliang Guan 已提交
194
  SShowObj *pShow = mndAcquireShowObj(pMnode, retrieveReq.showId);
S
Shengliang Guan 已提交
195
  if (pShow == NULL) {
S
Shengliang Guan 已提交
196
    terrno = TSDB_CODE_MND_INVALID_SHOWOBJ;
S
Shengliang Guan 已提交
197
    mError("failed to process show-retrieve req:%p since %s", pShow, terrstr());
S
Shengliang Guan 已提交
198 199 200 201 202 203 204
    return -1;
  }

  ShowRetrieveFp retrieveFp = pMgmt->retrieveFps[pShow->type];
  if (retrieveFp == NULL) {
    mndReleaseShowObj(pShow, false);
    terrno = TSDB_CODE_MSG_NOT_PROCESSED;
S
Shengliang Guan 已提交
205
    mError("show:0x%" PRIx64 ", failed to retrieve data since %s", pShow->id, terrstr());
S
Shengliang Guan 已提交
206 207 208
    return -1;
  }

S
Shengliang Guan 已提交
209
  mDebug("show:0x%" PRIx64 ", start retrieve data, numOfReads:%d numOfRows:%d type:%s", pShow->id, pShow->numOfReads,
S
Shengliang Guan 已提交
210
         pShow->numOfRows, mndShowStr(pShow->type));
S
Shengliang Guan 已提交
211 212

  if (mndCheckRetrieveFinished(pShow)) {
S
Shengliang Guan 已提交
213 214
    mDebug("show:0x%" PRIx64 ", read finished, numOfReads:%d numOfRows:%d", pShow->id, pShow->numOfReads,
           pShow->numOfRows);
S
Shengliang Guan 已提交
215 216 217
    pShow->numOfReads = pShow->numOfRows;
  }

S
Shengliang Guan 已提交
218
  if ((retrieveReq.free & TSDB_QUERY_TYPE_FREE_RESOURCE) != TSDB_QUERY_TYPE_FREE_RESOURCE) {
S
Shengliang Guan 已提交
219 220 221 222
    rowsToRead = pShow->numOfRows - pShow->numOfReads;
  }

  /* return no more than 100 tables in one round trip */
S
Shengliang Guan 已提交
223
  if (rowsToRead > SHOW_STEP_SIZE) rowsToRead = SHOW_STEP_SIZE;
S
Shengliang Guan 已提交
224 225 226 227 228 229 230 231

  /*
   * the actual number of table may be larger than the value of pShow->numOfRows, if a query is
   * issued during a continuous create table operation. Therefore, rowToRead may be less than 0.
   */
  if (rowsToRead < 0) rowsToRead = 0;
  size = pShow->rowSize * rowsToRead;

S
Shengliang Guan 已提交
232
  size += SHOW_STEP_SIZE;
S
Shengliang Guan 已提交
233 234 235 236
  SRetrieveTableRsp *pRsp = rpcMallocCont(size);
  if (pRsp == NULL) {
    mndReleaseShowObj(pShow, false);
    terrno = TSDB_CODE_OUT_OF_MEMORY;
S
Shengliang Guan 已提交
237
    mError("show:0x%" PRIx64 ", failed to retrieve data since %s", pShow->id, terrstr());
S
Shengliang Guan 已提交
238 239 240 241
    return -1;
  }

  // if free flag is set, client wants to clean the resources
S
Shengliang Guan 已提交
242
  if ((retrieveReq.free & TSDB_QUERY_TYPE_FREE_RESOURCE) != TSDB_QUERY_TYPE_FREE_RESOURCE) {
S
Shengliang Guan 已提交
243
    rowsRead = (*retrieveFp)(pReq, pShow, pRsp->data, rowsToRead);
S
Shengliang Guan 已提交
244 245
  }

S
Shengliang Guan 已提交
246
  mDebug("show:0x%" PRIx64 ", stop retrieve data, rowsRead:%d rowsToRead:%d", pShow->id, rowsRead, rowsToRead);
S
Shengliang Guan 已提交
247 248

  pRsp->numOfRows = htonl(rowsRead);
S
Shengliang Guan 已提交
249
  pRsp->precision = TSDB_TIME_PRECISION_MILLI;  // millisecond time precision
S
Shengliang Guan 已提交
250

S
Shengliang Guan 已提交
251 252
  pReq->pCont = pRsp;
  pReq->contLen = size;
S
Shengliang Guan 已提交
253

S
Shengliang Guan 已提交
254
  if (rowsRead == 0 || rowsToRead == 0 || (rowsRead == rowsToRead && pShow->numOfRows == pShow->numOfReads)) {
S
Shengliang Guan 已提交
255
    pRsp->completed = 1;
S
Shengliang Guan 已提交
256
    mDebug("show:0x%" PRIx64 ", retrieve completed", pShow->id);
S
Shengliang Guan 已提交
257 258
    mndReleaseShowObj(pShow, true);
  } else {
S
Shengliang Guan 已提交
259
    mDebug("show:0x%" PRIx64 ", retrieve not completed yet", pShow->id);
S
Shengliang Guan 已提交
260 261 262 263 264 265
    mndReleaseShowObj(pShow, false);
  }

  return TSDB_CODE_SUCCESS;
}

266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
static int32_t mndProcessRetrieveSysTableReq(SMnodeMsg *pReq) {
  SMnode    *pMnode = pReq->pMnode;
  SShowMgmt *pMgmt = &pMnode->showMgmt;
  int32_t    rowsToRead = 0;
  int32_t    size = 0;
  int32_t    rowsRead = 0;

  SRetrieveTableReq retrieveReq = {0};
  if (tDeserializeSRetrieveTableReq(pReq->rpcMsg.pCont, pReq->rpcMsg.contLen, &retrieveReq) != 0) {
    terrno = TSDB_CODE_INVALID_MSG;
    return -1;
  }

  SShowObj* pShow = NULL;

  if (retrieveReq.showId == 0) {
    SShowReq req = {0};
    req.type = retrieveReq.type;
    strncpy(req.db, retrieveReq.db, tListLen(req.db));

    pShow = mndCreateShowObj(pMnode, &req);
H
Haojun Liao 已提交
287 288 289 290 291 292 293 294 295 296 297 298 299 300
    STableMetaRsp *meta = (STableMetaRsp *)taosHashGet(pMnode->infosMeta, TSDB_INS_TABLE_USER_DATABASES, strlen(TSDB_INS_TABLE_USER_DATABASES));
    pShow->numOfRows = 100;

    int32_t offset = 0;
    for(int32_t i = 0; i < meta->numOfColumns; ++i) {
      pShow->numOfColumns = meta->numOfColumns;
      pShow->offset[i] = offset;

      int32_t bytes = meta->pSchemas[i].bytes;
      pShow->rowSize += bytes;
      pShow->bytes[i] = bytes;
      offset += bytes;
    }

301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
    if (pShow == NULL) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      mError("failed to process show-meta req since %s", terrstr());
      return -1;
    }
  } else {
    pShow = mndAcquireShowObj(pMnode, retrieveReq.showId);
    if (pShow == NULL) {
      terrno = TSDB_CODE_MND_INVALID_SHOWOBJ;
      mError("failed to process show-retrieve req:%p since %s", pShow, terrstr());
      return -1;
    }
  }

  ShowRetrieveFp retrieveFp = pMgmt->retrieveFps[pShow->type];
  if (retrieveFp == NULL) {
    mndReleaseShowObj((SShowObj*) pShow, false);
    terrno = TSDB_CODE_MSG_NOT_PROCESSED;
    mError("show:0x%" PRIx64 ", failed to retrieve data since %s", pShow->id, terrstr());
    return -1;
  }

  mDebug("show:0x%" PRIx64 ", start retrieve data, numOfReads:%d numOfRows:%d type:%s", pShow->id, pShow->numOfReads,
         pShow->numOfRows, mndShowStr(pShow->type));

  if (mndCheckRetrieveFinished((SShowObj*) pShow)) {
    mDebug("show:0x%" PRIx64 ", read finished, numOfReads:%d numOfRows:%d", pShow->id, pShow->numOfReads,
           pShow->numOfRows);
    pShow->numOfReads = pShow->numOfRows;
  }

  if ((retrieveReq.free & TSDB_QUERY_TYPE_FREE_RESOURCE) != TSDB_QUERY_TYPE_FREE_RESOURCE) {
    rowsToRead = pShow->numOfRows - pShow->numOfReads;
  }

  /* return no more than 100 tables in one round trip */
  if (rowsToRead > SHOW_STEP_SIZE) rowsToRead = SHOW_STEP_SIZE;

  /*
   * the actual number of table may be larger than the value of pShow->numOfRows, if a query is
   * issued during a continuous create table operation. Therefore, rowToRead may be less than 0.
   */
  if (rowsToRead < 0) rowsToRead = 0;
  size = pShow->rowSize * rowsToRead;

  size += SHOW_STEP_SIZE;
H
Haojun Liao 已提交
347
  SRetrieveMetaTableRsp *pRsp = rpcMallocCont(size);
348 349 350 351 352 353 354
  if (pRsp == NULL) {
    mndReleaseShowObj((SShowObj*) pShow, false);
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    mError("show:0x%" PRIx64 ", failed to retrieve data since %s", pShow->id, terrstr());
    return -1;
  }

H
Haojun Liao 已提交
355 356
  pRsp->handle = htobe64(pShow->id);

357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
  // if free flag is set, client wants to clean the resources
  if ((retrieveReq.free & TSDB_QUERY_TYPE_FREE_RESOURCE) != TSDB_QUERY_TYPE_FREE_RESOURCE) {
    rowsRead = (*retrieveFp)(pReq, (SShowObj*) pShow, pRsp->data, rowsToRead);
  }

  mDebug("show:0x%" PRIx64 ", stop retrieve data, rowsRead:%d rowsToRead:%d", pShow->id, rowsRead, rowsToRead);

  pRsp->numOfRows = htonl(rowsRead);
  pRsp->precision = TSDB_TIME_PRECISION_MILLI;  // millisecond time precision

  pReq->pCont = pRsp;
  pReq->contLen = size;

  if (rowsRead == 0 || rowsToRead == 0 || (rowsRead == rowsToRead && pShow->numOfRows == pShow->numOfReads)) {
    pRsp->completed = 1;
    mDebug("show:0x%" PRIx64 ", retrieve completed", pShow->id);
    mndReleaseShowObj((SShowObj*) pShow, true);
  } else {
    mDebug("show:0x%" PRIx64 ", retrieve not completed yet", pShow->id);
    mndReleaseShowObj((SShowObj*) pShow, false);
  }

  return TSDB_CODE_SUCCESS;
}

S
Shengliang Guan 已提交
382
char *mndShowStr(int32_t showType) {
S
Shengliang Guan 已提交
383 384 385 386 387 388 389 390 391 392 393 394 395
  switch (showType) {
    case TSDB_MGMT_TABLE_ACCT:
      return "show accounts";
    case TSDB_MGMT_TABLE_USER:
      return "show users";
    case TSDB_MGMT_TABLE_DB:
      return "show databases";
    case TSDB_MGMT_TABLE_TABLE:
      return "show tables";
    case TSDB_MGMT_TABLE_DNODE:
      return "show dnodes";
    case TSDB_MGMT_TABLE_MNODE:
      return "show mnodes";
S
Shengliang Guan 已提交
396 397 398 399 400 401
    case TSDB_MGMT_TABLE_QNODE:
      return "show qnodes";
    case TSDB_MGMT_TABLE_SNODE:
      return "show snodes";
    case TSDB_MGMT_TABLE_BNODE:
      return "show bnodes";
S
Shengliang Guan 已提交
402 403
    case TSDB_MGMT_TABLE_VGROUP:
      return "show vgroups";
S
Shengliang Guan 已提交
404
    case TSDB_MGMT_TABLE_STB:
S
Shengliang Guan 已提交
405 406 407 408 409 410 411 412 413 414 415
      return "show stables";
    case TSDB_MGMT_TABLE_MODULE:
      return "show modules";
    case TSDB_MGMT_TABLE_QUERIES:
      return "show queries";
    case TSDB_MGMT_TABLE_STREAMS:
      return "show streams";
    case TSDB_MGMT_TABLE_VARIABLES:
      return "show configs";
    case TSDB_MGMT_TABLE_CONNS:
      return "show connections";
S
Shengliang Guan 已提交
416 417
    case TSDB_MGMT_TABLE_TRANS:
      return "show trans";
S
Shengliang Guan 已提交
418 419 420 421 422
    case TSDB_MGMT_TABLE_GRANTS:
      return "show grants";
    case TSDB_MGMT_TABLE_VNODES:
      return "show vnodes";
    case TSDB_MGMT_TABLE_CLUSTER:
S
Shengliang Guan 已提交
423
      return "show cluster";
S
Shengliang Guan 已提交
424 425 426 427
    case TSDB_MGMT_TABLE_STREAMTABLES:
      return "show streamtables";
    case TSDB_MGMT_TABLE_TP:
      return "show topics";
S
Shengliang 已提交
428
    case TSDB_MGMT_TABLE_FUNC:
S
Shengliang Guan 已提交
429
      return "show functions";
S
Shengliang Guan 已提交
430 431 432 433 434 435 436 437
    default:
      return "undefined";
  }
}

static bool mndCheckRetrieveFinished(SShowObj *pShow) {
  if (pShow->pIter == NULL && pShow->numOfReads != 0) {
    return true;
S
Shengliang Guan 已提交
438
  }
S
Shengliang Guan 已提交
439 440 441
  return false;
}

S
Shengliang Guan 已提交
442
void mndVacuumResult(char *data, int32_t numOfCols, int32_t rows, int32_t capacity, SShowObj *pShow) {
S
Shengliang Guan 已提交
443 444 445 446 447 448 449
  if (rows < capacity) {
    for (int32_t i = 0; i < numOfCols; ++i) {
      memmove(data + pShow->offset[i] * rows, data + pShow->offset[i] * capacity, pShow->bytes[i] * rows);
    }
  }
}

S
Shengliang Guan 已提交
450
void mndAddShowMetaHandle(SMnode *pMnode, EShowType showType, ShowMetaFp fp) {
S
Shengliang Guan 已提交
451 452 453 454
  SShowMgmt *pMgmt = &pMnode->showMgmt;
  pMgmt->metaFps[showType] = fp;
}

S
Shengliang Guan 已提交
455
void mndAddShowRetrieveHandle(SMnode *pMnode, EShowType showType, ShowRetrieveFp fp) {
S
Shengliang Guan 已提交
456 457 458 459
  SShowMgmt *pMgmt = &pMnode->showMgmt;
  pMgmt->retrieveFps[showType] = fp;
}

S
Shengliang Guan 已提交
460
void mndAddShowFreeIterHandle(SMnode *pMnode, EShowType showType, ShowFreeIterFp fp) {
S
Shengliang Guan 已提交
461 462 463
  SShowMgmt *pMgmt = &pMnode->showMgmt;
  pMgmt->freeIterFps[showType] = fp;
}