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

taosdata / TDengine / #4923

09 Jan 2026 08:13AM UTC coverage: 65.373% (+0.2%) from 65.161%
#4923

push

travis-ci

web-flow
merge: from main to 3.0 branch #34232

33 of 56 new or added lines in 8 files covered. (58.93%)

3042 existing lines in 131 files now uncovered.

198273 of 303297 relevant lines covered (65.37%)

118980690.73 hits per line

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

64.79
/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 vnodeProcessTrimWalReq(SVnode *pVnode, void *pReq, int32_t len, SRpcMsg *pRsp);
52
static int32_t vnodeProcessDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
53
                                     SRpcMsg *pOriginalMsg);
54
static int32_t vnodeProcessBatchDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
55
static int32_t vnodeProcessCreateIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
56
static int32_t vnodeProcessDropIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
57
static int32_t vnodeProcessCompactVnodeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
58
static int32_t vnodeProcessConfigChangeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
59
static int32_t vnodeProcessArbCheckSyncReq(SVnode *pVnode, void *pReq, int32_t len, SRpcMsg *pRsp);
60
static int32_t vnodeProcessDropTSmaCtbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
61
                                          SRpcMsg *pOriginRpc);
62
static int32_t vnodeProcessCreateRsmaReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
63
static int32_t vnodeProcessDropRsmaReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
64
static int32_t vnodeProcessAlterRsmaReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
65

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

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

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

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

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

85
static int32_t vnodePreprocessCreateTableReq(SVnode *pVnode, SDecoder *pCoder, int64_t btime, int64_t *pUid) {
49,504,072✔
86
  int32_t code = 0;
49,504,072✔
87
  int32_t lino = 0;
49,504,072✔
88

89
  if (tStartDecode(pCoder) < 0) {
49,504,072✔
90
    code = TSDB_CODE_INVALID_MSG;
×
91
    TSDB_CHECK_CODE(code, lino, _exit);
×
92
  }
93

94
  // flags
95
  if (tDecodeI32v(pCoder, NULL) < 0) {
49,505,688✔
96
    code = TSDB_CODE_INVALID_MSG;
×
97
    TSDB_CHECK_CODE(code, lino, _exit);
×
98
  }
99

100
  // name
101
  char *name = NULL;
49,505,688✔
102
  if (tDecodeCStr(pCoder, &name) < 0) {
49,505,754✔
103
    code = TSDB_CODE_INVALID_MSG;
×
104
    TSDB_CHECK_CODE(code, lino, _exit);
×
105
  }
106

107
  // uid
108
  int64_t uid = metaGetTableEntryUidByName(pVnode->pMeta, name);
49,505,754✔
109
  if (uid == 0) {
49,505,102✔
110
    uid = tGenIdPI64();
49,155,564✔
111
  }
112
  taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), uid);
49,501,817✔
113

114
  // btime
115
  taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos + 8), btime);
49,501,420✔
116

117
  tEndDecode(pCoder);
49,502,059✔
118

119
_exit:
49,498,746✔
120
  if (code) {
49,499,517✔
121
    vError("vgId:%d %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
122
  } else {
123
    vTrace("vgId:%d %s done, table:%s uid generated:%" PRId64, TD_VID(pVnode), __func__, name, uid);
49,499,517✔
124
    if (pUid) *pUid = uid;
49,496,053✔
125
  }
126
  return code;
49,496,053✔
127
}
128
static int32_t vnodePreProcessCreateTableMsg(SVnode *pVnode, SRpcMsg *pMsg) {
33,857,773✔
129
  int32_t code = 0;
33,857,773✔
130
  int32_t lino = 0;
33,857,773✔
131

132
  int64_t  btime = taosGetTimestampMs();
33,857,773✔
133
  SDecoder dc = {0};
33,857,773✔
134
  int32_t  nReqs;
33,857,011✔
135

136
  tDecoderInit(&dc, (uint8_t *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead));
33,857,773✔
137
  if (tStartDecode(&dc) < 0) {
33,857,773✔
138
    code = TSDB_CODE_INVALID_MSG;
×
139
    return code;
×
140
  }
141

142
  if (tDecodeI32v(&dc, &nReqs) < 0) {
33,857,773✔
143
    code = TSDB_CODE_INVALID_MSG;
×
144
    TSDB_CHECK_CODE(code, lino, _exit);
×
145
  }
146
  for (int32_t iReq = 0; iReq < nReqs; iReq++) {
72,336,181✔
147
    code = vnodePreprocessCreateTableReq(pVnode, &dc, btime, NULL);
38,482,810✔
148
    TSDB_CHECK_CODE(code, lino, _exit);
38,478,408✔
149
  }
150

151
  tEndDecode(&dc);
33,853,371✔
152

153
_exit:
33,856,671✔
154
  tDecoderClear(&dc);
33,856,671✔
155
  if (code) {
33,857,473✔
156
    vError("vgId:%d, %s:%d failed to preprocess submit request since %s, msg type:%s", TD_VID(pVnode), __func__, lino,
×
157
           tstrerror(code), TMSG_INFO(pMsg->msgType));
158
  }
159
  return code;
33,856,371✔
160
}
161

162
static int32_t vnodePreProcessAlterTableMsg(SVnode *pVnode, SRpcMsg *pMsg) {
11,432,766✔
163
  int32_t code = TSDB_CODE_INVALID_MSG;
11,432,766✔
164
  int32_t lino = 0;
11,432,766✔
165

166
  SDecoder dc = {0};
11,432,766✔
167
  tDecoderInit(&dc, (uint8_t *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead));
11,432,766✔
168

169
  SVAlterTbReq vAlterTbReq = {0};
11,432,766✔
170
  int64_t      ctimeMs = taosGetTimestampMs();
11,432,766✔
171
  if (tDecodeSVAlterTbReqSetCtime(&dc, &vAlterTbReq, ctimeMs) < 0) {
11,432,766✔
172
    taosArrayDestroy(vAlterTbReq.pMultiTag);
×
173
    vAlterTbReq.pMultiTag = NULL;
×
174
    goto _exit;
×
175
  }
176
  taosArrayDestroy(vAlterTbReq.pMultiTag);
11,432,766✔
177
  vAlterTbReq.pMultiTag = NULL;
11,432,766✔
178

179
  code = 0;
11,432,766✔
180

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

192
static int32_t vnodePreProcessDropTtlMsg(SVnode *pVnode, SRpcMsg *pMsg) {
12,982,012✔
193
  int32_t code = TSDB_CODE_INVALID_MSG;
12,982,012✔
194
  int32_t lino = 0;
12,982,012✔
195

196
  SMsgHead *pContOld = pMsg->pCont;
12,982,012✔
197
  int32_t   reqLenOld = pMsg->contLen - sizeof(SMsgHead);
12,983,338✔
198

199
  SArray *tbUids = NULL;
12,983,188✔
200
  int64_t timestampMs = 0;
12,983,188✔
201

202
  SVDropTtlTableReq ttlReq = {0};
12,983,188✔
203
  if (tDeserializeSVDropTtlTableReq((char *)pContOld + sizeof(SMsgHead), reqLenOld, &ttlReq) != 0) {
12,982,513✔
204
    code = TSDB_CODE_INVALID_MSG;
×
205
    TSDB_CHECK_CODE(code, lino, _exit);
×
206
  }
207

208
  {  // find expired uids
209
    tbUids = taosArrayInit(8, sizeof(tb_uid_t));
12,981,764✔
210
    if (tbUids == NULL) {
12,981,795✔
211
      code = terrno;
×
212
      TSDB_CHECK_CODE(code, lino, _exit);
×
213
    }
214

215
    timestampMs = (int64_t)ttlReq.timestampSec * 1000;
12,981,795✔
216
    code = metaTtlFindExpired(pVnode->pMeta, timestampMs, tbUids, ttlReq.ttlDropMaxCount);
12,981,795✔
217
    if (code != 0) {
12,974,122✔
218
      code = TSDB_CODE_INVALID_MSG;
×
219
      TSDB_CHECK_CODE(code, lino, _exit);
×
220
    }
221

222
    ttlReq.nUids = taosArrayGetSize(tbUids);
12,974,122✔
223
    ttlReq.pTbUids = tbUids;
12,976,701✔
224
  }
225

226
  if (ttlReq.nUids == 0) {
12,976,701✔
227
    code = TSDB_CODE_MSG_PREPROCESSED;
12,961,130✔
228
    TSDB_CHECK_CODE(code, lino, _exit);
12,961,130✔
229
  }
230

231
  {  // prepare new content
232
    int32_t reqLenNew = tSerializeSVDropTtlTableReq(NULL, 0, &ttlReq);
12,995✔
233
    int32_t contLenNew = reqLenNew + sizeof(SMsgHead);
12,388✔
234

235
    SMsgHead *pContNew = rpcMallocCont(contLenNew);
12,388✔
236
    if (pContNew == NULL) {
12,388✔
237
      code = terrno;
×
238
      TSDB_CHECK_CODE(code, lino, _exit);
×
239
    }
240

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

247
    rpcFreeCont(pContOld);
12,388✔
248
    pMsg->pCont = pContNew;
12,388✔
249
    pMsg->contLen = contLenNew;
12,388✔
250
  }
251

252
  code = 0;
12,388✔
253

254
_exit:
12,976,094✔
255
  taosArrayDestroy(tbUids);
12,978,259✔
256

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

264
  return code;
12,965,140✔
265
}
266

267
extern int64_t tsMaxKeyByPrecision[];
268
static int32_t vnodePreProcessSubmitTbData(SVnode *pVnode, SDecoder *pCoder, int64_t btimeMs, int64_t ctimeMs) {
576,288,062✔
269
  int32_t code = 0;
576,288,062✔
270
  int32_t lino = 0;
576,288,062✔
271

272
  if (tStartDecode(pCoder) < 0) {
576,288,062✔
273
    code = TSDB_CODE_INVALID_MSG;
×
274
    TSDB_CHECK_CODE(code, lino, _exit);
×
275
  }
276

277
  SSubmitTbData submitTbData;
576,285,993✔
278
  uint8_t       version;
279
  if (tDecodeI32v(pCoder, &submitTbData.flags) < 0) {
576,287,365✔
280
    code = TSDB_CODE_INVALID_MSG;
×
281
    TSDB_CHECK_CODE(code, lino, _exit);
×
282
  }
283
  version = (submitTbData.flags >> 8) & 0xff;
576,287,365✔
284
  submitTbData.flags = submitTbData.flags & 0xff;
576,287,365✔
285

286
  int64_t uid;
576,284,185✔
287
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
576,287,951✔
288
    code = vnodePreprocessCreateTableReq(pVnode, pCoder, btimeMs, &uid);
11,018,361✔
289
    TSDB_CHECK_CODE(code, lino, _exit);
11,017,628✔
290
  }
291

292
  // submit data
293
  if (tDecodeI64(pCoder, &submitTbData.suid) < 0) {
576,287,866✔
294
    code = TSDB_CODE_INVALID_MSG;
×
295
    TSDB_CHECK_CODE(code, lino, _exit);
×
296
  }
297

298
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
576,287,866✔
299
    taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), uid);
11,017,739✔
300
    pCoder->pos += sizeof(int64_t);
11,017,123✔
301
  } else {
302
    if (tDecodeI64(pCoder, &submitTbData.uid) < 0) {
565,269,179✔
303
      code = TSDB_CODE_INVALID_MSG;
×
304
      TSDB_CHECK_CODE(code, lino, _exit);
×
305
    }
306
  }
307

308
  if (tDecodeI32v(pCoder, &submitTbData.sver) < 0) {
576,290,583✔
309
    code = TSDB_CODE_INVALID_MSG;
×
310
    TSDB_CHECK_CODE(code, lino, _exit);
×
311
  }
312

313
  // scan and check
314
  TSKEY now = btimeMs;
576,290,583✔
315
  if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_MICRO) {
576,290,583✔
316
    now *= 1000;
2,371,986✔
317
  } else if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_NANO) {
573,915,572✔
318
    now *= 1000000;
1,364,898✔
319
  }
320

321
  int32_t keep = pVnode->config.tsdbCfg.keep2;
576,288,565✔
322

323
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * keep;
576,287,429✔
324
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
576,289,559✔
325
  if (submitTbData.flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
576,287,475✔
326
    uint64_t nColData;
3,213,228✔
327
    if (tDecodeU64v(pCoder, &nColData) < 0) {
3,213,612✔
328
      code = TSDB_CODE_INVALID_MSG;
×
329
      TSDB_CHECK_CODE(code, lino, _exit);
×
330
    }
331

332
    SColData colData = {0};
3,213,612✔
333
    code = tDecodeColData(version, pCoder, &colData, false);
3,213,304✔
334
    if (code) {
3,213,970✔
335
      code = TSDB_CODE_INVALID_MSG;
×
336
      TSDB_CHECK_CODE(code, lino, _exit);
×
337
    }
338

339
    if (colData.flag != HAS_VALUE) {
3,213,970✔
340
      code = TSDB_CODE_INVALID_MSG;
×
341
      TSDB_CHECK_CODE(code, lino, _exit);
×
342
    }
343

344
    for (int32_t iRow = 0; iRow < colData.nVal; iRow++) {
2,147,483,647✔
345
      if (((TSKEY *)colData.pData)[iRow] < minKey || ((TSKEY *)colData.pData)[iRow] > maxKey) {
2,147,483,647✔
346
        code = TSDB_CODE_TDB_TIMESTAMP_OUT_OF_RANGE;
557✔
347
        TSDB_CHECK_CODE(code, lino, _exit);
557✔
348
      }
349
    }
350

351
    for (uint64_t i = 1; i < nColData; i++) {
10,027,396✔
352
      code = tDecodeColData(version, pCoder, &colData, true);
6,884,160✔
353
      if (code) {
6,884,418✔
354
        code = TSDB_CODE_INVALID_MSG;
×
355
        TSDB_CHECK_CODE(code, lino, _exit);
×
356
      }
357
    }
358
  } else {
359
    uint64_t nRow;
573,071,067✔
360
    if (tDecodeU64v(pCoder, &nRow) < 0) {
573,072,257✔
361
      code = TSDB_CODE_INVALID_MSG;
×
362
      TSDB_CHECK_CODE(code, lino, _exit);
×
363
    }
364

365
    for (int32_t iRow = 0; iRow < nRow; ++iRow) {
2,147,483,647✔
366
      SRow *pRow = (SRow *)(pCoder->data + pCoder->pos);
2,147,483,647✔
367
      pCoder->pos += pRow->len;
2,147,483,647✔
368
#ifndef NO_UNALIGNED_ACCESS
369
      if (pRow->ts < minKey || pRow->ts > maxKey) {
2,147,483,647✔
370
#else
371
      TSKEY ts = taosGetInt64Aligned(&pRow->ts);
372
      if (ts < minKey || ts > maxKey) {
373
#endif
374
        code = TSDB_CODE_TDB_TIMESTAMP_OUT_OF_RANGE;
64,250✔
375
        TSDB_CHECK_CODE(code, lino, _exit);
64,250✔
376
      }
377

378
      // Check pRow->sver
379
      if (pRow->sver != submitTbData.sver) {
2,147,483,647✔
380
        code = TSDB_CODE_INVALID_DATA_FMT;
×
381
        TSDB_CHECK_CODE(code, lino, _exit);
×
382
      }
383
    }
384
  }
385

386
  if (!tDecodeIsEnd(pCoder)) {
576,220,736✔
387
    taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), ctimeMs);
576,221,398✔
388
    pCoder->pos += sizeof(int64_t);
576,213,275✔
389
  }
390

391
  tEndDecode(pCoder);
576,214,371✔
392

393
_exit:
576,285,589✔
394
  if (code) {
576,286,138✔
395
    vError("vgId:%d, %s:%d failed to vnodePreProcessSubmitTbData submit request since %s", TD_VID(pVnode), __func__,
64,807✔
396
           lino, tstrerror(code));
397
  }
398
  return code;
576,286,138✔
399
}
400
static int32_t vnodePreProcessSubmitMsg(SVnode *pVnode, SRpcMsg *pMsg) {
550,359,695✔
401
  int32_t code = 0;
550,359,695✔
402
  int32_t lino = 0;
550,359,695✔
403

404
  if (tsBypassFlag & TSDB_BYPASS_RA_RPC_RECV_SUBMIT) {
550,359,695✔
405
    return TSDB_CODE_MSG_PREPROCESSED;
67✔
406
  }
407

408
  SDecoder *pCoder = &(SDecoder){0};
550,359,628✔
409

410
  if (taosHton64(((SSubmitReq2Msg *)pMsg->pCont)->version) != 1) {
550,359,144✔
411
    code = TSDB_CODE_INVALID_MSG;
×
412
    TSDB_CHECK_CODE(code, lino, _exit);
×
413
  }
414

415
  tDecoderInit(pCoder, (uint8_t *)pMsg->pCont + sizeof(SSubmitReq2Msg), pMsg->contLen - sizeof(SSubmitReq2Msg));
550,359,730✔
416

417
  if (tStartDecode(pCoder) < 0) {
550,359,730✔
418
    code = TSDB_CODE_INVALID_MSG;
×
419
    TSDB_CHECK_CODE(code, lino, _exit);
×
420
  }
421

422
  uint64_t nSubmitTbData;
550,355,964✔
423
  if (tDecodeU64v(pCoder, &nSubmitTbData) < 0) {
550,359,214✔
424
    code = TSDB_CODE_INVALID_MSG;
×
425
    TSDB_CHECK_CODE(code, lino, _exit);
×
426
  }
427

428
  int64_t btimeMs = taosGetTimestampMs();
550,359,247✔
429
  int64_t ctimeMs = btimeMs;
550,359,247✔
430
  for (int32_t i = 0; i < nSubmitTbData; i++) {
1,126,580,685✔
431
    code = vnodePreProcessSubmitTbData(pVnode, pCoder, btimeMs, ctimeMs);
576,288,280✔
432
    TSDB_CHECK_CODE(code, lino, _exit);
576,286,245✔
433
  }
434

435
  tEndDecode(pCoder);
550,292,405✔
436

437
_exit:
550,358,650✔
438
  tDecoderClear(pCoder);
550,358,650✔
439
  if (code) {
550,359,179✔
440
    vError("vgId:%d, %s:%d failed to preprocess submit request since %s, msg type:%s", TD_VID(pVnode), __func__, lino,
64,807✔
441
           tstrerror(code), TMSG_INFO(pMsg->msgType));
442
  }
443
  return code;
550,356,391✔
444
}
445

446
static int32_t vnodePreProcessDeleteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
1,661,533✔
447
  int32_t code = 0;
1,661,533✔
448

449
  int32_t    size;
450
  int32_t    ret;
451
  uint8_t   *pCont;
452
  SEncoder  *pCoder = &(SEncoder){0};
1,661,533✔
453
  SDeleteRes res = {0};
1,661,533✔
454

455
  SReadHandle handle = {0};
1,661,533✔
456
  handle.vnode = pVnode;
1,661,533✔
457
  handle.pMsgCb = &pVnode->msgCb;
1,661,533✔
458
  handle.skipRollup = 1;
1,661,533✔
459
  initStorageAPI(&handle.api);
1,661,533✔
460

461
  code = qWorkerProcessDeleteMsg(&handle, pVnode->pQuery, pMsg, &res);
1,661,533✔
462
  if (code) goto _exit;
1,661,533✔
463

464
  res.ctimeMs = taosGetTimestampMs();
1,661,533✔
465
  // malloc and encode
466
  tEncodeSize(tEncodeDeleteRes, &res, size, ret);
1,661,533✔
467
  pCont = rpcMallocCont(size + sizeof(SMsgHead));
1,661,533✔
468

469
  ((SMsgHead *)pCont)->contLen = size + sizeof(SMsgHead);
1,661,533✔
470
  ((SMsgHead *)pCont)->vgId = TD_VID(pVnode);
1,661,533✔
471

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

478
  rpcFreeCont(pMsg->pCont);
1,661,533✔
479
  pMsg->pCont = pCont;
1,661,533✔
480
  pMsg->contLen = size + sizeof(SMsgHead);
1,661,533✔
481

482
  taosArrayDestroy(res.uidList);
