dndExec.c 8.7 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
shm  
Shengliang Guan 已提交
17
#include "dndInt.h"
S
Shengliang Guan 已提交
18

S
shm  
Shengliang Guan 已提交
19
static bool dndRequireNode(SMgmtWrapper *pWrapper) {
S
shm  
Shengliang Guan 已提交
20 21
  bool required = false;
  int32_t code =(*pWrapper->fp.requiredFp)(pWrapper, &required);
S
Shengliang Guan 已提交
22
  if (!required) {
S
shm  
Shengliang Guan 已提交
23
    dDebug("node:%s, no need to start", pWrapper->name);
S
Shengliang Guan 已提交
24
  } else {
S
shm  
Shengliang Guan 已提交
25
    dDebug("node:%s, need to start", pWrapper->name);
S
Shengliang Guan 已提交
26
  }
S
Shengliang Guan 已提交
27 28
  return required;
}
S
Shengliang Guan 已提交
29

S
shm  
Shengliang Guan 已提交
30
int32_t dndOpenNode(SMgmtWrapper *pWrapper) {
S
shm  
Shengliang Guan 已提交
31 32 33
  int32_t code = (*pWrapper->fp.openFp)(pWrapper);
  if (code != 0) {
    dError("node:%s, failed to open since %s", pWrapper->name, terrstr());
S
shm  
Shengliang Guan 已提交
34
    return -1;
S
shm  
Shengliang Guan 已提交
35 36 37 38
  } else {
    dDebug("node:%s, has been opened", pWrapper->name);
  }

S
shm  
Shengliang Guan 已提交
39 40
  pWrapper->deployed = true;
  return 0;
S
shm  
Shengliang Guan 已提交
41
}
S
shm  
Shengliang Guan 已提交
42

S
shm  
Shengliang Guan 已提交
43
void dndCloseNode(SMgmtWrapper *pWrapper) {
S
Shengliang Guan 已提交
44
  dDebug("node:%s, start to close", pWrapper->name);
S
shm  
Shengliang Guan 已提交
45
  pWrapper->required = false;
S
shm  
Shengliang Guan 已提交
46
  taosWLockLatch(&pWrapper->latch);
S
shm  
Shengliang Guan 已提交
47 48 49 50
  if (pWrapper->deployed) {
    (*pWrapper->fp.closeFp)(pWrapper);
    pWrapper->deployed = false;
  }
S
Shengliang Guan 已提交
51 52 53 54 55 56
  taosWUnLockLatch(&pWrapper->latch);

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

S
shm  
Shengliang Guan 已提交
57 58 59 60
  if (pWrapper->pProc) {
    taosProcCleanup(pWrapper->pProc);
    pWrapper->pProc = NULL;
  }
S
Shengliang Guan 已提交
61
  dDebug("node:%s, has been closed", pWrapper->name);
S
shm  
Shengliang Guan 已提交
62
}
S
shm  
Shengliang Guan 已提交
63

