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

taosdata / TDengine / #4473

08 Jul 2025 09:38AM UTC coverage: 62.922% (+0.7%) from 62.22%
#4473

push

travis-ci

web-flow
Merge pull request #31712 from taosdata/merge/mainto3.0

merge: from main to 3.0 branch

158525 of 321496 branches covered (49.31%)

Branch coverage included in aggregate %.

56 of 60 new or added lines in 13 files covered. (93.33%)

1333 existing lines in 67 files now uncovered.

245526 of 320647 relevant lines covered (76.57%)

17689640.25 hits per line

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

59.84
/source/dnode/vnode/src/vnd/vnodeSvr.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#include "audit.h"
17
#include "cos.h"
18
#include "monitor.h"
19
#include "tencode.h"
20
#include "tglobal.h"
21
#include "tmsg.h"
22
#include "tstrbuild.h"
23
#include "vnd.h"
24
#include "vnode.h"
25
#include "vnodeInt.h"
26

27
extern taos_counter_t *tsInsertCounter;
28

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

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

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

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

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

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

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

85
  // uid
86
  int64_t uid = metaGetTableEntryUidByName(pVnode->pMeta, name);
187,679✔
87
  if (uid == 0) {
187,660✔
88
    uid = tGenIdPI64();
179,968✔
89
  }
90
  taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), uid);
187,679✔
91

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

95
  tEndDecode(pCoder);
187,679✔
96

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

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

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

120
  if (tDecodeI32v(&dc, &nReqs) < 0) {
80,629!
121
    code = TSDB_CODE_INVALID_MSG;
×
122
    TSDB_CHECK_CODE(code, lino, _exit);
×
123
  }
124
  for (int32_t iReq = 0; iReq < nReqs; iReq++) {
185,895✔
125
    code = vnodePreprocessCreateTableReq(pVnode, &dc, btime, NULL);
105,288✔
126
    TSDB_CHECK_CODE(code, lino, _exit);
105,266!
127
  }
128

129
  tEndDecode(&dc);
80,607✔
130

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

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

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

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

157
  code = 0;
6,361✔
158

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

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

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

177
  SArray *tbUids = NULL;
67,344✔
178
  int64_t timestampMs = 0;
67,344✔
179

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

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

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

200
    ttlReq.nUids = taosArrayGetSize(tbUids);
67,321✔
201
    ttlReq.pTbUids = tbUids;
67,150✔
202
  }
203

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

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

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

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

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

230
  code = 0;
30✔
231

232
_exit:
67,167✔
233
  taosArrayDestroy(tbUids);
67,167✔
234

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

242
  return code;
67,279✔
243
}
244

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

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

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

264
  int64_t uid;
265
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
11,324,351✔
266
    code = vnodePreprocessCreateTableReq(pVnode, pCoder, btimeMs, &uid);
82,352✔
267
    TSDB_CHECK_CODE(code, lino, _exit);
82,380!
268
  }
269

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

276
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
11,324,316✔
277
    taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), uid);
82,375✔
278
    pCoder->pos += sizeof(int64_t);
82,375✔
279
  } else {
280
    if (tDecodeI64(pCoder, &submitTbData.uid) < 0) {
11,241,979!
281
      code = TSDB_CODE_INVALID_MSG;
×
282
      TSDB_CHECK_CODE(code, lino, _exit);
×
283
    }
284
  }
285

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

291
  // scan and check
292
  TSKEY now = btimeMs;
11,324,284✔
293
  if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_MICRO) {
11,324,284✔
294
    now *= 1000;
23,046✔
295
  } else if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_NANO) {
11,301,238✔
296
    now *= 1000000;
4,409✔
297
  }
298

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

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

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

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

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

339
    for (uint64_t i = 1; i < nColData; i++) {
30,602✔
340
      code = tDecodeColData(version, pCoder, &colData);
20,472✔
341
      if (code) {
20,472!
342
        code = TSDB_CODE_INVALID_MSG;
×
343
        TSDB_CHECK_CODE(code, lino, _exit);
×
344
      }
345
    }
346
  } else {
347
    uint64_t nRow;
348
    if (tDecodeU64v(pCoder, &nRow) < 0) {
11,314,159!
349
      code = TSDB_CODE_INVALID_MSG;
×
350
      TSDB_CHECK_CODE(code, lino, _exit);
520!
351
    }
352

353
    for (int32_t iRow = 0; iRow < nRow; ++iRow) {
674,154,689✔
354
      SRow *pRow = (SRow *)(pCoder->data + pCoder->pos);
662,841,050✔
355
      pCoder->pos += pRow->len;
662,841,050✔
356
#ifndef NO_UNALIGNED_ACCESS
357
      if (pRow->ts < minKey || pRow->ts > maxKey) {
662,841,050✔
358
#else
359
      TSKEY ts = taosGetInt64Aligned(&pRow->ts);
360
      if (ts < minKey || ts > maxKey) {
361
#endif
362
        code = TSDB_CODE_TDB_TIMESTAMP_OUT_OF_RANGE;
1,375✔
363
        TSDB_CHECK_CODE(code, lino, _exit);
1,375✔
364
      }
365

366
      // Check pRow->sver
367
      if (pRow->sver != submitTbData.sver) {
662,840,530!
368
        code = TSDB_CODE_INVALID_DATA_FMT;
×
369
        TSDB_CHECK_CODE(code, lino, _exit);
×
370
      }
371
    }
372
  }
373

374
  if (!tDecodeIsEnd(pCoder)) {
11,323,769!
375
    taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), ctimeMs);
11,323,824✔
376
    pCoder->pos += sizeof(int64_t);
11,323,824✔
377
  }
378

379
  tEndDecode(pCoder);
11,323,769✔
380

381
_exit:
11,324,229✔
382
  if (code) {
11,324,229✔
383
    vError("vgId:%d, %s:%d failed to vnodePreProcessSubmitTbData submit request since %s", TD_VID(pVnode), __func__,
521!
384
           lino, tstrerror(code));
385
  }
386
  return code;
11,324,275✔
387
}
388
static int32_t vnodePreProcessSubmitMsg(SVnode *pVnode, SRpcMsg *pMsg) {
10,603,133✔
389
  int32_t code = 0;
10,603,133✔
390
  int32_t lino = 0;
10,603,133✔
391

392
  if (tsBypassFlag & TSDB_BYPASS_RA_RPC_RECV_SUBMIT) {
10,603,133!
393
    return TSDB_CODE_MSG_PREPROCESSED;
×
394
  }
395

396
  SDecoder *pCoder = &(SDecoder){0};
10,603,133✔
397

398
  if (taosHton64(((SSubmitReq2Msg *)pMsg->pCont)->version) != 1) {
10,603,133!
399
    code = TSDB_CODE_INVALID_MSG;
×
400
    TSDB_CHECK_CODE(code, lino, _exit);
×
401
  }
402

403
  tDecoderInit(pCoder, (uint8_t *)pMsg->pCont + sizeof(SSubmitReq2Msg), pMsg->contLen - sizeof(SSubmitReq2Msg));
10,603,210✔
404

405
  if (tStartDecode(pCoder) < 0) {
10,603,155!
406
    code = TSDB_CODE_INVALID_MSG;
×
407
    TSDB_CHECK_CODE(code, lino, _exit);
×
408
  }
409

410
  uint64_t nSubmitTbData;
411
  if (tDecodeU64v(pCoder, &nSubmitTbData) < 0) {
10,603,138!
412
    code = TSDB_CODE_INVALID_MSG;
×
413
    TSDB_CHECK_CODE(code, lino, _exit);
×
414
  }
415

416
  int64_t btimeMs = taosGetTimestampMs();
10,603,318✔
417
  int64_t ctimeMs = btimeMs;
10,603,318✔
418
  for (int32_t i = 0; i < nSubmitTbData; i++) {
21,927,088✔
419
    code = vnodePreProcessSubmitTbData(pVnode, pCoder, btimeMs, ctimeMs);
11,324,315✔
420
    TSDB_CHECK_CODE(code, lino, _exit);
11,324,291✔
421
  }
422

423
  tEndDecode(pCoder);
10,602,773✔
424

425
_exit:
10,603,276✔
426
  tDecoderClear(pCoder);
10,603,276✔
427
  if (code) {
10,603,219✔
428
    vError("vgId:%d, %s:%d failed to preprocess submit request since %s, msg type:%s", TD_VID(pVnode), __func__, lino,
521!
429
           tstrerror(code), TMSG_INFO(pMsg->msgType));
430
  }
431
  return code;
10,603,149✔
432
}
433

434
static int32_t vnodePreProcessDeleteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
58,154✔
435
  int32_t code = 0;
58,154✔
436

437
  int32_t    size;
438
  int32_t    ret;
439
  uint8_t   *pCont;
440
  SEncoder  *pCoder = &(SEncoder){0};
58,154✔
441
  SDeleteRes res = {0};
58,154✔
442

443
  SReadHandle handle = {.vnode = pVnode, .pMsgCb = &pVnode->msgCb, .skipRollup = 1};
58,154✔
444
  initStorageAPI(&handle.api);
58,154✔
445

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

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

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

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

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

467
  taosArrayDestroy(res.uidList);
58,154✔
468

469
_exit:
58,154✔
470
  return code;
58,154✔
471
}
472

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

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

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

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

487
  tDecoderClear(pCoder);
2,305✔
488
  taosArrayDestroy(pReq.deleteReqs);
2,305✔
489

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

498
static int32_t vnodePreProcessArbCheckSyncMsg(SVnode *pVnode, SRpcMsg *pMsg) {
37✔
499
  SVArbCheckSyncReq syncReq = {0};
37✔
500

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

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

511
  int32_t code = terrno;
37✔
512
  tFreeSVArbCheckSyncReq(&syncReq);
37✔
513

514
  return code;
37✔
515
}
516

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

526
  tDecoderInit(&dc, POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), pMsg->contLen - sizeof(SMsgHead));
15,699✔
527

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

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

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

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

572
int32_t vnodePreProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
10,944,237✔
573
  int32_t code = 0;
10,944,237✔
574

575
  switch (pMsg->msgType) {
10,944,237✔
576
    case TDMT_VND_CREATE_TABLE: {
80,650✔
577
      code = vnodePreProcessCreateTableMsg(pVnode, pMsg);
80,650✔
578
    } break;
80,645✔
579
    case TDMT_VND_ALTER_TABLE: {
6,361✔
580
      code = vnodePreProcessAlterTableMsg(pVnode, pMsg);
6,361✔
581
    } break;
6,361✔
582
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
67,428✔
583
    case TDMT_VND_DROP_TTL_TABLE: {
584
      code = vnodePreProcessDropTtlMsg(pVnode, pMsg);
67,428✔
585
    } break;
67,286✔
586
    case TDMT_VND_SUBMIT: {
10,603,288✔
587
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.preprocess_time, METRIC_LEVEL_LOW,
10,603,288!
588
                           { code = vnodePreProcessSubmitMsg(pVnode, pMsg); });
589
    } break;
10,603,141✔
590
    case TDMT_VND_DELETE: {
58,155✔
591
      code = vnodePreProcessDeleteMsg(pVnode, pMsg);
58,155✔
592
    } break;
58,154✔
593
    case TDMT_VND_BATCH_DEL: {
2,305✔
594
      code = vnodePreProcessBatchDeleteMsg(pVnode, pMsg);
2,305✔
595
    } break;
2,304✔
596
    case TDMT_VND_ARB_CHECK_SYNC: {
37✔
597
      code = vnodePreProcessArbCheckSyncMsg(pVnode, pMsg);
37✔
598
    } break;
37✔
599
    case TDMT_VND_DROP_TABLE: {
15,699✔
600
      code = vnodePreProcessDropTbMsg(pVnode, pMsg);
15,699✔
601
    } break;
15,699✔
602
    default:
110,314✔
603
      break;
110,314✔
604
  }
605

606
  if (code && code != TSDB_CODE_MSG_PREPROCESSED) {
10,943,941✔
607
    vError("vgId:%d, failed to preprocess write request since %s, msg type:%s", TD_VID(pVnode), tstrerror(code),
554!
608
           TMSG_INFO(pMsg->msgType));
609
  }
610
  return code;
10,943,910✔
611
}
612

