• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

taosdata / TDengine / #3599

08 Feb 2025 11:23AM UTC coverage: 1.77% (-61.6%) from 63.396%
#3599

push

travis-ci

web-flow
Merge pull request #29712 from taosdata/fix/TD-33652-3.0

fix: reduce write rows from 30w to 3w

3776 of 278949 branches covered (1.35%)

Branch coverage included in aggregate %.

6012 of 274147 relevant lines covered (2.19%)

1642.73 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/source/dnode/vnode/src/vnd/vnodeSvr.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#include "audit.h"
17
#include "cos.h"
18
#include "monitor.h"
19
#include "tencode.h"
20
#include "tglobal.h"
21
#include "tmsg.h"
22
#include "tstrbuild.h"
23
#include "vnd.h"
24
#include "vnode.h"
25
#include "vnodeInt.h"
26

27
extern taos_counter_t *tsInsertCounter;
28

29
static int32_t vnodeProcessCreateStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
30
static int32_t vnodeProcessAlterStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
31
static int32_t vnodeProcessDropStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
32
static int32_t vnodeProcessCreateTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
33
                                       SRpcMsg *pOriginRpc);
34
static int32_t vnodeProcessAlterTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
35
static int32_t vnodeProcessDropTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
36
                                     SRpcMsg *pOriginRpc);
37
static int32_t vnodeProcessSubmitReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
38
                                     SRpcMsg *pOriginalMsg);
39
static int32_t vnodeProcessCreateTSmaReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
40
static int32_t vnodeProcessAlterConfirmReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
41
static int32_t vnodeProcessAlterConfigReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
42
static int32_t vnodeProcessDropTtlTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
43
static int32_t vnodeProcessTrimReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
44
static int32_t vnodeProcessS3MigrateReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
45
static int32_t vnodeProcessDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
46
                                     SRpcMsg *pOriginalMsg);
47
static int32_t vnodeProcessBatchDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
48
static int32_t vnodeProcessCreateIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
49
static int32_t vnodeProcessDropIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
50
static int32_t vnodeProcessCompactVnodeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
51
static int32_t vnodeProcessConfigChangeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
52
static int32_t vnodeProcessArbCheckSyncReq(SVnode *pVnode, void *pReq, int32_t len, SRpcMsg *pRsp);
53
static int32_t vnodeProcessDropTSmaCtbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
54
                                          SRpcMsg *pOriginRpc);
55

56
static int32_t vnodePreCheckAssignedLogSyncd(SVnode *pVnode, char *member0Token, char *member1Token);
57
static int32_t vnodeCheckAssignedLogSyncd(SVnode *pVnode, char *member0Token, char *member1Token);
58
static int32_t vnodeProcessFetchTtlExpiredTbs(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
59

60
extern int32_t vnodeProcessKillCompactReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
61
extern int32_t vnodeQueryCompactProgress(SVnode *pVnode, SRpcMsg *pMsg);
62

63
static int32_t vnodePreprocessCreateTableReq(SVnode *pVnode, SDecoder *pCoder, int64_t btime, int64_t *pUid) {
×
64
  int32_t code = 0;
×
65
  int32_t lino = 0;
×
66

67
  if (tStartDecode(pCoder) < 0) {
×
68
    code = TSDB_CODE_INVALID_MSG;
×
69
    TSDB_CHECK_CODE(code, lino, _exit);
×
70
  }
71

72
  // flags
73
  if (tDecodeI32v(pCoder, NULL) < 0) {
×
74
    code = TSDB_CODE_INVALID_MSG;
×
75
    TSDB_CHECK_CODE(code, lino, _exit);
×
76
  }
77

78
  // name
79
  char *name = NULL;
×
80
  if (tDecodeCStr(pCoder, &name) < 0) {
×
81
    code = TSDB_CODE_INVALID_MSG;
×
82
    TSDB_CHECK_CODE(code, lino, _exit);
×
83
  }
84

85
  // uid
86
  int64_t uid = metaGetTableEntryUidByName(pVnode->pMeta, name);
×
87
  if (uid == 0) {
×
88
    uid = tGenIdPI64();
×
89
  }
90
  *(int64_t *)(pCoder->data + pCoder->pos) = uid;
×
91

92
  // btime
93
  *(int64_t *)(pCoder->data + pCoder->pos + 8) = btime;
×
94

95
  tEndDecode(pCoder);
×
96

97
_exit:
×
98
  if (code) {
×
99
    vError("vgId:%d %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
100
  } else {
101
    vTrace("vgId:%d %s done, table:%s uid generated:%" PRId64, TD_VID(pVnode), __func__, name, uid);
×
102
    if (pUid) *pUid = uid;
×
103
  }
104
  return code;
×
105
}
106
static int32_t vnodePreProcessCreateTableMsg(SVnode *pVnode, SRpcMsg *pMsg) {
×
107
  int32_t code = 0;
×
108
  int32_t lino = 0;
×
109

110
  int64_t  btime = taosGetTimestampMs();
×
111
  SDecoder dc = {0};
×
112
  int32_t  nReqs;
113

114
  tDecoderInit(&dc, (uint8_t *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead));
×
115
  if (tStartDecode(&dc) < 0) {
×
116
    code = TSDB_CODE_INVALID_MSG;
×
117
    return code;
×
118
  }
119

120
  if (tDecodeI32v(&dc, &nReqs) < 0) {
×
121
    code = TSDB_CODE_INVALID_MSG;
×
122
    TSDB_CHECK_CODE(code, lino, _exit);
×
123
  }
124
  for (int32_t iReq = 0; iReq < nReqs; iReq++) {
×
125
    code = vnodePreprocessCreateTableReq(pVnode, &dc, btime, NULL);
×
126
    TSDB_CHECK_CODE(code, lino, _exit);
×
127
  }
128

129
  tEndDecode(&dc);
×
130

131
_exit:
×
132
  tDecoderClear(&dc);
×
133
  if (code) {
×
134
    vError("vgId:%d, %s:%d failed to preprocess submit request since %s, msg type:%s", TD_VID(pVnode), __func__, lino,
×
135
           tstrerror(code), TMSG_INFO(pMsg->msgType));
136
  }
137
  return code;
×
138
}
139

140
static int32_t vnodePreProcessAlterTableMsg(SVnode *pVnode, SRpcMsg *pMsg) {
×
141
  int32_t code = TSDB_CODE_INVALID_MSG;
×
142
  int32_t lino = 0;
×
143

144
  SDecoder dc = {0};
×
145
  tDecoderInit(&dc, (uint8_t *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead));
×
146

147
  SVAlterTbReq vAlterTbReq = {0};
×
148
  int64_t      ctimeMs = taosGetTimestampMs();
×
149
  if (tDecodeSVAlterTbReqSetCtime(&dc, &vAlterTbReq, ctimeMs) < 0) {
×
150
    taosArrayDestroy(vAlterTbReq.pMultiTag);
×
151
    vAlterTbReq.pMultiTag = NULL;
×
152
    goto _exit;
×
153
  }
154
  taosArrayDestroy(vAlterTbReq.pMultiTag);
×
155
  vAlterTbReq.pMultiTag = NULL;
×
156

157
  code = 0;
×
158

159
_exit:
×
160
  tDecoderClear(&dc);
×
161
  if (code) {
×
162
    vError("vgId:%d %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
163
  } else {
164
    vTrace("vgId:%d %s done, table:%s ctimeMs generated:%" PRId64, TD_VID(pVnode), __func__, vAlterTbReq.tbName,
×
165
           ctimeMs);
166
  }
167
  return code;
×
168
}
169

170
static int32_t vnodePreProcessDropTtlMsg(SVnode *pVnode, SRpcMsg *pMsg) {
×
171
  int32_t code = TSDB_CODE_INVALID_MSG;
×
172
  int32_t lino = 0;
×
173

174
  SMsgHead *pContOld = pMsg->pCont;
×
175
  int32_t   reqLenOld = pMsg->contLen - sizeof(SMsgHead);
×
176

177
  SArray *tbUids = NULL;
×
178
  int64_t timestampMs = 0;
×
179

180
  SVDropTtlTableReq ttlReq = {0};
×
181
  if (tDeserializeSVDropTtlTableReq((char *)pContOld + sizeof(SMsgHead), reqLenOld, &ttlReq) != 0) {
×
182
    code = TSDB_CODE_INVALID_MSG;
×
183
    TSDB_CHECK_CODE(code, lino, _exit);
×
184
  }
185

186
  {  // find expired uids
187
    tbUids = taosArrayInit(8, sizeof(tb_uid_t));
×
188
    if (tbUids == NULL) {
×
189
      code = terrno;
×
190
      TSDB_CHECK_CODE(code, lino, _exit);
×
191
    }
192

193
    timestampMs = (int64_t)ttlReq.timestampSec * 1000;
×
194
    code = metaTtlFindExpired(pVnode->pMeta, timestampMs, tbUids, ttlReq.ttlDropMaxCount);
×
195
    if (code != 0) {
×
196
      code = TSDB_CODE_INVALID_MSG;
×
197
      TSDB_CHECK_CODE(code, lino, _exit);
×
198
    }
199

200
    ttlReq.nUids = taosArrayGetSize(tbUids);
×
201
    ttlReq.pTbUids = tbUids;
×
202
  }
203

204
  if (ttlReq.nUids == 0) {
×
205
    code = TSDB_CODE_MSG_PREPROCESSED;
×
206
    TSDB_CHECK_CODE(code, lino, _exit);
×
207
  }
208

209
  {  // prepare new content
210
    int32_t reqLenNew = tSerializeSVDropTtlTableReq(NULL, 0, &ttlReq);
×
211
    int32_t contLenNew = reqLenNew + sizeof(SMsgHead);
×
212

213
    SMsgHead *pContNew = rpcMallocCont(contLenNew);
×
214
    if (pContNew == NULL) {
×
215
      code = terrno;
×
216
      TSDB_CHECK_CODE(code, lino, _exit);
×
217
    }
218

219
    if (tSerializeSVDropTtlTableReq((char *)pContNew + sizeof(SMsgHead), reqLenNew, &ttlReq) != 0) {
×
220
      vError("vgId:%d %s:%d failed to serialize drop ttl request", TD_VID(pVnode), __func__, lino);
×
221
    }
222
    pContNew->contLen = htonl(reqLenNew);
×
223
    pContNew->vgId = pContOld->vgId;
×
224

225
    rpcFreeCont(pContOld);
×
226
    pMsg->pCont = pContNew;
×
227
    pMsg->contLen = contLenNew;
×
228
  }
229

230
  code = 0;
×
231

232
_exit:
×
233
  taosArrayDestroy(tbUids);
×
234

235
  if (code && code != TSDB_CODE_MSG_PREPROCESSED) {
×
236
    vError("vgId:%d, %s:%d failed to preprocess drop ttl request since %s, msg type:%s", TD_VID(pVnode), __func__, lino,
×
237
           tstrerror(code), TMSG_INFO(pMsg->msgType));
238
  } else {
239
    vTrace("vgId:%d, %s done, timestampSec:%d, nUids:%d", TD_VID(pVnode), __func__, ttlReq.timestampSec, ttlReq.nUids);
×
240
  }
241

242
  return code;
×
243
}
244

245
extern int64_t tsMaxKeyByPrecision[];
246
static int32_t vnodePreProcessSubmitTbData(SVnode *pVnode, SDecoder *pCoder, int64_t btimeMs, int64_t ctimeMs) {
×
247
  int32_t code = 0;
×
248
  int32_t lino = 0;
×
249

250
  if (tStartDecode(pCoder) < 0) {
×
251
    code = TSDB_CODE_INVALID_MSG;
×
252
    TSDB_CHECK_CODE(code, lino, _exit);
×
253
  }
254

255
  SSubmitTbData submitTbData;
256
  uint8_t       version;
257
  if (tDecodeI32v(pCoder, &submitTbData.flags) < 0) {
×
258
    code = TSDB_CODE_INVALID_MSG;
×
259
    TSDB_CHECK_CODE(code, lino, _exit);
×
260
  }
261
  version = (submitTbData.flags >> 8) & 0xff;
×
262
  submitTbData.flags = submitTbData.flags & 0xff;
×
263

264
  int64_t uid;
265
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
×
266
    code = vnodePreprocessCreateTableReq(pVnode, pCoder, btimeMs, &uid);
×
267
    TSDB_CHECK_CODE(code, lino, _exit);
×
268
  }
269

270
  // submit data
271
  if (tDecodeI64(pCoder, &submitTbData.suid) < 0) {
×
272
    code = TSDB_CODE_INVALID_MSG;
×
273
    TSDB_CHECK_CODE(code, lino, _exit);
×
274
  }
275

276
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
×
277
    *(int64_t *)(pCoder->data + pCoder->pos) = uid;
×
278
    pCoder->pos += sizeof(int64_t);
×
279
  } else {
280
    if (tDecodeI64(pCoder, &submitTbData.uid) < 0) {
×
281
      code = TSDB_CODE_INVALID_MSG;
×
282
      TSDB_CHECK_CODE(code, lino, _exit);
×
283
    }
284
  }
285

286
  if (tDecodeI32v(pCoder, &submitTbData.sver) < 0) {
×
287
    code = TSDB_CODE_INVALID_MSG;
×
288
    TSDB_CHECK_CODE(code, lino, _exit);
×
289
  }
290

291
  // scan and check
292
  TSKEY now = btimeMs;
×
293
  if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_MICRO) {
×
294
    now *= 1000;
×
295
  } else if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_NANO) {
×
296
    now *= 1000000;
×
297
  }
298

299
  int32_t keep = pVnode->config.tsdbCfg.keep2;
×
300
  /*
301
  int32_t nlevel = tfsGetLevel(pVnode->pTfs);
302
  if (nlevel > 1 && tsS3Enabled) {
303
    if (nlevel == 3) {
304
      keep = pVnode->config.tsdbCfg.keep1;
305
    } else if (nlevel == 2) {
306
      keep = pVnode->config.tsdbCfg.keep0;
307
    }
308
  }
309
  */
310

311
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * keep;
×
312
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
×
313
  if (submitTbData.flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
×
314
    uint64_t nColData;
315
    if (tDecodeU64v(pCoder, &nColData) < 0) {
×
316
      code = TSDB_CODE_INVALID_MSG;
×
317
      goto _exit;
×
318
    }
319

320
    SColData colData = {0};
×
321
    code = tDecodeColData(version, pCoder, &colData);
×
322
    if (code) {
×
323
      code = TSDB_CODE_INVALID_MSG;
×
324
      goto _exit;
×
325
    }
326

327
    if (colData.flag != HAS_VALUE) {
×
328
      code = TSDB_CODE_INVALID_MSG;
×
329
      goto _exit;
×
330
    }
331

332
    for (int32_t iRow = 0; iRow < colData.nVal; iRow++) {
×
333
      if (((TSKEY *)colData.pData)[iRow] < minKey || ((TSKEY *)colData.pData)[iRow] > maxKey) {
×
334
        code = TSDB_CODE_TDB_TIMESTAMP_OUT_OF_RANGE;
×
335
        goto _exit;
×
336
      }
337
    }
338

339
    for (uint64_t i = 1; i < nColData; i++) {
×
340
      code = tDecodeColData(version, pCoder, &colData);
×
341
      if (code) {
×
342
        code = TSDB_CODE_INVALID_MSG;
×
343
        goto _exit;
×
344
      }
345
    }
346
  } else {
347
    uint64_t nRow;
348
    if (tDecodeU64v(pCoder, &nRow) < 0) {
×
349
      code = TSDB_CODE_INVALID_MSG;
×
350
      goto _exit;
×
351
    }
352

353
    for (int32_t iRow = 0; iRow < nRow; ++iRow) {
×
354
      SRow *pRow = (SRow *)(pCoder->data + pCoder->pos);
×
355
      pCoder->pos += pRow->len;
×
356

357
      if (pRow->ts < minKey || pRow->ts > maxKey) {
×
358
        code = TSDB_CODE_TDB_TIMESTAMP_OUT_OF_RANGE;
×
359
        goto _exit;
×
360
      }
361
    }
362
  }