S
shm  
Shengliang Guan 已提交
64
static int32_t dndRunInSingleProcess(SDnode *pDnode) {
S
shm  
Shengliang Guan 已提交
65 66 67
  dDebug("dnode run in single process mode");
  SMsgCb msgCb = dndCreateMsgcb(&pDnode->wrappers[0]);
  tmsgSetDefaultMsgCb(&msgCb);
S
shm  
Shengliang Guan 已提交
68

S
shm  
Shengliang Guan 已提交
69
  for (ENodeType n = DNODE; n < NODE_MAX; ++n) {
S
shm  
Shengliang Guan 已提交
70
    SMgmtWrapper *pWrapper = &pDnode->wrappers[n];
S
shm  
Shengliang Guan 已提交
71
    pWrapper->required = dndRequireNode(pWrapper);
S
shm  
Shengliang Guan 已提交
72 73
    if (!pWrapper->required) continue;

S
shm  
Shengliang Guan 已提交
74 75
    if (taosMkDir(pWrapper->path) != 0) {
      terrno = TAOS_SYSTEM_ERROR(errno);
S
shm  
Shengliang Guan 已提交
76
      dError("node:%s, failed to create dir:%s since %s", pWrapper->name, pWrapper->path, terrstr());
S
shm  
Shengliang Guan 已提交
77 78 79
      return -1;
    }

S
shm  
Shengliang Guan 已提交
80
    pWrapper->procType = PROC_SINGLE;
S
shm  
Shengliang Guan 已提交
81
    if (dndOpenNode(pWrapper) != 0) {
S
shm  
Shengliang Guan 已提交
82 83 84 85
      dError("node:%s, failed to start since %s", pWrapper->name, terrstr());
      return -1;
    }
  }
S
shm  
Shengliang Guan 已提交
86

S
shm  
Shengliang Guan 已提交
87 88 89 90 91 92 93 94 95 96
  dndSetStatus(pDnode, DND_STAT_RUNNING);

  for (ENodeType n = 0; n < NODE_MAX; ++n) {
    SMgmtWrapper *pWrapper = &pDnode->wrappers[n];
    if (!pWrapper->required) continue;
    if (pWrapper->fp.startFp == NULL) continue;
    if ((*pWrapper->fp.startFp)(pWrapper) != 0) {
      dError("node:%s, failed to start since %s", pWrapper->name, terrstr());
      return -1;
    }
S
shm  
Shengliang Guan 已提交
97 98
  }

S
shm  
Shengliang Guan 已提交
99
  return 0;
S
shm  
Shengliang Guan 已提交
100 101
}

S
shm  
Shengliang Guan 已提交
102
static void dndClearNodesExecpt(SDnode *pDnode, ENodeType except) {
S
shm  
Shengliang Guan 已提交
103
  // dndCleanupServer(pDnode);
S
shm  
Shengliang Guan 已提交
104 105 106
  for (ENodeType n = 0; n < NODE_MAX; ++n) {
    if (except == n) continue;
    SMgmtWrapper *pWrapper = &pDnode->wrappers[n];
S
shm  
Shengliang Guan 已提交
107
    pWrapper->required = false;
S
shm  
Shengliang Guan 已提交
108 109 110
  }
}

S
Shengliang Guan 已提交
111 112
static void dndConsumeChildQueue(SMgmtWrapper *pWrapper, SNodeMsg *pMsg, int16_t msgLen, void *pCont, int32_t contLen,
                                 ProcFuncType ftype) {
S
shm  
Shengliang Guan 已提交
113 114
  SRpcMsg *pRpc = &pMsg->rpcMsg;
  pRpc->pCont = pCont;
S
shm  
Shengliang Guan 已提交
115
  dTrace("msg:%p, get from child queue, handle:%p app:%p", pMsg, pRpc->handle, pRpc->ahandle);
S
shm  
Shengliang Guan 已提交
116 117

  NodeMsgFp msgFp = pWrapper->msgFps[TMSG_INDEX(pRpc->msgType)];
S
Shengliang Guan 已提交
118
  int32_t   code = (*msgFp)(pWrapper, pMsg);
S
shm  
Shengliang Guan 已提交
119 120

  if (code != 0) {
S
Shengliang Guan 已提交
121
    dError("msg:%p, failed to process since code:0x%04x:%s", pMsg, code & 0XFFFF, tstrerror(code));
S
shm  
Shengliang Guan 已提交
122
    if (pRpc->msgType & 1U) {
S
shm  
Shengliang Guan 已提交
123 124 125 126 127 128 129 130 131 132
      SRpcMsg rsp = {.handle = pRpc->handle, .ahandle = pRpc->ahandle, .code = terrno};
      dndSendRsp(pWrapper, &rsp);
    }

    dTrace("msg:%p, is freed", pMsg);
    taosFreeQitem(pMsg);
    rpcFreeCont(pCont);
  }
}

