mmWorker.c 6.0 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

S
Shengliang Guan 已提交
19
static inline void mmSendRsp(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,
24
      .info = pMsg->info,
S
Shengliang Guan 已提交
25
  };
26 27 28
  tmsgSendRsp(&rsp);
}

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

S
Shengliang Guan 已提交
34
  switch (pMsg->msgType) {
S
Shengliang Guan 已提交
35 36 37 38
    case TDMT_DND_ALTER_MNODE:
      code = mmProcessAlterReq(pMgmt, pMsg);
      break;
    case TDMT_MON_MM_INFO:
S
Shengliang 已提交
39
      code = mmProcessGetMonitorInfoReq(pMgmt, pMsg);
S
Shengliang Guan 已提交
40 41
      break;
    case TDMT_MON_MM_LOAD:
S
Shengliang 已提交
42
      code = mmProcessGetLoadsReq(pMgmt, pMsg);
S
Shengliang Guan 已提交
43 44
      break;
    default:
S
Shengliang Guan 已提交
45
      pMsg->info.node = pMgmt->pMnode;
S
Shengliang Guan 已提交
46
      code = mndProcessMsg(pMsg);
S
shm  
Shengliang Guan 已提交
47 48
  }

S
Shengliang Guan 已提交
49 50 51
  if (IsReq(pMsg) && pMsg->info.handle != NULL && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) {
    if (code != 0 && terrno != 0) code = terrno;
    mmSendRsp(pMsg, code);
S
shm  
Shengliang Guan 已提交
52 53
  }

S
Shengliang Guan 已提交
54
  dTrace("msg:%p, is freed, code:0x%x", pMsg, code);
S
Shengliang Guan 已提交
55
  rpcFreeCont(pMsg->pCont);
S
shm  
Shengliang Guan 已提交
56 57 58
  taosFreeQitem(pMsg);
}

S
Shengliang Guan 已提交
59
static void mmProcessQueryQueue(SQueueInfo *pInfo, SRpcMsg *pMsg) {
D
dapan1121 已提交
60
  SMnodeMgmt *pMgmt = pInfo->ahandle;
S
Shengliang 已提交
61
  int32_t     code = -1;
S
Shengliang Guan 已提交
62
  tmsg_t      msgType = pMsg->msgType;
S
Shengliang 已提交
63
  bool        isRequest = msgType & 1U;
64
  dTrace("msg:%p, get from mnode-query queue", pMsg);
D
dapan1121 已提交
65

S
Shengliang Guan 已提交
66
  pMsg->info.node = pMgmt->pMnode;
D
dapan1121 已提交
67 68
  code = mndProcessMsg(pMsg);

S
Shengliang 已提交
69
  if (isRequest) {
S
Shengliang Guan 已提交
70
    if (pMsg->info.handle != NULL && code != 0) {
S
Shengliang 已提交
71
      if (code != 0 && terrno != 0) code = terrno;
72
      mmSendRsp(pMsg, code);
D
dapan1121 已提交
73 74 75
    }
  }

S
Shengliang Guan 已提交
76
  dTrace("msg:%p, is freed, code:0x%x", pMsg, code);
S
Shengliang Guan 已提交
77
  rpcFreeCont(pMsg->pCont);
D
dapan1121 已提交
78 79 80
  taosFreeQitem(pMsg);
}

S
Shengliang Guan 已提交
81
static int32_t mmPutNodeMsgToWorker(SSingleWorker *pWorker, SRpcMsg *pMsg) {
S
Shengliang Guan 已提交
82
  dTrace("msg:%p, put into worker %s, type:%s", pMsg, pWorker->name, TMSG_INFO(pMsg->msgType));
S
Shengliang Guan 已提交
83 84
  taosWriteQitem(pWorker->queue, pMsg);
  return 0;
S
shm  
Shengliang Guan 已提交
85 86
}

S
Shengliang Guan 已提交
87 88 89
int32_t mmPutNodeMsgToWriteQueue(SMnodeMgmt *pMgmt, SRpcMsg *pMsg) {
  return mmPutNodeMsgToWorker(&pMgmt->writeWorker, pMsg);
}
S
shm  
Shengliang Guan 已提交
90

S
Shengliang Guan 已提交
91 92 93
int32_t mmPutNodeMsgToSyncQueue(SMnodeMgmt *pMgmt, SRpcMsg *pMsg) {
  return mmPutNodeMsgToWorker(&pMgmt->syncWorker, pMsg);
}
S
shm  
Shengliang Guan 已提交
94

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

S
Shengliang Guan 已提交
99 100
int32_t mmPutNodeMsgToQueryQueue(SMnodeMgmt *pMgmt, SRpcMsg *pMsg) {
  return mmPutNodeMsgToWorker(&pMgmt->queryWorker, pMsg);
D
dapan1121 已提交
101 102
}

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

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

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

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

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

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

dengyihao's avatar
dengyihao 已提交
129 130 131
int32_t mmPutRpcMsgToSyncQueue(SMnodeMgmt *pMgmt, SRpcMsg *pMsg) {
  return mmPutRpcMsgToWorker(&pMgmt->syncWorker, pMsg);
}
D
dapan1121 已提交
132

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

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

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

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

S
Shengliang Guan 已提交
182 183 184 185 186 187 188 189 190 191
  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;
192 193
  }

S
Shengliang Guan 已提交
194
  dDebug("mnode workers are initialized");
S
Shengliang Guan 已提交
195 196 197 198
  return 0;
}

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