dmRun.c 10.4 KB
Newer Older
S
Shengliang Guan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * 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
S
Shengliang Guan 已提交
17
#include "dmMgmt.h"
S
Shengliang Guan 已提交
18

S
Shengliang Guan 已提交
19
static int32_t dmInitParentProc(SMgmtWrapper *pWrapper) {
20
  int32_t shmsize = tsMnodeShmSize;
S
Shengliang Guan 已提交
21
  if (pWrapper->nodeType == VNODE) {
22
    shmsize = tsVnodeShmSize;
S
Shengliang Guan 已提交
23
  } else if (pWrapper->nodeType == QNODE) {
24
    shmsize = tsQnodeShmSize;
S
Shengliang Guan 已提交
25
  } else if (pWrapper->nodeType == SNODE) {
26
    shmsize = tsSnodeShmSize;
S
Shengliang Guan 已提交
27
  } else if (pWrapper->nodeType == MNODE) {
28
    shmsize = tsMnodeShmSize;
S
Shengliang Guan 已提交
29
  } else if (pWrapper->nodeType == BNODE) {
30 31 32 33 34
    shmsize = tsBnodeShmSize;
  } else {
    return -1;
  }

S
Shengliang Guan 已提交
35
  if (taosCreateShm(&pWrapper->procShm, pWrapper->nodeType, shmsize) != 0) {
36 37 38 39
    terrno = TAOS_SYSTEM_ERROR(terrno);
    dError("node:%s, failed to create shm size:%d since %s", pWrapper->name, shmsize, terrstr());
    return -1;
  }
S
Shengliang Guan 已提交
40
  dInfo("node:%s, shm:%d is created, size:%d", pWrapper->name, pWrapper->procShm.id, shmsize);
41

S
Shengliang Guan 已提交
42
  SProcCfg cfg = dmGenProcCfg(pWrapper);
43
  cfg.isChild = false;
S
Shengliang Guan 已提交
44 45 46
  pWrapper->procType = DND_PROC_PARENT;
  pWrapper->procObj = taosProcInit(&cfg);
  if (pWrapper->procObj == NULL) {
47 48 49 50 51 52 53
    dError("node:%s, failed to create proc since %s", pWrapper->name, terrstr());
    return -1;
  }

  return 0;
}

S
Shengliang Guan 已提交
54
static int32_t dmNewNodeProc(SMgmtWrapper *pWrapper, EDndNodeType n) {
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
  char  tstr[8] = {0};
  char *args[6] = {0};
  snprintf(tstr, sizeof(tstr), "%d", n);
  args[1] = "-c";
  args[2] = configDir;
  args[3] = "-n";
  args[4] = tstr;
  args[5] = NULL;

  int32_t pid = taosNewProc(args);
  if (pid <= 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    dError("node:%s, failed to exec in new process since %s", pWrapper->name, terrstr());
    return -1;
  }

  pWrapper->procId = pid;
  dInfo("node:%s, continue running in new process:%d", pWrapper->name, pid);
  return 0;
}

S
Shengliang Guan 已提交
76
static int32_t dmRunParentProc(SMgmtWrapper *pWrapper) {
S
Shengliang Guan 已提交
77
  if (pWrapper->pDnode->ntype == NODE_END) {
S
Shengliang Guan 已提交
78
    dInfo("node:%s, should be started manually in child process", pWrapper->name);
79
  } else {
S
Shengliang Guan 已提交
80
    if (dmNewNodeProc(pWrapper, pWrapper->nodeType) != 0) {
81 82 83
      return -1;
    }
  }
S
Shengliang Guan 已提交
84
  if (taosProcRun(pWrapper->procObj) != 0) {
85 86 87
    dError("node:%s, failed to run proc since %s", pWrapper->name, terrstr());
    return -1;
  }
S
Shengliang Guan 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100
  return 0;
}

