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

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 23
      .pCont = pMsg->info.rsp,
      .contLen = pMsg->info.rspLen,
S
Shengliang Guan 已提交
24
      .info = pMsg->info,
S
Shengliang Guan 已提交
25
  };
S
Shengliang Guan 已提交
26
  tmsgSendRsp(&rsp);
S
Shengliang Guan 已提交
27 28
}

S
Shengliang Guan 已提交
29
static void qmProcessQueue(SQueueInfo *pInfo, SRpcMsg *pMsg) {
30
  SQnodeMgmt *pMgmt = pInfo->ahandle;
S
Shengliang Guan 已提交
31 32
  int32_t     code = -1;
  dTrace("msg:%p, get from qnode queue", pMsg);
33

S
Shengliang Guan 已提交
34 35 36 37 38
  switch (pMsg->msgType) {
    case TDMT_MON_QM_INFO:
      code = qmProcessGetMonitorInfoReq(pMgmt, pMsg);
      break;
    default:
D
dapan1121 已提交
39
      code = qndProcessQueryMsg(pMgmt->pQnode, pInfo->timestamp, pMsg);
S
Shengliang Guan 已提交
40
      break;
41 42
  }

S
Shengliang Guan 已提交
43
  if (IsReq(pMsg) && code != TSDB_CODE_ACTION_IN_PROGRESS) {
44
    if (code != 0 && terrno != 0) code = terrno;
45
    qmSendRsp(pMsg, code);
46 47
  }

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

S
Shengliang Guan 已提交
53
static int32_t qmPutNodeMsgToWorker(SSingleWorker *pWorker, SRpcMsg *pMsg) {
S
Shengliang Guan 已提交
54
  dTrace("msg:%p, put into worker %s, type:%s", pMsg, pWorker->name, TMSG_INFO(pMsg->msgType));
S
Shengliang Guan 已提交
55
  taosWriteQitem(pWorker->queue, pMsg);
S
Shengliang 已提交
56
  return 0;
S
shm  
Shengliang Guan 已提交
57 58
}

S
Shengliang Guan 已提交
59
int32_t qmPutNodeMsgToQueryQueue(SQnodeMgmt *pMgmt, SRpcMsg *pMsg) {
S
Shengliang 已提交
60
  return qmPutNodeMsgToWorker(&pMgmt->queryWorker, pMsg);
S
Shengliang Guan 已提交
61
}
S
shm  
Shengliang Guan 已提交
62

S
Shengliang Guan 已提交
63
int32_t qmPutNodeMsgToFetchQueue(SQnodeMgmt *pMgmt, SRpcMsg *pMsg) {
S
Shengliang 已提交
64
  return qmPutNodeMsgToWorker(&pMgmt->fetchWorker, pMsg);
S
Shengliang Guan 已提交
65
}
S
Shengliang Guan 已提交
66

S
Shengliang Guan 已提交
67
int32_t qmPutNodeMsgToMonitorQueue(SQnodeMgmt *pMgmt, SRpcMsg *pMsg) {
S
Shengliang 已提交
68
  return qmPutNodeMsgToWorker(&pMgmt->monitorWorker, pMsg);
69 70
}

S
Shengliang Guan 已提交
71
static int32_t qmPutRpcMsgToWorker(SQnodeMgmt *pMgmt, SSingleWorker *pWorker, SRpcMsg *pRpc) {
S
Shengliang Guan 已提交
72
  SRpcMsg *pMsg = taosAllocateQitem(sizeof(SRpcMsg), RPC_QITEM);
S
Shengliang Guan 已提交
73
  if (pMsg == NULL) return -1;
S
Shengliang Guan 已提交
74

S
Shengliang Guan 已提交
75
  dTrace("msg:%p, create and put into worker:%s, type:%s", pMsg, pWorker->name, TMSG_INFO(pRpc->msgType));
S
Shengliang Guan 已提交
76
  memcpy(pMsg, pRpc, sizeof(SRpcMsg));
S
Shengliang Guan 已提交
77 78
  taosWriteQitem(pWorker->queue, pMsg);
  return 0;
S
Shengliang Guan 已提交
79 80
}

S
Shengliang 已提交
81
int32_t qmPutRpcMsgToQueryQueue(SQnodeMgmt *pMgmt, SRpcMsg *pRpc) {
S
Shengliang Guan 已提交
82 83 84
  return qmPutRpcMsgToWorker(pMgmt, &pMgmt->queryWorker, pRpc);
}

S
Shengliang 已提交
85
int32_t qmPutRpcMsgToFetchQueue(SQnodeMgmt *pMgmt, SRpcMsg *pRpc) {
S
Shengliang Guan 已提交
86
  return qmPutRpcMsgToWorker(pMgmt, &pMgmt->fetchWorker, pRpc);
S
shm  
Shengliang Guan 已提交
87 88
}

S
Shengliang 已提交
89 90
int32_t qmGetQueueSize(SQnodeMgmt *pMgmt, int32_t vgId, EQueueType qtype) {
  int32_t size = -1;
91

S
Shengliang Guan 已提交
92 93
  switch (qtype) {
    case QUERY_QUEUE:
94
      size = taosQueueItemSize(pMgmt->queryWorker.queue);
S
Shengliang Guan 已提交
95 96
      break;
    case FETCH_QUEUE:
97
      size = taosQueueItemSize(pMgmt->fetchWorker.queue);
S
Shengliang Guan 已提交
98 99 100 101 102 103 104 105
      break;
    default:
      break;
  }

  return size;
}

S
shm  
Shengliang Guan 已提交
106
int32_t qmStartWorker(SQnodeMgmt *pMgmt) {
S
Shengliang Guan 已提交
107 108 109 110
  SSingleWorkerCfg queryCfg = {
      .min = tsNumOfVnodeQueryThreads,
      .max = tsNumOfVnodeQueryThreads,
      .name = "qnode-query",
S
Shengliang Guan 已提交
111
      .fp = (FItem)qmProcessQueue,
S
Shengliang Guan 已提交
112 113
      .param = pMgmt,
  };
S
Shengliang Guan 已提交
114

S
Shengliang Guan 已提交
115
  if (tSingleWorkerInit(&pMgmt->queryWorker, &queryCfg) != 0) {
S
Shengliang Guan 已提交
116
    dError("failed to start qnode-query worker since %s", terrstr());
S
shm  
Shengliang Guan 已提交
117 118 119
    return -1;
  }

S
Shengliang Guan 已提交
120 121 122 123
  SSingleWorkerCfg fetchCfg = {
      .min = tsNumOfQnodeFetchThreads,
      .max = tsNumOfQnodeFetchThreads,
      .name = "qnode-fetch",
S
Shengliang Guan 已提交
124
      .fp = (FItem)qmProcessQueue,
S
Shengliang Guan 已提交
125 126
      .param = pMgmt,
  };
S
Shengliang Guan 已提交
127

S
Shengliang Guan 已提交
128
  if (tSingleWorkerInit(&pMgmt->fetchWorker, &fetchCfg) != 0) {
S
Shengliang Guan 已提交
129
    dError("failed to start qnode-fetch worker since %s", terrstr());
S
shm  
Shengliang Guan 已提交
130 131 132
    return -1;
  }

S
Shengliang Guan 已提交
133 134 135 136
  SSingleWorkerCfg mCfg = {
      .min = 1,
      .max = 1,
      .name = "qnode-monitor",
S
Shengliang Guan 已提交
137
      .fp = (FItem)qmProcessQueue,
S
Shengliang Guan 已提交
138 139 140 141 142
      .param = pMgmt,
  };
  if (tSingleWorkerInit(&pMgmt->monitorWorker, &mCfg) != 0) {
    dError("failed to start qnode-monitor worker since %s", terrstr());
    return -1;
143 144
  }

S
Shengliang Guan 已提交
145
  dDebug("qnode workers are initialized");
S
shm  
Shengliang Guan 已提交
146 147 148 149
  return 0;
}

void qmStopWorker(SQnodeMgmt *pMgmt) {
150
  tSingleWorkerCleanup(&pMgmt->monitorWorker);
S
Shengliang Guan 已提交
151 152
  tSingleWorkerCleanup(&pMgmt->queryWorker);
  tSingleWorkerCleanup(&pMgmt->fetchWorker);
S
Shengliang Guan 已提交
153
  dDebug("qnode workers are closed");
S
shm  
Shengliang Guan 已提交
154
}