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

taosdata / TDengine / #4687

25 Aug 2025 07:22AM UTC coverage: 57.894% (-2.2%) from 60.092%
#4687

push

travis-ci

web-flow
fix: add taosBenchmark windows support params (#32708)

132643 of 292257 branches covered (45.39%)

Branch coverage included in aggregate %.

201266 of 284501 relevant lines covered (70.74%)

4743408.21 hits per line

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

52.41
/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
static int32_t vnodeProcessCreateStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
35
static int32_t vnodeProcessAlterStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
36
static int32_t vnodeProcessDropStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
37
static int32_t vnodeProcessCreateTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
38
                                       SRpcMsg *pOriginRpc);
39
static int32_t vnodeProcessAlterTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
40
static int32_t vnodeProcessDropTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
41
                                     SRpcMsg *pOriginRpc);
42
static int32_t vnodeProcessSubmitReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
43
                                     SRpcMsg *pOriginalMsg);
44
static int32_t vnodeProcessAlterConfirmReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
45
static int32_t vnodeProcessAlterConfigReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
46
static int32_t vnodeProcessDropTtlTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
47
static int32_t vnodeProcessTrimReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
48
static int32_t vnodeProcessSsMigrateReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
49
static int32_t vnodeProcessFollowerSsMigrateReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
50
static int32_t vnodeProcessDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
51
                                     SRpcMsg *pOriginalMsg);
52
static int32_t vnodeProcessBatchDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
53
static int32_t vnodeProcessCreateIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
54
static int32_t vnodeProcessDropIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
55
static int32_t vnodeProcessCompactVnodeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
56
static int32_t vnodeProcessConfigChangeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
57
static int32_t vnodeProcessArbCheckSyncReq(SVnode *pVnode, void *pReq, int32_t len, SRpcMsg *pRsp);
58
static int32_t vnodeProcessDropTSmaCtbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
59
                                          SRpcMsg *pOriginRpc);
60

61
static int32_t vnodeCheckState(SVnode *pVnode);
62
static int32_t vnodeCheckToken(SVnode *pVnode, char *member0Token, char *member1Token);
63
static int32_t vnodeCheckSyncd(SVnode *pVnode, char *member0Token, char *member1Token);
64
static int32_t vnodeProcessFetchTtlExpiredTbs(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
65

66
extern int32_t vnodeProcessKillCompactReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
67
extern int32_t vnodeQueryCompactProgress(SVnode *pVnode, SRpcMsg *pMsg);
68
extern int32_t vnodeQuerySsMigrateProgress(SVnode *pVnode, SRpcMsg *pMsg);
69

70
static int32_t vnodePreprocessCreateTableReq(SVnode *pVnode, SDecoder *pCoder, int64_t btime, int64_t *pUid) {
133,154✔
71
  int32_t code = 0;
133,154✔
72
  int32_t lino = 0;
133,154✔
73

74
  if (tStartDecode(pCoder) < 0) {
133,154!
75
    code = TSDB_CODE_INVALID_MSG;
×
76
    TSDB_CHECK_CODE(code, lino, _exit);
×
77
  }
78

79
  // flags
80
  if (tDecodeI32v(pCoder, NULL) < 0) {
133,135!
81
    code = TSDB_CODE_INVALID_MSG;
×
82
    TSDB_CHECK_CODE(code, lino, _exit);
×
83
  }
84

85
  // name
86
  char *name = NULL;
133,135✔
87
  if (tDecodeCStr(pCoder, &name) < 0) {
133,043!
88
    code = TSDB_CODE_INVALID_MSG;
×
89
    TSDB_CHECK_CODE(code, lino, _exit);
×
90
  }
91

92
  // uid
93
  int64_t uid = metaGetTableEntryUidByName(pVnode->pMeta, name);
133,043✔
94
  if (uid == 0) {
133,282✔
95
    uid = tGenIdPI64();
129,617✔
96
  }
97
  taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), uid);
133,269✔
98

99
  // btime
100
  taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos + 8), btime);
133,269✔
101

102
  tEndDecode(pCoder);
133,269✔
103

104
_exit:
133,258✔
105
  if (code) {
133,258!
106
    vError("vgId:%d %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
107
  } else {
108
    vTrace("vgId:%d %s done, table:%s uid generated:%" PRId64, TD_VID(pVnode), __func__, name, uid);
133,258✔
109
    if (pUid) *pUid = uid;
133,258✔
110
  }
111
  return code;
133,260✔
112
}
113
static int32_t vnodePreProcessCreateTableMsg(SVnode *pVnode, SRpcMsg *pMsg) {
68,803✔
114
  int32_t code = 0;
68,803✔
115
  int32_t lino = 0;
68,803✔
116

117
  int64_t  btime = taosGetTimestampMs();
68,803✔
118
  SDecoder dc = {0};
68,803✔
119
  int32_t  nReqs;
120

121
  tDecoderInit(&dc, (uint8_t *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead));
68,803✔
122
  if (tStartDecode(&dc) < 0) {
68,803!
123
    code = TSDB_CODE_INVALID_MSG;
×
124
    return code;
×
125
  }
126

127
  if (tDecodeI32v(&dc, &nReqs) < 0) {
68,803!
128
    code = TSDB_CODE_INVALID_MSG;
×
129
    TSDB_CHECK_CODE(code, lino, _exit);
×
130
  }
131
  for (int32_t iReq = 0; iReq < nReqs; iReq++) {
154,148✔
132
    code = vnodePreprocessCreateTableReq(pVnode, &dc, btime, NULL);
85,363✔
133
    TSDB_CHECK_CODE(code, lino, _exit);
85,345!
134
  }
135

136
  tEndDecode(&dc);
68,785✔
137

138
_exit:
68,791✔
139
  tDecoderClear(&dc);
68,791✔
140
  if (code) {
68,800!
141
    vError("vgId:%d, %s:%d failed to preprocess submit request since %s, msg type:%s", TD_VID(pVnode), __func__, lino,
×
142
           tstrerror(code), TMSG_INFO(pMsg->msgType));
143
  }
144
  return code;
68,800✔
145
}
146

147
static int32_t vnodePreProcessAlterTableMsg(SVnode *pVnode, SRpcMsg *pMsg) {
19,674✔
148
  int32_t code = TSDB_CODE_INVALID_MSG;
19,674✔
149
  int32_t lino = 0;
19,674✔
150

151
  SDecoder dc = {0};
19,674✔
152
  tDecoderInit(&dc, (uint8_t *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead));
19,674✔
153

154
  SVAlterTbReq vAlterTbReq = {0};
19,674✔
155
  int64_t      ctimeMs = taosGetTimestampMs();
19,674✔
156
  if (tDecodeSVAlterTbReqSetCtime(&dc, &vAlterTbReq, ctimeMs) < 0) {
19,674!
157
    taosArrayDestroy(vAlterTbReq.pMultiTag);
×
158
    vAlterTbReq.pMultiTag = NULL;
×
159
    goto _exit;
×
160
  }
161
  taosArrayDestroy(vAlterTbReq.pMultiTag);
19,674✔
162
  vAlterTbReq.pMultiTag = NULL;
19,674✔
163

164
  code = 0;
19,674✔
165

166
_exit:
19,674✔
167
  tDecoderClear(&dc);
19,674✔
168
  if (code) {
19,674!
169
    vError("vgId:%d %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
170
  } else {
171
    vTrace("vgId:%d %s done, table:%s ctimeMs generated:%" PRId64, TD_VID(pVnode), __func__, vAlterTbReq.tbName,
19,674!
172
           ctimeMs);
173
  }
174
  return code;
19,674✔
175
}
176

177
static int32_t vnodePreProcessDropTtlMsg(SVnode *pVnode, SRpcMsg *pMsg) {
18,126✔
178
  int32_t code = TSDB_CODE_INVALID_MSG;
18,126✔
179
  int32_t lino = 0;
18,126✔
180

181
  SMsgHead *pContOld = pMsg->pCont;
18,126✔
182
  int32_t   reqLenOld = pMsg->contLen - sizeof(SMsgHead);
18,126✔
183

184
  SArray *tbUids = NULL;
18,126✔
185
  int64_t timestampMs = 0;
18,126✔
186

187
  SVDropTtlTableReq ttlReq = {0};
18,126✔
188
  if (tDeserializeSVDropTtlTableReq((char *)pContOld + sizeof(SMsgHead), reqLenOld, &ttlReq) != 0) {
18,126!
189
    code = TSDB_CODE_INVALID_MSG;
×
190
    TSDB_CHECK_CODE(code, lino, _exit);
×
191
  }
192

193
  {  // find expired uids
194
    tbUids = taosArrayInit(8, sizeof(tb_uid_t));
18,131✔
195
    if (tbUids == NULL) {
18,137!
196
      code = terrno;
×
197
      TSDB_CHECK_CODE(code, lino, _exit);
×
198
    }
199

200
    timestampMs = (int64_t)ttlReq.timestampSec * 1000;
18,137✔
201
    code = metaTtlFindExpired(pVnode->pMeta, timestampMs, tbUids, ttlReq.ttlDropMaxCount);
18,137✔
202
    if (code != 0) {
18,116!
203
      code = TSDB_CODE_INVALID_MSG;
×
204
      TSDB_CHECK_CODE(code, lino, _exit);
×
205
    }
206

207
    ttlReq.nUids = taosArrayGetSize(tbUids);
18,116✔
208
    ttlReq.pTbUids = tbUids;
18,118✔
209
  }
210

211
  if (ttlReq.nUids == 0) {
18,118✔
212
    code = TSDB_CODE_MSG_PREPROCESSED;
18,091✔
213
    TSDB_CHECK_CODE(code, lino, _exit);
18,091!
214
  }
215

216
  {  // prepare new content
217
    int32_t reqLenNew = tSerializeSVDropTtlTableReq(NULL, 0, &ttlReq);
22✔
218
    int32_t contLenNew = reqLenNew + sizeof(SMsgHead);
33✔
219

220
    SMsgHead *pContNew = rpcMallocCont(contLenNew);
33✔
221
    if (pContNew == NULL) {
33!
222
      code = terrno;
×
223
      TSDB_CHECK_CODE(code, lino, _exit);
×
224
    }
225

226
    if (tSerializeSVDropTtlTableReq((char *)pContNew + sizeof(SMsgHead), reqLenNew, &ttlReq) != 0) {
33!
227
      vError("vgId:%d %s:%d failed to serialize drop ttl request", TD_VID(pVnode), __func__, lino);
33!
228
    }
229
    pContNew->contLen = htonl(reqLenNew);
33✔
230
    pContNew->vgId = pContOld->vgId;
33✔
231

232
    rpcFreeCont(pContOld);
33✔
233
    pMsg->pCont = pContNew;
33✔
234
    pMsg->contLen = contLenNew;
33✔
235
  }
236

237
  code = 0;
33✔
238

239
_exit:
18,129✔
240
  taosArrayDestroy(tbUids);
18,129✔
241

242
  if (code && code != TSDB_CODE_MSG_PREPROCESSED) {
18,117!
243
    vError("vgId:%d, %s:%d failed to preprocess drop ttl request since %s, msg type:%s", TD_VID(pVnode), __func__, lino,
×
244
           tstrerror(code), TMSG_INFO(pMsg->msgType));
245
  } else {
246
    vTrace("vgId:%d, %s done, timestampSec:%d, nUids:%d", TD_VID(pVnode), __func__, ttlReq.timestampSec, ttlReq.nUids);
18,117✔
247
  }
248

249
  return code;
18,118✔
250
}
251

252
extern int64_t tsMaxKeyByPrecision[];
253
static int32_t vnodePreProcessSubmitTbData(SVnode *pVnode, SDecoder *pCoder, int64_t btimeMs, int64_t ctimeMs) {
1,778,304✔
254
  int32_t code = 0;
1,778,304✔
255
  int32_t lino = 0;
1,778,304✔
256

257
  if (tStartDecode(pCoder) < 0) {
1,778,304!
258
    code = TSDB_CODE_INVALID_MSG;
×
259
    TSDB_CHECK_CODE(code, lino, _exit);
×
260
  }
261

262
  SSubmitTbData submitTbData;
263
  uint8_t       version;
264
  if (tDecodeI32v(pCoder, &submitTbData.flags) < 0) {
1,778,699!
265
    code = TSDB_CODE_INVALID_MSG;
×
266
    TSDB_CHECK_CODE(code, lino, _exit);
×
267
  }
268
  version = (submitTbData.flags >> 8) & 0xff;
1,778,699✔
269
  submitTbData.flags = submitTbData.flags & 0xff;
1,778,699✔
270

271
  int64_t uid;
272
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
1,778,699✔
273
    code = vnodePreprocessCreateTableReq(pVnode, pCoder, btimeMs, &uid);
47,795✔
274
    TSDB_CHECK_CODE(code, lino, _exit);
47,913!
275
  }
276

277
  // submit data
278
  if (tDecodeI64(pCoder, &submitTbData.suid) < 0) {
1,778,807!
279
    code = TSDB_CODE_INVALID_MSG;
×
280
    TSDB_CHECK_CODE(code, lino, _exit);
×
281
  }
282

283
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
1,778,807✔
284
    taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), uid);
47,898✔
285
    pCoder->pos += sizeof(int64_t);
47,898✔
286
  } else {
287
    if (tDecodeI64(pCoder, &submitTbData.uid) < 0) {
1,730,890!
288
      code = TSDB_CODE_INVALID_MSG;
×
289
      TSDB_CHECK_CODE(code, lino, _exit);
×
290
    }
291
  }
292

293
  if (tDecodeI32v(pCoder, &submitTbData.sver) < 0) {
1,778,724!
294
    code = TSDB_CODE_INVALID_MSG;
×
295
    TSDB_CHECK_CODE(code, lino, _exit);
×
296
  }
297

298
  // scan and check
299
  TSKEY now = btimeMs;
1,778,724✔
300
  if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_MICRO) {
1,778,724✔
301
    now *= 1000;
14,988✔
302
  } else if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_NANO) {
1,763,736✔
303
    now *= 1000000;
2,908✔
304
  }
305

306
  int32_t keep = pVnode->config.tsdbCfg.keep2;
1,778,724✔
307

308
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * keep;
1,778,724✔
309
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
1,778,724✔
310
  if (submitTbData.flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
1,778,724✔
311
    uint64_t nColData;
312
    if (tDecodeU64v(pCoder, &nColData) < 0) {
999!
313
      code = TSDB_CODE_INVALID_MSG;
×
314
      TSDB_CHECK_CODE(code, lino, _exit);
1!
315
    }
316

317
    SColData colData = {0};
999✔
318
    code = tDecodeColData(version, pCoder, &colData);
999✔
319
    if (code) {
999!
320
      code = TSDB_CODE_INVALID_MSG;
×
321
      TSDB_CHECK_CODE(code, lino, _exit);
×
322
    }
323

324
    if (colData.flag != HAS_VALUE) {
999!
325
      code = TSDB_CODE_INVALID_MSG;
×
326
      TSDB_CHECK_CODE(code, lino, _exit);
×
327
    }
328

329
    for (int32_t iRow = 0; iRow < colData.nVal; iRow++) {
73,907✔
330
      if (((TSKEY *)colData.pData)[iRow] < minKey || ((TSKEY *)colData.pData)[iRow] > maxKey) {
72,909!
331
        code = TSDB_CODE_TDB_TIMESTAMP_OUT_OF_RANGE;
1✔
332
        TSDB_CHECK_CODE(code, lino, _exit);
1!
333
      }
334
    }
335

336
    for (uint64_t i = 1; i < nColData; i++) {
6,841✔
337
      code = tDecodeColData(version, pCoder, &colData);
5,842✔
338
      if (code) {
5,843!
339
        code = TSDB_CODE_INVALID_MSG;
×
340
        TSDB_CHECK_CODE(code, lino, _exit);
×
341
      }
342
    }
343
  } else {
344
    uint64_t nRow;
345
    if (tDecodeU64v(pCoder, &nRow) < 0) {
1,777,647!
346
      code = TSDB_CODE_INVALID_MSG;
×
347
      TSDB_CHECK_CODE(code, lino, _exit);
101!
348
    }
349

350
    for (int32_t iRow = 0; iRow < nRow; ++iRow) {
98,170,276✔
351
      SRow *pRow = (SRow *)(pCoder->data + pCoder->pos);
96,392,730✔
352
      pCoder->pos += pRow->len;
96,392,730✔
353
#ifndef NO_UNALIGNED_ACCESS
354
      if (pRow->ts < minKey || pRow->ts > maxKey) {
96,392,730✔
355
#else
356
      TSKEY ts = taosGetInt64Aligned(&pRow->ts);
357
      if (ts < minKey || ts > maxKey) {
358
#endif
359
        code = TSDB_CODE_TDB_TIMESTAMP_OUT_OF_RANGE;
285✔
360
        TSDB_CHECK_CODE(code, lino, _exit);
285✔
361
      }
362

363
      // Check pRow->sver
364
      if (pRow->sver != submitTbData.sver) {
96,392,629!
365
        code = TSDB_CODE_INVALID_DATA_FMT;
×
366
        TSDB_CHECK_CODE(code, lino, _exit);
×
367
      }
368
    }
369
  }
370

371
  if (!tDecodeIsEnd(pCoder)) {
1,778,545!
372
    taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), ctimeMs);
1,778,618✔
373
    pCoder->pos += sizeof(int64_t);
1,778,618✔
374
  }
375

376
  tEndDecode(pCoder);
1,778,545✔
377

378
_exit:
1,778,291✔
379
  if (code) {
1,778,291✔
380
    vError("vgId:%d, %s:%d failed to vnodePreProcessSubmitTbData submit request since %s", TD_VID(pVnode), __func__,
102!
381
           lino, tstrerror(code));
382
  }
383
  return code;
1,778,291✔
384
}
385
static int32_t vnodePreProcessSubmitMsg(SVnode *pVnode, SRpcMsg *pMsg) {
1,710,265✔
386
  int32_t code = 0;
1,710,265✔
387
  int32_t lino = 0;
1,710,265✔
388

389
  if (tsBypassFlag & TSDB_BYPASS_RA_RPC_RECV_SUBMIT) {
1,710,265✔
390
    return TSDB_CODE_MSG_PREPROCESSED;
1✔
391
  }
392

393
  SDecoder *pCoder = &(SDecoder){0};
1,710,264✔
394

395
  if (taosHton64(((SSubmitReq2Msg *)pMsg->pCont)->version) != 1) {
1,710,264!
396
    code = TSDB_CODE_INVALID_MSG;
×
397
    TSDB_CHECK_CODE(code, lino, _exit);
×
398
  }
399

400
  tDecoderInit(pCoder, (uint8_t *)pMsg->pCont + sizeof(SSubmitReq2Msg), pMsg->contLen - sizeof(SSubmitReq2Msg));
1,710,262✔
401

402
  if (tStartDecode(pCoder) < 0) {
1,710,264!
403
    code = TSDB_CODE_INVALID_MSG;
×
404
    TSDB_CHECK_CODE(code, lino, _exit);
×
405
  }
406

407
  uint64_t nSubmitTbData;
408
  if (tDecodeU64v(pCoder, &nSubmitTbData) < 0) {
1,710,266!
409
    code = TSDB_CODE_INVALID_MSG;
×
410
    TSDB_CHECK_CODE(code, lino, _exit);
×
411
  }
412

413
  int64_t btimeMs = taosGetTimestampMs();
1,710,265✔
414
  int64_t ctimeMs = btimeMs;
1,710,265✔
415
  for (int32_t i = 0; i < nSubmitTbData; i++) {
3,488,454✔
416
    code = vnodePreProcessSubmitTbData(pVnode, pCoder, btimeMs, ctimeMs);
1,778,302✔
417
    TSDB_CHECK_CODE(code, lino, _exit);
1,778,291✔
418
  }
419

420
  tEndDecode(pCoder);
1,710,152✔
421

422
_exit:
1,710,258✔
423
  tDecoderClear(pCoder);
1,710,258✔
424
  if (code) {
1,710,263✔
425
    vError("vgId:%d, %s:%d failed to preprocess submit request since %s, msg type:%s", TD_VID(pVnode), __func__, lino,
102!
426
           tstrerror(code), TMSG_INFO(pMsg->msgType));
427
  }
428
  return code;
1,710,263✔
429
}
430

431
static int32_t vnodePreProcessDeleteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
7,013✔
432
  int32_t code = 0;
