mmWorker.c 6.2 KB
Newer Older
S
shm  
Shengliang Guan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * 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
S
shm  
Shengliang Guan 已提交
17
#include "mmInt.h"
S
shm  
Shengliang Guan 已提交
18

19
static inline void mmSendRsp(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,
  };
28 29 30
  tmsgSendRsp(&rsp);
}

S
Shengliang Guan 已提交
31 32
static void mmProcessQueue(SQueueInfo *pInfo, SNodeMsg *pMsg) {
  SMnodeMgmt *pMgmt = pInfo->ahandle;
S
Shengliang 已提交
33 34 35
  int32_t     code = -1;
  tmsg_t      msgType = pMsg->rpcMsg.msgType;
  bool        isRequest = msgType & 1U;
36
  dTrace("msg:%p, get from mnode queue, type:%s", pMsg, TMSG_INFO(msgType));
S
shm  
Shengliang Guan 已提交
37

S
Shengliang Guan 已提交
38 39 40 41 42
  switch (msgType) {
    case TDMT_DND_ALTER_MNODE:
      code = mmProcessAlterReq(pMgmt, pMsg);
      break;
    case TDMT_MON_MM_INFO:
S
Shengliang 已提交
43
      code = mmProcessGetMonitorInfoReq(pMgmt, pMsg);
S
Shengliang Guan 已提交
44 45
      break;
    case TDMT_MON_MM_LOAD:
S
Shengliang 已提交
46
      code = mmProcessGetLoadsReq(pMgmt, pMsg);
S
Shengliang Guan 已提交
47 48 49 50
      break;
    default:
      pMsg->pNode = pMgmt->pMnode;
      code = mndProcessMsg(pMsg);
S
shm  
Shengliang Guan 已提交
51 52
  }

S
Shengliang 已提交
53
  if (isRequest) {
S
Shengliang Guan 已提交
54
    if (pMsg->rpcMsg.handle != NULL && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) {
55
      if (code != 0 && terrno != 0) code = terrno;
56
      mmSendRsp(pMsg, code);
S
shm  
Shengliang Guan 已提交
57 58 59
    }
  }

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

D
dapan1121 已提交
65 66
static void mmProcessQueryQueue(SQueueInfo *pInfo, SNodeMsg *pMsg) {
  SMnodeMgmt *pMgmt = pInfo->ahandle;
S
Shengliang 已提交
67 68 69
  int32_t     code = -1;
  tmsg_t      msgType = pMsg->rpcMsg.msgType;
  bool        isRequest = msgType & 1U;
70
  dTrace("msg:%p, get from mnode-query queue", pMsg);
D
dapan1121 已提交
71 72 73 74

  pMsg->pNode = pMgmt->pMnode;
  code = mndProcessMsg(pMsg);

S
Shengliang 已提交
75 76 77
  if (isRequest) {
    if (pMsg->rpcMsg.handle != NULL && code != 0) {
      if (code != 0 && terrno != 0) code = terrno;
78
      mmSendRsp(pMsg, code);
D
dapan1121 已提交
79 80 81 82
    }
  }

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

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

S
Shengliang 已提交
93
int32_t mmPutNodeMsgToWriteQueue(SMnodeMgmt *pMgmt, SNodeMsg *pMsg) { return mmPutNodeMsgToWorker(&pMgmt->writeWorker, pMsg); }
S
shm  
Shengliang Guan 已提交
94

S
Shengliang 已提交
95
int32_t mmPutNodeMsgToSyncQueue(SMnodeMgmt *pMgmt, SNodeMsg *pMsg) { return mmPutNodeMsgToWorker(&pMgmt->syncWorker, pMsg); }
S
shm  
Shengliang Guan 已提交
96

S
Shengliang 已提交
97 98 99
int32_t mmPutNodeMsgToReadQueue(SMnodeMgmt *pMgmt, SNodeMsg *pMsg) { return mmPutNodeMsgToWorker(&pMgmt->readWorker, pMsg); }

int32_t mmPutNodeMsgToQueryQueue(SMnodeMgmt *pMgmt, SNodeMsg *pMsg) { return mmPutNodeMsgToWorker(&pMgmt->queryWorker, pMsg);
D
dapan1121 已提交
100 101
}

S
Shengliang 已提交
102 103
int32_t mmPutNodeMsgToMonitorQueue(SMnodeMgmt *pMgmt, SNodeMsg *pMsg) {
  return mmPutNodeMsgToWorker(&pMgmt->monitorWorker, pMsg);
104 105
}

S
Shengliang 已提交
106
static inline int32_t mmPutRpcMsgToWorker(SSingleWorker *pWorker, SRpcMsg *pRpc) {
107
  SNodeMsg *pMsg = taosAllocateQitem(sizeof(SNodeMsg), RPC_QITEM);
S
Shengliang Guan 已提交
108
  if (pMsg == NULL) return -1;
S
shm  
Shengliang Guan 已提交
109

S
Shengliang Guan 已提交
110
  dTrace("msg:%p, is created and put into worker:%s, type:%s", pMsg, pWorker->name, TMSG_INFO(pRpc->msgType));
S
shm  
Shengliang Guan 已提交
111
  pMsg->rpcMsg = *pRpc;
S
Shengliang Guan 已提交
112 113 114
  taosWriteQitem(pWorker->queue, pMsg);
  return 0;
}
S
shm  
Shengliang Guan 已提交
115

S
Shengliang 已提交
116
int32_t mmPutRpcMsgToQueryQueue(SMnodeMgmt *pMgmt, SRpcMsg *pRpc) {
S
Shengliang Guan 已提交
117
  return mmPutRpcMsgToWorker(&pMgmt->queryWorker, pRpc);
S
shm  
Shengliang Guan 已提交
118 119
}

S
Shengliang 已提交
120
int32_t mmPutRpcMsgToWriteQueue(SMnodeMgmt *pMgmt, SRpcMsg *pRpc) {
S
Shengliang Guan 已提交
121
  return mmPutRpcMsgToWorker(&pMgmt->writeWorker, pRpc);
S
shm  
Shengliang Guan 已提交
122
}
S
shm  
Shengliang Guan 已提交
123

S
Shengliang 已提交
124
int32_t mmPutRpcMsgToReadQueue(SMnodeMgmt *pMgmt, SRpcMsg *pRpc) {
S
Shengliang Guan 已提交
125
  return mmPutRpcMsgToWorker(&pMgmt->readWorker, pRpc);
S
shm  
Shengliang Guan 已提交
126
}
S
Shengliang Guan 已提交
127

S
Shengliang 已提交
128
int32_t mmPutMsgToSyncQueue(SMnodeMgmt *pMgmt, SRpcMsg *pRpc) { return mmPutRpcMsgToWorker(&pMgmt->syncWorker, pRpc); }
D
dapan1121 已提交
129

S
Shengliang Guan 已提交
130
int32_t mmStartWorker(SMnodeMgmt *pMgmt) {
S
Shengliang Guan 已提交
131 132 133 134 135 136 137
  SSingleWorkerCfg qCfg = {
      .min = tsNumOfMnodeQueryThreads,
      .max = tsNumOfMnodeQueryThreads,
      .name = "mnode-query",
      .fp = (FItem)mmProcessQueryQueue,
      .param = pMgmt,
  };
S
shm  
Shengliang Guan 已提交
138
  if (tSingleWorkerInit(&pMgmt->queryWorker, &qCfg) != 0) {
D
dapan1121 已提交
139 140 141 142
    dError("failed to start mnode-query worker since %s", terrstr());
    return -1;
  }

S
Shengliang Guan 已提交
143 144 145 146 147 148 149
  SSingleWorkerCfg rCfg = {
      .min = tsNumOfMnodeReadThreads,
      .max = tsNumOfMnodeReadThreads,
      .name = "mnode-read",
      .fp = (FItem)mmProcessQueue,
      .param = pMgmt,
  };
S
shm  
Shengliang Guan 已提交
150
  if (tSingleWorkerInit(&pMgmt->readWorker, &rCfg) != 0) {
S
Shengliang Guan 已提交
151
    dError("failed to start mnode-read worker since %s", terrstr());
S
Shengliang Guan 已提交
152 153 154
    return -1;
  }

S
Shengliang Guan 已提交
155 156 157 158 159 160 161
  SSingleWorkerCfg wCfg = {
      .min = 1,
      .max = 1,
      .name = "mnode-write",
      .fp = (FItem)mmProcessQueue,
      .param = pMgmt,
  };
S
shm  
Shengliang Guan 已提交
162
  if (tSingleWorkerInit(&pMgmt->writeWorker, &wCfg) != 0) {
S
Shengliang Guan 已提交
163
    dError("failed to start mnode-write worker since %s", terrstr());
S
Shengliang Guan 已提交
164 165 166
    return -1;
  }

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

S
Shengliang Guan 已提交
179 180 181 182 183 184 185 186 187 188
  SSingleWorkerCfg mCfg = {
      .min = 1,
      .max = 1,
      .name = "mnode-monitor",
      .fp = (FItem)mmProcessQueue,
      .param = pMgmt,
  };
  if (tSingleWorkerInit(&pMgmt->monitorWorker, &mCfg) != 0) {
    dError("failed to start mnode mnode-monitor worker since %s", terrstr());
    return -1;
189 190
  }

S
Shengliang Guan 已提交
191
  dDebug("mnode workers are initialized");
S
Shengliang Guan 已提交
192 193 194 195
  return 0;
}

void mmStopWorker(SMnodeMgmt *pMgmt) {
196
  tSingleWorkerCleanup(&pMgmt->monitorWorker);
D
dapan1121 已提交
197
  tSingleWorkerCleanup(&pMgmt->queryWorker);
S
Shengliang Guan 已提交
198
  tSingleWorkerCleanup(&pMgmt->readWorker);
S
Shengliang Guan 已提交
199 200
  tSingleWorkerCleanup(&pMgmt->writeWorker);
  tSingleWorkerCleanup(&pMgmt->syncWorker);
S
Shengliang Guan 已提交
201
  dDebug("mnode workers are closed");
S
Shengliang Guan 已提交
202
}