mmInt.c 4.5 KB
Newer Older
S
shm  
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 "mmInt.h"
S
xshm  
Shengliang Guan 已提交
18
#include "wal.h"
S
shm  
Shengliang Guan 已提交
19

S
Shengliang 已提交
20
static bool mmDeployRequired(const SMgmtInputOpt *pInput) {
S
Shengliang Guan 已提交
21 22
  if (pInput->pData->dnodeId > 0) return false;
  if (pInput->pData->clusterId > 0) return false;
23
  if (strcmp(tsLocalEp, tsFirst) != 0) return false;
S
shm  
Shengliang Guan 已提交
24
  return true;
S
shm  
Shengliang Guan 已提交
25 26
}

S
Shengliang 已提交
27
static int32_t mmRequire(const SMgmtInputOpt *pInput, bool *required) {
28 29
  SMnodeOpt option = {0};
  if (mmReadFile(pInput->path, &option) != 0) {
S
shm  
Shengliang Guan 已提交
30 31 32
    return -1;
  }

33
  if (!option.deploy) {
S
Shengliang 已提交
34
    *required = mmDeployRequired(pInput);
35 36
  } else {
    *required = true;
S
shm  
Shengliang Guan 已提交
37 38 39 40 41
  }

  return 0;
}

S
Shengliang 已提交
42
static void mmBuildOptionForDeploy(SMnodeMgmt *pMgmt, const SMgmtInputOpt *pInput, SMnodeOpt *pOption) {
S
Shengliang Guan 已提交
43
  pOption->deploy = true;
S
Shengliang 已提交
44
  pOption->msgCb = pMgmt->msgCb;
45
  pOption->dnodeId = pMgmt->pData->dnodeId;
46 47 48 49 50
  pOption->selfIndex = 0;
  pOption->numOfReplicas = 1;
  pOption->replicas[0].id = 1;
  pOption->replicas[0].port = tsServerPort;
  tstrncpy(pOption->replicas[0].fqdn, tsLocalFqdn, TSDB_FQDN_LEN);
S
shm  
Shengliang Guan 已提交
51 52
}

53
static void mmBuildOptionForOpen(SMnodeMgmt *pMgmt, SMnodeOpt *pOption) {
S
Shengliang Guan 已提交
54
  pOption->deploy = false;
55 56
  pOption->msgCb = pMgmt->msgCb;
  pOption->dnodeId = pMgmt->pData->dnodeId;
S
shm  
Shengliang Guan 已提交
57 58
}

S
Shengliang 已提交
59
static void mmClose(SMnodeMgmt *pMgmt) {
S
Shengliang Guan 已提交
60 61 62
  if (pMgmt->pMnode != NULL) {
    mmStopWorker(pMgmt);
    mndClose(pMgmt->pMnode);
63
    taosThreadRwlockDestroy(&pMgmt->lock);
S
Shengliang Guan 已提交
64 65 66
    pMgmt->pMnode = NULL;
  }

wafwerar's avatar
wafwerar 已提交
67
  taosMemoryFree(pMgmt);
S
shm  
Shengliang Guan 已提交
68 69
}

