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

L
Liu Jicong 已提交
18 19 20 21 22
void smaHandleRes(void *pVnode, int64_t smaId, const SArray *data) {
  // TODO
  blockDebugShowData(data);
}

S
Shengliang Guan 已提交
23
void vnodeProcessWMsgs(SVnode *pVnode, SArray *pMsgs) {
S
shm  
Shengliang Guan 已提交
24
  SNodeMsg *pMsg;
L
Liu Jicong 已提交
25
  SRpcMsg  *pRpc;
H
more  
Hongze Cheng 已提交
26

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

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

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

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

H
more  
Hongze Cheng 已提交
45
  // TODO: Integrate RAFT module here
H
more  
Hongze Cheng 已提交
46

S
Shengliang Guan 已提交
47 48
  // No results are returned because error handling is difficult
  // return 0;
H
more  
Hongze Cheng 已提交
49 50
}

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

L
Liu Jicong 已提交
54 55 56 57 58 59 60 61 62
  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 已提交
63 64

  // todo: change the interface here
L
Liu Jicong 已提交
65 66
  int64_t ver;
  taosDecodeFixedI64(POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), &ver);
L
Liu Jicong 已提交
67
  if (tqPushMsg(pVnode->pTq, pMsg->pCont, pMsg->contLen, pMsg->msgType, ver) < 0) {
H
more  
Hongze Cheng 已提交
68 69
    // TODO: handle error
  }
H
more  
Hongze Cheng 已提交
70

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

C
Cary Xu 已提交
79
      // TODO: maybe need to clear the request struct
wafwerar's avatar
wafwerar 已提交
80 81
      taosMemoryFree(vCreateTbReq.stbCfg.pSchema);
      taosMemoryFree(vCreateTbReq.stbCfg.pTagSchema);
82 83 84
      taosMemoryFree(vCreateTbReq.stbCfg.pBSmaCols);
      taosMemoryFree(vCreateTbReq.stbCfg.pRSmaParam);
      taosMemoryFree(vCreateTbReq.dbFName);
wafwerar's avatar
wafwerar 已提交
85
      taosMemoryFree(vCreateTbReq.name);
H
more  
Hongze Cheng 已提交
86
      break;
C
Cary Xu 已提交
87 88 89
    }
    case TDMT_VND_CREATE_TABLE: {
      SVCreateTbBatchReq vCreateTbBatchReq = {0};
D
dapan1121 已提交
90
      SVCreateTbBatchRsp vCreateTbBatchRsp = {0};
S
Shengliang Guan 已提交
91
      tDeserializeSVCreateTbBatchReq(POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), &vCreateTbBatchReq);
