vmWorker.c 16.9 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

19 20
#include "sync.h"
#include "syncTools.h"
S
shm  
Shengliang Guan 已提交
21

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

S
Shengliang Guan 已提交
34
static void vmProcessMgmtMonitorQueue(SQueueInfo *pInfo, SNodeMsg *pMsg) {
S
Shengliang Guan 已提交
35 36
  SVnodesMgmt *pMgmt = pInfo->ahandle;

S
Shengliang Guan 已提交
37 38
  int32_t code = -1;
  tmsg_t  msgType = pMsg->rpcMsg.msgType;
S
Shengliang Guan 已提交
39
  dTrace("msg:%p, will be processed in vnode-mgmt/monitor queue", pMsg);
S
Shengliang Guan 已提交
40 41

  switch (msgType) {
42 43 44 45 46 47
    case TDMT_MON_VM_INFO:
      code = vmProcessGetMonVmInfoReq(pMgmt->pWrapper, pMsg);
      break;
    case TDMT_MON_VM_LOAD:
      code = vmProcessGetVnodeLoadsReq(pMgmt->pWrapper, pMsg);
      break;
S
Shengliang Guan 已提交
48 49 50 51 52 53 54 55
    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 已提交
56
      dError("msg:%p, not processed in vnode-mgmt/monitor queue", pMsg);
S
Shengliang Guan 已提交
57 58 59 60
  }

  if (msgType & 1u) {
    if (code != 0 && terrno != 0) code = terrno;
S
Shengliang Guan 已提交
61
    vmSendRsp(pMsg, code);
S
Shengliang Guan 已提交
62 63 64 65 66 67 68
  }

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

S
Shengliang Guan 已提交
69 70 71
static void vmProcessQueryQueue(SQueueInfo *pInfo, SNodeMsg *pMsg) {
  SVnodeObj *pVnode = pInfo->ahandle;

S
Shengliang Guan 已提交
72 73 74
  dTrace("msg:%p, will be processed in vnode-query queue", pMsg);
  int32_t code = vnodeProcessQueryMsg(pVnode->pImpl, &pMsg->rpcMsg);
  if (code != 0) {
S
Shengliang Guan 已提交
75 76 77
    if (terrno != 0) code = terrno;
    vmSendRsp(pMsg, code);

S
Shengliang Guan 已提交
78 79 80
    dTrace("msg:%p, is freed, result:0x%04x:%s", pMsg, code & 0XFFFF, tstrerror(code));
    rpcFreeCont(pMsg->rpcMsg.pCont);
    taosFreeQitem(pMsg);
S
Shengliang Guan 已提交
81
  }
S
shm  
Shengliang Guan 已提交
82
}
S
shm  
Shengliang Guan 已提交
83

S
Shengliang Guan 已提交
84 85 86
static void vmProcessFetchQueue(SQueueInfo *pInfo, SNodeMsg *pMsg) {
  SVnodeObj *pVnode = pInfo->ahandle;

S
Shengliang Guan 已提交
87
  dTrace("msg:%p, will be processed in vnode-fetch queue", pMsg);
L
Liu Jicong 已提交
88
  int32_t code = vnodeProcessFetchMsg(pVnode->pImpl, &pMsg->rpcMsg, pInfo);
S
Shengliang Guan 已提交
89
  if (code != 0) {
S
Shengliang Guan 已提交
90 91 92
    if (terrno != 0) code = terrno;
    vmSendRsp(pMsg, code);

S
Shengliang Guan 已提交
93 94 95
    dTrace("msg:%p, is freed, result:0x%04x:%s", pMsg, code & 0XFFFF, tstrerror(code));
    rpcFreeCont(pMsg->rpcMsg.pCont);
    taosFreeQitem(pMsg);
S
Shengliang Guan 已提交
96
  }
S
Shengliang Guan 已提交
97 98
}

S
Shengliang Guan 已提交
99 100
static void vmProcessWriteQueue(SQueueInfo *pInfo, STaosQall *qall, int32_t numOfMsgs) {
  SVnodeObj *pVnode = pInfo->ahandle;
H
Hongze Cheng 已提交
101
  SRpcMsg    rsp;
S
Shengliang Guan 已提交
102

S
shm  
Shengliang Guan 已提交
103
  SArray *pArray = taosArrayInit(numOfMsgs, sizeof(SNodeMsg *));
S
Shengliang Guan 已提交
104 105 106 107
  if (pArray == NULL) {
    dError("failed to process %d msgs in write-queue since %s", numOfMsgs, terrstr());
    return;
  }
S
shm  
Shengliang Guan 已提交
108 109

  for (int32_t i = 0; i < numOfMsgs; ++i) {
S
shm  
Shengliang Guan 已提交
110
    SNodeMsg *pMsg = NULL;
S
Shengliang Guan 已提交
111 112 113 114 115
    if (taosGetQitem(qall, (void **)&pMsg) == 0) continue;

    dTrace("msg:%p, will be processed in vnode-write queue", pMsg);
    if (taosArrayPush(pArray, &pMsg) == NULL) {
      dTrace("msg:%p, failed to process since %s", pMsg, terrstr());
S
Shengliang Guan 已提交
116
      vmSendRsp(pMsg, TSDB_CODE_OUT_OF_MEMORY);
S
Shengliang Guan 已提交
117
    }
S
shm  
Shengliang Guan 已提交
118 119
  }

M
Minghao Li 已提交
120 121
  for (int i = 0; i < taosArrayGetSize(pArray); i++) {
    SNodeMsg *pMsg;
M
Minghao Li 已提交
122
    SRpcMsg  *pRpc;
H
Hongze Cheng 已提交
123

M
Minghao Li 已提交
124 125 126
    pMsg = *(SNodeMsg **)taosArrayGet(pArray, i);
    pRpc = &pMsg->rpcMsg;

H
Hongze Cheng 已提交
127 128
    rsp.ahandle = pRpc->ahandle;
    rsp.handle = pRpc->handle;
dengyihao's avatar
dengyihao 已提交
129
    rsp.refId = pRpc->refId;
H
Hongze Cheng 已提交
130 131
    rsp.pCont = NULL;
    rsp.contLen = 0;
M
Minghao Li 已提交
132

H
Hongze Cheng 已提交
133
    int32_t ret = syncPropose(vnodeGetSyncHandle(pVnode->pImpl), pRpc, false);
M
Minghao Li 已提交
134
    if (ret == TAOS_SYNC_PROPOSE_NOT_LEADER) {
M
Minghao Li 已提交
135 136
      // rsp.code = TSDB_CODE_SYN_NOT_LEADER;
      // tmsgSendRsp(&rsp);
M
Minghao Li 已提交
137
      dTrace("syncPropose not leader redirect, vgId:%d ", syncGetVgId(vnodeGetSyncHandle(pVnode->pImpl)));
M
Minghao Li 已提交
138 139 140
      rsp.code = TSDB_CODE_RPC_REDIRECT;
      SEpSet newEpSet;
      syncGetEpSet(vnodeGetSyncHandle(pVnode->pImpl), &newEpSet);
M
Minghao Li 已提交
141
      newEpSet.inUse = (newEpSet.inUse + 1) % newEpSet.numOfEps;
M
Minghao Li 已提交
142
      tmsgSendRedirectRsp(&rsp, &newEpSet);
M
Minghao Li 已提交
143

M
Minghao Li 已提交
144
    } else if (ret == TAOS_SYNC_PROPOSE_OTHER_ERROR) {
M
Minghao Li 已提交
145
      rsp.code = TSDB_CODE_SYN_INTERNAL_ERROR;
H
Hongze Cheng 已提交
146
      tmsgSendRsp(&rsp);
M
Minghao Li 已提交
147 148 149
    } else if (ret == TAOS_SYNC_PROPOSE_SUCCESS) {
      // ok
      // send response in applyQ
S
shm  
Shengliang Guan 已提交
150
    } else {
M
Minghao Li 已提交
151
      assert(0);
S
shm  
Shengliang Guan 已提交
152 153 154
    }
  }

S
Shengliang Guan 已提交
155
  for (int32_t i = 0; i < numOfMsgs; i++) {
S
shm  
Shengliang Guan 已提交
156 157 158 159
    SNodeMsg *pMsg = *(SNodeMsg **)taosArrayGet(pArray, i);
    dTrace("msg:%p, is freed", pMsg);
    rpcFreeCont(pMsg->rpcMsg.pCont);
    taosFreeQitem(pMsg);
S
shm  
Shengliang Guan 已提交
160 161 162 163 164
  }

  taosArrayDestroy(pArray);
}

S
Shengliang Guan 已提交
165 166
static void vmProcessApplyQueue(SQueueInfo *pInfo, STaosQall *qall, int32_t numOfMsgs) {
  SVnodeObj *pVnode = pInfo->ahandle;
M
Minghao Li 已提交
167
  SNodeMsg  *pMsg = NULL;
H
Hongze Cheng 已提交
168
  SRpcMsg    rsp;
S
shm  
Shengliang Guan 已提交
169 170 171 172

  for (int32_t i = 0; i < numOfMsgs; ++i) {
    taosGetQitem(qall, (void **)&pMsg);

173
    // init response rpc msg
M
Minghao Li 已提交
174 175 176
    rsp.code = 0;
    rsp.pCont = NULL;
    rsp.contLen = 0;
177 178 179 180 181 182 183 184

    // get original rpc msg
    assert(pMsg->rpcMsg.msgType == TDMT_VND_SYNC_APPLY_MSG);
    SyncApplyMsg *pSyncApplyMsg = syncApplyMsgFromRpcMsg2(&pMsg->rpcMsg);
    syncApplyMsgLog2("==vmProcessApplyQueue==", pSyncApplyMsg);
    SRpcMsg originalRpcMsg;
    syncApplyMsg2OriginalRpcMsg(pSyncApplyMsg, &originalRpcMsg);

185
    // apply data into tsdb
186
    if (vnodeProcessWriteReq(pVnode->pImpl, &originalRpcMsg, pSyncApplyMsg->fsmMeta.index, &rsp) < 0) {
M
Minghao Li 已提交
187 188 189 190
      rsp.code = terrno;
      dTrace("vnodeProcessWriteReq error, code:%d", terrno);
    }

191 192 193
    syncApplyMsgDestroy(pSyncApplyMsg);
    rpcFreeCont(originalRpcMsg.pCont);

194
    // if leader, send response
H
Hongze Cheng 已提交
195 196 197
    if (pMsg->rpcMsg.handle != NULL && pMsg->rpcMsg.ahandle != NULL) {
      rsp.ahandle = pMsg->rpcMsg.ahandle;
      rsp.handle = pMsg->rpcMsg.handle;
dengyihao's avatar
dengyihao 已提交
198
      rsp.refId = pMsg->rpcMsg.refId;
M
Minghao Li 已提交
199
      tmsgSendRsp(&rsp);
H
Hongze Cheng 已提交
200
    }
M
Minghao Li 已提交
201 202 203

    rpcFreeCont(pMsg->rpcMsg.pCont);
    taosFreeQitem(pMsg);
S
shm  
Shengliang Guan 已提交
204 205 206
  }
}

S
Shengliang Guan 已提交
207 208
static void vmProcessSyncQueue(SQueueInfo *pInfo, STaosQall *qall, int32_t numOfMsgs) {
  SVnodeObj *pVnode = pInfo->ahandle;
M
Minghao Li 已提交
209
  SNodeMsg  *pMsg = NULL;
S
shm  
Shengliang Guan 已提交
210 211 212 213 214 215

  for (int32_t i = 0; i < numOfMsgs; ++i) {
    taosGetQitem(qall, (void **)&pMsg);

    // todo
    SRpcMsg *pRsp = NULL;
S
shm  
Shengliang Guan 已提交
216
    (void)vnodeProcessSyncReq(pVnode->pImpl, &pMsg->rpcMsg, &pRsp);
M
Minghao Li 已提交
217 218 219

    rpcFreeCont(pMsg->rpcMsg.pCont);
    taosFreeQitem(pMsg);
S
shm  
Shengliang Guan 已提交
220 221 222
  }
}

L
Liu Jicong 已提交
223 224
static void vmProcessMergeQueue(SQueueInfo *pInfo, STaosQall *qall, int32_t numOfMsgs) {
  SVnodeObj *pVnode = pInfo->ahandle;
M
Minghao Li 已提交
225
  SNodeMsg  *pMsg = NULL;
L
Liu Jicong 已提交
226 227 228 229 230

  for (int32_t i = 0; i < numOfMsgs; ++i) {
    taosGetQitem(qall, (void **)&pMsg);

    dTrace("msg:%p, will be processed in vnode-merge queue", pMsg);
L
Liu Jicong 已提交
231
    int32_t code = vnodeProcessFetchMsg(pVnode->pImpl, &pMsg->rpcMsg, pInfo);
L
Liu Jicong 已提交
232
    if (code != 0) {
S
Shengliang Guan 已提交
233 234 235
      if (terrno != 0) code = terrno;
      vmSendRsp(pMsg, code);

L
Liu Jicong 已提交
236 237 238 239 240 241 242
      dTrace("msg:%p, is freed, result:0x%04x:%s", pMsg, code & 0XFFFF, tstrerror(code));
      rpcFreeCont(pMsg->rpcMsg.pCont);
      taosFreeQitem(pMsg);
    }
  }
}

S
Shengliang Guan 已提交
243
static int32_t vmPutNodeMsgToQueue(SVnodesMgmt *pMgmt, SNodeMsg *pMsg, EQueueType qtype) {
M
Minghao Li 已提交
244
  SRpcMsg  *pRpc = &pMsg->rpcMsg;
S
Shengliang Guan 已提交
245
  SMsgHead *pHead = pRpc->pCont;
S
Shengliang Guan 已提交
246 247
  int32_t   code = 0;

D
dapan1121 已提交
248 249
  pHead->contLen = ntohl(pHead->contLen);
  pHead->vgId = ntohl(pHead->vgId);
S
shm  
Shengliang Guan 已提交
250 251 252

  SVnodeObj *pVnode = vmAcquireVnode(pMgmt, pHead->vgId);
  if (pVnode == NULL) {
253
    dError("vgId:%d, failed to write msg:%p to vnode-queue since %s", pHead->vgId, pMsg, terrstr());
S
Shengliang Guan 已提交
254
    return terrno != 0 ? terrno : -1;
S
shm  
Shengliang Guan 已提交
255 256
  }

S
Shengliang Guan 已提交
257
  switch (qtype) {
S
Shengliang Guan 已提交
258
    case QUERY_QUEUE:
259
      dTrace("msg:%p, type:%s will be written into vnode-query queue", pMsg, TMSG_INFO(pRpc->msgType));
S
Shengliang Guan 已提交
260
      taosWriteQitem(pVnode->pQueryQ, pMsg);
S
Shengliang Guan 已提交
261
      break;
S
Shengliang Guan 已提交
262
    case FETCH_QUEUE:
263
      dTrace("msg:%p, type:%s will be written into vnode-fetch queue", pMsg, TMSG_INFO(pRpc->msgType));
S
Shengliang Guan 已提交
264
      taosWriteQitem(pVnode->pFetchQ, pMsg);
S
Shengliang Guan 已提交
265
      break;
S
Shengliang Guan 已提交
266
    case WRITE_QUEUE:
267
      dTrace("msg:%p, type:%s will be written into vnode-write queue", pMsg, TMSG_INFO(pRpc->msgType));
S
Shengliang Guan 已提交
268
      taosWriteQitem(pVnode->pWriteQ, pMsg);
S
Shengliang Guan 已提交
269 270
      break;
    case SYNC_QUEUE:
271
      dTrace("msg:%p, type:%s will be written into vnode-sync queue", pMsg, TMSG_INFO(pRpc->msgType));
S
Shengliang Guan 已提交
272
      taosWriteQitem(pVnode->pSyncQ, pMsg);
S
Shengliang Guan 已提交
273
      break;
L
fix  
Liu Jicong 已提交
274
    case MERGE_QUEUE:
275
      dTrace("msg:%p, type:%s will be written into vnode-merge queue", pMsg, TMSG_INFO(pRpc->msgType));
S
Shengliang Guan 已提交
276
      taosWriteQitem(pVnode->pMergeQ, pMsg);
L
fix  
Liu Jicong 已提交
277
      break;
S
Shengliang Guan 已提交
278
    default:
S
Shengliang Guan 已提交
279
      code = -1;
S
Shengliang Guan 已提交
280 281 282
      terrno = TSDB_CODE_INVALID_PARA;
      break;
  }
S
shm  
Shengliang Guan 已提交
283 284 285

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

S
Shengliang Guan 已提交
288 289 290 291
int32_t vmProcessSyncMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) {
  SVnodesMgmt *pMgmt = pWrapper->pMgmt;
  return vmPutNodeMsgToQueue(pMgmt, pMsg, SYNC_QUEUE);
}
S
shm  
Shengliang Guan 已提交
292

S
Shengliang Guan 已提交
293 294 295 296
int32_t vmProcessWriteMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) {
  SVnodesMgmt *pMgmt = pWrapper->pMgmt;
  return vmPutNodeMsgToQueue(pMgmt, pMsg, WRITE_QUEUE);
}
S
shm  
Shengliang Guan 已提交
297

