mndQnode.c 16.3 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 "mndQnode.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 QNODE_VER_NUMBER   1
#define QNODE_RESERVE_SIZE 64
S
Shengliang Guan 已提交
26 27 28 29

static SSdbRaw *mndQnodeActionEncode(SQnodeObj *pObj);
static SSdbRow *mndQnodeActionDecode(SSdbRaw *pRaw);
static int32_t  mndQnodeActionInsert(SSdb *pSdb, SQnodeObj *pObj);
S
Shengliang Guan 已提交
30
static int32_t  mndQnodeActionUpdate(SSdb *pSdb, SQnodeObj *pOld, SQnodeObj *pNew);
31
static int32_t  mndQnodeActionDelete(SSdb *pSdb, SQnodeObj *pObj);
S
Shengliang Guan 已提交
32 33
static int32_t  mndProcessCreateQnodeReq(SNodeMsg *pReq);
static int32_t  mndProcessCreateQnodeRsp(SNodeMsg *pRsp);
34
static int32_t  mndProcessDropQnodeReq(SNodeMsg *pReq);
S
Shengliang Guan 已提交
35
static int32_t  mndProcessDropQnodeRsp(SNodeMsg *pRsp);
36
static int32_t  mndProcessQnodeListReq(SNodeMsg *pReq);
S
Shengliang Guan 已提交
37
static int32_t  mndRetrieveQnodes(SNodeMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
S
Shengliang Guan 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
static void     mndCancelGetNextQnode(SMnode *pMnode, void *pIter);

int32_t mndInitQnode(SMnode *pMnode) {
  SSdbTable table = {.sdbType = SDB_QNODE,
                     .keyType = SDB_KEY_INT32,
                     .encodeFp = (SdbEncodeFp)mndQnodeActionEncode,
                     .decodeFp = (SdbDecodeFp)mndQnodeActionDecode,
                     .insertFp = (SdbInsertFp)mndQnodeActionInsert,
                     .updateFp = (SdbUpdateFp)mndQnodeActionUpdate,
                     .deleteFp = (SdbDeleteFp)mndQnodeActionDelete};

  mndSetMsgHandle(pMnode, TDMT_MND_CREATE_QNODE, mndProcessCreateQnodeReq);
  mndSetMsgHandle(pMnode, TDMT_MND_DROP_QNODE, mndProcessDropQnodeReq);
  mndSetMsgHandle(pMnode, TDMT_DND_CREATE_QNODE_RSP, mndProcessCreateQnodeRsp);
  mndSetMsgHandle(pMnode, TDMT_DND_DROP_QNODE_RSP, mndProcessDropQnodeRsp);
53
  mndSetMsgHandle(pMnode, TDMT_MND_QNODE_LIST, mndProcessQnodeListReq);
S
Shengliang Guan 已提交
54 55 56 57 58 59 60 61 62 63

  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_QNODE, mndRetrieveQnodes);
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_QNODE, mndCancelGetNextQnode);

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

void mndCleanupQnode(SMnode *pMnode) {}

static SQnodeObj *mndAcquireQnode(SMnode *pMnode, int32_t qnodeId) {
64 65
  SQnodeObj *pObj = sdbAcquire(pMnode->pSdb, SDB_QNODE, &qnodeId);
  if (pObj == NULL && terrno == TSDB_CODE_SDB_OBJ_NOT_THERE) {
S
Shengliang Guan 已提交
66 67 68 69 70 71 72 73 74 75 76
    terrno = TSDB_CODE_MND_QNODE_NOT_EXIST;
  }
  return pObj;
}

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

static SSdbRaw *mndQnodeActionEncode(SQnodeObj *pObj) {
77 78
  terrno = TSDB_CODE_OUT_OF_MEMORY;

79 80
  SSdbRaw *pRaw = sdbAllocRaw(SDB_QNODE, QNODE_VER_NUMBER, sizeof(SQnodeObj) + QNODE_RESERVE_SIZE);
  if (pRaw == NULL) goto _OVER;
S
Shengliang Guan 已提交
81 82

  int32_t dataPos = 0;
83 84 85 86
  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, QNODE_RESERVE_SIZE, _OVER)
87 88

  terrno = 0;
S
Shengliang Guan 已提交
89

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

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

static SSdbRow *mndQnodeActionDecode(SSdbRaw *pRaw) {
102 103
  terrno = TSDB_CODE_OUT_OF_MEMORY;

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

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

112
  SSdbRow *pRow = sdbAllocRow(sizeof(SQnodeObj));
113
  if (pRow == NULL) goto _OVER;
114

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

  int32_t dataPos = 0;
119 120 121 122
  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, QNODE_RESERVE_SIZE, _OVER)
123 124

  terrno = 0;
S
Shengliang Guan 已提交
125

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

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

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

  return 0;
}

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

  return 0;
}

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

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

