smWorker.c 4.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 "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;
  }

S
Shengliang Guan 已提交
53
  for (int32_t i = 0; i < tsNumOfSnodeUniqueThreads; i++) {
wafwerar's avatar
wafwerar 已提交
54
    SMultiWorker *pUniqueWorker = taosMemoryMalloc(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
shm  
Shengliang Guan 已提交
60
    SMultiWorkerCfg cfg = {.max = 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
  SSingleWorkerCfg cfg = {.min = tsNumOfSnodeSharedThreads,
                          .max = tsNumOfSnodeSharedThreads,
S
Shengliang Guan 已提交
74 75 76
                          .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
    return -1;
  }

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

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

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

S
shm  
Shengliang Guan 已提交
103
static FORCE_INLINE int32_t smGetSWTypeFromMsg(SRpcMsg *pMsg) {
L
Liu Jicong 已提交
104 105 106 107
  /*SMsgHead *pHead = pMsg->pCont;*/
  /*pHead->workerType = htonl(pHead->workerType);*/
  /*return pHead->workerType;*/
  return 0;
S
shm  
Shengliang Guan 已提交
108 109
}

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

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

S
Shengliang Guan 已提交
123 124
int32_t smProcessUniqueMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) {
  SSnodeMgmt   *pMgmt = pWrapper->pMgmt;
S
Shengliang Guan 已提交
125 126
  int32_t       index = smGetSWIdFromMsg(&pMsg->rpcMsg);
  SMultiWorker *pWorker = taosArrayGetP(pMgmt->uniqueWorkers, index);
S
shm  
Shengliang Guan 已提交
127 128 129
  if (pWorker == NULL) {
    terrno = TSDB_CODE_INVALID_MSG;
    return -1;
S
Shengliang Guan 已提交
130 131
  }

S
shm  
Shengliang Guan 已提交
132
  dTrace("msg:%p, put into worker:%s", pMsg, pWorker->name);
S
Shengliang Guan 已提交
133 134
  taosWriteQitem(pWorker->queue, pMsg);
  return 0;
L
Liu Jicong 已提交
135 136
}

S
Shengliang Guan 已提交
137 138
int32_t smProcessSharedMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) {
  SSnodeMgmt    *pMgmt = pWrapper->pMgmt;
S
Shengliang Guan 已提交
139
  SSingleWorker *pWorker = &pMgmt->sharedWorker;
L
Liu Jicong 已提交
140

S
shm  
Shengliang Guan 已提交
141
  dTrace("msg:%p, put into worker:%s", pMsg, pWorker->name);
S
Shengliang Guan 已提交
142 143
  taosWriteQitem(pWorker->queue, pMsg);
  return 0;
S
Shengliang Guan 已提交
144
}
S
shm  
Shengliang Guan 已提交
145

S
Shengliang Guan 已提交
146 147
int32_t smProcessExecMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) {
  int32_t     workerType = smGetSWTypeFromMsg(&pMsg->rpcMsg);
S
shm  
Shengliang Guan 已提交
148
  if (workerType == SND_WORKER_TYPE__SHARED) {
S
Shengliang Guan 已提交
149
    return smProcessSharedMsg(pWrapper, pMsg);
S
shm  
Shengliang Guan 已提交
150
  } else {
S
Shengliang Guan 已提交
151
    return smProcessUniqueMsg(pWrapper, pMsg);
S
shm  
Shengliang Guan 已提交
152 153
  }
}