S
Shengliang Guan 已提交
298 299 300 301
int32_t vmProcessQueryMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) {
  SVnodesMgmt *pMgmt = pWrapper->pMgmt;
  return vmPutNodeMsgToQueue(pMgmt, pMsg, QUERY_QUEUE);
}
S
shm  
Shengliang Guan 已提交
302

S
Shengliang Guan 已提交
303 304 305 306
int32_t vmProcessFetchMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) {
  SVnodesMgmt *pMgmt = pWrapper->pMgmt;
  return vmPutNodeMsgToQueue(pMgmt, pMsg, FETCH_QUEUE);
}
S
shm  
Shengliang Guan 已提交
307

S
Shengliang Guan 已提交
308 309 310 311
int32_t vmProcessMergeMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) {
  SVnodesMgmt *pMgmt = pWrapper->pMgmt;
  return vmPutNodeMsgToQueue(pMgmt, pMsg, MERGE_QUEUE);
}
L
Liu Jicong 已提交
312

S
Shengliang Guan 已提交
313
int32_t vmProcessMgmtMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) {
M
Minghao Li 已提交
314
  SVnodesMgmt   *pMgmt = pWrapper->pMgmt;
S
Shengliang Guan 已提交
315
  SSingleWorker *pWorker = &pMgmt->mgmtWorker;
S
Shengliang Guan 已提交
316
  dTrace("msg:%p, will be put into vnode-mgmt queue, worker:%s", pMsg, pWorker->name);
S
Shengliang Guan 已提交
317 318
  taosWriteQitem(pWorker->queue, pMsg);
  return 0;
S
shm  
Shengliang Guan 已提交
319 320
}