static int32_t dmInitChildProc(SMgmtWrapper *pWrapper) {
  SProcCfg cfg = dmGenProcCfg(pWrapper);
  cfg.isChild = true;
  pWrapper->procObj = taosProcInit(&cfg);
  if (pWrapper->procObj == NULL) {
    dError("node:%s, failed to create proc since %s", pWrapper->name, terrstr());
    return -1;
  }
  return 0;
}
101

S
Shengliang Guan 已提交
102 103 104 105 106
static int32_t dmRunChildProc(SMgmtWrapper *pWrapper) {
  if (taosProcRun(pWrapper->procObj) != 0) {
    dError("node:%s, failed to run proc since %s", pWrapper->name, terrstr());
    return -1;
  }
107 108 109
  return 0;
}

S
Shengliang Guan 已提交
110
int32_t dmOpenNode(SMgmtWrapper *pWrapper) {
S
shm  
Shengliang Guan 已提交
111 112 113 114 115 116
  if (taosMkDir(pWrapper->path) != 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    dError("node:%s, failed to create dir:%s since %s", pWrapper->name, pWrapper->path, terrstr());
    return -1;
  }

S
Shengliang Guan 已提交
117
  SMgmtOutputOpt output = {0};
S
Shengliang Guan 已提交
118 119 120
  SMgmtInputOpt *pInput = &pWrapper->pDnode->input;
  pInput->name = pWrapper->name;
  pInput->path = pWrapper->path;
S
Shengliang Guan 已提交
121 122 123 124 125
  pInput->msgCb = dmGetMsgcb(pWrapper);
  if (pWrapper->nodeType == DNODE) {
    tmsgSetDefaultMsgCb(&pInput->msgCb);
  }

S
Shengliang Guan 已提交
126
  if (pWrapper->procType == DND_PROC_SINGLE || pWrapper->procType == DND_PROC_CHILD) {
S
Shengliang Guan 已提交
127
    if ((*pWrapper->func.openFp)(pInput, &output) != 0) {
S
Shengliang Guan 已提交
128 129 130
      dError("node:%s, failed to open since %s", pWrapper->name, terrstr());
      return -1;
    }
S
Shengliang Guan 已提交
131 132 133 134
    if (pWrapper->procType == DND_PROC_CHILD) {
      if (dmInitChildProc(pWrapper) != 0) return -1;
      if (dmRunChildProc(pWrapper) != 0) return -1;
    }
S
Shengliang Guan 已提交
135 136 137
    dDebug("node:%s, has been opened", pWrapper->name);
    pWrapper->deployed = true;
  } else {
S
Shengliang Guan 已提交
138
    if (dmInitParentProc(pWrapper) != 0) return -1;
S
Shengliang Guan 已提交
139
    if (dmWriteShmFile(pWrapper->path, pWrapper->name, &pWrapper->procShm) != 0) return -1;
S
Shengliang Guan 已提交
140
    if (dmRunParentProc(pWrapper) != 0) return -1;
S
shm  
Shengliang Guan 已提交
141 142
  }

S
Shengliang Guan 已提交
143 144 145 146 147 148 149 150 151 152
  if (output.dnodeId != 0) {
    pInput->dnodeId = output.dnodeId;
  }
  if (output.pMgmt != NULL) {
    pWrapper->pMgmt = output.pMgmt;
  }
  if (output.mnodeEps.numOfEps != 0) {
    pWrapper->pDnode->mnodeEps = output.mnodeEps;
  }

S
Shengliang Guan 已提交
153
  dmReportStartup(pWrapper->pDnode, pWrapper->name, "openned");
S
shm  
Shengliang Guan 已提交
154
  return 0;
S
shm  
Shengliang Guan 已提交
155
}
S
shm  
Shengliang Guan 已提交
156

