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

taosdata / TDengine / #4786

09 Oct 2025 07:30AM UTC coverage: 58.476% (+0.01%) from 58.462%
#4786

push

travis-ci

web-flow
Merge pull request #33183 from taosdata/fix/sort-release-note

fix: replace DocCardList with SortedDocCardList in release notes

138909 of 302745 branches covered (45.88%)

Branch coverage included in aggregate %.

210277 of 294403 relevant lines covered (71.42%)

23908667.53 hits per line

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

53.89
/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 <stdint.h>
17
#include "audit.h"
18
#include "cos.h"
19
#include "libs/new-stream/stream.h"
20
#include "monitor.h"
21
#include "taoserror.h"
22
#include "tencode.h"
23
#include "tglobal.h"
24
#include "tmsg.h"
25
#include "tmsgcb.h"
26
#include "tstrbuild.h"
27
#include "tutil.h"
28
#include "vnd.h"
29
#include "vnode.h"
30
#include "vnodeInt.h"
31

32
extern taos_counter_t *tsInsertCounter;
33

34
extern int32_t vnodeProcessScanVnodeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
35
extern int32_t vnodeQueryScanProgress(SVnode *pVnode, SRpcMsg *pMsg);
36

37
static int32_t vnodeProcessCreateStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
38
static int32_t vnodeProcessAlterStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
39
static int32_t vnodeProcessDropStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp, SRpcMsg *pOriginRpc);
40
static int32_t vnodeProcessCreateTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
41
                                       SRpcMsg *pOriginRpc);
42
static int32_t vnodeProcessAlterTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
43
static int32_t vnodeProcessDropTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
44
                                     SRpcMsg *pOriginRpc);
45
static int32_t vnodeProcessSubmitReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
46
                                     SRpcMsg *pOriginalMsg);
47
static int32_t vnodeProcessAlterConfirmReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
48
static int32_t vnodeProcessAlterConfigReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
49
static int32_t vnodeProcessDropTtlTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
50
static int32_t vnodeProcessTrimReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
51
static int32_t vnodeProcessDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
52
                                     SRpcMsg *pOriginalMsg);
53
static int32_t vnodeProcessBatchDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
54
static int32_t vnodeProcessCreateIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
55
static int32_t vnodeProcessDropIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
56
static int32_t vnodeProcessCompactVnodeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
57
static int32_t vnodeProcessConfigChangeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
58
static int32_t vnodeProcessArbCheckSyncReq(SVnode *pVnode, void *pReq, int32_t len, SRpcMsg *pRsp);
59
static int32_t vnodeProcessDropTSmaCtbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
60
                                          SRpcMsg *pOriginRpc);
61
static int32_t vnodeProcessCreateRsmaReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
62
static int32_t vnodeProcessDropRsmaReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
63

64
static int32_t vnodeCheckState(SVnode *pVnode);
65
static int32_t vnodeCheckToken(SVnode *pVnode, char *member0Token, char *member1Token);
66
static int32_t vnodeCheckSyncd(SVnode *pVnode, char *member0Token, char *member1Token);
67
static int32_t vnodeProcessFetchTtlExpiredTbs(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
68

69
extern int32_t vnodeProcessKillCompactReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
70
extern int32_t vnodeQueryCompactProgress(SVnode *pVnode, SRpcMsg *pMsg);
71

72
extern int32_t vnodeProcessKillRetentionReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
73
extern int32_t vnodeQueryRetentionProgress(SVnode *pVnode, SRpcMsg *pMsg);
74

75
extern int32_t vnodeProcessKillScanReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
76

77
extern int32_t vnodeListSsMigrateFileSets(SVnode *pVnode, SRpcMsg *pMsg);
78
static int32_t vnodeProcessSsMigrateFileSetReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
79
extern int32_t vnodeQuerySsMigrateProgress(SVnode *pVnode, SRpcMsg *pMsg);
80
static int32_t vnodeProcessFollowerSsMigrateReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
81
static int32_t vnodeProcessKillSsMigrateReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
82

83
static int32_t vnodePreprocessCreateTableReq(SVnode *pVnode, SDecoder *pCoder, int64_t btime, int64_t *pUid) {
176,550✔
84
  int32_t code = 0;
176,550✔
85
  int32_t lino = 0;
176,550✔
86

87
  if (tStartDecode(pCoder) < 0) {
176,550!
88
    code = TSDB_CODE_INVALID_MSG;
×
89
    TSDB_CHECK_CODE(code, lino, _exit);
×
90
  }
91

92
  // flags
93
  if (tDecodeI32v(pCoder, NULL) < 0) {
176,556!
94
    code = TSDB_CODE_INVALID_MSG;
×
95
    TSDB_CHECK_CODE(code, lino, _exit);
×
96
  }
97

98
  // name
99
  char *name = NULL;
176,556✔
100
  if (tDecodeCStr(pCoder, &name) < 0) {
176,399!
101
    code = TSDB_CODE_INVALID_MSG;
×
102
    TSDB_CHECK_CODE(code, lino, _exit);
×
103
  }
104

105
  // uid
106
  int64_t uid = metaGetTableEntryUidByName(pVnode->pMeta, name);
176,399✔
107
  if (uid == 0) {
176,722✔
108
    uid = tGenIdPI64();
161,383✔
109
  }
110
  taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), uid);
176,570✔
111

112
  // btime
113
  taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos + 8), btime);
176,570✔
114

115
  tEndDecode(pCoder);
176,570✔
116

117
_exit:
176,553✔
118
  if (code) {
176,553!
119
    vError("vgId:%d %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
120
  } else {
121
    vTrace("vgId:%d %s done, table:%s uid generated:%" PRId64, TD_VID(pVnode), __func__, name, uid);
176,553✔
122
    if (pUid) *pUid = uid;
176,553✔
123
  }
124
  return code;
176,550✔
125
}
126
static int32_t vnodePreProcessCreateTableMsg(SVnode *pVnode, SRpcMsg *pMsg) {
75,791✔
127
  int32_t code = 0;
75,791✔
128
  int32_t lino = 0;
75,791✔
129

130
  int64_t  btime = taosGetTimestampMs();
75,792✔
131
  SDecoder dc = {0};
75,792✔
132
  int32_t  nReqs;
133

134
  tDecoderInit(&dc, (uint8_t *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead));
75,792✔
135
  if (tStartDecode(&dc) < 0) {
75,786!
136
    code = TSDB_CODE_INVALID_MSG;
×
137
    return code;
×
138
  }
139

140
  if (tDecodeI32v(&dc, &nReqs) < 0) {
75,792!
141
    code = TSDB_CODE_INVALID_MSG;
×
142
    TSDB_CHECK_CODE(code, lino, _exit);
×
143
  }
144
  for (int32_t iReq = 0; iReq < nReqs; iReq++) {
191,301✔
145
    code = vnodePreprocessCreateTableReq(pVnode, &dc, btime, NULL);
115,521✔
146
    TSDB_CHECK_CODE(code, lino, _exit);
115,509!
147
  }
148

149
  tEndDecode(&dc);
75,780✔
150

151
_exit:
75,779✔
152
  tDecoderClear(&dc);
75,779✔
153
  if (code) {
75,789!
154
    vError("vgId:%d, %s:%d failed to preprocess submit request since %s, msg type:%s", TD_VID(pVnode), __func__, lino,
×
155
           tstrerror(code), TMSG_INFO(pMsg->msgType));
156
  }
157
  return code;
75,784✔
158
}
159

160
static int32_t vnodePreProcessAlterTableMsg(SVnode *pVnode, SRpcMsg *pMsg) {
19,778✔
161
  int32_t code = TSDB_CODE_INVALID_MSG;
19,778✔
162
  int32_t lino = 0;
19,778✔
163

164
  SDecoder dc = {0};
19,778✔
165
  tDecoderInit(&dc, (uint8_t *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead));
19,778✔
166

167
  SVAlterTbReq vAlterTbReq = {0};
19,778✔
168
  int64_t      ctimeMs = taosGetTimestampMs();
19,778✔
169
  if (tDecodeSVAlterTbReqSetCtime(&dc, &vAlterTbReq, ctimeMs) < 0) {
19,778!
170
    taosArrayDestroy(vAlterTbReq.pMultiTag);
×
171
    vAlterTbReq.pMultiTag = NULL;
×
172
    goto _exit;
×
173
  }
174
  taosArrayDestroy(vAlterTbReq.pMultiTag);
19,778✔
175
  vAlterTbReq.pMultiTag = NULL;
19,778✔
176

177
  code = 0;
19,778✔
178

179
_exit:
19,778✔
180
  tDecoderClear(&dc);
19,778✔
181
  if (code) {
19,778!
182
    vError("vgId:%d %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
183
  } else {
184
    vTrace("vgId:%d %s done, table:%s ctimeMs generated:%" PRId64, TD_VID(pVnode), __func__, vAlterTbReq.tbName,
19,778!
185
           ctimeMs);
186
  }
187
  return code;
19,778✔
188
}
189

190
static int32_t vnodePreProcessDropTtlMsg(SVnode *pVnode, SRpcMsg *pMsg) {
102,113✔
191
  int32_t code = TSDB_CODE_INVALID_MSG;
102,113✔
192
  int32_t lino = 0;
102,113✔
193

194
  SMsgHead *pContOld = pMsg->pCont;
102,113✔
195
  int32_t   reqLenOld = pMsg->contLen - sizeof(SMsgHead);
102,113✔
196

197
  SArray *tbUids = NULL;
102,113✔
198
  int64_t timestampMs = 0;
102,113✔
199

200
  SVDropTtlTableReq ttlReq = {0};
102,113✔
201
  if (tDeserializeSVDropTtlTableReq((char *)pContOld + sizeof(SMsgHead), reqLenOld, &ttlReq) != 0) {
102,113!
202
    code = TSDB_CODE_INVALID_MSG;
×
203
    TSDB_CHECK_CODE(code, lino, _exit);
×
204
  }
205

206
  {  // find expired uids
207
    tbUids = taosArrayInit(8, sizeof(tb_uid_t));
102,290✔
208
    if (tbUids == NULL) {
102,395!
209
      code = terrno;
×
210
      TSDB_CHECK_CODE(code, lino, _exit);
×
211
    }
212

213
    timestampMs = (int64_t)ttlReq.timestampSec * 1000;
102,395✔
214
    code = metaTtlFindExpired(pVnode->pMeta, timestampMs, tbUids, ttlReq.ttlDropMaxCount);
102,395✔
215
    if (code != 0) {
102,117!
216
      code = TSDB_CODE_INVALID_MSG;
×
217
      TSDB_CHECK_CODE(code, lino, _exit);
×
218
    }
219

220
    ttlReq.nUids = taosArrayGetSize(tbUids);
102,117✔
221
    ttlReq.pTbUids = tbUids;
101,659✔
222
  }
223

224
  if (ttlReq.nUids == 0) {
101,659!
225
    code = TSDB_CODE_MSG_PREPROCESSED;
101,745✔
226
    TSDB_CHECK_CODE(code, lino, _exit);
101,745!
227
  }
228

229
  {  // prepare new content
230
    int32_t reqLenNew = tSerializeSVDropTtlTableReq(NULL, 0, &ttlReq);
×
231
    int32_t contLenNew = reqLenNew + sizeof(SMsgHead);
16✔
232

233
    SMsgHead *pContNew = rpcMallocCont(contLenNew);
16✔
234
    if (pContNew == NULL) {
16!
235
      code = terrno;
×
236
      TSDB_CHECK_CODE(code, lino, _exit);
×
237
    }
238

239
    if (tSerializeSVDropTtlTableReq((char *)pContNew + sizeof(SMsgHead), reqLenNew, &ttlReq) != 0) {
16!
240
      vError("vgId:%d %s:%d failed to serialize drop ttl request", TD_VID(pVnode), __func__, lino);
16!
241
    }
242
    pContNew->contLen = htonl(reqLenNew);
16✔
243
    pContNew->vgId = pContOld->vgId;
16✔
244

245
    rpcFreeCont(pContOld);
16✔
246
    pMsg->pCont = pContNew;
16✔
247
    pMsg->contLen = contLenNew;
16✔
248
  }
249

250
  code = 0;
16✔
251

252
_exit:
101,814✔
253
  taosArrayDestroy(tbUids);
101,814✔
254

255
  if (code && code != TSDB_CODE_MSG_PREPROCESSED) {
102,042!
256
    vError("vgId:%d, %s:%d failed to preprocess drop ttl request since %s, msg type:%s", TD_VID(pVnode), __func__, lino,
×
257
           tstrerror(code), TMSG_INFO(pMsg->msgType));
258
  } else {
259
    vTrace("vgId:%d, %s done, timestampSec:%d, nUids:%d", TD_VID(pVnode), __func__, ttlReq.timestampSec, ttlReq.nUids);
102,042✔
260
  }
261

262
  return code;
102,104✔
263
}
264

265
extern int64_t tsMaxKeyByPrecision[];
266
static int32_t vnodePreProcessSubmitTbData(SVnode *pVnode, SDecoder *pCoder, int64_t btimeMs, int64_t ctimeMs) {
17,913,664✔
267
  int32_t code = 0;
17,913,664✔
268
  int32_t lino = 0;
17,913,664✔
269

270
  if (tStartDecode(pCoder) < 0) {
17,913,664!
271
    code = TSDB_CODE_INVALID_MSG;
×
272
    TSDB_CHECK_CODE(code, lino, _exit);
×
273
  }
274

275
  SSubmitTbData submitTbData;
276
  uint8_t       version;
277
  if (tDecodeI32v(pCoder, &submitTbData.flags) < 0) {
17,914,607!
278
    code = TSDB_CODE_INVALID_MSG;
×
279
    TSDB_CHECK_CODE(code, lino, _exit);
×
280
  }
281
  version = (submitTbData.flags >> 8) & 0xff;
17,914,607✔
282
  submitTbData.flags = submitTbData.flags & 0xff;
17,914,607✔
283

284
  int64_t uid;
285
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
17,914,607✔
286
    code = vnodePreprocessCreateTableReq(pVnode, pCoder, btimeMs, &uid);
61,033✔
287
    TSDB_CHECK_CODE(code, lino, _exit);
61,039!
288
  }
289

290
  // submit data
291
  if (tDecodeI64(pCoder, &submitTbData.suid) < 0) {
17,914,567!
292
    code = TSDB_CODE_INVALID_MSG;
×
293
    TSDB_CHECK_CODE(code, lino, _exit);
×
294
  }
295

296
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
17,914,567✔
297
    taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), uid);
61,034✔
298
    pCoder->pos += sizeof(int64_t);
61,034✔
299
  } else {
300
    if (tDecodeI64(pCoder, &submitTbData.uid) < 0) {
17,853,489!
301
      code = TSDB_CODE_INVALID_MSG;
×
302
      TSDB_CHECK_CODE(code, lino, _exit);
×
303
    }
304
  }
305

306
  if (tDecodeI32v(pCoder, &submitTbData.sver) < 0) {
17,914,387!
307
    code = TSDB_CODE_INVALID_MSG;
×
308
    TSDB_CHECK_CODE(code, lino, _exit);
×
309
  }
310

311
  // scan and check
312
  TSKEY now = btimeMs;
17,914,387✔
313
  if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_MICRO) {
17,914,387✔
314
    now *= 1000;
18,511✔
315
  } else if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_NANO) {
17,895,876✔
316
    now *= 1000000;
2,176✔
317
  }
318

319
  int32_t keep = pVnode->config.tsdbCfg.keep2;
17,914,387✔
320

321
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * keep;
17,914,387✔
322
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
17,914,387✔
323
  if (submitTbData.flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
17,914,387✔
324
    uint64_t nColData;
325
    if (tDecodeU64v(pCoder, &nColData) < 0) {
1,159!
326
      code = TSDB_CODE_INVALID_MSG;
×
327
      TSDB_CHECK_CODE(code, lino, _exit);
1!
328
    }
329

330
    SColData colData = {0};
1,159✔
331
    code = tDecodeColData(version, pCoder, &colData, false);
1,159✔
332
    if (code) {
1,158!
333
      code = TSDB_CODE_INVALID_MSG;
×
334
      TSDB_CHECK_CODE(code, lino, _exit);
×
335
    }
336

337
    if (colData.flag != HAS_VALUE) {
1,158!
338
      code = TSDB_CODE_INVALID_MSG;
×
339
      TSDB_CHECK_CODE(code, lino, _exit);
×
340
    }
341

342
    for (int32_t iRow = 0; iRow < colData.nVal; iRow++) {
74,516✔
343
      if (((TSKEY *)colData.pData)[iRow] < minKey || ((TSKEY *)colData.pData)[iRow] > maxKey) {
73,359!
344
        code = TSDB_CODE_TDB_TIMESTAMP_OUT_OF_RANGE;
1✔
345
        TSDB_CHECK_CODE(code, lino, _exit);
1!
346
      }
347
    }
348

349
    for (uint64_t i = 1; i < nColData; i++) {
7,290✔
350
      code = tDecodeColData(version, pCoder, &colData, true);
6,133✔
351
      if (code) {
6,133!
352
        code = TSDB_CODE_INVALID_MSG;
×
353
        TSDB_CHECK_CODE(code, lino, _exit);
×
354
      }
355
    }
356
  } else {
357
    uint64_t nRow;
358
    if (tDecodeU64v(pCoder, &nRow) < 0) {
17,913,077!
359
      code = TSDB_CODE_INVALID_MSG;
×
360
      TSDB_CHECK_CODE(code, lino, _exit);
893!
361
    }
362

363
    for (int32_t iRow = 0; iRow < nRow; ++iRow) {
1,429,932,052✔
364
      SRow *pRow = (SRow *)(pCoder->data + pCoder->pos);
1,412,019,868✔
365
      pCoder->pos += pRow->len;
1,412,019,868✔
366
#ifndef NO_UNALIGNED_ACCESS
367
      if (pRow->ts < minKey || pRow->ts > maxKey) {
1,412,019,868!
368
#else
369
      TSKEY ts = taosGetInt64Aligned(&pRow->ts);
370
      if (ts < minKey || ts > maxKey) {
371
#endif
372
        code = TSDB_CODE_TDB_TIMESTAMP_OUT_OF_RANGE;
×
373
        TSDB_CHECK_CODE(code, lino, _exit);
×
374
      }
375

376
      // Check pRow->sver
377
      if (pRow->sver != submitTbData.sver) {
1,412,018,975!
378
        code = TSDB_CODE_INVALID_DATA_FMT;
×
379
        TSDB_CHECK_CODE(code, lino, _exit);
×
380
      }
381
    }
382
  }
383

384
  if (!tDecodeIsEnd(pCoder)) {
17,913,341!
385
    taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), ctimeMs);
17,913,451✔
386
    pCoder->pos += sizeof(int64_t);
17,913,451✔
387
  }
388

389
  tEndDecode(pCoder);
17,913,341✔
390

391
_exit:
17,913,636✔
392
  if (code) {
17,913,636✔
393
    vError("vgId:%d, %s:%d failed to vnodePreProcessSubmitTbData submit request since %s", TD_VID(pVnode), __func__,
894!
394
           lino, tstrerror(code));
395
  }
396
  return code;
17,913,646✔
397
}
398
static int32_t vnodePreProcessSubmitMsg(SVnode *pVnode, SRpcMsg *pMsg) {
17,844,093✔
399
  int32_t code = 0;
17,844,093✔
400
  int32_t lino = 0;
17,844,093✔
401

402
  if (tsBypassFlag & TSDB_BYPASS_RA_RPC_RECV_SUBMIT) {
17,844,093✔
403
    return TSDB_CODE_MSG_PREPROCESSED;
1✔
404
  }
405

406
  SDecoder *pCoder = &(SDecoder){0};
17,844,092✔
407

408
  if (taosHton64(((SSubmitReq2Msg *)pMsg->pCont)->version) != 1) {
17,844,092!
409
    code = TSDB_CODE_INVALID_MSG;
×
410
    TSDB_CHECK_CODE(code, lino, _exit);
×
411
  }
412

413
  tDecoderInit(pCoder, (uint8_t *)pMsg->pCont + sizeof(SSubmitReq2Msg), pMsg->contLen - sizeof(SSubmitReq2Msg));
17,844,116✔
414

415
  if (tStartDecode(pCoder) < 0) {
17,844,052!
416
    code = TSDB_CODE_INVALID_MSG;
×
417
    TSDB_CHECK_CODE(code, lino, _exit);
×
418
  }
419

420
  uint64_t nSubmitTbData;
421
  if (tDecodeU64v(pCoder, &nSubmitTbData) < 0) {
17,844,230!
422
    code = TSDB_CODE_INVALID_MSG;
×
423
    TSDB_CHECK_CODE(code, lino, _exit);
×
424
  }
425

426
  int64_t btimeMs = taosGetTimestampMs();
17,844,162✔
427
  int64_t ctimeMs = btimeMs;
17,844,162✔
428
  for (int32_t i = 0; i < nSubmitTbData; i++) {
35,756,936✔
429
    code = vnodePreProcessSubmitTbData(pVnode, pCoder, btimeMs, ctimeMs);
17,913,758✔
430
    TSDB_CHECK_CODE(code, lino, _exit);
17,913,668✔
431
  }
432

433
  tEndDecode(pCoder);
17,843,178✔
434

435
_exit:
17,844,160✔
436
  tDecoderClear(pCoder);
17,844,160✔
437
  if (code) {
17,844,110✔
438
    vError("vgId:%d, %s:%d failed to preprocess submit request since %s, msg type:%s", TD_VID(pVnode), __func__, lino,
894!
439
           tstrerror(code), TMSG_INFO(pMsg->msgType));
440
  }
441
  return code;
17,844,064✔
442
}
443

