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

taosdata / TDengine / #4794

15 Oct 2025 11:06AM UTC coverage: 60.905% (-0.001%) from 60.906%
#4794

push

travis-ci

web-flow
Merge c54016657 into 7e74ade39

154617 of 324341 branches covered (47.67%)

Branch coverage included in aggregate %.

54 of 65 new or added lines in 13 files covered. (83.08%)

2506 existing lines in 114 files now uncovered.

207077 of 269525 relevant lines covered (76.83%)

125143530.79 hits per line

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

55.38
/source/dnode/vnode/src/vnd/vnodeSvr.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

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

32
extern taos_counter_t *tsInsertCounter;
33

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

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

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

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

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

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

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

84
static int32_t vnodePreprocessCreateTableReq(SVnode *pVnode, SDecoder *pCoder, int64_t btime, int64_t *pUid) {
56,177,101✔
85
  int32_t code = 0;
56,177,101✔
86
  int32_t lino = 0;
56,177,101✔
87

88
  if (tStartDecode(pCoder) < 0) {
56,177,101!
89
    code = TSDB_CODE_INVALID_MSG;
×
90
    TSDB_CHECK_CODE(code, lino, _exit);
×
91
  }
92

93
  // flags
94
  if (tDecodeI32v(pCoder, NULL) < 0) {
56,177,749!
95
    code = TSDB_CODE_INVALID_MSG;
×
96
    TSDB_CHECK_CODE(code, lino, _exit);
×
97
  }
98

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

106
  // uid
107
  int64_t uid = metaGetTableEntryUidByName(pVnode->pMeta, name);
56,176,551✔
108
  if (uid == 0) {
56,174,375✔
109
    uid = tGenIdPI64();
54,850,150✔
110
  }
111
  taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), uid);
56,173,417✔
112

113
  // btime
114
  taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos + 8), btime);
56,173,932✔
115

116
  tEndDecode(pCoder);
56,171,257✔
117

118
_exit:
56,173,929✔
119
  if (code) {
56,172,132!
120
    vError("vgId:%d %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
121
  } else {
122
    vTrace("vgId:%d %s done, table:%s uid generated:%" PRId64, TD_VID(pVnode), __func__, name, uid);
56,172,132✔
123
    if (pUid) *pUid = uid;
56,173,890✔
124
  }
125
  return code;
56,173,890✔
126
}
127
static int32_t vnodePreProcessCreateTableMsg(SVnode *pVnode, SRpcMsg *pMsg) {
28,311,948✔
128
  int32_t code = 0;
28,311,948✔
129
  int32_t lino = 0;
28,311,948✔
130

131
  int64_t  btime = taosGetTimestampMs();
28,311,948✔
132
  SDecoder dc = {0};
28,311,948✔
133
  int32_t  nReqs;
28,308,201✔
134

135
  tDecoderInit(&dc, (uint8_t *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead));
28,311,948✔
136
  if (tStartDecode(&dc) < 0) {
28,312,025!
137
    code = TSDB_CODE_INVALID_MSG;
×
138
    return code;
×
139
  }
140

141
  if (tDecodeI32v(&dc, &nReqs) < 0) {
28,311,948!
142
    code = TSDB_CODE_INVALID_MSG;
×
143
    TSDB_CHECK_CODE(code, lino, _exit);
×
144
  }
145
  for (int32_t iReq = 0; iReq < nReqs; iReq++) {
59,394,667✔
146
    code = vnodePreprocessCreateTableReq(pVnode, &dc, btime, NULL);
31,087,671✔
147
    TSDB_CHECK_CODE(code, lino, _exit);
31,082,719!
148
  }
149

150
  tEndDecode(&dc);
28,306,996✔
151

152
_exit:
28,309,988✔
153
  tDecoderClear(&dc);
28,309,840✔
154
  if (code) {
28,310,523!
155
    vError("vgId:%d, %s:%d failed to preprocess submit request since %s, msg type:%s", TD_VID(pVnode), __func__, lino,
×
156
           tstrerror(code), TMSG_INFO(pMsg->msgType));
157
  }
158
  return code;
28,309,939✔
159
}
160

161
static int32_t vnodePreProcessAlterTableMsg(SVnode *pVnode, SRpcMsg *pMsg) {
7,883,820✔
162
  int32_t code = TSDB_CODE_INVALID_MSG;
7,883,820✔
163
  int32_t lino = 0;
7,883,820✔
164

165
  SDecoder dc = {0};
7,883,820✔
166
  tDecoderInit(&dc, (uint8_t *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead));
7,883,820✔
167

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

178
  code = 0;
7,883,820✔
179

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

191
static int32_t vnodePreProcessDropTtlMsg(SVnode *pVnode, SRpcMsg *pMsg) {
7,939,889✔
192
  int32_t code = TSDB_CODE_INVALID_MSG;
7,939,889✔
193
  int32_t lino = 0;
7,939,889✔
194

195
  SMsgHead *pContOld = pMsg->pCont;
7,939,889✔
196
  int32_t   reqLenOld = pMsg->contLen - sizeof(SMsgHead);
7,939,921✔
197

198
  SArray *tbUids = NULL;
7,938,125✔
199
  int64_t timestampMs = 0;
7,938,125✔
200

201
  SVDropTtlTableReq ttlReq = {0};
7,938,125✔
202
  if (tDeserializeSVDropTtlTableReq((char *)pContOld + sizeof(SMsgHead), reqLenOld, &ttlReq) != 0) {
7,939,457!
203
    code = TSDB_CODE_INVALID_MSG;
×
204
    TSDB_CHECK_CODE(code, lino, _exit);
×
205
  }
206

207
  {  // find expired uids
208
    tbUids = taosArrayInit(8, sizeof(tb_uid_t));
7,938,157✔
209
    if (tbUids == NULL) {
7,937,387!
210
      code = terrno;
×
211
      TSDB_CHECK_CODE(code, lino, _exit);
×
212
    }
213

214
    timestampMs = (int64_t)ttlReq.timestampSec * 1000;
7,937,387✔
215
    code = metaTtlFindExpired(pVnode->pMeta, timestampMs, tbUids, ttlReq.ttlDropMaxCount);
7,937,387✔
216
    if (code != 0) {
7,917,511!
217
      code = TSDB_CODE_INVALID_MSG;
×
218
      TSDB_CHECK_CODE(code, lino, _exit);
×
219
    }
220

221
    ttlReq.nUids = taosArrayGetSize(tbUids);
7,917,511✔
222
    ttlReq.pTbUids = tbUids;
7,929,683✔
223
  }
224

225
  if (ttlReq.nUids == 0) {
7,929,683✔
226
    code = TSDB_CODE_MSG_PREPROCESSED;
7,903,790✔
227
    TSDB_CHECK_CODE(code, lino, _exit);
7,903,790!
228
  }
229

230
  {  // prepare new content
231
    int32_t reqLenNew = tSerializeSVDropTtlTableReq(NULL, 0, &ttlReq);
14,740✔
232
    int32_t contLenNew = reqLenNew + sizeof(SMsgHead);
11,666✔
233

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

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

246
    rpcFreeCont(pContOld);
11,666✔
247
    pMsg->pCont = pContNew;
11,666✔
248
    pMsg->contLen = contLenNew;
11,666✔
249
  }
250

251
  code = 0;
11,666✔
252

253
_exit:
7,926,609✔
254
  taosArrayDestroy(tbUids);
7,931,224✔
255

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

263
  return code;
7,909,980✔
264
}
265

266
extern int64_t tsMaxKeyByPrecision[];
267
static int32_t vnodePreProcessSubmitTbData(SVnode *pVnode, SDecoder *pCoder, int64_t btimeMs, int64_t ctimeMs) {
567,936,973✔
268
  int32_t code = 0;
567,936,973✔
269
  int32_t lino = 0;
567,936,973✔
270

271
  if (tStartDecode(pCoder) < 0) {
567,936,973!
272
    code = TSDB_CODE_INVALID_MSG;
×
273
    TSDB_CHECK_CODE(code, lino, _exit);
×
274
  }
275

276
  SSubmitTbData submitTbData;
567,917,267✔
277
  uint8_t       version;
278
  if (tDecodeI32v(pCoder, &submitTbData.flags) < 0) {
567,940,373!
279
    code = TSDB_CODE_INVALID_MSG;
×
280
    TSDB_CHECK_CODE(code, lino, _exit);
×
281
  }
282
  version = (submitTbData.flags >> 8) & 0xff;
567,940,373✔
283
  submitTbData.flags = submitTbData.flags & 0xff;
567,940,373✔
284

285
  int64_t uid;
567,917,386✔
286
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
567,939,058✔
287
    code = vnodePreprocessCreateTableReq(pVnode, pCoder, btimeMs, &uid);
25,089,706✔
288
    TSDB_CHECK_CODE(code, lino, _exit);
25,088,295!
289
  }
290

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

297
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
567,939,394✔
298
    taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), uid);
25,089,608✔
299
    pCoder->pos += sizeof(int64_t);
25,088,269✔
300
  } else {
301
    if (tDecodeI64(pCoder, &submitTbData.uid) < 0) {
542,849,123!
302
      code = TSDB_CODE_INVALID_MSG;
×
303
      TSDB_CHECK_CODE(code, lino, _exit);
×
304
    }
305
  }
306

307
  if (tDecodeI32v(pCoder, &submitTbData.sver) < 0) {
567,938,010!
308
    code = TSDB_CODE_INVALID_MSG;
×
309
    TSDB_CHECK_CODE(code, lino, _exit);
×
310
  }
311

312
  // scan and check
313
  TSKEY now = btimeMs;
567,938,010✔
314
  if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_MICRO) {
567,938,010✔
315
    now *= 1000;
1,611,512✔
316
  } else if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_NANO) {
566,326,980✔
317
    now *= 1000000;
1,335,584✔
318
  }
319

320
  int32_t keep = pVnode->config.tsdbCfg.keep2;
567,938,527✔
321

322
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * keep;
567,937,436✔
323
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
567,937,161✔
324
  if (submitTbData.flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
567,939,853✔
325
    uint64_t nColData;
713,570✔
326
    if (tDecodeU64v(pCoder, &nColData) < 0) {
713,500!
327
      code = TSDB_CODE_INVALID_MSG;
×
328
      TSDB_CHECK_CODE(code, lino, _exit);
×
329
    }
330

331
    SColData colData = {0};
713,500✔
332
    code = tDecodeColData(version, pCoder, &colData, false);
713,570✔
333
    if (code) {
713,710!
334
      code = TSDB_CODE_INVALID_MSG;
×
335
      TSDB_CHECK_CODE(code, lino, _exit);
×
336
    }
337

338
    if (colData.flag != HAS_VALUE) {
713,710!
339
      code = TSDB_CODE_INVALID_MSG;
×
340
      TSDB_CHECK_CODE(code, lino, _exit);
×
341
    }
342

343
    for (int32_t iRow = 0; iRow < colData.nVal; iRow++) {
365,343,784✔
344
      if (((TSKEY *)colData.pData)[iRow] < minKey || ((TSKEY *)colData.pData)[iRow] > maxKey) {
364,659,652✔
345
        code = TSDB_CODE_TDB_TIMESTAMP_OUT_OF_RANGE;
738✔
346
        TSDB_CHECK_CODE(code, lino, _exit);
738!
347
      }
348
    }
349

350
    for (uint64_t i = 1; i < nColData; i++) {
3,702,978✔
351
      code = tDecodeColData(version, pCoder, &colData, true);
3,018,846✔
352
      if (code) {
3,018,846!
353
        code = TSDB_CODE_INVALID_MSG;
×
354
        TSDB_CHECK_CODE(code, lino, _exit);
×
355
      }
356
    }
357
  } else {
358
    uint64_t nRow;
567,203,296✔
359
    if (tDecodeU64v(pCoder, &nRow) < 0) {
567,225,371!
360
      code = TSDB_CODE_INVALID_MSG;
×
361
      TSDB_CHECK_CODE(code, lino, _exit);
×
362
    }
363

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

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

385
  if (!tDecodeIsEnd(pCoder)) {
567,855,389!
386
    taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), ctimeMs);
567,858,661✔
387
    pCoder->pos += sizeof(int64_t);
567,852,092✔
388
  }
389

390
  tEndDecode(pCoder);
567,856,430✔
391

392
_exit:
567,932,277✔
393
  if (code) {
567,934,067✔
394
    vError("vgId:%d, %s:%d failed to vnodePreProcessSubmitTbData submit request since %s", TD_VID(pVnode), __func__,
77,997!
395
           lino, tstrerror(code));
396
  }
397
  return code;
567,934,067✔
398
}
399
static int32_t vnodePreProcessSubmitMsg(SVnode *pVnode, SRpcMsg *pMsg) {
528,477,701✔
400
  int32_t code = 0;
528,477,701✔
401
  int32_t lino = 0;
528,477,701✔
402

403
  if (tsBypassFlag & TSDB_BYPASS_RA_RPC_RECV_SUBMIT) {
528,477,701✔
404
    return TSDB_CODE_MSG_PREPROCESSED;
465✔
405
  }
406

407
  SDecoder *pCoder = &(SDecoder){0};
528,477,236✔
408

409
  if (taosHton64(((SSubmitReq2Msg *)pMsg->pCont)->version) != 1) {
528,478,030!
410
    code = TSDB_CODE_INVALID_MSG;
×
411
    TSDB_CHECK_CODE(code, lino, _exit);
×
412
  }
413

414
  tDecoderInit(pCoder, (uint8_t *)pMsg->pCont + sizeof(SSubmitReq2Msg), pMsg->contLen - sizeof(SSubmitReq2Msg));
528,478,427✔
415

416
  if (tStartDecode(pCoder) < 0) {
528,478,561!
417
    code = TSDB_CODE_INVALID_MSG;
×
418
    TSDB_CHECK_CODE(code, lino, _exit);
×
419
  }
420

421
  uint64_t nSubmitTbData;
528,455,048✔
422
  if (tDecodeU64v(pCoder, &nSubmitTbData) < 0) {
528,478,293!
423
    code = TSDB_CODE_INVALID_MSG;
×
424
    TSDB_CHECK_CODE(code, lino, _exit);
×
425
  }
426

427
  int64_t btimeMs = taosGetTimestampMs();
528,477,269✔
428
  int64_t ctimeMs = btimeMs;
528,477,269✔
429
  for (int32_t i = 0; i < nSubmitTbData; i++) {
1,096,334,530✔
430
    code = vnodePreProcessSubmitTbData(pVnode, pCoder, btimeMs, ctimeMs);
567,936,044✔
431
    TSDB_CHECK_CODE(code, lino, _exit);
567,935,258✔
432
  }
433

434
  tEndDecode(pCoder);
528,398,486✔
435

436
_exit:
528,477,096✔
437
  tDecoderClear(pCoder);
528,477,096✔
438
  if (code) {
528,476,626✔
439
    vError("vgId:%d, %s:%d failed to preprocess submit request since %s, msg type:%s", TD_VID(pVnode), __func__, lino,
77,997!
440
           tstrerror(code), TMSG_INFO(pMsg->msgType));
441
  }
442
  return code;
528,474,142✔
443
}
444

445
static int32_t vnodePreProcessDeleteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
3,042,633✔
446
  int32_t code = 0;
3,042,633✔
447

448
  int32_t    size;
449
  int32_t    ret;
450
  uint8_t   *pCont;
451
  SEncoder  *pCoder = &(SEncoder){0};
3,042,633✔
452
  SDeleteRes res = {0};
3,042,633✔
453

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

460
  code = qWorkerProcessDeleteMsg(&handle, pVnode->pQuery, pMsg, &res);
3,042,633✔
461
  if (code) goto _exit;
3,042,633!
462

463
  res.ctimeMs = taosGetTimestampMs();
3,042,633✔
464
  // malloc and encode
465
  tEncodeSize(tEncodeDeleteRes, &res, size, ret);
3,042,633!
466
  pCont = rpcMallocCont(size + sizeof(SMsgHead));
3,041,903✔
467

468
  ((SMsgHead *)pCont)->contLen = size + sizeof(SMsgHead);
3,042,633✔
469
  ((SMsgHead *)pCont)->vgId = TD_VID(pVnode);
3,042,633✔
470

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

477
  rpcFreeCont(pMsg->pCont);
3,041,903✔
478
  pMsg->pCont = pCont;
3,042,633✔
479
  pMsg->contLen = size + sizeof(SMsgHead);
3,042,633✔
480

481
  taosArrayDestroy(res.uidList);
3,042,633✔
482

483
_exit:
3,042,633✔
484
  return code;
3,042,633✔
485
}
486

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

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

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

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

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

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

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

519
  SVArbCheckSyncReq syncReq = {0};
32,719✔
520

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

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

531
  int32_t code = terrno;
32,719✔
532
  tFreeSVArbCheckSyncReq(&syncReq);
32,719✔
533

534
  return code;
32,719✔
535
}
536

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

546
  tDecoderInit(&dc, POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), pMsg->contLen - sizeof(SMsgHead));
8,601,524✔
547

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

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

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

584
_exit:
8,601,524✔
585
  tDecoderClear(&dc);
8,601,524✔
586
  if (sentBatchReqs.pArray) {
8,601,524!
587
    taosArrayDestroy(sentBatchReqs.pArray);
8,601,524✔
588
  }
589
  return code;
8,601,524✔
590
}
591

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

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

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

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

611
int32_t vnodePreProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
602,039,973✔
612
  int32_t code = 0;
602,039,973✔
613

614
  if (pVnode->mounted) {
602,039,973!
615
    vError("vgId:%d, mountVgId:%d, skip write msg:%s", TD_VID(pVnode), pVnode->config.mountVgId,
1,580!
616
           TMSG_INFO(pMsg->msgType));
617
    return TSDB_CODE_OPS_NOT_SUPPORT;
1,580✔
618
  }
619

620
  switch (pMsg->msgType) {
602,040,654!
621
    case TDMT_VND_CREATE_TABLE: {
28,312,025✔
622
      code = vnodePreProcessCreateTableMsg(pVnode, pMsg);
28,312,025✔
623
    } break;
28,309,969✔
624
    case TDMT_VND_ALTER_TABLE: {
7,883,820✔
625
      code = vnodePreProcessAlterTableMsg(pVnode, pMsg);
7,883,820✔
626
    } break;
7,883,820✔
627
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
7,939,863✔
628
    case TDMT_VND_DROP_TTL_TABLE: {
629
      code = vnodePreProcessDropTtlMsg(pVnode, pMsg);
7,939,863✔
630
    } break;
7,914,115✔
631
    case TDMT_VND_SUBMIT: {
528,478,639✔
632
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.preprocess_time, METRIC_LEVEL_LOW,
528,478,639!
633
                           { code = vnodePreProcessSubmitMsg(pVnode, pMsg); });
634
    } break;
528,476,292✔
635
    case TDMT_VND_DELETE: {
3,042,633✔
636
      code = vnodePreProcessDeleteMsg(pVnode, pMsg);
3,042,633✔
637
    } break;
