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

taosdata / TDengine / #3523

06 Nov 2024 02:29AM UTC coverage: 55.861% (-2.4%) from 58.216%
#3523

push

travis-ci

web-flow
Merge pull request #28551 from taosdata/feat/TS-5215-2

test(blob): testing & fixes for blob

106075 of 245834 branches covered (43.15%)

Branch coverage included in aggregate %.

0 of 15 new or added lines in 2 files covered. (0.0%)

17003 existing lines in 254 files now uncovered.

181910 of 269703 relevant lines covered (67.45%)

1527639.59 hits per line

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

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

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

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

61
static int32_t vnodePreprocessCreateTableReq(SVnode *pVnode, SDecoder *pCoder, int64_t btime, int64_t *pUid) {
43,266✔
62
  int32_t code = 0;
43,266✔
63
  int32_t lino = 0;
43,266✔
64

65
  if (tStartDecode(pCoder) < 0) {
43,266!
66
    code = TSDB_CODE_INVALID_MSG;
×
67
    TSDB_CHECK_CODE(code, lino, _exit);
×
68
  }
69

70
  // flags
71
  if (tDecodeI32v(pCoder, NULL) < 0) {
43,274!
72
    code = TSDB_CODE_INVALID_MSG;
×
73
    TSDB_CHECK_CODE(code, lino, _exit);
×
74
  }
75

76
  // name
77
  char *name = NULL;
43,274✔
78
  if (tDecodeCStr(pCoder, &name) < 0) {
43,271!
79
    code = TSDB_CODE_INVALID_MSG;
×
80
    TSDB_CHECK_CODE(code, lino, _exit);
×
81
  }
82

83
  // uid
84
  int64_t uid = metaGetTableEntryUidByName(pVnode->pMeta, name);
43,271✔
85
  if (uid == 0) {
43,273✔
86
    uid = tGenIdPI64();
28,839✔
87
  }
88
  *(int64_t *)(pCoder->data + pCoder->pos) = uid;
43,271✔
89

90
  // btime
91
  *(int64_t *)(pCoder->data + pCoder->pos + 8) = btime;
43,271✔
92

93
  tEndDecode(pCoder);
43,271✔
94

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

108
  int64_t  btime = taosGetTimestampMs();
15,372✔
109
  SDecoder dc = {0};
15,372✔
110
  int32_t  nReqs;
111

112
  tDecoderInit(&dc, (uint8_t *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead));
15,372✔
113
  if (tStartDecode(&dc) < 0) {
15,371!
114
    code = TSDB_CODE_INVALID_MSG;
×
115
    return code;
×
116
  }
117

118
  if (tDecodeI32v(&dc, &nReqs) < 0) {
15,371!
119
    code = TSDB_CODE_INVALID_MSG;
×
120
    TSDB_CHECK_CODE(code, lino, _exit);
×
121
  }
122
  for (int32_t iReq = 0; iReq < nReqs; iReq++) {
42,687✔
123
    code = vnodePreprocessCreateTableReq(pVnode, &dc, btime, NULL);
27,312✔
124
    TSDB_CHECK_CODE(code, lino, _exit);
27,316!
125
  }
126

127
  tEndDecode(&dc);
15,375✔
128

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

138
static int32_t vnodePreProcessAlterTableMsg(SVnode *pVnode, SRpcMsg *pMsg) {
598✔
139
  int32_t code = TSDB_CODE_INVALID_MSG;
598✔
140
  int32_t lino = 0;
598✔
141

142
  SDecoder dc = {0};
598✔
143
  tDecoderInit(&dc, (uint8_t *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead));
598✔
144

145
  SVAlterTbReq vAlterTbReq = {0};
598✔
146
  int64_t      ctimeMs = taosGetTimestampMs();
598✔
147
  if (tDecodeSVAlterTbReqSetCtime(&dc, &vAlterTbReq, ctimeMs) < 0) {
598!
148
    goto _exit;
×
149
  }
150

151
  code = 0;
598✔
152

153
_exit:
598✔
154
  tDecoderClear(&dc);
598✔
155
  if (code) {
598!
156
    vError("vgId:%d %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
157
  } else {
158
    vTrace("vgId:%d %s done, table:%s ctimeMs generated:%" PRId64, TD_VID(pVnode), __func__, vAlterTbReq.tbName,
598!
159
           ctimeMs);
160
  }
161
  return code;
598✔
162
}
163

164
static int32_t vnodePreProcessDropTtlMsg(SVnode *pVnode, SRpcMsg *pMsg) {
299,169✔
165
  int32_t code = TSDB_CODE_INVALID_MSG;
299,169✔
166
  int32_t lino = 0;
299,169✔
167

168
  SMsgHead *pContOld = pMsg->pCont;
299,169✔
169
  int32_t   reqLenOld = pMsg->contLen - sizeof(SMsgHead);
299,169✔
170

171
  SArray *tbUids = NULL;
299,169✔
172
  int64_t timestampMs = 0;
299,169✔
173

174
  SVDropTtlTableReq ttlReq = {0};
299,169✔
175
  if (tDeserializeSVDropTtlTableReq((char *)pContOld + sizeof(SMsgHead), reqLenOld, &ttlReq) != 0) {
299,169!
176
    code = TSDB_CODE_INVALID_MSG;
×
177
    TSDB_CHECK_CODE(code, lino, _exit);
×
178
  }
179

180
  {  // find expired uids
181
    tbUids = taosArrayInit(8, sizeof(tb_uid_t));
300,087✔
182
    if (tbUids == NULL) {
299,887!
183
      code = terrno;
×
184
      TSDB_CHECK_CODE(code, lino, _exit);
×
185
    }
186

187
    timestampMs = (int64_t)ttlReq.timestampSec * 1000;
299,887✔
188
    code = metaTtlFindExpired(pVnode->pMeta, timestampMs, tbUids, ttlReq.ttlDropMaxCount);
299,887✔
189
    if (code != 0) {
298,800!
190
      code = TSDB_CODE_INVALID_MSG;
×
191
      TSDB_CHECK_CODE(code, lino, _exit);
×
192
    }
193

194
    ttlReq.nUids = taosArrayGetSize(tbUids);
298,800✔
195
    ttlReq.pTbUids = tbUids;
298,588✔
196
  }
197

198
  if (ttlReq.nUids == 0) {
298,588!
199
    code = TSDB_CODE_MSG_PREPROCESSED;
299,000✔
200
    TSDB_CHECK_CODE(code, lino, _exit);
299,000!
201
  }
202

203
  {  // prepare new content
204
    int32_t reqLenNew = tSerializeSVDropTtlTableReq(NULL, 0, &ttlReq);
×
205
    int32_t contLenNew = reqLenNew + sizeof(SMsgHead);
×
206

207
    SMsgHead *pContNew = rpcMallocCont(contLenNew);
×
208
    if (pContNew == NULL) {
×
209
      code = terrno;
×
210
      TSDB_CHECK_CODE(code, lino, _exit);
×
211
    }
212

213
    if (tSerializeSVDropTtlTableReq((char *)pContNew + sizeof(SMsgHead), reqLenNew, &ttlReq) != 0) {
×
214
      vError("vgId:%d %s:%d failed to serialize drop ttl request", TD_VID(pVnode), __func__, lino);
×
215
    }
216
    pContNew->contLen = htonl(reqLenNew);
×
217
    pContNew->vgId = pContOld->vgId;
×
218

219
    rpcFreeCont(pContOld);
×
220
    pMsg->pCont = pContNew;
×
221
    pMsg->contLen = contLenNew;
×
222
  }
223

224
  code = 0;
×
225

226
_exit:
299,222✔
227
  taosArrayDestroy(tbUids);
299,222✔
228

229
  if (code && code != TSDB_CODE_MSG_PREPROCESSED) {
298,123!
230
    vError("vgId:%d, %s:%d failed to preprocess drop ttl request since %s, msg type:%s", TD_VID(pVnode), __func__, lino,
×
231
           tstrerror(code), TMSG_INFO(pMsg->msgType));
232
  } else {
233
    vTrace("vgId:%d, %s done, timestampSec:%d, nUids:%d", TD_VID(pVnode), __func__, ttlReq.timestampSec, ttlReq.nUids);
298,123!
234
  }
235

236
  return code;
298,474✔
237
}
238

239
extern int64_t tsMaxKeyByPrecision[];
240
static int32_t vnodePreProcessSubmitTbData(SVnode *pVnode, SDecoder *pCoder, int64_t btimeMs, int64_t ctimeMs) {
4,227,866✔
241
  int32_t code = 0;
4,227,866✔
242
  int32_t lino = 0;
4,227,866✔
243

244
  if (tStartDecode(pCoder) < 0) {
4,227,866!
245
    code = TSDB_CODE_INVALID_MSG;
×
246
    TSDB_CHECK_CODE(code, lino, _exit);
×
247
  }
248

249
  SSubmitTbData submitTbData;
250
  uint8_t       version;
251
  if (tDecodeI32v(pCoder, &submitTbData.flags) < 0) {
4,227,980!
252
    code = TSDB_CODE_INVALID_MSG;
×
253
    TSDB_CHECK_CODE(code, lino, _exit);
×
254
  }
255
  version = (submitTbData.flags >> 8) & 0xff;
4,227,980✔
256
  submitTbData.flags = submitTbData.flags & 0xff;
4,227,980✔
257

258
  int64_t uid;
259
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
4,227,980✔
260
    code = vnodePreprocessCreateTableReq(pVnode, pCoder, btimeMs, &uid);
15,955✔
261
    TSDB_CHECK_CODE(code, lino, _exit);
15,950!
262
  }
263

264
  // submit data
265
  if (tDecodeI64(pCoder, &submitTbData.suid) < 0) {
4,227,954!
266
    code = TSDB_CODE_INVALID_MSG;
×
267
    TSDB_CHECK_CODE(code, lino, _exit);
×
268
  }
269

270
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
4,227,954✔
271
    *(int64_t *)(pCoder->data + pCoder->pos) = uid;
15,948✔
272
    pCoder->pos += sizeof(int64_t);
15,948✔
273
  } else {
274
    if (tDecodeI64(pCoder, &submitTbData.uid) < 0) {
4,212,000!
275
      code = TSDB_CODE_INVALID_MSG;
×
276
      TSDB_CHECK_CODE(code, lino, _exit);
×
277
    }
278
  }
279

280
  if (tDecodeI32v(pCoder, &submitTbData.sver) < 0) {
4,227,904!
281
    code = TSDB_CODE_INVALID_MSG;
×
282
    TSDB_CHECK_CODE(code, lino, _exit);
×
283
  }
284

285
  // scan and check
286
  TSKEY now = btimeMs;
4,227,904✔
287
  if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_MICRO) {
4,227,904✔
288
    now *= 1000;
967✔
289
  } else if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_NANO) {
4,226,937✔
290
    now *= 1000000;
407✔
291
  }
292

293
  int32_t keep = pVnode->config.tsdbCfg.keep2;
4,227,904✔
294
  /*
295
  int32_t nlevel = tfsGetLevel(pVnode->pTfs);
296
  if (nlevel > 1 && tsS3Enabled) {
297
    if (nlevel == 3) {
298
      keep = pVnode->config.tsdbCfg.keep1;
299
    } else if (nlevel == 2) {
300
      keep = pVnode->config.tsdbCfg.keep0;
301
    }
302
  }
303
  */
304

305
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * keep;
4,227,904✔
306
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
4,227,904✔
307
  if (submitTbData.flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
4,227,904✔
308
    uint64_t nColData;
309
    if (tDecodeU64v(pCoder, &nColData) < 0) {
7!
310
      code = TSDB_CODE_INVALID_MSG;
×
311
      goto _exit;
×
312
    }
313

314
    SColData colData = {0};
7✔
315
    pCoder->pos += tGetColData(version, pCoder->data + pCoder->pos, &colData);
7✔
316
    if (colData.flag != HAS_VALUE) {
7!
317
      code = TSDB_CODE_INVALID_MSG;
×
318
      goto _exit;
×
319
    }
320

321
    for (int32_t iRow = 0; iRow < colData.nVal; iRow++) {
24✔
322
      if (((TSKEY *)colData.pData)[iRow] < minKey || ((TSKEY *)colData.pData)[iRow] > maxKey) {
17!
323
        code = TSDB_CODE_TDB_TIMESTAMP_OUT_OF_RANGE;
×
324
        goto _exit;
×
325
      }
326
    }
327

328
    for (uint64_t i = 1; i < nColData; i++) {
28✔
329
      pCoder->pos += tGetColData(version, pCoder->data + pCoder->pos, &colData);
21✔
330
    }
331
  } else {
332
    uint64_t nRow;
333
    if (tDecodeU64v(pCoder, &nRow) < 0) {
4,227,924!
334
      code = TSDB_CODE_INVALID_MSG;
×
335
      goto _exit;
466✔
336
    }
337

338
    for (int32_t iRow = 0; iRow < nRow; ++iRow) {
22,606,455✔
339
      SRow *pRow = (SRow *)(pCoder->data + pCoder->pos);
18,378,997✔
340
      pCoder->pos += pRow->len;
18,378,997✔
341

342
      if (pRow->ts < minKey || pRow->ts > maxKey) {
18,378,997!
343
        code = TSDB_CODE_TDB_TIMESTAMP_OUT_OF_RANGE;
466✔
344
        goto _exit;
466✔
345
      }
346
    }
347
  }
348

349
  if (!tDecodeIsEnd(pCoder)) {
4,227,465!
350
    *(int64_t *)(pCoder->data + pCoder->pos) = ctimeMs;
4,227,516✔
351
    pCoder->pos += sizeof(int64_t);
4,227,516✔
352
  }
353

354
  tEndDecode(pCoder);
4,227,465✔
355

356
_exit:
4,227,855✔
357
  return code;
4,227,855✔
358
}
359
static int32_t vnodePreProcessSubmitMsg(SVnode *pVnode, SRpcMsg *pMsg) {
4,205,023✔
360
  int32_t code = 0;
4,205,023✔
361
  int32_t lino = 0;
4,205,023✔
362

363
  SDecoder *pCoder = &(SDecoder){0};
4,205,023✔
364

365
  if (taosHton64(((SSubmitReq2Msg *)pMsg->pCont)->version) != 1) {
4,205,023!
366
    code = TSDB_CODE_INVALID_MSG;
×
367
    TSDB_CHECK_CODE(code, lino, _exit);
×
368
  }
369

370
  tDecoderInit(pCoder, (uint8_t *)pMsg->pCont + sizeof(SSubmitReq2Msg), pMsg->contLen - sizeof(SSubmitReq2Msg));
4,205,081✔
371

372
  if (tStartDecode(pCoder) < 0) {
4,204,995!
373
    code = TSDB_CODE_INVALID_MSG;
×
374
    TSDB_CHECK_CODE(code, lino, _exit);
×
375
  }
376

377
  uint64_t nSubmitTbData;
378
  if (tDecodeU64v(pCoder, &nSubmitTbData) < 0) {
4,205,110!
379
    code = TSDB_CODE_INVALID_MSG;
×
380
    TSDB_CHECK_CODE(code, lino, _exit);
×
381
  }
382

383
  int64_t btimeMs = taosGetTimestampMs();
4,205,097✔
384
  int64_t ctimeMs = btimeMs;
4,205,097✔
385
  for (int32_t i = 0; i < nSubmitTbData; i++) {
8,432,503✔
386
    code = vnodePreProcessSubmitTbData(pVnode, pCoder, btimeMs, ctimeMs);
4,227,979✔
387
    TSDB_CHECK_CODE(code, lino, _exit);
4,227,872✔
388
  }
389

390
  tEndDecode(pCoder);
4,204,524✔
391

392
_exit:
4,205,018✔
393
  tDecoderClear(pCoder);
4,205,018✔
394
  if (code) {
4,204,961✔
395
    vError("vgId:%d, %s:%d failed to preprocess submit request since %s, msg type:%s", TD_VID(pVnode), __func__, lino,
466!
396
           tstrerror(code), TMSG_INFO(pMsg->msgType));
397
  }
398
  return code;
4,204,966✔
399
}
400

