mndQnode.c 16.2 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 "mndQnode.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 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 34 35
static int32_t  mndProcessCreateQnodeReq(SRpcMsg *pReq);
static int32_t  mndProcessDropQnodeReq(SRpcMsg *pReq);
static int32_t  mndProcessQnodeListReq(SRpcMsg *pReq);
static int32_t  mndRetrieveQnodes(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
S
Shengliang Guan 已提交
36 37 38
static void     mndCancelGetNextQnode(SMnode *pMnode, void *pIter);

int32_t mndInitQnode(SMnode *pMnode) {
39 40 41 42 43 44 45 46 47
  SSdbTable table = {
      .sdbType = SDB_QNODE,
      .keyType = SDB_KEY_INT32,
      .encodeFp = (SdbEncodeFp)mndQnodeActionEncode,
      .decodeFp = (SdbDecodeFp)mndQnodeActionDecode,
      .insertFp = (SdbInsertFp)mndQnodeActionInsert,
      .updateFp = (SdbUpdateFp)mndQnodeActionUpdate,
      .deleteFp = (SdbDeleteFp)mndQnodeActionDelete,
  };
S
Shengliang Guan 已提交
48 49 50

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

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

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

void mndCleanupQnode(SMnode *pMnode) {}

D
dapan1121 已提交
63
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
    terrno = TSDB_CODE_MND_QNODE_NOT_EXIST;
  }
  return pObj;
}

D
dapan1121 已提交
71
void mndReleaseQnode(SMnode *pMnode, SQnodeObj *pObj) {
S
Shengliang Guan 已提交
72 73 74 75 76
  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
  terrno = TSDB_CODE_OUT_OF_MEMORY;
103 104
  SSdbRow   *pRow = NULL;
  SQnodeObj *pObj = NULL;
105

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

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

114
  pRow = sdbAllocRow(sizeof(SQnodeObj));
115
  if (pRow == NULL) goto _OVER;
116

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

  int32_t dataPos = 0;
121 122 123 124
  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)
125 126

  terrno = 0;
S
Shengliang Guan 已提交
127

128
_OVER:
129
  if (terrno != 0) {
130
    mError("qnode:%d, failed to decode from raw:%p since %s", pObj == NULL ? 0 : pObj->id, pRaw, terrstr());
wafwerar's avatar
wafwerar 已提交
131
    taosMemoryFreeClear(pRow);
132 133 134 135
    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;
}

C
cadem 已提交
183
int32_t mndSetCreateQnodeCommitLogs(STrans *pTrans, SQnodeObj *pObj) {
S
Shengliang Guan 已提交
184 185 186 187 188 189 190
  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;
}

C
cadem 已提交
191 192 193 194 195
bool mndQnodeInDnode(SQnodeObj *pQnode, int32_t dnodeId) { 
  return pQnode->pDnode->id == dnodeId;
}

int32_t mndSetCreateQnodeRedoActions(STrans *pTrans, SDnodeObj *pDnode, SQnodeObj *pObj) {
S
Shengliang Guan 已提交
196 197 198
  SDCreateQnodeReq createReq = {0};
  createReq.dnodeId = pDnode->id;

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

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

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

  return 0;
}

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

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

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

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

  return 0;
}

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

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

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

260
  mInfo("trans:%d, used to create qnode:%d", pTrans->id, pCreate->dnodeId);
261 262 263 264 265 266
  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 已提交
267 268 269

  code = 0;

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

S
Shengliang Guan 已提交
275 276
static int32_t mndProcessCreateQnodeReq(SRpcMsg *pReq) {
  SMnode          *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
277 278 279 280 281
  int32_t          code = -1;
  SQnodeObj       *pObj = NULL;
  SDnodeObj       *pDnode = NULL;
  SMCreateQnodeReq createReq = {0};

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

287
  mInfo("qnode:%d, start to create", createReq.dnodeId);
288
  if (mndCheckOperPrivilege(pMnode, pReq->info.conn.user, MND_OPER_CREATE_QNODE) != 0) {
S
Shengliang Guan 已提交
289 290
    goto _OVER;
  }
S
Shengliang Guan 已提交
291

S
Shengliang Guan 已提交
292
  pObj = mndAcquireQnode(pMnode, createReq.dnodeId);
S
Shengliang Guan 已提交
293
  if (pObj != NULL) {
S
Shengliang Guan 已提交
294
    terrno = TSDB_CODE_MND_QNODE_ALREADY_EXIST;
295
    goto _OVER;
296
  } else if (terrno != TSDB_CODE_MND_QNODE_NOT_EXIST) {
297
    goto _OVER;
S
Shengliang Guan 已提交
298 299
  }

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

S
Shengliang Guan 已提交
306
  code = mndCreateQnode(pMnode, pReq, pDnode, &createReq);
S
Shengliang Guan 已提交
307
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
S
Shengliang Guan 已提交
308

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

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

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 已提交
336 337 338
  SDDropQnodeReq dropReq = {0};
  dropReq.dnodeId = pDnode->id;

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

  STransAction action = {0};
  action.epSet = mndGetDnodeEpset(pDnode);
S
Shengliang Guan 已提交
349
  action.pCont = pReq;
S
Shengliang Guan 已提交
350
  action.contLen = contLen;
S
Shengliang Guan 已提交
351
  action.msgType = TDMT_DND_DROP_QNODE;
352
  action.acceptableCode = TSDB_CODE_QNODE_NOT_DEPLOYED;
S
Shengliang Guan 已提交
353 354

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

  return 0;
}

