smWorker.c 5.9 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
static inline void smSendRsp(SRpcMsg *pMsg, int32_t code) {
S
Shengliang Guan 已提交
20 21
  SRpcMsg rsp = {
      .code = code,
S
Shengliang Guan 已提交
22 23
      .pCont = pMsg->info.rsp,
      .contLen = pMsg->info.rspLen,
24
      .info = pMsg->info,
S
Shengliang Guan 已提交
25
  };
26 27 28
  tmsgSendRsp(&rsp);
}

S
Shengliang Guan 已提交
29
static void smProcessMonitorQueue(SQueueInfo *pInfo, SRpcMsg *pMsg) {
30
  SSnodeMgmt *pMgmt = pInfo->ahandle;
S
Shengliang Guan 已提交
31
  int32_t     code = -1;
32
  dTrace("msg:%p, get from snode-monitor queue", pMsg);
33

S
Shengliang Guan 已提交
34
  if (pMsg->msgType == TDMT_MON_SM_INFO) {
S
Shengliang 已提交
35
    code = smProcessGetMonitorInfoReq(pMgmt, pMsg);
36 37
  } else {
    terrno = TSDB_CODE_MSG_NOT_PROCESSED;
38 39
  }

S
Shengliang Guan 已提交
40
  if (IsReq(pMsg)) {
41
    if (code != 0 && terrno != 0) code = terrno;
42
    smSendRsp(pMsg, code);
43 44
  }

S
Shengliang Guan 已提交
45
  dTrace("msg:%p, is freed, code:0x%x", pMsg, code);
S
Shengliang Guan 已提交
46
  rpcFreeCont(pMsg->pCont);
47 48 49
  taosFreeQitem(pMsg);
}

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

S
shm  
Shengliang Guan 已提交
53
  for (int32_t i = 0; i < numOfMsgs; i++) {
S
Shengliang Guan 已提交
54
    SRpcMsg *pMsg = NULL;
S
shm  
Shengliang Guan 已提交
55
    taosGetQitem(qall, (void **)&pMsg);
L
Liu Jicong 已提交
56

57
    dTrace("msg:%p, get from snode-unique queue", pMsg);
S
Shengliang Guan 已提交
58
    sndProcessUMsg(pMgmt->pSnode, pMsg);
L
Liu Jicong 已提交
59

S
shm  
Shengliang Guan 已提交
60
    dTrace("msg:%p, is freed", pMsg);
S
Shengliang Guan 已提交
61
    rpcFreeCont(pMsg->pCont);
S
shm  
Shengliang Guan 已提交
62 63 64 65
    taosFreeQitem(pMsg);
  }
}

