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

taosdata / TDengine / #4308

14 Jun 2025 02:06PM UTC coverage: 62.454% (-0.3%) from 62.777%
#4308

push

travis-ci

web-flow
fix: taosdump windows pthread_mutex_unlock crash(3.0) (#31357)

* fix: windows pthread_mutex_unlock crash

* enh: sync from main fix taosdump crash windows

* fix: restore .github action branch to main

153985 of 315105 branches covered (48.87%)

Branch coverage included in aggregate %.

238120 of 312727 relevant lines covered (76.14%)

6462519.65 hits per line

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

60.78
/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) {
129,175✔
64
  int32_t code = 0;
129,175✔
65
  int32_t lino = 0;
129,175✔
66

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

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

78
  // name
79
  char *name = NULL;
129,255✔
80
  if (tDecodeCStr(pCoder, &name) < 0) {
129,224!
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);
129,224✔
87
  if (uid == 0) {
129,215✔
88
    uid = tGenIdPI64();
127,761✔
89
  }
90
  taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), uid);
129,184✔
91

92
  // btime
93
  taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos + 8), btime);
129,184✔
94

95
  tEndDecode(pCoder);
129,184✔
96

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

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

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

120
  if (tDecodeI32v(&dc, &nReqs) < 0) {
67,572!
121
    code = TSDB_CODE_INVALID_MSG;
×
122
    TSDB_CHECK_CODE(code, lino, _exit);
×
123
  }
124
  for (int32_t iReq = 0; iReq < nReqs; iReq++) {
150,093✔
125
    code = vnodePreprocessCreateTableReq(pVnode, &dc, btime, NULL);
82,544✔
126
    TSDB_CHECK_CODE(code, lino, _exit);
82,521!
127
  }
128

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

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

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

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

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

157
  code = 0;
5,487✔
158

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

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

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

177
  SArray *tbUids = NULL;
19,654✔
178
  int64_t timestampMs = 0;
19,654✔
179

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

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

200
    ttlReq.nUids = taosArrayGetSize(tbUids);
19,644✔
201
    ttlReq.pTbUids = tbUids;
19,642✔
202
  }
203

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

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

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

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

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

230
  code = 0;
33✔
231

232
_exit:
19,652✔
233
  taosArrayDestroy(tbUids);
19,652✔
234

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

242
  return code;
19,651✔
243
}
244

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

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

264
  int64_t uid;
265
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
2,832,177✔
266
    code = vnodePreprocessCreateTableReq(pVnode, pCoder, btimeMs, &uid);
46,652✔
267
    TSDB_CHECK_CODE(code, lino, _exit);
46,651!
268
  }
269

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

276
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
2,832,168✔
277
    taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), uid);
46,649✔
278
    pCoder->pos += sizeof(int64_t);
46,649✔
279
  } else {
280
    if (tDecodeI64(pCoder, &submitTbData.uid) < 0) {
2,785,486!
281
      code = TSDB_CODE_INVALID_MSG;
×
282
      TSDB_CHECK_CODE(code, lino, _exit);
×
283
    }
284
  }
285

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

291
  // scan and check
292
  TSKEY now = btimeMs;
2,832,142✔
293
  if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_MICRO) {
2,832,142✔
294
    now *= 1000;
16,250✔
295
  } else if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_NANO) {
2,815,892✔
296
    now *= 1000000;
3,759✔
297
  }
298

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

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

327
    if (colData.flag != HAS_VALUE) {
10,148!
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++) {
9,964,600✔
333
      if (((TSKEY *)colData.pData)[iRow] < minKey || ((TSKEY *)colData.pData)[iRow] > maxKey) {
9,954,453!
334
        code = TSDB_CODE_TDB_TIMESTAMP_OUT_OF_RANGE;
11✔
335
        TSDB_CHECK_CODE(code, lino, _exit);
11✔
336
      }
337
    }
338

339
    for (uint64_t i = 1; i < nColData; i++) {
30,671✔
340
      code = tDecodeColData(version, pCoder, &colData);
20,526✔
341
      if (code) {
20,524!
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) {
2,821,870!
349
      code = TSDB_CODE_INVALID_MSG;
×
350
      TSDB_CHECK_CODE(code, lino, _exit);
97!
351
    }
352

353
    for (int32_t iRow = 0; iRow < nRow; ++iRow) {
123,118,781✔
354
      SRow *pRow = (SRow *)(pCoder->data + pCoder->pos);
120,297,008✔
355
      pCoder->pos += pRow->len;
120,297,008✔
356
#ifndef NO_UNALIGNED_ACCESS
357
      if (pRow->ts < minKey || pRow->ts > maxKey) {
120,297,008✔
358
#else
359
      TSKEY ts = taosGetInt64Aligned(&pRow->ts);
360
      if (ts < minKey || ts > maxKey) {
361
#endif
362
        code = TSDB_CODE_TDB_TIMESTAMP_OUT_OF_RANGE;
9,580✔
363
        TSDB_CHECK_CODE(code, lino, _exit);
9,580✔
364
      }
365
    }
366
  }
367

368
  if (!tDecodeIsEnd(pCoder)) {
2,831,918!
369
    taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), ctimeMs);
2,832,027✔
370
    pCoder->pos += sizeof(int64_t);
2,832,027✔
371
  }
372

373
  tEndDecode(pCoder);
2,831,918✔
374

375
_exit:
2,832,063✔
376
  if (code) {
2,832,063✔
377
    vError("vgId:%d, %s:%d failed to vnodePreProcessSubmitTbData submit request since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
98!
378
  }
379
  return code;
2,832,117✔
380
}
381
static int32_t vnodePreProcessSubmitMsg(SVnode *pVnode, SRpcMsg *pMsg) {
2,136,768✔
382
  int32_t code = 0;
2,136,768✔
383
  int32_t lino = 0;
2,136,768✔
384

385
  if (tsBypassFlag & TSDB_BYPASS_RA_RPC_RECV_SUBMIT) {
2,136,768!
386
    return TSDB_CODE_MSG_PREPROCESSED;
×
387
  }
388

389
  SDecoder *pCoder = &(SDecoder){0};
2,136,768✔
390

391
  if (taosHton64(((SSubmitReq2Msg *)pMsg->pCont)->version) != 1) {
2,136,768!
392
    code = TSDB_CODE_INVALID_MSG;
×
393
    TSDB_CHECK_CODE(code, lino, _exit);
×
394
  }
395

396
  tDecoderInit(pCoder, (uint8_t *)pMsg->pCont + sizeof(SSubmitReq2Msg), pMsg->contLen - sizeof(SSubmitReq2Msg));
2,136,905✔
397

398
  if (tStartDecode(pCoder) < 0) {
2,136,818!
399
    code = TSDB_CODE_INVALID_MSG;
×
400
    TSDB_CHECK_CODE(code, lino, _exit);
×
401
  }
402

403
  uint64_t nSubmitTbData;
404
  if (tDecodeU64v(pCoder, &nSubmitTbData) < 0) {
2,137,010!
405
    code = TSDB_CODE_INVALID_MSG;
×
406
    TSDB_CHECK_CODE(code, lino, _exit);
×
407
  }
408

409
  int64_t btimeMs = taosGetTimestampMs();
2,136,888✔
410
  int64_t ctimeMs = btimeMs;
2,136,888✔
411
  for (int32_t i = 0; i < nSubmitTbData; i++) {
4,968,916✔
412
    code = vnodePreProcessSubmitTbData(pVnode, pCoder, btimeMs, ctimeMs);
2,832,107✔
413
    TSDB_CHECK_CODE(code, lino, _exit);
2,832,126✔
414
  }
415

416
  tEndDecode(pCoder);
2,136,809✔
417

418
_exit:
2,136,981✔
419
  tDecoderClear(pCoder);
2,136,981✔
420
  if (code) {
2,136,860✔
421
    vError("vgId:%d, %s:%d failed to preprocess submit request since %s, msg type:%s", TD_VID(pVnode), __func__, lino,
98!
422
           tstrerror(code), TMSG_INFO(pMsg->msgType));
423
  }
424
  return code;
2,136,797✔
425
}
426

427
static int32_t vnodePreProcessDeleteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
7,234✔
428
  int32_t code = 0;
7,234✔
429

430
  int32_t    size;
431
  int32_t    ret;
432
  uint8_t   *pCont;
433
  SEncoder  *pCoder = &(SEncoder){0};
7,234✔
434
  SDeleteRes res = {0};
7,234✔
435

436
  SReadHandle handle = {.vnode = pVnode, .pMsgCb = &pVnode->msgCb, .skipRollup = 1};
7,234✔
437
  initStorageAPI(&handle.api);
7,234✔
438

439
  code = qWorkerProcessDeleteMsg(&handle, pVnode->pQuery, pMsg, &res);
7,234✔
440
  if (code) goto _exit;
7,234!
441

442
  res.ctimeMs = taosGetTimestampMs();
7,234✔
443
  // malloc and encode
444
  tEncodeSize(tEncodeDeleteRes, &res, size, ret);
7,234!
445
  pCont = rpcMallocCont(size + sizeof(SMsgHead));
7,234✔
446

447
  ((SMsgHead *)pCont)->contLen = size + sizeof(SMsgHead);
7,234✔
448
  ((SMsgHead *)pCont)->vgId = TD_VID(pVnode);
7,234✔
449

450
  tEncoderInit(pCoder, pCont + sizeof(SMsgHead), size);
7,234✔
451
  if (tEncodeDeleteRes(pCoder, &res) != 0) {
7,234!
452
    vError("vgId:%d %s failed to encode delete response", TD_VID(pVnode), __func__);
×
453
  }
454
  tEncoderClear(pCoder);
7,234✔
455

456
  rpcFreeCont(pMsg->pCont);
7,234✔
457
  pMsg->pCont = pCont;
7,234✔
458
  pMsg->contLen = size + sizeof(SMsgHead);
7,234✔
459

460
  taosArrayDestroy(res.uidList);
7,234✔
461

462
_exit:
7,234✔
463
  return code;
7,234✔
464
}
465

466
static int32_t vnodePreProcessBatchDeleteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
1,045✔
467
  int32_t code = 0;
1,045✔
468
  int32_t lino = 0;
1,045✔
469

470
  int64_t         ctimeMs = taosGetTimestampMs();
1,045✔
471
  SBatchDeleteReq pReq = {0};
1,045✔
472
  SDecoder       *pCoder = &(SDecoder){0};
1,045✔
473

474
  tDecoderInit(pCoder, (uint8_t *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead));
1,045✔
475

476
  if (tDecodeSBatchDeleteReqSetCtime(pCoder, &pReq, ctimeMs) < 0) {
1,045!
477
    code = TSDB_CODE_INVALID_MSG;
×
478
  }
479

480
  tDecoderClear(pCoder);
1,045✔
481
  taosArrayDestroy(pReq.deleteReqs);
1,045✔
482

483
  if (code) {
1,045!
484
    vError("vgId:%d %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
485
  } else {
486
    vTrace("vgId:%d %s done, ctimeMs generated:%" PRId64, TD_VID(pVnode), __func__, ctimeMs);
1,045✔
487
  }
488
  return code;
1,045✔
489
}
490

491
static int32_t vnodePreProcessArbCheckSyncMsg(SVnode *pVnode, SRpcMsg *pMsg) {
24✔
492
  SVArbCheckSyncReq syncReq = {0};
24✔
493

494
  if (tDeserializeSVArbCheckSyncReq((char *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead),
24!
495
                                    &syncReq) != 0) {
496
    return TSDB_CODE_INVALID_MSG;
×
497
  }
498

499
  int32_t ret = vnodeCheckToken(pVnode, syncReq.member0Token, syncReq.member1Token);
24✔
500
  if (ret != 0) {
24✔
501
    vError("vgId:%d, failed to preprocess arb check sync request since %s", TD_VID(pVnode), tstrerror(ret));
20!
502
  }
503

504
  int32_t code = terrno;
24✔
505
  tFreeSVArbCheckSyncReq(&syncReq);
24✔
506

507
  return code;
24✔
508
}
509

510
int32_t vnodePreProcessDropTbMsg(SVnode *pVnode, SRpcMsg *pMsg) {
15,834✔
511
  int32_t          code = TSDB_CODE_SUCCESS;
15,834✔
512
  int32_t          lino = 0;
15,834✔
513
  int32_t          size = 0;
15,834✔
514
  SDecoder         dc = {0};
15,834✔
515
  SEncoder         ec = {0};
15,834✔
516
  SVDropTbBatchReq receivedBatchReqs = {0};
15,834✔
517
  SVDropTbBatchReq sentBatchReqs = {0};
15,834✔
518

519
  tDecoderInit(&dc, POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), pMsg->contLen - sizeof(SMsgHead));
15,834✔
520

521
  code = tDecodeSVDropTbBatchReq(&dc, &receivedBatchReqs);
15,834✔
522
  if (code < 0) {
15,834!
523
    terrno = code;
×
524
    TSDB_CHECK_CODE(code, lino, _exit);
×
525
  }
526
  sentBatchReqs.pArray = taosArrayInit(receivedBatchReqs.nReqs, sizeof(SVDropTbReq));
15,834✔
527
  if (!sentBatchReqs.pArray) {
15,834!
528
    code = terrno;
×
529
    goto _exit;
×
530
  }
531

532
  for (int32_t i = 0; i < receivedBatchReqs.nReqs; ++i) {
31,688✔
533
    SVDropTbReq *pReq = receivedBatchReqs.pReqs + i;
15,854✔
534
    tb_uid_t     uid = metaGetTableEntryUidByName(pVnode->pMeta, pReq->name);
15,854✔
535
    if (uid == 0) {
15,854!
536
      vWarn("vgId:%d, preprocess drop ctb: %s not found", TD_VID(pVnode), pReq->name);
×
537
      continue;
×
538
    }
539
    pReq->uid = uid;
15,854✔
540
    vDebug("vgId:%d %s for: %s, uid: %" PRId64, TD_VID(pVnode), __func__, pReq->name, pReq->uid);
15,854✔
541
    if (taosArrayPush(sentBatchReqs.pArray, pReq) == NULL) {
31,708!
542
      code = terrno;
×
543
      goto _exit;
×
544
    }
545
  }
546
  sentBatchReqs.nReqs = sentBatchReqs.pArray->size;
15,834✔
547

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

557
_exit:
15,834✔
558
  tDecoderClear(&dc);
15,834✔
559
  if (sentBatchReqs.pArray) {
15,834!
560
    taosArrayDestroy(sentBatchReqs.pArray);
15,834✔
561
  }
562
  return code;
15,834✔
563
}
564

565
int32_t vnodePreProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
2,336,298✔
566
  int32_t code = 0;
2,336,298✔
567

568
  switch (pMsg->msgType) {
2,336,298✔
569
    case TDMT_VND_CREATE_TABLE: {
67,565✔
570
      code = vnodePreProcessCreateTableMsg(pVnode, pMsg);
67,565✔
571
    } break;
67,561✔
572
    case TDMT_VND_ALTER_TABLE: {
5,487✔
573
      code = vnodePreProcessAlterTableMsg(pVnode, pMsg);
5,487✔
574
    } break;
5,487✔
575
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
19,659✔
576
    case TDMT_VND_DROP_TTL_TABLE: {
577
      code = vnodePreProcessDropTtlMsg(pVnode, pMsg);
19,659✔
578
    } break;
19,652✔
579
    case TDMT_VND_SUBMIT: {
2,136,944✔
580
      code = vnodePreProcessSubmitMsg(pVnode, pMsg);
2,136,944✔
581
    } break;
2,136,752✔
582
    case TDMT_VND_DELETE: {
7,234✔
583
      code = vnodePreProcessDeleteMsg(pVnode, pMsg);
7,234✔
584
    } break;
7,234✔
585
    case TDMT_VND_BATCH_DEL: {
1,045✔
586
      code = vnodePreProcessBatchDeleteMsg(pVnode, pMsg);
1,045✔
587
    } break;
1,045✔
588
    case TDMT_VND_ARB_CHECK_SYNC: {
24✔
589
      code = vnodePreProcessArbCheckSyncMsg(pVnode, pMsg);
24✔
590
    } break;
24✔
591
    case TDMT_VND_DROP_TABLE: {
15,834✔
592
      code = vnodePreProcessDropTbMsg(pVnode, pMsg);
15,834✔
593
    } break;
15,834✔
594
    default:
82,506✔
595
      break;
82,506✔
596
  }
