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

taosdata / TDengine / #4827

28 Oct 2025 01:21AM UTC coverage: 59.754% (+0.02%) from 59.732%
#4827

push

travis-ci

web-flow
test: tidy up cases on main branch(10-27) (#33381)

121341 of 258518 branches covered (46.94%)

Branch coverage included in aggregate %.

193621 of 268583 relevant lines covered (72.09%)

4339204.91 hits per line

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

54.16
/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) {
99,153✔
85
  int32_t code = 0;
99,153✔
86
  int32_t lino = 0;
99,153✔
87

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

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

99
  // name
100
  char *name = NULL;
99,157✔
101
  if (tDecodeCStr(pCoder, &name) < 0) {
99,099!
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);
99,099✔
108
  if (uid == 0) {
99,207✔
109
    uid = tGenIdPI64();
95,892✔
110
  }
111
  taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), uid);
99,205✔
112

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

116
  tEndDecode(pCoder);
99,205✔
117

118
_exit:
99,182✔
119
  if (code) {
99,182!
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);
99,182✔
123
    if (pUid) *pUid = uid;
99,182✔
124
  }
125
  return code;
99,180✔
126
}
127
static int32_t vnodePreProcessCreateTableMsg(SVnode *pVnode, SRpcMsg *pMsg) {
58,295✔
128
  int32_t code = 0;
58,295✔
129
  int32_t lino = 0;
58,295✔
130

131
  int64_t  btime = taosGetTimestampMs();
58,296✔
132
  SDecoder dc = {0};
58,296✔
133
  int32_t  nReqs;
134

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

141
  if (tDecodeI32v(&dc, &nReqs) < 0) {
58,295!
142
    code = TSDB_CODE_INVALID_MSG;
×
143
    TSDB_CHECK_CODE(code, lino, _exit);
×
144
  }
145
  for (int32_t iReq = 0; iReq < nReqs; iReq++) {
133,526✔
146
    code = vnodePreprocessCreateTableReq(pVnode, &dc, btime, NULL);
75,250✔
147
    TSDB_CHECK_CODE(code, lino, _exit);
75,231!
148
  }
149

150
  tEndDecode(&dc);
58,276✔
151

152
_exit:
58,289✔
153
  tDecoderClear(&dc);
58,289✔
154
  if (code) {
58,295!
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;
58,295✔
159
}
160

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

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

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

178
  code = 0;
19,798✔
179

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

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

195
  SMsgHead *pContOld = pMsg->pCont;
17,657✔
196
  int32_t   reqLenOld = pMsg->contLen - sizeof(SMsgHead);
17,657✔
197

198
  SArray *tbUids = NULL;
17,657✔
199
  int64_t timestampMs = 0;
17,657✔
200

201
  SVDropTtlTableReq ttlReq = {0};
17,657✔
202
  if (tDeserializeSVDropTtlTableReq((char *)pContOld + sizeof(SMsgHead), reqLenOld, &ttlReq) != 0) {
17,657!
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));
17,662✔
209
    if (tbUids == NULL) {
17,680!
210
      code = terrno;
×
211
      TSDB_CHECK_CODE(code, lino, _exit);
×
212
    }
213

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

221
    ttlReq.nUids = taosArrayGetSize(tbUids);
17,638✔
222
    ttlReq.pTbUids = tbUids;
17,640✔
223
  }
224

225
  if (ttlReq.nUids == 0) {
17,640✔
226
    code = TSDB_CODE_MSG_PREPROCESSED;
17,636✔
227
    TSDB_CHECK_CODE(code, lino, _exit);
17,636!
228
  }
229

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

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

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

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

251
  code = 0;
17✔
252

253
_exit:
17,666✔
254
  taosArrayDestroy(tbUids);
17,666✔
255

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

263
  return code;
17,654✔
264
}
265

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

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

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

285
  int64_t uid;
286
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
1,300,614✔
287
    code = vnodePreprocessCreateTableReq(pVnode, pCoder, btimeMs, &uid);
23,905✔
288
    TSDB_CHECK_CODE(code, lino, _exit);
23,949!
289
  }
290

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

297
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
1,300,650✔
298
    taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), uid);
23,942✔
299
    pCoder->pos += sizeof(int64_t);
23,942✔
300
  } else {
301
    if (tDecodeI64(pCoder, &submitTbData.uid) < 0) {
1,276,709!
302
      code = TSDB_CODE_INVALID_MSG;
×
303
      TSDB_CHECK_CODE(code, lino, _exit);
×
304
    }
305
  }
306

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

312
  // scan and check
313
  TSKEY now = btimeMs;
1,300,620✔
314
  if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_MICRO) {
1,300,620✔
315
    now *= 1000;
4,469✔
316
  } else if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_NANO) {
1,296,151✔
317
    now *= 1000000;
2,182✔
318
  }
319

320
  int32_t keep = pVnode->config.tsdbCfg.keep2;
1,300,620✔
321

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

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

338
    if (colData.flag != HAS_VALUE) {
837!
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++) {
68,898✔
344
      if (((TSKEY *)colData.pData)[iRow] < minKey || ((TSKEY *)colData.pData)[iRow] > maxKey) {
68,062!
345
        code = TSDB_CODE_TDB_TIMESTAMP_OUT_OF_RANGE;
1✔
346
        TSDB_CHECK_CODE(code, lino, _exit);
1!
347
      }
348
    }
349

350
    for (uint64_t i = 1; i < nColData; i++) {
4,762✔
351
      code = tDecodeColData(version, pCoder, &colData, true);
3,927✔
352
      if (code) {
3,926!
353
        code = TSDB_CODE_INVALID_MSG;
×
354
        TSDB_CHECK_CODE(code, lino, _exit);
×
355
      }
356
    }
357
  } else {
358
    uint64_t nRow;
359
    if (tDecodeU64v(pCoder, &nRow) < 0) {
1,299,742!
360
      code = TSDB_CODE_INVALID_MSG;
×
361
      TSDB_CHECK_CODE(code, lino, _exit);
93!
362
    }
363

364
    for (int32_t iRow = 0; iRow < nRow; ++iRow) {
104,165,017✔
365
      SRow *pRow = (SRow *)(pCoder->data + pCoder->pos);
102,865,368✔
366
      pCoder->pos += pRow->len;
102,865,368✔
367
#ifndef NO_UNALIGNED_ACCESS
368
      if (pRow->ts < minKey || pRow->ts > maxKey) {
102,865,368!
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;
×
374
        TSDB_CHECK_CODE(code, lino, _exit);
×
375
      }
376

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

385
  if (!tDecodeIsEnd(pCoder)) {
1,300,484✔
386
    taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), ctimeMs);
1,300,481✔
387
    pCoder->pos += sizeof(int64_t);
1,300,481✔
388
  }
389

390
  tEndDecode(pCoder);
1,300,484✔
391

392
_exit:
1,300,338✔
393
  if (code) {
1,300,338✔
394
    vError("vgId:%d, %s:%d failed to vnodePreProcessSubmitTbData submit request since %s", TD_VID(pVnode), __func__,
94!
395
           lino, tstrerror(code));
396
  }