613
int32_t vnodeProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg, int64_t ver, SRpcMsg *pRsp) {
14,254,741✔
614
  int32_t code = 0;
14,254,741✔
615
  void   *ptr = NULL;
14,254,741✔
616
  void   *pReq;
617
  int32_t len;
618

619
  (void)taosThreadMutexLock(&pVnode->mutex);
14,254,741✔
620
  if (pVnode->disableWrite) {
14,255,257!
621
    (void)taosThreadMutexUnlock(&pVnode->mutex);
×
622
    vError("vgId:%d write is disabled for snapshot, version:%" PRId64, TD_VID(pVnode), ver);
×
623
    return TSDB_CODE_VND_WRITE_DISABLED;
×
624
  }
625
  (void)taosThreadMutexUnlock(&pVnode->mutex);
14,255,257✔
626

627
  if (ver <= pVnode->state.applied) {
14,255,252!
628
    vError("vgId:%d, duplicate write request. ver: %" PRId64 ", applied: %" PRId64, TD_VID(pVnode), ver,
×
629
           pVnode->state.applied);
630
    return terrno = TSDB_CODE_VND_DUP_REQUEST;
×
631
  }
632

633
  vGDebug(&pMsg->info.traceId,
14,255,252!
634
          "vgId:%d, index:%" PRId64 ", process write request:%s, applied:%" PRId64 ", state.applyTerm:%" PRId64
635
          ", conn.applyTerm:%" PRId64 ", contLen:%d",
636
          TD_VID(pVnode), ver, TMSG_INFO(pMsg->msgType), pVnode->state.applied, pVnode->state.applyTerm,
637
          pMsg->info.conn.applyTerm, pMsg->contLen);
638

639
  if (!(pVnode->state.applyTerm <= pMsg->info.conn.applyTerm)) {
14,254,678!
640
    vError("vgId:%d, applyTerm mismatch, expected: %" PRId64 ", received: %" PRId64, TD_VID(pVnode),
×
641
           pVnode->state.applyTerm, pMsg->info.conn.applyTerm);
642
    return terrno = TSDB_CODE_INTERNAL_ERROR;
×
643
  }
644

645
  if (!(pVnode->state.applied + 1 == ver)) {
14,254,678!
646
    vError("vgId:%d, ver mismatch, expected: %" PRId64 ", received: %" PRId64, TD_VID(pVnode),
×
647
           pVnode->state.applied + 1, ver);
648
    return terrno = TSDB_CODE_INTERNAL_ERROR;
×
649
  }
650

651
  atomic_store_64(&pVnode->state.applied, ver);
14,254,678✔
652
  atomic_store_64(&pVnode->state.applyTerm, pMsg->info.conn.applyTerm);
14,255,199✔
653

654
  if (!syncUtilUserCommit(pMsg->msgType)) goto _exit;
14,255,198✔
655

656
  // skip header
657
  pReq = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
14,213,863✔
658
  len = pMsg->contLen - sizeof(SMsgHead);
14,213,863✔
659
  bool needCommit = false;
14,213,863✔
660

661
  switch (pMsg->msgType) {
14,213,863!
662
    /* META */
663
    case TDMT_VND_CREATE_STB:
30,771✔
664
      if (vnodeProcessCreateStbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
30,771✔
665
      break;
30,897✔
666
    case TDMT_VND_ALTER_STB:
11,919✔
667
      if (vnodeProcessAlterStbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
11,919!
668
      break;
11,942✔
669
    case TDMT_VND_DROP_STB:
3,006✔
670
      if (vnodeProcessDropStbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
3,006!
671
      break;
3,019✔
672
    case TDMT_VND_CREATE_TABLE:
91,401✔
673
      if (vnodeProcessCreateTbReq(pVnode, ver, pReq, len, pRsp, pMsg) < 0) goto _err;
91,401!
674
      break;
91,490✔
675
    case TDMT_VND_ALTER_TABLE:
6,338✔
676
      if (vnodeProcessAlterTbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
6,338!
677
      break;
6,338✔
678
    case TDMT_VND_DROP_TABLE:
15,741✔
679
      if (vnodeProcessDropTbReq(pVnode, ver, pReq, len, pRsp, pMsg) < 0) goto _err;
15,741!
680
      break;
15,741✔
681
    case TDMT_VND_DROP_TTL_TABLE:
×
682
      if (vnodeProcessDropTtlTbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
683
      break;
×
684
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
30✔
685
      if (vnodeProcessFetchTtlExpiredTbs(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
30!
686
      break;
30✔
687
    case TDMT_VND_TRIM:
115✔
688
      if (vnodeProcessTrimReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
115!
689
      break;
115✔
690
    case TDMT_VND_S3MIGRATE:
×
691
      if (vnodeProcessS3MigrateReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
692
      break;
×
693
    case TDMT_VND_CREATE_SMA:
×
694
      if (vnodeProcessCreateTSmaReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
695
      break;
×
696
    /* TSDB */
697
    case TDMT_VND_SUBMIT: {
13,903,199✔
698
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.apply_time, METRIC_LEVEL_LOW, {
13,903,199!
699
        if (vnodeProcessSubmitReq(pVnode, ver, pMsg->pCont, pMsg->contLen, pRsp, pMsg) < 0) goto _err;
700
      });
701
      METRICS_UPDATE(pVnode->writeMetrics.apply_bytes, METRIC_LEVEL_LOW, (int64_t)pMsg->contLen);
13,903,361!
702
      break;
13,903,276✔
703
    }
704
    case TDMT_VND_DELETE:
59,350✔
705
      if (vnodeProcessDeleteReq(pVnode, ver, pReq, len, pRsp, pMsg) < 0) goto _err;
59,350!
706
      break;
59,352✔
707
    case TDMT_VND_BATCH_DEL:
2,301✔
708
      if (vnodeProcessBatchDeleteReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
2,301!
709
      break;
2,306✔
710
      /* TQ */
711
#if defined(USE_TQ) || defined(USE_STREAM)
712
    case TDMT_VND_TMQ_SUBSCRIBE:
3,303✔
713
      if (tqProcessSubscribeReq(pVnode->pTq, ver, pReq, len) < 0) {
3,303!
714
        goto _err;
×
715
      }
716
      break;
3,307✔
717
    case TDMT_VND_TMQ_DELETE_SUB:
1,001✔
718
      if (tqProcessDeleteSubReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen) < 0) {
1,001!
719
        goto _err;
×
720
      }
721
      break;
1,004✔
722
    case TDMT_VND_TMQ_COMMIT_OFFSET:
8,174✔
723
      if (tqProcessOffsetCommitReq(pVnode->pTq, ver, pReq, len) < 0) {
8,174!
724
        goto _err;
×
725
      }
726
      break;
8,188✔
727
    case TDMT_VND_TMQ_ADD_CHECKINFO:
123✔
728
      if (tqProcessAddCheckInfoReq(pVnode->pTq, ver, pReq, len) < 0) {
123!
729
        goto _err;
×
730
      }
731
      break;
123✔
732
    case TDMT_VND_TMQ_DEL_CHECKINFO:
10✔
733
      if (tqProcessDelCheckInfoReq(pVnode->pTq, ver, pReq, len) < 0) {
10!
734
        goto _err;
×
735
      }
736
      break;
10✔
737
    case TDMT_STREAM_TASK_DEPLOY: {
11,108✔
738
      if ((code = tqProcessTaskDeployReq(pVnode->pTq, ver, pReq, len)) != TSDB_CODE_SUCCESS) {
11,108!
739
        goto _err;
×
740
      }
741
    } break;
11,126✔
742
    case TDMT_STREAM_TASK_DROP: {
5,672✔
743
      if ((code = tqProcessTaskDropReq(pVnode->pTq, pMsg->pCont, pMsg->contLen)) < 0) {
5,672!
744
        goto _err;
×
745
      }
746
    } break;
5,697✔
747
    case TDMT_STREAM_TASK_UPDATE_CHKPT: {
3,169✔
748
      if ((code = tqProcessTaskUpdateCheckpointReq(pVnode->pTq, pMsg->pCont, pMsg->contLen)) < 0) {
3,169!
749
        goto _err;
×
750
      }
751
    } break;
3,176✔
752
    case TDMT_STREAM_CONSEN_CHKPT: {
102✔
753
      if (pVnode->restored && (code = tqProcessTaskConsenChkptIdReq(pVnode->pTq, pMsg)) < 0) {
102!
754
        goto _err;
×
755
      }
756

757
    } break;
104✔
758
    case TDMT_STREAM_TASK_PAUSE: {
583✔
759
      if (pVnode->restored && vnodeIsLeader(pVnode) &&
1,066!
760
          (code = tqProcessTaskPauseReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen)) < 0) {
476✔
761
        goto _err;
×
762
      }
763
    } break;
591✔
764
    case TDMT_STREAM_TASK_RESUME: {
826✔
765
      if (pVnode->restored && vnodeIsLeader(pVnode) &&
1,551!
766
          (code = tqProcessTaskResumeReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen)) < 0) {
719✔
767
        goto _err;
×
768
      }
769
    } break;
833✔
770
    case TDMT_VND_STREAM_TASK_RESET: {
×
771
      if (pVnode->restored && vnodeIsLeader(pVnode) && (code = tqProcessTaskResetReq(pVnode->pTq, pMsg)) < 0) {
×
772
        goto _err;
×
773
      }
774

775
    } break;
×
776
    case TDMT_VND_STREAM_ALL_STOP: {
4,978✔
777
      if (pVnode->restored && vnodeIsLeader(pVnode) && (code = tqProcessAllTaskStopReq(pVnode->pTq, pMsg)) < 0) {
4,978!
778
        goto _err;
×
779
      }
780

781
    } break;
4,994✔
782
#endif
783
    case TDMT_VND_ALTER_CONFIRM:
15,093✔
784
      needCommit = pVnode->config.hashChange;
15,093✔
785
      if (vnodeProcessAlterConfirmReq(pVnode, ver, pReq, len, pRsp) < 0) {
15,093!
786
        goto _err;
×
787
      }
788
      break;
15,093✔
789
    case TDMT_VND_ALTER_CONFIG:
976✔
790
      vnodeProcessAlterConfigReq(pVnode, ver, pReq, len, pRsp);
976✔
791
      break;
977✔
792
    case TDMT_VND_COMMIT:
29,171✔
793
      needCommit = true;
29,171✔
794
      break;
29,171✔
795
    case TDMT_VND_CREATE_INDEX:
946✔
796
      vnodeProcessCreateIndexReq(pVnode, ver, pReq, len, pRsp);
946✔
797
      break;
946✔
798
    case TDMT_VND_DROP_INDEX:
2,128✔
799
      vnodeProcessDropIndexReq(pVnode, ver, pReq, len, pRsp);
2,128✔
800
      break;
2,128✔
801
    case TDMT_VND_STREAM_CHECK_POINT_SOURCE:  // always return true
2,077✔
802
      tqProcessTaskCheckPointSourceReq(pVnode->pTq, pMsg, pRsp);
2,077✔
803
      break;
2,078✔
804
    case TDMT_VND_STREAM_TASK_UPDATE:  // always return true
40✔
805
      tqProcessTaskUpdateReq(pVnode->pTq, pMsg);
40✔
806
      break;
40✔
807
    case TDMT_VND_COMPACT:
204✔
808
      vnodeProcessCompactVnodeReq(pVnode, ver, pReq, len, pRsp);
204✔
809
      goto _exit;
204✔
810
    case TDMT_SYNC_CONFIG_CHANGE:
×
811
      vnodeProcessConfigChangeReq(pVnode, ver, pReq, len, pRsp);
×
812
      break;
×
813
#ifdef TD_ENTERPRISE
814
    case TDMT_VND_KILL_COMPACT:
×
815
      vnodeProcessKillCompactReq(pVnode, ver, pReq, len, pRsp);
×
816
      break;
×
817
#endif
818
    /* ARB */
819
    case TDMT_VND_ARB_CHECK_SYNC:
8✔
820
      vnodeProcessArbCheckSyncReq(pVnode, pReq, len, pRsp);
8✔
821
      break;
8✔
822
    default:
×
823
      vError("vgId:%d, unprocessed msg, %d", TD_VID(pVnode), pMsg->msgType);
×
824
      return TSDB_CODE_INVALID_MSG;
×
825
  }
826

827
  vGDebug(&pMsg->info.traceId, "vgId:%d, index:%" PRId64 ", msg processed, code:0x%x", TD_VID(pVnode), ver, pRsp->code);
14,214,100!
828

829
  walApplyVer(pVnode->pWal, ver);
14,214,146✔
830

831
  code = tqPushMsg(pVnode->pTq, pMsg->msgType);
14,214,027✔
832
  if (code) {
14,214,033!
833
    vError("vgId:%d, failed to push msg to TQ since %s", TD_VID(pVnode), tstrerror(terrno));
×
834
    return code;
×
835
  }
836

837
  // commit if need
838
  if (needCommit) {
14,214,033✔
839
    vInfo("vgId:%d, commit at version %" PRId64, TD_VID(pVnode), ver);
29,193!
840
    code = vnodeAsyncCommit(pVnode);
29,280✔
841
    if (code) {
29,282!
842
      vError("vgId:%d, failed to vnode async commit since %s.", TD_VID(pVnode), tstrerror(terrno));
×
843
      goto _err;
×
844
    }
845

846
    // start a new one
847
    METRICS_TIMING_BLOCK(pVnode->writeMetrics.memtable_wait_time, METRIC_LEVEL_LOW, {
29,282!
848
      code = vnodeBegin(pVnode);
849
      if (code) {
850
        vError("vgId:%d, failed to begin vnode since %s.", TD_VID(pVnode), tstrerror(terrno));
851
        goto _err;
852
      }
853
    });
854
  }
855

856
_exit:
14,214,114✔
857
  return 0;
14,254,805✔
858

859
_err:
12✔
860
  vError("vgId:%d, process %s request failed since %s, ver:%" PRId64, TD_VID(pVnode), TMSG_INFO(pMsg->msgType),
12!
861
         tstrerror(terrno), ver);
862
  return code;
12✔
863
}
864

865
int32_t vnodePreprocessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg) {
8,288,292✔
866
  if (TDMT_SCH_QUERY != pMsg->msgType && TDMT_SCH_MERGE_QUERY != pMsg->msgType) {
8,288,292✔
867
    return 0;
432,629✔
868
  }
869

870
  return qWorkerPreprocessQueryMsg(pVnode->pQuery, pMsg, TDMT_SCH_QUERY == pMsg->msgType);
7,855,663✔
871
}
872

873
int32_t vnodeProcessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
8,297,935✔
874
  vTrace("message in vnode query queue is processing");
8,297,935✔
875
  if (pMsg->msgType == TDMT_VND_TMQ_CONSUME && !syncIsReadyForRead(pVnode->sync)) {
8,297,936✔
876
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
1,423✔
877
    return 0;
1,423✔
878
  }
879

880
  if (pMsg->msgType == TDMT_VND_TMQ_CONSUME && !pVnode->restored) {
8,296,507!
881
    vnodeRedirectRpcMsg(pVnode, pMsg, TSDB_CODE_SYN_RESTORING);
×
882
    return 0;
×
883
  }
884

885
  SReadHandle handle = {.vnode = pVnode, .pMsgCb = &pVnode->msgCb, .pWorkerCb = pInfo->workerCb};
8,296,507✔
886
  initStorageAPI(&handle.api);
8,296,507✔
887
  int32_t code = TSDB_CODE_SUCCESS;
8,295,402✔
888
  bool    redirected = false;
8,295,402✔
889

890
  switch (pMsg->msgType) {
8,295,402✔
891
    case TDMT_SCH_QUERY:
6,091,782✔
892
      if (!syncIsReadyForRead(pVnode->sync)) {
6,091,782✔
893
        pMsg->code = (terrno) ? terrno : TSDB_CODE_SYN_NOT_LEADER;
3,398!
894
        redirected = true;
3,398✔
895
      }
896
      code = qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
6,092,163✔
897
      if (redirected) {
6,090,070✔
898
        vnodeRedirectRpcMsg(pVnode, pMsg, pMsg->code);
2,206✔
899
        return 0;
3,398✔
900
      }
901
      return code;
6,087,864✔
902
    case TDMT_SCH_MERGE_QUERY:
1,772,380✔
903
      return qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
1,772,380✔
904
    case TDMT_SCH_QUERY_CONTINUE:
365,583✔
905
      return qWorkerProcessCQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
365,583✔
906
    case TDMT_VND_TMQ_CONSUME:
51,149✔
907
      return tqProcessPollReq(pVnode->pTq, pMsg);
51,149✔
908
    case TDMT_VND_TMQ_CONSUME_PUSH:
14,458✔
909
      return tqProcessPollPush(pVnode->pTq);
14,458✔
910
    default:
50✔
911
      vError("unknown msg type:%d in query queue", pMsg->msgType);
50!
912
      return TSDB_CODE_APP_ERROR;
×
913
  }
914
}
915

916
int32_t vnodeProcessFetchMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
21,610,863✔
917
  int32_t code = TSDB_CODE_SUCCESS;
21,610,863✔
918
  vTrace("vgId:%d, msg:%p in fetch queue is processing", pVnode->config.vgId, pMsg);
21,610,863✔
919
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
21,610,866✔
920
       pMsg->msgType == TDMT_VND_BATCH_META || pMsg->msgType == TDMT_VND_TABLE_NAME ||
15,155,488!
921
       pMsg->msgType == TDMT_VND_VSUBTABLES_META || pMsg->msgType == TDMT_VND_VSTB_REF_DBS) &&
13,979,516✔
922
      !syncIsReadyForRead(pVnode->sync)) {
7,636,289✔
923
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
4,895✔
924
    return 0;
4,895✔
925
  }
926

927
  switch (pMsg->msgType) {
21,610,466!
928
    case TDMT_SCH_FETCH:
10,214,429✔
929
    case TDMT_SCH_MERGE_FETCH:
930
      return qWorkerProcessFetchMsg(pVnode, pVnode->pQuery, pMsg, 0);
10,214,429✔
931
    case TDMT_SCH_FETCH_RSP:
×
932
      return qWorkerProcessRspMsg(pVnode, pVnode->pQuery, pMsg, 0);
×
933
    // case TDMT_SCH_CANCEL_TASK:
934
    //   return qWorkerProcessCancelMsg(pVnode, pVnode->pQuery, pMsg, 0);
935
    case TDMT_SCH_DROP_TASK:
7,853,523✔
936
      return qWorkerProcessDropMsg(pVnode, pVnode->pQuery, pMsg, 0);
7,853,523✔
937
    case TDMT_SCH_TASK_NOTIFY:
65✔
938
      return qWorkerProcessNotifyMsg(pVnode, pVnode->pQuery, pMsg, 0);
65✔
939
    case TDMT_SCH_QUERY_HEARTBEAT:
2,367,560✔
940
      return qWorkerProcessHbMsg(pVnode, pVnode->pQuery, pMsg, 0);
2,367,560✔
941
    case TDMT_VND_TABLE_META:
3,000✔
942
    case TDMT_VND_TABLE_NAME:
943
      return vnodeGetTableMeta(pVnode, pMsg, true);
3,000✔
944
    case TDMT_VND_TABLE_CFG:
×
945
      return vnodeGetTableCfg(pVnode, pMsg, true);
×
946
    case TDMT_VND_BATCH_META: {
1,171,792✔
947
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.fetch_batch_meta_time, METRIC_LEVEL_LOW,
1,171,792!
948
                           { code = vnodeGetBatchMeta(pVnode, pMsg); });
949
      METRICS_UPDATE(pVnode->writeMetrics.fetch_batch_meta_count, METRIC_LEVEL_LOW, 1);
1,172,552!
950
      return code;
1,172,545✔
951
    }
952
    case TDMT_VND_VSUBTABLES_META:
×
953
      return vnodeGetVSubtablesMeta(pVnode, pMsg);
×
954
    case TDMT_VND_VSTB_REF_DBS:
×
955
      return vnodeGetVStbRefDbs(pVnode, pMsg);
×
956
#ifdef TD_ENTERPRISE
957
    case TDMT_VND_QUERY_COMPACT_PROGRESS:
56✔
958
      return vnodeQueryCompactProgress(pVnode, pMsg);
56✔
959
#endif
960
      //    case TDMT_VND_TMQ_CONSUME:
961
      //      return tqProcessPollReq(pVnode->pTq, pMsg);
962
#ifdef USE_TQ
963
    case TDMT_VND_TMQ_VG_WALINFO:
11✔
964
      return tqProcessVgWalInfoReq(pVnode->pTq, pMsg);
11✔
965
    case TDMT_VND_TMQ_VG_COMMITTEDINFO:
6✔
966
      return tqProcessVgCommittedInfoReq(pVnode->pTq, pMsg);
6✔
967
    case TDMT_VND_TMQ_SEEK:
24✔
968
      return tqProcessSeekReq(pVnode->pTq, pMsg);
24✔
969
#endif
970
    default:
×
971
      vError("unknown msg type:%d in fetch queue", pMsg->msgType);
×
972
      return TSDB_CODE_APP_ERROR;
×
973
  }
974
}
975
#ifdef USE_STREAM
976
int32_t vnodeProcessStreamMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
263,733✔
977
  vTrace("vgId:%d, msg:%p in stream queue is processing", pVnode->config.vgId, pMsg);
263,733✔
978

979
  // todo: NOTE: some command needs to run on follower, such as, stop_all_tasks
980
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
263,733!
981
       pMsg->msgType == TDMT_VND_BATCH_META) &&