7,013✔
433

434
  int32_t    size;
435
  int32_t    ret;
436
  uint8_t   *pCont;
437
  SEncoder  *pCoder = &(SEncoder){0};
7,013✔
438
  SDeleteRes res = {0};
7,013✔
439

440
  SReadHandle handle = {0};
7,013✔
441
  handle.vnode = pVnode;
7,013✔
442
  handle.pMsgCb = &pVnode->msgCb;
7,013✔
443
  handle.skipRollup = 1;
7,013✔
444
  initStorageAPI(&handle.api);
7,013✔
445

446
  code = qWorkerProcessDeleteMsg(&handle, pVnode->pQuery, pMsg, &res);
7,013✔
447
  if (code) goto _exit;
7,013!
448

449
  res.ctimeMs = taosGetTimestampMs();
7,013✔
450
  // malloc and encode
451
  tEncodeSize(tEncodeDeleteRes, &res, size, ret);
7,013!
452
  pCont = rpcMallocCont(size + sizeof(SMsgHead));
7,013✔
453

454
  ((SMsgHead *)pCont)->contLen = size + sizeof(SMsgHead);
7,013✔
455
  ((SMsgHead *)pCont)->vgId = TD_VID(pVnode);
7,013✔
456

457
  tEncoderInit(pCoder, pCont + sizeof(SMsgHead), size);
7,013✔
458
  if (tEncodeDeleteRes(pCoder, &res) != 0) {
7,013!
459
    vError("vgId:%d %s failed to encode delete response", TD_VID(pVnode), __func__);
×
460
  }
461
  tEncoderClear(pCoder);
7,013✔
462

463
  rpcFreeCont(pMsg->pCont);
7,013✔
464
  pMsg->pCont = pCont;
7,013✔
465
  pMsg->contLen = size + sizeof(SMsgHead);
7,013✔
466

467
  taosArrayDestroy(res.uidList);
7,013✔
468

469
_exit:
7,013✔
470
  return code;
7,013✔
471
}
472

473
static int32_t vnodePreProcessBatchDeleteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
×
474
  int32_t code = 0;
×
475
  int32_t lino = 0;
×
476

477
  int64_t         ctimeMs = taosGetTimestampMs();
×
478
  SBatchDeleteReq pReq = {0};
×
479
  SDecoder       *pCoder = &(SDecoder){0};
×
480

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

483
  if (tDecodeSBatchDeleteReqSetCtime(pCoder, &pReq, ctimeMs) < 0) {
×
484
    code = TSDB_CODE_INVALID_MSG;
×
485
  }
486

487
  tDecoderClear(pCoder);
×
488
  taosArrayDestroy(pReq.deleteReqs);
×
489

490
  if (code) {
×
491
    vError("vgId:%d %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
492
  } else {
493
    vTrace("vgId:%d %s done, ctimeMs generated:%" PRId64, TD_VID(pVnode), __func__, ctimeMs);
×
494
  }
495
  return code;
×
496
}
497

498
static int32_t vnodePreProcessArbCheckSyncMsg(SVnode *pVnode, SRpcMsg *pMsg) {
70✔
499
  int32_t ret = 0;
70✔
500
  if ((ret = vnodeCheckState(pVnode)) != 0) {
70✔
501
    vDebug("vgId:%d, failed to preprocess vnode-arb-check-sync request since %s", TD_VID(pVnode), tstrerror(ret));
49!
502
    return 0;
49✔
503
  }
504

505
  SVArbCheckSyncReq syncReq = {0};
21✔
506

507
  if (tDeserializeSVArbCheckSyncReq((char *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead),
21!
508
                                    &syncReq) != 0) {
509
    return TSDB_CODE_INVALID_MSG;
×
510
  }
511

512
  ret = vnodeCheckToken(pVnode, syncReq.member0Token, syncReq.member1Token);
21✔
513
  if (ret != 0) {
21✔
514
    vError("vgId:%d, failed to preprocess vnode-arb-check-sync request since %s", TD_VID(pVnode), tstrerror(ret));
15!
515
  }
516

517
  int32_t code = terrno;
21✔
518
  tFreeSVArbCheckSyncReq(&syncReq);
21✔
519

520
  return code;
21✔
521
}
522

523
int32_t vnodePreProcessDropTbMsg(SVnode *pVnode, SRpcMsg *pMsg) {
15,711✔
524
  int32_t          code = TSDB_CODE_SUCCESS;
15,711✔
525
  int32_t          lino = 0;
15,711✔
526
  int32_t          size = 0;
15,711✔
527
  SDecoder         dc = {0};
15,711✔
528
  SEncoder         ec = {0};
15,711✔
529
  SVDropTbBatchReq receivedBatchReqs = {0};
15,711✔
530
  SVDropTbBatchReq sentBatchReqs = {0};
15,711✔
531

532
  tDecoderInit(&dc, POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), pMsg->contLen - sizeof(SMsgHead));
15,711✔
533

534
  code = tDecodeSVDropTbBatchReq(&dc, &receivedBatchReqs);
15,711✔
535
  if (code < 0) {
15,711!
536
    terrno = code;
×
537
    TSDB_CHECK_CODE(code, lino, _exit);
×
538
  }
539
  sentBatchReqs.pArray = taosArrayInit(receivedBatchReqs.nReqs, sizeof(SVDropTbReq));
15,711✔
540
  if (!sentBatchReqs.pArray) {
15,711!
541
    code = terrno;
×
542
    goto _exit;
×
543
  }
544

545
  for (int32_t i = 0; i < receivedBatchReqs.nReqs; ++i) {
31,825✔
546
    SVDropTbReq *pReq = receivedBatchReqs.pReqs + i;
16,114✔
547
    tb_uid_t     uid = metaGetTableEntryUidByName(pVnode->pMeta, pReq->name);
16,114✔
548
    if (uid == 0) {
16,114!
549
      vWarn("vgId:%d, preprocess drop ctb: %s not found", TD_VID(pVnode), pReq->name);
×
550
      continue;
×
551
    }
552
    pReq->uid = uid;
16,114✔
553
    vDebug("vgId:%d %s for: %s, uid: %" PRId64, TD_VID(pVnode), __func__, pReq->name, pReq->uid);
16,114!
554
    if (taosArrayPush(sentBatchReqs.pArray, pReq) == NULL) {
32,228!
555
      code = terrno;
×
556
      goto _exit;
×
557
    }
558
  }
559
  sentBatchReqs.nReqs = sentBatchReqs.pArray->size;
15,711✔
560

561
  tEncodeSize(tEncodeSVDropTbBatchReq, &sentBatchReqs, size, code);
15,711!
562
  tEncoderInit(&ec, POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), size);
15,711✔
563
  code = tEncodeSVDropTbBatchReq(&ec, &sentBatchReqs);
15,711✔
564
  tEncoderClear(&ec);
15,711✔
565
  if (code != TSDB_CODE_SUCCESS) {
15,711!
566
    vError("vgId:%d %s failed to encode drop tb batch req: %s", TD_VID(pVnode), __func__, tstrerror(code));
×
567
    TSDB_CHECK_CODE(code, lino, _exit);
×
568
  }
569

570
_exit:
15,711✔
571
  tDecoderClear(&dc);
15,711✔
572
  if (sentBatchReqs.pArray) {
15,711!
573
    taosArrayDestroy(sentBatchReqs.pArray);
15,711✔
574
  }
575
  return code;
15,711✔
576
}
577

578
int32_t vnodePreProcessSsMigrateReq(SVnode *pVnode, SRpcMsg *pMsg) {
×
579
  int32_t             code = TSDB_CODE_SUCCESS;
×
580
  int32_t             lino = 0;
×
581
  SSsMigrateVgroupReq req = {0};
×
582

583
  code = tDeserializeSSsMigrateVgroupReq(POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), pMsg->contLen - sizeof(SMsgHead),
×
584
                                         &req);
585
  if (code < 0) {
×
586
    terrno = code;
×
587
    TSDB_CHECK_CODE(code, lino, _exit);
×
588
  }
589

590
  req.nodeId = vnodeNodeId(pVnode);
×
591
  if (tSerializeSSsMigrateVgroupReq(POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), pMsg->contLen - sizeof(SMsgHead),
×
592
                                    &req) < 0) {
593
    vError("vgId:%d %s failed to serialize ss migrate request", TD_VID(pVnode), __func__);
×
594
    code = TSDB_CODE_INVALID_MSG;
×
595
  }
596
_exit:
×
597
  return code;
×
598
}
599

600
int32_t vnodePreProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
1,902,695✔
601
  int32_t code = 0;
1,902,695✔
602

603
  if (pVnode->mounted) {
1,902,695✔
604
    vError("vgId:%d, mountVgId:%d, skip write msg:%s", TD_VID(pVnode), pVnode->config.mountVgId,
10!
605
           TMSG_INFO(pMsg->msgType));
606
    return TSDB_CODE_OPS_NOT_SUPPORT;
10✔
607
  }
608

609
  switch (pMsg->msgType) {
1,902,685!
610
    case TDMT_VND_CREATE_TABLE: {
68,802✔
611
      code = vnodePreProcessCreateTableMsg(pVnode, pMsg);
68,802✔
612
    } break;
68,800✔
613
    case TDMT_VND_ALTER_TABLE: {
19,674✔
614
      code = vnodePreProcessAlterTableMsg(pVnode, pMsg);
19,674✔
615
    } break;
19,674✔
616
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
18,130✔
617
    case TDMT_VND_DROP_TTL_TABLE: {
618
      code = vnodePreProcessDropTtlMsg(pVnode, pMsg);
18,130✔
619
    } break;
18,115✔
620
    case TDMT_VND_SUBMIT: {
1,710,266✔
621
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.preprocess_time, METRIC_LEVEL_LOW,
1,710,266!
622
                           { code = vnodePreProcessSubmitMsg(pVnode, pMsg); });
623
    } break;
1,710,264✔
624
    case TDMT_VND_DELETE: {
7,013✔
625
      code = vnodePreProcessDeleteMsg(pVnode, pMsg);
7,013✔
626
    } break;
7,013✔
627
    case TDMT_VND_BATCH_DEL: {
×
628
      code = vnodePreProcessBatchDeleteMsg(pVnode, pMsg);
×
629
    } break;
×
630
    case TDMT_VND_ARB_CHECK_SYNC: {
70✔
631
      code = vnodePreProcessArbCheckSyncMsg(pVnode, pMsg);
70✔
632
    } break;
70✔
633
    case TDMT_VND_DROP_TABLE: {
15,711✔
634
      code = vnodePreProcessDropTbMsg(pVnode, pMsg);
15,711✔
635
    } break;
15,711✔
636
    case TDMT_VND_SSMIGRATE: {
×
637
      code = vnodePreProcessSsMigrateReq(pVnode, pMsg);
×
638
    } break;
×
639
    default:
63,019✔
640
      break;
63,019✔
641
  }
642

643
  if (code && code != TSDB_CODE_MSG_PREPROCESSED) {
1,902,666✔
644
    vError("vgId:%d, failed to preprocess write request since %s, msg type:%s", TD_VID(pVnode), tstrerror(code),
117!
645
           TMSG_INFO(pMsg->msgType));
646
  }
647
  return code;
1,902,654✔
648
}
649

650
static int32_t inline vnodeSubmitSubRowBlobData(SVnode *pVnode, SSubmitTbData *pSubmitTbData) {
×
651
  int32_t code = 0;
×
652
  int32_t lino = 0;
×
653

654
  int64_t   st = taosGetTimestampUs();
×
655
  SBlobSet *pBlobSet = pSubmitTbData->pBlobSet;
×
656
  int32_t   sz = taosArrayGetSize(pBlobSet->pSeqTable);
×
657

658
  SBseBatch *pBatch = NULL;
×
659

660
  code = bseBatchInit(pVnode->pBse, &pBatch, sz);
×
661
  TSDB_CHECK_CODE(code, lino, _exit);
×
662

663
  SRow  **pRow = (SRow **)TARRAY_DATA(pSubmitTbData->aRowP);
×
664
  int32_t rowIdx = -1;
×
665
  for (int32_t i = 0; i < sz; i++) {
×
666
    int64_t     seq = 0;
×
667
    SBlobValue *p = taosArrayGet(pBlobSet->pSeqTable, i);
×
668
    if (p->type == TSDB_DATA_BLOB_EMPTY_VALUE || p->type == TSDB_DATA_BLOB_NULL_VALUE) {
×
669
      // skip empty or null blob
670
      continue;
×
671
    }
672

673
    code = bseBatchPut(pBatch, &seq, pBlobSet->data + p->offset, p->len);
×
674
    TSDB_CHECK_CODE(code, lino, _exit);
×
675

676
    SRow *row = taosArrayGetP(pSubmitTbData->aRowP, i);
×
677
    if (row == NULL) {
×
678
      int32_t tlen = taosArrayGetSize(pBlobSet->pSeqTable);
×
679
      uTrace("blob invalid row index:%d, sz:%d, pBlobSet size:%d", rowIdx, sz, tlen);
×
680
      break;
×
681
    }
682

683
    if (tPutU64(row->data + p->dataOffset, seq) < 0) {
×
684
      code = TSDB_CODE_INVALID_MSG;
×
685
      TSDB_CHECK_CODE(code, lino, _exit);
×
686
    }
687
  }
688

689
  code = bseCommitBatch(pVnode->pBse, pBatch);
×
690
  TSDB_CHECK_CODE(code, lino, _exit);
×
691

692
  int64_t cost = taosGetTimestampUs() - st;
×
693
  if (cost >= 500) {
×
694
    vDebug("vgId:%d, %s, cost:%" PRId64 "us, rows:%d, size:%" PRId64 "", TD_VID(pVnode), __func__, cost, sz,
×
695
           pBlobSet->len);
696
  }
697
_exit:
×
698
  if (code != 0) {
×
699
    vError("vgId:%d %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
700
  }
701
  return code;
×
702
}
703

704
static int32_t inline vnodeSubmitSubColBlobData(SVnode *pVnode, SSubmitTbData *pSubmitTbData) {
×
705
  int32_t code = 0;
×
706
  int32_t lino = 0;
×
707

708
  int32_t   blobColIdx = 0;
×
709
  SColData *pBlobCol = NULL;
×
710
  int64_t   st = taosGetTimestampUs();
×
711
  SBlobSet *pBlobSet = pSubmitTbData->pBlobSet;
×
712
  int32_t   sz = taosArrayGetSize(pBlobSet->pSeqTable);
×
713

714
  SBseBatch *pBatch = NULL;
×
715

716
  code = bseBatchInit(pVnode->pBse, &pBatch, sz);
×
717
  TSDB_CHECK_CODE(code, lino, _exit);
×
718

719
  SColData *p = (SColData *)TARRAY_DATA(pSubmitTbData->aCol);
×
720
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitTbData->aCol); i++) {
×
721
    if (IS_STR_DATA_BLOB(p[i].type)) {
×
722
      pBlobCol = &p[i];
×
723
      break;
×
724
    }
725
  }
726
  if (pBlobCol == NULL) {
×
727
    vError("vgId:%d %s failed to find blob column in submit data", TD_VID(pVnode), __func__);
×
728
    code = TSDB_CODE_INVALID_MSG;
×
729
    goto _exit;
×
730
  }
731

732
  int32_t offset = 0;
×
733
  // int32_t   rowIdx = -1;
734
  for (int32_t i = 0; i < sz; i++) {
×
735
    int64_t     seq = 0;
×
736
    SBlobValue *p = taosArrayGet(pBlobSet->pSeqTable, i);
×
737
    if (p->type == TSDB_DATA_BLOB_EMPTY_VALUE || p->type == TSDB_DATA_BLOB_NULL_VALUE) {
×
738
      // skip empty or null blob
739
      continue;
×
740
    }
741
    code = bseBatchPut(pBatch, &seq, pBlobSet->data + p->offset, p->len);
×
742
    TSDB_CHECK_CODE(code, lino, _exit);
×
743

744
    memcpy(pBlobCol->pData + offset, (void *)&seq, BSE_SEQUECE_SIZE);
×
745
    offset += BSE_SEQUECE_SIZE;
×
746
  }
747

748
  code = bseCommitBatch(pVnode->pBse, pBatch);
×
749
  TSDB_CHECK_CODE(code, lino, _exit);
×
750

751
  int64_t cost = taosGetTimestampUs() - st;
×
752
  if (cost >= 500) {
×
753
    vDebug("vgId:%d, %s, cost:%" PRId64 "us, rows:%d, size:%" PRId64 "", TD_VID(pVnode), __func__, cost, sz,
×
754
           pBlobSet->len);
755
  }
756
_exit:
×
757
  if (code != 0) {
×
758
    vError("vgId:%d %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
759
  }
760
  return code;
×
761
}
762
static int32_t inline vnodeSubmitBlobData(SVnode *pVnode, SSubmitTbData *pData) {
×
763
  int32_t code = 0;
×
764
  if (pData->flags & SUBMIT_REQ_WITH_BLOB) {
×
765
    if (pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
×
766
      code = vnodeSubmitSubColBlobData(pVnode, pData);
×
767
    } else {
768
      code = vnodeSubmitSubRowBlobData(pVnode, pData);
×
769
    }
770
  }
771

772
  return code;
×
773
}
774

