mmWorker.c 6.3 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
  if (IsReq(pMsg) && pMsg->info.handle != NULL && code != TSDB_CODE_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
50 51
    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);
}

M
Minghao Li 已提交
59 60 61 62 63
static void mmProcessSyncQueue(SQueueInfo *pInfo, SRpcMsg *pMsg) {
  SMnodeMgmt *pMgmt = pInfo->ahandle;
  pMsg->info.node = pMgmt->pMnode;
  mndProcessSyncMsg(pMsg);
}
M
Minghao Li 已提交
64

S
Shengliang Guan 已提交
65
static int32_t mmPutNodeMsgToWorker(SSingleWorker *pWorker, SRpcMsg *pMsg) {
S
Shengliang Guan 已提交
66
  dTrace("msg:%p, put into worker %s, type:%s", pMsg, pWorker->name, TMSG_INFO(pMsg->msgType));
S
Shengliang Guan 已提交
67 68
  taosWriteQitem(pWorker->queue, pMsg);
  return 0;
S
shm  
Shengliang Guan 已提交
69 70
}

S
Shengliang Guan 已提交
71 72 73
int32_t mmPutNodeMsgToWriteQueue(SMnodeMgmt *pMgmt, SRpcMsg *pMsg) {
  return mmPutNodeMsgToWorker(&pMgmt->writeWorker, pMsg);
}
S
shm  
Shengliang Guan 已提交
74

S
Shengliang Guan 已提交
75 76 77
int32_t mmPutNodeMsgToSyncQueue(SMnodeMgmt *pMgmt, SRpcMsg *pMsg) {
  return mmPutNodeMsgToWorker(&pMgmt->syncWorker, pMsg);
}
S
shm  
Shengliang Guan 已提交
78

S
Shengliang Guan 已提交
79 80 81
int32_t mmPutNodeMsgToReadQueue(SMnodeMgmt *pMgmt, SRpcMsg *pMsg) {
  return mmPutNodeMsgToWorker(&pMgmt->readWorker, pMsg);
}
S
Shengliang 已提交
82

S
Shengliang Guan 已提交
83 84
int32_t mmPutNodeMsgToQueryQueue(SMnodeMgmt *pMgmt, SRpcMsg *pMsg) {
  return mmPutNodeMsgToWorker(&pMgmt->queryWorker, pMsg);
D
dapan1121 已提交
85 86
}

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

S
Shengliang 已提交
91
static inline int32_t mmPutRpcMsgToWorker(SSingleWorker *pWorker, SRpcMsg *pRpc) {
S
Shengliang Guan 已提交
92
  SRpcMsg *pMsg = taosAllocateQitem(sizeof(SRpcMsg), RPC_QITEM);
S
Shengliang Guan 已提交
93
  if (pMsg == NULL) return -1;
S
shm  
Shengliang Guan 已提交
94

S
Shengliang Guan 已提交
95
  dTrace("msg:%p, create and put into worker:%s, type:%s", pMsg, pWorker->name, TMSG_INFO(pRpc->msgType));
S
Shengliang Guan 已提交
96
  memcpy(pMsg, pRpc, sizeof(SRpcMsg));
S
Shengliang Guan 已提交
97 98 99
  taosWriteQitem(pWorker->queue, pMsg);
  return 0;
}
S
shm  
Shengliang Guan 已提交
100

S
Shengliang Guan 已提交
101
int32_t mmPutRpcMsgToQueryQueue(SMnodeMgmt *pMgmt, SRpcMsg *pMsg) {
S
Shengliang Guan 已提交
102 103 104 105 106 107 108 109 110
  int32_t code = -1;
  if (mmAcquire(pMgmt) == 0) {
    mmPutRpcMsgToWorker(&pMgmt->queryWorker, pMsg);
    mmRelease(pMgmt);
  } else {
    rpcFreeCont(pMsg->pCont);
    pMsg->pCont = NULL;
  }
  return code;
S
shm  
Shengliang Guan 已提交
111 112
}