263,775!
982
      !syncIsReadyForRead(pVnode->sync)) {
×
983
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
×
984
    return 0;
×
985
  }
986

987
  switch (pMsg->msgType) {
263,775!
988
    case TDMT_STREAM_TASK_RUN:
262,814✔
989
      return tqProcessTaskRunReq(pVnode->pTq, pMsg);
262,814✔
990
    case TDMT_STREAM_RETRIEVE:
539✔
991
      return tqProcessTaskRetrieveReq(pVnode->pTq, pMsg);
539✔
992
    case TDMT_STREAM_RETRIEVE_RSP:
422✔
993
      return tqProcessTaskRetrieveRsp(pVnode->pTq, pMsg);
422✔
994
    case TDMT_VND_GET_STREAM_PROGRESS:
×
995
      return tqStreamProgressRetrieveReq(pVnode->pTq, pMsg);
×
996
    default:
×
997
      vError("unknown msg type:%d in stream queue", pMsg->msgType);
×
998
      return TSDB_CODE_APP_ERROR;
×
999
  }
1000
}
1001

1002
int32_t vnodeProcessStreamCtrlMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
117,422✔
1003
  vTrace("vgId:%d, msg:%p in stream ctrl queue is processing", pVnode->config.vgId, pMsg);
117,422✔
1004
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
117,423!
1005
       pMsg->msgType == TDMT_VND_BATCH_META) &&
117,455!
UNCOV
1006
      !syncIsReadyForRead(pVnode->sync)) {
×
1007
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
×
1008
    return 0;
×
1009
  }
1010

1011
  switch (pMsg->msgType) {
117,455!
1012
    case TDMT_MND_STREAM_HEARTBEAT_RSP:
19,138✔
1013
      return tqProcessStreamHbRsp(pVnode->pTq, pMsg);
19,138✔
1014
    case TDMT_STREAM_TASK_DISPATCH:
21,872✔
1015
      return tqProcessTaskDispatchReq(pVnode->pTq, pMsg);
21,872✔
1016
    case TDMT_STREAM_TASK_DISPATCH_RSP:
22,170✔
1017
      return tqProcessTaskDispatchRsp(pVnode->pTq, pMsg);
22,170✔
1018
    case TDMT_VND_STREAM_TASK_CHECK:
19,722✔
1019
      return tqProcessTaskCheckReq(pVnode->pTq, pMsg);
19,722✔
1020
    case TDMT_VND_STREAM_TASK_CHECK_RSP:
18,216✔
1021
      return tqProcessTaskCheckRsp(pVnode->pTq, pMsg);
18,216✔
1022
    case TDMT_STREAM_TASK_CHECKPOINT_READY:
4,772✔
1023
      return tqProcessTaskCheckpointReadyMsg(pVnode->pTq, pMsg);
4,772✔
1024
    case TDMT_STREAM_TASK_CHECKPOINT_READY_RSP:
4,777✔
1025
      return tqProcessTaskCheckpointReadyRsp(pVnode->pTq, pMsg);
4,777✔
1026
    case TDMT_STREAM_RETRIEVE_TRIGGER:
×
1027
      return tqProcessTaskRetrieveTriggerReq(pVnode->pTq, pMsg);
×
1028
    case TDMT_STREAM_RETRIEVE_TRIGGER_RSP:
×
1029
      return tqProcessTaskRetrieveTriggerRsp(pVnode->pTq, pMsg);
×
1030
    case TDMT_MND_STREAM_REQ_CHKPT_RSP:
2,838✔
1031
      return tqProcessStreamReqCheckpointRsp(pVnode->pTq, pMsg);
2,838✔
1032
    case TDMT_MND_STREAM_CHKPT_REPORT_RSP:
3,950✔
1033
      return tqProcessTaskChkptReportRsp(pVnode->pTq, pMsg);
3,950✔
1034
    default:
×
1035
      vError("unknown msg type:%d in stream ctrl queue", pMsg->msgType);
×
1036
      return TSDB_CODE_APP_ERROR;
×
1037
  }
1038
}
1039

1040
int32_t vnodeProcessStreamLongExecMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
1,761✔
1041
  vTrace("vgId:%d, msg:%p in stream long exec queue is processing", pVnode->config.vgId, pMsg);
1,761✔
1042
  if (!syncIsReadyForRead(pVnode->sync)) {
1,761!
1043
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
×
1044
    return 0;
×
1045
  }
1046

1047
  switch (pMsg->msgType) {
1,761!
1048
    case TDMT_VND_STREAM_SCAN_HISTORY:
1,761✔
1049
      return tqProcessTaskScanHistory(pVnode->pTq, pMsg);
1,761✔
1050
    default:
×
1051
      vError("unknown msg type:%d in stream long exec queue", pMsg->msgType);
×
1052
      return TSDB_CODE_APP_ERROR;
×
1053
  }
1054
}
1055

1056
int32_t vnodeProcessStreamChkptMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
7,985✔
1057
  vTrace("vgId:%d, msg:%p in stream chkpt queue is processing", pVnode->config.vgId, pMsg);
7,985✔
1058
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
7,985!
1059
       pMsg->msgType == TDMT_VND_BATCH_META) &&
7,985!
1060
      !syncIsReadyForRead(pVnode->sync)) {
×
1061
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
×
1062
    return 0;
×
1063
  }
1064

1065
  switch (pMsg->msgType) {
7,985!
1066
    case TDMT_STREAM_CHKPT_EXEC:
7,985✔
1067
      return tqProcessTaskRunReq(pVnode->pTq, pMsg);
7,985✔
1068
    default:
×
1069
      vError("unknown msg type:%d in stream chkpt queue", pMsg->msgType);
×
1070
      return TSDB_CODE_APP_ERROR;
×
1071
  }
1072
}
1073
#endif
1074

1075
void smaHandleRes(void *pVnode, int64_t smaId, const SArray *data) {
×
1076
  int32_t code = tdProcessTSmaInsert(((SVnode *)pVnode)->pSma, smaId, (const char *)data);
×
1077
  if (code) {
×
1078
    vError("failed to process sma result since %s", tstrerror(code));
×
1079
  }
1080
}
×
1081

1082
void vnodeUpdateMetaRsp(SVnode *pVnode, STableMetaRsp *pMetaRsp) {
209,499✔
1083
  if (NULL == pMetaRsp) {
209,499!
1084
    return;
×
1085
  }
1086

1087
  tstrncpy(pMetaRsp->dbFName, pVnode->config.dbname, TSDB_DB_FNAME_LEN);
209,499✔
1088
  pMetaRsp->dbId = pVnode->config.dbId;
209,499✔
1089
  pMetaRsp->vgId = TD_VID(pVnode);
209,499✔
1090
  pMetaRsp->precision = pVnode->config.tsdbCfg.precision;
209,499✔
1091
}
1092

1093
extern int32_t vnodeAsyncRetention(SVnode *pVnode, int64_t now);
1094

1095
static int32_t vnodeProcessTrimReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
115✔
1096
  if (!pVnode->restored) {
115✔
1097
    vInfo("vgId:%d, ignore trim req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
6!
1098
    return 0;
6✔
1099
  }
1100

1101
  int32_t     code = 0;
109✔
1102
  SVTrimDbReq trimReq = {0};
109✔
1103

1104
  // decode
1105
  if (tDeserializeSVTrimDbReq(pReq, len, &trimReq) != 0) {
109!
1106
    code = TSDB_CODE_INVALID_MSG;
×
1107
    goto _exit;
×
1108
  }
1109

1110
  vInfo("vgId:%d, process trim vnode request, time:%d", pVnode->config.vgId, trimReq.timestamp);
109!
1111

1112
  code = vnodeAsyncRetention(pVnode, trimReq.timestamp);
109✔
1113

1114
_exit:
109✔
1115
  return code;
109✔
1116
}
1117

1118
extern int32_t vnodeAsyncS3Migrate(SVnode *pVnode, int64_t now);
1119

1120
static int32_t vnodeProcessS3MigrateReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1121
  int32_t          code = 0;
×
1122
  SVS3MigrateDbReq s3migrateReq = {0};
×
1123

1124
  // decode
1125
  if (tDeserializeSVS3MigrateDbReq(pReq, len, &s3migrateReq) != 0) {
×
1126
    code = TSDB_CODE_INVALID_MSG;
×
1127
    goto _exit;
×
1128
  }
1129

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

1132
  code = vnodeAsyncS3Migrate(pVnode, s3migrateReq.timestamp);
×
1133

1134
_exit:
×
1135
  return code;
×
1136
}
1137

1138
static int32_t vnodeProcessDropTtlTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1139
  int               ret = 0;
×
1140
  SVDropTtlTableReq ttlReq = {0};
×
1141
  if (tDeserializeSVDropTtlTableReq(pReq, len, &ttlReq) != 0) {
×
1142
    ret = TSDB_CODE_INVALID_MSG;
×
1143
    goto end;
×
1144
  }
1145

1146
  if (ttlReq.nUids != taosArrayGetSize(ttlReq.pTbUids)) {
×
1147
    ret = TSDB_CODE_INVALID_MSG;
×
1148
    goto end;
×
1149
  }
1150

1151
  if (ttlReq.nUids != 0) {
×
1152
    vInfo("vgId:%d, process drop ttl table request, time:%d, ntbUids:%d", pVnode->config.vgId, ttlReq.timestampSec,
×
1153
          ttlReq.nUids);
1154
  }
1155

1156
  if (ttlReq.nUids > 0) {
×
1157
    int32_t code = metaDropMultipleTables(pVnode->pMeta, ver, ttlReq.pTbUids);
×
1158
    if (code) return code;
×
1159

1160
    code = tqUpdateTbUidList(pVnode->pTq, ttlReq.pTbUids, false);
×
1161
    if (code) {
×
1162
      vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1163
    }
1164
  }
1165

1166
end:
×
1167
  taosArrayDestroy(ttlReq.pTbUids);
×
1168
  return ret;
×
1169
}
1170

1171
static int32_t vnodeProcessFetchTtlExpiredTbs(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
30✔
1172
  int32_t                 code = -1;
30✔
1173
  SMetaReader             mr = {0};
30✔
1174
  SVDropTtlTableReq       ttlReq = {0};
30✔
1175
  SVFetchTtlExpiredTbsRsp rsp = {0};
30✔
1176
  SEncoder                encoder = {0};
30✔
1177
  SArray                 *pNames = NULL;
30✔
1178
  pRsp->msgType = TDMT_VND_FETCH_TTL_EXPIRED_TBS_RSP;
30✔
1179
  pRsp->code = TSDB_CODE_SUCCESS;
30✔
1180
  pRsp->pCont = NULL;
30✔
1181
  pRsp->contLen = 0;
30✔
1182

1183
  if (tDeserializeSVDropTtlTableReq(pReq, len, &ttlReq) != 0) {
30!
1184
    terrno = TSDB_CODE_INVALID_MSG;
×
1185
    goto _end;
×
1186
  }
1187

1188
  if (!(ttlReq.nUids == taosArrayGetSize(ttlReq.pTbUids))) {
30!
1189
    terrno = TSDB_CODE_INVALID_MSG;
×
1190
    goto _end;
×
1191
  }
1192

1193
  tb_uid_t    suid;
1194
  char        ctbName[TSDB_TABLE_NAME_LEN];
1195
  SVDropTbReq expiredTb = {.igNotExists = true};
30✔
1196
  metaReaderDoInit(&mr, pVnode->pMeta, 0);
30✔
1197
  rsp.vgId = TD_VID(pVnode);
30✔
1198
  rsp.pExpiredTbs = taosArrayInit(ttlReq.nUids, sizeof(SVDropTbReq));
30✔
1199
  if (!rsp.pExpiredTbs) goto _end;
30!
1200

1201
  pNames = taosArrayInit(ttlReq.nUids, TSDB_TABLE_NAME_LEN);
30✔
1202
  if (!pNames) {
30!
1203
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1204
    goto _end;
×
1205
  }
1206
  char buf[TSDB_TABLE_NAME_LEN];
1207
  for (int32_t i = 0; i < ttlReq.nUids; ++i) {
152✔
1208
    tb_uid_t *uid = taosArrayGet(ttlReq.pTbUids, i);
122✔
1209
    expiredTb.suid = *uid;
122✔
1210
    terrno = metaReaderGetTableEntryByUid(&mr, *uid);
122✔
1211
    if (terrno < 0) goto _end;
123!
1212
    tstrncpy(buf, mr.me.name, TSDB_TABLE_NAME_LEN);
123✔
1213
    void *p = taosArrayPush(pNames, buf);
121✔
1214
    if (p == NULL) {
121!
1215
      goto _end;
×
1216
    }
1217

1218
    expiredTb.name = p;
121✔
1219
    if (mr.me.type == TSDB_CHILD_TABLE) {
121✔
1220
      expiredTb.suid = mr.me.ctbEntry.suid;
74✔
1221
    }
1222

1223
    if (taosArrayPush(rsp.pExpiredTbs, &expiredTb) == NULL) {
243!
1224
      goto _end;
×
1225
    }
1226
  }
1227

1228
  int32_t ret = 0;
30✔
1229
  tEncodeSize(tEncodeVFetchTtlExpiredTbsRsp, &rsp, pRsp->contLen, ret);
30!
1230
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
30✔
1231
  if (pRsp->pCont == NULL) {
30!
1232
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1233
    code = -1;
×
1234
    goto _end;
×
1235
  }
1236
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
30✔
1237
  terrno = tEncodeVFetchTtlExpiredTbsRsp(&encoder, &rsp);
30✔
1238
  tEncoderClear(&encoder);
30✔
1239

1240
  if (terrno == 0) code = 0;
30!
1241
_end:
×
1242
  metaReaderClear(&mr);
30✔
1243
  tFreeFetchTtlExpiredTbsRsp(&rsp);
30✔
1244
  taosArrayDestroy(ttlReq.pTbUids);
30✔
1245
  if (pNames) taosArrayDestroy(pNames);
30!
1246
  pRsp->code = terrno;
30✔
1247
  return code;
30✔
1248
}
1249

