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

taosdata / TDengine / #4491

12 Jul 2025 07:47AM UTC coverage: 62.635% (+0.4%) from 62.207%
#4491

push

travis-ci

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

159461 of 324087 branches covered (49.2%)

Branch coverage included in aggregate %.

245736 of 322830 relevant lines covered (76.12%)

18039735.02 hits per line

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

59.6
/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) {
188,751✔
64
  int32_t code = 0;
188,751✔
65
  int32_t lino = 0;
188,751✔
66

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

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

78
  // name
79
  char *name = NULL;
188,805✔
80
  if (tDecodeCStr(pCoder, &name) < 0) {
188,772!
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);
188,772✔
87
  if (uid == 0) {
188,786✔
88
    uid = tGenIdPI64();
181,284✔
89
  }
90
  taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), uid);
188,805✔
91

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

95
  tEndDecode(pCoder);
188,805✔
96

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

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

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

120
  if (tDecodeI32v(&dc, &nReqs) < 0) {
79,785!
121
    code = TSDB_CODE_INVALID_MSG;
×
122
    TSDB_CHECK_CODE(code, lino, _exit);
×
123
  }
124
  for (int32_t iReq = 0; iReq < nReqs; iReq++) {
184,231✔
125
    code = vnodePreprocessCreateTableReq(pVnode, &dc, btime, NULL);
104,487✔
126
    TSDB_CHECK_CODE(code, lino, _exit);
104,446!
127
  }
128

129
  tEndDecode(&dc);
79,744✔
130

131
_exit:
79,800✔
132
  tDecoderClear(&dc);
79,800✔
133
  if (code) {
79,813!
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;
79,808✔
138
}
139

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

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

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

157
  code = 0;
6,277✔
158

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

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

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

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

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

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

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

200
    ttlReq.nUids = taosArrayGetSize(tbUids);
65,951✔
201
    ttlReq.pTbUids = tbUids;
65,735✔
202
  }
203

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

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

213
    SMsgHead *pContNew = rpcMallocCont(contLenNew);
30✔
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);
29!
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:
65,761✔
233
  taosArrayDestroy(tbUids);
65,761✔
234

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

242
  return code;
65,893✔
243
}
244

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

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

264
  int64_t uid;
265
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
11,427,598✔
266
    code = vnodePreprocessCreateTableReq(pVnode, pCoder, btimeMs, &uid);
84,297✔
267
    TSDB_CHECK_CODE(code, lino, _exit);
84,303!
268
  }
269

270
  // submit data
271
  if (tDecodeI64(pCoder, &submitTbData.suid) < 0) {
11,427,573!
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,427,573✔
277
    taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), uid);
84,304✔
278
    pCoder->pos += sizeof(int64_t);
84,304✔
279
  } else {
280
    if (tDecodeI64(pCoder, &submitTbData.uid) < 0) {
11,343,257!
281
      code = TSDB_CODE_INVALID_MSG;
×
282
      TSDB_CHECK_CODE(code, lino, _exit);
×
283
    }
284
  }
285

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

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

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

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

327
    if (colData.flag != HAS_VALUE) {
10,125!
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,967,789✔
333
      if (((TSKEY *)colData.pData)[iRow] < minKey || ((TSKEY *)colData.pData)[iRow] > maxKey) {
9,957,665!
334
        code = TSDB_CODE_TDB_TIMESTAMP_OUT_OF_RANGE;
60✔
335
        TSDB_CHECK_CODE(code, lino, _exit);
60✔
336
      }
337
    }
338

339
    for (uint64_t i = 1; i < nColData; i++) {
30,584✔
340
      code = tDecodeColData(version, pCoder, &colData);
20,460✔
341
      if (code) {
20,460!
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,417,379!
349
      code = TSDB_CODE_INVALID_MSG;
×
350
      TSDB_CHECK_CODE(code, lino, _exit);
518!
351
    }
352

353
    for (int32_t iRow = 0; iRow < nRow; ++iRow) {
740,194,772✔
354
      SRow *pRow = (SRow *)(pCoder->data + pCoder->pos);
728,777,911✔
355
      pCoder->pos += pRow->len;
728,777,911✔
356
#ifndef NO_UNALIGNED_ACCESS
357
      if (pRow->ts < minKey || pRow->ts > maxKey) {
728,777,911!
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;
×
363
        TSDB_CHECK_CODE(code, lino, _exit);
×
364
      }
365

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

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

379
  tEndDecode(pCoder);
11,426,985✔
380

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

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

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

398
  if (taosHton64(((SSubmitReq2Msg *)pMsg->pCont)->version) != 1) {
10,692,200!
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,692,278✔
404

405
  if (tStartDecode(pCoder) < 0) {
10,692,197!
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,692,209!
412
    code = TSDB_CODE_INVALID_MSG;
×
413
    TSDB_CHECK_CODE(code, lino, _exit);
×
414
  }
415

416
  int64_t btimeMs = taosGetTimestampMs();
10,692,311✔
417
  int64_t ctimeMs = btimeMs;
10,692,311✔
418
  for (int32_t i = 0; i < nSubmitTbData; i++) {
22,119,268✔
419
    code = vnodePreProcessSubmitTbData(pVnode, pCoder, btimeMs, ctimeMs);
11,427,534✔
420
    TSDB_CHECK_CODE(code, lino, _exit);
11,427,476✔
421
  }
422

423
  tEndDecode(pCoder);
10,691,734✔
424

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

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

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

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

446
  code = qWorkerProcessDeleteMsg(&handle, pVnode->pQuery, pMsg, &res);
60,592✔
447
  if (code) goto _exit;
60,590!
448

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

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

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

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

467
  taosArrayDestroy(res.uidList);
60,592✔
468

469
_exit:
60,591✔
470
  return code;
60,591✔
471
}
472

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

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

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

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

487
  tDecoderClear(pCoder);
1,929✔
488
  taosArrayDestroy(pReq.deleteReqs);
1,929✔
489

490
  if (code) {
1,929!
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);
1,929✔
494
  }
495
  return code;
1,928✔
496
}
497

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

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

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

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

514
  return code;
32✔
515
}
516

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

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

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

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

555
  tEncodeSize(tEncodeSVDropTbBatchReq, &sentBatchReqs, size, code);
15,600!
556
  tEncoderInit(&ec, POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), size);
15,600✔
557
  code = tEncodeSVDropTbBatchReq(&ec, &sentBatchReqs);
15,600✔
558
  tEncoderClear(&ec);
15,600✔
559
  if (code != TSDB_CODE_SUCCESS) {
15,600!
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,600✔
565
  tDecoderClear(&dc);
15,600✔
566
  if (sentBatchReqs.pArray) {
15,600!
567
    taosArrayDestroy(sentBatchReqs.pArray);
15,600✔
568
  }
569
  return code;
15,600✔
570
}
571

572
int32_t vnodePreProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
11,031,794✔
573
  int32_t code = 0;
11,031,794✔
574

575
  if (pVnode->mounted) {
11,031,794!
576
    vError("vgId:%d, mountVgId:%d, skip write msg:%s", TD_VID(pVnode), pVnode->config.mountVgId,
×
577
           TMSG_INFO(pMsg->msgType));
578
    return TSDB_CODE_OPS_NOT_SUPPORT;
×
579
  }
580

581
  switch (pMsg->msgType) {
11,031,794✔
582
    case TDMT_VND_CREATE_TABLE: {
79,792✔
583
      code = vnodePreProcessCreateTableMsg(pVnode, pMsg);
79,792✔
584
    } break;
79,805✔
585
    case TDMT_VND_ALTER_TABLE: {
6,277✔
586
      code = vnodePreProcessAlterTableMsg(pVnode, pMsg);
6,277✔
587
    } break;
6,277✔
588
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
66,034✔
589
    case TDMT_VND_DROP_TTL_TABLE: {
590
      code = vnodePreProcessDropTtlMsg(pVnode, pMsg);
66,034✔
591
    } break;
65,877✔
592
    case TDMT_VND_SUBMIT: {
10,692,258✔
593
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.preprocess_time, METRIC_LEVEL_LOW,
10,692,258!
594
                           { code = vnodePreProcessSubmitMsg(pVnode, pMsg); });
595
    } break;
10,692,219✔
596
    case TDMT_VND_DELETE: {
60,590✔
597
      code = vnodePreProcessDeleteMsg(pVnode, pMsg);
60,590✔
598
    } break;
60,590✔
599
    case TDMT_VND_BATCH_DEL: {
1,928✔
600
      code = vnodePreProcessBatchDeleteMsg(pVnode, pMsg);
1,928✔
601
    } break;
1,928✔
602
    case TDMT_VND_ARB_CHECK_SYNC: {
32✔
603
      code = vnodePreProcessArbCheckSyncMsg(pVnode, pMsg);
32✔
604
    } break;
32✔
605
    case TDMT_VND_DROP_TABLE: {
15,600✔
606
      code = vnodePreProcessDropTbMsg(pVnode, pMsg);
15,600✔
607
    } break;
15,600✔
608
    default:
109,283✔
609
      break;
109,283✔
610
  }
611

612
  if (code && code != TSDB_CODE_MSG_PREPROCESSED) {
11,031,611✔
613
    vError("vgId:%d, failed to preprocess write request since %s, msg type:%s", TD_VID(pVnode), tstrerror(code),
546!
614
           TMSG_INFO(pMsg->msgType));
615
  }
616
  return code;
11,031,420✔
617
}
618