775
int32_t vnodeProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg, int64_t ver, SRpcMsg *pRsp) {
2,239,159✔
776
  int32_t code = 0;
2,239,159✔
777
  int32_t lino = 0;
2,239,159✔
778
  void   *ptr = NULL;
2,239,159✔
779
  void   *pReq;
780
  int32_t len;
781

782
  (void)taosThreadMutexLock(&pVnode->mutex);
2,239,159✔
783
  if (pVnode->disableWrite) {
2,239,303!
784
    (void)taosThreadMutexUnlock(&pVnode->mutex);
×
785
    vError("vgId:%d write is disabled for snapshot, version:%" PRId64, TD_VID(pVnode), ver);
×
786
    return TSDB_CODE_VND_WRITE_DISABLED;
×
787
  }
788
  (void)taosThreadMutexUnlock(&pVnode->mutex);
2,239,303✔
789

790
  if (ver <= pVnode->state.applied) {
2,239,288!
791
    vError("vgId:%d, duplicate write request. ver: %" PRId64 ", applied: %" PRId64, TD_VID(pVnode), ver,
×
792
           pVnode->state.applied);
793
    return terrno = TSDB_CODE_VND_DUP_REQUEST;
×
794
  }
795

796
  vGDebug(&pMsg->info.traceId,
2,239,288!
797
          "vgId:%d, index:%" PRId64 ", process write request:%s, applied:%" PRId64 ", state.applyTerm:%" PRId64
798
          ", conn.applyTerm:%" PRId64 ", contLen:%d",
799
          TD_VID(pVnode), ver, TMSG_INFO(pMsg->msgType), pVnode->state.applied, pVnode->state.applyTerm,
800
          pMsg->info.conn.applyTerm, pMsg->contLen);
801

802
  if (!(pVnode->state.applyTerm <= pMsg->info.conn.applyTerm)) {
2,239,307!
803
    vError("vgId:%d, applyTerm mismatch, expected: %" PRId64 ", received: %" PRId64, TD_VID(pVnode),
×
804
           pVnode->state.applyTerm, pMsg->info.conn.applyTerm);
805
    return terrno = TSDB_CODE_INTERNAL_ERROR;
×
806
  }
807

808
  if (!(pVnode->state.applied + 1 == ver)) {
2,239,307!
809
    vError("vgId:%d, mountVgId:%d, ver mismatch, expected: %" PRId64 ", received: %" PRId64, TD_VID(pVnode),
×
810
           pVnode->config.mountVgId, pVnode->state.applied + 1, ver);
811
    return terrno = TSDB_CODE_INTERNAL_ERROR;
×
812
  }
813

814
  atomic_store_64(&pVnode->state.applied, ver);
2,239,307✔
815
  atomic_store_64(&pVnode->state.applyTerm, pMsg->info.conn.applyTerm);
2,239,316✔
816

817
  if (!syncUtilUserCommit(pMsg->msgType)) goto _exit;
2,239,317✔
818

819
  // skip header
820
  pReq = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
2,212,559✔
821
  len = pMsg->contLen - sizeof(SMsgHead);
2,212,559✔
822
  bool needCommit = false;
2,212,559✔
823

824
  switch (pMsg->msgType) {
2,212,559!
825
    /* META */
826
    case TDMT_VND_CREATE_STB:
25,624✔
827
      code = vnodeProcessCreateStbReq(pVnode, ver, pReq, len, pRsp);
25,624✔
828
      TSDB_CHECK_CODE(code, lino, _err);
25,609✔
829
      break;
25,606✔
830
    case TDMT_VND_ALTER_STB:
16,084✔
831
      code = vnodeProcessAlterStbReq(pVnode, ver, pReq, len, pRsp);
16,084✔
832
      TSDB_CHECK_CODE(code, lino, _err);
16,084!
833
      break;
16,084✔
834
    case TDMT_VND_DROP_STB:
2,032✔
835
      code = vnodeProcessDropStbReq(pVnode, ver, pReq, len, pRsp);
2,032✔
836
      TSDB_CHECK_CODE(code, lino, _err);
2,032!
837
      break;
2,032✔
838
    case TDMT_VND_CREATE_TABLE:
76,383✔
839
      code = vnodeProcessCreateTbReq(pVnode, ver, pReq, len, pRsp, pMsg);
76,383✔
840
      TSDB_CHECK_CODE(code, lino, _err);
76,383!
841
      break;
76,383✔
842
    case TDMT_VND_ALTER_TABLE:
19,681✔
843
      code = vnodeProcessAlterTbReq(pVnode, ver, pReq, len, pRsp);
19,681✔
844
      TSDB_CHECK_CODE(code, lino, _err);
19,681!
845
      break;
19,681✔
846
    case TDMT_VND_DROP_TABLE:
15,712✔
847
      code = vnodeProcessDropTbReq(pVnode, ver, pReq, len, pRsp, pMsg);
15,712✔
848
      TSDB_CHECK_CODE(code, lino, _err);
15,712!
849
      break;
15,712✔
850
    case TDMT_VND_DROP_TTL_TABLE:
×
851
      code = vnodeProcessDropTtlTbReq(pVnode, ver, pReq, len, pRsp);
×
852
      TSDB_CHECK_CODE(code, lino, _err);
×
853
      break;
×
854
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
33✔
855
      code = vnodeProcessFetchTtlExpiredTbs(pVnode, ver, pReq, len, pRsp);
33✔
856
      TSDB_CHECK_CODE(code, lino, _err);
33!
857
      break;
33✔
858
    case TDMT_VND_TRIM:
63✔
859
      code = vnodeProcessTrimReq(pVnode, ver, pReq, len, pRsp);
63✔
860
      TSDB_CHECK_CODE(code, lino, _err);
63!
861
      break;
63✔
862
    case TDMT_VND_SSMIGRATE:
×
863
      if (vnodeProcessSsMigrateReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
864
      break;
×
865
    case TDMT_VND_FOLLOWER_SSMIGRATE:
×
866
      if (vnodeProcessFollowerSsMigrateReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
867
      break;
×
868
      break;
869
    /* TSDB */
870
    case TDMT_VND_SUBMIT: {
2,005,870✔
871
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.apply_time, METRIC_LEVEL_LOW, {
2,005,870!
872
        if (vnodeProcessSubmitReq(pVnode, ver, pMsg->pCont, pMsg->contLen, pRsp, pMsg) < 0) goto _err;
873
      });
874
      METRICS_UPDATE(pVnode->writeMetrics.apply_bytes, METRIC_LEVEL_LOW, (int64_t)pMsg->contLen);
2,005,767!
875
      break;
2,005,767✔
876
    }
877
    case TDMT_VND_DELETE:
8,239✔
878
      code = vnodeProcessDeleteReq(pVnode, ver, pReq, len, pRsp, pMsg);
8,239✔
879
      TSDB_CHECK_CODE(code, lino, _err);
8,239!
880
      break;
8,239✔
881
    case TDMT_VND_BATCH_DEL:
×
882
      code = vnodeProcessBatchDeleteReq(pVnode, ver, pReq, len, pRsp);
×
883
      TSDB_CHECK_CODE(code, lino, _err);
×
884
      break;
×
885
      /* TQ */
886
#if defined(USE_TQ) || defined(USE_STREAM)
887
    case TDMT_VND_TMQ_SUBSCRIBE:
2,674✔
888
      code = tqProcessSubscribeReq(pVnode->pTq, ver, pReq, len);
2,674✔
889
      TSDB_CHECK_CODE(code, lino, _err);
2,673!
890
      break;
2,673✔
891
    case TDMT_VND_TMQ_DELETE_SUB:
870✔
892
      code = tqProcessDeleteSubReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen);
870✔
893
      TSDB_CHECK_CODE(code, lino, _err);
869!
894
      break;
869✔
895
    case TDMT_VND_TMQ_COMMIT_OFFSET:
8,782✔
896
      code = tqProcessOffsetCommitReq(pVnode->pTq, ver, pReq, len);
8,782✔
897
      TSDB_CHECK_CODE(code, lino, _err);
8,783!
898
      break;
8,783✔
899
    case TDMT_VND_TMQ_ADD_CHECKINFO:
48✔
900
      code = tqProcessAddCheckInfoReq(pVnode->pTq, ver, pReq, len);
48✔
901
      TSDB_CHECK_CODE(code, lino, _err);
48!
902
      break;
48✔
903
    case TDMT_VND_TMQ_DEL_CHECKINFO:
10✔
904
      code = tqProcessDelCheckInfoReq(pVnode->pTq, ver, pReq, len);
10✔
905
      TSDB_CHECK_CODE(code, lino, _err);
10!
906
      break;
10✔
907
#endif
908
    case TDMT_VND_ALTER_CONFIRM:
8,221✔
909
      needCommit = pVnode->config.hashChange;
8,221✔
910
      code = vnodeProcessAlterConfirmReq(pVnode, ver, pReq, len, pRsp);
8,221✔
911
      TSDB_CHECK_CODE(code, lino, _err);
8,221!
912
      break;
8,221✔
913
    case TDMT_VND_ALTER_CONFIG:
614✔
914
      vnodeProcessAlterConfigReq(pVnode, ver, pReq, len, pRsp);
614✔
915
      break;
614✔
916
    case TDMT_VND_COMMIT:
21,391✔
917
      needCommit = true;
21,391✔
918
      break;
21,391✔
919
    case TDMT_VND_CREATE_INDEX:
19✔
920
      vnodeProcessCreateIndexReq(pVnode, ver, pReq, len, pRsp);
19✔
921
      break;
19✔
922
    case TDMT_VND_DROP_INDEX:
10✔
923
      vnodeProcessDropIndexReq(pVnode, ver, pReq, len, pRsp);
10✔
924
      break;
10✔
925
    case TDMT_VND_COMPACT:
175✔
926
      vnodeProcessCompactVnodeReq(pVnode, ver, pReq, len, pRsp);
175✔
927
      goto _exit;
174✔
928
    case TDMT_SYNC_CONFIG_CHANGE:
×
929
      vnodeProcessConfigChangeReq(pVnode, ver, pReq, len, pRsp);
×
930
      break;
×
931
#ifdef TD_ENTERPRISE
932
    case TDMT_VND_KILL_COMPACT:
2✔
933
      vnodeProcessKillCompactReq(pVnode, ver, pReq, len, pRsp);
2✔
934
      break;
2✔
935
#endif
936
    /* ARB */
937
    case TDMT_VND_ARB_CHECK_SYNC:
22✔
938
      vnodeProcessArbCheckSyncReq(pVnode, pReq, len, pRsp);
22✔
939
      break;
22✔
940
    default:
×
941
      vError("vgId:%d, unprocessed msg, %d", TD_VID(pVnode), pMsg->msgType);
×
942
      return TSDB_CODE_INVALID_MSG;
×
943
  }
944

945
  vGDebug(&pMsg->info.traceId, "vgId:%d, index:%" PRId64 ", msg processed, code:0x%x", TD_VID(pVnode), ver, pRsp->code);
2,212,262!
946

947
  walApplyVer(pVnode->pWal, ver);
2,212,282✔
948

949
  if (pVnode->pTq) {
2,212,282!
950
    code = tqPushMsg(pVnode->pTq, pMsg->msgType);
2,212,282✔
951
    if (code) {
2,212,281!
952
      vError("vgId:%d, failed to push msg to TQ since %s", TD_VID(pVnode), tstrerror(terrno));
×
953
      return code;
×
954
    }
955
  }
956

957
  // commit if need
958
  if (needCommit) {
2,212,281✔
959
    vInfo("vgId:%d, commit at version %" PRId64, TD_VID(pVnode), ver);
21,455✔
960
    code = vnodeAsyncCommit(pVnode);
21,457✔
961
    if (code) {
21,457!
962
      vError("vgId:%d, failed to vnode async commit since %s.", TD_VID(pVnode), tstrerror(terrno));
×
963
      goto _err;
×
964
    }
965

966
    // start a new one
967
    METRICS_TIMING_BLOCK(pVnode->writeMetrics.memtable_wait_time, METRIC_LEVEL_LOW, {
21,457!
968
      code = vnodeBegin(pVnode);
969
      if (code) {
970
        vError("vgId:%d, failed to begin vnode since %s.", TD_VID(pVnode), tstrerror(terrno));
971
        goto _err;
972
      }
973
    });
974
  }
975

976
_exit:
2,212,279✔
977
  return 0;
2,239,199✔
978

979
_err:
109✔
980
  if (code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
109✔
981
    vInfo("vgId:%d, process %s request failed since %s, lino 1:%d ver:%" PRId64, TD_VID(pVnode),
3!
982
          TMSG_INFO(pMsg->msgType), tstrerror(code), lino, ver);
983
  } else if (code) {
106!
984
    vError("vgId:%d, process %s request failed since %s, lino 2:%d ver:%" PRId64, TD_VID(pVnode),
×
985
           TMSG_INFO(pMsg->msgType), tstrerror(code), lino, ver);
986
  }
987

988
  return code;
109✔
989
}
990

991
int32_t vnodePreprocessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg) {
1,357,412✔
992
  if (TDMT_SCH_QUERY != pMsg->msgType && TDMT_SCH_MERGE_QUERY != pMsg->msgType) {
1,357,412✔
993
    return 0;
72,086✔
994
  }
995

996
  return qWorkerPreprocessQueryMsg(pVnode->pQuery, pMsg, TDMT_SCH_QUERY == pMsg->msgType);
1,285,326✔
997
}
998

999
int32_t vnodeProcessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
1,357,799✔
1000
  vTrace("message in vnode query queue is processing");
1,357,799!
1001
  if (pMsg->msgType == TDMT_VND_TMQ_CONSUME && !syncIsReadyForRead(pVnode->sync)) {
1,357,799✔
1002
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
1,291✔
1003
    return 0;
1,291✔
1004
  }
1005

1006
  if (pMsg->msgType == TDMT_VND_TMQ_CONSUME && !pVnode->restored) {
1,356,505!
1007
    vnodeRedirectRpcMsg(pVnode, pMsg, TSDB_CODE_SYN_RESTORING);
×
1008
    return 0;
×
1009
  }
1010

1011
  SReadHandle handle = {0};
1,356,505✔
1012
  handle.vnode = pVnode;
1,356,505✔
1013
  handle.pMsgCb = &pVnode->msgCb;
1,356,505✔
1014
  handle.pWorkerCb = pInfo->workerCb;
1,356,505✔
1015
  initStorageAPI(&handle.api);
1,356,505✔
1016
  int32_t code = TSDB_CODE_SUCCESS;
1,356,430✔
1017
  bool    redirected = false;
1,356,430✔
1018

1019
  switch (pMsg->msgType) {
1,356,430!
1020
    case TDMT_SCH_QUERY:
1,164,720✔
1021
      if (!syncIsReadyForRead(pVnode->sync)) {
1,164,720✔
1022
        pMsg->code = (terrno) ? terrno : TSDB_CODE_SYN_NOT_LEADER;
639!
1023
        redirected = true;
639✔
1024
      }
1025
      code = qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
1,164,422✔
1026
      if (redirected) {
1,164,746✔
1027
        vnodeRedirectRpcMsg(pVnode, pMsg, pMsg->code);
641✔
1028
        return 0;
640✔
1029
      }
1030

1031
      return code;
1,164,105✔
1032
    case TDMT_SCH_MERGE_QUERY:
120,930✔
1033
      return qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
120,930✔
1034
    case TDMT_SCH_QUERY_CONTINUE:
19,806✔
1035
      return qWorkerProcessCQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
19,806✔
1036
    case TDMT_VND_TMQ_CONSUME:
39,876✔
1037
      return tqProcessPollReq(pVnode->pTq, pMsg);
39,876✔
1038
    case TDMT_VND_TMQ_CONSUME_PUSH:
11,099✔
1039
      return tqProcessPollPush(pVnode->pTq);
11,099✔
1040
    default:
×
1041
      vError("unknown msg type:%d in query queue", pMsg->msgType);
×
1042
      return TSDB_CODE_APP_ERROR;
×
1043
  }
1044
}
1045

1046
int32_t vnodeProcessFetchMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
3,506,387✔
1047
  int32_t code = TSDB_CODE_SUCCESS;
3,506,387✔
1048
  vTrace("vgId:%d, msg:%p in fetch queue is processing", pVnode->config.vgId, pMsg);
3,506,387!
1049
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
3,506,387✔
1050
       pMsg->msgType == TDMT_VND_BATCH_META || pMsg->msgType == TDMT_VND_TABLE_NAME ||
2,321,827!
1051
       pMsg->msgType == TDMT_VND_VSUBTABLES_META || pMsg->msgType == TDMT_VND_VSTB_REF_DBS) &&
2,226,176!
1052
      !syncIsReadyForRead(pVnode->sync)) {
1,280,826✔
1053
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
1,394✔
1054
    return 0;
1,394✔
1055
  }
1056

1057
  switch (pMsg->msgType) {
3,504,935!
1058
    case TDMT_SCH_FETCH:
1,325,620✔
1059
    case TDMT_SCH_MERGE_FETCH:
1060
      return qWorkerProcessFetchMsg(pVnode, pVnode->pQuery, pMsg, 0);
1,325,620✔
1061
    case TDMT_SCH_FETCH_RSP:
×
1062
      return qWorkerProcessRspMsg(pVnode, pVnode->pQuery, pMsg, 0);
×
1063
    // case TDMT_SCH_CANCEL_TASK:
1064
    //   return qWorkerProcessCancelMsg(pVnode, pVnode->pQuery, pMsg, 0);
1065
    case TDMT_SCH_DROP_TASK:
1,284,429✔
1066
      return qWorkerProcessDropMsg(pVnode, pVnode->pQuery, pMsg, 0);
1,284,429✔
1067
    case TDMT_SCH_TASK_NOTIFY:
26✔
1068
      return qWorkerProcessNotifyMsg(pVnode, pVnode->pQuery, pMsg, 0);
26✔
1069
    case TDMT_SCH_QUERY_HEARTBEAT:
797,989✔
1070
      return qWorkerProcessHbMsg(pVnode, pVnode->pQuery, pMsg, 0);
797,989✔
1071
    case TDMT_VND_TABLE_META:
2,411✔
1072
    case TDMT_VND_TABLE_NAME:
1073
      return vnodeGetTableMeta(pVnode, pMsg, true);
2,411✔
1074
    case TDMT_VND_TABLE_CFG:
×
1075
      return vnodeGetTableCfg(pVnode, pMsg, true);
×
1076
    case TDMT_VND_BATCH_META: {
94,290✔
1077
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.fetch_batch_meta_time, METRIC_LEVEL_LOW,
94,290!
1078
                           { code = vnodeGetBatchMeta(pVnode, pMsg); });
1079
      METRICS_UPDATE(pVnode->writeMetrics.fetch_batch_meta_count, METRIC_LEVEL_LOW, 1);
94,290!
1080
      return code;
94,290✔
1081
    }
1082
    case TDMT_VND_VSUBTABLES_META:
×
1083
      return vnodeGetVSubtablesMeta(pVnode, pMsg);
×
1084
    case TDMT_VND_VSTB_REF_DBS:
×
1085
      return vnodeGetVStbRefDbs(pVnode, pMsg);
×
1086
#ifdef TD_ENTERPRISE
1087
    case TDMT_VND_QUERY_COMPACT_PROGRESS:
162✔
1088
      return vnodeQueryCompactProgress(pVnode, pMsg);
162✔
1089
#endif
1090
    case TDMT_VND_QUERY_SSMIGRATE_PROGRESS:
×
1091
      return vnodeQuerySsMigrateProgress(pVnode, pMsg);
×
1092
      //    case TDMT_VND_TMQ_CONSUME:
1093
      //      return tqProcessPollReq(pVnode->pTq, pMsg);
1094
#ifdef USE_TQ
1095
    case TDMT_VND_TMQ_VG_WALINFO:
3✔
1096
      return tqProcessVgWalInfoReq(pVnode->pTq, pMsg);
3✔
1097
    case TDMT_VND_TMQ_VG_COMMITTEDINFO:
1✔
1098
      return tqProcessVgCommittedInfoReq(pVnode->pTq, pMsg);
1✔
1099
    case TDMT_VND_TMQ_SEEK:
4✔
1100
      return tqProcessSeekReq(pVnode->pTq, pMsg);
4✔
1101
#endif
1102
    default:
×
1103
      vError("unknown msg type:%d in fetch queue", pMsg->msgType);
×
1104
      return TSDB_CODE_APP_ERROR;
×
1105
  }
1106
}
1107

1108
void smaHandleRes(void *pVnode, int64_t smaId, const SArray *data) {
×
1109
  int32_t code = tdProcessTSmaInsert(((SVnode *)pVnode)->pSma, smaId, (const char *)data);
×
1110
  if (code) {
×
1111
    vError("failed to process sma result since %s", tstrerror(code));
×
1112
  }
1113
}
×
1114

1115
void vnodeUpdateMetaRsp(SVnode *pVnode, STableMetaRsp *pMetaRsp) {
157,163✔
1116
  if (NULL == pMetaRsp) {
157,163!
1117
    return;
×
1118
  }
1119

1120
  tstrncpy(pMetaRsp->dbFName, pVnode->config.dbname, TSDB_DB_FNAME_LEN);
157,163✔
1121
  pMetaRsp->dbId = pVnode->config.dbId;
157,163✔
1122
  pMetaRsp->vgId = TD_VID(pVnode);
157,163✔
1123
  pMetaRsp->precision = pVnode->config.tsdbCfg.precision;
157,163✔
1124
}
1125

1126
extern int32_t vnodeAsyncRetention(SVnode *pVnode, int64_t now);
1127

1128
static int32_t vnodeProcessTrimReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
63✔
1129
  if (!pVnode->restored) {
63✔
1130
    vInfo("vgId:%d, ignore trim req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
22!
1131
    return 0;
22✔
1132
  }
1133

1134
  int32_t     code = 0;
41✔
1135
  SVTrimDbReq trimReq = {0};
41✔
1136

1137
  // decode
1138
  if (tDeserializeSVTrimDbReq(pReq, len, &trimReq) != 0) {
41!
1139
    code = TSDB_CODE_INVALID_MSG;
×
1140
    goto _exit;
×
1141
  }
1142

1143
  vInfo("vgId:%d, process trim vnode request, time:%d", pVnode->config.vgId, trimReq.timestamp);
41!
1144

1145
  code = vnodeAsyncRetention(pVnode, trimReq.timestamp);
41✔
1146

1147
_exit:
41✔
1148
  return code;
41✔
1149
}
1150

1151
extern int32_t vnodeAsyncSsMigrate(SVnode *pVnode, SSsMigrateVgroupReq *pReq);
1152

1153
static int32_t vnodeProcessSsMigrateReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1154
  int32_t             code = 0;
×
1155
  SSsMigrateVgroupReq req = {0};
×
1156
  SSsMigrateVgroupRsp rsp = {0};
×
1157
  pRsp->msgType = TDMT_VND_SSMIGRATE_RSP;
×
1158
  pRsp->code = 0;
×
1159
  pRsp->pCont = NULL;
×
1160
  pRsp->contLen = 0;
×
1161

1162
  if (tDeserializeSSsMigrateVgroupReq(pReq, len, &req) != 0) {
×
1163
    code = TSDB_CODE_INVALID_MSG;
×
1164
    goto _exit;
×
1165
  }
1166

1167
  vInfo("vgId:%d, process ssmigrate vnode request, time:%" PRId64, pVnode->config.vgId, req.timestamp);
×
1168

