mndVgroup.c 20.6 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)
83
  SDB_SET_BINARY(pRaw, dataPos, pVgroup->dbName, TSDB_DB_FNAME_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
  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_RESERVE(pRaw, dataPos, TSDB_VGROUP_RESERVE_SIZE)
  SDB_SET_DATALEN(pRaw, dataPos);

  return pRaw;
}

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

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

S
Shengliang Guan 已提交
106
  SSdbRow *pRow = sdbAllocRow(sizeof(SVgObj));
S
Shengliang Guan 已提交
107 108 109 110 111 112 113 114
  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 已提交
115 116
  SDB_GET_INT32(pRaw, pRow, dataPos, &pVgroup->hashBegin)
  SDB_GET_INT32(pRaw, pRow, dataPos, &pVgroup->hashEnd)
117
  SDB_GET_BINARY(pRaw, pRow, dataPos, pVgroup->dbName, TSDB_DB_FNAME_LEN)
S
Shengliang Guan 已提交
118
  SDB_GET_INT64(pRaw, pRow, dataPos, &pVgroup->dbUid)
S
Shengliang Guan 已提交
119 120 121 122
  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)
S
Shengliang Guan 已提交
123 124 125
    if (pVgroup->replica == 1) {
      pVgid->role = TAOS_SYNC_STATE_LEADER;
    }
S
Shengliang Guan 已提交
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
  }
  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 已提交
142
static int32_t mndVgroupActionUpdate(SSdb *pSdb, SVgObj *pOldVgroup, SVgObj *pNewVgroup) {
S
Shengliang Guan 已提交
143 144 145
  mTrace("vgId:%d, perform update action", pOldVgroup->vgId);
  pOldVgroup->updateTime = pNewVgroup->updateTime;
  pOldVgroup->version = pNewVgroup->version;
S
Shengliang Guan 已提交
146 147
  pOldVgroup->hashBegin = pNewVgroup->hashBegin;
  pOldVgroup->hashEnd = pNewVgroup->hashEnd;
S
Shengliang Guan 已提交
148 149 150 151 152 153
  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 已提交
154 155 156 157 158 159
  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 已提交
160 161 162 163 164 165 166
}

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

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

  pCreate->vgId = htonl(pVgroup->vgId);
S
Shengliang Guan 已提交
175
  pCreate->dnodeId = htonl(pDnode->id);
176
  memcpy(pCreate->db, pDb->name, TSDB_DB_FNAME_LEN);
S
Shengliang Guan 已提交
177
  pCreate->dbUid = htobe64(pDb->uid);
178
  pCreate->vgVersion = htonl(pVgroup->version);
S
Shengliang Guan 已提交
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
  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 已提交
203
      free(pCreate);
S
Shengliang Guan 已提交
204 205 206 207 208 209 210 211 212 213 214 215 216 217
      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 已提交
218
    free(pCreate);
S
Shengliang Guan 已提交
219 220 221 222 223 224 225 226
    terrno = TSDB_CODE_MND_APP_ERROR;
    return NULL;
  }

  return pCreate;
}

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

  pDrop->dnodeId = htonl(pDnode->id);
  pDrop->vgId = htonl(pVgroup->vgId);
235
  memcpy(pDrop->db, pDb->name, TSDB_DB_FNAME_LEN);
S
Shengliang Guan 已提交
236 237 238 239 240
  pDrop->dbUid = htobe64(pDb->uid);

  return pDrop;
}

S
Shengliang Guan 已提交
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
static bool mndResetDnodesArrayFp(SMnode *pMnode, void *pObj, void *p1, void *p2, void *p3) {
  SDnodeObj *pDnode = pObj;
  pDnode->numOfVnodes = 0;
  return true;
}

