bmWorker.c 4.5 KB
Newer Older
S
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 "bmInt.h"
S
Shengliang Guan 已提交
18

19
static void bmSendErrorRsp(SNodeMsg *pMsg, int32_t code) {
dengyihao's avatar
dengyihao 已提交
20 21
  SRpcMsg rpcRsp = {
      .handle = pMsg->rpcMsg.handle, .ahandle = pMsg->rpcMsg.ahandle, .code = code, .refId = pMsg->rpcMsg.refId};
S
Shengliang Guan 已提交
22
  tmsgSendRsp(&rpcRsp);
S
shm  
Shengliang Guan 已提交
23 24

  dTrace("msg:%p, is freed", pMsg);
S
shm  
Shengliang Guan 已提交
25
  rpcFreeCont(pMsg->rpcMsg.pCont);
S
Shengliang Guan 已提交
26 27 28
  taosFreeQitem(pMsg);
}

29
static void bmSendErrorRsps(STaosQall *qall, int32_t numOfMsgs, int32_t code) {
S
Shengliang Guan 已提交
30
  for (int32_t i = 0; i < numOfMsgs; ++i) {
S
shm  
Shengliang Guan 已提交
31
    SNodeMsg *pMsg = NULL;
S
Shengliang Guan 已提交
32
    taosGetQitem(qall, (void **)&pMsg);
33 34 35
    if (pMsg != NULL) {
      bmSendErrorRsp(pMsg, code);
    }
S
Shengliang Guan 已提交
36 37 38
  }
}

39
static inline void bmSendRsp(SNodeMsg *pMsg, int32_t code) {
40 41
  SRpcMsg rsp = {.handle = pMsg->rpcMsg.handle,
                 .ahandle = pMsg->rpcMsg.ahandle,
dengyihao's avatar
dengyihao 已提交
42
                 .refId = pMsg->rpcMsg.refId,
43 44 45 46 47 48
                 .code = code,
                 .pCont = pMsg->pRsp,
                 .contLen = pMsg->rspLen};
  tmsgSendRsp(&rsp);
}

