mmInt.c 8.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
shm  
Shengliang Guan 已提交
18 19
#include "dmInt.h"
#include "mmFile.h"
S
rename  
Shengliang Guan 已提交
20
#include "mmMsg.h"
S
shm  
Shengliang Guan 已提交
21
#include "mmWorker.h"
S
shm  
Shengliang Guan 已提交
22

S
shm  
Shengliang Guan 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
SMnode *mmAcquire(SMnodeMgmt *pMgmt) {
  SMnode *pMnode = NULL;
  int32_t refCount = 0;

  taosRLockLatch(&pMgmt->latch);
  if (pMgmt->deployed && !pMgmt->dropped) {
    refCount = atomic_add_fetch_32(&pMgmt->refCount, 1);
    pMnode = pMgmt->pMnode;
  } else {
    terrno = TSDB_CODE_DND_MNODE_NOT_DEPLOYED;
  }
  taosRUnLockLatch(&pMgmt->latch);

  if (pMnode != NULL) {
    dTrace("acquire mnode, refCount:%d", refCount);
  }
  return pMnode;
}

void mmRelease(SMnodeMgmt *pMgmt, SMnode *pMnode) {
  if (pMnode == NULL) return;

  taosRLockLatch(&pMgmt->latch);
  int32_t refCount = atomic_sub_fetch_32(&pMgmt->refCount, 1);
  taosRUnLockLatch(&pMgmt->latch);
  dTrace("release mnode, refCount:%d", refCount);
}

int32_t mmOpen(SMnodeMgmt *pMgmt, SMnodeOpt *pOption) {
S
shm  
Shengliang Guan 已提交
52 53 54 55 56 57
  if (walInit() != 0) {
    dError("failed to init wal since %s", terrstr());
    dndCleanup();
    return -1;
  }

S
shm  
Shengliang Guan 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
  SMnode *pMnode = mndOpen(pMgmt->path, pOption);
  if (pMnode == NULL) {
    dError("failed to open mnode since %s", terrstr());
    return -1;
  }

  if (mmStartWorker(pMgmt) != 0) {
    dError("failed to start mnode worker since %s", terrstr());
    mndClose(pMnode);
    mndDestroy(pMgmt->path);
    return -1;
  }

  pMgmt->deployed = 1;
  if (mmWriteFile(pMgmt) != 0) {
    dError("failed to write mnode file since %s", terrstr());
    pMgmt->deployed = 0;
    mmStopWorker(pMgmt);
    mndClose(pMnode);
    mndDestroy(pMgmt->path);
    return -1;
  }

  taosWLockLatch(&pMgmt->latch);
  pMgmt->pMnode = pMnode;
  pMgmt->deployed = 1;
  taosWUnLockLatch(&pMgmt->latch);

  dInfo("mnode open successfully");
  return 0;
}

int32_t mmAlter(SMnodeMgmt *pMgmt, SMnodeOpt *pOption) {
  SMnode *pMnode = mmAcquire(pMgmt);
  if (pMnode == NULL) {
    dError("failed to alter mnode since %s", terrstr());
    return -1;
  }

  if (mndAlter(pMnode, pOption) != 0) {
    dError("failed to alter mnode since %s", terrstr());
    mmRelease(pMgmt, pMnode);
    return -1;
  }

  mmRelease(pMgmt, pMnode);
  return 0;
}