597

598
  if (code && code != TSDB_CODE_MSG_PREPROCESSED) {
2,336,095✔
599
    vError("vgId:%d, failed to preprocess write request since %s, msg type:%s", TD_VID(pVnode), tstrerror(code),
118!
600
           TMSG_INFO(pMsg->msgType));
601
  }
602
  return code;
2,336,135✔
603
}
604

605
int32_t vnodeProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg, int64_t ver, SRpcMsg *pRsp) {
3,134,812✔
606
  int32_t code = 0;
3,134,812✔
607
  void   *ptr = NULL;
3,134,812✔
608
  void   *pReq;
609
  int32_t len;
610

611
  (void)taosThreadMutexLock(&pVnode->mutex);
3,134,812✔
612
  if (pVnode->disableWrite) {
3,135,109!
613
    (void)taosThreadMutexUnlock(&pVnode->mutex);
×
614
    vError("vgId:%d write is disabled for snapshot, version:%" PRId64, TD_VID(pVnode), ver);
×
615
    return TSDB_CODE_VND_WRITE_DISABLED;
×
616
  }
617
  (void)taosThreadMutexUnlock(&pVnode->mutex);
3,135,109✔
618

619
  if (ver <= pVnode->state.applied) {
3,135,051!
620
    vError("vgId:%d, duplicate write request. ver: %" PRId64 ", applied: %" PRId64, TD_VID(pVnode), ver,
×
621
           pVnode->state.applied);
622
    return terrno = TSDB_CODE_VND_DUP_REQUEST;
×
623
  }
624

625
  vGDebug(&pMsg->info.traceId,
3,135,051!
626
          "vgId:%d, index:%" PRId64 ", process write request:%s, applied:%" PRId64 ", state.applyTerm:%" PRId64
627
          ", conn.applyTerm:%" PRId64 ", contLen:%d",
628
          TD_VID(pVnode), ver, TMSG_INFO(pMsg->msgType), pVnode->state.applied, pVnode->state.applyTerm,
629
          pMsg->info.conn.applyTerm, pMsg->contLen);
630

631
  if (!(pVnode->state.applyTerm <= pMsg->info.conn.applyTerm)) {
3,134,547!
632
    vError("vgId:%d, applyTerm mismatch, expected: %" PRId64 ", received: %" PRId64, TD_VID(pVnode),
×
633
           pVnode->state.applyTerm, pMsg->info.conn.applyTerm);
634
    return terrno = TSDB_CODE_INTERNAL_ERROR;
×
635
  }
636

637
  if (!(pVnode->state.applied + 1 == ver)) {
3,134,547!
638
    vError("vgId:%d, ver mismatch, expected: %" PRId64 ", received: %" PRId64, TD_VID(pVnode),
×
639
           pVnode->state.applied + 1, ver);
640
    return terrno = TSDB_CODE_INTERNAL_ERROR;
×
641
  }
642

643
  atomic_store_64(&pVnode->state.applied, ver);
3,134,547✔
644
  atomic_store_64(&pVnode->state.applyTerm, pMsg->info.conn.applyTerm);
3,135,129✔
645

646
  if (!syncUtilUserCommit(pMsg->msgType)) goto _exit;
3,135,071✔
647

648
  // skip header
649
  pReq = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
3,101,134✔
650
  len = pMsg->contLen - sizeof(SMsgHead);
3,101,134✔
651
  bool needCommit = false;
3,101,134✔
652

653
  switch (pMsg->msgType) {
3,101,134!
654
    /* META */
655
    case TDMT_VND_CREATE_STB:
26,511✔
656
      if (vnodeProcessCreateStbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
26,511✔
657
      break;
26,527✔
658
    case TDMT_VND_ALTER_STB:
9,920✔
659
      if (vnodeProcessAlterStbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
9,920!
660
      break;
9,933✔
661
    case TDMT_VND_DROP_STB:
2,061✔
662
      if (vnodeProcessDropStbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
2,061!
663
      break;
2,065✔
664
    case TDMT_VND_CREATE_TABLE:
77,483✔
665
      if (vnodeProcessCreateTbReq(pVnode, ver, pReq, len, pRsp, pMsg) < 0) goto _err;
77,483!
666
      break;
77,551✔
667
    case TDMT_VND_ALTER_TABLE:
5,491✔
668
      if (vnodeProcessAlterTbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
5,491!
669
      break;
5,491✔
670
    case TDMT_VND_DROP_TABLE:
15,875✔
671
      if (vnodeProcessDropTbReq(pVnode, ver, pReq, len, pRsp, pMsg) < 0) goto _err;
15,875!
672
      break;
15,875✔
673
    case TDMT_VND_DROP_TTL_TABLE:
×
674
      if (vnodeProcessDropTtlTbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
675
      break;
×
676
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
32✔
677
      if (vnodeProcessFetchTtlExpiredTbs(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
32!
678
      break;
33✔
679
    case TDMT_VND_TRIM:
35✔
680
      if (vnodeProcessTrimReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
35!
681
      break;
36✔
682
    case TDMT_VND_S3MIGRATE:
×
683
      if (vnodeProcessS3MigrateReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
684
      break;
×
685
    case TDMT_VND_CREATE_SMA:
×
686
      if (vnodeProcessCreateTSmaReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
687
      break;
×
688
    /* TSDB */
689
    case TDMT_VND_SUBMIT:
2,887,415✔
690
      if (vnodeProcessSubmitReq(pVnode, ver, pMsg->pCont, pMsg->contLen, pRsp, pMsg) < 0) goto _err;
2,887,415✔
691
      break;
2,887,501✔
692
    case TDMT_VND_DELETE:
8,452✔
693
      if (vnodeProcessDeleteReq(pVnode, ver, pReq, len, pRsp, pMsg) < 0) goto _err;
8,452!
694
      break;
8,452✔
695
    case TDMT_VND_BATCH_DEL:
1,045✔
696
      if (vnodeProcessBatchDeleteReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
1,045!
697
      break;
1,045✔
698
    /* TQ */
699
#if defined(USE_TQ) || defined(USE_STREAM)
700
    case TDMT_VND_TMQ_SUBSCRIBE:
4,284✔
701
      if (tqProcessSubscribeReq(pVnode->pTq, ver, pReq, len) < 0) {
4,284!
702
        goto _err;
×
703
      }
704
      break;
4,286✔
705
    case TDMT_VND_TMQ_DELETE_SUB:
933✔
706
      if (tqProcessDeleteSubReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen) < 0) {
933!
707
        goto _err;
×
708
      }
709
      break;
935✔
710
    case TDMT_VND_TMQ_COMMIT_OFFSET:
8,404✔
711
      if (tqProcessOffsetCommitReq(pVnode->pTq, ver, pReq, len) < 0) {
8,404!
712
        goto _err;
×
713
      }
714
      break;
8,413✔
715
    case TDMT_VND_TMQ_ADD_CHECKINFO:
137✔
716
      if (tqProcessAddCheckInfoReq(pVnode->pTq, ver, pReq, len) < 0) {
137!
717
        goto _err;
×
718
      }
719
      break;
137✔
720
    case TDMT_VND_TMQ_DEL_CHECKINFO:
24✔
721
      if (tqProcessDelCheckInfoReq(pVnode->pTq, ver, pReq, len) < 0) {
24!
722
        goto _err;
×
723
      }
724
      break;
24✔
725
    case TDMT_STREAM_TASK_DEPLOY: {
6,607✔
726
      if ((code = tqProcessTaskDeployReq(pVnode->pTq, ver, pReq, len)) != TSDB_CODE_SUCCESS) {
6,607!
727
        goto _err;
×
728
      }
729
    } break;
6,608✔
730
    case TDMT_STREAM_TASK_DROP: {
2,657✔
731
      if ((code = tqProcessTaskDropReq(pVnode->pTq, pMsg->pCont, pMsg->contLen)) < 0) {
2,657!
732
        goto _err;
×
733
      }
734
    } break;
2,657✔
735
    case TDMT_STREAM_TASK_UPDATE_CHKPT: {
1,360✔
736
      if ((code = tqProcessTaskUpdateCheckpointReq(pVnode->pTq, pMsg->pCont, pMsg->contLen)) < 0) {
1,360!
737
        goto _err;
×
738
      }
739
    } break;
1,361✔
740
    case TDMT_STREAM_CONSEN_CHKPT: {
26✔
741
      if (pVnode->restored && (code = tqProcessTaskConsenChkptIdReq(pVnode->pTq, pMsg)) < 0) {
26!
742
        goto _err;
×
743
      }
744

745
    } break;
26✔
746
    case TDMT_STREAM_TASK_PAUSE: {
231✔
747
      if (pVnode->restored && vnodeIsLeader(pVnode) &&
372!
748
          (code = tqProcessTaskPauseReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen)) < 0) {
141✔
749
        goto _err;
×
750
      }
751
    } break;
231✔
752
    case TDMT_STREAM_TASK_RESUME: {
224✔
753
      if (pVnode->restored && vnodeIsLeader(pVnode) &&
358!
754
          (code = tqProcessTaskResumeReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen)) < 0) {
134✔
755
        goto _err;
×
756
      }
757
    } break;
224✔
758
    case TDMT_VND_STREAM_TASK_RESET: {
×
759
      if (pVnode->restored && vnodeIsLeader(pVnode) && (code = tqProcessTaskResetReq(pVnode->pTq, pMsg)) < 0) {
×
760
        goto _err;
×
761
      }
762

763
    } break;
×
764
    case TDMT_VND_STREAM_ALL_STOP: {
3,733✔
765
      if (pVnode->restored && vnodeIsLeader(pVnode) && (code = tqProcessAllTaskStopReq(pVnode->pTq, pMsg)) < 0) {
3,733!
766
        goto _err;
×
767
      }
768

769
    } break;
3,736✔
770
#endif
771
    case TDMT_VND_ALTER_CONFIRM:
11,644✔
772
      needCommit = pVnode->config.hashChange;
11,644✔
773
      if (vnodeProcessAlterConfirmReq(pVnode, ver, pReq, len, pRsp) < 0) {
11,644!
774
        goto _err;
×
775
      }
776
      break;
11,644✔
777
    case TDMT_VND_ALTER_CONFIG:
616✔
778
      vnodeProcessAlterConfigReq(pVnode, ver, pReq, len, pRsp);
616✔
779
      break;
616✔
780
    case TDMT_VND_COMMIT:
24,682✔
781
      needCommit = true;
24,682✔
782
      break;
24,682✔
783
    case TDMT_VND_CREATE_INDEX:
45✔
784
      vnodeProcessCreateIndexReq(pVnode, ver, pReq, len, pRsp);
45✔
785
      break;
45✔
786
    case TDMT_VND_DROP_INDEX:
36✔
787
      vnodeProcessDropIndexReq(pVnode, ver, pReq, len, pRsp);
36✔
788
      break;
36✔
789
    case TDMT_VND_STREAM_CHECK_POINT_SOURCE:  // always return true
886✔
790
      tqProcessTaskCheckPointSourceReq(pVnode->pTq, pMsg, pRsp);
886✔
791
      break;
886✔
792
    case TDMT_VND_STREAM_TASK_UPDATE:  // always return true
28✔
793
      tqProcessTaskUpdateReq(pVnode->pTq, pMsg);
28✔
794
      break;
28✔
795
    case TDMT_VND_COMPACT:
247✔
796
      vnodeProcessCompactVnodeReq(pVnode, ver, pReq, len, pRsp);
247✔
797
      goto _exit;
247✔
798
    case TDMT_SYNC_CONFIG_CHANGE:
×
799
      vnodeProcessConfigChangeReq(pVnode, ver, pReq, len, pRsp);
×
800
      break;
×
801
#ifdef TD_ENTERPRISE
802
    case TDMT_VND_KILL_COMPACT:
2✔
803
      vnodeProcessKillCompactReq(pVnode, ver, pReq, len, pRsp);
2✔
804
      break;
2✔
805
#endif
806
    /* ARB */
807
    case TDMT_VND_ARB_CHECK_SYNC:
8✔
808
      vnodeProcessArbCheckSyncReq(pVnode, pReq, len, pRsp);
8✔
809
      break;
8✔
810
    default:
×
811
      vError("vgId:%d, unprocessed msg, %d", TD_VID(pVnode), pMsg->msgType);
×
812
      return TSDB_CODE_INVALID_MSG;
×
813
  }
814

815
  vGDebug(&pMsg->info.traceId, "vgId:%d, index:%" PRId64 ", msg processed, code:0x%x", TD_VID(pVnode), ver, pRsp->code);
3,101,094!
816

817
  walApplyVer(pVnode->pWal, ver);
3,101,131✔
818

819
  code = tqPushMsg(pVnode->pTq, pMsg->msgType);
3,101,363✔
820
  if (code) {
3,101,465!
821
    vError("vgId:%d, failed to push msg to TQ since %s", TD_VID(pVnode), tstrerror(terrno));
×
822
    return code;
×
823
  }
824

825
  // commit if need
826
  if (needCommit) {
3,101,465✔
827
    vInfo("vgId:%d, commit at version %" PRId64, TD_VID(pVnode), ver);
24,764!
828
    code = vnodeAsyncCommit(pVnode);
24,811✔
829
    if (code) {
24,813!
830
      vError("vgId:%d, failed to vnode async commit since %s.", TD_VID(pVnode), tstrerror(terrno));
×
831
      goto _err;
×
832
    }
833

834
    // start a new one
835
    code = vnodeBegin(pVnode);
24,813✔
836
    if (code) {
24,796✔
837
      vError("vgId:%d, failed to begin vnode since %s.", TD_VID(pVnode), tstrerror(terrno));
215!
838
      goto _err;
×
839
    }
840
  }
841

842
_exit:
3,101,282✔
843
  return 0;
3,134,743✔
844

845
_err:
13✔
846
  vError("vgId:%d, process %s request failed since %s, ver:%" PRId64, TD_VID(pVnode), TMSG_INFO(pMsg->msgType),
13!
847
         tstrerror(terrno), ver);
848
  return code;
13✔
849
}
850

851
int32_t vnodePreprocessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg) {
1,676,409✔
852
  if (TDMT_SCH_QUERY != pMsg->msgType && TDMT_SCH_MERGE_QUERY != pMsg->msgType) {
1,676,409✔
853
    return 0;
84,849✔
854
  }
855

856
  return qWorkerPreprocessQueryMsg(pVnode->pQuery, pMsg, TDMT_SCH_QUERY == pMsg->msgType);
1,591,560✔
857
}
858

859
int32_t vnodeProcessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
1,679,105✔
860
  vTrace("message in vnode query queue is processing");
1,679,105✔
861
  if (pMsg->msgType == TDMT_VND_TMQ_CONSUME && !syncIsReadyForRead(pVnode->sync)) {
1,679,105✔
862
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
1,417✔
863
    return 0;
1,417✔
864
  }
865

866
  if (pMsg->msgType == TDMT_VND_TMQ_CONSUME && !pVnode->restored) {
1,677,681!
867
    vnodeRedirectRpcMsg(pVnode, pMsg, TSDB_CODE_SYN_RESTORING);
×
868
    return 0;
×
869
  }
870

871
  SReadHandle handle = {.vnode = pVnode, .pMsgCb = &pVnode->msgCb, .pWorkerCb = pInfo->workerCb};
1,677,681✔
872
  initStorageAPI(&handle.api);
1,677,681✔
873
  int32_t code = TSDB_CODE_SUCCESS;
1,677,606✔
874
  bool    redirected = false;
1,677,606✔
875

876
  switch (pMsg->msgType) {
1,677,606✔
877
    case TDMT_SCH_QUERY:
1,423,052✔
878
      if (!syncIsReadyForRead(pVnode->sync)) {
1,423,052✔
879
        pMsg->code = (terrno) ? terrno : TSDB_CODE_SYN_NOT_LEADER;
3,534!
880
        redirected = true;
3,534✔
881
      }
882
      code = qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
1,422,693✔
883
      if (redirected) {
1,421,665✔
884
        vnodeRedirectRpcMsg(pVnode, pMsg, pMsg->code);
3,520✔
885
        return 0;
3,535✔
886
      }
887
      return code;
1,418,145✔
888
    case TDMT_SCH_MERGE_QUERY:
171,135✔
889
      return qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
171,135✔
890
    case TDMT_SCH_QUERY_CONTINUE:
13,776✔
891
      return qWorkerProcessCQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
13,776✔
892
    case TDMT_VND_TMQ_CONSUME:
53,978✔
893
      return tqProcessPollReq(pVnode->pTq, pMsg);
53,978✔
894
    case TDMT_VND_TMQ_CONSUME_PUSH:
15,662✔
895
      return tqProcessPollPush(pVnode->pTq);
15,662✔
896
    default:
3✔
897
      vError("unknown msg type:%d in query queue", pMsg->msgType);
3!
898
      return TSDB_CODE_APP_ERROR;
×
899
  }
900
}
901

902
int32_t vnodeProcessFetchMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
4,350,302✔
903
  vTrace("vgId:%d, msg:%p in fetch queue is processing", pVnode->config.vgId, pMsg);
4,350,302✔
904
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
4,350,304!
905
       pMsg->msgType == TDMT_VND_BATCH_META || pMsg->msgType == TDMT_VND_TABLE_NAME ||
2,920,923!
906
       pMsg->msgType == TDMT_VND_VSUBTABLES_META || pMsg->msgType == TDMT_VND_VSTB_REF_DBS) &&
2,829,842✔
907
      !syncIsReadyForRead(pVnode->sync)) {
1,522,604✔
908
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
1,152✔
909
    return 0;
1,152✔
910
  }
911

912
  switch (pMsg->msgType) {
4,348,977!
913
    case TDMT_SCH_FETCH:
1,663,621✔
914
    case TDMT_SCH_MERGE_FETCH:
915
      return qWorkerProcessFetchMsg(pVnode, pVnode->pQuery, pMsg, 0);
1,663,621✔
916
    case TDMT_SCH_FETCH_RSP:
×
917
      return qWorkerProcessRspMsg(pVnode, pVnode->pQuery, pMsg, 0);
×
918
    // case TDMT_SCH_CANCEL_TASK:
919
    //   return qWorkerProcessCancelMsg(pVnode, pVnode->pQuery, pMsg, 0);
920
    case TDMT_SCH_DROP_TASK:
1,592,481✔
921
      return qWorkerProcessDropMsg(pVnode, pVnode->pQuery, pMsg, 0);
1,592,481✔
922
    case TDMT_SCH_TASK_NOTIFY:
39✔
923
      return qWorkerProcessNotifyMsg(pVnode, pVnode->pQuery, pMsg, 0);
39✔
924
    case TDMT_SCH_QUERY_HEARTBEAT:
1,000,927✔
925
      return qWorkerProcessHbMsg(pVnode, pVnode->pQuery, pMsg, 0);
1,000,927✔
926
    case TDMT_VND_TABLE_META:
1,722✔
927
    case TDMT_VND_TABLE_NAME:
928
      return vnodeGetTableMeta(pVnode, pMsg, true);
1,722✔
929
    case TDMT_VND_TABLE_CFG:
×
930
      return vnodeGetTableCfg(pVnode, pMsg, true);
×
931
    case TDMT_VND_BATCH_META:
90,064✔
932
      return vnodeGetBatchMeta(pVnode, pMsg);
90,064✔
933
    case TDMT_VND_VSUBTABLES_META:
×
934
      return vnodeGetVSubtablesMeta(pVnode, pMsg);
×
935
    case TDMT_VND_VSTB_REF_DBS:
×
936
      return vnodeGetVStbRefDbs(pVnode, pMsg);
×
937
#ifdef TD_ENTERPRISE
938
    case TDMT_VND_QUERY_COMPACT_PROGRESS:
86✔
939
      return vnodeQueryCompactProgress(pVnode, pMsg);
86✔
940
#endif
941
      //    case TDMT_VND_TMQ_CONSUME:
942
      //      return tqProcessPollReq(pVnode->pTq, pMsg);
943
#ifdef USE_TQ
944
    case TDMT_VND_TMQ_VG_WALINFO:
11✔
945
      return tqProcessVgWalInfoReq(pVnode->pTq, pMsg);
11✔
946
    case TDMT_VND_TMQ_VG_COMMITTEDINFO:
2✔
947
      return tqProcessVgCommittedInfoReq(pVnode->pTq, pMsg);
2✔
948
    case TDMT_VND_TMQ_SEEK:
24✔
949
      return tqProcessSeekReq(pVnode->pTq, pMsg);
24✔
950
#endif
951
    default:
×
952
      vError("unknown msg type:%d in fetch queue", pMsg->msgType);
×
953
      return TSDB_CODE_APP_ERROR;
×
954
  }
955
}
956
#ifdef USE_STREAM
957
int32_t vnodeProcessStreamMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
103,067✔
958
  vTrace("vgId:%d, msg:%p in stream queue is processing", pVnode->config.vgId, pMsg);
103,067✔
959

960
  // todo: NOTE: some command needs to run on follower, such as, stop_all_tasks
961
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
103,067!
962
       pMsg->msgType == TDMT_VND_BATCH_META) &&
103,104!
963
      !syncIsReadyForRead(pVnode->sync)) {
×
964
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
×
965
    return 0;
×
966
  }
967

968
  switch (pMsg->msgType) {
103,104!
969
    case TDMT_STREAM_TASK_RUN:
102,286✔
970
      return tqProcessTaskRunReq(pVnode->pTq, pMsg);
102,286✔
971
    case TDMT_STREAM_RETRIEVE:
412✔
972
      return tqProcessTaskRetrieveReq(pVnode->pTq, pMsg);
412✔
973
    case TDMT_STREAM_RETRIEVE_RSP:
406✔
974
      return tqProcessTaskRetrieveRsp(pVnode->pTq, pMsg);
406✔
975
    case TDMT_VND_GET_STREAM_PROGRESS:
×
976
      return tqStreamProgressRetrieveReq(pVnode->pTq, pMsg);
×
977
    default:
×
978
      vError("unknown msg type:%d in stream queue", pMsg->msgType);
×
979
      return TSDB_CODE_APP_ERROR;
×
980
  }
981
}
982

