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

taosdata / TDengine / #4506

15 Jul 2025 12:33AM UTC coverage: 62.026% (-0.7%) from 62.706%
#4506

push

travis-ci

web-flow
docs: update stream docs (#31874)

155391 of 320094 branches covered (48.55%)

Branch coverage included in aggregate %.

240721 of 318525 relevant lines covered (75.57%)

6529048.03 hits per line

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

58.62
/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 vnodeProcessSsMigrateReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
45
static int32_t vnodeProcessFollowerSsMigrateReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
46
static int32_t vnodeProcessDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
47
                                     SRpcMsg *pOriginalMsg);
48
static int32_t vnodeProcessBatchDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
49
static int32_t vnodeProcessCreateIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
50
static int32_t vnodeProcessDropIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
51
static int32_t vnodeProcessCompactVnodeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
52
static int32_t vnodeProcessConfigChangeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
53
static int32_t vnodeProcessArbCheckSyncReq(SVnode *pVnode, void *pReq, int32_t len, SRpcMsg *pRsp);
54
static int32_t vnodeProcessDropTSmaCtbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
55
                                          SRpcMsg *pOriginRpc);
56

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

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

65
static int32_t vnodePreprocessCreateTableReq(SVnode *pVnode, SDecoder *pCoder, int64_t btime, int64_t *pUid) {
148,882✔
66
  int32_t code = 0;
148,882✔
67
  int32_t lino = 0;
148,882✔
68

69
  if (tStartDecode(pCoder) < 0) {
148,882!
70
    code = TSDB_CODE_INVALID_MSG;
×
71
    TSDB_CHECK_CODE(code, lino, _exit);
×
72
  }
73

74
  // flags
75
  if (tDecodeI32v(pCoder, NULL) < 0) {
148,906!
76
    code = TSDB_CODE_INVALID_MSG;
×
77
    TSDB_CHECK_CODE(code, lino, _exit);
×
78
  }
79

80
  // name
81
  char *name = NULL;
148,906✔
82
  if (tDecodeCStr(pCoder, &name) < 0) {
148,904!
83
    code = TSDB_CODE_INVALID_MSG;
×
84
    TSDB_CHECK_CODE(code, lino, _exit);
×
85
  }
86

87
  // uid
88
  int64_t uid = metaGetTableEntryUidByName(pVnode->pMeta, name);
148,904✔
89
  if (uid == 0) {
148,891✔
90
    uid = tGenIdPI64();
147,509✔
91
  }
92
  taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), uid);
148,862✔
93

94
  // btime
95
  taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos + 8), btime);
148,862✔
96

97
  tEndDecode(pCoder);
148,862✔
98

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

112
  int64_t  btime = taosGetTimestampMs();
68,591✔
113
  SDecoder dc = {0};
68,591✔
114
  int32_t  nReqs;
115

116
  tDecoderInit(&dc, (uint8_t *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead));
68,591✔
117
  if (tStartDecode(&dc) < 0) {
68,584!
118
    code = TSDB_CODE_INVALID_MSG;
×
119
    return code;
×
120
  }
121

122
  if (tDecodeI32v(&dc, &nReqs) < 0) {
68,586!
123
    code = TSDB_CODE_INVALID_MSG;
×
124
    TSDB_CHECK_CODE(code, lino, _exit);
×
125
  }
126
  for (int32_t iReq = 0; iReq < nReqs; iReq++) {
150,336✔
127
    code = vnodePreprocessCreateTableReq(pVnode, &dc, btime, NULL);
81,796✔
128
    TSDB_CHECK_CODE(code, lino, _exit);
81,750!
129
  }
130

131
  tEndDecode(&dc);
68,540✔
132

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

142
static int32_t vnodePreProcessAlterTableMsg(SVnode *pVnode, SRpcMsg *pMsg) {
10,415✔
143
  int32_t code = TSDB_CODE_INVALID_MSG;
10,415✔
144
  int32_t lino = 0;
10,415✔
145

146
  SDecoder dc = {0};
10,415✔
147
  tDecoderInit(&dc, (uint8_t *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead));
10,415✔
148

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

159
  code = 0;
10,415✔
160

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

172
static int32_t vnodePreProcessDropTtlMsg(SVnode *pVnode, SRpcMsg *pMsg) {
20,429✔
173
  int32_t code = TSDB_CODE_INVALID_MSG;
20,429✔
174
  int32_t lino = 0;
20,429✔
175

176
  SMsgHead *pContOld = pMsg->pCont;
20,429✔
177
  int32_t   reqLenOld = pMsg->contLen - sizeof(SMsgHead);
20,429✔
178

179
  SArray *tbUids = NULL;
20,429✔
180
  int64_t timestampMs = 0;
20,429✔
181

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

188
  {  // find expired uids
189
    tbUids = taosArrayInit(8, sizeof(tb_uid_t));
20,443✔
190
    if (tbUids == NULL) {
20,453!
191
      code = terrno;
×
192
      TSDB_CHECK_CODE(code, lino, _exit);
×
193
    }
194

195
    timestampMs = (int64_t)ttlReq.timestampSec * 1000;
20,453✔
196
    code = metaTtlFindExpired(pVnode->pMeta, timestampMs, tbUids, ttlReq.ttlDropMaxCount);
20,453✔
197
    if (code != 0) {
20,424!
198
      code = TSDB_CODE_INVALID_MSG;
×
199
      TSDB_CHECK_CODE(code, lino, _exit);
×
200
    }
201

202
    ttlReq.nUids = taosArrayGetSize(tbUids);
20,424✔
203
    ttlReq.pTbUids = tbUids;
20,415✔
204
  }
205

206
  if (ttlReq.nUids == 0) {
20,415✔
207
    code = TSDB_CODE_MSG_PREPROCESSED;
20,390✔
208
    TSDB_CHECK_CODE(code, lino, _exit);
20,390!
209
  }
210

211
  {  // prepare new content
212
    int32_t reqLenNew = tSerializeSVDropTtlTableReq(NULL, 0, &ttlReq);
21✔
213
    int32_t contLenNew = reqLenNew + sizeof(SMsgHead);
30✔
214

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

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

227
    rpcFreeCont(pContOld);
30✔
228
    pMsg->pCont = pContNew;
30✔
229
    pMsg->contLen = contLenNew;
30✔
230
  }
231

232
  code = 0;
30✔
233

234
_exit:
20,424✔
235
  taosArrayDestroy(tbUids);
20,424✔
236

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

244
  return code;
20,417✔
245
}
246

247
extern int64_t tsMaxKeyByPrecision[];
248
static int32_t vnodePreProcessSubmitTbData(SVnode *pVnode, SDecoder *pCoder, int64_t btimeMs, int64_t ctimeMs) {
2,978,822✔
249
  int32_t code = 0;
2,978,822✔
250
  int32_t lino = 0;
2,978,822✔
251

252
  if (tStartDecode(pCoder) < 0) {
2,978,822!
253
    code = TSDB_CODE_INVALID_MSG;
×
254
    TSDB_CHECK_CODE(code, lino, _exit);
×
255
  }
256

257
  SSubmitTbData submitTbData;
258
  uint8_t       version;
259
  if (tDecodeI32v(pCoder, &submitTbData.flags) < 0) {
2,978,915!
260
    code = TSDB_CODE_INVALID_MSG;
×
261
    TSDB_CHECK_CODE(code, lino, _exit);
×
262
  }
263
  version = (submitTbData.flags >> 8) & 0xff;
2,978,915✔
264
  submitTbData.flags = submitTbData.flags & 0xff;
2,978,915✔
265

266
  int64_t uid;
267
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
2,978,915✔
268
    code = vnodePreprocessCreateTableReq(pVnode, pCoder, btimeMs, &uid);
67,094✔
269
    TSDB_CHECK_CODE(code, lino, _exit);
67,096!
270
  }
271

272
  // submit data
273
  if (tDecodeI64(pCoder, &submitTbData.suid) < 0) {
2,978,894!
274
    code = TSDB_CODE_INVALID_MSG;
×
275
    TSDB_CHECK_CODE(code, lino, _exit);
×
276
  }
277

278
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
2,978,894✔
279
    taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), uid);
67,093✔
280
    pCoder->pos += sizeof(int64_t);
67,093✔
281
  } else {
282
    if (tDecodeI64(pCoder, &submitTbData.uid) < 0) {
2,911,805!
283
      code = TSDB_CODE_INVALID_MSG;
×
284
      TSDB_CHECK_CODE(code, lino, _exit);
×
285
    }
286
  }
287

288
  if (tDecodeI32v(pCoder, &submitTbData.sver) < 0) {
2,978,877!
289
    code = TSDB_CODE_INVALID_MSG;
×
290
    TSDB_CHECK_CODE(code, lino, _exit);
×
291
  }
292

293
  // scan and check
294
  TSKEY now = btimeMs;
2,978,877✔
295
  if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_MICRO) {
2,978,877✔
296
    now *= 1000;
16,034✔
297
  } else if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_NANO) {
2,962,843✔
298
    now *= 1000000;
4,403✔
299
  }
300

301
  int32_t keep = pVnode->config.tsdbCfg.keep2;
2,978,877✔
302
  /*
303
  int32_t nlevel = tfsGetLevel(pVnode->pTfs);
304
  if (nlevel > 1 && tsSsEnabled) {
305
    if (nlevel == 3) {
306
      keep = pVnode->config.tsdbCfg.keep1;
307
    } else if (nlevel == 2) {
308
      keep = pVnode->config.tsdbCfg.keep0;
309
    }
310
  }
311
  */
312

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

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

329
    if (colData.flag != HAS_VALUE) {
10,114!
330
      code = TSDB_CODE_INVALID_MSG;
×
331
      TSDB_CHECK_CODE(code, lino, _exit);
×
332
    }
333

334
    for (int32_t iRow = 0; iRow < colData.nVal; iRow++) {
9,969,747✔
335
      if (((TSKEY *)colData.pData)[iRow] < minKey || ((TSKEY *)colData.pData)[iRow] > maxKey) {
9,959,634!
336
        code = TSDB_CODE_TDB_TIMESTAMP_OUT_OF_RANGE;
×
337
        TSDB_CHECK_CODE(code, lino, _exit);
×
338
      }
339
    }
340

341
    for (uint64_t i = 1; i < nColData; i++) {
30,540✔
342
      code = tDecodeColData(version, pCoder, &colData);
20,430✔
343
      if (code) {
20,427!
344
        code = TSDB_CODE_INVALID_MSG;
×
345
        TSDB_CHECK_CODE(code, lino, _exit);
×
346
      }
347
    }
348
  } else {
349
    uint64_t nRow;
350
    if (tDecodeU64v(pCoder, &nRow) < 0) {
2,968,754!
351
      code = TSDB_CODE_INVALID_MSG;
×
352
      TSDB_CHECK_CODE(code, lino, _exit);
108!
353
    }
354

355
    for (int32_t iRow = 0; iRow < nRow; ++iRow) {
100,873,435✔
356
      SRow *pRow = (SRow *)(pCoder->data + pCoder->pos);
97,904,789✔
357
      pCoder->pos += pRow->len;
97,904,789✔
358
#ifndef NO_UNALIGNED_ACCESS
359
      if (pRow->ts < minKey || pRow->ts > maxKey) {
97,904,789✔
360
#else
361
      TSKEY ts = taosGetInt64Aligned(&pRow->ts);
362
      if (ts < minKey || ts > maxKey) {
363
#endif
364
        code = TSDB_CODE_TDB_TIMESTAMP_OUT_OF_RANGE;
2,196✔
365
        TSDB_CHECK_CODE(code, lino, _exit);
2,196✔
366
      }
367

368
      // Check pRow->sver
369
      if (pRow->sver != submitTbData.sver) {
97,904,681!
370
        code = TSDB_CODE_INVALID_DATA_FMT;
×
371
        TSDB_CHECK_CODE(code, lino, _exit);
×
372
      }
373
    }
374
  }
375

376
  if (!tDecodeIsEnd(pCoder)) {
2,978,756!
377
    taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), ctimeMs);
2,978,797✔
378
    pCoder->pos += sizeof(int64_t);
2,978,797✔
379
  }
380

381
  tEndDecode(pCoder);
2,978,756✔
382

383
_exit:
2,978,871✔
384
  if (code) {
2,978,871✔
385
    vError("vgId:%d, %s:%d failed to vnodePreProcessSubmitTbData submit request since %s", TD_VID(pVnode), __func__,
109!
386
           lino, tstrerror(code));
387
  }
388
  return code;