321
int32_t vmProcessMonitorMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) {
M
Minghao Li 已提交
322
  SVnodesMgmt   *pMgmt = pWrapper->pMgmt;
323 324
  SSingleWorker *pWorker = &pMgmt->monitorWorker;

S
Shengliang Guan 已提交
325
  dTrace("msg:%p, will be put into vnode-monitor queue, worker:%s", pMsg, pWorker->name);
326 327 328 329
  taosWriteQitem(pWorker->queue, pMsg);
  return 0;
}

S
Shengliang Guan 已提交
330
static int32_t vmPutRpcMsgToQueue(SMgmtWrapper *pWrapper, SRpcMsg *pRpc, EQueueType qtype) {
S
shm  
Shengliang Guan 已提交
331
  SVnodesMgmt *pMgmt = pWrapper->pMgmt;
M
Minghao Li 已提交
332
  SMsgHead    *pHead = pRpc->pCont;
S
shm  
Shengliang Guan 已提交
333 334 335 336

  SVnodeObj *pVnode = vmAcquireVnode(pMgmt, pHead->vgId);
  if (pVnode == NULL) return -1;

S
shm  
Shengliang Guan 已提交
337
  SNodeMsg *pMsg = taosAllocateQitem(sizeof(SNodeMsg));
S
Shengliang Guan 已提交
338 339
  int32_t   code = 0;

S
Shengliang Guan 已提交
340
  if (pMsg != NULL) {
S
Shengliang Guan 已提交
341
    dTrace("msg:%p, is created, type:%s", pMsg, TMSG_INFO(pRpc->msgType));
S
shm  
Shengliang Guan 已提交
342
    pMsg->rpcMsg = *pRpc;
dengyihao's avatar
dengyihao 已提交
343
    // if (pMsg->rpcMsg.handle != NULL) assert(pMsg->rpcMsg.refId != 0);
S
Shengliang Guan 已提交
344
    switch (qtype) {
L
Liu Jicong 已提交
345 346 347 348
      case WRITE_QUEUE:
        dTrace("msg:%p, will be put into vnode-write queue", pMsg);
        taosWriteQitem(pVnode->pWriteQ, pMsg);
        break;
S
Shengliang Guan 已提交
349
      case QUERY_QUEUE:
S
Shengliang Guan 已提交
350
        dTrace("msg:%p, will be put into vnode-query queue", pMsg);
S
Shengliang Guan 已提交
351
        taosWriteQitem(pVnode->pQueryQ, pMsg);
S
Shengliang Guan 已提交
352
        break;
S
Shengliang Guan 已提交
353
      case FETCH_QUEUE:
S
Shengliang Guan 已提交
354
        dTrace("msg:%p, will be put into vnode-fetch queue", pMsg);
S
Shengliang Guan 已提交
355
        taosWriteQitem(pVnode->pFetchQ, pMsg);
S
Shengliang Guan 已提交
356
        break;
S
Shengliang Guan 已提交
357
      case APPLY_QUEUE:
S
Shengliang Guan 已提交
358
        dTrace("msg:%p, will be put into vnode-apply queue", pMsg);
S
Shengliang Guan 已提交
359
        taosWriteQitem(pVnode->pApplyQ, pMsg);
S
Shengliang Guan 已提交
360
        break;
L
Liu Jicong 已提交
361 362
      case MERGE_QUEUE:
        dTrace("msg:%p, will be put into vnode-merge queue", pMsg);
S
Shengliang Guan 已提交
363
        taosWriteQitem(pVnode->pMergeQ, pMsg);
L
Liu Jicong 已提交
364
        break;
S
Shengliang Guan 已提交
365
      case SYNC_QUEUE:
M
Minghao Li 已提交
366 367 368
        dTrace("msg:%p, will be put into vnode-sync queue", pMsg);
        taosWriteQitem(pVnode->pSyncQ, pMsg);
        break;
S
Shengliang Guan 已提交
369
      default:
S
Shengliang Guan 已提交
370
        code = -1;
S
Shengliang Guan 已提交
371 372 373
        terrno = TSDB_CODE_INVALID_PARA;
        break;
    }
S
shm  
Shengliang Guan 已提交
374
  }
S
Shengliang Guan 已提交
375

S
shm  
Shengliang Guan 已提交
376 377 378 379
  vmReleaseVnode(pMgmt, pVnode);
  return code;
}

