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

taosdata / TDengine / #4934

21 Jan 2026 06:06AM UTC coverage: 66.691% (+0.02%) from 66.671%
#4934

push

travis-ci

web-flow
 enh:stmt support interval opt (#34335)

0 of 26 new or added lines in 3 files covered. (0.0%)

562 existing lines in 98 files now uncovered.

203203 of 304692 relevant lines covered (66.69%)

129902664.38 hits per line

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

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

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

32
extern taos_counter_t *tsInsertCounter;
33

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

37
static int32_t vnodeProcessCreateStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
38
static int32_t vnodeProcessAlterStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
39
static int32_t vnodeProcessDropStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp, SRpcMsg *pOriginRpc);
40
static int32_t vnodeProcessCreateTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
41
                                       SRpcMsg *pOriginRpc);
42
static int32_t vnodeProcessAlterTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
43
static int32_t vnodeProcessDropTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
44
                                     SRpcMsg *pOriginRpc);
45
static int32_t vnodeProcessSubmitReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
46
                                     SRpcMsg *pOriginalMsg);
47
static int32_t vnodeProcessAlterConfirmReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
48
static int32_t vnodeProcessAlterConfigReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
49
static int32_t vnodeProcessDropTtlTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
50
static int32_t vnodeProcessTrimReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
51
static int32_t 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) {
53,636,379✔
85
  int32_t code = 0;
53,636,379✔
86
  int32_t lino = 0;
53,636,379✔
87

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

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

99
  // name
100
  char *name = NULL;
53,642,594✔
101
  if (tDecodeCStr(pCoder, &name) < 0) {
53,646,646✔
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);
53,646,646✔
108
  if (uid == 0) {
53,634,162✔
109
    uid = tGenIdPI64();
53,269,118✔
110
  }
111
  taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), uid);
53,618,407✔
112

113
  // btime
114
  taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos + 8), btime);
53,620,508✔
115

116
  tEndDecode(pCoder);
53,615,076✔
117

118
_exit:
53,625,191✔
119
  if (code) {
53,624,948✔
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);
53,624,948✔
123
    if (pUid) *pUid = uid;
53,610,191✔
124
  }
125
  return code;
53,612,177✔
126
}
127
static int32_t vnodePreProcessCreateTableMsg(SVnode *pVnode, SRpcMsg *pMsg) {
37,169,297✔
128
  int32_t code = 0;
37,169,297✔
129
  int32_t lino = 0;
37,169,297✔
130

131
  int64_t  btime = taosGetTimestampMs();
37,170,008✔
132
  SDecoder dc = {0};
37,170,008✔
133
  int32_t  nReqs;
37,169,517✔
134

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

141
  if (tDecodeI32v(&dc, &nReqs) < 0) {
37,169,387✔
142
    code = TSDB_CODE_INVALID_MSG;
×
143
    TSDB_CHECK_CODE(code, lino, _exit);
×
144
  }
145
  for (int32_t iReq = 0; iReq < nReqs; iReq++) {
79,214,556✔
146
    code = vnodePreprocessCreateTableReq(pVnode, &dc, btime, NULL);
42,063,173✔
147
    TSDB_CHECK_CODE(code, lino, _exit);
42,045,169✔
148
  }
149

150
  tEndDecode(&dc);
37,151,383✔
151

152
_exit:
37,167,925✔
153
  tDecoderClear(&dc);
37,168,232✔
154
  if (code) {
37,168,476✔
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;
37,165,892✔
159
}
160

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

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

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

178
  code = 0;
11,810,355✔
179

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

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

195
  SMsgHead *pContOld = pMsg->pCont;
9,798,877✔
196
  int32_t   reqLenOld = pMsg->contLen - sizeof(SMsgHead);
9,800,804✔
197

198
  SArray *tbUids = NULL;
9,801,087✔
199
  int64_t timestampMs = 0;
9,801,087✔
200

201
  SVDropTtlTableReq ttlReq = {0};
9,801,087✔
202
  if (tDeserializeSVDropTtlTableReq((char *)pContOld + sizeof(SMsgHead), reqLenOld, &ttlReq) != 0) {
9,801,280✔
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,799,085✔
209
    if (tbUids == NULL) {
9,800,537✔
210
      code = terrno;
×
211
      TSDB_CHECK_CODE(code, lino, _exit);
×
212
    }
213

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

221
    ttlReq.nUids = taosArrayGetSize(tbUids);
9,784,131✔
222
    ttlReq.pTbUids = tbUids;
9,790,060✔
223
  }
224

225
  if (ttlReq.nUids == 0) {
9,790,060✔
226
    code = TSDB_CODE_MSG_PREPROCESSED;
9,756,972✔
227
    TSDB_CHECK_CODE(code, lino, _exit);
9,756,972✔
228
  }
229

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

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

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

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

251
  code = 0;
11,337✔
252

253
_exit:
9,780,508✔
254
  taosArrayDestroy(tbUids);
9,788,536✔
255

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

263
  return code;
9,778,564✔
264
}
265

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

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

276
  SSubmitTbData submitTbData;
595,450,942✔
277
  uint8_t       version;
278
  if (tDecodeI32v(pCoder, &submitTbData.flags) < 0) {
595,445,503✔
279
    code = TSDB_CODE_INVALID_MSG;
×
280
    TSDB_CHECK_CODE(code, lino, _exit);
×
281
  }
282
  version = (submitTbData.flags >> 8) & 0xff;
595,445,503✔
283
  submitTbData.flags = submitTbData.flags & 0xff;
595,445,503✔
284

285
  int64_t uid;
595,442,481✔
286
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
595,442,688✔
287
    code = vnodePreprocessCreateTableReq(pVnode, pCoder, btimeMs, &uid);
11,573,577✔
288
    TSDB_CHECK_CODE(code, lino, _exit);
11,571,412✔
289
  }
290

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

297
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
595,445,765✔
298
    taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), uid);
11,570,593✔
299
    pCoder->pos += sizeof(int64_t);
11,568,833✔
300
  } else {
301
    if (tDecodeI64(pCoder, &submitTbData.uid) < 0) {
583,874,471✔
302
      code = TSDB_CODE_INVALID_MSG;
×
303
      TSDB_CHECK_CODE(code, lino, _exit);
×
304
    }
305
  }
306

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

312
  // scan and check
313
  TSKEY now = btimeMs;
595,442,343✔
314
  if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_MICRO) {
595,442,343✔
315
    now *= 1000;
2,446,201✔
316
  } else if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_NANO) {
592,995,862✔
317
    now *= 1000000;
1,425,913✔
318
  }
319

320
  int32_t keep = pVnode->config.tsdbCfg.keep2;
595,442,709✔
321

322
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * keep;
595,438,618✔
323
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
595,445,598✔
324
  if (submitTbData.flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
595,444,052✔
325
    uint64_t nColData;
3,318,782✔
326
    if (tDecodeU64v(pCoder, &nColData) < 0) {
3,318,878✔
327
      code = TSDB_CODE_INVALID_MSG;
×
328
      TSDB_CHECK_CODE(code, lino, _exit);
×
329
    }
330

331
    SColData colData = {0};
3,318,878✔
332
    code = tDecodeColData(version, pCoder, &colData, false);
3,318,884✔
333
    if (code) {
3,317,604✔
334
      code = TSDB_CODE_INVALID_MSG;
×
335
      TSDB_CHECK_CODE(code, lino, _exit);
×
336
    }
337

338
    if (colData.flag != HAS_VALUE) {
3,317,604✔
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;
578✔
346
        TSDB_CHECK_CODE(code, lino, _exit);
578✔
347
      }
348
    }
349

350
    for (uint64_t i = 1; i < nColData; i++) {
10,257,555✔
351
      code = tDecodeColData(version, pCoder, &colData, true);
7,153,444✔
352
      if (code) {
7,153,761✔
353
        code = TSDB_CODE_INVALID_MSG;
×
354
        TSDB_CHECK_CODE(code, lino, _exit);
×
355
      }
356
    }
357
  } else {
358
    uint64_t nRow;
592,122,248✔
359
    if (tDecodeU64v(pCoder, &nRow) < 0) {
592,126,810✔
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;
65,021✔
374
        TSDB_CHECK_CODE(code, lino, _exit);
65,021✔
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)) {
595,376,604✔
386
    taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), ctimeMs);
595,377,837✔
387
    pCoder->pos += sizeof(int64_t);
595,371,265✔
388
  }
389

390
  tEndDecode(pCoder);
595,373,787✔
391

392
_exit:
595,441,220✔
393
  if (code) {
595,448,500✔
394
    vError("vgId:%d, %s:%d failed to vnodePreProcessSubmitTbData submit request since %s", TD_VID(pVnode), __func__,
65,599✔
395
           lino, tstrerror(code));
396
  }
397
  return code;
595,448,500✔
398
}
399
static int32_t vnodePreProcessSubmitMsg(SVnode *pVnode, SRpcMsg *pMsg) {
568,275,611✔
400
  int32_t code = 0;
568,275,611✔
401
  int32_t lino = 0;
568,275,611✔
402

403
  if (tsBypassFlag & TSDB_BYPASS_RA_RPC_RECV_SUBMIT) {
568,275,611✔
404
    return TSDB_CODE_MSG_PREPROCESSED;
77✔
405
  }
406

407
  SDecoder *pCoder = &(SDecoder){0};
568,275,534✔
408

409
  if (taosHton64(((SSubmitReq2Msg *)pMsg->pCont)->version) != 1) {
568,278,172✔
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));
568,277,979✔
415

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

421
  uint64_t nSubmitTbData;
568,274,396✔
422
  if (tDecodeU64v(pCoder, &nSubmitTbData) < 0) {
568,279,407✔
423
    code = TSDB_CODE_INVALID_MSG;
×
424
    TSDB_CHECK_CODE(code, lino, _exit);
×
425
  }
426

427
  int64_t btimeMs = taosGetTimestampMs();
568,278,661✔
428
  int64_t ctimeMs = btimeMs;
568,278,661✔
429
  for (int32_t i = 0; i < nSubmitTbData; i++) {
1,163,655,563✔
430
    code = vnodePreProcessSubmitTbData(pVnode, pCoder, btimeMs, ctimeMs);
595,447,736✔
431
    TSDB_CHECK_CODE(code, lino, _exit);
595,442,501✔
432
  }
433

434
  tEndDecode(pCoder);
568,207,827✔
435

436
_exit:
568,277,911✔
437
  tDecoderClear(pCoder);
568,277,955✔
438
  if (code) {
568,273,567✔
439
    vError("vgId:%d, %s:%d failed to preprocess submit request since %s, msg type:%s", TD_VID(pVnode), __func__, lino,
65,599✔
440
           tstrerror(code), TMSG_INFO(pMsg->msgType));
441
  }
442
  return code;
568,272,397✔
443
}
444

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

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

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

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

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

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

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

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

481
  taosArrayDestroy(res.uidList);
1,694,661✔
482

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

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

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

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

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

534
  return code;
32,272✔
535
}
536

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

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

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

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

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

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

620
  switch (pMsg->msgType) {
650,239,393✔
621
    case TDMT_VND_CREATE_TABLE: {
37,170,319✔
622
      code = vnodePreProcessCreateTableMsg(pVnode, pMsg);
37,170,319✔
623
    } break;
37,165,679✔
624
    case TDMT_VND_ALTER_TABLE: {
11,810,355✔
625
      code = vnodePreProcessAlterTableMsg(pVnode, pMsg);
11,810,355✔
626
    } break;
11,810,355✔
627
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
9,802,281✔
628
    case TDMT_VND_DROP_TTL_TABLE: {
629
      code = vnodePreProcessDropTtlMsg(pVnode, pMsg);
9,802,281✔
630
    } break;
9,784,068✔
631
    case TDMT_VND_SUBMIT: {
568,277,414✔
632
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.preprocess_time, METRIC_LEVEL_LOW,
568,277,414✔
633
                           { code = vnodePreProcessSubmitMsg(pVnode, pMsg); });
634
    } break;
568,273,818✔
635
    case TDMT_VND_DELETE: {
1,695,231✔
636
      code = vnodePreProcessDeleteMsg(pVnode, pMsg);
1,695,231✔
637
    } break;
1,695,231✔
638
    case TDMT_VND_BATCH_DEL: {
×
639
      code = vnodePreProcessBatchDeleteMsg(pVnode, pMsg);
×
640
    } break;
×
641
    case TDMT_VND_ARB_CHECK_SYNC: {
48,785✔
642
      code = vnodePreProcessArbCheckSyncMsg(pVnode, pMsg);
48,785✔
643
    } break;
48,785✔
644
    case TDMT_VND_DROP_TABLE: {
1,337,321✔
645
      code = vnodePreProcessDropTbMsg(pVnode, pMsg);
1,337,321✔
646
    } break;
1,337,060✔
647
#ifdef TD_ENTERPRISE
648
    case TDMT_VND_SSMIGRATE_FILESET: {
1,153✔
649
      code = vnodePreProcessSsMigrateFileSetReq(pVnode, pMsg);
1,153✔
650
    } break;
×
651
#endif
652
    default:
20,097,449✔
653
      break;
20,097,449✔
654
  }
655

656
  if (code && code != TSDB_CODE_MSG_PREPROCESSED) {
650,212,445✔
657
    vError("vgId:%d, failed to preprocess write request since %s, msg type:%s", TD_VID(pVnode), tstrerror(code),
96,849✔
658
           TMSG_INFO(pMsg->msgType));
659
  }
660
  return code;
650,214,463✔
661
}
662

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

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

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

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

676
  SRow  **pRow = (SRow **)TARRAY_DATA(pSubmitTbData->aRowP);