397
  return code;
1,300,338✔
398
}
399
static int32_t vnodePreProcessSubmitMsg(SVnode *pVnode, SRpcMsg *pMsg) {
1,242,890✔
400
  int32_t code = 0;
1,242,890✔
401
  int32_t lino = 0;
1,242,890✔
402

403
  if (tsBypassFlag & TSDB_BYPASS_RA_RPC_RECV_SUBMIT) {
1,242,890✔
404
    return TSDB_CODE_MSG_PREPROCESSED;
1✔
405
  }
406

407
  SDecoder *pCoder = &(SDecoder){0};
1,242,889✔
408

409
  if (taosHton64(((SSubmitReq2Msg *)pMsg->pCont)->version) != 1) {
1,242,889!
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));
1,242,888✔
415

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

421
  uint64_t nSubmitTbData;
422
  if (tDecodeU64v(pCoder, &nSubmitTbData) < 0) {
1,242,890!
423
    code = TSDB_CODE_INVALID_MSG;
×
424
    TSDB_CHECK_CODE(code, lino, _exit);
×
425
  }
426

427
  int64_t btimeMs = taosGetTimestampMs();
1,242,887✔
428
  int64_t ctimeMs = btimeMs;
1,242,887✔
429
  for (int32_t i = 0; i < nSubmitTbData; i++) {
2,543,131✔
430
    code = vnodePreProcessSubmitTbData(pVnode, pCoder, btimeMs, ctimeMs);
1,300,358✔
431
    TSDB_CHECK_CODE(code, lino, _exit);
1,300,338✔
432
  }
433

434
  tEndDecode(pCoder);
1,242,773✔
435

436
_exit:
1,242,883✔
437
  tDecoderClear(pCoder);
1,242,883✔
438
  if (code) {
1,242,890✔
439
    vError("vgId:%d, %s:%d failed to preprocess submit request since %s, msg type:%s", TD_VID(pVnode), __func__, lino,
94!
440
           tstrerror(code), TMSG_INFO(pMsg->msgType));
441
  }
442
  return code;
1,242,890✔
443
}
444

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

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

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

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

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

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

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

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

481
  taosArrayDestroy(res.uidList);
5,567✔
482

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

519
  SVArbCheckSyncReq syncReq = {0};
23✔
520

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

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

531
  int32_t code = terrno;
23✔
532
  tFreeSVArbCheckSyncReq(&syncReq);
23✔
533

534
  return code;
23✔
535
}
536

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

546
  tDecoderInit(&dc, POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), pMsg->contLen - sizeof(SMsgHead));
15,521✔
547

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

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

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

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

620
  switch (pMsg->msgType) {
1,415,038!
621
    case TDMT_VND_CREATE_TABLE: {
58,296✔
622
      code = vnodePreProcessCreateTableMsg(pVnode, pMsg);
58,296✔
623
    } break;
58,295✔
624
    case TDMT_VND_ALTER_TABLE: {
19,798✔
625
      code = vnodePreProcessAlterTableMsg(pVnode, pMsg);
19,798✔
626
    } break;
19,798✔
627
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
17,666✔
628
    case TDMT_VND_DROP_TTL_TABLE: {
629
      code = vnodePreProcessDropTtlMsg(pVnode, pMsg);
17,666✔
630
    } break;
17,627✔
631
    case TDMT_VND_SUBMIT: {
1,242,891✔
632
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.preprocess_time, METRIC_LEVEL_LOW,
1,242,891!
633
                           { code = vnodePreProcessSubmitMsg(pVnode, pMsg); });
634
    } break;
1,242,891✔
635
    case TDMT_VND_DELETE: {
5,567✔
636
      code = vnodePreProcessDeleteMsg(pVnode, pMsg);
5,567✔
637
    } break;
5,567✔
638
    case TDMT_VND_BATCH_DEL: {
×
639
      code = vnodePreProcessBatchDeleteMsg(pVnode, pMsg);
×
640
    } break;
×
641
    case TDMT_VND_ARB_CHECK_SYNC: {
29✔
642
      code = vnodePreProcessArbCheckSyncMsg(pVnode, pMsg);
29✔
643
    } break;
29✔
644
    case TDMT_VND_DROP_TABLE: {
15,521✔
645
      code = vnodePreProcessDropTbMsg(pVnode, pMsg);
15,521✔
646
    } break;
15,521✔
647
#ifdef TD_ENTERPRISE
648
    case TDMT_VND_SSMIGRATE_FILESET: {
×
649
      code = vnodePreProcessSsMigrateFileSetReq(pVnode, pMsg);
×
650
    } break;
×
651
#endif
652
    default:
55,270✔
653
      break;
55,270✔
654
  }
655

656
  if (code && code != TSDB_CODE_MSG_PREPROCESSED) {
1,414,998✔
657
    vError("vgId:%d, failed to preprocess write request since %s, msg type:%s", TD_VID(pVnode), tstrerror(code),
112!
658
           TMSG_INFO(pMsg->msgType));
659
  }
660
  return code;
1,414,999✔
661
}
662

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

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

671
  SBseBatch *pBatch = NULL;
×
672

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

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

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

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

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

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

785
  return code;
×
786
}
787

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

795
  (void)taosThreadMutexLock(&pVnode->mutex);
1,731,990✔
796
  if (pVnode->disableWrite) {
1,732,089!
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);
1,732,089✔
802

803
  if (ver <= pVnode->state.applied) {
1,732,086!
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,
1,732,086!
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)) {
1,732,093!
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)) {
1,732,093!
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);
1,732,093✔
828
  atomic_store_64(&pVnode->state.applyTerm, pMsg->info.conn.applyTerm);
1,732,098✔
829

830
  if (!syncUtilUserCommit(pMsg->msgType)) goto _exit;
1,732,096✔
831

832
  // skip header
833
  pReq = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
1,707,961✔
834
  len = pMsg->contLen - sizeof(SMsgHead);
1,707,961✔
835
  bool needCommit = false;
1,707,961✔
836