1,661,533✔
483

484
_exit:
1,661,533✔
485
  return code;
1,661,533✔
486
}
487

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

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

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

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

502
  tDecoderClear(pCoder);
×
503
  taosArrayDestroy(pReq.deleteReqs);
×
504

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

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

520
  SVArbCheckSyncReq syncReq = {0};
28,592✔
521

522
  if (tDeserializeSVArbCheckSyncReq((char *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead),
28,592✔
523
                                    &syncReq) != 0) {
524
    return TSDB_CODE_INVALID_MSG;
×
525
  }
526

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

532
  int32_t code = terrno;
28,592✔
533
  tFreeSVArbCheckSyncReq(&syncReq);
28,592✔
534

535
  return code;
28,592✔
536
}
537

538
int32_t vnodePreProcessDropTbMsg(SVnode *pVnode, SRpcMsg *pMsg) {
1,277,018✔
539
  int32_t          code = TSDB_CODE_SUCCESS;
1,277,018✔
540
  int32_t          lino = 0;
1,277,018✔
541
  int32_t          size = 0;
1,277,018✔
542
  SDecoder         dc = {0};
1,277,018✔
543
  SEncoder         ec = {0};
1,277,018✔
544
  SVDropTbBatchReq receivedBatchReqs = {0};
1,277,018✔
545
  SVDropTbBatchReq sentBatchReqs = {0};
1,277,018✔
546

547
  tDecoderInit(&dc, POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), pMsg->contLen - sizeof(SMsgHead));
1,277,018✔
548

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

560
  for (int32_t i = 0; i < receivedBatchReqs.nReqs; ++i) {
2,605,555✔
561
    SVDropTbReq *pReq = receivedBatchReqs.pReqs + i;
1,328,537✔
562
    tb_uid_t     uid = metaGetTableEntryUidByName(pVnode->pMeta, pReq->name);
1,328,537✔
563
    if (uid == 0) {
1,328,537✔
564
      vWarn("vgId:%d, preprocess drop ctb: %s not found", TD_VID(pVnode), pReq->name);
×
565
      continue;
×
566
    }
567
    pReq->uid = uid;
1,328,537✔
568
    vDebug("vgId:%d %s for: %s, uid: %" PRId64, TD_VID(pVnode), __func__, pReq->name, pReq->uid);
1,328,537✔
569
    if (taosArrayPush(sentBatchReqs.pArray, pReq) == NULL) {
2,657,074✔
570
      code = terrno;
×
571
      goto _exit;
×
572
    }
573
  }
574
  sentBatchReqs.nReqs = sentBatchReqs.pArray->size;
1,277,018✔
575

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

585
_exit:
1,277,018✔
586
  tDecoderClear(&dc);
1,277,018✔
587
  if (sentBatchReqs.pArray) {
1,277,018✔
588
    taosArrayDestroy(sentBatchReqs.pArray);
1,277,018✔
589
  }
590
  return code;
1,277,018✔
591
}
592

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

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

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

608
_exit:
×
609
  return code;
×
610
}
611

612
int32_t vnodePreProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
630,905,400✔
613
  int32_t code = 0;
630,905,400✔
614

615
  if (pVnode->mounted) {
630,905,400✔
616
    vError("vgId:%d, mountVgId:%d, skip write msg:%s", TD_VID(pVnode), pVnode->config.mountVgId,
1,270✔
617
           TMSG_INFO(pMsg->msgType));
618
    return TSDB_CODE_OPS_NOT_SUPPORT;
1,270✔
619
  }
620

621
  switch (pMsg->msgType) {
630,904,988✔
622
    case TDMT_VND_CREATE_TABLE: {
33,857,773✔
623
      code = vnodePreProcessCreateTableMsg(pVnode, pMsg);
33,857,773✔
624
    } break;
33,856,671✔
625
    case TDMT_VND_ALTER_TABLE: {
11,432,766✔
626
      code = vnodePreProcessAlterTableMsg(pVnode, pMsg);
11,432,766✔
627
    } break;
11,432,766✔
628
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
12,982,859✔
629
    case TDMT_VND_DROP_TTL_TABLE: {
630
      code = vnodePreProcessDropTtlMsg(pVnode, pMsg);
12,982,859✔
631
    } break;
12,970,195✔
632
    case TDMT_VND_SUBMIT: {
550,359,797✔
633
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.preprocess_time, METRIC_LEVEL_LOW,
550,359,797✔
634
                           { code = vnodePreProcessSubmitMsg(pVnode, pMsg); });
635
    } break;
550,357,448✔
636
    case TDMT_VND_DELETE: {
1,661,533✔
637
      code = vnodePreProcessDeleteMsg(pVnode, pMsg);
1,661,533✔
638
    } break;
1,661,533✔
639
    case TDMT_VND_BATCH_DEL: {
×
640
      code = vnodePreProcessBatchDeleteMsg(pVnode, pMsg);
×
641
    } break;
×
642
    case TDMT_VND_ARB_CHECK_SYNC: {
52,671✔
643
      code = vnodePreProcessArbCheckSyncMsg(pVnode, pMsg);
52,671✔
644
    } break;
52,671✔
645
    case TDMT_VND_DROP_TABLE: {
1,277,018✔
646
      code = vnodePreProcessDropTbMsg(pVnode, pMsg);
1,277,018✔
647
    } break;
1,277,018✔
648
#ifdef TD_ENTERPRISE
649
    case TDMT_VND_SSMIGRATE_FILESET: {
705✔
650
      code = vnodePreProcessSsMigrateFileSetReq(pVnode, pMsg);
705✔
651
    } break;
×
652
#endif
653
    default:
19,279,974✔
654
      break;
19,279,974✔
655
  }
656

657
  if (code && code != TSDB_CODE_MSG_PREPROCESSED) {
630,888,276✔
658
    vError("vgId:%d, failed to preprocess write request since %s, msg type:%s", TD_VID(pVnode), tstrerror(code),
92,018✔
659
           TMSG_INFO(pMsg->msgType));
660
  }
661
  return code;
630,892,120✔
662
}
663

664
static int32_t inline vnodeSubmitSubRowBlobData(SVnode *pVnode, SSubmitTbData *pSubmitTbData) {
21,306✔
665
  int32_t code = 0;
21,306✔
666
  int32_t lino = 0;
21,306✔
667

668
  int64_t   st = taosGetTimestampUs();
21,306✔
669
  SBlobSet *pBlobSet = pSubmitTbData->pBlobSet;
21,306✔
670
  int32_t   sz = taosArrayGetSize(pBlobSet->pSeqTable);
21,306✔
671

672
  SBseBatch *pBatch = NULL;
21,306✔
673

674
  code = bseBatchInit(pVnode->pBse, &pBatch, sz);
21,306✔
675
  TSDB_CHECK_CODE(code, lino, _exit);
21,306✔
676

677
  SRow  **pRow = (SRow **)TARRAY_DATA(pSubmitTbData->aRowP);
21,306✔
678
  int32_t rowIdx = -1;
21,306✔
679
  for (int32_t i = 0; i < sz; i++) {
23,187,912✔
680
    int64_t     seq = 0;
23,166,606✔
681
    SBlobValue *p = taosArrayGet(pBlobSet->pSeqTable, i);
23,166,606✔
682
    if (p->type == TSDB_DATA_BLOB_EMPTY_VALUE || p->type == TSDB_DATA_BLOB_NULL_VALUE) {
23,166,606✔
683
      // skip empty or null blob
684
      continue;
2,327,412✔
685
    }
686

687
    code = bseBatchPut(pBatch, &seq, pBlobSet->data + p->offset, p->len);
20,839,194✔
688
    TSDB_CHECK_CODE(code, lino, _exit);
20,839,194✔
689

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

697
    if (tPutU64(row->data + p->dataOffset, seq) < 0) {
41,678,388✔
698
      code = TSDB_CODE_INVALID_MSG;
×
699
      TSDB_CHECK_CODE(code, lino, _exit);
×
700
    }
701
  }
702

703
  code = bseCommitBatch(pVnode->pBse, pBatch);
21,306✔
704
  TSDB_CHECK_CODE(code, lino, _exit);
21,306✔
705

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

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

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

728
  SBseBatch *pBatch = NULL;
×
729

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

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

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

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

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

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

786
  return code;
21,306✔
787
}
788

789
int32_t vnodeProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg, int64_t ver, SRpcMsg *pRsp) {
735,548,063✔
790
  int32_t code = 0;
735,548,063✔
791
  int32_t lino = 0;
735,548,063✔
792
  void   *ptr = NULL;
735,548,063✔
793
  void   *pReq;
794
  int32_t len;
795

796
  (void)taosThreadMutexLock(&pVnode->mutex);
735,548,063✔
797
  if (pVnode->disableWrite) {
735,594,330✔
798
    (void)taosThreadMutexUnlock(&pVnode->mutex);
×
799
    vError("vgId:%d write is disabled for snapshot, version:%" PRId64, TD_VID(pVnode), ver);
×
800
    return TSDB_CODE_VND_WRITE_DISABLED;
×
801
  }
802
  (void)taosThreadMutexUnlock(&pVnode->mutex);
735,563,936✔
803

804
  if (ver <= pVnode->state.applied) {
735,597,148✔
805
    vError("vgId:%d, duplicate write request. ver: %" PRId64 ", applied: %" PRId64, TD_VID(pVnode), ver,
×
806
           pVnode->state.applied);
807
    return terrno = TSDB_CODE_VND_DUP_REQUEST;
×
808
  }
809

810
  vGDebug(&pMsg->info.traceId,
735,570,141✔
811
          "vgId:%d, index:%" PRId64 ", process write request:%s, applied:%" PRId64 ", state.applyTerm:%" PRId64
812
          ", conn.applyTerm:%" PRId64 ", contLen:%d",
813
          TD_VID(pVnode), ver, TMSG_INFO(pMsg->msgType), pVnode->state.applied, pVnode->state.applyTerm,
814
          pMsg->info.conn.applyTerm, pMsg->contLen);
815

816
  if (!(pVnode->state.applyTerm <= pMsg->info.conn.applyTerm)) {
735,605,822✔
817
    vError("vgId:%d, applyTerm mismatch, expected: %" PRId64 ", received: %" PRId64, TD_VID(pVnode),
×
818
           pVnode->state.applyTerm, pMsg->info.conn.applyTerm);
819
    return terrno = TSDB_CODE_INTERNAL_ERROR;
×
820
  }
821

822
  if (!(pVnode->state.applied + 1 == ver)) {
735,604,687✔
823
    vError("vgId:%d, mountVgId:%d, ver mismatch, expected: %" PRId64 ", received: %" PRId64, TD_VID(pVnode),
×
824
           pVnode->config.mountVgId, pVnode->state.applied + 1, ver);
825
    return terrno = TSDB_CODE_INTERNAL_ERROR;
×
826
  }
827

828
  atomic_store_64(&pVnode->state.applied, ver);
735,605,373✔
829
  atomic_store_64(&pVnode->state.applyTerm, pMsg->info.conn.applyTerm);
735,604,687✔
830

831
  if (!syncUtilUserCommit(pMsg->msgType)) goto _exit;
735,604,687✔
832

833
  // skip header
834
  pReq = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
725,349,700✔
835
  len = pMsg->contLen - sizeof(SMsgHead);
725,349,700✔
836
  bool needCommit = false;
725,349,700✔
837

838
  switch (pMsg->msgType) {
725,349,700✔
839
    /* META */
840
    case TDMT_VND_CREATE_STB:
4,710,496✔
841
      code = vnodeProcessCreateStbReq(pVnode, ver, pReq, len, pRsp);
4,710,496✔
842
      TSDB_CHECK_CODE(code, lino, _err);
4,710,413✔
843
      break;
4,707,553✔
844
    case TDMT_VND_ALTER_STB:
7,263,186✔
845
      code = vnodeProcessAlterStbReq(pVnode, ver, pReq, len, pRsp);
7,263,186✔
846
      TSDB_CHECK_CODE(code, lino, _err);
7,263,186✔
847
      break;
7,263,186✔
848
    case TDMT_VND_DROP_STB:
856,875✔
849
      code = vnodeProcessDropStbReq(pVnode, ver, pReq, len, pRsp, pMsg);
856,875✔
850
      TSDB_CHECK_CODE(code, lino, _err);
856,875✔
851
      break;
856,875✔
852
    case TDMT_VND_CREATE_TABLE:
36,558,492✔
853
      code = vnodeProcessCreateTbReq(pVnode, ver, pReq, len, pRsp, pMsg);
36,558,492✔
854
      TSDB_CHECK_CODE(code, lino, _err);
36,555,291✔
855
      break;
36,555,291✔
856
    case TDMT_VND_ALTER_TABLE:
11,433,614✔
857
      code = vnodeProcessAlterTbReq(pVnode, ver, pReq, len, pRsp);
11,433,614✔
858
      TSDB_CHECK_CODE(code, lino, _err);
11,433,614✔
859
      break;
11,433,614✔
860
    case TDMT_VND_DROP_TABLE:
1,277,323✔
861
    case TDMT_VND_SNODE_DROP_TABLE:
862
      code = vnodeProcessDropTbReq(pVnode, ver, pReq, len, pRsp, pMsg);
1,277,323✔
863
      TSDB_CHECK_CODE(code, lino, _err);
1,277,323✔
864
      break;
1,277,323✔
865
    case TDMT_VND_CREATE_RSMA:
22,020✔
866
      code = vnodeProcessCreateRsmaReq(pVnode, ver, pReq, len, pRsp);
22,020✔
867
      TSDB_CHECK_CODE(code, lino, _err);
22,020✔
868
      break;
22,020✔
869
    case TDMT_VND_DROP_RSMA:
4,404✔
870
      code = vnodeProcessDropRsmaReq(pVnode, ver, pReq, len, pRsp);
4,404✔
871
      TSDB_CHECK_CODE(code, lino, _err);
4,404✔
872
      break;
4,404✔
873
    case TDMT_VND_ALTER_RSMA:
11,744✔
874
      code = vnodeProcessAlterRsmaReq(pVnode, ver, pReq, len, pRsp);
11,744✔
875
      TSDB_CHECK_CODE(code, lino, _err);
11,744✔
876
      break;
11,744✔
877
    case TDMT_VND_DROP_TTL_TABLE:
×
878
      code = vnodeProcessDropTtlTbReq(pVnode, ver, pReq, len, pRsp);
×
879
      TSDB_CHECK_CODE(code, lino, _err);
×
880
      break;
×
881
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
12,388✔
882
      code = vnodeProcessFetchTtlExpiredTbs(pVnode, ver, pReq, len, pRsp);
12,388✔
883
      TSDB_CHECK_CODE(code, lino, _err);
12,388✔
884
      break;
12,388✔
885
    case TDMT_VND_TRIM:
42,435✔
886
      code = vnodeProcessTrimReq(pVnode, ver, pReq, len, pRsp);
42,435✔
887
      TSDB_CHECK_CODE(code, lino, _err);
42,435✔
888
      break;
42,435✔
889
      case TDMT_VND_TRIM_WAL:
3,204✔
890
      if (vnodeProcessTrimWalReq(pVnode, pReq, len, pRsp) < 0) goto _err;
3,204✔
891
      break;
3,204✔
892
#ifdef TD_ENTERPRISE
893
    case TDMT_VND_SSMIGRATE_FILESET:
×
894
      if (vnodeProcessSsMigrateFileSetReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
895
      break;
×
896
    case TDMT_VND_FOLLOWER_SSMIGRATE:
×
897
      if (vnodeProcessFollowerSsMigrateReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
898
      break;
×
899
    case TDMT_VND_KILL_SSMIGRATE:
×
900
      if (vnodeProcessKillSsMigrateReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
901
      break;
×
902
#endif
903

904
    /* TSDB */
905
    case TDMT_VND_SUBMIT: {
646,025,335✔
906
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.apply_time, METRIC_LEVEL_LOW, {
646,025,335✔
907
        if (vnodeProcessSubmitReq(pVnode, ver, pMsg->pCont, pMsg->contLen, pRsp, pMsg) < 0) goto _err;
908
      });
909
      METRICS_UPDATE(pVnode->writeMetrics.apply_bytes, METRIC_LEVEL_LOW, (int64_t)pMsg->contLen);
645,981,561✔
910
      break;
645,979,350✔
911
    }
912
    case TDMT_VND_DELETE:
2,014,615✔
913
      code = vnodeProcessDeleteReq(pVnode, ver, pReq, len, pRsp, pMsg);
2,014,615✔
914
      TSDB_CHECK_CODE(code, lino, _err);
2,014,615✔
915
      break;
2,014,615✔
916
    case TDMT_VND_BATCH_DEL:
×
917
      code = vnodeProcessBatchDeleteReq(pVnode, ver, pReq, len, pRsp);
×
918
      TSDB_CHECK_CODE(code, lino, _err);
×
919
      break;
×
920
      /* TQ */
921
#if defined(USE_TQ) || defined(USE_STREAM)
922
    case TDMT_VND_TMQ_SUBSCRIBE:
978,344✔
923
      code = tqProcessSubscribeReq(pVnode->pTq, ver, pReq, len);
978,344✔
924
      TSDB_CHECK_CODE(code, lino, _err);
977,506✔
925
      break;
977,506✔
926
    case TDMT_VND_TMQ_DELETE_SUB:
269,801✔
927
      code = tqProcessDeleteSubReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen);
269,801✔
928
      TSDB_CHECK_CODE(code, lino, _err);
269,801✔
929
      break;
269,801✔
930
    case TDMT_VND_TMQ_COMMIT_OFFSET:
4,663,576✔
931
      code = tqProcessOffsetCommitReq(pVnode->pTq, ver, pReq, len);
4,663,576✔
932
      TSDB_CHECK_CODE(code, lino, _err);
4,663,296✔
933
      break;
4,663,296✔
934
#endif
935
    case TDMT_VND_ALTER_CONFIRM:
4,168,430✔
936
      needCommit = pVnode->config.hashChange;
4,168,430✔
937
      code = vnodeProcessAlterConfirmReq(pVnode, ver, pReq, len, pRsp);
4,168,430✔
938
      TSDB_CHECK_CODE(code, lino, _err);
4,168,430✔
939
      break;
4,168,430✔
940
    case TDMT_VND_ALTER_CONFIG:
313,503✔
941
      vnodeProcessAlterConfigReq(pVnode, ver, pReq, len, pRsp);
313,503✔
942
      break;
313,503✔
943
    case TDMT_VND_COMMIT:
4,618,162✔
944
      needCommit = true;
4,618,162✔
945
      break;
4,618,162✔
946
    case TDMT_VND_CREATE_INDEX:
4,221✔
947
      vnodeProcessCreateIndexReq(pVnode, ver, pReq, len, pRsp);
4,221✔
948
      break;
4,221✔
949
    case TDMT_VND_DROP_INDEX:
2,766✔
950
      vnodeProcessDropIndexReq(pVnode, ver, pReq, len, pRsp);
2,766✔
951
      break;
2,766✔
952
    case TDMT_VND_COMPACT:
67,802✔
953
      vnodeProcessCompactVnodeReq(pVnode, ver, pReq, len, pRsp);
67,802✔
954
      goto _exit;
67,802✔
955
    case TDMT_VND_SCAN:
235✔
956
      vnodeProcessScanVnodeReq(pVnode, ver, pReq, len, pRsp);
235✔
957
      goto _exit;
235✔
958
    case TDMT_SYNC_CONFIG_CHANGE:
×
959
      vnodeProcessConfigChangeReq(pVnode, ver, pReq, len, pRsp);
×
960
      break;
×
961
#ifdef TD_ENTERPRISE
962
    case TDMT_VND_KILL_COMPACT:
1,378✔
963
      vnodeProcessKillCompactReq(pVnode, ver, pReq, len, pRsp);
1,378✔
964
      break;
1,378✔
965
#endif
966
    case TDMT_VND_KILL_SCAN:
94✔
967
      vnodeProcessKillScanReq(pVnode, ver, pReq, len, pRsp);
94✔
968
      break;
94✔
969
    case TDMT_VND_KILL_TRIM:
×
970
      vnodeProcessKillRetentionReq(pVnode, ver, pReq, len, pRsp);
×
971
      break;
×
972
    /* ARB */
973
    case TDMT_VND_ARB_CHECK_SYNC:
24,458✔
974
      vnodeProcessArbCheckSyncReq(pVnode, pReq, len, pRsp);
24,458✔
975
      break;
24,458✔
976
    default:
×
977
      vError("vgId:%d, unprocessed msg, %d", TD_VID(pVnode), pMsg->msgType);
×
978
      return TSDB_CODE_INVALID_MSG;
×
979
  }
980

981
  vGDebug(&pMsg->info.traceId, "vgId:%d, index:%" PRId64 ", msg processed, code:0x%x", TD_VID(pVnode), ver, pRsp->code);
725,227,617✔
982

983
  walApplyVer(pVnode->pWal, ver);
725,236,082✔
984

985
  if (pVnode->pTq) {
725,241,619✔
986
    code = tqPushMsg(pVnode->pTq, pMsg->msgType);
725,242,153✔
987
    if (code) {
725,241,031✔
988
      vError("vgId:%d, failed to push msg to TQ since %s", TD_VID(pVnode), tstrerror(terrno));
269✔
989
      return code;
269✔
990
    }
991
  }
992

993
  // commit if need
994
  if (needCommit) {
725,240,762✔
995
    vInfo("vgId:%d, commit at version %" PRId64, TD_VID(pVnode), ver);
4,642,147✔
996
    code = vnodeAsyncCommit(pVnode);
4,641,734✔
997
    if (code) {
4,642,147✔
998
      vError("vgId:%d, failed to vnode async commit since %s.", TD_VID(pVnode), tstrerror(terrno));
×
999
      goto _err;
×
1000
    }
1001

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

1012
_exit:
725,239,671✔
1013
  return 0;
735,559,963✔
1014

1015
_err:
39,377✔
1016
  if (code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
39,377✔
1017
    vInfo("vgId:%d, process %s request failed since %s, lino 1:%d ver:%" PRId64, TD_VID(pVnode),
2,860✔
1018
          TMSG_INFO(pMsg->msgType), tstrerror(code), lino, ver);
1019
  } else if (code) {
36,517✔
1020
    vError("vgId:%d, process %s request failed since %s, lino 2:%d ver:%" PRId64, TD_VID(pVnode),
×
1021
           TMSG_INFO(pMsg->msgType), tstrerror(code), lino, ver);
1022
  }
1023

1024
  return code;
39,377✔
1025
}
1026

1027
int32_t vnodePreprocessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg) {
367,268,352✔
1028
  if (TDMT_SCH_QUERY != pMsg->msgType && TDMT_SCH_MERGE_QUERY != pMsg->msgType) {
367,268,352✔
1029
    return 0;
54,629,656✔
1030
  }
1031

1032
  int32_t qType = 0;
312,662,071✔
1033
  return qWorkerPreprocessQueryMsg(pVnode->pQuery, pMsg, (TDMT_SCH_QUERY == pMsg->msgType), &qType);
312,662,875✔
1034
}
1035

1036
int32_t vnodeProcessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
367,337,258✔
1037
  vTrace("message in vnode query queue is processing");
367,337,258✔
1038
  if (pMsg->msgType == TDMT_VND_TMQ_CONSUME && !syncIsReadyForRead(pVnode->sync)) {
367,337,258✔
1039
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
356,912✔
1040
    return 0;
356,912✔
1041
  }
1042

1043
  if (pMsg->msgType == TDMT_VND_TMQ_CONSUME && !pVnode->restored) {
366,981,623✔
1044
    vnodeRedirectRpcMsg(pVnode, pMsg, TSDB_CODE_SYN_RESTORING);
×
1045
    return 0;
×
1046
  }
1047

1048
  SReadHandle handle = {0};
366,979,318✔
1049
  handle.vnode = pVnode;
366,976,045✔
1050
  handle.pMsgCb = &pVnode->msgCb;
366,976,045✔
1051
  handle.pWorkerCb = pInfo->workerCb;
366,973,570✔
1052
  initStorageAPI(&handle.api);
366,975,318✔
1053
  int32_t code = TSDB_CODE_SUCCESS;
366,908,359✔
1054
  bool    redirected = false;
366,908,359✔
1055

1056
  switch (pMsg->msgType) {
366,908,359✔
1057
    case TDMT_SCH_QUERY:
252,740,813✔
1058
      if (!syncIsReadyForRead(pVnode->sync)) {
252,740,813✔
1059
        pMsg->code = (terrno) ? terrno : TSDB_CODE_SYN_NOT_LEADER;
1,536,953✔
1060
        redirected = true;
1,536,953✔
1061
      }
1062
      code = qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
252,763,368✔
1063
      if (redirected) {
252,760,150✔
1064
        vnodeRedirectRpcMsg(pVnode, pMsg, pMsg->code);
1,536,953✔
1065
        return 0;
1,536,953✔
1066
      }
1067

1068
      return code;
251,223,197✔
1069
    case TDMT_SCH_MERGE_QUERY:
59,932,515✔
1070
      return qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
59,932,515✔
1071
    case TDMT_SCH_QUERY_CONTINUE:
10,649,271✔
1072
      return qWorkerProcessCQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
10,649,271✔
1073
    case TDMT_VND_TMQ_CONSUME:
33,543,150✔
1074
      return tqProcessPollReq(pVnode->pTq, pMsg);
33,543,150✔
1075
    case TDMT_VND_TMQ_CONSUME_PUSH:
10,073,710✔
1076
      return tqProcessPollPush(pVnode->pTq);
10,073,710✔
1077
    default:
×
1078
      vError("unknown msg type:%d in query queue", pMsg->msgType);
×
1079
      return TSDB_CODE_APP_ERROR;
×
1080
  }
1081
}
1082