983
int32_t vnodeProcessStreamCtrlMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
59,201✔
984
  vTrace("vgId:%d, msg:%p in stream ctrl queue is processing", pVnode->config.vgId, pMsg);
59,201✔
985
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
59,202!
986
       pMsg->msgType == TDMT_VND_BATCH_META) &&
59,208✔
987
      !syncIsReadyForRead(pVnode->sync)) {
×
988
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
×
989
    return 0;
×
990
  }
991

992
  switch (pMsg->msgType) {
59,202!
993
    case TDMT_MND_STREAM_HEARTBEAT_RSP:
5,230✔
994
      return tqProcessStreamHbRsp(pVnode->pTq, pMsg);
5,230✔
995
    case TDMT_STREAM_TASK_DISPATCH:
12,051✔
996
      return tqProcessTaskDispatchReq(pVnode->pTq, pMsg);
12,051✔
997
    case TDMT_STREAM_TASK_DISPATCH_RSP:
12,373✔
998
      return tqProcessTaskDispatchRsp(pVnode->pTq, pMsg);
12,373✔
999
    case TDMT_VND_STREAM_TASK_CHECK:
11,382✔
1000
      return tqProcessTaskCheckReq(pVnode->pTq, pMsg);
11,382✔
1001
    case TDMT_VND_STREAM_TASK_CHECK_RSP:
9,735✔
1002
      return tqProcessTaskCheckRsp(pVnode->pTq, pMsg);
9,735✔
1003
    case TDMT_STREAM_TASK_CHECKPOINT_READY:
2,494✔
1004
      return tqProcessTaskCheckpointReadyMsg(pVnode->pTq, pMsg);
2,494✔
1005
    case TDMT_STREAM_TASK_CHECKPOINT_READY_RSP:
2,494✔
1006
      return tqProcessTaskCheckpointReadyRsp(pVnode->pTq, pMsg);
2,494✔
1007
    case TDMT_STREAM_RETRIEVE_TRIGGER:
×
1008
      return tqProcessTaskRetrieveTriggerReq(pVnode->pTq, pMsg);
×
1009
    case TDMT_STREAM_RETRIEVE_TRIGGER_RSP:
×
1010
      return tqProcessTaskRetrieveTriggerRsp(pVnode->pTq, pMsg);
×
1011
    case TDMT_MND_STREAM_REQ_CHKPT_RSP:
1,717✔
1012
      return tqProcessStreamReqCheckpointRsp(pVnode->pTq, pMsg);
1,717✔
1013
    case TDMT_MND_STREAM_CHKPT_REPORT_RSP:
1,726✔
1014
      return tqProcessTaskChkptReportRsp(pVnode->pTq, pMsg);
1,726✔
1015
    default:
×
1016
      vError("unknown msg type:%d in stream ctrl queue", pMsg->msgType);
×
1017
      return TSDB_CODE_APP_ERROR;
×
1018
  }
1019
}
1020

1021
int32_t vnodeProcessStreamLongExecMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
1,024✔
1022
  vTrace("vgId:%d, msg:%p in stream long exec queue is processing", pVnode->config.vgId, pMsg);
1,024✔
1023
  if (!syncIsReadyForRead(pVnode->sync)) {
1,024!
1024
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
×
1025
    return 0;
×
1026
  }
1027

1028
  switch (pMsg->msgType) {
1,024!
1029
    case TDMT_VND_STREAM_SCAN_HISTORY:
1,024✔
1030
      return tqProcessTaskScanHistory(pVnode->pTq, pMsg);
1,024✔
1031
    default:
×
1032
      vError("unknown msg type:%d in stream long exec queue", pMsg->msgType);
×
1033
      return TSDB_CODE_APP_ERROR;
×
1034
  }
1035
}
1036

1037
int32_t vnodeProcessStreamChkptMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
3,332✔
1038
  vTrace("vgId:%d, msg:%p in stream chkpt queue is processing", pVnode->config.vgId, pMsg);
3,332✔
1039
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
3,332!
1040
       pMsg->msgType == TDMT_VND_BATCH_META) &&
3,332!
1041
      !syncIsReadyForRead(pVnode->sync)) {
×
1042
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
×
1043
    return 0;
×
1044
  }
1045

1046
  switch (pMsg->msgType) {
3,332!
1047
    case TDMT_STREAM_CHKPT_EXEC:
3,332✔
1048
      return tqProcessTaskRunReq(pVnode->pTq, pMsg);
3,332✔
1049
    default:
×
1050
      vError("unknown msg type:%d in stream chkpt queue", pMsg->msgType);
×
1051
      return TSDB_CODE_APP_ERROR;
×
1052
  }
1053
}
1054
#endif
1055

1056
void smaHandleRes(void *pVnode, int64_t smaId, const SArray *data) {
×
1057
  int32_t code = tdProcessTSmaInsert(((SVnode *)pVnode)->pSma, smaId, (const char *)data);
×
1058
  if (code) {
×
1059
    vError("failed to process sma result since %s", tstrerror(code));
×
1060
  }
1061
}
×
1062

1063
void vnodeUpdateMetaRsp(SVnode *pVnode, STableMetaRsp *pMetaRsp) {
154,723✔
1064
  if (NULL == pMetaRsp) {
154,723!
1065
    return;
×
1066
  }
1067

1068
  tstrncpy(pMetaRsp->dbFName, pVnode->config.dbname, TSDB_DB_FNAME_LEN);
154,723✔
1069
  pMetaRsp->dbId = pVnode->config.dbId;
154,723✔
1070
  pMetaRsp->vgId = TD_VID(pVnode);
154,723✔
1071
  pMetaRsp->precision = pVnode->config.tsdbCfg.precision;
154,723✔
1072
}
1073

1074
extern int32_t vnodeAsyncRetention(SVnode *pVnode, int64_t now);
1075

1076
static int32_t vnodeProcessTrimReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
35✔
1077
  if (!pVnode->restored) {
35✔
1078
    vInfo("vgId:%d, ignore trim req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
6!
1079
    return 0;
6✔
1080
  }
1081

1082
  int32_t     code = 0;
29✔
1083
  SVTrimDbReq trimReq = {0};
29✔
1084

1085
  // decode
1086
  if (tDeserializeSVTrimDbReq(pReq, len, &trimReq) != 0) {
29!
1087
    code = TSDB_CODE_INVALID_MSG;
×
1088
    goto _exit;
×
1089
  }
1090

1091
  vInfo("vgId:%d, process trim vnode request, time:%d", pVnode->config.vgId, trimReq.timestamp);
29!
1092

1093
  code = vnodeAsyncRetention(pVnode, trimReq.timestamp);
29✔
1094

1095
_exit:
30✔
1096
  return code;
30✔
1097
}
1098

1099
extern int32_t vnodeAsyncS3Migrate(SVnode *pVnode, int64_t now);
1100

1101
static int32_t vnodeProcessS3MigrateReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1102
  int32_t          code = 0;
×
1103
  SVS3MigrateDbReq s3migrateReq = {0};
×
1104

1105
  // decode
1106
  if (tDeserializeSVS3MigrateDbReq(pReq, len, &s3migrateReq) != 0) {
×
1107
    code = TSDB_CODE_INVALID_MSG;
×
1108
    goto _exit;
×
1109
  }
1110

1111
  vInfo("vgId:%d, process s3migrate vnode request, time:%d", pVnode->config.vgId, s3migrateReq.timestamp);
×
1112

1113
  code = vnodeAsyncS3Migrate(pVnode, s3migrateReq.timestamp);
×
1114

1115
_exit:
×
1116
  return code;
×
1117
}
1118

1119
static int32_t vnodeProcessDropTtlTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1120
  int               ret = 0;
×
1121
  SVDropTtlTableReq ttlReq = {0};
×
1122
  if (tDeserializeSVDropTtlTableReq(pReq, len, &ttlReq) != 0) {
×
1123
    ret = TSDB_CODE_INVALID_MSG;
×
1124
    goto end;
×
1125
  }
1126

1127
  if (ttlReq.nUids != taosArrayGetSize(ttlReq.pTbUids)) {
×
1128
    ret = TSDB_CODE_INVALID_MSG;
×
1129
    goto end;
×
1130
  }
1131

1132
  if (ttlReq.nUids != 0) {
×
1133
    vInfo("vgId:%d, process drop ttl table request, time:%d, ntbUids:%d", pVnode->config.vgId,
×
1134
          ttlReq.timestampSec, ttlReq.nUids);
1135
  }
1136

1137
  if (ttlReq.nUids > 0) {
×
1138
    int32_t code = metaDropMultipleTables(pVnode->pMeta, ver, ttlReq.pTbUids);
×
1139
    if (code) return code;
×
1140

1141
    code = tqUpdateTbUidList(pVnode->pTq, ttlReq.pTbUids, false);
×
1142
    if (code) {
×
1143
      vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1144
    }
1145
  }
1146

1147
end:
×
1148
  taosArrayDestroy(ttlReq.pTbUids);
×
1149
  return ret;
×
1150
}
1151