1169
  code = vnodeAsyncSsMigrate(pVnode, &req);
×
1170
  if (code != TSDB_CODE_SUCCESS) {
×
1171
    vError("vgId:%d, failed to async ssmigrate since %s", TD_VID(pVnode), tstrerror(code));
×
1172
    pRsp->code = code;
×
1173
    goto _exit;
×
1174
  }
1175

1176
  rsp.ssMigrateId = req.ssMigrateId;
×
1177
  rsp.vgId = TD_VID(pVnode);
×
1178
  rsp.nodeId = req.nodeId;
×
1179

1180
  pRsp->code = TSDB_CODE_SUCCESS;
×
1181
  pRsp->contLen = tSerializeSSsMigrateVgroupRsp(NULL, 0, &rsp);
×
1182
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
×
1183
  if (pRsp->pCont == NULL) {
×
1184
    vError("vgId:%d, failed to allocate memory for ssmigrate response", TD_VID(pVnode));
×
1185
    code = TSDB_CODE_OUT_OF_MEMORY;
×
1186
    goto _exit;
×
1187
  }
1188

1189
  if (tSerializeSSsMigrateVgroupRsp(pRsp->pCont, pRsp->contLen, &rsp) < 0) {
×
1190
    vError("vgId:%d, failed to serialize ssmigrate response", TD_VID(pVnode));
×
1191
    code = TSDB_CODE_INVALID_MSG;
×
1192
  }
1193

1194
_exit:
×
1195
  if (code != TSDB_CODE_SUCCESS) {
×
1196
    pRsp->code = code;
×
1197
  }
1198
  return code;
×
1199
}
1200

1201
extern int32_t vnodeFollowerSsMigrate(SVnode *pVnode, SFollowerSsMigrateReq *pReq);
1202

1203
static int32_t vnodeProcessFollowerSsMigrateReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1204
  int32_t               code = 0;
×
1205
  SFollowerSsMigrateReq req = {0};
×
1206

1207
  // decode
1208
  if (tDeserializeSFollowerSsMigrateReq(pReq, len, &req) != 0) {
×
1209
    code = TSDB_CODE_INVALID_MSG;
×
1210
    goto _exit;
×
1211
  }
1212

1213
  code = vnodeFollowerSsMigrate(pVnode, &req);
×
1214

1215
_exit:
×
1216
  tFreeSFollowerSsMigrateReq(&req);
×
1217
  return code;
×
1218
}
1219

1220
static int32_t vnodeProcessDropTtlTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1221
  int               ret = 0;
×
1222
  SVDropTtlTableReq ttlReq = {0};
×
1223
  if (tDeserializeSVDropTtlTableReq(pReq, len, &ttlReq) != 0) {
×
1224
    ret = TSDB_CODE_INVALID_MSG;
×
1225
    goto end;
×
1226
  }
1227

1228
  if (ttlReq.nUids != taosArrayGetSize(ttlReq.pTbUids)) {
×
1229
    ret = TSDB_CODE_INVALID_MSG;
×
1230
    goto end;
×
1231
  }
1232

1233
  if (ttlReq.nUids != 0) {
×
1234
    vInfo("vgId:%d, process drop ttl table request, time:%d, ntbUids:%d", pVnode->config.vgId, ttlReq.timestampSec,
×
1235
          ttlReq.nUids);
1236
  }
1237

1238
  if (ttlReq.nUids > 0) {
×
1239
    int32_t code = metaDropMultipleTables(pVnode->pMeta, ver, ttlReq.pTbUids);
×
1240
    if (code) return code;
×
1241

1242
    code = tqUpdateTbUidList(pVnode->pTq, ttlReq.pTbUids, false);
×
1243
    if (code) {
×
1244
      vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1245
    }
1246
  }
1247

1248
end:
×
1249
  taosArrayDestroy(ttlReq.pTbUids);
×
1250
  return ret;
×
1251
}
1252

1253
static int32_t vnodeProcessFetchTtlExpiredTbs(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
33✔
1254
  int32_t                 code = -1;
33✔
1255
  SMetaReader             mr = {0};
33✔
1256
  SVDropTtlTableReq       ttlReq = {0};
33✔
1257
  SVFetchTtlExpiredTbsRsp rsp = {0};
33✔
1258
  SEncoder                encoder = {0};
33✔
1259
  SArray                 *pNames = NULL;
33✔
1260
  pRsp->msgType = TDMT_VND_FETCH_TTL_EXPIRED_TBS_RSP;
33✔
1261
  pRsp->code = TSDB_CODE_SUCCESS;
33✔
1262
  pRsp->pCont = NULL;
33✔
1263
  pRsp->contLen = 0;
33✔
1264

1265
  if (tDeserializeSVDropTtlTableReq(pReq, len, &ttlReq) != 0) {
33!
1266
    terrno = TSDB_CODE_INVALID_MSG;
×
1267
    goto _end;
×
1268
  }
1269

1270
  if (!(ttlReq.nUids == taosArrayGetSize(ttlReq.pTbUids))) {
33!
1271
    terrno = TSDB_CODE_INVALID_MSG;
×
1272
    goto _end;
×
1273
  }
1274

1275
  tb_uid_t    suid;
1276
  char        ctbName[TSDB_TABLE_NAME_LEN];
1277
  SVDropTbReq expiredTb = {.igNotExists = true};
33✔
1278
  metaReaderDoInit(&mr, pVnode->pMeta, 0);
33✔
1279
  rsp.vgId = TD_VID(pVnode);
33✔
1280
  rsp.pExpiredTbs = taosArrayInit(ttlReq.nUids, sizeof(SVDropTbReq));
33✔
1281
  if (!rsp.pExpiredTbs) goto _end;
33!
1282

1283
  pNames = taosArrayInit(ttlReq.nUids, TSDB_TABLE_NAME_LEN);
33✔
1284
  if (!pNames) {
33!
1285
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1286
    goto _end;
×
1287
  }
1288
  char buf[TSDB_TABLE_NAME_LEN];
1289
  for (int32_t i = 0; i < ttlReq.nUids; ++i) {
159✔
1290
    tb_uid_t *uid = taosArrayGet(ttlReq.pTbUids, i);
126✔
1291
    expiredTb.suid = *uid;
126✔
1292
    terrno = metaReaderGetTableEntryByUid(&mr, *uid);
126✔
1293
    if (terrno < 0) goto _end;
125!
1294
    tstrncpy(buf, mr.me.name, TSDB_TABLE_NAME_LEN);
125✔
1295
    void *p = taosArrayPush(pNames, buf);
125✔
1296
    if (p == NULL) {
125!
1297
      goto _end;
×
1298
    }
1299

1300
    expiredTb.name = p;
125✔
1301
    if (mr.me.type == TSDB_CHILD_TABLE) {
125✔
1302
      expiredTb.suid = mr.me.ctbEntry.suid;
75✔
1303
    }
1304

1305
    if (taosArrayPush(rsp.pExpiredTbs, &expiredTb) == NULL) {
251!
1306
      goto _end;
×
1307
    }
1308
  }
1309

1310
  int32_t ret = 0;
33✔
1311
  tEncodeSize(tEncodeVFetchTtlExpiredTbsRsp, &rsp, pRsp->contLen, ret);
33!
1312
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
33✔
1313
  if (pRsp->pCont == NULL) {
33!
1314
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1315
    code = -1;
×
1316
    goto _end;
×
1317
  }
1318
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
33✔
1319
  terrno = tEncodeVFetchTtlExpiredTbsRsp(&encoder, &rsp);
33✔
1320
  tEncoderClear(&encoder);
33✔
1321

1322
  if (terrno == 0) code = 0;
33!
1323
_end:
×
1324
  metaReaderClear(&mr);
33✔
1325
  tFreeFetchTtlExpiredTbsRsp(&rsp);
33✔
1326
  taosArrayDestroy(ttlReq.pTbUids);
33✔
1327
  if (pNames) taosArrayDestroy(pNames);
33!
1328
  pRsp->code = terrno;
33✔
1329
  return code;
33✔
1330
}
1331

1332
static int32_t vnodeProcessCreateStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
25,597✔
1333
  int32_t        code = 0;
25,597✔
1334
  SVCreateStbReq req = {0};
25,597✔
1335
  SDecoder       coder;
1336

1337
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
25,597✔
1338
  pRsp->code = TSDB_CODE_SUCCESS;
25,597✔
1339
  pRsp->pCont = NULL;
25,597✔
1340
  pRsp->contLen = 0;
25,597✔
1341

1342
  // decode and process req
1343
  tDecoderInit(&coder, pReq, len);
25,597✔
1344

1345
  code = tDecodeSVCreateStbReq(&coder, &req);
25,620✔
1346
  if (code) {
25,606!
1347
    pRsp->code = code;
×
1348
    goto _err;
×
1349
  }
1350

1351
  code = metaCreateSuperTable(pVnode->pMeta, ver, &req);
25,606✔
1352
  if (code) {
25,625✔
1353
    pRsp->code = code;
3✔
1354
    goto _err;
3✔
1355
  }
1356

1357
  if ((code = tdProcessRSmaCreate(pVnode->pSma, &req)) < 0) {
25,622!
1358
    pRsp->code = code;
×
1359
    goto _err;
×
1360
  }
1361

1362
  tDecoderClear(&coder);
25,612✔
1363
  return 0;
25,596✔
1364

1365
_err:
3✔
1366
  tDecoderClear(&coder);
3✔
1367
  return code;
3✔
1368
}
1369

1370
static int32_t vnodeProcessCreateTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
76,383✔
1371
                                       SRpcMsg *pOriginRpc) {
1372
  SDecoder           decoder = {0};
76,383✔
1373
  SEncoder           encoder = {0};
76,383✔
1374
  int32_t            rcode = 0;
76,383✔
1375
  SVCreateTbBatchReq req = {0};
76,383✔
1376
  SVCreateTbReq     *pCreateReq;
1377
  SVCreateTbBatchRsp rsp = {0};
76,383✔
1378
  SVCreateTbRsp      cRsp = {0};
76,383✔
1379
  char               tbName[TSDB_TABLE_FNAME_LEN];
1380
  STbUidStore       *pStore = NULL;
76,383✔
1381
  SArray            *tbUids = NULL;
76,383✔
1382
  SArray            *tbNames = NULL;
76,383✔
1383
  pRsp->msgType = TDMT_VND_CREATE_TABLE_RSP;
76,383✔
1384
  pRsp->code = TSDB_CODE_SUCCESS;
76,383✔
1385
  pRsp->pCont = NULL;
76,383✔
1386
  pRsp->contLen = 0;
76,383✔
1387

1388
  // decode
1389
  tDecoderInit(&decoder, pReq, len);
76,383✔
1390
  if (tDecodeSVCreateTbBatchReq(&decoder, &req) < 0) {
76,383!
1391
    rcode = -1;
×
1392
    terrno = TSDB_CODE_INVALID_MSG;
×
1393
    goto _exit;
×
1394
  }
1395

1396
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(cRsp));
76,374✔
1397
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
76,382✔
1398
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
76,382✔
1399
  if (rsp.pArray == NULL || tbUids == NULL || tbNames == NULL) {
76,382!
1400
    rcode = -1;
×
1401
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1402
    goto _exit;
×
1403
  }
1404

1405
  // loop to create table
1406
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
183,710✔
1407
    pCreateReq = req.pReqs + iReq;
107,325✔
1408
    memset(&cRsp, 0, sizeof(cRsp));
107,325✔
1409

1410
    if (tsEnableAudit && tsEnableAuditCreateTable) {
107,325!
1411
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
107,323!
1412
      if (str == NULL) {
107,322!
1413
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1414
        rcode = -1;
×
1415
        goto _exit;
×
1416
      }
1417
      tstrncpy(str, pCreateReq->name, TSDB_TABLE_FNAME_LEN);
107,322✔
1418
      if (taosArrayPush(tbNames, &str) == NULL) {
107,324!
1419
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1420
        rcode = -1;
×
1421
        goto _exit;
×
1422
      }
1423
    }
1424

1425
    // validate hash
1426
    (void)tsnprintf(tbName, TSDB_TABLE_FNAME_LEN, "%s.%s", pVnode->config.dbname, pCreateReq->name);
107,326✔
1427
    if (vnodeValidateTableHash(pVnode, tbName) < 0) {
107,323!
1428
      cRsp.code = TSDB_CODE_VND_HASH_MISMATCH;
×
1429
      if (taosArrayPush(rsp.pArray, &cRsp) == NULL) {
×
1430
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1431
        rcode = -1;
×
1432
        goto _exit;
×
1433
      }
1434
      vError("vgId:%d create-table:%s failed due to hash value mismatch", TD_VID(pVnode), tbName);
×
1435
      continue;
×
1436
    }
1437

1438
    // do create table
1439
    if (metaCreateTable2(pVnode->pMeta, ver, pCreateReq, &cRsp.pMeta) < 0) {
107,322✔
1440
      if (pCreateReq->flags & TD_CREATE_IF_NOT_EXISTS && terrno == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
125!
1441
        cRsp.code = TSDB_CODE_SUCCESS;
109✔
1442
      } else {
1443
        cRsp.code = terrno;
16✔
1444
      }
1445
    } else {
1446
      cRsp.code = TSDB_CODE_SUCCESS;
107,201✔
1447
      if (tdFetchTbUidList(pVnode->pSma, &pStore, pCreateReq->ctb.suid, pCreateReq->uid) < 0) {
107,201!
1448
        vError("vgId:%d, failed to fetch tbUid list", TD_VID(pVnode));
×
1449
      }
1450
      if (taosArrayPush(tbUids, &pCreateReq->uid) == NULL) {
214,406!
1451
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1452
        rcode = -1;
×
1453
        goto _exit;
×
1454
      }
1455
      vnodeUpdateMetaRsp(pVnode, cRsp.pMeta);
107,203✔
1456
    }
1457

1458
    if (taosArrayPush(rsp.pArray, &cRsp) == NULL) {
214,655!
1459
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1460
      rcode = -1;
×
1461
      goto _exit;
×
1462
    }
1463
  }
1464

1465
  vTrace("vgId:%d, add %d new created tables into query table list", TD_VID(pVnode), (int32_t)taosArrayGetSize(tbUids));
76,385✔
1466
  if (tqUpdateTbUidList(pVnode->pTq, tbUids, true) < 0) {
76,385!
1467
    vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(terrno));
×
1468
  }
1469
  if (tdUpdateTbUidList(pVnode->pSma, pStore, true) < 0) {
76,384!
1470
    goto _exit;
×
1471
  }
1472
  pStore = tdUidStoreFree(pStore);
76,384✔
1473

1474
  // prepare rsp
1475
  int32_t ret = 0;
76,384✔
1476
  tEncodeSize(tEncodeSVCreateTbBatchRsp, &rsp, pRsp->contLen, ret);
76,384!
1477
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
76,383✔
1478
  if (pRsp->pCont == NULL) {
76,383!
1479
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1480
    rcode = -1;
×
1481
    goto _exit;
×
1482
  }
1483
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
76,383✔
1484
  if (tEncodeSVCreateTbBatchRsp(&encoder, &rsp) != 0) {
76,383!
1485
    vError("vgId:%d, failed to encode create table batch response", TD_VID(pVnode));
×
1486
  }
1487

1488
  if (tsEnableAudit && tsEnableAuditCreateTable) {
76,383!
1489
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
76,382✔
1490

1491
    SName name = {0};
76,382✔
1492
    if (tNameFromString(&name, pVnode->config.dbname, T_NAME_ACCT | T_NAME_DB) < 0) {
76,382!
1493
      vError("vgId:%d, failed to get name from string", TD_VID(pVnode));
×
1494
    }
1495

1496
    SStringBuilder sb = {0};
76,381✔
1497
    for (int32_t i = 0; i < tbNames->size; i++) {
183,701✔
1498
      char **key = (char **)taosArrayGet(tbNames, i);
107,318✔
1499
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
107,317✔
1500
      if (i < tbNames->size - 1) {
107,321✔
1501
        taosStringBuilderAppendChar(&sb, ',');
30,939✔
1502
      }
1503
      // taosMemoryFreeClear(*key);
1504
    }
1505

1506
    size_t len = 0;
76,383✔
1507
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
76,383✔
1508

1509
    if (pOriginRpc->info.conn.user != NULL && strlen(pOriginRpc->info.conn.user) > 0) {
76,383!
1510
      auditAddRecord(pOriginRpc, clusterId, "createTable", name.dbname, "", keyJoined, len);
68,802✔
1511
    }
1512

1513
    taosStringBuilderDestroy(&sb);
76,380✔
1514
  }
1515

1516
_exit:
1✔
1517
  tDeleteSVCreateTbBatchReq(&req);
76,382✔
1518
  taosArrayDestroyEx(rsp.pArray, tFreeSVCreateTbRsp);
76,383✔
1519
  taosArrayDestroy(tbUids);
76,383✔
1520
  tDecoderClear(&decoder);
76,384✔
1521
  tEncoderClear(&encoder);
76,383✔
1522
  taosArrayDestroyP(tbNames, NULL);
76,384✔
1523
  return rcode;
76,384✔
1524
}
1525

1526
static int32_t vnodeProcessAlterStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
16,084✔
1527
  int32_t        code = 0;
16,084✔
1528
  SVCreateStbReq req = {0};
16,084✔
1529
  SDecoder       dc = {0};
16,084✔
1530

1531
  pRsp->msgType = TDMT_VND_ALTER_STB_RSP;
16,084✔
1532
  pRsp->code = TSDB_CODE_SUCCESS;
16,084✔
1533
  pRsp->pCont = NULL;
16,084✔
1534
  pRsp->contLen = 0;
16,084✔
1535

1536
  tDecoderInit(&dc, pReq, len);
16,084✔
1537

1538
  // decode req
1539
  code = tDecodeSVCreateStbReq(&dc, &req);
16,084✔
1540
  if (code) {
16,084!
1541
    tDecoderClear(&dc);
×
1542
    return code;
×
1543
  }
1544

1545
  code = metaAlterSuperTable(pVnode->pMeta, ver, &req);
16,084✔
1546
  if (code) {
16,084!
1547
    pRsp->code = code;
×
1548
    tDecoderClear(&dc);
×
1549
    return code;
×
1550
  }
1551

1552
  tDecoderClear(&dc);
16,084✔
1553

1554
  return 0;
16,084✔
1555
}
1556

1557
static int32_t vnodeProcessDropStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
2,032✔
1558
  SVDropStbReq req = {0};
2,032✔
1559
  int32_t      rcode = TSDB_CODE_SUCCESS;
2,032✔
1560
  SDecoder     decoder = {0};
2,032✔
1561
  SArray      *tbUidList = NULL;
2,032✔
1562

1563
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
2,032✔
1564
  pRsp->pCont = NULL;
2,032✔
1565
  pRsp->contLen = 0;
2,032✔
1566

1567
  // decode request
1568
  tDecoderInit(&decoder, pReq, len);
2,032✔
1569
  if (tDecodeSVDropStbReq(&decoder, &req) < 0) {
2,032!
1570
    rcode = TSDB_CODE_INVALID_MSG;
×
1571
    goto _exit;
×
1572
  }
1573

1574
  // process request
1575
  tbUidList = taosArrayInit(8, sizeof(int64_t));
2,032✔
1576
  if (tbUidList == NULL) goto _exit;
2,032!
1577
  if (metaDropSuperTable(pVnode->pMeta, ver, &req) < 0) {
2,032✔
1578
    rcode = terrno;
2✔
1579
    goto _exit;
2✔
1580
  }
1581

1582
  if (tqUpdateTbUidList(pVnode->pTq, tbUidList, false) < 0) {
2,030!
1583
    rcode = terrno;
×
1584
    goto _exit;
×
1585
  }
1586

1587
  if (tdProcessRSmaDrop(pVnode->pSma, &req) < 0) {
2,030!
1588
    rcode = terrno;
×
1589
    goto _exit;
×
1590
  }
1591

1592
  // return rsp
1593
_exit:
2,030✔
1594
  if (tbUidList) taosArrayDestroy(tbUidList);
2,032!
1595
  pRsp->code = rcode;
2,032✔
1596
  tDecoderClear(&decoder);
2,032✔
1597
  return 0;
2,032✔
1598
}
1599

1600
static int32_t vnodeProcessAlterTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
19,681✔
1601
  SVAlterTbReq  vAlterTbReq = {0};