S
Shengliang Guan 已提交
157
int32_t dmStartNode(SMgmtWrapper *pWrapper) {
S
Shengliang Guan 已提交
158 159
  if (!pWrapper->required) return 0;

S
Shengliang Guan 已提交
160 161 162 163
  if (pWrapper->procType == DND_PROC_PARENT) {
    dInfo("node:%s, not start in parent process", pWrapper->name);
  } else if (pWrapper->procType == DND_PROC_CHILD) {
    dInfo("node:%s, start in child process", pWrapper->name);
S
Shengliang Guan 已提交
164
    if (pWrapper->nodeType != DNODE) {
S
Shengliang Guan 已提交
165
      if (pWrapper->func.startFp != NULL && (*pWrapper->func.startFp)(pWrapper->pMgmt) != 0) {
166 167 168 169
        dError("node:%s, failed to start since %s", pWrapper->name, terrstr());
        return -1;
      }
    }
S
Shengliang Guan 已提交
170
  } else {
S
Shengliang Guan 已提交
171
    if (pWrapper->func.startFp != NULL && (*pWrapper->func.startFp)(pWrapper->pMgmt) != 0) {
S
Shengliang Guan 已提交
172 173 174
      dError("node:%s, failed to start since %s", pWrapper->name, terrstr());
      return -1;
    }
175
  }
S
Shengliang Guan 已提交
176

S
Shengliang Guan 已提交
177
  dmReportStartup(pWrapper->pDnode, pWrapper->name, "started");
178 179 180
  return 0;
}

S
Shengliang Guan 已提交
181
void dmStopNode(SMgmtWrapper *pWrapper) {
S
Shengliang Guan 已提交
182
  if (pWrapper->func.stopFp != NULL && pWrapper->pMgmt != NULL) {
S
Shengliang Guan 已提交
183
    (*pWrapper->func.stopFp)(pWrapper->pMgmt);
S
Shengliang Guan 已提交
184
    dDebug("node:%s, has been stopped", pWrapper->name);
S
Shengliang Guan 已提交
185
  }
S
Shengliang Guan 已提交
186 187 188 189
}

void dmCloseNode(SMgmtWrapper *pWrapper) {
  dInfo("node:%s, start to close", pWrapper->name);
S
Shengliang Guan 已提交
190
  pWrapper->deployed = false;
191 192 193 194 195

  while (pWrapper->refCount > 0) {
    taosMsleep(10);
  }

S
Shengliang Guan 已提交
196 197 198 199 200 201 202 203 204 205
  if (pWrapper->procType == DND_PROC_PARENT) {
    if (pWrapper->procId > 0 && taosProcExist(pWrapper->procId)) {
      dInfo("node:%s, send kill signal to the child process:%d", pWrapper->name, pWrapper->procId);
      taosKillProc(pWrapper->procId);
      dInfo("node:%s, wait for child process:%d to stop", pWrapper->name, pWrapper->procId);
      taosWaitProc(pWrapper->procId);
      dInfo("node:%s, child process:%d is stopped", pWrapper->name, pWrapper->procId);
    }
  }

S
shm  
Shengliang Guan 已提交
206
  taosWLockLatch(&pWrapper->latch);
S
Shengliang Guan 已提交
207 208 209 210
  if (pWrapper->pMgmt != NULL) {
    (*pWrapper->func.closeFp)(pWrapper->pMgmt);
    pWrapper->pMgmt = NULL;
  }
S
Shengliang Guan 已提交
211 212
  taosWUnLockLatch(&pWrapper->latch);

S
Shengliang Guan 已提交
213 214 215
  if (pWrapper->procObj) {
    taosProcCleanup(pWrapper->procObj);
    pWrapper->procObj = NULL;
S
shm  
Shengliang Guan 已提交
216
  }
S
shm  
Shengliang Guan 已提交
217

S
Shengliang Guan 已提交
218
  dInfo("node:%s, has been closed", pWrapper->name);
S
shm  
Shengliang Guan 已提交
219 220
}

