mmWorker.c 3.5 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
shm  
Shengliang Guan 已提交
19
static void mmProcessQueue(SMnodeMgmt *pMgmt, SNodeMsg *pMsg) {
S
shm  
Shengliang Guan 已提交
20
  dTrace("msg:%p, will be processed in mnode queue", pMsg);
S
shm  
Shengliang Guan 已提交
21 22 23
  SRpcMsg *pRpc = &pMsg->rpcMsg;
  int32_t  code = -1;

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

S
shm  
Shengliang Guan 已提交
31 32 33 34
  if (pRpc->msgType & 1U) {
    if (pRpc->handle == NULL) return;
    if (code != TSDB_CODE_MND_ACTION_IN_PROGRESS) {
      if (code != 0) code = terrno;
S
shm  
Shengliang Guan 已提交
35
      SRpcMsg rsp = {.handle = pRpc->handle, .contLen = pMsg->rspLen, .pCont = pMsg->pRsp};
S
shm  
Shengliang Guan 已提交
36
      dndSendRsp(pMgmt->pWrapper, &rsp);
S
shm  
Shengliang Guan 已提交
37 38 39 40 41 42 43 44 45
    }
  }

  dTrace("msg:%p, is freed", pMsg);
  rpcFreeCont(pRpc->pCont);
  taosFreeQitem(pMsg);
}

int32_t mmStartWorker(SMnodeMgmt *pMgmt) {
S
shm  
Shengliang Guan 已提交
46
  if (dndInitWorker(pMgmt, &pMgmt->readWorker, DND_WORKER_SINGLE, "mnode-read", 0, 1, mmProcessQueue) != 0) {
S
shm  
Shengliang Guan 已提交
47 48 49 50
    dError("failed to start mnode read worker since %s", terrstr());
    return -1;
  }

S
shm  
Shengliang Guan 已提交
51
  if (dndInitWorker(pMgmt, &pMgmt->writeWorker, DND_WORKER_SINGLE, "mnode-write", 0, 1, mmProcessQueue) != 0) {
S
shm  
Shengliang Guan 已提交
52 53 54 55
    dError("failed to start mnode write worker since %s", terrstr());
    return -1;
  }

S
shm  
Shengliang Guan 已提交
56
  if (dndInitWorker(pMgmt, &pMgmt->syncWorker, DND_WORKER_SINGLE, "mnode-sync", 0, 1, mmProcessQueue) != 0) {
S
shm  
Shengliang Guan 已提交
57 58 59 60 61 62 63
    dError("failed to start mnode sync worker since %s", terrstr());
    return -1;
  }

  return 0;
}

S
shm  
Shengliang Guan 已提交
64
void mmStopWorker(SMnodeMgmt *pMgmt) {
S
shm  
Shengliang Guan 已提交
65 66 67 68
  dndCleanupWorker(&pMgmt->readWorker);
  dndCleanupWorker(&pMgmt->writeWorker);
  dndCleanupWorker(&pMgmt->syncWorker);
}
S
shm  
Shengliang Guan 已提交
69

S
shm  
Shengliang Guan 已提交
70 71
static int32_t mmPutMsgToWorker(SMnodeMgmt *pMgmt, SDnodeWorker *pWorker, SNodeMsg *pMsg) {
  dTrace("msg:%p, put into worker %s", pMsg, pWorker->name);
S
shm  
Shengliang Guan 已提交
72
  return dndWriteMsgToWorker(pWorker, pMsg);
S
shm  
Shengliang Guan 已提交
73 74
}

S
shm  
Shengliang Guan 已提交
75
int32_t mmProcessWriteMsg(SMnodeMgmt *pMgmt, SNodeMsg *pMsg) {
S
shm  
Shengliang Guan 已提交
76
  return mmPutMsgToWorker(pMgmt, &pMgmt->writeWorker, pMsg);
S
shm  
Shengliang Guan 已提交
77 78
}

S
shm  
Shengliang Guan 已提交
79
int32_t mmProcessSyncMsg(SMnodeMgmt *pMgmt, SNodeMsg *pMsg) {
S
shm  
Shengliang Guan 已提交
80
  return mmPutMsgToWorker(pMgmt, &pMgmt->syncWorker, pMsg);
S
shm  
Shengliang Guan 已提交
81 82
}

S
shm  
Shengliang Guan 已提交
83
int32_t mmProcessReadMsg(SMnodeMgmt *pMgmt, SNodeMsg *pMsg) {
S
shm  
Shengliang Guan 已提交
84
  return mmPutMsgToWorker(pMgmt, &pMgmt->readWorker, pMsg);
S
shm  
Shengliang Guan 已提交
85
}
S
shm  
Shengliang Guan 已提交
86

S
shm  
Shengliang Guan 已提交
87
static int32_t mmPutRpcMsgToWorker(SMnodeMgmt *pMgmt, SDnodeWorker *pWorker, SRpcMsg *pRpc) {
S
shm  
Shengliang Guan 已提交
88
  SNodeMsg *pMsg = taosAllocateQitem(sizeof(SNodeMsg));
S
shm  
Shengliang Guan 已提交
89
  if (pMsg == NULL) {
S
shm  
Shengliang Guan 已提交
90 91 92
    return -1;
  }

S
shm  
Shengliang Guan 已提交
93
  dTrace("msg:%p, is created", pMsg);
S
shm  
Shengliang Guan 已提交
94
  pMsg->rpcMsg = *pRpc;
S
shm  
Shengliang Guan 已提交
95

S
shm  
Shengliang Guan 已提交
96
  int32_t code = mmPutMsgToWorker(pMgmt, pWorker, pMsg);
S
shm  
Shengliang Guan 已提交
97
  if (code != 0) {
S
shm  
Shengliang Guan 已提交
98
    dTrace("msg:%p, is freed", pMsg);
S
shm  
Shengliang Guan 已提交
99
    taosFreeQitem(pMsg);
S
shm  
Shengliang Guan 已提交
100
    rpcFreeCont(pRpc->pCont);
S
shm  
Shengliang Guan 已提交
101 102 103 104 105
  }

  return code;
}

S
shm  
Shengliang Guan 已提交
106 107 108
int32_t mmPutMsgToWriteQueue(SMgmtWrapper *pWrapper, SRpcMsg *pRpc) {
  SMnodeMgmt *pMgmt = pWrapper->pMgmt;
  return mmPutRpcMsgToWorker(pMgmt, &pMgmt->writeWorker, pRpc);
S
shm  
Shengliang Guan 已提交
109
}
S
shm  
Shengliang Guan 已提交
110

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