qmWorker.c 4.7 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 20
static void qmSendRsp(SMgmtWrapper *pWrapper, SNodeMsg *pMsg, int32_t code) {
  SRpcMsg rsp = {.handle = pMsg->rpcMsg.handle, .ahandle = pMsg->rpcMsg.ahandle, .code = code};
S
Shengliang Guan 已提交
21
  tmsgSendRsp(&rsp);
S
Shengliang Guan 已提交
22 23
}

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

S
Shengliang Guan 已提交
27 28 29 30
  dTrace("msg:%p, will be processed in qnode-query queue", pMsg);
  int32_t code = qndProcessQueryMsg(pMgmt->pQnode, &pMsg->rpcMsg);
  if (code != 0) {
    qmSendRsp(pMgmt->pWrapper, pMsg, code);
S
shm  
Shengliang Guan 已提交
31 32
  }

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

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

S
Shengliang Guan 已提交
41 42 43 44 45 46 47 48 49 50
  dTrace("msg:%p, will be processed in qnode-fetch queue", pMsg);
  int32_t code = qndProcessFetchMsg(pMgmt->pQnode, &pMsg->rpcMsg);
  if (code != 0) {
    qmSendRsp(pMgmt->pWrapper, pMsg, code);
  }

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

S
Shengliang Guan 已提交
52
static void qmPutMsgToWorker(SSingleWorker *pWorker, SNodeMsg *pMsg) {
S
Shengliang Guan 已提交
53
  dTrace("msg:%p, put into worker %s", pMsg, pWorker->name);
S
Shengliang Guan 已提交
54
  taosWriteQitem(pWorker->queue, pMsg);
S
shm  
Shengliang Guan 已提交
55 56
}

S
Shengliang Guan 已提交
57 58 59 60 61
int32_t qmProcessQueryMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) {
  SQnodeMgmt *pMgmt = pWrapper->pMgmt;
  qmPutMsgToWorker(&pMgmt->queryWorker, pMsg);
  return 0;
}
S
shm  
Shengliang Guan 已提交
62

S
Shengliang Guan 已提交
63 64 65 66 67
int32_t qmProcessFetchMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) {
  SQnodeMgmt *pMgmt = pWrapper->pMgmt;
  qmPutMsgToWorker(&pMgmt->fetchWorker, pMsg);
  return 0;
}
S
Shengliang Guan 已提交
68

S
Shengliang Guan 已提交
69
static int32_t qmPutRpcMsgToWorker(SQnodeMgmt *pMgmt, SSingleWorker *pWorker, SRpcMsg *pRpc) {
S
Shengliang Guan 已提交
70 71 72 73 74 75 76
  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 已提交
77 78
  taosWriteQitem(pWorker->queue, pMsg);
  return 0;
S
Shengliang Guan 已提交
79 80 81 82 83 84 85 86 87 88
}

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 已提交
89 90
}

S
Shengliang Guan 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
int32_t qmGetQueueSize(SMgmtWrapper *pWrapper, int32_t vgId, EQueueType qtype) {
  int32_t     size = -1;
  SQnodeMgmt *pMgmt = pWrapper->pMgmt;
  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 已提交
108
int32_t qmStartWorker(SQnodeMgmt *pMgmt) {
S
Shengliang Guan 已提交
109 110 111 112 113
  int32_t maxFetchThreads = 4;
  int32_t minFetchThreads = TMIN(maxFetchThreads, tsNumOfCores);
  int32_t minQueryThreads = TMAX((int32_t)(tsNumOfCores * tsRatioOfQueryCores), 1);
  int32_t maxQueryThreads = minQueryThreads;

S
shm  
Shengliang Guan 已提交
114 115
  SSingleWorkerCfg queryCfg = {.min = minQueryThreads,
                               .max = maxQueryThreads,
S
Shengliang Guan 已提交
116 117 118
                               .name = "qnode-query",
                               .fp = (FItem)qmProcessQueryQueue,
                               .param = pMgmt};
S
Shengliang Guan 已提交
119

S
Shengliang Guan 已提交
120
  if (tSingleWorkerInit(&pMgmt->queryWorker, &queryCfg) != 0) {
S
Shengliang Guan 已提交
121
    dError("failed to start qnode-query worker since %s", terrstr());
S
shm  
Shengliang Guan 已提交
122 123 124
    return -1;
  }

S
shm  
Shengliang Guan 已提交
125 126
  SSingleWorkerCfg fetchCfg = {.min = minFetchThreads,
                               .max = maxFetchThreads,
S
Shengliang Guan 已提交
127 128 129
                               .name = "qnode-fetch",
                               .fp = (FItem)qmProcessFetchQueue,
                               .param = pMgmt};
S
Shengliang Guan 已提交
130

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

S
Shengliang Guan 已提交
136
  dDebug("qnode workers are initialized");
S
shm  
Shengliang Guan 已提交
137 138 139 140
  return 0;
}

void qmStopWorker(SQnodeMgmt *pMgmt) {
S
Shengliang Guan 已提交
141 142
  tSingleWorkerCleanup(&pMgmt->queryWorker);
  tSingleWorkerCleanup(&pMgmt->fetchWorker);
S
Shengliang Guan 已提交
143
  dDebug("qnode workers are closed");
S
shm  
Shengliang Guan 已提交
144
}