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

taosdata / TDengine / #3620

21 Feb 2025 09:00AM UTC coverage: 63.573% (+0.2%) from 63.423%
#3620

push

travis-ci

web-flow
ci: taosBenchmark add coverage cases branch 3.0 (#29788)

* fix: add unit test for taos-tools

* fix: only .cpp include

* fix: remove no use function

* fix: restore toolsSys.c

* fix: add toolsSys case

* fix: rebuild error fixed

* fix: fix build error

* fix: support get vgroups with core and memory limit

* fix: build error for strcasecmp

* fix: add insertBasic.py case

* fix: add command line set vgroups=3

* fix: change with ns database

* toolscJson read with int replace float and add insertPrecison.py

* fix: add insertBindVGroup.json case

* fix: remove public fun removeQuotation

* fix: vgroups change method

* fix: memory leak for runInsertLimitThread slot

* insertPrecision.py word write wrong

* fix: check isFloat number

* fix: vgroups change logic error

* fix: insertBasic.py real and expect error

* fix: adjust default vgroups

* fix: adjust default vgroups modify comment

148962 of 300203 branches covered (49.62%)

Branch coverage included in aggregate %.

15 of 16 new or added lines in 1 file covered. (93.75%)

2018 existing lines in 133 files now uncovered.

233201 of 300933 relevant lines covered (77.49%)

18174406.98 hits per line

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

61.91
/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 "audit.h"
17
#include "cos.h"
18
#include "monitor.h"
19
#include "tencode.h"
20
#include "tglobal.h"
21
#include "tmsg.h"
22
#include "tstrbuild.h"
23
#include "vnd.h"
24
#include "vnode.h"
25
#include "vnodeInt.h"
26

27
extern taos_counter_t *tsInsertCounter;
28

29
static int32_t vnodeProcessCreateStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
30
static int32_t vnodeProcessAlterStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
31
static int32_t vnodeProcessDropStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
32
static int32_t vnodeProcessCreateTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
33
                                       SRpcMsg *pOriginRpc);
34
static int32_t vnodeProcessAlterTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
35
static int32_t vnodeProcessDropTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
36
                                     SRpcMsg *pOriginRpc);
37
static int32_t vnodeProcessSubmitReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
38
                                     SRpcMsg *pOriginalMsg);
39
static int32_t vnodeProcessCreateTSmaReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
40
static int32_t vnodeProcessAlterConfirmReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
41
static int32_t vnodeProcessAlterConfigReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
42
static int32_t vnodeProcessDropTtlTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
43
static int32_t vnodeProcessTrimReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
44
static int32_t vnodeProcessS3MigrateReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
45
static int32_t vnodeProcessDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
46
                                     SRpcMsg *pOriginalMsg);
47
static int32_t vnodeProcessBatchDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
48
static int32_t vnodeProcessCreateIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
49
static int32_t vnodeProcessDropIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
50
static int32_t vnodeProcessCompactVnodeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
51
static int32_t vnodeProcessConfigChangeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
52
static int32_t vnodeProcessArbCheckSyncReq(SVnode *pVnode, void *pReq, int32_t len, SRpcMsg *pRsp);
53
static int32_t vnodeProcessDropTSmaCtbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
54
                                          SRpcMsg *pOriginRpc);
55

56
static int32_t vnodeCheckToken(SVnode *pVnode, char *member0Token, char *member1Token);
57
static int32_t vnodeCheckSyncd(SVnode *pVnode, char *member0Token, char *member1Token);
58
static int32_t vnodeProcessFetchTtlExpiredTbs(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
59

60
extern int32_t vnodeProcessKillCompactReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
61
extern int32_t vnodeQueryCompactProgress(SVnode *pVnode, SRpcMsg *pMsg);
62

63
static int32_t vnodePreprocessCreateTableReq(SVnode *pVnode, SDecoder *pCoder, int64_t btime, int64_t *pUid) {
154,875✔
64
  int32_t code = 0;
154,875✔
65
  int32_t lino = 0;
154,875✔
66

67
  if (tStartDecode(pCoder) < 0) {
154,875!
68
    code = TSDB_CODE_INVALID_MSG;
×
69
    TSDB_CHECK_CODE(code, lino, _exit);
×
70
  }
71

72
  // flags
73
  if (tDecodeI32v(pCoder, NULL) < 0) {
154,911!
74
    code = TSDB_CODE_INVALID_MSG;
×
75
    TSDB_CHECK_CODE(code, lino, _exit);
×
76
  }
77

78
  // name
79
  char *name = NULL;
154,911✔
80
  if (tDecodeCStr(pCoder, &name) < 0) {
154,894!
81
    code = TSDB_CODE_INVALID_MSG;
×
82
    TSDB_CHECK_CODE(code, lino, _exit);
×
83
  }
84

85
  // uid
86
  int64_t uid = metaGetTableEntryUidByName(pVnode->pMeta, name);
154,894✔
87
  if (uid == 0) {
154,888✔
88
    uid = tGenIdPI64();
128,377✔
89
  }
90
  *(int64_t *)(pCoder->data + pCoder->pos) = uid;
154,916✔
91

92
  // btime
93
  *(int64_t *)(pCoder->data + pCoder->pos + 8) = btime;
154,916✔
94

95
  tEndDecode(pCoder);
154,916✔
96

97
_exit:
154,884✔
98
  if (code) {
154,884!
99
    vError("vgId:%d %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
100
  } else {
101
    vTrace("vgId:%d %s done, table:%s uid generated:%" PRId64, TD_VID(pVnode), __func__, name, uid);
154,884✔
102
    if (pUid) *pUid = uid;
154,884✔
103
  }
104
  return code;
154,881✔
105
}
106
static int32_t vnodePreProcessCreateTableMsg(SVnode *pVnode, SRpcMsg *pMsg) {
69,140✔
107
  int32_t code = 0;
69,140✔
108
  int32_t lino = 0;
69,140✔
109

110
  int64_t  btime = taosGetTimestampMs();
69,145✔
111
  SDecoder dc = {0};
69,145✔
112
  int32_t  nReqs;
113

114
  tDecoderInit(&dc, (uint8_t *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead));
69,145✔
115
  if (tStartDecode(&dc) < 0) {
69,141!
116
    code = TSDB_CODE_INVALID_MSG;
×
117
    return code;
×
118
  }
119

120
  if (tDecodeI32v(&dc, &nReqs) < 0) {
69,139!
121
    code = TSDB_CODE_INVALID_MSG;
×
122
    TSDB_CHECK_CODE(code, lino, _exit);
×
123
  }
124
  for (int32_t iReq = 0; iReq < nReqs; iReq++) {
161,517✔
125
    code = vnodePreprocessCreateTableReq(pVnode, &dc, btime, NULL);
92,408✔
126
    TSDB_CHECK_CODE(code, lino, _exit);
92,378!
127
  }
128

129
  tEndDecode(&dc);
69,109✔
130

131
_exit:
69,134✔
132
  tDecoderClear(&dc);
69,134✔
133
  if (code) {
69,145!
134
    vError("vgId:%d, %s:%d failed to preprocess submit request since %s, msg type:%s", TD_VID(pVnode), __func__, lino,
×
135
           tstrerror(code), TMSG_INFO(pMsg->msgType));
136
  }
137
  return code;
69,142✔
138
}
139

140
static int32_t vnodePreProcessAlterTableMsg(SVnode *pVnode, SRpcMsg *pMsg) {
968✔
141
  int32_t code = TSDB_CODE_INVALID_MSG;
968✔
142
  int32_t lino = 0;
968✔
143

144
  SDecoder dc = {0};
968✔
145
  tDecoderInit(&dc, (uint8_t *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead));
968✔
146

147
  SVAlterTbReq vAlterTbReq = {0};
968✔
148
  int64_t      ctimeMs = taosGetTimestampMs();
968✔
149
  if (tDecodeSVAlterTbReqSetCtime(&dc, &vAlterTbReq, ctimeMs) < 0) {
968!
150
    taosArrayDestroy(vAlterTbReq.pMultiTag);
×
151
    vAlterTbReq.pMultiTag = NULL;
×
152
    goto _exit;
×
153
  }
154
  taosArrayDestroy(vAlterTbReq.pMultiTag);
968✔
155
  vAlterTbReq.pMultiTag = NULL;
968✔
156

157
  code = 0;
968✔
158

159
_exit:
968✔
160
  tDecoderClear(&dc);
968✔
161
  if (code) {
968!
162
    vError("vgId:%d %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
163
  } else {
164
    vTrace("vgId:%d %s done, table:%s ctimeMs generated:%" PRId64, TD_VID(pVnode), __func__, vAlterTbReq.tbName,
968✔
165
           ctimeMs);
166
  }
167
  return code;
968✔
168
}
169

170
static int32_t vnodePreProcessDropTtlMsg(SVnode *pVnode, SRpcMsg *pMsg) {
99,909✔
171
  int32_t code = TSDB_CODE_INVALID_MSG;
99,909✔
172
  int32_t lino = 0;
99,909✔
173

174
  SMsgHead *pContOld = pMsg->pCont;
99,909✔
175
  int32_t   reqLenOld = pMsg->contLen - sizeof(SMsgHead);
99,909✔
176

177
  SArray *tbUids = NULL;
99,909✔
178
  int64_t timestampMs = 0;
99,909✔
179

180
  SVDropTtlTableReq ttlReq = {0};
99,909✔
181
  if (tDeserializeSVDropTtlTableReq((char *)pContOld + sizeof(SMsgHead), reqLenOld, &ttlReq) != 0) {
99,909!
182
    code = TSDB_CODE_INVALID_MSG;
×
183
    TSDB_CHECK_CODE(code, lino, _exit);
×
184
  }
185

186
  {  // find expired uids
187
    tbUids = taosArrayInit(8, sizeof(tb_uid_t));
100,291✔
188
    if (tbUids == NULL) {
100,359!
189
      code = terrno;
×
190
      TSDB_CHECK_CODE(code, lino, _exit);
×
191
    }
192

193
    timestampMs = (int64_t)ttlReq.timestampSec * 1000;
100,359✔
194
    code = metaTtlFindExpired(pVnode->pMeta, timestampMs, tbUids, ttlReq.ttlDropMaxCount);
100,359✔
195
    if (code != 0) {
100,097!
196
      code = TSDB_CODE_INVALID_MSG;
×
197
      TSDB_CHECK_CODE(code, lino, _exit);
×
198
    }
199

200
    ttlReq.nUids = taosArrayGetSize(tbUids);
100,097✔
201
    ttlReq.pTbUids = tbUids;
100,024✔
202
  }
203

204
  if (ttlReq.nUids == 0) {
100,024✔
205
    code = TSDB_CODE_MSG_PREPROCESSED;
100,004✔
206
    TSDB_CHECK_CODE(code, lino, _exit);
100,004!
207
  }
208

209
  {  // prepare new content
210
    int32_t reqLenNew = tSerializeSVDropTtlTableReq(NULL, 0, &ttlReq);
×
211
    int32_t contLenNew = reqLenNew + sizeof(SMsgHead);
5✔
212

213
    SMsgHead *pContNew = rpcMallocCont(contLenNew);
5✔
214
    if (pContNew == NULL) {
5!
215
      code = terrno;
×
216
      TSDB_CHECK_CODE(code, lino, _exit);
×
217
    }
218

219
    if (tSerializeSVDropTtlTableReq((char *)pContNew + sizeof(SMsgHead), reqLenNew, &ttlReq) != 0) {
5!
220
      vError("vgId:%d %s:%d failed to serialize drop ttl request", TD_VID(pVnode), __func__, lino);
5!
221
    }
222
    pContNew->contLen = htonl(reqLenNew);
5✔
223
    pContNew->vgId = pContOld->vgId;
5✔
224

225
    rpcFreeCont(pContOld);
5✔
226
    pMsg->pCont = pContNew;
5✔
227
    pMsg->contLen = contLenNew;
5✔
228
  }
229

230
  code = 0;
5✔
231

232
_exit:
100,067✔
233
  taosArrayDestroy(tbUids);
100,067✔
234

235
  if (code && code != TSDB_CODE_MSG_PREPROCESSED) {
100,253!
236
    vError("vgId:%d, %s:%d failed to preprocess drop ttl request since %s, msg type:%s", TD_VID(pVnode), __func__, lino,
×
237
           tstrerror(code), TMSG_INFO(pMsg->msgType));
238
  } else {
239
    vTrace("vgId:%d, %s done, timestampSec:%d, nUids:%d", TD_VID(pVnode), __func__, ttlReq.timestampSec, ttlReq.nUids);
100,253✔
240
  }
241

242
  return code;
100,093✔
243
}
244

245
extern int64_t tsMaxKeyByPrecision[];
246
static int32_t vnodePreProcessSubmitTbData(SVnode *pVnode, SDecoder *pCoder, int64_t btimeMs, int64_t ctimeMs) {
10,657,887✔
247
  int32_t code = 0;
10,657,887✔
248
  int32_t lino = 0;
10,657,887✔
249

250
  if (tStartDecode(pCoder) < 0) {
10,657,887!
251
    code = TSDB_CODE_INVALID_MSG;
×
252
    TSDB_CHECK_CODE(code, lino, _exit);
×
253
  }
254

255
  SSubmitTbData submitTbData;
256
  uint8_t       version;
257
  if (tDecodeI32v(pCoder, &submitTbData.flags) < 0) {
10,685,046!
258
    code = TSDB_CODE_INVALID_MSG;
×
259
    TSDB_CHECK_CODE(code, lino, _exit);
×
260
  }
261
  version = (submitTbData.flags >> 8) & 0xff;
10,685,046✔
262
  submitTbData.flags = submitTbData.flags & 0xff;
10,685,046✔
263

264
  int64_t uid;
265
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
10,685,046✔
266
    code = vnodePreprocessCreateTableReq(pVnode, pCoder, btimeMs, &uid);
62,477✔
267
    TSDB_CHECK_CODE(code, lino, _exit);
62,504!
268
  }
269

270
  // submit data
271
  if (tDecodeI64(pCoder, &submitTbData.suid) < 0) {
10,674,228!
272
    code = TSDB_CODE_INVALID_MSG;
×
273
    TSDB_CHECK_CODE(code, lino, _exit);
×
274
  }
275

276
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
10,674,228✔
277
    *(int64_t *)(pCoder->data + pCoder->pos) = uid;
62,499✔
278
    pCoder->pos += sizeof(int64_t);
62,499✔
279
  } else {
280
    if (tDecodeI64(pCoder, &submitTbData.uid) < 0) {
10,605,680!
281
      code = TSDB_CODE_INVALID_MSG;
×
282
      TSDB_CHECK_CODE(code, lino, _exit);
×
283
    }
284
  }
285

286
  if (tDecodeI32v(pCoder, &submitTbData.sver) < 0) {
10,654,408!
287
    code = TSDB_CODE_INVALID_MSG;
×
288
    TSDB_CHECK_CODE(code, lino, _exit);
×
289
  }
290

291
  // scan and check
292
  TSKEY now = btimeMs;
10,654,408✔
293
  if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_MICRO) {
10,654,408✔
294
    now *= 1000;
22,681✔
295
  } else if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_NANO) {
10,631,727✔
296
    now *= 1000000;
3,709✔
297
  }
298

299
  int32_t keep = pVnode->config.tsdbCfg.keep2;
10,654,408✔
300
  /*
301
  int32_t nlevel = tfsGetLevel(pVnode->pTfs);
302
  if (nlevel > 1 && tsS3Enabled) {
303
    if (nlevel == 3) {
304
      keep = pVnode->config.tsdbCfg.keep1;
305
    } else if (nlevel == 2) {
306
      keep = pVnode->config.tsdbCfg.keep0;
307
    }
308
  }
309
  */
310

311
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * keep;
10,654,408✔
312
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
10,654,408✔
313
  if (submitTbData.flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
10,654,408✔
314
    uint64_t nColData;
315
    if (tDecodeU64v(pCoder, &nColData) < 0) {
149!
316
      code = TSDB_CODE_INVALID_MSG;
×
317
      TSDB_CHECK_CODE(code, lino, _exit);
1!
318
    }
319

320
    SColData colData = {0};
149✔
321
    code = tDecodeColData(version, pCoder, &colData);
149✔
322
    if (code) {
149!
323
      code = TSDB_CODE_INVALID_MSG;
×
324
      TSDB_CHECK_CODE(code, lino, _exit);
×
325
    }
326

327
    if (colData.flag != HAS_VALUE) {
149!
328
      code = TSDB_CODE_INVALID_MSG;
×
329
      TSDB_CHECK_CODE(code, lino, _exit);
×
330
    }
331

332
    for (int32_t iRow = 0; iRow < colData.nVal; iRow++) {
415✔
333
      if (((TSKEY *)colData.pData)[iRow] < minKey || ((TSKEY *)colData.pData)[iRow] > maxKey) {
267!
334
        code = TSDB_CODE_TDB_TIMESTAMP_OUT_OF_RANGE;
1✔
335
        TSDB_CHECK_CODE(code, lino, _exit);
1!
336
      }
337
    }
338

339
    for (uint64_t i = 1; i < nColData; i++) {
637✔
340
      code = tDecodeColData(version, pCoder, &colData);
489✔
341
      if (code) {
489!
342
        code = TSDB_CODE_INVALID_MSG;
×
343
        TSDB_CHECK_CODE(code, lino, _exit);
×
344
      }
345
    }
346
  } else {
347
    uint64_t nRow;
348
    if (tDecodeU64v(pCoder, &nRow) < 0) {
10,654,771!
349
      code = TSDB_CODE_INVALID_MSG;
×
350
      TSDB_CHECK_CODE(code, lino, _exit);
497!
351
    }
352

353
    for (int32_t iRow = 0; iRow < nRow; ++iRow) {
675,686,372✔
354
      SRow *pRow = (SRow *)(pCoder->data + pCoder->pos);
665,032,098✔
355
      pCoder->pos += pRow->len;
665,032,098✔
356

357
      if (pRow->ts < minKey || pRow->ts > maxKey) {
665,032,098✔
358
        code = TSDB_CODE_TDB_TIMESTAMP_OUT_OF_RANGE;
6,680✔
359
        TSDB_CHECK_CODE(code, lino, _exit);
6,680✔
360
      }
361
    }
362
  }
