dmMgmt.c 12.1 KB
Newer Older
S
shm  
Shengliang Guan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * 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/>.
 */

S
shm  
Shengliang Guan 已提交
16
#define _DEFAULT_SOURCE
S
Shengliang Guan 已提交
17
#include "dmMgmt.h"
S
Shengliang Guan 已提交
18

S
Shengliang Guan 已提交
19 20 21
static bool dmIsNodeRequired(SDnode *pDnode, EDndNodeType ntype) { return pDnode->wrappers[ntype].required; }

static bool dmRequireNode(SMgmtWrapper *pWrapper) {
S
Shengliang Guan 已提交
22
  SMgmtInputOpt input = dmBuildMgmtInputOpt(pWrapper);
S
Shengliang Guan 已提交
23 24

  bool    required = false;
S
Shengliang Guan 已提交
25
  int32_t code = (*pWrapper->func.requiredFp)(&input, &required);
S
Shengliang Guan 已提交
26 27 28 29 30 31 32 33 34 35 36
  if (!required) {
    dDebug("node:%s, does not require startup", pWrapper->name);
  }

  if (pWrapper->ntype == DNODE && pWrapper->pDnode->rtype != DNODE && pWrapper->pDnode->rtype != NODE_END) {
    required = false;
    dDebug("node:%s, does not require startup in child process", pWrapper->name);
  }

  return required;
}
S
Shengliang Guan 已提交
37

