dndMain.c 7.8 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 "dndMain.h"
S
shm  
Shengliang Guan 已提交
18
#include "dmMgmt.h"
S
Shengliang Guan 已提交
19
#include "dndTransport.h"
S
Shengliang Guan 已提交
20

S
shm  
Shengliang Guan 已提交
21 22 23 24 25
#include "bmInt.h"
#include "mmInt.h"
#include "qmInt.h"
#include "smInt.h"
#include "vmInt.h"
S
Shengliang Guan 已提交
26 27 28 29 30 31 32 33 34 35

static void dndResetLog(SMgmtWrapper *pMgmt) {
  char logname[24] = {0};
  snprintf(logname, sizeof(logname), "%slog", pMgmt->name);

  dInfo("node:%s, reset log to %s", pMgmt->name, logname);
  taosCloseLog();
  taosInitLog(logname, 1);
}

S
shm  
Shengliang Guan 已提交
36
static bool dndRequireNode(SMgmtWrapper *pMgmt) {
S
Shengliang Guan 已提交
37 38 39 40 41
  bool required = (*pMgmt->fp.requiredFp)(pMgmt);
  if (!required) {
    dDebug("node:%s, no need to start on this dnode", pMgmt->name);
  } else {
    dDebug("node:%s, need to start on this dnode", pMgmt->name);
S
Shengliang Guan 已提交
42
  }
S
Shengliang Guan 已提交
43 44
  return required;
}
S
Shengliang Guan 已提交
45

S
shm  
Shengliang Guan 已提交
46
static void dndClearMemory(SDnode *pDnode) {
S
Shengliang Guan 已提交
47
  for (ENodeType n = 0; n < NODE_MAX; ++n) {
S
shm  
Shengliang Guan 已提交
48
    SMgmtWrapper *pMgmt = &pDnode->wrappers[n];
S
Shengliang Guan 已提交
49
    tfree(pMgmt->path);
S
Shengliang Guan 已提交
50
  }
S
Shengliang Guan 已提交
51 52 53 54
  if (pDnode->pLockFile != NULL) {
    taosUnLockFile(pDnode->pLockFile);
    taosCloseFile(&pDnode->pLockFile);
  }
S
shm  
Shengliang Guan 已提交
55
  tfree(pDnode->path);
S
Shengliang Guan 已提交
56 57
  dDebug("dnode object memory is cleared, data:%p", pDnode);
}
S
Shengliang Guan 已提交
58

S
shm  
Shengliang Guan 已提交
59
static int32_t dndInitResource(SDnode *pDnode) {
S
Shengliang Guan 已提交
60 61


S
Shengliang Guan 已提交
62

S
Shengliang Guan 已提交
63 64 65
  return 0;
}

S
shm  
Shengliang Guan 已提交
66
static void dndClearResource(SDnode *pDnode) {
S
Shengliang Guan 已提交
67 68
  dndCleanupTrans(pDnode);
  dndStopMgmt(pDnode);
S
Shengliang Guan 已提交
69
  dndCleanupMgmt(pDnode);
S
Shengliang Guan 已提交
70 71
  tfsClose(pDnode->pTfs);
  dDebug("dnode object resource is cleared, data:%p", pDnode);
S
Shengliang Guan 已提交
72 73
}

