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

taosdata / TDengine / #3616

19 Feb 2025 11:22AM UTC coverage: 63.315% (+0.4%) from 62.953%
#3616

push

travis-ci

web-flow
Merge pull request #29823 from taosdata/feat/TS-5928

fix:[TS-5928]add consumer parameters

148228 of 300186 branches covered (49.38%)

Branch coverage included in aggregate %.

232358 of 300909 relevant lines covered (77.22%)

17990742.15 hits per line

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

61.52
/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) {
153,210✔
64
  int32_t code = 0;
153,210✔
65
  int32_t lino = 0;
153,210✔
66

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

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

78
  // name
79
  char *name = NULL;
153,270✔
80
  if (tDecodeCStr(pCoder, &name) < 0) {
153,232!
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);
153,232✔
87
  if (uid == 0) {
153,236✔
88
    uid = tGenIdPI64();
126,678✔
89
  }
90
  *(int64_t *)(pCoder->data + pCoder->pos) = uid;
153,255✔
91

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

95
  tEndDecode(pCoder);
153,255✔
96

97
_exit:
153,205✔
98
  if (code) {
153,205!
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);
153,205✔
102
    if (pUid) *pUid = uid;
153,205✔
103
  }
104
  return code;
153,199✔
105
}
106
static int32_t vnodePreProcessCreateTableMsg(SVnode *pVnode, SRpcMsg *pMsg) {
67,099✔
107
  int32_t code = 0;
67,099✔
108
  int32_t lino = 0;
67,099✔
109

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

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

120
  if (tDecodeI32v(&dc, &nReqs) < 0) {
67,097!
121
    code = TSDB_CODE_INVALID_MSG;
×
122
    TSDB_CHECK_CODE(code, lino, _exit);
×
123
  }
124
  for (int32_t iReq = 0; iReq < nReqs; iReq++) {
157,765✔
125
    code = vnodePreprocessCreateTableReq(pVnode, &dc, btime, NULL);
90,726✔
126
    TSDB_CHECK_CODE(code, lino, _exit);
90,668!
127
  }
128

129
  tEndDecode(&dc);
67,039✔
130

131
_exit:
67,094✔
132
  tDecoderClear(&dc);
67,094✔
133
  if (code) {
67,108!
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;
67,103✔
138
}
139

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

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

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

157
  code = 0;
967✔
158

159
_exit:
967✔
160
  tDecoderClear(&dc);
967✔
161
  if (code) {
967!
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,
967✔
165
           ctimeMs);
166
  }
167
  return code;
967✔
168
}
169

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

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

177
  SArray *tbUids = NULL;
88,873✔
178
  int64_t timestampMs = 0;
88,873✔
179

180
  SVDropTtlTableReq ttlReq = {0};
88,873✔
181
  if (tDeserializeSVDropTtlTableReq((char *)pContOld + sizeof(SMsgHead), reqLenOld, &ttlReq) != 0) {
88,873!
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));
89,195✔
188
    if (tbUids == NULL) {
89,136!
189
      code = terrno;
×
190
      TSDB_CHECK_CODE(code, lino, _exit);
×
191
    }
192

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

200
    ttlReq.nUids = taosArrayGetSize(tbUids);
89,036✔
201
    ttlReq.pTbUids = tbUids;
88,935✔
202
  }
203

204
  if (ttlReq.nUids == 0) {
88,935✔
205
    code = TSDB_CODE_MSG_PREPROCESSED;
88,932✔
206
    TSDB_CHECK_CODE(code, lino, _exit);
88,932!
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:
88,958✔
233
  taosArrayDestroy(tbUids);
88,958✔
234

235
  if (code && code != TSDB_CODE_MSG_PREPROCESSED) {
89,116!
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);
89,116✔
240
  }
241

242
  return code;
89,025✔
243
}
244

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

250
  if (tStartDecode(pCoder) < 0) {
10,623,349!
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,640,294!
258
    code = TSDB_CODE_INVALID_MSG;
×
259
    TSDB_CHECK_CODE(code, lino, _exit);
×
260
  }
261
  version = (submitTbData.flags >> 8) & 0xff;
10,640,294✔
262
  submitTbData.flags = submitTbData.flags & 0xff;
10,640,294✔
263

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

270
  // submit data
271
  if (tDecodeI64(pCoder, &submitTbData.suid) < 0) {
10,627,369!
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,627,369✔
277
    *(int64_t *)(pCoder->data + pCoder->pos) = uid;
62,519✔
278
    pCoder->pos += sizeof(int64_t);
62,519✔
279
  } else {
280
    if (tDecodeI64(pCoder, &submitTbData.uid) < 0) {
10,559,914!
281
      code = TSDB_CODE_INVALID_MSG;
×
282
      TSDB_CHECK_CODE(code, lino, _exit);
×
283
    }
284
  }
285

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

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

299
  int32_t keep = pVnode->config.tsdbCfg.keep2;
10,612,543✔
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,612,543✔
312
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
10,612,543✔
313
  if (submitTbData.flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
10,612,543✔
314
    uint64_t nColData;
315
    if (tDecodeU64v(pCoder, &nColData) < 0) {
148!
316
      code = TSDB_CODE_INVALID_MSG;
×
317
      TSDB_CHECK_CODE(code, lino, _exit);
1!
318
    }
319

320
    SColData colData = {0};
148✔
321
    code = tDecodeColData(version, pCoder, &colData);
148✔
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++) {
631✔
340
      code = tDecodeColData(version, pCoder, &colData);
485✔
341
      if (code) {
483!
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,623,386!
349
      code = TSDB_CODE_INVALID_MSG;
×
350
      TSDB_CHECK_CODE(code, lino, _exit);
496!
351
    }
352

353
    for (int32_t iRow = 0; iRow < nRow; ++iRow) {
660,991,289✔
354
      SRow *pRow = (SRow *)(pCoder->data + pCoder->pos);
650,368,399✔
355
      pCoder->pos += pRow->len;
650,368,399✔
356

357
      if (pRow->ts < minKey || pRow->ts > maxKey) {
650,368,399✔
358
        code = TSDB_CODE_TDB_TIMESTAMP_OUT_OF_RANGE;
7,147✔
359
        TSDB_CHECK_CODE(code, lino, _exit);
7,147✔
360
      }
361
    }
362
  }