363

364
  if (!tDecodeIsEnd(pCoder)) {
×
365
    *(int64_t *)(pCoder->data + pCoder->pos) = ctimeMs;
×
366
    pCoder->pos += sizeof(int64_t);
×
367
  }
368

369
  tEndDecode(pCoder);
×
370

371
_exit:
×
372
  return code;
×
373
}
374
static int32_t vnodePreProcessSubmitMsg(SVnode *pVnode, SRpcMsg *pMsg) {
×
375
  int32_t code = 0;
×
376
  int32_t lino = 0;
×
377

378
  if (tsBypassFlag & TSDB_BYPASS_RA_RPC_RECV_SUBMIT) {
×
379
    return TSDB_CODE_MSG_PREPROCESSED;
×
380
  }
381

382
  SDecoder *pCoder = &(SDecoder){0};
×
383

384
  if (taosHton64(((SSubmitReq2Msg *)pMsg->pCont)->version) != 1) {
×
385
    code = TSDB_CODE_INVALID_MSG;
×
386
    TSDB_CHECK_CODE(code, lino, _exit);
×
387
  }
388

389
  tDecoderInit(pCoder, (uint8_t *)pMsg->pCont + sizeof(SSubmitReq2Msg), pMsg->contLen - sizeof(SSubmitReq2Msg));
×
390

391
  if (tStartDecode(pCoder) < 0) {
×
392
    code = TSDB_CODE_INVALID_MSG;
×
393
    TSDB_CHECK_CODE(code, lino, _exit);
×
394
  }
395

396
  uint64_t nSubmitTbData;
397
  if (tDecodeU64v(pCoder, &nSubmitTbData) < 0) {
×
398
    code = TSDB_CODE_INVALID_MSG;
×
399
    TSDB_CHECK_CODE(code, lino, _exit);
×
400
  }
401

402
  int64_t btimeMs = taosGetTimestampMs();
×
403
  int64_t ctimeMs = btimeMs;
×
404
  for (int32_t i = 0; i < nSubmitTbData; i++) {
×
405
    code = vnodePreProcessSubmitTbData(pVnode, pCoder, btimeMs, ctimeMs);
×
406
    TSDB_CHECK_CODE(code, lino, _exit);
×
407
  }
408

409
  tEndDecode(pCoder);
×
410

411
_exit:
×
412
  tDecoderClear(pCoder);
×
413
  if (code) {
×
414
    vError("vgId:%d, %s:%d failed to preprocess submit request since %s, msg type:%s", TD_VID(pVnode), __func__, lino,
×
415
           tstrerror(code), TMSG_INFO(pMsg->msgType));
416
  }
417
  return code;
×
418
}
419

420
static int32_t vnodePreProcessDeleteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
×
421
  int32_t code = 0;
×
422

423
  int32_t    size;
424
  int32_t    ret;
425
  uint8_t   *pCont;
426
  SEncoder  *pCoder = &(SEncoder){0};
×
427
  SDeleteRes res = {0};
×
428

429
  SReadHandle handle = {.vnode = pVnode, .pMsgCb = &pVnode->msgCb, .skipRollup = 1};
×
430
  initStorageAPI(&handle.api);
×
431

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

435
  res.ctimeMs = taosGetTimestampMs();
×
436
  // malloc and encode
437
  tEncodeSize(tEncodeDeleteRes, &res, size, ret);
×
438
  pCont = rpcMallocCont(size + sizeof(SMsgHead));
×
439

440
  ((SMsgHead *)pCont)->contLen = size + sizeof(SMsgHead);
×
441
  ((SMsgHead *)pCont)->vgId = TD_VID(pVnode);
×
442

443
  tEncoderInit(pCoder, pCont + sizeof(SMsgHead), size);
×
444
  if (tEncodeDeleteRes(pCoder, &res) != 0) {
×
445
    vError("vgId:%d %s failed to encode delete response", TD_VID(pVnode), __func__);
×
446
  }
447
  tEncoderClear(pCoder);
×
448

449
  rpcFreeCont(pMsg->pCont);
×
450
  pMsg->pCont = pCont;
×
451
  pMsg->contLen = size + sizeof(SMsgHead);
×
452

453
  taosArrayDestroy(res.uidList);
×
454

455
_exit:
×
456
  return code;
×
457
}
458

459
static int32_t vnodePreProcessBatchDeleteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
×
460
  int32_t code = 0;
×
461
  int32_t lino = 0;
×
462

463
  int64_t         ctimeMs = taosGetTimestampMs();
×
464
  SBatchDeleteReq pReq = {0};
×
465
  SDecoder       *pCoder = &(SDecoder){0};
×
466

467
  tDecoderInit(pCoder, (uint8_t *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead));
×
468

469
  if (tDecodeSBatchDeleteReqSetCtime(pCoder, &pReq, ctimeMs) < 0) {
×
470
    code = TSDB_CODE_INVALID_MSG;
×
471
  }
472

473
  tDecoderClear(pCoder);
×
474
  taosArrayDestroy(pReq.deleteReqs);
×
475

476
  if (code) {
×
477
    vError("vgId:%d %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
478
  } else {
479
    vTrace("vgId:%d %s done, ctimeMs generated:%" PRId64, TD_VID(pVnode), __func__, ctimeMs);
×
480
  }
481
  return code;
×
482
}
483

484
static int32_t vnodePreProcessArbCheckSyncMsg(SVnode *pVnode, SRpcMsg *pMsg) {
×
485
  SVArbCheckSyncReq syncReq = {0};
×
486

487
  if (tDeserializeSVArbCheckSyncReq((char *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead),
×
488
                                    &syncReq) != 0) {
489
    return TSDB_CODE_INVALID_MSG;
×
490
  }
491

492
  int32_t ret = vnodePreCheckAssignedLogSyncd(pVnode, syncReq.member0Token, syncReq.member1Token);
×
493
  if (ret != 0) {
×
494
    vError("vgId:%d, failed to preprocess arb check sync request since %s", TD_VID(pVnode), tstrerror(ret));
×
495
  }
496

497
  int32_t code = terrno;
×
498
  tFreeSVArbCheckSyncReq(&syncReq);
×
499

500
  return code;
×
501
}
502

503
int32_t vnodePreProcessDropTbMsg(SVnode *pVnode, SRpcMsg *pMsg) {
×
504
  int32_t          code = TSDB_CODE_SUCCESS;
×
505
  int32_t          lino = 0;
×
506
  int32_t          size = 0;
×
507
  SDecoder         dc = {0};
×
508
  SEncoder         ec = {0};
×
509
  SVDropTbBatchReq receivedBatchReqs = {0};
×
510
  SVDropTbBatchReq sentBatchReqs = {0};
×
511

512
  tDecoderInit(&dc, POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), pMsg->contLen - sizeof(SMsgHead));
×
513

514
  code = tDecodeSVDropTbBatchReq(&dc, &receivedBatchReqs);
×
515
  if (code < 0) {
×
516
    terrno = code;
×
517
    TSDB_CHECK_CODE(code, lino, _exit);
×
518
  }
519
  sentBatchReqs.pArray = taosArrayInit(receivedBatchReqs.nReqs, sizeof(SVDropTbReq));
×
520
  if (!sentBatchReqs.pArray) {
×
521
    code = terrno;
×
522
    goto _exit;
×
523
  }
524

525
  for (int32_t i = 0; i < receivedBatchReqs.nReqs; ++i) {
×
526
    SVDropTbReq *pReq = receivedBatchReqs.pReqs + i;
×
527
    tb_uid_t     uid = metaGetTableEntryUidByName(pVnode->pMeta, pReq->name);
×
528
    if (uid == 0) {
×
529
      vWarn("vgId:%d, preprocess drop ctb: %s not found", TD_VID(pVnode), pReq->name);
×
530
      continue;
×
531
    }
532
    pReq->uid = uid;
×
533
    vDebug("vgId:%d %s for: %s, uid: %" PRId64, TD_VID(pVnode), __func__, pReq->name, pReq->uid);
×
534
    if (taosArrayPush(sentBatchReqs.pArray, pReq) == NULL) {
×
535
      code = terrno;
×
536
      goto _exit;
×
537
    }
538
  }
539
  sentBatchReqs.nReqs = sentBatchReqs.pArray->size;
×
540

541
  tEncodeSize(tEncodeSVDropTbBatchReq, &sentBatchReqs, size, code);
×
542
  tEncoderInit(&ec, POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), size);
×
543
  code = tEncodeSVDropTbBatchReq(&ec, &sentBatchReqs);
×
544
  tEncoderClear(&ec);
×
545
  if (code != TSDB_CODE_SUCCESS) {
×
546
    vError("vgId:%d %s failed to encode drop tb batch req: %s", TD_VID(pVnode), __func__, tstrerror(code));
×
547
    TSDB_CHECK_CODE(code, lino, _exit);
×
548
  }
549

550
_exit:
×
551
  tDecoderClear(&dc);
×
552
  if (sentBatchReqs.pArray) {
×
553
    taosArrayDestroy(sentBatchReqs.pArray);
×
554
  }
555
  return code;
×
556
}
557

558
int32_t vnodePreProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
×
559
  int32_t code = 0;
×
560

561
  switch (pMsg->msgType) {
×
562
    case TDMT_VND_CREATE_TABLE: {
×
563
      code = vnodePreProcessCreateTableMsg(pVnode, pMsg);
×
564
    } break;
×
565
    case TDMT_VND_ALTER_TABLE: {
×
566
      code = vnodePreProcessAlterTableMsg(pVnode, pMsg);
×
567
    } break;
×
568
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
×
569
    case TDMT_VND_DROP_TTL_TABLE: {
570
      code = vnodePreProcessDropTtlMsg(pVnode, pMsg);
×
571
    } break;
×
572
    case TDMT_VND_SUBMIT: {
×
573
      code = vnodePreProcessSubmitMsg(pVnode, pMsg);
×
574
    } break;
×
575
    case TDMT_VND_DELETE: {
×
576
      code = vnodePreProcessDeleteMsg(pVnode, pMsg);
×
577
    } break;
×
578
    case TDMT_VND_BATCH_DEL: {
×
579
      code = vnodePreProcessBatchDeleteMsg(pVnode, pMsg);
×
580
    } break;
×
581
    case TDMT_VND_ARB_CHECK_SYNC: {
×
582
      code = vnodePreProcessArbCheckSyncMsg(pVnode, pMsg);
×
583
    } break;
×
584
    case TDMT_VND_DROP_TABLE: {
×
585
      code = vnodePreProcessDropTbMsg(pVnode, pMsg);
×
586
    } break;
×
587
    default:
×
588
      break;
×
589
  }
590

591
  if (code && code != TSDB_CODE_MSG_PREPROCESSED) {
×
592
    vError("vgId:%d, failed to preprocess write request since %s, msg type:%s", TD_VID(pVnode), tstrerror(code),
×
593
           TMSG_INFO(pMsg->msgType));
594
  }
595
  return code;
×
596
}
597