401
static int32_t vnodePreProcessDeleteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
1,524✔
402
  int32_t code = 0;
1,524✔
403

404
  int32_t    size;
405
  int32_t    ret;
406
  uint8_t   *pCont;
407
  SEncoder  *pCoder = &(SEncoder){0};
1,524✔
408
  SDeleteRes res = {0};
1,524✔
409

410
  SReadHandle handle = {.vnode = pVnode, .pMsgCb = &pVnode->msgCb, .skipRollup = 1};
1,524✔
411
  initStorageAPI(&handle.api);
1,524✔
412

413
  code = qWorkerProcessDeleteMsg(&handle, pVnode->pQuery, pMsg, &res);
1,525✔
414
  if (code) goto _exit;
1,524!
415

416
  res.ctimeMs = taosGetTimestampMs();
1,525✔
417
  // malloc and encode
418
  tEncodeSize(tEncodeDeleteRes, &res, size, ret);
1,525!
419
  pCont = rpcMallocCont(size + sizeof(SMsgHead));
1,523✔
420

421
  ((SMsgHead *)pCont)->contLen = size + sizeof(SMsgHead);
1,525✔
422
  ((SMsgHead *)pCont)->vgId = TD_VID(pVnode);
1,525✔
423

424
  tEncoderInit(pCoder, pCont + sizeof(SMsgHead), size);
1,525✔
425
  if (tEncodeDeleteRes(pCoder, &res) != 0) {
1,525!
426
    vError("vgId:%d %s failed to encode delete response", TD_VID(pVnode), __func__);
×
427
  }
428
  tEncoderClear(pCoder);
1,525✔
429

430
  rpcFreeCont(pMsg->pCont);
1,523✔
431
  pMsg->pCont = pCont;
1,525✔
432
  pMsg->contLen = size + sizeof(SMsgHead);
1,525✔
433

434
  taosArrayDestroy(res.uidList);
1,525✔
435

436
_exit:
1,525✔
437
  return code;
1,525✔
438
}
439

440
static int32_t vnodePreProcessBatchDeleteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
1,855✔
441
  int32_t code = 0;
1,855✔
442
  int32_t lino = 0;
1,855✔
443

444
  int64_t         ctimeMs = taosGetTimestampMs();
1,858✔
445
  SBatchDeleteReq pReq = {0};
1,858✔
446
  SDecoder       *pCoder = &(SDecoder){0};
1,858✔
447

448
  tDecoderInit(pCoder, (uint8_t *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead));
1,858✔
449

450
  if (tDecodeSBatchDeleteReqSetCtime(pCoder, &pReq, ctimeMs) < 0) {
1,855!
451
    code = TSDB_CODE_INVALID_MSG;
×
452
  }
453

454
  tDecoderClear(pCoder);
1,857✔
455
  taosArrayDestroy(pReq.deleteReqs);
1,855✔
456

457
  if (code) {
1,859!
458
    vError("vgId:%d %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
459
  } else {
460
    vTrace("vgId:%d %s done, ctimeMs generated:%" PRId64, TD_VID(pVnode), __func__, ctimeMs);
1,859!
461
  }
462
  return code;
1,857✔
463
}
464

465
static int32_t vnodePreProcessArbCheckSyncMsg(SVnode *pVnode, SRpcMsg *pMsg) {
×
466
  SVArbCheckSyncReq syncReq = {0};
×
467

468
  if (tDeserializeSVArbCheckSyncReq((char *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead),
×
469
                                    &syncReq) != 0) {
470
    return TSDB_CODE_INVALID_MSG;
×
471
  }
472

473
  int32_t ret = vnodePreCheckAssignedLogSyncd(pVnode, syncReq.member0Token, syncReq.member1Token);
×
474
  if (ret != 0) {
×
475
    vError("vgId:%d, failed to preprocess arb check sync request since %s", TD_VID(pVnode), tstrerror(ret));
×
476
  }
477

478
  int32_t code = terrno;
×
479
  tFreeSVArbCheckSyncReq(&syncReq);
×
480

481
  return code;
×
482
}
483

484
int32_t vnodePreProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
4,551,660✔
485
  int32_t code = 0;
4,551,660✔
486

487
  switch (pMsg->msgType) {
4,551,660!
488
    case TDMT_VND_CREATE_TABLE: {
15,373✔
489
      code = vnodePreProcessCreateTableMsg(pVnode, pMsg);
15,373✔
490
    } break;
15,374✔
491
    case TDMT_VND_ALTER_TABLE: {
598✔
492
      code = vnodePreProcessAlterTableMsg(pVnode, pMsg);
598✔
493
    } break;
598✔
494
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
300,110✔
495
    case TDMT_VND_DROP_TTL_TABLE: {
496
      code = vnodePreProcessDropTtlMsg(pVnode, pMsg);
300,110✔
497
    } break;
298,219✔
498
    case TDMT_VND_SUBMIT: {
4,205,092✔
499
      code = vnodePreProcessSubmitMsg(pVnode, pMsg);
4,205,092✔
500
    } break;
4,204,957✔
501
    case TDMT_VND_DELETE: {
1,524✔
502
      code = vnodePreProcessDeleteMsg(pVnode, pMsg);
1,524✔
503
    } break;
1,525✔
504
    case TDMT_VND_BATCH_DEL: {
1,856✔
505
      code = vnodePreProcessBatchDeleteMsg(pVnode, pMsg);
1,856✔
506
    } break;
1,856✔
507
    case TDMT_VND_ARB_CHECK_SYNC: {
×
508
      code = vnodePreProcessArbCheckSyncMsg(pVnode, pMsg);
×
509
    } break;
×
510
    default:
27,107✔
511
      break;
27,107✔
512
  }
513

514
  if (code && code != TSDB_CODE_MSG_PREPROCESSED) {
4,549,636✔
515
    vError("vgId:%d, failed to preprocess write request since %s, msg type:%s", TD_VID(pVnode), tstrerror(code),
466!
516
           TMSG_INFO(pMsg->msgType));
517
  }
518
  return code;
4,549,283✔
519
}
520

