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

S
shm  
Shengliang Guan 已提交
19 20 21 22 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
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 已提交
48 49 50 51 52 53
  if (walInit() != 0) {
    dError("failed to init wal since %s", terrstr());
    dndCleanup();
    return -1;
  }

S
shm  
Shengliang Guan 已提交
54 55 56 57 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
  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;
S
shm  
Shengliang Guan 已提交
139
  pOption->sendReqFp = dndSendReqToDnode;
S
shm  
Shengliang Guan 已提交
140 141 142
  pOption->sendReqToMnodeFp = dndSendReqToMnode;
  pOption->putReqToMWriteQFp = mmPutMsgToWriteQueue;
  pOption->putReqToMReadQFp = mmPutMsgToReadQueue;
S
shm  
Shengliang Guan 已提交
143 144
  pOption->dnodeId = pDnode->dnodeId;
  pOption->clusterId = pDnode->clusterId;
S
shm  
Shengliang Guan 已提交
145 146 147 148 149 150 151 152 153 154
}

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 已提交
155 156
  pReplica->port = pDnode->serverPort;
  memcpy(pReplica->fqdn, pDnode->localFqdn, TSDB_FQDN_LEN);
S
shm  
Shengliang Guan 已提交
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173

  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);
S
shm  
Shengliang Guan 已提交
174 175
  pOption->dnodeId = pDnode->dnodeId;
  pOption->clusterId = pDnode->clusterId;
S
shm  
Shengliang Guan 已提交
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

  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 已提交
204
  dInfo("mnode-mgmt start to cleanup");
S
shm  
Shengliang Guan 已提交
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
  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 已提交
221
  dInfo("mnode-mgmt start to init");
S
shm  
Shengliang Guan 已提交
222 223
  if (pMgmt == NULL) goto _OVER;

S
shm  
Shengliang Guan 已提交
224 225
  pMgmt->path = pWrapper->path;
  pMgmt->pDnode = pWrapper->pDnode;
S
shm  
Shengliang Guan 已提交
226
  pMgmt->pWrapper = pWrapper;
S
shm  
Shengliang Guan 已提交
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
  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 已提交
246
    pWrapper->pMgmt = pMgmt;
S
shm  
Shengliang Guan 已提交
247 248
    dInfo("mnode-mgmt is initialized");
  } else {
S
shm  
Shengliang Guan 已提交
249
    dError("failed to init mnode-mgmt since %s", terrstr());
S
shm  
Shengliang Guan 已提交
250 251 252 253 254 255 256
    mmCleanup(pWrapper);
  }

  return code;
}

static bool mmDeployRequired(SDnode *pDnode) {
S
shm  
Shengliang Guan 已提交
257
  if (pDnode->dnodeId > 0) {
S
shm  
Shengliang Guan 已提交
258 259 260
    return false;
  }

S
shm  
Shengliang Guan 已提交
261
  if (pDnode->clusterId > 0) {
S
shm  
Shengliang Guan 已提交
262 263 264
    return false;
  }

S
shm  
Shengliang Guan 已提交
265
  if (strcmp(pDnode->localEp, pDnode->firstEp) != 0) {
S
shm  
Shengliang Guan 已提交
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
    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 已提交
297

S
shm  
Shengliang Guan 已提交
298
void mmGetMgmtFp(SMgmtWrapper *pWrapper) {
S
shm  
Shengliang Guan 已提交
299
  SMgmtFp mgmtFp = {0};
S
shm  
Shengliang Guan 已提交
300
  mgmtFp.openFp = mmInit;
S
shm  
Shengliang Guan 已提交
301
  mgmtFp.closeFp = mmCleanup;
S
shm  
Shengliang Guan 已提交
302
  mgmtFp.requiredFp = mmRequire;
S
shm  
Shengliang Guan 已提交
303 304 305 306

  mmInitMsgHandles(pWrapper);
  pWrapper->name = "mnode";
  pWrapper->fp = mgmtFp;
S
shm  
Shengliang Guan 已提交
307 308 309
}

int32_t mmGetUserAuth(SMgmtWrapper *pWrapper, char *user, char *spi, char *encrypt, char *secret, char *ckey) {
S
shm  
Shengliang Guan 已提交
310 311 312 313 314 315 316 317 318 319 320 321 322 323
  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 已提交
324
}
S
shm  
Shengliang Guan 已提交
325

S
shm  
Shengliang Guan 已提交
326
int32_t mmGetMonitorInfo(SMgmtWrapper *pWrapper, SMonClusterInfo *pClusterInfo, SMonVgroupInfo *pVgroupInfo,
S
shm  
Shengliang Guan 已提交
327
                         SMonGrantInfo *pGrantInfo) {
S
shm  
Shengliang Guan 已提交
328 329 330 331 332 333 334
  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 已提交
335
}