L
Liu Jicong 已提交
380 381 382 383
int32_t vmPutMsgToWriteQueue(SMgmtWrapper *pWrapper, SRpcMsg *pRpc) {
  return vmPutRpcMsgToQueue(pWrapper, pRpc, WRITE_QUEUE);
}

S
Shengliang Guan 已提交
384 385 386 387 388 389 390 391
int32_t vmPutMsgToSyncQueue(SMgmtWrapper *pWrapper, SRpcMsg *pRpc) {
  return vmPutRpcMsgToQueue(pWrapper, pRpc, SYNC_QUEUE);
}

int32_t vmPutMsgToApplyQueue(SMgmtWrapper *pWrapper, SRpcMsg *pRpc) {
  return vmPutRpcMsgToQueue(pWrapper, pRpc, APPLY_QUEUE);
}

S
Shengliang Guan 已提交
392
int32_t vmPutMsgToQueryQueue(SMgmtWrapper *pWrapper, SRpcMsg *pRpc) {
S
Shengliang Guan 已提交
393
  return vmPutRpcMsgToQueue(pWrapper, pRpc, QUERY_QUEUE);
S
Shengliang Guan 已提交
394
}
S
shm  
Shengliang Guan 已提交
395

S
Shengliang Guan 已提交
396
int32_t vmPutMsgToFetchQueue(SMgmtWrapper *pWrapper, SRpcMsg *pRpc) {
S
Shengliang Guan 已提交
397
  return vmPutRpcMsgToQueue(pWrapper, pRpc, FETCH_QUEUE);
S
Shengliang Guan 已提交
398
}
S
shm  
Shengliang Guan 已提交
399

