mndVgroup.c 18.4 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 "mndVgroup.h"
S
Shengliang Guan 已提交
18
#include "mndDb.h"
S
Shengliang Guan 已提交
19
#include "mndDnode.h"
S
Shengliang Guan 已提交
20
#include "mndMnode.h"
S
Shengliang Guan 已提交
21 22
#include "mndShow.h"
#include "mndTrans.h"
S
Shengliang Guan 已提交
23

S
Shengliang Guan 已提交
24
#define TSDB_VGROUP_VER_NUMBER 1
S
Shengliang Guan 已提交
25 26
#define TSDB_VGROUP_RESERVE_SIZE 64

S
Shengliang Guan 已提交
27 28 29 30
static SSdbRow *mndVgroupActionDecode(SSdbRaw *pRaw);
static int32_t  mndVgroupActionInsert(SSdb *pSdb, SVgObj *pVgroup);
static int32_t  mndVgroupActionDelete(SSdb *pSdb, SVgObj *pVgroup);
static int32_t  mndVgroupActionUpdate(SSdb *pSdb, SVgObj *pOldVgroup, SVgObj *pNewVgroup);
S
Shengliang Guan 已提交
31 32 33 34 35 36 37 38 39 40

static int32_t mndProcessCreateVnodeRsp(SMnodeMsg *pMsg);
static int32_t mndProcessAlterVnodeRsp(SMnodeMsg *pMsg);
static int32_t mndProcessDropVnodeRsp(SMnodeMsg *pMsg);
static int32_t mndProcessSyncVnodeRsp(SMnodeMsg *pMsg);
static int32_t mndProcessCompactVnodeRsp(SMnodeMsg *pMsg);

static int32_t mndGetVgroupMeta(SMnodeMsg *pMsg, SShowObj *pShow, STableMetaMsg *pMeta);
static int32_t mndRetrieveVgroups(SMnodeMsg *pMsg, SShowObj *pShow, char *data, int32_t rows);
static void    mndCancelGetNextVgroup(SMnode *pMnode, void *pIter);
S
Shengliang Guan 已提交
41 42 43 44 45
static int32_t mndGetVnodeMeta(SMnodeMsg *pMsg, SShowObj *pShow, STableMetaMsg *pMeta);
static int32_t mndRetrieveVnodes(SMnodeMsg *pMsg, SShowObj *pShow, char *data, int32_t rows);
static void    mndCancelGetNextVnode(SMnode *pMnode, void *pIter);

int32_t mndInitVgroup(SMnode *pMnode) {
S
Shengliang Guan 已提交
46
  SSdbTable table = {.sdbType = SDB_VGROUP,
47
                     .keyType = SDB_KEY_INT32,
S
Shengliang Guan 已提交
48 49 50 51 52 53
                     .encodeFp = (SdbEncodeFp)mndVgroupActionEncode,
                     .decodeFp = (SdbDecodeFp)mndVgroupActionDecode,
                     .insertFp = (SdbInsertFp)mndVgroupActionInsert,
                     .updateFp = (SdbUpdateFp)mndVgroupActionDelete,
                     .deleteFp = (SdbDeleteFp)mndVgroupActionUpdate};

H
Hongze Cheng 已提交
54 55 56 57 58
  mndSetMsgHandle(pMnode, TDMT_DND_CREATE_VNODE_RSP, mndProcessCreateVnodeRsp);
  mndSetMsgHandle(pMnode, TDMT_DND_ALTER_VNODE_RSP, mndProcessAlterVnodeRsp);
  mndSetMsgHandle(pMnode, TDMT_DND_DROP_VNODE_RSP, mndProcessDropVnodeRsp);
  mndSetMsgHandle(pMnode, TDMT_DND_SYNC_VNODE_RSP, mndProcessSyncVnodeRsp);
  mndSetMsgHandle(pMnode, TDMT_DND_COMPACT_VNODE_RSP, mndProcessCompactVnodeRsp);
S
Shengliang Guan 已提交
59 60 61 62

  mndAddShowMetaHandle(pMnode, TSDB_MGMT_TABLE_VGROUP, mndGetVgroupMeta);
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_VGROUP, mndRetrieveVgroups);
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_VGROUP, mndCancelGetNextVgroup);
S
Shengliang Guan 已提交
63 64 65 66
  mndAddShowMetaHandle(pMnode, TSDB_MGMT_TABLE_VNODES, mndGetVnodeMeta);
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_VNODES, mndRetrieveVnodes);
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_VNODES, mndCancelGetNextVnode);