S
Shengliang Guan 已提交
74
SDnode *dndCreate(SDndCfg *pCfg) {
S
Shengliang Guan 已提交
75
  dInfo("start to create dnode object");
S
Shengliang Guan 已提交
76 77 78
  int32_t code = -1;
  char    path[PATH_MAX + 100];
  SDnode *pDnode = NULL;
S
Shengliang Guan 已提交
79

S
Shengliang Guan 已提交
80
  pDnode = calloc(1, sizeof(SDnode));
S
Shengliang Guan 已提交
81 82
  if (pDnode == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
S
Shengliang Guan 已提交
83
    goto _OVER;
S
Shengliang Guan 已提交
84 85
  }

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

  snprintf(path, sizeof(path), "%s%sdnode", pCfg->dataDir, TD_DIRSEP);
  pDnode->path = strdup(path);
  if (taosMkDir(path) != 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    dError("failed to create dir:%s since %s", path, terrstr());
    goto _OVER;
  }

S
shm  
Shengliang Guan 已提交
96
  pDnode->wrappers[DNODE].fp = dmGetMgmtFp();
S
shm  
Shengliang Guan 已提交
97 98 99 100 101 102 103 104 105 106 107
  pDnode->wrappers[MNODE].fp = mmGetMgmtFp();
  pDnode->wrappers[VNODES].fp = vmGetMgmtFp();
  pDnode->wrappers[QNODE].fp = qmGetMgmtFp();
  pDnode->wrappers[SNODE].fp = smGetMgmtFp();
  pDnode->wrappers[BNODE].fp = bmGetMgmtFp();
  pDnode->wrappers[DNODE].name = "dnode";
  pDnode->wrappers[MNODE].name = "mnode";
  pDnode->wrappers[VNODES].name = "vnodes";
  pDnode->wrappers[QNODE].name = "qnode";
  pDnode->wrappers[SNODE].name = "snode";
  pDnode->wrappers[BNODE].name = "bnode";
S
Shengliang Guan 已提交
108
  memcpy(&pDnode->cfg, pCfg, sizeof(SDndCfg));
S
Shengliang Guan 已提交
109

S
Shengliang Guan 已提交
110
  for (ENodeType n = 0; n < NODE_MAX; ++n) {
S
shm  
Shengliang Guan 已提交
111 112 113 114
    SMgmtWrapper *pWrapper = &pDnode->wrappers[n];
    snprintf(path, sizeof(path), "%s%s%s", pCfg->dataDir, TD_DIRSEP, pDnode->wrappers[n].name);
    pWrapper->path = strdup(path);
    if (pDnode->wrappers[n].path == NULL) {
S
Shengliang Guan 已提交
115 116 117 118
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      goto _OVER;
    }

S
shm  
Shengliang Guan 已提交
119 120 121 122
    pWrapper->procType = PROC_SINGLE;
    pWrapper->required = dndRequireNode(pWrapper);
    if (pWrapper->required) {
      if (taosMkDir(pWrapper->path) != 0) {
S
Shengliang Guan 已提交
123
        terrno = TAOS_SYSTEM_ERROR(errno);
S
shm  
Shengliang Guan 已提交
124
        dError("failed to create dir:%s since %s", pWrapper->path, terrstr());
S
Shengliang Guan 已提交
125 126 127
        goto _OVER;
      }
    }
S
Shengliang Guan 已提交
128 129
  }

S
Shengliang Guan 已提交
130 131 132
  pDnode->pLockFile = dndCheckRunning(pCfg->dataDir);
  if (pDnode->pLockFile == NULL) {
    goto _OVER;
S
tfs cfg  
Shengliang Guan 已提交
133 134
  }

S
shm  
Shengliang Guan 已提交
135 136
  code = 0;

S
Shengliang Guan 已提交
137 138
_OVER:
  if (code != 0 && pDnode) {
S
shm  
Shengliang Guan 已提交
139
    dndClearMemory(pDnode);
S
Shengliang Guan 已提交
140 141 142 143
    tfree(pDnode);
    dError("failed to create dnode object since %s", terrstr());
  } else {
    dInfo("dnode object is created, data:%p", pDnode);
S
Shengliang Guan 已提交
144 145
  }

S
Shengliang Guan 已提交
146 147 148
  return pDnode;
}

S
Shengliang Guan 已提交
149
void dndClose(SDnode *pDnode) {
S
Shengliang Guan 已提交
150 151
  if (pDnode == NULL) return;

S
shm  
Shengliang Guan 已提交
152
  if (dndGetStatus(pDnode) == DND_STAT_STOPPED) {
S
Shengliang Guan 已提交
153
    dError("dnode is shutting down, data:%p", pDnode);
S
Shengliang Guan 已提交
154 155 156
    return;
  }

S
Shengliang Guan 已提交
157
  dInfo("start to close dnode, data:%p", pDnode);
S
shm  
Shengliang Guan 已提交
158
  dndSetStatus(pDnode, DND_STAT_STOPPED);
159

S
shm  
Shengliang Guan 已提交
160 161
  dndClearResource(pDnode);
  dndClearMemory(pDnode);
S
Shengliang Guan 已提交
162
  tfree(pDnode);
S
Shengliang Guan 已提交
163
  dInfo("dnode object is closed, data:%p", pDnode);
S
Shengliang Guan 已提交
164
}
S
Shengliang Guan 已提交
165