837
  switch (pMsg->msgType) {
1,707,961!
838
    /* META */
839
    case TDMT_VND_CREATE_STB:
21,326✔
840
      code = vnodeProcessCreateStbReq(pVnode, ver, pReq, len, pRsp);
21,326✔
841
      TSDB_CHECK_CODE(code, lino, _err);
21,327✔
842
      break;
21,324✔
843
    case TDMT_VND_ALTER_STB:
17,485✔
844
      code = vnodeProcessAlterStbReq(pVnode, ver, pReq, len, pRsp);
17,485✔
845
      TSDB_CHECK_CODE(code, lino, _err);
17,485!
846
      break;
17,485✔
847
    case TDMT_VND_DROP_STB:
1,878✔
848
      code = vnodeProcessDropStbReq(pVnode, ver, pReq, len, pRsp, pMsg);
1,878✔
849
      TSDB_CHECK_CODE(code, lino, _err);
1,878!
850
      break;
1,878✔
851
    case TDMT_VND_CREATE_TABLE:
65,932✔
852
      code = vnodeProcessCreateTbReq(pVnode, ver, pReq, len, pRsp, pMsg);
65,932✔
853
      TSDB_CHECK_CODE(code, lino, _err);
65,930!
854
      break;
65,930✔
855
    case TDMT_VND_ALTER_TABLE:
19,805✔
856
      code = vnodeProcessAlterTbReq(pVnode, ver, pReq, len, pRsp);
19,805✔
857
      TSDB_CHECK_CODE(code, lino, _err);
19,805!
858
      break;
19,805✔
859
    case TDMT_VND_DROP_TABLE:
15,523✔
860
    case TDMT_VND_SNODE_DROP_TABLE:
861
      code = vnodeProcessDropTbReq(pVnode, ver, pReq, len, pRsp, pMsg);
15,523✔
862
      TSDB_CHECK_CODE(code, lino, _err);
15,523!
863
      break;
15,523✔
864
    case TDMT_VND_CREATE_RSMA:
28✔
865
      code = vnodeProcessCreateRsmaReq(pVnode, ver, pReq, len, pRsp);
28✔
866
      TSDB_CHECK_CODE(code, lino, _err);
28!
867
      break;
28✔
868
    case TDMT_VND_DROP_RSMA:
6✔
869
      code = vnodeProcessDropRsmaReq(pVnode, ver, pReq, len, pRsp);
6✔
870
      TSDB_CHECK_CODE(code, lino, _err);
6!
871
      break;
6✔
872
    case TDMT_VND_ALTER_RSMA:
14✔
873
      code = vnodeProcessAlterRsmaReq(pVnode, ver, pReq, len, pRsp);
14✔
874
      TSDB_CHECK_CODE(code, lino, _err);
14!
875
      break;
14✔
876
    case TDMT_VND_DROP_TTL_TABLE:
×
877
      code = vnodeProcessDropTtlTbReq(pVnode, ver, pReq, len, pRsp);
×
878
      TSDB_CHECK_CODE(code, lino, _err);
×
879
      break;
×
880
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
17✔
881
      code = vnodeProcessFetchTtlExpiredTbs(pVnode, ver, pReq, len, pRsp);
17✔
882
      TSDB_CHECK_CODE(code, lino, _err);
17!
883
      break;
17✔
884
    case TDMT_VND_TRIM:
104✔
885
      code = vnodeProcessTrimReq(pVnode, ver, pReq, len, pRsp);
104✔
886
      TSDB_CHECK_CODE(code, lino, _err);
104!
887
      break;
104✔
888
#ifdef TD_ENTERPRISE
889
    case TDMT_VND_SSMIGRATE_FILESET:
×
890
      if (vnodeProcessSsMigrateFileSetReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
891
      break;
×
892
    case TDMT_VND_FOLLOWER_SSMIGRATE:
×
893
      if (vnodeProcessFollowerSsMigrateReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
894
      break;
×
895
    case TDMT_VND_KILL_SSMIGRATE:
×
896
      if (vnodeProcessKillSsMigrateReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
897
      break;
×
898
#endif
899
    /* TSDB */
900
    case TDMT_VND_SUBMIT: {
1,518,540✔
901
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.apply_time, METRIC_LEVEL_LOW, {
1,518,540!
902
        if (vnodeProcessSubmitReq(pVnode, ver, pMsg->pCont, pMsg->contLen, pRsp, pMsg) < 0) goto _err;
903
      });
904
      METRICS_UPDATE(pVnode->writeMetrics.apply_bytes, METRIC_LEVEL_LOW, (int64_t)pMsg->contLen);
1,518,389!
905
      break;
1,518,389✔
906
    }
907
    case TDMT_VND_DELETE:
6,793✔
908
      code = vnodeProcessDeleteReq(pVnode, ver, pReq, len, pRsp, pMsg);
6,793✔
909
      TSDB_CHECK_CODE(code, lino, _err);
6,793!
910
      break;
6,793✔
911
    case TDMT_VND_BATCH_DEL:
×
912
      code = vnodeProcessBatchDeleteReq(pVnode, ver, pReq, len, pRsp);
×
913
      TSDB_CHECK_CODE(code, lino, _err);
×
914
      break;
×
915
      /* TQ */
916
#if defined(USE_TQ) || defined(USE_STREAM)
917
    case TDMT_VND_TMQ_SUBSCRIBE:
2,652✔
918
      code = tqProcessSubscribeReq(pVnode->pTq, ver, pReq, len);
2,652✔
919
      TSDB_CHECK_CODE(code, lino, _err);
2,651!
920
      break;
2,651✔
921
    case TDMT_VND_TMQ_DELETE_SUB:
851✔
922
      code = tqProcessDeleteSubReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen);
851✔
923
      TSDB_CHECK_CODE(code, lino, _err);
849!
924
      break;
849✔
925
    case TDMT_VND_TMQ_COMMIT_OFFSET:
13,445✔
926
      code = tqProcessOffsetCommitReq(pVnode->pTq, ver, pReq, len);
13,445✔
927
      TSDB_CHECK_CODE(code, lino, _err);
13,444!
928
      break;
13,444✔
929
    case TDMT_VND_TMQ_ADD_CHECKINFO:
47✔
930
      code = tqProcessAddCheckInfoReq(pVnode->pTq, ver, pReq, len);
47✔
931
      TSDB_CHECK_CODE(code, lino, _err);
47!
932
      break;
47✔
933
    case TDMT_VND_TMQ_DEL_CHECKINFO:
10✔
934
      code = tqProcessDelCheckInfoReq(pVnode->pTq, ver, pReq, len);
10✔
935
      TSDB_CHECK_CODE(code, lino, _err);
10!
936
      break;
10✔
937
#endif
938
    case TDMT_VND_ALTER_CONFIRM:
8,466✔
939
      needCommit = pVnode->config.hashChange;
8,466✔
940
      code = vnodeProcessAlterConfirmReq(pVnode, ver, pReq, len, pRsp);
8,466✔
941
      TSDB_CHECK_CODE(code, lino, _err);
8,466!
942
      break;
8,466✔
943
    case TDMT_VND_ALTER_CONFIG:
519✔
944
      vnodeProcessAlterConfigReq(pVnode, ver, pReq, len, pRsp);
519✔
945
      break;
519✔
946
    case TDMT_VND_COMMIT:
14,276✔
947
      needCommit = true;
14,276✔
948
      break;
14,276✔
949
    case TDMT_VND_CREATE_INDEX:
19✔
950
      vnodeProcessCreateIndexReq(pVnode, ver, pReq, len, pRsp);
19✔
951
      break;
19✔
952
    case TDMT_VND_DROP_INDEX:
10✔
953
      vnodeProcessDropIndexReq(pVnode, ver, pReq, len, pRsp);
10✔
954
      break;
10✔
955
    case TDMT_VND_COMPACT:
164✔
956
      vnodeProcessCompactVnodeReq(pVnode, ver, pReq, len, pRsp);
164✔
957
      goto _exit;
164✔
958
    case TDMT_VND_SCAN:
5✔
959
      vnodeProcessScanVnodeReq(pVnode, ver, pReq, len, pRsp);
5✔
960
      goto _exit;
5✔
961
    case TDMT_SYNC_CONFIG_CHANGE:
×
962
      vnodeProcessConfigChangeReq(pVnode, ver, pReq, len, pRsp);
×
963
      break;
×
964
#ifdef TD_ENTERPRISE
965
    case TDMT_VND_KILL_COMPACT:
2✔
966
      vnodeProcessKillCompactReq(pVnode, ver, pReq, len, pRsp);
2✔
967
      break;
2✔
968
#endif
969
    case TDMT_VND_KILL_SCAN:
2✔
970
      vnodeProcessKillScanReq(pVnode, ver, pReq, len, pRsp);
2✔
971
      break;
2✔
972
    case TDMT_VND_KILL_TRIM:
×
973
      vnodeProcessKillRetentionReq(pVnode, ver, pReq, len, pRsp);
×
974
      break;
×
975
    /* ARB */
976
    case TDMT_VND_ARB_CHECK_SYNC:
42✔
977
      vnodeProcessArbCheckSyncReq(pVnode, pReq, len, pRsp);
42✔
978
      break;
42✔
979
    default:
×
980
      vError("vgId:%d, unprocessed msg, %d", TD_VID(pVnode), pMsg->msgType);
×
981
      return TSDB_CODE_INVALID_MSG;
×
982
  }
983

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

986
  walApplyVer(pVnode->pWal, ver);
1,707,640✔
987

988
  if (pVnode->pTq) {
1,707,640!
989
    code = tqPushMsg(pVnode->pTq, pMsg->msgType);
1,707,640✔
990
    if (code) {
1,707,636!
991
      vError("vgId:%d, failed to push msg to TQ since %s", TD_VID(pVnode), tstrerror(terrno));
×
992
      return code;
×
993
    }
994
  }
995

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

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

1015
_exit:
1,707,640✔
1016
  return 0;
1,731,939✔
1017

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

1027
  return code;
156✔
1028
}
1029