1083
int32_t vnodeProcessFetchMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
799,622,534✔
1084
  int32_t code = TSDB_CODE_SUCCESS;
799,622,534✔
1085
  vTrace("vgId:%d, msg:%p in fetch queue is processing", pVnode->config.vgId, pMsg);
799,622,534✔
1086
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
799,622,534✔
1087
       pMsg->msgType == TDMT_VND_BATCH_META || pMsg->msgType == TDMT_VND_TABLE_NAME ||
559,202,584✔
1088
       pMsg->msgType == TDMT_VND_VSUBTABLES_META || pMsg->msgType == TDMT_VND_VSTB_REF_DBS) &&
537,313,879✔
1089
      !syncIsReadyForRead(pVnode->sync)) {
262,339,916✔
1090
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
599,048✔
1091
    return 0;
599,048✔
1092
  }
1093

1094
  switch (pMsg->msgType) {
799,076,082✔
1095
    case TDMT_SCH_FETCH:
315,951,565✔
1096
    case TDMT_SCH_MERGE_FETCH:
1097
      return qWorkerProcessFetchMsg(pVnode, pVnode->pQuery, pMsg, 0);
315,951,565✔
1098
    case TDMT_SCH_FETCH_RSP:
×
1099
      return qWorkerProcessRspMsg(pVnode, pVnode->pQuery, pMsg, 0);
×
1100
    // case TDMT_SCH_CANCEL_TASK:
1101
    //   return qWorkerProcessCancelMsg(pVnode, pVnode->pQuery, pMsg, 0);
1102
    case TDMT_SCH_DROP_TASK:
312,437,569✔
1103
      return qWorkerProcessDropMsg(pVnode, pVnode->pQuery, pMsg, 0);
312,437,569✔
1104
    case TDMT_SCH_TASK_NOTIFY:
9,450✔
1105
      return qWorkerProcessNotifyMsg(pVnode, pVnode->pQuery, pMsg, 0);
9,450✔
1106
    case TDMT_SCH_QUERY_HEARTBEAT:
148,432,900✔
1107
      return qWorkerProcessHbMsg(pVnode, pVnode->pQuery, pMsg, 0);
148,432,900✔
1108
    case TDMT_VND_TABLE_META:
745,421✔
1109
    case TDMT_VND_TABLE_NAME:
1110
      return vnodeGetTableMeta(pVnode, pMsg, true);
745,421✔
1111
    case TDMT_VND_TABLE_CFG:
×
1112
      return vnodeGetTableCfg(pVnode, pMsg, true);
×
1113
    case TDMT_VND_BATCH_META: {
21,291,545✔
1114
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.fetch_batch_meta_time, METRIC_LEVEL_LOW,
21,291,545✔
1115
                           { code = vnodeGetBatchMeta(pVnode, pMsg); });
1116
      METRICS_UPDATE(pVnode->writeMetrics.fetch_batch_meta_count, METRIC_LEVEL_LOW, 1);
21,291,500✔
1117
      return code;
21,289,762✔
1118
    }
1119
    case TDMT_VND_VSUBTABLES_META:
×
1120
      return vnodeGetVSubtablesMeta(pVnode, pMsg);
×
1121
    case TDMT_VND_VSTB_REF_DBS:
×
1122
      return vnodeGetVStbRefDbs(pVnode, pMsg);
×
1123
    case TDMT_VND_QUERY_SCAN_PROGRESS:
470✔
1124
      return vnodeQueryScanProgress(pVnode, pMsg);
470✔
1125
#ifdef TD_ENTERPRISE
1126
    case TDMT_VND_QUERY_COMPACT_PROGRESS:
97,424✔
1127
      return vnodeQueryCompactProgress(pVnode, pMsg);
97,424✔
1128

1129
    case TDMT_VND_LIST_SSMIGRATE_FILESETS:
×
1130
      return vnodeListSsMigrateFileSets(pVnode, pMsg);
×
1131

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

1153
void vnodeUpdateMetaRsp(SVnode *pVnode, STableMetaRsp *pMetaRsp) {
57,881,114✔
1154
  if (NULL == pMetaRsp) {
57,881,114✔
1155
    return;
×
1156
  }
1157

1158
  tstrncpy(pMetaRsp->dbFName, pVnode->config.dbname, TSDB_DB_FNAME_LEN);
57,881,114✔
1159
  pMetaRsp->dbId = pVnode->config.dbId;
57,881,814✔
1160
  pMetaRsp->vgId = TD_VID(pVnode);
57,881,816✔
1161
  pMetaRsp->precision = pVnode->config.tsdbCfg.precision;
57,880,425✔
1162
}
1163

1164
extern int32_t vnodeAsyncRetention(SVnode *pVnode, STimeWindow tw, int8_t optrType, int8_t triggerType);
1165

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

1172
  int32_t     code = 0;
39,706✔
1173
  SVTrimDbReq trimReq = {0};
39,706✔
1174

1175
  // decode
1176
  if (tDeserializeSVTrimDbReq(pReq, len, &trimReq) != 0) {
39,706✔
1177
    code = TSDB_CODE_INVALID_MSG;
×
1178
    goto _exit;
×
1179
  }
1180

1181
  vInfo("vgId:%d, process trim vnode request, time:%" PRIi64 ",%" PRIi64 ", optr:%d, trigger:%d", pVnode->config.vgId,
39,706✔
1182
        trimReq.tw.skey, trimReq.tw.ekey, (int32_t)trimReq.optrType, (int32_t)trimReq.triggerType);
1183

1184
  code = vnodeAsyncRetention(pVnode, trimReq.tw, (int8_t)trimReq.optrType, (int8_t)trimReq.triggerType);
39,706✔
1185

1186
_exit:
39,706✔
1187
  return code;
39,706✔
1188
}
1189

1190
extern int32_t vnodeAsyncSsMigrateFileSet(SVnode *pVnode, SSsMigrateFileSetReq *pReq);
1191

1192
static int32_t vnodeProcessTrimWalReq(SVnode *pVnode, void *pReq, int32_t len, SRpcMsg *pRsp) {
3,204✔
1193
  int32_t code = 0;
3,204✔
1194

1195
  vInfo("vgId:%d, process trim wal request, force clean expired WAL files by triggering commit with forceTrim",
3,204✔
1196
        pVnode->config.vgId);
1197

1198
  // Trigger a commit with forceTrim flag
1199
  // This will properly calculate ver through sync layer and apply forceTrim during snapshot
1200
  code = vnodeAsyncCommitEx(pVnode, true);
3,204✔
1201
  if (code != TSDB_CODE_SUCCESS) {
3,204✔
1202
    vError("vgId:%d, failed to trigger trim wal commit since %s", pVnode->config.vgId, tstrerror(code));
×
1203
  } else {
1204
    vInfo("vgId:%d, successfully triggered trim wal commit", pVnode->config.vgId);
3,204✔
1205
  }
1206

1207
  return code;
3,204✔
1208
}
1209

1210
extern int32_t vnodeAsyncS3Migrate(SVnode *pVnode, int64_t now);
1211

1212
static int32_t vnodeProcessSsMigrateFileSetReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1213
  int32_t code = 0;
×
1214

1215
  SSsMigrateFileSetReq req = {0};
×
1216
  SSsMigrateFileSetRsp rsp = {0};
×
1217
  pRsp->msgType = TDMT_VND_SSMIGRATE_FILESET_RSP;
×
1218
  pRsp->code = 0;
×
1219
  pRsp->pCont = NULL;
×
1220
  pRsp->contLen = 0;
×
1221

1222
  if (tDeserializeSSsMigrateFileSetReq(pReq, len, &req) != 0) {
×
1223
    code = TSDB_CODE_INVALID_MSG;
×
1224
    goto _exit;
×
1225
  }
1226

1227
  vInfo("vgId:%d, ssmigrate:%d, fid:%d, process ssmigrate fileset request, time:%" PRId64, pVnode->config.vgId,
×
1228
        req.ssMigrateId, req.fid, req.startTimeSec);
1229
  rsp.ssMigrateId = req.ssMigrateId;
×
1230
  rsp.vgId = TD_VID(pVnode);
×
1231
  rsp.nodeId = req.nodeId;
×
1232
  rsp.fid = req.fid;
×
1233

1234
  code = vnodeAsyncSsMigrateFileSet(pVnode, &req);
×
1235
  if (code != TSDB_CODE_SUCCESS) {
×
1236
    vError("vgId:%d, failed to async ssmigrate since %s", TD_VID(pVnode), tstrerror(code));
×
1237
    pRsp->code = code;
×
1238
    goto _exit;
×
1239
  }
1240

1241
  pRsp->code = TSDB_CODE_SUCCESS;
×
1242
  pRsp->contLen = tSerializeSSsMigrateFileSetRsp(NULL, 0, &rsp);
×
1243
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
×
1244
  if (pRsp->pCont == NULL) {
×
1245
    vError("vgId:%d, failed to allocate memory for ssmigrate fileset response", TD_VID(pVnode));
×
1246
    code = TSDB_CODE_OUT_OF_MEMORY;
×
1247
    goto _exit;
×
1248
  }
1249
  TAOS_UNUSED(tSerializeSSsMigrateFileSetRsp(pRsp->pCont, pRsp->contLen, &rsp));
×
1250

1251
_exit:
×
1252
  pRsp->code = code;
×
1253
  return code;
×
1254
}
1255

1256
extern int32_t vnodeFollowerSsMigrate(SVnode *pVnode, SSsMigrateProgress *pReq);
1257

1258
static int32_t vnodeProcessFollowerSsMigrateReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1259
  int32_t            code = 0;
×
1260
  SSsMigrateProgress req = {0};
×
1261

1262
  // decode
1263
  if (tDeserializeSSsMigrateProgress(pReq, len, &req) != 0) {
×
1264
    code = TSDB_CODE_INVALID_MSG;
×
1265
    goto _exit;
×
1266
  }
1267

1268
  code = vnodeFollowerSsMigrate(pVnode, &req);
×
1269

1270
_exit:
×
1271
  pRsp->code = code;
×
1272
  return code;
×
1273
}
1274

1275
extern int32_t vnodeKillSsMigrate(SVnode *pVnode, SVnodeKillSsMigrateReq *pReq);
1276

1277
static int32_t vnodeProcessKillSsMigrateReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1278
  int32_t          code = 0;
×
1279
  SVnodeKillSsMigrateReq req = {0};
×
1280

1281
  // decode
1282
  if (tDeserializeSVnodeKillSsMigrateReq(pReq, len, &req) != 0) {
×
1283
    code = TSDB_CODE_INVALID_MSG;
×
1284
    goto _exit;
×
1285
  }
1286

1287
  code = vnodeKillSsMigrate(pVnode, &req);
×
1288

1289
_exit:
×
1290
  pRsp->code = code;
×
1291
  return code;
×
1292
}
1293

1294
static int32_t vnodeProcessDropTtlTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1295
  int               ret = 0;
×
1296
  SVDropTtlTableReq ttlReq = {0};
×
1297
  if (tDeserializeSVDropTtlTableReq(pReq, len, &ttlReq) != 0) {
×
1298
    ret = TSDB_CODE_INVALID_MSG;
×
1299
    goto end;
×
1300
  }
1301

1302
  if (ttlReq.nUids != taosArrayGetSize(ttlReq.pTbUids)) {
×
1303
    ret = TSDB_CODE_INVALID_MSG;
×
1304
    goto end;
×
1305
  }
1306

1307
  if (ttlReq.nUids != 0) {
×
1308
    vInfo("vgId:%d, process drop ttl table request, time:%d, ntbUids:%d", pVnode->config.vgId, ttlReq.timestampSec,
×
1309
          ttlReq.nUids);
1310
  }
1311

1312
  if (ttlReq.nUids > 0) {
×
1313
    int32_t code = metaDropMultipleTables(pVnode->pMeta, ver, ttlReq.pTbUids);
×
1314
    if (code) return code;
×
1315

1316
    code = tqUpdateTbUidList(pVnode->pTq, ttlReq.pTbUids, false);
×
1317
    if (code) {
×
1318
      vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1319
    }
1320
  }
1321

1322
end:
×
1323
  taosArrayDestroy(ttlReq.pTbUids);
×
1324
  return ret;
×
1325
}
1326

1327
static int32_t vnodeProcessFetchTtlExpiredTbs(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
12,388✔
1328
  int32_t                 code = -1;
12,388✔
1329
  SMetaReader             mr = {0};
12,388✔
1330
  SVDropTtlTableReq       ttlReq = {0};
12,388✔
1331
  SVFetchTtlExpiredTbsRsp rsp = {0};
12,388✔
1332
  SEncoder                encoder = {0};
12,388✔
1333
  SArray                 *pNames = NULL;
12,388✔
1334
  pRsp->msgType = TDMT_VND_FETCH_TTL_EXPIRED_TBS_RSP;
12,388✔
1335
  pRsp->code = TSDB_CODE_SUCCESS;
12,388✔
1336
  pRsp->pCont = NULL;
12,388✔
1337
  pRsp->contLen = 0;
12,388✔
1338

1339
  if (tDeserializeSVDropTtlTableReq(pReq, len, &ttlReq) != 0) {
12,388✔
1340
    terrno = TSDB_CODE_INVALID_MSG;
×
1341
    goto _end;
×
1342
  }
1343

1344
  if (!(ttlReq.nUids == taosArrayGetSize(ttlReq.pTbUids))) {
12,388✔
1345
    terrno = TSDB_CODE_INVALID_MSG;
×
1346
    goto _end;
×
1347
  }
1348

1349
  tb_uid_t    suid;
1350
  char        ctbName[TSDB_TABLE_NAME_LEN];
1351
  SVDropTbReq expiredTb = {.igNotExists = true};
12,388✔
1352
  metaReaderDoInit(&mr, pVnode->pMeta, 0);
12,388✔
1353
  rsp.vgId = TD_VID(pVnode);
12,388✔
1354
  rsp.pExpiredTbs = taosArrayInit(ttlReq.nUids, sizeof(SVDropTbReq));
12,388✔
1355
  if (!rsp.pExpiredTbs) goto _end;
12,388✔
1356

1357
  pNames = taosArrayInit(ttlReq.nUids, TSDB_TABLE_NAME_LEN);
12,388✔
1358
  if (!pNames) {
12,388✔
1359
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1360
    goto _end;
×
1361
  }
1362
  char buf[TSDB_TABLE_NAME_LEN];
12,388✔
1363
  for (int32_t i = 0; i < ttlReq.nUids; ++i) {
60,706✔
1364
    tb_uid_t *uid = taosArrayGet(ttlReq.pTbUids, i);
48,318✔
1365
    expiredTb.suid = *uid;
48,318✔
1366
    terrno = metaReaderGetTableEntryByUid(&mr, *uid);
48,318✔
1367
    if (terrno < 0) goto _end;
48,318✔
1368
    tstrncpy(buf, mr.me.name, TSDB_TABLE_NAME_LEN);
48,318✔
1369
    void *p = taosArrayPush(pNames, buf);
48,318✔
1370
    if (p == NULL) {
48,318✔
1371
      goto _end;
×
1372
    }
1373

1374
    expiredTb.name = p;
48,318✔
1375
    if (mr.me.type == TSDB_CHILD_TABLE) {
48,318✔
1376
      expiredTb.suid = mr.me.ctbEntry.suid;
32,244✔
1377
    }
1378

1379
    if (taosArrayPush(rsp.pExpiredTbs, &expiredTb) == NULL) {
96,636✔
1380
      goto _end;
×
1381
    }
1382
  }
1383

1384
  int32_t ret = 0;
12,388✔
1385
  tEncodeSize(tEncodeVFetchTtlExpiredTbsRsp, &rsp, pRsp->contLen, ret);
12,388✔
1386
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
12,388✔
1387
  if (pRsp->pCont == NULL) {
12,388✔
1388
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1389
    code = -1;
×
1390
    goto _end;
×
1391
  }
1392
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
12,388✔
1393
  terrno = tEncodeVFetchTtlExpiredTbsRsp(&encoder, &rsp);
12,388✔
1394
  tEncoderClear(&encoder);
12,388✔
1395

1396
  if (terrno == 0) code = 0;
12,388✔
1397
_end:
12,388✔
1398
  metaReaderClear(&mr);
12,388✔
1399
  tFreeFetchTtlExpiredTbsRsp(&rsp);
12,388✔
1400
  taosArrayDestroy(ttlReq.pTbUids);
12,388✔
1401
  if (pNames) taosArrayDestroy(pNames);
12,388✔
1402
  pRsp->code = terrno;
12,388✔
1403
  return code;
12,388✔
1404
}
1405

1406
static int32_t vnodeProcessCreateStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
4,710,592✔
1407
  int32_t        code = 0;
4,710,592✔
1408
  SVCreateStbReq req = {0};
4,710,592✔
1409
  SDecoder       coder;
4,710,640✔
1410

1411
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
4,710,987✔
1412
  pRsp->code = TSDB_CODE_SUCCESS;
4,710,987✔
1413
  pRsp->pCont = NULL;
4,710,987✔
1414
  pRsp->contLen = 0;
4,710,987✔
1415

1416
  // decode and process req
1417
  tDecoderInit(&coder, pReq, len);
4,710,987✔
1418

1419
  code = tDecodeSVCreateStbReq(&coder, &req);
4,710,987✔
1420
  if (code) {
4,707,332✔
1421
    pRsp->code = code;
×
1422
    goto _err;
×
1423
  }
1424

1425
  code = metaCreateSuperTable(pVnode->pMeta, ver, &req);
4,707,332✔
1426
  if (code) {
4,710,987✔
1427
    pRsp->code = code;
2,860✔
1428
    goto _err;
2,860✔
1429
  }
1430

1431
  tDecoderClear(&coder);
4,708,127✔
1432
  return 0;
4,708,102✔
1433

1434
_err:
2,860✔
1435
  tDecoderClear(&coder);
2,860✔
1436
  return code;
2,860✔
1437
}
1438

1439
static int32_t vnodeProcessCreateTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
36,558,492✔
1440
                                       SRpcMsg *pOriginRpc) {
1441
  SDecoder           decoder = {0};
36,558,492✔
1442
  SEncoder           encoder = {0};
36,558,492✔
1443
  int32_t            rcode = 0;
36,558,492✔
1444
  SVCreateTbBatchReq req = {0};
36,558,492✔
1445
  SVCreateTbReq     *pCreateReq;
1446
  SVCreateTbBatchRsp rsp = {0};
36,558,492✔
1447
  SVCreateTbRsp      cRsp = {0};
36,558,492✔
1448
  char               tbName[TSDB_TABLE_FNAME_LEN];
36,557,698✔
1449
  SArray            *tbUids = NULL;
36,558,492✔
1450
  SArray            *tbNames = NULL;
36,558,492✔
1451
  int64_t            tss = taosGetTimestampMs();
36,558,492✔
1452
  pRsp->msgType = TDMT_VND_CREATE_TABLE_RSP;
36,558,492✔
1453
  pRsp->code = TSDB_CODE_SUCCESS;
36,558,492✔
1454
  pRsp->pCont = NULL;
36,558,492✔
1455
  pRsp->contLen = 0;
36,558,492✔
1456

1457
  // decode
1458
  tDecoderInit(&decoder, pReq, len);
36,558,090✔
1459
  if (tDecodeSVCreateTbBatchReq(&decoder, &req) < 0) {
36,558,492✔
1460
    rcode = -1;
×
1461
    terrno = TSDB_CODE_INVALID_MSG;
×
1462
    goto _exit;
×
1463
  }
1464

1465
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(cRsp));
36,557,304✔
1466
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
36,557,492✔
1467
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
36,554,566✔
1468
  if (rsp.pArray == NULL || tbUids == NULL || tbNames == NULL) {
36,556,518✔
UNCOV
1469
    rcode = -1;
×
UNCOV
1470
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1471
    goto _exit;
×
1472
  }
1473

1474
  // loop to create table
1475
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
80,213,619✔
1476
    pCreateReq = req.pReqs + iReq;
43,654,425✔
1477
    memset(&cRsp, 0, sizeof(cRsp));
43,651,092✔
1478

1479
    if (tsEnableAudit && tsEnableAuditCreateTable && tsAuditLevel >= AUDIT_LEVEL_CHILDTABLE) {
43,651,092✔
1480
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
61✔
1481
      if (str == NULL) {
61✔
1482
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1483
        rcode = -1;
×
1484
        goto _exit;
×
1485
      }
1486
      tstrncpy(str, pCreateReq->name, TSDB_TABLE_FNAME_LEN);
61✔
1487
      if (taosArrayPush(tbNames, &str) == NULL) {
61✔
1488
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1489
        rcode = -1;
×
1490
        goto _exit;
×
1491
      }
1492
    }
1493

1494
    // validate hash
1495
    (void)tsnprintf(tbName, TSDB_TABLE_FNAME_LEN, "%s.%s", pVnode->config.dbname, pCreateReq->name);
43,651,092✔
1496
    if (vnodeValidateTableHash(pVnode, tbName) < 0) {
43,655,381✔
1497
      cRsp.code = TSDB_CODE_VND_HASH_MISMATCH;
×
1498
      if (taosArrayPush(rsp.pArray, &cRsp) == NULL) {
×
1499
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1500
        rcode = -1;
×
1501
        goto _exit;
×
1502
      }
1503
      vError("vgId:%d create-table:%s failed due to hash value mismatch", TD_VID(pVnode), tbName);
×
1504
      continue;
×
1505
    }
1506

1507
    // do create table
1508
    if (metaCreateTable2(pVnode->pMeta, ver, pCreateReq, &cRsp.pMeta) < 0) {
43,648,144✔
1509
      if (pCreateReq->flags & TD_CREATE_IF_NOT_EXISTS && terrno == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
14,938✔
1510
        cRsp.code = TSDB_CODE_SUCCESS;
3,864✔
1511
      } else {
1512
        cRsp.code = terrno;
11,074✔
1513
      }
1514
    } else {
1515
      cRsp.code = TSDB_CODE_SUCCESS;
43,640,219✔
1516
      if (taosArrayPush(tbUids, &pCreateReq->uid) == NULL) {
87,282,364✔
1517
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1518
        rcode = -1;
×
1519
        goto _exit;
×
1520
      }
1521
      vnodeUpdateMetaRsp(pVnode, cRsp.pMeta);
43,642,145✔
1522
    }
1523

1524
    if (taosArrayPush(rsp.pArray, &cRsp) == NULL) {
87,313,572✔
1525
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1526
      rcode = -1;
×
1527
      goto _exit;
×
1528
    }
1529
  }
1530

1531
  vTrace("vgId:%d, add %d new created tables into query table list", TD_VID(pVnode), (int32_t)taosArrayGetSize(tbUids));
36,559,194✔
1532
  if (tqUpdateTbUidList(pVnode->pTq, tbUids, true) < 0) {
36,559,194✔
1533
    vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(terrno));
×
1534
  }
1535

1536
  // prepare rsp
1537
  int32_t ret = 0;
36,558,492✔
1538
  tEncodeSize(tEncodeSVCreateTbBatchRsp, &rsp, pRsp->contLen, ret);
36,558,492✔
1539
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
36,556,602✔
1540
  if (pRsp->pCont == NULL) {
36,557,494✔
1541
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1542
    rcode = -1;
×
1543
    goto _exit;
×
1544
  }
1545
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
36,556,498✔
1546
  if (tEncodeSVCreateTbBatchRsp(&encoder, &rsp) != 0) {
36,557,392✔
1547
    vError("vgId:%d, failed to encode create table batch response", TD_VID(pVnode));
×
1548
  }
1549

1550
  if (tsEnableAudit && tsEnableAuditCreateTable && tsAuditLevel >= AUDIT_LEVEL_CHILDTABLE) {
36,557,024✔
1551
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
61✔
1552

1553
    SName name = {0};
61✔
1554
    if (tNameFromString(&name, pVnode->config.dbname, T_NAME_ACCT | T_NAME_DB) < 0) {
61✔
1555
      vError("vgId:%d, failed to get name from string", TD_VID(pVnode));
×
1556
    }
1557

1558
    SStringBuilder sb = {0};
61✔
1559
    for (int32_t i = 0; i < tbNames->size; i++) {
122✔
1560
      char **key = (char **)taosArrayGet(tbNames, i);
61✔
1561
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
61✔
1562
      if (i < tbNames->size - 1) {
61✔
1563
        taosStringBuilderAppendChar(&sb, ',');
×
1564
      }
1565
      // taosMemoryFreeClear(*key);
1566
    }
1567

1568
    size_t len = 0;
61✔
1569
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
61✔
1570

1571
    if (strlen(RPC_MSG_USER(pOriginRpc)) > 0) {
61✔
1572
      int64_t tse = taosGetTimestampMs();
61✔
1573
      double  duration = (double)(tse - tss);
61✔
1574
      duration = duration / 1000;
61✔
1575
      auditAddRecord(pOriginRpc, clusterId, "createTable", name.dbname, "", keyJoined, len, duration, 0);
61✔
1576
    }
1577

1578
    taosStringBuilderDestroy(&sb);
61✔
1579
  }
1580

1581
_exit:
36,557,024✔
1582
  tDeleteSVCreateTbBatchReq(&req);
36,554,244✔
1583
  taosArrayDestroyEx(rsp.pArray, tFreeSVCreateTbRsp);
36,555,024✔
1584
  taosArrayDestroy(tbUids);
36,550,850✔
1585
  tDecoderClear(&decoder);
36,550,657✔
1586
  tEncoderClear(&encoder);
36,556,920✔
1587
  taosArrayDestroyP(tbNames, NULL);
36,557,810✔
1588
  return rcode;
36,552,667✔
1589
}
1590

1591
static int32_t vnodeProcessAlterStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
7,263,186✔
1592
  int32_t        code = 0;
7,263,186✔
1593
  SVCreateStbReq req = {0};
7,263,186✔
1594
  SDecoder       dc = {0};
7,263,186✔
1595

1596
  pRsp->msgType = TDMT_VND_ALTER_STB_RSP;
7,263,186✔
1597
  pRsp->code = TSDB_CODE_SUCCESS;
7,263,186✔
1598
  pRsp->pCont = NULL;
7,263,186✔
1599
  pRsp->contLen = 0;
7,263,186✔
1600

1601
  tDecoderInit(&dc, pReq, len);
7,263,186✔
1602

1603
  // decode req
1604
  code = tDecodeSVCreateStbReq(&dc, &req);
7,263,186✔
1605
  if (code) {
7,261,953✔
1606
    tDecoderClear(&dc);
×
1607
    return code;
×
1608
  }
1609

1610
  code = metaAlterSuperTable(pVnode->pMeta, ver, &req);
7,261,953✔
1611
  if (code) {
7,263,186✔
1612
    pRsp->code = code;
×
1613
    tDecoderClear(&dc);
×
1614
    return code;
×
1615
  }
1616

1617
  tDecoderClear(&dc);
7,263,186✔
1618

1619
  return 0;
7,263,186✔
1620
}
1621

1622
static int32_t vnodeProcessDropStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp, SRpcMsg *pOriginRpc) {
856,875✔
1623
  SVDropStbReq req = {0};
856,875✔
1624
  int32_t      rcode = TSDB_CODE_SUCCESS;
856,875✔
1625
  SDecoder     decoder = {0};
856,875✔
1626
  SArray      *tbUidList = NULL;
856,875✔
1627

1628
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
856,875✔
1629
  pRsp->pCont = NULL;
856,875✔
1630
  pRsp->contLen = 0;
856,875✔
1631

1632
  // decode request
1633
  tDecoderInit(&decoder, pReq, len);
856,875✔
1634
  if (tDecodeSVDropStbReq(&decoder, &req) < 0) {
856,875✔
1635
    rcode = TSDB_CODE_INVALID_MSG;
×
1636
    goto _exit;
×
1637
  }
1638

1639
  STraceId* trace = &(pOriginRpc->info.traceId);
856,875✔
1640

1641
  vInfo("vgId:%d, start to process vnode-drop-stb, QID:0x%" PRIx64 ":0x%" PRIx64 ", drop stb:%s", TD_VID(pVnode), trace ? trace->rootId : 0, 
856,875✔
1642
              trace ? trace->msgId : 0, req.name);
1643

1644
  // process request
1645
  tbUidList = taosArrayInit(8, sizeof(int64_t));
856,875✔
1646
  if (tbUidList == NULL) goto _exit;
856,875✔
1647
  if (metaDropSuperTable(pVnode->pMeta, ver, &req) < 0) {
856,875✔
1648
    rcode = terrno;
1,430✔
1649
    goto _exit;
1,430✔
1650
  }
1651

1652
  if (tqUpdateTbUidList(pVnode->pTq, tbUidList, false) < 0) {
855,445✔
1653
    rcode = terrno;
×
1654
    goto _exit;
×
1655
  }
1656

1657
  // return rsp
1658
_exit:
856,875✔
1659
  vInfo("vgId:%d, finished to process vnode-drop-stb, QID:0x%" PRIx64 ":0x%" PRIx64 ", drop stb:%s", TD_VID(pVnode), trace ? trace->rootId : 0, 
856,875✔
1660
              trace ? trace->msgId : 0, req.name);
1661
  if (tbUidList) taosArrayDestroy(tbUidList);
856,875✔
1662
  pRsp->code = rcode;
856,875✔
1663
  tDecoderClear(&decoder);
856,875✔
1664
  return 0;
856,875✔
1665
}
1666

1667
static int32_t vnodeProcessAlterTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
11,433,614✔
1668
  SVAlterTbReq  vAlterTbReq = {0};
11,433,614✔
1669
  SVAlterTbRsp  vAlterTbRsp = {0};
11,433,614✔
1670
  SDecoder      dc = {0};
11,433,614✔
1671
  int32_t       code = 0;
11,433,614✔
1672
  int32_t       lino = 0;
11,433,614✔
1673
  int32_t       ret;
1674
  SEncoder      ec = {0};
11,433,614✔
1675
  STableMetaRsp vMetaRsp = {0};
11,433,614✔
1676

1677
  pRsp->msgType = TDMT_VND_ALTER_TABLE_RSP;
11,433,614✔
1678
  pRsp->pCont = NULL;
11,433,614✔
1679
  pRsp->contLen = 0;
11,433,614✔
1680
  pRsp->code = TSDB_CODE_SUCCESS;
11,433,614✔
1681

1682
  tDecoderInit(&dc, pReq, len);
11,433,614✔
1683

1684
  // decode
1685
  if (tDecodeSVAlterTbReq(&dc, &vAlterTbReq) < 0) {
11,433,614✔
1686
    vAlterTbRsp.code = TSDB_CODE_INVALID_MSG;
×
1687
    tDecoderClear(&dc);
×
1688
    goto _exit;
×
1689
  }
1690

1691
  // process
1692
  if (metaAlterTable(pVnode->pMeta, ver, &vAlterTbReq, &vMetaRsp) < 0) {
11,433,614✔
1693
    vAlterTbRsp.code = terrno;
673,686✔
1694
    tDecoderClear(&dc);
673,686✔
1695
    goto _exit;
673,686✔
1696
  }
1697
  tDecoderClear(&dc);
10,759,928✔
1698

1699
  if (NULL != vMetaRsp.pSchemas) {
10,759,928✔
1700
    vnodeUpdateMetaRsp(pVnode, &vMetaRsp);
3,538,975✔
1701
    vAlterTbRsp.pMeta = &vMetaRsp;
3,538,975✔
1702
  }
1703

1704
  if (vAlterTbReq.action == TSDB_ALTER_TABLE_UPDATE_TAG_VAL ||
10,759,928✔
1705
      vAlterTbReq.action == TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL) {
3,571,280✔
1706
    int64_t uid = metaGetTableEntryUidByName(pVnode->pMeta, vAlterTbReq.tbName);
7,191,176✔
1707
    if (uid == 0) {
7,191,176✔
1708
      vError("vgId:%d, %s failed at %s:%d since table %s not found", TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
1709
             vAlterTbReq.tbName);
1710
      goto _exit;
×
1711
    }
1712

1713
    SArray *tbUids = taosArrayInit(4, sizeof(int64_t));
7,191,176✔
1714
    void   *p = taosArrayPush(tbUids, &uid);
7,191,176✔
1715
    TSDB_CHECK_NULL(p, code, lino, _exit, terrno);
7,191,176✔
1716

1717
    vDebug("vgId:%d, remove tags value altered table:%s from query table list", TD_VID(pVnode), vAlterTbReq.tbName);
7,191,176✔
1718
    if ((code = tqUpdateTbUidList(pVnode->pTq, tbUids, false)) < 0) {
7,191,176✔
1719
      vError("vgId:%d, failed to remove tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1720
    }
1721

1722
    vDebug("vgId:%d, try to add table:%s in query table list", TD_VID(pVnode), vAlterTbReq.tbName);
7,191,176✔
1723
    if ((code = tqUpdateTbUidList(pVnode->pTq, tbUids, true)) < 0) {
7,191,176✔
1724
      vError("vgId:%d, failed to add tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1725
    }
1726

1727
    taosArrayDestroy(tbUids);
7,191,176✔
1728
  }
1729

1730
_exit:
11,433,614✔
1731
  taosArrayDestroy(vAlterTbReq.pMultiTag);
11,433,614✔
1732
  tEncodeSize(tEncodeSVAlterTbRsp, &vAlterTbRsp, pRsp->contLen, ret);
11,433,614✔
1733
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
11,433,614✔
1734
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
11,433,614✔
1735
  if (tEncodeSVAlterTbRsp(&ec, &vAlterTbRsp) != 0) {
11,433,614✔
1736
    vError("vgId:%d, failed to encode alter table response", TD_VID(pVnode));
×
1737
  }
1738

1739
  tEncoderClear(&ec);
11,433,614✔
1740
  if (vMetaRsp.pSchemas) {
11,433,614✔
1741
    taosMemoryFree(vMetaRsp.pSchemas);
3,538,975✔
1742
    taosMemoryFree(vMetaRsp.pSchemaExt);
3,538,975✔
1743
  }
1744
  if (vMetaRsp.pColRefs) {
11,433,614✔
1745
    taosMemoryFree(vMetaRsp.pColRefs);
251,538✔
1746
  }
1747
  return 0;
11,433,614✔
1748
}
1749

1750
static int32_t vnodeProcessDropTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
1,277,323✔
1751
                                     SRpcMsg *pOriginRpc) {
1752
  SVDropTbBatchReq req = {0};
1,277,323✔
1753
  SVDropTbBatchRsp rsp = {0};
1,277,323✔
1754
  SDecoder         decoder = {0};
1,277,323✔
1755
  SEncoder         encoder = {0};
1,277,323✔
1756
  int32_t          ret;
1757
  SArray          *tbUids = NULL;
1,277,323✔
1758
  SArray          *tbNames = NULL;
1,277,323✔
1759
  int64_t          tss = taosGetTimestampMs();
1,277,323✔
1760

1761
  pRsp->msgType = ((SRpcMsg *)pReq)->msgType + 1;
1,277,323✔
1762
  pRsp->pCont = NULL;
1,277,323✔
1763
  pRsp->contLen = 0;
1,277,323✔
1764
  pRsp->code = TSDB_CODE_SUCCESS;
1,277,323✔
1765

1766
  // decode req
1767
  tDecoderInit(&decoder, pReq, len);
1,277,323✔
1768
  ret = tDecodeSVDropTbBatchReq(&decoder, &req);
1,277,323✔
1769
  if (ret < 0) {
1,277,323✔
1770
    terrno = TSDB_CODE_INVALID_MSG;
×
1771
    pRsp->code = terrno;
×
1772
    goto _exit;
×
1773
  }
1774

1775
  // process req
1776
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
1,277,323✔
1777
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(SVDropTbRsp));
1,277,323✔
1778
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
1,277,323✔
1779
  if (tbUids == NULL || rsp.pArray == NULL || tbNames == NULL) goto _exit;
1,277,323✔
1780

1781
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
2,606,165✔
1782
    SVDropTbReq *pDropTbReq = req.pReqs + iReq;
1,328,842✔
1783
    SVDropTbRsp  dropTbRsp = {0};
1,328,842✔
1784
    tb_uid_t     tbUid = 0;
1,328,842✔
1785

1786
    /* code */
1787
    ret = metaDropTable2(pVnode->pMeta, ver, pDropTbReq);
1,328,842✔
1788
    if (ret < 0) {
1,328,842✔
1789
      if (pDropTbReq->igNotExists && terrno == TSDB_CODE_TDB_TABLE_NOT_EXIST) {
×
1790
        dropTbRsp.code = TSDB_CODE_SUCCESS;
×
1791
      } else {
1792
        dropTbRsp.code = terrno;
×
1793
      }
1794
    } else {
1795
      dropTbRsp.code = TSDB_CODE_SUCCESS;
1,328,842✔
1796
    }
1797

1798
    if (taosArrayPush(rsp.pArray, &dropTbRsp) == NULL) {
2,657,684✔
1799
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1800
      pRsp->code = terrno;
×
1801
      goto _exit;
×
1802
    }
1803

1804
    if (tsEnableAudit && tsEnableAuditCreateTable && tsAuditLevel >= AUDIT_LEVEL_CHILDTABLE) {
1,328,842✔
1805
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
×
1806
      if (str == NULL) {
×
1807
        pRsp->code = terrno;
×
1808
        goto _exit;
×
1809
      }
1810
      tstrncpy(str, pDropTbReq->name, TSDB_TABLE_FNAME_LEN);
×
1811
      if (taosArrayPush(tbNames, &str) == NULL) {
×
1812
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1813
        pRsp->code = terrno;
×
1814
        goto _exit;
×
1815
      }
1816
    }
1817
  }
1818

1819
  if (tqUpdateTbUidList(pVnode->pTq, tbUids, false) < 0) {
1,277,323✔
1820
    vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(terrno));
×
1821
  }
1822

1823
  if (tsEnableAudit && tsEnableAuditCreateTable && tsAuditLevel >= AUDIT_LEVEL_CHILDTABLE) {
1,277,323✔
1824
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
×
1825

1826
    SName name = {0};
×
1827
    if (tNameFromString(&name, pVnode->config.dbname, T_NAME_ACCT | T_NAME_DB) != 0) {
×
1828
      vError("vgId:%d, failed to get name from string", TD_VID(pVnode));
×
1829
    }
1830

1831
    SStringBuilder sb = {0};
×
1832
    for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
×
1833
      char **key = (char **)taosArrayGet(tbNames, iReq);
×
1834
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
×
1835
      if (iReq < req.nReqs - 1) {
×
1836
        taosStringBuilderAppendChar(&sb, ',');
×
1837
      }
1838
      taosMemoryFreeClear(*key);
×
1839
    }
1840

1841
    size_t len = 0;
×
1842
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
×
1843

1844
    if (strlen(RPC_MSG_USER(pOriginRpc)) > 0) {
×
1845
      int64_t tse = taosGetTimestampMs();
×
1846
      double  duration = (double)(tse - tss);
×
1847
      duration = duration / 1000;
×
1848
      auditAddRecord(pOriginRpc, clusterId, "dropTable", name.dbname, "", keyJoined, len, duration, 0);
×
1849
    }
1850

1851
    taosStringBuilderDestroy(&sb);
×
1852
  }
1853

1854
_exit:
1,277,323✔
1855
  taosArrayDestroy(tbUids);
1,277,323✔
1856
  tDecoderClear(&decoder);
1,277,323✔
1857
  tEncodeSize(tEncodeSVDropTbBatchRsp, &rsp, pRsp->contLen, ret);
1,277,323✔
1858
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
1,277,323✔
1859
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
1,277,323✔
1860
  if (tEncodeSVDropTbBatchRsp(&encoder, &rsp) != 0) {
1,277,323✔
1861
    vError("vgId:%d, failed to encode drop table batch response", TD_VID(pVnode));
×
1862
  }
1863
  tEncoderClear(&encoder);
1,277,323✔
1864
  taosArrayDestroy(rsp.pArray);
1,277,323✔
1865
  taosArrayDestroy(tbNames);
1,277,323✔
1866
  return 0;
1,277,323✔
1867
}
1868

1869
static int32_t vnodeProcessCreateRsmaReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
22,020✔
1870
  int32_t         code = 0, lino = 0;
22,020✔
1871
  SVCreateRsmaReq req = {0};
22,020✔
1872
  TAOS_CHECK_EXIT(tDeserializeSVCreateRsmaReq(pReq, len, &req));
22,020✔
1873
  TAOS_CHECK_EXIT(metaCreateRsma(pVnode->pMeta, ver, &req));
22,020✔
1874
_exit:
22,020✔
1875
  pRsp->msgType = TDMT_VND_CREATE_RSMA_RSP;
22,020✔
1876
  pRsp->pCont = NULL;
22,020✔
1877
  pRsp->code = code;
22,020✔
1878
  pRsp->contLen = 0;
22,020✔
1879
  tFreeSVCreateRsmaReq(&req);
22,020✔
1880
  return code;
22,020✔
1881
}
1882

1883
static int32_t vnodeProcessDropRsmaReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
4,404✔
1884
  int32_t       code = 0, lino = 0;
4,404✔
1885
  SVDropRsmaReq req = {0};
4,404✔
1886
  TAOS_CHECK_EXIT(tDeserializeSVDropRsmaReq(pReq, len, &req));
4,404✔
1887
  TAOS_CHECK_EXIT(metaDropRsma(pVnode->pMeta, ver, &req));
4,404✔
1888
_exit:
4,404✔
1889
  pRsp->msgType = TDMT_VND_DROP_RSMA_RSP;
4,404✔
1890
  pRsp->pCont = NULL;
4,404✔
1891
  pRsp->code = code;
4,404✔
1892
  pRsp->contLen = 0;
4,404✔
1893
  return 0;
4,404✔
1894
}
1895

1896
static int32_t vnodeProcessAlterRsmaReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
11,744✔
1897
  int32_t         code = 0, lino = 0;
11,744✔
1898
  SVAlterRsmaReq req = {0};
11,744✔
1899
  TAOS_CHECK_EXIT(tDeserializeSVAlterRsmaReq(pReq, len, &req));
11,744✔
1900
  TAOS_CHECK_EXIT(metaAlterRsma(pVnode->pMeta, ver, &req));
11,744✔
1901
_exit:
11,744✔
1902
  pRsp->msgType = TDMT_VND_ALTER_RSMA_RSP;
11,744✔
1903
  pRsp->pCont = NULL;
11,744✔
1904
  pRsp->code = code;
11,744✔
1905
  pRsp->contLen = 0;
11,744✔
1906
  tFreeSVAlterRsmaReq(&req);
11,744✔
1907
  return code;
11,744✔
1908
}
1909

1910
#ifdef BUILD_NO_CALL
1911
static int32_t vnodeDebugPrintSingleSubmitMsg(SMeta *pMeta, SSubmitBlk *pBlock, SSubmitMsgIter *msgIter,
1912
                                              const char *tags) {
1913
  SSubmitBlkIter blkIter = {0};
1914
  STSchema      *pSchema = NULL;
1915
  tb_uid_t       suid = 0;
1916
  STSRow        *row = NULL;
1917
  int32_t        rv = -1;
1918

1919
  tInitSubmitBlkIter(msgIter, pBlock, &blkIter);
1920
  if (blkIter.row == NULL) return 0;
1921

1922
  int32_t code = metaGetTbTSchemaNotNull(pMeta, msgIter->suid, TD_ROW_SVER(blkIter.row), 1,
1923
                                         &pSchema);  // TODO: use the real schema
1924
  if (TSDB_CODE_SUCCESS != code) {
1925
    printf("%s:%d no valid schema\n", tags, __LINE__);
1926
    return code;
1927
  }
1928

1929
  suid = msgIter->suid;
1930
  rv = TD_ROW_SVER(blkIter.row);
1931

1932
  char __tags[128] = {0};
1933
  snprintf(__tags, 128, "%s: uid %" PRIi64 " ", tags, msgIter->uid);
1934
  while ((row = tGetSubmitBlkNext(&blkIter))) {
1935
    tdSRowPrint(row, pSchema, __tags);
1936
  }
1937

1938
  taosMemoryFreeClear(pSchema);
1939

1940
  return TSDB_CODE_SUCCESS;
1941
}
1942
#endif
1943
typedef struct SSubmitReqConvertCxt {
1944
  SSubmitMsgIter msgIter;
1945
  SSubmitBlk    *pBlock;
1946
  SSubmitBlkIter blkIter;
1947
  STSRow        *pRow;
1948
  STSRowIter     rowIter;
1949
  SSubmitTbData *pTbData;
1950
  STSchema      *pTbSchema;
1951
  SArray        *pColValues;
1952
} SSubmitReqConvertCxt;
1953

1954
static int32_t vnodeResetTableCxt(SMeta *pMeta, SSubmitReqConvertCxt *pCxt) {
×
1955
  taosMemoryFreeClear(pCxt->pTbSchema);
×
1956
  int32_t code = metaGetTbTSchemaNotNull(pMeta, pCxt->msgIter.suid > 0 ? pCxt->msgIter.suid : pCxt->msgIter.uid,
×
1957
                                         pCxt->msgIter.sversion, 1, &pCxt->pTbSchema);
1958
  if (TSDB_CODE_SUCCESS != code) {
×
1959
    return code;
×
1960
  }
1961
  tdSTSRowIterInit(&pCxt->rowIter, pCxt->pTbSchema);
×
1962

1963
  tDestroySubmitTbData(pCxt->pTbData, TSDB_MSG_FLG_ENCODE);
×
1964
  if (NULL == pCxt->pTbData) {
×
1965
    pCxt->pTbData = taosMemoryCalloc(1, sizeof(SSubmitTbData));
×
1966
    if (NULL == pCxt->pTbData) {
×
1967
      return terrno;
×
1968
    }
1969
  }
1970
  pCxt->pTbData->flags = 0;
×
1971
  pCxt->pTbData->suid = pCxt->msgIter.suid;
×
1972
  pCxt->pTbData->uid = pCxt->msgIter.uid;
×
1973
  pCxt->pTbData->sver = pCxt->msgIter.sversion;
×
1974
  pCxt->pTbData->pCreateTbReq = NULL;
×
1975
  pCxt->pTbData->aRowP = taosArrayInit(128, POINTER_BYTES);
×
1976
  if (NULL == pCxt->pTbData->aRowP) {
×
1977
    return terrno;
×
1978
  }
1979

1980
  taosArrayDestroy(pCxt->pColValues);
×
1981
  pCxt->pColValues = taosArrayInit(pCxt->pTbSchema->numOfCols, sizeof(SColVal));
×
1982
  if (NULL == pCxt->pColValues) {
×
1983
    return terrno;
×
1984
  }
1985
  for (int32_t i = 0; i < pCxt->pTbSchema->numOfCols; ++i) {
×
1986
    SColVal val = COL_VAL_NONE(pCxt->pTbSchema->columns[i].colId, pCxt->pTbSchema->columns[i].type);
×
1987
    if (taosArrayPush(pCxt->pColValues, &val) == NULL) {
×
1988
      return terrno;
×
1989
    }
1990
  }
1991

1992
  return TSDB_CODE_SUCCESS;
×
1993
}
1994

1995
static void vnodeDestroySubmitReqConvertCxt(SSubmitReqConvertCxt *pCxt) {
×
1996
  taosMemoryFreeClear(pCxt->pTbSchema);
×
1997
  tDestroySubmitTbData(pCxt->pTbData, TSDB_MSG_FLG_ENCODE);
×
1998
  taosMemoryFreeClear(pCxt->pTbData);
×
1999
  taosArrayDestroy(pCxt->pColValues);
×
2000
}
×
2001

2002
static int32_t vnodeCellValConvertToColVal(STColumn *pCol, SCellVal *pCellVal, SColVal *pColVal) {
×
2003
  if (tdValTypeIsNone(pCellVal->valType)) {
×
2004
    pColVal->flag = CV_FLAG_NONE;
×
2005
    return TSDB_CODE_SUCCESS;
×
2006
  }
2007

2008
  if (tdValTypeIsNull(pCellVal->valType)) {
×
2009
    pColVal->flag = CV_FLAG_NULL;
×
2010
    return TSDB_CODE_SUCCESS;
×
2011
  }
2012

2013
  if (IS_VAR_DATA_TYPE(pCol->type)) {
×
2014
    if (IS_STR_DATA_BLOB(pCol->type)) {
×
2015
      pColVal->value.nData = blobDataLen(pCellVal->val);
×
2016
      pColVal->value.pData = (uint8_t *)blobDataVal(pCellVal->val);
×
2017

2018
    } else {
2019
      pColVal->value.nData = varDataLen(pCellVal->val);
×
2020
      pColVal->value.pData = (uint8_t *)varDataVal(pCellVal->val);
×
2021
    }
2022
  } else if (TSDB_DATA_TYPE_FLOAT == pCol->type) {
×
2023
    float f = GET_FLOAT_VAL(pCellVal->val);
×
2024
    valueSetDatum(&pColVal->value, pCol->type, &f, sizeof(f));
×
2025
  } else if (TSDB_DATA_TYPE_DOUBLE == pCol->type) {
×
2026
    taosSetPInt64Aligned(&pColVal->value.val, (int64_t *)pCellVal->val);
×
2027
  } else {
2028
    valueSetDatum(&pColVal->value, pCol->type, pCellVal->val, tDataTypes[pCol->type].bytes);
×
2029
  }
2030

2031
  pColVal->flag = CV_FLAG_VALUE;
×
2032
  return TSDB_CODE_SUCCESS;
×
2033
}
2034

2035
static int32_t vnodeTSRowConvertToColValArray(SSubmitReqConvertCxt *pCxt) {
×
2036
  int32_t code = TSDB_CODE_SUCCESS;
×
2037
  tdSTSRowIterReset(&pCxt->rowIter, pCxt->pRow);
×
2038
  for (int32_t i = 0; TSDB_CODE_SUCCESS == code && i < pCxt->pTbSchema->numOfCols; ++i) {
×
2039
    STColumn *pCol = pCxt->pTbSchema->columns + i;
×
2040
    SCellVal  cellVal = {0};
×
2041
    if (!tdSTSRowIterFetch(&pCxt->rowIter, pCol->colId, pCol->type, &cellVal)) {
×
2042
      break;
×
2043
    }
2044
    code = vnodeCellValConvertToColVal(pCol, &cellVal, (SColVal *)taosArrayGet(pCxt->pColValues, i));
×
2045
  }
2046
  return code;
×
2047
}
2048

2049
static int32_t vnodeDecodeCreateTbReq(SSubmitReqConvertCxt *pCxt) {
×
2050
  if (pCxt->msgIter.schemaLen <= 0) {
×
2051
    return TSDB_CODE_SUCCESS;
×
2052
  }
2053

2054
  pCxt->pTbData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
×
2055
  if (NULL == pCxt->pTbData->pCreateTbReq) {
×
2056
    return terrno;
×
2057
  }
2058

2059
  SDecoder decoder = {0};
×
2060
  tDecoderInit(&decoder, (uint8_t *)pCxt->pBlock->data, pCxt->msgIter.schemaLen);
×
2061
  int32_t code = tDecodeSVCreateTbReq(&decoder, pCxt->pTbData->pCreateTbReq);
×
2062
  tDecoderClear(&decoder);
×
2063

2064
  return code;
×
2065
}
2066

2067
static int32_t vnodeSubmitReqConvertToSubmitReq2(SVnode *pVnode, SSubmitReq *pReq, SSubmitReq2 *pReq2) {
×
2068
  pReq2->aSubmitTbData = taosArrayInit(128, sizeof(SSubmitTbData));
×
2069
  if (NULL == pReq2->aSubmitTbData) {
×
2070
    return terrno;
×
2071
  }
2072

2073
  SSubmitReqConvertCxt cxt = {0};
×
2074

2075
  int32_t code = tInitSubmitMsgIter(pReq, &cxt.msgIter);
×
2076
  while (TSDB_CODE_SUCCESS == code) {
×
2077
    code = tGetSubmitMsgNext(&cxt.msgIter, &cxt.pBlock);
×
2078
    if (TSDB_CODE_SUCCESS == code) {
×
2079
      if (NULL == cxt.pBlock) {
×
2080
        break;
×
2081
      }
2082
      code = vnodeResetTableCxt(pVnode->pMeta, &cxt);
×
2083
    }
2084
    if (TSDB_CODE_SUCCESS == code) {
×
2085
      code = tInitSubmitBlkIter(&cxt.msgIter, cxt.pBlock, &cxt.blkIter);
×
2086
    }
2087
    if (TSDB_CODE_SUCCESS == code) {
×
2088
      code = vnodeDecodeCreateTbReq(&cxt);
×
2089
    }
2090
    while (TSDB_CODE_SUCCESS == code && (cxt.pRow = tGetSubmitBlkNext(&cxt.blkIter)) != NULL) {
×
2091
      code = vnodeTSRowConvertToColValArray(&cxt);
×
2092
      if (TSDB_CODE_SUCCESS == code) {
×
2093
        SRow **pNewRow = taosArrayReserve(cxt.pTbData->aRowP, 1);
×
2094

2095
        SRowBuildScanInfo sinfo = {0};
×
2096
        code = tRowBuild(cxt.pColValues, cxt.pTbSchema, pNewRow, &sinfo);
×
2097
      }
2098
    }
2099
    if (TSDB_CODE_SUCCESS == code) {
×
2100
      code = (NULL == taosArrayPush(pReq2->aSubmitTbData, cxt.pTbData) ? terrno : TSDB_CODE_SUCCESS);
×
2101
    }
2102
    if (TSDB_CODE_SUCCESS == code) {
×
2103
      taosMemoryFreeClear(cxt.pTbData);
×
2104
    }
2105
  }
2106

2107
  vnodeDestroySubmitReqConvertCxt(&cxt);
×
2108
  return code;
×
2109
}
2110

2111
static int32_t vnodeRebuildSubmitReqMsg(SSubmitReq2 *pSubmitReq, void **ppMsg) {
×
2112
  int32_t  code = TSDB_CODE_SUCCESS;
×
2113
  char    *pMsg = NULL;
×
2114
  uint32_t msglen = 0;
×
2115
  tEncodeSize(tEncodeSubmitReq, pSubmitReq, msglen, code);
×
2116
  if (TSDB_CODE_SUCCESS == code) {
×
2117
    pMsg = taosMemoryMalloc(msglen);
×
2118
    if (NULL == pMsg) {
×
2119
      code = terrno;
×
2120
    }
2121
  }
2122
  if (TSDB_CODE_SUCCESS == code) {
×
2123
    SEncoder encoder;
×
2124
    tEncoderInit(&encoder, (uint8_t *)pMsg, msglen);
×
2125
    code = tEncodeSubmitReq(&encoder, pSubmitReq);
×
2126
    tEncoderClear(&encoder);
×
2127
  }
2128
  if (TSDB_CODE_SUCCESS == code) {
×
2129
    *ppMsg = pMsg;
×
2130
  }
2131
  return code;
×
2132
}
2133

