mmInt.c 5.9 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) {
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;
47 48
  pReplica->port = tsServerPort;
  tstrncpy(pReplica->fqdn, tsLocalFqdn, 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);
73
    if (pReplica->id == pMgmt->pData->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
Shengliang Guan 已提交
110 111 112 113 114 115
  if (pMgmt->pMnode != NULL) {
    mmStopWorker(pMgmt);
    mndClose(pMgmt->pMnode);
    pMgmt->pMnode = NULL;
  }

wafwerar's avatar
wafwerar 已提交
116
  taosMemoryFree(pMgmt);
S
shm  
Shengliang Guan 已提交
117 118
}

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

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

131
  pMgmt->pData = pInput->pData;
S
Shengliang 已提交
132 133 134 135 136 137 138 139
  pMgmt->path = pInput->path;
  pMgmt->name = pInput->name;
  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 已提交
140

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

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

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

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

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

181
  pInput->pData->dnodeId = pMgmt->pData->dnodeId;
S
Shengliang 已提交
182
  pOutput->pMgmt = pMgmt;
S
Shengliang Guan 已提交
183 184
  return 0;
}
S
shm  
Shengliang Guan 已提交
185

S
Shengliang 已提交
186
static int32_t mmStart(SMnodeMgmt *pMgmt) {
S
Shengliang Guan 已提交
187
  dDebug("mnode-mgmt start to run");
S
shm  
Shengliang Guan 已提交
188 189 190
  return mndStart(pMgmt->pMnode);
}

S
Shengliang 已提交
191
static void mmStop(SMnodeMgmt *pMgmt) {
S
Shengliang Guan 已提交
192
  dDebug("mnode-mgmt start to stop");
S
Shengliang 已提交
193
  mndStop(pMgmt->pMnode);
S
Shengliang Guan 已提交
194 195
}

S
Shengliang 已提交
196 197 198 199 200 201 202 203 204 205 206 207
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 已提交
208
}