1030
int32_t vnodePreprocessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg) {
697,426✔
1031
  if (TDMT_SCH_QUERY != pMsg->msgType && TDMT_SCH_MERGE_QUERY != pMsg->msgType) {
697,426✔
1032
    return 0;
145,971✔
1033
  }
1034

1035
  int32_t qType = 0;
551,455✔
1036
  return qWorkerPreprocessQueryMsg(pVnode->pQuery, pMsg, (TDMT_SCH_QUERY == pMsg->msgType), &qType);
551,455✔
1037
}
1038

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

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

1051
  SReadHandle handle = {0};
696,264✔
1052
  handle.vnode = pVnode;
696,264✔
1053
  handle.pMsgCb = &pVnode->msgCb;
696,264✔
1054
  handle.pWorkerCb = pInfo->workerCb;
696,264✔
1055
  initStorageAPI(&handle.api);
696,264✔
1056
  int32_t code = TSDB_CODE_SUCCESS;
696,223✔
1057
  bool    redirected = false;
696,223✔
1058

1059
  switch (pMsg->msgType) {
696,223✔
1060
    case TDMT_SCH_QUERY:
446,898✔
1061
      if (!syncIsReadyForRead(pVnode->sync)) {
446,898✔
1062
        pMsg->code = (terrno) ? terrno : TSDB_CODE_SYN_NOT_LEADER;
59,919!
1063
        redirected = true;
59,919✔
1064
      }
1065
      code = qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
446,924✔
1066
      if (redirected) {
446,933✔
1067
        vnodeRedirectRpcMsg(pVnode, pMsg, pMsg->code);
59,916✔
1068
        return 0;
59,919✔
1069
      }
1070

1071
      return code;
387,017✔
1072
    case TDMT_SCH_MERGE_QUERY:
104,598✔
1073
      return qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
104,598✔
1074
    case TDMT_SCH_QUERY_CONTINUE:
83,405✔
1075
      return qWorkerProcessCQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
83,405✔
1076
    case TDMT_VND_TMQ_CONSUME:
48,064✔
1077
      return tqProcessPollReq(pVnode->pTq, pMsg);
48,064✔
1078
    case TDMT_VND_TMQ_CONSUME_PUSH:
13,251✔
1079
      return tqProcessPollPush(pVnode->pTq);
13,251✔
1080
    default:
7✔
1081
      vError("unknown msg type:%d in query queue", pMsg->msgType);
7!
1082
      return TSDB_CODE_APP_ERROR;
×
1083
  }
1084
}
1085

1086
int32_t vnodeProcessFetchMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
1,531,473✔
1087
  int32_t code = TSDB_CODE_SUCCESS;
1,531,473✔
1088
  vTrace("vgId:%d, msg:%p in fetch queue is processing", pVnode->config.vgId, pMsg);
1,531,473!
1089
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
1,531,473✔
1090
       pMsg->msgType == TDMT_VND_BATCH_META || pMsg->msgType == TDMT_VND_TABLE_NAME ||
1,075,528!
1091
       pMsg->msgType == TDMT_VND_VSUBTABLES_META || pMsg->msgType == TDMT_VND_VSTB_REF_DBS) &&
1,000,572!
1092
      !syncIsReadyForRead(pVnode->sync)) {
530,986✔
1093
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
2,266✔
1094
    return 0;
2,266✔
1095
  }
1096

1097
  switch (pMsg->msgType) {
1,529,374!
1098
    case TDMT_SCH_FETCH:
578,671✔
1099
    case TDMT_SCH_MERGE_FETCH:
1100
      return qWorkerProcessFetchMsg(pVnode, pVnode->pQuery, pMsg, 0);
578,671✔
1101
    case TDMT_SCH_FETCH_RSP:
×
1102
      return qWorkerProcessRspMsg(pVnode, pVnode->pQuery, pMsg, 0);
×
1103
    // case TDMT_SCH_CANCEL_TASK:
1104
    //   return qWorkerProcessCancelMsg(pVnode, pVnode->pQuery, pMsg, 0);
1105
    case TDMT_SCH_DROP_TASK:
551,004✔
1106
      return qWorkerProcessDropMsg(pVnode, pVnode->pQuery, pMsg, 0);
551,004✔
1107
    case TDMT_SCH_TASK_NOTIFY:
26✔
1108
      return qWorkerProcessNotifyMsg(pVnode, pVnode->pQuery, pMsg, 0);
26✔
1109
    case TDMT_SCH_QUERY_HEARTBEAT:
324,367✔
1110
      return qWorkerProcessHbMsg(pVnode, pVnode->pQuery, pMsg, 0);
324,367✔
1111
    case TDMT_VND_TABLE_META:
2,303✔
1112
    case TDMT_VND_TABLE_NAME:
1113
      return vnodeGetTableMeta(pVnode, pMsg, true);
2,303✔
1114
    case TDMT_VND_TABLE_CFG:
×
1115
      return vnodeGetTableCfg(pVnode, pMsg, true);
×
1116
    case TDMT_VND_BATCH_META: {
72,697✔
1117
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.fetch_batch_meta_time, METRIC_LEVEL_LOW,
72,697!
1118
                           { code = vnodeGetBatchMeta(pVnode, pMsg); });
1119
      METRICS_UPDATE(pVnode->writeMetrics.fetch_batch_meta_count, METRIC_LEVEL_LOW, 1);
72,698!
1120
      return code;
72,698✔
1121
    }