619
int32_t vnodeProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg, int64_t ver, SRpcMsg *pRsp) {
14,713,134✔
620
  int32_t code = 0;
14,713,134✔
621
  void   *ptr = NULL;
14,713,134✔
622
  void   *pReq;
623
  int32_t len;
624

625
  (void)taosThreadMutexLock(&pVnode->mutex);
14,713,134✔
626
  if (pVnode->disableWrite) {
14,713,700!
627
    (void)taosThreadMutexUnlock(&pVnode->mutex);
×
628
    vError("vgId:%d write is disabled for snapshot, version:%" PRId64, TD_VID(pVnode), ver);
×
629
    return TSDB_CODE_VND_WRITE_DISABLED;
×
630
  }
631
  (void)taosThreadMutexUnlock(&pVnode->mutex);
14,713,700✔
632

633
  if (ver <= pVnode->state.applied) {
14,713,642!
634
    vError("vgId:%d, duplicate write request. ver: %" PRId64 ", applied: %" PRId64, TD_VID(pVnode), ver,
×
635
           pVnode->state.applied);
636
    return terrno = TSDB_CODE_VND_DUP_REQUEST;
×
637
  }
638

639
  vGDebug(&pMsg->info.traceId,
14,713,642!
640
          "vgId:%d, index:%" PRId64 ", process write request:%s, applied:%" PRId64 ", state.applyTerm:%" PRId64
641
          ", conn.applyTerm:%" PRId64 ", contLen:%d",
642
          TD_VID(pVnode), ver, TMSG_INFO(pMsg->msgType), pVnode->state.applied, pVnode->state.applyTerm,
643
          pMsg->info.conn.applyTerm, pMsg->contLen);
644

645
  if (!(pVnode->state.applyTerm <= pMsg->info.conn.applyTerm)) {
14,713,216!
646
    vError("vgId:%d, applyTerm mismatch, expected: %" PRId64 ", received: %" PRId64, TD_VID(pVnode),
×
647
           pVnode->state.applyTerm, pMsg->info.conn.applyTerm);
648
    return terrno = TSDB_CODE_INTERNAL_ERROR;
×
649
  }
650

651
  if (!(pVnode->state.applied + 1 == ver)) {
14,713,216!
652
    vError("vgId:%d, mountVgId:%d, ver mismatch, expected: %" PRId64 ", received: %" PRId64, TD_VID(pVnode), pVnode->config.mountVgId,
×
653
           pVnode->state.applied + 1, ver);
654
    return terrno = TSDB_CODE_INTERNAL_ERROR;
×
655
  }
656

657
  atomic_store_64(&pVnode->state.applied, ver);
14,713,216✔
658
  atomic_store_64(&pVnode->state.applyTerm, pMsg->info.conn.applyTerm);
14,713,713✔
659

660
  if (!syncUtilUserCommit(pMsg->msgType)) goto _exit;
14,713,696✔
661

662
  // skip header
663
  pReq = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
14,672,560✔
664
  len = pMsg->contLen - sizeof(SMsgHead);
14,672,560✔
665
  bool needCommit = false;
14,672,560✔
666

667
  switch (pMsg->msgType) {
14,672,560!
668
    /* META */
669
    case TDMT_VND_CREATE_STB:
32,442✔
670
      if (vnodeProcessCreateStbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
32,442✔
671
      break;
32,556✔
672
    case TDMT_VND_ALTER_STB:
12,592✔
673
      if (vnodeProcessAlterStbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
12,592!
674
      break;
12,609✔
675
    case TDMT_VND_DROP_STB:
2,952✔
676
      if (vnodeProcessDropStbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
2,952!
677
      break;
2,959✔
678
    case TDMT_VND_CREATE_TABLE:
90,680✔
679
      if (vnodeProcessCreateTbReq(pVnode, ver, pReq, len, pRsp, pMsg) < 0) goto _err;
90,680!
680
      break;
90,742✔
681
    case TDMT_VND_ALTER_TABLE:
6,283✔
682
      if (vnodeProcessAlterTbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
6,283!
683
      break;
6,283✔
684
    case TDMT_VND_DROP_TABLE:
15,642✔
685
      if (vnodeProcessDropTbReq(pVnode, ver, pReq, len, pRsp, pMsg) < 0) goto _err;
15,642!
686
      break;
15,642✔
687
    case TDMT_VND_DROP_TTL_TABLE:
×
688
      if (vnodeProcessDropTtlTbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
689
      break;
×
690
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
29✔
691
      if (vnodeProcessFetchTtlExpiredTbs(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
29!
692
      break;
30✔
693
    case TDMT_VND_TRIM:
111✔
694
      if (vnodeProcessTrimReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
111!
695
      break;
112✔
696
    case TDMT_VND_S3MIGRATE:
×
697
      if (vnodeProcessS3MigrateReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
698
      break;
×
699
    case TDMT_VND_CREATE_SMA:
×
700
      if (vnodeProcessCreateTSmaReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
701
      break;
×
702
    /* TSDB */
703
    case TDMT_VND_SUBMIT: {
14,357,229✔
704
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.apply_time, METRIC_LEVEL_LOW, {
14,357,229!
705
        if (vnodeProcessSubmitReq(pVnode, ver, pMsg->pCont, pMsg->contLen, pRsp, pMsg) < 0) goto _err;
706
      });
707
      METRICS_UPDATE(pVnode->writeMetrics.apply_bytes, METRIC_LEVEL_LOW, (int64_t)pMsg->contLen);
14,357,192!
708
      break;
14,357,281✔
709
    }
710
    case TDMT_VND_DELETE:
63,028✔
711
      if (vnodeProcessDeleteReq(pVnode, ver, pReq, len, pRsp, pMsg) < 0) goto _err;
63,028!
712
      break;
63,024✔
713
    case TDMT_VND_BATCH_DEL:
1,928✔
714
      if (vnodeProcessBatchDeleteReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
1,928!
715
      break;
1,927✔
716
      /* TQ */
717
#if defined(USE_TQ) || defined(USE_STREAM)
718
    case TDMT_VND_TMQ_SUBSCRIBE:
3,304✔
719
      if (tqProcessSubscribeReq(pVnode->pTq, ver, pReq, len) < 0) {
3,304!
720
        goto _err;
×
721
      }
722
      break;
3,317✔
723
    case TDMT_VND_TMQ_DELETE_SUB:
997✔
724
      if (tqProcessDeleteSubReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen) < 0) {
997!
725
        goto _err;
×
726
      }
727
      break;
1,000✔
728
    case TDMT_VND_TMQ_COMMIT_OFFSET:
10,418✔
729
      if (tqProcessOffsetCommitReq(pVnode->pTq, ver, pReq, len) < 0) {
10,418!
730
        goto _err;
×
731
      }
732
      break;
10,430✔
733
    case TDMT_VND_TMQ_ADD_CHECKINFO:
124✔
734
      if (tqProcessAddCheckInfoReq(pVnode->pTq, ver, pReq, len) < 0) {
124!
735
        goto _err;
×
736
      }
737
      break;
124✔
738
    case TDMT_VND_TMQ_DEL_CHECKINFO:
11✔
739
      if (tqProcessDelCheckInfoReq(pVnode->pTq, ver, pReq, len) < 0) {
11!
740
        goto _err;
×
741
      }
742
      break;
11✔
743
    case TDMT_STREAM_TASK_DEPLOY: {
10,141✔
744
      if ((code = tqProcessTaskDeployReq(pVnode->pTq, ver, pReq, len)) != TSDB_CODE_SUCCESS) {
10,141!
745
        goto _err;
×
746
      }
747
    } break;
10,159✔
748
    case TDMT_STREAM_TASK_DROP: {
4,927✔
749
      if ((code = tqProcessTaskDropReq(pVnode->pTq, pMsg->pCont, pMsg->contLen)) < 0) {
4,927!
750
        goto _err;
×
751
      }
752
    } break;
4,941✔
753
    case TDMT_STREAM_TASK_UPDATE_CHKPT: {
2,916✔
754
      if ((code = tqProcessTaskUpdateCheckpointReq(pVnode->pTq, pMsg->pCont, pMsg->contLen)) < 0) {
2,916!
755
        goto _err;
×
756
      }
757
    } break;
2,926✔
758
    case TDMT_STREAM_CONSEN_CHKPT: {
60✔
759
      if (pVnode->restored && (code = tqProcessTaskConsenChkptIdReq(pVnode->pTq, pMsg)) < 0) {
60!
760
        goto _err;
×
761
      }
762

763
    } break;
62✔
764
    case TDMT_STREAM_TASK_PAUSE: {
241✔
765
      if (pVnode->restored && vnodeIsLeader(pVnode) &&
382!
766
          (code = tqProcessTaskPauseReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen)) < 0) {
141✔
767
        goto _err;
×
768
      }
769
    } break;
241✔
770
    case TDMT_STREAM_TASK_RESUME: {
234✔
771
      if (pVnode->restored && vnodeIsLeader(pVnode) &&
368!
772
          (code = tqProcessTaskResumeReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen)) < 0) {
134✔
773
        goto _err;
×
774
      }
775
    } break;
234✔
776
    case TDMT_VND_STREAM_TASK_RESET: {
×
777
      if (pVnode->restored && vnodeIsLeader(pVnode) && (code = tqProcessTaskResetReq(pVnode->pTq, pMsg)) < 0) {
×
778
        goto _err;
×
779
      }
780

781
    } break;
×
782
    case TDMT_VND_STREAM_ALL_STOP: {
4,816✔
783
      if (pVnode->restored && vnodeIsLeader(pVnode) && (code = tqProcessAllTaskStopReq(pVnode->pTq, pMsg)) < 0) {
4,816!
784
        goto _err;
×
785
      }
786

787
    } break;
4,829✔
788
#endif
789
    case TDMT_VND_ALTER_CONFIRM:
15,043✔
790
      needCommit = pVnode->config.hashChange;
15,043✔
791
      if (vnodeProcessAlterConfirmReq(pVnode, ver, pReq, len, pRsp) < 0) {
15,043!
792
        goto _err;
×
793
      }
794
      break;
15,043✔
795
    case TDMT_VND_ALTER_CONFIG:
1,112✔
796
      vnodeProcessAlterConfigReq(pVnode, ver, pReq, len, pRsp);
1,112✔
797
      break;
1,112✔
798
    case TDMT_VND_COMMIT:
29,265✔
799
      needCommit = true;
29,265✔
800
      break;
29,265✔
801
    case TDMT_VND_CREATE_INDEX:
1,160✔
802
      vnodeProcessCreateIndexReq(pVnode, ver, pReq, len, pRsp);
1,160✔
803
      break;
1,160✔
804
    case TDMT_VND_DROP_INDEX:
2,662✔
805
      vnodeProcessDropIndexReq(pVnode, ver, pReq, len, pRsp);
2,662✔
806
      break;
2,662✔
807
    case TDMT_VND_STREAM_CHECK_POINT_SOURCE:  // always return true
1,965✔
808
      tqProcessTaskCheckPointSourceReq(pVnode->pTq, pMsg, pRsp);
1,965✔
809
      break;
1,965✔
810
    case TDMT_VND_STREAM_TASK_UPDATE:  // always return true
34✔
811
      tqProcessTaskUpdateReq(pVnode->pTq, pMsg);
34✔
812
      break;
34✔
813
    case TDMT_VND_COMPACT:
204✔
814
      vnodeProcessCompactVnodeReq(pVnode, ver, pReq, len, pRsp);
204✔
815
      goto _exit;
204✔
816
    case TDMT_SYNC_CONFIG_CHANGE:
×
817
      vnodeProcessConfigChangeReq(pVnode, ver, pReq, len, pRsp);
×
818
      break;
×
819
#ifdef TD_ENTERPRISE
820
    case TDMT_VND_KILL_COMPACT:
×
821
      vnodeProcessKillCompactReq(pVnode, ver, pReq, len, pRsp);
×
822
      break;
×
823
#endif
824
    /* ARB */
825
    case TDMT_VND_ARB_CHECK_SYNC:
10✔
826
      vnodeProcessArbCheckSyncReq(pVnode, pReq, len, pRsp);
10✔
827
      break;
10✔
828
    default:
×
829
      vError("vgId:%d, unprocessed msg, %d", TD_VID(pVnode), pMsg->msgType);
×
830
      return TSDB_CODE_INVALID_MSG;
×
831
  }
832

833
  vGDebug(&pMsg->info.traceId, "vgId:%d, index:%" PRId64 ", msg processed, code:0x%x", TD_VID(pVnode), ver, pRsp->code);
14,672,690!
834

835
  walApplyVer(pVnode->pWal, ver);
14,672,727✔
836

837
  code = tqPushMsg(pVnode->pTq, pMsg->msgType);
14,672,580✔
838
  if (code) {
14,672,676!
839
    vError("vgId:%d, failed to push msg to TQ since %s", TD_VID(pVnode), tstrerror(terrno));
×
840
    return code;
×
841
  }
842

843
  // commit if need
844
  if (needCommit) {
14,672,676✔
845
    vInfo("vgId:%d, commit at version %" PRId64, TD_VID(pVnode), ver);
29,306!
846
    code = vnodeAsyncCommit(pVnode);
29,364✔
847
    if (code) {
29,374!
848
      vError("vgId:%d, failed to vnode async commit since %s.", TD_VID(pVnode), tstrerror(terrno));
×
849
      goto _err;
×
850
    }
851

852
    // start a new one
853
    METRICS_TIMING_BLOCK(pVnode->writeMetrics.memtable_wait_time, METRIC_LEVEL_LOW, {
29,374!
854
      code = vnodeBegin(pVnode);
855
      if (code) {
856
        vError("vgId:%d, failed to begin vnode since %s.", TD_VID(pVnode), tstrerror(terrno));
857
        goto _err;
858
      }
859
    });
860
  }
861

862
_exit:
14,672,742✔
863
  return 0;
14,713,266✔
864

865
_err:
13✔
866
  vError("vgId:%d, process %s request failed since %s, ver:%" PRId64, TD_VID(pVnode), TMSG_INFO(pMsg->msgType),
13!
867
         tstrerror(terrno), ver);
868
  return code;
13✔
869
}
870

871
int32_t vnodePreprocessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg) {
8,360,778✔
872
  if (TDMT_SCH_QUERY != pMsg->msgType && TDMT_SCH_MERGE_QUERY != pMsg->msgType) {
8,360,778✔
873
    return 0;
440,420✔
874
  }
875

876
  return qWorkerPreprocessQueryMsg(pVnode->pQuery, pMsg, TDMT_SCH_QUERY == pMsg->msgType);
7,920,358✔
877
}
878

879
int32_t vnodeProcessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
8,370,376✔
880
  vTrace("message in vnode query queue is processing");
8,370,376✔
881
  if (pMsg->msgType == TDMT_VND_TMQ_CONSUME && !syncIsReadyForRead(pVnode->sync)) {
8,370,376✔
882
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
1,446✔
883
    return 0;
1,446✔
884
  }
885

886
  if (pMsg->msgType == TDMT_VND_TMQ_CONSUME && !pVnode->restored) {
8,368,928!
887
    vnodeRedirectRpcMsg(pVnode, pMsg, TSDB_CODE_SYN_RESTORING);
×
888
    return 0;
×
889
  }
890

891
  SReadHandle handle = {.vnode = pVnode, .pMsgCb = &pVnode->msgCb, .pWorkerCb = pInfo->workerCb};
8,368,928✔
892
  initStorageAPI(&handle.api);
8,368,928✔
893
  int32_t code = TSDB_CODE_SUCCESS;
8,367,792✔
894
  bool    redirected = false;
8,367,792✔
895

896
  switch (pMsg->msgType) {
8,367,792!
897
    case TDMT_SCH_QUERY:
6,104,360✔
898
      if (!syncIsReadyForRead(pVnode->sync)) {
6,104,360✔
899
        pMsg->code = (terrno) ? terrno : TSDB_CODE_SYN_NOT_LEADER;
4,197!
900
        redirected = true;
4,197✔
901
      }
902
      code = qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
6,103,315✔
903
      if (redirected) {
6,102,001✔
904
        vnodeRedirectRpcMsg(pVnode, pMsg, pMsg->code);
4,138✔
905
        return 0;
4,197✔
906
      }
907
      return code;
6,097,863✔
908
    case TDMT_SCH_MERGE_QUERY:
1,825,164✔
909
      return qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
1,825,164✔
910
    case TDMT_SCH_QUERY_CONTINUE:
377,408✔
911
      return qWorkerProcessCQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
377,408✔
912
    case TDMT_VND_TMQ_CONSUME:
48,955✔
913
      return tqProcessPollReq(pVnode->pTq, pMsg);
48,955✔
914
    case TDMT_VND_TMQ_CONSUME_PUSH:
12,594✔
915
      return tqProcessPollPush(pVnode->pTq);
12,594✔
916
    default:
×
917
      vError("unknown msg type:%d in query queue", pMsg->msgType);
×
918
      return TSDB_CODE_APP_ERROR;
×
919
  }
920
}
921

922
int32_t vnodeProcessFetchMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
21,532,517✔
923
  int32_t code = TSDB_CODE_SUCCESS;
21,532,517✔
924
  vTrace("vgId:%d, msg:%p in fetch queue is processing", pVnode->config.vgId, pMsg);
21,532,517✔
925
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
21,532,521!
926
       pMsg->msgType == TDMT_VND_BATCH_META || pMsg->msgType == TDMT_VND_TABLE_NAME ||
15,035,326!
927
       pMsg->msgType == TDMT_VND_VSUBTABLES_META || pMsg->msgType == TDMT_VND_VSTB_REF_DBS) &&
13,837,851!
928
      !syncIsReadyForRead(pVnode->sync)) {
7,698,956✔
929
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
5,805✔
930
    return 0;
5,805✔
931
  }
932

933
  switch (pMsg->msgType) {
21,532,542!
934
    case TDMT_SCH_FETCH:
10,070,115✔
935
    case TDMT_SCH_MERGE_FETCH:
936
      return qWorkerProcessFetchMsg(pVnode, pVnode->pQuery, pMsg, 0);
10,070,115✔
937
    case TDMT_SCH_FETCH_RSP:
×
938
      return qWorkerProcessRspMsg(pVnode, pVnode->pQuery, pMsg, 0);
×
939
    // case TDMT_SCH_CANCEL_TASK:
940
    //   return qWorkerProcessCancelMsg(pVnode, pVnode->pQuery, pMsg, 0);
941
    case TDMT_SCH_DROP_TASK:
7,917,880✔
942
      return qWorkerProcessDropMsg(pVnode, pVnode->pQuery, pMsg, 0);
7,917,880✔
943
    case TDMT_SCH_TASK_NOTIFY:
73✔
944
      return qWorkerProcessNotifyMsg(pVnode, pVnode->pQuery, pMsg, 0);
73✔
945
    case TDMT_SCH_QUERY_HEARTBEAT:
2,349,316✔
946
      return qWorkerProcessHbMsg(pVnode, pVnode->pQuery, pMsg, 0);
2,349,316✔
947
    case TDMT_VND_TABLE_META:
3,000✔
948
    case TDMT_VND_TABLE_NAME:
949
      return vnodeGetTableMeta(pVnode, pMsg, true);
3,000✔
950
    case TDMT_VND_TABLE_CFG:
×
951
      return vnodeGetTableCfg(pVnode, pMsg, true);
×
952
    case TDMT_VND_BATCH_META: {
1,192,075✔
953
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.fetch_batch_meta_time, METRIC_LEVEL_LOW,
1,192,075!
954
                           { code = vnodeGetBatchMeta(pVnode, pMsg); });
955
      METRICS_UPDATE(pVnode->writeMetrics.fetch_batch_meta_count, METRIC_LEVEL_LOW, 1);
1,192,675!
956
      return code;
1,192,707✔
957
    }
958
    case TDMT_VND_VSUBTABLES_META:
×
959
      return vnodeGetVSubtablesMeta(pVnode, pMsg);
×
960
    case TDMT_VND_VSTB_REF_DBS:
×
961
      return vnodeGetVStbRefDbs(pVnode, pMsg);
×
962
#ifdef TD_ENTERPRISE
963
    case TDMT_VND_QUERY_COMPACT_PROGRESS:
46✔
964
      return vnodeQueryCompactProgress(pVnode, pMsg);
46✔
965
#endif
966
      //    case TDMT_VND_TMQ_CONSUME:
967
      //      return tqProcessPollReq(pVnode->pTq, pMsg);
968
#ifdef USE_TQ
969
    case TDMT_VND_TMQ_VG_WALINFO:
11✔
970
      return tqProcessVgWalInfoReq(pVnode->pTq, pMsg);
11✔
971
    case TDMT_VND_TMQ_VG_COMMITTEDINFO:
2✔
972
      return tqProcessVgCommittedInfoReq(pVnode->pTq, pMsg);
2✔
973
    case TDMT_VND_TMQ_SEEK:
24✔
974
      return tqProcessSeekReq(pVnode->pTq, pMsg);
24✔
975
#endif
976
    default:
×
977
      vError("unknown msg type:%d in fetch queue", pMsg->msgType);
×
978
      return TSDB_CODE_APP_ERROR;
×
979
  }
980
}
981
#ifdef USE_STREAM
982
int32_t vnodeProcessStreamMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
257,212✔
983
  vTrace("vgId:%d, msg:%p in stream queue is processing", pVnode->config.vgId, pMsg);
257,212✔
984

985
  // todo: NOTE: some command needs to run on follower, such as, stop_all_tasks
986
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
257,212!
987
       pMsg->msgType == TDMT_VND_BATCH_META) &&
