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

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

S
shm  
Shengliang Guan 已提交
21
static void qmInitOption(SQnodeMgmt *pMgmt, SQnodeOpt *pOption) {
S
Shengliang Guan 已提交
22
  SMsgCb msgCb = pMgmt->pDnode->data.msgCb;
S
Shengliang Guan 已提交
23
  msgCb.pWrapper = pMgmt->pWrapper;
S
Shengliang Guan 已提交
24 25
  msgCb.queueFps[QUERY_QUEUE] = qmPutMsgToQueryQueue;
  msgCb.queueFps[FETCH_QUEUE] = qmPutMsgToFetchQueue;
S
Shengliang Guan 已提交
26
  msgCb.qsizeFp = qmGetQueueSize;
S
Shengliang Guan 已提交
27
  pOption->msgCb = msgCb;
S
shm  
Shengliang Guan 已提交
28 29
}

S
Shengliang Guan 已提交
30 31 32
static void qmClose(SMgmtWrapper *pWrapper) {
  SQnodeMgmt *pMgmt = pWrapper->pMgmt;
  if (pMgmt == NULL) return;
S
shm  
Shengliang Guan 已提交
33

S
Shengliang Guan 已提交
34
  dInfo("qnode-mgmt start to cleanup");
S
shm  
Shengliang Guan 已提交
35 36 37 38 39 40 41
  if (pMgmt->pQnode != NULL) {
    qmStopWorker(pMgmt);
    qndClose(pMgmt->pQnode);
    pMgmt->pQnode = NULL;
  }

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

S
Shengliang Guan 已提交
46
static int32_t qmOpen(SMgmtWrapper *pWrapper) {
S
shm  
Shengliang Guan 已提交
47
  dInfo("qnode-mgmt start to init");
wafwerar's avatar
wafwerar 已提交
48
  SQnodeMgmt *pMgmt = taosMemoryCalloc(1, sizeof(SQnodeMgmt));
S
shm  
Shengliang Guan 已提交
49 50 51 52 53 54 55 56 57 58
  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 已提交
59 60 61 62 63
  SQnodeOpt option = {0};
  qmInitOption(pMgmt, &option);
  pMgmt->pQnode = qndOpen(&option);
  if (pMgmt->pQnode == NULL) {
    dError("failed to open qnode since %s", terrstr());
S
shm  
Shengliang Guan 已提交
64
    qmClose(pWrapper);
S
Shengliang Guan 已提交
65
    return -1;
S
shm  
Shengliang Guan 已提交
66 67
  }

S
Shengliang Guan 已提交
68 69 70 71 72 73 74 75
  if (qmStartWorker(pMgmt) != 0) {
    dError("failed to start qnode worker since %s", terrstr());
    qmClose(pWrapper);
    return -1;
  }

  dInfo("qnode-mgmt is initialized");
  return 0;
S
shm  
Shengliang Guan 已提交
76 77
}

S
Shengliang Guan 已提交
78
void qmSetMgmtFp(SMgmtWrapper *pWrapper) {
S
shm  
Shengliang Guan 已提交
79
  SMgmtFp mgmtFp = {0};
S
shm  
Shengliang Guan 已提交
80 81
  mgmtFp.openFp = qmOpen;
  mgmtFp.closeFp = qmClose;
S
Shengliang Guan 已提交
82 83
  mgmtFp.createFp = qmProcessCreateReq;
  mgmtFp.dropFp = qmProcessDropReq;
S
shm  
Shengliang Guan 已提交
84
  mgmtFp.requiredFp = qmRequire;
S
shm  
Shengliang Guan 已提交
85

S
Shengliang Guan 已提交
86
  qmInitMsgHandle(pWrapper);
S
shm  
Shengliang Guan 已提交
87 88
  pWrapper->name = "qnode";
  pWrapper->fp = mgmtFp;
S
shm  
Shengliang Guan 已提交
89
}