1122
    case TDMT_VND_VSUBTABLES_META:
×
1123
      return vnodeGetVSubtablesMeta(pVnode, pMsg);
×
1124
    case TDMT_VND_VSTB_REF_DBS:
×
1125
      return vnodeGetVStbRefDbs(pVnode, pMsg);
×
1126
    case TDMT_VND_QUERY_SCAN_PROGRESS:
10✔
1127
      return vnodeQueryScanProgress(pVnode, pMsg);
10✔
1128
#ifdef TD_ENTERPRISE
1129
    case TDMT_VND_QUERY_COMPACT_PROGRESS:
176✔
1130
      return vnodeQueryCompactProgress(pVnode, pMsg);
176✔
1131

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

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

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

1161
  tstrncpy(pMetaRsp->dbFName, pVnode->config.dbname, TSDB_DB_FNAME_LEN);
123,692✔
1162
  pMetaRsp->dbId = pVnode->config.dbId;
123,692✔
1163
  pMetaRsp->vgId = TD_VID(pVnode);
123,692✔
1164
  pMetaRsp->precision = pVnode->config.tsdbCfg.precision;
123,692✔
1165
}
1166

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

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

1175
  int32_t     code = 0;
73✔
1176
  SVTrimDbReq trimReq = {0};
73✔
1177

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

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

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

1189
_exit:
73✔
1190
  return code;
73✔
1191
}
1192

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1327
  if (!(ttlReq.nUids == taosArrayGetSize(ttlReq.pTbUids))) {
17!
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};
17✔
1335
  metaReaderDoInit(&mr, pVnode->pMeta, 0);
17✔
1336
  rsp.vgId = TD_VID(pVnode);
17✔
1337
  rsp.pExpiredTbs = taosArrayInit(ttlReq.nUids, sizeof(SVDropTbReq));
17✔
1338
  if (!rsp.pExpiredTbs) goto _end;
17!
1339

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

1357
    expiredTb.name = p;
63✔
1358
    if (mr.me.type == TSDB_CHILD_TABLE) {
63✔
1359
      expiredTb.suid = mr.me.ctbEntry.suid;
38✔
1360
    }
1361

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

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

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

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

1394
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
21,326✔
1395
  pRsp->code = TSDB_CODE_SUCCESS;
21,326✔
1396
  pRsp->pCont = NULL;
21,326✔
1397
  pRsp->contLen = 0;
21,326✔
1398

1399
  // decode and process req
1400
  tDecoderInit(&coder, pReq, len);
21,326✔
1401

1402
  code = tDecodeSVCreateStbReq(&coder, &req);
21,327✔
1403
  if (code) {
21,308!
1404
    pRsp->code = code;
×
1405
    goto _err;
×
1406
  }
1407

1408
  code = metaCreateSuperTable(pVnode->pMeta, ver, &req);
21,308✔
1409
  if (code) {
21,327✔
1410
    pRsp->code = code;
3✔
1411
    goto _err;
3✔
1412
  }
1413

1414
  tDecoderClear(&coder);
21,324✔
1415
  return 0;
21,324✔
1416

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

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

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

1447
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(cRsp));
65,920✔
1448
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
65,932✔
1449
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
65,931✔
1450
  if (rsp.pArray == NULL || tbUids == NULL || tbNames == NULL) {
65,931!
1451
    rcode = -1;
×
1452
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1453
    goto _exit;
×
1454
  }
1455

1456
  // loop to create table
1457
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
163,281✔
1458
    pCreateReq = req.pReqs + iReq;
97,349✔
1459
    memset(&cRsp, 0, sizeof(cRsp));
97,349✔
1460

1461
    if (tsEnableAudit && tsEnableAuditCreateTable) {
97,349!
1462
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
97,347!
1463
      if (str == NULL) {
97,340!
1464
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1465
        rcode = -1;
×
1466
        goto _exit;
×
1467
      }
1468
      tstrncpy(str, pCreateReq->name, TSDB_TABLE_FNAME_LEN);
97,340✔
1469
      if (taosArrayPush(tbNames, &str) == NULL) {
97,343!
1470
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1471
        rcode = -1;
×
1472
        goto _exit;
×
1473
      }
1474
    }
1475

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

1489
    // do create table
1490
    if (metaCreateTable2(pVnode->pMeta, ver, pCreateReq, &cRsp.pMeta) < 0) {
97,343✔
1491
      if (pCreateReq->flags & TD_CREATE_IF_NOT_EXISTS && terrno == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
124!
1492
        cRsp.code = TSDB_CODE_SUCCESS;
108✔
1493
      } else {
1494
        cRsp.code = terrno;
16✔
1495
      }
1496
    } else {
1497
      cRsp.code = TSDB_CODE_SUCCESS;
97,226✔
1498
      if (taosArrayPush(tbUids, &pCreateReq->uid) == NULL) {
194,451!
1499
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1500
        rcode = -1;
×
1501
        goto _exit;
×
1502
      }
1503
      vnodeUpdateMetaRsp(pVnode, cRsp.pMeta);
97,225✔
1504
    }
1505

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

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

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

1532
  if (tsEnableAudit && tsEnableAuditCreateTable) {
65,929!
1533
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
65,929✔
1534

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

1540
    SStringBuilder sb = {0};
65,929✔
1541
    for (int32_t i = 0; i < tbNames->size; i++) {
163,274✔
1542
      char **key = (char **)taosArrayGet(tbNames, i);
97,345✔
1543
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
97,344✔
1544
      if (i < tbNames->size - 1) {
97,346✔
1545
        taosStringBuilderAppendChar(&sb, ',');
31,418✔
1546
      }
1547
      // taosMemoryFreeClear(*key);
1548
    }
1549

1550
    size_t len = 0;
65,929✔
1551
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
65,929✔
1552

1553
    if (pOriginRpc->info.conn.user != NULL && strlen(pOriginRpc->info.conn.user) > 0) {
65,930!
1554
      auditAddRecord(pOriginRpc, clusterId, "createTable", name.dbname, "", keyJoined, len);
58,293✔
1555
    }
1556

1557
    taosStringBuilderDestroy(&sb);
65,929✔
1558
  }
1559

1560
_exit:
×
1561
  tDeleteSVCreateTbBatchReq(&req);
65,929✔
1562
  taosArrayDestroyEx(rsp.pArray, tFreeSVCreateTbRsp);
65,932✔
1563
  taosArrayDestroy(tbUids);
