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;
49
  pMsg->info.node = pMgmt->pMnode;
S
Shengliang Guan 已提交
50 51

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

54
  int32_t code = mndProcessRpcMsg(pMsg);
S
shm  
Shengliang Guan 已提交
55

S
Shengliang Guan 已提交
56
  if (IsReq(pMsg) && pMsg->info.handle != NULL && code != TSDB_CODE_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
57 58
    if (code != 0 && terrno != 0) code = terrno;
    mmSendRsp(pMsg, code);
59 60 61
  } else {
    rpcFreeCont(pMsg->info.rsp);
    pMsg->info.rsp = NULL;
S
shm  
Shengliang Guan 已提交
62 63
  }

64
  if (code == TSDB_CODE_SYN_NOT_LEADER || code == TSDB_CODE_SYN_RESTORING) {
65 66 67
    mndPostProcessQueryMsg(pMsg);
  }

H
Haojun Liao 已提交
68
  dGTrace("msg:%p is freed, code:%s", pMsg, tstrerror(code));
S
Shengliang Guan 已提交
69
  rpcFreeCont(pMsg->pCont);
S
shm  
Shengliang Guan 已提交
70 71 72
  taosFreeQitem(pMsg);
}

S
Shengliang Guan 已提交
73
static void mmProcessSyncMsg(SQueueInfo *pInfo, SRpcMsg *pMsg) {
M
Minghao Li 已提交
74 75
  SMnodeMgmt *pMgmt = pInfo->ahandle;
  pMsg->info.node = pMgmt->pMnode;
S
Shengliang Guan 已提交
76 77 78

  const STraceId *trace = &pMsg->info.traceId;
  dGTrace("msg:%p, get from mnode-sync queue", pMsg);
M
Minghao Li 已提交
79 80 81 82 83

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

84 85
  int32_t code = mndProcessSyncMsg(pMsg);

S
Shengliang Guan 已提交
86
  dGTrace("msg:%p, is freed, code:0x%x", pMsg, code);
87 88
  rpcFreeCont(pMsg->pCont);
  taosFreeQitem(pMsg);
M
Minghao Li 已提交
89
}
M
Minghao Li 已提交
90

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

S
Shengliang Guan 已提交
94
  if (mmAcquire(pMgmt) == 0) {
S
Shengliang Guan 已提交
95
    dGTrace("msg:%p, put into %s queue, type:%s", pMsg, pWorker->name, TMSG_INFO(pMsg->msgType));
S
Shengliang Guan 已提交
96 97 98 99
    taosWriteQitem(pWorker->queue, pMsg);
    mmRelease(pMgmt);
    return 0;
  } else {
S
Shengliang Guan 已提交
100 101
    dGTrace("msg:%p, failed to put into %s queue since %s, type:%s", pMsg, pWorker->name, terrstr(),
            TMSG_INFO(pMsg->msgType));
S
Shengliang Guan 已提交
102 103
    return -1;
  }
S
shm  
Shengliang Guan 已提交
104 105
}

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

S
Shengliang Guan 已提交
110
int32_t mmPutMsgToSyncQueue(SMnodeMgmt *pMgmt, SRpcMsg *pMsg) {
S
Shengliang Guan 已提交
111
  return mmPutMsgToWorker(pMgmt, &pMgmt->syncWorker, pMsg);
S
Shengliang Guan 已提交
112
}
S
shm  
Shengliang Guan 已提交
113

114 115
int32_t mmPutMsgToSyncRdQueue(SMnodeMgmt *pMgmt, SRpcMsg *pMsg) {
  return mmPutMsgToWorker(pMgmt, &pMgmt->syncRdWorker, pMsg);
116 117
}

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

S
Shengliang Guan 已提交
122
int32_t mmPutMsgToQueryQueue(SMnodeMgmt *pMgmt, SRpcMsg *pMsg) {
123
  pMsg->info.node = pMgmt->pMnode;
124
  if (mndPreProcessQueryMsg(pMsg) != 0) {
S
Shengliang Guan 已提交
125 126
    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 已提交
127 128 129
    return -1;
  }
  return mmPutMsgToWorker(pMgmt, &pMgmt->queryWorker, pMsg);
D
dapan1121 已提交
130 131
}

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

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

  if (pWorker == NULL) return -1;
S
Shengliang Guan 已提交
162
  SRpcMsg *pMsg = taosAllocateQitem(sizeof(SRpcMsg), RPC_QITEM, pRpc->contLen);
S
Shengliang Guan 已提交
163
  if (pMsg == NULL) return -1;
S
Shengliang Guan 已提交
164
  memcpy(pMsg, pRpc, sizeof(SRpcMsg));
165
  pRpc->pCont = NULL;
S
Shengliang Guan 已提交
166

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

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

D
dapan1121 已提交
190 191 192 193 194 195 196 197 198 199 200 201
  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 已提交
202 203 204 205
  SSingleWorkerCfg rCfg = {
      .min = tsNumOfMnodeReadThreads,
      .max = tsNumOfMnodeReadThreads,
      .name = "mnode-read",
S
Shengliang Guan 已提交
206
      .fp = (FItem)mmProcessRpcMsg,
S
Shengliang Guan 已提交
207 208
      .param = pMgmt,
  };
S
shm  
Shengliang Guan 已提交
209
  if (tSingleWorkerInit(&pMgmt->readWorker, &rCfg) != 0) {
S
Shengliang Guan 已提交
210
    dError("failed to start mnode-read worker since %s", terrstr());
S
Shengliang Guan 已提交
211 212 213
    return -1;
  }

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

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

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

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

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

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