363

364
  if (!tDecodeIsEnd(pCoder)) {
10,623,036!
365
    *(int64_t *)(pCoder->data + pCoder->pos) = ctimeMs;
10,625,781✔
366
    pCoder->pos += sizeof(int64_t);
10,625,781✔
367
  }
368

369
  tEndDecode(pCoder);
10,623,036✔
370

371
_exit:
10,622,835✔
372
  if (code) {
10,622,835✔
373
    vError("vgId:%d, %s:%d failed to vnodePreProcessSubmitTbData submit request since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
497!
374
  }
375
  return code;
10,623,209✔
376
}
377
static int32_t vnodePreProcessSubmitMsg(SVnode *pVnode, SRpcMsg *pMsg) {
9,958,395✔
378
  int32_t code = 0;
9,958,395✔
379
  int32_t lino = 0;
9,958,395✔
380

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

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

387
  if (taosHton64(((SSubmitReq2Msg *)pMsg->pCont)->version) != 1) {
9,958,395!
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,958,422✔
393

394
  if (tStartDecode(pCoder) < 0) {
9,958,357!
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,958,401!
401
    code = TSDB_CODE_INVALID_MSG;
×
402
    TSDB_CHECK_CODE(code, lino, _exit);
×
403
  }
404

405
  int64_t btimeMs = taosGetTimestampMs();
9,958,465✔
406
  int64_t ctimeMs = btimeMs;
9,958,465✔
407
  for (int32_t i = 0; i < nSubmitTbData; i++) {
20,581,118✔
408
    code = vnodePreProcessSubmitTbData(pVnode, pCoder, btimeMs, ctimeMs);
10,623,729✔
409
    TSDB_CHECK_CODE(code, lino, _exit);
10,623,152✔
410
  }
411

412
  tEndDecode(pCoder);
9,957,389✔
413

414
_exit:
9,958,408✔
415
  tDecoderClear(pCoder);
9,958,408✔
416
  if (code) {
9,958,391✔
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,958,409✔
421
}
422

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

479
  if (code) {
2,612!
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,612✔
483
  }
484
  return code;
2,612✔
485
}
486

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

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

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

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

503
  return code;
3✔
504
}
505

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

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

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

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

544
  tEncodeSize(tEncodeSVDropTbBatchReq, &sentBatchReqs, size, code);
2,559!
545
  tEncoderInit(&ec, POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), size);
2,559✔
546
  code = tEncodeSVDropTbBatchReq(&ec, &sentBatchReqs);
2,559✔
547
  tEncoderClear(&ec);
2,559✔
548
  if (code != TSDB_CODE_SUCCESS) {
2,559!
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,559✔
554
  tDecoderClear(&dc);
2,559✔
555
  if (sentBatchReqs.pArray) {
2,559!
556
    taosArrayDestroy(sentBatchReqs.pArray);
2,559✔
557
  }
558
  return code;
2,559✔
559
}
560

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

564
  switch (pMsg->msgType) {
10,292,810✔
565
    case TDMT_VND_CREATE_TABLE: {
67,110✔
566
      code = vnodePreProcessCreateTableMsg(pVnode, pMsg);
67,110✔
567
    } break;
67,102✔
568
    case TDMT_VND_ALTER_TABLE: {
967✔
569
      code = vnodePreProcessAlterTableMsg(pVnode, pMsg);
967✔
570
    } break;
967✔
571
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
89,164✔
572
    case TDMT_VND_DROP_TTL_TABLE: {
573
      code = vnodePreProcessDropTtlMsg(pVnode, pMsg);
89,164✔
574
    } break;
88,986✔
575
    case TDMT_VND_SUBMIT: {
9,958,469✔
576
      code = vnodePreProcessSubmitMsg(pVnode, pMsg);
9,958,469✔
577
    } break;
9,958,411✔
578
    case TDMT_VND_DELETE: {
67,419✔
579
      code = vnodePreProcessDeleteMsg(pVnode, pMsg);
67,419✔
580
    } break;
67,421✔
581
    case TDMT_VND_BATCH_DEL: {
2,612✔
582
      code = vnodePreProcessBatchDeleteMsg(pVnode, pMsg);
2,612✔
583
    } break;
2,611✔
584
    case TDMT_VND_ARB_CHECK_SYNC: {
3✔
585
      code = vnodePreProcessArbCheckSyncMsg(pVnode, pMsg);
3✔
586
    } break;
3✔
587
    case TDMT_VND_DROP_TABLE: {
2,559✔
588
      code = vnodePreProcessDropTbMsg(pVnode, pMsg);
2,559✔
589
    } break;
2,559✔
590
    default:
104,507✔
591
      break;
104,507✔
592
  }
593

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

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

607
  (void)taosThreadMutexLock(&pVnode->mutex);