65,932✔
1564
  tDecoderClear(&decoder);
65,930✔
1565
  tEncoderClear(&encoder);
65,932✔
1566
  taosArrayDestroyP(tbNames, NULL);
65,931✔
1567
  return rcode;
65,932✔
1568
}
1569

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

1575
  pRsp->msgType = TDMT_VND_ALTER_STB_RSP;
17,485✔
1576
  pRsp->code = TSDB_CODE_SUCCESS;
17,485✔
1577
  pRsp->pCont = NULL;
17,485✔
1578
  pRsp->contLen = 0;
17,485✔
1579

1580
  tDecoderInit(&dc, pReq, len);
17,485✔
1581

1582
  // decode req
1583
  code = tDecodeSVCreateStbReq(&dc, &req);
17,485✔
1584
  if (code) {
17,485!
1585
    tDecoderClear(&dc);
×
1586
    return code;
×
1587
  }
1588

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

1596
  tDecoderClear(&dc);
17,485✔
1597

1598
  return 0;
17,485✔
1599
}
1600

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

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

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

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

1620
  vInfo("vgId:%d, start to process vnode-drop-stb, QID:0x%" PRIx64 ":0x%" PRIx64 ", drop stb:%s", TD_VID(pVnode), trace ? trace->rootId : 0, 
1,878!
1621
              trace ? trace->msgId : 0, req.name);
1622

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

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

1636
  // return rsp
1637
_exit:
1,876✔
1638
  vInfo("vgId:%d, finished to process vnode-drop-stb, QID:0x%" PRIx64 ":0x%" PRIx64 ", drop stb:%s", TD_VID(pVnode), trace ? trace->rootId : 0, 
1,878!
1639
              trace ? trace->msgId : 0, req.name);
1640
  if (tbUidList) taosArrayDestroy(tbUidList);
1,878!
1641
  pRsp->code = rcode;
1,878✔
1642
  tDecoderClear(&decoder);
1,878✔
1643
  return 0;
1,878✔
1644
}
1645

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

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

1661
  tDecoderInit(&dc, pReq, len);
19,805✔
1662

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

1670
  // process
1671
  if (metaAlterTable(pVnode->pMeta, ver, &vAlterTbReq, &vMetaRsp) < 0) {
19,805✔
1672
    vAlterTbRsp.code = terrno;
922✔
1673
    tDecoderClear(&dc);
922✔
1674
    goto _exit;
922✔
1675
  }
1676
  tDecoderClear(&dc);
18,883✔
1677

1678
  if (NULL != vMetaRsp.pSchemas) {
18,883✔
1679
    vnodeUpdateMetaRsp(pVnode, &vMetaRsp);
5,631✔
1680
    vAlterTbRsp.pMeta = &vMetaRsp;
5,631✔
1681
  }
1682

1683
  if (vAlterTbReq.action == TSDB_ALTER_TABLE_UPDATE_TAG_VAL ||
18,883✔
1684
      vAlterTbReq.action == TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL) {
5,693✔
1685
    int64_t uid = metaGetTableEntryUidByName(pVnode->pMeta, vAlterTbReq.tbName);
13,195✔
1686
    if (uid == 0) {
13,195!
1687
      vError("vgId:%d, %s failed at %s:%d since table %s not found", TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
1688
             vAlterTbReq.tbName);
1689
      goto _exit;
×
1690
    }
1691

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

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

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

1706
    taosArrayDestroy(tbUids);
13,195✔
1707
  }
1708

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

1718
  tEncoderClear(&ec);
19,805✔
1719
  if (vMetaRsp.pSchemas) {
19,805✔
1720
    taosMemoryFree(vMetaRsp.pSchemas);
5,631!
1721
    taosMemoryFree(vMetaRsp.pSchemaExt);
5,631!
1722
  }
1723
  if (vMetaRsp.pColRefs) {
19,805✔
1724
    taosMemoryFree(vMetaRsp.pColRefs);
432!
1725
  }
1726
  return 0;
19,805✔
1727
}
1728

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

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

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

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

1759
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
31,449✔
1760
    SVDropTbReq *pDropTbReq = req.pReqs + iReq;
15,926✔
1761
    SVDropTbRsp  dropTbRsp = {0};
15,926✔
1762
    tb_uid_t     tbUid = 0;
15,926✔
1763

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

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

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

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

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

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

1809
    SStringBuilder sb = {0};
15,523✔
1810
    for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
31,449✔
1811
      char **key = (char **)taosArrayGet(tbNames, iReq);
15,926✔
1812
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
15,926✔
1813
      if (iReq < req.nReqs - 1) {
15,926✔
1814
        taosStringBuilderAppendChar(&sb, ',');
403✔
1815
      }
1816
      taosMemoryFreeClear(*key);
15,926!
1817
    }
1818

1819
    size_t len = 0;
15,523✔
1820
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
15,523✔
1821

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

1826
    taosStringBuilderDestroy(&sb);
15,523✔
1827
  }
1828

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

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

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

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

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

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

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

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

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

1913
  taosMemoryFreeClear(pSchema);
1914

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

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

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

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

1967
  return TSDB_CODE_SUCCESS;
×
1968
}
1969

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

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

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

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

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

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

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

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

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

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

2039
  return code;
×
2040
}
2041

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

2048
  SSubmitReqConvertCxt cxt = {0};
×
2049

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

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

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

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

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

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

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

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

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

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

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

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

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

2217
static int32_t buildExistTableInStreamRsp(SVnode *pVnode, SSubmitTbData *pSubmitTbData, STableMetaRsp **ppRsp) {
2,046✔
2218
  if (pSubmitTbData->pCreateTbReq->flags & TD_CREATE_NORMAL_TB_IN_STREAM) {
2,046✔
2219
    int32_t code = buildExistNormalTalbeRsp(pVnode, pSubmitTbData->uid, ppRsp);
144✔
2220
    if (code) {
144!
2221
      vError("vgId:%d, table uid:%" PRId64 " not exists, line:%d", TD_VID(pVnode), pSubmitTbData->uid, __LINE__);
×
2222
      return code;
×
2223
    }
2224
    return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
144✔
2225
  } else if (pSubmitTbData->pCreateTbReq->flags & TD_CREATE_SUB_TB_IN_STREAM) {
1,902✔
2226
    return buildExistSubTalbeRsp(pVnode, pSubmitTbData, ppRsp);
261✔
2227
  }
2228
  return TSDB_CODE_SUCCESS;
1,641✔
2229
}
2230

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