3,042,633✔
638
    case TDMT_VND_BATCH_DEL: {
×
639
      code = vnodePreProcessBatchDeleteMsg(pVnode, pMsg);
×
640
    } break;
×
641
    case TDMT_VND_ARB_CHECK_SYNC: {
60,591✔
642
      code = vnodePreProcessArbCheckSyncMsg(pVnode, pMsg);
60,591✔
643
    } break;
60,591✔
644
    case TDMT_VND_DROP_TABLE: {
8,601,524✔
645
      code = vnodePreProcessDropTbMsg(pVnode, pMsg);
8,601,524✔
646
    } break;
8,601,524✔
647
#ifdef TD_ENTERPRISE
648
    case TDMT_VND_SSMIGRATE_FILESET: {
×
649
      code = vnodePreProcessSsMigrateFileSetReq(pVnode, pMsg);
×
650
    } break;
×
651
#endif
652
    default:
17,721,186✔
653
      break;
17,721,186✔
654
  }
655

656
  if (code && code != TSDB_CODE_MSG_PREPROCESSED) {
602,010,130✔
657
    vError("vgId:%d, failed to preprocess write request since %s, msg type:%s", TD_VID(pVnode), tstrerror(code),
108,851!
658
           TMSG_INFO(pMsg->msgType));
659
  }
660
  return code;
602,018,990✔
661
}
662

663
static int32_t inline vnodeSubmitSubRowBlobData(SVnode *pVnode, SSubmitTbData *pSubmitTbData) {
40,020✔
664
  int32_t code = 0;
40,020✔
665
  int32_t lino = 0;
40,020✔
666

667
  int64_t   st = taosGetTimestampUs();
40,020✔
668
  SBlobSet *pBlobSet = pSubmitTbData->pBlobSet;
40,020✔
669
  int32_t   sz = taosArrayGetSize(pBlobSet->pSeqTable);
40,020✔
670

671
  SBseBatch *pBatch = NULL;
40,020✔
672

673
  code = bseBatchInit(pVnode->pBse, &pBatch, sz);
40,020✔
674
  TSDB_CHECK_CODE(code, lino, _exit);
40,020!
675

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

686
    code = bseBatchPut(pBatch, &seq, pBlobSet->data + p->offset, p->len);
24,360✔
687
    TSDB_CHECK_CODE(code, lino, _exit);
24,360!
688

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

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

702
  code = bseCommitBatch(pVnode->pBse, pBatch);
40,020✔
703
  TSDB_CHECK_CODE(code, lino, _exit);
40,020!
704

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

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

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

727
  SBseBatch *pBatch = NULL;
×
728

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

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

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

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

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

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

785
  return code;
40,020✔
786
}
787

788
int32_t vnodeProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg, int64_t ver, SRpcMsg *pRsp) {
633,895,099✔
789
  int32_t code = 0;
633,895,099✔
790
  int32_t lino = 0;
633,895,099✔
791
  void   *ptr = NULL;
633,895,099✔
792
  void   *pReq;
793
  int32_t len;
794

795
  (void)taosThreadMutexLock(&pVnode->mutex);
633,895,099✔
796
  if (pVnode->disableWrite) {
633,983,903!
797
    (void)taosThreadMutexUnlock(&pVnode->mutex);
×
798
    vError("vgId:%d write is disabled for snapshot, version:%" PRId64, TD_VID(pVnode), ver);
×
799
    return TSDB_CODE_VND_WRITE_DISABLED;
×
800
  }
801
  (void)taosThreadMutexUnlock(&pVnode->mutex);
633,920,313✔
802

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

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

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

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

827
  atomic_store_64(&pVnode->state.applied, ver);
633,994,021✔
828
  atomic_store_64(&pVnode->state.applyTerm, pMsg->info.conn.applyTerm);
633,995,246✔
829

830
  if (!syncUtilUserCommit(pMsg->msgType)) goto _exit;
633,993,514✔
831

832
  // skip header
833
  pReq = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
623,010,877✔
834
  len = pMsg->contLen - sizeof(SMsgHead);
623,010,047✔
835
  bool needCommit = false;
623,008,292✔
836

837
  switch (pMsg->msgType) {
623,008,292!
838
    /* META */
839
    case TDMT_VND_CREATE_STB:
6,353,200✔
840
      code = vnodeProcessCreateStbReq(pVnode, ver, pReq, len, pRsp);
6,353,200✔
841
      TSDB_CHECK_CODE(code, lino, _err);
6,353,290✔
842
      break;
6,350,680✔
843
    case TDMT_VND_ALTER_STB:
4,937,099✔
844
      code = vnodeProcessAlterStbReq(pVnode, ver, pReq, len, pRsp);
4,937,099✔
845
      TSDB_CHECK_CODE(code, lino, _err);
4,937,099!
846
      break;
4,937,099✔
847
    case TDMT_VND_DROP_STB:
1,202,783✔
848
      code = vnodeProcessDropStbReq(pVnode, ver, pReq, len, pRsp, pMsg);
1,202,783✔
849
      TSDB_CHECK_CODE(code, lino, _err);
1,202,783!
850
      break;
1,202,783✔
851
    case TDMT_VND_CREATE_TABLE:
30,108,413✔
852
      code = vnodeProcessCreateTbReq(pVnode, ver, pReq, len, pRsp, pMsg);
30,108,413✔
853
      TSDB_CHECK_CODE(code, lino, _err);
30,105,562!
854
      break;
30,105,562✔
855
    case TDMT_VND_ALTER_TABLE:
7,885,150✔
856
      code = vnodeProcessAlterTbReq(pVnode, ver, pReq, len, pRsp);
7,885,150✔
857
      TSDB_CHECK_CODE(code, lino, _err);
7,885,150!
858
      break;
7,885,150✔
859
    case TDMT_VND_DROP_TABLE:
8,602,556✔
860
    case TDMT_VND_SNODE_DROP_TABLE:
861
      code = vnodeProcessDropTbReq(pVnode, ver, pReq, len, pRsp, pMsg);
8,602,556✔
862
      TSDB_CHECK_CODE(code, lino, _err);
8,602,556!
863
      break;
8,602,556✔
864
    case TDMT_VND_CREATE_RSMA:
13,972✔
865
      code = vnodeProcessCreateRsmaReq(pVnode, ver, pReq, len, pRsp);
13,972✔
866
      TSDB_CHECK_CODE(code, lino, _err);
13,473!
867
      break;
13,473✔
868
    case TDMT_VND_DROP_RSMA:
2,994✔
869
      code = vnodeProcessDropRsmaReq(pVnode, ver, pReq, len, pRsp);
2,994✔
870
      TSDB_CHECK_CODE(code, lino, _err);
2,994!
871
      break;
2,994✔
872
    case TDMT_VND_ALTER_RSMA:
6,986✔
873
      code = vnodeProcessAlterRsmaReq(pVnode, ver, pReq, len, pRsp);
6,986✔
874
      TSDB_CHECK_CODE(code, lino, _err);
6,986!
875
      break;
6,986✔
876
    case TDMT_VND_DROP_TTL_TABLE:
×
877
      code = vnodeProcessDropTtlTbReq(pVnode, ver, pReq, len, pRsp);
×
878
      TSDB_CHECK_CODE(code, lino, _err);
×
879
      break;
×
880
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
11,666✔
881
      code = vnodeProcessFetchTtlExpiredTbs(pVnode, ver, pReq, len, pRsp);
11,666✔
882
      TSDB_CHECK_CODE(code, lino, _err);
11,666!
883
      break;
11,666✔
884
    case TDMT_VND_TRIM:
41,015✔
885
      code = vnodeProcessTrimReq(pVnode, ver, pReq, len, pRsp);
41,015✔
886
      TSDB_CHECK_CODE(code, lino, _err);
41,015!
887
      break;
41,015✔
888
#ifdef TD_ENTERPRISE
889
    case TDMT_VND_SSMIGRATE_FILESET:
×
890
      if (vnodeProcessSsMigrateFileSetReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
891
      break;
×
892
    case TDMT_VND_FOLLOWER_SSMIGRATE:
×
893
      if (vnodeProcessFollowerSsMigrateReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
894
      break;
×
895
    case TDMT_VND_KILL_SSMIGRATE:
×
896
      if (vnodeProcessKillSsMigrateReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
897
      break;
×
898
#endif
899
    /* TSDB */
900
    case TDMT_VND_SUBMIT: {
548,748,346✔
901
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.apply_time, METRIC_LEVEL_LOW, {
548,748,346!
902
        if (vnodeProcessSubmitReq(pVnode, ver, pMsg->pCont, pMsg->contLen, pRsp, pMsg) < 0) goto _err;
903
      });
904
      METRICS_UPDATE(pVnode->writeMetrics.apply_bytes, METRIC_LEVEL_LOW, (int64_t)pMsg->contLen);
548,682,305!
905
      break;
548,676,460✔
906
    }
907
    case TDMT_VND_DELETE:
3,109,095✔
908
      code = vnodeProcessDeleteReq(pVnode, ver, pReq, len, pRsp, pMsg);
3,109,095✔
909
      TSDB_CHECK_CODE(code, lino, _err);
3,109,095!
910
      break;
3,109,095✔
911
    case TDMT_VND_BATCH_DEL:
×
912
      code = vnodeProcessBatchDeleteReq(pVnode, ver, pReq, len, pRsp);
×
913
      TSDB_CHECK_CODE(code, lino, _err);
×
914
      break;
×
915
      /* TQ */
916
#if defined(USE_TQ) || defined(USE_STREAM)
917
    case TDMT_VND_TMQ_SUBSCRIBE:
275,165✔
918
      code = tqProcessSubscribeReq(pVnode->pTq, ver, pReq, len);
275,165✔
919
      TSDB_CHECK_CODE(code, lino, _err);
274,383!
920
      break;
274,383✔
921
    case TDMT_VND_TMQ_DELETE_SUB:
102,567✔
922
      code = tqProcessDeleteSubReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen);
102,567✔
923
      TSDB_CHECK_CODE(code, lino, _err);
102,324!
924
      break;
102,324✔
925
    case TDMT_VND_TMQ_COMMIT_OFFSET:
492,057✔
926
      code = tqProcessOffsetCommitReq(pVnode->pTq, ver, pReq, len);
492,057✔
927
      TSDB_CHECK_CODE(code, lino, _err);
491,907!
928
      break;
491,907✔
929
    case TDMT_VND_TMQ_ADD_CHECKINFO:
4,469✔
930
      code = tqProcessAddCheckInfoReq(pVnode->pTq, ver, pReq, len);
4,469✔
931
      TSDB_CHECK_CODE(code, lino, _err);
4,469!
932
      break;
4,469✔
933
    case TDMT_VND_TMQ_DEL_CHECKINFO:
1,046✔
934
      code = tqProcessDelCheckInfoReq(pVnode->pTq, ver, pReq, len);
1,046✔
935
      TSDB_CHECK_CODE(code, lino, _err);
1,046!
936
      break;
1,046✔
937
#endif
938
    case TDMT_VND_ALTER_CONFIRM:
4,504,830✔
939
      needCommit = pVnode->config.hashChange;
4,504,830!
940
      code = vnodeProcessAlterConfirmReq(pVnode, ver, pReq, len, pRsp);
4,504,830✔
941
      TSDB_CHECK_CODE(code, lino, _err);
4,504,830!
942
      break;
4,504,830✔
943
    case TDMT_VND_ALTER_CONFIG:
260,578✔
944
      vnodeProcessAlterConfigReq(pVnode, ver, pReq, len, pRsp);
260,578✔
945
      break;
260,578✔
946
    case TDMT_VND_COMMIT:
6,277,414✔
947
      needCommit = true;
6,277,414✔
948
      break;
6,277,414✔
949
    case TDMT_VND_CREATE_INDEX:
7,153✔
950
      vnodeProcessCreateIndexReq(pVnode, ver, pReq, len, pRsp);
7,153✔
951
      break;
7,153✔
952
    case TDMT_VND_DROP_INDEX:
5,168✔
953
      vnodeProcessDropIndexReq(pVnode, ver, pReq, len, pRsp);
5,168✔
954
      break;
5,168✔
955
    case TDMT_VND_COMPACT:
45,993✔
956
      vnodeProcessCompactVnodeReq(pVnode, ver, pReq, len, pRsp);
45,993✔
957
      goto _exit;
45,993✔
958
    case TDMT_VND_SCAN:
655✔
959
      vnodeProcessScanVnodeReq(pVnode, ver, pReq, len, pRsp);
655✔
960
      goto _exit;
655✔
961
    case TDMT_SYNC_CONFIG_CHANGE:
×
962
      vnodeProcessConfigChangeReq(pVnode, ver, pReq, len, pRsp);
×
963
      break;
×
964
#ifdef TD_ENTERPRISE
965
    case TDMT_VND_KILL_COMPACT:
262✔
966
      vnodeProcessKillCompactReq(pVnode, ver, pReq, len, pRsp);
262✔
967
      break;
262✔
968
#endif
969
    case TDMT_VND_KILL_SCAN:
262✔
970
      vnodeProcessKillScanReq(pVnode, ver, pReq, len, pRsp);
262✔
971
      break;
262✔
972
    case TDMT_VND_KILL_TRIM:
×
973
      vnodeProcessKillRetentionReq(pVnode, ver, pReq, len, pRsp);
×
974
      break;
×
975
    /* ARB */
976
    case TDMT_VND_ARB_CHECK_SYNC:
8,229✔
977
      vnodeProcessArbCheckSyncReq(pVnode, pReq, len, pRsp);
8,229✔
978
      break;
8,229✔
979
    default:
×
980
      vError("vgId:%d, unprocessed msg, %d", TD_VID(pVnode), pMsg->msgType);
×
981
      return TSDB_CODE_INVALID_MSG;
×
982
  }
983

984
  vGDebug(&pMsg->info.traceId, "vgId:%d, index:%" PRId64 ", msg processed, code:0x%x", TD_VID(pVnode), ver, pRsp->code);
622,883,544!
985

986
  walApplyVer(pVnode->pWal, ver);
622,891,179✔
987

988
  if (pVnode->pTq) {
622,900,319!
989
    code = tqPushMsg(pVnode->pTq, pMsg->msgType);
622,900,319✔
990
    if (code) {
622,899,414✔
991
      vError("vgId:%d, failed to push msg to TQ since %s", TD_VID(pVnode), tstrerror(terrno));
22!
992
      return code;
22✔
993
    }
994
  }
995

996
  // commit if need
997
  if (needCommit) {
622,899,392✔
998
    vInfo("vgId:%d, commit at version %" PRId64, TD_VID(pVnode), ver);
6,284,465✔
999
    code = vnodeAsyncCommit(pVnode);
6,284,465✔
1000
    if (code) {
6,284,465!
1001
      vError("vgId:%d, failed to vnode async commit since %s.", TD_VID(pVnode), tstrerror(terrno));
×
1002
      goto _err;
×
1003
    }
1004

1005
    // start a new one
1006
    METRICS_TIMING_BLOCK(pVnode->writeMetrics.memtable_wait_time, METRIC_LEVEL_LOW, {
6,284,465!
1007
      code = vnodeBegin(pVnode);
1008
      if (code) {
1009
        vError("vgId:%d, failed to begin vnode since %s.", TD_VID(pVnode), tstrerror(terrno));
1010
        goto _err;
1011
      }
1012
    });
1013
  }
1014

1015
_exit:
622,896,882✔
1016
  return 0;
633,925,889✔
1017

1018
_err:
64,309✔
1019
  if (code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
64,309✔
1020
    vInfo("vgId:%d, process %s request failed since %s, lino 1:%d ver:%" PRId64, TD_VID(pVnode),
2,610!
1021
          TMSG_INFO(pMsg->msgType), tstrerror(code), lino, ver);
1022
  } else if (code) {
61,699!
1023
    vError("vgId:%d, process %s request failed since %s, lino 2:%d ver:%" PRId64, TD_VID(pVnode),
×
1024
           TMSG_INFO(pMsg->msgType), tstrerror(code), lino, ver);
1025
  }
1026

1027
  return code;
64,309✔
1028
}
1029

1030
int32_t vnodePreprocessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg) {
275,735,384✔
1031
  if (TDMT_SCH_QUERY != pMsg->msgType && TDMT_SCH_MERGE_QUERY != pMsg->msgType) {
275,735,384✔
1032
    return 0;
46,744,407✔
1033
  }
1034

1035
  int32_t qType = 0;
229,000,223✔
1036
  return qWorkerPreprocessQueryMsg(pVnode->pQuery, pMsg, (TDMT_SCH_QUERY == pMsg->msgType), &qType);
229,004,755✔
1037
}
1038

1039
int32_t vnodeProcessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
275,748,351✔
1040
  vTrace("message in vnode query queue is processing");
275,748,351!
1041
  if (pMsg->msgType == TDMT_VND_TMQ_CONSUME && !syncIsReadyForRead(pVnode->sync)) {
275,748,351✔
1042
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
27,555✔
1043
    return 0;
27,555✔
1044
  }
1045

1046
  if (pMsg->msgType == TDMT_VND_TMQ_CONSUME && !pVnode->restored) {
275,724,254!
1047
    vnodeRedirectRpcMsg(pVnode, pMsg, TSDB_CODE_SYN_RESTORING);
×
1048
    return 0;
×
1049
  }
1050

1051
  SReadHandle handle = {0};
275,724,092✔
1052
  handle.vnode = pVnode;
275,723,362✔
1053
  handle.pMsgCb = &pVnode->msgCb;
275,723,362✔
1054
  handle.pWorkerCb = pInfo->workerCb;
275,713,039✔
1055
  initStorageAPI(&handle.api);
275,715,880✔
1056
  int32_t code = TSDB_CODE_SUCCESS;
275,674,251✔
1057
  bool    redirected = false;
275,674,251✔
1058

1059
  switch (pMsg->msgType) {
275,674,251!
1060
    case TDMT_SCH_QUERY:
191,218,911✔
1061
      if (!syncIsReadyForRead(pVnode->sync)) {
191,218,911✔
1062
        pMsg->code = (terrno) ? terrno : TSDB_CODE_SYN_NOT_LEADER;
34,820,621!
1063
        redirected = true;
34,820,621✔
1064
      }
1065
      code = qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
191,248,683✔
1066
      if (redirected) {
191,262,961✔
1067
        vnodeRedirectRpcMsg(pVnode, pMsg, pMsg->code);
34,820,621✔
1068
        return 0;
34,820,621✔
1069
      }
1070

1071
      return code;
156,442,340✔
1072
    case TDMT_SCH_MERGE_QUERY:
37,745,209✔
1073
      return qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
37,745,209✔
1074
    case TDMT_SCH_QUERY_CONTINUE:
40,855,253✔
1075
      return qWorkerProcessCQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
40,855,253✔
1076
    case TDMT_VND_TMQ_CONSUME:
4,658,816✔
1077
      return tqProcessPollReq(pVnode->pTq, pMsg);
4,658,816✔
1078
    case TDMT_VND_TMQ_CONSUME_PUSH:
1,200,044✔
1079
      return tqProcessPollPush(pVnode->pTq);
1,200,044✔
1080
    default:
×
1081
      vError("unknown msg type:%d in query queue", pMsg->msgType);
×
1082
      return TSDB_CODE_APP_ERROR;
×
1083
  }
