vnodeWrite.c 4.8 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

H
refact  
Hongze Cheng 已提交
19
int vnodeProcessWMsgs(SVnode *pVnode, SArray *pMsgs) {
H
Hongze Cheng 已提交
20
  SRpcMsg *pMsg;
H
more  
Hongze Cheng 已提交
21

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

L
Liu Jicong 已提交
25
    // set request version
L
Liu Jicong 已提交
26
    void   *pBuf = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
L
Liu Jicong 已提交
27
    int64_t ver = pVnode->state.processed++;
L
Liu Jicong 已提交
28
    taosEncodeFixedI64(&pBuf, ver);
H
more  
Hongze Cheng 已提交
29

H
Hongze Cheng 已提交
30
    if (walWrite(pVnode->pWal, ver, pMsg->msgType, pMsg->pCont, pMsg->contLen) < 0) {
H
more  
Hongze Cheng 已提交
31
      // TODO: handle error
L
Liu Jicong 已提交
32 33
      /*ASSERT(false);*/
      vError("vnode:%d  write wal error since %s", pVnode->vgId, terrstr());
H
more  
Hongze Cheng 已提交
34
    }
H
more  
Hongze Cheng 已提交
35 36
  }

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

H
more  
Hongze Cheng 已提交
39
  // TODO: Integrate RAFT module here
H
more  
Hongze Cheng 已提交
40 41 42 43 44

  return 0;
}

int vnodeApplyWMsg(SVnode *pVnode, SRpcMsg *pMsg, SRpcMsg **pRsp) {
H
Hongze Cheng 已提交
45 46
  SVCreateTbReq      vCreateTbReq;
  SVCreateTbBatchReq vCreateTbBatchReq;
L
Liu Jicong 已提交
47
  void              *ptr = NULL;
H
more  
Hongze Cheng 已提交
48

L
Liu Jicong 已提交
49 50 51 52 53 54 55 56 57
  if (pVnode->config.streamMode == 0) {
    ptr = vnodeMalloc(pVnode, pMsg->contLen);
    if (ptr == NULL) {
      // TODO: handle error
    }

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

  // todo: change the interface here
L
Liu Jicong 已提交
60 61
  int64_t ver;
  taosDecodeFixedI64(POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), &ver);
L
Liu Jicong 已提交
62
  if (tqPushMsg(pVnode->pTq, ptr, pMsg->msgType, ver) < 0) {
H
more  
Hongze Cheng 已提交
63 64
    // TODO: handle error
  }
H
more  
Hongze Cheng 已提交
65

H
more  
Hongze Cheng 已提交
66
  switch (pMsg->msgType) {
H
Hongze Cheng 已提交
67
    case TDMT_VND_CREATE_STB:
H
more  
Hongze Cheng 已提交
68 69
      tDeserializeSVCreateTbReq(POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), &vCreateTbReq);
      if (metaCreateTable(pVnode->pMeta, &(vCreateTbReq)) < 0) {
H
more  
Hongze Cheng 已提交
70 71 72
        // TODO: handle error
      }

H
more  
Hongze Cheng 已提交
73
      // TODO: maybe need to clear the requst struct
H
Hongze Cheng 已提交
74 75 76
      free(vCreateTbReq.stbCfg.pSchema);
      free(vCreateTbReq.stbCfg.pTagSchema);
      free(vCreateTbReq.name);
H
more  
Hongze Cheng 已提交
77
      break;
H
Hongze Cheng 已提交
78
    case TDMT_VND_CREATE_TABLE:
S
Shengliang Guan 已提交
79
      tDeserializeSVCreateTbBatchReq(POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), &vCreateTbBatchReq);
H
Hongze Cheng 已提交
80 81 82 83
      for (int i = 0; i < taosArrayGetSize(vCreateTbBatchReq.pArray); i++) {
        SVCreateTbReq *pCreateTbReq = taosArrayGet(vCreateTbBatchReq.pArray, i);
        if (metaCreateTable(pVnode->pMeta, pCreateTbReq) < 0) {
          // TODO: handle error
84
          vError("vgId:%d, failed to create table: %s", pVnode->vgId, pCreateTbReq->name);
H
Hongze Cheng 已提交
85
        }
H
Hongze Cheng 已提交
86
        free(pCreateTbReq->name);
H
Hongze Cheng 已提交
87 88 89 90 91 92 93 94
        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 已提交
95
      }
96

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

S
Shengliang Guan 已提交
101
    case TDMT_VND_ALTER_STB:
S
Shengliang Guan 已提交
102 103 104 105 106
      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 已提交
107
      break;
H
Hongze Cheng 已提交
108
    case TDMT_VND_DROP_STB:
S
Shengliang Guan 已提交
109 110
      vTrace("vgId:%d, process drop stb req", pVnode->vgId);
      break;
S
Shengliang Guan 已提交
111
    case TDMT_VND_DROP_TABLE:
H
Hongze Cheng 已提交
112 113 114
      // if (metaDropTable(pVnode->pMeta, vReq.dtReq.uid) < 0) {
      //   // TODO: handle error
      // }
H
more  
Hongze Cheng 已提交
115
      break;
H
Hongze Cheng 已提交
116
    case TDMT_VND_SUBMIT:
L
Liu Jicong 已提交
117 118 119 120
      if (pVnode->config.streamMode == 0) {
        if (tsdbInsertData(pVnode->pTsdb, (SSubmitReq *)ptr, NULL) < 0) {
          // TODO: handle error
        }
H
more  
Hongze Cheng 已提交
121
      }
H
more  
Hongze Cheng 已提交
122
      break;
L
Liu Jicong 已提交
123
    case TDMT_VND_MQ_SET_CONN: {
L
Liu Jicong 已提交
124
      if (tqProcessSetConnReq(pVnode->pTq, POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead))) < 0) {
L
Liu Jicong 已提交
125
        // TODO: handle error
L
Liu Jicong 已提交
126
      }
L
Liu Jicong 已提交
127
    } break;
L
Liu Jicong 已提交
128
    case TDMT_VND_MQ_REB: {
L
Liu Jicong 已提交
129
      if (tqProcessRebReq(pVnode->pTq, POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead))) < 0) {
L
Liu Jicong 已提交
130 131
      }
    } break;
H
more  
Hongze Cheng 已提交
132
    default:
H
more  
Hongze Cheng 已提交
133
      ASSERT(0);
H
more  
Hongze Cheng 已提交
134
      break;
H
refact  
Hongze Cheng 已提交
135 136
  }

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

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

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

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