mmInt.c 6.3 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
shm  
Shengliang Guan 已提交
20
static bool mmDeployRequired(SDnode *pDnode) {
S
Shengliang Guan 已提交
21 22 23
  if (pDnode->data.dnodeId > 0) return false;
  if (pDnode->data.clusterId > 0) return false;
  if (strcmp(pDnode->data.localEp, pDnode->data.firstEp) != 0) return false;
S
shm  
Shengliang Guan 已提交
24
  return true;
S
shm  
Shengliang Guan 已提交
25 26
}

S
shm  
Shengliang Guan 已提交
27 28 29 30
static int32_t mmRequire(SMgmtWrapper *pWrapper, bool *required) {
  SMnodeMgmt mgmt = {0};
  mgmt.path = pWrapper->path;
  if (mmReadFile(&mgmt, required) != 0) {
S
shm  
Shengliang Guan 已提交
31 32 33
    return -1;
  }

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

  return 0;
}

static void mmInitOption(SMnodeMgmt *pMgmt, SMnodeOpt *pOption) {
S
Shengliang Guan 已提交
42
  SMsgCb msgCb = pMgmt->pDnode->data.msgCb;
S
Shengliang Guan 已提交
43
  msgCb.pWrapper = pMgmt->pWrapper;
D
dapan1121 已提交
44 45
  msgCb.queueFps[QUERY_QUEUE] = mmPutMsgToQueryQueue;
  msgCb.queueFps[READ_QUEUE] = mmPutMsgToReadQueue;
S
Shengliang Guan 已提交
46
  msgCb.queueFps[WRITE_QUEUE] = mmPutMsgToWriteQueue;
S
Shengliang Guan 已提交
47
  msgCb.queueFps[SYNC_QUEUE] = mmPutMsgToWriteQueue;
S
Shengliang Guan 已提交
48
  pOption->msgCb = msgCb;
S
shm  
Shengliang Guan 已提交
49 50 51 52 53 54 55 56
}

static void mmBuildOptionForDeploy(SMnodeMgmt *pMgmt, SMnodeOpt *pOption) {
  mmInitOption(pMgmt, pOption);
  pOption->replica = 1;
  pOption->selfIndex = 0;
  SReplica *pReplica = &pOption->replicas[0];
  pReplica->id = 1;
S
Shengliang Guan 已提交
57 58
  pReplica->port = pMgmt->pDnode->data.serverPort;
  tstrncpy(pReplica->fqdn, pMgmt->pDnode->data.localFqdn, TSDB_FQDN_LEN);
S
shm  
Shengliang Guan 已提交
59
  pOption->deploy = true;
S
shm  
Shengliang Guan 已提交
60 61 62 63 64 65 66 67 68 69 70

  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) {
  mmInitOption(pMgmt, pOption);
  pOption->selfIndex = pMgmt->selfIndex;
  pOption->replica = pMgmt->replica;
  memcpy(&pOption->replicas, pMgmt->replicas, sizeof(SReplica) * TSDB_MAX_REPLICA);
S
shm  
Shengliang Guan 已提交
71
  pOption->deploy = false;
S
shm  
Shengliang Guan 已提交
72 73
}

S
shm  
Shengliang Guan 已提交
74
static int32_t mmBuildOptionFromReq(SMnodeMgmt *pMgmt, SMnodeOpt *pOption, SDCreateMnodeReq *pCreate) {
S
shm  
Shengliang Guan 已提交
75 76 77 78 79 80 81 82 83
  mmInitOption(pMgmt, pOption);

  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 Guan 已提交
84
    if (pReplica->id == pMgmt->pDnode->data.dnodeId) {
S
shm  
Shengliang Guan 已提交
85 86 87 88 89 90 91 92
      pOption->selfIndex = i;
    }
  }

  if (pOption->selfIndex == -1) {
    dError("failed to build mnode options since %s", terrstr());
    return -1;
  }
S
shm  
Shengliang Guan 已提交
93
  pOption->deploy = true;
S
shm  
Shengliang Guan 已提交
94 95 96 97 98 99 100

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

S
shm  
Shengliang Guan 已提交
101 102
int32_t mmAlter(SMnodeMgmt *pMgmt, SDAlterMnodeReq *pReq) {
  SMnodeOpt option = {0};
103 104
  if (mmBuildOptionFromReq(pMgmt, &option, pReq) != 0) {
    return -1;
S
shm  
Shengliang Guan 已提交
105
  }
S
Shengliang Guan 已提交
106 107 108 109 110 111 112 113 114 115 116 117

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

  bool deployed = true;
  if (mmWriteFile(pMgmt->pWrapper, pReq, deployed) != 0) {
    dError("failed to write mnode file since %s", terrstr());
    return -1;
  }

  return 0;
S
shm  
Shengliang Guan 已提交
118 119 120 121 122 123 124
}

static void mmClose(SMgmtWrapper *pWrapper) {
  SMnodeMgmt *pMgmt = pWrapper->pMgmt;
  if (pMgmt == NULL) return;

  dInfo("mnode-mgmt start to cleanup");
S
Shengliang Guan 已提交
125 126 127 128 129 130
  if (pMgmt->pMnode != NULL) {
    mmStopWorker(pMgmt);
    mndClose(pMgmt->pMnode);
    pMgmt->pMnode = NULL;
  }

S
shm  
Shengliang Guan 已提交
131
  pWrapper->pMgmt = NULL;
wafwerar's avatar
wafwerar 已提交
132
  taosMemoryFree(pMgmt);
S
shm  
Shengliang Guan 已提交
133 134 135
  dInfo("mnode-mgmt is cleaned up");
}

