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

taosdata / TDengine / #3633

11 Mar 2025 12:59PM UTC coverage: 0.0% (-60.7%) from 60.719%
#3633

push

travis-ci

web-flow
Merge pull request #30118 from taosdata/wl30

udpate ci workflow

0 of 280412 branches covered (0.0%)

Branch coverage included in aggregate %.

0 of 275582 relevant lines covered (0.0%)

0.0 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 vnodeCheckToken(SVnode *pVnode, char *member0Token, char *member1Token);
57
static int32_t vnodeCheckSyncd(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
      TSDB_CHECK_CODE(code, lino, _exit);
×
318
    }
319

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

327
    if (colData.flag != HAS_VALUE) {
×
328
      code = TSDB_CODE_INVALID_MSG;
×
329
      TSDB_CHECK_CODE(code, lino, _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
        TSDB_CHECK_CODE(code, lino, _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
        TSDB_CHECK_CODE(code, lino, _exit);
×
344
      }
345
    }
346
  } else {
347
    uint64_t nRow;
348
    if (tDecodeU64v(pCoder, &nRow) < 0) {
×
349
      code = TSDB_CODE_INVALID_MSG;
×
350
      TSDB_CHECK_CODE(code, lino, _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
        TSDB_CHECK_CODE(code, lino, _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
  if (code) {
×
373
    vError("vgId:%d, %s:%d failed to vnodePreProcessSubmitTbData submit request since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
374
  }
375
  return code;
×
376
}
377
static int32_t vnodePreProcessSubmitMsg(SVnode *pVnode, SRpcMsg *pMsg) {
×
378
  int32_t code = 0;
×
379
  int32_t lino = 0;
×
380

381
  if (tsBypassFlag & TSDB_BYPASS_RA_RPC_RECV_SUBMIT) {
×
382
    return TSDB_CODE_MSG_PREPROCESSED;
×
383
  }
384

385
  SDecoder *pCoder = &(SDecoder){0};
×
386

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

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

394
  if (tStartDecode(pCoder) < 0) {
×
395
    code = TSDB_CODE_INVALID_MSG;
×
396
    TSDB_CHECK_CODE(code, lino, _exit);
×
397
  }
398

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

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

412
  tEndDecode(pCoder);
×
413

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

423
static int32_t vnodePreProcessDeleteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
×
424
  int32_t code = 0;
×
425

426
  int32_t    size;
427
  int32_t    ret;
428
  uint8_t   *pCont;
429
  SEncoder  *pCoder = &(SEncoder){0};
×
430
  SDeleteRes res = {0};
×
431

432
  SReadHandle handle = {.vnode = pVnode, .pMsgCb = &pVnode->msgCb, .skipRollup = 1};
×
433
  initStorageAPI(&handle.api);
×
434

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

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

443
  ((SMsgHead *)pCont)->contLen = size + sizeof(SMsgHead);
×
444
  ((SMsgHead *)pCont)->vgId = TD_VID(pVnode);
×
445

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

452
  rpcFreeCont(pMsg->pCont);
×
453
  pMsg->pCont = pCont;
×
454
  pMsg->contLen = size + sizeof(SMsgHead);
×
455

456
  taosArrayDestroy(res.uidList);
×
457

458
_exit:
×
459
  return code;
×
460
}
461

462
static int32_t vnodePreProcessBatchDeleteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
×
463
  int32_t code = 0;
×
464
  int32_t lino = 0;
×
465

466
  int64_t         ctimeMs = taosGetTimestampMs();
×
467
  SBatchDeleteReq pReq = {0};
×
468
  SDecoder       *pCoder = &(SDecoder){0};
×
469

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

472
  if (tDecodeSBatchDeleteReqSetCtime(pCoder, &pReq, ctimeMs) < 0) {
×
473
    code = TSDB_CODE_INVALID_MSG;
×
474
  }
475

476
  tDecoderClear(pCoder);
×
477
  taosArrayDestroy(pReq.deleteReqs);
×
478

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

487
static int32_t vnodePreProcessArbCheckSyncMsg(SVnode *pVnode, SRpcMsg *pMsg) {
×
488
  SVArbCheckSyncReq syncReq = {0};
×
489

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

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

500
  int32_t code = terrno;
×
501
  tFreeSVArbCheckSyncReq(&syncReq);
×
502

503
  return code;
×
504
}
505

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

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

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

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

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

553
_exit:
×
554
  tDecoderClear(&dc);
×
555
  if (sentBatchReqs.pArray) {
×
556
    taosArrayDestroy(sentBatchReqs.pArray);
×
557
  }
558
  return code;
×
559
}
560

561
int32_t vnodePreProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
×
562
  int32_t code = 0;
×
563

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

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

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

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

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

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

626
  if (!(pVnode->state.applyTerm <= pMsg->info.conn.applyTerm)) {
×
627
    return terrno = TSDB_CODE_INTERNAL_ERROR;
×
628
  }
629

630
  if (!(pVnode->state.applied + 1 == ver)) {
×
631
    return terrno = TSDB_CODE_INTERNAL_ERROR;
×
632
  }
633

634
  atomic_store_64(&pVnode->state.applied, ver);
×
635
  atomic_store_64(&pVnode->state.applyTerm, pMsg->info.conn.applyTerm);
×
636

637
  if (!syncUtilUserCommit(pMsg->msgType)) goto _exit;
×
638

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

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

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

753
    } break;
×
754
    case TDMT_VND_STREAM_ALL_STOP: {
×
755
      if (pVnode->restored && vnodeIsLeader(pVnode) && (code = tqProcessAllTaskStopReq(pVnode->pTq, pMsg)) < 0) {
×
756
        goto _err;
×
757
      }
758

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

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

807
  walApplyVer(pVnode->pWal, ver);
×
808

809
  code = tqPushMsg(pVnode->pTq, pMsg->msgType);
×
810
  if (code) {
×
811
    vError("vgId:%d, failed to push msg to TQ since %s", TD_VID(pVnode), tstrerror(terrno));
×
812
    return code;
×
813
  }
814

815
  // commit if need
816
  if (needCommit) {
×
817
    vInfo("vgId:%d, commit at version %" PRId64, TD_VID(pVnode), ver);
×
818
    code = vnodeAsyncCommit(pVnode);
×
819
    if (code) {
×
820
      vError("vgId:%d, failed to vnode async commit since %s.", TD_VID(pVnode), tstrerror(terrno));
×
821
      goto _err;
×
822
    }
823

824
    // start a new one
825
    code = vnodeBegin(pVnode);
×
826
    if (code) {
×
827
      vError("vgId:%d, failed to begin vnode since %s.", TD_VID(pVnode), tstrerror(terrno));
×
828
      goto _err;
×
829
    }
830
  }
831

832
_exit:
×
833
  return 0;
×
834

835
_err:
×
836
  vError("vgId:%d, process %s request failed since %s, ver:%" PRId64, TD_VID(pVnode), TMSG_INFO(pMsg->msgType),
×
837
         tstrerror(terrno), ver);
838
  return code;
×
839
}
840

841
int32_t vnodePreprocessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg) {
×
842
  if (TDMT_SCH_QUERY != pMsg->msgType && TDMT_SCH_MERGE_QUERY != pMsg->msgType) {
×
843
    return 0;
×
844
  }
845

846
  return qWorkerPreprocessQueryMsg(pVnode->pQuery, pMsg, TDMT_SCH_QUERY == pMsg->msgType);
×
847
}
848

849
int32_t vnodeProcessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
×
850
  vTrace("message in vnode query queue is processing");
×
851
  if (pMsg->msgType == TDMT_VND_TMQ_CONSUME && !syncIsReadyForRead(pVnode->sync)) {
×
852
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
×
853
    return 0;
×
854
  }
855

856
  if (pMsg->msgType == TDMT_VND_TMQ_CONSUME && !pVnode->restored) {
×
857
    vnodeRedirectRpcMsg(pVnode, pMsg, TSDB_CODE_SYN_RESTORING);
×
858
    return 0;
×
859
  }
860

861
  SReadHandle handle = {.vnode = pVnode, .pMsgCb = &pVnode->msgCb, .pWorkerCb = pInfo->workerCb};
×
862
  initStorageAPI(&handle.api);
×
863
  int32_t code = TSDB_CODE_SUCCESS;
×
864
  bool    redirected = false;
×
865

866
  switch (pMsg->msgType) {
×
867
    case TDMT_SCH_QUERY:
×
868
      if (!syncIsReadyForRead(pVnode->sync)) {
×
869
        pMsg->code = (terrno) ? terrno : TSDB_CODE_SYN_NOT_LEADER;
×
870
        redirected = true;
×
871
      }
872
      code = qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
×
873
      if (redirected) {
×
874
        vnodeRedirectRpcMsg(pVnode, pMsg, pMsg->code);
×
875
        return 0;
×
876
      }
877
      return code;
×
878
    case TDMT_SCH_MERGE_QUERY:
×
879
      return qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
×
880
    case TDMT_SCH_QUERY_CONTINUE:
×
881
      return qWorkerProcessCQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
×
882
    case TDMT_VND_TMQ_CONSUME:
×
883
      return tqProcessPollReq(pVnode->pTq, pMsg);
×
884
    case TDMT_VND_TMQ_CONSUME_PUSH:
×
885
      return tqProcessPollPush(pVnode->pTq, pMsg);
×
886
    default:
×
887
      vError("unknown msg type:%d in query queue", pMsg->msgType);
×
888
      return TSDB_CODE_APP_ERROR;
×
889
  }
890
}
891

892
int32_t vnodeProcessFetchMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
×
893
  vTrace("vgId:%d, msg:%p in fetch queue is processing", pVnode->config.vgId, pMsg);
×
894
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
×
895
       pMsg->msgType == TDMT_VND_BATCH_META || pMsg->msgType == TDMT_VND_TABLE_NAME) &&
×
896
      !syncIsReadyForRead(pVnode->sync)) {
×
897
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
×
898
    return 0;
×
899
  }
900

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

935
    default:
×
936
      vError("unknown msg type:%d in fetch queue", pMsg->msgType);
×
937
      return TSDB_CODE_APP_ERROR;
×
938
  }
939
}
940

941
int32_t vnodeProcessStreamMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
×
942
  vTrace("vgId:%d, msg:%p in stream queue is processing", pVnode->config.vgId, pMsg);
×
943

944
  // todo: NOTE: some command needs to run on follower, such as, stop_all_tasks
945
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
×
946
       pMsg->msgType == TDMT_VND_BATCH_META) &&
×
947
      !syncIsReadyForRead(pVnode->sync)) {
×
948
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
×
949
    return 0;
×
950
  }
951

952
  switch (pMsg->msgType) {
×
953
    case TDMT_STREAM_TASK_RUN:
×
954
      return tqProcessTaskRunReq(pVnode->pTq, pMsg);
×
955
    case TDMT_STREAM_RETRIEVE:
×
956
      return tqProcessTaskRetrieveReq(pVnode->pTq, pMsg);
×
957
    case TDMT_STREAM_RETRIEVE_RSP:
×
958
      return tqProcessTaskRetrieveRsp(pVnode->pTq, pMsg);
×
959
    case TDMT_VND_GET_STREAM_PROGRESS:
×
960
      return tqStreamProgressRetrieveReq(pVnode->pTq, pMsg);
×
961
    default:
×
962
      vError("unknown msg type:%d in stream queue", pMsg->msgType);
×
963
      return TSDB_CODE_APP_ERROR;
×
964
  }
965
}
966

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

