mmWorker.c 5.8 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 102
int32_t mmPutRpcMsgToQueryQueue(SMnodeMgmt *pMgmt, SRpcMsg *pMsg) {
  return mmPutRpcMsgToWorker(&pMgmt->queryWorker, pMsg);
S
shm  
Shengliang Guan 已提交
103 104
}

S
Shengliang Guan 已提交
105 106
int32_t mmPutRpcMsgToWriteQueue(SMnodeMgmt *pMgmt, SRpcMsg *pMsg) {
  return mmPutRpcMsgToWorker(&pMgmt->writeWorker, pMsg);
S
shm  
Shengliang Guan 已提交
107
}
S
shm  
Shengliang Guan 已提交
108

S
Shengliang Guan 已提交
109 110
int32_t mmPutRpcMsgToReadQueue(SMnodeMgmt *pMgmt, SRpcMsg *pMsg) {
  return mmPutRpcMsgToWorker(&pMgmt->readWorker, pMsg);
S
shm  
Shengliang Guan 已提交
111
}
S
Shengliang Guan 已提交
112

dengyihao's avatar
dengyihao 已提交
113
int32_t mmPutRpcMsgToSyncQueue(SMnodeMgmt *pMgmt, SRpcMsg *pMsg) {
114 115 116 117
  if (mmAcquire(pMgmt) != 0) return -1;
  int32_t code = mmPutRpcMsgToWorker(&pMgmt->syncWorker, pMsg);
  mmRelease(pMgmt);
  return code;
dengyihao's avatar
dengyihao 已提交
118
}
D
dapan1121 已提交
119

S
Shengliang Guan 已提交
120
int32_t mmStartWorker(SMnodeMgmt *pMgmt) {
S
Shengliang Guan 已提交
121 122 123 124
  SSingleWorkerCfg qCfg = {
      .min = tsNumOfMnodeQueryThreads,
      .max = tsNumOfMnodeQueryThreads,
      .name = "mnode-query",
S
Shengliang Guan 已提交
125
      .fp = (FItem)mmProcessQueue,
S
Shengliang Guan 已提交
126 127
      .param = pMgmt,
  };
S
shm  
Shengliang Guan 已提交
128
  if (tSingleWorkerInit(&pMgmt->queryWorker, &qCfg) != 0) {
D
dapan1121 已提交
129 130 131 132
    dError("failed to start mnode-query worker since %s", terrstr());
    return -1;
  }

S
Shengliang Guan 已提交
133 134 135 136 137 138 139
  SSingleWorkerCfg rCfg = {
      .min = tsNumOfMnodeReadThreads,
      .max = tsNumOfMnodeReadThreads,
      .name = "mnode-read",
      .fp = (FItem)mmProcessQueue,
      .param = pMgmt,
  };
S
shm  
Shengliang Guan 已提交
140
  if (tSingleWorkerInit(&pMgmt->readWorker, &rCfg) != 0) {
S
Shengliang Guan 已提交
141
    dError("failed to start mnode-read worker since %s", terrstr());
S
Shengliang Guan 已提交
142 143 144
    return -1;
  }

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

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

S
Shengliang Guan 已提交
169 170 171 172 173 174 175 176 177 178
  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;
179 180
  }

S
Shengliang Guan 已提交
181
  dDebug("mnode workers are initialized");
S
Shengliang Guan 已提交
182 183 184 185
  return 0;
}

void mmStopWorker(SMnodeMgmt *pMgmt) {
186 187 188 189 190
  taosThreadRwlockWrlock(&pMgmt->lock);
  pMgmt->stopped = 1;
  taosThreadRwlockUnlock(&pMgmt->lock);
  while (pMgmt->refCount > 0) taosMsleep(10);

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