521
int32_t vnodeProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg, int64_t ver, SRpcMsg *pRsp) {
4,307,062✔
522
  int32_t code = 0;
4,307,062✔
523
  void   *ptr = NULL;
4,307,062✔
524
  void   *pReq;
525
  int32_t len;
526

527
  (void)taosThreadMutexLock(&pVnode->mutex);
4,307,062✔
528
  if (pVnode->disableWrite) {
4,307,328!
529
    (void)taosThreadMutexUnlock(&pVnode->mutex);
×
530
    vError("vgId:%d write is disabled for snapshot, version:%" PRId64, TD_VID(pVnode), ver);
×
531
    return TSDB_CODE_VND_WRITE_DISABLED;
×
532
  }
533
  (void)taosThreadMutexUnlock(&pVnode->mutex);
4,307,328✔
534

535
  if (ver <= pVnode->state.applied) {
4,307,236!
536
    vError("vgId:%d, duplicate write request. ver: %" PRId64 ", applied: %" PRId64 "", TD_VID(pVnode), ver,
×
537
           pVnode->state.applied);
538
    return terrno = TSDB_CODE_VND_DUP_REQUEST;
×
539
  }
540

541
  vDebug("vgId:%d, start to process write request %s, index:%" PRId64 ", applied:%" PRId64 ", state.applyTerm:%" PRId64
4,307,236!
542
         ", conn.applyTerm:%" PRId64,
543
         TD_VID(pVnode), TMSG_INFO(pMsg->msgType), ver, pVnode->state.applied, pVnode->state.applyTerm,
544
         pMsg->info.conn.applyTerm);
545

546
  if (!(pVnode->state.applyTerm <= pMsg->info.conn.applyTerm)) {
4,307,041!
547
    return terrno = TSDB_CODE_INTERNAL_ERROR;
×
548
  }
549

550
  if (!(pVnode->state.applied + 1 == ver)) {
4,307,041!
551
    return terrno = TSDB_CODE_INTERNAL_ERROR;
×
552
  }
553

554
  atomic_store_64(&pVnode->state.applied, ver);
4,307,041✔
555
  atomic_store_64(&pVnode->state.applyTerm, pMsg->info.conn.applyTerm);
4,307,327✔
556

557
  if (!syncUtilUserCommit(pMsg->msgType)) goto _exit;
4,307,323✔
558

559
  // skip header
560
  pReq = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
4,298,992✔
561
  len = pMsg->contLen - sizeof(SMsgHead);
4,298,992✔
562
  bool needCommit = false;
4,298,992✔
563

564
  switch (pMsg->msgType) {
4,298,992!
565
    /* META */
566
    case TDMT_VND_CREATE_STB:
8,048✔
567
      if (vnodeProcessCreateStbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
8,048✔
568
      break;
8,053✔
569
    case TDMT_VND_ALTER_STB:
568✔
570
      if (vnodeProcessAlterStbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
568!
571
      break;
576✔
572
    case TDMT_VND_DROP_STB:
870✔
573
      if (vnodeProcessDropStbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
870!
574
      break;
881✔
575
    case TDMT_VND_CREATE_TABLE:
16,570✔
576
      if (vnodeProcessCreateTbReq(pVnode, ver, pReq, len, pRsp, pMsg) < 0) goto _err;
16,570!
577
      break;
16,572✔
578
    case TDMT_VND_ALTER_TABLE:
598✔
579
      if (vnodeProcessAlterTbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
598!
580
      break;
598✔
581
    case TDMT_VND_DROP_TABLE:
325✔
582
      if (vnodeProcessDropTbReq(pVnode, ver, pReq, len, pRsp, pMsg) < 0) goto _err;
325!
583
      break;
325✔
584
    case TDMT_VND_DROP_TTL_TABLE:
×
585
      if (vnodeProcessDropTtlTbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
586
      break;
×
587
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
×
588
      if (vnodeProcessFetchTtlExpiredTbs(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
589
      break;
×
590
    case TDMT_VND_TRIM:
659✔
591
      if (vnodeProcessTrimReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
659!
592
      break;
660✔
593
    case TDMT_VND_S3MIGRATE:
×
594
      if (vnodeProcessS3MigrateReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
595
      break;
×
596
    case TDMT_VND_CREATE_SMA:
24✔
597
      if (vnodeProcessCreateTSmaReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
24!
598
      break;
24✔
599
    /* TSDB */
600
    case TDMT_VND_SUBMIT:
4,249,117✔
601
      if (vnodeProcessSubmitReq(pVnode, ver, pMsg->pCont, pMsg->contLen, pRsp, pMsg) < 0) goto _err;
4,249,117!
602
      break;
4,249,203✔
603
    case TDMT_VND_DELETE:
1,536✔
604
      if (vnodeProcessDeleteReq(pVnode, ver, pReq, len, pRsp, pMsg) < 0) goto _err;
1,536!
605
      break;
1,536✔
606
    case TDMT_VND_BATCH_DEL:
1,904✔
607
      if (vnodeProcessBatchDeleteReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
1,904!
608
      break;
1,909✔
609
    /* TQ */
610
    case TDMT_VND_TMQ_SUBSCRIBE:
254✔
611
      if (tqProcessSubscribeReq(pVnode->pTq, ver, pReq, len) < 0) {
254!
612
        goto _err;
×
613
      }
614
      break;
256✔
615
    case TDMT_VND_TMQ_DELETE_SUB:
7✔
616
      if (tqProcessDeleteSubReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen) < 0) {
7!
617
        goto _err;
×
618
      }
619
      break;
7✔
620
    case TDMT_VND_TMQ_COMMIT_OFFSET:
176✔
621
      if (tqProcessOffsetCommitReq(pVnode->pTq, ver, pReq, len) < 0) {
176!
622
        goto _err;
×
623
      }
624
      break;
176✔
625
    case TDMT_VND_TMQ_ADD_CHECKINFO:
38✔
626
      if (tqProcessAddCheckInfoReq(pVnode->pTq, ver, pReq, len) < 0) {
38!
627
        goto _err;
×
628
      }
629
      break;
38✔
630
    case TDMT_VND_TMQ_DEL_CHECKINFO:
3✔
631
      if (tqProcessDelCheckInfoReq(pVnode->pTq, ver, pReq, len) < 0) {
3✔
632
        goto _err;
1✔
633
      }
634
      break;
2✔
635
    case TDMT_STREAM_TASK_DEPLOY: {
5,398✔
636
      if ((code = tqProcessTaskDeployReq(pVnode->pTq, ver, pReq, len)) != TSDB_CODE_SUCCESS) {
5,398!
637
        goto _err;
×
638
      }
639
    } break;
5,411✔
640
    case TDMT_STREAM_TASK_DROP: {
2,991✔
641
      if ((code = tqProcessTaskDropReq(pVnode->pTq, pMsg->pCont, pMsg->contLen)) < 0) {
2,991!
642
        goto _err;
×
643
      }
644
    } break;
3,005✔
645
    case TDMT_STREAM_TASK_UPDATE_CHKPT: {
1,945✔
646
      if ((code = tqProcessTaskUpdateCheckpointReq(pVnode->pTq, pMsg->pCont, pMsg->contLen)) < 0) {
1,945!
647
        goto _err;
×
648
      }
649
    } break;
1,951✔
650
    case TDMT_STREAM_CONSEN_CHKPT: {
78✔
651
      if (pVnode->restored && (code = tqProcessTaskConsenChkptIdReq(pVnode->pTq, pMsg)) < 0) {
78!
652
        goto _err;
×
653
      }
654

655
    } break;
78✔
656
    case TDMT_STREAM_TASK_PAUSE: {
160✔
657
      if (pVnode->restored && vnodeIsLeader(pVnode) &&
287!
658
          (code = tqProcessTaskPauseReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen)) < 0) {
124✔
659
        goto _err;
×
660
      }
661
    } break;
163✔
662
    case TDMT_STREAM_TASK_RESUME: {
151✔
663
      if (pVnode->restored && vnodeIsLeader(pVnode) &&
271!
664
          (code = tqProcessTaskResumeReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen)) < 0) {
121✔
665
        goto _err;
120✔
666
      }
667
    } break;
36✔
668
    case TDMT_VND_STREAM_TASK_RESET: {
×
669
      if (pVnode->restored && vnodeIsLeader(pVnode) &&
×
670
           (code = tqProcessTaskResetReq(pVnode->pTq, pMsg)) < 0) {
×
671
          goto _err;
×
672
        }
673

674
    } break;
×
675
    case TDMT_VND_ALTER_CONFIRM:
1,466✔
676
      needCommit = pVnode->config.hashChange;
1,466✔
677
      if (vnodeProcessAlterConfirmReq(pVnode, ver, pReq, len, pRsp) < 0) {
1,466!
678
        goto _err;
×
679
      }
680
      break;
1,466✔
681
    case TDMT_VND_ALTER_CONFIG:
441✔
682
      vnodeProcessAlterConfigReq(pVnode, ver, pReq, len, pRsp);
441✔
683
      break;
443✔
684
    case TDMT_VND_COMMIT:
2,138✔
685
      needCommit = true;
2,138✔
686
      break;
2,138✔
687
    case TDMT_VND_CREATE_INDEX:
579✔
688
      vnodeProcessCreateIndexReq(pVnode, ver, pReq, len, pRsp);
579✔
689
      break;
579✔
690
    case TDMT_VND_DROP_INDEX:
1,452✔
691
      vnodeProcessDropIndexReq(pVnode, ver, pReq, len, pRsp);
1,452✔
692
      break;
1,452✔
693
    case TDMT_VND_STREAM_CHECK_POINT_SOURCE: // always return true
1,496✔
694
      tqProcessTaskCheckPointSourceReq(pVnode->pTq, pMsg, pRsp);
1,496✔
695
      break;
1,498✔
UNCOV
696
    case TDMT_VND_STREAM_TASK_UPDATE:  // always return true
×
UNCOV
697
      tqProcessTaskUpdateReq(pVnode->pTq, pMsg);
×
UNCOV
698
      break;
×
UNCOV
699
    case TDMT_VND_COMPACT:
×
UNCOV
700
      vnodeProcessCompactVnodeReq(pVnode, ver, pReq, len, pRsp);
×
UNCOV
701
      goto _exit;
×
702
    case TDMT_SYNC_CONFIG_CHANGE:
×
703
      vnodeProcessConfigChangeReq(pVnode, ver, pReq, len, pRsp);
×
704
      break;
×
705
#ifdef TD_ENTERPRISE
706
    case TDMT_VND_KILL_COMPACT:
×
707
      vnodeProcessKillCompactReq(pVnode, ver, pReq, len, pRsp);
×
708
      break;
×
709
#endif
710
    /* ARB */
711
    case TDMT_VND_ARB_CHECK_SYNC:
×
712
      vnodeProcessArbCheckSyncReq(pVnode, pReq, len, pRsp);
×
713
      break;
×
714
    default:
×
715
      vError("vgId:%d, unprocessed msg, %d", TD_VID(pVnode), pMsg->msgType);
×
716
      return TSDB_CODE_INVALID_MSG;
×
717
  }
718

719
  vTrace("vgId:%d, process %s request, code:0x%x index:%" PRId64, TD_VID(pVnode), TMSG_INFO(pMsg->msgType), pRsp->code,
4,299,036!
720
         ver);
721

722
  walApplyVer(pVnode->pWal, ver);
4,299,036✔
723

724
  code = tqPushMsg(pVnode->pTq, pMsg->msgType);
4,299,028✔
725
  if (code) {
4,299,092✔
726
    vError("vgId:%d, failed to push msg to TQ since %s", TD_VID(pVnode), tstrerror(terrno));
389!
727
    return code;
389✔
728
  }
729

730
  // commit if need
731
  if (needCommit) {
4,298,703✔
732
    vInfo("vgId:%d, commit at version %" PRId64, TD_VID(pVnode), ver);
1,755!
733
    code = vnodeAsyncCommit(pVnode);
1,755✔
734
    if (code) {
1,755!
735
      vError("vgId:%d, failed to vnode async commit since %s.", TD_VID(pVnode), tstrerror(terrno));
×
736
      goto _err;
×
737
    }
738

739
    // start a new one
740
    code = vnodeBegin(pVnode);
1,755✔
741
    if (code) {
1,755✔
742
      vError("vgId:%d, failed to begin vnode since %s.", TD_VID(pVnode), tstrerror(terrno));
52!
743
      goto _err;
×
744
    }
745
  }
746

747
_exit:
4,298,651✔
748
  return 0;
4,306,581✔
749

750
_err:
124✔
751
  vError("vgId:%d, process %s request failed since %s, ver:%" PRId64, TD_VID(pVnode), TMSG_INFO(pMsg->msgType),
124!
752
         tstrerror(code), ver);
753
  return code;
124✔
754
}
755

756
int32_t vnodePreprocessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg) {
189,628✔
757
  if (TDMT_SCH_QUERY != pMsg->msgType && TDMT_SCH_MERGE_QUERY != pMsg->msgType) {
189,628✔
758
    return 0;
1,764✔
759
  }
760

761
  return qWorkerPreprocessQueryMsg(pVnode->pQuery, pMsg, TDMT_SCH_QUERY == pMsg->msgType);
187,864✔
762
}
763

764
int32_t vnodeProcessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
189,778✔
765
  vTrace("message in vnode query queue is processing");
189,778!
766
  if ((pMsg->msgType == TDMT_SCH_QUERY || pMsg->msgType == TDMT_VND_TMQ_CONSUME) && !syncIsReadyForRead(pVnode->sync)) {
189,778✔
767
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
75✔
768
    return 0;
75✔
769
  }
770

771
  if (pMsg->msgType == TDMT_VND_TMQ_CONSUME && !pVnode->restored) {
189,731!
772
    vnodeRedirectRpcMsg(pVnode, pMsg, TSDB_CODE_SYN_RESTORING);
×
773
    return 0;
×
774
  }
775

776
  SReadHandle handle = {.vnode = pVnode, .pMsgCb = &pVnode->msgCb, .pWorkerCb = pInfo->workerCb};
189,731✔
777
  initStorageAPI(&handle.api);
189,731✔
778

779
  switch (pMsg->msgType) {
189,673!
780
    case TDMT_SCH_QUERY:
187,943✔
781
    case TDMT_SCH_MERGE_QUERY:
782
      return qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
187,943✔
783
    case TDMT_SCH_QUERY_CONTINUE:
1,000✔
784
      return qWorkerProcessCQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
1,000✔
785
    case TDMT_VND_TMQ_CONSUME:
614✔
786
      return tqProcessPollReq(pVnode->pTq, pMsg);
614✔
787
    case TDMT_VND_TMQ_CONSUME_PUSH:
117✔
788
      return tqProcessPollPush(pVnode->pTq, pMsg);
117✔
789
    default:
×
790
      vError("unknown msg type:%d in query queue", pMsg->msgType);
×
791
      return TSDB_CODE_APP_ERROR;
×
792
  }
793
}
794

795
int32_t vnodeProcessFetchMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
469,208✔
796
  vTrace("vgId:%d, msg:%p in fetch queue is processing", pVnode->config.vgId, pMsg);
469,208!
797
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
469,208✔
798
       pMsg->msgType == TDMT_VND_BATCH_META || pMsg->msgType == TDMT_VND_TABLE_NAME) &&
334,691✔
799
      !syncIsReadyForRead(pVnode->sync)) {
156,262!
UNCOV
800
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
×
UNCOV
801
    return 0;
×
802
  }
803

804
  switch (pMsg->msgType) {
469,250!
805
    case TDMT_SCH_FETCH:
189,465✔
806
    case TDMT_SCH_MERGE_FETCH:
807
      return qWorkerProcessFetchMsg(pVnode, pVnode->pQuery, pMsg, 0);
189,465✔
808
    case TDMT_SCH_FETCH_RSP:
×
809
      return qWorkerProcessRspMsg(pVnode, pVnode->pQuery, pMsg, 0);
×
810
    // case TDMT_SCH_CANCEL_TASK:
811
    //   return qWorkerProcessCancelMsg(pVnode, pVnode->pQuery, pMsg, 0);
812
    case TDMT_SCH_DROP_TASK:
187,733✔
813
      return qWorkerProcessDropMsg(pVnode, pVnode->pQuery, pMsg, 0);
187,733✔
814
    case TDMT_SCH_TASK_NOTIFY:
7✔
815
      return qWorkerProcessNotifyMsg(pVnode, pVnode->pQuery, pMsg, 0);
7✔
816
    case TDMT_SCH_QUERY_HEARTBEAT:
69,740✔
817
      return qWorkerProcessHbMsg(pVnode, pVnode->pQuery, pMsg, 0);
69,740✔
818
    case TDMT_VND_TABLE_META:
1,003✔
819
    case TDMT_VND_TABLE_NAME:
820
      return vnodeGetTableMeta(pVnode, pMsg, true);
1,003✔
821
    case TDMT_VND_TABLE_CFG:
×
822
      return vnodeGetTableCfg(pVnode, pMsg, true);
×
823
    case TDMT_VND_BATCH_META:
21,302✔
824
      return vnodeGetBatchMeta(pVnode, pMsg);
21,302✔
825
#ifdef TD_ENTERPRISE
UNCOV
826
    case TDMT_VND_QUERY_COMPACT_PROGRESS:
×
UNCOV
827
      return vnodeQueryCompactProgress(pVnode, pMsg);
×
828
#endif
829
      //    case TDMT_VND_TMQ_CONSUME:
830
      //      return tqProcessPollReq(pVnode->pTq, pMsg);
831
    case TDMT_VND_TMQ_VG_WALINFO:
×
832
      return tqProcessVgWalInfoReq(pVnode->pTq, pMsg);
×
833
    case TDMT_VND_TMQ_VG_COMMITTEDINFO:
×
834
      return tqProcessVgCommittedInfoReq(pVnode->pTq, pMsg);
×
835
    case TDMT_VND_TMQ_SEEK:
×
836
      return tqProcessSeekReq(pVnode->pTq, pMsg);
×
837

838
    default:
×
839
      vError("unknown msg type:%d in fetch queue", pMsg->msgType);
×
840
      return TSDB_CODE_APP_ERROR;
×
841
  }
842
}
843

844
int32_t vnodeProcessStreamMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
131,266✔
845
  vTrace("vgId:%d, msg:%p in fetch queue is processing", pVnode->config.vgId, pMsg);
131,266✔
846
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
131,266!
847
       pMsg->msgType == TDMT_VND_BATCH_META) &&
131,286!
848
      !syncIsReadyForRead(pVnode->sync)) {
×
849
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
×
850
    return 0;
×
851
  }
852

853
  switch (pMsg->msgType) {
131,319!
854
    case TDMT_STREAM_TASK_RUN:
69,403✔
855
      return tqProcessTaskRunReq(pVnode->pTq, pMsg);
69,403✔
856
    case TDMT_STREAM_TASK_DISPATCH:
10,509✔
857
      return tqProcessTaskDispatchReq(pVnode->pTq, pMsg);
10,509✔
858
    case TDMT_STREAM_TASK_DISPATCH_RSP:
10,646✔
859
      return tqProcessTaskDispatchRsp(pVnode->pTq, pMsg);
10,646✔
860
    case TDMT_VND_STREAM_TASK_CHECK:
8,752✔
861
      return tqProcessTaskCheckReq(pVnode->pTq, pMsg);
8,752✔
862
    case TDMT_VND_STREAM_TASK_CHECK_RSP:
8,774✔
863
      return tqProcessTaskCheckRsp(pVnode->pTq, pMsg);
8,774✔
864
    case TDMT_STREAM_RETRIEVE:
227✔
865
      return tqProcessTaskRetrieveReq(pVnode->pTq, pMsg);
227✔
866
    case TDMT_STREAM_RETRIEVE_RSP:
158✔
867
      return tqProcessTaskRetrieveRsp(pVnode->pTq, pMsg);
158✔
868
    case TDMT_VND_STREAM_SCAN_HISTORY:
694✔
869
      return tqProcessTaskScanHistory(pVnode->pTq, pMsg);
694✔
870
    case TDMT_STREAM_TASK_CHECKPOINT_READY:
2,978✔
871
      return tqProcessTaskCheckpointReadyMsg(pVnode->pTq, pMsg);
2,978✔
872
    case TDMT_STREAM_TASK_CHECKPOINT_READY_RSP:
2,978✔
873
      return tqProcessTaskCheckpointReadyRsp(pVnode->pTq, pMsg);
2,978✔
874
    case TDMT_STREAM_RETRIEVE_TRIGGER:
×
875
      return tqProcessTaskRetrieveTriggerReq(pVnode->pTq, pMsg);
×
876
    case TDMT_STREAM_RETRIEVE_TRIGGER_RSP:
×
877
      return tqProcessTaskRetrieveTriggerRsp(pVnode->pTq, pMsg);
×
878
    case TDMT_MND_STREAM_HEARTBEAT_RSP:
12,168✔
879
      return tqProcessStreamHbRsp(pVnode->pTq, pMsg);
12,168✔
880
    case TDMT_MND_STREAM_REQ_CHKPT_RSP:
1,360✔
881
      return tqProcessStreamReqCheckpointRsp(pVnode->pTq, pMsg);
1,360✔
882
    case TDMT_VND_GET_STREAM_PROGRESS:
×
883
      return tqStreamProgressRetrieveReq(pVnode->pTq, pMsg);
×
884
    case TDMT_MND_STREAM_CHKPT_REPORT_RSP:
2,672✔
885
      return tqProcessTaskChkptReportRsp(pVnode->pTq, pMsg);
2,672✔
886
    default:
×
887
      vError("unknown msg type:%d in stream queue", pMsg->msgType);
×
888
      return TSDB_CODE_APP_ERROR;
×
889
  }
890
}
891

892
void smaHandleRes(void *pVnode, int64_t smaId, const SArray *data) {
2✔
893
  int32_t code = tdProcessTSmaInsert(((SVnode *)pVnode)->pSma, smaId, (const char *)data);
2✔
894
  if (code) {
2!
895
    vError("failed to process sma result since %s", tstrerror(code));
×
896
  }
897
}
2✔
898

899
void vnodeUpdateMetaRsp(SVnode *pVnode, STableMetaRsp *pMetaRsp) {
31,743✔
900
  if (NULL == pMetaRsp) {
31,743!
901
    return;
×
902
  }
903

904
  strcpy(pMetaRsp->dbFName, pVnode->config.dbname);
31,743✔
905
  pMetaRsp->dbId = pVnode->config.dbId;
31,743✔
906
  pMetaRsp->vgId = TD_VID(pVnode);
31,743✔
907
  pMetaRsp->precision = pVnode->config.tsdbCfg.precision;
31,743✔
908
}
909

910
extern int32_t vnodeAsyncRetention(SVnode *pVnode, int64_t now);
911

912
static int32_t vnodeProcessTrimReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
659✔
913
  int32_t     code = 0;
659✔
914
  SVTrimDbReq trimReq = {0};
659✔
915

916
  // decode
917
  if (tDeserializeSVTrimDbReq(pReq, len, &trimReq) != 0) {
659!
918
    code = TSDB_CODE_INVALID_MSG;
×
919
    goto _exit;
×
920
  }
921

922
  vInfo("vgId:%d, trim vnode request will be processed, time:%d", pVnode->config.vgId, trimReq.timestamp);
661!
923

924
  code = vnodeAsyncRetention(pVnode, trimReq.timestamp);
662✔
925

926
_exit:
661✔
927
  return code;
661✔
928
}
929

930
extern int32_t vnodeAsyncS3Migrate(SVnode *pVnode, int64_t now);
931

932
static int32_t vnodeProcessS3MigrateReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
933
  int32_t          code = 0;
×
934
  SVS3MigrateDbReq s3migrateReq = {0};
×
935

936
  // decode
937
  if (tDeserializeSVS3MigrateDbReq(pReq, len, &s3migrateReq) != 0) {
×
938
    code = TSDB_CODE_INVALID_MSG;
×
939
    goto _exit;
×
940
  }
941

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

944
  code = vnodeAsyncS3Migrate(pVnode, s3migrateReq.timestamp);
×
945

946
_exit:
×
947
  return code;
×
948
}
949

950
static int32_t vnodeProcessDropTtlTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
951
  int               ret = 0;
×
952
  SVDropTtlTableReq ttlReq = {0};
×
953
  if (tDeserializeSVDropTtlTableReq(pReq, len, &ttlReq) != 0) {
×
954
    ret = TSDB_CODE_INVALID_MSG;
×
955
    goto end;
×
956
  }
