mndBnode.c 14.6 KB
Newer Older
S
Shengliang Guan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * 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/>.
 */

#define _DEFAULT_SOURCE
#include "mndBnode.h"
S
Shengliang Guan 已提交
18
#include "mndAuth.h"
S
Shengliang Guan 已提交
19 20 21
#include "mndDnode.h"
#include "mndShow.h"
#include "mndTrans.h"
S
Shengliang Guan 已提交
22
#include "mndUser.h"
S
Shengliang Guan 已提交
23

24 25
#define BNODE_VER_NUMBER   1
#define BNODE_RESERVE_SIZE 64
S
Shengliang Guan 已提交
26 27 28 29

static SSdbRaw *mndBnodeActionEncode(SBnodeObj *pObj);
static SSdbRow *mndBnodeActionDecode(SSdbRaw *pRaw);
static int32_t  mndBnodeActionInsert(SSdb *pSdb, SBnodeObj *pObj);
S
Shengliang Guan 已提交
30
static int32_t  mndBnodeActionUpdate(SSdb *pSdb, SBnodeObj *pOld, SBnodeObj *pNew);
31
static int32_t  mndBnodeActionDelete(SSdb *pSdb, SBnodeObj *pObj);
S
Shengliang Guan 已提交
32 33
static int32_t  mndProcessCreateBnodeReq(SNodeMsg *pReq);
static int32_t  mndProcessCreateBnodeRsp(SNodeMsg *pRsp);
34
static int32_t  mndProcessDropBnodeReq(SNodeMsg *pReq);
S
Shengliang Guan 已提交
35 36 37
static int32_t  mndProcessDropBnodeRsp(SNodeMsg *pRsp);
static int32_t  mndGetBnodeMeta(SNodeMsg *pReq, SShowObj *pShow, STableMetaRsp *pMeta);
static int32_t  mndRetrieveBnodes(SNodeMsg *pReq, SShowObj *pShow, char *data, int32_t rows);
S
Shengliang Guan 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
static void     mndCancelGetNextBnode(SMnode *pMnode, void *pIter);

int32_t mndInitBnode(SMnode *pMnode) {
  SSdbTable table = {.sdbType = SDB_BNODE,
                     .keyType = SDB_KEY_INT32,
                     .encodeFp = (SdbEncodeFp)mndBnodeActionEncode,
                     .decodeFp = (SdbDecodeFp)mndBnodeActionDecode,
                     .insertFp = (SdbInsertFp)mndBnodeActionInsert,
                     .updateFp = (SdbUpdateFp)mndBnodeActionUpdate,
                     .deleteFp = (SdbDeleteFp)mndBnodeActionDelete};

  mndSetMsgHandle(pMnode, TDMT_MND_CREATE_BNODE, mndProcessCreateBnodeReq);
  mndSetMsgHandle(pMnode, TDMT_MND_DROP_BNODE, mndProcessDropBnodeReq);
  mndSetMsgHandle(pMnode, TDMT_DND_CREATE_BNODE_RSP, mndProcessCreateBnodeRsp);
  mndSetMsgHandle(pMnode, TDMT_DND_DROP_BNODE_RSP, mndProcessDropBnodeRsp);

  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_BNODE, mndRetrieveBnodes);
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_BNODE, mndCancelGetNextBnode);

  return sdbSetTable(pMnode->pSdb, table);
}

void mndCleanupBnode(SMnode *pMnode) {}

62
static SBnodeObj *mndAcquireBnode(SMnode *pMnode, int32_t bnodeId) {
63 64
  SBnodeObj *pObj = sdbAcquire(pMnode->pSdb, SDB_BNODE, &bnodeId);
  if (pObj == NULL && terrno == TSDB_CODE_SDB_OBJ_NOT_THERE) {
S
Shengliang Guan 已提交
65 66 67 68 69 70 71 72 73 74 75
    terrno = TSDB_CODE_MND_BNODE_NOT_EXIST;
  }
  return pObj;
}

static void mndReleaseBnode(SMnode *pMnode, SBnodeObj *pObj) {
  SSdb *pSdb = pMnode->pSdb;
  sdbRelease(pSdb, pObj);
}

