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

taosdata / TDengine / #3798

31 Mar 2025 10:39AM UTC coverage: 9.424% (-20.9%) from 30.372%
#3798

push

travis-ci

happyguoxy
test:add test cases

21549 of 307601 branches covered (7.01%)

Branch coverage included in aggregate %.

36084 of 303967 relevant lines covered (11.87%)

58620.7 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
  taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), uid);
×
91

92
  // btime
93
  taosSetInt64Aligned((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
    taosSetInt64Aligned((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
#ifndef NO_UNALIGNED_ACCESS
357
      if (pRow->ts < minKey || pRow->ts > maxKey) {
×
358
#else
359
      TSKEY ts = taosGetInt64Aligned(&pRow->ts);
360
      if (ts < minKey || ts > maxKey) {
361
#endif
362
        code = TSDB_CODE_TDB_TIMESTAMP_OUT_OF_RANGE;
×
363
        TSDB_CHECK_CODE(code, lino, _exit);
×
364
      }
365
    }
366
  }
367

368
  if (!tDecodeIsEnd(pCoder)) {
×
369
    taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), ctimeMs);
×
370
    pCoder->pos += sizeof(int64_t);
×
371
  }
372

373
  tEndDecode(pCoder);
×
374

375
_exit:
×
376
  if (code) {
×
377
    vError("vgId:%d, %s:%d failed to vnodePreProcessSubmitTbData submit request since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
378
  }
379
  return code;
×
380
}
381
static int32_t vnodePreProcessSubmitMsg(SVnode *pVnode, SRpcMsg *pMsg) {
×
382
  int32_t code = 0;
×
383
  int32_t lino = 0;
×
384

385
  if (tsBypassFlag & TSDB_BYPASS_RA_RPC_RECV_SUBMIT) {
×
386
    return TSDB_CODE_MSG_PREPROCESSED;
×
387
  }
388

389
  SDecoder *pCoder = &(SDecoder){0};
×
390

391
  if (taosHton64(((SSubmitReq2Msg *)pMsg->pCont)->version) != 1) {
×
392
    code = TSDB_CODE_INVALID_MSG;
×
393
    TSDB_CHECK_CODE(code, lino, _exit);
×
394
  }
395

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

398
  if (tStartDecode(pCoder) < 0) {
×
399
    code = TSDB_CODE_INVALID_MSG;
×
400
    TSDB_CHECK_CODE(code, lino, _exit);
×
401
  }
402

403
  uint64_t nSubmitTbData;
404
  if (tDecodeU64v(pCoder, &nSubmitTbData) < 0) {
×
405
    code = TSDB_CODE_INVALID_MSG;
×
406
    TSDB_CHECK_CODE(code, lino, _exit);
×
407
  }
408

409
  int64_t btimeMs = taosGetTimestampMs();
×
410
  int64_t ctimeMs = btimeMs;
×
411
  for (int32_t i = 0; i < nSubmitTbData; i++) {
×
412
    code = vnodePreProcessSubmitTbData(pVnode, pCoder, btimeMs, ctimeMs);
×
413
    TSDB_CHECK_CODE(code, lino, _exit);
×
414
  }
415

416
  tEndDecode(pCoder);
×
417

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

427
static int32_t vnodePreProcessDeleteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
×
428
  int32_t code = 0;
×
429

430
  int32_t    size;
431
  int32_t    ret;
432
  uint8_t   *pCont;
433
  SEncoder  *pCoder = &(SEncoder){0};
×
434
  SDeleteRes res = {0};
×
435

436
  SReadHandle handle = {.vnode = pVnode, .pMsgCb = &pVnode->msgCb, .skipRollup = 1};
×
437
  initStorageAPI(&handle.api);
×
438

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

442
  res.ctimeMs = taosGetTimestampMs();
×
443
  // malloc and encode
444
  tEncodeSize(tEncodeDeleteRes, &res, size, ret);
×
445
  pCont = rpcMallocCont(size + sizeof(SMsgHead));
×
446

447
  ((SMsgHead *)pCont)->contLen = size + sizeof(SMsgHead);
×
448
  ((SMsgHead *)pCont)->vgId = TD_VID(pVnode);
×
449

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

456
  rpcFreeCont(pMsg->pCont);
×
457
  pMsg->pCont = pCont;
×
458
  pMsg->contLen = size + sizeof(SMsgHead);
×
459

460
  taosArrayDestroy(res.uidList);
×
461

462
_exit:
×
463
  return code;
×
464
}
465

466
static int32_t vnodePreProcessBatchDeleteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
×
467
  int32_t code = 0;
×
468
  int32_t lino = 0;
×
469

470
  int64_t         ctimeMs = taosGetTimestampMs();
×
471
  SBatchDeleteReq pReq = {0};
×
472
  SDecoder       *pCoder = &(SDecoder){0};
×
473

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

476
  if (tDecodeSBatchDeleteReqSetCtime(pCoder, &pReq, ctimeMs) < 0) {
×
477
    code = TSDB_CODE_INVALID_MSG;
×
478
  }
479

480
  tDecoderClear(pCoder);
×
481
  taosArrayDestroy(pReq.deleteReqs);
×
482

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

491
static int32_t vnodePreProcessArbCheckSyncMsg(SVnode *pVnode, SRpcMsg *pMsg) {
×
492
  SVArbCheckSyncReq syncReq = {0};
×
493

494
  if (tDeserializeSVArbCheckSyncReq((char *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead),
×
495
                                    &syncReq) != 0) {
496
    return TSDB_CODE_INVALID_MSG;
×
497
  }
498

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

504
  int32_t code = terrno;
×
505
  tFreeSVArbCheckSyncReq(&syncReq);
×
506

507
  return code;
×
508
}
509

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

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

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

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

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

557
_exit:
×
558
  tDecoderClear(&dc);
×
559
  if (sentBatchReqs.pArray) {
×
560
    taosArrayDestroy(sentBatchReqs.pArray);
×
561
  }
562
  return code;
×
563
}
564

565
int32_t vnodePreProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
×
566
  int32_t code = 0;
×
567

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

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

605
int32_t vnodeProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg, int64_t ver, SRpcMsg *pRsp) {
×
606
  int32_t code = 0;
×
607
  void   *ptr = NULL;
×
608
  void   *pReq;
609
  int32_t len;
610

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

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

625
  vGDebug(&pMsg->info.traceId,
×
626
          "vgId:%d, index:%" PRId64 ", process write request:%s, applied:%" PRId64 ", state.applyTerm:%" PRId64
627
          ", conn.applyTerm:%" PRId64 ", contLen:%d",
628
          TD_VID(pVnode), ver, TMSG_INFO(pMsg->msgType), pVnode->state.applied, pVnode->state.applyTerm,
629
          pMsg->info.conn.applyTerm, pMsg->contLen);
630

631
  if (!(pVnode->state.applyTerm <= pMsg->info.conn.applyTerm)) {
×
632
    return terrno = TSDB_CODE_INTERNAL_ERROR;
×
633
  }
634

635
  if (!(pVnode->state.applied + 1 == ver)) {
×
636
    return terrno = TSDB_CODE_INTERNAL_ERROR;
×
637
  }
638

639
  atomic_store_64(&pVnode->state.applied, ver);
×
640
  atomic_store_64(&pVnode->state.applyTerm, pMsg->info.conn.applyTerm);
×
641

642
  if (!syncUtilUserCommit(pMsg->msgType)) goto _exit;
×
643

644
  // skip header
645
  pReq = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
×
646
  len = pMsg->contLen - sizeof(SMsgHead);
×
647
  bool needCommit = false;
×
648

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

741
    } break;
×
742
    case TDMT_STREAM_TASK_PAUSE: {
×
743
      if (pVnode->restored && vnodeIsLeader(pVnode) &&
×
744
          (code = tqProcessTaskPauseReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen)) < 0) {
×
745
        goto _err;
×
746
      }
747
    } break;
×
748
    case TDMT_STREAM_TASK_RESUME: {
×
749
      if (pVnode->restored && vnodeIsLeader(pVnode) &&
×
750
          (code = tqProcessTaskResumeReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen)) < 0) {
×
751
        goto _err;
×
752
      }
753
    } break;
×
754
    case TDMT_VND_STREAM_TASK_RESET: {
×
755
      if (pVnode->restored && vnodeIsLeader(pVnode) && (code = tqProcessTaskResetReq(pVnode->pTq, pMsg)) < 0) {
×
756
        goto _err;
×
757
      }
758

759
    } break;
×
760
    case TDMT_VND_STREAM_ALL_STOP: {
×
761
      if (pVnode->restored && vnodeIsLeader(pVnode) && (code = tqProcessAllTaskStopReq(pVnode->pTq, pMsg)) < 0) {
×
762
        goto _err;
×
763
      }
764

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

811
  vGDebug(&pMsg->info.traceId, "vgId:%d, index:%" PRId64 ", msg processed, code:0x%x", TD_VID(pVnode), ver, pRsp->code);
×
812

813
  walApplyVer(pVnode->pWal, ver);
×
814

815
  code = tqPushMsg(pVnode->pTq, pMsg->msgType);
×
816
  if (code) {
×
817
    vError("vgId:%d, failed to push msg to TQ since %s", TD_VID(pVnode), tstrerror(terrno));
×
818
    return code;
×
819
  }
820

821
  // commit if need
822
  if (needCommit) {
×
823
    vInfo("vgId:%d, commit at version %" PRId64, TD_VID(pVnode), ver);
×
824
    code = vnodeAsyncCommit(pVnode);
×
825
    if (code) {
×
826
      vError("vgId:%d, failed to vnode async commit since %s.", TD_VID(pVnode), tstrerror(terrno));
×
827
      goto _err;
×
828
    }
829

830
    // start a new one
831
    code = vnodeBegin(pVnode);
×
832
    if (code) {
×
833
      vError("vgId:%d, failed to begin vnode since %s.", TD_VID(pVnode), tstrerror(terrno));
×
834
      goto _err;
×
835
    }
836
  }
837

838
_exit:
×
839
  return 0;
×
840

841
_err:
×
842
  vError("vgId:%d, process %s request failed since %s, ver:%" PRId64, TD_VID(pVnode), TMSG_INFO(pMsg->msgType),
×
843
         tstrerror(terrno), ver);
844
  return code;
×
845
}
846

847
int32_t vnodePreprocessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg) {
×
848
  if (TDMT_SCH_QUERY != pMsg->msgType && TDMT_SCH_MERGE_QUERY != pMsg->msgType) {
×
849
    return 0;
×
850
  }
851

852
  return qWorkerPreprocessQueryMsg(pVnode->pQuery, pMsg, TDMT_SCH_QUERY == pMsg->msgType);
×
853
}
854

855
int32_t vnodeProcessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
×
856
  vTrace("message in vnode query queue is processing");
×
857
  if (pMsg->msgType == TDMT_VND_TMQ_CONSUME && !syncIsReadyForRead(pVnode->sync)) {
×
858
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
×
859
    return 0;
×
860
  }
861

862
  if (pMsg->msgType == TDMT_VND_TMQ_CONSUME && !pVnode->restored) {
×
863
    vnodeRedirectRpcMsg(pVnode, pMsg, TSDB_CODE_SYN_RESTORING);
×
864
    return 0;
×
865
  }
866

867
  SReadHandle handle = {.vnode = pVnode, .pMsgCb = &pVnode->msgCb, .pWorkerCb = pInfo->workerCb};
×
868
  initStorageAPI(&handle.api);
×
869
  int32_t code = TSDB_CODE_SUCCESS;
×
870
  bool    redirected = false;
×
871

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

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