444
static int32_t vnodePreProcessDeleteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
116,700✔
445
  int32_t code = 0;
116,700✔
446

447
  int32_t    size;
448
  int32_t    ret;
449
  uint8_t   *pCont;
450
  SEncoder  *pCoder = &(SEncoder){0};
116,700✔
451
  SDeleteRes res = {0};
116,700✔
452

453
  SReadHandle handle = {0};
116,700✔
454
  handle.vnode = pVnode;
116,700✔
455
  handle.pMsgCb = &pVnode->msgCb;
116,700✔
456
  handle.skipRollup = 1;
116,700✔
457
  initStorageAPI(&handle.api);
116,700✔
458

459
  code = qWorkerProcessDeleteMsg(&handle, pVnode->pQuery, pMsg, &res);
116,705✔
460
  if (code) goto _exit;
116,703!
461

462
  res.ctimeMs = taosGetTimestampMs();
116,702✔
463
  // malloc and encode
464
  tEncodeSize(tEncodeDeleteRes, &res, size, ret);
116,702!
465
  pCont = rpcMallocCont(size + sizeof(SMsgHead));
116,701✔
466

467
  ((SMsgHead *)pCont)->contLen = size + sizeof(SMsgHead);
116,707✔
468
  ((SMsgHead *)pCont)->vgId = TD_VID(pVnode);
116,707✔
469

470
  tEncoderInit(pCoder, pCont + sizeof(SMsgHead), size);
116,707✔
471
  if (tEncodeDeleteRes(pCoder, &res) != 0) {
116,707!
472
    vError("vgId:%d %s failed to encode delete response", TD_VID(pVnode), __func__);
×
473
  }
474
  tEncoderClear(pCoder);
116,705✔
475

476
  rpcFreeCont(pMsg->pCont);
116,705✔
477
  pMsg->pCont = pCont;
116,706✔
478
  pMsg->contLen = size + sizeof(SMsgHead);
116,706✔
479

480
  taosArrayDestroy(res.uidList);
116,706✔
481

482
_exit:
116,707✔
483
  return code;
116,707✔
484
}
485

486
static int32_t vnodePreProcessBatchDeleteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
×
487
  int32_t code = 0;
×
488
  int32_t lino = 0;
×
489

490
  int64_t         ctimeMs = taosGetTimestampMs();
×
491
  SBatchDeleteReq pReq = {0};
×
492
  SDecoder       *pCoder = &(SDecoder){0};
×
493

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

496
  if (tDecodeSBatchDeleteReqSetCtime(pCoder, &pReq, ctimeMs) < 0) {
×
497
    code = TSDB_CODE_INVALID_MSG;
×
498
  }
499

500
  tDecoderClear(pCoder);
×
501
  taosArrayDestroy(pReq.deleteReqs);
×
502

503
  if (code) {
×
504
    vError("vgId:%d %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
505
  } else {
506
    vTrace("vgId:%d %s done, ctimeMs generated:%" PRId64, TD_VID(pVnode), __func__, ctimeMs);
×
507
  }
508
  return code;
×
509
}
510

511
static int32_t vnodePreProcessArbCheckSyncMsg(SVnode *pVnode, SRpcMsg *pMsg) {
87✔
512
  int32_t ret = 0;
87✔
513
  if ((ret = vnodeCheckState(pVnode)) != 0) {
87✔
514
    vDebug("vgId:%d, failed to preprocess vnode-arb-check-sync request since %s", TD_VID(pVnode), tstrerror(ret));
33!
515
    return 0;
33✔
516
  }
517

518
  SVArbCheckSyncReq syncReq = {0};
54✔
519

520
  if (tDeserializeSVArbCheckSyncReq((char *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead),
54!
521
                                    &syncReq) != 0) {
522
    return TSDB_CODE_INVALID_MSG;
×
523
  }
524

525
  ret = vnodeCheckToken(pVnode, syncReq.member0Token, syncReq.member1Token);
54✔
526
  if (ret != 0) {
54✔
527
    vError("vgId:%d, failed to preprocess vnode-arb-check-sync request since %s", TD_VID(pVnode), tstrerror(ret));
46!
528
  }
529

530
  int32_t code = terrno;
54✔
531
  tFreeSVArbCheckSyncReq(&syncReq);
54✔
532

533
  return code;
54✔
534
}
535

536
int32_t vnodePreProcessDropTbMsg(SVnode *pVnode, SRpcMsg *pMsg) {
15,486✔
537
  int32_t          code = TSDB_CODE_SUCCESS;
15,486✔
538
  int32_t          lino = 0;
15,486✔
539
  int32_t          size = 0;
15,486✔
540
  SDecoder         dc = {0};
15,486✔
541
  SEncoder         ec = {0};
15,486✔
542
  SVDropTbBatchReq receivedBatchReqs = {0};
15,486✔
543
  SVDropTbBatchReq sentBatchReqs = {0};
15,486✔
544

545
  tDecoderInit(&dc, POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), pMsg->contLen - sizeof(SMsgHead));
15,486✔
546

547
  code = tDecodeSVDropTbBatchReq(&dc, &receivedBatchReqs);
15,486✔
548
  if (code < 0) {
15,486!
549
    terrno = code;
×
550
    TSDB_CHECK_CODE(code, lino, _exit);
×
551
  }
552
  sentBatchReqs.pArray = taosArrayInit(receivedBatchReqs.nReqs, sizeof(SVDropTbReq));
15,486✔
553
  if (!sentBatchReqs.pArray) {
15,486!
554
    code = terrno;
×
555
    goto _exit;
×
556
  }
557

558
  for (int32_t i = 0; i < receivedBatchReqs.nReqs; ++i) {
31,375✔
559
    SVDropTbReq *pReq = receivedBatchReqs.pReqs + i;
15,889✔
560
    tb_uid_t     uid = metaGetTableEntryUidByName(pVnode->pMeta, pReq->name);
15,889✔
561
    if (uid == 0) {
15,889!
562
      vWarn("vgId:%d, preprocess drop ctb: %s not found", TD_VID(pVnode), pReq->name);
×
563
      continue;
×
564
    }
565
    pReq->uid = uid;
15,889✔
566
    vDebug("vgId:%d %s for: %s, uid: %" PRId64, TD_VID(pVnode), __func__, pReq->name, pReq->uid);
15,889✔
567
    if (taosArrayPush(sentBatchReqs.pArray, pReq) == NULL) {
31,778!
568
      code = terrno;
×
569
      goto _exit;
×
570
    }
571
  }
572
  sentBatchReqs.nReqs = sentBatchReqs.pArray->size;
15,486✔
573

574
  tEncodeSize(tEncodeSVDropTbBatchReq, &sentBatchReqs, size, code);
15,486!
575
  tEncoderInit(&ec, POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), size);
15,486✔
576
  code = tEncodeSVDropTbBatchReq(&ec, &sentBatchReqs);
15,486✔
577
  tEncoderClear(&ec);
15,486✔
578
  if (code != TSDB_CODE_SUCCESS) {
15,486!
579
    vError("vgId:%d %s failed to encode drop tb batch req: %s", TD_VID(pVnode), __func__, tstrerror(code));
×
580
    TSDB_CHECK_CODE(code, lino, _exit);
×
581
  }
582

583
_exit:
15,486✔
584
  tDecoderClear(&dc);
15,486✔
585
  if (sentBatchReqs.pArray) {
15,486!
586
    taosArrayDestroy(sentBatchReqs.pArray);
15,486✔
587
  }
588
  return code;
15,486✔
589
}
590

591
int32_t vnodePreProcessSsMigrateFileSetReq(SVnode *pVnode, SRpcMsg *pMsg) {
×
592
  int32_t              code = TSDB_CODE_SUCCESS, lino = 0;
×
593
  SSsMigrateFileSetReq req = {0};
×
594

595
  code = tDeserializeSSsMigrateFileSetReq(POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)),
×
596
                                          pMsg->contLen - sizeof(SMsgHead), &req);
×
597
  if (code < 0) {
×
598
    terrno = code;
×
599
    TSDB_CHECK_CODE(code, lino, _exit);
×
600
  }
601

602
  req.nodeId = vnodeNodeId(pVnode);
×
603
  TAOS_UNUSED(tSerializeSSsMigrateFileSetReq(POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)),
×
604
                                             pMsg->contLen - sizeof(SMsgHead), &req));
605

606
_exit:
×
607
  return code;
×
608
}
609

610
int32_t vnodePreProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
18,250,813✔
611
  int32_t code = 0;
18,250,813✔
612

613
  if (pVnode->mounted) {
18,250,813✔
614
    vError("vgId:%d, mountVgId:%d, skip write msg:%s", TD_VID(pVnode), pVnode->config.mountVgId,
10!
615
           TMSG_INFO(pMsg->msgType));
616
    return TSDB_CODE_OPS_NOT_SUPPORT;
10✔
617
  }
618

619
  switch (pMsg->msgType) {
18,250,803!
620
    case TDMT_VND_CREATE_TABLE: {
75,793✔
621
      code = vnodePreProcessCreateTableMsg(pVnode, pMsg);
75,793✔
622
    } break;
75,784✔
623
    case TDMT_VND_ALTER_TABLE: {
19,778✔
624
      code = vnodePreProcessAlterTableMsg(pVnode, pMsg);
19,778✔
625
    } break;
19,778✔
626
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
102,322✔
627
    case TDMT_VND_DROP_TTL_TABLE: {
628
      code = vnodePreProcessDropTtlMsg(pVnode, pMsg);
102,322✔
629
    } break;
101,974✔
630
    case TDMT_VND_SUBMIT: {
17,844,178✔
631
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.preprocess_time, METRIC_LEVEL_LOW,
17,844,178!
632
                           { code = vnodePreProcessSubmitMsg(pVnode, pMsg); });
633
    } break;
17,844,059✔
634
    case TDMT_VND_DELETE: {
116,700✔
635
      code = vnodePreProcessDeleteMsg(pVnode, pMsg);
116,700✔
636
    } break;
116,705✔
637
    case TDMT_VND_BATCH_DEL: {
×
638
      code = vnodePreProcessBatchDeleteMsg(pVnode, pMsg);
×
639
    } break;
×
640
    case TDMT_VND_ARB_CHECK_SYNC: {
87✔
641
      code = vnodePreProcessArbCheckSyncMsg(pVnode, pMsg);
87✔
642
    } break;
87✔
643
    case TDMT_VND_DROP_TABLE: {
15,486✔
644
      code = vnodePreProcessDropTbMsg(pVnode, pMsg);
15,486✔
645
    } break;
15,486✔
646
#ifdef TD_ENTERPRISE
647
    case TDMT_VND_SSMIGRATE_FILESET: {
×
648
      code = vnodePreProcessSsMigrateFileSetReq(pVnode, pMsg);
×
649
    } break;
×
650
#endif
651
    default:
76,459✔
652
      break;
76,459✔
653
  }
654

655
  if (code && code != TSDB_CODE_MSG_PREPROCESSED) {
18,250,332✔
656
    vError("vgId:%d, failed to preprocess write request since %s, msg type:%s", TD_VID(pVnode), tstrerror(code),
940!
657
           TMSG_INFO(pMsg->msgType));
658
  }
659
  return code;
18,250,326✔
660
}
661

662
static int32_t inline vnodeSubmitSubRowBlobData(SVnode *pVnode, SSubmitTbData *pSubmitTbData) {
×
663
  int32_t code = 0;
×
664
  int32_t lino = 0;
×
665

666
  int64_t   st = taosGetTimestampUs();
×
667
  SBlobSet *pBlobSet = pSubmitTbData->pBlobSet;
×
668
  int32_t   sz = taosArrayGetSize(pBlobSet->pSeqTable);
×
669

670
  SBseBatch *pBatch = NULL;
×
671

672
  code = bseBatchInit(pVnode->pBse, &pBatch, sz);
×
673
  TSDB_CHECK_CODE(code, lino, _exit);
×
674

675
  SRow  **pRow = (SRow **)TARRAY_DATA(pSubmitTbData->aRowP);
×
676
  int32_t rowIdx = -1;
×
677
  for (int32_t i = 0; i < sz; i++) {
×
678
    int64_t     seq = 0;
×
679
    SBlobValue *p = taosArrayGet(pBlobSet->pSeqTable, i);
×
680
    if (p->type == TSDB_DATA_BLOB_EMPTY_VALUE || p->type == TSDB_DATA_BLOB_NULL_VALUE) {
×
681
      // skip empty or null blob
682
      continue;
×
683
    }
684

685
    code = bseBatchPut(pBatch, &seq, pBlobSet->data + p->offset, p->len);
×
686
    TSDB_CHECK_CODE(code, lino, _exit);
×
687

688
    SRow *row = taosArrayGetP(pSubmitTbData->aRowP, i);
×
689
    if (row == NULL) {
×
690
      int32_t tlen = taosArrayGetSize(pBlobSet->pSeqTable);
×
691
      uTrace("blob invalid row index:%d, sz:%d, pBlobSet size:%d", rowIdx, sz, tlen);
×
692
      break;
×
693
    }
694

695
    if (tPutU64(row->data + p->dataOffset, seq) < 0) {
×
696
      code = TSDB_CODE_INVALID_MSG;
×
697
      TSDB_CHECK_CODE(code, lino, _exit);
×
698
    }
699
  }
700

701
  code = bseCommitBatch(pVnode->pBse, pBatch);
×
702
  TSDB_CHECK_CODE(code, lino, _exit);
×
703

704
  int64_t cost = taosGetTimestampUs() - st;
×
705
  if (cost >= 500) {
×
706
    vDebug("vgId:%d, %s, cost:%" PRId64 "us, rows:%d, size:%" PRId64 "", TD_VID(pVnode), __func__, cost, sz,
×
707
           pBlobSet->len);
708
  }
709
_exit:
×
710
  if (code != 0) {
×
711
    vError("vgId:%d %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
712
  }
713
  return code;
×
714
}
715

716
static int32_t inline vnodeSubmitSubColBlobData(SVnode *pVnode, SSubmitTbData *pSubmitTbData) {
×
717
  int32_t code = 0;
×
718
  int32_t lino = 0;
×
719

720
  int32_t   blobColIdx = 0;
×
721
  SColData *pBlobCol = NULL;
×
722
  int64_t   st = taosGetTimestampUs();
×
723
  SBlobSet *pBlobSet = pSubmitTbData->pBlobSet;
×
724
  int32_t   sz = taosArrayGetSize(pBlobSet->pSeqTable);
×
725

726
  SBseBatch *pBatch = NULL;
×
727

728
  code = bseBatchInit(pVnode->pBse, &pBatch, sz);
×
729
  TSDB_CHECK_CODE(code, lino, _exit);
×
730

731
  SColData *p = (SColData *)TARRAY_DATA(pSubmitTbData->aCol);
×
732
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitTbData->aCol); i++) {
×
733
    if (IS_STR_DATA_BLOB(p[i].type)) {
×
734
      pBlobCol = &p[i];
×
735
      break;
×
736
    }
737
  }
738
  if (pBlobCol == NULL) {
×
739
    vError("vgId:%d %s failed to find blob column in submit data", TD_VID(pVnode), __func__);
×
740
    code = TSDB_CODE_INVALID_MSG;
×
741
    goto _exit;
×
742
  }
743

744
  int32_t offset = 0;
×
745
  // int32_t   rowIdx = -1;
746
  for (int32_t i = 0; i < sz; i++) {
×
747
    int64_t     seq = 0;
×
748
    SBlobValue *p = taosArrayGet(pBlobSet->pSeqTable, i);
×
749
    if (p->type == TSDB_DATA_BLOB_EMPTY_VALUE || p->type == TSDB_DATA_BLOB_NULL_VALUE) {
×
750
      // skip empty or null blob
751
      continue;
×
752
    }
753
    code = bseBatchPut(pBatch, &seq, pBlobSet->data + p->offset, p->len);
×
754
    TSDB_CHECK_CODE(code, lino, _exit);
×
755

756
    memcpy(pBlobCol->pData + offset, (void *)&seq, BSE_SEQUECE_SIZE);
×
757
    offset += BSE_SEQUECE_SIZE;
×
758
  }
759

760
  code = bseCommitBatch(pVnode->pBse, pBatch);
×
761
  TSDB_CHECK_CODE(code, lino, _exit);
×
762

763
  int64_t cost = taosGetTimestampUs() - st;
×
764
  if (cost >= 500) {
×
765
    vDebug("vgId:%d, %s, cost:%" PRId64 "us, rows:%d, size:%" PRId64 "", TD_VID(pVnode), __func__, cost, sz,
×
766
           pBlobSet->len);
767
  }
768
_exit:
×
769
  if (code != 0) {
×
770
    vError("vgId:%d %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
771
  }
772
  return code;
×
773
}
774
static int32_t inline vnodeSubmitBlobData(SVnode *pVnode, SSubmitTbData *pData) {
×
775
  int32_t code = 0;
×
776
  if (pData->flags & SUBMIT_REQ_WITH_BLOB) {
×
777
    if (pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
×
778
      code = vnodeSubmitSubColBlobData(pVnode, pData);
×
779
    } else {
780
      code = vnodeSubmitSubRowBlobData(pVnode, pData);
×
781
    }
782
  }
783

784
  return code;
×
785
}
786