static SSdbRaw *mndBnodeActionEncode(SBnodeObj *pObj) {
76 77
  terrno = TSDB_CODE_OUT_OF_MEMORY;

78 79
  SSdbRaw *pRaw = sdbAllocRaw(SDB_BNODE, BNODE_VER_NUMBER, sizeof(SBnodeObj) + BNODE_RESERVE_SIZE);
  if (pRaw == NULL) goto _OVER;
S
Shengliang Guan 已提交
80 81

  int32_t dataPos = 0;
82 83 84 85
  SDB_SET_INT32(pRaw, dataPos, pObj->id, _OVER)
  SDB_SET_INT64(pRaw, dataPos, pObj->createdTime, _OVER)
  SDB_SET_INT64(pRaw, dataPos, pObj->updateTime, _OVER)
  SDB_SET_RESERVE(pRaw, dataPos, BNODE_RESERVE_SIZE, _OVER)
86 87

  terrno = 0;
S
Shengliang Guan 已提交
88

89
_OVER:
90 91 92 93 94 95 96
  if (terrno != 0) {
    mError("bnode:%d, failed to encode to raw:%p since %s", pObj->id, pRaw, terrstr());
    sdbFreeRaw(pRaw);
    return NULL;
  }

  mTrace("bnode:%d, encode to raw:%p, row:%p", pObj->id, pRaw, pObj);
S
Shengliang Guan 已提交
97 98 99 100
  return pRaw;
}

static SSdbRow *mndBnodeActionDecode(SSdbRaw *pRaw) {
101 102
  terrno = TSDB_CODE_OUT_OF_MEMORY;

S
Shengliang Guan 已提交
103
  int8_t sver = 0;
104
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) goto _OVER;
S
Shengliang Guan 已提交
105

106
  if (sver != BNODE_VER_NUMBER) {
S
Shengliang Guan 已提交
107
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
108
    goto _OVER;
S
Shengliang Guan 已提交
109 110
  }

111
  SSdbRow *pRow = sdbAllocRow(sizeof(SBnodeObj));
112
  if (pRow == NULL) goto _OVER;
113

S
Shengliang Guan 已提交
114
  SBnodeObj *pObj = sdbGetRowObj(pRow);
115
  if (pObj == NULL) goto _OVER;
S
Shengliang Guan 已提交
116 117

  int32_t dataPos = 0;
118 119 120 121
  SDB_GET_INT32(pRaw, dataPos, &pObj->id, _OVER)
  SDB_GET_INT64(pRaw, dataPos, &pObj->createdTime, _OVER)
  SDB_GET_INT64(pRaw, dataPos, &pObj->updateTime, _OVER)
  SDB_GET_RESERVE(pRaw, dataPos, BNODE_RESERVE_SIZE, _OVER)
122 123

  terrno = 0;
S
Shengliang Guan 已提交
124

125
_OVER:
126 127
  if (terrno != 0) {
    mError("bnode:%d, failed to decode from raw:%p since %s", pObj->id, pRaw, terrstr());
wafwerar's avatar
wafwerar 已提交
128
    taosMemoryFreeClear(pRow);
129 130 131 132
    return NULL;
  }

  mTrace("bnode:%d, decode from raw:%p, row:%p", pObj->id, pRaw, pObj);
S
Shengliang Guan 已提交
133 134 135 136
  return pRow;
}

static int32_t mndBnodeActionInsert(SSdb *pSdb, SBnodeObj *pObj) {
137
  mTrace("bnode:%d, perform insert action, row:%p", pObj->id, pObj);
S
Shengliang Guan 已提交
138 139 140
  pObj->pDnode = sdbAcquire(pSdb, SDB_DNODE, &pObj->id);
  if (pObj->pDnode == NULL) {
    terrno = TSDB_CODE_MND_DNODE_NOT_EXIST;
141
    mError("bnode:%d, failed to perform insert action since %s", pObj->id, terrstr());
S
Shengliang Guan 已提交
142 143 144 145 146 147 148
    return -1;
  }

  return 0;
}

static int32_t mndBnodeActionDelete(SSdb *pSdb, SBnodeObj *pObj) {
149
  mTrace("bnode:%d, perform delete action, row:%p", pObj->id, pObj);
S
Shengliang Guan 已提交
150 151 152 153 154 155 156 157
  if (pObj->pDnode != NULL) {
    sdbRelease(pSdb, pObj->pDnode);
    pObj->pDnode = NULL;
  }

  return 0;
}

