• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In
Build has been canceled!

taosdata / TDengine / #4938

23 Jan 2026 09:40AM UTC coverage: 66.8% (+0.006%) from 66.794%
#4938

push

travis-ci

web-flow
fix: case failuer caused by the modification of the error description (#34391)

204187 of 305671 relevant lines covered (66.8%)

124015580.65 hits per line

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

64.72
/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) {
54,620,482✔
85
  int32_t code = 0;
54,620,482✔
86
  int32_t lino = 0;
54,620,482✔
87

88
  if (tStartDecode(pCoder) < 0) {
54,620,482✔
89
    code = TSDB_CODE_INVALID_MSG;
×
90
    TSDB_CHECK_CODE(code, lino, _exit);
×
91
  }
92

93
  // flags
94
  if (tDecodeI32v(pCoder, NULL) < 0) {
54,631,594✔
95
    code = TSDB_CODE_INVALID_MSG;
×
96
    TSDB_CHECK_CODE(code, lino, _exit);
×
97
  }
98

99
  // name
100
  char *name = NULL;
54,631,594✔
101
  if (tDecodeCStr(pCoder, &name) < 0) {
54,627,150✔
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);
54,627,150✔
108
  if (uid == 0) {
54,625,332✔
109
    uid = tGenIdPI64();
54,283,077✔
110
  }
111
  taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), uid);
54,615,933✔
112

113
  // btime
114
  taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos + 8), btime);
54,617,483✔
115

116
  tEndDecode(pCoder);
54,608,418✔
117

118
_exit:
54,615,875✔
119
  if (code) {
54,616,425✔
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);
54,616,425✔
123
    if (pUid) *pUid = uid;
54,612,293✔
124
  }
125
  return code;
54,612,830✔
126
}
127
static int32_t vnodePreProcessCreateTableMsg(SVnode *pVnode, SRpcMsg *pMsg) {
35,834,223✔
128
  int32_t code = 0;
35,834,223✔
129
  int32_t lino = 0;
35,834,223✔
130

131
  int64_t  btime = taosGetTimestampMs();
35,837,537✔
132
  SDecoder dc = {0};
35,837,537✔
133
  int32_t  nReqs;
35,829,838✔
134

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

141
  if (tDecodeI32v(&dc, &nReqs) < 0) {
35,837,537✔
142
    code = TSDB_CODE_INVALID_MSG;
×
143
    TSDB_CHECK_CODE(code, lino, _exit);
×
144
  }
145
  for (int32_t iReq = 0; iReq < nReqs; iReq++) {
76,344,224✔
146
    code = vnodePreprocessCreateTableReq(pVnode, &dc, btime, NULL);
40,515,177✔
147
    TSDB_CHECK_CODE(code, lino, _exit);
40,506,687✔
148
  }
149

150
  tEndDecode(&dc);
35,829,047✔
151

152
_exit:
35,836,673✔
153
  tDecoderClear(&dc);
35,837,151✔
154
  if (code) {
35,835,209✔
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;
35,833,259✔
159
}
160

161
static int32_t vnodePreProcessAlterTableMsg(SVnode *pVnode, SRpcMsg *pMsg) {
11,473,357✔
162
  int32_t code = TSDB_CODE_INVALID_MSG;
11,473,357✔
163
  int32_t lino = 0;
11,473,357✔
164

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

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

178
  code = 0;
11,473,357✔
179

180
_exit:
11,473,357✔
181
  tDecoderClear(&dc);
11,473,357✔
182
  if (code) {
11,473,357✔
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,
11,473,357✔
186
           ctimeMs);
187
  }
188
  return code;
11,473,357✔
189
}
190

191
static int32_t vnodePreProcessDropTtlMsg(SVnode *pVnode, SRpcMsg *pMsg) {
9,163,084✔
192
  int32_t code = TSDB_CODE_INVALID_MSG;
9,163,084✔
193
  int32_t lino = 0;
9,163,084✔
194

195
  SMsgHead *pContOld = pMsg->pCont;
9,163,084✔
196
  int32_t   reqLenOld = pMsg->contLen - sizeof(SMsgHead);
9,167,380✔
197

198
  SArray *tbUids = NULL;
9,167,302✔
199
  int64_t timestampMs = 0;
9,167,302✔
200

201
  SVDropTtlTableReq ttlReq = {0};
9,167,302✔
202
  if (tDeserializeSVDropTtlTableReq((char *)pContOld + sizeof(SMsgHead), reqLenOld, &ttlReq) != 0) {
9,165,753✔
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));
9,167,783✔
209
    if (tbUids == NULL) {
9,168,977✔
210
      code = terrno;
×
211
      TSDB_CHECK_CODE(code, lino, _exit);
×
212
    }
213

214
    timestampMs = (int64_t)ttlReq.timestampSec * 1000;
9,168,977✔
215
    code = metaTtlFindExpired(pVnode->pMeta, timestampMs, tbUids, ttlReq.ttlDropMaxCount);
9,168,977✔
216
    if (code != 0) {
9,163,413✔
217
      code = TSDB_CODE_INVALID_MSG;
×
218
      TSDB_CHECK_CODE(code, lino, _exit);
×
219
    }
220

221
    ttlReq.nUids = taosArrayGetSize(tbUids);
9,163,413✔
222
    ttlReq.pTbUids = tbUids;
9,161,495✔
223
  }
224

225
  if (ttlReq.nUids == 0) {
9,161,495✔
226
    code = TSDB_CODE_MSG_PREPROCESSED;
9,144,114✔
227
    TSDB_CHECK_CODE(code, lino, _exit);
9,144,114✔
228
  }
229

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

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

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

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

251
  code = 0;
11,127✔
252

253
_exit:
9,158,813✔
254
  taosArrayDestroy(tbUids);
9,165,977✔
255

256
  if (code && code != TSDB_CODE_MSG_PREPROCESSED) {
9,140,453✔
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);
9,140,453✔
261
  }
262

263
  return code;
9,140,453✔
264
}
265

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

271
  if (tStartDecode(pCoder) < 0) {
560,195,929✔
272
    code = TSDB_CODE_INVALID_MSG;
×
273
    TSDB_CHECK_CODE(code, lino, _exit);
×
274
  }
275

276
  SSubmitTbData submitTbData;
560,191,398✔
277
  uint8_t       version;
278
  if (tDecodeI32v(pCoder, &submitTbData.flags) < 0) {
560,193,856✔
279
    code = TSDB_CODE_INVALID_MSG;
×
280
    TSDB_CHECK_CODE(code, lino, _exit);
×
281
  }
282
  version = (submitTbData.flags >> 8) & 0xff;
560,193,856✔
283
  submitTbData.flags = submitTbData.flags & 0xff;
560,193,856✔
284

285
  int64_t uid;
560,181,146✔
286
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
560,193,890✔
287
    code = vnodePreprocessCreateTableReq(pVnode, pCoder, btimeMs, &uid);
14,100,249✔
288
    TSDB_CHECK_CODE(code, lino, _exit);
14,106,297✔
289
  }
290

291
  // submit data
292
  if (tDecodeI64(pCoder, &submitTbData.suid) < 0) {
560,180,281✔
293
    code = TSDB_CODE_INVALID_MSG;
×
294
    TSDB_CHECK_CODE(code, lino, _exit);
×
295
  }
296

297
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
560,180,281✔
298
    taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), uid);
14,085,442✔
299
    pCoder->pos += sizeof(int64_t);
14,080,283✔
300
  } else {
301
    if (tDecodeI64(pCoder, &submitTbData.uid) < 0) {
546,093,300✔
302
      code = TSDB_CODE_INVALID_MSG;
×
303
      TSDB_CHECK_CODE(code, lino, _exit);
×
304
    }
305
  }
306

307
  if (tDecodeI32v(pCoder, &submitTbData.sver) < 0) {
560,188,823✔
308
    code = TSDB_CODE_INVALID_MSG;
×
309
    TSDB_CHECK_CODE(code, lino, _exit);
×
310
  }
311

312
  // scan and check
313
  TSKEY now = btimeMs;
560,188,823✔
314
  if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_MICRO) {
560,188,823✔
315
    now *= 1000;
2,388,212✔
316
  } else if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_NANO) {
557,799,866✔
317
    now *= 1000000;
1,384,544✔
318
  }
319

320
  int32_t keep = pVnode->config.tsdbCfg.keep2;
560,185,416✔
321

322
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * keep;
560,186,148✔
323
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
560,187,862✔
324
  if (submitTbData.flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
560,185,729✔
325
    uint64_t nColData;
6,280,148✔
326
    if (tDecodeU64v(pCoder, &nColData) < 0) {
6,281,151✔
327
      code = TSDB_CODE_INVALID_MSG;
×
328
      TSDB_CHECK_CODE(code, lino, _exit);
×
329
    }
330

331
    SColData colData = {0};
6,281,151✔
332
    code = tDecodeColData(version, pCoder, &colData, false);
6,281,151✔
333
    if (code) {
6,279,601✔
334
      code = TSDB_CODE_INVALID_MSG;
×
335
      TSDB_CHECK_CODE(code, lino, _exit);
×
336
    }
337

338
    if (colData.flag != HAS_VALUE) {
6,279,601✔
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++) {
2,147,483,647✔
344
      if (((TSKEY *)colData.pData)[iRow] < minKey || ((TSKEY *)colData.pData)[iRow] > maxKey) {
2,147,483,647✔
345
        code = TSDB_CODE_TDB_TIMESTAMP_OUT_OF_RANGE;
564✔
346
        TSDB_CHECK_CODE(code, lino, _exit);
564✔
347
      }
348
    }
349

350
    for (uint64_t i = 1; i < nColData; i++) {
19,155,961✔
351
      code = tDecodeColData(version, pCoder, &colData, true);
13,025,204✔
352
      if (code) {
13,026,011✔
353
        code = TSDB_CODE_INVALID_MSG;
×
354
        TSDB_CHECK_CODE(code, lino, _exit);
×
355
      }
356
    }
357
  } else {
358
    uint64_t nRow;
553,892,871✔
359
    if (tDecodeU64v(pCoder, &nRow) < 0) {
553,912,098✔
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;
64,297✔
374
        TSDB_CHECK_CODE(code, lino, _exit);
64,297✔
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)) {
560,114,625✔
386
    taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), ctimeMs);
560,116,582✔
387
    pCoder->pos += sizeof(int64_t);
560,073,439✔
388
  }
389

390
  tEndDecode(pCoder);
560,075,224✔
391

392
_exit:
560,156,816✔
393
  if (code) {
560,153,562✔
394
    vError("vgId:%d, %s:%d failed to vnodePreProcessSubmitTbData submit request since %s", TD_VID(pVnode), __func__,
64,861✔
395
           lino, tstrerror(code));
396
  }
397
  return code;
560,153,562✔
398
}
399
static int32_t vnodePreProcessSubmitMsg(SVnode *pVnode, SRpcMsg *pMsg) {
533,997,703✔
400
  int32_t code = 0;
533,997,703✔
401
  int32_t lino = 0;
533,997,703✔
402

403
  if (tsBypassFlag & TSDB_BYPASS_RA_RPC_RECV_SUBMIT) {
533,997,703✔
404
    return TSDB_CODE_MSG_PREPROCESSED;
65✔
405
  }
406

407
  SDecoder *pCoder = &(SDecoder){0};
533,997,638✔
408

409
  if (taosHton64(((SSubmitReq2Msg *)pMsg->pCont)->version) != 1) {
533,997,776✔
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));
533,998,477✔
415

416
  if (tStartDecode(pCoder) < 0) {
533,998,615✔
417
    code = TSDB_CODE_INVALID_MSG;
×
418
    TSDB_CHECK_CODE(code, lino, _exit);
×
419
  }
420

421
  uint64_t nSubmitTbData;
533,985,738✔
422
  if (tDecodeU64v(pCoder, &nSubmitTbData) < 0) {
533,997,914✔
423
    code = TSDB_CODE_INVALID_MSG;
×
424
    TSDB_CHECK_CODE(code, lino, _exit);
×
425
  }
426

427
  int64_t btimeMs = taosGetTimestampMs();
533,998,586✔
428
  int64_t ctimeMs = btimeMs;
533,998,586✔
429
  for (int32_t i = 0; i < nSubmitTbData; i++) {
1,094,092,554✔
430
    code = vnodePreProcessSubmitTbData(pVnode, pCoder, btimeMs, ctimeMs);
560,165,347✔
431
    TSDB_CHECK_CODE(code, lino, _exit);
560,158,829✔
432
  }
433

434
  tEndDecode(pCoder);
533,927,207✔
435

436
_exit:
533,993,589✔
437
  tDecoderClear(pCoder);
533,996,010✔
438
  if (code) {
533,995,297✔
439
    vError("vgId:%d, %s:%d failed to preprocess submit request since %s, msg type:%s", TD_VID(pVnode), __func__, lino,
64,861✔
440
           tstrerror(code), TMSG_INFO(pMsg->msgType));
441
  }
442
  return code;
533,992,628✔
443
}
444

445
static int32_t vnodePreProcessDeleteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
1,648,017✔
446
  int32_t code = 0;
1,648,017✔
447

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

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

460
  code = qWorkerProcessDeleteMsg(&handle, pVnode->pQuery, pMsg, &res);
1,648,017✔
461
  if (code) goto _exit;
1,648,017✔
462

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

468
  ((SMsgHead *)pCont)->contLen = size + sizeof(SMsgHead);
1,648,017✔
469
  ((SMsgHead *)pCont)->vgId = TD_VID(pVnode);
1,648,017✔
470

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

477
  rpcFreeCont(pMsg->pCont);
1,648,017✔
478
  pMsg->pCont = pCont;
1,648,017✔
479
  pMsg->contLen = size + sizeof(SMsgHead);
1,648,017✔
480

481
  taosArrayDestroy(res.uidList);
1,648,017✔
482

483
_exit:
1,648,017✔
484
  return code;
1,648,017✔
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) {
59,728✔
513
  int32_t ret = 0;
59,728✔
514
  if ((ret = vnodeCheckState(pVnode)) != 0) {
59,728✔
515
    vDebug("vgId:%d, failed to preprocess vnode-arb-check-sync request since %s", TD_VID(pVnode), tstrerror(ret));
22,691✔
516
    return 0;
22,691✔
517
  }
518

519
  SVArbCheckSyncReq syncReq = {0};
37,037✔
520

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

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

531
  int32_t code = terrno;
37,037✔
532
  tFreeSVArbCheckSyncReq(&syncReq);
37,037✔
533

534
  return code;
37,037✔
535
}
536

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

546
  tDecoderInit(&dc, POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), pMsg->contLen - sizeof(SMsgHead));
1,286,820✔
547

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

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

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

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

