vnodeSvr.c 50.6 KB
Newer Older
H
Hongze Cheng 已提交
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/>.
 */

H
Hongze Cheng 已提交
16 17
#include "tencode.h"
#include "tmsg.h"
H
Hongze Cheng 已提交
18
#include "vnd.h"
H
Hongze Cheng 已提交
19 20
#include "vnode.h"
#include "vnodeInt.h"
H
Hongze Cheng 已提交
21

22 23 24 25 26 27 28 29 30
static int32_t vnodeProcessCreateStbReq(SVnode *pVnode, int64_t version, void *pReq, int32_t len, SRpcMsg *pRsp);
static int32_t vnodeProcessAlterStbReq(SVnode *pVnode, int64_t version, void *pReq, int32_t len, SRpcMsg *pRsp);
static int32_t vnodeProcessDropStbReq(SVnode *pVnode, int64_t version, void *pReq, int32_t len, SRpcMsg *pRsp);
static int32_t vnodeProcessCreateTbReq(SVnode *pVnode, int64_t version, void *pReq, int32_t len, SRpcMsg *pRsp);
static int32_t vnodeProcessAlterTbReq(SVnode *pVnode, int64_t version, void *pReq, int32_t len, SRpcMsg *pRsp);
static int32_t vnodeProcessDropTbReq(SVnode *pVnode, int64_t version, void *pReq, int32_t len, SRpcMsg *pRsp);
static int32_t vnodeProcessSubmitReq(SVnode *pVnode, int64_t version, void *pReq, int32_t len, SRpcMsg *pRsp);
static int32_t vnodeProcessCreateTSmaReq(SVnode *pVnode, int64_t version, void *pReq, int32_t len, SRpcMsg *pRsp);
static int32_t vnodeProcessAlterConfirmReq(SVnode *pVnode, int64_t version, void *pReq, int32_t len, SRpcMsg *pRsp);
31
static int32_t vnodeProcessAlterConfigReq(SVnode *pVnode, int64_t version, void *pReq, int32_t len, SRpcMsg *pRsp);
32
static int32_t vnodeProcessDropTtlTbReq(SVnode *pVnode, int64_t version, void *pReq, int32_t len, SRpcMsg *pRsp);
S
Shengliang Guan 已提交
33
static int32_t vnodeProcessTrimReq(SVnode *pVnode, int64_t version, void *pReq, int32_t len, SRpcMsg *pRsp);
H
Hongze Cheng 已提交
34
static int32_t vnodeProcessDeleteReq(SVnode *pVnode, int64_t version, void *pReq, int32_t len, SRpcMsg *pRsp);
35
static int32_t vnodeProcessBatchDeleteReq(SVnode *pVnode, int64_t version, void *pReq, int32_t len, SRpcMsg *pRsp);
dengyihao's avatar
dengyihao 已提交
36 37
static int32_t vnodeProcessCreateIndexReq(SVnode *pVnode, int64_t version, void *pReq, int32_t len, SRpcMsg *pRsp);
static int32_t vnodeProcessDropIndexReq(SVnode *pVnode, int64_t version, void *pReq, int32_t len, SRpcMsg *pRsp);
38
static int32_t vnodeProcessCompactVnodeReq(SVnode *pVnode, int64_t version, void *pReq, int32_t len, SRpcMsg *pRsp);
H
Hongze Cheng 已提交
39

H
Hongze Cheng 已提交
40
static int32_t vnodePreprocessCreateTableReq(SVnode *pVnode, SDecoder *pCoder, int64_t ctime, int64_t *pUid) {
H
Hongze Cheng 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
  int32_t code = 0;
  int32_t lino = 0;

  if (tStartDecode(pCoder) < 0) {
    code = TSDB_CODE_INVALID_MSG;
    TSDB_CHECK_CODE(code, lino, _exit);
  }

  // flags
  if (tDecodeI32v(pCoder, NULL) < 0) {
    code = TSDB_CODE_INVALID_MSG;
    TSDB_CHECK_CODE(code, lino, _exit);
  }

  // name
  char *name = NULL;
  if (tDecodeCStr(pCoder, &name) < 0) {
    code = TSDB_CODE_INVALID_MSG;
    TSDB_CHECK_CODE(code, lino, _exit);
  }

  // uid
  int64_t uid = metaGetTableEntryUidByName(pVnode->pMeta, name);
  if (uid == 0) {
    uid = tGenIdPI64();
  }
  *(int64_t *)(pCoder->data + pCoder->pos) = uid;

  // ctime
  *(int64_t *)(pCoder->data + pCoder->pos + 8) = ctime;

  tEndDecode(pCoder);

_exit:
  if (code) {
    vError("vgId:%d %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
  } else {
    vTrace("vgId:%d %s done, table:%s uid generated:%" PRId64, TD_VID(pVnode), __func__, name, uid);
H
Hongze Cheng 已提交
79
    if (pUid) *pUid = uid;
H
Hongze Cheng 已提交
80 81 82
  }
  return code;
}
H
Hongze Cheng 已提交
83 84 85 86 87
static int32_t vnodePreProcessCreateTableMsg(SVnode *pVnode, SRpcMsg *pMsg) {
  int32_t code = 0;
  int32_t lino = 0;

  int64_t  ctime = taosGetTimestampMs();
H
Hongze Cheng 已提交
88
  SDecoder dc = {0};
H
Hongze Cheng 已提交
89
  int32_t  nReqs;
H
Hongze Cheng 已提交
90

H
Hongze Cheng 已提交
91 92 93 94 95 96 97 98 99 100 101
  tDecoderInit(&dc, (uint8_t *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead));
  if (tStartDecode(&dc) < 0) {
    code = TSDB_CODE_INVALID_MSG;
    return code;
  }

  if (tDecodeI32v(&dc, &nReqs) < 0) {
    code = TSDB_CODE_INVALID_MSG;
    TSDB_CHECK_CODE(code, lino, _exit);
  }
  for (int32_t iReq = 0; iReq < nReqs; iReq++) {
H
Hongze Cheng 已提交
102
    code = vnodePreprocessCreateTableReq(pVnode, &dc, ctime, NULL);
H
Hongze Cheng 已提交
103
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
104 105 106 107 108 109 110 111
  }

  tEndDecode(&dc);

_exit:
  tDecoderClear(&dc);
  return code;
}
H
Hongze Cheng 已提交
112 113
extern int64_t tsMaxKeyByPrecision[];
static int32_t vnodePreProcessSubmitTbData(SVnode *pVnode, SDecoder *pCoder, int64_t ctime) {
H
Hongze Cheng 已提交
114 115 116
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
117 118 119 120
  if (tStartDecode(pCoder) < 0) {
    code = TSDB_CODE_INVALID_MSG;
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
121

H
Hongze Cheng 已提交
122 123 124 125 126
  SSubmitTbData submitTbData;
  if (tDecodeI32v(pCoder, &submitTbData.flags) < 0) {
    code = TSDB_CODE_INVALID_MSG;
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
127

H
Hongze Cheng 已提交
128
  int64_t uid;
H
Hongze Cheng 已提交
129
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
H
Hongze Cheng 已提交
130
    code = vnodePreprocessCreateTableReq(pVnode, pCoder, ctime, &uid);
H
Hongze Cheng 已提交
131 132 133 134 135 136 137 138
    TSDB_CHECK_CODE(code, lino, _exit);
  }

  // submit data
  if (tDecodeI64(pCoder, &submitTbData.suid) < 0) {
    code = TSDB_CODE_INVALID_MSG;
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
139 140 141

  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
    *(int64_t *)(pCoder->data + pCoder->pos) = uid;
H
Hongze Cheng 已提交
142
    pCoder->pos += sizeof(int64_t);
H
Hongze Cheng 已提交
143
  } else {
H
Hongze Cheng 已提交
144 145 146 147
    if (tDecodeI64(pCoder, &submitTbData.uid) < 0) {
      code = TSDB_CODE_INVALID_MSG;
      TSDB_CHECK_CODE(code, lino, _exit);
    }
H
Hongze Cheng 已提交
148
  }
H
Hongze Cheng 已提交
149

H
Hongze Cheng 已提交
150
  if (tDecodeI32v(pCoder, &submitTbData.sver) < 0) {
H
Hongze Cheng 已提交
151 152 153 154
    code = TSDB_CODE_INVALID_MSG;
    TSDB_CHECK_CODE(code, lino, _exit);
  }

H
Hongze Cheng 已提交
155 156 157 158 159 160 161 162 163 164 165 166
  // scan and check
  TSKEY now = ctime;
  if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_MICRO) {
    now *= 1000;
  } else if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_NANO) {
    now *= 1000000;
  }
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * pVnode->config.tsdbCfg.keep2;
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
  if (submitTbData.flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
    uint64_t nColData;
    if (tDecodeU64v(pCoder, &nColData) < 0) {
H
Hongze Cheng 已提交
167
      code = TSDB_CODE_INVALID_MSG;
H
Hongze Cheng 已提交
168
      goto _exit;
H
Hongze Cheng 已提交
169 170
    }

H
Hongze Cheng 已提交
171 172 173
    SColData colData = {0};
    pCoder->pos += tGetColData(pCoder->data + pCoder->pos, &colData);

H
Hongze Cheng 已提交
174 175 176 177 178
    if (colData.flag != HAS_VALUE) {
      code = TSDB_CODE_INVALID_MSG;
      goto _exit;
    }

H
Hongze Cheng 已提交
179 180 181 182 183 184 185 186 187
    for (int32_t iRow = 0; iRow < colData.nVal; iRow++) {
      if (((TSKEY *)colData.pData)[iRow] < minKey || ((TSKEY *)colData.pData)[iRow] > maxKey) {
        code = TSDB_CODE_TDB_TIMESTAMP_OUT_OF_RANGE;
        goto _exit;
      }
    }
  } else {
    uint64_t nRow;
    if (tDecodeU64v(pCoder, &nRow) < 0) {
H
Hongze Cheng 已提交
188
      code = TSDB_CODE_INVALID_MSG;
H
Hongze Cheng 已提交
189
      goto _exit;
H
Hongze Cheng 已提交
190
    }
H
Hongze Cheng 已提交
191

H
Hongze Cheng 已提交
192 193 194
    for (int32_t iRow = 0; iRow < nRow; ++iRow) {
      SRow *pRow = (SRow *)(pCoder->data + pCoder->pos);
      pCoder->pos += pRow->len;
H
Hongze Cheng 已提交
195

H
Hongze Cheng 已提交
196 197 198
      if (pRow->ts < minKey || pRow->ts > maxKey) {
        code = TSDB_CODE_TDB_TIMESTAMP_OUT_OF_RANGE;
        goto _exit;
H
Hongze Cheng 已提交
199
      }
H
Hongze Cheng 已提交
200 201
    }
  }
H
Hongze Cheng 已提交
202

H
Hongze Cheng 已提交
203
  tEndDecode(pCoder);
H
Hongze Cheng 已提交
204

H
Hongze Cheng 已提交
205
_exit:
H
Hongze Cheng 已提交
206
  return code;
H
Hongze Cheng 已提交
207 208 209 210
}
static int32_t vnodePreProcessSubmitMsg(SVnode *pVnode, SRpcMsg *pMsg) {
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
211

H
Hongze Cheng 已提交
212
  SDecoder *pCoder = &(SDecoder){0};
H
Hongze Cheng 已提交
213

X
Xiaoyu Wang 已提交
214
  tDecoderInit(pCoder, (uint8_t *)pMsg->pCont + sizeof(SSubmitReq2Msg), pMsg->contLen - sizeof(SSubmitReq2Msg));
H
Hongze Cheng 已提交
215

H
Hongze Cheng 已提交
216 217 218 219
  if (tStartDecode(pCoder) < 0) {
    code = TSDB_CODE_INVALID_MSG;
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
220

H
Hongze Cheng 已提交
221 222 223 224 225
  uint64_t nSubmitTbData;
  if (tDecodeU64v(pCoder, &nSubmitTbData) < 0) {
    code = TSDB_CODE_INVALID_MSG;
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
226

H
Hongze Cheng 已提交
227 228 229 230
  int64_t ctime = taosGetTimestampMs();
  for (int32_t i = 0; i < nSubmitTbData; i++) {
    code = vnodePreProcessSubmitTbData(pVnode, pCoder, ctime);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
231
  }
H
Hongze Cheng 已提交
232

H
Hongze Cheng 已提交
233
  tEndDecode(pCoder);
H
Hongze Cheng 已提交
234

H
Hongze Cheng 已提交
235
_exit:
H
Hongze Cheng 已提交
236
  tDecoderClear(pCoder);
H
Hongze Cheng 已提交
237 238
  return code;
}
H
Hongze Cheng 已提交
239

H
Hongze Cheng 已提交
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
static int32_t vnodePreProcessDeleteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
  int32_t code = 0;

  int32_t     size;
  int32_t     ret;
  uint8_t    *pCont;
  SEncoder   *pCoder = &(SEncoder){0};
  SDeleteRes  res = {0};
  SReadHandle handle = {.meta = pVnode->pMeta, .config = &pVnode->config, .vnode = pVnode, .pMsgCb = &pVnode->msgCb};

  code = qWorkerProcessDeleteMsg(&handle, pVnode->pQuery, pMsg, &res);
  if (code) goto _exit;

  // malloc and encode
  tEncodeSize(tEncodeDeleteRes, &res, size, ret);
  pCont = rpcMallocCont(size + sizeof(SMsgHead));
H
Hongze Cheng 已提交
256

H
Hongze Cheng 已提交
257 258
  ((SMsgHead *)pCont)->contLen = size + sizeof(SMsgHead);
  ((SMsgHead *)pCont)->vgId = TD_VID(pVnode);
H
Hongze Cheng 已提交
259

H
Hongze Cheng 已提交
260 261 262
  tEncoderInit(pCoder, pCont + sizeof(SMsgHead), size);
  tEncodeDeleteRes(pCoder, &res);
  tEncoderClear(pCoder);
H
Hongze Cheng 已提交
263

H
Hongze Cheng 已提交
264 265 266
  rpcFreeCont(pMsg->pCont);
  pMsg->pCont = pCont;
  pMsg->contLen = size + sizeof(SMsgHead);
H
Hongze Cheng 已提交
267

H
Hongze Cheng 已提交
268 269 270 271 272
  taosArrayDestroy(res.uidList);

_exit:
  return code;
}
H
Hongze Cheng 已提交
273

H
Hongze Cheng 已提交
274 275 276 277 278 279 280 281 282 283 284 285
int32_t vnodePreProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
  int32_t code = 0;

  switch (pMsg->msgType) {
    case TDMT_VND_CREATE_TABLE: {
      code = vnodePreProcessCreateTableMsg(pVnode, pMsg);
    } break;
    case TDMT_VND_SUBMIT: {
      code = vnodePreProcessSubmitMsg(pVnode, pMsg);
    } break;
    case TDMT_VND_DELETE: {
      code = vnodePreProcessDeleteMsg(pVnode, pMsg);
H
Hongze Cheng 已提交
286
    } break;
H
Hongze Cheng 已提交
287 288 289
    default:
      break;
  }
H
Hongze Cheng 已提交
290

H
Hongze Cheng 已提交
291 292 293 294 295
_exit:
  if (code) {
    vError("vgId%d failed to preprocess write request since %s, msg type:%d", TD_VID(pVnode), tstrerror(code),
           pMsg->msgType);
  }
H
Hongze Cheng 已提交
296
  return code;
H
Hongze Cheng 已提交
297 298
}

