vmWorker.c 16.2 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
static inline void vmSendRsp(SRpcMsg *pMsg, int32_t code) {
S
Shengliang Guan 已提交
23 24
  SRpcMsg rsp = {
      .code = code,
S
Shengliang Guan 已提交
25
      .info = pMsg->info,
S
Shengliang Guan 已提交
26 27
      .pCont = pMsg->info.rsp,
      .contLen = pMsg->info.rspLen,
S
Shengliang Guan 已提交
28
  };
S
Shengliang Guan 已提交
29
  tmsgSendRsp(&rsp);
S
Shengliang Guan 已提交
30 31
}

S
Shengliang Guan 已提交
32
static void vmProcessMgmtMonitorQueue(SQueueInfo *pInfo, SRpcMsg *pMsg) {
S
Shengliang 已提交
33
  SVnodeMgmt *pMgmt = pInfo->ahandle;
S
Shengliang Guan 已提交
34

S
Shengliang Guan 已提交
35
  int32_t code = -1;
S
Shengliang Guan 已提交
36 37
  tmsg_t  msgType = pMsg->msgType;
  dTrace("msg:%p, get from vnode queue, type:%s", pMsg, TMSG_INFO(msgType));
S
Shengliang Guan 已提交
38 39

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

  if (msgType & 1u) {
    if (code != 0 && terrno != 0) code = terrno;
S
Shengliang Guan 已提交
59
    vmSendRsp(pMsg, code);
S
Shengliang Guan 已提交
60 61
  }

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

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

S
Shengliang Guan 已提交
70
  dTrace("msg:%p, get from vnode-query queue", pMsg);
S
Shengliang Guan 已提交
71
  int32_t code = vnodeProcessQueryMsg(pVnode->pImpl, pMsg);
S
Shengliang Guan 已提交
72
  if (code != 0) {
S
Shengliang Guan 已提交
73 74 75
    if (terrno != 0) code = terrno;
    vmSendRsp(pMsg, code);

S
Shengliang Guan 已提交
76
    dTrace("msg:%p, is freed, code:0x%x", pMsg, code);
S
Shengliang Guan 已提交
77
    rpcFreeCont(pMsg->pCont);
S
Shengliang Guan 已提交
78
    taosFreeQitem(pMsg);
S
Shengliang Guan 已提交
79
  }
S
shm  
Shengliang Guan 已提交
80
}
S
shm  
Shengliang Guan 已提交
81

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

S
Shengliang Guan 已提交
85
  dTrace("msg:%p, get from vnode-fetch queue", pMsg);
S
Shengliang Guan 已提交
86
  int32_t code = vnodeProcessFetchMsg(pVnode->pImpl, pMsg, pInfo);
S
Shengliang Guan 已提交
87
  if (code != 0) {
S
Shengliang Guan 已提交
88 89 90
    if (terrno != 0) code = terrno;
    vmSendRsp(pMsg, code);

S
Shengliang Guan 已提交
91
    dTrace("msg:%p, is freed, code:0x%x", pMsg, code);
S
Shengliang Guan 已提交
92
    rpcFreeCont(pMsg->pCont);
S
Shengliang Guan 已提交
93
    taosFreeQitem(pMsg);
S
Shengliang Guan 已提交
94
  }
S
Shengliang Guan 已提交
95 96
}

S
Shengliang Guan 已提交
97 98 99
static void vmProcessWriteQueue(SQueueInfo *pInfo, STaosQall *qall, int32_t numOfMsgs) {
  SVnodeObj *pVnode = pInfo->ahandle;

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

  for (int32_t i = 0; i < numOfMsgs; ++i) {
S
Shengliang Guan 已提交
107
    SRpcMsg *pMsg = NULL;
S
Shengliang Guan 已提交
108 109
    if (taosGetQitem(qall, (void **)&pMsg) == 0) continue;

S
Shengliang Guan 已提交
110
    dTrace("msg:%p, get from vnode-write queue", pMsg);
S
Shengliang Guan 已提交
111 112
    if (taosArrayPush(pArray, &pMsg) == NULL) {
      dTrace("msg:%p, failed to process since %s", pMsg, terrstr());
S
Shengliang Guan 已提交
113
      vmSendRsp(pMsg, TSDB_CODE_OUT_OF_MEMORY);
S
Shengliang Guan 已提交
114
    }
S
shm  
Shengliang Guan 已提交
115 116
  }

M
Minghao Li 已提交
117
  for (int i = 0; i < taosArrayGetSize(pArray); i++) {
S
Shengliang Guan 已提交
118 119
    SRpcMsg *pMsg = *(SRpcMsg **)taosArrayGet(pArray, i);
    SRpcMsg  rsp = {.info = pMsg->info, .pCont = NULL, .contLen = 0};
M
Minghao Li 已提交
120

S
Shengliang Guan 已提交
121
    int32_t ret = syncPropose(vnodeGetSyncHandle(pVnode->pImpl), pMsg, false);
M
Minghao Li 已提交
122
    if (ret == TAOS_SYNC_PROPOSE_NOT_LEADER) {
S
Shengliang Guan 已提交
123
      dTrace("msg:%p, is redirect since not leader, vgId:%d ", pMsg, pVnode->vgId);
M
Minghao Li 已提交
124 125 126
      rsp.code = TSDB_CODE_RPC_REDIRECT;
      SEpSet newEpSet;
      syncGetEpSet(vnodeGetSyncHandle(pVnode->pImpl), &newEpSet);
M
Minghao Li 已提交
127
      newEpSet.inUse = (newEpSet.inUse + 1) % newEpSet.numOfEps;
M
Minghao Li 已提交
128
      tmsgSendRedirectRsp(&rsp, &newEpSet);
M
Minghao Li 已提交
129
    } else if (ret == TAOS_SYNC_PROPOSE_OTHER_ERROR) {
M
Minghao Li 已提交
130
      rsp.code = TSDB_CODE_SYN_INTERNAL_ERROR;
H
Hongze Cheng 已提交
131
      tmsgSendRsp(&rsp);
M
Minghao Li 已提交
132 133 134
    } else if (ret == TAOS_SYNC_PROPOSE_SUCCESS) {
      // ok
      // send response in applyQ
S
shm  
Shengliang Guan 已提交
135
    } else {
M
Minghao Li 已提交
136
      assert(0);
S
shm  
Shengliang Guan 已提交
137 138 139
    }
  }

S
Shengliang Guan 已提交
140
  for (int32_t i = 0; i < numOfMsgs; i++) {
S
Shengliang Guan 已提交
141
    SRpcMsg *pMsg = *(SRpcMsg **)taosArrayGet(pArray, i);
S
shm  
Shengliang Guan 已提交
142
    dTrace("msg:%p, is freed", pMsg);
S
Shengliang Guan 已提交
143
    rpcFreeCont(pMsg->pCont);
S
shm  
Shengliang Guan 已提交
144
    taosFreeQitem(pMsg);
S
shm  
Shengliang Guan 已提交
145 146 147 148 149
  }

  taosArrayDestroy(pArray);
}

S
Shengliang Guan 已提交
150 151
static void vmProcessApplyQueue(SQueueInfo *pInfo, STaosQall *qall, int32_t numOfMsgs) {
  SVnodeObj *pVnode = pInfo->ahandle;
S
Shengliang Guan 已提交
152
  SRpcMsg   *pMsg = NULL;
H
Hongze Cheng 已提交
153
  SRpcMsg    rsp;
S
shm  
Shengliang Guan 已提交
154 155 156 157

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

158
    // init response rpc msg
M
Minghao Li 已提交
159 160 161
    rsp.code = 0;
    rsp.pCont = NULL;
    rsp.contLen = 0;
162 163

    // get original rpc msg
S
Shengliang Guan 已提交
164 165
    assert(pMsg->msgType == TDMT_VND_SYNC_APPLY_MSG);
    SyncApplyMsg *pSyncApplyMsg = syncApplyMsgFromRpcMsg2(pMsg);
166 167 168 169
    syncApplyMsgLog2("==vmProcessApplyQueue==", pSyncApplyMsg);
    SRpcMsg originalRpcMsg;
    syncApplyMsg2OriginalRpcMsg(pSyncApplyMsg, &originalRpcMsg);

170
    // apply data into tsdb
171
    if (vnodeProcessWriteReq(pVnode->pImpl, &originalRpcMsg, pSyncApplyMsg->fsmMeta.index, &rsp) < 0) {
M
Minghao Li 已提交
172
      rsp.code = terrno;
S
Shengliang Guan 已提交
173
      dTrace("msg:%p, process write error since %s", pMsg, terrstr());
M
Minghao Li 已提交
174 175
    }

176 177 178
    syncApplyMsgDestroy(pSyncApplyMsg);
    rpcFreeCont(originalRpcMsg.pCont);

179
    // if leader, send response
180 181
    // if (pMsg->rpcMsg.handle != NULL && pMsg->rpcMsg.ahandle != NULL) {
    if (pMsg->info.handle != NULL) {
S
Shengliang Guan 已提交
182
      rsp.info = pMsg->info;
M
Minghao Li 已提交
183
      tmsgSendRsp(&rsp);
H
Hongze Cheng 已提交
184
    }
M
Minghao Li 已提交
185

S
Shengliang Guan 已提交
186
    rpcFreeCont(pMsg->pCont);
M
Minghao Li 已提交
187
    taosFreeQitem(pMsg);
S
shm  
Shengliang Guan 已提交
188 189 190
  }
}

S
Shengliang Guan 已提交
191 192
static void vmProcessSyncQueue(SQueueInfo *pInfo, STaosQall *qall, int32_t numOfMsgs) {
  SVnodeObj *pVnode = pInfo->ahandle;
193
  SRpcMsg   *pMsg = NULL;
S
shm  
Shengliang Guan 已提交
194 195 196 197 198 199

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

    // todo
    SRpcMsg *pRsp = NULL;
200
    int32_t  ret = vnodeProcessSyncReq(pVnode->pImpl, pMsg, &pRsp);
201 202
    if (ret != 0) {
      // if leader, send response
203 204
      if (pMsg->info.handle != NULL) {
        SRpcMsg rsp = {0};
205
        rsp.code = terrno;
206
        rsp.info = pMsg->info;
S
Shengliang Guan 已提交
207
        dTrace("msg:%p, process sync queue error since code:%s", pMsg, terrstr());
208 209 210
        tmsgSendRsp(&rsp);
      }
    }
M
Minghao Li 已提交
211

S
Shengliang Guan 已提交
212
    rpcFreeCont(pMsg->pCont);
M
Minghao Li 已提交
213
    taosFreeQitem(pMsg);
S
shm  
Shengliang Guan 已提交
214 215 216
  }
}

L
Liu Jicong 已提交
217 218
static void vmProcessMergeQueue(SQueueInfo *pInfo, STaosQall *qall, int32_t numOfMsgs) {
  SVnodeObj *pVnode = pInfo->ahandle;
S
Shengliang Guan 已提交
219
  SRpcMsg   *pMsg = NULL;
L
Liu Jicong 已提交
220 221 222 223

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

S
Shengliang Guan 已提交
224
    dTrace("msg:%p, get from vnode-merge queue", pMsg);
S
Shengliang Guan 已提交
225
    int32_t code = vnodeProcessFetchMsg(pVnode->pImpl, pMsg, pInfo);
L
Liu Jicong 已提交
226
    if (code != 0) {
S
Shengliang Guan 已提交
227 228 229
      if (terrno != 0) code = terrno;
      vmSendRsp(pMsg, code);

S
Shengliang Guan 已提交
230
      dTrace("msg:%p, is freed, code:0x%x", pMsg, code);
S
Shengliang Guan 已提交
231
      rpcFreeCont(pMsg->pCont);
L
Liu Jicong 已提交
232 233 234 235 236
      taosFreeQitem(pMsg);
    }
  }
}