19,681✔
1602
  SVAlterTbRsp  vAlterTbRsp = {0};
19,681✔
1603
  SDecoder      dc = {0};
19,681✔
1604
  int32_t       code = 0;
19,681✔
1605
  int32_t       lino = 0;
19,681✔
1606
  int32_t       ret;
1607
  SEncoder      ec = {0};
19,681✔
1608
  STableMetaRsp vMetaRsp = {0};
19,681✔
1609

1610
  pRsp->msgType = TDMT_VND_ALTER_TABLE_RSP;
19,681✔
1611
  pRsp->pCont = NULL;
19,681✔
1612
  pRsp->contLen = 0;
19,681✔
1613
  pRsp->code = TSDB_CODE_SUCCESS;
19,681✔
1614

1615
  tDecoderInit(&dc, pReq, len);
19,681✔
1616

1617
  // decode
1618
  if (tDecodeSVAlterTbReq(&dc, &vAlterTbReq) < 0) {
19,681!
1619
    vAlterTbRsp.code = TSDB_CODE_INVALID_MSG;
×
1620
    tDecoderClear(&dc);
×
1621
    goto _exit;
×
1622
  }
1623

1624
  // process
1625
  if (metaAlterTable(pVnode->pMeta, ver, &vAlterTbReq, &vMetaRsp) < 0) {
19,681✔
1626
    vAlterTbRsp.code = terrno;
920✔
1627
    tDecoderClear(&dc);
920✔
1628
    goto _exit;
920✔
1629
  }
1630
  tDecoderClear(&dc);
18,761✔
1631

1632
  if (NULL != vMetaRsp.pSchemas) {
18,761✔
1633
    vnodeUpdateMetaRsp(pVnode, &vMetaRsp);
5,495✔
1634
    vAlterTbRsp.pMeta = &vMetaRsp;
5,495✔
1635
  }
1636

1637
  if (vAlterTbReq.action == TSDB_ALTER_TABLE_UPDATE_TAG_VAL ||
18,761✔
1638
      vAlterTbReq.action == TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL) {
5,615✔
1639
    int64_t uid = metaGetTableEntryUidByName(pVnode->pMeta, vAlterTbReq.tbName);
13,151✔
1640
    if (uid == 0) {
13,151!
1641
      vError("vgId:%d, %s failed at %s:%d since table %s not found", TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
1642
             vAlterTbReq.tbName);
1643
      goto _exit;
×
1644
    }
1645

1646
    SArray *tbUids = taosArrayInit(4, sizeof(int64_t));
13,151✔
1647
    void   *p = taosArrayPush(tbUids, &uid);
13,151✔
1648
    TSDB_CHECK_NULL(p, code, lino, _exit, terrno);
13,151!
1649

1650
    vDebug("vgId:%d, remove tags value altered table:%s from query table list", TD_VID(pVnode), vAlterTbReq.tbName);
13,151!
1651
    if ((code = tqUpdateTbUidList(pVnode->pTq, tbUids, false)) < 0) {
13,151!
1652
      vError("vgId:%d, failed to remove tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1653
    }
1654

1655
    vDebug("vgId:%d, try to add table:%s in query table list", TD_VID(pVnode), vAlterTbReq.tbName);
13,151!
1656
    if ((code = tqUpdateTbUidList(pVnode->pTq, tbUids, true)) < 0) {
13,151!
1657
      vError("vgId:%d, failed to add tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1658
    }
1659

1660
    taosArrayDestroy(tbUids);
13,151✔
1661
  }
1662

1663
_exit:
5,610✔
1664
  taosArrayDestroy(vAlterTbReq.pMultiTag);
19,681✔
1665
  tEncodeSize(tEncodeSVAlterTbRsp, &vAlterTbRsp, pRsp->contLen, ret);
19,681!
1666
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
19,681✔
1667
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
19,681✔
1668
  if (tEncodeSVAlterTbRsp(&ec, &vAlterTbRsp) != 0) {
19,681!
1669
    vError("vgId:%d, failed to encode alter table response", TD_VID(pVnode));
×
1670
  }
1671

1672
  tEncoderClear(&ec);
19,681✔
1673
  if (vMetaRsp.pSchemas) {
19,681✔
1674
    taosMemoryFree(vMetaRsp.pSchemas);
5,495!
1675
    taosMemoryFree(vMetaRsp.pSchemaExt);
5,495!
1676
  }
1677
  if (vMetaRsp.pColRefs) {
19,681✔
1678
    taosMemoryFree(vMetaRsp.pColRefs);
330!
1679
  }
1680
  return 0;
19,681✔
1681
}
1682

1683
static int32_t vnodeProcessDropTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
15,712✔
1684
                                     SRpcMsg *pOriginRpc) {
1685
  SVDropTbBatchReq req = {0};
15,712✔
1686
  SVDropTbBatchRsp rsp = {0};
15,712✔
1687
  SDecoder         decoder = {0};
15,712✔
1688
  SEncoder         encoder = {0};
15,712✔
1689
  int32_t          ret;
1690
  SArray          *tbUids = NULL;
15,712✔
1691
  STbUidStore     *pStore = NULL;
15,712✔
1692
  SArray          *tbNames = NULL;
15,712✔
1693

1694
  pRsp->msgType = TDMT_VND_DROP_TABLE_RSP;
15,712✔
1695
  pRsp->pCont = NULL;
15,712✔
1696
  pRsp->contLen = 0;
15,712✔
1697
  pRsp->code = TSDB_CODE_SUCCESS;
15,712✔
1698

1699
  // decode req
1700
  tDecoderInit(&decoder, pReq, len);
15,712✔
1701
  ret = tDecodeSVDropTbBatchReq(&decoder, &req);
15,712✔
1702
  if (ret < 0) {
15,712!
1703
    terrno = TSDB_CODE_INVALID_MSG;
×
1704
    pRsp->code = terrno;
×
1705
    goto _exit;
×
1706
  }
1707

1708
  // process req
1709
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
15,712✔
1710
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(SVDropTbRsp));
15,712✔
1711
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
15,712✔
1712
  if (tbUids == NULL || rsp.pArray == NULL || tbNames == NULL) goto _exit;
15,712!
1713

1714
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
31,827✔
1715
    SVDropTbReq *pDropTbReq = req.pReqs + iReq;
16,115✔
1716
    SVDropTbRsp  dropTbRsp = {0};
16,115✔
1717
    tb_uid_t     tbUid = 0;
16,115✔
1718

1719
    /* code */
1720
    ret = metaDropTable2(pVnode->pMeta, ver, pDropTbReq);
16,115✔
1721
    if (ret < 0) {
16,115!
1722
      if (pDropTbReq->igNotExists && terrno == TSDB_CODE_TDB_TABLE_NOT_EXIST) {
×
1723
        dropTbRsp.code = TSDB_CODE_SUCCESS;
×
1724
      } else {
1725
        dropTbRsp.code = terrno;
×
1726
      }
1727
    } else {
1728
      dropTbRsp.code = TSDB_CODE_SUCCESS;
16,115✔
1729
      if (tbUid > 0) {
16,115!
1730
        if (tdFetchTbUidList(pVnode->pSma, &pStore, pDropTbReq->suid, tbUid) < 0) {
×
1731
          vError("vgId:%d, failed to fetch tbUid list", TD_VID(pVnode));
×
1732
        }
1733
      }
1734
    }
1735

1736
    if (taosArrayPush(rsp.pArray, &dropTbRsp) == NULL) {
32,230!
1737
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1738
      pRsp->code = terrno;
×
1739
      goto _exit;
×
1740
    }
1741

1742
    if (tsEnableAuditCreateTable) {
16,115!
1743
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
16,115!
1744
      if (str == NULL) {
16,115!
1745
        pRsp->code = terrno;
×
1746
        goto _exit;
×
1747
      }
1748
      tstrncpy(str, pDropTbReq->name, TSDB_TABLE_FNAME_LEN);
16,115✔
1749
      if (taosArrayPush(tbNames, &str) == NULL) {
16,115!
1750
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1751
        pRsp->code = terrno;
×
1752
        goto _exit;
×
1753
      }
1754
    }
1755
  }
1756

1757
  if (tqUpdateTbUidList(pVnode->pTq, tbUids, false) < 0) {
15,712!
1758
    vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(terrno));
×
1759
  }
1760

1761
  if (tdUpdateTbUidList(pVnode->pSma, pStore, false) < 0) {
15,712!
1762
    goto _exit;
×
1763
  }
1764

1765
  if (tsEnableAuditCreateTable) {
15,712!
1766
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
15,712✔
1767

1768
    SName name = {0};
15,712✔
1769
    if (tNameFromString(&name, pVnode->config.dbname, T_NAME_ACCT | T_NAME_DB) != 0) {
15,712!
1770
      vError("vgId:%d, failed to get name from string", TD_VID(pVnode));
×
1771
    }
1772

1773
    SStringBuilder sb = {0};
15,712✔
1774
    for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
31,827✔
1775
      char **key = (char **)taosArrayGet(tbNames, iReq);
16,115✔
1776
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
16,115✔
1777
      if (iReq < req.nReqs - 1) {
16,115✔
1778
        taosStringBuilderAppendChar(&sb, ',');
403✔
1779
      }
1780
      taosMemoryFreeClear(*key);
16,115!
1781
    }
1782

1783
    size_t len = 0;
15,712✔
1784
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
15,712✔
1785

1786
    if (pOriginRpc->info.conn.user != NULL && strlen(pOriginRpc->info.conn.user) > 0) {
15,712!
1787
      auditAddRecord(pOriginRpc, clusterId, "dropTable", name.dbname, "", keyJoined, len);
15,711✔
1788
    }
1789

1790
    taosStringBuilderDestroy(&sb);
15,712✔
1791
  }
1792

1793
_exit:
×
1794
  taosArrayDestroy(tbUids);
15,712✔
1795
  pStore = tdUidStoreFree(pStore);
15,712✔
1796
  tDecoderClear(&decoder);
15,712✔
1797
  tEncodeSize(tEncodeSVDropTbBatchRsp, &rsp, pRsp->contLen, ret);
15,712!
1798
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
15,712✔
1799
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
15,712✔
1800
  if (tEncodeSVDropTbBatchRsp(&encoder, &rsp) != 0) {
15,712!
1801
    vError("vgId:%d, failed to encode drop table batch response", TD_VID(pVnode));
×
1802
  }
1803
  tEncoderClear(&encoder);
15,712✔
1804
  taosArrayDestroy(rsp.pArray);
15,712✔
1805
  taosArrayDestroy(tbNames);
15,712✔
1806
  return 0;
15,712✔
1807
}
1808

1809
#ifdef BUILD_NO_CALL
1810
static int32_t vnodeDebugPrintSingleSubmitMsg(SMeta *pMeta, SSubmitBlk *pBlock, SSubmitMsgIter *msgIter,
1811
                                              const char *tags) {
1812
  SSubmitBlkIter blkIter = {0};
1813
  STSchema      *pSchema = NULL;
1814
  tb_uid_t       suid = 0;
1815
  STSRow        *row = NULL;
1816
  int32_t        rv = -1;
1817

1818
  tInitSubmitBlkIter(msgIter, pBlock, &blkIter);
1819
  if (blkIter.row == NULL) return 0;
1820

1821
  int32_t code = metaGetTbTSchemaNotNull(pMeta, msgIter->suid, TD_ROW_SVER(blkIter.row), 1,
1822
                                         &pSchema);  // TODO: use the real schema
1823
  if (TSDB_CODE_SUCCESS != code) {
1824
    printf("%s:%d no valid schema\n", tags, __LINE__);
1825
    return code;
1826
  }
1827

1828
  suid = msgIter->suid;
1829
  rv = TD_ROW_SVER(blkIter.row);
1830

1831
  char __tags[128] = {0};
1832
  snprintf(__tags, 128, "%s: uid %" PRIi64 " ", tags, msgIter->uid);
1833
  while ((row = tGetSubmitBlkNext(&blkIter))) {
1834
    tdSRowPrint(row, pSchema, __tags);
1835
  }
1836

1837
  taosMemoryFreeClear(pSchema);
1838

1839
  return TSDB_CODE_SUCCESS;
1840
}
1841
#endif
1842
typedef struct SSubmitReqConvertCxt {
1843
  SSubmitMsgIter msgIter;
1844
  SSubmitBlk    *pBlock;
1845
  SSubmitBlkIter blkIter;
1846
  STSRow        *pRow;
1847
  STSRowIter     rowIter;
1848
  SSubmitTbData *pTbData;
1849
  STSchema      *pTbSchema;
1850
  SArray        *pColValues;
1851
} SSubmitReqConvertCxt;
1852

1853
static int32_t vnodeResetTableCxt(SMeta *pMeta, SSubmitReqConvertCxt *pCxt) {
×
1854
  taosMemoryFreeClear(pCxt->pTbSchema);
×
1855
  int32_t code = metaGetTbTSchemaNotNull(pMeta, pCxt->msgIter.suid > 0 ? pCxt->msgIter.suid : pCxt->msgIter.uid,
×
1856
                                         pCxt->msgIter.sversion, 1, &pCxt->pTbSchema);
1857
  if (TSDB_CODE_SUCCESS != code) {
×
1858
    return code;
×
1859
  }
1860
  tdSTSRowIterInit(&pCxt->rowIter, pCxt->pTbSchema);
×
1861

1862
  tDestroySubmitTbData(pCxt->pTbData, TSDB_MSG_FLG_ENCODE);
×
1863
  if (NULL == pCxt->pTbData) {
×
1864
    pCxt->pTbData = taosMemoryCalloc(1, sizeof(SSubmitTbData));
×
1865
    if (NULL == pCxt->pTbData) {
×
1866
      return terrno;
×
1867
    }
1868
  }
1869
  pCxt->pTbData->flags = 0;
×
1870
  pCxt->pTbData->suid = pCxt->msgIter.suid;
×
1871
  pCxt->pTbData->uid = pCxt->msgIter.uid;
×
1872
  pCxt->pTbData->sver = pCxt->msgIter.sversion;
×
1873
  pCxt->pTbData->pCreateTbReq = NULL;
×
1874
  pCxt->pTbData->aRowP = taosArrayInit(128, POINTER_BYTES);
×
1875
  if (NULL == pCxt->pTbData->aRowP) {
×
1876
    return terrno;
×
1877
  }
1878

1879
  taosArrayDestroy(pCxt->pColValues);
×
1880
  pCxt->pColValues = taosArrayInit(pCxt->pTbSchema->numOfCols, sizeof(SColVal));
×
1881
  if (NULL == pCxt->pColValues) {
×
1882
    return terrno;
×
1883
  }
1884
  for (int32_t i = 0; i < pCxt->pTbSchema->numOfCols; ++i) {
×
1885
    SColVal val = COL_VAL_NONE(pCxt->pTbSchema->columns[i].colId, pCxt->pTbSchema->columns[i].type);
×
1886
    if (taosArrayPush(pCxt->pColValues, &val) == NULL) {
×
1887
      return terrno;
×
1888
    }
1889
  }
1890

1891
  return TSDB_CODE_SUCCESS;
×
1892
}
1893

1894
static void vnodeDestroySubmitReqConvertCxt(SSubmitReqConvertCxt *pCxt) {
×
1895
  taosMemoryFreeClear(pCxt->pTbSchema);
×
1896
  tDestroySubmitTbData(pCxt->pTbData, TSDB_MSG_FLG_ENCODE);
×
1897
  taosMemoryFreeClear(pCxt->pTbData);
×
1898
  taosArrayDestroy(pCxt->pColValues);
×
1899
}
×
1900

1901
static int32_t vnodeCellValConvertToColVal(STColumn *pCol, SCellVal *pCellVal, SColVal *pColVal) {
×
1902
  if (tdValTypeIsNone(pCellVal->valType)) {
×
1903
    pColVal->flag = CV_FLAG_NONE;
×
1904
    return TSDB_CODE_SUCCESS;
×
1905
  }
1906

1907
  if (tdValTypeIsNull(pCellVal->valType)) {
×
1908
    pColVal->flag = CV_FLAG_NULL;
×
1909
    return TSDB_CODE_SUCCESS;
×
1910
  }
1911

1912
  if (IS_VAR_DATA_TYPE(pCol->type)) {
×
1913
    if (IS_STR_DATA_BLOB(pCol->type)) {
×
1914
      pColVal->value.nData = blobDataLen(pCellVal->val);
×
1915
      pColVal->value.pData = (uint8_t *)blobDataVal(pCellVal->val);
×
1916

1917
    } else {
1918
      pColVal->value.nData = varDataLen(pCellVal->val);
×
1919
      pColVal->value.pData = (uint8_t *)varDataVal(pCellVal->val);
×
1920
    }
1921
  } else if (TSDB_DATA_TYPE_FLOAT == pCol->type) {
×
1922
    float f = GET_FLOAT_VAL(pCellVal->val);
×
1923
    valueSetDatum(&pColVal->value, pCol->type, &f, sizeof(f));
×
1924
  } else if (TSDB_DATA_TYPE_DOUBLE == pCol->type) {
×
1925
    taosSetPInt64Aligned(&pColVal->value.val, (int64_t *)pCellVal->val);
×
1926
  } else {
1927
    valueSetDatum(&pColVal->value, pCol->type, pCellVal->val, tDataTypes[pCol->type].bytes);
×
1928
  }
1929

1930
  pColVal->flag = CV_FLAG_VALUE;
×
1931
  return TSDB_CODE_SUCCESS;
×
1932
}
1933

1934
static int32_t vnodeTSRowConvertToColValArray(SSubmitReqConvertCxt *pCxt) {
×
1935
  int32_t code = TSDB_CODE_SUCCESS;
×
1936
  tdSTSRowIterReset(&pCxt->rowIter, pCxt->pRow);
×
1937
  for (int32_t i = 0; TSDB_CODE_SUCCESS == code && i < pCxt->pTbSchema->numOfCols; ++i) {
×
1938
    STColumn *pCol = pCxt->pTbSchema->columns + i;
×
1939
    SCellVal  cellVal = {0};
×
1940
    if (!tdSTSRowIterFetch(&pCxt->rowIter, pCol->colId, pCol->type, &cellVal)) {
×
1941
      break;
×
1942
    }
1943
    code = vnodeCellValConvertToColVal(pCol, &cellVal, (SColVal *)taosArrayGet(pCxt->pColValues, i));
×
1944
  }
1945
  return code;
×
1946
}
1947

1948
static int32_t vnodeDecodeCreateTbReq(SSubmitReqConvertCxt *pCxt) {
×
1949
  if (pCxt->msgIter.schemaLen <= 0) {
×
1950
    return TSDB_CODE_SUCCESS;
×
1951
  }
1952

1953
  pCxt->pTbData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
×
1954
  if (NULL == pCxt->pTbData->pCreateTbReq) {
×
1955
    return terrno;
×
1956
  }
1957

1958
  SDecoder decoder = {0};
×
1959
  tDecoderInit(&decoder, (uint8_t *)pCxt->pBlock->data, pCxt->msgIter.schemaLen);
×
1960
  int32_t code = tDecodeSVCreateTbReq(&decoder, pCxt->pTbData->pCreateTbReq);
×
1961
  tDecoderClear(&decoder);
×
1962

1963
  return code;
×
1964
}
1965