1250
static int32_t vnodeProcessCreateStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
30,621✔
1251
  int32_t        code = 0;
30,621✔
1252
  SVCreateStbReq req = {0};
30,621✔
1253
  SDecoder       coder;
1254

1255
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
30,621✔
1256
  pRsp->code = TSDB_CODE_SUCCESS;
30,621✔
1257
  pRsp->pCont = NULL;
30,621✔
1258
  pRsp->contLen = 0;
30,621✔
1259

1260
  // decode and process req
1261
  tDecoderInit(&coder, pReq, len);
30,621✔
1262

1263
  code = tDecodeSVCreateStbReq(&coder, &req);
30,795✔
1264
  if (code) {
30,754!
1265
    pRsp->code = code;
×
1266
    goto _err;
×
1267
  }
1268

1269
  code = metaCreateSuperTable(pVnode->pMeta, ver, &req);
30,754✔
1270
  if (code) {
30,956✔
1271
    pRsp->code = code;
3✔
1272
    goto _err;
3✔
1273
  }
1274

1275
  if ((code = tdProcessRSmaCreate(pVnode->pSma, &req)) < 0) {
30,953!
1276
    pRsp->code = code;
×
1277
    goto _err;
×
1278
  }
1279

1280
  tDecoderClear(&coder);
30,937✔
1281
  return 0;
30,893✔
1282

1283
_err:
3✔
1284
  tDecoderClear(&coder);
3✔
1285
  return code;
3✔
1286
}
1287

1288
static int32_t vnodeProcessCreateTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
91,398✔
1289
                                       SRpcMsg *pOriginRpc) {
1290
  SDecoder           decoder = {0};
91,398✔
1291
  SEncoder           encoder = {0};
91,398✔
1292
  int32_t            rcode = 0;
91,398✔
1293
  SVCreateTbBatchReq req = {0};
91,398✔
1294
  SVCreateTbReq     *pCreateReq;
1295
  SVCreateTbBatchRsp rsp = {0};
91,398✔
1296
  SVCreateTbRsp      cRsp = {0};
91,398✔
1297
  char               tbName[TSDB_TABLE_FNAME_LEN];
1298
  STbUidStore       *pStore = NULL;
91,398✔
1299
  SArray            *tbUids = NULL;
91,398✔
1300
  SArray            *tbNames = NULL;
91,398✔
1301
  pRsp->msgType = TDMT_VND_CREATE_TABLE_RSP;
91,398✔
1302
  pRsp->code = TSDB_CODE_SUCCESS;
91,398✔
1303
  pRsp->pCont = NULL;
91,398✔
1304
  pRsp->contLen = 0;
91,398✔
1305

1306
  // decode
1307
  tDecoderInit(&decoder, pReq, len);
91,398✔
1308
  if (tDecodeSVCreateTbBatchReq(&decoder, &req) < 0) {
91,400!
1309
    rcode = -1;
×
1310
    terrno = TSDB_CODE_INVALID_MSG;
×
1311
    goto _exit;
×
1312
  }
1313

1314
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(cRsp));
91,464✔
1315
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
91,490✔
1316
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
91,490✔
1317
  if (rsp.pArray == NULL || tbUids == NULL || tbNames == NULL) {
91,491!
1318
    rcode = -1;
2✔
1319
    terrno = TSDB_CODE_OUT_OF_MEMORY;
2✔
1320
    goto _exit;
×
1321
  }
1322

1323
  // loop to create table
1324
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
223,476✔
1325
    pCreateReq = req.pReqs + iReq;
131,968✔
1326
    memset(&cRsp, 0, sizeof(cRsp));
131,968✔
1327

1328
    if (tsEnableAudit && tsEnableAuditCreateTable) {
131,968!
1329
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
131,971!
1330
      if (str == NULL) {
131,967!
1331
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1332
        rcode = -1;
×
1333
        goto _exit;
×
1334
      }
1335
      tstrncpy(str, pCreateReq->name, TSDB_TABLE_FNAME_LEN);
131,967✔
1336
      if (taosArrayPush(tbNames, &str) == NULL) {
131,973!
1337
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1338
        rcode = -1;
×
1339
        goto _exit;
×
1340
      }
1341
    }
1342

1343
    // validate hash
1344
    (void)tsnprintf(tbName, TSDB_TABLE_FNAME_LEN, "%s.%s", pVnode->config.dbname, pCreateReq->name);
131,970✔
1345
    if (vnodeValidateTableHash(pVnode, tbName) < 0) {
131,977!
1346
      cRsp.code = TSDB_CODE_VND_HASH_MISMATCH;
×
1347
      if (taosArrayPush(rsp.pArray, &cRsp) == NULL) {
×
1348
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1349
        rcode = -1;
×
1350
        goto _exit;
×
1351
      }
1352
      vError("vgId:%d create-table:%s failed due to hash value mismatch", TD_VID(pVnode), tbName);
×
1353
      continue;
×
1354
    }
1355

1356
    // do create table
1357
    if (metaCreateTable2(pVnode->pMeta, ver, pCreateReq, &cRsp.pMeta) < 0) {
131,946✔
1358
      if (pCreateReq->flags & TD_CREATE_IF_NOT_EXISTS && terrno == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
953!
1359
        cRsp.code = TSDB_CODE_SUCCESS;
5✔
1360
      } else {
1361
        cRsp.code = terrno;
948✔
1362
      }
1363
    } else {
1364
      cRsp.code = TSDB_CODE_SUCCESS;
131,033✔
1365
      if (tdFetchTbUidList(pVnode->pSma, &pStore, pCreateReq->ctb.suid, pCreateReq->uid) < 0) {
131,033!
1366
        vError("vgId:%d, failed to fetch tbUid list", TD_VID(pVnode));
×
1367
      }
1368
      if (taosArrayPush(tbUids, &pCreateReq->uid) == NULL) {
262,067!
1369
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1370
        rcode = -1;
×
1371
        goto _exit;
×
1372
      }
1373
      vnodeUpdateMetaRsp(pVnode, cRsp.pMeta);
131,032✔
1374
    }
1375

1376
    if (taosArrayPush(rsp.pArray, &cRsp) == NULL) {
263,975!
1377
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1378
      rcode = -1;
×
1379
      goto _exit;
×
1380
    }
1381
  }
1382

1383
  vTrace("vgId:%d, add %d new created tables into query table list", TD_VID(pVnode), (int32_t)taosArrayGetSize(tbUids));
91,508✔
1384
  if (tqUpdateTbUidList(pVnode->pTq, tbUids, true) < 0) {
91,508!
1385
    vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(terrno));
×
1386
  }
1387
  if (tdUpdateTbUidList(pVnode->pSma, pStore, true) < 0) {
91,491!
1388
    goto _exit;
×
1389
  }
1390
  pStore = tdUidStoreFree(pStore);
91,491✔
1391

1392
  // prepare rsp
1393
  int32_t ret = 0;
91,493✔
1394
  tEncodeSize(tEncodeSVCreateTbBatchRsp, &rsp, pRsp->contLen, ret);
91,493!
1395
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
91,489✔
1396
  if (pRsp->pCont == NULL) {
91,492!
1397
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1398
    rcode = -1;
×
1399
    goto _exit;
×
1400
  }
1401
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
91,492✔
1402
  if (tEncodeSVCreateTbBatchRsp(&encoder, &rsp) != 0) {
91,493!
1403
    vError("vgId:%d, failed to encode create table batch response", TD_VID(pVnode));
×
1404
  }
1405

1406
  if (tsEnableAudit && tsEnableAuditCreateTable) {
91,489!
1407
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
91,488✔
1408

1409
    SName name = {0};
91,488✔
1410
    if (tNameFromString(&name, pVnode->config.dbname, T_NAME_ACCT | T_NAME_DB) < 0) {
91,488!
1411
      vError("vgId:%d, failed to get name from string", TD_VID(pVnode));
×
1412
    }
1413

1414
    SStringBuilder sb = {0};
91,489✔
1415
    for (int32_t i = 0; i < tbNames->size; i++) {
223,430✔
1416
      char **key = (char **)taosArrayGet(tbNames, i);
131,944✔
1417
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
131,942✔
1418
      if (i < tbNames->size - 1) {
131,941✔
1419
        taosStringBuilderAppendChar(&sb, ',');
40,453✔
1420
      }
1421
      // taosMemoryFreeClear(*key);
1422
    }
1423

1424
    size_t len = 0;
91,486✔
1425
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
91,486✔
1426

1427
    if (pOriginRpc->info.conn.user != NULL && strlen(pOriginRpc->info.conn.user) > 0) {
91,489!
1428
      auditAddRecord(pOriginRpc, clusterId, "createTable", name.dbname, "", keyJoined, len);
75,632✔
1429
    }
1430

1431
    taosStringBuilderDestroy(&sb);
91,489✔
1432
  }
1433

1434
_exit:
1✔
1435
  tDeleteSVCreateTbBatchReq(&req);
91,490✔
1436
  taosArrayDestroyEx(rsp.pArray, tFreeSVCreateTbRsp);
91,492✔
1437
  taosArrayDestroy(tbUids);
91,494✔
1438
  tDecoderClear(&decoder);
91,494✔
1439
  tEncoderClear(&encoder);
91,492✔
1440
  taosArrayDestroyP(tbNames, NULL);
91,493✔
1441
  return rcode;
91,494✔
1442
}
1443

1444
static int32_t vnodeProcessAlterStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
11,916✔
1445
  int32_t        code = 0;
11,916✔
1446
  SVCreateStbReq req = {0};
11,916✔
1447
  SDecoder       dc = {0};
11,916✔
1448

1449
  pRsp->msgType = TDMT_VND_ALTER_STB_RSP;
11,916✔
1450
  pRsp->code = TSDB_CODE_SUCCESS;
11,916✔
1451
  pRsp->pCont = NULL;
11,916✔
1452
  pRsp->contLen = 0;
11,916✔
1453

1454
  tDecoderInit(&dc, pReq, len);
11,916✔
1455

1456
  // decode req
1457
  code = tDecodeSVCreateStbReq(&dc, &req);
11,916✔
1458
  if (code) {
11,931!
1459
    tDecoderClear(&dc);
×
1460
    return code;
×
1461
  }
1462

1463
  code = metaAlterSuperTable(pVnode->pMeta, ver, &req);
11,931✔
1464
  if (code) {
11,942!
1465
    pRsp->code = code;
×
1466
    tDecoderClear(&dc);
×
1467
    return code;
×
1468
  }
1469

1470
  tDecoderClear(&dc);
11,942✔
1471

1472
  return 0;
11,942✔
1473
}
1474

1475
static int32_t vnodeProcessDropStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
3,007✔
1476
  SVDropStbReq req = {0};
3,007✔
1477
  int32_t      rcode = TSDB_CODE_SUCCESS;
3,007✔
1478
  SDecoder     decoder = {0};
3,007✔
1479
  SArray      *tbUidList = NULL;
3,007✔
1480

1481
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
3,007✔
1482
  pRsp->pCont = NULL;
3,007✔
1483
  pRsp->contLen = 0;
3,007✔
1484

1485
  // decode request
1486
  tDecoderInit(&decoder, pReq, len);
3,007✔
1487
  if (tDecodeSVDropStbReq(&decoder, &req) < 0) {
3,005!
1488
    rcode = TSDB_CODE_INVALID_MSG;
×
1489
    goto _exit;
×
1490
  }
1491

1492
  // process request
1493
  tbUidList = taosArrayInit(8, sizeof(int64_t));
3,014✔
1494
  if (tbUidList == NULL) goto _exit;
3,018!
1495
  if (metaDropSuperTable(pVnode->pMeta, ver, &req) < 0) {
3,018✔
1496
    rcode = terrno;
38✔
1497
    goto _exit;
38✔
1498
  }
1499

1500
  if (tqUpdateTbUidList(pVnode->pTq, tbUidList, false) < 0) {
2,981!
1501
    rcode = terrno;
×
1502
    goto _exit;
×
1503
  }
1504

1505
  if (tdProcessRSmaDrop(pVnode->pSma, &req) < 0) {
2,981!
1506
    rcode = terrno;
×
1507
    goto _exit;
×
1508
  }
1509

1510
  // return rsp
1511
_exit:
2,980✔
1512
  if (tbUidList) taosArrayDestroy(tbUidList);
3,018!
1513
  pRsp->code = rcode;
3,019✔
1514
  tDecoderClear(&decoder);
3,019✔
1515
  return 0;
3,019✔
1516
}
1517

1518
static int32_t vnodeProcessAlterTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
6,338✔
1519
  SVAlterTbReq  vAlterTbReq = {0};
6,338✔
1520
  SVAlterTbRsp  vAlterTbRsp = {0};
6,338✔
1521
  SDecoder      dc = {0};
6,338✔
1522
  int32_t       code = 0;
6,338✔
1523
  int32_t       lino = 0;
6,338✔
1524
  int32_t       ret;
1525
  SEncoder      ec = {0};
6,338✔
1526
  STableMetaRsp vMetaRsp = {0};
6,338✔
1527

1528
  pRsp->msgType = TDMT_VND_ALTER_TABLE_RSP;
6,338✔
1529
  pRsp->pCont = NULL;
6,338✔
1530
  pRsp->contLen = 0;
6,338✔
1531
  pRsp->code = TSDB_CODE_SUCCESS;
6,338✔
1532

1533
  tDecoderInit(&dc, pReq, len);
6,338✔
1534

1535
  // decode
1536
  if (tDecodeSVAlterTbReq(&dc, &vAlterTbReq) < 0) {
6,338!
1537
    vAlterTbRsp.code = TSDB_CODE_INVALID_MSG;
×
1538
    tDecoderClear(&dc);
×
1539
    goto _exit;
×
1540
  }
1541

1542
  // process
1543
  if (metaAlterTable(pVnode->pMeta, ver, &vAlterTbReq, &vMetaRsp) < 0) {
6,338✔
1544
    vAlterTbRsp.code = terrno;
919✔
1545
    tDecoderClear(&dc);
919✔
1546
    goto _exit;
919✔
1547
  }
1548
  tDecoderClear(&dc);
5,419✔
1549

1550
  if (NULL != vMetaRsp.pSchemas) {
5,419✔
1551
    vnodeUpdateMetaRsp(pVnode, &vMetaRsp);
1,582✔
1552
    vAlterTbRsp.pMeta = &vMetaRsp;
1,582✔
1553
  }
1554