static bool mndBuildDnodesArrayFp(SMnode *pMnode, void *pObj, void *p1, void *p2, void *p3) {
  SDnodeObj *pDnode = pObj;
  SArray    *pArray = p1;

  pDnode->numOfVnodes = mndGetVnodesNum(pMnode, pDnode->id);

  int64_t curMs = taosGetTimestampMs();
  bool    online = mndIsDnodeOnline(pMnode, pDnode, curMs);
  if (online && pDnode->numOfSupportVnodes > 0) {
    taosArrayPush(pArray, pDnode);
  }

  bool isMnode = mndIsMnode(pMnode, pDnode->id);

  mDebug("dnode:%d, vnodes:%d supportVnodes:%d isMnode:%d online:%d", pDnode->id, pDnode->numOfVnodes,
         pDnode->numOfSupportVnodes, isMnode, online);

  if (isMnode) {
    pDnode->numOfVnodes++;
  }

  return true;
}

S
Shengliang Guan 已提交
271
static SArray *mndBuildDnodesArray(SMnode *pMnode) {
S
Shengliang Guan 已提交
272
  SSdb   *pSdb = pMnode->pSdb;
S
Shengliang Guan 已提交
273
  int32_t numOfDnodes = mndGetDnodeSize(pMnode);
S
Shengliang Guan 已提交
274

S
Shengliang Guan 已提交
275 276 277 278 279
  SArray *pArray = taosArrayInit(numOfDnodes, sizeof(SDnodeObj));
  if (pArray == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }
S
Shengliang Guan 已提交
280

S
Shengliang Guan 已提交
281 282
  sdbTraverse(pSdb, SDB_DNODE, mndResetDnodesArrayFp, NULL, NULL, NULL);
  sdbTraverse(pSdb, SDB_DNODE, mndBuildDnodesArrayFp, pArray, NULL, NULL);
S
Shengliang Guan 已提交
283 284 285 286 287 288
  return pArray;
}

static int32_t mndCompareDnodeVnodes(SDnodeObj *pDnode1, SDnodeObj *pDnode2) {
  float d1Score = (float)pDnode1->numOfVnodes / pDnode1->numOfSupportVnodes;
  float d2Score = (float)pDnode2->numOfVnodes / pDnode2->numOfSupportVnodes;
S
Shengliang Guan 已提交
289
  return d1Score > d2Score ? 1 : 0;
S
Shengliang Guan 已提交
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
}

static int32_t mndGetAvailableDnode(SMnode *pMnode, SVgObj *pVgroup, SArray *pArray) {
  SSdb   *pSdb = pMnode->pSdb;
  int32_t allocedVnodes = 0;
  void   *pIter = NULL;

  taosArraySort(pArray, (__compar_fn_t)mndCompareDnodeVnodes);

  for (int32_t v = 0; v < pVgroup->replica; ++v) {
    SVnodeGid *pVgid = &pVgroup->vnodeGid[v];
    SDnodeObj *pDnode = taosArrayGet(pArray, v);
    if (pDnode == NULL || pDnode->numOfVnodes > pDnode->numOfSupportVnodes) {
      terrno = TSDB_CODE_MND_NO_ENOUGH_DNODES;
      return -1;
    }

    pVgid->dnodeId = pDnode->id;
    if (pVgroup->replica == 1) {
      pVgid->role = TAOS_SYNC_STATE_LEADER;
    } else {
      pVgid->role = TAOS_SYNC_STATE_FOLLOWER;
    }

S
Shengliang Guan 已提交
314
    mDebug("db:%s, vgId:%d, vn:%d dnode:%d is alloced", pVgroup->dbName, pVgroup->vgId, v, pVgid->dnodeId);
S
Shengliang Guan 已提交
315
    pDnode->numOfVnodes++;
S
Shengliang Guan 已提交
316
  }
S
Shengliang Guan 已提交
317

S
Shengliang Guan 已提交
318 319
  return 0;
}
320

