vmWorker.c 16.0 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 26
      .pCont = pMsg->info.rsp,
      .contLen = pMsg->info.rspLen,
S
Shengliang Guan 已提交
27
      .info = pMsg->info,
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 35
  int32_t     code = -1;
  dTrace("msg:%p, get from vnode queue, type:%s", pMsg, TMSG_INFO(pMsg->msgType));
S
Shengliang Guan 已提交
36

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

S
Shengliang Guan 已提交
55
  if (IsReq(pMsg)) {
S
Shengliang Guan 已提交
56
    if (code != 0 && terrno != 0) code = terrno;
S
Shengliang Guan 已提交
57
    vmSendRsp(pMsg, code);
S
Shengliang Guan 已提交
58 59
  }

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

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

S
Shengliang Guan 已提交
68
  dTrace("msg:%p, get from vnode-query queue", pMsg);
S
Shengliang Guan 已提交
69
  int32_t code = vnodeProcessQueryMsg(pVnode->pImpl, pMsg);
S
Shengliang Guan 已提交
70
  if (code != 0) {
S
Shengliang Guan 已提交
71 72
    if (terrno != 0) code = terrno;
    vmSendRsp(pMsg, code);
S
Shengliang Guan 已提交
73
  }
dengyihao's avatar
dengyihao 已提交
74 75 76
  dTrace("msg:%p, is freed, code:0x%x", pMsg, code);
  rpcFreeCont(pMsg->pCont);
  taosFreeQitem(pMsg);
S
shm  
Shengliang Guan 已提交
77
}
S
shm  
Shengliang Guan 已提交
78

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

S
Shengliang Guan 已提交
82
  dTrace("msg:%p, get from vnode-fetch queue", pMsg);
S
Shengliang Guan 已提交
83
  int32_t code = vnodeProcessFetchMsg(pVnode->pImpl, pMsg, pInfo);
S
Shengliang Guan 已提交
84
  if (code != 0) {
S
Shengliang Guan 已提交
85 86
    if (terrno != 0) code = terrno;
    vmSendRsp(pMsg, code);
S
Shengliang Guan 已提交
87
  }
dengyihao's avatar
dengyihao 已提交
88 89 90
  dTrace("msg:%p, is freed, code:0x%x", pMsg, code);
  rpcFreeCont(pMsg->pCont);
  taosFreeQitem(pMsg);
S
Shengliang Guan 已提交
91 92
}