1084
}
1085

1086
int32_t vnodeProcessFetchMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
671,158,560✔
1087
  int32_t code = TSDB_CODE_SUCCESS;
671,158,560✔
1088
  vTrace("vgId:%d, msg:%p in fetch queue is processing", pVnode->config.vgId, pMsg);
671,158,560!
1089
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
671,158,560!
1090
       pMsg->msgType == TDMT_VND_BATCH_META || pMsg->msgType == TDMT_VND_TABLE_NAME ||
478,467,728!
1091
       pMsg->msgType == TDMT_VND_VSUBTABLES_META || pMsg->msgType == TDMT_VND_VSTB_REF_DBS) &&
433,979,051!
1092
      !syncIsReadyForRead(pVnode->sync)) {
237,189,031✔
1093
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
36,205✔
1094
    return 0;
36,205✔
1095
  }
1096

1097
  switch (pMsg->msgType) {
671,159,973!
1098
    case TDMT_SCH_FETCH:
244,285,695✔
1099
    case TDMT_SCH_MERGE_FETCH:
1100
      return qWorkerProcessFetchMsg(pVnode, pVnode->pQuery, pMsg, 0);
244,285,695✔
1101
    case TDMT_SCH_FETCH_RSP:
×
1102
      return qWorkerProcessRspMsg(pVnode, pVnode->pQuery, pMsg, 0);
×
1103
    // case TDMT_SCH_CANCEL_TASK:
1104
    //   return qWorkerProcessCancelMsg(pVnode, pVnode->pQuery, pMsg, 0);
1105
    case TDMT_SCH_DROP_TASK:
228,784,759✔
1106
      return qWorkerProcessDropMsg(pVnode, pVnode->pQuery, pMsg, 0);
228,784,759✔
1107
    case TDMT_SCH_TASK_NOTIFY:
17,299✔
1108
      return qWorkerProcessNotifyMsg(pVnode, pVnode->pQuery, pMsg, 0);
17,299✔
1109
    case TDMT_SCH_QUERY_HEARTBEAT:
152,220,969✔
1110
      return qWorkerProcessHbMsg(pVnode, pVnode->pQuery, pMsg, 0);
152,220,969✔
1111
    case TDMT_VND_TABLE_META:
1,252,330✔
1112
    case TDMT_VND_TABLE_NAME:
1113
      return vnodeGetTableMeta(pVnode, pMsg, true);
1,252,330✔
1114
    case TDMT_VND_TABLE_CFG:
×
1115
      return vnodeGetTableCfg(pVnode, pMsg, true);
×
1116
    case TDMT_VND_BATCH_META: {
44,458,387✔
1117
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.fetch_batch_meta_time, METRIC_LEVEL_LOW,
44,458,387!
1118
                           { code = vnodeGetBatchMeta(pVnode, pMsg); });
1119
      METRICS_UPDATE(pVnode->writeMetrics.fetch_batch_meta_count, METRIC_LEVEL_LOW, 1);
44,459,738!
1120
      return code;
44,457,396✔
1121
    }
1122
    case TDMT_VND_VSUBTABLES_META:
×
1123
      return vnodeGetVSubtablesMeta(pVnode, pMsg);
×
1124
    case TDMT_VND_VSTB_REF_DBS:
×
1125
      return vnodeGetVStbRefDbs(pVnode, pMsg);
×
1126
    case TDMT_VND_QUERY_SCAN_PROGRESS:
1,310✔
1127
      return vnodeQueryScanProgress(pVnode, pMsg);
1,310✔
1128
#ifdef TD_ENTERPRISE
1129
    case TDMT_VND_QUERY_COMPACT_PROGRESS:
64,520✔
1130
      return vnodeQueryCompactProgress(pVnode, pMsg);
64,520✔
1131

1132
    case TDMT_VND_LIST_SSMIGRATE_FILESETS:
×
1133
      return vnodeListSsMigrateFileSets(pVnode, pMsg);
×
1134

1135
    case TDMT_VND_QUERY_SSMIGRATE_PROGRESS:
×
1136
      return vnodeQuerySsMigrateProgress(pVnode, pMsg);
×
1137
#endif
1138
    case TDMT_VND_QUERY_TRIM_PROGRESS:
50,953✔
1139
      return vnodeQueryRetentionProgress(pVnode, pMsg);
50,953✔
1140
      //    case TDMT_VND_TMQ_CONSUME:
1141
      //      return tqProcessPollReq(pVnode->pTq, pMsg);
1142
#ifdef USE_TQ
1143
    case TDMT_VND_TMQ_VG_WALINFO:
195✔
1144
      return tqProcessVgWalInfoReq(pVnode->pTq, pMsg);
195✔
1145
    case TDMT_VND_TMQ_VG_COMMITTEDINFO:
128✔
1146
      return tqProcessVgCommittedInfoReq(pVnode->pTq, pMsg);
128✔
1147
    case TDMT_VND_TMQ_SEEK:
168✔
1148
      return tqProcessSeekReq(pVnode->pTq, pMsg);
168✔
1149
#endif
1150
    default:
14,796✔
1151
      vError("unknown msg type:%d in fetch queue", pMsg->msgType);
14,796!
1152
      return TSDB_CODE_APP_ERROR;
×
1153
  }
1154
}
1155

1156
void vnodeUpdateMetaRsp(SVnode *pVnode, STableMetaRsp *pMetaRsp) {
62,441,253✔
1157
  if (NULL == pMetaRsp) {
62,441,253!
1158
    return;
×
1159
  }
1160

1161
  tstrncpy(pMetaRsp->dbFName, pVnode->config.dbname, TSDB_DB_FNAME_LEN);
62,441,253!
1162
  pMetaRsp->dbId = pVnode->config.dbId;
62,441,361✔
1163
  pMetaRsp->vgId = TD_VID(pVnode);
62,441,156✔
1164
  pMetaRsp->precision = pVnode->config.tsdbCfg.precision;
62,440,578✔
1165
}
1166

1167
extern int32_t vnodeAsyncRetention(SVnode *pVnode, STimeWindow tw, int8_t optrType, int8_t triggerType);
1168

1169
static int32_t vnodeProcessTrimReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
40,883✔
1170
  if (!pVnode->restored) {
40,883!
1171
    vInfo("vgId:%d, ignore trim req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
13,043!
1172
    return 0;
13,043✔
1173
  }
1174

1175
  int32_t     code = 0;
27,840✔
1176
  SVTrimDbReq trimReq = {0};
27,840✔
1177

1178
  // decode
1179
  if (tDeserializeSVTrimDbReq(pReq, len, &trimReq) != 0) {
27,972!
1180
    code = TSDB_CODE_INVALID_MSG;
×
1181
    goto _exit;
×
1182
  }
1183

1184
  vInfo("vgId:%d, process trim vnode request, time:%" PRIi64 ",%" PRIi64 ", optr:%d, trigger:%d", pVnode->config.vgId,
27,972!
1185
        trimReq.tw.skey, trimReq.tw.ekey, (int32_t)trimReq.optrType, (int32_t)trimReq.triggerType);
1186

1187
  code = vnodeAsyncRetention(pVnode, trimReq.tw, (int8_t)trimReq.optrType, (int8_t)trimReq.triggerType);
27,972✔
1188

1189
_exit:
27,972✔
1190
  return code;
27,972✔
1191
}
1192

1193
extern int32_t vnodeAsyncSsMigrateFileSet(SVnode *pVnode, SSsMigrateFileSetReq *pReq);
1194

1195
static int32_t vnodeProcessSsMigrateFileSetReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1196
  int32_t code = 0;
×
1197

1198
  SSsMigrateFileSetReq req = {0};
×
1199
  SSsMigrateFileSetRsp rsp = {0};
×
1200
  pRsp->msgType = TDMT_VND_SSMIGRATE_FILESET_RSP;
×
1201
  pRsp->code = 0;
×
1202
  pRsp->pCont = NULL;
×
1203
  pRsp->contLen = 0;
×
1204

1205
  if (tDeserializeSSsMigrateFileSetReq(pReq, len, &req) != 0) {
×
1206
    code = TSDB_CODE_INVALID_MSG;
×
1207
    goto _exit;
×
1208
  }
1209

1210
  vInfo("vgId:%d, ssmigrate:%d, fid:%d, process ssmigrate fileset request, time:%" PRId64, pVnode->config.vgId,
×
1211
        req.ssMigrateId, req.fid, req.startTimeSec);
1212
  rsp.ssMigrateId = req.ssMigrateId;
×
1213
  rsp.vgId = TD_VID(pVnode);
×
1214
  rsp.nodeId = req.nodeId;
×
1215
  rsp.fid = req.fid;
×
1216

1217
  code = vnodeAsyncSsMigrateFileSet(pVnode, &req);
×
1218
  if (code != TSDB_CODE_SUCCESS) {
×
1219
    vError("vgId:%d, failed to async ssmigrate since %s", TD_VID(pVnode), tstrerror(code));
×
1220
    pRsp->code = code;
×
1221
    goto _exit;
×
1222
  }
1223

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

1234
_exit:
×
1235
  pRsp->code = code;
×
1236
  return code;
×
1237
}
1238

1239
extern int32_t vnodeFollowerSsMigrate(SVnode *pVnode, SSsMigrateProgress *pReq);
1240

1241
static int32_t vnodeProcessFollowerSsMigrateReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1242
  int32_t            code = 0;
×
1243
  SSsMigrateProgress req = {0};
×
1244

1245
  // decode
1246
  if (tDeserializeSSsMigrateProgress(pReq, len, &req) != 0) {
×
1247
    code = TSDB_CODE_INVALID_MSG;
×
1248
    goto _exit;
×
1249
  }
1250

1251
  code = vnodeFollowerSsMigrate(pVnode, &req);
×
1252

1253
_exit:
×
1254
  pRsp->code = code;
×
1255
  return code;
×
1256
}
1257

1258
extern int32_t vnodeKillSsMigrate(SVnode *pVnode, SVnodeKillSsMigrateReq *pReq);
1259

1260
static int32_t vnodeProcessKillSsMigrateReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1261
  int32_t          code = 0;
×
1262
  SVnodeKillSsMigrateReq req = {0};
×
1263

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

1270
  code = vnodeKillSsMigrate(pVnode, &req);
×
1271

1272
_exit:
×
1273
  pRsp->code = code;
×
1274
  return code;
×
1275
}
1276

1277
static int32_t vnodeProcessDropTtlTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1278
  int               ret = 0;
×
1279
  SVDropTtlTableReq ttlReq = {0};
×
1280
  if (tDeserializeSVDropTtlTableReq(pReq, len, &ttlReq) != 0) {
×
1281
    ret = TSDB_CODE_INVALID_MSG;
×
1282
    goto end;
×
1283
  }
1284

1285
  if (ttlReq.nUids != taosArrayGetSize(ttlReq.pTbUids)) {
×
1286
    ret = TSDB_CODE_INVALID_MSG;
×
1287
    goto end;
×
1288
  }
1289

1290
  if (ttlReq.nUids != 0) {
×
1291
    vInfo("vgId:%d, process drop ttl table request, time:%d, ntbUids:%d", pVnode->config.vgId, ttlReq.timestampSec,
×
1292
          ttlReq.nUids);
1293
  }
1294

1295
  if (ttlReq.nUids > 0) {
×
1296
    int32_t code = metaDropMultipleTables(pVnode->pMeta, ver, ttlReq.pTbUids);
×
1297
    if (code) return code;
×
1298

1299
    code = tqUpdateTbUidList(pVnode->pTq, ttlReq.pTbUids, false);
×
1300
    if (code) {
×
1301
      vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1302
    }
1303
  }
1304

1305
end:
×
1306
  taosArrayDestroy(ttlReq.pTbUids);
×
1307
  return ret;
×
1308
}
1309

1310
static int32_t vnodeProcessFetchTtlExpiredTbs(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
11,666✔
1311
  int32_t                 code = -1;
11,666✔
1312
  SMetaReader             mr = {0};
11,666✔
1313
  SVDropTtlTableReq       ttlReq = {0};
11,666✔
1314
  SVFetchTtlExpiredTbsRsp rsp = {0};
11,666✔
1315
  SEncoder                encoder = {0};
11,666✔
1316
  SArray                 *pNames = NULL;
11,666✔
1317
  pRsp->msgType = TDMT_VND_FETCH_TTL_EXPIRED_TBS_RSP;
11,666✔
1318
  pRsp->code = TSDB_CODE_SUCCESS;
11,666✔
1319
  pRsp->pCont = NULL;
11,666✔
1320
  pRsp->contLen = 0;
11,666✔
1321

1322
  if (tDeserializeSVDropTtlTableReq(pReq, len, &ttlReq) != 0) {
11,666!
1323
    terrno = TSDB_CODE_INVALID_MSG;
×
1324
    goto _end;
×
1325
  }
1326

1327
  if (!(ttlReq.nUids == taosArrayGetSize(ttlReq.pTbUids))) {
11,666!
1328
    terrno = TSDB_CODE_INVALID_MSG;
×
1329
    goto _end;
×
1330
  }
1331

1332
  tb_uid_t    suid;
1333
  char        ctbName[TSDB_TABLE_NAME_LEN];
1334
  SVDropTbReq expiredTb = {.igNotExists = true};
11,666✔
1335
  metaReaderDoInit(&mr, pVnode->pMeta, 0);
11,666✔
1336
  rsp.vgId = TD_VID(pVnode);
11,666✔
1337
  rsp.pExpiredTbs = taosArrayInit(ttlReq.nUids, sizeof(SVDropTbReq));
11,666✔
1338
  if (!rsp.pExpiredTbs) goto _end;
11,666!
1339

1340
  pNames = taosArrayInit(ttlReq.nUids, TSDB_TABLE_NAME_LEN);
11,666✔
1341
  if (!pNames) {
11,666!
1342
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1343
    goto _end;
×
1344
  }
1345
  char buf[TSDB_TABLE_NAME_LEN];
11,666✔
1346
  for (int32_t i = 0; i < ttlReq.nUids; ++i) {
59,428✔
1347
    tb_uid_t *uid = taosArrayGet(ttlReq.pTbUids, i);
47,762✔
1348
    expiredTb.suid = *uid;
47,762✔
1349
    terrno = metaReaderGetTableEntryByUid(&mr, *uid);
47,762✔
1350
    if (terrno < 0) goto _end;
47,762!
1351
    tstrncpy(buf, mr.me.name, TSDB_TABLE_NAME_LEN);
47,762!
1352
    void *p = taosArrayPush(pNames, buf);
47,762✔
1353
    if (p == NULL) {
47,762!
1354
      goto _end;
×
1355
    }
1356

1357
    expiredTb.name = p;
47,762✔
1358
    if (mr.me.type == TSDB_CHILD_TABLE) {
47,762✔
1359
      expiredTb.suid = mr.me.ctbEntry.suid;
29,184✔
1360
    }
1361

1362
    if (taosArrayPush(rsp.pExpiredTbs, &expiredTb) == NULL) {
95,524!
1363
      goto _end;
×
1364
    }
1365
  }
1366

1367
  int32_t ret = 0;
11,666✔
1368
  tEncodeSize(tEncodeVFetchTtlExpiredTbsRsp, &rsp, pRsp->contLen, ret);
11,666!
1369
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
11,666✔
1370
  if (pRsp->pCont == NULL) {
11,666!
1371
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1372
    code = -1;
×
1373
    goto _end;
×
1374
  }
1375
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
11,666✔
1376
  terrno = tEncodeVFetchTtlExpiredTbsRsp(&encoder, &rsp);
11,666✔
1377
  tEncoderClear(&encoder);
11,666✔
1378

1379
  if (terrno == 0) code = 0;
11,666!
1380
_end:
11,666✔
1381
  metaReaderClear(&mr);
11,666✔
1382
  tFreeFetchTtlExpiredTbsRsp(&rsp);
11,666✔
1383
  taosArrayDestroy(ttlReq.pTbUids);
11,666✔
1384
  if (pNames) taosArrayDestroy(pNames);
11,666!
1385
  pRsp->code = terrno;
11,666✔
1386
  return code;
11,666✔
1387
}
1388

1389
static int32_t vnodeProcessCreateStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
6,343,672✔
1390
  int32_t        code = 0;
6,343,672✔
1391
  SVCreateStbReq req = {0};
6,343,672✔
1392
  SDecoder       coder;
6,351,963✔
1393

1394
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
6,352,802✔
1395
  pRsp->code = TSDB_CODE_SUCCESS;
6,352,802✔
1396
  pRsp->pCont = NULL;
6,353,465✔
1397
  pRsp->contLen = 0;
6,352,801✔
1398

1399
  // decode and process req
1400
  tDecoderInit(&coder, pReq, len);
6,352,362✔
1401

1402
  code = tDecodeSVCreateStbReq(&coder, &req);
6,352,438✔
1403
  if (code) {
6,340,487!
1404
    pRsp->code = code;
×
1405
    goto _err;
×
1406
  }
1407

1408
  code = metaCreateSuperTable(pVnode->pMeta, ver, &req);
6,340,487✔
1409
  if (code) {
6,352,974✔
1410
    pRsp->code = code;
2,610✔
1411
    goto _err;
2,610✔
1412
  }
1413

1414
  tDecoderClear(&coder);
6,350,364✔
1415
  return 0;
6,351,385✔
1416

1417
_err:
2,610✔
1418
  tDecoderClear(&coder);
2,610✔
1419
  return code;
2,610✔
1420
}
1421

1422
static int32_t vnodeProcessCreateTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
30,108,001✔
1423
                                       SRpcMsg *pOriginRpc) {
1424
  SDecoder           decoder = {0};
30,108,001✔
1425
  SEncoder           encoder = {0};
30,108,413✔
1426
  int32_t            rcode = 0;
30,108,413✔
1427
  SVCreateTbBatchReq req = {0};
30,108,413✔
1428
  SVCreateTbReq     *pCreateReq;
1429
  SVCreateTbBatchRsp rsp = {0};
30,108,413✔
1430
  SVCreateTbRsp      cRsp = {0};
30,107,792✔
1431
  char               tbName[TSDB_TABLE_FNAME_LEN];
30,103,602✔
1432
  SArray            *tbUids = NULL;
30,108,413✔
1433
  SArray            *tbNames = NULL;
30,108,413✔
1434
  pRsp->msgType = TDMT_VND_CREATE_TABLE_RSP;
30,108,413✔
1435
  pRsp->code = TSDB_CODE_SUCCESS;
30,108,413✔
1436
  pRsp->pCont = NULL;
30,108,413✔
1437
  pRsp->contLen = 0;
30,108,413✔
1438

1439
  // decode
1440
  tDecoderInit(&decoder, pReq, len);
30,108,413✔
1441
  if (tDecodeSVCreateTbBatchReq(&decoder, &req) < 0) {
30,108,547!
1442
    rcode = -1;
×
1443
    terrno = TSDB_CODE_INVALID_MSG;
×
1444
    goto _exit;
×
1445
  }
1446

1447
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(cRsp));
30,104,888✔
1448
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
30,107,909✔
1449
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
30,107,006✔
1450
  if (rsp.pArray == NULL || tbUids == NULL || tbNames == NULL) {
30,106,515!
1451
    rcode = -1;
361✔
1452
    terrno = TSDB_CODE_OUT_OF_MEMORY;
361✔
1453
    goto _exit;
×
1454
  }
1455

1456
  // loop to create table
1457
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
64,423,029✔
1458
    pCreateReq = req.pReqs + iReq;
34,314,504✔
1459
    memset(&cRsp, 0, sizeof(cRsp));
34,313,413✔
1460

1461
    if (tsEnableAudit && tsEnableAuditCreateTable) {
34,313,413!
1462
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
34,316,192!
1463
      if (str == NULL) {
34,310,249!
1464
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1465
        rcode = -1;
×
1466
        goto _exit;
×
1467
      }
1468
      tstrncpy(str, pCreateReq->name, TSDB_TABLE_FNAME_LEN);
34,310,249!
1469
      if (taosArrayPush(tbNames, &str) == NULL) {
34,316,094!
1470
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1471
        rcode = -1;
×
1472
        goto _exit;
×
1473
      }
1474
    }
1475

1476
    // validate hash
1477
    (void)tsnprintf(tbName, TSDB_TABLE_FNAME_LEN, "%s.%s", pVnode->config.dbname, pCreateReq->name);
34,311,913✔
1478
    if (vnodeValidateTableHash(pVnode, tbName) < 0) {
34,315,290!
1479
      cRsp.code = TSDB_CODE_VND_HASH_MISMATCH;
×
1480
      if (taosArrayPush(rsp.pArray, &cRsp) == NULL) {
×
1481
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1482
        rcode = -1;
×
1483
        goto _exit;
×
1484
      }
1485
      vError("vgId:%d create-table:%s failed due to hash value mismatch", TD_VID(pVnode), tbName);
×
1486
      continue;
×
1487
    }
1488

1489
    // do create table
1490
    if (metaCreateTable2(pVnode->pMeta, ver, pCreateReq, &cRsp.pMeta) < 0) {
34,310,996✔
1491
      if (pCreateReq->flags & TD_CREATE_IF_NOT_EXISTS && terrno == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
49,360!
1492
        cRsp.code = TSDB_CODE_SUCCESS;
35,727✔
1493
      } else {
1494
        cRsp.code = terrno;
13,633✔
1495
      }
1496
    } else {
1497
      cRsp.code = TSDB_CODE_SUCCESS;
34,265,672✔
1498
      if (taosArrayPush(tbUids, &pCreateReq->uid) == NULL) {
68,533,049!
1499
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1500
        rcode = -1;
×
1501
        goto _exit;
×
1502
      }
1503
      vnodeUpdateMetaRsp(pVnode, cRsp.pMeta);
34,267,377✔
1504
    }
1505

1506
    if (taosArrayPush(rsp.pArray, &cRsp) == NULL) {
68,632,599!
1507
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1508
      rcode = -1;
×
1509
      goto _exit;
×
1510
    }
1511
  }
1512

1513
  vTrace("vgId:%d, add %d new created tables into query table list", TD_VID(pVnode), (int32_t)taosArrayGetSize(tbUids));
30,108,525✔
1514
  if (tqUpdateTbUidList(pVnode->pTq, tbUids, true) < 0) {
30,108,525!
1515
    vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(terrno));
×
1516
  }
1517

1518
  // prepare rsp
1519
  int32_t ret = 0;
30,108,547✔
1520
  tEncodeSize(tEncodeSVCreateTbBatchRsp, &rsp, pRsp->contLen, ret);
30,108,547!
1521
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
30,105,860✔
1522
  if (pRsp->pCont == NULL) {
30,107,660!
1523
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1524
    rcode = -1;
×
1525
    goto _exit;
×
1526
  }
1527
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
30,107,287✔
1528
  if (tEncodeSVCreateTbBatchRsp(&encoder, &rsp) != 0) {
30,106,691!
1529
    vError("vgId:%d, failed to encode create table batch response", TD_VID(pVnode));
×
1530
  }
1531

1532
  if (tsEnableAudit && tsEnableAuditCreateTable) {
30,106,416!
1533
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
30,107,354✔
1534

1535
    SName name = {0};
30,106,932✔
1536
    if (tNameFromString(&name, pVnode->config.dbname, T_NAME_ACCT | T_NAME_DB) < 0) {
30,107,411!
1537
      vError("vgId:%d, failed to get name from string", TD_VID(pVnode));
×
1538
    }
1539

1540
    SStringBuilder sb = {0};
30,106,029✔
1541
    for (int32_t i = 0; i < tbNames->size; i++) {
64,421,053✔
1542
      char **key = (char **)taosArrayGet(tbNames, i);
34,314,038✔
1543
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
34,315,940!
1544
      if (i < tbNames->size - 1) {
34,314,107✔
1545
        taosStringBuilderAppendChar(&sb, ',');
4,208,190✔
1546
      }
1547
      // taosMemoryFreeClear(*key);
1548
    }
1549

1550
    size_t len = 0;
30,106,106✔
1551
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
30,106,331✔
1552

1553
    if (pOriginRpc->info.conn.user != NULL && strlen(pOriginRpc->info.conn.user) > 0) {
30,106,234!
1554
      auditAddRecord(pOriginRpc, clusterId, "createTable", name.dbname, "", keyJoined, len);
28,310,800✔
1555
    }
1556

1557
    taosStringBuilderDestroy(&sb);
30,100,420✔
1558
  }
1559

1560
_exit:
30,102,041✔
1561
  tDeleteSVCreateTbBatchReq(&req);
30,105,793✔
1562
  taosArrayDestroyEx(rsp.pArray, tFreeSVCreateTbRsp);
30,105,507✔
1563
  taosArrayDestroy(tbUids);
30,104,709✔
1564
  tDecoderClear(&decoder);
30,101,304✔
1565
  tEncoderClear(&encoder);
30,106,690✔
1566
  taosArrayDestroyP(tbNames, NULL);
30,106,893✔
1567
  return rcode;
30,105,302✔
1568
}
1569

1570
static int32_t vnodeProcessAlterStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
4,937,099✔
1571
  int32_t        code = 0;
4,937,099✔
1572
  SVCreateStbReq req = {0};
4,937,099✔
1573
  SDecoder       dc = {0};
4,937,099✔
1574

1575
  pRsp->msgType = TDMT_VND_ALTER_STB_RSP;
4,937,099✔
1576
  pRsp->code = TSDB_CODE_SUCCESS;
4,937,099✔
1577
  pRsp->pCont = NULL;
4,936,997✔
1578
  pRsp->contLen = 0;
4,937,099✔
1579

1580
  tDecoderInit(&dc, pReq, len);
4,937,099✔
1581

1582
  // decode req
1583
  code = tDecodeSVCreateStbReq(&dc, &req);
4,936,997✔
1584
  if (code) {
4,926,007!
1585
    tDecoderClear(&dc);
×
1586
    return code;
×
1587
  }
1588

1589
  code = metaAlterSuperTable(pVnode->pMeta, ver, &req);
4,926,007✔
1590
  if (code) {
4,937,099!
1591
    pRsp->code = code;
×
1592
    tDecoderClear(&dc);
×
1593
    return code;
×
1594
  }
1595

1596
  tDecoderClear(&dc);
4,937,099✔
1597

1598
  return 0;
4,937,099✔
1599
}
1600

1601
static int32_t vnodeProcessDropStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp, SRpcMsg *pOriginRpc) {
1,202,783✔
1602
  SVDropStbReq req = {0};
1,202,783✔
1603
  int32_t      rcode = TSDB_CODE_SUCCESS;
1,202,783✔
1604
  SDecoder     decoder = {0};
1,202,783✔
1605
  SArray      *tbUidList = NULL;
1,202,783✔
1606

1607
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
1,202,783✔
1608
  pRsp->pCont = NULL;
1,202,783✔
1609
  pRsp->contLen = 0;
1,202,783✔
1610

1611
  // decode request
1612
  tDecoderInit(&decoder, pReq, len);
1,202,783✔
1613
  if (tDecodeSVDropStbReq(&decoder, &req) < 0) {
1,202,783!
1614
    rcode = TSDB_CODE_INVALID_MSG;
×
1615
    goto _exit;
×
1616
  }
1617

1618
  STraceId* trace = &(pOriginRpc->info.traceId);
1,200,566✔
1619

1620
  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,200,566!
1621
              trace ? trace->msgId : 0, req.name);
1622

1623
  // process request
1624
  tbUidList = taosArrayInit(8, sizeof(int64_t));
1,202,046✔
1625
  if (tbUidList == NULL) goto _exit;
1,202,783!
1626
  if (metaDropSuperTable(pVnode->pMeta, ver, &req) < 0) {
1,202,783✔
1627
    rcode = terrno;
1,740✔
1628
    goto _exit;
1,740✔
1629
  }
1630

1631
  if (tqUpdateTbUidList(pVnode->pTq, tbUidList, false) < 0) {
1,201,043!
1632
    rcode = terrno;
×
1633
    goto _exit;
×
1634
  }
1635

1636
  // return rsp
1637
_exit:
1,202,783✔
1638
  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,202,783!
1639
              trace ? trace->msgId : 0, req.name);
1640
  if (tbUidList) taosArrayDestroy(tbUidList);
1,202,783!
1641
  pRsp->code = rcode;
1,202,783✔
1642
  tDecoderClear(&decoder);
1,202,783✔
1643
  return 0;
1,202,783✔
1644
}
1645

1646
static int32_t vnodeProcessAlterTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
7,885,150✔
1647
  SVAlterTbReq  vAlterTbReq = {0};
7,885,150✔
1648
  SVAlterTbRsp  vAlterTbRsp = {0};
7,885,150✔
1649
  SDecoder      dc = {0};
7,885,150✔
1650
  int32_t       code = 0;
7,885,150✔
1651
  int32_t       lino = 0;
7,885,150✔
1652
  int32_t       ret;
1653
  SEncoder      ec = {0};
7,885,150✔
1654
  STableMetaRsp vMetaRsp = {0};
7,885,150✔
1655

1656
  pRsp->msgType = TDMT_VND_ALTER_TABLE_RSP;
7,885,150✔
1657
  pRsp->pCont = NULL;
7,885,150✔
1658
  pRsp->contLen = 0;
7,885,150✔
1659
  pRsp->code = TSDB_CODE_SUCCESS;
7,885,150✔
1660

1661
  tDecoderInit(&dc, pReq, len);
7,885,150✔
1662

1663
  // decode
1664
  if (tDecodeSVAlterTbReq(&dc, &vAlterTbReq) < 0) {
7,885,150!
1665
    vAlterTbRsp.code = TSDB_CODE_INVALID_MSG;
×
1666
    tDecoderClear(&dc);
×
1667
    goto _exit;
×
1668
  }
1669

1670
  // process
1671
  if (metaAlterTable(pVnode->pMeta, ver, &vAlterTbReq, &vMetaRsp) < 0) {
7,885,150✔
1672
    vAlterTbRsp.code = terrno;
618,977✔
1673
    tDecoderClear(&dc);
618,977✔
1674
    goto _exit;
618,977✔
1675
  }
1676
  tDecoderClear(&dc);
7,266,173✔
1677

1678
  if (NULL != vMetaRsp.pSchemas) {
7,266,173✔
1679
    vnodeUpdateMetaRsp(pVnode, &vMetaRsp);
4,323,109✔
1680
    vAlterTbRsp.pMeta = &vMetaRsp;
4,323,109✔
1681
  }
1682

1683
  if (vAlterTbReq.action == TSDB_ALTER_TABLE_UPDATE_TAG_VAL ||
7,266,173✔
1684
      vAlterTbReq.action == TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL) {
4,355,943✔
1685
    int64_t uid = metaGetTableEntryUidByName(pVnode->pMeta, vAlterTbReq.tbName);
2,914,130✔
1686
    if (uid == 0) {
2,914,130!
1687
      vError("vgId:%d, %s failed at %s:%d since table %s not found", TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
1688
             vAlterTbReq.tbName);
1689
      goto _exit;
×
1690
    }
1691

1692
    SArray *tbUids = taosArrayInit(4, sizeof(int64_t));
2,914,130✔
1693
    void   *p = taosArrayPush(tbUids, &uid);
2,914,130✔
1694
    TSDB_CHECK_NULL(p, code, lino, _exit, terrno);
2,914,130!
1695

1696
    vDebug("vgId:%d, remove tags value altered table:%s from query table list", TD_VID(pVnode), vAlterTbReq.tbName);
2,914,130✔
1697
    if ((code = tqUpdateTbUidList(pVnode->pTq, tbUids, false)) < 0) {
2,914,130!
1698
      vError("vgId:%d, failed to remove tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1699
    }
1700

1701
    vDebug("vgId:%d, try to add table:%s in query table list", TD_VID(pVnode), vAlterTbReq.tbName);
2,914,130✔
1702
    if ((code = tqUpdateTbUidList(pVnode->pTq, tbUids, true)) < 0) {
2,914,130!
1703
      vError("vgId:%d, failed to add tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1704
    }
1705

1706
    taosArrayDestroy(tbUids);
2,914,130✔
1707
  }
1708

1709
_exit:
7,885,150✔
1710
  taosArrayDestroy(vAlterTbReq.pMultiTag);
7,885,150✔
1711
  tEncodeSize(tEncodeSVAlterTbRsp, &vAlterTbRsp, pRsp->contLen, ret);
7,885,150!
1712
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
7,885,150✔
1713
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
7,885,150✔
1714
  if (tEncodeSVAlterTbRsp(&ec, &vAlterTbRsp) != 0) {
7,885,150!
1715
    vError("vgId:%d, failed to encode alter table response", TD_VID(pVnode));
×
1716
  }
1717

1718
  tEncoderClear(&ec);
7,885,150✔
1719
  if (vMetaRsp.pSchemas) {
7,885,150✔
1720
    taosMemoryFree(vMetaRsp.pSchemas);
4,323,109!
1721
    taosMemoryFree(vMetaRsp.pSchemaExt);
4,323,109!
1722
  }
1723
  if (vMetaRsp.pColRefs) {
7,885,150✔
1724
    taosMemoryFree(vMetaRsp.pColRefs);
323,498!
1725
  }
1726
  return 0;
7,885,150✔
1727
}
1728

1729
static int32_t vnodeProcessDropTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
8,602,556✔
1730
                                     SRpcMsg *pOriginRpc) {
1731
  SVDropTbBatchReq req = {0};
8,602,556✔
1732
  SVDropTbBatchRsp rsp = {0};
8,602,556✔
1733
  SDecoder         decoder = {0};
8,602,556✔
1734
  SEncoder         encoder = {0};
8,602,556✔
1735
  int32_t          ret;
1736
  SArray          *tbUids = NULL;
8,602,556✔
1737
  SArray          *tbNames = NULL;
8,602,556✔
1738

1739
  pRsp->msgType = ((SRpcMsg *)pReq)->msgType + 1;
8,602,556✔
1740
  pRsp->pCont = NULL;
8,602,556✔
1741
  pRsp->contLen = 0;
8,602,556✔
1742
  pRsp->code = TSDB_CODE_SUCCESS;
8,602,556✔
1743

1744
  // decode req
1745
  tDecoderInit(&decoder, pReq, len);
8,602,556✔
1746
  ret = tDecodeSVDropTbBatchReq(&decoder, &req);
8,602,556✔
1747
  if (ret < 0) {
8,602,556!
1748
    terrno = TSDB_CODE_INVALID_MSG;
×
1749
    pRsp->code = terrno;
×
1750
    goto _exit;
×
1751
  }
1752

1753
  // process req
1754
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
8,602,556✔
1755
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(SVDropTbRsp));
8,602,556✔
1756
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
8,602,556✔
1757
  if (tbUids == NULL || rsp.pArray == NULL || tbNames == NULL) goto _exit;
8,602,556!
1758

1759
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
17,387,010✔
1760
    SVDropTbReq *pDropTbReq = req.pReqs + iReq;
8,784,454✔
1761
    SVDropTbRsp  dropTbRsp = {0};
8,784,454✔
1762
    tb_uid_t     tbUid = 0;
8,784,454✔
1763

1764
    /* code */
1765
    ret = metaDropTable2(pVnode->pMeta, ver, pDropTbReq);
8,784,454✔
1766
    if (ret < 0) {
8,784,454!
1767
      if (pDropTbReq->igNotExists && terrno == TSDB_CODE_TDB_TABLE_NOT_EXIST) {
×
1768
        dropTbRsp.code = TSDB_CODE_SUCCESS;
×
1769
      } else {
1770
        dropTbRsp.code = terrno;
×
1771
      }
1772
    } else {
1773
      dropTbRsp.code = TSDB_CODE_SUCCESS;
8,784,454✔
1774
    }
1775

1776
    if (taosArrayPush(rsp.pArray, &dropTbRsp) == NULL) {
17,568,908!
1777
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1778
      pRsp->code = terrno;
×
1779
      goto _exit;
×
1780
    }
1781

1782
    if (tsEnableAuditCreateTable) {
8,784,454!
1783
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
8,784,454!
1784
      if (str == NULL) {
8,784,454!
1785
        pRsp->code = terrno;
×
1786
        goto _exit;
×
1787
      }
1788
      tstrncpy(str, pDropTbReq->name, TSDB_TABLE_FNAME_LEN);
8,784,454!
1789
      if (taosArrayPush(tbNames, &str) == NULL) {
8,784,454!
1790
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1791
        pRsp->code = terrno;
×
1792
        goto _exit;
×
1793
      }
1794
    }
1795
  }
1796

1797
  if (tqUpdateTbUidList(pVnode->pTq, tbUids, false) < 0) {
8,602,556!
1798
    vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(terrno));
×
1799
  }
1800

1801
  if (tsEnableAuditCreateTable) {
8,602,556!
1802
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
8,602,556✔
1803

1804
    SName name = {0};
8,602,556✔
1805
    if (tNameFromString(&name, pVnode->config.dbname, T_NAME_ACCT | T_NAME_DB) != 0) {
8,602,556!
1806
      vError("vgId:%d, failed to get name from string", TD_VID(pVnode));
×
1807
    }
1808

1809
    SStringBuilder sb = {0};
8,602,556✔
1810
    for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
17,387,010✔
1811
      char **key = (char **)taosArrayGet(tbNames, iReq);
8,784,454✔
1812
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
8,784,454!
1813
      if (iReq < req.nReqs - 1) {
8,784,454✔
1814
        taosStringBuilderAppendChar(&sb, ',');
181,898✔
1815
      }
1816
      taosMemoryFreeClear(*key);
8,784,454!
1817
    }
1818