620
  switch (pMsg->msgType) {
612,619,558✔
621
    case TDMT_VND_CREATE_TABLE: {
35,837,537✔
622
      code = vnodePreProcessCreateTableMsg(pVnode, pMsg);
35,837,537✔
623
    } break;
35,834,992✔
624
    case TDMT_VND_ALTER_TABLE: {
11,473,357✔
625
      code = vnodePreProcessAlterTableMsg(pVnode, pMsg);
11,473,357✔
626
    } break;
11,473,357✔
627
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
9,172,030✔
628
    case TDMT_VND_DROP_TTL_TABLE: {
629
      code = vnodePreProcessDropTtlMsg(pVnode, pMsg);
9,172,030✔
630
    } break;
9,163,479✔
631
    case TDMT_VND_SUBMIT: {
533,998,680✔
632
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.preprocess_time, METRIC_LEVEL_LOW,
533,998,680✔
633
                           { code = vnodePreProcessSubmitMsg(pVnode, pMsg); });
634
    } break;
533,995,694✔
635
    case TDMT_VND_DELETE: {
1,648,017✔
636
      code = vnodePreProcessDeleteMsg(pVnode, pMsg);
1,648,017✔
637
    } break;
1,648,017✔
638
    case TDMT_VND_BATCH_DEL: {
×
639
      code = vnodePreProcessBatchDeleteMsg(pVnode, pMsg);
×
640
    } break;
×
641
    case TDMT_VND_ARB_CHECK_SYNC: {
59,728✔
642
      code = vnodePreProcessArbCheckSyncMsg(pVnode, pMsg);
59,728✔
643
    } break;
59,728✔
644
    case TDMT_VND_DROP_TABLE: {
1,286,820✔
645
      code = vnodePreProcessDropTbMsg(pVnode, pMsg);
1,286,820✔
646
    } break;
1,286,820✔
647
#ifdef TD_ENTERPRISE
648
    case TDMT_VND_SSMIGRATE_FILESET: {
×
649
      code = vnodePreProcessSsMigrateFileSetReq(pVnode, pMsg);
×
650
    } break;
×
651
#endif
652
    default:
19,143,499✔
653
      break;
19,143,499✔
654
  }
655

656
  if (code && code != TSDB_CODE_MSG_PREPROCESSED) {
612,605,586✔
657
    vError("vgId:%d, failed to preprocess write request since %s, msg type:%s", TD_VID(pVnode), tstrerror(code),
100,329✔
658
           TMSG_INFO(pMsg->msgType));
659
  }
660
  return code;
612,603,135✔
661
}
662

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

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

671
  SBseBatch *pBatch = NULL;
21,179✔
672

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

676
  SRow  **pRow = (SRow **)TARRAY_DATA(pSubmitTbData->aRowP);
21,179✔
677
  int32_t rowIdx = -1;
21,179✔
678
  for (int32_t i = 0; i < sz; i++) {
23,649,520✔
679
    int64_t     seq = 0;
23,628,341✔
680
    SBlobValue *p = taosArrayGet(pBlobSet->pSeqTable, i);
23,628,341✔
681
    if (p->type == TSDB_DATA_BLOB_EMPTY_VALUE || p->type == TSDB_DATA_BLOB_NULL_VALUE) {
23,628,341✔
682
      // skip empty or null blob
683
      continue;
2,373,468✔
684
    }
685

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

689
    SRow *row = taosArrayGetP(pSubmitTbData->aRowP, i);
21,254,873✔
690
    if (row == NULL) {
21,254,873✔
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) {
42,509,746✔
697
      code = TSDB_CODE_INVALID_MSG;
×
698
      TSDB_CHECK_CODE(code, lino, _exit);
×
699
    }
700
  }
701

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

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

785
  return code;
21,179✔
786
}
787

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

795
  (void)taosThreadMutexLock(&pVnode->mutex);
736,062,983✔
796
  if (pVnode->disableWrite) {
736,123,138✔
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);
736,074,916✔
802

803
  if (ver <= pVnode->state.applied) {
736,147,460✔
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,
736,043,201✔
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)) {
736,098,819✔
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)) {
736,149,352✔
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);
736,152,294✔
828
  atomic_store_64(&pVnode->state.applyTerm, pMsg->info.conn.applyTerm);
736,150,469✔
829

830
  if (!syncUtilUserCommit(pMsg->msgType)) goto _exit;
736,150,803✔
831

832
  // skip header
833
  pReq = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
726,293,904✔
834
  len = pMsg->contLen - sizeof(SMsgHead);
726,294,198✔
835
  bool needCommit = false;
726,293,768✔
836
  bool forceTrimWal = false;
726,293,768✔
837

838
  switch (pMsg->msgType) {
726,293,768✔
839
    /* META */
840
    case TDMT_VND_CREATE_STB:
4,783,151✔
841
      code = vnodeProcessCreateStbReq(pVnode, ver, pReq, len, pRsp);
4,783,151✔
842
      TSDB_CHECK_CODE(code, lino, _err);
4,783,652✔
843
      break;
4,780,804✔
844
    case TDMT_VND_ALTER_STB:
7,135,092✔
845
      code = vnodeProcessAlterStbReq(pVnode, ver, pReq, len, pRsp);
7,135,092✔
846
      TSDB_CHECK_CODE(code, lino, _err);
7,134,533✔
847
      break;
7,134,533✔
848
    case TDMT_VND_DROP_STB:
862,333✔
849
      code = vnodeProcessDropStbReq(pVnode, ver, pReq, len, pRsp, pMsg);
862,333✔
850
      TSDB_CHECK_CODE(code, lino, _err);
862,333✔
851
      break;
862,333✔
852
    case TDMT_VND_CREATE_TABLE:
38,714,723✔
853
      code = vnodeProcessCreateTbReq(pVnode, ver, pReq, len, pRsp, pMsg);
38,714,723✔
854
      TSDB_CHECK_CODE(code, lino, _err);
38,711,264✔
855
      break;
38,711,264✔
856
    case TDMT_VND_ALTER_TABLE:
11,473,913✔
857
      code = vnodeProcessAlterTbReq(pVnode, ver, pReq, len, pRsp);
11,473,913✔
858
      TSDB_CHECK_CODE(code, lino, _err);
11,473,913✔
859
      break;
11,473,913✔
860
    case TDMT_VND_DROP_TABLE:
1,287,122✔
861
    case TDMT_VND_SNODE_DROP_TABLE:
862
      code = vnodeProcessDropTbReq(pVnode, ver, pReq, len, pRsp, pMsg);
1,287,122✔
863
      TSDB_CHECK_CODE(code, lino, _err);
1,287,122✔
864
      break;
1,287,122✔
865
    case TDMT_VND_CREATE_RSMA:
21,360✔
866
      code = vnodeProcessCreateRsmaReq(pVnode, ver, pReq, len, pRsp);
21,360✔
867
      TSDB_CHECK_CODE(code, lino, _err);
21,360✔
868
      break;
21,360✔
869
    case TDMT_VND_DROP_RSMA:
4,272✔
870
      code = vnodeProcessDropRsmaReq(pVnode, ver, pReq, len, pRsp);
4,272✔
871
      TSDB_CHECK_CODE(code, lino, _err);
4,272✔
872
      break;
4,272✔
873
    case TDMT_VND_ALTER_RSMA:
11,392✔
874
      code = vnodeProcessAlterRsmaReq(pVnode, ver, pReq, len, pRsp);
11,392✔
875
      TSDB_CHECK_CODE(code, lino, _err);
11,392✔
876
      break;
11,392✔
877
    case TDMT_VND_DROP_TTL_TABLE:
×
878
      code = vnodeProcessDropTtlTbReq(pVnode, ver, pReq, len, pRsp);
×
879
      TSDB_CHECK_CODE(code, lino, _err);
×
880
      break;
×
881
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
11,127✔
882
      code = vnodeProcessFetchTtlExpiredTbs(pVnode, ver, pReq, len, pRsp);
11,127✔
883
      TSDB_CHECK_CODE(code, lino, _err);
11,127✔
884
      break;
11,127✔
885
    case TDMT_VND_TRIM:
49,979✔
886
      code = vnodeProcessTrimReq(pVnode, ver, pReq, len, pRsp);
49,979✔
887
      TSDB_CHECK_CODE(code, lino, _err);
49,979✔
888
      break;
49,979✔
889
      case TDMT_VND_TRIM_WAL:
6,420✔
890
        needCommit = true;
6,420✔
891
        forceTrimWal = true;
6,420✔
892
        break;
6,420✔
893
#ifdef TD_ENTERPRISE
894
    case TDMT_VND_SSMIGRATE_FILESET:
×
895
      if (vnodeProcessSsMigrateFileSetReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
896
      break;
×
897
    case TDMT_VND_FOLLOWER_SSMIGRATE:
×
898
      if (vnodeProcessFollowerSsMigrateReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
899
      break;
×
900
    case TDMT_VND_KILL_SSMIGRATE:
×
901
      if (vnodeProcessKillSsMigrateReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
902
      break;
×
903
#endif
904

905
    /* TSDB */
906
    case TDMT_VND_SUBMIT: {
645,109,804✔
907
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.apply_time, METRIC_LEVEL_LOW, {
645,109,804✔
908
        if (vnodeProcessSubmitReq(pVnode, ver, pMsg->pCont, pMsg->contLen, pRsp, pMsg) < 0) goto _err;
909
      });
910
      METRICS_UPDATE(pVnode->writeMetrics.apply_bytes, METRIC_LEVEL_LOW, (int64_t)pMsg->contLen);
645,065,085✔
911
      break;
645,059,769✔
912
    }
913
    case TDMT_VND_DELETE:
2,004,554✔
914
      code = vnodeProcessDeleteReq(pVnode, ver, pReq, len, pRsp, pMsg);
2,004,554✔
915
      TSDB_CHECK_CODE(code, lino, _err);
2,004,554✔
916
      break;
2,004,554✔
917
    case TDMT_VND_BATCH_DEL:
×
918
      code = vnodeProcessBatchDeleteReq(pVnode, ver, pReq, len, pRsp);
×
919
      TSDB_CHECK_CODE(code, lino, _err);
×
920
      break;
×
921
      /* TQ */
922
#if defined(USE_TQ) || defined(USE_STREAM)
923
    case TDMT_VND_TMQ_SUBSCRIBE:
1,196,542✔
924
      code = tqProcessSubscribeReq(pVnode->pTq, ver, pReq, len);
1,196,542✔
925
      TSDB_CHECK_CODE(code, lino, _err);
1,193,779✔
926
      break;
1,193,779✔
927
    case TDMT_VND_TMQ_DELETE_SUB:
267,408✔
928
      code = tqProcessDeleteSubReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen);
267,408✔
929
      TSDB_CHECK_CODE(code, lino, _err);
266,859✔
930
      break;
266,859✔
931
    case TDMT_VND_TMQ_COMMIT_OFFSET:
4,356,717✔
932
      code = tqProcessOffsetCommitReq(pVnode->pTq, ver, pReq, len);
4,356,717✔
933
      TSDB_CHECK_CODE(code, lino, _err);
4,356,170✔
934
      break;
4,356,170✔
935
#endif
936
    case TDMT_VND_ALTER_CONFIRM:
3,829,664✔
937
      needCommit = pVnode->config.hashChange;
3,829,664✔
938
      code = vnodeProcessAlterConfirmReq(pVnode, ver, pReq, len, pRsp);
3,829,664✔
939
      TSDB_CHECK_CODE(code, lino, _err);
3,829,664✔
940
      break;
3,829,664✔
941
    case TDMT_VND_ALTER_CONFIG:
303,902✔
942
      vnodeProcessAlterConfigReq(pVnode, ver, pReq, len, pRsp);
303,902✔
943
      break;
303,902✔
944
    case TDMT_VND_COMMIT:
4,767,242✔
945
      needCommit = true;
4,767,242✔
946
      break;
4,767,242✔
947
    case TDMT_VND_CREATE_INDEX:
4,271✔
948
      vnodeProcessCreateIndexReq(pVnode, ver, pReq, len, pRsp);
4,271✔
949
      break;
4,271✔
950
    case TDMT_VND_DROP_INDEX:
2,812✔
951
      vnodeProcessDropIndexReq(pVnode, ver, pReq, len, pRsp);
2,812✔
952
      break;
2,812✔
953
    case TDMT_VND_COMPACT:
65,148✔
954
      vnodeProcessCompactVnodeReq(pVnode, ver, pReq, len, pRsp);
65,148✔
955
      goto _exit;
65,148✔
956
    case TDMT_VND_SCAN:
220✔
957
      vnodeProcessScanVnodeReq(pVnode, ver, pReq, len, pRsp);
220✔
958
      goto _exit;
220✔
959
    case TDMT_SYNC_CONFIG_CHANGE:
×
960
      vnodeProcessConfigChangeReq(pVnode, ver, pReq, len, pRsp);
×
961
      break;
×
962
#ifdef TD_ENTERPRISE
963
    case TDMT_VND_KILL_COMPACT:
1,424✔
964
      vnodeProcessKillCompactReq(pVnode, ver, pReq, len, pRsp);
1,424✔
965
      break;
1,424✔
966
#endif
967
    case TDMT_VND_KILL_SCAN:
88✔
968
      vnodeProcessKillScanReq(pVnode, ver, pReq, len, pRsp);
88✔
969
      break;
88✔
970
    case TDMT_VND_KILL_TRIM:
×
971
      vnodeProcessKillRetentionReq(pVnode, ver, pReq, len, pRsp);
×
972
      break;
×
973
    /* ARB */
974
    case TDMT_VND_ARB_CHECK_SYNC:
23,325✔
975
      vnodeProcessArbCheckSyncReq(pVnode, pReq, len, pRsp);
23,325✔
976
      break;
23,325✔
977
    default:
×
978
      vError("vgId:%d, unprocessed msg, %d", TD_VID(pVnode), pMsg->msgType);
×
979
      return TSDB_CODE_INVALID_MSG;
×
980
  }
981

982
  vGDebug(&pMsg->info.traceId, "vgId:%d, index:%" PRId64 ", msg processed, code:0x%x", TD_VID(pVnode), ver, pRsp->code);
726,168,378✔
983

984
  walApplyVer(pVnode->pWal, ver);
726,186,674✔
985

986
  if (pVnode->pTq) {
726,189,638✔
987
    code = tqPushMsg(pVnode->pTq, pMsg->msgType);
726,189,919✔
988
    if (code) {
726,188,385✔
989
      vError("vgId:%d, failed to push msg to TQ since %s", TD_VID(pVnode), tstrerror(terrno));
×
990
      return code;
×
991
    }
992
  }
993

994
  // commit if need
995
  if (needCommit) {
726,188,385✔
996
    vInfo("vgId:%d, commit at version %" PRId64, TD_VID(pVnode), ver);
4,797,144✔
997
    code = vnodeAsyncCommit(pVnode, forceTrimWal);
4,797,144✔
998
    if (code) {
4,797,144✔
999
      vError("vgId:%d, failed to vnode async commit since %s.", TD_VID(pVnode), tstrerror(terrno));
×
1000
      goto _err;
×
1001
    }
1002

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

1013
_exit:
726,185,756✔
1014
  return 0;
736,106,428✔
1015

1016
_err:
40,775✔
1017
  if (code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
40,775✔
1018
    vInfo("vgId:%d, process %s request failed since %s, lino 1:%d ver:%" PRId64, TD_VID(pVnode),
2,848✔
1019
          TMSG_INFO(pMsg->msgType), tstrerror(code), lino, ver);
1020
  } else if (code) {
37,927✔
1021
    vError("vgId:%d, process %s request failed since %s, lino 2:%d ver:%" PRId64, TD_VID(pVnode),
×
1022
           TMSG_INFO(pMsg->msgType), tstrerror(code), lino, ver);
1023
  }
1024

1025
  return code;
40,775✔
1026
}
1027

1028
int32_t vnodePreprocessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg) {
354,654,825✔
1029
  if (TDMT_SCH_QUERY != pMsg->msgType && TDMT_SCH_MERGE_QUERY != pMsg->msgType) {
354,654,825✔
1030
    return 0;
75,417,480✔
1031
  }
1032

1033
  int32_t qType = 0;
279,277,816✔
1034
  return qWorkerPreprocessQueryMsg(pVnode->pQuery, pMsg, (TDMT_SCH_QUERY == pMsg->msgType), &qType);
279,258,681✔
1035
}
1036