int32_t mmDrop(SMnodeMgmt *pMgmt) {
  SMnode *pMnode = mmAcquire(pMgmt);
  if (pMnode == NULL) {
    dError("failed to drop mnode since %s", terrstr());
    return -1;
  }

  taosRLockLatch(&pMgmt->latch);
  pMgmt->dropped = 1;
  taosRUnLockLatch(&pMgmt->latch);

  if (mmWriteFile(pMgmt) != 0) {
    taosRLockLatch(&pMgmt->latch);
    pMgmt->dropped = 0;
    taosRUnLockLatch(&pMgmt->latch);

    mmRelease(pMgmt, pMnode);
    dError("failed to drop mnode since %s", terrstr());
    return -1;
  }

  mmRelease(pMgmt, pMnode);
  mmStopWorker(pMgmt);
  pMgmt->deployed = 0;
  mmWriteFile(pMgmt);
  mndClose(pMnode);
  pMgmt->pMnode = NULL;
  mndDestroy(pMgmt->path);

  return 0;
}

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

  pOption->pDnode = pDnode;
  pOption->sendReqToDnodeFp = dndSendReqToDnode;
  pOption->sendReqToMnodeFp = dndSendReqToMnode;
  pOption->sendRedirectRspFp = dmSendRedirectRsp;
  pOption->putReqToMWriteQFp = mmPutMsgToWriteQueue;
  pOption->putReqToMReadQFp = mmPutMsgToReadQueue;
  pOption->dnodeId = dmGetDnodeId(pDnode);
  pOption->clusterId = dmGetClusterId(pDnode);
}

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;
  pReplica->port = pDnode->cfg.serverPort;
  memcpy(pReplica->fqdn, pDnode->cfg.localFqdn, TSDB_FQDN_LEN);

  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);
}

int32_t mmBuildOptionFromReq(SMnodeMgmt *pMgmt, SMnodeOpt *pOption, SDCreateMnodeReq *pCreate) {
  SDnode *pDnode = pMgmt->pDnode;

  mmInitOption(pMgmt, pOption);
  pOption->dnodeId = dmGetDnodeId(pDnode);
  pOption->clusterId = dmGetClusterId(pDnode);

  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;
}

static void mmCleanup(SMgmtWrapper *pWrapper) {
  SMnodeMgmt *pMgmt = pWrapper->pMgmt;
  if (pMgmt == NULL) return;

S
shm  
Shengliang Guan 已提交
209
  dInfo("mnode-mgmt start to cleanup");
S
shm  
Shengliang Guan 已提交
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
  if (pMgmt->pMnode) {
    mmStopWorker(pMgmt);
    mndClose(pMgmt->pMnode);
    pMgmt->pMnode = NULL;
  }
  free(pMgmt);
  pWrapper->pMgmt = NULL;
  dInfo("mnode-mgmt is cleaned up");
}

static int32_t mmInit(SMgmtWrapper *pWrapper) {
  SDnode     *pDnode = pWrapper->pDnode;
  SMnodeMgmt *pMgmt = calloc(1, sizeof(SMnodeMgmt));
  int32_t     code = -1;
  SMnodeOpt   option = {0};

S
shm  
Shengliang Guan 已提交
226
  dInfo("mnode-mgmt start to init");
S
shm  
Shengliang Guan 已提交
227 228
  if (pMgmt == NULL) goto _OVER;

S
shm  
Shengliang Guan 已提交
229 230
  pMgmt->path = pWrapper->path;
  pMgmt->pDnode = pWrapper->pDnode;
S
shm  
Shengliang Guan 已提交
231
  pMgmt->pWrapper = pWrapper;
S
shm  
Shengliang Guan 已提交
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
  taosInitRWLatch(&pMgmt->latch);

  if (mmReadFile(pMgmt) != 0) {
    dError("failed to read file since %s", terrstr());
    goto _OVER;
  }

  if (!pMgmt->deployed) {
    dInfo("mnode start to deploy");
    mmBuildOptionForDeploy(pMgmt, &option);
    code = mmOpen(pMgmt, &option);
  } else {
    dInfo("mnode start to open");
    mmBuildOptionForOpen(pMgmt, &option);
    code = mmOpen(pMgmt, &option);
  }

_OVER:
  if (code == 0) {
S
shm  
Shengliang Guan 已提交
251
    pWrapper->pMgmt = pMgmt;
S
shm  
Shengliang Guan 已提交
252 253
    dInfo("mnode-mgmt is initialized");
  } else {
S
shm  
Shengliang Guan 已提交
254
    dError("failed to init mnode-mgmt since %s", terrstr());
S
shm  
Shengliang Guan 已提交
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
    mmCleanup(pWrapper);
  }

  return code;
}

static bool mmDeployRequired(SDnode *pDnode) {
  if (dmGetDnodeId(pDnode) > 0) {
    return false;
  }

  if (dmGetClusterId(pDnode) > 0) {
    return false;
  }

  if (strcmp(pDnode->cfg.localEp, pDnode->cfg.firstEp) != 0) {
    return false;
  }

  return true;
}

static bool mmRequire(SMgmtWrapper *pWrapper) {
  SMnodeMgmt mgmt = {0};
  mgmt.path = pWrapper->path;
  if (mmReadFile(&mgmt) != 0) {
    return false;
  }

  if (mgmt.dropped) {
    dInfo("mnode has been dropped and needs to be deleted");
    mndDestroy(mgmt.path);
    return false;
  }

  if (mgmt.deployed) {
    dInfo("mnode has been deployed");
    return true;
  }

  bool required = mmDeployRequired(pWrapper->pDnode);
  if (required) {
    dInfo("mnode need to be deployed");
  }

  return required;
}
S
shm  
Shengliang Guan 已提交
302

S
shm  
Shengliang Guan 已提交
303
void mmGetMgmtFp(SMgmtWrapper *pWrapper) {
S
shm  
Shengliang Guan 已提交
304
  SMgmtFp mgmtFp = {0};
S
shm  
Shengliang Guan 已提交
305
  mgmtFp.openFp = mmInit;
S
shm  
Shengliang Guan 已提交
306
  mgmtFp.closeFp = mmCleanup;
S
shm  
Shengliang Guan 已提交
307
  mgmtFp.requiredFp = mmRequire;
S
shm  
Shengliang Guan 已提交
308 309 310 311

  mmInitMsgHandles(pWrapper);
  pWrapper->name = "mnode";
  pWrapper->fp = mgmtFp;
S
shm  
Shengliang Guan 已提交
312 313 314
}

int32_t mmGetUserAuth(SMgmtWrapper *pWrapper, char *user, char *spi, char *encrypt, char *secret, char *ckey) {
S
shm  
Shengliang Guan 已提交
315 316 317 318 319 320 321 322 323 324 325 326 327 328
  SMnodeMgmt *pMgmt = pWrapper->pMgmt;

  SMnode *pMnode = mmAcquire(pMgmt);
  if (pMnode == NULL) {
    terrno = TSDB_CODE_APP_NOT_READY;
    dTrace("failed to get user auth since %s", terrstr());
    return -1;
  }

  int32_t code = mndRetriveAuth(pMnode, user, spi, encrypt, secret, ckey);
  mmRelease(pMgmt, pMnode);

  dTrace("user:%s, retrieve auth spi:%d encrypt:%d", user, *spi, *encrypt);
  return code;
S
shm  
Shengliang Guan 已提交
329
}
S
shm  
Shengliang Guan 已提交
330

S
shm  
Shengliang Guan 已提交
331
int32_t mmGetMonitorInfo(SMgmtWrapper *pWrapper, SMonClusterInfo *pClusterInfo, SMonVgroupInfo *pVgroupInfo,
S
shm  
Shengliang Guan 已提交
332
                         SMonGrantInfo *pGrantInfo) {
S
shm  
Shengliang Guan 已提交
333 334 335 336 337 338 339
  SMnodeMgmt *pMgmt = pWrapper->pMgmt;
  SMnode     *pMnode = mmAcquire(pMgmt);
  if (pMnode == NULL) return -1;

  int32_t code = mndGetMonitorInfo(pMnode, pClusterInfo, pVgroupInfo, pGrantInfo);
  mmRelease(pMgmt, pMnode);
  return code;
S
shm  
Shengliang Guan 已提交
340
}