mmInt.c 4.6 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
Shengliang Guan 已提交
30
  if (mmReadFile(&mgmt, NULL, 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
static void mmBuildOptionForDeploy(SMnodeMgmt *pMgmt, const SMgmtInputOpt *pInput, SMnodeOpt *pOption) {
S
Shengliang Guan 已提交
42 43
  pOption->standby = false;
  pOption->deploy = true;
S
Shengliang 已提交
44
  pOption->msgCb = pMgmt->msgCb;
45
  pOption->dnodeId = pMgmt->pData->dnodeId;
S
Shengliang Guan 已提交
46 47 48
  pOption->replica.id = 1;
  pOption->replica.port = tsServerPort;
  tstrncpy(pOption->replica.fqdn, tsLocalFqdn, TSDB_FQDN_LEN);
S
shm  
Shengliang Guan 已提交
49 50
}

S
Shengliang Guan 已提交
51
static void mmBuildOptionForOpen(SMnodeMgmt *pMgmt, const SReplica *pReplica, SMnodeOpt *pOption) {
S
Shengliang Guan 已提交
52
  pOption->standby = false;
S
Shengliang Guan 已提交
53
  pOption->deploy = false;
54 55
  pOption->msgCb = pMgmt->msgCb;
  pOption->dnodeId = pMgmt->pData->dnodeId;
S
Shengliang Guan 已提交
56
  if (pReplica->id > 0) {
S
Shengliang Guan 已提交
57
    pOption->standby = true;
S
Shengliang Guan 已提交
58
    pOption->replica = *pReplica;
S
Shengliang Guan 已提交
59
  }
S
shm  
Shengliang Guan 已提交
60 61
}

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

wafwerar's avatar
wafwerar 已提交
70
  taosMemoryFree(pMgmt);
S
shm  
Shengliang Guan 已提交
71 72
}

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

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

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

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

S
Shengliang Guan 已提交
98 99 100
  bool     deployed = false;
  SReplica replica = {0};
  if (mmReadFile(pMgmt, &replica, &deployed) != 0) {
S
Shengliang Guan 已提交
101
    dError("failed to read file since %s", terrstr());
S
Shengliang 已提交
102
    mmClose(pMgmt);
S
Shengliang Guan 已提交
103 104 105 106 107 108
    return -1;
  }

  SMnodeOpt option = {0};
  if (!deployed) {
    dInfo("mnode start to deploy");
109
    pMgmt->pData->dnodeId = 1;
S
Shengliang 已提交
110
    mmBuildOptionForDeploy(pMgmt, pInput, &option);
S
shm  
Shengliang Guan 已提交
111
  } else {
S
Shengliang Guan 已提交
112
    dInfo("mnode start to open");
S
Shengliang Guan 已提交
113
    mmBuildOptionForOpen(pMgmt, &replica, &option);
S
shm  
Shengliang Guan 已提交
114 115
  }

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

S
Shengliang Guan 已提交
124 125
  if (mmStartWorker(pMgmt) != 0) {
    dError("failed to start mnode worker since %s", terrstr());
S
Shengliang 已提交
126
    mmClose(pMgmt);
S
Shengliang Guan 已提交
127 128
    return -1;
  }
S
Shengliang 已提交
129
  tmsgReportStartup("mnode-worker", "initialized");
S
Shengliang Guan 已提交
130

S
Shengliang Guan 已提交
131
  if (!deployed || replica.id > 0) {
S
Shengliang Guan 已提交
132
    deployed = true;
S
Shengliang 已提交
133
    if (mmWriteFile(pMgmt, NULL, deployed) != 0) {
S
Shengliang Guan 已提交
134 135 136 137 138
      dError("failed to write mnode file since %s", terrstr());
      return -1;
    }
  }

139
  pInput->pData->dnodeId = pMgmt->pData->dnodeId;
S
Shengliang 已提交
140
  pOutput->pMgmt = pMgmt;
S
Shengliang Guan 已提交
141 142
  return 0;
}
S
shm  
Shengliang Guan 已提交
143

S
Shengliang 已提交
144
static int32_t mmStart(SMnodeMgmt *pMgmt) {
S
Shengliang Guan 已提交
145
  dDebug("mnode-mgmt start to run");
S
shm  
Shengliang Guan 已提交
146 147 148
  return mndStart(pMgmt->pMnode);
}

S
Shengliang 已提交
149
static void mmStop(SMnodeMgmt *pMgmt) {
S
Shengliang Guan 已提交
150
  dDebug("mnode-mgmt start to stop");
151 152 153 154
  taosThreadRwlockWrlock(&pMgmt->lock);
  pMgmt->stopped = 1;
  taosThreadRwlockUnlock(&pMgmt->lock);

S
Shengliang 已提交
155
  mndStop(pMgmt->pMnode);
S
Shengliang Guan 已提交
156 157
}

S
Shengliang 已提交
158 159 160 161 162 163 164 165 166 167 168 169
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 已提交
170
}