1037
int32_t vnodeProcessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
354,760,055✔
1038
  const STraceId *trace = &pMsg->info.traceId;
354,760,055✔
1039
  vDebug("vgId:%d, msg:%s, %p in vnode query queue is processing, %" PRIx64 ":%" PRIx64, 
354,766,639✔
1040
    pVnode->config.vgId, TMSG_INFO(pMsg->msgType), pMsg, TRACE_GET_ROOTID(trace), TRACE_GET_MSGID(trace));
1041

1042
  if (pMsg->msgType == TDMT_VND_TMQ_CONSUME && !syncIsReadyForRead(pVnode->sync)) {
354,796,098✔
1043
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
411,892✔
1044
    return 0;
411,892✔
1045
  }
1046

1047
  if (pMsg->msgType == TDMT_VND_TMQ_CONSUME && !pVnode->restored) {
354,386,516✔
1048
    vnodeRedirectRpcMsg(pVnode, pMsg, TSDB_CODE_SYN_RESTORING);
×
1049
    return 0;
×
1050
  }
1051

1052
  SReadHandle handle = {0};
354,385,632✔
1053
  handle.vnode = pVnode;
354,384,159✔
1054
  handle.pMsgCb = &pVnode->msgCb;
354,384,159✔
1055
  handle.pWorkerCb = pInfo->workerCb;
354,354,210✔
1056
  initStorageAPI(&handle.api);
354,356,222✔
1057
  int32_t code = TSDB_CODE_SUCCESS;
354,322,732✔
1058
  bool    redirected = false;
354,322,732✔
1059

1060
  switch (pMsg->msgType) {
354,322,732✔
1061
    case TDMT_SCH_QUERY:
216,991,609✔
1062
      if (!syncIsReadyForRead(pVnode->sync)) {
216,991,609✔
1063
        pMsg->code = (terrno) ? terrno : TSDB_CODE_SYN_NOT_LEADER;
2,480,143✔
1064
        redirected = true;
2,480,143✔
1065
      }
1066
      code = qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
217,006,641✔
1067
      if (redirected) {
216,927,642✔
1068
        vnodeRedirectRpcMsg(pVnode, pMsg, pMsg->code);
2,480,143✔
1069
        return 0;
2,480,143✔
1070
      }
1071

1072
      return code;
214,465,283✔
1073
    case TDMT_SCH_MERGE_QUERY:
62,340,728✔
1074
      return qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
62,340,728✔
1075
    case TDMT_SCH_QUERY_CONTINUE:
39,282,336✔
1076
      return qWorkerProcessCQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
39,282,336✔
1077
    case TDMT_VND_TMQ_CONSUME:
26,750,794✔
1078
      return tqProcessPollReq(pVnode->pTq, pMsg);
26,750,794✔
1079
    case TDMT_VND_TMQ_CONSUME_PUSH:
8,973,988✔
1080
      return tqProcessPollPush(pVnode->pTq);
8,973,988✔
1081
    default:
×
1082
      vError("unknown msg type:%d in query queue", pMsg->msgType);
×
1083
      return TSDB_CODE_APP_ERROR;
×
1084
  }
1085
}
1086

1087
int32_t vnodeProcessFetchMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
760,845,481✔
1088
  int32_t code = TSDB_CODE_SUCCESS;
760,845,481✔
1089
  const STraceId *trace = &pMsg->info.traceId;
760,845,481✔
1090
  vDebug("vgId:%d, msg:%s, %p in fetch queue is processing, %" PRIx64 ":%" PRIx64, 
760,903,230✔
1091
    pVnode->config.vgId, TMSG_INFO(pMsg->msgType), pMsg, TRACE_GET_ROOTID(trace), TRACE_GET_MSGID(trace));
1092
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
761,061,455✔
1093
       pMsg->msgType == TDMT_VND_BATCH_META || pMsg->msgType == TDMT_VND_TABLE_NAME ||
522,786,349✔
1094
       pMsg->msgType == TDMT_VND_VSUBTABLES_META || pMsg->msgType == TDMT_VND_VSTB_REF_DBS) &&
497,456,425✔
1095
      !syncIsReadyForRead(pVnode->sync)) {
263,606,063✔
1096
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
684,419✔
1097
    return 0;
684,419✔
1098
  }
1099

1100
  switch (pMsg->msgType) {
760,401,166✔
1101
    case TDMT_SCH_FETCH:
315,750,150✔
1102
    case TDMT_SCH_MERGE_FETCH:
1103
      return qWorkerProcessFetchMsg(pVnode, pVnode->pQuery, pMsg, 0);
315,750,150✔
1104
    case TDMT_SCH_FETCH_RSP:
×
1105
      return qWorkerProcessRspMsg(pVnode, pVnode->pQuery, pMsg, 0);
×
1106
    // case TDMT_SCH_CANCEL_TASK:
1107
    //   return qWorkerProcessCancelMsg(pVnode, pVnode->pQuery, pMsg, 0);
1108
    case TDMT_SCH_DROP_TASK:
279,135,836✔
1109
      return qWorkerProcessDropMsg(pVnode, pVnode->pQuery, pMsg, 0);
279,135,836✔
1110
    case TDMT_SCH_TASK_NOTIFY:
12,965✔
1111
      return qWorkerProcessNotifyMsg(pVnode, pVnode->pQuery, pMsg, 0);
12,965✔
1112
    case TDMT_SCH_QUERY_HEARTBEAT:
139,778,965✔
1113
      return qWorkerProcessHbMsg(pVnode, pVnode->pQuery, pMsg, 0);
139,778,965✔
1114
    case TDMT_VND_TABLE_META:
856,184✔
1115
    case TDMT_VND_TABLE_NAME:
1116
      return vnodeGetTableMeta(pVnode, pMsg, true);
856,184✔
1117
    case TDMT_VND_TABLE_CFG:
×
1118
      return vnodeGetTableCfg(pVnode, pMsg, true);
×
1119
    case TDMT_VND_BATCH_META: {
24,643,254✔
1120
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.fetch_batch_meta_time, METRIC_LEVEL_LOW,
24,643,254✔
1121
                           { code = vnodeGetBatchMeta(pVnode, pMsg); });
1122
      METRICS_UPDATE(pVnode->writeMetrics.fetch_batch_meta_count, METRIC_LEVEL_LOW, 1);
24,647,023✔
1123
      return code;
24,639,162✔
1124
    }
1125
    case TDMT_VND_VSUBTABLES_META:
×
1126
      return vnodeGetVSubtablesMeta(pVnode, pMsg);
×
1127
    case TDMT_VND_VSTB_REF_DBS:
×
1128
      return vnodeGetVStbRefDbs(pVnode, pMsg);
×
1129
    case TDMT_VND_QUERY_SCAN_PROGRESS:
352✔
1130
      return vnodeQueryScanProgress(pVnode, pMsg);
352✔
1131
#ifdef TD_ENTERPRISE
1132
    case TDMT_VND_QUERY_COMPACT_PROGRESS:
93,404✔
1133
      return vnodeQueryCompactProgress(pVnode, pMsg);
93,404✔
1134

1135
    case TDMT_VND_LIST_SSMIGRATE_FILESETS:
×
1136
      return vnodeListSsMigrateFileSets(pVnode, pMsg);
×
1137

1138
    case TDMT_VND_QUERY_SSMIGRATE_PROGRESS:
×
1139
      return vnodeQuerySsMigrateProgress(pVnode, pMsg);
×
1140
#endif
1141
    case TDMT_VND_QUERY_TRIM_PROGRESS:
76,451✔
1142
      return vnodeQueryRetentionProgress(pVnode, pMsg);
76,451✔
1143
      //    case TDMT_VND_TMQ_CONSUME:
1144
      //      return tqProcessPollReq(pVnode->pTq, pMsg);
1145
#ifdef USE_TQ
1146
    case TDMT_VND_TMQ_VG_WALINFO:
562✔
1147
      return tqProcessVgWalInfoReq(pVnode->pTq, pMsg);
562✔
1148
    case TDMT_VND_TMQ_VG_COMMITTEDINFO:
281✔
1149
      return tqProcessVgCommittedInfoReq(pVnode->pTq, pMsg);
281✔
1150
    case TDMT_VND_TMQ_SEEK:
1,124✔
1151
      return tqProcessSeekReq(pVnode->pTq, pMsg);
1,124✔
1152
#endif
1153
    default:
25,046✔
1154
      vError("unknown msg type:%d in fetch queue", pMsg->msgType);
25,046✔
1155
      return TSDB_CODE_APP_ERROR;
×
1156
  }
1157
}
1158

1159
void vnodeUpdateMetaRsp(SVnode *pVnode, STableMetaRsp *pMetaRsp) {
63,454,247✔
1160
  if (NULL == pMetaRsp) {
63,454,247✔
1161
    return;
×
1162
  }
1163

1164
  tstrncpy(pMetaRsp->dbFName, pVnode->config.dbname, TSDB_DB_FNAME_LEN);
63,454,247✔
1165
  pMetaRsp->dbId = pVnode->config.dbId;
63,455,661✔
1166
  pMetaRsp->vgId = TD_VID(pVnode);
63,457,779✔
1167
  pMetaRsp->precision = pVnode->config.tsdbCfg.precision;
63,456,692✔
1168
}
1169

1170
extern int32_t vnodeAsyncRetention(SVnode *pVnode, STimeWindow tw, int8_t optrType, int8_t triggerType);
1171

1172
static int32_t vnodeProcessTrimReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
49,979✔
1173
  if (!pVnode->restored) {
49,979✔
1174
    vInfo("vgId:%d, ignore trim req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
2,251✔
1175
    return 0;
2,251✔
1176
  }
1177

1178
  int32_t     code = 0;
47,728✔
1179
  SVTrimDbReq trimReq = {0};
47,728✔
1180

1181
  // decode
1182
  if (tDeserializeSVTrimDbReq(pReq, len, &trimReq) != 0) {
47,728✔
1183
    code = TSDB_CODE_INVALID_MSG;
×
1184
    goto _exit;
×
1185
  }
1186

1187
  vInfo("vgId:%d, process trim vnode request, time:%" PRIi64 ",%" PRIi64 ", optr:%d, trigger:%d", pVnode->config.vgId,
47,728✔
1188
        trimReq.tw.skey, trimReq.tw.ekey, (int32_t)trimReq.optrType, (int32_t)trimReq.triggerType);
1189

1190
  code = vnodeAsyncRetention(pVnode, trimReq.tw, (int8_t)trimReq.optrType, (int8_t)trimReq.triggerType);
47,728✔
1191

1192
_exit:
47,728✔
1193
  return code;
47,728✔
1194
}
1195

1196
extern int32_t vnodeAsyncSsMigrateFileSet(SVnode *pVnode, SSsMigrateFileSetReq *pReq);
1197

1198
extern int32_t vnodeAsyncS3Migrate(SVnode *pVnode, int64_t now);
1199

1200
static int32_t vnodeProcessSsMigrateFileSetReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1201
  int32_t code = 0;
×
1202

1203
  SSsMigrateFileSetReq req = {0};
×
1204
  SSsMigrateFileSetRsp rsp = {0};
×
1205
  pRsp->msgType = TDMT_VND_SSMIGRATE_FILESET_RSP;
×
1206
  pRsp->code = 0;
×
1207
  pRsp->pCont = NULL;
×
1208
  pRsp->contLen = 0;
×
1209

1210
  if (tDeserializeSSsMigrateFileSetReq(pReq, len, &req) != 0) {
×
1211
    code = TSDB_CODE_INVALID_MSG;
×
1212
    goto _exit;
×
1213
  }
1214

1215
  vInfo("vgId:%d, ssmigrate:%d, fid:%d, process ssmigrate fileset request, time:%" PRId64, pVnode->config.vgId,
×
1216
        req.ssMigrateId, req.fid, req.startTimeSec);
1217
  rsp.ssMigrateId = req.ssMigrateId;
×
1218
  rsp.vgId = TD_VID(pVnode);
×
1219
  rsp.nodeId = req.nodeId;
×
1220
  rsp.fid = req.fid;
×
1221

1222
  code = vnodeAsyncSsMigrateFileSet(pVnode, &req);
×
1223
  if (code != TSDB_CODE_SUCCESS) {
×
1224
    vError("vgId:%d, failed to async ssmigrate since %s", TD_VID(pVnode), tstrerror(code));
×
1225
    pRsp->code = code;
×
1226
    goto _exit;
×
1227
  }
1228

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

1239
_exit:
×
1240
  pRsp->code = code;
×
1241
  return code;
×
1242
}
1243

1244
extern int32_t vnodeFollowerSsMigrate(SVnode *pVnode, SSsMigrateProgress *pReq);
1245

1246
static int32_t vnodeProcessFollowerSsMigrateReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1247
  int32_t            code = 0;
×
1248
  SSsMigrateProgress req = {0};
×
1249

1250
  // decode
1251
  if (tDeserializeSSsMigrateProgress(pReq, len, &req) != 0) {
×
1252
    code = TSDB_CODE_INVALID_MSG;
×
1253
    goto _exit;
×
1254
  }
1255

1256
  code = vnodeFollowerSsMigrate(pVnode, &req);
×
1257

1258
_exit:
×
1259
  pRsp->code = code;
×
1260
  return code;
×
1261
}
1262

1263
extern int32_t vnodeKillSsMigrate(SVnode *pVnode, SVnodeKillSsMigrateReq *pReq);
1264

1265
static int32_t vnodeProcessKillSsMigrateReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1266
  int32_t          code = 0;
×
1267
  SVnodeKillSsMigrateReq req = {0};
×
1268

1269
  // decode
1270
  if (tDeserializeSVnodeKillSsMigrateReq(pReq, len, &req) != 0) {
×
1271
    code = TSDB_CODE_INVALID_MSG;
×
1272
    goto _exit;
×
1273
  }
1274

1275
  code = vnodeKillSsMigrate(pVnode, &req);
×
1276

1277
_exit:
×
1278
  pRsp->code = code;
×
1279
  return code;
×
1280
}
1281

1282
static int32_t vnodeProcessDropTtlTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1283
  int               ret = 0;
×
1284
  SVDropTtlTableReq ttlReq = {0};
×
1285
  if (tDeserializeSVDropTtlTableReq(pReq, len, &ttlReq) != 0) {
×
1286
    ret = TSDB_CODE_INVALID_MSG;
×
1287
    goto end;
×
1288
  }
