vmWorker.c 16.6 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
17 18
#include "sync.h"
#include "syncTools.h"
S
shm  
Shengliang Guan 已提交
19
#include "vmInt.h"
S
shm  
Shengliang Guan 已提交
20

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

S
Shengliang Guan 已提交
30 31 32
static void vmProcessMgmtQueue(SQueueInfo *pInfo, SNodeMsg *pMsg) {
  SVnodesMgmt *pMgmt = pInfo->ahandle;

S
Shengliang Guan 已提交
33 34
  int32_t code = -1;
  tmsg_t  msgType = pMsg->rpcMsg.msgType;
35
  dTrace("msg:%p, will be processed in vnode-m queue", pMsg);
S
Shengliang Guan 已提交
36 37

  switch (msgType) {
38 39 40 41 42 43
    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 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
    case TDMT_DND_CREATE_VNODE:
      code = vmProcessCreateVnodeReq(pMgmt, pMsg);
      break;
    case TDMT_DND_ALTER_VNODE:
      code = vmProcessAlterVnodeReq(pMgmt, pMsg);
      break;
    case TDMT_DND_DROP_VNODE:
      code = vmProcessDropVnodeReq(pMgmt, pMsg);
      break;
    case TDMT_DND_SYNC_VNODE:
      code = vmProcessSyncVnodeReq(pMgmt, pMsg);
      break;
    case TDMT_DND_COMPACT_VNODE:
      code = vmProcessCompactVnodeReq(pMgmt, pMsg);
      break;
    default:
      terrno = TSDB_CODE_MSG_NOT_PROCESSED;
      dError("msg:%p, not processed in vnode-mgmt queue", pMsg);
  }

  if (msgType & 1u) {
    if (code != 0 && terrno != 0) code = terrno;
S
Shengliang Guan 已提交
66
    vmSendRsp(pMgmt->pWrapper, pMsg, code);
S
Shengliang Guan 已提交
67 68 69 70 71 72 73
  }

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

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

S
Shengliang Guan 已提交
77 78 79 80
  dTrace("msg:%p, will be processed in vnode-query queue", pMsg);
  int32_t code = vnodeProcessQueryMsg(pVnode->pImpl, &pMsg->rpcMsg);
  if (code != 0) {
    vmSendRsp(pVnode->pWrapper, pMsg, code);
S
Shengliang Guan 已提交
81 82 83
    dTrace("msg:%p, is freed, result:0x%04x:%s", pMsg, code & 0XFFFF, tstrerror(code));
    rpcFreeCont(pMsg->rpcMsg.pCont);
    taosFreeQitem(pMsg);
S
Shengliang Guan 已提交
84
  }
S
shm  
Shengliang Guan 已提交
85
}
S
shm  
Shengliang Guan 已提交
86

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

S
Shengliang Guan 已提交
90
  dTrace("msg:%p, will be processed in vnode-fetch queue", pMsg);
L
Liu Jicong 已提交
91
  int32_t code = vnodeProcessFetchMsg(pVnode->pImpl, &pMsg->rpcMsg, pInfo);
S
Shengliang Guan 已提交
92 93
  if (code != 0) {
    vmSendRsp(pVnode->pWrapper, pMsg, code);
S
Shengliang Guan 已提交
94 95 96
    dTrace("msg:%p, is freed, result:0x%04x:%s", pMsg, code & 0XFFFF, tstrerror(code));
    rpcFreeCont(pMsg->rpcMsg.pCont);
    taosFreeQitem(pMsg);
S
Shengliang Guan 已提交
97
  }
S
Shengliang Guan 已提交
98 99
}

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

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

  for (int32_t i = 0; i < numOfMsgs; ++i) {
S
shm  
Shengliang Guan 已提交
111
    SNodeMsg *pMsg = NULL;
S
Shengliang Guan 已提交
112 113 114 115 116 117 118
    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());
      vmSendRsp(pVnode->pWrapper, pMsg, TSDB_CODE_OUT_OF_MEMORY);
    }
S
shm  
Shengliang Guan 已提交
119 120
  }