21,629✔
677
  int32_t rowIdx = -1;
21,629✔
678
  for (int32_t i = 0; i < sz; i++) {
24,154,483✔
679
    int64_t     seq = 0;
24,132,854✔
680
    SBlobValue *p = taosArrayGet(pBlobSet->pSeqTable, i);
24,132,854✔
681
    if (p->type == TSDB_DATA_BLOB_EMPTY_VALUE || p->type == TSDB_DATA_BLOB_NULL_VALUE) {
24,132,854✔
682
      // skip empty or null blob
683
      continue;
2,424,138✔
684
    }
685

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

689
    SRow *row = taosArrayGetP(pSubmitTbData->aRowP, i);
21,708,716✔
690
    if (row == NULL) {
21,708,716✔
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) {
43,417,432✔
697
      code = TSDB_CODE_INVALID_MSG;
×
698
      TSDB_CHECK_CODE(code, lino, _exit);
×
699
    }
700
  }
701

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

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

785
  return code;
21,629✔
786
}
787

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

795
  (void)taosThreadMutexLock(&pVnode->mutex);
776,985,539✔
796
  if (pVnode->disableWrite) {
777,036,505✔
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);
776,906,830✔
802

803
  if (ver <= pVnode->state.applied) {
776,991,938✔
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,
776,955,407✔
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)) {
777,052,882✔
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)) {
777,055,001✔
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);
777,054,840✔
828
  atomic_store_64(&pVnode->state.applyTerm, pMsg->info.conn.applyTerm);
777,063,359✔
829

830
  if (!syncUtilUserCommit(pMsg->msgType)) goto _exit;
777,061,919✔
831

832
  // skip header
833
  pReq = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
767,441,896✔
834
  len = pMsg->contLen - sizeof(SMsgHead);
767,442,352✔
835
  bool needCommit = false;
767,440,903✔
836
  bool forceTrimWal = false;
767,440,903✔
837

838
  switch (pMsg->msgType) {
767,440,903✔
839
    /* META */
840
    case TDMT_VND_CREATE_STB:
4,910,592✔
841
      code = vnodeProcessCreateStbReq(pVnode, ver, pReq, len, pRsp);
4,910,592✔
842
      TSDB_CHECK_CODE(code, lino, _err);
4,910,346✔
843
      break;
4,907,382✔
844
    case TDMT_VND_ALTER_STB:
7,442,961✔
845
      code = vnodeProcessAlterStbReq(pVnode, ver, pReq, len, pRsp);
7,442,961✔
846
      TSDB_CHECK_CODE(code, lino, _err);
7,441,012✔
847
      break;
7,441,012✔
848
    case TDMT_VND_DROP_STB:
890,276✔
849
      code = vnodeProcessDropStbReq(pVnode, ver, pReq, len, pRsp, pMsg);
890,276✔
850
      TSDB_CHECK_CODE(code, lino, _err);
890,276✔
851
      break;
890,276✔
852
    case TDMT_VND_CREATE_TABLE:
39,935,531✔
853
      code = vnodeProcessCreateTbReq(pVnode, ver, pReq, len, pRsp, pMsg);
39,935,531✔
854
      TSDB_CHECK_CODE(code, lino, _err);
39,930,400✔
855
      break;
39,930,400✔
856
    case TDMT_VND_ALTER_TABLE:
11,810,681✔
857
      code = vnodeProcessAlterTbReq(pVnode, ver, pReq, len, pRsp);
11,810,681✔
858
      TSDB_CHECK_CODE(code, lino, _err);
11,810,681✔
859
      break;
11,810,681✔
860
    case TDMT_VND_DROP_TABLE:
1,337,650✔
861
    case TDMT_VND_SNODE_DROP_TABLE:
862
      code = vnodeProcessDropTbReq(pVnode, ver, pReq, len, pRsp, pMsg);
1,337,650✔
863
      TSDB_CHECK_CODE(code, lino, _err);
1,337,389✔
864
      break;
1,337,389✔
865
    case TDMT_VND_CREATE_RSMA:
22,230✔
866
      code = vnodeProcessCreateRsmaReq(pVnode, ver, pReq, len, pRsp);
22,230✔
867
      TSDB_CHECK_CODE(code, lino, _err);
22,230✔
868
      break;
22,230✔
869
    case TDMT_VND_DROP_RSMA:
4,446✔
870
      code = vnodeProcessDropRsmaReq(pVnode, ver, pReq, len, pRsp);
4,446✔
871
      TSDB_CHECK_CODE(code, lino, _err);
4,446✔
872
      break;
4,446✔
873
    case TDMT_VND_ALTER_RSMA:
11,856✔
874
      code = vnodeProcessAlterRsmaReq(pVnode, ver, pReq, len, pRsp);
11,856✔
875
      TSDB_CHECK_CODE(code, lino, _err);
11,856✔
876
      break;
11,856✔
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,337✔
882
      code = vnodeProcessFetchTtlExpiredTbs(pVnode, ver, pReq, len, pRsp);
11,337✔
883
      TSDB_CHECK_CODE(code, lino, _err);
10,712✔
884
      break;
10,712✔
885
    case TDMT_VND_TRIM:
79,043✔
886
      code = vnodeProcessTrimReq(pVnode, ver, pReq, len, pRsp);
79,043✔
887
      TSDB_CHECK_CODE(code, lino, _err);
79,614✔
888
      break;
79,614✔
889
      case TDMT_VND_TRIM_WAL:
6,504✔
890
        needCommit = true;
6,504✔
891
        forceTrimWal = true;
6,504✔
892
        break;
6,504✔
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: {
683,780,440✔
907
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.apply_time, METRIC_LEVEL_LOW, {
683,780,440✔
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);
683,740,442✔
911
      break;
683,727,716✔
912
    }
913
    case TDMT_VND_DELETE:
2,067,405✔
914
      code = vnodeProcessDeleteReq(pVnode, ver, pReq, len, pRsp, pMsg);
2,067,405✔
915
      TSDB_CHECK_CODE(code, lino, _err);
2,067,405✔
916
      break;
2,067,405✔
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,324,138✔
924
      code = tqProcessSubscribeReq(pVnode->pTq, ver, pReq, len);
1,324,138✔
925
      TSDB_CHECK_CODE(code, lino, _err);
1,319,578✔
926
      break;
1,319,578✔
927
    case TDMT_VND_TMQ_DELETE_SUB:
273,985✔
928
      code = tqProcessDeleteSubReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen);
273,985✔
929
      TSDB_CHECK_CODE(code, lino, _err);
273,710✔
930
      break;
273,710✔
931
    case TDMT_VND_TMQ_COMMIT_OFFSET:
4,589,495✔
932
      code = tqProcessOffsetCommitReq(pVnode->pTq, ver, pReq, len);
4,589,495✔
933
      TSDB_CHECK_CODE(code, lino, _err);
4,588,686✔
934
      break;
4,588,686✔
935
#endif
936
    case TDMT_VND_ALTER_CONFIRM:
3,634,921✔
937
      needCommit = pVnode->config.hashChange;
3,634,921✔
938
      code = vnodeProcessAlterConfirmReq(pVnode, ver, pReq, len, pRsp);
3,634,921✔
939
      TSDB_CHECK_CODE(code, lino, _err);
3,634,921✔
940
      break;
3,634,921✔
941
    case TDMT_VND_ALTER_CONFIG:
317,048✔
942
      vnodeProcessAlterConfigReq(pVnode, ver, pReq, len, pRsp);
317,048✔
943
      break;
317,717✔
944
    case TDMT_VND_COMMIT:
4,909,091✔
945
      needCommit = true;
4,909,091✔
946
      break;
4,909,091✔
947
    case TDMT_VND_CREATE_INDEX:
4,458✔
948
      vnodeProcessCreateIndexReq(pVnode, ver, pReq, len, pRsp);
4,458✔
949
      break;
4,458✔
950
    case TDMT_VND_DROP_INDEX:
2,952✔
951
      vnodeProcessDropIndexReq(pVnode, ver, pReq, len, pRsp);
2,952✔
952
      break;
2,952✔
953
    case TDMT_VND_COMPACT:
68,145✔
954
      vnodeProcessCompactVnodeReq(pVnode, ver, pReq, len, pRsp);
68,145✔
955
      goto _exit;
68,145✔
956
    case TDMT_VND_SCAN:
320✔
957
      vnodeProcessScanVnodeReq(pVnode, ver, pReq, len, pRsp);
320✔
958
      goto _exit;
320✔
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,388✔
964
      vnodeProcessKillCompactReq(pVnode, ver, pReq, len, pRsp);
1,388✔
965
      break;
1,388✔
966
#endif
967
    case TDMT_VND_KILL_SCAN:
128✔
968
      vnodeProcessKillScanReq(pVnode, ver, pReq, len, pRsp);
128✔
969
      break;
128✔
970
    case TDMT_VND_KILL_TRIM:
×
971
      vnodeProcessKillRetentionReq(pVnode, ver, pReq, len, pRsp);
×
972
      break;
×
973
    /* ARB */
974
    case TDMT_VND_ARB_CHECK_SYNC:
2,026✔
975
      vnodeProcessArbCheckSyncReq(pVnode, pReq, len, pRsp);
2,026✔
976
      break;
2,026✔
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);
767,302,278✔
983

984
  walApplyVer(pVnode->pWal, ver);
767,344,591✔
985

986
  if (pVnode->pTq) {
767,339,676✔
987
    code = tqPushMsg(pVnode->pTq, pMsg->msgType);
767,339,676✔
988
    if (code) {
767,337,172✔
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) {
767,337,172✔
996
    vInfo("vgId:%d, commit at version %" PRId64, TD_VID(pVnode), ver);
4,939,975✔
997
    code = vnodeAsyncCommit(pVnode, forceTrimWal);
4,939,975✔
998
    if (code) {
4,939,975✔
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,939,975✔
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:
767,335,816✔
1014
  return 0;
777,022,064✔
1015

1016
_err:
38,400✔
1017
  if (code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
38,400✔
1018
    vInfo("vgId:%d, process %s request failed since %s, lino 1:%d ver:%" PRId64, TD_VID(pVnode),
2,964✔
1019
          TMSG_INFO(pMsg->msgType), tstrerror(code), lino, ver);
1020
  } else if (code) {
35,436✔
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;
38,400✔
1026
}
1027

1028
int32_t vnodePreprocessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg) {
484,223,410✔
1029
  if (TDMT_SCH_QUERY != pMsg->msgType && TDMT_SCH_MERGE_QUERY != pMsg->msgType) {
484,223,410✔
1030
    return 0;
72,446,306✔
1031
  }
1032

1033
  int32_t qType = 0;
411,849,209✔
1034
  return qWorkerPreprocessQueryMsg(pVnode->pQuery, pMsg, (TDMT_SCH_QUERY == pMsg->msgType), &qType);
411,874,848✔
1035
}
1036

1037
int32_t vnodeProcessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
484,343,223✔
1038
  vTrace("message in vnode query queue is processing");
484,343,223✔
1039
  if (pMsg->msgType == TDMT_VND_TMQ_CONSUME && !syncIsReadyForRead(pVnode->sync)) {
484,343,223✔
1040
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
395,769✔
1041
    return 0;
395,769✔
1042
  }
1043

1044
  if (pMsg->msgType == TDMT_VND_TMQ_CONSUME && !pVnode->restored) {
483,968,007✔
1045
    vnodeRedirectRpcMsg(pVnode, pMsg, TSDB_CODE_SYN_RESTORING);
×
1046
    return 0;
×
1047
  }
1048

1049
  SReadHandle handle = {0};
483,993,153✔
1050
  handle.vnode = pVnode;
483,952,106✔
1051
  handle.pMsgCb = &pVnode->msgCb;
483,952,106✔
1052
  handle.pWorkerCb = pInfo->workerCb;
483,876,160✔
1053
  initStorageAPI(&handle.api);
483,880,764✔
1054
  int32_t code = TSDB_CODE_SUCCESS;
483,530,101✔
1055
  bool    redirected = false;
483,530,101✔
1056

1057
  switch (pMsg->msgType) {
483,530,101✔
1058
    case TDMT_SCH_QUERY:
331,931,552✔
1059
      if (!syncIsReadyForRead(pVnode->sync)) {
331,931,552✔
1060
        pMsg->code = (terrno) ? terrno : TSDB_CODE_SYN_NOT_LEADER;
2,558,642✔
1061
        redirected = true;
2,558,642✔
1062
      }
1063
      code = qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
332,266,494✔
1064
      if (redirected) {
332,020,533✔
1065
        vnodeRedirectRpcMsg(pVnode, pMsg, pMsg->code);
2,558,642✔
1066
        return 0;
2,558,642✔
1067
      }
1068

1069
      return code;
329,461,891✔
1070
    case TDMT_SCH_MERGE_QUERY:
79,656,981✔
1071
      return qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
79,656,981✔
1072
    case TDMT_SCH_QUERY_CONTINUE:
38,384,210✔
1073
      return qWorkerProcessCQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
38,384,210✔
1074
    case TDMT_VND_TMQ_CONSUME:
25,714,424✔
1075
      return tqProcessPollReq(pVnode->pTq, pMsg);
25,714,424✔
1076
    case TDMT_VND_TMQ_CONSUME_PUSH:
7,932,408✔
1077
      return tqProcessPollPush(pVnode->pTq);
7,932,408✔
1078
    default:
×
1079
      vError("unknown msg type:%d in query queue", pMsg->msgType);
×
1080
      return TSDB_CODE_APP_ERROR;
×
1081
  }
1082
}
1083

1084
int32_t vnodeProcessFetchMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
1,037,253,408✔
1085
  int32_t code = TSDB_CODE_SUCCESS;
1,037,253,408✔
1086
  vTrace("vgId:%d, msg:%p in fetch queue is processing", pVnode->config.vgId, pMsg);
1,037,253,408✔
1087
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
1,037,253,408✔
1088
       pMsg->msgType == TDMT_VND_BATCH_META || pMsg->msgType == TDMT_VND_TABLE_NAME ||
711,089,975✔
1089
       pMsg->msgType == TDMT_VND_VSUBTABLES_META || pMsg->msgType == TDMT_VND_VSTB_REF_DBS) &&
