dmNodes.c 8.3 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

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

S
Shengliang Guan 已提交
35
  if (taosCreateShm(&pWrapper->proc.shm, pWrapper->ntype, 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

S
Shengliang Guan 已提交
41
  dInfo("node:%s, shm:%d is created, size:%d", pWrapper->name, pWrapper->proc.shm.id, shmsize);
42 43 44
  return 0;
}

45
static int32_t dmNewProc(SMgmtWrapper *pWrapper, EDndNodeType ntype) {
46 47
  char  tstr[8] = {0};
  char *args[6] = {0};
S
Shengliang Guan 已提交
48
  snprintf(tstr, sizeof(tstr), "%d", ntype);
49 50 51 52 53 54 55 56 57 58 59 60 61
  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;
  }

62
  taosIgnSignal(SIGCHLD);
S
Shengliang Guan 已提交
63
  pWrapper->proc.pid = pid;
S
Shengliang Guan 已提交
64
  dInfo("node:%s, continue running in new pid:%d", pWrapper->name, pid);
65 66 67
  return 0;
}

S
Shengliang Guan 已提交
68
int32_t dmOpenNode(SMgmtWrapper *pWrapper) {
69
  SDnode *pDnode = pWrapper->pDnode;
70

S
shm  
Shengliang Guan 已提交
71 72 73 74 75 76
  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 已提交
77
  SMgmtOutputOpt output = {0};
S
Shengliang Guan 已提交
78
  SMgmtInputOpt  input = dmBuildMgmtInputOpt(pWrapper);
S
Shengliang Guan 已提交
79

80
  if (pWrapper->ntype == DNODE || InChildProc(pWrapper)) {
S
Shengliang Guan 已提交
81
    tmsgSetDefault(&input.msgCb);
S
Shengliang Guan 已提交
82 83
  }

84
  if (OnlyInSingleProc(pWrapper)) {
S
Shengliang Guan 已提交
85
    dInfo("node:%s, start to open", pWrapper->name);
S
Shengliang Guan 已提交
86
    if ((*pWrapper->func.openFp)(&input, &output) != 0) {
S
Shengliang Guan 已提交
87 88 89
      dError("node:%s, failed to open since %s", pWrapper->name, terrstr());
      return -1;
    }
S
Shengliang Guan 已提交
90
    dInfo("node:%s, has been opened", pWrapper->name);
S
Shengliang Guan 已提交
91
    pWrapper->deployed = true;
S
Shengliang Guan 已提交
92 93
  }

94
  if (InParentProc(pWrapper)) {
S
Shengliang Guan 已提交
95
    dDebug("node:%s, start to open", pWrapper->name);
96
    if (dmCreateShm(pWrapper) != 0) {
S
Shengliang Guan 已提交
97 98
      return -1;
    }
99
    if (dmWriteShmFile(pWrapper->path, pWrapper->name, &pWrapper->proc.shm) != 0) {
S
Shengliang Guan 已提交
100 101
      return -1;
    }
102

103
    if (OnlyInParentProc(pWrapper)) {
104 105 106 107
      if (dmInitProc(pWrapper) != 0) {
        dError("node:%s, failed to init proc since %s", pWrapper->name, terrstr());
        return -1;
      }
108
      if (pDnode->rtype == NODE_END) {
109 110 111 112 113 114 115 116 117 118
        dInfo("node:%s, should be started manually in child process", pWrapper->name);
      } else {
        if (dmNewProc(pWrapper, pWrapper->ntype) != 0) {
          return -1;
        }
      }
      if (dmRunProc(&pWrapper->proc) != 0) {
        dError("node:%s, failed to run proc since %s", pWrapper->name, terrstr());
        return -1;
      }
S
Shengliang Guan 已提交
119
    }
120
    dDebug("node:%s, has been opened in parent process", pWrapper->name);
S
Shengliang Guan 已提交
121 122
  }

123
  if (InChildProc(pWrapper)) {
S
Shengliang Guan 已提交
124
    dDebug("node:%s, start to open", pWrapper->name);
125 126
    if ((*pWrapper->func.openFp)(&input, &output) != 0) {
      dError("node:%s, failed to open since %s", pWrapper->name, terrstr());
S
Shengliang Guan 已提交
127 128
      return -1;
    }
129
    if (dmInitProc(pWrapper) != 0) {
S
Shengliang Guan 已提交
130 131
      return -1;
    }
132
    if (dmRunProc(&pWrapper->proc) != 0) {
S
Shengliang Guan 已提交
133 134
      return -1;
    }
135 136
    dDebug("node:%s, has been opened in child process", pWrapper->name);
    pWrapper->deployed = true;
S
shm  
Shengliang Guan 已提交
137 138
  }

S
Shengliang Guan 已提交
139 140 141 142
  if (output.pMgmt != NULL) {
    pWrapper->pMgmt = output.pMgmt;
  }

143
  dmReportStartup(pWrapper->name, "openned");
S
shm  
Shengliang Guan 已提交
144
  return 0;
S
shm  
Shengliang Guan 已提交
145
}
S
shm  
Shengliang Guan 已提交
146

S
Shengliang Guan 已提交
147
int32_t dmStartNode(SMgmtWrapper *pWrapper) {
148
  if (OnlyInParentProc(pWrapper)) return 0;
S
Shengliang Guan 已提交
149 150 151 152 153 154 155
  if (pWrapper->func.startFp != NULL) {
    dDebug("node:%s, start to start", pWrapper->name);
    if ((*pWrapper->func.startFp)(pWrapper->pMgmt) != 0) {
      dError("node:%s, failed to start since %s", pWrapper->name, terrstr());
      return -1;
    }
    dDebug("node:%s, has been started", pWrapper->name);
156
  }
S
Shengliang Guan 已提交
157

158
  dmReportStartup(pWrapper->name, "started");
159 160 161
  return 0;
}

S
Shengliang Guan 已提交
162
void dmStopNode(SMgmtWrapper *pWrapper) {
S
Shengliang Guan 已提交
163
  if (pWrapper->func.stopFp != NULL && pWrapper->pMgmt != NULL) {
S
Shengliang Guan 已提交
164
    dDebug("node:%s, start to stop", pWrapper->name);
S
Shengliang Guan 已提交
165
    (*pWrapper->func.stopFp)(pWrapper->pMgmt);
S
Shengliang Guan 已提交
166
    dDebug("node:%s, has been stopped", pWrapper->name);
S
Shengliang Guan 已提交
167
  }
S
Shengliang Guan 已提交
168 169 170 171
}

void dmCloseNode(SMgmtWrapper *pWrapper) {
  dInfo("node:%s, start to close", pWrapper->name);
S
Shengliang Guan 已提交
172
  pWrapper->deployed = false;
173 174 175 176 177

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

178
  if (OnlyInParentProc(pWrapper)) {
S
Shengliang Guan 已提交
179 180
    int32_t pid = pWrapper->proc.pid;
    if (pid > 0 && taosProcExist(pid)) {
S
Shengliang Guan 已提交
181
      dInfo("node:%s, send kill signal to the child pid:%d", pWrapper->name, pid);
S
Shengliang Guan 已提交
182
      taosKillProc(pid);
S
Shengliang Guan 已提交
183
      dInfo("node:%s, wait for child pid:%d to stop", pWrapper->name, pid);
S
Shengliang Guan 已提交
184
      taosWaitProc(pid);
S
Shengliang Guan 已提交
185
      dInfo("node:%s, child pid:%d is stopped", pWrapper->name, pid);
S
Shengliang Guan 已提交
186 187 188
    }
  }

189
  taosThreadRwlockWrlock(&pWrapper->lock);
S
Shengliang Guan 已提交
190 191 192 193
  if (pWrapper->pMgmt != NULL) {
    (*pWrapper->func.closeFp)(pWrapper->pMgmt);
    pWrapper->pMgmt = NULL;
  }
194
  taosThreadRwlockUnlock(&pWrapper->lock);
S
Shengliang Guan 已提交
195

196
  if (!OnlyInSingleProc(pWrapper)) {
S
Shengliang Guan 已提交
197
    dmCleanupProc(pWrapper);
S
shm  
Shengliang Guan 已提交
198
  }
S
shm  
Shengliang Guan 已提交
199

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

S
Shengliang Guan 已提交
203
static int32_t dmOpenNodes(SDnode *pDnode) {
S
Shengliang Guan 已提交
204 205 206
  for (EDndNodeType ntype = DNODE; ntype < NODE_END; ++ntype) {
    SMgmtWrapper *pWrapper = &pDnode->wrappers[ntype];
    if (!pWrapper->required) continue;
S
Shengliang Guan 已提交
207
    if (dmOpenNode(pWrapper) != 0) {
S
Shengliang Guan 已提交
208
      dError("node:%s, failed to open since %s", pWrapper->name, terrstr());
S
Shengliang Guan 已提交
209
      return -1;
S
Shengliang Guan 已提交
210
    }
S
shm  
Shengliang Guan 已提交
211 212
  }

S
Shengliang Guan 已提交
213
  dmSetStatus(pDnode, DND_STAT_RUNNING);
S
Shengliang Guan 已提交
214 215
  return 0;
}
S
shm  
Shengliang Guan 已提交
216

S
Shengliang Guan 已提交
217
static int32_t dmStartNodes(SDnode *pDnode) {
S
Shengliang Guan 已提交
218 219
  for (EDndNodeType ntype = DNODE; ntype < NODE_END; ++ntype) {
    SMgmtWrapper *pWrapper = &pDnode->wrappers[ntype];
S
Shengliang Guan 已提交
220
    if (!pWrapper->required) continue;
S
Shengliang Guan 已提交
221
    if (dmStartNode(pWrapper) != 0) {
S
shm  
Shengliang Guan 已提交
222 223 224 225 226 227
      dError("node:%s, failed to start since %s", pWrapper->name, terrstr());
      return -1;
    }
  }

  dInfo("TDengine initialized successfully");
228
  dmReportStartup("TDengine", "initialized successfully");
S
shm  
Shengliang Guan 已提交
229 230 231
  return 0;
}

S
Shengliang Guan 已提交
232 233
static void dmStopNodes(SDnode *pDnode) {
  for (EDndNodeType n = DNODE; n < NODE_END; ++n) {
S
shm  
Shengliang Guan 已提交
234
    SMgmtWrapper *pWrapper = &pDnode->wrappers[n];
S
Shengliang Guan 已提交
235
    dmStopNode(pWrapper);
S
shm  
Shengliang Guan 已提交
236
  }
S
Shengliang Guan 已提交
237
}
S
shm  
Shengliang Guan 已提交
238

S
Shengliang Guan 已提交
239 240
static void dmCloseNodes(SDnode *pDnode) {
  for (EDndNodeType n = DNODE; n < NODE_END; ++n) {
S
shm  
Shengliang Guan 已提交
241
    SMgmtWrapper *pWrapper = &pDnode->wrappers[n];
S
Shengliang Guan 已提交
242
    dmCloseNode(pWrapper);
S
shm  
Shengliang Guan 已提交
243
  }
S
Shengliang Guan 已提交
244
}
S
shm  
Shengliang Guan 已提交
245

S
Shengliang Guan 已提交
246
static void dmWatchNodes(SDnode *pDnode) {
247
  if (pDnode->ptype != PARENT_PROC) return;
S
Shengliang Guan 已提交
248
  if (pDnode->rtype == NODE_END) return;
249

250
  taosThreadMutexLock(&pDnode->mutex);
S
Shengliang Guan 已提交
251 252
  for (EDndNodeType ntype = DNODE + 1; ntype < NODE_END; ++ntype) {
    SMgmtWrapper *pWrapper = &pDnode->wrappers[ntype];
S
Shengliang Guan 已提交
253 254
    SProc        *proc = &pWrapper->proc;

255
    if (!pWrapper->required) continue;
256
    if (!OnlyInParentProc(pWrapper)) continue;
S
Shengliang Guan 已提交
257 258

    if (proc->pid <= 0 || !taosProcExist(proc->pid)) {
S
Shengliang Guan 已提交
259
      dError("node:%s, pid:%d is killed and needs to restart", pWrapper->name, proc->pid);
S
Shengliang Guan 已提交
260
      dmCloseProcRpcHandles(&pWrapper->proc);
261
      dmNewProc(pWrapper, ntype);
S
shm  
Shengliang Guan 已提交
262 263
    }
  }
264
  taosThreadMutexUnlock(&pDnode->mutex);
S
shm  
Shengliang Guan 已提交
265
}
S
shm  
Shengliang Guan 已提交
266

267
int32_t dmRunDnode(SDnode *pDnode) {
S
Shengliang Guan 已提交
268 269
  if (dmOpenNodes(pDnode) != 0) {
    dError("failed to open nodes since %s", terrstr());
S
shm  
Shengliang Guan 已提交
270 271
    return -1;
  }
S
shm  
Shengliang Guan 已提交
272

S
Shengliang Guan 已提交
273 274
  if (dmStartNodes(pDnode) != 0) {
    dError("failed to start nodes since %s", terrstr());
S
shm  
Shengliang Guan 已提交
275
    return -1;
S
shm  
Shengliang Guan 已提交
276 277
  }

S
shm  
Shengliang Guan 已提交
278
  while (1) {
279
    if (pDnode->stop) {
S
Shengliang Guan 已提交
280
      dInfo("TDengine is about to stop");
S
Shengliang Guan 已提交
281
      dmSetStatus(pDnode, DND_STAT_STOPPED);
S
Shengliang Guan 已提交
282 283 284
      dmStopNodes(pDnode);
      dmCloseNodes(pDnode);
      return 0;
S
shm  
Shengliang Guan 已提交
285
    }
S
Shengliang Guan 已提交
286 287 288

    dmWatchNodes(pDnode);
    taosMsleep(100);
S
shm  
Shengliang Guan 已提交
289
  }
S
shm  
Shengliang Guan 已提交
290
}