vmWorker.c 10.7 KB
Newer Older
S
shm  
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 "vmInt.h"
S
shm  
Shengliang Guan 已提交
18

S
Shengliang Guan 已提交
19
static inline void vmSendRsp(SRpcMsg *pMsg, int32_t code) {
S
Shengliang Guan 已提交
20
  if (pMsg->info.handle == NULL) return;
S
Shengliang Guan 已提交
21 22
  SRpcMsg rsp = {
      .code = code,
S
Shengliang Guan 已提交
23 24
      .pCont = pMsg->info.rsp,
      .contLen = pMsg->info.rspLen,
S
Shengliang Guan 已提交
25
      .info = pMsg->info,
S
Shengliang Guan 已提交
26
  };
S
Shengliang Guan 已提交
27
  tmsgSendRsp(&rsp);
S
Shengliang Guan 已提交
28 29
}

S
Shengliang Guan 已提交
30
static void vmProcessMgmtQueue(SQueueInfo *pInfo, SRpcMsg *pMsg) {
S
Shengliang Guan 已提交
31 32 33
  SVnodeMgmt     *pMgmt = pInfo->ahandle;
  int32_t         code = -1;
  const STraceId *trace = &pMsg->info.traceId;
S
Shengliang Guan 已提交
34

dengyihao's avatar
dengyihao 已提交
35
  dGTrace("msg:%p, get from vnode-mgmt queue", pMsg);
S
Shengliang Guan 已提交
36
  switch (pMsg->msgType) {
37
    case TDMT_MON_VM_INFO:
S
Shengliang 已提交
38
      code = vmProcessGetMonitorInfoReq(pMgmt, pMsg);
39 40
      break;
    case TDMT_MON_VM_LOAD:
S
Shengliang 已提交
41
      code = vmProcessGetLoadsReq(pMgmt, pMsg);
42
      break;
S
Shengliang Guan 已提交
43 44 45 46 47 48 49 50
    case TDMT_DND_CREATE_VNODE:
      code = vmProcessCreateVnodeReq(pMgmt, pMsg);
      break;
    case TDMT_DND_DROP_VNODE:
      code = vmProcessDropVnodeReq(pMgmt, pMsg);
      break;
    default:
      terrno = TSDB_CODE_MSG_NOT_PROCESSED;
S
Shengliang Guan 已提交
51
      dGError("msg:%p, not processed in vnode-mgmt queue", pMsg);
S
Shengliang Guan 已提交
52 53
  }

S
Shengliang Guan 已提交
54
  if (IsReq(pMsg)) {
55 56
    if (code != 0) {
      if (terrno != 0) code = terrno;
S
Shengliang Guan 已提交
57
      dGError("msg:%p, failed to process since %s", pMsg, terrstr());
S
Shengliang Guan 已提交
58
    }
S
Shengliang Guan 已提交
59
    vmSendRsp(pMsg, code);
S
Shengliang Guan 已提交
60 61
  }

S
Shengliang Guan 已提交
62
  dGTrace("msg:%p, is freed, code:0x%x", pMsg, code);
S
Shengliang Guan 已提交
63
  rpcFreeCont(pMsg->pCont);
S
Shengliang Guan 已提交
64 65 66
  taosFreeQitem(pMsg);
}

S
Shengliang Guan 已提交
67
static void vmProcessQueryQueue(SQueueInfo *pInfo, SRpcMsg *pMsg) {
S
Shengliang Guan 已提交
68 69
  SVnodeObj      *pVnode = pInfo->ahandle;
  const STraceId *trace = &pMsg->info.traceId;
S
Shengliang Guan 已提交
70

S
Shengliang Guan 已提交
71
  dGTrace("vgId:%d, msg:%p get from vnode-query queue", pVnode->vgId, pMsg);
S
Shengliang Guan 已提交
72
  int32_t code = vnodeProcessQueryMsg(pVnode->pImpl, pMsg);
S
Shengliang Guan 已提交
73
  if (code != 0) {
S
Shengliang Guan 已提交
74
    if (terrno != 0) code = terrno;
S
Shengliang Guan 已提交
75
    dGError("vgId:%d, msg:%p failed to query since %s", pVnode->vgId, pMsg, terrstr());
S
Shengliang Guan 已提交
76
    vmSendRsp(pMsg, code);
S
Shengliang Guan 已提交
77
  }
S
Shengliang Guan 已提交
78

S
Shengliang Guan 已提交
79
  dGTrace("vgId:%d, msg:%p is freed, code:0x%x", pVnode->vgId, pMsg, code);
dengyihao's avatar
dengyihao 已提交
80 81
  rpcFreeCont(pMsg->pCont);
  taosFreeQitem(pMsg);
S
shm  
Shengliang Guan 已提交
82
}
S
shm  
Shengliang Guan 已提交
83