H
Hongze Cheng 已提交
121
#if 0
H
Hongze Cheng 已提交
122 123
  int64_t version;

H
Hongze Cheng 已提交
124
  vnodePreprocessWriteReqs(pVnode->pImpl, pArray, &version);
S
shm  
Shengliang Guan 已提交
125

M
Minghao Li 已提交
126 127 128 129 130
  numOfMsgs = taosArrayGetSize(pArray);
  for (int32_t i = 0; i < numOfMsgs; i++) {
    SNodeMsg *pMsg = *(SNodeMsg **)taosArrayGet(pArray, i);
    SRpcMsg  *pRpc = &pMsg->rpcMsg;

H
Hongze Cheng 已提交
131 132 133 134 135 136 137 138
    rsp.pCont = NULL;
    rsp.contLen = 0;
    rsp.code = 0;
    rsp.handle = pRpc->handle;
    rsp.ahandle = pRpc->ahandle;

    int32_t code = vnodeProcessWriteReq(pVnode->pImpl, pRpc, version++, &rsp);
    tmsgSendRsp(&rsp);
M
Minghao Li 已提交
139
  }
H
Hongze Cheng 已提交
140
#else
M
Minghao Li 已提交
141 142 143 144 145 146 147 148
  // sync integration response
  for (int i = 0; i < taosArrayGetSize(pArray); i++) {
    SNodeMsg *pMsg;
    SRpcMsg  *pRpc;

    pMsg = *(SNodeMsg **)taosArrayGet(pArray, i);
    pRpc = &pMsg->rpcMsg;

H
Hongze Cheng 已提交
149 150 151 152
    rsp.ahandle = pRpc->ahandle;
    rsp.handle = pRpc->handle;
    rsp.pCont = NULL;
    rsp.contLen = 0;
M
Minghao Li 已提交
153

H
Hongze Cheng 已提交
154
    int32_t ret = syncPropose(vnodeGetSyncHandle(pVnode->pImpl), pRpc, false);
M
Minghao Li 已提交
155
    if (ret == TAOS_SYNC_PROPOSE_NOT_LEADER) {
M
Minghao Li 已提交
156
      rsp.code = TSDB_CODE_SYN_NOT_LEADER;
H
Hongze Cheng 已提交
157
      tmsgSendRsp(&rsp);
M
Minghao Li 已提交
158
    } else if (ret == TAOS_SYNC_PROPOSE_OTHER_ERROR) {
M
Minghao Li 已提交
159
      rsp.code = TSDB_CODE_SYN_INTERNAL_ERROR;
H
Hongze Cheng 已提交
160
      tmsgSendRsp(&rsp);
M
Minghao Li 已提交
161 162 163 164 165 166 167
    } else if (ret == TAOS_SYNC_PROPOSE_SUCCESS) {
      // ok
      // send response in applyQ
    } else {
      assert(0);
    }
  }
H
Hongze Cheng 已提交
168
#endif
M
Minghao Li 已提交
169

S
Shengliang Guan 已提交
170
  for (int32_t i = 0; i < numOfMsgs; i++) {
S
shm  
Shengliang Guan 已提交
171 172 173 174
    SNodeMsg *pMsg = *(SNodeMsg **)taosArrayGet(pArray, i);
    dTrace("msg:%p, is freed", pMsg);
    rpcFreeCont(pMsg->rpcMsg.pCont);
    taosFreeQitem(pMsg);
S
shm  
Shengliang Guan 已提交
175 176 177 178 179
  }

  taosArrayDestroy(pArray);
}