49
static void bmProcessMonitorQueue(SQueueInfo *pInfo, SNodeMsg *pMsg) {
50 51
  SBnodeMgmt *pMgmt = pInfo->ahandle;

52
  dTrace("msg:%p, get from bnode-monitor queue", pMsg);
53 54 55 56 57
  SRpcMsg *pRpc = &pMsg->rpcMsg;
  int32_t  code = -1;

  if (pMsg->rpcMsg.msgType == TDMT_MON_BM_INFO) {
    code = bmProcessGetMonBmInfoReq(pMgmt->pWrapper, pMsg);
58 59
  } else {
    terrno = TSDB_CODE_MSG_NOT_PROCESSED;
60 61 62
  }

  if (pRpc->msgType & 1U) {
63
    if (code != 0 && terrno != 0) code = terrno;
64
    bmSendRsp(pMsg, code);
65 66 67 68 69 70 71 72
  }

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

static void bmProcessWriteQueue(SQueueInfo *pInfo, STaosQall *qall, int32_t numOfMsgs) {
73
  SBnodeMgmt *pMgmt = pInfo->ahandle;
S
shm  
Shengliang Guan 已提交
74 75

  SArray *pArray = taosArrayInit(numOfMsgs, sizeof(SNodeMsg *));
S
Shengliang Guan 已提交
76
  if (pArray == NULL) {
77
    bmSendErrorRsps(qall, numOfMsgs, TSDB_CODE_OUT_OF_MEMORY);
S
Shengliang Guan 已提交
78 79 80 81
    return;
  }

  for (int32_t i = 0; i < numOfMsgs; ++i) {
S
shm  
Shengliang Guan 已提交
82
    SNodeMsg *pMsg = NULL;
S
Shengliang Guan 已提交
83
    taosGetQitem(qall, (void **)&pMsg);
84 85 86 87 88
    if (pMsg != NULL) {
      dTrace("msg:%p, get from bnode-write queue", pMsg);
      if (taosArrayPush(pArray, &pMsg) == NULL) {
        bmSendErrorRsp(pMsg, TSDB_CODE_OUT_OF_MEMORY);
      }
S
Shengliang Guan 已提交
89 90 91
    }
  }

S
shm  
Shengliang Guan 已提交
92
  bndProcessWMsgs(pMgmt->pBnode, pArray);
S
Shengliang Guan 已提交
93 94

  for (size_t i = 0; i < numOfMsgs; i++) {
S
shm  
Shengliang Guan 已提交
95
    SNodeMsg *pMsg = *(SNodeMsg **)taosArrayGet(pArray, i);
96 97 98 99 100
    if (pMsg != NULL) {
      dTrace("msg:%p, is freed", pMsg);
      rpcFreeCont(pMsg->rpcMsg.pCont);
      taosFreeQitem(pMsg);
    }
S
Shengliang Guan 已提交
101 102 103 104
  }
  taosArrayDestroy(pArray);
}

S
Shengliang Guan 已提交
105
int32_t bmProcessWriteMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) {
dengyihao's avatar
dengyihao 已提交
106
  SBnodeMgmt *  pMgmt = pWrapper->pMgmt;
S
Shengliang Guan 已提交
107
  SMultiWorker *pWorker = &pMgmt->writeWorker;
S
Shengliang Guan 已提交
108

S
shm  
Shengliang Guan 已提交
109
  dTrace("msg:%p, put into worker:%s", pMsg, pWorker->name);
S
Shengliang Guan 已提交
110 111
  taosWriteQitem(pWorker->queue, pMsg);
  return 0;
S
Shengliang Guan 已提交
112
}
S
shm  
Shengliang Guan 已提交
113

114
int32_t bmProcessMonitorMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) {
dengyihao's avatar
dengyihao 已提交
115
  SBnodeMgmt *   pMgmt = pWrapper->pMgmt;
116 117 118 119 120 121 122
  SSingleWorker *pWorker = &pMgmt->monitorWorker;

  dTrace("msg:%p, put into worker:%s", pMsg, pWorker->name);
  taosWriteQitem(pWorker->queue, pMsg);
  return 0;
}

S
shm  
Shengliang Guan 已提交
123
int32_t bmStartWorker(SBnodeMgmt *pMgmt) {
124
  SMultiWorkerCfg cfg = {.max = 1, .name = "bnode-write", .fp = (FItems)bmProcessWriteQueue, .param = pMgmt};
S
Shengliang Guan 已提交
125
  if (tMultiWorkerInit(&pMgmt->writeWorker, &cfg) != 0) {
126
    dError("failed to start bnode-write worker since %s", terrstr());
S
shm  
Shengliang Guan 已提交
127 128 129
    return -1;
  }

130
  if (tsMultiProcess) {
131
    SSingleWorkerCfg mCfg = {
132
        .min = 1, .max = 1, .name = "bnode-monitor", .fp = (FItem)bmProcessMonitorQueue, .param = pMgmt};
133
    if (tSingleWorkerInit(&pMgmt->monitorWorker, &mCfg) != 0) {
134 135 136
      dError("failed to start bnode-monitor worker since %s", terrstr());
      return -1;
    }
137
  }
138

S
Shengliang Guan 已提交
139
  dDebug("bnode workers are initialized");
S
shm  
Shengliang Guan 已提交
140 141 142
  return 0;
}

S
Shengliang Guan 已提交
143
void bmStopWorker(SBnodeMgmt *pMgmt) {
144
  tSingleWorkerCleanup(&pMgmt->monitorWorker);
S
Shengliang Guan 已提交
145 146 147
  tMultiWorkerCleanup(&pMgmt->writeWorker);
  dDebug("bnode workers are closed");
}