You need to sign in or sign up before continuing.
vnodeWrite.c 9.7 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
void smaHandleRes(void *pVnode, int64_t smaId, const SArray *data) {
  // TODO
C
Cary Xu 已提交
20

L
Liu Jicong 已提交
21
  blockDebugShowData(data);
C
Cary Xu 已提交
22
  tsdbInsertTSmaData(((SVnode *)pVnode)->pTsdb, smaId, (const char *)data);
L
Liu Jicong 已提交
23 24
}

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

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

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

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

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

H
more  
Hongze Cheng 已提交
47
  // TODO: Integrate RAFT module here
H
more  
Hongze Cheng 已提交
48

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

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

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

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

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

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

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

D
dapan1121 已提交
104 105 106 107 108 109 110
        int32_t code = vnodeValidateTableHash(&pVnode->config, tableFName);
        if (code) {
          SVCreateTbRsp rsp;
          rsp.code = code;

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

H
Hongze Cheng 已提交
112 113
        if (metaCreateTable(pVnode->pMeta, pCreateTbReq) < 0) {
          // TODO: handle error
114
          vError("vgId:%d, failed to create table: %s", pVnode->vgId, pCreateTbReq->name);
H
Hongze Cheng 已提交
115
        }
C
Cary Xu 已提交
116
        // TODO: to encapsule a free API
wafwerar's avatar
wafwerar 已提交
117
        taosMemoryFree(pCreateTbReq->name);
118
        taosMemoryFree(pCreateTbReq->dbFName);
H
Hongze Cheng 已提交
119
        if (pCreateTbReq->type == TD_SUPER_TABLE) {
wafwerar's avatar
wafwerar 已提交
120 121
          taosMemoryFree(pCreateTbReq->stbCfg.pSchema);
          taosMemoryFree(pCreateTbReq->stbCfg.pTagSchema);
C
Cary Xu 已提交
122 123 124 125
          if (pCreateTbReq->stbCfg.pRSmaParam) {
            taosMemoryFree(pCreateTbReq->stbCfg.pRSmaParam->pFuncIds);
            taosMemoryFree(pCreateTbReq->stbCfg.pRSmaParam);
          }
H
Hongze Cheng 已提交
126
        } else if (pCreateTbReq->type == TD_CHILD_TABLE) {
wafwerar's avatar
wafwerar 已提交
127
          taosMemoryFree(pCreateTbReq->ctbCfg.pTag);
H
Hongze Cheng 已提交
128
        } else {
wafwerar's avatar
wafwerar 已提交
129
          taosMemoryFree(pCreateTbReq->ntbCfg.pSchema);
C
Cary Xu 已提交
130 131 132 133
          if (pCreateTbReq->ntbCfg.pRSmaParam) {
            taosMemoryFree(pCreateTbReq->ntbCfg.pRSmaParam->pFuncIds);
            taosMemoryFree(pCreateTbReq->ntbCfg.pRSmaParam);
          }
H
Hongze Cheng 已提交
134
        }
H
Hongze Cheng 已提交
135
      }
136

S
Shengliang Guan 已提交
137
      vTrace("vgId:%d process create %" PRIzu " tables", pVnode->vgId, taosArrayGetSize(vCreateTbBatchReq.pArray));
H
Hongze Cheng 已提交
138
      taosArrayDestroy(vCreateTbBatchReq.pArray);
D
dapan1121 已提交
139
      if (vCreateTbBatchRsp.rspList) {
D
dapan 已提交
140
        int32_t contLen = tSerializeSVCreateTbBatchRsp(NULL, 0, &vCreateTbBatchRsp);
L
Liu Jicong 已提交
141
        void   *msg = rpcMallocCont(contLen);
D
dapan 已提交
142 143
        tSerializeSVCreateTbBatchRsp(msg, contLen, &vCreateTbBatchRsp);
        taosArrayDestroy(vCreateTbBatchRsp.rspList);
L
Liu Jicong 已提交
144

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

C
Cary Xu 已提交
207 208 209
      SSmaCfg vCreateSmaReq = {0};
      if (tDeserializeSVCreateTSmaReq(POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), &vCreateSmaReq) == NULL) {
        terrno = TSDB_CODE_OUT_OF_MEMORY;
C
Cary Xu 已提交
210
        vWarn("vgId:%d TDMT_VND_CREATE_SMA received but deserialize failed since %s", pVnode->config.vgId,
L
Liu Jicong 已提交
211
              terrstr(terrno));
C
Cary Xu 已提交
212 213
        return -1;
      }
C
Cary Xu 已提交
214 215
      vDebug("vgId:%d TDMT_VND_CREATE_SMA msg received for %s:%" PRIi64, pVnode->config.vgId,
             vCreateSmaReq.tSma.indexName, vCreateSmaReq.tSma.indexUid);
C
Cary Xu 已提交
216

217
      // record current timezone of server side
C
Cary Xu 已提交
218
      vCreateSmaReq.tSma.timezoneInt = tsTimezone;
219

C
Cary Xu 已提交
220 221 222 223 224
      if (metaCreateTSma(pVnode->pMeta, &vCreateSmaReq) < 0) {
        // TODO: handle error
        tdDestroyTSma(&vCreateSmaReq.tSma);
        return -1;
      }
C
Cary Xu 已提交
225 226 227

      tsdbTSmaAdd(pVnode->pTsdb, 1);

C
Cary Xu 已提交
228 229
      tdDestroyTSma(&vCreateSmaReq.tSma);
      // TODO: return directly or go on follow steps?
S
sma  
Shengliang Guan 已提交
230
#endif
C
Cary Xu 已提交
231 232 233
      if (tsdbCreateTSma(pVnode->pTsdb, POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead))) < 0) {
        // TODO
      }
C
Cary Xu 已提交
234 235 236 237
    } break;
    case TDMT_VND_CANCEL_SMA: {  // timeRangeSMA
    } break;
    case TDMT_VND_DROP_SMA: {  // timeRangeSMA
C
Cary Xu 已提交
238 239 240
      if (tsdbDropTSma(pVnode->pTsdb, POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead))) < 0) {
        // TODO
      }
S
sma  
Shengliang Guan 已提交
241
#if 0    
C
Cary Xu 已提交
242
      tsdbTSmaSub(pVnode->pTsdb, 1);
C
Cary Xu 已提交
243 244 245 246 247 248 249 250 251 252 253
      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 已提交
254 255 256 257 258 259 260 261 262 263 264 265
      // 

      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 已提交
266
      // TODO: return directly or go on follow steps?
S
sma  
Shengliang Guan 已提交
267
#endif
C
Cary Xu 已提交
268
    } break;
H
more  
Hongze Cheng 已提交
269
    default:
H
more  
Hongze Cheng 已提交
270
      ASSERT(0);
H
more  
Hongze Cheng 已提交
271
      break;
H
refact  
Hongze Cheng 已提交
272 273
  }

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

H
more  
Hongze Cheng 已提交
276 277
  // Check if it needs to commit
  if (vnodeShouldCommit(pVnode)) {
H
Hongze Cheng 已提交
278
    // tsem_wait(&(pVnode->canCommit));
H
more  
Hongze Cheng 已提交
279 280 281 282
    if (vnodeAsyncCommit(pVnode) < 0) {
      // TODO: handle error
    }
  }
H
Hongze Cheng 已提交
283

H
more  
Hongze Cheng 已提交
284 285 286
  return 0;
}

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