976
  switch (pMsg->msgType) {
×
977
    case TDMT_MND_STREAM_HEARTBEAT_RSP:
×
978
      return tqProcessStreamHbRsp(pVnode->pTq, pMsg);
×
979
    case TDMT_STREAM_TASK_DISPATCH:
×
980
      return tqProcessTaskDispatchReq(pVnode->pTq, pMsg);
×
981
    case TDMT_STREAM_TASK_DISPATCH_RSP:
×
982
      return tqProcessTaskDispatchRsp(pVnode->pTq, pMsg);
×
983
    case TDMT_VND_STREAM_TASK_CHECK:
×
984
      return tqProcessTaskCheckReq(pVnode->pTq, pMsg);
×
985
    case TDMT_VND_STREAM_TASK_CHECK_RSP:
×
986
      return tqProcessTaskCheckRsp(pVnode->pTq, pMsg);
×
987
    case TDMT_STREAM_TASK_CHECKPOINT_READY:
×
988
      return tqProcessTaskCheckpointReadyMsg(pVnode->pTq, pMsg);
×
989
    case TDMT_STREAM_TASK_CHECKPOINT_READY_RSP:
×
990
      return tqProcessTaskCheckpointReadyRsp(pVnode->pTq, pMsg);
×
991
    case TDMT_STREAM_RETRIEVE_TRIGGER:
×
992
      return tqProcessTaskRetrieveTriggerReq(pVnode->pTq, pMsg);
×
993
    case TDMT_STREAM_RETRIEVE_TRIGGER_RSP:
×
994
      return tqProcessTaskRetrieveTriggerRsp(pVnode->pTq, pMsg);
×
995
    case TDMT_MND_STREAM_REQ_CHKPT_RSP:
×
996
      return tqProcessStreamReqCheckpointRsp(pVnode->pTq, pMsg);
×
997
    case TDMT_MND_STREAM_CHKPT_REPORT_RSP:
×
998
      return tqProcessTaskChkptReportRsp(pVnode->pTq, pMsg);
×
999
    default:
×
1000
      vError("unknown msg type:%d in stream ctrl queue", pMsg->msgType);
×
1001
      return TSDB_CODE_APP_ERROR;
×
1002
  }
1003
}
1004

1005
int32_t vnodeProcessStreamLongExecMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
×
1006
  vTrace("vgId:%d, msg:%p in stream long exec queue is processing", pVnode->config.vgId, pMsg);
×
1007
  if (!syncIsReadyForRead(pVnode->sync)) {
×
1008
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
×
1009
    return 0;
×
1010
  }
1011

1012
  switch (pMsg->msgType) {
×
1013
    case TDMT_VND_STREAM_SCAN_HISTORY:
×
1014
      return tqProcessTaskScanHistory(pVnode->pTq, pMsg);
×
1015
    default:
×
1016
      vError("unknown msg type:%d in stream long exec queue", pMsg->msgType);
×
1017
      return TSDB_CODE_APP_ERROR;
×
1018
  }
1019
}
1020

1021
int32_t vnodeProcessStreamChkptMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
×
1022
  vTrace("vgId:%d, msg:%p in stream chkpt queue is processing", pVnode->config.vgId, pMsg);
×
1023
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
×
1024
       pMsg->msgType == TDMT_VND_BATCH_META) &&
×
1025
      !syncIsReadyForRead(pVnode->sync)) {
×
1026
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
×
1027
    return 0;
×
1028
  }
1029

1030
  switch (pMsg->msgType) {
×
1031
    case TDMT_STREAM_CHKPT_EXEC:
×
1032
      return tqProcessTaskRunReq(pVnode->pTq, pMsg);
×
1033
    default:
×
1034
      vError("unknown msg type:%d in stream chkpt queue", pMsg->msgType);
×
1035
      return TSDB_CODE_APP_ERROR;
×
1036
  }
1037
}
1038

1039
void smaHandleRes(void *pVnode, int64_t smaId, const SArray *data) {
×
1040
  int32_t code = tdProcessTSmaInsert(((SVnode *)pVnode)->pSma, smaId, (const char *)data);
×
1041
  if (code) {
×
1042
    vError("failed to process sma result since %s", tstrerror(code));
×
1043
  }
1044
}
×
1045

1046
void vnodeUpdateMetaRsp(SVnode *pVnode, STableMetaRsp *pMetaRsp) {
×
1047
  if (NULL == pMetaRsp) {
×
1048
    return;
×
1049
  }
1050

1051
  tstrncpy(pMetaRsp->dbFName, pVnode->config.dbname, TSDB_DB_FNAME_LEN);
×
1052
  pMetaRsp->dbId = pVnode->config.dbId;
×
1053
  pMetaRsp->vgId = TD_VID(pVnode);
×
1054
  pMetaRsp->precision = pVnode->config.tsdbCfg.precision;
×
1055
}
1056

1057
extern int32_t vnodeAsyncRetention(SVnode *pVnode, int64_t now);
1058

1059
static int32_t vnodeProcessTrimReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1060
  if (!pVnode->restored) {
×
1061
    vInfo("vgId:%d, ignore trim req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
×
1062
    return 0;
×
1063
  }
1064

1065
  int32_t     code = 0;
×
1066
  SVTrimDbReq trimReq = {0};
×
1067

1068
  // decode
1069
  if (tDeserializeSVTrimDbReq(pReq, len, &trimReq) != 0) {
×
1070
    code = TSDB_CODE_INVALID_MSG;
×
1071
    goto _exit;
×
1072
  }
1073

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

1076
  code = vnodeAsyncRetention(pVnode, trimReq.timestamp);
×
1077

1078
_exit:
×
1079
  return code;
×
1080
}
1081

1082
extern int32_t vnodeAsyncS3Migrate(SVnode *pVnode, int64_t now);
1083

1084
static int32_t vnodeProcessS3MigrateReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1085
  int32_t          code = 0;
×
1086
  SVS3MigrateDbReq s3migrateReq = {0};
×
1087

1088
  // decode
1089
  if (tDeserializeSVS3MigrateDbReq(pReq, len, &s3migrateReq) != 0) {
×
1090
    code = TSDB_CODE_INVALID_MSG;
×
1091
    goto _exit;
×
1092
  }
1093

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

1096
  code = vnodeAsyncS3Migrate(pVnode, s3migrateReq.timestamp);
×
1097

1098
_exit:
×
1099
  return code;
×
1100
}
1101

1102
static int32_t vnodeProcessDropTtlTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1103
  int               ret = 0;
×
1104
  SVDropTtlTableReq ttlReq = {0};
×
1105
  if (tDeserializeSVDropTtlTableReq(pReq, len, &ttlReq) != 0) {
×
1106
    ret = TSDB_CODE_INVALID_MSG;
×
1107
    goto end;
×
1108
  }
1109

1110
  if (ttlReq.nUids != taosArrayGetSize(ttlReq.pTbUids)) {
×
1111
    ret = TSDB_CODE_INVALID_MSG;
×
1112
    goto end;
×
1113
  }
1114

1115
  if (ttlReq.nUids != 0) {
×
1116
    vInfo("vgId:%d, drop ttl table req will be processed, time:%d, ntbUids:%d", pVnode->config.vgId,
×
1117
          ttlReq.timestampSec, ttlReq.nUids);
1118
  }
1119

1120
  if (ttlReq.nUids > 0) {
×
1121
    int32_t code = metaDropMultipleTables(pVnode->pMeta, ver, ttlReq.pTbUids);
×
1122
    if (code) return code;
×
1123

1124
    code = tqUpdateTbUidList(pVnode->pTq, ttlReq.pTbUids, false);
×
1125
    if (code) {
×
1126
      vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1127
    }
1128
  }
1129

1130
end:
×
1131
  taosArrayDestroy(ttlReq.pTbUids);
×
1132
  return ret;
×
1133
}
1134

1135
static int32_t vnodeProcessFetchTtlExpiredTbs(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1136
  int32_t                 code = -1;
×
1137
  SMetaReader             mr = {0};
×
1138
  SVDropTtlTableReq       ttlReq = {0};
×
1139
  SVFetchTtlExpiredTbsRsp rsp = {0};
×
1140
  SEncoder                encoder = {0};
×
1141
  SArray                 *pNames = NULL;
×
1142
  pRsp->msgType = TDMT_VND_FETCH_TTL_EXPIRED_TBS_RSP;
×
1143
  pRsp->code = TSDB_CODE_SUCCESS;
×
1144
  pRsp->pCont = NULL;
×
1145
  pRsp->contLen = 0;
×
1146

1147
  if (tDeserializeSVDropTtlTableReq(pReq, len, &ttlReq) != 0) {
×
1148
    terrno = TSDB_CODE_INVALID_MSG;
×
1149
    goto _end;
×
1150
  }
1151

1152
  if (!(ttlReq.nUids == taosArrayGetSize(ttlReq.pTbUids))) {
×
1153
    terrno = TSDB_CODE_INVALID_MSG;
×
1154
    goto _end;
×
1155
  }
1156

1157
  tb_uid_t    suid;
1158
  char        ctbName[TSDB_TABLE_NAME_LEN];
1159
  SVDropTbReq expiredTb = {.igNotExists = true};
×
1160
  metaReaderDoInit(&mr, pVnode->pMeta, 0);
×
1161
  rsp.vgId = TD_VID(pVnode);
×
1162
  rsp.pExpiredTbs = taosArrayInit(ttlReq.nUids, sizeof(SVDropTbReq));
×
1163
  if (!rsp.pExpiredTbs) goto _end;
×
1164

1165
  pNames = taosArrayInit(ttlReq.nUids, TSDB_TABLE_NAME_LEN);
×
1166
  if (!pNames) {
×
1167
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1168
    goto _end;
×
1169
  }
1170
  char buf[TSDB_TABLE_NAME_LEN];
1171
  for (int32_t i = 0; i < ttlReq.nUids; ++i) {
×
1172
    tb_uid_t *uid = taosArrayGet(ttlReq.pTbUids, i);
×
1173
    expiredTb.suid = *uid;
×
1174
    terrno = metaReaderGetTableEntryByUid(&mr, *uid);
×
1175
    if (terrno < 0) goto _end;
×
1176
    tstrncpy(buf, mr.me.name, TSDB_TABLE_NAME_LEN);
×
1177
    void *p = taosArrayPush(pNames, buf);
×
1178
    if (p == NULL) {
×
1179
      goto _end;
×
1180
    }
1181

1182
    expiredTb.name = p;
×
1183
    if (mr.me.type == TSDB_CHILD_TABLE) {
×
1184
      expiredTb.suid = mr.me.ctbEntry.suid;
×
1185
    }
1186

1187
    if (taosArrayPush(rsp.pExpiredTbs, &expiredTb) == NULL) {
×
1188
      goto _end;
×
1189
    }
1190
  }
1191

1192
  int32_t ret = 0;
×
1193
  tEncodeSize(tEncodeVFetchTtlExpiredTbsRsp, &rsp, pRsp->contLen, ret);
×
1194
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
×
1195
  if (pRsp->pCont == NULL) {
×
1196
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1197
    code = -1;
×
1198
    goto _end;
×
1199
  }
1200
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
×
1201
  terrno = tEncodeVFetchTtlExpiredTbsRsp(&encoder, &rsp);
×
1202
  tEncoderClear(&encoder);
×
1203

1204
  if (terrno == 0) code = 0;
×
1205
_end:
×
1206
  metaReaderClear(&mr);
×
1207
  tFreeFetchTtlExpiredTbsRsp(&rsp);
×
1208
  taosArrayDestroy(ttlReq.pTbUids);
×
1209
  if (pNames) taosArrayDestroy(pNames);
×
1210
  pRsp->code = terrno;
×
1211
  return code;
×
1212
}
1213