11,652,935✔
608
  if (pVnode->disableWrite) {
11,653,288!
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,653,288✔
614

615
  if (ver <= pVnode->state.applied) {
11,653,267!
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,653,267!
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,652,988!
627
    return terrno = TSDB_CODE_INTERNAL_ERROR;
×
628
  }
629

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

634
  atomic_store_64(&pVnode->state.applied, ver);
11,652,988✔
635
  atomic_store_64(&pVnode->state.applyTerm, pMsg->info.conn.applyTerm);
11,653,301✔
636

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

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

644
  switch (pMsg->msgType) {
11,627,437!
645
    /* META */
646
    case TDMT_VND_CREATE_STB:
27,050✔
647
      if (vnodeProcessCreateStbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
27,050✔
648
      break;
27,096✔
649
    case TDMT_VND_ALTER_STB:
6,410✔
650
      if (vnodeProcessAlterStbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
6,410!
651
      break;
6,414✔
652
    case TDMT_VND_DROP_STB:
2,323✔
653
      if (vnodeProcessDropStbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
2,323!
654
      break;
2,327✔
655
    case TDMT_VND_CREATE_TABLE:
75,192✔
656
      if (vnodeProcessCreateTbReq(pVnode, ver, pReq, len, pRsp, pMsg) < 0) goto _err;
75,192!
657
      break;
75,221✔
658
    case TDMT_VND_ALTER_TABLE:
972✔
659
      if (vnodeProcessAlterTbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
972!
660
      break;
972✔
661
    case TDMT_VND_DROP_TABLE:
2,601✔
662
      if (vnodeProcessDropTbReq(pVnode, ver, pReq, len, pRsp, pMsg) < 0) goto _err;
2,601!
663
      break;
2,601✔
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:
193✔
671
      if (vnodeProcessTrimReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
193!
672
      break;
193✔
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,349,803✔
681
      if (vnodeProcessSubmitReq(pVnode, ver, pMsg->pCont, pMsg->contLen, pRsp, pMsg) < 0) goto _err;
11,349,803✔
682
      break;
11,349,587✔
683
    case TDMT_VND_DELETE:
68,727✔
684
      if (vnodeProcessDeleteReq(pVnode, ver, pReq, len, pRsp, pMsg) < 0) goto _err;
68,727!
685
      break;
68,727✔
686
    case TDMT_VND_BATCH_DEL:
2,895✔
687
      if (vnodeProcessBatchDeleteReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
2,895!
688
      break;
2,901✔
689
    /* TQ */
690
    case TDMT_VND_TMQ_SUBSCRIBE:
3,168✔
691
      if (tqProcessSubscribeReq(pVnode->pTq, ver, pReq, len) < 0) {
3,168✔
692
        goto _err;
1✔
693
      }
694
      break;
3,174✔
695
    case TDMT_VND_TMQ_DELETE_SUB:
869✔
696
      if (tqProcessDeleteSubReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen) < 0) {
869!
697
        goto _err;
×
698
      }
699
      break;
872✔
700
    case TDMT_VND_TMQ_COMMIT_OFFSET:
11,161✔
701
      if (tqProcessOffsetCommitReq(pVnode->pTq, ver, pReq, len) < 0) {
11,161!
702
        goto _err;
×
703
      }
704
      break;
11,166✔
705
    case TDMT_VND_TMQ_ADD_CHECKINFO:
116✔
706
      if (tqProcessAddCheckInfoReq(pVnode->pTq, ver, pReq, len) < 0) {
116!
707
        goto _err;
×
708
      }
709
      break;
116✔
710
    case TDMT_VND_TMQ_DEL_CHECKINFO:
10✔
711
      if (tqProcessDelCheckInfoReq(pVnode->pTq, ver, pReq, len) < 0) {
10!
712
        goto _err;
×
713
      }
714
      break;
10✔
715
    case TDMT_STREAM_TASK_DEPLOY: {
14,210✔
716
      if ((code = tqProcessTaskDeployReq(pVnode->pTq, ver, pReq, len)) != TSDB_CODE_SUCCESS) {
14,210!
717
        goto _err;
×
718
      }
719
    } break;
14,220✔
720
    case TDMT_STREAM_TASK_DROP: {
6,959✔
721
      if ((code = tqProcessTaskDropReq(pVnode->pTq, pMsg->pCont, pMsg->contLen)) < 0) {
6,959!
722
        goto _err;
×
723
      }
724
    } break;
6,982✔
725
    case TDMT_STREAM_TASK_UPDATE_CHKPT: {
5,394✔
726
      if ((code = tqProcessTaskUpdateCheckpointReq(pVnode->pTq, pMsg->pCont, pMsg->contLen)) < 0) {
5,394!
727
        goto _err;
×
728
      }
729
    } break;
5,407✔
730
    case TDMT_STREAM_CONSEN_CHKPT: {
275✔
731
      if (pVnode->restored && (code = tqProcessTaskConsenChkptIdReq(pVnode->pTq, pMsg)) < 0) {
275!
732
        goto _err;
×
733
      }
734

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

753
    } break;
×
754
    case TDMT_VND_ALTER_CONFIRM:
7,966✔
755
      needCommit = pVnode->config.hashChange;
7,966✔
756
      if (vnodeProcessAlterConfirmReq(pVnode, ver, pReq, len, pRsp) < 0) {
7,966!
757
        goto _err;
×
758
      }
759
      break;
7,966✔
760
    case TDMT_VND_ALTER_CONFIG:
643✔
761
      vnodeProcessAlterConfigReq(pVnode, ver, pReq, len, pRsp);
643✔
762
      break;
645✔
763
    case TDMT_VND_COMMIT:
27,933✔
764
      needCommit = true;
27,933✔
765
      break;
27,933✔
766
    case TDMT_VND_CREATE_INDEX:
971✔
767
      vnodeProcessCreateIndexReq(pVnode, ver, pReq, len, pRsp);
971✔
768
      break;
971✔
769
    case TDMT_VND_DROP_INDEX:
2,146✔
770
      vnodeProcessDropIndexReq(pVnode, ver, pReq, len, pRsp);
2,146✔
771
      break;
2,146✔
772
    case TDMT_VND_STREAM_CHECK_POINT_SOURCE:  // always return true
3,219✔
773
      tqProcessTaskCheckPointSourceReq(pVnode->pTq, pMsg, pRsp);
3,219✔
774
      break;
3,223✔
775
    case TDMT_VND_STREAM_TASK_UPDATE:  // always return true
82✔
776
      tqProcessTaskUpdateReq(pVnode->pTq, pMsg);
82✔
777
      break;
82✔
778
    case TDMT_VND_COMPACT:
202✔
779
      vnodeProcessCompactVnodeReq(pVnode, ver, pReq, len, pRsp);
202✔
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:
6✔
791
      vnodeProcessArbCheckSyncReq(pVnode, pReq, len, pRsp);
6✔
792
      break;
6✔
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,627,184!
799
         ver);
800

801
  walApplyVer(pVnode->pWal, ver);
11,627,184✔
802

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

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

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

826
_exit:
11,626,801✔
827
  return 0;
11,652,393✔
828

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

835
int32_t vnodePreprocessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg) {
8,138,583✔
836
  if (TDMT_SCH_QUERY != pMsg->msgType && TDMT_SCH_MERGE_QUERY != pMsg->msgType) {
8,138,583✔
837
    return 0;
497,188✔
838
  }
839

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

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

850
  if (pMsg->msgType == TDMT_VND_TMQ_CONSUME && !pVnode->restored) {
8,148,326!
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,148,326✔
856
  initStorageAPI(&handle.api);
8,148,326✔
857
  int32_t code = TSDB_CODE_SUCCESS;
8,146,757✔
858
  bool    redirected = false;
8,146,757✔
859

860
  switch (pMsg->msgType) {
8,146,757✔
861
    case TDMT_SCH_QUERY:
5,849,863✔
862
      if (!syncIsReadyForRead(pVnode->sync)) {
5,849,863✔
863
        pMsg->code = (terrno) ? terrno : TSDB_CODE_SYN_NOT_LEADER;
2,512!
864
        redirected = true;
2,512✔
865
      }
866
      code = qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
5,851,252✔
867
      if (redirected) {
5,849,047✔
868
        vnodeRedirectRpcMsg(pVnode, pMsg, pMsg->code);
1,439✔
869
        return 0;
2,512✔
870
      }
871
      return code;
5,847,608✔
872
    case TDMT_SCH_MERGE_QUERY:
1,800,653✔
873
      return qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
1,800,653✔
874
    case TDMT_SCH_QUERY_CONTINUE:
403,794✔
875
      return qWorkerProcessCQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
403,794✔
876
    case TDMT_VND_TMQ_CONSUME:
68,771✔
877
      return tqProcessPollReq(pVnode->pTq, pMsg);
68,771✔
878
    case TDMT_VND_TMQ_CONSUME_PUSH:
23,126✔
879
      return tqProcessPollPush(pVnode->pTq, pMsg);
23,126✔
880
    default:
550✔
881
      vError("unknown msg type:%d in query queue", pMsg->msgType);
550!
882
      return TSDB_CODE_APP_ERROR;
×
883
  }
884
}
885

886
int32_t vnodeProcessFetchMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
20,178,535✔
887
  vTrace("vgId:%d, msg:%p in fetch queue is processing", pVnode->config.vgId, pMsg);
20,178,535✔
888
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
20,178,541!
889
       pMsg->msgType == TDMT_VND_BATCH_META || pMsg->msgType == TDMT_VND_TABLE_NAME) &&
14,021,010✔
890
      !syncIsReadyForRead(pVnode->sync)) {
7,312,005✔
891
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
2,464✔
892
    return 0;
2,464✔
893
  }
894

895
  switch (pMsg->msgType) {
20,177,299!
896
    case TDMT_SCH_FETCH:
9,522,118✔
897
    case TDMT_SCH_MERGE_FETCH:
898
      return qWorkerProcessFetchMsg(pVnode, pVnode->pQuery, pMsg, 0);
9,522,118✔
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,643,357✔
904
      return qWorkerProcessDropMsg(pVnode, pVnode->pQuery, pMsg, 0);
7,643,357✔
905
    case TDMT_SCH_TASK_NOTIFY:
44✔
906
      return qWorkerProcessNotifyMsg(pVnode, pVnode->pQuery, pMsg, 0);
44✔
907
    case TDMT_SCH_QUERY_HEARTBEAT:
1,861,964✔
908
      return qWorkerProcessHbMsg(pVnode, pVnode->pQuery, pMsg, 0);
1,861,964✔
909
    case TDMT_VND_TABLE_META:
2,088✔
910
    case TDMT_VND_TABLE_NAME:
911
      return vnodeGetTableMeta(pVnode, pMsg, true);
2,088✔
912
    case TDMT_VND_TABLE_CFG:
×
913
      return vnodeGetTableCfg(pVnode, pMsg, true);
×
914
    case TDMT_VND_BATCH_META:
1,147,641✔
915
      return vnodeGetBatchMeta(pVnode, pMsg);
1,147,641✔
916
#ifdef TD_ENTERPRISE
917
    case TDMT_VND_QUERY_COMPACT_PROGRESS:
46✔
918
      return vnodeQueryCompactProgress(pVnode, pMsg);
46✔
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:
4✔
925
      return tqProcessVgCommittedInfoReq(pVnode->pTq, pMsg);
4✔
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) {
191,247✔
936
  vTrace("vgId:%d, msg:%p in stream queue is processing", pVnode->config.vgId, pMsg);
191,247✔
937
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
191,248!
938
       pMsg->msgType == TDMT_VND_BATCH_META) &&
191,283!
939
      !syncIsReadyForRead(pVnode->sync)) {
×
940
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
×
941
    return 0;
×
942
  }
943

944
  switch (pMsg->msgType) {
191,287!
945
    case TDMT_STREAM_TASK_RUN:
161,984✔
946
      return tqProcessTaskRunReq(pVnode->pTq, pMsg);
161,984✔
947
    case TDMT_STREAM_RETRIEVE:
557✔
948
      return tqProcessTaskRetrieveReq(pVnode->pTq, pMsg);
557✔
949
    case TDMT_STREAM_RETRIEVE_RSP:
446✔
950
      return tqProcessTaskRetrieveRsp(pVnode->pTq, pMsg);
446✔
951
    case TDMT_VND_STREAM_SCAN_HISTORY:
2,587✔
952
      return tqProcessTaskScanHistory(pVnode->pTq, pMsg);
2,587✔
953
    case TDMT_STREAM_TASK_CHECKPOINT_READY:
7,707✔
954
      return tqProcessTaskCheckpointReadyMsg(pVnode->pTq, pMsg);
7,707✔
955
    case TDMT_STREAM_TASK_CHECKPOINT_READY_RSP:
7,707✔
956
      return tqProcessTaskCheckpointReadyRsp(pVnode->pTq, pMsg);
7,707✔
957
    case TDMT_STREAM_RETRIEVE_TRIGGER:
×
958
      return tqProcessTaskRetrieveTriggerReq(pVnode->pTq, pMsg);
×
959
    case TDMT_STREAM_RETRIEVE_TRIGGER_RSP:
×
960
      return tqProcessTaskRetrieveTriggerRsp(pVnode->pTq, pMsg);
×
961
    case TDMT_MND_STREAM_REQ_CHKPT_RSP:
4,368✔
962
      return tqProcessStreamReqCheckpointRsp(pVnode->pTq, pMsg);
4,368✔
963
    case TDMT_VND_GET_STREAM_PROGRESS:
×
964
      return tqStreamProgressRetrieveReq(pVnode->pTq, pMsg);
×
965
    case TDMT_MND_STREAM_CHKPT_REPORT_RSP:
5,931✔
966
      return tqProcessTaskChkptReportRsp(pVnode->pTq, pMsg);
5,931✔
967
    default:
×
968
      vError("unknown msg type:%d in stream queue", pMsg->msgType);
×
969
      return TSDB_CODE_APP_ERROR;
×
970
  }
971
}
972

973
int32_t vnodeProcessStreamCtrlMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
183,504✔
974
  vTrace("vgId:%d, msg:%p in stream ctrl queue is processing", pVnode->config.vgId, pMsg);
183,504✔
975
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
183,504!
976
       pMsg->msgType == TDMT_VND_BATCH_META) &&
183,504!
977
      !syncIsReadyForRead(pVnode->sync)) {
×
978
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
×
979
    return 0;
×
980
  }
981

982
  switch (pMsg->msgType) {
183,504!
983
    case TDMT_MND_STREAM_HEARTBEAT_RSP:
23,557✔
984
      return tqProcessStreamHbRsp(pVnode->pTq, pMsg);
23,557✔
985
    case TDMT_STREAM_TASK_DISPATCH:
57,611✔
986
      return tqProcessTaskDispatchReq(pVnode->pTq, pMsg);
57,611✔
987
    case TDMT_STREAM_TASK_DISPATCH_RSP:
57,570✔
988
      return tqProcessTaskDispatchRsp(pVnode->pTq, pMsg);
57,570✔
989
    case TDMT_VND_STREAM_TASK_CHECK:
22,243✔
990
      return tqProcessTaskCheckReq(pVnode->pTq, pMsg);
22,243✔
991
    case TDMT_VND_STREAM_TASK_CHECK_RSP:
22,523✔
992
      return tqProcessTaskCheckRsp(pVnode->pTq, pMsg);
22,523✔
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) {
2✔
1000
  int32_t code = tdProcessTSmaInsert(((SVnode *)pVnode)->pSma, smaId, (const char *)data);
2✔
1001
  if (code) {
2!
1002
    vError("failed to process sma result since %s", tstrerror(code));
×
1003
  }
1004
}
2✔
1005

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