1555
  if (vAlterTbReq.action == TSDB_ALTER_TABLE_UPDATE_TAG_VAL ||
5,419✔
1556
      vAlterTbReq.action == TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL) {
1,712✔
1557
    int64_t uid = metaGetTableEntryUidByName(pVnode->pMeta, vAlterTbReq.tbName);
3,717✔
1558
    if (uid == 0) {
3,717!
1559
      vError("vgId:%d, %s failed at %s:%d since table %s not found", TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
1560
             vAlterTbReq.tbName);
1561
      goto _exit;
×
1562
    }
1563

1564
    SArray *tbUids = taosArrayInit(4, sizeof(int64_t));
3,717✔
1565
    void   *p = taosArrayPush(tbUids, &uid);
3,717✔
1566
    TSDB_CHECK_NULL(p, code, lino, _exit, terrno);
3,717!
1567

1568
    vDebug("vgId:%d, remove tags value altered table:%s from query table list", TD_VID(pVnode), vAlterTbReq.tbName);
3,717✔
1569
    if ((code = tqUpdateTbUidList(pVnode->pTq, tbUids, false)) < 0) {
3,717!
1570
      vError("vgId:%d, failed to remove tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1571
    }
1572

1573
    vDebug("vgId:%d, try to add table:%s in query table list", TD_VID(pVnode), vAlterTbReq.tbName);
3,717✔
1574
    if ((code = tqUpdateTbUidList(pVnode->pTq, tbUids, true)) < 0) {
3,717!
1575
      vError("vgId:%d, failed to add tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1576
    }
1577

1578
    taosArrayDestroy(tbUids);
3,717✔
1579
  }
1580

1581
_exit:
1,702✔
1582
  taosArrayDestroy(vAlterTbReq.pMultiTag);
6,338✔
1583
  tEncodeSize(tEncodeSVAlterTbRsp, &vAlterTbRsp, pRsp->contLen, ret);
6,338!
1584
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
6,338✔
1585
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
6,338✔
1586
  if (tEncodeSVAlterTbRsp(&ec, &vAlterTbRsp) != 0) {
6,338!
1587
    vError("vgId:%d, failed to encode alter table response", TD_VID(pVnode));
×
1588
  }
1589

1590
  tEncoderClear(&ec);
6,338✔
1591
  if (vMetaRsp.pSchemas) {
6,338✔
1592
    taosMemoryFree(vMetaRsp.pSchemas);
1,582!
1593
    taosMemoryFree(vMetaRsp.pSchemaExt);
1,582!
1594
  }
1595
  if (vMetaRsp.pColRefs) {
6,338✔
1596
    taosMemoryFree(vMetaRsp.pColRefs);
302!
1597
  }
1598
  return 0;
6,338✔
1599
}
1600

1601
static int32_t vnodeProcessDropTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
15,741✔
1602
                                     SRpcMsg *pOriginRpc) {
1603
  SVDropTbBatchReq req = {0};
15,741✔
1604
  SVDropTbBatchRsp rsp = {0};
15,741✔
1605
  SDecoder         decoder = {0};
15,741✔
1606
  SEncoder         encoder = {0};
15,741✔
1607
  int32_t          ret;
1608
  SArray          *tbUids = NULL;
15,741✔
1609
  STbUidStore     *pStore = NULL;
15,741✔
1610
  SArray          *tbNames = NULL;
15,741✔
1611

1612
  pRsp->msgType = TDMT_VND_DROP_TABLE_RSP;
15,741✔
1613
  pRsp->pCont = NULL;
15,741✔
1614
  pRsp->contLen = 0;
15,741✔
1615
  pRsp->code = TSDB_CODE_SUCCESS;
15,741✔
1616

1617
  // decode req
1618
  tDecoderInit(&decoder, pReq, len);
15,741✔
1619
  ret = tDecodeSVDropTbBatchReq(&decoder, &req);
15,741✔
1620
  if (ret < 0) {
15,741!
1621
    terrno = TSDB_CODE_INVALID_MSG;
×
1622
    pRsp->code = terrno;
×
1623
    goto _exit;
×
1624
  }
1625

1626
  // process req
1627
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
15,741✔
1628
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(SVDropTbRsp));
15,741✔
1629
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
15,741✔
1630
  if (tbUids == NULL || rsp.pArray == NULL || tbNames == NULL) goto _exit;
15,741!
1631

1632
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
31,502✔
1633
    SVDropTbReq *pDropTbReq = req.pReqs + iReq;
15,761✔
1634
    SVDropTbRsp  dropTbRsp = {0};
15,761✔
1635
    tb_uid_t     tbUid = 0;
15,761✔
1636

1637
    /* code */
1638
    ret = metaDropTable2(pVnode->pMeta, ver, pDropTbReq);
15,761✔
1639
    if (ret < 0) {
15,761!
1640
      if (pDropTbReq->igNotExists && terrno == TSDB_CODE_TDB_TABLE_NOT_EXIST) {
×
1641
        dropTbRsp.code = TSDB_CODE_SUCCESS;
×
1642
      } else {
1643
        dropTbRsp.code = terrno;
×
1644
      }
1645
    } else {
1646
      dropTbRsp.code = TSDB_CODE_SUCCESS;
15,761✔
1647
      if (tbUid > 0) {
15,761!
1648
        if (tdFetchTbUidList(pVnode->pSma, &pStore, pDropTbReq->suid, tbUid) < 0) {
×
1649
          vError("vgId:%d, failed to fetch tbUid list", TD_VID(pVnode));
×
1650
        }
1651
      }
1652
    }
1653

1654
    if (taosArrayPush(rsp.pArray, &dropTbRsp) == NULL) {
31,522!
1655
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1656
      pRsp->code = terrno;
×
1657
      goto _exit;
×
1658
    }
1659

1660
    if (tsEnableAuditCreateTable) {
15,761!
1661
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
15,761!
1662
      if (str == NULL) {
15,761!
1663
        pRsp->code = terrno;
×
1664
        goto _exit;
×
1665
      }
1666
      tstrncpy(str, pDropTbReq->name, TSDB_TABLE_FNAME_LEN);
15,761✔
1667
      if (taosArrayPush(tbNames, &str) == NULL) {
15,761!
1668
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1669
        pRsp->code = terrno;
×
1670
        goto _exit;
×
1671
      }
1672
    }
1673
  }
1674

1675
  if (tqUpdateTbUidList(pVnode->pTq, tbUids, false) < 0) {
15,741!
1676
    vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(terrno));
×
1677
  }
1678

1679
  if (tdUpdateTbUidList(pVnode->pSma, pStore, false) < 0) {
15,741!
1680
    goto _exit;
×
1681
  }
1682

1683
  if (tsEnableAuditCreateTable) {
15,741!
1684
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
15,741✔
1685

1686
    SName name = {0};
15,741✔
1687
    if (tNameFromString(&name, pVnode->config.dbname, T_NAME_ACCT | T_NAME_DB) != 0) {
15,741!
1688
      vError("vgId:%d, failed to get name from string", TD_VID(pVnode));
×
1689
    }
1690

1691
    SStringBuilder sb = {0};
15,741✔
1692
    for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
31,502✔
1693
      char **key = (char **)taosArrayGet(tbNames, iReq);
15,760✔
1694
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
15,760✔
1695
      if (iReq < req.nReqs - 1) {
15,760✔
1696
        taosStringBuilderAppendChar(&sb, ',');
19✔
1697
      }
1698
      taosMemoryFreeClear(*key);
15,759!
1699
    }
1700

1701
    size_t len = 0;
15,742✔
1702
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
15,742✔
1703

1704
    if (pOriginRpc->info.conn.user != NULL && strlen(pOriginRpc->info.conn.user) > 0) {
15,741!
1705
      auditAddRecord(pOriginRpc, clusterId, "dropTable", name.dbname, "", keyJoined, len);
15,699✔
1706
    }
1707

1708
    taosStringBuilderDestroy(&sb);
15,741✔
1709
  }
1710

1711
_exit:
×
1712
  taosArrayDestroy(tbUids);
15,741✔
1713
  pStore = tdUidStoreFree(pStore);
15,741✔
1714
  tDecoderClear(&decoder);
15,741✔
1715
  tEncodeSize(tEncodeSVDropTbBatchRsp, &rsp, pRsp->contLen, ret);
15,741!
1716
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
15,741✔
1717
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
15,741✔
1718
  if (tEncodeSVDropTbBatchRsp(&encoder, &rsp) != 0) {
15,741!
1719
    vError("vgId:%d, failed to encode drop table batch response", TD_VID(pVnode));
×
1720
  }
1721
  tEncoderClear(&encoder);
15,741✔
1722
  taosArrayDestroy(rsp.pArray);
15,741✔
1723
  taosArrayDestroy(tbNames);
15,741✔
1724
  return 0;
15,741✔
1725
}
1726

1727
#ifdef BUILD_NO_CALL
1728
static int32_t vnodeDebugPrintSingleSubmitMsg(SMeta *pMeta, SSubmitBlk *pBlock, SSubmitMsgIter *msgIter,
1729
                                              const char *tags) {
1730
  SSubmitBlkIter blkIter = {0};
1731
  STSchema      *pSchema = NULL;
1732
  tb_uid_t       suid = 0;
1733
  STSRow        *row = NULL;
1734
  int32_t        rv = -1;
1735

1736
  tInitSubmitBlkIter(msgIter, pBlock, &blkIter);
1737
  if (blkIter.row == NULL) return 0;
1738

1739
  int32_t code = metaGetTbTSchemaNotNull(pMeta, msgIter->suid, TD_ROW_SVER(blkIter.row), 1,
1740
                                         &pSchema);  // TODO: use the real schema
1741
  if (TSDB_CODE_SUCCESS != code) {
1742
    printf("%s:%d no valid schema\n", tags, __LINE__);
1743
    return code;
1744
  }
1745

1746
  suid = msgIter->suid;
1747
  rv = TD_ROW_SVER(blkIter.row);
1748

1749
  char __tags[128] = {0};
1750
  snprintf(__tags, 128, "%s: uid %" PRIi64 " ", tags, msgIter->uid);
1751
  while ((row = tGetSubmitBlkNext(&blkIter))) {
1752
    tdSRowPrint(row, pSchema, __tags);
1753
  }
1754

1755
  taosMemoryFreeClear(pSchema);
1756

1757
  return TSDB_CODE_SUCCESS;
1758
}
1759
#endif
1760
typedef struct SSubmitReqConvertCxt {
1761
  SSubmitMsgIter msgIter;
1762
  SSubmitBlk    *pBlock;
1763
  SSubmitBlkIter blkIter;
1764
  STSRow        *pRow;
1765
  STSRowIter     rowIter;
1766
  SSubmitTbData *pTbData;
1767
  STSchema      *pTbSchema;
1768
  SArray        *pColValues;
1769
} SSubmitReqConvertCxt;
1770

1771
static int32_t vnodeResetTableCxt(SMeta *pMeta, SSubmitReqConvertCxt *pCxt) {
×
1772
  taosMemoryFreeClear(pCxt->pTbSchema);
×
1773
  int32_t code = metaGetTbTSchemaNotNull(pMeta, pCxt->msgIter.suid > 0 ? pCxt->msgIter.suid : pCxt->msgIter.uid,
×
1774
                                         pCxt->msgIter.sversion, 1, &pCxt->pTbSchema);
1775
  if (TSDB_CODE_SUCCESS != code) {
×
1776
    return code;
×
1777
  }
1778
  tdSTSRowIterInit(&pCxt->rowIter, pCxt->pTbSchema);
×
1779

1780
  tDestroySubmitTbData(pCxt->pTbData, TSDB_MSG_FLG_ENCODE);
×
1781
  if (NULL == pCxt->pTbData) {
×
1782
    pCxt->pTbData = taosMemoryCalloc(1, sizeof(SSubmitTbData));
×
1783
    if (NULL == pCxt->pTbData) {
×
1784
      return terrno;
×
1785
    }
1786
  }
1787
  pCxt->pTbData->flags = 0;
×
1788
  pCxt->pTbData->suid = pCxt->msgIter.suid;
×
1789
  pCxt->pTbData->uid = pCxt->msgIter.uid;
×
1790
  pCxt->pTbData->sver = pCxt->msgIter.sversion;
×
1791
  pCxt->pTbData->pCreateTbReq = NULL;
×
1792
  pCxt->pTbData->aRowP = taosArrayInit(128, POINTER_BYTES);
×
1793
  if (NULL == pCxt->pTbData->aRowP) {
×
1794
    return terrno;
×
1795
  }
1796

1797
  taosArrayDestroy(pCxt->pColValues);
×
1798
  pCxt->pColValues = taosArrayInit(pCxt->pTbSchema->numOfCols, sizeof(SColVal));
×
1799
  if (NULL == pCxt->pColValues) {
×
1800
    return terrno;
×
1801
  }
1802
  for (int32_t i = 0; i < pCxt->pTbSchema->numOfCols; ++i) {
×
1803
    SColVal val = COL_VAL_NONE(pCxt->pTbSchema->columns[i].colId, pCxt->pTbSchema->columns[i].type);
×
1804
    if (taosArrayPush(pCxt->pColValues, &val) == NULL) {
×
1805
      return terrno;
×
1806
    }
1807
  }
1808

1809
  return TSDB_CODE_SUCCESS;
×
1810
}
1811

1812
static void vnodeDestroySubmitReqConvertCxt(SSubmitReqConvertCxt *pCxt) {
×
1813
  taosMemoryFreeClear(pCxt->pTbSchema);
×
1814
  tDestroySubmitTbData(pCxt->pTbData, TSDB_MSG_FLG_ENCODE);
×
1815
  taosMemoryFreeClear(pCxt->pTbData);
×
1816
  taosArrayDestroy(pCxt->pColValues);
×
1817
}
×
1818

1819
static int32_t vnodeCellValConvertToColVal(STColumn *pCol, SCellVal *pCellVal, SColVal *pColVal) {
×
1820
  if (tdValTypeIsNone(pCellVal->valType)) {
×
1821
    pColVal->flag = CV_FLAG_NONE;
×
1822
    return TSDB_CODE_SUCCESS;
×
1823
  }
1824

1825
  if (tdValTypeIsNull(pCellVal->valType)) {
×
1826
    pColVal->flag = CV_FLAG_NULL;
×
1827
    return TSDB_CODE_SUCCESS;
×
1828
  }
1829

1830
  if (IS_VAR_DATA_TYPE(pCol->type)) {
×
1831
    pColVal->value.nData = varDataLen(pCellVal->val);
×
1832
    pColVal->value.pData = (uint8_t *)varDataVal(pCellVal->val);
×
1833
  } else if (TSDB_DATA_TYPE_FLOAT == pCol->type) {
×
1834
    float f = GET_FLOAT_VAL(pCellVal->val);
×
1835
    valueSetDatum(&pColVal->value, pCol->type, &f, sizeof(f));
×
1836
  } else if (TSDB_DATA_TYPE_DOUBLE == pCol->type) {
×
1837
    taosSetPInt64Aligned(&pColVal->value.val, (int64_t *)pCellVal->val);
×
1838
  } else {
1839
    valueSetDatum(&pColVal->value, pCol->type, pCellVal->val, tDataTypes[pCol->type].bytes);
×
1840
  }
1841

1842
  pColVal->flag = CV_FLAG_VALUE;
×
1843
  return TSDB_CODE_SUCCESS;
×
1844
}
1845

