smInt.c 2.5 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
Shengliang Guan 已提交
27
  dInfo("snode-mgmt start to cleanup");
S
shm  
Shengliang Guan 已提交
28 29 30 31 32 33
  if (pMgmt->pSnode != NULL) {
    smStopWorker(pMgmt);
    sndClose(pMgmt->pSnode);
    pMgmt->pSnode = NULL;
  }

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

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

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

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

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

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

S
Shengliang 已提交
75
  pOutput->pMgmt = pMgmt;
S
Shengliang Guan 已提交
76
  return 0;
S
shm  
Shengliang Guan 已提交
77 78
}

S
Shengliang 已提交
79 80 81 82 83 84 85 86
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 已提交
87

S
Shengliang 已提交
88
  return mgmtFunc;
S
shm  
Shengliang Guan 已提交
89
}