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

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 54 55
  SMnode *pMnode = mmAcquire(pMgmt);
  if (pMnode != NULL) {
    mmRelease(pMgmt, pMnode);
    terrno = TSDB_CODE_DND_MNODE_ALREADY_DEPLOYED;
    dError("failed to create mnode since %s", terrstr());
    return -1;
  }

S
shm  
Shengliang Guan 已提交
56 57
  if (walInit() != 0) {
    dError("failed to init wal since %s", terrstr());
S
shm  
Shengliang Guan 已提交
58
    mndDestroy(pMgmt->path);
S
shm  
Shengliang Guan 已提交
59 60 61
    return -1;
  }

S
shm  
Shengliang Guan 已提交
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
  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;

S
shm  
Shengliang Guan 已提交
146
  pOption->pWrapper = pMgmt->pWrapper;
S
shm  
Shengliang Guan 已提交
147
  pOption->sendReqFp = dndSendReqToDnode;
S
shm  
Shengliang Guan 已提交
148 149 150
  pOption->sendMnodeReqFp = dndSendReqToMnode;
  pOption->putToWriteQFp = mmPutMsgToWriteQueue;
  pOption->putToReadQFp = mmPutMsgToReadQueue;
S
shm  
Shengliang Guan 已提交
151 152
  pOption->dnodeId = pDnode->dnodeId;
  pOption->clusterId = pDnode->clusterId;
S
shm  
Shengliang Guan 已提交
153 154 155 156 157 158 159 160 161 162
}

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 已提交
163 164
  pReplica->port = pDnode->serverPort;
  memcpy(pReplica->fqdn, pDnode->localFqdn, TSDB_FQDN_LEN);
S
shm  
Shengliang Guan 已提交
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181

  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 已提交
182 183
  pOption->dnodeId = pDnode->dnodeId;
  pOption->clusterId = pDnode->clusterId;
S
shm  
Shengliang Guan 已提交
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 209 210 211

  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 已提交
212
  dInfo("mnode-mgmt start to cleanup");
S
shm  
Shengliang Guan 已提交
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
  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 已提交
229
  dInfo("mnode-mgmt start to init");
S
shm  
Shengliang Guan 已提交
230 231
  if (pMgmt == NULL) goto _OVER;

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

  return code;
}

static bool mmDeployRequired(SDnode *pDnode) {
S
shm  
Shengliang Guan 已提交
265
  if (pDnode->dnodeId > 0) {
S
shm  
Shengliang Guan 已提交
266 267 268
    return false;
  }

S
shm  
Shengliang Guan 已提交
269
  if (pDnode->clusterId > 0) {
S
shm  
Shengliang Guan 已提交
270 271 272
    return false;
  }

S
shm  
Shengliang Guan 已提交
273
  if (strcmp(pDnode->localEp, pDnode->firstEp) != 0) {
S
shm  
Shengliang Guan 已提交
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 302 303 304
    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 已提交
305

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

  mmInitMsgHandles(pWrapper);
  pWrapper->name = "mnode";
  pWrapper->fp = mgmtFp;
S
shm  
Shengliang Guan 已提交
315 316 317
}

int32_t mmGetUserAuth(SMgmtWrapper *pWrapper, char *user, char *spi, char *encrypt, char *secret, char *ckey) {
S
shm  
Shengliang Guan 已提交
318 319 320 321 322 323 324 325 326 327 328 329 330 331
  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 已提交
332
}
S
shm  
Shengliang Guan 已提交
333

S
shm  
Shengliang Guan 已提交
334
int32_t mmGetMonitorInfo(SMgmtWrapper *pWrapper, SMonClusterInfo *pClusterInfo, SMonVgroupInfo *pVgroupInfo,
S
shm  
Shengliang Guan 已提交
335
                         SMonGrantInfo *pGrantInfo) {
S
shm  
Shengliang Guan 已提交
336 337 338 339 340 341 342
  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 已提交
343
}