S
Shengliang Guan 已提交
180 181 182
static void vmProcessApplyQueue(SQueueInfo *pInfo, STaosQall *qall, int32_t numOfMsgs) {
  SVnodeObj *pVnode = pInfo->ahandle;
  SNodeMsg  *pMsg = NULL;
H
Hongze Cheng 已提交
183
  SRpcMsg    rsp;
S
shm  
Shengliang Guan 已提交
184

185
  // static int64_t version = 0;
H
Hongze Cheng 已提交
186

S
shm  
Shengliang Guan 已提交
187
  for (int32_t i = 0; i < numOfMsgs; ++i) {
H
Hongze Cheng 已提交
188
#if 1
M
Minghao Li 已提交
189 190
    // sync integration

S
shm  
Shengliang Guan 已提交
191 192
    taosGetQitem(qall, (void **)&pMsg);

193
    // init response rpc msg
M
Minghao Li 已提交
194 195 196
    rsp.code = 0;
    rsp.pCont = NULL;
    rsp.contLen = 0;
197 198 199 200 201 202 203 204

    // 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);

205
    // apply data into tsdb
206
    if (vnodeProcessWriteReq(pVnode->pImpl, &originalRpcMsg, pSyncApplyMsg->fsmMeta.index, &rsp) < 0) {
M
Minghao Li 已提交
207 208 209 210
      rsp.code = terrno;
      dTrace("vnodeProcessWriteReq error, code:%d", terrno);
    }

211 212 213
    syncApplyMsgDestroy(pSyncApplyMsg);
    rpcFreeCont(originalRpcMsg.pCont);

214
    // if leader, send response
H
Hongze Cheng 已提交
215 216 217
    if (pMsg->rpcMsg.handle != NULL && pMsg->rpcMsg.ahandle != NULL) {
      rsp.ahandle = pMsg->rpcMsg.ahandle;
      rsp.handle = pMsg->rpcMsg.handle;
M
Minghao Li 已提交
218
      tmsgSendRsp(&rsp);
H
Hongze Cheng 已提交
219 220
    }
#endif
S
shm  
Shengliang Guan 已提交
221 222 223
  }
}

S
Shengliang Guan 已提交
224 225 226
static void vmProcessSyncQueue(SQueueInfo *pInfo, STaosQall *qall, int32_t numOfMsgs) {
  SVnodeObj *pVnode = pInfo->ahandle;
  SNodeMsg  *pMsg = NULL;
S
shm  
Shengliang Guan 已提交
227 228 229 230 231 232

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

    // todo
    SRpcMsg *pRsp = NULL;
S
shm  
Shengliang Guan 已提交
233
    (void)vnodeProcessSyncReq(pVnode->pImpl, &pMsg->rpcMsg, &pRsp);
S
shm  
Shengliang Guan 已提交
234 235 236
  }
}

L
Liu Jicong 已提交
237 238 239 240 241 242 243 244
static void vmProcessMergeQueue(SQueueInfo *pInfo, STaosQall *qall, int32_t numOfMsgs) {
  SVnodeObj *pVnode = pInfo->ahandle;
  SNodeMsg  *pMsg = NULL;

  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 已提交
245
    int32_t code = vnodeProcessFetchMsg(pVnode->pImpl, &pMsg->rpcMsg, pInfo);
L
Liu Jicong 已提交
246 247 248 249 250 251 252 253 254
    if (code != 0) {
      vmSendRsp(pVnode->pWrapper, pMsg, code);
      dTrace("msg:%p, is freed, result:0x%04x:%s", pMsg, code & 0XFFFF, tstrerror(code));
      rpcFreeCont(pMsg->rpcMsg.pCont);
      taosFreeQitem(pMsg);
    }
  }
}