363

364
  if (!tDecodeIsEnd(pCoder)) {
10,654,422!
365
    *(int64_t *)(pCoder->data + pCoder->pos) = ctimeMs;
10,659,879✔
366
    pCoder->pos += sizeof(int64_t);
10,659,879✔
367
  }
368

369
  tEndDecode(pCoder);
10,654,422✔
370

371
_exit:
10,656,837✔
372
  if (code) {
10,656,837✔
373
    vError("vgId:%d, %s:%d failed to vnodePreProcessSubmitTbData submit request since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
498!
374
  }
375
  return code;
10,657,409✔
376
}
377
static int32_t vnodePreProcessSubmitMsg(SVnode *pVnode, SRpcMsg *pMsg) {
9,993,510✔
378
  int32_t code = 0;
9,993,510✔
379
  int32_t lino = 0;
9,993,510✔
380

381
  if (tsBypassFlag & TSDB_BYPASS_RA_RPC_RECV_SUBMIT) {
9,993,510!
382
    return TSDB_CODE_MSG_PREPROCESSED;
×
383
  }
384

385
  SDecoder *pCoder = &(SDecoder){0};
9,993,510✔
386

387
  if (taosHton64(((SSubmitReq2Msg *)pMsg->pCont)->version) != 1) {
9,993,510!
388
    code = TSDB_CODE_INVALID_MSG;
×
389
    TSDB_CHECK_CODE(code, lino, _exit);
×
390
  }
391

392
  tDecoderInit(pCoder, (uint8_t *)pMsg->pCont + sizeof(SSubmitReq2Msg), pMsg->contLen - sizeof(SSubmitReq2Msg));
9,993,622✔
393

394
  if (tStartDecode(pCoder) < 0) {
9,993,476!
395
    code = TSDB_CODE_INVALID_MSG;
×
396
    TSDB_CHECK_CODE(code, lino, _exit);
×
397
  }
398

399
  uint64_t nSubmitTbData;
400
  if (tDecodeU64v(pCoder, &nSubmitTbData) < 0) {
9,993,523!
401
    code = TSDB_CODE_INVALID_MSG;
×
402
    TSDB_CHECK_CODE(code, lino, _exit);
×
403
  }
404

405
  int64_t btimeMs = taosGetTimestampMs();
9,993,695✔
406
  int64_t ctimeMs = btimeMs;
9,993,695✔
407
  for (int32_t i = 0; i < nSubmitTbData; i++) {
20,650,647✔
408
    code = vnodePreProcessSubmitTbData(pVnode, pCoder, btimeMs, ctimeMs);
10,658,335✔
409
    TSDB_CHECK_CODE(code, lino, _exit);
10,657,451✔
410
  }
411

412
  tEndDecode(pCoder);
9,992,312✔
413

414
_exit:
9,993,649✔
415
  tDecoderClear(pCoder);
9,993,649✔
416
  if (code) {
9,993,628✔
417
    vError("vgId:%d, %s:%d failed to preprocess submit request since %s, msg type:%s", TD_VID(pVnode), __func__, lino,
499!
418
           tstrerror(code), TMSG_INFO(pMsg->msgType));
419
  }
420
  return code;
9,993,624✔
421
}
422

423
static int32_t vnodePreProcessDeleteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
67,302✔
424
  int32_t code = 0;
67,302✔
425

426
  int32_t    size;
427
  int32_t    ret;
428
  uint8_t   *pCont;
429
  SEncoder  *pCoder = &(SEncoder){0};
67,302✔
430
  SDeleteRes res = {0};
67,302✔
431

432
  SReadHandle handle = {.vnode = pVnode, .pMsgCb = &pVnode->msgCb, .skipRollup = 1};
67,302✔
433
  initStorageAPI(&handle.api);
67,302✔
434

435
  code = qWorkerProcessDeleteMsg(&handle, pVnode->pQuery, pMsg, &res);
67,306✔
436
  if (code) goto _exit;
67,305!
437

438
  res.ctimeMs = taosGetTimestampMs();
67,306✔
439
  // malloc and encode
440
  tEncodeSize(tEncodeDeleteRes, &res, size, ret);
67,306!
441
  pCont = rpcMallocCont(size + sizeof(SMsgHead));
67,302✔
442

443
  ((SMsgHead *)pCont)->contLen = size + sizeof(SMsgHead);
67,306✔
444
  ((SMsgHead *)pCont)->vgId = TD_VID(pVnode);
67,306✔
445

446
  tEncoderInit(pCoder, pCont + sizeof(SMsgHead), size);
67,306✔
447
  if (tEncodeDeleteRes(pCoder, &res) != 0) {
67,307!
448
    vError("vgId:%d %s failed to encode delete response", TD_VID(pVnode), __func__);
×
449
  }
450
  tEncoderClear(pCoder);
67,306✔
451

452
  rpcFreeCont(pMsg->pCont);
67,306✔
453
  pMsg->pCont = pCont;
67,307✔
454
  pMsg->contLen = size + sizeof(SMsgHead);
67,307✔
455

456
  taosArrayDestroy(res.uidList);
67,307✔
457

458
_exit:
67,307✔
459
  return code;
67,307✔
460
}
461

462
static int32_t vnodePreProcessBatchDeleteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
2,615✔
463
  int32_t code = 0;
2,615✔
464
  int32_t lino = 0;
2,615✔
465

466
  int64_t         ctimeMs = taosGetTimestampMs();
2,616✔
467
  SBatchDeleteReq pReq = {0};
2,616✔
468
  SDecoder       *pCoder = &(SDecoder){0};
2,616✔
469

470
  tDecoderInit(pCoder, (uint8_t *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead));
2,616✔
471

472
  if (tDecodeSBatchDeleteReqSetCtime(pCoder, &pReq, ctimeMs) < 0) {
2,616!
473
    code = TSDB_CODE_INVALID_MSG;
×
474
  }
475

476
  tDecoderClear(pCoder);
2,616✔
477
  taosArrayDestroy(pReq.deleteReqs);
2,616✔
478

479
  if (code) {
2,617!
480
    vError("vgId:%d %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
481
  } else {
482
    vTrace("vgId:%d %s done, ctimeMs generated:%" PRId64, TD_VID(pVnode), __func__, ctimeMs);
2,617✔
483
  }
484
  return code;
2,617✔
485
}
486

487
static int32_t vnodePreProcessArbCheckSyncMsg(SVnode *pVnode, SRpcMsg *pMsg) {
2✔
488
  SVArbCheckSyncReq syncReq = {0};
2✔
489

490
  if (tDeserializeSVArbCheckSyncReq((char *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead),
2!
491
                                    &syncReq) != 0) {
492
    return TSDB_CODE_INVALID_MSG;
×
493
  }
494

495
  int32_t ret = vnodeCheckToken(pVnode, syncReq.member0Token, syncReq.member1Token);
2✔
496
  if (ret != 0) {
2!
UNCOV
497
    vError("vgId:%d, failed to preprocess arb check sync request since %s", TD_VID(pVnode), tstrerror(ret));
×
498
  }
499

500
  int32_t code = terrno;
2✔
501
  tFreeSVArbCheckSyncReq(&syncReq);
2✔
502

503
  return code;
2✔
504
}
505

506
int32_t vnodePreProcessDropTbMsg(SVnode *pVnode, SRpcMsg *pMsg) {
2,500✔
507
  int32_t          code = TSDB_CODE_SUCCESS;
2,500✔
508
  int32_t          lino = 0;
2,500✔
509
  int32_t          size = 0;
2,500✔
510
  SDecoder         dc = {0};
2,500✔
511
  SEncoder         ec = {0};
2,500✔
512
  SVDropTbBatchReq receivedBatchReqs = {0};
2,500✔
513
  SVDropTbBatchReq sentBatchReqs = {0};
2,500✔
514

515
  tDecoderInit(&dc, POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), pMsg->contLen - sizeof(SMsgHead));
2,500✔
516

517
  code = tDecodeSVDropTbBatchReq(&dc, &receivedBatchReqs);
2,500✔
518
  if (code < 0) {
2,500!
519
    terrno = code;
×
520
    TSDB_CHECK_CODE(code, lino, _exit);
×
521
  }
522
  sentBatchReqs.pArray = taosArrayInit(receivedBatchReqs.nReqs, sizeof(SVDropTbReq));
2,500✔
523
  if (!sentBatchReqs.pArray) {
2,500!
524
    code = terrno;
×
525
    goto _exit;
×
526
  }
527

528
  for (int32_t i = 0; i < receivedBatchReqs.nReqs; ++i) {
5,025✔
529
    SVDropTbReq *pReq = receivedBatchReqs.pReqs + i;
2,525✔
530
    tb_uid_t     uid = metaGetTableEntryUidByName(pVnode->pMeta, pReq->name);
2,525✔
531
    if (uid == 0) {
2,525!
532
      vWarn("vgId:%d, preprocess drop ctb: %s not found", TD_VID(pVnode), pReq->name);
×
533
      continue;
×
534
    }
535
    pReq->uid = uid;
2,525✔
536
    vDebug("vgId:%d %s for: %s, uid: %" PRId64, TD_VID(pVnode), __func__, pReq->name, pReq->uid);
2,525✔
537
    if (taosArrayPush(sentBatchReqs.pArray, pReq) == NULL) {
5,050!
538
      code = terrno;
×
539
      goto _exit;
×
540
    }
541
  }
542
  sentBatchReqs.nReqs = sentBatchReqs.pArray->size;
2,500✔
543

544
  tEncodeSize(tEncodeSVDropTbBatchReq, &sentBatchReqs, size, code);
2,500!
545
  tEncoderInit(&ec, POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), size);
2,500✔
546
  code = tEncodeSVDropTbBatchReq(&ec, &sentBatchReqs);
2,500✔
547
  tEncoderClear(&ec);
2,500✔
548
  if (code != TSDB_CODE_SUCCESS) {
2,500!
549
    vError("vgId:%d %s failed to encode drop tb batch req: %s", TD_VID(pVnode), __func__, tstrerror(code));
×
550
    TSDB_CHECK_CODE(code, lino, _exit);
×
551
  }
552

553
_exit:
2,500✔
554
  tDecoderClear(&dc);
2,500✔
555
  if (sentBatchReqs.pArray) {
2,500!
556
    taosArrayDestroy(sentBatchReqs.pArray);
2,500✔
557
  }
558
  return code;
2,500✔
559
}
560

561
int32_t vnodePreProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
10,343,960✔
562
  int32_t code = 0;
10,343,960✔
563

564
  switch (pMsg->msgType) {
10,343,960✔
565
    case TDMT_VND_CREATE_TABLE: {
69,145✔
566
      code = vnodePreProcessCreateTableMsg(pVnode, pMsg);
69,145✔
567
    } break;
69,141✔
568
    case TDMT_VND_ALTER_TABLE: {
968✔
569
      code = vnodePreProcessAlterTableMsg(pVnode, pMsg);
968✔
570
    } break;
968✔
571
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
100,339✔
572
    case TDMT_VND_DROP_TTL_TABLE: {
573
      code = vnodePreProcessDropTtlMsg(pVnode, pMsg);
100,339✔
574
    } break;
100,062✔
575
    case TDMT_VND_SUBMIT: {
9,993,631✔
576
      code = vnodePreProcessSubmitMsg(pVnode, pMsg);
9,993,631✔
577
    } break;
9,993,594✔
578
    case TDMT_VND_DELETE: {
67,303✔
579
      code = vnodePreProcessDeleteMsg(pVnode, pMsg);
67,303✔
580
    } break;
67,307✔
581
    case TDMT_VND_BATCH_DEL: {
2,616✔
582
      code = vnodePreProcessBatchDeleteMsg(pVnode, pMsg);
2,616✔
583
    } break;
2,616✔
584
    case TDMT_VND_ARB_CHECK_SYNC: {
2✔
585
      code = vnodePreProcessArbCheckSyncMsg(pVnode, pMsg);
2✔
586
    } break;
2✔
587
    case TDMT_VND_DROP_TABLE: {
2,500✔
588
      code = vnodePreProcessDropTbMsg(pVnode, pMsg);
2,500✔
589
    } break;
2,500✔
590
    default:
107,456✔
591
      break;
107,456✔
592
  }
593

594
  if (code && code != TSDB_CODE_MSG_PREPROCESSED) {
10,343,646✔
595
    vError("vgId:%d, failed to preprocess write request since %s, msg type:%s", TD_VID(pVnode), tstrerror(code),
499!
596
           TMSG_INFO(pMsg->msgType));
597
  }
598
  return code;
10,343,642✔
599
}
600