685,038,081✔
1090
      !syncIsReadyForRead(pVnode->sync)) {
352,192,441✔
1091
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
656,168✔
1092
    return 0;
656,168✔
1093
  }
1094

1095
  switch (pMsg->msgType) {
1,036,710,957✔
1096
    case TDMT_SCH_FETCH:
419,688,364✔
1097
    case TDMT_SCH_MERGE_FETCH:
1098
      return qWorkerProcessFetchMsg(pVnode, pVnode->pQuery, pMsg, 0);
419,688,364✔
1099
    case TDMT_SCH_FETCH_RSP:
×
1100
      return qWorkerProcessRspMsg(pVnode, pVnode->pQuery, pMsg, 0);
×
1101
    // case TDMT_SCH_CANCEL_TASK:
1102
    //   return qWorkerProcessCancelMsg(pVnode, pVnode->pQuery, pMsg, 0);
1103
    case TDMT_SCH_DROP_TASK:
411,669,431✔
1104
      return qWorkerProcessDropMsg(pVnode, pVnode->pQuery, pMsg, 0);
411,669,431✔
1105
    case TDMT_SCH_TASK_NOTIFY:
15,840✔
1106
      return qWorkerProcessNotifyMsg(pVnode, pVnode->pQuery, pMsg, 0);
15,840✔
1107
    case TDMT_SCH_QUERY_HEARTBEAT:
178,715,495✔
1108
      return qWorkerProcessHbMsg(pVnode, pVnode->pQuery, pMsg, 0);
178,715,495✔
1109
    case TDMT_VND_TABLE_META:
881,515✔
1110
    case TDMT_VND_TABLE_NAME:
1111
      return vnodeGetTableMeta(pVnode, pMsg, true);
881,515✔
1112
    case TDMT_VND_TABLE_CFG:
×
1113
      return vnodeGetTableCfg(pVnode, pMsg, true);
×
1114
    case TDMT_VND_BATCH_META: {
25,393,826✔
1115
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.fetch_batch_meta_time, METRIC_LEVEL_LOW,
25,393,826✔
1116
                           { code = vnodeGetBatchMeta(pVnode, pMsg); });
1117
      METRICS_UPDATE(pVnode->writeMetrics.fetch_batch_meta_count, METRIC_LEVEL_LOW, 1);
25,398,669✔
1118
      return code;
25,390,267✔
1119
    }
1120
    case TDMT_VND_VSUBTABLES_META:
×
1121
      return vnodeGetVSubtablesMeta(pVnode, pMsg);
×
1122
    case TDMT_VND_VSTB_REF_DBS:
×
1123
      return vnodeGetVStbRefDbs(pVnode, pMsg);
×
1124
    case TDMT_VND_QUERY_SCAN_PROGRESS:
512✔
1125
      return vnodeQueryScanProgress(pVnode, pMsg);
512✔
1126
#ifdef TD_ENTERPRISE
1127
    case TDMT_VND_QUERY_COMPACT_PROGRESS:
99,727✔
1128
      return vnodeQueryCompactProgress(pVnode, pMsg);
99,727✔
1129

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

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

1154
void vnodeUpdateMetaRsp(SVnode *pVnode, STableMetaRsp *pMetaRsp) {
62,454,398✔
1155
  if (NULL == pMetaRsp) {
62,454,398✔
1156
    return;
×
1157
  }
1158

1159
  tstrncpy(pMetaRsp->dbFName, pVnode->config.dbname, TSDB_DB_FNAME_LEN);
62,454,398✔
1160
  pMetaRsp->dbId = pVnode->config.dbId;
62,453,473✔
1161
  pMetaRsp->vgId = TD_VID(pVnode);
62,456,712✔
1162
  pMetaRsp->precision = pVnode->config.tsdbCfg.precision;
62,454,393✔
1163
}
1164

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

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

1173
  int32_t     code = 0;
75,948✔
1174
  SVTrimDbReq trimReq = {0};
75,948✔
1175

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

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

1185
  code = vnodeAsyncRetention(pVnode, trimReq.tw, (int8_t)trimReq.optrType, (int8_t)trimReq.triggerType);
75,948✔
1186

1187
_exit:
75,948✔
1188
  return code;
75,948✔
1189
}
1190

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

1193
extern int32_t vnodeAsyncS3Migrate(SVnode *pVnode, int64_t now);
1194

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1362
    if (taosArrayPush(rsp.pExpiredTbs, &expiredTb) == NULL) {
92,571✔
1363
      goto _end;
×
1364
    }
1365
  }
1366

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

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

1389
static int32_t vnodeProcessCreateStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
4,907,395✔
1390
  int32_t        code = 0;
4,907,395✔
1391
  SVCreateStbReq req = {0};
4,907,395✔
1392
  SDecoder       coder;
4,910,943✔
1393

1394
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
4,910,729✔
1395
  pRsp->code = TSDB_CODE_SUCCESS;
4,910,729✔
1396
  pRsp->pCont = NULL;
4,911,303✔
1397
  pRsp->contLen = 0;
4,911,876✔
1398

1399
  // decode and process req
1400
  tDecoderInit(&coder, pReq, len);
4,910,729✔
1401

1402
  code = tDecodeSVCreateStbReq(&coder, &req);
4,911,738✔
1403
  if (code) {
4,896,561✔
1404
    pRsp->code = code;
×
1405
    goto _err;
×
1406
  }
1407

1408
  code = metaCreateSuperTable(pVnode->pMeta, ver, &req);
4,896,561✔
1409
  if (code) {
4,911,876✔
1410
    pRsp->code = code;
2,964✔
1411
    goto _err;
2,964✔
1412
  }
1413

1414
  tDecoderClear(&coder);
4,908,912✔
1415
  return 0;
4,908,223✔
1416

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

1422
static int32_t vnodeProcessCreateTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
39,935,842✔
1423
                                       SRpcMsg *pOriginRpc) {
1424
  SDecoder           decoder = {0};
39,935,842✔
1425
  SEncoder           encoder = {0};
39,935,842✔
1426
  int32_t            rcode = 0;
39,936,556✔
1427
  SVCreateTbBatchReq req = {0};
39,936,556✔
1428
  SVCreateTbReq     *pCreateReq;
1429
  SVCreateTbBatchRsp rsp = {0};
39,936,556✔
1430
  SVCreateTbRsp      cRsp = {0};
39,936,556✔
1431
  char               tbName[TSDB_TABLE_FNAME_LEN];
39,935,722✔
1432
  SArray            *tbUids = NULL;
39,936,556✔
1433
  SArray            *tbNames = NULL;
39,936,556✔
1434
  int64_t            tss = taosGetTimestampMs();
39,935,528✔
1435
  pRsp->msgType = TDMT_VND_CREATE_TABLE_RSP;
39,935,528✔
1436
  pRsp->code = TSDB_CODE_SUCCESS;
39,935,528✔
1437
  pRsp->pCont = NULL;
39,935,528✔
1438
  pRsp->contLen = 0;
39,935,217✔
1439

1440
  // decode
1441
  tDecoderInit(&decoder, pReq, len);
39,935,217✔
1442
  if (tDecodeSVCreateTbBatchReq(&decoder, &req) < 0) {
39,936,245✔
1443
    rcode = -1;
×
1444
    terrno = TSDB_CODE_INVALID_MSG;
×
1445
    goto _exit;
×
1446
  }
1447

1448
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(cRsp));
39,929,648✔
1449
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
39,932,157✔
1450
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
39,927,690✔
1451
  if (rsp.pArray == NULL || tbUids == NULL || tbNames == NULL) {
39,927,247✔
1452
    rcode = -1;
14✔
1453
    terrno = TSDB_CODE_OUT_OF_MEMORY;
14✔
1454
    goto _exit;
×
1455
  }
1456

1457
  // loop to create table
1458
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
87,505,921✔
1459
    pCreateReq = req.pReqs + iReq;
47,569,296✔
1460
    memset(&cRsp, 0, sizeof(cRsp));
47,565,507✔
1461

1462
    if (tsEnableAudit && tsEnableAuditCreateTable && tsAuditLevel >= AUDIT_LEVEL_CHILDTABLE) {
47,565,507✔
1463
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
80✔
1464
      if (str == NULL) {
80✔
1465
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1466
        rcode = -1;
×
1467
        goto _exit;
×
1468
      }
1469
      tstrncpy(str, pCreateReq->name, TSDB_TABLE_FNAME_LEN);
80✔
1470
      if (taosArrayPush(tbNames, &str) == NULL) {
80✔
1471
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1472
        rcode = -1;
×
1473
        goto _exit;
×
1474
      }
1475
    }
1476

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

1490
    // do create table
1491
    if (metaCreateTable2(pVnode->pMeta, ver, pCreateReq, &cRsp.pMeta) < 0) {
47,549,856✔
1492
      if (pCreateReq->flags & TD_CREATE_IF_NOT_EXISTS && terrno == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
17,484✔
1493
        cRsp.code = TSDB_CODE_SUCCESS;
5,154✔
1494
      } else {
1495
        cRsp.code = terrno;
12,330✔
1496
      }
1497
    } else {
1498
      cRsp.code = TSDB_CODE_SUCCESS;
47,556,992✔
1499
      if (taosArrayPush(tbUids, &pCreateReq->uid) == NULL) {
95,116,266✔
1500
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1501
        rcode = -1;
×
1502
        goto _exit;
×
1503
      }
1504
      vnodeUpdateMetaRsp(pVnode, cRsp.pMeta);
47,559,274✔
1505
    }
1506

1507
    if (taosArrayPush(rsp.pArray, &cRsp) == NULL) {
95,149,270✔
1508
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1509
      rcode = -1;
×
1510
      goto _exit;
×
1511
    }
1512
  }
1513

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

1519
  // prepare rsp
1520
  int32_t ret = 0;
39,936,555✔
1521
  tEncodeSize(tEncodeSVCreateTbBatchRsp, &rsp, pRsp->contLen, ret);
39,936,555✔
1522
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
39,930,088✔
1523
  if (pRsp->pCont == NULL) {
39,927,674✔
1524
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1525
    rcode = -1;
×
1526
    goto _exit;
×
1527
  }
1528
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
39,925,839✔
1529
  if (tEncodeSVCreateTbBatchRsp(&encoder, &rsp) != 0) {
39,925,391✔
1530
    vError("vgId:%d, failed to encode create table batch response", TD_VID(pVnode));
×
1531
  }
1532

1533
  if (tsEnableAudit && tsEnableAuditCreateTable && tsAuditLevel >= AUDIT_LEVEL_CHILDTABLE) {
39,930,081✔
1534
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
80✔
1535

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

1541
    SStringBuilder sb = {0};
80✔
1542
    for (int32_t i = 0; i < tbNames->size; i++) {
160✔
1543
      char **key = (char **)taosArrayGet(tbNames, i);
80✔
1544
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
80✔
1545
      if (i < tbNames->size - 1) {
80✔
1546
        taosStringBuilderAppendChar(&sb, ',');
×
1547
      }
1548
      // taosMemoryFreeClear(*key);
1549
    }
1550

1551
    size_t len = 0;
80✔
1552
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
80✔
1553

1554
    if (strlen(RPC_MSG_USER(pOriginRpc)) > 0) {
80✔
1555
      int64_t tse = taosGetTimestampMs();
80✔
1556
      double  duration = (double)(tse - tss);
80✔
1557
      duration = duration / 1000;
80✔
1558
      auditAddRecord(pOriginRpc, clusterId, "createTable", name.dbname, "", keyJoined, len, duration, 0);
80✔
1559
    }
1560

1561
    taosStringBuilderDestroy(&sb);
80✔
1562
  }
1563

1564
_exit:
39,930,081✔
1565
  tDeleteSVCreateTbBatchReq(&req);
39,925,040✔
1566
  taosArrayDestroyEx(rsp.pArray, tFreeSVCreateTbRsp);
39,928,028✔
1567
  taosArrayDestroy(tbUids);
39,919,171✔
1568
  tDecoderClear(&decoder);
39,915,275✔
1569
  tEncoderClear(&encoder);
39,931,647✔
1570
  taosArrayDestroyP(tbNames, NULL);
39,930,889✔
1571
  return rcode;
39,922,629✔
1572
}
1573

1574
static int32_t vnodeProcessAlterStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
7,442,961✔
1575
  int32_t        code = 0;
7,442,961✔
1576
  SVCreateStbReq req = {0};
7,442,961✔
1577
  SDecoder       dc = {0};
7,442,961✔
1578

1579
  pRsp->msgType = TDMT_VND_ALTER_STB_RSP;
7,442,961✔
1580
  pRsp->code = TSDB_CODE_SUCCESS;
7,442,961✔
1581
  pRsp->pCont = NULL;
7,442,961✔
1582
  pRsp->contLen = 0;
7,442,961✔
1583

1584
  tDecoderInit(&dc, pReq, len);
7,442,961✔
1585

1586
  // decode req
1587
  code = tDecodeSVCreateStbReq(&dc, &req);
7,442,961✔
1588
  if (code) {
7,429,797✔
1589
    tDecoderClear(&dc);
×
1590
    return code;
×
1591
  }