S
Shengliang Guan 已提交
38
static int32_t dmInitVars(SDnode *pDnode, const SDnodeOpt *pOption) {
S
Shengliang Guan 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
  pDnode->rtype = pOption->ntype;

  if (tsMultiProcess == 0) {
    pDnode->ptype = DND_PROC_SINGLE;
    dInfo("dnode will run in single-process mode");
  } else if (tsMultiProcess > 1) {
    pDnode->ptype = DND_PROC_TEST;
    dInfo("dnode will run in multi-process test mode");
  } else if (pDnode->rtype == DNODE || pDnode->rtype == NODE_END) {
    pDnode->ptype = DND_PROC_PARENT;
    dInfo("dnode will run in parent-process mode");
  } else {
    pDnode->ptype = DND_PROC_CHILD;
    SMgmtWrapper *pWrapper = &pDnode->wrappers[pDnode->rtype];
    dInfo("dnode will run in child-process mode, node:%s", pWrapper->name);
  }

S
Shengliang Guan 已提交
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
  pDnode->data.dnodeId = 0;
  pDnode->data.clusterId = 0;
  pDnode->data.dnodeVer = 0;
  pDnode->data.updateTime = 0;
  pDnode->data.rebootTime = taosGetTimestampMs();
  pDnode->data.dropped = 0;
  pDnode->data.localEp = strdup(pOption->localEp);
  pDnode->data.localFqdn = strdup(pOption->localFqdn);
  pDnode->data.firstEp = strdup(pOption->firstEp);
  pDnode->data.secondEp = strdup(pOption->secondEp);
  pDnode->data.serverPort = pOption->serverPort;
  pDnode->data.supportVnodes = pOption->numOfSupportVnodes;
  pDnode->data.numOfDisks = pOption->numOfDisks;
  pDnode->data.disks = pOption->disks;
  pDnode->data.dataDir = strdup(pOption->dataDir);

  if (pDnode->data.dataDir == NULL || pDnode->data.localEp == NULL || pDnode->data.localFqdn == NULL ||
      pDnode->data.firstEp == NULL || pDnode->data.secondEp == NULL) {
S
shm  
Shengliang Guan 已提交
74 75 76
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
S
shm  
Shengliang Guan 已提交
77

78
  taosThreadMutexInit(&pDnode->mutex, NULL);
S
shm  
Shengliang Guan 已提交
79 80 81
  return 0;
}

S
Shengliang Guan 已提交
82
static void dmClearVars(SDnode *pDnode) {
S
Shengliang Guan 已提交
83 84 85 86 87 88 89 90 91 92
  for (EDndNodeType ntype = DNODE; ntype < NODE_END; ++ntype) {
    SMgmtWrapper *pWrapper = &pDnode->wrappers[ntype];
    taosMemoryFreeClear(pWrapper->path);
  }
  if (pDnode->lockfile != NULL) {
    taosUnLockFile(pDnode->lockfile);
    taosCloseFile(&pDnode->lockfile);
    pDnode->lockfile = NULL;
  }

S
Shengliang Guan 已提交
93 94 95 96 97
  taosMemoryFreeClear(pDnode->data.localEp);
  taosMemoryFreeClear(pDnode->data.localFqdn);
  taosMemoryFreeClear(pDnode->data.firstEp);
  taosMemoryFreeClear(pDnode->data.secondEp);
  taosMemoryFreeClear(pDnode->data.dataDir);
S
Shengliang Guan 已提交
98 99 100 101

  taosThreadMutexDestroy(&pDnode->mutex);
  memset(&pDnode->mutex, 0, sizeof(pDnode->mutex));
  taosMemoryFree(pDnode);
S
shm  
Shengliang Guan 已提交
102
  dDebug("dnode memory is cleared, data:%p", pDnode);
S
shm  
Shengliang Guan 已提交
103 104
}

S
Shengliang Guan 已提交
105
SDnode *dmCreate(const SDnodeOpt *pOption) {
S
Shengliang Guan 已提交
106
  dInfo("start to create dnode");
S
shm  
Shengliang Guan 已提交
107
  int32_t code = -1;
S
Shengliang Guan 已提交
108
  char    path[PATH_MAX + 100] = {0};
S
shm  
Shengliang Guan 已提交
109 110
  SDnode *pDnode = NULL;

wafwerar's avatar
wafwerar 已提交
111
  pDnode = taosMemoryCalloc(1, sizeof(SDnode));
S
shm  
Shengliang Guan 已提交
112 113 114 115 116
  if (pDnode == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    goto _OVER;
  }

S
Shengliang Guan 已提交
117 118 119 120
  if (dmInitVars(pDnode, pOption) != 0) {
    goto _OVER;
  }

S
Shengliang Guan 已提交
121 122 123 124 125 126
  pDnode->wrappers[DNODE].func = dmGetMgmtFunc();
  pDnode->wrappers[MNODE].func = mmGetMgmtFunc();
  pDnode->wrappers[VNODE].func = vmGetMgmtFunc();
  pDnode->wrappers[QNODE].func = qmGetMgmtFunc();
  pDnode->wrappers[SNODE].func = smGetMgmtFunc();
  pDnode->wrappers[BNODE].func = bmGetMgmtFunc();
S
shm  
Shengliang Guan 已提交
127

S
Shengliang 已提交
128 129
  for (EDndNodeType ntype = DNODE; ntype < NODE_END; ++ntype) {
    SMgmtWrapper *pWrapper = &pDnode->wrappers[ntype];
S
shm  
Shengliang Guan 已提交
130
    pWrapper->pDnode = pDnode;
S
Shengliang Guan 已提交
131
    pWrapper->name = dmNodeName(ntype);
S
Shengliang Guan 已提交
132 133 134 135 136 137 138 139
    pWrapper->ntype = ntype;
    pWrapper->proc.wrapper = pWrapper;
    pWrapper->proc.shm.id = -1;
    pWrapper->proc.pid = -1;
    pWrapper->proc.ptype = pDnode->ptype;
    if (ntype == DNODE) {
      pWrapper->proc.ptype = DND_PROC_SINGLE;
    }
S
Shengliang Guan 已提交
140 141
    taosInitRWLatch(&pWrapper->latch);

S
Shengliang Guan 已提交
142 143
    snprintf(path, sizeof(path), "%s%s%s", pOption->dataDir, TD_DIRSEP, pWrapper->name);
    pWrapper->path = strdup(path);
S
shm  
Shengliang Guan 已提交
144 145 146 147 148
    if (pWrapper->path == NULL) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      goto _OVER;
    }

S
Shengliang Guan 已提交
149
    if (ntype != DNODE && dmReadShmFile(pWrapper->path, pWrapper->name, pDnode->rtype, &pWrapper->proc.shm) != 0) {
S
Shengliang Guan 已提交
150 151 152
      dError("node:%s, failed to read shm file since %s", pWrapper->name, terrstr());
      goto _OVER;
    }
S
Shengliang Guan 已提交
153 154

    pWrapper->required = dmRequireNode(pWrapper);
S
shm  
Shengliang Guan 已提交
155 156
  }

S
Shengliang Guan 已提交
157
  if (dmInitMsgHandle(pDnode) != 0) {
S
Shengliang Guan 已提交
158
    dError("failed to init msg handles since %s", terrstr());
S
shm  
Shengliang Guan 已提交
159 160 161
    goto _OVER;
  }

S
Shengliang Guan 已提交
162 163 164 165
  if (dmInitClient(pDnode) != 0) {
    goto _OVER;
  }

S
Shengliang Guan 已提交
166 167 168 169 170 171 172 173 174 175 176 177 178
  if (OnlyInSingleProc(pDnode->ptype) && InParentProc(pDnode->ptype)) {
    pDnode->lockfile = dmCheckRunning(pOption->dataDir);
    if (pDnode->lockfile == NULL) {
      goto _OVER;
    }

    if (dmInitServer(pDnode) != 0) {
      dError("failed to init transport since %s", terrstr());
      goto _OVER;
    }
  }

  dmReportStartup(pDnode, "dnode-transport", "initialized");
S
shm  
Shengliang Guan 已提交
179
  dInfo("dnode is created, data:%p", pDnode);
S
shm  
Shengliang Guan 已提交
180 181 182
  code = 0;

_OVER:
S
Shengliang Guan 已提交
183
  if (code != 0 && pDnode != NULL) {
S
Shengliang Guan 已提交
184
    dmClearVars(pDnode);
185
    pDnode = NULL;
S
shm  
Shengliang Guan 已提交
186
    dError("failed to create dnode since %s", terrstr());
S
shm  
Shengliang Guan 已提交
187 188 189 190 191
  }

  return pDnode;
}

S
Shengliang Guan 已提交
192
void dmClose(SDnode *pDnode) {
S
shm  
Shengliang Guan 已提交
193
  if (pDnode == NULL) return;
S
Shengliang Guan 已提交
194 195 196 197

  dmCleanupClient(pDnode);
  dmCleanupServer(pDnode);

S
Shengliang Guan 已提交
198
  dmClearVars(pDnode);
S
shm  
Shengliang Guan 已提交
199
  dInfo("dnode is closed, data:%p", pDnode);
S
shm  
Shengliang Guan 已提交
200
}
S
Shengliang Guan 已提交
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235

void dmSetStatus(SDnode *pDnode, EDndRunStatus status) {
  if (pDnode->status != status) {
    dDebug("dnode status set from %s to %s", dmStatStr(pDnode->status), dmStatStr(status));
    pDnode->status = status;
  }
}

void dmSetEvent(SDnode *pDnode, EDndEvent event) {
  if (event == DND_EVENT_STOP) {
    pDnode->event = event;
  }
}

SMgmtWrapper *dmAcquireWrapper(SDnode *pDnode, EDndNodeType ntype) {
  SMgmtWrapper *pWrapper = &pDnode->wrappers[ntype];
  SMgmtWrapper *pRetWrapper = pWrapper;

  taosRLockLatch(&pWrapper->latch);
  if (pWrapper->deployed) {
    int32_t refCount = atomic_add_fetch_32(&pWrapper->refCount, 1);
    dTrace("node:%s, is acquired, refCount:%d", pWrapper->name, refCount);
  } else {
    terrno = TSDB_CODE_NODE_NOT_DEPLOYED;
    pRetWrapper = NULL;
  }
  taosRUnLockLatch(&pWrapper->latch);

  return pRetWrapper;
}