1214
static int32_t vnodeProcessCreateStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1215
  int32_t        code = 0;
×
1216
  SVCreateStbReq req = {0};
×
1217
  SDecoder       coder;
1218

1219
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
×
1220
  pRsp->code = TSDB_CODE_SUCCESS;
×
1221
  pRsp->pCont = NULL;
×
1222
  pRsp->contLen = 0;
×
1223

1224
  // decode and process req
1225
  tDecoderInit(&coder, pReq, len);
×
1226

1227
  code = tDecodeSVCreateStbReq(&coder, &req);
×
1228
  if (code) {
×
1229
    pRsp->code = code;
×
1230
    goto _err;
×
1231
  }
1232

1233
  code = metaCreateSuperTable(pVnode->pMeta, ver, &req);
×
1234
  if (code) {
×
1235
    pRsp->code = code;
×
1236
    goto _err;
×
1237
  }
1238

1239
  if ((code = tdProcessRSmaCreate(pVnode->pSma, &req)) < 0) {
×
1240
    pRsp->code = code;
×
1241
    goto _err;
×
1242
  }
1243

1244
  tDecoderClear(&coder);
×
1245
  return 0;
×
1246

1247
_err:
×
1248
  tDecoderClear(&coder);
×
1249
  return code;
×
1250
}
1251

1252
static int32_t vnodeProcessCreateTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
×
1253
                                       SRpcMsg *pOriginRpc) {
1254
  SDecoder           decoder = {0};
×
1255
  SEncoder           encoder = {0};
×
1256
  int32_t            rcode = 0;
×
1257
  SVCreateTbBatchReq req = {0};
×
1258
  SVCreateTbReq     *pCreateReq;
1259
  SVCreateTbBatchRsp rsp = {0};
×
1260
  SVCreateTbRsp      cRsp = {0};
×
1261
  char               tbName[TSDB_TABLE_FNAME_LEN];
1262
  STbUidStore       *pStore = NULL;
×
1263
  SArray            *tbUids = NULL;
×
1264
  SArray            *tbNames = NULL;
×
1265
  pRsp->msgType = TDMT_VND_CREATE_TABLE_RSP;
×
1266
  pRsp->code = TSDB_CODE_SUCCESS;
×
1267
  pRsp->pCont = NULL;
×
1268
  pRsp->contLen = 0;
×
1269

1270
  // decode
1271
  tDecoderInit(&decoder, pReq, len);
×
1272
  if (tDecodeSVCreateTbBatchReq(&decoder, &req) < 0) {
×
1273
    rcode = -1;
×
1274
    terrno = TSDB_CODE_INVALID_MSG;
×
1275
    goto _exit;
×
1276
  }
1277

1278
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(cRsp));
×
1279
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
×
1280
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
×
1281
  if (rsp.pArray == NULL || tbUids == NULL || tbNames == NULL) {
×
1282
    rcode = -1;
×
1283
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1284
    goto _exit;
×
1285
  }
1286

1287
  // loop to create table
1288
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
×
1289
    pCreateReq = req.pReqs + iReq;
×
1290
    memset(&cRsp, 0, sizeof(cRsp));
×
1291

1292
    if (tsEnableAudit && tsEnableAuditCreateTable) {
×
1293
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
×
1294
      if (str == NULL) {
×
1295
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1296
        rcode = -1;
×
1297
        goto _exit;
×
1298
      }
1299
      tstrncpy(str, pCreateReq->name, TSDB_TABLE_FNAME_LEN);
×
1300
      if (taosArrayPush(tbNames, &str) == NULL) {
×
1301
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1302
        rcode = -1;
×
1303
        goto _exit;
×
1304
      }
1305
    }
1306

1307
    // validate hash
1308
    (void)tsnprintf(tbName, TSDB_TABLE_FNAME_LEN, "%s.%s", pVnode->config.dbname, pCreateReq->name);
×
1309
    if (vnodeValidateTableHash(pVnode, tbName) < 0) {
×
1310
      cRsp.code = TSDB_CODE_VND_HASH_MISMATCH;
×
1311
      if (taosArrayPush(rsp.pArray, &cRsp) == NULL) {
×
1312
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1313
        rcode = -1;
×
1314
        goto _exit;
×
1315
      }
1316
      vError("vgId:%d create-table:%s failed due to hash value mismatch", TD_VID(pVnode), tbName);
×
1317
      continue;
×
1318
    }
1319

1320
    // do create table
1321
    if (metaCreateTable2(pVnode->pMeta, ver, pCreateReq, &cRsp.pMeta) < 0) {
×
1322
      if (pCreateReq->flags & TD_CREATE_IF_NOT_EXISTS && terrno == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
×
1323
        cRsp.code = TSDB_CODE_SUCCESS;
×
1324
      } else {
1325
        cRsp.code = terrno;
×
1326
      }
1327
    } else {
1328
      cRsp.code = TSDB_CODE_SUCCESS;
×
1329
      if (tdFetchTbUidList(pVnode->pSma, &pStore, pCreateReq->ctb.suid, pCreateReq->uid) < 0) {
×
1330
        vError("vgId:%d, failed to fetch tbUid list", TD_VID(pVnode));
×
1331
      }
1332
      if (taosArrayPush(tbUids, &pCreateReq->uid) == NULL) {
×
1333
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1334
        rcode = -1;
×
1335
        goto _exit;
×
1336
      }
1337
      vnodeUpdateMetaRsp(pVnode, cRsp.pMeta);
×
1338
    }
1339

1340
    if (taosArrayPush(rsp.pArray, &cRsp) == NULL) {
×
1341
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1342
      rcode = -1;
×
1343
      goto _exit;
×
1344
    }
1345
  }
1346

1347
  vDebug("vgId:%d, add %d new created tables into query table list", TD_VID(pVnode), (int32_t)taosArrayGetSize(tbUids));
×
1348
  if (tqUpdateTbUidList(pVnode->pTq, tbUids, true) < 0) {
×
1349
    vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(terrno));
×
1350
  }
1351
  if (tdUpdateTbUidList(pVnode->pSma, pStore, true) < 0) {
×
1352
    goto _exit;
×
1353
  }
1354
  pStore = tdUidStoreFree(pStore);
×
1355

1356
  // prepare rsp
1357
  int32_t ret = 0;
×
1358
  tEncodeSize(tEncodeSVCreateTbBatchRsp, &rsp, pRsp->contLen, ret);
×
1359
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
×
1360
  if (pRsp->pCont == NULL) {
×
1361
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1362
    rcode = -1;
×
1363
    goto _exit;
×
1364
  }
1365
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
×
1366
  if (tEncodeSVCreateTbBatchRsp(&encoder, &rsp) != 0) {
×
1367
    vError("vgId:%d, failed to encode create table batch response", TD_VID(pVnode));
×
1368
  }
1369

1370
  if (tsEnableAudit && tsEnableAuditCreateTable) {
×
1371
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
×
1372

1373
    SName name = {0};
×
1374
    if (tNameFromString(&name, pVnode->config.dbname, T_NAME_ACCT | T_NAME_DB) < 0) {
×
1375
      vError("vgId:%d, failed to get name from string", TD_VID(pVnode));
×
1376
    }
1377

1378
    SStringBuilder sb = {0};
×
1379
    for (int32_t i = 0; i < tbNames->size; i++) {
×
1380
      char **key = (char **)taosArrayGet(tbNames, i);
×
1381
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
×
1382
      if (i < tbNames->size - 1) {
×
1383
        taosStringBuilderAppendChar(&sb, ',');
×
1384
      }
1385
      // taosMemoryFreeClear(*key);
1386
    }
1387

1388
    size_t len = 0;
×
1389
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
×
1390

1391
    if (pOriginRpc->info.conn.user != NULL && strlen(pOriginRpc->info.conn.user) > 0) {
×
1392
      auditAddRecord(pOriginRpc, clusterId, "createTable", name.dbname, "", keyJoined, len);
×
1393
    }
1394

1395
    taosStringBuilderDestroy(&sb);
×
1396
  }
1397

1398
_exit:
×
1399
  tDeleteSVCreateTbBatchReq(&req);
×
1400
  taosArrayDestroyEx(rsp.pArray, tFreeSVCreateTbRsp);
×
1401
  taosArrayDestroy(tbUids);
×
1402
  tDecoderClear(&decoder);
×
1403
  tEncoderClear(&encoder);
×
1404
  taosArrayDestroyP(tbNames, NULL);
×
1405
  return rcode;
×
1406
}
1407

1408
static int32_t vnodeProcessAlterStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1409
  int32_t        code = 0;
×
1410
  SVCreateStbReq req = {0};
×
1411
  SDecoder       dc = {0};
×
1412

1413
  pRsp->msgType = TDMT_VND_ALTER_STB_RSP;
×
1414
  pRsp->code = TSDB_CODE_SUCCESS;
×
1415
  pRsp->pCont = NULL;
×
1416
  pRsp->contLen = 0;
×
1417

1418
  tDecoderInit(&dc, pReq, len);
×
1419

1420
  // decode req
1421
  code = tDecodeSVCreateStbReq(&dc, &req);
×
1422
  if (code) {
×
1423
    tDecoderClear(&dc);
×
1424
    return code;
×
1425
  }
1426

1427
  code = metaAlterSuperTable(pVnode->pMeta, ver, &req);
×
1428
  if (code) {
×
1429
    pRsp->code = code;
×
1430
    tDecoderClear(&dc);
×
1431
    return code;
×
1432
  }
1433

1434
  tDecoderClear(&dc);
×
1435

1436
  return 0;
×
1437
}
1438

1439
static int32_t vnodeProcessDropStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1440
  SVDropStbReq req = {0};
×
1441
  int32_t      rcode = TSDB_CODE_SUCCESS;
×
1442
  SDecoder     decoder = {0};
×
1443
  SArray      *tbUidList = NULL;
×
1444

1445
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
×
1446
  pRsp->pCont = NULL;
×
1447
  pRsp->contLen = 0;
×
1448

1449
  // decode request
1450
  tDecoderInit(&decoder, pReq, len);
×
1451
  if (tDecodeSVDropStbReq(&decoder, &req) < 0) {
×
1452
    rcode = TSDB_CODE_INVALID_MSG;
×
1453
    goto _exit;
×
1454
  }
1455

1456
  // process request
1457
  tbUidList = taosArrayInit(8, sizeof(int64_t));
×
1458
  if (tbUidList == NULL) goto _exit;
×
1459
  if (metaDropSuperTable(pVnode->pMeta, ver, &req) < 0) {
×
1460
    rcode = terrno;
×
1461
    goto _exit;
×
1462
  }
1463

1464
  if (tqUpdateTbUidList(pVnode->pTq, tbUidList, false) < 0) {
×
1465
    rcode = terrno;
×
1466
    goto _exit;
×
1467
  }
1468

1469
  if (tdProcessRSmaDrop(pVnode->pSma, &req) < 0) {
×
1470
    rcode = terrno;
×
1471
    goto _exit;
×
1472
  }
1473

1474
  // return rsp
1475
_exit:
×
1476
  if (tbUidList) taosArrayDestroy(tbUidList);
×
1477
  pRsp->code = rcode;
×
1478
  tDecoderClear(&decoder);
×
1479
  return 0;
×
1480
}
1481

1482
static int32_t vnodeProcessAlterTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1483
  SVAlterTbReq  vAlterTbReq = {0};
×
1484
  SVAlterTbRsp  vAlterTbRsp = {0};
×
1485
  SDecoder      dc = {0};
×
1486
  int32_t       code = 0;
×
1487
  int32_t       lino = 0;
×
1488
  int32_t       ret;
1489
  SEncoder      ec = {0};
×
1490
  STableMetaRsp vMetaRsp = {0};
×
1491

1492
  pRsp->msgType = TDMT_VND_ALTER_TABLE_RSP;
×
1493
  pRsp->pCont = NULL;
×
1494
  pRsp->contLen = 0;
×
1495
  pRsp->code = TSDB_CODE_SUCCESS;
×
1496

1497
  tDecoderInit(&dc, pReq, len);
×
1498

1499
  // decode
1500
  if (tDecodeSVAlterTbReq(&dc, &vAlterTbReq) < 0) {
×
1501
    vAlterTbRsp.code = TSDB_CODE_INVALID_MSG;
×
1502
    tDecoderClear(&dc);
×
1503
    goto _exit;
×
1504
  }
1505

1506
  // process
1507
  if (metaAlterTable(pVnode->pMeta, ver, &vAlterTbReq, &vMetaRsp) < 0) {
×
1508
    vAlterTbRsp.code = terrno;
×
1509
    tDecoderClear(&dc);
×
1510
    goto _exit;
×
1511
  }
1512
  tDecoderClear(&dc);
×
1513

1514
  if (NULL != vMetaRsp.pSchemas) {
×
1515
    vnodeUpdateMetaRsp(pVnode, &vMetaRsp);
×
1516
    vAlterTbRsp.pMeta = &vMetaRsp;
×
1517
  }
1518

1519
  if (vAlterTbReq.action == TSDB_ALTER_TABLE_UPDATE_TAG_VAL || vAlterTbReq.action == TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL) {
×
1520
    int64_t uid = metaGetTableEntryUidByName(pVnode->pMeta, vAlterTbReq.tbName);
×
1521
    if (uid == 0) {
×
1522
      vError("vgId:%d, %s failed at %s:%d since table %s not found", TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
1523
             vAlterTbReq.tbName);
1524
      goto _exit;
×
1525
    }
1526

1527
    SArray* tbUids = taosArrayInit(4, sizeof(int64_t));
×
1528
    void* p = taosArrayPush(tbUids, &uid);
×
1529
    TSDB_CHECK_NULL(p, code, lino, _exit, terrno);
×
1530

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

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

1541
    taosArrayDestroy(tbUids);
×
1542
  }
1543

1544
_exit:
×
1545
  taosArrayDestroy(vAlterTbReq.pMultiTag);
×
1546
  tEncodeSize(tEncodeSVAlterTbRsp, &vAlterTbRsp, pRsp->contLen, ret);
×
1547
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
×
1548
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
×
1549
  if (tEncodeSVAlterTbRsp(&ec, &vAlterTbRsp) != 0) {
×
1550
    vError("vgId:%d, failed to encode alter table response", TD_VID(pVnode));
×
1551
  }
1552

1553
  tEncoderClear(&ec);
×
1554
  if (vMetaRsp.pSchemas) {
×
1555
    taosMemoryFree(vMetaRsp.pSchemas);
×
1556
    taosMemoryFree(vMetaRsp.pSchemaExt);
×
1557
  }
1558
  return 0;
×
1559
}
1560

1561
static int32_t vnodeProcessDropTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
×
1562
                                     SRpcMsg *pOriginRpc) {
1563
  SVDropTbBatchReq req = {0};
×
1564
  SVDropTbBatchRsp rsp = {0};
×
1565
  SDecoder         decoder = {0};
×
1566
  SEncoder         encoder = {0};
×
1567
  int32_t          ret;
1568
  SArray          *tbUids = NULL;
×
1569
  STbUidStore     *pStore = NULL;
×
1570
  SArray          *tbNames = NULL;
×
1571

1572
  pRsp->msgType = TDMT_VND_DROP_TABLE_RSP;
×
1573
  pRsp->pCont = NULL;
×
1574
  pRsp->contLen = 0;
×
1575
  pRsp->code = TSDB_CODE_SUCCESS;
×
1576

1577
  // decode req
1578
  tDecoderInit(&decoder, pReq, len);
×
1579
  ret = tDecodeSVDropTbBatchReq(&decoder, &req);
×
1580
  if (ret < 0) {
×
1581
    terrno = TSDB_CODE_INVALID_MSG;
×
1582
    pRsp->code = terrno;
×
1583
    goto _exit;
×
1584
  }
1585

1586
  // process req
1587
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
×
1588
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(SVDropTbRsp));
×
1589
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
×
1590
  if (tbUids == NULL || rsp.pArray == NULL || tbNames == NULL) goto _exit;
×
1591

1592
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
×
1593
    SVDropTbReq *pDropTbReq = req.pReqs + iReq;
×
1594
    SVDropTbRsp  dropTbRsp = {0};
×
1595
    tb_uid_t     tbUid = 0;
×
1596

1597
    /* code */
1598
    ret = metaDropTable2(pVnode->pMeta, ver, pDropTbReq);
×
1599
    if (ret < 0) {
×
1600
      if (pDropTbReq->igNotExists && terrno == TSDB_CODE_TDB_TABLE_NOT_EXIST) {
×
1601
        dropTbRsp.code = TSDB_CODE_SUCCESS;
×
1602
      } else {
1603
        dropTbRsp.code = terrno;
×
1604
      }
1605
    } else {
1606
      dropTbRsp.code = TSDB_CODE_SUCCESS;
×
1607
      if (tbUid > 0) {
×
1608
        if (tdFetchTbUidList(pVnode->pSma, &pStore, pDropTbReq->suid, tbUid) < 0) {
×
1609
          vError("vgId:%d, failed to fetch tbUid list", TD_VID(pVnode));
×
1610
        }
1611
      }
1612
    }
1613

1614
    if (taosArrayPush(rsp.pArray, &dropTbRsp) == NULL) {
×
1615
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1616
      pRsp->code = terrno;
×
1617
      goto _exit;
×
1618
    }
1619

1620
    if (tsEnableAuditCreateTable) {
×
1621
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
×
1622
      if (str == NULL) {
×
1623
        pRsp->code = terrno;
×
1624
        goto _exit;
×
1625
      }
1626
      tstrncpy(str, pDropTbReq->name, TSDB_TABLE_FNAME_LEN);
×
1627
      if (taosArrayPush(tbNames, &str) == NULL) {
×
1628
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1629
        pRsp->code = terrno;
×
1630
        goto _exit;
×
1631
      }
1632
    }
1633
  }
1634

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

1639
  if (tdUpdateTbUidList(pVnode->pSma, pStore, false) < 0) {
×
1640
    goto _exit;
×
1641
  }
1642

1643
  if (tsEnableAuditCreateTable) {
×
1644
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
×
1645

1646
    SName name = {0};
×
1647
    if (tNameFromString(&name, pVnode->config.dbname, T_NAME_ACCT | T_NAME_DB) != 0) {
×
1648
      vError("vgId:%d, failed to get name from string", TD_VID(pVnode));
×
1649
    }
1650

1651
    SStringBuilder sb = {0};
×
1652
    for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
×
1653
      char **key = (char **)taosArrayGet(tbNames, iReq);
×
1654
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
×
1655
      if (iReq < req.nReqs - 1) {
×
1656
        taosStringBuilderAppendChar(&sb, ',');
×
1657
      }
1658
      taosMemoryFreeClear(*key);
×
1659
    }
1660

1661
    size_t len = 0;
×
1662
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
×
1663

1664
    if (pOriginRpc->info.conn.user != NULL && strlen(pOriginRpc->info.conn.user) > 0) {
×
1665
      auditAddRecord(pOriginRpc, clusterId, "dropTable", name.dbname, "", keyJoined, len);
×
1666
    }
1667

1668
    taosStringBuilderDestroy(&sb);
×
1669
  }
1670

1671
_exit:
×
1672
  taosArrayDestroy(tbUids);
×
1673
  pStore = tdUidStoreFree(pStore);
×
1674
  tDecoderClear(&decoder);
×
1675
  tEncodeSize(tEncodeSVDropTbBatchRsp, &rsp, pRsp->contLen, ret);
×
1676
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
×
1677
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
×
1678
  if (tEncodeSVDropTbBatchRsp(&encoder, &rsp) != 0) {
×
1679
    vError("vgId:%d, failed to encode drop table batch response", TD_VID(pVnode));
×
1680
  }
1681
  tEncoderClear(&encoder);
×
1682
  taosArrayDestroy(rsp.pArray);
×
1683
  taosArrayDestroy(tbNames);
×
1684
  return 0;
×
1685
}
1686

1687
#ifdef BUILD_NO_CALL
1688
static int32_t vnodeDebugPrintSingleSubmitMsg(SMeta *pMeta, SSubmitBlk *pBlock, SSubmitMsgIter *msgIter,
1689
                                              const char *tags) {
1690
  SSubmitBlkIter blkIter = {0};
1691
  STSchema      *pSchema = NULL;
1692
  tb_uid_t       suid = 0;
1693
  STSRow        *row = NULL;
1694
  int32_t        rv = -1;
1695

1696
  tInitSubmitBlkIter(msgIter, pBlock, &blkIter);
1697
  if (blkIter.row == NULL) return 0;
1698

1699
  int32_t code = metaGetTbTSchemaNotNull(pMeta, msgIter->suid, TD_ROW_SVER(blkIter.row), 1,
1700
                                         &pSchema);  // TODO: use the real schema
1701
  if (TSDB_CODE_SUCCESS != code) {
1702
    printf("%s:%d no valid schema\n", tags, __LINE__);
1703
    return code;
1704
  }
1705

1706
  suid = msgIter->suid;
1707
  rv = TD_ROW_SVER(blkIter.row);
1708

1709
  char __tags[128] = {0};
1710
  snprintf(__tags, 128, "%s: uid %" PRIi64 " ", tags, msgIter->uid);
1711
  while ((row = tGetSubmitBlkNext(&blkIter))) {
1712
    tdSRowPrint(row, pSchema, __tags);
1713
  }
1714

1715
  taosMemoryFreeClear(pSchema);
1716

1717
  return TSDB_CODE_SUCCESS;
1718
}
1719
#endif
1720
typedef struct SSubmitReqConvertCxt {
1721
  SSubmitMsgIter msgIter;
1722
  SSubmitBlk    *pBlock;
1723
  SSubmitBlkIter blkIter;
1724
  STSRow        *pRow;
1725
  STSRowIter     rowIter;
1726
  SSubmitTbData *pTbData;
1727
  STSchema      *pTbSchema;
1728
  SArray        *pColValues;
1729
} SSubmitReqConvertCxt;
1730

1731
static int32_t vnodeResetTableCxt(SMeta *pMeta, SSubmitReqConvertCxt *pCxt) {
×
1732
  taosMemoryFreeClear(pCxt->pTbSchema);
×
1733
  int32_t code = metaGetTbTSchemaNotNull(pMeta, pCxt->msgIter.suid > 0 ? pCxt->msgIter.suid : pCxt->msgIter.uid,
×
1734
                                         pCxt->msgIter.sversion, 1, &pCxt->pTbSchema);
1735
  if (TSDB_CODE_SUCCESS != code) {
×
1736
    return code;
×
1737
  }
1738
  tdSTSRowIterInit(&pCxt->rowIter, pCxt->pTbSchema);
×
1739

1740
  tDestroySubmitTbData(pCxt->pTbData, TSDB_MSG_FLG_ENCODE);
×
1741
  if (NULL == pCxt->pTbData) {
×
1742
    pCxt->pTbData = taosMemoryCalloc(1, sizeof(SSubmitTbData));
×
1743
    if (NULL == pCxt->pTbData) {
×
1744
      return terrno;
×
1745
    }
1746
  }
1747
  pCxt->pTbData->flags = 0;
×
1748
  pCxt->pTbData->suid = pCxt->msgIter.suid;
×
1749
  pCxt->pTbData->uid = pCxt->msgIter.uid;
×
1750
  pCxt->pTbData->sver = pCxt->msgIter.sversion;
×
1751
  pCxt->pTbData->pCreateTbReq = NULL;
×
1752
  pCxt->pTbData->aRowP = taosArrayInit(128, POINTER_BYTES);
×
1753
  if (NULL == pCxt->pTbData->aRowP) {
×
1754
    return terrno;
×
1755
  }
1756

1757
  taosArrayDestroy(pCxt->pColValues);
×
1758
  pCxt->pColValues = taosArrayInit(pCxt->pTbSchema->numOfCols, sizeof(SColVal));
×
1759
  if (NULL == pCxt->pColValues) {
×
1760
    return terrno;
×
1761
  }
1762
  for (int32_t i = 0; i < pCxt->pTbSchema->numOfCols; ++i) {
×
1763
    SColVal val = COL_VAL_NONE(pCxt->pTbSchema->columns[i].colId, pCxt->pTbSchema->columns[i].type);
×
1764
    if (taosArrayPush(pCxt->pColValues, &val) == NULL) {
×
1765
      return terrno;
×
1766
    }
1767
  }
1768

1769
  return TSDB_CODE_SUCCESS;
×
1770
}
1771

1772
static void vnodeDestroySubmitReqConvertCxt(SSubmitReqConvertCxt *pCxt) {
×
1773
  taosMemoryFreeClear(pCxt->pTbSchema);
×
1774
  tDestroySubmitTbData(pCxt->pTbData, TSDB_MSG_FLG_ENCODE);
×
1775
  taosMemoryFreeClear(pCxt->pTbData);
×
1776
  taosArrayDestroy(pCxt->pColValues);
×
1777
}
×
1778

1779
static int32_t vnodeCellValConvertToColVal(STColumn *pCol, SCellVal *pCellVal, SColVal *pColVal) {
×
1780
  if (tdValTypeIsNone(pCellVal->valType)) {
×
1781
    pColVal->flag = CV_FLAG_NONE;
×
1782
    return TSDB_CODE_SUCCESS;
×
1783
  }
1784

1785
  if (tdValTypeIsNull(pCellVal->valType)) {
×
1786
    pColVal->flag = CV_FLAG_NULL;
×
1787
    return TSDB_CODE_SUCCESS;
×
1788
  }
1789

1790
  if (IS_VAR_DATA_TYPE(pCol->type)) {
×
1791
    pColVal->value.nData = varDataLen(pCellVal->val);
×
1792
    pColVal->value.pData = (uint8_t *)varDataVal(pCellVal->val);
×
1793
  } else if (TSDB_DATA_TYPE_FLOAT == pCol->type) {
×
1794
    float f = GET_FLOAT_VAL(pCellVal->val);
×
1795
    memcpy(&pColVal->value.val, &f, sizeof(f));
×
1796
  } else if (TSDB_DATA_TYPE_DOUBLE == pCol->type) {
×
1797
    pColVal->value.val = *(int64_t *)pCellVal->val;
×
1798
  } else {
1799
    GET_TYPED_DATA(pColVal->value.val, int64_t, pCol->type, pCellVal->val);
×
1800
  }
1801

1802
  pColVal->flag = CV_FLAG_VALUE;
×
1803
  return TSDB_CODE_SUCCESS;
×
1804
}
1805

1806
static int32_t vnodeTSRowConvertToColValArray(SSubmitReqConvertCxt *pCxt) {
×
1807
  int32_t code = TSDB_CODE_SUCCESS;
×
1808
  tdSTSRowIterReset(&pCxt->rowIter, pCxt->pRow);
×
1809
  for (int32_t i = 0; TSDB_CODE_SUCCESS == code && i < pCxt->pTbSchema->numOfCols; ++i) {
×
1810
    STColumn *pCol = pCxt->pTbSchema->columns + i;
×
1811
    SCellVal  cellVal = {0};
×
1812
    if (!tdSTSRowIterFetch(&pCxt->rowIter, pCol->colId, pCol->type, &cellVal)) {
×
1813
      break;
×
1814
    }
1815
    code = vnodeCellValConvertToColVal(pCol, &cellVal, (SColVal *)taosArrayGet(pCxt->pColValues, i));
×
1816
  }
1817
  return code;
×
1818
}
1819

1820
static int32_t vnodeDecodeCreateTbReq(SSubmitReqConvertCxt *pCxt) {
×
1821
  if (pCxt->msgIter.schemaLen <= 0) {
×
1822
    return TSDB_CODE_SUCCESS;
×
1823
  }
1824

1825
  pCxt->pTbData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
×
1826
  if (NULL == pCxt->pTbData->pCreateTbReq) {
×
1827
    return terrno;
×
1828
  }
1829

1830
  SDecoder decoder = {0};
×
1831
  tDecoderInit(&decoder, (uint8_t *)pCxt->pBlock->data, pCxt->msgIter.schemaLen);
×
1832
  int32_t code = tDecodeSVCreateTbReq(&decoder, pCxt->pTbData->pCreateTbReq);
×
1833
  tDecoderClear(&decoder);
×
1834

1835
  return code;
×
1836
}
1837

1838
static int32_t vnodeSubmitReqConvertToSubmitReq2(SVnode *pVnode, SSubmitReq *pReq, SSubmitReq2 *pReq2) {
×
1839
  pReq2->aSubmitTbData = taosArrayInit(128, sizeof(SSubmitTbData));
×
1840
  if (NULL == pReq2->aSubmitTbData) {
×
1841
    return terrno;
×
1842
  }
1843

1844
  SSubmitReqConvertCxt cxt = {0};
×
1845

1846
  int32_t code = tInitSubmitMsgIter(pReq, &cxt.msgIter);
×
1847
  while (TSDB_CODE_SUCCESS == code) {
×
1848
    code = tGetSubmitMsgNext(&cxt.msgIter, &cxt.pBlock);
×
1849
    if (TSDB_CODE_SUCCESS == code) {
×
1850
      if (NULL == cxt.pBlock) {
×
1851
        break;
×
1852
      }
1853
      code = vnodeResetTableCxt(pVnode->pMeta, &cxt);
×
1854
    }
1855
    if (TSDB_CODE_SUCCESS == code) {
×
1856
      code = tInitSubmitBlkIter(&cxt.msgIter, cxt.pBlock, &cxt.blkIter);
×
1857
    }
1858
    if (TSDB_CODE_SUCCESS == code) {
×
1859
      code = vnodeDecodeCreateTbReq(&cxt);
×
1860
    }
1861
    while (TSDB_CODE_SUCCESS == code && (cxt.pRow = tGetSubmitBlkNext(&cxt.blkIter)) != NULL) {
×
1862
      code = vnodeTSRowConvertToColValArray(&cxt);
×
1863
      if (TSDB_CODE_SUCCESS == code) {
×
1864
        SRow **pNewRow = taosArrayReserve(cxt.pTbData->aRowP, 1);
×
1865
        code = tRowBuild(cxt.pColValues, cxt.pTbSchema, pNewRow);
×
1866
      }
1867
    }
1868
    if (TSDB_CODE_SUCCESS == code) {
×
1869
      code = (NULL == taosArrayPush(pReq2->aSubmitTbData, cxt.pTbData) ? terrno : TSDB_CODE_SUCCESS);
×
1870
    }
1871
    if (TSDB_CODE_SUCCESS == code) {
×
1872
      taosMemoryFreeClear(cxt.pTbData);
×
1873
    }
1874
  }
1875

1876
  vnodeDestroySubmitReqConvertCxt(&cxt);
×
1877
  return code;
×
1878
}
1879

1880
static int32_t vnodeRebuildSubmitReqMsg(SSubmitReq2 *pSubmitReq, void **ppMsg) {
×
1881
  int32_t  code = TSDB_CODE_SUCCESS;
×
1882
  char    *pMsg = NULL;
×
1883
  uint32_t msglen = 0;
×
1884
  tEncodeSize(tEncodeSubmitReq, pSubmitReq, msglen, code);
×
1885
  if (TSDB_CODE_SUCCESS == code) {
×
1886
    pMsg = taosMemoryMalloc(msglen);
×
1887
    if (NULL == pMsg) {
×
1888
      code = terrno;
×
1889
    }
1890
  }
1891
  if (TSDB_CODE_SUCCESS == code) {
×
1892
    SEncoder encoder;
1893
    tEncoderInit(&encoder, (uint8_t *)pMsg, msglen);
×
1894
    code = tEncodeSubmitReq(&encoder, pSubmitReq);
×
1895
    tEncoderClear(&encoder);
×
1896
  }
1897
  if (TSDB_CODE_SUCCESS == code) {
×
1898
    *ppMsg = pMsg;
×
1899
  }
1900
  return code;
×
1901
}
1902

1903
static int32_t vnodeProcessSubmitReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
×
1904
                                     SRpcMsg *pOriginalMsg) {
1905
  int32_t code = 0;
×
1906
  terrno = 0;
×
1907

1908
  SSubmitReq2 *pSubmitReq = &(SSubmitReq2){0};
×
1909
  SSubmitRsp2 *pSubmitRsp = &(SSubmitRsp2){0};
×
1910
  SArray      *newTbUids = NULL;
×
1911
  int32_t      ret;
1912
  SEncoder     ec = {0};
×
1913

1914
  pRsp->code = TSDB_CODE_SUCCESS;
×
1915

1916
  void           *pAllocMsg = NULL;
×
1917
  SSubmitReq2Msg *pMsg = (SSubmitReq2Msg *)pReq;
×
1918
  if (0 == pMsg->version) {
×
1919
    code = vnodeSubmitReqConvertToSubmitReq2(pVnode, (SSubmitReq *)pMsg, pSubmitReq);
×
1920
    if (TSDB_CODE_SUCCESS == code) {
×
1921
      code = vnodeRebuildSubmitReqMsg(pSubmitReq, &pReq);
×
1922
    }
1923
    if (TSDB_CODE_SUCCESS == code) {
×
1924
      pAllocMsg = pReq;
×
1925
    }
1926
    if (TSDB_CODE_SUCCESS != code) {
×
1927
      goto _exit;
×
1928
    }
1929
  } else {
1930
    // decode
1931
    pReq = POINTER_SHIFT(pReq, sizeof(SSubmitReq2Msg));
×
1932
    len -= sizeof(SSubmitReq2Msg);
×
1933
    SDecoder dc = {0};
×
1934
    tDecoderInit(&dc, pReq, len);
×
1935
    if (tDecodeSubmitReq(&dc, pSubmitReq, NULL) < 0) {
×
1936
      code = TSDB_CODE_INVALID_MSG;
×
1937
      goto _exit;
×
1938
    }
1939
    tDecoderClear(&dc);
×
1940
  }
1941

1942
  // scan
1943
  TSKEY now = taosGetTimestamp(pVnode->config.tsdbCfg.precision);
×
1944
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * pVnode->config.tsdbCfg.keep2;
×
1945
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
×
1946
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
×
1947
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
×
1948

1949
    if (pSubmitTbData->pCreateTbReq && pSubmitTbData->pCreateTbReq->uid == 0) {
×
1950
      code = TSDB_CODE_INVALID_MSG;
×
1951
      goto _exit;
×
1952
    }
1953

1954
    if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
×
1955
      if (TARRAY_SIZE(pSubmitTbData->aCol) <= 0) {
×
1956
        code = TSDB_CODE_INVALID_MSG;
×
1957
        goto _exit;
×
1958
      }
1959

1960
      SColData *colDataArr = TARRAY_DATA(pSubmitTbData->aCol);
×
1961
      SRowKey   lastKey;
1962
      tColDataArrGetRowKey(colDataArr, TARRAY_SIZE(pSubmitTbData->aCol), 0, &lastKey);
×
1963
      for (int32_t iRow = 1; iRow < colDataArr[0].nVal; iRow++) {
×
1964
        SRowKey key;
1965
        tColDataArrGetRowKey(TARRAY_DATA(pSubmitTbData->aCol), TARRAY_SIZE(pSubmitTbData->aCol), iRow, &key);
×
1966
        if (tRowKeyCompare(&lastKey, &key) >= 0) {
×
1967
          code = TSDB_CODE_INVALID_MSG;
×
1968
          vError("vgId:%d %s failed 1 since %s, version:%" PRId64, TD_VID(pVnode), __func__, tstrerror(terrno), ver);
×
1969
          goto _exit;
×
1970
        }
1971
      }
1972
    } else {
1973
      int32_t nRow = TARRAY_SIZE(pSubmitTbData->aRowP);
×
1974
      SRow  **aRow = (SRow **)TARRAY_DATA(pSubmitTbData->aRowP);
×
1975
      SRowKey lastRowKey;
1976
      for (int32_t iRow = 0; iRow < nRow; ++iRow) {
×
1977
        if (aRow[iRow]->ts < minKey || aRow[iRow]->ts > maxKey) {
×
1978
          code = TSDB_CODE_INVALID_MSG;
×
1979
          vError("vgId:%d %s failed 2 since %s, version:%" PRId64, TD_VID(pVnode), __func__, tstrerror(code), ver);
×
1980
          goto _exit;
×
1981
        }
1982
        if (iRow == 0) {
×
1983
          tRowGetKey(aRow[iRow], &lastRowKey);
×
1984
        } else {
1985
          SRowKey rowKey;
1986
          tRowGetKey(aRow[iRow], &rowKey);
×
1987

1988
          if (tRowKeyCompare(&lastRowKey, &rowKey) >= 0) {
×
1989
            code = TSDB_CODE_INVALID_MSG;
×
1990
            vError("vgId:%d %s failed 3 since %s, version:%" PRId64, TD_VID(pVnode), __func__, tstrerror(code), ver);
×
1991
            goto _exit;
×
1992
          }
1993
          lastRowKey = rowKey;
×
1994
        }
1995
      }
1996
    }
1997
  }
1998

1999
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
×
2000
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
×
2001

2002
    if (pSubmitTbData->pCreateTbReq) {
×
2003
      pSubmitTbData->uid = pSubmitTbData->pCreateTbReq->uid;
×
2004
    } else {
2005
      SMetaInfo info = {0};
×
2006

2007
      code = metaGetInfo(pVnode->pMeta, pSubmitTbData->uid, &info, NULL);
×
2008
      if (code) {
×
2009
        code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
2010
        vWarn("vgId:%d, table uid:%" PRId64 " not exists", TD_VID(pVnode), pSubmitTbData->uid);
×
2011
        goto _exit;
×
2012
      }
2013

2014
      if (info.suid != pSubmitTbData->suid) {
×
2015
        code = TSDB_CODE_INVALID_MSG;
×
2016
        goto _exit;
×
2017
      }
2018

2019
      if (info.suid) {
×
2020
        if (metaGetInfo(pVnode->pMeta, info.suid, &info, NULL) != 0) {
×
2021
          vWarn("vgId:%d, table uid:%" PRId64 " not exists", TD_VID(pVnode), info.suid);
×
2022
        }
2023
      }
2024

2025
      if (pSubmitTbData->sver != info.skmVer) {
×
2026
        code = TSDB_CODE_TDB_INVALID_TABLE_SCHEMA_VER;
×
2027
        goto _exit;
×
2028
      }
2029
    }
2030

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

2035
      if (nColData <= 0) {
×
2036
        code = TSDB_CODE_INVALID_MSG;
×
2037
        goto _exit;
×
2038
      }
2039

2040
      if (aColData[0].cid != PRIMARYKEY_TIMESTAMP_COL_ID || aColData[0].type != TSDB_DATA_TYPE_TIMESTAMP ||
×
2041
          aColData[0].nVal <= 0) {
×
2042
        code = TSDB_CODE_INVALID_MSG;
×
2043
        goto _exit;
×
2044
      }
2045

2046
      for (int32_t j = 1; j < nColData; j++) {
×
2047
        if (aColData[j].nVal != aColData[0].nVal) {
×
2048
          code = TSDB_CODE_INVALID_MSG;
×
2049
          goto _exit;
×
2050
        }
2051
      }
2052
    }
2053
  }
2054

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

2057
  // loop to handle
2058
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
×
2059
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
×
2060

2061
    // create table
2062
    if (pSubmitTbData->pCreateTbReq) {
×
2063
      // alloc if need
2064
      if (pSubmitRsp->aCreateTbRsp == NULL &&
×
2065
          (pSubmitRsp->aCreateTbRsp = taosArrayInit(TARRAY_SIZE(pSubmitReq->aSubmitTbData), sizeof(SVCreateTbRsp))) ==
×
2066
              NULL) {
2067
        code = terrno;
×
2068
        goto _exit;
×
2069
      }
2070

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

2073
      // create table
2074
      if (metaCreateTable2(pVnode->pMeta, ver, pSubmitTbData->pCreateTbReq, &pCreateTbRsp->pMeta) == 0) {
×
2075
        // create table success
2076

2077
        if (newTbUids == NULL &&
×
2078
            (newTbUids = taosArrayInit(TARRAY_SIZE(pSubmitReq->aSubmitTbData), sizeof(int64_t))) == NULL) {
×
2079
          code = terrno;
×
2080
          goto _exit;
×
2081
        }
2082

2083
        if (taosArrayPush(newTbUids, &pSubmitTbData->uid) == NULL) {
×
2084
          code = terrno;
×
2085
          goto _exit;
×
2086
        }
2087

2088
        if (pCreateTbRsp->pMeta) {
×
2089
          vnodeUpdateMetaRsp(pVnode, pCreateTbRsp->pMeta);
×
2090
        }
2091
      } else {  // create table failed
2092
        if (terrno != TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
×
2093
          code = terrno;
×
2094
          vError("vgId:%d failed to create table:%s, code:%s", TD_VID(pVnode), pSubmitTbData->pCreateTbReq->name,
×
2095
                 tstrerror(terrno));
2096
          goto _exit;
×
2097
        }
2098
        terrno = 0;
×
2099
        pSubmitTbData->uid = pSubmitTbData->pCreateTbReq->uid;  // update uid if table exist for using below
×
2100
      }
2101
    }
2102

2103
    // insert data
2104
    int32_t affectedRows;
2105
    code = tsdbInsertTableData(pVnode->pTsdb, ver, pSubmitTbData, &affectedRows);
×
2106
    if (code) goto _exit;
×
2107

2108
    code = metaUpdateChangeTimeWithLock(pVnode->pMeta, pSubmitTbData->uid, pSubmitTbData->ctimeMs);
×
2109
    if (code) goto _exit;
×
2110

2111
    pSubmitRsp->affectedRows += affectedRows;
×
2112
  }
2113

2114
  // update the affected table uid list
2115
  if (taosArrayGetSize(newTbUids) > 0) {
×
2116
    vDebug("vgId:%d, add %d table into query table list in handling submit", TD_VID(pVnode),
×
2117
           (int32_t)taosArrayGetSize(newTbUids));
2118
    if (tqUpdateTbUidList(pVnode->pTq, newTbUids, true) != 0) {
×
2119
      vError("vgId:%d, failed to update tbUid list", TD_VID(pVnode));
×
2120
    }
2121
  }
2122

2123
_exit:
×
2124
  // message
2125
  pRsp->code = code;
×
2126
  tEncodeSize(tEncodeSSubmitRsp2, pSubmitRsp, pRsp->contLen, ret);
×
2127
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
×
2128
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
×
2129
  if (tEncodeSSubmitRsp2(&ec, pSubmitRsp) < 0) {
×
2130
    vError("vgId:%d, failed to encode submit response", TD_VID(pVnode));
×
2131
  }
2132
  tEncoderClear(&ec);
×
2133

2134
  // update statistics
2135
  (void)atomic_add_fetch_64(&pVnode->statis.nInsert, pSubmitRsp->affectedRows);
×
2136
  (void)atomic_add_fetch_64(&pVnode->statis.nInsertSuccess, pSubmitRsp->affectedRows);
×
2137
  (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsert, 1);
×
2138

2139
  if (tsEnableMonitor && tsMonitorFqdn[0] != 0 && tsMonitorPort != 0 && pSubmitRsp->affectedRows > 0 &&
×
2140
      strlen(pOriginalMsg->info.conn.user) > 0 && tsInsertCounter != NULL) {
×
2141
    const char *sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT_AFFECTED_ROWS,
×
2142
                                   pVnode->monitor.strClusterId,
×
2143
                                   pVnode->monitor.strDnodeId,
×
2144
                                   tsLocalEp,
2145
                                   pVnode->monitor.strVgId,
×
2146
                                   pOriginalMsg->info.conn.user,
×
2147
                                   "Success"};
2148
    int         tv = taos_counter_add(tsInsertCounter, pSubmitRsp->affectedRows, sample_labels);
×
2149
  }
2150

2151
  if (code == 0) {
×
2152
    (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
×
2153
    code = tdProcessRSmaSubmit(pVnode->pSma, ver, pSubmitReq, pReq, len);
×
2154
  }
2155
  /*
2156
  if (code == 0) {
2157
    atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
2158
    code = tdProcessRSmaSubmit(pVnode->pSma, ver, pSubmitReq, pReq, len);
2159

2160
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT, pVnode->monitor.strClusterId,
2161
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2162
                                          pOriginalMsg->info.conn.user, "Success"};
2163
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2164
  }
2165
  else{
2166
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT, pVnode->monitor.strClusterId,
2167
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2168
                                        pOriginalMsg->info.conn.user, "Failed"};
2169
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2170
  }
2171
  */
2172

2173
  // clear
2174
  taosArrayDestroy(newTbUids);
×
2175
  tDestroySubmitReq(pSubmitReq, 0 == pMsg->version ? TSDB_MSG_FLG_CMPT : TSDB_MSG_FLG_DECODE);
×
2176
  tDestroySSubmitRsp2(pSubmitRsp, TSDB_MSG_FLG_ENCODE);
×
2177

2178
  if (code) terrno = code;
×
2179

2180
  taosMemoryFree(pAllocMsg);
×
2181

2182
  return code;
×
2183
}
2184

2185
static int32_t vnodeProcessCreateTSmaReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
2186
  SVCreateTSmaReq req = {0};
×
2187
  SDecoder        coder = {0};
×
2188

2189
  if (pRsp) {
×
2190
    pRsp->msgType = TDMT_VND_CREATE_SMA_RSP;
×
2191
    pRsp->code = TSDB_CODE_SUCCESS;
×
2192
    pRsp->pCont = NULL;
×
2193
    pRsp->contLen = 0;
×
2194
  }
2195

2196
  // decode and process req
2197
  tDecoderInit(&coder, pReq, len);
×
2198

2199
  if (tDecodeSVCreateTSmaReq(&coder, &req) < 0) {
×
2200
    terrno = TSDB_CODE_MSG_DECODE_ERROR;
×
2201
    if (pRsp) pRsp->code = terrno;
×
2202
    goto _err;
×
2203
  }
2204

2205
  if (tdProcessTSmaCreate(pVnode->pSma, ver, (const char *)&req) < 0) {
×
2206
    if (pRsp) pRsp->code = terrno;
×
2207
    goto _err;
×
2208
  }
2209

2210
  tDecoderClear(&coder);
×
2211
  vDebug("vgId:%d, success to create tsma %s:%" PRIi64 " version %" PRIi64 " for table %" PRIi64, TD_VID(pVnode),
×
2212
         req.indexName, req.indexUid, ver, req.tableUid);
2213
  return 0;
×
2214

2215
_err:
×
2216
  tDecoderClear(&coder);
×
2217
  vError("vgId:%d, failed to create tsma %s:%" PRIi64 " version %" PRIi64 "for table %" PRIi64 " since %s",
×
2218
         TD_VID(pVnode), req.indexName, req.indexUid, ver, req.tableUid, terrstr());
2219
  return terrno;
×
2220
}
2221

2222
/**
2223
 * @brief specific for smaDstVnode
2224
 *
2225
 * @param pVnode
2226
 * @param pCont
2227
 * @param contLen
2228
 * @return int32_t
2229
 */
2230
int32_t vnodeProcessCreateTSma(SVnode *pVnode, void *pCont, uint32_t contLen) {
×
2231
  return vnodeProcessCreateTSmaReq(pVnode, 1, pCont, contLen, NULL);
×
2232
}
2233

2234
static int32_t vnodeConsolidateAlterHashRange(SVnode *pVnode, int64_t ver) {
×
2235
  int32_t code = TSDB_CODE_SUCCESS;
×
2236

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

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

2243
  return code;
×
2244
}
2245

2246
static int32_t vnodeProcessAlterConfirmReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
2247
  vInfo("vgId:%d, vnode handle msgType:alter-confirm, alter confirm msg is processed", TD_VID(pVnode));
×
2248
  int32_t code = TSDB_CODE_SUCCESS;
×
2249
  if (!pVnode->config.hashChange) {
×
2250
    goto _exit;
×
2251
  }
2252

2253
  code = vnodeConsolidateAlterHashRange(pVnode, ver);
×
2254
  if (code < 0) {
×
2255
    vError("vgId:%d, failed to consolidate alter hashrange since %s. version:%" PRId64, TD_VID(pVnode), terrstr(), ver);
×
2256
    goto _exit;
×
2257
  }
2258
  pVnode->config.hashChange = false;
×
2259

2260
_exit:
×
2261
  pRsp->msgType = TDMT_VND_ALTER_CONFIRM_RSP;
×
2262
  pRsp->code = code;
×
2263
  pRsp->pCont = NULL;
×
2264
  pRsp->contLen = 0;
×
2265

2266
  return code;
×
2267
}
2268

2269
extern int32_t tsdbDisableAndCancelAllBgTask(STsdb *pTsdb);
2270
extern void    tsdbEnableBgTask(STsdb *pTsdb);
2271

2272
static int32_t vnodeProcessAlterConfigReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
2273
  bool walChanged = false;
×
2274
  bool tsdbChanged = false;
×
2275

2276
  SAlterVnodeConfigReq req = {0};
×
2277
  if (tDeserializeSAlterVnodeConfigReq(pReq, len, &req) != 0) {
×
2278
    terrno = TSDB_CODE_INVALID_MSG;
×
2279
    return TSDB_CODE_INVALID_MSG;
×
2280
  }
2281

2282
  vInfo("vgId:%d, start to alter vnode config, page:%d pageSize:%d buffer:%d szPage:%d szBuf:%" PRIu64
×
2283
        " cacheLast:%d cacheLastSize:%d days:%d keep0:%d keep1:%d keep2:%d keepTimeOffset:%d s3KeepLocal:%d "
2284
        "s3Compact:%d fsync:%d level:%d "
2285
        "walRetentionPeriod:%d walRetentionSize:%d",
2286
        TD_VID(pVnode), req.pages, req.pageSize, req.buffer, req.pageSize * 1024, (uint64_t)req.buffer * 1024 * 1024,
2287
        req.cacheLast, req.cacheLastSize, req.daysPerFile, req.daysToKeep0, req.daysToKeep1, req.daysToKeep2,
2288
        req.keepTimeOffset, req.s3KeepLocal, req.s3Compact, req.walFsyncPeriod, req.walLevel, req.walRetentionPeriod,
2289
        req.walRetentionSize);
2290

2291
  if (pVnode->config.cacheLastSize != req.cacheLastSize) {
×
2292
    pVnode->config.cacheLastSize = req.cacheLastSize;
×
2293
    tsdbCacheSetCapacity(pVnode, (size_t)pVnode->config.cacheLastSize * 1024 * 1024);
×
2294
  }
2295

2296
  if (pVnode->config.szBuf != req.buffer * 1024LL * 1024LL) {
×
2297
    vInfo("vgId:%d, vnode buffer is changed from %" PRId64 " to %" PRId64, TD_VID(pVnode), pVnode->config.szBuf,
×
2298
          (uint64_t)(req.buffer * 1024LL * 1024LL));
2299
    pVnode->config.szBuf = req.buffer * 1024LL * 1024LL;
×
2300
  }
2301

2302
  if (pVnode->config.szCache != req.pages) {
×
2303
    if (metaAlterCache(pVnode->pMeta, req.pages) < 0) {
×
2304
      vError("vgId:%d, failed to change vnode pages from %d to %d failed since %s", TD_VID(pVnode),
×
2305
             pVnode->config.szCache, req.pages, tstrerror(errno));
2306
      return errno;
×
2307
    } else {
2308
      vInfo("vgId:%d, vnode pages is changed from %d to %d", TD_VID(pVnode), pVnode->config.szCache, req.pages);
×
2309
      pVnode->config.szCache = req.pages;
×
2310
    }
2311
  }
2312

2313
  if (pVnode->config.cacheLast != req.cacheLast) {
×
2314
    pVnode->config.cacheLast = req.cacheLast;
×
2315
  }
2316

2317
  if (pVnode->config.walCfg.fsyncPeriod != req.walFsyncPeriod) {
×
2318
    pVnode->config.walCfg.fsyncPeriod = req.walFsyncPeriod;
×
2319
    walChanged = true;
×
2320
  }
2321

2322
  if (pVnode->config.walCfg.level != req.walLevel) {
×
2323
    if (pVnode->config.walCfg.level == 0) {
×
2324
      pVnode->config.walCfg.clearFiles = 1;
×
2325
    }
2326
    pVnode->config.walCfg.level = req.walLevel;
×
2327
    walChanged = true;
×
2328
  }
2329

2330
  if (pVnode->config.walCfg.retentionPeriod != req.walRetentionPeriod) {
×
2331
    pVnode->config.walCfg.retentionPeriod = req.walRetentionPeriod;
×
2332
    walChanged = true;
×
2333
  }
2334

2335
  if (pVnode->config.walCfg.retentionSize != req.walRetentionSize) {
×
2336
    pVnode->config.walCfg.retentionSize = req.walRetentionSize;
×
2337
    walChanged = true;
×
2338
  }
2339

2340
  if (pVnode->config.tsdbCfg.keep0 != req.daysToKeep0) {
×
2341
    pVnode->config.tsdbCfg.keep0 = req.daysToKeep0;
×
2342
    if (!VND_IS_RSMA(pVnode)) {
×
2343
      tsdbChanged = true;
×
2344
    }
2345
  }
2346

2347
  if (pVnode->config.tsdbCfg.keep1 != req.daysToKeep1) {
×
2348
    pVnode->config.tsdbCfg.keep1 = req.daysToKeep1;
×
2349
    if (!VND_IS_RSMA(pVnode)) {
×
2350
      tsdbChanged = true;
×
2351
    }
2352
  }
2353

2354
  if (pVnode->config.tsdbCfg.keep2 != req.daysToKeep2) {
×
2355
    pVnode->config.tsdbCfg.keep2 = req.daysToKeep2;
×
2356
    if (!VND_IS_RSMA(pVnode)) {
×
2357
      tsdbChanged = true;
×
2358
    }
2359
  }
2360

2361
  if (pVnode->config.tsdbCfg.keepTimeOffset != req.keepTimeOffset) {
×
2362
    pVnode->config.tsdbCfg.keepTimeOffset = req.keepTimeOffset;
×
2363
    if (!VND_IS_RSMA(pVnode)) {
×
2364
      tsdbChanged = true;
×
2365
    }
2366
  }
2367

2368
  if (req.sttTrigger != -1 && req.sttTrigger != pVnode->config.sttTrigger) {
×
2369
    if (req.sttTrigger > 1 && pVnode->config.sttTrigger > 1) {
×
2370
      pVnode->config.sttTrigger = req.sttTrigger;
×
2371
    } else {
2372
      vnodeAWait(&pVnode->commitTask);
×
2373

2374
      int32_t ret = tsdbDisableAndCancelAllBgTask(pVnode->pTsdb);
×
2375
      if (ret != 0) {
×
2376
        vError("vgId:%d, failed to disable bg task since %s", TD_VID(pVnode), tstrerror(errno));
×
2377
      }
2378

2379
      pVnode->config.sttTrigger = req.sttTrigger;
×
2380
      tsdbEnableBgTask(pVnode->pTsdb);
×
2381
    }
2382
  }
2383

2384
  if (req.minRows != -1 && req.minRows != pVnode->config.tsdbCfg.minRows) {
×
2385
    pVnode->config.tsdbCfg.minRows = req.minRows;
×
2386
  }
2387

2388
  if (req.s3KeepLocal != -1 && req.s3KeepLocal != pVnode->config.s3KeepLocal) {
×
2389
    pVnode->config.s3KeepLocal = req.s3KeepLocal;
×
2390
  }
2391
  if (req.s3Compact != -1 && req.s3Compact != pVnode->config.s3Compact) {
×
2392
    pVnode->config.s3Compact = req.s3Compact;
×
2393
  }
2394

2395
  if (walChanged) {
×
2396
    if (walAlter(pVnode->pWal, &pVnode->config.walCfg) != 0) {
×
2397
      vError("vgId:%d, failed to alter wal config since %s", TD_VID(pVnode), tstrerror(errno));
×
2398
    }
2399
  }
2400

2401
  if (tsdbChanged) {
×
2402
    tsdbSetKeepCfg(pVnode->pTsdb, &pVnode->config.tsdbCfg);
×
2403
  }
2404

2405
  return 0;
×
2406
}
2407

2408
static int32_t vnodeProcessBatchDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
2409
  SBatchDeleteReq deleteReq;
2410
  SDecoder        decoder;
2411
  tDecoderInit(&decoder, pReq, len);
×
2412
  if (tDecodeSBatchDeleteReq(&decoder, &deleteReq) < 0) {
×
2413
    tDecoderClear(&decoder);
×
2414
    return terrno = TSDB_CODE_INVALID_MSG;
×
2415
  }
2416

2417
  SMetaReader mr = {0};
×
2418
  metaReaderDoInit(&mr, pVnode->pMeta, META_READER_NOLOCK);
×
2419
  STsdb *pTsdb = pVnode->pTsdb;
×
2420

2421
  if (deleteReq.level) {
×
2422
    pTsdb = deleteReq.level == 1 ? VND_RSMA1(pVnode) : VND_RSMA2(pVnode);
×
2423
  }
2424

2425
  int32_t sz = taosArrayGetSize(deleteReq.deleteReqs);