907
  switch (pMsg->msgType) {
×
908
    case TDMT_SCH_FETCH:
×
909
    case TDMT_SCH_MERGE_FETCH:
910
      return qWorkerProcessFetchMsg(pVnode, pVnode->pQuery, pMsg, 0);
×
911
    case TDMT_SCH_FETCH_RSP:
×
912
      return qWorkerProcessRspMsg(pVnode, pVnode->pQuery, pMsg, 0);
×
913
    // case TDMT_SCH_CANCEL_TASK:
914
    //   return qWorkerProcessCancelMsg(pVnode, pVnode->pQuery, pMsg, 0);
915
    case TDMT_SCH_DROP_TASK:
×
916
      return qWorkerProcessDropMsg(pVnode, pVnode->pQuery, pMsg, 0);
×
917
    case TDMT_SCH_TASK_NOTIFY:
×
918
      return qWorkerProcessNotifyMsg(pVnode, pVnode->pQuery, pMsg, 0);
×
919
    case TDMT_SCH_QUERY_HEARTBEAT:
×
920
      return qWorkerProcessHbMsg(pVnode, pVnode->pQuery, pMsg, 0);
×
921
    case TDMT_VND_TABLE_META:
×
922
    case TDMT_VND_TABLE_NAME:
923
      return vnodeGetTableMeta(pVnode, pMsg, true);
×
924
    case TDMT_VND_TABLE_CFG:
×
925
      return vnodeGetTableCfg(pVnode, pMsg, true);
×
926
    case TDMT_VND_BATCH_META:
×
927
      return vnodeGetBatchMeta(pVnode, pMsg);
×
928
    case TDMT_VND_VSUBTABLES_META:
×
929
      return vnodeGetVSubtablesMeta(pVnode, pMsg);
×
930
#ifdef TD_ENTERPRISE
931
    case TDMT_VND_QUERY_COMPACT_PROGRESS:
×
932
      return vnodeQueryCompactProgress(pVnode, pMsg);
×
933
#endif
934
      //    case TDMT_VND_TMQ_CONSUME:
935
      //      return tqProcessPollReq(pVnode->pTq, pMsg);
936
#ifdef USE_TQ
937
    case TDMT_VND_TMQ_VG_WALINFO:
×
938
      return tqProcessVgWalInfoReq(pVnode->pTq, pMsg);
×
939
    case TDMT_VND_TMQ_VG_COMMITTEDINFO:
×
940
      return tqProcessVgCommittedInfoReq(pVnode->pTq, pMsg);
×
941
    case TDMT_VND_TMQ_SEEK:
×
942
      return tqProcessSeekReq(pVnode->pTq, pMsg);
×
943
#endif
944
    default:
×
945
      vError("unknown msg type:%d in fetch queue", pMsg->msgType);
×
946
      return TSDB_CODE_APP_ERROR;
×
947
  }
948
}
949
#ifdef USE_STREAM
950
int32_t vnodeProcessStreamMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
×
951
  vTrace("vgId:%d, msg:%p in stream queue is processing", pVnode->config.vgId, pMsg);
×
952

953
  // todo: NOTE: some command needs to run on follower, such as, stop_all_tasks
954
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
×
955
       pMsg->msgType == TDMT_VND_BATCH_META) &&
×
956
      !syncIsReadyForRead(pVnode->sync)) {
×
957
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
×
958
    return 0;
×
959
  }
960

961
  switch (pMsg->msgType) {
×
962
    case TDMT_STREAM_TASK_RUN:
×
963
      return tqProcessTaskRunReq(pVnode->pTq, pMsg);
×
964
    case TDMT_STREAM_RETRIEVE:
×
965
      return tqProcessTaskRetrieveReq(pVnode->pTq, pMsg);
×
966
    case TDMT_STREAM_RETRIEVE_RSP:
×
967
      return tqProcessTaskRetrieveRsp(pVnode->pTq, pMsg);
×
968
    case TDMT_VND_GET_STREAM_PROGRESS:
×
969
      return tqStreamProgressRetrieveReq(pVnode->pTq, pMsg);
×
970
    default:
×
971
      vError("unknown msg type:%d in stream queue", pMsg->msgType);
×
972
      return TSDB_CODE_APP_ERROR;
×
973
  }
974
}
975

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

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

1014
int32_t vnodeProcessStreamLongExecMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
×
1015
  vTrace("vgId:%d, msg:%p in stream long exec queue is processing", pVnode->config.vgId, pMsg);
×
1016
  if (!syncIsReadyForRead(pVnode->sync)) {
×
1017
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
×
1018
    return 0;
×
1019
  }
1020

1021
  switch (pMsg->msgType) {
×
1022
    case TDMT_VND_STREAM_SCAN_HISTORY:
×
1023
      return tqProcessTaskScanHistory(pVnode->pTq, pMsg);
×
1024
    default:
×
1025
      vError("unknown msg type:%d in stream long exec queue", pMsg->msgType);
×
1026
      return TSDB_CODE_APP_ERROR;
×
1027
  }
1028
}
1029

1030
int32_t vnodeProcessStreamChkptMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
×
1031
  vTrace("vgId:%d, msg:%p in stream chkpt queue is processing", pVnode->config.vgId, pMsg);
×
1032
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
×
1033
       pMsg->msgType == TDMT_VND_BATCH_META) &&
×
1034
      !syncIsReadyForRead(pVnode->sync)) {
×
1035
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
×
1036
    return 0;
×
1037
  }
1038

1039
  switch (pMsg->msgType) {
×
1040
    case TDMT_STREAM_CHKPT_EXEC:
×
1041
      return tqProcessTaskRunReq(pVnode->pTq, pMsg);
×
1042
    default:
×
1043
      vError("unknown msg type:%d in stream chkpt queue", pMsg->msgType);
×
1044
      return TSDB_CODE_APP_ERROR;
×
1045
  }
1046
}
1047
#endif
1048

1049
void smaHandleRes(void *pVnode, int64_t smaId, const SArray *data) {
×
1050
  int32_t code = tdProcessTSmaInsert(((SVnode *)pVnode)->pSma, smaId, (const char *)data);
×
1051
  if (code) {
×
1052
    vError("failed to process sma result since %s", tstrerror(code));
×
1053
  }
1054
}
×
1055

1056
void vnodeUpdateMetaRsp(SVnode *pVnode, STableMetaRsp *pMetaRsp) {
×
1057
  if (NULL == pMetaRsp) {
×
1058
    return;
×
1059
  }
1060

1061
  tstrncpy(pMetaRsp->dbFName, pVnode->config.dbname, TSDB_DB_FNAME_LEN);
×
1062
  pMetaRsp->dbId = pVnode->config.dbId;
×
1063
  pMetaRsp->vgId = TD_VID(pVnode);
×
1064
  pMetaRsp->precision = pVnode->config.tsdbCfg.precision;
×
1065
}
1066

1067
extern int32_t vnodeAsyncRetention(SVnode *pVnode, int64_t now);
1068

1069
static int32_t vnodeProcessTrimReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1070
  if (!pVnode->restored) {
×
1071
    vInfo("vgId:%d, ignore trim req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
×
1072
    return 0;
×
1073
  }
1074

1075
  int32_t     code = 0;
×
1076
  SVTrimDbReq trimReq = {0};
×
1077

1078
  // decode
1079
  if (tDeserializeSVTrimDbReq(pReq, len, &trimReq) != 0) {
×
1080
    code = TSDB_CODE_INVALID_MSG;
×
1081
    goto _exit;
×
1082
  }
1083

1084
  vInfo("vgId:%d, process trim vnode request, time:%d", pVnode->config.vgId, trimReq.timestamp);
×
1085

1086
  code = vnodeAsyncRetention(pVnode, trimReq.timestamp);
×
1087

1088
_exit:
×
1089
  return code;
×
1090
}
1091

1092
extern int32_t vnodeAsyncS3Migrate(SVnode *pVnode, int64_t now);
1093

1094
static int32_t vnodeProcessS3MigrateReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1095
  int32_t          code = 0;
×
1096
  SVS3MigrateDbReq s3migrateReq = {0};
×
1097

1098
  // decode
1099
  if (tDeserializeSVS3MigrateDbReq(pReq, len, &s3migrateReq) != 0) {
×
1100
    code = TSDB_CODE_INVALID_MSG;
×
1101
    goto _exit;
×
1102
  }
1103

1104
  vInfo("vgId:%d, process s3migrate vnode request, time:%d", pVnode->config.vgId, s3migrateReq.timestamp);
×
1105

1106
  code = vnodeAsyncS3Migrate(pVnode, s3migrateReq.timestamp);
×
1107

1108
_exit:
×
1109
  return code;
×
1110
}
1111

1112
static int32_t vnodeProcessDropTtlTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1113
  int               ret = 0;
×
1114
  SVDropTtlTableReq ttlReq = {0};
×
1115
  if (tDeserializeSVDropTtlTableReq(pReq, len, &ttlReq) != 0) {
×
1116
    ret = TSDB_CODE_INVALID_MSG;
×
1117
    goto end;
×
1118
  }
1119

1120
  if (ttlReq.nUids != taosArrayGetSize(ttlReq.pTbUids)) {
×
1121
    ret = TSDB_CODE_INVALID_MSG;
×
1122
    goto end;
×
1123
  }
1124

1125
  if (ttlReq.nUids != 0) {
×
1126
    vInfo("vgId:%d, process drop ttl table request, time:%d, ntbUids:%d", pVnode->config.vgId,
×
1127
          ttlReq.timestampSec, ttlReq.nUids);
1128
  }
1129

1130
  if (ttlReq.nUids > 0) {
×
1131
    int32_t code = metaDropMultipleTables(pVnode->pMeta, ver, ttlReq.pTbUids);
×
1132
    if (code) return code;
×
1133

1134
    code = tqUpdateTbUidList(pVnode->pTq, ttlReq.pTbUids, false);
×
1135
    if (code) {
×
1136
      vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1137
    }
1138
  }
1139

1140
end:
×
1141
  taosArrayDestroy(ttlReq.pTbUids);
×
1142
  return ret;
×
1143
}
1144

1145
static int32_t vnodeProcessFetchTtlExpiredTbs(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1146
  int32_t                 code = -1;
×
1147
  SMetaReader             mr = {0};
×
1148
  SVDropTtlTableReq       ttlReq = {0};
×
1149
  SVFetchTtlExpiredTbsRsp rsp = {0};
×
1150
  SEncoder                encoder = {0};
×
1151
  SArray                 *pNames = NULL;
×
1152
  pRsp->msgType = TDMT_VND_FETCH_TTL_EXPIRED_TBS_RSP;
×
1153
  pRsp->code = TSDB_CODE_SUCCESS;
×
1154
  pRsp->pCont = NULL;
×
1155
  pRsp->contLen = 0;
×
1156

1157
  if (tDeserializeSVDropTtlTableReq(pReq, len, &ttlReq) != 0) {
×
1158
    terrno = TSDB_CODE_INVALID_MSG;
×
1159
    goto _end;
×
1160
  }
1161

1162
  if (!(ttlReq.nUids == taosArrayGetSize(ttlReq.pTbUids))) {
×
1163
    terrno = TSDB_CODE_INVALID_MSG;
×
1164
    goto _end;
×
1165
  }
1166

1167
  tb_uid_t    suid;
1168
  char        ctbName[TSDB_TABLE_NAME_LEN];
1169
  SVDropTbReq expiredTb = {.igNotExists = true};
×
1170
  metaReaderDoInit(&mr, pVnode->pMeta, 0);
×
1171
  rsp.vgId = TD_VID(pVnode);
×
1172
  rsp.pExpiredTbs = taosArrayInit(ttlReq.nUids, sizeof(SVDropTbReq));
×
1173
  if (!rsp.pExpiredTbs) goto _end;
×
1174

1175
  pNames = taosArrayInit(ttlReq.nUids, TSDB_TABLE_NAME_LEN);
×
1176
  if (!pNames) {
×
1177
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1178
    goto _end;
×
1179
  }
1180
  char buf[TSDB_TABLE_NAME_LEN];
1181
  for (int32_t i = 0; i < ttlReq.nUids; ++i) {
×
1182
    tb_uid_t *uid = taosArrayGet(ttlReq.pTbUids, i);
×
1183
    expiredTb.suid = *uid;
×
1184
    terrno = metaReaderGetTableEntryByUid(&mr, *uid);
×
1185
    if (terrno < 0) goto _end;
×
1186
    tstrncpy(buf, mr.me.name, TSDB_TABLE_NAME_LEN);
×
1187
    void *p = taosArrayPush(pNames, buf);
×
1188
    if (p == NULL) {
×
1189
      goto _end;
×
1190
    }
1191

1192
    expiredTb.name = p;
×
1193
    if (mr.me.type == TSDB_CHILD_TABLE) {
×
1194
      expiredTb.suid = mr.me.ctbEntry.suid;
×
1195
    }
1196

1197
    if (taosArrayPush(rsp.pExpiredTbs, &expiredTb) == NULL) {
×
1198
      goto _end;
×
1199
    }
1200
  }
1201

1202
  int32_t ret = 0;
×
1203
  tEncodeSize(tEncodeVFetchTtlExpiredTbsRsp, &rsp, pRsp->contLen, ret);
×
1204
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
×
1205
  if (pRsp->pCont == NULL) {
×
1206
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1207
    code = -1;
×
1208
    goto _end;
×
1209
  }
1210
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
×
1211
  terrno = tEncodeVFetchTtlExpiredTbsRsp(&encoder, &rsp);
×
1212
  tEncoderClear(&encoder);
×
1213

1214
  if (terrno == 0) code = 0;
×
1215
_end:
×
1216
  metaReaderClear(&mr);
×
1217
  tFreeFetchTtlExpiredTbsRsp(&rsp);
×
1218
  taosArrayDestroy(ttlReq.pTbUids);
×
1219
  if (pNames) taosArrayDestroy(pNames);
×
1220
  pRsp->code = terrno;
×
1221
  return code;
×
1222
}
1223