D
dapan1121 已提交
92 93
      int reqNum = taosArrayGetSize(vCreateTbBatchReq.pArray);
      for (int i = 0; i < reqNum; i++) {
H
Hongze Cheng 已提交
94
        SVCreateTbReq *pCreateTbReq = taosArrayGet(vCreateTbBatchReq.pArray, i);
D
dapan1121 已提交
95

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

D
dapan1121 已提交
100 101 102 103 104 105 106
        int32_t code = vnodeValidateTableHash(&pVnode->config, tableFName);
        if (code) {
          SVCreateTbRsp rsp;
          rsp.code = code;

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

H
Hongze Cheng 已提交
108 109
        if (metaCreateTable(pVnode->pMeta, pCreateTbReq) < 0) {
          // TODO: handle error
110
          vError("vgId:%d, failed to create table: %s", pVnode->vgId, pCreateTbReq->name);
H
Hongze Cheng 已提交
111
        }
wafwerar's avatar
wafwerar 已提交
112
        taosMemoryFree(pCreateTbReq->name);
113
        taosMemoryFree(pCreateTbReq->dbFName);
H
Hongze Cheng 已提交
114
        if (pCreateTbReq->type == TD_SUPER_TABLE) {
wafwerar's avatar
wafwerar 已提交
115 116
          taosMemoryFree(pCreateTbReq->stbCfg.pSchema);
          taosMemoryFree(pCreateTbReq->stbCfg.pTagSchema);
117 118
          taosMemoryFree(pCreateTbReq->stbCfg.pBSmaCols);
          taosMemoryFree(pCreateTbReq->stbCfg.pRSmaParam);
H
Hongze Cheng 已提交
119
        } else if (pCreateTbReq->type == TD_CHILD_TABLE) {
wafwerar's avatar
wafwerar 已提交
120
          taosMemoryFree(pCreateTbReq->ctbCfg.pTag);
H
Hongze Cheng 已提交
121
        } else {
wafwerar's avatar
wafwerar 已提交
122
          taosMemoryFree(pCreateTbReq->ntbCfg.pSchema);
123 124
          taosMemoryFree(pCreateTbReq->ntbCfg.pBSmaCols);
          taosMemoryFree(pCreateTbReq->ntbCfg.pRSmaParam);
H
Hongze Cheng 已提交
125
        }
H
Hongze Cheng 已提交
126
      }
127

S
Shengliang Guan 已提交
128
      vTrace("vgId:%d process create %" PRIzu " tables", pVnode->vgId, taosArrayGetSize(vCreateTbBatchReq.pArray));
H
Hongze Cheng 已提交
129
      taosArrayDestroy(vCreateTbBatchReq.pArray);
D
dapan1121 已提交
130
      if (vCreateTbBatchRsp.rspList) {
D
dapan 已提交
131
        int32_t contLen = tSerializeSVCreateTbBatchRsp(NULL, 0, &vCreateTbBatchRsp);
L
Liu Jicong 已提交
132
        void   *msg = rpcMallocCont(contLen);
D
dapan 已提交
133 134
        tSerializeSVCreateTbBatchRsp(msg, contLen, &vCreateTbBatchRsp);
        taosArrayDestroy(vCreateTbBatchRsp.rspList);
L
Liu Jicong 已提交
135

wafwerar's avatar
wafwerar 已提交
136
        *pRsp = taosMemoryCalloc(1, sizeof(SRpcMsg));
D
dapan 已提交
137 138 139 140 141
        (*pRsp)->msgType = TDMT_VND_CREATE_TABLE_RSP;
        (*pRsp)->pCont = msg;
        (*pRsp)->contLen = contLen;
        (*pRsp)->handle = pMsg->handle;
        (*pRsp)->ahandle = pMsg->ahandle;
D
dapan1121 已提交
142
      }
H
Hongze Cheng 已提交
143
      break;
C
Cary Xu 已提交
144 145
    }
    case TDMT_VND_ALTER_STB: {
L
Liu Jicong 已提交
146
      SVCreateTbReq vAlterTbReq = {0};
S
Shengliang Guan 已提交
147
      vTrace("vgId:%d, process alter stb req", pVnode->vgId);
C
Cary Xu 已提交
148
      tDeserializeSVCreateTbReq(POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), &vAlterTbReq);
wafwerar's avatar
wafwerar 已提交
149 150
      taosMemoryFree(vAlterTbReq.stbCfg.pSchema);
      taosMemoryFree(vAlterTbReq.stbCfg.pTagSchema);
151 152 153
      taosMemoryFree(vAlterTbReq.stbCfg.pBSmaCols);
      taosMemoryFree(vAlterTbReq.stbCfg.pRSmaParam);
      taosMemoryFree(vAlterTbReq.dbFName);
wafwerar's avatar
wafwerar 已提交
154
      taosMemoryFree(vAlterTbReq.name);
S
Shengliang Guan 已提交
155
      break;
C
Cary Xu 已提交
156
    }
H
Hongze Cheng 已提交
157
    case TDMT_VND_DROP_STB:
S
Shengliang Guan 已提交
158 159
      vTrace("vgId:%d, process drop stb req", pVnode->vgId);
      break;
S
Shengliang Guan 已提交
160
    case TDMT_VND_DROP_TABLE:
H
Hongze Cheng 已提交
161 162 163
      // if (metaDropTable(pVnode->pMeta, vReq.dtReq.uid) < 0) {
      //   // TODO: handle error
      // }