S
Shengliang Guan 已提交
255
static int32_t vmPutNodeMsgToQueue(SVnodesMgmt *pMgmt, SNodeMsg *pMsg, EQueueType qtype) {
S
Shengliang Guan 已提交
256
  SRpcMsg  *pRpc = &pMsg->rpcMsg;
S
Shengliang Guan 已提交
257
  SMsgHead *pHead = pRpc->pCont;
D
dapan1121 已提交
258 259
  pHead->contLen = ntohl(pHead->contLen);
  pHead->vgId = ntohl(pHead->vgId);
S
shm  
Shengliang Guan 已提交
260 261 262

  SVnodeObj *pVnode = vmAcquireVnode(pMgmt, pHead->vgId);
  if (pVnode == NULL) {
263
    dError("vgId:%d, failed to write msg:%p to vnode-queue since %s", pHead->vgId, pMsg, terrstr());
H
Haojun Liao 已提交
264
    return terrno;
S
shm  
Shengliang Guan 已提交
265 266
  }

S
Shengliang Guan 已提交
267
  int32_t code = 0;
S
Shengliang Guan 已提交
268
  switch (qtype) {
S
Shengliang Guan 已提交
269
    case QUERY_QUEUE:
S
Shengliang Guan 已提交
270
      dTrace("msg:%p, will be written into vnode-query queue", pMsg);
S
Shengliang Guan 已提交
271
      taosWriteQitem(pVnode->pQueryQ, pMsg);
S
Shengliang Guan 已提交
272
      break;
S
Shengliang Guan 已提交
273
    case FETCH_QUEUE:
S
Shengliang Guan 已提交
274
      dTrace("msg:%p, will be written into vnode-fetch queue", pMsg);
S
Shengliang Guan 已提交
275
      taosWriteQitem(pVnode->pFetchQ, pMsg);
S
Shengliang Guan 已提交
276
      break;
S
Shengliang Guan 已提交
277
    case WRITE_QUEUE:
S
Shengliang Guan 已提交
278
      dTrace("msg:%p, will be written into vnode-write queue", pMsg);
S
Shengliang Guan 已提交
279
      taosWriteQitem(pVnode->pWriteQ, pMsg);
S
Shengliang Guan 已提交
280 281
      break;
    case SYNC_QUEUE:
S
Shengliang Guan 已提交
282
      dTrace("msg:%p, will be written into vnode-sync queue", pMsg);
S
Shengliang Guan 已提交
283
      taosWriteQitem(pVnode->pSyncQ, pMsg);
S
Shengliang Guan 已提交
284
      break;
L
fix  
Liu Jicong 已提交
285 286
    case MERGE_QUEUE:
      dTrace("msg:%p, will be written into vnode-merge queue", pMsg);
S
Shengliang Guan 已提交
287
      taosWriteQitem(pVnode->pMergeQ, pMsg);
L
fix  
Liu Jicong 已提交
288
      break;
S
Shengliang Guan 已提交
289
    default:
S
Shengliang Guan 已提交
290
      code = -1;
S
Shengliang Guan 已提交
291 292 293
      terrno = TSDB_CODE_INVALID_PARA;
      break;
  }
S
shm  
Shengliang Guan 已提交
294 295 296

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

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

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

S
Shengliang Guan 已提交
309 310 311 312
int32_t vmProcessQueryMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) {
  SVnodesMgmt *pMgmt = pWrapper->pMgmt;
  return vmPutNodeMsgToQueue(pMgmt, pMsg, QUERY_QUEUE);
}
S
shm  
Shengliang Guan 已提交
313

S
Shengliang Guan 已提交
314 315 316 317
int32_t vmProcessFetchMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) {
  SVnodesMgmt *pMgmt = pWrapper->pMgmt;
  return vmPutNodeMsgToQueue(pMgmt, pMsg, FETCH_QUEUE);
}
S
shm  
Shengliang Guan 已提交
318

S
Shengliang Guan 已提交
319 320 321 322
int32_t vmProcessMergeMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) {
  SVnodesMgmt *pMgmt = pWrapper->pMgmt;
  return vmPutNodeMsgToQueue(pMgmt, pMsg, MERGE_QUEUE);
}
L
Liu Jicong 已提交
323

S
Shengliang Guan 已提交
324 325
int32_t vmProcessMgmtMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) {
  SVnodesMgmt   *pMgmt = pWrapper->pMgmt;
S
Shengliang Guan 已提交
326
  SSingleWorker *pWorker = &pMgmt->mgmtWorker;
S
Shengliang Guan 已提交
327
  dTrace("msg:%p, will be written to vnode-mgmt queue, worker:%s", pMsg, pWorker->name);
S
Shengliang Guan 已提交
328 329
  taosWriteQitem(pWorker->queue, pMsg);
  return 0;
S
shm  
Shengliang Guan 已提交
330 331
}

332 333 334 335 336 337 338 339 340
int32_t vmProcessMonitorMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) {
  SVnodesMgmt   *pMgmt = pWrapper->pMgmt;
  SSingleWorker *pWorker = &pMgmt->monitorWorker;

  dTrace("msg:%p, put into worker:%s", pMsg, pWorker->name);
  taosWriteQitem(pWorker->queue, pMsg);
  return 0;
}