S
Shengliang Guan 已提交
84
static void vmProcessFetchQueue(SQueueInfo *pInfo, SRpcMsg *pMsg) {
S
Shengliang Guan 已提交
85 86
  SVnodeObj      *pVnode = pInfo->ahandle;
  const STraceId *trace = &pMsg->info.traceId;
S
Shengliang Guan 已提交
87

S
Shengliang Guan 已提交
88
  dGTrace("vgId:%d, msg:%p get from vnode-fetch queue", pVnode->vgId, pMsg);
S
Shengliang Guan 已提交
89
  int32_t code = vnodeProcessFetchMsg(pVnode->pImpl, pMsg, pInfo);
S
Shengliang Guan 已提交
90
  if (code != 0) {
S
Shengliang Guan 已提交
91
    if (terrno != 0) code = terrno;
S
Shengliang Guan 已提交
92
    dGError("vgId:%d, msg:%p failed to fetch since %s", pVnode->vgId, pMsg, terrstr());
S
Shengliang Guan 已提交
93
    vmSendRsp(pMsg, code);
S
Shengliang Guan 已提交
94
  }
S
Shengliang Guan 已提交
95

S
Shengliang Guan 已提交
96
  dGTrace("vgId:%d, msg:%p is freed, code:0x%x", pVnode->vgId, pMsg, code);
dengyihao's avatar
dengyihao 已提交
97 98
  rpcFreeCont(pMsg->pCont);
  taosFreeQitem(pMsg);
S
Shengliang Guan 已提交
99 100
}

S
Shengliang Guan 已提交
101 102
static void vmProcessSyncQueue(SQueueInfo *pInfo, STaosQall *qall, int32_t numOfMsgs) {
  SVnodeObj *pVnode = pInfo->ahandle;
L
Liu Jicong 已提交
103
  SRpcMsg   *pMsg = NULL;
S
shm  
Shengliang Guan 已提交
104 105

  for (int32_t i = 0; i < numOfMsgs; ++i) {
S
Shengliang Guan 已提交
106
    if (taosGetQitem(qall, (void **)&pMsg) == 0) continue;
S
Shengliang Guan 已提交
107 108
    const STraceId *trace = &pMsg->info.traceId;
    dGTrace("vgId:%d, msg:%p get from vnode-sync queue", pVnode->vgId, pMsg);
S
shm  
Shengliang Guan 已提交
109

110
    int32_t code = vnodeProcessSyncMsg(pVnode->pImpl, pMsg, NULL);  // no response here
S
Shengliang Guan 已提交
111
    dGTrace("vgId:%d, msg:%p is freed, code:0x%x", pVnode->vgId, pMsg, code);
S
Shengliang Guan 已提交
112
    rpcFreeCont(pMsg->pCont);
M
Minghao Li 已提交
113
    taosFreeQitem(pMsg);
S
shm  
Shengliang Guan 已提交
114 115 116
  }
}