×
2426
  for (int32_t i = 0; i < sz; i++) {
×
2427
    SSingleDeleteReq *pOneReq = taosArrayGet(deleteReq.deleteReqs, i);
×
2428
    char             *name = pOneReq->tbname;
×
2429
    if (metaGetTableEntryByName(&mr, name) < 0) {
×
2430
      vDebug("vgId:%d, stream delete msg, skip since no table: %s", pVnode->config.vgId, name);
×
2431
      continue;
×
2432
    }
2433

2434
    int64_t uid = mr.me.uid;
×
2435

2436
    int32_t code = tsdbDeleteTableData(pTsdb, ver, deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
×
2437
    if (code < 0) {
×
2438
      terrno = code;
×
2439
      vError("vgId:%d, delete error since %s, suid:%" PRId64 ", uid:%" PRId64 ", start ts:%" PRId64 ", end ts:%" PRId64,
×
2440
             TD_VID(pVnode), terrstr(), deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
2441
    }
2442

2443
    if (deleteReq.level == 0) {
×
2444
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, deleteReq.ctimeMs);
×
2445
      if (code < 0) {
×
2446
        terrno = code;
×
2447
        vError("vgId:%d, update change time error since %s, suid:%" PRId64 ", uid:%" PRId64 ", start ts:%" PRId64
×
2448
               ", end ts:%" PRId64,
2449
               TD_VID(pVnode), terrstr(), deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
2450
      }
2451
    }
2452
    tDecoderClear(&mr.coder);
×
2453
  }
2454
  metaReaderClear(&mr);
×
2455
  taosArrayDestroy(deleteReq.deleteReqs);
×
2456
  return 0;
×
2457
}
2458

2459
static int32_t vnodeProcessDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
×
2460
                                     SRpcMsg *pOriginalMsg) {
2461
  int32_t     code = 0;
×
2462
  SDecoder   *pCoder = &(SDecoder){0};
×
2463
  SDeleteRes *pRes = &(SDeleteRes){0};
×
2464

2465
  pRsp->msgType = TDMT_VND_DELETE_RSP;
×
2466
  pRsp->pCont = NULL;
×
2467
  pRsp->contLen = 0;
×
2468
  pRsp->code = TSDB_CODE_SUCCESS;
×
2469

2470
  pRes->uidList = taosArrayInit(0, sizeof(tb_uid_t));
×
2471
  if (pRes->uidList == NULL) {
×
2472
    code = terrno;
×
2473
    goto _err;
×
2474
  }
2475

2476
  tDecoderInit(pCoder, pReq, len);
×
2477
  code = tDecodeDeleteRes(pCoder, pRes);
×
2478
  if (code) goto _err;
×
2479

2480
  if (pRes->affectedRows > 0) {
×
2481
    for (int32_t iUid = 0; iUid < taosArrayGetSize(pRes->uidList); iUid++) {
×
2482
      uint64_t uid = *(uint64_t *)taosArrayGet(pRes->uidList, iUid);
×
2483
      code = tsdbDeleteTableData(pVnode->pTsdb, ver, pRes->suid, uid, pRes->skey, pRes->ekey);
×
2484
      if (code) goto _err;
×
2485
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, pRes->ctimeMs);
×
2486
      if (code) goto _err;
×
2487
    }
2488
  }
2489

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

2492
  tDecoderClear(pCoder);
×
2493
  taosArrayDestroy(pRes->uidList);
×
2494

2495
  SVDeleteRsp rsp = {.affectedRows = pRes->affectedRows};
×
2496
  int32_t     ret = 0;
×
2497
  tEncodeSize(tEncodeSVDeleteRsp, &rsp, pRsp->contLen, ret);
×
2498
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
×
2499
  SEncoder ec = {0};
×
2500
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
×
2501
  code = tEncodeSVDeleteRsp(&ec, &rsp);
×
2502
  if (code) goto _err;
×
2503
  tEncoderClear(&ec);
×
2504
  return code;
×
2505

2506
_err:
×
2507
  /*
2508
  if(code == TSDB_CODE_SUCCESS){
2509
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_DELETE, pVnode->monitor.strClusterId,
2510
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2511
                                        pOriginalMsg->info.conn.user, "Success"};
2512
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2513
  }
2514
  else{
2515
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_DELETE, pVnode->monitor.strClusterId,
2516
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2517
                                        pOriginalMsg->info.conn.user, "Failed"};
2518
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2519
  }
2520
  */
2521

2522
  return code;
×
2523
}
2524
static int32_t vnodeProcessCreateIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
2525
  SVCreateStbReq req = {0};
×
2526
  SDecoder       dc = {0};
×
2527
  int32_t        code = 0;
×
2528

2529
  pRsp->msgType = TDMT_VND_CREATE_INDEX_RSP;
×
2530
  pRsp->code = TSDB_CODE_SUCCESS;
×
2531
  pRsp->pCont = NULL;
×
2532
  pRsp->contLen = 0;
×
2533

2534
  tDecoderInit(&dc, pReq, len);
×
2535
  // decode req
2536
  if (tDecodeSVCreateStbReq(&dc, &req) < 0) {
×
2537
    tDecoderClear(&dc);
×
2538
    return terrno = TSDB_CODE_INVALID_MSG;
×
2539
  }
2540

2541
  code = metaAddIndexToSuperTable(pVnode->pMeta, ver, &req);
×
2542
  if (code) {
×
2543
    pRsp->code = code;
×
2544
    goto _err;
×
2545
  }
2546
  tDecoderClear(&dc);
×
2547
  return 0;
×
2548

2549
_err:
×
2550
  tDecoderClear(&dc);
×
2551
  return code;
×
2552
}
2553
static int32_t vnodeProcessDropIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
2554
  SDropIndexReq req = {0};
×
2555
  int32_t       code = 0;
×
2556
  pRsp->msgType = TDMT_VND_DROP_INDEX_RSP;
×
2557
  pRsp->code = TSDB_CODE_SUCCESS;
×
2558
  pRsp->pCont = NULL;
×
2559
  pRsp->contLen = 0;
×
2560

2561
  if ((code = tDeserializeSDropIdxReq(pReq, len, &req))) {
×
2562
    pRsp->code = code;
×
2563
    return code;
×
2564
  }
2565

2566
  code = metaDropIndexFromSuperTable(pVnode->pMeta, ver, &req);
×
2567
  if (code) {
×
2568
    pRsp->code = code;
×
2569
    return code;
×
2570
  }
2571
  return TSDB_CODE_SUCCESS;
×
2572
}
2573

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

2576
static int32_t vnodeProcessCompactVnodeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
2577
  if (!pVnode->restored) {
×
2578
    vInfo("vgId:%d, ignore compact req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
×
2579
    return 0;
×
2580
  }
2581
  return vnodeAsyncCompact(pVnode, ver, pReq, len, pRsp);
×
2582
}
2583

2584
static int32_t vnodeProcessConfigChangeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
2585
  if (syncCheckMember(pVnode->sync) != 0) {
×
2586
    vError("vgId:%d, failed to check member", TD_VID(pVnode));
×
2587
  }
2588

2589
  pRsp->msgType = TDMT_SYNC_CONFIG_CHANGE_RSP;
×
2590
  pRsp->code = TSDB_CODE_SUCCESS;
×
2591
  pRsp->pCont = NULL;
×
2592
  pRsp->contLen = 0;
×
2593

2594
  return 0;
×
2595
}
2596

2597
static int32_t vnodeCheckToken(SVnode *pVnode, char *member0Token, char *member1Token) {
×
2598
  SSyncState syncState = syncGetState(pVnode->sync);
×
2599
  if (syncState.state != TAOS_SYNC_STATE_LEADER) {
×
2600
    return terrno = TSDB_CODE_SYN_NOT_LEADER;
×
2601
  }
2602

2603
  char token[TSDB_ARB_TOKEN_SIZE] = {0};
×
2604
  if (vnodeGetArbToken(pVnode, token) != 0) {
×
2605
    return terrno = TSDB_CODE_NOT_FOUND;
×
2606
  }
2607

2608
  if (strncmp(token, member0Token, TSDB_ARB_TOKEN_SIZE) != 0 &&
×
2609
      strncmp(token, member1Token, TSDB_ARB_TOKEN_SIZE) != 0) {
×
2610
    return terrno = TSDB_CODE_MND_ARB_TOKEN_MISMATCH;
×
2611
  }
2612

2613
  terrno = TSDB_CODE_SUCCESS;
×
2614
  return 0;
×
2615
}
2616

2617
static int32_t vnodeCheckSyncd(SVnode *pVnode, char *member0Token, char *member1Token) {
×
2618
  int32_t code = vnodeCheckToken(pVnode, member0Token, member1Token);
×
2619
  if (code != 0) {
×
2620
    return code;
×
2621
  }
2622

2623
  return syncCheckSynced(pVnode->sync);
×
2624
}
2625

2626
static int32_t vnodeProcessArbCheckSyncReq(SVnode *pVnode, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
2627
  int32_t code = 0;
×
2628

2629
  SVArbCheckSyncReq syncReq = {0};
×
2630

2631
  code = tDeserializeSVArbCheckSyncReq(pReq, len, &syncReq);
×
2632
  if (code) {
×
2633
    return terrno = code;
×
2634
  }
2635

2636
  pRsp->msgType = TDMT_VND_ARB_CHECK_SYNC_RSP;
×
2637
  pRsp->code = TSDB_CODE_SUCCESS;
×
2638
  pRsp->pCont = NULL;
×
2639
  pRsp->contLen = 0;
×
2640

2641
  SVArbCheckSyncRsp syncRsp = {0};
×
2642
  syncRsp.arbToken = syncReq.arbToken;
×
2643
  syncRsp.member0Token = syncReq.member0Token;
×
2644
  syncRsp.member1Token = syncReq.member1Token;
×
2645
  syncRsp.vgId = TD_VID(pVnode);
×
2646

2647
  if (vnodeCheckSyncd(pVnode, syncReq.member0Token, syncReq.member1Token) != 0) {
×
2648
    vError("vgId:%d, failed to check assigned log syncd", TD_VID(pVnode));
×
2649
  }
2650
  syncRsp.errCode = terrno;
×
2651

2652
  if (vnodeUpdateArbTerm(pVnode, syncReq.arbTerm) != 0) {
×
2653
    vError("vgId:%d, failed to update arb term", TD_VID(pVnode));
×
2654
    code = -1;
×
2655
    goto _OVER;
×
2656
  }
2657

2658
  int32_t contLen = tSerializeSVArbCheckSyncRsp(NULL, 0, &syncRsp);
×
2659
  if (contLen <= 0) {
×
2660
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2661
    code = -1;
×
2662
    goto _OVER;
×
2663
  }
2664
  void *pHead = rpcMallocCont(contLen);
×
2665
  if (!pHead) {
×
2666
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2667
    code = -1;
×
2668
    goto _OVER;
×
2669
  }
2670

2671
  if (tSerializeSVArbCheckSyncRsp(pHead, contLen, &syncRsp) <= 0) {
×
2672
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2673
    rpcFreeCont(pHead);
×
2674
    code = -1;
×
2675
    goto _OVER;
×
2676
  }
2677

2678
  pRsp->pCont = pHead;
×
2679
  pRsp->contLen = contLen;
×
2680

2681
  terrno = TSDB_CODE_SUCCESS;
×
2682

2683
_OVER:
×
2684
  tFreeSVArbCheckSyncReq(&syncReq);
×
2685
  return code;
×
2686
}
2687

2688
#ifndef TD_ENTERPRISE
2689
int32_t vnodeAsyncCompact(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) { return 0; }
2690
int32_t tsdbAsyncCompact(STsdb *tsdb, const STimeWindow *tw, bool sync) { return 0; }
2691
#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