787
int32_t vnodeProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg, int64_t ver, SRpcMsg *pRsp) {
24,484,134✔
788
  int32_t code = 0;
24,484,134✔
789
  int32_t lino = 0;
24,484,134✔
790
  void   *ptr = NULL;
24,484,134✔
791
  void   *pReq;
792
  int32_t len;
793

794
  (void)taosThreadMutexLock(&pVnode->mutex);
24,484,134✔
795
  if (pVnode->disableWrite) {
24,484,483!
796
    (void)taosThreadMutexUnlock(&pVnode->mutex);
×
797
    vError("vgId:%d write is disabled for snapshot, version:%" PRId64, TD_VID(pVnode), ver);
×
798
    return TSDB_CODE_VND_WRITE_DISABLED;
×
799
  }
800
  (void)taosThreadMutexUnlock(&pVnode->mutex);
24,484,483✔
801

802
  if (ver <= pVnode->state.applied) {
24,484,385!
803
    vError("vgId:%d, duplicate write request. ver: %" PRId64 ", applied: %" PRId64, TD_VID(pVnode), ver,
×
804
           pVnode->state.applied);
805
    return terrno = TSDB_CODE_VND_DUP_REQUEST;
×
806
  }
807

808
  vGDebug(&pMsg->info.traceId,
24,484,385!
809
          "vgId:%d, index:%" PRId64 ", process write request:%s, applied:%" PRId64 ", state.applyTerm:%" PRId64
810
          ", conn.applyTerm:%" PRId64 ", contLen:%d",
811
          TD_VID(pVnode), ver, TMSG_INFO(pMsg->msgType), pVnode->state.applied, pVnode->state.applyTerm,
812
          pMsg->info.conn.applyTerm, pMsg->contLen);
813

814
  if (!(pVnode->state.applyTerm <= pMsg->info.conn.applyTerm)) {
24,484,222!
815
    vError("vgId:%d, applyTerm mismatch, expected: %" PRId64 ", received: %" PRId64, TD_VID(pVnode),
×
816
           pVnode->state.applyTerm, pMsg->info.conn.applyTerm);
817
    return terrno = TSDB_CODE_INTERNAL_ERROR;
×
818
  }
819

820
  if (!(pVnode->state.applied + 1 == ver)) {
24,484,222!
821
    vError("vgId:%d, mountVgId:%d, ver mismatch, expected: %" PRId64 ", received: %" PRId64, TD_VID(pVnode),
×
822
           pVnode->config.mountVgId, pVnode->state.applied + 1, ver);
823
    return terrno = TSDB_CODE_INTERNAL_ERROR;
×
824
  }
825

826
  atomic_store_64(&pVnode->state.applied, ver);
24,484,222✔
827
  atomic_store_64(&pVnode->state.applyTerm, pMsg->info.conn.applyTerm);
24,484,454✔
828

829
  if (!syncUtilUserCommit(pMsg->msgType)) goto _exit;
24,484,463✔
830

831
  // skip header
832
  pReq = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
24,458,881✔
833
  len = pMsg->contLen - sizeof(SMsgHead);
24,458,881✔
834
  bool needCommit = false;
24,458,881✔
835

836
  switch (pMsg->msgType) {
24,458,881!
837
    /* META */
838
    case TDMT_VND_CREATE_STB:
26,406✔
839
      code = vnodeProcessCreateStbReq(pVnode, ver, pReq, len, pRsp);
26,406✔
840
      TSDB_CHECK_CODE(code, lino, _err);
26,416✔
841
      break;
26,412✔
842
    case TDMT_VND_ALTER_STB:
16,187✔
843
      code = vnodeProcessAlterStbReq(pVnode, ver, pReq, len, pRsp);
16,187✔
844
      TSDB_CHECK_CODE(code, lino, _err);
16,187!
845
      break;
16,187✔
846
    case TDMT_VND_DROP_STB:
3,946✔
847
      code = vnodeProcessDropStbReq(pVnode, ver, pReq, len, pRsp, pMsg);
3,946✔
848
      TSDB_CHECK_CODE(code, lino, _err);
3,946!
849
      break;
3,946✔
850
    case TDMT_VND_CREATE_TABLE:
84,495✔
851
      code = vnodeProcessCreateTbReq(pVnode, ver, pReq, len, pRsp, pMsg);
84,495✔
852
      TSDB_CHECK_CODE(code, lino, _err);
84,512!
853
      break;
84,512✔
854
    case TDMT_VND_ALTER_TABLE:
19,783✔
855
      code = vnodeProcessAlterTbReq(pVnode, ver, pReq, len, pRsp);
19,783✔
856
      TSDB_CHECK_CODE(code, lino, _err);
19,783!
857
      break;
19,783✔
858
    case TDMT_VND_DROP_TABLE:
15,488✔
859
    case TDMT_VND_SNODE_DROP_TABLE:
860
      code = vnodeProcessDropTbReq(pVnode, ver, pReq, len, pRsp, pMsg);
15,488✔
861
      TSDB_CHECK_CODE(code, lino, _err);
15,488!
862
      break;
15,488✔
863
    case TDMT_VND_CREATE_RSMA:
24✔
864
      code = vnodeProcessCreateRsmaReq(pVnode, ver, pReq, len, pRsp);
24✔
865
      TSDB_CHECK_CODE(code, lino, _err);
24!
866
      break;
24✔
867
    case TDMT_VND_DROP_RSMA:
6✔
868
      code = vnodeProcessDropRsmaReq(pVnode, ver, pReq, len, pRsp);
6✔
869
      TSDB_CHECK_CODE(code, lino, _err);
6!
870
      break;
6✔
871
    case TDMT_VND_DROP_TTL_TABLE:
×
872
      code = vnodeProcessDropTtlTbReq(pVnode, ver, pReq, len, pRsp);
×
873
      TSDB_CHECK_CODE(code, lino, _err);
×
874
      break;
×
875
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
16✔
876
      code = vnodeProcessFetchTtlExpiredTbs(pVnode, ver, pReq, len, pRsp);
16✔
877
      TSDB_CHECK_CODE(code, lino, _err);
16!
878
      break;
16✔
879
    case TDMT_VND_TRIM:
250✔
880
      code = vnodeProcessTrimReq(pVnode, ver, pReq, len, pRsp);
250✔
881
      TSDB_CHECK_CODE(code, lino, _err);
250!
882
      break;
250✔
883
#ifdef TD_ENTERPRISE
884
    case TDMT_VND_SSMIGRATE_FILESET:
×
885
      if (vnodeProcessSsMigrateFileSetReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
886
      break;
×
887
    case TDMT_VND_FOLLOWER_SSMIGRATE:
×
888
      if (vnodeProcessFollowerSsMigrateReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
889
      break;
×
890
    case TDMT_VND_KILL_SSMIGRATE:
×
891
      if (vnodeProcessKillSsMigrateReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
892
      break;
×
893
#endif
894
    /* TSDB */
895
    case TDMT_VND_SUBMIT: {
24,118,149✔
896
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.apply_time, METRIC_LEVEL_LOW, {
24,118,149!
897
        if (vnodeProcessSubmitReq(pVnode, ver, pMsg->pCont, pMsg->contLen, pRsp, pMsg) < 0) goto _err;
898
      });
899
      METRICS_UPDATE(pVnode->writeMetrics.apply_bytes, METRIC_LEVEL_LOW, (int64_t)pMsg->contLen);
24,118,079!
900
      break;
24,118,049✔
901
    }
902
    case TDMT_VND_DELETE:
119,030✔
903
      code = vnodeProcessDeleteReq(pVnode, ver, pReq, len, pRsp, pMsg);
119,030✔
904
      TSDB_CHECK_CODE(code, lino, _err);
119,029!
905
      break;
119,029✔
906
    case TDMT_VND_BATCH_DEL:
×
907
      code = vnodeProcessBatchDeleteReq(pVnode, ver, pReq, len, pRsp);
×
908
      TSDB_CHECK_CODE(code, lino, _err);
×
909
      break;
×
910
      /* TQ */
911
#if defined(USE_TQ) || defined(USE_STREAM)
912
    case TDMT_VND_TMQ_SUBSCRIBE:
2,386✔
913
      code = tqProcessSubscribeReq(pVnode->pTq, ver, pReq, len);
2,386✔
914
      TSDB_CHECK_CODE(code, lino, _err);
2,385!
915
      break;
2,385✔
916
    case TDMT_VND_TMQ_DELETE_SUB:
781✔
917
      code = tqProcessDeleteSubReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen);
781✔
918
      TSDB_CHECK_CODE(code, lino, _err);
780!
919
      break;
780✔
920
    case TDMT_VND_TMQ_COMMIT_OFFSET:
11,021✔
921
      code = tqProcessOffsetCommitReq(pVnode->pTq, ver, pReq, len);
11,021✔
922
      TSDB_CHECK_CODE(code, lino, _err);
11,020!
923
      break;
11,020✔
924
    case TDMT_VND_TMQ_ADD_CHECKINFO:
49✔
925
      code = tqProcessAddCheckInfoReq(pVnode->pTq, ver, pReq, len);
49✔
926
      TSDB_CHECK_CODE(code, lino, _err);
49!
927
      break;
49✔
928
    case TDMT_VND_TMQ_DEL_CHECKINFO:
10✔
929
      code = tqProcessDelCheckInfoReq(pVnode->pTq, ver, pReq, len);
10✔
930
      TSDB_CHECK_CODE(code, lino, _err);
10!
931
      break;
10✔
932
#endif
933
    case TDMT_VND_ALTER_CONFIRM:
8,498✔
934
      needCommit = pVnode->config.hashChange;
8,498✔
935
      code = vnodeProcessAlterConfirmReq(pVnode, ver, pReq, len, pRsp);
8,498✔
936
      TSDB_CHECK_CODE(code, lino, _err);
8,498!
937
      break;
8,498✔
938
    case TDMT_VND_ALTER_CONFIG:
1,012✔
939
      vnodeProcessAlterConfigReq(pVnode, ver, pReq, len, pRsp);
1,012✔
940
      break;
1,012✔
941
    case TDMT_VND_COMMIT:
24,982✔
942
      needCommit = true;
24,982✔
943
      break;
24,982✔
944
    case TDMT_VND_CREATE_INDEX:
1,889✔
945
      vnodeProcessCreateIndexReq(pVnode, ver, pReq, len, pRsp);
1,889✔
946
      break;
1,889✔
947
    case TDMT_VND_DROP_INDEX:
4,264✔
948
      vnodeProcessDropIndexReq(pVnode, ver, pReq, len, pRsp);
4,264✔
949
      break;
4,264✔
950
    case TDMT_VND_COMPACT:
179✔
951
      vnodeProcessCompactVnodeReq(pVnode, ver, pReq, len, pRsp);
179✔
952
      goto _exit;
179✔
953
    case TDMT_VND_SCAN:
5✔
954
      vnodeProcessScanVnodeReq(pVnode, ver, pReq, len, pRsp);
5✔
955
      goto _exit;
5✔
956
    case TDMT_SYNC_CONFIG_CHANGE:
×
957
      vnodeProcessConfigChangeReq(pVnode, ver, pReq, len, pRsp);
×
958
      break;
×
959
#ifdef TD_ENTERPRISE
960
    case TDMT_VND_KILL_COMPACT:
2✔
961
      vnodeProcessKillCompactReq(pVnode, ver, pReq, len, pRsp);
2✔
962
      break;
2✔
963
#endif
964
    case TDMT_VND_KILL_SCAN:
2✔
965
      vnodeProcessKillScanReq(pVnode, ver, pReq, len, pRsp);
2✔
966
      break;
2✔
967
    case TDMT_VND_KILL_TRIM:
×
968
      vnodeProcessKillRetentionReq(pVnode, ver, pReq, len, pRsp);
×
969
      break;
×
970
    /* ARB */
971
    case TDMT_VND_ARB_CHECK_SYNC:
21✔
972
      vnodeProcessArbCheckSyncReq(pVnode, pReq, len, pRsp);
21✔
973
      break;
21✔
974
    default:
×
975
      vError("vgId:%d, unprocessed msg, %d", TD_VID(pVnode), pMsg->msgType);
×
976
      return TSDB_CODE_INVALID_MSG;
×
977
  }
978

979
  vGDebug(&pMsg->info.traceId, "vgId:%d, index:%" PRId64 ", msg processed, code:0x%x", TD_VID(pVnode), ver, pRsp->code);
24,458,616!
980

981
  walApplyVer(pVnode->pWal, ver);
24,458,619✔
982

983
  if (pVnode->pTq) {
24,458,564!
984
    code = tqPushMsg(pVnode->pTq, pMsg->msgType);
24,458,613✔
985
    if (code) {
24,458,716!
986
      vError("vgId:%d, failed to push msg to TQ since %s", TD_VID(pVnode), tstrerror(terrno));
×
987
      return code;
×
988
    }
989
  }
990

991
  // commit if need
992
  if (needCommit) {
24,458,671✔
993
    vInfo("vgId:%d, commit at version %" PRId64, TD_VID(pVnode), ver);
25,071✔
994
    code = vnodeAsyncCommit(pVnode);
25,089✔
995
    if (code) {
25,094!
996
      vError("vgId:%d, failed to vnode async commit since %s.", TD_VID(pVnode), tstrerror(terrno));
×
997
      goto _err;
×
998
    }
999

1000
    // start a new one
1001
    METRICS_TIMING_BLOCK(pVnode->writeMetrics.memtable_wait_time, METRIC_LEVEL_LOW, {
25,094!
1002
      code = vnodeBegin(pVnode);
1003
      if (code) {
1004
        vError("vgId:%d, failed to begin vnode since %s.", TD_VID(pVnode), tstrerror(terrno));
1005
        goto _err;
1006
      }
1007
    });
1008
  }
1009

1010
_exit:
24,458,689✔
1011
  return 0;
24,484,004✔
1012

1013
_err:
175✔
1014
  if (code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
175✔
1015
    vInfo("vgId:%d, process %s request failed since %s, lino 1:%d ver:%" PRId64, TD_VID(pVnode),
4!
1016
          TMSG_INFO(pMsg->msgType), tstrerror(code), lino, ver);
1017
  } else if (code) {
171!
1018
    vError("vgId:%d, process %s request failed since %s, lino 2:%d ver:%" PRId64, TD_VID(pVnode),
×
1019
           TMSG_INFO(pMsg->msgType), tstrerror(code), lino, ver);
1020
  }
1021

1022
  return code;
175✔
1023
}
1024

1025
int32_t vnodePreprocessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg) {
13,789,731✔
1026
  if (TDMT_SCH_QUERY != pMsg->msgType && TDMT_SCH_MERGE_QUERY != pMsg->msgType) {
13,789,731✔
1027
    return 0;
711,050✔
1028
  }
1029

1030
  int32_t qType = 0;
13,078,681✔
1031
  return qWorkerPreprocessQueryMsg(pVnode->pQuery, pMsg, (TDMT_SCH_QUERY == pMsg->msgType), &qType);
13,078,681✔
1032
}
1033

1034
int32_t vnodeProcessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
13,802,070✔
1035
  vTrace("message in vnode query queue is processing");
13,802,070!
1036
  if (pMsg->msgType == TDMT_VND_TMQ_CONSUME && !syncIsReadyForRead(pVnode->sync)) {
13,802,070✔
1037
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
1,121✔
1038
    return 0;
1,121✔
1039
  }
1040

1041
  if (pMsg->msgType == TDMT_VND_TMQ_CONSUME && !pVnode->restored) {
13,800,945!
1042
    vnodeRedirectRpcMsg(pVnode, pMsg, TSDB_CODE_SYN_RESTORING);
×
1043
    return 0;
×
1044
  }
1045

1046
  SReadHandle handle = {0};
13,800,945✔
1047
  handle.vnode = pVnode;
13,800,945✔
1048
  handle.pMsgCb = &pVnode->msgCb;
13,800,945✔
1049
  handle.pWorkerCb = pInfo->workerCb;
13,800,945✔
1050
  initStorageAPI(&handle.api);
13,800,945✔
1051
  int32_t code = TSDB_CODE_SUCCESS;
13,799,122✔
1052
  bool    redirected = false;
13,799,122✔
1053

1054
  switch (pMsg->msgType) {
13,799,122!
1055
    case TDMT_SCH_QUERY:
9,717,830✔
1056
      if (!syncIsReadyForRead(pVnode->sync)) {
9,717,830✔
1057
        pMsg->code = (terrno) ? terrno : TSDB_CODE_SYN_NOT_LEADER;
80,502!
1058
        redirected = true;
80,502✔
1059
      }
1060
      code = qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
9,715,283✔
1061
      if (redirected) {
9,715,802✔
1062
        vnodeRedirectRpcMsg(pVnode, pMsg, pMsg->code);
80,635✔
1063
        return 0;
80,502✔
1064
      }
1065

1066
      return code;
9,635,167✔
1067
    case TDMT_SCH_MERGE_QUERY:
3,372,607✔
1068
      return qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
3,372,607✔
1069
    case TDMT_SCH_QUERY_CONTINUE:
654,546✔
1070
      return qWorkerProcessCQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
654,546✔
1071
    case TDMT_VND_TMQ_CONSUME:
42,855✔
1072
      return tqProcessPollReq(pVnode->pTq, pMsg);
42,855✔
1073
    case TDMT_VND_TMQ_CONSUME_PUSH:
12,524✔
1074
      return tqProcessPollPush(pVnode->pTq);
12,524✔
1075
    default:
×
1076
      vError("unknown msg type:%d in query queue", pMsg->msgType);
×
1077
      return TSDB_CODE_APP_ERROR;
×
1078
  }
1079
}
1080

1081
int32_t vnodeProcessFetchMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
36,208,902✔
1082
  int32_t code = TSDB_CODE_SUCCESS;
36,208,902✔
1083
  vTrace("vgId:%d, msg:%p in fetch queue is processing", pVnode->config.vgId, pMsg);
36,208,902!
1084
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
36,208,902!
1085
       pMsg->msgType == TDMT_VND_BATCH_META || pMsg->msgType == TDMT_VND_TABLE_NAME ||
25,733,562!
1086
       pMsg->msgType == TDMT_VND_VSUBTABLES_META || pMsg->msgType == TDMT_VND_VSTB_REF_DBS) &&
23,476,971!
1087
      !syncIsReadyForRead(pVnode->sync)) {
12,735,931✔
1088
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
8,180✔
1089
    return 0;
8,180✔
1090
  }
1091

1092
  switch (pMsg->msgType) {
36,212,035!
1093
    case TDMT_SCH_FETCH:
17,882,394✔
1094
    case TDMT_SCH_MERGE_FETCH:
1095
      return qWorkerProcessFetchMsg(pVnode, pVnode->pQuery, pMsg, 0);
17,882,394✔
1096
    case TDMT_SCH_FETCH_RSP:
×
1097
      return qWorkerProcessRspMsg(pVnode, pVnode->pQuery, pMsg, 0);
×
1098
    // case TDMT_SCH_CANCEL_TASK:
1099
    //   return qWorkerProcessCancelMsg(pVnode, pVnode->pQuery, pMsg, 0);
1100
    case TDMT_SCH_DROP_TASK:
13,070,340✔
1101
      return qWorkerProcessDropMsg(pVnode, pVnode->pQuery, pMsg, 0);
13,070,340✔
1102
    case TDMT_SCH_TASK_NOTIFY:
93✔
1103
      return qWorkerProcessNotifyMsg(pVnode, pVnode->pQuery, pMsg, 0);
93✔
1104
    case TDMT_SCH_QUERY_HEARTBEAT:
3,004,960✔
1105
      return qWorkerProcessHbMsg(pVnode, pVnode->pQuery, pMsg, 0);
3,004,960✔
1106
    case TDMT_VND_TABLE_META:
4,516✔
1107
    case TDMT_VND_TABLE_NAME:
1108
      return vnodeGetTableMeta(pVnode, pMsg, true);
4,516✔
1109
    case TDMT_VND_TABLE_CFG:
×
1110
      return vnodeGetTableCfg(pVnode, pMsg, true);
×
1111
    case TDMT_VND_BATCH_META: {
2,249,097✔
1112
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.fetch_batch_meta_time, METRIC_LEVEL_LOW,
2,249,097!
1113
                           { code = vnodeGetBatchMeta(pVnode, pMsg); });
1114
      METRICS_UPDATE(pVnode->writeMetrics.fetch_batch_meta_count, METRIC_LEVEL_LOW, 1);
2,250,527!
1115
      return code;
2,250,567✔
1116
    }
1117
    case TDMT_VND_VSUBTABLES_META:
×
1118
      return vnodeGetVSubtablesMeta(pVnode, pMsg);
×
1119
    case TDMT_VND_VSTB_REF_DBS:
×
1120
      return vnodeGetVStbRefDbs(pVnode, pMsg);
×
1121
    case TDMT_VND_QUERY_SCAN_PROGRESS:
10✔
1122
      return vnodeQueryScanProgress(pVnode, pMsg);
10✔
1123
#ifdef TD_ENTERPRISE
1124
    case TDMT_VND_QUERY_COMPACT_PROGRESS:
209✔
1125
      return vnodeQueryCompactProgress(pVnode, pMsg);
209✔
1126

1127
    case TDMT_VND_LIST_SSMIGRATE_FILESETS:
×
1128
      return vnodeListSsMigrateFileSets(pVnode, pMsg);
×
1129

1130
    case TDMT_VND_QUERY_SSMIGRATE_PROGRESS:
×
1131
      return vnodeQuerySsMigrateProgress(pVnode, pMsg);
×
1132
#endif
1133
    case TDMT_VND_QUERY_TRIM_PROGRESS:
408✔
1134
      return vnodeQueryRetentionProgress(pVnode, pMsg);
408✔
1135
      //    case TDMT_VND_TMQ_CONSUME:
1136
      //      return tqProcessPollReq(pVnode->pTq, pMsg);
1137
#ifdef USE_TQ
1138
    case TDMT_VND_TMQ_VG_WALINFO:
3✔
1139
      return tqProcessVgWalInfoReq(pVnode->pTq, pMsg);
3✔
1140
    case TDMT_VND_TMQ_VG_COMMITTEDINFO:
1✔
1141
      return tqProcessVgCommittedInfoReq(pVnode->pTq, pMsg);
1✔
1142
    case TDMT_VND_TMQ_SEEK:
4✔
1143
      return tqProcessSeekReq(pVnode->pTq, pMsg);
4✔
1144
#endif
1145
    default:
×
1146
      vError("unknown msg type:%d in fetch queue", pMsg->msgType);
×
1147
      return TSDB_CODE_APP_ERROR;
×
1148
  }
1149
}
1150

1151
void vnodeUpdateMetaRsp(SVnode *pVnode, STableMetaRsp *pMetaRsp) {
194,026✔
1152
  if (NULL == pMetaRsp) {
194,026!
1153
    return;
×
1154
  }
1155

1156
  tstrncpy(pMetaRsp->dbFName, pVnode->config.dbname, TSDB_DB_FNAME_LEN);
194,026✔
1157
  pMetaRsp->dbId = pVnode->config.dbId;
194,026✔
1158
  pMetaRsp->vgId = TD_VID(pVnode);
194,026✔
1159
  pMetaRsp->precision = pVnode->config.tsdbCfg.precision;
194,026✔
1160
}
1161

1162
extern int32_t vnodeAsyncRetention(SVnode *pVnode, STimeWindow tw, int8_t optrType, int8_t triggerType);
1163

