dnodeMnode.c 16.4 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 "dnodeMnode.h"
S
Shengliang Guan 已提交
18
#include "cJSON.h"
S
Shengliang Guan 已提交
19
#include "dnodeDnode.h"
S
Shengliang Guan 已提交
20 21
#include "dnodeTransport.h"
#include "mnode.h"
S
Shengliang Guan 已提交
22 23 24 25
#include "tlockfree.h"
#include "tqueue.h"
#include "tstep.h"
#include "tworker.h"
S
Shengliang Guan 已提交
26

S
Shengliang Guan 已提交
27
static struct {
S
Shengliang Guan 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40
  int32_t     refCount;
  int8_t      deployed;
  int8_t      dropped;
  SWorkerPool mgmtPool;
  SWorkerPool readPool;
  SWorkerPool writePool;
  SWorkerPool syncPool;
  taos_queue  pReadQ;
  taos_queue  pWriteQ;
  taos_queue  pApplyQ;
  taos_queue  pSyncQ;
  taos_queue  pMgmtQ;
  SSteps     *pSteps;
S
Shengliang Guan 已提交
41
  SMnode     *pMnode;
S
Shengliang Guan 已提交
42
  SRWLatch    latch;
S
Shengliang Guan 已提交
43
} tsMnode = {0};
S
Shengliang Guan 已提交
44

S
Shengliang Guan 已提交
45 46 47 48 49 50 51 52 53
static int32_t dnodeAllocMnodeReadQueue();
static void    dnodeFreeMnodeReadQueue();
static int32_t dnodeAllocMnodeWriteQueue();
static void    dnodeFreeMnodeWriteQueue();
static int32_t dnodeAllocMnodeApplyQueue();
static void    dnodeFreeMnodeApplyQueue();
static int32_t dnodeAllocMnodeSyncQueue();
static void    dnodeFreeMnodeSyncQueue();

S
Shengliang Guan 已提交
54 55
static SMnode *dnodeAcquireMnode() {
  SMnode *pMnode = NULL;
S
Shengliang Guan 已提交
56 57
  taosRLockLatch(&tsMnode.latch);

S
Shengliang Guan 已提交
58
  if (tsMnode.deployed) {
S
Shengliang Guan 已提交
59
    atomic_add_fetch_32(&tsMnode.refCount, 1);
S
Shengliang Guan 已提交
60
    pMnode = tsMnode.pMnode;
S
Shengliang Guan 已提交
61 62 63
  }

  taosRUnLockLatch(&tsMnode.latch);
S
Shengliang Guan 已提交
64
  return pMnode;
S
Shengliang Guan 已提交
65 66
}

S
Shengliang Guan 已提交
67 68 69 70 71
static void dnodeReleaseMnode(SMnode *pMnode) {
  taosRLockLatch(&tsMnode.latch);
  atomic_sub_fetch_32(&tsMnode.refCount, 1);
  taosRUnLockLatch(&tsMnode.latch);
}
S
Shengliang Guan 已提交
72 73 74