257,257!
988
      !syncIsReadyForRead(pVnode->sync)) {
×
989
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
×
990
    return 0;
×
991
  }
992

993
  switch (pMsg->msgType) {
257,258!
994
    case TDMT_STREAM_TASK_RUN:
256,366✔
995
      return tqProcessTaskRunReq(pVnode->pTq, pMsg);
256,366✔
996
    case TDMT_STREAM_RETRIEVE:
482✔
997
      return tqProcessTaskRetrieveReq(pVnode->pTq, pMsg);
482✔
998
    case TDMT_STREAM_RETRIEVE_RSP:
410✔
999
      return tqProcessTaskRetrieveRsp(pVnode->pTq, pMsg);
410✔
1000
    case TDMT_VND_GET_STREAM_PROGRESS:
×
1001
      return tqStreamProgressRetrieveReq(pVnode->pTq, pMsg);
×
1002
    default:
×
1003
      vError("unknown msg type:%d in stream queue", pMsg->msgType);
×
1004
      return TSDB_CODE_APP_ERROR;
×
1005
  }
1006
}
1007

1008
int32_t vnodeProcessStreamCtrlMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
152,858✔
1009
  vTrace("vgId:%d, msg:%p in stream ctrl queue is processing", pVnode->config.vgId, pMsg);
152,858✔
1010
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
152,860!
1011
       pMsg->msgType == TDMT_VND_BATCH_META) &&
152,890✔
1012
      !syncIsReadyForRead(pVnode->sync)) {
×
1013
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
×
1014
    return 0;
×
1015
  }
1016

1017
  switch (pMsg->msgType) {
152,882!
1018
    case TDMT_MND_STREAM_HEARTBEAT_RSP:
18,364✔
1019
      return tqProcessStreamHbRsp(pVnode->pTq, pMsg);
18,364✔
1020
    case TDMT_STREAM_TASK_DISPATCH:
42,580✔
1021
      return tqProcessTaskDispatchReq(pVnode->pTq, pMsg);
42,580✔
1022
    case TDMT_STREAM_TASK_DISPATCH_RSP:
42,886✔
1023
      return tqProcessTaskDispatchRsp(pVnode->pTq, pMsg);
42,886✔
1024
    case TDMT_VND_STREAM_TASK_CHECK:
17,623✔
1025
      return tqProcessTaskCheckReq(pVnode->pTq, pMsg);
17,623✔
1026
    case TDMT_VND_STREAM_TASK_CHECK_RSP:
16,078✔
1027
      return tqProcessTaskCheckRsp(pVnode->pTq, pMsg);
16,078✔
1028
    case TDMT_STREAM_TASK_CHECKPOINT_READY:
4,484✔
1029
      return tqProcessTaskCheckpointReadyMsg(pVnode->pTq, pMsg);
4,484✔
1030
    case TDMT_STREAM_TASK_CHECKPOINT_READY_RSP:
4,487✔
1031
      return tqProcessTaskCheckpointReadyRsp(pVnode->pTq, pMsg);
4,487✔
1032
    case TDMT_STREAM_RETRIEVE_TRIGGER:
×
1033
      return tqProcessTaskRetrieveTriggerReq(pVnode->pTq, pMsg);
×
1034
    case TDMT_STREAM_RETRIEVE_TRIGGER_RSP:
×
1035
      return tqProcessTaskRetrieveTriggerRsp(pVnode->pTq, pMsg);
×
1036
    case TDMT_MND_STREAM_REQ_CHKPT_RSP:
2,647✔
1037
      return tqProcessStreamReqCheckpointRsp(pVnode->pTq, pMsg);
2,647✔
1038
    case TDMT_MND_STREAM_CHKPT_REPORT_RSP:
3,733✔
1039
      return tqProcessTaskChkptReportRsp(pVnode->pTq, pMsg);
3,733✔
1040
    default:
×
1041
      vError("unknown msg type:%d in stream ctrl queue", pMsg->msgType);
×
1042
      return TSDB_CODE_APP_ERROR;
×
1043
  }
1044
}
1045

1046
int32_t vnodeProcessStreamLongExecMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
1,650✔
1047
  vTrace("vgId:%d, msg:%p in stream long exec queue is processing", pVnode->config.vgId, pMsg);
1,650✔
1048
  if (!syncIsReadyForRead(pVnode->sync)) {
1,650!
1049
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
×
1050
    return 0;
×
1051
  }
1052

1053
  switch (pMsg->msgType) {
1,650!
1054
    case TDMT_VND_STREAM_SCAN_HISTORY:
1,650✔
1055
      return tqProcessTaskScanHistory(pVnode->pTq, pMsg);
1,650✔
1056
    default:
×
1057
      vError("unknown msg type:%d in stream long exec queue", pMsg->msgType);
×
1058
      return TSDB_CODE_APP_ERROR;
×
1059
  }
1060
}
1061

1062
int32_t vnodeProcessStreamChkptMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
7,508✔
1063
  vTrace("vgId:%d, msg:%p in stream chkpt queue is processing", pVnode->config.vgId, pMsg);
7,508✔
1064
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
7,508!
1065
       pMsg->msgType == TDMT_VND_BATCH_META) &&
7,508!
1066
      !syncIsReadyForRead(pVnode->sync)) {
×
1067
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
×
1068
    return 0;
×
1069
  }
1070

1071
  switch (pMsg->msgType) {
7,508!
1072
    case TDMT_STREAM_CHKPT_EXEC:
7,508✔
1073
      return tqProcessTaskRunReq(pVnode->pTq, pMsg);
7,508✔
1074
    default:
×
1075
      vError("unknown msg type:%d in stream chkpt queue", pMsg->msgType);
×
1076
      return TSDB_CODE_APP_ERROR;
×
1077
  }
1078
}
1079
#endif
1080

1081
void smaHandleRes(void *pVnode, int64_t smaId, const SArray *data) {
×
1082
  int32_t code = tdProcessTSmaInsert(((SVnode *)pVnode)->pSma, smaId, (const char *)data);
×
1083
  if (code) {
×
1084
    vError("failed to process sma result since %s", tstrerror(code));
×
1085
  }
1086
}
×
1087

1088
void vnodeUpdateMetaRsp(SVnode *pVnode, STableMetaRsp *pMetaRsp) {
211,168✔
1089
  if (NULL == pMetaRsp) {
211,168!
1090
    return;
×
1091
  }
1092

1093
  tstrncpy(pMetaRsp->dbFName, pVnode->config.dbname, TSDB_DB_FNAME_LEN);
211,168✔
1094
  pMetaRsp->dbId = pVnode->config.dbId;
211,168✔
1095
  pMetaRsp->vgId = TD_VID(pVnode);
211,168✔
1096
  pMetaRsp->precision = pVnode->config.tsdbCfg.precision;
211,168✔
1097
}
1098

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

1101
static int32_t vnodeProcessTrimReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
111✔
1102
  if (!pVnode->restored) {
111✔
1103
    vInfo("vgId:%d, ignore trim req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
6!
1104
    return 0;
6✔
1105
  }
1106

1107
  int32_t     code = 0;
105✔
1108
  SVTrimDbReq trimReq = {0};
105✔
1109

1110
  // decode
1111
  if (tDeserializeSVTrimDbReq(pReq, len, &trimReq) != 0) {
105!
1112
    code = TSDB_CODE_INVALID_MSG;
×
1113
    goto _exit;
×
1114
  }
1115

1116
  vInfo("vgId:%d, process trim vnode request, time:%d", pVnode->config.vgId, trimReq.timestamp);
105!
1117

1118
  code = vnodeAsyncRetention(pVnode, trimReq.timestamp);
105✔
1119

1120
_exit:
106✔
1121
  return code;
106✔
1122
}
1123

1124
extern int32_t vnodeAsyncS3Migrate(SVnode *pVnode, int64_t now);
1125

1126
static int32_t vnodeProcessS3MigrateReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1127
  int32_t          code = 0;
×
1128
  SVS3MigrateDbReq s3migrateReq = {0};
×
1129

1130
  // decode
1131
  if (tDeserializeSVS3MigrateDbReq(pReq, len, &s3migrateReq) != 0) {
×
1132
    code = TSDB_CODE_INVALID_MSG;
×
1133
    goto _exit;
×
1134
  }
1135

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

1138
  code = vnodeAsyncS3Migrate(pVnode, s3migrateReq.timestamp);
×
1139

1140
_exit:
×
1141
  return code;
×
1142
}
1143

1144
static int32_t vnodeProcessDropTtlTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1145
  int               ret = 0;
×
1146
  SVDropTtlTableReq ttlReq = {0};
×
1147
  if (tDeserializeSVDropTtlTableReq(pReq, len, &ttlReq) != 0) {
×
1148
    ret = TSDB_CODE_INVALID_MSG;
×
1149
    goto end;
×
1150
  }
1151

1152
  if (ttlReq.nUids != taosArrayGetSize(ttlReq.pTbUids)) {
×
1153
    ret = TSDB_CODE_INVALID_MSG;
×
1154
    goto end;
×
1155
  }
1156

1157
  if (ttlReq.nUids != 0) {
×
1158
    vInfo("vgId:%d, process drop ttl table request, time:%d, ntbUids:%d", pVnode->config.vgId, ttlReq.timestampSec,
×
1159
          ttlReq.nUids);
1160
  }
1161

1162
  if (ttlReq.nUids > 0) {
×
1163
    int32_t code = metaDropMultipleTables(pVnode->pMeta, ver, ttlReq.pTbUids);
×
1164
    if (code) return code;
×
1165

1166
    code = tqUpdateTbUidList(pVnode->pTq, ttlReq.pTbUids, false);
×
1167
    if (code) {
×
1168
      vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1169
    }
1170
  }
1171

1172
end:
×
1173
  taosArrayDestroy(ttlReq.pTbUids);
×
1174
  return ret;
×
1175
}
1176