1289

1290
  if (ttlReq.nUids != taosArrayGetSize(ttlReq.pTbUids)) {
×
1291
    ret = TSDB_CODE_INVALID_MSG;
×
1292
    goto end;
×
1293
  }
1294

1295
  if (ttlReq.nUids != 0) {
×
1296
    vInfo("vgId:%d, process drop ttl table request, time:%d, ntbUids:%d", pVnode->config.vgId, ttlReq.timestampSec,
×
1297
          ttlReq.nUids);
1298
  }
1299

1300
  if (ttlReq.nUids > 0) {
×
1301
    int32_t code = metaDropMultipleTables(pVnode->pMeta, ver, ttlReq.pTbUids);
×
1302
    if (code) return code;
×
1303

1304
    code = tqUpdateTbUidList(pVnode->pTq, ttlReq.pTbUids, false);
×
1305
    if (code) {
×
1306
      vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1307
    }
1308
  }
1309

1310
end:
×
1311
  taosArrayDestroy(ttlReq.pTbUids);
×
1312
  return ret;
×
1313
}
1314

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

1327
  if (tDeserializeSVDropTtlTableReq(pReq, len, &ttlReq) != 0) {
11,127✔
1328
    terrno = TSDB_CODE_INVALID_MSG;
×
1329
    goto _end;
×
1330
  }
1331

1332
  if (!(ttlReq.nUids == taosArrayGetSize(ttlReq.pTbUids))) {
11,127✔
1333
    terrno = TSDB_CODE_INVALID_MSG;
×
1334
    goto _end;
×
1335
  }
1336

1337
  tb_uid_t    suid;
1338
  char        ctbName[TSDB_TABLE_NAME_LEN];
1339
  SVDropTbReq expiredTb = {.igNotExists = true};
11,127✔
1340
  metaReaderDoInit(&mr, pVnode->pMeta, 0);
11,127✔
1341
  rsp.vgId = TD_VID(pVnode);
11,127✔
1342
  rsp.pExpiredTbs = taosArrayInit(ttlReq.nUids, sizeof(SVDropTbReq));
11,127✔
1343
  if (!rsp.pExpiredTbs) goto _end;
11,127✔
1344

1345
  pNames = taosArrayInit(ttlReq.nUids, TSDB_TABLE_NAME_LEN);
11,127✔
1346
  if (!pNames) {
11,127✔
1347
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1348
    goto _end;
×
1349
  }
1350
  char buf[TSDB_TABLE_NAME_LEN];
11,127✔
1351
  for (int32_t i = 0; i < ttlReq.nUids; ++i) {
56,867✔
1352
    tb_uid_t *uid = taosArrayGet(ttlReq.pTbUids, i);
45,740✔
1353
    expiredTb.suid = *uid;
45,740✔
1354
    terrno = metaReaderGetTableEntryByUid(&mr, *uid);
45,740✔
1355
    if (terrno < 0) goto _end;
45,740✔
1356
    tstrncpy(buf, mr.me.name, TSDB_TABLE_NAME_LEN);
45,740✔
1357
    void *p = taosArrayPush(pNames, buf);
45,740✔
1358
    if (p == NULL) {
45,740✔
1359
      goto _end;
×
1360
    }
1361

1362
    expiredTb.name = p;
45,740✔
1363
    if (mr.me.type == TSDB_CHILD_TABLE) {
45,740✔
1364
      expiredTb.suid = mr.me.ctbEntry.suid;
29,698✔
1365
    }
1366

1367
    if (taosArrayPush(rsp.pExpiredTbs, &expiredTb) == NULL) {
91,480✔
1368
      goto _end;
×
1369
    }
1370
  }
1371

1372
  int32_t ret = 0;
11,127✔
1373
  tEncodeSize(tEncodeVFetchTtlExpiredTbsRsp, &rsp, pRsp->contLen, ret);
11,127✔
1374
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
11,127✔
1375
  if (pRsp->pCont == NULL) {
11,127✔
1376
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1377
    code = -1;
×
1378
    goto _end;
×
1379
  }
1380
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
11,127✔
1381
  terrno = tEncodeVFetchTtlExpiredTbsRsp(&encoder, &rsp);
11,127✔
1382
  tEncoderClear(&encoder);
11,127✔
1383

1384
  if (terrno == 0) code = 0;
11,127✔
1385
_end:
11,127✔
1386
  metaReaderClear(&mr);
11,127✔
1387
  tFreeFetchTtlExpiredTbsRsp(&rsp);
11,127✔
1388
  taosArrayDestroy(ttlReq.pTbUids);
11,127✔
1389
  if (pNames) taosArrayDestroy(pNames);
11,127✔
1390
  pRsp->code = terrno;
11,127✔
1391
  return code;
11,127✔
1392
}
1393

1394
static int32_t vnodeProcessCreateStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
4,778,808✔
1395
  int32_t        code = 0;
4,778,808✔
1396
  SVCreateStbReq req = {0};
4,778,808✔
1397
  SDecoder       coder;
4,781,014✔
1398

1399
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
4,783,652✔
1400
  pRsp->code = TSDB_CODE_SUCCESS;
4,783,652✔
1401
  pRsp->pCont = NULL;
4,783,652✔
1402
  pRsp->contLen = 0;
4,783,652✔
1403

1404
  // decode and process req
1405
  tDecoderInit(&coder, pReq, len);
4,783,163✔
1406

1407
  code = tDecodeSVCreateStbReq(&coder, &req);
4,783,652✔
1408
  if (code) {
4,772,003✔
1409
    pRsp->code = code;
×
1410
    goto _err;
×
1411
  }
1412

1413
  code = metaCreateSuperTable(pVnode->pMeta, ver, &req);
4,772,003✔
1414
  if (code) {
4,783,517✔
1415
    pRsp->code = code;
2,848✔
1416
    goto _err;
2,848✔
1417
  }
1418

1419
  tDecoderClear(&coder);
4,780,669✔
1420
  return 0;
4,780,804✔
1421

1422
_err:
2,848✔
1423
  tDecoderClear(&coder);
2,848✔
1424
  return code;
2,848✔
1425
}
1426

1427
static int32_t vnodeProcessCreateTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
38,715,397✔
1428
                                       SRpcMsg *pOriginRpc) {
1429
  SDecoder           decoder = {0};
38,715,397✔
1430
  SEncoder           encoder = {0};
38,715,399✔
1431
  int32_t            rcode = 0;
38,715,017✔
1432
  SVCreateTbBatchReq req = {0};
38,715,017✔
1433
  SVCreateTbReq     *pCreateReq;
1434
  SVCreateTbBatchRsp rsp = {0};
38,714,411✔
1435
  SVCreateTbRsp      cRsp = {0};
38,715,399✔
1436
  char               tbName[TSDB_TABLE_FNAME_LEN];
38,706,906✔
1437
  SArray            *tbUids = NULL;
38,714,255✔
1438
  SArray            *tbNames = NULL;
38,714,255✔
1439
  int64_t            tss = taosGetTimestampMs();
38,714,811✔
1440
  pRsp->msgType = TDMT_VND_CREATE_TABLE_RSP;
38,714,811✔
1441
  pRsp->code = TSDB_CODE_SUCCESS;
38,715,105✔
1442
  pRsp->pCont = NULL;
38,715,105✔
1443
  pRsp->contLen = 0;
38,714,931✔
1444

1445
  // decode
1446
  tDecoderInit(&decoder, pReq, len);
38,715,225✔
1447
  if (tDecodeSVCreateTbBatchReq(&decoder, &req) < 0) {
38,714,931✔
1448
    rcode = -1;
×
1449
    terrno = TSDB_CODE_INVALID_MSG;
×
1450
    goto _exit;
×
1451
  }
1452

1453
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(cRsp));
38,709,127✔
1454
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
38,713,116✔
1455
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
38,708,498✔
1456
  if (rsp.pArray == NULL || tbUids == NULL || tbNames == NULL) {
38,707,729✔
1457
    rcode = -1;
×
1458
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1459
    goto _exit;
×
1460
  }
1461

1462
  // loop to create table
1463
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
84,854,225✔
1464
    pCreateReq = req.pReqs + iReq;
46,139,236✔
1465
    memset(&cRsp, 0, sizeof(cRsp));
46,134,506✔
1466

1467
    if (tsEnableAudit && tsEnableAuditCreateTable && tsAuditLevel >= AUDIT_LEVEL_CHILDTABLE) {
46,134,506✔
1468
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
65✔
1469
      if (str == NULL) {
65✔
1470
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1471
        rcode = -1;
×
1472
        goto _exit;
×
1473
      }
1474
      tstrncpy(str, pCreateReq->name, TSDB_TABLE_FNAME_LEN);
65✔
1475
      if (taosArrayPush(tbNames, &str) == NULL) {
65✔
1476
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1477
        rcode = -1;
×
1478
        goto _exit;
×
1479
      }
1480
    }
1481

1482
    // validate hash
1483
    (void)tsnprintf(tbName, TSDB_TABLE_FNAME_LEN, "%s.%s", pVnode->config.dbname, pCreateReq->name);
46,134,506✔
1484
    if (vnodeValidateTableHash(pVnode, tbName) < 0) {
46,139,453✔
1485
      cRsp.code = TSDB_CODE_VND_HASH_MISMATCH;
×
1486
      if (taosArrayPush(rsp.pArray, &cRsp) == NULL) {
×
1487
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1488
        rcode = -1;
×
1489
        goto _exit;
×
1490
      }
1491
      vError("vgId:%d create-table:%s failed due to hash value mismatch", TD_VID(pVnode), tbName);
×
1492
      continue;
×
1493
    }
1494

1495
    // do create table
1496
    if (metaCreateTable2(pVnode->pMeta, ver, pCreateReq, &cRsp.pMeta) < 0) {
46,125,372✔
1497
      if (pCreateReq->flags & TD_CREATE_IF_NOT_EXISTS && terrno == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
16,013✔
1498
        cRsp.code = TSDB_CODE_SUCCESS;
3,951✔
1499
      } else {
1500
        cRsp.code = terrno;
12,036✔
1501
      }
1502
    } else {
1503
      cRsp.code = TSDB_CODE_SUCCESS;
46,127,948✔
1504
      if (taosArrayPush(tbUids, &pCreateReq->uid) == NULL) {
92,256,514✔
1505
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1506
        rcode = -1;
×
1507
        goto _exit;
×
1508
      }
1509
      vnodeUpdateMetaRsp(pVnode, cRsp.pMeta);
46,128,566✔
1510
    }
1511

1512
    if (taosArrayPush(rsp.pArray, &cRsp) == NULL) {
92,288,277✔
1513
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1514
      rcode = -1;
×
1515
      goto _exit;
×
1516
    }
1517
  }
1518

1519
  vTrace("vgId:%d, add %d new created tables into query table list", TD_VID(pVnode), (int32_t)taosArrayGetSize(tbUids));
38,714,989✔
1520
  if (tqUpdateTbUidList(pVnode->pTq, tbUids, true) < 0) {
38,714,989✔
1521
    vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(terrno));
×
1522
  }
1523

1524
  // prepare rsp
1525
  int32_t ret = 0;
38,715,397✔
1526
  tEncodeSize(tEncodeSVCreateTbBatchRsp, &rsp, pRsp->contLen, ret);
38,715,397✔
1527
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
38,710,555✔
1528
  if (pRsp->pCont == NULL) {
38,711,909✔
1529
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1530
    rcode = -1;
×
1531
    goto _exit;
×
1532
  }
1533
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
38,710,554✔
1534
  if (tEncodeSVCreateTbBatchRsp(&encoder, &rsp) != 0) {
38,707,179✔
1535
    vError("vgId:%d, failed to encode create table batch response", TD_VID(pVnode));
×
1536
  }
1537

1538
  if (tsEnableAudit && tsEnableAuditCreateTable && tsAuditLevel >= AUDIT_LEVEL_CHILDTABLE) {
38,713,762✔
1539
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
65✔
1540

1541
    SName name = {0};
65✔
1542
    if (tNameFromString(&name, pVnode->config.dbname, T_NAME_ACCT | T_NAME_DB) < 0) {
65✔
1543
      vError("vgId:%d, failed to get name from string", TD_VID(pVnode));
×
1544
    }
1545

1546
    SStringBuilder sb = {0};
65✔
1547
    for (int32_t i = 0; i < tbNames->size; i++) {
130✔
1548
      char **key = (char **)taosArrayGet(tbNames, i);
65✔
1549
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
65✔
1550
      if (i < tbNames->size - 1) {
65✔
1551
        taosStringBuilderAppendChar(&sb, ',');
×
1552
      }
1553
      // taosMemoryFreeClear(*key);
1554
    }
1555

1556
    size_t len = 0;
65✔
1557
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
65✔
1558

1559
    if (strlen(RPC_MSG_USER(pOriginRpc)) > 0) {
65✔
1560
      int64_t tse = taosGetTimestampMs();
65✔
1561
      double  duration = (double)(tse - tss);
65✔
1562
      duration = duration / 1000;
65✔
1563
      auditAddRecord(pOriginRpc, clusterId, "createTable", name.dbname, "", keyJoined, len, duration, 0);
65✔
1564
    }
1565

1566
    taosStringBuilderDestroy(&sb);
65✔
1567
  }
1568

1569
_exit:
38,713,762✔
1570
  tDeleteSVCreateTbBatchReq(&req);
38,708,996✔
1571
  taosArrayDestroyEx(rsp.pArray, tFreeSVCreateTbRsp);
38,706,413✔
1572
  taosArrayDestroy(tbUids);
38,700,479✔
1573
  tDecoderClear(&decoder);
38,702,330✔
1574
  tEncoderClear(&encoder);
38,714,193✔
1575
  taosArrayDestroyP(tbNames, NULL);
38,712,905✔
1576
  return rcode;
38,711,576✔
1577
}
1578

1579
static int32_t vnodeProcessAlterStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
7,135,092✔
1580
  int32_t        code = 0;
7,135,092✔
1581
  SVCreateStbReq req = {0};
7,135,092✔
1582
  SDecoder       dc = {0};
7,135,092✔
1583

1584
  pRsp->msgType = TDMT_VND_ALTER_STB_RSP;
7,135,092✔
1585
  pRsp->code = TSDB_CODE_SUCCESS;
7,135,092✔
1586
  pRsp->pCont = NULL;
7,135,092✔
1587
  pRsp->contLen = 0;
7,135,092✔
1588

1589
  tDecoderInit(&dc, pReq, len);
7,135,092✔
1590

1591
  // decode req
1592
  code = tDecodeSVCreateStbReq(&dc, &req);
7,134,479✔
1593
  if (code) {
7,128,087✔
1594
    tDecoderClear(&dc);
×
1595
    return code;
×
1596
  }
1597

1598
  code = metaAlterSuperTable(pVnode->pMeta, ver, &req);
7,128,087✔
1599
  if (code) {
7,134,533✔
1600
    pRsp->code = code;
×
1601
    tDecoderClear(&dc);
×
1602
    return code;
×
1603
  }
1604

1605
  tDecoderClear(&dc);
7,134,533✔
1606

1607
  return 0;
7,134,533✔
1608
}
1609

1610
static int32_t vnodeProcessDropStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp, SRpcMsg *pOriginRpc) {
862,333✔
1611
  SVDropStbReq req = {0};
862,333✔
1612
  int32_t      rcode = TSDB_CODE_SUCCESS;
862,333✔
1613
  SDecoder     decoder = {0};
862,333✔
1614
  SArray      *tbUidList = NULL;
862,333✔
1615

1616
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
862,333✔
1617
  pRsp->pCont = NULL;
862,333✔
1618
  pRsp->contLen = 0;
862,333✔
1619

1620
  // decode request
1621
  tDecoderInit(&decoder, pReq, len);
862,333✔
1622
  if (tDecodeSVDropStbReq(&decoder, &req) < 0) {
862,333✔
1623
    rcode = TSDB_CODE_INVALID_MSG;
×
1624
    goto _exit;
×
1625
  }
1626

1627
  STraceId* trace = &(pOriginRpc->info.traceId);
862,333✔
1628

1629
  vInfo("vgId:%d, start to process vnode-drop-stb, QID:0x%" PRIx64 ":0x%" PRIx64 ", drop stb:%s", TD_VID(pVnode), trace ? trace->rootId : 0, 
862,333✔
1630
              trace ? trace->msgId : 0, req.name);
1631

1632
  // process request
1633
  tbUidList = taosArrayInit(8, sizeof(int64_t));
862,333✔
1634
  if (tbUidList == NULL) goto _exit;
862,333✔
1635
  if (metaDropSuperTable(pVnode->pMeta, ver, &req) < 0) {
862,333✔
1636
    rcode = terrno;
1,424✔
1637
    goto _exit;
1,424✔
1638
  }
1639

1640
  if (tqUpdateTbUidList(pVnode->pTq, tbUidList, false) < 0) {
860,909✔
1641
    rcode = terrno;
×
1642
    goto _exit;
×
1643
  }
1644

1645
  // return rsp
1646
_exit:
862,333✔
1647
  vInfo("vgId:%d, finished to process vnode-drop-stb, QID:0x%" PRIx64 ":0x%" PRIx64 ", drop stb:%s", TD_VID(pVnode), trace ? trace->rootId : 0, 
862,333✔
1648
              trace ? trace->msgId : 0, req.name);
1649
  if (tbUidList) taosArrayDestroy(tbUidList);
862,333✔
1650
  pRsp->code = rcode;
862,333✔
1651
  tDecoderClear(&decoder);
862,333✔
1652
  return 0;
862,333✔
1653
}
1654

1655
static int32_t vnodeProcessAlterTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
11,473,913✔
1656
  SVAlterTbReq  vAlterTbReq = {0};
11,473,913✔
1657
  SVAlterTbRsp  vAlterTbRsp = {0};
11,473,913✔
1658
  SDecoder      dc = {0};
11,473,913✔
1659
  int32_t       code = 0;
11,473,913✔
1660
  int32_t       lino = 0;
11,473,913✔
1661
  int32_t       ret;
1662
  SEncoder      ec = {0};
11,473,913✔
1663
  STableMetaRsp vMetaRsp = {0};
11,473,913✔
1664

1665
  pRsp->msgType = TDMT_VND_ALTER_TABLE_RSP;
11,473,913✔
1666
  pRsp->pCont = NULL;
11,473,913✔
1667
  pRsp->contLen = 0;
11,473,913✔
1668
  pRsp->code = TSDB_CODE_SUCCESS;
11,473,913✔
1669

1670
  tDecoderInit(&dc, pReq, len);
11,473,913✔
1671

1672
  // decode
1673
  if (tDecodeSVAlterTbReq(&dc, &vAlterTbReq) < 0) {
11,473,913✔
1674
    vAlterTbRsp.code = TSDB_CODE_INVALID_MSG;
×
1675
    tDecoderClear(&dc);
×
1676
    goto _exit;
×
1677
  }
1678

1679
  // process
1680
  if (metaAlterTable(pVnode->pMeta, ver, &vAlterTbReq, &vMetaRsp) < 0) {
11,473,913✔
1681
    vAlterTbRsp.code = terrno;
659,690✔
1682
    tDecoderClear(&dc);
659,690✔
1683
    goto _exit;
659,690✔
1684
  }
1685
  tDecoderClear(&dc);
10,814,223✔
1686

1687
  if (NULL != vMetaRsp.pSchemas) {
10,814,223✔
1688
    vnodeUpdateMetaRsp(pVnode, &vMetaRsp);
3,523,821✔
1689
    vAlterTbRsp.pMeta = &vMetaRsp;
3,523,821✔
1690
  }
1691

1692
  if (vAlterTbReq.action == TSDB_ALTER_TABLE_UPDATE_TAG_VAL ||
10,814,223✔
1693
      vAlterTbReq.action == TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL) {
3,555,939✔
1694
    int64_t uid = metaGetTableEntryUidByName(pVnode->pMeta, vAlterTbReq.tbName);
7,260,804✔
1695
    if (uid == 0) {
7,260,804✔
1696
      vError("vgId:%d, %s failed at %s:%d since table %s not found", TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
1697
             vAlterTbReq.tbName);
1698
      goto _exit;
×
1699
    }
1700

1701
    SArray *tbUids = taosArrayInit(4, sizeof(int64_t));
7,260,804✔
1702
    void   *p = taosArrayPush(tbUids, &uid);
7,260,804✔
1703
    TSDB_CHECK_NULL(p, code, lino, _exit, terrno);
7,260,804✔
1704

1705
    vDebug("vgId:%d, remove tags value altered table:%s from query table list", TD_VID(pVnode), vAlterTbReq.tbName);
7,260,804✔
1706
    if ((code = tqUpdateTbUidList(pVnode->pTq, tbUids, false)) < 0) {
7,260,804✔
1707
      vError("vgId:%d, failed to remove tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1708
    }
1709

1710
    vDebug("vgId:%d, try to add table:%s in query table list", TD_VID(pVnode), vAlterTbReq.tbName);
7,260,804✔
1711
    if ((code = tqUpdateTbUidList(pVnode->pTq, tbUids, true)) < 0) {
7,260,804✔
1712
      vError("vgId:%d, failed to add tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1713
    }
1714

1715
    taosArrayDestroy(tbUids);
7,260,804✔
1716
  }
1717

1718
_exit:
11,473,913✔
1719
  taosArrayDestroy(vAlterTbReq.pMultiTag);
11,473,913✔
1720
  tEncodeSize(tEncodeSVAlterTbRsp, &vAlterTbRsp, pRsp->contLen, ret);
11,473,913✔
1721
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
11,473,913✔
1722
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
11,473,913✔
1723
  if (tEncodeSVAlterTbRsp(&ec, &vAlterTbRsp) != 0) {
11,473,913✔
1724
    vError("vgId:%d, failed to encode alter table response", TD_VID(pVnode));
×
1725
  }
1726

1727
  tEncoderClear(&ec);
11,473,913✔
1728
  if (vMetaRsp.pSchemas) {
11,473,913✔
1729
    taosMemoryFree(vMetaRsp.pSchemas);
3,523,821✔
1730
    taosMemoryFree(vMetaRsp.pSchemaExt);
3,523,821✔
1731
  }
1732
  if (vMetaRsp.pColRefs) {
11,473,913✔
1733
    taosMemoryFree(vMetaRsp.pColRefs);
253,758✔
1734
  }
1735
  return 0;
11,473,913✔
1736
}
1737

1738
static int32_t vnodeProcessDropTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
1,287,122✔
1739
                                     SRpcMsg *pOriginRpc) {
1740
  SVDropTbBatchReq req = {0};
1,287,122✔
1741
  SVDropTbBatchRsp rsp = {0};
1,287,122✔
1742
  SDecoder         decoder = {0};
1,287,122✔
1743
  SEncoder         encoder = {0};
1,287,122✔
1744
  int32_t          ret;
1745
  SArray          *tbUids = NULL;
1,287,122✔
1746
  SArray          *tbNames = NULL;
1,287,122✔
1747
  int64_t          tss = taosGetTimestampMs();
1,287,122✔
1748

1749
  pRsp->msgType = ((SRpcMsg *)pReq)->msgType + 1;
1,287,122✔
1750
  pRsp->pCont = NULL;
1,287,122✔
1751
  pRsp->contLen = 0;
1,287,122✔
1752
  pRsp->code = TSDB_CODE_SUCCESS;
1,287,122✔
1753

1754
  // decode req
1755
  tDecoderInit(&decoder, pReq, len);
1,287,122✔
1756
  ret = tDecodeSVDropTbBatchReq(&decoder, &req);
1,287,122✔
1757
  if (ret < 0) {
1,287,122✔
1758
    terrno = TSDB_CODE_INVALID_MSG;
×
1759
    pRsp->code = terrno;
×
1760
    goto _exit;
×
1761
  }
1762

1763
  // process req
1764
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
1,287,122✔
1765
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(SVDropTbRsp));
1,287,122✔
1766
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
1,287,122✔
1767
  if (tbUids == NULL || rsp.pArray == NULL || tbNames == NULL) goto _exit;
1,287,122✔
1768

1769
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
2,625,778✔
1770
    SVDropTbReq *pDropTbReq = req.pReqs + iReq;
1,338,656✔
1771
    SVDropTbRsp  dropTbRsp = {0};
1,338,656✔
1772
    tb_uid_t     tbUid = 0;
1,338,656✔
1773

1774
    /* code */
1775
    ret = metaDropTable2(pVnode->pMeta, ver, pDropTbReq);
1,338,656✔
1776
    if (ret < 0) {
1,338,656✔
1777
      if (pDropTbReq->igNotExists && terrno == TSDB_CODE_TDB_TABLE_NOT_EXIST) {
×
1778
        dropTbRsp.code = TSDB_CODE_SUCCESS;
×
1779
      } else {
1780
        dropTbRsp.code = terrno;
×
1781
      }
1782
    } else {
1783
      dropTbRsp.code = TSDB_CODE_SUCCESS;
1,338,656✔
1784
    }
1785

1786
    if (taosArrayPush(rsp.pArray, &dropTbRsp) == NULL) {
2,677,312✔
1787
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1788
      pRsp->code = terrno;
×
1789
      goto _exit;
×
1790
    }
1791

1792
    if (tsEnableAudit && tsEnableAuditCreateTable && tsAuditLevel >= AUDIT_LEVEL_CHILDTABLE) {
1,338,656✔
1793
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
×
1794
      if (str == NULL) {
×
1795
        pRsp->code = terrno;
×
1796
        goto _exit;
×
1797
      }
1798
      tstrncpy(str, pDropTbReq->name, TSDB_TABLE_FNAME_LEN);
×
1799
      if (taosArrayPush(tbNames, &str) == NULL) {
×
1800
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1801
        pRsp->code = terrno;
×
1802
        goto _exit;
×
1803
      }
1804
    }
1805
  }
1806

1807
  if (tqUpdateTbUidList(pVnode->pTq, tbUids, false) < 0) {
1,287,122✔
1808
    vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(terrno));
×
1809
  }
1810

1811
  if (tsEnableAudit && tsEnableAuditCreateTable && tsAuditLevel >= AUDIT_LEVEL_CHILDTABLE) {
1,287,122✔
1812
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
×
1813

1814
    SName name = {0};
×
1815
    if (tNameFromString(&name, pVnode->config.dbname, T_NAME_ACCT | T_NAME_DB) != 0) {
×
1816
      vError("vgId:%d, failed to get name from string", TD_VID(pVnode));
×
1817
    }
1818

1819
    SStringBuilder sb = {0};
×
1820
    for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
×
1821
      char **key = (char **)taosArrayGet(tbNames, iReq);
×
1822
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
×
1823
      if (iReq < req.nReqs - 1) {
×
1824
        taosStringBuilderAppendChar(&sb, ',');
×
1825
      }
1826
      taosMemoryFreeClear(*key);
×
1827
    }
1828

1829
    size_t len = 0;
×
1830
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
×
1831

1832
    if (strlen(RPC_MSG_USER(pOriginRpc)) > 0) {
×
1833
      int64_t tse = taosGetTimestampMs();
×
1834
      double  duration = (double)(tse - tss);
×
1835
      duration = duration / 1000;
×
1836
      auditAddRecord(pOriginRpc, clusterId, "dropTable", name.dbname, "", keyJoined, len, duration, 0);
×
1837
    }
1838

1839
    taosStringBuilderDestroy(&sb);
×
1840
  }
1841

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

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

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

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

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

1907
  tInitSubmitBlkIter(msgIter, pBlock, &blkIter);
1908
  if (blkIter.row == NULL) return 0;
1909

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

1917
  suid = msgIter->suid;
1918
  rv = TD_ROW_SVER(blkIter.row);
1919

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

1926
  taosMemoryFreeClear(pSchema);
1927

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

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

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

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

1980
  return TSDB_CODE_SUCCESS;
×
1981
}
1982

1983
static void vnodeDestroySubmitReqConvertCxt(SSubmitReqConvertCxt *pCxt) {
×
1984
  taosMemoryFreeClear(pCxt->pTbSchema);
×
1985
  tDestroySubmitTbData(pCxt->pTbData, TSDB_MSG_FLG_ENCODE);
×
1986
  taosMemoryFreeClear(pCxt->pTbData);
×
1987
  taosArrayDestroy(pCxt->pColValues);
×
1988
}
×
1989

1990
static int32_t vnodeCellValConvertToColVal(STColumn *pCol, SCellVal *pCellVal, SColVal *pColVal) {
×
1991
  if (tdValTypeIsNone(pCellVal->valType)) {
×
1992
    pColVal->flag = CV_FLAG_NONE;
×
1993
    return TSDB_CODE_SUCCESS;
×
1994
  }
1995

1996
  if (tdValTypeIsNull(pCellVal->valType)) {
×
1997
    pColVal->flag = CV_FLAG_NULL;
×
1998
    return TSDB_CODE_SUCCESS;
×
1999
  }
2000

2001
  if (IS_VAR_DATA_TYPE(pCol->type)) {
×
2002
    if (IS_STR_DATA_BLOB(pCol->type)) {
×
2003
      pColVal->value.nData = blobDataLen(pCellVal->val);
×
2004
      pColVal->value.pData = (uint8_t *)blobDataVal(pCellVal->val);
×
2005

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

2019
  pColVal->flag = CV_FLAG_VALUE;
×
2020
  return TSDB_CODE_SUCCESS;
×
2021
}
2022

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

2037
static int32_t vnodeDecodeCreateTbReq(SSubmitReqConvertCxt *pCxt) {
×
2038
  if (pCxt->msgIter.schemaLen <= 0) {
×
2039
    return TSDB_CODE_SUCCESS;
×
2040
  }
2041

2042
  pCxt->pTbData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
×
2043
  if (NULL == pCxt->pTbData->pCreateTbReq) {
×
2044
    return terrno;
×
2045
  }
2046

2047
  SDecoder decoder = {0};
×
2048
  tDecoderInit(&decoder, (uint8_t *)pCxt->pBlock->data, pCxt->msgIter.schemaLen);
×
2049
  int32_t code = tDecodeSVCreateTbReq(&decoder, pCxt->pTbData->pCreateTbReq);
×
2050
  tDecoderClear(&decoder);
×
2051

2052
  return code;
×
2053
}
2054

2055
static int32_t vnodeSubmitReqConvertToSubmitReq2(SVnode *pVnode, SSubmitReq *pReq, SSubmitReq2 *pReq2) {
×
2056
  pReq2->aSubmitTbData = taosArrayInit(128, sizeof(SSubmitTbData));
×
2057
  if (NULL == pReq2->aSubmitTbData) {
×
2058
    return terrno;
×
2059
  }
2060

2061
  SSubmitReqConvertCxt cxt = {0};
×
2062

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

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

2095
  vnodeDestroySubmitReqConvertCxt(&cxt);
×
2096
  return code;
×
2097
}
2098

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

2122
static int32_t buildExistSubTalbeRsp(SVnode *pVnode, SSubmitTbData *pSubmitTbData, STableMetaRsp **ppRsp) {
57,039✔
2123
  int32_t code = 0;
57,039✔
2124
  int32_t lino = 0;
57,039✔
2125

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

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

2170
  if (pEntry->stbEntry.schemaRow.version != pSubmitTbData->sver) {
57,039✔
2171
    TSDB_CHECK_CODE(code = TSDB_CODE_TDB_TABLE_ALREADY_EXIST, lino, _exit);
418✔
2172
  }
2173
_exit:
57,039✔
2174
  metaFetchEntryFree(&pEntry);
57,039✔
2175
  if (code != TSDB_CODE_SUCCESS) {
57,039✔
2176
    vError("vgId:%d, failed to build exist sub table response, code:0x%0x, line:%d", TD_VID(pVnode), code, lino);
418✔
2177
  }
2178
  return code;
57,039✔
2179
}
2180

2181
static int32_t buildExistNormalTalbeRsp(SVnode *pVnode, int64_t uid, STableMetaRsp **ppRsp) {
32,548✔
2182
  int32_t code = 0;
32,548✔
2183
  int32_t lino = 0;
32,548✔
2184

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

2197
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
32,548✔
2198
  if (NULL == *ppRsp) {
32,548✔
2199
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2200
  }
2201

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

2222
_exit:
32,548✔
2223
  metaFetchEntryFree(&pEntry);
32,548✔
2224
  if (code != TSDB_CODE_SUCCESS) {
32,548✔
2225
    vError("vgId:%d, failed to build exist normal table response, code:%d, line:%d", TD_VID(pVnode), code, lino);
×
2226
  }
2227
  return code;
32,548✔
2228
}
2229

2230
static int32_t buildExistTableInStreamRsp(SVnode *pVnode, SSubmitTbData *pSubmitTbData, STableMetaRsp **ppRsp) {
279,004✔
2231
  if (pSubmitTbData->pCreateTbReq->flags & TD_CREATE_NORMAL_TB_IN_STREAM) {
279,004✔
2232
    int32_t code = buildExistNormalTalbeRsp(pVnode, pSubmitTbData->uid, ppRsp);
32,548✔
2233
    if (code) {
32,548✔
2234
      vError("vgId:%d, table uid:%" PRId64 " not exists, line:%d", TD_VID(pVnode), pSubmitTbData->uid, __LINE__);
×
2235
      return code;
×
2236
    }
2237
    return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
32,548✔
2238
  } else if (pSubmitTbData->pCreateTbReq->flags & TD_CREATE_SUB_TB_IN_STREAM) {
246,456✔
2239
    return buildExistSubTalbeRsp(pVnode, pSubmitTbData, ppRsp);
57,039✔
2240
  }
2241
  return TSDB_CODE_SUCCESS;
189,417✔
2242
}
2243

2244
static int32_t vnodeHandleAutoCreateTable(SVnode      *pVnode,    // vnode
645,108,560✔
2245
                                          int64_t      version,   // version
2246
                                          SSubmitReq2 *pRequest,  // request
2247
                                          SSubmitRsp2 *pResponse  // response
2248
) {
2249
  int32_t code = TSDB_CODE_SUCCESS;
645,108,560✔
2250
  int32_t numTbData = taosArrayGetSize(pRequest->aSubmitTbData);
645,108,560✔
2251
  SArray *newTbUids = NULL;
645,111,344✔
2252

2253
  for (int32_t i = 0; i < numTbData; ++i) {
1,316,402,412✔
2254
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
671,326,165✔
2255

2256
    if (pTbData->pCreateTbReq == NULL) {
671,327,541✔
2257
      continue;
657,191,552✔
2258
    }
2259

2260
    pTbData->uid = pTbData->pCreateTbReq->uid;
14,134,975✔
2261

2262
    // Alloc necessary resources
2263
    if (pResponse->aCreateTbRsp == NULL) {
14,134,975✔
2264
      pResponse->aCreateTbRsp = taosArrayInit(numTbData, sizeof(SVCreateTbRsp));
11,413,758✔
2265
      if (pResponse->aCreateTbRsp == NULL) {
11,413,758✔
2266
        code = terrno;
×
2267
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2268
               tstrerror(code), version);
2269
        taosArrayDestroy(newTbUids);
×
2270
        return code;
×
2271
      }
2272
    }
2273

2274
    // Do create table
2275
    vDebug("vgId:%d start to handle auto create table, version:%" PRId64, TD_VID(pVnode), version);
14,135,012✔
2276

2277
    SVCreateTbRsp *pCreateTbRsp = taosArrayReserve(pResponse->aCreateTbRsp, 1);
14,135,049✔
2278
    code = metaCreateTable2(pVnode->pMeta, version, pTbData->pCreateTbReq, &pCreateTbRsp->pMeta);
14,135,012✔
2279
    if (code == TSDB_CODE_SUCCESS) {
14,134,168✔
2280
      // Allocate necessary resources
2281
      if (newTbUids == NULL) {
13,804,592✔
2282
        newTbUids = taosArrayInit(numTbData, sizeof(int64_t));
11,134,514✔
2283
        if (newTbUids == NULL) {
11,134,821✔
2284
          code = terrno;
×
2285
          vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2286
                 tstrerror(code), version);
2287
          return code;
×
2288
        }
2289
      }
2290

2291
      if (taosArrayPush(newTbUids, &pTbData->uid) == NULL) {
27,610,298✔
2292
        code = terrno;
×
2293
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2294
               tstrerror(code), version);
2295
        taosArrayDestroy(newTbUids);
×
2296
        return code;
×
2297
      }
2298

2299
      if (pCreateTbRsp->pMeta) {
13,805,399✔
2300
        vnodeUpdateMetaRsp(pVnode, pCreateTbRsp->pMeta);
13,804,899✔
2301
      }
2302
    } else if (code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
329,576✔
2303
      code = terrno = 0;
328,576✔
2304
      pTbData->uid = pTbData->pCreateTbReq->uid;  // update uid if table exist for using below
328,613✔
2305

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

2327
  // Update the affected table uid list
2328
  if (taosArrayGetSize(newTbUids) > 0) {
645,076,247✔
2329
    vDebug("vgId:%d, add %d table into query table list in handling submit", TD_VID(pVnode),
11,133,900✔
2330
           (int32_t)taosArrayGetSize(newTbUids));
2331
    if (tqUpdateTbUidList(pVnode->pTq, newTbUids, true) != 0) {
11,134,821✔
2332
      vError("vgId:%d, failed to update tbUid list", TD_VID(pVnode));
×
2333
    }
2334
  }
2335

2336
  vDebug("vgId:%d, handle auto create table done, version:%" PRId64, TD_VID(pVnode), version);
645,077,182✔
2337

2338
  taosArrayDestroy(newTbUids);
645,077,240✔
2339
  return code;
645,077,240✔
2340
}
2341

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

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

2380
static int32_t vnodeHandleDataWrite(SVnode *pVnode, int64_t version, SSubmitReq2 *pRequest, SSubmitRsp2 *pResponse) {
645,077,240✔
2381
  int32_t code = TSDB_CODE_SUCCESS;
645,077,240✔
2382
  int32_t numTbData = taosArrayGetSize(pRequest->aSubmitTbData);
645,077,240✔
2383
  int8_t  hasBlob = 0;
645,077,269✔
2384

2385
  // Scan submit data
2386
  for (int32_t i = 0; i < numTbData; ++i) {
1,316,363,864✔
2387
    SMetaInfo      info = {0};
671,290,984✔
2388
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
671,291,455✔
2389

2390
    if (pTbData->flags & SUBMIT_REQ_WITH_BLOB) {
671,290,762✔
2391
      hasBlob = 1;
21,179✔
2392
    }
2393
    if (pTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
671,291,504✔
2394
      continue;  // skip column data format
6,282,127✔
2395
    }
2396
    if (pTbData->flags & SUBMIT_REQ_ONLY_CREATE_TABLE) {
665,008,676✔
2397
      continue;  // skip only crate table request
2,271,122✔
2398
    }
2399

2400
    code = metaGetInfo(pVnode->pMeta, pTbData->uid, &info, NULL);
662,737,585✔
2401
    if (code) {
662,737,835✔
2402
      code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
3,543✔
2403
      vWarn("vgId:%d, error occurred at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __FILE__,
3,543✔
2404
            __LINE__, tstrerror(code), version, pTbData->uid);
2405
      return code;
3,543✔
2406
    }
2407

2408
    if (info.suid != pTbData->suid) {
662,734,292✔
2409
      code = TSDB_CODE_INVALID_MSG;
×
2410
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64 " suid:%" PRId64
×
2411
             " info.suid:%" PRId64,
2412
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), version, pTbData->uid, pTbData->suid,
2413
             info.suid);
2414
      return code;
×
2415
    }
2416

2417
    if (info.suid) {
662,732,812✔
2418
      code = metaGetInfo(pVnode->pMeta, info.suid, &info, NULL);
501,218,602✔
2419
      if (code) {
501,222,133✔
2420
        code = TSDB_CODE_INTERNAL_ERROR;
×
2421
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " suid:%" PRId64, TD_VID(pVnode), __func__,
×
2422
               __FILE__, __LINE__, tstrerror(code), version, info.suid);
2423
        return code;
×
2424
      }
2425
    }
2426

2427
    if (pTbData->sver != info.skmVer) {
662,736,343✔
2428
      code = TSDB_CODE_TDB_INVALID_TABLE_SCHEMA_VER;
418✔
2429
      addExistTableInfoIntoRes(pVnode, pRequest, pResponse, pTbData, numTbData);
418✔
2430
      vDebug("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64
418✔
2431
             " sver:%d"
2432
             " info.skmVer:%d",
2433
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), version, pTbData->uid, pTbData->sver,
2434
             info.skmVer);
2435
      return code;
418✔
2436
    }
2437
  }
2438

2439
  // Do write data
2440
  vDebug("vgId:%d start to handle data write, version:%" PRId64, TD_VID(pVnode), version);
645,072,880✔
2441

2442
  for (int32_t i = 0; i < numTbData; ++i) {
1,316,344,715✔
2443
    int32_t        affectedRows = 0;
671,286,923✔
2444
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
671,277,161✔
2445

2446
    if (pTbData->flags & SUBMIT_REQ_ONLY_CREATE_TABLE) {
671,287,460✔
2447
      continue;
2,271,122✔
2448
    }
2449

2450
    if (hasBlob) {
669,015,499✔
2451
      code = vnodeSubmitBlobData(pVnode, pTbData);
21,179✔
2452
      if (code) {
21,179✔
2453
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2454
               __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2455
        return code;
×
2456
      }
2457
    }
2458

2459
    code = tsdbInsertTableData(pVnode->pTsdb, version, pTbData, &affectedRows);
669,015,499✔
2460
    if (code) {
669,001,385✔
2461
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2462
             __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2463
      return code;
×
2464
    }
2465

2466
    code = metaUpdateChangeTimeWithLock(pVnode->pMeta, pTbData->uid, pTbData->ctimeMs);
669,001,385✔
2467
    if (code) {
668,986,291✔
2468
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2469
             __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2470
      return code;
×
2471
    }
2472
    pResponse->affectedRows += affectedRows;
668,986,291✔
2473
  }
2474

2475
  vDebug("vgId:%d, handle data write done, version:%" PRId64 ", affectedRows:%d", TD_VID(pVnode), version,
645,057,792✔
2476
         pResponse->affectedRows);
2477
  return code;
645,074,256✔
2478
}
2479

2480
static int32_t vnodeScanColumnData(SVnode *pVnode, SSubmitTbData *pTbData, TSKEY minKey, TSKEY maxKey) {
6,281,206✔
2481
  int32_t code = 0;
6,281,206✔
2482

2483
  int32_t   numCols = taosArrayGetSize(pTbData->aCol);
6,281,206✔
2484
  SColData *aColData = (SColData *)TARRAY_DATA(pTbData->aCol);
6,280,092✔
2485

2486
  if (numCols <= 0) {
6,280,899✔
2487
    code = TSDB_CODE_INVALID_MSG;
×
2488
    vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " numCols:%d", TD_VID(pVnode), __func__,
×
2489
           __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, numCols);
2490
    return code;
×
2491
  }
2492

2493
  if (aColData[0].cid != PRIMARYKEY_TIMESTAMP_COL_ID || aColData[0].type != TSDB_DATA_TYPE_TIMESTAMP ||
6,280,899✔
2494
      aColData[0].nVal <= 0) {
6,280,582✔
2495
    code = TSDB_CODE_INVALID_MSG;
5✔
2496
    vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64
5✔
2497
           " first column is not primary key timestamp, cid:%d type:%d nVal:%d",
2498
           TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, aColData[0].cid,
2499
           aColData[0].type, aColData[0].nVal);
2500
    return code;
×
2501
  }
2502

2503
  for (int32_t i = 1; i < numCols; ++i) {
19,302,511✔
2504
    if (aColData[i].nVal != aColData[0].nVal) {
13,022,450✔
2505
      code = TSDB_CODE_INVALID_MSG;
×
2506
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64
×
2507
             " column cid:%d type:%d nVal:%d is not equal to primary key timestamp nVal:%d",
2508
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid,
2509
             aColData[i].cid, aColData[i].type, aColData[i].nVal, aColData[0].nVal);
2510
      return code;
×
2511
    }
2512
  }
2513

2514
  SRowKey *pLastKey = NULL;
6,280,061✔
2515
  SRowKey  lastKey = {0};
6,280,061✔
2516
  for (int32_t i = 0; i < aColData[0].nVal; ++i) {
2,147,483,647✔
2517
    SRowKey key = {0};
2,147,483,647✔
2518

2519
    tColDataArrGetRowKey(aColData, numCols, i, &key);
2,147,483,647✔
2520

2521
    if (key.ts < minKey || key.ts > maxKey) {
2,147,483,647✔
2522
      code = TSDB_CODE_INVALID_MSG;
×
2523
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] key:%" PRId64
×
2524
             " is out of range [%" PRId64 ", %" PRId64 "]",
2525
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, i, key.ts,
2526
             minKey, maxKey);
2527
      return code;
×
2528
    }
2529

2530
    if (pLastKey && tRowKeyCompare(pLastKey, &key) >= 0) {
2,147,483,647✔
2531
      code = TSDB_CODE_INVALID_MSG;
×
2532
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] key:%" PRId64
×
2533
             " is not in order, lastKey:%" PRId64,