1152
static int32_t vnodeProcessFetchTtlExpiredTbs(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
32✔
1153
  int32_t                 code = -1;
32✔
1154
  SMetaReader             mr = {0};
32✔
1155
  SVDropTtlTableReq       ttlReq = {0};
32✔
1156
  SVFetchTtlExpiredTbsRsp rsp = {0};
32✔
1157
  SEncoder                encoder = {0};
32✔
1158
  SArray                 *pNames = NULL;
32✔
1159
  pRsp->msgType = TDMT_VND_FETCH_TTL_EXPIRED_TBS_RSP;
32✔
1160
  pRsp->code = TSDB_CODE_SUCCESS;
32✔
1161
  pRsp->pCont = NULL;
32✔
1162
  pRsp->contLen = 0;
32✔
1163

1164
  if (tDeserializeSVDropTtlTableReq(pReq, len, &ttlReq) != 0) {
32!
1165
    terrno = TSDB_CODE_INVALID_MSG;
×
1166
    goto _end;
×
1167
  }
1168

1169
  if (!(ttlReq.nUids == taosArrayGetSize(ttlReq.pTbUids))) {
33!
1170
    terrno = TSDB_CODE_INVALID_MSG;
×
1171
    goto _end;
×
1172
  }
1173

1174
  tb_uid_t    suid;
1175
  char        ctbName[TSDB_TABLE_NAME_LEN];
1176
  SVDropTbReq expiredTb = {.igNotExists = true};
33✔
1177
  metaReaderDoInit(&mr, pVnode->pMeta, 0);
33✔
1178
  rsp.vgId = TD_VID(pVnode);
32✔
1179
  rsp.pExpiredTbs = taosArrayInit(ttlReq.nUids, sizeof(SVDropTbReq));
32✔
1180
  if (!rsp.pExpiredTbs) goto _end;
33!
1181

1182
  pNames = taosArrayInit(ttlReq.nUids, TSDB_TABLE_NAME_LEN);
33✔
1183
  if (!pNames) {
33!
1184
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1185
    goto _end;
×
1186
  }
1187
  char buf[TSDB_TABLE_NAME_LEN];
1188
  for (int32_t i = 0; i < ttlReq.nUids; ++i) {
166✔
1189
    tb_uid_t *uid = taosArrayGet(ttlReq.pTbUids, i);
132✔
1190
    expiredTb.suid = *uid;
132✔
1191
    terrno = metaReaderGetTableEntryByUid(&mr, *uid);
132✔
1192
    if (terrno < 0) goto _end;
132!
1193
    tstrncpy(buf, mr.me.name, TSDB_TABLE_NAME_LEN);
132✔
1194
    void *p = taosArrayPush(pNames, buf);
131✔
1195
    if (p == NULL) {
131!
1196
      goto _end;
×
1197
    }
1198

1199
    expiredTb.name = p;
131✔
1200
    if (mr.me.type == TSDB_CHILD_TABLE) {
131✔
1201
      expiredTb.suid = mr.me.ctbEntry.suid;
85✔
1202
    }
1203

1204
    if (taosArrayPush(rsp.pExpiredTbs, &expiredTb) == NULL) {
264!
1205
      goto _end;
×
1206
    }
1207
  }
1208

1209
  int32_t ret = 0;
34✔
1210
  tEncodeSize(tEncodeVFetchTtlExpiredTbsRsp, &rsp, pRsp->contLen, ret);
34!
1211
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
33✔
1212
  if (pRsp->pCont == NULL) {
33!
1213
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1214
    code = -1;
×
1215
    goto _end;
×
1216
  }
1217
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
33✔
1218
  terrno = tEncodeVFetchTtlExpiredTbsRsp(&encoder, &rsp);
33✔
1219
  tEncoderClear(&encoder);
33✔
1220

1221
  if (terrno == 0) code = 0;
33!
1222
_end:
×
1223
  metaReaderClear(&mr);
33✔
1224
  tFreeFetchTtlExpiredTbsRsp(&rsp);
33✔
1225
  taosArrayDestroy(ttlReq.pTbUids);
33✔
1226
  if (pNames) taosArrayDestroy(pNames);
33!
1227
  pRsp->code = terrno;
33✔
1228
  return code;
33✔
1229
}
1230

1231
static int32_t vnodeProcessCreateStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
26,388✔
1232
  int32_t        code = 0;
26,388✔
1233
  SVCreateStbReq req = {0};
26,388✔
1234
  SDecoder       coder;
1235

1236
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
26,388✔
1237
  pRsp->code = TSDB_CODE_SUCCESS;
26,388✔
1238
  pRsp->pCont = NULL;
26,388✔
1239
  pRsp->contLen = 0;
26,388✔
1240

1241
  // decode and process req
1242
  tDecoderInit(&coder, pReq, len);
26,388✔
1243

1244
  code = tDecodeSVCreateStbReq(&coder, &req);
26,528✔
1245
  if (code) {
26,408!
1246
    pRsp->code = code;
×
1247
    goto _err;
×
1248
  }
1249

1250
  code = metaCreateSuperTable(pVnode->pMeta, ver, &req);
26,408✔
1251
  if (code) {
26,560✔
1252
    pRsp->code = code;
4✔
1253
    goto _err;
4✔
1254
  }
1255

1256
  if ((code = tdProcessRSmaCreate(pVnode->pSma, &req)) < 0) {
26,556!
1257
    pRsp->code = code;
×
1258
    goto _err;
×
1259
  }
1260

1261
  tDecoderClear(&coder);
26,545✔
1262
  return 0;
26,518✔
1263

1264
_err:
4✔
1265
  tDecoderClear(&coder);
4✔
1266
  return code;
4✔
1267
}
1268

1269
static int32_t vnodeProcessCreateTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
77,479✔
1270
                                       SRpcMsg *pOriginRpc) {
1271
  SDecoder           decoder = {0};
77,479✔
1272
  SEncoder           encoder = {0};
77,479✔
1273
  int32_t            rcode = 0;
77,479✔
1274
  SVCreateTbBatchReq req = {0};
77,479✔
1275
  SVCreateTbReq     *pCreateReq;
1276
  SVCreateTbBatchRsp rsp = {0};
77,479✔
1277
  SVCreateTbRsp      cRsp = {0};
77,479✔
1278
  char               tbName[TSDB_TABLE_FNAME_LEN];
1279
  STbUidStore       *pStore = NULL;
77,479✔
1280
  SArray            *tbUids = NULL;
77,479✔
1281
  SArray            *tbNames = NULL;
77,479✔
1282
  pRsp->msgType = TDMT_VND_CREATE_TABLE_RSP;
77,479✔
1283
  pRsp->code = TSDB_CODE_SUCCESS;
77,479✔
1284
  pRsp->pCont = NULL;
77,479✔
1285
  pRsp->contLen = 0;
77,479✔
1286

1287
  // decode
1288
  tDecoderInit(&decoder, pReq, len);
77,479✔
1289
  if (tDecodeSVCreateTbBatchReq(&decoder, &req) < 0) {
77,470!
1290
    rcode = -1;
×
1291
    terrno = TSDB_CODE_INVALID_MSG;
×
1292
    goto _exit;
×
1293
  }
1294

1295
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(cRsp));
77,543✔
1296
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
77,542✔
1297
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
77,556✔
1298
  if (rsp.pArray == NULL || tbUids == NULL || tbNames == NULL) {
77,551!
1299
    rcode = -1;
×
1300
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1301
    goto _exit;
×
1302
  }
1303

1304
  // loop to create table
1305
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
184,543✔
1306
    pCreateReq = req.pReqs + iReq;
106,963✔
1307
    memset(&cRsp, 0, sizeof(cRsp));
106,963✔
1308

1309
    if (tsEnableAudit && tsEnableAuditCreateTable) {
106,963!
1310
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
106,974!
1311
      if (str == NULL) {
106,981!
1312
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1313
        rcode = -1;
×
1314
        goto _exit;
×
1315
      }
1316
      tstrncpy(str, pCreateReq->name, TSDB_TABLE_FNAME_LEN);
106,981✔
1317
      if (taosArrayPush(tbNames, &str) == NULL) {
106,979!
1318
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1319
        rcode = -1;
×
1320
        goto _exit;
×
1321
      }
1322
    }
1323

1324
    // validate hash
1325
    (void)tsnprintf(tbName, TSDB_TABLE_FNAME_LEN, "%s.%s", pVnode->config.dbname, pCreateReq->name);
106,968✔
1326
    if (vnodeValidateTableHash(pVnode, tbName) < 0) {
106,981!
1327
      cRsp.code = TSDB_CODE_VND_HASH_MISMATCH;
×
1328
      if (taosArrayPush(rsp.pArray, &cRsp) == NULL) {
×
1329
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1330
        rcode = -1;
×
1331
        goto _exit;
×
1332
      }
1333
      vError("vgId:%d create-table:%s failed due to hash value mismatch", TD_VID(pVnode), tbName);
×
1334
      continue;
×
1335
    }
1336

1337
    // do create table
1338
    if (metaCreateTable2(pVnode->pMeta, ver, pCreateReq, &cRsp.pMeta) < 0) {
106,966✔
1339
      if (pCreateReq->flags & TD_CREATE_IF_NOT_EXISTS && terrno == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
631!
1340
        cRsp.code = TSDB_CODE_SUCCESS;
4✔
1341
      } else {
1342
        cRsp.code = terrno;
627✔
1343
      }
1344
    } else {
1345
      cRsp.code = TSDB_CODE_SUCCESS;
106,363✔
1346
      if (tdFetchTbUidList(pVnode->pSma, &pStore, pCreateReq->ctb.suid, pCreateReq->uid) < 0) {
106,363!
1347
        vError("vgId:%d, failed to fetch tbUid list", TD_VID(pVnode));
×
1348
      }
1349
      if (taosArrayPush(tbUids, &pCreateReq->uid) == NULL) {
212,723!
1350
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1351
        rcode = -1;
×
1352
        goto _exit;
×
1353
      }
1354
      vnodeUpdateMetaRsp(pVnode, cRsp.pMeta);
106,360✔
1355
    }
1356

1357
    if (taosArrayPush(rsp.pArray, &cRsp) == NULL) {
213,983!
1358
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1359
      rcode = -1;
×
1360
      goto _exit;
×
1361
    }
1362
  }
1363

1364
  vTrace("vgId:%d, add %d new created tables into query table list", TD_VID(pVnode), (int32_t)taosArrayGetSize(tbUids));
77,580✔
1365
  if (tqUpdateTbUidList(pVnode->pTq, tbUids, true) < 0) {
77,580!
1366
    vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(terrno));
×
1367
  }
1368
  if (tdUpdateTbUidList(pVnode->pSma, pStore, true) < 0) {
77,559!
1369
    goto _exit;
×
1370
  }
1371
  pStore = tdUidStoreFree(pStore);
77,559✔
1372

1373
  // prepare rsp
1374
  int32_t ret = 0;
77,559✔
1375
  tEncodeSize(tEncodeSVCreateTbBatchRsp, &rsp, pRsp->contLen, ret);
77,559!
1376
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
77,554✔
1377
  if (pRsp->pCont == NULL) {
77,550!
1378
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1379
    rcode = -1;
×
1380
    goto _exit;
×
1381
  }
1382
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
77,550✔
1383
  if (tEncodeSVCreateTbBatchRsp(&encoder, &rsp) != 0) {
77,550!
1384
    vError("vgId:%d, failed to encode create table batch response", TD_VID(pVnode));
×
1385
  }
1386

1387
  if (tsEnableAudit && tsEnableAuditCreateTable) {
77,555✔
1388
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
77,552✔
1389

1390
    SName name = {0};
77,552✔
1391
    if (tNameFromString(&name, pVnode->config.dbname, T_NAME_ACCT | T_NAME_DB) < 0) {
77,552!
1392
      vError("vgId:%d, failed to get name from string", TD_VID(pVnode));
×
1393
    }
1394

1395
    SStringBuilder sb = {0};
77,548✔
1396
    for (int32_t i = 0; i < tbNames->size; i++) {
184,507✔
1397
      char **key = (char **)taosArrayGet(tbNames, i);
106,957✔
1398
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
106,957✔
1399
      if (i < tbNames->size - 1) {
106,960✔
1400
        taosStringBuilderAppendChar(&sb, ',');
29,409✔
1401
      }
1402
      // taosMemoryFreeClear(*key);
1403
    }
1404

1405
    size_t len = 0;
77,550✔
1406
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
77,550✔
1407

1408
    if (pOriginRpc->info.conn.user != NULL && strlen(pOriginRpc->info.conn.user) > 0) {
77,553!
1409
      auditAddRecord(pOriginRpc, clusterId, "createTable", name.dbname, "", keyJoined, len);
65,102✔
1410
    }
1411

1412
    taosStringBuilderDestroy(&sb);
77,553✔
1413
  }
1414

1415
_exit:
3✔
1416
  tDeleteSVCreateTbBatchReq(&req);
77,558✔
1417
  taosArrayDestroyEx(rsp.pArray, tFreeSVCreateTbRsp);
77,556✔
1418
  taosArrayDestroy(tbUids);
77,557✔
1419
  tDecoderClear(&decoder);
77,555✔
1420
  tEncoderClear(&encoder);
77,556✔
1421
  taosArrayDestroyP(tbNames, NULL);
77,558✔
1422
  return rcode;
77,559✔
1423
}
1424

1425
static int32_t vnodeProcessAlterStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
9,918✔
1426
  int32_t        code = 0;
9,918✔
1427
  SVCreateStbReq req = {0};
9,918✔
1428
  SDecoder       dc = {0};
9,918✔
1429

1430
  pRsp->msgType = TDMT_VND_ALTER_STB_RSP;
9,918✔
1431
  pRsp->code = TSDB_CODE_SUCCESS;
9,918✔
1432
  pRsp->pCont = NULL;
9,918✔
1433
  pRsp->contLen = 0;
9,918✔
1434

1435
  tDecoderInit(&dc, pReq, len);
9,918✔
1436

1437
  // decode req
1438
  code = tDecodeSVCreateStbReq(&dc, &req);
9,922✔
1439
  if (code) {
9,925!
1440
    tDecoderClear(&dc);
×
1441
    return code;
×
1442
  }
1443

1444
  code = metaAlterSuperTable(pVnode->pMeta, ver, &req);
9,925✔
1445
  if (code) {
9,933!
1446
    pRsp->code = code;
×
1447
    tDecoderClear(&dc);
×
1448
    return code;
×
1449
  }
1450

1451
  tDecoderClear(&dc);
9,933✔
1452

1453
  return 0;
9,933✔
1454
}
1455

1456
static int32_t vnodeProcessDropStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
2,060✔
1457
  SVDropStbReq req = {0};
2,060✔
1458
  int32_t      rcode = TSDB_CODE_SUCCESS;
2,060✔
1459
  SDecoder     decoder = {0};
2,060✔
1460
  SArray      *tbUidList = NULL;
2,060✔
1461

1462
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
2,060✔
1463
  pRsp->pCont = NULL;
2,060✔
1464
  pRsp->contLen = 0;
2,060✔
1465

1466
  // decode request
1467
  tDecoderInit(&decoder, pReq, len);
2,060✔
1468
  if (tDecodeSVDropStbReq(&decoder, &req) < 0) {
2,061!
1469
    rcode = TSDB_CODE_INVALID_MSG;
×
1470
    goto _exit;
×
1471
  }
1472

1473
  // process request
1474
  tbUidList = taosArrayInit(8, sizeof(int64_t));
2,060✔
1475
  if (tbUidList == NULL) goto _exit;
2,061!
1476
  if (metaDropSuperTable(pVnode->pMeta, ver, &req) < 0) {
2,061✔
1477
    rcode = terrno;
11✔
1478
    goto _exit;
11✔
1479
  }
1480

1481
  if (tqUpdateTbUidList(pVnode->pTq, tbUidList, false) < 0) {
2,054!
1482
    rcode = terrno;
×
1483
    goto _exit;
×
1484
  }
1485

1486
  if (tdProcessRSmaDrop(pVnode->pSma, &req) < 0) {
2,054!
1487
    rcode = terrno;
×
1488
    goto _exit;
×
1489
  }
1490

1491
  // return rsp
1492
_exit:
2,054✔
1493
  if (tbUidList) taosArrayDestroy(tbUidList);
2,065!
1494
  pRsp->code = rcode;
