dmNodes.c 4.4 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 "dmMgmt.h"
S
Shengliang Guan 已提交
18

S
Shengliang Guan 已提交
19
int32_t dmOpenNode(SMgmtWrapper *pWrapper) {
20
  SDnode *pDnode = pWrapper->pDnode;
21

S
shm  
Shengliang Guan 已提交
22 23 24 25 26 27
  if (taosMkDir(pWrapper->path) != 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    dError("node:%s, failed to create dir:%s since %s", pWrapper->name, pWrapper->path, terrstr());
    return -1;
  }

S
Shengliang Guan 已提交
28
  SMgmtOutputOpt output = {0};
S
Shengliang Guan 已提交
29
  SMgmtInputOpt  input = dmBuildMgmtInputOpt(pWrapper);
S
Shengliang Guan 已提交
30

31 32 33 34 35
  dInfo("node:%s, start to open", pWrapper->name);
  tmsgSetDefault(&input.msgCb);
  if ((*pWrapper->func.openFp)(&input, &output) != 0) {
    dError("node:%s, failed to open since %s", pWrapper->name, terrstr());
    return -1;
S
shm  
Shengliang Guan 已提交
36
  }
37 38
  dInfo("node:%s, has been opened", pWrapper->name);
  pWrapper->deployed = true;
S
shm  
Shengliang Guan 已提交
39

S
Shengliang Guan 已提交
40 41 42 43
  if (output.pMgmt != NULL) {
    pWrapper->pMgmt = output.pMgmt;
  }

44
  dmReportStartup(pWrapper->name, "openned");
S
shm  
Shengliang Guan 已提交
45
  return 0;
S
shm  
Shengliang Guan 已提交
46
}
S
shm  
Shengliang Guan 已提交
47

S
Shengliang Guan 已提交
48
int32_t dmStartNode(SMgmtWrapper *pWrapper) {
S
Shengliang Guan 已提交
49 50 51 52 53 54 55
  if (pWrapper->func.startFp != NULL) {
    dDebug("node:%s, start to start", pWrapper->name);
    if ((*pWrapper->func.startFp)(pWrapper->pMgmt) != 0) {
      dError("node:%s, failed to start since %s", pWrapper->name, terrstr());
      return -1;
    }
    dDebug("node:%s, has been started", pWrapper->name);
56
  }
S
Shengliang Guan 已提交
57

58
  dmReportStartup(pWrapper->name, "started");
59 60 61
  return 0;
}

S
Shengliang Guan 已提交
62
void dmStopNode(SMgmtWrapper *pWrapper) {
S
Shengliang Guan 已提交
63
  if (pWrapper->func.stopFp != NULL && pWrapper->pMgmt != NULL) {
S
Shengliang Guan 已提交
64
    dDebug("node:%s, start to stop", pWrapper->name);
S
Shengliang Guan 已提交
65
    (*pWrapper->func.stopFp)(pWrapper->pMgmt);
S
Shengliang Guan 已提交
66
    dDebug("node:%s, has been stopped", pWrapper->name);
S
Shengliang Guan 已提交
67
  }
S
Shengliang Guan 已提交
68 69 70 71
}

void dmCloseNode(SMgmtWrapper *pWrapper) {
  dInfo("node:%s, start to close", pWrapper->name);
S
Shengliang Guan 已提交
72
  pWrapper->deployed = false;
73 74 75 76 77

  while (pWrapper->refCount > 0) {
    taosMsleep(10);
  }

78
  taosThreadRwlockWrlock(&pWrapper->lock);
S
Shengliang Guan 已提交
79 80 81 82
  if (pWrapper->pMgmt != NULL) {
    (*pWrapper->func.closeFp)(pWrapper->pMgmt);
    pWrapper->pMgmt = NULL;
  }
83
  taosThreadRwlockUnlock(&pWrapper->lock);
S
Shengliang Guan 已提交
84

S
Shengliang Guan 已提交
85
  dInfo("node:%s, has been closed", pWrapper->name);
S
shm  
Shengliang Guan 已提交
86 87
}

S
Shengliang Guan 已提交
88
static int32_t dmOpenNodes(SDnode *pDnode) {
S
Shengliang Guan 已提交
89 90 91
  for (EDndNodeType ntype = DNODE; ntype < NODE_END; ++ntype) {
    SMgmtWrapper *pWrapper = &pDnode->wrappers[ntype];
    if (!pWrapper->required) continue;
S
Shengliang Guan 已提交
92
    if (dmOpenNode(pWrapper) != 0) {
S
Shengliang Guan 已提交
93
      dError("node:%s, failed to open since %s", pWrapper->name, terrstr());
S
Shengliang Guan 已提交
94
      return -1;
S
Shengliang Guan 已提交
95
    }
S
shm  
Shengliang Guan 已提交
96 97
  }

S
Shengliang Guan 已提交
98
  dmSetStatus(pDnode, DND_STAT_RUNNING);
S
Shengliang Guan 已提交
99 100
  return 0;
}
S
shm  
Shengliang Guan 已提交
101

S
Shengliang Guan 已提交
102
static int32_t dmStartNodes(SDnode *pDnode) {
S
Shengliang Guan 已提交
103 104
  for (EDndNodeType ntype = DNODE; ntype < NODE_END; ++ntype) {
    SMgmtWrapper *pWrapper = &pDnode->wrappers[ntype];
S
Shengliang Guan 已提交
105
    if (!pWrapper->required) continue;
S
Shengliang Guan 已提交
106
    if (dmStartNode(pWrapper) != 0) {
S
shm  
Shengliang Guan 已提交
107 108 109 110 111 112
      dError("node:%s, failed to start since %s", pWrapper->name, terrstr());
      return -1;
    }
  }

  dInfo("TDengine initialized successfully");
113
  dmReportStartup("TDengine", "initialized successfully");
D
dapan1121 已提交
114
  
S
shm  
Shengliang Guan 已提交
115 116 117
  return 0;
}

S
Shengliang Guan 已提交
118 119
static void dmStopNodes(SDnode *pDnode) {
  for (EDndNodeType n = DNODE; n < NODE_END; ++n) {
S
shm  
Shengliang Guan 已提交
120
    SMgmtWrapper *pWrapper = &pDnode->wrappers[n];
S
Shengliang Guan 已提交
121
    dmStopNode(pWrapper);
S
shm  
Shengliang Guan 已提交
122
  }
S
Shengliang Guan 已提交
123
}
S
shm  
Shengliang Guan 已提交
124

S
Shengliang Guan 已提交
125 126
static void dmCloseNodes(SDnode *pDnode) {
  for (EDndNodeType n = DNODE; n < NODE_END; ++n) {
S
shm  
Shengliang Guan 已提交
127
    SMgmtWrapper *pWrapper = &pDnode->wrappers[n];
S
Shengliang Guan 已提交
128
    dmCloseNode(pWrapper);
S
shm  
Shengliang Guan 已提交
129
  }
S
Shengliang Guan 已提交
130
}
S
shm  
Shengliang Guan 已提交
131

132
int32_t dmRunDnode(SDnode *pDnode) {
133
  int32_t count = 0;
S
Shengliang Guan 已提交
134 135
  if (dmOpenNodes(pDnode) != 0) {
    dError("failed to open nodes since %s", terrstr());
S
shm  
Shengliang Guan 已提交
136 137
    return -1;
  }
S
shm  
Shengliang Guan 已提交
138

S
Shengliang Guan 已提交
139 140
  if (dmStartNodes(pDnode) != 0) {
    dError("failed to start nodes since %s", terrstr());
S
shm  
Shengliang Guan 已提交
141
    return -1;
S
shm  
Shengliang Guan 已提交
142
  }
143

S
shm  
Shengliang Guan 已提交
144
  while (1) {
145
    if (pDnode->stop) {
S
Shengliang Guan 已提交
146
      dInfo("TDengine is about to stop");
S
Shengliang Guan 已提交
147
      dmSetStatus(pDnode, DND_STAT_STOPPED);
S
Shengliang Guan 已提交
148 149 150
      dmStopNodes(pDnode);
      dmCloseNodes(pDnode);
      return 0;
S
shm  
Shengliang Guan 已提交
151
    }
S
Shengliang Guan 已提交
152

D
dapan1121 已提交
153 154 155 156 157 158 159
    if (count == 10) {
      osUpdate();
      count = 0;
    } else {
      count++;
    }
    
S
Shengliang Guan 已提交
160
    taosMsleep(100);
S
shm  
Shengliang Guan 已提交
161
  }
S
shm  
Shengliang Guan 已提交
162
}