bmInt.c 3.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
shm  
Shengliang Guan 已提交
19
static int32_t bmRequire(SMgmtWrapper *pWrapper, bool *required) { return dndReadFile(pWrapper, required); }
S
shm  
Shengliang Guan 已提交
20

S
shm  
Shengliang Guan 已提交
21
static void bmInitOption(SBnodeMgmt *pMgmt, SBnodeOpt *pOption) {
S
Shengliang Guan 已提交
22 23 24 25 26
  SMsgCb msgCb = {0};
  msgCb.pWrapper = pMgmt->pWrapper;
  msgCb.sendReqFp = dndSendReqToDnode;
  msgCb.sendMnodeReqFp = dndSendReqToMnode;
  msgCb.sendRspFp = dndSendRsp;
S
Shengliang Guan 已提交
27
  msgCb.registerBrokenLinkArgFp = dndRegisterBrokenLinkArg;
S
Shengliang Guan 已提交
28
  pOption->msgCb = msgCb;
S
shm  
Shengliang Guan 已提交
29 30
}

S
shm  
Shengliang Guan 已提交
31
static int32_t bmOpenImp(SBnodeMgmt *pMgmt) {
S
shm  
Shengliang Guan 已提交
32 33
  SBnodeOpt option = {0};
  bmInitOption(pMgmt, &option);
S
shm  
Shengliang Guan 已提交
34

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

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

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

  return 0;
}

S
shm  
Shengliang Guan 已提交
55
static void bmCloseImp(SBnodeMgmt *pMgmt) {
S
shm  
Shengliang Guan 已提交
56
  if (pMgmt->pBnode != NULL) {
S
shm  
Shengliang Guan 已提交
57 58 59
    bmStopWorker(pMgmt);
    bndClose(pMgmt->pBnode);
    pMgmt->pBnode = NULL;
S
shm  
Shengliang Guan 已提交
60
  }
S
shm  
Shengliang Guan 已提交
61
}
S
shm  
Shengliang Guan 已提交
62

S
shm  
Shengliang Guan 已提交
63 64 65
int32_t bmDrop(SMgmtWrapper *pWrapper) {
  SBnodeMgmt *pMgmt = pWrapper->pMgmt;
  if (pMgmt == NULL) return 0;
S
shm  
Shengliang Guan 已提交
66

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

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

S
shm  
Shengliang Guan 已提交
82
static void bmClose(SMgmtWrapper *pWrapper) {
S
shm  
Shengliang Guan 已提交
83 84 85 86
  SBnodeMgmt *pMgmt = pWrapper->pMgmt;
  if (pMgmt == NULL) return;

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

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

  pMgmt->path = pWrapper->path;
  pMgmt->pDnode = pWrapper->pDnode;
  pMgmt->pWrapper = pWrapper;
S
shm  
Shengliang Guan 已提交
104
  pWrapper->pMgmt = pMgmt;
S
shm  
Shengliang Guan 已提交
105

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

  return code;
}

S
shm  
Shengliang Guan 已提交
117
void bmGetMgmtFp(SMgmtWrapper *pWrapper) {
S
shm  
Shengliang Guan 已提交
118
  SMgmtFp mgmtFp = {0};
S
shm  
Shengliang Guan 已提交
119 120 121 122
  mgmtFp.openFp = bmOpen;
  mgmtFp.closeFp = bmClose;
  mgmtFp.createMsgFp = bmProcessCreateReq;
  mgmtFp.dropMsgFp = bmProcessDropReq;
S
shm  
Shengliang Guan 已提交
123
  mgmtFp.requiredFp = bmRequire;
S
shm  
Shengliang Guan 已提交
124

S
shm  
Shengliang Guan 已提交
125
  bmInitMsgHandles(pWrapper);
S
shm  
Shengliang Guan 已提交
126
  pWrapper->name = "bnode";
S
shm  
Shengliang Guan 已提交
127
  pWrapper->fp = mgmtFp;
S
shm  
Shengliang Guan 已提交
128
}