S
Shengliang Guan 已提交
362
int32_t mndSetDropQnodeInfoToTrans(SMnode *pMnode, STrans *pTrans, SQnodeObj *pObj, bool force) {
363 364 365
  if (pObj == NULL) return 0;
  if (mndSetDropQnodeRedoLogs(pTrans, pObj) != 0) return -1;
  if (mndSetDropQnodeCommitLogs(pTrans, pObj) != 0) return -1;
S
Shengliang Guan 已提交
366 367 368
  if (!force) {
    if (mndSetDropQnodeRedoActions(pTrans, pObj->pDnode, pObj) != 0) return -1;
  }
369 370 371
  return 0;
}

S
Shengliang Guan 已提交
372
static int32_t mndDropQnode(SMnode *pMnode, SRpcMsg *pReq, SQnodeObj *pObj) {
S
Shengliang Guan 已提交
373
  int32_t code = -1;
374

375
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_NOTHING, pReq, "drop-qnode");
376
  if (pTrans == NULL) goto _OVER;
S
Shengliang Guan 已提交
377

378
  mInfo("trans:%d, used to drop qnode:%d", pTrans->id, pObj->id);
S
Shengliang Guan 已提交
379
  if (mndSetDropQnodeInfoToTrans(pMnode, pTrans, pObj, false) != 0) goto _OVER;
380
  if (mndTransPrepare(pMnode, pTrans) != 0) goto _OVER;
S
Shengliang Guan 已提交
381 382 383

  code = 0;

384
_OVER:
S
Shengliang Guan 已提交
385 386 387 388
  mndTransDrop(pTrans);
  return code;
}

S
Shengliang Guan 已提交
389 390
static int32_t mndProcessDropQnodeReq(SRpcMsg *pReq) {
  SMnode        *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
391 392 393 394
  int32_t        code = -1;
  SQnodeObj     *pObj = NULL;
  SMDropQnodeReq dropReq = {0};

S
Shengliang Guan 已提交
395
  if (tDeserializeSCreateDropMQSNodeReq(pReq->pCont, pReq->contLen, &dropReq) != 0) {
S
Shengliang Guan 已提交
396
    terrno = TSDB_CODE_INVALID_MSG;
397
    goto _OVER;
S
Shengliang Guan 已提交
398
  }
S
Shengliang Guan 已提交
399

400
  mInfo("qnode:%d, start to drop", dropReq.dnodeId);
401
  if (mndCheckOperPrivilege(pMnode, pReq->info.conn.user, MND_OPER_DROP_QNODE) != 0) {
S
Shengliang Guan 已提交
402 403
    goto _OVER;
  }
S
Shengliang Guan 已提交
404

S
Shengliang Guan 已提交
405
  if (dropReq.dnodeId <= 0) {
406
    terrno = TSDB_CODE_INVALID_MSG;
407
    goto _OVER;
S
Shengliang Guan 已提交
408 409
  }

S
Shengliang Guan 已提交
410
  pObj = mndAcquireQnode(pMnode, dropReq.dnodeId);
S
Shengliang Guan 已提交
411
  if (pObj == NULL) {
412
    goto _OVER;
S
Shengliang Guan 已提交
413 414 415
  }

  code = mndDropQnode(pMnode, pReq, pObj);
S
Shengliang Guan 已提交
416
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
S
Shengliang Guan 已提交
417

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

S
Shengliang Guan 已提交
423 424
  mndReleaseQnode(pMnode, pObj);
  return code;
S
Shengliang Guan 已提交
425 426
}