601
int32_t vnodeProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg, int64_t ver, SRpcMsg *pRsp) {
11,650,315✔
602
  int32_t code = 0;
11,650,315✔
603
  void   *ptr = NULL;
11,650,315✔
604
  void   *pReq;
605
  int32_t len;
606

607
  (void)taosThreadMutexLock(&pVnode->mutex);
11,650,315✔
608
  if (pVnode->disableWrite) {
11,650,834!
609
    (void)taosThreadMutexUnlock(&pVnode->mutex);
×
610
    vError("vgId:%d write is disabled for snapshot, version:%" PRId64, TD_VID(pVnode), ver);
×
611
    return TSDB_CODE_VND_WRITE_DISABLED;
×
612
  }
613
  (void)taosThreadMutexUnlock(&pVnode->mutex);
11,650,834✔
614

615
  if (ver <= pVnode->state.applied) {
11,650,835!
616
    vError("vgId:%d, duplicate write request. ver: %" PRId64 ", applied: %" PRId64 "", TD_VID(pVnode), ver,
×
617
           pVnode->state.applied);
618
    return terrno = TSDB_CODE_VND_DUP_REQUEST;
×
619
  }
620

621
  vDebug("vgId:%d, start to process write request %s, index:%" PRId64 ", applied:%" PRId64 ", state.applyTerm:%" PRId64
11,650,835!
622
         ", conn.applyTerm:%" PRId64 ", contLen:%d",
623
         TD_VID(pVnode), TMSG_INFO(pMsg->msgType), ver, pVnode->state.applied, pVnode->state.applyTerm,
624
         pMsg->info.conn.applyTerm, pMsg->contLen);
625

626
  if (!(pVnode->state.applyTerm <= pMsg->info.conn.applyTerm)) {
11,650,454!
627
    return terrno = TSDB_CODE_INTERNAL_ERROR;
×
628
  }
629

630
  if (!(pVnode->state.applied + 1 == ver)) {
11,650,454!
631
    return terrno = TSDB_CODE_INTERNAL_ERROR;
×
632
  }
633

634
  atomic_store_64(&pVnode->state.applied, ver);
11,650,454✔
635
  atomic_store_64(&pVnode->state.applyTerm, pMsg->info.conn.applyTerm);
11,650,851✔
636

637
  if (!syncUtilUserCommit(pMsg->msgType)) goto _exit;
11,650,818✔
638

639
  // skip header
640
  pReq = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
11,624,471✔
641
  len = pMsg->contLen - sizeof(SMsgHead);
11,624,471✔
642
  bool needCommit = false;
11,624,471✔
643

644
  switch (pMsg->msgType) {
11,624,471!
645
    /* META */
646
    case TDMT_VND_CREATE_STB:
28,545✔
647
      if (vnodeProcessCreateStbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
28,545✔
648
      break;
28,639✔
649
    case TDMT_VND_ALTER_STB:
7,459✔
650
      if (vnodeProcessAlterStbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
7,459!
651
      break;
7,472✔
652
    case TDMT_VND_DROP_STB:
2,346✔
653
      if (vnodeProcessDropStbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
2,346!
654
      break;
2,349✔
655
    case TDMT_VND_CREATE_TABLE:
77,855✔
656
      if (vnodeProcessCreateTbReq(pVnode, ver, pReq, len, pRsp, pMsg) < 0) goto _err;
77,855!
657
      break;
77,882✔
658
    case TDMT_VND_ALTER_TABLE:
974✔
659
      if (vnodeProcessAlterTbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
974!
660
      break;
974✔
661
    case TDMT_VND_DROP_TABLE:
2,542✔
662
      if (vnodeProcessDropTbReq(pVnode, ver, pReq, len, pRsp, pMsg) < 0) goto _err;
2,542!
663
      break;
2,542✔
664
    case TDMT_VND_DROP_TTL_TABLE:
×
665
      if (vnodeProcessDropTtlTbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
666
      break;
×
667
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
5✔
668
      if (vnodeProcessFetchTtlExpiredTbs(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
5!
669
      break;
5✔
670
    case TDMT_VND_TRIM:
232✔
671
      if (vnodeProcessTrimReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
232!
672
      break;
232✔
673
    case TDMT_VND_S3MIGRATE:
×
674
      if (vnodeProcessS3MigrateReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
675
      break;
×
676
    case TDMT_VND_CREATE_SMA:
31✔
677
      if (vnodeProcessCreateTSmaReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
31!
678
      break;
31✔
679
    /* TSDB */
680
    case TDMT_VND_SUBMIT:
11,340,801✔
681
      if (vnodeProcessSubmitReq(pVnode, ver, pMsg->pCont, pMsg->contLen, pRsp, pMsg) < 0) goto _err;
11,340,801✔
682
      break;
11,340,787✔
683
    case TDMT_VND_DELETE:
68,612✔
684
      if (vnodeProcessDeleteReq(pVnode, ver, pReq, len, pRsp, pMsg) < 0) goto _err;
68,612!
685
      break;
68,614✔
686
    case TDMT_VND_BATCH_DEL:
2,898✔
687
      if (vnodeProcessBatchDeleteReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
2,898!
688
      break;
2,901✔
689
    /* TQ */
690
    case TDMT_VND_TMQ_SUBSCRIBE:
3,113✔
691
      if (tqProcessSubscribeReq(pVnode->pTq, ver, pReq, len) < 0) {
3,113✔
692
        goto _err;
1✔
693
      }
694
      break;
3,117✔
695
    case TDMT_VND_TMQ_DELETE_SUB:
860✔
696
      if (tqProcessDeleteSubReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen) < 0) {
860!
697
        goto _err;
×
698
      }
699
      break;
860✔
700
    case TDMT_VND_TMQ_COMMIT_OFFSET:
11,297✔
701
      if (tqProcessOffsetCommitReq(pVnode->pTq, ver, pReq, len) < 0) {
11,297!
702
        goto _err;
×
703
      }
704
      break;
11,302✔
705
    case TDMT_VND_TMQ_ADD_CHECKINFO:
119✔
706
      if (tqProcessAddCheckInfoReq(pVnode->pTq, ver, pReq, len) < 0) {
119!
707
        goto _err;
×
708
      }
709
      break;
119✔
710
    case TDMT_VND_TMQ_DEL_CHECKINFO:
12✔
711
      if (tqProcessDelCheckInfoReq(pVnode->pTq, ver, pReq, len) < 0) {
12✔
712
        goto _err;
1✔
713
      }
714
      break;
11✔
715
    case TDMT_STREAM_TASK_DEPLOY: {
14,307✔
716
      if ((code = tqProcessTaskDeployReq(pVnode->pTq, ver, pReq, len)) != TSDB_CODE_SUCCESS) {
14,307!
717
        goto _err;
×
718
      }
719
    } break;
14,327✔
720
    case TDMT_STREAM_TASK_DROP: {
6,958✔
721
      if ((code = tqProcessTaskDropReq(pVnode->pTq, pMsg->pCont, pMsg->contLen)) < 0) {
6,958!
722
        goto _err;
×
723
      }
724
    } break;
6,977✔
725
    case TDMT_STREAM_TASK_UPDATE_CHKPT: {
5,478✔
726
      if ((code = tqProcessTaskUpdateCheckpointReq(pVnode->pTq, pMsg->pCont, pMsg->contLen)) < 0) {
5,478!
727
        goto _err;
×
728
      }
729
    } break;
5,483✔
730
    case TDMT_STREAM_CONSEN_CHKPT: {
239✔
731
      if (pVnode->restored && (code = tqProcessTaskConsenChkptIdReq(pVnode->pTq, pMsg)) < 0) {
239!
732
        goto _err;
×
733
      }
734

735
    } break;
239✔
736
    case TDMT_STREAM_TASK_PAUSE: {
2,122✔
737
      if (pVnode->restored && vnodeIsLeader(pVnode) &&
3,521!
738
          (code = tqProcessTaskPauseReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen)) < 0) {
1,398✔
739
        goto _err;
×
740
      }
741
    } break;
2,123✔
742
    case TDMT_STREAM_TASK_RESUME: {
3,795✔
743
      if (pVnode->restored && vnodeIsLeader(pVnode) &&
6,363!
744
          (code = tqProcessTaskResumeReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen)) < 0) {
2,567✔
745
        goto _err;
×
746
      }
747
    } break;
3,796✔
748
    case TDMT_VND_STREAM_TASK_RESET: {
14✔
749
      if (pVnode->restored && vnodeIsLeader(pVnode) && (code = tqProcessTaskResetReq(pVnode->pTq, pMsg)) < 0) {
14!
750
        goto _err;
×
751
      }
752

753
    } break;
14✔
754
    case TDMT_VND_ALTER_CONFIRM:
8,004✔
755
      needCommit = pVnode->config.hashChange;
8,004✔
756
      if (vnodeProcessAlterConfirmReq(pVnode, ver, pReq, len, pRsp) < 0) {
8,004!
757
        goto _err;
×
758
      }
759
      break;
8,004✔
760
    case TDMT_VND_ALTER_CONFIG:
749✔
761
      vnodeProcessAlterConfigReq(pVnode, ver, pReq, len, pRsp);
749✔
762
      break;
752✔
763
    case TDMT_VND_COMMIT:
28,434✔
764
      needCommit = true;
28,434✔
765
      break;
28,434✔
766
    case TDMT_VND_CREATE_INDEX:
965✔
767
      vnodeProcessCreateIndexReq(pVnode, ver, pReq, len, pRsp);
965✔
768
      break;
965✔
769
    case TDMT_VND_DROP_INDEX:
2,140✔
770
      vnodeProcessDropIndexReq(pVnode, ver, pReq, len, pRsp);
2,140✔
771
      break;
2,140✔
772
    case TDMT_VND_STREAM_CHECK_POINT_SOURCE:  // always return true
3,290✔
773
      tqProcessTaskCheckPointSourceReq(pVnode->pTq, pMsg, pRsp);
3,290✔
774
      break;
3,299✔
775
    case TDMT_VND_STREAM_TASK_UPDATE:  // always return true
64✔
776
      tqProcessTaskUpdateReq(pVnode->pTq, pMsg);
64✔
777
      break;
64✔
778
    case TDMT_VND_COMPACT:
203✔
779
      vnodeProcessCompactVnodeReq(pVnode, ver, pReq, len, pRsp);
203✔
780
      goto _exit;
203✔
781
    case TDMT_SYNC_CONFIG_CHANGE:
×
782
      vnodeProcessConfigChangeReq(pVnode, ver, pReq, len, pRsp);
×
783
      break;
×
784
#ifdef TD_ENTERPRISE
785
    case TDMT_VND_KILL_COMPACT:
×
786
      vnodeProcessKillCompactReq(pVnode, ver, pReq, len, pRsp);
×
787
      break;
×
788
#endif
789
    /* ARB */
790
    case TDMT_VND_ARB_CHECK_SYNC:
8✔
791
      vnodeProcessArbCheckSyncReq(pVnode, pReq, len, pRsp);
8✔
792
      break;
8✔
793
    default:
×
794
      vError("vgId:%d, unprocessed msg, %d", TD_VID(pVnode), pMsg->msgType);
×
795
      return TSDB_CODE_INVALID_MSG;
×
796
  }
797

798
  vTrace("vgId:%d, process %s request, code:0x%x index:%" PRId64, TD_VID(pVnode), TMSG_INFO(pMsg->msgType), pRsp->code,
11,624,462!
799
         ver);
800

801
  walApplyVer(pVnode->pWal, ver);
11,624,462✔
802

803
  code = tqPushMsg(pVnode->pTq, pMsg->msgType);
11,624,410✔
804
  if (code) {
11,624,610✔
805
    vError("vgId:%d, failed to push msg to TQ since %s", TD_VID(pVnode), tstrerror(terrno));
566!
806
    return code;
566✔
807
  }
808

809
  // commit if need
810
  if (needCommit) {
11,624,044✔
811
    vInfo("vgId:%d, commit at version %" PRId64, TD_VID(pVnode), ver);
27,932!
812
    code = vnodeAsyncCommit(pVnode);
27,949✔
813
    if (code) {
27,958!
UNCOV
814
      vError("vgId:%d, failed to vnode async commit since %s.", TD_VID(pVnode), tstrerror(terrno));
×
UNCOV
815
      goto _err;
×
816
    }
817

818
    // start a new one
819
    code = vnodeBegin(pVnode);
27,958✔
820
    if (code) {
27,954✔
821
      vError("vgId:%d, failed to begin vnode since %s.", TD_VID(pVnode), tstrerror(terrno));
32!
822
      goto _err;
×
823
    }
824
  }
825

826
_exit:
11,624,034✔
827
  return 0;
11,649,967✔
828

829
_err:
18✔
830
  vError("vgId:%d, process %s request failed since %s, ver:%" PRId64, TD_VID(pVnode), TMSG_INFO(pMsg->msgType),
18!
831
         tstrerror(terrno), ver);
832
  return code;
18✔
833
}
834

835
int32_t vnodePreprocessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg) {
8,280,437✔
836
  if (TDMT_SCH_QUERY != pMsg->msgType && TDMT_SCH_MERGE_QUERY != pMsg->msgType) {
8,280,437✔
837
    return 0;
596,990✔
838
  }
839

840
  return qWorkerPreprocessQueryMsg(pVnode->pQuery, pMsg, TDMT_SCH_QUERY == pMsg->msgType);
7,683,447✔
841
}
842

843
int32_t vnodeProcessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
8,292,079✔
844
  vTrace("message in vnode query queue is processing");
8,292,079✔
845
  if (pMsg->msgType == TDMT_VND_TMQ_CONSUME && !syncIsReadyForRead(pVnode->sync)) {
8,292,079✔
846
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
1,263✔
847
    return 0;
1,263✔
848
  }
849

850
  if (pMsg->msgType == TDMT_VND_TMQ_CONSUME && !pVnode->restored) {
8,290,810!
851
    vnodeRedirectRpcMsg(pVnode, pMsg, TSDB_CODE_SYN_RESTORING);
×
852
    return 0;
×
853
  }
854

855
  SReadHandle handle = {.vnode = pVnode, .pMsgCb = &pVnode->msgCb, .pWorkerCb = pInfo->workerCb};
8,290,810✔
856
  initStorageAPI(&handle.api);
8,290,810✔
857
  int32_t code = TSDB_CODE_SUCCESS;
8,289,342✔
858
  bool    redirected = false;
8,289,342✔
859

860
  switch (pMsg->msgType) {
8,289,342✔
861
    case TDMT_SCH_QUERY:
5,909,093✔
862
      if (!syncIsReadyForRead(pVnode->sync)) {
5,909,093✔
863
        pMsg->code = (terrno) ? terrno : TSDB_CODE_SYN_NOT_LEADER;
3,109!
864
        redirected = true;
3,109✔
865
      }
866
      code = qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
5,909,777✔
867
      if (redirected) {
5,907,927✔
868
        vnodeRedirectRpcMsg(pVnode, pMsg, pMsg->code);
2,001✔
869
        return 0;
3,109✔
870
      }
871
      return code;
5,905,926✔
872
    case TDMT_SCH_MERGE_QUERY:
1,784,301✔
873
      return qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
1,784,301✔
874
    case TDMT_SCH_QUERY_CONTINUE:
501,772✔
875
      return qWorkerProcessCQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
501,772✔
876
    case TDMT_VND_TMQ_CONSUME:
69,399✔
877
      return tqProcessPollReq(pVnode->pTq, pMsg);
69,399✔
878
    case TDMT_VND_TMQ_CONSUME_PUSH:
24,554✔
879
      return tqProcessPollPush(pVnode->pTq, pMsg);
24,554✔
880
    default:
223✔
881
      vError("unknown msg type:%d in query queue", pMsg->msgType);
223!
882
      return TSDB_CODE_APP_ERROR;
×
883
  }
884
}
885

886
int32_t vnodeProcessFetchMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
20,918,719✔
887
  vTrace("vgId:%d, msg:%p in fetch queue is processing", pVnode->config.vgId, pMsg);
20,918,719✔
888
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
20,918,720!
889
       pMsg->msgType == TDMT_VND_BATCH_META || pMsg->msgType == TDMT_VND_TABLE_NAME) &&
14,581,126✔
890
      !syncIsReadyForRead(pVnode->sync)) {
7,422,900✔
891
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
2,076✔
892
    return 0;
2,076✔
893
  }
894

895
  switch (pMsg->msgType) {
20,920,347!
896
    case TDMT_SCH_FETCH:
10,199,150✔
897
    case TDMT_SCH_MERGE_FETCH:
898
      return qWorkerProcessFetchMsg(pVnode, pVnode->pQuery, pMsg, 0);
10,199,150✔
899
    case TDMT_SCH_FETCH_RSP:
×
900
      return qWorkerProcessRspMsg(pVnode, pVnode->pQuery, pMsg, 0);
×
901
    // case TDMT_SCH_CANCEL_TASK:
902
    //   return qWorkerProcessCancelMsg(pVnode, pVnode->pQuery, pMsg, 0);
903
    case TDMT_SCH_DROP_TASK:
7,685,062✔
904
      return qWorkerProcessDropMsg(pVnode, pVnode->pQuery, pMsg, 0);
7,685,062✔
905
    case TDMT_SCH_TASK_NOTIFY:
35✔
906
      return qWorkerProcessNotifyMsg(pVnode, pVnode->pQuery, pMsg, 0);
35✔
907
    case TDMT_SCH_QUERY_HEARTBEAT:
1,953,967✔
908
      return qWorkerProcessHbMsg(pVnode, pVnode->pQuery, pMsg, 0);
1,953,967✔
909
    case TDMT_VND_TABLE_META:
2,082✔
910
    case TDMT_VND_TABLE_NAME:
911
      return vnodeGetTableMeta(pVnode, pMsg, true);
2,082✔
912
    case TDMT_VND_TABLE_CFG:
×
913
      return vnodeGetTableCfg(pVnode, pMsg, true);
×
914
    case TDMT_VND_BATCH_META:
1,079,954✔
915
      return vnodeGetBatchMeta(pVnode, pMsg);
1,079,954✔
916
#ifdef TD_ENTERPRISE
917
    case TDMT_VND_QUERY_COMPACT_PROGRESS:
58✔
918
      return vnodeQueryCompactProgress(pVnode, pMsg);
58✔
919
#endif
920
      //    case TDMT_VND_TMQ_CONSUME:
921
      //      return tqProcessPollReq(pVnode->pTq, pMsg);
922
    case TDMT_VND_TMQ_VG_WALINFO:
13✔
923
      return tqProcessVgWalInfoReq(pVnode->pTq, pMsg);
13✔
924
    case TDMT_VND_TMQ_VG_COMMITTEDINFO:
2✔
925
      return tqProcessVgCommittedInfoReq(pVnode->pTq, pMsg);
2✔
926
    case TDMT_VND_TMQ_SEEK:
24✔
927
      return tqProcessSeekReq(pVnode->pTq, pMsg);
24✔
928

929
    default:
×
930
      vError("unknown msg type:%d in fetch queue", pMsg->msgType);
×
931
      return TSDB_CODE_APP_ERROR;
×
932
  }
933
}
934

935
int32_t vnodeProcessStreamMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
172,631✔
936
  vTrace("vgId:%d, msg:%p in stream queue is processing", pVnode->config.vgId, pMsg);
172,631✔
937
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
172,631!
938
       pMsg->msgType == TDMT_VND_BATCH_META) &&
172,653✔
939
      !syncIsReadyForRead(pVnode->sync)) {
×
940
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
×
941
    return 0;
×
942
  }
943

944
  switch (pMsg->msgType) {
172,651!
945
    case TDMT_STREAM_TASK_RUN:
169,070✔
946
      return tqProcessTaskRunReq(pVnode->pTq, pMsg);
169,070✔
947
    case TDMT_STREAM_RETRIEVE:
556✔
948
      return tqProcessTaskRetrieveReq(pVnode->pTq, pMsg);
556✔
949
    case TDMT_STREAM_RETRIEVE_RSP:
442✔
950
      return tqProcessTaskRetrieveRsp(pVnode->pTq, pMsg);
442✔
951
    case TDMT_VND_STREAM_SCAN_HISTORY:
2,583✔
952
      return tqProcessTaskScanHistory(pVnode->pTq, pMsg);
2,583✔
UNCOV
953
    case TDMT_VND_GET_STREAM_PROGRESS:
×
UNCOV
954
      return tqStreamProgressRetrieveReq(pVnode->pTq, pMsg);
×
UNCOV
955
    default:
×
UNCOV
956
      vError("unknown msg type:%d in stream queue", pMsg->msgType);
×
957
      return TSDB_CODE_APP_ERROR;
×
958
  }
959
}
960

961
int32_t vnodeProcessStreamCtrlMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
213,263✔
962
  vTrace("vgId:%d, msg:%p in stream ctrl queue is processing", pVnode->config.vgId, pMsg);