1819
    size_t len = 0;
8,602,556✔
1820
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
8,602,556✔
1821

1822
    if (pOriginRpc->info.conn.user != NULL && strlen(pOriginRpc->info.conn.user) > 0) {
8,602,530!
1823
      auditAddRecord(pOriginRpc, clusterId, "dropTable", name.dbname, "", keyJoined, len);
8,602,091✔
1824
    }
1825

1826
    taosStringBuilderDestroy(&sb);
8,602,504✔
1827
  }
1828

1829
_exit:
8,602,556✔
1830
  taosArrayDestroy(tbUids);
8,602,530✔
1831
  tDecoderClear(&decoder);
8,602,530✔
1832
  tEncodeSize(tEncodeSVDropTbBatchRsp, &rsp, pRsp->contLen, ret);
8,602,530!
1833
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
8,602,530✔
1834
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
8,602,530✔
1835
  if (tEncodeSVDropTbBatchRsp(&encoder, &rsp) != 0) {
8,602,556!
1836
    vError("vgId:%d, failed to encode drop table batch response", TD_VID(pVnode));
×
1837
  }
1838
  tEncoderClear(&encoder);
8,602,556✔
1839
  taosArrayDestroy(rsp.pArray);
8,602,556✔
1840
  taosArrayDestroy(tbNames);
8,602,530✔
1841
  return 0;
8,602,530✔
1842
}
1843

1844
static int32_t vnodeProcessCreateRsmaReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
13,972✔
1845
  int32_t         code = 0, lino = 0;
13,972✔
1846
  SVCreateRsmaReq req = {0};
13,972✔
1847
  TAOS_CHECK_EXIT(tDeserializeSVCreateRsmaReq(pReq, len, &req));
13,972!
1848
  TAOS_CHECK_EXIT(metaCreateRsma(pVnode->pMeta, ver, &req));
13,972!
1849
_exit:
13,473✔
1850
  pRsp->msgType = TDMT_VND_CREATE_RSMA_RSP;
13,473✔
1851
  pRsp->pCont = NULL;
13,473✔
1852
  pRsp->code = code;
13,473✔
1853
  pRsp->contLen = 0;
13,473✔
1854
  tFreeSVCreateRsmaReq(&req);
13,473✔
1855
  return code;
13,473✔
1856
}
1857

1858
static int32_t vnodeProcessDropRsmaReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
2,994✔
1859
  int32_t       code = 0, lino = 0;
2,994✔
1860
  SVDropRsmaReq req = {0};
2,994✔
1861
  TAOS_CHECK_EXIT(tDeserializeSVDropRsmaReq(pReq, len, &req));
2,994!
1862
  TAOS_CHECK_EXIT(metaDropRsma(pVnode->pMeta, ver, &req));
2,994!
1863
_exit:
2,994✔
1864
  pRsp->msgType = TDMT_VND_DROP_RSMA_RSP;
2,994✔
1865
  pRsp->pCont = NULL;
2,994✔
1866
  pRsp->code = code;
2,994✔
1867
  pRsp->contLen = 0;
2,994✔
1868
  return 0;
2,994✔
1869
}
1870

1871
static int32_t vnodeProcessAlterRsmaReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
6,986✔
1872
  int32_t         code = 0, lino = 0;
6,986✔
1873
  SVAlterRsmaReq req = {0};
6,986✔
1874
  TAOS_CHECK_EXIT(tDeserializeSVAlterRsmaReq(pReq, len, &req));
6,986!
1875
  TAOS_CHECK_EXIT(metaAlterRsma(pVnode->pMeta, ver, &req));
6,986!
1876
_exit:
6,986✔
1877
  pRsp->msgType = TDMT_VND_ALTER_RSMA_RSP;
6,986✔
1878
  pRsp->pCont = NULL;
6,986✔
1879
  pRsp->code = code;
6,986✔
1880
  pRsp->contLen = 0;
6,986✔
1881
  tFreeSVAlterRsmaReq(&req);
6,986✔
1882
  return code;
6,986✔
1883
}
1884

1885
#ifdef BUILD_NO_CALL
1886
static int32_t vnodeDebugPrintSingleSubmitMsg(SMeta *pMeta, SSubmitBlk *pBlock, SSubmitMsgIter *msgIter,
1887
                                              const char *tags) {
1888
  SSubmitBlkIter blkIter = {0};
1889
  STSchema      *pSchema = NULL;
1890
  tb_uid_t       suid = 0;
1891
  STSRow        *row = NULL;
1892
  int32_t        rv = -1;
1893

1894
  tInitSubmitBlkIter(msgIter, pBlock, &blkIter);
1895
  if (blkIter.row == NULL) return 0;
1896

1897
  int32_t code = metaGetTbTSchemaNotNull(pMeta, msgIter->suid, TD_ROW_SVER(blkIter.row), 1,
1898
                                         &pSchema);  // TODO: use the real schema
1899
  if (TSDB_CODE_SUCCESS != code) {
1900
    printf("%s:%d no valid schema\n", tags, __LINE__);
1901
    return code;
1902
  }
1903

1904
  suid = msgIter->suid;
1905
  rv = TD_ROW_SVER(blkIter.row);
1906

1907
  char __tags[128] = {0};
1908
  snprintf(__tags, 128, "%s: uid %" PRIi64 " ", tags, msgIter->uid);
1909
  while ((row = tGetSubmitBlkNext(&blkIter))) {
1910
    tdSRowPrint(row, pSchema, __tags);
1911
  }
1912

1913
  taosMemoryFreeClear(pSchema);
1914

1915
  return TSDB_CODE_SUCCESS;
1916
}
1917
#endif
1918
typedef struct SSubmitReqConvertCxt {
1919
  SSubmitMsgIter msgIter;
1920
  SSubmitBlk    *pBlock;
1921
  SSubmitBlkIter blkIter;
1922
  STSRow        *pRow;
1923
  STSRowIter     rowIter;
1924
  SSubmitTbData *pTbData;
1925
  STSchema      *pTbSchema;
1926
  SArray        *pColValues;
1927
} SSubmitReqConvertCxt;
1928

1929
static int32_t vnodeResetTableCxt(SMeta *pMeta, SSubmitReqConvertCxt *pCxt) {
×
1930
  taosMemoryFreeClear(pCxt->pTbSchema);
×
1931
  int32_t code = metaGetTbTSchemaNotNull(pMeta, pCxt->msgIter.suid > 0 ? pCxt->msgIter.suid : pCxt->msgIter.uid,
×
1932
                                         pCxt->msgIter.sversion, 1, &pCxt->pTbSchema);
1933
  if (TSDB_CODE_SUCCESS != code) {
×
1934
    return code;
×
1935
  }
1936
  tdSTSRowIterInit(&pCxt->rowIter, pCxt->pTbSchema);
×
1937

1938
  tDestroySubmitTbData(pCxt->pTbData, TSDB_MSG_FLG_ENCODE);
×
1939
  if (NULL == pCxt->pTbData) {
×
1940
    pCxt->pTbData = taosMemoryCalloc(1, sizeof(SSubmitTbData));
×
1941
    if (NULL == pCxt->pTbData) {
×
1942
      return terrno;
×
1943
    }
1944
  }
1945
  pCxt->pTbData->flags = 0;
×
1946
  pCxt->pTbData->suid = pCxt->msgIter.suid;
×
1947
  pCxt->pTbData->uid = pCxt->msgIter.uid;
×
1948
  pCxt->pTbData->sver = pCxt->msgIter.sversion;
×
1949
  pCxt->pTbData->pCreateTbReq = NULL;
×
1950
  pCxt->pTbData->aRowP = taosArrayInit(128, POINTER_BYTES);
×
1951
  if (NULL == pCxt->pTbData->aRowP) {
×
1952
    return terrno;
×
1953
  }
1954

1955
  taosArrayDestroy(pCxt->pColValues);
×
1956
  pCxt->pColValues = taosArrayInit(pCxt->pTbSchema->numOfCols, sizeof(SColVal));
×
1957
  if (NULL == pCxt->pColValues) {
×
1958
    return terrno;
×
1959
  }
1960
  for (int32_t i = 0; i < pCxt->pTbSchema->numOfCols; ++i) {
×
1961
    SColVal val = COL_VAL_NONE(pCxt->pTbSchema->columns[i].colId, pCxt->pTbSchema->columns[i].type);
×
1962
    if (taosArrayPush(pCxt->pColValues, &val) == NULL) {
×
1963
      return terrno;
×
1964
    }
1965
  }
1966

1967
  return TSDB_CODE_SUCCESS;
×
1968
}
1969

1970
static void vnodeDestroySubmitReqConvertCxt(SSubmitReqConvertCxt *pCxt) {
×
1971
  taosMemoryFreeClear(pCxt->pTbSchema);
×
1972
  tDestroySubmitTbData(pCxt->pTbData, TSDB_MSG_FLG_ENCODE);
×
1973
  taosMemoryFreeClear(pCxt->pTbData);
×
1974
  taosArrayDestroy(pCxt->pColValues);
×
1975
}
×
1976

1977
static int32_t vnodeCellValConvertToColVal(STColumn *pCol, SCellVal *pCellVal, SColVal *pColVal) {
×
1978
  if (tdValTypeIsNone(pCellVal->valType)) {
×
1979
    pColVal->flag = CV_FLAG_NONE;
×
1980
    return TSDB_CODE_SUCCESS;
×
1981
  }
1982

1983
  if (tdValTypeIsNull(pCellVal->valType)) {
×
1984
    pColVal->flag = CV_FLAG_NULL;
×
1985
    return TSDB_CODE_SUCCESS;
×
1986
  }
1987

1988
  if (IS_VAR_DATA_TYPE(pCol->type)) {
×
1989
    if (IS_STR_DATA_BLOB(pCol->type)) {
×
1990
      pColVal->value.nData = blobDataLen(pCellVal->val);
×
1991
      pColVal->value.pData = (uint8_t *)blobDataVal(pCellVal->val);
×
1992

1993
    } else {
1994
      pColVal->value.nData = varDataLen(pCellVal->val);
×
1995
      pColVal->value.pData = (uint8_t *)varDataVal(pCellVal->val);
×
1996
    }
1997
  } else if (TSDB_DATA_TYPE_FLOAT == pCol->type) {
×
1998
    float f = GET_FLOAT_VAL(pCellVal->val);
×
1999
    valueSetDatum(&pColVal->value, pCol->type, &f, sizeof(f));
×
2000
  } else if (TSDB_DATA_TYPE_DOUBLE == pCol->type) {
×
2001
    taosSetPInt64Aligned(&pColVal->value.val, (int64_t *)pCellVal->val);
×
2002
  } else {
2003
    valueSetDatum(&pColVal->value, pCol->type, pCellVal->val, tDataTypes[pCol->type].bytes);
×
2004
  }
2005

2006
  pColVal->flag = CV_FLAG_VALUE;
×
2007
  return TSDB_CODE_SUCCESS;
×
2008
}
2009

2010
static int32_t vnodeTSRowConvertToColValArray(SSubmitReqConvertCxt *pCxt) {
×
2011
  int32_t code = TSDB_CODE_SUCCESS;
×
2012
  tdSTSRowIterReset(&pCxt->rowIter, pCxt->pRow);
×
2013
  for (int32_t i = 0; TSDB_CODE_SUCCESS == code && i < pCxt->pTbSchema->numOfCols; ++i) {
×
2014
    STColumn *pCol = pCxt->pTbSchema->columns + i;
×
2015
    SCellVal  cellVal = {0};
×
2016
    if (!tdSTSRowIterFetch(&pCxt->rowIter, pCol->colId, pCol->type, &cellVal)) {
×
2017
      break;
×
2018
    }
2019
    code = vnodeCellValConvertToColVal(pCol, &cellVal, (SColVal *)taosArrayGet(pCxt->pColValues, i));
×
2020
  }
2021
  return code;
×
2022
}
2023

2024
static int32_t vnodeDecodeCreateTbReq(SSubmitReqConvertCxt *pCxt) {
×
2025
  if (pCxt->msgIter.schemaLen <= 0) {
×
2026
    return TSDB_CODE_SUCCESS;
×
2027
  }
2028

2029
  pCxt->pTbData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
×
2030
  if (NULL == pCxt->pTbData->pCreateTbReq) {
×
2031
    return terrno;
×
2032
  }
2033

2034
  SDecoder decoder = {0};
×
2035
  tDecoderInit(&decoder, (uint8_t *)pCxt->pBlock->data, pCxt->msgIter.schemaLen);
×
2036
  int32_t code = tDecodeSVCreateTbReq(&decoder, pCxt->pTbData->pCreateTbReq);
×
2037
  tDecoderClear(&decoder);
×
2038

2039
  return code;
×
2040
}
2041

2042
static int32_t vnodeSubmitReqConvertToSubmitReq2(SVnode *pVnode, SSubmitReq *pReq, SSubmitReq2 *pReq2) {
×
2043
  pReq2->aSubmitTbData = taosArrayInit(128, sizeof(SSubmitTbData));
×
2044
  if (NULL == pReq2->aSubmitTbData) {
×
2045
    return terrno;
×
2046
  }
2047

2048
  SSubmitReqConvertCxt cxt = {0};
×
2049

2050
  int32_t code = tInitSubmitMsgIter(pReq, &cxt.msgIter);
×
2051
  while (TSDB_CODE_SUCCESS == code) {
×
2052
    code = tGetSubmitMsgNext(&cxt.msgIter, &cxt.pBlock);
×
2053
    if (TSDB_CODE_SUCCESS == code) {
×
2054
      if (NULL == cxt.pBlock) {
×
2055
        break;
×
2056
      }
2057
      code = vnodeResetTableCxt(pVnode->pMeta, &cxt);
×
2058
    }
2059
    if (TSDB_CODE_SUCCESS == code) {
×
2060
      code = tInitSubmitBlkIter(&cxt.msgIter, cxt.pBlock, &cxt.blkIter);
×
2061
    }
2062
    if (TSDB_CODE_SUCCESS == code) {
×
2063
      code = vnodeDecodeCreateTbReq(&cxt);
×
2064
    }
2065
    while (TSDB_CODE_SUCCESS == code && (cxt.pRow = tGetSubmitBlkNext(&cxt.blkIter)) != NULL) {
×
2066
      code = vnodeTSRowConvertToColValArray(&cxt);
×
2067
      if (TSDB_CODE_SUCCESS == code) {
×
2068
        SRow **pNewRow = taosArrayReserve(cxt.pTbData->aRowP, 1);
×
2069

2070
        SRowBuildScanInfo sinfo = {0};
×
2071
        code = tRowBuild(cxt.pColValues, cxt.pTbSchema, pNewRow, &sinfo);
×
2072
      }
2073
    }
2074
    if (TSDB_CODE_SUCCESS == code) {
×
2075
      code = (NULL == taosArrayPush(pReq2->aSubmitTbData, cxt.pTbData) ? terrno : TSDB_CODE_SUCCESS);
×
2076
    }
2077
    if (TSDB_CODE_SUCCESS == code) {
×
2078
      taosMemoryFreeClear(cxt.pTbData);
×
2079
    }
2080
  }
2081

2082
  vnodeDestroySubmitReqConvertCxt(&cxt);
×
2083
  return code;
×
2084
}
2085

2086
static int32_t vnodeRebuildSubmitReqMsg(SSubmitReq2 *pSubmitReq, void **ppMsg) {
×
2087
  int32_t  code = TSDB_CODE_SUCCESS;
×
2088
  char    *pMsg = NULL;
×
2089
  uint32_t msglen = 0;
×
2090
  tEncodeSize(tEncodeSubmitReq, pSubmitReq, msglen, code);
×
2091
  if (TSDB_CODE_SUCCESS == code) {
×
2092
    pMsg = taosMemoryMalloc(msglen);
×
2093
    if (NULL == pMsg) {
×
2094
      code = terrno;
×
2095
    }
2096
  }
2097
  if (TSDB_CODE_SUCCESS == code) {
×
2098
    SEncoder encoder;
×
2099
    tEncoderInit(&encoder, (uint8_t *)pMsg, msglen);
×
2100
    code = tEncodeSubmitReq(&encoder, pSubmitReq);
×
2101
    tEncoderClear(&encoder);
×
2102
  }
2103
  if (TSDB_CODE_SUCCESS == code) {
×
2104
    *ppMsg = pMsg;
×
2105
  }
2106
  return code;
×
2107
}
2108

2109
static int32_t buildExistSubTalbeRsp(SVnode *pVnode, SSubmitTbData *pSubmitTbData, STableMetaRsp **ppRsp) {
127,034✔
2110
  int32_t code = 0;
127,034✔
2111
  int32_t lino = 0;
127,034✔
2112

2113
  SMetaEntry *pEntry = NULL;
127,034✔
2114
  code = metaFetchEntryByUid(pVnode->pMeta, pSubmitTbData->suid, &pEntry);
127,034✔
2115
  if (code) {
127,034!
2116
    vError("vgId:%d, table uid:%" PRId64 " not exists, line:%d", TD_VID(pVnode), pSubmitTbData->uid, __LINE__);
×
2117
    TSDB_CHECK_CODE(code, lino, _exit);
×
2118
  }
2119
  if (pEntry->type != TSDB_SUPER_TABLE) {
127,034!
2120
    vError("vgId:%d, table uid:%" PRId64 " exists, but is not super table, line:%d", TD_VID(pVnode), pSubmitTbData->uid,
×
2121
           __LINE__);
2122
    code = TSDB_CODE_STREAM_INSERT_SCHEMA_NOT_MATCH;
×
2123
    TSDB_CHECK_CODE(code, lino, _exit);
×
2124
  }
2125

2126
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
127,034!
2127
  if (NULL == *ppRsp) {
127,034!
2128
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2129
  }
2130
  (*ppRsp)->suid = pSubmitTbData->suid;
127,034✔
2131
  (*ppRsp)->tuid = pSubmitTbData->uid;
127,034✔
2132
  (*ppRsp)->sversion = pEntry->stbEntry.schemaRow.version;
127,034✔
2133
  (*ppRsp)->vgId = pVnode->config.vgId;
127,034✔
2134
  (*ppRsp)->numOfColumns = pEntry->stbEntry.schemaRow.nCols;
127,034✔
2135
  (*ppRsp)->numOfTags = pEntry->stbEntry.schemaTag.nCols;
127,034✔
2136
  (*ppRsp)->pSchemas =
127,034✔
2137
      taosMemoryCalloc(pEntry->stbEntry.schemaRow.nCols + pEntry->stbEntry.schemaTag.nCols, sizeof(SSchema));
127,034!
2138
  if (NULL == (*ppRsp)->pSchemas) {
127,034!
2139
    taosMemoryFree(*ppRsp);
×
2140
    *ppRsp = NULL;
×
2141
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2142
  }
2143
  memcpy((*ppRsp)->pSchemas, pEntry->stbEntry.schemaRow.pSchema, pEntry->stbEntry.schemaRow.nCols * sizeof(SSchema));
127,034!
2144
  memcpy((*ppRsp)->pSchemas + pEntry->stbEntry.schemaRow.nCols, pEntry->stbEntry.schemaTag.pSchema,
127,034!
2145
         pEntry->stbEntry.schemaTag.nCols * sizeof(SSchema));
127,034✔
2146
  if (pEntry->pExtSchemas != NULL) {
127,034!
2147
    (*ppRsp)->pSchemaExt = taosMemoryCalloc(pEntry->colCmpr.nCols, sizeof(SExtSchema));
×
2148
    if (NULL == (*ppRsp)->pSchemaExt) {
×
2149
      taosMemoryFree((*ppRsp)->pSchemas);
×
2150
      taosMemoryFree(*ppRsp);
×
2151
      *ppRsp = NULL;
×
2152
      TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2153
    }
2154
    memcpy((*ppRsp)->pSchemaExt, pEntry->pExtSchemas, pEntry->colCmpr.nCols * sizeof(SExtSchema));
×
2155
  }
2156

2157
  if (pEntry->stbEntry.schemaRow.version != pSubmitTbData->sver) {
127,034✔
2158
    TSDB_CHECK_CODE(code = TSDB_CODE_TDB_TABLE_ALREADY_EXIST, lino, _exit);
1,070✔
2159
  }
2160
_exit:
127,034✔
2161
  metaFetchEntryFree(&pEntry);
127,034✔
2162
  if (code != TSDB_CODE_SUCCESS) {
127,034✔
2163
    vError("vgId:%d, failed to build exist sub table response, code:0x%0x, line:%d", TD_VID(pVnode), code, lino);
1,070!
2164
  }
2165
  return code;
127,034✔
2166
}
2167

