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
  SMsgCb msgCb = pMgmt->pDnode->data.msgCb;;
S
Shengliang Guan 已提交
23
  pOption->msgCb = msgCb;
S
shm  
Shengliang Guan 已提交
24 25
}

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

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

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

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

  return 0;
}

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

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

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

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

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

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

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

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

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

  return code;
}

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

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