S
Shengliang Guan 已提交
321
int32_t mndAllocVgroup(SMnode *pMnode, SDbObj *pDb, SVgObj **ppVgroups) {
S
Shengliang Guan 已提交
322 323 324 325 326
  int32_t code = -1;
  SArray *pArray = NULL;
  SVgObj *pVgroups = NULL;

  pVgroups = calloc(pDb->cfg.numOfVgroups, sizeof(SVgObj));
S
Shengliang Guan 已提交
327 328
  if (pVgroups == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
S
Shengliang Guan 已提交
329
    goto ALLOC_VGROUP_OVER;
S
Shengliang Guan 已提交
330 331
  }

S
Shengliang Guan 已提交
332 333 334 335 336 337 338 339
  pArray = mndBuildDnodesArray(pMnode);
  if (pArray == NULL) {
    goto ALLOC_VGROUP_OVER;
  }

  mDebug("db:%s, total %d dnodes used to create %d vgroups (%d vnodes)", pDb->name, (int32_t)taosArrayGetSize(pArray),
         pDb->cfg.numOfVgroups, pDb->cfg.numOfVgroups * pDb->cfg.replications);

S
Shengliang Guan 已提交
340
  int32_t  allocedVgroups = 0;
S
Shengliang Guan 已提交
341 342 343
  int32_t  maxVgId = sdbGetMaxId(pMnode->pSdb, SDB_VGROUP);
  uint32_t hashMin = 0;
  uint32_t hashMax = UINT32_MAX;
344
  uint32_t hashInterval = (hashMax - hashMin) / pDb->cfg.numOfVgroups;
S
Shengliang Guan 已提交
345

346 347
  if (maxVgId < 2) maxVgId = 2;

348
  for (uint32_t v = 0; v < pDb->cfg.numOfVgroups; v++) {
S
Shengliang Guan 已提交
349
    SVgObj *pVgroup = &pVgroups[v];
S
Shengliang Guan 已提交
350
    pVgroup->vgId = maxVgId++;
S
Shengliang Guan 已提交
351 352
    pVgroup->createdTime = taosGetTimestampMs();
    pVgroup->updateTime = pVgroups->createdTime;
S
Shengliang Guan 已提交
353
    pVgroup->version = 1;
S
Shengliang Guan 已提交
354
    pVgroup->hashBegin = hashMin + hashInterval * v;
355
    if (v == pDb->cfg.numOfVgroups - 1) {
S
Shengliang Guan 已提交
356 357
      pVgroup->hashEnd = hashMax;
    } else {
S
Shengliang Guan 已提交
358
      pVgroup->hashEnd = hashMin + hashInterval * (v + 1) - 1;
S
Shengliang Guan 已提交
359
    }
S
Shengliang Guan 已提交
360

361
    memcpy(pVgroup->dbName, pDb->name, TSDB_DB_FNAME_LEN);
S
Shengliang Guan 已提交
362
    pVgroup->dbUid = pDb->uid;
S
Shengliang Guan 已提交
363
    pVgroup->replica = pDb->cfg.replications;
S
Shengliang Guan 已提交
364

S
Shengliang Guan 已提交
365
    if (mndGetAvailableDnode(pMnode, pVgroup, pArray) != 0) {
S
Shengliang Guan 已提交
366
      terrno = TSDB_CODE_MND_NO_ENOUGH_DNODES;
S
Shengliang Guan 已提交
367
      goto ALLOC_VGROUP_OVER;
S
Shengliang Guan 已提交
368 369
    }

S
Shengliang Guan 已提交
370
    allocedVgroups++;
371 372
  }

S
Shengliang Guan 已提交
373
  *ppVgroups = pVgroups;
S
Shengliang Guan 已提交
374 375 376 377 378 379 380 381
  code = 0;

  mDebug("db:%s, %d vgroups is alloced, replica:%d", pDb->name, pDb->cfg.numOfVgroups, pDb->cfg.replications);

ALLOC_VGROUP_OVER:
  if (code != 0) free(pVgroups);
  taosArrayDestroy(pArray);
  return code;
382 383
}

384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
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;
}

405
static int32_t mndProcessCreateVnodeRsp(SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
406
  mndTransProcessRsp(pMsg);
407 408 409
  return 0;
}

S
Shengliang Guan 已提交
410
static int32_t mndProcessAlterVnodeRsp(SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
411
  mndTransProcessRsp(pMsg);
S
Shengliang Guan 已提交
412 413 414 415
  return 0;
}

static int32_t mndProcessDropVnodeRsp(SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
416
  mndTransProcessRsp(pMsg);
S
Shengliang Guan 已提交
417 418 419
  return 0;
}

S
Shengliang Guan 已提交
420
static int32_t mndProcessSyncVnodeRsp(SMnodeMsg *pMsg) { return 0; }
S
Shengliang Guan 已提交
421

S
Shengliang Guan 已提交
422 423
static int32_t mndProcessCompactVnodeRsp(SMnodeMsg *pMsg) { return 0; }

S
Shengliang Guan 已提交
424 425 426 427 428 429 430 431 432 433 434 435 436 437
static bool mndGetVgroupMaxReplicaFp(SMnode *pMnode, void *pObj, void *p1, void *p2, void *p3) {
  SVgObj  *pVgroup = pObj;
  int64_t  uid = *(int64_t *)p1;
  int8_t  *pReplica = p2;
  int32_t *pNumOfVgroups = p3;

  if (pVgroup->dbUid == uid) {
    *pReplica = MAX(*pReplica, pVgroup->replica);
    (*pNumOfVgroups)++;
  }

  return true;
}

S
Shengliang Guan 已提交
438
static int32_t mndGetVgroupMaxReplica(SMnode *pMnode, char *dbName, int8_t *pReplica, int32_t *pNumOfVgroups) {
S
Shengliang Guan 已提交
439
  SSdb   *pSdb = pMnode->pSdb;
S
Shengliang Guan 已提交
440 441 442 443 444 445
  SDbObj *pDb = mndAcquireDb(pMnode, dbName);
  if (pDb == NULL) {
    terrno = TSDB_CODE_MND_DB_NOT_SELECTED;
    return -1;
  }

S
Shengliang Guan 已提交
446 447 448 449
  *pReplica = 1;
  *pNumOfVgroups = 0;
  sdbTraverse(pSdb, SDB_VGROUP, mndGetVgroupMaxReplicaFp, &pDb->uid, pReplica, pNumOfVgroups);
  mndReleaseDb(pMnode, pDb);
S
Shengliang Guan 已提交
450
  return 0;
S
Shengliang Guan 已提交
451 452 453 454 455 456
}

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

S
Shengliang Guan 已提交
457 458 459
  if (mndGetVgroupMaxReplica(pMnode, pShow->db, &pShow->replica, &pShow->numOfRows) != 0) {
    return -1;
  }
S
Shengliang Guan 已提交
460 461

  int32_t  cols = 0;
S
Shengliang Guan 已提交
462
  SSchema *pSchema = pMeta->pSchema;
S
Shengliang Guan 已提交
463 464 465 466

  pShow->bytes[cols] = 4;
  pSchema[cols].type = TSDB_DATA_TYPE_INT;
  strcpy(pSchema[cols].name, "vgId");
H
Haojun Liao 已提交
467
  pSchema[cols].bytes = htonl(pShow->bytes[cols]);
S
Shengliang Guan 已提交
468 469 470 471 472
  cols++;

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

  for (int32_t i = 0; i < pShow->replica; ++i) {
S
Shengliang Guan 已提交
477
    pShow->bytes[cols] = 2;
S
Shengliang Guan 已提交
478 479
    pSchema[cols].type = TSDB_DATA_TYPE_SMALLINT;
    snprintf(pSchema[cols].name, TSDB_COL_NAME_LEN, "v%d_dnode", i + 1);
H
Haojun Liao 已提交
480
    pSchema[cols].bytes = htonl(pShow->bytes[cols]);
S
Shengliang Guan 已提交
481 482 483 484 485
    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 已提交
486
    pSchema[cols].bytes = htonl(pShow->bytes[cols]);
S
Shengliang Guan 已提交
487 488 489
    cols++;
  }

S
Shengliang Guan 已提交
490
  pMeta->numOfColumns = htonl(cols);
S
Shengliang Guan 已提交
491 492 493 494 495 496 497 498
  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];
499
  strcpy(pMeta->tbFname, mndShowStr(pShow->type));
S
Shengliang Guan 已提交
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527

  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 已提交
528
      *(int16_t *)pWrite = pVgroup->vnodeGid[i].dnodeId;
S
Shengliang Guan 已提交
529 530 531 532 533 534 535
      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 已提交
536

S
Shengliang Guan 已提交
537 538 539 540
    sdbRelease(pSdb, pVgroup);
    numOfRows++;
  }

S
Shengliang Guan 已提交
541
  mndVacuumResult(data, pShow->numOfColumns, numOfRows, rows, pShow);
S
Shengliang Guan 已提交
542 543 544 545 546 547 548 549 550
  pShow->numOfReads += numOfRows;
  return numOfRows;
}

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

S
Shengliang Guan 已提交
551 552 553 554
static bool mndGetVnodesNumFp(SMnode *pMnode, void *pObj, void *p1, void *p2, void *p3) {
  SVgObj  *pVgroup = pObj;
  int32_t  dnodeId = *(int32_t *)p1;
  int32_t *pNumOfVnodes = (int32_t *)p2;
S
Shengliang Guan 已提交
555

S
Shengliang Guan 已提交
556 557 558
  for (int32_t v = 0; v < pVgroup->replica; ++v) {
    if (pVgroup->vnodeGid[v].dnodeId == dnodeId) {
      (*pNumOfVnodes)++;
S
Shengliang Guan 已提交
559
    }
S
Shengliang Guan 已提交
560 561
  }

S
Shengliang Guan 已提交
562 563 564 565 566 567
  return true;
}

int32_t mndGetVnodesNum(SMnode *pMnode, int32_t dnodeId) {
  int32_t numOfVnodes = 0;
  sdbTraverse(pMnode->pSdb, SDB_VGROUP, mndGetVnodesNumFp, &dnodeId, &numOfVnodes, NULL);
S
Shengliang Guan 已提交
568
  return numOfVnodes;
S
Shengliang Guan 已提交
569 570 571 572 573 574 575
}

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 已提交
576
  SSchema *pSchema = pMeta->pSchema;
S
Shengliang Guan 已提交
577 578 579 580

  pShow->bytes[cols] = 4;
  pSchema[cols].type = TSDB_DATA_TYPE_INT;
  strcpy(pSchema[cols].name, "vgId");
H
Haojun Liao 已提交
581
  pSchema[cols].bytes = htonl(pShow->bytes[cols]);
S
Shengliang Guan 已提交
582 583 584 585 586
  cols++;

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

S
Shengliang Guan 已提交
590
  pMeta->numOfColumns = htonl(cols);
S
Shengliang Guan 已提交
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605
  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];
606
  strcpy(pMeta->tbFname, mndShowStr(pShow->type));
S
Shengliang Guan 已提交
607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623

  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 已提交
624
    for (int32_t i = 0; i < pVgroup->replica && numOfRows < rows; ++i) {
S
Shengliang Guan 已提交
625 626 627 628 629 630 631 632 633 634
      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 已提交
635
      STR_TO_VARSTR(pWrite, mndGetRoleStr(pVgid->role));
S
Shengliang Guan 已提交
636 637 638 639 640 641 642
      cols++;
      numOfRows++;
    }

    sdbRelease(pSdb, pVgroup);
  }

S
Shengliang Guan 已提交
643
  mndVacuumResult(data, pShow->numOfColumns, numOfRows, rows, pShow);
S
Shengliang Guan 已提交
644 645 646 647 648 649 650 651
  pShow->numOfReads += numOfRows;
  return numOfRows;
}

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