1011
  tstrncpy(pMetaRsp->dbFName, pVnode->config.dbname, TSDB_DB_FNAME_LEN);
149,476✔
1012
  pMetaRsp->dbId = pVnode->config.dbId;
149,476✔
1013
  pMetaRsp->vgId = TD_VID(pVnode);
149,476✔
1014
  pMetaRsp->precision = pVnode->config.tsdbCfg.precision;
149,476✔
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) {
192✔
1020
  if (!pVnode->restored) {
192✔
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;
185✔
1026
  SVTrimDbReq trimReq = {0};
185✔
1027

1028
  // decode
1029
  if (tDeserializeSVTrimDbReq(pReq, len, &trimReq) != 0) {
185!
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);
186!
1035

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

1038
_exit:
186✔
1039
  return code;
186✔
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) {
27,053✔
1175
  int32_t        code = 0;
27,053✔
1176
  SVCreateStbReq req = {0};
27,053✔
1177
  SDecoder       coder;
1178

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

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

1187
  code = tDecodeSVCreateStbReq(&coder, &req);
27,037✔
1188
  if (code) {
26,981!
1189
    pRsp->code = code;
×
1190
    goto _err;
×
1191
  }
1192

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

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

1204
  tDecoderClear(&coder);
27,116✔
1205
  return 0;
27,088✔
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,
75,187✔
1213
                                       SRpcMsg *pOriginRpc) {
1214
  SDecoder           decoder = {0};
75,187✔
1215
  SEncoder           encoder = {0};
75,187✔
1216
  int32_t            rcode = 0;
75,187✔
1217
  SVCreateTbBatchReq req = {0};
75,187✔
1218
  SVCreateTbReq     *pCreateReq;
1219
  SVCreateTbBatchRsp rsp = {0};
75,187✔
1220
  SVCreateTbRsp      cRsp = {0};
75,187✔
1221
  char               tbName[TSDB_TABLE_FNAME_LEN];
1222
  STbUidStore       *pStore = NULL;
75,187✔
1223
  SArray            *tbUids = NULL;
75,187✔
1224
  SArray            *tbNames = NULL;
75,187✔
1225
  pRsp->msgType = TDMT_VND_CREATE_TABLE_RSP;
75,187✔
1226
  pRsp->code = TSDB_CODE_SUCCESS;
75,187✔
1227
  pRsp->pCont = NULL;
75,187✔
1228
  pRsp->contLen = 0;
75,187✔
1229

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

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

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

1252
    if (tsEnableAudit && tsEnableAuditCreateTable) {
113,288!
1253
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
113,295!
1254
      if (str == NULL) {
113,294!
1255
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1256
        rcode = -1;
×
1257
        goto _exit;
×
1258
      }
1259
      tstrncpy(str, pCreateReq->name, TSDB_TABLE_FNAME_LEN);
113,294✔
1260
      if (taosArrayPush(tbNames, &str) == NULL) {
113,297!
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);
113,290✔
1269
    if (vnodeValidateTableHash(pVnode, tbName) < 0) {
113,298✔
1270
      cRsp.code = TSDB_CODE_VND_HASH_MISMATCH;
25✔
1271
      if (taosArrayPush(rsp.pArray, &cRsp) == NULL) {
50!
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);
25!
1277
      continue;
25✔
1278
    }
1279

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

1300
    if (taosArrayPush(rsp.pArray, &cRsp) == NULL) {
226,559!
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));
75,232✔
1308
  if (tqUpdateTbUidList(pVnode->pTq, tbUids, true) < 0) {
75,232!
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) {
75,225!
1312
    goto _exit;
×
1313
  }
1314
  pStore = tdUidStoreFree(pStore);
75,224✔
1315

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

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

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

1338
    SStringBuilder sb = {0};
75,220✔
1339
    for (int32_t i = 0; i < tbNames->size; i++) {
188,497✔
1340
      char **key = (char **)taosArrayGet(tbNames, i);
113,280✔
1341
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
113,280✔
1342
      if (i < tbNames->size - 1) {
113,279✔
1343
        taosStringBuilderAppendChar(&sb, ',');
38,063✔
1344
      }
1345
      // taosMemoryFreeClear(*key);
1346
    }
1347

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

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

1355
    taosStringBuilderDestroy(&sb);
75,218✔
1356
  }
1357

1358
_exit:
×
1359
  tDeleteSVCreateTbBatchReq(&req);
75,216✔
1360
  taosArrayDestroyEx(rsp.pArray, tFreeSVCreateTbRsp);
75,220✔
1361
  taosArrayDestroy(tbUids);
75,225✔
1362
  tDecoderClear(&decoder);
75,220✔
1363
  tEncoderClear(&encoder);
75,223✔
1364
  taosArrayDestroyP(tbNames, NULL);
75,223✔
1365
  return rcode;
75,222✔
1366
}
1367

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

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

