vnodeWrite.c 7.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 "vnd.h"
H
refact  
Hongze Cheng 已提交
17

S
Shengliang Guan 已提交
18
void vnodeProcessWMsgs(SVnode *pVnode, SArray *pMsgs) {
S
shm  
Shengliang Guan 已提交
19
  SNodeMsg *pMsg;
L
Liu Jicong 已提交
20
  SRpcMsg  *pRpc;
H
more  
Hongze Cheng 已提交
21

H
more  
Hongze Cheng 已提交
22
  for (int i = 0; i < taosArrayGetSize(pMsgs); i++) {
S
shm  
Shengliang Guan 已提交
23 24
    pMsg = *(SNodeMsg **)taosArrayGet(pMsgs, i);
    pRpc = &pMsg->rpcMsg;
H
more  
Hongze Cheng 已提交
25

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

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

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

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

S
Shengliang Guan 已提交
42 43
  // No results are returned because error handling is difficult
  // return 0;
H
more  
Hongze Cheng 已提交
44 45
}

L
Liu Jicong 已提交
46
int vnodeApplyWMsg(SVnode *pVnode, SRpcMsg *pMsg, SRpcMsg **pRsp) {
C
Cary Xu 已提交
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, pMsg->pCont, pMsg->contLen, 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) {
C
Cary Xu 已提交
67
    case TDMT_VND_CREATE_STB: {
L
Liu Jicong 已提交
68
      SVCreateTbReq vCreateTbReq = {0};
H
more  
Hongze Cheng 已提交
69 70
      tDeserializeSVCreateTbReq(POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), &vCreateTbReq);
      if (metaCreateTable(pVnode->pMeta, &(vCreateTbReq)) < 0) {
H
more  
Hongze Cheng 已提交
71 72 73
        // TODO: handle error
      }

C
Cary Xu 已提交
74
      // TODO: maybe need to clear the request struct
H
Hongze Cheng 已提交
75 76 77
      free(vCreateTbReq.stbCfg.pSchema);
      free(vCreateTbReq.stbCfg.pTagSchema);
      free(vCreateTbReq.name);
H
more  
Hongze Cheng 已提交
78
      break;
C
Cary Xu 已提交
79 80 81
    }
    case TDMT_VND_CREATE_TABLE: {
      SVCreateTbBatchReq vCreateTbBatchReq = {0};
D
dapan1121 已提交
82
      SVCreateTbBatchRsp vCreateTbBatchRsp = {0};
S
Shengliang Guan 已提交
83
      tDeserializeSVCreateTbBatchReq(POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), &vCreateTbBatchReq);
D
dapan1121 已提交
84 85
      int reqNum = taosArrayGetSize(vCreateTbBatchReq.pArray);
      for (int i = 0; i < reqNum; i++) {
H
Hongze Cheng 已提交
86
        SVCreateTbReq *pCreateTbReq = taosArrayGet(vCreateTbBatchReq.pArray, i);
D
dapan1121 已提交
87

L
Liu Jicong 已提交
88
        char      tableFName[TSDB_TABLE_FNAME_LEN];
D
dapan1121 已提交
89
        SMsgHead *pHead = (SMsgHead *)pMsg->pCont;
D
dapan1121 已提交
90
        sprintf(tableFName, "%s.%s", pCreateTbReq->dbFName, pCreateTbReq->name);
L
Liu Jicong 已提交
91

D
dapan1121 已提交
92 93 94 95 96 97 98
        int32_t code = vnodeValidateTableHash(&pVnode->config, tableFName);
        if (code) {
          SVCreateTbRsp rsp;
          rsp.code = code;

          taosArrayPush(vCreateTbBatchRsp.rspList, &rsp);
        }
L
Liu Jicong 已提交
99

H
Hongze Cheng 已提交
100 101
        if (metaCreateTable(pVnode->pMeta, pCreateTbReq) < 0) {
          // TODO: handle error
102
          vError("vgId:%d, failed to create table: %s", pVnode->vgId, pCreateTbReq->name);
H
Hongze Cheng 已提交
103
        }
H
Hongze Cheng 已提交
104
        free(pCreateTbReq->name);
H
Hongze Cheng 已提交
105 106 107 108 109 110 111 112
        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 已提交
113
      }
114

S
Shengliang Guan 已提交
115
      vTrace("vgId:%d process create %" PRIzu " tables", pVnode->vgId, taosArrayGetSize(vCreateTbBatchReq.pArray));
H
Hongze Cheng 已提交
116
      taosArrayDestroy(vCreateTbBatchReq.pArray);
D
dapan1121 已提交
117
      if (vCreateTbBatchRsp.rspList) {
D
dapan 已提交
118
        int32_t contLen = tSerializeSVCreateTbBatchRsp(NULL, 0, &vCreateTbBatchRsp);
L
Liu Jicong 已提交
119
        void   *msg = rpcMallocCont(contLen);
D
dapan 已提交
120 121
        tSerializeSVCreateTbBatchRsp(msg, contLen, &vCreateTbBatchRsp);
        taosArrayDestroy(vCreateTbBatchRsp.rspList);
L
Liu Jicong 已提交
122

D
dapan 已提交
123 124 125 126 127 128
        *pRsp = calloc(1, sizeof(SRpcMsg));
        (*pRsp)->msgType = TDMT_VND_CREATE_TABLE_RSP;
        (*pRsp)->pCont = msg;
        (*pRsp)->contLen = contLen;
        (*pRsp)->handle = pMsg->handle;
        (*pRsp)->ahandle = pMsg->ahandle;
D
dapan1121 已提交
129
      }
H
Hongze Cheng 已提交
130
      break;
C
Cary Xu 已提交
131 132
    }
    case TDMT_VND_ALTER_STB: {
L
Liu Jicong 已提交
133
      SVCreateTbReq vAlterTbReq = {0};
S
Shengliang Guan 已提交
134
      vTrace("vgId:%d, process alter stb req", pVnode->vgId);
C
Cary Xu 已提交
135 136 137 138
      tDeserializeSVCreateTbReq(POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), &vAlterTbReq);
      free(vAlterTbReq.stbCfg.pSchema);
      free(vAlterTbReq.stbCfg.pTagSchema);
      free(vAlterTbReq.name);
