mmWorker.c 5.4 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);
}

S
Shengliang Guan 已提交
59
static int32_t mmPutNodeMsgToWorker(SSingleWorker *pWorker, SRpcMsg *pMsg) {
S
Shengliang Guan 已提交
60
  dTrace("msg:%p, put into worker %s, type:%s", pMsg, pWorker->name, TMSG_INFO(pMsg->msgType));
S
Shengliang Guan 已提交
61 62
  taosWriteQitem(pWorker->queue, pMsg);
  return 0;
S
shm  
Shengliang Guan 已提交
63 64
}

S
Shengliang Guan 已提交
65 66 67
int32_t mmPutNodeMsgToWriteQueue(SMnodeMgmt *pMgmt, SRpcMsg *pMsg) {
  return mmPutNodeMsgToWorker(&pMgmt->writeWorker, pMsg);
}
S
shm  
Shengliang Guan 已提交
68

S
Shengliang Guan 已提交
69 70 71
int32_t mmPutNodeMsgToSyncQueue(SMnodeMgmt *pMgmt, SRpcMsg *pMsg) {
  return mmPutNodeMsgToWorker(&pMgmt->syncWorker, pMsg);
}
S
shm  
Shengliang Guan 已提交
72

S
Shengliang Guan 已提交
73 74 75
int32_t mmPutNodeMsgToReadQueue(SMnodeMgmt *pMgmt, SRpcMsg *pMsg) {
  return mmPutNodeMsgToWorker(&pMgmt->readWorker, pMsg);
}
S
Shengliang 已提交
76

S
Shengliang Guan 已提交
77 78
int32_t mmPutNodeMsgToQueryQueue(SMnodeMgmt *pMgmt, SRpcMsg *pMsg) {
  return mmPutNodeMsgToWorker(&pMgmt->queryWorker, pMsg);
D
dapan1121 已提交
79 80
}

S
Shengliang Guan 已提交
81
int32_t mmPutNodeMsgToMonitorQueue(SMnodeMgmt *pMgmt, SRpcMsg *pMsg) {
S
Shengliang 已提交
82
  return mmPutNodeMsgToWorker(&pMgmt->monitorWorker, pMsg);
83 84
}

S
Shengliang 已提交
85
static inline int32_t mmPutRpcMsgToWorker(SSingleWorker *pWorker, SRpcMsg *pRpc) {
S
Shengliang Guan 已提交
86
  SRpcMsg *pMsg = taosAllocateQitem(sizeof(SRpcMsg), RPC_QITEM);
S
Shengliang Guan 已提交
87
  if (pMsg == NULL) return -1;
S
shm  
Shengliang Guan 已提交
88

S
Shengliang Guan 已提交
89
  dTrace("msg:%p, create and put into worker:%s, type:%s", pMsg, pWorker->name, TMSG_INFO(pRpc->msgType));
S
Shengliang Guan 已提交
90
  memcpy(pMsg, pRpc, sizeof(SRpcMsg));
S
Shengliang Guan 已提交
91 92 93
  taosWriteQitem(pWorker->queue, pMsg);
  return 0;
}
S
shm  
Shengliang Guan 已提交
94

S
Shengliang Guan 已提交
95 96
int32_t mmPutRpcMsgToQueryQueue(SMnodeMgmt *pMgmt, SRpcMsg *pMsg) {
  return mmPutRpcMsgToWorker(&pMgmt->queryWorker, pMsg);
S
shm  
Shengliang Guan 已提交
97 98
}

S
Shengliang Guan 已提交
99 100
int32_t mmPutRpcMsgToWriteQueue(SMnodeMgmt *pMgmt, SRpcMsg *pMsg) {
  return mmPutRpcMsgToWorker(&pMgmt->writeWorker, pMsg);
S
shm  
Shengliang Guan 已提交
101
}
S
shm  
Shengliang Guan 已提交
102

S
Shengliang Guan 已提交
103 104
int32_t mmPutRpcMsgToReadQueue(SMnodeMgmt *pMgmt, SRpcMsg *pMsg) {
  return mmPutRpcMsgToWorker(&pMgmt->readWorker, pMsg);
S
shm  
Shengliang Guan 已提交
105
}
S
Shengliang Guan 已提交
106

dengyihao's avatar
dengyihao 已提交
107 108 109
int32_t mmPutRpcMsgToSyncQueue(SMnodeMgmt *pMgmt, SRpcMsg *pMsg) {
  return mmPutRpcMsgToWorker(&pMgmt->syncWorker, pMsg);
}
D
dapan1121 已提交
110

S
Shengliang Guan 已提交
111
int32_t mmStartWorker(SMnodeMgmt *pMgmt) {
S
Shengliang Guan 已提交
112 113 114 115
  SSingleWorkerCfg qCfg = {
      .min = tsNumOfMnodeQueryThreads,
      .max = tsNumOfMnodeQueryThreads,
      .name = "mnode-query",
S
Shengliang Guan 已提交
116
      .fp = (FItem)mmProcessQueue,
S
Shengliang Guan 已提交
117 118
      .param = pMgmt,
  };
S
shm  
Shengliang Guan 已提交
119
  if (tSingleWorkerInit(&pMgmt->queryWorker, &qCfg) != 0) {
D
dapan1121 已提交
120 121 122 123
    dError("failed to start mnode-query worker since %s", terrstr());
    return -1;
  }

S
Shengliang Guan 已提交
124 125 126 127 128 129 130
  SSingleWorkerCfg rCfg = {
      .min = tsNumOfMnodeReadThreads,
      .max = tsNumOfMnodeReadThreads,
      .name = "mnode-read",
      .fp = (FItem)mmProcessQueue,
      .param = pMgmt,
  };
S
shm  
Shengliang Guan 已提交
131
  if (tSingleWorkerInit(&pMgmt->readWorker, &rCfg) != 0) {
S
Shengliang Guan 已提交
132
    dError("failed to start mnode-read worker since %s", terrstr());
S
Shengliang Guan 已提交
133 134 135
    return -1;
  }

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

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

S
Shengliang Guan 已提交
160 161 162 163 164 165 166 167 168 169
  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;
170 171
  }

S
Shengliang Guan 已提交
172
  dDebug("mnode workers are initialized");
S
Shengliang Guan 已提交
173 174 175 176
  return 0;
}

void mmStopWorker(SMnodeMgmt *pMgmt) {
177
  tSingleWorkerCleanup(&pMgmt->monitorWorker);
D
dapan1121 已提交
178
  tSingleWorkerCleanup(&pMgmt->queryWorker);
S
Shengliang Guan 已提交
179
  tSingleWorkerCleanup(&pMgmt->readWorker);
S
Shengliang Guan 已提交
180 181
  tSingleWorkerCleanup(&pMgmt->writeWorker);
  tSingleWorkerCleanup(&pMgmt->syncWorker);
S
Shengliang Guan 已提交
182
  dDebug("mnode workers are closed");
S
Shengliang Guan 已提交
183
}