2,978,881✔
389
}
390
static int32_t vnodePreProcessSubmitMsg(SVnode *pVnode, SRpcMsg *pMsg) {
2,280,498✔
391
  int32_t code = 0;
2,280,498✔
392
  int32_t lino = 0;
2,280,498✔
393

394
  if (tsBypassFlag & TSDB_BYPASS_RA_RPC_RECV_SUBMIT) {
2,280,498!
395
    return TSDB_CODE_MSG_PREPROCESSED;
×
396
  }
397

398
  SDecoder *pCoder = &(SDecoder){0};
2,280,498✔
399

400
  if (taosHton64(((SSubmitReq2Msg *)pMsg->pCont)->version) != 1) {
2,280,498!
401
    code = TSDB_CODE_INVALID_MSG;
×
402
    TSDB_CHECK_CODE(code, lino, _exit);
×
403
  }
404

405
  tDecoderInit(pCoder, (uint8_t *)pMsg->pCont + sizeof(SSubmitReq2Msg), pMsg->contLen - sizeof(SSubmitReq2Msg));
2,280,536✔
406

407
  if (tStartDecode(pCoder) < 0) {
2,280,502!
408
    code = TSDB_CODE_INVALID_MSG;
×
409
    TSDB_CHECK_CODE(code, lino, _exit);
×
410
  }
411

412
  uint64_t nSubmitTbData;
413
  if (tDecodeU64v(pCoder, &nSubmitTbData) < 0) {
2,280,497!
414
    code = TSDB_CODE_INVALID_MSG;
×
415
    TSDB_CHECK_CODE(code, lino, _exit);
×
416
  }
417

418
  int64_t btimeMs = taosGetTimestampMs();
2,280,581✔
419
  int64_t ctimeMs = btimeMs;
2,280,581✔
420
  for (int32_t i = 0; i < nSubmitTbData; i++) {
5,259,360✔
421
    code = vnodePreProcessSubmitTbData(pVnode, pCoder, btimeMs, ctimeMs);
2,978,890✔
422
    TSDB_CHECK_CODE(code, lino, _exit);
2,978,888✔
423
  }
424

425
  tEndDecode(pCoder);
2,280,470✔
426

427
_exit:
2,280,573✔
428
  tDecoderClear(pCoder);
2,280,573✔
429
  if (code) {
2,280,563✔
430
    vError("vgId:%d, %s:%d failed to preprocess submit request since %s, msg type:%s", TD_VID(pVnode), __func__, lino,
109!
431
           tstrerror(code), TMSG_INFO(pMsg->msgType));
432
  }
433
  return code;
2,280,562✔
434
}
435

436
static int32_t vnodePreProcessDeleteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
7,153✔
437
  int32_t code = 0;
7,153✔
438

439
  int32_t    size;
440
  int32_t    ret;
441
  uint8_t   *pCont;
442
  SEncoder  *pCoder = &(SEncoder){0};
7,153✔
443
  SDeleteRes res = {0};
7,153✔
444

445
  SReadHandle handle = {.vnode = pVnode, .pMsgCb = &pVnode->msgCb, .skipRollup = 1};
7,153✔
446
  initStorageAPI(&handle.api);
7,153✔
447

448
  code = qWorkerProcessDeleteMsg(&handle, pVnode->pQuery, pMsg, &res);
7,153✔
449
  if (code) goto _exit;
7,153!
450

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

456
  ((SMsgHead *)pCont)->contLen = size + sizeof(SMsgHead);
7,153✔
457
  ((SMsgHead *)pCont)->vgId = TD_VID(pVnode);
7,153✔
458

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

465
  rpcFreeCont(pMsg->pCont);
7,153✔
466
  pMsg->pCont = pCont;
7,153✔
467
  pMsg->contLen = size + sizeof(SMsgHead);
7,153✔
468

469
  taosArrayDestroy(res.uidList);
7,153✔
470

471
_exit:
7,153✔
472
  return code;
7,153✔
473
}
474

475
static int32_t vnodePreProcessBatchDeleteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
965✔
476
  int32_t code = 0;
965✔
477
  int32_t lino = 0;
965✔
478

479
  int64_t         ctimeMs = taosGetTimestampMs();
965✔
480
  SBatchDeleteReq pReq = {0};
965✔
481
  SDecoder       *pCoder = &(SDecoder){0};
965✔
482

483
  tDecoderInit(pCoder, (uint8_t *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead));
965✔
484

485
  if (tDecodeSBatchDeleteReqSetCtime(pCoder, &pReq, ctimeMs) < 0) {
965!
486
    code = TSDB_CODE_INVALID_MSG;
×
487
  }
488

489
  tDecoderClear(pCoder);
965✔
490
  taosArrayDestroy(pReq.deleteReqs);
965✔
491

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

500
static int32_t vnodePreProcessArbCheckSyncMsg(SVnode *pVnode, SRpcMsg *pMsg) {
34✔
501
  SVArbCheckSyncReq syncReq = {0};
34✔
502

503
  if (tDeserializeSVArbCheckSyncReq((char *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead),
34!
504
                                    &syncReq) != 0) {
505
    return TSDB_CODE_INVALID_MSG;
×
506
  }
507

508
  int32_t ret = vnodeCheckToken(pVnode, syncReq.member0Token, syncReq.member1Token);
34✔
509
  if (ret != 0) {
34✔
510
    vError("vgId:%d, failed to preprocess arb check sync request since %s", TD_VID(pVnode), tstrerror(ret));
27!
511
  }
512

513
  int32_t code = terrno;
34✔
514
  tFreeSVArbCheckSyncReq(&syncReq);
34✔
515

516
  return code;
34✔
517
}
518

519
int32_t vnodePreProcessDropTbMsg(SVnode *pVnode, SRpcMsg *pMsg) {
15,749✔
520
  int32_t          code = TSDB_CODE_SUCCESS;
15,749✔
521
  int32_t          lino = 0;
15,749✔
522
  int32_t          size = 0;
15,749✔
523
  SDecoder         dc = {0};
15,749✔
524
  SEncoder         ec = {0};
15,749✔
525
  SVDropTbBatchReq receivedBatchReqs = {0};
15,749✔
526
  SVDropTbBatchReq sentBatchReqs = {0};
15,749✔
527

528
  tDecoderInit(&dc, POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), pMsg->contLen - sizeof(SMsgHead));
15,749✔
529

530
  code = tDecodeSVDropTbBatchReq(&dc, &receivedBatchReqs);
15,749✔
531
  if (code < 0) {
15,749!
532
    terrno = code;
×
533
    TSDB_CHECK_CODE(code, lino, _exit);
×
534
  }
535
  sentBatchReqs.pArray = taosArrayInit(receivedBatchReqs.nReqs, sizeof(SVDropTbReq));
15,749✔
536
  if (!sentBatchReqs.pArray) {
15,749!
537
    code = terrno;
×
538
    goto _exit;
×
539
  }
540

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

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

566
_exit:
15,749✔
567
  tDecoderClear(&dc);
15,749✔
568
  if (sentBatchReqs.pArray) {
15,749!
569
    taosArrayDestroy(sentBatchReqs.pArray);
15,749✔
570
  }
571
  return code;
15,749✔
572
}
573

574

575
int32_t vnodePreProcessSsMigrateReq(SVnode* pVnode, SRpcMsg* pMsg) {
×
576
  int32_t          code = TSDB_CODE_SUCCESS;
×
577
  int32_t          lino = 0;
×
578
  SSsMigrateVgroupReq req = {0};
×
579

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

586
  req.nodeId = vnodeNodeId(pVnode);
×
587
  tSerializeSSsMigrateVgroupReq(POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), pMsg->contLen - sizeof(SMsgHead), &req);
×
588

589
_exit:
×
590
  return code;
×
591
}
592

593

594
int32_t vnodePreProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
2,489,000✔
595
  int32_t code = 0;
2,489,000✔
596

597
  if (pVnode->mounted) {
2,489,000!
598
    vError("vgId:%d, mountVgId:%d, skip write msg:%s", TD_VID(pVnode), pVnode->config.mountVgId,
×
599
           TMSG_INFO(pMsg->msgType));
600
    return TSDB_CODE_OPS_NOT_SUPPORT;
×
601
  }
602

603
  switch (pMsg->msgType) {
2,489,000!
604
    case TDMT_VND_CREATE_TABLE: {
68,589✔
605
      code = vnodePreProcessCreateTableMsg(pVnode, pMsg);
68,589✔
606
    } break;
68,591✔
607
    case TDMT_VND_ALTER_TABLE: {
10,415✔
608
      code = vnodePreProcessAlterTableMsg(pVnode, pMsg);
10,415✔
609
    } break;
10,415✔
610
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
20,434✔
611
    case TDMT_VND_DROP_TTL_TABLE: {
612
      code = vnodePreProcessDropTtlMsg(pVnode, pMsg);
20,434✔
613
    } break;
20,415✔
614
    case TDMT_VND_SUBMIT: {
2,280,512✔
615
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.preprocess_time, METRIC_LEVEL_LOW,
2,280,512!
616
                           { code = vnodePreProcessSubmitMsg(pVnode, pMsg); });
617
    } break;
2,280,563✔
618
    case TDMT_VND_DELETE: {
7,153✔
619
      code = vnodePreProcessDeleteMsg(pVnode, pMsg);
7,153✔
620
    } break;
7,153✔
621
    case TDMT_VND_BATCH_DEL: {
965✔
622
      code = vnodePreProcessBatchDeleteMsg(pVnode, pMsg);
965✔
623
    } break;
965✔
624
    case TDMT_VND_ARB_CHECK_SYNC: {
34✔
625
      code = vnodePreProcessArbCheckSyncMsg(pVnode, pMsg);
34✔
626
    } break;
34✔
627
    case TDMT_VND_DROP_TABLE: {
15,749✔
628
      code = vnodePreProcessDropTbMsg(pVnode, pMsg);
15,749✔
629
    } break;
15,749✔
630
    case TDMT_VND_SSMIGRATE: {
×
631
      code = vnodePreProcessSsMigrateReq(pVnode, pMsg);
×
632
    } break;
×
633
    default:
85,149✔
634
      break;
85,149✔
635
  }
636

637
  if (code && code != TSDB_CODE_MSG_PREPROCESSED) {
2,489,034✔
638
    vError("vgId:%d, failed to preprocess write request since %s, msg type:%s", TD_VID(pVnode), tstrerror(code),
136!
639
           TMSG_INFO(pMsg->msgType));
640
  }
641
  return code;
2,488,941✔
642
}
643

