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

S
Shengliang Guan 已提交
19 20 21
static void smProcessUniqueQueue(SQueueInfo *pInfo, STaosQall *qall, int32_t numOfMsgs) {
  SSnodeMgmt *pMgmt = pInfo->ahandle;

S
shm  
Shengliang Guan 已提交
22 23 24
  for (int32_t i = 0; i < numOfMsgs; i++) {
    SNodeMsg *pMsg = NULL;
    taosGetQitem(qall, (void **)&pMsg);
L
Liu Jicong 已提交
25

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

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

S
Shengliang Guan 已提交
35 36 37
static void smProcessSharedQueue(SQueueInfo *pInfo, SNodeMsg *pMsg) {
  SSnodeMgmt *pMgmt = pInfo->ahandle;

S
shm  
Shengliang Guan 已提交
38
  dTrace("msg:%p, will be processed in snode shared queue", pMsg);
S
shm  
Shengliang Guan 已提交
39
  sndProcessSMsg(pMgmt->pSnode, &pMsg->rpcMsg);
S
shm  
Shengliang Guan 已提交
40 41

  dTrace("msg:%p, is freed", pMsg);
S
shm  
Shengliang Guan 已提交
42
  rpcFreeCont(pMsg->rpcMsg.pCont);
S
shm  
Shengliang Guan 已提交
43 44
  taosFreeQitem(pMsg);
}
S
Shengliang Guan 已提交
45

S
shm  
Shengliang Guan 已提交
46
int32_t smStartWorker(SSnodeMgmt *pMgmt) {
S
Shengliang Guan 已提交
47
  pMgmt->uniqueWorkers = taosArrayInit(0, sizeof(SMultiWorker *));
S
shm  
Shengliang Guan 已提交
48 49 50 51 52
  if (pMgmt->uniqueWorkers == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

L
Liu Jicong 已提交
53
  for (int32_t i = 0; i < SND_UNIQUE_THREAD_NUM; i++) {
S
Shengliang Guan 已提交
54
    SMultiWorker *pUniqueWorker = malloc(sizeof(SMultiWorker));
L
Liu Jicong 已提交
55
    if (pUniqueWorker == NULL) {
S
shm  
Shengliang Guan 已提交
56
      terrno = TSDB_CODE_OUT_OF_MEMORY;
L
Liu Jicong 已提交
57 58
      return -1;
    }
S
Shengliang Guan 已提交
59

S
Shengliang Guan 已提交
60
    SMultiWorkerCfg cfg = {.maxNum = 1, .name = "snode-unique", .fp = smProcessUniqueQueue, .param = pMgmt};
S
Shengliang Guan 已提交
61

S
Shengliang Guan 已提交
62
    if (tMultiWorkerInit(pUniqueWorker, &cfg) != 0) {
S
Shengliang Guan 已提交
63
      dError("failed to start snode-unique worker since %s", terrstr());
L
Liu Jicong 已提交
64 65
      return -1;
    }
S
shm  
Shengliang Guan 已提交
66 67 68 69
    if (taosArrayPush(pMgmt->uniqueWorkers, &pUniqueWorker) == NULL) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
L
Liu Jicong 已提交
70
  }
S
shm  
Shengliang Guan 已提交
71

S
Shengliang Guan 已提交
72 73 74 75 76
  SSingleWorkerCfg cfg = {.minNum = SND_SHARED_THREAD_NUM,
                          .maxNum = SND_SHARED_THREAD_NUM,
                          .name = "snode-shared",
                          .fp = (FItem)smProcessSharedQueue,
                          .param = pMgmt};
S
Shengliang Guan 已提交
77

S
Shengliang Guan 已提交
78
  if (tSingleWorkerInit(&pMgmt->sharedWorker, &cfg)) {
S
Shengliang Guan 已提交
79
    dError("failed to start snode shared-worker since %s", terrstr());
S
Shengliang Guan 已提交
80 81 82 83 84 85
    return -1;
  }

  return 0;
}

S
shm  
Shengliang Guan 已提交
86
void smStopWorker(SSnodeMgmt *pMgmt) {
L
Liu Jicong 已提交
87
  for (int32_t i = 0; i < taosArrayGetSize(pMgmt->uniqueWorkers); i++) {
S
Shengliang Guan 已提交
88 89
    SMultiWorker *pWorker = taosArrayGetP(pMgmt->uniqueWorkers, i);
    tMultiWorkerCleanup(pWorker);
L
Liu Jicong 已提交
90 91
  }
  taosArrayDestroy(pMgmt->uniqueWorkers);
S
Shengliang Guan 已提交
92
  tSingleWorkerCleanup(&pMgmt->sharedWorker);
S
Shengliang Guan 已提交
93 94
}

S
shm  
Shengliang Guan 已提交
95
static FORCE_INLINE int32_t smGetSWIdFromMsg(SRpcMsg *pMsg) {
L
Liu Jicong 已提交
96 97 98 99 100
  SMsgHead *pHead = pMsg->pCont;
  pHead->streamTaskId = htonl(pHead->streamTaskId);
  return pHead->streamTaskId % SND_UNIQUE_THREAD_NUM;
}

S
shm  
Shengliang Guan 已提交
101 102 103 104 105 106
static FORCE_INLINE int32_t smGetSWTypeFromMsg(SRpcMsg *pMsg) {
  SStreamExecMsgHead *pHead = pMsg->pCont;
  pHead->workerType = htonl(pHead->workerType);
  return pHead->workerType;
}

S
shm  
Shengliang Guan 已提交
107
int32_t smProcessMgmtMsg(SSnodeMgmt *pMgmt, SNodeMsg *pMsg) {
S
Shengliang Guan 已提交
108
  SMultiWorker *pWorker = taosArrayGetP(pMgmt->uniqueWorkers, 0);
S
shm  
Shengliang Guan 已提交
109 110 111
  if (pWorker == NULL) {
    terrno = TSDB_CODE_INVALID_MSG;
    return -1;
L
Liu Jicong 已提交
112 113
  }

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

S
shm  
Shengliang Guan 已提交
118
int32_t smProcessUniqueMsg(SSnodeMgmt *pMgmt, SNodeMsg *pMsg) {
S
Shengliang Guan 已提交
119 120
  int32_t       index = smGetSWIdFromMsg(&pMsg->rpcMsg);
  SMultiWorker *pWorker = taosArrayGetP(pMgmt->uniqueWorkers, index);
S
shm  
Shengliang Guan 已提交
121 122 123
  if (pWorker == NULL) {
    terrno = TSDB_CODE_INVALID_MSG;
    return -1;
S
Shengliang Guan 已提交
124 125
  }

S
shm  
Shengliang Guan 已提交
126
  dTrace("msg:%p, put into worker:%s", pMsg, pWorker->name);
S
Shengliang Guan 已提交
127
  return taosWriteQitem(pWorker->queue, pMsg);
L
Liu Jicong 已提交
128 129
}

S
shm  
Shengliang Guan 已提交
130
int32_t smProcessSharedMsg(SSnodeMgmt *pMgmt, SNodeMsg *pMsg) {
S
Shengliang Guan 已提交
131
  SSingleWorker *pWorker = &pMgmt->sharedWorker;
L
Liu Jicong 已提交
132

S
shm  
Shengliang Guan 已提交
133
  dTrace("msg:%p, put into worker:%s", pMsg, pWorker->name);
S
Shengliang Guan 已提交
134
  return taosWriteQitem(pWorker->queue, pMsg);
S
Shengliang Guan 已提交
135
}
S
shm  
Shengliang Guan 已提交
136 137 138 139 140 141 142 143 144

int32_t smProcessExecMsg(SSnodeMgmt *pMgmt, SNodeMsg *pMsg) {
  int32_t workerType = smGetSWTypeFromMsg(&pMsg->rpcMsg);
  if (workerType == SND_WORKER_TYPE__SHARED) {
    return smProcessSharedMsg(pMgmt, pMsg);
  } else {
    return smProcessUniqueMsg(pMgmt, pMsg);
  }
}