299
int32_t vnodeProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg, int64_t version, SRpcMsg *pRsp) {
300 301 302 303
  void   *ptr = NULL;
  void   *pReq;
  int32_t len;
  int32_t ret;
304
  /*
305
  if (!pVnode->inUse) {
306
    terrno = TSDB_CODE_VND_NO_AVAIL_BUFPOOL;
H
Hongze Cheng 已提交
307 308
    vError("vgId:%d, not ready to write since %s", TD_VID(pVnode), terrstr());
    return -1;
309
  }
310
  */
311 312 313
  if (version <= pVnode->state.applied) {
    vError("vgId:%d, duplicate write request. version: %" PRId64 ", applied: %" PRId64 "", TD_VID(pVnode), version,
           pVnode->state.applied);
314
    terrno = TSDB_CODE_VND_DUP_REQUEST;
315 316 317
    return -1;
  }

318
  vDebug("vgId:%d, start to process write request %s, index:%" PRId64, TD_VID(pVnode), TMSG_INFO(pMsg->msgType),
H
Hongze Cheng 已提交
319
         version);
H
Hongze Cheng 已提交
320

321 322
  ASSERT(pVnode->state.applyTerm <= pMsg->info.conn.applyTerm);
  ASSERT(pVnode->state.applied + 1 == version);
323

H
Hongze Cheng 已提交
324
  pVnode->state.applied = version;
H
Hongze Cheng 已提交
325
  pVnode->state.applyTerm = pMsg->info.conn.applyTerm;
H
Hongze Cheng 已提交
326

327 328
  if (!syncUtilUserCommit(pMsg->msgType)) goto _exit;

L
Liu Jicong 已提交
329
  if (pMsg->msgType == TDMT_VND_STREAM_RECOVER_BLOCKING_STAGE || pMsg->msgType == TDMT_STREAM_TASK_CHECK_RSP) {
L
Liu Jicong 已提交
330 331 332
    if (tqCheckLogInWal(pVnode->pTq, version)) return 0;
  }

H
Hongze Cheng 已提交
333 334 335
  // skip header
  pReq = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
  len = pMsg->contLen - sizeof(SMsgHead);
B
Benguang Zhao 已提交
336
  bool needCommit = false;
H
Hongze Cheng 已提交
337 338

  switch (pMsg->msgType) {
H
Hongze Cheng 已提交
339
    /* META */
H
Hongze Cheng 已提交
340
    case TDMT_VND_CREATE_STB:
H
Hongze Cheng 已提交
341
      if (vnodeProcessCreateStbReq(pVnode, version, pReq, len, pRsp) < 0) goto _err;
H
Hongze Cheng 已提交
342
      break;
H
Hongze Cheng 已提交
343
    case TDMT_VND_ALTER_STB:
H
Hongze Cheng 已提交
344
      if (vnodeProcessAlterStbReq(pVnode, version, pReq, len, pRsp) < 0) goto _err;
H
Hongze Cheng 已提交
345
      break;
H
Hongze Cheng 已提交
346
    case TDMT_VND_DROP_STB:
H
Hongze Cheng 已提交
347
      if (vnodeProcessDropStbReq(pVnode, version, pReq, len, pRsp) < 0) goto _err;
H
Hongze Cheng 已提交
348
      break;
H
Hongze Cheng 已提交
349
    case TDMT_VND_CREATE_TABLE:
H
Hongze Cheng 已提交
350
      if (vnodeProcessCreateTbReq(pVnode, version, pReq, len, pRsp) < 0) goto _err;
H
Hongze Cheng 已提交
351 352
      break;
    case TDMT_VND_ALTER_TABLE:
H
Hongze Cheng 已提交
353
      if (vnodeProcessAlterTbReq(pVnode, version, pReq, len, pRsp) < 0) goto _err;
H
Hongze Cheng 已提交
354
      break;
H
Hongze Cheng 已提交
355
    case TDMT_VND_DROP_TABLE:
H
Hongze Cheng 已提交
356
      if (vnodeProcessDropTbReq(pVnode, version, pReq, len, pRsp) < 0) goto _err;
H
Hongze Cheng 已提交
357
      break;
358
    case TDMT_VND_DROP_TTL_TABLE:
359
      if (vnodeProcessDropTtlTbReq(pVnode, version, pReq, len, pRsp) < 0) goto _err;
360
      break;
S
Shengliang Guan 已提交
361 362 363 364
    case TDMT_VND_TRIM:
      if (vnodeProcessTrimReq(pVnode, version, pReq, len, pRsp) < 0) goto _err;
      break;
    case TDMT_VND_CREATE_SMA:
365
      if (vnodeProcessCreateTSmaReq(pVnode, version, pReq, len, pRsp) < 0) goto _err;
S
Shengliang Guan 已提交
366
      break;
H
Hongze Cheng 已提交
367
    /* TSDB */
H
Hongze Cheng 已提交
368
    case TDMT_VND_SUBMIT:
369
      if (vnodeProcessSubmitReq(pVnode, version, pMsg->pCont, pMsg->contLen, pRsp) < 0) goto _err;
H
Hongze Cheng 已提交
370
      break;
D
dapan1121 已提交
371
    case TDMT_VND_DELETE:
H
Hongze Cheng 已提交
372
      if (vnodeProcessDeleteReq(pVnode, version, pReq, len, pRsp) < 0) goto _err;
D
dapan1121 已提交
373
      break;
374 375 376
    case TDMT_VND_BATCH_DEL:
      if (vnodeProcessBatchDeleteReq(pVnode, version, pReq, len, pRsp) < 0) goto _err;
      break;
H
Hongze Cheng 已提交
377
    /* TQ */
L
Liu Jicong 已提交
378
    case TDMT_VND_TMQ_SUBSCRIBE:
379
      if (tqProcessSubscribeReq(pVnode->pTq, version, pReq, len) < 0) {
380
        goto _err;
L
Liu Jicong 已提交
381 382
      }
      break;
L
Liu Jicong 已提交
383 384
    case TDMT_VND_TMQ_DELETE_SUB:
      if (tqProcessDeleteSubReq(pVnode->pTq, version, pMsg->pCont, pMsg->contLen) < 0) {
385 386 387
        goto _err;
      }
      break;
L
Liu Jicong 已提交
388
    case TDMT_VND_TMQ_COMMIT_OFFSET:
389
      if (tqProcessOffsetCommitReq(pVnode->pTq, version, pReq, pMsg->contLen - sizeof(SMsgHead)) < 0) {
390
        goto _err;
L
Liu Jicong 已提交
391 392
      }
      break;
L
Liu Jicong 已提交
393
    case TDMT_VND_TMQ_ADD_CHECKINFO:
394
      if (tqProcessAddCheckInfoReq(pVnode->pTq, version, pReq, len) < 0) {
395 396 397
        goto _err;
      }
      break;
L
Liu Jicong 已提交
398
    case TDMT_VND_TMQ_DEL_CHECKINFO:
399
      if (tqProcessDelCheckInfoReq(pVnode->pTq, version, pReq, len) < 0) {
L
Liu Jicong 已提交
400 401 402
        goto _err;
      }
      break;
403
    case TDMT_STREAM_TASK_DEPLOY: {
404
      if (tqProcessTaskDeployReq(pVnode->pTq, version, pReq, len) < 0) {
405
        goto _err;
H
Hongze Cheng 已提交
406 407
      }
    } break;
L
Liu Jicong 已提交
408
    case TDMT_STREAM_TASK_DROP: {
409
      if (tqProcessTaskDropReq(pVnode->pTq, version, pMsg->pCont, pMsg->contLen) < 0) {
L
Liu Jicong 已提交
410 411 412
        goto _err;
      }
    } break;
L
Liu Jicong 已提交
413
    case TDMT_VND_STREAM_RECOVER_BLOCKING_STAGE: {
L
Liu Jicong 已提交
414 415 416 417
      if (tqProcessTaskRecover2Req(pVnode->pTq, version, pMsg->pCont, pMsg->contLen) < 0) {
        goto _err;
      }
    } break;
418 419 420 421 422
    case TDMT_STREAM_TASK_CHECK_RSP: {
      if (tqProcessStreamTaskCheckRsp(pVnode->pTq, version, pReq, len) < 0) {
        goto _err;
      }
    } break;
423 424 425
    case TDMT_VND_ALTER_CONFIRM:
      vnodeProcessAlterConfirmReq(pVnode, version, pReq, len, pRsp);
      break;
426
    case TDMT_VND_ALTER_CONFIG:
427
      vnodeProcessAlterConfigReq(pVnode, version, pReq, len, pRsp);
S
Shengliang Guan 已提交
428
      break;
H
Hongze Cheng 已提交
429
    case TDMT_VND_COMMIT:
B
Benguang Zhao 已提交
430 431
      needCommit = true;
      break;
dengyihao's avatar
dengyihao 已提交
432 433 434 435 436 437
    case TDMT_VND_CREATE_INDEX:
      vnodeProcessCreateIndexReq(pVnode, version, pReq, len, pRsp);
      break;
    case TDMT_VND_DROP_INDEX:
      vnodeProcessDropIndexReq(pVnode, version, pReq, len, pRsp);
      break;
H
Hongze Cheng 已提交
438
    case TDMT_VND_COMPACT:
439
      vnodeProcessCompactVnodeReq(pVnode, version, pReq, len, pRsp);
H
Hongze Cheng 已提交
440
      goto _exit;
H
Hongze Cheng 已提交
441
    default:
L
Liu Jicong 已提交
442 443
      vError("vgId:%d, unprocessed msg, %d", TD_VID(pVnode), pMsg->msgType);
      return -1;
H
Hongze Cheng 已提交
444 445
  }

S
Shengliang Guan 已提交
446 447
  vTrace("vgId:%d, process %s request, code:0x%x index:%" PRId64, TD_VID(pVnode), TMSG_INFO(pMsg->msgType), pRsp->code,
         version);
H
Hongze Cheng 已提交
448

449 450
  walApplyVer(pVnode->pWal, version);

L
fix log  
Liu Jicong 已提交
451
  /*vInfo("vgId:%d, push msg begin", pVnode->config.vgId);*/
452
  if (tqPushMsg(pVnode->pTq, pMsg->pCont, pMsg->contLen, pMsg->msgType, version) < 0) {
L
fix log  
Liu Jicong 已提交
453
    /*vInfo("vgId:%d, push msg end", pVnode->config.vgId);*/
S
Shengliang Guan 已提交
454
    vError("vgId:%d, failed to push msg to TQ since %s", TD_VID(pVnode), tstrerror(terrno));
455 456
    return -1;
  }
L
fix log  
Liu Jicong 已提交
457
  /*vInfo("vgId:%d, push msg end", pVnode->config.vgId);*/
458

H
Hongze Cheng 已提交
459
  // commit if need
B
Benguang Zhao 已提交
460
  if (needCommit) {
S
Shengliang Guan 已提交
461
    vInfo("vgId:%d, commit at version %" PRId64, TD_VID(pVnode), version);
462 463 464 465
    if (vnodeAsyncCommit(pVnode) < 0) {
      vError("vgId:%d, failed to vnode async commit since %s.", TD_VID(pVnode), tstrerror(terrno));
      goto _err;
    }
H
Hongze Cheng 已提交
466 467

    // start a new one
468
    if (vnodeBegin(pVnode) < 0) {
H
Hongze Cheng 已提交
469 470
      vError("vgId:%d, failed to begin vnode since %s.", TD_VID(pVnode), tstrerror(terrno));
      goto _err;
471
    }
H
Hongze Cheng 已提交
472 473
  }

H
Hongze Cheng 已提交
474
_exit:
H
Hongze Cheng 已提交
475
  return 0;
H
Hongze Cheng 已提交
476 477

_err:
S
Shengliang Guan 已提交
478
  vError("vgId:%d, process %s request failed since %s, version:%" PRId64, TD_VID(pVnode), TMSG_INFO(pMsg->msgType),
H
Hongze Cheng 已提交
479 480
         tstrerror(terrno), version);
  return -1;
H
Hongze Cheng 已提交
481 482
}