1378
  tDecoderInit(&dc, pReq, len);
6,410✔
1379

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

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

1394
  tDecoderClear(&dc);
6,414✔
1395

1396
  return 0;
6,414✔
1397
}
1398

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

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

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

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

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

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

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

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

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

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

1459
  // decode
1460
  if (tDecodeSVAlterTbReq(&dc, &vAlterTbReq) < 0) {
972!
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) {
972✔
1468
    vAlterTbRsp.code = terrno;
38✔
1469
    tDecoderClear(&dc);
38✔
1470
    goto _exit;
38✔
1471
  }
1472
  tDecoderClear(&dc);
934✔
1473

1474
  if (NULL != vMetaRsp.pSchemas) {
934✔
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) {
934✔
1480
    int64_t uid = metaGetTableEntryUidByName(pVnode->pMeta, vAlterTbReq.tbName);
658✔
1481
    if (uid == 0) {
658!
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));
658✔
1488
    void* p = taosArrayPush(tbUids, &uid);
658✔
1489
    TSDB_CHECK_NULL(p, code, lino, _exit, terrno);
658!
1490

1491
    vDebug("vgId:%d, remove tags value altered table:%s from query table list", TD_VID(pVnode), vAlterTbReq.tbName);
658✔
1492
    if ((code = tqUpdateTbUidList(pVnode->pTq, tbUids, false)) < 0) {
658!
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);
658✔
1497
    if ((code = tqUpdateTbUidList(pVnode->pTq, tbUids, true)) < 0) {
658!
1498
      vError("vgId:%d, failed to add tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1499
    }
1500

1501
    taosArrayDestroy(tbUids);
658✔
1502
  }
1503

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

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

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

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

1537
  // decode req
1538
  tDecoderInit(&decoder, pReq, len);
2,601✔
1539
  ret = tDecodeSVDropTbBatchReq(&decoder, &req);
2,601✔
1540
  if (ret < 0) {
2,601!
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,601✔
1548
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(SVDropTbRsp));
2,601✔
1549
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
2,601✔
1550
  if (tbUids == NULL || rsp.pArray == NULL || tbNames == NULL) goto _exit;
2,601!
1551

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

1557
    /* code */
1558
    ret = metaDropTable2(pVnode->pMeta, ver, pDropTbReq);
2,626✔
1559
    if (ret < 0) {
2,626!
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,626✔
1567
      if (tbUid > 0) {
2,626!
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,252!
1575
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1576
      pRsp->code = terrno;
×
1577
      goto _exit;
×
1578
    }
1579

1580
    if (tsEnableAuditCreateTable) {
2,626!
1581
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
2,626!
1582
      if (str == NULL) {
2,626!
1583
        pRsp->code = terrno;
×
1584
        goto _exit;
×
1585
      }
1586
      tstrncpy(str, pDropTbReq->name, TSDB_TABLE_FNAME_LEN);
2,626✔
1587
      if (taosArrayPush(tbNames, &str) == NULL) {
2,626!
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,601!
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,601!
1600
    goto _exit;
×
1601
  }
1602

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

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

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

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

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

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

1631
_exit:
×
1632
  taosArrayDestroy(tbUids);
2,601✔
1633
  pStore = tdUidStoreFree(pStore);
2,601✔
1634
  tDecoderClear(&decoder);
2,601✔
1635
  tEncodeSize(tEncodeSVDropTbBatchRsp, &rsp, pRsp->contLen, ret);
2,601!
1636
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
2,601✔
1637
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
2,601✔
1638
  if (tEncodeSVDropTbBatchRsp(&encoder, &rsp) != 0) {
2,601!
1639
    vError("vgId:%d, failed to encode drop table batch response", TD_VID(pVnode));
×
1640
  }
1641
  tEncoderClear(&encoder);
2,601✔
1642
  taosArrayDestroy(rsp.pArray);
2,601✔
1643
  taosArrayDestroy(tbNames);
2,601✔
1644
  return 0;
2,601✔
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,349,794✔
1864
                                     SRpcMsg *pOriginalMsg) {
1865
  int32_t code = 0;
11,349,794✔
1866
  terrno = 0;
11,349,794✔
1867

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

1874
  pRsp->code = TSDB_CODE_SUCCESS;
11,349,912✔
1875

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

1902
  // scan
1903
  TSKEY now = taosGetTimestamp(pVnode->config.tsdbCfg.precision);
11,349,914✔
1904
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * pVnode->config.tsdbCfg.keep2;
11,349,764✔
1905
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
11,349,764✔
1906
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
23,390,258✔
1907
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
12,039,243✔
1908

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

1914
    if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
12,039,219✔
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,039,067✔
1934
      SRow  **aRow = (SRow **)TARRAY_DATA(pSubmitTbData->aRowP);
12,039,067✔
1935
      SRowKey lastRowKey;
1936
      for (int32_t iRow = 0; iRow < nRow; ++iRow) {
709,244,001✔
1937
        if (aRow[iRow]->ts < minKey || aRow[iRow]->ts > maxKey) {
697,203,659✔
1938
          code = TSDB_CODE_INVALID_MSG;
55,714✔
1939
          vError("vgId:%d %s failed 2 since %s, version:%" PRId64, TD_VID(pVnode), __func__, tstrerror(code), ver);
55,714!
1940
          goto _exit;
×
1941
        }
1942
        if (iRow == 0) {
697,147,945✔
1943
          tRowGetKey(aRow[iRow], &lastRowKey);
12,039,049✔
1944
        } else {
1945
          SRowKey rowKey;
1946
          tRowGetKey(aRow[iRow], &rowKey);
685,108,896✔
1947

1948
          if (tRowKeyCompare(&lastRowKey, &rowKey) >= 0) {
685,098,995!
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;
685,164,725✔
1954
        }
1955
      }
1956
    }
1957
  }
1958

1959
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
23,391,249✔
1960
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
12,039,962✔
1961

1962
    if (pSubmitTbData->pCreateTbReq) {
12,039,653✔
1963
      pSubmitTbData->uid = pSubmitTbData->pCreateTbReq->uid;
63,246✔
1964
    } else {
1965
      SMetaInfo info = {0};
11,976,407✔
1966

1967
      code = metaGetInfo(pVnode->pMeta, pSubmitTbData->uid, &info, NULL);
11,976,407✔
1968
      if (code) {
11,977,042!
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;
7✔
1972
      }
1973

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

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

1985
      if (pSubmitTbData->sver != info.skmVer) {
11,976,988!
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,040,234✔
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++) {
647✔
2007
        if (aColData[j].nVal != aColData[0].nVal) {
495!
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,351,287✔
2016

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

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

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

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

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

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

2048
        if (pCreateTbRsp->pMeta) {
37,397!
2049
          vnodeUpdateMetaRsp(pVnode, pCreateTbRsp->pMeta);
37,397✔
2050
        }
2051
      } else {  // create table failed
2052
        if (terrno != TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
25,842✔
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,834✔
2059
        pSubmitTbData->uid = pSubmitTbData->pCreateTbReq->uid;  // update uid if table exist for using below
25,834✔
2060
      }
2061
    }
2062

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

2068
    code = metaUpdateChangeTimeWithLock(pVnode->pMeta, pSubmitTbData->uid, pSubmitTbData->ctimeMs);
12,038,318✔
2069
    if (code) goto _exit;
12,036,791!
2070

2071
    pSubmitRsp->affectedRows += affectedRows;
12,036,791✔
2072
  }
2073

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

2083
_exit:
11,349,841✔
2084
  // message
2085
  pRsp->code = code;
11,349,856✔
2086
  tEncodeSize(tEncodeSSubmitRsp2, pSubmitRsp, pRsp->contLen, ret);
11,349,856!
2087
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
11,349,667✔
2088
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
11,349,961✔
2089
  if (tEncodeSSubmitRsp2(&ec, pSubmitRsp) < 0) {
11,349,967!
2090
    vError("vgId:%d, failed to encode submit response", TD_VID(pVnode));
×
2091
  }
2092
  tEncoderClear(&ec);
11,349,798✔
2093

2094
  // update statistics
2095
  (void)atomic_add_fetch_64(&pVnode->statis.nInsert, pSubmitRsp->affectedRows);
11,349,895✔
2096
  (void)atomic_add_fetch_64(&pVnode->statis.nInsertSuccess, pSubmitRsp->affectedRows);
11,349,944✔
2097
  (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsert, 1);
11,349,964✔
2098

2099
  if (tsEnableMonitor && tsMonitorFqdn[0] != 0 && tsMonitorPort != 0 && pSubmitRsp->affectedRows > 0 &&
11,349,975!
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,349,975✔
2112
    (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
11,349,958✔
2113
    code = tdProcessRSmaSubmit(pVnode->pSma, ver, pSubmitReq, pReq, len);
11,350,003✔
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,349,908✔
2135
  tDestroySubmitReq(pSubmitReq, 0 == pMsg->version ? TSDB_MSG_FLG_CMPT : TSDB_MSG_FLG_DECODE);
11,349,795!
2136
  tDestroySSubmitRsp2(pSubmitRsp, TSDB_MSG_FLG_ENCODE);
11,349,831✔
2137

2138
  if (code) terrno = code;
11,349,763✔
2139

2140
  taosMemoryFree(pAllocMsg);
11,349,763!
2141

2142
  return code;
11,349,684✔
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) {
7,966✔
2207
  vInfo("vgId:%d, vnode handle msgType:alter-confirm, alter confirm msg is processed", TD_VID(pVnode));
7,966!
2208
  int32_t code = TSDB_CODE_SUCCESS;
7,966✔
2209
  if (!pVnode->config.hashChange) {
7,966✔
2210
    goto _exit;
7,934✔
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:
7,966✔
2221
  pRsp->msgType = TDMT_VND_ALTER_CONFIRM_RSP;
7,966✔
2222
  pRsp->code = code;
7,966✔
2223
  pRsp->pCont = NULL;
7,966✔
2224
  pRsp->contLen = 0;
7,966✔
2225

2226
  return code;
7,966✔
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) {
643✔
2233
  bool walChanged = false;
643✔
2234
  bool tsdbChanged = false;
643✔
2235

2236
  SAlterVnodeConfigReq req = {0};
643✔
2237
  if (tDeserializeSAlterVnodeConfigReq(pReq, len, &req) != 0) {
643!
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
645✔
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) {
646✔
2252
    pVnode->config.cacheLastSize = req.cacheLastSize;
250✔
2253
    tsdbCacheSetCapacity(pVnode, (size_t)pVnode->config.cacheLastSize * 1024 * 1024);
250✔
2254
  }
2255

2256
  if (pVnode->config.szBuf != req.buffer * 1024LL * 1024LL) {
646!
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) {
646!
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) {
646✔
2274
    pVnode->config.cacheLast = req.cacheLast;
363✔
2275
  }
2276

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

2282
  if (pVnode->config.walCfg.level != req.walLevel) {
646✔
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) {
646✔
2291
    pVnode->config.walCfg.retentionPeriod = req.walRetentionPeriod;
132✔
2292
    walChanged = true;
132✔
2293
  }
2294

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

2300
  if (pVnode->config.tsdbCfg.keep0 != req.daysToKeep0) {
646✔
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) {
646✔
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) {
646✔
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) {
646✔
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) {
646!
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) {
646!
2345
    pVnode->config.tsdbCfg.minRows = req.minRows;
×
2346
  }
2347

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

2355
  if (walChanged) {
646✔
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) {
645✔
2362
    tsdbSetKeepCfg(pVnode->pTsdb, &pVnode->config.tsdbCfg);
58✔
2363
  }
2364

2365
  return 0;
645✔
2366
}
2367

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

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

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

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

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

2396
    int32_t code = tsdbDeleteTableData(pTsdb, ver, deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
1,856✔
2397
    if (code < 0) {
1,858!
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,858✔
2404
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, deleteReq.ctimeMs);
1,242✔
2405
      if (code < 0) {
1,242!
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,858✔
2413
  }
2414
  metaReaderClear(&mr);
2,899✔
2415
  taosArrayDestroy(deleteReq.deleteReqs);
2,900✔
2416
  return 0;
2,902✔
2417
}
2418

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

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

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

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

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

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

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

2455
  SVDeleteRsp rsp = {.affectedRows = pRes->affectedRows};
68,733✔
2456
  int32_t     ret = 0;
68,733✔
2457
  tEncodeSize(tEncodeSVDeleteRsp, &rsp, pRsp->contLen, ret);
68,733!
2458
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
68,731✔
2459
  SEncoder ec = {0};
68,730✔
2460
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
68,730✔
2461
  code = tEncodeSVDeleteRsp(&ec, &rsp);
68,731✔
2462
  if (code) goto _err;
68,729!
2463
  tEncoderClear(&ec);
68,729✔
2464
  return code;
68,733✔
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) {
971✔
2485
  SVCreateStbReq req = {0};
971✔
2486
  SDecoder       dc = {0};
971✔
2487
  int32_t        code = 0;
971✔
2488

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

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

2501
  code = metaAddIndexToSuperTable(pVnode->pMeta, ver, &req);
971✔
2502
  if (code) {
971!
2503
    pRsp->code = code;
×
2504
    goto _err;
×
2505
  }
2506
  tDecoderClear(&dc);
971✔
2507
  return 0;
971✔
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,146✔
2514
  SDropIndexReq req = {0};
2,146✔
2515
  int32_t       code = 0;
2,146✔
2516
  pRsp->msgType = TDMT_VND_DROP_INDEX_RSP;
2,146✔
2517
  pRsp->code = TSDB_CODE_SUCCESS;
2,146✔
2518
  pRsp->pCont = NULL;
2,146✔
2519
  pRsp->contLen = 0;
2,146✔
2520

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

2526
  code = metaDropIndexFromSuperTable(pVnode->pMeta, ver, &req);
2,146✔
2527
  if (code) {
2,146!
2528
    pRsp->code = code;
×
2529
    return code;
×
2530
  }
2531
  return TSDB_CODE_SUCCESS;
2,146✔
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) {
202✔
2537
  if (!pVnode->restored) {
202✔
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);
29✔
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) {
9✔
2558
  SSyncState syncState = syncGetState(pVnode->sync);
9✔
2559
  if (syncState.state != TAOS_SYNC_STATE_LEADER) {
9✔
2560
    return terrno = TSDB_CODE_SYN_NOT_LEADER;
5✔
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✔
2570
    return terrno = TSDB_CODE_MND_ARB_TOKEN_MISMATCH;
2✔
2571
  }
2572

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

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

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

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

2589
  SVArbCheckSyncReq syncReq = {0};
6✔
2590

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

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

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

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

2612
  if (vnodeUpdateArbTerm(pVnode, syncReq.arbTerm) != 0) {
6!
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);
6✔
2619
  if (contLen <= 0) {
6!
2620
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2621
    code = -1;
×
2622
    goto _OVER;
×
2623
  }
2624
  void *pHead = rpcMallocCont(contLen);
6✔
2625
  if (!pHead) {
6!
2626
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2627
    code = -1;
×
2628
    goto _OVER;
×
2629
  }
2630

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

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

2641
  terrno = TSDB_CODE_SUCCESS;
6✔
2642

2643
_OVER:
6✔
2644
  tFreeSVArbCheckSyncReq(&syncReq);
6✔
2645
  return code;
6✔
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