S
Shengliang Guan 已提交
113
int32_t mmPutRpcMsgToWriteQueue(SMnodeMgmt *pMgmt, SRpcMsg *pMsg) {
S
Shengliang Guan 已提交
114 115 116 117 118 119 120 121 122
  int32_t code = -1;
  if (mmAcquire(pMgmt) == 0) {
    code = mmPutRpcMsgToWorker(&pMgmt->writeWorker, pMsg);
    mmRelease(pMgmt);
  } else {
    rpcFreeCont(pMsg->pCont);
    pMsg->pCont = NULL;
  }
  return code;
S
shm  
Shengliang Guan 已提交
123
}
S
shm  
Shengliang Guan 已提交
124

S
Shengliang Guan 已提交
125
int32_t mmPutRpcMsgToReadQueue(SMnodeMgmt *pMgmt, SRpcMsg *pMsg) {
S
Shengliang Guan 已提交
126 127 128 129 130 131 132 133 134
  int32_t code = -1;
  if (mmAcquire(pMgmt) == 0) {
    code = mmPutRpcMsgToWorker(&pMgmt->readWorker, pMsg);
    mmRelease(pMgmt);
  } else {
    rpcFreeCont(pMsg->pCont);
    pMsg->pCont = NULL;
  }
  return code;
S
shm  
Shengliang Guan 已提交
135
}
S
Shengliang Guan 已提交
136

dengyihao's avatar
dengyihao 已提交
137
int32_t mmPutRpcMsgToSyncQueue(SMnodeMgmt *pMgmt, SRpcMsg *pMsg) {
S
Shengliang Guan 已提交
138 139 140 141 142 143 144 145
  int32_t code = -1;
  if (mmAcquire(pMgmt) == 0) {
    code = mmPutRpcMsgToWorker(&pMgmt->syncWorker, pMsg);
    mmRelease(pMgmt);
  } else {
    rpcFreeCont(pMsg->pCont);
    pMsg->pCont = NULL;
  }
146
  return code;
dengyihao's avatar
dengyihao 已提交
147
}
D
dapan1121 已提交
148

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

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

S
Shengliang Guan 已提交
174 175 176 177 178 179 180
  SSingleWorkerCfg wCfg = {
      .min = 1,
      .max = 1,
      .name = "mnode-write",
      .fp = (FItem)mmProcessQueue,
      .param = pMgmt,
  };
S
shm  
Shengliang Guan 已提交
181
  if (tSingleWorkerInit(&pMgmt->writeWorker, &wCfg) != 0) {
S
Shengliang Guan 已提交
182
    dError("failed to start mnode-write worker since %s", terrstr());
S
Shengliang Guan 已提交
183 184 185
    return -1;
  }

S
Shengliang Guan 已提交
186 187 188 189
  SSingleWorkerCfg sCfg = {
      .min = 1,
      .max = 1,
      .name = "mnode-sync",
M
Minghao Li 已提交
190
      .fp = (FItem)mmProcessSyncQueue,
S
Shengliang Guan 已提交
191 192
      .param = pMgmt,
  };
S
shm  
Shengliang Guan 已提交
193
  if (tSingleWorkerInit(&pMgmt->syncWorker, &sCfg) != 0) {
194
    dError("failed to start mnode mnode-sync worker since %s", terrstr());
S
Shengliang Guan 已提交
195 196 197
    return -1;
  }

S
Shengliang Guan 已提交
198 199 200 201 202 203 204 205 206 207
  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;
208 209
  }

S
Shengliang Guan 已提交
210
  dDebug("mnode workers are initialized");
S
Shengliang Guan 已提交
211 212 213 214
  return 0;
}

void mmStopWorker(SMnodeMgmt *pMgmt) {
215 216 217 218 219
  taosThreadRwlockWrlock(&pMgmt->lock);
  pMgmt->stopped = 1;
  taosThreadRwlockUnlock(&pMgmt->lock);
  while (pMgmt->refCount > 0) taosMsleep(10);

220
  tSingleWorkerCleanup(&pMgmt->monitorWorker);
D
dapan1121 已提交
221
  tSingleWorkerCleanup(&pMgmt->queryWorker);
S
Shengliang Guan 已提交
222
  tSingleWorkerCleanup(&pMgmt->readWorker);
S
Shengliang Guan 已提交
223 224
  tSingleWorkerCleanup(&pMgmt->writeWorker);
  tSingleWorkerCleanup(&pMgmt->syncWorker);
S
Shengliang Guan 已提交
225
  dDebug("mnode workers are closed");
S
Shengliang Guan 已提交
226
}