smWorker.c 6.1 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

19
static inline void smSendRsp(SNodeMsg *pMsg, int32_t code) {
S
Shengliang Guan 已提交
20 21 22 23 24 25 26 27
  SRpcMsg rsp = {
      .handle = pMsg->rpcMsg.handle,
      .ahandle = pMsg->rpcMsg.ahandle,
      .refId = pMsg->rpcMsg.refId,
      .code = code,
      .pCont = pMsg->pRsp,
      .contLen = pMsg->rspLen,
  };
28 29 30
  tmsgSendRsp(&rsp);
}

31 32 33
static void smProcessMonitorQueue(SQueueInfo *pInfo, SNodeMsg *pMsg) {
  SSnodeMgmt *pMgmt = pInfo->ahandle;

34
  dTrace("msg:%p, get from snode-monitor queue", pMsg);
35 36 37 38
  SRpcMsg *pRpc = &pMsg->rpcMsg;
  int32_t  code = -1;

  if (pMsg->rpcMsg.msgType == TDMT_MON_SM_INFO) {
S
Shengliang 已提交
39
    code = smProcessGetMonitorInfoReq(pMgmt, pMsg);
40 41
  } else {
    terrno = TSDB_CODE_MSG_NOT_PROCESSED;
42 43 44
  }

  if (pRpc->msgType & 1U) {
45
    if (code != 0 && terrno != 0) code = terrno;
46
    smSendRsp(pMsg, code);
47 48 49 50 51 52 53
  }

  dTrace("msg:%p, is freed, result:0x%04x:%s", pMsg, code & 0XFFFF, tstrerror(code));
  rpcFreeCont(pRpc->pCont);
  taosFreeQitem(pMsg);
}

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

S
shm  
Shengliang Guan 已提交
57 58 59
  for (int32_t i = 0; i < numOfMsgs; i++) {
    SNodeMsg *pMsg = NULL;
    taosGetQitem(qall, (void **)&pMsg);
L
Liu Jicong 已提交
60

61
    dTrace("msg:%p, get from snode-unique queue", pMsg);
S
shm  
Shengliang Guan 已提交
62
    sndProcessUMsg(pMgmt->pSnode, &pMsg->rpcMsg);
L
Liu Jicong 已提交
63

S
shm  
Shengliang Guan 已提交
64 65 66 67 68 69
    dTrace("msg:%p, is freed", pMsg);
    rpcFreeCont(pMsg->rpcMsg.pCont);
    taosFreeQitem(pMsg);
  }
}

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

73
  dTrace("msg:%p, get from snode-shared queue", pMsg);
S
shm  
Shengliang Guan 已提交
74
  sndProcessSMsg(pMgmt->pSnode, &pMsg->rpcMsg);
S
shm  
Shengliang Guan 已提交
75 76

  dTrace("msg:%p, is freed", pMsg);
S
shm  
Shengliang Guan 已提交
77
  rpcFreeCont(pMsg->rpcMsg.pCont);
S
shm  
Shengliang Guan 已提交
78 79
  taosFreeQitem(pMsg);
}
S
Shengliang Guan 已提交
80

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

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

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

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

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

S
Shengliang Guan 已提交
124 125 126 127 128 129 130 131 132 133
  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;
134 135
  }

S
Shengliang Guan 已提交
136
  dDebug("snode workers are initialized");
S
Shengliang Guan 已提交
137 138 139
  return 0;
}

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

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

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

S
Shengliang 已提交
164
int32_t smPutNodeMsgToMgmtQueue(SSnodeMgmt *pMgmt, SNodeMsg *pMsg) {
S
Shengliang Guan 已提交
165
  SMultiWorker *pWorker = taosArrayGetP(pMgmt->uniqueWorkers, 0);
S
shm  
Shengliang Guan 已提交
166 167 168
  if (pWorker == NULL) {
    terrno = TSDB_CODE_INVALID_MSG;
    return -1;
L
Liu Jicong 已提交
169 170
  }

S
shm  
Shengliang Guan 已提交
171
  dTrace("msg:%p, put into worker:%s", pMsg, pWorker->name);
S
Shengliang Guan 已提交
172
  taosWriteQitem(pWorker->queue, pMsg);
173 174 175
  return 0;
}

S
Shengliang 已提交
176
int32_t smPutNodeMsgToMonitorQueue(SSnodeMgmt *pMgmt, SNodeMsg *pMsg) {
177 178 179 180
  SSingleWorker *pWorker = &pMgmt->monitorWorker;

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

S
Shengliang 已提交
184
int32_t smPutNodeMsgToUniqueQueue(SSnodeMgmt *pMgmt, SNodeMsg *pMsg) {
S
Shengliang Guan 已提交
185 186
  int32_t       index = smGetSWIdFromMsg(&pMsg->rpcMsg);
  SMultiWorker *pWorker = taosArrayGetP(pMgmt->uniqueWorkers, index);
S
shm  
Shengliang Guan 已提交
187 188 189
  if (pWorker == NULL) {
    terrno = TSDB_CODE_INVALID_MSG;
    return -1;
S
Shengliang Guan 已提交
190 191
  }

S
shm  
Shengliang Guan 已提交
192
  dTrace("msg:%p, put into worker:%s", pMsg, pWorker->name);
S
Shengliang Guan 已提交
193 194
  taosWriteQitem(pWorker->queue, pMsg);
  return 0;
L
Liu Jicong 已提交
195 196
}

S
Shengliang 已提交
197
int32_t smPutNodeMsgToSharedQueue(SSnodeMgmt *pMgmt, SNodeMsg *pMsg) {
S
Shengliang Guan 已提交
198
  SSingleWorker *pWorker = &pMgmt->sharedWorker;
L
Liu Jicong 已提交
199

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

S
Shengliang 已提交
205
int32_t smPutNodeMsgToExecQueue(SSnodeMgmt *pMgmt, SNodeMsg *pMsg) {
206
  int32_t workerType = smGetSWTypeFromMsg(&pMsg->rpcMsg);
S
shm  
Shengliang Guan 已提交
207
  if (workerType == SND_WORKER_TYPE__SHARED) {
S
Shengliang 已提交
208
    return smPutNodeMsgToSharedQueue(pMgmt, pMsg);
S
shm  
Shengliang Guan 已提交
209
  } else {
S
Shengliang 已提交
210
    return smPutNodeMsgToUniqueQueue(pMgmt, pMsg);
S
shm  
Shengliang Guan 已提交
211 212
  }
}