1846
static int32_t vnodeTSRowConvertToColValArray(SSubmitReqConvertCxt *pCxt) {
×
1847
  int32_t code = TSDB_CODE_SUCCESS;
×
1848
  tdSTSRowIterReset(&pCxt->rowIter, pCxt->pRow);
×
1849
  for (int32_t i = 0; TSDB_CODE_SUCCESS == code && i < pCxt->pTbSchema->numOfCols; ++i) {
×
1850
    STColumn *pCol = pCxt->pTbSchema->columns + i;
×
1851
    SCellVal  cellVal = {0};
×
1852
    if (!tdSTSRowIterFetch(&pCxt->rowIter, pCol->colId, pCol->type, &cellVal)) {
×
1853
      break;
×
1854
    }
1855
    code = vnodeCellValConvertToColVal(pCol, &cellVal, (SColVal *)taosArrayGet(pCxt->pColValues, i));
×
1856
  }
1857
  return code;
×
1858
}
1859

1860
static int32_t vnodeDecodeCreateTbReq(SSubmitReqConvertCxt *pCxt) {
×
1861
  if (pCxt->msgIter.schemaLen <= 0) {
×
1862
    return TSDB_CODE_SUCCESS;
×
1863
  }
1864

1865
  pCxt->pTbData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
×
1866
  if (NULL == pCxt->pTbData->pCreateTbReq) {
×
1867
    return terrno;
×
1868
  }
1869

1870
  SDecoder decoder = {0};
×
1871
  tDecoderInit(&decoder, (uint8_t *)pCxt->pBlock->data, pCxt->msgIter.schemaLen);
×
1872
  int32_t code = tDecodeSVCreateTbReq(&decoder, pCxt->pTbData->pCreateTbReq);
×
1873
  tDecoderClear(&decoder);
×
1874

1875
  return code;
×
1876
}
1877

1878
static int32_t vnodeSubmitReqConvertToSubmitReq2(SVnode *pVnode, SSubmitReq *pReq, SSubmitReq2 *pReq2) {
×
1879
  pReq2->aSubmitTbData = taosArrayInit(128, sizeof(SSubmitTbData));
×
1880
  if (NULL == pReq2->aSubmitTbData) {
×
1881
    return terrno;
×
1882
  }
1883

1884
  SSubmitReqConvertCxt cxt = {0};
×
1885

1886
  int32_t code = tInitSubmitMsgIter(pReq, &cxt.msgIter);
×
1887
  while (TSDB_CODE_SUCCESS == code) {
×
1888
    code = tGetSubmitMsgNext(&cxt.msgIter, &cxt.pBlock);
×
1889
    if (TSDB_CODE_SUCCESS == code) {
×
1890
      if (NULL == cxt.pBlock) {
×
1891
        break;
×
1892
      }
1893
      code = vnodeResetTableCxt(pVnode->pMeta, &cxt);
×
1894
    }
1895
    if (TSDB_CODE_SUCCESS == code) {
×
1896
      code = tInitSubmitBlkIter(&cxt.msgIter, cxt.pBlock, &cxt.blkIter);
×
1897
    }
1898
    if (TSDB_CODE_SUCCESS == code) {
×
1899
      code = vnodeDecodeCreateTbReq(&cxt);
×
1900
    }
1901
    while (TSDB_CODE_SUCCESS == code && (cxt.pRow = tGetSubmitBlkNext(&cxt.blkIter)) != NULL) {
×
1902
      code = vnodeTSRowConvertToColValArray(&cxt);
×
1903
      if (TSDB_CODE_SUCCESS == code) {
×
1904
        SRow **pNewRow = taosArrayReserve(cxt.pTbData->aRowP, 1);
×
1905
        code = tRowBuild(cxt.pColValues, cxt.pTbSchema, pNewRow);
×
1906
      }
1907
    }
1908
    if (TSDB_CODE_SUCCESS == code) {
×
1909
      code = (NULL == taosArrayPush(pReq2->aSubmitTbData, cxt.pTbData) ? terrno : TSDB_CODE_SUCCESS);
×
1910
    }
1911
    if (TSDB_CODE_SUCCESS == code) {
×
1912
      taosMemoryFreeClear(cxt.pTbData);
×
1913
    }
1914
  }
1915

1916
  vnodeDestroySubmitReqConvertCxt(&cxt);
×
1917
  return code;
×
1918
}
1919

1920
static int32_t vnodeRebuildSubmitReqMsg(SSubmitReq2 *pSubmitReq, void **ppMsg) {
×
1921
  int32_t  code = TSDB_CODE_SUCCESS;
×
1922
  char    *pMsg = NULL;
×
1923
  uint32_t msglen = 0;
×
1924
  tEncodeSize(tEncodeSubmitReq, pSubmitReq, msglen, code);
×
1925
  if (TSDB_CODE_SUCCESS == code) {
×
1926
    pMsg = taosMemoryMalloc(msglen);
×
1927
    if (NULL == pMsg) {
×
1928
      code = terrno;
×
1929
    }
1930
  }
1931
  if (TSDB_CODE_SUCCESS == code) {
×
1932
    SEncoder encoder;
1933
    tEncoderInit(&encoder, (uint8_t *)pMsg, msglen);
×
1934
    code = tEncodeSubmitReq(&encoder, pSubmitReq);
×
1935
    tEncoderClear(&encoder);
×
1936
  }
1937
  if (TSDB_CODE_SUCCESS == code) {
×
1938
    *ppMsg = pMsg;
×
1939
  }
1940
  return code;
×
1941
}
1942

1943
static int32_t vnodeProcessSubmitReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
13,903,177✔
1944
                                     SRpcMsg *pOriginalMsg) {
1945
  int32_t code = 0;
13,903,177✔
1946
  terrno = 0;
13,903,177✔
1947

1948
  SSubmitReq2 *pSubmitReq = &(SSubmitReq2){0};
13,903,474✔
1949
  SSubmitRsp2 *pSubmitRsp = &(SSubmitRsp2){0};
13,903,474✔
1950
  SArray      *newTbUids = NULL;
13,903,474✔
1951
  int32_t      ret;
1952
  SEncoder     ec = {0};
13,903,474✔
1953

1954
  pRsp->code = TSDB_CODE_SUCCESS;
13,903,474✔
1955

1956
  void           *pAllocMsg = NULL;
13,903,474✔
1957
  SSubmitReq2Msg *pMsg = (SSubmitReq2Msg *)pReq;
13,903,474✔
1958
  if (0 == pMsg->version) {
13,903,474!
1959
    code = vnodeSubmitReqConvertToSubmitReq2(pVnode, (SSubmitReq *)pMsg, pSubmitReq);
×
1960
    if (TSDB_CODE_SUCCESS == code) {
×
1961
      code = vnodeRebuildSubmitReqMsg(pSubmitReq, &pReq);
×
1962
    }
1963
    if (TSDB_CODE_SUCCESS == code) {
×
1964
      pAllocMsg = pReq;
×
1965
    }
1966
    if (TSDB_CODE_SUCCESS != code) {
×
1967
      goto _exit;
×
1968
    }
1969
  } else {
1970
    // decode
1971
    pReq = POINTER_SHIFT(pReq, sizeof(SSubmitReq2Msg));
13,903,474✔
1972
    len -= sizeof(SSubmitReq2Msg);
13,903,474✔
1973
    SDecoder dc = {0};
13,903,474✔
1974
    tDecoderInit(&dc, pReq, len);
13,903,474✔
1975
    if (tDecodeSubmitReq(&dc, pSubmitReq, NULL) < 0) {
13,903,286!
1976
      code = TSDB_CODE_INVALID_MSG;
×
1977
      goto _exit;
×
1978
    }
1979
    tDecoderClear(&dc);
13,903,428✔
1980
  }
1981

1982
  // scan
1983
  TSKEY now = taosGetTimestamp(pVnode->config.tsdbCfg.precision);
13,903,456✔
1984
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * pVnode->config.tsdbCfg.keep2;
13,903,530✔
1985
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
13,903,530✔
1986
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
28,527,061✔
1987
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
14,624,306✔
1988

1989
    if (pSubmitTbData->pCreateTbReq && pSubmitTbData->pCreateTbReq->uid == 0) {
14,624,345!
1990
      code = TSDB_CODE_INVALID_MSG;
×
1991
      goto _exit;
×
1992
    }
1993

1994
    if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
14,624,345✔
1995
      if (TARRAY_SIZE(pSubmitTbData->aCol) <= 0) {
13,731!
1996
        code = TSDB_CODE_INVALID_MSG;
×
1997
        goto _exit;
×
1998
      }
1999

2000
      SColData *colDataArr = TARRAY_DATA(pSubmitTbData->aCol);
13,731✔
2001
      SRowKey   lastKey;
2002
      tColDataArrGetRowKey(colDataArr, TARRAY_SIZE(pSubmitTbData->aCol), 0, &lastKey);
13,731✔
2003
      for (int32_t iRow = 1; iRow < colDataArr[0].nVal; iRow++) {
21,470,158✔
2004
        SRowKey key;
2005
        tColDataArrGetRowKey(TARRAY_DATA(pSubmitTbData->aCol), TARRAY_SIZE(pSubmitTbData->aCol), iRow, &key);
21,456,877✔
2006
        if (tRowKeyCompare(&lastKey, &key) >= 0) {
21,454,588!
2007
          code = TSDB_CODE_INVALID_MSG;
×
2008
          vError("vgId:%d %s failed 1 since %s, version:%" PRId64, TD_VID(pVnode), __func__, tstrerror(terrno), ver);
×
2009
          goto _exit;
×
2010
        }
2011
      }
2012
    } else {
2013
      int32_t nRow = TARRAY_SIZE(pSubmitTbData->aRowP);
14,610,614✔
2014
      SRow  **aRow = (SRow **)TARRAY_DATA(pSubmitTbData->aRowP);
14,610,614✔
2015
      SRowKey lastRowKey;
2016
      for (int32_t iRow = 0; iRow < nRow; ++iRow) {
732,469,532✔
2017
#ifndef NO_UNALIGNED_ACCESS
2018
        if (aRow[iRow]->ts < minKey || aRow[iRow]->ts > maxKey) {
717,859,282!
2019
#else
2020
        TSKEY ts = taosGetInt64Aligned(&(aRow[iRow]->ts));
2021
        if (ts < minKey || ts > maxKey) {
2022
#endif
2023
          code = TSDB_CODE_INVALID_MSG;
18,033✔
2024
          vError("vgId:%d %s failed 2 since %s, version:%" PRId64, TD_VID(pVnode), __func__, tstrerror(code), ver);
18,033!
2025
          goto _exit;
×
2026
        }
2027
        if (iRow == 0) {
717,841,249✔
2028
          tRowGetKey(aRow[iRow], &lastRowKey);
29,221,154✔
2029
        } else {
2030
          SRowKey rowKey;
2031
          tRowGetKey(aRow[iRow], &rowKey);
1,406,461,344✔
2032

2033
          if (tRowKeyCompare(&lastRowKey, &rowKey) >= 0) {
703,229,450!
2034
            code = TSDB_CODE_INVALID_MSG;
×
2035
            vError("vgId:%d %s failed 3 since %s, version:%" PRId64, TD_VID(pVnode), __func__, tstrerror(code), ver);
×
2036
            goto _exit;
×
2037
          }
2038
          lastRowKey = rowKey;
703,239,287✔
2039
        }
2040
      }
2041
    }
2042
  }
2043

2044
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
28,527,320✔
2045
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
14,624,251✔
2046

2047
    if (pSubmitTbData->pCreateTbReq) {
14,624,399✔
2048
      pSubmitTbData->uid = pSubmitTbData->pCreateTbReq->uid;
85,989✔
2049
    } else {
2050
      SMetaInfo info = {0};
14,538,410✔
2051

2052
      code = metaGetInfo(pVnode->pMeta, pSubmitTbData->uid, &info, NULL);
14,538,410✔
2053
      if (code) {
14,538,381!
2054
        code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
2055
        vWarn("vgId:%d, table uid:%" PRId64 " not exists", TD_VID(pVnode), pSubmitTbData->uid);
×
2056
        goto _exit;
1✔
2057
      }
2058

2059
      if (info.suid != pSubmitTbData->suid) {
14,538,419!
2060
        code = TSDB_CODE_INVALID_MSG;
×
2061
        goto _exit;
×
2062
      }
2063

2064
      if (info.suid) {
14,538,419✔
2065
        if (metaGetInfo(pVnode->pMeta, info.suid, &info, NULL) != 0) {
13,243,732!
2066
          vWarn("vgId:%d, table uid:%" PRId64 " not exists", TD_VID(pVnode), info.suid);
×
2067
        }
2068
      }
2069

2070
      if (pSubmitTbData->sver != info.skmVer) {
14,538,576!
2071
        code = TSDB_CODE_TDB_INVALID_TABLE_SCHEMA_VER;
×
2072
        goto _exit;
×
2073
      }
2074
    }
2075

2076
    if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
14,624,565✔
2077
      int32_t   nColData = TARRAY_SIZE(pSubmitTbData->aCol);
13,730✔
2078
      SColData *aColData = (SColData *)TARRAY_DATA(pSubmitTbData->aCol);
13,730✔
2079

2080
      if (nColData <= 0) {
13,730!
2081
        code = TSDB_CODE_INVALID_MSG;
×
2082
        goto _exit;
×
2083
      }
2084

2085
      if (aColData[0].cid != PRIMARYKEY_TIMESTAMP_COL_ID || aColData[0].type != TSDB_DATA_TYPE_TIMESTAMP ||
13,730!
2086
          aColData[0].nVal <= 0) {
13,729!
2087
        code = TSDB_CODE_INVALID_MSG;
×
2088
        goto _exit;
×
2089
      }
2090

2091
      for (int32_t j = 1; j < nColData; j++) {
41,396✔
2092
        if (aColData[j].nVal != aColData[0].nVal) {
27,666!
2093
          code = TSDB_CODE_INVALID_MSG;
×
2094
          goto _exit;
×
2095
        }
2096
      }
2097
    }
2098
  }
2099

2100
  vGDebug(pOriginalMsg ? &pOriginalMsg->info.traceId : NULL, "vgId:%d, index:%" PRId64 ", submit block, rows:%d",
13,903,069!
2101
          TD_VID(pVnode), ver, (int32_t)taosArrayGetSize(pSubmitReq->aSubmitTbData));
2102

2103
  // loop to handle
2104
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
28,528,004✔
2105
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
14,624,447✔
2106

2107
    // create table
2108
    if (pSubmitTbData->pCreateTbReq) {
14,624,461✔
2109
      // alloc if need
2110
      if (pSubmitRsp->aCreateTbRsp == NULL &&
86,033✔
2111
          (pSubmitRsp->aCreateTbRsp = taosArrayInit(TARRAY_SIZE(pSubmitReq->aSubmitTbData), sizeof(SVCreateTbRsp))) ==
71,141!
2112
              NULL) {
2113
        code = terrno;
×
2114
        goto _exit;
8✔
2115
      }
2116

2117
      SVCreateTbRsp *pCreateTbRsp = taosArrayReserve(pSubmitRsp->aCreateTbRsp, 1);
86,034✔
2118

2119
      // create table
2120
      if (metaCreateTable2(pVnode->pMeta, ver, pSubmitTbData->pCreateTbReq, &pCreateTbRsp->pMeta) == 0) {
86,030✔
2121
        // create table success
2122

2123
        if (newTbUids == NULL &&
144,713!
2124
            (newTbUids = taosArrayInit(TARRAY_SIZE(pSubmitReq->aSubmitTbData), sizeof(int64_t))) == NULL) {
67,827✔
2125
          code = terrno;
×
2126
          goto _exit;
×
2127
        }
2128

2129
        if (taosArrayPush(newTbUids, &pSubmitTbData->uid) == NULL) {
153,772!
2130
          code = terrno;
×
2131
          goto _exit;
×
2132
        }
2133

2134
        if (pCreateTbRsp->pMeta) {
76,886!
2135
          vnodeUpdateMetaRsp(pVnode, pCreateTbRsp->pMeta);
76,886✔
2136
        }
2137
      } else {  // create table failed
2138
        if (terrno != TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
9,150✔
2139
          code = terrno;
8✔
2140
          vError("vgId:%d failed to create table:%s, code:%s", TD_VID(pVnode), pSubmitTbData->pCreateTbReq->name,
8!
2141
                 tstrerror(terrno));
2142
          goto _exit;
8✔
2143
        }
2144
        terrno = 0;
9,142✔
2145
        pSubmitTbData->uid = pSubmitTbData->pCreateTbReq->uid;  // update uid if table exist for using below
9,142✔
2146
      }
2147
    }