598
int32_t vnodeProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg, int64_t ver, SRpcMsg *pRsp) {
×
599
  int32_t code = 0;
×
600
  void   *ptr = NULL;
×
601
  void   *pReq;
602
  int32_t len;
603

604
  (void)taosThreadMutexLock(&pVnode->mutex);
×
605
  if (pVnode->disableWrite) {
×
606
    (void)taosThreadMutexUnlock(&pVnode->mutex);
×
607
    vError("vgId:%d write is disabled for snapshot, version:%" PRId64, TD_VID(pVnode), ver);
×
608
    return TSDB_CODE_VND_WRITE_DISABLED;
×
609
  }
610
  (void)taosThreadMutexUnlock(&pVnode->mutex);
×
611

612
  if (ver <= pVnode->state.applied) {
×
613
    vError("vgId:%d, duplicate write request. ver: %" PRId64 ", applied: %" PRId64 "", TD_VID(pVnode), ver,
×
614
           pVnode->state.applied);
615
    return terrno = TSDB_CODE_VND_DUP_REQUEST;
×
616
  }
617

618
  vDebug("vgId:%d, start to process write request %s, index:%" PRId64 ", applied:%" PRId64 ", state.applyTerm:%" PRId64
×
619
         ", conn.applyTerm:%" PRId64 ", contLen:%d",
620
         TD_VID(pVnode), TMSG_INFO(pMsg->msgType), ver, pVnode->state.applied, pVnode->state.applyTerm,
621
         pMsg->info.conn.applyTerm, pMsg->contLen);
622

623
  if (!(pVnode->state.applyTerm <= pMsg->info.conn.applyTerm)) {
×
624
    return terrno = TSDB_CODE_INTERNAL_ERROR;
×
625
  }
626

627
  if (!(pVnode->state.applied + 1 == ver)) {
×
628
    return terrno = TSDB_CODE_INTERNAL_ERROR;
×
629
  }
630

631
  atomic_store_64(&pVnode->state.applied, ver);
×
632
  atomic_store_64(&pVnode->state.applyTerm, pMsg->info.conn.applyTerm);
×
633

634
  if (!syncUtilUserCommit(pMsg->msgType)) goto _exit;
×
635

636
  // skip header
637
  pReq = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
×
638
  len = pMsg->contLen - sizeof(SMsgHead);
×
639
  bool needCommit = false;
×
640

641
  switch (pMsg->msgType) {
×
642
    /* META */
643
    case TDMT_VND_CREATE_STB:
×
644
      if (vnodeProcessCreateStbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
645
      break;
×
646
    case TDMT_VND_ALTER_STB:
×
647
      if (vnodeProcessAlterStbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
648
      break;
×
649
    case TDMT_VND_DROP_STB:
×
650
      if (vnodeProcessDropStbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
651
      break;
×
652
    case TDMT_VND_CREATE_TABLE:
×
653
      if (vnodeProcessCreateTbReq(pVnode, ver, pReq, len, pRsp, pMsg) < 0) goto _err;
×
654
      break;
×
655
    case TDMT_VND_ALTER_TABLE:
×
656
      if (vnodeProcessAlterTbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
657
      break;
×
658
    case TDMT_VND_DROP_TABLE:
×
659
      if (vnodeProcessDropTbReq(pVnode, ver, pReq, len, pRsp, pMsg) < 0) goto _err;
×
660
      break;
×
661
    case TDMT_VND_DROP_TTL_TABLE:
×
662
      if (vnodeProcessDropTtlTbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
663
      break;
×
664
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
×
665
      if (vnodeProcessFetchTtlExpiredTbs(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
666
      break;
×
667
    case TDMT_VND_TRIM:
×
668
      if (vnodeProcessTrimReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
669
      break;
×
670
    case TDMT_VND_S3MIGRATE:
×
671
      if (vnodeProcessS3MigrateReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
672
      break;
×
673
    case TDMT_VND_CREATE_SMA:
×
674
      if (vnodeProcessCreateTSmaReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
675
      break;
×
676
    /* TSDB */
677
    case TDMT_VND_SUBMIT:
×
678
      if (vnodeProcessSubmitReq(pVnode, ver, pMsg->pCont, pMsg->contLen, pRsp, pMsg) < 0) goto _err;
×
679
      break;
×
680
    case TDMT_VND_DELETE:
×
681
      if (vnodeProcessDeleteReq(pVnode, ver, pReq, len, pRsp, pMsg) < 0) goto _err;
×
682
      break;
×
683
    case TDMT_VND_BATCH_DEL:
×
684
      if (vnodeProcessBatchDeleteReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
685
      break;
×
686
    /* TQ */
687
    case TDMT_VND_TMQ_SUBSCRIBE:
×
688
      if (tqProcessSubscribeReq(pVnode->pTq, ver, pReq, len) < 0) {
×
689
        goto _err;
×
690
      }
691
      break;
×
692
    case TDMT_VND_TMQ_DELETE_SUB:
×
693
      if (tqProcessDeleteSubReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen) < 0) {
×
694
        goto _err;
×
695
      }
696
      break;
×
697
    case TDMT_VND_TMQ_COMMIT_OFFSET:
×
698
      if (tqProcessOffsetCommitReq(pVnode->pTq, ver, pReq, len) < 0) {
×
699
        goto _err;
×
700
      }
701
      break;
×
702
    case TDMT_VND_TMQ_ADD_CHECKINFO:
×
703
      if (tqProcessAddCheckInfoReq(pVnode->pTq, ver, pReq, len) < 0) {
×
704
        goto _err;
×
705
      }
706
      break;
×
707
    case TDMT_VND_TMQ_DEL_CHECKINFO:
×
708
      if (tqProcessDelCheckInfoReq(pVnode->pTq, ver, pReq, len) < 0) {
×
709
        goto _err;
×
710
      }
711
      break;
×
712
    case TDMT_STREAM_TASK_DEPLOY: {
×
713
      if ((code = tqProcessTaskDeployReq(pVnode->pTq, ver, pReq, len)) != TSDB_CODE_SUCCESS) {
×
714
        goto _err;
×
715
      }
716
    } break;
×
717
    case TDMT_STREAM_TASK_DROP: {
×
718
      if ((code = tqProcessTaskDropReq(pVnode->pTq, pMsg->pCont, pMsg->contLen)) < 0) {
×
719
        goto _err;
×
720
      }
721
    } break;
×
722
    case TDMT_STREAM_TASK_UPDATE_CHKPT: {
×
723
      if ((code = tqProcessTaskUpdateCheckpointReq(pVnode->pTq, pMsg->pCont, pMsg->contLen)) < 0) {
×
724
        goto _err;
×
725
      }
726
    } break;
×
727
    case TDMT_STREAM_CONSEN_CHKPT: {
×
728
      if (pVnode->restored && (code = tqProcessTaskConsenChkptIdReq(pVnode->pTq, pMsg)) < 0) {
×
729
        goto _err;
×
730
      }
731

732
    } break;
×
733
    case TDMT_STREAM_TASK_PAUSE: {
×
734
      if (pVnode->restored && vnodeIsLeader(pVnode) &&
×
735
          (code = tqProcessTaskPauseReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen)) < 0) {
×
736
        goto _err;
×
737
      }
738
    } break;
×
739
    case TDMT_STREAM_TASK_RESUME: {
×
740
      if (pVnode->restored && vnodeIsLeader(pVnode) &&
×
741
          (code = tqProcessTaskResumeReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen)) < 0) {
×
742
        goto _err;
×
743
      }
744
    } break;
×
745
    case TDMT_VND_STREAM_TASK_RESET: {
×
746
      if (pVnode->restored && vnodeIsLeader(pVnode) && (code = tqProcessTaskResetReq(pVnode->pTq, pMsg)) < 0) {
×
747
        goto _err;
×
748
      }
749

750
    } break;
×
751
    case TDMT_VND_ALTER_CONFIRM:
×
752
      needCommit = pVnode->config.hashChange;
×
753
      if (vnodeProcessAlterConfirmReq(pVnode, ver, pReq, len, pRsp) < 0) {
×
754
        goto _err;
×
755
      }
756
      break;
×
757
    case TDMT_VND_ALTER_CONFIG:
×
758
      vnodeProcessAlterConfigReq(pVnode, ver, pReq, len, pRsp);
×
759
      break;
×
760
    case TDMT_VND_COMMIT:
×
761
      needCommit = true;
×
762
      break;
×
763
    case TDMT_VND_CREATE_INDEX:
×
764
      vnodeProcessCreateIndexReq(pVnode, ver, pReq, len, pRsp);
×
765
      break;
×
766
    case TDMT_VND_DROP_INDEX:
×
767
      vnodeProcessDropIndexReq(pVnode, ver, pReq, len, pRsp);
×
768
      break;
×
769
    case TDMT_VND_STREAM_CHECK_POINT_SOURCE:  // always return true
×
770
      tqProcessTaskCheckPointSourceReq(pVnode->pTq, pMsg, pRsp);
×
771
      break;
×
772
    case TDMT_VND_STREAM_TASK_UPDATE:  // always return true
×
773
      tqProcessTaskUpdateReq(pVnode->pTq, pMsg);
×
774
      break;
×
775
    case TDMT_VND_COMPACT:
×
776
      vnodeProcessCompactVnodeReq(pVnode, ver, pReq, len, pRsp);
×
777
      goto _exit;
×
778
    case TDMT_SYNC_CONFIG_CHANGE:
×
779
      vnodeProcessConfigChangeReq(pVnode, ver, pReq, len, pRsp);
×
780
      break;
×
781
#ifdef TD_ENTERPRISE
782
    case TDMT_VND_KILL_COMPACT:
×
783
      vnodeProcessKillCompactReq(pVnode, ver, pReq, len, pRsp);
×
784
      break;
×
785
#endif
786
    /* ARB */
787
    case TDMT_VND_ARB_CHECK_SYNC:
×
788
      vnodeProcessArbCheckSyncReq(pVnode, pReq, len, pRsp);
×
789
      break;
×
790
    default:
×
791
      vError("vgId:%d, unprocessed msg, %d", TD_VID(pVnode), pMsg->msgType);
×
792
      return TSDB_CODE_INVALID_MSG;
×
793
  }
794

795
  vTrace("vgId:%d, process %s request, code:0x%x index:%" PRId64, TD_VID(pVnode), TMSG_INFO(pMsg->msgType), pRsp->code,
×
796
         ver);
797

798
  walApplyVer(pVnode->pWal, ver);
×
799

800
  code = tqPushMsg(pVnode->pTq, pMsg->msgType);
×
801
  if (code) {
×
802
    vError("vgId:%d, failed to push msg to TQ since %s", TD_VID(pVnode), tstrerror(terrno));
×
803
    return code;
×
804
  }
805

806
  // commit if need
807
  if (needCommit) {
×
808
    vInfo("vgId:%d, commit at version %" PRId64, TD_VID(pVnode), ver);
×
809
    code = vnodeAsyncCommit(pVnode);
×
810
    if (code) {
×
811
      vError("vgId:%d, failed to vnode async commit since %s.", TD_VID(pVnode), tstrerror(terrno));
×
812
      goto _err;
×
813
    }
814

815
    // start a new one
816
    code = vnodeBegin(pVnode);
×
817
    if (code) {
×
818
      vError("vgId:%d, failed to begin vnode since %s.", TD_VID(pVnode), tstrerror(terrno));
×
819
      goto _err;
×
820
    }
821
  }
822

823
_exit:
×
824
  return 0;
×
825

826
_err:
×
827
  vError("vgId:%d, process %s request failed since %s, ver:%" PRId64, TD_VID(pVnode), TMSG_INFO(pMsg->msgType),
×
828
         tstrerror(terrno), ver);
829
  return code;
×
830
}
831

832
int32_t vnodePreprocessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg) {
×
833
  if (TDMT_SCH_QUERY != pMsg->msgType && TDMT_SCH_MERGE_QUERY != pMsg->msgType) {
×
834
    return 0;
×
835
  }
836

837
  return qWorkerPreprocessQueryMsg(pVnode->pQuery, pMsg, TDMT_SCH_QUERY == pMsg->msgType);
×
838
}
839

840
int32_t vnodeProcessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
×
841
  vTrace("message in vnode query queue is processing");
×
842
  if (pMsg->msgType == TDMT_VND_TMQ_CONSUME && !syncIsReadyForRead(pVnode->sync)) {
×
843
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
×
844
    return 0;
×
845
  }
846

847
  if (pMsg->msgType == TDMT_VND_TMQ_CONSUME && !pVnode->restored) {
×
848
    vnodeRedirectRpcMsg(pVnode, pMsg, TSDB_CODE_SYN_RESTORING);
×
849
    return 0;
×
850
  }
851

852
  SReadHandle handle = {.vnode = pVnode, .pMsgCb = &pVnode->msgCb, .pWorkerCb = pInfo->workerCb};
×
853
  initStorageAPI(&handle.api);
×
854
  int32_t code = TSDB_CODE_SUCCESS;
×
855
  bool    redirected = false;
×
856

857
  switch (pMsg->msgType) {
×
858
    case TDMT_SCH_QUERY:
×
859
      if (!syncIsReadyForRead(pVnode->sync)) {
×
860
        pMsg->code = (terrno) ? terrno : TSDB_CODE_SYN_NOT_LEADER;
×
861
        redirected = true;
×
862
      }
863
      code = qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
×
864
      if (redirected) {
×
865
        vnodeRedirectRpcMsg(pVnode, pMsg, pMsg->code);
×
866
        return 0;
×
867
      }
868
      return code;
×
869
    case TDMT_SCH_MERGE_QUERY:
×
870
      return qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
×
871
    case TDMT_SCH_QUERY_CONTINUE:
×
872
      return qWorkerProcessCQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
×
873
    case TDMT_VND_TMQ_CONSUME:
×
874
      return tqProcessPollReq(pVnode->pTq, pMsg);
×
875
    case TDMT_VND_TMQ_CONSUME_PUSH:
×
876
      return tqProcessPollPush(pVnode->pTq, pMsg);
×
877
    default:
×
878
      vError("unknown msg type:%d in query queue", pMsg->msgType);
×
879
      return TSDB_CODE_APP_ERROR;
×
880
  }
881
}
882

883
int32_t vnodeProcessFetchMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
×
884
  vTrace("vgId:%d, msg:%p in fetch queue is processing", pVnode->config.vgId, pMsg);
×
885
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
×
886
       pMsg->msgType == TDMT_VND_BATCH_META || pMsg->msgType == TDMT_VND_TABLE_NAME) &&
×
887
      !syncIsReadyForRead(pVnode->sync)) {
×
888
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
×
889
    return 0;
×
890
  }
891

892
  switch (pMsg->msgType) {
×
893
    case TDMT_SCH_FETCH:
×
894
    case TDMT_SCH_MERGE_FETCH:
895
      return qWorkerProcessFetchMsg(pVnode, pVnode->pQuery, pMsg, 0);
×
896
    case TDMT_SCH_FETCH_RSP:
×
897
      return qWorkerProcessRspMsg(pVnode, pVnode->pQuery, pMsg, 0);
×
898
    // case TDMT_SCH_CANCEL_TASK:
899
    //   return qWorkerProcessCancelMsg(pVnode, pVnode->pQuery, pMsg, 0);
900
    case TDMT_SCH_DROP_TASK:
×
901
      return qWorkerProcessDropMsg(pVnode, pVnode->pQuery, pMsg, 0);
×
902
    case TDMT_SCH_TASK_NOTIFY:
×
903
      return qWorkerProcessNotifyMsg(pVnode, pVnode->pQuery, pMsg, 0);
×
904
    case TDMT_SCH_QUERY_HEARTBEAT:
×
905
      return qWorkerProcessHbMsg(pVnode, pVnode->pQuery, pMsg, 0);
×
906
    case TDMT_VND_TABLE_META:
×
907
    case TDMT_VND_TABLE_NAME:
908
      return vnodeGetTableMeta(pVnode, pMsg, true);
×
909
    case TDMT_VND_TABLE_CFG:
×
910
      return vnodeGetTableCfg(pVnode, pMsg, true);
×
911
    case TDMT_VND_BATCH_META:
×
912
      return vnodeGetBatchMeta(pVnode, pMsg);
×
913
#ifdef TD_ENTERPRISE
914
    case TDMT_VND_QUERY_COMPACT_PROGRESS:
×
915
      return vnodeQueryCompactProgress(pVnode, pMsg);
×
916
#endif
917
      //    case TDMT_VND_TMQ_CONSUME:
918
      //      return tqProcessPollReq(pVnode->pTq, pMsg);
919
    case TDMT_VND_TMQ_VG_WALINFO:
×
920
      return tqProcessVgWalInfoReq(pVnode->pTq, pMsg);
×
921
    case TDMT_VND_TMQ_VG_COMMITTEDINFO:
×
922
      return tqProcessVgCommittedInfoReq(pVnode->pTq, pMsg);
×
923
    case TDMT_VND_TMQ_SEEK:
×
924
      return tqProcessSeekReq(pVnode->pTq, pMsg);
×
925

926
    default:
×
927
      vError("unknown msg type:%d in fetch queue", pMsg->msgType);
×
928
      return TSDB_CODE_APP_ERROR;
×
929
  }
930
}
931

932
int32_t vnodeProcessStreamMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
×
933
  vTrace("vgId:%d, msg:%p in stream queue is processing", pVnode->config.vgId, pMsg);
×
934
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
×
935
       pMsg->msgType == TDMT_VND_BATCH_META) &&
×
936
      !syncIsReadyForRead(pVnode->sync)) {
×
937
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
×
938
    return 0;
×
939
  }
940

941
  switch (pMsg->msgType) {
×
942
    case TDMT_STREAM_TASK_RUN:
×
943
      return tqProcessTaskRunReq(pVnode->pTq, pMsg);
×
944
    case TDMT_STREAM_RETRIEVE:
×
945
      return tqProcessTaskRetrieveReq(pVnode->pTq, pMsg);
×
946
    case TDMT_STREAM_RETRIEVE_RSP:
×
947
      return tqProcessTaskRetrieveRsp(pVnode->pTq, pMsg);
×
948
    case TDMT_VND_STREAM_SCAN_HISTORY:
×
949
      return tqProcessTaskScanHistory(pVnode->pTq, pMsg);
×
950
    case TDMT_STREAM_TASK_CHECKPOINT_READY:
×
951
      return tqProcessTaskCheckpointReadyMsg(pVnode->pTq, pMsg);
×
952
    case TDMT_STREAM_TASK_CHECKPOINT_READY_RSP:
×
953
      return tqProcessTaskCheckpointReadyRsp(pVnode->pTq, pMsg);
×
954
    case TDMT_STREAM_RETRIEVE_TRIGGER:
×
955
      return tqProcessTaskRetrieveTriggerReq(pVnode->pTq, pMsg);
×
956
    case TDMT_STREAM_RETRIEVE_TRIGGER_RSP:
×
957
      return tqProcessTaskRetrieveTriggerRsp(pVnode->pTq, pMsg);
×
958
    case TDMT_MND_STREAM_REQ_CHKPT_RSP:
×
959
      return tqProcessStreamReqCheckpointRsp(pVnode->pTq, pMsg);
×
960
    case TDMT_VND_GET_STREAM_PROGRESS:
×
961
      return tqStreamProgressRetrieveReq(pVnode->pTq, pMsg);
×
962
    case TDMT_MND_STREAM_CHKPT_REPORT_RSP:
×
963
      return tqProcessTaskChkptReportRsp(pVnode->pTq, pMsg);
×
964
    default:
×
965
      vError("unknown msg type:%d in stream queue", pMsg->msgType);
×
966
      return TSDB_CODE_APP_ERROR;
×
967
  }
968
}
969

970
int32_t vnodeProcessStreamCtrlMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
×
971
  vTrace("vgId:%d, msg:%p in stream ctrl queue is processing", pVnode->config.vgId, pMsg);
×
972
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
×
973
       pMsg->msgType == TDMT_VND_BATCH_META) &&
×
974
      !syncIsReadyForRead(pVnode->sync)) {
×
975
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
×
976
    return 0;
×
977
  }
978

979
  switch (pMsg->msgType) {
×
980
    case TDMT_MND_STREAM_HEARTBEAT_RSP:
×
981
      return tqProcessStreamHbRsp(pVnode->pTq, pMsg);
×
982
    case TDMT_STREAM_TASK_DISPATCH:
×
983
      return tqProcessTaskDispatchReq(pVnode->pTq, pMsg);
×
984
    case TDMT_STREAM_TASK_DISPATCH_RSP:
×
985
      return tqProcessTaskDispatchRsp(pVnode->pTq, pMsg);
×
986
    case TDMT_VND_STREAM_TASK_CHECK:
×
987
      return tqProcessTaskCheckReq(pVnode->pTq, pMsg);
×
988
    case TDMT_VND_STREAM_TASK_CHECK_RSP:
×
989
      return tqProcessTaskCheckRsp(pVnode->pTq, pMsg);
×
990
    default:
×
991
      vError("unknown msg type:%d in stream ctrl queue", pMsg->msgType);
×
992
      return TSDB_CODE_APP_ERROR;
×
993
  }
994
}
995

996
void smaHandleRes(void *pVnode, int64_t smaId, const SArray *data) {
×
997
  int32_t code = tdProcessTSmaInsert(((SVnode *)pVnode)->pSma, smaId, (const char *)data);
×
998
  if (code) {
×
999
    vError("failed to process sma result since %s", tstrerror(code));
×
1000
  }
1001
}
×
1002

1003
void vnodeUpdateMetaRsp(SVnode *pVnode, STableMetaRsp *pMetaRsp) {
×
1004
  if (NULL == pMetaRsp) {
×
1005
    return;
×
1006
  }
1007

1008
  tstrncpy(pMetaRsp->dbFName, pVnode->config.dbname, TSDB_DB_FNAME_LEN);
×
1009
  pMetaRsp->dbId = pVnode->config.dbId;
×
1010
  pMetaRsp->vgId = TD_VID(pVnode);
×
1011
  pMetaRsp->precision = pVnode->config.tsdbCfg.precision;
×
1012
}
1013

1014
extern int32_t vnodeAsyncRetention(SVnode *pVnode, int64_t now);
1015

1016
static int32_t vnodeProcessTrimReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1017
  if (!pVnode->restored) {
×
1018
    vInfo("vgId:%d, ignore trim req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
×
1019
    return 0;
×
1020
  }
1021

1022
  int32_t     code = 0;
×
1023
  SVTrimDbReq trimReq = {0};
×
1024

1025
  // decode
1026
  if (tDeserializeSVTrimDbReq(pReq, len, &trimReq) != 0) {
×
1027
    code = TSDB_CODE_INVALID_MSG;
×
1028
    goto _exit;
×
1029
  }
1030

1031
  vInfo("vgId:%d, trim vnode request will be processed, time:%d", pVnode->config.vgId, trimReq.timestamp);
×
1032

1033
  code = vnodeAsyncRetention(pVnode, trimReq.timestamp);
×
1034

1035
_exit:
×
1036
  return code;
×
1037
}
1038

1039
extern int32_t vnodeAsyncS3Migrate(SVnode *pVnode, int64_t now);
1040

1041
static int32_t vnodeProcessS3MigrateReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1042
  int32_t          code = 0;
×
1043
  SVS3MigrateDbReq s3migrateReq = {0};
×
1044

1045
  // decode
1046
  if (tDeserializeSVS3MigrateDbReq(pReq, len, &s3migrateReq) != 0) {
×
1047
    code = TSDB_CODE_INVALID_MSG;
×
1048
    goto _exit;
×
1049
  }
1050

1051
  vInfo("vgId:%d, s3migrate vnode request will be processed, time:%d", pVnode->config.vgId, s3migrateReq.timestamp);
×
1052

1053
  code = vnodeAsyncS3Migrate(pVnode, s3migrateReq.timestamp);
×
1054

1055
_exit:
×
1056
  return code;
×
1057
}
1058

1059
static int32_t vnodeProcessDropTtlTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1060
  int               ret = 0;
×
1061
  SVDropTtlTableReq ttlReq = {0};
×
1062
  if (tDeserializeSVDropTtlTableReq(pReq, len, &ttlReq) != 0) {
×
1063
    ret = TSDB_CODE_INVALID_MSG;
×
1064
    goto end;
×
1065
  }
1066

1067
  if (ttlReq.nUids != taosArrayGetSize(ttlReq.pTbUids)) {
×
1068
    ret = TSDB_CODE_INVALID_MSG;
×
1069
    goto end;
×
1070
  }
1071

1072
  if (ttlReq.nUids != 0) {
×
1073
    vInfo("vgId:%d, drop ttl table req will be processed, time:%d, ntbUids:%d", pVnode->config.vgId,
×
1074
          ttlReq.timestampSec, ttlReq.nUids);
1075
  }
1076

1077
  if (ttlReq.nUids > 0) {
×
1078
    int32_t code = metaDropMultipleTables(pVnode->pMeta, ver, ttlReq.pTbUids);
×
1079
    if (code) return code;
×
1080

1081
    code = tqUpdateTbUidList(pVnode->pTq, ttlReq.pTbUids, false);
×
1082
    if (code) {
×
1083
      vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1084
    }
1085
  }
1086

1087
end:
×
1088
  taosArrayDestroy(ttlReq.pTbUids);
×
1089
  return ret;
×
1090
}
1091

1092
static int32_t vnodeProcessFetchTtlExpiredTbs(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1093
  int32_t                 code = -1;
×
1094
  SMetaReader             mr = {0};
×
1095
  SVDropTtlTableReq       ttlReq = {0};
×
1096
  SVFetchTtlExpiredTbsRsp rsp = {0};
×
1097
  SEncoder                encoder = {0};
×
1098
  SArray                 *pNames = NULL;
×
1099
  pRsp->msgType = TDMT_VND_FETCH_TTL_EXPIRED_TBS_RSP;
×
1100
  pRsp->code = TSDB_CODE_SUCCESS;
×
1101
  pRsp->pCont = NULL;
×
1102
  pRsp->contLen = 0;
×
1103

1104
  if (tDeserializeSVDropTtlTableReq(pReq, len, &ttlReq) != 0) {
×
1105
    terrno = TSDB_CODE_INVALID_MSG;
×
1106
    goto _end;
×
1107
  }
1108

1109
  if (!(ttlReq.nUids == taosArrayGetSize(ttlReq.pTbUids))) {
×
1110
    terrno = TSDB_CODE_INVALID_MSG;
×
1111
    goto _end;
×
1112
  }
1113

1114
  tb_uid_t    suid;
1115
  char        ctbName[TSDB_TABLE_NAME_LEN];
1116
  SVDropTbReq expiredTb = {.igNotExists = true};
×
1117
  metaReaderDoInit(&mr, pVnode->pMeta, 0);
×
1118
  rsp.vgId = TD_VID(pVnode);
×
1119
  rsp.pExpiredTbs = taosArrayInit(ttlReq.nUids, sizeof(SVDropTbReq));
×
1120
  if (!rsp.pExpiredTbs) goto _end;
×
1121

1122
  pNames = taosArrayInit(ttlReq.nUids, TSDB_TABLE_NAME_LEN);
×
1123
  if (!pNames) {
×
1124
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1125
    goto _end;
×
1126
  }
1127
  char buf[TSDB_TABLE_NAME_LEN];
1128
  for (int32_t i = 0; i < ttlReq.nUids; ++i) {
×
1129
    tb_uid_t *uid = taosArrayGet(ttlReq.pTbUids, i);
×
1130
    expiredTb.suid = *uid;
×
1131
    terrno = metaReaderGetTableEntryByUid(&mr, *uid);
×
1132
    if (terrno < 0) goto _end;
×
1133
    tstrncpy(buf, mr.me.name, TSDB_TABLE_NAME_LEN);
×
1134
    void *p = taosArrayPush(pNames, buf);
×
1135
    if (p == NULL) {
×
1136
      goto _end;
×
1137
    }
1138

1139
    expiredTb.name = p;
×
1140
    if (mr.me.type == TSDB_CHILD_TABLE) {
×
1141
      expiredTb.suid = mr.me.ctbEntry.suid;
×
1142
    }
1143

1144
    if (taosArrayPush(rsp.pExpiredTbs, &expiredTb) == NULL) {
×
1145
      goto _end;
×
1146
    }
1147
  }
1148

1149
  int32_t ret = 0;
×
1150
  tEncodeSize(tEncodeVFetchTtlExpiredTbsRsp, &rsp, pRsp->contLen, ret);
×
1151
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
×
1152
  if (pRsp->pCont == NULL) {
×
1153
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1154
    code = -1;
×
1155
    goto _end;
×
1156
  }
1157
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
×
1158
  terrno = tEncodeVFetchTtlExpiredTbsRsp(&encoder, &rsp);
×
1159
  tEncoderClear(&encoder);
×
1160

1161
  if (terrno == 0) code = 0;
×
1162
_end:
×
1163
  metaReaderClear(&mr);
×
1164
  tFreeFetchTtlExpiredTbsRsp(&rsp);
×
1165
  taosArrayDestroy(ttlReq.pTbUids);
×
1166
  if (pNames) taosArrayDestroy(pNames);
×
1167
  pRsp->code = terrno;
×
1168
  return code;
×
1169
}
1170

1171
static int32_t vnodeProcessCreateStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1172
  int32_t        code = 0;
×
1173
  SVCreateStbReq req = {0};
×
1174
  SDecoder       coder;
1175

1176
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
×
1177
  pRsp->code = TSDB_CODE_SUCCESS;
×
1178
  pRsp->pCont = NULL;
×
1179
  pRsp->contLen = 0;
×
1180

1181
  // decode and process req
1182
  tDecoderInit(&coder, pReq, len);
×
1183

1184
  code = tDecodeSVCreateStbReq(&coder, &req);
×
1185
  if (code) {
×
1186
    pRsp->code = code;
×
1187
    goto _err;
×
1188
  }
1189

1190
  code = metaCreateSuperTable(pVnode->pMeta, ver, &req);
×
1191
  if (code) {
×
1192
    pRsp->code = code;
×
1193
    goto _err;
×
1194
  }
1195

1196
  if ((code = tdProcessRSmaCreate(pVnode->pSma, &req)) < 0) {
×
1197
    pRsp->code = code;
×
1198
    goto _err;
×
1199
  }
1200

1201
  tDecoderClear(&coder);
×
1202
  return 0;
×
1203

1204
_err:
×
1205
  tDecoderClear(&coder);
×
1206
  return code;
×
1207
}
1208

1209
static int32_t vnodeProcessCreateTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
×
1210
                                       SRpcMsg *pOriginRpc) {
1211
  SDecoder           decoder = {0};
×
1212
  SEncoder           encoder = {0};
×
1213
  int32_t            rcode = 0;
×
1214
  SVCreateTbBatchReq req = {0};
×
1215
  SVCreateTbReq     *pCreateReq;
1216
  SVCreateTbBatchRsp rsp = {0};
×
1217
  SVCreateTbRsp      cRsp = {0};
×
1218
  char               tbName[TSDB_TABLE_FNAME_LEN];
1219
  STbUidStore       *pStore = NULL;
×
1220
  SArray            *tbUids = NULL;
×
1221
  SArray            *tbNames = NULL;
×
1222
  pRsp->msgType = TDMT_VND_CREATE_TABLE_RSP;
×
1223
  pRsp->code = TSDB_CODE_SUCCESS;
×
1224
  pRsp->pCont = NULL;
×
1225
  pRsp->contLen = 0;
×
1226

1227
  // decode
1228
  tDecoderInit(&decoder, pReq, len);
×
1229
  if (tDecodeSVCreateTbBatchReq(&decoder, &req) < 0) {
×
1230
    rcode = -1;
×
1231
    terrno = TSDB_CODE_INVALID_MSG;
×
1232
    goto _exit;
×
1233
  }
1234

1235
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(cRsp));
×
1236
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
×
1237
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
×
1238
  if (rsp.pArray == NULL || tbUids == NULL || tbNames == NULL) {
×
1239
    rcode = -1;
×
1240
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1241
    goto _exit;
×
1242
  }
1243

1244
  // loop to create table
1245
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
×
1246
    pCreateReq = req.pReqs + iReq;
×
1247
    memset(&cRsp, 0, sizeof(cRsp));
×
1248

1249
    if (tsEnableAudit && tsEnableAuditCreateTable) {
×
1250
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
×
1251
      if (str == NULL) {
×
1252
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1253
        rcode = -1;
×
1254
        goto _exit;
×
1255
      }
1256
      tstrncpy(str, pCreateReq->name, TSDB_TABLE_FNAME_LEN);
×
1257
      if (taosArrayPush(tbNames, &str) == NULL) {
×
1258
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1259
        rcode = -1;
×
1260
        goto _exit;
×
1261
      }
1262
    }
1263

1264
    // validate hash
1265
    (void)tsnprintf(tbName, TSDB_TABLE_FNAME_LEN, "%s.%s", pVnode->config.dbname, pCreateReq->name);
×
1266
    if (vnodeValidateTableHash(pVnode, tbName) < 0) {
×
1267
      cRsp.code = TSDB_CODE_VND_HASH_MISMATCH;
×
1268
      if (taosArrayPush(rsp.pArray, &cRsp) == NULL) {
×
1269
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1270
        rcode = -1;
×
1271
        goto _exit;
×
1272
      }
1273
      vError("vgId:%d create-table:%s failed due to hash value mismatch", TD_VID(pVnode), tbName);
×
1274
      continue;
×
1275
    }
1276

1277
    // do create table
1278
    if (metaCreateTable2(pVnode->pMeta, ver, pCreateReq, &cRsp.pMeta) < 0) {
×
1279
      if (pCreateReq->flags & TD_CREATE_IF_NOT_EXISTS && terrno == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
×
1280
        cRsp.code = TSDB_CODE_SUCCESS;
×
1281
      } else {
1282
        cRsp.code = terrno;
×
1283
      }
1284
    } else {
1285
      cRsp.code = TSDB_CODE_SUCCESS;
×
1286
      if (tdFetchTbUidList(pVnode->pSma, &pStore, pCreateReq->ctb.suid, pCreateReq->uid) < 0) {
×
1287
        vError("vgId:%d, failed to fetch tbUid list", TD_VID(pVnode));
×
1288
      }
1289
      if (taosArrayPush(tbUids, &pCreateReq->uid) == NULL) {
×
1290
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1291
        rcode = -1;
×
1292
        goto _exit;
×
1293
      }
1294
      vnodeUpdateMetaRsp(pVnode, cRsp.pMeta);
×
1295
    }
1296

1297
    if (taosArrayPush(rsp.pArray, &cRsp) == NULL) {
×
1298
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1299
      rcode = -1;
×
1300
      goto _exit;
×
1301
    }
1302
  }
1303

1304
  vDebug("vgId:%d, add %d new created tables into query table list", TD_VID(pVnode), (int32_t)taosArrayGetSize(tbUids));
×
1305
  if (tqUpdateTbUidList(pVnode->pTq, tbUids, true) < 0) {
×
1306
    vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(terrno));
×
1307
  }
1308
  if (tdUpdateTbUidList(pVnode->pSma, pStore, true) < 0) {
×
1309
    goto _exit;
×
1310
  }
1311
  pStore = tdUidStoreFree(pStore);
×
1312

1313
  // prepare rsp
1314
  int32_t ret = 0;
×
1315
  tEncodeSize(tEncodeSVCreateTbBatchRsp, &rsp, pRsp->contLen, ret);
×
1316
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
×
1317
  if (pRsp->pCont == NULL) {
×
1318
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1319
    rcode = -1;
×
1320
    goto _exit;
×
1321
  }
1322
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
×
1323
  if (tEncodeSVCreateTbBatchRsp(&encoder, &rsp) != 0) {
×
1324
    vError("vgId:%d, failed to encode create table batch response", TD_VID(pVnode));
×
1325
  }
1326

1327
  if (tsEnableAudit && tsEnableAuditCreateTable) {
×
1328
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
×
1329

1330
    SName name = {0};
×
1331
    if (tNameFromString(&name, pVnode->config.dbname, T_NAME_ACCT | T_NAME_DB) < 0) {
×
1332
      vError("vgId:%d, failed to get name from string", TD_VID(pVnode));
×
1333
    }
1334

1335
    SStringBuilder sb = {0};
×
1336
    for (int32_t i = 0; i < tbNames->size; i++) {
×
1337
      char **key = (char **)taosArrayGet(tbNames, i);
×
1338
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
×
1339
      if (i < tbNames->size - 1) {
×
1340
        taosStringBuilderAppendChar(&sb, ',');
×
1341
      }
1342
      // taosMemoryFreeClear(*key);
1343
    }
1344

1345
    size_t len = 0;
×
1346
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
×
1347

1348
    if (pOriginRpc->info.conn.user != NULL && strlen(pOriginRpc->info.conn.user) > 0) {
×
1349
      auditAddRecord(pOriginRpc, clusterId, "createTable", name.dbname, "", keyJoined, len);
×
1350
    }
1351

1352
    taosStringBuilderDestroy(&sb);
×
1353
  }
1354

1355
_exit:
×
1356
  tDeleteSVCreateTbBatchReq(&req);
×
1357
  taosArrayDestroyEx(rsp.pArray, tFreeSVCreateTbRsp);
×
1358
  taosArrayDestroy(tbUids);
×
1359
  tDecoderClear(&decoder);
×
1360
  tEncoderClear(&encoder);
×
1361
  taosArrayDestroyP(tbNames, NULL);
×
1362
  return rcode;
×
1363
}
1364

1365
static int32_t vnodeProcessAlterStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1366
  int32_t        code = 0;
×
1367
  SVCreateStbReq req = {0};
×
1368
  SDecoder       dc = {0};
×
1369

1370
  pRsp->msgType = TDMT_VND_ALTER_STB_RSP;
×
1371
  pRsp->code = TSDB_CODE_SUCCESS;
×
1372
  pRsp->pCont = NULL;
×
1373
  pRsp->contLen = 0;
×
1374

1375
  tDecoderInit(&dc, pReq, len);
×
1376

1377
  // decode req
1378
  code = tDecodeSVCreateStbReq(&dc, &req);
×
1379
  if (code) {
×
1380
    tDecoderClear(&dc);
×
1381
    return code;
×
1382
  }
1383

1384
  code = metaAlterSuperTable(pVnode->pMeta, ver, &req);
×
1385
  if (code) {
×
1386
    pRsp->code = code;
×
1387
    tDecoderClear(&dc);
×
1388
    return code;
×
1389
  }
1390

1391
  tDecoderClear(&dc);
×
1392

1393
  return 0;
×
1394
}
1395

1396
static int32_t vnodeProcessDropStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1397
  SVDropStbReq req = {0};
×
1398
  int32_t      rcode = TSDB_CODE_SUCCESS;
×
1399
  SDecoder     decoder = {0};
×
1400
  SArray      *tbUidList = NULL;
×
1401

1402
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
×
1403
  pRsp->pCont = NULL;
×
1404
  pRsp->contLen = 0;
×
1405

1406
  // decode request
1407
  tDecoderInit(&decoder, pReq, len);
×
1408
  if (tDecodeSVDropStbReq(&decoder, &req) < 0) {
×
1409
    rcode = TSDB_CODE_INVALID_MSG;
×
1410
    goto _exit;
×
1411
  }
1412

1413
  // process request
1414
  tbUidList = taosArrayInit(8, sizeof(int64_t));
×
1415
  if (tbUidList == NULL) goto _exit;
×
1416
  if (metaDropSuperTable(pVnode->pMeta, ver, &req) < 0) {
×
1417
    rcode = terrno;
×
1418
    goto _exit;
×
1419
  }
1420

1421
  if (tqUpdateTbUidList(pVnode->pTq, tbUidList, false) < 0) {
×
1422
    rcode = terrno;
×
1423
    goto _exit;
×
1424
  }
1425

1426
  if (tdProcessRSmaDrop(pVnode->pSma, &req) < 0) {
×
1427
    rcode = terrno;
×
1428
    goto _exit;
×
1429
  }
1430

1431
  // return rsp
1432
_exit:
×
1433
  if (tbUidList) taosArrayDestroy(tbUidList);
×
1434
  pRsp->code = rcode;
×
1435
  tDecoderClear(&decoder);
×
1436
  return 0;
×
1437
}
1438

1439
static int32_t vnodeProcessAlterTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1440
  SVAlterTbReq  vAlterTbReq = {0};
×
1441
  SVAlterTbRsp  vAlterTbRsp = {0};
×
1442
  SDecoder      dc = {0};
×
1443
  int32_t       code = 0;
×
1444
  int32_t       lino = 0;
×
1445
  int32_t       ret;
1446
  SEncoder      ec = {0};
×
1447
  STableMetaRsp vMetaRsp = {0};
×
1448

1449
  pRsp->msgType = TDMT_VND_ALTER_TABLE_RSP;
×
1450
  pRsp->pCont = NULL;
×
1451
  pRsp->contLen = 0;
×
1452
  pRsp->code = TSDB_CODE_SUCCESS;
×
1453

1454
  tDecoderInit(&dc, pReq, len);
×
1455

1456
  // decode
1457
  if (tDecodeSVAlterTbReq(&dc, &vAlterTbReq) < 0) {
×
1458
    vAlterTbRsp.code = TSDB_CODE_INVALID_MSG;
×
1459
    tDecoderClear(&dc);
×
1460
    goto _exit;
×
1461
  }
1462

1463
  // process
1464
  if (metaAlterTable(pVnode->pMeta, ver, &vAlterTbReq, &vMetaRsp) < 0) {
×
1465
    vAlterTbRsp.code = terrno;
×
1466
    tDecoderClear(&dc);
×
1467
    goto _exit;
×
1468
  }
1469
  tDecoderClear(&dc);
×
1470

1471
  if (NULL != vMetaRsp.pSchemas) {
×
1472
    vnodeUpdateMetaRsp(pVnode, &vMetaRsp);
×
1473
    vAlterTbRsp.pMeta = &vMetaRsp;
×
1474
  }
1475

1476
  if (vAlterTbReq.action == TSDB_ALTER_TABLE_UPDATE_TAG_VAL || vAlterTbReq.action == TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL) {
×
1477
    int64_t uid = metaGetTableEntryUidByName(pVnode->pMeta, vAlterTbReq.tbName);
×
1478
    if (uid == 0) {
×
1479
      vError("vgId:%d, %s failed at %s:%d since table %s not found", TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
1480
             vAlterTbReq.tbName);
1481
      goto _exit;
×
1482
    }
1483

1484
    SArray* tbUids = taosArrayInit(4, sizeof(int64_t));
×
1485
    void* p = taosArrayPush(tbUids, &uid);
×
1486
    TSDB_CHECK_NULL(p, code, lino, _exit, terrno);
×
1487

1488
    vDebug("vgId:%d, remove tags value altered table:%s from query table list", TD_VID(pVnode), vAlterTbReq.tbName);
×
1489
    if ((code = tqUpdateTbUidList(pVnode->pTq, tbUids, false)) < 0) {
×
1490
      vError("vgId:%d, failed to remove tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1491
    }
1492

1493
    vDebug("vgId:%d, try to add table:%s in query table list", TD_VID(pVnode), vAlterTbReq.tbName);
×
1494
    if ((code = tqUpdateTbUidList(pVnode->pTq, tbUids, true)) < 0) {
×
1495
      vError("vgId:%d, failed to add tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1496
    }
1497

1498
    taosArrayDestroy(tbUids);
×
1499
  }
1500

1501
_exit:
×
1502
  taosArrayDestroy(vAlterTbReq.pMultiTag);
×
1503
  tEncodeSize(tEncodeSVAlterTbRsp, &vAlterTbRsp, pRsp->contLen, ret);
×
1504
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
×
1505
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
×
1506
  if (tEncodeSVAlterTbRsp(&ec, &vAlterTbRsp) != 0) {
×
1507
    vError("vgId:%d, failed to encode alter table response", TD_VID(pVnode));
×
1508
  }
1509

1510
  tEncoderClear(&ec);
×
1511
  if (vMetaRsp.pSchemas) {
×
1512
    taosMemoryFree(vMetaRsp.pSchemas);
×
1513
    taosMemoryFree(vMetaRsp.pSchemaExt);
×
1514
  }
1515
  return 0;
×
1516
}
1517

1518
static int32_t vnodeProcessDropTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
×
1519
                                     SRpcMsg *pOriginRpc) {
1520
  SVDropTbBatchReq req = {0};
×
1521
  SVDropTbBatchRsp rsp = {0};
×
1522
  SDecoder         decoder = {0};
×
1523
  SEncoder         encoder = {0};
×
1524
  int32_t          ret;
1525
  SArray          *tbUids = NULL;
×
1526
  STbUidStore     *pStore = NULL;
×
1527
  SArray          *tbNames = NULL;
×
1528

1529
  pRsp->msgType = TDMT_VND_DROP_TABLE_RSP;
×
1530
  pRsp->pCont = NULL;
×
1531
  pRsp->contLen = 0;
×
1532
  pRsp->code = TSDB_CODE_SUCCESS;
×
1533

1534
  // decode req
1535
  tDecoderInit(&decoder, pReq, len);
×
1536
  ret = tDecodeSVDropTbBatchReq(&decoder, &req);
×
1537
  if (ret < 0) {
×
1538
    terrno = TSDB_CODE_INVALID_MSG;
×
1539
    pRsp->code = terrno;
×
1540
    goto _exit;
×
1541
  }
1542

1543
  // process req
1544
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
×
1545
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(SVDropTbRsp));
×
1546
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
×
1547
  if (tbUids == NULL || rsp.pArray == NULL || tbNames == NULL) goto _exit;
×
1548

1549
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
×
1550
    SVDropTbReq *pDropTbReq = req.pReqs + iReq;
×
1551
    SVDropTbRsp  dropTbRsp = {0};
×
1552
    tb_uid_t     tbUid = 0;
×
1553

1554
    /* code */
1555
    ret = metaDropTable2(pVnode->pMeta, ver, pDropTbReq);
×
1556
    if (ret < 0) {
×
1557
      if (pDropTbReq->igNotExists && terrno == TSDB_CODE_TDB_TABLE_NOT_EXIST) {
×
1558
        dropTbRsp.code = TSDB_CODE_SUCCESS;
×
1559
      } else {
1560
        dropTbRsp.code = terrno;
×
1561
      }
1562
    } else {
1563
      dropTbRsp.code = TSDB_CODE_SUCCESS;
×
1564
      if (tbUid > 0) {
×
1565
        if (tdFetchTbUidList(pVnode->pSma, &pStore, pDropTbReq->suid, tbUid) < 0) {
×
1566
          vError("vgId:%d, failed to fetch tbUid list", TD_VID(pVnode));
×
1567
        }
1568
      }
1569
    }
1570

1571
    if (taosArrayPush(rsp.pArray, &dropTbRsp) == NULL) {
×
1572
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1573
      pRsp->code = terrno;
×
1574
      goto _exit;
×
1575
    }
1576

1577
    if (tsEnableAuditCreateTable) {
×
1578
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
×
1579
      if (str == NULL) {
×
1580
        pRsp->code = terrno;
×
1581
        goto _exit;
×
1582
      }
1583
      tstrncpy(str, pDropTbReq->name, TSDB_TABLE_FNAME_LEN);
×
1584
      if (taosArrayPush(tbNames, &str) == NULL) {
×
1585
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1586
        pRsp->code = terrno;
×
1587
        goto _exit;
×
1588
      }
1589
    }
1590
  }
1591

1592
  if (tqUpdateTbUidList(pVnode->pTq, tbUids, false) < 0) {
×
1593
    vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(terrno));
×
1594
  }
1595

1596
  if (tdUpdateTbUidList(pVnode->pSma, pStore, false) < 0) {
×
1597
    goto _exit;
×
1598
  }
1599

1600
  if (tsEnableAuditCreateTable) {
×
1601
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
×
1602

1603
    SName name = {0};
×
1604
    if (tNameFromString(&name, pVnode->config.dbname, T_NAME_ACCT | T_NAME_DB) != 0) {
×
1605
      vError("vgId:%d, failed to get name from string", TD_VID(pVnode));
×
1606
    }
1607

1608
    SStringBuilder sb = {0};
×
1609
    for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
×
1610
      char **key = (char **)taosArrayGet(tbNames, iReq);
×
1611
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
×
1612
      if (iReq < req.nReqs - 1) {
×
1613
        taosStringBuilderAppendChar(&sb, ',');
×
1614
      }
1615
      taosMemoryFreeClear(*key);
×
1616
    }
1617

1618
    size_t len = 0;
×
1619
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
×
1620

1621
    if (pOriginRpc->info.conn.user != NULL && strlen(pOriginRpc->info.conn.user) > 0) {
×
1622
      auditAddRecord(pOriginRpc, clusterId, "dropTable", name.dbname, "", keyJoined, len);
×
1623
    }
1624

1625
    taosStringBuilderDestroy(&sb);
×
1626
  }
1627

1628
_exit:
×
1629
  taosArrayDestroy(tbUids);
×
1630
  pStore = tdUidStoreFree(pStore);
×
1631
  tDecoderClear(&decoder);
×
1632
  tEncodeSize(tEncodeSVDropTbBatchRsp, &rsp, pRsp->contLen, ret);
×
1633
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
×
1634
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
×
1635
  if (tEncodeSVDropTbBatchRsp(&encoder, &rsp) != 0) {
×
1636
    vError("vgId:%d, failed to encode drop table batch response", TD_VID(pVnode));
×
1637
  }
1638
  tEncoderClear(&encoder);
×
1639
  taosArrayDestroy(rsp.pArray);
×
1640
  taosArrayDestroy(tbNames);
×
1641
  return 0;
×
1642
}
1643

1644
#ifdef BUILD_NO_CALL
1645
static int32_t vnodeDebugPrintSingleSubmitMsg(SMeta *pMeta, SSubmitBlk *pBlock, SSubmitMsgIter *msgIter,
1646
                                              const char *tags) {
1647
  SSubmitBlkIter blkIter = {0};
1648
  STSchema      *pSchema = NULL;
1649
  tb_uid_t       suid = 0;
1650
  STSRow        *row = NULL;
1651
  int32_t        rv = -1;
1652

1653
  tInitSubmitBlkIter(msgIter, pBlock, &blkIter);
1654
  if (blkIter.row == NULL) return 0;
1655

1656
  int32_t code = metaGetTbTSchemaNotNull(pMeta, msgIter->suid, TD_ROW_SVER(blkIter.row), 1,
1657
                                         &pSchema);  // TODO: use the real schema
1658
  if (TSDB_CODE_SUCCESS != code) {
1659
    printf("%s:%d no valid schema\n", tags, __LINE__);
1660
    return code;
1661
  }
1662

1663
  suid = msgIter->suid;
1664
  rv = TD_ROW_SVER(blkIter.row);
1665

1666
  char __tags[128] = {0};
1667
  snprintf(__tags, 128, "%s: uid %" PRIi64 " ", tags, msgIter->uid);
1668
  while ((row = tGetSubmitBlkNext(&blkIter))) {
1669
    tdSRowPrint(row, pSchema, __tags);
1670
  }
1671

1672
  taosMemoryFreeClear(pSchema);
1673

1674
  return TSDB_CODE_SUCCESS;
1675
}
1676
#endif
1677
typedef struct SSubmitReqConvertCxt {
1678
  SSubmitMsgIter msgIter;
1679
  SSubmitBlk    *pBlock;
1680
  SSubmitBlkIter blkIter;
1681
  STSRow        *pRow;
1682
  STSRowIter     rowIter;
1683
  SSubmitTbData *pTbData;
1684
  STSchema      *pTbSchema;
1685
  SArray        *pColValues;
1686
} SSubmitReqConvertCxt;
1687

