vmWorker.c 14.6 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

S
Shengliang Guan 已提交
19
static inline void vmSendRsp(SRpcMsg *pMsg, int32_t code) {
S
Shengliang Guan 已提交
20
  if (pMsg->info.handle == NULL) return;
S
Shengliang Guan 已提交
21 22
  SRpcMsg rsp = {
      .code = code,
S
Shengliang Guan 已提交
23 24
      .pCont = pMsg->info.rsp,
      .contLen = pMsg->info.rspLen,
S
Shengliang Guan 已提交
25
      .info = pMsg->info,
S
Shengliang Guan 已提交
26
  };
S
Shengliang Guan 已提交
27
  tmsgSendRsp(&rsp);
S
Shengliang Guan 已提交
28 29
}

S
Shengliang Guan 已提交
30
static void vmProcessMgmtQueue(SQueueInfo *pInfo, SRpcMsg *pMsg) {
S
Shengliang Guan 已提交
31 32 33
  SVnodeMgmt     *pMgmt = pInfo->ahandle;
  int32_t         code = -1;
  const STraceId *trace = &pMsg->info.traceId;
S
Shengliang Guan 已提交
34

dengyihao's avatar
dengyihao 已提交
35
  dGTrace("msg:%p, get from vnode-mgmt queue", pMsg);
S
Shengliang Guan 已提交
36
  switch (pMsg->msgType) {
S
Shengliang Guan 已提交
37 38 39 40 41 42
    case TDMT_DND_CREATE_VNODE:
      code = vmProcessCreateVnodeReq(pMgmt, pMsg);
      break;
    case TDMT_DND_DROP_VNODE:
      code = vmProcessDropVnodeReq(pMgmt, pMsg);
      break;
S
Shengliang Guan 已提交
43
    case TDMT_VND_ALTER_REPLICA:
44
      code = vmProcessAlterVnodeReplicaReq(pMgmt, pMsg);
S
Shengliang Guan 已提交
45
      break;
46 47 48
    case TDMT_VND_DISABLE_WRITE:
      code = vmProcessDisableVnodeWriteReq(pMgmt, pMsg);
      break;
49 50 51
    case TDMT_VND_ALTER_HASHRANGE:
      code = vmProcessAlterHashRangeReq(pMgmt, pMsg);
      break;
C
cadem 已提交
52 53 54
    case TDMT_DND_ALTER_VNODE_TYPE:
      code = vmProcessAlterVnodeTypeReq(pMgmt, pMsg);
      break;
S
Shengliang Guan 已提交
55 56
    default:
      terrno = TSDB_CODE_MSG_NOT_PROCESSED;
S
Shengliang Guan 已提交
57
      dGError("msg:%p, not processed in vnode-mgmt queue", pMsg);
S
Shengliang Guan 已提交
58 59
  }

S
Shengliang Guan 已提交
60
  if (IsReq(pMsg)) {
61 62
    if (code != 0) {
      if (terrno != 0) code = terrno;
63
      dGError("msg:%p, failed to process since %s, type:%s", pMsg, terrstr(code), TMSG_INFO(pMsg->msgType));
S
Shengliang Guan 已提交
64
    }
S
Shengliang Guan 已提交
65
    vmSendRsp(pMsg, code);
S
Shengliang Guan 已提交
66 67
  }

S
Shengliang Guan 已提交
68
  dGTrace("msg:%p, is freed, code:0x%x", pMsg, code);
S
Shengliang Guan 已提交
69
  rpcFreeCont(pMsg->pCont);
S
Shengliang Guan 已提交
70 71 72
  taosFreeQitem(pMsg);
}

S
Shengliang Guan 已提交
73
static void vmProcessQueryQueue(SQueueInfo *pInfo, SRpcMsg *pMsg) {
S
Shengliang Guan 已提交
74 75
  SVnodeObj      *pVnode = pInfo->ahandle;
  const STraceId *trace = &pMsg->info.traceId;
S
Shengliang Guan 已提交
76

S
Shengliang Guan 已提交
77
  dGTrace("vgId:%d, msg:%p get from vnode-query queue", pVnode->vgId, pMsg);
S
Shengliang Guan 已提交
78
  int32_t code = vnodeProcessQueryMsg(pVnode->pImpl, pMsg);
S
Shengliang Guan 已提交
79
  if (code != 0) {
S
Shengliang Guan 已提交
80
    if (terrno != 0) code = terrno;
C
Cary Xu 已提交
81
    dGError("vgId:%d, msg:%p failed to query since %s", pVnode->vgId, pMsg, terrstr(code));
S
Shengliang Guan 已提交
82
    vmSendRsp(pMsg, code);
S
Shengliang Guan 已提交
83
  }
S
Shengliang Guan 已提交
84

S
Shengliang Guan 已提交
85
  dGTrace("vgId:%d, msg:%p is freed, code:0x%x", pVnode->vgId, pMsg, code);
dengyihao's avatar
dengyihao 已提交
86 87
  rpcFreeCont(pMsg->pCont);
  taosFreeQitem(pMsg);
S
shm  
Shengliang Guan 已提交
88
}
S
shm  
Shengliang Guan 已提交
89

S
Shengliang Guan 已提交
90
static void vmProcessStreamQueue(SQueueInfo *pInfo, SRpcMsg *pMsg) {
S
Shengliang Guan 已提交
91 92
  SVnodeObj      *pVnode = pInfo->ahandle;
  const STraceId *trace = &pMsg->info.traceId;
S
Shengliang Guan 已提交
93

S
Shengliang Guan 已提交
94
  dGTrace("vgId:%d, msg:%p get from vnode-stream queue", pVnode->vgId, pMsg);
S
Shengliang Guan 已提交
95
  int32_t code = vnodeProcessFetchMsg(pVnode->pImpl, pMsg, pInfo);
S
Shengliang Guan 已提交
96
  if (code != 0) {
S
Shengliang Guan 已提交
97
    if (terrno != 0) code = terrno;
L
Liu Jicong 已提交
98 99
    dGError("vgId:%d, msg:%p failed to process stream msg %s since %s", pVnode->vgId, pMsg, TMSG_INFO(pMsg->msgType),
            terrstr(code));
S
Shengliang Guan 已提交
100
    vmSendRsp(pMsg, code);
S
Shengliang Guan 已提交
101
  }
S
Shengliang Guan 已提交
102

S
Shengliang Guan 已提交
103
  dGTrace("vgId:%d, msg:%p is freed, code:0x%x", pVnode->vgId, pMsg, code);
dengyihao's avatar
dengyihao 已提交
104 105
  rpcFreeCont(pMsg->pCont);
  taosFreeQitem(pMsg);
S
Shengliang Guan 已提交
106 107
}

108 109 110
static void vmProcessFetchQueue(SQueueInfo *pInfo, STaosQall *qall, int32_t numOfMsgs) {
  SVnodeObj *pVnode = pInfo->ahandle;
  SRpcMsg   *pMsg = NULL;
S
Shengliang Guan 已提交
111

112 113 114 115
  for (int32_t i = 0; i < numOfMsgs; ++i) {
    if (taosGetQitem(qall, (void **)&pMsg) == 0) continue;
    const STraceId *trace = &pMsg->info.traceId;
    dGTrace("vgId:%d, msg:%p get from vnode-fetch queue", pVnode->vgId, pMsg);
S
Shengliang Guan 已提交
116

117 118 119
    int32_t code = vnodeProcessFetchMsg(pVnode->pImpl, pMsg, pInfo);
    if (code != 0) {
      if (terrno != 0) code = terrno;
120
      dGError("vnodeProcessFetchMsg vgId:%d, msg:%p failed to fetch since %s", pVnode->vgId, pMsg, terrstr());
121 122 123
      vmSendRsp(pMsg, code);
    }

124
    dGTrace("vnodeProcessFetchMsg vgId:%d, msg:%p is freed, code:0x%x", pVnode->vgId, pMsg, code);
125 126 127
    rpcFreeCont(pMsg->pCont);
    taosFreeQitem(pMsg);
  }
S
Shengliang Guan 已提交
128 129
}

S
Shengliang Guan 已提交
130 131
static void vmProcessSyncQueue(SQueueInfo *pInfo, STaosQall *qall, int32_t numOfMsgs) {
  SVnodeObj *pVnode = pInfo->ahandle;
L
Liu Jicong 已提交
132
  SRpcMsg   *pMsg = NULL;
S
shm  
Shengliang Guan 已提交
133 134

  for (int32_t i = 0; i < numOfMsgs; ++i) {
S
Shengliang Guan 已提交
135
    if (taosGetQitem(qall, (void **)&pMsg) == 0) continue;
S
Shengliang Guan 已提交
136 137
    const STraceId *trace = &pMsg->info.traceId;
    dGTrace("vgId:%d, msg:%p get from vnode-sync queue", pVnode->vgId, pMsg);
S
shm  
Shengliang Guan 已提交
138

139
    int32_t code = vnodeProcessSyncMsg(pVnode->pImpl, pMsg, NULL);  // no response here
S
Shengliang Guan 已提交
140
    dGTrace("vgId:%d, msg:%p is freed, code:0x%x", pVnode->vgId, pMsg, code);
S
Shengliang Guan 已提交
141
    rpcFreeCont(pMsg->pCont);
M
Minghao Li 已提交
142
    taosFreeQitem(pMsg);
S
shm  
Shengliang Guan 已提交
143 144 145
  }
}

146 147 148 149 150 151 152
static void vmSendResponse(SRpcMsg *pMsg) {
  if (pMsg->info.handle) {
    SRpcMsg rsp = {.info = pMsg->info, .code = terrno};
    rpcSendResponse(&rsp);
  }
}

S
Shengliang Guan 已提交
153
static int32_t vmPutMsgToQueue(SVnodeMgmt *pMgmt, SRpcMsg *pMsg, EQueueType qtype) {
S
Shengliang Guan 已提交
154
  const STraceId *trace = &pMsg->info.traceId;
155 156 157 158 159 160
  if (pMsg->contLen < sizeof(SMsgHead)) {
    dGError("invalid rpc msg with no msg head at pCont. pMsg:%p, type:%s, contLen:%d", pMsg, TMSG_INFO(pMsg->msgType),
            pMsg->contLen);
    return -1;
  }

L
Liu Jicong 已提交
161 162
  SMsgHead *pHead = pMsg->pCont;
  int32_t   code = 0;
S
Shengliang Guan 已提交
163

D
dapan1121 已提交
164 165
  pHead->contLen = ntohl(pHead->contLen);
  pHead->vgId = ntohl(pHead->vgId);
S
shm  
Shengliang Guan 已提交
166 167 168

  SVnodeObj *pVnode = vmAcquireVnode(pMgmt, pHead->vgId);
  if (pVnode == NULL) {
169 170
    dGWarn("vgId:%d, msg:%p failed to put into vnode queue since %s, type:%s qtype:%d contLen:%d", pHead->vgId, pMsg,
           terrstr(), TMSG_INFO(pMsg->msgType), qtype, pHead->contLen);
171 172
    terrno = (terrno != 0) ? terrno : -1;
    return terrno;
S
shm  
Shengliang Guan 已提交
173 174
  }

S
Shengliang Guan 已提交
175
  switch (qtype) {
S
Shengliang Guan 已提交
176
    case QUERY_QUEUE:
177 178 179 180 181 182 183
      code = vnodePreprocessQueryMsg(pVnode->pImpl, pMsg);
      if (code) {
        dError("vgId:%d, msg:%p preprocess query msg failed since %s", pVnode->vgId, pMsg, terrstr(code));
      } else {
        dGTrace("vgId:%d, msg:%p put into vnode-query queue", pVnode->vgId, pMsg);
        taosWriteQitem(pVnode->pQueryQ, pMsg);
      }
S
Shengliang Guan 已提交
184
      break;
S
Shengliang Guan 已提交
185 186
    case STREAM_QUEUE:
      dGTrace("vgId:%d, msg:%p put into vnode-stream queue", pVnode->vgId, pMsg);
L
Liu Jicong 已提交
187 188 189 190 191
      if (pMsg->msgType == TDMT_STREAM_TASK_DISPATCH) {
        vnodeEnqueueStreamMsg(pVnode->pImpl, pMsg);
      } else {
        taosWriteQitem(pVnode->pStreamQ, pMsg);
      }
S
Shengliang Guan 已提交
192
      break;
S
Shengliang Guan 已提交
193
    case FETCH_QUEUE:
S
Shengliang Guan 已提交
194
      dGTrace("vgId:%d, msg:%p put into vnode-fetch queue", pVnode->vgId, pMsg);
S
Shengliang Guan 已提交
195
      taosWriteQitem(pVnode->pFetchQ, pMsg);
S
Shengliang Guan 已提交
196
      break;
S
Shengliang Guan 已提交
197
    case WRITE_QUEUE:
198 199
      if (!osDataSpaceSufficient()) {
        terrno = TSDB_CODE_NO_ENOUGH_DISKSPACE;
wafwerar's avatar
wafwerar 已提交
200
        code = terrno;
C
Cary Xu 已提交
201
        dError("vgId:%d, msg:%p put into vnode-write queue failed since %s", pVnode->vgId, pMsg, terrstr(code));
202 203 204
        break;
      }
      if (pMsg->msgType == TDMT_VND_SUBMIT && (grantCheck(TSDB_GRANT_STORAGE) != TSDB_CODE_SUCCESS)) {
C
Cary Xu 已提交
205 206
        terrno = TSDB_CODE_VND_NO_WRITE_AUTH;
        code = terrno;
C
Cary Xu 已提交
207
        dDebug("vgId:%d, msg:%p put into vnode-write queue failed since %s", pVnode->vgId, pMsg, terrstr(code));
208 209 210 211 212 213
        break;
      }
      if (pMsg->msgType != TDMT_VND_ALTER_CONFIRM && pVnode->disable) {
        dDebug("vgId:%d, msg:%p put into vnode-write queue failed since its disable", pVnode->vgId, pMsg);
        terrno = TSDB_CODE_VND_STOPPED;
        break;
C
Cary Xu 已提交
214
      }
215 216
      dGTrace("vgId:%d, msg:%p put into vnode-write queue", pVnode->vgId, pMsg);
      taosWriteQitem(pVnode->pWriteW.queue, pMsg);
S
Shengliang Guan 已提交
217 218
      break;
    case SYNC_QUEUE:
S
Shengliang Guan 已提交
219
      dGTrace("vgId:%d, msg:%p put into vnode-sync queue", pVnode->vgId, pMsg);
220
      taosWriteQitem(pVnode->pSyncW.queue, pMsg);
S
Shengliang Guan 已提交
221
      break;
222 223 224
    case SYNC_RD_QUEUE:
      dGTrace("vgId:%d, msg:%p put into vnode-sync-rd queue", pVnode->vgId, pMsg);
      taosWriteQitem(pVnode->pSyncRdW.queue, pMsg);
225
      break;
S
Shengliang Guan 已提交
226
    case APPLY_QUEUE:
S
Shengliang Guan 已提交
227
      dGTrace("vgId:%d, msg:%p put into vnode-apply queue", pVnode->vgId, pMsg);
228
      taosWriteQitem(pVnode->pApplyW.queue, pMsg);
S
Shengliang Guan 已提交
229
      break;
S
Shengliang Guan 已提交
230
    default:
S
Shengliang Guan 已提交
231
      code = -1;
S
Shengliang Guan 已提交
232 233 234
      terrno = TSDB_CODE_INVALID_PARA;
      break;
  }
S
shm  
Shengliang Guan 已提交
235 236 237

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

240
int32_t vmPutMsgToSyncRdQueue(SVnodeMgmt *pMgmt, SRpcMsg *pMsg) { return vmPutMsgToQueue(pMgmt, pMsg, SYNC_RD_QUEUE); }
S
shm  
Shengliang Guan 已提交
241

S
Shengliang Guan 已提交
242
int32_t vmPutMsgToSyncQueue(SVnodeMgmt *pMgmt, SRpcMsg *pMsg) { return vmPutMsgToQueue(pMgmt, pMsg, SYNC_QUEUE); }
243

S
Shengliang Guan 已提交
244
int32_t vmPutMsgToWriteQueue(SVnodeMgmt *pMgmt, SRpcMsg *pMsg) { return vmPutMsgToQueue(pMgmt, pMsg, WRITE_QUEUE); }
S
shm  
Shengliang Guan 已提交
245

S
Shengliang Guan 已提交
246
int32_t vmPutMsgToQueryQueue(SVnodeMgmt *pMgmt, SRpcMsg *pMsg) { return vmPutMsgToQueue(pMgmt, pMsg, QUERY_QUEUE); }
S
shm  
Shengliang Guan 已提交
247

S
Shengliang Guan 已提交
248
int32_t vmPutMsgToFetchQueue(SVnodeMgmt *pMgmt, SRpcMsg *pMsg) { return vmPutMsgToQueue(pMgmt, pMsg, FETCH_QUEUE); }
S
shm  
Shengliang Guan 已提交
249

S
Shengliang Guan 已提交
250 251
int32_t vmPutMsgToStreamQueue(SVnodeMgmt *pMgmt, SRpcMsg *pMsg) { return vmPutMsgToQueue(pMgmt, pMsg, STREAM_QUEUE); }

S
Shengliang Guan 已提交
252
int32_t vmPutMsgToMgmtQueue(SVnodeMgmt *pMgmt, SRpcMsg *pMsg) {
S
Shengliang Guan 已提交
253 254
  const STraceId *trace = &pMsg->info.traceId;
  dGTrace("msg:%p, put into vnode-mgmt queue", pMsg);
S
Shengliang Guan 已提交
255
  taosWriteQitem(pMgmt->mgmtWorker.queue, pMsg);
S
Shengliang Guan 已提交
256
  return 0;
S
shm  
Shengliang Guan 已提交
257 258
}

S
Shengliang Guan 已提交
259
int32_t vmPutRpcMsgToQueue(SVnodeMgmt *pMgmt, EQueueType qtype, SRpcMsg *pRpc) {
260
  if (pRpc->contLen < sizeof(SMsgHead)) {
261
    dError("invalid rpc msg with no msg head at pCont. pRpc:%p, type:%s, len:%d", pRpc, TMSG_INFO(pRpc->msgType),
262 263 264 265 266 267
           pRpc->contLen);
    rpcFreeCont(pRpc->pCont);
    pRpc->pCont = NULL;
    return -1;
  }

S
Shengliang Guan 已提交
268
  SRpcMsg *pMsg = taosAllocateQitem(sizeof(SRpcMsg), RPC_QITEM, pRpc->contLen);
S
Shengliang Guan 已提交
269
  if (pMsg == NULL) {
dengyihao's avatar
dengyihao 已提交
270
    rpcFreeCont(pRpc->pCont);
S
Shengliang Guan 已提交
271 272 273
    pRpc->pCont = NULL;
    return -1;
  }
S
Shengliang Guan 已提交
274

S
Shengliang Guan 已提交
275
  SMsgHead *pHead = pRpc->pCont;
S
Shengliang Guan 已提交
276
  dTrace("vgId:%d, msg:%p is created, type:%s len:%d", pHead->vgId, pMsg, TMSG_INFO(pRpc->msgType), pRpc->contLen);
S
Shengliang Guan 已提交
277

S
Shengliang Guan 已提交
278 279 280
  pHead->contLen = htonl(pHead->contLen);
  pHead->vgId = htonl(pHead->vgId);
  memcpy(pMsg, pRpc, sizeof(SRpcMsg));
281
  pRpc->pCont = NULL;
S
Shengliang Guan 已提交
282 283 284 285 286

  int32_t code = vmPutMsgToQueue(pMgmt, pMsg, qtype);
  if (code != 0) {
    dTrace("msg:%p, is freed", pMsg);
    rpcFreeCont(pMsg->pCont);
287
    taosFreeQitem(pMsg);
S
Shengliang Guan 已提交
288 289 290
  }

  return code;
S
shm  
Shengliang Guan 已提交
291 292
}

S
Shengliang 已提交
293
int32_t vmGetQueueSize(SVnodeMgmt *pMgmt, int32_t vgId, EQueueType qtype) {
S
Shengliang Guan 已提交
294
  int32_t    size = -1;
S
Shengliang 已提交
295
  SVnodeObj *pVnode = vmAcquireVnode(pMgmt, vgId);
S
Shengliang Guan 已提交
296 297 298
  if (pVnode != NULL) {
    switch (qtype) {
      case WRITE_QUEUE:
299
        size = taosQueueItemSize(pVnode->pWriteW.queue);
S
Shengliang Guan 已提交
300 301
        break;
      case SYNC_QUEUE:
302
        size = taosQueueItemSize(pVnode->pSyncW.queue);
S
Shengliang Guan 已提交
303 304
        break;
      case APPLY_QUEUE:
305
        size = taosQueueItemSize(pVnode->pApplyW.queue);
S
Shengliang Guan 已提交
306
        break;
S
Shengliang Guan 已提交
307
      case QUERY_QUEUE:
308
        size = taosQueueItemSize(pVnode->pQueryQ);
S
Shengliang Guan 已提交
309 310
        break;
      case FETCH_QUEUE:
311
        size = taosQueueItemSize(pVnode->pFetchQ);
S
Shengliang Guan 已提交
312
        break;
S
Shengliang Guan 已提交
313 314 315
      case STREAM_QUEUE:
        size = taosQueueItemSize(pVnode->pStreamQ);
        break;
S
Shengliang Guan 已提交
316 317 318
      default:
        break;
    }
319
    vmReleaseVnode(pMgmt, pVnode);
S
Shengliang Guan 已提交
320
  }
S
Shengliang Guan 已提交
321
  if (size < 0) {
S
Shengliang Guan 已提交
322
    dTrace("vgId:%d, can't get size from queue since %s, qtype:%d", vgId, terrstr(), qtype);
S
Shengliang Guan 已提交
323 324
    size = 0;
  }
S
Shengliang Guan 已提交
325 326 327
  return size;
}

S
Shengliang 已提交
328
int32_t vmAllocQueue(SVnodeMgmt *pMgmt, SVnodeObj *pVnode) {
329 330
  SMultiWorkerCfg wcfg = {.max = 1, .name = "vnode-write", .fp = (FItems)vnodeProposeWriteMsg, .param = pVnode->pImpl};
  SMultiWorkerCfg scfg = {.max = 1, .name = "vnode-sync", .fp = (FItems)vmProcessSyncQueue, .param = pVnode};
331
  SMultiWorkerCfg sccfg = {.max = 1, .name = "vnode-sync-rd", .fp = (FItems)vmProcessSyncQueue, .param = pVnode};
332
  SMultiWorkerCfg acfg = {.max = 1, .name = "vnode-apply", .fp = (FItems)vnodeApplyWriteMsg, .param = pVnode->pImpl};
333 334
  (void)tMultiWorkerInit(&pVnode->pWriteW, &wcfg);
  (void)tMultiWorkerInit(&pVnode->pSyncW, &scfg);
335
  (void)tMultiWorkerInit(&pVnode->pSyncRdW, &sccfg);
336 337
  (void)tMultiWorkerInit(&pVnode->pApplyW, &acfg);

S
shm  
Shengliang Guan 已提交
338
  pVnode->pQueryQ = tQWorkerAllocQueue(&pMgmt->queryPool, pVnode, (FItem)vmProcessQueryQueue);
339
  pVnode->pStreamQ = tAutoQWorkerAllocQueue(&pMgmt->streamPool, pVnode, (FItem)vmProcessStreamQueue);
340
  pVnode->pFetchQ = tWWorkerAllocQueue(&pMgmt->fetchPool, pVnode, (FItems)vmProcessFetchQueue);
S
shm  
Shengliang Guan 已提交
341

342
  if (pVnode->pWriteW.queue == NULL || pVnode->pSyncW.queue == NULL || pVnode->pSyncRdW.queue == NULL ||
343
      pVnode->pApplyW.queue == NULL || pVnode->pQueryQ == NULL || pVnode->pStreamQ == NULL || pVnode->pFetchQ == NULL) {
S
shm  
Shengliang Guan 已提交
344 345 346 347
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

348 349 350 351
  dInfo("vgId:%d, write-queue:%p is alloced, thread:%08" PRId64, pVnode->vgId, pVnode->pWriteW.queue,
        pVnode->pWriteW.queue->threadId);
  dInfo("vgId:%d, sync-queue:%p is alloced, thread:%08" PRId64, pVnode->vgId, pVnode->pSyncW.queue,
        pVnode->pSyncW.queue->threadId);
352 353
  dInfo("vgId:%d, sync-rd-queue:%p is alloced, thread:%08" PRId64, pVnode->vgId, pVnode->pSyncRdW.queue,
        pVnode->pSyncRdW.queue->threadId);
354 355
  dInfo("vgId:%d, apply-queue:%p is alloced, thread:%08" PRId64, pVnode->vgId, pVnode->pApplyW.queue,
        pVnode->pApplyW.queue->threadId);
356 357 358 359
  dInfo("vgId:%d, query-queue:%p is alloced", pVnode->vgId, pVnode->pQueryQ);
  dInfo("vgId:%d, fetch-queue:%p is alloced, thread:%08" PRId64, pVnode->vgId, pVnode->pFetchQ,
        pVnode->pFetchQ->threadId);
  dInfo("vgId:%d, stream-queue:%p is alloced", pVnode->vgId, pVnode->pStreamQ);
S
shm  
Shengliang Guan 已提交
360 361 362
  return 0;
}

S
Shengliang 已提交
363
void vmFreeQueue(SVnodeMgmt *pMgmt, SVnodeObj *pVnode) {
S
Shengliang Guan 已提交
364
  tQWorkerFreeQueue(&pMgmt->queryPool, pVnode->pQueryQ);
365
  tAutoQWorkerFreeQueue(&pMgmt->streamPool, pVnode->pStreamQ);
366
  tWWorkerFreeQueue(&pMgmt->fetchPool, pVnode->pFetchQ);
S
shm  
Shengliang Guan 已提交
367
  pVnode->pQueryQ = NULL;
S
Shengliang Guan 已提交
368
  pVnode->pStreamQ = NULL;
S
Shengliang Guan 已提交
369
  pVnode->pFetchQ = NULL;
S
Shengliang Guan 已提交
370
  dDebug("vgId:%d, queue is freed", pVnode->vgId);
S
shm  
Shengliang Guan 已提交
371 372
}

S
Shengliang 已提交
373
int32_t vmStartWorker(SVnodeMgmt *pMgmt) {
S
shm  
Shengliang Guan 已提交
374 375
  SQWorkerPool *pQPool = &pMgmt->queryPool;
  pQPool->name = "vnode-query";
S
Shengliang Guan 已提交
376 377
  pQPool->min = tsNumOfVnodeQueryThreads;
  pQPool->max = tsNumOfVnodeQueryThreads;
S
shm  
Shengliang Guan 已提交
378 379
  if (tQWorkerInit(pQPool) != 0) return -1;

380
  SAutoQWorkerPool *pStreamPool = &pMgmt->streamPool;
S
Shengliang Guan 已提交
381
  pStreamPool->name = "vnode-stream";
382 383
  pStreamPool->ratio = tsRatioOfVnodeStreamThreads;
  if (tAutoQWorkerInit(pStreamPool) != 0) return -1;
S
Shengliang Guan 已提交
384

385
  SWWorkerPool *pFPool = &pMgmt->fetchPool;
S
shm  
Shengliang Guan 已提交
386
  pFPool->name = "vnode-fetch";
S
Shengliang Guan 已提交
387
  pFPool->max = tsNumOfVnodeFetchThreads;
388
  if (tWWorkerInit(pFPool) != 0) return -1;
S
shm  
Shengliang Guan 已提交
389

S
Shengliang Guan 已提交
390
  SSingleWorkerCfg mgmtCfg = {
S
Shengliang Guan 已提交
391 392 393
      .min = 1,
      .max = 1,
      .name = "vnode-mgmt",
S
Shengliang Guan 已提交
394
      .fp = (FItem)vmProcessMgmtQueue,
S
Shengliang Guan 已提交
395 396
      .param = pMgmt,
  };
S
Shengliang Guan 已提交
397
  if (tSingleWorkerInit(&pMgmt->mgmtWorker, &mgmtCfg) != 0) return -1;
S
shm  
Shengliang Guan 已提交
398

S
Shengliang Guan 已提交
399
  dDebug("vnode workers are initialized");
S
shm  
Shengliang Guan 已提交
400 401 402
  return 0;
}

S
Shengliang 已提交
403
void vmStopWorker(SVnodeMgmt *pMgmt) {
S
Shengliang Guan 已提交
404
  tQWorkerCleanup(&pMgmt->queryPool);
405
  tAutoQWorkerCleanup(&pMgmt->streamPool);
406
  tWWorkerCleanup(&pMgmt->fetchPool);
S
Shengliang Guan 已提交
407
  dDebug("vnode workers are closed");
S
shm  
Shengliang Guan 已提交
408
}