bmWorker.c 2.8 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
shm  
Shengliang Guan 已提交
19 20
static void bmSendErrorRsp(SMgmtWrapper *pWrapper, SNodeMsg *pMsg, int32_t code) {
  SRpcMsg rpcRsp = {.handle = pMsg->rpcMsg.handle, .ahandle = pMsg->rpcMsg.ahandle, .code = code};
S
Shengliang Guan 已提交
21
  tmsgSendRsp(&rpcRsp);
S
shm  
Shengliang Guan 已提交
22 23

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

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

S
Shengliang Guan 已提交
36 37
static void bmProcessQueue(SQueueInfo *pInfo, STaosQall *qall, int32_t numOfMsgs) {
  SBnodeMgmt   *pMgmt = pInfo->ahandle;
S
shm  
Shengliang Guan 已提交
38 39 40
  SMgmtWrapper *pWrapper = pMgmt->pWrapper;

  SArray *pArray = taosArrayInit(numOfMsgs, sizeof(SNodeMsg *));
S
Shengliang Guan 已提交
41
  if (pArray == NULL) {
S
shm  
Shengliang Guan 已提交
42
    bmSendErrorRsps(pWrapper, qall, numOfMsgs, TSDB_CODE_OUT_OF_MEMORY);
S
Shengliang Guan 已提交
43 44 45 46
    return;
  }

  for (int32_t i = 0; i < numOfMsgs; ++i) {
S
shm  
Shengliang Guan 已提交
47
    SNodeMsg *pMsg = NULL;
S
Shengliang Guan 已提交
48
    taosGetQitem(qall, (void **)&pMsg);
S
shm  
Shengliang Guan 已提交
49
    dTrace("msg:%p, will be processed in bnode queue", pMsg);
S
shm  
Shengliang Guan 已提交
50
    if (taosArrayPush(pArray, &pMsg) == NULL) {
S
shm  
Shengliang Guan 已提交
51
      bmSendErrorRsp(pWrapper, pMsg, TSDB_CODE_OUT_OF_MEMORY);
S
Shengliang Guan 已提交
52 53 54
    }
  }

S
shm  
Shengliang Guan 已提交
55
  bndProcessWMsgs(pMgmt->pBnode, pArray);
S
Shengliang Guan 已提交
56 57

  for (size_t i = 0; i < numOfMsgs; i++) {
S
shm  
Shengliang Guan 已提交
58 59 60 61
    SNodeMsg *pMsg = *(SNodeMsg **)taosArrayGet(pArray, i);
    dTrace("msg:%p, is freed", pMsg);
    rpcFreeCont(pMsg->rpcMsg.pCont);
    taosFreeQitem(pMsg);
S
Shengliang Guan 已提交
62 63 64 65
  }
  taosArrayDestroy(pArray);
}

S
Shengliang Guan 已提交
66 67
int32_t bmProcessWriteMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) {
  SBnodeMgmt   *pMgmt = pWrapper->pMgmt;
S
Shengliang Guan 已提交
68
  SMultiWorker *pWorker = &pMgmt->writeWorker;
S
Shengliang Guan 已提交
69

S
shm  
Shengliang Guan 已提交
70
  dTrace("msg:%p, put into worker:%s", pMsg, pWorker->name);
S
Shengliang Guan 已提交
71 72
  taosWriteQitem(pWorker->queue, pMsg);
  return 0;
S
Shengliang Guan 已提交
73
}
S
shm  
Shengliang Guan 已提交
74

S
shm  
Shengliang Guan 已提交
75
int32_t bmStartWorker(SBnodeMgmt *pMgmt) {
S
shm  
Shengliang Guan 已提交
76
  SMultiWorkerCfg cfg = {.max = 1, .name = "bnode-write", .fp = (FItems)bmProcessQueue, .param = pMgmt};
S
Shengliang Guan 已提交
77
  if (tMultiWorkerInit(&pMgmt->writeWorker, &cfg) != 0) {
S
shm  
Shengliang Guan 已提交
78 79 80 81
    dError("failed to start bnode write worker since %s", terrstr());
    return -1;
  }

S
Shengliang Guan 已提交
82
  dDebug("bnode workers are initialized");
S
shm  
Shengliang Guan 已提交
83 84 85
  return 0;
}

S
Shengliang Guan 已提交
86 87 88 89
void bmStopWorker(SBnodeMgmt *pMgmt) {
  tMultiWorkerCleanup(&pMgmt->writeWorker);
  dDebug("bnode workers are closed");
}