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

S
Shengliang Guan 已提交
19
static int32_t bmRequire(SMgmtWrapper *pWrapper, bool *required) { return dmReadFile(pWrapper, required); }
S
shm  
Shengliang Guan 已提交
20

S
shm  
Shengliang Guan 已提交
21
static void bmInitOption(SBnodeMgmt *pMgmt, SBnodeOpt *pOption) {
S
Shengliang Guan 已提交
22 23
  SMsgCb msgCb = pMgmt->pDnode->data.msgCb;
  msgCb.pWrapper = pMgmt->pWrapper;
S
Shengliang Guan 已提交
24
  pOption->msgCb = msgCb;
S
shm  
Shengliang Guan 已提交
25 26
}

S
shm  
Shengliang Guan 已提交
27
static int32_t bmOpenImp(SBnodeMgmt *pMgmt) {
S
shm  
Shengliang Guan 已提交
28 29
  SBnodeOpt option = {0};
  bmInitOption(pMgmt, &option);
S
shm  
Shengliang Guan 已提交
30

S
shm  
Shengliang Guan 已提交
31 32
  pMgmt->pBnode = bndOpen(pMgmt->path, &option);
  if (pMgmt->pBnode == NULL) {
S
shm  
Shengliang Guan 已提交
33 34 35 36
    dError("failed to open bnode since %s", terrstr());
    return -1;
  }

S
shm  
Shengliang Guan 已提交
37
  if (bmStartWorker(pMgmt) != 0) {
S
shm  
Shengliang Guan 已提交
38 39 40 41
    dError("failed to start bnode worker since %s", terrstr());
    return -1;
  }

S
shm  
Shengliang Guan 已提交
42
  bool deployed = true;
S
Shengliang Guan 已提交
43
  if (dmWriteFile(pMgmt->pWrapper, deployed) != 0) {
S
shm  
Shengliang Guan 已提交
44 45 46 47 48 49 50
    dError("failed to write bnode file since %s", terrstr());
    return -1;
  }

  return 0;
}

S
shm  
Shengliang Guan 已提交
51
static void bmCloseImp(SBnodeMgmt *pMgmt) {
S
shm  
Shengliang Guan 已提交
52
  if (pMgmt->pBnode != NULL) {
S
shm  
Shengliang Guan 已提交
53 54 55
    bmStopWorker(pMgmt);
    bndClose(pMgmt->pBnode);
    pMgmt->pBnode = NULL;
S
shm  
Shengliang Guan 已提交
56
  }
S
shm  
Shengliang Guan 已提交
57
}
S
shm  
Shengliang Guan 已提交
58

S
shm  
Shengliang Guan 已提交
59 60 61
int32_t bmDrop(SMgmtWrapper *pWrapper) {
  SBnodeMgmt *pMgmt = pWrapper->pMgmt;
  if (pMgmt == NULL) return 0;
S
shm  
Shengliang Guan 已提交
62

S
shm  
Shengliang Guan 已提交
63 64
  dInfo("bnode-mgmt start to drop");
  bool deployed = false;
S
Shengliang Guan 已提交
65
  if (dmWriteFile(pWrapper, deployed) != 0) {
S
shm  
Shengliang Guan 已提交
66 67 68 69
    dError("failed to drop bnode since %s", terrstr());
    return -1;
  }

S
shm  
Shengliang Guan 已提交
70
  bmCloseImp(pMgmt);
S
Shengliang Guan 已提交
71
  taosRemoveDir(pMgmt->path);
S
shm  
Shengliang Guan 已提交
72
  pWrapper->pMgmt = NULL;
wafwerar's avatar
wafwerar 已提交
73
  taosMemoryFree(pMgmt);
S
shm  
Shengliang Guan 已提交
74
  dInfo("bnode-mgmt is dropped");
S
shm  
Shengliang Guan 已提交
75 76 77
  return 0;
}

S
shm  
Shengliang Guan 已提交
78
static void bmClose(SMgmtWrapper *pWrapper) {
S
shm  
Shengliang Guan 已提交
79 80 81 82
  SBnodeMgmt *pMgmt = pWrapper->pMgmt;
  if (pMgmt == NULL) return;

  dInfo("bnode-mgmt start to cleanup");
S
shm  
Shengliang Guan 已提交
83
  bmCloseImp(pMgmt);
S
shm  
Shengliang Guan 已提交
84
  pWrapper->pMgmt = NULL;
wafwerar's avatar
wafwerar 已提交
85
  taosMemoryFree(pMgmt);
S
shm  
Shengliang Guan 已提交
86 87 88
  dInfo("bnode-mgmt is cleaned up");
}

S
shm  
Shengliang Guan 已提交
89
int32_t bmOpen(SMgmtWrapper *pWrapper) {
S
shm  
Shengliang Guan 已提交
90
  dInfo("bnode-mgmt start to init");
wafwerar's avatar
wafwerar 已提交
91
  SBnodeMgmt *pMgmt = taosMemoryCalloc(1, sizeof(SBnodeMgmt));
S
shm  
Shengliang Guan 已提交
92 93 94 95
  if (pMgmt == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
S
shm  
Shengliang Guan 已提交
96 97 98 99

  pMgmt->path = pWrapper->path;
  pMgmt->pDnode = pWrapper->pDnode;
  pMgmt->pWrapper = pWrapper;
S
shm  
Shengliang Guan 已提交
100
  pWrapper->pMgmt = pMgmt;
S
shm  
Shengliang Guan 已提交
101

S
shm  
Shengliang Guan 已提交
102 103
  int32_t code = bmOpenImp(pMgmt);
  if (code != 0) {
S
shm  
Shengliang Guan 已提交
104
    dError("failed to init bnode-mgmt since %s", terrstr());
S
shm  
Shengliang Guan 已提交
105 106 107
    bmClose(pWrapper);
  } else {
    dInfo("bnode-mgmt is initialized");
S
shm  
Shengliang Guan 已提交
108 109 110 111 112
  }

  return code;
}

S
Shengliang Guan 已提交
113
void bmSetMgmtFp(SMgmtWrapper *pWrapper) {
S
shm  
Shengliang Guan 已提交
114
  SMgmtFp mgmtFp = {0};
S
shm  
Shengliang Guan 已提交
115 116
  mgmtFp.openFp = bmOpen;
  mgmtFp.closeFp = bmClose;
S
Shengliang Guan 已提交
117 118
  mgmtFp.createFp = bmProcessCreateReq;
  mgmtFp.dropFp = bmProcessDropReq;
S
shm  
Shengliang Guan 已提交
119
  mgmtFp.requiredFp = bmRequire;
S
shm  
Shengliang Guan 已提交
120

S
Shengliang Guan 已提交
121
  bmInitMsgHandle(pWrapper);
S
shm  
Shengliang Guan 已提交
122
  pWrapper->name = "bnode";
S
shm  
Shengliang Guan 已提交
123
  pWrapper->fp = mgmtFp;
S
shm  
Shengliang Guan 已提交
124
}