mmWorker.c 6.6 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 20 21
static inline void mmSendRsp(SNodeMsg *pMsg, int32_t code) {
  SRpcMsg rsp = {.handle = pMsg->rpcMsg.handle,
                 .ahandle = pMsg->rpcMsg.ahandle,
dengyihao's avatar
dengyihao 已提交
22
                 .refId = pMsg->rpcMsg.refId,
23 24 25 26 27 28
                 .code = code,
                 .pCont = pMsg->pRsp,
                 .contLen = pMsg->rspLen};
  tmsgSendRsp(&rsp);
}

S
Shengliang Guan 已提交
29 30 31
static void mmProcessQueue(SQueueInfo *pInfo, SNodeMsg *pMsg) {
  SMnodeMgmt *pMgmt = pInfo->ahandle;

S
Shengliang Guan 已提交
32
  dTrace("msg:%p, get from mnode queue", pMsg);
S
shm  
Shengliang Guan 已提交
33 34 35
  SRpcMsg *pRpc = &pMsg->rpcMsg;
  int32_t  code = -1;

36 37 38 39
  if (pMsg->rpcMsg.msgType == TDMT_DND_ALTER_MNODE) {
    code = mmProcessAlterReq(pMgmt, pMsg);
  } else if (pMsg->rpcMsg.msgType == TDMT_MON_MM_INFO) {
    code = mmProcessGetMonMmInfoReq(pMgmt->pWrapper, pMsg);
S
Shengliang Guan 已提交
40 41
  } else if (pMsg->rpcMsg.msgType == TDMT_MON_MM_LOAD) {
    code = mmProcessGetMnodeLoadsReq(pMgmt->pWrapper, pMsg);
42
  } else {
S
shm  
Shengliang Guan 已提交
43
    pMsg->pNode = pMgmt->pMnode;
S
shm  
Shengliang Guan 已提交
44
    code = mndProcessMsg(pMsg);
S
shm  
Shengliang Guan 已提交
45 46
  }

S
shm  
Shengliang Guan 已提交
47
  if (pRpc->msgType & 1U) {
S
Shengliang Guan 已提交
48
    if (pRpc->handle != NULL && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) {
49
      if (code != 0 && terrno != 0) code = terrno;
50
      mmSendRsp(pMsg, code);
S
shm  
Shengliang Guan 已提交
51 52 53
    }
  }

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

D
dapan1121 已提交
59 60 61
static void mmProcessQueryQueue(SQueueInfo *pInfo, SNodeMsg *pMsg) {
  SMnodeMgmt *pMgmt = pInfo->ahandle;

62
  dTrace("msg:%p, get from mnode-query queue", pMsg);
D
dapan1121 已提交
63 64 65 66 67 68 69
  SRpcMsg *pRpc = &pMsg->rpcMsg;
  int32_t  code = -1;

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

  if (pRpc->msgType & 1U) {
S
Shengliang Guan 已提交
70 71
    if (pRpc->handle != NULL && code != 0) {
      dError("msg:%p, failed to process since %s", pMsg, terrstr());
72
      mmSendRsp(pMsg, code);
D
dapan1121 已提交
73 74 75 76 77 78 79 80
    }
  }

  dTrace("msg:%p, is freed, result:0x%04x:%s", pMsg, code & 0XFFFF, tstrerror(code));
  rpcFreeCont(pRpc->pCont);
  taosFreeQitem(pMsg);
}

S
Shengliang Guan 已提交
81
static void mmPutMsgToWorker(SSingleWorker *pWorker, SNodeMsg *pMsg) {
S
shm  
Shengliang Guan 已提交
82
  dTrace("msg:%p, put into worker %s", pMsg, pWorker->name);
S
Shengliang Guan 已提交
83
  taosWriteQitem(pWorker->queue, pMsg);
S
shm  
Shengliang Guan 已提交
84 85
}

S
Shengliang Guan 已提交
86 87 88 89
int32_t mmProcessWriteMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) {
  SMnodeMgmt *pMgmt = pWrapper->pMgmt;
  mmPutMsgToWorker(&pMgmt->writeWorker, pMsg);
  return 0;
S
shm  
Shengliang Guan 已提交
90 91
}

S
Shengliang Guan 已提交
92 93 94 95
int32_t mmProcessSyncMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) {
  SMnodeMgmt *pMgmt = pWrapper->pMgmt;
  mmPutMsgToWorker(&pMgmt->syncWorker, pMsg);
  return 0;
S
shm  
Shengliang Guan 已提交
96 97
}