S
Shengliang Guan 已提交
93 94
static void vmProcessWriteQueue(SQueueInfo *pInfo, STaosQall *qall, int32_t numOfMsgs) {
  SVnodeObj *pVnode = pInfo->ahandle;
dengyihao's avatar
dengyihao 已提交
95
  SArray *   pArray = taosArrayInit(numOfMsgs, sizeof(SRpcMsg *));
S
Shengliang Guan 已提交
96 97 98 99
  if (pArray == NULL) {
    dError("failed to process %d msgs in write-queue since %s", numOfMsgs, terrstr());
    return;
  }
S
shm  
Shengliang Guan 已提交
100 101

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

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

M
Minghao Li 已提交
112
  for (int i = 0; i < taosArrayGetSize(pArray); i++) {
S
Shengliang Guan 已提交
113
    SRpcMsg *pMsg = *(SRpcMsg **)taosArrayGet(pArray, i);
S
Shengliang Guan 已提交
114
    SRpcMsg  rsp = {.info = pMsg->info};
M
Minghao Li 已提交
115

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

S
Shengliang Guan 已提交
134
  for (int32_t i = 0; i < numOfMsgs; i++) {
S
Shengliang Guan 已提交
135
    SRpcMsg *pMsg = *(SRpcMsg **)taosArrayGet(pArray, i);
S
shm  
Shengliang Guan 已提交
136
    dTrace("msg:%p, is freed", pMsg);
S
Shengliang Guan 已提交
137
    rpcFreeCont(pMsg->pCont);
S
shm  
Shengliang Guan 已提交
138
    taosFreeQitem(pMsg);
S
shm  
Shengliang Guan 已提交
139 140 141 142 143
  }

  taosArrayDestroy(pArray);
}

S
Shengliang Guan 已提交
144 145
static void vmProcessApplyQueue(SQueueInfo *pInfo, STaosQall *qall, int32_t numOfMsgs) {
  SVnodeObj *pVnode = pInfo->ahandle;
S
shm  
Shengliang Guan 已提交
146 147

  for (int32_t i = 0; i < numOfMsgs; ++i) {
S
Shengliang Guan 已提交
148
    SRpcMsg *pMsg = NULL;
S
shm  
Shengliang Guan 已提交
149 150
    taosGetQitem(qall, (void **)&pMsg);

151
    // init response rpc msg
S
Shengliang Guan 已提交
152
    SRpcMsg rsp = {0};
153 154

    // get original rpc msg
S
Shengliang Guan 已提交
155 156
    assert(pMsg->msgType == TDMT_VND_SYNC_APPLY_MSG);
    SyncApplyMsg *pSyncApplyMsg = syncApplyMsgFromRpcMsg2(pMsg);
157 158 159 160
    syncApplyMsgLog2("==vmProcessApplyQueue==", pSyncApplyMsg);
    SRpcMsg originalRpcMsg;
    syncApplyMsg2OriginalRpcMsg(pSyncApplyMsg, &originalRpcMsg);

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

167 168 169
    syncApplyMsgDestroy(pSyncApplyMsg);
    rpcFreeCont(originalRpcMsg.pCont);

170
    // if leader, send response
171
    if (pMsg->info.handle != NULL) {
S
Shengliang Guan 已提交
172
      rsp.info = pMsg->info;
M
Minghao Li 已提交
173
      tmsgSendRsp(&rsp);
H
Hongze Cheng 已提交
174
    }
M
Minghao Li 已提交
175

S
Shengliang Guan 已提交
176
    rpcFreeCont(pMsg->pCont);
M
Minghao Li 已提交
177
    taosFreeQitem(pMsg);
S
shm  
Shengliang Guan 已提交
178 179 180
  }
}

S
Shengliang Guan 已提交
181 182
static void vmProcessSyncQueue(SQueueInfo *pInfo, STaosQall *qall, int32_t numOfMsgs) {
  SVnodeObj *pVnode = pInfo->ahandle;
S
shm  
Shengliang Guan 已提交
183 184

  for (int32_t i = 0; i < numOfMsgs; ++i) {
S
Shengliang Guan 已提交
185
    SRpcMsg *pMsg = NULL;
S
shm  
Shengliang Guan 已提交
186 187
    taosGetQitem(qall, (void **)&pMsg);

S
Shengliang Guan 已提交
188 189
    int32_t code = vnodeProcessSyncReq(pVnode->pImpl, pMsg, NULL);
    if (code != 0) {
190
      if (pMsg->info.handle != NULL) {
S
Shengliang Guan 已提交
191 192 193 194 195
        SRpcMsg rsp = {
            .code = (terrno < 0) ? terrno : code,
            .info = pMsg->info,
        };
        dTrace("msg:%p, failed to process sync queue since %s", pMsg, terrstr());
196 197 198
        tmsgSendRsp(&rsp);
      }
    }
M
Minghao Li 已提交
199

S
Shengliang Guan 已提交
200
    rpcFreeCont(pMsg->pCont);
M
Minghao Li 已提交
201
    taosFreeQitem(pMsg);
S
shm  
Shengliang Guan 已提交
202 203 204
  }
}

L
Liu Jicong 已提交
205 206 207 208
static void vmProcessMergeQueue(SQueueInfo *pInfo, STaosQall *qall, int32_t numOfMsgs) {
  SVnodeObj *pVnode = pInfo->ahandle;

  for (int32_t i = 0; i < numOfMsgs; ++i) {
S
Shengliang Guan 已提交
209
    SRpcMsg *pMsg = NULL;
L
Liu Jicong 已提交
210 211
    taosGetQitem(qall, (void **)&pMsg);

S
Shengliang Guan 已提交
212
    dTrace("msg:%p, get from vnode-merge queue", pMsg);
S
Shengliang Guan 已提交
213
    int32_t code = vnodeProcessFetchMsg(pVnode->pImpl, pMsg, pInfo);
L
Liu Jicong 已提交
214
    if (code != 0) {
S
Shengliang Guan 已提交
215 216
      if (terrno != 0) code = terrno;
      vmSendRsp(pMsg, code);
L
Liu Jicong 已提交
217
    }
dengyihao's avatar
dengyihao 已提交
218 219 220
    dTrace("msg:%p, is freed, code:0x%x", pMsg, code);
    rpcFreeCont(pMsg->pCont);
    taosFreeQitem(pMsg);
L
Liu Jicong 已提交
221 222 223
  }
}

S
Shengliang Guan 已提交
224
static int32_t vmPutNodeMsgToQueue(SVnodeMgmt *pMgmt, SRpcMsg *pMsg, EQueueType qtype) {
dengyihao's avatar
dengyihao 已提交
225
  SRpcMsg * pRpc = pMsg;
S
Shengliang Guan 已提交
226
  SMsgHead *pHead = pRpc->pCont;
S
Shengliang Guan 已提交
227 228
  int32_t   code = 0;

D
dapan1121 已提交
229 230
  pHead->contLen = ntohl(pHead->contLen);
  pHead->vgId = ntohl(pHead->vgId);
S
shm  
Shengliang Guan 已提交
231 232 233

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

S
Shengliang Guan 已提交
238
  switch (qtype) {
S
Shengliang Guan 已提交
239
    case QUERY_QUEUE:
S
Shengliang Guan 已提交
240
      dTrace("msg:%p, put into vnode-query worker, type:%s", pMsg, TMSG_INFO(pRpc->msgType));
S
Shengliang Guan 已提交
241
      taosWriteQitem(pVnode->pQueryQ, pMsg);
S
Shengliang Guan 已提交
242
      break;
S
Shengliang Guan 已提交
243
    case FETCH_QUEUE:
S
Shengliang Guan 已提交
244
      dTrace("msg:%p, put into vnode-fetch worker, type:%s", pMsg, TMSG_INFO(pRpc->msgType));
S
Shengliang Guan 已提交
245
      taosWriteQitem(pVnode->pFetchQ, pMsg);
S
Shengliang Guan 已提交
246
      break;
S
Shengliang Guan 已提交
247
    case WRITE_QUEUE:
S
Shengliang Guan 已提交
248
      dTrace("msg:%p, put into vnode-write worker, type:%s", pMsg, TMSG_INFO(pRpc->msgType));
S
Shengliang Guan 已提交
249
      taosWriteQitem(pVnode->pWriteQ, pMsg);
S
Shengliang Guan 已提交
250 251
      break;
    case SYNC_QUEUE:
S
Shengliang Guan 已提交
252
      dTrace("msg:%p, put into vnode-sync worker, type:%s", pMsg, TMSG_INFO(pRpc->msgType));
S
Shengliang Guan 已提交
253
      taosWriteQitem(pVnode->pSyncQ, pMsg);
S
Shengliang Guan 已提交
254
      break;
L
fix  
Liu Jicong 已提交
255
    case MERGE_QUEUE:
S
Shengliang Guan 已提交
256
      dTrace("msg:%p, put into vnode-merge worker, type:%s", pMsg, TMSG_INFO(pRpc->msgType));
S
Shengliang Guan 已提交
257
      taosWriteQitem(pVnode->pMergeQ, pMsg);
L
fix  
Liu Jicong 已提交
258
      break;
S
Shengliang Guan 已提交
259
    default:
S
Shengliang Guan 已提交
260
      code = -1;
S
Shengliang Guan 已提交
261 262 263
      terrno = TSDB_CODE_INVALID_PARA;
      break;
  }
S
shm  
Shengliang Guan 已提交
264 265 266

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

S
Shengliang Guan 已提交
269
int32_t vmPutNodeMsgToSyncQueue(SVnodeMgmt *pMgmt, SRpcMsg *pMsg) {
S
Shengliang Guan 已提交
270 271
  return vmPutNodeMsgToQueue(pMgmt, pMsg, SYNC_QUEUE);
}
S
shm  
Shengliang Guan 已提交
272

S
Shengliang Guan 已提交
273
int32_t vmPutNodeMsgToWriteQueue(SVnodeMgmt *pMgmt, SRpcMsg *pMsg) {
S
Shengliang Guan 已提交
274 275
  return vmPutNodeMsgToQueue(pMgmt, pMsg, WRITE_QUEUE);
}
S
shm  
Shengliang Guan 已提交
276

S
Shengliang Guan 已提交
277
int32_t vmPutNodeMsgToQueryQueue(SVnodeMgmt *pMgmt, SRpcMsg *pMsg) {
S
Shengliang Guan 已提交
278 279
  return vmPutNodeMsgToQueue(pMgmt, pMsg, QUERY_QUEUE);
}
S
shm  
Shengliang Guan 已提交
280

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

S
Shengliang Guan 已提交
285
int32_t vmPutNodeMsgToMergeQueue(SVnodeMgmt *pMgmt, SRpcMsg *pMsg) {
S
Shengliang Guan 已提交
286 287
  return vmPutNodeMsgToQueue(pMgmt, pMsg, MERGE_QUEUE);
}
L
Liu Jicong 已提交
288

S
Shengliang Guan 已提交
289
int32_t vmPutNodeMsgToMgmtQueue(SVnodeMgmt *pMgmt, SRpcMsg *pMsg) {
S
Shengliang Guan 已提交
290
  SSingleWorker *pWorker = &pMgmt->mgmtWorker;
S
Shengliang Guan 已提交
291
  dTrace("msg:%p, put into vnode-mgmt worker, type:%s", pMsg, TMSG_INFO(pMsg->msgType));
S
Shengliang Guan 已提交
292 293
  taosWriteQitem(pWorker->queue, pMsg);
  return 0;
S
shm  
Shengliang Guan 已提交
294 295
}

S
Shengliang Guan 已提交
296
int32_t vmPutNodeMsgToMonitorQueue(SVnodeMgmt *pMgmt, SRpcMsg *pMsg) {
297
  SSingleWorker *pWorker = &pMgmt->monitorWorker;
S
Shengliang Guan 已提交
298
  dTrace("msg:%p, put into vnode-monitor worker, type:%s", pMsg, TMSG_INFO(pMsg->msgType));
299 300 301 302
  taosWriteQitem(pWorker->queue, pMsg);
  return 0;
}

S
Shengliang 已提交
303
static int32_t vmPutRpcMsgToQueue(SVnodeMgmt *pMgmt, SRpcMsg *pRpc, EQueueType qtype) {
dengyihao's avatar
dengyihao 已提交
304
  SMsgHead * pHead = pRpc->pCont;
S
shm  
Shengliang Guan 已提交
305 306 307
  SVnodeObj *pVnode = vmAcquireVnode(pMgmt, pHead->vgId);
  if (pVnode == NULL) return -1;

S
Shengliang Guan 已提交
308
  SRpcMsg *pMsg = taosAllocateQitem(sizeof(SRpcMsg), RPC_QITEM);
S
Shengliang Guan 已提交
309
  int32_t  code = 0;
S
Shengliang Guan 已提交
310

S
Shengliang Guan 已提交
311 312 313
  if (pMsg == NULL) {
    rpcFreeCont(pRpc->pCont);
    pRpc->pCont = NULL;
S
Shengliang Guan 已提交
314
    code = -1;
S
Shengliang Guan 已提交
315
  } else {
S
Shengliang Guan 已提交
316
    memcpy(pMsg, pRpc, sizeof(SRpcMsg));
S
Shengliang Guan 已提交
317
    switch (qtype) {
L
Liu Jicong 已提交
318
      case WRITE_QUEUE:
S
Shengliang Guan 已提交
319
        dTrace("msg:%p, create and put into vnode-write worker, type:%s", pMsg, TMSG_INFO(pRpc->msgType));
L
Liu Jicong 已提交
320 321
        taosWriteQitem(pVnode->pWriteQ, pMsg);
        break;
S
Shengliang Guan 已提交
322
      case QUERY_QUEUE:
S
Shengliang Guan 已提交
323
        dTrace("msg:%p, create and put into vnode-query queue, type:%s", pMsg, TMSG_INFO(pRpc->msgType));
S
Shengliang Guan 已提交
324
        taosWriteQitem(pVnode->pQueryQ, pMsg);
S
Shengliang Guan 已提交
325
        break;
S
Shengliang Guan 已提交
326
      case FETCH_QUEUE:
S
Shengliang Guan 已提交
327
        dTrace("msg:%p, create and put into vnode-fetch queue, type:%s", pMsg, TMSG_INFO(pRpc->msgType));
S
Shengliang Guan 已提交
328
        taosWriteQitem(pVnode->pFetchQ, pMsg);
S
Shengliang Guan 已提交
329
        break;
S
Shengliang Guan 已提交
330
      case APPLY_QUEUE:
S
Shengliang Guan 已提交
331
        dTrace("msg:%p, create and put into vnode-apply queue, type:%s", pMsg, TMSG_INFO(pRpc->msgType));
S
Shengliang Guan 已提交
332
        taosWriteQitem(pVnode->pApplyQ, pMsg);
S
Shengliang Guan 已提交
333
        break;
L
Liu Jicong 已提交
334
      case MERGE_QUEUE:
S
Shengliang Guan 已提交
335
        dTrace("msg:%p, create and put into vnode-merge queue, type:%s", pMsg, TMSG_INFO(pRpc->msgType));
S
Shengliang Guan 已提交
336
        taosWriteQitem(pVnode->pMergeQ, pMsg);
L
Liu Jicong 已提交
337
        break;
S
Shengliang Guan 已提交
338
      case SYNC_QUEUE:
S
Shengliang Guan 已提交
339
        dTrace("msg:%p, create and put into vnode-sync queue, type:%s", pMsg, TMSG_INFO(pRpc->msgType));
M
Minghao Li 已提交
340 341
        taosWriteQitem(pVnode->pSyncQ, pMsg);
        break;
S
Shengliang Guan 已提交
342
      default:
S
Shengliang Guan 已提交
343
        code = -1;
S
Shengliang Guan 已提交
344 345 346
        terrno = TSDB_CODE_INVALID_PARA;
        break;
    }
S
shm  
Shengliang Guan 已提交
347
  }
S
Shengliang Guan 已提交
348

S
shm  
Shengliang Guan 已提交
349 350 351 352
  vmReleaseVnode(pMgmt, pVnode);
  return code;
}

S
Shengliang 已提交
353 354
int32_t vmPutRpcMsgToWriteQueue(SVnodeMgmt *pMgmt, SRpcMsg *pRpc) {
  return vmPutRpcMsgToQueue(pMgmt, pRpc, WRITE_QUEUE);
L
Liu Jicong 已提交
355 356
}

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

S
Shengliang 已提交
359 360
int32_t vmPutRpcMsgToApplyQueue(SVnodeMgmt *pMgmt, SRpcMsg *pRpc) {
  return vmPutRpcMsgToQueue(pMgmt, pRpc, APPLY_QUEUE);
S
Shengliang Guan 已提交
361 362
}

S
Shengliang 已提交
363 364
int32_t vmPutRpcMsgToQueryQueue(SVnodeMgmt *pMgmt, SRpcMsg *pRpc) {
  return vmPutRpcMsgToQueue(pMgmt, pRpc, QUERY_QUEUE);
S
Shengliang Guan 已提交
365
}
S
shm  
Shengliang Guan 已提交
366

S
Shengliang 已提交
367 368
int32_t vmPutRpcMsgToFetchQueue(SVnodeMgmt *pMgmt, SRpcMsg *pRpc) {
  return vmPutRpcMsgToQueue(pMgmt, pRpc, FETCH_QUEUE);
S
Shengliang Guan 已提交
369
}
S
shm  
Shengliang Guan 已提交
370

S
Shengliang 已提交
371 372
int32_t vmPutRpcMsgToMergeQueue(SVnodeMgmt *pMgmt, SRpcMsg *pRpc) {
  return vmPutRpcMsgToQueue(pMgmt, pRpc, MERGE_QUEUE);
L
Liu Jicong 已提交
373 374
}

S
Shengliang 已提交
375
int32_t vmGetQueueSize(SVnodeMgmt *pMgmt, int32_t vgId, EQueueType qtype) {
S
Shengliang Guan 已提交
376
  int32_t    size = -1;
S
Shengliang 已提交
377
  SVnodeObj *pVnode = vmAcquireVnode(pMgmt, vgId);
S
Shengliang Guan 已提交
378 379 380
  if (pVnode != NULL) {
    switch (qtype) {
      case WRITE_QUEUE:
381
        size = taosQueueItemSize(pVnode->pWriteQ);
S
Shengliang Guan 已提交
382 383
        break;
      case SYNC_QUEUE:
384
        size = taosQueueItemSize(pVnode->pSyncQ);
S
Shengliang Guan 已提交
385 386
        break;
      case APPLY_QUEUE:
387
        size = taosQueueItemSize(pVnode->pApplyQ);
S
Shengliang Guan 已提交
388
        break;
S
Shengliang Guan 已提交
389
      case QUERY_QUEUE:
390
        size = taosQueueItemSize(pVnode->pQueryQ);
S
Shengliang Guan 已提交
391 392
        break;
      case FETCH_QUEUE:
393
        size = taosQueueItemSize(pVnode->pFetchQ);
S
Shengliang Guan 已提交
394
        break;
L
Liu Jicong 已提交
395
      case MERGE_QUEUE:
396
        size = taosQueueItemSize(pVnode->pMergeQ);
L
Liu Jicong 已提交
397
        break;
S
Shengliang Guan 已提交
398 399 400 401
      default:
        break;
    }
  }
S
Shengliang 已提交
402
  vmReleaseVnode(pMgmt, pVnode);
S
Shengliang Guan 已提交
403 404 405
  return size;
}

S
Shengliang 已提交
406
int32_t vmAllocQueue(SVnodeMgmt *pMgmt, SVnodeObj *pVnode) {
S
shm  
Shengliang Guan 已提交
407 408
  pVnode->pWriteQ = tWWorkerAllocQueue(&pMgmt->writePool, pVnode, (FItems)vmProcessWriteQueue);
  pVnode->pSyncQ = tWWorkerAllocQueue(&pMgmt->syncPool, pVnode, (FItems)vmProcessSyncQueue);
S
Shengliang Guan 已提交
409
  pVnode->pApplyQ = tWWorkerAllocQueue(&pMgmt->writePool, pVnode, (FItems)vmProcessApplyQueue);
S
shm  
Shengliang Guan 已提交
410
  pVnode->pQueryQ = tQWorkerAllocQueue(&pMgmt->queryPool, pVnode, (FItem)vmProcessQueryQueue);
S
Shengliang Guan 已提交
411 412
  pVnode->pFetchQ = tQWorkerAllocQueue(&pMgmt->fetchPool, pVnode, (FItem)vmProcessFetchQueue);
  pVnode->pMergeQ = tWWorkerAllocQueue(&pMgmt->mergePool, pVnode, (FItems)vmProcessMergeQueue);
S
shm  
Shengliang Guan 已提交
413

S
Shengliang Guan 已提交
414 415
  if (pVnode->pWriteQ == NULL || pVnode->pSyncQ == NULL || pVnode->pApplyQ == NULL || pVnode->pQueryQ == NULL ||
      pVnode->pFetchQ == NULL || pVnode->pMergeQ == NULL) {
S
shm  
Shengliang Guan 已提交
416 417 418 419
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

S
Shengliang Guan 已提交
420
  dDebug("vgId:%d, queue is alloced", pVnode->vgId);
S
shm  
Shengliang Guan 已提交
421 422 423
  return 0;
}

S
Shengliang 已提交
424
void vmFreeQueue(SVnodeMgmt *pMgmt, SVnodeObj *pVnode) {
S
shm  
Shengliang Guan 已提交
425
  tWWorkerFreeQueue(&pMgmt->writePool, pVnode->pWriteQ);
S
Shengliang Guan 已提交
426
  tWWorkerFreeQueue(&pMgmt->syncPool, pVnode->pSyncQ);
S
shm  
Shengliang Guan 已提交
427
  tWWorkerFreeQueue(&pMgmt->writePool, pVnode->pApplyQ);
S
Shengliang Guan 已提交
428 429
  tQWorkerFreeQueue(&pMgmt->queryPool, pVnode->pQueryQ);
  tQWorkerFreeQueue(&pMgmt->fetchPool, pVnode->pFetchQ);
L
Liu Jicong 已提交
430
  tWWorkerFreeQueue(&pMgmt->mergePool, pVnode->pMergeQ);
S
shm  
Shengliang Guan 已提交
431 432
  pVnode->pWriteQ = NULL;
  pVnode->pSyncQ = NULL;
S
Shengliang Guan 已提交
433
  pVnode->pApplyQ = NULL;
S
shm  
Shengliang Guan 已提交
434
  pVnode->pQueryQ = NULL;
S
Shengliang Guan 已提交
435
  pVnode->pFetchQ = NULL;
L
Liu Jicong 已提交
436
  pVnode->pMergeQ = NULL;
S
Shengliang Guan 已提交
437
  dDebug("vgId:%d, queue is freed", pVnode->vgId);
S
shm  
Shengliang Guan 已提交
438 439
}

S
Shengliang 已提交
440
int32_t vmStartWorker(SVnodeMgmt *pMgmt) {
S
shm  
Shengliang Guan 已提交
441 442
  SQWorkerPool *pQPool = &pMgmt->queryPool;
  pQPool->name = "vnode-query";
S
Shengliang Guan 已提交
443 444
  pQPool->min = tsNumOfVnodeQueryThreads;
  pQPool->max = tsNumOfVnodeQueryThreads;
S
shm  
Shengliang Guan 已提交
445 446
  if (tQWorkerInit(pQPool) != 0) return -1;

S
Shengliang Guan 已提交
447
  SQWorkerPool *pFPool = &pMgmt->fetchPool;
S
shm  
Shengliang Guan 已提交
448
  pFPool->name = "vnode-fetch";
S
Shengliang Guan 已提交
449 450
  pFPool->min = tsNumOfVnodeFetchThreads;
  pFPool->max = tsNumOfVnodeFetchThreads;
S
Shengliang Guan 已提交
451
  if (tQWorkerInit(pFPool) != 0) return -1;
S
shm  
Shengliang Guan 已提交
452 453 454

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

S
Shengliang Guan 已提交
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
  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 已提交
475
  if (tSingleWorkerInit(&pMgmt->mgmtWorker, &cfg) != 0) {
S
Shengliang Guan 已提交
476
    dError("failed to start vnode-mgmt worker since %s", terrstr());
S
shm  
Shengliang Guan 已提交
477 478 479
    return -1;
  }

S
Shengliang Guan 已提交
480 481 482 483 484 485 486 487
  SSingleWorkerCfg mCfg = {
      .min = 1,
      .max = 1,
      .name = "vnode-monitor",
      .fp = (FItem)vmProcessMgmtMonitorQueue,
      .param = pMgmt,
  };
  if (tSingleWorkerInit(&pMgmt->monitorWorker, &mCfg) != 0) {
S
Shengliang Guan 已提交
488
    dError("failed to start vnode-monitor worker since %s", terrstr());
S
Shengliang Guan 已提交
489
    return -1;
490 491
  }

S
Shengliang Guan 已提交
492
  dDebug("vnode workers are initialized");
S
shm  
Shengliang Guan 已提交
493 494 495
  return 0;
}

S
Shengliang 已提交
496
void vmStopWorker(SVnodeMgmt *pMgmt) {
497
  tSingleWorkerCleanup(&pMgmt->monitorWorker);
S
Shengliang Guan 已提交
498
  tSingleWorkerCleanup(&pMgmt->mgmtWorker);
S
shm  
Shengliang Guan 已提交
499 500
  tWWorkerCleanup(&pMgmt->writePool);
  tWWorkerCleanup(&pMgmt->syncPool);
S
Shengliang Guan 已提交
501 502
  tQWorkerCleanup(&pMgmt->queryPool);
  tQWorkerCleanup(&pMgmt->fetchPool);
L
fix  
Liu Jicong 已提交
503
  tWWorkerCleanup(&pMgmt->mergePool);
S
Shengliang Guan 已提交
504
  dDebug("vnode workers are closed");
S
shm  
Shengliang Guan 已提交
505
}