S
Shengliang Guan 已提交
427 428 429 430 431
int32_t mndCreateQnodeList(SMnode *pMnode, SArray **pList, int32_t limit) {
  SSdb      *pSdb = pMnode->pSdb;
  void      *pIter = NULL;
  SQnodeObj *pObj = NULL;
  int32_t    numOfRows = 0;
D
dapan1121 已提交
432

S
Shengliang Guan 已提交
433
  SArray *qnodeList = taosArrayInit(5, sizeof(SQueryNodeLoad));
D
dapan1121 已提交
434
  if (NULL == qnodeList) {
435
    mError("failed to alloc epSet while process qnode list req");
D
dapan1121 已提交
436
    terrno = TSDB_CODE_OUT_OF_MEMORY;
D
dapan1121 已提交
437
    return terrno;
D
dapan1121 已提交
438
  }
S
Shengliang Guan 已提交
439

440
  while (1) {
D
dapan 已提交
441
    pIter = sdbFetch(pSdb, SDB_QNODE, pIter, (void **)&pObj);
D
dapan1121 已提交
442 443
    if (pIter == NULL) break;

D
dapan1121 已提交
444 445 446 447 448 449
    SQueryNodeLoad nodeLoad = {0};
    nodeLoad.addr.nodeId = QNODE_HANDLE;
    nodeLoad.addr.epSet.numOfEps = 1;
    tstrncpy(nodeLoad.addr.epSet.eps[0].fqdn, pObj->pDnode->fqdn, TSDB_FQDN_LEN);
    nodeLoad.addr.epSet.eps[0].port = pObj->pDnode->port;
    nodeLoad.load = QNODE_LOAD_VALUE(pObj);
D
dapan1121 已提交
450

D
dapan1121 已提交
451
    (void)taosArrayPush(qnodeList, &nodeLoad);
D
dapan1121 已提交
452 453 454 455

    numOfRows++;
    sdbRelease(pSdb, pObj);

D
dapan1121 已提交
456
    if (limit > 0 && numOfRows >= limit) {
D
dapan1121 已提交
457 458 459 460
      break;
    }
  }

D
dapan1121 已提交
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
  *pList = qnodeList;

  return TSDB_CODE_SUCCESS;
}

static int32_t mndProcessQnodeListReq(SRpcMsg *pReq) {
  int32_t       code = -1;
  SMnode       *pMnode = pReq->info.node;
  SQnodeListReq qlistReq = {0};
  SQnodeListRsp qlistRsp = {0};

  if (tDeserializeSQnodeListReq(pReq->pCont, pReq->contLen, &qlistReq) != 0) {
    mError("failed to parse qnode list req");
    terrno = TSDB_CODE_INVALID_MSG;
    goto _OVER;
  }

  if (mndCreateQnodeList(pMnode, &qlistRsp.qnodeList, qlistReq.rowNum) != 0) {
    goto _OVER;
  }

D
dapan1121 已提交
482
  int32_t rspLen = tSerializeSQnodeListRsp(NULL, 0, &qlistRsp);
D
dapan 已提交
483
  void   *pRsp = rpcMallocCont(rspLen);
D
dapan1121 已提交
484 485
  if (pRsp == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
486
    goto _OVER;
D
dapan1121 已提交
487
  }
488

D
dapan1121 已提交
489
  tSerializeSQnodeListRsp(pRsp, rspLen, &qlistRsp);
490

S
Shengliang Guan 已提交
491 492
  pReq->info.rspLen = rspLen;
  pReq->info.rsp = pRsp;
D
dapan1121 已提交
493 494
  code = 0;

495
_OVER:
D
dapan1121 已提交
496 497 498 499
  tFreeSQnodeListRsp(&qlistRsp);
  return code;
}

S
Shengliang Guan 已提交
500 501
static int32_t mndRetrieveQnodes(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
  SMnode    *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
502 503 504 505 506 507 508 509 510 511 512
  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 已提交
513
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
514
    colDataSetVal(pColInfo, numOfRows, (const char *)&pObj->id, false);
S
Shengliang Guan 已提交
515

516
    char ep[TSDB_EP_LEN + VARSTR_HEADER_SIZE] = {0};
517
    STR_WITH_MAXSIZE_TO_VARSTR(ep, pObj->pDnode->ep, pShow->pMeta->pSchemas[cols].bytes);
518
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
519
    colDataSetVal(pColInfo, numOfRows, (const char *)ep, false);
S
Shengliang Guan 已提交
520

521
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
522
    colDataSetVal(pColInfo, numOfRows, (const char *)&pObj->createdTime, false);
S
Shengliang Guan 已提交
523 524 525 526 527

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

528
  pShow->numOfRows += numOfRows;
S
Shengliang Guan 已提交
529 530 531 532 533 534 535 536

  return numOfRows;
}

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