bmInt.c 2.3 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
Shengliang Guan 已提交
27 28 29
static void bmClose(SMgmtWrapper *pWrapper) {
  SBnodeMgmt *pMgmt = pWrapper->pMgmt;
  if (pMgmt == NULL) return;
S
shm  
Shengliang Guan 已提交
30

S
Shengliang Guan 已提交
31
  dInfo("bnode-mgmt start to cleanup");
S
shm  
Shengliang Guan 已提交
32
  if (pMgmt->pBnode != NULL) {
S
shm  
Shengliang Guan 已提交
33 34 35
    bmStopWorker(pMgmt);
    bndClose(pMgmt->pBnode);
    pMgmt->pBnode = NULL;
S
shm  
Shengliang Guan 已提交
36 37
  }

S
shm  
Shengliang Guan 已提交
38
  pWrapper->pMgmt = NULL;
wafwerar's avatar
wafwerar 已提交
39
  taosMemoryFree(pMgmt);
S
shm  
Shengliang Guan 已提交
40 41 42
  dInfo("bnode-mgmt is cleaned up");
}

S
shm  
Shengliang Guan 已提交
43
int32_t bmOpen(SMgmtWrapper *pWrapper) {
S
shm  
Shengliang Guan 已提交
44
  dInfo("bnode-mgmt start to init");
wafwerar's avatar
wafwerar 已提交
45
  SBnodeMgmt *pMgmt = taosMemoryCalloc(1, sizeof(SBnodeMgmt));
S
shm  
Shengliang Guan 已提交
46 47 48 49
  if (pMgmt == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
S
shm  
Shengliang Guan 已提交
50 51 52 53

  pMgmt->path = pWrapper->path;
  pMgmt->pDnode = pWrapper->pDnode;
  pMgmt->pWrapper = pWrapper;
S
shm  
Shengliang Guan 已提交
54
  pWrapper->pMgmt = pMgmt;
S
shm  
Shengliang Guan 已提交
55

S
Shengliang Guan 已提交
56 57 58 59 60
  SBnodeOpt option = {0};
  bmInitOption(pMgmt, &option);
  pMgmt->pBnode = bndOpen(pMgmt->path, &option);
  if (pMgmt->pBnode == NULL) {
    dError("failed to open bnode since %s", terrstr());
S
shm  
Shengliang Guan 已提交
61
    bmClose(pWrapper);
S
Shengliang Guan 已提交
62
    return -1;
S
shm  
Shengliang Guan 已提交
63 64
  }

S
Shengliang Guan 已提交
65 66 67 68 69 70 71
  if (bmStartWorker(pMgmt) != 0) {
    dError("failed to start bnode worker since %s", terrstr());
    bmClose(pWrapper);
    return -1;
  }

  return 0;
S
shm  
Shengliang Guan 已提交
72 73
}

S
Shengliang Guan 已提交
74
void bmSetMgmtFp(SMgmtWrapper *pWrapper) {
S
shm  
Shengliang Guan 已提交
75
  SMgmtFp mgmtFp = {0};
S
shm  
Shengliang Guan 已提交
76 77
  mgmtFp.openFp = bmOpen;
  mgmtFp.closeFp = bmClose;
S
Shengliang Guan 已提交
78 79
  mgmtFp.createFp = bmProcessCreateReq;
  mgmtFp.dropFp = bmProcessDropReq;
S
shm  
Shengliang Guan 已提交
80
  mgmtFp.requiredFp = bmRequire;
S
shm  
Shengliang Guan 已提交
81

S
Shengliang Guan 已提交
82
  bmInitMsgHandle(pWrapper);
S
shm  
Shengliang Guan 已提交
83
  pWrapper->name = "bnode";
S
shm  
Shengliang Guan 已提交
84
  pWrapper->fp = mgmtFp;
S
shm  
Shengliang Guan 已提交
85
}