S
Shengliang Guan 已提交
237 238
static int32_t vmPutNodeMsgToQueue(SVnodeMgmt *pMgmt, SRpcMsg *pMsg, EQueueType qtype) {
  SRpcMsg  *pRpc = pMsg;
S
Shengliang Guan 已提交
239
  SMsgHead *pHead = pRpc->pCont;
S
Shengliang Guan 已提交
240 241
  int32_t   code = 0;

D
dapan1121 已提交
242 243
  pHead->contLen = ntohl(pHead->contLen);
  pHead->vgId = ntohl(pHead->vgId);
S
shm  
Shengliang Guan 已提交
244 245 246

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

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

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

S
Shengliang Guan 已提交
282
int32_t vmPutNodeMsgToSyncQueue(SVnodeMgmt *pMgmt, SRpcMsg *pMsg) {
S
Shengliang Guan 已提交
283 284
  return vmPutNodeMsgToQueue(pMgmt, pMsg, SYNC_QUEUE);
}
S
shm  
Shengliang Guan 已提交
285

S
Shengliang Guan 已提交
286
int32_t vmPutNodeMsgToWriteQueue(SVnodeMgmt *pMgmt, SRpcMsg *pMsg) {
S
Shengliang Guan 已提交
287 288
  return vmPutNodeMsgToQueue(pMgmt, pMsg, WRITE_QUEUE);
}
S
shm  
Shengliang Guan 已提交
289

S
Shengliang Guan 已提交
290
int32_t vmPutNodeMsgToQueryQueue(SVnodeMgmt *pMgmt, SRpcMsg *pMsg) {
S
Shengliang Guan 已提交
291 292
  return vmPutNodeMsgToQueue(pMgmt, pMsg, QUERY_QUEUE);
}
S
shm  
Shengliang Guan 已提交
293

S
Shengliang Guan 已提交
294
int32_t vmPutNodeMsgToFetchQueue(SVnodeMgmt *pMgmt, SRpcMsg *pMsg) {
S
Shengliang Guan 已提交
295 296
  return vmPutNodeMsgToQueue(pMgmt, pMsg, FETCH_QUEUE);
}
S
shm  
Shengliang Guan 已提交
297

S
Shengliang Guan 已提交
298
int32_t vmPutNodeMsgToMergeQueue(SVnodeMgmt *pMgmt, SRpcMsg *pMsg) {
S
Shengliang Guan 已提交
299 300
  return vmPutNodeMsgToQueue(pMgmt, pMsg, MERGE_QUEUE);
}
L
Liu Jicong 已提交
301

S
Shengliang Guan 已提交
302
int32_t vmPutNodeMsgToMgmtQueue(SVnodeMgmt *pMgmt, SRpcMsg *pMsg) {
S
Shengliang Guan 已提交
303
  SSingleWorker *pWorker = &pMgmt->mgmtWorker;
S
Shengliang Guan 已提交
304
  dTrace("msg:%p, put into vnode-mgmt worker, type:%s", pMsg, TMSG_INFO(pMsg->msgType));
S
Shengliang Guan 已提交
305 306
  taosWriteQitem(pWorker->queue, pMsg);
  return 0;
S
shm  
Shengliang Guan 已提交
307 308
}

S
Shengliang Guan 已提交
309
int32_t vmPutNodeMsgToMonitorQueue(SVnodeMgmt *pMgmt, SRpcMsg *pMsg) {
310 311
  SSingleWorker *pWorker = &pMgmt->monitorWorker;

S
Shengliang Guan 已提交
312
  dTrace("msg:%p, put into vnode-monitor worker, type:%s", pMsg, TMSG_INFO(pMsg->msgType));
313 314 315 316
  taosWriteQitem(pWorker->queue, pMsg);
  return 0;
}

S
Shengliang 已提交
317 318
static int32_t vmPutRpcMsgToQueue(SVnodeMgmt *pMgmt, SRpcMsg *pRpc, EQueueType qtype) {
  SMsgHead *pHead = pRpc->pCont;
S
shm  
Shengliang Guan 已提交
319 320 321 322

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

S
Shengliang Guan 已提交
323
  SRpcMsg *pMsg = taosAllocateQitem(sizeof(SRpcMsg), RPC_QITEM);
S
Shengliang Guan 已提交
324
  int32_t  code = 0;
S
Shengliang Guan 已提交
325

S
Shengliang Guan 已提交
326
  if (pMsg != NULL) {
S
Shengliang Guan 已提交
327
    memcpy(pMsg, pRpc, sizeof(SRpcMsg));
S
Shengliang Guan 已提交
328
    switch (qtype) {
L
Liu Jicong 已提交
329
      case WRITE_QUEUE:
S
Shengliang Guan 已提交
330
        dTrace("msg:%p, create and put into vnode-write worker, type:%s", pMsg, TMSG_INFO(pRpc->msgType));
L
Liu Jicong 已提交
331 332
        taosWriteQitem(pVnode->pWriteQ, pMsg);
        break;
S
Shengliang Guan 已提交
333
      case QUERY_QUEUE:
S
Shengliang Guan 已提交
334
        dTrace("msg:%p, create and put into vnode-query queue, type:%s", pMsg, TMSG_INFO(pRpc->msgType));
S
Shengliang Guan 已提交
335
        taosWriteQitem(pVnode->pQueryQ, pMsg);
S
Shengliang Guan 已提交
336
        break;
S
Shengliang Guan 已提交
337
      case FETCH_QUEUE:
S
Shengliang Guan 已提交
338
        dTrace("msg:%p, create and put into vnode-fetch queue, type:%s", pMsg, TMSG_INFO(pRpc->msgType));
S
Shengliang Guan 已提交
339
        taosWriteQitem(pVnode->pFetchQ, pMsg);
S
Shengliang Guan 已提交
340
        break;
S
Shengliang Guan 已提交
341
      case APPLY_QUEUE:
S
Shengliang Guan 已提交
342
        dTrace("msg:%p, create and put into vnode-apply queue, type:%s", pMsg, TMSG_INFO(pRpc->msgType));
S
Shengliang Guan 已提交
343
        taosWriteQitem(pVnode->pApplyQ, pMsg);
S
Shengliang Guan 已提交
344
        break;
L
Liu Jicong 已提交
345
      case MERGE_QUEUE:
S
Shengliang Guan 已提交
346
        dTrace("msg:%p, create and put into vnode-merge queue, type:%s", pMsg, TMSG_INFO(pRpc->msgType));
S
Shengliang Guan 已提交
347
        taosWriteQitem(pVnode->pMergeQ, pMsg);
L
Liu Jicong 已提交
348
        break;
S
Shengliang Guan 已提交
349
      case SYNC_QUEUE:
S
Shengliang Guan 已提交
350
        dTrace("msg:%p, create and put into vnode-sync queue, type:%s", pMsg, TMSG_INFO(pRpc->msgType));
M
Minghao Li 已提交
351 352
        taosWriteQitem(pVnode->pSyncQ, pMsg);
        break;
S
Shengliang Guan 已提交
353
      default:
S
Shengliang Guan 已提交
354
        code = -1;
S
Shengliang Guan 已提交
355 356 357
        terrno = TSDB_CODE_INVALID_PARA;
        break;
    }
S
shm  
Shengliang Guan 已提交
358
  }
S
Shengliang Guan 已提交
359

S
shm  
Shengliang Guan 已提交
360 361 362 363
  vmReleaseVnode(pMgmt, pVnode);
  return code;
}

S
Shengliang 已提交
364 365
int32_t vmPutRpcMsgToWriteQueue(SVnodeMgmt *pMgmt, SRpcMsg *pRpc) {
  return vmPutRpcMsgToQueue(pMgmt, pRpc, WRITE_QUEUE);
L
Liu Jicong 已提交
366 367
}

S
Shengliang 已提交
368
int32_t vmPutRpcMsgToSyncQueue(SVnodeMgmt *pMgmt, SRpcMsg *pRpc) { return vmPutRpcMsgToQueue(pMgmt, pRpc, SYNC_QUEUE); }
S
Shengliang Guan 已提交
369

S
Shengliang 已提交
370 371
int32_t vmPutRpcMsgToApplyQueue(SVnodeMgmt *pMgmt, SRpcMsg *pRpc) {
  return vmPutRpcMsgToQueue(pMgmt, pRpc, APPLY_QUEUE);
S
Shengliang Guan 已提交
372 373
}

S
Shengliang 已提交
374 375
int32_t vmPutRpcMsgToQueryQueue(SVnodeMgmt *pMgmt, SRpcMsg *pRpc) {
  return vmPutRpcMsgToQueue(pMgmt, pRpc, QUERY_QUEUE);
S
Shengliang Guan 已提交
376
}
S
shm  
Shengliang Guan 已提交
377

S
Shengliang 已提交
378 379
int32_t vmPutRpcMsgToFetchQueue(SVnodeMgmt *pMgmt, SRpcMsg *pRpc) {
  return vmPutRpcMsgToQueue(pMgmt, pRpc, FETCH_QUEUE);
S
Shengliang Guan 已提交
380
}
S
shm  
Shengliang Guan 已提交
381

S
Shengliang 已提交
382 383
int32_t vmPutRpcMsgToMergeQueue(SVnodeMgmt *pMgmt, SRpcMsg *pRpc) {
  return vmPutRpcMsgToQueue(pMgmt, pRpc, MERGE_QUEUE);
L
Liu Jicong 已提交
384 385
}

S
Shengliang 已提交
386
int32_t vmGetQueueSize(SVnodeMgmt *pMgmt, int32_t vgId, EQueueType qtype) {
S
Shengliang Guan 已提交
387
  int32_t    size = -1;
S
Shengliang 已提交
388
  SVnodeObj *pVnode = vmAcquireVnode(pMgmt, vgId);
S
Shengliang Guan 已提交
389 390 391
  if (pVnode != NULL) {
    switch (qtype) {
      case WRITE_QUEUE:
392
        size = taosQueueItemSize(pVnode->pWriteQ);
S
Shengliang Guan 已提交
393 394
        break;
      case SYNC_QUEUE:
395
        size = taosQueueItemSize(pVnode->pSyncQ);
S
Shengliang Guan 已提交
396 397
        break;
      case APPLY_QUEUE:
398
        size = taosQueueItemSize(pVnode->pApplyQ);
S
Shengliang Guan 已提交
399
        break;
S
Shengliang Guan 已提交
400
      case QUERY_QUEUE:
401
        size = taosQueueItemSize(pVnode->pQueryQ);
S
Shengliang Guan 已提交
402 403
        break;
      case FETCH_QUEUE:
404
        size = taosQueueItemSize(pVnode->pFetchQ);
S
Shengliang Guan 已提交
405
        break;
L
Liu Jicong 已提交
406
      case MERGE_QUEUE:
407
        size = taosQueueItemSize(pVnode->pMergeQ);
L
Liu Jicong 已提交
408
        break;
S
Shengliang Guan 已提交
409 410 411 412
      default:
        break;
    }
  }
S
Shengliang 已提交
413
  vmReleaseVnode(pMgmt, pVnode);
S
Shengliang Guan 已提交
414 415 416
  return size;
}

S
Shengliang 已提交
417
int32_t vmAllocQueue(SVnodeMgmt *pMgmt, SVnodeObj *pVnode) {
S
shm  
Shengliang Guan 已提交
418 419
  pVnode->pWriteQ = tWWorkerAllocQueue(&pMgmt->writePool, pVnode, (FItems)vmProcessWriteQueue);
  pVnode->pSyncQ = tWWorkerAllocQueue(&pMgmt->syncPool, pVnode, (FItems)vmProcessSyncQueue);
S
Shengliang Guan 已提交
420
  pVnode->pApplyQ = tWWorkerAllocQueue(&pMgmt->writePool, pVnode, (FItems)vmProcessApplyQueue);
S
shm  
Shengliang Guan 已提交
421
  pVnode->pQueryQ = tQWorkerAllocQueue(&pMgmt->queryPool, pVnode, (FItem)vmProcessQueryQueue);
S
Shengliang Guan 已提交
422 423
  pVnode->pFetchQ = tQWorkerAllocQueue(&pMgmt->fetchPool, pVnode, (FItem)vmProcessFetchQueue);
  pVnode->pMergeQ = tWWorkerAllocQueue(&pMgmt->mergePool, pVnode, (FItems)vmProcessMergeQueue);
S
shm  
Shengliang Guan 已提交
424

S
Shengliang Guan 已提交
425 426
  if (pVnode->pWriteQ == NULL || pVnode->pSyncQ == NULL || pVnode->pApplyQ == NULL || pVnode->pQueryQ == NULL ||
      pVnode->pFetchQ == NULL || pVnode->pMergeQ == NULL) {
S
shm  
Shengliang Guan 已提交
427 428 429 430
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

S
Shengliang Guan 已提交
431
  dDebug("vgId:%d, vnode queue is alloced", pVnode->vgId);
S
shm  
Shengliang Guan 已提交
432 433 434
  return 0;
}

S
Shengliang 已提交
435
void vmFreeQueue(SVnodeMgmt *pMgmt, SVnodeObj *pVnode) {
S
shm  
Shengliang Guan 已提交
436
  tWWorkerFreeQueue(&pMgmt->writePool, pVnode->pWriteQ);
S
Shengliang Guan 已提交
437
  tWWorkerFreeQueue(&pMgmt->syncPool, pVnode->pSyncQ);
S
shm  
Shengliang Guan 已提交
438
  tWWorkerFreeQueue(&pMgmt->writePool, pVnode->pApplyQ);
S
Shengliang Guan 已提交
439 440
  tQWorkerFreeQueue(&pMgmt->queryPool, pVnode->pQueryQ);
  tQWorkerFreeQueue(&pMgmt->fetchPool, pVnode->pFetchQ);
L
Liu Jicong 已提交
441
  tWWorkerFreeQueue(&pMgmt->mergePool, pVnode->pMergeQ);
S
shm  
Shengliang Guan 已提交
442 443
  pVnode->pWriteQ = NULL;
  pVnode->pSyncQ = NULL;
S
Shengliang Guan 已提交
444
  pVnode->pApplyQ = NULL;
S
shm  
Shengliang Guan 已提交
445
  pVnode->pQueryQ = NULL;
S
Shengliang Guan 已提交
446
  pVnode->pFetchQ = NULL;
L
Liu Jicong 已提交
447
  pVnode->pMergeQ = NULL;
S
Shengliang Guan 已提交
448
  dDebug("vgId:%d, vnode queue is freed", pVnode->vgId);
S
shm  
Shengliang Guan 已提交
449 450
}

S
Shengliang 已提交
451
int32_t vmStartWorker(SVnodeMgmt *pMgmt) {
S
shm  
Shengliang Guan 已提交
452 453
  SQWorkerPool *pQPool = &pMgmt->queryPool;
  pQPool->name = "vnode-query";
S
Shengliang Guan 已提交
454 455
  pQPool->min = tsNumOfVnodeQueryThreads;
  pQPool->max = tsNumOfVnodeQueryThreads;
S
shm  
Shengliang Guan 已提交
456 457
  if (tQWorkerInit(pQPool) != 0) return -1;

S
Shengliang Guan 已提交
458
  SQWorkerPool *pFPool = &pMgmt->fetchPool;
S
shm  
Shengliang Guan 已提交
459
  pFPool->name = "vnode-fetch";
S
Shengliang Guan 已提交
460 461
  pFPool->min = tsNumOfVnodeFetchThreads;
  pFPool->max = tsNumOfVnodeFetchThreads;
S
Shengliang Guan 已提交
462
  if (tQWorkerInit(pFPool) != 0) return -1;
S
shm  
Shengliang Guan 已提交
463 464 465

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

S
Shengliang Guan 已提交
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
  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 已提交
486
  if (tSingleWorkerInit(&pMgmt->mgmtWorker, &cfg) != 0) {
S
Shengliang Guan 已提交
487
    dError("failed to start vnode-mgmt worker since %s", terrstr());
S
shm  
Shengliang Guan 已提交
488 489 490
    return -1;
  }

S
Shengliang Guan 已提交
491 492 493 494 495 496 497 498 499 500
  SSingleWorkerCfg mCfg = {
      .min = 1,
      .max = 1,
      .name = "vnode-monitor",
      .fp = (FItem)vmProcessMgmtMonitorQueue,
      .param = pMgmt,
  };
  if (tSingleWorkerInit(&pMgmt->monitorWorker, &mCfg) != 0) {
    dError("failed to start mnode vnode-monitor worker since %s", terrstr());
    return -1;
501 502
  }

S
Shengliang Guan 已提交
503
  dDebug("vnode workers are initialized");
S
shm  
Shengliang Guan 已提交
504 505 506
  return 0;
}

S
Shengliang 已提交
507
void vmStopWorker(SVnodeMgmt *pMgmt) {
508
  tSingleWorkerCleanup(&pMgmt->monitorWorker);
S
Shengliang Guan 已提交
509
  tSingleWorkerCleanup(&pMgmt->mgmtWorker);
S
shm  
Shengliang Guan 已提交
510 511
  tWWorkerCleanup(&pMgmt->writePool);
  tWWorkerCleanup(&pMgmt->syncPool);
S
Shengliang Guan 已提交
512 513
  tQWorkerCleanup(&pMgmt->queryPool);
  tQWorkerCleanup(&pMgmt->fetchPool);
L
fix  
Liu Jicong 已提交
514
  tWWorkerCleanup(&pMgmt->mergePool);
S
Shengliang Guan 已提交
515
  dDebug("vnode workers are closed");
S
shm  
Shengliang Guan 已提交
516
}