qmWorker.c 5.8 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"

19
static inline void qmSendRsp(SNodeMsg *pMsg, int32_t code) {
20 21
  SRpcMsg rsp = {.handle = pMsg->rpcMsg.handle,
                 .ahandle = pMsg->rpcMsg.ahandle,
dengyihao's avatar
dengyihao 已提交
22
                 .refId = pMsg->rpcMsg.refId,
23 24 25
                 .code = code,
                 .pCont = pMsg->pRsp,
                 .contLen = pMsg->rspLen};
S
Shengliang Guan 已提交
26
  tmsgSendRsp(&rsp);
S
Shengliang Guan 已提交
27 28
}

29
static void qmProcessMonitorQueue(SQueueInfo *pInfo, SNodeMsg *pMsg) {
30 31
  SQnodeMgmt *pMgmt = pInfo->ahandle;

32
  dTrace("msg:%p, get from qnode-monitor queue", pMsg);
33 34 35
  SRpcMsg *pRpc = &pMsg->rpcMsg;
  int32_t  code = -1;

36
  if (pMsg->rpcMsg.msgType == TDMT_MON_QM_INFO) {
37
    code = qmProcessGetMonQmInfoReq(pMgmt->pWrapper, pMsg);
38 39
  } else {
    terrno = TSDB_CODE_MSG_NOT_PROCESSED;
40 41 42
  }

  if (pRpc->msgType & 1U) {
43
    if (code != 0 && terrno != 0) code = terrno;
44
    qmSendRsp(pMsg, code);
45 46 47 48 49 50 51
  }

  dTrace("msg:%p, is freed, result:0x%04x:%s", pMsg, code & 0XFFFF, tstrerror(code));
  rpcFreeCont(pRpc->pCont);
  taosFreeQitem(pMsg);
}

S
Shengliang Guan 已提交
52 53 54
static void qmProcessQueryQueue(SQueueInfo *pInfo, SNodeMsg *pMsg) {
  SQnodeMgmt *pMgmt = pInfo->ahandle;

55 56 57 58 59
  dTrace("msg:%p, get from qnode-query queue", pMsg);
  SRpcMsg *pRpc = &pMsg->rpcMsg;
  int32_t  code = qndProcessQueryMsg(pMgmt->pQnode, pRpc);

  if (pRpc->msgType & 1U && code != 0) {
60
    qmSendRsp(pMsg, code);
S
shm  
Shengliang Guan 已提交
61 62
  }

S
shm  
Shengliang Guan 已提交
63
  dTrace("msg:%p, is freed, result:0x%04x:%s", pMsg, code & 0XFFFF, tstrerror(code));
S
Shengliang Guan 已提交
64
  rpcFreeCont(pMsg->rpcMsg.pCont);
S
shm  
Shengliang Guan 已提交
65 66 67
  taosFreeQitem(pMsg);
}

S
Shengliang Guan 已提交
68 69 70
static void qmProcessFetchQueue(SQueueInfo *pInfo, SNodeMsg *pMsg) {
  SQnodeMgmt *pMgmt = pInfo->ahandle;

71 72 73 74 75
  dTrace("msg:%p, get from qnode-fetch queue", pMsg);
  SRpcMsg *pRpc = &pMsg->rpcMsg;
  int32_t  code = qndProcessFetchMsg(pMgmt->pQnode, pRpc);

  if (pRpc->msgType & 1U && code != 0) {
76
    qmSendRsp(pMsg, code);
S
Shengliang Guan 已提交
77 78 79 80 81 82
  }

  dTrace("msg:%p, is freed, result:0x%04x:%s", pMsg, code & 0XFFFF, tstrerror(code));
  rpcFreeCont(pMsg->rpcMsg.pCont);
  taosFreeQitem(pMsg);
}
S
shm  
Shengliang Guan 已提交
83

S
Shengliang Guan 已提交
84
static void qmPutMsgToWorker(SSingleWorker *pWorker, SNodeMsg *pMsg) {
S
Shengliang Guan 已提交
85
  dTrace("msg:%p, put into worker %s", pMsg, pWorker->name);
S
Shengliang Guan 已提交
86
  taosWriteQitem(pWorker->queue, pMsg);
S
shm  
Shengliang Guan 已提交
87 88
}

S
Shengliang Guan 已提交
89 90 91 92 93
int32_t qmProcessQueryMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) {
  SQnodeMgmt *pMgmt = pWrapper->pMgmt;
  qmPutMsgToWorker(&pMgmt->queryWorker, pMsg);
  return 0;
}
S
shm  
Shengliang Guan 已提交
94

S
Shengliang Guan 已提交
95 96 97 98 99
int32_t qmProcessFetchMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) {
  SQnodeMgmt *pMgmt = pWrapper->pMgmt;
  qmPutMsgToWorker(&pMgmt->fetchWorker, pMsg);
  return 0;
}
S
Shengliang Guan 已提交
100

101
int32_t qmProcessMonitorMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) {
102 103
  SQnodeMgmt *pMgmt = pWrapper->pMgmt;
  qmPutMsgToWorker(&pMgmt->monitorWorker, pMsg);
104 105 106
  return 0;
}