2,064✔
1495
  tDecoderClear(&decoder);
2,064✔
1496
  return 0;
2,064✔
1497
}
1498

1499
static int32_t vnodeProcessAlterTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
5,491✔
1500
  SVAlterTbReq  vAlterTbReq = {0};
5,491✔
1501
  SVAlterTbRsp  vAlterTbRsp = {0};
5,491✔
1502
  SDecoder      dc = {0};
5,491✔
1503
  int32_t       code = 0;
5,491✔
1504
  int32_t       lino = 0;
5,491✔
1505
  int32_t       ret;
1506
  SEncoder      ec = {0};
5,491✔
1507
  STableMetaRsp vMetaRsp = {0};
5,491✔
1508

1509
  pRsp->msgType = TDMT_VND_ALTER_TABLE_RSP;
5,491✔
1510
  pRsp->pCont = NULL;
5,491✔
1511
  pRsp->contLen = 0;
5,491✔
1512
  pRsp->code = TSDB_CODE_SUCCESS;
5,491✔
1513

1514
  tDecoderInit(&dc, pReq, len);
5,491✔
1515

1516
  // decode
1517
  if (tDecodeSVAlterTbReq(&dc, &vAlterTbReq) < 0) {
5,491!
1518
    vAlterTbRsp.code = TSDB_CODE_INVALID_MSG;
×
1519
    tDecoderClear(&dc);
×
1520
    goto _exit;
×
1521
  }
1522

1523
  // process
1524
  if (metaAlterTable(pVnode->pMeta, ver, &vAlterTbReq, &vMetaRsp) < 0) {
5,491✔
1525
    vAlterTbRsp.code = terrno;
919✔
1526
    tDecoderClear(&dc);
919✔
1527
    goto _exit;
919✔
1528
  }
1529
  tDecoderClear(&dc);
4,572✔
1530

1531
  if (NULL != vMetaRsp.pSchemas) {
4,572✔
1532
    vnodeUpdateMetaRsp(pVnode, &vMetaRsp);
1,281✔
1533
    vAlterTbRsp.pMeta = &vMetaRsp;
1,281✔
1534
  }
1535

1536
  if (vAlterTbReq.action == TSDB_ALTER_TABLE_UPDATE_TAG_VAL || vAlterTbReq.action == TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL) {
4,572✔
1537
    int64_t uid = metaGetTableEntryUidByName(pVnode->pMeta, vAlterTbReq.tbName);
3,171✔
1538
    if (uid == 0) {
3,171!
1539
      vError("vgId:%d, %s failed at %s:%d since table %s not found", TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
1540
             vAlterTbReq.tbName);
1541
      goto _exit;
×
1542
    }
1543

1544
    SArray* tbUids = taosArrayInit(4, sizeof(int64_t));
3,171✔
1545
    void* p = taosArrayPush(tbUids, &uid);
3,171✔
1546
    TSDB_CHECK_NULL(p, code, lino, _exit, terrno);
3,171!
1547

1548
    vDebug("vgId:%d, remove tags value altered table:%s from query table list", TD_VID(pVnode), vAlterTbReq.tbName);
3,171✔
1549
    if ((code = tqUpdateTbUidList(pVnode->pTq, tbUids, false)) < 0) {
3,171!
1550
      vError("vgId:%d, failed to remove tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1551
    }
1552

1553
    vDebug("vgId:%d, try to add table:%s in query table list", TD_VID(pVnode), vAlterTbReq.tbName);
3,171✔
1554
    if ((code = tqUpdateTbUidList(pVnode->pTq, tbUids, true)) < 0) {
3,171!
1555
      vError("vgId:%d, failed to add tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1556
    }
1557

1558
    taosArrayDestroy(tbUids);
3,171✔
1559
  }
1560

1561
_exit:
1,401✔
1562
  taosArrayDestroy(vAlterTbReq.pMultiTag);
5,491✔
1563
  tEncodeSize(tEncodeSVAlterTbRsp, &vAlterTbRsp, pRsp->contLen, ret);
5,491!
1564
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
5,491✔
1565
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
5,491✔
1566
  if (tEncodeSVAlterTbRsp(&ec, &vAlterTbRsp) != 0) {
5,491!
1567
    vError("vgId:%d, failed to encode alter table response", TD_VID(pVnode));
×
1568
  }
1569

1570
  tEncoderClear(&ec);
5,491✔
1571
  if (vMetaRsp.pSchemas) {
5,491✔
1572
    taosMemoryFree(vMetaRsp.pSchemas);
1,281!
1573
    taosMemoryFree(vMetaRsp.pSchemaExt);
1,281!
1574
  }
1575
  if (vMetaRsp.pColRefs) {
5,491✔
1576
    taosMemoryFree(vMetaRsp.pColRefs);
6!
1577
  }
1578
  return 0;
5,491✔
1579
}
1580

1581
static int32_t vnodeProcessDropTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
15,875✔
1582
                                     SRpcMsg *pOriginRpc) {
1583
  SVDropTbBatchReq req = {0};
15,875✔
1584
  SVDropTbBatchRsp rsp = {0};
15,875✔
1585
  SDecoder         decoder = {0};
15,875✔
1586
  SEncoder         encoder = {0};
15,875✔
1587
  int32_t          ret;
1588
  SArray          *tbUids = NULL;
15,875✔
1589
  STbUidStore     *pStore = NULL;
15,875✔
1590
  SArray          *tbNames = NULL;
15,875✔
1591

1592
  pRsp->msgType = TDMT_VND_DROP_TABLE_RSP;
15,875✔
1593
  pRsp->pCont = NULL;
15,875✔
1594
  pRsp->contLen = 0;
15,875✔
1595
  pRsp->code = TSDB_CODE_SUCCESS;
15,875✔
1596

1597
  // decode req
1598
  tDecoderInit(&decoder, pReq, len);
15,875✔
1599
  ret = tDecodeSVDropTbBatchReq(&decoder, &req);
15,875✔
1600
  if (ret < 0) {
15,875!
1601
    terrno = TSDB_CODE_INVALID_MSG;
×
1602
    pRsp->code = terrno;
×
1603
    goto _exit;
×
1604
  }
1605

1606
  // process req
1607
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
15,875✔
1608
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(SVDropTbRsp));
15,875✔
1609
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
15,875✔
1610
  if (tbUids == NULL || rsp.pArray == NULL || tbNames == NULL) goto _exit;
15,875!
1611

1612
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
31,770✔
1613
    SVDropTbReq *pDropTbReq = req.pReqs + iReq;
15,895✔
1614
    SVDropTbRsp  dropTbRsp = {0};
15,895✔
1615
    tb_uid_t     tbUid = 0;
15,895✔
1616

1617
    /* code */
1618
    ret = metaDropTable2(pVnode->pMeta, ver, pDropTbReq);
15,895✔
1619
    if (ret < 0) {
15,895!
1620
      if (pDropTbReq->igNotExists && terrno == TSDB_CODE_TDB_TABLE_NOT_EXIST) {
×
1621
        dropTbRsp.code = TSDB_CODE_SUCCESS;
×
1622
      } else {
1623
        dropTbRsp.code = terrno;
×
1624
      }
1625
    } else {
1626
      dropTbRsp.code = TSDB_CODE_SUCCESS;
15,895✔
1627
      if (tbUid > 0) {
15,895!
1628
        if (tdFetchTbUidList(pVnode->pSma, &pStore, pDropTbReq->suid, tbUid) < 0) {
×
1629
          vError("vgId:%d, failed to fetch tbUid list", TD_VID(pVnode));
×
1630
        }
1631
      }
1632
    }
1633

1634
    if (taosArrayPush(rsp.pArray, &dropTbRsp) == NULL) {
31,790!
1635
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1636
      pRsp->code = terrno;
×
1637
      goto _exit;
×
1638
    }
1639

1640
    if (tsEnableAuditCreateTable) {
15,895!
1641
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
15,895!
1642
      if (str == NULL) {
15,895!
1643
        pRsp->code = terrno;
×
1644
        goto _exit;
×
1645
      }
1646
      tstrncpy(str, pDropTbReq->name, TSDB_TABLE_FNAME_LEN);
15,895✔
1647
      if (taosArrayPush(tbNames, &str) == NULL) {
15,895!
1648
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1649
        pRsp->code = terrno;
×
1650
        goto _exit;
×
1651
      }
1652
    }
1653
  }
1654

1655
  if (tqUpdateTbUidList(pVnode->pTq, tbUids, false) < 0) {
15,875!
1656
    vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(terrno));
×
1657
  }
1658

1659
  if (tdUpdateTbUidList(pVnode->pSma, pStore, false) < 0) {
15,875!
1660
    goto _exit;
×
1661
  }
1662

1663
  if (tsEnableAuditCreateTable) {
15,875!
1664
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
15,875✔
1665

1666
    SName name = {0};
15,875✔
1667
    if (tNameFromString(&name, pVnode->config.dbname, T_NAME_ACCT | T_NAME_DB) != 0) {
15,875!
1668
      vError("vgId:%d, failed to get name from string", TD_VID(pVnode));
×
1669
    }
1670

1671
    SStringBuilder sb = {0};
15,875✔
1672
    for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
31,770✔
1673
      char **key = (char **)taosArrayGet(tbNames, iReq);
15,895✔
1674
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
15,895✔
1675
      if (iReq < req.nReqs - 1) {
15,895✔
1676
        taosStringBuilderAppendChar(&sb, ',');
20✔
1677
      }
1678
      taosMemoryFreeClear(*key);
15,895!
1679
    }
1680

1681
    size_t len = 0;
15,875✔
1682
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
15,875✔
1683

1684
    if (pOriginRpc->info.conn.user != NULL && strlen(pOriginRpc->info.conn.user) > 0) {
15,875!
1685
      auditAddRecord(pOriginRpc, clusterId, "dropTable", name.dbname, "", keyJoined, len);
15,833✔
1686
    }
1687

1688
    taosStringBuilderDestroy(&sb);
15,875✔
1689
  }
1690

1691
_exit:
×
1692
  taosArrayDestroy(tbUids);
15,875✔
1693
  pStore = tdUidStoreFree(pStore);
15,875✔
1694
  tDecoderClear(&decoder);
15,875✔
1695
  tEncodeSize(tEncodeSVDropTbBatchRsp, &rsp, pRsp->contLen, ret);
15,875!
1696
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
15,875✔
1697
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
15,875✔
1698
  if (tEncodeSVDropTbBatchRsp(&encoder, &rsp) != 0) {
15,875!
1699
    vError("vgId:%d, failed to encode drop table batch response", TD_VID(pVnode));
×
1700
  }
1701
  tEncoderClear(&encoder);
15,875✔
1702
  taosArrayDestroy(rsp.pArray);
15,875✔
1703
  taosArrayDestroy(tbNames);
15,875✔
1704
  return 0;
15,875✔
1705
}
1706

1707
#ifdef BUILD_NO_CALL
1708
static int32_t vnodeDebugPrintSingleSubmitMsg(SMeta *pMeta, SSubmitBlk *pBlock, SSubmitMsgIter *msgIter,
1709
                                              const char *tags) {
1710
  SSubmitBlkIter blkIter = {0};
1711
  STSchema      *pSchema = NULL;
1712
  tb_uid_t       suid = 0;
1713
  STSRow        *row = NULL;
1714
  int32_t        rv = -1;
1715

1716
  tInitSubmitBlkIter(msgIter, pBlock, &blkIter);
1717
  if (blkIter.row == NULL) return 0;
1718

1719
  int32_t code = metaGetTbTSchemaNotNull(pMeta, msgIter->suid, TD_ROW_SVER(blkIter.row), 1,
1720
                                         &pSchema);  // TODO: use the real schema
1721
  if (TSDB_CODE_SUCCESS != code) {
1722
    printf("%s:%d no valid schema\n", tags, __LINE__);
1723
    return code;
1724
  }
1725

1726
  suid = msgIter->suid;
1727
  rv = TD_ROW_SVER(blkIter.row);
1728

1729
  char __tags[128] = {0};
1730
  snprintf(__tags, 128, "%s: uid %" PRIi64 " ", tags, msgIter->uid);
1731
  while ((row = tGetSubmitBlkNext(&blkIter))) {
1732
    tdSRowPrint(row, pSchema, __tags);
1733
  }
1734

1735
  taosMemoryFreeClear(pSchema);
1736

1737
  return TSDB_CODE_SUCCESS;
1738
}
1739
#endif
1740
typedef struct SSubmitReqConvertCxt {
1741
  SSubmitMsgIter msgIter;
1742
  SSubmitBlk    *pBlock;
1743
  SSubmitBlkIter blkIter;
1744
  STSRow        *pRow;
1745
  STSRowIter     rowIter;
1746
  SSubmitTbData *pTbData;
1747
  STSchema      *pTbSchema;
1748
  SArray        *pColValues;
1749
} SSubmitReqConvertCxt;
1750

1751
static int32_t vnodeResetTableCxt(SMeta *pMeta, SSubmitReqConvertCxt *pCxt) {
×
1752
  taosMemoryFreeClear(pCxt->pTbSchema);
×
1753
  int32_t code = metaGetTbTSchemaNotNull(pMeta, pCxt->msgIter.suid > 0 ? pCxt->msgIter.suid : pCxt->msgIter.uid,
×
1754
                                         pCxt->msgIter.sversion, 1, &pCxt->pTbSchema);
1755
  if (TSDB_CODE_SUCCESS != code) {
×
1756
    return code;
×
1757
  }
1758
  tdSTSRowIterInit(&pCxt->rowIter, pCxt->pTbSchema);
×
1759

1760
  tDestroySubmitTbData(pCxt->pTbData, TSDB_MSG_FLG_ENCODE);
×
1761
  if (NULL == pCxt->pTbData) {
×
1762
    pCxt->pTbData = taosMemoryCalloc(1, sizeof(SSubmitTbData));
×
1763
    if (NULL == pCxt->pTbData) {
×
1764
      return terrno;
×
1765
    }
1766
  }
1767
  pCxt->pTbData->flags = 0;
×
1768
  pCxt->pTbData->suid = pCxt->msgIter.suid;
×
1769
  pCxt->pTbData->uid = pCxt->msgIter.uid;
×
1770
  pCxt->pTbData->sver = pCxt->msgIter.sversion;
×
1771
  pCxt->pTbData->pCreateTbReq = NULL;
×
1772
  pCxt->pTbData->aRowP = taosArrayInit(128, POINTER_BYTES);
×
1773
  if (NULL == pCxt->pTbData->aRowP) {
×
1774
    return terrno;
×
1775
  }
1776

1777
  taosArrayDestroy(pCxt->pColValues);
×
1778
  pCxt->pColValues = taosArrayInit(pCxt->pTbSchema->numOfCols, sizeof(SColVal));
×
1779
  if (NULL == pCxt->pColValues) {
×
1780
    return terrno;
×
1781
  }
1782
  for (int32_t i = 0; i < pCxt->pTbSchema->numOfCols; ++i) {
×
1783
    SColVal val = COL_VAL_NONE(pCxt->pTbSchema->columns[i].colId, pCxt->pTbSchema->columns[i].type);
×
1784
    if (taosArrayPush(pCxt->pColValues, &val) == NULL) {
×
1785
      return terrno;
×
1786
    }
1787
  }
1788

1789
  return TSDB_CODE_SUCCESS;
×
1790
}
1791

1792
static void vnodeDestroySubmitReqConvertCxt(SSubmitReqConvertCxt *pCxt) {
×
1793
  taosMemoryFreeClear(pCxt->pTbSchema);
×
1794
  tDestroySubmitTbData(pCxt->pTbData, TSDB_MSG_FLG_ENCODE);
×
1795
  taosMemoryFreeClear(pCxt->pTbData);
×
1796
  taosArrayDestroy(pCxt->pColValues);
×
1797
}
×
1798