1592

1593
  code = metaAlterSuperTable(pVnode->pMeta, ver, &req);
7,429,797✔
1594
  if (code) {
7,441,012✔
1595
    pRsp->code = code;
×
1596
    tDecoderClear(&dc);
×
1597
    return code;
×
1598
  }
1599

1600
  tDecoderClear(&dc);
7,441,012✔
1601

1602
  return 0;
7,441,012✔
1603
}
1604

1605
static int32_t vnodeProcessDropStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp, SRpcMsg *pOriginRpc) {
890,276✔
1606
  SVDropStbReq req = {0};
890,276✔
1607
  int32_t      rcode = TSDB_CODE_SUCCESS;
890,276✔
1608
  SDecoder     decoder = {0};
890,276✔
1609
  SArray      *tbUidList = NULL;
890,276✔
1610

1611
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
890,276✔
1612
  pRsp->pCont = NULL;
890,276✔
1613
  pRsp->contLen = 0;
890,276✔
1614

1615
  // decode request
1616
  tDecoderInit(&decoder, pReq, len);
890,276✔
1617
  if (tDecodeSVDropStbReq(&decoder, &req) < 0) {
890,276✔
1618
    rcode = TSDB_CODE_INVALID_MSG;
×
1619
    goto _exit;
×
1620
  }
1621

1622
  STraceId* trace = &(pOriginRpc->info.traceId);
889,688✔
1623

1624
  vInfo("vgId:%d, start to process vnode-drop-stb, QID:0x%" PRIx64 ":0x%" PRIx64 ", drop stb:%s", TD_VID(pVnode), trace ? trace->rootId : 0, 
889,688✔
1625
              trace ? trace->msgId : 0, req.name);
1626

1627
  // process request
1628
  tbUidList = taosArrayInit(8, sizeof(int64_t));
890,276✔
1629
  if (tbUidList == NULL) goto _exit;
890,276✔
1630
  if (metaDropSuperTable(pVnode->pMeta, ver, &req) < 0) {
890,276✔
1631
    rcode = terrno;
1,482✔
1632
    goto _exit;
1,482✔
1633
  }
1634

1635
  if (tqUpdateTbUidList(pVnode->pTq, tbUidList, false) < 0) {
888,794✔
1636
    rcode = terrno;
×
1637
    goto _exit;
×
1638
  }
1639

1640
  // return rsp
1641
_exit:
890,276✔
1642
  vInfo("vgId:%d, finished to process vnode-drop-stb, QID:0x%" PRIx64 ":0x%" PRIx64 ", drop stb:%s", TD_VID(pVnode), trace ? trace->rootId : 0, 
890,276✔
1643
              trace ? trace->msgId : 0, req.name);
1644
  if (tbUidList) taosArrayDestroy(tbUidList);
890,276✔
1645
  pRsp->code = rcode;
890,276✔
1646
  tDecoderClear(&decoder);
890,276✔
1647
  return 0;
890,276✔
1648
}
1649

1650
static int32_t vnodeProcessAlterTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
11,810,681✔
1651
  SVAlterTbReq  vAlterTbReq = {0};
11,810,681✔
1652
  SVAlterTbRsp  vAlterTbRsp = {0};
11,810,681✔
1653
  SDecoder      dc = {0};
11,810,681✔
1654
  int32_t       code = 0;
11,810,681✔
1655
  int32_t       lino = 0;
11,810,681✔
1656
  int32_t       ret;
1657
  SEncoder      ec = {0};
11,810,681✔
1658
  STableMetaRsp vMetaRsp = {0};
11,810,681✔
1659

1660
  pRsp->msgType = TDMT_VND_ALTER_TABLE_RSP;
11,810,681✔
1661
  pRsp->pCont = NULL;
11,810,681✔
1662
  pRsp->contLen = 0;
11,810,681✔
1663
  pRsp->code = TSDB_CODE_SUCCESS;
11,810,681✔
1664

1665
  tDecoderInit(&dc, pReq, len);
11,810,681✔
1666

1667
  // decode
1668
  if (tDecodeSVAlterTbReq(&dc, &vAlterTbReq) < 0) {
11,810,681✔
1669
    vAlterTbRsp.code = TSDB_CODE_INVALID_MSG;
×
1670
    tDecoderClear(&dc);
×
1671
    goto _exit;
×
1672
  }
1673

1674
  // process
1675
  if (metaAlterTable(pVnode->pMeta, ver, &vAlterTbReq, &vMetaRsp) < 0) {
11,810,681✔
1676
    vAlterTbRsp.code = terrno;
669,037✔
1677
    tDecoderClear(&dc);
669,037✔
1678
    goto _exit;
669,037✔
1679
  }
1680
  tDecoderClear(&dc);
11,141,644✔
1681

1682
  if (NULL != vMetaRsp.pSchemas) {
11,141,644✔
1683
    vnodeUpdateMetaRsp(pVnode, &vMetaRsp);
3,656,544✔
1684
    vAlterTbRsp.pMeta = &vMetaRsp;
3,656,544✔
1685
  }
1686

1687
  if (vAlterTbReq.action == TSDB_ALTER_TABLE_UPDATE_TAG_VAL ||
11,141,644✔
1688
      vAlterTbReq.action == TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL) {
3,689,583✔
1689
    int64_t uid = metaGetTableEntryUidByName(pVnode->pMeta, vAlterTbReq.tbName);
7,454,685✔
1690
    if (uid == 0) {
7,454,685✔
1691
      vError("vgId:%d, %s failed at %s:%d since table %s not found", TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
1692
             vAlterTbReq.tbName);
1693
      goto _exit;
×
1694
    }
1695

1696
    SArray *tbUids = taosArrayInit(4, sizeof(int64_t));
7,454,685✔
1697
    void   *p = taosArrayPush(tbUids, &uid);
7,454,685✔
1698
    TSDB_CHECK_NULL(p, code, lino, _exit, terrno);
7,454,685✔
1699

1700
    vDebug("vgId:%d, remove tags value altered table:%s from query table list", TD_VID(pVnode), vAlterTbReq.tbName);
7,454,685✔
1701
    if ((code = tqUpdateTbUidList(pVnode->pTq, tbUids, false)) < 0) {
7,454,685✔
1702
      vError("vgId:%d, failed to remove tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1703
    }
1704

1705
    vDebug("vgId:%d, try to add table:%s in query table list", TD_VID(pVnode), vAlterTbReq.tbName);
7,454,685✔
1706
    if ((code = tqUpdateTbUidList(pVnode->pTq, tbUids, true)) < 0) {
7,454,685✔
1707
      vError("vgId:%d, failed to add tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1708
    }
1709

1710
    taosArrayDestroy(tbUids);
7,454,685✔
1711
  }
1712

1713
_exit:
11,810,681✔
1714
  taosArrayDestroy(vAlterTbReq.pMultiTag);
11,810,681✔
1715
  tEncodeSize(tEncodeSVAlterTbRsp, &vAlterTbRsp, pRsp->contLen, ret);
11,810,681✔
1716
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
11,810,681✔
1717
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
11,810,681✔
1718
  if (tEncodeSVAlterTbRsp(&ec, &vAlterTbRsp) != 0) {
11,810,681✔
1719
    vError("vgId:%d, failed to encode alter table response", TD_VID(pVnode));
×
1720
  }
1721

1722
  tEncoderClear(&ec);
11,810,681✔
1723
  if (vMetaRsp.pSchemas) {
11,810,681✔
1724
    taosMemoryFree(vMetaRsp.pSchemas);
3,656,544✔
1725
    taosMemoryFree(vMetaRsp.pSchemaExt);
3,656,544✔
1726
  }
1727
  if (vMetaRsp.pColRefs) {
11,810,681✔
1728
    taosMemoryFree(vMetaRsp.pColRefs);
259,230✔
1729
  }
1730
  return 0;
11,810,681✔
1731
}
1732

1733
static int32_t vnodeProcessDropTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
1,337,650✔
1734
                                     SRpcMsg *pOriginRpc) {
1735
  SVDropTbBatchReq req = {0};
1,337,650✔
1736
  SVDropTbBatchRsp rsp = {0};
1,337,650✔
1737
  SDecoder         decoder = {0};
1,337,650✔
1738
  SEncoder         encoder = {0};
1,337,650✔
1739
  int32_t          ret;
1740
  SArray          *tbUids = NULL;
1,337,650✔
1741
  SArray          *tbNames = NULL;
1,337,650✔
1742
  int64_t          tss = taosGetTimestampMs();
1,337,650✔
1743

1744
  pRsp->msgType = ((SRpcMsg *)pReq)->msgType + 1;
1,337,650✔
1745
  pRsp->pCont = NULL;
1,337,650✔
1746
  pRsp->contLen = 0;
1,337,650✔
1747
  pRsp->code = TSDB_CODE_SUCCESS;
1,337,650✔
1748

1749
  // decode req
1750
  tDecoderInit(&decoder, pReq, len);
1,337,650✔
1751
  ret = tDecodeSVDropTbBatchReq(&decoder, &req);
1,337,650✔
1752
  if (ret < 0) {
1,337,650✔
1753
    terrno = TSDB_CODE_INVALID_MSG;
×
1754
    pRsp->code = terrno;
×
1755
    goto _exit;
×
1756
  }
1757

1758
  // process req
1759
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
1,337,650✔
1760
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(SVDropTbRsp));
1,337,650✔
1761
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
1,337,650✔
1762
  if (tbUids == NULL || rsp.pArray == NULL || tbNames == NULL) goto _exit;
1,337,650✔
1763

1764
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
2,730,051✔
1765
    SVDropTbReq *pDropTbReq = req.pReqs + iReq;
1,392,401✔
1766
    SVDropTbRsp  dropTbRsp = {0};
1,392,401✔
1767
    tb_uid_t     tbUid = 0;
1,392,401✔
1768

1769
    /* code */
1770
    ret = metaDropTable2(pVnode->pMeta, ver, pDropTbReq);
1,392,401✔
1771
    if (ret < 0) {
1,392,401✔
1772
      if (pDropTbReq->igNotExists && terrno == TSDB_CODE_TDB_TABLE_NOT_EXIST) {
×
1773
        dropTbRsp.code = TSDB_CODE_SUCCESS;
×
1774
      } else {
1775
        dropTbRsp.code = terrno;
×
1776
      }
1777
    } else {
1778
      dropTbRsp.code = TSDB_CODE_SUCCESS;
1,392,401✔
1779
    }
1780

1781
    if (taosArrayPush(rsp.pArray, &dropTbRsp) == NULL) {
2,784,802✔
1782
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1783
      pRsp->code = terrno;
×
1784
      goto _exit;
×
1785
    }
1786

1787
    if (tsEnableAudit && tsEnableAuditCreateTable && tsAuditLevel >= AUDIT_LEVEL_CHILDTABLE) {
1,392,401✔
1788
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
×
1789
      if (str == NULL) {
×
1790
        pRsp->code = terrno;
×
1791
        goto _exit;
×
1792
      }
1793
      tstrncpy(str, pDropTbReq->name, TSDB_TABLE_FNAME_LEN);
×
1794
      if (taosArrayPush(tbNames, &str) == NULL) {
×
1795
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1796
        pRsp->code = terrno;
×
1797
        goto _exit;
×
1798
      }
1799
    }
1800
  }
1801

1802
  if (tqUpdateTbUidList(pVnode->pTq, tbUids, false) < 0) {
1,337,650✔
1803
    vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(terrno));
×
1804
  }
1805

1806
  if (tsEnableAudit && tsEnableAuditCreateTable && tsAuditLevel >= AUDIT_LEVEL_CHILDTABLE) {
1,337,650✔
1807
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
×
1808

1809
    SName name = {0};
×
1810
    if (tNameFromString(&name, pVnode->config.dbname, T_NAME_ACCT | T_NAME_DB) != 0) {
×
1811
      vError("vgId:%d, failed to get name from string", TD_VID(pVnode));
×
1812
    }
1813

1814
    SStringBuilder sb = {0};
×
1815
    for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
×
1816
      char **key = (char **)taosArrayGet(tbNames, iReq);
×
1817
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
×
1818
      if (iReq < req.nReqs - 1) {
×
1819
        taosStringBuilderAppendChar(&sb, ',');
×
1820
      }
1821
      taosMemoryFreeClear(*key);
×
1822
    }
1823

1824
    size_t len = 0;
×
1825
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
×
1826

1827
    if (strlen(RPC_MSG_USER(pOriginRpc)) > 0) {
×
1828
      int64_t tse = taosGetTimestampMs();
×
1829
      double  duration = (double)(tse - tss);
×
1830
      duration = duration / 1000;
×
1831
      auditAddRecord(pOriginRpc, clusterId, "dropTable", name.dbname, "", keyJoined, len, duration, 0);
×
1832
    }
1833

1834
    taosStringBuilderDestroy(&sb);
×
1835
  }
1836

1837
_exit:
1,337,650✔
1838
  taosArrayDestroy(tbUids);
1,337,650✔
1839
  tDecoderClear(&decoder);
1,337,650✔
1840
  tEncodeSize(tEncodeSVDropTbBatchRsp, &rsp, pRsp->contLen, ret);
1,337,650✔
1841
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
1,337,650✔
1842
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
1,337,650✔
1843
  if (tEncodeSVDropTbBatchRsp(&encoder, &rsp) != 0) {
1,337,650✔
1844
    vError("vgId:%d, failed to encode drop table batch response", TD_VID(pVnode));
×
1845
  }
1846
  tEncoderClear(&encoder);
1,337,650✔
1847
  taosArrayDestroy(rsp.pArray);
1,337,650✔
1848
  taosArrayDestroy(tbNames);
