dmMgmt.c 9.2 KB
Newer Older
S
shm  
Shengliang Guan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * 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/>.
 */

S
shm  
Shengliang Guan 已提交
16
#define _DEFAULT_SOURCE
S
Shengliang Guan 已提交
17
#include "dmMgmt.h"
S
Shengliang Guan 已提交
18
#include "dmNodes.h"
S
Shengliang Guan 已提交
19

20
static bool dmRequireNode(SDnode *pDnode, SMgmtWrapper *pWrapper) {
S
Shengliang Guan 已提交
21
  SMgmtInputOpt input = dmBuildMgmtInputOpt(pWrapper);
S
Shengliang Guan 已提交
22 23

  bool    required = false;
S
Shengliang Guan 已提交
24
  int32_t code = (*pWrapper->func.requiredFp)(&input, &required);
S
Shengliang Guan 已提交
25 26 27 28
  if (!required) {
    dDebug("node:%s, does not require startup", pWrapper->name);
  }

29 30 31 32 33 34 35 36 37 38 39 40
  if (pWrapper->ntype == DNODE) {
    if (pDnode->rtype != DNODE && pDnode->rtype != NODE_END) {
      required = false;
      dDebug("node:%s, does not require startup in child process", pWrapper->name);
    }
  } else {
    if (OnlyInChildProc(pWrapper)) {
      if (pWrapper->ntype != pDnode->rtype) {
        dDebug("node:%s, does not require startup in child process", pWrapper->name);
        required = false;
      }
    }
S
Shengliang Guan 已提交
41 42
  }

S
Shengliang Guan 已提交
43 44 45 46
  if (required) {
    dDebug("node:%s, required to startup", pWrapper->name);
  }

S
Shengliang Guan 已提交
47 48
  return required;
}
S
Shengliang Guan 已提交
49

50 51
static int32_t dmInitVars(SDnode *pDnode, EDndNodeType rtype) {
  pDnode->rtype = rtype;
S
Shengliang Guan 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64

  if (tsMultiProcess == 0) {
    pDnode->ptype = DND_PROC_SINGLE;
    dInfo("dnode will run in single-process mode");
  } else if (tsMultiProcess > 1) {
    pDnode->ptype = DND_PROC_TEST;
    dInfo("dnode will run in multi-process test mode");
  } else if (pDnode->rtype == DNODE || pDnode->rtype == NODE_END) {
    pDnode->ptype = DND_PROC_PARENT;
    dInfo("dnode will run in parent-process mode");
  } else {
    pDnode->ptype = DND_PROC_CHILD;
    SMgmtWrapper *pWrapper = &pDnode->wrappers[pDnode->rtype];
S
Shengliang Guan 已提交
65
    dInfo("dnode will run in child-process mode, node:%s", dmNodeName(pDnode->rtype));
S
Shengliang Guan 已提交
66 67
  }

S
Shengliang Guan 已提交
68 69 70 71 72 73 74 75
  SDnodeData *pData = &pDnode->data;
  pData->dnodeId = 0;
  pData->clusterId = 0;
  pData->dnodeVer = 0;
  pData->updateTime = 0;
  pData->rebootTime = taosGetTimestampMs();
  pData->dropped = 0;
  pData->stopped = 0;
S
shm  
Shengliang Guan 已提交
76

S
Shengliang Guan 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
  pData->dnodeHash = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_NO_LOCK);
  if (pData->dnodeHash == NULL) {
    dError("failed to init dnode hash");
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

  if (dmReadEps(pData) != 0) {
    dError("failed to read file since %s", terrstr());
    return -1;
  }

  if (pData->dropped) {
    dError("dnode will not start since its already dropped");
    return -1;
  }

94
  taosThreadRwlockInit(&pData->lock, NULL);
95
  taosThreadMutexInit(&pDnode->mutex, NULL);
S
shm  
Shengliang Guan 已提交
96 97 98
  return 0;
}

S
Shengliang Guan 已提交
99
static void dmClearVars(SDnode *pDnode) {
S
Shengliang Guan 已提交
100 101 102
  for (EDndNodeType ntype = DNODE; ntype < NODE_END; ++ntype) {
    SMgmtWrapper *pWrapper = &pDnode->wrappers[ntype];
    taosMemoryFreeClear(pWrapper->path);
103
    taosThreadRwlockDestroy(&pWrapper->lock);
S
Shengliang Guan 已提交
104 105 106 107 108 109 110
  }
  if (pDnode->lockfile != NULL) {
    taosUnLockFile(pDnode->lockfile);
    taosCloseFile(&pDnode->lockfile);
    pDnode->lockfile = NULL;
  }

S
Shengliang Guan 已提交
111
  SDnodeData *pData = &pDnode->data;
112
  taosThreadRwlockWrlock(&pData->lock);
S
Shengliang Guan 已提交
113 114 115 116 117 118 119 120
  if (pData->dnodeEps != NULL) {
    taosArrayDestroy(pData->dnodeEps);
    pData->dnodeEps = NULL;
  }
  if (pData->dnodeHash != NULL) {
    taosHashCleanup(pData->dnodeHash);
    pData->dnodeHash = NULL;
  }
121
  taosThreadRwlockUnlock(&pData->lock);
S
Shengliang Guan 已提交
122

123
  taosThreadRwlockDestroy(&pData->lock);
S
Shengliang Guan 已提交
124 125
  taosThreadMutexDestroy(&pDnode->mutex);
  memset(&pDnode->mutex, 0, sizeof(pDnode->mutex));
S
shm  
Shengliang Guan 已提交
126 127
}

128
int32_t dmInitDnode(SDnode *pDnode, EDndNodeType rtype) {
S
Shengliang Guan 已提交
129
  dInfo("start to create dnode");
S
shm  
Shengliang Guan 已提交
130
  int32_t code = -1;
S
Shengliang Guan 已提交
131
  char    path[PATH_MAX + 100] = {0};
S
shm  
Shengliang Guan 已提交
132

133
  if (dmInitVars(pDnode, rtype) != 0) {
S
Shengliang Guan 已提交
134 135 136
    goto _OVER;
  }

S
Shengliang Guan 已提交
137 138 139 140 141 142
  pDnode->wrappers[DNODE].func = dmGetMgmtFunc();
  pDnode->wrappers[MNODE].func = mmGetMgmtFunc();
  pDnode->wrappers[VNODE].func = vmGetMgmtFunc();
  pDnode->wrappers[QNODE].func = qmGetMgmtFunc();
  pDnode->wrappers[SNODE].func = smGetMgmtFunc();
  pDnode->wrappers[BNODE].func = bmGetMgmtFunc();
S
shm  
Shengliang Guan 已提交
143

S
Shengliang 已提交
144 145
  for (EDndNodeType ntype = DNODE; ntype < NODE_END; ++ntype) {
    SMgmtWrapper *pWrapper = &pDnode->wrappers[ntype];
146
    pWrapper->pDnode = pDnode;
S
Shengliang Guan 已提交
147
    pWrapper->name = dmNodeName(ntype);
S
Shengliang Guan 已提交
148 149 150 151 152 153 154 155
    pWrapper->ntype = ntype;
    pWrapper->proc.wrapper = pWrapper;
    pWrapper->proc.shm.id = -1;
    pWrapper->proc.pid = -1;
    pWrapper->proc.ptype = pDnode->ptype;
    if (ntype == DNODE) {
      pWrapper->proc.ptype = DND_PROC_SINGLE;
    }
156
    taosThreadRwlockInit(&pWrapper->lock, NULL);
S
Shengliang Guan 已提交
157

158
    snprintf(path, sizeof(path), "%s%s%s", tsDataDir, TD_DIRSEP, pWrapper->name);
S
Shengliang Guan 已提交
159
    pWrapper->path = strdup(path);
S
shm  
Shengliang Guan 已提交
160 161 162 163 164
    if (pWrapper->path == NULL) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      goto _OVER;
    }

165
    pWrapper->required = dmRequireNode(pDnode, pWrapper);
S
Shengliang Guan 已提交
166

S
Shengliang Guan 已提交
167
    if (ntype != DNODE && dmReadShmFile(pWrapper->path, pWrapper->name, pDnode->rtype, &pWrapper->proc.shm) != 0) {
S
Shengliang Guan 已提交
168 169 170
      dError("node:%s, failed to read shm file since %s", pWrapper->name, terrstr());
      goto _OVER;
    }
S
shm  
Shengliang Guan 已提交
171 172
  }

S
Shengliang Guan 已提交
173
  if (dmInitMsgHandle(pDnode) != 0) {
S
Shengliang Guan 已提交
174
    dError("failed to init msg handles since %s", terrstr());
S
shm  
Shengliang Guan 已提交
175 176 177
    goto _OVER;
  }

178
  if (pDnode->ptype == SINGLE_PROC || (pDnode->ptype & PARENT_PROC)) {
179
    pDnode->lockfile = dmCheckRunning(tsDataDir);
S
Shengliang Guan 已提交
180 181 182 183 184 185 186 187 188 189
    if (pDnode->lockfile == NULL) {
      goto _OVER;
    }

    if (dmInitServer(pDnode) != 0) {
      dError("failed to init transport since %s", terrstr());
      goto _OVER;
    }
  }

S
Shengliang Guan 已提交
190 191 192 193
  if (dmInitClient(pDnode) != 0) {
    goto _OVER;
  }

194
  dmReportStartup("dnode-transport", "initialized");
S
Shengliang Guan 已提交
195
  dDebug("dnode is created, ptr:%p", pDnode);
S
shm  
Shengliang Guan 已提交
196 197 198
  code = 0;

_OVER:
S
Shengliang Guan 已提交
199
  if (code != 0 && pDnode != NULL) {
S
Shengliang Guan 已提交
200
    dmClearVars(pDnode);
201
    pDnode = NULL;
S
shm  
Shengliang Guan 已提交
202
    dError("failed to create dnode since %s", terrstr());
S
shm  
Shengliang Guan 已提交
203 204
  }

205
  return code;
S
shm  
Shengliang Guan 已提交
206 207
}

208
void dmCleanupDnode(SDnode *pDnode) {
S
shm  
Shengliang Guan 已提交
209
  if (pDnode == NULL) return;
S
Shengliang Guan 已提交
210 211 212

  dmCleanupClient(pDnode);
  dmCleanupServer(pDnode);
S
Shengliang Guan 已提交
213
  dmClearVars(pDnode);
S
Shengliang Guan 已提交
214
  dDebug("dnode is closed, ptr:%p", pDnode);
S
shm  
Shengliang Guan 已提交
215
}
S
Shengliang Guan 已提交
216 217 218 219 220 221 222 223 224 225 226 227

void dmSetStatus(SDnode *pDnode, EDndRunStatus status) {
  if (pDnode->status != status) {
    dDebug("dnode status set from %s to %s", dmStatStr(pDnode->status), dmStatStr(status));
    pDnode->status = status;
  }
}

SMgmtWrapper *dmAcquireWrapper(SDnode *pDnode, EDndNodeType ntype) {
  SMgmtWrapper *pWrapper = &pDnode->wrappers[ntype];
  SMgmtWrapper *pRetWrapper = pWrapper;

228
  taosThreadRwlockRdlock(&pWrapper->lock);
S
Shengliang Guan 已提交
229 230
  if (pWrapper->deployed) {
    int32_t refCount = atomic_add_fetch_32(&pWrapper->refCount, 1);
S
Shengliang Guan 已提交
231
    dTrace("node:%s, is acquired, ref:%d", pWrapper->name, refCount);
S
Shengliang Guan 已提交
232 233 234 235
  } else {
    terrno = TSDB_CODE_NODE_NOT_DEPLOYED;
    pRetWrapper = NULL;
  }
236
  taosThreadRwlockUnlock(&pWrapper->lock);
S
Shengliang Guan 已提交
237 238 239 240 241 242 243

  return pRetWrapper;
}

int32_t dmMarkWrapper(SMgmtWrapper *pWrapper) {
  int32_t code = 0;

244
  taosThreadRwlockRdlock(&pWrapper->lock);
245
  if (pWrapper->deployed || (InParentProc(pWrapper) && pWrapper->required)) {
S
Shengliang Guan 已提交
246
    int32_t refCount = atomic_add_fetch_32(&pWrapper->refCount, 1);
S
Shengliang Guan 已提交
247
    dTrace("node:%s, is marked, ref:%d", pWrapper->name, refCount);
S
Shengliang Guan 已提交
248 249 250 251
  } else {
    terrno = TSDB_CODE_NODE_NOT_DEPLOYED;
    code = -1;
  }
252
  taosThreadRwlockUnlock(&pWrapper->lock);
S
Shengliang Guan 已提交
253 254 255 256 257 258 259

  return code;
}

void dmReleaseWrapper(SMgmtWrapper *pWrapper) {
  if (pWrapper == NULL) return;

260
  taosThreadRwlockRdlock(&pWrapper->lock);
S
Shengliang Guan 已提交
261
  int32_t refCount = atomic_sub_fetch_32(&pWrapper->refCount, 1);
262
  taosThreadRwlockUnlock(&pWrapper->lock);
S
Shengliang Guan 已提交
263
  dTrace("node:%s, is released, ref:%d", pWrapper->name, refCount);
S
Shengliang Guan 已提交
264 265
}

266
static void dmGetServerStartupStatus(SDnode *pDnode, SServerStatusRsp *pStatus) {
S
Shengliang Guan 已提交
267
  SDnodeMgmt *pMgmt = pDnode->wrappers[DNODE].pMgmt;
S
Shengliang Guan 已提交
268 269 270 271 272
  pStatus->details[0] = 0;

  if (pDnode->status == DND_STAT_INIT) {
    pStatus->statusCode = TSDB_SRV_STATUS_NETWORK_OK;
    snprintf(pStatus->details, sizeof(pStatus->details), "%s: %s", pDnode->startup.name, pDnode->startup.desc);
273
  } else if (pDnode->status == DND_STAT_STOPPED) {
S
Shengliang Guan 已提交
274
    pStatus->statusCode = TSDB_SRV_STATUS_EXTING;
275 276
  } else {
    pStatus->statusCode = TSDB_SRV_STATUS_SERVICE_OK;
S
Shengliang Guan 已提交
277 278 279
  }
}

S
Shengliang Guan 已提交
280
void dmProcessNetTestReq(SDnode *pDnode, SRpcMsg *pMsg) {
S
Shengliang Guan 已提交
281
  dDebug("msg:%p, net test req will be processed", pMsg);
S
Shengliang Guan 已提交
282 283
  SRpcMsg rsp = {.code = 0, .info = pMsg->info};
  rsp.pCont = rpcMallocCont(pMsg->contLen);
S
Shengliang Guan 已提交
284 285 286
  if (rsp.pCont == NULL) {
    rsp.code = TSDB_CODE_OUT_OF_MEMORY;
  } else {
S
Shengliang Guan 已提交
287
    rsp.contLen = pMsg->contLen;
S
Shengliang Guan 已提交
288 289 290 291
  }
  rpcSendResponse(&rsp);
}

S
Shengliang Guan 已提交
292
void dmProcessServerStartupStatus(SDnode *pDnode, SRpcMsg *pMsg) {
S
Shengliang Guan 已提交
293
  dDebug("msg:%p, server startup status req will be processed", pMsg);
S
Shengliang Guan 已提交
294
  SServerStatusRsp statusRsp = {0};
295
  dmGetServerStartupStatus(pDnode, &statusRsp);
S
Shengliang Guan 已提交
296

S
Shengliang Guan 已提交
297
  SRpcMsg rspMsg = {.info = pMsg->info};
S
Shengliang Guan 已提交
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
  int32_t rspLen = tSerializeSServerStatusRsp(NULL, 0, &statusRsp);
  if (rspLen < 0) {
    rspMsg.code = TSDB_CODE_OUT_OF_MEMORY;
    goto _OVER;
  }

  void *pRsp = rpcMallocCont(rspLen);
  if (pRsp == NULL) {
    rspMsg.code = TSDB_CODE_OUT_OF_MEMORY;
    goto _OVER;
  }

  tSerializeSServerStatusRsp(pRsp, rspLen, &statusRsp);
  rspMsg.pCont = pRsp;
  rspMsg.contLen = rspLen;

_OVER:
  rpcSendResponse(&rspMsg);
}