S
Shengliang Guan 已提交
67
  return sdbSetTable(pMnode->pSdb, table);
S
Shengliang Guan 已提交
68 69 70 71
}

void mndCleanupVgroup(SMnode *pMnode) {}

S
Shengliang Guan 已提交
72
SSdbRaw *mndVgroupActionEncode(SVgObj *pVgroup) {
S
Shengliang Guan 已提交
73
  SSdbRaw *pRaw = sdbAllocRaw(SDB_VGROUP, TSDB_VGROUP_VER_NUMBER, sizeof(SVgObj) + TSDB_VGROUP_RESERVE_SIZE);
S
Shengliang Guan 已提交
74 75 76 77 78 79 80
  if (pRaw == NULL) return NULL;

  int32_t dataPos = 0;
  SDB_SET_INT32(pRaw, dataPos, pVgroup->vgId)
  SDB_SET_INT64(pRaw, dataPos, pVgroup->createdTime)
  SDB_SET_INT64(pRaw, dataPos, pVgroup->updateTime)
  SDB_SET_INT32(pRaw, dataPos, pVgroup->version)
S
Shengliang Guan 已提交
81 82
  SDB_SET_INT32(pRaw, dataPos, pVgroup->hashBegin)
  SDB_SET_INT32(pRaw, dataPos, pVgroup->hashEnd)
S
Shengliang Guan 已提交
83
  SDB_SET_BINARY(pRaw, dataPos, pVgroup->dbName, TSDB_FULL_DB_NAME_LEN)
S
Shengliang Guan 已提交
84
  SDB_SET_INT64(pRaw, dataPos, pVgroup->dbUid)
S
Shengliang Guan 已提交
85 86 87 88 89 90 91 92 93 94 95 96
  SDB_SET_INT8(pRaw, dataPos, pVgroup->replica)
  for (int8_t i = 0; i < pVgroup->replica; ++i) {
    SVnodeGid *pVgid = &pVgroup->vnodeGid[i];
    SDB_SET_INT32(pRaw, dataPos, pVgid->dnodeId)
    SDB_SET_INT8(pRaw, dataPos, pVgid->role)
  }
  SDB_SET_RESERVE(pRaw, dataPos, TSDB_VGROUP_RESERVE_SIZE)
  SDB_SET_DATALEN(pRaw, dataPos);

  return pRaw;
}

S
Shengliang Guan 已提交
97
SSdbRow *mndVgroupActionDecode(SSdbRaw *pRaw) {
S
Shengliang Guan 已提交
98 99 100
  int8_t sver = 0;
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) return NULL;

S
Shengliang Guan 已提交
101
  if (sver != TSDB_VGROUP_VER_NUMBER) {
S
Shengliang Guan 已提交
102 103 104 105 106
    mError("failed to decode vgroup since %s", terrstr());
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
    return NULL;
  }

S
Shengliang Guan 已提交
107
  SSdbRow *pRow = sdbAllocRow(sizeof(SVgObj));
S
Shengliang Guan 已提交
108 109 110 111 112 113 114 115
  SVgObj  *pVgroup = sdbGetRowObj(pRow);
  if (pVgroup == NULL) return NULL;

  int32_t dataPos = 0;
  SDB_GET_INT32(pRaw, pRow, dataPos, &pVgroup->vgId)
  SDB_GET_INT64(pRaw, pRow, dataPos, &pVgroup->createdTime)
  SDB_GET_INT64(pRaw, pRow, dataPos, &pVgroup->updateTime)
  SDB_GET_INT32(pRaw, pRow, dataPos, &pVgroup->version)
S
Shengliang Guan 已提交
116 117
  SDB_GET_INT32(pRaw, pRow, dataPos, &pVgroup->hashBegin)
  SDB_GET_INT32(pRaw, pRow, dataPos, &pVgroup->hashEnd)