483
int32_t vnodePreprocessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg) {
D
dapan1121 已提交
484
  if (TDMT_SCH_QUERY != pMsg->msgType && TDMT_SCH_MERGE_QUERY != pMsg->msgType) {
D
dapan1121 已提交
485 486 487
    return 0;
  }

488
  return qWorkerPreprocessQueryMsg(pVnode->pQuery, pMsg, TDMT_SCH_QUERY == pMsg->msgType);
D
dapan1121 已提交
489 490 491
}

int32_t vnodeProcessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg) {
D
dapan 已提交
492
  vTrace("message in vnode query queue is processing");
493
  if ((pMsg->msgType == TDMT_SCH_QUERY) && !syncIsReadyForRead(pVnode->sync)) {
494
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
495 496 497
    return 0;
  }

498
  SReadHandle handle = {.meta = pVnode->pMeta, .config = &pVnode->config, .vnode = pVnode, .pMsgCb = &pVnode->msgCb};
H
Hongze Cheng 已提交
499
  switch (pMsg->msgType) {
D
dapan1121 已提交
500
    case TDMT_SCH_QUERY:
D
dapan1121 已提交
501
    case TDMT_SCH_MERGE_QUERY:
D
dapan1121 已提交
502
      return qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
D
dapan1121 已提交
503
    case TDMT_SCH_QUERY_CONTINUE:
D
dapan1121 已提交
504
      return qWorkerProcessCQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
H
Hongze Cheng 已提交
505 506
    default:
      vError("unknown msg type:%d in query queue", pMsg->msgType);
507
      return TSDB_CODE_APP_ERROR;
H
Hongze Cheng 已提交
508 509 510
  }
}

511
int32_t vnodeProcessFetchMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
S
Shengliang Guan 已提交
512
  vTrace("vgId:%d, msg:%p in fetch queue is processing", pVnode->config.vgId, pMsg);
L
Liu Jicong 已提交
513 514
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
       pMsg->msgType == TDMT_VND_BATCH_META) &&
515
      !syncIsReadyForRead(pVnode->sync)) {
516
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
517 518 519
    return 0;
  }

L
Liu Jicong 已提交
520
  if (pMsg->msgType == TDMT_VND_TMQ_CONSUME && !pVnode->restored) {
521
    vnodeRedirectRpcMsg(pVnode, pMsg, TSDB_CODE_SYN_RESTORING);
L
Liu Jicong 已提交
522 523 524
    return 0;
  }

H
Hongze Cheng 已提交
525
  switch (pMsg->msgType) {
D
dapan1121 已提交
526
    case TDMT_SCH_FETCH:
D
dapan1121 已提交
527
    case TDMT_SCH_MERGE_FETCH:
D
dapan1121 已提交
528
      return qWorkerProcessFetchMsg(pVnode, pVnode->pQuery, pMsg, 0);
D
dapan1121 已提交
529
    case TDMT_SCH_FETCH_RSP:
D
dapan1121 已提交
530
      return qWorkerProcessRspMsg(pVnode, pVnode->pQuery, pMsg, 0);
L
Liu Jicong 已提交
531 532
    // case TDMT_SCH_CANCEL_TASK:
    //   return qWorkerProcessCancelMsg(pVnode, pVnode->pQuery, pMsg, 0);
D
dapan1121 已提交
533
    case TDMT_SCH_DROP_TASK:
D
dapan1121 已提交
534
      return qWorkerProcessDropMsg(pVnode, pVnode->pQuery, pMsg, 0);
D
dapan1121 已提交
535
    case TDMT_SCH_QUERY_HEARTBEAT:
D
dapan1121 已提交
536
      return qWorkerProcessHbMsg(pVnode, pVnode->pQuery, pMsg, 0);
H
Hongze Cheng 已提交
537
    case TDMT_VND_TABLE_META:
D
dapan1121 已提交
538
      return vnodeGetTableMeta(pVnode, pMsg, true);
D
dapan1121 已提交
539
    case TDMT_VND_TABLE_CFG:
D
dapan1121 已提交
540 541 542
      return vnodeGetTableCfg(pVnode, pMsg, true);
    case TDMT_VND_BATCH_META:
      return vnodeGetBatchMeta(pVnode, pMsg);
L
Liu Jicong 已提交
543
    case TDMT_VND_TMQ_CONSUME:
L
Liu Jicong 已提交
544
      return tqProcessPollReq(pVnode->pTq, pMsg);
545 546
    case TDMT_STREAM_TASK_RUN:
      return tqProcessTaskRunReq(pVnode->pTq, pMsg);
547
#if 1
548
    case TDMT_STREAM_TASK_DISPATCH:
L
Liu Jicong 已提交
549
      return tqProcessTaskDispatchReq(pVnode->pTq, pMsg, true);
L
Liu Jicong 已提交
550
#endif
551 552
    case TDMT_STREAM_TASK_CHECK:
      return tqProcessStreamTaskCheckReq(pVnode->pTq, pMsg);
553
    case TDMT_STREAM_TASK_DISPATCH_RSP:
L
Liu Jicong 已提交
554
      return tqProcessTaskDispatchRsp(pVnode->pTq, pMsg);
L
Liu Jicong 已提交
555 556
    case TDMT_STREAM_RETRIEVE:
      return tqProcessTaskRetrieveReq(pVnode->pTq, pMsg);
L
Liu Jicong 已提交
557 558
    case TDMT_STREAM_RETRIEVE_RSP:
      return tqProcessTaskRetrieveRsp(pVnode->pTq, pMsg);
L
Liu Jicong 已提交
559
    case TDMT_VND_STREAM_RECOVER_NONBLOCKING_STAGE:
L
Liu Jicong 已提交
560 561 562 563 564
      return tqProcessTaskRecover1Req(pVnode->pTq, pMsg);
    case TDMT_STREAM_RECOVER_FINISH:
      return tqProcessTaskRecoverFinishReq(pVnode->pTq, pMsg);
    case TDMT_STREAM_RECOVER_FINISH_RSP:
      return tqProcessTaskRecoverFinishRsp(pVnode->pTq, pMsg);
H
Hongze Cheng 已提交
565 566
    default:
      vError("unknown msg type:%d in fetch queue", pMsg->msgType);
567
      return TSDB_CODE_APP_ERROR;
H
Hongze Cheng 已提交
568 569 570 571 572 573
  }
}