int32_t dmMarkWrapper(SMgmtWrapper *pWrapper) {
  int32_t code = 0;

  taosRLockLatch(&pWrapper->latch);
S
Shengliang Guan 已提交
236
  if (pWrapper->deployed || (InParentProc(pWrapper->proc.ptype) && pWrapper->required)) {
S
Shengliang Guan 已提交
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
    int32_t refCount = atomic_add_fetch_32(&pWrapper->refCount, 1);
    dTrace("node:%s, is marked, refCount:%d", pWrapper->name, refCount);
  } else {
    terrno = TSDB_CODE_NODE_NOT_DEPLOYED;
    code = -1;
  }
  taosRUnLockLatch(&pWrapper->latch);

  return code;
}

void dmReleaseWrapper(SMgmtWrapper *pWrapper) {
  if (pWrapper == NULL) return;

  taosRLockLatch(&pWrapper->latch);
  int32_t refCount = atomic_sub_fetch_32(&pWrapper->refCount, 1);
  taosRUnLockLatch(&pWrapper->latch);
  dTrace("node:%s, is released, refCount:%d", pWrapper->name, refCount);
}

void dmReportStartup(SDnode *pDnode, const char *pName, const char *pDesc) {
  SStartupInfo *pStartup = &pDnode->startup;
  tstrncpy(pStartup->name, pName, TSDB_STEP_NAME_LEN);
  tstrncpy(pStartup->desc, pDesc, TSDB_STEP_DESC_LEN);
  dInfo("step:%s, %s", pStartup->name, pStartup->desc);
}

void dmReportStartupByWrapper(SMgmtWrapper *pWrapper, const char *pName, const char *pDesc) {
  dmReportStartup(pWrapper->pDnode, pName, pDesc);
}

268
static void dmGetServerStartupStatus(SDnode *pDnode, SServerStatusRsp *pStatus) {
S
Shengliang Guan 已提交
269
  SDnodeMgmt *pMgmt = pDnode->wrappers[DNODE].pMgmt;
S
Shengliang Guan 已提交
270 271 272 273 274
  pStatus->details[0] = 0;

  if (pDnode->status == DND_STAT_INIT) {
    pStatus->statusCode = TSDB_SRV_STATUS_NETWORK_OK;
    snprintf(pStatus->details, sizeof(pStatus->details), "%s: %s", pDnode->startup.name, pDnode->startup.desc);
275
  } else if (pDnode->status == DND_STAT_STOPPED) {
S
Shengliang Guan 已提交
276
    pStatus->statusCode = TSDB_SRV_STATUS_EXTING;
277 278
  } else {
    pStatus->statusCode = TSDB_SRV_STATUS_SERVICE_OK;
S
Shengliang Guan 已提交
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
  }
}

void dmProcessNetTestReq(SDnode *pDnode, SRpcMsg *pReq) {
  dDebug("net test req is received");
  SRpcMsg rsp = {.handle = pReq->handle, .refId = pReq->refId, .ahandle = pReq->ahandle, .code = 0};
  rsp.pCont = rpcMallocCont(pReq->contLen);
  if (rsp.pCont == NULL) {
    rsp.code = TSDB_CODE_OUT_OF_MEMORY;
  } else {
    rsp.contLen = pReq->contLen;
  }
  rpcSendResponse(&rsp);
  rpcFreeCont(pReq->pCont);
}

295 296
void dmProcessServerStartupStatus(SDnode *pDnode, SRpcMsg *pReq) {
  dDebug("server startup status req is received");
S
Shengliang Guan 已提交
297 298

  SServerStatusRsp statusRsp = {0};
299
  dmGetServerStartupStatus(pDnode, &statusRsp);
S
Shengliang Guan 已提交
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322

  SRpcMsg rspMsg = {.handle = pReq->handle, .ahandle = pReq->ahandle, .refId = pReq->refId};
  int32_t rspLen = tSerializeSServerStatusRsp(NULL, 0, &statusRsp);
  if (rspLen < 0) {
    rspMsg.code = TSDB_CODE_OUT_OF_MEMORY;
    goto _OVER;
  }

  void *pRsp = rpcMallocCont(rspLen);
  if (pRsp == NULL) {
    rspMsg.code = TSDB_CODE_OUT_OF_MEMORY;
    goto _OVER;
  }

  tSerializeSServerStatusRsp(pRsp, rspLen, &statusRsp);
  rspMsg.pCont = pRsp;
  rspMsg.contLen = rspLen;

_OVER:
  rpcSendResponse(&rspMsg);
  rpcFreeCont(pReq->pCont);
}

S
Shengliang Guan 已提交
323
static int32_t dmProcessCreateNodeReq(SDnode *pDnode, EDndNodeType ntype, SNodeMsg *pMsg) {
S
Shengliang Guan 已提交
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
  SMgmtWrapper *pWrapper = dmAcquireWrapper(pDnode, ntype);
  if (pWrapper != NULL) {
    dmReleaseWrapper(pWrapper);
    terrno = TSDB_CODE_NODE_ALREADY_DEPLOYED;
    dError("failed to create node since %s", terrstr());
    return -1;
  }

  taosThreadMutexLock(&pDnode->mutex);
  pWrapper = &pDnode->wrappers[ntype];

  if (taosMkDir(pWrapper->path) != 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    dError("failed to create dir:%s since %s", pWrapper->path, terrstr());
    return -1;
  }

S
Shengliang Guan 已提交
341
  SMgmtInputOpt input = dmBuildMgmtInputOpt(pWrapper);
S
Shengliang Guan 已提交
342

S
Shengliang Guan 已提交
343
  int32_t code = (*pWrapper->func.createFp)(&input, pMsg);
S
Shengliang Guan 已提交
344 345 346 347 348 349 350
  if (code != 0) {
    dError("node:%s, failed to create since %s", pWrapper->name, terrstr());
  } else {
    dDebug("node:%s, has been created", pWrapper->name);
    (void)dmOpenNode(pWrapper);
    pWrapper->required = true;
    pWrapper->deployed = true;
S
Shengliang Guan 已提交
351
    pWrapper->proc.ptype = pDnode->ptype;
S
Shengliang Guan 已提交
352 353 354 355 356 357
  }

  taosThreadMutexUnlock(&pDnode->mutex);
  return code;
}

S
Shengliang Guan 已提交
358
static int32_t dmProcessDropNodeReq(SDnode *pDnode, EDndNodeType ntype, SNodeMsg *pMsg) {
S
Shengliang Guan 已提交
359 360 361 362 363 364 365 366 367
  SMgmtWrapper *pWrapper = dmAcquireWrapper(pDnode, ntype);
  if (pWrapper == NULL) {
    terrno = TSDB_CODE_NODE_NOT_DEPLOYED;
    dError("failed to drop node since %s", terrstr());
    return -1;
  }

  taosThreadMutexLock(&pDnode->mutex);

S
Shengliang Guan 已提交
368
  int32_t code = (*pWrapper->func.dropFp)(pWrapper->pMgmt, pMsg);
S
Shengliang Guan 已提交
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
  if (code != 0) {
    dError("node:%s, failed to drop since %s", pWrapper->name, terrstr());
  } else {
    dDebug("node:%s, has been dropped", pWrapper->name);
    pWrapper->required = false;
    pWrapper->deployed = false;
  }

  dmReleaseWrapper(pWrapper);

  if (code == 0) {
    dmCloseNode(pWrapper);
    taosRemoveDir(pWrapper->path);
  }
  taosThreadMutexUnlock(&pDnode->mutex);
  return code;
S
Shengliang Guan 已提交
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
}

SMgmtInputOpt dmBuildMgmtInputOpt(SMgmtWrapper *pWrapper) {
  SMgmtInputOpt opt = {
      .pDnode = pWrapper->pDnode,
      .pData = &pWrapper->pDnode->data,
      .processCreateNodeFp = dmProcessCreateNodeReq,
      .processDropNodeFp = dmProcessDropNodeReq,
      .isNodeRequiredFp = dmIsNodeRequired,
      .name = pWrapper->name,
      .path = pWrapper->path,
  };

  opt.msgCb = dmGetMsgcb(pWrapper);
  return opt;
S
Shengliang Guan 已提交
400
}