2240
  for (int32_t i = 0; i < numTbData; ++i) {
3,094,715✔
2241
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
1,576,323✔
2242

2243
    if (pTbData->pCreateTbReq == NULL) {
1,576,323✔
2244
      continue;
1,552,280✔
2245
    }
2246

2247
    pTbData->uid = pTbData->pCreateTbReq->uid;
24,043✔
2248

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

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

2264
    SVCreateTbRsp *pCreateTbRsp = taosArrayReserve(pResponse->aCreateTbRsp, 1);
24,045✔
2265
    code = metaCreateTable2(pVnode->pMeta, version, pTbData->pCreateTbReq, &pCreateTbRsp->pMeta);
24,042✔
2266
    if (code == TSDB_CODE_SUCCESS) {
24,043✔
2267
      // Allocate necessary resources
2268
      if (newTbUids == NULL) {
20,836✔
2269
        newTbUids = taosArrayInit(numTbData, sizeof(int64_t));
15,392✔
2270
        if (newTbUids == NULL) {
15,392!
2271
          code = terrno;
×
2272
          vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2273
                 tstrerror(code), version);
2274
          return code;
×
2275
        }
2276
      }
2277

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

2286
      if (pCreateTbRsp->pMeta) {
20,836!
2287
        vnodeUpdateMetaRsp(pVnode, pCreateTbRsp->pMeta);
20,836✔
2288
      }
2289
    } else if (code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
3,207✔
2290
      code = terrno = 0;
3,205✔
2291
      pTbData->uid = pTbData->pCreateTbReq->uid;  // update uid if table exist for using below
3,205✔
2292

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

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

2323
  vDebug("vgId:%d, handle auto create table done, version:%" PRId64, TD_VID(pVnode), version);
1,518,393✔
2324

2325
  taosArrayDestroy(newTbUids);
1,518,394✔
2326
  return code;
1,518,394✔
2327
}
2328

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

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

2367
static int32_t vnodeHandleDataWrite(SVnode *pVnode, int64_t version, SSubmitReq2 *pRequest, SSubmitRsp2 *pResponse) {
1,518,393✔
2368
  int32_t code = TSDB_CODE_SUCCESS;
1,518,393✔
2369
  int32_t numTbData = taosArrayGetSize(pRequest->aSubmitTbData);
1,518,393✔
2370
  int8_t  hasBlob = 0;
1,518,394✔
2371

2372
  // Scan submit data
2373
  for (int32_t i = 0; i < numTbData; ++i) {
3,094,566✔
2374
    SMetaInfo      info = {0};
1,576,176✔
2375
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
1,576,176✔
2376

2377
    if (pTbData->flags & SUBMIT_REQ_WITH_BLOB) {
1,576,175!
2378
      hasBlob = 1;
×
2379
    }
2380
    if (pTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
1,576,175✔
2381
      continue;  // skip column data format
836✔
2382
    }
2383

2384
    code = metaGetInfo(pVnode->pMeta, pTbData->uid, &info, NULL);
1,575,339✔
2385
    if (code) {
1,575,341✔
2386
      code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
3✔
2387
      vWarn("vgId:%d, error occurred at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __FILE__,
3!
2388
            __LINE__, tstrerror(code), version, pTbData->uid);
2389
      return code;
5✔
2390
    }
2391

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

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

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

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

2426
  for (int32_t i = 0; i < numTbData; ++i) {
3,094,553✔
2427
    int32_t        affectedRows = 0;
1,576,164✔
2428
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
1,576,164✔
2429

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

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

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

2455
  vDebug("vgId:%d, handle data write done, version:%" PRId64 ", affectedRows:%d", TD_VID(pVnode), version,
1,518,389✔
2456
         pResponse->affectedRows);
2457
  return code;
1,518,388✔
2458
}
2459

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

2463
  int32_t   numCols = taosArrayGetSize(pTbData->aCol);
836✔
2464
  SColData *aColData = (SColData *)TARRAY_DATA(pTbData->aCol);
836✔
2465

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

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

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

2494
  SRowKey *pLastKey = NULL;
836✔
2495
  SRowKey  lastKey = {0};
836✔
2496
  for (int32_t i = 0; i < aColData[0].nVal; ++i) {
68,410✔
2497
    SRowKey key = {0};
67,584✔
2498

2499
    tColDataArrGetRowKey(aColData, numCols, i, &key);
67,584✔
2500

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

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

2521
    *pLastKey = key;
67,574✔
2522
  }
2523

2524
  return code;
826✔
2525
}
2526

2527
static int32_t vnodeScanSubmitRowData(SVnode *pVnode, SSubmitTbData *pTbData, TSKEY minKey, TSKEY maxKey) {
1,575,472✔
2528
  int32_t code = 0;
1,575,472✔
2529

2530
  int32_t numRows = taosArrayGetSize(pTbData->aRowP);
1,575,472✔
2531
  SRow  **aRow = (SRow **)TARRAY_DATA(pTbData->aRowP);
1,575,472✔
2532

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

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

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

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

2574
    *pLastKey = key;
150,309,291✔
2575
  }
2576

2577
  return code;
1,588,245✔
2578
}
2579

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

2584
  TSKEY now = taosGetTimestamp(pVnode->config.tsdbCfg.precision);
1,518,531✔
2585
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * pVnode->config.tsdbCfg.keep2;
1,518,528✔
2586
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
1,518,528✔
2587
  for (int32_t i = 0; i < numTbData; i++) {
3,094,851✔
2588
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
1,576,312✔
2589

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

2597
    if (pTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
1,576,309✔
2598
      code = vnodeScanColumnData(pVnode, pTbData, minKey, maxKey);
836✔
2599
      if (code) {
836!
2600
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2601
               __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2602
        return code;
×
2603
      }
2604
    } else {
2605
      code = vnodeScanSubmitRowData(pVnode, pTbData, minKey, maxKey);
1,575,473✔
2606
      if (code) {
1,575,487!
2607
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2608
               __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2609
        return code;
×
2610
      }
2611
    }
2612
  }
2613

2614
  return code;
1,518,539✔
2615
}
2616

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

2623
  SSubmitReq2 *pSubmitReq = &(SSubmitReq2){0};
1,518,539✔
2624
  SSubmitRsp2 *pSubmitRsp = &(SSubmitRsp2){0};
1,518,539✔
2625
  int32_t      ret;
2626
  SEncoder     ec = {0};
1,518,539✔
2627

2628
  pRsp->code = TSDB_CODE_SUCCESS;
1,518,539✔
2629

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

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

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

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

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

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

2681
_exit:
1,518,388✔
2682
  // message
2683
  pRsp->code = code;
1,518,541✔
2684
  tEncodeSize(tEncodeSSubmitRsp2, pSubmitRsp, pRsp->contLen, ret);
1,518,541!
2685
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
1,518,541✔
2686
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
1,518,542✔
2687
  if (tEncodeSSubmitRsp2(&ec, pSubmitRsp) < 0) {
1,518,542!
2688
    vError("vgId:%d, failed to encode submit response", TD_VID(pVnode));
×
2689
  }
2690
  tEncoderClear(&ec);
1,518,541✔
2691

2692
  // update statistics
2693
  (void)atomic_add_fetch_64(&pVnode->statis.nInsert, pSubmitRsp->affectedRows);
1,518,541✔
2694
  (void)atomic_add_fetch_64(&pVnode->statis.nInsertSuccess, pSubmitRsp->affectedRows);
1,518,542✔
2695
  (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsert, 1);