S
Shengliang Guan 已提交
118
  SDB_GET_BINARY(pRaw, pRow, dataPos, pVgroup->dbName, TSDB_FULL_DB_NAME_LEN)
S
Shengliang Guan 已提交
119
  SDB_GET_INT64(pRaw, pRow, dataPos, &pVgroup->dbUid)
S
Shengliang Guan 已提交
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
  SDB_GET_INT8(pRaw, pRow, dataPos, &pVgroup->replica)
  for (int8_t i = 0; i < pVgroup->replica; ++i) {
    SVnodeGid *pVgid = &pVgroup->vnodeGid[i];
    SDB_GET_INT32(pRaw, pRow, dataPos, &pVgid->dnodeId)
    SDB_GET_INT8(pRaw, pRow, dataPos, (int8_t *)&pVgid->role)
  }
  SDB_GET_RESERVE(pRaw, pRow, dataPos, TSDB_VGROUP_RESERVE_SIZE)

  return pRow;
}

static int32_t mndVgroupActionInsert(SSdb *pSdb, SVgObj *pVgroup) {
  mTrace("vgId:%d, perform insert action", pVgroup->vgId);
  return 0;
}

static int32_t mndVgroupActionDelete(SSdb *pSdb, SVgObj *pVgroup) {
  mTrace("vgId:%d, perform delete action", pVgroup->vgId);
  return 0;
}

S
Shengliang Guan 已提交
141
static int32_t mndVgroupActionUpdate(SSdb *pSdb, SVgObj *pOldVgroup, SVgObj *pNewVgroup) {
S
Shengliang Guan 已提交
142 143 144
  mTrace("vgId:%d, perform update action", pOldVgroup->vgId);
  pOldVgroup->updateTime = pNewVgroup->updateTime;
  pOldVgroup->version = pNewVgroup->version;
S
Shengliang Guan 已提交
145 146
  pOldVgroup->hashBegin = pNewVgroup->hashBegin;
  pOldVgroup->hashEnd = pNewVgroup->hashEnd;
S
Shengliang Guan 已提交
147 148 149 150 151 152
  pOldVgroup->replica = pNewVgroup->replica;
  memcpy(pOldVgroup->vnodeGid, pNewVgroup->vnodeGid, TSDB_MAX_REPLICA * sizeof(SVnodeGid));
  return 0;
}

SVgObj *mndAcquireVgroup(SMnode *pMnode, int32_t vgId) {
S
Shengliang Guan 已提交
153 154 155 156 157 158
  SSdb   *pSdb = pMnode->pSdb;
  SVgObj *pVgroup = sdbAcquire(pSdb, SDB_VGROUP, &vgId);
  if (pVgroup == NULL) {
    terrno = TSDB_CODE_MND_VGROUP_NOT_EXIST;
  }
  return pVgroup;
S
Shengliang Guan 已提交
159 160 161 162 163 164 165
}

void mndReleaseVgroup(SMnode *pMnode, SVgObj *pVgroup) {
  SSdb *pSdb = pMnode->pSdb;
  sdbRelease(pSdb, pVgroup);
}

