mmWorker.c 3.7 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, .code = code, .contLen = pMsg->rspLen, .pCont = pMsg->pRsp};
S
shm  
Shengliang Guan 已提交
36
      dndSendRsp(pMgmt->pWrapper, &rsp);
S
shm  
Shengliang Guan 已提交
37 38 39
    }
  }

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

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

S
shm  
Shengliang Guan 已提交
50
int32_t mmProcessWriteMsg(SMnodeMgmt *pMgmt, SNodeMsg *pMsg) {
S
shm  
Shengliang Guan 已提交
51
  return mmPutMsgToWorker(pMgmt, &pMgmt->writeWorker, pMsg);
S
shm  
Shengliang Guan 已提交
52 53
}

S
shm  
Shengliang Guan 已提交
54
int32_t mmProcessSyncMsg(SMnodeMgmt *pMgmt, SNodeMsg *pMsg) {
S
shm  
Shengliang Guan 已提交
55
  return mmPutMsgToWorker(pMgmt, &pMgmt->syncWorker, pMsg);
S
shm  
Shengliang Guan 已提交
56 57
}

S
shm  
Shengliang Guan 已提交
58
int32_t mmProcessReadMsg(SMnodeMgmt *pMgmt, SNodeMsg *pMsg) {
S
shm  
Shengliang Guan 已提交
59
  return mmPutMsgToWorker(pMgmt, &pMgmt->readWorker, pMsg);
S
shm  
Shengliang Guan 已提交
60
}
S
shm  
Shengliang Guan 已提交
61

S
shm  
Shengliang Guan 已提交
62
static int32_t mmPutRpcMsgToWorker(SMnodeMgmt *pMgmt, SDnodeWorker *pWorker, SRpcMsg *pRpc) {
S
shm  
Shengliang Guan 已提交
63
  SNodeMsg *pMsg = taosAllocateQitem(sizeof(SNodeMsg));
S
shm  
Shengliang Guan 已提交
64
  if (pMsg == NULL) {
S
shm  
Shengliang Guan 已提交
65 66 67
    return -1;
  }

S
Shengliang Guan 已提交
68
  dTrace("msg:%p, is created and put into worker:%s, type:%s", pMsg, pWorker->name, TMSG_INFO(pRpc->msgType));
S
shm  
Shengliang Guan 已提交
69
  pMsg->rpcMsg = *pRpc;
S
shm  
Shengliang Guan 已提交
70

S
Shengliang Guan 已提交
71
  int32_t code = dndWriteMsgToWorker(pWorker, pMsg);
S
shm  
Shengliang Guan 已提交
72
  if (code != 0) {
S
shm  
Shengliang Guan 已提交
73
    dTrace("msg:%p, is freed", pMsg);
S
shm  
Shengliang Guan 已提交
74
    taosFreeQitem(pMsg);
S
shm  
Shengliang Guan 已提交
75
    rpcFreeCont(pRpc->pCont);
S
shm  
Shengliang Guan 已提交
76 77 78 79 80
  }

  return code;
}

S
shm  
Shengliang Guan 已提交
81 82 83
int32_t mmPutMsgToWriteQueue(SMgmtWrapper *pWrapper, SRpcMsg *pRpc) {
  SMnodeMgmt *pMgmt = pWrapper->pMgmt;
  return mmPutRpcMsgToWorker(pMgmt, &pMgmt->writeWorker, pRpc);
S
shm  
Shengliang Guan 已提交
84
}
S
shm  
Shengliang Guan 已提交
85

S
shm  
Shengliang Guan 已提交
86 87 88
int32_t mmPutMsgToReadQueue(SMgmtWrapper *pWrapper, SRpcMsg *pRpc) {
  SMnodeMgmt *pMgmt = pWrapper->pMgmt;
  return mmPutRpcMsgToWorker(pMgmt, &pMgmt->readWorker, pRpc);
S
shm  
Shengliang Guan 已提交
89
}
S
Shengliang Guan 已提交
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114

int32_t mmStartWorker(SMnodeMgmt *pMgmt) {
  if (dndInitWorker(pMgmt, &pMgmt->readWorker, DND_WORKER_SINGLE, "mnode-read", 0, 1, mmProcessQueue) != 0) {
    dError("failed to start mnode read worker since %s", terrstr());
    return -1;
  }

  if (dndInitWorker(pMgmt, &pMgmt->writeWorker, DND_WORKER_SINGLE, "mnode-write", 0, 1, mmProcessQueue) != 0) {
    dError("failed to start mnode write worker since %s", terrstr());
    return -1;
  }

  if (dndInitWorker(pMgmt, &pMgmt->syncWorker, DND_WORKER_SINGLE, "mnode-sync", 0, 1, mmProcessQueue) != 0) {
    dError("failed to start mnode sync worker since %s", terrstr());
    return -1;
  }

  return 0;
}

void mmStopWorker(SMnodeMgmt *pMgmt) {
  dndCleanupWorker(&pMgmt->readWorker);
  dndCleanupWorker(&pMgmt->writeWorker);
  dndCleanupWorker(&pMgmt->syncWorker);
}