dmRun.c 10.5 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
static int32_t dmOpenNodes(SDnode *pDnode) {
S
Shengliang Guan 已提交
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
  for (EDndNodeType ntype = DNODE; ntype < NODE_END; ++ntype) {
    SMgmtWrapper *pWrapper = &pDnode->wrappers[ntype];
    if (!pWrapper->required) continue;

    if (pDnode->ptype == DND_PROC_CHILD) {
      if (pDnode->ntype == ntype) {
        pWrapper->procType = DND_PROC_CHILD;
        if (dmOpenNode(pWrapper) != 0) {
          return -1;
        }
      } else {
        pWrapper->required = false;
      }
    } else {
      if (ntype == DNODE) {
S
Shengliang Guan 已提交
237 238 239
        pWrapper->procType = DND_PROC_SINGLE;
      } else {
        pWrapper->procType = pDnode->ptype;
S
Shengliang Guan 已提交
240
      }
S
Shengliang Guan 已提交
241 242 243
      if (dmOpenNode(pWrapper) != 0) {
        return -1;
      }
S
Shengliang Guan 已提交
244
    }
S
shm  
Shengliang Guan 已提交
245 246
  }

S
Shengliang Guan 已提交
247
  dmSetStatus(pDnode, DND_STAT_RUNNING);
S
Shengliang Guan 已提交
248 249
  return 0;
}
S
shm  
Shengliang Guan 已提交
250

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

  dInfo("TDengine initialized successfully");
S
Shengliang Guan 已提交
261
  dmReportStartup(pDnode, "TDengine", "initialized successfully");
S
shm  
Shengliang Guan 已提交
262 263 264
  return 0;
}

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

S
Shengliang Guan 已提交
272 273
static void dmCloseNodes(SDnode *pDnode) {
  for (EDndNodeType n = DNODE; n < NODE_END; ++n) {
S
shm  
Shengliang Guan 已提交
274
    SMgmtWrapper *pWrapper = &pDnode->wrappers[n];
S
Shengliang Guan 已提交
275
    dmCloseNode(pWrapper);
S
shm  
Shengliang Guan 已提交
276
  }
S
Shengliang Guan 已提交
277
}
S
shm  
Shengliang Guan 已提交
278

S
Shengliang Guan 已提交
279 280 281 282 283
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 已提交
284

S
Shengliang Guan 已提交
285
static void dmWatchNodes(SDnode *pDnode) {
286 287 288
  if (pDnode->ptype != DND_PROC_PARENT) return;
  if (pDnode->ntype == NODE_END) return;

289
  taosThreadMutexLock(&pDnode->mutex);
290 291 292 293
  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 已提交
294

295 296 297 298
    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 已提交
299
      }
300
      dmNewNodeProc(pWrapper, n);
S
shm  
Shengliang Guan 已提交
301 302
    }
  }
303
  taosThreadMutexUnlock(&pDnode->mutex);
S
shm  
Shengliang Guan 已提交
304
}
S
shm  
Shengliang Guan 已提交
305

S
Shengliang Guan 已提交
306
int32_t dmRun(SDnode *pDnode) {
S
Shengliang Guan 已提交
307
  if (tsMultiProcess == 0) {
S
Shengliang Guan 已提交
308
    pDnode->ptype = DND_PROC_SINGLE;
S
Shengliang Guan 已提交
309 310 311 312
    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 已提交
313 314
  } else if (pDnode->ntype == DNODE || pDnode->ntype == NODE_END) {
    pDnode->ptype = DND_PROC_PARENT;
S
Shengliang Guan 已提交
315
    dInfo("dnode run in parent process mode");
S
Shengliang Guan 已提交
316 317 318
  } else {
    pDnode->ptype = DND_PROC_CHILD;
    SMgmtWrapper *pWrapper = &pDnode->wrappers[pDnode->ntype];
S
Shengliang Guan 已提交
319 320 321 322 323 324 325 326 327
    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 已提交
328 329
  }

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

S
Shengliang Guan 已提交
335 336
  if (dmStartNodes(pDnode) != 0) {
    dError("failed to start nodes since %s", terrstr());
S
shm  
Shengliang Guan 已提交
337
    return -1;
S
shm  
Shengliang Guan 已提交
338 339
  }

S
shm  
Shengliang Guan 已提交
340
  while (1) {
S
Shengliang Guan 已提交
341 342 343
    taosMsleep(100);
    if (pDnode->event & DND_EVENT_STOP) {
      dInfo("dnode is about to stop");
S
Shengliang Guan 已提交
344
      dmSetStatus(pDnode, DND_STAT_STOPPED);
S
Shengliang Guan 已提交
345 346 347 348 349
      dmStopNodes(pDnode);
      dmCloseNodes(pDnode);
      return 0;
    } else {
      dmWatchNodes(pDnode);
S
shm  
Shengliang Guan 已提交
350
    }
S
shm  
Shengliang Guan 已提交
351
  }
S
shm  
Shengliang Guan 已提交
352
}