S
Shengliang Guan 已提交
98 99 100 101
int32_t mmProcessReadMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) {
  SMnodeMgmt *pMgmt = pWrapper->pMgmt;
  mmPutMsgToWorker(&pMgmt->readWorker, pMsg);
  return 0;
S
shm  
Shengliang Guan 已提交
102
}
S
shm  
Shengliang Guan 已提交
103

S
Shengliang Guan 已提交
104 105 106 107
int32_t mmProcessQueryMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) {
  SMnodeMgmt *pMgmt = pWrapper->pMgmt;
  mmPutMsgToWorker(&pMgmt->queryWorker, pMsg);
  return 0;
D
dapan1121 已提交
108 109
}

110
int32_t mmProcessMonitorMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) {
111 112
  SMnodeMgmt *pMgmt = pWrapper->pMgmt;
  mmPutMsgToWorker(&pMgmt->monitorWorker, pMsg);
113 114 115
  return 0;
}

S
Shengliang Guan 已提交
116
static int32_t mmPutRpcMsgToWorker(SSingleWorker *pWorker, SRpcMsg *pRpc) {
S
shm  
Shengliang Guan 已提交
117
  SNodeMsg *pMsg = taosAllocateQitem(sizeof(SNodeMsg));
S
Shengliang Guan 已提交
118
  if (pMsg == NULL) return -1;
S
shm  
Shengliang Guan 已提交
119

S
Shengliang Guan 已提交
120
  dTrace("msg:%p, is created and put into worker:%s, type:%s", pMsg, pWorker->name, TMSG_INFO(pRpc->msgType));
S
shm  
Shengliang Guan 已提交
121
  pMsg->rpcMsg = *pRpc;
S
Shengliang Guan 已提交
122 123 124
  taosWriteQitem(pWorker->queue, pMsg);
  return 0;
}
S
shm  
Shengliang Guan 已提交
125

S
Shengliang Guan 已提交
126 127 128
int32_t mmPutMsgToQueryQueue(SMgmtWrapper *pWrapper, SRpcMsg *pRpc) {
  SMnodeMgmt *pMgmt = pWrapper->pMgmt;
  return mmPutRpcMsgToWorker(&pMgmt->queryWorker, pRpc);
S
shm  
Shengliang Guan 已提交
129 130
}

S
shm  
Shengliang Guan 已提交
131 132
int32_t mmPutMsgToWriteQueue(SMgmtWrapper *pWrapper, SRpcMsg *pRpc) {
  SMnodeMgmt *pMgmt = pWrapper->pMgmt;
S
Shengliang Guan 已提交
133
  return mmPutRpcMsgToWorker(&pMgmt->writeWorker, pRpc);
S
shm  
Shengliang Guan 已提交
134
}
S
shm  
Shengliang Guan 已提交
135

S
shm  
Shengliang Guan 已提交
136 137
int32_t mmPutMsgToReadQueue(SMgmtWrapper *pWrapper, SRpcMsg *pRpc) {
  SMnodeMgmt *pMgmt = pWrapper->pMgmt;
S
Shengliang Guan 已提交
138
  return mmPutRpcMsgToWorker(&pMgmt->readWorker, pRpc);
S
shm  
Shengliang Guan 已提交
139
}
S
Shengliang Guan 已提交
140

S
Shengliang Guan 已提交
141
int32_t mmPutMsgToSyncQueue(SMgmtWrapper *pWrapper, SRpcMsg *pRpc) {
D
dapan1121 已提交
142
  SMnodeMgmt *pMgmt = pWrapper->pMgmt;
S
Shengliang Guan 已提交
143
  return mmPutRpcMsgToWorker(&pMgmt->syncWorker, pRpc);
D
dapan1121 已提交
144 145
}

S
Shengliang Guan 已提交
146
int32_t mmStartWorker(SMnodeMgmt *pMgmt) {
S
Shengliang Guan 已提交
147 148 149 150 151
  SSingleWorkerCfg qCfg = {.min = tsNumOfMnodeQueryThreads,
                           .max = tsNumOfMnodeQueryThreads,
                           .name = "mnode-query",
                           .fp = (FItem)mmProcessQueryQueue,
                           .param = pMgmt};
S
shm  
Shengliang Guan 已提交
152
  if (tSingleWorkerInit(&pMgmt->queryWorker, &qCfg) != 0) {
D
dapan1121 已提交
153 154 155 156
    dError("failed to start mnode-query worker since %s", terrstr());
    return -1;
  }

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

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

S
Shengliang Guan 已提交
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;
  }

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

S
Shengliang Guan 已提交
187
  dDebug("mnode workers are initialized");
S
Shengliang Guan 已提交
188 189 190 191
  return 0;
}

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