mmWorker.c 4.0 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 24 25 26 27
  SMnode  *pMnode = mmAcquire(pMgmt);
  SRpcMsg *pRpc = &pMsg->rpcMsg;
  bool     isReq = (pRpc->msgType & 1U);
  int32_t  code = -1;

  if (pMnode != NULL) {
    pMsg->pNode = pMnode;
S
shm  
Shengliang Guan 已提交
28
    code = mndProcessMsg(pMsg);
S
shm  
Shengliang Guan 已提交
29 30 31 32 33 34 35
    mmRelease(pMgmt, pMnode);
  }

  if (isReq) {
    if (pMsg->rpcMsg.handle == NULL) return;
    if (code == 0) {
      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
    } else {
      if (terrno != TSDB_CODE_MND_ACTION_IN_PROGRESS) {
        SRpcMsg rsp = {.handle = pRpc->handle, .contLen = pMsg->rspLen, .pCont = pMsg->pRsp, .code = terrno};
S
shm  
Shengliang Guan 已提交
40
        dndSendRsp(pMgmt->pWrapper, &rsp);
S
shm  
Shengliang Guan 已提交
41 42 43 44 45 46 47 48 49 50
      }
    }
  }

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

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

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

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

  return 0;
}

S
shm  
Shengliang Guan 已提交
69
void mmStopWorker(SMnodeMgmt *pMgmt) {
S
shm  
Shengliang Guan 已提交
70 71 72 73 74 75 76 77 78 79 80 81
  taosWLockLatch(&pMgmt->latch);
  pMgmt->deployed = 0;
  taosWUnLockLatch(&pMgmt->latch);

  while (pMgmt->refCount > 1) {
    taosMsleep(10);
  }

  dndCleanupWorker(&pMgmt->readWorker);
  dndCleanupWorker(&pMgmt->writeWorker);
  dndCleanupWorker(&pMgmt->syncWorker);
}
S
shm  
Shengliang Guan 已提交
82

S
shm  
Shengliang Guan 已提交
83 84 85
static int32_t mmPutMsgToWorker(SMnodeMgmt *pMgmt, SDnodeWorker *pWorker, SNodeMsg *pMsg) {
  SMnode *pMnode = mmAcquire(pMgmt);
  if (pMnode == NULL) return -1;
S
shm  
Shengliang Guan 已提交
86

S
shm  
Shengliang Guan 已提交
87
  dTrace("msg:%p, put into worker %s", pMsg, pWorker->name);
S
shm  
Shengliang Guan 已提交
88
  int32_t code = dndWriteMsgToWorker(pWorker, pMsg);
S
shm  
Shengliang Guan 已提交
89 90
  mmRelease(pMgmt, pMnode);
  return code;
S
shm  
Shengliang Guan 已提交
91 92
}

S
shm  
Shengliang Guan 已提交
93
int32_t mmProcessWriteMsg(SMnodeMgmt *pMgmt, SNodeMsg *pMsg) {
S
shm  
Shengliang Guan 已提交
94
  return mmPutMsgToWorker(pMgmt, &pMgmt->writeWorker, pMsg);
S
shm  
Shengliang Guan 已提交
95 96
}

S
shm  
Shengliang Guan 已提交
97
int32_t mmProcessSyncMsg(SMnodeMgmt *pMgmt, SNodeMsg *pMsg) {
S
shm  
Shengliang Guan 已提交
98
  return mmPutMsgToWorker(pMgmt, &pMgmt->syncWorker, pMsg);
S
shm  
Shengliang Guan 已提交
99 100
}

S
shm  
Shengliang Guan 已提交
101
int32_t mmProcessReadMsg(SMnodeMgmt *pMgmt, SNodeMsg *pMsg) {
S
shm  
Shengliang Guan 已提交
102
  return mmPutMsgToWorker(pMgmt, &pMgmt->readWorker, pMsg);
S
shm  
Shengliang Guan 已提交
103
}
S
shm  
Shengliang Guan 已提交
104

S
shm  
Shengliang Guan 已提交
105
static int32_t mmPutRpcMsgToWorker(SMnodeMgmt *pMgmt, SDnodeWorker *pWorker, SRpcMsg *pRpc) {
S
shm  
Shengliang Guan 已提交
106
  SNodeMsg *pMsg = taosAllocateQitem(sizeof(SNodeMsg));
S
shm  
Shengliang Guan 已提交
107
  if (pMsg == NULL) {
S
shm  
Shengliang Guan 已提交
108 109 110
    return -1;
  }

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

S
shm  
Shengliang Guan 已提交
114
  int32_t code = mmPutMsgToWorker(pMgmt, pWorker, pMsg);
S
shm  
Shengliang Guan 已提交
115
  if (code != 0) {
S
shm  
Shengliang Guan 已提交
116
    dTrace("msg:%p, is freed", pMsg);
S
shm  
Shengliang Guan 已提交
117
    taosFreeQitem(pMsg);
S
shm  
Shengliang Guan 已提交
118
    rpcFreeCont(pRpc->pCont);
S
shm  
Shengliang Guan 已提交
119 120 121 122 123
  }

  return code;
}

S
shm  
Shengliang Guan 已提交
124 125 126
int32_t mmPutMsgToWriteQueue(SMgmtWrapper *pWrapper, SRpcMsg *pRpc) {
  SMnodeMgmt *pMgmt = pWrapper->pMgmt;
  return mmPutRpcMsgToWorker(pMgmt, &pMgmt->writeWorker, pRpc);
S
shm  
Shengliang Guan 已提交
127
}
S
shm  
Shengliang Guan 已提交
128

S
shm  
Shengliang Guan 已提交
129 130 131
int32_t mmPutMsgToReadQueue(SMgmtWrapper *pWrapper, SRpcMsg *pRpc) {
  SMnodeMgmt *pMgmt = pWrapper->pMgmt;
  return mmPutRpcMsgToWorker(pMgmt, &pMgmt->readWorker, pRpc);
S
shm  
Shengliang Guan 已提交
132
}