1177
static int32_t vnodeProcessFetchTtlExpiredTbs(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
29✔
1178
  int32_t                 code = -1;
29✔
1179
  SMetaReader             mr = {0};
29✔
1180
  SVDropTtlTableReq       ttlReq = {0};
29✔
1181
  SVFetchTtlExpiredTbsRsp rsp = {0};
29✔
1182
  SEncoder                encoder = {0};
29✔
1183
  SArray                 *pNames = NULL;
29✔
1184
  pRsp->msgType = TDMT_VND_FETCH_TTL_EXPIRED_TBS_RSP;
29✔
1185
  pRsp->code = TSDB_CODE_SUCCESS;
29✔
1186
  pRsp->pCont = NULL;
29✔
1187
  pRsp->contLen = 0;
29✔
1188

1189
  if (tDeserializeSVDropTtlTableReq(pReq, len, &ttlReq) != 0) {
29!
1190
    terrno = TSDB_CODE_INVALID_MSG;
×
1191
    goto _end;
×
1192
  }
1193

1194
  if (!(ttlReq.nUids == taosArrayGetSize(ttlReq.pTbUids))) {
29!
1195
    terrno = TSDB_CODE_INVALID_MSG;
×
1196
    goto _end;
×
1197
  }
1198

1199
  tb_uid_t    suid;
1200
  char        ctbName[TSDB_TABLE_NAME_LEN];
1201
  SVDropTbReq expiredTb = {.igNotExists = true};
29✔
1202
  metaReaderDoInit(&mr, pVnode->pMeta, 0);
29✔
1203
  rsp.vgId = TD_VID(pVnode);
30✔
1204
  rsp.pExpiredTbs = taosArrayInit(ttlReq.nUids, sizeof(SVDropTbReq));
30✔
1205
  if (!rsp.pExpiredTbs) goto _end;
30!
1206

1207
  pNames = taosArrayInit(ttlReq.nUids, TSDB_TABLE_NAME_LEN);
30✔
1208
  if (!pNames) {
30!
1209
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1210
    goto _end;
×
1211
  }
1212
  char buf[TSDB_TABLE_NAME_LEN];
1213
  for (int32_t i = 0; i < ttlReq.nUids; ++i) {
154✔
1214
    tb_uid_t *uid = taosArrayGet(ttlReq.pTbUids, i);
124✔
1215
    expiredTb.suid = *uid;
123✔
1216
    terrno = metaReaderGetTableEntryByUid(&mr, *uid);
123✔
1217
    if (terrno < 0) goto _end;
123!
1218
    tstrncpy(buf, mr.me.name, TSDB_TABLE_NAME_LEN);
123✔
1219
    void *p = taosArrayPush(pNames, buf);
124✔
1220
    if (p == NULL) {
124!
1221
      goto _end;
×
1222
    }
1223

1224
    expiredTb.name = p;
124✔
1225
    if (mr.me.type == TSDB_CHILD_TABLE) {
124✔
1226
      expiredTb.suid = mr.me.ctbEntry.suid;
76✔
1227
    }
1228

1229
    if (taosArrayPush(rsp.pExpiredTbs, &expiredTb) == NULL) {
248!
1230
      goto _end;
×
1231
    }
1232
  }
1233

1234
  int32_t ret = 0;
30✔
1235
  tEncodeSize(tEncodeVFetchTtlExpiredTbsRsp, &rsp, pRsp->contLen, ret);
30!
1236
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
30✔
1237
  if (pRsp->pCont == NULL) {
30!
1238
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1239
    code = -1;
×
1240
    goto _end;
×
1241
  }
1242
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
30✔
1243
  terrno = tEncodeVFetchTtlExpiredTbsRsp(&encoder, &rsp);
30✔
1244
  tEncoderClear(&encoder);
30✔
1245

1246
  if (terrno == 0) code = 0;
30!
1247
_end:
×
1248
  metaReaderClear(&mr);
30✔
1249
  tFreeFetchTtlExpiredTbsRsp(&rsp);
30✔
1250
  taosArrayDestroy(ttlReq.pTbUids);
30✔
1251
  if (pNames) taosArrayDestroy(pNames);
30✔
1252
  pRsp->code = terrno;
31✔
1253
  return code;
30✔
1254
}
1255

1256
static int32_t vnodeProcessCreateStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
32,444✔
1257
  int32_t        code = 0;
32,444✔
1258
  SVCreateStbReq req = {0};
32,444✔
1259
  SDecoder       coder;
1260

1261
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
32,444✔
1262
  pRsp->code = TSDB_CODE_SUCCESS;
32,444✔
1263
  pRsp->pCont = NULL;
32,444✔
1264
  pRsp->contLen = 0;
32,444✔
1265

1266
  // decode and process req
1267
  tDecoderInit(&coder, pReq, len);
32,444✔
1268

1269
  code = tDecodeSVCreateStbReq(&coder, &req);
32,430✔
1270
  if (code) {
32,395!
1271
    pRsp->code = code;
×
1272
    goto _err;
×
1273
  }
1274

1275
  code = metaCreateSuperTable(pVnode->pMeta, ver, &req);
32,395✔
1276
  if (code) {
32,605✔
1277
    pRsp->code = code;
4✔
1278
    goto _err;
4✔
1279
  }
1280

1281
  if ((code = tdProcessRSmaCreate(pVnode->pSma, &req)) < 0) {
32,601!
1282
    pRsp->code = code;
×
1283
    goto _err;
×
1284
  }
1285

1286
  tDecoderClear(&coder);
32,588✔
1287
  return 0;
32,537✔
1288

1289
_err:
4✔
1290
  tDecoderClear(&coder);
4✔
1291
  return code;
4✔
1292
}
1293

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

1312
  // decode
1313
  tDecoderInit(&decoder, pReq, len);
90,671✔
1314
  if (tDecodeSVCreateTbBatchReq(&decoder, &req) < 0) {
90,700!
1315
    rcode = -1;
×
1316
    terrno = TSDB_CODE_INVALID_MSG;
×
1317
    goto _exit;
×
1318
  }
1319

1320
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(cRsp));
90,715✔
1321
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
90,733✔
1322
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
90,744✔
1323
  if (rsp.pArray == NULL || tbUids == NULL || tbNames == NULL) {
90,740!
1324
    rcode = -1;
×
1325
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1326
    goto _exit;
×
1327
  }
1328

1329
  // loop to create table
1330
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
222,270✔
1331
    pCreateReq = req.pReqs + iReq;
131,519✔
1332
    memset(&cRsp, 0, sizeof(cRsp));
131,519✔
1333

1334
    if (tsEnableAudit && tsEnableAuditCreateTable) {
131,519!
1335
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
131,519!
1336
      if (str == NULL) {
131,518!
1337
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1338
        rcode = -1;
×
1339
        goto _exit;
×
1340
      }
1341
      tstrncpy(str, pCreateReq->name, TSDB_TABLE_FNAME_LEN);
131,518✔
1342
      if (taosArrayPush(tbNames, &str) == NULL) {
131,510!
1343
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1344
        rcode = -1;
×
1345
        goto _exit;
×
1346
      }
1347
    }
1348

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

1362
    // do create table
1363
    if (metaCreateTable2(pVnode->pMeta, ver, pCreateReq, &cRsp.pMeta) < 0) {
131,508✔
1364
      if (pCreateReq->flags & TD_CREATE_IF_NOT_EXISTS && terrno == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
768!
1365
        cRsp.code = TSDB_CODE_SUCCESS;
5✔
1366
      } else {
1367
        cRsp.code = terrno;
763✔
1368
      }
1369
    } else {
1370
      cRsp.code = TSDB_CODE_SUCCESS;
130,764✔
1371
      if (tdFetchTbUidList(pVnode->pSma, &pStore, pCreateReq->ctb.suid, pCreateReq->uid) < 0) {
130,764!
1372
        vError("vgId:%d, failed to fetch tbUid list", TD_VID(pVnode));
×
1373
      }
1374
      if (taosArrayPush(tbUids, &pCreateReq->uid) == NULL) {
261,526!
1375
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1376
        rcode = -1;
×
1377
        goto _exit;
×
1378
      }
1379
      vnodeUpdateMetaRsp(pVnode, cRsp.pMeta);
130,763✔
1380
    }
1381

1382
    if (taosArrayPush(rsp.pArray, &cRsp) == NULL) {
263,060!
1383
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1384
      rcode = -1;
×
1385
      goto _exit;
×
1386
    }
1387
  }
1388

1389
  vTrace("vgId:%d, add %d new created tables into query table list", TD_VID(pVnode), (int32_t)taosArrayGetSize(tbUids));
90,751✔
1390
  if (tqUpdateTbUidList(pVnode->pTq, tbUids, true) < 0) {
90,751!
1391
    vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(terrno));
×
1392
  }
1393
  if (tdUpdateTbUidList(pVnode->pSma, pStore, true) < 0) {
90,743!
1394
    goto _exit;
×
1395
  }
1396
  pStore = tdUidStoreFree(pStore);
90,743✔
1397

1398
  // prepare rsp
1399
  int32_t ret = 0;
90,744✔
1400
  tEncodeSize(tEncodeSVCreateTbBatchRsp, &rsp, pRsp->contLen, ret);
90,744!
1401
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
90,742✔
1402
  if (pRsp->pCont == NULL) {
90,745!
1403
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1404
    rcode = -1;
×
1405
    goto _exit;
×
1406
  }
1407
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
90,745✔
1408
  if (tEncodeSVCreateTbBatchRsp(&encoder, &rsp) != 0) {
90,744!
1409
    vError("vgId:%d, failed to encode create table batch response", TD_VID(pVnode));
×
1410
  }
1411

1412
  if (tsEnableAudit && tsEnableAuditCreateTable) {
90,741!
1413
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
90,739✔
1414

1415
    SName name = {0};
90,739✔
1416
    if (tNameFromString(&name, pVnode->config.dbname, T_NAME_ACCT | T_NAME_DB) < 0) {
90,739!
1417
      vError("vgId:%d, failed to get name from string", TD_VID(pVnode));
×
1418
    }
1419

1420
    SStringBuilder sb = {0};
90,738✔
1421
    for (int32_t i = 0; i < tbNames->size; i++) {
222,259✔
1422
      char **key = (char **)taosArrayGet(tbNames, i);
131,519✔
1423
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
131,519✔
1424
      if (i < tbNames->size - 1) {
131,521✔
1425
        taosStringBuilderAppendChar(&sb, ',');
40,784✔
1426
      }
1427
      // taosMemoryFreeClear(*key);
1428
    }
1429

1430
    size_t len = 0;
90,740✔
1431
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
90,740✔
1432

1433
    if (pOriginRpc->info.conn.user != NULL && strlen(pOriginRpc->info.conn.user) > 0) {
90,737!
1434
      auditAddRecord(pOriginRpc, clusterId, "createTable", name.dbname, "", keyJoined, len);
75,972✔
1435
    }
1436

1437
    taosStringBuilderDestroy(&sb);
90,734✔
1438
  }
1439

1440
_exit:
2✔
1441
  tDeleteSVCreateTbBatchReq(&req);
90,739✔
1442
  taosArrayDestroyEx(rsp.pArray, tFreeSVCreateTbRsp);
90,743✔
1443
  taosArrayDestroy(tbUids);
90,743✔
1444
  tDecoderClear(&decoder);
90,744✔
1445
  tEncoderClear(&encoder);
90,744✔
1446
  taosArrayDestroyP(tbNames, NULL);
90,741✔
1447
  return rcode;
90,745✔
1448
}
1449

1450
static int32_t vnodeProcessAlterStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
12,587✔
1451
  int32_t        code = 0;
12,587✔
1452
  SVCreateStbReq req = {0};
12,587✔
1453
  SDecoder       dc = {0};
12,587✔
1454

1455
  pRsp->msgType = TDMT_VND_ALTER_STB_RSP;
12,587✔
1456
  pRsp->code = TSDB_CODE_SUCCESS;
12,587✔
1457
  pRsp->pCont = NULL;
12,587✔
1458
  pRsp->contLen = 0;
12,587✔
1459

1460
  tDecoderInit(&dc, pReq, len);
12,587✔
1461

1462
  // decode req
1463
  code = tDecodeSVCreateStbReq(&dc, &req);
12,592✔
1464
  if (code) {
12,602!
1465
    tDecoderClear(&dc);
×
1466
    return code;
×
1467
  }
1468

1469
  code = metaAlterSuperTable(pVnode->pMeta, ver, &req);
12,602✔
1470
  if (code) {
12,609!
1471
    pRsp->code = code;
×
1472
    tDecoderClear(&dc);
×
1473
    return code;
×
1474
  }
1475

1476
  tDecoderClear(&dc);
12,609✔
1477

1478
  return 0;
12,609✔
1479
}
1480

1481
static int32_t vnodeProcessDropStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
2,953✔
1482
  SVDropStbReq req = {0};
2,953✔
1483
  int32_t      rcode = TSDB_CODE_SUCCESS;
2,953✔
1484
  SDecoder     decoder = {0};
2,953✔
1485
  SArray      *tbUidList = NULL;
2,953✔
1486

1487
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
2,953✔
1488
  pRsp->pCont = NULL;
2,953✔
1489
  pRsp->contLen = 0;
2,953✔
1490

1491
  // decode request
1492
  tDecoderInit(&decoder, pReq, len);
2,953✔
1493
  if (tDecodeSVDropStbReq(&decoder, &req) < 0) {
2,954!
1494
    rcode = TSDB_CODE_INVALID_MSG;
×
1495
    goto _exit;
×
1496
  }
1497

1498
  // process request
1499
  tbUidList = taosArrayInit(8, sizeof(int64_t));
2,958✔
1500
  if (tbUidList == NULL) goto _exit;
2,954!
1501
  if (metaDropSuperTable(pVnode->pMeta, ver, &req) < 0) {
2,954✔
1502
    rcode = terrno;
10✔
1503
    goto _exit;
10✔
1504
  }
1505

1506
  if (tqUpdateTbUidList(pVnode->pTq, tbUidList, false) < 0) {
2,949!
1507
    rcode = terrno;
×
1508
    goto _exit;
×
1509
  }
1510

1511
  if (tdProcessRSmaDrop(pVnode->pSma, &req) < 0) {
2,949!
1512
    rcode = terrno;
×
1513
    goto _exit;
×
1514
  }
1515

1516
  // return rsp
1517
_exit:
2,949✔
1518
  if (tbUidList) taosArrayDestroy(tbUidList);
2,959!
1519
  pRsp->code = rcode;
2,959✔
1520
  tDecoderClear(&decoder);
2,959✔
1521
  return 0;
2,959✔
1522
}
1523

1524
static int32_t vnodeProcessAlterTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
6,283✔
1525
  SVAlterTbReq  vAlterTbReq = {0};
6,283✔
1526
  SVAlterTbRsp  vAlterTbRsp = {0};
6,283✔
1527
  SDecoder      dc = {0};
6,283✔
1528
  int32_t       code = 0;
6,283✔
1529
  int32_t       lino = 0;
6,283✔
1530
  int32_t       ret;
1531
  SEncoder      ec = {0};
6,283✔
1532
  STableMetaRsp vMetaRsp = {0};
6,283✔
1533

1534
  pRsp->msgType = TDMT_VND_ALTER_TABLE_RSP;
6,283✔
1535
  pRsp->pCont = NULL;
6,283✔
1536
  pRsp->contLen = 0;
6,283✔
1537
  pRsp->code = TSDB_CODE_SUCCESS;
6,283✔
1538

1539
  tDecoderInit(&dc, pReq, len);
6,283✔
1540

1541
  // decode
1542
  if (tDecodeSVAlterTbReq(&dc, &vAlterTbReq) < 0) {
6,283!
1543
    vAlterTbRsp.code = TSDB_CODE_INVALID_MSG;
×
1544
    tDecoderClear(&dc);
×
1545
    goto _exit;
×
1546
  }
1547

1548
  // process
1549
  if (metaAlterTable(pVnode->pMeta, ver, &vAlterTbReq, &vMetaRsp) < 0) {
6,283✔
1550
    vAlterTbRsp.code = terrno;
918✔
1551
    tDecoderClear(&dc);
918✔
1552
    goto _exit;
918✔
1553
  }
