qmWorker.c 5.2 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 inline void qmSendRsp(SRpcMsg *pMsg, int32_t code) {
S
Shengliang Guan 已提交
20 21
  SRpcMsg rsp = {
      .code = code,
S
Shengliang Guan 已提交
22
      .info = pMsg->info,
S
Shengliang Guan 已提交
23 24
      .pCont = pMsg->info.rsp,
      .contLen = pMsg->info.rspLen,
S
Shengliang Guan 已提交
25
  };
S
Shengliang Guan 已提交
26
  tmsgSendRsp(&rsp);
S
Shengliang Guan 已提交
27 28
}

S
Shengliang Guan 已提交
29
static void qmProcessMonitorQueue(SQueueInfo *pInfo, SRpcMsg *pMsg) {
30 31
  SQnodeMgmt *pMgmt = pInfo->ahandle;

32
  dTrace("msg:%p, get from qnode-monitor queue", pMsg);
S
Shengliang Guan 已提交
33
  SRpcMsg *pRpc = pMsg;
34 35
  int32_t  code = -1;

S
Shengliang Guan 已提交
36
  if (pMsg->msgType == TDMT_MON_QM_INFO) {
S
Shengliang 已提交
37
    code = qmProcessGetMonitorInfoReq(pMgmt, 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
  }

S
Shengliang Guan 已提交
47
  dTrace("msg:%p, is freed, code:0x%x", pMsg, code);
48 49 50 51
  rpcFreeCont(pRpc->pCont);
  taosFreeQitem(pMsg);
}

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

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

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

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

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

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

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

S
Shengliang Guan 已提交
79
  dTrace("msg:%p, is freed, code:0x%x", pMsg, code);
S
Shengliang Guan 已提交
80
  rpcFreeCont(pMsg->pCont);
S
Shengliang Guan 已提交
81 82
  taosFreeQitem(pMsg);
}
S
shm  
Shengliang Guan 已提交
83

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

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

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

S
Shengliang Guan 已提交
98
int32_t qmPutNodeMsgToMonitorQueue(SQnodeMgmt *pMgmt, SRpcMsg *pMsg) {
S
Shengliang 已提交
99
  return qmPutNodeMsgToWorker(&pMgmt->monitorWorker, pMsg);
100 101
}

S
Shengliang Guan 已提交
102
static int32_t qmPutRpcMsgToWorker(SQnodeMgmt *pMgmt, SSingleWorker *pWorker, SRpcMsg *pRpc) {
S
Shengliang Guan 已提交
103
  SRpcMsg *pMsg = taosAllocateQitem(sizeof(SRpcMsg), RPC_QITEM);
S
Shengliang Guan 已提交
104 105 106 107
  if (pMsg == NULL) {
    return -1;
  }

S
Shengliang Guan 已提交
108
  dTrace("msg:%p, create and put into worker:%s, type:%s", pMsg, pWorker->name, TMSG_INFO(pRpc->msgType));
S
Shengliang Guan 已提交
109
  memcpy(pMsg, pRpc, sizeof(SRpcMsg));
S
Shengliang Guan 已提交
110 111
  taosWriteQitem(pWorker->queue, pMsg);
  return 0;
S
Shengliang Guan 已提交
112 113
}

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

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

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

S
Shengliang Guan 已提交
125 126
  switch (qtype) {
    case QUERY_QUEUE:
127
      size = taosQueueItemSize(pMgmt->queryWorker.queue);
S
Shengliang Guan 已提交
128 129
      break;
    case FETCH_QUEUE:
130
      size = taosQueueItemSize(pMgmt->fetchWorker.queue);
S
Shengliang Guan 已提交
131 132 133 134 135 136 137 138
      break;
    default:
      break;
  }

  return size;
}

S
shm  
Shengliang Guan 已提交
139
int32_t qmStartWorker(SQnodeMgmt *pMgmt) {
S
Shengliang Guan 已提交
140 141 142 143 144 145 146
  SSingleWorkerCfg queryCfg = {
      .min = tsNumOfVnodeQueryThreads,
      .max = tsNumOfVnodeQueryThreads,
      .name = "qnode-query",
      .fp = (FItem)qmProcessQueryQueue,
      .param = pMgmt,
  };
S
Shengliang Guan 已提交
147

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

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

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

S
Shengliang Guan 已提交
166 167 168 169 170 171 172 173 174 175
  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;
176 177
  }

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

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