static int32_t dnodeReadMnodeFile() {
  int32_t code = TSDB_CODE_DND_READ_MNODE_FILE_ERROR;
S
Shengliang Guan 已提交
75 76 77 78 79
  int32_t len = 0;
  int32_t maxLen = 300;
  char   *content = calloc(1, maxLen + 1);
  cJSON  *root = NULL;
  FILE   *fp = NULL;
S
Shengliang Guan 已提交
80
  char    file[PATH_MAX + 20] = {0};
S
Shengliang Guan 已提交
81

S
Shengliang Guan 已提交
82 83
  snprintf(file, sizeof(file), "%s/mnode.json", tsDnodeDir);
  fp = fopen(file, "r");
S
Shengliang Guan 已提交
84
  if (!fp) {
S
Shengliang Guan 已提交
85 86
    dDebug("file %s not exist", file);
    code = 0;
S
Shengliang Guan 已提交
87 88
    goto PRASE_MNODE_OVER;
  }
S
Shengliang Guan 已提交
89

S
Shengliang Guan 已提交
90 91
  len = (int32_t)fread(content, 1, maxLen, fp);
  if (len <= 0) {
S
Shengliang Guan 已提交
92
    dError("failed to read %s since content is null", file);
S
Shengliang Guan 已提交
93 94 95 96 97 98
    goto PRASE_MNODE_OVER;
  }

  content[len] = 0;
  root = cJSON_Parse(content);
  if (root == NULL) {
S
Shengliang Guan 已提交
99
    dError("failed to read %s since invalid json format", file);
S
Shengliang Guan 已提交
100
    goto PRASE_MNODE_OVER;
S
Shengliang Guan 已提交
101 102
  }

S
Shengliang Guan 已提交
103 104
  cJSON *deployed = cJSON_GetObjectItem(root, "deployed");
  if (!deployed || deployed->type != cJSON_String) {
S
Shengliang Guan 已提交
105
    dError("failed to read %s since deployed not found", file);
S
Shengliang Guan 已提交
106 107 108 109 110 111
    goto PRASE_MNODE_OVER;
  }
  tsMnode.deployed = atoi(deployed->valuestring);

  cJSON *dropped = cJSON_GetObjectItem(root, "dropped");
  if (!dropped || dropped->type != cJSON_String) {
S
Shengliang Guan 已提交
112
    dError("failed to read %s since dropped not found", file);
S
Shengliang Guan 已提交
113 114 115 116
    goto PRASE_MNODE_OVER;
  }
  tsMnode.dropped = atoi(dropped->valuestring);

S
Shengliang Guan 已提交
117 118
  code = 0;
  dInfo("succcessed to read file %s", file);
S
Shengliang Guan 已提交
119 120 121 122 123 124

PRASE_MNODE_OVER:
  if (content != NULL) free(content);
  if (root != NULL) cJSON_Delete(root);
  if (fp != NULL) fclose(fp);

S
Shengliang Guan 已提交
125
  return code;
S
Shengliang Guan 已提交
126 127
}

S
Shengliang Guan 已提交
128 129 130 131 132 133 134
static int32_t dnodeWriteMnodeFile() {
  char file[PATH_MAX + 20] = {0};
  char realfile[PATH_MAX + 20] = {0};
  snprintf(file, sizeof(file), "%s/mnode.json.bak", tsDnodeDir);
  snprintf(realfile, sizeof(realfile), "%s/mnode.json", tsDnodeDir);

  FILE *fp = fopen(file, "w");
S
Shengliang Guan 已提交
135
  if (!fp) {
S
Shengliang Guan 已提交
136 137
    dError("failed to write %s since %s", file, strerror(errno));
    return TSDB_CODE_DND_WRITE_MNODE_FILE_ERROR;
S
Shengliang Guan 已提交
138 139 140 141 142 143 144
  }

  int32_t len = 0;
  int32_t maxLen = 300;
  char   *content = calloc(1, maxLen + 1);

  len += snprintf(content + len, maxLen - len, "{\n");
S
Shengliang Guan 已提交
145 146
  len += snprintf(content + len, maxLen - len, "  \"deployed\": \"%d\",\n", tsMnode.deployed);
  len += snprintf(content + len, maxLen - len, "  \"dropped\": \"%d\"\n", tsMnode.dropped);
S
Shengliang Guan 已提交
147 148 149 150 151 152 153
  len += snprintf(content + len, maxLen - len, "}\n");

  fwrite(content, 1, len, fp);
  taosFsyncFile(fileno(fp));
  fclose(fp);
  free(content);

S
Shengliang Guan 已提交
154 155 156 157 158 159 160
  int32_t code = taosRenameFile(file, realfile);
  if (code != 0) {
    dError("failed to rename %s since %s", file, tstrerror(code));
    return TSDB_CODE_DND_WRITE_MNODE_FILE_ERROR;
  }

  dInfo("successed to write %s", realfile);
S
Shengliang Guan 已提交
161 162 163
  return 0;
}

S
Shengliang Guan 已提交
164 165 166 167 168
static int32_t dnodeStartMnode() {
  int32_t code = dnodeAllocMnodeReadQueue();
  if (code != 0) {
    return code;
  }
S
Shengliang Guan 已提交
169

S
Shengliang Guan 已提交
170 171
  code = dnodeAllocMnodeWriteQueue();
  if (code != 0) {
S
Shengliang Guan 已提交
172
    return code;
S
Shengliang Guan 已提交
173 174
  }

S
Shengliang Guan 已提交
175 176
  code = dnodeAllocMnodeApplyQueue();
  if (code != 0) {
S
Shengliang Guan 已提交
177 178 179
    return code;
  }

S
Shengliang Guan 已提交
180 181 182
  code = dnodeAllocMnodeSyncQueue();
  if (code != 0) {
    return code;
S
Shengliang Guan 已提交
183
  }
S
Shengliang Guan 已提交
184

S
Shengliang Guan 已提交
185
  taosWLockLatch(&tsMnode.latch);
S
Shengliang Guan 已提交
186
  tsMnode.deployed = 1;
S
Shengliang Guan 已提交
187 188
  taosWUnLockLatch(&tsMnode.latch);

S
Shengliang Guan 已提交
189
  return mnodeStart(NULL);
S
Shengliang Guan 已提交
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
}

static void dnodeStopMnode() {
  taosWLockLatch(&tsMnode.latch);
  tsMnode.deployed = 0;
  taosWUnLockLatch(&tsMnode.latch);

  dnodeReleaseMnode();

  while (tsMnode.refCount > 0) taosMsleep(10);
  while (!taosQueueEmpty(tsMnode.pReadQ)) taosMsleep(10);
  while (!taosQueueEmpty(tsMnode.pApplyQ)) taosMsleep(10);
  while (!taosQueueEmpty(tsMnode.pWriteQ)) taosMsleep(10);
  while (!taosQueueEmpty(tsMnode.pSyncQ)) taosMsleep(10);

  dnodeFreeMnodeReadQueue();
  dnodeFreeMnodeWriteQueue();
  dnodeFreeMnodeApplyQueue();
  dnodeFreeMnodeSyncQueue();
}
S
Shengliang Guan 已提交
210

S
Shengliang Guan 已提交
211 212 213
static int32_t dnodeUnDeployMnode() {
  tsMnode.dropped = 1;
  int32_t code = dnodeWriteMnodeFile();
S
Shengliang Guan 已提交
214
  if (code != 0) {
S
Shengliang Guan 已提交
215 216
    tsMnode.dropped = 0;
    dError("failed to undeploy mnode since %s", tstrerror(code));
S
Shengliang Guan 已提交
217 218 219
    return code;
  }

S
Shengliang Guan 已提交
220
  dnodeStopMnode();
S
Shengliang Guan 已提交
221
  mnodeUnDeploy();
S
Shengliang Guan 已提交
222 223 224 225 226 227
  dnodeWriteMnodeFile();

  return code;
}

static int32_t dnodeDeployMnode(SMnodeCfg *pCfg) {
S
Shengliang Guan 已提交
228
  int32_t code = mnodeDeploy(pCfg);
S
Shengliang Guan 已提交
229
  if (code != 0) {
S
Shengliang Guan 已提交
230
    dError("failed to deploy mnode since %s", tstrerror(code));
S
Shengliang Guan 已提交
231 232 233
    return code;
  }

S
Shengliang Guan 已提交
234
  code = dnodeStartMnode();
S
Shengliang Guan 已提交
235
  if (code != 0) {
S
Shengliang Guan 已提交
236 237
    dnodeUnDeployMnode();
    dError("failed to deploy mnode since %s", tstrerror(code));
S
Shengliang Guan 已提交
238 239 240
    return code;
  }

S
Shengliang Guan 已提交
241 242 243 244 245 246 247 248 249
  code = dnodeWriteMnodeFile();
  if (code != 0) {
    dnodeUnDeployMnode();
    dError("failed to deploy mnode since %s", tstrerror(code));
    return code;
  }

  dInfo("deploy mnode success");
  return code;
S
Shengliang Guan 已提交
250 251
}

S
Shengliang Guan 已提交
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
static int32_t dnodeAlterMnode(SMnodeCfg *pCfg) {
  int32_t code = dnodeAcquireMnode();
  if (code == 0) {
    code = mnodeAlter(pCfg);
    dnodeReleaseMnode();
  }
  return code;
}

static SCreateMnodeMsg *dnodeParseCreateMnodeMsg(SRpcMsg *pRpcMsg) {
  SCreateMnodeMsg *pMsg = pRpcMsg->pCont;
  pMsg->dnodeId = htonl(pMsg->dnodeId);
  for (int32_t i = 0; i < pMsg->replica; ++i) {
    pMsg->replicas[i].port = htons(pMsg->replicas[i].port);
  }
  return pMsg;
}

static int32_t dnodeProcessCreateMnodeReq(SRpcMsg *pRpcMsg) {
  SAlterMnodeMsg *pMsg = (SAlterMnodeMsg *)dnodeParseCreateMnodeMsg(pRpcMsg->pCont);

  if (pMsg->dnodeId != dnodeGetDnodeId()) {
    return TSDB_CODE_DND_MNODE_ID_NOT_MATCH_DNODE;
  } else {
    SMnodeCfg cfg = {0};
    cfg.replica = pMsg->replica;
    memcpy(cfg.replicas, pMsg->replicas, sizeof(SReplica) * sizeof(TSDB_MAX_REPLICA));
    return dnodeDeployMnode(&cfg);
  }
}

static int32_t dnodeProcessAlterMnodeReq(SRpcMsg *pRpcMsg) {
  SAlterMnodeMsg *pMsg = (SAlterMnodeMsg *)dnodeParseCreateMnodeMsg(pRpcMsg->pCont);
  if (pMsg->dnodeId != dnodeGetDnodeId()) {
    return TSDB_CODE_DND_MNODE_ID_NOT_MATCH_DNODE;
  } else {
    SMnodeCfg cfg = {0};
    cfg.replica = pMsg->replica;
    memcpy(cfg.replicas, pMsg->replicas, sizeof(SReplica) * sizeof(TSDB_MAX_REPLICA));
    return dnodeAlterMnode(&cfg);
  }
}

static int32_t dnodeProcessDropMnodeReq(SRpcMsg *pMsg) {
  SAlterMnodeMsg *pCfg = pMsg->pCont;
  pCfg->dnodeId = htonl(pCfg->dnodeId);
S
Shengliang Guan 已提交
298 299

  if (pCfg->dnodeId != dnodeGetDnodeId()) {
S
Shengliang Guan 已提交
300 301 302
    return TSDB_CODE_DND_MNODE_ID_NOT_MATCH_DNODE;
  } else {
    return dnodeUnDeployMnode();
S
Shengliang Guan 已提交
303
  }
S
Shengliang Guan 已提交
304
}
S
Shengliang Guan 已提交
305

S
Shengliang Guan 已提交
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
static void dnodeProcessMnodeMgmtQueue(void *unused, SRpcMsg *pMsg) {
  int32_t code = 0;

  switch (pMsg->msgType) {
    case TSDB_MSG_TYPE_CREATE_MNODE_IN:
      code = dnodeProcessCreateMnodeReq(pMsg);
      break;
    case TSDB_MSG_TYPE_ALTER_MNODE_IN:
      code = dnodeProcessAlterMnodeReq(pMsg);
      break;
    case TSDB_MSG_TYPE_DROP_MNODE_IN:
      code = dnodeProcessDropMnodeReq(pMsg);
      break;
    default:
      code = TSDB_CODE_DND_MSG_NOT_PROCESSED;
      break;
S
Shengliang Guan 已提交
322 323
  }

S
Shengliang Guan 已提交
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
  SRpcMsg rsp = {.code = code, .handle = pMsg->handle};
  rpcSendResponse(&rsp);
  rpcFreeCont(pMsg->pCont);
  taosFreeQitem(pMsg);
}

static void dnodeProcessMnodeReadQueue(void *unused, SMnodeMsg *pMsg) { mnodeProcessMsg(pMsg, MN_MSG_TYPE_READ); }

static void dnodeProcessMnodeWriteQueue(void *unused, SMnodeMsg *pMsg) { mnodeProcessMsg(pMsg, MN_MSG_TYPE_WRITE); }

static void dnodeProcessMnodeApplyQueue(void *unused, SMnodeMsg *pMsg) { mnodeProcessMsg(pMsg, MN_MSG_TYPE_APPLY); }

static void dnodeProcessMnodeSyncQueue(void *unused, SMnodeMsg *pMsg) { mnodeProcessMsg(pMsg, MN_MSG_TYPE_SYNC); }

static int32_t dnodeWriteMnodeMsgToQueue(taos_queue pQueue, SRpcMsg *pRpcMsg) {
S
Shengliang Guan 已提交
339 340
  int32_t    code = 0;
  SMnodeMsg *pMsg = NULL;
S
Shengliang Guan 已提交
341 342 343 344

  if (pQueue == NULL) {
    code = TSDB_CODE_DND_MSG_NOT_PROCESSED;
  } else {
S
Shengliang Guan 已提交
345
    pMsg = mnodeInitMsg(pRpcMsg);
S
Shengliang Guan 已提交
346
    if (pMsg == NULL) {
S
Shengliang Guan 已提交
347
      code = terrno;
S
Shengliang Guan 已提交
348
    }
S
Shengliang Guan 已提交
349 350
  }

S
Shengliang Guan 已提交
351 352 353 354
  if (code == 0) {
    code = taosWriteQitem(pQueue, pMsg);
  }

S
Shengliang Guan 已提交
355 356 357 358 359 360
  if (code != TSDB_CODE_SUCCESS) {
    SRpcMsg rsp = {.handle = pRpcMsg->handle, .code = code};
    rpcSendResponse(&rsp);
    rpcFreeCont(pRpcMsg->pCont);
  }
}
S
Shengliang Guan 已提交
361

S
Shengliang Guan 已提交
362
void dnodeProcessMnodeMgmtMsg(SRpcMsg *pMsg, SEpSet *pEpSet) { dnodeWriteMnodeMsgToQueue(tsMnode.pMgmtQ, pMsg); }
S
Shengliang Guan 已提交
363

S
Shengliang Guan 已提交
364 365 366 367 368
void dnodeProcessMnodeWriteMsg(SRpcMsg *pMsg, SEpSet *pEpSet) {
  if (dnodeAcquireMnode() == 0) {
    dnodeWriteMnodeMsgToQueue(tsMnode.pWriteQ, pMsg);
    dnodeReleaseMnode();
  } else {
S
Shengliang Guan 已提交
369
    dnodeSendRedirectMsg(NULL, pMsg, 0);
S
Shengliang Guan 已提交
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
  }
}

void dnodeProcessMnodeSyncMsg(SRpcMsg *pMsg, SEpSet *pEpSet) {
  int32_t code = dnodeAcquireMnode();
  if (code == 0) {
    dnodeWriteMnodeMsgToQueue(tsMnode.pSyncQ, pMsg);
    dnodeReleaseMnode();
  } else {
    SRpcMsg rsp = {.handle = pMsg->handle, .code = code};
    rpcSendResponse(&rsp);
    rpcFreeCont(pMsg->pCont);
  }
}

void dnodeProcessMnodeReadMsg(SRpcMsg *pMsg, SEpSet *pEpSet) {
  if (dnodeAcquireMnode() == 0) {
    dnodeWriteMnodeMsgToQueue(tsMnode.pReadQ, pMsg);
    dnodeReleaseMnode();
  } else {
S
Shengliang Guan 已提交
390
    dnodeSendRedirectMsg(NULL, pMsg, 0);
S
Shengliang Guan 已提交
391
  }
S
Shengliang Guan 已提交
392
}
S
Shengliang Guan 已提交
393

S
Shengliang Guan 已提交
394 395 396
static int32_t dnodePutMsgIntoMnodeApplyQueue(SMnodeMsg *pMsg) {
  int32_t code = dnodeAcquireMnode();
  if (code != 0) return code;
S
Shengliang Guan 已提交
397

S
Shengliang Guan 已提交
398 399 400 401 402 403 404 405 406 407
  code = taosWriteQitem(tsMnode.pApplyQ, pMsg);
  dnodeReleaseMnode();
  return code;
}

static int32_t dnodeAllocMnodeMgmtQueue() {
  tsMnode.pMgmtQ = tWorkerAllocQueue(&tsMnode.mgmtPool, NULL, (FProcessItem)dnodeProcessMnodeMgmtQueue);
  if (tsMnode.pMgmtQ == NULL) {
    return TSDB_CODE_DND_OUT_OF_MEMORY;
  }
S
Shengliang Guan 已提交
408 409
  return 0;
}
S
Shengliang Guan 已提交
410

S
Shengliang Guan 已提交
411 412 413 414
static void dnodeFreeMnodeMgmtQueue() {
  tWorkerFreeQueue(&tsMnode.mgmtPool, tsMnode.pMgmtQ);
  tsMnode.pMgmtQ = NULL;
}
S
Shengliang Guan 已提交
415

S
Shengliang Guan 已提交
416 417 418 419 420 421
static int32_t dnodeInitMnodeMgmtWorker() {
  SWorkerPool *pPool = &tsMnode.mgmtPool;
  pPool->name = "mnode-mgmt";
  pPool->min = 1;
  pPool->max = 1;
  return tWorkerInit(pPool);
S
Shengliang Guan 已提交
422 423
}

S
Shengliang Guan 已提交
424
static void dnodeCleanupMnodeMgmtWorker() { tWorkerCleanup(&tsMnode.mgmtPool); }
S
Shengliang Guan 已提交
425

S
Shengliang Guan 已提交
426 427 428 429 430 431
static int32_t dnodeAllocMnodeReadQueue() {
  tsMnode.pReadQ = tWorkerAllocQueue(&tsMnode.readPool, NULL, (FProcessItem)dnodeProcessMnodeReadQueue);
  if (tsMnode.pReadQ == NULL) {
    return TSDB_CODE_DND_OUT_OF_MEMORY;
  }
  return 0;
S
Shengliang Guan 已提交
432 433
}

S
Shengliang Guan 已提交
434 435 436
static void dnodeFreeMnodeReadQueue() {
  tWorkerFreeQueue(&tsMnode.readPool, tsMnode.pReadQ);
  tsMnode.pReadQ = NULL;
S
Shengliang Guan 已提交
437 438
}

S
Shengliang Guan 已提交
439 440 441 442 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 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
static int32_t dnodeInitMnodeReadWorker() {
  SWorkerPool *pPool = &tsMnode.readPool;
  pPool->name = "mnode-read";
  pPool->min = 0;
  pPool->max = 1;
  return tWorkerInit(pPool);
}

static void dnodeCleanupMnodeReadWorker() { tWorkerCleanup(&tsMnode.readPool); }

static int32_t dnodeAllocMnodeWriteQueue() {
  tsMnode.pWriteQ = tWorkerAllocQueue(&tsMnode.writePool, NULL, (FProcessItem)dnodeProcessMnodeWriteQueue);
  if (tsMnode.pWriteQ == NULL) {
    return TSDB_CODE_DND_OUT_OF_MEMORY;
  }
  return 0;
}

static void dnodeFreeMnodeWriteQueue() {
  tWorkerFreeQueue(&tsMnode.writePool, tsMnode.pWriteQ);
  tsMnode.pWriteQ = NULL;
}