1966
static int32_t vnodeSubmitReqConvertToSubmitReq2(SVnode *pVnode, SSubmitReq *pReq, SSubmitReq2 *pReq2) {
×
1967
  pReq2->aSubmitTbData = taosArrayInit(128, sizeof(SSubmitTbData));
×
1968
  if (NULL == pReq2->aSubmitTbData) {
×
1969
    return terrno;
×
1970
  }
1971

1972
  SSubmitReqConvertCxt cxt = {0};
×
1973

1974
  int32_t code = tInitSubmitMsgIter(pReq, &cxt.msgIter);
×
1975
  while (TSDB_CODE_SUCCESS == code) {
×
1976
    code = tGetSubmitMsgNext(&cxt.msgIter, &cxt.pBlock);
×
1977
    if (TSDB_CODE_SUCCESS == code) {
×
1978
      if (NULL == cxt.pBlock) {
×
1979
        break;
×
1980
      }
1981
      code = vnodeResetTableCxt(pVnode->pMeta, &cxt);
×
1982
    }
1983
    if (TSDB_CODE_SUCCESS == code) {
×
1984
      code = tInitSubmitBlkIter(&cxt.msgIter, cxt.pBlock, &cxt.blkIter);
×
1985
    }
1986
    if (TSDB_CODE_SUCCESS == code) {
×
1987
      code = vnodeDecodeCreateTbReq(&cxt);
×
1988
    }
1989
    while (TSDB_CODE_SUCCESS == code && (cxt.pRow = tGetSubmitBlkNext(&cxt.blkIter)) != NULL) {
×
1990
      code = vnodeTSRowConvertToColValArray(&cxt);
×
1991
      if (TSDB_CODE_SUCCESS == code) {
×
1992
        SRow **pNewRow = taosArrayReserve(cxt.pTbData->aRowP, 1);
×
1993

1994
        SRowBuildScanInfo sinfo = {0};
×
1995
        code = tRowBuild(cxt.pColValues, cxt.pTbSchema, pNewRow, &sinfo);
×
1996
      }
1997
    }
1998
    if (TSDB_CODE_SUCCESS == code) {
×
1999
      code = (NULL == taosArrayPush(pReq2->aSubmitTbData, cxt.pTbData) ? terrno : TSDB_CODE_SUCCESS);
×
2000
    }
2001
    if (TSDB_CODE_SUCCESS == code) {
×
2002
      taosMemoryFreeClear(cxt.pTbData);
×
2003
    }
2004
  }
2005

2006
  vnodeDestroySubmitReqConvertCxt(&cxt);
×
2007
  return code;
×
2008
}
2009

2010
static int32_t vnodeRebuildSubmitReqMsg(SSubmitReq2 *pSubmitReq, void **ppMsg) {
×
2011
  int32_t  code = TSDB_CODE_SUCCESS;
×
2012
  char    *pMsg = NULL;
×
2013
  uint32_t msglen = 0;
×
2014
  tEncodeSize(tEncodeSubmitReq, pSubmitReq, msglen, code);
×
2015
  if (TSDB_CODE_SUCCESS == code) {
×
2016
    pMsg = taosMemoryMalloc(msglen);
×
2017
    if (NULL == pMsg) {
×
2018
      code = terrno;
×
2019
    }
2020
  }
2021
  if (TSDB_CODE_SUCCESS == code) {
×
2022
    SEncoder encoder;
2023
    tEncoderInit(&encoder, (uint8_t *)pMsg, msglen);
×
2024
    code = tEncodeSubmitReq(&encoder, pSubmitReq);
×
2025
    tEncoderClear(&encoder);
×
2026
  }
2027
  if (TSDB_CODE_SUCCESS == code) {
×
2028
    *ppMsg = pMsg;
×
2029
  }
2030
  return code;
×
2031
}
2032

2033
static int32_t buildExistSubTalbeRsp(SVnode *pVnode, SSubmitTbData *pSubmitTbData, STableMetaRsp **ppRsp) {
236✔
2034
  int32_t code = 0;
236✔
2035
  int32_t lino = 0;
236✔
2036

2037
  SMetaEntry *pEntry = NULL;
236✔
2038
  code = metaFetchEntryByUid(pVnode->pMeta, pSubmitTbData->suid, &pEntry);
236✔
2039
  if (code) {
236!
2040
    vError("vgId:%d, table uid:%" PRId64 " not exists, line:%d", TD_VID(pVnode), pSubmitTbData->uid, __LINE__);
×
2041
    TSDB_CHECK_CODE(code, lino, _exit);
×
2042
  }
2043
  if (pEntry->type != TSDB_SUPER_TABLE) {
236!
2044
    vError("vgId:%d, table uid:%" PRId64 " exists, but is not super table, line:%d", TD_VID(pVnode), pSubmitTbData->uid,
×
2045
           __LINE__);
2046
    code = TSDB_CODE_STREAM_INSERT_SCHEMA_NOT_MATCH;
×
2047
    TSDB_CHECK_CODE(code, lino, _exit);
×
2048
  }
2049

2050
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
236!
2051
  if (NULL == *ppRsp) {
236!
2052
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2053
  }
2054
  (*ppRsp)->suid = pSubmitTbData->suid;
236✔
2055
  (*ppRsp)->tuid = pSubmitTbData->uid;
236✔
2056
  (*ppRsp)->sversion = pEntry->stbEntry.schemaRow.version;
236✔
2057
  (*ppRsp)->vgId = pVnode->config.vgId;
236✔
2058
  (*ppRsp)->numOfColumns = pEntry->stbEntry.schemaRow.nCols;
236✔
2059
  (*ppRsp)->numOfTags = pEntry->stbEntry.schemaTag.nCols;
236✔
2060
  (*ppRsp)->pSchemas =
472✔
2061
      taosMemoryCalloc(pEntry->stbEntry.schemaRow.nCols + pEntry->stbEntry.schemaTag.nCols, sizeof(SSchema));
236!
2062
  if (NULL == (*ppRsp)->pSchemas) {
236!
2063
    taosMemoryFree(*ppRsp);
×
2064
    *ppRsp = NULL;
×
2065
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2066
  }
2067
  memcpy((*ppRsp)->pSchemas, pEntry->stbEntry.schemaRow.pSchema, pEntry->stbEntry.schemaRow.nCols * sizeof(SSchema));
236✔
2068
  memcpy((*ppRsp)->pSchemas + pEntry->stbEntry.schemaRow.nCols, pEntry->stbEntry.schemaTag.pSchema,
236✔
2069
         pEntry->stbEntry.schemaTag.nCols * sizeof(SSchema));
236✔
2070
  if (pEntry->pExtSchemas != NULL) {
236!
2071
    (*ppRsp)->pSchemaExt = taosMemoryCalloc(pEntry->colCmpr.nCols, sizeof(SExtSchema));
×
2072
    if (NULL == (*ppRsp)->pSchemaExt) {
×
2073
      taosMemoryFree((*ppRsp)->pSchemas);
×
2074
      taosMemoryFree(*ppRsp);
×
2075
      *ppRsp = NULL;
×
2076
      TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2077
    }
2078
    memcpy((*ppRsp)->pSchemaExt, pEntry->pExtSchemas, pEntry->colCmpr.nCols * sizeof(SExtSchema));
×
2079
  }
2080

2081
  if (pEntry->stbEntry.schemaRow.version != pSubmitTbData->sver) {
236✔
2082
    TSDB_CHECK_CODE(code = TSDB_CODE_TDB_TABLE_ALREADY_EXIST, lino, _exit);
2✔
2083
  }
2084
_exit:
234✔
2085
  metaFetchEntryFree(&pEntry);
236✔
2086
  if (code != TSDB_CODE_SUCCESS) {
236✔
2087
    vError("vgId:%d, failed to build exist sub table response, code:0x%0x, line:%d", TD_VID(pVnode), code, lino);
2!
2088
  }
2089
  return code;
236✔
2090
}
2091

2092
static int32_t buildExistNormalTalbeRsp(SVnode *pVnode, int64_t uid, STableMetaRsp **ppRsp) {
92✔
2093
  int32_t code = 0;
92✔
2094
  int32_t lino = 0;
92✔
2095

2096
  SMetaEntry *pEntry = NULL;
92✔
2097
  code = metaFetchEntryByUid(pVnode->pMeta, uid, &pEntry);
92✔
2098
  if (code) {
92!
2099
    vError("vgId:%d, table uid:%" PRId64 " not exists, line:%d", TD_VID(pVnode), uid, __LINE__);
×
2100
    TSDB_CHECK_CODE(code, lino, _exit);
×
2101
  }
2102
  if (pEntry->type != TSDB_NORMAL_TABLE) {
92!
2103
    vError("vgId:%d, table uid:%" PRId64 " exists, but is not normal table, line:%d", TD_VID(pVnode),
×
2104
           uid, __LINE__);
2105
    code = TSDB_CODE_STREAM_INSERT_SCHEMA_NOT_MATCH;
×
2106
    TSDB_CHECK_CODE(code, lino, _exit);
×
2107
  }
2108

2109
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
92!
2110
  if (NULL == *ppRsp) {
92!
2111
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2112
  }
2113

2114
  (*ppRsp)->tuid = pEntry->uid;
92✔
2115
  (*ppRsp)->vgId = pVnode->config.vgId;
92✔
2116
  (*ppRsp)->sversion = pEntry->ntbEntry.schemaRow.version;
92✔
2117
  (*ppRsp)->numOfColumns = pEntry->ntbEntry.schemaRow.nCols;
92✔
2118
  (*ppRsp)->pSchemas = taosMemoryCalloc(pEntry->ntbEntry.schemaRow.nCols, sizeof(SSchema));
92!
2119
  if (NULL == (*ppRsp)->pSchemas) {
92!
2120
    taosMemoryFree(*ppRsp);
×
2121
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2122
  }
2123
  memcpy((*ppRsp)->pSchemas, pEntry->ntbEntry.schemaRow.pSchema, pEntry->ntbEntry.schemaRow.nCols * sizeof(SSchema));
92✔
2124
  if (pEntry->pExtSchemas != NULL) {
92!
2125
    (*ppRsp)->pSchemaExt = taosMemoryCalloc(pEntry->ntbEntry.schemaRow.nCols, sizeof(SSchemaExt));
×
2126
    if (NULL == (*ppRsp)->pSchemaExt) {
×
2127
      taosMemoryFree((*ppRsp)->pSchemas);
×
2128
      taosMemoryFree(*ppRsp);
×
2129
      TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2130
    }
2131
    memcpy((*ppRsp)->pSchemaExt, pEntry->pExtSchemas, pEntry->ntbEntry.schemaRow.nCols * sizeof(SSchemaExt));
×
2132
  }
2133

2134
_exit:
92✔
2135
  metaFetchEntryFree(&pEntry);
92✔
2136
  if (code != TSDB_CODE_SUCCESS) {
92!
2137
    vError("vgId:%d, failed to build exist normal table response, code:%d, line:%d", TD_VID(pVnode), code, lino);
×
2138
  }
2139
  return code;
92✔
2140
}
2141

2142
static int32_t buildExistTableInStreamRsp(SVnode *pVnode, SSubmitTbData *pSubmitTbData, STableMetaRsp **ppRsp) {
2,391✔
2143
  if (pSubmitTbData->pCreateTbReq->flags & TD_CREATE_NORMAL_TB_IN_STREAM) {
2,391✔
2144
    int32_t code = buildExistNormalTalbeRsp(pVnode, pSubmitTbData->uid, ppRsp);
92✔
2145
    if (code) {
92!
2146
      vError("vgId:%d, table uid:%" PRId64 " not exists, line:%d", TD_VID(pVnode), pSubmitTbData->uid, __LINE__);
×
2147
      return code;
×
2148
    }
2149
    return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
92✔
2150
  } else if (pSubmitTbData->pCreateTbReq->flags & TD_CREATE_SUB_TB_IN_STREAM) {
2,299✔
2151
    return buildExistSubTalbeRsp(pVnode, pSubmitTbData, ppRsp);
236✔
2152
  }
2153
  return TSDB_CODE_SUCCESS;
2,063✔
2154
}
2155

2156
static int32_t vnodeHandleAutoCreateTable(SVnode      *pVnode,    // vnode
2,005,874✔
2157
                                          int64_t      version,   // version
2158
                                          SSubmitReq2 *pRequest,  // request
2159
                                          SSubmitRsp2 *pResponse  // response
2160
) {
2161
  int32_t code = TSDB_CODE_SUCCESS;
2,005,874✔
2162
  int32_t numTbData = taosArrayGetSize(pRequest->aSubmitTbData);
2,005,874✔
2163
  SArray *newTbUids = NULL;
2,005,873✔
2164

2165
  for (int32_t i = 0; i < numTbData; ++i) {
4,080,198✔
2166
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
2,074,433✔
2167

2168
    if (pTbData->pCreateTbReq == NULL) {
2,074,431✔
2169
      continue;
2,026,416✔
2170
    }
2171

2172
    pTbData->uid = pTbData->pCreateTbReq->uid;
48,015✔
2173

2174
    // Alloc necessary resources
2175
    if (pResponse->aCreateTbRsp == NULL) {
48,015✔
2176
      pResponse->aCreateTbRsp = taosArrayInit(numTbData, sizeof(SVCreateTbRsp));
36,110✔
2177
      if (pResponse->aCreateTbRsp == NULL) {
36,110!
2178
        code = terrno;
×
2179
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2180
               tstrerror(code), version);
2181
        taosArrayDestroy(newTbUids);
×
2182
        return code;
×
2183
      }
2184
    }
2185

2186
    // Do create table
2187
    vDebug("vgId:%d start to handle auto create table, version:%" PRId64, TD_VID(pVnode), version);
48,015✔
2188

2189
    SVCreateTbRsp *pCreateTbRsp = taosArrayReserve(pResponse->aCreateTbRsp, 1);
48,018✔
2190
    code = metaCreateTable2(pVnode->pMeta, version, pTbData->pCreateTbReq, &pCreateTbRsp->pMeta);
48,019✔
2191
    if (code == TSDB_CODE_SUCCESS) {
48,015✔
2192
      // Allocate necessary resources
2193
      if (newTbUids == NULL) {
44,467✔
2194
        newTbUids = taosArrayInit(numTbData, sizeof(int64_t));
33,715✔
2195
        if (newTbUids == NULL) {
33,715!
2196
          code = terrno;
×
2197
          vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2198
                 tstrerror(code), version);
2199
          return code;
×
2200
        }
2201
      }
2202

2203
      if (taosArrayPush(newTbUids, &pTbData->uid) == NULL) {
88,934!
2204
        code = terrno;
×
2205
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2206
               tstrerror(code), version);
2207
        taosArrayDestroy(newTbUids);
×
2208
        return code;
×
2209
      }
2210

2211
      if (pCreateTbRsp->pMeta) {
44,467!
2212
        vnodeUpdateMetaRsp(pVnode, pCreateTbRsp->pMeta);
44,467✔
2213
      }
2214
    } else if (code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
3,548✔
2215
      code = terrno = 0;
3,540✔
2216
      pTbData->uid = pTbData->pCreateTbReq->uid;  // update uid if table exist for using below
3,540✔
2217

2218
      // stream: get sver from meta, write to pCreateTbRsp, and need to check crateTbReq is same as meta.
2219
      if (i == 0) {
3,540✔
2220
        // In the streaming scenario, multiple grouped req requests will only operate on the same write table, and
2221
        // only the first one needs to be processed.
2222
        code = buildExistTableInStreamRsp(pVnode, pTbData, &pCreateTbRsp->pMeta);
2,391✔
2223
        if (code) {
2,391✔
2224
          vInfo("vgId:%d failed to create table in stream:%s, code(0x%0x):%s", TD_VID(pVnode),
94!
2225
                pTbData->pCreateTbReq->name, code, tstrerror(code));
2226
          taosArrayDestroy(newTbUids);
94✔
2227
          return code;
94✔
2228
        }
2229
      }
2230
    } else {
2231
      code = terrno;
8✔
2232
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
8!
2233
             tstrerror(code), version);
2234
      taosArrayDestroy(newTbUids);
8✔
2235
      return code;
8✔
2236
    }
2237
  }
2238

2239
  // Update the affected table uid list
2240
  if (taosArrayGetSize(newTbUids) > 0) {
2,005,765✔
2241
    vDebug("vgId:%d, add %d table into query table list in handling submit", TD_VID(pVnode),
33,715✔
2242
           (int32_t)taosArrayGetSize(newTbUids));
2243
    if (tqUpdateTbUidList(pVnode->pTq, newTbUids, true) != 0) {
33,715!
2244
      vError("vgId:%d, failed to update tbUid list", TD_VID(pVnode));
×
2245
    }
2246
  }
2247

2248
  vDebug("vgId:%d, handle auto create table done, version:%" PRId64, TD_VID(pVnode), version);
2,005,770✔
2249

2250
  taosArrayDestroy(newTbUids);
2,005,772✔
2251
  return code;
2,005,772✔
2252
}
2253

2254
static void addExistTableInfoIntoRes(SVnode *pVnode, SSubmitReq2 *pRequest, SSubmitRsp2 *pResponse, SSubmitTbData *pTbData,
2✔
2255
                                     int32_t numTbData) {
2256
  int32_t code = 0;
2✔
2257
  int32_t lino = 0;
2✔
2258
  if ((pTbData->flags & SUBMIT_REQ_SCHEMA_RES) == 0) {
2!
2259
    return;
×
2260
  }
2261
  if (pResponse->aCreateTbRsp) {  // If aSubmitTbData is not NULL, it means that the request is a create table request,
2!
2262
                                  // so table info has exitst and we do not need to add again.
2263
    return;
2✔
2264
  }
2265
  pResponse->aCreateTbRsp = taosArrayInit(numTbData, sizeof(SVCreateTbRsp));
×
2266
  if (pResponse->aCreateTbRsp == NULL) {
×
2267
    code = terrno;
×
2268
    TSDB_CHECK_CODE(code, lino, _exit);
×
2269
  }
2270
  SVCreateTbRsp *pCreateTbRsp = taosArrayReserve(pResponse->aCreateTbRsp, 1);
×
2271
  if (pCreateTbRsp == NULL) {
×
2272
    code = terrno;
×
2273
    TSDB_CHECK_CODE(code, lino, _exit);
×
2274
  }
2275
  if (pTbData->suid == 0) {
×
2276
    code = buildExistNormalTalbeRsp(pVnode, pTbData->uid, &pCreateTbRsp->pMeta);
×
2277
    if (code) {
×
2278
      vError("vgId:%d, table uid:%" PRId64 " not exists, line:%d", TD_VID(pVnode), pTbData->uid, __LINE__);
×
2279
    }
2280
  } else {
2281
    code = buildExistSubTalbeRsp(pVnode, pTbData, &pCreateTbRsp->pMeta);
×
2282
  }
2283

2284
  TSDB_CHECK_CODE(code, lino, _exit);
×
2285
_exit:
×
2286
  if (code != TSDB_CODE_SUCCESS) {
×
2287
    vError("vgId:%d, failed to add exist table info into response, code:0x%0x, line:%d", TD_VID(pVnode), code, lino);
×
2288
  }
2289
  return;
×
2290
}
2291

2292
static int32_t vnodeHandleDataWrite(SVnode *pVnode, int64_t version, SSubmitReq2 *pRequest, SSubmitRsp2 *pResponse) {
2,005,772✔
2293
  int32_t code = TSDB_CODE_SUCCESS;
2,005,772✔
2294
  int32_t numTbData = taosArrayGetSize(pRequest->aSubmitTbData);
2,005,772✔
2295
  int8_t  hasBlob = 0;
2,005,771✔
2296

2297
  // Scan submit data
2298
  for (int32_t i = 0; i < numTbData; ++i) {
4,080,101✔
2299
    SMetaInfo      info = {0};
2,074,334✔
2300
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
2,074,334✔
2301

2302
    if (pTbData->flags & SUBMIT_REQ_WITH_BLOB) {
2,074,334!
2303
      hasBlob = 1;
×
2304
    }
2305
    if (pTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
2,074,334✔
2306
      continue;  // skip column data format
998✔
2307
    }
2308

2309
    code = metaGetInfo(pVnode->pMeta, pTbData->uid, &info, NULL);
2,073,336✔
2310
    if (code) {
2,073,337✔
2311
      code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
2✔
2312
      vWarn("vgId:%d, error occurred at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __FILE__,
2!
2313
            __LINE__, tstrerror(code), version, pTbData->uid);
2314
      return code;
4✔
2315
    }
2316

2317
    if (info.suid != pTbData->suid) {
2,073,335!
2318
      code = TSDB_CODE_INVALID_MSG;
×
2319
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64 " suid:%" PRId64
×
2320
             " info.suid:%" PRId64,
2321
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), version, pTbData->uid, pTbData->suid,
2322
             info.suid);
2323
      return code;
×
2324
    }
2325

2326
    if (info.suid) {
2,073,335✔
2327
      code = metaGetInfo(pVnode->pMeta, info.suid, &info, NULL);
1,666,670✔
2328
      if (code) {
1,666,669!
2329
        code = TSDB_CODE_INTERNAL_ERROR;
×
2330
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " suid:%" PRId64, TD_VID(pVnode), __func__,
×
2331
               __FILE__, __LINE__, tstrerror(code), version, info.suid);
2332
        return code;
×
2333
      }
2334
    }
2335

2336
    if (pTbData->sver != info.skmVer) {
2,073,334✔
2337
      code = TSDB_CODE_TDB_INVALID_TABLE_SCHEMA_VER;
2✔
2338
      addExistTableInfoIntoRes(pVnode, pRequest, pResponse, pTbData, numTbData);
2✔
2339
      vDebug("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64
2!
2340
             " sver:%d"
2341
             " info.skmVer:%d",
2342
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), version, pTbData->uid, pTbData->sver,
2343
             info.skmVer);
2344
      return code;
2✔
2345
    }
2346
  }
2347

2348
  // Do write data
2349
  vDebug("vgId:%d start to handle data write, version:%" PRId64, TD_VID(pVnode), version);
2,005,767✔
2350

2351
  for (int32_t i = 0; i < numTbData; ++i) {
4,080,086✔
2352
    int32_t        affectedRows = 0;
2,074,322✔
2353
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
2,074,322✔
2354

2355
    if (hasBlob) {
2,074,320!
2356
      code = vnodeSubmitBlobData(pVnode, pTbData);
×
2357
      if (code) {
×
2358
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2359
               __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2360
        return code;
×
2361
      }
2362
    }
2363

2364
    code = tsdbInsertTableData(pVnode->pTsdb, version, pTbData, &affectedRows);
2,074,320✔
2365
    if (code) {
2,074,320!
2366
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2367
             __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2368
      return code;
×
2369
    }
2370

2371
    code = metaUpdateChangeTimeWithLock(pVnode->pMeta, pTbData->uid, pTbData->ctimeMs);
2,074,320✔
2372
    if (code) {
2,074,319!
2373
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2374
             __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2375
      return code;
×
2376
    }
2377
    pResponse->affectedRows += affectedRows;
2,074,319✔
2378
  }
2379

2380
  vDebug("vgId:%d, handle data write done, version:%" PRId64 ", affectedRows:%d", TD_VID(pVnode), version,
2,005,764✔
2381
         pResponse->affectedRows);
2382
  return code;
2,005,768✔
2383
}
2384

2385
static int32_t vnodeScanColumnData(SVnode *pVnode, SSubmitTbData *pTbData, TSKEY minKey, TSKEY maxKey) {
997✔
2386
  int32_t code = 0;
997✔
2387

2388
  int32_t   numCols = taosArrayGetSize(pTbData->aCol);
997✔
2389
  SColData *aColData = (SColData *)TARRAY_DATA(pTbData->aCol);
997✔
2390

2391
  if (numCols <= 0) {
997!
2392
    code = TSDB_CODE_INVALID_MSG;
×
2393
    vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " numCols:%d", TD_VID(pVnode), __func__,
×
2394
           __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, numCols);
2395
    return code;
×
2396
  }
2397

2398
  if (aColData[0].cid != PRIMARYKEY_TIMESTAMP_COL_ID || aColData[0].type != TSDB_DATA_TYPE_TIMESTAMP ||
997!
2399
      aColData[0].nVal <= 0) {
997!
2400
    code = TSDB_CODE_INVALID_MSG;
×
2401
    vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64
×
2402
           " first column is not primary key timestamp, cid:%d type:%d nVal:%d",
2403
           TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, aColData[0].cid,
2404
           aColData[0].type, aColData[0].nVal);
2405
    return code;
×
2406
  }
2407

2408
  for (int32_t i = 1; i < numCols; ++i) {
6,840✔
2409
    if (aColData[i].nVal != aColData[0].nVal) {
5,843!
2410
      code = TSDB_CODE_INVALID_MSG;
×
2411
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64
×
2412
             " column cid:%d type:%d nVal:%d is not equal to primary key timestamp nVal:%d",
2413
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid,
2414
             aColData[i].cid, aColData[i].type, aColData[i].nVal, aColData[0].nVal);
2415
      return code;
×
2416
    }
2417
  }
2418

2419
  SRowKey *pLastKey = NULL;
997✔
2420
  SRowKey  lastKey = {0};
997✔
2421
  for (int32_t i = 0; i < aColData[0].nVal; ++i) {
73,592✔
2422
    SRowKey key = {0};
72,597✔
2423

2424
    tColDataArrGetRowKey(aColData, numCols, i, &key);
72,597✔
2425

2426
    if (key.ts < minKey || key.ts > maxKey) {
72,546!
2427
      code = TSDB_CODE_INVALID_MSG;
×
2428
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] key:%" PRId64
×
2429
             " is out of range [%" PRId64 ", %" PRId64 "]",
2430
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, i, key.ts,
2431
             minKey, maxKey);
2432
      return code;
×
2433
    }
2434

2435
    if (pLastKey && tRowKeyCompare(pLastKey, &key) >= 0) {
72,551!
2436
      code = TSDB_CODE_INVALID_MSG;
×
2437
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] key:%" PRId64
×
2438
             " is not in order, lastKey:%" PRId64,
2439
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, i, key.ts,
2440
             pLastKey->ts);
2441
      return code;
×
2442
    } else if (pLastKey == NULL) {
72,595✔
2443
      pLastKey = &lastKey;
996✔
2444
    }
2445

2446
    *pLastKey = key;
72,595✔
2447
  }
2448

2449
  return code;
995✔
2450
}
2451

2452
static int32_t vnodeScanSubmitRowData(SVnode *pVnode, SSubmitTbData *pTbData, TSKEY minKey, TSKEY maxKey) {
2,073,427✔
2453
  int32_t code = 0;
2,073,427✔
2454

2455
  int32_t numRows = taosArrayGetSize(pTbData->aRowP);
2,073,427✔
2456
  SRow  **aRow = (SRow **)TARRAY_DATA(pTbData->aRowP);
2,073,427✔
2457

2458
  if (numRows <= 0) {
2,073,427!
2459
    code = TSDB_CODE_INVALID_MSG;
×
2460
    vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " numRows:%d", TD_VID(pVnode), __func__,
×
2461
           __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, numRows);
2462
    return code;
×
2463
  }
2464

2465
  SRowKey *pLastKey = NULL;
2,073,428✔
2466
  SRowKey  lastKey = {0};
2,073,428✔
2467
  for (int32_t i = 0; i < numRows; ++i) {
146,013,700✔
2468
    SRow *pRow = aRow[i];
143,928,782✔
2469
    if (pRow->sver != pTbData->sver) {
143,928,782!
2470
      code = TSDB_CODE_INVALID_MSG;
×
2471
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] sver:%d pTbData->sver:%d",
×
2472
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, i, pRow->sver,
2473
             pTbData->sver);
2474
      return code;
×
2475
    }
2476

2477
    SRowKey key = {0};
143,928,782✔
2478
    tRowGetKey(pRow, &key);
287,857,564✔
2479
    if (key.ts < minKey || key.ts > maxKey) {
143,928,766!
2480
      code = TSDB_CODE_INVALID_MSG;
2,095✔
2481
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] key:%" PRId64
2,095!
2482
             " is out of range [%" PRId64 ", %" PRId64 "]",
2483
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, i, key.ts,
2484
             minKey, maxKey);
2485
      return code;
×
2486
    }
2487

2488
    if (pLastKey && tRowKeyCompare(pLastKey, &key) >= 0) {
143,926,671!
2489
      code = TSDB_CODE_INVALID_MSG;
×
2490
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] key:%" PRId64
×
2491
             " is not in order, lastKey:%" PRId64,
2492
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, i, key.ts,
2493
             pLastKey->ts);
2494
      return code;
×
2495
    } else if (pLastKey == NULL) {
143,940,272✔
2496
      pLastKey = &lastKey;
2,073,427✔
2497
    }
2498

2499
    *pLastKey = key;
143,940,272✔
2500
  }
2501

2502
  return code;
2,084,918✔
2503
}
2504

2505
static int32_t vnodeScanSubmitReq(SVnode *pVnode, int64_t version, SSubmitReq2 *pRequest, SSubmitRsp2 *pResponse) {
2,005,862✔
2506
  int32_t code = TSDB_CODE_SUCCESS;
2,005,862✔
2507
  int32_t numTbData = taosArrayGetSize(pRequest->aSubmitTbData);
2,005,862✔
2508

2509
  TSKEY now = taosGetTimestamp(pVnode->config.tsdbCfg.precision);
2,005,862✔
2510
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * pVnode->config.tsdbCfg.keep2;
2,005,864✔
2511
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
2,005,864✔
2512
  for (int32_t i = 0; i < numTbData; i++) {
4,080,301✔
2513
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
2,074,426✔
2514

2515
    if (pTbData->pCreateTbReq && pTbData->pCreateTbReq->uid == 0) {
2,074,427!
2516
      code = TSDB_CODE_INVALID_MSG;
×
2517
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2518
             tstrerror(code), version);
2519
      return code;
×
2520
    }
2521

2522
    if (pTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
2,074,427✔
2523
      code = vnodeScanColumnData(pVnode, pTbData, minKey, maxKey);
998✔
2524
      if (code) {
997!
2525
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2526
               __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2527
        return code;
×
2528
      }
2529
    } else {
2530
      code = vnodeScanSubmitRowData(pVnode, pTbData, minKey, maxKey);
2,073,429✔
2531
      if (code) {
2,073,438!
2532
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2533
               __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2534
        return code;
×
2535
      }
2536
    }
2537
  }
2538

2539
  return code;
2,005,875✔
2540
}
2541

2542
static int32_t vnodeProcessSubmitReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
2,005,863✔
2543
                                     SRpcMsg *pOriginalMsg) {
2544
  int32_t code = 0;
2,005,863✔
2545
  int32_t lino = 0;
2,005,863✔
2546
  terrno = 0;
2,005,863✔
2547

2548
  SSubmitReq2 *pSubmitReq = &(SSubmitReq2){0};
2,005,869✔
2549
  SSubmitRsp2 *pSubmitRsp = &(SSubmitRsp2){0};
2,005,869✔
2550
  int32_t      ret;
2551
  SEncoder     ec = {0};
2,005,869✔
2552

2553
  pRsp->code = TSDB_CODE_SUCCESS;
2,005,869✔
2554

2555
  void           *pAllocMsg = NULL;
2,005,869✔
2556
  SSubmitReq2Msg *pMsg = (SSubmitReq2Msg *)pReq;
2,005,869✔
2557
  SDecoder        dc = {0};
2,005,869✔
2558
  if (0 == taosHton64(pMsg->version)) {
2,005,869!
2559
    code = vnodeSubmitReqConvertToSubmitReq2(pVnode, (SSubmitReq *)pMsg, pSubmitReq);
×
2560
    if (TSDB_CODE_SUCCESS == code) {
×
2561
      code = vnodeRebuildSubmitReqMsg(pSubmitReq, &pReq);
×
2562
    }
2563
    if (TSDB_CODE_SUCCESS == code) {
×
2564
      pAllocMsg = pReq;
×
2565
    }
2566
    TSDB_CHECK_CODE(code, lino, _exit);
×
2567
  } else {
2568
    // decode
2569
    pReq = POINTER_SHIFT(pReq, sizeof(SSubmitReq2Msg));
2,005,871✔
2570
    len -= sizeof(SSubmitReq2Msg);
2,005,871✔
2571

2572
    tDecoderInit(&dc, pReq, len);
2,005,871✔
2573
    if (tDecodeSubmitReq(&dc, pSubmitReq, NULL) < 0) {
2,005,870!
2574
      code = TSDB_CODE_INVALID_MSG;
×
2575
      TSDB_CHECK_CODE(code, lino, _exit);
×
2576
    }
2577
  }
2578

2579
  // Scan the request
2580
  code = vnodeScanSubmitReq(pVnode, ver, pSubmitReq, pSubmitRsp);
2,005,864✔
2581
  if (code) {
2,005,870!
2582
    vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2583
           tstrerror(code), ver);
2584
    TSDB_CHECK_CODE(code, lino, _exit);
×
2585
  }
2586

2587
  vGDebug(pOriginalMsg ? &pOriginalMsg->info.traceId : NULL, "vgId:%d, index:%" PRId64 ", submit block, rows:%d",
2,005,870!
2588
          TD_VID(pVnode), ver, (int32_t)taosArrayGetSize(pSubmitReq->aSubmitTbData));
2589

2590
  // Handle auto create table
2591
  code = vnodeHandleAutoCreateTable(pVnode, ver, pSubmitReq, pSubmitRsp);
2,005,874✔
2592
  if (code) {
2,005,874✔
2593
    vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
102!
2594
           tstrerror(code), ver);
2595
    TSDB_CHECK_CODE(code, lino, _exit);
102!
2596
  }
2597

2598
  // Handle data write
2599
  code = vnodeHandleDataWrite(pVnode, ver, pSubmitReq, pSubmitRsp);
2,005,772✔
2600
  if (code) {
2,005,772✔
2601
    vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
4!
2602
           tstrerror(code), ver);
2603
    TSDB_CHECK_CODE(code, lino, _exit);
4!
2604
  }
2605

2606
_exit:
2,005,768✔
2607
  // message
2608
  pRsp->code = code;
2,005,874✔
2609
  tEncodeSize(tEncodeSSubmitRsp2, pSubmitRsp, pRsp->contLen, ret);
2,005,874!
2610
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
2,005,869✔
2611
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
2,005,873✔
2612
  if (tEncodeSSubmitRsp2(&ec, pSubmitRsp) < 0) {
2,005,873!
2613
    vError("vgId:%d, failed to encode submit response", TD_VID(pVnode));
×
2614
  }
2615
  tEncoderClear(&ec);
2,005,872✔
2616

2617
  // update statistics
2618
  (void)atomic_add_fetch_64(&pVnode->statis.nInsert, pSubmitRsp->affectedRows);
2,005,872✔
2619
  (void)atomic_add_fetch_64(&pVnode->statis.nInsertSuccess, pSubmitRsp->affectedRows);
2,005,873✔
2620
  (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsert, 1);
2,005,874✔
2621

2622
  // update metrics
2623
  METRICS_UPDATE(pVnode->writeMetrics.total_requests, METRIC_LEVEL_LOW, 1);
2,005,873!
2624
  METRICS_UPDATE(pVnode->writeMetrics.total_rows, METRIC_LEVEL_HIGH, pSubmitRsp->affectedRows);
2,005,873!
2625
  METRICS_UPDATE(pVnode->writeMetrics.total_bytes, METRIC_LEVEL_LOW, pMsg->header.contLen);
2,005,873!
2626

2627
  if (tsEnableMonitor && tsMonitorFqdn[0] != 0 && tsMonitorPort != 0 && pSubmitRsp->affectedRows > 0 &&
2,005,873!
2628
      strlen(pOriginalMsg->info.conn.user) > 0 && tsInsertCounter != NULL) {
×
2629
    const char *sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT_AFFECTED_ROWS,
×
2630
                                   pVnode->monitor.strClusterId,
×
2631
                                   pVnode->monitor.strDnodeId,
×
2632
                                   tsLocalEp,
2633
                                   pVnode->monitor.strVgId,
×
2634
                                   pOriginalMsg->info.conn.user,
×
2635
                                   "Success"};
2636
    int         tv = taos_counter_add(tsInsertCounter, pSubmitRsp->affectedRows, sample_labels);
×
2637
  }
2638

2639
  if (code == 0) {
2,005,873✔
2640
    (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
2,005,766✔
2641
    code = tdProcessRSmaSubmit(pVnode->pSma, ver, pSubmitReq, pReq, len);
2,005,768✔
2642
  }
2643

2644
  // clear
2645
  tDestroySubmitReq(pSubmitReq, 0 == taosHton64(pMsg->version) ? TSDB_MSG_FLG_CMPT : TSDB_MSG_FLG_DECODE);
2,005,872!
2646
  tDestroySSubmitRsp2(pSubmitRsp, TSDB_MSG_FLG_ENCODE);
2,005,874✔
2647

2648
  if (code) {
2,005,872✔
2649
    terrno = code;
106✔
2650
    if (code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
106✔
2651
      vInfo("vgId:%d, failed to process submit request since %s, lino:%d, version:%" PRId64, TD_VID(pVnode),
94!
2652
            tstrerror(code), lino, ver);
2653

2654
    } else {
2655
      vError("vgId:%d, failed to process submit request since %s, lino:%d, version:%" PRId64, TD_VID(pVnode),
12!
2656
             tstrerror(code), lino, ver);
2657
    }
2658
  }
2659

2660
  taosMemoryFree(pAllocMsg);
2,005,871!
2661
  tDecoderClear(&dc);
2,005,870✔
2662

2663
  return code;
2,005,874✔
2664
}
2665

2666
static int32_t vnodeConsolidateAlterHashRange(SVnode *pVnode, int64_t ver) {
64✔
2667
  int32_t code = TSDB_CODE_SUCCESS;
64✔
2668

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

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

2675
  return code;
64✔
2676
}
2677

2678
static int32_t vnodeProcessAlterConfirmReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
8,221✔
2679
  vInfo("vgId:%d, vnode handle msgType:alter-confirm, alter confirm msg is processed", TD_VID(pVnode));
8,221!
2680
  int32_t code = TSDB_CODE_SUCCESS;
8,221✔
2681
  if (!pVnode->config.hashChange) {
8,221✔
2682
    goto _exit;
8,157✔
2683
  }
2684

2685
  code = vnodeConsolidateAlterHashRange(pVnode, ver);
64✔
2686
  if (code < 0) {
64!
2687
    vError("vgId:%d, failed to consolidate alter hashrange since %s. version:%" PRId64, TD_VID(pVnode), terrstr(), ver);
×
2688
    goto _exit;
×
2689
  }
2690
  pVnode->config.hashChange = false;
64✔
2691

2692
_exit:
8,221✔
2693
  pRsp->msgType = TDMT_VND_ALTER_CONFIRM_RSP;
8,221✔
2694
  pRsp->code = code;
8,221✔
2695
  pRsp->pCont = NULL;
8,221✔
2696
  pRsp->contLen = 0;
8,221✔
2697

2698
  return code;
8,221✔
2699
}
2700

2701
extern int32_t tsdbDisableAndCancelAllBgTask(STsdb *pTsdb);
2702
extern void    tsdbEnableBgTask(STsdb *pTsdb);
2703

2704
static int32_t vnodeProcessAlterConfigReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
614✔
2705
  bool walChanged = false;
614✔
2706
  bool tsdbChanged = false;
614✔
2707

2708
  SAlterVnodeConfigReq req = {0};
614✔
2709
  if (tDeserializeSAlterVnodeConfigReq(pReq, len, &req) != 0) {
614!
2710
    terrno = TSDB_CODE_INVALID_MSG;
×
2711
    return TSDB_CODE_INVALID_MSG;
×
2712
  }
2713

2714
  vInfo("vgId:%d, start to alter vnode config, page:%d pageSize:%d buffer:%d szPage:%d szBuf:%" PRIu64
613!
2715
        " cacheLast:%d cacheLastSize:%d days:%d keep0:%d keep1:%d keep2:%d keepTimeOffset:%d ssKeepLocal:%d "
2716
        "ssCompact:%d fsync:%d level:%d "
2717
        "walRetentionPeriod:%d walRetentionSize:%d",
2718
        TD_VID(pVnode), req.pages, req.pageSize, req.buffer, req.pageSize * 1024, (uint64_t)req.buffer * 1024 * 1024,
2719
        req.cacheLast, req.cacheLastSize, req.daysPerFile, req.daysToKeep0, req.daysToKeep1, req.daysToKeep2,
2720
        req.keepTimeOffset, req.ssKeepLocal, req.ssCompact, req.walFsyncPeriod, req.walLevel, req.walRetentionPeriod,
2721
        req.walRetentionSize);
2722

2723
  if (pVnode->config.cacheLastSize != req.cacheLastSize) {
614✔
2724
    pVnode->config.cacheLastSize = req.cacheLastSize;
77✔
2725
    tsdbCacheSetCapacity(pVnode, (size_t)pVnode->config.cacheLastSize * 1024 * 1024);
77✔
2726
  }
2727

2728
  if (pVnode->config.szBuf != req.buffer * 1024LL * 1024LL) {
614✔
2729
    vInfo("vgId:%d, vnode buffer is changed from %" PRId64 " to %" PRId64, TD_VID(pVnode), pVnode->config.szBuf,
14!
2730
          (uint64_t)(req.buffer * 1024LL * 1024LL));
2731
    pVnode->config.szBuf = req.buffer * 1024LL * 1024LL;
14✔
2732
  }
2733