S
Shengliang Guan 已提交
341
static int32_t vmPutRpcMsgToQueue(SMgmtWrapper *pWrapper, SRpcMsg *pRpc, EQueueType qtype) {
S
shm  
Shengliang Guan 已提交
342
  SVnodesMgmt *pMgmt = pWrapper->pMgmt;
S
Shengliang Guan 已提交
343
  SMsgHead    *pHead = pRpc->pCont;
S
shm  
Shengliang Guan 已提交
344 345 346 347

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

S
shm  
Shengliang Guan 已提交
348
  SNodeMsg *pMsg = taosAllocateQitem(sizeof(SNodeMsg));
S
Shengliang Guan 已提交
349 350 351 352 353
  int32_t   code = 0;

  if (pMsg == NULL) {
    code = -1;
  } else {
S
Shengliang Guan 已提交
354
    dTrace("msg:%p, is created, type:%s", pMsg, TMSG_INFO(pRpc->msgType));
S
shm  
Shengliang Guan 已提交
355
    pMsg->rpcMsg = *pRpc;
S
Shengliang Guan 已提交
356
    switch (qtype) {
S
Shengliang Guan 已提交
357
      case QUERY_QUEUE:
S
Shengliang Guan 已提交
358
        dTrace("msg:%p, will be put into vnode-query queue", pMsg);
S
Shengliang Guan 已提交
359
        taosWriteQitem(pVnode->pQueryQ, pMsg);
S
Shengliang Guan 已提交
360
        break;
S
Shengliang Guan 已提交
361
      case FETCH_QUEUE:
S
Shengliang Guan 已提交
362
        dTrace("msg:%p, will be put into vnode-fetch queue", pMsg);
S
Shengliang Guan 已提交
363
        taosWriteQitem(pVnode->pFetchQ, pMsg);
S
Shengliang Guan 已提交
364
        break;
S
Shengliang Guan 已提交
365
      case APPLY_QUEUE:
S
Shengliang Guan 已提交
366
        dTrace("msg:%p, will be put into vnode-apply queue", pMsg);
S
Shengliang Guan 已提交
367
        taosWriteQitem(pVnode->pApplyQ, pMsg);
S
Shengliang Guan 已提交
368
        break;
L
Liu Jicong 已提交
369 370
      case MERGE_QUEUE:
        dTrace("msg:%p, will be put into vnode-merge queue", pMsg);
S
Shengliang Guan 已提交
371
        taosWriteQitem(pVnode->pMergeQ, pMsg);
L
Liu Jicong 已提交
372
        break;
M
Minghao Li 已提交
373 374 375 376
      case SYNC_QUEUE:  // sync integration
        dTrace("msg:%p, will be put into vnode-sync queue", pMsg);
        taosWriteQitem(pVnode->pSyncQ, pMsg);
        break;
S
Shengliang Guan 已提交
377
      default:
S
Shengliang Guan 已提交
378
        code = -1;
S
Shengliang Guan 已提交
379 380 381
        terrno = TSDB_CODE_INVALID_PARA;
        break;
    }
S
shm  
Shengliang Guan 已提交
382
  }
S
shm  
Shengliang Guan 已提交
383 384 385 386
  vmReleaseVnode(pMgmt, pVnode);
  return code;
}

S
Shengliang Guan 已提交
387
int32_t vmPutMsgToQueryQueue(SMgmtWrapper *pWrapper, SRpcMsg *pRpc) {
S
Shengliang Guan 已提交
388
  return vmPutRpcMsgToQueue(pWrapper, pRpc, QUERY_QUEUE);
S
Shengliang Guan 已提交
389
}
S
shm  
Shengliang Guan 已提交
390

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

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

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

M
Minghao Li 已提交
403 404 405 406 407
// sync integration
int32_t vmPutMsgToSyncQueue(SMgmtWrapper *pWrapper, SRpcMsg *pRpc) {
  return vmPutRpcMsgToQueue(pWrapper, pRpc, SYNC_QUEUE);
}

