vnodeWrite.c 4.6 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/>.
 */

L
Liu Jicong 已提交
16
#include "tq.h"
L
Liu Jicong 已提交
17
#include "vnd.h"
H
refact  
Hongze Cheng 已提交
18

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

H
refact  
Hongze Cheng 已提交
32
int vnodeProcessWMsgs(SVnode *pVnode, SArray *pMsgs) {
H
Hongze Cheng 已提交
33
  SRpcMsg *pMsg;
H
more  
Hongze Cheng 已提交
34

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

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

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

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

H
more  
Hongze Cheng 已提交
50
  // TODO: Integrate RAFT module here
H
more  
Hongze Cheng 已提交
51 52 53 54 55

  return 0;
}

int vnodeApplyWMsg(SVnode *pVnode, SRpcMsg *pMsg, SRpcMsg **pRsp) {
H
Hongze Cheng 已提交
56 57
  SVCreateTbReq      vCreateTbReq;
  SVCreateTbBatchReq vCreateTbBatchReq;
H
Hongze Cheng 已提交
58
  void *             ptr = vnodeMalloc(pVnode, pMsg->contLen);
H
more  
Hongze Cheng 已提交
59 60 61
  if (ptr == NULL) {
    // TODO: handle error
  }
H
more  
Hongze Cheng 已提交
62

H
more  
Hongze Cheng 已提交
63 64 65 66 67
  // TODO: copy here need to be extended
  memcpy(ptr, pMsg->pCont, pMsg->contLen);

  // todo: change the interface here
  uint64_t ver;
H
more  
Hongze Cheng 已提交
68
  taosDecodeFixedU64(POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), &ver);
H
more  
Hongze Cheng 已提交
69 70 71
  if (tqPushMsg(pVnode->pTq, ptr, ver) < 0) {
    // TODO: handle error
  }
H
more  
Hongze Cheng 已提交
72

H
more  
Hongze Cheng 已提交
73
  switch (pMsg->msgType) {
H
Hongze Cheng 已提交
74
    case TDMT_VND_CREATE_STB:
H
more  
Hongze Cheng 已提交
75 76
      tDeserializeSVCreateTbReq(POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), &vCreateTbReq);
      if (metaCreateTable(pVnode->pMeta, &(vCreateTbReq)) < 0) {
H
more  
Hongze Cheng 已提交
77 78 79
        // TODO: handle error
      }

H
more  
Hongze Cheng 已提交
80
      // TODO: maybe need to clear the requst struct
H
Hongze Cheng 已提交
81 82 83
      free(vCreateTbReq.stbCfg.pSchema);
      free(vCreateTbReq.stbCfg.pTagSchema);
      free(vCreateTbReq.name);
H
more  
Hongze Cheng 已提交
84
      break;
H
Hongze Cheng 已提交
85
    case TDMT_VND_CREATE_TABLE:
S
Shengliang Guan 已提交
86
      tDeserializeSVCreateTbBatchReq(POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), &vCreateTbBatchReq);
H
Hongze Cheng 已提交
87 88 89 90
      for (int i = 0; i < taosArrayGetSize(vCreateTbBatchReq.pArray); i++) {
        SVCreateTbReq *pCreateTbReq = taosArrayGet(vCreateTbBatchReq.pArray, i);
        if (metaCreateTable(pVnode->pMeta, pCreateTbReq) < 0) {
          // TODO: handle error
91
          vError("vgId:%d, failed to create table: %s", pVnode->vgId, pCreateTbReq->name);
H
Hongze Cheng 已提交
92
        }
H
Hongze Cheng 已提交
93
        free(pCreateTbReq->name);
H
Hongze Cheng 已提交
94 95 96 97 98 99 100 101
        if (pCreateTbReq->type == TD_SUPER_TABLE) {
          free(pCreateTbReq->stbCfg.pSchema);
          free(pCreateTbReq->stbCfg.pTagSchema);
        } else if (pCreateTbReq->type == TD_CHILD_TABLE) {
          free(pCreateTbReq->ctbCfg.pTag);
        } else {
          free(pCreateTbReq->ntbCfg.pSchema);
        }
H
Hongze Cheng 已提交
102
      }
103

S
Shengliang Guan 已提交
104
      vTrace("vgId:%d process create %" PRIzu " tables", pVnode->vgId, taosArrayGetSize(vCreateTbBatchReq.pArray));
H
Hongze Cheng 已提交
105 106 107
      taosArrayDestroy(vCreateTbBatchReq.pArray);
      break;

S
Shengliang Guan 已提交
108
    case TDMT_VND_ALTER_STB:
S
Shengliang Guan 已提交
109 110 111 112 113
      vTrace("vgId:%d, process alter stb req", pVnode->vgId);
      tDeserializeSVCreateTbReq(POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), &vCreateTbReq);
      free(vCreateTbReq.stbCfg.pSchema);
      free(vCreateTbReq.stbCfg.pTagSchema);
      free(vCreateTbReq.name);
S
Shengliang Guan 已提交
114
      break;
H
Hongze Cheng 已提交
115
    case TDMT_VND_DROP_STB:
S
Shengliang Guan 已提交
116 117
      vTrace("vgId:%d, process drop stb req", pVnode->vgId);
      break;
S
Shengliang Guan 已提交
118
    case TDMT_VND_DROP_TABLE:
H
Hongze Cheng 已提交
119 120 121
      // if (metaDropTable(pVnode->pMeta, vReq.dtReq.uid) < 0) {
      //   // TODO: handle error
      // }
H
more  
Hongze Cheng 已提交
122
      break;
H
Hongze Cheng 已提交
123
    case TDMT_VND_SUBMIT:
H
Hongze Cheng 已提交
124
      if (tsdbInsertData(pVnode->pTsdb, (SSubmitMsg *)ptr, NULL) < 0) {
H
more  
Hongze Cheng 已提交
125
        // TODO: handle error
H
more  
Hongze Cheng 已提交
126
      }
H
more  
Hongze Cheng 已提交
127
      break;
L
Liu Jicong 已提交
128
    case TDMT_VND_MQ_SET_CONN: {
129
      if (tqProcessSetConnReq(pVnode->pTq, POINTER_SHIFT(ptr, sizeof(SMsgHead))) < 0) {
L
Liu Jicong 已提交
130
        // TODO: handle error
L
Liu Jicong 已提交
131
      }
L
Liu Jicong 已提交
132
    } break;
H
more  
Hongze Cheng 已提交
133
    default:
H
more  
Hongze Cheng 已提交
134
      ASSERT(0);
H
more  
Hongze Cheng 已提交
135
      break;
H
refact  
Hongze Cheng 已提交
136 137
  }

H
more  
Hongze Cheng 已提交
138
  pVnode->state.applied = ver;
H
more  
Hongze Cheng 已提交
139

H
more  
Hongze Cheng 已提交
140 141
  // Check if it needs to commit
  if (vnodeShouldCommit(pVnode)) {
H
Hongze Cheng 已提交
142
    // tsem_wait(&(pVnode->canCommit));
H
more  
Hongze Cheng 已提交
143 144 145 146
    if (vnodeAsyncCommit(pVnode) < 0) {
      // TODO: handle error
    }
  }
H
Hongze Cheng 已提交
147

H
more  
Hongze Cheng 已提交
148 149 150
  return 0;
}

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