static int32_t dnodeAllocMnodeApplyQueue() {
  tsMnode.pApplyQ = tWorkerAllocQueue(&tsMnode.writePool, NULL, (FProcessItem)dnodeProcessMnodeApplyQueue);
  if (tsMnode.pApplyQ == NULL) {
    return TSDB_CODE_DND_OUT_OF_MEMORY;
  }
  return 0;
}

static void dnodeFreeMnodeApplyQueue() {
  tWorkerFreeQueue(&tsMnode.writePool, tsMnode.pApplyQ);
  tsMnode.pApplyQ = NULL;
}

static int32_t dnodeInitMnodeWriteWorker() {
  SWorkerPool *pPool = &tsMnode.writePool;
  pPool->name = "mnode-write";
  pPool->min = 0;
  pPool->max = 1;
  return tWorkerInit(pPool);
}

static void dnodeCleanupMnodeWriteWorker() { tWorkerCleanup(&tsMnode.writePool); }

static int32_t dnodeAllocMnodeSyncQueue() {
  tsMnode.pSyncQ = tWorkerAllocQueue(&tsMnode.syncPool, NULL, (FProcessItem)dnodeProcessMnodeSyncQueue);
  if (tsMnode.pSyncQ == NULL) {
    return TSDB_CODE_DND_OUT_OF_MEMORY;
  }
  return 0;
}

static void dnodeFreeMnodeSyncQueue() {
  tWorkerFreeQueue(&tsMnode.syncPool, tsMnode.pSyncQ);
  tsMnode.pSyncQ = NULL;
}

static int32_t dnodeInitMnodeSyncWorker() {
  SWorkerPool *pPool = &tsMnode.syncPool;
  pPool->name = "mnode-sync";
  pPool->min = 0;
  pPool->max = 1;
  return tWorkerInit(pPool);
}

static void dnodeCleanupMnodeSyncWorker() { tWorkerCleanup(&tsMnode.syncPool); }

static int32_t dnodeInitMnodeModule() {
  taosInitRWLatch(&tsMnode.latch);
S
Shengliang Guan 已提交
510

S
Shengliang Guan 已提交
511
  SMnodeOptions para;
S
Shengliang Guan 已提交
512 513
  para.dnodeId = dnodeGetDnodeId();
  para.clusterId = dnodeGetClusterId();
S
Shengliang Guan 已提交
514 515
  para.sendMsgToDnodeFp = dnodeSendMsgToDnode;
  para.sendMsgToMnodeFp = dnodeSendMsgToMnode;
S
Shengliang Guan 已提交
516
  para.sendRedirectMsgFp = dnodeSendRedirectMsg;
S
Shengliang Guan 已提交
517

S
Shengliang Guan 已提交
518 519 520 521 522
  tsMnode.pMnode = mnodeCreate(para);
  if (tsMnode.pMnode != NULL) {
    return -1;
  }
  return 0;
S
Shengliang Guan 已提交
523
}
S
Shengliang Guan 已提交
524

S
Shengliang Guan 已提交
525
static void dnodeCleanupMnodeModule() { mnodeDrop(NULL); }
S
Shengliang Guan 已提交
526 527 528 529 530 531 532 533 534 535

static bool dnodeNeedDeployMnode() {
  if (dnodeGetDnodeId() > 0) return false;
  if (dnodeGetClusterId() > 0) return false;
  if (strcmp(tsFirst, tsLocalEp) != 0) return false;
  return true;
}

