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

taosdata / TDengine / #3653

14 Mar 2025 08:10AM UTC coverage: 22.565% (-41.0%) from 63.596%
#3653

push

travis-ci

web-flow
feat(keep): support keep on super table level. (#30097)

* Feat: support use keep while create super table.

* Test(keep): add test for create super table with keep option.

* Feat(keep): Add tmsg for create keep.

* Feat(keep): support alter table option keep.

* Fix(keep): Add baisc test for alter table option.

* Fix(keep): memory leek.

* Feat(keep): add keep to metaEntry&metaCache and fix earliestTs with stn keep.

* Test(keep): add some cases for select with stb keep.

* Fix: fix ci core while alter stb.

* Feat(keep): delete expired data in super table level.

* Feat: remove get stb keep while query.

* Fix : build error.

* Revert "Fix : build error."

This reverts commit 0ed66e4e8.

* Revert "Feat(keep): delete expired data in super table level."

This reverts commit 36330f6b4.

* Fix : build errors.

* Feat : support restart taosd.

* Fix : alter table comment problems.

* Test : add tests for super table keep.

* Fix: change sdb stb reserve size.

* Test: add more tests.

* Feat: Disable normal tables and sub tables from setting the keep parameter

* Fix: add more checks to avoid unknown address.

* Docs: Add docs for stable keep.

* Fix: some review changes.

* Fix: review errors.

49248 of 302527 branches covered (16.28%)

Branch coverage included in aggregate %.

53 of 99 new or added lines in 12 files covered. (53.54%)

155872 existing lines in 443 files now uncovered.

87359 of 302857 relevant lines covered (28.84%)

570004.22 hits per line

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

5.25
/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

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

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

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

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

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

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

UNCOV
95
  tEndDecode(pCoder);
×
96

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

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

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

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

UNCOV
129
  tEndDecode(&dc);
×
130

UNCOV
131
_exit:
×
UNCOV
132
  tDecoderClear(&dc);
×
UNCOV
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
  }
UNCOV
137
  return code;
×
138
}
139

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

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

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

UNCOV
157
  code = 0;
×
158

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

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

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

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

UNCOV
180
  SVDropTtlTableReq ttlReq = {0};
×
UNCOV
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
UNCOV
187
    tbUids = taosArrayInit(8, sizeof(tb_uid_t));
×
UNCOV
188
    if (tbUids == NULL) {
×
189
      code = terrno;
×
190
      TSDB_CHECK_CODE(code, lino, _exit);
×
191
    }
192

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

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

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

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

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

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

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

UNCOV
230
  code = 0;
×
231

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

UNCOV
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 {
UNCOV
239
    vTrace("vgId:%d, %s done, timestampSec:%d, nUids:%d", TD_VID(pVnode), __func__, ttlReq.timestampSec, ttlReq.nUids);
×
240
  }
241

UNCOV
242
  return code;
×
243
}
244

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

UNCOV
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;
UNCOV
257
  if (tDecodeI32v(pCoder, &submitTbData.flags) < 0) {
×
258
    code = TSDB_CODE_INVALID_MSG;
×
259
    TSDB_CHECK_CODE(code, lino, _exit);
×
260
  }
UNCOV
261
  version = (submitTbData.flags >> 8) & 0xff;
×
UNCOV
262
  submitTbData.flags = submitTbData.flags & 0xff;
×
263

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

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

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

UNCOV
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
UNCOV
292
  TSKEY now = btimeMs;
×
UNCOV
293
  if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_MICRO) {
×
UNCOV
294
    now *= 1000;
×
UNCOV
295
  } else if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_NANO) {
×
UNCOV
296
    now *= 1000000;
×
297
  }
298

UNCOV
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

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

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

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

UNCOV
332
    for (int32_t iRow = 0; iRow < colData.nVal; iRow++) {
×
UNCOV
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

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

UNCOV
353
    for (int32_t iRow = 0; iRow < nRow; ++iRow) {
×
UNCOV
354
      SRow *pRow = (SRow *)(pCoder->data + pCoder->pos);
×
UNCOV
355
      pCoder->pos += pRow->len;
×
356
#ifndef NO_UNALIGNED_ACCESS
UNCOV
357
      if (pRow->ts < minKey || pRow->ts > maxKey) {
×
358
#else
359
      TSKEY ts = taosGetInt64Aligned(&pRow->ts);
360
      if (ts < minKey || ts > maxKey) {
361
#endif
UNCOV
362
        code = TSDB_CODE_TDB_TIMESTAMP_OUT_OF_RANGE;
×
UNCOV
363
        TSDB_CHECK_CODE(code, lino, _exit);
×
364
      }
365
    }
366
  }
367

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

UNCOV
373
  tEndDecode(pCoder);
×
374

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

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

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

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

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

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

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

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

UNCOV
416
  tEndDecode(pCoder);
×
417

UNCOV
418
_exit:
×
UNCOV
419
  tDecoderClear(pCoder);
×
UNCOV
420
  if (code) {
×
UNCOV
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
  }
UNCOV
424
  return code;
×
425
}
426

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

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

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

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

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

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

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

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

UNCOV
460
  taosArrayDestroy(res.uidList);
×
461

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

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

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

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

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

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

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

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

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

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

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

UNCOV
507
  return code;
×
508
}
509

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

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

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

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

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

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

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

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

598
  if (code && code != TSDB_CODE_MSG_PREPROCESSED) {
56!
UNCOV
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;
56✔
603
}
604

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

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

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

625
  vDebug("vgId:%d, start to process write request %s, index:%" PRId64 ", applied:%" PRId64 ", state.applyTerm:%" PRId64
76!
626
         ", conn.applyTerm:%" PRId64 ", contLen:%d",
627
         TD_VID(pVnode), TMSG_INFO(pMsg->msgType), ver, pVnode->state.applied, pVnode->state.applyTerm,
628
         pMsg->info.conn.applyTerm, pMsg->contLen);
629

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

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

638
  atomic_store_64(&pVnode->state.applied, ver);
76✔
639
  atomic_store_64(&pVnode->state.applyTerm, pMsg->info.conn.applyTerm);
76✔
640

641
  if (!syncUtilUserCommit(pMsg->msgType)) goto _exit;
76✔
642

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

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

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

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

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

810
  vTrace("vgId:%d, process %s request, code:0x%x index:%" PRId64, TD_VID(pVnode), TMSG_INFO(pMsg->msgType), pRsp->code,
56!
811
         ver);
812

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

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

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

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

838
_exit:
56✔
839
  return 0;
76✔
840

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

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

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

UNCOV
855
int32_t vnodeProcessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
×
UNCOV
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);
×
UNCOV
859
    return 0;
×
860
  }
861

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1065
extern int32_t vnodeAsyncRetention(SVnode *pVnode, int64_t now);
1066

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

UNCOV
1073
  int32_t     code = 0;
×
UNCOV
1074
  SVTrimDbReq trimReq = {0};
×
1075

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

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

1084
  code = vnodeAsyncRetention(pVnode, trimReq.timestamp);
×
1085

1086
_exit:
×
UNCOV
1087
  return code;
×
1088
}
1089

1090
extern int32_t vnodeAsyncS3Migrate(SVnode *pVnode, int64_t now);
1091

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

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

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