957

958
  if (ttlReq.nUids != taosArrayGetSize(ttlReq.pTbUids)) {
×
959
    ret = TSDB_CODE_INVALID_MSG;
×
960
    goto end;
×
961
  }
962

963
  if (ttlReq.nUids != 0) {
×
964
    vInfo("vgId:%d, drop ttl table req will be processed, time:%d, ntbUids:%d", pVnode->config.vgId,
×
965
          ttlReq.timestampSec, ttlReq.nUids);
966
  }
967

968
  if (ttlReq.nUids > 0) {
×
969
    int32_t code = metaDropTables(pVnode->pMeta, ttlReq.pTbUids);
×
970
    if (code) return code;
×
971

972
    code = tqUpdateTbUidList(pVnode->pTq, ttlReq.pTbUids, false);
×
973
    if (code) {
×
974
      vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
975
    }
976
  }
977

978
end:
×
979
  taosArrayDestroy(ttlReq.pTbUids);
×
980
  return ret;
×
981
}
982

983
static int32_t vnodeProcessFetchTtlExpiredTbs(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
984
  int32_t                 code = -1;
×
985
  SMetaReader             mr = {0};
×
986
  SVDropTtlTableReq       ttlReq = {0};
×
987
  SVFetchTtlExpiredTbsRsp rsp = {0};
×
988
  SEncoder                encoder = {0};
×
989
  SArray                 *pNames = NULL;
×
990
  pRsp->msgType = TDMT_VND_FETCH_TTL_EXPIRED_TBS_RSP;
×
991
  pRsp->code = TSDB_CODE_SUCCESS;
×
992
  pRsp->pCont = NULL;
×
993
  pRsp->contLen = 0;
×
994

995
  if (tDeserializeSVDropTtlTableReq(pReq, len, &ttlReq) != 0) {
×
996
    terrno = TSDB_CODE_INVALID_MSG;
×
997
    goto _end;
×
998
  }
999

1000
  if (!(ttlReq.nUids == taosArrayGetSize(ttlReq.pTbUids))) {
×
1001
    terrno = TSDB_CODE_INVALID_MSG;
×
1002
    goto _end;
×
1003
  }
1004

1005
  tb_uid_t    suid;
1006
  char        ctbName[TSDB_TABLE_NAME_LEN];
1007
  SVDropTbReq expiredTb = {.igNotExists = true};
×
1008
  metaReaderDoInit(&mr, pVnode->pMeta, 0);
×
1009
  rsp.vgId = TD_VID(pVnode);
×
1010
  rsp.pExpiredTbs = taosArrayInit(ttlReq.nUids, sizeof(SVDropTbReq));
×
1011
  if (!rsp.pExpiredTbs) goto _end;
×
1012

1013
  pNames = taosArrayInit(ttlReq.nUids, TSDB_TABLE_NAME_LEN);
×
1014
  if (!pNames) {
×
1015
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1016
    goto _end;
×
1017
  }
1018
  char buf[TSDB_TABLE_NAME_LEN];
1019
  for (int32_t i = 0; i < ttlReq.nUids; ++i) {
×
1020
    tb_uid_t *uid = taosArrayGet(ttlReq.pTbUids, i);
×
1021
    expiredTb.suid = *uid;
×
1022
    terrno = metaReaderGetTableEntryByUid(&mr, *uid);
×
1023
    if (terrno < 0) goto _end;
×
1024
    strncpy(buf, mr.me.name, TSDB_TABLE_NAME_LEN);
×
1025
    void *p = taosArrayPush(pNames, buf);
×
1026
    if (p == NULL) {
×
1027
      goto _end;
×
1028
    }
1029

1030
    expiredTb.name = p;
×
1031
    if (mr.me.type == TSDB_CHILD_TABLE) {
×
1032
      expiredTb.suid = mr.me.ctbEntry.suid;
×
1033
    }
1034

1035
    if (taosArrayPush(rsp.pExpiredTbs, &expiredTb) == NULL) {
×
1036
      goto _end;
×
1037
    }
1038
  }
1039

1040
  int32_t ret = 0;
×
1041
  tEncodeSize(tEncodeVFetchTtlExpiredTbsRsp, &rsp, pRsp->contLen, ret);
×
1042
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
×
1043
  if (pRsp->pCont == NULL) {
×
1044
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1045
    code = -1;
×
1046
    goto _end;
×
1047
  }
1048
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
×
1049
  terrno = tEncodeVFetchTtlExpiredTbsRsp(&encoder, &rsp);
×
1050
  tEncoderClear(&encoder);
×
1051

1052
  if (terrno == 0) code = 0;
×
1053
_end:
×
1054
  metaReaderClear(&mr);
×
1055
  tFreeFetchTtlExpiredTbsRsp(&rsp);
×
1056
  taosArrayDestroy(ttlReq.pTbUids);
×
1057
  if (pNames) taosArrayDestroy(pNames);
×
1058
  pRsp->code = terrno;
×
1059
  return code;
×
1060
}
1061

1062
static int32_t vnodeProcessCreateStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
8,050✔
1063
  int32_t        code = 0;
8,050✔
1064
  SVCreateStbReq req = {0};
8,050✔
1065
  SDecoder       coder;
1066

1067
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
8,050✔
1068
  pRsp->code = TSDB_CODE_SUCCESS;
8,050✔
1069
  pRsp->pCont = NULL;
8,050✔
1070
  pRsp->contLen = 0;
8,050✔
1071

1072
  // decode and process req
1073
  tDecoderInit(&coder, pReq, len);
8,050✔
1074

1075
  code = tDecodeSVCreateStbReq(&coder, &req);
8,047✔
1076
  if (code) {
8,074!
1077
    pRsp->code = code;
×
1078
    goto _err;
×
1079
  }
1080

1081
  code = metaCreateSTable(pVnode->pMeta, ver, &req);
8,074✔
1082
  if (code) {
8,060✔
1083
    pRsp->code = code;
3✔
1084
    goto _err;
3✔
1085
  }
1086

1087
  if ((code = tdProcessRSmaCreate(pVnode->pSma, &req)) < 0) {
8,057!
1088
    pRsp->code = code;
×
1089
    goto _err;
×
1090
  }
1091

1092
  tDecoderClear(&coder);
8,094✔
1093
  return 0;
8,089✔
1094

1095
_err:
3✔
1096
  tDecoderClear(&coder);
3✔
1097
  return code;
3✔
1098
}
1099

1100
static int32_t vnodeProcessCreateTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
16,570✔
1101
                                       SRpcMsg *pOriginRpc) {
1102
  SDecoder           decoder = {0};
16,570✔
1103
  SEncoder           encoder = {0};
16,570✔
1104
  int32_t            rcode = 0;
16,570✔
1105
  SVCreateTbBatchReq req = {0};
16,570✔
1106
  SVCreateTbReq     *pCreateReq;
1107
  SVCreateTbBatchRsp rsp = {0};
16,570✔
1108
  SVCreateTbRsp      cRsp = {0};
16,570✔
1109
  char               tbName[TSDB_TABLE_FNAME_LEN];
1110
  STbUidStore       *pStore = NULL;
16,570✔
1111
  SArray            *tbUids = NULL;
16,570✔
1112
  SArray            *tbNames = NULL;
16,570✔
1113

1114
  pRsp->msgType = TDMT_VND_CREATE_TABLE_RSP;
16,570✔
1115
  pRsp->code = TSDB_CODE_SUCCESS;
16,570✔
1116
  pRsp->pCont = NULL;
16,570✔
1117
  pRsp->contLen = 0;
16,570✔
1118

1119
  // decode
1120
  tDecoderInit(&decoder, pReq, len);
16,570✔
1121
  if (tDecodeSVCreateTbBatchReq(&decoder, &req) < 0) {
16,567!
1122
    rcode = -1;
×
1123
    terrno = TSDB_CODE_INVALID_MSG;
×
1124
    goto _exit;
×
1125
  }
1126

1127
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(cRsp));
16,569✔
1128
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
16,571✔
1129
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
16,572✔
1130
  if (rsp.pArray == NULL || tbUids == NULL || tbNames == NULL) {
16,571!
UNCOV
1131
    rcode = -1;
×
UNCOV
1132
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1133
    goto _exit;
×
1134
  }
1135

1136
  // loop to create table
1137
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
46,719✔
1138
    pCreateReq = req.pReqs + iReq;
30,154✔
1139
    memset(&cRsp, 0, sizeof(cRsp));
30,154✔
1140

1141
    if (tsEnableAudit && tsEnableAuditCreateTable) {
30,154!
1142
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
30,153✔
1143
      if (str == NULL) {
30,158!
1144
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1145
        rcode = -1;
×
1146
        goto _exit;
×
1147
      }
1148
      strcpy(str, pCreateReq->name);
30,158✔
1149
      if (taosArrayPush(tbNames, &str) == NULL) {
30,155!
1150
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1151
        rcode = -1;
×
1152
        goto _exit;
×
1153
      }
1154
    }
1155

1156
    // validate hash
1157
    sprintf(tbName, "%s.%s", pVnode->config.dbname, pCreateReq->name);
30,156✔
1158
    if (vnodeValidateTableHash(pVnode, tbName) < 0) {
30,156✔
1159
      cRsp.code = TSDB_CODE_VND_HASH_MISMATCH;
12✔
1160
      if (taosArrayPush(rsp.pArray, &cRsp) == NULL) {
24!
1161
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1162
        rcode = -1;
×
1163
        goto _exit;
×
1164
      }
1165
      vError("vgId:%d create-table:%s failed due to hash value mismatch", TD_VID(pVnode), tbName);
12!
1166
      continue;
12✔
1167
    }
1168

1169
    // do create table
1170
    if (metaCreateTable(pVnode->pMeta, ver, pCreateReq, &cRsp.pMeta) < 0) {
30,145✔
1171
      if (pCreateReq->flags & TD_CREATE_IF_NOT_EXISTS && terrno == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
206!
1172
        cRsp.code = TSDB_CODE_SUCCESS;
3✔
1173
      } else {
1174
        cRsp.code = terrno;
203✔
1175
      }
1176
    } else {
1177
      cRsp.code = TSDB_CODE_SUCCESS;
29,938✔
1178
      if (tdFetchTbUidList(pVnode->pSma, &pStore, pCreateReq->ctb.suid, pCreateReq->uid) < 0) {
29,938!
1179
        vError("vgId:%d, failed to fetch tbUid list", TD_VID(pVnode));
×
1180
      }
1181
      if (taosArrayPush(tbUids, &pCreateReq->uid) == NULL) {
59,873!
1182
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1183
        rcode = -1;
×
1184
        goto _exit;
×
1185
      }
1186
      vnodeUpdateMetaRsp(pVnode, cRsp.pMeta);
29,933✔
1187
    }
1188

1189
    if (taosArrayPush(rsp.pArray, &cRsp) == NULL) {
60,274!
1190
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1191
      rcode = -1;
×
1192
      goto _exit;
×
1193
    }
1194
  }
1195

1196
  vDebug("vgId:%d, add %d new created tables into query table list", TD_VID(pVnode), (int32_t)taosArrayGetSize(tbUids));
16,565✔
1197
  if (tqUpdateTbUidList(pVnode->pTq, tbUids, true) < 0) {
16,565!
1198
    vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(terrno));
×
1199
  }
1200
  if (tdUpdateTbUidList(pVnode->pSma, pStore, true) < 0) {
16,572!
1201
    goto _exit;
×
1202
  }
1203
  pStore = tdUidStoreFree(pStore);
16,573✔
1204

1205
  // prepare rsp
1206
  int32_t ret = 0;
16,572✔
1207
  tEncodeSize(tEncodeSVCreateTbBatchRsp, &rsp, pRsp->contLen, ret);
16,572!
1208
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
16,571✔
1209
  if (pRsp->pCont == NULL) {
16,573!
1210
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1211
    rcode = -1;
×
1212
    goto _exit;
×
1213
  }
1214
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
16,573✔
1215
  if (tEncodeSVCreateTbBatchRsp(&encoder, &rsp) != 0) {
16,572!
1216
    vError("vgId:%d, failed to encode create table batch response", TD_VID(pVnode));
×
1217
  }
1218

1219
  if (tsEnableAudit && tsEnableAuditCreateTable) {
16,571!
1220
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
16,571✔
1221

1222
    SName name = {0};
16,571✔
1223
    if (tNameFromString(&name, pVnode->config.dbname, T_NAME_ACCT | T_NAME_DB) < 0) {
16,571!
1224
      vError("vgId:%d, failed to get name from string", TD_VID(pVnode));
×
1225
    }
1226

1227
    SStringBuilder sb = {0};
16,570✔
1228
    for (int32_t i = 0; i < tbNames->size; i++) {
46,728✔
1229
      char **key = (char **)taosArrayGet(tbNames, i);
30,162✔
1230
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
30,158✔
1231
      if (i < tbNames->size - 1) {
30,159✔
1232
        taosStringBuilderAppendChar(&sb, ',');
13,590✔
1233
      }
1234
      // taosMemoryFreeClear(*key);
1235
    }
1236

1237
    size_t len = 0;
16,566✔
1238
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
16,566✔
1239

1240
    if (pOriginRpc->info.conn.user != NULL && strlen(pOriginRpc->info.conn.user) > 0) {
16,569!
1241
      auditAddRecord(pOriginRpc, clusterId, "createTable", name.dbname, "", keyJoined, len);
14,246✔
1242
    }
1243

1244
    taosStringBuilderDestroy(&sb);
16,570✔
1245
  }
1246

UNCOV
1247
_exit:
×
1248
  tDeleteSVCreateTbBatchReq(&req);
16,570✔
1249
  taosArrayDestroyEx(rsp.pArray, tFreeSVCreateTbRsp);
16,571✔
1250
  taosArrayDestroy(tbUids);
16,572✔
1251
  tDecoderClear(&decoder);
16,573✔
1252
  tEncoderClear(&encoder);
16,573✔
1253
  taosArrayDestroyP(tbNames, taosMemoryFree);
16,573✔
1254
  return rcode;
16,573✔
1255
}
1256

1257
static int32_t vnodeProcessAlterStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
568✔
1258
  int32_t        code = 0;
568✔
1259
  SVCreateStbReq req = {0};
568✔
1260
  SDecoder       dc = {0};
568✔
1261

1262
  pRsp->msgType = TDMT_VND_ALTER_STB_RSP;
568✔
1263
  pRsp->code = TSDB_CODE_SUCCESS;
568✔
1264
  pRsp->pCont = NULL;
568✔
1265
  pRsp->contLen = 0;
568✔
1266

1267
  tDecoderInit(&dc, pReq, len);
568✔
1268

1269
  // decode req
1270
  code = tDecodeSVCreateStbReq(&dc, &req);
569✔
1271
  if (code) {
577!
1272
    tDecoderClear(&dc);
×
1273
    return code;
×
1274
  }
1275

1276
  code = metaAlterSTable(pVnode->pMeta, ver, &req);
577✔
1277
  if (code) {
577!
1278
    pRsp->code = code;
×
1279
    tDecoderClear(&dc);
×
1280
    return code;
×
1281
  }
1282

1283
  tDecoderClear(&dc);
577✔
1284

1285
  return 0;
577✔
1286
}
1287

1288
static int32_t vnodeProcessDropStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
870✔
1289
  SVDropStbReq req = {0};
870✔
1290
  int32_t      rcode = TSDB_CODE_SUCCESS;
870✔
1291
  SDecoder     decoder = {0};