S
Shengliang Guan 已提交
70
static int32_t mmOpen(SMgmtInputOpt *pInput, SMgmtOutputOpt *pOutput) {
S
shm  
Shengliang Guan 已提交
71 72
  if (walInit() != 0) {
    dError("failed to init wal since %s", terrstr());
S
shm  
Shengliang Guan 已提交
73
    return -1;
S
shm  
Shengliang Guan 已提交
74 75
  }

M
Minghao Li 已提交
76 77 78 79 80
  if (syncInit() != 0) {
    dError("failed to init sync since %s", terrstr());
    return -1;
  }

wafwerar's avatar
wafwerar 已提交
81
  SMnodeMgmt *pMgmt = taosMemoryCalloc(1, sizeof(SMnodeMgmt));
S
shm  
Shengliang Guan 已提交
82 83
  if (pMgmt == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
S
shm  
Shengliang Guan 已提交
84
    return -1;
S
shm  
Shengliang Guan 已提交
85 86
  }

87
  pMgmt->pData = pInput->pData;
S
Shengliang 已提交
88 89 90
  pMgmt->path = pInput->path;
  pMgmt->name = pInput->name;
  pMgmt->msgCb = pInput->msgCb;
S
Shengliang Guan 已提交
91
  pMgmt->msgCb.putToQueueFp = (PutToQueueFp)mmPutMsgToQueue;
92
  pMgmt->msgCb.mgmt = pMgmt;
93
  taosThreadRwlockInit(&pMgmt->lock, NULL);
S
shm  
Shengliang Guan 已提交
94

95 96
  SMnodeOpt option = {0};
  if (mmReadFile(pMgmt->path, &option) != 0) {
S
Shengliang Guan 已提交
97
    dError("failed to read file since %s", terrstr());
S
Shengliang 已提交
98
    mmClose(pMgmt);
S
Shengliang Guan 已提交
99 100 101
    return -1;
  }

102
  if (!option.deploy) {
S
Shengliang Guan 已提交
103
    dInfo("mnode start to deploy");
104
    pMgmt->pData->dnodeId = 1;
S
Shengliang 已提交
105
    mmBuildOptionForDeploy(pMgmt, pInput, &option);
S
shm  
Shengliang Guan 已提交
106
  } else {
S
Shengliang Guan 已提交
107
    dInfo("mnode start to open");
108
    mmBuildOptionForOpen(pMgmt, &option);
S
shm  
Shengliang Guan 已提交
109 110
  }

S
Shengliang Guan 已提交
111 112 113
  pMgmt->pMnode = mndOpen(pMgmt->path, &option);
  if (pMgmt->pMnode == NULL) {
    dError("failed to open mnode since %s", terrstr());
S
Shengliang 已提交
114
    mmClose(pMgmt);
S
Shengliang Guan 已提交
115 116
    return -1;
  }
S
Shengliang 已提交
117
  tmsgReportStartup("mnode-impl", "initialized");
S
shm  
Shengliang Guan 已提交
118

S
Shengliang Guan 已提交
119 120
  if (mmStartWorker(pMgmt) != 0) {
    dError("failed to start mnode worker since %s", terrstr());
S
Shengliang 已提交
121
    mmClose(pMgmt);
S
Shengliang Guan 已提交
122 123
    return -1;
  }
S
Shengliang 已提交
124
  tmsgReportStartup("mnode-worker", "initialized");
S
Shengliang Guan 已提交
125

126 127 128 129
  if (option.numOfReplicas > 0) {
    option.deploy = true;
    option.numOfReplicas = 0;
    if (mmWriteFile(pMgmt->path, &option) != 0) {
S
Shengliang Guan 已提交
130 131 132 133 134
      dError("failed to write mnode file since %s", terrstr());
      return -1;
    }
  }

135
  pInput->pData->dnodeId = pMgmt->pData->dnodeId;
S
Shengliang 已提交
136
  pOutput->pMgmt = pMgmt;
S
Shengliang Guan 已提交
137 138
  return 0;
}
S
shm  
Shengliang Guan 已提交
139

S
Shengliang 已提交
140
static int32_t mmStart(SMnodeMgmt *pMgmt) {
S
Shengliang Guan 已提交
141
  dDebug("mnode-mgmt start to run");
S
shm  
Shengliang Guan 已提交
142 143 144
  return mndStart(pMgmt->pMnode);
}

S
Shengliang 已提交
145
static void mmStop(SMnodeMgmt *pMgmt) {
S
Shengliang Guan 已提交
146
  dDebug("mnode-mgmt start to stop");
147
  mndPreClose(pMgmt->pMnode);
148 149 150 151
  taosThreadRwlockWrlock(&pMgmt->lock);
  pMgmt->stopped = 1;
  taosThreadRwlockUnlock(&pMgmt->lock);

S
Shengliang 已提交
152
  mndStop(pMgmt->pMnode);
S
Shengliang Guan 已提交
153 154
}

S
Shengliang 已提交
155 156 157 158 159 160 161 162 163 164 165 166
SMgmtFunc mmGetMgmtFunc() {
  SMgmtFunc mgmtFunc = {0};
  mgmtFunc.openFp = mmOpen;
  mgmtFunc.closeFp = (NodeCloseFp)mmClose;
  mgmtFunc.startFp = (NodeStartFp)mmStart;
  mgmtFunc.stopFp = (NodeStopFp)mmStop;
  mgmtFunc.createFp = (NodeCreateFp)mmProcessCreateReq;
  mgmtFunc.dropFp = (NodeDropFp)mmProcessDropReq;
  mgmtFunc.requiredFp = mmRequire;
  mgmtFunc.getHandlesFp = mmGetMsgHandles;

  return mgmtFunc;
S
shm  
Shengliang Guan 已提交
167
}