S
Shengliang Guan 已提交
139
      break;
C
Cary Xu 已提交
140
    }
H
Hongze Cheng 已提交
141
    case TDMT_VND_DROP_STB:
S
Shengliang Guan 已提交
142 143
      vTrace("vgId:%d, process drop stb req", pVnode->vgId);
      break;
S
Shengliang Guan 已提交
144
    case TDMT_VND_DROP_TABLE:
H
Hongze Cheng 已提交
145 146 147
      // if (metaDropTable(pVnode->pMeta, vReq.dtReq.uid) < 0) {
      //   // TODO: handle error
      // }
H
more  
Hongze Cheng 已提交
148
      break;
H
Hongze Cheng 已提交
149
    case TDMT_VND_SUBMIT:
L
Liu Jicong 已提交
150 151 152 153
      if (pVnode->config.streamMode == 0) {
        if (tsdbInsertData(pVnode->pTsdb, (SSubmitReq *)ptr, NULL) < 0) {
          // TODO: handle error
        }
H
more  
Hongze Cheng 已提交
154
      }
H
more  
Hongze Cheng 已提交
155
      break;
L
Liu Jicong 已提交
156
    case TDMT_VND_MQ_SET_CONN: {
L
Liu Jicong 已提交
157
      if (tqProcessSetConnReq(pVnode->pTq, POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead))) < 0) {
L
Liu Jicong 已提交
158
        // TODO: handle error
L
Liu Jicong 已提交
159
      }
L
Liu Jicong 已提交
160
    } break;
L
Liu Jicong 已提交
161
    case TDMT_VND_MQ_REB: {
L
Liu Jicong 已提交
162
      if (tqProcessRebReq(pVnode->pTq, POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead))) < 0) {
L
Liu Jicong 已提交
163 164
      }
    } break;
L
Liu Jicong 已提交
165 166 167 168 169
    case TDMT_VND_TASK_DEPLOY: {
      if (tqProcessTaskDeploy(pVnode->pTq, POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)),
                              pMsg->contLen - sizeof(SMsgHead)) < 0) {
      }
    } break;
C
Cary Xu 已提交
170
    case TDMT_VND_CREATE_SMA: {  // timeRangeSMA
S
sma  
Shengliang Guan 已提交
171
#if 0
C
Cary Xu 已提交
172 173 174 175 176 177
      SSmaCfg vCreateSmaReq = {0};
      if (tDeserializeSVCreateTSmaReq(POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), &vCreateSmaReq) == NULL) {
        terrno = TSDB_CODE_OUT_OF_MEMORY;
        return -1;
      }

178 179 180
      // record current timezone of server side
      tstrncpy(vCreateSmaReq.tSma.timezone, tsTimezone, TD_TIMEZONE_LEN);

C
Cary Xu 已提交
181 182 183 184 185 186 187 188 189 190 191 192
      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?
S
sma  
Shengliang Guan 已提交
193
#endif
C
Cary Xu 已提交
194 195 196 197
    } break;
    case TDMT_VND_CANCEL_SMA: {  // timeRangeSMA
    } break;
    case TDMT_VND_DROP_SMA: {  // timeRangeSMA
S
sma  
Shengliang Guan 已提交
198
#if 0    
C
Cary Xu 已提交
199 200 201 202 203 204 205 206 207 208 209
      SVDropTSmaReq vDropSmaReq = {0};
      if (tDeserializeSVDropTSmaReq(POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), &vDropSmaReq) == NULL) {
        terrno = TSDB_CODE_OUT_OF_MEMORY;
        return -1;
      }

      // TODO: send msg to stream computing to drop tSma
      // if ((send msg to stream computing) < 0) {
      //   tdDestroyTSma(&vCreateSmaReq);
      //   return -1;
      // }
C
Cary Xu 已提交
210 211 212 213 214 215 216 217 218 219 220 221
      // 

      if (metaDropTSma(pVnode->pMeta, vDropSmaReq.indexUid) < 0) {
        // TODO: handle error
        return -1;
      }

      if(tsdbDropTSmaData(pVnode->pTsdb, vDropSmaReq.indexUid) < 0) {
        // TODO: handle error
        return -1;
      }

C
Cary Xu 已提交
222
      // TODO: return directly or go on follow steps?
S
sma  
Shengliang Guan 已提交
223
#endif
C
Cary Xu 已提交
224
    } break;
H
more  
Hongze Cheng 已提交
225
    default:
H
more  
Hongze Cheng 已提交
226
      ASSERT(0);
H
more  
Hongze Cheng 已提交
227
      break;
H
refact  
Hongze Cheng 已提交
228 229
  }

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

H
more  
Hongze Cheng 已提交
232 233
  // Check if it needs to commit
  if (vnodeShouldCommit(pVnode)) {
H
Hongze Cheng 已提交
234
    // tsem_wait(&(pVnode->canCommit));
H
more  
Hongze Cheng 已提交
235 236 237 238
    if (vnodeAsyncCommit(pVnode) < 0) {
      // TODO: handle error
    }
  }
H
Hongze Cheng 已提交
239

H
more  
Hongze Cheng 已提交
240 241 242
  return 0;
}

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