213,263✔
963
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
213,263!
964
       pMsg->msgType == TDMT_VND_BATCH_META) &&
213,263!
UNCOV
965
      !syncIsReadyForRead(pVnode->sync)) {
×
UNCOV
966
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
×
967
    return 0;
×
968
  }
969

970
  switch (pMsg->msgType) {
213,263!
971
    case TDMT_MND_STREAM_HEARTBEAT_RSP:
25,199✔
972
      return tqProcessStreamHbRsp(pVnode->pTq, pMsg);
25,199✔
973
    case TDMT_STREAM_TASK_DISPATCH:
58,627✔
974
      return tqProcessTaskDispatchReq(pVnode->pTq, pMsg);
58,627✔
975
    case TDMT_STREAM_TASK_DISPATCH_RSP:
58,657✔
976
      return tqProcessTaskDispatchRsp(pVnode->pTq, pMsg);
58,657✔
977
    case TDMT_VND_STREAM_TASK_CHECK:
22,109✔
978
      return tqProcessTaskCheckReq(pVnode->pTq, pMsg);
22,109✔
979
    case TDMT_VND_STREAM_TASK_CHECK_RSP:
22,566✔
980
      return tqProcessTaskCheckRsp(pVnode->pTq, pMsg);
22,566✔
981
    case TDMT_STREAM_TASK_CHECKPOINT_READY:
7,815✔
982
      return tqProcessTaskCheckpointReadyMsg(pVnode->pTq, pMsg);
7,815✔
983
    case TDMT_STREAM_TASK_CHECKPOINT_READY_RSP:
7,812✔
984
      return tqProcessTaskCheckpointReadyRsp(pVnode->pTq, pMsg);
7,812✔
985
    case TDMT_STREAM_RETRIEVE_TRIGGER:
2✔
986
      return tqProcessTaskRetrieveTriggerReq(pVnode->pTq, pMsg);
2✔
987
    case TDMT_STREAM_RETRIEVE_TRIGGER_RSP:
2✔
988
      return tqProcessTaskRetrieveTriggerRsp(pVnode->pTq, pMsg);
2✔
989
    case TDMT_MND_STREAM_REQ_CHKPT_RSP:
4,371✔
990
      return tqProcessStreamReqCheckpointRsp(pVnode->pTq, pMsg);
4,371✔
991
    case TDMT_MND_STREAM_CHKPT_REPORT_RSP:
6,103✔
992
      return tqProcessTaskChkptReportRsp(pVnode->pTq, pMsg);
6,103✔
993
    default:
×
994
      vError("unknown msg type:%d in stream ctrl queue", pMsg->msgType);
×
995
      return TSDB_CODE_APP_ERROR;
×
996
  }
997
}
998

999
void smaHandleRes(void *pVnode, int64_t smaId, const SArray *data) {
4✔
1000
  int32_t code = tdProcessTSmaInsert(((SVnode *)pVnode)->pSma, smaId, (const char *)data);
4✔
1001
  if (code) {
4!
1002
    vError("failed to process sma result since %s", tstrerror(code));
×
1003
  }
1004
}
4✔
1005

1006
void vnodeUpdateMetaRsp(SVnode *pVnode, STableMetaRsp *pMetaRsp) {
152,731✔
1007
  if (NULL == pMetaRsp) {
152,731!
1008
    return;
×
1009
  }
1010

1011
  tstrncpy(pMetaRsp->dbFName, pVnode->config.dbname, TSDB_DB_FNAME_LEN);
152,731✔
1012
  pMetaRsp->dbId = pVnode->config.dbId;
152,731✔
1013
  pMetaRsp->vgId = TD_VID(pVnode);
152,731✔
1014
  pMetaRsp->precision = pVnode->config.tsdbCfg.precision;
152,731✔
1015
}
1016

1017
extern int32_t vnodeAsyncRetention(SVnode *pVnode, int64_t now);
1018

1019
static int32_t vnodeProcessTrimReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
232✔
1020
  if (!pVnode->restored) {
232✔
1021
    vInfo("vgId:%d, ignore trim req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
7!
1022
    return 0;
7✔
1023
  }
1024

1025
  int32_t     code = 0;
225✔
1026
  SVTrimDbReq trimReq = {0};
225✔
1027

1028
  // decode
1029
  if (tDeserializeSVTrimDbReq(pReq, len, &trimReq) != 0) {
225!
1030
    code = TSDB_CODE_INVALID_MSG;
×
1031
    goto _exit;
×
1032
  }
1033

1034
  vInfo("vgId:%d, trim vnode request will be processed, time:%d", pVnode->config.vgId, trimReq.timestamp);
225!
1035

1036
  code = vnodeAsyncRetention(pVnode, trimReq.timestamp);
225✔
1037

1038
_exit:
225✔
1039
  return code;
225✔
1040
}
1041

1042
extern int32_t vnodeAsyncS3Migrate(SVnode *pVnode, int64_t now);
1043

1044
static int32_t vnodeProcessS3MigrateReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1045
  int32_t          code = 0;
×
1046
  SVS3MigrateDbReq s3migrateReq = {0};
×
1047

1048
  // decode
1049
  if (tDeserializeSVS3MigrateDbReq(pReq, len, &s3migrateReq) != 0) {
×
1050
    code = TSDB_CODE_INVALID_MSG;
×
1051
    goto _exit;
×
1052
  }
1053

1054
  vInfo("vgId:%d, s3migrate vnode request will be processed, time:%d", pVnode->config.vgId, s3migrateReq.timestamp);
×
1055

1056
  code = vnodeAsyncS3Migrate(pVnode, s3migrateReq.timestamp);
×
1057

1058
_exit:
×
1059
  return code;
×
1060
}
1061

1062
static int32_t vnodeProcessDropTtlTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1063
  int               ret = 0;
×
1064
  SVDropTtlTableReq ttlReq = {0};
×
1065
  if (tDeserializeSVDropTtlTableReq(pReq, len, &ttlReq) != 0) {
×
1066
    ret = TSDB_CODE_INVALID_MSG;
×
1067
    goto end;
×
1068
  }
1069

1070
  if (ttlReq.nUids != taosArrayGetSize(ttlReq.pTbUids)) {
×
1071
    ret = TSDB_CODE_INVALID_MSG;
×
1072
    goto end;
×
1073
  }
1074

1075
  if (ttlReq.nUids != 0) {
×
1076
    vInfo("vgId:%d, drop ttl table req will be processed, time:%d, ntbUids:%d", pVnode->config.vgId,
×
1077
          ttlReq.timestampSec, ttlReq.nUids);
1078
  }
1079

1080
  if (ttlReq.nUids > 0) {
×
1081
    int32_t code = metaDropMultipleTables(pVnode->pMeta, ver, ttlReq.pTbUids);
×
1082
    if (code) return code;
×
1083

1084
    code = tqUpdateTbUidList(pVnode->pTq, ttlReq.pTbUids, false);
×
1085
    if (code) {
×
1086
      vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1087
    }
1088
  }
1089

1090
end:
×
1091
  taosArrayDestroy(ttlReq.pTbUids);
×
1092
  return ret;
×
1093
}
1094

1095
static int32_t vnodeProcessFetchTtlExpiredTbs(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
5✔
1096
  int32_t                 code = -1;
5✔
1097
  SMetaReader             mr = {0};
5✔
1098
  SVDropTtlTableReq       ttlReq = {0};
5✔
1099
  SVFetchTtlExpiredTbsRsp rsp = {0};
5✔
1100
  SEncoder                encoder = {0};
5✔
1101
  SArray                 *pNames = NULL;
5✔
1102
  pRsp->msgType = TDMT_VND_FETCH_TTL_EXPIRED_TBS_RSP;
5✔
1103
  pRsp->code = TSDB_CODE_SUCCESS;
5✔
1104
  pRsp->pCont = NULL;
5✔
1105
  pRsp->contLen = 0;
5✔
1106

1107
  if (tDeserializeSVDropTtlTableReq(pReq, len, &ttlReq) != 0) {
5!
1108
    terrno = TSDB_CODE_INVALID_MSG;
×
1109
    goto _end;
×
1110
  }
1111

1112
  if (!(ttlReq.nUids == taosArrayGetSize(ttlReq.pTbUids))) {
5!
1113
    terrno = TSDB_CODE_INVALID_MSG;
×
1114
    goto _end;
×
1115
  }
1116

1117
  tb_uid_t    suid;
1118
  char        ctbName[TSDB_TABLE_NAME_LEN];
1119
  SVDropTbReq expiredTb = {.igNotExists = true};
5✔
1120
  metaReaderDoInit(&mr, pVnode->pMeta, 0);
5✔
1121
  rsp.vgId = TD_VID(pVnode);
5✔
1122
  rsp.pExpiredTbs = taosArrayInit(ttlReq.nUids, sizeof(SVDropTbReq));
5✔
1123
  if (!rsp.pExpiredTbs) goto _end;
5!
1124

1125
  pNames = taosArrayInit(ttlReq.nUids, TSDB_TABLE_NAME_LEN);
5✔
1126
  if (!pNames) {
5!
1127
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1128
    goto _end;
×
1129
  }
1130
  char buf[TSDB_TABLE_NAME_LEN];
1131
  for (int32_t i = 0; i < ttlReq.nUids; ++i) {
10✔
1132
    tb_uid_t *uid = taosArrayGet(ttlReq.pTbUids, i);
5✔
1133
    expiredTb.suid = *uid;
5✔
1134
    terrno = metaReaderGetTableEntryByUid(&mr, *uid);
5✔
1135
    if (terrno < 0) goto _end;
5!
1136
    tstrncpy(buf, mr.me.name, TSDB_TABLE_NAME_LEN);
5✔
1137
    void *p = taosArrayPush(pNames, buf);
5✔
1138
    if (p == NULL) {
5!
1139
      goto _end;
×
1140
    }
1141

1142
    expiredTb.name = p;
5✔
1143
    if (mr.me.type == TSDB_CHILD_TABLE) {
5!
1144
      expiredTb.suid = mr.me.ctbEntry.suid;
5✔
1145
    }
1146

1147
    if (taosArrayPush(rsp.pExpiredTbs, &expiredTb) == NULL) {
10!
1148
      goto _end;
×
1149
    }
1150
  }
1151

1152
  int32_t ret = 0;
5✔
1153
  tEncodeSize(tEncodeVFetchTtlExpiredTbsRsp, &rsp, pRsp->contLen, ret);
5!
1154
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
5✔
1155
  if (pRsp->pCont == NULL) {
5!
1156
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1157
    code = -1;
×
1158
    goto _end;
×
1159
  }
1160
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
5✔
1161
  terrno = tEncodeVFetchTtlExpiredTbsRsp(&encoder, &rsp);
5✔
1162
  tEncoderClear(&encoder);
5✔
1163

1164
  if (terrno == 0) code = 0;
5!
1165
_end:
×
1166
  metaReaderClear(&mr);
5✔
1167
  tFreeFetchTtlExpiredTbsRsp(&rsp);
5✔
1168
  taosArrayDestroy(ttlReq.pTbUids);
5✔
1169
  if (pNames) taosArrayDestroy(pNames);
5!
1170
  pRsp->code = terrno;
5✔
1171
  return code;
5✔
1172
}
1173

1174
static int32_t vnodeProcessCreateStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
28,540✔
1175
  int32_t        code = 0;
28,540✔
1176
  SVCreateStbReq req = {0};
28,540✔
1177
  SDecoder       coder;
1178

1179
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
28,540✔
1180
  pRsp->code = TSDB_CODE_SUCCESS;
28,540✔
1181
  pRsp->pCont = NULL;
28,540✔
1182
  pRsp->contLen = 0;
28,540✔
1183

1184
  // decode and process req
1185
  tDecoderInit(&coder, pReq, len);
28,540✔
1186

1187
  code = tDecodeSVCreateStbReq(&coder, &req);
28,541✔
1188
  if (code) {
28,508!
1189
    pRsp->code = code;
×
1190
    goto _err;
×
1191
  }
1192

1193
  code = metaCreateSuperTable(pVnode->pMeta, ver, &req);
28,508✔
1194
  if (code) {
28,670✔
1195
    pRsp->code = code;
4✔
1196
    goto _err;
4✔
1197
  }
1198

1199
  if ((code = tdProcessRSmaCreate(pVnode->pSma, &req)) < 0) {
28,666!
1200
    pRsp->code = code;
×
1201
    goto _err;
×
1202
  }
1203

1204
  tDecoderClear(&coder);
28,655✔
1205
  return 0;
28,626✔
1206

1207
_err:
4✔
1208
  tDecoderClear(&coder);
4✔
1209
  return code;
4✔
1210
}
1211

1212
static int32_t vnodeProcessCreateTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
77,854✔
1213
                                       SRpcMsg *pOriginRpc) {
1214
  SDecoder           decoder = {0};
77,854✔
1215
  SEncoder           encoder = {0};
77,854✔
1216
  int32_t            rcode = 0;
77,854✔
1217
  SVCreateTbBatchReq req = {0};
77,854✔
1218
  SVCreateTbReq     *pCreateReq;
1219
  SVCreateTbBatchRsp rsp = {0};
77,854✔
1220
  SVCreateTbRsp      cRsp = {0};
77,854✔
1221
  char               tbName[TSDB_TABLE_FNAME_LEN];
1222
  STbUidStore       *pStore = NULL;
77,854✔
1223
  SArray            *tbUids = NULL;
77,854✔
1224
  SArray            *tbNames = NULL;
77,854✔
1225
  pRsp->msgType = TDMT_VND_CREATE_TABLE_RSP;
77,854✔
1226
  pRsp->code = TSDB_CODE_SUCCESS;
77,854✔
1227
  pRsp->pCont = NULL;
77,854✔
1228
  pRsp->contLen = 0;
77,854✔
1229

1230
  // decode
1231
  tDecoderInit(&decoder, pReq, len);
77,854✔
1232
  if (tDecodeSVCreateTbBatchReq(&decoder, &req) < 0) {
77,855!
1233
    rcode = -1;
×
1234
    terrno = TSDB_CODE_INVALID_MSG;
×
1235
    goto _exit;
×
1236
  }
1237

1238
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(cRsp));
77,871✔
1239
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
77,889✔
1240
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
77,889✔
1241
  if (rsp.pArray == NULL || tbUids == NULL || tbNames == NULL) {
77,882!
1242
    rcode = -1;
1✔
1243
    terrno = TSDB_CODE_OUT_OF_MEMORY;
1✔
1244
    goto _exit;
×
1245
  }
1246

1247
  // loop to create table
1248
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
194,395✔
1249
    pCreateReq = req.pReqs + iReq;
116,501✔
1250
    memset(&cRsp, 0, sizeof(cRsp));
116,501✔
1251

1252
    if (tsEnableAudit && tsEnableAuditCreateTable) {
116,501!
1253
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
116,500!
1254
      if (str == NULL) {
116,510!
1255
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1256
        rcode = -1;
×
1257
        goto _exit;
×
1258
      }
1259
      tstrncpy(str, pCreateReq->name, TSDB_TABLE_FNAME_LEN);
116,510✔
1260
      if (taosArrayPush(tbNames, &str) == NULL) {
116,515!
1261
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1262
        rcode = -1;
×
1263
        goto _exit;
×
1264
      }
1265
    }
1266

1267
    // validate hash
1268
    (void)tsnprintf(tbName, TSDB_TABLE_FNAME_LEN, "%s.%s", pVnode->config.dbname, pCreateReq->name);
116,516✔
1269
    if (vnodeValidateTableHash(pVnode, tbName) < 0) {
116,509✔
1270
      cRsp.code = TSDB_CODE_VND_HASH_MISMATCH;
19✔
1271
      if (taosArrayPush(rsp.pArray, &cRsp) == NULL) {
38!
1272
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1273
        rcode = -1;
×
1274
        goto _exit;
×
1275
      }
1276
      vError("vgId:%d create-table:%s failed due to hash value mismatch", TD_VID(pVnode), tbName);
19!
1277
      continue;
19✔
1278
    }
1279

1280
    // do create table
1281
    if (metaCreateTable2(pVnode->pMeta, ver, pCreateReq, &cRsp.pMeta) < 0) {
116,479✔
1282
      if (pCreateReq->flags & TD_CREATE_IF_NOT_EXISTS && terrno == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
1,384!
1283
        cRsp.code = TSDB_CODE_SUCCESS;
4✔
1284
      } else {
1285
        cRsp.code = terrno;
1,380✔
1286
      }
1287
    } else {
1288
      cRsp.code = TSDB_CODE_SUCCESS;
115,117✔
1289
      if (tdFetchTbUidList(pVnode->pSma, &pStore, pCreateReq->ctb.suid, pCreateReq->uid) < 0) {
115,117!
1290
        vError("vgId:%d, failed to fetch tbUid list", TD_VID(pVnode));
×
1291
      }
1292
      if (taosArrayPush(tbUids, &pCreateReq->uid) == NULL) {
230,228!
1293
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1294
        rcode = -1;
×
1295
        goto _exit;
×
1296
      }
1297
      vnodeUpdateMetaRsp(pVnode, cRsp.pMeta);
115,113✔
1298
    }
1299

1300
    if (taosArrayPush(rsp.pArray, &cRsp) == NULL) {
232,993!
1301
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1302
      rcode = -1;
×
1303
      goto _exit;
×
1304
    }
1305
  }
1306

1307
  vDebug("vgId:%d, add %d new created tables into query table list", TD_VID(pVnode), (int32_t)taosArrayGetSize(tbUids));
77,894✔
1308
  if (tqUpdateTbUidList(pVnode->pTq, tbUids, true) < 0) {
77,894!
1309
    vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(terrno));
×
1310
  }
1311
  if (tdUpdateTbUidList(pVnode->pSma, pStore, true) < 0) {
77,885!
1312
    goto _exit;
×
1313
  }
1314
  pStore = tdUidStoreFree(pStore);
77,882✔
1315

1316
  // prepare rsp
1317
  int32_t ret = 0;
77,882✔
1318
  tEncodeSize(tEncodeSVCreateTbBatchRsp, &rsp, pRsp->contLen, ret);
77,882!
1319
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
77,883✔
1320
  if (pRsp->pCont == NULL) {
77,887!
1321
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1322
    rcode = -1;
×
1323
    goto _exit;
×
1324
  }
1325
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
77,887✔
1326
  if (tEncodeSVCreateTbBatchRsp(&encoder, &rsp) != 0) {
77,888!
1327
    vError("vgId:%d, failed to encode create table batch response", TD_VID(pVnode));
×
1328
  }
1329

1330
  if (tsEnableAudit && tsEnableAuditCreateTable) {
77,887!
1331
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
77,885✔
1332

1333
    SName name = {0};
77,885✔
1334
    if (tNameFromString(&name, pVnode->config.dbname, T_NAME_ACCT | T_NAME_DB) < 0) {
77,885!
1335
      vError("vgId:%d, failed to get name from string", TD_VID(pVnode));
×
1336
    }
1337

1338
    SStringBuilder sb = {0};
77,882✔
1339
    for (int32_t i = 0; i < tbNames->size; i++) {
194,395✔
1340
      char **key = (char **)taosArrayGet(tbNames, i);
116,510✔
1341
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
116,507✔
1342
      if (i < tbNames->size - 1) {
116,511✔
1343
        taosStringBuilderAppendChar(&sb, ',');
38,629✔
1344
      }
1345
      // taosMemoryFreeClear(*key);
1346
    }
1347

1348
    size_t len = 0;
77,885✔
1349
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
77,885✔
1350

1351
    if (pOriginRpc->info.conn.user != NULL && strlen(pOriginRpc->info.conn.user) > 0) {
77,882!
1352
      auditAddRecord(pOriginRpc, clusterId, "createTable", name.dbname, "", keyJoined, len);
62,278✔
1353
    }
1354

1355
    taosStringBuilderDestroy(&sb);
77,881✔
1356
  }
1357

1358
_exit:
2✔
1359
  tDeleteSVCreateTbBatchReq(&req);
77,880✔
1360
  taosArrayDestroyEx(rsp.pArray, tFreeSVCreateTbRsp);
77,888✔
1361
  taosArrayDestroy(tbUids);
77,890✔
1362
  tDecoderClear(&decoder);
77,888✔
1363
  tEncoderClear(&encoder);
77,887✔
1364
  taosArrayDestroyP(tbNames, NULL);
77,888✔
1365
  return rcode;
77,886✔
1366
}
1367

1368
static int32_t vnodeProcessAlterStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
7,458✔
1369
  int32_t        code = 0;
7,458✔
1370
  SVCreateStbReq req = {0};
7,458✔
1371
  SDecoder       dc = {0};
7,458✔
1372

1373
  pRsp->msgType = TDMT_VND_ALTER_STB_RSP;
7,458✔
1374
  pRsp->code = TSDB_CODE_SUCCESS;
7,458✔
1375
  pRsp->pCont = NULL;
7,458✔
1376
  pRsp->contLen = 0;
7,458✔
1377

1378
  tDecoderInit(&dc, pReq, len);
7,458✔
1379

1380
  // decode req
1381
  code = tDecodeSVCreateStbReq(&dc, &req);
7,461✔
1382
  if (code) {
7,467!
1383
    tDecoderClear(&dc);
×
1384
    return code;
×
1385
  }
1386

1387
  code = metaAlterSuperTable(pVnode->pMeta, ver, &req);
7,467✔
1388
  if (code) {
7,472!
1389
    pRsp->code = code;
×
1390
    tDecoderClear(&dc);
×
1391
    return code;
×
1392
  }
1393

1394
  tDecoderClear(&dc);
7,472✔
1395

1396
  return 0;
7,472✔
1397
}
1398

1399
static int32_t vnodeProcessDropStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
2,345✔
1400
  SVDropStbReq req = {0};
2,345✔
1401
  int32_t      rcode = TSDB_CODE_SUCCESS;
2,345✔
1402
  SDecoder     decoder = {0};
2,345✔
1403
  SArray      *tbUidList = NULL;
2,345✔
1404

1405
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
2,345✔
1406
  pRsp->pCont = NULL;
2,345✔
1407
  pRsp->contLen = 0;
2,345✔
1408

1409
  // decode request
1410
  tDecoderInit(&decoder, pReq, len);
2,345✔
1411
  if (tDecodeSVDropStbReq(&decoder, &req) < 0) {
2,344!
1412
    rcode = TSDB_CODE_INVALID_MSG;
×
1413
    goto _exit;
×
1414
  }
1415

1416
  // process request
1417
  tbUidList = taosArrayInit(8, sizeof(int64_t));
2,345✔
1418
  if (tbUidList == NULL) goto _exit;
2,349!
1419
  if (metaDropSuperTable(pVnode->pMeta, ver, &req) < 0) {
2,349✔
1420
    rcode = terrno;
3✔
1421
    goto _exit;
3✔
1422
  }
1423

1424
  if (tqUpdateTbUidList(pVnode->pTq, tbUidList, false) < 0) {
2,346!
1425
    rcode = terrno;
×
1426
    goto _exit;
×
1427
  }
1428

1429
  if (tdProcessRSmaDrop(pVnode->pSma, &req) < 0) {
2,345!
1430
    rcode = terrno;
×
1431
    goto _exit;
×
1432
  }
1433

1434
  // return rsp
1435
_exit:
2,346✔
1436
  if (tbUidList) taosArrayDestroy(tbUidList);
2,349!
1437
  pRsp->code = rcode;
2,349✔
1438
  tDecoderClear(&decoder);
2,349✔
1439
  return 0;
2,349✔
1440
}
1441

1442
static int32_t vnodeProcessAlterTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
974✔
1443
  SVAlterTbReq  vAlterTbReq = {0};
974✔
1444
  SVAlterTbRsp  vAlterTbRsp = {0};
974✔
1445
  SDecoder      dc = {0};
974✔
1446
  int32_t       code = 0;
974✔
1447
  int32_t       lino = 0;
974✔
1448
  int32_t       ret;
1449
  SEncoder      ec = {0};
974✔
1450
  STableMetaRsp vMetaRsp = {0};
974✔
1451

1452
  pRsp->msgType = TDMT_VND_ALTER_TABLE_RSP;
974✔
1453
  pRsp->pCont = NULL;
974✔
1454
  pRsp->contLen = 0;
974✔
1455
  pRsp->code = TSDB_CODE_SUCCESS;
974✔
1456

1457
  tDecoderInit(&dc, pReq, len);
974✔
1458

1459
  // decode
1460
  if (tDecodeSVAlterTbReq(&dc, &vAlterTbReq) < 0) {
974!
1461
    vAlterTbRsp.code = TSDB_CODE_INVALID_MSG;
×
1462
    tDecoderClear(&dc);
×
1463
    goto _exit;
×
1464
  }
1465

1466
  // process
1467
  if (metaAlterTable(pVnode->pMeta, ver, &vAlterTbReq, &vMetaRsp) < 0) {
974✔
1468
    vAlterTbRsp.code = terrno;
38✔
1469
    tDecoderClear(&dc);
38✔
1470
    goto _exit;
38✔
1471
  }
1472
  tDecoderClear(&dc);
936✔
1473

1474
  if (NULL != vMetaRsp.pSchemas) {
936✔
1475
    vnodeUpdateMetaRsp(pVnode, &vMetaRsp);
191✔
1476
    vAlterTbRsp.pMeta = &vMetaRsp;
191✔
1477
  }
1478

1479
  if (vAlterTbReq.action == TSDB_ALTER_TABLE_UPDATE_TAG_VAL || vAlterTbReq.action == TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL) {
936✔
1480
    int64_t uid = metaGetTableEntryUidByName(pVnode->pMeta, vAlterTbReq.tbName);
660✔
1481
    if (uid == 0) {
660!
1482
      vError("vgId:%d, %s failed at %s:%d since table %s not found", TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
1483
             vAlterTbReq.tbName);
1484
      goto _exit;
×
1485
    }
1486

1487
    SArray* tbUids = taosArrayInit(4, sizeof(int64_t));
660✔
1488
    void* p = taosArrayPush(tbUids, &uid);
660✔
1489
    TSDB_CHECK_NULL(p, code, lino, _exit, terrno);
660!
1490

1491
    vDebug("vgId:%d, remove tags value altered table:%s from query table list", TD_VID(pVnode), vAlterTbReq.tbName);
660✔
1492
    if ((code = tqUpdateTbUidList(pVnode->pTq, tbUids, false)) < 0) {
660!
1493
      vError("vgId:%d, failed to remove tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1494
    }
1495

1496
    vDebug("vgId:%d, try to add table:%s in query table list", TD_VID(pVnode), vAlterTbReq.tbName);
660✔
1497
    if ((code = tqUpdateTbUidList(pVnode->pTq, tbUids, true)) < 0) {
660!
1498
      vError("vgId:%d, failed to add tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1499
    }
1500

1501
    taosArrayDestroy(tbUids);
660✔
1502
  }
1503

1504
_exit:
276✔
1505
  taosArrayDestroy(vAlterTbReq.pMultiTag);
974✔
1506
  tEncodeSize(tEncodeSVAlterTbRsp, &vAlterTbRsp, pRsp->contLen, ret);
974!
1507
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
974✔
1508
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
974✔
1509
  if (tEncodeSVAlterTbRsp(&ec, &vAlterTbRsp) != 0) {
974!
1510
    vError("vgId:%d, failed to encode alter table response", TD_VID(pVnode));
×
1511
  }
1512

1513
  tEncoderClear(&ec);
974✔
1514
  if (vMetaRsp.pSchemas) {
974✔
1515
    taosMemoryFree(vMetaRsp.pSchemas);
191!
1516
    taosMemoryFree(vMetaRsp.pSchemaExt);
191!
1517
  }
1518
  return 0;
974✔
1519
}
1520

1521
static int32_t vnodeProcessDropTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
2,542✔
1522
                                     SRpcMsg *pOriginRpc) {
1523
  SVDropTbBatchReq req = {0};
2,542✔
1524
  SVDropTbBatchRsp rsp = {0};
2,542✔
1525
  SDecoder         decoder = {0};
2,542✔
1526
  SEncoder         encoder = {0};
2,542✔
1527
  int32_t          ret;
1528
  SArray          *tbUids = NULL;
2,542✔
1529
  STbUidStore     *pStore = NULL;
2,542✔
1530
  SArray          *tbNames = NULL;
2,542✔
1531

1532
  pRsp->msgType = TDMT_VND_DROP_TABLE_RSP;
2,542✔
1533
  pRsp->pCont = NULL;
2,542✔
1534
  pRsp->contLen = 0;
2,542✔
1535
  pRsp->code = TSDB_CODE_SUCCESS;
2,542✔
1536

1537
  // decode req
1538
  tDecoderInit(&decoder, pReq, len);
2,542✔
1539
  ret = tDecodeSVDropTbBatchReq(&decoder, &req);
2,542✔
1540
  if (ret < 0) {
2,542!
1541
    terrno = TSDB_CODE_INVALID_MSG;
×
1542
    pRsp->code = terrno;
×
1543
    goto _exit;
×
1544
  }
1545

1546
  // process req
1547
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
2,542✔
1548
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(SVDropTbRsp));
2,542✔
1549
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
2,542✔
1550
  if (tbUids == NULL || rsp.pArray == NULL || tbNames == NULL) goto _exit;
2,542!
1551

1552
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
5,109✔
1553
    SVDropTbReq *pDropTbReq = req.pReqs + iReq;
2,567✔
1554
    SVDropTbRsp  dropTbRsp = {0};
2,567✔
1555
    tb_uid_t     tbUid = 0;
2,567✔
1556

1557
    /* code */
1558
    ret = metaDropTable2(pVnode->pMeta, ver, pDropTbReq);
2,567✔
1559
    if (ret < 0) {
2,567!
1560
      if (pDropTbReq->igNotExists && terrno == TSDB_CODE_TDB_TABLE_NOT_EXIST) {
×
1561
        dropTbRsp.code = TSDB_CODE_SUCCESS;
×
1562
      } else {
1563
        dropTbRsp.code = terrno;
×
1564
      }
1565
    } else {
1566
      dropTbRsp.code = TSDB_CODE_SUCCESS;
2,567✔
1567
      if (tbUid > 0) {
2,567!
1568
        if (tdFetchTbUidList(pVnode->pSma, &pStore, pDropTbReq->suid, tbUid) < 0) {
×
1569
          vError("vgId:%d, failed to fetch tbUid list", TD_VID(pVnode));
×
1570
        }
1571
      }
1572
    }
1573

1574
    if (taosArrayPush(rsp.pArray, &dropTbRsp) == NULL) {
5,134!
1575
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1576
      pRsp->code = terrno;
×
1577
      goto _exit;
×
1578
    }
1579

1580
    if (tsEnableAuditCreateTable) {
2,567!
1581
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
2,567!
1582
      if (str == NULL) {
2,567!
1583
        pRsp->code = terrno;
×
1584
        goto _exit;
×
1585
      }
1586
      tstrncpy(str, pDropTbReq->name, TSDB_TABLE_FNAME_LEN);
2,567✔
1587
      if (taosArrayPush(tbNames, &str) == NULL) {
2,567!
1588
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1589
        pRsp->code = terrno;
×
1590
        goto _exit;
×
1591
      }
1592
    }
1593
  }
1594

1595
  if (tqUpdateTbUidList(pVnode->pTq, tbUids, false) < 0) {
2,542!
1596
    vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(terrno));
×
1597
  }
1598

1599
  if (tdUpdateTbUidList(pVnode->pSma, pStore, false) < 0) {
2,542!
1600
    goto _exit;
×
1601
  }
1602

1603
  if (tsEnableAuditCreateTable) {
2,542!
1604
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
2,542✔
1605

1606
    SName name = {0};
2,542✔
1607
    if (tNameFromString(&name, pVnode->config.dbname, T_NAME_ACCT | T_NAME_DB) != 0) {
2,542!
1608
      vError("vgId:%d, failed to get name from string", TD_VID(pVnode));
×
1609
    }
1610

1611
    SStringBuilder sb = {0};
2,542✔
1612
    for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
5,109✔
1613
      char **key = (char **)taosArrayGet(tbNames, iReq);
2,567✔
1614
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
2,567✔
1615
      if (iReq < req.nReqs - 1) {
2,567✔
1616
        taosStringBuilderAppendChar(&sb, ',');
25✔
1617
      }
1618
      taosMemoryFreeClear(*key);
2,567!
1619
    }
1620

1621
    size_t len = 0;
2,542✔
1622
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
2,542✔
1623

1624
    if (pOriginRpc->info.conn.user != NULL && strlen(pOriginRpc->info.conn.user) > 0) {
2,542!
1625
      auditAddRecord(pOriginRpc, clusterId, "dropTable", name.dbname, "", keyJoined, len);
2,445✔
1626
    }
1627

1628
    taosStringBuilderDestroy(&sb);
2,542✔
1629
  }
1630

1631
_exit:
×
1632
  taosArrayDestroy(tbUids);
2,542✔
1633
  pStore = tdUidStoreFree(pStore);
2,542✔
1634
  tDecoderClear(&decoder);
2,542✔
1635
  tEncodeSize(tEncodeSVDropTbBatchRsp, &rsp, pRsp->contLen, ret);
2,542!
1636
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
2,542✔
1637
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
2,542✔
1638
  if (tEncodeSVDropTbBatchRsp(&encoder, &rsp) != 0) {
2,542!
1639
    vError("vgId:%d, failed to encode drop table batch response", TD_VID(pVnode));
×
1640
  }
1641
  tEncoderClear(&encoder);
2,542✔
1642
  taosArrayDestroy(rsp.pArray);
2,542✔
1643
  taosArrayDestroy(tbNames);
2,542✔
1644
  return 0;
2,542✔
1645
}
1646

1647
#ifdef BUILD_NO_CALL
1648
static int32_t vnodeDebugPrintSingleSubmitMsg(SMeta *pMeta, SSubmitBlk *pBlock, SSubmitMsgIter *msgIter,
1649
                                              const char *tags) {
1650
  SSubmitBlkIter blkIter = {0};
1651
  STSchema      *pSchema = NULL;
1652
  tb_uid_t       suid = 0;
1653
  STSRow        *row = NULL;
1654
  int32_t        rv = -1;
1655

1656
  tInitSubmitBlkIter(msgIter, pBlock, &blkIter);
1657
  if (blkIter.row == NULL) return 0;
1658

1659
  int32_t code = metaGetTbTSchemaNotNull(pMeta, msgIter->suid, TD_ROW_SVER(blkIter.row), 1,
1660
                                         &pSchema);  // TODO: use the real schema
1661
  if (TSDB_CODE_SUCCESS != code) {
1662
    printf("%s:%d no valid schema\n", tags, __LINE__);
1663
    return code;
1664
  }
1665

1666
  suid = msgIter->suid;
1667
  rv = TD_ROW_SVER(blkIter.row);
1668

1669
  char __tags[128] = {0};
1670
  snprintf(__tags, 128, "%s: uid %" PRIi64 " ", tags, msgIter->uid);
1671
  while ((row = tGetSubmitBlkNext(&blkIter))) {
1672
    tdSRowPrint(row, pSchema, __tags);
1673
  }
1674

1675
  taosMemoryFreeClear(pSchema);
1676

1677
  return TSDB_CODE_SUCCESS;
1678
}
1679
#endif
1680
typedef struct SSubmitReqConvertCxt {
1681
  SSubmitMsgIter msgIter;
1682
  SSubmitBlk    *pBlock;
1683
  SSubmitBlkIter blkIter;
1684
  STSRow        *pRow;
1685
  STSRowIter     rowIter;
1686
  SSubmitTbData *pTbData;
1687
  STSchema      *pTbSchema;
1688
  SArray        *pColValues;
1689
} SSubmitReqConvertCxt;
1690

1691
static int32_t vnodeResetTableCxt(SMeta *pMeta, SSubmitReqConvertCxt *pCxt) {
×
1692
  taosMemoryFreeClear(pCxt->pTbSchema);
×
1693
  int32_t code = metaGetTbTSchemaNotNull(pMeta, pCxt->msgIter.suid > 0 ? pCxt->msgIter.suid : pCxt->msgIter.uid,
×
1694
                                         pCxt->msgIter.sversion, 1, &pCxt->pTbSchema);
1695
  if (TSDB_CODE_SUCCESS != code) {
×
1696
    return code;
×
1697
  }
1698
  tdSTSRowIterInit(&pCxt->rowIter, pCxt->pTbSchema);
×
1699

1700
  tDestroySubmitTbData(pCxt->pTbData, TSDB_MSG_FLG_ENCODE);
×
1701
  if (NULL == pCxt->pTbData) {
×
1702
    pCxt->pTbData = taosMemoryCalloc(1, sizeof(SSubmitTbData));
×
1703
    if (NULL == pCxt->pTbData) {
×
1704
      return terrno;
×
1705
    }
1706
  }
1707
  pCxt->pTbData->flags = 0;
×
1708
  pCxt->pTbData->suid = pCxt->msgIter.suid;
×
1709
  pCxt->pTbData->uid = pCxt->msgIter.uid;
×
1710
  pCxt->pTbData->sver = pCxt->msgIter.sversion;
×
1711
  pCxt->pTbData->pCreateTbReq = NULL;
×
1712
  pCxt->pTbData->aRowP = taosArrayInit(128, POINTER_BYTES);
×
1713
  if (NULL == pCxt->pTbData->aRowP) {
×
1714
    return terrno;
×
1715
  }
1716

1717
  taosArrayDestroy(pCxt->pColValues);
×
1718
  pCxt->pColValues = taosArrayInit(pCxt->pTbSchema->numOfCols, sizeof(SColVal));
×
1719
  if (NULL == pCxt->pColValues) {
×
1720
    return terrno;
×
1721
  }
1722
  for (int32_t i = 0; i < pCxt->pTbSchema->numOfCols; ++i) {
×
1723
    SColVal val = COL_VAL_NONE(pCxt->pTbSchema->columns[i].colId, pCxt->pTbSchema->columns[i].type);
×
1724
    if (taosArrayPush(pCxt->pColValues, &val) == NULL) {
×
1725
      return terrno;
×
1726
    }
1727
  }
1728

1729
  return TSDB_CODE_SUCCESS;
×
1730
}
1731

1732
static void vnodeDestroySubmitReqConvertCxt(SSubmitReqConvertCxt *pCxt) {
×
1733
  taosMemoryFreeClear(pCxt->pTbSchema);
×
1734
  tDestroySubmitTbData(pCxt->pTbData, TSDB_MSG_FLG_ENCODE);
×
1735
  taosMemoryFreeClear(pCxt->pTbData);
×
1736
  taosArrayDestroy(pCxt->pColValues);
×
1737
}
×
1738

1739
static int32_t vnodeCellValConvertToColVal(STColumn *pCol, SCellVal *pCellVal, SColVal *pColVal) {
×
1740
  if (tdValTypeIsNone(pCellVal->valType)) {
×
1741
    pColVal->flag = CV_FLAG_NONE;
×
1742
    return TSDB_CODE_SUCCESS;
×
1743
  }
1744

1745
  if (tdValTypeIsNull(pCellVal->valType)) {
×
1746
    pColVal->flag = CV_FLAG_NULL;
×
1747
    return TSDB_CODE_SUCCESS;
×
1748
  }
1749

1750
  if (IS_VAR_DATA_TYPE(pCol->type)) {
×
1751
    pColVal->value.nData = varDataLen(pCellVal->val);
×
1752
    pColVal->value.pData = (uint8_t *)varDataVal(pCellVal->val);
×
1753
  } else if (TSDB_DATA_TYPE_FLOAT == pCol->type) {
×
1754
    float f = GET_FLOAT_VAL(pCellVal->val);
×
1755
    memcpy(&pColVal->value.val, &f, sizeof(f));
×
1756
  } else if (TSDB_DATA_TYPE_DOUBLE == pCol->type) {
×
1757
    pColVal->value.val = *(int64_t *)pCellVal->val;
×
1758
  } else {
1759
    GET_TYPED_DATA(pColVal->value.val, int64_t, pCol->type, pCellVal->val);
×
1760
  }
1761

1762
  pColVal->flag = CV_FLAG_VALUE;
×
1763
  return TSDB_CODE_SUCCESS;
×
1764
}
1765

1766
static int32_t vnodeTSRowConvertToColValArray(SSubmitReqConvertCxt *pCxt) {
×
1767
  int32_t code = TSDB_CODE_SUCCESS;
×
1768
  tdSTSRowIterReset(&pCxt->rowIter, pCxt->pRow);
×
1769
  for (int32_t i = 0; TSDB_CODE_SUCCESS == code && i < pCxt->pTbSchema->numOfCols; ++i) {
×
1770
    STColumn *pCol = pCxt->pTbSchema->columns + i;
×
1771
    SCellVal  cellVal = {0};
×
1772
    if (!tdSTSRowIterFetch(&pCxt->rowIter, pCol->colId, pCol->type, &cellVal)) {
×
1773
      break;
×
1774
    }
1775
    code = vnodeCellValConvertToColVal(pCol, &cellVal, (SColVal *)taosArrayGet(pCxt->pColValues, i));
×
1776
  }
1777
  return code;
×
1778
}
1779

1780
static int32_t vnodeDecodeCreateTbReq(SSubmitReqConvertCxt *pCxt) {
×
1781
  if (pCxt->msgIter.schemaLen <= 0) {
×
1782
    return TSDB_CODE_SUCCESS;
×
1783
  }
1784

1785
  pCxt->pTbData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
×
1786
  if (NULL == pCxt->pTbData->pCreateTbReq) {
×
1787
    return terrno;
×
1788
  }
1789

1790
  SDecoder decoder = {0};
×
1791
  tDecoderInit(&decoder, (uint8_t *)pCxt->pBlock->data, pCxt->msgIter.schemaLen);
×
1792
  int32_t code = tDecodeSVCreateTbReq(&decoder, pCxt->pTbData->pCreateTbReq);
×
1793
  tDecoderClear(&decoder);
×
1794

1795
  return code;
×
1796
}
1797

1798
static int32_t vnodeSubmitReqConvertToSubmitReq2(SVnode *pVnode, SSubmitReq *pReq, SSubmitReq2 *pReq2) {
×
1799
  pReq2->aSubmitTbData = taosArrayInit(128, sizeof(SSubmitTbData));
×
1800
  if (NULL == pReq2->aSubmitTbData) {
×
1801
    return terrno;
×
1802
  }
1803

1804
  SSubmitReqConvertCxt cxt = {0};
×
1805

1806
  int32_t code = tInitSubmitMsgIter(pReq, &cxt.msgIter);
×
1807
  while (TSDB_CODE_SUCCESS == code) {
×
1808
    code = tGetSubmitMsgNext(&cxt.msgIter, &cxt.pBlock);
×
1809
    if (TSDB_CODE_SUCCESS == code) {
×
1810
      if (NULL == cxt.pBlock) {
×
1811
        break;
×
1812
      }
1813
      code = vnodeResetTableCxt(pVnode->pMeta, &cxt);
×
1814
    }
1815
    if (TSDB_CODE_SUCCESS == code) {
×
1816
      code = tInitSubmitBlkIter(&cxt.msgIter, cxt.pBlock, &cxt.blkIter);
×
1817
    }
1818
    if (TSDB_CODE_SUCCESS == code) {
×
1819
      code = vnodeDecodeCreateTbReq(&cxt);
×
1820
    }
1821
    while (TSDB_CODE_SUCCESS == code && (cxt.pRow = tGetSubmitBlkNext(&cxt.blkIter)) != NULL) {
×
1822
      code = vnodeTSRowConvertToColValArray(&cxt);
×
1823
      if (TSDB_CODE_SUCCESS == code) {
×
1824
        SRow **pNewRow = taosArrayReserve(cxt.pTbData->aRowP, 1);
×
1825
        code = tRowBuild(cxt.pColValues, cxt.pTbSchema, pNewRow);
×
1826
      }
1827
    }
1828
    if (TSDB_CODE_SUCCESS == code) {
×
1829
      code = (NULL == taosArrayPush(pReq2->aSubmitTbData, cxt.pTbData) ? terrno : TSDB_CODE_SUCCESS);
×
1830
    }
1831
    if (TSDB_CODE_SUCCESS == code) {
×
1832
      taosMemoryFreeClear(cxt.pTbData);
×
1833
    }
1834
  }
1835

1836
  vnodeDestroySubmitReqConvertCxt(&cxt);
×
1837
  return code;
×
1838
}
1839

1840
static int32_t vnodeRebuildSubmitReqMsg(SSubmitReq2 *pSubmitReq, void **ppMsg) {
×
1841
  int32_t  code = TSDB_CODE_SUCCESS;
×
1842
  char    *pMsg = NULL;
×
1843
  uint32_t msglen = 0;
×
1844
  tEncodeSize(tEncodeSubmitReq, pSubmitReq, msglen, code);
×
1845
  if (TSDB_CODE_SUCCESS == code) {
×
1846
    pMsg = taosMemoryMalloc(msglen);
×
1847
    if (NULL == pMsg) {
×
1848
      code = terrno;
×
1849
    }
1850
  }
1851
  if (TSDB_CODE_SUCCESS == code) {
×
1852
    SEncoder encoder;
1853
    tEncoderInit(&encoder, (uint8_t *)pMsg, msglen);
×
1854
    code = tEncodeSubmitReq(&encoder, pSubmitReq);
×
1855
    tEncoderClear(&encoder);
×
1856
  }
1857
  if (TSDB_CODE_SUCCESS == code) {
×
1858
    *ppMsg = pMsg;
×
1859
  }
1860
  return code;
×
1861
}
1862

1863
static int32_t vnodeProcessSubmitReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
11,340,789✔
1864
                                     SRpcMsg *pOriginalMsg) {
1865
  int32_t code = 0;
11,340,789✔
1866
  terrno = 0;
11,340,789✔
1867

1868
  SSubmitReq2 *pSubmitReq = &(SSubmitReq2){0};
11,341,021✔
1869
  SSubmitRsp2 *pSubmitRsp = &(SSubmitRsp2){0};
11,341,021✔
1870
  SArray      *newTbUids = NULL;
11,341,021✔
1871
  int32_t      ret;
1872
  SEncoder     ec = {0};
11,341,021✔
1873

1874
  pRsp->code = TSDB_CODE_SUCCESS;
11,341,021✔
1875

1876
  void           *pAllocMsg = NULL;
11,341,021✔
1877
  SSubmitReq2Msg *pMsg = (SSubmitReq2Msg *)pReq;
11,341,021✔
1878
  if (0 == pMsg->version) {
11,341,021!
1879
    code = vnodeSubmitReqConvertToSubmitReq2(pVnode, (SSubmitReq *)pMsg, pSubmitReq);
×
1880
    if (TSDB_CODE_SUCCESS == code) {
×
1881
      code = vnodeRebuildSubmitReqMsg(pSubmitReq, &pReq);
×
1882
    }
UNCOV
1883
    if (TSDB_CODE_SUCCESS == code) {
×
1884
      pAllocMsg = pReq;
×
1885
    }
UNCOV
1886
    if (TSDB_CODE_SUCCESS != code) {
×
1887
      goto _exit;
×
1888
    }
1889
  } else {
1890
    // decode
1891
    pReq = POINTER_SHIFT(pReq, sizeof(SSubmitReq2Msg));
11,341,021✔
1892
    len -= sizeof(SSubmitReq2Msg);
11,341,021✔
1893
    SDecoder dc = {0};
11,341,021✔
1894
    tDecoderInit(&dc, pReq, len);
11,341,021✔
1895
    if (tDecodeSubmitReq(&dc, pSubmitReq, NULL) < 0) {
11,340,863!
1896
      code = TSDB_CODE_INVALID_MSG;
×
1897
      goto _exit;
×
1898
    }
1899
    tDecoderClear(&dc);
11,340,886✔
1900
  }
1901

1902
  // scan
1903
  TSKEY now = taosGetTimestamp(pVnode->config.tsdbCfg.precision);
11,341,087✔
1904
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * pVnode->config.tsdbCfg.keep2;
11,340,872✔
1905
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
11,340,872✔
1906
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
23,382,498✔
1907
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
12,040,232✔
1908

1909
    if (pSubmitTbData->pCreateTbReq && pSubmitTbData->pCreateTbReq->uid == 0) {
12,040,211!
1910
      code = TSDB_CODE_INVALID_MSG;
×
1911
      goto _exit;
×
1912
    }
1913

1914
    if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
12,040,211✔
1915
      if (TARRAY_SIZE(pSubmitTbData->aCol) <= 0) {
152!
1916
        code = TSDB_CODE_INVALID_MSG;
×
1917
        goto _exit;
×
1918
      }
1919

1920
      SColData *colDataArr = TARRAY_DATA(pSubmitTbData->aCol);
152✔
1921
      SRowKey   lastKey;
1922
      tColDataArrGetRowKey(colDataArr, TARRAY_SIZE(pSubmitTbData->aCol), 0, &lastKey);
152✔
1923
      for (int32_t iRow = 1; iRow < colDataArr[0].nVal; iRow++) {
274✔
1924
        SRowKey key;
1925
        tColDataArrGetRowKey(TARRAY_DATA(pSubmitTbData->aCol), TARRAY_SIZE(pSubmitTbData->aCol), iRow, &key);
122✔
1926
        if (tRowKeyCompare(&lastKey, &key) >= 0) {
122!
1927
          code = TSDB_CODE_INVALID_MSG;
×
1928
          vError("vgId:%d %s failed 1 since %s, version:%" PRId64, TD_VID(pVnode), __func__, tstrerror(terrno), ver);
×
1929
          goto _exit;
×
1930
        }
1931
      }
1932
    } else {
1933
      int32_t nRow = TARRAY_SIZE(pSubmitTbData->aRowP);
12,040,059✔
1934
      SRow  **aRow = (SRow **)TARRAY_DATA(pSubmitTbData->aRowP);
12,040,059✔
1935
      SRowKey lastRowKey;
1936
      for (int32_t iRow = 0; iRow < nRow; ++iRow) {
724,044,947✔
1937
        if (aRow[iRow]->ts < minKey || aRow[iRow]->ts > maxKey) {
712,003,473✔
1938
          code = TSDB_CODE_INVALID_MSG;
40,503✔
1939
          vError("vgId:%d %s failed 2 since %s, version:%" PRId64, TD_VID(pVnode), __func__, tstrerror(code), ver);
40,503!
1940
          goto _exit;
×
1941
        }
1942
        if (iRow == 0) {
711,962,970✔
1943
          tRowGetKey(aRow[iRow], &lastRowKey);
12,040,068✔
1944
        } else {
1945
          SRowKey rowKey;
1946
          tRowGetKey(aRow[iRow], &rowKey);
699,922,902✔
1947

1948
          if (tRowKeyCompare(&lastRowKey, &rowKey) >= 0) {
699,900,729!
1949
            code = TSDB_CODE_INVALID_MSG;
×
1950
            vError("vgId:%d %s failed 3 since %s, version:%" PRId64, TD_VID(pVnode), __func__, tstrerror(code), ver);
×
1951
            goto _exit;
×
1952
          }
1953
          lastRowKey = rowKey;
699,960,846✔
1954
        }
1955
      }
1956
    }
1957
  }
1958

1959
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
23,387,120✔
1960
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
12,044,396✔
1961

1962
    if (pSubmitTbData->pCreateTbReq) {
12,043,938✔
1963
      pSubmitTbData->uid = pSubmitTbData->pCreateTbReq->uid;
63,225✔
1964
    } else {
1965
      SMetaInfo info = {0};
11,980,713✔
1966

1967
      code = metaGetInfo(pVnode->pMeta, pSubmitTbData->uid, &info, NULL);
11,980,713✔
1968
      if (code) {
11,981,123!
1969
        code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
1970
        vWarn("vgId:%d, table uid:%" PRId64 " not exists", TD_VID(pVnode), pSubmitTbData->uid);
×
1971
        goto _exit;
4✔
1972
      }
1973

1974
      if (info.suid != pSubmitTbData->suid) {
11,981,141!
1975
        code = TSDB_CODE_INVALID_MSG;
×
1976
        goto _exit;
×
1977
      }
1978

1979
      if (info.suid) {
11,981,141✔
1980
        if (metaGetInfo(pVnode->pMeta, info.suid, &info, NULL) != 0) {
11,073,857!
1981
          vWarn("vgId:%d, table uid:%" PRId64 " not exists", TD_VID(pVnode), info.suid);
×
1982
        }
1983
      }
1984

1985
      if (pSubmitTbData->sver != info.skmVer) {
11,981,629!
1986
        code = TSDB_CODE_TDB_INVALID_TABLE_SCHEMA_VER;
×
1987
        goto _exit;
×
1988
      }
1989
    }
1990

1991
    if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
12,044,854✔
1992
      int32_t   nColData = TARRAY_SIZE(pSubmitTbData->aCol);
152✔
1993
      SColData *aColData = (SColData *)TARRAY_DATA(pSubmitTbData->aCol);
152✔
1994

1995
      if (nColData <= 0) {
152!
1996
        code = TSDB_CODE_INVALID_MSG;
×
1997
        goto _exit;
×
1998
      }
1999

2000
      if (aColData[0].cid != PRIMARYKEY_TIMESTAMP_COL_ID || aColData[0].type != TSDB_DATA_TYPE_TIMESTAMP ||
152!
2001
          aColData[0].nVal <= 0) {
152!
2002
        code = TSDB_CODE_INVALID_MSG;
×
2003
        goto _exit;
×
2004
      }
2005

2006
      for (int32_t j = 1; j < nColData; j++) {
645✔
2007
        if (aColData[j].nVal != aColData[0].nVal) {
493!
2008
          code = TSDB_CODE_INVALID_MSG;
×
2009
          goto _exit;
×
2010
        }
2011
      }
2012
    }
2013
  }
2014

2015
  vDebug("vgId:%d, submit block size %d", TD_VID(pVnode), (int32_t)taosArrayGetSize(pSubmitReq->aSubmitTbData));
11,342,724✔
2016

2017
  // loop to handle
2018
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
23,382,434✔
2019
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
12,041,276✔
2020

2021
    // create table
2022
    if (pSubmitTbData->pCreateTbReq) {
12,041,045✔
2023
      // alloc if need
2024
      if (pSubmitRsp->aCreateTbRsp == NULL &&
63,215✔
2025
          (pSubmitRsp->aCreateTbRsp = taosArrayInit(TARRAY_SIZE(pSubmitReq->aSubmitTbData), sizeof(SVCreateTbRsp))) ==
46,186!
2026
              NULL) {
2027
        code = terrno;
×
2028
        goto _exit;
8✔
2029
      }
2030

2031
      SVCreateTbRsp *pCreateTbRsp = taosArrayReserve(pSubmitRsp->aCreateTbRsp, 1);
63,215✔
2032

2033
      // create table
2034
      if (metaCreateTable2(pVnode->pMeta, ver, pSubmitTbData->pCreateTbReq, &pCreateTbRsp->pMeta) == 0) {
63,212✔
2035
        // create table success
2036

2037
        if (newTbUids == NULL &&
63,655!
2038
            (newTbUids = taosArrayInit(TARRAY_SIZE(pSubmitReq->aSubmitTbData), sizeof(int64_t))) == NULL) {
26,228✔
2039
          code = terrno;
×
2040
          goto _exit;
×
2041
        }
2042

2043
        if (taosArrayPush(newTbUids, &pSubmitTbData->uid) == NULL) {
74,854!
2044
          code = terrno;
×
2045
          goto _exit;
×
2046
        }
2047

2048
        if (pCreateTbRsp->pMeta) {
37,427!
2049
          vnodeUpdateMetaRsp(pVnode, pCreateTbRsp->pMeta);
37,427✔
2050
        }
2051
      } else {  // create table failed
2052
        if (terrno != TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
25,794✔
2053
          code = terrno;
8✔
2054
          vError("vgId:%d failed to create table:%s, code:%s", TD_VID(pVnode), pSubmitTbData->pCreateTbReq->name,
8!
2055
                 tstrerror(terrno));
2056
          goto _exit;
8✔
2057
        }
2058
        terrno = 0;
25,784✔
2059
        pSubmitTbData->uid = pSubmitTbData->pCreateTbReq->uid;  // update uid if table exist for using below
25,784✔
2060
      }
2061
    }
2062

2063
    // insert data
2064
    int32_t affectedRows;
2065
    code = tsdbInsertTableData(pVnode->pTsdb, ver, pSubmitTbData, &affectedRows);
12,041,041✔
2066
    if (code) goto _exit;
12,041,962!
2067

2068
    code = metaUpdateChangeTimeWithLock(pVnode->pMeta, pSubmitTbData->uid, pSubmitTbData->ctimeMs);
12,041,962✔
2069
    if (code) goto _exit;
12,041,341!
2070

2071
    pSubmitRsp->affectedRows += affectedRows;
12,041,341✔
2072
  }
2073

2074
  // update the affected table uid list
2075
  if (taosArrayGetSize(newTbUids) > 0) {
11,341,158✔
2076
    vDebug("vgId:%d, add %d table into query table list in handling submit", TD_VID(pVnode),
26,228✔
2077
           (int32_t)taosArrayGetSize(newTbUids));
2078
    if (tqUpdateTbUidList(pVnode->pTq, newTbUids, true) != 0) {
26,228!
2079
      vError("vgId:%d, failed to update tbUid list", TD_VID(pVnode));
×
2080
    }
2081
  }
2082

2083
_exit:
11,340,992✔
2084
  // message
2085
  pRsp->code = code;
11,341,004✔
2086
  tEncodeSize(tEncodeSSubmitRsp2, pSubmitRsp, pRsp->contLen, ret);
11,341,004!
2087
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
11,340,869✔
2088
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
11,341,019✔
2089
  if (tEncodeSSubmitRsp2(&ec, pSubmitRsp) < 0) {
11,341,029!
2090
    vError("vgId:%d, failed to encode submit response", TD_VID(pVnode));
×
2091
  }
2092
  tEncoderClear(&ec);
11,341,006✔
2093

2094
  // update statistics
2095
  (void)atomic_add_fetch_64(&pVnode->statis.nInsert, pSubmitRsp->affectedRows);
11,340,958✔
2096
  (void)atomic_add_fetch_64(&pVnode->statis.nInsertSuccess, pSubmitRsp->affectedRows);
11,341,126✔
2097
  (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsert, 1);
11,341,122✔
2098

2099
  if (tsEnableMonitor && tsMonitorFqdn[0] != 0 && tsMonitorPort != 0 && pSubmitRsp->affectedRows > 0 &&
11,341,109!
2100
      strlen(pOriginalMsg->info.conn.user) > 0 && tsInsertCounter != NULL) {
×
2101
    const char *sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT_AFFECTED_ROWS,
×
2102
                                   pVnode->monitor.strClusterId,
×
2103
                                   pVnode->monitor.strDnodeId,
×
2104
                                   tsLocalEp,
2105
                                   pVnode->monitor.strVgId,
×
2106
                                   pOriginalMsg->info.conn.user,
×
2107
                                   "Success"};
2108
    int         tv = taos_counter_add(tsInsertCounter, pSubmitRsp->affectedRows, sample_labels);
×
2109
  }
2110

2111
  if (code == 0) {
11,341,109✔
2112
    (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
11,341,085✔
2113
    code = tdProcessRSmaSubmit(pVnode->pSma, ver, pSubmitReq, pReq, len);
11,341,099✔
2114
  }
2115
  /*
2116
  if (code == 0) {
2117
    atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
2118
    code = tdProcessRSmaSubmit(pVnode->pSma, ver, pSubmitReq, pReq, len);
2119

2120
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT, pVnode->monitor.strClusterId,
2121
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2122
                                          pOriginalMsg->info.conn.user, "Success"};
2123
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2124
  }
2125
  else{
2126
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT, pVnode->monitor.strClusterId,
2127
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2128
                                        pOriginalMsg->info.conn.user, "Failed"};
2129
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2130
  }
2131
  */
2132

2133
  // clear
2134
  taosArrayDestroy(newTbUids);
11,340,986✔
2135
  tDestroySubmitReq(pSubmitReq, 0 == pMsg->version ? TSDB_MSG_FLG_CMPT : TSDB_MSG_FLG_DECODE);
11,340,876!
2136
  tDestroySSubmitRsp2(pSubmitRsp, TSDB_MSG_FLG_ENCODE);
11,341,054✔
2137

2138
  if (code) terrno = code;
11,340,924✔
2139

2140
  taosMemoryFree(pAllocMsg);
11,340,924!
2141

2142
  return code;
11,340,898✔
2143
}
2144

2145
static int32_t vnodeProcessCreateTSmaReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
31✔
2146
  SVCreateTSmaReq req = {0};
31✔
2147
  SDecoder        coder = {0};
31✔
2148

2149
  if (pRsp) {
31!
2150
    pRsp->msgType = TDMT_VND_CREATE_SMA_RSP;
31✔
2151
    pRsp->code = TSDB_CODE_SUCCESS;
31✔
2152
    pRsp->pCont = NULL;
31✔
2153
    pRsp->contLen = 0;
31✔
2154
  }
2155

2156
  // decode and process req
2157
  tDecoderInit(&coder, pReq, len);
31✔
2158

2159
  if (tDecodeSVCreateTSmaReq(&coder, &req) < 0) {
31!
2160
    terrno = TSDB_CODE_MSG_DECODE_ERROR;
×
2161
    if (pRsp) pRsp->code = terrno;
×
2162
    goto _err;
×
2163
  }
2164

2165
  if (tdProcessTSmaCreate(pVnode->pSma, ver, (const char *)&req) < 0) {
31!
2166
    if (pRsp) pRsp->code = terrno;
×
2167
    goto _err;
×
2168
  }
2169

2170
  tDecoderClear(&coder);
31✔
2171
  vDebug("vgId:%d, success to create tsma %s:%" PRIi64 " version %" PRIi64 " for table %" PRIi64, TD_VID(pVnode),
31!
2172
         req.indexName, req.indexUid, ver, req.tableUid);
2173
  return 0;
31✔
2174

2175
_err:
×
2176
  tDecoderClear(&coder);
×
2177
  vError("vgId:%d, failed to create tsma %s:%" PRIi64 " version %" PRIi64 "for table %" PRIi64 " since %s",
×
2178
         TD_VID(pVnode), req.indexName, req.indexUid, ver, req.tableUid, terrstr());
2179
  return terrno;
×
2180
}
2181

2182
/**
2183
 * @brief specific for smaDstVnode
2184
 *
2185
 * @param pVnode
2186
 * @param pCont
2187
 * @param contLen
2188
 * @return int32_t
2189
 */
2190
int32_t vnodeProcessCreateTSma(SVnode *pVnode, void *pCont, uint32_t contLen) {
×
2191
  return vnodeProcessCreateTSmaReq(pVnode, 1, pCont, contLen, NULL);
×
2192
}
2193

2194
static int32_t vnodeConsolidateAlterHashRange(SVnode *pVnode, int64_t ver) {
32✔
2195
  int32_t code = TSDB_CODE_SUCCESS;
32✔
2196

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

2200
  // TODO: trim meta of tables from TDB per hash range [pVnode->config.hashBegin, pVnode->config.hashEnd]
2201
  code = metaTrimTables(pVnode->pMeta, ver);
32✔
2202

2203
  return code;
32✔
2204
}
2205

2206
static int32_t vnodeProcessAlterConfirmReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
8,004✔
2207
  vInfo("vgId:%d, vnode handle msgType:alter-confirm, alter confirm msg is processed", TD_VID(pVnode));
8,004!
2208
  int32_t code = TSDB_CODE_SUCCESS;
8,004✔
2209
  if (!pVnode->config.hashChange) {
8,004✔
2210
    goto _exit;
7,972✔
2211
  }
2212

2213
  code = vnodeConsolidateAlterHashRange(pVnode, ver);
32✔
2214
  if (code < 0) {
32!
2215
    vError("vgId:%d, failed to consolidate alter hashrange since %s. version:%" PRId64, TD_VID(pVnode), terrstr(), ver);
×
2216
    goto _exit;
×
2217
  }
2218
  pVnode->config.hashChange = false;
32✔
2219

2220
_exit:
8,004✔
2221
  pRsp->msgType = TDMT_VND_ALTER_CONFIRM_RSP;
8,004✔
2222
  pRsp->code = code;
8,004✔
2223
  pRsp->pCont = NULL;
8,004✔
2224
  pRsp->contLen = 0;
8,004✔
2225

2226
  return code;
8,004✔
2227
}
2228

2229
extern int32_t tsdbDisableAndCancelAllBgTask(STsdb *pTsdb);
2230
extern void    tsdbEnableBgTask(STsdb *pTsdb);
2231

2232
static int32_t vnodeProcessAlterConfigReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
748✔
2233
  bool walChanged = false;
748✔
2234
  bool tsdbChanged = false;
748✔
2235

2236
  SAlterVnodeConfigReq req = {0};
748✔
2237
  if (tDeserializeSAlterVnodeConfigReq(pReq, len, &req) != 0) {
748!
2238
    terrno = TSDB_CODE_INVALID_MSG;
×
2239
    return TSDB_CODE_INVALID_MSG;
×
2240
  }
2241

2242
  vInfo("vgId:%d, start to alter vnode config, page:%d pageSize:%d buffer:%d szPage:%d szBuf:%" PRIu64
750✔
2243
        " cacheLast:%d cacheLastSize:%d days:%d keep0:%d keep1:%d keep2:%d keepTimeOffset:%d s3KeepLocal:%d "
2244
        "s3Compact:%d fsync:%d level:%d "
2245
        "walRetentionPeriod:%d walRetentionSize:%d",
2246
        TD_VID(pVnode), req.pages, req.pageSize, req.buffer, req.pageSize * 1024, (uint64_t)req.buffer * 1024 * 1024,
2247
        req.cacheLast, req.cacheLastSize, req.daysPerFile, req.daysToKeep0, req.daysToKeep1, req.daysToKeep2,
2248
        req.keepTimeOffset, req.s3KeepLocal, req.s3Compact, req.walFsyncPeriod, req.walLevel, req.walRetentionPeriod,
2249
        req.walRetentionSize);
2250

2251
  if (pVnode->config.cacheLastSize != req.cacheLastSize) {
753✔
2252
    pVnode->config.cacheLastSize = req.cacheLastSize;
358✔
2253
    tsdbCacheSetCapacity(pVnode, (size_t)pVnode->config.cacheLastSize * 1024 * 1024);
358✔
2254
  }
2255

2256
  if (pVnode->config.szBuf != req.buffer * 1024LL * 1024LL) {
753!
2257
    vInfo("vgId:%d, vnode buffer is changed from %" PRId64 " to %" PRId64, TD_VID(pVnode), pVnode->config.szBuf,
×
2258
          (uint64_t)(req.buffer * 1024LL * 1024LL));
2259
    pVnode->config.szBuf = req.buffer * 1024LL * 1024LL;
×
2260
  }
2261

2262
  if (pVnode->config.szCache != req.pages) {
753!
2263
    if (metaAlterCache(pVnode->pMeta, req.pages) < 0) {
×
2264
      vError("vgId:%d, failed to change vnode pages from %d to %d failed since %s", TD_VID(pVnode),
×
2265
             pVnode->config.szCache, req.pages, tstrerror(errno));
2266
      return errno;
×
2267
    } else {
2268
      vInfo("vgId:%d, vnode pages is changed from %d to %d", TD_VID(pVnode), pVnode->config.szCache, req.pages);
×
2269
      pVnode->config.szCache = req.pages;
×
2270
    }
2271
  }
2272

2273
  if (pVnode->config.cacheLast != req.cacheLast) {
753✔
2274
    pVnode->config.cacheLast = req.cacheLast;
470✔
2275
  }
2276

2277
  if (pVnode->config.walCfg.fsyncPeriod != req.walFsyncPeriod) {
753✔
2278
    pVnode->config.walCfg.fsyncPeriod = req.walFsyncPeriod;
54✔
2279
    walChanged = true;
54✔
2280
  }
2281

2282
  if (pVnode->config.walCfg.level != req.walLevel) {
753✔
2283
    if (pVnode->config.walCfg.level == 0) {
36!
2284
      pVnode->config.walCfg.clearFiles = 1;
×
2285
    }
2286
    pVnode->config.walCfg.level = req.walLevel;
36✔
2287
    walChanged = true;
36✔
2288
  }
2289

2290
  if (pVnode->config.walCfg.retentionPeriod != req.walRetentionPeriod) {
753✔
2291
    pVnode->config.walCfg.retentionPeriod = req.walRetentionPeriod;
132✔
2292
    walChanged = true;
132✔
2293
  }