S
Shengliang Guan 已提交
66
static void smProcessSharedQueue(SQueueInfo *pInfo, SRpcMsg *pMsg) {
S
Shengliang Guan 已提交
67 68
  SSnodeMgmt *pMgmt = pInfo->ahandle;

69
  dTrace("msg:%p, get from snode-shared queue", pMsg);
S
Shengliang Guan 已提交
70
  sndProcessSMsg(pMgmt->pSnode, pMsg);
S
shm  
Shengliang Guan 已提交
71 72

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

S
shm  
Shengliang Guan 已提交
77
int32_t smStartWorker(SSnodeMgmt *pMgmt) {
S
Shengliang Guan 已提交
78
  pMgmt->uniqueWorkers = taosArrayInit(0, sizeof(SMultiWorker *));
S
shm  
Shengliang Guan 已提交
79 80 81 82 83
  if (pMgmt->uniqueWorkers == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

S
Shengliang Guan 已提交
84
  for (int32_t i = 0; i < tsNumOfSnodeUniqueThreads; i++) {
wafwerar's avatar
wafwerar 已提交
85
    SMultiWorker *pUniqueWorker = taosMemoryMalloc(sizeof(SMultiWorker));
L
Liu Jicong 已提交
86
    if (pUniqueWorker == NULL) {
S
shm  
Shengliang Guan 已提交
87
      terrno = TSDB_CODE_OUT_OF_MEMORY;
L
Liu Jicong 已提交
88 89
      return -1;
    }
S
Shengliang Guan 已提交
90

S
Shengliang Guan 已提交
91 92 93 94 95 96
    SMultiWorkerCfg cfg = {
        .max = 1,
        .name = "snode-unique",
        .fp = smProcessUniqueQueue,
        .param = pMgmt,
    };
S
Shengliang Guan 已提交
97
    if (tMultiWorkerInit(pUniqueWorker, &cfg) != 0) {
S
Shengliang Guan 已提交
98
      dError("failed to start snode-unique worker since %s", terrstr());
L
Liu Jicong 已提交
99 100
      return -1;
    }
S
shm  
Shengliang Guan 已提交
101 102 103 104
    if (taosArrayPush(pMgmt->uniqueWorkers, &pUniqueWorker) == NULL) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
L
Liu Jicong 已提交
105
  }
S
shm  
Shengliang Guan 已提交
106

S
Shengliang Guan 已提交
107 108 109 110 111 112 113
  SSingleWorkerCfg cfg = {
      .min = tsNumOfSnodeSharedThreads,
      .max = tsNumOfSnodeSharedThreads,
      .name = "snode-shared",
      .fp = (FItem)smProcessSharedQueue,
      .param = pMgmt,
  };
S
Shengliang Guan 已提交
114

S
Shengliang Guan 已提交
115
  if (tSingleWorkerInit(&pMgmt->sharedWorker, &cfg)) {
S
Shengliang Guan 已提交
116
    dError("failed to start snode shared-worker since %s", terrstr());
S
Shengliang Guan 已提交
117 118 119
    return -1;
  }

S
Shengliang Guan 已提交
120 121 122 123 124 125 126 127 128 129
  SSingleWorkerCfg mCfg = {
      .min = 1,
      .max = 1,
      .name = "snode-monitor",
      .fp = (FItem)smProcessMonitorQueue,
      .param = pMgmt,
  };
  if (tSingleWorkerInit(&pMgmt->monitorWorker, &mCfg) != 0) {
    dError("failed to start snode-monitor worker since %s", terrstr());
    return -1;
130 131
  }

S
Shengliang Guan 已提交
132
  dDebug("snode workers are initialized");
S
Shengliang Guan 已提交
133 134 135
  return 0;
}

S
shm  
Shengliang Guan 已提交
136
void smStopWorker(SSnodeMgmt *pMgmt) {
137
  tSingleWorkerCleanup(&pMgmt->monitorWorker);
L
Liu Jicong 已提交
138
  for (int32_t i = 0; i < taosArrayGetSize(pMgmt->uniqueWorkers); i++) {
S
Shengliang Guan 已提交
139 140
    SMultiWorker *pWorker = taosArrayGetP(pMgmt->uniqueWorkers, i);
    tMultiWorkerCleanup(pWorker);
L
Liu Jicong 已提交
141 142
  }
  taosArrayDestroy(pMgmt->uniqueWorkers);
S
Shengliang Guan 已提交
143
  tSingleWorkerCleanup(&pMgmt->sharedWorker);
S
Shengliang Guan 已提交
144
  dDebug("snode workers are closed");
S
Shengliang Guan 已提交
145 146
}

S
shm  
Shengliang Guan 已提交
147
static FORCE_INLINE int32_t smGetSWIdFromMsg(SRpcMsg *pMsg) {
L
Liu Jicong 已提交
148
  SMsgHead *pHead = pMsg->pCont;
L
Liu Jicong 已提交
149
  pHead->vgId = htonl(pHead->vgId);
S
Shengliang Guan 已提交
150
  return pHead->vgId % tsNumOfSnodeUniqueThreads;
L
Liu Jicong 已提交
151 152
}

S
shm  
Shengliang Guan 已提交
153
static FORCE_INLINE int32_t smGetSWTypeFromMsg(SRpcMsg *pMsg) {
L
Liu Jicong 已提交
154 155 156 157
  /*SMsgHead *pHead = pMsg->pCont;*/
  /*pHead->workerType = htonl(pHead->workerType);*/
  /*return pHead->workerType;*/
  return 0;
S
shm  
Shengliang Guan 已提交
158 159
}

S
Shengliang Guan 已提交
160
int32_t smPutNodeMsgToMgmtQueue(SSnodeMgmt *pMgmt, SRpcMsg *pMsg) {
S
Shengliang Guan 已提交
161
  SMultiWorker *pWorker = taosArrayGetP(pMgmt->uniqueWorkers, 0);
S
shm  
Shengliang Guan 已提交
162 163 164
  if (pWorker == NULL) {
    terrno = TSDB_CODE_INVALID_MSG;
    return -1;
L
Liu Jicong 已提交
165 166
  }

S
Shengliang Guan 已提交
167
  dTrace("msg:%p, put into worker %s", pMsg, pWorker->name);
S
Shengliang Guan 已提交
168
  taosWriteQitem(pWorker->queue, pMsg);
169 170 171
  return 0;
}

S
Shengliang Guan 已提交
172
int32_t smPutNodeMsgToMonitorQueue(SSnodeMgmt *pMgmt, SRpcMsg *pMsg) {
173 174
  SSingleWorker *pWorker = &pMgmt->monitorWorker;

S
Shengliang Guan 已提交
175
  dTrace("msg:%p, put into worker %s", pMsg, pWorker->name);
176
  taosWriteQitem(pWorker->queue, pMsg);
S
Shengliang Guan 已提交
177
  return 0;
L
Liu Jicong 已提交
178 179
}

S
Shengliang Guan 已提交
180 181
int32_t smPutNodeMsgToUniqueQueue(SSnodeMgmt *pMgmt, SRpcMsg *pMsg) {
  int32_t       index = smGetSWIdFromMsg(pMsg);
S
Shengliang Guan 已提交
182
  SMultiWorker *pWorker = taosArrayGetP(pMgmt->uniqueWorkers, index);
S
shm  
Shengliang Guan 已提交
183 184 185
  if (pWorker == NULL) {
    terrno = TSDB_CODE_INVALID_MSG;
    return -1;
S
Shengliang Guan 已提交
186 187
  }

S
Shengliang Guan 已提交
188
  dTrace("msg:%p, put into worker %s", pMsg, pWorker->name);
S
Shengliang Guan 已提交
189 190
  taosWriteQitem(pWorker->queue, pMsg);
  return 0;
L
Liu Jicong 已提交
191 192
}

S
Shengliang Guan 已提交
193
int32_t smPutNodeMsgToSharedQueue(SSnodeMgmt *pMgmt, SRpcMsg *pMsg) {
S
Shengliang Guan 已提交
194
  SSingleWorker *pWorker = &pMgmt->sharedWorker;
L
Liu Jicong 已提交
195

S
Shengliang Guan 已提交
196
  dTrace("msg:%p, put into worker %s", pMsg, pWorker->name);
S
Shengliang Guan 已提交
197 198
  taosWriteQitem(pWorker->queue, pMsg);
  return 0;
S
Shengliang Guan 已提交
199
}
S
shm  
Shengliang Guan 已提交
200

S
Shengliang Guan 已提交
201 202
int32_t smPutNodeMsgToExecQueue(SSnodeMgmt *pMgmt, SRpcMsg *pMsg) {
  int32_t workerType = smGetSWTypeFromMsg(pMsg);
S
shm  
Shengliang Guan 已提交
203
  if (workerType == SND_WORKER_TYPE__SHARED) {
S
Shengliang 已提交
204
    return smPutNodeMsgToSharedQueue(pMgmt, pMsg);
S
shm  
Shengliang Guan 已提交
205
  } else {
S
Shengliang 已提交
206
    return smPutNodeMsgToUniqueQueue(pMgmt, pMsg);
S
shm  
Shengliang Guan 已提交
207 208
  }
}