S
Shengliang Guan 已提交
117
static int32_t vmPutMsgToQueue(SVnodeMgmt *pMgmt, SRpcMsg *pMsg, EQueueType qtype) {
S
Shengliang Guan 已提交
118 119 120
  const STraceId *trace = &pMsg->info.traceId;
  SMsgHead       *pHead = pMsg->pCont;
  int32_t         code = 0;
S
Shengliang Guan 已提交
121

D
dapan1121 已提交
122 123
  pHead->contLen = ntohl(pHead->contLen);
  pHead->vgId = ntohl(pHead->vgId);
S
shm  
Shengliang Guan 已提交
124 125 126

  SVnodeObj *pVnode = vmAcquireVnode(pMgmt, pHead->vgId);
  if (pVnode == NULL) {
S
Shengliang Guan 已提交
127 128
    dGError("vgId:%d, msg:%p failed to put into vnode queue since %s, msgtype:%s qtype:%d", pHead->vgId, pMsg,
            terrstr(), TMSG_INFO(pMsg->msgType), qtype);
S
Shengliang Guan 已提交
129
    return terrno != 0 ? terrno : -1;
S
shm  
Shengliang Guan 已提交
130 131
  }

S
Shengliang Guan 已提交
132
  switch (qtype) {
S
Shengliang Guan 已提交
133
    case QUERY_QUEUE:
D
dapan1121 已提交
134
      vnodePreprocessQueryMsg(pVnode->pImpl, pMsg);
S
Shengliang Guan 已提交
135
      dGTrace("vgId:%d, msg:%p put into vnode-query queue", pVnode->vgId, pMsg);
S
Shengliang Guan 已提交
136
      taosWriteQitem(pVnode->pQueryQ, pMsg);
S
Shengliang Guan 已提交
137
      break;
S
Shengliang Guan 已提交
138
    case FETCH_QUEUE:
S
Shengliang Guan 已提交
139
      dGTrace("vgId:%d, msg:%p put into vnode-fetch queue", pVnode->vgId, pMsg);
S
Shengliang Guan 已提交
140
      taosWriteQitem(pVnode->pFetchQ, pMsg);
S
Shengliang Guan 已提交
141
      break;
S
Shengliang Guan 已提交
142
    case WRITE_QUEUE:
S
Shengliang Guan 已提交
143
      dGTrace("vgId:%d, msg:%p put into vnode-write queue", pVnode->vgId, pMsg);
S
Shengliang Guan 已提交
144
      taosWriteQitem(pVnode->pWriteQ, pMsg);
S
Shengliang Guan 已提交
145 146
      break;
    case SYNC_QUEUE:
S
Shengliang Guan 已提交
147
      dGTrace("vgId:%d, msg:%p put into vnode-sync queue", pVnode->vgId, pMsg);
S
Shengliang Guan 已提交
148
      taosWriteQitem(pVnode->pSyncQ, pMsg);
S
Shengliang Guan 已提交
149
      break;
S
Shengliang Guan 已提交
150
    case APPLY_QUEUE:
S
Shengliang Guan 已提交
151
      dGTrace("vgId:%d, msg:%p put into vnode-apply queue", pVnode->vgId, pMsg);
S
Shengliang Guan 已提交
152 153
      taosWriteQitem(pVnode->pApplyQ, pMsg);
      break;
S
Shengliang Guan 已提交
154
    default:
S
Shengliang Guan 已提交
155
      code = -1;
S
Shengliang Guan 已提交
156 157 158
      terrno = TSDB_CODE_INVALID_PARA;
      break;
  }
S
shm  
Shengliang Guan 已提交
159 160 161

  vmReleaseVnode(pMgmt, pVnode);
  return code;
S
shm  
Shengliang Guan 已提交
162 163
}

S
Shengliang Guan 已提交
164
int32_t vmPutMsgToSyncQueue(SVnodeMgmt *pMgmt, SRpcMsg *pMsg) { return vmPutMsgToQueue(pMgmt, pMsg, SYNC_QUEUE); }
S
shm  
Shengliang Guan 已提交
165

S
Shengliang Guan 已提交
166
int32_t vmPutMsgToWriteQueue(SVnodeMgmt *pMgmt, SRpcMsg *pMsg) { return vmPutMsgToQueue(pMgmt, pMsg, WRITE_QUEUE); }
S
shm  
Shengliang Guan 已提交
167

S
Shengliang Guan 已提交
168
int32_t vmPutMsgToQueryQueue(SVnodeMgmt *pMgmt, SRpcMsg *pMsg) { return vmPutMsgToQueue(pMgmt, pMsg, QUERY_QUEUE); }
S
shm  
Shengliang Guan 已提交
169