1164
static int32_t vnodeProcessTrimReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
250✔
1165
  if (!pVnode->restored) {
250✔
1166
    vInfo("vgId:%d, ignore trim req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
9!
1167
    return 0;
9✔
1168
  }
1169

1170
  int32_t     code = 0;
241✔
1171
  SVTrimDbReq trimReq = {0};
241✔
1172

1173
  // decode
1174
  if (tDeserializeSVTrimDbReq(pReq, len, &trimReq) != 0) {
241!
1175
    code = TSDB_CODE_INVALID_MSG;
×
1176
    goto _exit;
×
1177
  }
1178

1179
  vInfo("vgId:%d, process trim vnode request, time:%" PRIi64 ",%" PRIi64 ", optr:%d, trigger:%d", pVnode->config.vgId,
241!
1180
        trimReq.tw.skey, trimReq.tw.ekey, (int32_t)trimReq.optrType, (int32_t)trimReq.triggerType);
1181

1182
  code = vnodeAsyncRetention(pVnode, trimReq.tw, (int8_t)trimReq.optrType, (int8_t)trimReq.triggerType);
241✔
1183

1184
_exit:
241✔
1185
  return code;
241✔
1186
}
1187

1188
extern int32_t vnodeAsyncSsMigrateFileSet(SVnode *pVnode, SSsMigrateFileSetReq *pReq);
1189

1190
static int32_t vnodeProcessSsMigrateFileSetReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1191
  int32_t code = 0;
×
1192

1193
  SSsMigrateFileSetReq req = {0};
×
1194
  SSsMigrateFileSetRsp rsp = {0};
×
1195
  pRsp->msgType = TDMT_VND_SSMIGRATE_FILESET_RSP;
×
1196
  pRsp->code = 0;
×
1197
  pRsp->pCont = NULL;
×
1198
  pRsp->contLen = 0;
×
1199

1200
  if (tDeserializeSSsMigrateFileSetReq(pReq, len, &req) != 0) {
×
1201
    code = TSDB_CODE_INVALID_MSG;
×
1202
    goto _exit;
×
1203
  }
1204

1205
  vInfo("vgId:%d, ssmigrate:%d, fid:%d, process ssmigrate fileset request, time:%" PRId64, pVnode->config.vgId,
×
1206
        req.ssMigrateId, req.fid, req.startTimeSec);
1207
  rsp.ssMigrateId = req.ssMigrateId;
×
1208
  rsp.vgId = TD_VID(pVnode);
×
1209
  rsp.nodeId = req.nodeId;
×
1210
  rsp.fid = req.fid;
×
1211

1212
  code = vnodeAsyncSsMigrateFileSet(pVnode, &req);
×
1213
  if (code != TSDB_CODE_SUCCESS) {
×
1214
    vError("vgId:%d, failed to async ssmigrate since %s", TD_VID(pVnode), tstrerror(code));
×
1215
    pRsp->code = code;
×
1216
    goto _exit;
×
1217
  }
1218

1219
  pRsp->code = TSDB_CODE_SUCCESS;
×
1220
  pRsp->contLen = tSerializeSSsMigrateFileSetRsp(NULL, 0, &rsp);
×
1221
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
×
1222
  if (pRsp->pCont == NULL) {
×
1223
    vError("vgId:%d, failed to allocate memory for ssmigrate fileset response", TD_VID(pVnode));
×
1224
    code = TSDB_CODE_OUT_OF_MEMORY;
×
1225
    goto _exit;
×
1226
  }
1227
  TAOS_UNUSED(tSerializeSSsMigrateFileSetRsp(pRsp->pCont, pRsp->contLen, &rsp));
×
1228

1229
_exit:
×
1230
  pRsp->code = code;
×
1231
  return code;
×
1232
}
1233

1234
extern int32_t vnodeFollowerSsMigrate(SVnode *pVnode, SSsMigrateProgress *pReq);
1235

1236
static int32_t vnodeProcessFollowerSsMigrateReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1237
  int32_t            code = 0;
×
1238
  SSsMigrateProgress req = {0};
×
1239

1240
  // decode
1241
  if (tDeserializeSSsMigrateProgress(pReq, len, &req) != 0) {
×
1242
    code = TSDB_CODE_INVALID_MSG;
×
1243
    goto _exit;
×
1244
  }
1245

1246
  code = vnodeFollowerSsMigrate(pVnode, &req);
×
1247

1248
_exit:
×
1249
  pRsp->code = code;
×
1250
  return code;
×
1251
}
1252

1253
extern int32_t vnodeKillSsMigrate(SVnode *pVnode, SVnodeKillSsMigrateReq *pReq);
1254

1255
static int32_t vnodeProcessKillSsMigrateReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1256
  int32_t          code = 0;
×
1257
  SVnodeKillSsMigrateReq req = {0};
×
1258

1259
  // decode
1260
  if (tDeserializeSVnodeKillSsMigrateReq(pReq, len, &req) != 0) {
×
1261
    code = TSDB_CODE_INVALID_MSG;
×
1262
    goto _exit;
×
1263
  }
1264

1265
  code = vnodeKillSsMigrate(pVnode, &req);
×
1266

1267
_exit:
×
1268
  pRsp->code = code;
×
1269
  return code;
×
1270
}
1271

1272
static int32_t vnodeProcessDropTtlTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1273
  int               ret = 0;
×
1274
  SVDropTtlTableReq ttlReq = {0};
×
1275
  if (tDeserializeSVDropTtlTableReq(pReq, len, &ttlReq) != 0) {
×
1276
    ret = TSDB_CODE_INVALID_MSG;
×
1277
    goto end;
×
1278
  }
1279

1280
  if (ttlReq.nUids != taosArrayGetSize(ttlReq.pTbUids)) {
×
1281
    ret = TSDB_CODE_INVALID_MSG;
×
1282
    goto end;
×
1283
  }
1284

1285
  if (ttlReq.nUids != 0) {
×
1286
    vInfo("vgId:%d, process drop ttl table request, time:%d, ntbUids:%d", pVnode->config.vgId, ttlReq.timestampSec,
×
1287
          ttlReq.nUids);
1288
  }
1289

1290
  if (ttlReq.nUids > 0) {
×
1291
    int32_t code = metaDropMultipleTables(pVnode->pMeta, ver, ttlReq.pTbUids);
×
1292
    if (code) return code;
×
1293

1294
    code = tqUpdateTbUidList(pVnode->pTq, ttlReq.pTbUids, false);
×
1295
    if (code) {
×
1296
      vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1297
    }
1298
  }
1299

1300
end:
×
1301
  taosArrayDestroy(ttlReq.pTbUids);
×
1302
  return ret;
×
1303
}
1304

1305
static int32_t vnodeProcessFetchTtlExpiredTbs(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
16✔
1306
  int32_t                 code = -1;
16✔
1307
  SMetaReader             mr = {0};
16✔
1308
  SVDropTtlTableReq       ttlReq = {0};
16✔
1309
  SVFetchTtlExpiredTbsRsp rsp = {0};
16✔
1310
  SEncoder                encoder = {0};
16✔
1311
  SArray                 *pNames = NULL;
16✔
1312
  pRsp->msgType = TDMT_VND_FETCH_TTL_EXPIRED_TBS_RSP;
16✔
1313
  pRsp->code = TSDB_CODE_SUCCESS;
16✔
1314
  pRsp->pCont = NULL;
16✔
1315
  pRsp->contLen = 0;
16✔
1316

1317
  if (tDeserializeSVDropTtlTableReq(pReq, len, &ttlReq) != 0) {
16!
1318
    terrno = TSDB_CODE_INVALID_MSG;
×
1319
    goto _end;
×
1320
  }
1321

1322
  if (!(ttlReq.nUids == taosArrayGetSize(ttlReq.pTbUids))) {
16!
1323
    terrno = TSDB_CODE_INVALID_MSG;
×
1324
    goto _end;
×
1325
  }
1326

1327
  tb_uid_t    suid;
1328
  char        ctbName[TSDB_TABLE_NAME_LEN];
1329
  SVDropTbReq expiredTb = {.igNotExists = true};
16✔
1330
  metaReaderDoInit(&mr, pVnode->pMeta, 0);
16✔
1331
  rsp.vgId = TD_VID(pVnode);
16✔
1332
  rsp.pExpiredTbs = taosArrayInit(ttlReq.nUids, sizeof(SVDropTbReq));
16✔
1333
  if (!rsp.pExpiredTbs) goto _end;
16!
1334

1335
  pNames = taosArrayInit(ttlReq.nUids, TSDB_TABLE_NAME_LEN);
16✔
1336
  if (!pNames) {
16!
1337
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1338
    goto _end;
×
1339
  }
1340
  char buf[TSDB_TABLE_NAME_LEN];
1341
  for (int32_t i = 0; i < ttlReq.nUids; ++i) {
78✔
1342
    tb_uid_t *uid = taosArrayGet(ttlReq.pTbUids, i);
62✔
1343
    expiredTb.suid = *uid;
62✔
1344
    terrno = metaReaderGetTableEntryByUid(&mr, *uid);
62✔
1345
    if (terrno < 0) goto _end;
62!
1346
    tstrncpy(buf, mr.me.name, TSDB_TABLE_NAME_LEN);
62✔
1347
    void *p = taosArrayPush(pNames, buf);
62✔
1348
    if (p == NULL) {
62!
1349
      goto _end;
×
1350
    }
1351

1352
    expiredTb.name = p;
62✔
1353
    if (mr.me.type == TSDB_CHILD_TABLE) {
62✔
1354
      expiredTb.suid = mr.me.ctbEntry.suid;
37✔
1355
    }
1356

1357
    if (taosArrayPush(rsp.pExpiredTbs, &expiredTb) == NULL) {
124!
1358
      goto _end;
×
1359
    }
1360
  }
1361

1362
  int32_t ret = 0;
16✔
1363
  tEncodeSize(tEncodeVFetchTtlExpiredTbsRsp, &rsp, pRsp->contLen, ret);
16!
1364
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
16✔
1365
  if (pRsp->pCont == NULL) {
16!
1366
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1367
    code = -1;
×
1368
    goto _end;
×
1369
  }
1370
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
16✔
1371
  terrno = tEncodeVFetchTtlExpiredTbsRsp(&encoder, &rsp);
16✔
1372
  tEncoderClear(&encoder);
16✔
1373

1374
  if (terrno == 0) code = 0;
16!
1375
_end:
×
1376
  metaReaderClear(&mr);
16✔
1377
  tFreeFetchTtlExpiredTbsRsp(&rsp);
16✔
1378
  taosArrayDestroy(ttlReq.pTbUids);
16✔
1379
  if (pNames) taosArrayDestroy(pNames);
16!
1380
  pRsp->code = terrno;
16✔
1381
  return code;
16✔
1382
}
1383

1384
static int32_t vnodeProcessCreateStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
26,406✔
1385
  int32_t        code = 0;
26,406✔
1386
  SVCreateStbReq req = {0};
26,406✔
1387
  SDecoder       coder;
1388

1389
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
26,406✔
1390
  pRsp->code = TSDB_CODE_SUCCESS;
26,406✔
1391
  pRsp->pCont = NULL;
26,406✔
1392
  pRsp->contLen = 0;
26,406✔
1393

1394
  // decode and process req
1395
  tDecoderInit(&coder, pReq, len);
26,406✔
1396

1397
  code = tDecodeSVCreateStbReq(&coder, &req);
26,406✔
1398
  if (code) {
26,402!
1399
    pRsp->code = code;
×
1400
    goto _err;
×
1401
  }
1402

1403
  code = metaCreateSuperTable(pVnode->pMeta, ver, &req);
26,402✔
1404
  if (code) {
26,417✔
1405
    pRsp->code = code;
4✔
1406
    goto _err;
4✔
1407
  }
1408

1409
  tDecoderClear(&coder);
26,413✔
1410
  return 0;
26,413✔
1411

1412
_err:
4✔
1413
  tDecoderClear(&coder);
4✔
1414
  return code;
4✔
1415
}
1416

1417
static int32_t vnodeProcessCreateTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
84,496✔
1418
                                       SRpcMsg *pOriginRpc) {
1419
  SDecoder           decoder = {0};
84,496✔
1420
  SEncoder           encoder = {0};
84,496✔
1421
  int32_t            rcode = 0;
84,496✔
1422
  SVCreateTbBatchReq req = {0};
84,496✔
1423
  SVCreateTbReq     *pCreateReq;
1424
  SVCreateTbBatchRsp rsp = {0};
84,496✔
1425
  SVCreateTbRsp      cRsp = {0};
84,496✔
1426
  char               tbName[TSDB_TABLE_FNAME_LEN];
1427
  SArray            *tbUids = NULL;
84,496✔
1428
  SArray            *tbNames = NULL;
84,496✔
1429
  pRsp->msgType = TDMT_VND_CREATE_TABLE_RSP;
84,496✔
1430
  pRsp->code = TSDB_CODE_SUCCESS;
84,496✔
1431
  pRsp->pCont = NULL;
84,496✔
1432
  pRsp->contLen = 0;
84,496✔
1433

1434
  // decode
1435
  tDecoderInit(&decoder, pReq, len);
84,496✔
1436
  if (tDecodeSVCreateTbBatchReq(&decoder, &req) < 0) {
84,496!
1437
    rcode = -1;
×
1438
    terrno = TSDB_CODE_INVALID_MSG;
×
1439
    goto _exit;
×
1440
  }
1441

1442
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(cRsp));
84,494✔
1443
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
84,510✔
1444
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
84,508✔
1445
  if (rsp.pArray == NULL || tbUids == NULL || tbNames == NULL) {
84,510!
1446
    rcode = -1;
1✔
1447
    terrno = TSDB_CODE_OUT_OF_MEMORY;
1✔
1448
    goto _exit;
×
1449
  }
1450

1451
  // loop to create table
1452
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
227,002✔
1453
    pCreateReq = req.pReqs + iReq;
142,482✔
1454
    memset(&cRsp, 0, sizeof(cRsp));
142,482✔
1455

1456
    if (tsEnableAudit && tsEnableAuditCreateTable) {
142,482!
1457
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
142,481!
1458
      if (str == NULL) {
142,478!
1459
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1460
        rcode = -1;
×
1461
        goto _exit;
×
1462
      }
1463
      tstrncpy(str, pCreateReq->name, TSDB_TABLE_FNAME_LEN);
142,478✔
1464
      if (taosArrayPush(tbNames, &str) == NULL) {
142,473!
1465
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1466
        rcode = -1;
×
1467
        goto _exit;
×
1468
      }
1469
    }
1470

1471
    // validate hash
1472
    (void)tsnprintf(tbName, TSDB_TABLE_FNAME_LEN, "%s.%s", pVnode->config.dbname, pCreateReq->name);
142,474✔
1473
    if (vnodeValidateTableHash(pVnode, tbName) < 0) {
142,480!
1474
      cRsp.code = TSDB_CODE_VND_HASH_MISMATCH;
×
1475
      if (taosArrayPush(rsp.pArray, &cRsp) == NULL) {
×
1476
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1477
        rcode = -1;
×
1478
        goto _exit;
×
1479
      }
1480
      vError("vgId:%d create-table:%s failed due to hash value mismatch", TD_VID(pVnode), tbName);
×
1481
      continue;
×
1482
    }
1483

1484
    // do create table
1485
    if (metaCreateTable2(pVnode->pMeta, ver, pCreateReq, &cRsp.pMeta) < 0) {
142,475✔
1486
      if (pCreateReq->flags & TD_CREATE_IF_NOT_EXISTS && terrno == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
124!
1487
        cRsp.code = TSDB_CODE_SUCCESS;
108✔
1488
      } else {
1489
        cRsp.code = terrno;
16✔
1490
      }
1491
    } else {
1492
      cRsp.code = TSDB_CODE_SUCCESS;
142,368✔
1493
      if (taosArrayPush(tbUids, &pCreateReq->uid) == NULL) {
284,735!
1494
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1495
        rcode = -1;
×
1496
        goto _exit;
×
1497
      }
1498
      vnodeUpdateMetaRsp(pVnode, cRsp.pMeta);
142,367✔
1499
    }
1500

1501
    if (taosArrayPush(rsp.pArray, &cRsp) == NULL) {
284,985!
1502
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1503
      rcode = -1;
×
1504
      goto _exit;
×
1505
    }
1506
  }
1507

1508
  vTrace("vgId:%d, add %d new created tables into query table list", TD_VID(pVnode), (int32_t)taosArrayGetSize(tbUids));
84,520✔
1509
  if (tqUpdateTbUidList(pVnode->pTq, tbUids, true) < 0) {
84,520!
1510
    vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(terrno));
×
1511
  }
1512

1513
  // prepare rsp
1514
  int32_t ret = 0;
84,514✔
1515
  tEncodeSize(tEncodeSVCreateTbBatchRsp, &rsp, pRsp->contLen, ret);
84,514!
1516
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
84,511✔
1517
  if (pRsp->pCont == NULL) {
84,514!
1518
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1519
    rcode = -1;
×
1520
    goto _exit;
×
1521
  }
1522
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
84,514✔
1523
  if (tEncodeSVCreateTbBatchRsp(&encoder, &rsp) != 0) {
84,514!
1524
    vError("vgId:%d, failed to encode create table batch response", TD_VID(pVnode));
×
1525
  }
1526

1527
  if (tsEnableAudit && tsEnableAuditCreateTable) {
84,512!
1528
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
84,511✔
1529

1530
    SName name = {0};
84,511✔
1531
    if (tNameFromString(&name, pVnode->config.dbname, T_NAME_ACCT | T_NAME_DB) < 0) {
84,511!
1532
      vError("vgId:%d, failed to get name from string", TD_VID(pVnode));
×
1533
    }
1534

1535
    SStringBuilder sb = {0};
84,507✔
1536
    for (int32_t i = 0; i < tbNames->size; i++) {
226,964✔
1537
      char **key = (char **)taosArrayGet(tbNames, i);
142,464✔
1538
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
142,465✔
1539
      if (i < tbNames->size - 1) {
142,462✔
1540
        taosStringBuilderAppendChar(&sb, ',');
57,956✔
1541
      }
1542
      // taosMemoryFreeClear(*key);
1543
    }
1544

1545
    size_t len = 0;
84,500✔
1546
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
84,500✔
1547

1548
    if (pOriginRpc->info.conn.user != NULL && strlen(pOriginRpc->info.conn.user) > 0) {
84,510!
1549
      auditAddRecord(pOriginRpc, clusterId, "createTable", name.dbname, "", keyJoined, len);
75,789✔
1550
    }
1551

1552
    taosStringBuilderDestroy(&sb);
84,511✔
1553
  }
1554

1555
_exit:
1✔
1556
  tDeleteSVCreateTbBatchReq(&req);
84,513✔
1557
  taosArrayDestroyEx(rsp.pArray, tFreeSVCreateTbRsp);
84,511✔
1558
  taosArrayDestroy(tbUids);
84,514✔
1559
  tDecoderClear(&decoder);
84,512✔
1560
  tEncoderClear(&encoder);
84,510✔
1561
  taosArrayDestroyP(tbNames, NULL);
84,512✔
1562
  return rcode;
84,513✔
1563
}
1564

1565
static int32_t vnodeProcessAlterStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
16,187✔
1566
  int32_t        code = 0;
16,187✔
1567
  SVCreateStbReq req = {0};
16,187✔
1568
  SDecoder       dc = {0};
16,187✔
1569

1570
  pRsp->msgType = TDMT_VND_ALTER_STB_RSP;
16,187✔
1571
  pRsp->code = TSDB_CODE_SUCCESS;
16,187✔
1572
  pRsp->pCont = NULL;
16,187✔
1573
  pRsp->contLen = 0;
16,187✔
1574

1575
  tDecoderInit(&dc, pReq, len);
16,187✔
1576

1577
  // decode req
1578
  code = tDecodeSVCreateStbReq(&dc, &req);
16,187✔
1579
  if (code) {
16,185!
1580
    tDecoderClear(&dc);
×
1581
    return code;
×
1582
  }
1583

1584
  code = metaAlterSuperTable(pVnode->pMeta, ver, &req);
16,185✔
1585
  if (code) {
16,186!
1586
    pRsp->code = code;
×
1587
    tDecoderClear(&dc);
×
1588
    return code;
×
1589
  }
1590

1591
  tDecoderClear(&dc);
16,186✔
1592

1593
  return 0;
16,187✔
1594
}
1595