644
int32_t vnodeProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg, int64_t ver, SRpcMsg *pRsp) {
3,345,347✔
645
  int32_t code = 0;
3,345,347✔
646
  void   *ptr = NULL;
3,345,347✔
647
  void   *pReq;
648
  int32_t len;
649

650
  (void)taosThreadMutexLock(&pVnode->mutex);
3,345,347✔
651
  if (pVnode->disableWrite) {
3,345,636!
652
    (void)taosThreadMutexUnlock(&pVnode->mutex);
×
653
    vError("vgId:%d write is disabled for snapshot, version:%" PRId64, TD_VID(pVnode), ver);
×
654
    return TSDB_CODE_VND_WRITE_DISABLED;
×
655
  }
656
  (void)taosThreadMutexUnlock(&pVnode->mutex);
3,345,636✔
657

658
  if (ver <= pVnode->state.applied) {
3,345,627!
659
    vError("vgId:%d, duplicate write request. ver: %" PRId64 ", applied: %" PRId64, TD_VID(pVnode), ver,
×
660
           pVnode->state.applied);
661
    return terrno = TSDB_CODE_VND_DUP_REQUEST;
×
662
  }
663

664
  vGDebug(&pMsg->info.traceId,
3,345,627!
665
          "vgId:%d, index:%" PRId64 ", process write request:%s, applied:%" PRId64 ", state.applyTerm:%" PRId64
666
          ", conn.applyTerm:%" PRId64 ", contLen:%d",
667
          TD_VID(pVnode), ver, TMSG_INFO(pMsg->msgType), pVnode->state.applied, pVnode->state.applyTerm,
668
          pMsg->info.conn.applyTerm, pMsg->contLen);
669

670
  if (!(pVnode->state.applyTerm <= pMsg->info.conn.applyTerm)) {
3,345,319!
671
    vError("vgId:%d, applyTerm mismatch, expected: %" PRId64 ", received: %" PRId64, TD_VID(pVnode),
×
672
           pVnode->state.applyTerm, pMsg->info.conn.applyTerm);
673
    return terrno = TSDB_CODE_INTERNAL_ERROR;
×
674
  }
675

676
  if (!(pVnode->state.applied + 1 == ver)) {
3,345,319!
677
    vError("vgId:%d, mountVgId:%d, ver mismatch, expected: %" PRId64 ", received: %" PRId64, TD_VID(pVnode), pVnode->config.mountVgId,
×
678
           pVnode->state.applied + 1, ver);
679
    return terrno = TSDB_CODE_INTERNAL_ERROR;
×
680
  }
681

682
  atomic_store_64(&pVnode->state.applied, ver);
3,345,319✔
683
  atomic_store_64(&pVnode->state.applyTerm, pMsg->info.conn.applyTerm);
3,345,583✔
684

685
  if (!syncUtilUserCommit(pMsg->msgType)) goto _exit;
3,345,626✔
686

687
  // skip header
688
  pReq = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
3,305,760✔
689
  len = pMsg->contLen - sizeof(SMsgHead);
3,305,760✔
690
  bool needCommit = false;
3,305,760✔
691

692
  switch (pMsg->msgType) {
3,305,760!
693
    /* META */
694
    case TDMT_VND_CREATE_STB:
26,430✔
695
      if (vnodeProcessCreateStbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
26,430✔
696
      break;
26,532✔
697
    case TDMT_VND_ALTER_STB:
12,685✔
698
      if (vnodeProcessAlterStbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
12,685!
699
      break;
12,732✔
700
    case TDMT_VND_DROP_STB:
1,942✔
701
      if (vnodeProcessDropStbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
1,942!
702
      break;
1,947✔
703
    case TDMT_VND_CREATE_TABLE:
79,046✔
704
      if (vnodeProcessCreateTbReq(pVnode, ver, pReq, len, pRsp, pMsg) < 0) goto _err;
79,046!
705
      break;
79,075✔
706
    case TDMT_VND_ALTER_TABLE:
10,420✔
707
      if (vnodeProcessAlterTbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
10,420!
708
      break;
10,420✔
709
    case TDMT_VND_DROP_TABLE:
15,791✔
710
      if (vnodeProcessDropTbReq(pVnode, ver, pReq, len, pRsp, pMsg) < 0) goto _err;
15,791!
711
      break;
15,791✔
712
    case TDMT_VND_DROP_TTL_TABLE:
×
713
      if (vnodeProcessDropTtlTbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
714
      break;
×
715
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
29✔
716
      if (vnodeProcessFetchTtlExpiredTbs(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
29!
717
      break;
29✔
718
    case TDMT_VND_TRIM:
18✔
719
      if (vnodeProcessTrimReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
18!
720
      break;
18✔
721
    case TDMT_VND_SSMIGRATE:
×
722
      if (vnodeProcessSsMigrateReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
723
      break;
×
724
    case TDMT_VND_FOLLOWER_SSMIGRATE:
×
725
      if (vnodeProcessFollowerSsMigrateReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
726
      break;
×
727
    case TDMT_VND_CREATE_SMA:
×
728
      if (vnodeProcessCreateTSmaReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
729
      break;
×
730
    /* TSDB */
731
    case TDMT_VND_SUBMIT: {
3,079,911✔
732
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.apply_time, METRIC_LEVEL_LOW, {
3,079,911!
733
        if (vnodeProcessSubmitReq(pVnode, ver, pMsg->pCont, pMsg->contLen, pRsp, pMsg) < 0) goto _err;
734
      });
735
      METRICS_UPDATE(pVnode->writeMetrics.apply_bytes, METRIC_LEVEL_LOW, (int64_t)pMsg->contLen);
3,079,962!
736
      break;
3,079,954✔
737
    }
738
    case TDMT_VND_DELETE:
8,353✔
739
      if (vnodeProcessDeleteReq(pVnode, ver, pReq, len, pRsp, pMsg) < 0) goto _err;
8,353!
740
      break;
8,353✔
741
    case TDMT_VND_BATCH_DEL:
965✔
742
      if (vnodeProcessBatchDeleteReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
965!
743
      break;
965✔
744
      /* TQ */
745
#if defined(USE_TQ) || defined(USE_STREAM)
746
    case TDMT_VND_TMQ_SUBSCRIBE:
3,319✔
747
      if (tqProcessSubscribeReq(pVnode->pTq, ver, pReq, len) < 0) {
3,319!
748
        goto _err;
×
749
      }
750
      break;
3,331✔
751
    case TDMT_VND_TMQ_DELETE_SUB:
995✔
752
      if (tqProcessDeleteSubReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen) < 0) {
995!
753
        goto _err;
×
754
      }
755
      break;
998✔
756
    case TDMT_VND_TMQ_COMMIT_OFFSET:
10,155✔
757
      if (tqProcessOffsetCommitReq(pVnode->pTq, ver, pReq, len) < 0) {
10,155!
758
        goto _err;
×
759
      }
760
      break;
10,164✔
761
    case TDMT_VND_TMQ_ADD_CHECKINFO:
122✔
762
      if (tqProcessAddCheckInfoReq(pVnode->pTq, ver, pReq, len) < 0) {
122!
763
        goto _err;
×
764
      }
765
      break;
122✔
766
    case TDMT_VND_TMQ_DEL_CHECKINFO:
12✔
767
      if (tqProcessDelCheckInfoReq(pVnode->pTq, ver, pReq, len) < 0) {
12!
768
        goto _err;
×
769
      }
770
      break;
12✔
771
    case TDMT_STREAM_TASK_DEPLOY: {
6,574✔
772
      if ((code = tqProcessTaskDeployReq(pVnode->pTq, ver, pReq, len)) != TSDB_CODE_SUCCESS) {
6,574!
773
        goto _err;
×
774
      }
775
    } break;
6,575✔
776
    case TDMT_STREAM_TASK_DROP: {
2,633✔
777
      if ((code = tqProcessTaskDropReq(pVnode->pTq, pMsg->pCont, pMsg->contLen)) < 0) {
2,633!
778
        goto _err;
×
779
      }
780
    } break;
2,633✔
781
    case TDMT_STREAM_TASK_UPDATE_CHKPT: {
1,313✔
782
      if ((code = tqProcessTaskUpdateCheckpointReq(pVnode->pTq, pMsg->pCont, pMsg->contLen)) < 0) {
1,313!
783
        goto _err;
×
784
      }
785
    } break;
1,313✔
786
    case TDMT_STREAM_CONSEN_CHKPT: {
26✔
787
      if (pVnode->restored && (code = tqProcessTaskConsenChkptIdReq(pVnode->pTq, pMsg)) < 0) {
26!
788
        goto _err;
×
789
      }
790

791
    } break;
26✔
792
    case TDMT_STREAM_TASK_PAUSE: {
231✔
793
      if (pVnode->restored && vnodeIsLeader(pVnode) &&
372!
794
          (code = tqProcessTaskPauseReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen)) < 0) {
141✔
795
        goto _err;
×
796
      }
797
    } break;
231✔
798
    case TDMT_STREAM_TASK_RESUME: {
224✔
799
      if (pVnode->restored && vnodeIsLeader(pVnode) &&
358!
800
          (code = tqProcessTaskResumeReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen)) < 0) {
134✔
801
        goto _err;
×
802
      }
803
    } break;
224✔
804
    case TDMT_VND_STREAM_TASK_RESET: {
×
805
      if (pVnode->restored && vnodeIsLeader(pVnode) && (code = tqProcessTaskResetReq(pVnode->pTq, pMsg)) < 0) {
×
806
        goto _err;
×
807
      }
808

809
    } break;
×
810
    case TDMT_VND_STREAM_ALL_STOP: {
4,070✔
811
      if (pVnode->restored && vnodeIsLeader(pVnode) && (code = tqProcessAllTaskStopReq(pVnode->pTq, pMsg)) < 0) {
4,070!
812
        goto _err;
×
813
      }
814

815
    } break;
4,082✔
816
#endif
817
    case TDMT_VND_ALTER_CONFIRM:
15,036✔
818
      needCommit = pVnode->config.hashChange;
15,036✔
819
      if (vnodeProcessAlterConfirmReq(pVnode, ver, pReq, len, pRsp) < 0) {
15,036!
820
        goto _err;
×
821
      }
822
      break;
15,036✔
823
    case TDMT_VND_ALTER_CONFIG:
821✔
824
      vnodeProcessAlterConfigReq(pVnode, ver, pReq, len, pRsp);
821✔
825
      break;
823✔
826
    case TDMT_VND_COMMIT:
23,500✔
827
      needCommit = true;
23,500✔
828
      break;
23,500✔
829
    case TDMT_VND_CREATE_INDEX:
26✔
830
      vnodeProcessCreateIndexReq(pVnode, ver, pReq, len, pRsp);
26✔
831
      break;
26✔
832
    case TDMT_VND_DROP_INDEX:
16✔
833
      vnodeProcessDropIndexReq(pVnode, ver, pReq, len, pRsp);
16✔
834
      break;
16✔
835
    case TDMT_VND_STREAM_CHECK_POINT_SOURCE:  // always return true
861✔
836
      tqProcessTaskCheckPointSourceReq(pVnode->pTq, pMsg, pRsp);
861✔
837
      break;
861✔
838
    case TDMT_VND_STREAM_TASK_UPDATE:  // always return true
28✔
839
      tqProcessTaskUpdateReq(pVnode->pTq, pMsg);
28✔
840
      break;
28✔
841
    case TDMT_VND_COMPACT:
194✔
842
      vnodeProcessCompactVnodeReq(pVnode, ver, pReq, len, pRsp);
194✔
843
      goto _exit;
194✔
844
    case TDMT_SYNC_CONFIG_CHANGE:
×
845
      vnodeProcessConfigChangeReq(pVnode, ver, pReq, len, pRsp);
×
846
      break;
×
847
#ifdef TD_ENTERPRISE
848
    case TDMT_VND_KILL_COMPACT:
×
849
      vnodeProcessKillCompactReq(pVnode, ver, pReq, len, pRsp);
×
850
      break;
×
851
#endif
852
    /* ARB */
853
    case TDMT_VND_ARB_CHECK_SYNC:
14✔
854
      vnodeProcessArbCheckSyncReq(pVnode, pReq, len, pRsp);
14✔
855
      break;
14✔
856
    default:
×
857
      vError("vgId:%d, unprocessed msg, %d", TD_VID(pVnode), pMsg->msgType);
×
858
      return TSDB_CODE_INVALID_MSG;
×
859
  }
860

861
  vGDebug(&pMsg->info.traceId, "vgId:%d, index:%" PRId64 ", msg processed, code:0x%x", TD_VID(pVnode), ver, pRsp->code);
3,305,831!
862

863
  walApplyVer(pVnode->pWal, ver);
3,305,876✔
864

865
  code = tqPushMsg(pVnode->pTq, pMsg->msgType);
3,305,840✔
866
  if (code) {
3,305,859!
867
    vError("vgId:%d, failed to push msg to TQ since %s", TD_VID(pVnode), tstrerror(terrno));
×
868
    return code;
×
869
  }
870

871
  // commit if need
872
  if (needCommit) {
3,305,859✔
873
    vInfo("vgId:%d, commit at version %" PRId64, TD_VID(pVnode), ver);
23,538!
874
    code = vnodeAsyncCommit(pVnode);
23,585✔
875
    if (code) {
23,590!
876
      vError("vgId:%d, failed to vnode async commit since %s.", TD_VID(pVnode), tstrerror(terrno));
×
877
      goto _err;
×
878
    }
879

880
    // start a new one
881
    METRICS_TIMING_BLOCK(pVnode->writeMetrics.memtable_wait_time, METRIC_LEVEL_LOW, {
23,590!
882
      code = vnodeBegin(pVnode);
883
      if (code) {
884
        vError("vgId:%d, failed to begin vnode since %s.", TD_VID(pVnode), tstrerror(terrno));
885
        goto _err;
886
      }
887
    });
888
  }
889

890
_exit:
3,305,908✔
891
  return 0;
3,345,494✔
892

893
_err:
11✔
894
  vError("vgId:%d, process %s request failed since %s, ver:%" PRId64, TD_VID(pVnode), TMSG_INFO(pMsg->msgType),
11!
895
         tstrerror(terrno), ver);
896
  return code;
11✔
897
}
898

899
int32_t vnodePreprocessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg) {
1,748,278✔
900
  if (TDMT_SCH_QUERY != pMsg->msgType && TDMT_SCH_MERGE_QUERY != pMsg->msgType) {
1,748,278✔
901
    return 0;
106,888✔
902
  }
903

904
  return qWorkerPreprocessQueryMsg(pVnode->pQuery, pMsg, TDMT_SCH_QUERY == pMsg->msgType);
1,641,390✔
905
}
906

907
int32_t vnodeProcessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
1,750,958✔
908
  vTrace("message in vnode query queue is processing");
1,750,958✔
909
  if (pMsg->msgType == TDMT_VND_TMQ_CONSUME && !syncIsReadyForRead(pVnode->sync)) {
1,750,958✔
910
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
1,467✔
911
    return 0;
1,467✔
912
  }
913

914
  if (pMsg->msgType == TDMT_VND_TMQ_CONSUME && !pVnode->restored) {
1,749,473!
915
    vnodeRedirectRpcMsg(pVnode, pMsg, TSDB_CODE_SYN_RESTORING);
×
916
    return 0;
×
917
  }
918

919
  SReadHandle handle = {.vnode = pVnode, .pMsgCb = &pVnode->msgCb, .pWorkerCb = pInfo->workerCb};
1,749,473✔
920
  initStorageAPI(&handle.api);
1,749,473✔
921
  int32_t code = TSDB_CODE_SUCCESS;
1,749,357✔
922
  bool    redirected = false;
1,749,357✔
923

924
  switch (pMsg->msgType) {
1,749,357✔
925
    case TDMT_SCH_QUERY:
1,466,343✔
926
      if (!syncIsReadyForRead(pVnode->sync)) {
1,466,343✔
927
        pMsg->code = (terrno) ? terrno : TSDB_CODE_SYN_NOT_LEADER;
4,479!
928
        redirected = true;
4,479✔
929
      }
930
      code = qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
1,466,374✔
931
      if (redirected) {
1,465,353✔
932
        vnodeRedirectRpcMsg(pVnode, pMsg, pMsg->code);
4,073✔
933
        return 0;
4,479✔
934
      }
935
      return code;
1,461,280✔
936
    case TDMT_SCH_MERGE_QUERY:
177,570✔
937
      return qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
177,570✔
938
    case TDMT_SCH_QUERY_CONTINUE:
35,632✔
939
      return qWorkerProcessCQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
35,632✔
940
    case TDMT_VND_TMQ_CONSUME:
54,472✔
941
      return tqProcessPollReq(pVnode->pTq, pMsg);
54,472✔
942
    case TDMT_VND_TMQ_CONSUME_PUSH:
15,288✔
943
      return tqProcessPollPush(pVnode->pTq);
15,288✔
944
    default:
52✔
945
      vError("unknown msg type:%d in query queue", pMsg->msgType);
52!
946
      return TSDB_CODE_APP_ERROR;
×
947
  }
948
}
949

950
int32_t vnodeProcessFetchMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
4,550,642✔
951
  int32_t code = TSDB_CODE_SUCCESS;
4,550,642✔
952
  vTrace("vgId:%d, msg:%p in fetch queue is processing", pVnode->config.vgId, pMsg);
4,550,642✔
953
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
4,550,647!
954
       pMsg->msgType == TDMT_VND_BATCH_META || pMsg->msgType == TDMT_VND_TABLE_NAME ||
3,047,070!
955
       pMsg->msgType == TDMT_VND_VSUBTABLES_META || pMsg->msgType == TDMT_VND_VSTB_REF_DBS) &&
2,932,255✔
956
      !syncIsReadyForRead(pVnode->sync)) {
1,619,645✔
957
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
1,598✔
958
    return 0;
1,598✔
959
  }
960

961
  switch (pMsg->msgType) {
4,549,747!
962
    case TDMT_SCH_FETCH:
1,752,726✔
963
    case TDMT_SCH_MERGE_FETCH:
964
      return qWorkerProcessFetchMsg(pVnode, pVnode->pQuery, pMsg, 0);
1,752,726✔
965
    case TDMT_SCH_FETCH_RSP:
×
966
      return qWorkerProcessRspMsg(pVnode, pVnode->pQuery, pMsg, 0);
×
967
    // case TDMT_SCH_CANCEL_TASK:
968
    //   return qWorkerProcessCancelMsg(pVnode, pVnode->pQuery, pMsg, 0);
969
    case TDMT_SCH_DROP_TASK:
1,642,624✔
970
      return qWorkerProcessDropMsg(pVnode, pVnode->pQuery, pMsg, 0);
1,642,624✔
971
    case TDMT_SCH_TASK_NOTIFY:
39✔
972
      return qWorkerProcessNotifyMsg(pVnode, pVnode->pQuery, pMsg, 0);
39✔
973
    case TDMT_SCH_QUERY_HEARTBEAT:
1,039,201✔
974
      return qWorkerProcessHbMsg(pVnode, pVnode->pQuery, pMsg, 0);
1,039,201✔
975
    case TDMT_VND_TABLE_META:
1,771✔
976
    case TDMT_VND_TABLE_NAME:
977
      return vnodeGetTableMeta(pVnode, pMsg, true);
1,771✔
978
    case TDMT_VND_TABLE_CFG:
×
979
      return vnodeGetTableCfg(pVnode, pMsg, true);
×
980
    case TDMT_VND_BATCH_META: {
113,305✔
981
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.fetch_batch_meta_time, METRIC_LEVEL_LOW,
113,305!
982
                           { code = vnodeGetBatchMeta(pVnode, pMsg); });
983
      METRICS_UPDATE(pVnode->writeMetrics.fetch_batch_meta_count, METRIC_LEVEL_LOW, 1);
113,309!
984
      return code;
113,308✔
985
    }
986
    case TDMT_VND_VSUBTABLES_META:
×
987
      return vnodeGetVSubtablesMeta(pVnode, pMsg);
×
988
    case TDMT_VND_VSTB_REF_DBS:
×
989
      return vnodeGetVStbRefDbs(pVnode, pMsg);
×
990
#ifdef TD_ENTERPRISE
991
    case TDMT_VND_QUERY_COMPACT_PROGRESS:
38✔
992
      return vnodeQueryCompactProgress(pVnode, pMsg);
38✔
993
#endif
994
    case TDMT_VND_QUERY_SSMIGRATE_PROGRESS:
×
995
      return vnodeQuerySsMigrateProgress(pVnode, pMsg);
×
996
      //    case TDMT_VND_TMQ_CONSUME:
997
      //      return tqProcessPollReq(pVnode->pTq, pMsg);
998
#ifdef USE_TQ
999
    case TDMT_VND_TMQ_VG_WALINFO:
11✔
1000
      return tqProcessVgWalInfoReq(pVnode->pTq, pMsg);
11✔
1001
    case TDMT_VND_TMQ_VG_COMMITTEDINFO:
8✔
1002
      return tqProcessVgCommittedInfoReq(pVnode->pTq, pMsg);
8✔
1003
    case TDMT_VND_TMQ_SEEK:
24✔
1004
      return tqProcessSeekReq(pVnode->pTq, pMsg);
24✔
1005
#endif
1006
    default:
×
1007
      vError("unknown msg type:%d in fetch queue", pMsg->msgType);
×
1008
      return TSDB_CODE_APP_ERROR;
×
1009
  }
1010
}
1011
#ifdef USE_STREAM
1012
int32_t vnodeProcessStreamMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
104,595✔
1013
  vTrace("vgId:%d, msg:%p in stream queue is processing", pVnode->config.vgId, pMsg);
104,595✔
1014

1015
  // todo: NOTE: some command needs to run on follower, such as, stop_all_tasks
1016
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
104,595!
1017
       pMsg->msgType == TDMT_VND_BATCH_META) &&
104,638!
1018
      !syncIsReadyForRead(pVnode->sync)) {
×
1019
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
×
1020
    return 0;
×
1021
  }