// TODO: remove the function
void smaHandleRes(void *pVnode, int64_t smaId, const SArray *data) {
  // TODO
C
Cary Xu 已提交
574
  // blockDebugShowDataBlocks(data, __func__);
575
  tdProcessTSmaInsert(((SVnode *)pVnode)->pSma, smaId, (const char *)data);
H
Hongze Cheng 已提交
576 577
}

D
dapan1121 已提交
578
void vnodeUpdateMetaRsp(SVnode *pVnode, STableMetaRsp *pMetaRsp) {
579 580 581
  if (NULL == pMetaRsp) {
    return;
  }
L
Liu Jicong 已提交
582

D
dapan1121 已提交
583 584 585 586 587 588
  strcpy(pMetaRsp->dbFName, pVnode->config.dbname);
  pMetaRsp->dbId = pVnode->config.dbId;
  pMetaRsp->vgId = TD_VID(pVnode);
  pMetaRsp->precision = pVnode->config.tsdbCfg.precision;
}

S
Shengliang Guan 已提交
589
static int32_t vnodeProcessTrimReq(SVnode *pVnode, int64_t version, void *pReq, int32_t len, SRpcMsg *pRsp) {
H
Hongze Cheng 已提交
590
  int32_t     code = 0;
S
Shengliang Guan 已提交
591 592
  SVTrimDbReq trimReq = {0};

H
Hongze Cheng 已提交
593 594 595 596
  // decode
  if (tDeserializeSVTrimDbReq(pReq, len, &trimReq) != 0) {
    code = TSDB_CODE_INVALID_MSG;
    goto _exit;
S
Shengliang Guan 已提交
597 598
  }

C
Cary Xu 已提交
599 600
  vInfo("vgId:%d, trim vnode request will be processed, time:%d", pVnode->config.vgId, trimReq.timestamp);

H
Hongze Cheng 已提交
601 602 603 604
  // process
  code = tsdbDoRetention(pVnode->pTsdb, trimReq.timestamp);
  if (code) goto _exit;

C
Cary Xu 已提交
605 606 607
  code = smaDoRetention(pVnode->pSma, trimReq.timestamp);
  if (code) goto _exit;

H
Hongze Cheng 已提交
608 609
_exit:
  return code;
S
Shengliang Guan 已提交
610 611
}

L
Liu Jicong 已提交
612
static int32_t vnodeProcessDropTtlTbReq(SVnode *pVnode, int64_t version, void *pReq, int32_t len, SRpcMsg *pRsp) {
613 614 615
  SArray *tbUids = taosArrayInit(8, sizeof(int64_t));
  if (tbUids == NULL) return TSDB_CODE_OUT_OF_MEMORY;

S
Shengliang Guan 已提交
616 617 618 619 620 621
  SVDropTtlTableReq ttlReq = {0};
  if (tDeserializeSVDropTtlTableReq(pReq, len, &ttlReq) != 0) {
    terrno = TSDB_CODE_INVALID_MSG;
    goto end;
  }

622
  vDebug("vgId:%d, drop ttl table req will be processed, time:%d", pVnode->config.vgId, ttlReq.timestamp);
S
Shengliang Guan 已提交
623
  int32_t ret = metaTtlDropTable(pVnode->pMeta, ttlReq.timestamp, tbUids);
L
Liu Jicong 已提交
624
  if (ret != 0) {
625 626
    goto end;
  }
L
Liu Jicong 已提交
627
  if (taosArrayGetSize(tbUids) > 0) {
wmmhello's avatar
wmmhello 已提交
628 629
    tqUpdateTbUidList(pVnode->pTq, tbUids, false);
  }
630

H
Hongze Cheng 已提交
631
#if 0
H
Hongze Cheng 已提交
632 633 634 635 636 637
  // process
  ret = tsdbDoRetention(pVnode->pTsdb, ttlReq.timestamp);
  if (ret) goto end;

  ret = smaDoRetention(pVnode->pSma, ttlReq.timestamp);
  if (ret) goto end;
H
Hongze Cheng 已提交
638
#endif
H
Hongze Cheng 已提交
639

640 641 642 643 644
end:
  taosArrayDestroy(tbUids);
  return ret;
}

645
static int32_t vnodeProcessCreateStbReq(SVnode *pVnode, int64_t version, void *pReq, int32_t len, SRpcMsg *pRsp) {
H
Hongze Cheng 已提交
646
  SVCreateStbReq req = {0};
H
Hongze Cheng 已提交
647
  SDecoder       coder;
H
Hongze Cheng 已提交
648

H
Hongze Cheng 已提交
649 650 651 652 653 654
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
  pRsp->code = TSDB_CODE_SUCCESS;
  pRsp->pCont = NULL;
  pRsp->contLen = 0;

  // decode and process req
H
Hongze Cheng 已提交
655
  tDecoderInit(&coder, pReq, len);
H
Hongze Cheng 已提交
656 657

  if (tDecodeSVCreateStbReq(&coder, &req) < 0) {
H
Hongze Cheng 已提交
658 659
    pRsp->code = terrno;
    goto _err;
H
Hongze Cheng 已提交
660 661
  }

H
Hongze Cheng 已提交
662
  if (metaCreateSTable(pVnode->pMeta, version, &req) < 0) {
H
Hongze Cheng 已提交
663 664
    pRsp->code = terrno;
    goto _err;
H
Hongze Cheng 已提交
665 666
  }

667
  if (tdProcessRSmaCreate(pVnode->pSma, &req) < 0) {
668 669 670
    pRsp->code = terrno;
    goto _err;
  }
C
Cary Xu 已提交
671

H
Hongze Cheng 已提交
672
  tDecoderClear(&coder);
H
Hongze Cheng 已提交
673
  return 0;
H
Hongze Cheng 已提交
674 675

_err:
H
Hongze Cheng 已提交
676
  tDecoderClear(&coder);
H
Hongze Cheng 已提交
677
  return -1;
H
Hongze Cheng 已提交
678 679
}

680
static int32_t vnodeProcessCreateTbReq(SVnode *pVnode, int64_t version, void *pReq, int32_t len, SRpcMsg *pRsp) {
H
Hongze Cheng 已提交
681
  SDecoder           decoder = {0};
682
  SEncoder           encoder = {0};
683
  int32_t            rcode = 0;
H
Hongze Cheng 已提交
684 685
  SVCreateTbBatchReq req = {0};
  SVCreateTbReq     *pCreateReq;
H
Hongze Cheng 已提交
686 687 688
  SVCreateTbBatchRsp rsp = {0};
  SVCreateTbRsp      cRsp = {0};
  char               tbName[TSDB_TABLE_FNAME_LEN];
C
Cary Xu 已提交
689
  STbUidStore       *pStore = NULL;
690
  SArray            *tbUids = NULL;
H
Hongze Cheng 已提交
691 692

  pRsp->msgType = TDMT_VND_CREATE_TABLE_RSP;
H
Hongze Cheng 已提交
693 694 695
  pRsp->code = TSDB_CODE_SUCCESS;
  pRsp->pCont = NULL;
  pRsp->contLen = 0;
H
Hongze Cheng 已提交
696

H
Hongze Cheng 已提交
697
  // decode
H
Hongze Cheng 已提交
698 699
  tDecoderInit(&decoder, pReq, len);
  if (tDecodeSVCreateTbBatchReq(&decoder, &req) < 0) {
H
Hongze Cheng 已提交
700 701 702 703
    rcode = -1;
    terrno = TSDB_CODE_INVALID_MSG;
    goto _exit;
  }
H
Hongze Cheng 已提交
704

H
Hongze Cheng 已提交
705
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(cRsp));
706 707
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
  if (rsp.pArray == NULL || tbUids == NULL) {
H
Hongze Cheng 已提交
708 709 710 711 712
    rcode = -1;
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
  }

H
Hongze Cheng 已提交
713
  // loop to create table
714
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
H
Hongze Cheng 已提交
715
    pCreateReq = req.pReqs + iReq;
D
dapan1121 已提交
716
    memset(&cRsp, 0, sizeof(cRsp));
H
Hongze Cheng 已提交
717

C
Cary Xu 已提交
718 719 720 721
    if ((terrno = grantCheck(TSDB_GRANT_TIMESERIES)) < 0) {
      rcode = -1;
      goto _exit;
    }
L
Liu Jicong 已提交
722

wafwerar's avatar
wafwerar 已提交
723 724 725 726 727
    if ((terrno = grantCheck(TSDB_GRANT_TABLE)) < 0) {
      rcode = -1;
      goto _exit;
    }

H
Hongze Cheng 已提交
728 729 730 731 732 733 734 735 736
    // validate hash
    sprintf(tbName, "%s.%s", pVnode->config.dbname, pCreateReq->name);
    if (vnodeValidateTableHash(pVnode, tbName) < 0) {
      cRsp.code = TSDB_CODE_VND_HASH_MISMATCH;
      taosArrayPush(rsp.pArray, &cRsp);
      continue;
    }

    // do create table
737
    if (metaCreateTable(pVnode->pMeta, version, pCreateReq, &cRsp.pMeta) < 0) {
H
Hongze Cheng 已提交
738 739 740 741 742
      if (pCreateReq->flags & TD_CREATE_IF_NOT_EXISTS && terrno == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
        cRsp.code = TSDB_CODE_SUCCESS;
      } else {
        cRsp.code = terrno;
      }
H
Hongze Cheng 已提交
743
    } else {
H
Hongze Cheng 已提交
744
      cRsp.code = TSDB_CODE_SUCCESS;
745
      tdFetchTbUidList(pVnode->pSma, &pStore, pCreateReq->ctb.suid, pCreateReq->uid);
746
      taosArrayPush(tbUids, &pCreateReq->uid);
L
Liu Jicong 已提交
747
      vnodeUpdateMetaRsp(pVnode, cRsp.pMeta);
H
Hongze Cheng 已提交
748
    }
H
Hongze Cheng 已提交
749 750

    taosArrayPush(rsp.pArray, &cRsp);
H
Hongze Cheng 已提交
751 752
  }

H
Haojun Liao 已提交
753
  vDebug("vgId:%d, add %d new created tables into query table list", TD_VID(pVnode), (int32_t)taosArrayGetSize(tbUids));
754
  tqUpdateTbUidList(pVnode->pTq, tbUids, true);
755
  if (tdUpdateTbUidList(pVnode->pSma, pStore, true) < 0) {
C
Cary Xu 已提交
756 757
    goto _exit;
  }
758
  tdUidStoreFree(pStore);
C
Cary Xu 已提交
759

H
Hongze Cheng 已提交
760
  // prepare rsp
761
  int32_t ret = 0;
wafwerar's avatar
wafwerar 已提交
762
  tEncodeSize(tEncodeSVCreateTbBatchRsp, &rsp, pRsp->contLen, ret);
H
Hongze Cheng 已提交
763 764 765 766 767 768
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
  if (pRsp->pCont == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    rcode = -1;
    goto _exit;
  }
H
Hongze Cheng 已提交
769 770
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
  tEncodeSVCreateTbBatchRsp(&encoder, &rsp);
H
Hongze Cheng 已提交
771