1,518,542✔
2696

2697
  // update metrics
2698
  METRICS_UPDATE(pVnode->writeMetrics.total_requests, METRIC_LEVEL_LOW, 1);
1,518,541!
2699
  METRICS_UPDATE(pVnode->writeMetrics.total_rows, METRIC_LEVEL_HIGH, pSubmitRsp->affectedRows);
1,518,541!
2700
  METRICS_UPDATE(pVnode->writeMetrics.total_bytes, METRIC_LEVEL_LOW, pMsg->header.contLen);
1,518,541!
2701

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

2714
  if (code == 0) {
1,518,541✔
2715
    (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
1,518,389✔
2716
  }
2717

2718
  // clear
2719
  tDestroySubmitReq(pSubmitReq, 0 == taosHton64(pMsg->version) ? TSDB_MSG_FLG_CMPT : TSDB_MSG_FLG_DECODE);
1,518,541!
2720
  tDestroySSubmitRsp2(pSubmitRsp, TSDB_MSG_FLG_ENCODE);
1,518,542✔
2721

2722
  if (code) {
1,518,540✔
2723
    terrno = code;
153✔
2724
    if (code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
153✔
2725
      vInfo("vgId:%d, failed to process submit request since %s, lino:%d, version:%" PRId64, TD_VID(pVnode),
146!
2726
            tstrerror(code), lino, ver);
2727

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

2734
  taosMemoryFree(pAllocMsg);
1,518,541!
2735
  tDecoderClear(&dc);
1,518,540✔
2736

2737
  return code;
1,518,542✔
2738
}
2739

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

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

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

2749
  return code;
64✔
2750
}
2751

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

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

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

2772
  return code;
8,466✔
2773
}
2774

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

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

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

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

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

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

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

2819
  if (pVnode->config.cacheLast != req.cacheLast) {
519✔
2820
    pVnode->config.cacheLast = req.cacheLast;
137✔
2821
  }
2822

2823
  if (pVnode->config.walCfg.fsyncPeriod != req.walFsyncPeriod) {
519✔
2824
    pVnode->config.walCfg.fsyncPeriod = req.walFsyncPeriod;
72✔
2825
    walChanged = true;
72✔
2826
  }
2827

2828
  if (pVnode->config.walCfg.level != req.walLevel) {
519✔
2829
    if (pVnode->config.walCfg.level == 0) {
50!
2830
      pVnode->config.walCfg.clearFiles = 1;
×
2831
    }
2832
    pVnode->config.walCfg.level = req.walLevel;
50✔
2833
    walChanged = true;
50✔
2834
  }
2835

2836
  if (pVnode->config.walCfg.retentionPeriod != req.walRetentionPeriod) {
519✔
2837
    pVnode->config.walCfg.retentionPeriod = req.walRetentionPeriod;
132✔
2838
    walChanged = true;
132✔
2839
  }
2840

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

2846
  if (pVnode->config.tsdbCfg.keep0 != req.daysToKeep0) {
519✔
2847
    pVnode->config.tsdbCfg.keep0 = req.daysToKeep0;
56✔
2848
    tsdbChanged = true;
56✔
2849
  }
2850

2851
  if (pVnode->config.tsdbCfg.keep1 != req.daysToKeep1) {
519✔
2852
    pVnode->config.tsdbCfg.keep1 = req.daysToKeep1;
56✔
2853
    tsdbChanged = true;
56✔
2854
  }
2855

2856
  if (pVnode->config.tsdbCfg.keep2 != req.daysToKeep2) {
519✔
2857
    pVnode->config.tsdbCfg.keep2 = req.daysToKeep2;
48✔
2858
    tsdbChanged = true;
48✔
2859
  }
2860

2861
  if (pVnode->config.tsdbCfg.keepTimeOffset != req.keepTimeOffset) {
519✔
2862
    pVnode->config.tsdbCfg.keepTimeOffset = req.keepTimeOffset;
10✔
2863
    tsdbChanged = true;
10✔
2864
  }
2865

2866
  if (req.sttTrigger != -1 && req.sttTrigger != pVnode->config.sttTrigger) {
519!
2867
    if (req.sttTrigger > 1 && pVnode->config.sttTrigger > 1) {
4!
2868
      pVnode->config.sttTrigger = req.sttTrigger;
2✔
2869
    } else {
2870
      vnodeAWait(&pVnode->commitTask);
2✔
2871

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

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

2882
  if (req.minRows != -1 && req.minRows != pVnode->config.tsdbCfg.minRows) {
519!
2883
    pVnode->config.tsdbCfg.minRows = req.minRows;
2✔
2884
  }
2885

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

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

2899
  if (tsdbChanged) {
519✔
2900
    tsdbSetKeepCfg(pVnode->pTsdb, &pVnode->config.tsdbCfg);
76✔
2901
  }
2902

2903
  return 0;
519✔
2904
}
2905

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

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

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

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

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

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

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

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

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

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

2974
  if (pRes->affectedRows > 0) {
6,793✔
2975
    for (int32_t iUid = 0; iUid < taosArrayGetSize(pRes->uidList); iUid++) {
12,850✔
2976
      uint64_t uid = *(uint64_t *)taosArrayGet(pRes->uidList, iUid);
6,928✔
2977
      code = tsdbDeleteTableData(pVnode->pTsdb, ver, pRes->suid, uid, pRes->skey, pRes->ekey);
6,928✔
2978
      if (code) goto _err;
6,928!
2979
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, pRes->ctimeMs);
6,928✔
2980
      if (code) goto _err;
6,928!
2981
    }
2982
  }
2983

2984
  tDecoderClear(pCoder);
6,793✔
2985
  taosArrayDestroy(pRes->uidList);
6,793✔
2986

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

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

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

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

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

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

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

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

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

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

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

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

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

3086
  return 0;
×
3087
}
3088

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

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

3103
  if (strncmp(token, member0Token, TSDB_ARB_TOKEN_SIZE) != 0 &&
40✔
3104
      strncmp(token, member1Token, TSDB_ARB_TOKEN_SIZE) != 0) {
38✔
3105
    return terrno = TSDB_CODE_MND_ARB_TOKEN_MISMATCH;
30✔
3106
  }
3107

3108
  terrno = TSDB_CODE_SUCCESS;
10✔
3109
  return 0;
10✔
3110
}
3111

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

3118
  return syncCheckSynced(pVnode->sync);
5✔
3119
}
3120

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

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

3129
  SVArbCheckSyncReq syncReq = {0};
17✔
3130

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

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

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

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

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

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

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

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

3182
  pRsp->pCont = pHead;
17✔
3183
  pRsp->contLen = contLen;
17✔
3184

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

3190
  code = TSDB_CODE_SUCCESS;
17✔
3191

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

3201
#ifndef TD_ENTERPRISE
3202
int32_t vnodeAsyncCompact(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) { return 0; }
3203
int32_t tsdbAsyncCompact(STsdb *tsdb, const STimeWindow *tw, ETsdbOpType type) { return 0; }
3204
#endif
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc