smInt.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
/*
 * 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
shenglian zhou 已提交
18
#include "libs/function/function.h"
S
shm  
Shengliang Guan 已提交
19

S
Shengliang 已提交
20 21
static int32_t smRequire(const SMgmtInputOpt *pInput, bool *required) {
  return dmReadFile(pInput->path, pInput->name, required);
S
shm  
Shengliang Guan 已提交
22 23
}

S
Shengliang 已提交
24
static void smInitOption(SSnodeMgmt *pMgmt, SSnodeOpt *pOption) { pOption->msgCb = pMgmt->msgCb; }
S
shm  
Shengliang Guan 已提交
25

S
Shengliang 已提交
26
static void smClose(SSnodeMgmt *pMgmt) {
S
shm  
Shengliang Guan 已提交
27 28 29 30 31 32
  if (pMgmt->pSnode != NULL) {
    smStopWorker(pMgmt);
    sndClose(pMgmt->pSnode);
    pMgmt->pSnode = NULL;
  }

wafwerar's avatar
wafwerar 已提交
33
  taosMemoryFree(pMgmt);
S
shm  
Shengliang Guan 已提交
34 35
}

S
Shengliang Guan 已提交
36
int32_t smOpen(SMgmtInputOpt *pInput, SMgmtOutputOpt *pOutput) {
wafwerar's avatar
wafwerar 已提交
37
  SSnodeMgmt *pMgmt = taosMemoryCalloc(1, sizeof(SSnodeMgmt));
S
shm  
Shengliang Guan 已提交
38 39 40 41 42
  if (pMgmt == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

43
  pMgmt->pData = pInput->pData;
S
Shengliang 已提交
44 45 46 47
  pMgmt->path = pInput->path;
  pMgmt->name = pInput->name;
  pMgmt->msgCb = pInput->msgCb;
  pMgmt->msgCb.pMgmt = pMgmt;
S
shm  
Shengliang Guan 已提交
48

S
Shengliang Guan 已提交
49 50 51 52 53
  SSnodeOpt option = {0};
  smInitOption(pMgmt, &option);
  pMgmt->pSnode = sndOpen(pMgmt->path, &option);
  if (pMgmt->pSnode == NULL) {
    dError("failed to open snode since %s", terrstr());
S
Shengliang 已提交
54
    smClose(pMgmt);
S
Shengliang Guan 已提交
55
    return -1;
S
shm  
Shengliang Guan 已提交
56
  }
S
Shengliang 已提交
57
  tmsgReportStartup("snode-impl", "initialized");
S
shm  
Shengliang Guan 已提交
58

S
Shengliang Guan 已提交
59 60
  if (smStartWorker(pMgmt) != 0) {
    dError("failed to start snode worker since %s", terrstr());
S
Shengliang 已提交
61
    smClose(pMgmt);
S
Shengliang Guan 已提交
62 63
    return -1;
  }
S
Shengliang 已提交
64
  tmsgReportStartup("snode-worker", "initialized");
S
Shengliang Guan 已提交
65

S
shenglian zhou 已提交
66 67
  if (udfcOpen() != 0) {
    dError("failed to open udfc in snode");
S
Shengliang 已提交
68 69
    smClose(pMgmt);
    return -1;
S
shenglian zhou 已提交
70 71
  }

S
Shengliang 已提交
72
  pOutput->pMgmt = pMgmt;
S
Shengliang Guan 已提交
73
  return 0;
S
shm  
Shengliang Guan 已提交
74 75
}

S
Shengliang 已提交
76 77 78 79 80 81 82 83
SMgmtFunc smGetMgmtFunc() {
  SMgmtFunc mgmtFunc = {0};
  mgmtFunc.openFp = smOpen;
  mgmtFunc.closeFp = (NodeCloseFp)smClose;
  mgmtFunc.createFp = (NodeCreateFp)smProcessCreateReq;
  mgmtFunc.dropFp = (NodeDropFp)smProcessDropReq;
  mgmtFunc.requiredFp = smRequire;
  mgmtFunc.getHandlesFp = smGetMsgHandles;
S
shm  
Shengliang Guan 已提交
84

S
Shengliang 已提交
85
  return mgmtFunc;
S
shm  
Shengliang Guan 已提交
86
}