bmInt.c 2.4 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 已提交
19 20
static int32_t bmRequire(const SMgmtInputOpt *pInput, bool *required) {
  return dmReadFile(pInput->path, pInput->name, required);
S
shm  
Shengliang Guan 已提交
21 22
}

S
Shengliang 已提交
23
static void bmInitOption(SBnodeMgmt *pMgmt, SBnodeOpt *pOption) { pOption->msgCb = pMgmt->msgCb; }
S
shm  
Shengliang Guan 已提交
24

S
Shengliang 已提交
25
static void bmClose(SBnodeMgmt *pMgmt) {
S
Shengliang Guan 已提交
26
  dInfo("bnode-mgmt start to cleanup");
S
shm  
Shengliang Guan 已提交
27
  if (pMgmt->pBnode != NULL) {
S
shm  
Shengliang Guan 已提交
28 29 30
    bmStopWorker(pMgmt);
    bndClose(pMgmt->pBnode);
    pMgmt->pBnode = NULL;
S
shm  
Shengliang Guan 已提交
31 32
  }

wafwerar's avatar
wafwerar 已提交
33
  taosMemoryFree(pMgmt);
S
shm  
Shengliang Guan 已提交
34 35 36
  dInfo("bnode-mgmt is cleaned up");
}

S
Shengliang 已提交
37
int32_t bmOpen(const SMgmtInputOpt *pInput, SMgmtOutputOpt *pOutput) {
S
shm  
Shengliang Guan 已提交
38
  dInfo("bnode-mgmt start to init");
wafwerar's avatar
wafwerar 已提交
39
  SBnodeMgmt *pMgmt = taosMemoryCalloc(1, sizeof(SBnodeMgmt));
S
shm  
Shengliang Guan 已提交
40 41 42 43
  if (pMgmt == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
S
shm  
Shengliang Guan 已提交
44

S
Shengliang 已提交
45 46 47 48 49
  pMgmt->path = pInput->path;
  pMgmt->name = pInput->name;
  pMgmt->dnodeId = pInput->dnodeId;
  pMgmt->msgCb = pInput->msgCb;
  pMgmt->msgCb.pMgmt = pMgmt;
S
shm  
Shengliang Guan 已提交
50

S
Shengliang Guan 已提交
51 52 53 54 55
  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
Shengliang 已提交
56
    bmClose(pMgmt);
S
Shengliang Guan 已提交
57
    return -1;
S
shm  
Shengliang Guan 已提交
58
  }
S
Shengliang 已提交
59
  tmsgReportStartup("bnode-impl", "initialized");
S
shm  
Shengliang Guan 已提交
60

S
Shengliang Guan 已提交
61 62
  if (bmStartWorker(pMgmt) != 0) {
    dError("failed to start bnode worker since %s", terrstr());
S
Shengliang 已提交
63
    bmClose(pMgmt);
S
Shengliang Guan 已提交
64 65
    return -1;
  }
S
Shengliang 已提交
66
  tmsgReportStartup("bnode-worker", "initialized");
S
Shengliang Guan 已提交
67

S
Shengliang 已提交
68 69
  pOutput->pMgmt = pMgmt;
  dInfo("bnode-mgmt is initialized");
S
Shengliang Guan 已提交
70
  return 0;
S
shm  
Shengliang Guan 已提交
71 72
}

S
Shengliang 已提交
73 74 75 76 77 78 79 80
SMgmtFunc bmGetMgmtFunc() {
  SMgmtFunc mgmtFunc = {0};
  mgmtFunc.openFp = bmOpen;
  mgmtFunc.closeFp = (NodeCloseFp)bmClose;
  mgmtFunc.createFp = (NodeCreateFp)bmProcessCreateReq;
  mgmtFunc.dropFp = (NodeDropFp)bmProcessDropReq;
  mgmtFunc.requiredFp = bmRequire;
  mgmtFunc.getHandlesFp = bmGetMsgHandles;
S
shm  
Shengliang Guan 已提交
81

S
Shengliang 已提交
82
  return mgmtFunc;
S
shm  
Shengliang Guan 已提交
83
}