1,337,389✔
1849
  return 0;
1,337,389✔
1850
}
1851

1852
static int32_t vnodeProcessCreateRsmaReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
22,230✔
1853
  int32_t         code = 0, lino = 0;
22,230✔
1854
  SVCreateRsmaReq req = {0};
22,230✔
1855
  TAOS_CHECK_EXIT(tDeserializeSVCreateRsmaReq(pReq, len, &req));
22,230✔
1856
  TAOS_CHECK_EXIT(metaCreateRsma(pVnode->pMeta, ver, &req));
22,230✔
1857
_exit:
22,230✔
1858
  pRsp->msgType = TDMT_VND_CREATE_RSMA_RSP;
22,230✔
1859
  pRsp->pCont = NULL;
22,230✔
1860
  pRsp->code = code;
22,230✔
1861
  pRsp->contLen = 0;
22,230✔
1862
  tFreeSVCreateRsmaReq(&req);
22,230✔
1863
  return code;
22,230✔
1864
}
1865

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

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

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

1902
  tInitSubmitBlkIter(msgIter, pBlock, &blkIter);
1903
  if (blkIter.row == NULL) return 0;
1904

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

1912
  suid = msgIter->suid;
1913
  rv = TD_ROW_SVER(blkIter.row);
1914

1915
  char __tags[128] = {0};
1916
  snprintf(__tags, 128, "%s: uid %" PRIi64 " ", tags, msgIter->uid);
1917
  while ((row = tGetSubmitBlkNext(&blkIter))) {
1918
    tdSRowPrint(row, pSchema, __tags);
1919
  }
1920

1921
  taosMemoryFreeClear(pSchema);
1922

1923
  return TSDB_CODE_SUCCESS;
1924
}
1925
#endif
1926
typedef struct SSubmitReqConvertCxt {
1927
  SSubmitMsgIter msgIter;
1928
  SSubmitBlk    *pBlock;
1929
  SSubmitBlkIter blkIter;
1930
  STSRow        *pRow;
1931
  STSRowIter     rowIter;
1932
  SSubmitTbData *pTbData;
1933
  STSchema      *pTbSchema;
1934
  SArray        *pColValues;
1935
} SSubmitReqConvertCxt;
1936

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

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

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

1975
  return TSDB_CODE_SUCCESS;
×
1976
}
1977

1978
static void vnodeDestroySubmitReqConvertCxt(SSubmitReqConvertCxt *pCxt) {
×
1979
  taosMemoryFreeClear(pCxt->pTbSchema);
×
1980
  tDestroySubmitTbData(pCxt->pTbData, TSDB_MSG_FLG_ENCODE);
×
1981
  taosMemoryFreeClear(pCxt->pTbData);
×
1982
  taosArrayDestroy(pCxt->pColValues);
×
1983
}
×
1984

1985
static int32_t vnodeCellValConvertToColVal(STColumn *pCol, SCellVal *pCellVal, SColVal *pColVal) {
×
1986
  if (tdValTypeIsNone(pCellVal->valType)) {
×
1987
    pColVal->flag = CV_FLAG_NONE;
×
1988
    return TSDB_CODE_SUCCESS;
×
1989
  }
1990

1991
  if (tdValTypeIsNull(pCellVal->valType)) {
×
1992
    pColVal->flag = CV_FLAG_NULL;
×
1993
    return TSDB_CODE_SUCCESS;
×
1994
  }
1995

1996
  if (IS_VAR_DATA_TYPE(pCol->type)) {
×
1997
    if (IS_STR_DATA_BLOB(pCol->type)) {
×
1998
      pColVal->value.nData = blobDataLen(pCellVal->val);
×
1999
      pColVal->value.pData = (uint8_t *)blobDataVal(pCellVal->val);
×
2000

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

2014
  pColVal->flag = CV_FLAG_VALUE;
×
2015
  return TSDB_CODE_SUCCESS;
×
2016
}
2017

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

2032
static int32_t vnodeDecodeCreateTbReq(SSubmitReqConvertCxt *pCxt) {
×
2033
  if (pCxt->msgIter.schemaLen <= 0) {
×
2034
    return TSDB_CODE_SUCCESS;
×
2035
  }
2036

2037
  pCxt->pTbData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
×
2038
  if (NULL == pCxt->pTbData->pCreateTbReq) {
×
2039
    return terrno;
×
2040
  }
2041

2042
  SDecoder decoder = {0};
×
2043
  tDecoderInit(&decoder, (uint8_t *)pCxt->pBlock->data, pCxt->msgIter.schemaLen);
×
2044
  int32_t code = tDecodeSVCreateTbReq(&decoder, pCxt->pTbData->pCreateTbReq);
×
2045
  tDecoderClear(&decoder);
×
2046

2047
  return code;
×
2048
}
2049

2050
static int32_t vnodeSubmitReqConvertToSubmitReq2(SVnode *pVnode, SSubmitReq *pReq, SSubmitReq2 *pReq2) {
×
2051
  pReq2->aSubmitTbData = taosArrayInit(128, sizeof(SSubmitTbData));
×
2052
  if (NULL == pReq2->aSubmitTbData) {
×
2053
    return terrno;
×
2054
  }
2055

2056
  SSubmitReqConvertCxt cxt = {0};
×
2057

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

2078
        SRowBuildScanInfo sinfo = {0};
×
2079
        code = tRowBuild(cxt.pColValues, cxt.pTbSchema, pNewRow, &sinfo);
×
2080
      }
2081
    }
2082
    if (TSDB_CODE_SUCCESS == code) {
×
2083
      code = (NULL == taosArrayPush(pReq2->aSubmitTbData, cxt.pTbData) ? terrno : TSDB_CODE_SUCCESS);
×
2084
    }
2085
    if (TSDB_CODE_SUCCESS == code) {
×
2086
      taosMemoryFreeClear(cxt.pTbData);
×
2087
    }
2088
  }
2089

2090
  vnodeDestroySubmitReqConvertCxt(&cxt);
×
2091
  return code;
×
2092
}
2093

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

2117
static int32_t buildExistSubTalbeRsp(SVnode *pVnode, SSubmitTbData *pSubmitTbData, STableMetaRsp **ppRsp) {
59,690✔
2118
  int32_t code = 0;
59,690✔
2119
  int32_t lino = 0;
59,690✔
2120

2121
  SMetaEntry *pEntry = NULL;
59,690✔
2122
  code = metaFetchEntryByUid(pVnode->pMeta, pSubmitTbData->suid, &pEntry);
59,690✔
2123
  if (code) {
59,690✔
2124
    vError("vgId:%d, table uid:%" PRId64 " not exists, line:%d", TD_VID(pVnode), pSubmitTbData->uid, __LINE__);
×
2125
    TSDB_CHECK_CODE(code, lino, _exit);
×
2126
  }
2127
  if (pEntry->type != TSDB_SUPER_TABLE) {
59,690✔
2128
    vError("vgId:%d, table uid:%" PRId64 " exists, but is not super table, line:%d", TD_VID(pVnode), pSubmitTbData->uid,
×
2129
           __LINE__);
2130
    code = TSDB_CODE_STREAM_INSERT_SCHEMA_NOT_MATCH;
×
2131
    TSDB_CHECK_CODE(code, lino, _exit);
×
2132
  }
2133

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

2165
  if (pEntry->stbEntry.schemaRow.version != pSubmitTbData->sver) {
59,690✔
2166
    TSDB_CHECK_CODE(code = TSDB_CODE_TDB_TABLE_ALREADY_EXIST, lino, _exit);
432✔
2167
  }
2168
_exit:
59,690✔
2169
  metaFetchEntryFree(&pEntry);
59,690✔
2170
  if (code != TSDB_CODE_SUCCESS) {
59,690✔
2171
    vError("vgId:%d, failed to build exist sub table response, code:0x%0x, line:%d", TD_VID(pVnode), code, lino);
432✔
2172
  }
2173
  return code;
59,690✔
2174
}
2175

2176
static int32_t buildExistNormalTalbeRsp(SVnode *pVnode, int64_t uid, STableMetaRsp **ppRsp) {
31,058✔
2177
  int32_t code = 0;
31,058✔
2178
  int32_t lino = 0;
31,058✔
2179

2180
  SMetaEntry *pEntry = NULL;
31,058✔
2181
  code = metaFetchEntryByUid(pVnode->pMeta, uid, &pEntry);
31,058✔
2182
  if (code) {
31,058✔
2183
    vError("vgId:%d, table uid:%" PRId64 " not exists, line:%d", TD_VID(pVnode), uid, __LINE__);
×
2184
    TSDB_CHECK_CODE(code, lino, _exit);
×
2185
  }
2186
  if (pEntry->type != TSDB_NORMAL_TABLE) {
31,058✔
2187
    vError("vgId:%d, table uid:%" PRId64 " exists, but is not normal table, line:%d", TD_VID(pVnode), uid, __LINE__);
×
2188
    code = TSDB_CODE_STREAM_INSERT_SCHEMA_NOT_MATCH;
×
2189
    TSDB_CHECK_CODE(code, lino, _exit);
×
2190
  }
2191

2192
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
31,058✔
2193
  if (NULL == *ppRsp) {
31,058✔
2194
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2195
  }
2196

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

2217
_exit:
31,058✔
2218
  metaFetchEntryFree(&pEntry);
31,058✔
2219
  if (code != TSDB_CODE_SUCCESS) {
31,058✔
2220
    vError("vgId:%d, failed to build exist normal table response, code:%d, line:%d", TD_VID(pVnode), code, lino);
×
2221
  }
2222
  return code;
31,058✔
2223
}
2224

2225
static int32_t buildExistTableInStreamRsp(SVnode *pVnode, SSubmitTbData *pSubmitTbData, STableMetaRsp **ppRsp) {
292,655✔
2226
  if (pSubmitTbData->pCreateTbReq->flags & TD_CREATE_NORMAL_TB_IN_STREAM) {
292,655✔
2227
    int32_t code = buildExistNormalTalbeRsp(pVnode, pSubmitTbData->uid, ppRsp);
31,058✔
2228
    if (code) {
31,058✔
2229
      vError("vgId:%d, table uid:%" PRId64 " not exists, line:%d", TD_VID(pVnode), pSubmitTbData->uid, __LINE__);
×
2230
      return code;
×
2231
    }
2232
    return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
31,058✔
2233
  } else if (pSubmitTbData->pCreateTbReq->flags & TD_CREATE_SUB_TB_IN_STREAM) {
261,597✔
2234
    return buildExistSubTalbeRsp(pVnode, pSubmitTbData, ppRsp);
59,690✔
2235
  }
2236
  return TSDB_CODE_SUCCESS;
201,907✔
2237
}
2238

2239
static int32_t vnodeHandleAutoCreateTable(SVnode      *pVnode,    // vnode
683,783,334✔
2240
                                          int64_t      version,   // version
2241
                                          SSubmitReq2 *pRequest,  // request
2242
                                          SSubmitRsp2 *pResponse  // response
2243
) {
2244
  int32_t code = TSDB_CODE_SUCCESS;
683,783,334✔
2245
  int32_t numTbData = taosArrayGetSize(pRequest->aSubmitTbData);
683,783,334✔
2246
  SArray *newTbUids = NULL;
683,783,017✔
2247

2248
  for (int32_t i = 0; i < numTbData; ++i) {
1,394,260,506✔
2249
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
710,510,805✔
2250

2251
    if (pTbData->pCreateTbReq == NULL) {
710,510,801✔
2252
      continue;
698,919,721✔
2253
    }
2254

2255
    pTbData->uid = pTbData->pCreateTbReq->uid;
11,592,317✔
2256

2257
    // Alloc necessary resources
2258
    if (pResponse->aCreateTbRsp == NULL) {
11,591,627✔
2259
      pResponse->aCreateTbRsp = taosArrayInit(numTbData, sizeof(SVCreateTbRsp));
8,761,679✔
2260
      if (pResponse->aCreateTbRsp == NULL) {
8,761,850✔
2261
        code = terrno;
×
2262
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2263
               tstrerror(code), version);
2264
        taosArrayDestroy(newTbUids);
×
2265
        return code;
×
2266
      }
2267
    }
2268

2269
    // Do create table
2270
    vDebug("vgId:%d start to handle auto create table, version:%" PRId64, TD_VID(pVnode), version);
11,591,798✔
2271

2272
    SVCreateTbRsp *pCreateTbRsp = taosArrayReserve(pResponse->aCreateTbRsp, 1);
11,591,798✔
2273
    code = metaCreateTable2(pVnode->pMeta, version, pTbData->pCreateTbReq, &pCreateTbRsp->pMeta);
11,592,317✔
2274
    if (code == TSDB_CODE_SUCCESS) {
11,592,361✔
2275
      // Allocate necessary resources
2276
      if (newTbUids == NULL) {
11,241,606✔
2277
        newTbUids = taosArrayInit(numTbData, sizeof(int64_t));
8,469,028✔
2278
        if (newTbUids == NULL) {
8,469,345✔
2279
          code = terrno;
×
2280
          vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2281
                 tstrerror(code), version);
2282
          return code;
×
2283
        }
2284
      }
2285

2286
      if (taosArrayPush(newTbUids, &pTbData->uid) == NULL) {
22,483,529✔
2287
        code = terrno;
×
2288
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2289
               tstrerror(code), version);
2290
        taosArrayDestroy(newTbUids);
×
2291
        return code;
×
2292
      }
2293

2294
      if (pCreateTbRsp->pMeta) {
11,241,606✔
2295
        vnodeUpdateMetaRsp(pVnode, pCreateTbRsp->pMeta);
11,241,289✔
2296
      }