H
Hongze Cheng 已提交
772
_exit:
wmmhello's avatar
wmmhello 已提交
773 774
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
    pCreateReq = req.pReqs + iReq;
775
    taosMemoryFree(pCreateReq->comment);
wmmhello's avatar
wmmhello 已提交
776 777
    taosArrayDestroy(pCreateReq->ctb.tagName);
  }
778
  taosArrayDestroyEx(rsp.pArray, tFreeSVCreateTbRsp);
779
  taosArrayDestroy(tbUids);
H
Hongze Cheng 已提交
780 781
  tDecoderClear(&decoder);
  tEncoderClear(&encoder);
H
Hongze Cheng 已提交
782
  return rcode;
H
Hongze Cheng 已提交
783 784
}

785
static int32_t vnodeProcessAlterStbReq(SVnode *pVnode, int64_t version, void *pReq, int32_t len, SRpcMsg *pRsp) {
H
Hongze Cheng 已提交
786 787 788 789 790 791 792 793 794 795 796 797 798 799 800
  SVCreateStbReq req = {0};
  SDecoder       dc = {0};

  pRsp->msgType = TDMT_VND_ALTER_STB_RSP;
  pRsp->code = TSDB_CODE_SUCCESS;
  pRsp->pCont = NULL;
  pRsp->contLen = 0;

  tDecoderInit(&dc, pReq, len);

  // decode req
  if (tDecodeSVCreateStbReq(&dc, &req) < 0) {
    terrno = TSDB_CODE_INVALID_MSG;
    tDecoderClear(&dc);
    return -1;
H
Hongze Cheng 已提交
801
  }
H
Hongze Cheng 已提交
802 803 804 805 806 807 808 809 810

  if (metaAlterSTable(pVnode->pMeta, version, &req) < 0) {
    pRsp->code = terrno;
    tDecoderClear(&dc);
    return -1;
  }

  tDecoderClear(&dc);

H
Hongze Cheng 已提交
811 812 813
  return 0;
}

814
static int32_t vnodeProcessDropStbReq(SVnode *pVnode, int64_t version, void *pReq, int32_t len, SRpcMsg *pRsp) {
H
Hongze Cheng 已提交
815
  SVDropStbReq req = {0};
816
  int32_t      rcode = TSDB_CODE_SUCCESS;
H
Hongze Cheng 已提交
817
  SDecoder     decoder = {0};
818
  SArray      *tbUidList = NULL;
H
Hongze Cheng 已提交
819 820 821 822 823 824

  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
  pRsp->pCont = NULL;
  pRsp->contLen = 0;

  // decode request
H
Hongze Cheng 已提交
825 826
  tDecoderInit(&decoder, pReq, len);
  if (tDecodeSVDropStbReq(&decoder, &req) < 0) {
H
Hongze Cheng 已提交
827 828 829 830 831
    rcode = TSDB_CODE_INVALID_MSG;
    goto _exit;
  }

  // process request
832 833 834 835 836 837 838 839
  tbUidList = taosArrayInit(8, sizeof(int64_t));
  if (tbUidList == NULL) goto _exit;
  if (metaDropSTable(pVnode->pMeta, version, &req, tbUidList) < 0) {
    rcode = terrno;
    goto _exit;
  }

  if (tqUpdateTbUidList(pVnode->pTq, tbUidList, false) < 0) {
840 841 842
    rcode = terrno;
    goto _exit;
  }
H
Hongze Cheng 已提交
843

844 845 846 847 848
  if (tdProcessRSmaDrop(pVnode->pSma, &req) < 0) {
    rcode = terrno;
    goto _exit;
  }

H
Hongze Cheng 已提交
849 850
  // return rsp
_exit:
851
  if (tbUidList) taosArrayDestroy(tbUidList);
H
Hongze Cheng 已提交
852
  pRsp->code = rcode;
H
Hongze Cheng 已提交
853
  tDecoderClear(&decoder);
H
Hongze Cheng 已提交
854 855 856
  return 0;
}

857
static int32_t vnodeProcessAlterTbReq(SVnode *pVnode, int64_t version, void *pReq, int32_t len, SRpcMsg *pRsp) {
D
dapan1121 已提交
858 859 860
  SVAlterTbReq  vAlterTbReq = {0};
  SVAlterTbRsp  vAlterTbRsp = {0};
  SDecoder      dc = {0};
861 862
  int32_t       rcode = 0;
  int32_t       ret;
D
dapan1121 已提交
863 864
  SEncoder      ec = {0};
  STableMetaRsp vMetaRsp = {0};
H
Hongze Cheng 已提交
865 866 867 868 869 870 871 872 873 874

  pRsp->msgType = TDMT_VND_ALTER_TABLE_RSP;
  pRsp->pCont = NULL;
  pRsp->contLen = 0;
  pRsp->code = TSDB_CODE_SUCCESS;

  tDecoderInit(&dc, pReq, len);

  // decode
  if (tDecodeSVAlterTbReq(&dc, &vAlterTbReq) < 0) {
H
Hongze Cheng 已提交
875
    vAlterTbRsp.code = TSDB_CODE_INVALID_MSG;
H
Hongze Cheng 已提交
876
    tDecoderClear(&dc);
H
Hongze Cheng 已提交
877 878
    rcode = -1;
    goto _exit;
H
Hongze Cheng 已提交
879 880 881
  }

  // process
D
dapan1121 已提交
882
  if (metaAlterTable(pVnode->pMeta, version, &vAlterTbReq, &vMetaRsp) < 0) {
wmmhello's avatar
wmmhello 已提交
883
    vAlterTbRsp.code = terrno;
H
Hongze Cheng 已提交
884
    tDecoderClear(&dc);
H
Hongze Cheng 已提交
885 886
    rcode = -1;
    goto _exit;
H
Hongze Cheng 已提交
887 888
  }
  tDecoderClear(&dc);
H
Hongze Cheng 已提交
889

D
dapan1121 已提交
890 891 892 893 894
  if (NULL != vMetaRsp.pSchemas) {
    vnodeUpdateMetaRsp(pVnode, &vMetaRsp);
    vAlterTbRsp.pMeta = &vMetaRsp;
  }

H
Hongze Cheng 已提交
895 896 897 898 899 900
_exit:
  tEncodeSize(tEncodeSVAlterTbRsp, &vAlterTbRsp, pRsp->contLen, ret);
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
  tEncodeSVAlterTbRsp(&ec, &vAlterTbRsp);
  tEncoderClear(&ec);
M
Minglei Jin 已提交
901 902 903
  if (vMetaRsp.pSchemas) {
    taosMemoryFree(vMetaRsp.pSchemas);
  }
H
Hongze Cheng 已提交
904 905 906
  return 0;
}

907
static int32_t vnodeProcessDropTbReq(SVnode *pVnode, int64_t version, void *pReq, int32_t len, SRpcMsg *pRsp) {
H
Hongze Cheng 已提交
908 909
  SVDropTbBatchReq req = {0};
  SVDropTbBatchRsp rsp = {0};
H
Hongze Cheng 已提交
910
  SDecoder         decoder = {0};
H
Hongze Cheng 已提交
911
  SEncoder         encoder = {0};
912
  int32_t          ret;
913
  SArray          *tbUids = NULL;
914
  STbUidStore     *pStore = NULL;
H
Hongze Cheng 已提交
915

H
Hongze Cheng 已提交
916
  pRsp->msgType = TDMT_VND_DROP_TABLE_RSP;
H
Hongze Cheng 已提交
917 918 919
  pRsp->pCont = NULL;
  pRsp->contLen = 0;
  pRsp->code = TSDB_CODE_SUCCESS;
H
Hongze Cheng 已提交
920 921

  // decode req
H
Hongze Cheng 已提交
922 923
  tDecoderInit(&decoder, pReq, len);
  ret = tDecodeSVDropTbBatchReq(&decoder, &req);
H
Hongze Cheng 已提交
924 925 926 927 928
  if (ret < 0) {
    terrno = TSDB_CODE_INVALID_MSG;
    pRsp->code = terrno;
    goto _exit;
  }
H
Hongze Cheng 已提交
929 930

  // process req
931
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
H
Hongze Cheng 已提交
932
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(SVDropTbRsp));
933 934
  if (tbUids == NULL || rsp.pArray == NULL) goto _exit;

935
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
H
Hongze Cheng 已提交
936 937
    SVDropTbReq *pDropTbReq = req.pReqs + iReq;
    SVDropTbRsp  dropTbRsp = {0};
938
    tb_uid_t     tbUid = 0;
H
Hongze Cheng 已提交
939

H
Hongze Cheng 已提交
940
    /* code */
941
    ret = metaDropTable(pVnode->pMeta, version, pDropTbReq, tbUids, &tbUid);
H
Hongze Cheng 已提交
942
    if (ret < 0) {
943
      if (pDropTbReq->igNotExists && terrno == TSDB_CODE_TDB_TABLE_NOT_EXIST) {
H
Hongze Cheng 已提交
944 945 946 947
        dropTbRsp.code = TSDB_CODE_SUCCESS;
      } else {
        dropTbRsp.code = terrno;
      }
H
Hongze Cheng 已提交
948
    } else {
H
Hongze Cheng 已提交
949
      dropTbRsp.code = TSDB_CODE_SUCCESS;
950
      if (tbUid > 0) tdFetchTbUidList(pVnode->pSma, &pStore, pDropTbReq->suid, tbUid);
H
Hongze Cheng 已提交
951 952 953 954 955
    }

    taosArrayPush(rsp.pArray, &dropTbRsp);
  }

956
  tqUpdateTbUidList(pVnode->pTq, tbUids, false);
957
  tdUpdateTbUidList(pVnode->pSma, pStore, false);
958

H
Hongze Cheng 已提交
959
_exit:
960
  taosArrayDestroy(tbUids);
961
  tdUidStoreFree(pStore);
H
Hongze Cheng 已提交
962
  tDecoderClear(&decoder);
H
Hongze Cheng 已提交
963 964 965 966 967
  tEncodeSize(tEncodeSVDropTbBatchRsp, &rsp, pRsp->contLen, ret);
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
  tEncodeSVDropTbBatchRsp(&encoder, &rsp);
  tEncoderClear(&encoder);
968
  taosArrayDestroy(rsp.pArray);
H
Hongze Cheng 已提交
969 970 971
  return 0;
}

972 973
static int32_t vnodeDebugPrintSingleSubmitMsg(SMeta *pMeta, SSubmitBlk *pBlock, SSubmitMsgIter *msgIter,
                                              const char *tags) {
D
dapan 已提交
974 975 976 977
  SSubmitBlkIter blkIter = {0};
  STSchema      *pSchema = NULL;
  tb_uid_t       suid = 0;
  STSRow        *row = NULL;
C
Cary Xu 已提交
978
  int32_t        rv = -1;
D
dapan 已提交
979 980 981

  tInitSubmitBlkIter(msgIter, pBlock, &blkIter);
  if (blkIter.row == NULL) return 0;
982 983 984 985 986

  pSchema = metaGetTbTSchema(pMeta, msgIter->suid, TD_ROW_SVER(blkIter.row), 1);  // TODO: use the real schema
  if (pSchema) {
    suid = msgIter->suid;
    rv = TD_ROW_SVER(blkIter.row);
D
dapan 已提交
987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002
  }
  if (!pSchema) {
    printf("%s:%d no valid schema\n", tags, __LINE__);
    return -1;
  }
  char __tags[128] = {0};
  snprintf(__tags, 128, "%s: uid %" PRIi64 " ", tags, msgIter->uid);
  while ((row = tGetSubmitBlkNext(&blkIter))) {
    tdSRowPrint(row, pSchema, __tags);
  }

  taosMemoryFreeClear(pSchema);

  return TSDB_CODE_SUCCESS;
}