S
Shengliang Guan 已提交
158
static int32_t mndBnodeActionUpdate(SSdb *pSdb, SBnodeObj *pOld, SBnodeObj *pNew) {
S
Shengliang Guan 已提交
159
  mTrace("bnode:%d, perform update action, old row:%p new row:%p", pOld->id, pOld, pNew);
S
Shengliang Guan 已提交
160
  pOld->updateTime = pNew->updateTime;
S
Shengliang Guan 已提交
161 162 163 164 165 166 167 168 169 170 171
  return 0;
}

static int32_t mndSetCreateBnodeRedoLogs(STrans *pTrans, SBnodeObj *pObj) {
  SSdbRaw *pRedoRaw = mndBnodeActionEncode(pObj);
  if (pRedoRaw == NULL) return -1;
  if (mndTransAppendRedolog(pTrans, pRedoRaw) != 0) return -1;
  if (sdbSetRawStatus(pRedoRaw, SDB_STATUS_CREATING) != 0) return -1;
  return 0;
}

172 173 174 175 176 177 178 179
static int32_t mndSetCreateBnodeUndoLogs(STrans *pTrans, SBnodeObj *pObj) {
  SSdbRaw *pUndoRaw = mndBnodeActionEncode(pObj);
  if (pUndoRaw == NULL) return -1;
  if (mndTransAppendUndolog(pTrans, pUndoRaw) != 0) return -1;
  if (sdbSetRawStatus(pUndoRaw, SDB_STATUS_DROPPED) != 0) return -1;
  return 0;
}

S
Shengliang Guan 已提交
180 181 182 183 184 185 186 187 188
static int32_t mndSetCreateBnodeCommitLogs(STrans *pTrans, SBnodeObj *pObj) {
  SSdbRaw *pCommitRaw = mndBnodeActionEncode(pObj);
  if (pCommitRaw == NULL) return -1;
  if (mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) return -1;
  if (sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY) != 0) return -1;
  return 0;
}