2168
static int32_t buildExistNormalTalbeRsp(SVnode *pVnode, int64_t uid, STableMetaRsp **ppRsp) {
56,957✔
2169
  int32_t code = 0;
56,957✔
2170
  int32_t lino = 0;
56,957✔
2171

2172
  SMetaEntry *pEntry = NULL;
56,957✔
2173
  code = metaFetchEntryByUid(pVnode->pMeta, uid, &pEntry);
56,957✔
2174
  if (code) {
56,957!
2175
    vError("vgId:%d, table uid:%" PRId64 " not exists, line:%d", TD_VID(pVnode), uid, __LINE__);
×
2176
    TSDB_CHECK_CODE(code, lino, _exit);
×
2177
  }
2178
  if (pEntry->type != TSDB_NORMAL_TABLE) {
56,957!
2179
    vError("vgId:%d, table uid:%" PRId64 " exists, but is not normal table, line:%d", TD_VID(pVnode), uid, __LINE__);
×
2180
    code = TSDB_CODE_STREAM_INSERT_SCHEMA_NOT_MATCH;
×
2181
    TSDB_CHECK_CODE(code, lino, _exit);
×
2182
  }
2183

2184
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
56,957!
2185
  if (NULL == *ppRsp) {
56,957!
2186
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2187
  }
2188

2189
  (*ppRsp)->tuid = pEntry->uid;
56,957✔
2190
  (*ppRsp)->vgId = pVnode->config.vgId;
56,957✔
2191
  (*ppRsp)->sversion = pEntry->ntbEntry.schemaRow.version;
56,957✔
2192
  (*ppRsp)->numOfColumns = pEntry->ntbEntry.schemaRow.nCols;
56,957✔
2193
  (*ppRsp)->pSchemas = taosMemoryCalloc(pEntry->ntbEntry.schemaRow.nCols, sizeof(SSchema));
56,957!
2194
  if (NULL == (*ppRsp)->pSchemas) {
56,957!
2195
    taosMemoryFree(*ppRsp);
×
2196
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2197
  }
2198
  memcpy((*ppRsp)->pSchemas, pEntry->ntbEntry.schemaRow.pSchema, pEntry->ntbEntry.schemaRow.nCols * sizeof(SSchema));
56,957!
2199
  if (pEntry->pExtSchemas != NULL) {
56,957!
2200
    (*ppRsp)->pSchemaExt = taosMemoryCalloc(pEntry->ntbEntry.schemaRow.nCols, sizeof(SSchemaExt));
×
2201
    if (NULL == (*ppRsp)->pSchemaExt) {
×
2202
      taosMemoryFree((*ppRsp)->pSchemas);
×
2203
      taosMemoryFree(*ppRsp);
×
2204
      TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2205
    }
2206
    memcpy((*ppRsp)->pSchemaExt, pEntry->pExtSchemas, pEntry->ntbEntry.schemaRow.nCols * sizeof(SSchemaExt));
×
2207
  }
2208

2209
_exit:
56,957✔
2210
  metaFetchEntryFree(&pEntry);
56,957✔
2211
  if (code != TSDB_CODE_SUCCESS) {
56,957!
2212
    vError("vgId:%d, failed to build exist normal table response, code:%d, line:%d", TD_VID(pVnode), code, lino);
×
2213
  }
2214
  return code;
56,957✔
2215
}
2216

2217
static int32_t buildExistTableInStreamRsp(SVnode *pVnode, SSubmitTbData *pSubmitTbData, STableMetaRsp **ppRsp) {
833,238✔
2218
  if (pSubmitTbData->pCreateTbReq->flags & TD_CREATE_NORMAL_TB_IN_STREAM) {
833,238✔
2219
    int32_t code = buildExistNormalTalbeRsp(pVnode, pSubmitTbData->uid, ppRsp);
56,957✔
2220
    if (code) {
56,957!
2221
      vError("vgId:%d, table uid:%" PRId64 " not exists, line:%d", TD_VID(pVnode), pSubmitTbData->uid, __LINE__);
×
2222
      return code;
×
2223
    }
2224
    return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
56,957✔
2225
  } else if (pSubmitTbData->pCreateTbReq->flags & TD_CREATE_SUB_TB_IN_STREAM) {
776,281✔
2226
    return buildExistSubTalbeRsp(pVnode, pSubmitTbData, ppRsp);
127,034✔
2227
  }
2228
  return TSDB_CODE_SUCCESS;
649,247✔
2229
}
2230

2231
static int32_t vnodeHandleAutoCreateTable(SVnode      *pVnode,    // vnode
548,749,409✔
2232
                                          int64_t      version,   // version
2233
                                          SSubmitReq2 *pRequest,  // request
2234
                                          SSubmitRsp2 *pResponse  // response
2235
) {
2236
  int32_t code = TSDB_CODE_SUCCESS;
548,749,409✔
2237
  int32_t numTbData = taosArrayGetSize(pRequest->aSubmitTbData);
548,749,409✔
2238
  SArray *newTbUids = NULL;
548,749,000✔
2239

2240
  for (int32_t i = 0; i < numTbData; ++i) {
1,136,901,253✔
2241
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
588,210,790✔
2242

2243
    if (pTbData->pCreateTbReq == NULL) {
588,211,328✔
2244
      continue;
563,079,666✔
2245
    }
2246

2247
    pTbData->uid = pTbData->pCreateTbReq->uid;
25,130,690✔
2248

2249
    // Alloc necessary resources
2250
    if (pResponse->aCreateTbRsp == NULL) {
25,131,404✔
2251
      pResponse->aCreateTbRsp = taosArrayInit(numTbData, sizeof(SVCreateTbRsp));
18,787,773✔
2252
      if (pResponse->aCreateTbRsp == NULL) {
18,787,773!
2253
        code = terrno;
×
2254
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2255
               tstrerror(code), version);
2256
        taosArrayDestroy(newTbUids);
×
2257
        return code;
×
2258
      }
2259
    }
2260

2261
    // Do create table
2262
    vDebug("vgId:%d start to handle auto create table, version:%" PRId64, TD_VID(pVnode), version);
25,131,404✔
2263

2264
    SVCreateTbRsp *pCreateTbRsp = taosArrayReserve(pResponse->aCreateTbRsp, 1);
25,130,380✔
2265
    code = metaCreateTable2(pVnode->pMeta, version, pTbData->pCreateTbReq, &pCreateTbRsp->pMeta);
25,132,118✔
2266
    if (code == TSDB_CODE_SUCCESS) {
25,131,714✔
2267
      // Allocate necessary resources
2268
      if (newTbUids == NULL) {
23,850,909✔
2269
        newTbUids = taosArrayInit(numTbData, sizeof(int64_t));
17,954,554✔
2270
        if (newTbUids == NULL) {
17,954,519!
2271
          code = terrno;
×
2272
          vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2273
                 tstrerror(code), version);
2274
          return code;
×
2275
        }
2276
      }
2277

2278
      if (taosArrayPush(newTbUids, &pTbData->uid) == NULL) {
47,701,783!
2279
        code = terrno;
×
2280
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2281
               tstrerror(code), version);
2282
        taosArrayDestroy(newTbUids);
×
2283
        return code;
×
2284
      }
2285

2286
      if (pCreateTbRsp->pMeta) {
23,850,909!
2287
        vnodeUpdateMetaRsp(pVnode, pCreateTbRsp->pMeta);
23,850,909✔
2288
      }
2289
    } else if (code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
1,280,805✔
2290
      code = terrno = 0;
1,280,343✔
2291
      pTbData->uid = pTbData->pCreateTbReq->uid;  // update uid if table exist for using below
1,280,343✔
2292

2293
      // stream: get sver from meta, write to pCreateTbRsp, and need to check crateTbReq is same as meta.
2294
      if (i == 0) {
1,280,343✔
2295
        // In the streaming scenario, multiple grouped req requests will only operate on the same write table, and
2296
        // only the first one needs to be processed.
2297
        code = buildExistTableInStreamRsp(pVnode, pTbData, &pCreateTbRsp->pMeta);
833,238✔
2298
        if (code) {
833,238✔
2299
          vInfo("vgId:%d failed to create table in stream:%s, code(0x%0x):%s", TD_VID(pVnode),
58,027!
2300
                pTbData->pCreateTbReq->name, code, tstrerror(code));
2301
          taosArrayDestroy(newTbUids);
58,027✔
2302
          return code;
58,027✔
2303
        }
2304
      }
2305
    } else {
2306
      code = terrno;
462✔
2307
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
462!
2308
             tstrerror(code), version);
2309
      taosArrayDestroy(newTbUids);
462✔
2310
      return code;
462✔
2311
    }
2312
  }
2313

2314
  // Update the affected table uid list
2315
  if (taosArrayGetSize(newTbUids) > 0) {
548,690,463✔
2316
    vDebug("vgId:%d, add %d table into query table list in handling submit", TD_VID(pVnode),
17,954,554✔
2317
           (int32_t)taosArrayGetSize(newTbUids));
2318
    if (tqUpdateTbUidList(pVnode->pTq, newTbUids, true) != 0) {
17,954,624!
2319
      vError("vgId:%d, failed to update tbUid list", TD_VID(pVnode));
×
2320
    }
2321
  }
2322

2323
  vDebug("vgId:%d, handle auto create table done, version:%" PRId64, TD_VID(pVnode), version);
548,690,920✔
2324

2325
  taosArrayDestroy(newTbUids);
548,690,096✔
2326
  return code;
548,690,925✔
2327
}
2328

2329
static void addExistTableInfoIntoRes(SVnode *pVnode, SSubmitReq2 *pRequest, SSubmitRsp2 *pResponse,
1,070✔
2330
                                     SSubmitTbData *pTbData, int32_t numTbData) {
2331
  int32_t code = 0;
1,070✔
2332
  int32_t lino = 0;
1,070✔
2333
  if ((pTbData->flags & SUBMIT_REQ_SCHEMA_RES) == 0) {
1,070!
2334
    return;
×
2335
  }
2336
  if (pResponse->aCreateTbRsp) {  // If aSubmitTbData is not NULL, it means that the request is a create table request,
1,070!
2337
                                  // so table info has exitst and we do not need to add again.
2338
    return;
1,070✔
2339
  }
2340
  pResponse->aCreateTbRsp = taosArrayInit(numTbData, sizeof(SVCreateTbRsp));
×
2341
  if (pResponse->aCreateTbRsp == NULL) {
×
2342
    code = terrno;
×
2343
    TSDB_CHECK_CODE(code, lino, _exit);
×
2344
  }
2345
  SVCreateTbRsp *pCreateTbRsp = taosArrayReserve(pResponse->aCreateTbRsp, 1);
×
2346
  if (pCreateTbRsp == NULL) {
×
2347
    code = terrno;
×
2348
    TSDB_CHECK_CODE(code, lino, _exit);
×
2349
  }
2350
  if (pTbData->suid == 0) {
×
2351
    code = buildExistNormalTalbeRsp(pVnode, pTbData->uid, &pCreateTbRsp->pMeta);
×
2352
    if (code) {
×
2353
      vError("vgId:%d, table uid:%" PRId64 " not exists, line:%d", TD_VID(pVnode), pTbData->uid, __LINE__);
×
2354
    }
2355
  } else {
2356
    code = buildExistSubTalbeRsp(pVnode, pTbData, &pCreateTbRsp->pMeta);
×
2357
  }
2358

2359
  TSDB_CHECK_CODE(code, lino, _exit);
×
2360
_exit:
×
2361
  if (code != TSDB_CODE_SUCCESS) {
×
2362
    vError("vgId:%d, failed to add exist table info into response, code:0x%0x, line:%d", TD_VID(pVnode), code, lino);
×
2363
  }
2364
  return;
×
2365
}
2366

2367
static int32_t vnodeHandleDataWrite(SVnode *pVnode, int64_t version, SSubmitReq2 *pRequest, SSubmitRsp2 *pResponse) {
548,690,920✔
2368
  int32_t code = TSDB_CODE_SUCCESS;
548,690,920✔
2369
  int32_t numTbData = taosArrayGetSize(pRequest->aSubmitTbData);
548,690,920✔
2370
  int8_t  hasBlob = 0;
548,689,609✔
2371

2372
  // Scan submit data
2373
  for (int32_t i = 0; i < numTbData; ++i) {
1,136,839,325✔
2374
    SMetaInfo      info = {0};
588,151,739✔
2375
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
588,152,242✔
2376

2377
    if (pTbData->flags & SUBMIT_REQ_WITH_BLOB) {
588,151,873✔
2378
      hasBlob = 1;
40,020✔
2379
    }
2380
    if (pTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
588,152,376✔
2381
      continue;  // skip column data format
713,007✔
2382
    }
2383

2384
    code = metaGetInfo(pVnode->pMeta, pTbData->uid, &info, NULL);
587,439,240✔
2385
    if (code) {
587,440,025✔
2386
      code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
2,140✔
2387
      vWarn("vgId:%d, error occurred at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __FILE__,
2,140!
2388
            __LINE__, tstrerror(code), version, pTbData->uid);
2389
      return code;
2,140✔
2390
    }
2391

2392
    if (info.suid != pTbData->suid) {
587,437,885!
2393
      code = TSDB_CODE_INVALID_MSG;
×
2394
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64 " suid:%" PRId64
×
2395
             " info.suid:%" PRId64,
2396
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), version, pTbData->uid, pTbData->suid,
2397
             info.suid);
2398
      return code;
×
2399
    }
2400

2401
    if (info.suid) {
587,437,387✔
2402
      code = metaGetInfo(pVnode->pMeta, info.suid, &info, NULL);
441,295,005✔
2403
      if (code) {
441,295,629!
2404
        code = TSDB_CODE_INTERNAL_ERROR;
×
2405
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " suid:%" PRId64, TD_VID(pVnode), __func__,
×
2406
               __FILE__, __LINE__, tstrerror(code), version, info.suid);
2407
        return code;
×
2408
      }
2409
    }
2410

2411
    if (pTbData->sver != info.skmVer) {
587,438,011✔
2412
      code = TSDB_CODE_TDB_INVALID_TABLE_SCHEMA_VER;
1,070✔
2413
      addExistTableInfoIntoRes(pVnode, pRequest, pResponse, pTbData, numTbData);
1,070✔
2414
      vDebug("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64
1,070!
2415
             " sver:%d"
2416
             " info.skmVer:%d",
2417
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), version, pTbData->uid, pTbData->sver,
2418
             info.skmVer);
2419
      return code;
1,070✔
2420
    }
2421
  }
2422

2423
  // Do write data
2424
  vDebug("vgId:%d start to handle data write, version:%" PRId64, TD_VID(pVnode), version);
548,687,586✔
2425

2426
  for (int32_t i = 0; i < numTbData; ++i) {
1,136,823,345✔
2427
    int32_t        affectedRows = 0;
588,148,286✔
2428
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
588,148,286✔
2429

2430
    if (hasBlob) {
588,147,648✔
2431
      code = vnodeSubmitBlobData(pVnode, pTbData);
40,020✔
2432
      if (code) {
40,020!
2433
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2434
               __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2435
        return code;
×
2436
      }
2437
    }
2438

2439
    code = tsdbInsertTableData(pVnode->pTsdb, version, pTbData, &affectedRows);
588,147,648✔
2440
    if (code) {
588,129,267!
2441
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2442
             __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2443
      return code;
×
2444
    }
2445

2446
    code = metaUpdateChangeTimeWithLock(pVnode->pMeta, pTbData->uid, pTbData->ctimeMs);
588,129,267✔
2447
    if (code) {
588,129,325!
2448
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2449
             __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2450
      return code;
×
2451
    }
2452
    pResponse->affectedRows += affectedRows;
588,129,325✔
2453
  }
2454

2455
  vDebug("vgId:%d, handle data write done, version:%" PRId64 ", affectedRows:%d", TD_VID(pVnode), version,
548,675,059✔
2456
         pResponse->affectedRows);
2457
  return code;
548,688,102✔
2458
}
2459

2460
static int32_t vnodeScanColumnData(SVnode *pVnode, SSubmitTbData *pTbData, TSKEY minKey, TSKEY maxKey) {
712,937✔
2461
  int32_t code = 0;
712,937✔
2462

2463
  int32_t   numCols = taosArrayGetSize(pTbData->aCol);
712,937✔
2464
  SColData *aColData = (SColData *)TARRAY_DATA(pTbData->aCol);
712,972✔
2465

2466
  if (numCols <= 0) {
712,937!
2467
    code = TSDB_CODE_INVALID_MSG;
×
2468
    vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " numCols:%d", TD_VID(pVnode), __func__,
×
2469
           __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, numCols);
2470
    return code;
×
2471
  }
2472

2473
  if (aColData[0].cid != PRIMARYKEY_TIMESTAMP_COL_ID || aColData[0].type != TSDB_DATA_TYPE_TIMESTAMP ||
712,937!
2474
      aColData[0].nVal <= 0) {
712,937✔
2475
    code = TSDB_CODE_INVALID_MSG;
70✔
2476
    vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64
70!
2477
           " first column is not primary key timestamp, cid:%d type:%d nVal:%d",
2478
           TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, aColData[0].cid,
2479
           aColData[0].type, aColData[0].nVal);
2480
    return code;
×
2481
  }
2482

2483
  for (int32_t i = 1; i < numCols; ++i) {
3,731,608✔
2484
    if (aColData[i].nVal != aColData[0].nVal) {
3,018,636!
2485
      code = TSDB_CODE_INVALID_MSG;
×
2486
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64
×
2487
             " column cid:%d type:%d nVal:%d is not equal to primary key timestamp nVal:%d",
2488
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid,
2489
             aColData[i].cid, aColData[i].type, aColData[i].nVal, aColData[0].nVal);
2490
      return code;
×
2491
    }
2492
  }
2493

2494
  SRowKey *pLastKey = NULL;
712,972✔
2495
  SRowKey  lastKey = {0};
712,972✔
2496
  for (int32_t i = 0; i < aColData[0].nVal; ++i) {
366,732,641✔
2497
    SRowKey key = {0};
366,510,556✔
2498

2499
    tColDataArrGetRowKey(aColData, numCols, i, &key);
365,993,094✔
2500

2501
    if (key.ts < minKey || key.ts > maxKey) {
366,212,962!
2502
      code = TSDB_CODE_INVALID_MSG;
×
2503
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] key:%" PRId64
×
2504
             " is out of range [%" PRId64 ", %" PRId64 "]",
2505
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, i, key.ts,
2506
             minKey, maxKey);
2507
      return code;
×
2508
    }
2509

2510
    if (pLastKey && tRowKeyCompare(pLastKey, &key) >= 0) {
366,634,887!
2511
      code = TSDB_CODE_INVALID_MSG;
×
2512
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] key:%" PRId64
×
2513
             " is not in order, lastKey:%" PRId64,
2514
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, i, key.ts,
2515
             pLastKey->ts);
2516
      return code;
×
2517
    } else if (pLastKey == NULL) {
366,486,872✔
2518
      pLastKey = &lastKey;
712,867✔
2519
    }
2520

2521
    *pLastKey = key;
366,486,872✔
2522
  }
2523

2524
  return code;
713,007✔
2525
}
2526

2527
static int32_t vnodeScanSubmitRowData(SVnode *pVnode, SSubmitTbData *pTbData, TSKEY minKey, TSKEY maxKey) {
587,488,514✔
2528
  int32_t code = 0;
587,488,514✔
2529

2530
  int32_t numRows = taosArrayGetSize(pTbData->aRowP);
587,488,514✔
2531
  SRow  **aRow = (SRow **)TARRAY_DATA(pTbData->aRowP);
587,486,183✔
2532

2533
  if (numRows <= 0) {
587,478,265!
2534
    code = TSDB_CODE_INVALID_MSG;
×
2535
    vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " numRows:%d", TD_VID(pVnode), __func__,
×
2536
           __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, numRows);
2537
    return code;
×
2538
  }
2539

2540
  SRowKey *pLastKey = NULL;
587,478,265✔
2541
  SRowKey  lastKey = {0};
587,478,265✔
2542
  for (int32_t i = 0; i < numRows; ++i) {
2,147,483,647✔
2543
    SRow *pRow = aRow[i];
2,147,483,647✔
2544
    if (pRow->sver != pTbData->sver) {
2,147,483,647!
2545
      code = TSDB_CODE_INVALID_MSG;
×
2546
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] sver:%d pTbData->sver:%d",
×
2547
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, i, pRow->sver,
2548
             pTbData->sver);
2549
      return code;
×
2550
    }
2551

2552
    SRowKey key = {0};
2,147,483,647✔
2553
    tRowGetKey(pRow, &key);
2,147,483,647✔
2554
    if (key.ts < minKey || key.ts > maxKey) {
2,147,483,647!
2555
      code = TSDB_CODE_INVALID_MSG;
324✔
2556
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] key:%" PRId64
324!
2557
             " is out of range [%" PRId64 ", %" PRId64 "]",
2558
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, i, key.ts,
2559
             minKey, maxKey);
2560
      return code;
×
2561
    }
2562

2563
    if (pLastKey && tRowKeyCompare(pLastKey, &key) >= 0) {
2,147,483,647!
2564
      code = TSDB_CODE_INVALID_MSG;
×
2565
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] key:%" PRId64
×
2566
             " is not in order, lastKey:%" PRId64,
2567
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, i, key.ts,
2568
             pLastKey->ts);
2569
      return code;
×
2570
    } else if (pLastKey == NULL) {
2,147,483,647✔
2571
      pLastKey = &lastKey;
587,476,150✔
2572
    }
2573

2574
    *pLastKey = key;
2,147,483,647✔
2575
  }
2576

2577
  return code;
587,483,845✔
2578
}
2579

2580
static int32_t vnodeScanSubmitReq(SVnode *pVnode, int64_t version, SSubmitReq2 *pRequest, SSubmitRsp2 *pResponse) {
548,741,271✔
2581
  int32_t code = TSDB_CODE_SUCCESS;
548,741,271✔
2582
  int32_t numTbData = taosArrayGetSize(pRequest->aSubmitTbData);
548,741,271✔
2583

2584
  TSKEY now = taosGetTimestamp(pVnode->config.tsdbCfg.precision);
548,737,911✔
2585
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * pVnode->config.tsdbCfg.keep2;
548,741,682✔
2586
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
548,740,223✔
2587
  for (int32_t i = 0; i < numTbData; i++) {
1,136,935,624✔
2588
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
588,201,495✔
2589

2590
    if (pTbData->pCreateTbReq && pTbData->pCreateTbReq->uid == 0) {
588,205,556!
2591
      code = TSDB_CODE_INVALID_MSG;
×
2592
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2593
             tstrerror(code), version);
2594
      return code;
×
2595
    }
2596

2597
    if (pTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
588,205,856✔
2598
      code = vnodeScanColumnData(pVnode, pTbData, minKey, maxKey);
712,972✔
2599
      if (code) {
713,007!
2600
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2601
               __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2602
        return code;
×
2603
      }
2604
    } else {
2605
      code = vnodeScanSubmitRowData(pVnode, pTbData, minKey, maxKey);
587,486,638✔
2606
      if (code) {
587,485,846✔
2607
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
3,166!
2608
               __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2609
        return code;
×
2610
      }
2611
    }
2612
  }
2613

2614
  return code;
548,734,129✔
2615
}
2616

2617
static int32_t vnodeProcessSubmitReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
548,748,191✔
2618
                                     SRpcMsg *pOriginalMsg) {
2619
  int32_t code = 0;
548,748,191✔
2620
  int32_t lino = 0;
548,748,191✔
2621
  terrno = 0;
548,748,191✔
2622

2623
  SSubmitReq2 *pSubmitReq = &(SSubmitReq2){0};
548,749,264✔
2624
  SSubmitRsp2 *pSubmitRsp = &(SSubmitRsp2){0};
548,749,006✔
2625
  int32_t      ret;
2626
  SEncoder     ec = {0};
548,748,614✔
2627

2628
  pRsp->code = TSDB_CODE_SUCCESS;
548,748,748✔
2629

2630
  void           *pAllocMsg = NULL;
548,747,141✔
2631
  SSubmitReq2Msg *pMsg = (SSubmitReq2Msg *)pReq;
548,747,141✔
2632
  SDecoder        dc = {0};
548,747,141✔
2633
  if (0 == taosHton64(pMsg->version)) {
548,747,801!
2634
    code = vnodeSubmitReqConvertToSubmitReq2(pVnode, (SSubmitReq *)pMsg, pSubmitReq);
×
2635
    if (TSDB_CODE_SUCCESS == code) {
×
2636
      code = vnodeRebuildSubmitReqMsg(pSubmitReq, &pReq);
×
2637
    }
2638
    if (TSDB_CODE_SUCCESS == code) {
×
2639
      pAllocMsg = pReq;
×
2640
    }
2641
    TSDB_CHECK_CODE(code, lino, _exit);
×
2642
  } else {
2643
    // decode
2644
    pReq = POINTER_SHIFT(pReq, sizeof(SSubmitReq2Msg));
548,749,005✔
2645
    len -= sizeof(SSubmitReq2Msg);
548,749,263✔
2646

2647
    tDecoderInit(&dc, pReq, len);
548,749,263✔
2648
    if (tDecodeSubmitReq(&dc, pSubmitReq, NULL) < 0) {
548,749,409!
2649
      code = TSDB_CODE_INVALID_MSG;
×
2650
      TSDB_CHECK_CODE(code, lino, _exit);
×
2651
    }
2652
  }
2653

2654
  // Scan the request
2655
  code = vnodeScanSubmitReq(pVnode, ver, pSubmitReq, pSubmitRsp);
548,744,072✔
2656
  if (code) {
548,733,789!
2657
    vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2658
           tstrerror(code), ver);
2659
    TSDB_CHECK_CODE(code, lino, _exit);
×
2660
  }
2661

2662
  vGDebug(pOriginalMsg ? &pOriginalMsg->info.traceId : NULL, "vgId:%d, index:%" PRId64 ", submit block, rows:%d",
548,733,789!
2663
          TD_VID(pVnode), ver, (int32_t)taosArrayGetSize(pSubmitReq->aSubmitTbData));
2664

2665
  // Handle auto create table
2666
  code = vnodeHandleAutoCreateTable(pVnode, ver, pSubmitReq, pSubmitRsp);
548,743,395✔
2667
  if (code) {
548,749,379✔
2668
    vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
58,489!
2669
           tstrerror(code), ver);
2670
    TSDB_CHECK_CODE(code, lino, _exit);
58,489!
2671
  }
2672

2673
  // Handle data write
2674
  code = vnodeHandleDataWrite(pVnode, ver, pSubmitReq, pSubmitRsp);
548,690,890✔
2675
  if (code) {
548,691,312✔
2676
    vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
3,210!
2677
           tstrerror(code), ver);
2678
    TSDB_CHECK_CODE(code, lino, _exit);
3,210!
2679
  }
2680

2681
_exit:
548,748,527✔
2682
  // message
2683
  pRsp->code = code;
548,749,801✔
2684
  tEncodeSize(tEncodeSSubmitRsp2, pSubmitRsp, pRsp->contLen, ret);
548,749,801!
2685
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
548,749,667✔
2686
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
548,749,364✔
2687
  if (tEncodeSSubmitRsp2(&ec, pSubmitRsp) < 0) {
548,749,357!
2688
    vError("vgId:%d, failed to encode submit response", TD_VID(pVnode));
×
2689
  }
2690
  tEncoderClear(&ec);
548,747,363✔
2691

2692
  // update statistics
2693
  (void)atomic_add_fetch_64(&pVnode->statis.nInsert, pSubmitRsp->affectedRows);
548,747,328✔
2694
  (void)atomic_add_fetch_64(&pVnode->statis.nInsertSuccess, pSubmitRsp->affectedRows);
548,748,243✔
2695
  (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsert, 1);
548,748,679✔
2696

2697
  // update metrics
2698
  METRICS_UPDATE(pVnode->writeMetrics.total_requests, METRIC_LEVEL_LOW, 1);
548,747,051!
2699
  METRICS_UPDATE(pVnode->writeMetrics.total_rows, METRIC_LEVEL_HIGH, pSubmitRsp->affectedRows);
548,744,131!
2700
  METRICS_UPDATE(pVnode->writeMetrics.total_bytes, METRIC_LEVEL_LOW, pMsg->header.contLen);
548,744,449!
2701

2702
  if (tsEnableMonitor && tsMonitorFqdn[0] != 0 && tsMonitorPort != 0 && pSubmitRsp->affectedRows > 0 &&
548,743,391!
2703
      strlen(pOriginalMsg->info.conn.user) > 0 && tsInsertCounter != NULL) {
×
2704
    const char *sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT_AFFECTED_ROWS,
×
2705
                                   pVnode->monitor.strClusterId,
×
2706
                                   pVnode->monitor.strDnodeId,
×
2707
                                   tsLocalEp,
2708
                                   pVnode->monitor.strVgId,
×
2709
                                   pOriginalMsg->info.conn.user,
×
2710
                                   "Success"};
2711
    int         tv = taos_counter_add(tsInsertCounter, pSubmitRsp->affectedRows, sample_labels);
×
2712
  }
2713

2714
  if (code == 0) {
548,744,583✔
2715
    (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
548,682,884✔
2716
  }
2717

2718
  // clear
2719
  tDestroySubmitReq(pSubmitReq, 0 == taosHton64(pMsg->version) ? TSDB_MSG_FLG_CMPT : TSDB_MSG_FLG_DECODE);
548,745,989!
2720
  tDestroySSubmitRsp2(pSubmitRsp, TSDB_MSG_FLG_ENCODE);
548,740,600✔
2721

2722
  if (code) {
548,733,289✔
2723
    terrno = code;
61,699✔
2724
    if (code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
61,699✔
2725
      vInfo("vgId:%d, failed to process submit request since %s, lino:%d, version:%" PRId64, TD_VID(pVnode),
58,027!
2726
            tstrerror(code), lino, ver);
2727

2728
    } else {
2729
      vError("vgId:%d, failed to process submit request since %s, lino:%d, version:%" PRId64, TD_VID(pVnode),
3,672!
2730
             tstrerror(code), lino, ver);
2731
    }
2732
  }
2733

2734
  taosMemoryFree(pAllocMsg);
548,738,099!
2735
  tDecoderClear(&dc);
548,738,541✔
2736

2737
  return code;
548,741,682✔
2738
}
2739

2740
static int32_t vnodeConsolidateAlterHashRange(SVnode *pVnode, int64_t ver) {
7,051✔
2741
  int32_t code = TSDB_CODE_SUCCESS;
7,051✔
2742

2743
  vInfo("vgId:%d, trim meta of tables per hash range [%" PRIu32 ", %" PRIu32 "]. apply-index:%" PRId64, TD_VID(pVnode),
7,051!
2744
        pVnode->config.hashBegin, pVnode->config.hashEnd, ver);
2745

2746
  // TODO: trim meta of tables from TDB per hash range [pVnode->config.hashBegin, pVnode->config.hashEnd]
2747
  code = metaTrimTables(pVnode->pMeta, ver);
7,051✔
2748

2749
  return code;
7,051✔
2750
}
2751

2752
static int32_t vnodeProcessAlterConfirmReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
4,504,830✔
2753
  vInfo("vgId:%d, vnode handle msgType:alter-confirm, alter confirm msg is processed", TD_VID(pVnode));
4,504,830!
2754
  int32_t code = TSDB_CODE_SUCCESS;
4,504,830✔
2755
  if (!pVnode->config.hashChange) {
4,504,830!
2756
    goto _exit;
4,497,779✔
2757
  }
2758

2759
  code = vnodeConsolidateAlterHashRange(pVnode, ver);
7,051✔
2760
  if (code < 0) {
7,051!
2761
    vError("vgId:%d, failed to consolidate alter hashrange since %s. version:%" PRId64, TD_VID(pVnode), terrstr(), ver);
×
2762
    goto _exit;
×
2763
  }
2764
  pVnode->config.hashChange = false;
7,051✔
2765

2766
_exit:
4,504,830✔
2767
  pRsp->msgType = TDMT_VND_ALTER_CONFIRM_RSP;
4,504,830✔
2768
  pRsp->code = code;
4,504,830✔
2769
  pRsp->pCont = NULL;
4,504,830✔
2770
  pRsp->contLen = 0;
4,504,830✔
2771

2772
  return code;
4,504,830✔
2773
}
2774

2775
extern int32_t tsdbDisableAndCancelAllBgTask(STsdb *pTsdb);
2776
extern void    tsdbEnableBgTask(STsdb *pTsdb);
2777

2778
static int32_t vnodeProcessAlterConfigReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
260,578✔
2779
  bool walChanged = false;
260,578✔
2780
  bool tsdbChanged = false;
260,578✔
2781

2782
  SAlterVnodeConfigReq req = {0};
260,578✔
2783
  if (tDeserializeSAlterVnodeConfigReq(pReq, len, &req) != 0) {
260,578!
2784
    terrno = TSDB_CODE_INVALID_MSG;
×
2785
    return TSDB_CODE_INVALID_MSG;
×
2786
  }
2787

2788
  vInfo("vgId:%d, start to alter vnode config, page:%d pageSize:%d buffer:%d szPage:%d szBuf:%" PRIu64
260,578!
2789
        " cacheLast:%d cacheLastSize:%d days:%d keep0:%d keep1:%d keep2:%d keepTimeOffset:%d ssKeepLocal:%d "
2790
        "ssCompact:%d fsync:%d level:%d "
2791
        "walRetentionPeriod:%d walRetentionSize:%d",
2792
        TD_VID(pVnode), req.pages, req.pageSize, req.buffer, req.pageSize * 1024, (uint64_t)req.buffer * 1024 * 1024,
2793
        req.cacheLast, req.cacheLastSize, req.daysPerFile, req.daysToKeep0, req.daysToKeep1, req.daysToKeep2,
2794
        req.keepTimeOffset, req.ssKeepLocal, req.ssCompact, req.walFsyncPeriod, req.walLevel, req.walRetentionPeriod,
2795
        req.walRetentionSize);
2796

2797
  if (pVnode->config.cacheLastSize != req.cacheLastSize) {
260,578✔
2798
    pVnode->config.cacheLastSize = req.cacheLastSize;
4,116✔
2799
    tsdbCacheSetCapacity(pVnode, (size_t)pVnode->config.cacheLastSize * 1024 * 1024);
4,116✔
2800
  }
2801

2802
  if (pVnode->config.szBuf != req.buffer * 1024LL * 1024LL) {
260,578✔
2803
    vInfo("vgId:%d, vnode buffer is changed from %" PRId64 " to %" PRId64, TD_VID(pVnode), pVnode->config.szBuf,
10,136!
2804
          (uint64_t)(req.buffer * 1024LL * 1024LL));
2805
    pVnode->config.szBuf = req.buffer * 1024LL * 1024LL;
10,136✔
2806
  }
2807

2808
  if (pVnode->config.szCache != req.pages) {
260,578✔
2809
    if ((terrno = metaAlterCache(pVnode->pMeta, req.pages)) < 0) {
4,980!
2810
      vError("vgId:%d, failed to change vnode pages from %d to %d failed since %s", TD_VID(pVnode),
×
2811
             pVnode->config.szCache, req.pages, tstrerror(terrno));
2812
      return terrno;
×
2813
    } else {
2814
      vInfo("vgId:%d, vnode pages is changed from %d to %d", TD_VID(pVnode), pVnode->config.szCache, req.pages);
4,980!
2815
      pVnode->config.szCache = req.pages;
4,980✔
2816
    }
2817
  }
2818

2819
  if (pVnode->config.cacheLast != req.cacheLast) {
260,578✔
2820
    pVnode->config.cacheLast = req.cacheLast;
90,480✔
2821
  }
2822

2823
  if (pVnode->config.walCfg.fsyncPeriod != req.walFsyncPeriod) {
260,578✔
2824
    pVnode->config.walCfg.fsyncPeriod = req.walFsyncPeriod;
48,084✔
2825
    walChanged = true;
48,084✔
2826
  }
2827

2828
  if (pVnode->config.walCfg.level != req.walLevel) {
260,578✔
2829
    if (pVnode->config.walCfg.level == 0) {
32,648✔
2830
      pVnode->config.walCfg.clearFiles = 1;
90✔
2831
    }
2832
    pVnode->config.walCfg.level = req.walLevel;
32,648✔
2833
    walChanged = true;
32,648✔
2834
  }
