mmInt.c 6.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
shm  
Shengliang Guan 已提交
18

S
shm  
Shengliang Guan 已提交
19 20 21 22 23
static bool mmDeployRequired(SDnode *pDnode) {
  if (pDnode->dnodeId > 0) return false;
  if (pDnode->clusterId > 0) return false;
  if (strcmp(pDnode->localEp, pDnode->firstEp) != 0) return false;
  return true;
S
shm  
Shengliang Guan 已提交
24 25
}

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

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

  return 0;
}

static void mmInitOption(SMnodeMgmt *pMgmt, SMnodeOpt *pOption) {
  SDnode *pDnode = pMgmt->pDnode;
S
shm  
Shengliang Guan 已提交
42 43 44
  pOption->pWrapper = pMgmt->pWrapper;
  pOption->putToWriteQFp = mmPutMsgToWriteQueue;
  pOption->putToReadQFp = mmPutMsgToReadQueue;
S
shm  
Shengliang Guan 已提交
45 46 47
  pOption->sendReqFp = dndSendReqToDnode;
  pOption->sendMnodeReqFp = dndSendReqToMnode;
  pOption->sendRspFp = dndSendRsp;
S
shm  
Shengliang Guan 已提交
48 49
  pOption->dnodeId = pDnode->dnodeId;
  pOption->clusterId = pDnode->clusterId;
S
shm  
Shengliang Guan 已提交
50 51 52 53 54 55 56 57 58 59
}

static void mmBuildOptionForDeploy(SMnodeMgmt *pMgmt, SMnodeOpt *pOption) {
  SDnode *pDnode = pMgmt->pDnode;

  mmInitOption(pMgmt, pOption);
  pOption->replica = 1;
  pOption->selfIndex = 0;
  SReplica *pReplica = &pOption->replicas[0];
  pReplica->id = 1;
S
shm  
Shengliang Guan 已提交
60 61
  pReplica->port = pDnode->serverPort;
  memcpy(pReplica->fqdn, pDnode->localFqdn, TSDB_FQDN_LEN);
S
shm  
Shengliang Guan 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74

  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 已提交
75
static int32_t mmBuildOptionFromReq(SMnodeMgmt *pMgmt, SMnodeOpt *pOption, SDCreateMnodeReq *pCreate) {
S
shm  
Shengliang Guan 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
  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);
    if (pReplica->id == pOption->dnodeId) {
      pOption->selfIndex = i;
    }
  }

  if (pOption->selfIndex == -1) {
    dError("failed to build mnode options since %s", terrstr());
    return -1;
  }

  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 103 104 105 106 107 108 109 110 111 112
static int32_t mmOpenImp(SMnodeMgmt *pMgmt, SDCreateMnodeReq *pReq) {
  SMnodeOpt option = {0};
  if (pReq != NULL) {
    if (mmBuildOptionFromReq(pMgmt, &option, pReq) != 0) {
      return -1;
    }
  } else {
    bool deployed = false;
    if (mmReadFile(pMgmt, &deployed) != 0) {
      dError("failed to read file since %s", terrstr());
      return -1;
    }
S
shm  
Shengliang Guan 已提交
113

S
shm  
Shengliang Guan 已提交
114 115 116 117 118 119 120
    if (!deployed) {
      dInfo("mnode start to deploy");
      mmBuildOptionForDeploy(pMgmt, &option);
    } else {
      dInfo("mnode start to open");
      mmBuildOptionForOpen(pMgmt, &option);
    }
S
shm  
Shengliang Guan 已提交
121 122
  }

S
shm  
Shengliang Guan 已提交
123 124 125 126
  pMgmt->pMnode = mndOpen(pMgmt->path, &option);
  if (pMgmt->pMnode == NULL) {
    dError("failed to open mnode since %s", terrstr());
    return -1;
S
shm  
Shengliang Guan 已提交
127 128
  }

S
shm  
Shengliang Guan 已提交
129 130 131
  if (mmStartWorker(pMgmt) != 0) {
    dError("failed to start mnode worker since %s", terrstr());
    return -1;
S
shm  
Shengliang Guan 已提交
132 133
  }

S
shm  
Shengliang Guan 已提交
134 135 136 137
  bool deployed = true;
  if (dndWriteFile(pMgmt->pWrapper, deployed) != 0) {
    dError("failed to write mnode file since %s", terrstr());
    return -1;
S
shm  
Shengliang Guan 已提交
138 139
  }

S
shm  
Shengliang Guan 已提交
140
  return 0;
S
shm  
Shengliang Guan 已提交
141 142
}

S
shm  
Shengliang Guan 已提交
143 144 145 146 147
static void mmCloseImp(SMnodeMgmt *pMgmt) {
  if (pMgmt->pMnode != NULL) {
    mmStopWorker(pMgmt);
    mndClose(pMgmt->pMnode);
    pMgmt->pMnode = NULL;
S
shm  
Shengliang Guan 已提交
148
  }
S
shm  
Shengliang Guan 已提交
149
}
S
shm  
Shengliang Guan 已提交
150

S
shm  
Shengliang Guan 已提交
151 152 153 154 155 156
int32_t mmAlter(SMnodeMgmt *pMgmt, SDAlterMnodeReq *pReq) {
  SMnodeOpt option = {0};
  if (pReq != NULL) {
    if (mmBuildOptionFromReq(pMgmt, &option, pReq) != 0) {
      return -1;
    }
S
shm  
Shengliang Guan 已提交
157 158
  }

S
shm  
Shengliang Guan 已提交
159 160 161 162 163 164 165 166 167 168 169 170
  return mndAlter(pMgmt->pMnode, &option);
}

int32_t mmDrop(SMgmtWrapper *pWrapper) {
  SMnodeMgmt *pMgmt = pWrapper->pMgmt;
  if (pMgmt == NULL) return 0;

  dInfo("mnode-mgmt start to drop");
  bool deployed = false;
  if (mmWriteFile(pMgmt, deployed) != 0) {
    dError("failed to drop mnode since %s", terrstr());
    return -1;
S
shm  
Shengliang Guan 已提交
171 172
  }

S
shm  
Shengliang Guan 已提交
173 174 175 176 177 178
  mmCloseImp(pMgmt);
  taosRemoveDir(pMgmt->path);
  pWrapper->pMgmt = NULL;
  free(pMgmt);
  dInfo("mnode-mgmt is dropped");
  return 0;
S
shm  
Shengliang Guan 已提交
179 180
}

S
shm  
Shengliang Guan 已提交
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
static void mmClose(SMgmtWrapper *pWrapper) {
  SMnodeMgmt *pMgmt = pWrapper->pMgmt;
  if (pMgmt == NULL) return;

  dInfo("mnode-mgmt start to cleanup");
  mmCloseImp(pMgmt);
  pWrapper->pMgmt = NULL;
  free(pMgmt);
  dInfo("mnode-mgmt is cleaned up");
}

int32_t mmOpenFromMsg(SMgmtWrapper *pWrapper, SDCreateMnodeReq *pReq) {
  dInfo("mnode-mgmt start to init");
  if (walInit() != 0) {
    dError("failed to init wal since %s", terrstr());
S
shm  
Shengliang Guan 已提交
196
    return -1;
S
shm  
Shengliang Guan 已提交
197 198
  }

S
shm  
Shengliang Guan 已提交
199 200 201
  SMnodeMgmt *pMgmt = calloc(1, sizeof(SMnodeMgmt));
  if (pMgmt == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
S
shm  
Shengliang Guan 已提交
202
    return -1;
S
shm  
Shengliang Guan 已提交
203 204
  }

S
shm  
Shengliang Guan 已提交
205 206 207 208
  pMgmt->path = pWrapper->path;
  pMgmt->pDnode = pWrapper->pDnode;
  pMgmt->pWrapper = pWrapper;
  pWrapper->pMgmt = pMgmt;
S
shm  
Shengliang Guan 已提交
209

S
shm  
Shengliang Guan 已提交
210 211 212 213 214 215
  int32_t code = mmOpenImp(pMgmt, pReq);
  if (code != 0) {
    dError("failed to init mnode-mgmt since %s", terrstr());
    mmClose(pWrapper);
  } else {
    dInfo("mnode-mgmt is initialized");
S
shm  
Shengliang Guan 已提交
216 217
  }

S
shm  
Shengliang Guan 已提交
218 219 220 221 222
  return code;
}

static int32_t mmOpen(SMgmtWrapper *pWrapper) {
  return mmOpenFromMsg(pWrapper, NULL);
S
shm  
Shengliang Guan 已提交
223
}
S
shm  
Shengliang Guan 已提交
224

S
shm  
Shengliang Guan 已提交
225
void mmGetMgmtFp(SMgmtWrapper *pWrapper) {
S
shm  
Shengliang Guan 已提交
226
  SMgmtFp mgmtFp = {0};
S
shm  
Shengliang Guan 已提交
227 228 229 230
  mgmtFp.openFp = mmOpen;
  mgmtFp.closeFp = mmClose;
  mgmtFp.createMsgFp = mmProcessCreateReq;
  mgmtFp.dropMsgFp = mmProcessDropReq;
S
shm  
Shengliang Guan 已提交
231
  mgmtFp.requiredFp = mmRequire;
S
shm  
Shengliang Guan 已提交
232 233 234 235

  mmInitMsgHandles(pWrapper);
  pWrapper->name = "mnode";
  pWrapper->fp = mgmtFp;
S
shm  
Shengliang Guan 已提交
236 237 238
}

int32_t mmGetUserAuth(SMgmtWrapper *pWrapper, char *user, char *spi, char *encrypt, char *secret, char *ckey) {
S
shm  
Shengliang Guan 已提交
239 240
  SMnodeMgmt *pMgmt = pWrapper->pMgmt;

S
shm  
Shengliang Guan 已提交
241
  int32_t code = mndRetriveAuth(pMgmt->pMnode, user, spi, encrypt, secret, ckey);
S
shm  
Shengliang Guan 已提交
242 243
  dTrace("user:%s, retrieve auth spi:%d encrypt:%d", user, *spi, *encrypt);
  return code;
S
shm  
Shengliang Guan 已提交
244
}
S
shm  
Shengliang Guan 已提交
245

S
shm  
Shengliang Guan 已提交
246
int32_t mmGetMonitorInfo(SMgmtWrapper *pWrapper, SMonClusterInfo *pClusterInfo, SMonVgroupInfo *pVgroupInfo,
S
shm  
Shengliang Guan 已提交
247
                         SMonGrantInfo *pGrantInfo) {
S
shm  
Shengliang Guan 已提交
248
  SMnodeMgmt *pMgmt = pWrapper->pMgmt;
S
shm  
Shengliang Guan 已提交
249
  return mndGetMonitorInfo(pMgmt->pMnode, pClusterInfo, pVgroupInfo, pGrantInfo);
S
shm  
Shengliang Guan 已提交
250
}