S
Shengliang Guan 已提交
221 222 223 224
static int32_t dmOpenNodes(SDnode *pDnode) {
  if (pDnode->ptype == DND_PROC_CHILD) {
    SMgmtWrapper *pWrapper = &pDnode->wrappers[pDnode->ntype];
    pWrapper->procType = DND_PROC_CHILD;
S
Shengliang Guan 已提交
225
    return dmOpenNode(pWrapper);
S
Shengliang Guan 已提交
226 227 228 229
  } else {
    for (EDndNodeType n = DNODE; n < NODE_END; ++n) {
      SMgmtWrapper *pWrapper = &pDnode->wrappers[n];
      if (!pWrapper->required) continue;
230
      if (n == DNODE) {
S
Shengliang Guan 已提交
231 232 233
        pWrapper->procType = DND_PROC_SINGLE;
      } else {
        pWrapper->procType = pDnode->ptype;
S
Shengliang Guan 已提交
234
      }
S
Shengliang Guan 已提交
235 236 237
      if (dmOpenNode(pWrapper) != 0) {
        return -1;
      }
S
Shengliang Guan 已提交
238
    }
S
shm  
Shengliang Guan 已提交
239 240
  }

S
Shengliang Guan 已提交
241
  dmSetStatus(pDnode, DND_STAT_RUNNING);
S
Shengliang Guan 已提交
242 243
  return 0;
}
S
shm  
Shengliang Guan 已提交
244

S
Shengliang Guan 已提交
245
static int32_t dmStartNodes(SDnode *pDnode) {
S
Shengliang Guan 已提交
246
  for (EDndNodeType n = DNODE; n < NODE_END; ++n) {
S
shm  
Shengliang Guan 已提交
247
    SMgmtWrapper *pWrapper = &pDnode->wrappers[n];
S
Shengliang Guan 已提交
248
    if (dmStartNode(pWrapper) != 0) {
S
shm  
Shengliang Guan 已提交
249 250 251 252 253 254
      dError("node:%s, failed to start since %s", pWrapper->name, terrstr());
      return -1;
    }
  }

  dInfo("TDengine initialized successfully");
S
Shengliang Guan 已提交
255
  dmReportStartup(pDnode, "TDengine", "initialized successfully");
S
shm  
Shengliang Guan 已提交
256 257 258
  return 0;
}

S
Shengliang Guan 已提交
259 260
static void dmStopNodes(SDnode *pDnode) {
  for (EDndNodeType n = DNODE; n < NODE_END; ++n) {
S
shm  
Shengliang Guan 已提交
261
    SMgmtWrapper *pWrapper = &pDnode->wrappers[n];
S
Shengliang Guan 已提交
262
    dmStopNode(pWrapper);
S
shm  
Shengliang Guan 已提交
263
  }
S
Shengliang Guan 已提交
264
}
S
shm  
Shengliang Guan 已提交
265

S
Shengliang Guan 已提交
266 267
static void dmCloseNodes(SDnode *pDnode) {
  for (EDndNodeType n = DNODE; n < NODE_END; ++n) {
S
shm  
Shengliang Guan 已提交
268
    SMgmtWrapper *pWrapper = &pDnode->wrappers[n];
S
Shengliang Guan 已提交
269
    dmCloseNode(pWrapper);
S
shm  
Shengliang Guan 已提交
270
  }
S
Shengliang Guan 已提交
271
}
S
shm  
Shengliang Guan 已提交
272

S
Shengliang Guan 已提交
273 274 275 276 277
static void dmProcessProcHandle(void *handle) {
  dWarn("handle:%p, the child process dies and send an offline rsp", handle);
  SRpcMsg rpcMsg = {.handle = handle, .code = TSDB_CODE_NODE_OFFLINE};
  rpcSendResponse(&rpcMsg);
}
S
shm  
Shengliang Guan 已提交
278

