bmWorker.c 4.4 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

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

  dTrace("msg:%p, is freed", pMsg);
S
Shengliang Guan 已提交
29
  rpcFreeCont(pMsg->pCont);
S
Shengliang Guan 已提交
30 31 32
  taosFreeQitem(pMsg);
}

33
static void bmSendErrorRsps(STaosQall *qall, int32_t numOfMsgs, int32_t code) {
S
Shengliang Guan 已提交
34
  for (int32_t i = 0; i < numOfMsgs; ++i) {
S
Shengliang Guan 已提交
35
    SRpcMsg *pMsg = NULL;
S
Shengliang Guan 已提交
36
    taosGetQitem(qall, (void **)&pMsg);
37 38 39
    if (pMsg != NULL) {
      bmSendErrorRsp(pMsg, code);
    }
S
Shengliang Guan 已提交
40 41 42
  }
}

S
Shengliang Guan 已提交
43 44 45 46
static inline void bmSendRsp(SRpcMsg *pMsg, int32_t code) {
  SRpcMsg rsp = {.info.handle = pMsg->info.handle,
                 .info.ahandle = pMsg->info.ahandle,
                 .info.refId = pMsg->info.refId,
47
                 .code = code,
S
Shengliang Guan 已提交
48 49
                 .pCont = pMsg->info.rsp,
                 .contLen = pMsg->info.rspLen,};
50 51 52
  tmsgSendRsp(&rsp);
}

S
Shengliang Guan 已提交
53
static void bmProcessMonitorQueue(SQueueInfo *pInfo, SRpcMsg *pMsg) {
54 55
  SBnodeMgmt *pMgmt = pInfo->ahandle;

56
  dTrace("msg:%p, get from bnode-monitor queue", pMsg);
S
Shengliang Guan 已提交
57
  SRpcMsg *pRpc = pMsg;
58 59
  int32_t  code = -1;

S
Shengliang Guan 已提交
60
  if (pMsg->msgType == TDMT_MON_BM_INFO) {
S
Shengliang 已提交
61
    code = bmProcessGetMonBmInfoReq(pMgmt, pMsg);
62 63
  } else {
    terrno = TSDB_CODE_MSG_NOT_PROCESSED;
64 65 66
  }

  if (pRpc->msgType & 1U) {
67
    if (code != 0 && terrno != 0) code = terrno;
68
    bmSendRsp(pMsg, code);
69 70 71 72 73 74 75 76
  }

  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) {
77
  SBnodeMgmt *pMgmt = pInfo->ahandle;
S
shm  
Shengliang Guan 已提交
78

S
Shengliang Guan 已提交
79
  SArray *pArray = taosArrayInit(numOfMsgs, sizeof(SRpcMsg *));
S
Shengliang Guan 已提交
80
  if (pArray == NULL) {
81
    bmSendErrorRsps(qall, numOfMsgs, TSDB_CODE_OUT_OF_MEMORY);
S
Shengliang Guan 已提交
82 83 84 85
    return;
  }

  for (int32_t i = 0; i < numOfMsgs; ++i) {
S
Shengliang Guan 已提交
86
    SRpcMsg *pMsg = NULL;
S
Shengliang Guan 已提交
87
    taosGetQitem(qall, (void **)&pMsg);
88 89 90 91 92
    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 已提交
93 94 95
    }
  }

S
shm  
Shengliang Guan 已提交
96
  bndProcessWMsgs(pMgmt->pBnode, pArray);
S
Shengliang Guan 已提交
97 98

  for (size_t i = 0; i < numOfMsgs; i++) {
S
Shengliang Guan 已提交
99
    SRpcMsg *pMsg = *(SRpcMsg **)taosArrayGet(pArray, i);
100 101
    if (pMsg != NULL) {
      dTrace("msg:%p, is freed", pMsg);
S
Shengliang Guan 已提交
102
      rpcFreeCont(pMsg->pCont);
103 104
      taosFreeQitem(pMsg);
    }
S
Shengliang Guan 已提交
105 106 107 108
  }
  taosArrayDestroy(pArray);
}

S
Shengliang Guan 已提交
109
int32_t bmPutNodeMsgToWriteQueue(SBnodeMgmt *pMgmt, SRpcMsg *pMsg) {
S
Shengliang Guan 已提交
110
  SMultiWorker *pWorker = &pMgmt->writeWorker;
S
Shengliang Guan 已提交
111

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

S
Shengliang Guan 已提交
117
int32_t bmPutNodeMsgToMonitorQueue(SBnodeMgmt *pMgmt, SRpcMsg *pMsg) {
118 119 120 121 122 123 124
  SSingleWorker *pWorker = &pMgmt->monitorWorker;

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

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

S
Shengliang Guan 已提交
137 138 139 140 141 142 143 144 145 146
  SSingleWorkerCfg mCfg = {
      .min = 1,
      .max = 1,
      .name = "bnode-monitor",
      .fp = (FItem)bmProcessMonitorQueue,
      .param = pMgmt,
  };
  if (tSingleWorkerInit(&pMgmt->monitorWorker, &mCfg) != 0) {
    dError("failed to start bnode-monitor worker since %s", terrstr());
    return -1;
147
  }
148

S
Shengliang Guan 已提交
149
  dDebug("bnode workers are initialized");
S
shm  
Shengliang Guan 已提交
150 151 152
  return 0;
}

S
Shengliang Guan 已提交
153
void bmStopWorker(SBnodeMgmt *pMgmt) {
154
  tSingleWorkerCleanup(&pMgmt->monitorWorker);
S
Shengliang Guan 已提交
155 156 157
  tMultiWorkerCleanup(&pMgmt->writeWorker);
  dDebug("bnode workers are closed");
}