dndInt.c 3.7 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
Shengliang Guan 已提交
23
  dDebug("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
  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();

  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());
    return -1;
  }

S
Shengliang Guan 已提交
44
  dDebug("dnode env is initialized");
S
shm  
Shengliang Guan 已提交
45 46 47 48
  return 0;
}

void dndCleanup() {
S
Shengliang Guan 已提交
49
  dDebug("start to cleanup dnode env");
S
shm  
Shengliang Guan 已提交
50 51 52 53 54 55
  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();
S
shm  
Shengliang Guan 已提交
56
  walCleanUp();
S
shm  
Shengliang Guan 已提交
57
  taosStopCacheRefreshWorker();
S
Shengliang Guan 已提交
58
  dDebug("dnode env is cleaned up");
S
shm  
Shengliang Guan 已提交
59 60
}

S
Shengliang Guan 已提交
61
void dndSetMsgHandle(SMgmtWrapper *pWrapper, tmsg_t msgType, NodeMsgFp nodeMsgFp, int8_t vgId) {
S
shm  
Shengliang Guan 已提交
62
  pWrapper->msgFps[TMSG_INDEX(msgType)] = nodeMsgFp;
63
  pWrapper->msgVgIds[TMSG_INDEX(msgType)] = vgId;
S
shm  
Shengliang Guan 已提交
64 65
}

S
shm  
Shengliang Guan 已提交
66 67 68 69 70 71 72 73 74
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 已提交
75
void dndReportStartup(SDnode *pDnode, const char *pName, const char *pDesc) {
S
shm  
Shengliang Guan 已提交
76 77 78 79 80 81 82 83 84 85 86
  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 已提交
87
TdFilePtr dndCheckRunning(const char *dataDir) {
S
shm  
Shengliang Guan 已提交
88 89 90 91 92 93
  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 已提交
94
    dError("failed to lock file:%s since %s", filepath, terrstr());
S
shm  
Shengliang Guan 已提交
95 96 97 98 99 100
    return NULL;
  }

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

S
shm  
Shengliang Guan 已提交
106
  dDebug("file:%s is locked", filepath);
S
shm  
Shengliang Guan 已提交
107 108
  return pFile;
}
S
shm  
Shengliang Guan 已提交
109 110 111 112 113 114 115

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 已提交
116 117
  SRpcMsg rpcRsp = {
      .handle = pReq->handle, .pCont = pStartup, .contLen = sizeof(SStartupReq), .ahandle = pReq->ahandle};
S
shm  
Shengliang Guan 已提交
118 119
  rpcSendResponse(&rpcRsp);
}