870✔
1292
  SArray      *tbUidList = NULL;
870✔
1293

1294
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
870✔
1295
  pRsp->pCont = NULL;
870✔
1296
  pRsp->contLen = 0;
870✔
1297

1298
  // decode request
1299
  tDecoderInit(&decoder, pReq, len);
870✔
1300
  if (tDecodeSVDropStbReq(&decoder, &req) < 0) {
872!
1301
    rcode = TSDB_CODE_INVALID_MSG;
×
1302
    goto _exit;
×
1303
  }
1304

1305
  // process request
1306
  tbUidList = taosArrayInit(8, sizeof(int64_t));
876✔
1307
  if (tbUidList == NULL) goto _exit;
881!
1308
  if (metaDropSTable(pVnode->pMeta, ver, &req, tbUidList) < 0) {
881✔
1309
    rcode = terrno;
7✔
1310
    goto _exit;
7✔
1311
  }
1312

1313
  if (tqUpdateTbUidList(pVnode->pTq, tbUidList, false) < 0) {
874!
1314
    rcode = terrno;
×
1315
    goto _exit;
×
1316
  }
1317

1318
  if (tdProcessRSmaDrop(pVnode->pSma, &req) < 0) {
874!
1319
    rcode = terrno;
×
1320
    goto _exit;
×
1321
  }
1322

1323
  // return rsp
1324
_exit:
874✔
1325
  if (tbUidList) taosArrayDestroy(tbUidList);
881!
1326
  pRsp->code = rcode;
881✔
1327
  tDecoderClear(&decoder);
881✔
1328
  return 0;
881✔
1329
}
1330

1331
static int32_t vnodeProcessAlterTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
598✔
1332
  SVAlterTbReq  vAlterTbReq = {0};
598✔
1333
  SVAlterTbRsp  vAlterTbRsp = {0};
598✔
1334
  SDecoder      dc = {0};
598✔
1335
  int32_t       rcode = 0;
598✔
1336
  int32_t       ret;
1337
  SEncoder      ec = {0};
598✔
1338
  STableMetaRsp vMetaRsp = {0};
598✔
1339

1340
  pRsp->msgType = TDMT_VND_ALTER_TABLE_RSP;
598✔
1341
  pRsp->pCont = NULL;
598✔
1342
  pRsp->contLen = 0;
598✔
1343
  pRsp->code = TSDB_CODE_SUCCESS;
598✔
1344

1345
  tDecoderInit(&dc, pReq, len);
598✔
1346

1347
  // decode
1348
  if (tDecodeSVAlterTbReq(&dc, &vAlterTbReq) < 0) {
598!
1349
    vAlterTbRsp.code = TSDB_CODE_INVALID_MSG;
×
1350
    tDecoderClear(&dc);
×
1351
    rcode = -1;
×
1352
    goto _exit;
×
1353
  }
1354

1355
  // process
1356
  if (metaAlterTable(pVnode->pMeta, ver, &vAlterTbReq, &vMetaRsp) < 0) {
598✔
1357
    vAlterTbRsp.code = terrno;
3✔
1358
    tDecoderClear(&dc);
3✔
1359
    rcode = -1;
3✔
1360
    goto _exit;
3✔
1361
  }
1362
  tDecoderClear(&dc);
595✔
1363

1364
  if (NULL != vMetaRsp.pSchemas) {
595✔
1365
    vnodeUpdateMetaRsp(pVnode, &vMetaRsp);
74✔
1366
    vAlterTbRsp.pMeta = &vMetaRsp;
74✔
1367
  }
1368

1369
_exit:
521✔
1370
  tEncodeSize(tEncodeSVAlterTbRsp, &vAlterTbRsp, pRsp->contLen, ret);
598!
1371
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
598✔
1372
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
598✔
1373
  if (tEncodeSVAlterTbRsp(&ec, &vAlterTbRsp) != 0) {
598!
1374
    vError("vgId:%d, failed to encode alter table response", TD_VID(pVnode));
×
1375
  }
1376
  tEncoderClear(&ec);
598✔
1377
  if (vMetaRsp.pSchemas) {
598✔
1378
    taosMemoryFree(vMetaRsp.pSchemas);
74✔
1379
    taosMemoryFree(vMetaRsp.pSchemaExt);
74✔
1380
  }
1381
  return 0;
598✔
1382
}
1383

1384
static int32_t vnodeProcessDropTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
325✔
1385
                                     SRpcMsg *pOriginRpc) {
1386
  SVDropTbBatchReq req = {0};
325✔
1387
  SVDropTbBatchRsp rsp = {0};
325✔
1388
  SDecoder         decoder = {0};
325✔
1389
  SEncoder         encoder = {0};
325✔
1390
  int32_t          ret;
1391
  SArray          *tbUids = NULL;
325✔
1392
  STbUidStore     *pStore = NULL;
325✔
1393
  SArray          *tbNames = NULL;
325✔
1394

1395
  pRsp->msgType = TDMT_VND_DROP_TABLE_RSP;
325✔
1396
  pRsp->pCont = NULL;
325✔
1397
  pRsp->contLen = 0;
325✔
1398
  pRsp->code = TSDB_CODE_SUCCESS;
325✔
1399

1400
  // decode req
1401
  tDecoderInit(&decoder, pReq, len);
325✔
1402
  ret = tDecodeSVDropTbBatchReq(&decoder, &req);
325✔
1403
  if (ret < 0) {
325!
1404
    terrno = TSDB_CODE_INVALID_MSG;
×
1405
    pRsp->code = terrno;
×
1406
    goto _exit;
×
1407
  }
1408

1409
  // process req
1410
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
325✔
1411
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(SVDropTbRsp));
325✔
1412
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
325✔
1413
  if (tbUids == NULL || rsp.pArray == NULL || tbNames == NULL) goto _exit;
325!
1414

1415
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
650✔
1416
    SVDropTbReq *pDropTbReq = req.pReqs + iReq;
325✔
1417
    SVDropTbRsp  dropTbRsp = {0};
325✔
1418
    tb_uid_t     tbUid = 0;
325✔
1419

1420
    /* code */
1421
    ret = metaDropTable(pVnode->pMeta, ver, pDropTbReq, tbUids, &tbUid);
325✔
1422
    if (ret < 0) {
325!
1423
      if (pDropTbReq->igNotExists && terrno == TSDB_CODE_TDB_TABLE_NOT_EXIST) {
×
1424
        dropTbRsp.code = TSDB_CODE_SUCCESS;
×
1425
      } else {
1426
        dropTbRsp.code = terrno;
×
1427
      }
1428
    } else {
1429
      dropTbRsp.code = TSDB_CODE_SUCCESS;
325✔
1430
      if (tbUid > 0) {
325✔
1431
        if (tdFetchTbUidList(pVnode->pSma, &pStore, pDropTbReq->suid, tbUid) < 0) {
84!
1432
          vError("vgId:%d, failed to fetch tbUid list", TD_VID(pVnode));
×
1433
        }
1434
      }
1435
    }
1436

1437
    if (taosArrayPush(rsp.pArray, &dropTbRsp) == NULL) {
650!
1438
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1439
      pRsp->code = terrno;
×
1440
      goto _exit;
×
1441
    }
1442

1443
    if (tsEnableAuditCreateTable) {
325!
1444
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
325✔
1445
      if (str == NULL) {
325!
1446
        pRsp->code = terrno;
×
1447
        goto _exit;
×
1448
      }
1449
      strcpy(str, pDropTbReq->name);
325✔
1450
      if (taosArrayPush(tbNames, &str) == NULL) {
325!
1451
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1452
        pRsp->code = terrno;
×
1453
        goto _exit;
×
1454
      }
1455
    }
1456
  }
1457

1458
  if (tqUpdateTbUidList(pVnode->pTq, tbUids, false) < 0) {
325!
1459
    vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(terrno));
×
1460
  }
1461

1462
  if (tdUpdateTbUidList(pVnode->pSma, pStore, false) < 0) {
325!
1463
    goto _exit;
×
1464
  }
1465

1466
  if (tsEnableAuditCreateTable) {
325!
1467
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
325✔
1468

1469
    SName name = {0};
325✔
1470
    if (tNameFromString(&name, pVnode->config.dbname, T_NAME_ACCT | T_NAME_DB) != 0) {
325!
1471
      vError("vgId:%d, failed to get name from string", TD_VID(pVnode));
×
1472
    }
1473

1474
    SStringBuilder sb = {0};
325✔
1475
    for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
650✔
1476
      char **key = (char **)taosArrayGet(tbNames, iReq);
325✔
1477
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
325✔
1478
      if (iReq < req.nReqs - 1) {
325!
1479
        taosStringBuilderAppendChar(&sb, ',');
×
1480
      }
1481
      taosMemoryFreeClear(*key);
325!
1482
    }
1483

1484
    size_t len = 0;
325✔
1485
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
325✔
1486

1487
    if (pOriginRpc->info.conn.user != NULL && strlen(pOriginRpc->info.conn.user) > 0) {
325!
1488
      auditAddRecord(pOriginRpc, clusterId, "dropTable", name.dbname, "", keyJoined, len);
325✔
1489
    }
1490

1491
    taosStringBuilderDestroy(&sb);
325✔
1492
  }
1493

1494
_exit:
×
1495
  taosArrayDestroy(tbUids);
325✔
1496
  pStore = tdUidStoreFree(pStore);
325✔
1497
  tDecoderClear(&decoder);
325✔
1498
  tEncodeSize(tEncodeSVDropTbBatchRsp, &rsp, pRsp->contLen, ret);
325!
1499
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
325✔
1500
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
325✔
1501
  if (tEncodeSVDropTbBatchRsp(&encoder, &rsp) != 0) {
325!
1502
    vError("vgId:%d, failed to encode drop table batch response", TD_VID(pVnode));
×
1503
  }
1504
  tEncoderClear(&encoder);
325✔
1505
  taosArrayDestroy(rsp.pArray);
325✔
1506
  taosArrayDestroy(tbNames);
325✔
1507
  return 0;
325✔
1508
}
1509

1510
#ifdef BUILD_NO_CALL
1511
static int32_t vnodeDebugPrintSingleSubmitMsg(SMeta *pMeta, SSubmitBlk *pBlock, SSubmitMsgIter *msgIter,
1512
                                              const char *tags) {
1513
  SSubmitBlkIter blkIter = {0};
1514
  STSchema      *pSchema = NULL;
1515
  tb_uid_t       suid = 0;
1516
  STSRow        *row = NULL;
1517
  int32_t        rv = -1;
1518

1519
  tInitSubmitBlkIter(msgIter, pBlock, &blkIter);
1520
  if (blkIter.row == NULL) return 0;
1521

1522
  int32_t code = metaGetTbTSchemaNotNull(pMeta, msgIter->suid, TD_ROW_SVER(blkIter.row), 1,
1523
                                         &pSchema);  // TODO: use the real schema
1524
  if (TSDB_CODE_SUCCESS != code) {
1525
    printf("%s:%d no valid schema\n", tags, __LINE__);
1526
    return code;
1527
  }
1528

1529
  suid = msgIter->suid;
1530
  rv = TD_ROW_SVER(blkIter.row);
1531

1532
  char __tags[128] = {0};
1533
  snprintf(__tags, 128, "%s: uid %" PRIi64 " ", tags, msgIter->uid);
1534
  while ((row = tGetSubmitBlkNext(&blkIter))) {
1535
    tdSRowPrint(row, pSchema, __tags);
1536
  }
1537

1538
  taosMemoryFreeClear(pSchema);
1539

1540
  return TSDB_CODE_SUCCESS;
1541
}
1542
#endif
1543
typedef struct SSubmitReqConvertCxt {
1544
  SSubmitMsgIter msgIter;
1545
  SSubmitBlk    *pBlock;
1546
  SSubmitBlkIter blkIter;
1547
  STSRow        *pRow;
1548
  STSRowIter     rowIter;
1549
  SSubmitTbData *pTbData;
1550
  STSchema      *pTbSchema;
1551
  SArray        *pColValues;
1552
} SSubmitReqConvertCxt;
1553

1554
static int32_t vnodeResetTableCxt(SMeta *pMeta, SSubmitReqConvertCxt *pCxt) {
×
1555
  taosMemoryFreeClear(pCxt->pTbSchema);
×
1556
  int32_t code = metaGetTbTSchemaNotNull(pMeta, pCxt->msgIter.suid > 0 ? pCxt->msgIter.suid : pCxt->msgIter.uid,
×
1557
                                         pCxt->msgIter.sversion, 1, &pCxt->pTbSchema);
1558
  if (TSDB_CODE_SUCCESS != code) {
×
1559
    return code;
×
1560
  }
1561
  tdSTSRowIterInit(&pCxt->rowIter, pCxt->pTbSchema);
×
1562

1563
  tDestroySubmitTbData(pCxt->pTbData, TSDB_MSG_FLG_ENCODE);
×
1564
  if (NULL == pCxt->pTbData) {
×
1565
    pCxt->pTbData = taosMemoryCalloc(1, sizeof(SSubmitTbData));
×
1566
    if (NULL == pCxt->pTbData) {
×
1567
      return terrno;
×
1568
    }
1569
  }
1570
  pCxt->pTbData->flags = 0;
×
1571
  pCxt->pTbData->suid = pCxt->msgIter.suid;
×
1572
  pCxt->pTbData->uid = pCxt->msgIter.uid;
×
1573
  pCxt->pTbData->sver = pCxt->msgIter.sversion;
×
1574
  pCxt->pTbData->pCreateTbReq = NULL;
×
1575
  pCxt->pTbData->aRowP = taosArrayInit(128, POINTER_BYTES);
×
1576
  if (NULL == pCxt->pTbData->aRowP) {
×
1577
    return terrno;
×
1578
  }
1579

1580
  taosArrayDestroy(pCxt->pColValues);
×
1581
  pCxt->pColValues = taosArrayInit(pCxt->pTbSchema->numOfCols, sizeof(SColVal));
×
1582
  if (NULL == pCxt->pColValues) {
×
1583
    return terrno;
×
1584
  }
1585
  for (int32_t i = 0; i < pCxt->pTbSchema->numOfCols; ++i) {
×
1586
    SColVal val = COL_VAL_NONE(pCxt->pTbSchema->columns[i].colId, pCxt->pTbSchema->columns[i].type);
×
1587
    if (taosArrayPush(pCxt->pColValues, &val) == NULL) {
×
1588
      return terrno;
×
1589
    }
1590
  }
1591

1592
  return TSDB_CODE_SUCCESS;
×
1593
}
1594

1595
static void vnodeDestroySubmitReqConvertCxt(SSubmitReqConvertCxt *pCxt) {
×
1596
  taosMemoryFreeClear(pCxt->pTbSchema);
×
1597
  tDestroySubmitTbData(pCxt->pTbData, TSDB_MSG_FLG_ENCODE);
×
1598
  taosMemoryFreeClear(pCxt->pTbData);
×
1599
  taosArrayDestroy(pCxt->pColValues);
×
1600
}
×
1601

