dmMgmt.c 8.8 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

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

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

28
  if (pWrapper->ntype == DNODE && pDnode->rtype != DNODE && pDnode->rtype != NODE_END) {
S
Shengliang Guan 已提交
29 30 31 32
    required = false;
    dDebug("node:%s, does not require startup in child process", pWrapper->name);
  }

S
Shengliang Guan 已提交
33 34 35 36
  if (required) {
    dDebug("node:%s, required to startup", pWrapper->name);
  }

S
Shengliang Guan 已提交
37 38
  return required;
}
S
Shengliang Guan 已提交
39

40 41
static int32_t dmInitVars(SDnode *pDnode, EDndNodeType rtype) {
  pDnode->rtype = rtype;
S
Shengliang Guan 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54

  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 已提交
55
    dInfo("dnode will run in child-process mode, node:%s", dmNodeName(pDnode->rtype));
S
Shengliang Guan 已提交
56 57
  }

S
Shengliang Guan 已提交
58 59 60 61 62 63 64 65
  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 已提交
66

S
Shengliang Guan 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
  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;
  }

  taosInitRWLatch(&pData->latch);
85
  taosThreadMutexInit(&pDnode->mutex, NULL);
S
shm  
Shengliang Guan 已提交
86 87 88
  return 0;
}

S
Shengliang Guan 已提交
89
static void dmClearVars(SDnode *pDnode) {
S
Shengliang Guan 已提交
90 91 92 93 94 95 96 97 98 99
  for (EDndNodeType ntype = DNODE; ntype < NODE_END; ++ntype) {
    SMgmtWrapper *pWrapper = &pDnode->wrappers[ntype];
    taosMemoryFreeClear(pWrapper->path);
  }
  if (pDnode->lockfile != NULL) {
    taosUnLockFile(pDnode->lockfile);
    taosCloseFile(&pDnode->lockfile);
    pDnode->lockfile = NULL;
  }

S
Shengliang Guan 已提交
100 101 102 103 104 105 106 107 108 109 110 111
  SDnodeData *pData = &pDnode->data;
  taosWLockLatch(&pData->latch);
  if (pData->dnodeEps != NULL) {
    taosArrayDestroy(pData->dnodeEps);
    pData->dnodeEps = NULL;
  }
  if (pData->dnodeHash != NULL) {
    taosHashCleanup(pData->dnodeHash);
    pData->dnodeHash = NULL;
  }
  taosWUnLockLatch(&pData->latch);

S
Shengliang Guan 已提交
112 113 114
  taosThreadMutexDestroy(&pDnode->mutex);
  memset(&pDnode->mutex, 0, sizeof(pDnode->mutex));
  taosMemoryFree(pDnode);
S
shm  
Shengliang Guan 已提交
115 116
}

117
int32_t dmInitDnode(SDnode *pDnode, EDndNodeType rtype) {
S
Shengliang Guan 已提交
118
  dInfo("start to create dnode");
S
shm  
Shengliang Guan 已提交
119
  int32_t code = -1;
S
Shengliang Guan 已提交
120
  char    path[PATH_MAX + 100] = {0};
S
shm  
Shengliang Guan 已提交
121

122
  if (dmInitVars(pDnode, rtype) != 0) {
S
Shengliang Guan 已提交
123 124 125
    goto _OVER;
  }

S
Shengliang Guan 已提交
126 127 128 129 130 131
  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 已提交
132

S
Shengliang 已提交
133 134
  for (EDndNodeType ntype = DNODE; ntype < NODE_END; ++ntype) {
    SMgmtWrapper *pWrapper = &pDnode->wrappers[ntype];
135
    pWrapper->pDnode = pDnode;
S
Shengliang Guan 已提交
136
    pWrapper->name = dmNodeName(ntype);
S
Shengliang Guan 已提交
137 138 139 140 141 142 143 144
    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;
    }
S
Shengliang Guan 已提交
145 146
    taosInitRWLatch(&pWrapper->latch);

147
    snprintf(path, sizeof(path), "%s%s%s", tsDataDir, TD_DIRSEP, pWrapper->name);
S
Shengliang Guan 已提交
148
    pWrapper->path = strdup(path);
S
shm  
Shengliang Guan 已提交
149 150 151 152 153
    if (pWrapper->path == NULL) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      goto _OVER;
    }

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

S
Shengliang Guan 已提交
156
    if (ntype != DNODE && dmReadShmFile(pWrapper->path, pWrapper->name, pDnode->rtype, &pWrapper->proc.shm) != 0) {
S
Shengliang Guan 已提交
157 158 159
      dError("node:%s, failed to read shm file since %s", pWrapper->name, terrstr());
      goto _OVER;
    }
S
shm  
Shengliang Guan 已提交
160 161
  }

S
Shengliang Guan 已提交
162
  if (dmInitMsgHandle(pDnode) != 0) {
S
Shengliang Guan 已提交
163
    dError("failed to init msg handles since %s", terrstr());
S
shm  
Shengliang Guan 已提交
164 165 166
    goto _OVER;
  }