1596
static int32_t vnodeProcessDropStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp, SRpcMsg *pOriginRpc) {
3,946✔
1597
  SVDropStbReq req = {0};
3,946✔
1598
  int32_t      rcode = TSDB_CODE_SUCCESS;
3,946✔
1599
  SDecoder     decoder = {0};
3,946✔
1600
  SArray      *tbUidList = NULL;
3,946✔
1601

1602
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
3,946✔
1603
  pRsp->pCont = NULL;
3,946✔
1604
  pRsp->contLen = 0;
3,946✔
1605

1606
  // decode request
1607
  tDecoderInit(&decoder, pReq, len);
3,946✔
1608
  if (tDecodeSVDropStbReq(&decoder, &req) < 0) {
3,946!
1609
    rcode = TSDB_CODE_INVALID_MSG;
×
1610
    goto _exit;
×
1611
  }
1612

1613
  STraceId* trace = &(pOriginRpc->info.traceId);
3,941✔
1614

1615
  vInfo("vgId:%d, start to process vnode-drop-stb, QID:0x%" PRIx64 ":0x%" PRIx64 ", drop stb:%s", TD_VID(pVnode), trace ? trace->rootId : 0, 
3,941!
1616
              trace ? trace->msgId : 0, req.name);
1617

1618
  // process request
1619
  tbUidList = taosArrayInit(8, sizeof(int64_t));
3,946✔
1620
  if (tbUidList == NULL) goto _exit;
3,946!
1621
  if (metaDropSuperTable(pVnode->pMeta, ver, &req) < 0) {
3,946✔
1622
    rcode = terrno;
2✔
1623
    goto _exit;
2✔
1624
  }
1625

1626
  if (tqUpdateTbUidList(pVnode->pTq, tbUidList, false) < 0) {
3,944!
1627
    rcode = terrno;
×
1628
    goto _exit;
×
1629
  }
1630

1631
  // return rsp
1632
_exit:
3,944✔
1633
  vInfo("vgId:%d, finished to process vnode-drop-stb, QID:0x%" PRIx64 ":0x%" PRIx64 ", drop stb:%s", TD_VID(pVnode), trace ? trace->rootId : 0, 
3,946!
1634
              trace ? trace->msgId : 0, req.name);
1635
  if (tbUidList) taosArrayDestroy(tbUidList);
3,946!
1636
  pRsp->code = rcode;
3,946✔
1637
  tDecoderClear(&decoder);
3,946✔
1638
  return 0;
3,946✔
1639
}
1640

1641
static int32_t vnodeProcessAlterTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
19,783✔
1642
  SVAlterTbReq  vAlterTbReq = {0};
19,783✔
1643
  SVAlterTbRsp  vAlterTbRsp = {0};
19,783✔
1644
  SDecoder      dc = {0};
19,783✔
1645
  int32_t       code = 0;
19,783✔
1646
  int32_t       lino = 0;
19,783✔
1647
  int32_t       ret;
1648
  SEncoder      ec = {0};
19,783✔
1649
  STableMetaRsp vMetaRsp = {0};
19,783✔
1650

1651
  pRsp->msgType = TDMT_VND_ALTER_TABLE_RSP;
19,783✔
1652
  pRsp->pCont = NULL;
19,783✔
1653
  pRsp->contLen = 0;
19,783✔
1654
  pRsp->code = TSDB_CODE_SUCCESS;
19,783✔
1655

1656
  tDecoderInit(&dc, pReq, len);
19,783✔
1657

1658
  // decode
1659
  if (tDecodeSVAlterTbReq(&dc, &vAlterTbReq) < 0) {
19,783!
1660
    vAlterTbRsp.code = TSDB_CODE_INVALID_MSG;
×
1661
    tDecoderClear(&dc);
×
1662
    goto _exit;
×
1663
  }
1664

1665
  // process
1666
  if (metaAlterTable(pVnode->pMeta, ver, &vAlterTbReq, &vMetaRsp) < 0) {
19,783✔
1667
    vAlterTbRsp.code = terrno;
922✔
1668
    tDecoderClear(&dc);
922✔
1669
    goto _exit;
922✔
1670
  }
1671
  tDecoderClear(&dc);
18,861✔
1672

1673
  if (NULL != vMetaRsp.pSchemas) {
18,861✔
1674
    vnodeUpdateMetaRsp(pVnode, &vMetaRsp);
5,612✔
1675
    vAlterTbRsp.pMeta = &vMetaRsp;
5,612✔
1676
  }
1677

1678
  if (vAlterTbReq.action == TSDB_ALTER_TABLE_UPDATE_TAG_VAL ||
18,861✔
1679
      vAlterTbReq.action == TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL) {
5,674✔
1680
    int64_t uid = metaGetTableEntryUidByName(pVnode->pMeta, vAlterTbReq.tbName);
13,192✔
1681
    if (uid == 0) {
13,192!
1682
      vError("vgId:%d, %s failed at %s:%d since table %s not found", TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
1683
             vAlterTbReq.tbName);
1684
      goto _exit;
×
1685
    }
1686

1687
    SArray *tbUids = taosArrayInit(4, sizeof(int64_t));
13,192✔
1688
    void   *p = taosArrayPush(tbUids, &uid);
13,192✔
1689
    TSDB_CHECK_NULL(p, code, lino, _exit, terrno);
13,192!
1690

1691
    vDebug("vgId:%d, remove tags value altered table:%s from query table list", TD_VID(pVnode), vAlterTbReq.tbName);
13,192✔
1692
    if ((code = tqUpdateTbUidList(pVnode->pTq, tbUids, false)) < 0) {
13,192!
1693
      vError("vgId:%d, failed to remove tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1694
    }
1695

1696
    vDebug("vgId:%d, try to add table:%s in query table list", TD_VID(pVnode), vAlterTbReq.tbName);
13,192✔
1697
    if ((code = tqUpdateTbUidList(pVnode->pTq, tbUids, true)) < 0) {
13,192!
1698
      vError("vgId:%d, failed to add tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1699
    }
1700

1701
    taosArrayDestroy(tbUids);
13,192✔
1702
  }
1703

1704
_exit:
5,669✔
1705
  taosArrayDestroy(vAlterTbReq.pMultiTag);
19,783✔
1706
  tEncodeSize(tEncodeSVAlterTbRsp, &vAlterTbRsp, pRsp->contLen, ret);
19,783!
1707
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
19,783✔
1708
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
19,783✔
1709
  if (tEncodeSVAlterTbRsp(&ec, &vAlterTbRsp) != 0) {
19,783!
1710
    vError("vgId:%d, failed to encode alter table response", TD_VID(pVnode));
×
1711
  }
1712

1713
  tEncoderClear(&ec);
19,783✔
1714
  if (vMetaRsp.pSchemas) {
19,783✔
1715
    taosMemoryFree(vMetaRsp.pSchemas);
5,612!
1716
    taosMemoryFree(vMetaRsp.pSchemaExt);
5,612!
1717
  }
1718
  if (vMetaRsp.pColRefs) {
19,783✔
1719
    taosMemoryFree(vMetaRsp.pColRefs);
432!
1720
  }
1721
  return 0;
19,783✔
1722
}
1723

1724
static int32_t vnodeProcessDropTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
15,488✔
1725
                                     SRpcMsg *pOriginRpc) {
1726
  SVDropTbBatchReq req = {0};
15,488✔
1727
  SVDropTbBatchRsp rsp = {0};
15,488✔
1728
  SDecoder         decoder = {0};
15,488✔
1729
  SEncoder         encoder = {0};
15,488✔
1730
  int32_t          ret;
1731
  SArray          *tbUids = NULL;
15,488✔
1732
  SArray          *tbNames = NULL;
15,488✔
1733

1734
  pRsp->msgType = ((SRpcMsg *)pReq)->msgType + 1;
15,488✔
1735
  pRsp->pCont = NULL;
15,488✔
1736
  pRsp->contLen = 0;
15,488✔
1737
  pRsp->code = TSDB_CODE_SUCCESS;
15,488✔
1738

1739
  // decode req
1740
  tDecoderInit(&decoder, pReq, len);
15,488✔
1741
  ret = tDecodeSVDropTbBatchReq(&decoder, &req);
15,488✔
1742
  if (ret < 0) {
15,488!
1743
    terrno = TSDB_CODE_INVALID_MSG;
×
1744
    pRsp->code = terrno;
×
1745
    goto _exit;
×
1746
  }
1747

1748
  // process req
1749
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
15,488✔
1750
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(SVDropTbRsp));
15,488✔
1751
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
15,488✔
1752
  if (tbUids == NULL || rsp.pArray == NULL || tbNames == NULL) goto _exit;
15,488!
1753

1754
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
31,379✔
1755
    SVDropTbReq *pDropTbReq = req.pReqs + iReq;
15,891✔
1756
    SVDropTbRsp  dropTbRsp = {0};
15,891✔
1757
    tb_uid_t     tbUid = 0;
15,891✔
1758

1759
    /* code */
1760
    ret = metaDropTable2(pVnode->pMeta, ver, pDropTbReq);
15,891✔
1761
    if (ret < 0) {
15,891!
1762
      if (pDropTbReq->igNotExists && terrno == TSDB_CODE_TDB_TABLE_NOT_EXIST) {
×
1763
        dropTbRsp.code = TSDB_CODE_SUCCESS;
×
1764
      } else {
1765
        dropTbRsp.code = terrno;
×
1766
      }
1767
    } else {
1768
      dropTbRsp.code = TSDB_CODE_SUCCESS;
15,891✔
1769
    }
1770

1771
    if (taosArrayPush(rsp.pArray, &dropTbRsp) == NULL) {
31,782!
1772
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1773
      pRsp->code = terrno;
×
1774
      goto _exit;
×
1775
    }
1776

1777
    if (tsEnableAuditCreateTable) {
15,891!
1778
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
15,891!
1779
      if (str == NULL) {
15,891!
1780
        pRsp->code = terrno;
×
1781
        goto _exit;
×
1782
      }
1783
      tstrncpy(str, pDropTbReq->name, TSDB_TABLE_FNAME_LEN);
15,891✔
1784
      if (taosArrayPush(tbNames, &str) == NULL) {
15,891!
1785
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1786
        pRsp->code = terrno;
×
1787
        goto _exit;
×
1788
      }
1789
    }
1790
  }
1791

1792
  if (tqUpdateTbUidList(pVnode->pTq, tbUids, false) < 0) {
15,488!
1793
    vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(terrno));
×
1794
  }
1795

1796
  if (tsEnableAuditCreateTable) {
15,488!
1797
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
15,488✔
1798

1799
    SName name = {0};
15,488✔
1800
    if (tNameFromString(&name, pVnode->config.dbname, T_NAME_ACCT | T_NAME_DB) != 0) {
15,488!
1801
      vError("vgId:%d, failed to get name from string", TD_VID(pVnode));
×
1802
    }
1803

1804
    SStringBuilder sb = {0};
15,488✔
1805
    for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
31,379✔
1806
      char **key = (char **)taosArrayGet(tbNames, iReq);
15,891✔
1807
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
15,891✔
1808
      if (iReq < req.nReqs - 1) {
15,891✔
1809
        taosStringBuilderAppendChar(&sb, ',');
403✔
1810
      }
1811
      taosMemoryFreeClear(*key);
15,891!
1812
    }
1813

1814
    size_t len = 0;
15,488✔
1815
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
15,488✔
1816

1817
    if (pOriginRpc->info.conn.user != NULL && strlen(pOriginRpc->info.conn.user) > 0) {
15,488!
1818
      auditAddRecord(pOriginRpc, clusterId, "dropTable", name.dbname, "", keyJoined, len);
15,487✔
1819
    }
1820

1821
    taosStringBuilderDestroy(&sb);
15,488✔
1822
  }
1823

1824
_exit:
×
1825
  taosArrayDestroy(tbUids);
15,488✔
1826
  tDecoderClear(&decoder);
15,488✔
1827
  tEncodeSize(tEncodeSVDropTbBatchRsp, &rsp, pRsp->contLen, ret);
15,488!
1828
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
15,488✔
1829
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
15,488✔
1830
  if (tEncodeSVDropTbBatchRsp(&encoder, &rsp) != 0) {
15,488!
1831
    vError("vgId:%d, failed to encode drop table batch response", TD_VID(pVnode));
×
1832
  }
1833
  tEncoderClear(&encoder);
15,488✔
1834
  taosArrayDestroy(rsp.pArray);
15,488✔
1835
  taosArrayDestroy(tbNames);
15,488✔
1836
  return 0;
15,488✔
1837
}
1838

1839
static int32_t vnodeProcessCreateRsmaReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
24✔
1840
  int32_t         code = 0, lino = 0;
24✔
1841
  SVCreateRsmaReq req = {0};
24✔
1842
  TAOS_CHECK_EXIT(tDeserializeSVCreateRsmaReq(pReq, len, &req));
24!
1843
  TAOS_CHECK_EXIT(metaCreateRsma(pVnode->pMeta, ver, &req));
24!
1844
_exit:
24✔
1845
  pRsp->msgType = TDMT_VND_CREATE_RSMA_RSP;
24✔
1846
  pRsp->pCont = NULL;
24✔
1847
  pRsp->code = code;
24✔
1848
  pRsp->contLen = 0;
24✔
1849
  tFreeSVCreateRsmaReq(&req);
24✔
1850
  return code;
24✔
1851
}
1852

1853
static int32_t vnodeProcessDropRsmaReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
6✔
1854
  int32_t       code = 0, lino = 0;
6✔
1855
  SVDropRsmaReq req = {0};
6✔
1856
  TAOS_CHECK_EXIT(tDeserializeSVDropRsmaReq(pReq, len, &req));
6!
1857
  TAOS_CHECK_EXIT(metaDropRsma(pVnode->pMeta, ver, &req));
6!
1858
_exit:
6✔
1859
  pRsp->msgType = TDMT_VND_DROP_RSMA_RSP;
6✔
1860
  pRsp->pCont = NULL;
6✔
1861
  pRsp->code = code;
6✔
1862
  pRsp->contLen = 0;
6✔
1863
  return 0;
6✔
1864
}
1865

1866
#ifdef BUILD_NO_CALL
1867
static int32_t vnodeDebugPrintSingleSubmitMsg(SMeta *pMeta, SSubmitBlk *pBlock, SSubmitMsgIter *msgIter,
1868
                                              const char *tags) {
1869
  SSubmitBlkIter blkIter = {0};
1870
  STSchema      *pSchema = NULL;
1871
  tb_uid_t       suid = 0;
1872
  STSRow        *row = NULL;
1873
  int32_t        rv = -1;
1874

1875
  tInitSubmitBlkIter(msgIter, pBlock, &blkIter);
1876
  if (blkIter.row == NULL) return 0;
1877

1878
  int32_t code = metaGetTbTSchemaNotNull(pMeta, msgIter->suid, TD_ROW_SVER(blkIter.row), 1,
1879
                                         &pSchema);  // TODO: use the real schema
1880
  if (TSDB_CODE_SUCCESS != code) {
1881
    printf("%s:%d no valid schema\n", tags, __LINE__);
1882
    return code;
1883
  }
1884

1885
  suid = msgIter->suid;
1886
  rv = TD_ROW_SVER(blkIter.row);
1887

1888
  char __tags[128] = {0};
1889
  snprintf(__tags, 128, "%s: uid %" PRIi64 " ", tags, msgIter->uid);
1890
  while ((row = tGetSubmitBlkNext(&blkIter))) {
1891
    tdSRowPrint(row, pSchema, __tags);
1892
  }
1893

1894
  taosMemoryFreeClear(pSchema);
1895

1896
  return TSDB_CODE_SUCCESS;
1897
}
1898
#endif
1899
typedef struct SSubmitReqConvertCxt {
1900
  SSubmitMsgIter msgIter;
1901
  SSubmitBlk    *pBlock;
1902
  SSubmitBlkIter blkIter;
1903
  STSRow        *pRow;
1904
  STSRowIter     rowIter;
1905
  SSubmitTbData *pTbData;
1906
  STSchema      *pTbSchema;
1907
  SArray        *pColValues;
1908
} SSubmitReqConvertCxt;
1909

1910
static int32_t vnodeResetTableCxt(SMeta *pMeta, SSubmitReqConvertCxt *pCxt) {
×
1911
  taosMemoryFreeClear(pCxt->pTbSchema);
×
1912
  int32_t code = metaGetTbTSchemaNotNull(pMeta, pCxt->msgIter.suid > 0 ? pCxt->msgIter.suid : pCxt->msgIter.uid,
×
1913
                                         pCxt->msgIter.sversion, 1, &pCxt->pTbSchema);
1914
  if (TSDB_CODE_SUCCESS != code) {
×
1915
    return code;
×
1916
  }
1917
  tdSTSRowIterInit(&pCxt->rowIter, pCxt->pTbSchema);
×
1918

1919
  tDestroySubmitTbData(pCxt->pTbData, TSDB_MSG_FLG_ENCODE);
×
1920
  if (NULL == pCxt->pTbData) {
×
1921
    pCxt->pTbData = taosMemoryCalloc(1, sizeof(SSubmitTbData));
×
1922
    if (NULL == pCxt->pTbData) {
×
1923
      return terrno;
×
1924
    }
1925
  }
1926
  pCxt->pTbData->flags = 0;
×
1927
  pCxt->pTbData->suid = pCxt->msgIter.suid;
×
1928
  pCxt->pTbData->uid = pCxt->msgIter.uid;
×
1929
  pCxt->pTbData->sver = pCxt->msgIter.sversion;
×
1930
  pCxt->pTbData->pCreateTbReq = NULL;
×
1931
  pCxt->pTbData->aRowP = taosArrayInit(128, POINTER_BYTES);
×
1932
  if (NULL == pCxt->pTbData->aRowP) {
×
1933
    return terrno;
×
1934
  }
1935

1936
  taosArrayDestroy(pCxt->pColValues);
×
1937
  pCxt->pColValues = taosArrayInit(pCxt->pTbSchema->numOfCols, sizeof(SColVal));
×
1938
  if (NULL == pCxt->pColValues) {
×
1939
    return terrno;
×
1940
  }
1941
  for (int32_t i = 0; i < pCxt->pTbSchema->numOfCols; ++i) {
×
1942
    SColVal val = COL_VAL_NONE(pCxt->pTbSchema->columns[i].colId, pCxt->pTbSchema->columns[i].type);
×
1943
    if (taosArrayPush(pCxt->pColValues, &val) == NULL) {
×
1944
      return terrno;
×
1945
    }
1946
  }
1947

1948
  return TSDB_CODE_SUCCESS;
×
1949
}
1950

1951
static void vnodeDestroySubmitReqConvertCxt(SSubmitReqConvertCxt *pCxt) {
×
1952
  taosMemoryFreeClear(pCxt->pTbSchema);
×
1953
  tDestroySubmitTbData(pCxt->pTbData, TSDB_MSG_FLG_ENCODE);
×
1954
  taosMemoryFreeClear(pCxt->pTbData);
×
1955
  taosArrayDestroy(pCxt->pColValues);
×
1956
}
×
1957

1958
static int32_t vnodeCellValConvertToColVal(STColumn *pCol, SCellVal *pCellVal, SColVal *pColVal) {
×
1959
  if (tdValTypeIsNone(pCellVal->valType)) {
×
1960
    pColVal->flag = CV_FLAG_NONE;
×
1961
    return TSDB_CODE_SUCCESS;
×
1962
  }
1963

1964
  if (tdValTypeIsNull(pCellVal->valType)) {
×
1965
    pColVal->flag = CV_FLAG_NULL;
×
1966
    return TSDB_CODE_SUCCESS;
×
1967
  }
1968

1969
  if (IS_VAR_DATA_TYPE(pCol->type)) {
×
1970
    if (IS_STR_DATA_BLOB(pCol->type)) {
×
1971
      pColVal->value.nData = blobDataLen(pCellVal->val);
×
1972
      pColVal->value.pData = (uint8_t *)blobDataVal(pCellVal->val);
×
1973

1974
    } else {
1975
      pColVal->value.nData = varDataLen(pCellVal->val);
×
1976
      pColVal->value.pData = (uint8_t *)varDataVal(pCellVal->val);
×
1977
    }
1978
  } else if (TSDB_DATA_TYPE_FLOAT == pCol->type) {
×
1979
    float f = GET_FLOAT_VAL(pCellVal->val);
×
1980
    valueSetDatum(&pColVal->value, pCol->type, &f, sizeof(f));
×
1981
  } else if (TSDB_DATA_TYPE_DOUBLE == pCol->type) {
×
1982
    taosSetPInt64Aligned(&pColVal->value.val, (int64_t *)pCellVal->val);
×
1983
  } else {
1984
    valueSetDatum(&pColVal->value, pCol->type, pCellVal->val, tDataTypes[pCol->type].bytes);
×
1985
  }
1986

1987
  pColVal->flag = CV_FLAG_VALUE;
×
1988
  return TSDB_CODE_SUCCESS;
×
1989
}
1990

1991
static int32_t vnodeTSRowConvertToColValArray(SSubmitReqConvertCxt *pCxt) {
×
1992
  int32_t code = TSDB_CODE_SUCCESS;
×
1993
  tdSTSRowIterReset(&pCxt->rowIter, pCxt->pRow);
×
1994
  for (int32_t i = 0; TSDB_CODE_SUCCESS == code && i < pCxt->pTbSchema->numOfCols; ++i) {
×
1995
    STColumn *pCol = pCxt->pTbSchema->columns + i;
×
1996
    SCellVal  cellVal = {0};
×
1997
    if (!tdSTSRowIterFetch(&pCxt->rowIter, pCol->colId, pCol->type, &cellVal)) {
×
1998
      break;
×
1999
    }
2000
    code = vnodeCellValConvertToColVal(pCol, &cellVal, (SColVal *)taosArrayGet(pCxt->pColValues, i));
×
2001
  }
2002
  return code;
×
2003
}
2004

2005
static int32_t vnodeDecodeCreateTbReq(SSubmitReqConvertCxt *pCxt) {
×
2006
  if (pCxt->msgIter.schemaLen <= 0) {
×
2007
    return TSDB_CODE_SUCCESS;
×
2008
  }
2009

2010
  pCxt->pTbData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
×
2011
  if (NULL == pCxt->pTbData->pCreateTbReq) {
×
2012
    return terrno;
×
2013
  }
2014

2015
  SDecoder decoder = {0};
×
2016
  tDecoderInit(&decoder, (uint8_t *)pCxt->pBlock->data, pCxt->msgIter.schemaLen);
×
2017
  int32_t code = tDecodeSVCreateTbReq(&decoder, pCxt->pTbData->pCreateTbReq);
×
2018
  tDecoderClear(&decoder);
×
2019

2020
  return code;
×
2021
}
2022

2023
static int32_t vnodeSubmitReqConvertToSubmitReq2(SVnode *pVnode, SSubmitReq *pReq, SSubmitReq2 *pReq2) {
×
2024
  pReq2->aSubmitTbData = taosArrayInit(128, sizeof(SSubmitTbData));
×
2025
  if (NULL == pReq2->aSubmitTbData) {
×
2026
    return terrno;
×
2027
  }
2028

2029
  SSubmitReqConvertCxt cxt = {0};
×
2030

2031
  int32_t code = tInitSubmitMsgIter(pReq, &cxt.msgIter);
×
2032
  while (TSDB_CODE_SUCCESS == code) {
×
2033
    code = tGetSubmitMsgNext(&cxt.msgIter, &cxt.pBlock);
×
2034
    if (TSDB_CODE_SUCCESS == code) {
×
2035
      if (NULL == cxt.pBlock) {
×
2036
        break;
×
2037
      }
2038
      code = vnodeResetTableCxt(pVnode->pMeta, &cxt);
×
2039
    }
2040
    if (TSDB_CODE_SUCCESS == code) {
×
2041
      code = tInitSubmitBlkIter(&cxt.msgIter, cxt.pBlock, &cxt.blkIter);
×
2042
    }
2043
    if (TSDB_CODE_SUCCESS == code) {
×
2044
      code = vnodeDecodeCreateTbReq(&cxt);
×
2045
    }
2046
    while (TSDB_CODE_SUCCESS == code && (cxt.pRow = tGetSubmitBlkNext(&cxt.blkIter)) != NULL) {
×
2047
      code = vnodeTSRowConvertToColValArray(&cxt);
×
2048
      if (TSDB_CODE_SUCCESS == code) {
×
2049
        SRow **pNewRow = taosArrayReserve(cxt.pTbData->aRowP, 1);
×
2050

2051
        SRowBuildScanInfo sinfo = {0};
×
2052
        code = tRowBuild(cxt.pColValues, cxt.pTbSchema, pNewRow, &sinfo);
×
2053
      }
2054
    }
2055
    if (TSDB_CODE_SUCCESS == code) {
×
2056
      code = (NULL == taosArrayPush(pReq2->aSubmitTbData, cxt.pTbData) ? terrno : TSDB_CODE_SUCCESS);
×
2057
    }
2058
    if (TSDB_CODE_SUCCESS == code) {
×
2059
      taosMemoryFreeClear(cxt.pTbData);
×
2060
    }
2061
  }
2062

2063
  vnodeDestroySubmitReqConvertCxt(&cxt);
×
2064
  return code;
×
2065
}
2066

2067
static int32_t vnodeRebuildSubmitReqMsg(SSubmitReq2 *pSubmitReq, void **ppMsg) {
×
2068
  int32_t  code = TSDB_CODE_SUCCESS;
×
2069
  char    *pMsg = NULL;
×
2070
  uint32_t msglen = 0;
×
2071
  tEncodeSize(tEncodeSubmitReq, pSubmitReq, msglen, code);
×
2072
  if (TSDB_CODE_SUCCESS == code) {
×
2073
    pMsg = taosMemoryMalloc(msglen);
×
2074
    if (NULL == pMsg) {
×
2075
      code = terrno;
×
2076
    }
2077
  }
2078
  if (TSDB_CODE_SUCCESS == code) {
×
2079
    SEncoder encoder;
2080
    tEncoderInit(&encoder, (uint8_t *)pMsg, msglen);
×
2081
    code = tEncodeSubmitReq(&encoder, pSubmitReq);
×
2082
    tEncoderClear(&encoder);
×
2083
  }
2084
  if (TSDB_CODE_SUCCESS == code) {
×
2085
    *ppMsg = pMsg;
×
2086
  }
2087
  return code;
×
2088
}
2089

2090
static int32_t buildExistSubTalbeRsp(SVnode *pVnode, SSubmitTbData *pSubmitTbData, STableMetaRsp **ppRsp) {
270✔
2091
  int32_t code = 0;
270✔
2092
  int32_t lino = 0;
270✔
2093

2094
  SMetaEntry *pEntry = NULL;
270✔
2095
  code = metaFetchEntryByUid(pVnode->pMeta, pSubmitTbData->suid, &pEntry);
270✔
2096
  if (code) {
270!
2097
    vError("vgId:%d, table uid:%" PRId64 " not exists, line:%d", TD_VID(pVnode), pSubmitTbData->uid, __LINE__);
×
2098
    TSDB_CHECK_CODE(code, lino, _exit);
×
2099
  }
2100
  if (pEntry->type != TSDB_SUPER_TABLE) {
270!
2101
    vError("vgId:%d, table uid:%" PRId64 " exists, but is not super table, line:%d", TD_VID(pVnode), pSubmitTbData->uid,
×
2102
           __LINE__);
2103
    code = TSDB_CODE_STREAM_INSERT_SCHEMA_NOT_MATCH;
×
2104
    TSDB_CHECK_CODE(code, lino, _exit);
×
2105
  }
2106

2107
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
270!
2108
  if (NULL == *ppRsp) {
270!
2109
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2110
  }
2111
  (*ppRsp)->suid = pSubmitTbData->suid;
270✔
2112
  (*ppRsp)->tuid = pSubmitTbData->uid;
270✔
2113
  (*ppRsp)->sversion = pEntry->stbEntry.schemaRow.version;
270✔
2114
  (*ppRsp)->vgId = pVnode->config.vgId;
270✔
2115
  (*ppRsp)->numOfColumns = pEntry->stbEntry.schemaRow.nCols;
270✔
2116
  (*ppRsp)->numOfTags = pEntry->stbEntry.schemaTag.nCols;
270✔
2117
  (*ppRsp)->pSchemas =
540✔
2118
      taosMemoryCalloc(pEntry->stbEntry.schemaRow.nCols + pEntry->stbEntry.schemaTag.nCols, sizeof(SSchema));
270!
2119
  if (NULL == (*ppRsp)->pSchemas) {
270!
2120
    taosMemoryFree(*ppRsp);
×
2121
    *ppRsp = NULL;
×
2122
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2123
  }
2124
  memcpy((*ppRsp)->pSchemas, pEntry->stbEntry.schemaRow.pSchema, pEntry->stbEntry.schemaRow.nCols * sizeof(SSchema));
270✔
2125
  memcpy((*ppRsp)->pSchemas + pEntry->stbEntry.schemaRow.nCols, pEntry->stbEntry.schemaTag.pSchema,
270✔
2126
         pEntry->stbEntry.schemaTag.nCols * sizeof(SSchema));
270✔
2127
  if (pEntry->pExtSchemas != NULL) {
270!
2128
    (*ppRsp)->pSchemaExt = taosMemoryCalloc(pEntry->colCmpr.nCols, sizeof(SExtSchema));
×
2129
    if (NULL == (*ppRsp)->pSchemaExt) {
×
2130
      taosMemoryFree((*ppRsp)->pSchemas);
×
2131
      taosMemoryFree(*ppRsp);
×
2132
      *ppRsp = NULL;
×
2133
      TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2134
    }
2135
    memcpy((*ppRsp)->pSchemaExt, pEntry->pExtSchemas, pEntry->colCmpr.nCols * sizeof(SExtSchema));
×
2136
  }
2137

2138
  if (pEntry->stbEntry.schemaRow.version != pSubmitTbData->sver) {
270✔
2139
    TSDB_CHECK_CODE(code = TSDB_CODE_TDB_TABLE_ALREADY_EXIST, lino, _exit);
2✔
2140
  }
2141
_exit:
268✔
2142
  metaFetchEntryFree(&pEntry);
270✔
2143
  if (code != TSDB_CODE_SUCCESS) {
270✔
2144
    vError("vgId:%d, failed to build exist sub table response, code:0x%0x, line:%d", TD_VID(pVnode), code, lino);
2!
2145
  }
2146
  return code;
270✔
2147
}
2148

2149
static int32_t buildExistNormalTalbeRsp(SVnode *pVnode, int64_t uid, STableMetaRsp **ppRsp) {
160✔
2150
  int32_t code = 0;
160✔
2151
  int32_t lino = 0;
160✔
2152

2153
  SMetaEntry *pEntry = NULL;
160✔
2154
  code = metaFetchEntryByUid(pVnode->pMeta, uid, &pEntry);
160✔
2155
  if (code) {
160!
2156
    vError("vgId:%d, table uid:%" PRId64 " not exists, line:%d", TD_VID(pVnode), uid, __LINE__);
×
2157
    TSDB_CHECK_CODE(code, lino, _exit);
×
2158
  }
2159
  if (pEntry->type != TSDB_NORMAL_TABLE) {
160!
2160
    vError("vgId:%d, table uid:%" PRId64 " exists, but is not normal table, line:%d", TD_VID(pVnode), uid, __LINE__);
×
2161
    code = TSDB_CODE_STREAM_INSERT_SCHEMA_NOT_MATCH;
×
2162
    TSDB_CHECK_CODE(code, lino, _exit);
×
2163
  }
2164

2165
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
160!
2166
  if (NULL == *ppRsp) {
160!
2167
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2168
  }
2169

2170
  (*ppRsp)->tuid = pEntry->uid;
160✔
2171
  (*ppRsp)->vgId = pVnode->config.vgId;
160✔
2172
  (*ppRsp)->sversion = pEntry->ntbEntry.schemaRow.version;
160✔
2173
  (*ppRsp)->numOfColumns = pEntry->ntbEntry.schemaRow.nCols;
160✔
2174
  (*ppRsp)->pSchemas = taosMemoryCalloc(pEntry->ntbEntry.schemaRow.nCols, sizeof(SSchema));
160!
2175
  if (NULL == (*ppRsp)->pSchemas) {
160!
2176
    taosMemoryFree(*ppRsp);
×
2177
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2178
  }
2179
  memcpy((*ppRsp)->pSchemas, pEntry->ntbEntry.schemaRow.pSchema, pEntry->ntbEntry.schemaRow.nCols * sizeof(SSchema));
160✔
2180
  if (pEntry->pExtSchemas != NULL) {
160!
2181
    (*ppRsp)->pSchemaExt = taosMemoryCalloc(pEntry->ntbEntry.schemaRow.nCols, sizeof(SSchemaExt));
×
2182
    if (NULL == (*ppRsp)->pSchemaExt) {
×
2183
      taosMemoryFree((*ppRsp)->pSchemas);
×
2184
      taosMemoryFree(*ppRsp);
×
2185
      TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2186
    }
2187
    memcpy((*ppRsp)->pSchemaExt, pEntry->pExtSchemas, pEntry->ntbEntry.schemaRow.nCols * sizeof(SSchemaExt));
×
2188
  }
2189

2190
_exit:
160✔
2191
  metaFetchEntryFree(&pEntry);
160✔
2192
  if (code != TSDB_CODE_SUCCESS) {
160!
2193
    vError("vgId:%d, failed to build exist normal table response, code:%d, line:%d", TD_VID(pVnode), code, lino);
×
2194
  }
2195
  return code;
160✔
2196
}
2197

2198
static int32_t buildExistTableInStreamRsp(SVnode *pVnode, SSubmitTbData *pSubmitTbData, STableMetaRsp **ppRsp) {
2,493✔
2199
  if (pSubmitTbData->pCreateTbReq->flags & TD_CREATE_NORMAL_TB_IN_STREAM) {
2,493✔
2200
    int32_t code = buildExistNormalTalbeRsp(pVnode, pSubmitTbData->uid, ppRsp);
160✔
2201
    if (code) {
160!
2202
      vError("vgId:%d, table uid:%" PRId64 " not exists, line:%d", TD_VID(pVnode), pSubmitTbData->uid, __LINE__);
×
2203
      return code;
×
2204
    }
2205
    return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
160✔
2206
  } else if (pSubmitTbData->pCreateTbReq->flags & TD_CREATE_SUB_TB_IN_STREAM) {
2,333✔
2207
    return buildExistSubTalbeRsp(pVnode, pSubmitTbData, ppRsp);
270✔
2208
  }
2209
  return TSDB_CODE_SUCCESS;
2,063✔
2210
}
2211

2212
static int32_t vnodeHandleAutoCreateTable(SVnode      *pVnode,    // vnode
24,118,072✔
2213
                                          int64_t      version,   // version
2214
                                          SSubmitReq2 *pRequest,  // request
2215
                                          SSubmitRsp2 *pResponse  // response
2216
) {
2217
  int32_t code = TSDB_CODE_SUCCESS;
24,118,072✔
2218
  int32_t numTbData = taosArrayGetSize(pRequest->aSubmitTbData);
24,118,072✔
2219
  SArray *newTbUids = NULL;
24,118,277✔
2220

2221
  for (int32_t i = 0; i < numTbData; ++i) {
48,306,884✔
2222
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
24,188,601✔
2223

2224
    if (pTbData->pCreateTbReq == NULL) {
24,188,784✔
2225
      continue;
24,127,536✔
2226
    }
2227

2228
    pTbData->uid = pTbData->pCreateTbReq->uid;
61,248✔
2229

2230
    // Alloc necessary resources
2231
    if (pResponse->aCreateTbRsp == NULL) {
61,248✔
2232
      pResponse->aCreateTbRsp = taosArrayInit(numTbData, sizeof(SVCreateTbRsp));
37,191✔
2233
      if (pResponse->aCreateTbRsp == NULL) {
37,191!
2234
        code = terrno;
×
2235
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2236
               tstrerror(code), version);
2237
        taosArrayDestroy(newTbUids);
×
2238
        return code;
×
2239
      }
2240
    }
2241

2242
    // Do create table
2243
    vDebug("vgId:%d start to handle auto create table, version:%" PRId64, TD_VID(pVnode), version);
61,248✔
2244

2245
    SVCreateTbRsp *pCreateTbRsp = taosArrayReserve(pResponse->aCreateTbRsp, 1);
61,252✔
2246
    code = metaCreateTable2(pVnode->pMeta, version, pTbData->pCreateTbReq, &pCreateTbRsp->pMeta);
61,279✔
2247
    if (code == TSDB_CODE_SUCCESS) {
61,275✔
2248
      // Allocate necessary resources
2249
      if (newTbUids == NULL) {
46,047✔
2250
        newTbUids = taosArrayInit(numTbData, sizeof(int64_t));
34,698✔
2251
        if (newTbUids == NULL) {
34,698!
2252
          code = terrno;
×
2253
          vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2254
                 tstrerror(code), version);
2255
          return code;
×
2256
        }
2257
      }
2258

2259
      if (taosArrayPush(newTbUids, &pTbData->uid) == NULL) {
92,094!
2260
        code = terrno;
×
2261
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2262
               tstrerror(code), version);
2263
        taosArrayDestroy(newTbUids);
×
2264
        return code;
×
2265
      }
2266

2267
      if (pCreateTbRsp->pMeta) {
46,047!
2268
        vnodeUpdateMetaRsp(pVnode, pCreateTbRsp->pMeta);
46,047✔
2269
      }
2270
    } else if (code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
15,228✔
2271
      code = terrno = 0;
15,226✔
2272
      pTbData->uid = pTbData->pCreateTbReq->uid;  // update uid if table exist for using below
15,223✔
2273

2274
      // stream: get sver from meta, write to pCreateTbRsp, and need to check crateTbReq is same as meta.
2275
      if (i == 0) {
15,223✔
2276
        // In the streaming scenario, multiple grouped req requests will only operate on the same write table, and
2277
        // only the first one needs to be processed.
2278
        code = buildExistTableInStreamRsp(pVnode, pTbData, &pCreateTbRsp->pMeta);
2,493✔
2279
        if (code) {
2,493✔
2280
          vInfo("vgId:%d failed to create table in stream:%s, code(0x%0x):%s", TD_VID(pVnode),
162!
2281
                pTbData->pCreateTbReq->name, code, tstrerror(code));
2282
          taosArrayDestroy(newTbUids);
162✔
2283
          return code;
162✔
2284
        }
2285
      }
2286
    } else {
2287
      code = terrno;
2✔
2288
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
2!
2289
             tstrerror(code), version);
2290
      taosArrayDestroy(newTbUids);
2✔
2291
      return code;
2✔
2292
    }
2293
  }
2294

2295
  // Update the affected table uid list
2296
  if (taosArrayGetSize(newTbUids) > 0) {
24,118,283✔
2297
    vDebug("vgId:%d, add %d table into query table list in handling submit", TD_VID(pVnode),
34,698✔
2298
           (int32_t)taosArrayGetSize(newTbUids));
2299
    if (tqUpdateTbUidList(pVnode->pTq, newTbUids, true) != 0) {
34,698!
2300
      vError("vgId:%d, failed to update tbUid list", TD_VID(pVnode));
×
2301
    }
2302
  }
2303

2304
  vDebug("vgId:%d, handle auto create table done, version:%" PRId64, TD_VID(pVnode), version);
24,118,075✔
2305

2306
  taosArrayDestroy(newTbUids);
24,118,077✔
2307
  return code;
24,117,895✔
2308
}
2309

2310
static void addExistTableInfoIntoRes(SVnode *pVnode, SSubmitReq2 *pRequest, SSubmitRsp2 *pResponse,
2✔
2311
                                     SSubmitTbData *pTbData, int32_t numTbData) {
2312
  int32_t code = 0;
2✔
2313
  int32_t lino = 0;
2✔
2314
  if ((pTbData->flags & SUBMIT_REQ_SCHEMA_RES) == 0) {
2!
2315
    return;
×
2316
  }
2317
  if (pResponse->aCreateTbRsp) {  // If aSubmitTbData is not NULL, it means that the request is a create table request,
2!
2318
                                  // so table info has exitst and we do not need to add again.
2319
    return;
2✔
2320
  }
2321
  pResponse->aCreateTbRsp = taosArrayInit(numTbData, sizeof(SVCreateTbRsp));
×
2322
  if (pResponse->aCreateTbRsp == NULL) {
×
2323
    code = terrno;
×
2324
    TSDB_CHECK_CODE(code, lino, _exit);
×
2325
  }
2326
  SVCreateTbRsp *pCreateTbRsp = taosArrayReserve(pResponse->aCreateTbRsp, 1);
×
2327
  if (pCreateTbRsp == NULL) {
×
2328
    code = terrno;
×
2329
    TSDB_CHECK_CODE(code, lino, _exit);
×
2330
  }
2331
  if (pTbData->suid == 0) {
×
2332
    code = buildExistNormalTalbeRsp(pVnode, pTbData->uid, &pCreateTbRsp->pMeta);
×
2333
    if (code) {
×
2334
      vError("vgId:%d, table uid:%" PRId64 " not exists, line:%d", TD_VID(pVnode), pTbData->uid, __LINE__);
×
2335
    }
2336
  } else {
2337
    code = buildExistSubTalbeRsp(pVnode, pTbData, &pCreateTbRsp->pMeta);
×
2338
  }
2339

2340
  TSDB_CHECK_CODE(code, lino, _exit);
×
2341
_exit:
×
2342
  if (code != TSDB_CODE_SUCCESS) {
×
2343
    vError("vgId:%d, failed to add exist table info into response, code:0x%0x, line:%d", TD_VID(pVnode), code, lino);
×
2344
  }
2345
  return;
×
2346
}
2347

2348
static int32_t vnodeHandleDataWrite(SVnode *pVnode, int64_t version, SSubmitReq2 *pRequest, SSubmitRsp2 *pResponse) {
24,117,844✔
2349
  int32_t code = TSDB_CODE_SUCCESS;
24,117,844✔
2350
  int32_t numTbData = taosArrayGetSize(pRequest->aSubmitTbData);
24,117,844✔
2351
  int8_t  hasBlob = 0;
24,118,053✔
2352

2353
  // Scan submit data
2354
  for (int32_t i = 0; i < numTbData; ++i) {
48,306,756✔
2355
    SMetaInfo      info = {0};
24,188,423✔
2356
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
24,188,423✔
2357

2358
    if (pTbData->flags & SUBMIT_REQ_WITH_BLOB) {
24,188,587!
2359
      hasBlob = 1;
×
2360
    }
2361
    if (pTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
24,188,587✔
2362
      continue;  // skip column data format
1,159✔
2363
    }
2364

2365
    code = metaGetInfo(pVnode->pMeta, pTbData->uid, &info, NULL);
24,187,428✔
2366
    if (code) {
24,187,479✔
2367
      code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
24✔
2368
      vWarn("vgId:%d, error occurred at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __FILE__,
24✔
2369
            __LINE__, tstrerror(code), version, pTbData->uid);
2370
      return code;
7✔
2371
    }
2372

2373
    if (info.suid != pTbData->suid) {
24,187,455!
2374
      code = TSDB_CODE_INVALID_MSG;
×
2375
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64 " suid:%" PRId64
×
2376
             " info.suid:%" PRId64,
2377
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), version, pTbData->uid, pTbData->suid,
2378
             info.suid);
2379
      return code;
×
2380
    }
2381

2382
    if (info.suid) {
24,187,455✔
2383
      code = metaGetInfo(pVnode->pMeta, info.suid, &info, NULL);
23,498,979✔
2384
      if (code) {
23,499,086✔
2385
        code = TSDB_CODE_INTERNAL_ERROR;
16✔
2386
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " suid:%" PRId64, TD_VID(pVnode), __func__,
16!
2387
               __FILE__, __LINE__, tstrerror(code), version, info.suid);
2388
        return code;
×
2389
      }
2390
    }
2391

2392
    if (pTbData->sver != info.skmVer) {
24,187,546✔
2393
      code = TSDB_CODE_TDB_INVALID_TABLE_SCHEMA_VER;
2✔
2394
      addExistTableInfoIntoRes(pVnode, pRequest, pResponse, pTbData, numTbData);
2✔
2395
      vDebug("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64
2!
2396
             " sver:%d"
2397
             " info.skmVer:%d",
2398
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), version, pTbData->uid, pTbData->sver,
2399
             info.skmVer);
2400
      return code;
2✔
2401
    }
2402
  }
2403

2404
  // Do write data
2405
  vDebug("vgId:%d start to handle data write, version:%" PRId64, TD_VID(pVnode), version);
24,118,333✔
2406

2407
  for (int32_t i = 0; i < numTbData; ++i) {
48,306,919✔
2408
    int32_t        affectedRows = 0;
24,188,595✔
2409
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
24,188,595✔
2410

2411
    if (hasBlob) {
24,188,614!
2412
      code = vnodeSubmitBlobData(pVnode, pTbData);
×
2413
      if (code) {
×
2414
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2415
               __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2416
        return code;
×
2417
      }
2418
    }
2419

2420
    code = tsdbInsertTableData(pVnode->pTsdb, version, pTbData, &affectedRows);
24,188,614✔
2421
    if (code) {
24,188,700!
2422
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2423
             __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2424
      return code;
×
2425
    }
2426

2427
    code = metaUpdateChangeTimeWithLock(pVnode->pMeta, pTbData->uid, pTbData->ctimeMs);
24,188,700✔
2428
    if (code) {
24,188,696!
2429
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2430
             __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2431
      return code;
×
2432
    }
2433
    pResponse->affectedRows += affectedRows;
24,188,729✔
2434
  }
2435

2436
  vDebug("vgId:%d, handle data write done, version:%" PRId64 ", affectedRows:%d", TD_VID(pVnode), version,
24,118,324✔
2437
         pResponse->affectedRows);
2438
  return code;
24,118,230✔
2439
}
2440

2441
static int32_t vnodeScanColumnData(SVnode *pVnode, SSubmitTbData *pTbData, TSKEY minKey, TSKEY maxKey) {
1,159✔
2442
  int32_t code = 0;
1,159✔
2443

2444
  int32_t   numCols = taosArrayGetSize(pTbData->aCol);
1,159✔
2445
  SColData *aColData = (SColData *)TARRAY_DATA(pTbData->aCol);
1,159✔
2446

2447
  if (numCols <= 0) {
1,159!
2448
    code = TSDB_CODE_INVALID_MSG;
×
2449
    vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " numCols:%d", TD_VID(pVnode), __func__,
×
2450
           __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, numCols);
2451
    return code;
×
2452
  }
2453

2454
  if (aColData[0].cid != PRIMARYKEY_TIMESTAMP_COL_ID || aColData[0].type != TSDB_DATA_TYPE_TIMESTAMP ||
1,159!
2455
      aColData[0].nVal <= 0) {
1,159!
2456
    code = TSDB_CODE_INVALID_MSG;
×
2457
    vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64
×
2458
           " first column is not primary key timestamp, cid:%d type:%d nVal:%d",
2459
           TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, aColData[0].cid,
2460
           aColData[0].type, aColData[0].nVal);
2461
    return code;
×
2462
  }
2463

2464
  for (int32_t i = 1; i < numCols; ++i) {
7,297✔
2465
    if (aColData[i].nVal != aColData[0].nVal) {
6,138!
2466
      code = TSDB_CODE_INVALID_MSG;
×
2467
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64
×
2468
             " column cid:%d type:%d nVal:%d is not equal to primary key timestamp nVal:%d",
2469
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid,
2470
             aColData[i].cid, aColData[i].type, aColData[i].nVal, aColData[0].nVal);
2471
      return code;
×
2472
    }
2473
  }
2474

2475
  SRowKey *pLastKey = NULL;
1,159✔
2476
  SRowKey  lastKey = {0};
1,159✔
2477
  for (int32_t i = 0; i < aColData[0].nVal; ++i) {
74,426✔
2478
    SRowKey key = {0};
73,270✔
2479

2480
    tColDataArrGetRowKey(aColData, numCols, i, &key);
73,270✔
2481

2482
    if (key.ts < minKey || key.ts > maxKey) {
73,262!
2483
      code = TSDB_CODE_INVALID_MSG;
×
2484
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] key:%" PRId64
×
2485
             " is out of range [%" PRId64 ", %" PRId64 "]",
2486
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, i, key.ts,
2487
             minKey, maxKey);
2488
      return code;
×
2489
    }
2490

2491
    if (pLastKey && tRowKeyCompare(pLastKey, &key) >= 0) {
73,263!
2492
      code = TSDB_CODE_INVALID_MSG;
×
2493
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] key:%" PRId64
×
2494
             " is not in order, lastKey:%" PRId64,
2495
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, i, key.ts,
2496
             pLastKey->ts);
2497
      return code;
×
2498
    } else if (pLastKey == NULL) {
73,267✔
2499
      pLastKey = &lastKey;
1,157✔
2500
    }
2501

2502
    *pLastKey = key;
73,267✔
2503
  }
2504

2505
  return code;
1,156✔
2506
}
2507

2508
static int32_t vnodeScanSubmitRowData(SVnode *pVnode, SSubmitTbData *pTbData, TSKEY minKey, TSKEY maxKey) {
24,187,238✔
2509
  int32_t code = 0;
24,187,238✔
2510

2511
  int32_t numRows = taosArrayGetSize(pTbData->aRowP);
24,187,238✔
2512
  SRow  **aRow = (SRow **)TARRAY_DATA(pTbData->aRowP);
24,187,327✔
2513

2514
  if (numRows <= 0) {
24,187,327✔
2515
    code = TSDB_CODE_INVALID_MSG;
425✔
2516
    vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " numRows:%d", TD_VID(pVnode), __func__,
425!
2517
           __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, numRows);
2518
    return code;
×
2519
  }
2520

2521
  SRowKey *pLastKey = NULL;
24,186,902✔
2522
  SRowKey  lastKey = {0};
24,186,902✔
2523
  for (int32_t i = 0; i < numRows; ++i) {
1,522,027,131✔
2524
    SRow *pRow = aRow[i];
1,497,805,366✔
2525
    if (pRow->sver != pTbData->sver) {
1,497,805,366!
2526
      code = TSDB_CODE_INVALID_MSG;
×
2527
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] sver:%d pTbData->sver:%d",
×
2528
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, i, pRow->sver,
2529
             pTbData->sver);
2530
      return code;
×
2531
    }
2532

2533
    SRowKey key = {0};
1,497,805,366✔
2534
    tRowGetKey(pRow, &key);
2,147,483,647✔
2535
    if (key.ts < minKey || key.ts > maxKey) {
1,497,797,781!
2536
      code = TSDB_CODE_INVALID_MSG;
×
2537
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] key:%" PRId64
×
2538
             " is out of range [%" PRId64 ", %" PRId64 "]",
2539
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, i, key.ts,
2540
             minKey, maxKey);
2541
      return code;
×
2542
    }
2543

2544
    if (pLastKey && tRowKeyCompare(pLastKey, &key) >= 0) {
1,497,845,352!
2545
      code = TSDB_CODE_INVALID_MSG;
×
2546
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] key:%" PRId64
×
2547
             " is not in order, lastKey:%" PRId64,
2548
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, i, key.ts,
2549
             pLastKey->ts);
2550
      return code;
×
2551
    } else if (pLastKey == NULL) {
1,497,840,229✔
2552
      pLastKey = &lastKey;
24,187,014✔
2553
    }
2554

2555
    *pLastKey = key;
1,497,840,229✔
2556
  }
2557

2558
  return code;
24,221,765✔
2559
}
2560

2561
static int32_t vnodeScanSubmitReq(SVnode *pVnode, int64_t version, SSubmitReq2 *pRequest, SSubmitRsp2 *pResponse) {
24,118,111✔
2562
  int32_t code = TSDB_CODE_SUCCESS;
24,118,111✔
2563
  int32_t numTbData = taosArrayGetSize(pRequest->aSubmitTbData);
24,118,111✔
2564

2565
  TSKEY now = taosGetTimestamp(pVnode->config.tsdbCfg.precision);
24,118,201✔
2566
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * pVnode->config.tsdbCfg.keep2;
24,118,303✔
2567
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
24,118,303✔
2568
  for (int32_t i = 0; i < numTbData; i++) {
48,307,110✔
2569
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
24,188,550✔
2570

2571
    if (pTbData->pCreateTbReq && pTbData->pCreateTbReq->uid == 0) {
24,188,575!
2572
      code = TSDB_CODE_INVALID_MSG;
×
2573
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2574
             tstrerror(code), version);
2575
      return code;
×
2576
    }
2577

2578
    if (pTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
24,188,575✔
2579
      code = vnodeScanColumnData(pVnode, pTbData, minKey, maxKey);
1,159✔
2580
      if (code) {
1,159!
2581
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2582
               __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2583
        return code;
×
2584
      }
2585
    } else {
2586
      code = vnodeScanSubmitRowData(pVnode, pTbData, minKey, maxKey);
24,187,416✔
2587
      if (code) {
24,187,592!
2588
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2589
               __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2590
        return code;
×
2591
      }
2592
    }
2593
  }
2594

2595
  return code;
24,118,560✔
2596
}
2597

2598
static int32_t vnodeProcessSubmitReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
24,118,114✔
2599
                                     SRpcMsg *pOriginalMsg) {
2600
  int32_t code = 0;
24,118,114✔
2601
  int32_t lino = 0;
24,118,114✔
2602
  terrno = 0;
24,118,114✔
2603

2604
  SSubmitReq2 *pSubmitReq = &(SSubmitReq2){0};
24,118,334✔
2605
  SSubmitRsp2 *pSubmitRsp = &(SSubmitRsp2){0};
24,118,334✔
2606
  int32_t      ret;
2607
  SEncoder     ec = {0};
24,118,334✔
2608

2609
  pRsp->code = TSDB_CODE_SUCCESS;
24,118,334✔
2610

2611
  void           *pAllocMsg = NULL;
24,118,334✔
2612
  SSubmitReq2Msg *pMsg = (SSubmitReq2Msg *)pReq;
24,118,334✔
2613
  SDecoder        dc = {0};
24,118,334✔
2614
  if (0 == taosHton64(pMsg->version)) {
24,118,334!
2615
    code = vnodeSubmitReqConvertToSubmitReq2(pVnode, (SSubmitReq *)pMsg, pSubmitReq);
×
2616
    if (TSDB_CODE_SUCCESS == code) {
×
2617
      code = vnodeRebuildSubmitReqMsg(pSubmitReq, &pReq);
×
2618
    }
2619
    if (TSDB_CODE_SUCCESS == code) {
×
2620
      pAllocMsg = pReq;
×
2621
    }
2622
    TSDB_CHECK_CODE(code, lino, _exit);
×
2623
  } else {
2624
    // decode
2625
    pReq = POINTER_SHIFT(pReq, sizeof(SSubmitReq2Msg));
24,118,129✔
2626
    len -= sizeof(SSubmitReq2Msg);
24,118,129✔
2627

2628
    tDecoderInit(&dc, pReq, len);
24,118,129✔
2629
    if (tDecodeSubmitReq(&dc, pSubmitReq, NULL) < 0) {
24,118,075!
2630
      code = TSDB_CODE_INVALID_MSG;
×
2631
      TSDB_CHECK_CODE(code, lino, _exit);
×
2632
    }
2633
  }
2634

2635
  // Scan the request
2636
  code = vnodeScanSubmitReq(pVnode, ver, pSubmitReq, pSubmitRsp);
24,118,342✔
2637
  if (code) {
24,118,176!
2638
    vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2639
           tstrerror(code), ver);
2640
    TSDB_CHECK_CODE(code, lino, _exit);
×
2641
  }
2642

2643
  vGDebug(pOriginalMsg ? &pOriginalMsg->info.traceId : NULL, "vgId:%d, index:%" PRId64 ", submit block, rows:%d",
24,118,176!
2644
          TD_VID(pVnode), ver, (int32_t)taosArrayGetSize(pSubmitReq->aSubmitTbData));
2645

2646
  // Handle auto create table
2647
  code = vnodeHandleAutoCreateTable(pVnode, ver, pSubmitReq, pSubmitRsp);
24,118,179✔
2648
  if (code) {
24,118,009✔
2649
    vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
164!
2650
           tstrerror(code), ver);
2651
    TSDB_CHECK_CODE(code, lino, _exit);
164!
2652
  }
2653

2654
  // Handle data write
2655
  code = vnodeHandleDataWrite(pVnode, ver, pSubmitReq, pSubmitRsp);
24,117,845✔
2656
  if (code) {
24,118,217✔
2657
    vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
7!
2658
           tstrerror(code), ver);
2659
    TSDB_CHECK_CODE(code, lino, _exit);
7!
2660
  }
2661

2662
_exit:
24,118,210✔
2663
  // message
2664
  pRsp->code = code;
24,118,381✔
2665
  tEncodeSize(tEncodeSSubmitRsp2, pSubmitRsp, pRsp->contLen, ret);
24,118,381!
2666
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
24,118,229✔
2667
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
24,118,372✔
2668
  if (tEncodeSSubmitRsp2(&ec, pSubmitRsp) < 0) {
24,118,387!
2669
    vError("vgId:%d, failed to encode submit response", TD_VID(pVnode));
×
2670
  }
2671
  tEncoderClear(&ec);
24,118,373✔
2672

2673
  // update statistics
2674
  (void)atomic_add_fetch_64(&pVnode->statis.nInsert, pSubmitRsp->affectedRows);
24,118,408✔
2675
  (void)atomic_add_fetch_64(&pVnode->statis.nInsertSuccess, pSubmitRsp->affectedRows);
24,118,428✔
2676
  (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsert, 1);
24,118,443✔
2677

2678
  // update metrics
2679
  METRICS_UPDATE(pVnode->writeMetrics.total_requests, METRIC_LEVEL_LOW, 1);
24,118,431!
2680
  METRICS_UPDATE(pVnode->writeMetrics.total_rows, METRIC_LEVEL_HIGH, pSubmitRsp->affectedRows);
24,118,431!
2681
  METRICS_UPDATE(pVnode->writeMetrics.total_bytes, METRIC_LEVEL_LOW, pMsg->header.contLen);
24,118,431!
2682

2683
  if (tsEnableMonitor && tsMonitorFqdn[0] != 0 && tsMonitorPort != 0 && pSubmitRsp->affectedRows > 0 &&
24,118,431!
2684
      strlen(pOriginalMsg->info.conn.user) > 0 && tsInsertCounter != NULL) {
×
2685
    const char *sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT_AFFECTED_ROWS,
×
2686
                                   pVnode->monitor.strClusterId,
×
2687
                                   pVnode->monitor.strDnodeId,
×
2688
                                   tsLocalEp,
2689
                                   pVnode->monitor.strVgId,
×
2690
                                   pOriginalMsg->info.conn.user,
×
2691
                                   "Success"};
2692
    int         tv = taos_counter_add(tsInsertCounter, pSubmitRsp->affectedRows, sample_labels);
×
2693
  }
2694

2695
  if (code == 0) {
24,118,431✔
2696
    (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
24,118,118✔
2697
  }
2698

2699
  // clear
2700
  tDestroySubmitReq(pSubmitReq, 0 == taosHton64(pMsg->version) ? TSDB_MSG_FLG_CMPT : TSDB_MSG_FLG_DECODE);
24,118,600!
2701
  tDestroySSubmitRsp2(pSubmitRsp, TSDB_MSG_FLG_ENCODE);
24,118,413✔
2702

2703
  if (code) {
24,118,270✔
2704
    terrno = code;
171✔
2705
    if (code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
171✔
2706
      vInfo("vgId:%d, failed to process submit request since %s, lino:%d, version:%" PRId64, TD_VID(pVnode),
162!
2707
            tstrerror(code), lino, ver);
2708

2709
    } else {
2710
      vError("vgId:%d, failed to process submit request since %s, lino:%d, version:%" PRId64, TD_VID(pVnode),
9!
2711
             tstrerror(code), lino, ver);
2712
    }
2713
  }
2714

2715
  taosMemoryFree(pAllocMsg);
24,118,190!
2716
  tDecoderClear(&dc);
24,118,267✔
2717

2718
  return code;
24,118,423✔
2719
}
2720

2721
static int32_t vnodeConsolidateAlterHashRange(SVnode *pVnode, int64_t ver) {
88✔
2722
  int32_t code = TSDB_CODE_SUCCESS;
88✔
2723

2724
  vInfo("vgId:%d, trim meta of tables per hash range [%" PRIu32 ", %" PRIu32 "]. apply-index:%" PRId64, TD_VID(pVnode),
88!
2725
        pVnode->config.hashBegin, pVnode->config.hashEnd, ver);
2726

2727
  // TODO: trim meta of tables from TDB per hash range [pVnode->config.hashBegin, pVnode->config.hashEnd]
2728
  code = metaTrimTables(pVnode->pMeta, ver);
88✔
2729

2730
  return code;
88✔
2731
}
2732

2733
static int32_t vnodeProcessAlterConfirmReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
8,498✔
2734
  vInfo("vgId:%d, vnode handle msgType:alter-confirm, alter confirm msg is processed", TD_VID(pVnode));
8,498!
2735
  int32_t code = TSDB_CODE_SUCCESS;
8,498✔
2736
  if (!pVnode->config.hashChange) {
8,498✔
2737
    goto _exit;
8,410✔
2738
  }
2739

2740
  code = vnodeConsolidateAlterHashRange(pVnode, ver);
88✔
2741
  if (code < 0) {
88!
2742
    vError("vgId:%d, failed to consolidate alter hashrange since %s. version:%" PRId64, TD_VID(pVnode), terrstr(), ver);
×
2743
    goto _exit;
×
2744
  }
2745
  pVnode->config.hashChange = false;
88✔
2746

2747
_exit:
8,498✔
2748
  pRsp->msgType = TDMT_VND_ALTER_CONFIRM_RSP;
8,498✔
2749
  pRsp->code = code;
8,498✔
2750
  pRsp->pCont = NULL;
8,498✔
2751
  pRsp->contLen = 0;
8,498✔
2752

2753
  return code;
8,498✔
2754
}
2755

2756
extern int32_t tsdbDisableAndCancelAllBgTask(STsdb *pTsdb);
2757
extern void    tsdbEnableBgTask(STsdb *pTsdb);
2758

2759
static int32_t vnodeProcessAlterConfigReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
1,012✔
2760
  bool walChanged = false;
1,012✔
2761
  bool tsdbChanged = false;
1,012✔
2762

2763
  SAlterVnodeConfigReq req = {0};
1,012✔
2764
  if (tDeserializeSAlterVnodeConfigReq(pReq, len, &req) != 0) {
1,012!
2765
    terrno = TSDB_CODE_INVALID_MSG;
×
2766
    return TSDB_CODE_INVALID_MSG;
×
2767
  }
2768

2769
  vInfo("vgId:%d, start to alter vnode config, page:%d pageSize:%d buffer:%d szPage:%d szBuf:%" PRIu64
1,012!
2770
        " cacheLast:%d cacheLastSize:%d days:%d keep0:%d keep1:%d keep2:%d keepTimeOffset:%d ssKeepLocal:%d "
2771
        "ssCompact:%d fsync:%d level:%d "
2772
        "walRetentionPeriod:%d walRetentionSize:%d",
2773
        TD_VID(pVnode), req.pages, req.pageSize, req.buffer, req.pageSize * 1024, (uint64_t)req.buffer * 1024 * 1024,
2774
        req.cacheLast, req.cacheLastSize, req.daysPerFile, req.daysToKeep0, req.daysToKeep1, req.daysToKeep2,
2775
        req.keepTimeOffset, req.ssKeepLocal, req.ssCompact, req.walFsyncPeriod, req.walLevel, req.walRetentionPeriod,
2776
        req.walRetentionSize);