S
Shengliang Guan 已提交
279
static void dmWatchNodes(SDnode *pDnode) {
280 281 282
  if (pDnode->ptype != DND_PROC_PARENT) return;
  if (pDnode->ntype == NODE_END) return;

283
  taosThreadMutexLock(&pDnode->mutex);
284 285 286 287
  for (EDndNodeType n = DNODE + 1; n < NODE_END; ++n) {
    SMgmtWrapper *pWrapper = &pDnode->wrappers[n];
    if (!pWrapper->required) continue;
    if (pWrapper->procType != DND_PROC_PARENT) continue;
S
Shengliang Guan 已提交
288

289 290 291 292
    if (pWrapper->procId <= 0 || !taosProcExist(pWrapper->procId)) {
      dWarn("node:%s, process:%d is killed and needs to be restarted", pWrapper->name, pWrapper->procId);
      if (pWrapper->procObj) {
        taosProcCloseHandles(pWrapper->procObj, dmProcessProcHandle);
S
shm  
Shengliang Guan 已提交
293
      }
294
      dmNewNodeProc(pWrapper, n);
S
shm  
Shengliang Guan 已提交
295 296
    }
  }
297
  taosThreadMutexUnlock(&pDnode->mutex);
S
shm  
Shengliang Guan 已提交
298
}
S
shm  
Shengliang Guan 已提交
299

S
Shengliang Guan 已提交
300
int32_t dmRun(SDnode *pDnode) {
S
Shengliang Guan 已提交
301
  if (tsMultiProcess == 0) {
S
Shengliang Guan 已提交
302
    pDnode->ptype = DND_PROC_SINGLE;
S
Shengliang Guan 已提交
303 304 305 306
    dInfo("dnode run in single process mode");
  } else if (tsMultiProcess == 2) {
    pDnode->ptype = DND_PROC_TEST;
    dInfo("dnode run in multi-process test mode");
S
Shengliang Guan 已提交
307 308
  } else if (pDnode->ntype == DNODE || pDnode->ntype == NODE_END) {
    pDnode->ptype = DND_PROC_PARENT;
S
Shengliang Guan 已提交
309
    dInfo("dnode run in parent process mode");
S
Shengliang Guan 已提交
310 311 312
  } else {
    pDnode->ptype = DND_PROC_CHILD;
    SMgmtWrapper *pWrapper = &pDnode->wrappers[pDnode->ntype];
S
Shengliang Guan 已提交
313 314 315 316 317 318 319 320 321
    dInfo("%s run in child process mode", pWrapper->name);
  }

  if (pDnode->ptype != DND_PROC_CHILD) {
    if (dmInitServer(pDnode) != 0) {
      dError("failed to init transport since %s", terrstr());
      return -1;
    }
    dmReportStartup(pDnode, "dnode-transport", "initialized");
S
shm  
Shengliang Guan 已提交
322 323
  }

S
Shengliang Guan 已提交
324 325
  if (dmOpenNodes(pDnode) != 0) {
    dError("failed to open nodes since %s", terrstr());
S
shm  
Shengliang Guan 已提交
326 327
    return -1;
  }
S
shm  
Shengliang Guan 已提交
328

S
Shengliang Guan 已提交
329 330
  if (dmStartNodes(pDnode) != 0) {
    dError("failed to start nodes since %s", terrstr());
S
shm  
Shengliang Guan 已提交
331
    return -1;
S
shm  
Shengliang Guan 已提交
332 333
  }

S
shm  
Shengliang Guan 已提交
334
  while (1) {
S
Shengliang Guan 已提交
335 336 337
    taosMsleep(100);
    if (pDnode->event & DND_EVENT_STOP) {
      dInfo("dnode is about to stop");
S
Shengliang Guan 已提交
338
      dmSetStatus(pDnode, DND_STAT_STOPPED);
S
Shengliang Guan 已提交
339 340 341 342 343
      dmStopNodes(pDnode);
      dmCloseNodes(pDnode);
      return 0;
    } else {
      dmWatchNodes(pDnode);
S
shm  
Shengliang Guan 已提交
344
    }
S
shm  
Shengliang Guan 已提交
345
  }
S
shm  
Shengliang Guan 已提交
346
}