1554
  tDecoderClear(&dc);
5,365✔
1555

1556
  if (NULL != vMetaRsp.pSchemas) {
5,365✔
1557
    vnodeUpdateMetaRsp(pVnode, &vMetaRsp);
1,584✔
1558
    vAlterTbRsp.pMeta = &vMetaRsp;
1,584✔
1559
  }
1560

1561
  if (vAlterTbReq.action == TSDB_ALTER_TABLE_UPDATE_TAG_VAL ||
5,365✔
1562
      vAlterTbReq.action == TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL) {
1,714✔
1563
    int64_t uid = metaGetTableEntryUidByName(pVnode->pMeta, vAlterTbReq.tbName);
3,661✔
1564
    if (uid == 0) {
3,661!
1565
      vError("vgId:%d, %s failed at %s:%d since table %s not found", TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
1566
             vAlterTbReq.tbName);
1567
      goto _exit;
×
1568
    }
1569

1570
    SArray *tbUids = taosArrayInit(4, sizeof(int64_t));
3,661✔
1571
    void   *p = taosArrayPush(tbUids, &uid);
3,661✔
1572
    TSDB_CHECK_NULL(p, code, lino, _exit, terrno);
3,661!
1573

1574
    vDebug("vgId:%d, remove tags value altered table:%s from query table list", TD_VID(pVnode), vAlterTbReq.tbName);
3,661✔
1575
    if ((code = tqUpdateTbUidList(pVnode->pTq, tbUids, false)) < 0) {
3,661!
1576
      vError("vgId:%d, failed to remove tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1577
    }
1578

1579
    vDebug("vgId:%d, try to add table:%s in query table list", TD_VID(pVnode), vAlterTbReq.tbName);
3,661✔
1580
    if ((code = tqUpdateTbUidList(pVnode->pTq, tbUids, true)) < 0) {
3,661!
1581
      vError("vgId:%d, failed to add tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1582
    }
1583

1584
    taosArrayDestroy(tbUids);
3,661✔
1585
  }
1586

1587
_exit:
1,704✔
1588
  taosArrayDestroy(vAlterTbReq.pMultiTag);
6,283✔
1589
  tEncodeSize(tEncodeSVAlterTbRsp, &vAlterTbRsp, pRsp->contLen, ret);
6,283!
1590
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
6,283✔
1591
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
6,283✔
1592
  if (tEncodeSVAlterTbRsp(&ec, &vAlterTbRsp) != 0) {
6,283!
1593
    vError("vgId:%d, failed to encode alter table response", TD_VID(pVnode));
×
1594
  }
1595

1596
  tEncoderClear(&ec);
6,283✔
1597
  if (vMetaRsp.pSchemas) {
6,283✔
1598
    taosMemoryFree(vMetaRsp.pSchemas);
1,584!
1599
    taosMemoryFree(vMetaRsp.pSchemaExt);
1,584!
1600
  }
1601
  if (vMetaRsp.pColRefs) {
6,283✔
1602
    taosMemoryFree(vMetaRsp.pColRefs);
302!
1603
  }
1604
  return 0;
6,283✔
1605
}
1606

1607
static int32_t vnodeProcessDropTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
15,642✔
1608
                                     SRpcMsg *pOriginRpc) {
1609
  SVDropTbBatchReq req = {0};
15,642✔
1610
  SVDropTbBatchRsp rsp = {0};
15,642✔
1611
  SDecoder         decoder = {0};
15,642✔
1612
  SEncoder         encoder = {0};
15,642✔
1613
  int32_t          ret;
1614
  SArray          *tbUids = NULL;
15,642✔
1615
  STbUidStore     *pStore = NULL;
15,642✔
1616
  SArray          *tbNames = NULL;
15,642✔
1617

1618
  pRsp->msgType = TDMT_VND_DROP_TABLE_RSP;
15,642✔
1619
  pRsp->pCont = NULL;
15,642✔
1620
  pRsp->contLen = 0;
15,642✔
1621
  pRsp->code = TSDB_CODE_SUCCESS;
15,642✔
1622

1623
  // decode req
1624
  tDecoderInit(&decoder, pReq, len);
15,642✔
1625
  ret = tDecodeSVDropTbBatchReq(&decoder, &req);
15,642✔
1626
  if (ret < 0) {
15,642!
1627
    terrno = TSDB_CODE_INVALID_MSG;
×
1628
    pRsp->code = terrno;
×
1629
    goto _exit;
×
1630
  }
1631

1632
  // process req
1633
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
15,642✔
1634
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(SVDropTbRsp));
15,642✔
1635
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
15,642✔
1636
  if (tbUids == NULL || rsp.pArray == NULL || tbNames == NULL) goto _exit;
15,642!
1637

1638
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
31,304✔
1639
    SVDropTbReq *pDropTbReq = req.pReqs + iReq;
15,662✔
1640
    SVDropTbRsp  dropTbRsp = {0};
15,662✔
1641
    tb_uid_t     tbUid = 0;
15,662✔
1642

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

1660
    if (taosArrayPush(rsp.pArray, &dropTbRsp) == NULL) {
31,324!
1661
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1662
      pRsp->code = terrno;
×
1663
      goto _exit;
×
1664
    }
1665

1666
    if (tsEnableAuditCreateTable) {
15,662!
1667
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
15,662!
1668
      if (str == NULL) {
15,662!
1669
        pRsp->code = terrno;
×
1670
        goto _exit;
×
1671
      }
1672
      tstrncpy(str, pDropTbReq->name, TSDB_TABLE_FNAME_LEN);
15,662✔
1673
      if (taosArrayPush(tbNames, &str) == NULL) {
15,662!
1674
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1675
        pRsp->code = terrno;
×
1676
        goto _exit;
×
1677
      }
1678
    }
1679
  }
1680

1681
  if (tqUpdateTbUidList(pVnode->pTq, tbUids, false) < 0) {
15,642!
1682
    vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(terrno));
×
1683
  }
1684

1685
  if (tdUpdateTbUidList(pVnode->pSma, pStore, false) < 0) {
15,642!
1686
    goto _exit;
×
1687
  }
1688

1689
  if (tsEnableAuditCreateTable) {
15,642!
1690
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
15,642✔
1691

1692
    SName name = {0};
15,642✔
1693
    if (tNameFromString(&name, pVnode->config.dbname, T_NAME_ACCT | T_NAME_DB) != 0) {
15,642!
1694
      vError("vgId:%d, failed to get name from string", TD_VID(pVnode));
×
1695
    }
1696

1697
    SStringBuilder sb = {0};
15,642✔
1698
    for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
31,304✔
1699
      char **key = (char **)taosArrayGet(tbNames, iReq);
15,662✔
1700
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
15,662✔
1701
      if (iReq < req.nReqs - 1) {
15,662✔
1702
        taosStringBuilderAppendChar(&sb, ',');
20✔
1703
      }
1704
      taosMemoryFreeClear(*key);
15,662!
1705
    }
1706

1707
    size_t len = 0;
15,642✔
1708
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
15,642✔
1709

1710
    if (pOriginRpc->info.conn.user != NULL && strlen(pOriginRpc->info.conn.user) > 0) {
15,642!
1711
      auditAddRecord(pOriginRpc, clusterId, "dropTable", name.dbname, "", keyJoined, len);
15,600✔
1712
    }
1713

1714
    taosStringBuilderDestroy(&sb);
15,642✔
1715
  }
1716

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

1733
#ifdef BUILD_NO_CALL
1734
static int32_t vnodeDebugPrintSingleSubmitMsg(SMeta *pMeta, SSubmitBlk *pBlock, SSubmitMsgIter *msgIter,
1735
                                              const char *tags) {
1736
  SSubmitBlkIter blkIter = {0};
1737
  STSchema      *pSchema = NULL;
1738
  tb_uid_t       suid = 0;
1739
  STSRow        *row = NULL;
1740
  int32_t        rv = -1;
1741

1742
  tInitSubmitBlkIter(msgIter, pBlock, &blkIter);
1743
  if (blkIter.row == NULL) return 0;
1744

1745
  int32_t code = metaGetTbTSchemaNotNull(pMeta, msgIter->suid, TD_ROW_SVER(blkIter.row), 1,
1746
                                         &pSchema);  // TODO: use the real schema
1747
  if (TSDB_CODE_SUCCESS != code) {
1748
    printf("%s:%d no valid schema\n", tags, __LINE__);
1749
    return code;
1750
  }
1751

1752
  suid = msgIter->suid;
1753
  rv = TD_ROW_SVER(blkIter.row);
1754

1755
  char __tags[128] = {0};
1756
  snprintf(__tags, 128, "%s: uid %" PRIi64 " ", tags, msgIter->uid);
1757
  while ((row = tGetSubmitBlkNext(&blkIter))) {
1758
    tdSRowPrint(row, pSchema, __tags);
1759
  }
1760

1761
  taosMemoryFreeClear(pSchema);
1762

1763
  return TSDB_CODE_SUCCESS;
1764
}
1765
#endif
1766
typedef struct SSubmitReqConvertCxt {
1767
  SSubmitMsgIter msgIter;
1768
  SSubmitBlk    *pBlock;
1769
  SSubmitBlkIter blkIter;
1770
  STSRow        *pRow;
1771
  STSRowIter     rowIter;
1772
  SSubmitTbData *pTbData;
1773
  STSchema      *pTbSchema;
1774
  SArray        *pColValues;
1775
} SSubmitReqConvertCxt;
1776

1777
static int32_t vnodeResetTableCxt(SMeta *pMeta, SSubmitReqConvertCxt *pCxt) {
×
1778
  taosMemoryFreeClear(pCxt->pTbSchema);
×
1779
  int32_t code = metaGetTbTSchemaNotNull(pMeta, pCxt->msgIter.suid > 0 ? pCxt->msgIter.suid : pCxt->msgIter.uid,
×
1780
                                         pCxt->msgIter.sversion, 1, &pCxt->pTbSchema);
1781
  if (TSDB_CODE_SUCCESS != code) {
×
1782
    return code;
×
1783
  }
1784
  tdSTSRowIterInit(&pCxt->rowIter, pCxt->pTbSchema);
×
1785

1786
  tDestroySubmitTbData(pCxt->pTbData, TSDB_MSG_FLG_ENCODE);
×
1787
  if (NULL == pCxt->pTbData) {
×
1788
    pCxt->pTbData = taosMemoryCalloc(1, sizeof(SSubmitTbData));
×
1789
    if (NULL == pCxt->pTbData) {
×
1790
      return terrno;
×
1791
    }
1792
  }
1793
  pCxt->pTbData->flags = 0;
×
1794
  pCxt->pTbData->suid = pCxt->msgIter.suid;
×
1795
  pCxt->pTbData->uid = pCxt->msgIter.uid;
×
1796
  pCxt->pTbData->sver = pCxt->msgIter.sversion;
×
1797
  pCxt->pTbData->pCreateTbReq = NULL;
×
1798
  pCxt->pTbData->aRowP = taosArrayInit(128, POINTER_BYTES);
×
1799
  if (NULL == pCxt->pTbData->aRowP) {
×
1800
    return terrno;
×
1801
  }
1802

1803
  taosArrayDestroy(pCxt->pColValues);
×
1804
  pCxt->pColValues = taosArrayInit(pCxt->pTbSchema->numOfCols, sizeof(SColVal));
×
1805
  if (NULL == pCxt->pColValues) {
×
1806
    return terrno;
×
1807
  }
1808
  for (int32_t i = 0; i < pCxt->pTbSchema->numOfCols; ++i) {
×
1809
    SColVal val = COL_VAL_NONE(pCxt->pTbSchema->columns[i].colId, pCxt->pTbSchema->columns[i].type);
×
1810
    if (taosArrayPush(pCxt->pColValues, &val) == NULL) {
×
1811
      return terrno;
×
1812
    }
1813
  }
1814

1815
  return TSDB_CODE_SUCCESS;
×
1816
}
1817

1818
static void vnodeDestroySubmitReqConvertCxt(SSubmitReqConvertCxt *pCxt) {
×
1819
  taosMemoryFreeClear(pCxt->pTbSchema);
×
1820
  tDestroySubmitTbData(pCxt->pTbData, TSDB_MSG_FLG_ENCODE);
×
1821
  taosMemoryFreeClear(pCxt->pTbData);
×
1822
  taosArrayDestroy(pCxt->pColValues);
×
1823
}
×
1824

1825
static int32_t vnodeCellValConvertToColVal(STColumn *pCol, SCellVal *pCellVal, SColVal *pColVal) {
×
1826
  if (tdValTypeIsNone(pCellVal->valType)) {
×
1827
    pColVal->flag = CV_FLAG_NONE;
×
1828
    return TSDB_CODE_SUCCESS;
×
1829
  }
1830

1831
  if (tdValTypeIsNull(pCellVal->valType)) {
×
1832
    pColVal->flag = CV_FLAG_NULL;
×
1833
    return TSDB_CODE_SUCCESS;
×
1834
  }
1835

1836
  if (IS_VAR_DATA_TYPE(pCol->type)) {
×
1837
    pColVal->value.nData = varDataLen(pCellVal->val);
×
1838
    pColVal->value.pData = (uint8_t *)varDataVal(pCellVal->val);
×
1839
  } else if (TSDB_DATA_TYPE_FLOAT == pCol->type) {
×
1840
    float f = GET_FLOAT_VAL(pCellVal->val);
×
1841
    valueSetDatum(&pColVal->value, pCol->type, &f, sizeof(f));
×
1842
  } else if (TSDB_DATA_TYPE_DOUBLE == pCol->type) {
×
1843
    taosSetPInt64Aligned(&pColVal->value.val, (int64_t *)pCellVal->val);
×
1844
  } else {
1845
    valueSetDatum(&pColVal->value, pCol->type, pCellVal->val, tDataTypes[pCol->type].bytes);
×
1846
  }
1847

1848
  pColVal->flag = CV_FLAG_VALUE;
×
1849
  return TSDB_CODE_SUCCESS;
×
1850
}
1851

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