2777

2778
  if (pVnode->config.cacheLastSize != req.cacheLastSize) {
1,012✔
2779
    pVnode->config.cacheLastSize = req.cacheLastSize;
497✔
2780
    tsdbCacheSetCapacity(pVnode, (size_t)pVnode->config.cacheLastSize * 1024 * 1024);
497✔
2781
  }
2782

2783
  if (pVnode->config.szBuf != req.buffer * 1024LL * 1024LL) {
1,012✔
2784
    vInfo("vgId:%d, vnode buffer is changed from %" PRId64 " to %" PRId64, TD_VID(pVnode), pVnode->config.szBuf,
14!
2785
          (uint64_t)(req.buffer * 1024LL * 1024LL));
2786
    pVnode->config.szBuf = req.buffer * 1024LL * 1024LL;
14✔
2787
  }
2788

2789
  if (pVnode->config.szCache != req.pages) {
1,012✔
2790
    if ((terrno = metaAlterCache(pVnode->pMeta, req.pages)) < 0) {
6!
2791
      vError("vgId:%d, failed to change vnode pages from %d to %d failed since %s", TD_VID(pVnode),
×
2792
             pVnode->config.szCache, req.pages, tstrerror(terrno));
2793
      return terrno;
×
2794
    } else {
2795
      vInfo("vgId:%d, vnode pages is changed from %d to %d", TD_VID(pVnode), pVnode->config.szCache, req.pages);
6!
2796
      pVnode->config.szCache = req.pages;
6✔
2797
    }
2798
  }
2799

2800
  if (pVnode->config.cacheLast != req.cacheLast) {
1,012✔
2801
    pVnode->config.cacheLast = req.cacheLast;
622✔
2802
  }
2803

2804
  if (pVnode->config.walCfg.fsyncPeriod != req.walFsyncPeriod) {
1,012✔
2805
    pVnode->config.walCfg.fsyncPeriod = req.walFsyncPeriod;
72✔
2806
    walChanged = true;
72✔
2807
  }
2808

2809
  if (pVnode->config.walCfg.level != req.walLevel) {
1,012✔
2810
    if (pVnode->config.walCfg.level == 0) {
50!
2811
      pVnode->config.walCfg.clearFiles = 1;
×
2812
    }
2813
    pVnode->config.walCfg.level = req.walLevel;
50✔
2814
    walChanged = true;
50✔
2815
  }
2816

2817
  if (pVnode->config.walCfg.retentionPeriod != req.walRetentionPeriod) {
1,012✔
2818
    pVnode->config.walCfg.retentionPeriod = req.walRetentionPeriod;
124✔
2819
    walChanged = true;
124✔
2820
  }
2821

2822
  if (pVnode->config.walCfg.retentionSize != req.walRetentionSize) {
1,012✔
2823
    pVnode->config.walCfg.retentionSize = req.walRetentionSize;
2✔
2824
    walChanged = true;
2✔
2825
  }
2826

2827
  if (pVnode->config.tsdbCfg.keep0 != req.daysToKeep0) {
1,012✔
2828
    pVnode->config.tsdbCfg.keep0 = req.daysToKeep0;
74✔
2829
    tsdbChanged = true;
74✔
2830
  }
2831

2832
  if (pVnode->config.tsdbCfg.keep1 != req.daysToKeep1) {
1,012✔
2833
    pVnode->config.tsdbCfg.keep1 = req.daysToKeep1;
74✔
2834
    tsdbChanged = true;
74✔
2835
  }
2836

2837
  if (pVnode->config.tsdbCfg.keep2 != req.daysToKeep2) {
1,012✔
2838
    pVnode->config.tsdbCfg.keep2 = req.daysToKeep2;
68✔
2839
    tsdbChanged = true;
68✔
2840
  }
2841

2842
  if (pVnode->config.tsdbCfg.keepTimeOffset != req.keepTimeOffset) {
1,012✔
2843
    pVnode->config.tsdbCfg.keepTimeOffset = req.keepTimeOffset;
10✔
2844
    tsdbChanged = true;
10✔
2845
  }
2846

2847
  if (req.sttTrigger != -1 && req.sttTrigger != pVnode->config.sttTrigger) {
1,012!
2848
    if (req.sttTrigger > 1 && pVnode->config.sttTrigger > 1) {
4!
2849
      pVnode->config.sttTrigger = req.sttTrigger;
2✔
2850
    } else {
2851
      vnodeAWait(&pVnode->commitTask);
2✔
2852

2853
      int32_t ret = tsdbDisableAndCancelAllBgTask(pVnode->pTsdb);
2✔
2854
      if (ret != 0) {
2!
2855
        vError("vgId:%d, failed to disable bg task since %s", TD_VID(pVnode), tstrerror(ERRNO));
×
2856
      }
2857

2858
      pVnode->config.sttTrigger = req.sttTrigger;
2✔
2859
      tsdbEnableBgTask(pVnode->pTsdb);
2✔
2860
    }
2861
  }
2862

2863
  if (req.minRows != -1 && req.minRows != pVnode->config.tsdbCfg.minRows) {
1,012!
2864
    pVnode->config.tsdbCfg.minRows = req.minRows;
2✔
2865
  }
2866

2867
  if (req.ssKeepLocal != -1 && req.ssKeepLocal != pVnode->config.ssKeepLocal) {
1,012!
2868
    pVnode->config.ssKeepLocal = req.ssKeepLocal;
2✔
2869
  }
2870
  if (req.ssCompact != -1 && req.ssCompact != pVnode->config.ssCompact) {
1,012!
2871
    pVnode->config.ssCompact = req.ssCompact;
2✔
2872
  }
2873

2874
  if (walChanged) {
1,012✔
2875
    if (walAlter(pVnode->pWal, &pVnode->config.walCfg) != 0) {
244!
2876
      vError("vgId:%d, failed to alter wal config since %s", TD_VID(pVnode), tstrerror(ERRNO));
×
2877
    }
2878
  }
2879

2880
  if (tsdbChanged) {
1,012✔
2881
    tsdbSetKeepCfg(pVnode->pTsdb, &pVnode->config.tsdbCfg);
92✔
2882
  }
2883

2884
  return 0;
1,012✔
2885
}
2886

2887
static int32_t vnodeProcessBatchDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
2888
  SBatchDeleteReq deleteReq;
2889
  SDecoder        decoder;
2890
  tDecoderInit(&decoder, pReq, len);
×
2891
  if (tDecodeSBatchDeleteReq(&decoder, &deleteReq) < 0) {
×
2892
    tDecoderClear(&decoder);
×
2893
    return terrno = TSDB_CODE_INVALID_MSG;
×
2894
  }
2895

2896
  SMetaReader mr = {0};
×
2897
  metaReaderDoInit(&mr, pVnode->pMeta, META_READER_NOLOCK);
×
2898
  STsdb *pTsdb = pVnode->pTsdb;
×
2899

2900
  int32_t sz = taosArrayGetSize(deleteReq.deleteReqs);
×
2901
  for (int32_t i = 0; i < sz; i++) {
×
2902
    SSingleDeleteReq *pOneReq = taosArrayGet(deleteReq.deleteReqs, i);
×
2903
    char             *name = pOneReq->tbname;
×
2904
    if (metaGetTableEntryByName(&mr, name) < 0) {
×
2905
      vDebug("vgId:%d, stream delete msg, skip since no table: %s", pVnode->config.vgId, name);
×
2906
      continue;
×
2907
    }
2908

2909
    int64_t uid = mr.me.uid;
×
2910

2911
    int32_t code = tsdbDeleteTableData(pTsdb, ver, deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
×
2912
    if (code < 0) {
×
2913
      terrno = code;
×
2914
      vError("vgId:%d, delete error since %s, suid:%" PRId64 ", uid:%" PRId64 ", start ts:%" PRId64 ", end ts:%" PRId64,
×
2915
             TD_VID(pVnode), terrstr(), deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
2916
    }
2917

2918
    if (deleteReq.level == 0) {
×
2919
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, deleteReq.ctimeMs);
×
2920
      if (code < 0) {
×
2921
        terrno = code;
×
2922
        vError("vgId:%d, update change time error since %s, suid:%" PRId64 ", uid:%" PRId64 ", start ts:%" PRId64
×
2923
               ", end ts:%" PRId64,
2924
               TD_VID(pVnode), terrstr(), deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
2925
      }
2926
    }
2927
    tDecoderClear(&mr.coder);
×
2928
  }
2929
  metaReaderClear(&mr);
×
2930
  taosArrayDestroy(deleteReq.deleteReqs);
×
2931
  return 0;
×
2932
}
2933

2934
static int32_t vnodeProcessDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
119,030✔
2935
                                     SRpcMsg *pOriginalMsg) {
2936
  int32_t     code = 0;
119,030✔
2937
  SDecoder   *pCoder = &(SDecoder){0};
119,030✔
2938
  SDeleteRes *pRes = &(SDeleteRes){0};
119,030✔
2939

2940
  pRsp->msgType = TDMT_VND_DELETE_RSP;
119,030✔
2941
  pRsp->pCont = NULL;
119,030✔
2942
  pRsp->contLen = 0;
119,030✔
2943
  pRsp->code = TSDB_CODE_SUCCESS;
119,030✔
2944

2945
  pRes->uidList = taosArrayInit(0, sizeof(tb_uid_t));
119,030✔
2946
  if (pRes->uidList == NULL) {
119,028!
2947
    code = terrno;
×
2948
    goto _err;
×
2949
  }
2950

2951
  tDecoderInit(pCoder, pReq, len);
119,028✔
2952
  code = tDecodeDeleteRes(pCoder, pRes);
119,030✔
2953
  if (code) goto _err;
119,037!
2954

2955
  if (pRes->affectedRows > 0) {
119,037✔
2956
    for (int32_t iUid = 0; iUid < taosArrayGetSize(pRes->uidList); iUid++) {
233,363✔
2957
      uint64_t uid = *(uint64_t *)taosArrayGet(pRes->uidList, iUid);
120,505✔
2958
      code = tsdbDeleteTableData(pVnode->pTsdb, ver, pRes->suid, uid, pRes->skey, pRes->ekey);
120,504✔
2959
      if (code) goto _err;
120,504!
2960
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, pRes->ctimeMs);
120,504✔
2961
      if (code) goto _err;
120,502!
2962
    }
2963
  }
2964

2965
  tDecoderClear(pCoder);
119,031✔
2966
  taosArrayDestroy(pRes->uidList);
119,029✔
2967

2968
  SVDeleteRsp rsp = {.affectedRows = pRes->affectedRows};
119,035✔
2969
  int32_t     ret = 0;
119,035✔
2970
  tEncodeSize(tEncodeSVDeleteRsp, &rsp, pRsp->contLen, ret);
119,035!
2971
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
119,026✔
2972
  SEncoder ec = {0};
119,036✔
2973
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
119,036✔
2974
  code = tEncodeSVDeleteRsp(&ec, &rsp);
119,037✔
2975
  if (code) goto _err;
119,032!
2976
  tEncoderClear(&ec);
119,032✔
2977
  return code;
119,038✔
2978

2979
_err:
×
2980
  /*
2981
  if(code == TSDB_CODE_SUCCESS){
2982
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_DELETE, pVnode->monitor.strClusterId,
2983
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2984
                                        pOriginalMsg->info.conn.user, "Success"};
2985
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2986
  }
2987
  else{
2988
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_DELETE, pVnode->monitor.strClusterId,
2989
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2990
                                        pOriginalMsg->info.conn.user, "Failed"};
2991
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2992
  }
2993
  */
2994

2995
  return code;
×
2996
}
2997
static int32_t vnodeProcessCreateIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
1,889✔
2998
  SVCreateStbReq req = {0};
1,889✔
2999
  SDecoder       dc = {0};
1,889✔
3000
  int32_t        code = 0;
1,889✔
3001

3002
  pRsp->msgType = TDMT_VND_CREATE_INDEX_RSP;
1,889✔
3003
  pRsp->code = TSDB_CODE_SUCCESS;
1,889✔
3004
  pRsp->pCont = NULL;
1,889✔
3005
  pRsp->contLen = 0;
1,889✔
3006

3007
  tDecoderInit(&dc, pReq, len);
1,889✔
3008
  // decode req
3009
  if (tDecodeSVCreateStbReq(&dc, &req) < 0) {
1,889!
3010
    tDecoderClear(&dc);
×
3011
    return terrno = TSDB_CODE_INVALID_MSG;
×
3012
  }
3013

3014
  code = metaAddIndexToSuperTable(pVnode->pMeta, ver, &req);
1,889✔
3015
  if (code) {
1,889!
3016
    pRsp->code = code;
×
3017
    goto _err;
×
3018
  }
3019
  tDecoderClear(&dc);
1,889✔
3020
  return 0;
1,889✔
3021

3022
_err:
×
3023
  tDecoderClear(&dc);
×
3024
  return code;
×
3025
}
3026
static int32_t vnodeProcessDropIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
4,264✔
3027
  SDropIndexReq req = {0};
4,264✔
3028
  int32_t       code = 0;
4,264✔
3029
  pRsp->msgType = TDMT_VND_DROP_INDEX_RSP;
4,264✔
3030
  pRsp->code = TSDB_CODE_SUCCESS;
4,264✔
3031
  pRsp->pCont = NULL;
4,264✔
3032
  pRsp->contLen = 0;
4,264✔
3033

3034
  if ((code = tDeserializeSDropIdxReq(pReq, len, &req))) {
4,264!
3035
    pRsp->code = code;
×
3036
    return code;
×
3037
  }
3038

3039
  code = metaDropIndexFromSuperTable(pVnode->pMeta, ver, &req);
4,264✔
3040
  if (code) {
4,264!
3041
    pRsp->code = code;
×
3042
    return code;
×
3043
  }
3044
  return TSDB_CODE_SUCCESS;
4,264✔
3045
}
3046

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

3049
static int32_t vnodeProcessCompactVnodeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
179✔
3050
  if (!pVnode->restored) {
179✔
3051
    vInfo("vgId:%d, ignore compact req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
43!
3052
    return 0;
43✔
3053
  }
3054
  return vnodeAsyncCompact(pVnode, ver, pReq, len, pRsp);
136✔
3055
}
3056

3057
static int32_t vnodeProcessConfigChangeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
3058
  if (syncCheckMember(pVnode->sync) != 0) {
×
3059
    vError("vgId:%d, failed to check member", TD_VID(pVnode));
×
3060
  }
3061

3062
  pRsp->msgType = TDMT_SYNC_CONFIG_CHANGE_RSP;
×
3063
  pRsp->code = TSDB_CODE_SUCCESS;
×
3064
  pRsp->pCont = NULL;
×
3065
  pRsp->contLen = 0;
×
3066

3067
  return 0;
×
3068
}
3069

3070
static int32_t vnodeCheckState(SVnode *pVnode) {
108✔
3071
  SSyncState syncState = syncGetState(pVnode->sync);
108✔
3072
  if (syncState.state != TAOS_SYNC_STATE_LEADER) {
108✔
3073
    return terrno = TSDB_CODE_SYN_NOT_LEADER;
46✔
3074
  }
3075
  return 0;
62✔
3076
}
3077

3078
static int32_t vnodeCheckToken(SVnode *pVnode, char *member0Token, char *member1Token) {
62✔
3079
  char token[TSDB_ARB_TOKEN_SIZE] = {0};
62✔
3080
  if (vnodeGetArbToken(pVnode, token) != 0) {
62!
3081
    return terrno = TSDB_CODE_NOT_FOUND;
×
3082
  }
3083

3084
  if (strncmp(token, member0Token, TSDB_ARB_TOKEN_SIZE) != 0 &&
62✔
3085
      strncmp(token, member1Token, TSDB_ARB_TOKEN_SIZE) != 0) {
50✔
3086
    return terrno = TSDB_CODE_MND_ARB_TOKEN_MISMATCH;
46✔
3087
  }
3088

3089
  terrno = TSDB_CODE_SUCCESS;
16✔
3090
  return 0;
16✔
3091
}
3092

3093
static int32_t vnodeCheckSyncd(SVnode *pVnode, char *member0Token, char *member1Token) {
8✔
3094
  int32_t code = vnodeCheckToken(pVnode, member0Token, member1Token);
8✔
3095
  if (code != 0) {
8!
3096
    return code;
×
3097
  }
3098

3099
  return syncCheckSynced(pVnode->sync);
8✔
3100
}
3101

3102
static int32_t vnodeProcessArbCheckSyncReq(SVnode *pVnode, void *pReq, int32_t len, SRpcMsg *pRsp) {
21✔
3103
  int32_t code = 0;
21✔
3104

3105
  if ((code = vnodeCheckState(pVnode)) != 0) {
21✔
3106
    vDebug("vgId:%d, failed to preprocess vnode-arb-check-sync request since %s", TD_VID(pVnode), tstrerror(code));
13!
3107
    return 0;
13✔
3108
  }
3109

3110
  SVArbCheckSyncReq syncReq = {0};
8✔
3111

3112
  code = tDeserializeSVArbCheckSyncReq(pReq, len, &syncReq);
8✔
3113
  if (code) {
8!
3114
    return code;
×
3115
  }
3116

3117
  vInfo("vgId:%d, start to process vnode-arb-check-sync req QID:0x%" PRIx64 ":0x%" PRIx64 ", seqNum:%" PRIx64
8!
3118
        ", arbToken:%s, member0Token:%s, member1Token:%s, "
3119
        "arbTerm:%" PRId64,
3120
        TD_VID(pVnode), pRsp->info.traceId.rootId, pRsp->info.traceId.msgId, pRsp->info.seqNum, syncReq.arbToken,
3121
        syncReq.member0Token, syncReq.member1Token, syncReq.arbTerm);
3122

3123
  pRsp->msgType = TDMT_VND_ARB_CHECK_SYNC_RSP;
8✔
3124
  pRsp->code = TSDB_CODE_SUCCESS;
8✔
3125
  pRsp->pCont = NULL;
8✔
3126
  pRsp->contLen = 0;
8✔
3127

3128
  SVArbCheckSyncRsp syncRsp = {0};
8✔
3129
  syncRsp.arbToken = syncReq.arbToken;
8✔
3130
  syncRsp.member0Token = syncReq.member0Token;
8✔
3131
  syncRsp.member1Token = syncReq.member1Token;
8✔
3132
  syncRsp.vgId = TD_VID(pVnode);
8✔
3133

3134
  if ((syncRsp.errCode = vnodeCheckSyncd(pVnode, syncReq.member0Token, syncReq.member1Token)) != 0) {
8!
3135
    vError("vgId:%d, failed to check assigned log syncd since %s", TD_VID(pVnode), tstrerror(syncRsp.errCode));
×
3136
  }
3137

3138
  if ((code = vnodeUpdateArbTerm(pVnode, syncReq.arbTerm)) != 0) {
8!
3139
    vError("vgId:%d, failed to update arb term since %s", TD_VID(pVnode), tstrerror(code));
×
3140
    goto _OVER;
×
3141
  }
3142

3143
  int32_t contLen = tSerializeSVArbCheckSyncRsp(NULL, 0, &syncRsp);
8✔
3144
  if (contLen <= 0) {
8!
3145
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
3146
    code = -1;
×
3147
    goto _OVER;
×
3148
  }
3149
  void *pHead = rpcMallocCont(contLen);
8✔
3150
  if (!pHead) {
8!
3151
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
3152
    code = -1;
×
3153
    goto _OVER;
×
3154
  }
3155

3156
  if (tSerializeSVArbCheckSyncRsp(pHead, contLen, &syncRsp) <= 0) {
8!
3157
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
3158
    rpcFreeCont(pHead);
×
3159
    code = -1;
×
3160
    goto _OVER;
×
3161
  }
3162

3163
  pRsp->pCont = pHead;
8✔
3164
  pRsp->contLen = contLen;
8✔
3165

3166
  vInfo(
8!
3167
      "vgId:%d, suceed to process vnode-arb-check-sync req rsp.code:%s, arbToken:%s, member0Token:%s, "
3168
      "member1Token:%s",
3169
      TD_VID(pVnode), tstrerror(syncRsp.errCode), syncRsp.arbToken, syncRsp.member0Token, syncRsp.member1Token);
3170

3171
  code = TSDB_CODE_SUCCESS;
8✔
3172

3173
_OVER:
8✔
3174
  if (code != 0) {
8!
3175
    vError("vgId:%d, failed to process arb check req rsp.code:%s since %s", TD_VID(pVnode), tstrerror(syncRsp.errCode),
×
3176
           tstrerror(code));
3177
  }
3178
  tFreeSVArbCheckSyncReq(&syncReq);
8✔
3179
  return code;
8✔
3180
}
3181

3182
#ifndef TD_ENTERPRISE
3183
int32_t vnodeAsyncCompact(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) { return 0; }
3184
int32_t tsdbAsyncCompact(STsdb *tsdb, const STimeWindow *tw, ETsdbOpType type) { return 0; }
3185
#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