1799
static int32_t vnodeCellValConvertToColVal(STColumn *pCol, SCellVal *pCellVal, SColVal *pColVal) {
×
1800
  if (tdValTypeIsNone(pCellVal->valType)) {
×
1801
    pColVal->flag = CV_FLAG_NONE;
×
1802
    return TSDB_CODE_SUCCESS;
×
1803
  }
1804

1805
  if (tdValTypeIsNull(pCellVal->valType)) {
×
1806
    pColVal->flag = CV_FLAG_NULL;
×
1807
    return TSDB_CODE_SUCCESS;
×
1808
  }
1809

1810
  if (IS_VAR_DATA_TYPE(pCol->type)) {
×
1811
    pColVal->value.nData = varDataLen(pCellVal->val);
×
1812
    pColVal->value.pData = (uint8_t *)varDataVal(pCellVal->val);
×
1813
  } else if (TSDB_DATA_TYPE_FLOAT == pCol->type) {
×
1814
    float f = GET_FLOAT_VAL(pCellVal->val);
×
1815
    valueSetDatum(&pColVal->value, pCol->type, &f, sizeof(f));
×
1816
  } else if (TSDB_DATA_TYPE_DOUBLE == pCol->type) {
×
1817
    taosSetPInt64Aligned(&pColVal->value.val, (int64_t *)pCellVal->val);
×
1818
  } else {
1819
    valueSetDatum(&pColVal->value, pCol->type, pCellVal->val, tDataTypes[pCol->type].bytes);
×
1820
  }
1821

1822
  pColVal->flag = CV_FLAG_VALUE;
×
1823
  return TSDB_CODE_SUCCESS;
×
1824
}
1825

1826
static int32_t vnodeTSRowConvertToColValArray(SSubmitReqConvertCxt *pCxt) {
×
1827
  int32_t code = TSDB_CODE_SUCCESS;
×
1828
  tdSTSRowIterReset(&pCxt->rowIter, pCxt->pRow);
×
1829
  for (int32_t i = 0; TSDB_CODE_SUCCESS == code && i < pCxt->pTbSchema->numOfCols; ++i) {
×
1830
    STColumn *pCol = pCxt->pTbSchema->columns + i;
×
1831
    SCellVal  cellVal = {0};
×
1832
    if (!tdSTSRowIterFetch(&pCxt->rowIter, pCol->colId, pCol->type, &cellVal)) {
×
1833
      break;
×
1834
    }
1835
    code = vnodeCellValConvertToColVal(pCol, &cellVal, (SColVal *)taosArrayGet(pCxt->pColValues, i));
×
1836
  }
1837
  return code;
×
1838
}
1839

1840
static int32_t vnodeDecodeCreateTbReq(SSubmitReqConvertCxt *pCxt) {
×
1841
  if (pCxt->msgIter.schemaLen <= 0) {
×
1842
    return TSDB_CODE_SUCCESS;
×
1843
  }
1844

1845
  pCxt->pTbData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
×
1846
  if (NULL == pCxt->pTbData->pCreateTbReq) {
×
1847
    return terrno;
×
1848
  }
1849

1850
  SDecoder decoder = {0};
×
1851
  tDecoderInit(&decoder, (uint8_t *)pCxt->pBlock->data, pCxt->msgIter.schemaLen);
×
1852
  int32_t code = tDecodeSVCreateTbReq(&decoder, pCxt->pTbData->pCreateTbReq);
×
1853
  tDecoderClear(&decoder);
×
1854

1855
  return code;
×
1856
}
1857

1858
static int32_t vnodeSubmitReqConvertToSubmitReq2(SVnode *pVnode, SSubmitReq *pReq, SSubmitReq2 *pReq2) {
×
1859
  pReq2->aSubmitTbData = taosArrayInit(128, sizeof(SSubmitTbData));
×
1860
  if (NULL == pReq2->aSubmitTbData) {
×
1861
    return terrno;
×
1862
  }
1863

1864
  SSubmitReqConvertCxt cxt = {0};
×
1865

1866
  int32_t code = tInitSubmitMsgIter(pReq, &cxt.msgIter);
×
1867
  while (TSDB_CODE_SUCCESS == code) {
×
1868
    code = tGetSubmitMsgNext(&cxt.msgIter, &cxt.pBlock);
×
1869
    if (TSDB_CODE_SUCCESS == code) {
×
1870
      if (NULL == cxt.pBlock) {
×
1871
        break;
×
1872
      }
1873
      code = vnodeResetTableCxt(pVnode->pMeta, &cxt);
×
1874
    }
1875
    if (TSDB_CODE_SUCCESS == code) {
×
1876
      code = tInitSubmitBlkIter(&cxt.msgIter, cxt.pBlock, &cxt.blkIter);
×
1877
    }
1878
    if (TSDB_CODE_SUCCESS == code) {
×
1879
      code = vnodeDecodeCreateTbReq(&cxt);
×
1880
    }
1881
    while (TSDB_CODE_SUCCESS == code && (cxt.pRow = tGetSubmitBlkNext(&cxt.blkIter)) != NULL) {
×
1882
      code = vnodeTSRowConvertToColValArray(&cxt);
×
1883
      if (TSDB_CODE_SUCCESS == code) {
×
1884
        SRow **pNewRow = taosArrayReserve(cxt.pTbData->aRowP, 1);
×
1885
        code = tRowBuild(cxt.pColValues, cxt.pTbSchema, pNewRow);
×
1886
      }
1887
    }
1888
    if (TSDB_CODE_SUCCESS == code) {
×
1889
      code = (NULL == taosArrayPush(pReq2->aSubmitTbData, cxt.pTbData) ? terrno : TSDB_CODE_SUCCESS);
×
1890
    }
1891
    if (TSDB_CODE_SUCCESS == code) {
×
1892
      taosMemoryFreeClear(cxt.pTbData);
×
1893
    }
1894
  }
1895

1896
  vnodeDestroySubmitReqConvertCxt(&cxt);
×
1897
  return code;
×
1898
}
1899

1900
static int32_t vnodeRebuildSubmitReqMsg(SSubmitReq2 *pSubmitReq, void **ppMsg) {
×
1901
  int32_t  code = TSDB_CODE_SUCCESS;
×
1902
  char    *pMsg = NULL;
×
1903
  uint32_t msglen = 0;
×
1904
  tEncodeSize(tEncodeSubmitReq, pSubmitReq, msglen, code);
×
1905
  if (TSDB_CODE_SUCCESS == code) {
×
1906
    pMsg = taosMemoryMalloc(msglen);
×
1907
    if (NULL == pMsg) {
×
1908
      code = terrno;
×
1909
    }
1910
  }
1911
  if (TSDB_CODE_SUCCESS == code) {
×
1912
    SEncoder encoder;
1913
    tEncoderInit(&encoder, (uint8_t *)pMsg, msglen);
×
1914
    code = tEncodeSubmitReq(&encoder, pSubmitReq);
×
1915
    tEncoderClear(&encoder);
×
1916
  }
1917
  if (TSDB_CODE_SUCCESS == code) {
×
1918
    *ppMsg = pMsg;
×
1919
  }
1920
  return code;
×
1921
}
1922

1923
static int32_t vnodeProcessSubmitReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
2,887,411✔
1924
                                     SRpcMsg *pOriginalMsg) {
1925
  int32_t code = 0;
2,887,411✔
1926
  terrno = 0;
2,887,411✔
1927

1928
  SSubmitReq2 *pSubmitReq = &(SSubmitReq2){0};
2,887,900✔
1929
  SSubmitRsp2 *pSubmitRsp = &(SSubmitRsp2){0};
2,887,900✔
1930
  SArray      *newTbUids = NULL;
2,887,900✔
1931
  int32_t      ret;
1932
  SEncoder     ec = {0};
2,887,900✔
1933

1934
  pRsp->code = TSDB_CODE_SUCCESS;
2,887,900✔
1935

1936
  void           *pAllocMsg = NULL;
2,887,900✔
1937
  SSubmitReq2Msg *pMsg = (SSubmitReq2Msg *)pReq;
2,887,900✔
1938
  if (0 == pMsg->version) {
2,887,900!
1939
    code = vnodeSubmitReqConvertToSubmitReq2(pVnode, (SSubmitReq *)pMsg, pSubmitReq);
×
1940
    if (TSDB_CODE_SUCCESS == code) {
×
1941
      code = vnodeRebuildSubmitReqMsg(pSubmitReq, &pReq);
×
1942
    }
1943
    if (TSDB_CODE_SUCCESS == code) {
×
1944
      pAllocMsg = pReq;
×
1945
    }
1946
    if (TSDB_CODE_SUCCESS != code) {
×
1947
      goto _exit;
×
1948
    }
1949
  } else {
1950
    // decode
1951
    pReq = POINTER_SHIFT(pReq, sizeof(SSubmitReq2Msg));
2,887,900✔
1952
    len -= sizeof(SSubmitReq2Msg);
2,887,900✔
1953
    SDecoder dc = {0};
2,887,900✔
1954
    tDecoderInit(&dc, pReq, len);
2,887,900✔
1955
    if (tDecodeSubmitReq(&dc, pSubmitReq, NULL) < 0) {
2,887,638!
1956
      code = TSDB_CODE_INVALID_MSG;
×
1957
      goto _exit;
×
1958
    }
1959
    tDecoderClear(&dc);
2,887,783✔
1960
  }
1961

1962
  // scan
1963
  TSKEY now = taosGetTimestamp(pVnode->config.tsdbCfg.precision);
2,887,925✔
1964
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * pVnode->config.tsdbCfg.keep2;
2,887,678✔
1965
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
2,887,678✔
1966
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
6,473,116✔
1967
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
3,582,721✔
1968

1969
    if (pSubmitTbData->pCreateTbReq && pSubmitTbData->pCreateTbReq->uid == 0) {
3,582,824!
1970
      code = TSDB_CODE_INVALID_MSG;
×
1971
      goto _exit;
×
1972
    }
1973

1974
    if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
3,582,824✔
1975
      if (TARRAY_SIZE(pSubmitTbData->aCol) <= 0) {
13,749!
1976
        code = TSDB_CODE_INVALID_MSG;
×
1977
        goto _exit;
×
1978
      }
1979

1980
      SColData *colDataArr = TARRAY_DATA(pSubmitTbData->aCol);
13,749✔
1981
      SRowKey   lastKey;
1982
      tColDataArrGetRowKey(colDataArr, TARRAY_SIZE(pSubmitTbData->aCol), 0, &lastKey);
13,749✔
1983
      for (int32_t iRow = 1; iRow < colDataArr[0].nVal; iRow++) {
21,605,830✔
1984
        SRowKey key;
1985
        tColDataArrGetRowKey(TARRAY_DATA(pSubmitTbData->aCol), TARRAY_SIZE(pSubmitTbData->aCol), iRow, &key);
21,592,017✔
1986
        if (tRowKeyCompare(&lastKey, &key) >= 0) {
21,590,518!
1987
          code = TSDB_CODE_INVALID_MSG;
×
1988
          vError("vgId:%d %s failed 1 since %s, version:%" PRId64, TD_VID(pVnode), __func__, tstrerror(terrno), ver);
×
1989
          goto _exit;
×
1990
        }
1991
      }
1992
    } else {
1993
      int32_t nRow = TARRAY_SIZE(pSubmitTbData->aRowP);
3,569,075✔
1994
      SRow  **aRow = (SRow **)TARRAY_DATA(pSubmitTbData->aRowP);
3,569,075✔
1995
      SRowKey lastRowKey;
1996
      for (int32_t iRow = 0; iRow < nRow; ++iRow) {
165,800,061✔
1997
#ifndef NO_UNALIGNED_ACCESS
1998
        if (aRow[iRow]->ts < minKey || aRow[iRow]->ts > maxKey) {
162,228,436✔
1999
#else
2000
        TSKEY ts = taosGetInt64Aligned(&(aRow[iRow]->ts));
2001
        if (ts < minKey || ts > maxKey) {
2002
#endif
2003
          code = TSDB_CODE_INVALID_MSG;
56,798✔
2004
          vError("vgId:%d %s failed 2 since %s, version:%" PRId64, TD_VID(pVnode), __func__, tstrerror(code), ver);
56,798!
2005
          goto _exit;
×
2006
        }
2007
        if (iRow == 0) {
162,171,638✔
2008
          tRowGetKey(aRow[iRow], &lastRowKey);
7,138,464✔
2009
        } else {
2010
          SRowKey rowKey;
2011
          tRowGetKey(aRow[iRow], &rowKey);
317,204,812✔
2012

2013
          if (tRowKeyCompare(&lastRowKey, &rowKey) >= 0) {
158,602,413!
2014
            code = TSDB_CODE_INVALID_MSG;
×
2015
            vError("vgId:%d %s failed 3 since %s, version:%" PRId64, TD_VID(pVnode), __func__, tstrerror(code), ver);
×
2016
            goto _exit;
×
2017
          }
2018
          lastRowKey = rowKey;
158,664,322✔
2019
        }
2020
      }
2021
    }
2022
  }
2023

2024
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
6,473,538✔
2025
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
3,582,816✔
2026

2027
    if (pSubmitTbData->pCreateTbReq) {
3,582,792✔
2028
      pSubmitTbData->uid = pSubmitTbData->pCreateTbReq->uid;
50,307✔
2029
    } else {
2030
      SMetaInfo info = {0};
3,532,485✔
2031

2032
      code = metaGetInfo(pVnode->pMeta, pSubmitTbData->uid, &info, NULL);
3,532,485✔
2033
      if (code) {
3,532,563!
2034
        code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
2035
        vWarn("vgId:%d, table uid:%" PRId64 " not exists", TD_VID(pVnode), pSubmitTbData->uid);
×
2036
        goto _exit;
1✔
2037
      }
2038

2039
      if (info.suid != pSubmitTbData->suid) {
3,532,668!
2040
        code = TSDB_CODE_INVALID_MSG;
×
2041
        goto _exit;
×
2042
      }
2043

2044
      if (info.suid) {
3,532,668✔
2045
        if (metaGetInfo(pVnode->pMeta, info.suid, &info, NULL) != 0) {
2,566,504!
2046
          vWarn("vgId:%d, table uid:%" PRId64 " not exists", TD_VID(pVnode), info.suid);
×
2047
        }
2048
      }
2049

2050
      if (pSubmitTbData->sver != info.skmVer) {
3,532,837✔
2051
        code = TSDB_CODE_TDB_INVALID_TABLE_SCHEMA_VER;
1✔
2052
        goto _exit;
1✔
2053
      }
2054
    }
2055

2056
    if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
3,583,143✔
2057
      int32_t   nColData = TARRAY_SIZE(pSubmitTbData->aCol);
13,749✔
2058
      SColData *aColData = (SColData *)TARRAY_DATA(pSubmitTbData->aCol);
13,749✔
2059

2060
      if (nColData <= 0) {
13,749!
2061
        code = TSDB_CODE_INVALID_MSG;
×
2062
        goto _exit;
×
2063
      }
2064

2065
      if (aColData[0].cid != PRIMARYKEY_TIMESTAMP_COL_ID || aColData[0].type != TSDB_DATA_TYPE_TIMESTAMP ||
13,749!
2066
          aColData[0].nVal <= 0) {
13,749!
2067
        code = TSDB_CODE_INVALID_MSG;
×
2068
        goto _exit;
×
2069
      }
2070

2071
      for (int32_t j = 1; j < nColData; j++) {
41,471✔
2072
        if (aColData[j].nVal != aColData[0].nVal) {
27,722!
2073
          code = TSDB_CODE_INVALID_MSG;
×
2074
          goto _exit;
×
2075
        }
2076
      }
2077
    }
2078
  }
2079

2080
  vGDebug(pOriginalMsg ? &pOriginalMsg->info.traceId : NULL, "vgId:%d, index:%" PRId64 ", submit block, rows:%d",
2,890,722!
2081
          TD_VID(pVnode), ver, (int32_t)taosArrayGetSize(pSubmitReq->aSubmitTbData));
2082

2083
  // loop to handle
2084
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
6,470,754✔
2085
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
3,582,722✔
2086

2087
    // create table