1022

1023
  switch (pMsg->msgType) {
104,638!
1024
    case TDMT_STREAM_TASK_RUN:
103,836✔
1025
      return tqProcessTaskRunReq(pVnode->pTq, pMsg);
103,836✔
1026
    case TDMT_STREAM_RETRIEVE:
404✔
1027
      return tqProcessTaskRetrieveReq(pVnode->pTq, pMsg);
404✔
1028
    case TDMT_STREAM_RETRIEVE_RSP:
398✔
1029
      return tqProcessTaskRetrieveRsp(pVnode->pTq, pMsg);
398✔
1030
    case TDMT_VND_GET_STREAM_PROGRESS:
×
1031
      return tqStreamProgressRetrieveReq(pVnode->pTq, pMsg);
×
1032
    default:
×
1033
      vError("unknown msg type:%d in stream queue", pMsg->msgType);
×
1034
      return TSDB_CODE_APP_ERROR;
×
1035
  }
1036
}
1037

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

1047
  switch (pMsg->msgType) {
57,358!
1048
    case TDMT_MND_STREAM_HEARTBEAT_RSP:
5,338✔
1049
      return tqProcessStreamHbRsp(pVnode->pTq, pMsg);
5,338✔
1050
    case TDMT_STREAM_TASK_DISPATCH:
12,018✔
1051
      return tqProcessTaskDispatchReq(pVnode->pTq, pMsg);
12,018✔
1052
    case TDMT_STREAM_TASK_DISPATCH_RSP:
12,330✔
1053
      return tqProcessTaskDispatchRsp(pVnode->pTq, pMsg);
12,330✔
1054
    case TDMT_VND_STREAM_TASK_CHECK:
9,725✔
1055
      return tqProcessTaskCheckReq(pVnode->pTq, pMsg);
9,725✔
1056
    case TDMT_VND_STREAM_TASK_CHECK_RSP:
9,751✔
1057
      return tqProcessTaskCheckRsp(pVnode->pTq, pMsg);
9,751✔
1058
    case TDMT_STREAM_TASK_CHECKPOINT_READY:
2,426✔
1059
      return tqProcessTaskCheckpointReadyMsg(pVnode->pTq, pMsg);
2,426✔
1060
    case TDMT_STREAM_TASK_CHECKPOINT_READY_RSP:
2,426✔
1061
      return tqProcessTaskCheckpointReadyRsp(pVnode->pTq, pMsg);
2,426✔
1062
    case TDMT_STREAM_RETRIEVE_TRIGGER:
×
1063
      return tqProcessTaskRetrieveTriggerReq(pVnode->pTq, pMsg);
×
1064
    case TDMT_STREAM_RETRIEVE_TRIGGER_RSP:
×
1065
      return tqProcessTaskRetrieveTriggerRsp(pVnode->pTq, pMsg);
×
1066
    case TDMT_MND_STREAM_REQ_CHKPT_RSP:
1,677✔
1067
      return tqProcessStreamReqCheckpointRsp(pVnode->pTq, pMsg);
1,677✔
1068
    case TDMT_MND_STREAM_CHKPT_REPORT_RSP:
1,667✔
1069
      return tqProcessTaskChkptReportRsp(pVnode->pTq, pMsg);
1,667✔
1070
    default:
×
1071
      vError("unknown msg type:%d in stream ctrl queue", pMsg->msgType);
×
1072
      return TSDB_CODE_APP_ERROR;
×
1073
  }
1074
}
1075

1076
int32_t vnodeProcessStreamLongExecMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
1,029✔
1077
  vTrace("vgId:%d, msg:%p in stream long exec queue is processing", pVnode->config.vgId, pMsg);
1,029✔
1078
  if (!syncIsReadyForRead(pVnode->sync)) {
1,029!
1079
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
×
1080
    return 0;
×
1081
  }
1082

1083
  switch (pMsg->msgType) {
1,029!
1084
    case TDMT_VND_STREAM_SCAN_HISTORY:
1,029✔
1085
      return tqProcessTaskScanHistory(pVnode->pTq, pMsg);
1,029✔
1086
    default:
×
1087
      vError("unknown msg type:%d in stream long exec queue", pMsg->msgType);
×
1088
      return TSDB_CODE_APP_ERROR;
×
1089
  }
1090
}
1091

1092
int32_t vnodeProcessStreamChkptMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
3,208✔
1093
  vTrace("vgId:%d, msg:%p in stream chkpt queue is processing", pVnode->config.vgId, pMsg);
3,208✔
1094
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
3,208!
1095
       pMsg->msgType == TDMT_VND_BATCH_META) &&
3,208!
1096
      !syncIsReadyForRead(pVnode->sync)) {
×
1097
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
×
1098
    return 0;
×
1099
  }
1100

1101
  switch (pMsg->msgType) {
3,208!
1102
    case TDMT_STREAM_CHKPT_EXEC:
3,208✔
1103
      return tqProcessTaskRunReq(pVnode->pTq, pMsg);
3,208✔
1104
    default:
×
1105
      vError("unknown msg type:%d in stream chkpt queue", pMsg->msgType);
×
1106
      return TSDB_CODE_APP_ERROR;
×
1107
  }
1108
}
1109
#endif
1110

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

