dndInt.c 3.8 KB
Newer Older
S
shm  
Shengliang Guan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * 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
#include "dndInt.h"
S
xshm  
Shengliang Guan 已提交
18
#include "wal.h"
S
shm  
Shengliang Guan 已提交
19

S
shm  
Shengliang Guan 已提交
20 21
static int8_t once = DND_ENV_INIT;

S
shm  
Shengliang Guan 已提交
22
int32_t dndInit() {
S
xshm  
Shengliang Guan 已提交
23
  dInfo("start to init dnode env");
S
shm  
Shengliang Guan 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
  if (atomic_val_compare_exchange_8(&once, DND_ENV_INIT, DND_ENV_READY) != DND_ENV_INIT) {
    terrno = TSDB_CODE_REPEAT_INIT;
    dError("failed to init dnode env since %s", terrstr());
    return -1;
  }

  taosIgnSIGPIPE();
  taosBlockSIGPIPE();
  taosResolveCRC();

  if (rpcInit() != 0) {
    dError("failed to init rpc since %s", terrstr());
    dndCleanup();
    return -1;
  }

  SMonCfg monCfg = {0};
  monCfg.maxLogs = tsMonitorMaxLogs;
  monCfg.port = tsMonitorPort;
  monCfg.server = tsMonitorFqdn;
  monCfg.comp = tsMonitorComp;
  if (monInit(&monCfg) != 0) {
    dError("failed to init monitor since %s", terrstr());
    dndCleanup();
    return -1;
  }

  dInfo("dnode env is initialized");
  return 0;
}

void dndCleanup() {
S
xshm  
Shengliang Guan 已提交
56
  dInfo("start to cleanup dnode env");
S
shm  
Shengliang Guan 已提交
57 58 59 60 61 62 63
  if (atomic_val_compare_exchange_8(&once, DND_ENV_READY, DND_ENV_CLEANUP) != DND_ENV_READY) {
    dError("dnode env is already cleaned up");
    return;
  }

  monCleanup();
  rpcCleanup();
S
shm  
Shengliang Guan 已提交
64
  walCleanUp();
S
shm  
Shengliang Guan 已提交
65 66 67 68
  taosStopCacheRefreshWorker();
  dInfo("dnode env is cleaned up");
}

69
void dndSetMsgHandle(SMgmtWrapper *pWrapper, int32_t msgType, NodeMsgFp nodeMsgFp, int32_t vgId) {
S
shm  
Shengliang Guan 已提交
70
  pWrapper->msgFps[TMSG_INDEX(msgType)] = nodeMsgFp;
71
  pWrapper->msgVgIds[TMSG_INDEX(msgType)] = vgId;
S
shm  
Shengliang Guan 已提交
72 73
}

S
shm  
Shengliang Guan 已提交
74 75 76 77 78 79 80 81 82
EDndStatus dndGetStatus(SDnode *pDnode) { return pDnode->status; }

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

S
xshm  
Shengliang Guan 已提交
83
void dndReportStartup(SDnode *pDnode, const char *pName, const char *pDesc) {
S
shm  
Shengliang Guan 已提交
84 85 86 87 88 89 90 91 92 93 94
  SStartupReq *pStartup = &pDnode->startup;
  tstrncpy(pStartup->name, pName, TSDB_STEP_NAME_LEN);
  tstrncpy(pStartup->desc, pDesc, TSDB_STEP_DESC_LEN);
  pStartup->finished = 0;
}

void dndGetStartup(SDnode *pDnode, SStartupReq *pStartup) {
  memcpy(pStartup, &pDnode->startup, sizeof(SStartupReq));
  pStartup->finished = (dndGetStatus(pDnode) == DND_STAT_RUNNING);
}

S
xshm  
Shengliang Guan 已提交
95
TdFilePtr dndCheckRunning(const char *dataDir) {
S
shm  
Shengliang Guan 已提交
96 97 98 99 100 101
  char filepath[PATH_MAX] = {0};
  snprintf(filepath, sizeof(filepath), "%s/.running", dataDir);

  TdFilePtr pFile = taosOpenFile(filepath, TD_FILE_CTEATE | TD_FILE_WRITE | TD_FILE_TRUNC);
  if (pFile == NULL) {
    terrno = TAOS_SYSTEM_ERROR(errno);
S
xshm  
Shengliang Guan 已提交
102
    dError("failed to lock file:%s since %s", filepath, terrstr());
S
shm  
Shengliang Guan 已提交
103 104 105 106 107 108
    return NULL;
  }

  int32_t ret = taosLockFile(pFile);
  if (ret != 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
S
xshm  
Shengliang Guan 已提交
109
    dError("failed to lock file:%s since %s", filepath, terrstr());
S
shm  
Shengliang Guan 已提交
110 111 112 113
    taosCloseFile(&pFile);
    return NULL;
  }

S
shm  
Shengliang Guan 已提交
114
  dDebug("file:%s is locked", filepath);
S
shm  
Shengliang Guan 已提交
115 116
  return pFile;
}
S
shm  
Shengliang Guan 已提交
117 118 119 120 121 122 123

void dndProcessStartupReq(SDnode *pDnode, SRpcMsg *pReq) {
  dDebug("startup req is received");
  SStartupReq *pStartup = rpcMallocCont(sizeof(SStartupReq));
  dndGetStartup(pDnode, pStartup);

  dDebug("startup req is sent, step:%s desc:%s finished:%d", pStartup->name, pStartup->desc, pStartup->finished);
dengyihao's avatar
dengyihao 已提交
124 125
  SRpcMsg rpcRsp = {
      .handle = pReq->handle, .pCont = pStartup, .contLen = sizeof(SStartupReq), .ahandle = pReq->ahandle};
S
shm  
Shengliang Guan 已提交
126 127
  rpcSendResponse(&rpcRsp);
}