S
Shengliang Guan 已提交
107
static int32_t qmPutRpcMsgToWorker(SQnodeMgmt *pMgmt, SSingleWorker *pWorker, SRpcMsg *pRpc) {
S
Shengliang Guan 已提交
108 109 110 111 112 113 114
  SNodeMsg *pMsg = taosAllocateQitem(sizeof(SNodeMsg));
  if (pMsg == NULL) {
    return -1;
  }

  dTrace("msg:%p, is created and put into worker:%s, type:%s", pMsg, pWorker->name, TMSG_INFO(pRpc->msgType));
  pMsg->rpcMsg = *pRpc;
S
Shengliang Guan 已提交
115 116
  taosWriteQitem(pWorker->queue, pMsg);
  return 0;
S
Shengliang Guan 已提交
117 118 119 120 121 122 123 124 125 126
}

int32_t qmPutMsgToQueryQueue(SMgmtWrapper *pWrapper, SRpcMsg *pRpc) {
  SQnodeMgmt *pMgmt = pWrapper->pMgmt;
  return qmPutRpcMsgToWorker(pMgmt, &pMgmt->queryWorker, pRpc);
}

int32_t qmPutMsgToFetchQueue(SMgmtWrapper *pWrapper, SRpcMsg *pRpc) {
  SQnodeMgmt *pMgmt = pWrapper->pMgmt;
  return qmPutRpcMsgToWorker(pMgmt, &pMgmt->fetchWorker, pRpc);
S
shm  
Shengliang Guan 已提交
127 128
}

S
Shengliang Guan 已提交
129 130 131
int32_t qmGetQueueSize(SMgmtWrapper *pWrapper, int32_t vgId, EQueueType qtype) {
  int32_t     size = -1;
  SQnodeMgmt *pMgmt = pWrapper->pMgmt;
132

S
Shengliang Guan 已提交
133 134 135 136 137 138 139 140 141 142 143 144 145 146
  switch (qtype) {
    case QUERY_QUEUE:
      size = taosQueueSize(pMgmt->queryWorker.queue);
      break;
    case FETCH_QUEUE:
      size = taosQueueSize(pMgmt->fetchWorker.queue);
      break;
    default:
      break;
  }

  return size;
}

S
shm  
Shengliang Guan 已提交
147
int32_t qmStartWorker(SQnodeMgmt *pMgmt) {
S
Shengliang Guan 已提交
148 149
  SSingleWorkerCfg queryCfg = {.min = tsNumOfVnodeQueryThreads,
                               .max = tsNumOfVnodeQueryThreads,
S
Shengliang Guan 已提交
150 151 152
                               .name = "qnode-query",
                               .fp = (FItem)qmProcessQueryQueue,
                               .param = pMgmt};
S
Shengliang Guan 已提交
153

S
Shengliang Guan 已提交
154
  if (tSingleWorkerInit(&pMgmt->queryWorker, &queryCfg) != 0) {
S
Shengliang Guan 已提交
155
    dError("failed to start qnode-query worker since %s", terrstr());
S
shm  
Shengliang Guan 已提交
156 157 158
    return -1;
  }

S
Shengliang Guan 已提交
159 160
  SSingleWorkerCfg fetchCfg = {.min = tsNumOfQnodeFetchThreads,
                               .max = tsNumOfQnodeFetchThreads,
S
Shengliang Guan 已提交
161 162 163
                               .name = "qnode-fetch",
                               .fp = (FItem)qmProcessFetchQueue,
                               .param = pMgmt};
S
Shengliang Guan 已提交
164

S
Shengliang Guan 已提交
165
  if (tSingleWorkerInit(&pMgmt->fetchWorker, &fetchCfg) != 0) {
S
Shengliang Guan 已提交
166
    dError("failed to start qnode-fetch worker since %s", terrstr());
S
shm  
Shengliang Guan 已提交
167 168 169
    return -1;
  }

170
  if (tsMultiProcess) {
171
    SSingleWorkerCfg mCfg = {
172
        .min = 1, .max = 1, .name = "qnode-monitor", .fp = (FItem)qmProcessMonitorQueue, .param = pMgmt};
173
    if (tSingleWorkerInit(&pMgmt->monitorWorker, &mCfg) != 0) {
174 175 176 177 178
      dError("failed to start qnode-monitor worker since %s", terrstr());
      return -1;
    }
  }

S
Shengliang Guan 已提交
179
  dDebug("qnode workers are initialized");
S
shm  
Shengliang Guan 已提交
180 181 182 183
  return 0;
}

void qmStopWorker(SQnodeMgmt *pMgmt) {
184
  tSingleWorkerCleanup(&pMgmt->monitorWorker);
S
Shengliang Guan 已提交
185 186
  tSingleWorkerCleanup(&pMgmt->queryWorker);
  tSingleWorkerCleanup(&pMgmt->fetchWorker);
S
Shengliang Guan 已提交
187
  dDebug("qnode workers are closed");
S
shm  
Shengliang Guan 已提交
188
}