1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
typedef struct SSubmitReqConvertCxt {
  SSubmitMsgIter msgIter;
  SSubmitBlk    *pBlock;
  SSubmitBlkIter blkIter;
  STSRow        *pRow;
  STSRowIter     rowIter;
  SSubmitTbData *pTbData;
  STSchema      *pTbSchema;
  SArray        *pColValues;
} SSubmitReqConvertCxt;

static int32_t vnodeResetTableCxt(SMeta *pMeta, SSubmitReqConvertCxt *pCxt) {
  taosMemoryFreeClear(pCxt->pTbSchema);
1016 1017
  pCxt->pTbSchema = metaGetTbTSchema(pMeta, pCxt->msgIter.suid > 0 ? pCxt->msgIter.suid : pCxt->msgIter.uid,
                                     pCxt->msgIter.sversion, 1);
1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183
  if (NULL == pCxt->pTbSchema) {
    return TSDB_CODE_INVALID_MSG;
  }
  tdSTSRowIterInit(&pCxt->rowIter, pCxt->pTbSchema);

  tDestroySSubmitTbData(pCxt->pTbData, TSDB_MSG_FLG_ENCODE);
  if (NULL == pCxt->pTbData) {
    pCxt->pTbData = taosMemoryCalloc(1, sizeof(SSubmitTbData));
    if (NULL == pCxt->pTbData) {
      return TSDB_CODE_OUT_OF_MEMORY;
    }
  }
  pCxt->pTbData->flags = 0;
  pCxt->pTbData->suid = pCxt->msgIter.suid;
  pCxt->pTbData->uid = pCxt->msgIter.uid;
  pCxt->pTbData->sver = pCxt->msgIter.sversion;
  pCxt->pTbData->pCreateTbReq = NULL;
  pCxt->pTbData->aRowP = taosArrayInit(128, POINTER_BYTES);
  if (NULL == pCxt->pTbData->aRowP) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

  taosArrayDestroy(pCxt->pColValues);
  pCxt->pColValues = taosArrayInit(pCxt->pTbSchema->numOfCols, sizeof(SColVal));
  if (NULL == pCxt->pColValues) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }
  for (int32_t i = 0; i < pCxt->pTbSchema->numOfCols; ++i) {
    SColVal val = COL_VAL_NONE(pCxt->pTbSchema->columns[i].colId, pCxt->pTbSchema->columns[i].type);
    taosArrayPush(pCxt->pColValues, &val);
  }

  return TSDB_CODE_SUCCESS;
}

static void vnodeDestroySubmitReqConvertCxt(SSubmitReqConvertCxt *pCxt) {
  taosMemoryFreeClear(pCxt->pTbSchema);
  tDestroySSubmitTbData(pCxt->pTbData, TSDB_MSG_FLG_ENCODE);
  taosMemoryFreeClear(pCxt->pTbData);
  taosArrayDestroy(pCxt->pColValues);
}

static int32_t vnodeCellValConvertToColVal(STColumn *pCol, SCellVal *pCellVal, SColVal *pColVal) {
  if (tdValTypeIsNone(pCellVal->valType)) {
    pColVal->flag = CV_FLAG_NONE;
    return TSDB_CODE_SUCCESS;
  }

  if (tdValTypeIsNull(pCellVal->valType)) {
    pColVal->flag = CV_FLAG_NULL;
    return TSDB_CODE_SUCCESS;
  }

  if (IS_VAR_DATA_TYPE(pCol->type)) {
    pColVal->value.nData = varDataLen(pCellVal->val);
    pColVal->value.pData = varDataVal(pCellVal->val);
  } else if (TSDB_DATA_TYPE_FLOAT == pCol->type) {
    float f = GET_FLOAT_VAL(pCellVal->val);
    memcpy(&pColVal->value.val, &f, sizeof(f));
  } else if (TSDB_DATA_TYPE_DOUBLE == pCol->type) {
    pColVal->value.val = *(int64_t *)pCellVal->val;
  } else {
    GET_TYPED_DATA(pColVal->value.val, int64_t, pCol->type, pCellVal->val);
  }

  pColVal->flag = CV_FLAG_VALUE;
  return TSDB_CODE_SUCCESS;
}

static int32_t vnodeTSRowConvertToColValArray(SSubmitReqConvertCxt *pCxt) {
  int32_t code = TSDB_CODE_SUCCESS;
  tdSTSRowIterReset(&pCxt->rowIter, pCxt->pRow);
  for (int32_t i = 0; TSDB_CODE_SUCCESS == code && i < pCxt->pTbSchema->numOfCols; ++i) {
    STColumn *pCol = pCxt->pTbSchema->columns + i;
    SCellVal  cellVal = {0};
    if (!tdSTSRowIterFetch(&pCxt->rowIter, pCol->colId, pCol->type, &cellVal)) {
      break;
    }
    code = vnodeCellValConvertToColVal(pCol, &cellVal, (SColVal *)taosArrayGet(pCxt->pColValues, i));
  }
  return code;
}

static int32_t vnodeDecodeCreateTbReq(SSubmitReqConvertCxt *pCxt) {
  if (pCxt->msgIter.schemaLen <= 0) {
    return TSDB_CODE_SUCCESS;
  }

  pCxt->pTbData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
  if (NULL == pCxt->pTbData->pCreateTbReq) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

  SDecoder decoder = {0};
  tDecoderInit(&decoder, pCxt->pBlock->data, pCxt->msgIter.schemaLen);
  int32_t code = tDecodeSVCreateTbReq(&decoder, pCxt->pTbData->pCreateTbReq);
  tDecoderClear(&decoder);

  return code;
}

static int32_t vnodeSubmitReqConvertToSubmitReq2(SVnode *pVnode, SSubmitReq *pReq, SSubmitReq2 *pReq2) {
  pReq2->aSubmitTbData = taosArrayInit(128, sizeof(SSubmitTbData));
  if (NULL == pReq2->aSubmitTbData) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

  SSubmitReqConvertCxt cxt = {0};

  int32_t code = tInitSubmitMsgIter(pReq, &cxt.msgIter);
  while (TSDB_CODE_SUCCESS == code) {
    code = tGetSubmitMsgNext(&cxt.msgIter, &cxt.pBlock);
    if (TSDB_CODE_SUCCESS == code) {
      if (NULL == cxt.pBlock) {
        break;
      }
      code = vnodeResetTableCxt(pVnode->pMeta, &cxt);
    }
    if (TSDB_CODE_SUCCESS == code) {
      code = tInitSubmitBlkIter(&cxt.msgIter, cxt.pBlock, &cxt.blkIter);
    }
    if (TSDB_CODE_SUCCESS == code) {
      code = vnodeDecodeCreateTbReq(&cxt);
    }
    while (TSDB_CODE_SUCCESS == code && (cxt.pRow = tGetSubmitBlkNext(&cxt.blkIter)) != NULL) {
      code = vnodeTSRowConvertToColValArray(&cxt);
      if (TSDB_CODE_SUCCESS == code) {
        SRow **pNewRow = taosArrayReserve(cxt.pTbData->aRowP, 1);
        code = tRowBuild(cxt.pColValues, cxt.pTbSchema, pNewRow);
      }
    }
    if (TSDB_CODE_SUCCESS == code) {
      code = (NULL == taosArrayPush(pReq2->aSubmitTbData, cxt.pTbData) ? TSDB_CODE_OUT_OF_MEMORY : TSDB_CODE_SUCCESS);
    }
    if (TSDB_CODE_SUCCESS == code) {
      taosMemoryFreeClear(cxt.pTbData);
    }
  }

  vnodeDestroySubmitReqConvertCxt(&cxt);
  return code;
}

static int32_t vnodeRebuildSubmitReqMsg(SSubmitReq2 *pSubmitReq, void **ppMsg) {
  int32_t  code = TSDB_CODE_SUCCESS;
  char    *pMsg = NULL;
  uint32_t msglen = 0;
  tEncodeSize(tEncodeSSubmitReq2, pSubmitReq, msglen, code);
  if (TSDB_CODE_SUCCESS == code) {
    pMsg = taosMemoryMalloc(msglen);
    if (NULL == pMsg) {
      code = TSDB_CODE_OUT_OF_MEMORY;
    }
  }
  if (TSDB_CODE_SUCCESS == code) {
    SEncoder encoder;
    tEncoderInit(&encoder, pMsg, msglen);
    code = tEncodeSSubmitReq2(&encoder, pSubmitReq);
    tEncoderClear(&encoder);
  }
  if (TSDB_CODE_SUCCESS == code) {
    *ppMsg = pMsg;
  }
  return code;
}

1184
static int32_t vnodeProcessSubmitReq(SVnode *pVnode, int64_t version, void *pReq, int32_t len, SRpcMsg *pRsp) {
H
Hongze Cheng 已提交
1185
  int32_t code = 0;
D
dapan1121 已提交
1186
  terrno = 0;
C
Cary Xu 已提交
1187

H
Hongze Cheng 已提交
1188
  SSubmitReq2 *pSubmitReq = &(SSubmitReq2){0};
H
Hongze Cheng 已提交
1189
  SSubmitRsp2 *pSubmitRsp = &(SSubmitRsp2){0};
H
Hongze Cheng 已提交
1190
  SArray      *newTbUids = NULL;
H
Hongze Cheng 已提交
1191 1192 1193 1194
  int32_t      ret;
  SEncoder     ec = {0};

  pRsp->code = TSDB_CODE_SUCCESS;
H
Hongze Cheng 已提交
1195

1196
  void           *pAllocMsg = NULL;
1197 1198 1199 1200 1201 1202
  SSubmitReq2Msg *pMsg = (SSubmitReq2Msg *)pReq;
  if (0 == pMsg->version) {
    code = vnodeSubmitReqConvertToSubmitReq2(pVnode, (SSubmitReq *)pMsg, pSubmitReq);
    if (TSDB_CODE_SUCCESS == code) {
      code = vnodeRebuildSubmitReqMsg(pSubmitReq, &pReq);
    }
1203 1204 1205
    if (TSDB_CODE_SUCCESS == code) {
      pAllocMsg = pReq;
    }
1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219
    if (TSDB_CODE_SUCCESS != code) {
      goto _exit;
    }
  } else {
    // decode
    pReq = POINTER_SHIFT(pReq, sizeof(SSubmitReq2Msg));
    len -= sizeof(SSubmitReq2Msg);
    SDecoder dc = {0};
    tDecoderInit(&dc, pReq, len);
    if (tDecodeSSubmitReq2(&dc, pSubmitReq) < 0) {
      code = TSDB_CODE_INVALID_MSG;
      goto _exit;
    }
    tDecoderClear(&dc);
D
dapan 已提交
1220
  }
C
Cary Xu 已提交
1221

H
Hongze Cheng 已提交
1222
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
H
Hongze Cheng 已提交
1223 1224 1225 1226 1227 1228 1229 1230 1231 1232
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);

    if (pSubmitTbData->pCreateTbReq) {
      pSubmitTbData->uid = pSubmitTbData->pCreateTbReq->uid;
    } else {
      SMetaInfo info = {0};

      code = metaGetInfo(pVnode->pMeta, pSubmitTbData->uid, &info, NULL);
      if (code) {
        code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
D
dapan1121 已提交
1233
        vWarn("vgId:%d, table uid:%" PRId64 " not exists", TD_VID(pVnode), pSubmitTbData->uid);
H
Hongze Cheng 已提交
1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250
        goto _exit;
      }

      if (info.suid != pSubmitTbData->suid) {
        code = TSDB_CODE_INVALID_MSG;
        goto _exit;
      }

      if (info.suid) {
        metaGetInfo(pVnode->pMeta, info.suid, &info, NULL);
      }

      if (pSubmitTbData->sver != info.skmVer) {
        code = TSDB_CODE_TDB_INVALID_TABLE_SCHEMA_VER;
        goto _exit;
      }
    }