2835

2836
  if (pVnode->config.walCfg.retentionPeriod != req.walRetentionPeriod) {
260,578✔
2837
    pVnode->config.walCfg.retentionPeriod = req.walRetentionPeriod;
8,600✔
2838
    walChanged = true;
8,600✔
2839
  }
2840

2841
  if (pVnode->config.walCfg.retentionSize != req.walRetentionSize) {
260,578✔
2842
    pVnode->config.walCfg.retentionSize = req.walRetentionSize;
920✔
2843
    walChanged = true;
920✔
2844
  }
2845

2846
  if (pVnode->config.tsdbCfg.keep0 != req.daysToKeep0) {
260,578✔
2847
    pVnode->config.tsdbCfg.keep0 = req.daysToKeep0;
42,232✔
2848
    tsdbChanged = true;
42,232✔
2849
  }
2850

2851
  if (pVnode->config.tsdbCfg.keep1 != req.daysToKeep1) {
260,578✔
2852
    pVnode->config.tsdbCfg.keep1 = req.daysToKeep1;
42,232✔
2853
    tsdbChanged = true;
42,232✔
2854
  }
2855

2856
  if (pVnode->config.tsdbCfg.keep2 != req.daysToKeep2) {
260,578✔
2857
    pVnode->config.tsdbCfg.keep2 = req.daysToKeep2;
39,632✔
2858
    tsdbChanged = true;
39,632✔
2859
  }
2860

2861
  if (pVnode->config.tsdbCfg.keepTimeOffset != req.keepTimeOffset) {
260,578✔
2862
    pVnode->config.tsdbCfg.keepTimeOffset = req.keepTimeOffset;
6,820✔
2863
    tsdbChanged = true;
6,820✔
2864
  }
2865

2866
  if (req.sttTrigger != -1 && req.sttTrigger != pVnode->config.sttTrigger) {
260,578!
2867
    if (req.sttTrigger > 1 && pVnode->config.sttTrigger > 1) {
1,740!
2868
      pVnode->config.sttTrigger = req.sttTrigger;
920✔
2869
    } else {
2870
      vnodeAWait(&pVnode->commitTask);
820✔
2871

2872
      int32_t ret = tsdbDisableAndCancelAllBgTask(pVnode->pTsdb);
820✔
2873
      if (ret != 0) {
820!
2874
        vError("vgId:%d, failed to disable bg task since %s", TD_VID(pVnode), tstrerror(ERRNO));
×
2875
      }
2876

2877
      pVnode->config.sttTrigger = req.sttTrigger;
820✔
2878
      tsdbEnableBgTask(pVnode->pTsdb);
820✔
2879
    }
2880
  }
2881

2882
  if (req.minRows != -1 && req.minRows != pVnode->config.tsdbCfg.minRows) {
260,578!
2883
    pVnode->config.tsdbCfg.minRows = req.minRows;
920✔
2884
  }
2885

2886
  if (req.ssKeepLocal != -1 && req.ssKeepLocal != pVnode->config.ssKeepLocal) {
260,578!
2887
    pVnode->config.ssKeepLocal = req.ssKeepLocal;
920✔
2888
  }
2889
  if (req.ssCompact != -1 && req.ssCompact != pVnode->config.ssCompact) {
260,578!
2890
    pVnode->config.ssCompact = req.ssCompact;
920✔
2891
  }
2892

2893
  if (walChanged) {
260,578✔
2894
    if (walAlter(pVnode->pWal, &pVnode->config.walCfg) != 0) {
89,666!
2895
      vError("vgId:%d, failed to alter wal config since %s", TD_VID(pVnode), tstrerror(ERRNO));
×
2896
    }
2897
  }
2898

2899
  if (tsdbChanged) {
260,578✔
2900
    tsdbSetKeepCfg(pVnode->pTsdb, &pVnode->config.tsdbCfg);
54,704✔
2901
  }
2902

2903
  return 0;
260,578✔
2904
}
2905

2906
static int32_t vnodeProcessBatchDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
2907
  SBatchDeleteReq deleteReq;
×
2908
  SDecoder        decoder;
×
2909
  tDecoderInit(&decoder, pReq, len);
×
2910
  if (tDecodeSBatchDeleteReq(&decoder, &deleteReq) < 0) {
×
2911
    tDecoderClear(&decoder);
×
2912
    return terrno = TSDB_CODE_INVALID_MSG;
×
2913
  }
2914

2915
  SMetaReader mr = {0};
×
2916
  metaReaderDoInit(&mr, pVnode->pMeta, META_READER_NOLOCK);
×
2917
  STsdb *pTsdb = pVnode->pTsdb;
×
2918

2919
  int32_t sz = taosArrayGetSize(deleteReq.deleteReqs);
×
2920
  for (int32_t i = 0; i < sz; i++) {
×
2921
    SSingleDeleteReq *pOneReq = taosArrayGet(deleteReq.deleteReqs, i);
×
2922
    char             *name = pOneReq->tbname;
×
2923
    if (metaGetTableEntryByName(&mr, name) < 0) {
×
2924
      vDebug("vgId:%d, stream delete msg, skip since no table: %s", pVnode->config.vgId, name);
×
2925
      continue;
×
2926
    }
2927

2928
    int64_t uid = mr.me.uid;
×
2929

2930
    int32_t code = tsdbDeleteTableData(pTsdb, ver, deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
×
2931
    if (code < 0) {
×
2932
      terrno = code;
×
2933
      vError("vgId:%d, delete error since %s, suid:%" PRId64 ", uid:%" PRId64 ", start ts:%" PRId64 ", end ts:%" PRId64,
×
2934
             TD_VID(pVnode), terrstr(), deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
2935
    }
2936

2937
    if (deleteReq.level == 0) {
×
2938
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, deleteReq.ctimeMs);
×
2939
      if (code < 0) {
×
2940
        terrno = code;
×
2941
        vError("vgId:%d, update change time error since %s, suid:%" PRId64 ", uid:%" PRId64 ", start ts:%" PRId64
×
2942
               ", end ts:%" PRId64,
2943
               TD_VID(pVnode), terrstr(), deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
2944
      }
2945
    }
2946
    tDecoderClear(&mr.coder);
×
2947
  }
2948
  metaReaderClear(&mr);
×
2949
  taosArrayDestroy(deleteReq.deleteReqs);
×
2950
  return 0;
×
2951
}
2952

2953
static int32_t vnodeProcessDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
3,109,095✔
2954
                                     SRpcMsg *pOriginalMsg) {
2955
  int32_t     code = 0;
3,109,095✔
2956
  SDecoder   *pCoder = &(SDecoder){0};
3,109,095✔
2957
  SDeleteRes *pRes = &(SDeleteRes){0};
3,109,095✔
2958

2959
  pRsp->msgType = TDMT_VND_DELETE_RSP;
3,109,095✔
2960
  pRsp->pCont = NULL;
3,109,095✔
2961
  pRsp->contLen = 0;
3,109,095✔
2962
  pRsp->code = TSDB_CODE_SUCCESS;
3,109,095✔
2963

2964
  pRes->uidList = taosArrayInit(0, sizeof(tb_uid_t));
3,109,095✔
2965
  if (pRes->uidList == NULL) {
3,109,095!
2966
    code = terrno;
×
2967
    goto _err;
×
2968
  }
2969

2970
  tDecoderInit(pCoder, pReq, len);
3,109,095✔
2971
  code = tDecodeDeleteRes(pCoder, pRes);
3,109,095✔
2972
  if (code) goto _err;
3,109,095!
2973

2974
  if (pRes->affectedRows > 0) {
3,109,095✔
2975
    for (int32_t iUid = 0; iUid < taosArrayGetSize(pRes->uidList); iUid++) {
5,681,320✔
2976
      uint64_t uid = *(uint64_t *)taosArrayGet(pRes->uidList, iUid);
3,187,424✔
2977
      code = tsdbDeleteTableData(pVnode->pTsdb, ver, pRes->suid, uid, pRes->skey, pRes->ekey);
3,187,424✔
2978
      if (code) goto _err;
3,187,424!
2979
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, pRes->ctimeMs);
3,187,424✔
2980
      if (code) goto _err;
3,187,424!
2981
    }
2982
  }
2983

2984
  tDecoderClear(pCoder);
3,109,095✔
2985
  taosArrayDestroy(pRes->uidList);
3,109,095✔
2986

2987
  SVDeleteRsp rsp = {.affectedRows = pRes->affectedRows};
3,109,095✔
2988
  int32_t     ret = 0;
3,109,095✔
2989
  tEncodeSize(tEncodeSVDeleteRsp, &rsp, pRsp->contLen, ret);
3,109,095!
2990
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
3,109,095✔
2991
  SEncoder ec = {0};
3,109,095✔
2992
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
3,109,095✔
2993
  code = tEncodeSVDeleteRsp(&ec, &rsp);
3,109,095✔
2994
  if (code) goto _err;
3,109,095!
2995
  tEncoderClear(&ec);
3,109,095✔
2996
  return code;
3,109,095✔
2997

2998
_err:
×
2999
  /*
3000
  if(code == TSDB_CODE_SUCCESS){
3001
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_DELETE, pVnode->monitor.strClusterId,
3002
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
3003
                                        pOriginalMsg->info.conn.user, "Success"};
3004
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
3005
  }
3006
  else{
3007
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_DELETE, pVnode->monitor.strClusterId,
3008
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
3009
                                        pOriginalMsg->info.conn.user, "Failed"};
3010
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
3011
  }
3012
  */
3013

3014
  return code;
×
3015
}
3016
static int32_t vnodeProcessCreateIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
7,153✔
3017
  SVCreateStbReq req = {0};
7,153✔
3018
  SDecoder       dc = {0};
7,153✔
3019
  int32_t        code = 0;
7,153✔
3020

3021
  pRsp->msgType = TDMT_VND_CREATE_INDEX_RSP;
7,153✔
3022
  pRsp->code = TSDB_CODE_SUCCESS;
7,153✔
3023
  pRsp->pCont = NULL;
7,153✔
3024
  pRsp->contLen = 0;
7,153✔
3025

3026
  tDecoderInit(&dc, pReq, len);
7,153✔
3027
  // decode req
3028
  if (tDecodeSVCreateStbReq(&dc, &req) < 0) {
7,153!
3029
    tDecoderClear(&dc);
×
3030
    return terrno = TSDB_CODE_INVALID_MSG;
×
3031
  }
3032

3033
  code = metaAddIndexToSuperTable(pVnode->pMeta, ver, &req);
7,153✔
3034
  if (code) {
7,153!
3035
    pRsp->code = code;
×
3036
    goto _err;
×
3037
  }
3038
  tDecoderClear(&dc);
7,153✔
3039
  return 0;
7,153✔
3040

3041
_err:
×
3042
  tDecoderClear(&dc);
×
3043
  return code;
×
3044
}
3045
static int32_t vnodeProcessDropIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
5,168✔
3046
  SDropIndexReq req = {0};
5,168✔
3047
  int32_t       code = 0;
5,168✔
3048
  pRsp->msgType = TDMT_VND_DROP_INDEX_RSP;
5,168✔
3049
  pRsp->code = TSDB_CODE_SUCCESS;
5,168✔
3050
  pRsp->pCont = NULL;
5,168✔
3051
  pRsp->contLen = 0;
5,168✔
3052

3053
  if ((code = tDeserializeSDropIdxReq(pReq, len, &req))) {
5,168!
3054
    pRsp->code = code;
×
3055
    return code;
×
3056
  }
3057

3058
  code = metaDropIndexFromSuperTable(pVnode->pMeta, ver, &req);
5,168✔
3059
  if (code) {
5,168!
3060
    pRsp->code = code;
×
3061
    return code;
×
3062
  }
3063
  return TSDB_CODE_SUCCESS;
5,168✔
3064
}
3065

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

3068
static int32_t vnodeProcessCompactVnodeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
45,993✔
3069
  if (!pVnode->restored) {
45,993!
3070
    vInfo("vgId:%d, ignore compact req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
13,023!
3071
    return 0;
13,023✔
3072
  }
3073
  return vnodeAsyncCompact(pVnode, ver, pReq, len, pRsp);
32,970✔
3074
}
3075

3076
static int32_t vnodeProcessConfigChangeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
3077
  if (syncCheckMember(pVnode->sync) != 0) {
×
3078
    vError("vgId:%d, failed to check member", TD_VID(pVnode));
×
3079
  }
3080

3081
  pRsp->msgType = TDMT_SYNC_CONFIG_CHANGE_RSP;
×
3082
  pRsp->code = TSDB_CODE_SUCCESS;
×
3083
  pRsp->pCont = NULL;
×
3084
  pRsp->contLen = 0;
×
3085

3086
  return 0;
×
3087
}
3088

3089
static int32_t vnodeCheckState(SVnode *pVnode) {
68,820✔
3090
  SSyncState syncState = syncGetState(pVnode->sync);
68,820✔
3091
  if (syncState.state != TAOS_SYNC_STATE_LEADER) {
68,820✔
3092
    return terrno = TSDB_CODE_SYN_NOT_LEADER;
34,236✔
3093
  }
3094
  return 0;
34,584✔
3095
}
3096

3097
static int32_t vnodeCheckToken(SVnode *pVnode, char *member0Token, char *member1Token) {
34,584✔
3098
  char token[TSDB_ARB_TOKEN_SIZE] = {0};
34,584✔
3099
  if (vnodeGetArbToken(pVnode, token) != 0) {
34,584!
3100
    return terrno = TSDB_CODE_NOT_FOUND;
×
3101
  }
3102

3103
  if (strncmp(token, member0Token, TSDB_ARB_TOKEN_SIZE) != 0 &&
34,584!
3104
      strncmp(token, member1Token, TSDB_ARB_TOKEN_SIZE) != 0) {
31,810!
3105
    return terrno = TSDB_CODE_MND_ARB_TOKEN_MISMATCH;
30,854✔
3106
  }
3107

3108
  terrno = TSDB_CODE_SUCCESS;
3,730✔
3109
  return 0;
3,730✔
3110
}
3111

3112
static int32_t vnodeCheckSyncd(SVnode *pVnode, char *member0Token, char *member1Token) {
1,865✔
3113
  int32_t code = vnodeCheckToken(pVnode, member0Token, member1Token);
1,865✔
3114
  if (code != 0) {
1,865!
UNCOV
3115
    return code;
×
3116
  }
3117

3118
  return syncCheckSynced(pVnode->sync);
1,865✔
3119
}
3120

3121
static int32_t vnodeProcessArbCheckSyncReq(SVnode *pVnode, void *pReq, int32_t len, SRpcMsg *pRsp) {
8,229✔
3122
  int32_t code = 0;
8,229✔
3123

3124
  if ((code = vnodeCheckState(pVnode)) != 0) {
8,229✔
3125
    vDebug("vgId:%d, failed to preprocess vnode-arb-check-sync request since %s", TD_VID(pVnode), tstrerror(code));
6,364!
3126
    return 0;
6,364✔
3127
  }
3128

3129
  SVArbCheckSyncReq syncReq = {0};
1,865✔
3130

3131
  code = tDeserializeSVArbCheckSyncReq(pReq, len, &syncReq);
1,865✔
3132
  if (code) {
1,865!
3133
    return code;
×
3134
  }
3135

3136
  vInfo("vgId:%d, start to process vnode-arb-check-sync req QID:0x%" PRIx64 ":0x%" PRIx64 ", seqNum:%" PRIx64
1,865!
3137
        ", arbToken:%s, member0Token:%s, member1Token:%s, "
3138
        "arbTerm:%" PRId64,
3139
        TD_VID(pVnode), pRsp->info.traceId.rootId, pRsp->info.traceId.msgId, pRsp->info.seqNum, syncReq.arbToken,
3140
        syncReq.member0Token, syncReq.member1Token, syncReq.arbTerm);
3141

3142
  pRsp->msgType = TDMT_VND_ARB_CHECK_SYNC_RSP;
1,865✔
3143
  pRsp->code = TSDB_CODE_SUCCESS;
1,865✔
3144
  pRsp->pCont = NULL;
1,865✔
3145
  pRsp->contLen = 0;
1,865✔
3146

3147
  SVArbCheckSyncRsp syncRsp = {0};
1,865✔
3148
  syncRsp.arbToken = syncReq.arbToken;
1,865✔
3149
  syncRsp.member0Token = syncReq.member0Token;
1,865✔
3150
  syncRsp.member1Token = syncReq.member1Token;
1,865✔
3151
  syncRsp.vgId = TD_VID(pVnode);
1,865✔
3152

3153
  if ((syncRsp.errCode = vnodeCheckSyncd(pVnode, syncReq.member0Token, syncReq.member1Token)) != 0) {
1,865!
UNCOV
3154
    vError("vgId:%d, failed to check assigned log syncd since %s", TD_VID(pVnode), tstrerror(syncRsp.errCode));
×
3155
  }
3156

3157
  if ((code = vnodeUpdateArbTerm(pVnode, syncReq.arbTerm)) != 0) {
1,865!
3158
    vError("vgId:%d, failed to update arb term since %s", TD_VID(pVnode), tstrerror(code));
×
3159
    goto _OVER;
×
3160
  }
3161

3162
  int32_t contLen = tSerializeSVArbCheckSyncRsp(NULL, 0, &syncRsp);
1,865✔
3163
  if (contLen <= 0) {
1,865!
3164
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
3165
    code = -1;
×
3166
    goto _OVER;
×
3167
  }
3168
  void *pHead = rpcMallocCont(contLen);
1,865✔
3169
  if (!pHead) {
1,865!
3170
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
3171
    code = -1;
×
3172
    goto _OVER;
×
3173
  }
3174

3175
  if (tSerializeSVArbCheckSyncRsp(pHead, contLen, &syncRsp) <= 0) {
1,865!
3176
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
3177
    rpcFreeCont(pHead);
×
3178
    code = -1;
×
3179
    goto _OVER;
×
3180
  }
3181

3182
  pRsp->pCont = pHead;
1,865✔
3183
  pRsp->contLen = contLen;
1,865✔
3184

3185
  vInfo(
1,865!
3186
      "vgId:%d, suceed to process vnode-arb-check-sync req rsp.code:%s, arbToken:%s, member0Token:%s, "
3187
      "member1Token:%s",
3188
      TD_VID(pVnode), tstrerror(syncRsp.errCode), syncRsp.arbToken, syncRsp.member0Token, syncRsp.member1Token);
3189

3190
  code = TSDB_CODE_SUCCESS;
1,865✔
3191

3192
_OVER:
1,865✔
3193
  if (code != 0) {
1,865!
3194
    vError("vgId:%d, failed to process arb check req rsp.code:%s since %s", TD_VID(pVnode), tstrerror(syncRsp.errCode),
×
3195
           tstrerror(code));
3196
  }
3197
  tFreeSVArbCheckSyncReq(&syncReq);
1,865✔
3198
  return code;
1,865✔
3199
}
3200

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