S
Shengliang Guan 已提交
166
SCreateVnodeMsg *mndBuildCreateVnodeMsg(SMnode *pMnode, SDnodeObj *pDnode, SDbObj *pDb, SVgObj *pVgroup) {
S
Shengliang Guan 已提交
167
  SCreateVnodeMsg *pCreate = calloc(1, sizeof(SCreateVnodeMsg));
S
Shengliang Guan 已提交
168 169 170 171 172 173
  if (pCreate == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }

  pCreate->vgId = htonl(pVgroup->vgId);
S
Shengliang Guan 已提交
174
  pCreate->dnodeId = htonl(pDnode->id);
S
Shengliang Guan 已提交
175 176
  memcpy(pCreate->db, pDb->name, TSDB_FULL_DB_NAME_LEN);
  pCreate->dbUid = htobe64(pDb->uid);
177
  pCreate->vgVersion = htonl(pVgroup->version);
S
Shengliang Guan 已提交
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
  pCreate->cacheBlockSize = htonl(pDb->cfg.cacheBlockSize);
  pCreate->totalBlocks = htonl(pDb->cfg.totalBlocks);
  pCreate->daysPerFile = htonl(pDb->cfg.daysPerFile);
  pCreate->daysToKeep0 = htonl(pDb->cfg.daysToKeep0);
  pCreate->daysToKeep1 = htonl(pDb->cfg.daysToKeep1);
  pCreate->daysToKeep2 = htonl(pDb->cfg.daysToKeep2);
  pCreate->minRows = htonl(pDb->cfg.minRows);
  pCreate->maxRows = htonl(pDb->cfg.maxRows);
  pCreate->commitTime = htonl(pDb->cfg.commitTime);
  pCreate->fsyncPeriod = htonl(pDb->cfg.fsyncPeriod);
  pCreate->walLevel = pDb->cfg.walLevel;
  pCreate->precision = pDb->cfg.precision;
  pCreate->compression = pDb->cfg.compression;
  pCreate->quorum = pDb->cfg.quorum;
  pCreate->update = pDb->cfg.update;
  pCreate->cacheLastRow = pDb->cfg.cacheLastRow;
  pCreate->replica = pVgroup->replica;
  pCreate->selfIndex = -1;

  for (int32_t v = 0; v < pVgroup->replica; ++v) {
    SReplica  *pReplica = &pCreate->replicas[v];
    SVnodeGid *pVgid = &pVgroup->vnodeGid[v];
    SDnodeObj *pVgidDnode = mndAcquireDnode(pMnode, pVgid->dnodeId);
    if (pVgidDnode == NULL) {
S
Shengliang Guan 已提交
202
      free(pCreate);
S
Shengliang Guan 已提交
203 204 205 206 207 208 209 210 211 212 213 214 215 216
      return NULL;
    }

    pReplica->id = htonl(pVgidDnode->id);
    pReplica->port = htons(pVgidDnode->port);
    memcpy(pReplica->fqdn, pVgidDnode->fqdn, TSDB_FQDN_LEN);
    mndReleaseDnode(pMnode, pVgidDnode);

    if (pDnode->id == pVgid->dnodeId) {
      pCreate->selfIndex = v;
    }
  }

  if (pCreate->selfIndex == -1) {
S
Shengliang Guan 已提交
217
    free(pCreate);
S
Shengliang Guan 已提交
218 219 220 221 222 223 224 225
    terrno = TSDB_CODE_MND_APP_ERROR;
    return NULL;
  }

  return pCreate;
}

SDropVnodeMsg *mndBuildDropVnodeMsg(SMnode *pMnode, SDnodeObj *pDnode, SDbObj *pDb, SVgObj *pVgroup) {
S
Shengliang Guan 已提交
226
  SDropVnodeMsg *pDrop = calloc(1, sizeof(SDropVnodeMsg));
S
Shengliang Guan 已提交
227 228 229 230 231 232 233 234 235 236 237 238 239
  if (pDrop == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }

  pDrop->dnodeId = htonl(pDnode->id);
  pDrop->vgId = htonl(pVgroup->vgId);
  memcpy(pDrop->db, pDb->name, TSDB_FULL_DB_NAME_LEN);
  pDrop->dbUid = htobe64(pDb->uid);

  return pDrop;
}

S
Shengliang Guan 已提交
240 241 242 243 244 245 246 247 248 249 250 251 252 253
static int32_t mndGetAvailableDnode(SMnode *pMnode, SVgObj *pVgroup) {
  SSdb   *pSdb = pMnode->pSdb;
  int32_t allocedVnodes = 0;
  void   *pIter = NULL;

  while (allocedVnodes < pVgroup->replica) {
    SDnodeObj *pDnode = NULL;
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
    if (pIter == NULL) break;

    // todo
    if (mndIsDnodeInReadyStatus(pMnode, pDnode)) {
      SVnodeGid *pVgid = &pVgroup->vnodeGid[allocedVnodes];
      pVgid->dnodeId = pDnode->id;
S
Shengliang Guan 已提交
254 255 256 257 258
      if (pVgroup->replica == 1) {
        pVgid->role = TAOS_SYNC_STATE_LEADER;
      } else {
        pVgid->role = TAOS_SYNC_STATE_FOLLOWER;
      }
S
Shengliang Guan 已提交
259 260 261 262 263 264 265 266 267 268 269
      allocedVnodes++;
    }
    sdbRelease(pSdb, pDnode);
  }

  if (allocedVnodes != pVgroup->replica) {
    terrno = TSDB_CODE_MND_NO_ENOUGH_DNODES;
    return -1;
  }
  return 0;
}
270

