mmInt.c 4.8 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
  pOption->selfIndex = 0;
  pOption->numOfReplicas = 1;
C
cadem 已提交
48
  pOption->numOfTotalReplicas = 1;
49 50 51
  pOption->replicas[0].id = 1;
  pOption->replicas[0].port = tsServerPort;
  tstrncpy(pOption->replicas[0].fqdn, tsLocalFqdn, TSDB_FQDN_LEN);
C
cadem 已提交
52
  pOption->lastIndex = SYNC_INDEX_INVALID;
S
shm  
Shengliang Guan 已提交
53 54
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

C
cadem 已提交
158 159 160 161
static int32_t mmSyncIsCatchUp(SMnodeMgmt *pMgmt) {
  return mndIsCatchUp(pMgmt->pMnode);
}

S
Shengliang 已提交
162 163 164 165 166 167 168 169 170 171
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;
C
cadem 已提交
172
  mgmtFunc.isCatchUpFp = (NodeIsCatchUpFp)mmSyncIsCatchUp;
S
Shengliang 已提交
173 174

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