vnodeWrite.c 3.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * 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/>.
 */

H
refact  
Hongze Cheng 已提交
16 17
#include "vnodeDef.h"

L
Liu Jicong 已提交
18 19
int vnodeProcessNoWalWMsgs(SVnode *pVnode, SRpcMsg *pMsg) {
  switch (pMsg->msgType) {
L
Liu Jicong 已提交
20
    case TSDB_MSG_TYPE_MQ_SET_CUR:
L
Liu Jicong 已提交
21 22 23 24 25 26 27 28
      if (tqSetCursor(pVnode->pTq, pMsg->pCont) < 0) {
        // TODO: handle error
      }
      break;
  }
  return 0;
}

H
refact  
Hongze Cheng 已提交
29
int vnodeProcessWMsgs(SVnode *pVnode, SArray *pMsgs) {
L
Liu Jicong 已提交
30
  SRpcMsg   *pMsg;
H
more  
Hongze Cheng 已提交
31
  SVnodeReq *pVnodeReq;
H
more  
Hongze Cheng 已提交
32

H
more  
Hongze Cheng 已提交
33 34
  for (int i = 0; i < taosArrayGetSize(pMsgs); i++) {
    pMsg = *(SRpcMsg **)taosArrayGet(pMsgs, i);
H
more  
Hongze Cheng 已提交
35

H
more  
Hongze Cheng 已提交
36
    // ser request version
L
Liu Jicong 已提交
37 38
    void   *pBuf = pMsg->pCont;
    int64_t ver = pVnode->state.processed++;
H
Hongze Cheng 已提交
39
    taosEncodeFixedU64(&pBuf, ver);
H
more  
Hongze Cheng 已提交
40

H
Hongze Cheng 已提交
41
    if (walWrite(pVnode->pWal, ver, pMsg->msgType, pMsg->pCont, pMsg->contLen) < 0) {
H
more  
Hongze Cheng 已提交
42 43
      // TODO: handle error
    }
H
more  
Hongze Cheng 已提交
44 45
  }

H
more  
Hongze Cheng 已提交
46
  walFsync(pVnode->pWal, false);
H
refact  
Hongze Cheng 已提交
47

H
more  
Hongze Cheng 已提交
48 49 50
  // Apply each request now
  for (int i = 0; i < taosArrayGetSize(pMsgs); i++) {
    pMsg = *(SRpcMsg **)taosArrayGet(pMsgs, i);
H
more  
Hongze Cheng 已提交
51
    SVnodeReq vReq;
H
refact  
Hongze Cheng 已提交
52

H
more  
Hongze Cheng 已提交
53 54 55 56 57
    // Apply the request
    {
      void *ptr = vnodeMalloc(pVnode, pMsg->contLen);
      if (ptr == NULL) {
        // TODO: handle error
H
more  
Hongze Cheng 已提交
58
      }
H
more  
Hongze Cheng 已提交
59

H
more  
Hongze Cheng 已提交
60 61
      // TODO: copy here need to be extended
      memcpy(ptr, pMsg->pCont, pMsg->contLen);
H
more  
Hongze Cheng 已提交
62

H
Hongze Cheng 已提交
63
      // todo: change the interface here
H
more  
Hongze Cheng 已提交
64 65 66
      uint64_t ver;
      taosDecodeFixedU64(pMsg->pCont, &ver);
      if (tqPushMsg(pVnode->pTq, ptr, ver) < 0) {
H
more  
Hongze Cheng 已提交
67 68 69
        // TODO: handle error
      }

H
more  
Hongze Cheng 已提交
70 71
      vnodeParseReq(pMsg->pCont, &vReq, pMsg->msgType);

H
more  
Hongze Cheng 已提交
72 73
      switch (pMsg->msgType) {
        case TSDB_MSG_TYPE_CREATE_TABLE:
H
more  
Hongze Cheng 已提交
74
          if (metaCreateTable(pVnode->pMeta, &(vReq.ctReq)) < 0) {
H
more  
Hongze Cheng 已提交
75 76 77 78 79 80
            // TODO: handle error
          }

          // TODO: maybe need to clear the requst struct
          break;
        case TSDB_MSG_TYPE_DROP_TABLE:
H
more  
Hongze Cheng 已提交
81
          if (metaDropTable(pVnode->pMeta, vReq.dtReq.uid) < 0) {
H
more  
Hongze Cheng 已提交
82 83
            // TODO: handle error
          }
H
more  
Hongze Cheng 已提交
84 85
          break;
        case TSDB_MSG_TYPE_SUBMIT:
H
more  
Hongze Cheng 已提交
86 87 88
          if (tsdbInsertData(pVnode->pTsdb, (SSubmitMsg *)ptr) < 0) {
            // TODO: handle error
          }
H
more  
Hongze Cheng 已提交
89 90 91
          break;
        default:
          break;
H
more  
Hongze Cheng 已提交
92
      }
H
more  
Hongze Cheng 已提交
93

H
more  
Hongze Cheng 已提交
94 95
      pVnode->state.applied = ver;
    }
H
more  
Hongze Cheng 已提交
96 97 98 99 100

    // Check if it needs to commit
    if (vnodeShouldCommit(pVnode)) {
      if (vnodeAsyncCommit(pVnode) < 0) {
        // TODO: handle error
H
more  
Hongze Cheng 已提交
101
      }
H
more  
Hongze Cheng 已提交
102
    }
H
refact  
Hongze Cheng 已提交
103 104
  }

H
more  
Hongze Cheng 已提交
105
  return 0;
H
more  
Hongze Cheng 已提交
106 107
}

H
more  
Hongze Cheng 已提交
108 109 110 111 112
int vnodeApplyWMsg(SVnode *pVnode, SRpcMsg *pMsg, SRpcMsg **pRsp) {
  // TODO
  return 0;
}

L
Liu Jicong 已提交
113
/* ------------------------ STATIC METHODS ------------------------ */