2534
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, i, key.ts,
2535
             pLastKey->ts);
2536
      return code;
×
2537
    } else if (pLastKey == NULL) {
2,147,483,647✔
2538
      pLastKey = &lastKey;
6,281,013✔
2539
    }
2540

2541
    *pLastKey = key;
2,147,483,647✔
2542
  }
2543

2544
  return code;
6,282,127✔
2545
}
2546

2547
static int32_t vnodeScanSubmitRowData(SVnode *pVnode, SSubmitTbData *pTbData, TSKEY minKey, TSKEY maxKey) {
665,035,129✔
2548
  int32_t code = 0;
665,035,129✔
2549

2550
  int32_t numRows = taosArrayGetSize(pTbData->aRowP);
665,035,129✔
2551
  SRow  **aRow = (SRow **)TARRAY_DATA(pTbData->aRowP);
665,025,967✔
2552

2553
  if (numRows <= 0 && (pTbData->flags & SUBMIT_REQ_ONLY_CREATE_TABLE) == 0) {
665,021,737✔
2554
    code = TSDB_CODE_INVALID_MSG;
×
2555
    vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " numRows:%d", TD_VID(pVnode), __func__,
×
2556
           __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, numRows);
2557
    return code;
×
2558
  }
2559

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

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

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

2594
    *pLastKey = key;
2,147,483,647✔
2595
  }
2596

2597
  return code;
655,225,225✔
2598
}
2599

2600
static int32_t vnodeScanSubmitReq(SVnode *pVnode, int64_t version, SSubmitReq2 *pRequest, SSubmitRsp2 *pResponse) {
645,098,150✔
2601
  int32_t code = TSDB_CODE_SUCCESS;
645,098,150✔
2602
  int32_t numTbData = taosArrayGetSize(pRequest->aSubmitTbData);
645,098,150✔
2603

2604
  TSKEY now = taosGetTimestamp(pVnode->config.tsdbCfg.precision);
645,094,293✔
2605
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * pVnode->config.tsdbCfg.keep2;
645,103,536✔
2606
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
645,099,869✔
2607
  for (int32_t i = 0; i < numTbData; i++) {
1,316,394,858✔
2608
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
671,313,659✔
2609

2610
    if (pTbData->pCreateTbReq && pTbData->pCreateTbReq->uid == 0) {
671,311,165✔
2611
      code = TSDB_CODE_INVALID_MSG;
×
2612
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2613
             tstrerror(code), version);
2614
      return code;
×
2615
    }
2616

2617
    if (pTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
671,313,123✔
2618
      code = vnodeScanColumnData(pVnode, pTbData, minKey, maxKey);
6,281,206✔
2619
      if (code) {
6,281,820✔
2620
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2621
               __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2622
        return code;
×
2623
      }
2624
    } else {
2625
      code = vnodeScanSubmitRowData(pVnode, pTbData, minKey, maxKey);
665,031,832✔
2626
      if (code) {
665,015,676✔
2627
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
3,953✔
2628
               __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2629
        return code;
×
2630
      }
2631
    }
2632
  }
2633

2634
  return code;
645,081,199✔
2635
}
2636

2637
static int32_t vnodeProcessSubmitReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
645,109,721✔
2638
                                     SRpcMsg *pOriginalMsg) {
2639
  int32_t code = 0;
645,109,721✔
2640
  int32_t lino = 0;
645,109,721✔
2641
  terrno = 0;
645,109,721✔
2642

2643
  SSubmitReq2 *pSubmitReq = &(SSubmitReq2){0};
645,112,183✔
2644
  SSubmitRsp2 *pSubmitRsp = &(SSubmitRsp2){0};
645,112,183✔
2645
  int32_t      ret;
2646
  SEncoder     ec = {0};
645,110,781✔
2647

2648
  pRsp->code = TSDB_CODE_SUCCESS;
645,110,505✔
2649

2650
  void           *pAllocMsg = NULL;
645,111,344✔
2651
  SSubmitReq2Msg *pMsg = (SSubmitReq2Msg *)pReq;
645,111,344✔
2652
  SDecoder        dc = {0};
645,111,344✔
2653
  if (0 == taosHton64(pMsg->version)) {
645,111,344✔
2654
    code = vnodeSubmitReqConvertToSubmitReq2(pVnode, (SSubmitReq *)pMsg, pSubmitReq);
×
2655
    if (TSDB_CODE_SUCCESS == code) {
×
2656
      code = vnodeRebuildSubmitReqMsg(pSubmitReq, &pReq);
×
2657
    }
2658
    if (TSDB_CODE_SUCCESS == code) {
×
2659
      pAllocMsg = pReq;
×
2660
    }
2661
    TSDB_CHECK_CODE(code, lino, _exit);
×
2662
  } else {
2663
    // decode
2664
    pReq = POINTER_SHIFT(pReq, sizeof(SSubmitReq2Msg));
645,111,344✔
2665
    len -= sizeof(SSubmitReq2Msg);
645,112,045✔
2666

2667
    tDecoderInit(&dc, pReq, len);
645,112,045✔
2668
    if (tDecodeSubmitReq(&dc, pSubmitReq, NULL) < 0) {
645,110,861✔
2669
      code = TSDB_CODE_INVALID_MSG;
×
2670
      TSDB_CHECK_CODE(code, lino, _exit);
×
2671
    }
2672
  }
2673

2674
  // Scan the request
2675
  code = vnodeScanSubmitReq(pVnode, ver, pSubmitReq, pSubmitRsp);
645,100,548✔
2676
  if (code) {
645,080,833✔
2677
    vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2678
           tstrerror(code), ver);
2679
    TSDB_CHECK_CODE(code, lino, _exit);
×
2680
  }
2681

2682
  vGDebug(pOriginalMsg ? &pOriginalMsg->info.traceId : NULL, "vgId:%d, index:%" PRId64 ", submit block, rows:%d",
645,080,833✔
2683
          TD_VID(pVnode), ver, (int32_t)taosArrayGetSize(pSubmitReq->aSubmitTbData));
2684

2685
  // Handle auto create table
2686
  code = vnodeHandleAutoCreateTable(pVnode, ver, pSubmitReq, pSubmitRsp);
645,096,643✔
2687
  if (code) {
645,110,660✔
2688
    vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
33,966✔
2689
           tstrerror(code), ver);
2690
    TSDB_CHECK_CODE(code, lino, _exit);
33,966✔
2691
  }
2692

2693
  // Handle data write
2694
  code = vnodeHandleDataWrite(pVnode, ver, pSubmitReq, pSubmitRsp);
645,076,694✔
2695
  if (code) {
645,078,217✔
2696
    vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
3,961✔
2697
           tstrerror(code), ver);
2698
    TSDB_CHECK_CODE(code, lino, _exit);
3,961✔
2699
  }
2700

2701
_exit:
645,112,039✔
2702
  // message
2703
  pRsp->code = code;
645,112,183✔
2704
  tEncodeSize(tEncodeSSubmitRsp2, pSubmitRsp, pRsp->contLen, ret);
645,112,183✔
2705
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
645,110,762✔
2706
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
645,110,438✔
2707
  if (tEncodeSSubmitRsp2(&ec, pSubmitRsp) < 0) {
645,109,345✔
2708
    vError("vgId:%d, failed to encode submit response", TD_VID(pVnode));
×
2709
  }
2710
  tEncoderClear(&ec);
645,108,985✔
2711

2712
  // update statistics
2713
  (void)atomic_add_fetch_64(&pVnode->statis.nInsert, pSubmitRsp->affectedRows);
645,107,822✔
2714
  (void)atomic_add_fetch_64(&pVnode->statis.nInsertSuccess, pSubmitRsp->affectedRows);
645,110,150✔
2715
  (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsert, 1);
645,109,933✔
2716

2717
  // update metrics
2718
  METRICS_UPDATE(pVnode->writeMetrics.total_requests, METRIC_LEVEL_LOW, 1);
645,109,275✔
2719
  METRICS_UPDATE(pVnode->writeMetrics.total_rows, METRIC_LEVEL_HIGH, pSubmitRsp->affectedRows);
645,106,218✔
2720
  METRICS_UPDATE(pVnode->writeMetrics.total_bytes, METRIC_LEVEL_LOW, pMsg->header.contLen);
645,106,301✔
2721

2722
  if (tsEnableMonitor && tsMonitorFqdn[0] != 0 && tsMonitorPort != 0 && pSubmitRsp->affectedRows > 0 &&
645,106,320✔
2723
      strlen(RPC_MSG_USER(pOriginalMsg)) > 0 && tsInsertCounter != NULL) {
×
2724
    const char *sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT_AFFECTED_ROWS,
×
2725
                                   pVnode->monitor.strClusterId,
×
2726
                                   pVnode->monitor.strDnodeId,
×
2727
                                   tsLocalEp,
2728
                                   pVnode->monitor.strVgId,
×
2729
                                   RPC_MSG_USER(pOriginalMsg),
×
2730
                                   "Success"};
2731
    int         tv = taos_counter_add(tsInsertCounter, pSubmitRsp->affectedRows, sample_labels);
×
2732
  }
2733

2734
  if (code == 0) {
645,104,913✔
2735
    (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
645,066,986✔
2736
  }
2737

2738
  // clear
2739
  tDestroySubmitReq(pSubmitReq, 0 == taosHton64(pMsg->version) ? TSDB_MSG_FLG_CMPT : TSDB_MSG_FLG_DECODE);
645,110,142✔
2740
  tDestroySSubmitRsp2(pSubmitRsp, TSDB_MSG_FLG_ENCODE);
645,098,282✔
2741

2742
  if (code) {
645,087,113✔
2743
    terrno = code;
37,927✔
2744
    if (code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
37,927✔
2745
      vInfo("vgId:%d, failed to process submit request since %s, lino:%d, version:%" PRId64, TD_VID(pVnode),
32,966✔
2746
            tstrerror(code), lino, ver);
2747

2748
    } else {
2749
      vError("vgId:%d, failed to process submit request since %s, lino:%d, version:%" PRId64, TD_VID(pVnode),
4,961✔
2750
             tstrerror(code), lino, ver);
2751
    }
2752
  }
2753

2754
  taosMemoryFree(pAllocMsg);
645,098,413✔
2755
  tDecoderClear(&dc);
645,089,782✔
2756

2757
  return code;
645,108,815✔
2758
}
2759

2760
static int32_t vnodeConsolidateAlterHashRange(SVnode *pVnode, int64_t ver) {
23,482✔
2761
  int32_t code = TSDB_CODE_SUCCESS;
23,482✔
2762

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

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

2769
  return code;
23,482✔
2770
}
2771

2772
static int32_t vnodeProcessAlterConfirmReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
3,829,664✔
2773
  vInfo("vgId:%d, vnode handle msgType:alter-confirm, alter confirm msg is processed", TD_VID(pVnode));
3,829,664✔
2774
  int32_t code = TSDB_CODE_SUCCESS;
3,829,664✔
2775
  if (!pVnode->config.hashChange) {
3,829,664✔
2776
    goto _exit;
3,806,182✔
2777
  }
2778

2779
  code = vnodeConsolidateAlterHashRange(pVnode, ver);
23,482✔
2780
  if (code < 0) {
23,482✔
2781
    vError("vgId:%d, failed to consolidate alter hashrange since %s. version:%" PRId64, TD_VID(pVnode), terrstr(), ver);
×
2782
    goto _exit;
×
2783
  }
2784
  pVnode->config.hashChange = false;
23,482✔
2785

2786
_exit:
3,829,664✔
2787
  pRsp->msgType = TDMT_VND_ALTER_CONFIRM_RSP;
3,829,664✔
2788
  pRsp->code = code;
3,829,664✔
2789
  pRsp->pCont = NULL;
3,829,664✔
2790
  pRsp->contLen = 0;
3,829,664✔
2791

2792
  return code;
3,829,664✔
2793
}
2794

2795
extern int32_t tsdbDisableAndCancelAllBgTask(STsdb *pTsdb);
2796
extern void    tsdbEnableBgTask(STsdb *pTsdb);
2797

2798
static int32_t vnodeProcessAlterConfigReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
303,228✔
2799
  bool walChanged = false;
303,228✔
2800
  bool tsdbChanged = false;
303,228✔
2801

2802
  SAlterVnodeConfigReq req = {0};
303,228✔
2803
  if (tDeserializeSAlterVnodeConfigReq(pReq, len, &req) != 0) {
303,902✔
2804
    terrno = TSDB_CODE_INVALID_MSG;
×
2805
    return TSDB_CODE_INVALID_MSG;
×
2806
  }
2807

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

2817
  if (pVnode->config.cacheLastSize != req.cacheLastSize) {
303,902✔
2818
    pVnode->config.cacheLastSize = req.cacheLastSize;
5,686✔
2819
    tsdbCacheSetCapacity(pVnode, (size_t)pVnode->config.cacheLastSize * 1024 * 1024);
5,686✔
2820
  }
2821

2822
  if (pVnode->config.szBuf != req.buffer * 1024LL * 1024LL) {
303,902✔
2823
    vInfo("vgId:%d, vnode buffer is changed from %" PRId64 " to %" PRId64, TD_VID(pVnode), pVnode->config.szBuf,
8,642✔
2824
          (uint64_t)(req.buffer * 1024LL * 1024LL));
2825
    pVnode->config.szBuf = req.buffer * 1024LL * 1024LL;
8,642✔
2826
  }
2827

2828
  if (pVnode->config.szCache != req.pages) {
303,902✔
2829
    if ((terrno = metaAlterCache(pVnode->pMeta, req.pages)) < 0) {
4,356✔
2830
      vError("vgId:%d, failed to change vnode pages from %d to %d failed since %s", TD_VID(pVnode),
×
2831
             pVnode->config.szCache, req.pages, tstrerror(terrno));
2832
      return terrno;
×
2833
    } else {
2834
      vInfo("vgId:%d, vnode pages is changed from %d to %d", TD_VID(pVnode), pVnode->config.szCache, req.pages);
4,356✔
2835
      pVnode->config.szCache = req.pages;
4,356✔
2836
    }
2837
  }
2838

2839
  if (pVnode->config.cacheLast != req.cacheLast) {
303,902✔
2840
    pVnode->config.cacheLast = req.cacheLast;
83,294✔
2841
  }
2842

2843
  if (pVnode->config.walCfg.fsyncPeriod != req.walFsyncPeriod) {
303,902✔
2844
    pVnode->config.walCfg.fsyncPeriod = req.walFsyncPeriod;
50,040✔
2845
    walChanged = true;
50,040✔
2846
  }
2847

2848
  if (pVnode->config.walCfg.level != req.walLevel) {
303,902✔
2849
    if (pVnode->config.walCfg.level == 0) {
36,879✔
2850
      pVnode->config.walCfg.clearFiles = 1;
2,113✔
2851
    }
2852
    pVnode->config.walCfg.level = req.walLevel;
36,879✔
2853
    walChanged = true;
36,879✔
2854
  }
2855