1866
static int32_t vnodeDecodeCreateTbReq(SSubmitReqConvertCxt *pCxt) {
×
1867
  if (pCxt->msgIter.schemaLen <= 0) {
×
1868
    return TSDB_CODE_SUCCESS;
×
1869
  }
1870

1871
  pCxt->pTbData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
×
1872
  if (NULL == pCxt->pTbData->pCreateTbReq) {
×
1873
    return terrno;
×
1874
  }
1875

1876
  SDecoder decoder = {0};
×
1877
  tDecoderInit(&decoder, (uint8_t *)pCxt->pBlock->data, pCxt->msgIter.schemaLen);
×
1878
  int32_t code = tDecodeSVCreateTbReq(&decoder, pCxt->pTbData->pCreateTbReq);
×
1879
  tDecoderClear(&decoder);
×
1880

1881
  return code;
×
1882
}
1883

1884
static int32_t vnodeSubmitReqConvertToSubmitReq2(SVnode *pVnode, SSubmitReq *pReq, SSubmitReq2 *pReq2) {
×
1885
  pReq2->aSubmitTbData = taosArrayInit(128, sizeof(SSubmitTbData));
×
1886
  if (NULL == pReq2->aSubmitTbData) {
×
1887
    return terrno;
×
1888
  }
1889

1890
  SSubmitReqConvertCxt cxt = {0};
×
1891

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

1922
  vnodeDestroySubmitReqConvertCxt(&cxt);
×
1923
  return code;
×
1924
}
1925

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

1949
static int32_t vnodeProcessSubmitReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
14,357,191✔
1950
                                     SRpcMsg *pOriginalMsg) {
1951
  int32_t code = 0;
14,357,191✔
1952
  terrno = 0;
14,357,191✔
1953

1954
  SSubmitReq2 *pSubmitReq = &(SSubmitReq2){0};
14,357,467✔
1955
  SSubmitRsp2 *pSubmitRsp = &(SSubmitRsp2){0};
14,357,467✔
1956
  SArray      *newTbUids = NULL;
14,357,467✔
1957
  int32_t      ret;
1958
  SEncoder     ec = {0};
14,357,467✔
1959

1960
  pRsp->code = TSDB_CODE_SUCCESS;
14,357,467✔
1961

1962
  void           *pAllocMsg = NULL;
14,357,467✔
1963
  SSubmitReq2Msg *pMsg = (SSubmitReq2Msg *)pReq;
14,357,467✔
1964
  if (0 == pMsg->version) {
14,357,467!
1965
    code = vnodeSubmitReqConvertToSubmitReq2(pVnode, (SSubmitReq *)pMsg, pSubmitReq);
×
1966
    if (TSDB_CODE_SUCCESS == code) {
×
1967
      code = vnodeRebuildSubmitReqMsg(pSubmitReq, &pReq);
×
1968
    }
1969
    if (TSDB_CODE_SUCCESS == code) {
×
1970
      pAllocMsg = pReq;
×
1971
    }
1972
    if (TSDB_CODE_SUCCESS != code) {
×
1973
      goto _exit;
×
1974
    }
1975
  } else {
1976
    // decode
1977
    pReq = POINTER_SHIFT(pReq, sizeof(SSubmitReq2Msg));
14,357,467✔
1978
    len -= sizeof(SSubmitReq2Msg);
14,357,467✔
1979
    SDecoder dc = {0};
14,357,467✔
1980
    tDecoderInit(&dc, pReq, len);
14,357,467✔
1981
    if (tDecodeSubmitReq(&dc, pSubmitReq, NULL) < 0) {
14,357,234!
1982
      code = TSDB_CODE_INVALID_MSG;
×
1983
      goto _exit;
×
1984
    }
1985
    tDecoderClear(&dc);
14,357,361✔
1986
  }
1987

1988
  // scan
1989
  TSKEY now = taosGetTimestamp(pVnode->config.tsdbCfg.precision);
14,357,581✔
1990
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * pVnode->config.tsdbCfg.keep2;
14,357,273✔
1991
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
14,357,273✔
1992
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
29,449,801✔
1993
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
15,092,475✔
1994

1995
    if (pSubmitTbData->pCreateTbReq && pSubmitTbData->pCreateTbReq->uid == 0) {
15,092,527!
1996
      code = TSDB_CODE_INVALID_MSG;
×
1997
      goto _exit;
×
1998
    }
1999

2000
    if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
15,092,527✔
2001
      if (TARRAY_SIZE(pSubmitTbData->aCol) <= 0) {
13,729!
2002
        code = TSDB_CODE_INVALID_MSG;
×
2003
        goto _exit;
×
2004
      }
2005

2006
      SColData *colDataArr = TARRAY_DATA(pSubmitTbData->aCol);
13,729✔
2007
      SRowKey   lastKey;
2008
      tColDataArrGetRowKey(colDataArr, TARRAY_SIZE(pSubmitTbData->aCol), 0, &lastKey);
13,729✔
2009
      for (int32_t iRow = 1; iRow < colDataArr[0].nVal; iRow++) {
21,491,239✔
2010
        SRowKey key;
2011
        tColDataArrGetRowKey(TARRAY_DATA(pSubmitTbData->aCol), TARRAY_SIZE(pSubmitTbData->aCol), iRow, &key);
21,476,973✔
2012
        if (tRowKeyCompare(&lastKey, &key) >= 0) {
21,476,144!
2013
          code = TSDB_CODE_INVALID_MSG;
×
2014
          vError("vgId:%d %s failed 1 since %s, version:%" PRId64, TD_VID(pVnode), __func__, tstrerror(terrno), ver);
×
2015
          goto _exit;
×
2016
        }
2017
      }
2018
    } else {
2019
      int32_t nRow = TARRAY_SIZE(pSubmitTbData->aRowP);
15,078,798✔
2020
      SRow  **aRow = (SRow **)TARRAY_DATA(pSubmitTbData->aRowP);
15,078,798✔
2021
      SRowKey lastRowKey;
2022
      for (int32_t iRow = 0; iRow < nRow; ++iRow) {
805,023,055✔
2023
#ifndef NO_UNALIGNED_ACCESS
2024
        if (aRow[iRow]->ts < minKey || aRow[iRow]->ts > maxKey) {
789,944,793✔
2025
#else
2026
        TSKEY ts = taosGetInt64Aligned(&(aRow[iRow]->ts));
2027
        if (ts < minKey || ts > maxKey) {
2028
#endif
2029
          code = TSDB_CODE_INVALID_MSG;
74,772✔
2030
          vError("vgId:%d %s failed 2 since %s, version:%" PRId64, TD_VID(pVnode), __func__, tstrerror(code), ver);
74,772!
2031
          goto _exit;
×
2032
        }
2033
        if (iRow == 0) {
789,870,021✔
2034
          tRowGetKey(aRow[iRow], &lastRowKey);
30,157,524✔
2035
        } else {
2036
          SRowKey rowKey;
2037
          tRowGetKey(aRow[iRow], &rowKey);
1,549,582,518✔
2038

2039
          if (tRowKeyCompare(&lastRowKey, &rowKey) >= 0) {
774,786,858!
2040
            code = TSDB_CODE_INVALID_MSG;
×
2041
            vError("vgId:%d %s failed 3 since %s, version:%" PRId64, TD_VID(pVnode), __func__, tstrerror(code), ver);
×
2042
            goto _exit;
×
2043
          }
2044
          lastRowKey = rowKey;
774,867,742✔
2045
        }
2046
      }
2047
    }
2048
  }
2049

2050
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
29,450,138✔
2051
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
15,092,451✔
2052

2053
    if (pSubmitTbData->pCreateTbReq) {
15,092,395✔
2054
      pSubmitTbData->uid = pSubmitTbData->pCreateTbReq->uid;
87,906✔
2055
    } else {
2056
      SMetaInfo info = {0};
15,004,489✔
2057

2058
      code = metaGetInfo(pVnode->pMeta, pSubmitTbData->uid, &info, NULL);
15,004,489✔
2059
      if (code) {
15,004,707!
2060
        code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
2061
        vWarn("vgId:%d, table uid:%" PRId64 " not exists", TD_VID(pVnode), pSubmitTbData->uid);
×
2062
        goto _exit;
1✔
2063
      }
2064

2065
      if (info.suid != pSubmitTbData->suid) {
15,004,739!
2066
        code = TSDB_CODE_INVALID_MSG;
×
2067
        goto _exit;
×
2068
      }
2069

2070
      if (info.suid) {
15,004,739✔
2071
        if (metaGetInfo(pVnode->pMeta, info.suid, &info, NULL) != 0) {
13,702,144!
2072
          vWarn("vgId:%d, table uid:%" PRId64 " not exists", TD_VID(pVnode), info.suid);
×
2073
        }
2074
      }
2075

2076
      if (pSubmitTbData->sver != info.skmVer) {
15,004,907✔
2077
        code = TSDB_CODE_TDB_INVALID_TABLE_SCHEMA_VER;
1✔
2078
        goto _exit;
1✔
2079
      }
2080
    }
2081

2082
    if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
15,092,812✔
2083
      int32_t   nColData = TARRAY_SIZE(pSubmitTbData->aCol);
13,727✔
2084
      SColData *aColData = (SColData *)TARRAY_DATA(pSubmitTbData->aCol);
13,727✔
2085

2086
      if (nColData <= 0) {
13,727!
2087
        code = TSDB_CODE_INVALID_MSG;
×
2088
        goto _exit;
×
2089
      }
2090

2091
      if (aColData[0].cid != PRIMARYKEY_TIMESTAMP_COL_ID || aColData[0].type != TSDB_DATA_TYPE_TIMESTAMP ||
13,727!
2092
          aColData[0].nVal <= 0) {
13,727!
2093
        code = TSDB_CODE_INVALID_MSG;
×
2094
        goto _exit;
×
2095
      }
2096

2097
      for (int32_t j = 1; j < nColData; j++) {
41,382✔
2098
        if (aColData[j].nVal != aColData[0].nVal) {
27,655!
2099
          code = TSDB_CODE_INVALID_MSG;
×
2100
          goto _exit;
×
2101
        }
2102
      }
2103
    }
2104
  }
2105

2106
  vGDebug(pOriginalMsg ? &pOriginalMsg->info.traceId : NULL, "vgId:%d, index:%" PRId64 ", submit block, rows:%d",
14,357,687!
2107
          TD_VID(pVnode), ver, (int32_t)taosArrayGetSize(pSubmitReq->aSubmitTbData));
2108

2109
  // loop to handle
2110
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
29,450,229✔
2111
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
15,092,667✔
2112

2113
    // create table
2114
    if (pSubmitTbData->pCreateTbReq) {
15,092,755✔
2115
      // alloc if need
2116
      if (pSubmitRsp->aCreateTbRsp == NULL &&
87,970✔
2117
          (pSubmitRsp->aCreateTbRsp = taosArrayInit(TARRAY_SIZE(pSubmitReq->aSubmitTbData), sizeof(SVCreateTbRsp))) ==
71,106!
2118
              NULL) {
2119
        code = terrno;
×
2120
        goto _exit;
8✔
2121
      }
2122

2123
      SVCreateTbRsp *pCreateTbRsp = taosArrayReserve(pSubmitRsp->aCreateTbRsp, 1);
87,970✔
2124

2125
      // create table
2126
      if (metaCreateTable2(pVnode->pMeta, ver, pSubmitTbData->pCreateTbReq, &pCreateTbRsp->pMeta) == 0) {
87,968✔
2127
        // create table success
2128

2129
        if (newTbUids == NULL &&
146,616!
2130
            (newTbUids = taosArrayInit(TARRAY_SIZE(pSubmitReq->aSubmitTbData), sizeof(int64_t))) == NULL) {
67,793✔
2131
          code = terrno;
×
2132
          goto _exit;
×
2133
        }
2134

2135
        if (taosArrayPush(newTbUids, &pSubmitTbData->uid) == NULL) {
157,646!
2136
          code = terrno;
×
2137
          goto _exit;
×
2138
        }
2139

2140
        if (pCreateTbRsp->pMeta) {
78,823!
2141
          vnodeUpdateMetaRsp(pVnode, pCreateTbRsp->pMeta);
78,823✔
2142
        }
2143
      } else {  // create table failed
2144
        if (terrno != TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
9,149✔
2145
          code = terrno;
8✔
2146
          vError("vgId:%d failed to create table:%s, code:%s", TD_VID(pVnode), pSubmitTbData->pCreateTbReq->name,
8!
2147
                 tstrerror(terrno));
2148
          goto _exit;
8✔
2149
        }
2150
        terrno = 0;
9,140✔
2151
        pSubmitTbData->uid = pSubmitTbData->pCreateTbReq->uid;  // update uid if table exist for using below
9,140✔
2152
      }
2153
    }
2154

2155
    // insert data
2156
    int32_t affectedRows;
2157
    code = tsdbInsertTableData(pVnode->pTsdb, ver, pSubmitTbData, &affectedRows);
15,092,748✔
2158
    if (code) goto _exit;
15,092,781!
2159

2160
    code = metaUpdateChangeTimeWithLock(pVnode->pMeta, pSubmitTbData->uid, pSubmitTbData->ctimeMs);
15,092,781✔
2161
    if (code) goto _exit;
15,092,724!
2162

2163
    pSubmitRsp->affectedRows += affectedRows;
15,092,724✔
2164
  }
2165

2166
  // update the affected table uid list
2167
  if (taosArrayGetSize(newTbUids) > 0) {
14,357,562✔
2168
    vDebug("vgId:%d, add %d table into query table list in handling submit", TD_VID(pVnode),
67,793✔
2169
           (int32_t)taosArrayGetSize(newTbUids));
2170
    if (tqUpdateTbUidList(pVnode->pTq, newTbUids, true) != 0) {
67,793!
2171
      vError("vgId:%d, failed to update tbUid list", TD_VID(pVnode));
×
2172
    }
2173
  }
2174

2175
_exit:
14,357,480✔
2176
  // message
2177
  pRsp->code = code;
14,357,489✔
2178
  tEncodeSize(tEncodeSSubmitRsp2, pSubmitRsp, pRsp->contLen, ret);
14,357,489!
2179
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
14,357,226✔
2180
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
14,357,517✔
2181
  if (tEncodeSSubmitRsp2(&ec, pSubmitRsp) < 0) {
14,357,537!
2182
    vError("vgId:%d, failed to encode submit response", TD_VID(pVnode));
×
2183
  }
2184
  tEncoderClear(&ec);
14,357,517✔
2185

2186
  // update statistics
2187
  (void)atomic_add_fetch_64(&pVnode->statis.nInsert, pSubmitRsp->affectedRows);