2294

2295
  if (pVnode->config.walCfg.retentionSize != req.walRetentionSize) {
753!
2296
    pVnode->config.walCfg.retentionSize = req.walRetentionSize;
×
2297
    walChanged = true;
×
2298
  }
2299

2300
  if (pVnode->config.tsdbCfg.keep0 != req.daysToKeep0) {
753✔
2301
    pVnode->config.tsdbCfg.keep0 = req.daysToKeep0;
54✔
2302
    if (!VND_IS_RSMA(pVnode)) {
54!
2303
      tsdbChanged = true;
54✔
2304
    }
2305
  }
2306

2307
  if (pVnode->config.tsdbCfg.keep1 != req.daysToKeep1) {
753✔
2308
    pVnode->config.tsdbCfg.keep1 = req.daysToKeep1;
54✔
2309
    if (!VND_IS_RSMA(pVnode)) {
54!
2310
      tsdbChanged = true;
54✔
2311
    }
2312
  }
2313

2314
  if (pVnode->config.tsdbCfg.keep2 != req.daysToKeep2) {
753✔
2315
    pVnode->config.tsdbCfg.keep2 = req.daysToKeep2;
56✔
2316
    if (!VND_IS_RSMA(pVnode)) {
56!
2317
      tsdbChanged = true;
56✔
2318
    }
2319
  }
2320

2321
  if (pVnode->config.tsdbCfg.keepTimeOffset != req.keepTimeOffset) {
753✔
2322
    pVnode->config.tsdbCfg.keepTimeOffset = req.keepTimeOffset;
2✔
2323
    if (!VND_IS_RSMA(pVnode)) {
2!
2324
      tsdbChanged = true;
2✔
2325
    }
2326
  }
2327

2328
  if (req.sttTrigger != -1 && req.sttTrigger != pVnode->config.sttTrigger) {
753!
2329
    if (req.sttTrigger > 1 && pVnode->config.sttTrigger > 1) {
×
2330
      pVnode->config.sttTrigger = req.sttTrigger;
×
2331
    } else {
2332
      vnodeAWait(&pVnode->commitTask);
×
2333

2334
      int32_t ret = tsdbDisableAndCancelAllBgTask(pVnode->pTsdb);
×
2335
      if (ret != 0) {
×
2336
        vError("vgId:%d, failed to disable bg task since %s", TD_VID(pVnode), tstrerror(errno));
×
2337
      }
2338

2339
      pVnode->config.sttTrigger = req.sttTrigger;
×
2340
      tsdbEnableBgTask(pVnode->pTsdb);
×
2341
    }
2342
  }
2343

2344
  if (req.minRows != -1 && req.minRows != pVnode->config.tsdbCfg.minRows) {
753!
2345
    pVnode->config.tsdbCfg.minRows = req.minRows;
×
2346
  }
2347

2348
  if (req.s3KeepLocal != -1 && req.s3KeepLocal != pVnode->config.s3KeepLocal) {
753!
2349
    pVnode->config.s3KeepLocal = req.s3KeepLocal;
×
2350
  }
2351
  if (req.s3Compact != -1 && req.s3Compact != pVnode->config.s3Compact) {
753!
2352
    pVnode->config.s3Compact = req.s3Compact;
×
2353
  }
2354

2355
  if (walChanged) {
753✔
2356
    if (walAlter(pVnode->pWal, &pVnode->config.walCfg) != 0) {
222!
2357
      vError("vgId:%d, failed to alter wal config since %s", TD_VID(pVnode), tstrerror(errno));
×
2358
    }
2359
  }
2360

2361
  if (tsdbChanged) {
752✔
2362
    tsdbSetKeepCfg(pVnode->pTsdb, &pVnode->config.tsdbCfg);
58✔
2363
  }
2364

2365
  return 0;
752✔
2366
}
2367

2368
static int32_t vnodeProcessBatchDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
2,897✔
2369
  SBatchDeleteReq deleteReq;
2370
  SDecoder        decoder;
2371
  tDecoderInit(&decoder, pReq, len);
2,897✔
2372
  if (tDecodeSBatchDeleteReq(&decoder, &deleteReq) < 0) {
2,899!
2373
    tDecoderClear(&decoder);
×
2374
    return terrno = TSDB_CODE_INVALID_MSG;
×
2375
  }
2376

2377
  SMetaReader mr = {0};
2,900✔
2378
  metaReaderDoInit(&mr, pVnode->pMeta, META_READER_NOLOCK);
2,900✔
2379
  STsdb *pTsdb = pVnode->pTsdb;
2,900✔
2380

2381
  if (deleteReq.level) {
2,900✔
2382
    pTsdb = deleteReq.level == 1 ? VND_RSMA1(pVnode) : VND_RSMA2(pVnode);
16✔
2383
  }
2384

2385
  int32_t sz = taosArrayGetSize(deleteReq.deleteReqs);
2,900✔
2386
  for (int32_t i = 0; i < sz; i++) {
6,688✔
2387
    SSingleDeleteReq *pOneReq = taosArrayGet(deleteReq.deleteReqs, i);
3,785✔
2388
    char             *name = pOneReq->tbname;
3,787✔
2389
    if (metaGetTableEntryByName(&mr, name) < 0) {
3,787✔
2390
      vDebug("vgId:%d, stream delete msg, skip since no table: %s", pVnode->config.vgId, name);
1,916✔
2391
      continue;
1,916✔
2392
    }
2393

2394
    int64_t uid = mr.me.uid;
1,874✔
2395

2396
    int32_t code = tsdbDeleteTableData(pTsdb, ver, deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
1,874✔
2397
    if (code < 0) {
1,875!
2398
      terrno = code;
×
2399
      vError("vgId:%d, delete error since %s, suid:%" PRId64 ", uid:%" PRId64 ", start ts:%" PRId64 ", end ts:%" PRId64,
×
2400
             TD_VID(pVnode), terrstr(), deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
2401
    }
2402

2403
    if (deleteReq.level == 0) {
1,875✔
2404
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, deleteReq.ctimeMs);
1,259✔
2405
      if (code < 0) {
1,259!
2406
        terrno = code;
×
2407
        vError("vgId:%d, update change time error since %s, suid:%" PRId64 ", uid:%" PRId64 ", start ts:%" PRId64
×
2408
               ", end ts:%" PRId64,
2409
               TD_VID(pVnode), terrstr(), deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
2410
      }
2411
    }
2412
    tDecoderClear(&mr.coder);
1,875✔
2413
  }
2414
  metaReaderClear(&mr);
2,903✔
2415
  taosArrayDestroy(deleteReq.deleteReqs);
2,899✔
2416
  return 0;
2,901✔
2417
}
2418

2419
static int32_t vnodeProcessDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
68,614✔
2420
                                     SRpcMsg *pOriginalMsg) {
2421
  int32_t     code = 0;
68,614✔
2422
  SDecoder   *pCoder = &(SDecoder){0};
68,614✔
2423
  SDeleteRes *pRes = &(SDeleteRes){0};
68,614✔
2424

2425
  pRsp->msgType = TDMT_VND_DELETE_RSP;
68,614✔
2426
  pRsp->pCont = NULL;
68,614✔
2427
  pRsp->contLen = 0;
68,614✔
2428
  pRsp->code = TSDB_CODE_SUCCESS;
68,614✔
2429

2430
  pRes->uidList = taosArrayInit(0, sizeof(tb_uid_t));
68,614✔
2431
  if (pRes->uidList == NULL) {
68,613!
2432
    code = terrno;
×
2433
    goto _err;
×
2434
  }
2435

2436
  tDecoderInit(pCoder, pReq, len);
68,613✔
2437
  code = tDecodeDeleteRes(pCoder, pRes);
68,612✔
2438
  if (code) goto _err;
68,617!
2439

2440
  if (pRes->affectedRows > 0) {
68,617✔
2441
    for (int32_t iUid = 0; iUid < taosArrayGetSize(pRes->uidList); iUid++) {
129,343✔
2442
      uint64_t uid = *(uint64_t *)taosArrayGet(pRes->uidList, iUid);
65,566✔
2443
      code = tsdbDeleteTableData(pVnode->pTsdb, ver, pRes->suid, uid, pRes->skey, pRes->ekey);
65,566✔
2444
      if (code) goto _err;
65,566!
2445
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, pRes->ctimeMs);
65,566✔
2446
      if (code) goto _err;
65,565!
2447
    }
2448
  }
2449

2450
  code = tdProcessRSmaDelete(pVnode->pSma, ver, pRes, pReq, len);
68,617✔
2451

2452
  tDecoderClear(pCoder);
68,613✔
2453
  taosArrayDestroy(pRes->uidList);
68,611✔
2454

2455
  SVDeleteRsp rsp = {.affectedRows = pRes->affectedRows};
68,616✔
2456
  int32_t     ret = 0;
68,616✔
2457
  tEncodeSize(tEncodeSVDeleteRsp, &rsp, pRsp->contLen, ret);
68,616!
2458
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
68,615✔
2459
  SEncoder ec = {0};
68,617✔
2460
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
68,617✔
2461
  code = tEncodeSVDeleteRsp(&ec, &rsp);
68,618✔
2462
  if (code) goto _err;
68,614!
2463
  tEncoderClear(&ec);
68,614✔
2464
  return code;
68,618✔
2465

2466
_err:
×
2467
  /*
2468
  if(code == TSDB_CODE_SUCCESS){
2469
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_DELETE, pVnode->monitor.strClusterId,
2470
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2471
                                        pOriginalMsg->info.conn.user, "Success"};
2472
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2473
  }
2474
  else{
2475
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_DELETE, pVnode->monitor.strClusterId,
2476
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2477
                                        pOriginalMsg->info.conn.user, "Failed"};
2478
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2479
  }
2480
  */
2481

2482
  return code;
×
2483
}
2484
static int32_t vnodeProcessCreateIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
965✔
2485
  SVCreateStbReq req = {0};
965✔
2486
  SDecoder       dc = {0};
965✔
2487
  int32_t        code = 0;
965✔
2488

2489
  pRsp->msgType = TDMT_VND_CREATE_INDEX_RSP;
965✔
2490
  pRsp->code = TSDB_CODE_SUCCESS;
965✔
2491
  pRsp->pCont = NULL;
965✔
2492
  pRsp->contLen = 0;
965✔
2493

2494
  tDecoderInit(&dc, pReq, len);
965✔
2495
  // decode req
2496
  if (tDecodeSVCreateStbReq(&dc, &req) < 0) {
965!
2497
    tDecoderClear(&dc);
×
2498
    return terrno = TSDB_CODE_INVALID_MSG;
×
2499
  }
2500

2501
  code = metaAddIndexToSuperTable(pVnode->pMeta, ver, &req);
965✔
2502
  if (code) {
965!
2503
    pRsp->code = code;
×
2504
    goto _err;
×
2505
  }
2506
  tDecoderClear(&dc);
965✔
2507
  return 0;
965✔
2508

2509
_err:
×
2510
  tDecoderClear(&dc);
×
2511
  return code;
×
2512
}
2513
static int32_t vnodeProcessDropIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
2,140✔
2514
  SDropIndexReq req = {0};
2,140✔
2515
  int32_t       code = 0;
2,140✔
2516
  pRsp->msgType = TDMT_VND_DROP_INDEX_RSP;
2,140✔
2517
  pRsp->code = TSDB_CODE_SUCCESS;
2,140✔
2518
  pRsp->pCont = NULL;
2,140✔
2519
  pRsp->contLen = 0;
2,140✔
2520

2521
  if ((code = tDeserializeSDropIdxReq(pReq, len, &req))) {
2,140!
2522
    pRsp->code = code;
×
2523
    return code;
×
2524
  }
2525

2526
  code = metaDropIndexFromSuperTable(pVnode->pMeta, ver, &req);
2,140✔
2527
  if (code) {
2,140!
2528
    pRsp->code = code;
×
2529
    return code;
×
2530
  }
2531
  return TSDB_CODE_SUCCESS;
2,140✔
2532
}
2533

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

2536
static int32_t vnodeProcessCompactVnodeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
203✔
2537
  if (!pVnode->restored) {
203✔
2538
    vInfo("vgId:%d, ignore compact req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
173!
2539
    return 0;
173✔
2540
  }
2541
  return vnodeAsyncCompact(pVnode, ver, pReq, len, pRsp);
30✔
2542
}
2543

2544
static int32_t vnodeProcessConfigChangeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
2545
  if (syncCheckMember(pVnode->sync) != 0) {
×
2546
    vError("vgId:%d, failed to check member", TD_VID(pVnode));
×
2547
  }
2548

2549
  pRsp->msgType = TDMT_SYNC_CONFIG_CHANGE_RSP;
×
2550
  pRsp->code = TSDB_CODE_SUCCESS;
×
2551
  pRsp->pCont = NULL;
×
2552
  pRsp->contLen = 0;
×
2553

2554
  return 0;
×
2555
}
2556

2557
static int32_t vnodeCheckToken(SVnode *pVnode, char *member0Token, char *member1Token) {
10✔
2558
  SSyncState syncState = syncGetState(pVnode->sync);
10✔
2559
  if (syncState.state != TAOS_SYNC_STATE_LEADER) {
10✔
2560
    return terrno = TSDB_CODE_SYN_NOT_LEADER;
6✔
2561
  }
2562

2563
  char token[TSDB_ARB_TOKEN_SIZE] = {0};
4✔
2564
  if (vnodeGetArbToken(pVnode, token) != 0) {
4!
2565
    return terrno = TSDB_CODE_NOT_FOUND;
×
2566
  }
2567

2568
  if (strncmp(token, member0Token, TSDB_ARB_TOKEN_SIZE) != 0 &&
4!
2569
      strncmp(token, member1Token, TSDB_ARB_TOKEN_SIZE) != 0) {
4!
UNCOV
2570
    return terrno = TSDB_CODE_MND_ARB_TOKEN_MISMATCH;
×
2571
  }
2572

2573
  terrno = TSDB_CODE_SUCCESS;
4✔
2574
  return 0;
4✔
2575
}
2576

2577
static int32_t vnodeCheckSyncd(SVnode *pVnode, char *member0Token, char *member1Token) {
8✔
2578
  int32_t code = vnodeCheckToken(pVnode, member0Token, member1Token);
8✔
2579
  if (code != 0) {
8✔
2580
    return code;
6✔
2581
  }
2582

2583
  return syncCheckSynced(pVnode->sync);
2✔
2584
}
2585

2586
static int32_t vnodeProcessArbCheckSyncReq(SVnode *pVnode, void *pReq, int32_t len, SRpcMsg *pRsp) {
8✔
2587
  int32_t code = 0;
8✔
2588

2589
  SVArbCheckSyncReq syncReq = {0};
8✔
2590

2591
  code = tDeserializeSVArbCheckSyncReq(pReq, len, &syncReq);
8✔
2592
  if (code) {
8!
2593
    return terrno = code;
×
2594
  }
2595

2596
  pRsp->msgType = TDMT_VND_ARB_CHECK_SYNC_RSP;
8✔
2597
  pRsp->code = TSDB_CODE_SUCCESS;
8✔
2598
  pRsp->pCont = NULL;
8✔
2599
  pRsp->contLen = 0;
8✔
2600

2601
  SVArbCheckSyncRsp syncRsp = {0};
8✔
2602
  syncRsp.arbToken = syncReq.arbToken;
8✔
2603
  syncRsp.member0Token = syncReq.member0Token;
8✔
2604
  syncRsp.member1Token = syncReq.member1Token;
8✔
2605
  syncRsp.vgId = TD_VID(pVnode);
8✔
2606

2607
  if (vnodeCheckSyncd(pVnode, syncReq.member0Token, syncReq.member1Token) != 0) {
8✔
2608
    vError("vgId:%d, failed to check assigned log syncd", TD_VID(pVnode));
6!
2609
  }
2610
  syncRsp.errCode = terrno;
8✔
2611

2612
  if (vnodeUpdateArbTerm(pVnode, syncReq.arbTerm) != 0) {
8!
2613
    vError("vgId:%d, failed to update arb term", TD_VID(pVnode));
×
2614
    code = -1;
×
2615
    goto _OVER;
×
2616
  }
2617

2618
  int32_t contLen = tSerializeSVArbCheckSyncRsp(NULL, 0, &syncRsp);
8✔
2619
  if (contLen <= 0) {
8!
2620
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2621
    code = -1;
×
2622
    goto _OVER;
×
2623
  }
2624
  void *pHead = rpcMallocCont(contLen);
8✔
2625
  if (!pHead) {
8!
2626
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2627
    code = -1;
×
2628
    goto _OVER;
×
2629
  }
2630

2631
  if (tSerializeSVArbCheckSyncRsp(pHead, contLen, &syncRsp) <= 0) {
8!
2632
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2633
    rpcFreeCont(pHead);
×
2634
    code = -1;
×
2635
    goto _OVER;
×
2636
  }
2637

2638
  pRsp->pCont = pHead;
8✔
2639
  pRsp->contLen = contLen;
8✔
2640

2641
  terrno = TSDB_CODE_SUCCESS;
8✔
2642

2643
_OVER:
8✔
2644
  tFreeSVArbCheckSyncReq(&syncReq);
8✔
2645
  return code;
8✔
2646
}
2647

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