2134
static int32_t buildExistSubTalbeRsp(SVnode *pVnode, SSubmitTbData *pSubmitTbData, STableMetaRsp **ppRsp) {
59,141✔
2135
  int32_t code = 0;
59,141✔
2136
  int32_t lino = 0;
59,141✔
2137

2138
  SMetaEntry *pEntry = NULL;
59,141✔
2139
  code = metaFetchEntryByUid(pVnode->pMeta, pSubmitTbData->suid, &pEntry);
59,141✔
2140
  if (code) {
59,141✔
2141
    vError("vgId:%d, table uid:%" PRId64 " not exists, line:%d", TD_VID(pVnode), pSubmitTbData->uid, __LINE__);
×
2142
    TSDB_CHECK_CODE(code, lino, _exit);
×
2143
  }
2144
  if (pEntry->type != TSDB_SUPER_TABLE) {
59,141✔
2145
    vError("vgId:%d, table uid:%" PRId64 " exists, but is not super table, line:%d", TD_VID(pVnode), pSubmitTbData->uid,
×
2146
           __LINE__);
2147
    code = TSDB_CODE_STREAM_INSERT_SCHEMA_NOT_MATCH;
×
2148
    TSDB_CHECK_CODE(code, lino, _exit);
×
2149
  }
2150

2151
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
59,141✔
2152
  if (NULL == *ppRsp) {
59,141✔
2153
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2154
  }
2155
  (*ppRsp)->suid = pSubmitTbData->suid;
59,141✔
2156
  (*ppRsp)->tuid = pSubmitTbData->uid;
59,141✔
2157
  (*ppRsp)->sversion = pEntry->stbEntry.schemaRow.version;
59,141✔
2158
  (*ppRsp)->vgId = pVnode->config.vgId;
59,141✔
2159
  (*ppRsp)->numOfColumns = pEntry->stbEntry.schemaRow.nCols;
59,141✔
2160
  (*ppRsp)->numOfTags = pEntry->stbEntry.schemaTag.nCols;
59,141✔
2161
  (*ppRsp)->pSchemas =
59,141✔
2162
      taosMemoryCalloc(pEntry->stbEntry.schemaRow.nCols + pEntry->stbEntry.schemaTag.nCols, sizeof(SSchema));
59,141✔
2163
  if (NULL == (*ppRsp)->pSchemas) {
59,141✔
2164
    taosMemoryFree(*ppRsp);
×
2165
    *ppRsp = NULL;
×
2166
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2167
  }
2168
  memcpy((*ppRsp)->pSchemas, pEntry->stbEntry.schemaRow.pSchema, pEntry->stbEntry.schemaRow.nCols * sizeof(SSchema));
59,141✔
2169
  memcpy((*ppRsp)->pSchemas + pEntry->stbEntry.schemaRow.nCols, pEntry->stbEntry.schemaTag.pSchema,
59,141✔
2170
         pEntry->stbEntry.schemaTag.nCols * sizeof(SSchema));
59,141✔
2171
  if (pEntry->pExtSchemas != NULL) {
59,141✔
2172
    (*ppRsp)->pSchemaExt = taosMemoryCalloc(pEntry->colCmpr.nCols, sizeof(SExtSchema));
×
2173
    if (NULL == (*ppRsp)->pSchemaExt) {
×
2174
      taosMemoryFree((*ppRsp)->pSchemas);
×
2175
      taosMemoryFree(*ppRsp);
×
2176
      *ppRsp = NULL;
×
2177
      TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2178
    }
2179
    memcpy((*ppRsp)->pSchemaExt, pEntry->pExtSchemas, pEntry->colCmpr.nCols * sizeof(SExtSchema));
×
2180
  }
2181

2182
  if (pEntry->stbEntry.schemaRow.version != pSubmitTbData->sver) {
59,141✔
2183
    TSDB_CHECK_CODE(code = TSDB_CODE_TDB_TABLE_ALREADY_EXIST, lino, _exit);
418✔
2184
  }
2185
_exit:
59,141✔
2186
  metaFetchEntryFree(&pEntry);
59,141✔
2187
  if (code != TSDB_CODE_SUCCESS) {
59,141✔
2188
    vError("vgId:%d, failed to build exist sub table response, code:0x%0x, line:%d", TD_VID(pVnode), code, lino);
418✔
2189
  }
2190
  return code;
59,141✔
2191
}
2192

2193
static int32_t buildExistNormalTalbeRsp(SVnode *pVnode, int64_t uid, STableMetaRsp **ppRsp) {
34,068✔
2194
  int32_t code = 0;
34,068✔
2195
  int32_t lino = 0;
34,068✔
2196

2197
  SMetaEntry *pEntry = NULL;
34,068✔
2198
  code = metaFetchEntryByUid(pVnode->pMeta, uid, &pEntry);
34,068✔
2199
  if (code) {
34,068✔
2200
    vError("vgId:%d, table uid:%" PRId64 " not exists, line:%d", TD_VID(pVnode), uid, __LINE__);
×
2201
    TSDB_CHECK_CODE(code, lino, _exit);
×
2202
  }
2203
  if (pEntry->type != TSDB_NORMAL_TABLE) {
34,068✔
2204
    vError("vgId:%d, table uid:%" PRId64 " exists, but is not normal table, line:%d", TD_VID(pVnode), uid, __LINE__);
×
2205
    code = TSDB_CODE_STREAM_INSERT_SCHEMA_NOT_MATCH;
×
2206
    TSDB_CHECK_CODE(code, lino, _exit);
×
2207
  }
2208

2209
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
34,068✔
2210
  if (NULL == *ppRsp) {
34,068✔
2211
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2212
  }
2213

2214
  (*ppRsp)->tuid = pEntry->uid;
34,068✔
2215
  (*ppRsp)->vgId = pVnode->config.vgId;
34,068✔
2216
  (*ppRsp)->sversion = pEntry->ntbEntry.schemaRow.version;
34,068✔
2217
  (*ppRsp)->numOfColumns = pEntry->ntbEntry.schemaRow.nCols;
34,068✔
2218
  (*ppRsp)->pSchemas = taosMemoryCalloc(pEntry->ntbEntry.schemaRow.nCols, sizeof(SSchema));
34,068✔
2219
  if (NULL == (*ppRsp)->pSchemas) {
34,068✔
2220
    taosMemoryFree(*ppRsp);
×
2221
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2222
  }
2223
  memcpy((*ppRsp)->pSchemas, pEntry->ntbEntry.schemaRow.pSchema, pEntry->ntbEntry.schemaRow.nCols * sizeof(SSchema));
34,068✔
2224
  if (pEntry->pExtSchemas != NULL) {
34,068✔
2225
    (*ppRsp)->pSchemaExt = taosMemoryCalloc(pEntry->ntbEntry.schemaRow.nCols, sizeof(SSchemaExt));
×
2226
    if (NULL == (*ppRsp)->pSchemaExt) {
×
2227
      taosMemoryFree((*ppRsp)->pSchemas);
×
2228
      taosMemoryFree(*ppRsp);
×
2229
      TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2230
    }
2231
    memcpy((*ppRsp)->pSchemaExt, pEntry->pExtSchemas, pEntry->ntbEntry.schemaRow.nCols * sizeof(SSchemaExt));
×
2232
  }
2233

2234
_exit:
34,068✔
2235
  metaFetchEntryFree(&pEntry);
34,068✔
2236
  if (code != TSDB_CODE_SUCCESS) {
34,068✔
2237
    vError("vgId:%d, failed to build exist normal table response, code:%d, line:%d", TD_VID(pVnode), code, lino);
×
2238
  }
2239
  return code;
34,068✔
2240
}
2241

2242
static int32_t buildExistTableInStreamRsp(SVnode *pVnode, SSubmitTbData *pSubmitTbData, STableMetaRsp **ppRsp) {
282,834✔
2243
  if (pSubmitTbData->pCreateTbReq->flags & TD_CREATE_NORMAL_TB_IN_STREAM) {
282,834✔
2244
    int32_t code = buildExistNormalTalbeRsp(pVnode, pSubmitTbData->uid, ppRsp);
34,068✔
2245
    if (code) {
34,068✔
2246
      vError("vgId:%d, table uid:%" PRId64 " not exists, line:%d", TD_VID(pVnode), pSubmitTbData->uid, __LINE__);
×
2247
      return code;
×
2248
    }
2249
    return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
34,068✔
2250
  } else if (pSubmitTbData->pCreateTbReq->flags & TD_CREATE_SUB_TB_IN_STREAM) {
248,766✔
2251
    return buildExistSubTalbeRsp(pVnode, pSubmitTbData, ppRsp);
59,141✔
2252
  }
2253
  return TSDB_CODE_SUCCESS;
189,664✔
2254
}
2255

2256
static int32_t vnodeHandleAutoCreateTable(SVnode      *pVnode,    // vnode
646,025,643✔
2257
                                          int64_t      version,   // version
2258
                                          SSubmitReq2 *pRequest,  // request
2259
                                          SSubmitRsp2 *pResponse  // response
2260
) {
2261
  int32_t code = TSDB_CODE_SUCCESS;
646,025,643✔
2262
  int32_t numTbData = taosArrayGetSize(pRequest->aSubmitTbData);
646,025,643✔
2263
  SArray *newTbUids = NULL;
646,025,282✔
2264

2265
  for (int32_t i = 0; i < numTbData; ++i) {
1,317,946,978✔
2266
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
671,957,085✔
2267

2268
    if (pTbData->pCreateTbReq == NULL) {
671,957,443✔
2269
      continue;
660,920,978✔
2270
    }
2271

2272
    pTbData->uid = pTbData->pCreateTbReq->uid;
11,036,465✔
2273

2274
    // Alloc necessary resources
2275
    if (pResponse->aCreateTbRsp == NULL) {
11,036,426✔
2276
      pResponse->aCreateTbRsp = taosArrayInit(numTbData, sizeof(SVCreateTbRsp));
8,288,231✔
2277
      if (pResponse->aCreateTbRsp == NULL) {
8,288,231✔
2278
        code = terrno;
×
2279
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2280
               tstrerror(code), version);
2281
        taosArrayDestroy(newTbUids);
×
2282
        return code;
×
2283
      }
2284
    }
2285

2286
    // Do create table
2287
    vDebug("vgId:%d start to handle auto create table, version:%" PRId64, TD_VID(pVnode), version);
11,035,737✔
2288

2289
    SVCreateTbRsp *pCreateTbRsp = taosArrayReserve(pResponse->aCreateTbRsp, 1);
11,036,543✔
2290
    code = metaCreateTable2(pVnode->pMeta, version, pTbData->pCreateTbReq, &pCreateTbRsp->pMeta);
11,036,465✔
2291
    if (code == TSDB_CODE_SUCCESS) {
11,036,465✔
2292
      // Allocate necessary resources
2293
      if (newTbUids == NULL) {
10,700,994✔
2294
        newTbUids = taosArrayInit(numTbData, sizeof(int64_t));
8,005,407✔
2295
        if (newTbUids == NULL) {
8,005,407✔
2296
          code = terrno;
×
2297
          vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2298
                 tstrerror(code), version);
2299
          return code;
×
2300
        }
2301
      }
2302

2303
      if (taosArrayPush(newTbUids, &pTbData->uid) == NULL) {
21,401,988✔
2304
        code = terrno;
×
2305
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2306
               tstrerror(code), version);
2307
        taosArrayDestroy(newTbUids);
×
2308
        return code;
×
2309
      }
2310

2311
      if (pCreateTbRsp->pMeta) {
10,700,994✔
2312
        vnodeUpdateMetaRsp(pVnode, pCreateTbRsp->pMeta);
10,700,994✔
2313
      }
2314
    } else if (code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
335,471✔
2315
      code = terrno = 0;
334,459✔
2316
      pTbData->uid = pTbData->pCreateTbReq->uid;  // update uid if table exist for using below
334,420✔
2317

2318
      // stream: get sver from meta, write to pCreateTbRsp, and need to check crateTbReq is same as meta.
2319
      if (i == 0) {
334,420✔
2320
        // In the streaming scenario, multiple grouped req requests will only operate on the same write table, and
2321
        // only the first one needs to be processed.
2322
        code = buildExistTableInStreamRsp(pVnode, pTbData, &pCreateTbRsp->pMeta);
282,834✔
2323
        if (code) {
282,873✔
2324
          vInfo("vgId:%d failed to create table in stream:%s, code(0x%0x):%s", TD_VID(pVnode),
34,486✔
2325
                pTbData->pCreateTbReq->name, code, tstrerror(code));
2326
          taosArrayDestroy(newTbUids);
34,486✔
2327
          return code;
34,486✔
2328
        }
2329
      }
2330
    } else {
2331
      code = terrno;
1,012✔
2332
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
1,012✔
2333
             tstrerror(code), version);
2334
      taosArrayDestroy(newTbUids);
1,012✔
2335
      return code;
1,012✔
2336
    }
2337
  }
2338

2339
  // Update the affected table uid list
2340
  if (taosArrayGetSize(newTbUids) > 0) {
645,989,893✔
2341
    vDebug("vgId:%d, add %d table into query table list in handling submit", TD_VID(pVnode),
8,005,407✔
2342
           (int32_t)taosArrayGetSize(newTbUids));
2343
    if (tqUpdateTbUidList(pVnode->pTq, newTbUids, true) != 0) {
8,005,407✔
2344
      vError("vgId:%d, failed to update tbUid list", TD_VID(pVnode));
×
2345
    }
2346
  }
2347

2348
  vDebug("vgId:%d, handle auto create table done, version:%" PRId64, TD_VID(pVnode), version);
645,989,825✔
2349

2350
  taosArrayDestroy(newTbUids);
645,989,935✔
2351
  return code;
645,989,935✔
2352
}
2353

2354
static void addExistTableInfoIntoRes(SVnode *pVnode, SSubmitReq2 *pRequest, SSubmitRsp2 *pResponse,
418✔
2355
                                     SSubmitTbData *pTbData, int32_t numTbData) {
2356
  int32_t code = 0;
418✔
2357
  int32_t lino = 0;
418✔
2358
  if ((pTbData->flags & SUBMIT_REQ_SCHEMA_RES) == 0) {
418✔
2359
    return;
×
2360
  }
2361
  if (pResponse->aCreateTbRsp) {  // If aSubmitTbData is not NULL, it means that the request is a create table request,
418✔
2362
                                  // so table info has exitst and we do not need to add again.
2363
    return;
418✔
2364
  }
2365
  pResponse->aCreateTbRsp = taosArrayInit(numTbData, sizeof(SVCreateTbRsp));
×
2366
  if (pResponse->aCreateTbRsp == NULL) {
×
2367
    code = terrno;
×
2368
    TSDB_CHECK_CODE(code, lino, _exit);
×
2369
  }
2370
  SVCreateTbRsp *pCreateTbRsp = taosArrayReserve(pResponse->aCreateTbRsp, 1);
×
2371
  if (pCreateTbRsp == NULL) {
×
2372
    code = terrno;
×
2373
    TSDB_CHECK_CODE(code, lino, _exit);
×
2374
  }
2375
  if (pTbData->suid == 0) {
×
2376
    code = buildExistNormalTalbeRsp(pVnode, pTbData->uid, &pCreateTbRsp->pMeta);
×
2377
    if (code) {
×
2378
      vError("vgId:%d, table uid:%" PRId64 " not exists, line:%d", TD_VID(pVnode), pTbData->uid, __LINE__);
×
2379
    }
2380
  } else {
2381
    code = buildExistSubTalbeRsp(pVnode, pTbData, &pCreateTbRsp->pMeta);
×
2382
  }
2383

2384
  TSDB_CHECK_CODE(code, lino, _exit);
×
2385
_exit:
×
2386
  if (code != TSDB_CODE_SUCCESS) {
×
2387
    vError("vgId:%d, failed to add exist table info into response, code:0x%0x, line:%d", TD_VID(pVnode), code, lino);
×
2388
  }
2389
  return;
×
2390
}
2391

2392
static int32_t vnodeHandleDataWrite(SVnode *pVnode, int64_t version, SSubmitReq2 *pRequest, SSubmitRsp2 *pResponse) {
645,989,935✔
2393
  int32_t code = TSDB_CODE_SUCCESS;
645,989,935✔
2394
  int32_t numTbData = taosArrayGetSize(pRequest->aSubmitTbData);
645,989,935✔
2395
  int8_t  hasBlob = 0;
645,990,119✔
2396

2397
  // Scan submit data
2398
  for (int32_t i = 0; i < numTbData; ++i) {
1,317,910,880✔
2399
    SMetaInfo      info = {0};
671,921,754✔
2400
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
671,921,528✔
2401

2402
    if (pTbData->flags & SUBMIT_REQ_WITH_BLOB) {
671,921,104✔
2403
      hasBlob = 1;
21,306✔
2404
    }
2405
    if (pTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
671,921,104✔
2406
      continue;  // skip column data format
3,213,671✔
2407
    }
2408
    if (pTbData->flags & SUBMIT_REQ_ONLY_CREATE_TABLE) {
668,707,175✔
2409
      continue;  // skip only crate table request
2,140,462✔
2410
    }
2411

2412
    code = metaGetInfo(pVnode->pMeta, pTbData->uid, &info, NULL);
666,567,220✔
2413
    if (code) {
666,566,283✔
2414
      code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
601✔
2415
      vWarn("vgId:%d, error occurred at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __FILE__,
601✔
2416
            __LINE__, tstrerror(code), version, pTbData->uid);
2417
      return code;
601✔
2418
    }
2419

2420
    if (info.suid != pTbData->suid) {
666,565,682✔
2421
      code = TSDB_CODE_INVALID_MSG;
×
2422
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64 " suid:%" PRId64
×
2423
             " info.suid:%" PRId64,
2424
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), version, pTbData->uid, pTbData->suid,
2425
             info.suid);
2426
      return code;
×
2427
    }
2428

2429
    if (info.suid) {
666,565,864✔
2430
      code = metaGetInfo(pVnode->pMeta, info.suid, &info, NULL);
506,757,615✔
2431
      if (code) {
506,758,413✔
2432
        code = TSDB_CODE_INTERNAL_ERROR;
×
2433
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " suid:%" PRId64, TD_VID(pVnode), __func__,
×
2434
               __FILE__, __LINE__, tstrerror(code), version, info.suid);
2435
        return code;
×
2436
      }
2437
    }
2438

2439
    if (pTbData->sver != info.skmVer) {
666,566,662✔
2440
      code = TSDB_CODE_TDB_INVALID_TABLE_SCHEMA_VER;
418✔
2441
      addExistTableInfoIntoRes(pVnode, pRequest, pResponse, pTbData, numTbData);
418✔
2442
      vDebug("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64
418✔
2443
             " sver:%d"
2444
             " info.skmVer:%d",
2445
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), version, pTbData->uid, pTbData->sver,
2446
             info.skmVer);
2447
      return code;
418✔
2448
    }
2449
  }
2450

2451
  // Do write data
2452
  vDebug("vgId:%d start to handle data write, version:%" PRId64, TD_VID(pVnode), version);
645,989,126✔
2453

2454
  for (int32_t i = 0; i < numTbData; ++i) {
1,317,897,840✔
2455
    int32_t        affectedRows = 0;
671,920,166✔
2456
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
671,919,192✔
2457

2458
    if (pTbData->flags & SUBMIT_REQ_ONLY_CREATE_TABLE) {
671,919,116✔
2459
      continue;
2,140,462✔
2460
    }
2461

2462
    if (hasBlob) {
669,778,730✔
2463
      code = vnodeSubmitBlobData(pVnode, pTbData);
21,306✔
2464
      if (code) {
21,306✔
2465
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2466
               __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2467
        return code;
×
2468
      }
2469
    }
2470

2471
    code = tsdbInsertTableData(pVnode->pTsdb, version, pTbData, &affectedRows);
669,778,730✔
2472
    if (code) {
669,768,917✔
2473
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2474
             __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2475
      return code;
×
2476
    }
2477

2478
    code = metaUpdateChangeTimeWithLock(pVnode->pMeta, pTbData->uid, pTbData->ctimeMs);
669,768,917✔
2479
    if (code) {
669,766,399✔
2480
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2481
             __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2482
      return code;
×
2483
    }
2484
    pResponse->affectedRows += affectedRows;
669,766,399✔
2485
  }
2486

2487
  vDebug("vgId:%d, handle data write done, version:%" PRId64 ", affectedRows:%d", TD_VID(pVnode), version,
645,977,674✔
2488
         pResponse->affectedRows);
2489
  return code;
645,988,993✔
2490
}
2491

2492
static int32_t vnodeScanColumnData(SVnode *pVnode, SSubmitTbData *pTbData, TSKEY minKey, TSKEY maxKey) {
3,213,671✔
2493
  int32_t code = 0;
3,213,671✔
2494

2495
  int32_t   numCols = taosArrayGetSize(pTbData->aCol);
3,213,671✔
2496
  SColData *aColData = (SColData *)TARRAY_DATA(pTbData->aCol);
3,213,363✔
2497

2498
  if (numCols <= 0) {
3,213,671✔
2499
    code = TSDB_CODE_INVALID_MSG;
×
2500
    vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " numCols:%d", TD_VID(pVnode), __func__,
×
2501
           __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, numCols);
2502
    return code;
×
2503
  }
2504

2505
  if (aColData[0].cid != PRIMARYKEY_TIMESTAMP_COL_ID || aColData[0].type != TSDB_DATA_TYPE_TIMESTAMP ||
3,213,671✔
2506
      aColData[0].nVal <= 0) {
3,213,671✔
2507
    code = TSDB_CODE_INVALID_MSG;
×
2508
    vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64
×
2509
           " first column is not primary key timestamp, cid:%d type:%d nVal:%d",
2510
           TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, aColData[0].cid,
2511
           aColData[0].type, aColData[0].nVal);
2512
    return code;
×
2513
  }
2514

2515
  for (int32_t i = 1; i < numCols; ++i) {
10,097,473✔
2516
    if (aColData[i].nVal != aColData[0].nVal) {
6,884,110✔
2517
      code = TSDB_CODE_INVALID_MSG;
×
2518
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64
×
2519
             " column cid:%d type:%d nVal:%d is not equal to primary key timestamp nVal:%d",
2520
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid,
2521
             aColData[i].cid, aColData[i].type, aColData[i].nVal, aColData[0].nVal);
2522
      return code;
×
2523
    }
2524
  }
2525

2526
  SRowKey *pLastKey = NULL;
3,213,363✔
2527
  SRowKey  lastKey = {0};
3,213,363✔
2528
  for (int32_t i = 0; i < aColData[0].nVal; ++i) {
2,147,483,647✔
2529
    SRowKey key = {0};
2,147,483,647✔
2530

2531
    tColDataArrGetRowKey(aColData, numCols, i, &key);
2,147,483,647✔
2532

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

2542
    if (pLastKey && tRowKeyCompare(pLastKey, &key) >= 0) {
2,147,483,647✔
2543
      code = TSDB_CODE_INVALID_MSG;
×
2544
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] key:%" PRId64
×
2545
             " is not in order, lastKey:%" PRId64,
2546
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, i, key.ts,
2547
             pLastKey->ts);
2548
      return code;
×
2549
    } else if (pLastKey == NULL) {
2,147,483,647✔
2550
      pLastKey = &lastKey;
3,213,105✔
2551
    }
2552

2553
    *pLastKey = key;
2,147,483,647✔
2554
  }
2555

2556
  return code;
3,213,671✔
2557
}
2558

2559
static int32_t vnodeScanSubmitRowData(SVnode *pVnode, SSubmitTbData *pTbData, TSKEY minKey, TSKEY maxKey) {
668,734,560✔
2560
  int32_t code = 0;
668,734,560✔
2561

2562
  int32_t numRows = taosArrayGetSize(pTbData->aRowP);
668,734,560✔
2563
  SRow  **aRow = (SRow **)TARRAY_DATA(pTbData->aRowP);
668,736,155✔
2564

2565
  if (numRows <= 0 && (pTbData->flags & SUBMIT_REQ_ONLY_CREATE_TABLE) == 0) {
668,730,440✔
2566
    code = TSDB_CODE_INVALID_MSG;
×
2567
    vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " numRows:%d", TD_VID(pVnode), __func__,
×
2568
           __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, numRows);
2569
    return code;
×
2570
  }
2571

2572
  SRowKey *pLastKey = NULL;
668,730,440✔
2573
  SRowKey  lastKey = {0};
668,730,440✔
2574
  for (int32_t i = 0; i < numRows; ++i) {
2,147,483,647✔
2575
    SRow *pRow = aRow[i];
2,147,483,647✔
2576
    if (pRow->sver != pTbData->sver) {
2,147,483,647✔
2577
      code = TSDB_CODE_INVALID_MSG;
×
2578
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] sver:%d pTbData->sver:%d",
×
2579
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, i, pRow->sver,
2580
             pTbData->sver);
2581
      return code;
×
2582
    }
2583

2584
    SRowKey key = {0};
2,147,483,647✔
2585
    tRowGetKey(pRow, &key);
2,147,483,647✔
2586
    if (key.ts < minKey || key.ts > maxKey) {
2,147,483,647✔
2587
      code = TSDB_CODE_INVALID_MSG;
×
2588
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] key:%" PRId64
×
2589
             " is out of range [%" PRId64 ", %" PRId64 "]",
2590
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, i, key.ts,
2591
             minKey, maxKey);
2592
      return code;
×
2593
    }
2594

2595
    if (pLastKey && tRowKeyCompare(pLastKey, &key) >= 0) {
2,147,483,647✔
2596
      code = TSDB_CODE_INVALID_MSG;
×
2597
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] key:%" PRId64
×
2598
             " is not in order, lastKey:%" PRId64,
2599
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, i, key.ts,
2600
             pLastKey->ts);
2601
      return code;
×
2602
    } else if (pLastKey == NULL) {
2,147,483,647✔
2603
      pLastKey = &lastKey;
666,581,556✔
2604
    }
2605

2606
    *pLastKey = key;
2,147,483,647✔
2607
  }
2608

2609
  return code;
655,554,091✔
2610
}
2611

2612
static int32_t vnodeScanSubmitReq(SVnode *pVnode, int64_t version, SSubmitReq2 *pRequest, SSubmitRsp2 *pResponse) {
646,020,317✔
2613
  int32_t code = TSDB_CODE_SUCCESS;
646,020,317✔
2614
  int32_t numTbData = taosArrayGetSize(pRequest->aSubmitTbData);
646,020,317✔
2615

2616
  TSKEY now = taosGetTimestamp(pVnode->config.tsdbCfg.precision);
646,018,419✔
2617
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * pVnode->config.tsdbCfg.keep2;
646,021,511✔
2618
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
646,018,716✔
2619
  for (int32_t i = 0; i < numTbData; i++) {
1,317,964,501✔
2620
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
671,950,143✔
2621

2622
    if (pTbData->pCreateTbReq && pTbData->pCreateTbReq->uid == 0) {
671,952,858✔
2623
      code = TSDB_CODE_INVALID_MSG;
×
2624
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2625
             tstrerror(code), version);
2626
      return code;
×
2627
    }
2628

2629
    if (pTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
671,953,581✔
2630
      code = vnodeScanColumnData(pVnode, pTbData, minKey, maxKey);
3,213,671✔
2631
      if (code) {
3,213,413✔
2632
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2633
               __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2634
        return code;
×
2635
      }
2636
    } else {
2637
      code = vnodeScanSubmitRowData(pVnode, pTbData, minKey, maxKey);
668,735,681✔
2638
      if (code) {
668,728,340✔
2639
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2640
               __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2641
        return code;
×
2642
      }
2643
    }
2644
  }
2645

2646
  return code;
646,014,358✔
2647
}
2648

2649
static int32_t vnodeProcessSubmitReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
646,024,709✔
2650
                                     SRpcMsg *pOriginalMsg) {
2651
  int32_t code = 0;
646,024,709✔
2652
  int32_t lino = 0;
646,024,709✔
2653
  terrno = 0;
646,024,709✔
2654

2655
  SSubmitReq2 *pSubmitReq = &(SSubmitReq2){0};
646,025,643✔
2656
  SSubmitRsp2 *pSubmitRsp = &(SSubmitRsp2){0};
646,025,643✔
2657
  int32_t      ret;
2658
  SEncoder     ec = {0};
646,025,643✔
2659

2660
  pRsp->code = TSDB_CODE_SUCCESS;
646,025,643✔
2661

2662
  void           *pAllocMsg = NULL;
646,025,643✔
2663
  SSubmitReq2Msg *pMsg = (SSubmitReq2Msg *)pReq;
646,025,643✔
2664
  SDecoder        dc = {0};
646,025,643✔
2665
  if (0 == taosHton64(pMsg->version)) {
646,025,643✔
2666
    code = vnodeSubmitReqConvertToSubmitReq2(pVnode, (SSubmitReq *)pMsg, pSubmitReq);
×
2667
    if (TSDB_CODE_SUCCESS == code) {
×
2668
      code = vnodeRebuildSubmitReqMsg(pSubmitReq, &pReq);
×
2669
    }
2670
    if (TSDB_CODE_SUCCESS == code) {
×
2671
      pAllocMsg = pReq;
×
2672
    }
2673
    TSDB_CHECK_CODE(code, lino, _exit);
×
2674
  } else {
2675
    // decode
2676
    pReq = POINTER_SHIFT(pReq, sizeof(SSubmitReq2Msg));
646,025,609✔
2677
    len -= sizeof(SSubmitReq2Msg);
646,025,609✔
2678

2679
    tDecoderInit(&dc, pReq, len);
646,025,609✔
2680
    if (tDecodeSubmitReq(&dc, pSubmitReq, NULL) < 0) {
646,025,643✔
2681
      code = TSDB_CODE_INVALID_MSG;
×
2682
      TSDB_CHECK_CODE(code, lino, _exit);
×
2683
    }
2684
  }
2685

2686
  // Scan the request
2687
  code = vnodeScanSubmitReq(pVnode, ver, pSubmitReq, pSubmitRsp);
646,018,876✔
2688
  if (code) {
646,013,243✔
2689
    vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2690
           tstrerror(code), ver);
2691
    TSDB_CHECK_CODE(code, lino, _exit);
×
2692
  }
2693

2694
  vGDebug(pOriginalMsg ? &pOriginalMsg->info.traceId : NULL, "vgId:%d, index:%" PRId64 ", submit block, rows:%d",
646,013,243✔
2695
          TD_VID(pVnode), ver, (int32_t)taosArrayGetSize(pSubmitReq->aSubmitTbData));
2696

2697
  // Handle auto create table
2698
  code = vnodeHandleAutoCreateTable(pVnode, ver, pSubmitReq, pSubmitRsp);
646,023,844✔
2699
  if (code) {
646,025,433✔
2700
    vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
35,498✔
2701
           tstrerror(code), ver);
2702
    TSDB_CHECK_CODE(code, lino, _exit);
35,498✔
2703
  }
2704

2705
  // Handle data write
2706
  code = vnodeHandleDataWrite(pVnode, ver, pSubmitReq, pSubmitRsp);
645,989,935✔
2707
  if (code) {
645,990,145✔
2708
    vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
1,019✔
2709
           tstrerror(code), ver);
2710
    TSDB_CHECK_CODE(code, lino, _exit);
1,019✔
2711
  }
2712

2713
_exit:
646,025,475✔
2714
  // message
2715
  pRsp->code = code;
646,025,643✔
2716
  tEncodeSize(tEncodeSSubmitRsp2, pSubmitRsp, pRsp->contLen, ret);
646,025,643✔
2717
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
646,022,975✔
2718
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
646,023,212✔
2719
  if (tEncodeSSubmitRsp2(&ec, pSubmitRsp) < 0) {
646,024,203✔
2720
    vError("vgId:%d, failed to encode submit response", TD_VID(pVnode));
×
2721
  }
2722
  tEncoderClear(&ec);
646,025,575✔
2723

2724
  // update statistics
2725
  (void)atomic_add_fetch_64(&pVnode->statis.nInsert, pSubmitRsp->affectedRows);
646,023,170✔
2726
  (void)atomic_add_fetch_64(&pVnode->statis.nInsertSuccess, pSubmitRsp->affectedRows);
646,025,510✔
2727
  (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsert, 1);
646,025,510✔
2728

2729
  // update metrics
2730
  METRICS_UPDATE(pVnode->writeMetrics.total_requests, METRIC_LEVEL_LOW, 1);
646,024,428✔
2731
  METRICS_UPDATE(pVnode->writeMetrics.total_rows, METRIC_LEVEL_HIGH, pSubmitRsp->affectedRows);
646,023,579✔
2732
  METRICS_UPDATE(pVnode->writeMetrics.total_bytes, METRIC_LEVEL_LOW, pMsg->header.contLen);
646,022,674✔
2733

2734
  if (tsEnableMonitor && tsMonitorFqdn[0] != 0 && tsMonitorPort != 0 && pSubmitRsp->affectedRows > 0 &&
646,022,221✔
2735
      strlen(RPC_MSG_USER(pOriginalMsg)) > 0 && tsInsertCounter != NULL) {
×
2736
    const char *sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT_AFFECTED_ROWS,
×
2737
                                   pVnode->monitor.strClusterId,
×
2738
                                   pVnode->monitor.strDnodeId,
×
2739
                                   tsLocalEp,
2740
                                   pVnode->monitor.strVgId,
×
2741
                                   RPC_MSG_USER(pOriginalMsg),
×
2742
                                   "Success"};
2743
    int         tv = taos_counter_add(tsInsertCounter, pSubmitRsp->affectedRows, sample_labels);
×
2744
  }
2745

2746
  if (code == 0) {
646,022,320✔
2747
    (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
645,985,803✔
2748
  }
2749

2750
  // clear
2751
  tDestroySubmitReq(pSubmitReq, 0 == taosHton64(pMsg->version) ? TSDB_MSG_FLG_CMPT : TSDB_MSG_FLG_DECODE);
646,023,863✔
2752
  tDestroySSubmitRsp2(pSubmitRsp, TSDB_MSG_FLG_ENCODE);
646,018,925✔
2753

2754
  if (code) {
646,018,695✔
2755
    terrno = code;
36,517✔
2756
    if (code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
36,517✔
2757
      vInfo("vgId:%d, failed to process submit request since %s, lino:%d, version:%" PRId64, TD_VID(pVnode),
34,486✔
2758
            tstrerror(code), lino, ver);
2759

2760
    } else {
2761
      vError("vgId:%d, failed to process submit request since %s, lino:%d, version:%" PRId64, TD_VID(pVnode),
2,031✔
2762
             tstrerror(code), lino, ver);
2763
    }
2764
  }
2765

2766
  taosMemoryFree(pAllocMsg);
646,015,015✔
2767
  tDecoderClear(&dc);
646,015,406✔
2768

2769
  return code;
646,024,734✔
2770
}
2771

2772
static int32_t vnodeConsolidateAlterHashRange(SVnode *pVnode, int64_t ver) {
23,985✔
2773
  int32_t code = TSDB_CODE_SUCCESS;
23,985✔
2774

2775
  vInfo("vgId:%d, trim meta of tables per hash range [%" PRIu32 ", %" PRIu32 "]. apply-index:%" PRId64, TD_VID(pVnode),
23,985✔
2776
        pVnode->config.hashBegin, pVnode->config.hashEnd, ver);
2777

2778
  // TODO: trim meta of tables from TDB per hash range [pVnode->config.hashBegin, pVnode->config.hashEnd]
2779
  code = metaTrimTables(pVnode->pMeta, ver);
23,985✔
2780

2781
  return code;
23,985✔
2782
}
2783

2784
static int32_t vnodeProcessAlterConfirmReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
4,168,430✔
2785
  vInfo("vgId:%d, vnode handle msgType:alter-confirm, alter confirm msg is processed", TD_VID(pVnode));
4,168,430✔
2786
  int32_t code = TSDB_CODE_SUCCESS;
4,168,430✔
2787
  if (!pVnode->config.hashChange) {
4,168,430✔
2788
    goto _exit;
4,144,445✔
2789
  }
2790

2791
  code = vnodeConsolidateAlterHashRange(pVnode, ver);
23,985✔
2792
  if (code < 0) {
23,985✔
2793
    vError("vgId:%d, failed to consolidate alter hashrange since %s. version:%" PRId64, TD_VID(pVnode), terrstr(), ver);
×
2794
    goto _exit;
×
2795
  }
2796
  pVnode->config.hashChange = false;
23,985✔
2797

2798
_exit:
4,168,430✔
2799
  pRsp->msgType = TDMT_VND_ALTER_CONFIRM_RSP;
4,168,430✔
2800
  pRsp->code = code;
4,168,430✔
2801
  pRsp->pCont = NULL;
4,168,430✔
2802
  pRsp->contLen = 0;
4,168,430✔
2803

2804
  return code;
4,168,430✔
2805
}
2806

2807
extern int32_t tsdbDisableAndCancelAllBgTask(STsdb *pTsdb);
2808
extern void    tsdbEnableBgTask(STsdb *pTsdb);
2809

2810
static int32_t vnodeProcessAlterConfigReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
313,503✔
2811
  bool walChanged = false;
313,503✔
2812
  bool tsdbChanged = false;
313,503✔
2813

2814
  SAlterVnodeConfigReq req = {0};
313,503✔
2815
  if (tDeserializeSAlterVnodeConfigReq(pReq, len, &req) != 0) {
313,503✔
2816
    terrno = TSDB_CODE_INVALID_MSG;
×
2817
    return TSDB_CODE_INVALID_MSG;
×
2818
  }
2819

2820
  vInfo("vgId:%d, start to alter vnode config, page:%d pageSize:%d buffer:%d szPage:%d szBuf:%" PRIu64
313,503✔
2821
        " cacheLast:%d cacheLastSize:%d days:%d keep0:%d keep1:%d keep2:%d keepTimeOffset:%d ssKeepLocal:%d "
2822
        "ssCompact:%d fsync:%d level:%d "
2823
        "walRetentionPeriod:%d walRetentionSize:%d",
2824
        TD_VID(pVnode), req.pages, req.pageSize, req.buffer, req.pageSize * 1024, (uint64_t)req.buffer * 1024 * 1024,
2825
        req.cacheLast, req.cacheLastSize, req.daysPerFile, req.daysToKeep0, req.daysToKeep1, req.daysToKeep2,
2826
        req.keepTimeOffset, req.ssKeepLocal, req.ssCompact, req.walFsyncPeriod, req.walLevel, req.walRetentionPeriod,
2827
        req.walRetentionSize);
2828

2829
  if (pVnode->config.cacheLastSize != req.cacheLastSize) {
313,503✔
2830
    pVnode->config.cacheLastSize = req.cacheLastSize;
11,572✔
2831
    tsdbCacheSetCapacity(pVnode, (size_t)pVnode->config.cacheLastSize * 1024 * 1024);
11,572✔
2832
  }
2833

2834
  if (pVnode->config.szBuf != req.buffer * 1024LL * 1024LL) {
313,503✔
2835
    vInfo("vgId:%d, vnode buffer is changed from %" PRId64 " to %" PRId64, TD_VID(pVnode), pVnode->config.szBuf,
8,754✔
2836
          (uint64_t)(req.buffer * 1024LL * 1024LL));
2837
    pVnode->config.szBuf = req.buffer * 1024LL * 1024LL;
8,754✔
2838
  }
2839

2840
  if (pVnode->config.szCache != req.pages) {
313,503✔
2841
    if ((terrno = metaAlterCache(pVnode->pMeta, req.pages)) < 0) {
4,404✔
2842
      vError("vgId:%d, failed to change vnode pages from %d to %d failed since %s", TD_VID(pVnode),
×
2843
             pVnode->config.szCache, req.pages, tstrerror(terrno));
2844
      return terrno;
×
2845
    } else {
2846
      vInfo("vgId:%d, vnode pages is changed from %d to %d", TD_VID(pVnode), pVnode->config.szCache, req.pages);
4,404✔
2847
      pVnode->config.szCache = req.pages;
4,404✔
2848
    }
2849
  }
2850

2851
  if (pVnode->config.cacheLast != req.cacheLast) {
313,503✔
2852
    pVnode->config.cacheLast = req.cacheLast;
91,014✔
2853
  }
2854

2855
  if (pVnode->config.walCfg.fsyncPeriod != req.walFsyncPeriod) {
313,503✔
2856
    pVnode->config.walCfg.fsyncPeriod = req.walFsyncPeriod;
50,944✔
2857
    walChanged = true;
50,944✔
2858
  }
2859

2860
  if (pVnode->config.walCfg.level != req.walLevel) {
313,503✔
2861
    if (pVnode->config.walCfg.level == 0) {
37,486✔
2862
      pVnode->config.walCfg.clearFiles = 1;
2,106✔
2863
    }
2864
    pVnode->config.walCfg.level = req.walLevel;
37,486✔
2865
    walChanged = true;
37,486✔
2866
  }
2867

2868
  if (pVnode->config.walCfg.retentionPeriod != req.walRetentionPeriod) {
313,503✔
2869
    pVnode->config.walCfg.retentionPeriod = req.walRetentionPeriod;
44,617✔
2870
    walChanged = true;
44,617✔
2871
  }
2872

2873
  if (pVnode->config.walCfg.retentionSize != req.walRetentionSize) {
313,503✔
2874
    pVnode->config.walCfg.retentionSize = req.walRetentionSize;
1,414✔
2875
    walChanged = true;
1,414✔
2876
  }
2877

2878
  if (pVnode->config.tsdbCfg.keep0 != req.daysToKeep0) {
313,503✔
2879
    pVnode->config.tsdbCfg.keep0 = req.daysToKeep0;
40,564✔
2880
    tsdbChanged = true;
40,564✔
2881
  }
2882

2883
  if (pVnode->config.tsdbCfg.keep1 != req.daysToKeep1) {
313,503✔
2884
    pVnode->config.tsdbCfg.keep1 = req.daysToKeep1;
39,096✔
2885
    tsdbChanged = true;
39,096✔
2886
  }
2887

2888
  if (pVnode->config.tsdbCfg.keep2 != req.daysToKeep2) {
313,503✔
2889
    pVnode->config.tsdbCfg.keep2 = req.daysToKeep2;
33,336✔
2890
    tsdbChanged = true;
33,336✔
2891
  }
2892

2893
  if (pVnode->config.tsdbCfg.keepTimeOffset != req.keepTimeOffset) {
313,503✔
2894
    pVnode->config.tsdbCfg.keepTimeOffset = req.keepTimeOffset;
7,102✔
2895
    tsdbChanged = true;
7,102✔
2896
  }
2897

2898
  if (req.sttTrigger != -1 && req.sttTrigger != pVnode->config.sttTrigger) {
313,503✔
2899
    if (req.sttTrigger > 1 && pVnode->config.sttTrigger > 1) {
2,448✔
2900
      pVnode->config.sttTrigger = req.sttTrigger;
1,414✔
2901
    } else {
2902
      vnodeAWait(&pVnode->commitTask);
1,034✔
2903

2904
      int32_t ret = tsdbDisableAndCancelAllBgTask(pVnode->pTsdb);
1,034✔
2905
      if (ret != 0) {
1,034✔
2906
        vError("vgId:%d, failed to disable bg task since %s", TD_VID(pVnode), tstrerror(ERRNO));
×
2907
      }
2908

2909
      pVnode->config.sttTrigger = req.sttTrigger;
1,034✔
2910
      tsdbEnableBgTask(pVnode->pTsdb);
1,034✔
2911
    }
2912
  }
2913

2914
  if (req.minRows != -1 && req.minRows != pVnode->config.tsdbCfg.minRows) {
313,503✔
2915
    pVnode->config.tsdbCfg.minRows = req.minRows;
1,414✔
2916
  }
2917

2918
  if (req.ssKeepLocal != -1 && req.ssKeepLocal != pVnode->config.ssKeepLocal) {
313,503✔
2919
    pVnode->config.ssKeepLocal = req.ssKeepLocal;
1,414✔
2920
  }
2921
  if (req.ssCompact != -1 && req.ssCompact != pVnode->config.ssCompact) {
313,503✔
2922
    pVnode->config.ssCompact = req.ssCompact;
1,414✔
2923
  }
2924

2925
  if (walChanged) {
313,503✔
2926
    if (walAlter(pVnode->pWal, &pVnode->config.walCfg) != 0) {
131,623✔
2927
      vError("vgId:%d, failed to alter wal config since %s", TD_VID(pVnode), tstrerror(ERRNO));
×
2928
    }
2929
  }
2930

2931
  if (tsdbChanged) {
313,503✔
2932
    tsdbSetKeepCfg(pVnode->pTsdb, &pVnode->config.tsdbCfg);
54,952✔
2933
  }
2934

2935
  return 0;
313,503✔
2936
}
2937

2938
static int32_t vnodeProcessBatchDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
2939
  SBatchDeleteReq deleteReq;
×
2940
  SDecoder        decoder;
×
2941
  tDecoderInit(&decoder, pReq, len);
×
2942
  if (tDecodeSBatchDeleteReq(&decoder, &deleteReq) < 0) {
×
2943
    tDecoderClear(&decoder);
×
2944
    return terrno = TSDB_CODE_INVALID_MSG;
×
2945
  }
2946

2947
  SMetaReader mr = {0};
×
2948
  metaReaderDoInit(&mr, pVnode->pMeta, META_READER_NOLOCK);
×
2949
  STsdb *pTsdb = pVnode->pTsdb;
×
2950

2951
  int32_t sz = taosArrayGetSize(deleteReq.deleteReqs);
×
2952
  for (int32_t i = 0; i < sz; i++) {
×
2953
    SSingleDeleteReq *pOneReq = taosArrayGet(deleteReq.deleteReqs, i);
×
2954
    char             *name = pOneReq->tbname;
×
2955
    if (metaGetTableEntryByName(&mr, name) < 0) {
×
2956
      vDebug("vgId:%d, stream delete msg, skip since no table: %s", pVnode->config.vgId, name);
×
2957
      continue;
×
2958
    }
2959

2960
    int64_t uid = mr.me.uid;
×
2961

2962
    int32_t code = tsdbDeleteTableData(pTsdb, ver, deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
×
2963
    if (code < 0) {
×
2964
      terrno = code;
×
2965
      vError("vgId:%d, delete error since %s, suid:%" PRId64 ", uid:%" PRId64 ", start ts:%" PRId64 ", end ts:%" PRId64,
×
2966
             TD_VID(pVnode), terrstr(), deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
2967
    }
2968

2969
    if (deleteReq.level == 0) {
×
2970
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, deleteReq.ctimeMs);
×
2971
      if (code < 0) {
×
2972
        terrno = code;
×
2973
        vError("vgId:%d, update change time error since %s, suid:%" PRId64 ", uid:%" PRId64 ", start ts:%" PRId64
×
2974
               ", end ts:%" PRId64,
2975
               TD_VID(pVnode), terrstr(), deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
2976
      }
2977
    }
2978
    tDecoderClear(&mr.coder);
×
2979
  }
2980
  metaReaderClear(&mr);
×
2981
  taosArrayDestroy(deleteReq.deleteReqs);
×
2982
  return 0;
×
2983
}
2984

2985
static int32_t vnodeProcessDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
2,014,615✔
2986
                                     SRpcMsg *pOriginalMsg) {
2987
  int32_t     code = 0;
2,014,615✔
2988
  SDecoder   *pCoder = &(SDecoder){0};
2,014,615✔
2989
  SDeleteRes *pRes = &(SDeleteRes){0};
2,014,615✔
2990

2991
  pRsp->msgType = TDMT_VND_DELETE_RSP;
2,014,615✔
2992
  pRsp->pCont = NULL;
2,014,615✔
2993
  pRsp->contLen = 0;
2,014,615✔
2994
  pRsp->code = TSDB_CODE_SUCCESS;
2,014,615✔
2995

2996
  pRes->uidList = taosArrayInit(0, sizeof(tb_uid_t));
2,014,615✔
2997
  if (pRes->uidList == NULL) {
2,014,615✔
2998
    code = terrno;
×
2999
    goto _err;
×
3000
  }
3001

3002
  tDecoderInit(pCoder, pReq, len);
2,014,615✔
3003
  code = tDecodeDeleteRes(pCoder, pRes);
2,014,615✔
3004
  if (code) goto _err;
2,014,615✔
3005

3006
  if (pRes->affectedRows > 0) {
2,014,615✔
3007
    for (int32_t iUid = 0; iUid < taosArrayGetSize(pRes->uidList); iUid++) {
3,583,493✔
3008
      uint64_t uid = *(uint64_t *)taosArrayGet(pRes->uidList, iUid);
2,047,192✔
3009
      code = tsdbDeleteTableData(pVnode->pTsdb, ver, pRes->suid, uid, pRes->skey, pRes->ekey);
2,047,192✔
3010
      if (code) goto _err;
2,047,192✔
3011
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, pRes->ctimeMs);
2,047,192✔
3012
      if (code) goto _err;
2,047,192✔
3013
    }
3014
  }
3015

3016
  tDecoderClear(pCoder);
2,014,615✔
3017
  taosArrayDestroy(pRes->uidList);
2,014,615✔
3018

3019
  SVDeleteRsp rsp = {.affectedRows = pRes->affectedRows};
2,014,615✔
3020
  int32_t     ret = 0;
2,014,615✔
3021
  tEncodeSize(tEncodeSVDeleteRsp, &rsp, pRsp->contLen, ret);
2,014,615✔
3022
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
2,014,615✔
3023
  SEncoder ec = {0};
2,014,615✔
3024
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
2,014,615✔
3025
  code = tEncodeSVDeleteRsp(&ec, &rsp);
2,014,615✔
3026
  if (code) goto _err;
2,014,615✔
3027
  tEncoderClear(&ec);
2,014,615✔
3028
  return code;
2,014,615✔
3029

3030
_err:
×
3031
  /*
3032
  if(code == TSDB_CODE_SUCCESS){
3033
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_DELETE, pVnode->monitor.strClusterId,
3034
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
3035
                                        RPC_MSG_USER(pOriginalMsg), "Success"};
3036
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
3037
  }
3038
  else{
3039
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_DELETE, pVnode->monitor.strClusterId,
3040
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
3041
                                        RPC_MSG_USER(pOriginalMsg), "Failed"};
3042
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
3043
  }
3044
  */
3045

3046
  return code;
×
3047
}
3048
static int32_t vnodeProcessCreateIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
4,221✔
3049
  SVCreateStbReq req = {0};
4,221✔
3050
  SDecoder       dc = {0};
4,221✔
3051
  int32_t        code = 0;
4,221✔
3052

3053
  pRsp->msgType = TDMT_VND_CREATE_INDEX_RSP;
4,221✔
3054
  pRsp->code = TSDB_CODE_SUCCESS;
4,221✔
3055
  pRsp->pCont = NULL;
4,221✔
3056
  pRsp->contLen = 0;
4,221✔
3057

3058
  tDecoderInit(&dc, pReq, len);
4,221✔
3059
  // decode req
3060
  if (tDecodeSVCreateStbReq(&dc, &req) < 0) {
4,221✔
3061
    tDecoderClear(&dc);
×
3062
    return terrno = TSDB_CODE_INVALID_MSG;
×
3063
  }
3064

3065
  code = metaAddIndexToSuperTable(pVnode->pMeta, ver, &req);
4,221✔
3066
  if (code) {
4,221✔
3067
    pRsp->code = code;
×
3068
    goto _err;
×
3069
  }
3070
  tDecoderClear(&dc);
4,221✔
3071
  return 0;
4,221✔
3072

3073
_err:
×
3074
  tDecoderClear(&dc);
×
3075
  return code;
×
3076
}
3077
static int32_t vnodeProcessDropIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
2,766✔
3078
  SDropIndexReq req = {0};
2,766✔
3079
  int32_t       code = 0;
2,766✔
3080
  pRsp->msgType = TDMT_VND_DROP_INDEX_RSP;
2,766✔
3081
  pRsp->code = TSDB_CODE_SUCCESS;
2,766✔
3082
  pRsp->pCont = NULL;
2,766✔
3083
  pRsp->contLen = 0;
2,766✔
3084

3085
  if ((code = tDeserializeSDropIdxReq(pReq, len, &req))) {
2,766✔
3086
    pRsp->code = code;
×
3087
    return code;
×
3088
  }
3089

3090
  code = metaDropIndexFromSuperTable(pVnode->pMeta, ver, &req);
2,766✔
3091
  if (code) {
2,766✔
3092
    pRsp->code = code;
×
3093
    return code;
×
3094
  }
3095
  return TSDB_CODE_SUCCESS;
2,766✔
3096
}
3097

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

3100
static int32_t vnodeProcessCompactVnodeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
67,802✔
3101
  if (!pVnode->restored) {
67,802✔
3102
    vInfo("vgId:%d, ignore compact req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
4,477✔
3103
    return 0;
4,477✔
3104
  }
3105
  return vnodeAsyncCompact(pVnode, ver, pReq, len, pRsp);
63,325✔
3106
}
3107

3108
static int32_t vnodeProcessConfigChangeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
3109
  if (syncCheckMember(pVnode->sync) != 0) {
×
3110
    vError("vgId:%d, failed to check member", TD_VID(pVnode));
×
3111
  }
3112

3113
  pRsp->msgType = TDMT_SYNC_CONFIG_CHANGE_RSP;
×
3114
  pRsp->code = TSDB_CODE_SUCCESS;
×
3115
  pRsp->pCont = NULL;
×
3116
  pRsp->contLen = 0;
×
3117

3118
  return 0;
×
3119
}
3120

3121
static int32_t vnodeCheckState(SVnode *pVnode) {
77,129✔
3122
  SSyncState syncState = syncGetState(pVnode->sync);
77,129✔
3123
  if (syncState.state != TAOS_SYNC_STATE_LEADER) {
77,129✔
3124
    return terrno = TSDB_CODE_SYN_NOT_LEADER;
39,698✔
3125
  }
3126
  return 0;
37,431✔
3127
}
3128

3129
static int32_t vnodeCheckToken(SVnode *pVnode, char *member0Token, char *member1Token) {
37,431✔
3130
  char token[TSDB_ARB_TOKEN_SIZE] = {0};
37,431✔
3131
  if (vnodeGetArbToken(pVnode, token) != 0) {
37,431✔
3132
    return terrno = TSDB_CODE_NOT_FOUND;
×
3133
  }
3134

3135
  if (strncmp(token, member0Token, TSDB_ARB_TOKEN_SIZE) != 0 &&
37,431✔
3136
      strncmp(token, member1Token, TSDB_ARB_TOKEN_SIZE) != 0) {
35,497✔
3137
    return terrno = TSDB_CODE_MND_ARB_TOKEN_MISMATCH;
34,669✔
3138
  }
3139

3140
  terrno = TSDB_CODE_SUCCESS;
2,762✔
3141
  return 0;
2,762✔
3142
}
3143

3144
static int32_t vnodeCheckSyncd(SVnode *pVnode, char *member0Token, char *member1Token) {
8,839✔
3145
  int32_t code = vnodeCheckToken(pVnode, member0Token, member1Token);
8,839✔
3146
  if (code != 0) {
8,839✔
3147
    return code;
7,458✔
3148
  }
3149

3150
  return syncCheckSynced(pVnode->sync);
1,381✔
3151
}
3152

3153
static int32_t vnodeProcessArbCheckSyncReq(SVnode *pVnode, void *pReq, int32_t len, SRpcMsg *pRsp) {
24,458✔
3154
  int32_t code = 0;
24,458✔
3155

3156
  if ((code = vnodeCheckState(pVnode)) != 0) {
24,458✔
3157
    vDebug("vgId:%d, failed to preprocess vnode-arb-check-sync request since %s", TD_VID(pVnode), tstrerror(code));
15,619✔
3158
    return 0;
15,619✔
3159
  }
3160

3161
  SVArbCheckSyncReq syncReq = {0};
8,839✔
3162

3163
  code = tDeserializeSVArbCheckSyncReq(pReq, len, &syncReq);
8,839✔
3164
  if (code) {
8,839✔
3165
    return code;
×
3166
  }
3167

3168
  vInfo("vgId:%d, start to process vnode-arb-check-sync req QID:0x%" PRIx64 ":0x%" PRIx64 ", seqNum:%" PRIx64
8,839✔
3169
        ", arbToken:%s, member0Token:%s, member1Token:%s, "
3170
        "arbTerm:%" PRId64,
3171
        TD_VID(pVnode), pRsp->info.traceId.rootId, pRsp->info.traceId.msgId, pRsp->info.seqNum, syncReq.arbToken,
3172
        syncReq.member0Token, syncReq.member1Token, syncReq.arbTerm);
3173

3174
  pRsp->msgType = TDMT_VND_ARB_CHECK_SYNC_RSP;
8,839✔
3175
  pRsp->code = TSDB_CODE_SUCCESS;
8,839✔
3176
  pRsp->pCont = NULL;
8,839✔
3177
  pRsp->contLen = 0;
8,839✔
3178

3179
  SVArbCheckSyncRsp syncRsp = {0};
8,839✔
3180
  syncRsp.arbToken = syncReq.arbToken;
8,839✔
3181
  syncRsp.member0Token = syncReq.member0Token;
8,839✔
3182
  syncRsp.member1Token = syncReq.member1Token;
8,839✔
3183
  syncRsp.vgId = TD_VID(pVnode);
8,839✔
3184

3185
  if ((syncRsp.errCode = vnodeCheckSyncd(pVnode, syncReq.member0Token, syncReq.member1Token)) != 0) {
8,839✔
3186
    vError("vgId:%d, failed to check assigned log syncd since %s", TD_VID(pVnode), tstrerror(syncRsp.errCode));
7,458✔
3187
  }
3188

3189
  if ((code = vnodeUpdateArbTerm(pVnode, syncReq.arbTerm)) != 0) {
8,839✔
3190
    vError("vgId:%d, failed to update arb term since %s", TD_VID(pVnode), tstrerror(code));
×
3191
    goto _OVER;
×
3192
  }
3193

3194
  int32_t contLen = tSerializeSVArbCheckSyncRsp(NULL, 0, &syncRsp);
8,839✔
3195
  if (contLen <= 0) {
8,839✔
3196
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
3197
    code = -1;
×
3198
    goto _OVER;
×
3199
  }
3200
  void *pHead = rpcMallocCont(contLen);
8,839✔
3201
  if (!pHead) {
8,839✔
3202
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
3203
    code = -1;
×
3204
    goto _OVER;
×
3205
  }
3206

3207
  if (tSerializeSVArbCheckSyncRsp(pHead, contLen, &syncRsp) <= 0) {
8,839✔
3208
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
3209
    rpcFreeCont(pHead);
×
3210
    code = -1;
×
3211
    goto _OVER;
×
3212
  }
3213

3214
  pRsp->pCont = pHead;
8,839✔
3215
  pRsp->contLen = contLen;
8,839✔
3216

3217
  vInfo(
8,839✔
3218
      "vgId:%d, suceed to process vnode-arb-check-sync req rsp.code:%s, arbToken:%s, member0Token:%s, "
3219
      "member1Token:%s",
3220
      TD_VID(pVnode), tstrerror(syncRsp.errCode), syncRsp.arbToken, syncRsp.member0Token, syncRsp.member1Token);
3221

3222
  code = TSDB_CODE_SUCCESS;
8,839✔
3223

3224
_OVER:
8,839✔
3225
  if (code != 0) {
8,839✔
3226
    vError("vgId:%d, failed to process arb check req rsp.code:%s since %s", TD_VID(pVnode), tstrerror(syncRsp.errCode),
×
3227
           tstrerror(code));
3228
  }
3229
  tFreeSVArbCheckSyncReq(&syncReq);
8,839✔
3230
  return code;
8,839✔
3231
}
3232

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