1224
static int32_t vnodeProcessCreateStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1225
  int32_t        code = 0;
×
1226
  SVCreateStbReq req = {0};
×
1227
  SDecoder       coder;
1228

1229
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
×
1230
  pRsp->code = TSDB_CODE_SUCCESS;
×
1231
  pRsp->pCont = NULL;
×
1232
  pRsp->contLen = 0;
×
1233

1234
  // decode and process req
1235
  tDecoderInit(&coder, pReq, len);
×
1236

1237
  code = tDecodeSVCreateStbReq(&coder, &req);
×
1238
  if (code) {
×
1239
    pRsp->code = code;
×
1240
    goto _err;
×
1241
  }
1242

1243
  code = metaCreateSuperTable(pVnode->pMeta, ver, &req);
×
1244
  if (code) {
×
1245
    pRsp->code = code;
×
1246
    goto _err;
×
1247
  }
1248

1249
  if ((code = tdProcessRSmaCreate(pVnode->pSma, &req)) < 0) {
×
1250
    pRsp->code = code;
×
1251
    goto _err;
×
1252
  }
1253

1254
  tDecoderClear(&coder);
×
1255
  return 0;
×
1256

1257
_err:
×
1258
  tDecoderClear(&coder);
×
1259
  return code;
×
1260
}
1261

1262
static int32_t vnodeProcessCreateTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
×
1263
                                       SRpcMsg *pOriginRpc) {
1264
  SDecoder           decoder = {0};
×
1265
  SEncoder           encoder = {0};
×
1266
  int32_t            rcode = 0;
×
1267
  SVCreateTbBatchReq req = {0};
×
1268
  SVCreateTbReq     *pCreateReq;
1269
  SVCreateTbBatchRsp rsp = {0};
×
1270
  SVCreateTbRsp      cRsp = {0};
×
1271
  char               tbName[TSDB_TABLE_FNAME_LEN];
1272
  STbUidStore       *pStore = NULL;
×
1273
  SArray            *tbUids = NULL;
×
1274
  SArray            *tbNames = NULL;
×
1275
  pRsp->msgType = TDMT_VND_CREATE_TABLE_RSP;
×
1276
  pRsp->code = TSDB_CODE_SUCCESS;
×
1277
  pRsp->pCont = NULL;
×
1278
  pRsp->contLen = 0;
×
1279

1280
  // decode
1281
  tDecoderInit(&decoder, pReq, len);
×
1282
  if (tDecodeSVCreateTbBatchReq(&decoder, &req) < 0) {
×
1283
    rcode = -1;
×
1284
    terrno = TSDB_CODE_INVALID_MSG;
×
1285
    goto _exit;
×
1286
  }
1287

1288
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(cRsp));
×
1289
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
×
1290
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
×
1291
  if (rsp.pArray == NULL || tbUids == NULL || tbNames == NULL) {
×
1292
    rcode = -1;
×
1293
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1294
    goto _exit;
×
1295
  }
1296

1297
  // loop to create table
1298
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
×
1299
    pCreateReq = req.pReqs + iReq;
×
1300
    memset(&cRsp, 0, sizeof(cRsp));
×
1301

1302
    if (tsEnableAudit && tsEnableAuditCreateTable) {
×
1303
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
×
1304
      if (str == NULL) {
×
1305
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1306
        rcode = -1;
×
1307
        goto _exit;
×
1308
      }
1309
      tstrncpy(str, pCreateReq->name, TSDB_TABLE_FNAME_LEN);
×
1310
      if (taosArrayPush(tbNames, &str) == NULL) {
×
1311
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1312
        rcode = -1;
×
1313
        goto _exit;
×
1314
      }
1315
    }
1316

1317
    // validate hash
1318
    (void)tsnprintf(tbName, TSDB_TABLE_FNAME_LEN, "%s.%s", pVnode->config.dbname, pCreateReq->name);
×
1319
    if (vnodeValidateTableHash(pVnode, tbName) < 0) {
×
1320
      cRsp.code = TSDB_CODE_VND_HASH_MISMATCH;
×
1321
      if (taosArrayPush(rsp.pArray, &cRsp) == NULL) {
×
1322
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1323
        rcode = -1;
×
1324
        goto _exit;
×
1325
      }
1326
      vError("vgId:%d create-table:%s failed due to hash value mismatch", TD_VID(pVnode), tbName);
×
1327
      continue;
×
1328
    }
1329

1330
    // do create table
1331
    if (metaCreateTable2(pVnode->pMeta, ver, pCreateReq, &cRsp.pMeta) < 0) {
×
1332
      if (pCreateReq->flags & TD_CREATE_IF_NOT_EXISTS && terrno == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
×
1333
        cRsp.code = TSDB_CODE_SUCCESS;
×
1334
      } else {
1335
        cRsp.code = terrno;
×
1336
      }
1337
    } else {
1338
      cRsp.code = TSDB_CODE_SUCCESS;
×
1339
      if (tdFetchTbUidList(pVnode->pSma, &pStore, pCreateReq->ctb.suid, pCreateReq->uid) < 0) {
×
1340
        vError("vgId:%d, failed to fetch tbUid list", TD_VID(pVnode));
×
1341
      }
1342
      if (taosArrayPush(tbUids, &pCreateReq->uid) == NULL) {
×
1343
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1344
        rcode = -1;
×
1345
        goto _exit;
×
1346
      }
1347
      vnodeUpdateMetaRsp(pVnode, cRsp.pMeta);
×
1348
    }
1349

1350
    if (taosArrayPush(rsp.pArray, &cRsp) == NULL) {
×
1351
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1352
      rcode = -1;
×
1353
      goto _exit;
×
1354
    }
1355
  }
1356

1357
  vTrace("vgId:%d, add %d new created tables into query table list", TD_VID(pVnode), (int32_t)taosArrayGetSize(tbUids));
×
1358
  if (tqUpdateTbUidList(pVnode->pTq, tbUids, true) < 0) {
×
1359
    vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(terrno));
×
1360
  }
1361
  if (tdUpdateTbUidList(pVnode->pSma, pStore, true) < 0) {
×
1362
    goto _exit;
×
1363
  }
1364
  pStore = tdUidStoreFree(pStore);
×
1365

1366
  // prepare rsp
1367
  int32_t ret = 0;
×
1368
  tEncodeSize(tEncodeSVCreateTbBatchRsp, &rsp, pRsp->contLen, ret);
×
1369
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
×
1370
  if (pRsp->pCont == NULL) {
×
1371
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1372
    rcode = -1;
×
1373
    goto _exit;
×
1374
  }
1375
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
×
1376
  if (tEncodeSVCreateTbBatchRsp(&encoder, &rsp) != 0) {
×
1377
    vError("vgId:%d, failed to encode create table batch response", TD_VID(pVnode));
×
1378
  }
1379

1380
  if (tsEnableAudit && tsEnableAuditCreateTable) {
×
1381
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
×
1382

1383
    SName name = {0};
×
1384
    if (tNameFromString(&name, pVnode->config.dbname, T_NAME_ACCT | T_NAME_DB) < 0) {
×
1385
      vError("vgId:%d, failed to get name from string", TD_VID(pVnode));
×
1386
    }
1387

1388
    SStringBuilder sb = {0};
×
1389
    for (int32_t i = 0; i < tbNames->size; i++) {
×
1390
      char **key = (char **)taosArrayGet(tbNames, i);
×
1391
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
×
1392
      if (i < tbNames->size - 1) {
×
1393
        taosStringBuilderAppendChar(&sb, ',');
×
1394
      }
1395
      // taosMemoryFreeClear(*key);
1396
    }
1397

1398
    size_t len = 0;
×
1399
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
×
1400

1401
    if (pOriginRpc->info.conn.user != NULL && strlen(pOriginRpc->info.conn.user) > 0) {
×
1402
      auditAddRecord(pOriginRpc, clusterId, "createTable", name.dbname, "", keyJoined, len);
×
1403
    }
1404

1405
    taosStringBuilderDestroy(&sb);
×
1406
  }
1407

1408
_exit:
×
1409
  tDeleteSVCreateTbBatchReq(&req);
×
1410
  taosArrayDestroyEx(rsp.pArray, tFreeSVCreateTbRsp);
×
1411
  taosArrayDestroy(tbUids);
×
1412
  tDecoderClear(&decoder);
×
1413
  tEncoderClear(&encoder);
×
1414
  taosArrayDestroyP(tbNames, NULL);
×
1415
  return rcode;
×
1416
}
1417

1418
static int32_t vnodeProcessAlterStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1419
  int32_t        code = 0;
×
1420
  SVCreateStbReq req = {0};
×
1421
  SDecoder       dc = {0};
×
1422

1423
  pRsp->msgType = TDMT_VND_ALTER_STB_RSP;
×
1424
  pRsp->code = TSDB_CODE_SUCCESS;
×
1425
  pRsp->pCont = NULL;
×
1426
  pRsp->contLen = 0;
×
1427

1428
  tDecoderInit(&dc, pReq, len);
×
1429

1430
  // decode req
1431
  code = tDecodeSVCreateStbReq(&dc, &req);
×
1432
  if (code) {
×
1433
    tDecoderClear(&dc);
×
1434
    return code;
×
1435
  }
1436

1437
  code = metaAlterSuperTable(pVnode->pMeta, ver, &req);
×
1438
  if (code) {
×
1439
    pRsp->code = code;
×
1440
    tDecoderClear(&dc);
×
1441
    return code;
×
1442
  }
1443

1444
  tDecoderClear(&dc);
×
1445

1446
  return 0;
×
1447
}
1448

1449
static int32_t vnodeProcessDropStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1450
  SVDropStbReq req = {0};
×
1451
  int32_t      rcode = TSDB_CODE_SUCCESS;
×
1452
  SDecoder     decoder = {0};
×
1453
  SArray      *tbUidList = NULL;
×
1454

1455
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
×
1456
  pRsp->pCont = NULL;
×
1457
  pRsp->contLen = 0;
×
1458

1459
  // decode request
1460
  tDecoderInit(&decoder, pReq, len);
×
1461
  if (tDecodeSVDropStbReq(&decoder, &req) < 0) {
×
1462
    rcode = TSDB_CODE_INVALID_MSG;
×
1463
    goto _exit;
×
1464
  }
1465

1466
  // process request