2088
    if (pSubmitTbData->pCreateTbReq) {
3,582,961✔
2089
      // alloc if need
2090
      if (pSubmitRsp->aCreateTbRsp == NULL &&
50,304✔
2091
          (pSubmitRsp->aCreateTbRsp = taosArrayInit(TARRAY_SIZE(pSubmitReq->aSubmitTbData), sizeof(SVCreateTbRsp))) ==
49,409!
2092
              NULL) {
2093
        code = terrno;
×
2094
        goto _exit;
8✔
2095
      }
2096

2097
      SVCreateTbRsp *pCreateTbRsp = taosArrayReserve(pSubmitRsp->aCreateTbRsp, 1);
50,305✔
2098

2099
      // create table
2100
      if (metaCreateTable2(pVnode->pMeta, ver, pSubmitTbData->pCreateTbReq, &pCreateTbRsp->pMeta) == 0) {
50,305✔
2101
        // create table success
2102

2103
        if (newTbUids == NULL &&
93,323!
2104
            (newTbUids = taosArrayInit(TARRAY_SIZE(pSubmitReq->aSubmitTbData), sizeof(int64_t))) == NULL) {
46,241✔
2105
          code = terrno;
×
2106
          goto _exit;
×
2107
        }
2108

2109
        if (taosArrayPush(newTbUids, &pSubmitTbData->uid) == NULL) {
94,164!
2110
          code = terrno;
×
2111
          goto _exit;
×
2112
        }
2113

2114
        if (pCreateTbRsp->pMeta) {
47,082!
2115
          vnodeUpdateMetaRsp(pVnode, pCreateTbRsp->pMeta);
47,082✔
2116
        }
2117
      } else {  // create table failed
2118
        if (terrno != TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
3,223✔
2119
          code = terrno;
8✔
2120
          vError("vgId:%d failed to create table:%s, code:%s", TD_VID(pVnode), pSubmitTbData->pCreateTbReq->name,
8!
2121
                 tstrerror(terrno));
2122
          goto _exit;
8✔
2123
        }
2124
        terrno = 0;
3,215✔
2125
        pSubmitTbData->uid = pSubmitTbData->pCreateTbReq->uid;  // update uid if table exist for using below
3,215✔
2126
      }
2127
    }
2128

2129
    // insert data
2130
    int32_t affectedRows;
2131
    code = tsdbInsertTableData(pVnode->pTsdb, ver, pSubmitTbData, &affectedRows);
3,582,954✔
2132
    if (code) goto _exit;
3,582,976!
2133

2134
    code = metaUpdateChangeTimeWithLock(pVnode->pMeta, pSubmitTbData->uid, pSubmitTbData->ctimeMs);
3,582,976✔
2135
    if (code) goto _exit;
3,582,986!
2136

2137
    pSubmitRsp->affectedRows += affectedRows;
3,582,986✔
2138
  }
2139

2140
  // update the affected table uid list
2141
  if (taosArrayGetSize(newTbUids) > 0) {
2,888,032✔
2142
    vDebug("vgId:%d, add %d table into query table list in handling submit", TD_VID(pVnode),
46,239✔
2143
           (int32_t)taosArrayGetSize(newTbUids));
2144
    if (tqUpdateTbUidList(pVnode->pTq, newTbUids, true) != 0) {
46,241!
2145
      vError("vgId:%d, failed to update tbUid list", TD_VID(pVnode));
×
2146
    }
2147
  }
2148

2149
_exit:
2,887,915✔
2150
  // message
2151
  pRsp->code = code;
2,887,924✔
2152
  tEncodeSize(tEncodeSSubmitRsp2, pSubmitRsp, pRsp->contLen, ret);
2,887,924!
2153
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
2,887,709✔
2154
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
2,887,791✔
2155
  if (tEncodeSSubmitRsp2(&ec, pSubmitRsp) < 0) {
2,887,907!
2156
    vError("vgId:%d, failed to encode submit response", TD_VID(pVnode));
×
2157
  }
2158
  tEncoderClear(&ec);
2,887,870✔
2159

2160
  // update statistics
2161
  (void)atomic_add_fetch_64(&pVnode->statis.nInsert, pSubmitRsp->affectedRows);
2,887,639✔
2162
  (void)atomic_add_fetch_64(&pVnode->statis.nInsertSuccess, pSubmitRsp->affectedRows);
2,887,956✔
2163
  (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsert, 1);
2,887,915✔
2164

2165
  if (tsEnableMonitor && tsMonitorFqdn[0] != 0 && tsMonitorPort != 0 && pSubmitRsp->affectedRows > 0 &&
2,887,954!
2166
      strlen(pOriginalMsg->info.conn.user) > 0 && tsInsertCounter != NULL) {
100!
2167
    const char *sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT_AFFECTED_ROWS,
100✔
2168
                                   pVnode->monitor.strClusterId,
100✔
2169
                                   pVnode->monitor.strDnodeId,
100✔
2170
                                   tsLocalEp,
2171
                                   pVnode->monitor.strVgId,
100✔
2172
                                   pOriginalMsg->info.conn.user,
100✔
2173
                                   "Success"};
2174
    int         tv = taos_counter_add(tsInsertCounter, pSubmitRsp->affectedRows, sample_labels);
100✔
2175
  }
2176

2177
  if (code == 0) {
2,887,954✔
2178
    (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
2,887,848✔
2179
    code = tdProcessRSmaSubmit(pVnode->pSma, ver, pSubmitReq, pReq, len);
2,887,960✔
2180
  }
2181
  /*
2182
  if (code == 0) {
2183
    atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
2184
    code = tdProcessRSmaSubmit(pVnode->pSma, ver, pSubmitReq, pReq, len);
2185

2186
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT, pVnode->monitor.strClusterId,
2187
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2188
                                          pOriginalMsg->info.conn.user, "Success"};
2189
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2190
  }
2191
  else{
2192
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT, pVnode->monitor.strClusterId,
2193
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2194
                                        pOriginalMsg->info.conn.user, "Failed"};
2195
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2196
  }
2197
  */
2198

2199
  // clear
2200
  taosArrayDestroy(newTbUids);
2,888,041✔
2201
  tDestroySubmitReq(pSubmitReq, 0 == pMsg->version ? TSDB_MSG_FLG_CMPT : TSDB_MSG_FLG_DECODE);
2,887,689!
2202
  tDestroySSubmitRsp2(pSubmitRsp, TSDB_MSG_FLG_ENCODE);
2,887,839✔
2203

2204
  if (code) terrno = code;
2,887,745✔
2205

2206
  taosMemoryFree(pAllocMsg);
2,887,745!
2207

2208
  return code;
2,887,581✔
2209
}
2210

2211
static int32_t vnodeProcessCreateTSmaReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
2212
#ifdef USE_TSMA
2213
  SVCreateTSmaReq req = {0};
×
2214
  SDecoder        coder = {0};
×
2215

2216
  if (pRsp) {
×
2217
    pRsp->msgType = TDMT_VND_CREATE_SMA_RSP;
×
2218
    pRsp->code = TSDB_CODE_SUCCESS;
×
2219
    pRsp->pCont = NULL;
×
2220
    pRsp->contLen = 0;
×
2221
  }
2222

2223
  // decode and process req
2224
  tDecoderInit(&coder, pReq, len);
×
2225

2226
  if (tDecodeSVCreateTSmaReq(&coder, &req) < 0) {
×
2227
    terrno = TSDB_CODE_MSG_DECODE_ERROR;
×
2228
    if (pRsp) pRsp->code = terrno;
×
2229
    goto _err;
×
2230
  }
2231

2232
  if (tdProcessTSmaCreate(pVnode->pSma, ver, (const char *)&req) < 0) {
×
2233
    if (pRsp) pRsp->code = terrno;
×
2234
    goto _err;
×
2235
  }
2236

2237
  tDecoderClear(&coder);
×
2238
  vDebug("vgId:%d, success to create tsma %s:%" PRIi64 " version %" PRIi64 " for table %" PRIi64, TD_VID(pVnode),
×
2239
         req.indexName, req.indexUid, ver, req.tableUid);
2240
  return 0;
×
2241

2242
_err:
×
2243
  tDecoderClear(&coder);
×
2244
  vError("vgId:%d, failed to create tsma %s:%" PRIi64 " version %" PRIi64 "for table %" PRIi64 " since %s",
×
2245
         TD_VID(pVnode), req.indexName, req.indexUid, ver, req.tableUid, terrstr());
2246
  return terrno;
×
2247
#else
2248
  return TSDB_CODE_INTERNAL_ERROR;
2249
#endif
2250
}
2251

2252
/**
2253
 * @brief specific for smaDstVnode
2254
 *
2255
 * @param pVnode
2256
 * @param pCont
2257
 * @param contLen
2258
 * @return int32_t
2259
 */
2260
int32_t vnodeProcessCreateTSma(SVnode *pVnode, void *pCont, uint32_t contLen) {
×
2261
  return vnodeProcessCreateTSmaReq(pVnode, 1, pCont, contLen, NULL);
×
2262
}
2263

2264
static int32_t vnodeConsolidateAlterHashRange(SVnode *pVnode, int64_t ver) {
78✔
2265
  int32_t code = TSDB_CODE_SUCCESS;
78✔
2266

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

2270
  // TODO: trim meta of tables from TDB per hash range [pVnode->config.hashBegin, pVnode->config.hashEnd]
2271
  code = metaTrimTables(pVnode->pMeta, ver);
78✔
2272

2273
  return code;
78✔
2274
}
2275

2276
static int32_t vnodeProcessAlterConfirmReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
11,644✔
2277
  vInfo("vgId:%d, vnode handle msgType:alter-confirm, alter confirm msg is processed", TD_VID(pVnode));
11,644!
2278
  int32_t code = TSDB_CODE_SUCCESS;
11,644✔
2279
  if (!pVnode->config.hashChange) {
11,644✔
2280
    goto _exit;
11,566✔
2281
  }
2282

2283
  code = vnodeConsolidateAlterHashRange(pVnode, ver);
78✔
2284
  if (code < 0) {
78!
2285
    vError("vgId:%d, failed to consolidate alter hashrange since %s. version:%" PRId64, TD_VID(pVnode), terrstr(), ver);
×
2286
    goto _exit;
×
2287
  }
2288
  pVnode->config.hashChange = false;
78✔
2289

2290
_exit:
11,644✔
2291
  pRsp->msgType = TDMT_VND_ALTER_CONFIRM_RSP;
11,644✔
2292
  pRsp->code = code;
11,644✔
2293
  pRsp->pCont = NULL;
11,644✔
2294
  pRsp->contLen = 0;
11,644✔
2295

2296
  return code;
11,644✔
2297
}
2298

2299
extern int32_t tsdbDisableAndCancelAllBgTask(STsdb *pTsdb);
2300
extern void    tsdbEnableBgTask(STsdb *pTsdb);
2301

2302
static int32_t vnodeProcessAlterConfigReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
615✔
2303
  bool walChanged = false;
615✔
2304
  bool tsdbChanged = false;
615✔
2305

2306
  SAlterVnodeConfigReq req = {0};
615✔
2307
  if (tDeserializeSAlterVnodeConfigReq(pReq, len, &req) != 0) {
615!
2308
    terrno = TSDB_CODE_INVALID_MSG;
×
2309
    return TSDB_CODE_INVALID_MSG;
×
2310
  }
2311

2312
  vInfo("vgId:%d, start to alter vnode config, page:%d pageSize:%d buffer:%d szPage:%d szBuf:%" PRIu64
616!
2313
        " cacheLast:%d cacheLastSize:%d days:%d keep0:%d keep1:%d keep2:%d keepTimeOffset:%d s3KeepLocal:%d "
2314
        "s3Compact:%d fsync:%d level:%d "
2315
        "walRetentionPeriod:%d walRetentionSize:%d",
2316
        TD_VID(pVnode), req.pages, req.pageSize, req.buffer, req.pageSize * 1024, (uint64_t)req.buffer * 1024 * 1024,
2317
        req.cacheLast, req.cacheLastSize, req.daysPerFile, req.daysToKeep0, req.daysToKeep1, req.daysToKeep2,
2318
        req.keepTimeOffset, req.s3KeepLocal, req.s3Compact, req.walFsyncPeriod, req.walLevel, req.walRetentionPeriod,
2319
        req.walRetentionSize);
2320

2321
  if (pVnode->config.cacheLastSize != req.cacheLastSize) {
616✔
2322
    pVnode->config.cacheLastSize = req.cacheLastSize;
94✔
2323
    tsdbCacheSetCapacity(pVnode, (size_t)pVnode->config.cacheLastSize * 1024 * 1024);
94✔
2324
  }
2325

2326
  if (pVnode->config.szBuf != req.buffer * 1024LL * 1024LL) {
616✔
2327
    vInfo("vgId:%d, vnode buffer is changed from %" PRId64 " to %" PRId64, TD_VID(pVnode), pVnode->config.szBuf,
10!
2328
          (uint64_t)(req.buffer * 1024LL * 1024LL));
2329
    pVnode->config.szBuf = req.buffer * 1024LL * 1024LL;
10✔
2330
  }
2331

2332
  if (pVnode->config.szCache != req.pages) {
616✔
2333
    if ((terrno = metaAlterCache(pVnode->pMeta, req.pages)) < 0) {
6!
2334
      vError("vgId:%d, failed to change vnode pages from %d to %d failed since %s", TD_VID(pVnode),
×
2335
             pVnode->config.szCache, req.pages, tstrerror(terrno));
2336
      return terrno;
×
2337
    } else {
2338
      vInfo("vgId:%d, vnode pages is changed from %d to %d", TD_VID(pVnode), pVnode->config.szCache, req.pages);
6!
2339
      pVnode->config.szCache = req.pages;
6✔
2340
    }
2341
  }
2342

2343
  if (pVnode->config.cacheLast != req.cacheLast) {
616✔
2344
    pVnode->config.cacheLast = req.cacheLast;
277✔
2345
  }
2346

2347
  if (pVnode->config.walCfg.fsyncPeriod != req.walFsyncPeriod) {
616✔
2348
    pVnode->config.walCfg.fsyncPeriod = req.walFsyncPeriod;
70✔
2349
    walChanged = true;
70✔
2350
  }
2351

2352
  if (pVnode->config.walCfg.level != req.walLevel) {
616✔
2353
    if (pVnode->config.walCfg.level == 0) {
51✔
2354
      pVnode->config.walCfg.clearFiles = 1;
3✔
2355
    }
2356
    pVnode->config.walCfg.level = req.walLevel;
51✔
2357
    walChanged = true;
51✔
2358
  }
2359

2360
  if (pVnode->config.walCfg.retentionPeriod != req.walRetentionPeriod) {
616✔
2361
    pVnode->config.walCfg.retentionPeriod = req.walRetentionPeriod;
132✔
2362
    walChanged = true;
132✔
2363
  }
2364

2365
  if (pVnode->config.walCfg.retentionSize != req.walRetentionSize) {
616!
2366
    pVnode->config.walCfg.retentionSize = req.walRetentionSize;
×
2367
    walChanged = true;
×
2368
  }
2369

2370
  if (pVnode->config.tsdbCfg.keep0 != req.daysToKeep0) {
616✔
2371
    pVnode->config.tsdbCfg.keep0 = req.daysToKeep0;
48✔
2372
    if (!VND_IS_RSMA(pVnode)) {
48!
2373
      tsdbChanged = true;
48✔
2374
    }
2375
  }
2376

2377
  if (pVnode->config.tsdbCfg.keep1 != req.daysToKeep1) {
616✔
2378
    pVnode->config.tsdbCfg.keep1 = req.daysToKeep1;
48✔
2379
    if (!VND_IS_RSMA(pVnode)) {
48!
2380
      tsdbChanged = true;
48✔
2381
    }
2382
  }
2383

2384
  if (pVnode->config.tsdbCfg.keep2 != req.daysToKeep2) {
616✔
2385
    pVnode->config.tsdbCfg.keep2 = req.daysToKeep2;
48✔
2386
    if (!VND_IS_RSMA(pVnode)) {
48!
2387
      tsdbChanged = true;
48✔
2388
    }
2389
  }