1602
static int32_t vnodeCellValConvertToColVal(STColumn *pCol, SCellVal *pCellVal, SColVal *pColVal) {
×
1603
  if (tdValTypeIsNone(pCellVal->valType)) {
×
1604
    pColVal->flag = CV_FLAG_NONE;
×
1605
    return TSDB_CODE_SUCCESS;
×
1606
  }
1607

1608
  if (tdValTypeIsNull(pCellVal->valType)) {
×
1609
    pColVal->flag = CV_FLAG_NULL;
×
1610
    return TSDB_CODE_SUCCESS;
×
1611
  }
1612

1613
  if (IS_VAR_DATA_TYPE(pCol->type)) {
×
1614
    pColVal->value.nData = varDataLen(pCellVal->val);
×
1615
    pColVal->value.pData = (uint8_t *)varDataVal(pCellVal->val);
×
1616
  } else if (TSDB_DATA_TYPE_FLOAT == pCol->type) {
×
1617
    float f = GET_FLOAT_VAL(pCellVal->val);
×
1618
    memcpy(&pColVal->value.val, &f, sizeof(f));
×
1619
  } else if (TSDB_DATA_TYPE_DOUBLE == pCol->type) {
×
1620
    pColVal->value.val = *(int64_t *)pCellVal->val;
×
1621
  } else {
1622
    GET_TYPED_DATA(pColVal->value.val, int64_t, pCol->type, pCellVal->val);
×
1623
  }
1624

1625
  pColVal->flag = CV_FLAG_VALUE;
×
1626
  return TSDB_CODE_SUCCESS;
×
1627
}
1628

1629
static int32_t vnodeTSRowConvertToColValArray(SSubmitReqConvertCxt *pCxt) {
×
1630
  int32_t code = TSDB_CODE_SUCCESS;
×
1631
  tdSTSRowIterReset(&pCxt->rowIter, pCxt->pRow);
×
1632
  for (int32_t i = 0; TSDB_CODE_SUCCESS == code && i < pCxt->pTbSchema->numOfCols; ++i) {
×
1633
    STColumn *pCol = pCxt->pTbSchema->columns + i;
×
1634
    SCellVal  cellVal = {0};
×
1635
    if (!tdSTSRowIterFetch(&pCxt->rowIter, pCol->colId, pCol->type, &cellVal)) {
×
1636
      break;
×
1637
    }
1638
    code = vnodeCellValConvertToColVal(pCol, &cellVal, (SColVal *)taosArrayGet(pCxt->pColValues, i));
×
1639
  }
1640
  return code;
×
1641
}
1642

1643
static int32_t vnodeDecodeCreateTbReq(SSubmitReqConvertCxt *pCxt) {
×
1644
  if (pCxt->msgIter.schemaLen <= 0) {
×
1645
    return TSDB_CODE_SUCCESS;
×
1646
  }
1647

1648
  pCxt->pTbData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
×
1649
  if (NULL == pCxt->pTbData->pCreateTbReq) {
×
1650
    return terrno;
×
1651
  }
1652

1653
  SDecoder decoder = {0};
×
1654
  tDecoderInit(&decoder, (uint8_t *)pCxt->pBlock->data, pCxt->msgIter.schemaLen);
×
1655
  int32_t code = tDecodeSVCreateTbReq(&decoder, pCxt->pTbData->pCreateTbReq);
×
1656
  tDecoderClear(&decoder);
×
1657

1658
  return code;
×
1659
}
1660

1661
static int32_t vnodeSubmitReqConvertToSubmitReq2(SVnode *pVnode, SSubmitReq *pReq, SSubmitReq2 *pReq2) {
×
1662
  pReq2->aSubmitTbData = taosArrayInit(128, sizeof(SSubmitTbData));
×
1663
  if (NULL == pReq2->aSubmitTbData) {
×
1664
    return terrno;
×
1665
  }
1666

1667
  SSubmitReqConvertCxt cxt = {0};
×
1668

1669
  int32_t code = tInitSubmitMsgIter(pReq, &cxt.msgIter);
×
1670
  while (TSDB_CODE_SUCCESS == code) {
×
1671
    code = tGetSubmitMsgNext(&cxt.msgIter, &cxt.pBlock);
×
1672
    if (TSDB_CODE_SUCCESS == code) {
×
1673
      if (NULL == cxt.pBlock) {
×
1674
        break;
×
1675
      }
1676
      code = vnodeResetTableCxt(pVnode->pMeta, &cxt);
×
1677
    }
1678
    if (TSDB_CODE_SUCCESS == code) {
×
1679
      code = tInitSubmitBlkIter(&cxt.msgIter, cxt.pBlock, &cxt.blkIter);
×
1680
    }
1681
    if (TSDB_CODE_SUCCESS == code) {
×
1682
      code = vnodeDecodeCreateTbReq(&cxt);
×
1683
    }
1684
    while (TSDB_CODE_SUCCESS == code && (cxt.pRow = tGetSubmitBlkNext(&cxt.blkIter)) != NULL) {
×
1685
      code = vnodeTSRowConvertToColValArray(&cxt);
×
1686
      if (TSDB_CODE_SUCCESS == code) {
×
1687
        SRow **pNewRow = taosArrayReserve(cxt.pTbData->aRowP, 1);
×
1688
        code = tRowBuild(cxt.pColValues, cxt.pTbSchema, pNewRow);
×
1689
      }
1690
    }
1691
    if (TSDB_CODE_SUCCESS == code) {
×
1692
      code = (NULL == taosArrayPush(pReq2->aSubmitTbData, cxt.pTbData) ? terrno : TSDB_CODE_SUCCESS);
×
1693
    }
1694
    if (TSDB_CODE_SUCCESS == code) {
×
1695
      taosMemoryFreeClear(cxt.pTbData);
×
1696
    }
1697
  }
1698

1699
  vnodeDestroySubmitReqConvertCxt(&cxt);
×
1700
  return code;
×
1701
}
1702

1703
static int32_t vnodeRebuildSubmitReqMsg(SSubmitReq2 *pSubmitReq, void **ppMsg) {
×
1704
  int32_t  code = TSDB_CODE_SUCCESS;
×
1705
  char    *pMsg = NULL;
×
1706
  uint32_t msglen = 0;
×
1707
  tEncodeSize(tEncodeSubmitReq, pSubmitReq, msglen, code);
×
1708
  if (TSDB_CODE_SUCCESS == code) {
×
1709
    pMsg = taosMemoryMalloc(msglen);
×
1710
    if (NULL == pMsg) {
×
1711
      code = terrno;
×
1712
    }
1713
  }
1714
  if (TSDB_CODE_SUCCESS == code) {
×
1715
    SEncoder encoder;
1716
    tEncoderInit(&encoder, (uint8_t *)pMsg, msglen);
×
1717
    code = tEncodeSubmitReq(&encoder, pSubmitReq);
×
1718
    tEncoderClear(&encoder);
×
1719
  }
1720
  if (TSDB_CODE_SUCCESS == code) {
×
1721
    *ppMsg = pMsg;
×
1722
  }
1723
  return code;
×
1724
}
1725

1726
static int32_t vnodeProcessSubmitReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
4,249,090✔
1727
                                     SRpcMsg *pOriginalMsg) {
1728
  int32_t code = 0;
4,249,090✔
1729
  terrno = 0;
4,249,090✔
1730

1731
  SSubmitReq2 *pSubmitReq = &(SSubmitReq2){0};
4,249,280✔
1732
  SSubmitRsp2 *pSubmitRsp = &(SSubmitRsp2){0};
4,249,280✔
1733
  SArray      *newTbUids = NULL;
4,249,280✔
1734
  int32_t      ret;
1735
  SEncoder     ec = {0};
4,249,280✔
1736

1737
  pRsp->code = TSDB_CODE_SUCCESS;
4,249,280✔
1738

1739
  void           *pAllocMsg = NULL;
4,249,280✔
1740
  SSubmitReq2Msg *pMsg = (SSubmitReq2Msg *)pReq;
4,249,280✔
1741
  if (0 == pMsg->version) {
4,249,280!
1742
    code = vnodeSubmitReqConvertToSubmitReq2(pVnode, (SSubmitReq *)pMsg, pSubmitReq);
×
1743
    if (TSDB_CODE_SUCCESS == code) {
×
1744
      code = vnodeRebuildSubmitReqMsg(pSubmitReq, &pReq);
×
1745
    }
1746
    if (TSDB_CODE_SUCCESS == code) {
×
1747
      pAllocMsg = pReq;
×
1748
    }
1749
    if (TSDB_CODE_SUCCESS != code) {
×
1750
      goto _exit;
×
1751
    }
1752
  } else {
1753
    // decode
1754
    pReq = POINTER_SHIFT(pReq, sizeof(SSubmitReq2Msg));
4,249,280✔
1755
    len -= sizeof(SSubmitReq2Msg);
4,249,280✔
1756
    SDecoder dc = {0};
4,249,280✔
1757
    tDecoderInit(&dc, pReq, len);
4,249,280✔
1758
    if (tDecodeSubmitReq(&dc, pSubmitReq) < 0) {
4,249,151!
1759
      code = TSDB_CODE_INVALID_MSG;
×
1760
      goto _exit;
×
1761
    }
1762
    tDecoderClear(&dc);
4,249,230✔
1763
  }
1764

1765
  // scan
1766
  TSKEY now = taosGetTimestamp(pVnode->config.tsdbCfg.precision);
4,249,338✔
1767
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * pVnode->config.tsdbCfg.keep2;
4,249,116✔
1768
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
4,249,116✔
1769
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
8,521,415✔
1770
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
4,271,988✔
1771

1772
    if (pSubmitTbData->pCreateTbReq && pSubmitTbData->pCreateTbReq->uid == 0) {
4,271,994!
1773
      code = TSDB_CODE_INVALID_MSG;
×
1774
      goto _exit;
×
1775
    }
1776

1777
    if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
4,271,994✔
1778
      if (TARRAY_SIZE(pSubmitTbData->aCol) <= 0) {
6!
1779
        code = TSDB_CODE_INVALID_MSG;
×
1780
        goto _exit;
×
1781
      }
1782

1783
      SColData *colDataArr = TARRAY_DATA(pSubmitTbData->aCol);
6✔
1784
      SRowKey   lastKey;
1785
      tColDataArrGetRowKey(colDataArr, TARRAY_SIZE(pSubmitTbData->aCol), 0, &lastKey);
6✔
1786
      for (int32_t iRow = 1; iRow < colDataArr[0].nVal; iRow++) {
16✔
1787
        SRowKey key;
1788
        tColDataArrGetRowKey(TARRAY_DATA(pSubmitTbData->aCol), TARRAY_SIZE(pSubmitTbData->aCol), iRow, &key);
10✔
1789
        if (tRowKeyCompare(&lastKey, &key) >= 0) {
10!
1790
          code = TSDB_CODE_INVALID_MSG;
×
1791
          vError("vgId:%d %s failed 1 since %s, version:%" PRId64, TD_VID(pVnode), __func__, tstrerror(terrno), ver);
×
1792
          goto _exit;
×
1793
        }
1794
      }
1795
    } else {
1796
      int32_t nRow = TARRAY_SIZE(pSubmitTbData->aRowP);
4,271,988✔
1797
      SRow  **aRow = (SRow **)TARRAY_DATA(pSubmitTbData->aRowP);
4,271,988✔
1798
      SRowKey lastRowKey;
1799
      for (int32_t iRow = 0; iRow < nRow; ++iRow) {
25,667,085✔
1800
        if (aRow[iRow]->ts < minKey || aRow[iRow]->ts > maxKey) {
21,394,792!
1801
          code = TSDB_CODE_INVALID_MSG;
8,262✔
1802
          vError("vgId:%d %s failed 2 since %s, version:%" PRId64, TD_VID(pVnode), __func__, tstrerror(code), ver);
8,262!
1803
          goto _exit;
×
1804
        }
1805
        if (iRow == 0) {
21,386,530✔
1806
          tRowGetKey(aRow[iRow], &lastRowKey);
4,272,019✔
1807
        } else {
1808
          SRowKey rowKey;
1809
          tRowGetKey(aRow[iRow], &rowKey);
17,114,511✔
1810

1811
          if (tRowKeyCompare(&lastRowKey, &rowKey) >= 0) {
17,114,511!
1812
            code = TSDB_CODE_INVALID_MSG;
×
1813
            vError("vgId:%d %s failed 3 since %s, version:%" PRId64, TD_VID(pVnode), __func__, tstrerror(code), ver);
×
1814
            goto _exit;
×
1815
          }
1816
          lastRowKey = rowKey;
17,120,512✔
1817
        }
1818
      }
1819
    }
1820
  }
1821

1822
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
8,521,685✔
1823
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
4,271,977✔
1824

1825
    if (pSubmitTbData->pCreateTbReq) {
4,271,950✔
1826
      pSubmitTbData->uid = pSubmitTbData->pCreateTbReq->uid;
15,968✔
1827
    } else {
1828
      SMetaInfo info = {0};
4,255,982✔
1829

1830
      code = metaGetInfo(pVnode->pMeta, pSubmitTbData->uid, &info, NULL);
4,255,982✔
1831
      if (code) {
4,256,174!
UNCOV
1832
        code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
UNCOV
1833
        vWarn("vgId:%d, table uid:%" PRId64 " not exists", TD_VID(pVnode), pSubmitTbData->uid);
×
1834
        goto _exit;
×
1835
      }
1836

1837
      if (info.suid != pSubmitTbData->suid) {
4,256,198!
1838
        code = TSDB_CODE_INVALID_MSG;
×
1839
        goto _exit;
×
1840
      }
1841

1842
      if (info.suid) {
4,256,198✔
1843
        if (metaGetInfo(pVnode->pMeta, info.suid, &info, NULL) != 0) {
3,889,084!
1844
          vWarn("vgId:%d, table uid:%" PRId64 " not exists", TD_VID(pVnode), info.suid);
×
1845
        }
1846
      }
1847

1848
      if (pSubmitTbData->sver != info.skmVer) {
4,256,290!
1849
        code = TSDB_CODE_TDB_INVALID_TABLE_SCHEMA_VER;
×
1850
        goto _exit;
×
1851
      }
1852
    }
1853

1854
    if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
4,272,258✔
1855
      int32_t   nColData = TARRAY_SIZE(pSubmitTbData->aCol);
7✔
1856
      SColData *aColData = (SColData *)TARRAY_DATA(pSubmitTbData->aCol);
7✔
1857

1858
      if (nColData <= 0) {
7!
1859
        code = TSDB_CODE_INVALID_MSG;
×
1860
        goto _exit;
×
1861
      }
1862

1863
      if (aColData[0].cid != PRIMARYKEY_TIMESTAMP_COL_ID || aColData[0].type != TSDB_DATA_TYPE_TIMESTAMP ||
7!
1864
          aColData[0].nVal <= 0) {
7!
1865
        code = TSDB_CODE_INVALID_MSG;
×
1866
        goto _exit;
×
1867
      }
1868

1869
      for (int32_t j = 1; j < nColData; j++) {
28✔
1870
        if (aColData[j].nVal != aColData[0].nVal) {
21!
1871
          code = TSDB_CODE_INVALID_MSG;
×
1872
          goto _exit;
×
1873
        }
1874
      }
1875
    }
1876
  }
1877

1878
  vDebug("vgId:%d, submit block size %d", TD_VID(pVnode), (int32_t)taosArrayGetSize(pSubmitReq->aSubmitTbData));
4,249,708✔
1879

1880
  // loop to handle
1881
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
8,521,536✔
1882
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
4,272,159✔
1883

1884
    // create table
