mmInt.c 6.0 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 21 22 23
static bool mmDeployRequired(const SMgmtInputOpt *pInput) {
  if (pInput->dnodeId > 0) return false;
  if (pInput->clusterId > 0) return false;
  if (strcmp(pInput->localEp, pInput->firstEp) != 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) {
S
shm  
Shengliang Guan 已提交
28
  SMnodeMgmt mgmt = {0};
S
Shengliang 已提交
29
  mgmt.path = pInput->path;
S
shm  
Shengliang Guan 已提交
30
  if (mmReadFile(&mgmt, required) != 0) {
S
shm  
Shengliang Guan 已提交
31 32 33
    return -1;
  }

S
shm  
Shengliang Guan 已提交
34
  if (!(*required)) {
S
Shengliang 已提交
35
    *required = mmDeployRequired(pInput);
S
shm  
Shengliang Guan 已提交
36 37 38 39 40
  }

  return 0;
}

S
Shengliang 已提交
41 42
static void mmBuildOptionForDeploy(SMnodeMgmt *pMgmt, const SMgmtInputOpt *pInput, SMnodeOpt *pOption) {
  pOption->msgCb = pMgmt->msgCb;
S
shm  
Shengliang Guan 已提交
43 44 45 46
  pOption->replica = 1;
  pOption->selfIndex = 0;
  SReplica *pReplica = &pOption->replicas[0];
  pReplica->id = 1;
S
Shengliang 已提交
47 48
  pReplica->port = pInput->serverPort;
  tstrncpy(pReplica->fqdn, pInput->localFqdn, TSDB_FQDN_LEN);
S
shm  
Shengliang Guan 已提交
49
  pOption->deploy = true;
S
shm  
Shengliang Guan 已提交
50 51 52 53 54 55 56

  pMgmt->selfIndex = pOption->selfIndex;
  pMgmt->replica = pOption->replica;
  memcpy(&pMgmt->replicas, pOption->replicas, sizeof(SReplica) * TSDB_MAX_REPLICA);
}

static void mmBuildOptionForOpen(SMnodeMgmt *pMgmt, SMnodeOpt *pOption) {
S
Shengliang 已提交
57
  pOption->msgCb = pMgmt->msgCb;
S
shm  
Shengliang Guan 已提交
58 59 60
  pOption->selfIndex = pMgmt->selfIndex;
  pOption->replica = pMgmt->replica;
  memcpy(&pOption->replicas, pMgmt->replicas, sizeof(SReplica) * TSDB_MAX_REPLICA);
S
shm  
Shengliang Guan 已提交
61
  pOption->deploy = false;
S
shm  
Shengliang Guan 已提交
62 63
}

S
shm  
Shengliang Guan 已提交
64
static int32_t mmBuildOptionFromReq(SMnodeMgmt *pMgmt, SMnodeOpt *pOption, SDCreateMnodeReq *pCreate) {
S
Shengliang 已提交
65
  pOption->msgCb = pMgmt->msgCb;
S
shm  
Shengliang Guan 已提交
66 67 68 69 70 71 72
  pOption->replica = pCreate->replica;
  pOption->selfIndex = -1;
  for (int32_t i = 0; i < pCreate->replica; ++i) {
    SReplica *pReplica = &pOption->replicas[i];
    pReplica->id = pCreate->replicas[i].id;
    pReplica->port = pCreate->replicas[i].port;
    memcpy(pReplica->fqdn, pCreate->replicas[i].fqdn, TSDB_FQDN_LEN);
S
Shengliang 已提交
73
    if (pReplica->id == pMgmt->dnodeId) {
S
shm  
Shengliang Guan 已提交
74 75 76 77 78 79 80 81
      pOption->selfIndex = i;
    }
  }

  if (pOption->selfIndex == -1) {
    dError("failed to build mnode options since %s", terrstr());
    return -1;
  }
S
shm  
Shengliang Guan 已提交
82
  pOption->deploy = true;
S
shm  
Shengliang Guan 已提交
83 84 85 86 87 88 89

  pMgmt->selfIndex = pOption->selfIndex;
  pMgmt->replica = pOption->replica;
  memcpy(&pMgmt->replicas, pOption->replicas, sizeof(SReplica) * TSDB_MAX_REPLICA);
  return 0;
}

S
shm  
Shengliang Guan 已提交
90 91
int32_t mmAlter(SMnodeMgmt *pMgmt, SDAlterMnodeReq *pReq) {
  SMnodeOpt option = {0};
92 93
  if (mmBuildOptionFromReq(pMgmt, &option, pReq) != 0) {
    return -1;
S
shm  
Shengliang Guan 已提交
94
  }
S
Shengliang Guan 已提交
95 96 97 98 99 100

  if (mndAlter(pMgmt->pMnode, &option) != 0) {
    return -1;
  }

  bool deployed = true;
S
Shengliang 已提交
101
  if (mmWriteFile(pMgmt, pReq, deployed) != 0) {
S
Shengliang Guan 已提交
102 103 104 105 106
    dError("failed to write mnode file since %s", terrstr());
    return -1;
  }

  return 0;
S
shm  
Shengliang Guan 已提交
107 108
}

S
Shengliang 已提交
109
static void mmClose(SMnodeMgmt *pMgmt) {
S
shm  
Shengliang Guan 已提交
110
  dInfo("mnode-mgmt start to cleanup");
S
Shengliang Guan 已提交
111 112 113 114 115 116
  if (pMgmt->pMnode != NULL) {
    mmStopWorker(pMgmt);
    mndClose(pMgmt->pMnode);
    pMgmt->pMnode = NULL;
  }

wafwerar's avatar
wafwerar 已提交
117
  taosMemoryFree(pMgmt);
S
shm  
Shengliang Guan 已提交
118 119 120
  dInfo("mnode-mgmt is cleaned up");
}

S
Shengliang 已提交
121
static int32_t mmOpen(const SMgmtInputOpt *pInput, SMgmtOutputOpt *pOutput) {
S
shm  
Shengliang Guan 已提交
122 123 124
  dInfo("mnode-mgmt start to init");
  if (walInit() != 0) {
    dError("failed to init wal since %s", terrstr());
S
shm  
Shengliang Guan 已提交
125
    return -1;
S
shm  
Shengliang Guan 已提交
126 127
  }

wafwerar's avatar
wafwerar 已提交
128
  SMnodeMgmt *pMgmt = taosMemoryCalloc(1, sizeof(SMnodeMgmt));
S
shm  
Shengliang Guan 已提交
129 130
  if (pMgmt == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
S
shm  
Shengliang Guan 已提交
131
    return -1;
S
shm  
Shengliang Guan 已提交
132 133
  }

S
Shengliang 已提交
134 135 136 137 138 139 140 141 142
  pMgmt->path = pInput->path;
  pMgmt->name = pInput->name;
  pMgmt->dnodeId = pInput->dnodeId;
  pMgmt->msgCb = pInput->msgCb;
  pMgmt->msgCb.queueFps[QUERY_QUEUE] = (PutToQueueFp)mmPutRpcMsgToQueryQueue;
  pMgmt->msgCb.queueFps[READ_QUEUE] = (PutToQueueFp)mmPutRpcMsgToReadQueue;
  pMgmt->msgCb.queueFps[WRITE_QUEUE] = (PutToQueueFp)mmPutRpcMsgToWriteQueue;
  pMgmt->msgCb.queueFps[SYNC_QUEUE] = (PutToQueueFp)mmPutRpcMsgToWriteQueue;
  pMgmt->msgCb.pMgmt = pMgmt;
S
shm  
Shengliang Guan 已提交
143

S
Shengliang Guan 已提交
144 145 146
  bool deployed = false;
  if (mmReadFile(pMgmt, &deployed) != 0) {
    dError("failed to read file since %s", terrstr());
S
Shengliang 已提交
147
    mmClose(pMgmt);
S
Shengliang Guan 已提交
148 149 150 151 152 153
    return -1;
  }

  SMnodeOpt option = {0};
  if (!deployed) {
    dInfo("mnode start to deploy");
S
Shengliang 已提交
154 155
    pMgmt->dnodeId = 1;
    mmBuildOptionForDeploy(pMgmt, pInput, &option);
S
shm  
Shengliang Guan 已提交
156
  } else {
S
Shengliang Guan 已提交
157 158
    dInfo("mnode start to open");
    mmBuildOptionForOpen(pMgmt, &option);
S
shm  
Shengliang Guan 已提交
159 160
  }

S
Shengliang Guan 已提交
161 162 163
  pMgmt->pMnode = mndOpen(pMgmt->path, &option);
  if (pMgmt->pMnode == NULL) {
    dError("failed to open mnode since %s", terrstr());
S
Shengliang 已提交
164
    mmClose(pMgmt);
S
Shengliang Guan 已提交
165 166
    return -1;
  }
S
Shengliang 已提交
167
  tmsgReportStartup("mnode-impl", "initialized");
S
shm  
Shengliang Guan 已提交
168

S
Shengliang Guan 已提交
169 170
  if (mmStartWorker(pMgmt) != 0) {
    dError("failed to start mnode worker since %s", terrstr());
S
Shengliang 已提交
171
    mmClose(pMgmt);
S
Shengliang Guan 已提交
172 173
    return -1;
  }
S
Shengliang 已提交
174
  tmsgReportStartup("mnode-worker", "initialized");
S
Shengliang Guan 已提交
175

S
Shengliang Guan 已提交
176 177
  if (!deployed) {
    deployed = true;
S
Shengliang 已提交
178
    if (mmWriteFile(pMgmt, NULL, deployed) != 0) {
S
Shengliang Guan 已提交
179 180 181 182 183
      dError("failed to write mnode file since %s", terrstr());
      return -1;
    }
  }

S
Shengliang 已提交
184 185
  pOutput->dnodeId = pMgmt->dnodeId;
  pOutput->pMgmt = pMgmt;
S
Shengliang Guan 已提交
186 187 188
  dInfo("mnode-mgmt is initialized");
  return 0;
}
S
shm  
Shengliang Guan 已提交
189

S
Shengliang 已提交
190
static int32_t mmStart(SMnodeMgmt *pMgmt) {
S
Shengliang Guan 已提交
191
  dDebug("mnode-mgmt start to run");
S
shm  
Shengliang Guan 已提交
192 193 194
  return mndStart(pMgmt->pMnode);
}

S
Shengliang 已提交
195
static void mmStop(SMnodeMgmt *pMgmt) {
S
Shengliang Guan 已提交
196
  dDebug("mnode-mgmt start to stop");
S
Shengliang 已提交
197
  mndStop(pMgmt->pMnode);
S
Shengliang Guan 已提交
198 199
}

S
Shengliang 已提交
200 201 202 203 204 205 206 207 208 209 210 211
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 已提交
212
}