S
Shengliang Guan 已提交
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
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 QUERY_QUEUE:
        size = taosQueueSize(pVnode->pQueryQ);
        break;
      case FETCH_QUEUE:
        size = taosQueueSize(pVnode->pFetchQ);
        break;
      case WRITE_QUEUE:
        size = taosQueueSize(pVnode->pWriteQ);
        break;
      case SYNC_QUEUE:
        size = taosQueueSize(pVnode->pSyncQ);
        break;
      case APPLY_QUEUE:
        size = taosQueueSize(pVnode->pApplyQ);
        break;
L
Liu Jicong 已提交
428 429 430
      case MERGE_QUEUE:
        size = taosQueueSize(pVnode->pMergeQ);
        break;
S
Shengliang Guan 已提交
431 432 433 434 435 436 437 438
      default:
        break;
    }
  }
  vmReleaseVnode(pWrapper->pMgmt, pVnode);
  return size;
}

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

  if (pVnode->pApplyQ == NULL || pVnode->pWriteQ == NULL || pVnode->pSyncQ == NULL || pVnode->pFetchQ == NULL ||
L
fix  
Liu Jicong 已提交
448
      pVnode->pQueryQ == NULL || pVnode->pMergeQ == NULL) {
S
shm  
Shengliang Guan 已提交
449 450 451 452
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

S
Shengliang Guan 已提交
453
  dDebug("vgId:%d, vnode queue is alloced", pVnode->vgId);
S
shm  
Shengliang Guan 已提交
454 455 456
  return 0;
}

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

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

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

  SWWorkerPool *pWPool = &pMgmt->writePool;
  pWPool->name = "vnode-write";
S
Shengliang Guan 已提交
488
  pWPool->max = tsNumOfVnodeWriteThreads;
S
shm  
Shengliang Guan 已提交
489 490 491 492
  if (tWWorkerInit(pWPool) != 0) return -1;

  pWPool = &pMgmt->syncPool;
  pWPool->name = "vnode-sync";
S
Shengliang Guan 已提交
493
  pWPool->max = tsNumOfVnodeSyncThreads;
S
shm  
Shengliang Guan 已提交
494 495
  if (tWWorkerInit(pWPool) != 0) return -1;

L
fix  
Liu Jicong 已提交
496 497
  pWPool = &pMgmt->mergePool;
  pWPool->name = "vnode-merge";
S
Shengliang Guan 已提交
498
  pWPool->max = tsNumOfVnodeMergeThreads;
L
fix  
Liu Jicong 已提交
499 500
  if (tWWorkerInit(pWPool) != 0) return -1;

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

507
  if (tsMultiProcess) {
508
    SSingleWorkerCfg mCfg = {
509
        .min = 1, .max = 1, .name = "vnode-monitor", .fp = (FItem)vmProcessMgmtQueue, .param = pMgmt};
510
    if (tSingleWorkerInit(&pMgmt->monitorWorker, &mCfg) != 0) {
511 512 513 514 515
      dError("failed to start mnode vnode-monitor worker since %s", terrstr());
      return -1;
    }
  }

S
Shengliang Guan 已提交
516
  dDebug("vnode workers are initialized");
S
shm  
Shengliang Guan 已提交
517 518 519 520
  return 0;
}

void vmStopWorker(SVnodesMgmt *pMgmt) {
521
  tSingleWorkerCleanup(&pMgmt->monitorWorker);
S
Shengliang Guan 已提交
522
  tSingleWorkerCleanup(&pMgmt->mgmtWorker);
S
Shengliang Guan 已提交
523
  tQWorkerCleanup(&pMgmt->fetchPool);
S
shm  
Shengliang Guan 已提交
524 525 526
  tQWorkerCleanup(&pMgmt->queryPool);
  tWWorkerCleanup(&pMgmt->writePool);
  tWWorkerCleanup(&pMgmt->syncPool);
L
fix  
Liu Jicong 已提交
527
  tWWorkerCleanup(&pMgmt->mergePool);
S
Shengliang Guan 已提交
528
  dDebug("vnode workers are closed");
S
shm  
Shengliang Guan 已提交
529
}