2856
  if (pVnode->config.walCfg.retentionPeriod != req.walRetentionPeriod) {
303,902✔
2857
    pVnode->config.walCfg.retentionPeriod = req.walRetentionPeriod;
45,155✔
2858
    walChanged = true;
45,155✔
2859
  }
2860

2861
  if (pVnode->config.walCfg.retentionSize != req.walRetentionSize) {
303,902✔
2862
    pVnode->config.walCfg.retentionSize = req.walRetentionSize;
1,382✔
2863
    walChanged = true;
1,382✔
2864
  }
2865

2866
  if (pVnode->config.tsdbCfg.keep0 != req.daysToKeep0) {
303,902✔
2867
    pVnode->config.tsdbCfg.keep0 = req.daysToKeep0;
39,990✔
2868
    tsdbChanged = true;
39,990✔
2869
  }
2870

2871
  if (pVnode->config.tsdbCfg.keep1 != req.daysToKeep1) {
303,902✔
2872
    pVnode->config.tsdbCfg.keep1 = req.daysToKeep1;
38,566✔
2873
    tsdbChanged = true;
38,566✔
2874
  }
2875

2876
  if (pVnode->config.tsdbCfg.keep2 != req.daysToKeep2) {
303,902✔
2877
    pVnode->config.tsdbCfg.keep2 = req.daysToKeep2;
32,974✔
2878
    tsdbChanged = true;
32,974✔
2879
  }
2880

2881
  if (pVnode->config.tsdbCfg.keepTimeOffset != req.keepTimeOffset) {
303,902✔
2882
    pVnode->config.tsdbCfg.keepTimeOffset = req.keepTimeOffset;
7,016✔
2883
    tsdbChanged = true;
7,016✔
2884
  }
2885

2886
  if (req.sttTrigger != -1 && req.sttTrigger != pVnode->config.sttTrigger) {
303,902✔
2887
    if (req.sttTrigger > 1 && pVnode->config.sttTrigger > 1) {
2,406✔
2888
      pVnode->config.sttTrigger = req.sttTrigger;
1,382✔
2889
    } else {
2890
      vnodeAWait(&pVnode->commitTask);
1,024✔
2891

2892
      int32_t ret = tsdbDisableAndCancelAllBgTask(pVnode->pTsdb);
1,024✔
2893
      if (ret != 0) {
1,024✔
2894
        vError("vgId:%d, failed to disable bg task since %s", TD_VID(pVnode), tstrerror(ERRNO));
×
2895
      }
2896

2897
      pVnode->config.sttTrigger = req.sttTrigger;
1,024✔
2898
      tsdbEnableBgTask(pVnode->pTsdb);
1,024✔
2899
    }
2900
  }
2901

2902
  if (req.minRows != -1 && req.minRows != pVnode->config.tsdbCfg.minRows) {
303,902✔
2903
    pVnode->config.tsdbCfg.minRows = req.minRows;
1,382✔
2904
  }
2905

2906
  if (req.ssKeepLocal != -1 && req.ssKeepLocal != pVnode->config.ssKeepLocal) {
303,902✔
2907
    pVnode->config.ssKeepLocal = req.ssKeepLocal;
1,382✔
2908
  }
2909
  if (req.ssCompact != -1 && req.ssCompact != pVnode->config.ssCompact) {
303,902✔
2910
    pVnode->config.ssCompact = req.ssCompact;
1,382✔
2911
  }
2912

2913
  if (walChanged) {
303,902✔
2914
    if (walAlter(pVnode->pWal, &pVnode->config.walCfg) != 0) {
130,620✔
2915
      vError("vgId:%d, failed to alter wal config since %s", TD_VID(pVnode), tstrerror(ERRNO));
×
2916
    }
2917
  }
2918

2919
  if (tsdbChanged) {
303,902✔
2920
    tsdbSetKeepCfg(pVnode->pTsdb, &pVnode->config.tsdbCfg);
54,106✔
2921
  }
2922

2923
  return 0;
303,902✔
2924
}
2925

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

2935
  SMetaReader mr = {0};
×
2936
  metaReaderDoInit(&mr, pVnode->pMeta, META_READER_NOLOCK);
×
2937
  STsdb *pTsdb = pVnode->pTsdb;
×
2938

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

2948
    int64_t uid = mr.me.uid;
×
2949

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

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

2973
static int32_t vnodeProcessDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
2,004,554✔
2974
                                     SRpcMsg *pOriginalMsg) {
2975
  int32_t     code = 0;
2,004,554✔
2976
  SDecoder   *pCoder = &(SDecoder){0};
2,004,554✔
2977
  SDeleteRes *pRes = &(SDeleteRes){0};
2,004,554✔
2978

2979
  pRsp->msgType = TDMT_VND_DELETE_RSP;
2,004,554✔
2980
  pRsp->pCont = NULL;
2,004,554✔
2981
  pRsp->contLen = 0;
2,004,554✔
2982
  pRsp->code = TSDB_CODE_SUCCESS;
2,004,554✔
2983

2984
  pRes->uidList = taosArrayInit(0, sizeof(tb_uid_t));
2,004,554✔
2985
  if (pRes->uidList == NULL) {
2,004,554✔
2986
    code = terrno;
×
2987
    goto _err;
×
2988
  }
2989

2990
  tDecoderInit(pCoder, pReq, len);
2,004,554✔
2991
  code = tDecodeDeleteRes(pCoder, pRes);
2,004,554✔
2992
  if (code) goto _err;
2,004,554✔
2993

2994
  if (pRes->affectedRows > 0) {
2,004,554✔
2995
    for (int32_t iUid = 0; iUid < taosArrayGetSize(pRes->uidList); iUid++) {
3,569,007✔
2996
      uint64_t uid = *(uint64_t *)taosArrayGet(pRes->uidList, iUid);
2,037,560✔
2997
      code = tsdbDeleteTableData(pVnode->pTsdb, ver, pRes->suid, uid, pRes->skey, pRes->ekey);
2,037,560✔
2998
      if (code) goto _err;
2,037,560✔
2999
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, pRes->ctimeMs);
2,037,560✔
3000
      if (code) goto _err;
2,037,560✔
3001
    }
3002
  }
3003

3004
  tDecoderClear(pCoder);
2,004,554✔
3005
  taosArrayDestroy(pRes->uidList);
2,004,554✔
3006

3007
  SVDeleteRsp rsp = {.affectedRows = pRes->affectedRows};
2,004,554✔
3008
  int32_t     ret = 0;
2,004,554✔
3009
  tEncodeSize(tEncodeSVDeleteRsp, &rsp, pRsp->contLen, ret);
2,004,554✔
3010
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
2,004,554✔
3011
  SEncoder ec = {0};
2,004,554✔
3012
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
2,004,554✔
3013
  code = tEncodeSVDeleteRsp(&ec, &rsp);
2,004,554✔
3014
  if (code) goto _err;
2,004,554✔
3015
  tEncoderClear(&ec);
2,004,554✔
3016
  return code;
2,004,554✔
3017

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

3034
  return code;
×
3035
}
3036
static int32_t vnodeProcessCreateIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
4,271✔
3037
  SVCreateStbReq req = {0};
4,271✔
3038
  SDecoder       dc = {0};
4,271✔
3039
  int32_t        code = 0;
4,271✔
3040

3041
  pRsp->msgType = TDMT_VND_CREATE_INDEX_RSP;
4,271✔
3042
  pRsp->code = TSDB_CODE_SUCCESS;
4,271✔
3043
  pRsp->pCont = NULL;
4,271✔
3044
  pRsp->contLen = 0;
4,271✔
3045

3046
  tDecoderInit(&dc, pReq, len);
4,271✔
3047
  // decode req
3048
  if (tDecodeSVCreateStbReq(&dc, &req) < 0) {
4,271✔
3049
    tDecoderClear(&dc);
×
3050
    return terrno = TSDB_CODE_INVALID_MSG;
×
3051
  }
3052

3053
  code = metaAddIndexToSuperTable(pVnode->pMeta, ver, &req);
4,271✔
3054
  if (code) {
4,271✔
3055
    pRsp->code = code;
×
3056
    goto _err;
×
3057
  }
3058
  tDecoderClear(&dc);
4,271✔
3059
  return 0;
4,271✔
3060

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

3073
  if ((code = tDeserializeSDropIdxReq(pReq, len, &req))) {
2,812✔
3074
    pRsp->code = code;
×
3075
    return code;
×
3076
  }
3077

3078
  code = metaDropIndexFromSuperTable(pVnode->pMeta, ver, &req);
2,812✔
3079
  if (code) {
2,812✔
3080
    pRsp->code = code;
×
3081
    return code;
×
3082
  }
3083
  return TSDB_CODE_SUCCESS;
2,812✔
3084
}
3085

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

3088
static int32_t vnodeProcessCompactVnodeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
65,148✔
3089
  if (!pVnode->restored) {
65,148✔
3090
    vInfo("vgId:%d, ignore compact req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
3,663✔
3091
    return 0;
3,663✔
3092
  }
3093
  return vnodeAsyncCompact(pVnode, ver, pReq, len, pRsp);
61,485✔
3094
}
3095

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

3101
  pRsp->msgType = TDMT_SYNC_CONFIG_CHANGE_RSP;
×
3102
  pRsp->code = TSDB_CODE_SUCCESS;
×
3103
  pRsp->pCont = NULL;
×
3104
  pRsp->contLen = 0;
×
3105

3106
  return 0;
×
3107
}
3108

3109
static int32_t vnodeCheckState(SVnode *pVnode) {
83,053✔
3110
  SSyncState syncState = syncGetState(pVnode->sync);
83,053✔
3111
  if (syncState.state != TAOS_SYNC_STATE_LEADER) {
83,053✔
3112
    return terrno = TSDB_CODE_SYN_NOT_LEADER;
37,707✔
3113
  }
3114
  return 0;
45,346✔
3115
}
3116

3117
static int32_t vnodeCheckToken(SVnode *pVnode, char *member0Token, char *member1Token) {
45,346✔
3118
  char token[TSDB_ARB_TOKEN_SIZE] = {0};
45,346✔
3119
  if (vnodeGetArbToken(pVnode, token) != 0) {
45,346✔
3120
    return terrno = TSDB_CODE_NOT_FOUND;
×
3121
  }
3122

3123
  if (strncmp(token, member0Token, TSDB_ARB_TOKEN_SIZE) != 0 &&
45,346✔
3124
      strncmp(token, member1Token, TSDB_ARB_TOKEN_SIZE) != 0) {
44,812✔
3125
    return terrno = TSDB_CODE_MND_ARB_TOKEN_MISMATCH;
42,882✔
3126
  }
3127

3128
  terrno = TSDB_CODE_SUCCESS;
2,464✔
3129
  return 0;
2,464✔
3130
}
3131

3132
static int32_t vnodeCheckSyncd(SVnode *pVnode, char *member0Token, char *member1Token) {
8,309✔
3133
  int32_t code = vnodeCheckToken(pVnode, member0Token, member1Token);
8,309✔
3134
  if (code != 0) {
8,309✔
3135
    return code;
7,414✔
3136
  }
3137

3138
  return syncCheckSynced(pVnode->sync);
895✔
3139
}
3140

3141
static int32_t vnodeProcessArbCheckSyncReq(SVnode *pVnode, void *pReq, int32_t len, SRpcMsg *pRsp) {
23,325✔
3142
  int32_t code = 0;
23,325✔
3143

3144
  if ((code = vnodeCheckState(pVnode)) != 0) {
23,325✔
3145
    vDebug("vgId:%d, failed to preprocess vnode-arb-check-sync request since %s", TD_VID(pVnode), tstrerror(code));
15,016✔
3146
    return 0;
15,016✔
3147
  }
3148

3149
  SVArbCheckSyncReq syncReq = {0};
8,309✔
3150

3151
  code = tDeserializeSVArbCheckSyncReq(pReq, len, &syncReq);
8,309✔
3152
  if (code) {
8,309✔
3153
    return code;
×
3154
  }
3155

3156
  vInfo("vgId:%d, start to process vnode-arb-check-sync req QID:0x%" PRIx64 ":0x%" PRIx64 ", seqNum:%" PRIx64
8,309✔
3157
        ", arbToken:%s, member0Token:%s, member1Token:%s, "
3158
        "arbTerm:%" PRId64,
3159
        TD_VID(pVnode), pRsp->info.traceId.rootId, pRsp->info.traceId.msgId, pRsp->info.seqNum, syncReq.arbToken,
3160
        syncReq.member0Token, syncReq.member1Token, syncReq.arbTerm);
3161

3162
  pRsp->msgType = TDMT_VND_ARB_CHECK_SYNC_RSP;
8,309✔
3163
  pRsp->code = TSDB_CODE_SUCCESS;
8,309✔
3164
  pRsp->pCont = NULL;
8,309✔
3165
  pRsp->contLen = 0;
8,309✔
3166

3167
  SVArbCheckSyncRsp syncRsp = {0};
8,309✔
3168
  syncRsp.arbToken = syncReq.arbToken;
8,309✔
3169
  syncRsp.member0Token = syncReq.member0Token;
8,309✔
3170
  syncRsp.member1Token = syncReq.member1Token;
8,309✔
3171
  syncRsp.vgId = TD_VID(pVnode);
8,309✔
3172

3173
  if ((syncRsp.errCode = vnodeCheckSyncd(pVnode, syncReq.member0Token, syncReq.member1Token)) != 0) {
8,309✔
3174
    vError("vgId:%d, failed to check assigned log syncd since %s", TD_VID(pVnode), tstrerror(syncRsp.errCode));
7,414✔
3175
  }
3176

3177
  if ((code = vnodeUpdateArbTerm(pVnode, syncReq.arbTerm)) != 0) {
8,309✔
3178
    vError("vgId:%d, failed to update arb term since %s", TD_VID(pVnode), tstrerror(code));
×
3179
    goto _OVER;
×
3180
  }
3181

3182
  int32_t contLen = tSerializeSVArbCheckSyncRsp(NULL, 0, &syncRsp);
8,309✔
3183
  if (contLen <= 0) {
8,309✔
3184
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
3185
    code = -1;
×
3186
    goto _OVER;
×
3187
  }
3188
  void *pHead = rpcMallocCont(contLen);
8,309✔
3189
  if (!pHead) {
8,309✔
3190
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
3191
    code = -1;
×
3192
    goto _OVER;
×
3193
  }
3194

3195
  if (tSerializeSVArbCheckSyncRsp(pHead, contLen, &syncRsp) <= 0) {
8,309✔
3196
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
3197
    rpcFreeCont(pHead);
×
3198
    code = -1;
×
3199
    goto _OVER;
×
3200
  }
3201

3202
  pRsp->pCont = pHead;
8,309✔
3203
  pRsp->contLen = contLen;
8,309✔
3204

3205
  vInfo(
8,309✔
3206
      "vgId:%d, suceed to process vnode-arb-check-sync req rsp.code:%s, arbToken:%s, member0Token:%s, "
3207
      "member1Token:%s",
3208
      TD_VID(pVnode), tstrerror(syncRsp.errCode), syncRsp.arbToken, syncRsp.member0Token, syncRsp.member1Token);
3209

3210
  code = TSDB_CODE_SUCCESS;
8,309✔
3211

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

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