smWorker.c 3.7 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 "smInt.h"
S
Shengliang Guan 已提交
18

S
shm  
Shengliang Guan 已提交
19 20 21 22
static void smProcessUniqueQueue(SSnodeMgmt *pMgmt, STaosQall *qall, int32_t numOfMsgs) {
  for (int32_t i = 0; i < numOfMsgs; i++) {
    SNodeMsg *pMsg = NULL;
    taosGetQitem(qall, (void **)&pMsg);
L
Liu Jicong 已提交
23

S
shm  
Shengliang Guan 已提交
24 25
    dTrace("msg:%p, will be processed in snode unique queue", pMsg);
    sndProcessUMsg(pMgmt->pSnode, &pMsg->rpcMsg);
L
Liu Jicong 已提交
26

S
shm  
Shengliang Guan 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40
    dTrace("msg:%p, is freed", pMsg);
    rpcFreeCont(pMsg->rpcMsg.pCont);
    taosFreeQitem(pMsg);
  }
}

static void smProcessSharedQueue(SSnodeMgmt *pMgmt, SRpcMsg *pMsg) {
  dTrace("msg:%p, will be processed in snode shared queue", pMsg);
  sndProcessSMsg(pMgmt->pSnode, pMsg);

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

S
shm  
Shengliang Guan 已提交
42
int32_t smStartWorker(SSnodeMgmt *pMgmt) {
L
Liu Jicong 已提交
43
  pMgmt->uniqueWorkers = taosArrayInit(0, sizeof(void *));
S
shm  
Shengliang Guan 已提交
44 45 46 47 48
  if (pMgmt->uniqueWorkers == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

L
Liu Jicong 已提交
49
  for (int32_t i = 0; i < SND_UNIQUE_THREAD_NUM; i++) {
L
Liu Jicong 已提交
50 51
    SDnodeWorker *pUniqueWorker = malloc(sizeof(SDnodeWorker));
    if (pUniqueWorker == NULL) {
S
shm  
Shengliang Guan 已提交
52
      terrno = TSDB_CODE_OUT_OF_MEMORY;
L
Liu Jicong 已提交
53 54
      return -1;
    }
S
shm  
Shengliang Guan 已提交
55
    if (dndInitWorker(pMgmt, pUniqueWorker, DND_WORKER_MULTI, "snode-unique", 1, 1, smProcessSharedQueue) != 0) {
L
Liu Jicong 已提交
56 57 58
      dError("failed to start snode unique worker since %s", terrstr());
      return -1;
    }
S
shm  
Shengliang Guan 已提交
59 60 61 62
    if (taosArrayPush(pMgmt->uniqueWorkers, &pUniqueWorker) == NULL) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
L
Liu Jicong 已提交
63
  }
S
shm  
Shengliang Guan 已提交
64 65 66

  if (dndInitWorker(pMgmt, &pMgmt->sharedWorker, DND_WORKER_SINGLE, "snode-shared", SND_SHARED_THREAD_NUM,
                    SND_SHARED_THREAD_NUM, smProcessSharedQueue)) {
L
Liu Jicong 已提交
67
    dError("failed to start snode shared worker since %s", terrstr());
S
Shengliang Guan 已提交
68 69 70 71 72 73
    return -1;
  }

  return 0;
}

S
shm  
Shengliang Guan 已提交
74
void smStopWorker(SSnodeMgmt *pMgmt) {
L
Liu Jicong 已提交
75 76 77 78 79
  for (int32_t i = 0; i < taosArrayGetSize(pMgmt->uniqueWorkers); i++) {
    SDnodeWorker *worker = taosArrayGetP(pMgmt->uniqueWorkers, i);
    dndCleanupWorker(worker);
  }
  taosArrayDestroy(pMgmt->uniqueWorkers);
S
shm  
Shengliang Guan 已提交
80
  dndCleanupWorker(&pMgmt->sharedWorker);
S
Shengliang Guan 已提交
81 82
}

S
shm  
Shengliang Guan 已提交
83
static FORCE_INLINE int32_t smGetSWIdFromMsg(SRpcMsg *pMsg) {
L
Liu Jicong 已提交
84 85 86 87 88
  SMsgHead *pHead = pMsg->pCont;
  pHead->streamTaskId = htonl(pHead->streamTaskId);
  return pHead->streamTaskId % SND_UNIQUE_THREAD_NUM;
}

S
shm  
Shengliang Guan 已提交
89 90 91 92 93
int32_t smProcessMgmtMsg(SSnodeMgmt *pMgmt, SNodeMsg *pMsg) {
  SDnodeWorker *pWorker = taosArrayGetP(pMgmt->uniqueWorkers, 0);
  if (pWorker == NULL) {
    terrno = TSDB_CODE_INVALID_MSG;
    return -1;
L
Liu Jicong 已提交
94 95
  }

S
shm  
Shengliang Guan 已提交
96 97
  dTrace("msg:%p, put into worker:%s", pMsg, pWorker->name);
  return dndWriteMsgToWorker(pWorker, pMsg);
L
Liu Jicong 已提交
98 99
}

S
shm  
Shengliang Guan 已提交
100 101 102 103 104 105
int32_t smProcessUniqueMsg(SSnodeMgmt *pMgmt, SNodeMsg *pMsg) {
  int32_t       index = smGetSWIdFromMsg(&pMsg->rpcMsg);
  SDnodeWorker *pWorker = taosArrayGetP(pMgmt->uniqueWorkers, index);
  if (pWorker == NULL) {
    terrno = TSDB_CODE_INVALID_MSG;
    return -1;
S
Shengliang Guan 已提交
106 107
  }

S
shm  
Shengliang Guan 已提交
108 109
  dTrace("msg:%p, put into worker:%s", pMsg, pWorker->name);
  return dndWriteMsgToWorker(pWorker, pMsg);
L
Liu Jicong 已提交
110 111
}

S
shm  
Shengliang Guan 已提交
112 113
int32_t smProcessSharedMsg(SSnodeMgmt *pMgmt, SNodeMsg *pMsg) {
  SDnodeWorker *pWorker = &pMgmt->sharedWorker;
L
Liu Jicong 已提交
114

S
shm  
Shengliang Guan 已提交
115 116
  dTrace("msg:%p, put into worker:%s", pMsg, pWorker->name);
  return dndWriteMsgToWorker(pWorker, pMsg);
S
Shengliang Guan 已提交
117
}