1885
    if (pSubmitTbData->pCreateTbReq) {
4,272,179✔
1886
      // alloc if need
1887
      if (pSubmitRsp->aCreateTbRsp == NULL &&
15,967✔
1888
          (pSubmitRsp->aCreateTbRsp = taosArrayInit(TARRAY_SIZE(pSubmitReq->aSubmitTbData), sizeof(SVCreateTbRsp))) ==
15,616!
1889
              NULL) {
1890
        code = terrno;
×
1891
        goto _exit;
×
1892
      }
1893

1894
      SVCreateTbRsp *pCreateTbRsp = taosArrayReserve(pSubmitRsp->aCreateTbRsp, 1);
15,968✔
1895

1896
      // create table
1897
      if (metaCreateTable(pVnode->pMeta, ver, pSubmitTbData->pCreateTbReq, &pCreateTbRsp->pMeta) == 0) {
15,968✔
1898
        // create table success
1899

1900
        if (newTbUids == NULL &&
3,127!
1901
            (newTbUids = taosArrayInit(TARRAY_SIZE(pSubmitReq->aSubmitTbData), sizeof(int64_t))) == NULL) {
1,389✔
1902
          code = terrno;
×
1903
          goto _exit;
×
1904
        }
1905

1906
        if (taosArrayPush(newTbUids, &pSubmitTbData->uid) == NULL) {
3,476!
1907
          code = terrno;
×
1908
          goto _exit;
×
1909
        }
1910

1911
        if (pCreateTbRsp->pMeta) {
1,738✔
1912
          vnodeUpdateMetaRsp(pVnode, pCreateTbRsp->pMeta);
1,737✔
1913
        }
1914
      } else {  // create table failed
1915
        if (terrno != TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
14,229!
1916
          code = terrno;
×
1917
          vError("vgId:%d failed to create table:%s, code:%s", TD_VID(pVnode), pSubmitTbData->pCreateTbReq->name,
×
1918
                 tstrerror(terrno));
1919
          goto _exit;
×
1920
        }
1921
        terrno = 0;
14,229✔
1922
        pSubmitTbData->uid = pSubmitTbData->pCreateTbReq->uid;  // update uid if table exist for using below
14,229✔
1923
      }
1924
    }
1925

1926
    // insert data
1927
    int32_t affectedRows;
1928
    code = tsdbInsertTableData(pVnode->pTsdb, ver, pSubmitTbData, &affectedRows);
4,272,179✔
1929
    if (code) goto _exit;
4,272,233!
1930

1931
    code = metaUpdateChangeTimeWithLock(pVnode->pMeta, pSubmitTbData->uid, pSubmitTbData->ctimeMs);
4,272,233✔
1932
    if (code) goto _exit;
4,272,191!
1933

1934
    pSubmitRsp->affectedRows += affectedRows;
4,272,191✔
1935
  }
1936

1937
  // update the affected table uid list
1938
  if (taosArrayGetSize(newTbUids) > 0) {
4,249,377✔
1939
    vDebug("vgId:%d, add %d table into query table list in handling submit", TD_VID(pVnode),
1,390✔
1940
           (int32_t)taosArrayGetSize(newTbUids));
1941
    if (tqUpdateTbUidList(pVnode->pTq, newTbUids, true) != 0) {
1,390!
1942
      vError("vgId:%d, failed to update tbUid list", TD_VID(pVnode));
×
1943
    }
1944
  }
1945

1946
_exit:
4,249,289✔
1947
  // message
1948
  pRsp->code = code;
4,249,289✔
1949
  tEncodeSize(tEncodeSSubmitRsp2, pSubmitRsp, pRsp->contLen, ret);
4,249,289!
1950
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
4,249,265✔
1951
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
4,249,334✔
1952
  if (tEncodeSSubmitRsp2(&ec, pSubmitRsp) < 0) {
4,249,336!
1953
    vError("vgId:%d, failed to encode submit response", TD_VID(pVnode));
×
1954
  }
1955
  tEncoderClear(&ec);
4,249,307✔
1956

1957
  // update statistics
1958
  (void)atomic_add_fetch_64(&pVnode->statis.nInsert, pSubmitRsp->affectedRows);
4,249,262✔
1959
  (void)atomic_add_fetch_64(&pVnode->statis.nInsertSuccess, pSubmitRsp->affectedRows);
4,249,373✔
1960
  (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsert, 1);
4,249,371✔
1961

1962
  if (tsEnableMonitor && tsMonitorFqdn[0] != 0 && tsMonitorPort != 0 && pSubmitRsp->affectedRows > 0 &&
4,249,377!
1963
      strlen(pOriginalMsg->info.conn.user) > 0 && tsInsertCounter != NULL) {
×
1964
    const char *sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT_AFFECTED_ROWS,
×
1965
                                   pVnode->monitor.strClusterId,
×
1966
                                   pVnode->monitor.strDnodeId,
×
1967
                                   tsLocalEp,
1968
                                   pVnode->monitor.strVgId,
×
1969
                                   pOriginalMsg->info.conn.user,
×
1970
                                   "Success"};
1971
    int         tv = taos_counter_add(tsInsertCounter, pSubmitRsp->affectedRows, sample_labels);
×
1972
  }
1973

1974
  if (code == 0) {
4,249,377✔
1975
    (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
4,249,354✔
1976
    code = tdProcessRSmaSubmit(pVnode->pSma, ver, pSubmitReq, pReq, len);
4,249,383✔
1977
  }
1978
  /*
1979
  if (code == 0) {
1980
    atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
1981
    code = tdProcessRSmaSubmit(pVnode->pSma, ver, pSubmitReq, pReq, len);
1982

1983
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT, pVnode->monitor.strClusterId,
1984
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
1985
                                          pOriginalMsg->info.conn.user, "Success"};
1986
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
1987
  }
1988
  else{
1989
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT, pVnode->monitor.strClusterId,
1990
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
1991
                                        pOriginalMsg->info.conn.user, "Failed"};
1992
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
1993
  }
1994
  */
1995

1996
  // clear
1997
  taosArrayDestroy(newTbUids);
4,249,340✔
1998
  tDestroySubmitReq(pSubmitReq, 0 == pMsg->version ? TSDB_MSG_FLG_CMPT : TSDB_MSG_FLG_DECODE);
4,249,286!
1999
  tDestroySSubmitRsp2(pSubmitRsp, TSDB_MSG_FLG_ENCODE);
4,249,353✔
2000

2001
  if (code) terrno = code;
4,249,287!
2002

2003
  taosMemoryFree(pAllocMsg);
4,249,287✔
2004

2005
  return code;
4,249,308✔
2006
}
2007

2008
static int32_t vnodeProcessCreateTSmaReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
24✔
2009
  SVCreateTSmaReq req = {0};
24✔
2010
  SDecoder        coder = {0};
24✔
2011

2012
  if (pRsp) {
24!
2013
    pRsp->msgType = TDMT_VND_CREATE_SMA_RSP;
24✔
2014
    pRsp->code = TSDB_CODE_SUCCESS;
24✔
2015
    pRsp->pCont = NULL;
24✔
2016
    pRsp->contLen = 0;
24✔
2017
  }
2018

2019
  // decode and process req
2020
  tDecoderInit(&coder, pReq, len);
24✔
2021

2022
  if (tDecodeSVCreateTSmaReq(&coder, &req) < 0) {
24!
2023
    terrno = TSDB_CODE_MSG_DECODE_ERROR;
×
2024
    if (pRsp) pRsp->code = terrno;
×
2025
    goto _err;
×
2026
  }
2027

2028
  if (tdProcessTSmaCreate(pVnode->pSma, ver, (const char *)&req) < 0) {
24!
2029
    if (pRsp) pRsp->code = terrno;
×
2030
    goto _err;
×
2031
  }
2032

2033
  tDecoderClear(&coder);
24✔
2034
  vDebug("vgId:%d, success to create tsma %s:%" PRIi64 " version %" PRIi64 " for table %" PRIi64, TD_VID(pVnode),
24!
2035
         req.indexName, req.indexUid, ver, req.tableUid);
2036
  return 0;
24✔
2037

2038
_err:
×
2039
  tDecoderClear(&coder);
×
2040
  vError("vgId:%d, failed to create tsma %s:%" PRIi64 " version %" PRIi64 "for table %" PRIi64 " since %s",
×
2041
         TD_VID(pVnode), req.indexName, req.indexUid, ver, req.tableUid, terrstr());
2042
  return terrno;
×
2043
}
2044

2045
/**
2046
 * @brief specific for smaDstVnode
2047
 *
2048
 * @param pVnode
2049
 * @param pCont
2050
 * @param contLen
2051
 * @return int32_t
2052
 */
2053
int32_t vnodeProcessCreateTSma(SVnode *pVnode, void *pCont, uint32_t contLen) {
×
2054
  return vnodeProcessCreateTSmaReq(pVnode, 1, pCont, contLen, NULL);
×
2055
}
2056

UNCOV
2057
static int32_t vnodeConsolidateAlterHashRange(SVnode *pVnode, int64_t ver) {
×
UNCOV
2058
  int32_t code = TSDB_CODE_SUCCESS;
×
2059

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

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

UNCOV
2066
  return code;
×
2067
}
2068

2069
static int32_t vnodeProcessAlterConfirmReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
1,466✔
2070
  vInfo("vgId:%d, vnode handle msgType:alter-confirm, alter confirm msg is processed", TD_VID(pVnode));
1,466!
2071
  int32_t code = TSDB_CODE_SUCCESS;
1,466✔
2072
  if (!pVnode->config.hashChange) {
1,466!
2073
    goto _exit;
1,466✔
2074
  }
2075

UNCOV
2076
  code = vnodeConsolidateAlterHashRange(pVnode, ver);
×
UNCOV
2077
  if (code < 0) {
×
2078
    vError("vgId:%d, failed to consolidate alter hashrange since %s. version:%" PRId64, TD_VID(pVnode), terrstr(), ver);
×
2079
    goto _exit;
×
2080
  }
UNCOV
2081
  pVnode->config.hashChange = false;
×
2082

2083
_exit:
1,466✔
2084
  pRsp->msgType = TDMT_VND_ALTER_CONFIRM_RSP;
1,466✔
2085
  pRsp->code = code;
1,466✔
2086
  pRsp->pCont = NULL;
1,466✔
2087
  pRsp->contLen = 0;
1,466✔
2088

2089
  return code;
1,466✔
2090
}
2091

2092
extern int32_t tsdbDisableAndCancelAllBgTask(STsdb *pTsdb);
2093
extern void    tsdbEnableBgTask(STsdb *pTsdb);
2094

2095
static int32_t vnodeProcessAlterConfigReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
441✔
2096
  bool walChanged = false;
441✔
2097
  bool tsdbChanged = false;
441✔
2098

2099
  SAlterVnodeConfigReq req = {0};
441✔
2100
  if (tDeserializeSAlterVnodeConfigReq(pReq, len, &req) != 0) {
441!
2101
    terrno = TSDB_CODE_INVALID_MSG;
×
2102
    return TSDB_CODE_INVALID_MSG;
×
2103
  }
2104

2105
  vInfo("vgId:%d, start to alter vnode config, page:%d pageSize:%d buffer:%d szPage:%d szBuf:%" PRIu64
443✔
2106
        " cacheLast:%d cacheLastSize:%d days:%d keep0:%d keep1:%d keep2:%d keepTimeOffset:%d s3KeepLocal:%d "
2107
        "s3Compact:%d fsync:%d level:%d "
2108
        "walRetentionPeriod:%d walRetentionSize:%d",
2109
        TD_VID(pVnode), req.pages, req.pageSize, req.buffer, req.pageSize * 1024, (uint64_t)req.buffer * 1024 * 1024,
2110
        req.cacheLast, req.cacheLastSize, req.daysPerFile, req.daysToKeep0, req.daysToKeep1, req.daysToKeep2,
2111
        req.keepTimeOffset, req.s3KeepLocal, req.s3Compact, req.walFsyncPeriod, req.walLevel, req.walRetentionPeriod,
2112
        req.walRetentionSize);
2113

2114
  if (pVnode->config.cacheLastSize != req.cacheLastSize) {
445✔
2115
    pVnode->config.cacheLastSize = req.cacheLastSize;
204✔
2116
    tsdbCacheSetCapacity(pVnode, (size_t)pVnode->config.cacheLastSize * 1024 * 1024);
204✔
2117
  }
2118

2119
  if (pVnode->config.szBuf != req.buffer * 1024LL * 1024LL) {
445!
2120
    vInfo("vgId:%d, vnode buffer is changed from %" PRId64 " to %" PRId64, TD_VID(pVnode), pVnode->config.szBuf,
×
2121
          (uint64_t)(req.buffer * 1024LL * 1024LL));
2122
    pVnode->config.szBuf = req.buffer * 1024LL * 1024LL;
×
2123
  }
2124

2125
  if (pVnode->config.szCache != req.pages) {
445!
2126
    if (metaAlterCache(pVnode->pMeta, req.pages) < 0) {
×
2127
      vError("vgId:%d, failed to change vnode pages from %d to %d failed since %s", TD_VID(pVnode),
×
2128
             pVnode->config.szCache, req.pages, tstrerror(errno));
2129
      return errno;
×
2130
    } else {
2131
      vInfo("vgId:%d, vnode pages is changed from %d to %d", TD_VID(pVnode), pVnode->config.szCache, req.pages);
×
2132
      pVnode->config.szCache = req.pages;
×
2133
    }
2134
  }
2135

2136
  if (pVnode->config.cacheLast != req.cacheLast) {
445✔
2137
    pVnode->config.cacheLast = req.cacheLast;
293✔
2138
  }
2139

2140
  if (pVnode->config.walCfg.fsyncPeriod != req.walFsyncPeriod) {
445✔
2141
    pVnode->config.walCfg.fsyncPeriod = req.walFsyncPeriod;
62✔
2142
    walChanged = true;
62✔
2143
  }
2144

2145
  if (pVnode->config.walCfg.level != req.walLevel) {
445✔
2146
    if (pVnode->config.walCfg.level == 0) {
42!
2147
      pVnode->config.walCfg.clearFiles = 1;
×
2148
    }
2149
    pVnode->config.walCfg.level = req.walLevel;
42✔
2150
    walChanged = true;
42✔
2151
  }
2152

2153
  if (pVnode->config.walCfg.retentionPeriod != req.walRetentionPeriod) {
445✔
2154
    pVnode->config.walCfg.retentionPeriod = req.walRetentionPeriod;
2✔
2155
    walChanged = true;
2✔
2156
  }
2157

2158
  if (pVnode->config.walCfg.retentionSize != req.walRetentionSize) {
445!
2159
    pVnode->config.walCfg.retentionSize = req.walRetentionSize;
×
2160
    walChanged = true;
×
2161
  }
2162

2163
  if (pVnode->config.tsdbCfg.keep0 != req.daysToKeep0) {
445✔
2164
    pVnode->config.tsdbCfg.keep0 = req.daysToKeep0;
42✔
2165
    if (!VND_IS_RSMA(pVnode)) {
42!
2166
      tsdbChanged = true;
42✔
2167
    }
2168
  }
2169

2170
  if (pVnode->config.tsdbCfg.keep1 != req.daysToKeep1) {
445✔
2171
    pVnode->config.tsdbCfg.keep1 = req.daysToKeep1;
42✔
2172
    if (!VND_IS_RSMA(pVnode)) {
42!
2173
      tsdbChanged = true;
42✔
2174
    }
2175
  }
2176

2177
  if (pVnode->config.tsdbCfg.keep2 != req.daysToKeep2) {
445✔
2178
    pVnode->config.tsdbCfg.keep2 = req.daysToKeep2;
44✔
2179
    if (!VND_IS_RSMA(pVnode)) {
44!
2180
      tsdbChanged = true;
44✔
2181
    }
2182
  }
2183

2184
  if (pVnode->config.tsdbCfg.keepTimeOffset != req.keepTimeOffset) {
445!
2185
    pVnode->config.tsdbCfg.keepTimeOffset = req.keepTimeOffset;
×
2186
    if (!VND_IS_RSMA(pVnode)) {
×
2187
      tsdbChanged = true;
×
2188
    }
2189
  }