1467
  tbUidList = taosArrayInit(8, sizeof(int64_t));
×
1468
  if (tbUidList == NULL) goto _exit;
×
1469
  if (metaDropSuperTable(pVnode->pMeta, ver, &req) < 0) {
×
1470
    rcode = terrno;
×
1471
    goto _exit;
×
1472
  }
1473

1474
  if (tqUpdateTbUidList(pVnode->pTq, tbUidList, false) < 0) {
×
1475
    rcode = terrno;
×
1476
    goto _exit;
×
1477
  }
1478

1479
  if (tdProcessRSmaDrop(pVnode->pSma, &req) < 0) {
×
1480
    rcode = terrno;
×
1481
    goto _exit;
×
1482
  }
1483

1484
  // return rsp
1485
_exit:
×
1486
  if (tbUidList) taosArrayDestroy(tbUidList);
×
1487
  pRsp->code = rcode;
×
1488
  tDecoderClear(&decoder);
×
1489
  return 0;
×
1490
}
1491

1492
static int32_t vnodeProcessAlterTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1493
  SVAlterTbReq  vAlterTbReq = {0};
×
1494
  SVAlterTbRsp  vAlterTbRsp = {0};
×
1495
  SDecoder      dc = {0};
×
1496
  int32_t       code = 0;
×
1497
  int32_t       lino = 0;
×
1498
  int32_t       ret;
1499
  SEncoder      ec = {0};
×
1500
  STableMetaRsp vMetaRsp = {0};
×
1501

1502
  pRsp->msgType = TDMT_VND_ALTER_TABLE_RSP;
×
1503
  pRsp->pCont = NULL;
×
1504
  pRsp->contLen = 0;
×
1505
  pRsp->code = TSDB_CODE_SUCCESS;
×
1506

1507
  tDecoderInit(&dc, pReq, len);
×
1508

1509
  // decode
1510
  if (tDecodeSVAlterTbReq(&dc, &vAlterTbReq) < 0) {
×
1511
    vAlterTbRsp.code = TSDB_CODE_INVALID_MSG;
×
1512
    tDecoderClear(&dc);
×
1513
    goto _exit;
×
1514
  }
1515

1516
  // process
1517
  if (metaAlterTable(pVnode->pMeta, ver, &vAlterTbReq, &vMetaRsp) < 0) {
×
1518
    vAlterTbRsp.code = terrno;
×
1519
    tDecoderClear(&dc);
×
1520
    goto _exit;
×
1521
  }
1522
  tDecoderClear(&dc);
×
1523

1524
  if (NULL != vMetaRsp.pSchemas) {
×
1525
    vnodeUpdateMetaRsp(pVnode, &vMetaRsp);
×
1526
    vAlterTbRsp.pMeta = &vMetaRsp;
×
1527
  }
1528

1529
  if (vAlterTbReq.action == TSDB_ALTER_TABLE_UPDATE_TAG_VAL || vAlterTbReq.action == TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL) {
×
1530
    int64_t uid = metaGetTableEntryUidByName(pVnode->pMeta, vAlterTbReq.tbName);
×
1531
    if (uid == 0) {
×
1532
      vError("vgId:%d, %s failed at %s:%d since table %s not found", TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
1533
             vAlterTbReq.tbName);
1534
      goto _exit;
×
1535
    }
1536

1537
    SArray* tbUids = taosArrayInit(4, sizeof(int64_t));
×
1538
    void* p = taosArrayPush(tbUids, &uid);
×
1539
    TSDB_CHECK_NULL(p, code, lino, _exit, terrno);
×
1540

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

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

1551
    taosArrayDestroy(tbUids);
×
1552
  }
1553

1554
_exit:
×
1555
  taosArrayDestroy(vAlterTbReq.pMultiTag);
×
1556
  tEncodeSize(tEncodeSVAlterTbRsp, &vAlterTbRsp, pRsp->contLen, ret);
×
1557
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
×
1558
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
×
1559
  if (tEncodeSVAlterTbRsp(&ec, &vAlterTbRsp) != 0) {
×
1560
    vError("vgId:%d, failed to encode alter table response", TD_VID(pVnode));
×
1561
  }
1562

1563
  tEncoderClear(&ec);
×
1564
  if (vMetaRsp.pSchemas) {
×
1565
    taosMemoryFree(vMetaRsp.pSchemas);
×
1566
    taosMemoryFree(vMetaRsp.pSchemaExt);
×
1567
  }
1568
  if (vMetaRsp.pColRefs) {
×
1569
    taosMemoryFree(vMetaRsp.pColRefs);
×
1570
  }
1571
  return 0;
×
1572
}
1573

1574
static int32_t vnodeProcessDropTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
×
1575
                                     SRpcMsg *pOriginRpc) {
1576
  SVDropTbBatchReq req = {0};
×
1577
  SVDropTbBatchRsp rsp = {0};
×
1578
  SDecoder         decoder = {0};
×
1579
  SEncoder         encoder = {0};
×
1580
  int32_t          ret;
1581
  SArray          *tbUids = NULL;
×
1582
  STbUidStore     *pStore = NULL;
×
1583
  SArray          *tbNames = NULL;
×
1584

1585
  pRsp->msgType = TDMT_VND_DROP_TABLE_RSP;
×
1586
  pRsp->pCont = NULL;
×
1587
  pRsp->contLen = 0;
×
1588
  pRsp->code = TSDB_CODE_SUCCESS;
×
1589

1590
  // decode req
1591
  tDecoderInit(&decoder, pReq, len);
×
1592
  ret = tDecodeSVDropTbBatchReq(&decoder, &req);
×
1593
  if (ret < 0) {
×
1594
    terrno = TSDB_CODE_INVALID_MSG;
×
1595
    pRsp->code = terrno;
×
1596
    goto _exit;
×
1597
  }
1598

1599
  // process req
1600
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
×
1601
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(SVDropTbRsp));
×
1602
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
×
1603
  if (tbUids == NULL || rsp.pArray == NULL || tbNames == NULL) goto _exit;
×
1604

1605
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
×
1606
    SVDropTbReq *pDropTbReq = req.pReqs + iReq;
×
1607
    SVDropTbRsp  dropTbRsp = {0};
×
1608
    tb_uid_t     tbUid = 0;
×
1609

1610
    /* code */
1611
    ret = metaDropTable2(pVnode->pMeta, ver, pDropTbReq);
×
1612
    if (ret < 0) {
×
1613
      if (pDropTbReq->igNotExists && terrno == TSDB_CODE_TDB_TABLE_NOT_EXIST) {
×
1614
        dropTbRsp.code = TSDB_CODE_SUCCESS;
×
1615
      } else {
1616
        dropTbRsp.code = terrno;
×
1617
      }
1618
    } else {
1619
      dropTbRsp.code = TSDB_CODE_SUCCESS;
×
1620
      if (tbUid > 0) {
×
1621
        if (tdFetchTbUidList(pVnode->pSma, &pStore, pDropTbReq->suid, tbUid) < 0) {
×
1622
          vError("vgId:%d, failed to fetch tbUid list", TD_VID(pVnode));
×
1623
        }
1624
      }
1625
    }
1626

1627
    if (taosArrayPush(rsp.pArray, &dropTbRsp) == NULL) {
×
1628
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1629
      pRsp->code = terrno;
×
1630
      goto _exit;
×
1631
    }
1632

1633
    if (tsEnableAuditCreateTable) {
×
1634
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
×
1635
      if (str == NULL) {
×
1636
        pRsp->code = terrno;
×
1637
        goto _exit;
×
1638
      }
1639
      tstrncpy(str, pDropTbReq->name, TSDB_TABLE_FNAME_LEN);
×
1640
      if (taosArrayPush(tbNames, &str) == NULL) {
×
1641
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1642
        pRsp->code = terrno;
×
1643
        goto _exit;
×
1644
      }
1645
    }
1646
  }
1647

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

1652
  if (tdUpdateTbUidList(pVnode->pSma, pStore, false) < 0) {
×
1653
    goto _exit;
×
1654
  }
1655

1656
  if (tsEnableAuditCreateTable) {
×
1657
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
×
1658

1659
    SName name = {0};
×
1660
    if (tNameFromString(&name, pVnode->config.dbname, T_NAME_ACCT | T_NAME_DB) != 0) {
×
1661
      vError("vgId:%d, failed to get name from string", TD_VID(pVnode));
×
1662
    }
1663

1664
    SStringBuilder sb = {0};
×
1665
    for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
×
1666
      char **key = (char **)taosArrayGet(tbNames, iReq);
×
1667
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
×
1668
      if (iReq < req.nReqs - 1) {
×
1669
        taosStringBuilderAppendChar(&sb, ',');
×
1670
      }
1671
      taosMemoryFreeClear(*key);
×
1672
    }
1673

1674
    size_t len = 0;
×
1675
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
×
1676

1677
    if (pOriginRpc->info.conn.user != NULL && strlen(pOriginRpc->info.conn.user) > 0) {
×
1678
      auditAddRecord(pOriginRpc, clusterId, "dropTable", name.dbname, "", keyJoined, len);
×
1679
    }
1680

1681
    taosStringBuilderDestroy(&sb);
×
1682
  }
1683

1684
_exit:
×
1685
  taosArrayDestroy(tbUids);
×
1686
  pStore = tdUidStoreFree(pStore);
×
1687
  tDecoderClear(&decoder);
×
1688
  tEncodeSize(tEncodeSVDropTbBatchRsp, &rsp, pRsp->contLen, ret);
×
1689
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
×
1690
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
×
1691
  if (tEncodeSVDropTbBatchRsp(&encoder, &rsp) != 0) {
×
1692
    vError("vgId:%d, failed to encode drop table batch response", TD_VID(pVnode));
×
1693
  }
1694
  tEncoderClear(&encoder);
×
1695
  taosArrayDestroy(rsp.pArray);
×
1696
  taosArrayDestroy(tbNames);
×
1697
  return 0;
×
1698
}
1699

1700
#ifdef BUILD_NO_CALL
1701
static int32_t vnodeDebugPrintSingleSubmitMsg(SMeta *pMeta, SSubmitBlk *pBlock, SSubmitMsgIter *msgIter,
1702
                                              const char *tags) {
1703
  SSubmitBlkIter blkIter = {0};
1704
  STSchema      *pSchema = NULL;
1705
  tb_uid_t       suid = 0;
1706
  STSRow        *row = NULL;
1707
  int32_t        rv = -1;
1708

1709
  tInitSubmitBlkIter(msgIter, pBlock, &blkIter);
1710
  if (blkIter.row == NULL) return 0;
1711

1712
  int32_t code = metaGetTbTSchemaNotNull(pMeta, msgIter->suid, TD_ROW_SVER(blkIter.row), 1,
1713
                                         &pSchema);  // TODO: use the real schema
1714
  if (TSDB_CODE_SUCCESS != code) {
1715
    printf("%s:%d no valid schema\n", tags, __LINE__);
1716
    return code;
1717
  }
1718

1719
  suid = msgIter->suid;
1720
  rv = TD_ROW_SVER(blkIter.row);
1721

1722
  char __tags[128] = {0};
1723
  snprintf(__tags, 128, "%s: uid %" PRIi64 " ", tags, msgIter->uid);
1724
  while ((row = tGetSubmitBlkNext(&blkIter))) {
1725
    tdSRowPrint(row, pSchema, __tags);
1726
  }
1727

1728
  taosMemoryFreeClear(pSchema);
1729

1730
  return TSDB_CODE_SUCCESS;
1731
}
1732
#endif
1733
typedef struct SSubmitReqConvertCxt {
1734
  SSubmitMsgIter msgIter;
1735
  SSubmitBlk    *pBlock;
1736
  SSubmitBlkIter blkIter;
1737
  STSRow        *pRow;
1738
  STSRowIter     rowIter;
1739
  SSubmitTbData *pTbData;
1740
  STSchema      *pTbSchema;
1741
  SArray        *pColValues;
1742
} SSubmitReqConvertCxt;
1743

1744
static int32_t vnodeResetTableCxt(SMeta *pMeta, SSubmitReqConvertCxt *pCxt) {
×
1745
  taosMemoryFreeClear(pCxt->pTbSchema);
×
1746
  int32_t code = metaGetTbTSchemaNotNull(pMeta, pCxt->msgIter.suid > 0 ? pCxt->msgIter.suid : pCxt->msgIter.uid,
×
1747
                                         pCxt->msgIter.sversion, 1, &pCxt->pTbSchema);
1748
  if (TSDB_CODE_SUCCESS != code) {
×
1749
    return code;
×
1750
  }
1751
  tdSTSRowIterInit(&pCxt->rowIter, pCxt->pTbSchema);
×
1752

1753
  tDestroySubmitTbData(pCxt->pTbData, TSDB_MSG_FLG_ENCODE);
×
1754
  if (NULL == pCxt->pTbData) {
×
1755
    pCxt->pTbData = taosMemoryCalloc(1, sizeof(SSubmitTbData));
×
1756
    if (NULL == pCxt->pTbData) {
×
1757
      return terrno;
×
1758
    }
1759
  }
1760
  pCxt->pTbData->flags = 0;
×
1761
  pCxt->pTbData->suid = pCxt->msgIter.suid;
×
1762
  pCxt->pTbData->uid = pCxt->msgIter.uid;
×
1763
  pCxt->pTbData->sver = pCxt->msgIter.sversion;
×
1764
  pCxt->pTbData->pCreateTbReq = NULL;
×
1765
  pCxt->pTbData->aRowP = taosArrayInit(128, POINTER_BYTES);
×
1766
  if (NULL == pCxt->pTbData->aRowP) {
×
1767
    return terrno;
×
1768
  }
1769

1770
  taosArrayDestroy(pCxt->pColValues);
×
1771
  pCxt->pColValues = taosArrayInit(pCxt->pTbSchema->numOfCols, sizeof(SColVal));
×
1772
  if (NULL == pCxt->pColValues) {
×
1773
    return terrno;
×
1774
  }
1775
  for (int32_t i = 0; i < pCxt->pTbSchema->numOfCols; ++i) {
×
1776
    SColVal val = COL_VAL_NONE(pCxt->pTbSchema->columns[i].colId, pCxt->pTbSchema->columns[i].type);
×
1777
    if (taosArrayPush(pCxt->pColValues, &val) == NULL) {
×
1778
      return terrno;
×
1779
    }
1780
  }
1781

1782
  return TSDB_CODE_SUCCESS;
×
1783
}
1784

1785
static void vnodeDestroySubmitReqConvertCxt(SSubmitReqConvertCxt *pCxt) {
×
1786
  taosMemoryFreeClear(pCxt->pTbSchema);
×
1787
  tDestroySubmitTbData(pCxt->pTbData, TSDB_MSG_FLG_ENCODE);
×
1788
  taosMemoryFreeClear(pCxt->pTbData);
×
1789
  taosArrayDestroy(pCxt->pColValues);
×
1790
}
×
1791

1792
static int32_t vnodeCellValConvertToColVal(STColumn *pCol, SCellVal *pCellVal, SColVal *pColVal) {
×
1793
  if (tdValTypeIsNone(pCellVal->valType)) {
×
1794
    pColVal->flag = CV_FLAG_NONE;
×
1795
    return TSDB_CODE_SUCCESS;
×
1796
  }
1797

1798
  if (tdValTypeIsNull(pCellVal->valType)) {
×
1799
    pColVal->flag = CV_FLAG_NULL;
×
1800
    return TSDB_CODE_SUCCESS;
×
1801
  }
1802

1803
  if (IS_VAR_DATA_TYPE(pCol->type)) {
×
1804
    pColVal->value.nData = varDataLen(pCellVal->val);
×
1805
    pColVal->value.pData = (uint8_t *)varDataVal(pCellVal->val);
×
1806
  } else if (TSDB_DATA_TYPE_FLOAT == pCol->type) {
×
1807
    float f = GET_FLOAT_VAL(pCellVal->val);
×
1808
    valueSetDatum(&pColVal->value, pCol->type, &f, sizeof(f));
×
1809
  } else if (TSDB_DATA_TYPE_DOUBLE == pCol->type) {
×
1810
    taosSetPInt64Aligned(&pColVal->value.val, (int64_t *)pCellVal->val);
×
1811
  } else {
1812
    valueSetDatum(&pColVal->value, pCol->type, pCellVal->val, tDataTypes[pCol->type].bytes);
×
1813
  }
1814

1815
  pColVal->flag = CV_FLAG_VALUE;
×
1816
  return TSDB_CODE_SUCCESS;
×
1817
}
1818

1819
static int32_t vnodeTSRowConvertToColValArray(SSubmitReqConvertCxt *pCxt) {
×
1820
  int32_t code = TSDB_CODE_SUCCESS;
×
1821
  tdSTSRowIterReset(&pCxt->rowIter, pCxt->pRow);
×
1822
  for (int32_t i = 0; TSDB_CODE_SUCCESS == code && i < pCxt->pTbSchema->numOfCols; ++i) {
×
1823
    STColumn *pCol = pCxt->pTbSchema->columns + i;
×
1824
    SCellVal  cellVal = {0};
×
1825
    if (!tdSTSRowIterFetch(&pCxt->rowIter, pCol->colId, pCol->type, &cellVal)) {
×
1826
      break;
×
1827
    }
1828
    code = vnodeCellValConvertToColVal(pCol, &cellVal, (SColVal *)taosArrayGet(pCxt->pColValues, i));
×
1829
  }
1830
  return code;
×
1831
}
1832

1833
static int32_t vnodeDecodeCreateTbReq(SSubmitReqConvertCxt *pCxt) {
×
1834
  if (pCxt->msgIter.schemaLen <= 0) {
×
1835
    return TSDB_CODE_SUCCESS;
×
1836
  }
1837

1838
  pCxt->pTbData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
×
1839
  if (NULL == pCxt->pTbData->pCreateTbReq) {
×
1840
    return terrno;
×
1841
  }
1842

1843
  SDecoder decoder = {0};
×
1844
  tDecoderInit(&decoder, (uint8_t *)pCxt->pBlock->data, pCxt->msgIter.schemaLen);
×
1845
  int32_t code = tDecodeSVCreateTbReq(&decoder, pCxt->pTbData->pCreateTbReq);
×
1846
  tDecoderClear(&decoder);
×
1847

1848
  return code;
×
1849
}
1850

1851
static int32_t vnodeSubmitReqConvertToSubmitReq2(SVnode *pVnode, SSubmitReq *pReq, SSubmitReq2 *pReq2) {
×
1852
  pReq2->aSubmitTbData = taosArrayInit(128, sizeof(SSubmitTbData));
×
1853
  if (NULL == pReq2->aSubmitTbData) {
×
1854
    return terrno;
×
1855
  }
1856

1857
  SSubmitReqConvertCxt cxt = {0};
×
1858

1859
  int32_t code = tInitSubmitMsgIter(pReq, &cxt.msgIter);
×
1860
  while (TSDB_CODE_SUCCESS == code) {
×
1861
    code = tGetSubmitMsgNext(&cxt.msgIter, &cxt.pBlock);
×
1862
    if (TSDB_CODE_SUCCESS == code) {
×
1863
      if (NULL == cxt.pBlock) {
×
1864
        break;
×
1865
      }
1866
      code = vnodeResetTableCxt(pVnode->pMeta, &cxt);
×
1867
    }
1868
    if (TSDB_CODE_SUCCESS == code) {
×
1869
      code = tInitSubmitBlkIter(&cxt.msgIter, cxt.pBlock, &cxt.blkIter);
×
1870
    }
1871
    if (TSDB_CODE_SUCCESS == code) {
×
1872
      code = vnodeDecodeCreateTbReq(&cxt);
×
1873
    }
1874
    while (TSDB_CODE_SUCCESS == code && (cxt.pRow = tGetSubmitBlkNext(&cxt.blkIter)) != NULL) {
×
1875
      code = vnodeTSRowConvertToColValArray(&cxt);
×
1876
      if (TSDB_CODE_SUCCESS == code) {
×
1877
        SRow **pNewRow = taosArrayReserve(cxt.pTbData->aRowP, 1);
×
1878
        code = tRowBuild(cxt.pColValues, cxt.pTbSchema, pNewRow);
×
1879
      }
1880
    }
1881
    if (TSDB_CODE_SUCCESS == code) {
×
1882
      code = (NULL == taosArrayPush(pReq2->aSubmitTbData, cxt.pTbData) ? terrno : TSDB_CODE_SUCCESS);
×
1883
    }
1884
    if (TSDB_CODE_SUCCESS == code) {
×
1885
      taosMemoryFreeClear(cxt.pTbData);
×
1886
    }
1887
  }
1888

1889
  vnodeDestroySubmitReqConvertCxt(&cxt);
×
1890
  return code;
×
1891
}
1892

1893
static int32_t vnodeRebuildSubmitReqMsg(SSubmitReq2 *pSubmitReq, void **ppMsg) {
×
1894
  int32_t  code = TSDB_CODE_SUCCESS;
×
1895
  char    *pMsg = NULL;
×
1896
  uint32_t msglen = 0;
×
1897
  tEncodeSize(tEncodeSubmitReq, pSubmitReq, msglen, code);
×
1898
  if (TSDB_CODE_SUCCESS == code) {
×
1899
    pMsg = taosMemoryMalloc(msglen);
×
1900
    if (NULL == pMsg) {
×
1901
      code = terrno;
×
1902
    }
1903
  }
1904
  if (TSDB_CODE_SUCCESS == code) {
×
1905
    SEncoder encoder;
1906
    tEncoderInit(&encoder, (uint8_t *)pMsg, msglen);
×
1907
    code = tEncodeSubmitReq(&encoder, pSubmitReq);
×
1908
    tEncoderClear(&encoder);
×
1909
  }
1910
  if (TSDB_CODE_SUCCESS == code) {
×
1911
    *ppMsg = pMsg;
×
1912
  }
1913
  return code;
×
1914
}
1915

1916
static int32_t vnodeProcessSubmitReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
×
1917
                                     SRpcMsg *pOriginalMsg) {
1918
  int32_t code = 0;
×
1919
  terrno = 0;
×
1920

1921
  SSubmitReq2 *pSubmitReq = &(SSubmitReq2){0};
×
1922
  SSubmitRsp2 *pSubmitRsp = &(SSubmitRsp2){0};
×
1923
  SArray      *newTbUids = NULL;
×
1924
  int32_t      ret;
1925
  SEncoder     ec = {0};
×
1926

1927
  pRsp->code = TSDB_CODE_SUCCESS;
×
1928

1929
  void           *pAllocMsg = NULL;
×
1930
  SSubmitReq2Msg *pMsg = (SSubmitReq2Msg *)pReq;
×
1931
  if (0 == pMsg->version) {
×
1932
    code = vnodeSubmitReqConvertToSubmitReq2(pVnode, (SSubmitReq *)pMsg, pSubmitReq);
×
1933
    if (TSDB_CODE_SUCCESS == code) {
×
1934
      code = vnodeRebuildSubmitReqMsg(pSubmitReq, &pReq);
×
1935
    }
1936
    if (TSDB_CODE_SUCCESS == code) {
×
1937
      pAllocMsg = pReq;
×
1938
    }
1939
    if (TSDB_CODE_SUCCESS != code) {
×
1940
      goto _exit;
×
1941
    }
1942
  } else {
1943
    // decode
1944
    pReq = POINTER_SHIFT(pReq, sizeof(SSubmitReq2Msg));
×
1945
    len -= sizeof(SSubmitReq2Msg);
×
1946
    SDecoder dc = {0};
×
1947
    tDecoderInit(&dc, pReq, len);
×
1948
    if (tDecodeSubmitReq(&dc, pSubmitReq, NULL) < 0) {
×
1949
      code = TSDB_CODE_INVALID_MSG;
×
1950
      goto _exit;
×
1951
    }
1952
    tDecoderClear(&dc);
×
1953
  }
1954

1955
  // scan
1956
  TSKEY now = taosGetTimestamp(pVnode->config.tsdbCfg.precision);
×
1957
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * pVnode->config.tsdbCfg.keep2;
×
1958
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
×
1959
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
×
1960
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
×
1961

1962
    if (pSubmitTbData->pCreateTbReq && pSubmitTbData->pCreateTbReq->uid == 0) {
×
1963
      code = TSDB_CODE_INVALID_MSG;
×
1964
      goto _exit;
×
1965
    }
1966

1967
    if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
×
1968
      if (TARRAY_SIZE(pSubmitTbData->aCol) <= 0) {
×
1969
        code = TSDB_CODE_INVALID_MSG;
×
1970
        goto _exit;
×
1971
      }
1972

1973
      SColData *colDataArr = TARRAY_DATA(pSubmitTbData->aCol);
×
1974
      SRowKey   lastKey;
1975
      tColDataArrGetRowKey(colDataArr, TARRAY_SIZE(pSubmitTbData->aCol), 0, &lastKey);
×
1976
      for (int32_t iRow = 1; iRow < colDataArr[0].nVal; iRow++) {
×
1977
        SRowKey key;
1978
        tColDataArrGetRowKey(TARRAY_DATA(pSubmitTbData->aCol), TARRAY_SIZE(pSubmitTbData->aCol), iRow, &key);
×
1979
        if (tRowKeyCompare(&lastKey, &key) >= 0) {
×
1980
          code = TSDB_CODE_INVALID_MSG;
×
1981
          vError("vgId:%d %s failed 1 since %s, version:%" PRId64, TD_VID(pVnode), __func__, tstrerror(terrno), ver);
×
1982
          goto _exit;
×
1983
        }
1984
      }
1985
    } else {
1986
      int32_t nRow = TARRAY_SIZE(pSubmitTbData->aRowP);
×
1987
      SRow  **aRow = (SRow **)TARRAY_DATA(pSubmitTbData->aRowP);
×
1988
      SRowKey lastRowKey;
1989
      for (int32_t iRow = 0; iRow < nRow; ++iRow) {
×
1990
#ifndef NO_UNALIGNED_ACCESS
1991
        if (aRow[iRow]->ts < minKey || aRow[iRow]->ts > maxKey) {
×
1992
#else
1993
        TSKEY ts = taosGetInt64Aligned(&(aRow[iRow]->ts));
1994
        if (ts < minKey || ts > maxKey) {
1995
#endif
1996
          code = TSDB_CODE_INVALID_MSG;
×
1997
          vError("vgId:%d %s failed 2 since %s, version:%" PRId64, TD_VID(pVnode), __func__, tstrerror(code), ver);
×
1998
          goto _exit;
×
1999
        }
2000
        if (iRow == 0) {
×
2001
          tRowGetKey(aRow[iRow], &lastRowKey);
×
2002
        } else {
2003
          SRowKey rowKey;
2004
          tRowGetKey(aRow[iRow], &rowKey);
×
2005

2006
          if (tRowKeyCompare(&lastRowKey, &rowKey) >= 0) {
×
2007
            code = TSDB_CODE_INVALID_MSG;
×
2008
            vError("vgId:%d %s failed 3 since %s, version:%" PRId64, TD_VID(pVnode), __func__, tstrerror(code), ver);
×
2009
            goto _exit;
×
2010
          }
2011
          lastRowKey = rowKey;
×
2012
        }
2013
      }
2014
    }
2015
  }
2016

2017
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
×
2018
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
×
2019

2020
    if (pSubmitTbData->pCreateTbReq) {
×
2021
      pSubmitTbData->uid = pSubmitTbData->pCreateTbReq->uid;
×
2022
    } else {
2023
      SMetaInfo info = {0};
×
2024

2025
      code = metaGetInfo(pVnode->pMeta, pSubmitTbData->uid, &info, NULL);
×
2026
      if (code) {
×
2027
        code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
2028
        vWarn("vgId:%d, table uid:%" PRId64 " not exists", TD_VID(pVnode), pSubmitTbData->uid);
×
2029
        goto _exit;
×
2030
      }
2031

2032
      if (info.suid != pSubmitTbData->suid) {
×
2033
        code = TSDB_CODE_INVALID_MSG;
×
2034
        goto _exit;
×
2035
      }
2036

2037
      if (info.suid) {
×
2038
        if (metaGetInfo(pVnode->pMeta, info.suid, &info, NULL) != 0) {
×
2039
          vWarn("vgId:%d, table uid:%" PRId64 " not exists", TD_VID(pVnode), info.suid);
×
2040
        }
2041
      }
2042

2043
      if (pSubmitTbData->sver != info.skmVer) {
×
2044
        code = TSDB_CODE_TDB_INVALID_TABLE_SCHEMA_VER;
×
2045
        goto _exit;
×
2046
      }
2047
    }
2048

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

2053
      if (nColData <= 0) {
×
2054
        code = TSDB_CODE_INVALID_MSG;
×
2055
        goto _exit;
×
2056
      }
2057

2058
      if (aColData[0].cid != PRIMARYKEY_TIMESTAMP_COL_ID || aColData[0].type != TSDB_DATA_TYPE_TIMESTAMP ||
×
2059
          aColData[0].nVal <= 0) {
×
2060
        code = TSDB_CODE_INVALID_MSG;
×
2061
        goto _exit;
×
2062
      }
2063

2064
      for (int32_t j = 1; j < nColData; j++) {
×
2065
        if (aColData[j].nVal != aColData[0].nVal) {
×
2066
          code = TSDB_CODE_INVALID_MSG;
×
2067
          goto _exit;
×
2068
        }
2069
      }
2070
    }
2071
  }
2072

2073
  vGDebug(pOriginalMsg ? &pOriginalMsg->info.traceId : NULL, "vgId:%d, index:%" PRId64 ", submit block, rows:%d",
×
2074
          TD_VID(pVnode), ver, (int32_t)taosArrayGetSize(pSubmitReq->aSubmitTbData));
2075

2076
  // loop to handle
2077
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
×
2078
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
×
2079

2080
    // create table
2081
    if (pSubmitTbData->pCreateTbReq) {
×
2082
      // alloc if need
2083
      if (pSubmitRsp->aCreateTbRsp == NULL &&
×
2084
          (pSubmitRsp->aCreateTbRsp = taosArrayInit(TARRAY_SIZE(pSubmitReq->aSubmitTbData), sizeof(SVCreateTbRsp))) ==
×
2085
              NULL) {
2086
        code = terrno;
×
2087
        goto _exit;
×
2088
      }
2089

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

2092
      // create table
2093
      if (metaCreateTable2(pVnode->pMeta, ver, pSubmitTbData->pCreateTbReq, &pCreateTbRsp->pMeta) == 0) {
×
2094
        // create table success
2095

2096
        if (newTbUids == NULL &&
×
2097
            (newTbUids = taosArrayInit(TARRAY_SIZE(pSubmitReq->aSubmitTbData), sizeof(int64_t))) == NULL) {
×
2098
          code = terrno;
×
2099
          goto _exit;
×
2100
        }
2101

2102
        if (taosArrayPush(newTbUids, &pSubmitTbData->uid) == NULL) {
×
2103
          code = terrno;
×
2104
          goto _exit;
×
2105
        }
2106

2107
        if (pCreateTbRsp->pMeta) {
×
2108
          vnodeUpdateMetaRsp(pVnode, pCreateTbRsp->pMeta);
×
2109
        }
2110
      } else {  // create table failed
2111
        if (terrno != TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
×
2112
          code = terrno;
×
2113
          vError("vgId:%d failed to create table:%s, code:%s", TD_VID(pVnode), pSubmitTbData->pCreateTbReq->name,
×
2114
                 tstrerror(terrno));
2115
          goto _exit;
×
2116
        }
2117
        terrno = 0;
×
2118
        pSubmitTbData->uid = pSubmitTbData->pCreateTbReq->uid;  // update uid if table exist for using below
×
2119
      }
2120
    }
2121

2122
    // insert data
2123
    int32_t affectedRows;
2124
    code = tsdbInsertTableData(pVnode->pTsdb, ver, pSubmitTbData, &affectedRows);
×
2125
    if (code) goto _exit;
×
2126

2127
    code = metaUpdateChangeTimeWithLock(pVnode->pMeta, pSubmitTbData->uid, pSubmitTbData->ctimeMs);
×
2128
    if (code) goto _exit;
×
2129

2130
    pSubmitRsp->affectedRows += affectedRows;
×
2131
  }
2132

2133
  // update the affected table uid list
2134
  if (taosArrayGetSize(newTbUids) > 0) {
×
2135
    vDebug("vgId:%d, add %d table into query table list in handling submit", TD_VID(pVnode),
×
2136
           (int32_t)taosArrayGetSize(newTbUids));
2137
    if (tqUpdateTbUidList(pVnode->pTq, newTbUids, true) != 0) {
×
2138
      vError("vgId:%d, failed to update tbUid list", TD_VID(pVnode));
×
2139
    }
2140
  }
2141

2142
_exit:
×
2143
  // message
2144
  pRsp->code = code;
×
2145
  tEncodeSize(tEncodeSSubmitRsp2, pSubmitRsp, pRsp->contLen, ret);
×
2146
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
×
2147
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
×
2148
  if (tEncodeSSubmitRsp2(&ec, pSubmitRsp) < 0) {
×
2149
    vError("vgId:%d, failed to encode submit response", TD_VID(pVnode));
×
2150
  }
2151
  tEncoderClear(&ec);
×
2152

2153
  // update statistics
2154
  (void)atomic_add_fetch_64(&pVnode->statis.nInsert, pSubmitRsp->affectedRows);
×
2155
  (void)atomic_add_fetch_64(&pVnode->statis.nInsertSuccess, pSubmitRsp->affectedRows);
×
2156
  (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsert, 1);
×
2157

2158
  if (tsEnableMonitor && tsMonitorFqdn[0] != 0 && tsMonitorPort != 0 && pSubmitRsp->affectedRows > 0 &&
×
2159
      strlen(pOriginalMsg->info.conn.user) > 0 && tsInsertCounter != NULL) {
×
2160
    const char *sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT_AFFECTED_ROWS,
×
2161
                                   pVnode->monitor.strClusterId,
×
2162
                                   pVnode->monitor.strDnodeId,
×
2163
                                   tsLocalEp,
2164
                                   pVnode->monitor.strVgId,
×
2165
                                   pOriginalMsg->info.conn.user,
×
2166
                                   "Success"};
2167
    int         tv = taos_counter_add(tsInsertCounter, pSubmitRsp->affectedRows, sample_labels);
×
2168
  }
2169

2170
  if (code == 0) {
×
2171
    (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
×
2172
    code = tdProcessRSmaSubmit(pVnode->pSma, ver, pSubmitReq, pReq, len);
×
2173
  }
2174
  /*
2175
  if (code == 0) {
2176
    atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
2177
    code = tdProcessRSmaSubmit(pVnode->pSma, ver, pSubmitReq, pReq, len);
2178

2179
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT, pVnode->monitor.strClusterId,
2180
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2181
                                          pOriginalMsg->info.conn.user, "Success"};
2182
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2183
  }
2184
  else{
2185
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT, pVnode->monitor.strClusterId,
2186
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2187
                                        pOriginalMsg->info.conn.user, "Failed"};
2188
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2189
  }
2190
  */
2191

2192
  // clear
2193
  taosArrayDestroy(newTbUids);
×
2194
  tDestroySubmitReq(pSubmitReq, 0 == pMsg->version ? TSDB_MSG_FLG_CMPT : TSDB_MSG_FLG_DECODE);
×
2195
  tDestroySSubmitRsp2(pSubmitRsp, TSDB_MSG_FLG_ENCODE);
×
2196

2197
  if (code) terrno = code;
×
2198

2199
  taosMemoryFree(pAllocMsg);
×
2200

2201
  return code;
×
2202
}
2203

2204
static int32_t vnodeProcessCreateTSmaReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
2205
#ifdef USE_TSMA
2206
  SVCreateTSmaReq req = {0};
×
2207
  SDecoder        coder = {0};
×
2208

2209
  if (pRsp) {
×
2210
    pRsp->msgType = TDMT_VND_CREATE_SMA_RSP;
×
2211
    pRsp->code = TSDB_CODE_SUCCESS;
×
2212
    pRsp->pCont = NULL;
×
2213
    pRsp->contLen = 0;
×
2214
  }
2215

2216
  // decode and process req
2217
  tDecoderInit(&coder, pReq, len);
×
2218

2219
  if (tDecodeSVCreateTSmaReq(&coder, &req) < 0) {
×
2220
    terrno = TSDB_CODE_MSG_DECODE_ERROR;
×
2221
    if (pRsp) pRsp->code = terrno;
×
2222
    goto _err;
×
2223
  }
2224

2225
  if (tdProcessTSmaCreate(pVnode->pSma, ver, (const char *)&req) < 0) {
×
2226
    if (pRsp) pRsp->code = terrno;
×
2227
    goto _err;
×
2228
  }
2229

2230
  tDecoderClear(&coder);
×
2231
  vDebug("vgId:%d, success to create tsma %s:%" PRIi64 " version %" PRIi64 " for table %" PRIi64, TD_VID(pVnode),
×
2232
         req.indexName, req.indexUid, ver, req.tableUid);
2233
  return 0;
×
2234

2235
_err:
×
2236
  tDecoderClear(&coder);
×
2237
  vError("vgId:%d, failed to create tsma %s:%" PRIi64 " version %" PRIi64 "for table %" PRIi64 " since %s",
×
2238
         TD_VID(pVnode), req.indexName, req.indexUid, ver, req.tableUid, terrstr());
2239
  return terrno;
×
2240
#else
2241
  return TSDB_CODE_INTERNAL_ERROR;
2242
#endif
2243
}
2244

2245
/**
2246
 * @brief specific for smaDstVnode
2247
 *
2248
 * @param pVnode
2249
 * @param pCont
2250
 * @param contLen
2251
 * @return int32_t
2252
 */
2253
int32_t vnodeProcessCreateTSma(SVnode *pVnode, void *pCont, uint32_t contLen) {
×
2254
  return vnodeProcessCreateTSmaReq(pVnode, 1, pCont, contLen, NULL);
×
2255
}
2256

2257
static int32_t vnodeConsolidateAlterHashRange(SVnode *pVnode, int64_t ver) {
×
2258
  int32_t code = TSDB_CODE_SUCCESS;
×
2259

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

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

2266
  return code;
×
2267
}
2268

2269
static int32_t vnodeProcessAlterConfirmReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
2270
  vInfo("vgId:%d, vnode handle msgType:alter-confirm, alter confirm msg is processed", TD_VID(pVnode));
×
2271
  int32_t code = TSDB_CODE_SUCCESS;
×
2272
  if (!pVnode->config.hashChange) {
×
2273
    goto _exit;
×
2274
  }
2275

2276
  code = vnodeConsolidateAlterHashRange(pVnode, ver);
×
2277
  if (code < 0) {
×
2278
    vError("vgId:%d, failed to consolidate alter hashrange since %s. version:%" PRId64, TD_VID(pVnode), terrstr(), ver);
×
2279
    goto _exit;
×
2280
  }
2281
  pVnode->config.hashChange = false;
×
2282

2283
_exit:
×
2284
  pRsp->msgType = TDMT_VND_ALTER_CONFIRM_RSP;
×
2285
  pRsp->code = code;
×
2286
  pRsp->pCont = NULL;
×
2287
  pRsp->contLen = 0;
×
2288

2289
  return code;
×
2290
}
2291

2292
extern int32_t tsdbDisableAndCancelAllBgTask(STsdb *pTsdb);
2293
extern void    tsdbEnableBgTask(STsdb *pTsdb);
2294

2295
static int32_t vnodeProcessAlterConfigReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
2296
  bool walChanged = false;
×
2297
  bool tsdbChanged = false;
×
2298

2299
  SAlterVnodeConfigReq req = {0};
×
2300
  if (tDeserializeSAlterVnodeConfigReq(pReq, len, &req) != 0) {
×
2301
    terrno = TSDB_CODE_INVALID_MSG;
×
2302
    return TSDB_CODE_INVALID_MSG;
×
2303
  }
2304

2305
  vInfo("vgId:%d, start to alter vnode config, page:%d pageSize:%d buffer:%d szPage:%d szBuf:%" PRIu64
×
2306
        " cacheLast:%d cacheLastSize:%d days:%d keep0:%d keep1:%d keep2:%d keepTimeOffset:%d s3KeepLocal:%d "
2307
        "s3Compact:%d fsync:%d level:%d "
2308
        "walRetentionPeriod:%d walRetentionSize:%d",
2309
        TD_VID(pVnode), req.pages, req.pageSize, req.buffer, req.pageSize * 1024, (uint64_t)req.buffer * 1024 * 1024,
2310
        req.cacheLast, req.cacheLastSize, req.daysPerFile, req.daysToKeep0, req.daysToKeep1, req.daysToKeep2,
2311
        req.keepTimeOffset, req.s3KeepLocal, req.s3Compact, req.walFsyncPeriod, req.walLevel, req.walRetentionPeriod,
2312
        req.walRetentionSize);
2313

2314
  if (pVnode->config.cacheLastSize != req.cacheLastSize) {
×
2315
    pVnode->config.cacheLastSize = req.cacheLastSize;
×
2316
    tsdbCacheSetCapacity(pVnode, (size_t)pVnode->config.cacheLastSize * 1024 * 1024);
×
2317
  }
2318

2319
  if (pVnode->config.szBuf != req.buffer * 1024LL * 1024LL) {
×
2320
    vInfo("vgId:%d, vnode buffer is changed from %" PRId64 " to %" PRId64, TD_VID(pVnode), pVnode->config.szBuf,
×
2321
          (uint64_t)(req.buffer * 1024LL * 1024LL));
2322
    pVnode->config.szBuf = req.buffer * 1024LL * 1024LL;
×
2323
  }
2324

2325
  if (pVnode->config.szCache != req.pages) {
×
2326
    if ((terrno = metaAlterCache(pVnode->pMeta, req.pages)) < 0) {
×
2327
      vError("vgId:%d, failed to change vnode pages from %d to %d failed since %s", TD_VID(pVnode),
×
2328
             pVnode->config.szCache, req.pages, tstrerror(terrno));
2329
      return terrno;
×
2330
    } else {
2331
      vInfo("vgId:%d, vnode pages is changed from %d to %d", TD_VID(pVnode), pVnode->config.szCache, req.pages);
×
2332
      pVnode->config.szCache = req.pages;
×
2333
    }
2334
  }
2335

2336
  if (pVnode->config.cacheLast != req.cacheLast) {
×
2337
    pVnode->config.cacheLast = req.cacheLast;
×
2338
  }
2339

2340
  if (pVnode->config.walCfg.fsyncPeriod != req.walFsyncPeriod) {
×
2341
    pVnode->config.walCfg.fsyncPeriod = req.walFsyncPeriod;
×
2342
    walChanged = true;
×
2343
  }
2344

2345
  if (pVnode->config.walCfg.level != req.walLevel) {
×
2346
    if (pVnode->config.walCfg.level == 0) {
×
2347
      pVnode->config.walCfg.clearFiles = 1;
×
2348
    }
2349
    pVnode->config.walCfg.level = req.walLevel;
×
2350
    walChanged = true;
×
2351
  }
2352

2353
  if (pVnode->config.walCfg.retentionPeriod != req.walRetentionPeriod) {
×
2354
    pVnode->config.walCfg.retentionPeriod = req.walRetentionPeriod;
×
2355
    walChanged = true;
×
2356
  }
2357

2358
  if (pVnode->config.walCfg.retentionSize != req.walRetentionSize) {
×
2359
    pVnode->config.walCfg.retentionSize = req.walRetentionSize;
×
2360
    walChanged = true;
×
2361
  }
2362

2363
  if (pVnode->config.tsdbCfg.keep0 != req.daysToKeep0) {
×
2364
    pVnode->config.tsdbCfg.keep0 = req.daysToKeep0;
×
2365
    if (!VND_IS_RSMA(pVnode)) {
×
2366
      tsdbChanged = true;
×
2367
    }
2368
  }
2369

2370
  if (pVnode->config.tsdbCfg.keep1 != req.daysToKeep1) {
×
2371
    pVnode->config.tsdbCfg.keep1 = req.daysToKeep1;
×
2372
    if (!VND_IS_RSMA(pVnode)) {
×
2373
      tsdbChanged = true;
×
2374
    }
2375
  }
2376

2377
  if (pVnode->config.tsdbCfg.keep2 != req.daysToKeep2) {
×
2378
    pVnode->config.tsdbCfg.keep2 = req.daysToKeep2;
×
2379
    if (!VND_IS_RSMA(pVnode)) {
×
2380
      tsdbChanged = true;
×
2381
    }
2382
  }
2383

2384
  if (pVnode->config.tsdbCfg.keepTimeOffset != req.keepTimeOffset) {
×
2385
    pVnode->config.tsdbCfg.keepTimeOffset = req.keepTimeOffset;
×
2386
    if (!VND_IS_RSMA(pVnode)) {
×
2387
      tsdbChanged = true;
×
2388
    }
2389
  }
2390

2391
  if (req.sttTrigger != -1 && req.sttTrigger != pVnode->config.sttTrigger) {
×
2392
    if (req.sttTrigger > 1 && pVnode->config.sttTrigger > 1) {
×
2393
      pVnode->config.sttTrigger = req.sttTrigger;
×
2394
    } else {
2395
      vnodeAWait(&pVnode->commitTask);
×
2396

2397
      int32_t ret = tsdbDisableAndCancelAllBgTask(pVnode->pTsdb);
×
2398
      if (ret != 0) {
×
2399
        vError("vgId:%d, failed to disable bg task since %s", TD_VID(pVnode), tstrerror(ERRNO));
×
2400
      }
2401

2402
      pVnode->config.sttTrigger = req.sttTrigger;
×
2403
      tsdbEnableBgTask(pVnode->pTsdb);
×
2404
    }
2405
  }
2406

2407
  if (req.minRows != -1 && req.minRows != pVnode->config.tsdbCfg.minRows) {
×
2408
    pVnode->config.tsdbCfg.minRows = req.minRows;
×
2409
  }
2410

2411
  if (req.s3KeepLocal != -1 && req.s3KeepLocal != pVnode->config.s3KeepLocal) {
×
2412
    pVnode->config.s3KeepLocal = req.s3KeepLocal;
×
2413
  }
2414
  if (req.s3Compact != -1 && req.s3Compact != pVnode->config.s3Compact) {
×
2415
    pVnode->config.s3Compact = req.s3Compact;
×
2416
  }
2417

2418
  if (walChanged) {
×
2419
    if (walAlter(pVnode->pWal, &pVnode->config.walCfg) != 0) {
×
2420
      vError("vgId:%d, failed to alter wal config since %s", TD_VID(pVnode), tstrerror(ERRNO));
×
2421
    }
2422
  }
2423

2424
  if (tsdbChanged) {
×
2425
    tsdbSetKeepCfg(pVnode->pTsdb, &pVnode->config.tsdbCfg);
×
2426
  }
2427

2428
  return 0;
×
2429
}
2430

2431
static int32_t vnodeProcessBatchDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
2432
  SBatchDeleteReq deleteReq;
2433
  SDecoder        decoder;
2434
  tDecoderInit(&decoder, pReq, len);
×
2435
  if (tDecodeSBatchDeleteReq(&decoder, &deleteReq) < 0) {
×
2436
    tDecoderClear(&decoder);
×
2437
    return terrno = TSDB_CODE_INVALID_MSG;
×
2438
  }
2439

2440
  SMetaReader mr = {0};
×
2441
  metaReaderDoInit(&mr, pVnode->pMeta, META_READER_NOLOCK);
×
2442
  STsdb *pTsdb = pVnode->pTsdb;
×
2443

2444
  if (deleteReq.level) {
×
2445
    pTsdb = deleteReq.level == 1 ? VND_RSMA1(pVnode) : VND_RSMA2(pVnode);
×
2446
  }
2447

2448
  int32_t sz = taosArrayGetSize(deleteReq.deleteReqs);
×
2449
  for (int32_t i = 0; i < sz; i++) {
×
2450
    SSingleDeleteReq *pOneReq = taosArrayGet(deleteReq.deleteReqs, i);
×
2451
    char             *name = pOneReq->tbname;
×
2452
    if (metaGetTableEntryByName(&mr, name) < 0) {
×
2453
      vDebug("vgId:%d, stream delete msg, skip since no table: %s", pVnode->config.vgId, name);
×
2454
      continue;
×
2455
    }
2456

2457
    int64_t uid = mr.me.uid;
×
2458

2459
    int32_t code = tsdbDeleteTableData(pTsdb, ver, deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
×
2460
    if (code < 0) {
×
2461
      terrno = code;
×
2462
      vError("vgId:%d, delete error since %s, suid:%" PRId64 ", uid:%" PRId64 ", start ts:%" PRId64 ", end ts:%" PRId64,
×
2463
             TD_VID(pVnode), terrstr(), deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
2464
    }
2465

2466
    if (deleteReq.level == 0) {
×
2467
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, deleteReq.ctimeMs);
×
2468
      if (code < 0) {
×
2469
        terrno = code;
×
2470
        vError("vgId:%d, update change time error since %s, suid:%" PRId64 ", uid:%" PRId64 ", start ts:%" PRId64
×
2471
               ", end ts:%" PRId64,
2472
               TD_VID(pVnode), terrstr(), deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
2473
      }
2474
    }
2475
    tDecoderClear(&mr.coder);
×
2476
  }
2477
  metaReaderClear(&mr);
×
2478
  taosArrayDestroy(deleteReq.deleteReqs);
×
2479
  return 0;
×
2480
}
2481

2482
static int32_t vnodeProcessDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
×
2483
                                     SRpcMsg *pOriginalMsg) {
2484
  int32_t     code = 0;
×
2485
  SDecoder   *pCoder = &(SDecoder){0};
×
2486
  SDeleteRes *pRes = &(SDeleteRes){0};
×
2487

2488
  pRsp->msgType = TDMT_VND_DELETE_RSP;
×
2489
  pRsp->pCont = NULL;
×
2490
  pRsp->contLen = 0;
×
2491
  pRsp->code = TSDB_CODE_SUCCESS;
×
2492

2493
  pRes->uidList = taosArrayInit(0, sizeof(tb_uid_t));
×
2494
  if (pRes->uidList == NULL) {
×
2495
    code = terrno;
×
2496
    goto _err;
×
2497
  }
2498

2499
  tDecoderInit(pCoder, pReq, len);
×
2500
  code = tDecodeDeleteRes(pCoder, pRes);
×
2501
  if (code) goto _err;
×
2502

2503
  if (pRes->affectedRows > 0) {
×
2504
    for (int32_t iUid = 0; iUid < taosArrayGetSize(pRes->uidList); iUid++) {
×
2505
      uint64_t uid = *(uint64_t *)taosArrayGet(pRes->uidList, iUid);
×
2506
      code = tsdbDeleteTableData(pVnode->pTsdb, ver, pRes->suid, uid, pRes->skey, pRes->ekey);
×
2507
      if (code) goto _err;
×
2508
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, pRes->ctimeMs);
×
2509
      if (code) goto _err;
×
2510
    }
2511
  }
2512

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

2515
  tDecoderClear(pCoder);
×
2516
  taosArrayDestroy(pRes->uidList);
×
2517

2518
  SVDeleteRsp rsp = {.affectedRows = pRes->affectedRows};
×
2519
  int32_t     ret = 0;
×
2520
  tEncodeSize(tEncodeSVDeleteRsp, &rsp, pRsp->contLen, ret);
×
2521
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
×
2522
  SEncoder ec = {0};
×
2523
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
×
2524
  code = tEncodeSVDeleteRsp(&ec, &rsp);
×
2525
  if (code) goto _err;
×
2526
  tEncoderClear(&ec);
×
2527
  return code;
×
2528

2529
_err:
×
2530
  /*
2531
  if(code == TSDB_CODE_SUCCESS){
2532
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_DELETE, pVnode->monitor.strClusterId,
2533
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2534
                                        pOriginalMsg->info.conn.user, "Success"};
2535
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2536
  }
2537
  else{
2538
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_DELETE, pVnode->monitor.strClusterId,
2539
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2540
                                        pOriginalMsg->info.conn.user, "Failed"};
2541
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2542
  }
2543
  */
2544

2545
  return code;
×
2546
}
2547
static int32_t vnodeProcessCreateIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
2548
  SVCreateStbReq req = {0};
×
2549
  SDecoder       dc = {0};
×
2550
  int32_t        code = 0;
×
2551

2552
  pRsp->msgType = TDMT_VND_CREATE_INDEX_RSP;
×
2553
  pRsp->code = TSDB_CODE_SUCCESS;
×
2554
  pRsp->pCont = NULL;
×
2555
  pRsp->contLen = 0;
×
2556

2557
  tDecoderInit(&dc, pReq, len);
×
2558
  // decode req
2559
  if (tDecodeSVCreateStbReq(&dc, &req) < 0) {
×
2560
    tDecoderClear(&dc);
×
2561
    return terrno = TSDB_CODE_INVALID_MSG;
×
2562
  }
2563

2564
  code = metaAddIndexToSuperTable(pVnode->pMeta, ver, &req);
×
2565
  if (code) {
×
2566
    pRsp->code = code;
×
2567
    goto _err;
×
2568
  }
2569
  tDecoderClear(&dc);
×
2570
  return 0;
×
2571

2572
_err:
×
2573
  tDecoderClear(&dc);
×
2574
  return code;
×
2575
}
2576
static int32_t vnodeProcessDropIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
2577
  SDropIndexReq req = {0};
×
2578
  int32_t       code = 0;
×
2579
  pRsp->msgType = TDMT_VND_DROP_INDEX_RSP;
×
2580
  pRsp->code = TSDB_CODE_SUCCESS;
×
2581
  pRsp->pCont = NULL;
×
2582
  pRsp->contLen = 0;
×
2583

2584
  if ((code = tDeserializeSDropIdxReq(pReq, len, &req))) {
×
2585
    pRsp->code = code;
×
2586
    return code;
×
2587
  }
2588

2589
  code = metaDropIndexFromSuperTable(pVnode->pMeta, ver, &req);
×
2590
  if (code) {
×
2591
    pRsp->code = code;
×
2592
    return code;
×
2593
  }
2594
  return TSDB_CODE_SUCCESS;
×
2595
}
2596

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

2599
static int32_t vnodeProcessCompactVnodeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
2600
  if (!pVnode->restored) {
×
2601
    vInfo("vgId:%d, ignore compact req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
×
2602
    return 0;
×
2603
  }
2604
  return vnodeAsyncCompact(pVnode, ver, pReq, len, pRsp);
×
2605
}
2606

2607
static int32_t vnodeProcessConfigChangeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
2608
  if (syncCheckMember(pVnode->sync) != 0) {
×
2609
    vError("vgId:%d, failed to check member", TD_VID(pVnode));
×
2610
  }
2611

2612
  pRsp->msgType = TDMT_SYNC_CONFIG_CHANGE_RSP;
×
2613
  pRsp->code = TSDB_CODE_SUCCESS;
×
2614
  pRsp->pCont = NULL;
×
2615
  pRsp->contLen = 0;
×
2616

2617
  return 0;
×
2618
}
2619

2620
static int32_t vnodeCheckToken(SVnode *pVnode, char *member0Token, char *member1Token) {
×
2621
  SSyncState syncState = syncGetState(pVnode->sync);
×
2622
  if (syncState.state != TAOS_SYNC_STATE_LEADER) {
×
2623
    return terrno = TSDB_CODE_SYN_NOT_LEADER;
×
2624
  }
2625

2626
  char token[TSDB_ARB_TOKEN_SIZE] = {0};
×
2627
  if (vnodeGetArbToken(pVnode, token) != 0) {
×
2628
    return terrno = TSDB_CODE_NOT_FOUND;
×
2629
  }
2630

2631
  if (strncmp(token, member0Token, TSDB_ARB_TOKEN_SIZE) != 0 &&
×
2632
      strncmp(token, member1Token, TSDB_ARB_TOKEN_SIZE) != 0) {
×
2633
    return terrno = TSDB_CODE_MND_ARB_TOKEN_MISMATCH;
×
2634
  }
2635

2636
  terrno = TSDB_CODE_SUCCESS;
×
2637
  return 0;
×
2638
}
2639

2640
static int32_t vnodeCheckSyncd(SVnode *pVnode, char *member0Token, char *member1Token) {
×
2641
  int32_t code = vnodeCheckToken(pVnode, member0Token, member1Token);
×
2642
  if (code != 0) {
×
2643
    return code;
×
2644
  }
2645

2646
  return syncCheckSynced(pVnode->sync);
×
2647
}
2648

2649
static int32_t vnodeProcessArbCheckSyncReq(SVnode *pVnode, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
2650
  int32_t code = 0;
×
2651

2652
  SVArbCheckSyncReq syncReq = {0};
×
2653

2654
  code = tDeserializeSVArbCheckSyncReq(pReq, len, &syncReq);
×
2655
  if (code) {
×
2656
    return terrno = code;
×
2657
  }
2658

2659
  pRsp->msgType = TDMT_VND_ARB_CHECK_SYNC_RSP;
×
2660
  pRsp->code = TSDB_CODE_SUCCESS;
×
2661
  pRsp->pCont = NULL;
×
2662
  pRsp->contLen = 0;
×
2663

2664
  SVArbCheckSyncRsp syncRsp = {0};
×
2665
  syncRsp.arbToken = syncReq.arbToken;
×
2666
  syncRsp.member0Token = syncReq.member0Token;
×
2667
  syncRsp.member1Token = syncReq.member1Token;
×
2668
  syncRsp.vgId = TD_VID(pVnode);
×
2669

2670
  if (vnodeCheckSyncd(pVnode, syncReq.member0Token, syncReq.member1Token) != 0) {
×
2671
    vError("vgId:%d, failed to check assigned log syncd", TD_VID(pVnode));
×
2672
  }
2673
  syncRsp.errCode = terrno;
×
2674

2675
  if (vnodeUpdateArbTerm(pVnode, syncReq.arbTerm) != 0) {
×
2676
    vError("vgId:%d, failed to update arb term", TD_VID(pVnode));
×
2677
    code = -1;
×
2678
    goto _OVER;
×
2679
  }
2680

2681
  int32_t contLen = tSerializeSVArbCheckSyncRsp(NULL, 0, &syncRsp);
×
2682
  if (contLen <= 0) {
×
2683
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2684
    code = -1;
×
2685
    goto _OVER;
×
2686
  }
2687
  void *pHead = rpcMallocCont(contLen);
×
2688
  if (!pHead) {
×
2689
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2690
    code = -1;
×
2691
    goto _OVER;
×
2692
  }
2693

2694
  if (tSerializeSVArbCheckSyncRsp(pHead, contLen, &syncRsp) <= 0) {
×
2695
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2696
    rpcFreeCont(pHead);
×
2697
    code = -1;
×
2698
    goto _OVER;
×
2699
  }
2700

2701
  pRsp->pCont = pHead;
×
2702
  pRsp->contLen = contLen;
×
2703

2704
  terrno = TSDB_CODE_SUCCESS;
×
2705

2706
_OVER:
×
2707
  tFreeSVArbCheckSyncReq(&syncReq);
×
2708
  return code;
×
2709
}
2710

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