H
more  
Hongze Cheng 已提交
164
      break;
H
Hongze Cheng 已提交
165
    case TDMT_VND_SUBMIT:
L
Liu Jicong 已提交
166 167 168 169
      if (pVnode->config.streamMode == 0) {
        if (tsdbInsertData(pVnode->pTsdb, (SSubmitReq *)ptr, NULL) < 0) {
          // TODO: handle error
        }
H
more  
Hongze Cheng 已提交
170
      }
H
more  
Hongze Cheng 已提交
171
      break;
L
Liu Jicong 已提交
172
    case TDMT_VND_MQ_SET_CONN: {
L
Liu Jicong 已提交
173
      if (tqProcessSetConnReq(pVnode->pTq, POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead))) < 0) {
L
Liu Jicong 已提交
174
        // TODO: handle error
L
Liu Jicong 已提交
175
      }
L
Liu Jicong 已提交
176
    } break;
L
Liu Jicong 已提交
177
    case TDMT_VND_MQ_REB: {
L
Liu Jicong 已提交
178
      if (tqProcessRebReq(pVnode->pTq, POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead))) < 0) {
L
Liu Jicong 已提交
179 180
      }
    } break;
L
Liu Jicong 已提交
181 182 183 184 185
    case TDMT_VND_TASK_DEPLOY: {
      if (tqProcessTaskDeploy(pVnode->pTq, POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)),
                              pMsg->contLen - sizeof(SMsgHead)) < 0) {
      }
    } break;
L
Liu Jicong 已提交
186 187 188 189 190
    case TDMT_VND_TASK_WRITE_EXEC: {
      if (tqProcessTaskExec(pVnode->pTq, POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)),
                            pMsg->contLen - sizeof(SMsgHead)) < 0) {
      }
    } break;
C
Cary Xu 已提交
191
    case TDMT_VND_CREATE_SMA: {  // timeRangeSMA
S
sma  
Shengliang Guan 已提交
192
#if 0
C
Cary Xu 已提交
193 194 195 196 197 198
      SSmaCfg vCreateSmaReq = {0};
      if (tDeserializeSVCreateTSmaReq(POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), &vCreateSmaReq) == NULL) {
        terrno = TSDB_CODE_OUT_OF_MEMORY;
        return -1;
      }

199
      // record current timezone of server side
wafwerar's avatar
wafwerar 已提交
200
      tstrncpy(vCreateSmaReq.tSma.timezone, tsTimezoneStr, TD_TIMEZONE_LEN);
201

C
Cary Xu 已提交
202 203 204 205 206 207 208 209 210 211 212 213
      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 已提交
214
#endif
C
Cary Xu 已提交
215 216 217 218
    } break;
    case TDMT_VND_CANCEL_SMA: {  // timeRangeSMA
    } break;
    case TDMT_VND_DROP_SMA: {  // timeRangeSMA
S
sma  
Shengliang Guan 已提交
219
#if 0    
C
Cary Xu 已提交
220 221 222 223 224 225 226 227 228 229 230
      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 已提交
231 232 233 234 235 236 237 238 239 240 241 242
      // 

      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 已提交
243
      // TODO: return directly or go on follow steps?
S
sma  
Shengliang Guan 已提交
244
#endif
C
Cary Xu 已提交
245
    } break;
H
more  
Hongze Cheng 已提交
246
    default:
H
more  
Hongze Cheng 已提交
247
      ASSERT(0);
H
more  
Hongze Cheng 已提交
248
      break;
H
refact  
Hongze Cheng 已提交
249 250
  }

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

H
more  
Hongze Cheng 已提交
253 254
  // Check if it needs to commit
  if (vnodeShouldCommit(pVnode)) {
H
Hongze Cheng 已提交
255
    // tsem_wait(&(pVnode->canCommit));
H
more  
Hongze Cheng 已提交
256 257 258 259
    if (vnodeAsyncCommit(pVnode) < 0) {
      // TODO: handle error
    }
  }
H
Hongze Cheng 已提交
260

H
more  
Hongze Cheng 已提交
261 262 263
  return 0;
}

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