1688
static int32_t vnodeResetTableCxt(SMeta *pMeta, SSubmitReqConvertCxt *pCxt) {
×
1689
  taosMemoryFreeClear(pCxt->pTbSchema);
×
1690
  int32_t code = metaGetTbTSchemaNotNull(pMeta, pCxt->msgIter.suid > 0 ? pCxt->msgIter.suid : pCxt->msgIter.uid,
×
1691
                                         pCxt->msgIter.sversion, 1, &pCxt->pTbSchema);
1692
  if (TSDB_CODE_SUCCESS != code) {
×
1693
    return code;
×
1694
  }
1695
  tdSTSRowIterInit(&pCxt->rowIter, pCxt->pTbSchema);
×
1696

1697
  tDestroySubmitTbData(pCxt->pTbData, TSDB_MSG_FLG_ENCODE);
×
1698
  if (NULL == pCxt->pTbData) {
×
1699
    pCxt->pTbData = taosMemoryCalloc(1, sizeof(SSubmitTbData));
×
1700
    if (NULL == pCxt->pTbData) {
×
1701
      return terrno;
×
1702
    }
1703
  }
1704
  pCxt->pTbData->flags = 0;
×
1705
  pCxt->pTbData->suid = pCxt->msgIter.suid;
×
1706
  pCxt->pTbData->uid = pCxt->msgIter.uid;
×
1707
  pCxt->pTbData->sver = pCxt->msgIter.sversion;
×
1708
  pCxt->pTbData->pCreateTbReq = NULL;
×
1709
  pCxt->pTbData->aRowP = taosArrayInit(128, POINTER_BYTES);
×
1710
  if (NULL == pCxt->pTbData->aRowP) {
×
1711
    return terrno;
×
1712
  }
1713

1714
  taosArrayDestroy(pCxt->pColValues);
×
1715
  pCxt->pColValues = taosArrayInit(pCxt->pTbSchema->numOfCols, sizeof(SColVal));
×
1716
  if (NULL == pCxt->pColValues) {
×
1717
    return terrno;
×
1718
  }
1719
  for (int32_t i = 0; i < pCxt->pTbSchema->numOfCols; ++i) {
×
1720
    SColVal val = COL_VAL_NONE(pCxt->pTbSchema->columns[i].colId, pCxt->pTbSchema->columns[i].type);
×
1721
    if (taosArrayPush(pCxt->pColValues, &val) == NULL) {
×
1722
      return terrno;
×
1723
    }
1724
  }
1725

1726
  return TSDB_CODE_SUCCESS;
×
1727
}
1728

1729
static void vnodeDestroySubmitReqConvertCxt(SSubmitReqConvertCxt *pCxt) {
×
1730
  taosMemoryFreeClear(pCxt->pTbSchema);
×
1731
  tDestroySubmitTbData(pCxt->pTbData, TSDB_MSG_FLG_ENCODE);
×
1732
  taosMemoryFreeClear(pCxt->pTbData);
×
1733
  taosArrayDestroy(pCxt->pColValues);
×
1734
}
×
1735

1736
static int32_t vnodeCellValConvertToColVal(STColumn *pCol, SCellVal *pCellVal, SColVal *pColVal) {
×
1737
  if (tdValTypeIsNone(pCellVal->valType)) {
×
1738
    pColVal->flag = CV_FLAG_NONE;
×
1739
    return TSDB_CODE_SUCCESS;
×
1740
  }
1741

1742
  if (tdValTypeIsNull(pCellVal->valType)) {
×
1743
    pColVal->flag = CV_FLAG_NULL;
×
1744
    return TSDB_CODE_SUCCESS;
×
1745
  }
1746

1747
  if (IS_VAR_DATA_TYPE(pCol->type)) {
×
1748
    pColVal->value.nData = varDataLen(pCellVal->val);
×
1749
    pColVal->value.pData = (uint8_t *)varDataVal(pCellVal->val);
×
1750
  } else if (TSDB_DATA_TYPE_FLOAT == pCol->type) {
×
1751
    float f = GET_FLOAT_VAL(pCellVal->val);
×
1752
    memcpy(&pColVal->value.val, &f, sizeof(f));
×
1753
  } else if (TSDB_DATA_TYPE_DOUBLE == pCol->type) {
×
1754
    pColVal->value.val = *(int64_t *)pCellVal->val;
×
1755
  } else {
1756
    GET_TYPED_DATA(pColVal->value.val, int64_t, pCol->type, pCellVal->val);
×
1757
  }
1758

1759
  pColVal->flag = CV_FLAG_VALUE;
×
1760
  return TSDB_CODE_SUCCESS;
×
1761
}
1762

1763
static int32_t vnodeTSRowConvertToColValArray(SSubmitReqConvertCxt *pCxt) {
×
1764
  int32_t code = TSDB_CODE_SUCCESS;
×
1765
  tdSTSRowIterReset(&pCxt->rowIter, pCxt->pRow);
×
1766
  for (int32_t i = 0; TSDB_CODE_SUCCESS == code && i < pCxt->pTbSchema->numOfCols; ++i) {
×
1767
    STColumn *pCol = pCxt->pTbSchema->columns + i;
×
1768
    SCellVal  cellVal = {0};
×
1769
    if (!tdSTSRowIterFetch(&pCxt->rowIter, pCol->colId, pCol->type, &cellVal)) {
×
1770
      break;
×
1771
    }
1772
    code = vnodeCellValConvertToColVal(pCol, &cellVal, (SColVal *)taosArrayGet(pCxt->pColValues, i));
×
1773
  }
1774
  return code;
×
1775
}
1776

1777
static int32_t vnodeDecodeCreateTbReq(SSubmitReqConvertCxt *pCxt) {
×
1778
  if (pCxt->msgIter.schemaLen <= 0) {
×
1779
    return TSDB_CODE_SUCCESS;
×
1780
  }
1781

1782
  pCxt->pTbData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
×
1783
  if (NULL == pCxt->pTbData->pCreateTbReq) {
×
1784
    return terrno;
×
1785
  }
1786

1787
  SDecoder decoder = {0};
×
1788
  tDecoderInit(&decoder, (uint8_t *)pCxt->pBlock->data, pCxt->msgIter.schemaLen);
×
1789
  int32_t code = tDecodeSVCreateTbReq(&decoder, pCxt->pTbData->pCreateTbReq);
×
1790
  tDecoderClear(&decoder);
×
1791

1792
  return code;
×
1793
}
1794

1795
static int32_t vnodeSubmitReqConvertToSubmitReq2(SVnode *pVnode, SSubmitReq *pReq, SSubmitReq2 *pReq2) {
×
1796
  pReq2->aSubmitTbData = taosArrayInit(128, sizeof(SSubmitTbData));
×
1797
  if (NULL == pReq2->aSubmitTbData) {
×
1798
    return terrno;
×
1799
  }
1800

1801
  SSubmitReqConvertCxt cxt = {0};
×
1802

1803
  int32_t code = tInitSubmitMsgIter(pReq, &cxt.msgIter);
×
1804
  while (TSDB_CODE_SUCCESS == code) {
×
1805
    code = tGetSubmitMsgNext(&cxt.msgIter, &cxt.pBlock);
×
1806
    if (TSDB_CODE_SUCCESS == code) {
×
1807
      if (NULL == cxt.pBlock) {
×
1808
        break;
×
1809
      }
1810
      code = vnodeResetTableCxt(pVnode->pMeta, &cxt);
×
1811
    }
1812
    if (TSDB_CODE_SUCCESS == code) {
×
1813
      code = tInitSubmitBlkIter(&cxt.msgIter, cxt.pBlock, &cxt.blkIter);
×
1814
    }
1815
    if (TSDB_CODE_SUCCESS == code) {
×
1816
      code = vnodeDecodeCreateTbReq(&cxt);
×
1817
    }
1818
    while (TSDB_CODE_SUCCESS == code && (cxt.pRow = tGetSubmitBlkNext(&cxt.blkIter)) != NULL) {
×
1819
      code = vnodeTSRowConvertToColValArray(&cxt);
×
1820
      if (TSDB_CODE_SUCCESS == code) {
×
1821
        SRow **pNewRow = taosArrayReserve(cxt.pTbData->aRowP, 1);
×
1822
        code = tRowBuild(cxt.pColValues, cxt.pTbSchema, pNewRow);
×
1823
      }
1824
    }
1825
    if (TSDB_CODE_SUCCESS == code) {
×
1826
      code = (NULL == taosArrayPush(pReq2->aSubmitTbData, cxt.pTbData) ? terrno : TSDB_CODE_SUCCESS);
×
1827
    }
1828
    if (TSDB_CODE_SUCCESS == code) {
×
1829
      taosMemoryFreeClear(cxt.pTbData);
×
1830
    }
1831
  }
1832

1833
  vnodeDestroySubmitReqConvertCxt(&cxt);
×
1834
  return code;
×
1835
}
1836

1837
static int32_t vnodeRebuildSubmitReqMsg(SSubmitReq2 *pSubmitReq, void **ppMsg) {
×
1838
  int32_t  code = TSDB_CODE_SUCCESS;
×
1839
  char    *pMsg = NULL;
×
1840
  uint32_t msglen = 0;
×
1841
  tEncodeSize(tEncodeSubmitReq, pSubmitReq, msglen, code);
×
1842
  if (TSDB_CODE_SUCCESS == code) {
×
1843
    pMsg = taosMemoryMalloc(msglen);
×
1844
    if (NULL == pMsg) {
×
1845
      code = terrno;
×
1846
    }
1847
  }
1848
  if (TSDB_CODE_SUCCESS == code) {
×
1849
    SEncoder encoder;
1850
    tEncoderInit(&encoder, (uint8_t *)pMsg, msglen);
×
1851
    code = tEncodeSubmitReq(&encoder, pSubmitReq);
×
1852
    tEncoderClear(&encoder);
×
1853
  }
1854
  if (TSDB_CODE_SUCCESS == code) {
×
1855
    *ppMsg = pMsg;
×
1856
  }
1857
  return code;
×
1858
}
1859

1860
static int32_t vnodeProcessSubmitReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
×
1861
                                     SRpcMsg *pOriginalMsg) {
1862
  int32_t code = 0;
×
1863
  terrno = 0;
×
1864

1865
  SSubmitReq2 *pSubmitReq = &(SSubmitReq2){0};
×
1866
  SSubmitRsp2 *pSubmitRsp = &(SSubmitRsp2){0};
×
1867
  SArray      *newTbUids = NULL;
×
1868
  int32_t      ret;
1869
  SEncoder     ec = {0};
×
1870

1871
  pRsp->code = TSDB_CODE_SUCCESS;
×
1872

1873
  void           *pAllocMsg = NULL;
×
1874
  SSubmitReq2Msg *pMsg = (SSubmitReq2Msg *)pReq;
×
1875
  if (0 == pMsg->version) {
×
1876
    code = vnodeSubmitReqConvertToSubmitReq2(pVnode, (SSubmitReq *)pMsg, pSubmitReq);
×
1877
    if (TSDB_CODE_SUCCESS == code) {
×
1878
      code = vnodeRebuildSubmitReqMsg(pSubmitReq, &pReq);
×
1879
    }
1880
    if (TSDB_CODE_SUCCESS == code) {
×
1881
      pAllocMsg = pReq;
×
1882
    }
1883
    if (TSDB_CODE_SUCCESS != code) {
×
1884
      goto _exit;
×
1885
    }
1886
  } else {
1887
    // decode
1888
    pReq = POINTER_SHIFT(pReq, sizeof(SSubmitReq2Msg));
×
1889
    len -= sizeof(SSubmitReq2Msg);
×
1890
    SDecoder dc = {0};
×
1891
    tDecoderInit(&dc, pReq, len);
×
1892
    if (tDecodeSubmitReq(&dc, pSubmitReq) < 0) {
×
1893
      code = TSDB_CODE_INVALID_MSG;
×
1894
      goto _exit;
×
1895
    }
1896
    tDecoderClear(&dc);
×
1897
  }
1898

1899
  // scan
1900
  TSKEY now = taosGetTimestamp(pVnode->config.tsdbCfg.precision);
×
1901
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * pVnode->config.tsdbCfg.keep2;
×
1902
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
×
1903
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
×
1904
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
×
1905

1906
    if (pSubmitTbData->pCreateTbReq && pSubmitTbData->pCreateTbReq->uid == 0) {
×
1907
      code = TSDB_CODE_INVALID_MSG;
×
1908
      goto _exit;
×
1909
    }
1910

1911
    if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
×
1912
      if (TARRAY_SIZE(pSubmitTbData->aCol) <= 0) {
×
1913
        code = TSDB_CODE_INVALID_MSG;
×
1914
        goto _exit;
×
1915
      }
1916

1917
      SColData *colDataArr = TARRAY_DATA(pSubmitTbData->aCol);
×
1918
      SRowKey   lastKey;
1919
      tColDataArrGetRowKey(colDataArr, TARRAY_SIZE(pSubmitTbData->aCol), 0, &lastKey);
×
1920
      for (int32_t iRow = 1; iRow < colDataArr[0].nVal; iRow++) {
×
1921
        SRowKey key;
1922
        tColDataArrGetRowKey(TARRAY_DATA(pSubmitTbData->aCol), TARRAY_SIZE(pSubmitTbData->aCol), iRow, &key);
×
1923
        if (tRowKeyCompare(&lastKey, &key) >= 0) {
×
1924
          code = TSDB_CODE_INVALID_MSG;
×
1925
          vError("vgId:%d %s failed 1 since %s, version:%" PRId64, TD_VID(pVnode), __func__, tstrerror(terrno), ver);
×
1926
          goto _exit;
×
1927
        }
1928
      }
1929
    } else {
1930
      int32_t nRow = TARRAY_SIZE(pSubmitTbData->aRowP);
×
1931
      SRow  **aRow = (SRow **)TARRAY_DATA(pSubmitTbData->aRowP);
×
1932
      SRowKey lastRowKey;
1933
      for (int32_t iRow = 0; iRow < nRow; ++iRow) {
×
1934
        if (aRow[iRow]->ts < minKey || aRow[iRow]->ts > maxKey) {
×
1935
          code = TSDB_CODE_INVALID_MSG;
×
1936
          vError("vgId:%d %s failed 2 since %s, version:%" PRId64, TD_VID(pVnode), __func__, tstrerror(code), ver);
×
1937
          goto _exit;
×
1938
        }
1939
        if (iRow == 0) {
×
1940
          tRowGetKey(aRow[iRow], &lastRowKey);
×
1941
        } else {
1942
          SRowKey rowKey;
1943
          tRowGetKey(aRow[iRow], &rowKey);
×
1944

1945
          if (tRowKeyCompare(&lastRowKey, &rowKey) >= 0) {
×
1946
            code = TSDB_CODE_INVALID_MSG;
×
1947
            vError("vgId:%d %s failed 3 since %s, version:%" PRId64, TD_VID(pVnode), __func__, tstrerror(code), ver);
×
1948
            goto _exit;
×
1949
          }
1950
          lastRowKey = rowKey;
×
1951
        }
1952
      }
1953
    }
1954
  }
1955

1956
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
×
1957
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
×
1958

1959
    if (pSubmitTbData->pCreateTbReq) {
×
1960
      pSubmitTbData->uid = pSubmitTbData->pCreateTbReq->uid;
×
1961
    } else {
1962
      SMetaInfo info = {0};
×
1963

1964
      code = metaGetInfo(pVnode->pMeta, pSubmitTbData->uid, &info, NULL);
×
1965
      if (code) {
×
1966
        code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
1967
        vWarn("vgId:%d, table uid:%" PRId64 " not exists", TD_VID(pVnode), pSubmitTbData->uid);
×
1968
        goto _exit;
×
1969
      }
1970

1971
      if (info.suid != pSubmitTbData->suid) {
×
1972
        code = TSDB_CODE_INVALID_MSG;
×
1973
        goto _exit;
×
1974
      }
1975

1976
      if (info.suid) {
×
1977
        if (metaGetInfo(pVnode->pMeta, info.suid, &info, NULL) != 0) {
×
1978
          vWarn("vgId:%d, table uid:%" PRId64 " not exists", TD_VID(pVnode), info.suid);
×
1979
        }
1980
      }
1981

1982
      if (pSubmitTbData->sver != info.skmVer) {
×
1983
        code = TSDB_CODE_TDB_INVALID_TABLE_SCHEMA_VER;
×
1984
        goto _exit;
×
1985
      }
1986
    }
1987

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

1992
      if (nColData <= 0) {
×
1993
        code = TSDB_CODE_INVALID_MSG;
×
1994
        goto _exit;
×
1995
      }
1996

1997
      if (aColData[0].cid != PRIMARYKEY_TIMESTAMP_COL_ID || aColData[0].type != TSDB_DATA_TYPE_TIMESTAMP ||
×
1998
          aColData[0].nVal <= 0) {
×
1999
        code = TSDB_CODE_INVALID_MSG;
×
2000
        goto _exit;
×
2001
      }
2002

2003
      for (int32_t j = 1; j < nColData; j++) {
×
2004
        if (aColData[j].nVal != aColData[0].nVal) {
×
2005
          code = TSDB_CODE_INVALID_MSG;
×
2006
          goto _exit;
×
2007
        }
2008
      }
2009
    }
2010
  }
2011

2012
  vDebug("vgId:%d, submit block size %d", TD_VID(pVnode), (int32_t)taosArrayGetSize(pSubmitReq->aSubmitTbData));
×
2013

2014
  // loop to handle
2015
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
×
2016
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
×
2017

2018
    // create table
2019
    if (pSubmitTbData->pCreateTbReq) {
×
2020
      // alloc if need
2021
      if (pSubmitRsp->aCreateTbRsp == NULL &&
×
2022
          (pSubmitRsp->aCreateTbRsp = taosArrayInit(TARRAY_SIZE(pSubmitReq->aSubmitTbData), sizeof(SVCreateTbRsp))) ==
×
2023
              NULL) {
2024
        code = terrno;
×
2025
        goto _exit;
×
2026
      }
2027

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

2030
      // create table
2031
      if (metaCreateTable2(pVnode->pMeta, ver, pSubmitTbData->pCreateTbReq, &pCreateTbRsp->pMeta) == 0) {
×
2032
        // create table success
2033

2034
        if (newTbUids == NULL &&
×
2035
            (newTbUids = taosArrayInit(TARRAY_SIZE(pSubmitReq->aSubmitTbData), sizeof(int64_t))) == NULL) {
×
2036
          code = terrno;
×
2037
          goto _exit;
×
2038
        }
2039

2040
        if (taosArrayPush(newTbUids, &pSubmitTbData->uid) == NULL) {
×
2041
          code = terrno;
×
2042
          goto _exit;
×
2043
        }
2044

2045
        if (pCreateTbRsp->pMeta) {
×
2046
          vnodeUpdateMetaRsp(pVnode, pCreateTbRsp->pMeta);
×
2047
        }
2048
      } else {  // create table failed
2049
        if (terrno != TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
×
2050
          code = terrno;
×
2051
          vError("vgId:%d failed to create table:%s, code:%s", TD_VID(pVnode), pSubmitTbData->pCreateTbReq->name,
×
2052
                 tstrerror(terrno));
2053
          goto _exit;
×
2054
        }
2055
        terrno = 0;
×
2056
        pSubmitTbData->uid = pSubmitTbData->pCreateTbReq->uid;  // update uid if table exist for using below
×
2057
      }
2058
    }
2059

2060
    // insert data
2061
    int32_t affectedRows;
2062
    code = tsdbInsertTableData(pVnode->pTsdb, ver, pSubmitTbData, &affectedRows);
×
2063
    if (code) goto _exit;
×
2064

2065
    code = metaUpdateChangeTimeWithLock(pVnode->pMeta, pSubmitTbData->uid, pSubmitTbData->ctimeMs);
×
2066
    if (code) goto _exit;
×
2067

2068
    pSubmitRsp->affectedRows += affectedRows;
×
2069
  }
2070

2071
  // update the affected table uid list
2072
  if (taosArrayGetSize(newTbUids) > 0) {
×
2073
    vDebug("vgId:%d, add %d table into query table list in handling submit", TD_VID(pVnode),
×
2074
           (int32_t)taosArrayGetSize(newTbUids));
2075
    if (tqUpdateTbUidList(pVnode->pTq, newTbUids, true) != 0) {
×
2076
      vError("vgId:%d, failed to update tbUid list", TD_VID(pVnode));
×
2077
    }
2078
  }
2079

2080
_exit:
×
2081
  // message
2082
  pRsp->code = code;
×
2083
  tEncodeSize(tEncodeSSubmitRsp2, pSubmitRsp, pRsp->contLen, ret);
×
2084
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
×
2085
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
×
2086
  if (tEncodeSSubmitRsp2(&ec, pSubmitRsp) < 0) {
×
2087
    vError("vgId:%d, failed to encode submit response", TD_VID(pVnode));
×
2088
  }
2089
  tEncoderClear(&ec);
×
2090

2091
  // update statistics
2092
  (void)atomic_add_fetch_64(&pVnode->statis.nInsert, pSubmitRsp->affectedRows);
×
2093
  (void)atomic_add_fetch_64(&pVnode->statis.nInsertSuccess, pSubmitRsp->affectedRows);
×
2094
  (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsert, 1);
×
2095

2096
  if (tsEnableMonitor && tsMonitorFqdn[0] != 0 && tsMonitorPort != 0 && pSubmitRsp->affectedRows > 0 &&
×
2097
      strlen(pOriginalMsg->info.conn.user) > 0 && tsInsertCounter != NULL) {
×
2098
    const char *sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT_AFFECTED_ROWS,
×
2099
                                   pVnode->monitor.strClusterId,
×
2100
                                   pVnode->monitor.strDnodeId,
×
2101
                                   tsLocalEp,
2102
                                   pVnode->monitor.strVgId,
×
2103
                                   pOriginalMsg->info.conn.user,
×
2104
                                   "Success"};
2105
    int         tv = taos_counter_add(tsInsertCounter, pSubmitRsp->affectedRows, sample_labels);
×
2106
  }
2107

2108
  if (code == 0) {
×
2109
    (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
×
2110
    code = tdProcessRSmaSubmit(pVnode->pSma, ver, pSubmitReq, pReq, len);
×
2111
  }
2112
  /*
2113
  if (code == 0) {
2114
    atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
2115
    code = tdProcessRSmaSubmit(pVnode->pSma, ver, pSubmitReq, pReq, len);
2116

2117
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT, pVnode->monitor.strClusterId,
2118
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2119
                                          pOriginalMsg->info.conn.user, "Success"};
2120
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2121
  }
2122
  else{
2123
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT, pVnode->monitor.strClusterId,
2124
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2125
                                        pOriginalMsg->info.conn.user, "Failed"};
2126
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2127
  }
2128
  */
2129

2130
  // clear
2131
  taosArrayDestroy(newTbUids);
×
2132
  tDestroySubmitReq(pSubmitReq, 0 == pMsg->version ? TSDB_MSG_FLG_CMPT : TSDB_MSG_FLG_DECODE);
×
2133
  tDestroySSubmitRsp2(pSubmitRsp, TSDB_MSG_FLG_ENCODE);
×
2134

2135
  if (code) terrno = code;
×
2136

2137
  taosMemoryFree(pAllocMsg);
×
2138

2139
  return code;
×
2140
}
2141

2142
static int32_t vnodeProcessCreateTSmaReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
2143
  SVCreateTSmaReq req = {0};
×
2144
  SDecoder        coder = {0};
×
2145

2146
  if (pRsp) {
×
2147
    pRsp->msgType = TDMT_VND_CREATE_SMA_RSP;
×
2148
    pRsp->code = TSDB_CODE_SUCCESS;
×
2149
    pRsp->pCont = NULL;
×
2150
    pRsp->contLen = 0;
×
2151
  }
2152

2153
  // decode and process req
2154
  tDecoderInit(&coder, pReq, len);
×
2155

2156
  if (tDecodeSVCreateTSmaReq(&coder, &req) < 0) {
×
2157
    terrno = TSDB_CODE_MSG_DECODE_ERROR;
×
2158
    if (pRsp) pRsp->code = terrno;
×
2159
    goto _err;
×
2160
  }
2161

2162
  if (tdProcessTSmaCreate(pVnode->pSma, ver, (const char *)&req) < 0) {
×
2163
    if (pRsp) pRsp->code = terrno;
×
2164
    goto _err;
×
2165
  }
2166

2167
  tDecoderClear(&coder);
×
2168
  vDebug("vgId:%d, success to create tsma %s:%" PRIi64 " version %" PRIi64 " for table %" PRIi64, TD_VID(pVnode),
×
2169
         req.indexName, req.indexUid, ver, req.tableUid);
2170
  return 0;
×
2171

2172
_err:
×
2173
  tDecoderClear(&coder);
×
2174
  vError("vgId:%d, failed to create tsma %s:%" PRIi64 " version %" PRIi64 "for table %" PRIi64 " since %s",
×
2175
         TD_VID(pVnode), req.indexName, req.indexUid, ver, req.tableUid, terrstr());
2176
  return terrno;
×
2177
}
2178

2179
/**
2180
 * @brief specific for smaDstVnode
2181
 *
2182
 * @param pVnode
2183
 * @param pCont
2184
 * @param contLen
2185
 * @return int32_t
2186
 */
2187
int32_t vnodeProcessCreateTSma(SVnode *pVnode, void *pCont, uint32_t contLen) {
×
2188
  return vnodeProcessCreateTSmaReq(pVnode, 1, pCont, contLen, NULL);
×
2189
}
2190

2191
static int32_t vnodeConsolidateAlterHashRange(SVnode *pVnode, int64_t ver) {
×
2192
  int32_t code = TSDB_CODE_SUCCESS;
×
2193

2194
  vInfo("vgId:%d, trim meta of tables per hash range [%" PRIu32 ", %" PRIu32 "]. apply-index:%" PRId64, TD_VID(pVnode),
×
2195
        pVnode->config.hashBegin, pVnode->config.hashEnd, ver);
2196

2197
  // TODO: trim meta of tables from TDB per hash range [pVnode->config.hashBegin, pVnode->config.hashEnd]
2198
  code = metaTrimTables(pVnode->pMeta, ver);
×
2199

2200
  return code;
×
2201
}
2202

2203
static int32_t vnodeProcessAlterConfirmReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
2204
  vInfo("vgId:%d, vnode handle msgType:alter-confirm, alter confirm msg is processed", TD_VID(pVnode));
×
2205
  int32_t code = TSDB_CODE_SUCCESS;
×
2206
  if (!pVnode->config.hashChange) {
×
2207
    goto _exit;
×
2208
  }
2209

2210
  code = vnodeConsolidateAlterHashRange(pVnode, ver);
×
2211
  if (code < 0) {
×
2212
    vError("vgId:%d, failed to consolidate alter hashrange since %s. version:%" PRId64, TD_VID(pVnode), terrstr(), ver);
×
2213
    goto _exit;
×
2214
  }
2215
  pVnode->config.hashChange = false;
×
2216

2217
_exit:
×
2218
  pRsp->msgType = TDMT_VND_ALTER_CONFIRM_RSP;
×
2219
  pRsp->code = code;
×
2220
  pRsp->pCont = NULL;
×
2221
  pRsp->contLen = 0;
×
2222

2223
  return code;
×
2224
}
2225

2226
extern int32_t tsdbDisableAndCancelAllBgTask(STsdb *pTsdb);
2227
extern void    tsdbEnableBgTask(STsdb *pTsdb);
2228

2229
static int32_t vnodeProcessAlterConfigReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
2230
  bool walChanged = false;
×
2231
  bool tsdbChanged = false;
×
2232

2233
  SAlterVnodeConfigReq req = {0};
×
2234
  if (tDeserializeSAlterVnodeConfigReq(pReq, len, &req) != 0) {
×
2235
    terrno = TSDB_CODE_INVALID_MSG;
×
2236
    return TSDB_CODE_INVALID_MSG;
×
2237
  }
2238

2239
  vInfo("vgId:%d, start to alter vnode config, page:%d pageSize:%d buffer:%d szPage:%d szBuf:%" PRIu64
×
2240
        " cacheLast:%d cacheLastSize:%d days:%d keep0:%d keep1:%d keep2:%d keepTimeOffset:%d s3KeepLocal:%d "
2241
        "s3Compact:%d fsync:%d level:%d "
2242
        "walRetentionPeriod:%d walRetentionSize:%d",
2243
        TD_VID(pVnode), req.pages, req.pageSize, req.buffer, req.pageSize * 1024, (uint64_t)req.buffer * 1024 * 1024,
2244
        req.cacheLast, req.cacheLastSize, req.daysPerFile, req.daysToKeep0, req.daysToKeep1, req.daysToKeep2,
2245
        req.keepTimeOffset, req.s3KeepLocal, req.s3Compact, req.walFsyncPeriod, req.walLevel, req.walRetentionPeriod,
2246
        req.walRetentionSize);
2247

2248
  if (pVnode->config.cacheLastSize != req.cacheLastSize) {
×
2249
    pVnode->config.cacheLastSize = req.cacheLastSize;
×
2250
    tsdbCacheSetCapacity(pVnode, (size_t)pVnode->config.cacheLastSize * 1024 * 1024);
×
2251
  }
2252

2253
  if (pVnode->config.szBuf != req.buffer * 1024LL * 1024LL) {
×
2254
    vInfo("vgId:%d, vnode buffer is changed from %" PRId64 " to %" PRId64, TD_VID(pVnode), pVnode->config.szBuf,
×
2255
          (uint64_t)(req.buffer * 1024LL * 1024LL));
2256
    pVnode->config.szBuf = req.buffer * 1024LL * 1024LL;
×
2257
  }
2258

2259
  if (pVnode->config.szCache != req.pages) {
×
2260
    if (metaAlterCache(pVnode->pMeta, req.pages) < 0) {
×
2261
      vError("vgId:%d, failed to change vnode pages from %d to %d failed since %s", TD_VID(pVnode),
×
2262
             pVnode->config.szCache, req.pages, tstrerror(errno));
2263
      return errno;
×
2264
    } else {
2265
      vInfo("vgId:%d, vnode pages is changed from %d to %d", TD_VID(pVnode), pVnode->config.szCache, req.pages);
×
2266
      pVnode->config.szCache = req.pages;
×
2267
    }
2268
  }
2269

2270
  if (pVnode->config.cacheLast != req.cacheLast) {
×
2271
    pVnode->config.cacheLast = req.cacheLast;
×
2272
  }
2273

2274
  if (pVnode->config.walCfg.fsyncPeriod != req.walFsyncPeriod) {
×
2275
    pVnode->config.walCfg.fsyncPeriod = req.walFsyncPeriod;
×
2276
    walChanged = true;
×
2277
  }
2278

2279
  if (pVnode->config.walCfg.level != req.walLevel) {
×
2280
    if (pVnode->config.walCfg.level == 0) {
×
2281
      pVnode->config.walCfg.clearFiles = 1;
×
2282
    }
2283
    pVnode->config.walCfg.level = req.walLevel;
×
2284
    walChanged = true;
×
2285
  }
2286

2287
  if (pVnode->config.walCfg.retentionPeriod != req.walRetentionPeriod) {
×
2288
    pVnode->config.walCfg.retentionPeriod = req.walRetentionPeriod;
×
2289
    walChanged = true;
×
2290
  }
2291

2292
  if (pVnode->config.walCfg.retentionSize != req.walRetentionSize) {
×
2293
    pVnode->config.walCfg.retentionSize = req.walRetentionSize;
×
2294
    walChanged = true;
×
2295
  }
2296

2297
  if (pVnode->config.tsdbCfg.keep0 != req.daysToKeep0) {
×
2298
    pVnode->config.tsdbCfg.keep0 = req.daysToKeep0;
×
2299
    if (!VND_IS_RSMA(pVnode)) {
×
2300
      tsdbChanged = true;
×
2301
    }
2302
  }
2303

2304
  if (pVnode->config.tsdbCfg.keep1 != req.daysToKeep1) {
×
2305
    pVnode->config.tsdbCfg.keep1 = req.daysToKeep1;
×
2306
    if (!VND_IS_RSMA(pVnode)) {
×
2307
      tsdbChanged = true;
×
2308
    }
2309
  }
2310

2311
  if (pVnode->config.tsdbCfg.keep2 != req.daysToKeep2) {
×
2312
    pVnode->config.tsdbCfg.keep2 = req.daysToKeep2;
×
2313
    if (!VND_IS_RSMA(pVnode)) {
×
2314
      tsdbChanged = true;
×
2315
    }
2316
  }
2317

2318
  if (pVnode->config.tsdbCfg.keepTimeOffset != req.keepTimeOffset) {
×
2319
    pVnode->config.tsdbCfg.keepTimeOffset = req.keepTimeOffset;
×
2320
    if (!VND_IS_RSMA(pVnode)) {
×
2321
      tsdbChanged = true;
×
2322
    }
2323
  }
2324

2325
  if (req.sttTrigger != -1 && req.sttTrigger != pVnode->config.sttTrigger) {
×
2326
    if (req.sttTrigger > 1 && pVnode->config.sttTrigger > 1) {
×
2327
      pVnode->config.sttTrigger = req.sttTrigger;
×
2328
    } else {
2329
      vnodeAWait(&pVnode->commitTask);
×
2330

2331
      int32_t ret = tsdbDisableAndCancelAllBgTask(pVnode->pTsdb);
×
2332
      if (ret != 0) {
×
2333
        vError("vgId:%d, failed to disable bg task since %s", TD_VID(pVnode), tstrerror(errno));
×
2334
      }
2335

2336
      pVnode->config.sttTrigger = req.sttTrigger;
×
2337
      tsdbEnableBgTask(pVnode->pTsdb);
×
2338
    }
2339
  }
2340

2341
  if (req.minRows != -1 && req.minRows != pVnode->config.tsdbCfg.minRows) {
×
2342
    pVnode->config.tsdbCfg.minRows = req.minRows;
×
2343
  }
2344

2345
  if (req.s3KeepLocal != -1 && req.s3KeepLocal != pVnode->config.s3KeepLocal) {
×
2346
    pVnode->config.s3KeepLocal = req.s3KeepLocal;
×
2347
  }
2348
  if (req.s3Compact != -1 && req.s3Compact != pVnode->config.s3Compact) {
×
2349
    pVnode->config.s3Compact = req.s3Compact;
×
2350
  }
2351

2352
  if (walChanged) {
×
2353
    if (walAlter(pVnode->pWal, &pVnode->config.walCfg) != 0) {
×
2354
      vError("vgId:%d, failed to alter wal config since %s", TD_VID(pVnode), tstrerror(errno));
×
2355
    }
2356
  }
2357

2358
  if (tsdbChanged) {
×
2359
    tsdbSetKeepCfg(pVnode->pTsdb, &pVnode->config.tsdbCfg);
×
2360
  }
2361

2362
  return 0;
×
2363
}
2364

2365
static int32_t vnodeProcessBatchDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
2366
  SBatchDeleteReq deleteReq;
2367
  SDecoder        decoder;
2368
  tDecoderInit(&decoder, pReq, len);
×
2369
  if (tDecodeSBatchDeleteReq(&decoder, &deleteReq) < 0) {
×
2370
    tDecoderClear(&decoder);
×
2371
    return terrno = TSDB_CODE_INVALID_MSG;
×
2372
  }
2373

2374
  SMetaReader mr = {0};
×
2375
  metaReaderDoInit(&mr, pVnode->pMeta, META_READER_NOLOCK);
×
2376
  STsdb *pTsdb = pVnode->pTsdb;
×
2377

2378
  if (deleteReq.level) {
×
2379
    pTsdb = deleteReq.level == 1 ? VND_RSMA1(pVnode) : VND_RSMA2(pVnode);
×
2380
  }
2381

2382
  int32_t sz = taosArrayGetSize(deleteReq.deleteReqs);
×
2383
  for (int32_t i = 0; i < sz; i++) {
×
2384
    SSingleDeleteReq *pOneReq = taosArrayGet(deleteReq.deleteReqs, i);
×
2385
    char             *name = pOneReq->tbname;
×
2386
    if (metaGetTableEntryByName(&mr, name) < 0) {
×
2387
      vDebug("vgId:%d, stream delete msg, skip since no table: %s", pVnode->config.vgId, name);
×
2388
      continue;
×
2389
    }
2390

2391
    int64_t uid = mr.me.uid;
×
2392

2393
    int32_t code = tsdbDeleteTableData(pTsdb, ver, deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
×
2394
    if (code < 0) {
×
2395
      terrno = code;
×
2396
      vError("vgId:%d, delete error since %s, suid:%" PRId64 ", uid:%" PRId64 ", start ts:%" PRId64 ", end ts:%" PRId64,
×
2397
             TD_VID(pVnode), terrstr(), deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
2398
    }
2399

2400
    if (deleteReq.level == 0) {
×
2401
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, deleteReq.ctimeMs);
×
2402
      if (code < 0) {
×
2403
        terrno = code;
×
2404
        vError("vgId:%d, update change time error since %s, suid:%" PRId64 ", uid:%" PRId64 ", start ts:%" PRId64
×
2405
               ", end ts:%" PRId64,
2406
               TD_VID(pVnode), terrstr(), deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
2407
      }
2408
    }
2409
    tDecoderClear(&mr.coder);
×
2410
  }
2411
  metaReaderClear(&mr);
×
2412
  taosArrayDestroy(deleteReq.deleteReqs);
×
2413
  return 0;
×
2414
}
2415

2416
static int32_t vnodeProcessDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
×
2417
                                     SRpcMsg *pOriginalMsg) {
2418
  int32_t     code = 0;
×
2419
  SDecoder   *pCoder = &(SDecoder){0};
×
2420
  SDeleteRes *pRes = &(SDeleteRes){0};
×
2421

2422
  pRsp->msgType = TDMT_VND_DELETE_RSP;
×
2423
  pRsp->pCont = NULL;
×
2424
  pRsp->contLen = 0;
×
2425
  pRsp->code = TSDB_CODE_SUCCESS;
×
2426

2427
  pRes->uidList = taosArrayInit(0, sizeof(tb_uid_t));
×
2428
  if (pRes->uidList == NULL) {
×
2429
    code = terrno;
×
2430
    goto _err;
×
2431
  }
2432

2433
  tDecoderInit(pCoder, pReq, len);
×
2434
  code = tDecodeDeleteRes(pCoder, pRes);
×
2435
  if (code) goto _err;
×
2436

2437
  if (pRes->affectedRows > 0) {
×
2438
    for (int32_t iUid = 0; iUid < taosArrayGetSize(pRes->uidList); iUid++) {
×
2439
      uint64_t uid = *(uint64_t *)taosArrayGet(pRes->uidList, iUid);
×
2440
      code = tsdbDeleteTableData(pVnode->pTsdb, ver, pRes->suid, uid, pRes->skey, pRes->ekey);
×
2441
      if (code) goto _err;
×
2442
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, pRes->ctimeMs);
×
2443
      if (code) goto _err;
×
2444
    }
2445
  }
2446

2447
  code = tdProcessRSmaDelete(pVnode->pSma, ver, pRes, pReq, len);
×
2448

2449
  tDecoderClear(pCoder);
×
2450
  taosArrayDestroy(pRes->uidList);
×
2451

2452
  SVDeleteRsp rsp = {.affectedRows = pRes->affectedRows};
×
2453
  int32_t     ret = 0;
×
2454
  tEncodeSize(tEncodeSVDeleteRsp, &rsp, pRsp->contLen, ret);
×
2455
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
×
2456
  SEncoder ec = {0};
×
2457
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
×
2458
  code = tEncodeSVDeleteRsp(&ec, &rsp);
×
2459
  if (code) goto _err;
×
2460
  tEncoderClear(&ec);
×
2461
  return code;
×
2462

2463
_err:
×
2464
  /*
2465
  if(code == TSDB_CODE_SUCCESS){
2466
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_DELETE, pVnode->monitor.strClusterId,
2467
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2468
                                        pOriginalMsg->info.conn.user, "Success"};
2469
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2470
  }
2471
  else{
2472
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_DELETE, pVnode->monitor.strClusterId,
2473
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2474
                                        pOriginalMsg->info.conn.user, "Failed"};
2475
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2476
  }
2477
  */
2478

2479
  return code;
×
2480
}
2481
static int32_t vnodeProcessCreateIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
2482
  SVCreateStbReq req = {0};
×
2483
  SDecoder       dc = {0};
×
2484
  int32_t        code = 0;
×
2485

2486
  pRsp->msgType = TDMT_VND_CREATE_INDEX_RSP;
×
2487
  pRsp->code = TSDB_CODE_SUCCESS;
×
2488
  pRsp->pCont = NULL;
×
2489
  pRsp->contLen = 0;
×
2490

2491
  tDecoderInit(&dc, pReq, len);
×
2492
  // decode req
2493
  if (tDecodeSVCreateStbReq(&dc, &req) < 0) {
×
2494
    tDecoderClear(&dc);
×
2495
    return terrno = TSDB_CODE_INVALID_MSG;
×
2496
  }
2497

2498
  code = metaAddIndexToSuperTable(pVnode->pMeta, ver, &req);
×
2499
  if (code) {
×
2500
    pRsp->code = code;
×
2501
    goto _err;
×
2502
  }
2503
  tDecoderClear(&dc);
×
2504
  return 0;
×
2505

2506
_err:
×
2507
  tDecoderClear(&dc);
×
2508
  return code;
×
2509
}
2510
static int32_t vnodeProcessDropIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
2511
  SDropIndexReq req = {0};
×
2512
  int32_t       code = 0;
×
2513
  pRsp->msgType = TDMT_VND_DROP_INDEX_RSP;
×
2514
  pRsp->code = TSDB_CODE_SUCCESS;
×
2515
  pRsp->pCont = NULL;
×
2516
  pRsp->contLen = 0;
×
2517

2518
  if ((code = tDeserializeSDropIdxReq(pReq, len, &req))) {
×
2519
    pRsp->code = code;
×
2520
    return code;
×
2521
  }
2522

2523
  code = metaDropIndexFromSuperTable(pVnode->pMeta, ver, &req);
×
2524
  if (code) {
×
2525
    pRsp->code = code;
×
2526
    return code;
×
2527
  }
2528
  return TSDB_CODE_SUCCESS;
×
2529
}
2530

2531
extern int32_t vnodeAsyncCompact(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
2532

2533
static int32_t vnodeProcessCompactVnodeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
2534
  if (!pVnode->restored) {
×
2535
    vInfo("vgId:%d, ignore compact req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
×
2536
    return 0;
×
2537
  }
2538
  return vnodeAsyncCompact(pVnode, ver, pReq, len, pRsp);
×
2539
}
2540

2541
static int32_t vnodeProcessConfigChangeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
2542
  if (syncCheckMember(pVnode->sync) != 0) {
×
2543
    vError("vgId:%d, failed to check member", TD_VID(pVnode));
×
2544
  }
2545

2546
  pRsp->msgType = TDMT_SYNC_CONFIG_CHANGE_RSP;
×
2547
  pRsp->code = TSDB_CODE_SUCCESS;
×
2548
  pRsp->pCont = NULL;
×
2549
  pRsp->contLen = 0;
×
2550

2551
  return 0;
×
2552
}
2553

2554
static int32_t vnodePreCheckAssignedLogSyncd(SVnode *pVnode, char *member0Token, char *member1Token) {
×
2555
  SSyncState syncState = syncGetState(pVnode->sync);
×
2556
  if (syncState.state != TAOS_SYNC_STATE_LEADER) {
×
2557
    return terrno = TSDB_CODE_SYN_NOT_LEADER;
×
2558
  }
2559

2560
  char token[TSDB_ARB_TOKEN_SIZE] = {0};
×
2561
  if (vnodeGetArbToken(pVnode, token) != 0) {
×
2562
    return terrno = TSDB_CODE_NOT_FOUND;
×
2563
  }
2564

2565
  if (strncmp(token, member0Token, TSDB_ARB_TOKEN_SIZE) != 0 &&
×
2566
      strncmp(token, member1Token, TSDB_ARB_TOKEN_SIZE) != 0) {
×
2567
    return terrno = TSDB_CODE_MND_ARB_TOKEN_MISMATCH;
×
2568
  }
2569

2570
  terrno = TSDB_CODE_SUCCESS;
×
2571
  return 0;
×
2572
}
2573

2574
static int32_t vnodeCheckAssignedLogSyncd(SVnode *pVnode, char *member0Token, char *member1Token) {
×
2575
  int32_t code = vnodePreCheckAssignedLogSyncd(pVnode, member0Token, member1Token);
×
2576
  if (code != 0) {
×
2577
    return code;
×
2578
  }
2579

2580
  return syncGetAssignedLogSynced(pVnode->sync);
×
2581
}
2582

2583
static int32_t vnodeProcessArbCheckSyncReq(SVnode *pVnode, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
2584
  int32_t code = 0;
×
2585

2586
  SVArbCheckSyncReq syncReq = {0};
×
2587

2588
  code = tDeserializeSVArbCheckSyncReq(pReq, len, &syncReq);
×
2589
  if (code) {
×
2590
    return terrno = code;
×
2591
  }
2592

2593
  pRsp->msgType = TDMT_VND_ARB_CHECK_SYNC_RSP;
×
2594
  pRsp->code = TSDB_CODE_SUCCESS;
×
2595
  pRsp->pCont = NULL;
×
2596
  pRsp->contLen = 0;
×
2597

2598
  SVArbCheckSyncRsp syncRsp = {0};
×
2599
  syncRsp.arbToken = syncReq.arbToken;
×
2600
  syncRsp.member0Token = syncReq.member0Token;
×
2601
  syncRsp.member1Token = syncReq.member1Token;
×
2602
  syncRsp.vgId = TD_VID(pVnode);
×
2603

2604
  if (vnodeCheckAssignedLogSyncd(pVnode, syncReq.member0Token, syncReq.member1Token) != 0) {
×
2605
    vError("vgId:%d, failed to check assigned log syncd", TD_VID(pVnode));
×
2606
  }
2607
  syncRsp.errCode = terrno;
×
2608

2609
  if (vnodeUpdateArbTerm(pVnode, syncReq.arbTerm) != 0) {
×
2610
    vError("vgId:%d, failed to update arb term", TD_VID(pVnode));
×
2611
    code = -1;
×
2612
    goto _OVER;
×
2613
  }
2614

2615
  int32_t contLen = tSerializeSVArbCheckSyncRsp(NULL, 0, &syncRsp);
×
2616
  if (contLen <= 0) {
×
2617
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2618
    code = -1;
×
2619
    goto _OVER;
×
2620
  }
2621
  void *pHead = rpcMallocCont(contLen);
×
2622
  if (!pHead) {
×
2623
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2624
    code = -1;
×
2625
    goto _OVER;
×
2626
  }
2627

2628
  if (tSerializeSVArbCheckSyncRsp(pHead, contLen, &syncRsp) <= 0) {
×
2629
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2630
    rpcFreeCont(pHead);
×
2631
    code = -1;
×
2632
    goto _OVER;
×
2633
  }
2634

2635
  pRsp->pCont = pHead;
×
2636
  pRsp->contLen = contLen;
×
2637

2638
  terrno = TSDB_CODE_SUCCESS;
×
2639

2640
_OVER:
×
2641
  tFreeSVArbCheckSyncReq(&syncReq);
×
2642
  return code;
×
2643
}
2644

2645
#ifndef TD_ENTERPRISE
2646
int32_t vnodeAsyncCompact(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) { return 0; }
2647
int32_t tsdbAsyncCompact(STsdb *tsdb, const STimeWindow *tw, bool sync) { return 0; }
2648
#endif
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc