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");
S
shm  
Shengliang Guan 已提交
114 115 116
  return 0;
}

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

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

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

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

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

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