167
  if (pDnode->ptype == SINGLE_PROC || (pDnode->ptype & PARENT_PROC)) {
168
    pDnode->lockfile = dmCheckRunning(tsDataDir);
S
Shengliang Guan 已提交
169 170 171 172 173 174 175 176 177 178
    if (pDnode->lockfile == NULL) {
      goto _OVER;
    }

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

S
Shengliang Guan 已提交
179 180 181 182
  if (dmInitClient(pDnode) != 0) {
    goto _OVER;
  }

183
  dmReportStartup("dnode-transport", "initialized");
S
Shengliang Guan 已提交
184
  dInfo("dnode is created, ptr:%p", pDnode);
S
shm  
Shengliang Guan 已提交
185 186 187
  code = 0;

_OVER:
S
Shengliang Guan 已提交
188
  if (code != 0 && pDnode != NULL) {
S
Shengliang Guan 已提交
189
    dmClearVars(pDnode);
190
    pDnode = NULL;
S
shm  
Shengliang Guan 已提交
191
    dError("failed to create dnode since %s", terrstr());
S
shm  
Shengliang Guan 已提交
192 193
  }

194
  return code;
S
shm  
Shengliang Guan 已提交
195 196
}

197
void dmCleanupDnode(SDnode *pDnode) {
S
shm  
Shengliang Guan 已提交
198
  if (pDnode == NULL) return;
S
Shengliang Guan 已提交
199 200 201

  dmCleanupClient(pDnode);
  dmCleanupServer(pDnode);
S
Shengliang Guan 已提交
202
  dmClearVars(pDnode);
S
Shengliang Guan 已提交
203
  dInfo("dnode is closed, ptr:%p", pDnode);
S
shm  
Shengliang Guan 已提交
204
}
S
Shengliang Guan 已提交
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219

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;

  taosRLockLatch(&pWrapper->latch);
  if (pWrapper->deployed) {
    int32_t refCount = atomic_add_fetch_32(&pWrapper->refCount, 1);
S
Shengliang Guan 已提交
220
    dTrace("node:%s, is acquired, ref:%d", pWrapper->name, refCount);
S
Shengliang Guan 已提交
221 222 223 224 225 226 227 228 229 230 231 232 233
  } else {
    terrno = TSDB_CODE_NODE_NOT_DEPLOYED;
    pRetWrapper = NULL;
  }
  taosRUnLockLatch(&pWrapper->latch);

  return pRetWrapper;
}

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

  taosRLockLatch(&pWrapper->latch);
234
  if (pWrapper->deployed || (InParentProc(pWrapper) && pWrapper->required)) {
S
Shengliang Guan 已提交
235
    int32_t refCount = atomic_add_fetch_32(&pWrapper->refCount, 1);
S
Shengliang Guan 已提交
236
    dTrace("node:%s, is marked, ref:%d", pWrapper->name, refCount);
S
Shengliang Guan 已提交
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
  } else {
    terrno = TSDB_CODE_NODE_NOT_DEPLOYED;
    code = -1;
  }
  taosRUnLockLatch(&pWrapper->latch);

  return code;
}

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

  taosRLockLatch(&pWrapper->latch);
  int32_t refCount = atomic_sub_fetch_32(&pWrapper->refCount, 1);
  taosRUnLockLatch(&pWrapper->latch);
S
Shengliang Guan 已提交
252
  dTrace("node:%s, is released, ref:%d", pWrapper->name, refCount);
S
Shengliang Guan 已提交
253 254
}

255
static void dmGetServerStartupStatus(SDnode *pDnode, SServerStatusRsp *pStatus) {
S
Shengliang Guan 已提交
256
  SDnodeMgmt *pMgmt = pDnode->wrappers[DNODE].pMgmt;
S
Shengliang Guan 已提交
257 258 259 260 261
  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);
262
  } else if (pDnode->status == DND_STAT_STOPPED) {
S
Shengliang Guan 已提交
263
    pStatus->statusCode = TSDB_SRV_STATUS_EXTING;
264 265
  } else {
    pStatus->statusCode = TSDB_SRV_STATUS_SERVICE_OK;
S
Shengliang Guan 已提交
266 267 268 269 270
  }
}

void dmProcessNetTestReq(SDnode *pDnode, SRpcMsg *pReq) {
  dDebug("net test req is received");
271
  SRpcMsg rsp = {.code = 0, .info = pReq->info};
S
Shengliang Guan 已提交
272 273 274 275 276 277 278 279 280
  rsp.pCont = rpcMallocCont(pReq->contLen);
  if (rsp.pCont == NULL) {
    rsp.code = TSDB_CODE_OUT_OF_MEMORY;
  } else {
    rsp.contLen = pReq->contLen;
  }
  rpcSendResponse(&rsp);
}

281 282
void dmProcessServerStartupStatus(SDnode *pDnode, SRpcMsg *pReq) {
  dDebug("server startup status req is received");
S
Shengliang Guan 已提交
283 284

  SServerStatusRsp statusRsp = {0};
285
  dmGetServerStartupStatus(pDnode, &statusRsp);
S
Shengliang Guan 已提交
286

S
Shengliang Guan 已提交
287
  SRpcMsg rspMsg = {.info = pReq->info};
S
Shengliang Guan 已提交
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
  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);
}