1104
  code = vnodeAsyncS3Migrate(pVnode, s3migrateReq.timestamp);
×
1105

1106
_exit:
×
1107
  return code;
×
1108
}
1109

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

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

UNCOV
1123
  if (ttlReq.nUids != 0) {
×
1124
    vInfo("vgId:%d, drop ttl table req will be processed, time:%d, ntbUids:%d", pVnode->config.vgId,
×
1125
          ttlReq.timestampSec, ttlReq.nUids);
1126
  }
1127

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

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

UNCOV
1138
end:
×
UNCOV
1139
  taosArrayDestroy(ttlReq.pTbUids);
×
UNCOV
1140
  return ret;
×
1141
}
1142

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

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

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

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

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

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

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

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

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

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

1227
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
18✔
1228
  pRsp->code = TSDB_CODE_SUCCESS;
18✔
1229
  pRsp->pCont = NULL;
18✔
1230
  pRsp->contLen = 0;
18✔
1231

1232
  // decode and process req
1233
  tDecoderInit(&coder, pReq, len);
18✔
1234

1235
  code = tDecodeSVCreateStbReq(&coder, &req);
18✔
1236
  if (code) {
18!
UNCOV
1237
    pRsp->code = code;
×
UNCOV
1238
    goto _err;
×
1239
  }
1240

1241
  code = metaCreateSuperTable(pVnode->pMeta, ver, &req);
18✔
1242
  if (code) {
18!
UNCOV
1243
    pRsp->code = code;
×
UNCOV
1244
    goto _err;
×
1245
  }
1246

1247
  if ((code = tdProcessRSmaCreate(pVnode->pSma, &req)) < 0) {
18!
UNCOV
1248
    pRsp->code = code;
×
UNCOV
1249
    goto _err;
×
1250
  }
1251

1252
  tDecoderClear(&coder);
18✔
1253
  return 0;
18✔
1254

UNCOV
1255
_err:
×
UNCOV
1256
  tDecoderClear(&coder);
×
UNCOV
1257
  return code;
×
1258
}
1259

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

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

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

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

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

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

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

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

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

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

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

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

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

UNCOV
1396
    size_t len = 0;
×
UNCOV
1397
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
×
1398

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

UNCOV
1403
    taosStringBuilderDestroy(&sb);
×
1404
  }
1405

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

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

1421
  pRsp->msgType = TDMT_VND_ALTER_STB_RSP;
16✔
1422
  pRsp->code = TSDB_CODE_SUCCESS;
16✔
1423
  pRsp->pCont = NULL;
16✔
1424
  pRsp->contLen = 0;
16✔
1425

1426
  tDecoderInit(&dc, pReq, len);
16✔
1427

1428
  // decode req
1429
  code = tDecodeSVCreateStbReq(&dc, &req);
16✔
1430
  if (code) {
16!
1431
    tDecoderClear(&dc);
×
UNCOV
1432
    return code;
×
1433
  }
1434

1435
  code = metaAlterSuperTable(pVnode->pMeta, ver, &req);
16✔
1436
  if (code) {
16!
UNCOV
1437
    pRsp->code = code;
×
UNCOV
1438
    tDecoderClear(&dc);
×
UNCOV
1439
    return code;
×
1440
  }
1441

1442
  tDecoderClear(&dc);
16✔
1443

1444
  return 0;
16✔
1445
}
1446

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

1453
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
4✔
1454
  pRsp->pCont = NULL;
4✔
1455
  pRsp->contLen = 0;
4✔
1456

1457
  // decode request
1458
  tDecoderInit(&decoder, pReq, len);
4✔
1459
  if (tDecodeSVDropStbReq(&decoder, &req) < 0) {
4!
UNCOV
1460
    rcode = TSDB_CODE_INVALID_MSG;
×
UNCOV
1461
    goto _exit;
×
1462
  }
1463

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

1472
  if (tqUpdateTbUidList(pVnode->pTq, tbUidList, false) < 0) {
4!
UNCOV
1473
    rcode = terrno;
×
UNCOV
1474
    goto _exit;
×
1475
  }
1476

1477
  if (tdProcessRSmaDrop(pVnode->pSma, &req) < 0) {
4!
UNCOV
1478
    rcode = terrno;
×
UNCOV
1479
    goto _exit;
×
1480
  }
1481

1482
  // return rsp
1483
_exit:
4✔
1484
  if (tbUidList) taosArrayDestroy(tbUidList);
4!
1485
  pRsp->code = rcode;
4✔
1486
  tDecoderClear(&decoder);
4✔
1487
  return 0;
4✔
1488
}
1489

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

UNCOV
1500
  pRsp->msgType = TDMT_VND_ALTER_TABLE_RSP;
×
1501
  pRsp->pCont = NULL;
×
1502
  pRsp->contLen = 0;
×
1503
  pRsp->code = TSDB_CODE_SUCCESS;
×
1504

UNCOV
1505
  tDecoderInit(&dc, pReq, len);
×
1506

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

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

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

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

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

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

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

UNCOV
1549
    taosArrayDestroy(tbUids);
×
1550
  }
1551

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

UNCOV
1561
  tEncoderClear(&ec);
×
UNCOV
1562
  if (vMetaRsp.pSchemas) {
×
UNCOV
1563
    taosMemoryFree(vMetaRsp.pSchemas);
×
UNCOV
1564
    taosMemoryFree(vMetaRsp.pSchemaExt);
×
1565
  }
UNCOV
1566
  return 0;
×
1567
}
1568

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

UNCOV
1580
  pRsp->msgType = TDMT_VND_DROP_TABLE_RSP;
×
1581
  pRsp->pCont = NULL;
×
1582
  pRsp->contLen = 0;
×
1583
  pRsp->code = TSDB_CODE_SUCCESS;
×
1584

1585
  // decode req
UNCOV
1586
  tDecoderInit(&decoder, pReq, len);
×
UNCOV
1587
  ret = tDecodeSVDropTbBatchReq(&decoder, &req);
×
UNCOV
1588
  if (ret < 0) {
×
UNCOV
1589
    terrno = TSDB_CODE_INVALID_MSG;
×
UNCOV
1590
    pRsp->code = terrno;
×
UNCOV
1591
    goto _exit;
×
1592
  }
1593

1594
  // process req
UNCOV
1595
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
×
UNCOV
1596
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(SVDropTbRsp));
×
UNCOV
1597
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
×
UNCOV
1598
  if (tbUids == NULL || rsp.pArray == NULL || tbNames == NULL) goto _exit;
×
1599

1600
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
×
1601
    SVDropTbReq *pDropTbReq = req.pReqs + iReq;
×
UNCOV
1602
    SVDropTbRsp  dropTbRsp = {0};
×
1603
    tb_uid_t     tbUid = 0;
×
1604

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

UNCOV
1622
    if (taosArrayPush(rsp.pArray, &dropTbRsp) == NULL) {
×
1623
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1624
      pRsp->code = terrno;
×
UNCOV
1625
      goto _exit;
×
1626
    }
1627