2297
    } else if (code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
350,755✔
2298
      code = terrno = 0;
349,715✔
2299
      pTbData->uid = pTbData->pCreateTbReq->uid;  // update uid if table exist for using below
349,715✔
2300

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

2322
  // Update the affected table uid list
2323
  if (taosArrayGetSize(newTbUids) > 0) {
683,749,701✔
2324
    vDebug("vgId:%d, add %d table into query table list in handling submit", TD_VID(pVnode),
8,468,559✔
2325
           (int32_t)taosArrayGetSize(newTbUids));
2326
    if (tqUpdateTbUidList(pVnode->pTq, newTbUids, true) != 0) {
8,468,559✔
2327
      vError("vgId:%d, failed to update tbUid list", TD_VID(pVnode));
×
2328
    }
2329
  }
2330

2331
  vDebug("vgId:%d, handle auto create table done, version:%" PRId64, TD_VID(pVnode), version);
683,751,201✔
2332

2333
  taosArrayDestroy(newTbUids);
683,751,201✔
2334
  return code;
683,750,986✔
2335
}
2336

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

2367
  TSDB_CHECK_CODE(code, lino, _exit);
×
2368
_exit:
×
2369
  if (code != TSDB_CODE_SUCCESS) {
×
2370
    vError("vgId:%d, failed to add exist table info into response, code:0x%0x, line:%d", TD_VID(pVnode), code, lino);
×
2371
  }
2372
  return;
×
2373
}
2374

2375
static int32_t vnodeHandleDataWrite(SVnode *pVnode, int64_t version, SSubmitReq2 *pRequest, SSubmitRsp2 *pResponse) {
683,750,986✔
2376
  int32_t code = TSDB_CODE_SUCCESS;
683,750,986✔
2377
  int32_t numTbData = taosArrayGetSize(pRequest->aSubmitTbData);
683,750,986✔
2378
  int8_t  hasBlob = 0;
683,749,419✔
2379

2380
  // Scan submit data
2381
  for (int32_t i = 0; i < numTbData; ++i) {
1,394,670,204✔
2382
    SMetaInfo      info = {0};
710,924,468✔
2383
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
710,924,919✔
2384

2385
    if (pTbData->flags & SUBMIT_REQ_WITH_BLOB) {
710,926,299✔
2386
      hasBlob = 1;
21,629✔
2387
    }
2388
    if (pTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
710,926,299✔
2389
      continue;  // skip column data format
3,320,208✔
2390
    }
2391
    if (pTbData->flags & SUBMIT_REQ_ONLY_CREATE_TABLE) {
707,604,737✔
2392
      continue;  // skip only crate table request
2,483,825✔
2393
    }
2394

2395
    code = metaGetInfo(pVnode->pMeta, pTbData->uid, &info, NULL);
705,121,221✔
2396
    if (code) {
705,119,692✔
2397
      code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
2,474✔
2398
      vWarn("vgId:%d, error occurred at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __FILE__,
2,474✔
2399
            __LINE__, tstrerror(code), version, pTbData->uid);
2400
      return code;
2,474✔
2401
    }
2402

2403
    if (info.suid != pTbData->suid) {
705,117,218✔
2404
      code = TSDB_CODE_INVALID_MSG;
×
2405
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64 " suid:%" PRId64
×
2406
             " info.suid:%" PRId64,
2407
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), version, pTbData->uid, pTbData->suid,
2408
             info.suid);
2409
      return code;
×
2410
    }
2411

2412
    if (info.suid) {
705,114,030✔
2413
      code = metaGetInfo(pVnode->pMeta, info.suid, &info, NULL);
518,167,289✔
2414
      if (code) {
518,172,542✔
2415
        code = TSDB_CODE_INTERNAL_ERROR;
×
2416
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " suid:%" PRId64, TD_VID(pVnode), __func__,
×
2417
               __FILE__, __LINE__, tstrerror(code), version, info.suid);
2418
        return code;
×
2419
      }
2420
    }
2421

2422
    if (pTbData->sver != info.skmVer) {
705,119,283✔
2423
      code = TSDB_CODE_TDB_INVALID_TABLE_SCHEMA_VER;
432✔
2424
      addExistTableInfoIntoRes(pVnode, pRequest, pResponse, pTbData, numTbData);
432✔
2425
      vDebug("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64
432✔
2426
             " sver:%d"
2427
             " info.skmVer:%d",
2428
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), version, pTbData->uid, pTbData->sver,
2429
             info.skmVer);
2430
      return code;
432✔
2431
    }
2432
  }
2433

2434
  // Do write data
2435
  vDebug("vgId:%d start to handle data write, version:%" PRId64, TD_VID(pVnode), version);
683,745,736✔
2436

2437
  for (int32_t i = 0; i < numTbData; ++i) {
1,394,631,687✔
2438
    int32_t        affectedRows = 0;
710,914,459✔
2439
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
710,912,486✔
2440

2441
    if (pTbData->flags & SUBMIT_REQ_ONLY_CREATE_TABLE) {
710,914,199✔
2442
      continue;
2,483,825✔
2443
    }
2444

2445
    if (hasBlob) {
708,429,495✔
2446
      code = vnodeSubmitBlobData(pVnode, pTbData);
21,629✔
2447
      if (code) {
21,629✔
2448
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2449
               __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2450
        return code;
×
2451
      }
2452
    }
2453

2454
    code = tsdbInsertTableData(pVnode->pTsdb, version, pTbData, &affectedRows);
708,429,495✔
2455
    if (code) {
708,406,670✔
2456
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2457
             __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2458
      return code;
×
2459
    }
2460

2461
    code = metaUpdateChangeTimeWithLock(pVnode->pMeta, pTbData->uid, pTbData->ctimeMs);
708,406,670✔
2462
    if (code) {
708,383,809✔
2463
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2464
             __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2465
      return code;
×
2466
    }
2467
    pResponse->affectedRows += affectedRows;
708,383,809✔
2468
  }
2469

2470
  vDebug("vgId:%d, handle data write done, version:%" PRId64 ", affectedRows:%d", TD_VID(pVnode), version,
683,717,228✔
2471
         pResponse->affectedRows);
2472
  return code;
683,748,794✔
2473
}
2474

2475
static int32_t vnodeScanColumnData(SVnode *pVnode, SSubmitTbData *pTbData, TSKEY minKey, TSKEY maxKey) {
3,317,728✔
2476
  int32_t code = 0;
3,317,728✔
2477

2478
  int32_t   numCols = taosArrayGetSize(pTbData->aCol);
3,317,728✔
2479
  SColData *aColData = (SColData *)TARRAY_DATA(pTbData->aCol);
3,317,411✔
2480

2481
  if (numCols <= 0) {
3,319,257✔
2482
    code = TSDB_CODE_INVALID_MSG;
×
2483
    vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " numCols:%d", TD_VID(pVnode), __func__,
×
2484
           __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, numCols);
2485
    return code;
×
2486
  }
2487

2488
  if (aColData[0].cid != PRIMARYKEY_TIMESTAMP_COL_ID || aColData[0].type != TSDB_DATA_TYPE_TIMESTAMP ||
3,319,257✔
2489
      aColData[0].nVal <= 0) {
3,319,891✔
2490
    code = TSDB_CODE_INVALID_MSG;
3✔
2491
    vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64
3✔
2492
           " first column is not primary key timestamp, cid:%d type:%d nVal:%d",
2493
           TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, aColData[0].cid,
2494
           aColData[0].type, aColData[0].nVal);
2495
    return code;
×
2496
  }
2497

2498
  for (int32_t i = 1; i < numCols; ++i) {
10,466,150✔
2499
    if (aColData[i].nVal != aColData[0].nVal) {
7,149,059✔
2500
      code = TSDB_CODE_INVALID_MSG;
×
2501
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64
×
2502
             " column cid:%d type:%d nVal:%d is not equal to primary key timestamp nVal:%d",
2503
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid,
2504
             aColData[i].cid, aColData[i].type, aColData[i].nVal, aColData[0].nVal);
2505
      return code;
×
2506
    }
2507
  }
2508

2509
  SRowKey *pLastKey = NULL;
3,317,091✔
2510
  SRowKey  lastKey = {0};
3,317,091✔
2511
  for (int32_t i = 0; i < aColData[0].nVal; ++i) {
2,147,483,647✔
2512
    SRowKey key = {0};
2,147,483,647✔
2513

2514
    tColDataArrGetRowKey(aColData, numCols, i, &key);
2,147,483,647✔
2515

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

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

2536
    *pLastKey = key;
2,147,483,647✔
2537
  }
2538

2539
  return code;
3,320,208✔
2540
}
2541

2542
static int32_t vnodeScanSubmitRowData(SVnode *pVnode, SSubmitTbData *pTbData, TSKEY minKey, TSKEY maxKey) {
707,606,158✔
2543
  int32_t code = 0;
707,606,158✔
2544

2545
  int32_t numRows = taosArrayGetSize(pTbData->aRowP);
707,606,158✔
2546
  SRow  **aRow = (SRow **)TARRAY_DATA(pTbData->aRowP);
707,615,228✔
2547

2548
  if (numRows <= 0 && (pTbData->flags & SUBMIT_REQ_ONLY_CREATE_TABLE) == 0) {
707,600,401✔
2549
    code = TSDB_CODE_INVALID_MSG;
×
2550
    vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " numRows:%d", TD_VID(pVnode), __func__,
×
2551
           __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, numRows);
2552
    return code;
×
2553
  }
2554

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

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

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

2589
    *pLastKey = key;
2,147,483,647✔
2590
  }
2591

2592
  return code;
701,666,597✔
2593
}
2594

2595
static int32_t vnodeScanSubmitReq(SVnode *pVnode, int64_t version, SSubmitReq2 *pRequest, SSubmitRsp2 *pResponse) {
683,768,512✔
2596
  int32_t code = TSDB_CODE_SUCCESS;
683,768,512✔
2597
  int32_t numTbData = taosArrayGetSize(pRequest->aSubmitTbData);
683,768,512✔
2598

2599
  TSKEY now = taosGetTimestamp(pVnode->config.tsdbCfg.precision);
683,764,293✔
2600
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * pVnode->config.tsdbCfg.keep2;
683,773,543✔
2601
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
683,761,916✔
2602
  for (int32_t i = 0; i < numTbData; i++) {
1,394,643,016✔
2603
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
710,891,514✔
2604

2605
    if (pTbData->pCreateTbReq && pTbData->pCreateTbReq->uid == 0) {
710,918,412✔
2606
      code = TSDB_CODE_INVALID_MSG;
×
2607
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2608
             tstrerror(code), version);
2609
      return code;
×
2610
    }
2611

2612
    if (pTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
710,920,633✔
2613
      code = vnodeScanColumnData(pVnode, pTbData, minKey, maxKey);
3,318,623✔
2614
      if (code) {
3,320,208✔
2615
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2616
               __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2617
        return code;
×
2618
      }
2619
    } else {
2620
      code = vnodeScanSubmitRowData(pVnode, pTbData, minKey, maxKey);
707,595,091✔
2621
      if (code) {
707,574,837✔
2622
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
24,587✔
2623
               __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2624
        return code;
×
2625
      }
2626
    }
2627
  }
2628

2629
  return code;
683,751,502✔
2630
}
2631

2632
static int32_t vnodeProcessSubmitReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
683,780,947✔
2633
                                     SRpcMsg *pOriginalMsg) {
2634
  int32_t code = 0;
683,780,947✔
2635
  int32_t lino = 0;
683,780,947✔
2636
  terrno = 0;
683,780,947✔
2637

2638
  SSubmitReq2 *pSubmitReq = &(SSubmitReq2){0};
683,782,491✔
2639
  SSubmitRsp2 *pSubmitRsp = &(SSubmitRsp2){0};
683,782,491✔
2640
  int32_t      ret;
2641
  SEncoder     ec = {0};
683,778,446✔
2642

2643
  pRsp->code = TSDB_CODE_SUCCESS;
683,779,200✔
2644

2645
  void           *pAllocMsg = NULL;
683,779,345✔
2646
  SSubmitReq2Msg *pMsg = (SSubmitReq2Msg *)pReq;
683,779,345✔
2647
  SDecoder        dc = {0};
683,779,345✔
2648
  if (0 == taosHton64(pMsg->version)) {
683,780,059✔
2649
    code = vnodeSubmitReqConvertToSubmitReq2(pVnode, (SSubmitReq *)pMsg, pSubmitReq);
×
2650
    if (TSDB_CODE_SUCCESS == code) {
×
2651
      code = vnodeRebuildSubmitReqMsg(pSubmitReq, &pReq);
×
2652
    }
2653
    if (TSDB_CODE_SUCCESS == code) {
×
2654
      pAllocMsg = pReq;
×
2655
    }
2656
    TSDB_CHECK_CODE(code, lino, _exit);
×
2657
  } else {
2658
    // decode
2659
    pReq = POINTER_SHIFT(pReq, sizeof(SSubmitReq2Msg));
683,780,250✔
2660
    len -= sizeof(SSubmitReq2Msg);
683,780,116✔
2661

2662
    tDecoderInit(&dc, pReq, len);
683,780,116✔
2663
    if (tDecodeSubmitReq(&dc, pSubmitReq, NULL) < 0) {
683,779,257✔
2664
      code = TSDB_CODE_INVALID_MSG;
×
2665
      TSDB_CHECK_CODE(code, lino, _exit);
×
2666
    }
2667
  }
2668

2669
  // Scan the request
2670
  code = vnodeScanSubmitReq(pVnode, ver, pSubmitReq, pSubmitRsp);
683,770,038✔
2671
  if (code) {
683,744,061✔
2672
    vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2673
           tstrerror(code), ver);
2674
    TSDB_CHECK_CODE(code, lino, _exit);
×
2675
  }
