vmWorker.c 16.4 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
M
Minghao Li 已提交
17

S
shm  
Shengliang Guan 已提交
18
#include "vmInt.h"
S
shm  
Shengliang Guan 已提交
19

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

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

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

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

  switch (msgType) {
41 42 43 44 45 46
    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 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59
    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;
      dError("msg:%p, not processed in vnode-mgmt queue", pMsg);
  }

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

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

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

S
Shengliang Guan 已提交
71 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) {
    vmSendRsp(pVnode->pWrapper, pMsg, code);
S
Shengliang Guan 已提交
75 76 77
    dTrace("msg:%p, is freed, result:0x%04x:%s", pMsg, code & 0XFFFF, tstrerror(code));
    rpcFreeCont(pMsg->rpcMsg.pCont);
    taosFreeQitem(pMsg);
S
Shengliang Guan 已提交
78
  }
S
shm  
Shengliang Guan 已提交
79
}
S
shm  
Shengliang Guan 已提交
80

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

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

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

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

  for (int32_t i = 0; i < numOfMsgs; ++i) {
S
shm  
Shengliang Guan 已提交
105
    SNodeMsg *pMsg = NULL;
S
Shengliang Guan 已提交
106 107 108 109 110 111 112
    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 已提交
113 114
  }

H
Hongze Cheng 已提交
115
#if 0
H
Hongze Cheng 已提交
116 117
  int64_t version;

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

S
Shengliang Guan 已提交
120 121
  numOfMsgs = taosArrayGetSize(pArray);
  for (int32_t i = 0; i < numOfMsgs; i++) {
S
shm  
Shengliang Guan 已提交
122
    SNodeMsg *pMsg = *(SNodeMsg **)taosArrayGet(pArray, i);
M
Minghao Li 已提交
123
    SRpcMsg  *pRpc = &pMsg->rpcMsg;
S
Shengliang Guan 已提交
124

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

    int32_t code = vnodeProcessWriteReq(pVnode->pImpl, pRpc, version++, &rsp);
    tmsgSendRsp(&rsp);
M
Minghao Li 已提交
134
  }
H
Hongze Cheng 已提交
135
#else
M
Minghao Li 已提交
136 137 138
  // sync integration response
  for (int i = 0; i < taosArrayGetSize(pArray); i++) {
    SNodeMsg *pMsg;
dengyihao's avatar
dengyihao 已提交
139
    SRpcMsg * pRpc;
H
Hongze Cheng 已提交
140

M
Minghao Li 已提交
141 142 143
    pMsg = *(SNodeMsg **)taosArrayGet(pArray, i);
    pRpc = &pMsg->rpcMsg;

H
Hongze Cheng 已提交
144 145
    rsp.ahandle = pRpc->ahandle;
    rsp.handle = pRpc->handle;
dengyihao's avatar
dengyihao 已提交
146
    rsp.refId = pRpc->refId;
H
Hongze Cheng 已提交
147 148
    rsp.pCont = NULL;
    rsp.contLen = 0;
M
Minghao Li 已提交
149

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

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

  taosArrayDestroy(pArray);
}

S
Shengliang Guan 已提交
176 177
static void vmProcessApplyQueue(SQueueInfo *pInfo, STaosQall *qall, int32_t numOfMsgs) {
  SVnodeObj *pVnode = pInfo->ahandle;
dengyihao's avatar
dengyihao 已提交
178
  SNodeMsg * pMsg = NULL;
H
Hongze Cheng 已提交
179
  SRpcMsg    rsp;
S
shm  
Shengliang Guan 已提交
180 181

  for (int32_t i = 0; i < numOfMsgs; ++i) {
H
Hongze Cheng 已提交
182
#if 1
M
Minghao Li 已提交
183 184
    // sync integration

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

187
    // init response rpc msg
M
Minghao Li 已提交
188 189 190
    rsp.code = 0;
    rsp.pCont = NULL;
    rsp.contLen = 0;
191 192 193 194 195 196 197 198

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

199
    // apply data into tsdb
200
    if (vnodeProcessWriteReq(pVnode->pImpl, &originalRpcMsg, pSyncApplyMsg->fsmMeta.index, &rsp) < 0) {
M
Minghao Li 已提交
201 202 203 204
      rsp.code = terrno;
      dTrace("vnodeProcessWriteReq error, code:%d", terrno);
    }

205 206 207
    syncApplyMsgDestroy(pSyncApplyMsg);
    rpcFreeCont(originalRpcMsg.pCont);

208
    // if leader, send response
H
Hongze Cheng 已提交
209 210 211
    if (pMsg->rpcMsg.handle != NULL && pMsg->rpcMsg.ahandle != NULL) {
      rsp.ahandle = pMsg->rpcMsg.ahandle;
      rsp.handle = pMsg->rpcMsg.handle;
dengyihao's avatar
dengyihao 已提交
212
      rsp.refId = pMsg->rpcMsg.refId;
M
Minghao Li 已提交
213
      tmsgSendRsp(&rsp);
H
Hongze Cheng 已提交
214 215
    }
#endif
S
shm  
Shengliang Guan 已提交
216 217 218
  }
}

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

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

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

L
Liu Jicong 已提交
232 233
static void vmProcessMergeQueue(SQueueInfo *pInfo, STaosQall *qall, int32_t numOfMsgs) {
  SVnodeObj *pVnode = pInfo->ahandle;
dengyihao's avatar
dengyihao 已提交
234
  SNodeMsg * pMsg = NULL;
L
Liu Jicong 已提交
235 236 237 238 239

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

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

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

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

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

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

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

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

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

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

327
int32_t vmProcessMonitorMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) {
dengyihao's avatar
dengyihao 已提交
328
  SVnodesMgmt *  pMgmt = pWrapper->pMgmt;
329 330 331 332 333 334 335
  SSingleWorker *pWorker = &pMgmt->monitorWorker;

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

S
Shengliang Guan 已提交
336
static int32_t vmPutRpcMsgToQueue(SMgmtWrapper *pWrapper, SRpcMsg *pRpc, EQueueType qtype) {
S
shm  
Shengliang Guan 已提交
337
  SVnodesMgmt *pMgmt = pWrapper->pMgmt;
dengyihao's avatar
dengyihao 已提交
338
  SMsgHead *   pHead = pRpc->pCont;
S
shm  
Shengliang Guan 已提交
339 340 341 342

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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