2390

2391
  if (pVnode->config.tsdbCfg.keepTimeOffset != req.keepTimeOffset) {
616✔
2392
    pVnode->config.tsdbCfg.keepTimeOffset = req.keepTimeOffset;
6✔
2393
    if (!VND_IS_RSMA(pVnode)) {
6!
2394
      tsdbChanged = true;
6✔
2395
    }
2396
  }
2397

2398
  if (req.sttTrigger != -1 && req.sttTrigger != pVnode->config.sttTrigger) {
616!
2399
    if (req.sttTrigger > 1 && pVnode->config.sttTrigger > 1) {
×
2400
      pVnode->config.sttTrigger = req.sttTrigger;
×
2401
    } else {
2402
      vnodeAWait(&pVnode->commitTask);
×
2403

2404
      int32_t ret = tsdbDisableAndCancelAllBgTask(pVnode->pTsdb);
×
2405
      if (ret != 0) {
×
2406
        vError("vgId:%d, failed to disable bg task since %s", TD_VID(pVnode), tstrerror(ERRNO));
×
2407
      }
2408

2409
      pVnode->config.sttTrigger = req.sttTrigger;
×
2410
      tsdbEnableBgTask(pVnode->pTsdb);
×
2411
    }
2412
  }
2413

2414
  if (req.minRows != -1 && req.minRows != pVnode->config.tsdbCfg.minRows) {
616!
2415
    pVnode->config.tsdbCfg.minRows = req.minRows;
×
2416
  }
2417

2418
  if (req.s3KeepLocal != -1 && req.s3KeepLocal != pVnode->config.s3KeepLocal) {
616!
2419
    pVnode->config.s3KeepLocal = req.s3KeepLocal;
×
2420
  }
2421
  if (req.s3Compact != -1 && req.s3Compact != pVnode->config.s3Compact) {
616!
2422
    pVnode->config.s3Compact = req.s3Compact;
×
2423
  }
2424

2425
  if (walChanged) {
616✔
2426
    if (walAlter(pVnode->pWal, &pVnode->config.walCfg) != 0) {
249!
2427
      vError("vgId:%d, failed to alter wal config since %s", TD_VID(pVnode), tstrerror(ERRNO));
×
2428
    }
2429
  }
2430

2431
  if (tsdbChanged) {
616✔
2432
    tsdbSetKeepCfg(pVnode->pTsdb, &pVnode->config.tsdbCfg);
56✔
2433
  }
2434

2435
  return 0;
616✔
2436
}
2437

2438
static int32_t vnodeProcessBatchDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
1,045✔
2439
  SBatchDeleteReq deleteReq;
2440
  SDecoder        decoder;
2441
  tDecoderInit(&decoder, pReq, len);
1,045✔
2442
  if (tDecodeSBatchDeleteReq(&decoder, &deleteReq) < 0) {
1,045!
2443
    tDecoderClear(&decoder);
×
2444
    return terrno = TSDB_CODE_INVALID_MSG;
×
2445
  }
2446

2447
  SMetaReader mr = {0};
1,045✔
2448
  metaReaderDoInit(&mr, pVnode->pMeta, META_READER_NOLOCK);
1,045✔
2449
  STsdb *pTsdb = pVnode->pTsdb;
1,045✔
2450

2451
  if (deleteReq.level) {
1,045✔
2452
    pTsdb = deleteReq.level == 1 ? VND_RSMA1(pVnode) : VND_RSMA2(pVnode);
16✔
2453
  }
2454

2455
  int32_t sz = taosArrayGetSize(deleteReq.deleteReqs);
1,045✔
2456
  for (int32_t i = 0; i < sz; i++) {
2,908✔
2457
    SSingleDeleteReq *pOneReq = taosArrayGet(deleteReq.deleteReqs, i);
1,863✔
2458
    char             *name = pOneReq->tbname;
1,863✔
2459
    if (metaGetTableEntryByName(&mr, name) < 0) {
1,863✔
2460
      vDebug("vgId:%d, stream delete msg, skip since no table: %s", pVnode->config.vgId, name);
673✔
2461
      continue;
673✔
2462
    }
2463

2464
    int64_t uid = mr.me.uid;
1,190✔
2465

2466
    int32_t code = tsdbDeleteTableData(pTsdb, ver, deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
1,190✔
2467
    if (code < 0) {
1,190!
2468
      terrno = code;
×
2469
      vError("vgId:%d, delete error since %s, suid:%" PRId64 ", uid:%" PRId64 ", start ts:%" PRId64 ", end ts:%" PRId64,
×
2470
             TD_VID(pVnode), terrstr(), deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
2471
    }
2472

2473
    if (deleteReq.level == 0) {
1,190✔
2474
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, deleteReq.ctimeMs);
574✔
2475
      if (code < 0) {
574!
2476
        terrno = code;
×
2477
        vError("vgId:%d, update change time error since %s, suid:%" PRId64 ", uid:%" PRId64 ", start ts:%" PRId64
×
2478
               ", end ts:%" PRId64,
2479
               TD_VID(pVnode), terrstr(), deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
2480
      }
2481
    }
2482
    tDecoderClear(&mr.coder);
1,190✔
2483
  }
2484
  metaReaderClear(&mr);
1,045✔
2485
  taosArrayDestroy(deleteReq.deleteReqs);
1,045✔
2486
  return 0;
1,045✔
2487
}
2488

2489
static int32_t vnodeProcessDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
8,452✔
2490
                                     SRpcMsg *pOriginalMsg) {
2491
  int32_t     code = 0;
8,452✔
2492
  SDecoder   *pCoder = &(SDecoder){0};
8,452✔
2493
  SDeleteRes *pRes = &(SDeleteRes){0};
8,452✔
2494

2495
  pRsp->msgType = TDMT_VND_DELETE_RSP;
8,452✔
2496
  pRsp->pCont = NULL;
8,452✔
2497
  pRsp->contLen = 0;
8,452✔
2498
  pRsp->code = TSDB_CODE_SUCCESS;
8,452✔
2499

2500
  pRes->uidList = taosArrayInit(0, sizeof(tb_uid_t));
8,452✔
2501
  if (pRes->uidList == NULL) {
8,452!
2502
    code = terrno;
×
2503
    goto _err;
×
2504
  }
2505

2506
  tDecoderInit(pCoder, pReq, len);
8,452✔
2507
  code = tDecodeDeleteRes(pCoder, pRes);
8,452✔
2508
  if (code) goto _err;
8,452!
2509

2510
  if (pRes->affectedRows > 0) {
8,452✔
2511
    for (int32_t iUid = 0; iUid < taosArrayGetSize(pRes->uidList); iUid++) {
14,661✔
2512
      uint64_t uid = *(uint64_t *)taosArrayGet(pRes->uidList, iUid);
7,852✔
2513
      code = tsdbDeleteTableData(pVnode->pTsdb, ver, pRes->suid, uid, pRes->skey, pRes->ekey);
7,852✔
2514
      if (code) goto _err;
7,852!
2515
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, pRes->ctimeMs);
7,852✔
2516
      if (code) goto _err;
7,852!
2517
    }
2518
  }
2519

2520
  code = tdProcessRSmaDelete(pVnode->pSma, ver, pRes, pReq, len);
8,452✔
2521

2522
  tDecoderClear(pCoder);
8,452✔
2523
  taosArrayDestroy(pRes->uidList);
8,452✔
2524

2525
  SVDeleteRsp rsp = {.affectedRows = pRes->affectedRows};
8,452✔
2526
  int32_t     ret = 0;
8,452✔
2527
  tEncodeSize(tEncodeSVDeleteRsp, &rsp, pRsp->contLen, ret);
8,452!
2528
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
8,452✔
2529
  SEncoder ec = {0};
8,452✔
2530
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
8,452✔
2531
  code = tEncodeSVDeleteRsp(&ec, &rsp);
8,452✔
2532
  if (code) goto _err;
8,452!
2533
  tEncoderClear(&ec);
8,452✔
2534
  return code;
8,452✔
2535

2536
_err:
×
2537
  /*
2538
  if(code == TSDB_CODE_SUCCESS){
2539
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_DELETE, pVnode->monitor.strClusterId,
2540
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2541
                                        pOriginalMsg->info.conn.user, "Success"};
2542
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2543
  }
2544
  else{
2545
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_DELETE, pVnode->monitor.strClusterId,
2546
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2547
                                        pOriginalMsg->info.conn.user, "Failed"};
2548
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2549
  }
2550
  */
2551

2552
  return code;
×
2553
}
2554
static int32_t vnodeProcessCreateIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
45✔
2555
  SVCreateStbReq req = {0};
45✔
2556
  SDecoder       dc = {0};
45✔
2557
  int32_t        code = 0;
45✔
2558

2559
  pRsp->msgType = TDMT_VND_CREATE_INDEX_RSP;
45✔
2560
  pRsp->code = TSDB_CODE_SUCCESS;
45✔
2561
  pRsp->pCont = NULL;
45✔
2562
  pRsp->contLen = 0;
45✔
2563

2564
  tDecoderInit(&dc, pReq, len);
45✔
2565
  // decode req
2566
  if (tDecodeSVCreateStbReq(&dc, &req) < 0) {
45!
2567
    tDecoderClear(&dc);
×
2568
    return terrno = TSDB_CODE_INVALID_MSG;
×
2569
  }
2570

2571
  code = metaAddIndexToSuperTable(pVnode->pMeta, ver, &req);
45✔
2572
  if (code) {
45!
2573
    pRsp->code = code;
×
2574
    goto _err;
×
2575
  }
2576
  tDecoderClear(&dc);
45✔
2577
  return 0;
45✔
2578

2579
_err:
×
2580
  tDecoderClear(&dc);
×
2581
  return code;
×
2582
}
2583
static int32_t vnodeProcessDropIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
36✔
2584
  SDropIndexReq req = {0};
36✔
2585
  int32_t       code = 0;
36✔
2586
  pRsp->msgType = TDMT_VND_DROP_INDEX_RSP;
36✔
2587
  pRsp->code = TSDB_CODE_SUCCESS;
36✔
2588
  pRsp->pCont = NULL;
36✔
2589
  pRsp->contLen = 0;
36✔
2590

2591
  if ((code = tDeserializeSDropIdxReq(pReq, len, &req))) {
36!
2592
    pRsp->code = code;
×
2593
    return code;
×
2594
  }
2595

2596
  code = metaDropIndexFromSuperTable(pVnode->pMeta, ver, &req);
36✔
2597
  if (code) {
36!
2598
    pRsp->code = code;
×
2599
    return code;
×
2600
  }
2601
  return TSDB_CODE_SUCCESS;
36✔
2602
}
2603

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

2606
static int32_t vnodeProcessCompactVnodeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
247✔
2607
  if (!pVnode->restored) {
247✔
2608
    vInfo("vgId:%d, ignore compact req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
174!
2609
    return 0;
174✔
2610
  }
2611
  return vnodeAsyncCompact(pVnode, ver, pReq, len, pRsp);
73✔
2612
}
2613

2614
static int32_t vnodeProcessConfigChangeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
2615
  if (syncCheckMember(pVnode->sync) != 0) {
×
2616
    vError("vgId:%d, failed to check member", TD_VID(pVnode));
×
2617
  }
2618

2619
  pRsp->msgType = TDMT_SYNC_CONFIG_CHANGE_RSP;
×
2620
  pRsp->code = TSDB_CODE_SUCCESS;
×
2621
  pRsp->pCont = NULL;
×
2622
  pRsp->contLen = 0;
×
2623

2624
  return 0;
×
2625
}
2626

2627
static int32_t vnodeCheckToken(SVnode *pVnode, char *member0Token, char *member1Token) {
32✔
2628
  SSyncState syncState = syncGetState(pVnode->sync);
32✔
2629
  if (syncState.state != TAOS_SYNC_STATE_LEADER) {
32✔
2630
    return terrno = TSDB_CODE_SYN_NOT_LEADER;
8✔
2631
  }
2632

2633
  char token[TSDB_ARB_TOKEN_SIZE] = {0};
24✔
2634
  if (vnodeGetArbToken(pVnode, token) != 0) {
24!
2635
    return terrno = TSDB_CODE_NOT_FOUND;
×
2636
  }
2637

2638
  if (strncmp(token, member0Token, TSDB_ARB_TOKEN_SIZE) != 0 &&
24✔
2639
      strncmp(token, member1Token, TSDB_ARB_TOKEN_SIZE) != 0) {
18✔
2640
    return terrno = TSDB_CODE_MND_ARB_TOKEN_MISMATCH;
16✔
2641
  }
2642

2643
  terrno = TSDB_CODE_SUCCESS;
8✔
2644
  return 0;
8✔
2645
}
2646

2647
static int32_t vnodeCheckSyncd(SVnode *pVnode, char *member0Token, char *member1Token) {
8✔
2648
  int32_t code = vnodeCheckToken(pVnode, member0Token, member1Token);
8✔
2649
  if (code != 0) {
8✔
2650
    return code;
4✔
2651
  }
2652

2653
  return syncCheckSynced(pVnode->sync);
4✔
2654
}
2655

2656
static int32_t vnodeProcessArbCheckSyncReq(SVnode *pVnode, void *pReq, int32_t len, SRpcMsg *pRsp) {
8✔
2657
  int32_t code = 0;
8✔
2658

2659
  SVArbCheckSyncReq syncReq = {0};
8✔
2660

2661
  code = tDeserializeSVArbCheckSyncReq(pReq, len, &syncReq);
8✔
2662
  if (code) {
8!
2663
    return terrno = code;
×
2664
  }
2665

2666
  pRsp->msgType = TDMT_VND_ARB_CHECK_SYNC_RSP;
8✔
2667
  pRsp->code = TSDB_CODE_SUCCESS;
8✔
2668
  pRsp->pCont = NULL;
8✔
2669
  pRsp->contLen = 0;
8✔
2670

2671
  SVArbCheckSyncRsp syncRsp = {0};
8✔
2672
  syncRsp.arbToken = syncReq.arbToken;
8✔
2673
  syncRsp.member0Token = syncReq.member0Token;
8✔
2674
  syncRsp.member1Token = syncReq.member1Token;
8✔
2675
  syncRsp.vgId = TD_VID(pVnode);
8✔
2676

2677
  if (vnodeCheckSyncd(pVnode, syncReq.member0Token, syncReq.member1Token) != 0) {
8✔
2678
    vError("vgId:%d, failed to check assigned log syncd", TD_VID(pVnode));
4!
2679
  }
2680
  syncRsp.errCode = terrno;
8✔
2681

2682
  if (vnodeUpdateArbTerm(pVnode, syncReq.arbTerm) != 0) {
8!
2683
    vError("vgId:%d, failed to update arb term", TD_VID(pVnode));
×
2684
    code = -1;
×
2685
    goto _OVER;
×
2686
  }
2687

2688
  int32_t contLen = tSerializeSVArbCheckSyncRsp(NULL, 0, &syncRsp);
8✔
2689
  if (contLen <= 0) {
8!
2690
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2691
    code = -1;
×
2692
    goto _OVER;
×
2693
  }
2694
  void *pHead = rpcMallocCont(contLen);
8✔
2695
  if (!pHead) {
8!
2696
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2697
    code = -1;
×
2698
    goto _OVER;
×
2699
  }
2700

2701
  if (tSerializeSVArbCheckSyncRsp(pHead, contLen, &syncRsp) <= 0) {
8!
2702
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2703
    rpcFreeCont(pHead);
×
2704
    code = -1;
×
2705
    goto _OVER;
×
2706
  }
2707

2708
  pRsp->pCont = pHead;
8✔
2709
  pRsp->contLen = contLen;
8✔
2710

2711
  terrno = TSDB_CODE_SUCCESS;
8✔
2712

2713
_OVER:
8✔
2714
  tFreeSVArbCheckSyncReq(&syncReq);
8✔
2715
  return code;
8✔
2716
}
2717

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