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

S
Shengliang Guan 已提交
48
static void mmProcessRpcMsg(SQueueInfo *pInfo, SRpcMsg *pMsg) {
S
Shengliang Guan 已提交
49
  SMnodeMgmt *pMgmt = pInfo->ahandle;
S
Shengliang 已提交
50
  int32_t     code = -1;
S
Shengliang Guan 已提交
51
  dTrace("msg:%p, get from mnode queue", pMsg);
S
shm  
Shengliang Guan 已提交
52

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

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

S
Shengliang Guan 已提交
70
  dTrace("msg:%p, is freed, code:0x%x", pMsg, code);
S
Shengliang Guan 已提交
71
  rpcFreeCont(pMsg->pCont);
S
shm  
Shengliang Guan 已提交
72 73 74
  taosFreeQitem(pMsg);
}

S
Shengliang Guan 已提交
75
static void mmProcessSyncMsg(SQueueInfo *pInfo, SRpcMsg *pMsg) {
M
Minghao Li 已提交
76 77
  SMnodeMgmt *pMgmt = pInfo->ahandle;
  pMsg->info.node = pMgmt->pMnode;
S
Shengliang Guan 已提交
78
  dTrace("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 86 87 88
  int32_t code = mndProcessSyncMsg(pMsg);

  dTrace("msg:%p, is freed, code:0x%x", pMsg, code);
  rpcFreeCont(pMsg->pCont);
  taosFreeQitem(pMsg);
M
Minghao Li 已提交
89
}
M
Minghao Li 已提交
90

S
Shengliang Guan 已提交
91 92 93 94 95 96 97 98 99 100 101
static inline int32_t mmPutMsgToWorker(SMnodeMgmt *pMgmt, SSingleWorker *pWorker, SRpcMsg *pMsg) {
  if (mmAcquire(pMgmt) == 0) {
    dTrace("msg:%p, put into %s queue, type:%s", pMsg, pWorker->name, TMSG_INFO(pMsg->msgType));
    taosWriteQitem(pWorker->queue, pMsg);
    mmRelease(pMgmt);
    return 0;
  } else {
    dTrace("msg:%p, failed to put into %s queue since %s, type:%s", pMsg, pWorker->name, terrstr(),
           TMSG_INFO(pMsg->msgType));
    return -1;
  }
S
shm  
Shengliang Guan 已提交
102 103
}

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

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

S
Shengliang Guan 已提交
112
int32_t mmPutMsgToReadQueue(SMnodeMgmt *pMgmt, SRpcMsg *pMsg) {
S
Shengliang Guan 已提交
113
  return mmPutMsgToWorker(pMgmt, &pMgmt->readWorker, pMsg);
S
Shengliang Guan 已提交
114
}
S
Shengliang 已提交
115

S
Shengliang Guan 已提交
116
int32_t mmPutMsgToQueryQueue(SMnodeMgmt *pMgmt, SRpcMsg *pMsg) {
117 118
  pMsg->info.node = pMgmt->pMnode;
  if (mndPreProcessMsg(pMsg) != 0) {
S
Shengliang Guan 已提交
119 120 121 122
    dError("msg:%p, failed to pre-process in mnode since %s, type:%s", pMsg, terrstr(), TMSG_INFO(pMsg->msgType));
    return -1;
  }
  return mmPutMsgToWorker(pMgmt, &pMgmt->queryWorker, pMsg);
D
dapan1121 已提交
123 124
}

S
Shengliang Guan 已提交
125
int32_t mmPutMsgToMonitorQueue(SMnodeMgmt *pMgmt, SRpcMsg *pMsg) {
S
Shengliang Guan 已提交
126
  return mmPutMsgToWorker(pMgmt, &pMgmt->monitorWorker, pMsg);
127 128
}

S
Shengliang Guan 已提交
129
int32_t mmPutMsgToQueue(SMnodeMgmt *pMgmt, EQueueType qtype, SRpcMsg *pRpc) {
S
Shengliang Guan 已提交
130
  SSingleWorker *pWorker = NULL;
S
Shengliang Guan 已提交
131 132
  switch (qtype) {
    case WRITE_QUEUE:
S
Shengliang Guan 已提交
133 134
      pWorker = &pMgmt->writeWorker;
      break;
S
Shengliang Guan 已提交
135
    case QUERY_QUEUE:
S
Shengliang Guan 已提交
136 137
      pWorker = &pMgmt->queryWorker;
      break;
S
Shengliang Guan 已提交
138
    case READ_QUEUE:
S
Shengliang Guan 已提交
139 140
      pWorker = &pMgmt->readWorker;
      break;
S
Shengliang Guan 已提交
141
    case SYNC_QUEUE:
S
Shengliang Guan 已提交
142 143
      pWorker = &pMgmt->syncWorker;
      break;
S
Shengliang Guan 已提交
144 145
    default:
      terrno = TSDB_CODE_INVALID_PARA;
S
Shengliang Guan 已提交
146
  }
S
Shengliang Guan 已提交
147 148 149 150

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

S
Shengliang Guan 已提交
153
  dTrace("msg:%p, is created and will put int %s queue", pMsg, pWorker->name);
S
Shengliang Guan 已提交
154
  return mmPutMsgToWorker(pMgmt, pWorker, pMsg);
dengyihao's avatar
dengyihao 已提交
155
}
D
dapan1121 已提交
156

S
Shengliang Guan 已提交
157
int32_t mmStartWorker(SMnodeMgmt *pMgmt) {
S
Shengliang Guan 已提交
158 159 160 161
  SSingleWorkerCfg qCfg = {
      .min = tsNumOfMnodeQueryThreads,
      .max = tsNumOfMnodeQueryThreads,
      .name = "mnode-query",
S
Shengliang Guan 已提交
162
      .fp = (FItem)mmProcessRpcMsg,
S
Shengliang Guan 已提交
163 164
      .param = pMgmt,
  };
S
shm  
Shengliang Guan 已提交
165
  if (tSingleWorkerInit(&pMgmt->queryWorker, &qCfg) != 0) {
D
dapan1121 已提交
166 167 168 169
    dError("failed to start mnode-query worker since %s", terrstr());
    return -1;
  }

S
Shengliang Guan 已提交
170 171 172 173
  SSingleWorkerCfg rCfg = {
      .min = tsNumOfMnodeReadThreads,
      .max = tsNumOfMnodeReadThreads,
      .name = "mnode-read",
S
Shengliang Guan 已提交
174
      .fp = (FItem)mmProcessRpcMsg,
S
Shengliang Guan 已提交
175 176
      .param = pMgmt,
  };
S
shm  
Shengliang Guan 已提交
177
  if (tSingleWorkerInit(&pMgmt->readWorker, &rCfg) != 0) {
S
Shengliang Guan 已提交
178
    dError("failed to start mnode-read worker since %s", terrstr());
S
Shengliang Guan 已提交
179 180 181
    return -1;
  }

S
Shengliang Guan 已提交
182 183 184 185
  SSingleWorkerCfg wCfg = {
      .min = 1,
      .max = 1,
      .name = "mnode-write",
S
Shengliang Guan 已提交
186
      .fp = (FItem)mmProcessRpcMsg,
S
Shengliang Guan 已提交
187 188
      .param = pMgmt,
  };
S
shm  
Shengliang Guan 已提交
189
  if (tSingleWorkerInit(&pMgmt->writeWorker, &wCfg) != 0) {
S
Shengliang Guan 已提交
190
    dError("failed to start mnode-write worker since %s", terrstr());
S
Shengliang Guan 已提交
191 192 193
    return -1;
  }

S
Shengliang Guan 已提交
194 195 196 197
  SSingleWorkerCfg sCfg = {
      .min = 1,
      .max = 1,
      .name = "mnode-sync",
S
Shengliang Guan 已提交
198
      .fp = (FItem)mmProcessSyncMsg,
S
Shengliang Guan 已提交
199 200
      .param = pMgmt,
  };
S
shm  
Shengliang Guan 已提交
201
  if (tSingleWorkerInit(&pMgmt->syncWorker, &sCfg) != 0) {
202
    dError("failed to start mnode mnode-sync worker since %s", terrstr());
S
Shengliang Guan 已提交
203 204 205
    return -1;
  }

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

S
Shengliang Guan 已提交
218
  dDebug("mnode workers are initialized");
S
Shengliang Guan 已提交
219 220 221 222
  return 0;
}

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

225
  tSingleWorkerCleanup(&pMgmt->monitorWorker);
D
dapan1121 已提交
226
  tSingleWorkerCleanup(&pMgmt->queryWorker);
S
Shengliang Guan 已提交
227
  tSingleWorkerCleanup(&pMgmt->readWorker);
S
Shengliang Guan 已提交
228 229
  tSingleWorkerCleanup(&pMgmt->writeWorker);
  tSingleWorkerCleanup(&pMgmt->syncWorker);
S
Shengliang Guan 已提交
230
  dDebug("mnode workers are closed");
S
Shengliang Guan 已提交
231
}