S
Shengliang Guan 已提交
170
int32_t vmPutMsgToFetchQueue(SVnodeMgmt *pMgmt, SRpcMsg *pMsg) { return vmPutMsgToQueue(pMgmt, pMsg, FETCH_QUEUE); }
S
shm  
Shengliang Guan 已提交
171

S
Shengliang Guan 已提交
172
int32_t vmPutMsgToMgmtQueue(SVnodeMgmt *pMgmt, SRpcMsg *pMsg) {
S
Shengliang Guan 已提交
173 174
  const STraceId *trace = &pMsg->info.traceId;
  dGTrace("msg:%p, put into vnode-mgmt queue", pMsg);
S
Shengliang Guan 已提交
175
  taosWriteQitem(pMgmt->mgmtWorker.queue, pMsg);
S
Shengliang Guan 已提交
176
  return 0;
S
shm  
Shengliang Guan 已提交
177 178
}

S
Shengliang Guan 已提交
179
int32_t vmPutMsgToMonitorQueue(SVnodeMgmt *pMgmt, SRpcMsg *pMsg) {
S
Shengliang Guan 已提交
180 181
  const STraceId *trace = &pMsg->info.traceId;
  dGTrace("msg:%p, put into vnode-monitor queue", pMsg);
S
Shengliang Guan 已提交
182
  taosWriteQitem(pMgmt->monitorWorker.queue, pMsg);
183 184 185
  return 0;
}

S
Shengliang Guan 已提交
186
int32_t vmPutRpcMsgToQueue(SVnodeMgmt *pMgmt, EQueueType qtype, SRpcMsg *pRpc) {
S
Shengliang Guan 已提交
187
  SRpcMsg *pMsg = taosAllocateQitem(sizeof(SRpcMsg), RPC_QITEM);
S
Shengliang Guan 已提交
188 189 190 191 192
  if (pMsg == NULL) {
    rpcFreeCont(pMsg->pCont);
    pRpc->pCont = NULL;
    return -1;
  }
S
Shengliang Guan 已提交
193

S
Shengliang Guan 已提交
194
  SMsgHead *pHead = pRpc->pCont;
195
  dTrace("vgId:%d, msg:%p is created, type:%s", pHead->vgId, pMsg, TMSG_INFO(pRpc->msgType));
S
Shengliang Guan 已提交
196

S
Shengliang Guan 已提交
197 198 199
  pHead->contLen = htonl(pHead->contLen);
  pHead->vgId = htonl(pHead->vgId);
  memcpy(pMsg, pRpc, sizeof(SRpcMsg));
S
Shengliang Guan 已提交
200 201 202 203 204 205 206 207 208 209

  int32_t code = vmPutMsgToQueue(pMgmt, pMsg, qtype);
  if (code != 0) {
    dTrace("msg:%p, is freed", pMsg);
    taosFreeQitem(pMsg);
    rpcFreeCont(pMsg->pCont);
    pRpc->pCont = NULL;
  }

  return code;
S
shm  
Shengliang Guan 已提交
210 211
}

S
Shengliang 已提交
212
int32_t vmGetQueueSize(SVnodeMgmt *pMgmt, int32_t vgId, EQueueType qtype) {
S
Shengliang Guan 已提交
213
  int32_t    size = -1;
S
Shengliang 已提交
214
  SVnodeObj *pVnode = vmAcquireVnode(pMgmt, vgId);
S
Shengliang Guan 已提交
215 216 217
  if (pVnode != NULL) {
    switch (qtype) {
      case WRITE_QUEUE:
218
        size = taosQueueItemSize(pVnode->pWriteQ);
S
Shengliang Guan 已提交
219 220
        break;
      case SYNC_QUEUE:
221
        size = taosQueueItemSize(pVnode->pSyncQ);
S
Shengliang Guan 已提交
222 223
        break;
      case APPLY_QUEUE:
224
        size = taosQueueItemSize(pVnode->pApplyQ);
S
Shengliang Guan 已提交
225
        break;
S
Shengliang Guan 已提交
226
      case QUERY_QUEUE:
227
        size = taosQueueItemSize(pVnode->pQueryQ);
S
Shengliang Guan 已提交
228 229
        break;
      case FETCH_QUEUE:
230
        size = taosQueueItemSize(pVnode->pFetchQ);
S
Shengliang Guan 已提交
231
        break;
S
Shengliang Guan 已提交
232 233 234 235
      default:
        break;
    }
  }
S
Shengliang 已提交
236
  vmReleaseVnode(pMgmt, pVnode);
S
Shengliang Guan 已提交
237 238 239
  return size;
}