L
Liu Jicong 已提交
400 401 402 403
int32_t vmPutMsgToMergeQueue(SMgmtWrapper *pWrapper, SRpcMsg *pRpc) {
  return vmPutRpcMsgToQueue(pWrapper, pRpc, MERGE_QUEUE);
}

S
Shengliang Guan 已提交
404 405 406 407 408 409 410 411 412 413 414 415 416 417
int32_t vmGetQueueSize(SMgmtWrapper *pWrapper, int32_t vgId, EQueueType qtype) {
  int32_t    size = -1;
  SVnodeObj *pVnode = vmAcquireVnode(pWrapper->pMgmt, vgId);
  if (pVnode != NULL) {
    switch (qtype) {
      case WRITE_QUEUE:
        size = taosQueueSize(pVnode->pWriteQ);
        break;
      case SYNC_QUEUE:
        size = taosQueueSize(pVnode->pSyncQ);
        break;
      case APPLY_QUEUE:
        size = taosQueueSize(pVnode->pApplyQ);
        break;
S
Shengliang Guan 已提交
418 419 420 421 422 423
      case QUERY_QUEUE:
        size = taosQueueSize(pVnode->pQueryQ);
        break;
      case FETCH_QUEUE:
        size = taosQueueSize(pVnode->pFetchQ);
        break;
L
Liu Jicong 已提交
424 425 426
      case MERGE_QUEUE:
        size = taosQueueSize(pVnode->pMergeQ);
        break;
S
Shengliang Guan 已提交
427 428 429 430 431 432 433 434
      default:
        break;
    }
  }
  vmReleaseVnode(pWrapper->pMgmt, pVnode);
  return size;
}

