mmInt.c 5.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
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
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
shm  
Shengliang Guan 已提交
46 47
  pOption->replica = 1;
  pOption->selfIndex = 0;
S
Shengliang Guan 已提交
48

S
shm  
Shengliang Guan 已提交
49 50
  SReplica *pReplica = &pOption->replicas[0];
  pReplica->id = 1;
51 52
  pReplica->port = tsServerPort;
  tstrncpy(pReplica->fqdn, tsLocalFqdn, TSDB_FQDN_LEN);
S
shm  
Shengliang Guan 已提交
53 54 55
}

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

S
Shengliang Guan 已提交
61 62
  if (pMgmt->replica > 0) {
    pOption->standby = true;
63 64 65 66
    pOption->replica = 1;
    pOption->selfIndex = 0;
    SReplica *pReplica = &pOption->replicas[0];
    for (int32_t i = 0; i < pMgmt->replica; ++i) {
S
Shengliang Guan 已提交
67 68 69 70
      if (pMgmt->replicas[i].id != pMgmt->pData->dnodeId) continue;
      pReplica->id = pMgmt->replicas[i].id;
      pReplica->port = pMgmt->replicas[i].port;
      memcpy(pReplica->fqdn, pMgmt->replicas[i].fqdn, TSDB_FQDN_LEN);
S
Shengliang Guan 已提交
71 72
    }
  }
S
shm  
Shengliang Guan 已提交
73 74
}

S
Shengliang 已提交
75
static void mmClose(SMnodeMgmt *pMgmt) {
S
Shengliang Guan 已提交
76 77 78
  if (pMgmt->pMnode != NULL) {
    mmStopWorker(pMgmt);
    mndClose(pMgmt->pMnode);
79
    taosThreadRwlockDestroy(&pMgmt->lock);
S
Shengliang Guan 已提交
80 81 82
    pMgmt->pMnode = NULL;
  }

wafwerar's avatar
wafwerar 已提交
83
  taosMemoryFree(pMgmt);
S
shm  
Shengliang Guan 已提交
84 85
}

S
Shengliang Guan 已提交
86
static int32_t mmOpen(SMgmtInputOpt *pInput, SMgmtOutputOpt *pOutput) {
S
shm  
Shengliang Guan 已提交
87 88
  if (walInit() != 0) {
    dError("failed to init wal since %s", terrstr());
S
shm  
Shengliang Guan 已提交
89
    return -1;
S
shm  
Shengliang Guan 已提交
90 91
  }

M
Minghao Li 已提交
92 93 94 95 96
  if (syncInit() != 0) {
    dError("failed to init sync since %s", terrstr());
    return -1;
  }

wafwerar's avatar
wafwerar 已提交
97
  SMnodeMgmt *pMgmt = taosMemoryCalloc(1, sizeof(SMnodeMgmt));
S
shm  
Shengliang Guan 已提交
98 99
  if (pMgmt == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
S
shm  
Shengliang Guan 已提交
100
    return -1;
S
shm  
Shengliang Guan 已提交
101 102
  }

103
  pMgmt->pData = pInput->pData;
S
Shengliang 已提交
104 105 106
  pMgmt->path = pInput->path;
  pMgmt->name = pInput->name;
  pMgmt->msgCb = pInput->msgCb;
S
Shengliang Guan 已提交
107
  pMgmt->msgCb.putToQueueFp = (PutToQueueFp)mmPutMsgToQueue;
108
  pMgmt->msgCb.mgmt = pMgmt;
109
  taosThreadRwlockInit(&pMgmt->lock, NULL);
S
shm  
Shengliang Guan 已提交
110

S
Shengliang Guan 已提交
111 112 113
  bool deployed = false;
  if (mmReadFile(pMgmt, &deployed) != 0) {
    dError("failed to read file since %s", terrstr());
S
Shengliang 已提交
114
    mmClose(pMgmt);
S
Shengliang Guan 已提交
115 116 117 118 119 120
    return -1;
  }

  SMnodeOpt option = {0};
  if (!deployed) {
    dInfo("mnode start to deploy");
121
    pMgmt->pData->dnodeId = 1;
S
Shengliang 已提交
122
    mmBuildOptionForDeploy(pMgmt, pInput, &option);
S
shm  
Shengliang Guan 已提交
123
  } else {
S
Shengliang Guan 已提交
124 125
    dInfo("mnode start to open");
    mmBuildOptionForOpen(pMgmt, &option);
S
shm  
Shengliang Guan 已提交
126 127
  }

S
Shengliang Guan 已提交
128 129 130
  pMgmt->pMnode = mndOpen(pMgmt->path, &option);
  if (pMgmt->pMnode == NULL) {
    dError("failed to open mnode since %s", terrstr());
S
Shengliang 已提交
131
    mmClose(pMgmt);
S
Shengliang Guan 已提交
132 133
    return -1;
  }
S
Shengliang 已提交
134
  tmsgReportStartup("mnode-impl", "initialized");
S
shm  
Shengliang Guan 已提交
135

S
Shengliang Guan 已提交
136 137
  if (mmStartWorker(pMgmt) != 0) {
    dError("failed to start mnode worker since %s", terrstr());
S
Shengliang 已提交
138
    mmClose(pMgmt);
S
Shengliang Guan 已提交
139 140
    return -1;
  }
S
Shengliang 已提交
141
  tmsgReportStartup("mnode-worker", "initialized");
S
Shengliang Guan 已提交
142

S
Shengliang Guan 已提交
143 144
  if (!deployed || pMgmt->replica > 0) {
    pMgmt->replica = 0;
S
Shengliang Guan 已提交
145
    deployed = true;
S
Shengliang 已提交
146
    if (mmWriteFile(pMgmt, NULL, deployed) != 0) {
S
Shengliang Guan 已提交
147 148 149 150 151
      dError("failed to write mnode file since %s", terrstr());
      return -1;
    }
  }

152
  pInput->pData->dnodeId = pMgmt->pData->dnodeId;
S
Shengliang 已提交
153
  pOutput->pMgmt = pMgmt;
S
Shengliang Guan 已提交
154 155
  return 0;
}
S
shm  
Shengliang Guan 已提交
156

S
Shengliang 已提交
157
static int32_t mmStart(SMnodeMgmt *pMgmt) {
S
Shengliang Guan 已提交
158
  dDebug("mnode-mgmt start to run");
S
shm  
Shengliang Guan 已提交
159 160 161
  return mndStart(pMgmt->pMnode);
}

S
Shengliang 已提交
162
static void mmStop(SMnodeMgmt *pMgmt) {
S
Shengliang Guan 已提交
163
  dDebug("mnode-mgmt start to stop");
S
Shengliang 已提交
164
  mndStop(pMgmt->pMnode);
S
Shengliang Guan 已提交
165 166
}

S
Shengliang 已提交
167 168 169 170 171 172 173 174 175 176 177 178
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 已提交
179
}
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198

int32_t mmAcquire(SMnodeMgmt *pMgmt) {
  int32_t code = 0;

  taosThreadRwlockRdlock(&pMgmt->lock);
  if (pMgmt->stopped) {
    code = -1;
  } else {
    atomic_add_fetch_32(&pMgmt->refCount, 1);
  }
  taosThreadRwlockUnlock(&pMgmt->lock);
  return code;
}

void mmRelease(SMnodeMgmt *pMgmt) {
  taosThreadRwlockRdlock(&pMgmt->lock);
  atomic_sub_fetch_32(&pMgmt->refCount, 1);
  taosThreadRwlockUnlock(&pMgmt->lock);
}