bmWorker.c 4.1 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) {
S
Shengliang Guan 已提交
20 21
  SRpcMsg rsp = {.code = code, .info = pMsg->info};
  tmsgSendRsp(&rsp);
S
shm  
Shengliang Guan 已提交
22 23

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

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

S
Shengliang Guan 已提交
38
static inline void bmSendRsp(SRpcMsg *pMsg, int32_t code) {
S
Shengliang Guan 已提交
39 40 41 42
  SRpcMsg rsp = {
      .code = code,
      .pCont = pMsg->info.rsp,
      .contLen = pMsg->info.rspLen,
43
      .info = pMsg->info,
S
Shengliang Guan 已提交
44
  };
45 46 47
  tmsgSendRsp(&rsp);
}

S
Shengliang Guan 已提交
48
static void bmProcessMonitorQueue(SQueueInfo *pInfo, SRpcMsg *pMsg) {
49
  SBnodeMgmt *pMgmt = pInfo->ahandle;
S
Shengliang Guan 已提交
50
  int32_t     code = -1;
51
  dTrace("msg:%p, get from bnode-monitor queue", pMsg);
52

S
Shengliang Guan 已提交
53
  if (pMsg->msgType == TDMT_MON_BM_INFO) {
S
Shengliang 已提交
54
    code = bmProcessGetMonBmInfoReq(pMgmt, pMsg);
55 56
  } else {
    terrno = TSDB_CODE_MSG_NOT_PROCESSED;
57 58
  }

S
Shengliang Guan 已提交
59
  if (IsReq(pMsg)) {
60
    if (code != 0 && terrno != 0) code = terrno;
61
    bmSendRsp(pMsg, code);
62 63
  }

S
Shengliang Guan 已提交
64
  dTrace("msg:%p, is freed, code:0x%x", pMsg, code);
S
Shengliang Guan 已提交
65
  rpcFreeCont(pMsg->pCont);
66 67 68 69
  taosFreeQitem(pMsg);
}

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

S
Shengliang Guan 已提交
72
  SArray *pArray = taosArrayInit(numOfMsgs, sizeof(SRpcMsg *));
S
Shengliang Guan 已提交
73
  if (pArray == NULL) {
74
    bmSendErrorRsps(qall, numOfMsgs, TSDB_CODE_OUT_OF_MEMORY);
S
Shengliang Guan 已提交
75 76 77 78
    return;
  }

  for (int32_t i = 0; i < numOfMsgs; ++i) {
S
Shengliang Guan 已提交
79
    SRpcMsg *pMsg = NULL;
S
Shengliang Guan 已提交
80
    taosGetQitem(qall, (void **)&pMsg);
81 82 83 84 85
    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 已提交
86 87 88
    }
  }

S
shm  
Shengliang Guan 已提交
89
  bndProcessWMsgs(pMgmt->pBnode, pArray);
S
Shengliang Guan 已提交
90 91

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

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

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

S
Shengliang Guan 已提交
110
int32_t bmPutNodeMsgToMonitorQueue(SBnodeMgmt *pMgmt, SRpcMsg *pMsg) {
111 112 113 114 115 116 117
  SSingleWorker *pWorker = &pMgmt->monitorWorker;

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

S
shm  
Shengliang Guan 已提交
118
int32_t bmStartWorker(SBnodeMgmt *pMgmt) {
S
Shengliang Guan 已提交
119 120 121 122 123 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;
  }

S
Shengliang Guan 已提交
130 131 132 133 134 135 136 137 138 139
  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;
140
  }
141

S
Shengliang Guan 已提交
142
  dDebug("bnode workers are initialized");
S
shm  
Shengliang Guan 已提交
143 144 145
  return 0;
}

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