173 174 175 176 177 178 179 180
static int32_t mndSetCreateQnodeUndoLogs(STrans *pTrans, SQnodeObj *pObj) {
  SSdbRaw *pUndoRaw = mndQnodeActionEncode(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 已提交
181 182 183 184 185 186 187 188 189
static int32_t mndSetCreateQnodeCommitLogs(STrans *pTrans, SQnodeObj *pObj) {
  SSdbRaw *pCommitRaw = mndQnodeActionEncode(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 mndSetCreateQnodeRedoActions(STrans *pTrans, SDnodeObj *pDnode, SQnodeObj *pObj) {
S
Shengliang Guan 已提交
190 191 192
  SDCreateQnodeReq createReq = {0};
  createReq.dnodeId = pDnode->id;

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

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

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

  return 0;
}

216
static int32_t mndSetCreateQnodeUndoActions(STrans *pTrans, SDnodeObj *pDnode, SQnodeObj *pObj) {
S
Shengliang Guan 已提交
217 218 219
  SDDropQnodeReq dropReq = {0};
  dropReq.dnodeId = pDnode->id;

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

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

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

  return 0;
}

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

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

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

254
  mDebug("trans:%d, used to create qnode:%d", pTrans->id, pCreate->dnodeId);
255 256 257 258 259 260
  if (mndSetCreateQnodeRedoLogs(pTrans, &qnodeObj) != 0) goto _OVER;
  if (mndSetCreateQnodeUndoLogs(pTrans, &qnodeObj) != 0) goto _OVER;
  if (mndSetCreateQnodeCommitLogs(pTrans, &qnodeObj) != 0) goto _OVER;
  if (mndSetCreateQnodeRedoActions(pTrans, pDnode, &qnodeObj) != 0) goto _OVER;
  if (mndSetCreateQnodeUndoActions(pTrans, pDnode, &qnodeObj) != 0) goto _OVER;
  if (mndTransPrepare(pMnode, pTrans) != 0) goto _OVER;
S
Shengliang Guan 已提交
261 262 263

  code = 0;

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

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

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

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

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

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

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

S
Shengliang Guan 已提交
304
  if (mndCheckNodeAuth(pUser)) {
305
    goto _OVER;
S
Shengliang Guan 已提交
306 307 308 309 310
  }

  code = mndCreateQnode(pMnode, pReq, pDnode, &createReq);
  if (code == 0) code = TSDB_CODE_MND_ACTION_IN_PROGRESS;

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

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

static int32_t mndSetDropQnodeRedoLogs(STrans *pTrans, SQnodeObj *pObj) {
  SSdbRaw *pRedoRaw = mndQnodeActionEncode(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 mndSetDropQnodeCommitLogs(STrans *pTrans, SQnodeObj *pObj) {
  SSdbRaw *pCommitRaw = mndQnodeActionEncode(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 mndSetDropQnodeRedoActions(STrans *pTrans, SDnodeObj *pDnode, SQnodeObj *pObj) {
S
Shengliang Guan 已提交
339 340 341
  SDDropQnodeReq dropReq = {0};
  dropReq.dnodeId = pDnode->id;

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

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

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

  return 0;
}

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

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

  mDebug("trans:%d, used to drop qnode:%d", pTrans->id, pObj->id);
372 373 374 375
  if (mndSetDropQnodeRedoLogs(pTrans, pObj) != 0) goto _OVER;
  if (mndSetDropQnodeCommitLogs(pTrans, pObj) != 0) goto _OVER;
  if (mndSetDropQnodeRedoActions(pTrans, pObj->pDnode, pObj) != 0) goto _OVER;
  if (mndTransPrepare(pMnode, pTrans) != 0) goto _OVER;
S
Shengliang Guan 已提交
376 377 378

  code = 0;

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

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

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

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

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

S
Shengliang Guan 已提交
403
  pObj = mndAcquireQnode(pMnode, dropReq.dnodeId);
S
Shengliang Guan 已提交
404
  if (pObj == NULL) {
405
    goto _OVER;
S
Shengliang Guan 已提交
406 407 408 409 410
  }

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

S
Shengliang Guan 已提交
414
  if (mndCheckNodeAuth(pUser)) {
415
    goto _OVER;
S
Shengliang Guan 已提交
416 417 418 419 420
  }

  code = mndDropQnode(pMnode, pReq, pObj);
  if (code == 0) code = TSDB_CODE_MND_ACTION_IN_PROGRESS;

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

S
Shengliang Guan 已提交
426 427 428 429
  mndReleaseQnode(pMnode, pObj);
  mndReleaseUser(pMnode, pUser);

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

S
Shengliang Guan 已提交
432
static int32_t mndProcessQnodeListReq(SNodeMsg *pReq) {
433 434 435 436 437 438
  int32_t       code = -1;
  int32_t       numOfRows = 0;
  SMnode       *pMnode = pReq->pNode;
  SSdb         *pSdb = pMnode->pSdb;
  SQnodeObj    *pObj = NULL;
  SQnodeListReq qlistReq = {0};
D
dapan1121 已提交
439 440 441
  SQnodeListRsp qlistRsp = {0};

  if (tDeserializeSQnodeListReq(pReq->rpcMsg.pCont, pReq->rpcMsg.contLen, &qlistReq) != 0) {
442
    mError("failed to parse qnode list req");
D
dapan1121 已提交
443
    terrno = TSDB_CODE_INVALID_MSG;
444
    goto _OVER;
D
dapan1121 已提交
445 446
  }

D
dapan1121 已提交
447 448
  qlistRsp.addrsList = taosArrayInit(5, sizeof(SQueryNodeAddr));
  if (NULL == qlistRsp.addrsList) {
449
    mError("failed to alloc epSet while process qnode list req");
D
dapan1121 已提交
450
    terrno = TSDB_CODE_OUT_OF_MEMORY;
451
    goto _OVER;
D
dapan1121 已提交
452
  }
453

D
dapan 已提交
454
  void *pIter = NULL;
455
  while (1) {
D
dapan 已提交
456
    pIter = sdbFetch(pSdb, SDB_QNODE, pIter, (void **)&pObj);
D
dapan1121 已提交
457 458
    if (pIter == NULL) break;

D
dapan1121 已提交
459 460 461 462 463
    SQueryNodeAddr nodeAddr = {0};
    nodeAddr.nodeId = QNODE_HANDLE;
    nodeAddr.epSet.numOfEps = 1;
    tstrncpy(nodeAddr.epSet.eps[0].fqdn, pObj->pDnode->fqdn, TSDB_FQDN_LEN);
    nodeAddr.epSet.eps[0].port = pObj->pDnode->port;
D
dapan1121 已提交
464

D
dapan1121 已提交
465
    (void)taosArrayPush(qlistRsp.addrsList, &nodeAddr);
D
dapan1121 已提交
466 467 468 469

    numOfRows++;
    sdbRelease(pSdb, pObj);

D
dapan1121 已提交
470
    if (qlistReq.rowNum > 0 && numOfRows >= qlistReq.rowNum) {
D
dapan1121 已提交
471 472 473 474 475
      break;
    }
  }

  int32_t rspLen = tSerializeSQnodeListRsp(NULL, 0, &qlistRsp);
D
dapan 已提交
476
  void   *pRsp = rpcMallocCont(rspLen);
D
dapan1121 已提交
477 478
  if (pRsp == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
479
    goto _OVER;
D
dapan1121 已提交
480
  }
481

D
dapan1121 已提交
482
  tSerializeSQnodeListRsp(pRsp, rspLen, &qlistRsp);
483

S
Shengliang Guan 已提交
484 485
  pReq->rspLen = rspLen;
  pReq->pRsp = pRsp;
D
dapan1121 已提交
486 487
  code = 0;

488
_OVER:
D
dapan1121 已提交
489 490 491 492
  tFreeSQnodeListRsp(&qlistRsp);
  return code;
}

S
Shengliang Guan 已提交
493
static int32_t mndProcessCreateQnodeRsp(SNodeMsg *pRsp) {
S
Shengliang Guan 已提交
494
  mndTransProcessRsp(pRsp);
S
Shengliang Guan 已提交
495 496 497
  return 0;
}

S
Shengliang Guan 已提交
498
static int32_t mndProcessDropQnodeRsp(SNodeMsg *pRsp) {
S
Shengliang Guan 已提交
499
  mndTransProcessRsp(pRsp);
S
Shengliang Guan 已提交
500 501 502
  return 0;
}

S
Shengliang Guan 已提交
503
static int32_t mndRetrieveQnodes(SNodeMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
S
Shengliang Guan 已提交
504
  SMnode    *pMnode = pReq->pNode;
S
Shengliang Guan 已提交
505 506 507 508 509 510 511 512 513 514 515
  SSdb      *pSdb = pMnode->pSdb;
  int32_t    numOfRows = 0;
  int32_t    cols = 0;
  SQnodeObj *pObj = NULL;
  char      *pWrite;

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

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

519 520 521 522
    char ep[TSDB_EP_LEN + VARSTR_HEADER_SIZE] = {0};
    STR_WITH_MAXSIZE_TO_VARSTR(ep, pObj->pDnode->ep, pShow->bytes[cols]);
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataAppend(pColInfo, numOfRows, (const char *)ep, false);
S
Shengliang Guan 已提交
523

524 525
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataAppend(pColInfo, numOfRows, (const char *)&pObj->createdTime, false);
S
Shengliang Guan 已提交
526 527 528 529 530

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

531
  pShow->numOfRows += numOfRows;
S
Shengliang Guan 已提交
532 533 534 535 536 537 538 539

  return numOfRows;
}

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