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

S
Shengliang Guan 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
static inline int32_t mmAcquire(SMnodeMgmt *pMgmt) {
  int32_t code = 0;
  taosThreadRwlockRdlock(&pMgmt->lock);
  if (pMgmt->stopped) {
    code = -1;
  } else {
    atomic_add_fetch_32(&pMgmt->refCount, 1);
  }
  taosThreadRwlockUnlock(&pMgmt->lock);
  return code;
}

static inline void mmRelease(SMnodeMgmt *pMgmt) {
  taosThreadRwlockRdlock(&pMgmt->lock);
  atomic_sub_fetch_32(&pMgmt->refCount, 1);
  taosThreadRwlockUnlock(&pMgmt->lock);
}

S
Shengliang Guan 已提交
37
static inline void mmSendRsp(SRpcMsg *pMsg, int32_t code) {
S
Shengliang Guan 已提交
38 39
  SRpcMsg rsp = {
      .code = code,
S
Shengliang Guan 已提交
40 41
      .pCont = pMsg->info.rsp,
      .contLen = pMsg->info.rspLen,
42
      .info = pMsg->info,
S
Shengliang Guan 已提交
43
  };
44 45 46
  tmsgSendRsp(&rsp);
}

S
Shengliang Guan 已提交
47
static void mmProcessRpcMsg(SQueueInfo *pInfo, SRpcMsg *pMsg) {
S
Shengliang Guan 已提交
48
  SMnodeMgmt *pMgmt = pInfo->ahandle;
S
Shengliang 已提交
49
  int32_t     code = -1;
S
Shengliang Guan 已提交
50 51

  const STraceId *trace = &pMsg->info.traceId;
dengyihao's avatar
dengyihao 已提交
52
  dGTrace("msg:%p, get from mnode queue", pMsg);
S
shm  
Shengliang Guan 已提交
53

S
Shengliang Guan 已提交
54
  switch (pMsg->msgType) {
S
Shengliang Guan 已提交
55
    case TDMT_MON_MM_INFO:
S
Shengliang 已提交
56
      code = mmProcessGetMonitorInfoReq(pMgmt, pMsg);
S
Shengliang Guan 已提交
57 58
      break;
    case TDMT_MON_MM_LOAD:
S
Shengliang 已提交
59
      code = mmProcessGetLoadsReq(pMgmt, pMsg);
S
Shengliang Guan 已提交
60 61
      break;
    default:
S
Shengliang Guan 已提交
62
      pMsg->info.node = pMgmt->pMnode;
S
Shengliang Guan 已提交
63
      code = mndProcessRpcMsg(pMsg);
S
shm  
Shengliang Guan 已提交
64 65
  }

S
Shengliang Guan 已提交
66
  if (IsReq(pMsg) && pMsg->info.handle != NULL && code != TSDB_CODE_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
67 68
    if (code != 0 && terrno != 0) code = terrno;
    mmSendRsp(pMsg, code);
S
shm  
Shengliang Guan 已提交
69 70
  }

71 72 73 74
  if (code == TSDB_CODE_RPC_REDIRECT) {
    mndPostProcessQueryMsg(pMsg);
  }

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

S
Shengliang Guan 已提交
80
static void mmProcessSyncMsg(SQueueInfo *pInfo, SRpcMsg *pMsg) {
M
Minghao Li 已提交
81 82
  SMnodeMgmt *pMgmt = pInfo->ahandle;
  pMsg->info.node = pMgmt->pMnode;
S
Shengliang Guan 已提交
83 84 85

  const STraceId *trace = &pMsg->info.traceId;
  dGTrace("msg:%p, get from mnode-sync queue", pMsg);
M
Minghao Li 已提交
86 87 88 89 90

  SMsgHead *pHead = pMsg->pCont;
  pHead->contLen = ntohl(pHead->contLen);
  pHead->vgId = ntohl(pHead->vgId);

91 92
  int32_t code = mndProcessSyncMsg(pMsg);

S
Shengliang Guan 已提交
93
  dGTrace("msg:%p, is freed, code:0x%x", pMsg, code);
94 95
  rpcFreeCont(pMsg->pCont);
  taosFreeQitem(pMsg);
M
Minghao Li 已提交
96
}
M
Minghao Li 已提交
97

S
Shengliang Guan 已提交
98
static inline int32_t mmPutMsgToWorker(SMnodeMgmt *pMgmt, SSingleWorker *pWorker, SRpcMsg *pMsg) {
S
Shengliang Guan 已提交
99 100
  const STraceId *trace = &pMsg->info.traceId;

S
Shengliang Guan 已提交
101
  if (mmAcquire(pMgmt) == 0) {
S
Shengliang Guan 已提交
102
    dGTrace("msg:%p, put into %s queue, type:%s", pMsg, pWorker->name, TMSG_INFO(pMsg->msgType));
S
Shengliang Guan 已提交
103 104 105 106
    taosWriteQitem(pWorker->queue, pMsg);
    mmRelease(pMgmt);
    return 0;
  } else {
S
Shengliang Guan 已提交
107 108
    dGTrace("msg:%p, failed to put into %s queue since %s, type:%s", pMsg, pWorker->name, terrstr(),
            TMSG_INFO(pMsg->msgType));
S
Shengliang Guan 已提交
109 110
    return -1;
  }
S
shm  
Shengliang Guan 已提交
111 112
}

S
Shengliang Guan 已提交
113
int32_t mmPutMsgToWriteQueue(SMnodeMgmt *pMgmt, SRpcMsg *pMsg) {
S
Shengliang Guan 已提交
114
  return mmPutMsgToWorker(pMgmt, &pMgmt->writeWorker, pMsg);
S
Shengliang Guan 已提交
115
}
S
shm  
Shengliang Guan 已提交
116

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

S
Shengliang Guan 已提交
121
int32_t mmPutMsgToReadQueue(SMnodeMgmt *pMgmt, SRpcMsg *pMsg) {
S
Shengliang Guan 已提交
122
  return mmPutMsgToWorker(pMgmt, &pMgmt->readWorker, pMsg);
S
Shengliang Guan 已提交
123
}
S
Shengliang 已提交
124

S
Shengliang Guan 已提交
125
int32_t mmPutMsgToQueryQueue(SMnodeMgmt *pMgmt, SRpcMsg *pMsg) {
126
  pMsg->info.node = pMgmt->pMnode;
127
  if (mndPreProcessQueryMsg(pMsg) != 0) {
S
Shengliang Guan 已提交
128 129
    const STraceId *trace = &pMsg->info.traceId;
    dGError("msg:%p, failed to pre-process in mnode since %s, type:%s", pMsg, terrstr(), TMSG_INFO(pMsg->msgType));
S
Shengliang Guan 已提交
130 131 132
    return -1;
  }
  return mmPutMsgToWorker(pMgmt, &pMgmt->queryWorker, pMsg);
D
dapan1121 已提交
133 134
}

D
dapan1121 已提交
135 136 137 138
int32_t mmPutMsgToFetchQueue(SMnodeMgmt *pMgmt, SRpcMsg *pMsg) {
  return mmPutMsgToWorker(pMgmt, &pMgmt->fetchWorker, pMsg);
}

S
Shengliang Guan 已提交
139
int32_t mmPutMsgToMonitorQueue(SMnodeMgmt *pMgmt, SRpcMsg *pMsg) {
S
Shengliang Guan 已提交
140
  return mmPutMsgToWorker(pMgmt, &pMgmt->monitorWorker, pMsg);
141 142
}

S
Shengliang Guan 已提交
143
int32_t mmPutMsgToQueue(SMnodeMgmt *pMgmt, EQueueType qtype, SRpcMsg *pRpc) {
S
Shengliang Guan 已提交
144
  SSingleWorker *pWorker = NULL;
S
Shengliang Guan 已提交
145 146
  switch (qtype) {
    case WRITE_QUEUE:
S
Shengliang Guan 已提交
147 148
      pWorker = &pMgmt->writeWorker;
      break;
S
Shengliang Guan 已提交
149
    case QUERY_QUEUE:
S
Shengliang Guan 已提交
150 151
      pWorker = &pMgmt->queryWorker;
      break;
D
dapan1121 已提交
152 153 154
    case FETCH_QUEUE:
      pWorker = &pMgmt->fetchWorker;
      break;
S
Shengliang Guan 已提交
155
    case READ_QUEUE:
S
Shengliang Guan 已提交
156 157
      pWorker = &pMgmt->readWorker;
      break;
S
Shengliang Guan 已提交
158
    case SYNC_QUEUE:
S
Shengliang Guan 已提交
159 160
      pWorker = &pMgmt->syncWorker;
      break;
S
Shengliang Guan 已提交
161 162
    default:
      terrno = TSDB_CODE_INVALID_PARA;
S
Shengliang Guan 已提交
163
  }
S
Shengliang Guan 已提交
164 165 166 167

  if (pWorker == NULL) return -1;
  SRpcMsg *pMsg = taosAllocateQitem(sizeof(SRpcMsg), RPC_QITEM);
  if (pMsg == NULL) return -1;
S
Shengliang Guan 已提交
168
  memcpy(pMsg, pRpc, sizeof(SRpcMsg));
S
Shengliang Guan 已提交
169

S
Shengliang Guan 已提交
170
  dTrace("msg:%p, is created and will put into %s queue, type:%s", pMsg, pWorker->name, TMSG_INFO(pRpc->msgType));
171 172 173 174 175 176
  int32_t code = mmPutMsgToWorker(pMgmt, pWorker, pMsg);
  if (code != 0) {
    dTrace("msg:%p, is freed", pMsg);
    taosFreeQitem(pMsg);
  }
  return code;
dengyihao's avatar
dengyihao 已提交
177
}
D
dapan1121 已提交
178

S
Shengliang Guan 已提交
179
int32_t mmStartWorker(SMnodeMgmt *pMgmt) {
S
Shengliang Guan 已提交
180 181 182 183
  SSingleWorkerCfg qCfg = {
      .min = tsNumOfMnodeQueryThreads,
      .max = tsNumOfMnodeQueryThreads,
      .name = "mnode-query",
S
Shengliang Guan 已提交
184
      .fp = (FItem)mmProcessRpcMsg,
S
Shengliang Guan 已提交
185 186
      .param = pMgmt,
  };
S
shm  
Shengliang Guan 已提交
187
  if (tSingleWorkerInit(&pMgmt->queryWorker, &qCfg) != 0) {
D
dapan1121 已提交
188 189 190 191
    dError("failed to start mnode-query worker since %s", terrstr());
    return -1;
  }

D
dapan1121 已提交
192 193 194 195 196 197 198 199 200 201 202 203
  SSingleWorkerCfg fCfg = {
      .min = tsNumOfMnodeFetchThreads,
      .max = tsNumOfMnodeFetchThreads,
      .name = "mnode-fetch",
      .fp = (FItem)mmProcessRpcMsg,
      .param = pMgmt,
  };
  if (tSingleWorkerInit(&pMgmt->fetchWorker, &fCfg) != 0) {
    dError("failed to start mnode-fetch worker since %s", terrstr());
    return -1;
  }

S
Shengliang Guan 已提交
204 205 206 207
  SSingleWorkerCfg rCfg = {
      .min = tsNumOfMnodeReadThreads,
      .max = tsNumOfMnodeReadThreads,
      .name = "mnode-read",
S
Shengliang Guan 已提交
208
      .fp = (FItem)mmProcessRpcMsg,
S
Shengliang Guan 已提交
209 210
      .param = pMgmt,
  };
S
shm  
Shengliang Guan 已提交
211
  if (tSingleWorkerInit(&pMgmt->readWorker, &rCfg) != 0) {
S
Shengliang Guan 已提交
212
    dError("failed to start mnode-read worker since %s", terrstr());
S
Shengliang Guan 已提交
213 214 215
    return -1;
  }

S
Shengliang Guan 已提交
216 217 218 219
  SSingleWorkerCfg wCfg = {
      .min = 1,
      .max = 1,
      .name = "mnode-write",
S
Shengliang Guan 已提交
220
      .fp = (FItem)mmProcessRpcMsg,
S
Shengliang Guan 已提交
221 222
      .param = pMgmt,
  };
S
shm  
Shengliang Guan 已提交
223
  if (tSingleWorkerInit(&pMgmt->writeWorker, &wCfg) != 0) {
S
Shengliang Guan 已提交
224
    dError("failed to start mnode-write worker since %s", terrstr());
S
Shengliang Guan 已提交
225 226 227
    return -1;
  }

S
Shengliang Guan 已提交
228 229 230 231
  SSingleWorkerCfg sCfg = {
      .min = 1,
      .max = 1,
      .name = "mnode-sync",
S
Shengliang Guan 已提交
232
      .fp = (FItem)mmProcessSyncMsg,
S
Shengliang Guan 已提交
233 234
      .param = pMgmt,
  };
S
shm  
Shengliang Guan 已提交
235
  if (tSingleWorkerInit(&pMgmt->syncWorker, &sCfg) != 0) {
236
    dError("failed to start mnode mnode-sync worker since %s", terrstr());
S
Shengliang Guan 已提交
237 238 239
    return -1;
  }

S
Shengliang Guan 已提交
240 241 242 243
  SSingleWorkerCfg mCfg = {
      .min = 1,
      .max = 1,
      .name = "mnode-monitor",
S
Shengliang Guan 已提交
244
      .fp = (FItem)mmProcessRpcMsg,
S
Shengliang Guan 已提交
245 246 247 248 249
      .param = pMgmt,
  };
  if (tSingleWorkerInit(&pMgmt->monitorWorker, &mCfg) != 0) {
    dError("failed to start mnode mnode-monitor worker since %s", terrstr());
    return -1;
250 251
  }

S
Shengliang Guan 已提交
252
  dDebug("mnode workers are initialized");
S
Shengliang Guan 已提交
253 254 255 256
  return 0;
}

void mmStopWorker(SMnodeMgmt *pMgmt) {
257 258
  while (pMgmt->refCount > 0) taosMsleep(10);

259
  tSingleWorkerCleanup(&pMgmt->monitorWorker);
D
dapan1121 已提交
260
  tSingleWorkerCleanup(&pMgmt->queryWorker);
D
dapan1121 已提交
261
  tSingleWorkerCleanup(&pMgmt->fetchWorker);
S
Shengliang Guan 已提交
262
  tSingleWorkerCleanup(&pMgmt->readWorker);
S
Shengliang Guan 已提交
263 264
  tSingleWorkerCleanup(&pMgmt->writeWorker);
  tSingleWorkerCleanup(&pMgmt->syncWorker);
S
Shengliang Guan 已提交
265
  dDebug("mnode workers are closed");
S
Shengliang Guan 已提交
266
}