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 20 21
static void mmProcessQueue(SQueueInfo *pInfo, SNodeMsg *pMsg) {
  SMnodeMgmt *pMgmt = pInfo->ahandle;

S
Shengliang Guan 已提交
22
  dTrace("msg:%p, get from mnode queue", pMsg);
S
shm  
Shengliang Guan 已提交
23 24 25
  SRpcMsg *pRpc = &pMsg->rpcMsg;
  int32_t  code = -1;

S
shm  
Shengliang Guan 已提交
26 27
  if (pMsg->rpcMsg.msgType != TDMT_DND_ALTER_MNODE) {
    pMsg->pNode = pMgmt->pMnode;
S
shm  
Shengliang Guan 已提交
28
    code = mndProcessMsg(pMsg);
S
shm  
Shengliang Guan 已提交
29 30
  } else {
    code = mmProcessAlterReq(pMgmt, pMsg);
S
shm  
Shengliang Guan 已提交
31 32
  }

S
shm  
Shengliang Guan 已提交
33
  if (pRpc->msgType & 1U) {
S
Shengliang Guan 已提交
34
    if (pRpc->handle != NULL && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) {
S
fix ci  
Shengliang Guan 已提交
35 36 37 38
      if (code != 0) {
        code = terrno;
        dError("msg:%p, failed to process since %s", pMsg, terrstr());
      }
S
shm  
Shengliang Guan 已提交
39
      SRpcMsg rsp = {.handle = pRpc->handle, .code = code, .contLen = pMsg->rspLen, .pCont = pMsg->pRsp};
S
Shengliang Guan 已提交
40
      tmsgSendRsp(&rsp);
S
shm  
Shengliang Guan 已提交
41 42 43
    }
  }

S
shm  
Shengliang Guan 已提交
44
  dTrace("msg:%p, is freed, result:0x%04x:%s", pMsg, code & 0XFFFF, tstrerror(code));
S
shm  
Shengliang Guan 已提交
45 46 47 48
  rpcFreeCont(pRpc->pCont);
  taosFreeQitem(pMsg);
}

D
dapan1121 已提交
49 50 51
static void mmProcessQueryQueue(SQueueInfo *pInfo, SNodeMsg *pMsg) {
  SMnodeMgmt *pMgmt = pInfo->ahandle;

S
Shengliang Guan 已提交
52
  dTrace("msg:%p, get from mnode query queue", pMsg);
D
dapan1121 已提交
53 54 55 56 57 58 59
  SRpcMsg *pRpc = &pMsg->rpcMsg;
  int32_t  code = -1;

  pMsg->pNode = pMgmt->pMnode;
  code = mndProcessMsg(pMsg);

  if (pRpc->msgType & 1U) {
S
Shengliang Guan 已提交
60 61
    if (pRpc->handle != NULL && code != 0) {
      dError("msg:%p, failed to process since %s", pMsg, terrstr());
D
dapan1121 已提交
62
      SRpcMsg rsp = {.handle = pRpc->handle, .code = code, .ahandle = pRpc->ahandle};
S
Shengliang Guan 已提交
63
      tmsgSendRsp(&rsp);
D
dapan1121 已提交
64 65 66 67 68 69 70 71
    }
  }

  dTrace("msg:%p, is freed, result:0x%04x:%s", pMsg, code & 0XFFFF, tstrerror(code));
  rpcFreeCont(pRpc->pCont);
  taosFreeQitem(pMsg);
}

S
Shengliang Guan 已提交
72
static void mmPutMsgToWorker(SSingleWorker *pWorker, SNodeMsg *pMsg) {
S
shm  
Shengliang Guan 已提交
73
  dTrace("msg:%p, put into worker %s", pMsg, pWorker->name);
S
Shengliang Guan 已提交
74
  taosWriteQitem(pWorker->queue, pMsg);
S
shm  
Shengliang Guan 已提交
75 76
}

S
Shengliang Guan 已提交
77 78 79 80
int32_t mmProcessWriteMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) {
  SMnodeMgmt *pMgmt = pWrapper->pMgmt;
  mmPutMsgToWorker(&pMgmt->writeWorker, pMsg);
  return 0;
S
shm  
Shengliang Guan 已提交
81 82
}

S
Shengliang Guan 已提交
83 84 85 86
int32_t mmProcessSyncMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) {
  SMnodeMgmt *pMgmt = pWrapper->pMgmt;
  mmPutMsgToWorker(&pMgmt->syncWorker, pMsg);
  return 0;
S
shm  
Shengliang Guan 已提交
87 88
}

