mndQnode.c 17.8 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 26 27 28 29 30

#define TSDB_QNODE_VER_NUMBER 1
#define TSDB_QNODE_RESERVE_SIZE 64

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

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);
D
dapan1121 已提交
52
  mndSetMsgHandle(pMnode, TDMT_MND_QNODE_LIST, mndProcessQnodeListReq);
S
Shengliang Guan 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65
  mndSetMsgHandle(pMnode, TDMT_DND_CREATE_QNODE_RSP, mndProcessCreateQnodeRsp);
  mndSetMsgHandle(pMnode, TDMT_DND_DROP_QNODE_RSP, mndProcessDropQnodeRsp);

  mndAddShowMetaHandle(pMnode, TSDB_MGMT_TABLE_QNODE, mndGetQnodeMeta);
  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) {
66 67
  SQnodeObj *pObj = sdbAcquire(pMnode->pSdb, SDB_QNODE, &qnodeId);
  if (pObj == NULL && terrno == TSDB_CODE_SDB_OBJ_NOT_THERE) {
S
Shengliang Guan 已提交
68 69 70 71 72 73 74 75 76 77 78
    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) {
79 80
  terrno = TSDB_CODE_OUT_OF_MEMORY;

S
Shengliang Guan 已提交
81
  SSdbRaw *pRaw = sdbAllocRaw(SDB_QNODE, TSDB_QNODE_VER_NUMBER, sizeof(SQnodeObj) + TSDB_QNODE_RESERVE_SIZE);
82
  if (pRaw == NULL) goto QNODE_ENCODE_OVER;
S
Shengliang Guan 已提交
83 84

  int32_t dataPos = 0;
85 86 87 88 89 90
  SDB_SET_INT32(pRaw, dataPos, pObj->id, QNODE_ENCODE_OVER)
  SDB_SET_INT64(pRaw, dataPos, pObj->createdTime, QNODE_ENCODE_OVER)
  SDB_SET_INT64(pRaw, dataPos, pObj->updateTime, QNODE_ENCODE_OVER)
  SDB_SET_RESERVE(pRaw, dataPos, TSDB_QNODE_RESERVE_SIZE, QNODE_ENCODE_OVER)

  terrno = 0;
S
Shengliang Guan 已提交
91

92 93 94 95 96 97 98 99
QNODE_ENCODE_OVER:
  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 已提交
100 101 102 103
  return pRaw;
}

static SSdbRow *mndQnodeActionDecode(SSdbRaw *pRaw) {
104 105
  terrno = TSDB_CODE_OUT_OF_MEMORY;

S
Shengliang Guan 已提交
106
  int8_t sver = 0;
107
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) goto QNODE_DECODE_OVER;
S
Shengliang Guan 已提交
108 109 110

  if (sver != TSDB_QNODE_VER_NUMBER) {
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
111
    goto QNODE_DECODE_OVER;
S
Shengliang Guan 已提交
112 113
  }

114 115 116
  SSdbRow *pRow = sdbAllocRow(sizeof(SQnodeObj));
  if (pRow == NULL) goto QNODE_DECODE_OVER;

S
Shengliang Guan 已提交
117
  SQnodeObj *pObj = sdbGetRowObj(pRow);
118
  if (pObj == NULL) goto QNODE_DECODE_OVER;
S
Shengliang Guan 已提交
119 120

  int32_t dataPos = 0;
121 122 123 124 125 126
  SDB_GET_INT32(pRaw, dataPos, &pObj->id, QNODE_DECODE_OVER)
  SDB_GET_INT64(pRaw, dataPos, &pObj->createdTime, QNODE_DECODE_OVER)
  SDB_GET_INT64(pRaw, dataPos, &pObj->updateTime, QNODE_DECODE_OVER)
  SDB_GET_RESERVE(pRaw, dataPos, TSDB_QNODE_RESERVE_SIZE, QNODE_DECODE_OVER)

  terrno = 0;
S
Shengliang Guan 已提交
127

128 129 130 131 132 133 134 135
QNODE_DECODE_OVER:
  if (terrno != 0) {
    mError("qnode:%d, failed to decode from raw:%p since %s", pObj->id, pRaw, terrstr());
    tfree(pRow);
    return NULL;
  }

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

static int32_t mndQnodeActionInsert(SSdb *pSdb, SQnodeObj *pObj) {
140
  mTrace("qnode:%d, perform insert action, row:%p", pObj->id, pObj);
S
Shengliang Guan 已提交
141 142 143 144 145 146 147 148 149 150 151
  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) {
152
  mTrace("qnode:%d, perform delete action, row:%p", pObj->id, pObj);
S
Shengliang Guan 已提交
153 154 155 156 157 158 159 160
  if (pObj->pDnode != NULL) {
    sdbRelease(pSdb, pObj->pDnode);
    pObj->pDnode = NULL;
  }

  return 0;
}

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

175 176 177 178 179 180 181 182
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 已提交
183 184 185 186 187 188 189 190 191
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 已提交
192 193 194 195 196
  SDCreateQnodeReq createReq = {0};
  createReq.dnodeId = pDnode->id;

  int32_t contLen = tSerializeSMCreateDropQSBNodeReq(NULL, 0, &createReq);
  void   *pReq = malloc(contLen);
S
Shengliang Guan 已提交
197
  if (pReq == NULL) {
S
Shengliang Guan 已提交
198 199 200
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
S
Shengliang Guan 已提交
201
  tSerializeSMCreateDropQSBNodeReq(pReq, contLen, &createReq);
S
Shengliang Guan 已提交
202 203 204

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

  if (mndTransAppendRedoAction(pTrans, &action) != 0) {
S
Shengliang Guan 已提交
211
    free(pReq);
S
Shengliang Guan 已提交
212 213 214 215 216 217
    return -1;
  }

  return 0;
}

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

  int32_t contLen = tSerializeSMCreateDropQSBNodeReq(NULL, 0, &dropReq);
  void   *pReq = malloc(contLen);
S
Shengliang Guan 已提交
224
  if (pReq == NULL) {
225 226 227
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
S
Shengliang Guan 已提交
228
  tSerializeSMCreateDropQSBNodeReq(pReq, contLen, &dropReq);
229 230 231

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

  if (mndTransAppendUndoAction(pTrans, &action) != 0) {
S
Shengliang Guan 已提交
238
    free(pReq);
239 240 241 242 243 244
    return -1;
  }

  return 0;
}

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

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

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

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

  code = 0;

CREATE_QNODE_OVER:
  mndTransDrop(pTrans);
  return code;
}

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

  if (tDeserializeSMCreateDropQSBNodeReq(pReq->rpcMsg.pCont, pReq->rpcMsg.contLen, &createReq) != 0) {
    terrno = TSDB_CODE_INVALID_MSG;
    goto CREATE_QNODE_OVER;
  }
