mmWorker.c 5.9 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
  return mmPutRpcMsgToWorker(&pMgmt->queryWorker, pMsg);
S
shm  
Shengliang Guan 已提交
103 104
}

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

S
Shengliang Guan 已提交
109
int32_t mmPutRpcMsgToReadQueue(SMnodeMgmt *pMgmt, SRpcMsg *pMsg) {
S
Shengliang Guan 已提交
110
  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) {
S
Shengliang Guan 已提交
114 115 116 117
  int32_t code = -1;
  if (mmAcquire(pMgmt) == 0) {
    code = mmPutRpcMsgToWorker(&pMgmt->syncWorker, pMsg);
    mmRelease(pMgmt);
S
Shengliang Guan 已提交
118 119 120
  }

  if (code != 0) {
S
Shengliang Guan 已提交
121 122 123
    rpcFreeCont(pMsg->pCont);
    pMsg->pCont = NULL;
  }
124
  return code;
dengyihao's avatar
dengyihao 已提交
125
}
D
dapan1121 已提交
126

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

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

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

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

S
Shengliang Guan 已提交
176 177 178 179 180 181 182 183 184 185
  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;
186 187
  }

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

void mmStopWorker(SMnodeMgmt *pMgmt) {
193 194 195 196 197
  taosThreadRwlockWrlock(&pMgmt->lock);
  pMgmt->stopped = 1;
  taosThreadRwlockUnlock(&pMgmt->lock);
  while (pMgmt->refCount > 0) taosMsleep(10);

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