S
Shengliang Guan 已提交
271
int32_t mndAllocVgroup(SMnode *pMnode, SDbObj *pDb, SVgObj **ppVgroups) {
272
  SVgObj *pVgroups = calloc(pDb->cfg.numOfVgroups, sizeof(SVgObj));
S
Shengliang Guan 已提交
273 274 275 276 277
  if (pVgroups == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

S
Shengliang Guan 已提交
278
  int32_t  allocedVgroups = 0;
S
Shengliang Guan 已提交
279 280 281
  int32_t  maxVgId = sdbGetMaxId(pMnode->pSdb, SDB_VGROUP);
  uint32_t hashMin = 0;
  uint32_t hashMax = UINT32_MAX;
282
  uint32_t hashInterval = (hashMax - hashMin) / pDb->cfg.numOfVgroups;
S
Shengliang Guan 已提交
283

284
  for (uint32_t v = 0; v < pDb->cfg.numOfVgroups; v++) {
S
Shengliang Guan 已提交
285
    SVgObj *pVgroup = &pVgroups[v];
S
Shengliang Guan 已提交
286
    pVgroup->vgId = maxVgId++;
S
Shengliang Guan 已提交
287 288
    pVgroup->createdTime = taosGetTimestampMs();
    pVgroup->updateTime = pVgroups->createdTime;
S
Shengliang Guan 已提交
289
    pVgroup->version = 1;
S
Shengliang Guan 已提交
290
    pVgroup->hashBegin = hashMin + hashInterval * v;
291
    if (v == pDb->cfg.numOfVgroups - 1) {
S
Shengliang Guan 已提交
292 293
      pVgroup->hashEnd = hashMax;
    } else {
S
Shengliang Guan 已提交
294
      pVgroup->hashEnd = hashMin + hashInterval * (v + 1) - 1;
S
Shengliang Guan 已提交
295
    }
S
Shengliang Guan 已提交
296

S
Shengliang Guan 已提交
297
    memcpy(pVgroup->dbName, pDb->name, TSDB_FULL_DB_NAME_LEN);
S
Shengliang Guan 已提交
298
    pVgroup->dbUid = pDb->uid;
S
Shengliang Guan 已提交
299
    pVgroup->replica = pDb->cfg.replications;
S
Shengliang Guan 已提交
300

S
Shengliang Guan 已提交
301
    if (mndGetAvailableDnode(pMnode, pVgroup) != 0) {
S
Shengliang Guan 已提交
302
      terrno = TSDB_CODE_MND_NO_ENOUGH_DNODES;
S
Shengliang Guan 已提交
303
      free(pVgroups);
S
Shengliang Guan 已提交
304 305 306
      return -1;
    }

S
Shengliang Guan 已提交
307
    allocedVgroups++;
308 309
  }

S
Shengliang Guan 已提交
310
  *ppVgroups = pVgroups;
311 312 313
  return 0;
}

314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
SEpSet mndGetVgroupEpset(SMnode *pMnode, SVgObj *pVgroup) {
  SEpSet epset = {0};

  for (int32_t v = 0; v < pVgroup->replica; ++v) {
    SVnodeGid *pVgid = &pVgroup->vnodeGid[v];
    SDnodeObj *pDnode = mndAcquireDnode(pMnode, pVgid->dnodeId);
    if (pDnode == NULL) continue;

    if (pVgid->role == TAOS_SYNC_STATE_LEADER) {
      epset.inUse = epset.numOfEps;
    }

    epset.port[epset.numOfEps] = pDnode->port;
    memcpy(&epset.fqdn[epset.numOfEps], pDnode->fqdn, TSDB_FQDN_LEN);
    epset.numOfEps++;
    mndReleaseDnode(pMnode, pDnode);
  }

  return epset;
}

335 336 337 338 339
static int32_t mndProcessCreateVnodeRsp(SMnodeMsg *pMsg) {
  mndTransHandleActionRsp(pMsg);
  return 0;
}

S
Shengliang Guan 已提交
340 341 342 343 344 345 346 347 348 349
static int32_t mndProcessAlterVnodeRsp(SMnodeMsg *pMsg) {
  mndTransHandleActionRsp(pMsg);
  return 0;
}

static int32_t mndProcessDropVnodeRsp(SMnodeMsg *pMsg) {
  mndTransHandleActionRsp(pMsg);
  return 0;
}

S
Shengliang Guan 已提交
350 351 352
static int32_t mndProcessSyncVnodeRsp(SMnodeMsg *pMsg) { return 0; }
static int32_t mndProcessCompactVnodeRsp(SMnodeMsg *pMsg) { return 0; }

S
Shengliang Guan 已提交
353
static int32_t mndGetVgroupMaxReplica(SMnode *pMnode, char *dbName, int8_t *pReplica, int32_t *pNumOfVgroups) {
S
Shengliang Guan 已提交
354
  SSdb   *pSdb = pMnode->pSdb;
S
Shengliang Guan 已提交
355 356 357 358 359 360
  SDbObj *pDb = mndAcquireDb(pMnode, dbName);
  if (pDb == NULL) {
    terrno = TSDB_CODE_MND_DB_NOT_SELECTED;
    return -1;
  }

S
Shengliang Guan 已提交
361 362 363 364 365 366 367 368 369
  int8_t  replica = 1;
  int32_t numOfVgroups = 0;

  void *pIter = NULL;
  while (1) {
    SVgObj *pVgroup = NULL;
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
    if (pIter == NULL) break;

S
Shengliang Guan 已提交
370
    if (pVgroup->dbUid == pDb->uid) {
S
Shengliang Guan 已提交
371 372 373 374 375 376 377 378 379
      replica = MAX(replica, pVgroup->replica);
      numOfVgroups++;
    }

    sdbRelease(pSdb, pVgroup);
  }

  *pReplica = replica;
  *pNumOfVgroups = numOfVgroups;
S
Shengliang Guan 已提交
380
  return 0;
S
Shengliang Guan 已提交
381 382 383 384 385 386
}

static int32_t mndGetVgroupMeta(SMnodeMsg *pMsg, SShowObj *pShow, STableMetaMsg *pMeta) {
  SMnode *pMnode = pMsg->pMnode;
  SSdb   *pSdb = pMnode->pSdb;

S
Shengliang Guan 已提交
387 388 389
  if (mndGetVgroupMaxReplica(pMnode, pShow->db, &pShow->replica, &pShow->numOfRows) != 0) {
    return -1;
  }
S
Shengliang Guan 已提交
390 391

  int32_t  cols = 0;
S
Shengliang Guan 已提交
392
  SSchema *pSchema = pMeta->pSchema;
S
Shengliang Guan 已提交
393 394 395 396

  pShow->bytes[cols] = 4;
  pSchema[cols].type = TSDB_DATA_TYPE_INT;
  strcpy(pSchema[cols].name, "vgId");
H
Haojun Liao 已提交
397
  pSchema[cols].bytes = htonl(pShow->bytes[cols]);
S
Shengliang Guan 已提交
398 399 400 401 402
  cols++;

  pShow->bytes[cols] = 4;
  pSchema[cols].type = TSDB_DATA_TYPE_INT;
  strcpy(pSchema[cols].name, "tables");
H
Haojun Liao 已提交
403
  pSchema[cols].bytes = htonl(pShow->bytes[cols]);
S
Shengliang Guan 已提交
404 405 406
  cols++;

  for (int32_t i = 0; i < pShow->replica; ++i) {
S
Shengliang Guan 已提交
407
    pShow->bytes[cols] = 2;
S
Shengliang Guan 已提交
408 409
    pSchema[cols].type = TSDB_DATA_TYPE_SMALLINT;
    snprintf(pSchema[cols].name, TSDB_COL_NAME_LEN, "v%d_dnode", i + 1);
H
Haojun Liao 已提交
410
    pSchema[cols].bytes = htonl(pShow->bytes[cols]);
S
Shengliang Guan 已提交
411 412 413 414 415
    cols++;

    pShow->bytes[cols] = 9 + VARSTR_HEADER_SIZE;
    pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
    snprintf(pSchema[cols].name, TSDB_COL_NAME_LEN, "v%d_status", i + 1);
H
Haojun Liao 已提交
416
    pSchema[cols].bytes = htonl(pShow->bytes[cols]);
S
Shengliang Guan 已提交
417 418 419
    cols++;
  }

S
Shengliang Guan 已提交
420
  pMeta->numOfColumns = htonl(cols);
S
Shengliang Guan 已提交
421 422 423 424 425 426 427 428
  pShow->numOfColumns = cols;

  pShow->offset[0] = 0;
  for (int32_t i = 1; i < cols; ++i) {
    pShow->offset[i] = pShow->offset[i - 1] + pShow->bytes[i - 1];
  }

  pShow->rowSize = pShow->offset[cols - 1] + pShow->bytes[cols - 1];
429
  strcpy(pMeta->tbFname, mndShowStr(pShow->type));
S
Shengliang Guan 已提交
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457

  return 0;
}

static int32_t mndRetrieveVgroups(SMnodeMsg *pMsg, SShowObj *pShow, char *data, int32_t rows) {
  SMnode *pMnode = pMsg->pMnode;
  SSdb   *pSdb = pMnode->pSdb;
  int32_t numOfRows = 0;
  SVgObj *pVgroup = NULL;
  int32_t cols = 0;
  char   *pWrite;

  while (numOfRows < rows) {
    pShow->pIter = sdbFetch(pSdb, SDB_VGROUP, pShow->pIter, (void **)&pVgroup);
    if (pShow->pIter == NULL) break;

    cols = 0;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
    *(int32_t *)pWrite = pVgroup->vgId;
    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
    *(int32_t *)pWrite = pVgroup->numOfTables;
    cols++;

    for (int32_t i = 0; i < pShow->replica; ++i) {
      pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
S
Shengliang Guan 已提交
458
      *(int16_t *)pWrite = pVgroup->vnodeGid[i].dnodeId;
S
Shengliang Guan 已提交
459 460 461 462 463 464 465
      cols++;

      const char *role = mndGetRoleStr(pVgroup->vnodeGid[i].role);
      pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
      STR_WITH_MAXSIZE_TO_VARSTR(pWrite, role, pShow->bytes[cols]);
      cols++;
    }
S
Shengliang Guan 已提交
466

S
Shengliang Guan 已提交
467 468 469 470
    sdbRelease(pSdb, pVgroup);
    numOfRows++;
  }

S
Shengliang Guan 已提交
471
  mndVacuumResult(data, pShow->numOfColumns, numOfRows, rows, pShow);
S
Shengliang Guan 已提交
472 473 474 475 476 477 478 479 480 481
  pShow->numOfReads += numOfRows;
  return numOfRows;
}

static void mndCancelGetNextVgroup(SMnode *pMnode, void *pIter) {
  SSdb *pSdb = pMnode->pSdb;
  sdbCancelFetch(pSdb, pIter);
}

static int32_t mndGetVnodesNum(SMnode *pMnode, int32_t dnodeId) {
S
Shengliang Guan 已提交
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
  SSdb   *pSdb = pMnode->pSdb;
  int32_t numOfVnodes = 0;
  void   *pIter = NULL;

  while (1) {
    SVgObj *pVgroup = NULL;
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
    if (pIter == NULL) break;

    for (int32_t v = 0; v < pVgroup->replica; ++v) {
      if (pVgroup->vnodeGid[v].dnodeId == dnodeId) {
        numOfVnodes++;
      }
    }

    sdbRelease(pSdb, pVgroup);
S
Shengliang Guan 已提交
498 499
  }

S
Shengliang Guan 已提交
500
  return numOfVnodes;
S
Shengliang Guan 已提交
501 502 503 504 505 506 507
}

static int32_t mndGetVnodeMeta(SMnodeMsg *pMsg, SShowObj *pShow, STableMetaMsg *pMeta) {
  SMnode *pMnode = pMsg->pMnode;
  SSdb   *pSdb = pMnode->pSdb;

  int32_t  cols = 0;
S
Shengliang Guan 已提交
508
  SSchema *pSchema = pMeta->pSchema;
S
Shengliang Guan 已提交
509 510 511 512

  pShow->bytes[cols] = 4;
  pSchema[cols].type = TSDB_DATA_TYPE_INT;
  strcpy(pSchema[cols].name, "vgId");
H
Haojun Liao 已提交
513
  pSchema[cols].bytes = htonl(pShow->bytes[cols]);
S
Shengliang Guan 已提交
514 515 516 517 518
  cols++;

  pShow->bytes[cols] = 12 + VARSTR_HEADER_SIZE;
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
  strcpy(pSchema[cols].name, "status");
H
Haojun Liao 已提交
519
  pSchema[cols].bytes = htonl(pShow->bytes[cols]);
S
Shengliang Guan 已提交
520 521
  cols++;

S
Shengliang Guan 已提交
522
  pMeta->numOfColumns = htonl(cols);
S
Shengliang Guan 已提交
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
  pShow->numOfColumns = cols;

  pShow->offset[0] = 0;
  for (int32_t i = 1; i < cols; ++i) {
    pShow->offset[i] = pShow->offset[i - 1] + pShow->bytes[i - 1];
  }

  int32_t dnodeId = 0;
  if (pShow->payloadLen > 0) {
    dnodeId = atoi(pShow->payload);
  }

  pShow->replica = dnodeId;
  pShow->numOfRows = mndGetVnodesNum(pMnode, dnodeId);
  pShow->rowSize = pShow->offset[cols - 1] + pShow->bytes[cols - 1];
538
  strcpy(pMeta->tbFname, mndShowStr(pShow->type));
S
Shengliang Guan 已提交
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555

  return 0;
}

static int32_t mndRetrieveVnodes(SMnodeMsg *pMsg, SShowObj *pShow, char *data, int32_t rows) {
  SMnode *pMnode = pMsg->pMnode;
  SSdb   *pSdb = pMnode->pSdb;
  int32_t numOfRows = 0;
  SVgObj *pVgroup = NULL;
  char   *pWrite;
  int32_t cols = 0;
  int32_t dnodeId = pShow->replica;

  while (numOfRows < rows) {
    pShow->pIter = sdbFetch(pSdb, SDB_VGROUP, pShow->pIter, (void **)&pVgroup);
    if (pShow->pIter == NULL) break;

S
Shengliang Guan 已提交
556
    for (int32_t i = 0; i < pVgroup->replica && numOfRows < rows; ++i) {
S
Shengliang Guan 已提交
557 558 559 560 561 562 563 564 565 566
      SVnodeGid *pVgid = &pVgroup->vnodeGid[i];
      if (pVgid->dnodeId != dnodeId) continue;

      cols = 0;

      pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
      *(uint32_t *)pWrite = pVgroup->vgId;
      cols++;

      pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
S
Shengliang Guan 已提交
567
      STR_TO_VARSTR(pWrite, mndGetRoleStr(pVgid->role));
S
Shengliang Guan 已提交
568 569 570 571 572 573 574
      cols++;
      numOfRows++;
    }

    sdbRelease(pSdb, pVgroup);
  }

S
Shengliang Guan 已提交
575
  mndVacuumResult(data, pShow->numOfColumns, numOfRows, rows, pShow);
S
Shengliang Guan 已提交
576 577 578 579 580 581 582 583
  pShow->numOfReads += numOfRows;
  return numOfRows;
}

static void mndCancelGetNextVnode(SMnode *pMnode, void *pIter) {
  SSdb *pSdb = pMnode->pSdb;
  sdbCancelFetch(pSdb, pIter);
}