14,357,445✔
2188
  (void)atomic_add_fetch_64(&pVnode->statis.nInsertSuccess, pSubmitRsp->affectedRows);
14,357,625✔
2189
  (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsert, 1);
14,357,611✔
2190

2191
  // update metrics
2192
  METRICS_UPDATE(pVnode->writeMetrics.total_requests, METRIC_LEVEL_LOW, 1);
14,357,609!
2193
  METRICS_UPDATE(pVnode->writeMetrics.total_rows, METRIC_LEVEL_HIGH, pSubmitRsp->affectedRows);
14,357,609!
2194
  METRICS_UPDATE(pVnode->writeMetrics.total_bytes, METRIC_LEVEL_LOW, pMsg->header.contLen);
14,357,609!
2195

2196
  if (tsEnableMonitor && tsMonitorFqdn[0] != 0 && tsMonitorPort != 0 && pSubmitRsp->affectedRows > 0 &&
14,357,609!
2197
      strlen(pOriginalMsg->info.conn.user) > 0 && tsInsertCounter != NULL) {
100!
2198
    const char *sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT_AFFECTED_ROWS,
100✔
2199
                                   pVnode->monitor.strClusterId,
100✔
2200
                                   pVnode->monitor.strDnodeId,
100✔
2201
                                   tsLocalEp,
2202
                                   pVnode->monitor.strVgId,
100✔
2203
                                   pOriginalMsg->info.conn.user,
100✔
2204
                                   "Success"};
2205
    int         tv = taos_counter_add(tsInsertCounter, pSubmitRsp->affectedRows, sample_labels);
100✔
2206
  }
2207

2208
  if (code == 0) {
14,357,609✔
2209
    (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
14,357,334✔
2210
    code = tdProcessRSmaSubmit(pVnode->pSma, ver, pSubmitReq, pReq, len);
14,357,615✔
2211
  }
2212
  /*
2213
  if (code == 0) {
2214
    atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
2215
    code = tdProcessRSmaSubmit(pVnode->pSma, ver, pSubmitReq, pReq, len);
2216

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, "Success"};
2220
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2221
  }
2222
  else{
2223
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT, pVnode->monitor.strClusterId,
2224
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2225
                                        pOriginalMsg->info.conn.user, "Failed"};
2226
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2227
  }
2228
  */
2229

2230
  // clear
2231
  taosArrayDestroy(newTbUids);
14,357,691✔
2232
  tDestroySubmitReq(pSubmitReq, 0 == pMsg->version ? TSDB_MSG_FLG_CMPT : TSDB_MSG_FLG_DECODE);
14,357,258!
2233
  tDestroySSubmitRsp2(pSubmitRsp, TSDB_MSG_FLG_ENCODE);
14,357,548✔
2234

2235
  if (code) terrno = code;
14,357,309✔
2236

2237
  taosMemoryFree(pAllocMsg);
14,357,309!
2238

2239
  return code;
14,357,308✔
2240
}
2241

2242
static int32_t vnodeProcessCreateTSmaReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
2243
#ifdef USE_TSMA
2244
  SVCreateTSmaReq req = {0};
×
2245
  SDecoder        coder = {0};
×
2246

2247
  if (pRsp) {
×
2248
    pRsp->msgType = TDMT_VND_CREATE_SMA_RSP;
×
2249
    pRsp->code = TSDB_CODE_SUCCESS;
×
2250
    pRsp->pCont = NULL;
×
2251
    pRsp->contLen = 0;
×
2252
  }
2253

2254
  // decode and process req
2255
  tDecoderInit(&coder, pReq, len);
×
2256

2257
  if (tDecodeSVCreateTSmaReq(&coder, &req) < 0) {
×
2258
    terrno = TSDB_CODE_MSG_DECODE_ERROR;
×
2259
    if (pRsp) pRsp->code = terrno;
×
2260
    goto _err;
×
2261
  }
2262

2263
  if (tdProcessTSmaCreate(pVnode->pSma, ver, (const char *)&req) < 0) {
×
2264
    if (pRsp) pRsp->code = terrno;
×
2265
    goto _err;
×
2266
  }
2267

2268
  tDecoderClear(&coder);
×
2269
  vDebug("vgId:%d, success to create tsma %s:%" PRIi64 " version %" PRIi64 " for table %" PRIi64, TD_VID(pVnode),
×
2270
         req.indexName, req.indexUid, ver, req.tableUid);
2271
  return 0;
×
2272

2273
_err:
×
2274
  tDecoderClear(&coder);
×
2275
  vError("vgId:%d, failed to create tsma %s:%" PRIi64 " version %" PRIi64 "for table %" PRIi64 " since %s",
×
2276
         TD_VID(pVnode), req.indexName, req.indexUid, ver, req.tableUid, terrstr());
2277
  return terrno;
×
2278
#else
2279
  return TSDB_CODE_INTERNAL_ERROR;
2280
#endif
2281
}
2282

2283
/**
2284
 * @brief specific for smaDstVnode
2285
 *
2286
 * @param pVnode
2287
 * @param pCont
2288
 * @param contLen
2289
 * @return int32_t
2290
 */
2291
int32_t vnodeProcessCreateTSma(SVnode *pVnode, void *pCont, uint32_t contLen) {
×
2292
  return vnodeProcessCreateTSmaReq(pVnode, 1, pCont, contLen, NULL);
×
2293
}
2294

2295
static int32_t vnodeConsolidateAlterHashRange(SVnode *pVnode, int64_t ver) {
44✔
2296
  int32_t code = TSDB_CODE_SUCCESS;
44✔
2297

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

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

2304
  return code;
44✔
2305
}
2306

2307
static int32_t vnodeProcessAlterConfirmReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
15,043✔
2308
  vInfo("vgId:%d, vnode handle msgType:alter-confirm, alter confirm msg is processed", TD_VID(pVnode));
15,043!
2309
  int32_t code = TSDB_CODE_SUCCESS;
15,043✔
2310
  if (!pVnode->config.hashChange) {
15,043✔
2311
    goto _exit;
14,999✔
2312
  }
2313

2314
  code = vnodeConsolidateAlterHashRange(pVnode, ver);
44✔
2315
  if (code < 0) {
44!
2316
    vError("vgId:%d, failed to consolidate alter hashrange since %s. version:%" PRId64, TD_VID(pVnode), terrstr(), ver);
×
2317
    goto _exit;
×
2318
  }
2319
  pVnode->config.hashChange = false;
44✔
2320

2321
_exit:
15,043✔
2322
  pRsp->msgType = TDMT_VND_ALTER_CONFIRM_RSP;
15,043✔
2323
  pRsp->code = code;
15,043✔
2324
  pRsp->pCont = NULL;
15,043✔
2325
  pRsp->contLen = 0;
15,043✔
2326

2327
  return code;
15,043✔
2328
}
2329

2330
extern int32_t tsdbDisableAndCancelAllBgTask(STsdb *pTsdb);
2331
extern void    tsdbEnableBgTask(STsdb *pTsdb);
2332

2333
static int32_t vnodeProcessAlterConfigReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
1,112✔
2334
  bool walChanged = false;
1,112✔
2335
  bool tsdbChanged = false;
1,112✔
2336

2337
  SAlterVnodeConfigReq req = {0};
1,112✔
2338
  if (tDeserializeSAlterVnodeConfigReq(pReq, len, &req) != 0) {
1,112!
2339
    terrno = TSDB_CODE_INVALID_MSG;
×
2340
    return TSDB_CODE_INVALID_MSG;
×
2341
  }
2342

2343
  vInfo("vgId:%d, start to alter vnode config, page:%d pageSize:%d buffer:%d szPage:%d szBuf:%" PRIu64
1,112!
2344
        " cacheLast:%d cacheLastSize:%d days:%d keep0:%d keep1:%d keep2:%d keepTimeOffset:%d s3KeepLocal:%d "
2345
        "s3Compact:%d fsync:%d level:%d "
2346
        "walRetentionPeriod:%d walRetentionSize:%d",
2347
        TD_VID(pVnode), req.pages, req.pageSize, req.buffer, req.pageSize * 1024, (uint64_t)req.buffer * 1024 * 1024,
2348
        req.cacheLast, req.cacheLastSize, req.daysPerFile, req.daysToKeep0, req.daysToKeep1, req.daysToKeep2,
2349
        req.keepTimeOffset, req.s3KeepLocal, req.s3Compact, req.walFsyncPeriod, req.walLevel, req.walRetentionPeriod,
2350
        req.walRetentionSize);
2351

2352
  if (pVnode->config.cacheLastSize != req.cacheLastSize) {
1,112✔
2353
    pVnode->config.cacheLastSize = req.cacheLastSize;
444✔
2354
    tsdbCacheSetCapacity(pVnode, (size_t)pVnode->config.cacheLastSize * 1024 * 1024);
444✔
2355
  }
2356

2357
  if (pVnode->config.szBuf != req.buffer * 1024LL * 1024LL) {
1,112!
2358
    vInfo("vgId:%d, vnode buffer is changed from %" PRId64 " to %" PRId64, TD_VID(pVnode), pVnode->config.szBuf,
×
2359
          (uint64_t)(req.buffer * 1024LL * 1024LL));
2360
    pVnode->config.szBuf = req.buffer * 1024LL * 1024LL;
×
2361
  }
2362

2363
  if (pVnode->config.szCache != req.pages) {
1,112!
2364
    if ((terrno = metaAlterCache(pVnode->pMeta, req.pages)) < 0) {
×
2365
      vError("vgId:%d, failed to change vnode pages from %d to %d failed since %s", TD_VID(pVnode),
×
2366
             pVnode->config.szCache, req.pages, tstrerror(terrno));
2367
      return terrno;
×
2368
    } else {
2369
      vInfo("vgId:%d, vnode pages is changed from %d to %d", TD_VID(pVnode), pVnode->config.szCache, req.pages);
×
2370
      pVnode->config.szCache = req.pages;
×
2371
    }
2372
  }
2373

2374
  if (pVnode->config.cacheLast != req.cacheLast) {
1,112✔
2375
    pVnode->config.cacheLast = req.cacheLast;
718✔
2376
  }
2377

2378
  if (pVnode->config.walCfg.fsyncPeriod != req.walFsyncPeriod) {
1,112✔
2379
    pVnode->config.walCfg.fsyncPeriod = req.walFsyncPeriod;
108✔
2380
    walChanged = true;
108✔
2381
  }
2382

2383
  if (pVnode->config.walCfg.level != req.walLevel) {
1,112✔
2384
    if (pVnode->config.walCfg.level == 0) {
72!
2385
      pVnode->config.walCfg.clearFiles = 1;
×
2386
    }
2387
    pVnode->config.walCfg.level = req.walLevel;
72✔
2388
    walChanged = true;
72✔
2389
  }
2390

2391
  if (pVnode->config.walCfg.retentionPeriod != req.walRetentionPeriod) {
1,112✔
2392
    pVnode->config.walCfg.retentionPeriod = req.walRetentionPeriod;
132✔
2393
    walChanged = true;
132✔
2394
  }
2395

2396
  if (pVnode->config.walCfg.retentionSize != req.walRetentionSize) {
1,112!
2397
    pVnode->config.walCfg.retentionSize = req.walRetentionSize;
×
2398
    walChanged = true;
×
2399
  }
2400

2401
  if (pVnode->config.tsdbCfg.keep0 != req.daysToKeep0) {
1,112✔
2402
    pVnode->config.tsdbCfg.keep0 = req.daysToKeep0;
74✔
2403
    if (!VND_IS_RSMA(pVnode)) {
74!
2404
      tsdbChanged = true;
74✔
2405
    }
2406
  }
2407

2408
  if (pVnode->config.tsdbCfg.keep1 != req.daysToKeep1) {
1,112✔
2409
    pVnode->config.tsdbCfg.keep1 = req.daysToKeep1;
74✔
2410
    if (!VND_IS_RSMA(pVnode)) {
74!
2411
      tsdbChanged = true;
74✔
2412
    }
2413
  }
2414

2415
  if (pVnode->config.tsdbCfg.keep2 != req.daysToKeep2) {
1,112✔
2416
    pVnode->config.tsdbCfg.keep2 = req.daysToKeep2;
76✔
2417
    if (!VND_IS_RSMA(pVnode)) {
76!
2418
      tsdbChanged = true;
76✔
2419
    }
2420
  }
2421

2422
  if (pVnode->config.tsdbCfg.keepTimeOffset != req.keepTimeOffset) {
1,112✔
2423
    pVnode->config.tsdbCfg.keepTimeOffset = req.keepTimeOffset;
2✔
2424
    if (!VND_IS_RSMA(pVnode)) {
2!
2425
      tsdbChanged = true;
2✔
2426
    }
2427
  }
2428

2429
  if (req.sttTrigger != -1 && req.sttTrigger != pVnode->config.sttTrigger) {
1,112!
2430
    if (req.sttTrigger > 1 && pVnode->config.sttTrigger > 1) {
×
2431
      pVnode->config.sttTrigger = req.sttTrigger;
×
2432
    } else {
2433
      vnodeAWait(&pVnode->commitTask);
×
2434

2435
      int32_t ret = tsdbDisableAndCancelAllBgTask(pVnode->pTsdb);
×
2436
      if (ret != 0) {
×
2437
        vError("vgId:%d, failed to disable bg task since %s", TD_VID(pVnode), tstrerror(ERRNO));
×
2438
      }
2439

2440
      pVnode->config.sttTrigger = req.sttTrigger;
×
2441
      tsdbEnableBgTask(pVnode->pTsdb);
×
2442
    }
2443
  }
2444

2445
  if (req.minRows != -1 && req.minRows != pVnode->config.tsdbCfg.minRows) {
1,112!
2446
    pVnode->config.tsdbCfg.minRows = req.minRows;
×
2447
  }
2448

2449
  if (req.s3KeepLocal != -1 && req.s3KeepLocal != pVnode->config.s3KeepLocal) {
1,112!
2450
    pVnode->config.s3KeepLocal = req.s3KeepLocal;
×
2451
  }
2452
  if (req.s3Compact != -1 && req.s3Compact != pVnode->config.s3Compact) {
1,112!
2453
    pVnode->config.s3Compact = req.s3Compact;
×
2454
  }
2455

