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

taosdata / TDengine / #4886

16 Dec 2025 01:13AM UTC coverage: 65.292% (+0.03%) from 65.258%
#4886

push

travis-ci

web-flow
fix: compile error (#33938)

178718 of 273721 relevant lines covered (65.29%)

103311111.65 hits per line

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

65.81
/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) {
66,882,244✔
86
  int32_t code = 0;
66,882,244✔
87
  int32_t lino = 0;
66,882,244✔
88

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

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

100
  // name
101
  char *name = NULL;
66,883,225✔
102
  if (tDecodeCStr(pCoder, &name) < 0) {
66,881,701✔
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);
66,881,701✔
109
  if (uid == 0) {
66,878,814✔
110
    uid = tGenIdPI64();
66,129,917✔
111
  }
112
  taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), uid);
66,877,860✔
113

114
  // btime
115
  taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos + 8), btime);
66,877,767✔
116

117
  tEndDecode(pCoder);
66,874,091✔
118

119
_exit:
66,879,770✔
120
  if (code) {
66,879,762✔
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);
66,879,762✔
124
    if (pUid) *pUid = uid;
66,879,569✔
125
  }
126
  return code;
66,879,540✔
127
}
128
static int32_t vnodePreProcessCreateTableMsg(SVnode *pVnode, SRpcMsg *pMsg) {
50,456,797✔
129
  int32_t code = 0;
50,456,797✔
130
  int32_t lino = 0;
50,456,797✔
131

132
  int64_t  btime = taosGetTimestampMs();
50,457,277✔
133
  SDecoder dc = {0};
50,457,277✔
134
  int32_t  nReqs;
50,455,491✔
135

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

142
  if (tDecodeI32v(&dc, &nReqs) < 0) {
50,457,219✔
143
    code = TSDB_CODE_INVALID_MSG;
×
144
    TSDB_CHECK_CODE(code, lino, _exit);
×
145
  }
146
  for (int32_t iReq = 0; iReq < nReqs; iReq++) {
104,649,008✔
147
    code = vnodePreprocessCreateTableReq(pVnode, &dc, btime, NULL);
54,195,016✔
148
    TSDB_CHECK_CODE(code, lino, _exit);
54,191,789✔
149
  }
150

151
  tEndDecode(&dc);
50,453,992✔
152

153
_exit:
50,457,031✔
154
  tDecoderClear(&dc);
50,457,092✔
155
  if (code) {
50,456,885✔
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;
50,456,690✔
160
}
161

162
static int32_t vnodePreProcessAlterTableMsg(SVnode *pVnode, SRpcMsg *pMsg) {
12,589,551✔
163
  int32_t code = TSDB_CODE_INVALID_MSG;
12,589,551✔
164
  int32_t lino = 0;
12,589,551✔
165

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

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

179
  code = 0;
12,589,551✔
180

181
_exit:
12,589,551✔
182
  tDecoderClear(&dc);
12,589,551✔
183
  if (code) {
12,589,551✔
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,
12,589,551✔
187
           ctimeMs);
188
  }
189
  return code;
12,589,551✔
190
}
191

192
static int32_t vnodePreProcessDropTtlMsg(SVnode *pVnode, SRpcMsg *pMsg) {
7,438,303✔
193
  int32_t code = TSDB_CODE_INVALID_MSG;
7,438,303✔
194
  int32_t lino = 0;
7,438,303✔
195

196
  SMsgHead *pContOld = pMsg->pCont;
7,438,303✔
197
  int32_t   reqLenOld = pMsg->contLen - sizeof(SMsgHead);
7,438,863✔
198

199
  SArray *tbUids = NULL;
7,439,406✔
200
  int64_t timestampMs = 0;
7,439,406✔
201

202
  SVDropTtlTableReq ttlReq = {0};
7,439,406✔
203
  if (tDeserializeSVDropTtlTableReq((char *)pContOld + sizeof(SMsgHead), reqLenOld, &ttlReq) != 0) {
7,437,909✔
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));
7,438,490✔
210
    if (tbUids == NULL) {
7,438,244✔
211
      code = terrno;
×
212
      TSDB_CHECK_CODE(code, lino, _exit);
×
213
    }
214

215
    timestampMs = (int64_t)ttlReq.timestampSec * 1000;
7,438,244✔
216
    code = metaTtlFindExpired(pVnode->pMeta, timestampMs, tbUids, ttlReq.ttlDropMaxCount);
7,438,244✔
217
    if (code != 0) {
7,426,236✔
218
      code = TSDB_CODE_INVALID_MSG;
×
219
      TSDB_CHECK_CODE(code, lino, _exit);
×
220
    }
221

222
    ttlReq.nUids = taosArrayGetSize(tbUids);
7,426,236✔
223
    ttlReq.pTbUids = tbUids;
7,422,760✔
224
  }
225

226
  if (ttlReq.nUids == 0) {
7,422,760✔
227
    code = TSDB_CODE_MSG_PREPROCESSED;
7,400,294✔
228
    TSDB_CHECK_CODE(code, lino, _exit);
7,400,294✔
229
  }
230

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

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

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

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

252
  code = 0;
12,228✔
253

254
_exit:
7,415,443✔
255
  taosArrayDestroy(tbUids);
7,428,357✔
256

257
  if (code && code != TSDB_CODE_MSG_PREPROCESSED) {
7,418,395✔
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);
7,418,395✔
262
  }
263

264
  return code;
7,418,395✔
265
}
266

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

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

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

286
  int64_t uid;
541,921,072✔
287
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
541,929,812✔
288
    code = vnodePreprocessCreateTableReq(pVnode, pCoder, btimeMs, &uid);
12,687,504✔
289
    TSDB_CHECK_CODE(code, lino, _exit);
12,686,761✔
290
  }
291

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

298
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
541,936,789✔
299
    taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), uid);
12,687,417✔
300
    pCoder->pos += sizeof(int64_t);
12,687,072✔
301
  } else {
302
    if (tDecodeI64(pCoder, &submitTbData.uid) < 0) {
529,218,783✔
303
      code = TSDB_CODE_INVALID_MSG;
×
304
      TSDB_CHECK_CODE(code, lino, _exit);
×
305
    }
306
  }
307

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

313
  // scan and check
314
  TSKEY now = btimeMs;
541,907,045✔
315
  if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_MICRO) {
541,907,045✔
316
    now *= 1000;
1,166,824✔
317
  } else if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_NANO) {
540,741,556✔
318
    now *= 1000000;
1,109,833✔
319
  }
320

321
  int32_t keep = pVnode->config.tsdbCfg.keep2;
541,907,997✔
322

323
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * keep;
541,905,858✔
324
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
541,908,662✔
325
  if (submitTbData.flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
541,923,266✔
326
    uint64_t nColData;
611,688✔
327
    if (tDecodeU64v(pCoder, &nColData) < 0) {
612,741✔
328
      code = TSDB_CODE_INVALID_MSG;
×
329
      TSDB_CHECK_CODE(code, lino, _exit);
×
330
    }
331

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

339
    if (colData.flag != HAS_VALUE) {
612,799✔
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++) {
742,588,696✔
345
      if (((TSKEY *)colData.pData)[iRow] < minKey || ((TSKEY *)colData.pData)[iRow] > maxKey) {
742,038,931✔
346
        code = TSDB_CODE_TDB_TIMESTAMP_OUT_OF_RANGE;
626✔
347
        TSDB_CHECK_CODE(code, lino, _exit);
626✔
348
      }
349
    }
350

351
    for (uint64_t i = 1; i < nColData; i++) {
3,285,876✔
352
      code = tDecodeColData(version, pCoder, &colData, true);
2,736,111✔
353
      if (code) {
2,736,111✔
354
        code = TSDB_CODE_INVALID_MSG;
×
355
        TSDB_CHECK_CODE(code, lino, _exit);
×
356
      }
357
    }
358
  } else {
359
    uint64_t nRow;
541,305,953✔
360
    if (tDecodeU64v(pCoder, &nRow) < 0) {
541,329,644✔
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;
69,952✔
375
        TSDB_CHECK_CODE(code, lino, _exit);
69,952✔
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)) {
541,868,069✔
387
    taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), ctimeMs);
541,870,182✔
388
    pCoder->pos += sizeof(int64_t);
541,832,638✔
389
  }
390

391
  tEndDecode(pCoder);
541,840,841✔
392

393
_exit:
541,945,027✔
394
  if (code) {
541,936,780✔
395
    vError("vgId:%d, %s:%d failed to vnodePreProcessSubmitTbData submit request since %s", TD_VID(pVnode), __func__,
70,578✔
396
           lino, tstrerror(code));
397
  }
398
  return code;
541,936,780✔
399
}
400
static int32_t vnodePreProcessSubmitMsg(SVnode *pVnode, SRpcMsg *pMsg) {
486,336,288✔
401
  int32_t code = 0;
486,336,288✔
402
  int32_t lino = 0;
486,336,288✔
403

404
  if (tsBypassFlag & TSDB_BYPASS_RA_RPC_RECV_SUBMIT) {
486,336,288✔
405
    return TSDB_CODE_MSG_PREPROCESSED;
276✔
406
  }
407

408
  SDecoder *pCoder = &(SDecoder){0};
486,336,012✔
409

410
  if (taosHton64(((SSubmitReq2Msg *)pMsg->pCont)->version) != 1) {
486,340,530✔
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));
486,342,834✔
416

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

422
  uint64_t nSubmitTbData;
486,334,922✔
423
  if (tDecodeU64v(pCoder, &nSubmitTbData) < 0) {
486,337,860✔
424
    code = TSDB_CODE_INVALID_MSG;
×
425
    TSDB_CHECK_CODE(code, lino, _exit);
×
426
  }
427

428
  int64_t btimeMs = taosGetTimestampMs();
486,342,576✔
429
  int64_t ctimeMs = btimeMs;
486,342,576✔
430
  for (int32_t i = 0; i < nSubmitTbData; i++) {
1,028,207,049✔
431
    code = vnodePreProcessSubmitTbData(pVnode, pCoder, btimeMs, ctimeMs);
541,931,980✔
432
    TSDB_CHECK_CODE(code, lino, _exit);
541,935,051✔
433
  }
434

435
  tEndDecode(pCoder);
486,275,069✔
436

437
_exit:
486,342,300✔
438
  tDecoderClear(pCoder);
486,344,370✔
439
  if (code) {
486,343,137✔
440
    vError("vgId:%d, %s:%d failed to preprocess submit request since %s, msg type:%s", TD_VID(pVnode), __func__, lino,
70,578✔
441
           tstrerror(code), TMSG_INFO(pMsg->msgType));
442
  }
443
  return code;
486,340,107✔
444
}
445

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

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

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

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

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

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

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

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

482
  taosArrayDestroy(res.uidList);
1,700,626✔
483

484
_exit:
1,700,626✔
485
  return code;
1,700,626✔
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) {
51,786✔
514
  int32_t ret = 0;
51,786✔
515
  if ((ret = vnodeCheckState(pVnode)) != 0) {
51,786✔
516
    vDebug("vgId:%d, failed to preprocess vnode-arb-check-sync request since %s", TD_VID(pVnode), tstrerror(ret));
20,847✔
517
    return 0;
20,847✔
518
  }
519

520
  SVArbCheckSyncReq syncReq = {0};
30,939✔
521

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

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

532
  int32_t code = terrno;
30,939✔
533
  tFreeSVArbCheckSyncReq(&syncReq);
30,939✔
534

535
  return code;
30,939✔
536
}
537

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

547
  tDecoderInit(&dc, POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), pMsg->contLen - sizeof(SMsgHead));
864,733✔
548

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

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

576
  tEncodeSize(tEncodeSVDropTbBatchReq, &sentBatchReqs, size, code);
864,733✔
577
  tEncoderInit(&ec, POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), size);
864,733✔
578
  code = tEncodeSVDropTbBatchReq(&ec, &sentBatchReqs);
864,733✔
579
  tEncoderClear(&ec);
864,733✔
580
  if (code != TSDB_CODE_SUCCESS) {
864,733✔
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:
864,733✔
586
  tDecoderClear(&dc);
864,733✔
587
  if (sentBatchReqs.pArray) {
864,733✔
588
    taosArrayDestroy(sentBatchReqs.pArray);
864,733✔
589
  }
590
  return code;
864,733✔
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) {
579,306,273✔
613
  int32_t code = 0;
579,306,273✔
614

615
  if (pVnode->mounted) {
579,306,273✔
616
    vError("vgId:%d, mountVgId:%d, skip write msg:%s", TD_VID(pVnode), pVnode->config.mountVgId,
3,230✔
617
           TMSG_INFO(pMsg->msgType));
618
    return TSDB_CODE_OPS_NOT_SUPPORT;
3,230✔
619
  }
620

621
  switch (pMsg->msgType) {
579,306,795✔
622
    case TDMT_VND_CREATE_TABLE: {
50,457,277✔
623
      code = vnodePreProcessCreateTableMsg(pVnode, pMsg);
50,457,277✔
624
    } break;
50,456,727✔
625
    case TDMT_VND_ALTER_TABLE: {
12,589,551✔
626
      code = vnodePreProcessAlterTableMsg(pVnode, pMsg);
12,589,551✔
627
    } break;
12,589,551✔
628
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
7,440,095✔
629
    case TDMT_VND_DROP_TTL_TABLE: {
630
      code = vnodePreProcessDropTtlMsg(pVnode, pMsg);
7,440,095✔
631
    } break;
7,428,366✔
632
    case TDMT_VND_SUBMIT: {
486,340,128✔
633
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.preprocess_time, METRIC_LEVEL_LOW,
486,340,128✔
634
                           { code = vnodePreProcessSubmitMsg(pVnode, pMsg); });
635
    } break;
486,342,001✔
636
    case TDMT_VND_DELETE: {
1,700,778✔
637
      code = vnodePreProcessDeleteMsg(pVnode, pMsg);
1,700,778✔
638
    } break;
1,700,626✔
639
    case TDMT_VND_BATCH_DEL: {
×
640
      code = vnodePreProcessBatchDeleteMsg(pVnode, pMsg);
×
641
    } break;
×
642
    case TDMT_VND_ARB_CHECK_SYNC: {
51,786✔
643
      code = vnodePreProcessArbCheckSyncMsg(pVnode, pMsg);
51,786✔
644
    } break;
51,786✔
645
    case TDMT_VND_DROP_TABLE: {
864,733✔
646
      code = vnodePreProcessDropTbMsg(pVnode, pMsg);
864,733✔
647
    } break;
864,733✔
648
#ifdef TD_ENTERPRISE
649
    case TDMT_VND_SSMIGRATE_FILESET: {
1,733✔
650
      code = vnodePreProcessSsMigrateFileSetReq(pVnode, pMsg);
1,733✔
651
    } break;
×
652
#endif
653
    default:
19,861,488✔
654
      break;
19,861,488✔
655
  }
656

657
  if (code && code != TSDB_CODE_MSG_PREPROCESSED) {
579,295,278✔
658
    vError("vgId:%d, failed to preprocess write request since %s, msg type:%s", TD_VID(pVnode), tstrerror(code),
101,019✔
659
           TMSG_INFO(pMsg->msgType));
660
  }
661
  return code;
579,295,780✔
662
}
663

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

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

672
  SBseBatch *pBatch = NULL;
22,830✔
673

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

677
  SRow  **pRow = (SRow **)TARRAY_DATA(pSubmitTbData->aRowP);
22,830✔
678
  int32_t rowIdx = -1;
22,830✔
679
  for (int32_t i = 0; i < sz; i++) {
26,131,098✔
680
    int64_t     seq = 0;
26,108,268✔
681
    SBlobValue *p = taosArrayGet(pBlobSet->pSeqTable, i);
26,108,268✔
682
    if (p->type == TSDB_DATA_BLOB_EMPTY_VALUE || p->type == TSDB_DATA_BLOB_NULL_VALUE) {
26,108,268✔
683
      // skip empty or null blob
684
      continue;
2,622,528✔
685
    }
686

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

690
    SRow *row = taosArrayGetP(pSubmitTbData->aRowP, i);
23,485,740✔
691
    if (row == NULL) {
23,485,740✔
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) {
46,971,480✔
698
      code = TSDB_CODE_INVALID_MSG;
×
699
      TSDB_CHECK_CODE(code, lino, _exit);
×
700
    }
701
  }
702

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

706
  int64_t cost = taosGetTimestampUs() - st;
22,830✔
707
  if (cost >= 500) {
22,830✔
708
    vDebug("vgId:%d, %s, cost:%" PRId64 "us, rows:%d, size:%" PRId64 "", TD_VID(pVnode), __func__, cost, sz,
7,706✔
709
           pBlobSet->len);
710
  }
711
_exit:
22,830✔
712
  if (code != 0) {
22,830✔
713
    vError("vgId:%d %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
714
  }
715
  return code;
22,830✔
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) {
22,830✔
777
  int32_t code = 0;
22,830✔
778
  if (pData->flags & SUBMIT_REQ_WITH_BLOB) {
22,830✔
779
    if (pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
22,830✔
780
      code = vnodeSubmitSubColBlobData(pVnode, pData);
×
781
    } else {
782
      code = vnodeSubmitSubRowBlobData(pVnode, pData);
22,830✔
783
    }
784
  }
785

786
  return code;
22,830✔
787
}
788

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

796
  (void)taosThreadMutexLock(&pVnode->mutex);
644,781,655✔
797
  if (pVnode->disableWrite) {
644,843,762✔
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);
644,813,845✔
803

804
  if (ver <= pVnode->state.applied) {
644,820,421✔
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,
644,770,332✔
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)) {
644,806,482✔
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)) {
644,852,535✔
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);
644,853,815✔
829
  atomic_store_64(&pVnode->state.applyTerm, pMsg->info.conn.applyTerm);
644,856,199✔
830

831
  if (!syncUtilUserCommit(pMsg->msgType)) goto _exit;
644,854,931✔
832

833
  // skip header
834
  pReq = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
634,372,094✔
835
  len = pMsg->contLen - sizeof(SMsgHead);
634,372,537✔
836
  bool needCommit = false;
634,372,039✔
837

838
  switch (pMsg->msgType) {
634,372,039✔
839
    /* META */
840
    case TDMT_VND_CREATE_STB:
6,788,791✔
841
      code = vnodeProcessCreateStbReq(pVnode, ver, pReq, len, pRsp);
6,788,791✔
842
      TSDB_CHECK_CODE(code, lino, _err);
6,788,303✔
843
      break;
6,785,984✔
844
    case TDMT_VND_ALTER_STB:
8,449,847✔
845
      code = vnodeProcessAlterStbReq(pVnode, ver, pReq, len, pRsp);
8,449,847✔
846
      TSDB_CHECK_CODE(code, lino, _err);
8,449,926✔
847
      break;
8,449,926✔
848
    case TDMT_VND_DROP_STB:
1,016,527✔
849
      code = vnodeProcessDropStbReq(pVnode, ver, pReq, len, pRsp, pMsg);
1,016,527✔
850
      TSDB_CHECK_CODE(code, lino, _err);
1,016,527✔
851
      break;
1,016,527✔
852
    case TDMT_VND_CREATE_TABLE:
52,875,787✔
853
      code = vnodeProcessCreateTbReq(pVnode, ver, pReq, len, pRsp, pMsg);
52,875,787✔
854
      TSDB_CHECK_CODE(code, lino, _err);
52,865,489✔
855
      break;
52,865,489✔
856
    case TDMT_VND_ALTER_TABLE:
12,590,979✔
857
      code = vnodeProcessAlterTbReq(pVnode, ver, pReq, len, pRsp);
12,590,979✔
858
      TSDB_CHECK_CODE(code, lino, _err);
12,590,979✔
859
      break;
12,590,979✔
860
    case TDMT_VND_DROP_TABLE:
865,440✔
861
    case TDMT_VND_SNODE_DROP_TABLE:
862
      code = vnodeProcessDropTbReq(pVnode, ver, pReq, len, pRsp, pMsg);
865,440✔
863
      TSDB_CHECK_CODE(code, lino, _err);
865,440✔
864
      break;
865,440✔
865
    case TDMT_VND_CREATE_RSMA:
23,190✔
866
      code = vnodeProcessCreateRsmaReq(pVnode, ver, pReq, len, pRsp);
23,190✔
867
      TSDB_CHECK_CODE(code, lino, _err);
23,190✔
868
      break;
23,190✔
869
    case TDMT_VND_DROP_RSMA:
4,638✔
870
      code = vnodeProcessDropRsmaReq(pVnode, ver, pReq, len, pRsp);
4,638✔
871
      TSDB_CHECK_CODE(code, lino, _err);
4,638✔
872
      break;
4,638✔
873
    case TDMT_VND_ALTER_RSMA:
12,368✔
874
      code = vnodeProcessAlterRsmaReq(pVnode, ver, pReq, len, pRsp);
12,368✔
875
      TSDB_CHECK_CODE(code, lino, _err);
12,368✔
876
      break;
12,368✔
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,228✔
882
      code = vnodeProcessFetchTtlExpiredTbs(pVnode, ver, pReq, len, pRsp);
12,228✔
883
      TSDB_CHECK_CODE(code, lino, _err);
12,228✔
884
      break;
12,228✔
885
    case TDMT_VND_TRIM:
54,496✔
886
      code = vnodeProcessTrimReq(pVnode, ver, pReq, len, pRsp);
54,496✔
887
      TSDB_CHECK_CODE(code, lino, _err);
54,496✔
888
      break;
54,496✔
889
      case TDMT_VND_TRIM_WAL:
3,592✔
890
      if (vnodeProcessTrimWalReq(pVnode, pReq, len, pRsp) < 0) goto _err;
3,592✔
891
      break;
3,592✔
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: {
538,827,827✔
906
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.apply_time, METRIC_LEVEL_LOW, {
538,827,827✔
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);
538,793,181✔
910
      break;
538,790,425✔
911
    }
912
    case TDMT_VND_DELETE:
1,772,980✔
913
      code = vnodeProcessDeleteReq(pVnode, ver, pReq, len, pRsp, pMsg);
1,772,980✔
914
      TSDB_CHECK_CODE(code, lino, _err);
1,772,828✔
915
      break;
1,772,828✔
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:
748,870✔
923
      code = tqProcessSubscribeReq(pVnode->pTq, ver, pReq, len);
748,870✔
924
      TSDB_CHECK_CODE(code, lino, _err);
745,939✔
925
      break;
745,939✔
926
    case TDMT_VND_TMQ_DELETE_SUB:
89,459✔
927
      code = tqProcessDeleteSubReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen);
89,459✔
928
      TSDB_CHECK_CODE(code, lino, _err);
88,331✔
929
      break;
88,331✔
930
    case TDMT_VND_TMQ_COMMIT_OFFSET:
628,019✔
931
      code = tqProcessOffsetCommitReq(pVnode->pTq, ver, pReq, len);
628,019✔
932
      TSDB_CHECK_CODE(code, lino, _err);
627,406✔
933
      break;
627,406✔
934
#endif
935
    case TDMT_VND_ALTER_CONFIRM:
3,856,708✔
936
      needCommit = pVnode->config.hashChange;
3,856,708✔
937
      code = vnodeProcessAlterConfirmReq(pVnode, ver, pReq, len, pRsp);
3,856,708✔
938
      TSDB_CHECK_CODE(code, lino, _err);
3,856,708✔
939
      break;
3,856,708✔
940
    case TDMT_VND_ALTER_CONFIG:
287,011✔
941
      vnodeProcessAlterConfigReq(pVnode, ver, pReq, len, pRsp);
287,011✔
942
      break;
287,011✔
943
    case TDMT_VND_COMMIT:
5,359,958✔
944
      needCommit = true;
5,359,958✔
945
      break;
5,359,958✔
946
    case TDMT_VND_CREATE_INDEX:
5,949✔
947
      vnodeProcessCreateIndexReq(pVnode, ver, pReq, len, pRsp);
5,949✔
948
      break;
5,949✔
949
    case TDMT_VND_DROP_INDEX:
3,962✔
950
      vnodeProcessDropIndexReq(pVnode, ver, pReq, len, pRsp);
3,962✔
951
      break;
3,962✔
952
    case TDMT_VND_COMPACT:
88,837✔
953
      vnodeProcessCompactVnodeReq(pVnode, ver, pReq, len, pRsp);
88,837✔
954
      goto _exit;
88,837✔
955
    case TDMT_VND_SCAN:
589✔
956
      vnodeProcessScanVnodeReq(pVnode, ver, pReq, len, pRsp);
589✔
957
      goto _exit;
589✔
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,546✔
963
      vnodeProcessKillCompactReq(pVnode, ver, pReq, len, pRsp);
1,546✔
964
      break;
1,546✔
965
#endif
966
    case TDMT_VND_KILL_SCAN:
238✔
967
      vnodeProcessKillScanReq(pVnode, ver, pReq, len, pRsp);
238✔
968
      break;
238✔
969
    case TDMT_VND_KILL_TRIM:
×
970
      vnodeProcessKillRetentionReq(pVnode, ver, pReq, len, pRsp);
×
971
      break;
×
972
    /* ARB */
973
    case TDMT_VND_ARB_CHECK_SYNC:
992✔
974
      vnodeProcessArbCheckSyncReq(pVnode, pReq, len, pRsp);
992✔
975
      break;
992✔
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);
634,226,150✔
982

983
  walApplyVer(pVnode->pWal, ver);
634,237,056✔
984

985
  if (pVnode->pTq) {
634,245,532✔
986
    code = tqPushMsg(pVnode->pTq, pMsg->msgType);
634,246,300✔
987
    if (code) {
634,246,440✔
988
      vError("vgId:%d, failed to push msg to TQ since %s", TD_VID(pVnode), tstrerror(terrno));
×
989
      return code;
×
990
    }
991
  }
992

993
  // commit if need
994
  if (needCommit) {
634,246,440✔
995
    vInfo("vgId:%d, commit at version %" PRId64, TD_VID(pVnode), ver);
5,380,778✔
996
    code = vnodeAsyncCommit(pVnode);
5,381,183✔
997
    if (code) {
5,380,778✔
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, {
5,380,778✔
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:
634,246,440✔
1013
  return 0;
644,817,537✔
1014

1015
_err:
36,787✔
1016
  if (code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
36,787✔
1017
    vInfo("vgId:%d, process %s request failed since %s, lino 1:%d ver:%" PRId64, TD_VID(pVnode),
2,319✔
1018
          TMSG_INFO(pMsg->msgType), tstrerror(code), lino, ver);
1019
  } else if (code) {
34,468✔
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;
36,787✔
1025
}
1026

1027
int32_t vnodePreprocessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg) {
164,224,066✔
1028
  if (TDMT_SCH_QUERY != pMsg->msgType && TDMT_SCH_MERGE_QUERY != pMsg->msgType) {
164,224,066✔
1029
    return 0;
16,488,032✔
1030
  }
1031

1032
  int32_t qType = 0;
147,742,168✔
1033
  return qWorkerPreprocessQueryMsg(pVnode->pQuery, pMsg, (TDMT_SCH_QUERY == pMsg->msgType), &qType);
147,743,625✔
1034
}
1035

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

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

1048
  SReadHandle handle = {0};
164,212,023✔
1049
  handle.vnode = pVnode;
164,209,345✔
1050
  handle.pMsgCb = &pVnode->msgCb;
164,209,345✔
1051
  handle.pWorkerCb = pInfo->workerCb;
164,194,364✔
1052
  initStorageAPI(&handle.api);
164,196,586✔
1053
  int32_t code = TSDB_CODE_SUCCESS;
164,158,813✔
1054
  bool    redirected = false;
164,158,813✔
1055

1056
  switch (pMsg->msgType) {
164,158,813✔
1057
    case TDMT_SCH_QUERY:
118,529,189✔
1058
      if (!syncIsReadyForRead(pVnode->sync)) {
118,529,189✔
1059
        pMsg->code = (terrno) ? terrno : TSDB_CODE_SYN_NOT_LEADER;
2,486,709✔
1060
        redirected = true;
2,486,709✔
1061
      }
1062
      code = qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
118,561,523✔
1063
      if (redirected) {
118,579,859✔
1064
        vnodeRedirectRpcMsg(pVnode, pMsg, pMsg->code);
2,486,709✔
1065
        return 0;
2,486,709✔
1066
      }
1067

1068
      return code;
116,093,150✔
1069
    case TDMT_SCH_MERGE_QUERY:
29,162,183✔
1070
      return qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
29,162,183✔
1071
    case TDMT_SCH_QUERY_CONTINUE:
9,655,754✔
1072
      return qWorkerProcessCQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
9,655,754✔
1073
    case TDMT_VND_TMQ_CONSUME:
5,354,999✔
1074
      return tqProcessPollReq(pVnode->pTq, pMsg);
5,354,999✔
1075
    case TDMT_VND_TMQ_CONSUME_PUSH:
1,455,536✔
1076
      return tqProcessPollPush(pVnode->pTq);
1,455,536✔
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) {
431,945,604✔
1084
  int32_t code = TSDB_CODE_SUCCESS;
431,945,604✔
1085
  vTrace("vgId:%d, msg:%p in fetch queue is processing", pVnode->config.vgId, pMsg);
431,945,604✔
1086
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
431,945,604✔
1087
       pMsg->msgType == TDMT_VND_BATCH_META || pMsg->msgType == TDMT_VND_TABLE_NAME ||
307,570,171✔
1088
       pMsg->msgType == TDMT_VND_VSUBTABLES_META || pMsg->msgType == TDMT_VND_VSTB_REF_DBS) &&
280,738,256✔
1089
      !syncIsReadyForRead(pVnode->sync)) {
151,223,654✔
1090
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
54,436✔
1091
    return 0;
54,436✔
1092
  }
1093

1094
  switch (pMsg->msgType) {
431,916,998✔
1095
    case TDMT_SCH_FETCH:
164,893,066✔
1096
    case TDMT_SCH_MERGE_FETCH:
1097
      return qWorkerProcessFetchMsg(pVnode, pVnode->pQuery, pMsg, 0);
164,893,066✔
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:
147,508,361✔
1103
      return qWorkerProcessDropMsg(pVnode, pVnode->pQuery, pMsg, 0);
147,508,361✔
1104
    case TDMT_SCH_TASK_NOTIFY:
11,989✔
1105
      return qWorkerProcessNotifyMsg(pVnode, pVnode->pQuery, pMsg, 0);
11,989✔
1106
    case TDMT_SCH_QUERY_HEARTBEAT:
91,751,991✔
1107
      return qWorkerProcessHbMsg(pVnode, pVnode->pQuery, pMsg, 0);
91,751,991✔
1108
    case TDMT_VND_TABLE_META:
752,917✔
1109
    case TDMT_VND_TABLE_NAME:
1110
      return vnodeGetTableMeta(pVnode, pMsg, true);
752,917✔
1111
    case TDMT_VND_TABLE_CFG:
×
1112
      return vnodeGetTableCfg(pVnode, pMsg, true);
×
1113
    case TDMT_VND_BATCH_META: {
26,788,449✔
1114
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.fetch_batch_meta_time, METRIC_LEVEL_LOW,
26,788,449✔
1115
                           { code = vnodeGetBatchMeta(pVnode, pMsg); });
1116
      METRICS_UPDATE(pVnode->writeMetrics.fetch_batch_meta_count, METRIC_LEVEL_LOW, 1);
26,789,075✔
1117
      return code;
26,788,449✔
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:
1,178✔
1124
      return vnodeQueryScanProgress(pVnode, pMsg);
1,178✔
1125
#ifdef TD_ENTERPRISE
1126
    case TDMT_VND_QUERY_COMPACT_PROGRESS:
114,811✔
1127
      return vnodeQueryCompactProgress(pVnode, pMsg);
114,811✔
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,404✔
1136
      return vnodeQueryRetentionProgress(pVnode, pMsg);
67,404✔
1137
      //    case TDMT_VND_TMQ_CONSUME:
1138
      //      return tqProcessPollReq(pVnode->pTq, pMsg);
1139
#ifdef USE_TQ
1140
    case TDMT_VND_TMQ_VG_WALINFO:
174✔
1141
      return tqProcessVgWalInfoReq(pVnode->pTq, pMsg);
174✔
1142
    case TDMT_VND_TMQ_VG_COMMITTEDINFO:
34✔
1143
      return tqProcessVgCommittedInfoReq(pVnode->pTq, pMsg);
34✔
1144
    case TDMT_VND_TMQ_SEEK:
136✔
1145
      return tqProcessSeekReq(pVnode->pTq, pMsg);
136✔
1146
#endif
1147
    default:
27,521✔
1148
      vError("unknown msg type:%d in fetch queue", pMsg->msgType);
27,521✔
1149
      return TSDB_CODE_APP_ERROR;
×
1150
  }
1151
}
1152

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

1158
  tstrncpy(pMetaRsp->dbFName, pVnode->config.dbname, TSDB_DB_FNAME_LEN);
75,026,233✔
1159
  pMetaRsp->dbId = pVnode->config.dbId;
75,027,615✔
1160
  pMetaRsp->vgId = TD_VID(pVnode);
75,027,615✔
1161
  pMetaRsp->precision = pVnode->config.tsdbCfg.precision;
75,026,687✔
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) {
54,496✔
1167
  if (!pVnode->restored) {
54,496✔
1168
    vInfo("vgId:%d, ignore trim req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
8,995✔
1169
    return 0;
8,995✔
1170
  }
1171

1172
  int32_t     code = 0;
45,501✔
1173
  SVTrimDbReq trimReq = {0};
45,501✔
1174

1175
  // decode
1176
  if (tDeserializeSVTrimDbReq(pReq, len, &trimReq) != 0) {
45,501✔
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,
45,501✔
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);
45,501✔
1185

1186
_exit:
45,501✔
1187
  return code;
45,501✔
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,592✔
1193
  int32_t code = 0;
3,592✔
1194

1195
  vInfo("vgId:%d, process trim wal request, force clean expired WAL files by triggering commit with forceTrim",
3,592✔
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,592✔
1201
  if (code != TSDB_CODE_SUCCESS) {
3,592✔
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,592✔
1205
  }
1206

1207
  return code;
3,592✔
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,228✔
1328
  int32_t                 code = -1;
12,228✔
1329
  SMetaReader             mr = {0};
12,228✔
1330
  SVDropTtlTableReq       ttlReq = {0};
12,228✔
1331
  SVFetchTtlExpiredTbsRsp rsp = {0};
12,228✔
1332
  SEncoder                encoder = {0};
12,228✔
1333
  SArray                 *pNames = NULL;
12,228✔
1334
  pRsp->msgType = TDMT_VND_FETCH_TTL_EXPIRED_TBS_RSP;
12,228✔
1335
  pRsp->code = TSDB_CODE_SUCCESS;
12,228✔
1336
  pRsp->pCont = NULL;
12,228✔
1337
  pRsp->contLen = 0;
12,228✔
1338

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

1344
  if (!(ttlReq.nUids == taosArrayGetSize(ttlReq.pTbUids))) {
12,228✔
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,228✔
1352
  metaReaderDoInit(&mr, pVnode->pMeta, 0);
12,228✔
1353
  rsp.vgId = TD_VID(pVnode);
12,228✔
1354
  rsp.pExpiredTbs = taosArrayInit(ttlReq.nUids, sizeof(SVDropTbReq));
12,228✔
1355
  if (!rsp.pExpiredTbs) goto _end;
12,228✔
1356

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

1374
    expiredTb.name = p;
49,589✔
1375
    if (mr.me.type == TSDB_CHILD_TABLE) {
49,589✔
1376
      expiredTb.suid = mr.me.ctbEntry.suid;
31,959✔
1377
    }
1378

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

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

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

1406
static int32_t vnodeProcessCreateStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
6,784,053✔
1407
  int32_t        code = 0;
6,784,053✔
1408
  SVCreateStbReq req = {0};
6,784,053✔
1409
  SDecoder       coder;
6,788,240✔
1410

1411
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
6,788,687✔
1412
  pRsp->code = TSDB_CODE_SUCCESS;
6,788,687✔
1413
  pRsp->pCont = NULL;
6,788,687✔
1414
  pRsp->contLen = 0;
6,788,226✔
1415

1416
  // decode and process req
1417
  tDecoderInit(&coder, pReq, len);
6,787,877✔
1418

1419
  code = tDecodeSVCreateStbReq(&coder, &req);
6,788,687✔
1420
  if (code) {
6,779,628✔
1421
    pRsp->code = code;
×
1422
    goto _err;
×
1423
  }
1424

1425
  code = metaCreateSuperTable(pVnode->pMeta, ver, &req);
6,779,628✔
1426
  if (code) {
6,788,975✔
1427
    pRsp->code = code;
2,319✔
1428
    goto _err;
2,319✔
1429
  }
1430

1431
  tDecoderClear(&coder);
6,786,656✔
1432
  return 0;
6,786,656✔
1433

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

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

1456
  // decode
1457
  tDecoderInit(&decoder, pReq, len);
52,875,718✔
1458
  if (tDecodeSVCreateTbBatchReq(&decoder, &req) < 0) {
52,875,379✔
1459
    rcode = -1;
×
1460
    terrno = TSDB_CODE_INVALID_MSG;
×
1461
    goto _exit;
×
1462
  }
1463

1464
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(cRsp));
52,874,350✔
1465
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
52,871,195✔
1466
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
52,867,777✔
1467
  if (rsp.pArray == NULL || tbUids == NULL || tbNames == NULL) {
52,868,398✔
1468
    rcode = -1;
×
1469
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1470
    goto _exit;
×
1471
  }
1472

1473
  // loop to create table
1474
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
112,041,968✔
1475
    pCreateReq = req.pReqs + iReq;
59,167,364✔
1476
    memset(&cRsp, 0, sizeof(cRsp));
59,160,644✔
1477

1478
    if (tsEnableAudit && tsEnableAuditCreateTable) {
59,160,644✔
1479
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
59,162,238✔
1480
      if (str == NULL) {
59,150,643✔
1481
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1482
        rcode = -1;
×
1483
        goto _exit;
×
1484
      }
1485
      tstrncpy(str, pCreateReq->name, TSDB_TABLE_FNAME_LEN);
59,150,643✔
1486
      if (taosArrayPush(tbNames, &str) == NULL) {
59,164,996✔
1487
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1488
        rcode = -1;
×
1489
        goto _exit;
×
1490
      }
1491
    }
1492

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

1506
    // do create table
1507
    if (metaCreateTable2(pVnode->pMeta, ver, pCreateReq, &cRsp.pMeta) < 0) {
59,152,785✔
1508
      if (pCreateReq->flags & TD_CREATE_IF_NOT_EXISTS && terrno == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
45,762✔
1509
        cRsp.code = TSDB_CODE_SUCCESS;
32,754✔
1510
      } else {
1511
        cRsp.code = terrno;
13,008✔
1512
      }
1513
    } else {
1514
      cRsp.code = TSDB_CODE_SUCCESS;
59,122,952✔
1515
      if (taosArrayPush(tbUids, &pCreateReq->uid) == NULL) {
118,246,111✔
1516
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1517
        rcode = -1;
×
1518
        goto _exit;
×
1519
      }
1520
      vnodeUpdateMetaRsp(pVnode, cRsp.pMeta);
59,123,159✔
1521
    }
1522

1523
    if (taosArrayPush(rsp.pArray, &cRsp) == NULL) {
118,337,611✔
1524
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1525
      rcode = -1;
×
1526
      goto _exit;
×
1527
    }
1528
  }
1529

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

1535
  // prepare rsp
1536
  int32_t ret = 0;
52,875,127✔
1537
  tEncodeSize(tEncodeSVCreateTbBatchRsp, &rsp, pRsp->contLen, ret);
52,875,127✔
1538
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
52,873,826✔
1539
  if (pRsp->pCont == NULL) {
52,869,010✔
1540
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1541
    rcode = -1;
×
1542
    goto _exit;
×
1543
  }
1544
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
52,869,526✔
1545
  if (tEncodeSVCreateTbBatchRsp(&encoder, &rsp) != 0) {
52,866,993✔
1546
    vError("vgId:%d, failed to encode create table batch response", TD_VID(pVnode));
×
1547
  }
1548

1549
  if (tsEnableAudit && tsEnableAuditCreateTable) {
52,868,690✔
1550
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
52,868,958✔
1551

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

1557
    SStringBuilder sb = {0};
52,868,196✔
1558
    for (int32_t i = 0; i < tbNames->size; i++) {
112,017,966✔
1559
      char **key = (char **)taosArrayGet(tbNames, i);
59,163,802✔
1560
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
59,163,507✔
1561
      if (i < tbNames->size - 1) {
59,155,245✔
1562
        taosStringBuilderAppendChar(&sb, ',');
6,292,211✔
1563
      }
1564
      // taosMemoryFreeClear(*key);
1565
    }
1566

1567
    size_t len = 0;
52,861,399✔
1568
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
52,864,807✔
1569

1570
    if (pOriginRpc->info.conn.user != NULL && strlen(pOriginRpc->info.conn.user) > 0) {
52,859,863✔
1571
      auditAddRecord(pOriginRpc, clusterId, "createTable", name.dbname, "", keyJoined, len);
50,453,794✔
1572
    }
1573

1574
    taosStringBuilderDestroy(&sb);
52,869,199✔
1575
  }
1576

1577
_exit:
52,863,819✔
1578
  tDeleteSVCreateTbBatchReq(&req);
52,868,729✔
1579
  taosArrayDestroyEx(rsp.pArray, tFreeSVCreateTbRsp);
52,865,474✔
1580
  taosArrayDestroy(tbUids);
52,865,004✔
1581
  tDecoderClear(&decoder);
52,861,818✔
1582
  tEncoderClear(&encoder);
52,870,244✔
1583
  taosArrayDestroyP(tbNames, NULL);
52,869,336✔
1584
  return rcode;
52,867,252✔
1585
}
1586

1587
static int32_t vnodeProcessAlterStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
8,449,926✔
1588
  int32_t        code = 0;
8,449,926✔
1589
  SVCreateStbReq req = {0};
8,449,926✔
1590
  SDecoder       dc = {0};
8,449,926✔
1591

1592
  pRsp->msgType = TDMT_VND_ALTER_STB_RSP;
8,449,926✔
1593
  pRsp->code = TSDB_CODE_SUCCESS;
8,449,926✔
1594
  pRsp->pCont = NULL;
8,449,926✔
1595
  pRsp->contLen = 0;
8,449,926✔
1596

1597
  tDecoderInit(&dc, pReq, len);
8,449,635✔
1598

1599
  // decode req
1600
  code = tDecodeSVCreateStbReq(&dc, &req);
8,449,847✔
1601
  if (code) {
8,441,826✔
1602
    tDecoderClear(&dc);
×
1603
    return code;
×
1604
  }
1605

1606
  code = metaAlterSuperTable(pVnode->pMeta, ver, &req);
8,441,826✔
1607
  if (code) {
8,449,926✔
1608
    pRsp->code = code;
×
1609
    tDecoderClear(&dc);
×
1610
    return code;
×
1611
  }
1612

1613
  tDecoderClear(&dc);
8,449,926✔
1614

1615
  return 0;
8,449,926✔
1616
}
1617

1618
static int32_t vnodeProcessDropStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp, SRpcMsg *pOriginRpc) {
1,016,527✔
1619
  SVDropStbReq req = {0};
1,016,527✔
1620
  int32_t      rcode = TSDB_CODE_SUCCESS;
1,016,527✔
1621
  SDecoder     decoder = {0};
1,016,527✔
1622
  SArray      *tbUidList = NULL;
1,016,527✔
1623

1624
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
1,016,527✔
1625
  pRsp->pCont = NULL;
1,016,527✔
1626
  pRsp->contLen = 0;
1,016,527✔
1627

1628
  // decode request
1629
  tDecoderInit(&decoder, pReq, len);
1,016,527✔
1630
  if (tDecodeSVDropStbReq(&decoder, &req) < 0) {
1,016,527✔
1631
    rcode = TSDB_CODE_INVALID_MSG;
×
1632
    goto _exit;
×
1633
  }
1634

1635
  STraceId* trace = &(pOriginRpc->info.traceId);
1,016,527✔
1636

1637
  vInfo("vgId:%d, start to process vnode-drop-stb, QID:0x%" PRIx64 ":0x%" PRIx64 ", drop stb:%s", TD_VID(pVnode), trace ? trace->rootId : 0, 
1,016,527✔
1638
              trace ? trace->msgId : 0, req.name);
1639

1640
  // process request
1641
  tbUidList = taosArrayInit(8, sizeof(int64_t));
1,017,227✔
1642
  if (tbUidList == NULL) goto _exit;
1,016,527✔
1643
  if (metaDropSuperTable(pVnode->pMeta, ver, &req) < 0) {
1,016,527✔
1644
    rcode = terrno;
1,546✔
1645
    goto _exit;
1,546✔
1646
  }
1647

1648
  if (tqUpdateTbUidList(pVnode->pTq, tbUidList, false) < 0) {
1,014,981✔
1649
    rcode = terrno;
×
1650
    goto _exit;
×
1651
  }
1652

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

1663
static int32_t vnodeProcessAlterTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
12,590,979✔
1664
  SVAlterTbReq  vAlterTbReq = {0};
12,590,979✔
1665
  SVAlterTbRsp  vAlterTbRsp = {0};
12,590,979✔
1666
  SDecoder      dc = {0};
12,590,979✔
1667
  int32_t       code = 0;
12,590,979✔
1668
  int32_t       lino = 0;
12,590,979✔
1669
  int32_t       ret;
1670
  SEncoder      ec = {0};
12,590,979✔
1671
  STableMetaRsp vMetaRsp = {0};
12,590,979✔
1672

1673
  pRsp->msgType = TDMT_VND_ALTER_TABLE_RSP;
12,590,979✔
1674
  pRsp->pCont = NULL;
12,590,979✔
1675
  pRsp->contLen = 0;
12,590,979✔
1676
  pRsp->code = TSDB_CODE_SUCCESS;
12,590,979✔
1677

1678
  tDecoderInit(&dc, pReq, len);
12,590,979✔
1679

1680
  // decode
1681
  if (tDecodeSVAlterTbReq(&dc, &vAlterTbReq) < 0) {
12,590,979✔
1682
    vAlterTbRsp.code = TSDB_CODE_INVALID_MSG;
×
1683
    tDecoderClear(&dc);
×
1684
    goto _exit;
×
1685
  }
1686

1687
  // process
1688
  if (metaAlterTable(pVnode->pMeta, ver, &vAlterTbReq, &vMetaRsp) < 0) {
12,590,979✔
1689
    vAlterTbRsp.code = terrno;
568,856✔
1690
    tDecoderClear(&dc);
568,856✔
1691
    goto _exit;
568,856✔
1692
  }
1693
  tDecoderClear(&dc);
12,022,123✔
1694

1695
  if (NULL != vMetaRsp.pSchemas) {
12,022,123✔
1696
    vnodeUpdateMetaRsp(pVnode, &vMetaRsp);
3,895,858✔
1697
    vAlterTbRsp.pMeta = &vMetaRsp;
3,895,858✔
1698
  }
1699

1700
  if (vAlterTbReq.action == TSDB_ALTER_TABLE_UPDATE_TAG_VAL ||
12,022,123✔
1701
      vAlterTbReq.action == TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL) {
3,923,587✔
1702
    int64_t uid = metaGetTableEntryUidByName(pVnode->pMeta, vAlterTbReq.tbName);
8,102,001✔
1703
    if (uid == 0) {
8,102,001✔
1704
      vError("vgId:%d, %s failed at %s:%d since table %s not found", TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
1705
             vAlterTbReq.tbName);
1706
      goto _exit;
×
1707
    }
1708

1709
    SArray *tbUids = taosArrayInit(4, sizeof(int64_t));
8,102,001✔
1710
    void   *p = taosArrayPush(tbUids, &uid);
8,102,001✔
1711
    TSDB_CHECK_NULL(p, code, lino, _exit, terrno);
8,102,001✔
1712

1713
    vDebug("vgId:%d, remove tags value altered table:%s from query table list", TD_VID(pVnode), vAlterTbReq.tbName);
8,102,001✔
1714
    if ((code = tqUpdateTbUidList(pVnode->pTq, tbUids, false)) < 0) {
8,102,001✔
1715
      vError("vgId:%d, failed to remove tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1716
    }
1717

1718
    vDebug("vgId:%d, try to add table:%s in query table list", TD_VID(pVnode), vAlterTbReq.tbName);
8,102,001✔
1719
    if ((code = tqUpdateTbUidList(pVnode->pTq, tbUids, true)) < 0) {
8,102,001✔
1720
      vError("vgId:%d, failed to add tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1721
    }
1722

1723
    taosArrayDestroy(tbUids);
8,102,001✔
1724
  }
1725

1726
_exit:
12,590,979✔
1727
  taosArrayDestroy(vAlterTbReq.pMultiTag);
12,590,979✔
1728
  tEncodeSize(tEncodeSVAlterTbRsp, &vAlterTbRsp, pRsp->contLen, ret);
12,590,979✔
1729
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
12,590,979✔
1730
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
12,590,979✔
1731
  if (tEncodeSVAlterTbRsp(&ec, &vAlterTbRsp) != 0) {
12,590,979✔
1732
    vError("vgId:%d, failed to encode alter table response", TD_VID(pVnode));
×
1733
  }
1734

1735
  tEncoderClear(&ec);
12,590,979✔
1736
  if (vMetaRsp.pSchemas) {
12,590,979✔
1737
    taosMemoryFree(vMetaRsp.pSchemas);
3,895,858✔
1738
    taosMemoryFree(vMetaRsp.pSchemaExt);
3,895,858✔
1739
  }
1740
  if (vMetaRsp.pColRefs) {
12,590,979✔
1741
    taosMemoryFree(vMetaRsp.pColRefs);
286,084✔
1742
  }
1743
  return 0;
12,590,979✔
1744
}
1745

1746
static int32_t vnodeProcessDropTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
865,440✔
1747
                                     SRpcMsg *pOriginRpc) {
1748
  SVDropTbBatchReq req = {0};
865,440✔
1749
  SVDropTbBatchRsp rsp = {0};
865,440✔
1750
  SDecoder         decoder = {0};
865,440✔
1751
  SEncoder         encoder = {0};
865,440✔
1752
  int32_t          ret;
1753
  SArray          *tbUids = NULL;
865,440✔
1754
  SArray          *tbNames = NULL;
865,440✔
1755

1756
  pRsp->msgType = ((SRpcMsg *)pReq)->msgType + 1;
865,440✔
1757
  pRsp->pCont = NULL;
865,440✔
1758
  pRsp->contLen = 0;
865,440✔
1759
  pRsp->code = TSDB_CODE_SUCCESS;
865,440✔
1760

1761
  // decode req
1762
  tDecoderInit(&decoder, pReq, len);
865,440✔
1763
  ret = tDecodeSVDropTbBatchReq(&decoder, &req);
865,440✔
1764
  if (ret < 0) {
865,440✔
1765
    terrno = TSDB_CODE_INVALID_MSG;
×
1766
    pRsp->code = terrno;
×
1767
    goto _exit;
×
1768
  }
1769

1770
  // process req
1771
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
865,440✔
1772
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(SVDropTbRsp));
865,440✔
1773
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
865,440✔
1774
  if (tbUids == NULL || rsp.pArray == NULL || tbNames == NULL) goto _exit;
865,440✔
1775

1776
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
1,862,472✔
1777
    SVDropTbReq *pDropTbReq = req.pReqs + iReq;
997,032✔
1778
    SVDropTbRsp  dropTbRsp = {0};
997,032✔
1779
    tb_uid_t     tbUid = 0;
997,032✔
1780

1781
    /* code */
1782
    ret = metaDropTable2(pVnode->pMeta, ver, pDropTbReq);
997,032✔
1783
    if (ret < 0) {
997,032✔
1784
      if (pDropTbReq->igNotExists && terrno == TSDB_CODE_TDB_TABLE_NOT_EXIST) {
×
1785
        dropTbRsp.code = TSDB_CODE_SUCCESS;
×
1786
      } else {
1787
        dropTbRsp.code = terrno;
×
1788
      }
1789
    } else {
1790
      dropTbRsp.code = TSDB_CODE_SUCCESS;
997,032✔
1791
    }
1792

1793
    if (taosArrayPush(rsp.pArray, &dropTbRsp) == NULL) {
1,994,064✔
1794
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1795
      pRsp->code = terrno;
×
1796
      goto _exit;
×
1797
    }
1798

1799
    if (tsEnableAuditCreateTable) {
997,032✔
1800
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
997,032✔
1801
      if (str == NULL) {
997,032✔
1802
        pRsp->code = terrno;
×
1803
        goto _exit;
×
1804
      }
1805
      tstrncpy(str, pDropTbReq->name, TSDB_TABLE_FNAME_LEN);
997,032✔
1806
      if (taosArrayPush(tbNames, &str) == NULL) {
997,032✔
1807
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1808
        pRsp->code = terrno;
×
1809
        goto _exit;
×
1810
      }
1811
    }
1812
  }
1813

1814
  if (tqUpdateTbUidList(pVnode->pTq, tbUids, false) < 0) {
865,440✔
1815
    vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(terrno));
×
1816
  }
1817

1818
  if (tsEnableAuditCreateTable) {
865,440✔
1819
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
865,440✔
1820

1821
    SName name = {0};
865,440✔
1822
    if (tNameFromString(&name, pVnode->config.dbname, T_NAME_ACCT | T_NAME_DB) != 0) {
865,440✔
1823
      vError("vgId:%d, failed to get name from string", TD_VID(pVnode));
×
1824
    }
1825

1826
    SStringBuilder sb = {0};
865,440✔
1827
    for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
1,862,472✔
1828
      char **key = (char **)taosArrayGet(tbNames, iReq);
997,032✔
1829
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
997,032✔
1830
      if (iReq < req.nReqs - 1) {
997,032✔
1831
        taosStringBuilderAppendChar(&sb, ',');
131,592✔
1832
      }
1833
      taosMemoryFreeClear(*key);
997,032✔
1834
    }
1835

1836
    size_t len = 0;
865,440✔
1837
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
865,440✔
1838

1839
    if (pOriginRpc->info.conn.user != NULL && strlen(pOriginRpc->info.conn.user) > 0) {
865,440✔
1840
      auditAddRecord(pOriginRpc, clusterId, "dropTable", name.dbname, "", keyJoined, len);
865,164✔
1841
    }
1842

1843
    taosStringBuilderDestroy(&sb);
865,440✔
1844
  }
1845

1846
_exit:
865,440✔
1847
  taosArrayDestroy(tbUids);
865,440✔
1848
  tDecoderClear(&decoder);
865,440✔
1849
  tEncodeSize(tEncodeSVDropTbBatchRsp, &rsp, pRsp->contLen, ret);
865,440✔
1850
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
865,440✔
1851
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
865,440✔
1852
  if (tEncodeSVDropTbBatchRsp(&encoder, &rsp) != 0) {
865,440✔
1853
    vError("vgId:%d, failed to encode drop table batch response", TD_VID(pVnode));
×
1854
  }
1855
  tEncoderClear(&encoder);
865,440✔
1856
  taosArrayDestroy(rsp.pArray);
865,440✔
1857
  taosArrayDestroy(tbNames);
865,440✔
1858
  return 0;
865,440✔
1859
}
1860

1861
static int32_t vnodeProcessCreateRsmaReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
23,190✔
1862
  int32_t         code = 0, lino = 0;
23,190✔
1863
  SVCreateRsmaReq req = {0};
23,190✔
1864
  TAOS_CHECK_EXIT(tDeserializeSVCreateRsmaReq(pReq, len, &req));
23,190✔
1865
  TAOS_CHECK_EXIT(metaCreateRsma(pVnode->pMeta, ver, &req));
23,190✔
1866
_exit:
23,190✔
1867
  pRsp->msgType = TDMT_VND_CREATE_RSMA_RSP;
23,190✔
1868
  pRsp->pCont = NULL;
23,190✔
1869
  pRsp->code = code;
23,190✔
1870
  pRsp->contLen = 0;
23,190✔
1871
  tFreeSVCreateRsmaReq(&req);
23,190✔
1872
  return code;
23,190✔
1873
}
1874

1875
static int32_t vnodeProcessDropRsmaReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
4,638✔
1876
  int32_t       code = 0, lino = 0;
4,638✔
1877
  SVDropRsmaReq req = {0};
4,638✔
1878
  TAOS_CHECK_EXIT(tDeserializeSVDropRsmaReq(pReq, len, &req));
4,638✔
1879
  TAOS_CHECK_EXIT(metaDropRsma(pVnode->pMeta, ver, &req));
4,638✔
1880
_exit:
4,638✔
1881
  pRsp->msgType = TDMT_VND_DROP_RSMA_RSP;
4,638✔
1882
  pRsp->pCont = NULL;
4,638✔
1883
  pRsp->code = code;
4,638✔
1884
  pRsp->contLen = 0;
4,638✔
1885
  return 0;
4,638✔
1886
}
1887

1888
static int32_t vnodeProcessAlterRsmaReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
12,368✔
1889
  int32_t         code = 0, lino = 0;
12,368✔
1890
  SVAlterRsmaReq req = {0};
12,368✔
1891
  TAOS_CHECK_EXIT(tDeserializeSVAlterRsmaReq(pReq, len, &req));
12,368✔
1892
  TAOS_CHECK_EXIT(metaAlterRsma(pVnode->pMeta, ver, &req));
12,368✔
1893
_exit:
12,368✔
1894
  pRsp->msgType = TDMT_VND_ALTER_RSMA_RSP;
12,368✔
1895
  pRsp->pCont = NULL;
12,368✔
1896
  pRsp->code = code;
12,368✔
1897
  pRsp->contLen = 0;
12,368✔
1898
  tFreeSVAlterRsmaReq(&req);
12,368✔
1899
  return code;
12,368✔
1900
}
1901

1902
#ifdef BUILD_NO_CALL
1903
static int32_t vnodeDebugPrintSingleSubmitMsg(SMeta *pMeta, SSubmitBlk *pBlock, SSubmitMsgIter *msgIter,
1904
                                              const char *tags) {
1905
  SSubmitBlkIter blkIter = {0};
1906
  STSchema      *pSchema = NULL;
1907
  tb_uid_t       suid = 0;
1908
  STSRow        *row = NULL;
1909
  int32_t        rv = -1;
1910

1911
  tInitSubmitBlkIter(msgIter, pBlock, &blkIter);
1912
  if (blkIter.row == NULL) return 0;
1913

1914
  int32_t code = metaGetTbTSchemaNotNull(pMeta, msgIter->suid, TD_ROW_SVER(blkIter.row), 1,
1915
                                         &pSchema);  // TODO: use the real schema
1916
  if (TSDB_CODE_SUCCESS != code) {
1917
    printf("%s:%d no valid schema\n", tags, __LINE__);
1918
    return code;
1919
  }
1920

1921
  suid = msgIter->suid;
1922
  rv = TD_ROW_SVER(blkIter.row);
1923

1924
  char __tags[128] = {0};
1925
  snprintf(__tags, 128, "%s: uid %" PRIi64 " ", tags, msgIter->uid);
1926
  while ((row = tGetSubmitBlkNext(&blkIter))) {
1927
    tdSRowPrint(row, pSchema, __tags);
1928
  }
1929

1930
  taosMemoryFreeClear(pSchema);
1931

1932
  return TSDB_CODE_SUCCESS;
1933
}
1934
#endif
1935
typedef struct SSubmitReqConvertCxt {
1936
  SSubmitMsgIter msgIter;
1937
  SSubmitBlk    *pBlock;
1938
  SSubmitBlkIter blkIter;
1939
  STSRow        *pRow;
1940
  STSRowIter     rowIter;
1941
  SSubmitTbData *pTbData;
1942
  STSchema      *pTbSchema;
1943
  SArray        *pColValues;
1944
} SSubmitReqConvertCxt;
1945

1946
static int32_t vnodeResetTableCxt(SMeta *pMeta, SSubmitReqConvertCxt *pCxt) {
×
1947
  taosMemoryFreeClear(pCxt->pTbSchema);
×
1948
  int32_t code = metaGetTbTSchemaNotNull(pMeta, pCxt->msgIter.suid > 0 ? pCxt->msgIter.suid : pCxt->msgIter.uid,
×
1949
                                         pCxt->msgIter.sversion, 1, &pCxt->pTbSchema);
1950
  if (TSDB_CODE_SUCCESS != code) {
×
1951
    return code;
×
1952
  }
1953
  tdSTSRowIterInit(&pCxt->rowIter, pCxt->pTbSchema);
×
1954

1955
  tDestroySubmitTbData(pCxt->pTbData, TSDB_MSG_FLG_ENCODE);
×
1956
  if (NULL == pCxt->pTbData) {
×
1957
    pCxt->pTbData = taosMemoryCalloc(1, sizeof(SSubmitTbData));
×
1958
    if (NULL == pCxt->pTbData) {
×
1959
      return terrno;
×
1960
    }
1961
  }
1962
  pCxt->pTbData->flags = 0;
×
1963
  pCxt->pTbData->suid = pCxt->msgIter.suid;
×
1964
  pCxt->pTbData->uid = pCxt->msgIter.uid;
×
1965
  pCxt->pTbData->sver = pCxt->msgIter.sversion;
×
1966
  pCxt->pTbData->pCreateTbReq = NULL;
×
1967
  pCxt->pTbData->aRowP = taosArrayInit(128, POINTER_BYTES);
×
1968
  if (NULL == pCxt->pTbData->aRowP) {
×
1969
    return terrno;
×
1970
  }
1971

1972
  taosArrayDestroy(pCxt->pColValues);
×
1973
  pCxt->pColValues = taosArrayInit(pCxt->pTbSchema->numOfCols, sizeof(SColVal));
×
1974
  if (NULL == pCxt->pColValues) {
×
1975
    return terrno;
×
1976
  }
1977
  for (int32_t i = 0; i < pCxt->pTbSchema->numOfCols; ++i) {
×
1978
    SColVal val = COL_VAL_NONE(pCxt->pTbSchema->columns[i].colId, pCxt->pTbSchema->columns[i].type);
×
1979
    if (taosArrayPush(pCxt->pColValues, &val) == NULL) {
×
1980
      return terrno;
×
1981
    }
1982
  }
1983

1984
  return TSDB_CODE_SUCCESS;
×
1985
}
1986

1987
static void vnodeDestroySubmitReqConvertCxt(SSubmitReqConvertCxt *pCxt) {
×
1988
  taosMemoryFreeClear(pCxt->pTbSchema);
×
1989
  tDestroySubmitTbData(pCxt->pTbData, TSDB_MSG_FLG_ENCODE);
×
1990
  taosMemoryFreeClear(pCxt->pTbData);
×
1991
  taosArrayDestroy(pCxt->pColValues);
×
1992
}
×
1993

1994
static int32_t vnodeCellValConvertToColVal(STColumn *pCol, SCellVal *pCellVal, SColVal *pColVal) {
×
1995
  if (tdValTypeIsNone(pCellVal->valType)) {
×
1996
    pColVal->flag = CV_FLAG_NONE;
×
1997
    return TSDB_CODE_SUCCESS;
×
1998
  }
1999

2000
  if (tdValTypeIsNull(pCellVal->valType)) {
×
2001
    pColVal->flag = CV_FLAG_NULL;
×
2002
    return TSDB_CODE_SUCCESS;
×
2003
  }
2004

2005
  if (IS_VAR_DATA_TYPE(pCol->type)) {
×
2006
    if (IS_STR_DATA_BLOB(pCol->type)) {
×
2007
      pColVal->value.nData = blobDataLen(pCellVal->val);
×
2008
      pColVal->value.pData = (uint8_t *)blobDataVal(pCellVal->val);
×
2009

2010
    } else {
2011
      pColVal->value.nData = varDataLen(pCellVal->val);
×
2012
      pColVal->value.pData = (uint8_t *)varDataVal(pCellVal->val);
×
2013
    }
2014
  } else if (TSDB_DATA_TYPE_FLOAT == pCol->type) {
×
2015
    float f = GET_FLOAT_VAL(pCellVal->val);
×
2016
    valueSetDatum(&pColVal->value, pCol->type, &f, sizeof(f));
×
2017
  } else if (TSDB_DATA_TYPE_DOUBLE == pCol->type) {
×
2018
    taosSetPInt64Aligned(&pColVal->value.val, (int64_t *)pCellVal->val);
×
2019
  } else {
2020
    valueSetDatum(&pColVal->value, pCol->type, pCellVal->val, tDataTypes[pCol->type].bytes);
×
2021
  }
2022

2023
  pColVal->flag = CV_FLAG_VALUE;
×
2024
  return TSDB_CODE_SUCCESS;
×
2025
}
2026

2027
static int32_t vnodeTSRowConvertToColValArray(SSubmitReqConvertCxt *pCxt) {
×
2028
  int32_t code = TSDB_CODE_SUCCESS;
×
2029
  tdSTSRowIterReset(&pCxt->rowIter, pCxt->pRow);
×
2030
  for (int32_t i = 0; TSDB_CODE_SUCCESS == code && i < pCxt->pTbSchema->numOfCols; ++i) {
×
2031
    STColumn *pCol = pCxt->pTbSchema->columns + i;
×
2032
    SCellVal  cellVal = {0};
×
2033
    if (!tdSTSRowIterFetch(&pCxt->rowIter, pCol->colId, pCol->type, &cellVal)) {
×
2034
      break;
×
2035
    }
2036
    code = vnodeCellValConvertToColVal(pCol, &cellVal, (SColVal *)taosArrayGet(pCxt->pColValues, i));
×
2037
  }
2038
  return code;
×
2039
}
2040

2041
static int32_t vnodeDecodeCreateTbReq(SSubmitReqConvertCxt *pCxt) {
×
2042
  if (pCxt->msgIter.schemaLen <= 0) {
×
2043
    return TSDB_CODE_SUCCESS;
×
2044
  }
2045

2046
  pCxt->pTbData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
×
2047
  if (NULL == pCxt->pTbData->pCreateTbReq) {
×
2048
    return terrno;
×
2049
  }
2050

2051
  SDecoder decoder = {0};
×
2052
  tDecoderInit(&decoder, (uint8_t *)pCxt->pBlock->data, pCxt->msgIter.schemaLen);
×
2053
  int32_t code = tDecodeSVCreateTbReq(&decoder, pCxt->pTbData->pCreateTbReq);
×
2054
  tDecoderClear(&decoder);
×
2055

2056
  return code;
×
2057
}
2058

2059
static int32_t vnodeSubmitReqConvertToSubmitReq2(SVnode *pVnode, SSubmitReq *pReq, SSubmitReq2 *pReq2) {
×
2060
  pReq2->aSubmitTbData = taosArrayInit(128, sizeof(SSubmitTbData));
×
2061
  if (NULL == pReq2->aSubmitTbData) {
×
2062
    return terrno;
×
2063
  }
2064

2065
  SSubmitReqConvertCxt cxt = {0};
×
2066

2067
  int32_t code = tInitSubmitMsgIter(pReq, &cxt.msgIter);
×
2068
  while (TSDB_CODE_SUCCESS == code) {
×
2069
    code = tGetSubmitMsgNext(&cxt.msgIter, &cxt.pBlock);
×
2070
    if (TSDB_CODE_SUCCESS == code) {
×
2071
      if (NULL == cxt.pBlock) {
×
2072
        break;
×
2073
      }
2074
      code = vnodeResetTableCxt(pVnode->pMeta, &cxt);
×
2075
    }
2076
    if (TSDB_CODE_SUCCESS == code) {
×
2077
      code = tInitSubmitBlkIter(&cxt.msgIter, cxt.pBlock, &cxt.blkIter);
×
2078
    }
2079
    if (TSDB_CODE_SUCCESS == code) {
×
2080
      code = vnodeDecodeCreateTbReq(&cxt);
×
2081
    }
2082
    while (TSDB_CODE_SUCCESS == code && (cxt.pRow = tGetSubmitBlkNext(&cxt.blkIter)) != NULL) {
×
2083
      code = vnodeTSRowConvertToColValArray(&cxt);
×
2084
      if (TSDB_CODE_SUCCESS == code) {
×
2085
        SRow **pNewRow = taosArrayReserve(cxt.pTbData->aRowP, 1);
×
2086

2087
        SRowBuildScanInfo sinfo = {0};
×
2088
        code = tRowBuild(cxt.pColValues, cxt.pTbSchema, pNewRow, &sinfo);
×
2089
      }
2090
    }
2091
    if (TSDB_CODE_SUCCESS == code) {
×
2092
      code = (NULL == taosArrayPush(pReq2->aSubmitTbData, cxt.pTbData) ? terrno : TSDB_CODE_SUCCESS);
×
2093
    }
2094
    if (TSDB_CODE_SUCCESS == code) {
×
2095
      taosMemoryFreeClear(cxt.pTbData);
×
2096
    }
2097
  }
2098

2099
  vnodeDestroySubmitReqConvertCxt(&cxt);
×
2100
  return code;
×
2101
}
2102

2103
static int32_t vnodeRebuildSubmitReqMsg(SSubmitReq2 *pSubmitReq, void **ppMsg) {
×
2104
  int32_t  code = TSDB_CODE_SUCCESS;
×
2105
  char    *pMsg = NULL;
×
2106
  uint32_t msglen = 0;
×
2107
  tEncodeSize(tEncodeSubmitReq, pSubmitReq, msglen, code);
×
2108
  if (TSDB_CODE_SUCCESS == code) {
×
2109
    pMsg = taosMemoryMalloc(msglen);
×
2110
    if (NULL == pMsg) {
×
2111
      code = terrno;
×
2112
    }
2113
  }
2114
  if (TSDB_CODE_SUCCESS == code) {
×
2115
    SEncoder encoder;
×
2116
    tEncoderInit(&encoder, (uint8_t *)pMsg, msglen);
×
2117
    code = tEncodeSubmitReq(&encoder, pSubmitReq);
×
2118
    tEncoderClear(&encoder);
×
2119
  }
2120
  if (TSDB_CODE_SUCCESS == code) {
×
2121
    *ppMsg = pMsg;
×
2122
  }
2123
  return code;
×
2124
}
2125

2126
static int32_t buildExistSubTalbeRsp(SVnode *pVnode, SSubmitTbData *pSubmitTbData, STableMetaRsp **ppRsp) {
90,492✔
2127
  int32_t code = 0;
90,492✔
2128
  int32_t lino = 0;
90,492✔
2129

2130
  SMetaEntry *pEntry = NULL;
90,492✔
2131
  code = metaFetchEntryByUid(pVnode->pMeta, pSubmitTbData->suid, &pEntry);
90,492✔
2132
  if (code) {
90,492✔
2133
    vError("vgId:%d, table uid:%" PRId64 " not exists, line:%d", TD_VID(pVnode), pSubmitTbData->uid, __LINE__);
×
2134
    TSDB_CHECK_CODE(code, lino, _exit);
×
2135
  }
2136
  if (pEntry->type != TSDB_SUPER_TABLE) {
90,492✔
2137
    vError("vgId:%d, table uid:%" PRId64 " exists, but is not super table, line:%d", TD_VID(pVnode), pSubmitTbData->uid,
×
2138
           __LINE__);
2139
    code = TSDB_CODE_STREAM_INSERT_SCHEMA_NOT_MATCH;
×
2140
    TSDB_CHECK_CODE(code, lino, _exit);
×
2141
  }
2142

2143
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
90,492✔
2144
  if (NULL == *ppRsp) {
90,492✔
2145
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2146
  }
2147
  (*ppRsp)->suid = pSubmitTbData->suid;
90,492✔
2148
  (*ppRsp)->tuid = pSubmitTbData->uid;
90,492✔
2149
  (*ppRsp)->sversion = pEntry->stbEntry.schemaRow.version;
90,492✔
2150
  (*ppRsp)->vgId = pVnode->config.vgId;
90,492✔
2151
  (*ppRsp)->numOfColumns = pEntry->stbEntry.schemaRow.nCols;
90,492✔
2152
  (*ppRsp)->numOfTags = pEntry->stbEntry.schemaTag.nCols;
90,492✔
2153
  (*ppRsp)->pSchemas =
90,492✔
2154
      taosMemoryCalloc(pEntry->stbEntry.schemaRow.nCols + pEntry->stbEntry.schemaTag.nCols, sizeof(SSchema));
90,492✔
2155
  if (NULL == (*ppRsp)->pSchemas) {
90,492✔
2156
    taosMemoryFree(*ppRsp);
×
2157
    *ppRsp = NULL;
×
2158
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2159
  }
2160
  memcpy((*ppRsp)->pSchemas, pEntry->stbEntry.schemaRow.pSchema, pEntry->stbEntry.schemaRow.nCols * sizeof(SSchema));
90,492✔
2161
  memcpy((*ppRsp)->pSchemas + pEntry->stbEntry.schemaRow.nCols, pEntry->stbEntry.schemaTag.pSchema,
90,492✔
2162
         pEntry->stbEntry.schemaTag.nCols * sizeof(SSchema));
90,492✔
2163
  if (pEntry->pExtSchemas != NULL) {
90,492✔
2164
    (*ppRsp)->pSchemaExt = taosMemoryCalloc(pEntry->colCmpr.nCols, sizeof(SExtSchema));
×
2165
    if (NULL == (*ppRsp)->pSchemaExt) {
×
2166
      taosMemoryFree((*ppRsp)->pSchemas);
×
2167
      taosMemoryFree(*ppRsp);
×
2168
      *ppRsp = NULL;
×
2169
      TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2170
    }
2171
    memcpy((*ppRsp)->pSchemaExt, pEntry->pExtSchemas, pEntry->colCmpr.nCols * sizeof(SExtSchema));
×
2172
  }
2173

2174
  if (pEntry->stbEntry.schemaRow.version != pSubmitTbData->sver) {
90,492✔
2175
    TSDB_CHECK_CODE(code = TSDB_CODE_TDB_TABLE_ALREADY_EXIST, lino, _exit);
810✔
2176
  }
2177
_exit:
90,492✔
2178
  metaFetchEntryFree(&pEntry);
90,492✔
2179
  if (code != TSDB_CODE_SUCCESS) {
90,492✔
2180
    vError("vgId:%d, failed to build exist sub table response, code:0x%0x, line:%d", TD_VID(pVnode), code, lino);
810✔
2181
  }
2182
  return code;
90,492✔
2183
}
2184

2185
static int32_t buildExistNormalTalbeRsp(SVnode *pVnode, int64_t uid, STableMetaRsp **ppRsp) {
31,358✔
2186
  int32_t code = 0;
31,358✔
2187
  int32_t lino = 0;
31,358✔
2188

2189
  SMetaEntry *pEntry = NULL;
31,358✔
2190
  code = metaFetchEntryByUid(pVnode->pMeta, uid, &pEntry);
31,358✔
2191
  if (code) {
31,358✔
2192
    vError("vgId:%d, table uid:%" PRId64 " not exists, line:%d", TD_VID(pVnode), uid, __LINE__);
×
2193
    TSDB_CHECK_CODE(code, lino, _exit);
×
2194
  }
2195
  if (pEntry->type != TSDB_NORMAL_TABLE) {
31,358✔
2196
    vError("vgId:%d, table uid:%" PRId64 " exists, but is not normal table, line:%d", TD_VID(pVnode), uid, __LINE__);
×
2197
    code = TSDB_CODE_STREAM_INSERT_SCHEMA_NOT_MATCH;
×
2198
    TSDB_CHECK_CODE(code, lino, _exit);
×
2199
  }
2200

2201
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
31,358✔
2202
  if (NULL == *ppRsp) {
31,358✔
2203
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2204
  }
2205

2206
  (*ppRsp)->tuid = pEntry->uid;
31,358✔
2207
  (*ppRsp)->vgId = pVnode->config.vgId;
31,358✔
2208
  (*ppRsp)->sversion = pEntry->ntbEntry.schemaRow.version;
31,358✔
2209
  (*ppRsp)->numOfColumns = pEntry->ntbEntry.schemaRow.nCols;
31,358✔
2210
  (*ppRsp)->pSchemas = taosMemoryCalloc(pEntry->ntbEntry.schemaRow.nCols, sizeof(SSchema));
31,358✔
2211
  if (NULL == (*ppRsp)->pSchemas) {
31,358✔
2212
    taosMemoryFree(*ppRsp);
×
2213
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2214
  }
2215
  memcpy((*ppRsp)->pSchemas, pEntry->ntbEntry.schemaRow.pSchema, pEntry->ntbEntry.schemaRow.nCols * sizeof(SSchema));
31,358✔
2216
  if (pEntry->pExtSchemas != NULL) {
31,358✔
2217
    (*ppRsp)->pSchemaExt = taosMemoryCalloc(pEntry->ntbEntry.schemaRow.nCols, sizeof(SSchemaExt));
×
2218
    if (NULL == (*ppRsp)->pSchemaExt) {
×
2219
      taosMemoryFree((*ppRsp)->pSchemas);
×
2220
      taosMemoryFree(*ppRsp);
×
2221
      TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2222
    }
2223
    memcpy((*ppRsp)->pSchemaExt, pEntry->pExtSchemas, pEntry->ntbEntry.schemaRow.nCols * sizeof(SSchemaExt));
×
2224
  }
2225

2226
_exit:
31,358✔
2227
  metaFetchEntryFree(&pEntry);
31,358✔
2228
  if (code != TSDB_CODE_SUCCESS) {
31,358✔
2229
    vError("vgId:%d, failed to build exist normal table response, code:%d, line:%d", TD_VID(pVnode), code, lino);
×
2230
  }
2231
  return code;
31,358✔
2232
}
2233

2234
static int32_t buildExistTableInStreamRsp(SVnode *pVnode, SSubmitTbData *pSubmitTbData, STableMetaRsp **ppRsp) {
416,234✔
2235
  if (pSubmitTbData->pCreateTbReq->flags & TD_CREATE_NORMAL_TB_IN_STREAM) {
416,234✔
2236
    int32_t code = buildExistNormalTalbeRsp(pVnode, pSubmitTbData->uid, ppRsp);
31,358✔
2237
    if (code) {
31,358✔
2238
      vError("vgId:%d, table uid:%" PRId64 " not exists, line:%d", TD_VID(pVnode), pSubmitTbData->uid, __LINE__);
×
2239
      return code;
×
2240
    }
2241
    return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
31,358✔
2242
  } else if (pSubmitTbData->pCreateTbReq->flags & TD_CREATE_SUB_TB_IN_STREAM) {
384,876✔
2243
    return buildExistSubTalbeRsp(pVnode, pSubmitTbData, ppRsp);
90,492✔
2244
  }
2245
  return TSDB_CODE_SUCCESS;
294,126✔
2246
}
2247

2248
static int32_t vnodeHandleAutoCreateTable(SVnode      *pVnode,    // vnode
538,828,760✔
2249
                                          int64_t      version,   // version
2250
                                          SSubmitReq2 *pRequest,  // request
2251
                                          SSubmitRsp2 *pResponse  // response
2252
) {
2253
  int32_t code = TSDB_CODE_SUCCESS;
538,828,760✔
2254
  int32_t numTbData = taosArrayGetSize(pRequest->aSubmitTbData);
538,828,760✔
2255
  SArray *newTbUids = NULL;
538,830,948✔
2256

2257
  for (int32_t i = 0; i < numTbData; ++i) {
1,133,239,998✔
2258
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
594,441,838✔
2259

2260
    if (pTbData->pCreateTbReq == NULL) {
594,443,455✔
2261
      continue;
581,726,727✔
2262
    }
2263

2264
    pTbData->uid = pTbData->pCreateTbReq->uid;
12,714,853✔
2265

2266
    // Alloc necessary resources
2267
    if (pResponse->aCreateTbRsp == NULL) {
12,714,853✔
2268
      pResponse->aCreateTbRsp = taosArrayInit(numTbData, sizeof(SVCreateTbRsp));
9,333,290✔
2269
      if (pResponse->aCreateTbRsp == NULL) {
9,333,290✔
2270
        code = terrno;
×
2271
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2272
               tstrerror(code), version);
2273
        taosArrayDestroy(newTbUids);
×
2274
        return code;
×
2275
      }
2276
    }
2277

2278
    // Do create table
2279
    vDebug("vgId:%d start to handle auto create table, version:%" PRId64, TD_VID(pVnode), version);
12,714,853✔
2280

2281
    SVCreateTbRsp *pCreateTbRsp = taosArrayReserve(pResponse->aCreateTbRsp, 1);
12,715,369✔
2282
    code = metaCreateTable2(pVnode->pMeta, version, pTbData->pCreateTbReq, &pCreateTbRsp->pMeta);
12,715,111✔
2283
    if (code == TSDB_CODE_SUCCESS) {
12,714,513✔
2284
      // Allocate necessary resources
2285
      if (newTbUids == NULL) {
12,008,029✔
2286
        newTbUids = taosArrayInit(numTbData, sizeof(int64_t));
8,917,077✔
2287
        if (newTbUids == NULL) {
8,917,077✔
2288
          code = terrno;
×
2289
          vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2290
                 tstrerror(code), version);
2291
          return code;
×
2292
        }
2293
      }
2294

2295
      if (taosArrayPush(newTbUids, &pTbData->uid) == NULL) {
24,016,627✔
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
        taosArrayDestroy(newTbUids);
×
2300
        return code;
×
2301
      }
2302

2303
      if (pCreateTbRsp->pMeta) {
12,008,598✔
2304
        vnodeUpdateMetaRsp(pVnode, pCreateTbRsp->pMeta);
12,008,598✔
2305
      }
2306
    } else if (code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
706,484✔
2307
      code = terrno = 0;
706,180✔
2308
      pTbData->uid = pTbData->pCreateTbReq->uid;  // update uid if table exist for using below
706,180✔
2309

2310
      // stream: get sver from meta, write to pCreateTbRsp, and need to check crateTbReq is same as meta.
2311
      if (i == 0) {
706,180✔
2312
        // In the streaming scenario, multiple grouped req requests will only operate on the same write table, and
2313
        // only the first one needs to be processed.
2314
        code = buildExistTableInStreamRsp(pVnode, pTbData, &pCreateTbRsp->pMeta);
415,976✔
2315
        if (code) {
415,976✔
2316
          vInfo("vgId:%d failed to create table in stream:%s, code(0x%0x):%s", TD_VID(pVnode),
32,168✔
2317
                pTbData->pCreateTbReq->name, code, tstrerror(code));
2318
          taosArrayDestroy(newTbUids);
32,168✔
2319
          return code;
32,168✔
2320
        }
2321
      }
2322
    } else {
2323
      code = terrno;
304✔
2324
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
304✔
2325
             tstrerror(code), version);
2326
      taosArrayDestroy(newTbUids);
304✔
2327
      return code;
304✔
2328
    }
2329
  }
2330

2331
  // Update the affected table uid list
2332
  if (taosArrayGetSize(newTbUids) > 0) {
538,798,160✔
2333
    vDebug("vgId:%d, add %d table into query table list in handling submit", TD_VID(pVnode),
8,917,019✔
2334
           (int32_t)taosArrayGetSize(newTbUids));
2335
    if (tqUpdateTbUidList(pVnode->pTq, newTbUids, true) != 0) {
8,917,048✔
2336
      vError("vgId:%d, failed to update tbUid list", TD_VID(pVnode));
×
2337
    }
2338
  }
2339

2340
  vDebug("vgId:%d, handle auto create table done, version:%" PRId64, TD_VID(pVnode), version);
538,796,543✔
2341

2342
  taosArrayDestroy(newTbUids);
538,796,291✔
2343
  return code;
538,797,140✔
2344
}
2345

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

2376
  TSDB_CHECK_CODE(code, lino, _exit);
×
2377
_exit:
×
2378
  if (code != TSDB_CODE_SUCCESS) {
×
2379
    vError("vgId:%d, failed to add exist table info into response, code:0x%0x, line:%d", TD_VID(pVnode), code, lino);
×
2380
  }
2381
  return;
×
2382
}
2383

2384
static int32_t vnodeHandleDataWrite(SVnode *pVnode, int64_t version, SSubmitReq2 *pRequest, SSubmitRsp2 *pResponse) {
538,795,694✔
2385
  int32_t code = TSDB_CODE_SUCCESS;
538,795,694✔
2386
  int32_t numTbData = taosArrayGetSize(pRequest->aSubmitTbData);
538,795,694✔
2387
  int8_t  hasBlob = 0;
538,797,078✔
2388

2389
  // Scan submit data
2390
  for (int32_t i = 0; i < numTbData; ++i) {
1,133,202,562✔
2391
    SMetaInfo      info = {0};
594,406,351✔
2392
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
594,407,726✔
2393

2394
    if (pTbData->flags & SUBMIT_REQ_WITH_BLOB) {
594,406,140✔
2395
      hasBlob = 1;
22,830✔
2396
    }
2397
    if (pTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
594,407,131✔
2398
      continue;  // skip column data format
612,202✔
2399
    }
2400
    if (pTbData->flags & SUBMIT_REQ_ONLY_CREATE_TABLE) {
593,795,268✔
2401
      continue;  // skip only crate table request
4,048,042✔
2402
    }
2403

2404
    code = metaGetInfo(pVnode->pMeta, pTbData->uid, &info, NULL);
589,746,554✔
2405
    if (code) {
589,747,656✔
2406
      code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
1,186✔
2407
      vWarn("vgId:%d, error occurred at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __FILE__,
1,186✔
2408
            __LINE__, tstrerror(code), version, pTbData->uid);
2409
      return code;
1,186✔
2410
    }
2411

2412
    if (info.suid != pTbData->suid) {
589,746,470✔
2413
      code = TSDB_CODE_INVALID_MSG;
×
2414
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64 " suid:%" PRId64
×
2415
             " info.suid:%" PRId64,
2416
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), version, pTbData->uid, pTbData->suid,
2417
             info.suid);
2418
      return code;
×
2419
    }
2420

2421
    if (info.suid) {
589,746,470✔
2422
      code = metaGetInfo(pVnode->pMeta, info.suid, &info, NULL);
438,284,373✔
2423
      if (code) {
438,286,622✔
2424
        code = TSDB_CODE_INTERNAL_ERROR;
×
2425
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " suid:%" PRId64, TD_VID(pVnode), __func__,
×
2426
               __FILE__, __LINE__, tstrerror(code), version, info.suid);
2427
        return code;
×
2428
      }
2429
    }
2430

2431
    if (pTbData->sver != info.skmVer) {
589,748,719✔
2432
      code = TSDB_CODE_TDB_INVALID_TABLE_SCHEMA_VER;
810✔
2433
      addExistTableInfoIntoRes(pVnode, pRequest, pResponse, pTbData, numTbData);
810✔
2434
      vDebug("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64
810✔
2435
             " sver:%d"
2436
             " info.skmVer:%d",
2437
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), version, pTbData->uid, pTbData->sver,
2438
             info.skmVer);
2439
      return code;
810✔
2440
    }
2441
  }
2442

2443
  // Do write data
2444
  vDebug("vgId:%d start to handle data write, version:%" PRId64, TD_VID(pVnode), version);
538,796,211✔
2445

2446
  for (int32_t i = 0; i < numTbData; ++i) {
1,133,184,879✔
2447
    int32_t        affectedRows = 0;
594,394,224✔
2448
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
594,395,594✔
2449

2450
    if (pTbData->flags & SUBMIT_REQ_ONLY_CREATE_TABLE) {
594,394,786✔
2451
      continue;
4,048,042✔
2452
    }
2453

2454
    if (hasBlob) {
590,348,172✔
2455
      code = vnodeSubmitBlobData(pVnode, pTbData);
22,830✔
2456
      if (code) {
22,830✔
2457
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2458
               __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2459
        return code;
×
2460
      }
2461
    }
2462

2463
    code = tsdbInsertTableData(pVnode->pTsdb, version, pTbData, &affectedRows);
590,348,172✔
2464
    if (code) {
590,342,898✔
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
    code = metaUpdateChangeTimeWithLock(pVnode->pMeta, pTbData->uid, pTbData->ctimeMs);
590,342,898✔
2471
    if (code) {
590,331,465✔
2472
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2473
             __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2474
      return code;
×
2475
    }
2476
    pResponse->affectedRows += affectedRows;
590,331,465✔
2477
  }
2478

2479
  vDebug("vgId:%d, handle data write done, version:%" PRId64 ", affectedRows:%d", TD_VID(pVnode), version,
538,790,655✔
2480
         pResponse->affectedRows);
2481
  return code;
538,797,019✔
2482
}
2483

2484
static int32_t vnodeScanColumnData(SVnode *pVnode, SSubmitTbData *pTbData, TSKEY minKey, TSKEY maxKey) {
612,173✔
2485
  int32_t code = 0;
612,173✔
2486

2487
  int32_t   numCols = taosArrayGetSize(pTbData->aCol);
612,173✔
2488
  SColData *aColData = (SColData *)TARRAY_DATA(pTbData->aCol);
612,028✔
2489

2490
  if (numCols <= 0) {
612,144✔
2491
    code = TSDB_CODE_INVALID_MSG;
×
2492
    vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " numCols:%d", TD_VID(pVnode), __func__,
×
2493
           __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, numCols);
2494
    return code;
×
2495
  }
2496

2497
  if (aColData[0].cid != PRIMARYKEY_TIMESTAMP_COL_ID || aColData[0].type != TSDB_DATA_TYPE_TIMESTAMP ||
612,144✔
2498
      aColData[0].nVal <= 0) {
611,629✔
2499
    code = TSDB_CODE_INVALID_MSG;
129✔
2500
    vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64
129✔
2501
           " first column is not primary key timestamp, cid:%d type:%d nVal:%d",
2502
           TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, aColData[0].cid,
2503
           aColData[0].type, aColData[0].nVal);
2504
    return code;
×
2505
  }
2506

2507
  for (int32_t i = 1; i < numCols; ++i) {
3,346,665✔
2508
    if (aColData[i].nVal != aColData[0].nVal) {
2,734,343✔
2509
      code = TSDB_CODE_INVALID_MSG;
×
2510
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64
×
2511
             " column cid:%d type:%d nVal:%d is not equal to primary key timestamp nVal:%d",
2512
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid,
2513
             aColData[i].cid, aColData[i].type, aColData[i].nVal, aColData[0].nVal);
2514
      return code;
×
2515
    }
2516
  }
2517

2518
  SRowKey *pLastKey = NULL;
612,322✔
2519
  SRowKey  lastKey = {0};
612,322✔
2520
  for (int32_t i = 0; i < aColData[0].nVal; ++i) {
743,539,245✔
2521
    SRowKey key = {0};
743,069,049✔
2522

2523
    tColDataArrGetRowKey(aColData, numCols, i, &key);
742,733,367✔
2524

2525
    if (key.ts < minKey || key.ts > maxKey) {
742,981,986✔
2526
      code = TSDB_CODE_INVALID_MSG;
×
2527
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] key:%" PRId64
×
2528
             " is out of range [%" PRId64 ", %" PRId64 "]",
2529
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, i, key.ts,
2530
             minKey, maxKey);
2531
      return code;
×
2532
    }
2533

2534
    if (pLastKey && tRowKeyCompare(pLastKey, &key) >= 0) {
743,208,457✔
2535
      code = TSDB_CODE_INVALID_MSG;
×
2536
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] key:%" PRId64
×
2537
             " is not in order, lastKey:%" PRId64,
2538
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, i, key.ts,
2539
             pLastKey->ts);
2540
      return code;
×
2541
    } else if (pLastKey == NULL) {
743,026,685✔
2542
      pLastKey = &lastKey;
612,123✔
2543
    }
2544

2545
    *pLastKey = key;
743,026,685✔
2546
  }
2547

2548
  return code;
612,202✔
2549
}
2550

2551
static int32_t vnodeScanSubmitRowData(SVnode *pVnode, SSubmitTbData *pTbData, TSKEY minKey, TSKEY maxKey) {
593,817,895✔
2552
  int32_t code = 0;
593,817,895✔
2553

2554
  int32_t numRows = taosArrayGetSize(pTbData->aRowP);
593,817,895✔
2555
  SRow  **aRow = (SRow **)TARRAY_DATA(pTbData->aRowP);
593,816,979✔
2556

2557
  if (numRows <= 0 && (pTbData->flags & SUBMIT_REQ_ONLY_CREATE_TABLE) == 0) {
593,816,241✔
2558
    code = TSDB_CODE_INVALID_MSG;
×
2559
    vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " numRows:%d", TD_VID(pVnode), __func__,
×
2560
           __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, numRows);
2561
    return code;
×
2562
  }
2563

2564
  SRowKey *pLastKey = NULL;
593,816,241✔
2565
  SRowKey  lastKey = {0};
593,816,241✔
2566
  for (int32_t i = 0; i < numRows; ++i) {
2,147,483,647✔
2567
    SRow *pRow = aRow[i];
2,147,483,647✔
2568
    if (pRow->sver != pTbData->sver) {
2,147,483,647✔
2569
      code = TSDB_CODE_INVALID_MSG;
×
2570
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] sver:%d pTbData->sver:%d",
×
2571
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, i, pRow->sver,
2572
             pTbData->sver);
2573
      return code;
×
2574
    }
2575

2576
    SRowKey key = {0};
2,147,483,647✔
2577
    tRowGetKey(pRow, &key);
2,147,483,647✔
2578
    if (key.ts < minKey || key.ts > maxKey) {
2,147,483,647✔
2579
      code = TSDB_CODE_INVALID_MSG;
6,870,265✔
2580
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] key:%" PRId64
6,870,265✔
2581
             " is out of range [%" PRId64 ", %" PRId64 "]",
2582
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, i, key.ts,
2583
             minKey, maxKey);
2584
      return code;
×
2585
    }
2586

2587
    if (pLastKey && tRowKeyCompare(pLastKey, &key) >= 0) {
2,147,483,647✔
2588
      code = TSDB_CODE_INVALID_MSG;
×
2589
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] key:%" PRId64
×
2590
             " is not in order, lastKey:%" PRId64,
2591
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, i, key.ts,
2592
             pLastKey->ts);
2593
      return code;
×
2594
    } else if (pLastKey == NULL) {
2,147,483,647✔
2595
      pLastKey = &lastKey;
589,768,079✔
2596
    }
2597

2598
    *pLastKey = key;
2,147,483,647✔
2599
  }
2600

2601
  return code;
574,280,049✔
2602
}
2603

2604
static int32_t vnodeScanSubmitReq(SVnode *pVnode, int64_t version, SSubmitReq2 *pRequest, SSubmitRsp2 *pResponse) {
538,821,888✔
2605
  int32_t code = TSDB_CODE_SUCCESS;
538,821,888✔
2606
  int32_t numTbData = taosArrayGetSize(pRequest->aSubmitTbData);
538,821,888✔
2607

2608
  TSKEY now = taosGetTimestamp(pVnode->config.tsdbCfg.precision);
538,822,778✔
2609
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * pVnode->config.tsdbCfg.keep2;
538,827,051✔
2610
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
538,819,902✔
2611
  for (int32_t i = 0; i < numTbData; i++) {
1,133,251,504✔
2612
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
594,430,877✔
2613

2614
    if (pTbData->pCreateTbReq && pTbData->pCreateTbReq->uid == 0) {
594,431,841✔
2615
      code = TSDB_CODE_INVALID_MSG;
×
2616
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2617
             tstrerror(code), version);
2618
      return code;
×
2619
    }
2620

2621
    if (pTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
594,431,925✔
2622
      code = vnodeScanColumnData(pVnode, pTbData, minKey, maxKey);
611,729✔
2623
      if (code) {
612,202✔
2624
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2625
               __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2626
        return code;
×
2627
      }
2628
    } else {
2629
      code = vnodeScanSubmitRowData(pVnode, pTbData, minKey, maxKey);
593,816,305✔
2630
      if (code) {
593,814,678✔
2631
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2632
               __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2633
        return code;
×
2634
      }
2635
    }
2636
  }
2637

2638
  return code;
538,820,627✔
2639
}
2640

2641
static int32_t vnodeProcessSubmitReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
538,827,815✔
2642
                                     SRpcMsg *pOriginalMsg) {
2643
  int32_t code = 0;
538,827,815✔
2644
  int32_t lino = 0;
538,827,815✔
2645
  terrno = 0;
538,827,815✔
2646

2647
  SSubmitReq2 *pSubmitReq = &(SSubmitReq2){0};
538,831,148✔
2648
  SSubmitRsp2 *pSubmitRsp = &(SSubmitRsp2){0};
538,831,487✔
2649
  int32_t      ret;
2650
  SEncoder     ec = {0};
538,828,844✔
2651

2652
  pRsp->code = TSDB_CODE_SUCCESS;
538,829,951✔
2653

2654
  void           *pAllocMsg = NULL;
538,827,059✔
2655
  SSubmitReq2Msg *pMsg = (SSubmitReq2Msg *)pReq;
538,827,059✔
2656
  SDecoder        dc = {0};
538,827,059✔
2657
  if (0 == taosHton64(pMsg->version)) {
538,827,333✔
2658
    code = vnodeSubmitReqConvertToSubmitReq2(pVnode, (SSubmitReq *)pMsg, pSubmitReq);
×
2659
    if (TSDB_CODE_SUCCESS == code) {
×
2660
      code = vnodeRebuildSubmitReqMsg(pSubmitReq, &pReq);
×
2661
    }
2662
    if (TSDB_CODE_SUCCESS == code) {
×
2663
      pAllocMsg = pReq;
×
2664
    }
2665
    TSDB_CHECK_CODE(code, lino, _exit);
×
2666
  } else {
2667
    // decode
2668
    pReq = POINTER_SHIFT(pReq, sizeof(SSubmitReq2Msg));
538,827,847✔
2669
    len -= sizeof(SSubmitReq2Msg);
538,828,505✔
2670

2671
    tDecoderInit(&dc, pReq, len);
538,828,505✔
2672
    if (tDecodeSubmitReq(&dc, pSubmitReq, NULL) < 0) {
538,827,142✔
2673
      code = TSDB_CODE_INVALID_MSG;
×
2674
      TSDB_CHECK_CODE(code, lino, _exit);
×
2675
    }
2676
  }
2677

2678
  // Scan the request
2679
  code = vnodeScanSubmitReq(pVnode, ver, pSubmitReq, pSubmitRsp);
538,825,226✔
2680
  if (code) {
538,821,741✔
2681
    vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2682
           tstrerror(code), ver);
2683
    TSDB_CHECK_CODE(code, lino, _exit);
×
2684
  }
2685

2686
  vGDebug(pOriginalMsg ? &pOriginalMsg->info.traceId : NULL, "vgId:%d, index:%" PRId64 ", submit block, rows:%d",
538,821,741✔
2687
          TD_VID(pVnode), ver, (int32_t)taosArrayGetSize(pSubmitReq->aSubmitTbData));
2688

2689
  // Handle auto create table
2690
  code = vnodeHandleAutoCreateTable(pVnode, ver, pSubmitReq, pSubmitRsp);
538,829,715✔
2691
  if (code) {
538,828,934✔
2692
    vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
32,472✔
2693
           tstrerror(code), ver);
2694
    TSDB_CHECK_CODE(code, lino, _exit);
32,472✔
2695
  }
2696

2697
  // Handle data write
2698
  code = vnodeHandleDataWrite(pVnode, ver, pSubmitReq, pSubmitRsp);
538,796,462✔
2699
  if (code) {
538,797,318✔
2700
    vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
1,996✔
2701
           tstrerror(code), ver);
2702
    TSDB_CHECK_CODE(code, lino, _exit);
1,996✔
2703
  }
2704

2705
_exit:
538,829,544✔
2706
  // message
2707
  pRsp->code = code;
538,831,487✔
2708
  tEncodeSize(tEncodeSSubmitRsp2, pSubmitRsp, pRsp->contLen, ret);
538,829,809✔
2709
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
538,829,583✔
2710
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
538,830,067✔
2711
  if (tEncodeSSubmitRsp2(&ec, pSubmitRsp) < 0) {
538,829,922✔
2712
    vError("vgId:%d, failed to encode submit response", TD_VID(pVnode));
×
2713
  }
2714
  tEncoderClear(&ec);
538,830,877✔
2715

2716
  // update statistics
2717
  (void)atomic_add_fetch_64(&pVnode->statis.nInsert, pSubmitRsp->affectedRows);
538,829,922✔
2718
  (void)atomic_add_fetch_64(&pVnode->statis.nInsertSuccess, pSubmitRsp->affectedRows);
538,830,395✔
2719
  (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsert, 1);
538,830,877✔
2720

2721
  // update metrics
2722
  METRICS_UPDATE(pVnode->writeMetrics.total_requests, METRIC_LEVEL_LOW, 1);
538,829,983✔
2723
  METRICS_UPDATE(pVnode->writeMetrics.total_rows, METRIC_LEVEL_HIGH, pSubmitRsp->affectedRows);
538,828,560✔
2724
  METRICS_UPDATE(pVnode->writeMetrics.total_bytes, METRIC_LEVEL_LOW, pMsg->header.contLen);
538,828,047✔
2725

2726
  if (tsEnableMonitor && tsMonitorFqdn[0] != 0 && tsMonitorPort != 0 && pSubmitRsp->affectedRows > 0 &&
538,828,302✔
2727
      strlen(pOriginalMsg->info.conn.user) > 0 && tsInsertCounter != NULL) {
×
2728
    const char *sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT_AFFECTED_ROWS,
×
2729
                                   pVnode->monitor.strClusterId,
×
2730
                                   pVnode->monitor.strDnodeId,
×
2731
                                   tsLocalEp,
2732
                                   pVnode->monitor.strVgId,
×
2733
                                   pOriginalMsg->info.conn.user,
×
2734
                                   "Success"};
2735
    int         tv = taos_counter_add(tsInsertCounter, pSubmitRsp->affectedRows, sample_labels);
×
2736
  }
2737

2738
  if (code == 0) {
538,825,229✔
2739
    (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
538,790,761✔
2740
  }
2741

2742
  // clear
2743
  tDestroySubmitReq(pSubmitReq, 0 == taosHton64(pMsg->version) ? TSDB_MSG_FLG_CMPT : TSDB_MSG_FLG_DECODE);
538,828,946✔
2744
  tDestroySSubmitRsp2(pSubmitRsp, TSDB_MSG_FLG_ENCODE);
538,824,807✔
2745

2746
  if (code) {
538,818,787✔
2747
    terrno = code;
34,468✔
2748
    if (code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
34,468✔
2749
      vInfo("vgId:%d, failed to process submit request since %s, lino:%d, version:%" PRId64, TD_VID(pVnode),
32,168✔
2750
            tstrerror(code), lino, ver);
2751

2752
    } else {
2753
      vError("vgId:%d, failed to process submit request since %s, lino:%d, version:%" PRId64, TD_VID(pVnode),
2,300✔
2754
             tstrerror(code), lino, ver);
2755
    }
2756
  }
2757

2758
  taosMemoryFree(pAllocMsg);
538,824,510✔
2759
  tDecoderClear(&dc);
538,825,164✔
2760

2761
  return code;
538,829,523✔
2762
}
2763

2764
static int32_t vnodeConsolidateAlterHashRange(SVnode *pVnode, int64_t ver) {
20,415✔
2765
  int32_t code = TSDB_CODE_SUCCESS;
20,415✔
2766

2767
  vInfo("vgId:%d, trim meta of tables per hash range [%" PRIu32 ", %" PRIu32 "]. apply-index:%" PRId64, TD_VID(pVnode),
20,415✔
2768
        pVnode->config.hashBegin, pVnode->config.hashEnd, ver);
2769

2770
  // TODO: trim meta of tables from TDB per hash range [pVnode->config.hashBegin, pVnode->config.hashEnd]
2771
  code = metaTrimTables(pVnode->pMeta, ver);
20,415✔
2772

2773
  return code;
20,415✔
2774
}
2775

2776
static int32_t vnodeProcessAlterConfirmReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
3,856,708✔
2777
  vInfo("vgId:%d, vnode handle msgType:alter-confirm, alter confirm msg is processed", TD_VID(pVnode));
3,856,708✔
2778
  int32_t code = TSDB_CODE_SUCCESS;
3,856,708✔
2779
  if (!pVnode->config.hashChange) {
3,856,708✔
2780
    goto _exit;
3,836,293✔
2781
  }
2782

2783
  code = vnodeConsolidateAlterHashRange(pVnode, ver);
20,415✔
2784
  if (code < 0) {
20,415✔
2785
    vError("vgId:%d, failed to consolidate alter hashrange since %s. version:%" PRId64, TD_VID(pVnode), terrstr(), ver);
×
2786
    goto _exit;
×
2787
  }
2788
  pVnode->config.hashChange = false;
20,415✔
2789

2790
_exit:
3,856,708✔
2791
  pRsp->msgType = TDMT_VND_ALTER_CONFIRM_RSP;
3,856,708✔
2792
  pRsp->code = code;
3,856,708✔
2793
  pRsp->pCont = NULL;
3,856,708✔
2794
  pRsp->contLen = 0;
3,856,708✔
2795

2796
  return code;
3,856,708✔
2797
}
2798

2799
extern int32_t tsdbDisableAndCancelAllBgTask(STsdb *pTsdb);
2800
extern void    tsdbEnableBgTask(STsdb *pTsdb);
2801

2802
static int32_t vnodeProcessAlterConfigReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
287,011✔
2803
  bool walChanged = false;
287,011✔
2804
  bool tsdbChanged = false;
287,011✔
2805

2806
  SAlterVnodeConfigReq req = {0};
287,011✔
2807
  if (tDeserializeSAlterVnodeConfigReq(pReq, len, &req) != 0) {
287,011✔
2808
    terrno = TSDB_CODE_INVALID_MSG;
×
2809
    return TSDB_CODE_INVALID_MSG;
×
2810
  }
2811

2812
  vInfo("vgId:%d, start to alter vnode config, page:%d pageSize:%d buffer:%d szPage:%d szBuf:%" PRIu64
287,011✔
2813
        " cacheLast:%d cacheLastSize:%d days:%d keep0:%d keep1:%d keep2:%d keepTimeOffset:%d ssKeepLocal:%d "
2814
        "ssCompact:%d fsync:%d level:%d "
2815
        "walRetentionPeriod:%d walRetentionSize:%d",
2816
        TD_VID(pVnode), req.pages, req.pageSize, req.buffer, req.pageSize * 1024, (uint64_t)req.buffer * 1024 * 1024,
2817
        req.cacheLast, req.cacheLastSize, req.daysPerFile, req.daysToKeep0, req.daysToKeep1, req.daysToKeep2,
2818
        req.keepTimeOffset, req.ssKeepLocal, req.ssCompact, req.walFsyncPeriod, req.walLevel, req.walRetentionPeriod,
2819
        req.walRetentionSize);
2820

2821
  if (pVnode->config.cacheLastSize != req.cacheLastSize) {
287,011✔
2822
    pVnode->config.cacheLastSize = req.cacheLastSize;
3,780✔
2823
    tsdbCacheSetCapacity(pVnode, (size_t)pVnode->config.cacheLastSize * 1024 * 1024);
3,780✔
2824
  }
2825

2826
  if (pVnode->config.szBuf != req.buffer * 1024LL * 1024LL) {
287,011✔
2827
    vInfo("vgId:%d, vnode buffer is changed from %" PRId64 " to %" PRId64, TD_VID(pVnode), pVnode->config.szBuf,
9,462✔
2828
          (uint64_t)(req.buffer * 1024LL * 1024LL));
2829
    pVnode->config.szBuf = req.buffer * 1024LL * 1024LL;
9,462✔
2830
  }
2831

2832
  if (pVnode->config.szCache != req.pages) {
287,011✔
2833
    if ((terrno = metaAlterCache(pVnode->pMeta, req.pages)) < 0) {
4,776✔
2834
      vError("vgId:%d, failed to change vnode pages from %d to %d failed since %s", TD_VID(pVnode),
×
2835
             pVnode->config.szCache, req.pages, tstrerror(terrno));
2836
      return terrno;
×
2837
    } else {
2838
      vInfo("vgId:%d, vnode pages is changed from %d to %d", TD_VID(pVnode), pVnode->config.szCache, req.pages);
4,776✔
2839
      pVnode->config.szCache = req.pages;
4,776✔
2840
    }
2841
  }
2842

2843
  if (pVnode->config.cacheLast != req.cacheLast) {
287,011✔
2844
    pVnode->config.cacheLast = req.cacheLast;
81,847✔
2845
  }
2846

2847
  if (pVnode->config.walCfg.fsyncPeriod != req.walFsyncPeriod) {
287,011✔
2848
    pVnode->config.walCfg.fsyncPeriod = req.walFsyncPeriod;
54,328✔
2849
    walChanged = true;
54,328✔
2850
  }
2851

2852
  if (pVnode->config.walCfg.level != req.walLevel) {
287,011✔
2853
    if (pVnode->config.walCfg.level == 0) {
40,009✔
2854
      pVnode->config.walCfg.clearFiles = 1;
2,267✔
2855
    }
2856
    pVnode->config.walCfg.level = req.walLevel;
40,009✔
2857
    walChanged = true;
40,009✔
2858
  }
2859

2860
  if (pVnode->config.walCfg.retentionPeriod != req.walRetentionPeriod) {
287,011✔
2861
    pVnode->config.walCfg.retentionPeriod = req.walRetentionPeriod;
15,389✔
2862
    walChanged = true;
15,389✔
2863
  }
2864

2865
  if (pVnode->config.walCfg.retentionSize != req.walRetentionSize) {
287,011✔
2866
    pVnode->config.walCfg.retentionSize = req.walRetentionSize;
1,502✔
2867
    walChanged = true;
1,502✔
2868
  }
2869

2870
  if (pVnode->config.tsdbCfg.keep0 != req.daysToKeep0) {
287,011✔
2871
    pVnode->config.tsdbCfg.keep0 = req.daysToKeep0;
42,336✔
2872
    tsdbChanged = true;
42,336✔
2873
  }
2874

2875
  if (pVnode->config.tsdbCfg.keep1 != req.daysToKeep1) {
287,011✔
2876
    pVnode->config.tsdbCfg.keep1 = req.daysToKeep1;
40,790✔
2877
    tsdbChanged = true;
40,790✔
2878
  }
2879

2880
  if (pVnode->config.tsdbCfg.keep2 != req.daysToKeep2) {
287,011✔
2881
    pVnode->config.tsdbCfg.keep2 = req.daysToKeep2;
35,920✔
2882
    tsdbChanged = true;
35,920✔
2883
  }
2884

2885
  if (pVnode->config.tsdbCfg.keepTimeOffset != req.keepTimeOffset) {
287,011✔
2886
    pVnode->config.tsdbCfg.keepTimeOffset = req.keepTimeOffset;
7,664✔
2887
    tsdbChanged = true;
7,664✔
2888
  }
2889

2890
  if (req.sttTrigger != -1 && req.sttTrigger != pVnode->config.sttTrigger) {
287,011✔
2891
    if (req.sttTrigger > 1 && pVnode->config.sttTrigger > 1) {
2,680✔
2892
      pVnode->config.sttTrigger = req.sttTrigger;
1,502✔
2893
    } else {
2894
      vnodeAWait(&pVnode->commitTask);
1,178✔
2895

2896
      int32_t ret = tsdbDisableAndCancelAllBgTask(pVnode->pTsdb);
1,178✔
2897
      if (ret != 0) {
1,178✔
2898
        vError("vgId:%d, failed to disable bg task since %s", TD_VID(pVnode), tstrerror(ERRNO));
×
2899
      }
2900

2901
      pVnode->config.sttTrigger = req.sttTrigger;
1,178✔
2902
      tsdbEnableBgTask(pVnode->pTsdb);
1,178✔
2903
    }
2904
  }
2905

2906
  if (req.minRows != -1 && req.minRows != pVnode->config.tsdbCfg.minRows) {
287,011✔
2907
    pVnode->config.tsdbCfg.minRows = req.minRows;
1,502✔
2908
  }
2909

2910
  if (req.ssKeepLocal != -1 && req.ssKeepLocal != pVnode->config.ssKeepLocal) {
287,011✔
2911
    pVnode->config.ssKeepLocal = req.ssKeepLocal;
1,502✔
2912
  }
2913
  if (req.ssCompact != -1 && req.ssCompact != pVnode->config.ssCompact) {
287,011✔
2914
    pVnode->config.ssCompact = req.ssCompact;
1,502✔
2915
  }
2916

2917
  if (walChanged) {
287,011✔
2918
    if (walAlter(pVnode->pWal, &pVnode->config.walCfg) != 0) {
108,160✔
2919
      vError("vgId:%d, failed to alter wal config since %s", TD_VID(pVnode), tstrerror(ERRNO));
×
2920
    }
2921
  }
2922

2923
  if (tsdbChanged) {
287,011✔
2924
    tsdbSetKeepCfg(pVnode->pTsdb, &pVnode->config.tsdbCfg);
57,714✔
2925
  }
2926

2927
  return 0;
287,011✔
2928
}
2929

2930
static int32_t vnodeProcessBatchDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
2931
  SBatchDeleteReq deleteReq;
×
2932
  SDecoder        decoder;
×
2933
  tDecoderInit(&decoder, pReq, len);
×
2934
  if (tDecodeSBatchDeleteReq(&decoder, &deleteReq) < 0) {
×
2935
    tDecoderClear(&decoder);
×
2936
    return terrno = TSDB_CODE_INVALID_MSG;
×
2937
  }
2938

2939
  SMetaReader mr = {0};
×
2940
  metaReaderDoInit(&mr, pVnode->pMeta, META_READER_NOLOCK);
×
2941
  STsdb *pTsdb = pVnode->pTsdb;
×
2942

2943
  int32_t sz = taosArrayGetSize(deleteReq.deleteReqs);
×
2944
  for (int32_t i = 0; i < sz; i++) {
×
2945
    SSingleDeleteReq *pOneReq = taosArrayGet(deleteReq.deleteReqs, i);
×
2946
    char             *name = pOneReq->tbname;
×
2947
    if (metaGetTableEntryByName(&mr, name) < 0) {
×
2948
      vDebug("vgId:%d, stream delete msg, skip since no table: %s", pVnode->config.vgId, name);
×
2949
      continue;
×
2950
    }
2951

2952
    int64_t uid = mr.me.uid;
×
2953

2954
    int32_t code = tsdbDeleteTableData(pTsdb, ver, deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
×
2955
    if (code < 0) {
×
2956
      terrno = code;
×
2957
      vError("vgId:%d, delete error since %s, suid:%" PRId64 ", uid:%" PRId64 ", start ts:%" PRId64 ", end ts:%" PRId64,
×
2958
             TD_VID(pVnode), terrstr(), deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
2959
    }
2960

2961
    if (deleteReq.level == 0) {
×
2962
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, deleteReq.ctimeMs);
×
2963
      if (code < 0) {
×
2964
        terrno = code;
×
2965
        vError("vgId:%d, update change time error since %s, suid:%" PRId64 ", uid:%" PRId64 ", start ts:%" PRId64
×
2966
               ", end ts:%" PRId64,
2967
               TD_VID(pVnode), terrstr(), deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
2968
      }
2969
    }
2970
    tDecoderClear(&mr.coder);
×
2971
  }
2972
  metaReaderClear(&mr);
×
2973
  taosArrayDestroy(deleteReq.deleteReqs);
×
2974
  return 0;
×
2975
}
2976

2977
static int32_t vnodeProcessDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
1,772,980✔
2978
                                     SRpcMsg *pOriginalMsg) {
2979
  int32_t     code = 0;
1,772,980✔
2980
  SDecoder   *pCoder = &(SDecoder){0};
1,772,980✔
2981
  SDeleteRes *pRes = &(SDeleteRes){0};
1,772,980✔
2982

2983
  pRsp->msgType = TDMT_VND_DELETE_RSP;
1,772,980✔
2984
  pRsp->pCont = NULL;
1,772,980✔
2985
  pRsp->contLen = 0;
1,772,980✔
2986
  pRsp->code = TSDB_CODE_SUCCESS;
1,772,980✔
2987

2988
  pRes->uidList = taosArrayInit(0, sizeof(tb_uid_t));
1,772,980✔
2989
  if (pRes->uidList == NULL) {
1,772,980✔
2990
    code = terrno;
×
2991
    goto _err;
×
2992
  }
2993

2994
  tDecoderInit(pCoder, pReq, len);
1,772,980✔
2995
  code = tDecodeDeleteRes(pCoder, pRes);
1,772,980✔
2996
  if (code) goto _err;
1,772,980✔
2997

2998
  if (pRes->affectedRows > 0) {
1,772,980✔
2999
    for (int32_t iUid = 0; iUid < taosArrayGetSize(pRes->uidList); iUid++) {
3,050,585✔
3000
      uint64_t uid = *(uint64_t *)taosArrayGet(pRes->uidList, iUid);
1,811,481✔
3001
      code = tsdbDeleteTableData(pVnode->pTsdb, ver, pRes->suid, uid, pRes->skey, pRes->ekey);
1,811,633✔
3002
      if (code) goto _err;
1,811,633✔
3003
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, pRes->ctimeMs);
1,811,633✔
3004
      if (code) goto _err;
1,811,633✔
3005
    }
3006
  }
3007

3008
  tDecoderClear(pCoder);
1,772,980✔
3009
  taosArrayDestroy(pRes->uidList);
1,772,980✔
3010

3011
  SVDeleteRsp rsp = {.affectedRows = pRes->affectedRows};
1,772,828✔
3012
  int32_t     ret = 0;
1,772,980✔
3013
  tEncodeSize(tEncodeSVDeleteRsp, &rsp, pRsp->contLen, ret);
1,772,980✔
3014
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
1,772,828✔
3015
  SEncoder ec = {0};
1,772,828✔
3016
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
1,772,828✔
3017
  code = tEncodeSVDeleteRsp(&ec, &rsp);
1,772,980✔
3018
  if (code) goto _err;
1,772,980✔
3019
  tEncoderClear(&ec);
1,772,980✔
3020
  return code;
1,772,980✔
3021

3022
_err:
×
3023
  /*
3024
  if(code == TSDB_CODE_SUCCESS){
3025
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_DELETE, pVnode->monitor.strClusterId,
3026
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
3027
                                        pOriginalMsg->info.conn.user, "Success"};
3028
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
3029
  }
3030
  else{
3031
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_DELETE, pVnode->monitor.strClusterId,
3032
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
3033
                                        pOriginalMsg->info.conn.user, "Failed"};
3034
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
3035
  }
3036
  */
3037

3038
  return code;
×
3039
}
3040
static int32_t vnodeProcessCreateIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
5,949✔
3041
  SVCreateStbReq req = {0};
5,949✔
3042
  SDecoder       dc = {0};
5,949✔
3043
  int32_t        code = 0;
5,949✔
3044

3045
  pRsp->msgType = TDMT_VND_CREATE_INDEX_RSP;
5,949✔
3046
  pRsp->code = TSDB_CODE_SUCCESS;
5,949✔
3047
  pRsp->pCont = NULL;
5,949✔
3048
  pRsp->contLen = 0;
5,949✔
3049

3050
  tDecoderInit(&dc, pReq, len);
5,949✔
3051
  // decode req
3052
  if (tDecodeSVCreateStbReq(&dc, &req) < 0) {
5,949✔
3053
    tDecoderClear(&dc);
×
3054
    return terrno = TSDB_CODE_INVALID_MSG;
×
3055
  }
3056

3057
  code = metaAddIndexToSuperTable(pVnode->pMeta, ver, &req);
5,949✔
3058
  if (code) {
5,949✔
3059
    pRsp->code = code;
×
3060
    goto _err;
×
3061
  }
3062
  tDecoderClear(&dc);
5,949✔
3063
  return 0;
5,949✔
3064

3065
_err:
×
3066
  tDecoderClear(&dc);
×
3067
  return code;
×
3068
}
3069
static int32_t vnodeProcessDropIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
3,962✔
3070
  SDropIndexReq req = {0};
3,962✔
3071
  int32_t       code = 0;
3,962✔
3072
  pRsp->msgType = TDMT_VND_DROP_INDEX_RSP;
3,962✔
3073
  pRsp->code = TSDB_CODE_SUCCESS;
3,962✔
3074
  pRsp->pCont = NULL;
3,962✔
3075
  pRsp->contLen = 0;
3,962✔
3076

3077
  if ((code = tDeserializeSDropIdxReq(pReq, len, &req))) {
3,962✔
3078
    pRsp->code = code;
×
3079
    return code;
×
3080
  }
3081

3082
  code = metaDropIndexFromSuperTable(pVnode->pMeta, ver, &req);
3,962✔
3083
  if (code) {
3,962✔
3084
    pRsp->code = code;
×
3085
    return code;
×
3086
  }
3087
  return TSDB_CODE_SUCCESS;
3,962✔
3088
}
3089

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

3092
static int32_t vnodeProcessCompactVnodeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
88,837✔
3093
  if (!pVnode->restored) {
88,837✔
3094
    vInfo("vgId:%d, ignore compact req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
17,179✔
3095
    return 0;
17,179✔
3096
  }
3097
  return vnodeAsyncCompact(pVnode, ver, pReq, len, pRsp);
71,658✔
3098
}
3099

3100
static int32_t vnodeProcessConfigChangeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
3101
  if (syncCheckMember(pVnode->sync) != 0) {
×
3102
    vError("vgId:%d, failed to check member", TD_VID(pVnode));
×
3103
  }
3104

3105
  pRsp->msgType = TDMT_SYNC_CONFIG_CHANGE_RSP;
×
3106
  pRsp->code = TSDB_CODE_SUCCESS;
×
3107
  pRsp->pCont = NULL;
×
3108
  pRsp->contLen = 0;
×
3109

3110
  return 0;
×
3111
}
3112

3113
static int32_t vnodeCheckState(SVnode *pVnode) {
52,778✔
3114
  SSyncState syncState = syncGetState(pVnode->sync);
52,778✔
3115
  if (syncState.state != TAOS_SYNC_STATE_LEADER) {
52,778✔
3116
    return terrno = TSDB_CODE_SYN_NOT_LEADER;
21,341✔
3117
  }
3118
  return 0;
31,437✔
3119
}
3120

3121
static int32_t vnodeCheckToken(SVnode *pVnode, char *member0Token, char *member1Token) {
31,437✔
3122
  char token[TSDB_ARB_TOKEN_SIZE] = {0};
31,437✔
3123
  if (vnodeGetArbToken(pVnode, token) != 0) {
31,437✔
3124
    return terrno = TSDB_CODE_NOT_FOUND;
×
3125
  }
3126

3127
  if (strncmp(token, member0Token, TSDB_ARB_TOKEN_SIZE) != 0 &&
31,437✔
3128
      strncmp(token, member1Token, TSDB_ARB_TOKEN_SIZE) != 0) {
30,535✔
3129
    return terrno = TSDB_CODE_MND_ARB_TOKEN_MISMATCH;
30,441✔
3130
  }
3131

3132
  terrno = TSDB_CODE_SUCCESS;
996✔
3133
  return 0;
996✔
3134
}
3135

3136
static int32_t vnodeCheckSyncd(SVnode *pVnode, char *member0Token, char *member1Token) {
498✔
3137
  int32_t code = vnodeCheckToken(pVnode, member0Token, member1Token);
498✔
3138
  if (code != 0) {
498✔
3139
    return code;
×
3140
  }
3141

3142
  return syncCheckSynced(pVnode->sync);
498✔
3143
}
3144

3145
static int32_t vnodeProcessArbCheckSyncReq(SVnode *pVnode, void *pReq, int32_t len, SRpcMsg *pRsp) {
992✔
3146
  int32_t code = 0;
992✔
3147

3148
  if ((code = vnodeCheckState(pVnode)) != 0) {
992✔
3149
    vDebug("vgId:%d, failed to preprocess vnode-arb-check-sync request since %s", TD_VID(pVnode), tstrerror(code));
494✔
3150
    return 0;
494✔
3151
  }
3152

3153
  SVArbCheckSyncReq syncReq = {0};
498✔
3154

3155
  code = tDeserializeSVArbCheckSyncReq(pReq, len, &syncReq);
498✔
3156
  if (code) {
498✔
3157
    return code;
×
3158
  }
3159

3160
  vInfo("vgId:%d, start to process vnode-arb-check-sync req QID:0x%" PRIx64 ":0x%" PRIx64 ", seqNum:%" PRIx64
498✔
3161
        ", arbToken:%s, member0Token:%s, member1Token:%s, "
3162
        "arbTerm:%" PRId64,
3163
        TD_VID(pVnode), pRsp->info.traceId.rootId, pRsp->info.traceId.msgId, pRsp->info.seqNum, syncReq.arbToken,
3164
        syncReq.member0Token, syncReq.member1Token, syncReq.arbTerm);
3165

3166
  pRsp->msgType = TDMT_VND_ARB_CHECK_SYNC_RSP;
498✔
3167
  pRsp->code = TSDB_CODE_SUCCESS;
498✔
3168
  pRsp->pCont = NULL;
498✔
3169
  pRsp->contLen = 0;
498✔
3170

3171
  SVArbCheckSyncRsp syncRsp = {0};
498✔
3172
  syncRsp.arbToken = syncReq.arbToken;
498✔
3173
  syncRsp.member0Token = syncReq.member0Token;
498✔
3174
  syncRsp.member1Token = syncReq.member1Token;
498✔
3175
  syncRsp.vgId = TD_VID(pVnode);
498✔
3176

3177
  if ((syncRsp.errCode = vnodeCheckSyncd(pVnode, syncReq.member0Token, syncReq.member1Token)) != 0) {
498✔
3178
    vError("vgId:%d, failed to check assigned log syncd since %s", TD_VID(pVnode), tstrerror(syncRsp.errCode));
×
3179
  }
3180

3181
  if ((code = vnodeUpdateArbTerm(pVnode, syncReq.arbTerm)) != 0) {
498✔
3182
    vError("vgId:%d, failed to update arb term since %s", TD_VID(pVnode), tstrerror(code));
×
3183
    goto _OVER;
×
3184
  }
3185

3186
  int32_t contLen = tSerializeSVArbCheckSyncRsp(NULL, 0, &syncRsp);
498✔
3187
  if (contLen <= 0) {
498✔
3188
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
3189
    code = -1;
×
3190
    goto _OVER;
×
3191
  }
3192
  void *pHead = rpcMallocCont(contLen);
498✔
3193
  if (!pHead) {
498✔
3194
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
3195
    code = -1;
×
3196
    goto _OVER;
×
3197
  }
3198

3199
  if (tSerializeSVArbCheckSyncRsp(pHead, contLen, &syncRsp) <= 0) {
498✔
3200
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
3201
    rpcFreeCont(pHead);
×
3202
    code = -1;
×
3203
    goto _OVER;
×
3204
  }
3205

3206
  pRsp->pCont = pHead;
498✔
3207
  pRsp->contLen = contLen;
498✔
3208

3209
  vInfo(
498✔
3210
      "vgId:%d, suceed to process vnode-arb-check-sync req rsp.code:%s, arbToken:%s, member0Token:%s, "
3211
      "member1Token:%s",
3212
      TD_VID(pVnode), tstrerror(syncRsp.errCode), syncRsp.arbToken, syncRsp.member0Token, syncRsp.member1Token);
3213

3214
  code = TSDB_CODE_SUCCESS;
498✔
3215

3216
_OVER:
498✔
3217
  if (code != 0) {
498✔
3218
    vError("vgId:%d, failed to process arb check req rsp.code:%s since %s", TD_VID(pVnode), tstrerror(syncRsp.errCode),
×
3219
           tstrerror(code));
3220
  }
3221
  tFreeSVArbCheckSyncReq(&syncReq);
498✔
3222
  return code;
498✔
3223
}
3224

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