1118
void vnodeUpdateMetaRsp(SVnode *pVnode, STableMetaRsp *pMetaRsp) {
179,300✔
1119
  if (NULL == pMetaRsp) {
179,300!
1120
    return;
×
1121
  }
1122

1123
  tstrncpy(pMetaRsp->dbFName, pVnode->config.dbname, TSDB_DB_FNAME_LEN);
179,300✔
1124
  pMetaRsp->dbId = pVnode->config.dbId;
179,300✔
1125
  pMetaRsp->vgId = TD_VID(pVnode);
179,300✔
1126
  pMetaRsp->precision = pVnode->config.tsdbCfg.precision;
179,300✔
1127
}
1128

1129
extern int32_t vnodeAsyncRetention(SVnode *pVnode, int64_t now);
1130

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

1137
  int32_t     code = 0;
12✔
1138
  SVTrimDbReq trimReq = {0};
12✔
1139

1140
  // decode
1141
  if (tDeserializeSVTrimDbReq(pReq, len, &trimReq) != 0) {
12!
1142
    code = TSDB_CODE_INVALID_MSG;
×
1143
    goto _exit;
×
1144
  }
1145

1146
  vInfo("vgId:%d, process trim vnode request, time:%d", pVnode->config.vgId, trimReq.timestamp);
12!
1147

1148
  code = vnodeAsyncRetention(pVnode, trimReq.timestamp);
12✔
1149

1150
_exit:
12✔
1151
  return code;
12✔
1152
}
1153

1154
extern int32_t vnodeAsyncSsMigrate(SVnode *pVnode, SSsMigrateVgroupReq *pReq);
1155

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

1165
  if (tDeserializeSSsMigrateVgroupReq(pReq, len, &req) != 0) {
×
1166
    code = TSDB_CODE_INVALID_MSG;
×
1167
    goto _exit;
×
1168
  }
1169

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

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

1179
  rsp.ssMigrateId = req.ssMigrateId;
×
1180
  rsp.vgId = TD_VID(pVnode);
×
1181
  rsp.nodeId = req.nodeId;
×
1182

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

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

1200

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1362
  tDecoderClear(&coder);
26,548✔
1363
  return 0;
26,521✔
1364

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

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

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

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

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

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

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

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

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

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

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

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

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

1496
    SStringBuilder sb = {0};
79,074✔
1497
    for (int32_t i = 0; i < tbNames->size; i++) {
185,691✔
1498
      char **key = (char **)taosArrayGet(tbNames, i);
106,616✔
1499
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
106,616✔
1500
      if (i < tbNames->size - 1) {
106,618✔
1501
        taosStringBuilderAppendChar(&sb, ',');
27,545✔
1502
      }
1503
      // taosMemoryFreeClear(*key);
1504
    }
1505

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

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

1513
    taosStringBuilderDestroy(&sb);
79,072✔
1514
  }
1515

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

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

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

1536
  tDecoderInit(&dc, pReq, len);
12,683✔
1537

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

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

1552
  tDecoderClear(&dc);
12,732✔
1553

1554
  return 0;
12,732✔
1555
}
1556

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

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

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

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

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

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

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

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

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

1615
  tDecoderInit(&dc, pReq, len);
10,420✔
1616

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

1624
  // process
1625
  if (metaAlterTable(pVnode->pMeta, ver, &vAlterTbReq, &vMetaRsp) < 0) {
10,420✔
1626
    vAlterTbRsp.code = terrno;
916✔
1627
    tDecoderClear(&dc);
916✔
1628
    goto _exit;
916✔
1629
  }
1630
  tDecoderClear(&dc);
9,504✔
1631

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

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

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

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

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

1660
    taosArrayDestroy(tbUids);
3,682✔
1661
  }
1662

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1837
  taosMemoryFreeClear(pSchema);
1838

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

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

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

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

1891
  return TSDB_CODE_SUCCESS;
×
1892
}
1893

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

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

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

1912
  if (IS_VAR_DATA_TYPE(pCol->type)) {
×
1913
    pColVal->value.nData = varDataLen(pCellVal->val);
×
1914
    pColVal->value.pData = (uint8_t *)varDataVal(pCellVal->val);
×
1915
  } else if (TSDB_DATA_TYPE_FLOAT == pCol->type) {
×
1916
    float f = GET_FLOAT_VAL(pCellVal->val);
×
1917
    valueSetDatum(&pColVal->value, pCol->type, &f, sizeof(f));
×
1918
  } else if (TSDB_DATA_TYPE_DOUBLE == pCol->type) {
×
1919
    taosSetPInt64Aligned(&pColVal->value.val, (int64_t *)pCellVal->val);
×
1920
  } else {
1921
    valueSetDatum(&pColVal->value, pCol->type, pCellVal->val, tDataTypes[pCol->type].bytes);
×
1922
  }
1923

1924
  pColVal->flag = CV_FLAG_VALUE;
×
1925
  return TSDB_CODE_SUCCESS;
×
1926
}
1927

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

1942
static int32_t vnodeDecodeCreateTbReq(SSubmitReqConvertCxt *pCxt) {
×
1943
  if (pCxt->msgIter.schemaLen <= 0) {
×
1944
    return TSDB_CODE_SUCCESS;
×
1945
  }
1946

1947
  pCxt->pTbData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
×
1948
  if (NULL == pCxt->pTbData->pCreateTbReq) {
×
1949
    return terrno;
×
1950
  }
1951

1952
  SDecoder decoder = {0};
×
1953
  tDecoderInit(&decoder, (uint8_t *)pCxt->pBlock->data, pCxt->msgIter.schemaLen);
×
1954
  int32_t code = tDecodeSVCreateTbReq(&decoder, pCxt->pTbData->pCreateTbReq);
×
1955
  tDecoderClear(&decoder);
×
1956

1957
  return code;
×
1958
}
1959

1960
static int32_t vnodeSubmitReqConvertToSubmitReq2(SVnode *pVnode, SSubmitReq *pReq, SSubmitReq2 *pReq2) {
×
1961
  pReq2->aSubmitTbData = taosArrayInit(128, sizeof(SSubmitTbData));
×
1962
  if (NULL == pReq2->aSubmitTbData) {
×
1963
    return terrno;
×
1964
  }
1965

1966
  SSubmitReqConvertCxt cxt = {0};
×
1967

1968
  int32_t code = tInitSubmitMsgIter(pReq, &cxt.msgIter);
×
1969
  while (TSDB_CODE_SUCCESS == code) {
×
1970
    code = tGetSubmitMsgNext(&cxt.msgIter, &cxt.pBlock);
×
1971
    if (TSDB_CODE_SUCCESS == code) {
×
1972
      if (NULL == cxt.pBlock) {
×
1973
        break;
×
1974
      }
1975
      code = vnodeResetTableCxt(pVnode->pMeta, &cxt);
×
1976
    }
1977
    if (TSDB_CODE_SUCCESS == code) {
×
1978
      code = tInitSubmitBlkIter(&cxt.msgIter, cxt.pBlock, &cxt.blkIter);
×
1979
    }
1980
    if (TSDB_CODE_SUCCESS == code) {
×
1981
      code = vnodeDecodeCreateTbReq(&cxt);
×
1982
    }
1983
    while (TSDB_CODE_SUCCESS == code && (cxt.pRow = tGetSubmitBlkNext(&cxt.blkIter)) != NULL) {
×
1984
      code = vnodeTSRowConvertToColValArray(&cxt);
×
1985
      if (TSDB_CODE_SUCCESS == code) {
×
1986
        SRow **pNewRow = taosArrayReserve(cxt.pTbData->aRowP, 1);
×
1987
        code = tRowBuild(cxt.pColValues, cxt.pTbSchema, pNewRow);
×
1988
      }
1989
    }
1990
    if (TSDB_CODE_SUCCESS == code) {
×
1991
      code = (NULL == taosArrayPush(pReq2->aSubmitTbData, cxt.pTbData) ? terrno : TSDB_CODE_SUCCESS);
×
1992
    }
1993
    if (TSDB_CODE_SUCCESS == code) {
×
1994
      taosMemoryFreeClear(cxt.pTbData);
×
1995
    }
1996
  }
1997

1998
  vnodeDestroySubmitReqConvertCxt(&cxt);
×
1999
  return code;
×
2000
}
2001

2002
static int32_t vnodeRebuildSubmitReqMsg(SSubmitReq2 *pSubmitReq, void **ppMsg) {
×
2003
  int32_t  code = TSDB_CODE_SUCCESS;
×
2004
  char    *pMsg = NULL;
×
2005
  uint32_t msglen = 0;
×
2006
  tEncodeSize(tEncodeSubmitReq, pSubmitReq, msglen, code);
×
2007
  if (TSDB_CODE_SUCCESS == code) {
×
2008
    pMsg = taosMemoryMalloc(msglen);
×
2009
    if (NULL == pMsg) {
×
2010
      code = terrno;
×
2011
    }
2012
  }
2013
  if (TSDB_CODE_SUCCESS == code) {
×
2014
    SEncoder encoder;
2015
    tEncoderInit(&encoder, (uint8_t *)pMsg, msglen);
×
2016
    code = tEncodeSubmitReq(&encoder, pSubmitReq);
×
2017
    tEncoderClear(&encoder);
×
2018
  }
2019
  if (TSDB_CODE_SUCCESS == code) {
×
2020
    *ppMsg = pMsg;
×
2021
  }
2022
  return code;
×
2023
}
2024

2025
static int32_t vnodeProcessSubmitReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
3,079,895✔
2026
                                     SRpcMsg *pOriginalMsg) {
2027
  int32_t code = 0;
3,079,895✔
2028
  terrno = 0;
3,079,895✔
2029

2030
  SSubmitReq2 *pSubmitReq = &(SSubmitReq2){0};
3,079,993✔
2031
  SSubmitRsp2 *pSubmitRsp = &(SSubmitRsp2){0};
3,079,993✔
2032
  SArray      *newTbUids = NULL;
3,079,993✔
2033
  int32_t      ret;
2034
  SEncoder     ec = {0};
3,079,993✔
2035

2036
  pRsp->code = TSDB_CODE_SUCCESS;
3,079,993✔
2037

2038
  void           *pAllocMsg = NULL;
3,079,993✔
2039
  SSubmitReq2Msg *pMsg = (SSubmitReq2Msg *)pReq;
3,079,993✔
2040
  if (0 == pMsg->version) {
3,079,993!
2041
    code = vnodeSubmitReqConvertToSubmitReq2(pVnode, (SSubmitReq *)pMsg, pSubmitReq);
×
2042
    if (TSDB_CODE_SUCCESS == code) {
×
2043
      code = vnodeRebuildSubmitReqMsg(pSubmitReq, &pReq);
×
2044
    }
2045
    if (TSDB_CODE_SUCCESS == code) {
×
2046
      pAllocMsg = pReq;
×
2047
    }
2048
    if (TSDB_CODE_SUCCESS != code) {
×
2049
      goto _exit;
×
2050
    }
2051
  } else {
2052
    // decode
2053
    pReq = POINTER_SHIFT(pReq, sizeof(SSubmitReq2Msg));
3,079,993✔
2054
    len -= sizeof(SSubmitReq2Msg);
3,079,993✔
2055
    SDecoder dc = {0};
3,079,993✔
2056
    tDecoderInit(&dc, pReq, len);
3,079,993✔
2057
    if (tDecodeSubmitReq(&dc, pSubmitReq, NULL) < 0) {
3,079,960!
2058
      code = TSDB_CODE_INVALID_MSG;
×
2059
      goto _exit;
×
2060
    }
2061
    tDecoderClear(&dc);
3,079,976✔
2062
  }
2063

2064
  // scan
2065
  TSKEY now = taosGetTimestamp(pVnode->config.tsdbCfg.precision);
3,079,968✔
2066
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * pVnode->config.tsdbCfg.keep2;
3,079,984✔
2067
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
3,079,984✔
2068
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
6,856,801✔
2069
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
3,778,259✔
2070

2071
    if (pSubmitTbData->pCreateTbReq && pSubmitTbData->pCreateTbReq->uid == 0) {
3,778,262!
2072
      code = TSDB_CODE_INVALID_MSG;
×
2073
      goto _exit;
×
2074
    }
2075

2076
    if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
3,778,262✔
2077
      if (TARRAY_SIZE(pSubmitTbData->aCol) <= 0) {
13,723!
2078
        code = TSDB_CODE_INVALID_MSG;
×
2079
        goto _exit;
×
2080
      }
2081

2082
      SColData *colDataArr = TARRAY_DATA(pSubmitTbData->aCol);
13,723✔
2083
      SRowKey   lastKey;
2084
      tColDataArrGetRowKey(colDataArr, TARRAY_SIZE(pSubmitTbData->aCol), 0, &lastKey);
13,723✔
2085
      for (int32_t iRow = 1; iRow < colDataArr[0].nVal; iRow++) {
21,332,021✔
2086
        SRowKey key;
2087
        tColDataArrGetRowKey(TARRAY_DATA(pSubmitTbData->aCol), TARRAY_SIZE(pSubmitTbData->aCol), iRow, &key);
21,318,407✔
2088
        if (tRowKeyCompare(&lastKey, &key) >= 0) {
21,315,901!
2089
          code = TSDB_CODE_INVALID_MSG;
×
2090
          vError("vgId:%d %s failed 1 since %s, version:%" PRId64, TD_VID(pVnode), __func__, tstrerror(terrno), ver);
×
2091
          goto _exit;
×
2092
        }
2093
      }
2094
    } else {
2095
      int32_t nRow = TARRAY_SIZE(pSubmitTbData->aRowP);
3,764,539✔
2096
      SRow  **aRow = (SRow **)TARRAY_DATA(pSubmitTbData->aRowP);
3,764,539✔
2097
      SRowKey lastRowKey;
2098
      for (int32_t iRow = 0; iRow < nRow; ++iRow) {
141,654,703✔
2099
#ifndef NO_UNALIGNED_ACCESS
2100
        if (aRow[iRow]->ts < minKey || aRow[iRow]->ts > maxKey) {
137,891,500!
2101
#else
2102
        TSKEY ts = taosGetInt64Aligned(&(aRow[iRow]->ts));
2103
        if (ts < minKey || ts > maxKey) {
2104
#endif
2105
          code = TSDB_CODE_INVALID_MSG;
36,234✔
2106
          vError("vgId:%d %s failed 2 since %s, version:%" PRId64, TD_VID(pVnode), __func__, tstrerror(code), ver);
36,234!
2107
          goto _exit;
×
2108
        }
2109
        if (iRow == 0) {
137,855,266✔
2110
          tRowGetKey(aRow[iRow], &lastRowKey);
7,529,138✔
2111
        } else {
2112
          SRowKey rowKey;
2113
          tRowGetKey(aRow[iRow], &rowKey);
268,181,394✔
2114

2115
          if (tRowKeyCompare(&lastRowKey, &rowKey) >= 0) {
134,090,669!
2116
            code = TSDB_CODE_INVALID_MSG;
×
2117
            vError("vgId:%d %s failed 3 since %s, version:%" PRId64, TD_VID(pVnode), __func__, tstrerror(code), ver);
×
2118
            goto _exit;
×
2119
          }
2120
          lastRowKey = rowKey;
134,121,731✔
2121
        }
2122
      }
2123
    }
2124
  }
2125

2126
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
6,856,941✔
2127
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
3,778,327✔
2128

2129
    if (pSubmitTbData->pCreateTbReq) {
3,778,341✔
2130
      pSubmitTbData->uid = pSubmitTbData->pCreateTbReq->uid;
70,761✔
2131
    } else {
2132
      SMetaInfo info = {0};
3,707,580✔
2133

2134
      code = metaGetInfo(pVnode->pMeta, pSubmitTbData->uid, &info, NULL);
3,707,580✔
2135
      if (code) {
3,707,556✔
2136
        code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
24✔
2137
        vWarn("vgId:%d, table uid:%" PRId64 " not exists", TD_VID(pVnode), pSubmitTbData->uid);
24!
2138
        goto _exit;
×
2139
      }
2140

2141
      if (info.suid != pSubmitTbData->suid) {
3,707,532!
2142
        code = TSDB_CODE_INVALID_MSG;
×
2143
        goto _exit;
×
2144
      }
2145

2146
      if (info.suid) {
3,707,532✔
2147
        if (metaGetInfo(pVnode->pMeta, info.suid, &info, NULL) != 0) {
2,570,375!
2148
          vWarn("vgId:%d, table uid:%" PRId64 " not exists", TD_VID(pVnode), info.suid);
×
2149
        }
2150
      }
2151

2152
      if (pSubmitTbData->sver != info.skmVer) {
3,707,638!
2153
        code = TSDB_CODE_TDB_INVALID_TABLE_SCHEMA_VER;
×
2154
        goto _exit;
×
2155
      }
2156
    }
2157

2158
    if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
3,778,399✔
2159
      int32_t   nColData = TARRAY_SIZE(pSubmitTbData->aCol);
13,725✔
2160
      SColData *aColData = (SColData *)TARRAY_DATA(pSubmitTbData->aCol);
13,725✔
2161

2162
      if (nColData <= 0) {
13,725!
2163
        code = TSDB_CODE_INVALID_MSG;
×
2164
        goto _exit;
×
2165
      }
2166

2167
      if (aColData[0].cid != PRIMARYKEY_TIMESTAMP_COL_ID || aColData[0].type != TSDB_DATA_TYPE_TIMESTAMP ||
13,725!
2168
          aColData[0].nVal <= 0) {
13,725!
2169
        code = TSDB_CODE_INVALID_MSG;
×
2170
        goto _exit;
×
2171
      }
2172

2173
      for (int32_t j = 1; j < nColData; j++) {
41,400✔
2174
        if (aColData[j].nVal != aColData[0].nVal) {
27,675!
2175
          code = TSDB_CODE_INVALID_MSG;
×
2176
          goto _exit;
×
2177
        }
2178
      }
2179
    }
2180
  }
2181

2182
  vGDebug(pOriginalMsg ? &pOriginalMsg->info.traceId : NULL, "vgId:%d, index:%" PRId64 ", submit block, rows:%d",
3,078,614!
2183
          TD_VID(pVnode), ver, (int32_t)taosArrayGetSize(pSubmitReq->aSubmitTbData));
2184

2185
  // loop to handle
2186
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
6,858,362✔
2187
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
3,778,304✔
2188

2189
    // create table
2190
    if (pSubmitTbData->pCreateTbReq) {
3,778,333✔
2191
      // alloc if need
2192
      if (pSubmitRsp->aCreateTbRsp == NULL &&
70,757✔
2193
          (pSubmitRsp->aCreateTbRsp = taosArrayInit(TARRAY_SIZE(pSubmitReq->aSubmitTbData), sizeof(SVCreateTbRsp))) ==
69,857!
2194
              NULL) {
2195
        code = terrno;
×
2196
        goto _exit;
8✔
2197
      }
2198

2199
      SVCreateTbRsp *pCreateTbRsp = taosArrayReserve(pSubmitRsp->aCreateTbRsp, 1);
70,757✔
2200

2201
      // create table
2202
      if (metaCreateTable2(pVnode->pMeta, ver, pSubmitTbData->pCreateTbReq, &pCreateTbRsp->pMeta) == 0) {
70,757✔
2203
        // create table success
2204

2205
        if (newTbUids == NULL &&
134,368!
2206
            (newTbUids = taosArrayInit(TARRAY_SIZE(pSubmitReq->aSubmitTbData), sizeof(int64_t))) == NULL) {
66,761✔
2207
          code = terrno;
×
2208
          goto _exit;
×
2209
        }
2210

2211
        if (taosArrayPush(newTbUids, &pSubmitTbData->uid) == NULL) {
135,214!
2212
          code = terrno;
×
2213
          goto _exit;
×
2214
        }
2215

2216
        if (pCreateTbRsp->pMeta) {
67,607!
2217
          vnodeUpdateMetaRsp(pVnode, pCreateTbRsp->pMeta);
67,607✔
2218
        }
2219
      } else {  // create table failed
2220
        if (terrno != TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
3,150✔
2221
          code = terrno;
8✔
2222
          vError("vgId:%d failed to create table:%s, code:%s", TD_VID(pVnode), pSubmitTbData->pCreateTbReq->name,
8!
2223
                 tstrerror(terrno));
2224
          goto _exit;
8✔
2225
        }
2226
        terrno = 0;
3,142✔
2227
        pSubmitTbData->uid = pSubmitTbData->pCreateTbReq->uid;  // update uid if table exist for using below
3,142✔
2228
      }
2229
    }
2230

2231
    // insert data
2232
    int32_t affectedRows;
2233
    code = tsdbInsertTableData(pVnode->pTsdb, ver, pSubmitTbData, &affectedRows);
3,778,325✔
2234
    if (code) goto _exit;
3,778,293!
2235

2236
    code = metaUpdateChangeTimeWithLock(pVnode->pMeta, pSubmitTbData->uid, pSubmitTbData->ctimeMs);
3,778,293✔
2237
    if (code) goto _exit;
3,778,326!
2238

2239
    pSubmitRsp->affectedRows += affectedRows;
3,778,326✔
2240
  }
2241

2242
  // update the affected table uid list
2243
  if (taosArrayGetSize(newTbUids) > 0) {
3,080,058✔
2244
    vDebug("vgId:%d, add %d table into query table list in handling submit", TD_VID(pVnode),
66,759✔
2245
           (int32_t)taosArrayGetSize(newTbUids));
2246
    if (tqUpdateTbUidList(pVnode->pTq, newTbUids, true) != 0) {
66,761!
2247
      vError("vgId:%d, failed to update tbUid list", TD_VID(pVnode));
×
2248
    }
2249
  }
2250

2251
_exit:
3,080,031✔
2252
  // message
2253
  pRsp->code = code;
3,080,039✔
2254
  tEncodeSize(tEncodeSSubmitRsp2, pSubmitRsp, pRsp->contLen, ret);
3,080,039!
2255
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
3,079,969✔
2256
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
3,080,020✔
2257
  if (tEncodeSSubmitRsp2(&ec, pSubmitRsp) < 0) {
3,079,998!
2258
    vError("vgId:%d, failed to encode submit response", TD_VID(pVnode));
×
2259
  }
2260
  tEncoderClear(&ec);
3,080,036✔
2261

2262
  // update statistics
2263
  (void)atomic_add_fetch_64(&pVnode->statis.nInsert, pSubmitRsp->affectedRows);
3,080,062✔
2264
  (void)atomic_add_fetch_64(&pVnode->statis.nInsertSuccess, pSubmitRsp->affectedRows);
3,080,072✔
2265
  (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsert, 1);
3,080,059✔
2266

2267
  // update metrics
2268
  METRICS_UPDATE(pVnode->writeMetrics.total_requests, METRIC_LEVEL_LOW, 1);
3,080,063!
2269
  METRICS_UPDATE(pVnode->writeMetrics.total_rows, METRIC_LEVEL_HIGH, pSubmitRsp->affectedRows);
3,080,063!
2270
  METRICS_UPDATE(pVnode->writeMetrics.total_bytes, METRIC_LEVEL_LOW, pMsg->header.contLen);
3,080,063!
2271

2272
  if (tsEnableMonitor && tsMonitorFqdn[0] != 0 && tsMonitorPort != 0 && pSubmitRsp->affectedRows > 0 &&
3,080,063!
2273
      strlen(pOriginalMsg->info.conn.user) > 0 && tsInsertCounter != NULL) {
100!
2274
    const char *sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT_AFFECTED_ROWS,
100✔
2275
                                   pVnode->monitor.strClusterId,
100✔
2276
                                   pVnode->monitor.strDnodeId,
100✔
2277
                                   tsLocalEp,
2278
                                   pVnode->monitor.strVgId,
100✔
2279
                                   pOriginalMsg->info.conn.user,
100✔
2280
                                   "Success"};
2281
    int         tv = taos_counter_add(tsInsertCounter, pSubmitRsp->affectedRows, sample_labels);
100✔
2282
  }
2283

2284
  if (code == 0) {
3,080,063✔
2285
    (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
3,079,974✔
2286
    code = tdProcessRSmaSubmit(pVnode->pSma, ver, pSubmitReq, pReq, len);
3,080,052✔
2287
  }
2288
  /*
2289
  if (code == 0) {
2290
    atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
2291
    code = tdProcessRSmaSubmit(pVnode->pSma, ver, pSubmitReq, pReq, len);
2292

2293
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT, pVnode->monitor.strClusterId,
2294
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2295
                                          pOriginalMsg->info.conn.user, "Success"};
2296
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2297
  }
2298
  else{
2299
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT, pVnode->monitor.strClusterId,
2300
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2301
                                        pOriginalMsg->info.conn.user, "Failed"};
2302
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2303
  }
2304
  */
2305

2306
  // clear
2307
  taosArrayDestroy(newTbUids);
3,080,089✔
2308
  tDestroySubmitReq(pSubmitReq, 0 == pMsg->version ? TSDB_MSG_FLG_CMPT : TSDB_MSG_FLG_DECODE);
3,079,971!
2309
  tDestroySSubmitRsp2(pSubmitRsp, TSDB_MSG_FLG_ENCODE);
3,080,051✔
2310

2311
  if (code) terrno = code;
3,080,004✔
2312

2313
  taosMemoryFree(pAllocMsg);
3,080,004!
2314

2315
  return code;
3,080,008✔
2316
}
2317