2676

2677
  vGDebug(pOriginalMsg ? &pOriginalMsg->info.traceId : NULL, "vgId:%d, index:%" PRId64 ", submit block, rows:%d",
683,744,061✔
2678
          TD_VID(pVnode), ver, (int32_t)taosArrayGetSize(pSubmitReq->aSubmitTbData));
2679

2680
  // Handle auto create table
2681
  code = vnodeHandleAutoCreateTable(pVnode, ver, pSubmitReq, pSubmitRsp);
683,761,781✔
2682
  if (code) {
683,783,516✔
2683
    vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
32,530✔
2684
           tstrerror(code), ver);
2685
    TSDB_CHECK_CODE(code, lino, _exit);
32,530✔
2686
  }
2687

2688
  // Handle data write
2689
  code = vnodeHandleDataWrite(pVnode, ver, pSubmitReq, pSubmitRsp);
683,750,986✔
2690
  if (code) {
683,750,814✔
2691
    vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
2,906✔
2692
           tstrerror(code), ver);
2693
    TSDB_CHECK_CODE(code, lino, _exit);
2,906✔
2694
  }
2695

2696
_exit:
683,783,170✔
2697
  // message
2698
  pRsp->code = code;
683,784,300✔
2699
  tEncodeSize(tEncodeSSubmitRsp2, pSubmitRsp, pRsp->contLen, ret);
683,784,445✔
2700
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
683,782,960✔
2701
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
683,782,129✔
2702
  if (tEncodeSSubmitRsp2(&ec, pSubmitRsp) < 0) {
683,781,908✔
2703
    vError("vgId:%d, failed to encode submit response", TD_VID(pVnode));
×
2704
  }
2705
  tEncoderClear(&ec);
683,780,282✔
2706

2707
  // update statistics
2708
  (void)atomic_add_fetch_64(&pVnode->statis.nInsert, pSubmitRsp->affectedRows);
683,778,407✔
2709
  (void)atomic_add_fetch_64(&pVnode->statis.nInsertSuccess, pSubmitRsp->affectedRows);
683,782,649✔
2710
  (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsert, 1);
683,781,642✔
2711

2712
  // update metrics
2713
  METRICS_UPDATE(pVnode->writeMetrics.total_requests, METRIC_LEVEL_LOW, 1);
683,780,556✔
2714
  METRICS_UPDATE(pVnode->writeMetrics.total_rows, METRIC_LEVEL_HIGH, pSubmitRsp->affectedRows);
683,774,387✔
2715
  METRICS_UPDATE(pVnode->writeMetrics.total_bytes, METRIC_LEVEL_LOW, pMsg->header.contLen);
683,774,613✔
2716

2717
  if (tsEnableMonitor && tsMonitorFqdn[0] != 0 && tsMonitorPort != 0 && pSubmitRsp->affectedRows > 0 &&
683,774,637✔
2718
      strlen(RPC_MSG_USER(pOriginalMsg)) > 0 && tsInsertCounter != NULL) {
×
2719
    const char *sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT_AFFECTED_ROWS,
×
2720
                                   pVnode->monitor.strClusterId,
×
2721
                                   pVnode->monitor.strDnodeId,
×
2722
                                   tsLocalEp,
2723
                                   pVnode->monitor.strVgId,
×
2724
                                   RPC_MSG_USER(pOriginalMsg),
×
2725
                                   "Success"};
2726
    int         tv = taos_counter_add(tsInsertCounter, pSubmitRsp->affectedRows, sample_labels);
×
2727
  }
2728

2729
  if (code == 0) {
683,774,573✔
2730
    (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
683,739,137✔
2731
  }
2732

2733
  // clear
2734
  tDestroySubmitReq(pSubmitReq, 0 == taosHton64(pMsg->version) ? TSDB_MSG_FLG_CMPT : TSDB_MSG_FLG_DECODE);
683,779,891✔
2735
  tDestroySSubmitRsp2(pSubmitRsp, TSDB_MSG_FLG_ENCODE);
683,766,044✔
2736

2737
  if (code) {
683,751,728✔
2738
    terrno = code;
35,436✔
2739
    if (code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
35,436✔
2740
      vInfo("vgId:%d, failed to process submit request since %s, lino:%d, version:%" PRId64, TD_VID(pVnode),
31,490✔
2741
            tstrerror(code), lino, ver);
2742

2743
    } else {
2744
      vError("vgId:%d, failed to process submit request since %s, lino:%d, version:%" PRId64, TD_VID(pVnode),
3,946✔
2745
             tstrerror(code), lino, ver);
2746
    }
2747
  }
2748

2749
  taosMemoryFree(pAllocMsg);
683,762,175✔
2750
  tDecoderClear(&dc);
683,754,628✔
2751

2752
  return code;
683,782,540✔
2753
}
2754

2755
static int32_t vnodeConsolidateAlterHashRange(SVnode *pVnode, int64_t ver) {
24,380✔
2756
  int32_t code = TSDB_CODE_SUCCESS;
24,380✔
2757

2758
  vInfo("vgId:%d, trim meta of tables per hash range [%" PRIu32 ", %" PRIu32 "]. apply-index:%" PRId64, TD_VID(pVnode),
24,380✔
2759
        pVnode->config.hashBegin, pVnode->config.hashEnd, ver);
2760

2761
  // TODO: trim meta of tables from TDB per hash range [pVnode->config.hashBegin, pVnode->config.hashEnd]
2762
  code = metaTrimTables(pVnode->pMeta, ver);
24,380✔
2763

2764
  return code;
24,380✔
2765
}
2766

2767
static int32_t vnodeProcessAlterConfirmReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
3,634,921✔
2768
  vInfo("vgId:%d, vnode handle msgType:alter-confirm, alter confirm msg is processed", TD_VID(pVnode));
3,634,921✔
2769
  int32_t code = TSDB_CODE_SUCCESS;
3,634,921✔
2770
  if (!pVnode->config.hashChange) {
3,634,921✔
2771
    goto _exit;
3,610,541✔
2772
  }
2773

2774
  code = vnodeConsolidateAlterHashRange(pVnode, ver);
24,380✔
2775
  if (code < 0) {
24,380✔
2776
    vError("vgId:%d, failed to consolidate alter hashrange since %s. version:%" PRId64, TD_VID(pVnode), terrstr(), ver);
×
2777
    goto _exit;
×
2778
  }
2779
  pVnode->config.hashChange = false;
24,380✔
2780

2781
_exit:
3,634,921✔
2782
  pRsp->msgType = TDMT_VND_ALTER_CONFIRM_RSP;
3,634,921✔
2783
  pRsp->code = code;
3,634,921✔
2784
  pRsp->pCont = NULL;
3,634,921✔
2785
  pRsp->contLen = 0;
3,634,921✔
2786

2787
  return code;
3,634,921✔
2788
}
2789

2790
extern int32_t tsdbDisableAndCancelAllBgTask(STsdb *pTsdb);
2791
extern void    tsdbEnableBgTask(STsdb *pTsdb);
2792

2793
static int32_t vnodeProcessAlterConfigReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
317,048✔
2794
  bool walChanged = false;
317,048✔
2795
  bool tsdbChanged = false;
317,048✔
2796

2797
  SAlterVnodeConfigReq req = {0};
317,048✔
2798
  if (tDeserializeSAlterVnodeConfigReq(pReq, len, &req) != 0) {
317,717✔
2799
    terrno = TSDB_CODE_INVALID_MSG;
×
2800
    return TSDB_CODE_INVALID_MSG;
×
2801
  }
2802

2803
  vInfo("vgId:%d, start to alter vnode config, page:%d pageSize:%d buffer:%d szPage:%d szBuf:%" PRIu64
317,717✔
2804
        " cacheLast:%d cacheLastSize:%d days:%d keep0:%d keep1:%d keep2:%d keepTimeOffset:%d ssKeepLocal:%d "
2805
        "ssCompact:%d fsync:%d level:%d "
2806
        "walRetentionPeriod:%d walRetentionSize:%d",
2807
        TD_VID(pVnode), req.pages, req.pageSize, req.buffer, req.pageSize * 1024, (uint64_t)req.buffer * 1024 * 1024,
2808
        req.cacheLast, req.cacheLastSize, req.daysPerFile, req.daysToKeep0, req.daysToKeep1, req.daysToKeep2,
2809
        req.keepTimeOffset, req.ssKeepLocal, req.ssCompact, req.walFsyncPeriod, req.walLevel, req.walRetentionPeriod,
2810
        req.walRetentionSize);
2811

2812
  if (pVnode->config.cacheLastSize != req.cacheLastSize) {
317,717✔
2813
    pVnode->config.cacheLastSize = req.cacheLastSize;
10,830✔
2814
    tsdbCacheSetCapacity(pVnode, (size_t)pVnode->config.cacheLastSize * 1024 * 1024);
10,830✔
2815
  }
2816

2817
  if (pVnode->config.szBuf != req.buffer * 1024LL * 1024LL) {
317,717✔
2818
    vInfo("vgId:%d, vnode buffer is changed from %" PRId64 " to %" PRId64, TD_VID(pVnode), pVnode->config.szBuf,
8,836✔
2819
          (uint64_t)(req.buffer * 1024LL * 1024LL));
2820
    pVnode->config.szBuf = req.buffer * 1024LL * 1024LL;
8,836✔
2821
  }
2822

2823
  if (pVnode->config.szCache != req.pages) {
317,717✔
2824
    if ((terrno = metaAlterCache(pVnode->pMeta, req.pages)) < 0) {
4,446✔
2825
      vError("vgId:%d, failed to change vnode pages from %d to %d failed since %s", TD_VID(pVnode),
×
2826
             pVnode->config.szCache, req.pages, tstrerror(terrno));
2827
      return terrno;
×
2828
    } else {
2829
      vInfo("vgId:%d, vnode pages is changed from %d to %d", TD_VID(pVnode), pVnode->config.szCache, req.pages);
4,446✔
2830
      pVnode->config.szCache = req.pages;
4,446✔
2831
    }
2832
  }
2833

2834
  if (pVnode->config.cacheLast != req.cacheLast) {
317,717✔
2835
    pVnode->config.cacheLast = req.cacheLast;
91,014✔
2836
  }
2837

2838
  if (pVnode->config.walCfg.fsyncPeriod != req.walFsyncPeriod) {
317,717✔
2839
    pVnode->config.walCfg.fsyncPeriod = req.walFsyncPeriod;
51,352✔
2840
    walChanged = true;
51,352✔
2841
  }
2842

2843
  if (pVnode->config.walCfg.level != req.walLevel) {
317,717✔
2844
    if (pVnode->config.walCfg.level == 0) {
37,807✔
2845
      pVnode->config.walCfg.clearFiles = 1;
2,145✔
2846
    }
2847
    pVnode->config.walCfg.level = req.walLevel;
37,807✔
2848
    walChanged = true;
37,807✔
2849
  }
2850

2851
  if (pVnode->config.walCfg.retentionPeriod != req.walRetentionPeriod) {
317,717✔
2852
    pVnode->config.walCfg.retentionPeriod = req.walRetentionPeriod;
47,156✔
2853
    walChanged = true;
47,156✔
2854
  }
2855

2856
  if (pVnode->config.walCfg.retentionSize != req.walRetentionSize) {
317,717✔
2857
    pVnode->config.walCfg.retentionSize = req.walRetentionSize;
1,426✔
2858
    walChanged = true;
1,426✔
2859
  }
2860

2861
  if (pVnode->config.tsdbCfg.keep0 != req.daysToKeep0) {
317,717✔
2862
    pVnode->config.tsdbCfg.keep0 = req.daysToKeep0;
41,030✔
2863
    tsdbChanged = true;
41,030✔
2864
  }
2865

2866
  if (pVnode->config.tsdbCfg.keep1 != req.daysToKeep1) {
317,717✔
2867
    pVnode->config.tsdbCfg.keep1 = req.daysToKeep1;
39,548✔
2868
    tsdbChanged = true;
39,548✔
2869
  }
2870

2871
  if (pVnode->config.tsdbCfg.keep2 != req.daysToKeep2) {
317,717✔
2872
    pVnode->config.tsdbCfg.keep2 = req.daysToKeep2;
33,710✔
2873
    tsdbChanged = true;
33,710✔
2874
  }
2875

2876
  if (pVnode->config.tsdbCfg.keepTimeOffset != req.keepTimeOffset) {
317,717✔
2877
    pVnode->config.tsdbCfg.keepTimeOffset = req.keepTimeOffset;
7,194✔
2878
    tsdbChanged = true;
7,194✔
2879
  }
2880

2881
  if (req.sttTrigger != -1 && req.sttTrigger != pVnode->config.sttTrigger) {
317,717✔
2882
    if (req.sttTrigger > 1 && pVnode->config.sttTrigger > 1) {
2,472✔
2883
      pVnode->config.sttTrigger = req.sttTrigger;
1,426✔
2884
    } else {
2885
      vnodeAWait(&pVnode->commitTask);
1,046✔
2886

2887
      int32_t ret = tsdbDisableAndCancelAllBgTask(pVnode->pTsdb);
1,046✔
2888
      if (ret != 0) {
1,046✔
2889
        vError("vgId:%d, failed to disable bg task since %s", TD_VID(pVnode), tstrerror(ERRNO));
×
2890
      }
2891

2892
      pVnode->config.sttTrigger = req.sttTrigger;
1,046✔
2893
      tsdbEnableBgTask(pVnode->pTsdb);
1,046✔
2894
    }
2895
  }
2896