2148

2149
    // insert data
2150
    int32_t affectedRows;
2151
    code = tsdbInsertTableData(pVnode->pTsdb, ver, pSubmitTbData, &affectedRows);
14,624,456✔
2152
    if (code) goto _exit;
14,624,402!
2153

2154
    code = metaUpdateChangeTimeWithLock(pVnode->pMeta, pSubmitTbData->uid, pSubmitTbData->ctimeMs);
14,624,402✔
2155
    if (code) goto _exit;
14,624,543!
2156

2157
    pSubmitRsp->affectedRows += affectedRows;
14,624,543✔
2158
  }
2159

2160
  // update the affected table uid list
2161
  if (taosArrayGetSize(newTbUids) > 0) {
13,903,557✔
2162
    vDebug("vgId:%d, add %d table into query table list in handling submit", TD_VID(pVnode),
67,822✔
2163
           (int32_t)taosArrayGetSize(newTbUids));
2164
    if (tqUpdateTbUidList(pVnode->pTq, newTbUids, true) != 0) {
67,826!
2165
      vError("vgId:%d, failed to update tbUid list", TD_VID(pVnode));
×
2166
    }
2167
  }
2168

2169
_exit:
13,903,515✔
2170
  // message
2171
  pRsp->code = code;
13,903,524✔
2172
  tEncodeSize(tEncodeSSubmitRsp2, pSubmitRsp, pRsp->contLen, ret);
13,903,524!
2173
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
13,903,330✔
2174
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
13,903,530✔
2175
  if (tEncodeSSubmitRsp2(&ec, pSubmitRsp) < 0) {
13,903,498!
2176
    vError("vgId:%d, failed to encode submit response", TD_VID(pVnode));
×
2177
  }
2178
  tEncoderClear(&ec);
13,903,515✔
2179

2180
  // update statistics
2181
  (void)atomic_add_fetch_64(&pVnode->statis.nInsert, pSubmitRsp->affectedRows);
13,903,564✔
2182
  (void)atomic_add_fetch_64(&pVnode->statis.nInsertSuccess, pSubmitRsp->affectedRows);
13,903,599✔
2183
  (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsert, 1);
13,903,598✔
2184

2185
  // update metrics
2186
  METRICS_UPDATE(pVnode->writeMetrics.total_requests, METRIC_LEVEL_LOW, 1);
13,903,606!
2187
  METRICS_UPDATE(pVnode->writeMetrics.total_rows, METRIC_LEVEL_HIGH, pSubmitRsp->affectedRows);
13,903,606!
2188
  METRICS_UPDATE(pVnode->writeMetrics.total_bytes, METRIC_LEVEL_LOW, pMsg->header.contLen);
13,903,606!
2189

2190
  if (tsEnableMonitor && tsMonitorFqdn[0] != 0 && tsMonitorPort != 0 && pSubmitRsp->affectedRows > 0 &&
13,903,606!
2191
      strlen(pOriginalMsg->info.conn.user) > 0 && tsInsertCounter != NULL) {
100!
2192
    const char *sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT_AFFECTED_ROWS,
100✔
2193
                                   pVnode->monitor.strClusterId,
100✔
2194
                                   pVnode->monitor.strDnodeId,
100✔
2195
                                   tsLocalEp,
2196
                                   pVnode->monitor.strVgId,
100✔
2197
                                   pOriginalMsg->info.conn.user,
100✔
2198
                                   "Success"};
2199
    int         tv = taos_counter_add(tsInsertCounter, pSubmitRsp->affectedRows, sample_labels);
100✔
2200
  }
2201

2202
  if (code == 0) {
13,903,606✔
2203
    (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
13,903,266✔
2204
    code = tdProcessRSmaSubmit(pVnode->pSma, ver, pSubmitReq, pReq, len);
13,903,594✔
2205
  }
2206
  /*
2207
  if (code == 0) {
2208
    atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
2209
    code = tdProcessRSmaSubmit(pVnode->pSma, ver, pSubmitReq, pReq, len);
2210

2211
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT, pVnode->monitor.strClusterId,
2212
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2213
                                          pOriginalMsg->info.conn.user, "Success"};
2214
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2215
  }
2216
  else{
2217
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT, pVnode->monitor.strClusterId,
2218
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2219
                                        pOriginalMsg->info.conn.user, "Failed"};
2220
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2221
  }
2222
  */
2223

2224
  // clear
2225
  taosArrayDestroy(newTbUids);
13,903,807✔
2226
  tDestroySubmitReq(pSubmitReq, 0 == pMsg->version ? TSDB_MSG_FLG_CMPT : TSDB_MSG_FLG_DECODE);
13,903,296!
2227
  tDestroySSubmitRsp2(pSubmitRsp, TSDB_MSG_FLG_ENCODE);
13,903,562✔
2228

2229
  if (code) terrno = code;
13,903,440✔
2230

2231
  taosMemoryFree(pAllocMsg);
13,903,440!
2232

2233
  return code;
13,903,492✔
2234
}
2235

2236
static int32_t vnodeProcessCreateTSmaReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
2237
#ifdef USE_TSMA
2238
  SVCreateTSmaReq req = {0};
×
2239
  SDecoder        coder = {0};
×
2240

2241
  if (pRsp) {
×
2242
    pRsp->msgType = TDMT_VND_CREATE_SMA_RSP;
×
2243
    pRsp->code = TSDB_CODE_SUCCESS;
×
2244
    pRsp->pCont = NULL;
×
2245
    pRsp->contLen = 0;
×
2246
  }
2247

2248
  // decode and process req
2249
  tDecoderInit(&coder, pReq, len);
×
2250

2251
  if (tDecodeSVCreateTSmaReq(&coder, &req) < 0) {
×
2252
    terrno = TSDB_CODE_MSG_DECODE_ERROR;
×
2253
    if (pRsp) pRsp->code = terrno;
×
2254
    goto _err;
×
2255
  }
2256

2257
  if (tdProcessTSmaCreate(pVnode->pSma, ver, (const char *)&req) < 0) {
×
2258
    if (pRsp) pRsp->code = terrno;
×
2259
    goto _err;
×
2260
  }
2261

2262
  tDecoderClear(&coder);
×
2263
  vDebug("vgId:%d, success to create tsma %s:%" PRIi64 " version %" PRIi64 " for table %" PRIi64, TD_VID(pVnode),
×
2264
         req.indexName, req.indexUid, ver, req.tableUid);
2265
  return 0;
×
2266

2267
_err:
×
2268
  tDecoderClear(&coder);
×
2269
  vError("vgId:%d, failed to create tsma %s:%" PRIi64 " version %" PRIi64 "for table %" PRIi64 " since %s",
×
2270
         TD_VID(pVnode), req.indexName, req.indexUid, ver, req.tableUid, terrstr());
2271
  return terrno;
×
2272
#else
2273
  return TSDB_CODE_INTERNAL_ERROR;
2274
#endif
2275
}
2276

2277
/**
2278
 * @brief specific for smaDstVnode
2279
 *
2280
 * @param pVnode
2281
 * @param pCont
2282
 * @param contLen
2283
 * @return int32_t
2284
 */
2285
int32_t vnodeProcessCreateTSma(SVnode *pVnode, void *pCont, uint32_t contLen) {
×
2286
  return vnodeProcessCreateTSmaReq(pVnode, 1, pCont, contLen, NULL);
×
2287
}
2288

2289
static int32_t vnodeConsolidateAlterHashRange(SVnode *pVnode, int64_t ver) {
44✔
2290
  int32_t code = TSDB_CODE_SUCCESS;
44✔
2291

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

2295
  // TODO: trim meta of tables from TDB per hash range [pVnode->config.hashBegin, pVnode->config.hashEnd]
2296
  code = metaTrimTables(pVnode->pMeta, ver);
44✔
2297

2298
  return code;
44✔
2299
}
2300

2301
static int32_t vnodeProcessAlterConfirmReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
15,093✔
2302
  vInfo("vgId:%d, vnode handle msgType:alter-confirm, alter confirm msg is processed", TD_VID(pVnode));
15,093!
2303
  int32_t code = TSDB_CODE_SUCCESS;
15,093✔
2304
  if (!pVnode->config.hashChange) {
15,093✔
2305
    goto _exit;
15,049✔
2306
  }
2307

2308
  code = vnodeConsolidateAlterHashRange(pVnode, ver);
44✔
2309
  if (code < 0) {
44!
2310
    vError("vgId:%d, failed to consolidate alter hashrange since %s. version:%" PRId64, TD_VID(pVnode), terrstr(), ver);
×
2311
    goto _exit;
×
2312
  }
2313
  pVnode->config.hashChange = false;
44✔
2314

2315
_exit:
15,093✔
2316
  pRsp->msgType = TDMT_VND_ALTER_CONFIRM_RSP;
15,093✔
2317
  pRsp->code = code;
15,093✔
2318
  pRsp->pCont = NULL;
15,093✔
2319
  pRsp->contLen = 0;
15,093✔
2320

2321
  return code;
15,093✔
2322
}
2323

2324
extern int32_t tsdbDisableAndCancelAllBgTask(STsdb *pTsdb);
2325
extern void    tsdbEnableBgTask(STsdb *pTsdb);
2326

2327
static int32_t vnodeProcessAlterConfigReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
976✔
2328
  bool walChanged = false;
976✔
2329
  bool tsdbChanged = false;
976✔
2330

2331
  SAlterVnodeConfigReq req = {0};
976✔
2332
  if (tDeserializeSAlterVnodeConfigReq(pReq, len, &req) != 0) {
976!
2333
    terrno = TSDB_CODE_INVALID_MSG;
×
2334
    return TSDB_CODE_INVALID_MSG;
×
2335
  }
2336

2337
  vInfo("vgId:%d, start to alter vnode config, page:%d pageSize:%d buffer:%d szPage:%d szBuf:%" PRIu64
976!
2338
        " cacheLast:%d cacheLastSize:%d days:%d keep0:%d keep1:%d keep2:%d keepTimeOffset:%d s3KeepLocal:%d "
2339
        "s3Compact:%d fsync:%d level:%d "
2340
        "walRetentionPeriod:%d walRetentionSize:%d",
2341
        TD_VID(pVnode), req.pages, req.pageSize, req.buffer, req.pageSize * 1024, (uint64_t)req.buffer * 1024 * 1024,
2342
        req.cacheLast, req.cacheLastSize, req.daysPerFile, req.daysToKeep0, req.daysToKeep1, req.daysToKeep2,
2343
        req.keepTimeOffset, req.s3KeepLocal, req.s3Compact, req.walFsyncPeriod, req.walLevel, req.walRetentionPeriod,
2344
        req.walRetentionSize);
2345

2346
  if (pVnode->config.cacheLastSize != req.cacheLastSize) {
977✔
2347
    pVnode->config.cacheLastSize = req.cacheLastSize;
374✔
2348
    tsdbCacheSetCapacity(pVnode, (size_t)pVnode->config.cacheLastSize * 1024 * 1024);
374✔
2349
  }
2350

2351
  if (pVnode->config.szBuf != req.buffer * 1024LL * 1024LL) {
977!
2352
    vInfo("vgId:%d, vnode buffer is changed from %" PRId64 " to %" PRId64, TD_VID(pVnode), pVnode->config.szBuf,
×
2353
          (uint64_t)(req.buffer * 1024LL * 1024LL));
2354
    pVnode->config.szBuf = req.buffer * 1024LL * 1024LL;
×
2355
  }
2356

2357
  if (pVnode->config.szCache != req.pages) {
977!
2358
    if ((terrno = metaAlterCache(pVnode->pMeta, req.pages)) < 0) {
×
2359
      vError("vgId:%d, failed to change vnode pages from %d to %d failed since %s", TD_VID(pVnode),
×
2360
             pVnode->config.szCache, req.pages, tstrerror(terrno));
2361
      return terrno;
×
2362
    } else {
2363
      vInfo("vgId:%d, vnode pages is changed from %d to %d", TD_VID(pVnode), pVnode->config.szCache, req.pages);
×
2364
      pVnode->config.szCache = req.pages;
×
2365
    }
2366
  }
2367

2368
  if (pVnode->config.cacheLast != req.cacheLast) {
977✔
2369
    pVnode->config.cacheLast = req.cacheLast;
621✔
2370
  }
2371

2372
  if (pVnode->config.walCfg.fsyncPeriod != req.walFsyncPeriod) {
977✔
2373
    pVnode->config.walCfg.fsyncPeriod = req.walFsyncPeriod;
90✔
2374
    walChanged = true;
90✔
2375
  }
2376

2377
  if (pVnode->config.walCfg.level != req.walLevel) {
977✔
2378
    if (pVnode->config.walCfg.level == 0) {
60!
2379
      pVnode->config.walCfg.clearFiles = 1;
×
2380
    }
2381
    pVnode->config.walCfg.level = req.walLevel;
60✔
2382
    walChanged = true;
60✔
2383
  }
2384

2385
  if (pVnode->config.walCfg.retentionPeriod != req.walRetentionPeriod) {
977✔
2386
    pVnode->config.walCfg.retentionPeriod = req.walRetentionPeriod;
132✔
2387
    walChanged = true;
132✔
2388
  }
2389

2390
  if (pVnode->config.walCfg.retentionSize != req.walRetentionSize) {
977!
2391
    pVnode->config.walCfg.retentionSize = req.walRetentionSize;
×
2392
    walChanged = true;
×
2393
  }
2394

2395
  if (pVnode->config.tsdbCfg.keep0 != req.daysToKeep0) {
977✔
2396
    pVnode->config.tsdbCfg.keep0 = req.daysToKeep0;
68✔
2397
    if (!VND_IS_RSMA(pVnode)) {
68!
2398
      tsdbChanged = true;
68✔
2399
    }
2400
  }
2401

2402
  if (pVnode->config.tsdbCfg.keep1 != req.daysToKeep1) {
977✔
2403
    pVnode->config.tsdbCfg.keep1 = req.daysToKeep1;
68✔
2404
    if (!VND_IS_RSMA(pVnode)) {
68!
2405
      tsdbChanged = true;
68✔
2406
    }
2407
  }
2408

2409
  if (pVnode->config.tsdbCfg.keep2 != req.daysToKeep2) {
977✔
2410
    pVnode->config.tsdbCfg.keep2 = req.daysToKeep2;
70✔
2411
    if (!VND_IS_RSMA(pVnode)) {
70!
2412
      tsdbChanged = true;
70✔
2413
    }
2414
  }
2415

2416
  if (pVnode->config.tsdbCfg.keepTimeOffset != req.keepTimeOffset) {
977✔
2417
    pVnode->config.tsdbCfg.keepTimeOffset = req.keepTimeOffset;
2✔
2418
    if (!VND_IS_RSMA(pVnode)) {
2!
2419
      tsdbChanged = true;
2✔
2420
    }
2421
  }
2422

