qmWorker.c 5.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 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) {
S
Shengliang Guan 已提交
20 21 22 23 24 25 26 27
  SRpcMsg rsp = {
      .handle = pMsg->rpcMsg.handle,
      .ahandle = pMsg->rpcMsg.ahandle,
      .refId = pMsg->rpcMsg.refId,
      .code = code,
      .pCont = pMsg->pRsp,
      .contLen = pMsg->rspLen,
  };
S
Shengliang Guan 已提交
28
  tmsgSendRsp(&rsp);
S
Shengliang Guan 已提交
29 30
}

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

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

38
  if (pMsg->rpcMsg.msgType == TDMT_MON_QM_INFO) {
S
Shengliang 已提交
39
    code = qmProcessGetMonitorInfoReq(pMgmt, pMsg);
40 41
  } else {
    terrno = TSDB_CODE_MSG_NOT_PROCESSED;
42 43 44
  }

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

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

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

57 58 59 60 61
  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) {
62
    qmSendRsp(pMsg, code);
S
shm  
Shengliang Guan 已提交
63 64
  }

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

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

73 74 75 76 77
  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) {
78
    qmSendRsp(pMsg, code);
S
Shengliang Guan 已提交
79 80 81 82 83 84
  }

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

S
Shengliang 已提交
86
static int32_t qmPutNodeMsgToWorker(SSingleWorker *pWorker, SNodeMsg *pMsg) {
S
Shengliang Guan 已提交
87
  dTrace("msg:%p, put into worker %s", pMsg, pWorker->name);
S
Shengliang Guan 已提交
88
  taosWriteQitem(pWorker->queue, pMsg);
S
Shengliang 已提交
89
  return 0;
S
shm  
Shengliang Guan 已提交
90 91
}

S
Shengliang 已提交
92 93
int32_t qmPutNodeMsgToQueryQueue(SQnodeMgmt *pMgmt, SNodeMsg *pMsg) {
  return qmPutNodeMsgToWorker(&pMgmt->queryWorker, pMsg);
S
Shengliang Guan 已提交
94
}
S
shm  
Shengliang Guan 已提交
95

S
Shengliang 已提交
96 97
int32_t qmPutNodeMsgToFetchQueue(SQnodeMgmt *pMgmt, SNodeMsg *pMsg) {
  return qmPutNodeMsgToWorker(&pMgmt->fetchWorker, pMsg);
S
Shengliang Guan 已提交
98
}
S
Shengliang Guan 已提交
99

S
Shengliang 已提交
100 101
int32_t qmPutNodeMsgToMonitorQueue(SQnodeMgmt *pMgmt, SNodeMsg *pMsg) {
  return qmPutNodeMsgToWorker(&pMgmt->monitorWorker, pMsg);
102 103
}

S
Shengliang Guan 已提交
104
static int32_t qmPutRpcMsgToWorker(SQnodeMgmt *pMgmt, SSingleWorker *pWorker, SRpcMsg *pRpc) {
S
Shengliang Guan 已提交
105 106 107 108 109 110 111
  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 已提交
112 113
  taosWriteQitem(pWorker->queue, pMsg);
  return 0;
S
Shengliang Guan 已提交
114 115
}

S
Shengliang 已提交
116
int32_t qmPutRpcMsgToQueryQueue(SQnodeMgmt *pMgmt, SRpcMsg *pRpc) {
S
Shengliang Guan 已提交
117 118 119
  return qmPutRpcMsgToWorker(pMgmt, &pMgmt->queryWorker, pRpc);
}

S
Shengliang 已提交
120
int32_t qmPutRpcMsgToFetchQueue(SQnodeMgmt *pMgmt, SRpcMsg *pRpc) {
S
Shengliang Guan 已提交
121
  return qmPutRpcMsgToWorker(pMgmt, &pMgmt->fetchWorker, pRpc);
S
shm  
Shengliang Guan 已提交
122 123
}

S
Shengliang 已提交
124 125
int32_t qmGetQueueSize(SQnodeMgmt *pMgmt, int32_t vgId, EQueueType qtype) {
  int32_t size = -1;
126

S
Shengliang Guan 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139 140
  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 已提交
141
int32_t qmStartWorker(SQnodeMgmt *pMgmt) {
S
Shengliang Guan 已提交
142 143 144 145 146 147 148
  SSingleWorkerCfg queryCfg = {
      .min = tsNumOfVnodeQueryThreads,
      .max = tsNumOfVnodeQueryThreads,
      .name = "qnode-query",
      .fp = (FItem)qmProcessQueryQueue,
      .param = pMgmt,
  };
S
Shengliang Guan 已提交
149

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

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

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

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

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

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