qmWorker.c 3.9 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
  dTrace("msg:%p, get from qnode queue", pMsg);
32

33
  int32_t code = qndProcessQueryMsg(pMgmt->pQnode, pInfo->timestamp, pMsg);
S
Shengliang Guan 已提交
34
  if (IsReq(pMsg) && code != TSDB_CODE_ACTION_IN_PROGRESS) {
35
    if (code != 0 && terrno != 0) code = terrno;
36
    qmSendRsp(pMsg, code);
37 38
  }

S
Shengliang Guan 已提交
39
  dTrace("msg:%p, is freed, code:0x%x", pMsg, code);
S
Shengliang Guan 已提交
40
  rpcFreeCont(pMsg->pCont);
S
Shengliang Guan 已提交
41 42
  taosFreeQitem(pMsg);
}
S
shm  
Shengliang Guan 已提交
43

S
Shengliang Guan 已提交
44
static int32_t qmPutNodeMsgToWorker(SSingleWorker *pWorker, SRpcMsg *pMsg) {
S
Shengliang Guan 已提交
45
  dTrace("msg:%p, put into worker %s, type:%s", pMsg, pWorker->name, TMSG_INFO(pMsg->msgType));
S
Shengliang Guan 已提交
46
  taosWriteQitem(pWorker->queue, pMsg);
S
Shengliang 已提交
47
  return 0;
S
shm  
Shengliang Guan 已提交
48 49
}

S
Shengliang Guan 已提交
50
int32_t qmPutNodeMsgToQueryQueue(SQnodeMgmt *pMgmt, SRpcMsg *pMsg) {
D
dapan1121 已提交
51 52
  qndPreprocessQueryMsg(pMgmt->pQnode, pMsg);

S
Shengliang 已提交
53
  return qmPutNodeMsgToWorker(&pMgmt->queryWorker, pMsg);
S
Shengliang Guan 已提交
54
}
S
shm  
Shengliang Guan 已提交
55

S
Shengliang Guan 已提交
56
int32_t qmPutNodeMsgToFetchQueue(SQnodeMgmt *pMgmt, SRpcMsg *pMsg) {
S
Shengliang 已提交
57
  return qmPutNodeMsgToWorker(&pMgmt->fetchWorker, pMsg);
S
Shengliang Guan 已提交
58
}
S
Shengliang Guan 已提交
59

S
Shengliang Guan 已提交
60
int32_t qmPutRpcMsgToQueue(SQnodeMgmt *pMgmt, EQueueType qtype, SRpcMsg *pRpc) {
S
Shengliang Guan 已提交
61
  SRpcMsg *pMsg = taosAllocateQitem(sizeof(SRpcMsg), RPC_QITEM, pRpc->contLen);
S
Shengliang Guan 已提交
62
  if (pMsg == NULL) return -1;
S
Shengliang Guan 已提交
63
  memcpy(pMsg, pRpc, sizeof(SRpcMsg));
64
  pRpc->pCont = NULL;
S
Shengliang Guan 已提交
65

S
Shengliang Guan 已提交
66 67 68 69 70 71
  switch (qtype) {
    case QUERY_QUEUE:
      dTrace("msg:%p, is created and will put into qnode-query queue", pMsg);
      taosWriteQitem(pMgmt->queryWorker.queue, pMsg);
      return 0;
    case READ_QUEUE:
D
dapan1121 已提交
72
    case FETCH_QUEUE:
S
Shengliang Guan 已提交
73 74 75 76 77
      dTrace("msg:%p, is created and will put into qnode-fetch queue", pMsg);
      taosWriteQitem(pMgmt->fetchWorker.queue, pMsg);
      return 0;
    default:
      terrno = TSDB_CODE_INVALID_PARA;
78
      rpcFreeCont(pMsg->pCont);
S
Shengliang Guan 已提交
79
      taosFreeQitem(pMsg);
S
Shengliang Guan 已提交
80 81
      return -1;
  }
S
shm  
Shengliang Guan 已提交
82 83
}

S
Shengliang 已提交
84 85
int32_t qmGetQueueSize(SQnodeMgmt *pMgmt, int32_t vgId, EQueueType qtype) {
  int32_t size = -1;
86

S
Shengliang Guan 已提交
87 88
  switch (qtype) {
    case QUERY_QUEUE:
89
      size = taosQueueItemSize(pMgmt->queryWorker.queue);
S
Shengliang Guan 已提交
90 91
      break;
    case FETCH_QUEUE:
92
      size = taosQueueItemSize(pMgmt->fetchWorker.queue);
S
Shengliang Guan 已提交
93 94 95 96 97 98 99 100
      break;
    default:
      break;
  }

  return size;
}

S
shm  
Shengliang Guan 已提交
101
int32_t qmStartWorker(SQnodeMgmt *pMgmt) {
S
Shengliang Guan 已提交
102 103 104 105
  SSingleWorkerCfg queryCfg = {
      .min = tsNumOfVnodeQueryThreads,
      .max = tsNumOfVnodeQueryThreads,
      .name = "qnode-query",
S
Shengliang Guan 已提交
106
      .fp = (FItem)qmProcessQueue,
S
Shengliang Guan 已提交
107 108
      .param = pMgmt,
  };
S
Shengliang Guan 已提交
109

S
Shengliang Guan 已提交
110
  if (tSingleWorkerInit(&pMgmt->queryWorker, &queryCfg) != 0) {
S
Shengliang Guan 已提交
111
    dError("failed to start qnode-query worker since %s", terrstr());
S
shm  
Shengliang Guan 已提交
112 113 114
    return -1;
  }

S
Shengliang Guan 已提交
115 116 117 118
  SSingleWorkerCfg fetchCfg = {
      .min = tsNumOfQnodeFetchThreads,
      .max = tsNumOfQnodeFetchThreads,
      .name = "qnode-fetch",
S
Shengliang Guan 已提交
119
      .fp = (FItem)qmProcessQueue,
S
Shengliang Guan 已提交
120 121
      .param = pMgmt,
  };
S
Shengliang Guan 已提交
122

S
Shengliang Guan 已提交
123
  if (tSingleWorkerInit(&pMgmt->fetchWorker, &fetchCfg) != 0) {
S
Shengliang Guan 已提交
124
    dError("failed to start qnode-fetch worker since %s", terrstr());
S
shm  
Shengliang Guan 已提交
125 126 127
    return -1;
  }

S
Shengliang Guan 已提交
128
  dDebug("qnode workers are initialized");
S
shm  
Shengliang Guan 已提交
129 130 131 132
  return 0;
}

void qmStopWorker(SQnodeMgmt *pMgmt) {
S
Shengliang Guan 已提交
133 134
  tSingleWorkerCleanup(&pMgmt->queryWorker);
  tSingleWorkerCleanup(&pMgmt->fetchWorker);
S
Shengliang Guan 已提交
135
  dDebug("qnode workers are closed");
S
shm  
Shengliang Guan 已提交
136
}