2318
static int32_t vnodeProcessCreateTSmaReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
2319
#ifdef USE_TSMA
2320
  SVCreateTSmaReq req = {0};
×
2321
  SDecoder        coder = {0};
×
2322

2323
  if (pRsp) {
×
2324
    pRsp->msgType = TDMT_VND_CREATE_SMA_RSP;
×
2325
    pRsp->code = TSDB_CODE_SUCCESS;
×
2326
    pRsp->pCont = NULL;
×
2327
    pRsp->contLen = 0;
×
2328
  }
2329

2330
  // decode and process req
2331
  tDecoderInit(&coder, pReq, len);
×
2332

2333
  if (tDecodeSVCreateTSmaReq(&coder, &req) < 0) {
×
2334
    terrno = TSDB_CODE_MSG_DECODE_ERROR;
×
2335
    if (pRsp) pRsp->code = terrno;
×
2336
    goto _err;
×
2337
  }
2338

2339
  if (tdProcessTSmaCreate(pVnode->pSma, ver, (const char *)&req) < 0) {
×
2340
    if (pRsp) pRsp->code = terrno;
×
2341
    goto _err;
×
2342
  }
2343

2344
  tDecoderClear(&coder);
×
2345
  vDebug("vgId:%d, success to create tsma %s:%" PRIi64 " version %" PRIi64 " for table %" PRIi64, TD_VID(pVnode),
×
2346
         req.indexName, req.indexUid, ver, req.tableUid);
2347
  return 0;
×
2348

2349
_err:
×
2350
  tDecoderClear(&coder);
×
2351
  vError("vgId:%d, failed to create tsma %s:%" PRIi64 " version %" PRIi64 "for table %" PRIi64 " since %s",
×
2352
         TD_VID(pVnode), req.indexName, req.indexUid, ver, req.tableUid, terrstr());
2353
  return terrno;
×
2354
#else
2355
  return TSDB_CODE_INTERNAL_ERROR;
2356
#endif
2357
}
2358

2359
/**
2360
 * @brief specific for smaDstVnode
2361
 *
2362
 * @param pVnode
2363
 * @param pCont
2364
 * @param contLen
2365
 * @return int32_t
2366
 */
2367
int32_t vnodeProcessCreateTSma(SVnode *pVnode, void *pCont, uint32_t contLen) {
×
2368
  return vnodeProcessCreateTSmaReq(pVnode, 1, pCont, contLen, NULL);
×
2369
}
2370

2371
static int32_t vnodeConsolidateAlterHashRange(SVnode *pVnode, int64_t ver) {
32✔
2372
  int32_t code = TSDB_CODE_SUCCESS;
32✔
2373

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

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

2380
  return code;
32✔
2381
}
2382

2383
static int32_t vnodeProcessAlterConfirmReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
15,036✔
2384
  vInfo("vgId:%d, vnode handle msgType:alter-confirm, alter confirm msg is processed", TD_VID(pVnode));
15,036!
2385
  int32_t code = TSDB_CODE_SUCCESS;
15,036✔
2386
  if (!pVnode->config.hashChange) {
15,036✔
2387
    goto _exit;
15,004✔
2388
  }
2389

2390
  code = vnodeConsolidateAlterHashRange(pVnode, ver);
32✔
2391
  if (code < 0) {
32!
2392
    vError("vgId:%d, failed to consolidate alter hashrange since %s. version:%" PRId64, TD_VID(pVnode), terrstr(), ver);
×
2393
    goto _exit;
×
2394
  }
2395
  pVnode->config.hashChange = false;
32✔
2396

2397
_exit:
15,036✔
2398
  pRsp->msgType = TDMT_VND_ALTER_CONFIRM_RSP;
15,036✔
2399
  pRsp->code = code;
15,036✔
2400
  pRsp->pCont = NULL;
15,036✔
2401
  pRsp->contLen = 0;
15,036✔
2402

2403
  return code;
15,036✔
2404
}
2405

2406
extern int32_t tsdbDisableAndCancelAllBgTask(STsdb *pTsdb);
2407
extern void    tsdbEnableBgTask(STsdb *pTsdb);
2408

2409
static int32_t vnodeProcessAlterConfigReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
819✔
2410
  bool walChanged = false;
819✔
2411
  bool tsdbChanged = false;
819✔
2412

2413
  SAlterVnodeConfigReq req = {0};
819✔
2414
  if (tDeserializeSAlterVnodeConfigReq(pReq, len, &req) != 0) {
819!
2415
    terrno = TSDB_CODE_INVALID_MSG;
×
2416
    return TSDB_CODE_INVALID_MSG;
×
2417
  }
2418

2419
  vInfo("vgId:%d, start to alter vnode config, page:%d pageSize:%d buffer:%d szPage:%d szBuf:%" PRIu64
823✔
2420
        " cacheLast:%d cacheLastSize:%d days:%d keep0:%d keep1:%d keep2:%d keepTimeOffset:%d ssKeepLocal:%d "
2421
        "ssCompact:%d fsync:%d level:%d "
2422
        "walRetentionPeriod:%d walRetentionSize:%d",
2423
        TD_VID(pVnode), req.pages, req.pageSize, req.buffer, req.pageSize * 1024, (uint64_t)req.buffer * 1024 * 1024,
2424
        req.cacheLast, req.cacheLastSize, req.daysPerFile, req.daysToKeep0, req.daysToKeep1, req.daysToKeep2,
2425
        req.keepTimeOffset, req.ssKeepLocal, req.ssCompact, req.walFsyncPeriod, req.walLevel, req.walRetentionPeriod,
2426
        req.walRetentionSize);
2427

2428
  if (pVnode->config.cacheLastSize != req.cacheLastSize) {
827✔
2429
    pVnode->config.cacheLastSize = req.cacheLastSize;
165✔
2430
    tsdbCacheSetCapacity(pVnode, (size_t)pVnode->config.cacheLastSize * 1024 * 1024);
165✔
2431
  }
2432

2433
  if (pVnode->config.szBuf != req.buffer * 1024LL * 1024LL) {
827!
2434
    vInfo("vgId:%d, vnode buffer is changed from %" PRId64 " to %" PRId64, TD_VID(pVnode), pVnode->config.szBuf,
×
2435
          (uint64_t)(req.buffer * 1024LL * 1024LL));
2436
    pVnode->config.szBuf = req.buffer * 1024LL * 1024LL;
×
2437
  }
2438

2439
  if (pVnode->config.szCache != req.pages) {
827!
2440
    if ((terrno = metaAlterCache(pVnode->pMeta, req.pages)) < 0) {
×
2441
      vError("vgId:%d, failed to change vnode pages from %d to %d failed since %s", TD_VID(pVnode),
×
2442
             pVnode->config.szCache, req.pages, tstrerror(terrno));
2443
      return terrno;
×
2444
    } else {
2445
      vInfo("vgId:%d, vnode pages is changed from %d to %d", TD_VID(pVnode), pVnode->config.szCache, req.pages);
×
2446
      pVnode->config.szCache = req.pages;
×
2447
    }
2448
  }
2449

2450
  if (pVnode->config.cacheLast != req.cacheLast) {
827✔
2451
    pVnode->config.cacheLast = req.cacheLast;
441✔
2452
  }
2453

2454
  if (pVnode->config.walCfg.fsyncPeriod != req.walFsyncPeriod) {
827✔
2455
    pVnode->config.walCfg.fsyncPeriod = req.walFsyncPeriod;
108✔
2456
    walChanged = true;
108✔
2457
  }
2458

2459
  if (pVnode->config.walCfg.level != req.walLevel) {
827✔
2460
    if (pVnode->config.walCfg.level == 0) {
72!
2461
      pVnode->config.walCfg.clearFiles = 1;
×
2462
    }
2463
    pVnode->config.walCfg.level = req.walLevel;
72✔
2464
    walChanged = true;
72✔
2465
  }
2466

2467
  if (pVnode->config.walCfg.retentionPeriod != req.walRetentionPeriod) {
827✔
2468
    pVnode->config.walCfg.retentionPeriod = req.walRetentionPeriod;
132✔
2469
    walChanged = true;
132✔
2470
  }
2471

2472
  if (pVnode->config.walCfg.retentionSize != req.walRetentionSize) {
827!
2473
    pVnode->config.walCfg.retentionSize = req.walRetentionSize;
×
2474
    walChanged = true;
×
2475
  }
2476

2477
  if (pVnode->config.tsdbCfg.keep0 != req.daysToKeep0) {
827✔
2478
    pVnode->config.tsdbCfg.keep0 = req.daysToKeep0;
64✔
2479
    if (!VND_IS_RSMA(pVnode)) {
64!
2480
      tsdbChanged = true;
64✔
2481
    }
2482
  }
2483

2484
  if (pVnode->config.tsdbCfg.keep1 != req.daysToKeep1) {
827✔
2485
    pVnode->config.tsdbCfg.keep1 = req.daysToKeep1;
64✔
2486
    if (!VND_IS_RSMA(pVnode)) {
64!
2487
      tsdbChanged = true;
64✔
2488
    }
2489
  }
2490

2491
  if (pVnode->config.tsdbCfg.keep2 != req.daysToKeep2) {
827✔
2492
    pVnode->config.tsdbCfg.keep2 = req.daysToKeep2;
66✔
2493
    if (!VND_IS_RSMA(pVnode)) {
66!
2494
      tsdbChanged = true;
66✔
2495
    }
2496
  }
2497

2498
  if (pVnode->config.tsdbCfg.keepTimeOffset != req.keepTimeOffset) {
827✔
2499
    pVnode->config.tsdbCfg.keepTimeOffset = req.keepTimeOffset;
2✔
2500
    if (!VND_IS_RSMA(pVnode)) {
2!
2501
      tsdbChanged = true;
2✔
2502
    }
2503
  }
2504

2505
  if (req.sttTrigger != -1 && req.sttTrigger != pVnode->config.sttTrigger) {
827!
2506
    if (req.sttTrigger > 1 && pVnode->config.sttTrigger > 1) {
×
2507
      pVnode->config.sttTrigger = req.sttTrigger;
×
2508
    } else {
2509
      vnodeAWait(&pVnode->commitTask);
×
2510

2511
      int32_t ret = tsdbDisableAndCancelAllBgTask(pVnode->pTsdb);
×
2512
      if (ret != 0) {
×
2513
        vError("vgId:%d, failed to disable bg task since %s", TD_VID(pVnode), tstrerror(ERRNO));
×
2514
      }
2515

2516
      pVnode->config.sttTrigger = req.sttTrigger;
×
2517
      tsdbEnableBgTask(pVnode->pTsdb);
×
2518
    }
2519
  }
2520

2521
  if (req.minRows != -1 && req.minRows != pVnode->config.tsdbCfg.minRows) {
827!
2522
    pVnode->config.tsdbCfg.minRows = req.minRows;
×
2523
  }
2524

2525
  if (req.ssKeepLocal != -1 && req.ssKeepLocal != pVnode->config.ssKeepLocal) {
827!
2526
    pVnode->config.ssKeepLocal = req.ssKeepLocal;
×
2527
  }
2528
  if (req.ssCompact != -1 && req.ssCompact != pVnode->config.ssCompact) {
827!
2529
    pVnode->config.ssCompact = req.ssCompact;
×
2530
  }
2531

2532
  if (walChanged) {
827✔
2533
    if (walAlter(pVnode->pWal, &pVnode->config.walCfg) != 0) {
312!
2534
      vError("vgId:%d, failed to alter wal config since %s", TD_VID(pVnode), tstrerror(ERRNO));
×
2535
    }
2536
  }
2537

2538
  if (tsdbChanged) {
823✔
2539
    tsdbSetKeepCfg(pVnode->pTsdb, &pVnode->config.tsdbCfg);
68✔
2540
  }
2541

2542
  return 0;
823✔
2543
}
2544

2545
static int32_t vnodeProcessBatchDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
965✔
2546
  SBatchDeleteReq deleteReq;
2547
  SDecoder        decoder;
2548
  tDecoderInit(&decoder, pReq, len);
965✔
2549
  if (tDecodeSBatchDeleteReq(&decoder, &deleteReq) < 0) {
965!
2550
    tDecoderClear(&decoder);
×
2551
    return terrno = TSDB_CODE_INVALID_MSG;
×
2552
  }
2553

2554
  SMetaReader mr = {0};