2190

2191
  if (req.sttTrigger != -1 && req.sttTrigger != pVnode->config.sttTrigger) {
445!
2192
    if (req.sttTrigger > 1 && pVnode->config.sttTrigger > 1) {
×
2193
      pVnode->config.sttTrigger = req.sttTrigger;
×
2194
    } else {
2195
      vnodeAWait(&pVnode->commitTask);
×
2196

2197
      int32_t ret = tsdbDisableAndCancelAllBgTask(pVnode->pTsdb);
×
2198
      if (ret != 0) {
×
2199
        vError("vgId:%d, failed to disable bg task since %s", TD_VID(pVnode), tstrerror(errno));
×
2200
      }
2201

2202
      pVnode->config.sttTrigger = req.sttTrigger;
×
2203
      tsdbEnableBgTask(pVnode->pTsdb);
×
2204
    }
2205
  }
2206

2207
  if (req.minRows != -1 && req.minRows != pVnode->config.tsdbCfg.minRows) {
445!
2208
    pVnode->config.tsdbCfg.minRows = req.minRows;
×
2209
  }
2210

2211
  if (req.s3KeepLocal != -1 && req.s3KeepLocal != pVnode->config.s3KeepLocal) {
445!
2212
    pVnode->config.s3KeepLocal = req.s3KeepLocal;
×
2213
  }
2214
  if (req.s3Compact != -1 && req.s3Compact != pVnode->config.s3Compact) {
445!
2215
    pVnode->config.s3Compact = req.s3Compact;
×
2216
  }
2217

2218
  if (walChanged) {
445✔
2219
    if (walAlter(pVnode->pWal, &pVnode->config.walCfg) != 0) {
104!
2220
      vError("vgId:%d, failed to alter wal config since %s", TD_VID(pVnode), tstrerror(errno));
×
2221
    }
2222
  }
2223

2224
  if (tsdbChanged) {
443✔
2225
    tsdbSetKeepCfg(pVnode->pTsdb, &pVnode->config.tsdbCfg);
44✔
2226
  }
2227

2228
  return 0;
443✔
2229
}
2230

2231
static int32_t vnodeProcessBatchDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
1,903✔
2232
  SBatchDeleteReq deleteReq;
2233
  SDecoder        decoder;
2234
  tDecoderInit(&decoder, pReq, len);
1,903✔
2235
  if (tDecodeSBatchDeleteReq(&decoder, &deleteReq) < 0) {
1,903!
2236
    tDecoderClear(&decoder);
×
2237
    return terrno = TSDB_CODE_INVALID_MSG;
×
2238
  }
2239

2240
  SMetaReader mr = {0};
1,908✔
2241
  metaReaderDoInit(&mr, pVnode->pMeta, META_READER_NOLOCK);
1,908✔
2242
  STsdb *pTsdb = pVnode->pTsdb;
1,907✔
2243

2244
  if (deleteReq.level) {
1,907✔
2245
    pTsdb = deleteReq.level == 1 ? VND_RSMA1(pVnode) : VND_RSMA2(pVnode);
16✔
2246
  }
2247

2248
  int32_t sz = taosArrayGetSize(deleteReq.deleteReqs);
1,907✔
2249
  for (int32_t i = 0; i < sz; i++) {
4,685✔
2250
    SSingleDeleteReq *pOneReq = taosArrayGet(deleteReq.deleteReqs, i);
2,776✔
2251
    char             *name = pOneReq->tbname;
2,777✔
2252
    if (metaGetTableEntryByName(&mr, name) < 0) {
2,777✔
2253
      vDebug("vgId:%d, stream delete msg, skip since no table: %s", pVnode->config.vgId, name);
1,412✔
2254
      continue;
1,412✔
2255
    }
2256

2257
    int64_t uid = mr.me.uid;
1,368✔
2258

2259
    int32_t code = tsdbDeleteTableData(pTsdb, ver, deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
1,368✔
2260
    if (code < 0) {
1,370!
2261
      terrno = code;
×
2262
      vError("vgId:%d, delete error since %s, suid:%" PRId64 ", uid:%" PRId64 ", start ts:%" PRId64 ", end ts:%" PRId64,
×
2263
             TD_VID(pVnode), terrstr(), deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
2264
    }
2265

2266
    if (deleteReq.level == 0) {
1,370✔
2267
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, deleteReq.ctimeMs);
754✔
2268
      if (code < 0) {
754!
2269
        terrno = code;
×
2270
        vError("vgId:%d, update change time error since %s, suid:%" PRId64 ", uid:%" PRId64 ", start ts:%" PRId64
×
2271
               ", end ts:%" PRId64,
2272
               TD_VID(pVnode), terrstr(), deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
2273
      }
2274
    }
2275
    tDecoderClear(&mr.coder);
1,370✔
2276
  }
2277
  metaReaderClear(&mr);
1,909✔
2278
  taosArrayDestroy(deleteReq.deleteReqs);
1,910✔
2279
  return 0;
1,911✔
2280
}
2281

2282
static int32_t vnodeProcessDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
1,536✔
2283
                                     SRpcMsg *pOriginalMsg) {
2284
  int32_t     code = 0;
1,536✔
2285
  SDecoder   *pCoder = &(SDecoder){0};
1,536✔
2286
  SDeleteRes *pRes = &(SDeleteRes){0};
1,536✔
2287

2288
  pRsp->msgType = TDMT_VND_DELETE_RSP;
1,536✔
2289
  pRsp->pCont = NULL;
1,536✔
2290
  pRsp->contLen = 0;
1,536✔
2291
  pRsp->code = TSDB_CODE_SUCCESS;
1,536✔
2292

2293
  pRes->uidList = taosArrayInit(0, sizeof(tb_uid_t));
1,536✔
2294
  if (pRes->uidList == NULL) {
1,536!
2295
    code = terrno;
×
2296
    goto _err;
×
2297
  }
2298

2299
  tDecoderInit(pCoder, pReq, len);
1,536✔
2300
  code = tDecodeDeleteRes(pCoder, pRes);
1,537✔
2301
  if (code) goto _err;
1,537!
2302

2303
  if (pRes->affectedRows > 0) {
1,537✔
2304
    for (int32_t iUid = 0; iUid < taosArrayGetSize(pRes->uidList); iUid++) {
1,189✔
2305
      uint64_t uid = *(uint64_t *)taosArrayGet(pRes->uidList, iUid);
663✔
2306
      code = tsdbDeleteTableData(pVnode->pTsdb, ver, pRes->suid, uid, pRes->skey, pRes->ekey);
663✔
2307
      if (code) goto _err;
663!
2308
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, pRes->ctimeMs);
663✔
2309
      if (code) goto _err;
663!
2310
    }
2311
  }
2312

2313
  code = tdProcessRSmaDelete(pVnode->pSma, ver, pRes, pReq, len);
1,537✔
2314

2315
  tDecoderClear(pCoder);
1,536✔
2316
  taosArrayDestroy(pRes->uidList);
1,536✔
2317

2318
  SVDeleteRsp rsp = {.affectedRows = pRes->affectedRows};
1,537✔
2319
  int32_t     ret = 0;
1,537✔
2320
  tEncodeSize(tEncodeSVDeleteRsp, &rsp, pRsp->contLen, ret);
1,537!
2321
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
1,537✔
2322
  SEncoder ec = {0};
1,537✔
2323
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
1,537✔
2324
  code = tEncodeSVDeleteRsp(&ec, &rsp);
1,537✔
2325
  if (code) goto _err;
1,536!
2326
  tEncoderClear(&ec);
1,536✔
2327
  return code;
1,537✔
2328

2329
_err:
×
2330
  /*
2331
  if(code == TSDB_CODE_SUCCESS){
2332
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_DELETE, pVnode->monitor.strClusterId,
2333
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2334
                                        pOriginalMsg->info.conn.user, "Success"};
2335
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2336
  }
2337
  else{
2338
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_DELETE, pVnode->monitor.strClusterId,
2339
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2340
                                        pOriginalMsg->info.conn.user, "Failed"};
2341
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2342
  }
2343
  */
2344

2345
  return code;
×
2346
}
2347
static int32_t vnodeProcessCreateIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
579✔
2348
  SVCreateStbReq req = {0};
579✔
2349
  SDecoder       dc = {0};
579✔
2350
  int32_t        code = 0;
579✔
2351

2352
  pRsp->msgType = TDMT_VND_CREATE_INDEX_RSP;
579✔
2353
  pRsp->code = TSDB_CODE_SUCCESS;
579✔
2354
  pRsp->pCont = NULL;
579✔
2355
  pRsp->contLen = 0;
579✔
2356

2357
  tDecoderInit(&dc, pReq, len);
579✔
2358
  // decode req
2359
  if (tDecodeSVCreateStbReq(&dc, &req) < 0) {
579!
2360
    tDecoderClear(&dc);
×
2361
    return terrno = TSDB_CODE_INVALID_MSG;
×
2362
  }
2363

2364
  code = metaAddIndexToSTable(pVnode->pMeta, ver, &req);
579✔
2365
  if (code) {
579!
2366
    pRsp->code = code;
×
2367
    goto _err;
×
2368
  }
2369
  tDecoderClear(&dc);
579✔
2370
  return 0;
579✔
2371

2372
_err:
×
2373
  tDecoderClear(&dc);
×
2374
  return code;
×
2375
}
2376
static int32_t vnodeProcessDropIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
1,452✔
2377
  SDropIndexReq req = {0};
1,452✔
2378
  int32_t       code = 0;
1,452✔
2379
  pRsp->msgType = TDMT_VND_DROP_INDEX_RSP;
1,452✔
2380
  pRsp->code = TSDB_CODE_SUCCESS;
1,452✔
2381
  pRsp->pCont = NULL;
1,452✔
2382
  pRsp->contLen = 0;
1,452✔
2383

2384
  if ((code = tDeserializeSDropIdxReq(pReq, len, &req))) {
1,452!
2385
    pRsp->code = code;
×
2386
    return code;
×
2387
  }
2388

2389
  code = metaDropIndexFromSTable(pVnode->pMeta, ver, &req);
1,452✔
2390
  if (code) {
1,452!
2391
    pRsp->code = code;
×
2392
    return code;
×
2393
  }
2394
  return TSDB_CODE_SUCCESS;
1,452✔
2395
}
2396

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

UNCOV
2399
static int32_t vnodeProcessCompactVnodeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
UNCOV
2400
  if (!pVnode->restored) {
×
UNCOV
2401
    vInfo("vgId:%d, ignore compact req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
×
UNCOV
2402
    return 0;
×
2403
  }
UNCOV
2404
  return vnodeAsyncCompact(pVnode, ver, pReq, len, pRsp);
×
2405
}
2406

2407
static int32_t vnodeProcessConfigChangeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
2408
  if (syncCheckMember(pVnode->sync) != 0) {
×
2409
    vError("vgId:%d, failed to check member", TD_VID(pVnode));
×
2410
  }
2411

2412
  pRsp->msgType = TDMT_SYNC_CONFIG_CHANGE_RSP;
×
2413
  pRsp->code = TSDB_CODE_SUCCESS;
×
2414
  pRsp->pCont = NULL;
×
2415
  pRsp->contLen = 0;
×
2416

2417
  return 0;
×
2418
}
2419

2420
static int32_t vnodePreCheckAssignedLogSyncd(SVnode *pVnode, char *member0Token, char *member1Token) {
×
2421
  SSyncState syncState = syncGetState(pVnode->sync);
×
2422
  if (syncState.state != TAOS_SYNC_STATE_LEADER) {
×
2423
    return terrno = TSDB_CODE_SYN_NOT_LEADER;
×
2424
  }
2425

2426
  char token[TSDB_ARB_TOKEN_SIZE] = {0};
×
2427
  if (vnodeGetArbToken(pVnode, token) != 0) {
×
2428
    return terrno = TSDB_CODE_NOT_FOUND;
×
2429
  }
2430

2431
  if (strncmp(token, member0Token, TSDB_ARB_TOKEN_SIZE) != 0 &&
×
2432
      strncmp(token, member1Token, TSDB_ARB_TOKEN_SIZE) != 0) {
×
2433
    return terrno = TSDB_CODE_MND_ARB_TOKEN_MISMATCH;
×
2434
  }
2435

2436
  terrno = TSDB_CODE_SUCCESS;
×
2437
  return 0;
×
2438
}
2439

2440
static int32_t vnodeCheckAssignedLogSyncd(SVnode *pVnode, char *member0Token, char *member1Token) {
×
2441
  int32_t code = vnodePreCheckAssignedLogSyncd(pVnode, member0Token, member1Token);
×
2442
  if (code != 0) {
×
2443
    return code;
×
2444
  }
2445

2446
  return syncGetAssignedLogSynced(pVnode->sync);
×
2447
}
2448

2449
static int32_t vnodeProcessArbCheckSyncReq(SVnode *pVnode, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
2450
  int32_t code = 0;
×
2451

2452
  SVArbCheckSyncReq syncReq = {0};
×
2453

2454
  code = tDeserializeSVArbCheckSyncReq(pReq, len, &syncReq);
×
2455
  if (code) {
×
2456
    return terrno = code;
×
2457
  }
2458

2459
  pRsp->msgType = TDMT_VND_ARB_CHECK_SYNC_RSP;
×
2460
  pRsp->code = TSDB_CODE_SUCCESS;
×
2461
  pRsp->pCont = NULL;
×
2462
  pRsp->contLen = 0;
×
2463

2464
  SVArbCheckSyncRsp syncRsp = {0};
×
2465
  syncRsp.arbToken = syncReq.arbToken;
×
2466
  syncRsp.member0Token = syncReq.member0Token;
×
2467
  syncRsp.member1Token = syncReq.member1Token;
×
2468
  syncRsp.vgId = TD_VID(pVnode);
×
2469

2470
  if (vnodeCheckAssignedLogSyncd(pVnode, syncReq.member0Token, syncReq.member1Token) != 0) {
×
2471
    vError("vgId:%d, failed to check assigned log syncd", TD_VID(pVnode));
×
2472
  }
2473
  syncRsp.errCode = terrno;
×
2474

2475
  if (vnodeUpdateArbTerm(pVnode, syncReq.arbTerm) != 0) {
×
2476
    vError("vgId:%d, failed to update arb term", TD_VID(pVnode));
×
2477
    code = -1;
×
2478
    goto _OVER;
×
2479
  }
2480

2481
  int32_t contLen = tSerializeSVArbCheckSyncRsp(NULL, 0, &syncRsp);
×
2482
  if (contLen <= 0) {
×
2483
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2484
    code = -1;
×
2485
    goto _OVER;
×
2486
  }
2487
  void *pHead = rpcMallocCont(contLen);
×
2488
  if (!pHead) {
×
2489
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2490
    code = -1;
×
2491
    goto _OVER;
×
2492
  }
2493

2494
  if (tSerializeSVArbCheckSyncRsp(pHead, contLen, &syncRsp) <= 0) {
×
2495
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2496
    rpcFreeCont(pHead);
×
2497
    code = -1;
×
2498
    goto _OVER;
×
2499
  }
2500

2501
  pRsp->pCont = pHead;
×
2502
  pRsp->contLen = contLen;
×
2503

2504
  terrno = TSDB_CODE_SUCCESS;
×
2505

2506
_OVER:
×
2507
  tFreeSVArbCheckSyncReq(&syncReq);
×
2508
  return code;
×
2509
}
2510

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