S
Shengliang Guan 已提交
133 134 135
static void dndConsumeParentQueue(SMgmtWrapper *pWrapper, SRpcMsg *pMsg, int16_t msgLen, void *pCont, int32_t contLen,
                                  ProcFuncType ftype) {
  pMsg->pCont = pCont;
S
shm  
Shengliang Guan 已提交
136
  dTrace("msg:%p, get from parent queue, handle:%p app:%p", pMsg, pMsg->handle, pMsg->ahandle);
S
Shengliang Guan 已提交
137 138

  switch (ftype) {
S
shm  
Shengliang Guan 已提交
139
    case PROC_REG:
S
Shengliang Guan 已提交
140 141
      rpcRegisterBrokenLinkArg(pMsg);
      break;
S
shm  
Shengliang Guan 已提交
142 143 144 145
    case PROC_RELEASE:
      rpcReleaseHandle(pMsg->handle, (int8_t)pMsg->code);
      rpcFreeCont(pCont);
      break;
S
shm  
Shengliang Guan 已提交
146 147 148
    case PROC_REQ:
      // todo send to dnode
      dndSendReqToMnode(pWrapper, pMsg);
S
Shengliang Guan 已提交
149 150 151 152 153
    default:
      dndSendRpcRsp(pWrapper, pMsg);
      break;
  }
  taosMemoryFree(pMsg);
S
shm  
Shengliang Guan 已提交
154 155
}

