“50f6388f0041b133e7de69f95be8a49ec08847a1”上不存在“git@gitcode.net:taosdata/tdengine.git”
vnodeWrite.c 6.4 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

  return 0;
}

L
Liu Jicong 已提交
44
int vnodeApplyWMsg(SVnode *pVnode, SRpcMsg *pMsg, SRpcMsg **pRsp) {
C
Cary Xu 已提交
45
  void *ptr = NULL;
H
more  
Hongze Cheng 已提交
46

L
Liu Jicong 已提交
47 48 49 50 51 52 53 54 55
  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 已提交
56 57

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

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

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

S
Shengliang Guan 已提交
98
      vTrace("vgId:%d process create %" PRIzu " tables", pVnode->vgId, taosArrayGetSize(vCreateTbBatchReq.pArray));
H
Hongze Cheng 已提交
99 100
      taosArrayDestroy(vCreateTbBatchReq.pArray);
      break;
C
Cary Xu 已提交
101 102
    }
    case TDMT_VND_ALTER_STB: {
L
Liu Jicong 已提交
103
      SVCreateTbReq vAlterTbReq = {0};
S
Shengliang Guan 已提交
104
      vTrace("vgId:%d, process alter stb req", pVnode->vgId);
C
Cary Xu 已提交
105 106 107 108
      tDeserializeSVCreateTbReq(POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), &vAlterTbReq);
      free(vAlterTbReq.stbCfg.pSchema);
      free(vAlterTbReq.stbCfg.pTagSchema);
      free(vAlterTbReq.name);
S
Shengliang Guan 已提交
109
      break;
C
Cary Xu 已提交
110
    }
H
Hongze Cheng 已提交
111
    case TDMT_VND_DROP_STB:
S
Shengliang Guan 已提交
112 113
      vTrace("vgId:%d, process drop stb req", pVnode->vgId);
      break;
S
Shengliang Guan 已提交
114
    case TDMT_VND_DROP_TABLE:
H
Hongze Cheng 已提交
115 116 117
      // if (metaDropTable(pVnode->pMeta, vReq.dtReq.uid) < 0) {
      //   // TODO: handle error
      // }
H
more  
Hongze Cheng 已提交
118
      break;
H
Hongze Cheng 已提交
119
    case TDMT_VND_SUBMIT:
L
Liu Jicong 已提交
120 121 122 123
      if (pVnode->config.streamMode == 0) {
        if (tsdbInsertData(pVnode->pTsdb, (SSubmitReq *)ptr, NULL) < 0) {
          // TODO: handle error
        }
H
more  
Hongze Cheng 已提交
124
      }
H
more  
Hongze Cheng 已提交
125
      break;
L
Liu Jicong 已提交
126
    case TDMT_VND_MQ_SET_CONN: {
L
Liu Jicong 已提交
127
      if (tqProcessSetConnReq(pVnode->pTq, POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead))) < 0) {
L
Liu Jicong 已提交
128
        // TODO: handle error
L
Liu Jicong 已提交
129
      }
L
Liu Jicong 已提交
130
    } break;
L
Liu Jicong 已提交
131
    case TDMT_VND_MQ_REB: {
L
Liu Jicong 已提交
132
      if (tqProcessRebReq(pVnode->pTq, POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead))) < 0) {
L
Liu Jicong 已提交
133 134
      }
    } break;
L
Liu Jicong 已提交
135 136 137 138 139
    case TDMT_VND_TASK_DEPLOY: {
      if (tqProcessTaskDeploy(pVnode->pTq, POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)),
                              pMsg->contLen - sizeof(SMsgHead)) < 0) {
      }
    } break;
C
Cary Xu 已提交
140
    case TDMT_VND_CREATE_SMA: {  // timeRangeSMA
C
Cary Xu 已提交
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
      SSmaCfg vCreateSmaReq = {0};
      if (tDeserializeSVCreateTSmaReq(POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), &vCreateSmaReq) == NULL) {
        terrno = TSDB_CODE_OUT_OF_MEMORY;
        return -1;
      }

      if (metaCreateTSma(pVnode->pMeta, &vCreateSmaReq) < 0) {
        // TODO: handle error
        tdDestroyTSma(&vCreateSmaReq.tSma);
        return -1;
      }
      // TODO: send msg to stream computing to create tSma
      // if ((send msg to stream computing) < 0) {
      //   tdDestroyTSma(&vCreateSmaReq);
      //   return -1;
      // }
      tdDestroyTSma(&vCreateSmaReq.tSma);
      // TODO: return directly or go on follow steps?
C
Cary Xu 已提交
159 160 161 162
    } break;
    case TDMT_VND_CANCEL_SMA: {  // timeRangeSMA
    } break;
    case TDMT_VND_DROP_SMA: {  // timeRangeSMA
C
Cary Xu 已提交
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
      SVDropTSmaReq vDropSmaReq = {0};
      if (tDeserializeSVDropTSmaReq(POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), &vDropSmaReq) == NULL) {
        terrno = TSDB_CODE_OUT_OF_MEMORY;
        return -1;
      }

      if (metaDropTSma(pVnode->pMeta, vDropSmaReq.indexName) < 0) {
        // TODO: handle error
        return -1;
      }
      // TODO: send msg to stream computing to drop tSma
      // if ((send msg to stream computing) < 0) {
      //   tdDestroyTSma(&vCreateSmaReq);
      //   return -1;
      // }
      // TODO: return directly or go on follow steps?
C
Cary Xu 已提交
179
    } break;
H
more  
Hongze Cheng 已提交
180
    default:
H
more  
Hongze Cheng 已提交
181
      ASSERT(0);
H
more  
Hongze Cheng 已提交
182
      break;
H
refact  
Hongze Cheng 已提交
183 184
  }

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

H
more  
Hongze Cheng 已提交
187 188
  // Check if it needs to commit
  if (vnodeShouldCommit(pVnode)) {
H
Hongze Cheng 已提交
189
    // tsem_wait(&(pVnode->canCommit));
H
more  
Hongze Cheng 已提交
190 191 192 193
    if (vnodeAsyncCommit(pVnode) < 0) {
      // TODO: handle error
    }
  }
H
Hongze Cheng 已提交
194

H
more  
Hongze Cheng 已提交
195 196 197
  return 0;
}

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