vmWorker.c 16.1 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-mgmt/monitor 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 62
  }

  dTrace("msg:%p, is freed, result:0x%04x:%s", pMsg, code & 0XFFFF, tstrerror(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, will be processed in 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, result:0x%04x:%s", pMsg, code & 0XFFFF, tstrerror(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, will be processed in 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, result:0x%04x:%s", pMsg, code & 0XFFFF, tstrerror(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
static void vmProcessWriteQueue(SQueueInfo *pInfo, STaosQall *qall, int32_t numOfMsgs) {
  SVnodeObj *pVnode = pInfo->ahandle;
H
Hongze Cheng 已提交
99
  SRpcMsg    rsp;
S
Shengliang Guan 已提交
100

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

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

M
Minghao Li 已提交
118
  for (int i = 0; i < taosArrayGetSize(pArray); i++) {
S
Shengliang Guan 已提交
119
    SRpcMsg *pMsg;
M
Minghao Li 已提交
120
    SRpcMsg  *pRpc;
H
Hongze Cheng 已提交
121

S
Shengliang Guan 已提交
122 123
    pMsg = *(SRpcMsg **)taosArrayGet(pArray, i);
    pRpc = pMsg;
M
Minghao Li 已提交
124

S
Shengliang Guan 已提交
125
    rsp.info = pRpc->info;
H
Hongze Cheng 已提交
126 127
    rsp.pCont = NULL;
    rsp.contLen = 0;
M
Minghao Li 已提交
128

H
Hongze Cheng 已提交
129
    int32_t ret = syncPropose(vnodeGetSyncHandle(pVnode->pImpl), pRpc, false);
M
Minghao Li 已提交
130
    if (ret == TAOS_SYNC_PROPOSE_NOT_LEADER) {
M
Minghao Li 已提交
131 132
      // rsp.code = TSDB_CODE_SYN_NOT_LEADER;
      // tmsgSendRsp(&rsp);
M
Minghao Li 已提交
133
      dTrace("syncPropose not leader redirect, vgId:%d ", syncGetVgId(vnodeGetSyncHandle(pVnode->pImpl)));
M
Minghao Li 已提交
134 135 136
      rsp.code = TSDB_CODE_RPC_REDIRECT;
      SEpSet newEpSet;
      syncGetEpSet(vnodeGetSyncHandle(pVnode->pImpl), &newEpSet);
M
Minghao Li 已提交
137
      newEpSet.inUse = (newEpSet.inUse + 1) % newEpSet.numOfEps;
M
Minghao Li 已提交
138
      tmsgSendRedirectRsp(&rsp, &newEpSet);
M
Minghao Li 已提交
139

M
Minghao Li 已提交
140
    } else if (ret == TAOS_SYNC_PROPOSE_OTHER_ERROR) {
M
Minghao Li 已提交
141
      rsp.code = TSDB_CODE_SYN_INTERNAL_ERROR;
H
Hongze Cheng 已提交
142
      tmsgSendRsp(&rsp);
M
Minghao Li 已提交
143 144 145
    } else if (ret == TAOS_SYNC_PROPOSE_SUCCESS) {
      // ok
      // send response in applyQ
S
shm  
Shengliang Guan 已提交
146
    } else {
M
Minghao Li 已提交
147
      assert(0);
S
shm  
Shengliang Guan 已提交
148 149 150
    }
  }

S
Shengliang Guan 已提交
151
  for (int32_t i = 0; i < numOfMsgs; i++) {
S
Shengliang Guan 已提交
152
    SRpcMsg *pMsg = *(SRpcMsg **)taosArrayGet(pArray, i);
S
shm  
Shengliang Guan 已提交
153
    dTrace("msg:%p, is freed", pMsg);
S
Shengliang Guan 已提交
154
    rpcFreeCont(pMsg->pCont);
S
shm  
Shengliang Guan 已提交
155
    taosFreeQitem(pMsg);
S
shm  
Shengliang Guan 已提交
156 157 158 159 160
  }

  taosArrayDestroy(pArray);
}

S
Shengliang Guan 已提交
161 162
static void vmProcessApplyQueue(SQueueInfo *pInfo, STaosQall *qall, int32_t numOfMsgs) {
  SVnodeObj *pVnode = pInfo->ahandle;
S
Shengliang Guan 已提交
163
  SRpcMsg  *pMsg = NULL;
H
Hongze Cheng 已提交
164
  SRpcMsg    rsp;
S
shm  
Shengliang Guan 已提交
165 166 167 168

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

169
    // init response rpc msg
M
Minghao Li 已提交
170 171 172
    rsp.code = 0;
    rsp.pCont = NULL;
    rsp.contLen = 0;
173 174

    // get original rpc msg
S
Shengliang Guan 已提交
175 176
    assert(pMsg->msgType == TDMT_VND_SYNC_APPLY_MSG);
    SyncApplyMsg *pSyncApplyMsg = syncApplyMsgFromRpcMsg2(pMsg);
177 178 179 180
    syncApplyMsgLog2("==vmProcessApplyQueue==", pSyncApplyMsg);
    SRpcMsg originalRpcMsg;
    syncApplyMsg2OriginalRpcMsg(pSyncApplyMsg, &originalRpcMsg);

181
    // apply data into tsdb
182
    if (vnodeProcessWriteReq(pVnode->pImpl, &originalRpcMsg, pSyncApplyMsg->fsmMeta.index, &rsp) < 0) {
M
Minghao Li 已提交
183 184 185 186
      rsp.code = terrno;
      dTrace("vnodeProcessWriteReq error, code:%d", terrno);
    }

187 188 189
    syncApplyMsgDestroy(pSyncApplyMsg);
    rpcFreeCont(originalRpcMsg.pCont);

190
    // if leader, send response
S
Shengliang Guan 已提交
191
    if (pMsg->info.handle != NULL && pMsg->info.ahandle != NULL) {
S
Shengliang Guan 已提交
192
      rsp.info = pMsg->info;
M
Minghao Li 已提交
193
      tmsgSendRsp(&rsp);
H
Hongze Cheng 已提交
194
    }
M
Minghao Li 已提交
195

S
Shengliang Guan 已提交
196
    rpcFreeCont(pMsg->pCont);
M
Minghao Li 已提交
197
    taosFreeQitem(pMsg);
S
shm  
Shengliang Guan 已提交
198 199 200
  }
}

S
Shengliang Guan 已提交
201 202
static void vmProcessSyncQueue(SQueueInfo *pInfo, STaosQall *qall, int32_t numOfMsgs) {
  SVnodeObj *pVnode = pInfo->ahandle;
S
Shengliang Guan 已提交
203
  SRpcMsg  *pMsg = NULL;
S
shm  
Shengliang Guan 已提交
204 205 206 207 208 209

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

    // todo
    SRpcMsg *pRsp = NULL;
S
Shengliang Guan 已提交
210
    (void)vnodeProcessSyncReq(pVnode->pImpl, pMsg, &pRsp);
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 224

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

    dTrace("msg:%p, will be processed in 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);

L
Liu Jicong 已提交
230
      dTrace("msg:%p, is freed, result:0x%04x:%s", pMsg, code & 0XFFFF, tstrerror(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) {
247
    dError("vgId:%d, failed to write msg:%p to 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:
253
      dTrace("msg:%p, type:%s will be written into vnode-query queue", 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:
257
      dTrace("msg:%p, type:%s will be written into vnode-fetch queue", 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:
261
      dTrace("msg:%p, type:%s will be written into vnode-write queue", pMsg, TMSG_INFO(pRpc->msgType));
S
Shengliang Guan 已提交
262
      taosWriteQitem(pVnode->pWriteQ, pMsg);
S
Shengliang Guan 已提交
263 264
      break;
    case SYNC_QUEUE:
265
      dTrace("msg:%p, type:%s will be written into vnode-sync queue", 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:
269
      dTrace("msg:%p, type:%s will be written into vnode-merge queue", 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, will be put into vnode-mgmt queue, worker:%s", pMsg, pWorker->name);
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, will be put into vnode-monitor queue, worker:%s", pMsg, pWorker->name);
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 325
  int32_t   code = 0;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

S
Shengliang Guan 已提交
493 494 495 496 497 498 499 500 501 502
  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;
503 504
  }

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

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