S
Shengliang Guan 已提交
283

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

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

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

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

S
Shengliang Guan 已提交
306
  if (mndCheckNodeAuth(pUser)) {
S
Shengliang Guan 已提交
307 308 309 310 311 312 313 314 315
    goto CREATE_QNODE_OVER;
  }

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

CREATE_QNODE_OVER:
  if (code != 0 && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) {
    mError("qnode:%d, failed to create since %s", createReq.dnodeId, terrstr());
S
Shengliang Guan 已提交
316 317
  }

S
Shengliang Guan 已提交
318 319 320 321 322
  mndReleaseQnode(pMnode, pObj);
  mndReleaseDnode(pMnode, pDnode);
  mndReleaseUser(pMnode, pUser);

  return code;
S
Shengliang Guan 已提交
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
}

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 已提交
342 343 344 345 346
  SDDropQnodeReq dropReq = {0};
  dropReq.dnodeId = pDnode->id;

  int32_t contLen = tSerializeSMCreateDropQSBNodeReq(NULL, 0, &dropReq);
  void   *pReq = malloc(contLen);
S
Shengliang Guan 已提交
347
  if (pReq == NULL) {
S
Shengliang Guan 已提交
348 349 350
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
S
Shengliang Guan 已提交
351
  tSerializeSMCreateDropQSBNodeReq(pReq, contLen, &dropReq);
S
Shengliang Guan 已提交
352 353 354

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

  if (mndTransAppendRedoAction(pTrans, &action) != 0) {
S
Shengliang Guan 已提交
361
    free(pReq);
S
Shengliang Guan 已提交
362 363 364 365 366 367
    return -1;
  }

  return 0;
}

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

S
Shengliang Guan 已提交
371
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_TYPE_DROP_QNODE, &pReq->rpcMsg);
372
  if (pTrans == NULL) goto DROP_QNODE_OVER;
S
Shengliang Guan 已提交
373 374

  mDebug("trans:%d, used to drop qnode:%d", pTrans->id, pObj->id);
375 376 377 378
  if (mndSetDropQnodeRedoLogs(pTrans, pObj) != 0) goto DROP_QNODE_OVER;
  if (mndSetDropQnodeCommitLogs(pTrans, pObj) != 0) goto DROP_QNODE_OVER;
  if (mndSetDropQnodeRedoActions(pTrans, pObj->pDnode, pObj) != 0) goto DROP_QNODE_OVER;
  if (mndTransPrepare(pMnode, pTrans) != 0) goto DROP_QNODE_OVER;
S
Shengliang Guan 已提交
379 380 381 382 383 384 385 386

  code = 0;

DROP_QNODE_OVER:
  mndTransDrop(pTrans);
  return code;
}

S
Shengliang Guan 已提交
387 388
static int32_t mndProcessDropQnodeReq(SNodeMsg *pReq) {
  SMnode        *pMnode = pReq->pNode;
S
Shengliang Guan 已提交
389 390 391 392 393 394 395 396 397
  int32_t        code = -1;
  SUserObj      *pUser = NULL;
  SQnodeObj     *pObj = NULL;
  SMDropQnodeReq dropReq = {0};

  if (tDeserializeSMCreateDropQSBNodeReq(pReq->rpcMsg.pCont, pReq->rpcMsg.contLen, &dropReq) != 0) {
    terrno = TSDB_CODE_INVALID_MSG;
    goto DROP_QNODE_OVER;
  }
S
Shengliang Guan 已提交
398

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

S
Shengliang Guan 已提交
401
  if (dropReq.dnodeId <= 0) {
S
Shengliang Guan 已提交
402
    terrno = TSDB_CODE_SDB_APP_ERROR;
S
Shengliang Guan 已提交
403
    goto DROP_QNODE_OVER;
S
Shengliang Guan 已提交
404 405
  }

S
Shengliang Guan 已提交
406
  pObj = mndAcquireQnode(pMnode, dropReq.dnodeId);
S
Shengliang Guan 已提交
407
  if (pObj == NULL) {
S
Shengliang Guan 已提交
408 409 410 411 412 413 414
    goto DROP_QNODE_OVER;
  }

  pUser = mndAcquireUser(pMnode, pReq->user);
  if (pUser == NULL) {
    terrno = TSDB_CODE_MND_NO_USER_FROM_CONN;
    goto DROP_QNODE_OVER;
S
Shengliang Guan 已提交
415 416
  }

S
Shengliang Guan 已提交
417
  if (mndCheckNodeAuth(pUser)) {
S
Shengliang Guan 已提交
418 419 420 421 422 423 424 425
    goto DROP_QNODE_OVER;
  }

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

DROP_QNODE_OVER:
  if (code != 0 && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
426 427 428
    mError("qnode:%d, failed to drop since %s", pMnode->dnodeId, terrstr());
  }

S
Shengliang Guan 已提交
429 430 431 432
  mndReleaseQnode(pMnode, pObj);
  mndReleaseUser(pMnode, pUser);

  return code;
S
Shengliang Guan 已提交
433 434
}

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

  if (tDeserializeSQnodeListReq(pReq->rpcMsg.pCont, pReq->rpcMsg.contLen, &qlistReq) != 0) {
    mError("invalid qnode list msg");
    terrno = TSDB_CODE_INVALID_MSG;
    goto QNODE_LIST_OVER;
  }