H
Hongze Cheng 已提交
1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273

    if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
      int32_t   nColData = TARRAY_SIZE(pSubmitTbData->aCol);
      SColData *aColData = (SColData *)TARRAY_DATA(pSubmitTbData->aCol);

      if (nColData <= 0) {
        code = TSDB_CODE_INVALID_MSG;
        goto _exit;
      }

      if (aColData[0].cid != PRIMARYKEY_TIMESTAMP_COL_ID || aColData[0].type != TSDB_DATA_TYPE_TIMESTAMP ||
          aColData[0].nVal <= 0) {
        code = TSDB_CODE_INVALID_MSG;
        goto _exit;
      }

      for (int32_t i = 1; i < nColData; i++) {
        if (aColData[i].nVal != aColData[0].nVal) {
          code = TSDB_CODE_INVALID_MSG;
          goto _exit;
        }
      }
    }
H
Hongze Cheng 已提交
1274 1275
  }

L
Liu Jicong 已提交
1276 1277
  vDebug("vgId:%d, submit block size %d", TD_VID(pVnode), (int32_t)taosArrayGetSize(pSubmitReq->aSubmitTbData));

H
Hongze Cheng 已提交
1278
  // loop to handle
H
Hongze Cheng 已提交
1279
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
H
Hongze Cheng 已提交
1280 1281 1282 1283
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);

    // create table
    if (pSubmitTbData->pCreateTbReq) {
H
Hongze Cheng 已提交
1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294
      // check (TODO: move check to create table)
      code = grantCheck(TSDB_GRANT_TIMESERIES);
      if (code) goto _exit;

      code = grantCheck(TSDB_GRANT_TABLE);
      if (code) goto _exit;

      // alloc if need
      if (pSubmitRsp->aCreateTbRsp == NULL &&
          (pSubmitRsp->aCreateTbRsp = taosArrayInit(TARRAY_SIZE(pSubmitReq->aSubmitTbData), sizeof(SVCreateTbRsp))) ==
              NULL) {
H
Hongze Cheng 已提交
1295
        code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306
        goto _exit;
      }

      SVCreateTbRsp *pCreateTbRsp = taosArrayReserve(pSubmitRsp->aCreateTbRsp, 1);

      // create table
      if (metaCreateTable(pVnode->pMeta, version, pSubmitTbData->pCreateTbReq, &pCreateTbRsp->pMeta) ==
          0) {  // create table success

        if (newTbUids == NULL &&
            (newTbUids = taosArrayInit(TARRAY_SIZE(pSubmitReq->aSubmitTbData), sizeof(int64_t))) == NULL) {
H
Hongze Cheng 已提交
1307
          code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
1308 1309 1310 1311 1312 1313
          goto _exit;
        }

        taosArrayPush(newTbUids, &pSubmitTbData->uid);

        if (pCreateTbRsp->pMeta) {
D
dapan1121 已提交
1314
          vnodeUpdateMetaRsp(pVnode, pCreateTbRsp->pMeta);
H
Hongze Cheng 已提交
1315 1316 1317 1318 1319 1320
        }
      } else {  // create table failed
        if (terrno != TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
          code = terrno;
          goto _exit;
        }
1321
        pSubmitTbData->uid = pSubmitTbData->pCreateTbReq->uid;  // update uid if table exist for using below
H
Hongze Cheng 已提交
1322
      }
H
Hongze Cheng 已提交
1323 1324 1325
    }

    // insert data
H
Hongze Cheng 已提交
1326 1327 1328 1329 1330
    int32_t affectedRows;
    code = tsdbInsertTableData(pVnode->pTsdb, version, pSubmitTbData, &affectedRows);
    if (code) goto _exit;

    pSubmitRsp->affectedRows += affectedRows;
H
Hongze Cheng 已提交
1331
  }
H
Hongze Cheng 已提交
1332

H
Hongze Cheng 已提交
1333 1334 1335 1336 1337
  // update table uid list
  if (taosArrayGetSize(newTbUids) > 0) {
    vDebug("vgId:%d, add %d table into query table list in handling submit", TD_VID(pVnode),
           (int32_t)taosArrayGetSize(newTbUids));
    tqUpdateTbUidList(pVnode->pTq, newTbUids, true);
H
Hongze Cheng 已提交
1338
  }
H
Hongze Cheng 已提交
1339 1340 1341 1342 1343 1344 1345 1346 1347 1348

_exit:
  // message
  pRsp->code = code;
  tEncodeSize(tEncodeSSubmitRsp2, pSubmitRsp, pRsp->contLen, ret);
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
  tEncodeSSubmitRsp2(&ec, pSubmitRsp);
  tEncoderClear(&ec);

H
Hongze Cheng 已提交
1349 1350 1351 1352 1353 1354
  // update statistics
  atomic_add_fetch_64(&pVnode->statis.nInsert, pSubmitRsp->affectedRows);
  atomic_add_fetch_64(&pVnode->statis.nInsertSuccess, pSubmitRsp->affectedRows);
  atomic_add_fetch_64(&pVnode->statis.nBatchInsert, 1);
  if (code == 0) {
    atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
K
kailixu 已提交
1355
    tdProcessRSmaSubmit(pVnode->pSma, version, pSubmitReq, pReq, len, STREAM_INPUT__DATA_SUBMIT);
H
Hongze Cheng 已提交
1356 1357
  }

H
Hongze Cheng 已提交
1358 1359
  // clear
  taosArrayDestroy(newTbUids);
1360
  tDestroySSubmitReq2(pSubmitReq, 0 == pMsg->version ? TSDB_MSG_FLG_CMPT : TSDB_MSG_FLG_DECODE);
H
Hongze Cheng 已提交
1361
  tDestroySSubmitRsp2(pSubmitRsp, TSDB_MSG_FLG_ENCODE);
H
Hongze Cheng 已提交
1362

D
dapan1121 已提交
1363 1364
  if (code) terrno = code;

1365
  taosMemoryFree(pAllocMsg);
1366

H
Hongze Cheng 已提交
1367
  return code;
L
Liu Jicong 已提交
1368
}
1369

1370
static int32_t vnodeProcessCreateTSmaReq(SVnode *pVnode, int64_t version, void *pReq, int32_t len, SRpcMsg *pRsp) {
1371
  SVCreateTSmaReq req = {0};
C
Cary Xu 已提交
1372
  SDecoder        coder = {0};
1373

C
Cary Xu 已提交
1374 1375 1376 1377 1378 1379
  if (pRsp) {
    pRsp->msgType = TDMT_VND_CREATE_SMA_RSP;
    pRsp->code = TSDB_CODE_SUCCESS;
    pRsp->pCont = NULL;
    pRsp->contLen = 0;
  }
1380 1381 1382 1383 1384

  // decode and process req
  tDecoderInit(&coder, pReq, len);

  if (tDecodeSVCreateTSmaReq(&coder, &req) < 0) {
C
Cary Xu 已提交
1385 1386
    terrno = TSDB_CODE_MSG_DECODE_ERROR;
    if (pRsp) pRsp->code = terrno;
1387
    goto _err;
1388
  }
C
Cary Xu 已提交
1389

C
Cary Xu 已提交
1390
  if (tdProcessTSmaCreate(pVnode->pSma, version, (const char *)&req) < 0) {
C
Cary Xu 已提交
1391
    if (pRsp) pRsp->code = terrno;
1392
    goto _err;
1393
  }
C
Cary Xu 已提交
1394

1395
  tDecoderClear(&coder);
S
Shengliang Guan 已提交
1396
  vDebug("vgId:%d, success to create tsma %s:%" PRIi64 " version %" PRIi64 " for table %" PRIi64, TD_VID(pVnode),
C
Cary Xu 已提交
1397
         req.indexName, req.indexUid, version, req.tableUid);
H
Hongze Cheng 已提交
1398
  return 0;
1399 1400 1401

_err:
  tDecoderClear(&coder);
S
Shengliang Guan 已提交
1402
  vError("vgId:%d, failed to create tsma %s:%" PRIi64 " version %" PRIi64 "for table %" PRIi64 " since %s",
C
Cary Xu 已提交
1403
         TD_VID(pVnode), req.indexName, req.indexUid, version, req.tableUid, terrstr());
1404
  return -1;
L
Liu Jicong 已提交
1405
}
C
Cary Xu 已提交
1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417

/**
 * @brief specific for smaDstVnode
 *
 * @param pVnode
 * @param pCont
 * @param contLen
 * @return int32_t
 */
int32_t vnodeProcessCreateTSma(SVnode *pVnode, void *pCont, uint32_t contLen) {
  return vnodeProcessCreateTSmaReq(pVnode, 1, pCont, contLen, NULL);
}
1418 1419 1420 1421 1422 1423 1424 1425 1426

static int32_t vnodeProcessAlterConfirmReq(SVnode *pVnode, int64_t version, void *pReq, int32_t len, SRpcMsg *pRsp) {
  vInfo("vgId:%d, alter replica confim msg is processed", TD_VID(pVnode));
  pRsp->msgType = TDMT_VND_ALTER_CONFIRM_RSP;
  pRsp->code = TSDB_CODE_SUCCESS;
  pRsp->pCont = NULL;
  pRsp->contLen = 0;

  return 0;
1427
}
S
Shengliang Guan 已提交
1428

