mndSnode.c 14.4 KB
Newer Older
S
Shengliang Guan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * 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 "mndSnode.h"
#include "mndDnode.h"
H
Hongze Cheng 已提交
19
#include "mndPrivilege.h"
S
Shengliang Guan 已提交
20 21
#include "mndShow.h"
#include "mndTrans.h"
S
Shengliang Guan 已提交
22
#include "mndUser.h"
S
Shengliang Guan 已提交
23

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

static SSdbRaw *mndSnodeActionEncode(SSnodeObj *pObj);
static SSdbRow *mndSnodeActionDecode(SSdbRaw *pRaw);
static int32_t  mndSnodeActionInsert(SSdb *pSdb, SSnodeObj *pObj);
S
Shengliang Guan 已提交
30
static int32_t  mndSnodeActionUpdate(SSdb *pSdb, SSnodeObj *pOld, SSnodeObj *pNew);
31
static int32_t  mndSnodeActionDelete(SSdb *pSdb, SSnodeObj *pObj);
S
Shengliang Guan 已提交
32 33 34
static int32_t  mndProcessCreateSnodeReq(SRpcMsg *pReq);
static int32_t  mndProcessDropSnodeReq(SRpcMsg *pReq);
static int32_t  mndRetrieveSnodes(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
S
Shengliang Guan 已提交
35 36 37
static void     mndCancelGetNextSnode(SMnode *pMnode, void *pIter);

int32_t mndInitSnode(SMnode *pMnode) {
38 39 40 41 42 43 44 45 46
  SSdbTable table = {
      .sdbType = SDB_SNODE,
      .keyType = SDB_KEY_INT32,
      .encodeFp = (SdbEncodeFp)mndSnodeActionEncode,
      .decodeFp = (SdbDecodeFp)mndSnodeActionDecode,
      .insertFp = (SdbInsertFp)mndSnodeActionInsert,
      .updateFp = (SdbUpdateFp)mndSnodeActionUpdate,
      .deleteFp = (SdbDeleteFp)mndSnodeActionDelete,
  };
S
Shengliang Guan 已提交
47 48 49

  mndSetMsgHandle(pMnode, TDMT_MND_CREATE_SNODE, mndProcessCreateSnodeReq);
  mndSetMsgHandle(pMnode, TDMT_MND_DROP_SNODE, mndProcessDropSnodeReq);
50 51
  mndSetMsgHandle(pMnode, TDMT_DND_CREATE_SNODE_RSP, mndTransProcessRsp);
  mndSetMsgHandle(pMnode, TDMT_DND_DROP_SNODE_RSP, mndTransProcessRsp);
S
Shengliang Guan 已提交
52 53 54 55 56 57 58 59 60

  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_SNODE, mndRetrieveSnodes);
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_SNODE, mndCancelGetNextSnode);

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

void mndCleanupSnode(SMnode *pMnode) {}

L
Liu Jicong 已提交
61
SEpSet mndAcquireEpFromSnode(SMnode *pMnode, const SSnodeObj *pSnode) {
62 63 64
  SEpSet epSet = {.numOfEps = 1, .inUse = 0};
  memcpy(epSet.eps[0].fqdn, pSnode->pDnode->fqdn, TSDB_FQDN_LEN);
  epSet.eps[0].port = pSnode->pDnode->port;
L
Liu Jicong 已提交
65 66 67
  return epSet;
}

68
SSnodeObj *mndAcquireSnode(SMnode *pMnode, int32_t snodeId) {
69 70
  SSnodeObj *pObj = sdbAcquire(pMnode->pSdb, SDB_SNODE, &snodeId);
  if (pObj == NULL && terrno == TSDB_CODE_SDB_OBJ_NOT_THERE) {
S
Shengliang Guan 已提交
71 72 73 74 75
    terrno = TSDB_CODE_MND_SNODE_NOT_EXIST;
  }
  return pObj;
}

76
void mndReleaseSnode(SMnode *pMnode, SSnodeObj *pObj) {
S
Shengliang Guan 已提交
77 78 79 80 81
  SSdb *pSdb = pMnode->pSdb;
  sdbRelease(pSdb, pObj);
}

static SSdbRaw *mndSnodeActionEncode(SSnodeObj *pObj) {
82 83
  terrno = TSDB_CODE_OUT_OF_MEMORY;

84 85
  SSdbRaw *pRaw = sdbAllocRaw(SDB_SNODE, SNODE_VER_NUMBER, sizeof(SSnodeObj) + SNODE_RESERVE_SIZE);
  if (pRaw == NULL) goto _OVER;
S
Shengliang Guan 已提交
86 87

  int32_t dataPos = 0;
88 89 90 91
  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, SNODE_RESERVE_SIZE, _OVER)
92 93

  terrno = 0;
S
Shengliang Guan 已提交
94

95
_OVER:
96 97 98 99 100 101 102
  if (terrno != 0) {
    mError("snode:%d, failed to encode to raw:%p since %s", pObj->id, pRaw, terrstr());
    sdbFreeRaw(pRaw);
    return NULL;
  }

  mTrace("snode:%d, encode to raw:%p, row:%p", pObj->id, pRaw, pObj);
S
Shengliang Guan 已提交
103 104 105 106
  return pRaw;
}

static SSdbRow *mndSnodeActionDecode(SSdbRaw *pRaw) {
107 108
  terrno = TSDB_CODE_OUT_OF_MEMORY;

S
Shengliang Guan 已提交
109
  int8_t sver = 0;
110
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) goto _OVER;
S
Shengliang Guan 已提交
111

112
  if (sver != SNODE_VER_NUMBER) {
S
Shengliang Guan 已提交
113
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
114
    goto _OVER;
S
Shengliang Guan 已提交
115 116
  }

117
  SSdbRow *pRow = sdbAllocRow(sizeof(SSnodeObj));
118
  if (pRow == NULL) goto _OVER;
119

S
Shengliang Guan 已提交
120
  SSnodeObj *pObj = sdbGetRowObj(pRow);
121
  if (pObj == NULL) goto _OVER;
S
Shengliang Guan 已提交
122 123

  int32_t dataPos = 0;
124 125 126 127
  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, SNODE_RESERVE_SIZE, _OVER)
128 129

  terrno = 0;
S
Shengliang Guan 已提交
130

131
_OVER:
132 133
  if (terrno != 0) {
    mError("snode:%d, failed to decode from raw:%p since %s", pObj->id, pRaw, terrstr());
wafwerar's avatar
wafwerar 已提交
134
    taosMemoryFreeClear(pRow);
135 136 137 138
    return NULL;
  }

  mTrace("snode:%d, decode from raw:%p, row:%p", pObj->id, pRaw, pObj);
S
Shengliang Guan 已提交
139 140 141 142
  return pRow;
}

static int32_t mndSnodeActionInsert(SSdb *pSdb, SSnodeObj *pObj) {
143
  mTrace("snode:%d, perform insert action, row:%p", pObj->id, pObj);
S
Shengliang Guan 已提交
144 145 146 147 148 149 150 151 152 153 154
  pObj->pDnode = sdbAcquire(pSdb, SDB_DNODE, &pObj->id);
  if (pObj->pDnode == NULL) {
    terrno = TSDB_CODE_MND_DNODE_NOT_EXIST;
    mError("snode:%d, failed to perform insert action since %s", pObj->id, terrstr());
    return -1;
  }

  return 0;
}

static int32_t mndSnodeActionDelete(SSdb *pSdb, SSnodeObj *pObj) {
155
  mTrace("snode:%d, perform delete action, row:%p", pObj->id, pObj);
S
Shengliang Guan 已提交
156 157 158 159 160 161 162 163
  if (pObj->pDnode != NULL) {
    sdbRelease(pSdb, pObj->pDnode);
    pObj->pDnode = NULL;
  }

  return 0;
}

S
Shengliang Guan 已提交
164
static int32_t mndSnodeActionUpdate(SSdb *pSdb, SSnodeObj *pOld, SSnodeObj *pNew) {
S
Shengliang Guan 已提交
165
  mTrace("snode:%d, perform update action, old row:%p new row:%p", pOld->id, pOld, pNew);
S
Shengliang Guan 已提交
166
  pOld->updateTime = pNew->updateTime;
S
Shengliang Guan 已提交
167 168 169 170 171 172 173 174 175 176 177
  return 0;
}

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

178 179 180 181 182 183 184 185
static int32_t mndSetCreateSnodeUndoLogs(STrans *pTrans, SSnodeObj *pObj) {
  SSdbRaw *pUndoRaw = mndSnodeActionEncode(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 已提交
186 187 188 189 190 191 192 193 194
static int32_t mndSetCreateSnodeCommitLogs(STrans *pTrans, SSnodeObj *pObj) {
  SSdbRaw *pCommitRaw = mndSnodeActionEncode(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 mndSetCreateSnodeRedoActions(STrans *pTrans, SDnodeObj *pDnode, SSnodeObj *pObj) {
S
Shengliang Guan 已提交
195 196 197
  SDCreateSnodeReq createReq = {0};
  createReq.dnodeId = pDnode->id;

S
Shengliang Guan 已提交
198
  int32_t contLen = tSerializeSCreateDropMQSNodeReq(NULL, 0, &createReq);
wafwerar's avatar
wafwerar 已提交
199
  void   *pReq = taosMemoryMalloc(contLen);
S
Shengliang Guan 已提交
200
  if (pReq == NULL) {
S
Shengliang Guan 已提交
201 202 203
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
S
Shengliang Guan 已提交
204
  tSerializeSCreateDropMQSNodeReq(pReq, contLen, &createReq);
S
Shengliang Guan 已提交
205 206 207

  STransAction action = {0};
  action.epSet = mndGetDnodeEpset(pDnode);
S
Shengliang Guan 已提交
208
  action.pCont = pReq;
S
Shengliang Guan 已提交
209
  action.contLen = contLen;
S
Shengliang Guan 已提交
210
  action.msgType = TDMT_DND_CREATE_SNODE;
S
shm  
Shengliang Guan 已提交
211
  action.acceptableCode = TSDB_CODE_NODE_ALREADY_DEPLOYED;
S
Shengliang Guan 已提交
212 213

  if (mndTransAppendRedoAction(pTrans, &action) != 0) {
wafwerar's avatar
wafwerar 已提交
214
    taosMemoryFree(pReq);
S
Shengliang Guan 已提交
215 216 217 218 219 220
    return -1;
  }

  return 0;
}

221
static int32_t mndSetCreateSnodeUndoActions(STrans *pTrans, SDnodeObj *pDnode, SSnodeObj *pObj) {
S
Shengliang Guan 已提交
222 223 224
  SDDropSnodeReq dropReq = {0};
  dropReq.dnodeId = pDnode->id;

S
Shengliang Guan 已提交
225
  int32_t contLen = tSerializeSCreateDropMQSNodeReq(NULL, 0, &dropReq);
wafwerar's avatar
wafwerar 已提交
226
  void   *pReq = taosMemoryMalloc(contLen);
S
Shengliang Guan 已提交
227
  if (pReq == NULL) {
228 229 230
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
S
Shengliang Guan 已提交
231
  tSerializeSCreateDropMQSNodeReq(pReq, contLen, &dropReq);
232 233 234

  STransAction action = {0};
  action.epSet = mndGetDnodeEpset(pDnode);
S
Shengliang Guan 已提交
235
  action.pCont = pReq;
S
Shengliang Guan 已提交
236
  action.contLen = contLen;
237
  action.msgType = TDMT_DND_DROP_SNODE;
S
shm  
Shengliang Guan 已提交
238
  action.acceptableCode = TSDB_CODE_NODE_NOT_DEPLOYED;
239 240

  if (mndTransAppendUndoAction(pTrans, &action) != 0) {
wafwerar's avatar
wafwerar 已提交
241
    taosMemoryFree(pReq);
242 243 244 245 246 247
    return -1;
  }

  return 0;
}

S
Shengliang Guan 已提交
248
static int32_t mndCreateSnode(SMnode *pMnode, SRpcMsg *pReq, SDnodeObj *pDnode, SMCreateSnodeReq *pCreate) {
249 250
  int32_t code = -1;

S
Shengliang Guan 已提交
251 252 253 254 255
  SSnodeObj snodeObj = {0};
  snodeObj.id = pDnode->id;
  snodeObj.createdTime = taosGetTimestampMs();
  snodeObj.updateTime = snodeObj.createdTime;

256
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_NOTHING, pReq, "create-snode");
257
  if (pTrans == NULL) goto _OVER;
S
Shengliang Guan 已提交
258

259
  mInfo("trans:%d, used to create snode:%d", pTrans->id, pCreate->dnodeId);
S
Shengliang Guan 已提交
260

261 262 263 264 265 266
  if (mndSetCreateSnodeRedoLogs(pTrans, &snodeObj) != 0) goto _OVER;
  if (mndSetCreateSnodeUndoLogs(pTrans, &snodeObj) != 0) goto _OVER;
  if (mndSetCreateSnodeCommitLogs(pTrans, &snodeObj) != 0) goto _OVER;
  if (mndSetCreateSnodeRedoActions(pTrans, pDnode, &snodeObj) != 0) goto _OVER;
  if (mndSetCreateSnodeUndoActions(pTrans, pDnode, &snodeObj) != 0) goto _OVER;
  if (mndTransPrepare(pMnode, pTrans) != 0) goto _OVER;
S
Shengliang Guan 已提交
267 268 269

  code = 0;

270
_OVER:
S
Shengliang Guan 已提交
271 272 273 274
  mndTransDrop(pTrans);
  return code;
}

S
Shengliang Guan 已提交
275
static int32_t mndProcessCreateSnodeReq(SRpcMsg *pReq) {
276 277 278
#if 1
  return TSDB_CODE_OPS_NOT_SUPPORT;
#else
S
Shengliang Guan 已提交
279
  SMnode          *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
280 281 282 283 284
  int32_t          code = -1;
  SSnodeObj       *pObj = NULL;
  SDnodeObj       *pDnode = NULL;
  SMCreateSnodeReq createReq = {0};

S
Shengliang Guan 已提交
285
  if (tDeserializeSCreateDropMQSNodeReq(pReq->pCont, pReq->contLen, &createReq) != 0) {
S
Shengliang Guan 已提交
286
    terrno = TSDB_CODE_INVALID_MSG;
287
    goto _OVER;
S
Shengliang Guan 已提交
288
  }
S
Shengliang Guan 已提交
289

290
  mInfo("snode:%d, start to create", createReq.dnodeId);
291
  if (mndCheckOperPrivilege(pMnode, pReq->info.conn.user, MND_OPER_CREATE_SNODE) != 0) {
S
Shengliang Guan 已提交
292 293
    goto _OVER;
  }
S
Shengliang Guan 已提交
294

S
Shengliang Guan 已提交
295
  pObj = mndAcquireSnode(pMnode, createReq.dnodeId);
S
Shengliang Guan 已提交
296
  if (pObj != NULL) {
S
Shengliang Guan 已提交
297
    terrno = TSDB_CODE_MND_SNODE_ALREADY_EXIST;
298
    goto _OVER;
299
  } else if (terrno != TSDB_CODE_MND_SNODE_NOT_EXIST) {
300
    goto _OVER;
S
Shengliang Guan 已提交
301 302
  }

S
Shengliang Guan 已提交
303
  pDnode = mndAcquireDnode(pMnode, createReq.dnodeId);
S
Shengliang Guan 已提交
304 305
  if (pDnode == NULL) {
    terrno = TSDB_CODE_MND_DNODE_NOT_EXIST;
306
    goto _OVER;
S
Shengliang Guan 已提交
307 308
  }

S
Shengliang Guan 已提交
309
  code = mndCreateSnode(pMnode, pReq, pDnode, &createReq);
S
Shengliang Guan 已提交
310
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
S
Shengliang Guan 已提交
311

312
_OVER:
S
Shengliang Guan 已提交
313
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
314
    mError("snode:%d, failed to create since %s", createReq.dnodeId, terrstr());
S
Shengliang Guan 已提交
315 316 317
    return -1;
  }

S
Shengliang Guan 已提交
318 319 320
  mndReleaseSnode(pMnode, pObj);
  mndReleaseDnode(pMnode, pDnode);
  return code;
321
#endif
S
Shengliang Guan 已提交
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
}

static int32_t mndSetDropSnodeRedoLogs(STrans *pTrans, SSnodeObj *pObj) {
  SSdbRaw *pRedoRaw = mndSnodeActionEncode(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 mndSetDropSnodeCommitLogs(STrans *pTrans, SSnodeObj *pObj) {
  SSdbRaw *pCommitRaw = mndSnodeActionEncode(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 mndSetDropSnodeRedoActions(STrans *pTrans, SDnodeObj *pDnode, SSnodeObj *pObj) {
S
Shengliang Guan 已提交
341 342 343
  SDDropSnodeReq dropReq = {0};
  dropReq.dnodeId = pDnode->id;

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

  STransAction action = {0};
  action.epSet = mndGetDnodeEpset(pDnode);
S
Shengliang Guan 已提交
354
  action.pCont = pReq;
S
Shengliang Guan 已提交
355
  action.contLen = contLen;
S
Shengliang Guan 已提交
356
  action.msgType = TDMT_DND_DROP_SNODE;
S
shm  
Shengliang Guan 已提交
357
  action.acceptableCode = TSDB_CODE_NODE_NOT_DEPLOYED;
S
Shengliang Guan 已提交
358 359

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

  return 0;
}

367 368 369 370 371 372 373 374
int32_t mndSetDropSnodeInfoToTrans(SMnode *pMnode, STrans *pTrans, SSnodeObj *pObj) {
  if (pObj == NULL) return 0;
  if (mndSetDropSnodeRedoLogs(pTrans, pObj) != 0) return -1;
  if (mndSetDropSnodeCommitLogs(pTrans, pObj) != 0) return -1;
  if (mndSetDropSnodeRedoActions(pTrans, pObj->pDnode, pObj) != 0) return -1;
  return 0;
}

S
Shengliang Guan 已提交
375
static int32_t mndDropSnode(SMnode *pMnode, SRpcMsg *pReq, SSnodeObj *pObj) {
S
Shengliang Guan 已提交
376
  int32_t code = -1;
377

378
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_NOTHING, pReq, "drop-snode");
379
  if (pTrans == NULL) goto _OVER;
S
Shengliang Guan 已提交
380

381
  mInfo("trans:%d, used to drop snode:%d", pTrans->id, pObj->id);
382
  if (mndSetDropSnodeInfoToTrans(pMnode, pTrans, pObj) != 0) goto _OVER;
383
  if (mndTransPrepare(pMnode, pTrans) != 0) goto _OVER;
S
Shengliang Guan 已提交
384 385 386

  code = 0;

387
_OVER:
S
Shengliang Guan 已提交
388 389 390 391
  mndTransDrop(pTrans);
  return code;
}

S
Shengliang Guan 已提交
392
static int32_t mndProcessDropSnodeReq(SRpcMsg *pReq) {
393 394 395 396 397 398
#if 1
  return TSDB_CODE_OPS_NOT_SUPPORT;
#else
  SMnode *pMnode = pReq->info.node;
  int32_t code = -1;
  SSnodeObj *pObj = NULL;
S
Shengliang Guan 已提交
399 400
  SMDropSnodeReq dropReq = {0};

S
Shengliang Guan 已提交
401
  if (tDeserializeSCreateDropMQSNodeReq(pReq->pCont, pReq->contLen, &dropReq) != 0) {
S
Shengliang Guan 已提交
402
    terrno = TSDB_CODE_INVALID_MSG;
403
    goto _OVER;
S
Shengliang Guan 已提交
404
  }
S
Shengliang Guan 已提交
405

406
  mInfo("snode:%d, start to drop", dropReq.dnodeId);
407
  if (mndCheckOperPrivilege(pMnode, pReq->info.conn.user, MND_OPER_DROP_SNODE) != 0) {
S
Shengliang Guan 已提交
408 409
    goto _OVER;
  }
S
Shengliang Guan 已提交
410

S
Shengliang Guan 已提交
411
  if (dropReq.dnodeId <= 0) {
412
    terrno = TSDB_CODE_INVALID_MSG;
413
    goto _OVER;
S
Shengliang Guan 已提交
414 415
  }

S
Shengliang Guan 已提交
416
  pObj = mndAcquireSnode(pMnode, dropReq.dnodeId);
S
Shengliang Guan 已提交
417
  if (pObj == NULL) {
418
    goto _OVER;
S
Shengliang Guan 已提交
419 420
  }

421
  // check deletable
S
Shengliang Guan 已提交
422
  code = mndDropSnode(pMnode, pReq, pObj);
S
Shengliang Guan 已提交
423
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
S
Shengliang Guan 已提交
424

425
_OVER:
S
Shengliang Guan 已提交
426
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
S
shm  
Shengliang Guan 已提交
427
    mError("snode:%d, failed to drop since %s", dropReq.dnodeId, terrstr());
S
Shengliang Guan 已提交
428 429
  }

S
Shengliang Guan 已提交
430 431
  mndReleaseSnode(pMnode, pObj);
  return code;
432
#endif
S
Shengliang Guan 已提交
433 434
}

S
Shengliang Guan 已提交
435 436
static int32_t mndRetrieveSnodes(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
  SMnode    *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
437 438 439 440 441 442 443 444 445 446
  SSdb      *pSdb = pMnode->pSdb;
  int32_t    numOfRows = 0;
  int32_t    cols = 0;
  SSnodeObj *pObj = NULL;

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

    cols = 0;
S
Shengliang Guan 已提交
447 448
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataAppend(pColInfo, numOfRows, (const char *)&pObj->id, false);
S
Shengliang Guan 已提交
449

450
    char ep[TSDB_EP_LEN + VARSTR_HEADER_SIZE] = {0};
451
    STR_WITH_MAXSIZE_TO_VARSTR(ep, pObj->pDnode->ep, pShow->pMeta->pSchemas[cols].bytes);
S
Shengliang Guan 已提交
452

453 454
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataAppend(pColInfo, numOfRows, (const char *)ep, false);
S
Shengliang Guan 已提交
455

456 457
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataAppend(pColInfo, numOfRows, (const char *)&pObj->createdTime, false);
S
Shengliang Guan 已提交
458 459 460 461 462

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

463
  pShow->numOfRows += numOfRows;
S
Shengliang Guan 已提交
464 465 466 467 468 469 470 471

  return numOfRows;
}

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