static int32_t mndSetCreateBnodeRedoActions(STrans *pTrans, SDnodeObj *pDnode, SBnodeObj *pObj) {
S
Shengliang Guan 已提交
189 190 191
  SDCreateBnodeReq createReq = {0};
  createReq.dnodeId = pDnode->id;

192
  int32_t contLen = tSerializeSCreateDropMQSBNodeReq(NULL, 0, &createReq);
wafwerar's avatar
wafwerar 已提交
193
  void   *pReq = taosMemoryMalloc(contLen);
S
Shengliang Guan 已提交
194
  if (pReq == NULL) {
S
Shengliang Guan 已提交
195 196 197
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
198
  tSerializeSCreateDropMQSBNodeReq(pReq, contLen, &createReq);
S
Shengliang Guan 已提交
199 200 201

  STransAction action = {0};
  action.epSet = mndGetDnodeEpset(pDnode);
S
Shengliang Guan 已提交
202
  action.pCont = pReq;
S
Shengliang Guan 已提交
203
  action.contLen = contLen;
S
Shengliang Guan 已提交
204
  action.msgType = TDMT_DND_CREATE_BNODE;
S
shm  
Shengliang Guan 已提交
205
  action.acceptableCode = TSDB_CODE_NODE_ALREADY_DEPLOYED;
S
Shengliang Guan 已提交
206 207

  if (mndTransAppendRedoAction(pTrans, &action) != 0) {
wafwerar's avatar
wafwerar 已提交
208
    taosMemoryFree(pReq);
S
Shengliang Guan 已提交
209 210 211 212 213 214
    return -1;
  }

  return 0;
}

215
static int32_t mndSetCreateBnodeUndoActions(STrans *pTrans, SDnodeObj *pDnode, SBnodeObj *pObj) {
S
Shengliang Guan 已提交
216 217 218
  SDDropBnodeReq dropReq = {0};
  dropReq.dnodeId = pDnode->id;

219
  int32_t contLen = tSerializeSCreateDropMQSBNodeReq(NULL, 0, &dropReq);
wafwerar's avatar
wafwerar 已提交
220
  void   *pReq = taosMemoryMalloc(contLen);
S
Shengliang Guan 已提交
221
  if (pReq == NULL) {
222 223 224
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
225
  tSerializeSCreateDropMQSBNodeReq(pReq, contLen, &dropReq);
226 227 228

  STransAction action = {0};
  action.epSet = mndGetDnodeEpset(pDnode);
S
Shengliang Guan 已提交
229
  action.pCont = pReq;
S
Shengliang Guan 已提交
230
  action.contLen = contLen;
231
  action.msgType = TDMT_DND_DROP_BNODE;
S
shm  
Shengliang Guan 已提交
232
  action.acceptableCode = TSDB_CODE_NODE_NOT_DEPLOYED;
233 234

  if (mndTransAppendUndoAction(pTrans, &action) != 0) {
wafwerar's avatar
wafwerar 已提交
235
    taosMemoryFree(pReq);
236 237 238 239 240 241
    return -1;
  }

  return 0;
}

S
Shengliang Guan 已提交
242
static int32_t mndCreateBnode(SMnode *pMnode, SNodeMsg *pReq, SDnodeObj *pDnode, SMCreateBnodeReq *pCreate) {
243 244
  int32_t code = -1;

245 246 247 248
  SBnodeObj bnodeObj = {0};
  bnodeObj.id = pDnode->id;
  bnodeObj.createdTime = taosGetTimestampMs();
  bnodeObj.updateTime = bnodeObj.createdTime;
S
Shengliang Guan 已提交
249

S
Shengliang Guan 已提交
250
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_TYPE_CREATE_BNODE, &pReq->rpcMsg);
251
  if (pTrans == NULL) goto _OVER;
S
Shengliang Guan 已提交
252

253
  mDebug("trans:%d, used to create bnode:%d", pTrans->id, pCreate->dnodeId);
254 255 256 257 258 259
  if (mndSetCreateBnodeRedoLogs(pTrans, &bnodeObj) != 0) goto _OVER;
  if (mndSetCreateBnodeUndoLogs(pTrans, &bnodeObj) != 0) goto _OVER;
  if (mndSetCreateBnodeCommitLogs(pTrans, &bnodeObj) != 0) goto _OVER;
  if (mndSetCreateBnodeRedoActions(pTrans, pDnode, &bnodeObj) != 0) goto _OVER;
  if (mndSetCreateBnodeUndoActions(pTrans, pDnode, &bnodeObj) != 0) goto _OVER;
  if (mndTransPrepare(pMnode, pTrans) != 0) goto _OVER;
S
Shengliang Guan 已提交
260 261 262

  code = 0;

263
_OVER:
S
Shengliang Guan 已提交
264 265 266 267
  mndTransDrop(pTrans);
  return code;
}

S
Shengliang Guan 已提交
268 269
static int32_t mndProcessCreateBnodeReq(SNodeMsg *pReq) {
  SMnode          *pMnode = pReq->pNode;
S
Shengliang Guan 已提交
270 271 272 273 274 275
  int32_t          code = -1;
  SBnodeObj       *pObj = NULL;
  SDnodeObj       *pDnode = NULL;
  SUserObj        *pUser = NULL;
  SMCreateBnodeReq createReq = {0};

276
  if (tDeserializeSCreateDropMQSBNodeReq(pReq->rpcMsg.pCont, pReq->rpcMsg.contLen, &createReq) != 0) {
S
Shengliang Guan 已提交
277
    terrno = TSDB_CODE_INVALID_MSG;
278
    goto _OVER;
S
Shengliang Guan 已提交
279
  }
S
Shengliang Guan 已提交
280

S
Shengliang Guan 已提交
281
  mDebug("bnode:%d, start to create", createReq.dnodeId);
S
Shengliang Guan 已提交
282

S
Shengliang Guan 已提交
283
  pObj = mndAcquireBnode(pMnode, createReq.dnodeId);
S
Shengliang Guan 已提交
284
  if (pObj != NULL) {
S
Shengliang Guan 已提交
285
    terrno = TSDB_CODE_MND_BNODE_ALREADY_EXIST;
286
    goto _OVER;
287
  } else if (terrno != TSDB_CODE_MND_BNODE_NOT_EXIST) {
288
    goto _OVER;
S
Shengliang Guan 已提交
289 290
  }

S
Shengliang Guan 已提交
291
  pDnode = mndAcquireDnode(pMnode, createReq.dnodeId);
S
Shengliang Guan 已提交
292 293
  if (pDnode == NULL) {
    terrno = TSDB_CODE_MND_DNODE_NOT_EXIST;
294
    goto _OVER;
S
Shengliang Guan 已提交
295 296
  }

S
Shengliang Guan 已提交
297 298 299
  pUser = mndAcquireUser(pMnode, pReq->user);
  if (pUser == NULL) {
    terrno = TSDB_CODE_MND_NO_USER_FROM_CONN;
300
    goto _OVER;
S
Shengliang Guan 已提交
301
  }
S
Shengliang Guan 已提交
302

S
Shengliang Guan 已提交
303
  if (mndCheckNodeAuth(pUser)) {
304
    goto _OVER;
S
Shengliang Guan 已提交
305 306
  }

S
Shengliang Guan 已提交
307 308 309
  code = mndCreateBnode(pMnode, pReq, pDnode, &createReq);
  if (code == 0) code = TSDB_CODE_MND_ACTION_IN_PROGRESS;

310
_OVER:
S
Shengliang Guan 已提交
311 312 313 314 315 316 317 318
  if (code != 0 && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) {
    mError("bnode:%d, failed to create since %s", createReq.dnodeId, terrstr());
  }

  mndReleaseBnode(pMnode, pObj);
  mndReleaseDnode(pMnode, pDnode);
  mndReleaseUser(pMnode, pUser);
  return code;
S
Shengliang Guan 已提交
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
}

static int32_t mndSetDropBnodeRedoLogs(STrans *pTrans, SBnodeObj *pObj) {
  SSdbRaw *pRedoRaw = mndBnodeActionEncode(pObj);
  if (pRedoRaw == NULL) return -1;
  if (mndTransAppendRedolog(pTrans, pRedoRaw) != 0) return -1;
  if (sdbSetRawStatus(pRedoRaw, SDB_STATUS_DROPPING) != 0) return -1;
  return 0;
}

static int32_t mndSetDropBnodeCommitLogs(STrans *pTrans, SBnodeObj *pObj) {
  SSdbRaw *pCommitRaw = mndBnodeActionEncode(pObj);
  if (pCommitRaw == NULL) return -1;
  if (mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) return -1;
  if (sdbSetRawStatus(pCommitRaw, SDB_STATUS_DROPPED) != 0) return -1;
  return 0;
}

static int32_t mndSetDropBnodeRedoActions(STrans *pTrans, SDnodeObj *pDnode, SBnodeObj *pObj) {
S
Shengliang Guan 已提交
338 339 340
  SDDropBnodeReq dropReq = {0};
  dropReq.dnodeId = pDnode->id;

341
  int32_t contLen = tSerializeSCreateDropMQSBNodeReq(NULL, 0, &dropReq);
wafwerar's avatar
wafwerar 已提交
342
  void   *pReq = taosMemoryMalloc(contLen);
S
Shengliang Guan 已提交
343
  if (pReq == NULL) {
S
Shengliang Guan 已提交
344 345 346
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
347
  tSerializeSCreateDropMQSBNodeReq(pReq, contLen, &dropReq);
S
Shengliang Guan 已提交
348 349 350

  STransAction action = {0};
  action.epSet = mndGetDnodeEpset(pDnode);
S
Shengliang Guan 已提交
351
  action.pCont = pReq;
S
Shengliang Guan 已提交
352
  action.contLen = contLen;
S
Shengliang Guan 已提交
353
  action.msgType = TDMT_DND_DROP_BNODE;
S
shm  
Shengliang Guan 已提交
354
  action.acceptableCode = TSDB_CODE_NODE_NOT_DEPLOYED;
S
Shengliang Guan 已提交
355 356

  if (mndTransAppendRedoAction(pTrans, &action) != 0) {
wafwerar's avatar
wafwerar 已提交
357
    taosMemoryFree(pReq);
S
Shengliang Guan 已提交
358 359 360 361 362 363
    return -1;
  }

  return 0;
}

S
Shengliang Guan 已提交
364
static int32_t mndDropBnode(SMnode *pMnode, SNodeMsg *pReq, SBnodeObj *pObj) {
S
Shengliang Guan 已提交
365
  int32_t code = -1;
366

S
Shengliang Guan 已提交
367
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_TYPE_DROP_BNODE, &pReq->rpcMsg);
368
  if (pTrans == NULL) goto _OVER;
S
Shengliang Guan 已提交
369

370
  mDebug("trans:%d, used to drop bnode:%d", pTrans->id, pObj->id);
371 372 373 374
  if (mndSetDropBnodeRedoLogs(pTrans, pObj) != 0) goto _OVER;
  if (mndSetDropBnodeCommitLogs(pTrans, pObj) != 0) goto _OVER;
  if (mndSetDropBnodeRedoActions(pTrans, pObj->pDnode, pObj) != 0) goto _OVER;
  if (mndTransPrepare(pMnode, pTrans) != 0) goto _OVER;
S
Shengliang Guan 已提交
375 376 377

  code = 0;

378
_OVER:
S
Shengliang Guan 已提交
379 380 381 382
  mndTransDrop(pTrans);
  return code;
}

S
Shengliang Guan 已提交
383 384
static int32_t mndProcessDropBnodeReq(SNodeMsg *pReq) {
  SMnode        *pMnode = pReq->pNode;
S
Shengliang Guan 已提交
385 386 387 388 389
  int32_t        code = -1;
  SUserObj      *pUser = NULL;
  SBnodeObj     *pObj = NULL;
  SMDropBnodeReq dropReq = {0};

390
  if (tDeserializeSCreateDropMQSBNodeReq(pReq->rpcMsg.pCont, pReq->rpcMsg.contLen, &dropReq) != 0) {
S
Shengliang Guan 已提交
391
    terrno = TSDB_CODE_INVALID_MSG;
392
    goto _OVER;
S
Shengliang Guan 已提交
393
  }
S
Shengliang Guan 已提交
394

S
Shengliang Guan 已提交
395
  mDebug("bnode:%d, start to drop", dropReq.dnodeId);
S
Shengliang Guan 已提交
396

S
Shengliang Guan 已提交
397
  if (dropReq.dnodeId <= 0) {
398
    terrno = TSDB_CODE_INVALID_MSG;
399
    goto _OVER;
S
Shengliang Guan 已提交
400 401
  }

S
Shengliang Guan 已提交
402
  pObj = mndAcquireBnode(pMnode, dropReq.dnodeId);
S
Shengliang Guan 已提交
403
  if (pObj == NULL) {
404
    goto _OVER;
S
Shengliang Guan 已提交
405 406
  }

S
Shengliang Guan 已提交
407 408 409
  pUser = mndAcquireUser(pMnode, pReq->user);
  if (pUser == NULL) {
    terrno = TSDB_CODE_MND_NO_USER_FROM_CONN;
410
    goto _OVER;
S
Shengliang Guan 已提交
411 412
  }

S
Shengliang Guan 已提交
413
  if (mndCheckNodeAuth(pUser)) {
414
    goto _OVER;
S
Shengliang Guan 已提交
415 416
  }

S
Shengliang Guan 已提交
417 418 419
  code = mndDropBnode(pMnode, pReq, pObj);
  if (code == 0) code = TSDB_CODE_MND_ACTION_IN_PROGRESS;

420
_OVER:
S
Shengliang Guan 已提交
421 422 423 424 425 426 427 428
  if (code != 0 && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) {
    mError("bnode:%d, failed to drop since %s", dropReq.dnodeId, terrstr());
  }

  mndReleaseBnode(pMnode, pObj);
  mndReleaseUser(pMnode, pUser);

  return code;
S
Shengliang Guan 已提交
429 430
}

S
Shengliang Guan 已提交
431
static int32_t mndProcessCreateBnodeRsp(SNodeMsg *pRsp) {
S
Shengliang Guan 已提交
432
  mndTransProcessRsp(pRsp);
S
Shengliang Guan 已提交
433 434 435
  return 0;
}

S
Shengliang Guan 已提交
436
static int32_t mndProcessDropBnodeRsp(SNodeMsg *pRsp) {
S
Shengliang Guan 已提交
437
  mndTransProcessRsp(pRsp);
S
Shengliang Guan 已提交
438 439 440
  return 0;
}

S
Shengliang Guan 已提交
441 442
static int32_t mndRetrieveBnodes(SNodeMsg *pReq, SShowObj *pShow, char *data, int32_t rows) {
  SMnode    *pMnode = pReq->pNode;
S
Shengliang Guan 已提交
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
  SSdb      *pSdb = pMnode->pSdb;
  int32_t    numOfRows = 0;
  int32_t    cols = 0;
  SBnodeObj *pObj = NULL;
  char      *pWrite;

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

    cols = 0;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
    *(int16_t *)pWrite = pObj->id;
    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
    STR_WITH_MAXSIZE_TO_VARSTR(pWrite, pObj->pDnode->ep, pShow->bytes[cols]);

    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
    *(int64_t *)pWrite = pObj->createdTime;
    cols++;

    numOfRows++;
    sdbRelease(pSdb, pObj);
  }

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

  return numOfRows;
}

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