S
shm  
Shengliang Guan 已提交
435 436 437
int32_t vmAllocQueue(SVnodesMgmt *pMgmt, SVnodeObj *pVnode) {
  pVnode->pWriteQ = tWWorkerAllocQueue(&pMgmt->writePool, pVnode, (FItems)vmProcessWriteQueue);
  pVnode->pSyncQ = tWWorkerAllocQueue(&pMgmt->syncPool, pVnode, (FItems)vmProcessSyncQueue);
S
Shengliang Guan 已提交
438
  pVnode->pApplyQ = tWWorkerAllocQueue(&pMgmt->writePool, pVnode, (FItems)vmProcessApplyQueue);
S
shm  
Shengliang Guan 已提交
439
  pVnode->pQueryQ = tQWorkerAllocQueue(&pMgmt->queryPool, pVnode, (FItem)vmProcessQueryQueue);
S
Shengliang Guan 已提交
440 441
  pVnode->pFetchQ = tQWorkerAllocQueue(&pMgmt->fetchPool, pVnode, (FItem)vmProcessFetchQueue);
  pVnode->pMergeQ = tWWorkerAllocQueue(&pMgmt->mergePool, pVnode, (FItems)vmProcessMergeQueue);
S
shm  
Shengliang Guan 已提交
442

S
Shengliang Guan 已提交
443 444
  if (pVnode->pWriteQ == NULL || pVnode->pSyncQ == NULL || pVnode->pApplyQ == NULL || pVnode->pQueryQ == NULL ||
      pVnode->pFetchQ == NULL || pVnode->pMergeQ == NULL) {
S
shm  
Shengliang Guan 已提交
445 446 447 448
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

S
Shengliang Guan 已提交
449
  dDebug("vgId:%d, vnode queue is alloced", pVnode->vgId);
S
shm  
Shengliang Guan 已提交
450 451 452
  return 0;
}

S
shm  
Shengliang Guan 已提交
453
void vmFreeQueue(SVnodesMgmt *pMgmt, SVnodeObj *pVnode) {
S
shm  
Shengliang Guan 已提交
454
  tWWorkerFreeQueue(&pMgmt->writePool, pVnode->pWriteQ);
S
Shengliang Guan 已提交
455
  tWWorkerFreeQueue(&pMgmt->syncPool, pVnode->pSyncQ);
S
shm  
Shengliang Guan 已提交
456
  tWWorkerFreeQueue(&pMgmt->writePool, pVnode->pApplyQ);
S
Shengliang Guan 已提交
457 458
  tQWorkerFreeQueue(&pMgmt->queryPool, pVnode->pQueryQ);
  tQWorkerFreeQueue(&pMgmt->fetchPool, pVnode->pFetchQ);
L
Liu Jicong 已提交
459
  tWWorkerFreeQueue(&pMgmt->mergePool, pVnode->pMergeQ);
S
shm  
Shengliang Guan 已提交
460 461
  pVnode->pWriteQ = NULL;
  pVnode->pSyncQ = NULL;
S
Shengliang Guan 已提交
462
  pVnode->pApplyQ = NULL;
S
shm  
Shengliang Guan 已提交
463
  pVnode->pQueryQ = NULL;
S
Shengliang Guan 已提交
464
  pVnode->pFetchQ = NULL;
L
Liu Jicong 已提交
465
  pVnode->pMergeQ = NULL;
S
Shengliang Guan 已提交
466
  dDebug("vgId:%d, vnode queue is freed", pVnode->vgId);
S
shm  
Shengliang Guan 已提交
467 468
}

S
shm  
Shengliang Guan 已提交
469 470 471
int32_t vmStartWorker(SVnodesMgmt *pMgmt) {
  SQWorkerPool *pQPool = &pMgmt->queryPool;
  pQPool->name = "vnode-query";
S
Shengliang Guan 已提交
472 473
  pQPool->min = tsNumOfVnodeQueryThreads;
  pQPool->max = tsNumOfVnodeQueryThreads;
S
shm  
Shengliang Guan 已提交
474 475
  if (tQWorkerInit(pQPool) != 0) return -1;

S
Shengliang Guan 已提交
476
  SQWorkerPool *pFPool = &pMgmt->fetchPool;
S
shm  
Shengliang Guan 已提交
477
  pFPool->name = "vnode-fetch";
S
Shengliang Guan 已提交
478 479
  pFPool->min = tsNumOfVnodeFetchThreads;
  pFPool->max = tsNumOfVnodeFetchThreads;
S
Shengliang Guan 已提交
480
  if (tQWorkerInit(pFPool) != 0) return -1;
S
shm  
Shengliang Guan 已提交
481 482 483

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

S
Shengliang Guan 已提交
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
  SWWorkerPool *pSPool = &pMgmt->syncPool;
  pSPool->name = "vnode-sync";
  pSPool->max = tsNumOfVnodeSyncThreads;
  if (tWWorkerInit(pSPool) != 0) return -1;

  SWWorkerPool *pMPool = &pMgmt->mergePool;
  pMPool->name = "vnode-merge";
  pMPool->max = tsNumOfVnodeMergeThreads;
  if (tWWorkerInit(pMPool) != 0) return -1;

  SSingleWorkerCfg cfg = {
      .min = 1,
      .max = 1,
      .name = "vnode-mgmt",
      .fp = (FItem)vmProcessMgmtMonitorQueue,
      .param = pMgmt,
  };
S
Shengliang Guan 已提交
504
  if (tSingleWorkerInit(&pMgmt->mgmtWorker, &cfg) != 0) {
S
Shengliang Guan 已提交
505
    dError("failed to start vnode-mgmt worker since %s", terrstr());
S
shm  
Shengliang Guan 已提交
506 507 508
    return -1;
  }

509
  if (tsMultiProcess) {
510
    SSingleWorkerCfg mCfg = {
S
Shengliang Guan 已提交
511 512 513 514 515 516
        .min = 1,
        .max = 1,
        .name = "vnode-monitor",
        .fp = (FItem)vmProcessMgmtMonitorQueue,
        .param = pMgmt,
    };
517
    if (tSingleWorkerInit(&pMgmt->monitorWorker, &mCfg) != 0) {
518 519 520 521 522
      dError("failed to start mnode vnode-monitor worker since %s", terrstr());
      return -1;
    }
  }

S
Shengliang Guan 已提交
523
  dDebug("vnode workers are initialized");
S
shm  
Shengliang Guan 已提交
524 525 526 527
  return 0;
}

void vmStopWorker(SVnodesMgmt *pMgmt) {
528
  tSingleWorkerCleanup(&pMgmt->monitorWorker);
S
Shengliang Guan 已提交
529
  tSingleWorkerCleanup(&pMgmt->mgmtWorker);
S
shm  
Shengliang Guan 已提交
530 531
  tWWorkerCleanup(&pMgmt->writePool);
  tWWorkerCleanup(&pMgmt->syncPool);
S
Shengliang Guan 已提交
532 533
  tQWorkerCleanup(&pMgmt->queryPool);
  tQWorkerCleanup(&pMgmt->fetchPool);
L
fix  
Liu Jicong 已提交
534
  tWWorkerCleanup(&pMgmt->mergePool);
S
Shengliang Guan 已提交
535
  dDebug("vnode workers are closed");
S
shm  
Shengliang Guan 已提交
536
}