smInt.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 "smInt.h"

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

S
shm  
Shengliang Guan 已提交
21
static void smInitOption(SSnodeMgmt *pMgmt, SSnodeOpt *pOption) {
S
Shengliang Guan 已提交
22
  SMsgCb msgCb = pMgmt->pDnode->data.msgCb;
S
Shengliang Guan 已提交
23
  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 smClose(SMgmtWrapper *pWrapper) {
  SSnodeMgmt *pMgmt = pWrapper->pMgmt;
  if (pMgmt == NULL) return;
S
shm  
Shengliang Guan 已提交
30

S
Shengliang Guan 已提交
31
  dInfo("snode-mgmt start to cleanup");
S
shm  
Shengliang Guan 已提交
32 33 34 35 36 37 38
  if (pMgmt->pSnode != NULL) {
    smStopWorker(pMgmt);
    sndClose(pMgmt->pSnode);
    pMgmt->pSnode = NULL;
  }

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

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

  pMgmt->path = pWrapper->path;
  pMgmt->pDnode = pWrapper->pDnode;
  pMgmt->pWrapper = pWrapper;
  pWrapper->pMgmt = pMgmt;

S
Shengliang Guan 已提交
56 57 58 59 60 61
  SSnodeOpt option = {0};
  smInitOption(pMgmt, &option);
  pMgmt->pSnode = sndOpen(pMgmt->path, &option);
  if (pMgmt->pSnode == NULL) {
    dError("failed to open snode since %s", terrstr());
    return -1;
S
shm  
Shengliang Guan 已提交
62 63
  }

S
Shengliang Guan 已提交
64 65 66 67 68 69
  if (smStartWorker(pMgmt) != 0) {
    dError("failed to start snode worker since %s", terrstr());
    return -1;
  }

  return 0;
S
shm  
Shengliang Guan 已提交
70 71
}

S
Shengliang Guan 已提交
72
void smSetMgmtFp(SMgmtWrapper *pWrapper) {
S
shm  
Shengliang Guan 已提交
73
  SMgmtFp mgmtFp = {0};
S
shm  
Shengliang Guan 已提交
74 75
  mgmtFp.openFp = smOpen;
  mgmtFp.closeFp = smClose;
S
Shengliang Guan 已提交
76 77
  mgmtFp.createFp = smProcessCreateReq;
  mgmtFp.dropFp = smProcessDropReq;
S
shm  
Shengliang Guan 已提交
78
  mgmtFp.requiredFp = smRequire;
S
shm  
Shengliang Guan 已提交
79

S
Shengliang Guan 已提交
80
  smInitMsgHandle(pWrapper);
S
shm  
Shengliang Guan 已提交
81 82
  pWrapper->name = "snode";
  pWrapper->fp = mgmtFp;
S
shm  
Shengliang Guan 已提交
83
}