D
dapan1121 已提交
450 451
  qlistRsp.epSetList = taosArrayInit(5, sizeof(SEpSet));
  if (NULL == qlistRsp.epSetList) {
D
dapan1121 已提交
452 453 454 455 456 457 458 459 460 461 462 463 464 465
    mError("taosArrayInit epSet failed");
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    goto QNODE_LIST_OVER;
  }
  
  while (true) {
    void *pIter = sdbFetch(pSdb, SDB_QNODE, NULL, (void **)&pObj);
    if (pIter == NULL) break;

    SEpSet epSet = {0};
    strcpy(epSet.eps[0].fqdn, pObj->pDnode->fqdn);
    epSet.eps[0].port = pObj->pDnode->port;
    epSet.numOfEps = 1;

D
dapan1121 已提交
466
    taosArrayPush(qlistRsp.epSetList, &epSet);
D
dapan1121 已提交
467 468 469 470

    numOfRows++;
    sdbRelease(pSdb, pObj);

D
dapan1121 已提交
471
    if (qlistReq.rowNum > 0 && numOfRows >= qlistReq.rowNum) {
D
dapan1121 已提交
472 473 474 475 476 477 478 479 480 481 482 483 484
      break;
    }
  }

  int32_t rspLen = tSerializeSQnodeListRsp(NULL, 0, &qlistRsp);
  void   *pRsp = malloc(rspLen);
  if (pRsp == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    goto QNODE_LIST_OVER;
  }
  
  tSerializeSQnodeListRsp(pRsp, rspLen, &qlistRsp);
  
S
Shengliang Guan 已提交
485 486
  pReq->rspLen = rspLen;
  pReq->pRsp = pRsp;
D
dapan1121 已提交
487 488 489 490 491 492 493 494 495
  code = 0;

QNODE_LIST_OVER:

  tFreeSQnodeListRsp(&qlistRsp);
  
  return code;
}

S
Shengliang Guan 已提交
496
static int32_t mndProcessCreateQnodeRsp(SNodeMsg *pRsp) {
S
Shengliang Guan 已提交
497
  mndTransProcessRsp(pRsp);
S
Shengliang Guan 已提交
498 499 500
  return 0;
}

S
Shengliang Guan 已提交
501
static int32_t mndProcessDropQnodeRsp(SNodeMsg *pRsp) {
S
Shengliang Guan 已提交
502
  mndTransProcessRsp(pRsp);
S
Shengliang Guan 已提交
503 504 505
  return 0;
}

S
Shengliang Guan 已提交
506 507
static int32_t mndGetQnodeMeta(SNodeMsg *pReq, SShowObj *pShow, STableMetaRsp *pMeta) {
  SMnode *pMnode = pReq->pNode;
S
Shengliang Guan 已提交
508 509 510
  SSdb   *pSdb = pMnode->pSdb;

  int32_t  cols = 0;
S
Shengliang Guan 已提交
511
  SSchema *pSchema = pMeta->pSchemas;
S
Shengliang Guan 已提交
512 513 514 515

  pShow->bytes[cols] = 2;
  pSchema[cols].type = TSDB_DATA_TYPE_SMALLINT;
  strcpy(pSchema[cols].name, "id");
S
Shengliang Guan 已提交
516
  pSchema[cols].bytes = pShow->bytes[cols];
S
Shengliang Guan 已提交
517 518 519 520 521
  cols++;

  pShow->bytes[cols] = TSDB_EP_LEN + VARSTR_HEADER_SIZE;
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
  strcpy(pSchema[cols].name, "endpoint");
S
Shengliang Guan 已提交
522
  pSchema[cols].bytes = pShow->bytes[cols];
S
Shengliang Guan 已提交
523 524 525 526 527
  cols++;

  pShow->bytes[cols] = 8;
  pSchema[cols].type = TSDB_DATA_TYPE_TIMESTAMP;
  strcpy(pSchema[cols].name, "create_time");
S
Shengliang Guan 已提交
528
  pSchema[cols].bytes = pShow->bytes[cols];
S
Shengliang Guan 已提交
529 530
  cols++;

S
Shengliang Guan 已提交
531
  pMeta->numOfColumns = cols;
S
Shengliang Guan 已提交
532 533 534 535 536 537 538 539 540
  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->numOfRows = sdbGetSize(pSdb, SDB_QNODE);
  pShow->rowSize = pShow->offset[cols - 1] + pShow->bytes[cols - 1];
D
dapan1121 已提交
541
  strcpy(pMeta->tbName, mndShowStr(pShow->type));
S
Shengliang Guan 已提交
542 543 544 545

  return 0;
}

S
Shengliang Guan 已提交
546 547
static int32_t mndRetrieveQnodes(SNodeMsg *pReq, SShowObj *pShow, char *data, int32_t rows) {
  SMnode    *pMnode = pReq->pNode;
S
Shengliang Guan 已提交
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
  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;

    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);
  pShow->numOfReads += numOfRows;

  return numOfRows;
}

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