965✔
2555
  metaReaderDoInit(&mr, pVnode->pMeta, META_READER_NOLOCK);
965✔
2556
  STsdb *pTsdb = pVnode->pTsdb;
965✔
2557

2558
  if (deleteReq.level) {
965✔
2559
    pTsdb = deleteReq.level == 1 ? VND_RSMA1(pVnode) : VND_RSMA2(pVnode);
16✔
2560
  }
2561

2562
  int32_t sz = taosArrayGetSize(deleteReq.deleteReqs);
965✔
2563
  for (int32_t i = 0; i < sz; i++) {
2,756✔
2564
    SSingleDeleteReq *pOneReq = taosArrayGet(deleteReq.deleteReqs, i);
1,791✔
2565
    char             *name = pOneReq->tbname;
1,791✔
2566
    if (metaGetTableEntryByName(&mr, name) < 0) {
1,791✔
2567
      vDebug("vgId:%d, stream delete msg, skip since no table: %s", pVnode->config.vgId, name);
637✔
2568
      continue;
637✔
2569
    }
2570

2571
    int64_t uid = mr.me.uid;
1,154✔
2572

2573
    int32_t code = tsdbDeleteTableData(pTsdb, ver, deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
1,154✔
2574
    if (code < 0) {
1,154!
2575
      terrno = code;
×
2576
      vError("vgId:%d, delete error since %s, suid:%" PRId64 ", uid:%" PRId64 ", start ts:%" PRId64 ", end ts:%" PRId64,
×
2577
             TD_VID(pVnode), terrstr(), deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
2578
    }
2579

2580
    if (deleteReq.level == 0) {
1,154✔
2581
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, deleteReq.ctimeMs);
538✔
2582
      if (code < 0) {
538!
2583
        terrno = code;
×
2584
        vError("vgId:%d, update change time error since %s, suid:%" PRId64 ", uid:%" PRId64 ", start ts:%" PRId64
×
2585
               ", end ts:%" PRId64,
2586
               TD_VID(pVnode), terrstr(), deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
2587
      }
2588
    }
2589
    tDecoderClear(&mr.coder);
1,154✔
2590
  }
2591
  metaReaderClear(&mr);
965✔
2592
  taosArrayDestroy(deleteReq.deleteReqs);
965✔
2593
  return 0;
965✔
2594
}
2595

2596
static int32_t vnodeProcessDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
8,353✔
2597
                                     SRpcMsg *pOriginalMsg) {
2598
  int32_t     code = 0;
8,353✔
2599
  SDecoder   *pCoder = &(SDecoder){0};
8,353✔
2600
  SDeleteRes *pRes = &(SDeleteRes){0};
8,353✔
2601

2602
  pRsp->msgType = TDMT_VND_DELETE_RSP;
8,353✔
2603
  pRsp->pCont = NULL;
8,353✔
2604
  pRsp->contLen = 0;
8,353✔
2605
  pRsp->code = TSDB_CODE_SUCCESS;
8,353✔
2606

2607
  pRes->uidList = taosArrayInit(0, sizeof(tb_uid_t));
8,353✔
2608
  if (pRes->uidList == NULL) {
8,353!
2609
    code = terrno;
×
2610
    goto _err;
×
2611
  }
2612

2613
  tDecoderInit(pCoder, pReq, len);
8,353✔
2614
  code = tDecodeDeleteRes(pCoder, pRes);
8,353✔
2615
  if (code) goto _err;
8,353!
2616

2617
  if (pRes->affectedRows > 0) {
8,353✔
2618
    for (int32_t iUid = 0; iUid < taosArrayGetSize(pRes->uidList); iUid++) {
14,457✔
2619
      uint64_t uid = *(uint64_t *)taosArrayGet(pRes->uidList, iUid);
7,724✔
2620
      code = tsdbDeleteTableData(pVnode->pTsdb, ver, pRes->suid, uid, pRes->skey, pRes->ekey);
7,724✔
2621
      if (code) goto _err;
7,724!
2622
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, pRes->ctimeMs);
7,724✔
2623
      if (code) goto _err;
7,724!
2624
    }
2625
  }
2626

2627
  code = tdProcessRSmaDelete(pVnode->pSma, ver, pRes, pReq, len);
8,353✔
2628

2629
  tDecoderClear(pCoder);
8,353✔
2630
  taosArrayDestroy(pRes->uidList);
8,353✔
2631

2632
  SVDeleteRsp rsp = {.affectedRows = pRes->affectedRows};
8,353✔
2633
  int32_t     ret = 0;
8,353✔
2634
  tEncodeSize(tEncodeSVDeleteRsp, &rsp, pRsp->contLen, ret);
8,353!
2635
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
8,353✔
2636
  SEncoder ec = {0};
8,353✔
2637
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
8,353✔
2638
  code = tEncodeSVDeleteRsp(&ec, &rsp);
8,353✔
2639
  if (code) goto _err;
8,353!
2640
  tEncoderClear(&ec);
8,353✔
2641
  return code;
8,353✔
2642

2643
_err:
×
2644
  /*
2645
  if(code == TSDB_CODE_SUCCESS){
2646
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_DELETE, pVnode->monitor.strClusterId,
2647
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2648
                                        pOriginalMsg->info.conn.user, "Success"};
2649
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2650
  }
2651
  else{
2652
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_DELETE, pVnode->monitor.strClusterId,
2653
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2654
                                        pOriginalMsg->info.conn.user, "Failed"};
2655
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2656
  }
2657
  */
2658

2659
  return code;
×
2660
}
2661
static int32_t vnodeProcessCreateIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
26✔
2662
  SVCreateStbReq req = {0};
26✔
2663
  SDecoder       dc = {0};
26✔
2664
  int32_t        code = 0;
26✔
2665

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

2671
  tDecoderInit(&dc, pReq, len);
26✔
2672
  // decode req
2673
  if (tDecodeSVCreateStbReq(&dc, &req) < 0) {
26!
2674
    tDecoderClear(&dc);
×
2675
    return terrno = TSDB_CODE_INVALID_MSG;
×
2676
  }
2677

2678
  code = metaAddIndexToSuperTable(pVnode->pMeta, ver, &req);
26✔
2679
  if (code) {
26!
2680
    pRsp->code = code;
×
2681
    goto _err;
×
2682
  }
2683
  tDecoderClear(&dc);
26✔
2684
  return 0;
26✔
2685

2686
_err:
×
2687
  tDecoderClear(&dc);
×
2688
  return code;
×
2689
}
2690
static int32_t vnodeProcessDropIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
16✔
2691
  SDropIndexReq req = {0};
16✔
2692
  int32_t       code = 0;
16✔
2693
  pRsp->msgType = TDMT_VND_DROP_INDEX_RSP;
16✔
2694
  pRsp->code = TSDB_CODE_SUCCESS;
16✔
2695
  pRsp->pCont = NULL;
16✔
2696
  pRsp->contLen = 0;
16✔
2697

2698
  if ((code = tDeserializeSDropIdxReq(pReq, len, &req))) {
16!
2699
    pRsp->code = code;
×
2700
    return code;
×
2701
  }
2702

2703
  code = metaDropIndexFromSuperTable(pVnode->pMeta, ver, &req);
16✔
2704
  if (code) {
16!
2705
    pRsp->code = code;
×
2706
    return code;
×
2707
  }
2708
  return TSDB_CODE_SUCCESS;
16✔
2709
}
2710

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

2713
static int32_t vnodeProcessCompactVnodeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
194✔
2714
  if (!pVnode->restored) {
194✔
2715
    vInfo("vgId:%d, ignore compact req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
174!
2716
    return 0;
174✔
2717
  }
2718
  return vnodeAsyncCompact(pVnode, ver, pReq, len, pRsp);
20✔
2719
}
2720

2721
static int32_t vnodeProcessConfigChangeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
2722
  if (syncCheckMember(pVnode->sync) != 0) {
×
2723
    vError("vgId:%d, failed to check member", TD_VID(pVnode));
×
2724
  }
2725

2726
  pRsp->msgType = TDMT_SYNC_CONFIG_CHANGE_RSP;
×
2727
  pRsp->code = TSDB_CODE_SUCCESS;
×
2728
  pRsp->pCont = NULL;
×
2729
  pRsp->contLen = 0;
×
2730

2731
  return 0;
×
2732
}
2733

2734
static int32_t vnodeCheckToken(SVnode *pVnode, char *member0Token, char *member1Token) {
48✔
2735
  SSyncState syncState = syncGetState(pVnode->sync);
48✔
2736
  if (syncState.state != TAOS_SYNC_STATE_LEADER) {
48✔
2737
    return terrno = TSDB_CODE_SYN_NOT_LEADER;
14✔
2738
  }
2739

2740
  char token[TSDB_ARB_TOKEN_SIZE] = {0};
34✔
2741
  if (vnodeGetArbToken(pVnode, token) != 0) {
34!
2742
    return terrno = TSDB_CODE_NOT_FOUND;
×
2743
  }
2744

2745
  if (strncmp(token, member0Token, TSDB_ARB_TOKEN_SIZE) != 0 &&
34✔
2746
      strncmp(token, member1Token, TSDB_ARB_TOKEN_SIZE) != 0) {
28✔
2747
    return terrno = TSDB_CODE_MND_ARB_TOKEN_MISMATCH;
20✔
2748
  }
2749

2750
  terrno = TSDB_CODE_SUCCESS;
14✔
2751
  return 0;
14✔
2752
}
2753

2754
static int32_t vnodeCheckSyncd(SVnode *pVnode, char *member0Token, char *member1Token) {
14✔
2755
  int32_t code = vnodeCheckToken(pVnode, member0Token, member1Token);
14✔
2756
  if (code != 0) {
14✔
2757
    return code;
7✔
2758
  }
2759

2760
  return syncCheckSynced(pVnode->sync);
7✔
2761
}
2762

2763
static int32_t vnodeProcessArbCheckSyncReq(SVnode *pVnode, void *pReq, int32_t len, SRpcMsg *pRsp) {
14✔
2764
  int32_t code = 0;
14✔
2765

2766
  SVArbCheckSyncReq syncReq = {0};
14✔
2767

2768
  code = tDeserializeSVArbCheckSyncReq(pReq, len, &syncReq);
14✔
2769
  if (code) {
14!
2770
    return terrno = code;
×
2771
  }
2772

2773
  pRsp->msgType = TDMT_VND_ARB_CHECK_SYNC_RSP;
14✔
2774
  pRsp->code = TSDB_CODE_SUCCESS;
14✔
2775
  pRsp->pCont = NULL;
14✔
2776
  pRsp->contLen = 0;
14✔
2777

2778
  SVArbCheckSyncRsp syncRsp = {0};
14✔
2779
  syncRsp.arbToken = syncReq.arbToken;
14✔
2780
  syncRsp.member0Token = syncReq.member0Token;
14✔
2781
  syncRsp.member1Token = syncReq.member1Token;
14✔
2782
  syncRsp.vgId = TD_VID(pVnode);
14✔
2783

2784
  if (vnodeCheckSyncd(pVnode, syncReq.member0Token, syncReq.member1Token) != 0) {
14✔
2785
    vError("vgId:%d, failed to check assigned log syncd", TD_VID(pVnode));
7!
2786
  }
2787
  syncRsp.errCode = terrno;
14✔
2788

2789
  if (vnodeUpdateArbTerm(pVnode, syncReq.arbTerm) != 0) {
14!
2790
    vError("vgId:%d, failed to update arb term", TD_VID(pVnode));
×
2791
    code = -1;
×
2792
    goto _OVER;
×
2793
  }
2794

2795
  int32_t contLen = tSerializeSVArbCheckSyncRsp(NULL, 0, &syncRsp);
14✔
2796
  if (contLen <= 0) {
14!
2797
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2798
    code = -1;
×
2799
    goto _OVER;
×
2800
  }
2801
  void *pHead = rpcMallocCont(contLen);
14✔
2802
  if (!pHead) {
14!
2803
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2804
    code = -1;
×
2805
    goto _OVER;
×
2806
  }
2807

2808
  if (tSerializeSVArbCheckSyncRsp(pHead, contLen, &syncRsp) <= 0) {
14!
2809
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2810
    rpcFreeCont(pHead);
×
2811
    code = -1;
×
2812
    goto _OVER;
×
2813
  }
2814

2815
  pRsp->pCont = pHead;
14✔
2816
  pRsp->contLen = contLen;
14✔
2817

2818
  terrno = TSDB_CODE_SUCCESS;
14✔
2819

2820
_OVER:
14✔
2821
  tFreeSVArbCheckSyncReq(&syncReq);
14✔
2822
  return code;
14✔
2823
}
2824

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