2734
  if (pVnode->config.szCache != req.pages) {
614✔
2735
    if ((terrno = metaAlterCache(pVnode->pMeta, req.pages)) < 0) {
6!
2736
      vError("vgId:%d, failed to change vnode pages from %d to %d failed since %s", TD_VID(pVnode),
×
2737
             pVnode->config.szCache, req.pages, tstrerror(terrno));
2738
      return terrno;
×
2739
    } else {
2740
      vInfo("vgId:%d, vnode pages is changed from %d to %d", TD_VID(pVnode), pVnode->config.szCache, req.pages);
6!
2741
      pVnode->config.szCache = req.pages;
6✔
2742
    }
2743
  }
2744

2745
  if (pVnode->config.cacheLast != req.cacheLast) {
614✔
2746
    pVnode->config.cacheLast = req.cacheLast;
256✔
2747
  }
2748

2749
  if (pVnode->config.walCfg.fsyncPeriod != req.walFsyncPeriod) {
614✔
2750
    pVnode->config.walCfg.fsyncPeriod = req.walFsyncPeriod;
72✔
2751
    walChanged = true;
72✔
2752
  }
2753

2754
  if (pVnode->config.walCfg.level != req.walLevel) {
614✔
2755
    if (pVnode->config.walCfg.level == 0) {
50!
2756
      pVnode->config.walCfg.clearFiles = 1;
×
2757
    }
2758
    pVnode->config.walCfg.level = req.walLevel;
50✔
2759
    walChanged = true;
50✔
2760
  }
2761

2762
  if (pVnode->config.walCfg.retentionPeriod != req.walRetentionPeriod) {
614✔
2763
    pVnode->config.walCfg.retentionPeriod = req.walRetentionPeriod;
124✔
2764
    walChanged = true;
124✔
2765
  }
2766

2767
  if (pVnode->config.walCfg.retentionSize != req.walRetentionSize) {
614✔
2768
    pVnode->config.walCfg.retentionSize = req.walRetentionSize;
2✔
2769
    walChanged = true;
2✔
2770
  }
2771

2772
  if (pVnode->config.tsdbCfg.keep0 != req.daysToKeep0) {
614✔
2773
    pVnode->config.tsdbCfg.keep0 = req.daysToKeep0;
48✔
2774
    if (!VND_IS_RSMA(pVnode)) {
48!
2775
      tsdbChanged = true;
48✔
2776
    }
2777
  }
2778

2779
  if (pVnode->config.tsdbCfg.keep1 != req.daysToKeep1) {
614✔
2780
    pVnode->config.tsdbCfg.keep1 = req.daysToKeep1;
48✔
2781
    if (!VND_IS_RSMA(pVnode)) {
48!
2782
      tsdbChanged = true;
48✔
2783
    }
2784
  }
2785

2786
  if (pVnode->config.tsdbCfg.keep2 != req.daysToKeep2) {
614✔
2787
    pVnode->config.tsdbCfg.keep2 = req.daysToKeep2;
48✔
2788
    if (!VND_IS_RSMA(pVnode)) {
48!
2789
      tsdbChanged = true;
48✔
2790
    }
2791
  }
2792

2793
  if (pVnode->config.tsdbCfg.keepTimeOffset != req.keepTimeOffset) {
614✔
2794
    pVnode->config.tsdbCfg.keepTimeOffset = req.keepTimeOffset;
10✔
2795
    if (!VND_IS_RSMA(pVnode)) {
10!
2796
      tsdbChanged = true;
10✔
2797
    }
2798
  }
2799

2800
  if (req.sttTrigger != -1 && req.sttTrigger != pVnode->config.sttTrigger) {
614!
2801
    if (req.sttTrigger > 1 && pVnode->config.sttTrigger > 1) {
4!
2802
      pVnode->config.sttTrigger = req.sttTrigger;
2✔
2803
    } else {
2804
      vnodeAWait(&pVnode->commitTask);
2✔
2805

2806
      int32_t ret = tsdbDisableAndCancelAllBgTask(pVnode->pTsdb);
2✔
2807
      if (ret != 0) {
2!
2808
        vError("vgId:%d, failed to disable bg task since %s", TD_VID(pVnode), tstrerror(ERRNO));
×
2809
      }
2810

2811
      pVnode->config.sttTrigger = req.sttTrigger;
2✔
2812
      tsdbEnableBgTask(pVnode->pTsdb);
2✔
2813
    }
2814
  }
2815

2816
  if (req.minRows != -1 && req.minRows != pVnode->config.tsdbCfg.minRows) {
614!
2817
    pVnode->config.tsdbCfg.minRows = req.minRows;
2✔
2818
  }
2819

2820
  if (req.ssKeepLocal != -1 && req.ssKeepLocal != pVnode->config.ssKeepLocal) {
614!
2821
    pVnode->config.ssKeepLocal = req.ssKeepLocal;
2✔
2822
  }
2823
  if (req.ssCompact != -1 && req.ssCompact != pVnode->config.ssCompact) {
614!
2824
    pVnode->config.ssCompact = req.ssCompact;
2✔
2825
  }
2826

2827
  if (walChanged) {
614✔
2828
    if (walAlter(pVnode->pWal, &pVnode->config.walCfg) != 0) {
244!
2829
      vError("vgId:%d, failed to alter wal config since %s", TD_VID(pVnode), tstrerror(ERRNO));
×
2830
    }
2831
  }
2832

2833
  if (tsdbChanged) {
614✔
2834
    tsdbSetKeepCfg(pVnode->pTsdb, &pVnode->config.tsdbCfg);
60✔
2835
  }
2836

2837
  return 0;
614✔
2838
}
2839

2840
static int32_t vnodeProcessBatchDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
2841
  SBatchDeleteReq deleteReq;
2842
  SDecoder        decoder;
2843
  tDecoderInit(&decoder, pReq, len);
×
2844
  if (tDecodeSBatchDeleteReq(&decoder, &deleteReq) < 0) {
×
2845
    tDecoderClear(&decoder);
×
2846
    return terrno = TSDB_CODE_INVALID_MSG;
×
2847
  }
2848

2849
  SMetaReader mr = {0};
×
2850
  metaReaderDoInit(&mr, pVnode->pMeta, META_READER_NOLOCK);
×
2851
  STsdb *pTsdb = pVnode->pTsdb;
×
2852

2853
  if (deleteReq.level) {
×
2854
    pTsdb = deleteReq.level == 1 ? VND_RSMA1(pVnode) : VND_RSMA2(pVnode);
×
2855
  }
2856

2857
  int32_t sz = taosArrayGetSize(deleteReq.deleteReqs);
×
2858
  for (int32_t i = 0; i < sz; i++) {
×
2859
    SSingleDeleteReq *pOneReq = taosArrayGet(deleteReq.deleteReqs, i);
×
2860
    char             *name = pOneReq->tbname;
×
2861
    if (metaGetTableEntryByName(&mr, name) < 0) {
×
2862
      vDebug("vgId:%d, stream delete msg, skip since no table: %s", pVnode->config.vgId, name);
×
2863
      continue;
×
2864
    }
2865

2866
    int64_t uid = mr.me.uid;
×
2867

2868
    int32_t code = tsdbDeleteTableData(pTsdb, ver, deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
×
2869
    if (code < 0) {
×
2870
      terrno = code;
×
2871
      vError("vgId:%d, delete error since %s, suid:%" PRId64 ", uid:%" PRId64 ", start ts:%" PRId64 ", end ts:%" PRId64,
×
2872
             TD_VID(pVnode), terrstr(), deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
2873
    }
2874

2875
    if (deleteReq.level == 0) {
×
2876
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, deleteReq.ctimeMs);
×
2877
      if (code < 0) {
×
2878
        terrno = code;
×
2879
        vError("vgId:%d, update change time error since %s, suid:%" PRId64 ", uid:%" PRId64 ", start ts:%" PRId64
×
2880
               ", end ts:%" PRId64,
2881
               TD_VID(pVnode), terrstr(), deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
2882
      }
2883
    }
2884
    tDecoderClear(&mr.coder);
×
2885
  }
2886
  metaReaderClear(&mr);
×
2887
  taosArrayDestroy(deleteReq.deleteReqs);
×
2888
  return 0;
×
2889
}
2890

2891
static int32_t vnodeProcessDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
8,239✔
2892
                                     SRpcMsg *pOriginalMsg) {
2893
  int32_t     code = 0;
8,239✔
2894
  SDecoder   *pCoder = &(SDecoder){0};
8,239✔
2895
  SDeleteRes *pRes = &(SDeleteRes){0};
8,239✔
2896

2897
  pRsp->msgType = TDMT_VND_DELETE_RSP;
8,239✔
2898
  pRsp->pCont = NULL;
8,239✔
2899
  pRsp->contLen = 0;
8,239✔
2900
  pRsp->code = TSDB_CODE_SUCCESS;
8,239✔
2901

2902
  pRes->uidList = taosArrayInit(0, sizeof(tb_uid_t));
8,239✔
2903
  if (pRes->uidList == NULL) {
8,239!
2904
    code = terrno;
×
2905
    goto _err;
×
2906
  }
2907

2908
  tDecoderInit(pCoder, pReq, len);
8,239✔
2909
  code = tDecodeDeleteRes(pCoder, pRes);
8,239✔
2910
  if (code) goto _err;
8,239!
2911

2912
  if (pRes->affectedRows > 0) {
8,239✔
2913
    for (int32_t iUid = 0; iUid < taosArrayGetSize(pRes->uidList); iUid++) {
15,606✔
2914
      uint64_t uid = *(uint64_t *)taosArrayGet(pRes->uidList, iUid);
8,321✔
2915
      code = tsdbDeleteTableData(pVnode->pTsdb, ver, pRes->suid, uid, pRes->skey, pRes->ekey);
8,321✔
2916
      if (code) goto _err;
8,321!
2917
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, pRes->ctimeMs);
8,321✔
2918
      if (code) goto _err;
8,321!
2919
    }
2920
  }
2921

2922
  code = tdProcessRSmaDelete(pVnode->pSma, ver, pRes, pReq, len);
8,239✔
2923

2924
  tDecoderClear(pCoder);
8,238✔
2925
  taosArrayDestroy(pRes->uidList);
8,238✔
2926

2927
  SVDeleteRsp rsp = {.affectedRows = pRes->affectedRows};
8,239✔
2928
  int32_t     ret = 0;
8,239✔
2929
  tEncodeSize(tEncodeSVDeleteRsp, &rsp, pRsp->contLen, ret);
8,239!
2930
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
8,238✔
2931
  SEncoder ec = {0};
8,239✔
2932
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
8,239✔
2933
  code = tEncodeSVDeleteRsp(&ec, &rsp);
8,239✔
2934
  if (code) goto _err;
8,238!
2935
  tEncoderClear(&ec);
8,238✔
2936
  return code;
8,239✔
2937

2938
_err:
×
2939
  /*
2940
  if(code == TSDB_CODE_SUCCESS){
2941
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_DELETE, pVnode->monitor.strClusterId,
2942
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2943
                                        pOriginalMsg->info.conn.user, "Success"};
2944
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2945
  }
2946
  else{
2947
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_DELETE, pVnode->monitor.strClusterId,
2948
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2949
                                        pOriginalMsg->info.conn.user, "Failed"};
2950
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2951
  }
2952
  */
2953

2954
  return code;
×
2955
}
2956
static int32_t vnodeProcessCreateIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
19✔
2957
  SVCreateStbReq req = {0};
19✔
2958
  SDecoder       dc = {0};
19✔
2959
  int32_t        code = 0;
19✔
2960

2961
  pRsp->msgType = TDMT_VND_CREATE_INDEX_RSP;
19✔
2962
  pRsp->code = TSDB_CODE_SUCCESS;
19✔
2963
  pRsp->pCont = NULL;
19✔
2964
  pRsp->contLen = 0;
19✔
2965

2966
  tDecoderInit(&dc, pReq, len);
19✔
2967
  // decode req
2968
  if (tDecodeSVCreateStbReq(&dc, &req) < 0) {
19!
2969
    tDecoderClear(&dc);
×
2970
    return terrno = TSDB_CODE_INVALID_MSG;
×
2971
  }
2972

2973
  code = metaAddIndexToSuperTable(pVnode->pMeta, ver, &req);
19✔
2974
  if (code) {
19!
2975
    pRsp->code = code;
×
2976
    goto _err;
×
2977
  }
2978
  tDecoderClear(&dc);
19✔
2979
  return 0;
19✔
2980

2981
_err:
×
2982
  tDecoderClear(&dc);
×
2983
  return code;
×
2984
}
2985
static int32_t vnodeProcessDropIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
10✔
2986
  SDropIndexReq req = {0};
10✔
2987
  int32_t       code = 0;
10✔
2988
  pRsp->msgType = TDMT_VND_DROP_INDEX_RSP;
10✔
2989
  pRsp->code = TSDB_CODE_SUCCESS;
10✔
2990
  pRsp->pCont = NULL;
10✔
2991
  pRsp->contLen = 0;
10✔
2992

2993
  if ((code = tDeserializeSDropIdxReq(pReq, len, &req))) {
10!
2994
    pRsp->code = code;
×
2995
    return code;
×
2996
  }
2997

2998
  code = metaDropIndexFromSuperTable(pVnode->pMeta, ver, &req);
10✔
2999
  if (code) {
10!
3000
    pRsp->code = code;
×
3001
    return code;
×
3002
  }
3003
  return TSDB_CODE_SUCCESS;
10✔
3004
}
3005

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

3008
static int32_t vnodeProcessCompactVnodeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
175✔
3009
  if (!pVnode->restored) {
175✔
3010
    vInfo("vgId:%d, ignore compact req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
58!
3011
    return 0;
58✔
3012
  }
3013
  return vnodeAsyncCompact(pVnode, ver, pReq, len, pRsp);
117✔
3014
}
3015

3016
static int32_t vnodeProcessConfigChangeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
3017
  if (syncCheckMember(pVnode->sync) != 0) {
×
3018
    vError("vgId:%d, failed to check member", TD_VID(pVnode));
×
3019
  }
3020

3021
  pRsp->msgType = TDMT_SYNC_CONFIG_CHANGE_RSP;
×
3022
  pRsp->code = TSDB_CODE_SUCCESS;
×
3023
  pRsp->pCont = NULL;
×
3024
  pRsp->contLen = 0;
×
3025

3026
  return 0;
×
3027
}
3028

3029
static int32_t vnodeCheckState(SVnode *pVnode) {
92✔
3030
  SSyncState syncState = syncGetState(pVnode->sync);
92✔
3031
  if (syncState.state != TAOS_SYNC_STATE_LEADER) {
92✔
3032
    return terrno = TSDB_CODE_SYN_NOT_LEADER;
65✔
3033
  }
3034
  return 0;
27✔
3035
}
3036

3037
static int32_t vnodeCheckToken(SVnode *pVnode, char *member0Token, char *member1Token) {
27✔
3038
  char token[TSDB_ARB_TOKEN_SIZE] = {0};
27✔
3039
  if (vnodeGetArbToken(pVnode, token) != 0) {
27!
3040
    return terrno = TSDB_CODE_NOT_FOUND;
×
3041
  }
3042

3043
  if (strncmp(token, member0Token, TSDB_ARB_TOKEN_SIZE) != 0 &&
27✔
3044
      strncmp(token, member1Token, TSDB_ARB_TOKEN_SIZE) != 0) {
21✔
3045
    return terrno = TSDB_CODE_MND_ARB_TOKEN_MISMATCH;
15✔
3046
  }
3047

3048
  terrno = TSDB_CODE_SUCCESS;
12✔
3049
  return 0;
12✔
3050
}
3051

3052
static int32_t vnodeCheckSyncd(SVnode *pVnode, char *member0Token, char *member1Token) {
6✔
3053
  int32_t code = vnodeCheckToken(pVnode, member0Token, member1Token);
6✔
3054
  if (code != 0) {
6!
3055
    return code;
×
3056
  }
3057

3058
  return syncCheckSynced(pVnode->sync);
6✔
3059
}
3060

3061
static int32_t vnodeProcessArbCheckSyncReq(SVnode *pVnode, void *pReq, int32_t len, SRpcMsg *pRsp) {
22✔
3062
  int32_t code = 0;
22✔
3063

3064
  if ((code = vnodeCheckState(pVnode)) != 0) {
22✔
3065
    vDebug("vgId:%d, failed to preprocess vnode-arb-check-sync request since %s", TD_VID(pVnode), tstrerror(code));
16!
3066
    return 0;
16✔
3067
  }
3068

3069
  SVArbCheckSyncReq syncReq = {0};
6✔
3070

3071
  code = tDeserializeSVArbCheckSyncReq(pReq, len, &syncReq);
6✔
3072
  if (code) {
6!
3073
    return code;
×
3074
  }
3075

3076
  vInfo("vgId:%d, start to process vnode-arb-check-sync req QID:0x%" PRIx64 ":0x%" PRIx64 ", seqNum:%" PRIx64
6!
3077
        ", arbToken:%s, member0Token:%s, member1Token:%s, "
3078
        "arbTerm:%" PRId64,
3079
        TD_VID(pVnode), pRsp->info.traceId.rootId, pRsp->info.traceId.msgId, pRsp->info.seqNum, syncReq.arbToken,
3080
        syncReq.member0Token, syncReq.member1Token, syncReq.arbTerm);
3081

3082
  pRsp->msgType = TDMT_VND_ARB_CHECK_SYNC_RSP;
6✔
3083
  pRsp->code = TSDB_CODE_SUCCESS;
6✔
3084
  pRsp->pCont = NULL;
6✔
3085
  pRsp->contLen = 0;
6✔
3086

3087
  SVArbCheckSyncRsp syncRsp = {0};
6✔
3088
  syncRsp.arbToken = syncReq.arbToken;
6✔
3089
  syncRsp.member0Token = syncReq.member0Token;
6✔
3090
  syncRsp.member1Token = syncReq.member1Token;
6✔
3091
  syncRsp.vgId = TD_VID(pVnode);
6✔
3092

3093
  if ((syncRsp.errCode = vnodeCheckSyncd(pVnode, syncReq.member0Token, syncReq.member1Token)) != 0) {
6!
3094
    vError("vgId:%d, failed to check assigned log syncd since %s", TD_VID(pVnode), tstrerror(syncRsp.errCode));
×
3095
  }
3096

3097
  if ((code = vnodeUpdateArbTerm(pVnode, syncReq.arbTerm)) != 0) {
6!
3098
    vError("vgId:%d, failed to update arb term since %s", TD_VID(pVnode), tstrerror(code));
×
3099
    goto _OVER;
×
3100
  }
3101

3102
  int32_t contLen = tSerializeSVArbCheckSyncRsp(NULL, 0, &syncRsp);
6✔
3103
  if (contLen <= 0) {
6!
3104
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
3105
    code = -1;
×
3106
    goto _OVER;
×
3107
  }
3108
  void *pHead = rpcMallocCont(contLen);
6✔
3109
  if (!pHead) {
6!
3110
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
3111
    code = -1;
×
3112
    goto _OVER;
×
3113
  }
3114

3115
  if (tSerializeSVArbCheckSyncRsp(pHead, contLen, &syncRsp) <= 0) {
6!
3116
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
3117
    rpcFreeCont(pHead);
×
3118
    code = -1;
×
3119
    goto _OVER;
×
3120
  }
3121

3122
  pRsp->pCont = pHead;
6✔
3123
  pRsp->contLen = contLen;
6✔
3124

3125
  vInfo(
6!
3126
      "vgId:%d, suceed to process vnode-arb-check-sync req rsp.code:%s, arbToken:%s, member0Token:%s, "
3127
      "member1Token:%s",
3128
      TD_VID(pVnode), tstrerror(syncRsp.errCode), syncRsp.arbToken, syncRsp.member0Token, syncRsp.member1Token);
3129

3130
  code = TSDB_CODE_SUCCESS;
6✔
3131

3132
_OVER:
6✔
3133
  if (code != 0) {
6!
3134
    vError("vgId:%d, failed to process arb check req rsp.code:%s since %s", TD_VID(pVnode), tstrerror(syncRsp.errCode),
×
3135
           tstrerror(code));
3136
  }
3137
  tFreeSVArbCheckSyncReq(&syncReq);
6✔
3138
  return code;
6✔
3139
}
3140

3141
#ifndef TD_ENTERPRISE
3142
int32_t vnodeAsyncCompact(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) { return 0; }
3143
int32_t tsdbAsyncCompact(STsdb *tsdb, const STimeWindow *tw, bool sync) { return 0; }
3144
#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