S
shm  
Shengliang Guan 已提交
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
static int32_t dndOpenNode(SDnode *pDnode, SMgmtWrapper *pWrapper) {
  // if (tsMultiProcess) {
  //   SProcCfg cfg = {0};
  //   pWrapper->pProc = taosProcInit(&cfg);
  //   if (taosProcIsChild(pWrapper->pProc)) {
  //     pWrapper->procType = PROC_CHILD;
  //     dInfo("node:%s, will start in child process", pWrapper->name);
  //   } else {
  //     pWrapper->procType = PROC_PARENT;
  //     dInfo("node:%s, will start in parent process", pWrapper->name);
  //   }
  // } else {
  //   pWrapper->procType = PROC_SINGLE;
  //   dInfo("node:%s, will start in single process mnode", pWrapper->name);
  // }

  // if (pWrapper->procType == PROC_SINGLE || pWrapper->procType == PROC_CHILD) {
  //   SDndInfo info;
  //   pWrapper->pNode = (*pWrapper->fp.openFp)(pWrapper->path, &info);
  //   if (pWrapper != NULL) {
  //     return -1;
  //   }
  // }

  // return 0;

S
shm  
Shengliang Guan 已提交
192
   (*pWrapper->fp.openFp)(pWrapper);
S
shm  
Shengliang Guan 已提交
193 194 195 196 197 198 199 200 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 236 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 268 269 270 271 272 273
  return 0;
}

static void dndClearNodeExecpt(SDnode *pDnode, SMgmtWrapper *pWrapper){}

static int32_t dndRunInSingleProcess(SDnode *pDnode) {
  dInfo("dnode run in single process mode");
  for (ENodeType n = 0; n < NODE_MAX; ++n) {
    SMgmtWrapper *pWrapper = &pDnode->wrappers[n];
    dInfo("node:%s, will start in single process", pWrapper->name);
    if (dndOpenNode(pDnode, pWrapper) != 0) {
      dError("node:%s, failed to start since %s", pWrapper->name, terrstr());
      return -1;
    }
  }
  return 0;
}

static int32_t dndRunInMultiProcess(SDnode *pDnode) {
  for (ENodeType n = 0; n < NODE_MAX; ++n) {
    SMgmtWrapper *pWrapper = &pDnode->wrappers[n];
    if (n == DNODE) {
      dInfo("node:%s, will start in parent process", pWrapper->name);
      pWrapper->procType = PROC_PARENT;
      if (dndOpenNode(pDnode, pWrapper) != 0) {
        dError("node:%s, failed to start since %s", pWrapper->name, terrstr());
        return -1;
      }
      continue;
    }

    SProcCfg  cfg = {0};
    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;
      dndResetLog(pWrapper);

      dInfo("node:%s, clean up resources inherited from parent", pWrapper->name);
      dndClearNodeExecpt(pDnode, pWrapper);

      dInfo("node:%s, init trans client in child process", pWrapper->name);
      dndInitClient(pDnode);

      dInfo("node:%s, will be initialized in child process", pWrapper->name);
      dndOpenNode(pDnode, pWrapper);
    } else {
      dInfo("node:%s, will not start in parent process", pWrapper->name);
      pWrapper->procType = PROC_PARENT;
      dndOpenNode(pDnode, pWrapper);
    }
  }

  return 0;
}

int32_t dndRun(SDnode *pDnode) {
  if (tsMultiProcess == 0) {
    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;
    }
  }

  while (1) {
    if (pDnode->event != DND_EVENT_STOP) {
      dInfo("dnode object receive stop event");
      break;
    }
S
shm  
Shengliang Guan 已提交
274 275
    taosMsleep(100);
  }
S
shm  
Shengliang Guan 已提交
276 277

  return 0;
S
shm  
Shengliang Guan 已提交
278
}
S
shm  
Shengliang Guan 已提交
279

S
shm  
Shengliang Guan 已提交
280
void dndProcessRpcMsg(SDnode *pDnode, SMgmtWrapper *pWrapper, SRpcMsg *pMsg, SEpSet *pEpSet) {}