2897
  if (req.minRows != -1 && req.minRows != pVnode->config.tsdbCfg.minRows) {
317,717✔
2898
    pVnode->config.tsdbCfg.minRows = req.minRows;
1,426✔
2899
  }
2900

2901
  if (req.ssKeepLocal != -1 && req.ssKeepLocal != pVnode->config.ssKeepLocal) {
317,717✔
2902
    pVnode->config.ssKeepLocal = req.ssKeepLocal;
1,426✔
2903
  }
2904
  if (req.ssCompact != -1 && req.ssCompact != pVnode->config.ssCompact) {
317,717✔
2905
    pVnode->config.ssCompact = req.ssCompact;
1,426✔
2906
  }
2907

2908
  if (walChanged) {
317,717✔
2909
    if (walAlter(pVnode->pWal, &pVnode->config.walCfg) != 0) {
134,885✔
2910
      vError("vgId:%d, failed to alter wal config since %s", TD_VID(pVnode), tstrerror(ERRNO));
×
2911
    }
2912
  }
2913

2914
  if (tsdbChanged) {
317,717✔
2915
    tsdbSetKeepCfg(pVnode->pTsdb, &pVnode->config.tsdbCfg);
55,580✔
2916
  }
2917

2918
  return 0;
317,717✔
2919
}
2920

2921
static int32_t vnodeProcessBatchDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
2922
  SBatchDeleteReq deleteReq;
×
2923
  SDecoder        decoder;
×
2924
  tDecoderInit(&decoder, pReq, len);
×
2925
  if (tDecodeSBatchDeleteReq(&decoder, &deleteReq) < 0) {
×
2926
    tDecoderClear(&decoder);
×
2927
    return terrno = TSDB_CODE_INVALID_MSG;
×
2928
  }
2929

2930
  SMetaReader mr = {0};
×
2931
  metaReaderDoInit(&mr, pVnode->pMeta, META_READER_NOLOCK);
×
2932
  STsdb *pTsdb = pVnode->pTsdb;
×
2933

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

2943
    int64_t uid = mr.me.uid;
×
2944

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

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

2968
static int32_t vnodeProcessDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
2,067,405✔
2969
                                     SRpcMsg *pOriginalMsg) {
2970
  int32_t     code = 0;
2,067,405✔
2971
  SDecoder   *pCoder = &(SDecoder){0};
2,067,405✔
2972
  SDeleteRes *pRes = &(SDeleteRes){0};
2,067,405✔
2973

2974
  pRsp->msgType = TDMT_VND_DELETE_RSP;
2,067,405✔
2975
  pRsp->pCont = NULL;
2,067,405✔
2976
  pRsp->contLen = 0;
2,067,405✔
2977
  pRsp->code = TSDB_CODE_SUCCESS;
2,067,405✔
2978

2979
  pRes->uidList = taosArrayInit(0, sizeof(tb_uid_t));
2,067,405✔
2980
  if (pRes->uidList == NULL) {
2,067,405✔
2981
    code = terrno;
×
2982
    goto _err;
×
2983
  }
2984

2985
  tDecoderInit(pCoder, pReq, len);
2,067,405✔
2986
  code = tDecodeDeleteRes(pCoder, pRes);
2,067,405✔
2987
  if (code) goto _err;
2,067,405✔
2988

2989
  if (pRes->affectedRows > 0) {
2,067,405✔
2990
    for (int32_t iUid = 0; iUid < taosArrayGetSize(pRes->uidList); iUid++) {
3,682,684✔
2991
      uint64_t uid = *(uint64_t *)taosArrayGet(pRes->uidList, iUid);
2,101,384✔
2992
      code = tsdbDeleteTableData(pVnode->pTsdb, ver, pRes->suid, uid, pRes->skey, pRes->ekey);
2,101,384✔
2993
      if (code) goto _err;
2,101,384✔
2994
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, pRes->ctimeMs);
2,101,384✔
2995
      if (code) goto _err;
2,101,384✔
2996
    }
2997
  }
2998

2999
  tDecoderClear(pCoder);
2,067,405✔
3000
  taosArrayDestroy(pRes->uidList);
2,067,405✔
3001

3002
  SVDeleteRsp rsp = {.affectedRows = pRes->affectedRows};
2,067,405✔
3003
  int32_t     ret = 0;
2,067,405✔
3004
  tEncodeSize(tEncodeSVDeleteRsp, &rsp, pRsp->contLen, ret);
2,067,405✔
3005
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
2,067,405✔
3006
  SEncoder ec = {0};
2,067,405✔
3007
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
2,067,405✔
3008
  code = tEncodeSVDeleteRsp(&ec, &rsp);
2,067,405✔
3009
  if (code) goto _err;
2,067,405✔
3010
  tEncoderClear(&ec);
2,067,405✔
3011
  return code;
2,067,405✔
3012

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

3029
  return code;
×
3030
}
3031
static int32_t vnodeProcessCreateIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
4,458✔
3032
  SVCreateStbReq req = {0};
4,458✔
3033
  SDecoder       dc = {0};
4,458✔
3034
  int32_t        code = 0;
4,458✔
3035

3036
  pRsp->msgType = TDMT_VND_CREATE_INDEX_RSP;
4,458✔
3037
  pRsp->code = TSDB_CODE_SUCCESS;
4,458✔
3038
  pRsp->pCont = NULL;
4,458✔
3039
  pRsp->contLen = 0;
4,458✔
3040

3041
  tDecoderInit(&dc, pReq, len);
4,458✔
3042
  // decode req
3043
  if (tDecodeSVCreateStbReq(&dc, &req) < 0) {
4,458✔
3044
    tDecoderClear(&dc);
×
3045
    return terrno = TSDB_CODE_INVALID_MSG;
×
3046
  }
3047

3048
  code = metaAddIndexToSuperTable(pVnode->pMeta, ver, &req);
4,458✔
3049
  if (code) {
4,458✔
3050
    pRsp->code = code;
×
3051
    goto _err;
×
3052
  }
3053
  tDecoderClear(&dc);
4,458✔
3054
  return 0;
4,458✔
3055

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

3068
  if ((code = tDeserializeSDropIdxReq(pReq, len, &req))) {
2,952✔
3069
    pRsp->code = code;
×
3070
    return code;
×
3071
  }
3072

3073
  code = metaDropIndexFromSuperTable(pVnode->pMeta, ver, &req);
2,952✔
3074
  if (code) {
2,952✔
3075
    pRsp->code = code;
×
3076
    return code;
×
3077
  }
3078
  return TSDB_CODE_SUCCESS;
2,952✔
3079
}
3080

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

3083
static int32_t vnodeProcessCompactVnodeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
68,145✔
3084
  if (!pVnode->restored) {
68,145✔
3085
    vInfo("vgId:%d, ignore compact req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
4,597✔
3086
    return 0;
4,597✔
3087
  }
3088
  return vnodeAsyncCompact(pVnode, ver, pReq, len, pRsp);
63,548✔
3089
}
3090

3091
static int32_t vnodeProcessConfigChangeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
3092
  if (syncCheckMember(pVnode->sync) != 0) {
×
3093
    vError("vgId:%d, failed to check member", TD_VID(pVnode));
×
3094
  }
3095

3096
  pRsp->msgType = TDMT_SYNC_CONFIG_CHANGE_RSP;
×
3097
  pRsp->code = TSDB_CODE_SUCCESS;
×
3098
  pRsp->pCont = NULL;
×
3099
  pRsp->contLen = 0;
×
3100

3101
  return 0;
×
3102
}
3103

3104
static int32_t vnodeCheckState(SVnode *pVnode) {
50,811✔
3105
  SSyncState syncState = syncGetState(pVnode->sync);
50,811✔
3106
  if (syncState.state != TAOS_SYNC_STATE_LEADER) {
50,811✔
3107
    return terrno = TSDB_CODE_SYN_NOT_LEADER;
17,517✔
3108
  }
3109
  return 0;
33,294✔
3110
}
3111

3112
static int32_t vnodeCheckToken(SVnode *pVnode, char *member0Token, char *member1Token) {
33,294✔
3113
  char token[TSDB_ARB_TOKEN_SIZE] = {0};
33,294✔
3114
  if (vnodeGetArbToken(pVnode, token) != 0) {
33,294✔
3115
    return terrno = TSDB_CODE_NOT_FOUND;
×
3116
  }
3117

3118
  if (strncmp(token, member0Token, TSDB_ARB_TOKEN_SIZE) != 0 &&
33,294✔
3119
      strncmp(token, member1Token, TSDB_ARB_TOKEN_SIZE) != 0) {
32,158✔
3120
    return terrno = TSDB_CODE_MND_ARB_TOKEN_MISMATCH;
31,250✔
3121
  }
3122

3123
  terrno = TSDB_CODE_SUCCESS;
2,044✔
3124
  return 0;
2,044✔
3125
}
3126

3127
static int32_t vnodeCheckSyncd(SVnode *pVnode, char *member0Token, char *member1Token) {
1,022✔
3128
  int32_t code = vnodeCheckToken(pVnode, member0Token, member1Token);
1,022✔
3129
  if (code != 0) {
1,022✔
UNCOV
3130
    return code;
×
3131
  }
3132

3133
  return syncCheckSynced(pVnode->sync);
1,022✔
3134
}
3135

3136
static int32_t vnodeProcessArbCheckSyncReq(SVnode *pVnode, void *pReq, int32_t len, SRpcMsg *pRsp) {
2,026✔
3137
  int32_t code = 0;
2,026✔
3138

3139
  if ((code = vnodeCheckState(pVnode)) != 0) {
2,026✔
3140
    vDebug("vgId:%d, failed to preprocess vnode-arb-check-sync request since %s", TD_VID(pVnode), tstrerror(code));
1,004✔
3141
    return 0;
1,004✔
3142
  }
3143

3144
  SVArbCheckSyncReq syncReq = {0};
1,022✔
3145

3146
  code = tDeserializeSVArbCheckSyncReq(pReq, len, &syncReq);
1,022✔
3147
  if (code) {
1,022✔
3148
    return code;
×
3149
  }
3150

3151
  vInfo("vgId:%d, start to process vnode-arb-check-sync req QID:0x%" PRIx64 ":0x%" PRIx64 ", seqNum:%" PRIx64
1,022✔
3152
        ", arbToken:%s, member0Token:%s, member1Token:%s, "
3153
        "arbTerm:%" PRId64,
3154
        TD_VID(pVnode), pRsp->info.traceId.rootId, pRsp->info.traceId.msgId, pRsp->info.seqNum, syncReq.arbToken,
3155
        syncReq.member0Token, syncReq.member1Token, syncReq.arbTerm);
3156

3157
  pRsp->msgType = TDMT_VND_ARB_CHECK_SYNC_RSP;
1,022✔
3158
  pRsp->code = TSDB_CODE_SUCCESS;
1,022✔
3159
  pRsp->pCont = NULL;
1,022✔
3160
  pRsp->contLen = 0;
1,022✔
3161

3162
  SVArbCheckSyncRsp syncRsp = {0};
1,022✔
3163
  syncRsp.arbToken = syncReq.arbToken;
1,022✔
3164
  syncRsp.member0Token = syncReq.member0Token;
1,022✔
3165
  syncRsp.member1Token = syncReq.member1Token;
1,022✔
3166
  syncRsp.vgId = TD_VID(pVnode);
1,022✔
3167

3168
  if ((syncRsp.errCode = vnodeCheckSyncd(pVnode, syncReq.member0Token, syncReq.member1Token)) != 0) {
1,022✔
UNCOV
3169
    vError("vgId:%d, failed to check assigned log syncd since %s", TD_VID(pVnode), tstrerror(syncRsp.errCode));
×
3170
  }
3171

3172
  if ((code = vnodeUpdateArbTerm(pVnode, syncReq.arbTerm)) != 0) {
1,022✔
3173
    vError("vgId:%d, failed to update arb term since %s", TD_VID(pVnode), tstrerror(code));
×
3174
    goto _OVER;
×
3175
  }
3176

3177
  int32_t contLen = tSerializeSVArbCheckSyncRsp(NULL, 0, &syncRsp);
1,022✔
3178
  if (contLen <= 0) {
1,022✔
3179
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
3180
    code = -1;
×
3181
    goto _OVER;
×
3182
  }
3183
  void *pHead = rpcMallocCont(contLen);
1,022✔
3184
  if (!pHead) {
1,022✔
3185
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
3186
    code = -1;
×
3187
    goto _OVER;
×
3188
  }
3189

3190
  if (tSerializeSVArbCheckSyncRsp(pHead, contLen, &syncRsp) <= 0) {
1,022✔
3191
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
3192
    rpcFreeCont(pHead);
×
3193
    code = -1;
×
3194
    goto _OVER;
×
3195
  }
3196

3197
  pRsp->pCont = pHead;
1,022✔
3198
  pRsp->contLen = contLen;
1,022✔
3199

3200
  vInfo(
1,022✔
3201
      "vgId:%d, suceed to process vnode-arb-check-sync req rsp.code:%s, arbToken:%s, member0Token:%s, "
3202
      "member1Token:%s",
3203
      TD_VID(pVnode), tstrerror(syncRsp.errCode), syncRsp.arbToken, syncRsp.member0Token, syncRsp.member1Token);
3204

3205
  code = TSDB_CODE_SUCCESS;
1,022✔
3206

3207
_OVER:
1,022✔
3208
  if (code != 0) {
1,022✔
3209
    vError("vgId:%d, failed to process arb check req rsp.code:%s since %s", TD_VID(pVnode), tstrerror(syncRsp.errCode),
×
3210
           tstrerror(code));
3211
  }
3212
  tFreeSVArbCheckSyncReq(&syncReq);
1,022✔
3213
  return code;
1,022✔
3214
}
3215

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