1628
    if (tsEnableAuditCreateTable) {
×
1629
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
×
1630
      if (str == NULL) {
×
UNCOV
1631
        pRsp->code = terrno;
×
UNCOV
1632
        goto _exit;
×
1633
      }
UNCOV
1634
      tstrncpy(str, pDropTbReq->name, TSDB_TABLE_FNAME_LEN);
×
UNCOV
1635
      if (taosArrayPush(tbNames, &str) == NULL) {
×
1636
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
1637
        pRsp->code = terrno;
×
UNCOV
1638
        goto _exit;
×
1639
      }
1640
    }
1641
  }
1642

UNCOV
1643
  if (tqUpdateTbUidList(pVnode->pTq, tbUids, false) < 0) {
×
UNCOV
1644
    vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(terrno));
×
1645
  }
1646

UNCOV
1647
  if (tdUpdateTbUidList(pVnode->pSma, pStore, false) < 0) {
×
1648
    goto _exit;
×
1649
  }
1650

UNCOV
1651
  if (tsEnableAuditCreateTable) {
×
UNCOV
1652
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
×
1653

UNCOV
1654
    SName name = {0};
×
UNCOV
1655
    if (tNameFromString(&name, pVnode->config.dbname, T_NAME_ACCT | T_NAME_DB) != 0) {
×
UNCOV
1656
      vError("vgId:%d, failed to get name from string", TD_VID(pVnode));
×
1657
    }
1658

UNCOV
1659
    SStringBuilder sb = {0};
×
UNCOV
1660
    for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
×
UNCOV
1661
      char **key = (char **)taosArrayGet(tbNames, iReq);
×
UNCOV
1662
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
×
UNCOV
1663
      if (iReq < req.nReqs - 1) {
×
UNCOV
1664
        taosStringBuilderAppendChar(&sb, ',');
×
1665
      }
UNCOV
1666
      taosMemoryFreeClear(*key);
×
1667
    }
1668

UNCOV
1669
    size_t len = 0;
×
UNCOV
1670
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
×
1671

UNCOV
1672
    if (pOriginRpc->info.conn.user != NULL && strlen(pOriginRpc->info.conn.user) > 0) {
×
UNCOV
1673
      auditAddRecord(pOriginRpc, clusterId, "dropTable", name.dbname, "", keyJoined, len);
×
1674
    }
1675

UNCOV
1676
    taosStringBuilderDestroy(&sb);
×
1677
  }
1678

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

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

1704
  tInitSubmitBlkIter(msgIter, pBlock, &blkIter);
1705
  if (blkIter.row == NULL) return 0;
1706

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

1714
  suid = msgIter->suid;
1715
  rv = TD_ROW_SVER(blkIter.row);
1716

1717
  char __tags[128] = {0};
1718
  snprintf(__tags, 128, "%s: uid %" PRIi64 " ", tags, msgIter->uid);
1719
  while ((row = tGetSubmitBlkNext(&blkIter))) {
1720
    tdSRowPrint(row, pSchema, __tags);
1721
  }
1722

1723
  taosMemoryFreeClear(pSchema);
1724

1725
  return TSDB_CODE_SUCCESS;
1726
}
1727
#endif
1728
typedef struct SSubmitReqConvertCxt {
1729
  SSubmitMsgIter msgIter;
1730
  SSubmitBlk    *pBlock;
1731
  SSubmitBlkIter blkIter;
1732
  STSRow        *pRow;
1733
  STSRowIter     rowIter;
1734
  SSubmitTbData *pTbData;
1735
  STSchema      *pTbSchema;
1736
  SArray        *pColValues;
1737
} SSubmitReqConvertCxt;
1738

UNCOV
1739
static int32_t vnodeResetTableCxt(SMeta *pMeta, SSubmitReqConvertCxt *pCxt) {
×
1740
  taosMemoryFreeClear(pCxt->pTbSchema);
×
1741
  int32_t code = metaGetTbTSchemaNotNull(pMeta, pCxt->msgIter.suid > 0 ? pCxt->msgIter.suid : pCxt->msgIter.uid,
×
1742
                                         pCxt->msgIter.sversion, 1, &pCxt->pTbSchema);
1743
  if (TSDB_CODE_SUCCESS != code) {
×
1744
    return code;
×
1745
  }
UNCOV
1746
  tdSTSRowIterInit(&pCxt->rowIter, pCxt->pTbSchema);
×
1747

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

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

1777
  return TSDB_CODE_SUCCESS;
×
1778
}
1779

1780
static void vnodeDestroySubmitReqConvertCxt(SSubmitReqConvertCxt *pCxt) {
×
1781
  taosMemoryFreeClear(pCxt->pTbSchema);
×
1782
  tDestroySubmitTbData(pCxt->pTbData, TSDB_MSG_FLG_ENCODE);
×
UNCOV
1783
  taosMemoryFreeClear(pCxt->pTbData);
×
UNCOV
1784
  taosArrayDestroy(pCxt->pColValues);
×
1785
}
×
1786

1787
static int32_t vnodeCellValConvertToColVal(STColumn *pCol, SCellVal *pCellVal, SColVal *pColVal) {
×
UNCOV
1788
  if (tdValTypeIsNone(pCellVal->valType)) {
×
UNCOV
1789
    pColVal->flag = CV_FLAG_NONE;
×
1790
    return TSDB_CODE_SUCCESS;
×
1791
  }
1792

1793
  if (tdValTypeIsNull(pCellVal->valType)) {
×
1794
    pColVal->flag = CV_FLAG_NULL;
×
1795
    return TSDB_CODE_SUCCESS;
×
1796
  }
1797

UNCOV
1798
  if (IS_VAR_DATA_TYPE(pCol->type)) {
×
1799
    pColVal->value.nData = varDataLen(pCellVal->val);
×
UNCOV
1800
    pColVal->value.pData = (uint8_t *)varDataVal(pCellVal->val);
×
UNCOV
1801
  } else if (TSDB_DATA_TYPE_FLOAT == pCol->type) {
×
1802
    float f = GET_FLOAT_VAL(pCellVal->val);
×
1803
    memcpy(&pColVal->value.val, &f, sizeof(f));
×
UNCOV
1804
  } else if (TSDB_DATA_TYPE_DOUBLE == pCol->type) {
×
UNCOV
1805
    taosSetPInt64Aligned(&pColVal->value.val, (int64_t *)pCellVal->val);
×
1806
  } else {
1807
    GET_TYPED_DATA(pColVal->value.val, int64_t, pCol->type, pCellVal->val);
×
1808
  }
1809

1810
  pColVal->flag = CV_FLAG_VALUE;
×
1811
  return TSDB_CODE_SUCCESS;
×
1812
}
1813

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

UNCOV
1828
static int32_t vnodeDecodeCreateTbReq(SSubmitReqConvertCxt *pCxt) {
×
UNCOV
1829
  if (pCxt->msgIter.schemaLen <= 0) {
×
1830
    return TSDB_CODE_SUCCESS;
×
1831
  }
1832

1833
  pCxt->pTbData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
×
UNCOV
1834
  if (NULL == pCxt->pTbData->pCreateTbReq) {
×
1835
    return terrno;
×
1836
  }
1837

1838
  SDecoder decoder = {0};
×
1839
  tDecoderInit(&decoder, (uint8_t *)pCxt->pBlock->data, pCxt->msgIter.schemaLen);
×
1840
  int32_t code = tDecodeSVCreateTbReq(&decoder, pCxt->pTbData->pCreateTbReq);
×
1841
  tDecoderClear(&decoder);
×
1842

UNCOV
1843
  return code;
×
1844
}
1845

1846
static int32_t vnodeSubmitReqConvertToSubmitReq2(SVnode *pVnode, SSubmitReq *pReq, SSubmitReq2 *pReq2) {
×
1847
  pReq2->aSubmitTbData = taosArrayInit(128, sizeof(SSubmitTbData));
×
1848
  if (NULL == pReq2->aSubmitTbData) {
×
1849
    return terrno;
×
1850
  }
1851

UNCOV
1852
  SSubmitReqConvertCxt cxt = {0};
×
1853

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

1884
  vnodeDestroySubmitReqConvertCxt(&cxt);
×
1885
  return code;
×
1886
}
1887

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

UNCOV
1911
static int32_t vnodeProcessSubmitReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
×
1912
                                     SRpcMsg *pOriginalMsg) {
UNCOV
1913
  int32_t code = 0;
×
UNCOV
1914
  terrno = 0;
×
1915

UNCOV
1916
  SSubmitReq2 *pSubmitReq = &(SSubmitReq2){0};
×
UNCOV
1917
  SSubmitRsp2 *pSubmitRsp = &(SSubmitRsp2){0};
×
UNCOV
1918
  SArray      *newTbUids = NULL;
×
1919
  int32_t      ret;
1920
  SEncoder     ec = {0};
×
1921

UNCOV
1922
  pRsp->code = TSDB_CODE_SUCCESS;
×
1923

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

1950
  // scan
1951
  TSKEY now = taosGetTimestamp(pVnode->config.tsdbCfg.precision);
×
UNCOV
1952
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * pVnode->config.tsdbCfg.keep2;
×
UNCOV
1953
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
×
UNCOV
1954
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
×
UNCOV
1955
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
×
1956

1957
    if (pSubmitTbData->pCreateTbReq && pSubmitTbData->pCreateTbReq->uid == 0) {
×
UNCOV
1958
      code = TSDB_CODE_INVALID_MSG;
×
UNCOV
1959
      goto _exit;
×
1960
    }
1961

UNCOV
1962
    if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
×
UNCOV
1963
      if (TARRAY_SIZE(pSubmitTbData->aCol) <= 0) {
×
UNCOV
1964
        code = TSDB_CODE_INVALID_MSG;
×
UNCOV
1965
        goto _exit;
×
1966
      }
1967

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

UNCOV
2001
          if (tRowKeyCompare(&lastRowKey, &rowKey) >= 0) {
×
UNCOV
2002
            code = TSDB_CODE_INVALID_MSG;
×
UNCOV
2003
            vError("vgId:%d %s failed 3 since %s, version:%" PRId64, TD_VID(pVnode), __func__, tstrerror(code), ver);
×
UNCOV
2004
            goto _exit;
×
2005
          }
UNCOV
2006
          lastRowKey = rowKey;
×
2007
        }
2008
      }
2009
    }
2010
  }
2011

UNCOV
2012
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
×
UNCOV
2013
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
×
2014

2015
    if (pSubmitTbData->pCreateTbReq) {
×
2016
      pSubmitTbData->uid = pSubmitTbData->pCreateTbReq->uid;
×
2017
    } else {
UNCOV
2018
      SMetaInfo info = {0};
×
2019

UNCOV
2020
      code = metaGetInfo(pVnode->pMeta, pSubmitTbData->uid, &info, NULL);
×
2021
      if (code) {
×
UNCOV
2022
        code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
UNCOV
2023
        vWarn("vgId:%d, table uid:%" PRId64 " not exists", TD_VID(pVnode), pSubmitTbData->uid);
×
UNCOV
2024
        goto _exit;
×
2025
      }
2026

2027
      if (info.suid != pSubmitTbData->suid) {
×
UNCOV
2028
        code = TSDB_CODE_INVALID_MSG;
×
UNCOV
2029
        goto _exit;
×
2030
      }
2031

UNCOV
2032
      if (info.suid) {
×
UNCOV
2033
        if (metaGetInfo(pVnode->pMeta, info.suid, &info, NULL) != 0) {
×
UNCOV
2034
          vWarn("vgId:%d, table uid:%" PRId64 " not exists", TD_VID(pVnode), info.suid);
×
2035
        }
2036
      }
2037

UNCOV
2038
      if (pSubmitTbData->sver != info.skmVer) {
×
UNCOV
2039
        code = TSDB_CODE_TDB_INVALID_TABLE_SCHEMA_VER;
×
UNCOV
2040
        goto _exit;
×
2041
      }
2042
    }
2043

UNCOV
2044
    if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
×
UNCOV
2045
      int32_t   nColData = TARRAY_SIZE(pSubmitTbData->aCol);
×
UNCOV
2046
      SColData *aColData = (SColData *)TARRAY_DATA(pSubmitTbData->aCol);
×
2047

2048
      if (nColData <= 0) {
×
2049
        code = TSDB_CODE_INVALID_MSG;
×
UNCOV
2050
        goto _exit;
×
2051
      }
2052

UNCOV
2053
      if (aColData[0].cid != PRIMARYKEY_TIMESTAMP_COL_ID || aColData[0].type != TSDB_DATA_TYPE_TIMESTAMP ||
×
UNCOV
2054
          aColData[0].nVal <= 0) {
×
UNCOV
2055
        code = TSDB_CODE_INVALID_MSG;
×
UNCOV
2056
        goto _exit;
×
2057
      }
2058

UNCOV
2059
      for (int32_t j = 1; j < nColData; j++) {
×
UNCOV
2060
        if (aColData[j].nVal != aColData[0].nVal) {
×
UNCOV
2061
          code = TSDB_CODE_INVALID_MSG;
×
UNCOV
2062
          goto _exit;
×
2063
        }
2064
      }
2065
    }
2066
  }
2067

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

2070
  // loop to handle
UNCOV
2071
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
×
UNCOV
2072
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
×
2073

2074
    // create table
UNCOV
2075
    if (pSubmitTbData->pCreateTbReq) {
×
2076
      // alloc if need
UNCOV
2077
      if (pSubmitRsp->aCreateTbRsp == NULL &&
×
UNCOV
2078
          (pSubmitRsp->aCreateTbRsp = taosArrayInit(TARRAY_SIZE(pSubmitReq->aSubmitTbData), sizeof(SVCreateTbRsp))) ==
×
2079
              NULL) {
2080
        code = terrno;
×
UNCOV
2081
        goto _exit;
×
2082
      }
2083

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

2086
      // create table
UNCOV
2087
      if (metaCreateTable2(pVnode->pMeta, ver, pSubmitTbData->pCreateTbReq, &pCreateTbRsp->pMeta) == 0) {
×
2088
        // create table success
2089

UNCOV
2090
        if (newTbUids == NULL &&
×
UNCOV
2091
            (newTbUids = taosArrayInit(TARRAY_SIZE(pSubmitReq->aSubmitTbData), sizeof(int64_t))) == NULL) {
×
UNCOV
2092
          code = terrno;
×
UNCOV
2093
          goto _exit;
×
2094
        }
2095

UNCOV
2096
        if (taosArrayPush(newTbUids, &pSubmitTbData->uid) == NULL) {
×
UNCOV
2097
          code = terrno;
×
UNCOV
2098
          goto _exit;
×
2099
        }
2100

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

2116
    // insert data
2117
    int32_t affectedRows;
UNCOV
2118
    code = tsdbInsertTableData(pVnode->pTsdb, ver, pSubmitTbData, &affectedRows);
×
2119
    if (code) goto _exit;
×
2120

UNCOV
2121
    code = metaUpdateChangeTimeWithLock(pVnode->pMeta, pSubmitTbData->uid, pSubmitTbData->ctimeMs);
×
UNCOV
2122
    if (code) goto _exit;
×
2123

UNCOV
2124
    pSubmitRsp->affectedRows += affectedRows;
×
2125
  }
2126

2127
  // update the affected table uid list
UNCOV
2128
  if (taosArrayGetSize(newTbUids) > 0) {
×
UNCOV
2129
    vDebug("vgId:%d, add %d table into query table list in handling submit", TD_VID(pVnode),
×
2130
           (int32_t)taosArrayGetSize(newTbUids));
UNCOV
2131
    if (tqUpdateTbUidList(pVnode->pTq, newTbUids, true) != 0) {
×
UNCOV
2132
      vError("vgId:%d, failed to update tbUid list", TD_VID(pVnode));
×
2133
    }
2134
  }
2135

UNCOV
2136
_exit:
×
2137
  // message
UNCOV
2138
  pRsp->code = code;
×
UNCOV
2139
  tEncodeSize(tEncodeSSubmitRsp2, pSubmitRsp, pRsp->contLen, ret);
×
2140
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
×
2141
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
×
2142
  if (tEncodeSSubmitRsp2(&ec, pSubmitRsp) < 0) {
×
2143
    vError("vgId:%d, failed to encode submit response", TD_VID(pVnode));
×
2144
  }
2145
  tEncoderClear(&ec);
×
2146

2147
  // update statistics
2148
  (void)atomic_add_fetch_64(&pVnode->statis.nInsert, pSubmitRsp->affectedRows);
×
UNCOV
2149
  (void)atomic_add_fetch_64(&pVnode->statis.nInsertSuccess, pSubmitRsp->affectedRows);
×
UNCOV
2150
  (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsert, 1);
×
2151

UNCOV
2152
  if (tsEnableMonitor && tsMonitorFqdn[0] != 0 && tsMonitorPort != 0 && pSubmitRsp->affectedRows > 0 &&
×
UNCOV
2153
      strlen(pOriginalMsg->info.conn.user) > 0 && tsInsertCounter != NULL) {
×
UNCOV
2154
    const char *sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT_AFFECTED_ROWS,
×
UNCOV
2155
                                   pVnode->monitor.strClusterId,
×
UNCOV
2156
                                   pVnode->monitor.strDnodeId,
×
2157
                                   tsLocalEp,
UNCOV
2158
                                   pVnode->monitor.strVgId,
×
UNCOV
2159
                                   pOriginalMsg->info.conn.user,
×
2160
                                   "Success"};
UNCOV
2161
    int         tv = taos_counter_add(tsInsertCounter, pSubmitRsp->affectedRows, sample_labels);
×
2162
  }
2163

UNCOV
2164
  if (code == 0) {
×
UNCOV
2165
    (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
×
UNCOV
2166
    code = tdProcessRSmaSubmit(pVnode->pSma, ver, pSubmitReq, pReq, len);
×
2167
  }
2168
  /*
2169
  if (code == 0) {
2170
    atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
2171
    code = tdProcessRSmaSubmit(pVnode->pSma, ver, pSubmitReq, pReq, len);
2172

2173
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT, pVnode->monitor.strClusterId,
2174
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2175
                                          pOriginalMsg->info.conn.user, "Success"};
2176
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2177
  }
2178
  else{
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, "Failed"};
2182
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2183
  }
2184
  */
2185

2186
  // clear
UNCOV
2187
  taosArrayDestroy(newTbUids);
×
UNCOV
2188
  tDestroySubmitReq(pSubmitReq, 0 == pMsg->version ? TSDB_MSG_FLG_CMPT : TSDB_MSG_FLG_DECODE);
×
UNCOV
2189
  tDestroySSubmitRsp2(pSubmitRsp, TSDB_MSG_FLG_ENCODE);
×
2190

UNCOV
2191
  if (code) terrno = code;
×
2192

UNCOV
2193
  taosMemoryFree(pAllocMsg);
×
2194

UNCOV
2195
  return code;
×
2196
}
2197

UNCOV
2198
static int32_t vnodeProcessCreateTSmaReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
2199
#ifdef USE_TSMA
2200
  SVCreateTSmaReq req = {0};
×
2201
  SDecoder        coder = {0};
×
2202

UNCOV
2203
  if (pRsp) {
×
UNCOV
2204
    pRsp->msgType = TDMT_VND_CREATE_SMA_RSP;
×
UNCOV
2205
    pRsp->code = TSDB_CODE_SUCCESS;
×
2206
    pRsp->pCont = NULL;
×
2207
    pRsp->contLen = 0;
×
2208
  }
2209

2210
  // decode and process req
UNCOV
2211
  tDecoderInit(&coder, pReq, len);
×
2212

UNCOV
2213
  if (tDecodeSVCreateTSmaReq(&coder, &req) < 0) {
×
UNCOV
2214
    terrno = TSDB_CODE_MSG_DECODE_ERROR;
×
2215
    if (pRsp) pRsp->code = terrno;
×
2216
    goto _err;
×
2217
  }
2218

2219
  if (tdProcessTSmaCreate(pVnode->pSma, ver, (const char *)&req) < 0) {
×
UNCOV
2220
    if (pRsp) pRsp->code = terrno;
×
UNCOV
2221
    goto _err;
×
2222
  }
2223

UNCOV
2224
  tDecoderClear(&coder);
×
UNCOV
2225
  vDebug("vgId:%d, success to create tsma %s:%" PRIi64 " version %" PRIi64 " for table %" PRIi64, TD_VID(pVnode),
×
2226
         req.indexName, req.indexUid, ver, req.tableUid);
UNCOV
2227
  return 0;
×
2228

UNCOV
2229
_err:
×
2230
  tDecoderClear(&coder);
×
2231
  vError("vgId:%d, failed to create tsma %s:%" PRIi64 " version %" PRIi64 "for table %" PRIi64 " since %s",
×
2232
         TD_VID(pVnode), req.indexName, req.indexUid, ver, req.tableUid, terrstr());
UNCOV
2233
  return terrno;
×
2234
#else
2235
  return TSDB_CODE_INTERNAL_ERROR;
2236
#endif
2237
}
2238

2239
/**
2240
 * @brief specific for smaDstVnode
2241
 *
2242
 * @param pVnode
2243
 * @param pCont
2244
 * @param contLen
2245
 * @return int32_t
2246
 */
UNCOV
2247
int32_t vnodeProcessCreateTSma(SVnode *pVnode, void *pCont, uint32_t contLen) {
×
UNCOV
2248
  return vnodeProcessCreateTSmaReq(pVnode, 1, pCont, contLen, NULL);
×
2249
}
2250

UNCOV
2251
static int32_t vnodeConsolidateAlterHashRange(SVnode *pVnode, int64_t ver) {
×
UNCOV
2252
  int32_t code = TSDB_CODE_SUCCESS;
×
2253

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

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

UNCOV
2260
  return code;
×
2261
}
2262

UNCOV
2263
static int32_t vnodeProcessAlterConfirmReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
UNCOV
2264
  vInfo("vgId:%d, vnode handle msgType:alter-confirm, alter confirm msg is processed", TD_VID(pVnode));
×
UNCOV
2265
  int32_t code = TSDB_CODE_SUCCESS;
×
UNCOV
2266
  if (!pVnode->config.hashChange) {
×
UNCOV
2267
    goto _exit;
×
2268
  }
2269

UNCOV
2270
  code = vnodeConsolidateAlterHashRange(pVnode, ver);
×
UNCOV
2271
  if (code < 0) {
×
UNCOV
2272
    vError("vgId:%d, failed to consolidate alter hashrange since %s. version:%" PRId64, TD_VID(pVnode), terrstr(), ver);
×
UNCOV
2273
    goto _exit;
×
2274
  }
UNCOV
2275
  pVnode->config.hashChange = false;
×
2276

UNCOV
2277
_exit:
×
2278
  pRsp->msgType = TDMT_VND_ALTER_CONFIRM_RSP;
×
2279
  pRsp->code = code;
×
UNCOV
2280
  pRsp->pCont = NULL;
×
UNCOV
2281
  pRsp->contLen = 0;
×
2282

UNCOV
2283
  return code;
×
2284
}
2285

2286
extern int32_t tsdbDisableAndCancelAllBgTask(STsdb *pTsdb);
2287
extern void    tsdbEnableBgTask(STsdb *pTsdb);
2288

UNCOV
2289
static int32_t vnodeProcessAlterConfigReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
UNCOV
2290
  bool walChanged = false;
×
UNCOV
2291
  bool tsdbChanged = false;
×
2292

UNCOV
2293
  SAlterVnodeConfigReq req = {0};
×
UNCOV
2294
  if (tDeserializeSAlterVnodeConfigReq(pReq, len, &req) != 0) {
×
UNCOV
2295
    terrno = TSDB_CODE_INVALID_MSG;
×
UNCOV
2296
    return TSDB_CODE_INVALID_MSG;
×
2297
  }
2298

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

2308
  if (pVnode->config.cacheLastSize != req.cacheLastSize) {
×
2309
    pVnode->config.cacheLastSize = req.cacheLastSize;
×
UNCOV
2310
    tsdbCacheSetCapacity(pVnode, (size_t)pVnode->config.cacheLastSize * 1024 * 1024);
×
2311
  }
2312

UNCOV
2313
  if (pVnode->config.szBuf != req.buffer * 1024LL * 1024LL) {
×
UNCOV
2314
    vInfo("vgId:%d, vnode buffer is changed from %" PRId64 " to %" PRId64, TD_VID(pVnode), pVnode->config.szBuf,
×
2315
          (uint64_t)(req.buffer * 1024LL * 1024LL));
UNCOV
2316
    pVnode->config.szBuf = req.buffer * 1024LL * 1024LL;
×
2317
  }
2318

UNCOV
2319
  if (pVnode->config.szCache != req.pages) {
×
UNCOV
2320
    if ((terrno = metaAlterCache(pVnode->pMeta, req.pages)) < 0) {
×
UNCOV
2321
      vError("vgId:%d, failed to change vnode pages from %d to %d failed since %s", TD_VID(pVnode),
×
2322
             pVnode->config.szCache, req.pages, tstrerror(terrno));
UNCOV
2323
      return terrno;
×
2324
    } else {
UNCOV
2325
      vInfo("vgId:%d, vnode pages is changed from %d to %d", TD_VID(pVnode), pVnode->config.szCache, req.pages);
×
UNCOV
2326
      pVnode->config.szCache = req.pages;
×
2327
    }
2328
  }
2329

UNCOV
2330
  if (pVnode->config.cacheLast != req.cacheLast) {
×
UNCOV
2331
    pVnode->config.cacheLast = req.cacheLast;
×
2332
  }
2333

UNCOV
2334
  if (pVnode->config.walCfg.fsyncPeriod != req.walFsyncPeriod) {
×
UNCOV
2335
    pVnode->config.walCfg.fsyncPeriod = req.walFsyncPeriod;
×
2336
    walChanged = true;
×
2337
  }
2338

UNCOV
2339
  if (pVnode->config.walCfg.level != req.walLevel) {
×
UNCOV
2340
    if (pVnode->config.walCfg.level == 0) {
×
UNCOV
2341
      pVnode->config.walCfg.clearFiles = 1;
×
2342
    }
UNCOV
2343
    pVnode->config.walCfg.level = req.walLevel;
×
UNCOV
2344
    walChanged = true;
×
2345
  }
2346

UNCOV
2347
  if (pVnode->config.walCfg.retentionPeriod != req.walRetentionPeriod) {
×
UNCOV
2348
    pVnode->config.walCfg.retentionPeriod = req.walRetentionPeriod;
×
UNCOV
2349
    walChanged = true;
×
2350
  }
2351

UNCOV
2352
  if (pVnode->config.walCfg.retentionSize != req.walRetentionSize) {
×
UNCOV
2353
    pVnode->config.walCfg.retentionSize = req.walRetentionSize;
×
UNCOV
2354
    walChanged = true;
×
2355
  }
2356

UNCOV
2357
  if (pVnode->config.tsdbCfg.keep0 != req.daysToKeep0) {
×
UNCOV
2358
    pVnode->config.tsdbCfg.keep0 = req.daysToKeep0;
×
UNCOV
2359
    if (!VND_IS_RSMA(pVnode)) {
×
UNCOV
2360
      tsdbChanged = true;
×
2361
    }
2362
  }
2363

UNCOV
2364
  if (pVnode->config.tsdbCfg.keep1 != req.daysToKeep1) {
×
UNCOV
2365
    pVnode->config.tsdbCfg.keep1 = req.daysToKeep1;
×
UNCOV
2366
    if (!VND_IS_RSMA(pVnode)) {
×
UNCOV
2367
      tsdbChanged = true;
×
2368
    }
2369
  }
2370

UNCOV
2371
  if (pVnode->config.tsdbCfg.keep2 != req.daysToKeep2) {
×
2372
    pVnode->config.tsdbCfg.keep2 = req.daysToKeep2;
×
UNCOV
2373
    if (!VND_IS_RSMA(pVnode)) {
×
2374
      tsdbChanged = true;
×
2375
    }
2376
  }
2377

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

2385
  if (req.sttTrigger != -1 && req.sttTrigger != pVnode->config.sttTrigger) {
×
UNCOV
2386
    if (req.sttTrigger > 1 && pVnode->config.sttTrigger > 1) {
×
UNCOV
2387
      pVnode->config.sttTrigger = req.sttTrigger;
×
2388
    } else {
2389
      vnodeAWait(&pVnode->commitTask);
×
2390

UNCOV
2391
      int32_t ret = tsdbDisableAndCancelAllBgTask(pVnode->pTsdb);
×
2392
      if (ret != 0) {
×
UNCOV
2393
        vError("vgId:%d, failed to disable bg task since %s", TD_VID(pVnode), tstrerror(ERRNO));
×
2394
      }
2395

UNCOV
2396
      pVnode->config.sttTrigger = req.sttTrigger;
×
2397
      tsdbEnableBgTask(pVnode->pTsdb);
×
2398
    }
2399
  }
2400

UNCOV
2401
  if (req.minRows != -1 && req.minRows != pVnode->config.tsdbCfg.minRows) {
×
UNCOV
2402
    pVnode->config.tsdbCfg.minRows = req.minRows;
×
2403
  }
2404

UNCOV
2405
  if (req.s3KeepLocal != -1 && req.s3KeepLocal != pVnode->config.s3KeepLocal) {
×
UNCOV
2406
    pVnode->config.s3KeepLocal = req.s3KeepLocal;
×
2407
  }
UNCOV
2408
  if (req.s3Compact != -1 && req.s3Compact != pVnode->config.s3Compact) {
×
UNCOV
2409
    pVnode->config.s3Compact = req.s3Compact;
×
2410
  }
2411

UNCOV
2412
  if (walChanged) {
×
2413
    if (walAlter(pVnode->pWal, &pVnode->config.walCfg) != 0) {
×
2414
      vError("vgId:%d, failed to alter wal config since %s", TD_VID(pVnode), tstrerror(ERRNO));
×
2415
    }
2416
  }
2417

UNCOV
2418
  if (tsdbChanged) {
×
UNCOV
2419
    tsdbSetKeepCfg(pVnode->pTsdb, &pVnode->config.tsdbCfg);
×
2420
  }
2421

UNCOV
2422
  return 0;
×
2423
}
2424

UNCOV
2425
static int32_t vnodeProcessBatchDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
2426
  SBatchDeleteReq deleteReq;
2427
  SDecoder        decoder;
UNCOV
2428
  tDecoderInit(&decoder, pReq, len);
×
UNCOV
2429
  if (tDecodeSBatchDeleteReq(&decoder, &deleteReq) < 0) {
×
UNCOV
2430
    tDecoderClear(&decoder);
×
UNCOV
2431
    return terrno = TSDB_CODE_INVALID_MSG;
×
2432
  }
2433

UNCOV
2434
  SMetaReader mr = {0};
×
UNCOV
2435
  metaReaderDoInit(&mr, pVnode->pMeta, META_READER_NOLOCK);
×
UNCOV
2436
  STsdb *pTsdb = pVnode->pTsdb;
×
2437

2438
  if (deleteReq.level) {
×
2439
    pTsdb = deleteReq.level == 1 ? VND_RSMA1(pVnode) : VND_RSMA2(pVnode);
×
2440
  }
2441

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

UNCOV
2451
    int64_t uid = mr.me.uid;
×
2452

UNCOV
2453
    int32_t code = tsdbDeleteTableData(pTsdb, ver, deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
×
UNCOV
2454
    if (code < 0) {
×
UNCOV
2455
      terrno = code;
×
UNCOV
2456
      vError("vgId:%d, delete error since %s, suid:%" PRId64 ", uid:%" PRId64 ", start ts:%" PRId64 ", end ts:%" PRId64,
×
2457
             TD_VID(pVnode), terrstr(), deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
2458
    }
2459

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

UNCOV
2476
static int32_t vnodeProcessDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
×
2477
                                     SRpcMsg *pOriginalMsg) {
UNCOV
2478
  int32_t     code = 0;
×
UNCOV
2479
  SDecoder   *pCoder = &(SDecoder){0};
×
UNCOV
2480
  SDeleteRes *pRes = &(SDeleteRes){0};
×
2481

UNCOV
2482
  pRsp->msgType = TDMT_VND_DELETE_RSP;
×
UNCOV
2483
  pRsp->pCont = NULL;
×
UNCOV
2484
  pRsp->contLen = 0;
×
UNCOV
2485
  pRsp->code = TSDB_CODE_SUCCESS;
×
2486

UNCOV
2487
  pRes->uidList = taosArrayInit(0, sizeof(tb_uid_t));
×
UNCOV
2488
  if (pRes->uidList == NULL) {
×
UNCOV
2489
    code = terrno;
×
UNCOV
2490
    goto _err;
×
2491
  }
2492

UNCOV
2493
  tDecoderInit(pCoder, pReq, len);
×
UNCOV
2494
  code = tDecodeDeleteRes(pCoder, pRes);
×
UNCOV
2495
  if (code) goto _err;
×
2496

UNCOV
2497
  if (pRes->affectedRows > 0) {
×
UNCOV
2498
    for (int32_t iUid = 0; iUid < taosArrayGetSize(pRes->uidList); iUid++) {
×
UNCOV
2499
      uint64_t uid = *(uint64_t *)taosArrayGet(pRes->uidList, iUid);
×
UNCOV
2500
      code = tsdbDeleteTableData(pVnode->pTsdb, ver, pRes->suid, uid, pRes->skey, pRes->ekey);
×
UNCOV
2501
      if (code) goto _err;
×
UNCOV
2502
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, pRes->ctimeMs);
×
UNCOV
2503
      if (code) goto _err;
×
2504
    }
2505
  }
2506

UNCOV
2507
  code = tdProcessRSmaDelete(pVnode->pSma, ver, pRes, pReq, len);
×
2508

UNCOV
2509
  tDecoderClear(pCoder);
×
UNCOV
2510
  taosArrayDestroy(pRes->uidList);
×
2511

UNCOV
2512
  SVDeleteRsp rsp = {.affectedRows = pRes->affectedRows};
×
UNCOV
2513
  int32_t     ret = 0;
×
UNCOV
2514
  tEncodeSize(tEncodeSVDeleteRsp, &rsp, pRsp->contLen, ret);
×
UNCOV
2515
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
×
UNCOV
2516
  SEncoder ec = {0};
×
UNCOV
2517
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
×
UNCOV
2518
  code = tEncodeSVDeleteRsp(&ec, &rsp);
×
UNCOV
2519
  if (code) goto _err;
×
UNCOV
2520
  tEncoderClear(&ec);
×
UNCOV
2521
  return code;
×
2522

UNCOV
2523
_err:
×
2524
  /*
2525
  if(code == TSDB_CODE_SUCCESS){
2526
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_DELETE, pVnode->monitor.strClusterId,
2527
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2528
                                        pOriginalMsg->info.conn.user, "Success"};
2529
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2530
  }
2531
  else{
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, "Failed"};
2535
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2536
  }
2537
  */
2538

UNCOV
2539
  return code;
×
2540
}
UNCOV
2541
static int32_t vnodeProcessCreateIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
UNCOV
2542
  SVCreateStbReq req = {0};
×
2543
  SDecoder       dc = {0};
×
2544
  int32_t        code = 0;
×
2545

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

2551
  tDecoderInit(&dc, pReq, len);
×
2552
  // decode req
UNCOV
2553
  if (tDecodeSVCreateStbReq(&dc, &req) < 0) {
×
UNCOV
2554
    tDecoderClear(&dc);
×
UNCOV
2555
    return terrno = TSDB_CODE_INVALID_MSG;
×
2556
  }
2557

UNCOV
2558
  code = metaAddIndexToSuperTable(pVnode->pMeta, ver, &req);
×
UNCOV
2559
  if (code) {
×
UNCOV
2560
    pRsp->code = code;
×
UNCOV
2561
    goto _err;
×
2562
  }
2563
  tDecoderClear(&dc);
×
UNCOV
2564
  return 0;
×
2565

UNCOV
2566
_err:
×
UNCOV
2567
  tDecoderClear(&dc);
×
2568
  return code;
×
2569
}
UNCOV
2570
static int32_t vnodeProcessDropIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
UNCOV
2571
  SDropIndexReq req = {0};
×
UNCOV
2572
  int32_t       code = 0;
×
UNCOV
2573
  pRsp->msgType = TDMT_VND_DROP_INDEX_RSP;
×
UNCOV
2574
  pRsp->code = TSDB_CODE_SUCCESS;
×
UNCOV
2575
  pRsp->pCont = NULL;
×
UNCOV
2576
  pRsp->contLen = 0;
×
2577

UNCOV
2578
  if ((code = tDeserializeSDropIdxReq(pReq, len, &req))) {
×
UNCOV
2579
    pRsp->code = code;
×
UNCOV
2580
    return code;
×
2581
  }
2582

UNCOV
2583
  code = metaDropIndexFromSuperTable(pVnode->pMeta, ver, &req);
×
2584
  if (code) {
×
2585
    pRsp->code = code;
×
2586
    return code;
×
2587
  }
UNCOV
2588
  return TSDB_CODE_SUCCESS;
×
2589
}
2590

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

UNCOV
2593
static int32_t vnodeProcessCompactVnodeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
2594
  if (!pVnode->restored) {
×
UNCOV
2595
    vInfo("vgId:%d, ignore compact req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
×
UNCOV
2596
    return 0;
×
2597
  }
UNCOV
2598
  return vnodeAsyncCompact(pVnode, ver, pReq, len, pRsp);
×
2599
}
2600

UNCOV
2601
static int32_t vnodeProcessConfigChangeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
UNCOV
2602
  if (syncCheckMember(pVnode->sync) != 0) {
×
UNCOV
2603
    vError("vgId:%d, failed to check member", TD_VID(pVnode));
×
2604
  }
2605

UNCOV
2606
  pRsp->msgType = TDMT_SYNC_CONFIG_CHANGE_RSP;
×
UNCOV
2607
  pRsp->code = TSDB_CODE_SUCCESS;
×
UNCOV
2608
  pRsp->pCont = NULL;
×
UNCOV
2609
  pRsp->contLen = 0;
×
2610

UNCOV
2611
  return 0;
×
2612
}
2613

UNCOV
2614
static int32_t vnodeCheckToken(SVnode *pVnode, char *member0Token, char *member1Token) {
×
UNCOV
2615
  SSyncState syncState = syncGetState(pVnode->sync);
×
UNCOV
2616
  if (syncState.state != TAOS_SYNC_STATE_LEADER) {
×
UNCOV
2617
    return terrno = TSDB_CODE_SYN_NOT_LEADER;
×
2618
  }
2619

UNCOV
2620
  char token[TSDB_ARB_TOKEN_SIZE] = {0};
×
UNCOV
2621
  if (vnodeGetArbToken(pVnode, token) != 0) {
×
UNCOV
2622
    return terrno = TSDB_CODE_NOT_FOUND;
×
2623
  }
2624

UNCOV
2625
  if (strncmp(token, member0Token, TSDB_ARB_TOKEN_SIZE) != 0 &&
×
UNCOV
2626
      strncmp(token, member1Token, TSDB_ARB_TOKEN_SIZE) != 0) {
×
UNCOV
2627
    return terrno = TSDB_CODE_MND_ARB_TOKEN_MISMATCH;
×
2628
  }
2629

UNCOV
2630
  terrno = TSDB_CODE_SUCCESS;
×
UNCOV
2631
  return 0;
×
2632
}
2633

UNCOV
2634
static int32_t vnodeCheckSyncd(SVnode *pVnode, char *member0Token, char *member1Token) {
×
UNCOV
2635
  int32_t code = vnodeCheckToken(pVnode, member0Token, member1Token);
×
UNCOV
2636
  if (code != 0) {
×
UNCOV
2637
    return code;
×
2638
  }
2639

UNCOV
2640
  return syncCheckSynced(pVnode->sync);
×
2641
}
2642

UNCOV
2643
static int32_t vnodeProcessArbCheckSyncReq(SVnode *pVnode, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
UNCOV
2644
  int32_t code = 0;
×
2645

UNCOV
2646
  SVArbCheckSyncReq syncReq = {0};
×
2647

UNCOV
2648
  code = tDeserializeSVArbCheckSyncReq(pReq, len, &syncReq);
×
UNCOV
2649
  if (code) {
×
UNCOV
2650
    return terrno = code;
×
2651
  }
2652

2653
  pRsp->msgType = TDMT_VND_ARB_CHECK_SYNC_RSP;
×
2654
  pRsp->code = TSDB_CODE_SUCCESS;
×
2655
  pRsp->pCont = NULL;
×
UNCOV
2656
  pRsp->contLen = 0;
×
2657

UNCOV
2658
  SVArbCheckSyncRsp syncRsp = {0};
×
UNCOV
2659
  syncRsp.arbToken = syncReq.arbToken;
×
2660
  syncRsp.member0Token = syncReq.member0Token;
×
2661
  syncRsp.member1Token = syncReq.member1Token;
×
2662
  syncRsp.vgId = TD_VID(pVnode);
×
2663

UNCOV
2664
  if (vnodeCheckSyncd(pVnode, syncReq.member0Token, syncReq.member1Token) != 0) {
×
UNCOV
2665
    vError("vgId:%d, failed to check assigned log syncd", TD_VID(pVnode));
×
2666
  }
2667
  syncRsp.errCode = terrno;
×
2668

UNCOV
2669
  if (vnodeUpdateArbTerm(pVnode, syncReq.arbTerm) != 0) {
×
UNCOV
2670
    vError("vgId:%d, failed to update arb term", TD_VID(pVnode));
×
UNCOV
2671
    code = -1;
×
2672
    goto _OVER;
×
2673
  }
2674

2675
  int32_t contLen = tSerializeSVArbCheckSyncRsp(NULL, 0, &syncRsp);
×
UNCOV
2676
  if (contLen <= 0) {
×
UNCOV
2677
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
2678
    code = -1;
×
UNCOV
2679
    goto _OVER;
×
2680
  }
UNCOV
2681
  void *pHead = rpcMallocCont(contLen);
×
UNCOV
2682
  if (!pHead) {
×
UNCOV
2683
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
2684
    code = -1;
×
UNCOV
2685
    goto _OVER;
×
2686
  }
2687

UNCOV
2688
  if (tSerializeSVArbCheckSyncRsp(pHead, contLen, &syncRsp) <= 0) {
×
UNCOV
2689
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
2690
    rpcFreeCont(pHead);
×
UNCOV
2691
    code = -1;
×
UNCOV
2692
    goto _OVER;
×
2693
  }
2694

UNCOV
2695
  pRsp->pCont = pHead;
×
UNCOV
2696
  pRsp->contLen = contLen;
×
2697

UNCOV
2698
  terrno = TSDB_CODE_SUCCESS;
×
2699

UNCOV
2700
_OVER:
×
UNCOV
2701
  tFreeSVArbCheckSyncReq(&syncReq);
×
UNCOV
2702
  return code;
×
2703
}
2704

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