S
Shengliang Guan 已提交
89 90 91 92
int32_t mmProcessReadMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) {
  SMnodeMgmt *pMgmt = pWrapper->pMgmt;
  mmPutMsgToWorker(&pMgmt->readWorker, pMsg);
  return 0;
S
shm  
Shengliang Guan 已提交
93
}
S
shm  
Shengliang Guan 已提交
94

S
Shengliang Guan 已提交
95 96 97 98
int32_t mmProcessQueryMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) {
  SMnodeMgmt *pMgmt = pWrapper->pMgmt;
  mmPutMsgToWorker(&pMgmt->queryWorker, pMsg);
  return 0;
D
dapan1121 已提交
99 100
}

S
Shengliang Guan 已提交
101
static int32_t mmPutRpcMsgToWorker(SSingleWorker *pWorker, SRpcMsg *pRpc) {
S
shm  
Shengliang Guan 已提交
102
  SNodeMsg *pMsg = taosAllocateQitem(sizeof(SNodeMsg));
S
Shengliang Guan 已提交
103
  if (pMsg == NULL) return -1;
S
shm  
Shengliang Guan 已提交
104

S
Shengliang Guan 已提交
105
  dTrace("msg:%p, is created and put into worker:%s, type:%s", pMsg, pWorker->name, TMSG_INFO(pRpc->msgType));
S
shm  
Shengliang Guan 已提交
106
  pMsg->rpcMsg = *pRpc;
S
Shengliang Guan 已提交
107 108 109
  taosWriteQitem(pWorker->queue, pMsg);
  return 0;
}
S
shm  
Shengliang Guan 已提交
110

S
Shengliang Guan 已提交
111 112 113
int32_t mmPutMsgToQueryQueue(SMgmtWrapper *pWrapper, SRpcMsg *pRpc) {
  SMnodeMgmt *pMgmt = pWrapper->pMgmt;
  return mmPutRpcMsgToWorker(&pMgmt->queryWorker, pRpc);
S
shm  
Shengliang Guan 已提交
114 115
}

S
shm  
Shengliang Guan 已提交
116 117
int32_t mmPutMsgToWriteQueue(SMgmtWrapper *pWrapper, SRpcMsg *pRpc) {
  SMnodeMgmt *pMgmt = pWrapper->pMgmt;
S
Shengliang Guan 已提交
118
  return mmPutRpcMsgToWorker(&pMgmt->writeWorker, pRpc);
S
shm  
Shengliang Guan 已提交
119
}
S
shm  
Shengliang Guan 已提交
120

S
shm  
Shengliang Guan 已提交
121 122
int32_t mmPutMsgToReadQueue(SMgmtWrapper *pWrapper, SRpcMsg *pRpc) {
  SMnodeMgmt *pMgmt = pWrapper->pMgmt;
S
Shengliang Guan 已提交
123
  return mmPutRpcMsgToWorker(&pMgmt->readWorker, pRpc);
S
shm  
Shengliang Guan 已提交
124
}
S
Shengliang Guan 已提交
125

S
Shengliang Guan 已提交
126
int32_t mmPutMsgToSyncQueue(SMgmtWrapper *pWrapper, SRpcMsg *pRpc) {
D
dapan1121 已提交
127
  SMnodeMgmt *pMgmt = pWrapper->pMgmt;
S
Shengliang Guan 已提交
128
  return mmPutRpcMsgToWorker(&pMgmt->syncWorker, pRpc);
D
dapan1121 已提交
129 130
}

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

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

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

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

S
Shengliang Guan 已提交
156
  dDebug("mnode workers are initialized");
S
Shengliang Guan 已提交
157 158 159 160
  return 0;
}

void mmStopWorker(SMnodeMgmt *pMgmt) {
D
dapan1121 已提交
161
  tSingleWorkerCleanup(&pMgmt->queryWorker);
S
Shengliang Guan 已提交
162
  tSingleWorkerCleanup(&pMgmt->readWorker);
S
Shengliang Guan 已提交
163 164
  tSingleWorkerCleanup(&pMgmt->writeWorker);
  tSingleWorkerCleanup(&pMgmt->syncWorker);
S
Shengliang Guan 已提交
165
  dDebug("mnode workers are closed");
S
Shengliang Guan 已提交
166
}