2423
  if (req.sttTrigger != -1 && req.sttTrigger != pVnode->config.sttTrigger) {
977!
2424
    if (req.sttTrigger > 1 && pVnode->config.sttTrigger > 1) {
×
2425
      pVnode->config.sttTrigger = req.sttTrigger;
×
2426
    } else {
2427
      vnodeAWait(&pVnode->commitTask);
×
2428

2429
      int32_t ret = tsdbDisableAndCancelAllBgTask(pVnode->pTsdb);
×
2430
      if (ret != 0) {
×
2431
        vError("vgId:%d, failed to disable bg task since %s", TD_VID(pVnode), tstrerror(ERRNO));
×
2432
      }
2433

2434
      pVnode->config.sttTrigger = req.sttTrigger;
×
2435
      tsdbEnableBgTask(pVnode->pTsdb);
×
2436
    }
2437
  }
2438

2439
  if (req.minRows != -1 && req.minRows != pVnode->config.tsdbCfg.minRows) {
977!
2440
    pVnode->config.tsdbCfg.minRows = req.minRows;
×
2441
  }
2442

2443
  if (req.s3KeepLocal != -1 && req.s3KeepLocal != pVnode->config.s3KeepLocal) {
977!
2444
    pVnode->config.s3KeepLocal = req.s3KeepLocal;
×
2445
  }
2446
  if (req.s3Compact != -1 && req.s3Compact != pVnode->config.s3Compact) {
977!
2447
    pVnode->config.s3Compact = req.s3Compact;
×
2448
  }
2449

2450
  if (walChanged) {
977✔
2451
    if (walAlter(pVnode->pWal, &pVnode->config.walCfg) != 0) {
282!
2452
      vError("vgId:%d, failed to alter wal config since %s", TD_VID(pVnode), tstrerror(ERRNO));
×
2453
    }
2454
  }
2455

2456
  if (tsdbChanged) {
977✔
2457
    tsdbSetKeepCfg(pVnode->pTsdb, &pVnode->config.tsdbCfg);
72✔
2458
  }
2459

2460
  return 0;
977✔
2461
}
2462

2463
static int32_t vnodeProcessBatchDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
2,301✔
2464
  SBatchDeleteReq deleteReq;
2465
  SDecoder        decoder;
2466
  tDecoderInit(&decoder, pReq, len);
2,301✔
2467
  if (tDecodeSBatchDeleteReq(&decoder, &deleteReq) < 0) {
2,301!
2468
    tDecoderClear(&decoder);
×
2469
    return terrno = TSDB_CODE_INVALID_MSG;
×
2470
  }
2471

2472
  SMetaReader mr = {0};
2,305✔
2473
  metaReaderDoInit(&mr, pVnode->pMeta, META_READER_NOLOCK);
2,305✔
2474
  STsdb *pTsdb = pVnode->pTsdb;
2,302✔
2475

2476
  if (deleteReq.level) {
2,302✔
2477
    pTsdb = deleteReq.level == 1 ? VND_RSMA1(pVnode) : VND_RSMA2(pVnode);
16✔
2478
  }
2479

2480
  int32_t sz = taosArrayGetSize(deleteReq.deleteReqs);
2,302✔
2481
  for (int32_t i = 0; i < sz; i++) {
5,540✔
2482
    SSingleDeleteReq *pOneReq = taosArrayGet(deleteReq.deleteReqs, i);
3,236✔
2483
    char             *name = pOneReq->tbname;
3,236✔
2484
    if (metaGetTableEntryByName(&mr, name) < 0) {
3,236✔
2485
      vDebug("vgId:%d, stream delete msg, skip since no table: %s", pVnode->config.vgId, name);
1,605✔
2486
      continue;
1,605✔
2487
    }
2488

2489
    int64_t uid = mr.me.uid;
1,631✔
2490

2491
    int32_t code = tsdbDeleteTableData(pTsdb, ver, deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
1,631✔
2492
    if (code < 0) {
1,632!
2493
      terrno = code;
×
2494
      vError("vgId:%d, delete error since %s, suid:%" PRId64 ", uid:%" PRId64 ", start ts:%" PRId64 ", end ts:%" PRId64,
×
2495
             TD_VID(pVnode), terrstr(), deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
2496
    }
2497

2498
    if (deleteReq.level == 0) {
1,632✔
2499
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, deleteReq.ctimeMs);
1,016✔
2500
      if (code < 0) {
1,017!
2501
        terrno = code;
×
2502
        vError("vgId:%d, update change time error since %s, suid:%" PRId64 ", uid:%" PRId64 ", start ts:%" PRId64
×
2503
               ", end ts:%" PRId64,
2504
               TD_VID(pVnode), terrstr(), deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
2505
      }
2506
    }
2507
    tDecoderClear(&mr.coder);
1,633✔
2508
  }
2509
  metaReaderClear(&mr);
2,304✔
2510
  taosArrayDestroy(deleteReq.deleteReqs);
2,305✔
2511
  return 0;
2,306✔
2512
}
2513

2514
static int32_t vnodeProcessDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
59,351✔
2515
                                     SRpcMsg *pOriginalMsg) {
2516
  int32_t     code = 0;
59,351✔
2517
  SDecoder   *pCoder = &(SDecoder){0};
59,351✔
2518
  SDeleteRes *pRes = &(SDeleteRes){0};
59,351✔
2519

2520
  pRsp->msgType = TDMT_VND_DELETE_RSP;
59,351✔
2521
  pRsp->pCont = NULL;
59,351✔
2522
  pRsp->contLen = 0;
59,351✔
2523
  pRsp->code = TSDB_CODE_SUCCESS;
59,351✔
2524

2525
  pRes->uidList = taosArrayInit(0, sizeof(tb_uid_t));
59,351✔
2526
  if (pRes->uidList == NULL) {
59,352!
2527
    code = terrno;
×
2528
    goto _err;
×
2529
  }
2530

2531
  tDecoderInit(pCoder, pReq, len);
59,352✔
2532
  code = tDecodeDeleteRes(pCoder, pRes);
59,350✔
2533
  if (code) goto _err;
59,354!
2534

2535
  if (pRes->affectedRows > 0) {
59,354✔
2536
    for (int32_t iUid = 0; iUid < taosArrayGetSize(pRes->uidList); iUid++) {
113,251✔
2537
      uint64_t uid = *(uint64_t *)taosArrayGet(pRes->uidList, iUid);
58,771✔
2538
      code = tsdbDeleteTableData(pVnode->pTsdb, ver, pRes->suid, uid, pRes->skey, pRes->ekey);
58,770✔
2539
      if (code) goto _err;
58,771!
2540
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, pRes->ctimeMs);
58,771✔
2541
      if (code) goto _err;
58,771!
2542
    }
2543
  }
2544

2545
  code = tdProcessRSmaDelete(pVnode->pSma, ver, pRes, pReq, len);
59,352✔
2546

2547
  tDecoderClear(pCoder);
59,353✔
2548
  taosArrayDestroy(pRes->uidList);
59,354✔
2549

2550
  SVDeleteRsp rsp = {.affectedRows = pRes->affectedRows};
59,355✔
2551
  int32_t     ret = 0;
59,355✔
2552
  tEncodeSize(tEncodeSVDeleteRsp, &rsp, pRsp->contLen, ret);
59,355!
2553
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
59,351✔
2554
  SEncoder ec = {0};
59,354✔
2555
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
59,354✔
2556
  code = tEncodeSVDeleteRsp(&ec, &rsp);
59,355✔
2557
  if (code) goto _err;
59,353!
2558
  tEncoderClear(&ec);
59,353✔
2559
  return code;
59,354✔
2560

2561
_err:
×
2562
  /*
2563
  if(code == TSDB_CODE_SUCCESS){
2564
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_DELETE, pVnode->monitor.strClusterId,
2565
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2566
                                        pOriginalMsg->info.conn.user, "Success"};
2567
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2568
  }
2569
  else{
2570
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_DELETE, pVnode->monitor.strClusterId,
2571
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2572
                                        pOriginalMsg->info.conn.user, "Failed"};
2573
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2574
  }
2575
  */
2576

2577
  return code;
×
2578
}
2579
static int32_t vnodeProcessCreateIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
946✔
2580
  SVCreateStbReq req = {0};
946✔
2581
  SDecoder       dc = {0};
946✔
2582
  int32_t        code = 0;
946✔
2583

2584
  pRsp->msgType = TDMT_VND_CREATE_INDEX_RSP;
946✔
2585
  pRsp->code = TSDB_CODE_SUCCESS;
946✔
2586
  pRsp->pCont = NULL;
946✔
2587
  pRsp->contLen = 0;
946✔
2588

2589
  tDecoderInit(&dc, pReq, len);
946✔
2590
  // decode req
2591
  if (tDecodeSVCreateStbReq(&dc, &req) < 0) {
946!
2592
    tDecoderClear(&dc);
×
2593
    return terrno = TSDB_CODE_INVALID_MSG;
×
2594
  }
2595

2596
  code = metaAddIndexToSuperTable(pVnode->pMeta, ver, &req);
946✔
2597
  if (code) {
946!
2598
    pRsp->code = code;
×
2599
    goto _err;
×
2600
  }
2601
  tDecoderClear(&dc);
946✔
2602
  return 0;
946✔
2603

2604
_err:
×
2605
  tDecoderClear(&dc);
×
2606
  return code;
×
2607
}
2608
static int32_t vnodeProcessDropIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
2,128✔
2609
  SDropIndexReq req = {0};
2,128✔
2610
  int32_t       code = 0;
2,128✔
2611
  pRsp->msgType = TDMT_VND_DROP_INDEX_RSP;
2,128✔
2612
  pRsp->code = TSDB_CODE_SUCCESS;
2,128✔
2613
  pRsp->pCont = NULL;
2,128✔
2614
  pRsp->contLen = 0;
2,128✔
2615

2616
  if ((code = tDeserializeSDropIdxReq(pReq, len, &req))) {
2,128!
2617
    pRsp->code = code;
×
2618
    return code;
×
2619
  }
2620

2621
  code = metaDropIndexFromSuperTable(pVnode->pMeta, ver, &req);
2,128✔
2622
  if (code) {
2,128!
2623
    pRsp->code = code;
×
2624
    return code;
×
2625
  }
2626
  return TSDB_CODE_SUCCESS;
2,128✔
2627
}
2628

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

2631
static int32_t vnodeProcessCompactVnodeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
204✔
2632
  if (!pVnode->restored) {
204✔
2633
    vInfo("vgId:%d, ignore compact req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
174!
2634
    return 0;
174✔
2635
  }
2636
  return vnodeAsyncCompact(pVnode, ver, pReq, len, pRsp);
30✔
2637
}
2638

2639
static int32_t vnodeProcessConfigChangeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
2640
  if (syncCheckMember(pVnode->sync) != 0) {
×
2641
    vError("vgId:%d, failed to check member", TD_VID(pVnode));
×
2642
  }
2643

2644
  pRsp->msgType = TDMT_SYNC_CONFIG_CHANGE_RSP;
×
2645
  pRsp->code = TSDB_CODE_SUCCESS;
×
2646
  pRsp->pCont = NULL;
×
2647
  pRsp->contLen = 0;
×
2648

2649
  return 0;
×
2650
}
2651

2652
static int32_t vnodeCheckToken(SVnode *pVnode, char *member0Token, char *member1Token) {
45✔
2653
  SSyncState syncState = syncGetState(pVnode->sync);
45✔
2654
  if (syncState.state != TAOS_SYNC_STATE_LEADER) {
45✔
2655
    return terrno = TSDB_CODE_SYN_NOT_LEADER;
10✔
2656
  }
2657

2658
  char token[TSDB_ARB_TOKEN_SIZE] = {0};
35✔
2659
  if (vnodeGetArbToken(pVnode, token) != 0) {
35!
2660
    return terrno = TSDB_CODE_NOT_FOUND;
×
2661
  }
2662

2663
  if (strncmp(token, member0Token, TSDB_ARB_TOKEN_SIZE) != 0 &&
35✔
2664
      strncmp(token, member1Token, TSDB_ARB_TOKEN_SIZE) != 0) {
29✔
2665
    return terrno = TSDB_CODE_MND_ARB_TOKEN_MISMATCH;
27✔
2666
  }
2667

2668
  terrno = TSDB_CODE_SUCCESS;
8✔
2669
  return 0;
8✔
2670
}
2671

2672
static int32_t vnodeCheckSyncd(SVnode *pVnode, char *member0Token, char *member1Token) {
8✔
2673
  int32_t code = vnodeCheckToken(pVnode, member0Token, member1Token);
8✔
2674
  if (code != 0) {
8✔
2675
    return code;
4✔
2676
  }
2677

2678
  return syncCheckSynced(pVnode->sync);
4✔
2679
}
2680

2681
static int32_t vnodeProcessArbCheckSyncReq(SVnode *pVnode, void *pReq, int32_t len, SRpcMsg *pRsp) {
8✔
2682
  int32_t code = 0;
8✔
2683

2684
  SVArbCheckSyncReq syncReq = {0};
8✔
2685

2686
  code = tDeserializeSVArbCheckSyncReq(pReq, len, &syncReq);
8✔
2687
  if (code) {
8!
2688
    return terrno = code;
×
2689
  }
2690

2691
  pRsp->msgType = TDMT_VND_ARB_CHECK_SYNC_RSP;
8✔
2692
  pRsp->code = TSDB_CODE_SUCCESS;
8✔
2693
  pRsp->pCont = NULL;
8✔
2694
  pRsp->contLen = 0;
8✔
2695

2696
  SVArbCheckSyncRsp syncRsp = {0};
8✔
2697
  syncRsp.arbToken = syncReq.arbToken;
8✔
2698
  syncRsp.member0Token = syncReq.member0Token;
8✔
2699
  syncRsp.member1Token = syncReq.member1Token;
8✔
2700
  syncRsp.vgId = TD_VID(pVnode);
8✔
2701

2702
  if (vnodeCheckSyncd(pVnode, syncReq.member0Token, syncReq.member1Token) != 0) {
8✔
2703
    vError("vgId:%d, failed to check assigned log syncd", TD_VID(pVnode));
4!
2704
  }
2705
  syncRsp.errCode = terrno;
8✔
2706

2707
  if (vnodeUpdateArbTerm(pVnode, syncReq.arbTerm) != 0) {
8!
2708
    vError("vgId:%d, failed to update arb term", TD_VID(pVnode));
×
2709
    code = -1;
×
2710
    goto _OVER;
×
2711
  }
2712

2713
  int32_t contLen = tSerializeSVArbCheckSyncRsp(NULL, 0, &syncRsp);
8✔
2714
  if (contLen <= 0) {
8!
2715
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2716
    code = -1;
×
2717
    goto _OVER;
×
2718
  }
2719
  void *pHead = rpcMallocCont(contLen);
8✔
2720
  if (!pHead) {
8!
2721
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2722
    code = -1;
×
2723
    goto _OVER;
×
2724
  }
2725

2726
  if (tSerializeSVArbCheckSyncRsp(pHead, contLen, &syncRsp) <= 0) {
8!
2727
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2728
    rpcFreeCont(pHead);
×
2729
    code = -1;
×
2730
    goto _OVER;
×
2731
  }
2732

2733
  pRsp->pCont = pHead;
8✔
2734
  pRsp->contLen = contLen;
8✔
2735

2736
  terrno = TSDB_CODE_SUCCESS;
8✔
2737

2738
_OVER:
8✔
2739
  tFreeSVArbCheckSyncReq(&syncReq);
8✔
2740
  return code;
8✔
2741
}
2742

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

© 2025 Coveralls, Inc