static int32_t dnodeOpenMnode() {
  int32_t code = dnodeReadMnodeFile();
S
Shengliang Guan 已提交
536
  if (code != 0) {
S
Shengliang Guan 已提交
537
    dError("failed to read open mnode since %s", tstrerror(code));
S
Shengliang Guan 已提交
538 539 540 541
    return code;
  }

  if (tsMnode.dropped) {
S
Shengliang Guan 已提交
542 543
    dInfo("mnode already dropped, undeploy it");
    return dnodeUnDeployMnode();
S
Shengliang Guan 已提交
544 545 546 547
  }

  if (!tsMnode.deployed) {
    bool needDeploy = dnodeNeedDeployMnode();
S
Shengliang Guan 已提交
548 549 550 551 552 553
    if (!needDeploy) return 0;

    dInfo("start to deploy mnode");
    SMnodeCfg cfg = {.replica = 1};
    cfg.replicas[0].port = tsServerPort;
    tstrncpy(cfg.replicas[0].fqdn, tsLocalFqdn, TSDB_FQDN_LEN);
554
    code = dnodeDeployMnode(&cfg);
S
Shengliang Guan 已提交
555 556 557
  } else {
    dInfo("start to open mnode");
    return dnodeStartMnode();
S
Shengliang Guan 已提交
558 559 560
  }
}

S
Shengliang Guan 已提交
561 562 563
static void dnodeCloseMnode() {
  if (dnodeAcquireMnode() == 0) {
    dnodeStopMnode();
S
Shengliang Guan 已提交
564
  }
S
Shengliang Guan 已提交
565
}
S
Shengliang Guan 已提交
566

S
Shengliang Guan 已提交
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
int32_t dnodeInitMnode() {
  dInfo("dnode-mnode start to init");

  SSteps *pSteps = taosStepInit(6, dnodeReportStartup);
  taosStepAdd(pSteps, "dnode-mnode-env", dnodeInitMnodeModule, dnodeCleanupMnodeModule);
  taosStepAdd(pSteps, "dnode-mnode-mgmt", dnodeInitMnodeMgmtWorker, dnodeCleanupMnodeMgmtWorker);
  taosStepAdd(pSteps, "dnode-mnode-read", dnodeInitMnodeReadWorker, dnodeCleanupMnodeReadWorker);
  taosStepAdd(pSteps, "dnode-mnode-write", dnodeInitMnodeWriteWorker, dnodeCleanupMnodeWriteWorker);
  taosStepAdd(pSteps, "dnode-mnode-sync", dnodeInitMnodeSyncWorker, dnodeCleanupMnodeSyncWorker);
  taosStepAdd(pSteps, "dnode-mnode", dnodeOpenMnode, dnodeCloseMnode);

  tsMnode.pSteps = pSteps;
  int32_t code = taosStepExec(pSteps);

  if (code != 0) {
    dError("dnode-mnode init failed since %s", tstrerror(code));
  } else {
    dInfo("dnode-mnode is initialized");
  }
S
Shengliang Guan 已提交
586 587
}

S
Shengliang Guan 已提交
588 589 590 591 592 593
void dnodeCleanupMnode() {
  if (tsMnode.pSteps == NULL) {
    dInfo("dnode-mnode start to clean up");
    taosStepCleanup(tsMnode.pSteps);
    tsMnode.pSteps = NULL;
    dInfo("dnode-mnode is cleaned up");
S
Shengliang Guan 已提交
594 595
  }
}
S
Shengliang Guan 已提交
596

S
Shengliang Guan 已提交
597
int32_t dnodeGetUserAuthFromMnode(char *user, char *spi, char *encrypt, char *secret, char *ckey) {
S
Shengliang Guan 已提交
598 599
  SMnode *pMnode = dnodeAcquireMnode();
  if (pMnode == NULL) {
S
Shengliang Guan 已提交
600
    dTrace("failed to get user auth since mnode not deployed");
S
Shengliang Guan 已提交
601 602
    terrno = TSDB_CODE_DND_MNODE_NOT_DEPLOYED;
    return -1;
S
Shengliang Guan 已提交
603 604
  }

S
Shengliang Guan 已提交
605 606
  int32_t code = mnodeRetriveAuth(pMnode, user, spi, encrypt, secret, ckey);
  dnodeReleaseMnode(pMnode);
S
Shengliang Guan 已提交
607
  return code;
S
Shengliang Guan 已提交
608
}