2456
  if (walChanged) {
1,112✔
2457
    if (walAlter(pVnode->pWal, &pVnode->config.walCfg) != 0) {
312!
2458
      vError("vgId:%d, failed to alter wal config since %s", TD_VID(pVnode), tstrerror(ERRNO));
×
2459
    }
2460
  }
2461

2462
  if (tsdbChanged) {
1,112✔
2463
    tsdbSetKeepCfg(pVnode->pTsdb, &pVnode->config.tsdbCfg);
78✔
2464
  }
2465

2466
  return 0;
1,112✔
2467
}
2468

2469
static int32_t vnodeProcessBatchDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
1,928✔
2470
  SBatchDeleteReq deleteReq;
2471
  SDecoder        decoder;
2472
  tDecoderInit(&decoder, pReq, len);
1,928✔
2473
  if (tDecodeSBatchDeleteReq(&decoder, &deleteReq) < 0) {
1,928!
2474
    tDecoderClear(&decoder);
×
2475
    return terrno = TSDB_CODE_INVALID_MSG;
×
2476
  }
2477

2478
  SMetaReader mr = {0};
1,928✔
2479
  metaReaderDoInit(&mr, pVnode->pMeta, META_READER_NOLOCK);
1,928✔
2480
  STsdb *pTsdb = pVnode->pTsdb;
1,927✔
2481

2482
  if (deleteReq.level) {
1,927✔
2483
    pTsdb = deleteReq.level == 1 ? VND_RSMA1(pVnode) : VND_RSMA2(pVnode);
16✔
2484
  }
2485

2486
  int32_t sz = taosArrayGetSize(deleteReq.deleteReqs);
1,927✔
2487
  for (int32_t i = 0; i < sz; i++) {
4,670✔
2488
    SSingleDeleteReq *pOneReq = taosArrayGet(deleteReq.deleteReqs, i);
2,743✔
2489
    char             *name = pOneReq->tbname;
2,743✔
2490
    if (metaGetTableEntryByName(&mr, name) < 0) {
2,743✔
2491
      vDebug("vgId:%d, stream delete msg, skip since no table: %s", pVnode->config.vgId, name);
1,261✔
2492
      continue;
1,260✔
2493
    }
2494

2495
    int64_t uid = mr.me.uid;
1,483✔
2496

2497
    int32_t code = tsdbDeleteTableData(pTsdb, ver, deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
1,483✔
2498
    if (code < 0) {
1,483!
2499
      terrno = code;
×
2500
      vError("vgId:%d, delete error since %s, suid:%" PRId64 ", uid:%" PRId64 ", start ts:%" PRId64 ", end ts:%" PRId64,
×
2501
             TD_VID(pVnode), terrstr(), deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
2502
    }
2503

2504
    if (deleteReq.level == 0) {
1,483✔
2505
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, deleteReq.ctimeMs);
867✔
2506
      if (code < 0) {
867!
2507
        terrno = code;
×
2508
        vError("vgId:%d, update change time error since %s, suid:%" PRId64 ", uid:%" PRId64 ", start ts:%" PRId64
×
2509
               ", end ts:%" PRId64,
2510
               TD_VID(pVnode), terrstr(), deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
2511
      }
2512
    }
2513
    tDecoderClear(&mr.coder);
1,483✔
2514
  }
2515
  metaReaderClear(&mr);
1,927✔
2516
  taosArrayDestroy(deleteReq.deleteReqs);
1,927✔
2517
  return 0;
1,928✔
2518
}
2519

2520
static int32_t vnodeProcessDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
63,028✔
2521
                                     SRpcMsg *pOriginalMsg) {
2522
  int32_t     code = 0;
63,028✔
2523
  SDecoder   *pCoder = &(SDecoder){0};
63,028✔
2524
  SDeleteRes *pRes = &(SDeleteRes){0};
63,028✔
2525

2526
  pRsp->msgType = TDMT_VND_DELETE_RSP;
63,028✔
2527
  pRsp->pCont = NULL;
63,028✔
2528
  pRsp->contLen = 0;
63,028✔
2529
  pRsp->code = TSDB_CODE_SUCCESS;
63,028✔
2530

2531
  pRes->uidList = taosArrayInit(0, sizeof(tb_uid_t));
63,028✔
2532
  if (pRes->uidList == NULL) {
63,028!
2533
    code = terrno;
×
2534
    goto _err;
×
2535
  }
2536

2537
  tDecoderInit(pCoder, pReq, len);
63,028✔
2538
  code = tDecodeDeleteRes(pCoder, pRes);
63,026✔
2539
  if (code) goto _err;
63,031!
2540

2541
  if (pRes->affectedRows > 0) {
63,031✔
2542
    for (int32_t iUid = 0; iUid < taosArrayGetSize(pRes->uidList); iUid++) {
120,593✔
2543
      uint64_t uid = *(uint64_t *)taosArrayGet(pRes->uidList, iUid);
62,443✔
2544
      code = tsdbDeleteTableData(pVnode->pTsdb, ver, pRes->suid, uid, pRes->skey, pRes->ekey);
62,444✔
2545
      if (code) goto _err;
62,445!
2546
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, pRes->ctimeMs);
62,445✔
2547
      if (code) goto _err;
62,443!
2548
    }
2549
  }
2550

2551
  code = tdProcessRSmaDelete(pVnode->pSma, ver, pRes, pReq, len);
63,029✔
2552

2553
  tDecoderClear(pCoder);
63,029✔
2554
  taosArrayDestroy(pRes->uidList);
63,030✔
2555

2556
  SVDeleteRsp rsp = {.affectedRows = pRes->affectedRows};
63,030✔
2557
  int32_t     ret = 0;
63,030✔
2558
  tEncodeSize(tEncodeSVDeleteRsp, &rsp, pRsp->contLen, ret);
63,030!
2559
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
63,029✔
2560
  SEncoder ec = {0};
63,030✔
2561
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
63,030✔
2562
  code = tEncodeSVDeleteRsp(&ec, &rsp);
63,031✔
2563
  if (code) goto _err;
63,031!
2564
  tEncoderClear(&ec);
63,031✔
2565
  return code;
63,032✔
2566

2567
_err:
×
2568
  /*
2569
  if(code == TSDB_CODE_SUCCESS){
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, "Success"};
2573
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2574
  }
2575
  else{
2576
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_DELETE, pVnode->monitor.strClusterId,
2577
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2578
                                        pOriginalMsg->info.conn.user, "Failed"};
2579
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2580
  }
2581
  */
2582

2583
  return code;
×
2584
}
2585
static int32_t vnodeProcessCreateIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
1,160✔
2586
  SVCreateStbReq req = {0};
1,160✔
2587
  SDecoder       dc = {0};
1,160✔
2588
  int32_t        code = 0;
1,160✔
2589

2590
  pRsp->msgType = TDMT_VND_CREATE_INDEX_RSP;
1,160✔
2591
  pRsp->code = TSDB_CODE_SUCCESS;
1,160✔
2592
  pRsp->pCont = NULL;
1,160✔
2593
  pRsp->contLen = 0;
1,160✔
2594

2595
  tDecoderInit(&dc, pReq, len);
1,160✔
2596
  // decode req
2597
  if (tDecodeSVCreateStbReq(&dc, &req) < 0) {
1,160!
2598
    tDecoderClear(&dc);
×
2599
    return terrno = TSDB_CODE_INVALID_MSG;
×
2600
  }
2601

2602
  code = metaAddIndexToSuperTable(pVnode->pMeta, ver, &req);
1,160✔
2603
  if (code) {
1,160!
2604
    pRsp->code = code;
×
2605
    goto _err;
×
2606
  }
2607
  tDecoderClear(&dc);
1,160✔
2608
  return 0;
1,160✔
2609

2610
_err:
×
2611
  tDecoderClear(&dc);
×
2612
  return code;
×
2613
}
2614
static int32_t vnodeProcessDropIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
2,662✔
2615
  SDropIndexReq req = {0};
2,662✔
2616
  int32_t       code = 0;
2,662✔
2617
  pRsp->msgType = TDMT_VND_DROP_INDEX_RSP;
2,662✔
2618
  pRsp->code = TSDB_CODE_SUCCESS;
2,662✔
2619
  pRsp->pCont = NULL;
2,662✔
2620
  pRsp->contLen = 0;
2,662✔
2621

2622
  if ((code = tDeserializeSDropIdxReq(pReq, len, &req))) {
2,662!
2623
    pRsp->code = code;
×
2624
    return code;
×
2625
  }
2626

2627
  code = metaDropIndexFromSuperTable(pVnode->pMeta, ver, &req);
2,662✔
2628
  if (code) {
2,662!
2629
    pRsp->code = code;
×
2630
    return code;
×
2631
  }
2632
  return TSDB_CODE_SUCCESS;
2,662✔
2633
}
2634

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

2637
static int32_t vnodeProcessCompactVnodeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
204✔
2638
  if (!pVnode->restored) {
204✔
2639
    vInfo("vgId:%d, ignore compact req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
174!
2640
    return 0;
174✔
2641
  }
2642
  return vnodeAsyncCompact(pVnode, ver, pReq, len, pRsp);
30✔
2643
}
2644

2645
static int32_t vnodeProcessConfigChangeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
2646
  if (syncCheckMember(pVnode->sync) != 0) {
×
2647
    vError("vgId:%d, failed to check member", TD_VID(pVnode));
×
2648
  }
2649

2650
  pRsp->msgType = TDMT_SYNC_CONFIG_CHANGE_RSP;
×
2651
  pRsp->code = TSDB_CODE_SUCCESS;
×
2652
  pRsp->pCont = NULL;
×
2653
  pRsp->contLen = 0;
×
2654

2655
  return 0;
×
2656
}
2657

2658
static int32_t vnodeCheckToken(SVnode *pVnode, char *member0Token, char *member1Token) {
42✔
2659
  SSyncState syncState = syncGetState(pVnode->sync);
42✔
2660
  if (syncState.state != TAOS_SYNC_STATE_LEADER) {
42✔
2661
    return terrno = TSDB_CODE_SYN_NOT_LEADER;
10✔
2662
  }
2663

2664
  char token[TSDB_ARB_TOKEN_SIZE] = {0};
32✔
2665
  if (vnodeGetArbToken(pVnode, token) != 0) {
32!
2666
    return terrno = TSDB_CODE_NOT_FOUND;
×
2667
  }
2668

2669
  if (strncmp(token, member0Token, TSDB_ARB_TOKEN_SIZE) != 0 &&
32✔
2670
      strncmp(token, member1Token, TSDB_ARB_TOKEN_SIZE) != 0) {
28✔
2671
    return terrno = TSDB_CODE_MND_ARB_TOKEN_MISMATCH;
22✔
2672
  }
2673

2674
  terrno = TSDB_CODE_SUCCESS;
10✔
2675
  return 0;
10✔
2676
}
2677

2678
static int32_t vnodeCheckSyncd(SVnode *pVnode, char *member0Token, char *member1Token) {
10✔
2679
  int32_t code = vnodeCheckToken(pVnode, member0Token, member1Token);
10✔
2680
  if (code != 0) {
10✔
2681
    return code;
5✔
2682
  }
2683

2684
  return syncCheckSynced(pVnode->sync);
5✔
2685
}
2686

2687
static int32_t vnodeProcessArbCheckSyncReq(SVnode *pVnode, void *pReq, int32_t len, SRpcMsg *pRsp) {
10✔
2688
  int32_t code = 0;
10✔
2689

2690
  SVArbCheckSyncReq syncReq = {0};
10✔
2691

2692
  code = tDeserializeSVArbCheckSyncReq(pReq, len, &syncReq);
10✔
2693
  if (code) {
10!
2694
    return terrno = code;
×
2695
  }
2696

2697
  pRsp->msgType = TDMT_VND_ARB_CHECK_SYNC_RSP;
10✔
2698
  pRsp->code = TSDB_CODE_SUCCESS;
10✔
2699
  pRsp->pCont = NULL;
10✔
2700
  pRsp->contLen = 0;
10✔
2701

2702
  SVArbCheckSyncRsp syncRsp = {0};
10✔
2703
  syncRsp.arbToken = syncReq.arbToken;
10✔
2704
  syncRsp.member0Token = syncReq.member0Token;
10✔
2705
  syncRsp.member1Token = syncReq.member1Token;
10✔
2706
  syncRsp.vgId = TD_VID(pVnode);
10✔
2707

2708
  if (vnodeCheckSyncd(pVnode, syncReq.member0Token, syncReq.member1Token) != 0) {
10✔
2709
    vError("vgId:%d, failed to check assigned log syncd", TD_VID(pVnode));
5!
2710
  }
2711
  syncRsp.errCode = terrno;
10✔
2712

2713
  if (vnodeUpdateArbTerm(pVnode, syncReq.arbTerm) != 0) {
10!
2714
    vError("vgId:%d, failed to update arb term", TD_VID(pVnode));
×
2715
    code = -1;
×
2716
    goto _OVER;
×
2717
  }
2718

2719
  int32_t contLen = tSerializeSVArbCheckSyncRsp(NULL, 0, &syncRsp);
10✔
2720
  if (contLen <= 0) {
10!
2721
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2722
    code = -1;
×
2723
    goto _OVER;
×
2724
  }
2725
  void *pHead = rpcMallocCont(contLen);
10✔
2726
  if (!pHead) {
10!
2727
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2728
    code = -1;
×
2729
    goto _OVER;
×
2730
  }
2731

2732
  if (tSerializeSVArbCheckSyncRsp(pHead, contLen, &syncRsp) <= 0) {
10!
2733
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2734
    rpcFreeCont(pHead);
×
2735
    code = -1;
×
2736
    goto _OVER;
×
2737
  }
2738

2739
  pRsp->pCont = pHead;
10✔
2740
  pRsp->contLen = contLen;
10✔
2741

2742
  terrno = TSDB_CODE_SUCCESS;
10✔
2743

2744
_OVER:
10✔
2745
  tFreeSVArbCheckSyncReq(&syncReq);
10✔
2746
  return code;
10✔
2747
}
2748

2749
#ifndef TD_ENTERPRISE
2750
int32_t vnodeAsyncCompact(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) { return 0; }
2751
int32_t tsdbAsyncCompact(STsdb *tsdb, const STimeWindow *tw, bool sync) { return 0; }
2752
#endif
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc