vmWorker.c 16.5 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) {
H
Hongze Cheng 已提交
156 157
      rsp.code = -1;
      tmsgSendRsp(&rsp);
M
Minghao Li 已提交
158
    } else if (ret == TAOS_SYNC_PROPOSE_OTHER_ERROR) {
H
Hongze Cheng 已提交
159 160
      rsp.code = -2;
      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);

M
Minghao Li 已提交
193 194 195
    rsp.code = 0;
    rsp.pCont = NULL;
    rsp.contLen = 0;
196 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);

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

209 210 211
    syncApplyMsgDestroy(pSyncApplyMsg);
    rpcFreeCont(originalRpcMsg.pCont);

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

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

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

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

L
Liu Jicong 已提交
234 235 236 237 238 239 240 241
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 已提交
242
    int32_t code = vnodeProcessFetchMsg(pVnode->pImpl, &pMsg->rpcMsg, pInfo);
L
Liu Jicong 已提交
243 244 245 246 247 248 249 250 251
    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 已提交
252
static int32_t vmPutNodeMsgToQueue(SVnodesMgmt *pMgmt, SNodeMsg *pMsg, EQueueType qtype) {
S
Shengliang Guan 已提交
253
  SRpcMsg  *pRpc = &pMsg->rpcMsg;
S
Shengliang Guan 已提交
254
  SMsgHead *pHead = pRpc->pCont;
D
dapan1121 已提交
255 256
  pHead->contLen = ntohl(pHead->contLen);
  pHead->vgId = ntohl(pHead->vgId);
S
shm  
Shengliang Guan 已提交
257 258 259

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

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

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

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

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

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

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

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

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

329 330 331 332 333 334 335 336 337
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 已提交
338
static int32_t vmPutRpcMsgToQueue(SMgmtWrapper *pWrapper, SRpcMsg *pRpc, EQueueType qtype) {
S
shm  
Shengliang Guan 已提交
339
  SVnodesMgmt *pMgmt = pWrapper->pMgmt;
S
Shengliang Guan 已提交
340
  SMsgHead    *pHead = pRpc->pCont;
S
shm  
Shengliang Guan 已提交
341 342 343 344

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

S
shm  
Shengliang Guan 已提交
345
  SNodeMsg *pMsg = taosAllocateQitem(sizeof(SNodeMsg));
S
Shengliang Guan 已提交
346 347 348 349 350
  int32_t   code = 0;

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

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

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

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

L
Liu Jicong 已提交
396 397 398 399
int32_t vmPutMsgToMergeQueue(SMgmtWrapper *pWrapper, SRpcMsg *pRpc) {
  return vmPutRpcMsgToQueue(pWrapper, pRpc, MERGE_QUEUE);
}

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

S
Shengliang Guan 已提交
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
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 已提交
425 426 427
      case MERGE_QUEUE:
        size = taosQueueSize(pVnode->pMergeQ);
        break;
S
Shengliang Guan 已提交
428 429 430 431 432 433 434 435
      default:
        break;
    }
  }
  vmReleaseVnode(pWrapper->pMgmt, pVnode);
  return size;
}

S
shm  
Shengliang Guan 已提交
436 437 438
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 已提交
439
  pVnode->pMergeQ = tWWorkerAllocQueue(&pMgmt->mergePool, pVnode, (FItems)vmProcessMergeQueue);
S
shm  
Shengliang Guan 已提交
440
  pVnode->pSyncQ = tWWorkerAllocQueue(&pMgmt->syncPool, pVnode, (FItems)vmProcessSyncQueue);
S
Shengliang Guan 已提交
441
  pVnode->pFetchQ = tQWorkerAllocQueue(&pMgmt->fetchPool, pVnode, (FItem)vmProcessFetchQueue);
S
shm  
Shengliang Guan 已提交
442
  pVnode->pQueryQ = tQWorkerAllocQueue(&pMgmt->queryPool, pVnode, (FItem)vmProcessQueryQueue);
S
shm  
Shengliang Guan 已提交
443 444

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

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

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

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

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

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

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

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

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

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

S
Shengliang Guan 已提交
513
  dDebug("vnode workers are initialized");
S
shm  
Shengliang Guan 已提交
514 515 516 517
  return 0;
}

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