S
shm  
Shengliang Guan 已提交
156
static int32_t dndRunInMultiProcess(SDnode *pDnode) {
S
shm  
Shengliang Guan 已提交
157 158
  dInfo("dnode run in multi process mode");

S
shm  
Shengliang Guan 已提交
159 160
  for (ENodeType n = 0; n < NODE_MAX; ++n) {
    SMgmtWrapper *pWrapper = &pDnode->wrappers[n];
S
shm  
Shengliang Guan 已提交
161
    pWrapper->required = dndRequireNode(pWrapper);
S
shm  
Shengliang Guan 已提交
162 163
    if (!pWrapper->required) continue;

S
shm  
Shengliang Guan 已提交
164 165 166
    SMsgCb msgCb = dndCreateMsgcb(pWrapper);
    tmsgSetDefaultMsgCb(&msgCb);

S
shm  
Shengliang Guan 已提交
167 168 169 170 171 172
    if (taosMkDir(pWrapper->path) != 0) {
      terrno = TAOS_SYSTEM_ERROR(errno);
      dError("failed to create dir:%s since %s", pWrapper->path, terrstr());
      return -1;
    }

S
shm  
Shengliang Guan 已提交
173
    if (n == DNODE) {
S
shm  
Shengliang Guan 已提交
174
      dInfo("node:%s, will start in parent process", pWrapper->name);
S
shm  
Shengliang Guan 已提交
175
      pWrapper->procType = PROC_SINGLE;
S
shm  
Shengliang Guan 已提交
176
      if (dndOpenNode(pWrapper) != 0) {
S
shm  
Shengliang Guan 已提交
177 178 179 180 181 182
        dError("node:%s, failed to start since %s", pWrapper->name, terrstr());
        return -1;
      }
      continue;
    }

S
shm  
Shengliang Guan 已提交
183 184 185 186 187 188 189 190
    SProcCfg  cfg = {.childQueueSize = 1024 * 1024 * 2,  // size will be a configuration item
                     .childConsumeFp = (ProcConsumeFp)dndConsumeChildQueue,
                     .childMallocHeadFp = (ProcMallocFp)taosAllocateQitem,
                     .childFreeHeadFp = (ProcFreeFp)taosFreeQitem,
                     .childMallocBodyFp = (ProcMallocFp)rpcMallocCont,
                     .childFreeBodyFp = (ProcFreeFp)rpcFreeCont,
                     .parentQueueSize = 1024 * 1024 * 2,  // size will be a configuration item
                     .parentConsumeFp = (ProcConsumeFp)dndConsumeParentQueue,
wafwerar's avatar
wafwerar 已提交
191 192
                     .parentdMallocHeadFp = (ProcMallocFp)taosMemoryMalloc,
                     .parentFreeHeadFp = (ProcFreeFp)taosMemoryFree,
S
shm  
Shengliang Guan 已提交
193 194 195 196
                     .parentMallocBodyFp = (ProcMallocFp)rpcMallocCont,
                     .parentFreeBodyFp = (ProcFreeFp)rpcFreeCont,
                     .pParent = pWrapper,
                     .name = pWrapper->name};
S
shm  
Shengliang Guan 已提交
197 198 199 200 201 202 203 204 205 206 207
    SProcObj *pProc = taosProcInit(&cfg);
    if (pProc == NULL) {
      dError("node:%s, failed to fork since %s", pWrapper->name, terrstr());
      return -1;
    }

    pWrapper->pProc = pProc;

    if (taosProcIsChild(pProc)) {
      dInfo("node:%s, will start in child process", pWrapper->name);
      pWrapper->procType = PROC_CHILD;
S
shm  
Shengliang Guan 已提交
208
      // dndResetLog(pWrapper);
S
shm  
Shengliang Guan 已提交
209 210

      dInfo("node:%s, clean up resources inherited from parent", pWrapper->name);
S
shm  
Shengliang Guan 已提交
211
      dndClearNodesExecpt(pDnode, n);
S
shm  
Shengliang Guan 已提交
212 213

      dInfo("node:%s, will be initialized in child process", pWrapper->name);
S
shm  
Shengliang Guan 已提交
214 215 216 217 218 219 220 221 222 223
      if (dndOpenNode(pWrapper) != 0) {
        dInfo("node:%s, failed to init in child process since %s", pWrapper->name, terrstr());
        return -1;
      }

      if (taosProcRun(pProc) != 0) {
        dError("node:%s, failed to run proc since %s", pWrapper->name, terrstr());
        return -1;
      }
      break;
S
shm  
Shengliang Guan 已提交
224
    } else {
S
shm  
Shengliang Guan 已提交
225
      dInfo("node:%s, will not start in parent process, child pid:%d", pWrapper->name, taosProcChildId(pProc));
S
shm  
Shengliang Guan 已提交
226
      pWrapper->procType = PROC_PARENT;
S
shm  
Shengliang Guan 已提交
227 228 229 230
      if (taosProcRun(pProc) != 0) {
        dError("node:%s, failed to run proc since %s", pWrapper->name, terrstr());
        return -1;
      }
S
shm  
Shengliang Guan 已提交
231
    }
S
shm  
Shengliang Guan 已提交
232 233
  }

S
shm  
Shengliang Guan 已提交
234 235 236 237 238 239 240 241 242 243 244 245
  dndSetStatus(pDnode, DND_STAT_RUNNING);

  for (ENodeType n = 0; n < NODE_MAX; ++n) {
    SMgmtWrapper *pWrapper = &pDnode->wrappers[n];
    if (!pWrapper->required) continue;
    if (pWrapper->fp.startFp == NULL) continue;
    if (pWrapper->procType == PROC_PARENT && n != DNODE) continue;
    if (pWrapper->procType == PROC_CHILD && n == DNODE) continue;
    if ((*pWrapper->fp.startFp)(pWrapper) != 0) {
      dError("node:%s, failed to start since %s", pWrapper->name, terrstr());
      return -1;
    }
S
shm  
Shengliang Guan 已提交
246 247
  }

S
shm  
Shengliang Guan 已提交
248 249 250 251
  return 0;
}

int32_t dndRun(SDnode *pDnode) {
S
Shengliang Guan 已提交
252
  if (!tsMultiProcess) {
S
shm  
Shengliang Guan 已提交
253 254 255 256 257 258 259 260 261 262 263
    if (dndRunInSingleProcess(pDnode) != 0) {
      dError("failed to run dnode in single process mode since %s", terrstr());
      return -1;
    }
  } else {
    if (dndRunInMultiProcess(pDnode) != 0) {
      dError("failed to run dnode in multi process mode since %s", terrstr());
      return -1;
    }
  }

S
shm  
Shengliang Guan 已提交
264 265
  dndReportStartup(pDnode, "TDengine", "initialized successfully");

S
shm  
Shengliang Guan 已提交
266
  while (1) {
S
shm  
Shengliang Guan 已提交
267
    if (pDnode->event == DND_EVENT_STOP) {
S
shm  
Shengliang Guan 已提交
268
      dInfo("dnode is about to stop");
S
shm  
Shengliang Guan 已提交
269 270
      break;
    }
S
shm  
Shengliang Guan 已提交
271 272
    taosMsleep(100);
  }
S
shm  
Shengliang Guan 已提交
273 274

  return 0;
S
shm  
Shengliang Guan 已提交
275
}