smWorker.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 "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
    dTrace("msg:%p, is freed", pMsg);
    rpcFreeCont(pMsg->rpcMsg.pCont);
    taosFreeQitem(pMsg);
  }
}

S
shm  
Shengliang Guan 已提交
33
static void smProcessSharedQueue(SSnodeMgmt *pMgmt, SNodeMsg *pMsg) {
S
shm  
Shengliang Guan 已提交
34
  dTrace("msg:%p, will be processed in snode shared queue", pMsg);
S
shm  
Shengliang Guan 已提交
35
  sndProcessSMsg(pMgmt->pSnode, &pMsg->rpcMsg);
S
shm  
Shengliang Guan 已提交
36 37

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

S
shm  
Shengliang Guan 已提交
42
int32_t smStartWorker(SSnodeMgmt *pMgmt) {
S
Shengliang Guan 已提交
43
  pMgmt->uniqueWorkers = taosArrayInit(0, sizeof(SWWorkerAll *));
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++) {
S
Shengliang Guan 已提交
50
    SWWorkerAll *pUniqueWorker = malloc(sizeof(SWWorkerAll));
L
Liu Jicong 已提交
51
    if (pUniqueWorker == NULL) {
S
shm  
Shengliang Guan 已提交
52
      terrno = TSDB_CODE_OUT_OF_MEMORY;
L
Liu Jicong 已提交
53 54
      return -1;
    }
S
Shengliang Guan 已提交
55 56 57 58 59

    SWWorkerAllCfg cfg = {.maxNum = 1, .name = "snode-unique", .fp = (FItems)smProcessUniqueQueue, .param = pMgmt};

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

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

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

  return 0;
}

S
shm  
Shengliang Guan 已提交
82
void smStopWorker(SSnodeMgmt *pMgmt) {
L
Liu Jicong 已提交
83
  for (int32_t i = 0; i < taosArrayGetSize(pMgmt->uniqueWorkers); i++) {
S
Shengliang Guan 已提交
84 85
    SWWorkerAll *pWorker = taosArrayGetP(pMgmt->uniqueWorkers, i);
    tWWorkerAllCleanup(pWorker);
L
Liu Jicong 已提交
86 87
  }
  taosArrayDestroy(pMgmt->uniqueWorkers);
S
Shengliang Guan 已提交
88
  tQWorkerAllCleanup(&pMgmt->sharedWorker);
S
Shengliang Guan 已提交
89 90
}

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

S
shm  
Shengliang Guan 已提交
97 98 99 100 101 102
static FORCE_INLINE int32_t smGetSWTypeFromMsg(SRpcMsg *pMsg) {
  SStreamExecMsgHead *pHead = pMsg->pCont;
  pHead->workerType = htonl(pHead->workerType);
  return pHead->workerType;
}

S
shm  
Shengliang Guan 已提交
103
int32_t smProcessMgmtMsg(SSnodeMgmt *pMgmt, SNodeMsg *pMsg) {
S
Shengliang Guan 已提交
104
  SWWorkerAll *pWorker = taosArrayGetP(pMgmt->uniqueWorkers, 0);
S
shm  
Shengliang Guan 已提交
105 106 107
  if (pWorker == NULL) {
    terrno = TSDB_CODE_INVALID_MSG;
    return -1;
L
Liu Jicong 已提交
108 109
  }

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

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

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

S
shm  
Shengliang Guan 已提交
126
int32_t smProcessSharedMsg(SSnodeMgmt *pMgmt, SNodeMsg *pMsg) {
S
Shengliang Guan 已提交
127
  SQWorkerAll *pWorker = &pMgmt->sharedWorker;
L
Liu Jicong 已提交
128

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

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);
  }
}