S
Shengliang 已提交
240
int32_t vmAllocQueue(SVnodeMgmt *pMgmt, SVnodeObj *pVnode) {
241
  pVnode->pWriteQ = tWWorkerAllocQueue(&pMgmt->writePool, pVnode->pImpl, (FItems)vnodeProposeWriteMsg);
S
shm  
Shengliang Guan 已提交
242
  pVnode->pSyncQ = tWWorkerAllocQueue(&pMgmt->syncPool, pVnode, (FItems)vmProcessSyncQueue);
243
  pVnode->pApplyQ = tWWorkerAllocQueue(&pMgmt->applyPool, pVnode->pImpl, (FItems)vnodeApplyWriteMsg);
S
shm  
Shengliang Guan 已提交
244
  pVnode->pQueryQ = tQWorkerAllocQueue(&pMgmt->queryPool, pVnode, (FItem)vmProcessQueryQueue);
S
Shengliang Guan 已提交
245
  pVnode->pFetchQ = tQWorkerAllocQueue(&pMgmt->fetchPool, pVnode, (FItem)vmProcessFetchQueue);
S
shm  
Shengliang Guan 已提交
246

S
Shengliang Guan 已提交
247
  if (pVnode->pWriteQ == NULL || pVnode->pSyncQ == NULL || pVnode->pApplyQ == NULL || pVnode->pQueryQ == NULL ||
L
Liu Jicong 已提交
248
      pVnode->pFetchQ == NULL) {
S
shm  
Shengliang Guan 已提交
249 250 251 252
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

S
Shengliang Guan 已提交
253
  dDebug("vgId:%d, queue is alloced", pVnode->vgId);
S
shm  
Shengliang Guan 已提交
254 255 256
  return 0;
}

S
Shengliang 已提交
257
void vmFreeQueue(SVnodeMgmt *pMgmt, SVnodeObj *pVnode) {
S
shm  
Shengliang Guan 已提交
258
  tWWorkerFreeQueue(&pMgmt->writePool, pVnode->pWriteQ);
S
Shengliang Guan 已提交
259
  tWWorkerFreeQueue(&pMgmt->applyPool, pVnode->pApplyQ);
260
  tWWorkerFreeQueue(&pMgmt->syncPool, pVnode->pSyncQ);
S
Shengliang Guan 已提交
261 262
  tQWorkerFreeQueue(&pMgmt->queryPool, pVnode->pQueryQ);
  tQWorkerFreeQueue(&pMgmt->fetchPool, pVnode->pFetchQ);
S
shm  
Shengliang Guan 已提交
263 264
  pVnode->pWriteQ = NULL;
  pVnode->pSyncQ = NULL;
S
Shengliang Guan 已提交
265
  pVnode->pApplyQ = NULL;
S
shm  
Shengliang Guan 已提交
266
  pVnode->pQueryQ = NULL;
S
Shengliang Guan 已提交
267
  pVnode->pFetchQ = NULL;
S
Shengliang Guan 已提交
268
  dDebug("vgId:%d, queue is freed", pVnode->vgId);
S
shm  
Shengliang Guan 已提交
269 270
}

S
Shengliang 已提交
271
int32_t vmStartWorker(SVnodeMgmt *pMgmt) {
S
shm  
Shengliang Guan 已提交
272 273
  SQWorkerPool *pQPool = &pMgmt->queryPool;
  pQPool->name = "vnode-query";
S
Shengliang Guan 已提交
274 275
  pQPool->min = tsNumOfVnodeQueryThreads;
  pQPool->max = tsNumOfVnodeQueryThreads;
S
shm  
Shengliang Guan 已提交
276 277
  if (tQWorkerInit(pQPool) != 0) return -1;

S
Shengliang Guan 已提交
278
  SQWorkerPool *pFPool = &pMgmt->fetchPool;
S
shm  
Shengliang Guan 已提交
279
  pFPool->name = "vnode-fetch";
S
Shengliang Guan 已提交
280 281
  pFPool->min = tsNumOfVnodeFetchThreads;
  pFPool->max = tsNumOfVnodeFetchThreads;
S
Shengliang Guan 已提交
282
  if (tQWorkerInit(pFPool) != 0) return -1;
S
shm  
Shengliang Guan 已提交
283 284 285

  SWWorkerPool *pWPool = &pMgmt->writePool;
  pWPool->name = "vnode-write";
S
Shengliang Guan 已提交
286
  pWPool->max = tsNumOfVnodeWriteThreads;
S
shm  
Shengliang Guan 已提交
287 288
  if (tWWorkerInit(pWPool) != 0) return -1;

S
Shengliang Guan 已提交
289 290 291 292 293
  SWWorkerPool *pAPool = &pMgmt->applyPool;
  pAPool->name = "vnode-apply";
  pAPool->max = tsNumOfVnodeWriteThreads;
  if (tWWorkerInit(pAPool) != 0) return -1;

S
Shengliang Guan 已提交
294 295 296 297 298
  SWWorkerPool *pSPool = &pMgmt->syncPool;
  pSPool->name = "vnode-sync";
  pSPool->max = tsNumOfVnodeSyncThreads;
  if (tWWorkerInit(pSPool) != 0) return -1;

S
Shengliang Guan 已提交
299
  SSingleWorkerCfg mgmtCfg = {
S
Shengliang Guan 已提交
300 301 302
      .min = 1,
      .max = 1,
      .name = "vnode-mgmt",
S
Shengliang Guan 已提交
303
      .fp = (FItem)vmProcessMgmtQueue,
S
Shengliang Guan 已提交
304 305
      .param = pMgmt,
  };
S
Shengliang Guan 已提交
306
  if (tSingleWorkerInit(&pMgmt->mgmtWorker, &mgmtCfg) != 0) return -1;
S
shm  
Shengliang Guan 已提交
307

S
Shengliang Guan 已提交
308
  SSingleWorkerCfg monitorCfg = {
S
Shengliang Guan 已提交
309 310 311
      .min = 1,
      .max = 1,
      .name = "vnode-monitor",
S
Shengliang Guan 已提交
312
      .fp = (FItem)vmProcessMgmtQueue,
S
Shengliang Guan 已提交
313 314
      .param = pMgmt,
  };
S
Shengliang Guan 已提交
315
  if (tSingleWorkerInit(&pMgmt->monitorWorker, &monitorCfg) != 0) return -1;
316

S
Shengliang Guan 已提交
317
  dDebug("vnode workers are initialized");
S
shm  
Shengliang Guan 已提交
318 319 320
  return 0;
}

S
Shengliang 已提交
321
void vmStopWorker(SVnodeMgmt *pMgmt) {
322
  tSingleWorkerCleanup(&pMgmt->monitorWorker);
S
Shengliang Guan 已提交
323
  tSingleWorkerCleanup(&pMgmt->mgmtWorker);
S
shm  
Shengliang Guan 已提交
324
  tWWorkerCleanup(&pMgmt->writePool);
S
Shengliang Guan 已提交
325
  tWWorkerCleanup(&pMgmt->applyPool);
S
shm  
Shengliang Guan 已提交
326
  tWWorkerCleanup(&pMgmt->syncPool);
S
Shengliang Guan 已提交
327 328
  tQWorkerCleanup(&pMgmt->queryPool);
  tQWorkerCleanup(&pMgmt->fetchPool);
S
Shengliang Guan 已提交
329
  dDebug("vnode workers are closed");
S
shm  
Shengliang Guan 已提交
330
}