S
Shengliang Guan 已提交
136
static int32_t mmOpen(SMgmtWrapper *pWrapper) {
S
shm  
Shengliang Guan 已提交
137 138 139
  dInfo("mnode-mgmt start to init");
  if (walInit() != 0) {
    dError("failed to init wal since %s", terrstr());
S
shm  
Shengliang Guan 已提交
140
    return -1;
S
shm  
Shengliang Guan 已提交
141 142
  }

wafwerar's avatar
wafwerar 已提交
143
  SMnodeMgmt *pMgmt = taosMemoryCalloc(1, sizeof(SMnodeMgmt));
S
shm  
Shengliang Guan 已提交
144 145
  if (pMgmt == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
S
shm  
Shengliang Guan 已提交
146
    return -1;
S
shm  
Shengliang Guan 已提交
147 148
  }

S
shm  
Shengliang Guan 已提交
149 150 151 152
  pMgmt->path = pWrapper->path;
  pMgmt->pDnode = pWrapper->pDnode;
  pMgmt->pWrapper = pWrapper;
  pWrapper->pMgmt = pMgmt;
S
shm  
Shengliang Guan 已提交
153

S
Shengliang Guan 已提交
154 155 156
  bool deployed = false;
  if (mmReadFile(pMgmt, &deployed) != 0) {
    dError("failed to read file since %s", terrstr());
S
shm  
Shengliang Guan 已提交
157
    mmClose(pWrapper);
S
Shengliang Guan 已提交
158 159 160 161 162 163 164 165 166 167
    return -1;
  }

  SMnodeOpt option = {0};
  if (!deployed) {
    dInfo("mnode start to deploy");
    if (pWrapper->procType == DND_PROC_CHILD) {
      pWrapper->pDnode->data.dnodeId = 1;
    }
    mmBuildOptionForDeploy(pMgmt, &option);
S
shm  
Shengliang Guan 已提交
168
  } else {
S
Shengliang Guan 已提交
169 170
    dInfo("mnode start to open");
    mmBuildOptionForOpen(pMgmt, &option);
S
shm  
Shengliang Guan 已提交
171 172
  }

S
Shengliang Guan 已提交
173 174 175 176 177 178
  pMgmt->pMnode = mndOpen(pMgmt->path, &option);
  if (pMgmt->pMnode == NULL) {
    dError("failed to open mnode since %s", terrstr());
    mmClose(pWrapper);
    return -1;
  }
S
Shengliang Guan 已提交
179
  dmReportStartup(pWrapper->pDnode, "mnode-impl", "initialized");
S
shm  
Shengliang Guan 已提交
180

S
Shengliang Guan 已提交
181 182 183 184 185
  if (mmStartWorker(pMgmt) != 0) {
    dError("failed to start mnode worker since %s", terrstr());
    mmClose(pWrapper);
    return -1;
  }
S
Shengliang Guan 已提交
186
  dmReportStartup(pWrapper->pDnode, "mnode-worker", "initialized");
S
Shengliang Guan 已提交
187

S
Shengliang Guan 已提交
188 189 190 191 192 193 194 195
  if (!deployed) {
    deployed = true;
    if (mmWriteFile(pWrapper, NULL, deployed) != 0) {
      dError("failed to write mnode file since %s", terrstr());
      return -1;
    }
  }

S
Shengliang Guan 已提交
196 197 198
  dInfo("mnode-mgmt is initialized");
  return 0;
}
S
shm  
Shengliang Guan 已提交
199

S
shm  
Shengliang Guan 已提交
200
static int32_t mmStart(SMgmtWrapper *pWrapper) {
S
Shengliang Guan 已提交
201
  dDebug("mnode-mgmt start to run");
S
shm  
Shengliang Guan 已提交
202 203 204 205
  SMnodeMgmt *pMgmt = pWrapper->pMgmt;
  return mndStart(pMgmt->pMnode);
}

S
Shengliang Guan 已提交
206 207 208
static void mmStop(SMgmtWrapper *pWrapper) {
  dDebug("mnode-mgmt start to stop");
  SMnodeMgmt *pMgmt = pWrapper->pMgmt;
S
Shengliang Guan 已提交
209 210 211
  if (pMgmt != NULL) {
    mndStop(pMgmt->pMnode);
  }
S
Shengliang Guan 已提交
212 213
}

S
Shengliang Guan 已提交
214
void mmSetMgmtFp(SMgmtWrapper *pWrapper) {
S
shm  
Shengliang Guan 已提交
215
  SMgmtFp mgmtFp = {0};
S
shm  
Shengliang Guan 已提交
216 217
  mgmtFp.openFp = mmOpen;
  mgmtFp.closeFp = mmClose;
S
shm  
Shengliang Guan 已提交
218
  mgmtFp.startFp = mmStart;
S
Shengliang Guan 已提交
219
  mgmtFp.stopFp = mmStop;
S
Shengliang Guan 已提交
220 221
  mgmtFp.createFp = mmProcessCreateReq;
  mgmtFp.dropFp = mmProcessDropReq;
S
shm  
Shengliang Guan 已提交
222
  mgmtFp.requiredFp = mmRequire;
S
shm  
Shengliang Guan 已提交
223

S
Shengliang Guan 已提交
224
  mmInitMsgHandle(pWrapper);
S
shm  
Shengliang Guan 已提交
225 226
  pWrapper->name = "mnode";
  pWrapper->fp = mgmtFp;
S
shm  
Shengliang Guan 已提交
227
}