1429
static int32_t vnodeProcessAlterConfigReq(SVnode *pVnode, int64_t version, void *pReq, int32_t len, SRpcMsg *pRsp) {
S
Shengliang Guan 已提交
1430 1431
  bool walChanged = false;
  bool tsdbChanged = false;
1432

S
Shengliang Guan 已提交
1433 1434
  SAlterVnodeConfigReq req = {0};
  if (tDeserializeSAlterVnodeConfigReq(pReq, len, &req) != 0) {
1435 1436 1437 1438
    terrno = TSDB_CODE_INVALID_MSG;
    return TSDB_CODE_INVALID_MSG;
  }

1439
  vInfo("vgId:%d, start to alter vnode config, page:%d pageSize:%d buffer:%d szPage:%d szBuf:%" PRIu64
S
Shengliang Guan 已提交
1440
        " cacheLast:%d cacheLastSize:%d days:%d keep0:%d keep1:%d keep2:%d fsync:%d level:%d",
1441 1442
        TD_VID(pVnode), req.pages, req.pageSize, req.buffer, req.pageSize * 1024, (uint64_t)req.buffer * 1024 * 1024,
        req.cacheLast, req.cacheLastSize, req.daysPerFile, req.daysToKeep0, req.daysToKeep1, req.daysToKeep2,
S
Shengliang Guan 已提交
1443
        req.walFsyncPeriod, req.walLevel);
1444 1445 1446

  if (pVnode->config.cacheLastSize != req.cacheLastSize) {
    pVnode->config.cacheLastSize = req.cacheLastSize;
1447 1448
    tsdbCacheSetCapacity(pVnode, (size_t)pVnode->config.cacheLastSize * 1024 * 1024);
  }
1449

1450
  if (pVnode->config.szBuf != req.buffer * 1024LL * 1024LL) {
S
Shengliang Guan 已提交
1451
    vInfo("vgId:%d, vnode buffer is changed from %" PRId64 " to %" PRId64, TD_VID(pVnode), pVnode->config.szBuf,
1452
          (uint64_t)(req.buffer * 1024LL * 1024LL));
1453
    pVnode->config.szBuf = req.buffer * 1024LL * 1024LL;
H
Hongze Cheng 已提交
1454 1455
  }

1456 1457
  if (pVnode->config.szCache != req.pages) {
    if (metaAlterCache(pVnode->pMeta, req.pages) < 0) {
S
Shengliang Guan 已提交
1458
      vError("vgId:%d, failed to change vnode pages from %d to %d failed since %s", TD_VID(pVnode),
1459
             pVnode->config.szCache, req.pages, tstrerror(errno));
H
Hongze Cheng 已提交
1460 1461
      return errno;
    } else {
S
Shengliang Guan 已提交
1462
      vInfo("vgId:%d, vnode pages is changed from %d to %d", TD_VID(pVnode), pVnode->config.szCache, req.pages);
1463
      pVnode->config.szCache = req.pages;
H
Hongze Cheng 已提交
1464
    }
H
Hongze Cheng 已提交
1465 1466
  }

1467 1468
  if (pVnode->config.cacheLast != req.cacheLast) {
    pVnode->config.cacheLast = req.cacheLast;
1469 1470
  }

1471 1472
  if (pVnode->config.walCfg.fsyncPeriod != req.walFsyncPeriod) {
    pVnode->config.walCfg.fsyncPeriod = req.walFsyncPeriod;
1473 1474 1475 1476

    walChanged = true;
  }

1477 1478
  if (pVnode->config.walCfg.level != req.walLevel) {
    pVnode->config.walCfg.level = req.walLevel;
1479 1480 1481 1482

    walChanged = true;
  }

1483 1484
  if (pVnode->config.tsdbCfg.keep0 != req.daysToKeep0) {
    pVnode->config.tsdbCfg.keep0 = req.daysToKeep0;
1485
    if (!VND_IS_RSMA(pVnode)) {
M
Minglei Jin 已提交
1486
      tsdbChanged = true;
1487 1488 1489
    }
  }

1490 1491
  if (pVnode->config.tsdbCfg.keep1 != req.daysToKeep1) {
    pVnode->config.tsdbCfg.keep1 = req.daysToKeep1;
1492
    if (!VND_IS_RSMA(pVnode)) {
M
Minglei Jin 已提交
1493
      tsdbChanged = true;
1494 1495 1496
    }
  }

1497 1498
  if (pVnode->config.tsdbCfg.keep2 != req.daysToKeep2) {
    pVnode->config.tsdbCfg.keep2 = req.daysToKeep2;
1499
    if (!VND_IS_RSMA(pVnode)) {
M
Minglei Jin 已提交
1500
      tsdbChanged = true;
1501 1502 1503 1504
    }
  }

  if (walChanged) {
M
Minglei Jin 已提交
1505 1506 1507 1508 1509
    walAlter(pVnode->pWal, &pVnode->config.walCfg);
  }

  if (tsdbChanged) {
    tsdbSetKeepCfg(pVnode->pTsdb, &pVnode->config.tsdbCfg);
1510 1511
  }

1512 1513 1514
  return 0;
}

1515 1516 1517 1518 1519 1520
static int32_t vnodeProcessBatchDeleteReq(SVnode *pVnode, int64_t version, void *pReq, int32_t len, SRpcMsg *pRsp) {
  SBatchDeleteReq deleteReq;
  SDecoder        decoder;
  tDecoderInit(&decoder, pReq, len);
  tDecodeSBatchDeleteReq(&decoder, &deleteReq);

1521
  SMetaReader mr = {0};
1522
  metaReaderInit(&mr, pVnode->pMeta, META_READER_NOLOCK);
1523

1524 1525 1526
  int32_t sz = taosArrayGetSize(deleteReq.deleteReqs);
  for (int32_t i = 0; i < sz; i++) {
    SSingleDeleteReq *pOneReq = taosArrayGet(deleteReq.deleteReqs, i);
1527 1528
    char             *name = pOneReq->tbname;
    if (metaGetTableEntryByName(&mr, name) < 0) {
S
Shengliang Guan 已提交
1529
      vDebug("vgId:%d, stream delete msg, skip since no table: %s", pVnode->config.vgId, name);
1530 1531 1532 1533 1534
      continue;
    }

    int64_t uid = mr.me.uid;

L
Liu Jicong 已提交
1535
    int32_t code = tsdbDeleteTableData(pVnode->pTsdb, version, deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
1536 1537 1538
    if (code < 0) {
      terrno = code;
      vError("vgId:%d, delete error since %s, suid:%" PRId64 ", uid:%" PRId64 ", start ts:%" PRId64 ", end ts:%" PRId64,
L
Liu Jicong 已提交
1539
             TD_VID(pVnode), terrstr(), deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
1540
    }
1541 1542

    tDecoderClear(&mr.coder);
1543
  }
1544
  metaReaderClear(&mr);
1545
  taosArrayDestroy(deleteReq.deleteReqs);
1546 1547 1548
  return 0;
}

H
Hongze Cheng 已提交
1549 1550 1551 1552 1553
static int32_t vnodeProcessDeleteReq(SVnode *pVnode, int64_t version, void *pReq, int32_t len, SRpcMsg *pRsp) {
  int32_t     code = 0;
  SDecoder   *pCoder = &(SDecoder){0};
  SDeleteRes *pRes = &(SDeleteRes){0};

wmmhello's avatar
wmmhello 已提交
1554 1555 1556 1557 1558
  pRsp->msgType = TDMT_VND_DELETE_RSP;
  pRsp->pCont = NULL;
  pRsp->contLen = 0;
  pRsp->code = TSDB_CODE_SUCCESS;

H
Hongze Cheng 已提交
1559 1560 1561 1562 1563 1564 1565 1566
  pRes->uidList = taosArrayInit(0, sizeof(tb_uid_t));
  if (pRes->uidList == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }

  tDecoderInit(pCoder, pReq, len);
  tDecodeDeleteRes(pCoder, pRes);
1567
  ASSERT(taosArrayGetSize(pRes->uidList) == 0 || (pRes->skey != 0 && pRes->ekey != 0));
H
Hongze Cheng 已提交
1568 1569 1570 1571 1572 1573 1574 1575 1576

  for (int32_t iUid = 0; iUid < taosArrayGetSize(pRes->uidList); iUid++) {
    code = tsdbDeleteTableData(pVnode->pTsdb, version, pRes->suid, *(uint64_t *)taosArrayGet(pRes->uidList, iUid),
                               pRes->skey, pRes->ekey);
    if (code) goto _err;
  }

  tDecoderClear(pCoder);
  taosArrayDestroy(pRes->uidList);
wmmhello's avatar
wmmhello 已提交
1577 1578

  SVDeleteRsp rsp = {.affectedRows = pRes->affectedRows};
L
Liu Jicong 已提交
1579
  int32_t     ret = 0;
wmmhello's avatar
wmmhello 已提交
1580 1581
  tEncodeSize(tEncodeSVDeleteRsp, &rsp, pRsp->contLen, ret);
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
L
Liu Jicong 已提交
1582
  SEncoder ec = {0};
wmmhello's avatar
wmmhello 已提交
1583 1584 1585
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
  tEncodeSVDeleteRsp(&ec, &rsp);
  tEncoderClear(&ec);
H
Hongze Cheng 已提交
1586 1587 1588 1589
  return code;

_err:
  return code;
M
Minglei Jin 已提交
1590
}
dengyihao's avatar
dengyihao 已提交
1591
static int32_t vnodeProcessCreateIndexReq(SVnode *pVnode, int64_t version, void *pReq, int32_t len, SRpcMsg *pRsp) {
dengyihao's avatar
dengyihao 已提交
1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615
  SVCreateStbReq req = {0};
  SDecoder       dc = {0};

  pRsp->msgType = TDMT_VND_CREATE_INDEX_RSP;
  pRsp->code = TSDB_CODE_SUCCESS;
  pRsp->pCont = NULL;
  pRsp->contLen = 0;

  tDecoderInit(&dc, pReq, len);
  // decode req
  if (tDecodeSVCreateStbReq(&dc, &req) < 0) {
    terrno = TSDB_CODE_INVALID_MSG;
    tDecoderClear(&dc);
    return -1;
  }
  if (metaAddIndexToSTable(pVnode->pMeta, version, &req) < 0) {
    pRsp->code = terrno;
    goto _err;
  }
  tDecoderClear(&dc);
  return 0;
_err:
  tDecoderClear(&dc);
  return -1;
dengyihao's avatar
dengyihao 已提交
1616 1617
}
static int32_t vnodeProcessDropIndexReq(SVnode *pVnode, int64_t version, void *pReq, int32_t len, SRpcMsg *pRsp) {
dengyihao's avatar
dengyihao 已提交
1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631
  SDropIndexReq req = {0};
  pRsp->msgType = TDMT_VND_CREATE_INDEX_RSP;
  pRsp->code = TSDB_CODE_SUCCESS;
  pRsp->pCont = NULL;
  pRsp->contLen = 0;

  if (tDeserializeSDropIdxReq(pReq, len, &req)) {
    terrno = TSDB_CODE_INVALID_MSG;
    return -1;
  }
  if (metaDropIndexFromSTable(pVnode->pMeta, version, &req) < 0) {
    pRsp->code = terrno;
    return -1;
  }
dengyihao's avatar
dengyihao 已提交
1632 1633
  return TSDB_CODE_SUCCESS;
}
1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647

static int32_t vnodeProcessCompactVnodeReq(SVnode *pVnode, int64_t version, void *pReq, int32_t len, SRpcMsg *pRsp) {
  SCompactVnodeReq req = {0};
  if (tDeserializeSCompactVnodeReq(pReq, len, &req) != 0) {
    terrno = TSDB_CODE_INVALID_MSG;
    return TSDB_CODE_INVALID_MSG;
  }
  vInfo("vgId:%d, compact msg will be processed, db:%s dbUid:%" PRId64 " compactStartTime:%" PRId64, TD_VID(pVnode),
        req.db, req.dbUid, req.compactStartTime);

  vnodeAsyncCompact(pVnode);
  vnodeBegin(pVnode);

  return 0;
dengyihao's avatar
dengyihao 已提交
1648
}