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

taosdata / TDengine / #4988

16 Mar 2026 12:26PM UTC coverage: 75.821% (+1.9%) from 73.883%
#4988

push

travis-ci

web-flow
feat: support secure delete option. (#34591)

274 of 464 new or added lines in 29 files covered. (59.05%)

4404 existing lines in 23 files now uncovered.

337108 of 444611 relevant lines covered (75.82%)

146708292.94 hits per line

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

69.59
/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 <stdint.h>
17
#include "audit.h"
18
#include "cos.h"
19
#include "libs/new-stream/stream.h"
20
#include "monitor.h"
21
#include "taoserror.h"
22
#include "tencode.h"
23
#include "tglobal.h"
24
#include "tmsg.h"
25
#include "tmsgcb.h"
26
#include "tstrbuild.h"
27
#include "tutil.h"
28
#include "vnd.h"
29
#include "vnode.h"
30
#include "vnodeInt.h"
31

32
extern taos_counter_t *tsInsertCounter;
33

34
extern int32_t vnodeProcessScanVnodeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
35
extern int32_t vnodeQueryScanProgress(SVnode *pVnode, SRpcMsg *pMsg);
36

37
static int32_t vnodeProcessCreateStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
38
static int32_t vnodeProcessAlterStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
39
static int32_t vnodeProcessDropStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp, SRpcMsg *pOriginRpc);
40
static int32_t vnodeProcessCreateTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
41
                                       SRpcMsg *pOriginRpc);
42
static int32_t vnodeProcessAlterTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
43
static int32_t vnodeProcessDropTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
44
                                     SRpcMsg *pOriginRpc);
45
static int32_t vnodeProcessSubmitReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
46
                                     SRpcMsg *pOriginalMsg);
47
static int32_t vnodeProcessAuditRecordReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pOriginalMsg);
48
static int32_t vnodeProcessAlterConfirmReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
49
static int32_t vnodeProcessAlterConfigReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
50
static int32_t vnodeProcessDropTtlTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
51
static int32_t vnodeProcessTrimReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
52
static int32_t vnodeProcessDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
53
                                     SRpcMsg *pOriginalMsg);
54
static int32_t vnodeProcessBatchDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
55
static int32_t vnodeProcessCreateIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
56
static int32_t vnodeProcessDropIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
57
static int32_t vnodeProcessCompactVnodeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
58
static int32_t vnodeProcessConfigChangeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
59
static int32_t vnodeProcessArbCheckSyncReq(SVnode *pVnode, void *pReq, int32_t len, SRpcMsg *pRsp);
60
static int32_t vnodeProcessDropTSmaCtbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
61
                                          SRpcMsg *pOriginRpc);
62
static int32_t vnodeProcessCreateRsmaReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
63
static int32_t vnodeProcessDropRsmaReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
64
static int32_t vnodeProcessAlterRsmaReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
65

66
static int32_t vnodeCheckState(SVnode *pVnode);
67
static int32_t vnodeCheckToken(SVnode *pVnode, char *member0Token, char *member1Token);
68
static int32_t vnodeCheckSyncd(SVnode *pVnode, char *member0Token, char *member1Token);
69
static int32_t vnodeProcessFetchTtlExpiredTbs(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
70

71
extern int32_t vnodeProcessKillCompactReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
72
extern int32_t vnodeQueryCompactProgress(SVnode *pVnode, SRpcMsg *pMsg);
73

74
extern int32_t vnodeProcessKillRetentionReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
75
extern int32_t vnodeQueryRetentionProgress(SVnode *pVnode, SRpcMsg *pMsg);
76

77
extern int32_t vnodeProcessKillScanReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
78

79
extern int32_t vnodeListSsMigrateFileSets(SVnode *pVnode, SRpcMsg *pMsg);
80
static int32_t vnodeProcessSsMigrateFileSetReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
81
extern int32_t vnodeQuerySsMigrateProgress(SVnode *pVnode, SRpcMsg *pMsg);
82
static int32_t vnodeProcessFollowerSsMigrateReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
83
static int32_t vnodeProcessKillSsMigrateReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
84

85
static int32_t vnodePreprocessCreateTableReq(SVnode *pVnode, SDecoder *pCoder, int64_t btime, int64_t *pUid) {
131,902,242✔
86
  int32_t code = 0;
131,902,242✔
87
  int32_t lino = 0;
131,902,242✔
88

89
  if (tStartDecode(pCoder) < 0) {
131,902,242✔
90
    code = TSDB_CODE_INVALID_MSG;
×
91
    TSDB_CHECK_CODE(code, lino, _exit);
×
92
  }
93

94
  // flags
95
  if (tDecodeI32v(pCoder, NULL) < 0) {
131,912,228✔
96
    code = TSDB_CODE_INVALID_MSG;
×
97
    TSDB_CHECK_CODE(code, lino, _exit);
×
98
  }
99

100
  // name
101
  char *name = NULL;
131,912,228✔
102
  if (tDecodeCStr(pCoder, &name) < 0) {
131,907,190✔
103
    code = TSDB_CODE_INVALID_MSG;
×
104
    TSDB_CHECK_CODE(code, lino, _exit);
×
105
  }
106

107
  // uid
108
  int64_t uid = metaGetTableEntryUidByName(pVnode->pMeta, name);
131,907,190✔
109
  if (uid == 0) {
131,885,862✔
110
    uid = tGenIdPI64();
131,123,334✔
111
  }
112
  taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), uid);
131,889,814✔
113

114
  // btime
115
  taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos + 8), btime);
131,884,642✔
116

117
  tEndDecode(pCoder);
131,879,462✔
118

119
_exit:
131,875,606✔
120
  if (code) {
131,874,364✔
121
    vError("vgId:%d %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
122
  } else {
123
    vTrace("vgId:%d %s done, table:%s uid generated:%" PRId64, TD_VID(pVnode), __func__, name, uid);
131,874,364✔
124
    if (pUid) *pUid = uid;
131,851,988✔
125
  }
126
  return code;
131,858,026✔
127
}
128
static int32_t vnodePreProcessCreateTableMsg(SVnode *pVnode, SRpcMsg *pMsg) {
88,991,976✔
129
  int32_t code = 0;
88,991,976✔
130
  int32_t lino = 0;
88,991,976✔
131

132
  int64_t  btime = taosGetTimestampMs();
88,991,306✔
133
  SDecoder dc = {0};
88,991,306✔
134
  int32_t  nReqs;
88,986,362✔
135

136
  tDecoderInit(&dc, (uint8_t *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead));
88,991,306✔
137
  if (tStartDecode(&dc) < 0) {
88,991,976✔
138
    code = TSDB_CODE_INVALID_MSG;
×
139
    return code;
×
140
  }
141

142
  if (tDecodeI32v(&dc, &nReqs) < 0) {
88,991,976✔
143
    code = TSDB_CODE_INVALID_MSG;
×
144
    TSDB_CHECK_CODE(code, lino, _exit);
×
145
  }
146
  for (int32_t iReq = 0; iReq < nReqs; iReq++) {
189,193,148✔
147
    code = vnodePreprocessCreateTableReq(pVnode, &dc, btime, NULL);
100,228,246✔
148
    TSDB_CHECK_CODE(code, lino, _exit);
100,201,172✔
149
  }
150

151
  tEndDecode(&dc);
88,964,902✔
152

153
_exit:
88,986,692✔
154
  tDecoderClear(&dc);
88,986,692✔
155
  if (code) {
88,987,706✔
156
    vError("vgId:%d, %s:%d failed to preprocess submit request since %s, msg type:%s", TD_VID(pVnode), __func__, lino,
×
157
           tstrerror(code), TMSG_INFO(pMsg->msgType));
158
  }
159
  return code;
88,986,524✔
160
}
161

162
static int32_t vnodePreProcessAlterTableMsg(SVnode *pVnode, SRpcMsg *pMsg) {
24,872,976✔
163
  int32_t code = TSDB_CODE_INVALID_MSG;
24,872,976✔
164
  int32_t lino = 0;
24,872,976✔
165

166
  if (pVnode->config.isAudit) {
24,872,976✔
167
    return TSDB_CODE_PAR_PERMISSION_DENIED;
×
168
  }
169

170
  SDecoder dc = {0};
24,872,976✔
171
  tDecoderInit(&dc, (uint8_t *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead));
24,872,976✔
172

173
  SVAlterTbReq vAlterTbReq = {0};
24,872,976✔
174
  int64_t      ctimeMs = taosGetTimestampMs();
24,872,976✔
175
  if (tDecodeSVAlterTbReqSetCtime(&dc, &vAlterTbReq, ctimeMs) < 0) {
24,872,976✔
176
    taosArrayDestroy(vAlterTbReq.pMultiTag);
×
177
    vAlterTbReq.pMultiTag = NULL;
×
178
    goto _exit;
×
179
  }
180
  taosArrayDestroy(vAlterTbReq.pMultiTag);
24,872,976✔
181
  vAlterTbReq.pMultiTag = NULL;
24,872,976✔
182

183
  code = 0;
24,872,976✔
184

185
_exit:
24,872,976✔
186
  tDecoderClear(&dc);
24,872,976✔
187
  if (code) {
24,872,976✔
188
    vError("vgId:%d %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
189
  } else {
190
    vTrace("vgId:%d %s done, table:%s ctimeMs generated:%" PRId64, TD_VID(pVnode), __func__, vAlterTbReq.tbName,
24,872,976✔
191
           ctimeMs);
192
  }
193
  return code;
24,872,976✔
194
}
195

196
static int32_t vnodePreProcessDropTtlMsg(SVnode *pVnode, SRpcMsg *pMsg) {
23,282,292✔
197
  int32_t code = TSDB_CODE_INVALID_MSG;
23,282,292✔
198
  int32_t lino = 0;
23,282,292✔
199

200
  SMsgHead *pContOld = pMsg->pCont;
23,282,292✔
201
  int32_t   reqLenOld = pMsg->contLen - sizeof(SMsgHead);
23,283,836✔
202

203
  SArray *tbUids = NULL;
23,284,182✔
204
  int64_t timestampMs = 0;
23,284,182✔
205

206
  SVDropTtlTableReq ttlReq = {0};
23,284,182✔
207
  if (tDeserializeSVDropTtlTableReq((char *)pContOld + sizeof(SMsgHead), reqLenOld, &ttlReq) != 0) {
23,281,362✔
208
    code = TSDB_CODE_INVALID_MSG;
×
209
    TSDB_CHECK_CODE(code, lino, _exit);
×
210
  }
211

212
  {  // find expired uids
213
    tbUids = taosArrayInit(8, sizeof(tb_uid_t));
23,280,410✔
214
    if (tbUids == NULL) {
23,272,830✔
215
      code = terrno;
×
216
      TSDB_CHECK_CODE(code, lino, _exit);
×
217
    }
218

219
    timestampMs = (int64_t)ttlReq.timestampSec * 1000;
23,272,830✔
220
    code = metaTtlFindExpired(pVnode->pMeta, timestampMs, tbUids, ttlReq.ttlDropMaxCount);
23,272,830✔
221
    if (code != 0) {
23,272,052✔
222
      code = TSDB_CODE_INVALID_MSG;
×
223
      TSDB_CHECK_CODE(code, lino, _exit);
×
224
    }
225

226
    ttlReq.nUids = taosArrayGetSize(tbUids);
23,272,052✔
227
    ttlReq.pTbUids = tbUids;
23,268,540✔
228
  }
229

230
  if (ttlReq.nUids == 0) {
23,268,540✔
231
    code = TSDB_CODE_MSG_PREPROCESSED;
23,234,466✔
232
    TSDB_CHECK_CODE(code, lino, _exit);
23,234,466✔
233
  }
234

235
  {  // prepare new content
236
    int32_t reqLenNew = tSerializeSVDropTtlTableReq(NULL, 0, &ttlReq);
35,442✔
237
    int32_t contLenNew = reqLenNew + sizeof(SMsgHead);
24,036✔
238

239
    SMsgHead *pContNew = rpcMallocCont(contLenNew);
24,036✔
240
    if (pContNew == NULL) {
24,036✔
241
      code = terrno;
×
242
      TSDB_CHECK_CODE(code, lino, _exit);
×
243
    }
244

245
    if (tSerializeSVDropTtlTableReq((char *)pContNew + sizeof(SMsgHead), reqLenNew, &ttlReq) != 0) {
24,036✔
246
      vError("vgId:%d %s:%d failed to serialize drop ttl request", TD_VID(pVnode), __func__, lino);
24,036✔
247
    }
248
    pContNew->contLen = htonl(reqLenNew);
24,036✔
249
    pContNew->vgId = pContOld->vgId;
24,036✔
250

251
    rpcFreeCont(pContOld);
24,036✔
252
    pMsg->pCont = pContNew;
24,036✔
253
    pMsg->contLen = contLenNew;
24,036✔
254
  }
255

256
  code = 0;
24,036✔
257

258
_exit:
23,257,134✔
259
  taosArrayDestroy(tbUids);
23,270,820✔
260

261
  if (code && code != TSDB_CODE_MSG_PREPROCESSED) {
23,238,904✔
262
    vError("vgId:%d, %s:%d failed to preprocess drop ttl request since %s, msg type:%s", TD_VID(pVnode), __func__, lino,
×
263
           tstrerror(code), TMSG_INFO(pMsg->msgType));
264
  } else {
265
    vTrace("vgId:%d, %s done, timestampSec:%d, nUids:%d", TD_VID(pVnode), __func__, ttlReq.timestampSec, ttlReq.nUids);
23,238,904✔
266
  }
267

268
  return code;
23,239,274✔
269
}
270

271
extern int64_t tsMaxKeyByPrecision[];
272
static int32_t vnodePreProcessSubmitTbData(SVnode *pVnode, SDecoder *pCoder, int64_t btimeMs, int64_t ctimeMs) {
1,104,679,844✔
273
  int32_t code = 0;
1,104,679,844✔
274
  int32_t lino = 0;
1,104,679,844✔
275

276
  if (tStartDecode(pCoder) < 0) {
1,104,679,844✔
277
    code = TSDB_CODE_INVALID_MSG;
×
278
    TSDB_CHECK_CODE(code, lino, _exit);
×
279
  }
280

281
  SSubmitTbData submitTbData;
1,104,684,684✔
282
  uint8_t       version;
283
  if (tDecodeI32v(pCoder, &submitTbData.flags) < 0) {
1,104,677,726✔
284
    code = TSDB_CODE_INVALID_MSG;
×
285
    TSDB_CHECK_CODE(code, lino, _exit);
×
286
  }
287
  version = (submitTbData.flags >> 8) & 0xff;
1,104,677,726✔
288
  submitTbData.flags = submitTbData.flags & 0xff;
1,104,677,726✔
289

290
  int64_t uid;
1,104,668,416✔
291
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
1,104,676,842✔
292
    code = vnodePreprocessCreateTableReq(pVnode, pCoder, btimeMs, &uid);
31,666,126✔
293
    TSDB_CHECK_CODE(code, lino, _exit);
31,651,396✔
294
  }
295

296
  // submit data
297
  if (tDecodeI64(pCoder, &submitTbData.suid) < 0) {
1,104,683,274✔
298
    code = TSDB_CODE_INVALID_MSG;
×
299
    TSDB_CHECK_CODE(code, lino, _exit);
×
300
  }
301

302
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
1,104,683,274✔
303
    taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), uid);
31,659,938✔
304
    pCoder->pos += sizeof(int64_t);
31,657,544✔
305
  } else {
306
    if (tDecodeI64(pCoder, &submitTbData.uid) < 0) {
1,073,011,632✔
307
      code = TSDB_CODE_INVALID_MSG;
×
308
      TSDB_CHECK_CODE(code, lino, _exit);
×
309
    }
310
  }
311

312
  if (tDecodeI32v(pCoder, &submitTbData.sver) < 0) {
1,104,669,902✔
313
    code = TSDB_CODE_INVALID_MSG;
×
314
    TSDB_CHECK_CODE(code, lino, _exit);
×
315
  }
316

317
  // scan and check
318
  TSKEY now = btimeMs;
1,104,669,902✔
319
  if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_MICRO) {
1,104,669,902✔
320
    now *= 1000;
5,204,964✔
321
  } else if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_NANO) {
1,099,470,678✔
322
    now *= 1000000;
3,058,858✔
323
  }
324

325
  int32_t keep = pVnode->config.tsdbCfg.keep2;
1,104,673,642✔
326

327
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * keep;
1,104,674,712✔
328
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
1,104,676,036✔
329
  if (submitTbData.flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
1,104,668,640✔
330
    uint64_t nColData;
13,730,936✔
331
    if (tDecodeU64v(pCoder, &nColData) < 0) {
13,733,944✔
332
      code = TSDB_CODE_INVALID_MSG;
×
333
      TSDB_CHECK_CODE(code, lino, _exit);
×
334
    }
335

336
    SColData colData = {0};
13,733,944✔
337
    code = tDecodeColData(version, pCoder, &colData, false);
13,733,944✔
338
    if (code) {
13,733,944✔
339
      code = TSDB_CODE_INVALID_MSG;
×
340
      TSDB_CHECK_CODE(code, lino, _exit);
×
341
    }
342

343
    if (colData.flag != HAS_VALUE) {
13,733,944✔
344
      code = TSDB_CODE_INVALID_MSG;
×
345
      TSDB_CHECK_CODE(code, lino, _exit);
×
346
    }
347

348
    for (uint32_t iRow = 0; iRow < colData.nVal; iRow++) {
2,147,483,647✔
349
      if (((TSKEY *)colData.pData)[iRow] < minKey || ((TSKEY *)colData.pData)[iRow] > maxKey) {
2,147,483,647✔
350
        code = TSDB_CODE_TDB_TIMESTAMP_OUT_OF_RANGE;
1,214✔
351
        TSDB_CHECK_CODE(code, lino, _exit);
1,214✔
352
      }
353
    }
354

355
    for (uint64_t i = 1; i < nColData; i++) {
42,064,534✔
356
      code = tDecodeColData(version, pCoder, &colData, true);
28,543,044✔
357
      if (code) {
28,543,044✔
358
        code = TSDB_CODE_INVALID_MSG;
×
359
        TSDB_CHECK_CODE(code, lino, _exit);
×
360
      }
361
    }
362
  } else {
363
    uint64_t nRow;
1,090,928,394✔
364
    if (tDecodeU64v(pCoder, &nRow) < 0) {
1,090,937,112✔
365
      code = TSDB_CODE_INVALID_MSG;
×
366
      TSDB_CHECK_CODE(code, lino, _exit);
×
367
    }
368

369
    for (uint32_t iRow = 0; iRow < nRow; ++iRow) {
2,147,483,647✔
370
      SRow *pRow = (SRow *)(pCoder->data + pCoder->pos);
2,147,483,647✔
371
      pCoder->pos += pRow->len;
2,147,483,647✔
372
#ifndef NO_UNALIGNED_ACCESS
373
      if (pRow->ts < minKey || pRow->ts > maxKey) {
2,147,483,647✔
374
#else
375
      TSKEY ts = taosGetInt64Aligned(&pRow->ts);
376
      if (ts < minKey || ts > maxKey) {
377
#endif
378
        code = TSDB_CODE_TDB_TIMESTAMP_OUT_OF_RANGE;
136,976✔
379
        TSDB_CHECK_CODE(code, lino, _exit);
136,976✔
380
      }
381

382
      // Check pRow->sver
383
      if (pRow->sver != submitTbData.sver) {
2,147,483,647✔
384
        code = TSDB_CODE_INVALID_DATA_FMT;
×
385
        TSDB_CHECK_CODE(code, lino, _exit);
×
386
      }
387
    }
388
  }
389

390
  if (!tDecodeIsEnd(pCoder)) {
1,104,539,010✔
391
    taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), ctimeMs);
1,104,532,892✔
392
    pCoder->pos += sizeof(int64_t);
1,104,520,090✔
393
  }
394

395
  tEndDecode(pCoder);
1,104,528,516✔
396

397
_exit:
1,104,681,524✔
398
  if (code) {
1,104,666,160✔
399
    vError("vgId:%d, %s:%d failed to vnodePreProcessSubmitTbData submit request since %s", TD_VID(pVnode), __func__,
138,190✔
400
           lino, tstrerror(code));
401
  }
402
  return code;
1,104,666,160✔
403
}
404
static int32_t vnodePreProcessSubmitMsg(SVnode *pVnode, SRpcMsg *pMsg) {
1,052,486,422✔
405
  int32_t code = 0;
1,052,486,422✔
406
  int32_t lino = 0;
1,052,486,422✔
407

408
  if (tsBypassFlag & TSDB_BYPASS_RA_RPC_RECV_SUBMIT) {
1,052,486,422✔
409
    return TSDB_CODE_MSG_PREPROCESSED;
162✔
410
  }
411

412
  SDecoder *pCoder = &(SDecoder){0};
1,052,486,260✔
413

414
  if (taosHton64(((SSubmitReq2Msg *)pMsg->pCont)->version) != 1) {
1,052,489,220✔
415
    code = TSDB_CODE_INVALID_MSG;
×
416
    TSDB_CHECK_CODE(code, lino, _exit);
×
417
  }
418

419
  tDecoderInit(pCoder, (uint8_t *)pMsg->pCont + sizeof(SSubmitReq2Msg), pMsg->contLen - sizeof(SSubmitReq2Msg));
1,052,487,740✔
420

421
  if (tStartDecode(pCoder) < 0) {
1,052,489,556✔
422
    code = TSDB_CODE_INVALID_MSG;
×
423
    TSDB_CHECK_CODE(code, lino, _exit);
×
424
  }
425

426
  uint64_t nSubmitTbData;
1,052,477,798✔
427
  if (tDecodeU64v(pCoder, &nSubmitTbData) < 0) {
1,052,487,008✔
428
    code = TSDB_CODE_INVALID_MSG;
×
429
    TSDB_CHECK_CODE(code, lino, _exit);
×
430
  }
431

432
  int64_t btimeMs = taosGetTimestampMs();
1,052,491,036✔
433
  int64_t ctimeMs = btimeMs;
1,052,491,036✔
434
  for (uint64_t i = 0; i < nSubmitTbData; i++) {
2,147,483,647✔
435
    code = vnodePreProcessSubmitTbData(pVnode, pCoder, btimeMs, ctimeMs);
1,104,679,144✔
436
    TSDB_CHECK_CODE(code, lino, _exit);
1,104,667,416✔
437
  }
438

439
  tEndDecode(pCoder);
1,052,341,118✔
440

441
_exit:
1,052,477,644✔
442
  tDecoderClear(pCoder);
1,052,490,188✔
443
  if (code) {
1,052,481,966✔
444
    vError("vgId:%d, %s:%d failed to preprocess submit request since %s, msg type:%s", TD_VID(pVnode), __func__, lino,
138,190✔
445
           tstrerror(code), TMSG_INFO(pMsg->msgType));
446
  }
447
  return code;
1,052,472,664✔
448
}
449

450
static int32_t vnodePreProcessDeleteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
3,574,958✔
451
  int32_t code = 0;
3,574,958✔
452

453
  int32_t    size;
454
  int32_t    ret;
455
  uint8_t   *pCont;
456
  SEncoder  *pCoder = &(SEncoder){0};
3,574,958✔
457
  SDeleteRes res = {0};
3,574,958✔
458

459
  SReadHandle handle = {0};
3,574,958✔
460
  handle.vnode = pVnode;
3,574,958✔
461
  handle.pMsgCb = &pVnode->msgCb;
3,574,958✔
462
  handle.skipRollup = 1;
3,574,958✔
463
  initStorageAPI(&handle.api);
3,574,958✔
464

465
  code = qWorkerProcessDeleteMsg(&handle, pVnode->pQuery, pMsg, &res);
3,574,958✔
466
  if (code) goto _exit;
3,574,958✔
467

468
  res.ctimeMs = taosGetTimestampMs();
3,574,958✔
469
  // malloc and encode
470
  tEncodeSize(tEncodeDeleteRes, &res, size, ret);
3,574,958✔
471
  pCont = rpcMallocCont(size + sizeof(SMsgHead));
3,574,958✔
472

473
  ((SMsgHead *)pCont)->contLen = size + sizeof(SMsgHead);
3,572,680✔
474
  ((SMsgHead *)pCont)->vgId = TD_VID(pVnode);
3,572,680✔
475

476
  tEncoderInit(pCoder, pCont + sizeof(SMsgHead), size);
3,573,822✔
477
  if (tEncodeDeleteRes(pCoder, &res) != 0) {
3,572,680✔
478
    vError("vgId:%d %s failed to encode delete response", TD_VID(pVnode), __func__);
×
479
  }
480
  tEncoderClear(pCoder);
3,573,816✔
481

482
  rpcFreeCont(pMsg->pCont);
3,574,958✔
483
  pMsg->pCont = pCont;
3,574,958✔
484
  pMsg->contLen = size + sizeof(SMsgHead);
3,574,958✔
485

486
  taosArrayDestroy(res.uidList);
3,574,958✔
487

488
_exit:
3,572,680✔
489
  return code;
3,574,958✔
490
}
491

492
static int32_t vnodePreProcessBatchDeleteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
×
493
  int32_t code = 0;
×
494
  int32_t lino = 0;
×
495

496
  int64_t         ctimeMs = taosGetTimestampMs();
×
497
  SBatchDeleteReq pReq = {0};
×
498
  SDecoder       *pCoder = &(SDecoder){0};
×
499

500
  tDecoderInit(pCoder, (uint8_t *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead));
×
501

502
  if (tDecodeSBatchDeleteReqSetCtime(pCoder, &pReq, ctimeMs) < 0) {
×
503
    code = TSDB_CODE_INVALID_MSG;
×
504
  }
505

506
  tDecoderClear(pCoder);
×
507
  taosArrayDestroy(pReq.deleteReqs);
×
508

509
  if (code) {
×
510
    vError("vgId:%d %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
511
  } else {
512
    vTrace("vgId:%d %s done, ctimeMs generated:%" PRId64, TD_VID(pVnode), __func__, ctimeMs);
×
513
  }
514
  return code;
×
515
}
516

517
static int32_t vnodePreProcessArbCheckSyncMsg(SVnode *pVnode, SRpcMsg *pMsg) {
101,782✔
518
  int32_t ret = 0;
101,782✔
519
  if ((ret = vnodeCheckState(pVnode)) != 0) {
101,782✔
520
    vDebug("vgId:%d, failed to preprocess vnode-arb-check-sync request since %s", TD_VID(pVnode), tstrerror(ret));
31,000✔
521
    return 0;
31,000✔
522
  }
523

524
  SVArbCheckSyncReq syncReq = {0};
70,782✔
525

526
  if (tDeserializeSVArbCheckSyncReq((char *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead),
70,782✔
527
                                    &syncReq) != 0) {
528
    return TSDB_CODE_INVALID_MSG;
×
529
  }
530

531
  ret = vnodeCheckToken(pVnode, syncReq.member0Token, syncReq.member1Token);
70,782✔
532
  if (ret != 0) {
70,782✔
533
    vError("vgId:%d, failed to preprocess vnode-arb-check-sync request since %s", TD_VID(pVnode), tstrerror(ret));
67,532✔
534
  }
535

536
  int32_t code = terrno;
70,782✔
537
  tFreeSVArbCheckSyncReq(&syncReq);
70,782✔
538

539
  return code;
70,782✔
540
}
541

542
int32_t vnodePreProcessDropTbMsg(SVnode *pVnode, SRpcMsg *pMsg) {
2,965,508✔
543
  int32_t          code = TSDB_CODE_SUCCESS;
2,965,508✔
544
  int32_t          lino = 0;
2,965,508✔
545
  int32_t          size = 0;
2,965,508✔
546
  SDecoder         dc = {0};
2,965,508✔
547
  SEncoder         ec = {0};
2,965,508✔
548
  SVDropTbBatchReq receivedBatchReqs = {0};
2,965,508✔
549
  SVDropTbBatchReq sentBatchReqs = {0};
2,965,508✔
550

551
  if (pVnode->config.isAudit) {
2,965,508✔
552
    return TSDB_CODE_PAR_PERMISSION_DENIED;
×
553
  }
554

555
  tDecoderInit(&dc, POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), pMsg->contLen - sizeof(SMsgHead));
2,965,508✔
556

557
  code = tDecodeSVDropTbBatchReq(&dc, &receivedBatchReqs);
2,965,508✔
558
  if (code < 0) {
2,965,508✔
559
    terrno = code;
×
560
    TSDB_CHECK_CODE(code, lino, _exit);
×
561
  }
562
  sentBatchReqs.pArray = taosArrayInit(receivedBatchReqs.nReqs, sizeof(SVDropTbReq));
2,965,508✔
563
  if (!sentBatchReqs.pArray) {
2,965,508✔
564
    code = terrno;
×
565
    goto _exit;
×
566
  }
567

568
  for (int32_t i = 0; i < receivedBatchReqs.nReqs; ++i) {
6,039,396✔
569
    SVDropTbReq *pReq = receivedBatchReqs.pReqs + i;
3,073,888✔
570
    tb_uid_t     uid = metaGetTableEntryUidByName(pVnode->pMeta, pReq->name);
3,073,888✔
571
    if (uid == 0) {
3,073,888✔
572
      vWarn("vgId:%d, preprocess drop ctb: %s not found", TD_VID(pVnode), pReq->name);
×
573
      continue;
×
574
    }
575
    pReq->uid = uid;
3,073,888✔
576
    vDebug("vgId:%d %s for: %s, uid: %" PRId64, TD_VID(pVnode), __func__, pReq->name, pReq->uid);
3,073,888✔
577
    if (taosArrayPush(sentBatchReqs.pArray, pReq) == NULL) {
6,147,776✔
578
      code = terrno;
×
579
      goto _exit;
×
580
    }
581
  }
582
  sentBatchReqs.nReqs = sentBatchReqs.pArray->size;
2,965,508✔
583

584
  tEncodeSize(tEncodeSVDropTbBatchReq, &sentBatchReqs, size, code);
2,965,508✔
585
  tEncoderInit(&ec, POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), size);
2,965,508✔
586
  code = tEncodeSVDropTbBatchReq(&ec, &sentBatchReqs);
2,965,508✔
587
  tEncoderClear(&ec);
2,965,508✔
588
  if (code != TSDB_CODE_SUCCESS) {
2,965,508✔
589
    vError("vgId:%d %s failed to encode drop tb batch req: %s", TD_VID(pVnode), __func__, tstrerror(code));
×
590
    TSDB_CHECK_CODE(code, lino, _exit);
×
591
  }
592

593
_exit:
2,965,508✔
594
  tDecoderClear(&dc);
2,965,508✔
595
  if (sentBatchReqs.pArray) {
2,965,508✔
596
    taosArrayDestroy(sentBatchReqs.pArray);
2,965,508✔
597
  }
598
  return code;
2,965,508✔
599
}
600

601
int32_t vnodePreProcessSsMigrateFileSetReq(SVnode *pVnode, SRpcMsg *pMsg) {
7,510✔
602
  int32_t              code = TSDB_CODE_SUCCESS, lino = 0;
7,510✔
603
  SSsMigrateFileSetReq req = {0};
7,510✔
604

605
  code = tDeserializeSSsMigrateFileSetReq(POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)),
7,510✔
606
                                          pMsg->contLen - sizeof(SMsgHead), &req);
7,510✔
607
  if (code < 0) {
7,510✔
608
    terrno = code;
×
609
    TSDB_CHECK_CODE(code, lino, _exit);
×
610
  }
611

612
  req.nodeId = vnodeNodeId(pVnode);
7,510✔
613
  TAOS_UNUSED(tSerializeSSsMigrateFileSetReq(POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)),
7,510✔
614
                                             pMsg->contLen - sizeof(SMsgHead), &req));
615

616
_exit:
7,510✔
617
  return code;
7,510✔
618
}
619

620
int32_t vnodePreProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
1,239,404,474✔
621
  int32_t code = 0;
1,239,404,474✔
622

623
  if (pVnode->mounted) {
1,239,404,474✔
624
    vError("vgId:%d, mountVgId:%d, skip write msg:%s", TD_VID(pVnode), pVnode->config.mountVgId,
3,040✔
625
           TMSG_INFO(pMsg->msgType));
626
    return TSDB_CODE_OPS_NOT_SUPPORT;
3,040✔
627
  }
628

629
  switch (pMsg->msgType) {
1,239,417,374✔
630
    case TDMT_VND_CREATE_TABLE: {
88,991,976✔
631
      code = vnodePreProcessCreateTableMsg(pVnode, pMsg);
88,991,976✔
632
    } break;
88,988,142✔
633
    case TDMT_VND_ALTER_TABLE: {
24,872,976✔
634
      code = vnodePreProcessAlterTableMsg(pVnode, pMsg);
24,872,976✔
635
    } break;
24,872,976✔
636
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
23,285,654✔
637
    case TDMT_VND_DROP_TTL_TABLE: {
638
      code = vnodePreProcessDropTtlMsg(pVnode, pMsg);
23,285,654✔
639
    } break;
23,265,226✔
640
    case TDMT_VND_SUBMIT: {
1,052,489,718✔
641
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.preprocess_time, METRIC_LEVEL_LOW,
1,052,489,718✔
642
                           { code = vnodePreProcessSubmitMsg(pVnode, pMsg); });
643
    } break;
1,052,475,274✔
644
    case TDMT_VND_DELETE: {
3,574,958✔
645
      code = vnodePreProcessDeleteMsg(pVnode, pMsg);
3,574,958✔
646
    } break;
3,573,816✔
647
    case TDMT_VND_BATCH_DEL: {
×
648
      code = vnodePreProcessBatchDeleteMsg(pVnode, pMsg);
×
649
    } break;
×
650
    case TDMT_VND_ARB_CHECK_SYNC: {
101,782✔
651
      code = vnodePreProcessArbCheckSyncMsg(pVnode, pMsg);
101,782✔
652
    } break;
101,782✔
653
    case TDMT_VND_DROP_TABLE: {
2,965,508✔
654
      code = vnodePreProcessDropTbMsg(pVnode, pMsg);
2,965,508✔
655
    } break;
2,965,508✔
656
#ifdef TD_ENTERPRISE
657
    case TDMT_VND_SSMIGRATE_FILESET: {
7,510✔
658
      code = vnodePreProcessSsMigrateFileSetReq(pVnode, pMsg);
7,510✔
659
    } break;
7,510✔
660
#endif
661
    case TDMT_VND_AUDIT_RECORD: {
544✔
662
      vTrace("vgId:%d, pre process TDMT_VND_AUDIT_RECORD", TD_VID(pVnode));
544✔
663
    } break;
600✔
664
    default:
43,125,176✔
665
      break;
43,125,176✔
666
  }
667

668
  if (code && code != TSDB_CODE_MSG_PREPROCESSED) {
1,239,376,010✔
669
    vError("vgId:%d, failed to preprocess write request since %s, msg type:%s", TD_VID(pVnode), tstrerror(code),
205,722✔
670
           TMSG_INFO(pMsg->msgType));
671
  }
672
  return code;
1,239,374,770✔
673
}
674

675
static int32_t inline vnodeSubmitSubRowBlobData(SVnode *pVnode, SSubmitTbData *pSubmitTbData) {
63,996✔
676
  int32_t code = 0;
63,996✔
677
  int32_t lino = 0;
63,996✔
678

679
  int64_t   st = taosGetTimestampUs();
63,996✔
680
  SBlobSet *pBlobSet = pSubmitTbData->pBlobSet;
63,996✔
681
  int32_t   sz = taosArrayGetSize(pBlobSet->pSeqTable);
63,996✔
682

683
  SBseBatch *pBatch = NULL;
63,996✔
684

685
  code = bseBatchInit(pVnode->pBse, &pBatch, sz);
63,996✔
686
  TSDB_CHECK_CODE(code, lino, _exit);
63,996✔
687

688
  SRow  **pRow = (SRow **)TARRAY_DATA(pSubmitTbData->aRowP);
63,996✔
689
  int32_t rowIdx = -1;
63,996✔
690
  for (int32_t i = 0; i < sz; i++) {
51,256,794✔
691
    int64_t     seq = 0;
51,192,798✔
692
    SBlobValue *p = taosArrayGet(pBlobSet->pSeqTable, i);
51,192,798✔
693
    if (p->type == TSDB_DATA_BLOB_EMPTY_VALUE || p->type == TSDB_DATA_BLOB_NULL_VALUE) {
51,192,798✔
694
      // skip empty or null blob
695
      continue;
5,082,346✔
696
    }
697

698
    code = bseBatchPut(pBatch, &seq, pBlobSet->data + p->offset, p->len);
46,110,452✔
699
    TSDB_CHECK_CODE(code, lino, _exit);
46,110,452✔
700

701
    SRow *row = taosArrayGetP(pSubmitTbData->aRowP, i);
46,110,452✔
702
    if (row == NULL) {
46,110,452✔
703
      int32_t tlen = taosArrayGetSize(pBlobSet->pSeqTable);
×
704
      uTrace("blob invalid row index:%d, sz:%d, pBlobSet size:%d", rowIdx, sz, tlen);
×
705
      break;
×
706
    }
707

708
    if (tPutU64(row->data + p->dataOffset, seq) < 0) {
92,220,904✔
709
      code = TSDB_CODE_INVALID_MSG;
×
710
      TSDB_CHECK_CODE(code, lino, _exit);
×
711
    }
712
  }
713

714
  code = bseCommitBatch(pVnode->pBse, pBatch);
63,996✔
715
  TSDB_CHECK_CODE(code, lino, _exit);
63,996✔
716

717
  int64_t cost = taosGetTimestampUs() - st;
63,996✔
718
  if (cost >= 500) {
63,996✔
719
    vDebug("vgId:%d, %s, cost:%" PRId64 "us, rows:%d, size:%" PRId64 "", TD_VID(pVnode), __func__, cost, sz,
20,790✔
720
           pBlobSet->len);
721
  }
722
_exit:
63,996✔
723
  if (code != 0) {
63,996✔
724
    vError("vgId:%d %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
725
  }
726
  return code;
63,996✔
727
}
728

729
static int32_t inline vnodeSubmitSubColBlobData(SVnode *pVnode, SSubmitTbData *pSubmitTbData) {
304✔
730
  int32_t code = 0;
304✔
731
  int32_t lino = 0;
304✔
732

733
  int32_t   blobColIdx = 0;
304✔
734
  SColData *pBlobCol = NULL;
304✔
735
  int64_t   st = taosGetTimestampUs();
304✔
736
  SBlobSet *pBlobSet = pSubmitTbData->pBlobSet;
304✔
737
  int32_t   sz = taosArrayGetSize(pBlobSet->pSeqTable);
304✔
738

739
  SBseBatch *pBatch = NULL;
304✔
740

741
  code = bseBatchInit(pVnode->pBse, &pBatch, sz);
304✔
742
  TSDB_CHECK_CODE(code, lino, _exit);
304✔
743

744
  SColData *p = (SColData *)TARRAY_DATA(pSubmitTbData->aCol);
304✔
745
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitTbData->aCol); i++) {
4,028✔
746
    if (IS_STR_DATA_BLOB(p[i].type)) {
4,028✔
747
      pBlobCol = &p[i];
304✔
748
      break;
304✔
749
    }
750
  }
751
  if (pBlobCol == NULL) {
304✔
752
    vError("vgId:%d %s failed to find blob column in submit data", TD_VID(pVnode), __func__);
×
753
    code = TSDB_CODE_INVALID_MSG;
×
754
    goto _exit;
×
755
  }
756

757
  int32_t offset = 0;
304✔
758
  // int32_t   rowIdx = -1;
759
  for (int32_t i = 0; i < sz; i++) {
646✔
760
    int64_t     seq = 0;
342✔
761
    SBlobValue *p = taosArrayGet(pBlobSet->pSeqTable, i);
342✔
762
    if (p->type == TSDB_DATA_BLOB_EMPTY_VALUE || p->type == TSDB_DATA_BLOB_NULL_VALUE) {
342✔
763
      // skip empty or null blob
764
      continue;
190✔
765
    }
766
    code = bseBatchPut(pBatch, &seq, pBlobSet->data + p->offset, p->len);
152✔
767
    TSDB_CHECK_CODE(code, lino, _exit);
152✔
768

769
    memcpy(pBlobCol->pData + offset, (void *)&seq, BSE_SEQUECE_SIZE);
152✔
770
    offset += BSE_SEQUECE_SIZE;
152✔
771
  }
772

773
  code = bseCommitBatch(pVnode->pBse, pBatch);
304✔
774
  TSDB_CHECK_CODE(code, lino, _exit);
304✔
775

776
  int64_t cost = taosGetTimestampUs() - st;
304✔
777
  if (cost >= 500) {
304✔
778
    vDebug("vgId:%d, %s, cost:%" PRId64 "us, rows:%d, size:%" PRId64 "", TD_VID(pVnode), __func__, cost, sz,
228✔
779
           pBlobSet->len);
780
  }
781
_exit:
304✔
782
  if (code != 0) {
304✔
783
    vError("vgId:%d %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
784
  }
785
  return code;
304✔
786
}
787
static int32_t inline vnodeSubmitBlobData(SVnode *pVnode, SSubmitTbData *pData) {
64,300✔
788
  int32_t code = 0;
64,300✔
789
  if (pData->flags & SUBMIT_REQ_WITH_BLOB) {
64,300✔
790
    if (pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
64,300✔
791
      code = vnodeSubmitSubColBlobData(pVnode, pData);
304✔
792
    } else {
793
      code = vnodeSubmitSubRowBlobData(pVnode, pData);
63,996✔
794
    }
795
  }
796

797
  return code;
64,300✔
798
}
799

800
int32_t vnodeProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg, int64_t ver, SRpcMsg *pRsp) {
1,516,485,246✔
801
  int32_t code = 0;
1,516,485,246✔
802
  int32_t lino = 0;
1,516,485,246✔
803
  void   *ptr = NULL;
1,516,485,246✔
804
  void   *pReq;
805
  int32_t len;
806

807
  (void)taosThreadMutexLock(&pVnode->mutex);
1,516,485,246✔
808
  if (pVnode->disableWrite) {
1,516,615,538✔
809
    (void)taosThreadMutexUnlock(&pVnode->mutex);
×
810
    vError("vgId:%d write is disabled for snapshot, version:%" PRId64, TD_VID(pVnode), ver);
×
811
    return TSDB_CODE_VND_WRITE_DISABLED;
×
812
  }
813
  (void)taosThreadMutexUnlock(&pVnode->mutex);
1,516,459,194✔
814

815
  if (ver <= pVnode->state.applied) {
1,516,637,086✔
816
    vError("vgId:%d, duplicate write request. ver: %" PRId64 ", applied: %" PRId64, TD_VID(pVnode), ver,
×
817
           pVnode->state.applied);
818
    return terrno = TSDB_CODE_VND_DUP_REQUEST;
×
819
  }
820

821
  vGDebug(&pMsg->info.traceId,
1,516,504,630✔
822
          "vgId:%d, index:%" PRId64 ", process write request:%s, applied:%" PRId64 ", state.applyTerm:%" PRId64
823
          ", conn.applyTerm:%" PRId64 ", contLen:%d",
824
          TD_VID(pVnode), ver, TMSG_INFO(pMsg->msgType), pVnode->state.applied, pVnode->state.applyTerm,
825
          pMsg->info.conn.applyTerm, pMsg->contLen);
826

827
  if (!(pVnode->state.applyTerm <= pMsg->info.conn.applyTerm)) {
1,516,631,718✔
828
    vError("vgId:%d, applyTerm mismatch, expected: %" PRId64 ", received: %" PRId64, TD_VID(pVnode),
×
829
           pVnode->state.applyTerm, pMsg->info.conn.applyTerm);
830
    return terrno = TSDB_CODE_INTERNAL_ERROR;
×
831
  }
832

833
  if (!(pVnode->state.applied + 1 == ver)) {
1,516,648,318✔
834
    vError("vgId:%d, mountVgId:%d, ver mismatch, expected: %" PRId64 ", received: %" PRId64, TD_VID(pVnode),
×
835
           pVnode->config.mountVgId, pVnode->state.applied + 1, ver);
836
    return terrno = TSDB_CODE_INTERNAL_ERROR;
×
837
  }
838

839
  atomic_store_64(&pVnode->state.applied, ver);
1,516,649,142✔
840
  atomic_store_64(&pVnode->state.applyTerm, pMsg->info.conn.applyTerm);
1,516,655,940✔
841

842
  if (!syncUtilUserCommit(pMsg->msgType)) goto _exit;
1,516,657,038✔
843

844
  // skip header
845
  pReq = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
1,494,015,396✔
846
  len = pMsg->contLen - sizeof(SMsgHead);
1,494,016,720✔
847
  bool needCommit = false;
1,494,014,374✔
848
  bool forceTrimWal = false;
1,494,014,374✔
849

850
  switch (pMsg->msgType) {
1,494,014,374✔
851
    /* META */
852
    case TDMT_VND_CREATE_STB:
11,210,956✔
853
      code = vnodeProcessCreateStbReq(pVnode, ver, pReq, len, pRsp);
11,210,956✔
854
      TSDB_CHECK_CODE(code, lino, _err);
11,210,956✔
855
      break;
11,204,740✔
856
    case TDMT_VND_ALTER_STB:
15,536,340✔
857
      code = vnodeProcessAlterStbReq(pVnode, ver, pReq, len, pRsp);
15,536,340✔
858
      TSDB_CHECK_CODE(code, lino, _err);
15,537,440✔
859
      break;
15,537,440✔
860
    case TDMT_VND_DROP_STB:
1,894,180✔
861
      code = vnodeProcessDropStbReq(pVnode, ver, pReq, len, pRsp, pMsg);
1,894,180✔
862
      TSDB_CHECK_CODE(code, lino, _err);
1,894,180✔
863
      break;
1,894,180✔
864
    case TDMT_VND_CREATE_TABLE:
94,958,284✔
865
      code = vnodeProcessCreateTbReq(pVnode, ver, pReq, len, pRsp, pMsg);
94,958,284✔
866
      TSDB_CHECK_CODE(code, lino, _err);
94,954,432✔
867
      break;
94,954,432✔
868
    case TDMT_VND_ALTER_TABLE:
24,874,264✔
869
      code = vnodeProcessAlterTbReq(pVnode, ver, pReq, len, pRsp);
24,874,264✔
870
      TSDB_CHECK_CODE(code, lino, _err);
24,874,264✔
871
      break;
24,874,264✔
872
    case TDMT_VND_DROP_TABLE:
2,966,218✔
873
    case TDMT_VND_SNODE_DROP_TABLE:
874
      code = vnodeProcessDropTbReq(pVnode, ver, pReq, len, pRsp, pMsg);
2,966,218✔
875
      TSDB_CHECK_CODE(code, lino, _err);
2,966,218✔
876
      break;
2,966,218✔
877
    case TDMT_VND_CREATE_RSMA:
79,844✔
878
      code = vnodeProcessCreateRsmaReq(pVnode, ver, pReq, len, pRsp);
79,844✔
879
      TSDB_CHECK_CODE(code, lino, _err);
79,844✔
880
      break;
79,844✔
881
    case TDMT_VND_DROP_RSMA:
40,212✔
882
      code = vnodeProcessDropRsmaReq(pVnode, ver, pReq, len, pRsp);
40,212✔
883
      TSDB_CHECK_CODE(code, lino, _err);
40,212✔
884
      break;
40,212✔
885
    case TDMT_VND_ALTER_RSMA:
24,612✔
886
      code = vnodeProcessAlterRsmaReq(pVnode, ver, pReq, len, pRsp);
24,612✔
887
      TSDB_CHECK_CODE(code, lino, _err);
24,612✔
888
      break;
24,612✔
889
    case TDMT_VND_DROP_TTL_TABLE:
×
890
      code = vnodeProcessDropTtlTbReq(pVnode, ver, pReq, len, pRsp);
×
891
      TSDB_CHECK_CODE(code, lino, _err);
×
892
      break;
×
893
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
24,036✔
894
      code = vnodeProcessFetchTtlExpiredTbs(pVnode, ver, pReq, len, pRsp);
24,036✔
895
      TSDB_CHECK_CODE(code, lino, _err);
24,036✔
896
      break;
24,036✔
897
    case TDMT_VND_TRIM:
162,534✔
898
      code = vnodeProcessTrimReq(pVnode, ver, pReq, len, pRsp);
162,534✔
899
      TSDB_CHECK_CODE(code, lino, _err);
162,534✔
900
      break;
162,534✔
901
      case TDMT_VND_TRIM_WAL:
13,680✔
902
        needCommit = true;
13,680✔
903
        forceTrimWal = true;
13,680✔
904
        break;
13,680✔
905
#ifdef TD_ENTERPRISE
906
    case TDMT_VND_SSMIGRATE_FILESET:
7,510✔
907
      if (vnodeProcessSsMigrateFileSetReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
7,510✔
908
      break;
7,510✔
909
    case TDMT_VND_FOLLOWER_SSMIGRATE:
13,518✔
910
      if (vnodeProcessFollowerSsMigrateReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
13,518✔
911
      break;
13,518✔
912
    case TDMT_VND_KILL_SSMIGRATE:
×
913
      if (vnodeProcessKillSsMigrateReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
914
      break;
×
915
#endif
916

917
    /* TSDB */
918
    case TDMT_VND_SUBMIT: {
1,303,457,104✔
919
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.apply_time, METRIC_LEVEL_LOW, {
1,303,457,104✔
920
        if (vnodeProcessSubmitReq(pVnode, ver, pMsg->pCont, pMsg->contLen, pRsp, pMsg) < 0) goto _err;
921
      });
922
      METRICS_UPDATE(pVnode->writeMetrics.apply_bytes, METRIC_LEVEL_LOW, (int64_t)pMsg->contLen);
1,303,365,228✔
923
      break;
1,303,356,304✔
924
    }
925
    case TDMT_VND_DELETE:
4,379,850✔
926
      code = vnodeProcessDeleteReq(pVnode, ver, pReq, len, pRsp, pMsg);
4,379,850✔
927
      TSDB_CHECK_CODE(code, lino, _err);
4,379,850✔
928
      break;
4,379,850✔
929
    case TDMT_VND_BATCH_DEL:
×
930
      code = vnodeProcessBatchDeleteReq(pVnode, ver, pReq, len, pRsp);
×
931
      TSDB_CHECK_CODE(code, lino, _err);
×
932
      break;
×
933
      /* TQ */
934
#if defined(USE_TQ) || defined(USE_STREAM)
935
    case TDMT_VND_TMQ_SUBSCRIBE:
2,665,280✔
936
      code = tqProcessSubscribeReq(pVnode->pTq, ver, pReq, len);
2,665,280✔
937
      TSDB_CHECK_CODE(code, lino, _err);
2,658,526✔
938
      break;
2,658,526✔
939
    case TDMT_VND_TMQ_DELETE_SUB:
601,624✔
940
      code = tqProcessDeleteSubReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen);
601,624✔
941
      TSDB_CHECK_CODE(code, lino, _err);
600,270✔
942
      break;
600,270✔
943
    case TDMT_VND_TMQ_COMMIT_OFFSET:
10,879,998✔
944
      code = tqProcessOffsetCommitReq(pVnode->pTq, ver, pReq, len);
10,879,998✔
945
      TSDB_CHECK_CODE(code, lino, _err);
10,879,404✔
946
      break;
10,879,404✔
947
#endif
948
    case TDMT_VND_ALTER_CONFIRM:
8,979,308✔
949
      needCommit = pVnode->config.hashChange;
8,979,308✔
950
      code = vnodeProcessAlterConfirmReq(pVnode, ver, pReq, len, pRsp);
8,979,308✔
951
      TSDB_CHECK_CODE(code, lino, _err);
8,979,308✔
952
      break;
8,979,308✔
953
    case TDMT_VND_ALTER_CONFIG:
664,962✔
954
      vnodeProcessAlterConfigReq(pVnode, ver, pReq, len, pRsp);
664,962✔
955
      break;
664,962✔
956
    case TDMT_VND_COMMIT:
10,357,918✔
957
      needCommit = true;
10,357,918✔
958
      break;
10,357,918✔
959
    case TDMT_VND_CREATE_INDEX:
37,334✔
960
      vnodeProcessCreateIndexReq(pVnode, ver, pReq, len, pRsp);
37,334✔
961
      break;
37,334✔
962
    case TDMT_VND_DROP_INDEX:
30,396✔
963
      vnodeProcessDropIndexReq(pVnode, ver, pReq, len, pRsp);
30,396✔
964
      break;
30,396✔
965
    case TDMT_VND_COMPACT:
142,454✔
966
      vnodeProcessCompactVnodeReq(pVnode, ver, pReq, len, pRsp);
142,454✔
967
      goto _exit;
142,454✔
968
    case TDMT_VND_SCAN:
1,190✔
969
      vnodeProcessScanVnodeReq(pVnode, ver, pReq, len, pRsp);
1,190✔
970
      goto _exit;
1,190✔
971
    case TDMT_SYNC_CONFIG_CHANGE:
×
972
      vnodeProcessConfigChangeReq(pVnode, ver, pReq, len, pRsp);
×
973
      break;
×
974
#ifdef TD_ENTERPRISE
975
    case TDMT_VND_KILL_COMPACT:
3,112✔
976
      vnodeProcessKillCompactReq(pVnode, ver, pReq, len, pRsp);
3,112✔
977
      break;
3,112✔
978
#endif
979
    case TDMT_VND_KILL_SCAN:
244✔
980
      vnodeProcessKillScanReq(pVnode, ver, pReq, len, pRsp);
244✔
981
      break;
244✔
982
    case TDMT_VND_KILL_TRIM:
×
983
      vnodeProcessKillRetentionReq(pVnode, ver, pReq, len, pRsp);
×
984
      break;
×
985
    /* ARB */
986
    case TDMT_VND_ARB_CHECK_SYNC:
6,494✔
987
      vnodeProcessArbCheckSyncReq(pVnode, pReq, len, pRsp);
6,494✔
988
      break;
6,494✔
989
    case TDMT_VND_AUDIT_RECORD:
600✔
990
      vTrace("vgId:%d, processed audit msg", TD_VID(pVnode));
600✔
991
      code = vnodeProcessAuditRecordReq(pVnode, ver, pReq, len, pMsg);
600✔
992
      pRsp->code = code;
600✔
993
      pRsp->msgType = TDMT_VND_AUDIT_RECORD_RSP;
600✔
994
      pRsp->pCont = NULL;
600✔
995
      pRsp->contLen = 0;
600✔
996
      if (code) {
600✔
997
        goto _err;
×
998
      }
999
      break;
600✔
1000
    default:
×
1001
      vError("vgId:%d, unprocessed msg, %d", TD_VID(pVnode), pMsg->msgType);
×
1002
      return TSDB_CODE_INVALID_MSG;
×
1003
  }
1004

1005
  vGDebug(&pMsg->info.traceId, "vgId:%d, index:%" PRId64 ", msg processed, code:0x%x", TD_VID(pVnode), ver, pRsp->code);
1,493,751,942✔
1006

1007
  walApplyVer(pVnode->pWal, ver);
1,493,785,746✔
1008

1009
  if (pVnode->pTq) {
1,493,807,062✔
1010
    code = tqPushMsg(pVnode->pTq, pMsg->msgType);
1,493,807,062✔
1011
    if (code) {
1,493,802,758✔
1012
      vError("vgId:%d, failed to push msg to TQ since %s", TD_VID(pVnode), tstrerror(terrno));
×
1013
      return code;
×
1014
    }
1015
  }
1016

1017
  // commit if need
1018
  if (needCommit) {
1,493,802,758✔
1019
    vInfo("vgId:%d, commit at version %" PRId64, TD_VID(pVnode), ver);
10,420,686✔
1020
    code = vnodeAsyncCommit(pVnode, forceTrimWal);
10,423,564✔
1021
    if (code) {
10,424,962✔
1022
      vError("vgId:%d, failed to vnode async commit since %s.", TD_VID(pVnode), tstrerror(terrno));
×
1023
      goto _err;
×
1024
    }
1025

1026
    // start a new one
1027
    METRICS_TIMING_BLOCK(pVnode->writeMetrics.memtable_wait_time, METRIC_LEVEL_LOW, {
10,424,962✔
1028
      code = vnodeBegin(pVnode);
1029
      if (code) {
1030
        vError("vgId:%d, failed to begin vnode since %s.", TD_VID(pVnode), tstrerror(terrno));
1031
        goto _err;
1032
      }
1033
    });
1034
  }
1035

1036
_exit:
1,493,804,978✔
1037
  return 0;
1,516,585,176✔
1038

1039
_err:
68,998✔
1040
  if (code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
68,998✔
1041
    vInfo("vgId:%d, process %s request failed since %s, lino 1:%d ver:%" PRId64, TD_VID(pVnode),
6,216✔
1042
          TMSG_INFO(pMsg->msgType), tstrerror(code), lino, ver);
1043
  } else if (code) {
62,782✔
1044
    vError("vgId:%d, process %s request failed since %s, lino 2:%d ver:%" PRId64, TD_VID(pVnode),
×
1045
           TMSG_INFO(pMsg->msgType), tstrerror(code), lino, ver);
1046
  }
1047

1048
  return code;
68,998✔
1049
}
1050

1051
int32_t vnodePreprocessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg) {
854,843,746✔
1052
  if (TDMT_SCH_QUERY != pMsg->msgType && TDMT_SCH_MERGE_QUERY != pMsg->msgType) {
854,843,746✔
1053
    return 0;
160,220,586✔
1054
  }
1055

1056
  int32_t qType = 0;
694,666,518✔
1057
  return qWorkerPreprocessQueryMsg(pVnode->pQuery, pMsg, (TDMT_SCH_QUERY == pMsg->msgType), &qType);
694,664,886✔
1058
}
1059

1060
int32_t vnodeProcessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
854,925,510✔
1061
  const STraceId *trace = &pMsg->info.traceId;
854,925,510✔
1062
  vDebug("vgId:%d, msg:%s, %p in vnode query queue is processing, %" PRIx64 ":%" PRIx64, 
854,931,890✔
1063
    pVnode->config.vgId, TMSG_INFO(pMsg->msgType), pMsg, TRACE_GET_ROOTID(trace), TRACE_GET_MSGID(trace));
1064

1065
  if (pMsg->msgType == TDMT_VND_TMQ_CONSUME && !syncIsReadyForRead(pVnode->sync)) {
854,961,036✔
1066
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
800,800✔
1067
    return 0;
800,800✔
1068
  }
1069

1070
  if (pMsg->msgType == TDMT_VND_TMQ_CONSUME && !pVnode->restored) {
854,163,140✔
1071
    vnodeRedirectRpcMsg(pVnode, pMsg, TSDB_CODE_SYN_RESTORING);
×
1072
    return 0;
×
1073
  }
1074

1075
  SReadHandle handle = {0};
854,158,556✔
1076
  handle.vnode = pVnode;
854,158,788✔
1077
  handle.pMsgCb = &pVnode->msgCb;
854,158,788✔
1078
  handle.pWorkerCb = pInfo->workerCb;
854,123,368✔
1079
  initStorageAPI(&handle.api);
854,125,260✔
1080
  int32_t code = TSDB_CODE_SUCCESS;
854,005,700✔
1081
  bool    redirected = false;
854,005,700✔
1082

1083
  switch (pMsg->msgType) {
854,005,700✔
1084
    case TDMT_SCH_QUERY:
572,000,574✔
1085
      if (!syncIsReadyForRead(pVnode->sync)) {
572,000,574✔
1086
        pMsg->code = (terrno) ? terrno : TSDB_CODE_SYN_NOT_LEADER;
4,586,702✔
1087
        redirected = true;
4,586,702✔
1088
      }
1089
      code = qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
572,049,264✔
1090
      if (redirected) {
571,997,678✔
1091
        vnodeRedirectRpcMsg(pVnode, pMsg, pMsg->code);
4,586,702✔
1092
        return 0;
4,586,702✔
1093
      }
1094

1095
      return code;
567,410,976✔
1096
    case TDMT_SCH_MERGE_QUERY:
122,667,278✔
1097
      return qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
122,667,278✔
1098
    case TDMT_SCH_QUERY_CONTINUE:
90,087,858✔
1099
      return qWorkerProcessCQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
90,087,858✔
1100
    case TDMT_VND_TMQ_CONSUME:
52,054,414✔
1101
      return tqProcessPollReq(pVnode->pTq, pMsg);
52,054,414✔
1102
    case TDMT_VND_TMQ_CONSUME_PUSH:
17,277,612✔
1103
      return tqProcessPollPush(pVnode->pTq);
17,277,612✔
1104
    default:
×
1105
      vError("unknown msg type:%d in query queue", pMsg->msgType);
×
1106
      return TSDB_CODE_APP_ERROR;
×
1107
  }
1108
}
1109

1110
int32_t vnodeProcessFetchMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
2,066,816,340✔
1111
  int32_t code = TSDB_CODE_SUCCESS;
2,066,816,340✔
1112
  const STraceId *trace = &pMsg->info.traceId;
2,066,816,340✔
1113
  vDebug("vgId:%d, msg:%s, %p in fetch queue is processing, %" PRIx64 ":%" PRIx64, 
2,066,900,494✔
1114
    pVnode->config.vgId, TMSG_INFO(pMsg->msgType), pMsg, TRACE_GET_ROOTID(trace), TRACE_GET_MSGID(trace));
1115
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
2,067,129,594✔
1116
       pMsg->msgType == TDMT_VND_BATCH_META || pMsg->msgType == TDMT_VND_TABLE_NAME ||
1,432,856,208✔
1117
       pMsg->msgType == TDMT_VND_VSUBTABLES_META || pMsg->msgType == TDMT_VND_VSTB_REF_DBS) &&
1,374,166,260✔
1118
      !syncIsReadyForRead(pVnode->sync)) {
692,979,474✔
1119
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
1,186,800✔
1120
    return 0;
1,186,800✔
1121
  }
1122

1123
  switch (pMsg->msgType) {
2,066,039,248✔
1124
    case TDMT_SCH_FETCH:
957,542,624✔
1125
    case TDMT_SCH_MERGE_FETCH:
1126
      return qWorkerProcessFetchMsg(pVnode, pVnode->pQuery, pMsg, 0);
957,542,624✔
1127
    case TDMT_SCH_FETCH_RSP:
×
1128
      return qWorkerProcessRspMsg(pVnode, pVnode->pQuery, pMsg, 0);
×
1129
    // case TDMT_SCH_CANCEL_TASK:
1130
    //   return qWorkerProcessCancelMsg(pVnode, pVnode->pQuery, pMsg, 0);
1131
    case TDMT_SCH_DROP_TASK:
694,167,760✔
1132
      return qWorkerProcessDropMsg(pVnode, pVnode->pQuery, pMsg, 0);
694,167,760✔
1133
    case TDMT_SCH_TASK_NOTIFY:
32,238✔
1134
      return qWorkerProcessNotifyMsg(pVnode, pVnode->pQuery, pMsg, 0);
32,238✔
1135
    case TDMT_SCH_QUERY_HEARTBEAT:
354,309,436✔
1136
      return qWorkerProcessHbMsg(pVnode, pVnode->pQuery, pMsg, 0);
354,309,436✔
1137
    case TDMT_VND_TABLE_META:
1,896,270✔
1138
    case TDMT_VND_TABLE_NAME:
1139
      return vnodeGetTableMeta(pVnode, pMsg, true);
1,896,270✔
1140
    case TDMT_VND_TABLE_CFG:
×
1141
      return vnodeGetTableCfg(pVnode, pMsg, true);
×
1142
    case TDMT_VND_BATCH_META: {
57,496,960✔
1143
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.fetch_batch_meta_time, METRIC_LEVEL_LOW,
57,496,960✔
1144
                           { code = vnodeGetBatchMeta(pVnode, pMsg); });
1145
      METRICS_UPDATE(pVnode->writeMetrics.fetch_batch_meta_count, METRIC_LEVEL_LOW, 1);
57,498,806✔
1146
      return code;
57,491,782✔
1147
    }
1148
    case TDMT_VND_VSUBTABLES_META:
×
1149
      return vnodeGetVSubtablesMeta(pVnode, pMsg);
×
1150
    case TDMT_VND_VSTB_REF_DBS:
×
1151
      return vnodeGetVStbRefDbs(pVnode, pMsg);
×
1152
    case TDMT_VND_QUERY_SCAN_PROGRESS:
1,220✔
1153
      return vnodeQueryScanProgress(pVnode, pMsg);
1,220✔
1154
#ifdef TD_ENTERPRISE
1155
    case TDMT_VND_QUERY_COMPACT_PROGRESS:
199,054✔
1156
      return vnodeQueryCompactProgress(pVnode, pMsg);
199,054✔
1157

1158
    case TDMT_VND_LIST_SSMIGRATE_FILESETS:
9,012✔
1159
      return vnodeListSsMigrateFileSets(pVnode, pMsg);
9,012✔
1160

1161
    case TDMT_VND_QUERY_SSMIGRATE_PROGRESS:
13,518✔
1162
      return vnodeQuerySsMigrateProgress(pVnode, pMsg);
13,518✔
1163
#endif
1164
    case TDMT_VND_QUERY_TRIM_PROGRESS:
281,562✔
1165
      return vnodeQueryRetentionProgress(pVnode, pMsg);
281,562✔
1166
      //    case TDMT_VND_TMQ_CONSUME:
1167
      //      return tqProcessPollReq(pVnode->pTq, pMsg);
1168
#ifdef USE_TQ
1169
    case TDMT_VND_TMQ_VG_WALINFO:
1,304✔
1170
      return tqProcessVgWalInfoReq(pVnode->pTq, pMsg);
1,304✔
1171
    case TDMT_VND_TMQ_VG_COMMITTEDINFO:
652✔
1172
      return tqProcessVgCommittedInfoReq(pVnode->pTq, pMsg);
652✔
1173
    case TDMT_VND_TMQ_SEEK:
2,608✔
1174
      return tqProcessSeekReq(pVnode->pTq, pMsg);
2,608✔
1175
#endif
1176
    default:
39,922✔
1177
      vError("unknown msg type:%d in fetch queue", pMsg->msgType);
39,922✔
1178
      return TSDB_CODE_APP_ERROR;
×
1179
  }
1180
}
1181

1182
void vnodeUpdateMetaRsp(SVnode *pVnode, STableMetaRsp *pMetaRsp) {
151,035,418✔
1183
  if (NULL == pMetaRsp) {
151,035,418✔
1184
    return;
×
1185
  }
1186

1187
  tstrncpy(pMetaRsp->dbFName, pVnode->config.dbname, TSDB_DB_FNAME_LEN);
151,035,418✔
1188
  pMetaRsp->dbId = pVnode->config.dbId;
151,038,018✔
1189
  pMetaRsp->vgId = TD_VID(pVnode);
151,040,790✔
1190
  pMetaRsp->precision = pVnode->config.tsdbCfg.precision;
151,039,958✔
1191
}
1192

1193
extern int32_t vnodeAsyncRetention(SVnode *pVnode, STimeWindow tw, int8_t optrType, int8_t triggerType);
1194

1195
static int32_t vnodeProcessTrimReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
162,534✔
1196
  if (!pVnode->restored) {
162,534✔
1197
    vInfo("vgId:%d, ignore trim req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
1,712✔
1198
    return 0;
1,712✔
1199
  }
1200

1201
  int32_t     code = 0;
160,822✔
1202
  SVTrimDbReq trimReq = {0};
160,822✔
1203

1204
  // decode
1205
  if (tDeserializeSVTrimDbReq(pReq, len, &trimReq) != 0) {
160,822✔
1206
    code = TSDB_CODE_INVALID_MSG;
×
1207
    goto _exit;
×
1208
  }
1209

1210
  vInfo("vgId:%d, process trim vnode request, time:%" PRIi64 ",%" PRIi64 ", optr:%d, trigger:%d", pVnode->config.vgId,
160,822✔
1211
        trimReq.tw.skey, trimReq.tw.ekey, (int32_t)trimReq.optrType, (int32_t)trimReq.triggerType);
1212

1213
  code = vnodeAsyncRetention(pVnode, trimReq.tw, (int8_t)trimReq.optrType, (int8_t)trimReq.triggerType);
160,822✔
1214

1215
_exit:
160,822✔
1216
  return code;
160,822✔
1217
}
1218

1219
extern int32_t vnodeAsyncSsMigrateFileSet(SVnode *pVnode, SSsMigrateFileSetReq *pReq);
1220

1221
extern int32_t vnodeAsyncS3Migrate(SVnode *pVnode, int64_t now);
1222

1223
static int32_t vnodeProcessSsMigrateFileSetReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
7,510✔
1224
  int32_t code = 0;
7,510✔
1225

1226
  SSsMigrateFileSetReq req = {0};
7,510✔
1227
  SSsMigrateFileSetRsp rsp = {0};
7,510✔
1228
  pRsp->msgType = TDMT_VND_SSMIGRATE_FILESET_RSP;
7,510✔
1229
  pRsp->code = 0;
7,510✔
1230
  pRsp->pCont = NULL;
7,510✔
1231
  pRsp->contLen = 0;
7,510✔
1232

1233
  if (tDeserializeSSsMigrateFileSetReq(pReq, len, &req) != 0) {
7,510✔
1234
    code = TSDB_CODE_INVALID_MSG;
×
1235
    goto _exit;
×
1236
  }
1237

1238
  vInfo("vgId:%d, ssmigrate:%d, fid:%d, process ssmigrate fileset request, time:%" PRId64, pVnode->config.vgId,
7,510✔
1239
        req.ssMigrateId, req.fid, req.startTimeSec);
1240
  rsp.ssMigrateId = req.ssMigrateId;
7,510✔
1241
  rsp.vgId = TD_VID(pVnode);
7,510✔
1242
  rsp.nodeId = req.nodeId;
7,510✔
1243
  rsp.fid = req.fid;
7,510✔
1244

1245
  code = vnodeAsyncSsMigrateFileSet(pVnode, &req);
7,510✔
1246
  if (code != TSDB_CODE_SUCCESS) {
7,510✔
1247
    vError("vgId:%d, failed to async ssmigrate since %s", TD_VID(pVnode), tstrerror(code));
×
1248
    pRsp->code = code;
×
1249
    goto _exit;
×
1250
  }
1251

1252
  pRsp->code = TSDB_CODE_SUCCESS;
7,510✔
1253
  pRsp->contLen = tSerializeSSsMigrateFileSetRsp(NULL, 0, &rsp);
7,510✔
1254
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
7,510✔
1255
  if (pRsp->pCont == NULL) {
7,510✔
1256
    vError("vgId:%d, failed to allocate memory for ssmigrate fileset response", TD_VID(pVnode));
×
1257
    code = TSDB_CODE_OUT_OF_MEMORY;
×
1258
    goto _exit;
×
1259
  }
1260
  TAOS_UNUSED(tSerializeSSsMigrateFileSetRsp(pRsp->pCont, pRsp->contLen, &rsp));
7,510✔
1261

1262
_exit:
7,510✔
1263
  pRsp->code = code;
7,510✔
1264
  return code;
7,510✔
1265
}
1266

1267
extern int32_t vnodeFollowerSsMigrate(SVnode *pVnode, SSsMigrateProgress *pReq);
1268

1269
static int32_t vnodeProcessFollowerSsMigrateReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
13,518✔
1270
  int32_t            code = 0;
13,518✔
1271
  SSsMigrateProgress req = {0};
13,518✔
1272

1273
  // decode
1274
  if (tDeserializeSSsMigrateProgress(pReq, len, &req) != 0) {
13,518✔
1275
    code = TSDB_CODE_INVALID_MSG;
×
1276
    goto _exit;
×
1277
  }
1278

1279
  code = vnodeFollowerSsMigrate(pVnode, &req);
13,518✔
1280

1281
_exit:
13,518✔
1282
  pRsp->code = code;
13,518✔
1283
  return code;
13,518✔
1284
}
1285

1286
extern int32_t vnodeKillSsMigrate(SVnode *pVnode, SVnodeKillSsMigrateReq *pReq);
1287

1288
static int32_t vnodeProcessKillSsMigrateReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1289
  int32_t          code = 0;
×
1290
  SVnodeKillSsMigrateReq req = {0};
×
1291

1292
  // decode
1293
  if (tDeserializeSVnodeKillSsMigrateReq(pReq, len, &req) != 0) {
×
1294
    code = TSDB_CODE_INVALID_MSG;
×
1295
    goto _exit;
×
1296
  }
1297

1298
  code = vnodeKillSsMigrate(pVnode, &req);
×
1299

1300
_exit:
×
1301
  pRsp->code = code;
×
1302
  return code;
×
1303
}
1304

1305
static int32_t vnodeProcessDropTtlTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1306
  int               ret = 0;
×
1307
  SVDropTtlTableReq ttlReq = {0};
×
1308
  if (tDeserializeSVDropTtlTableReq(pReq, len, &ttlReq) != 0) {
×
1309
    ret = TSDB_CODE_INVALID_MSG;
×
1310
    goto end;
×
1311
  }
1312

1313
  if (ttlReq.nUids != taosArrayGetSize(ttlReq.pTbUids)) {
×
1314
    ret = TSDB_CODE_INVALID_MSG;
×
1315
    goto end;
×
1316
  }
1317

1318
  if (ttlReq.nUids != 0) {
×
1319
    vInfo("vgId:%d, process drop ttl table request, time:%d, ntbUids:%d", pVnode->config.vgId, ttlReq.timestampSec,
×
1320
          ttlReq.nUids);
1321
  }
1322

1323
  if (ttlReq.nUids > 0) {
×
1324
    int32_t code = metaDropMultipleTables(pVnode->pMeta, ver, ttlReq.pTbUids);
×
1325
    if (code) return code;
×
1326

1327
    code = tqDeleteTbUidList(pVnode->pTq, ttlReq.pTbUids);
×
1328
    if (code) {
×
1329
      vError("vgId:%d, failed to delete tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1330
    }
1331
  }
1332

1333
end:
×
1334
  taosArrayDestroy(ttlReq.pTbUids);
×
1335
  return ret;
×
1336
}
1337

1338
static int32_t vnodeProcessFetchTtlExpiredTbs(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
24,036✔
1339
  int32_t                 code = -1;
24,036✔
1340
  SMetaReader             mr = {0};
24,036✔
1341
  SVDropTtlTableReq       ttlReq = {0};
24,036✔
1342
  SVFetchTtlExpiredTbsRsp rsp = {0};
24,036✔
1343
  SEncoder                encoder = {0};
24,036✔
1344
  SArray                 *pNames = NULL;
24,036✔
1345
  pRsp->msgType = TDMT_VND_FETCH_TTL_EXPIRED_TBS_RSP;
24,036✔
1346
  pRsp->code = TSDB_CODE_SUCCESS;
24,036✔
1347
  pRsp->pCont = NULL;
24,036✔
1348
  pRsp->contLen = 0;
24,036✔
1349

1350
  if (tDeserializeSVDropTtlTableReq(pReq, len, &ttlReq) != 0) {
24,036✔
1351
    terrno = TSDB_CODE_INVALID_MSG;
×
1352
    goto _end;
×
1353
  }
1354

1355
  if (!(ttlReq.nUids == taosArrayGetSize(ttlReq.pTbUids))) {
24,036✔
1356
    terrno = TSDB_CODE_INVALID_MSG;
×
1357
    goto _end;
×
1358
  }
1359

1360
  tb_uid_t    suid;
1361
  char        ctbName[TSDB_TABLE_NAME_LEN];
1362
  SVDropTbReq expiredTb = {.igNotExists = true};
24,036✔
1363
  metaReaderDoInit(&mr, pVnode->pMeta, 0);
24,036✔
1364
  rsp.vgId = TD_VID(pVnode);
24,036✔
1365
  rsp.pExpiredTbs = taosArrayInit(ttlReq.nUids, sizeof(SVDropTbReq));
24,036✔
1366
  if (!rsp.pExpiredTbs) goto _end;
24,036✔
1367

1368
  pNames = taosArrayInit(ttlReq.nUids, TSDB_TABLE_NAME_LEN);
24,036✔
1369
  if (!pNames) {
24,036✔
1370
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1371
    goto _end;
×
1372
  }
1373
  char buf[TSDB_TABLE_NAME_LEN];
24,036✔
1374
  for (int32_t i = 0; i < ttlReq.nUids; ++i) {
122,832✔
1375
    tb_uid_t *uid = taosArrayGet(ttlReq.pTbUids, i);
98,796✔
1376
    expiredTb.suid = *uid;
98,796✔
1377
    terrno = metaReaderGetTableEntryByUid(&mr, *uid);
98,796✔
1378
    if (terrno < 0) goto _end;
98,796✔
1379
    tstrncpy(buf, mr.me.name, TSDB_TABLE_NAME_LEN);
98,796✔
1380
    void *p = taosArrayPush(pNames, buf);
98,796✔
1381
    if (p == NULL) {
98,796✔
1382
      goto _end;
×
1383
    }
1384

1385
    expiredTb.name = p;
98,796✔
1386
    if (mr.me.type == TSDB_CHILD_TABLE) {
98,796✔
1387
      expiredTb.suid = mr.me.ctbEntry.suid;
64,208✔
1388
    }
1389

1390
    if (taosArrayPush(rsp.pExpiredTbs, &expiredTb) == NULL) {
197,592✔
1391
      goto _end;
×
1392
    }
1393
  }
1394

1395
  int32_t ret = 0;
24,036✔
1396
  tEncodeSize(tEncodeVFetchTtlExpiredTbsRsp, &rsp, pRsp->contLen, ret);
24,036✔
1397
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
24,036✔
1398
  if (pRsp->pCont == NULL) {
24,036✔
1399
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1400
    code = -1;
×
1401
    goto _end;
×
1402
  }
1403
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
24,036✔
1404
  terrno = tEncodeVFetchTtlExpiredTbsRsp(&encoder, &rsp);
24,036✔
1405
  tEncoderClear(&encoder);
24,036✔
1406

1407
  if (terrno == 0) code = 0;
24,036✔
1408
_end:
24,036✔
1409
  metaReaderClear(&mr);
24,036✔
1410
  tFreeFetchTtlExpiredTbsRsp(&rsp);
24,036✔
1411
  taosArrayDestroy(ttlReq.pTbUids);
24,036✔
1412
  if (pNames) taosArrayDestroy(pNames);
24,036✔
1413
  pRsp->code = terrno;
24,036✔
1414
  return code;
24,036✔
1415
}
1416

1417
static int32_t vnodeProcessCreateStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
11,208,134✔
1418
  int32_t        code = 0;
11,208,134✔
1419
  SVCreateStbReq req = {0};
11,208,134✔
1420
  SDecoder       coder;
11,201,390✔
1421

1422
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
11,210,928✔
1423
  pRsp->code = TSDB_CODE_SUCCESS;
11,210,928✔
1424
  pRsp->pCont = NULL;
11,210,928✔
1425
  pRsp->contLen = 0;
11,210,198✔
1426

1427
  // decode and process req
1428
  tDecoderInit(&coder, pReq, len);
11,210,198✔
1429

1430
  code = tDecodeSVCreateStbReq(&coder, &req);
11,210,956✔
1431
  if (code) {
11,165,232✔
1432
    pRsp->code = code;
×
1433
    goto _err;
×
1434
  }
1435

1436
  code = metaCreateSuperTable(pVnode->pMeta, ver, &req);
11,165,232✔
1437
  if (code) {
11,210,776✔
1438
    pRsp->code = code;
6,216✔
1439
    goto _err;
6,216✔
1440
  }
1441

1442
  tDecoderClear(&coder);
11,204,560✔
1443
  return 0;
11,203,952✔
1444

1445
_err:
6,216✔
1446
  tDecoderClear(&coder);
6,216✔
1447
  return code;
6,216✔
1448
}
1449

1450
static int32_t vnodeProcessCreateTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
94,959,134✔
1451
                                       SRpcMsg *pOriginRpc) {
1452
  SDecoder           decoder = {0};
94,959,134✔
1453
  SEncoder           encoder = {0};
94,959,134✔
1454
  int32_t            rcode = 0;
94,958,248✔
1455
  SVCreateTbBatchReq req = {0};
94,958,248✔
1456
  SVCreateTbReq     *pCreateReq;
1457
  SVCreateTbBatchRsp rsp = {0};
94,958,284✔
1458
  SVCreateTbRsp      cRsp = {0};
94,958,248✔
1459
  char               tbName[TSDB_TABLE_FNAME_LEN];
94,953,232✔
1460
  SArray            *tbUids = NULL;
94,958,284✔
1461
  SArray            *tbNames = NULL;
94,958,284✔
1462
  int64_t            tss = taosGetTimestampMs();
94,957,398✔
1463
  pRsp->msgType = TDMT_VND_CREATE_TABLE_RSP;
94,957,398✔
1464
  pRsp->code = TSDB_CODE_SUCCESS;
94,958,284✔
1465
  pRsp->pCont = NULL;
94,959,134✔
1466
  pRsp->contLen = 0;
94,957,398✔
1467

1468
  // decode
1469
  tDecoderInit(&decoder, pReq, len);
94,959,134✔
1470
  if (tDecodeSVCreateTbBatchReq(&decoder, &req) < 0) {
94,958,248✔
1471
    rcode = -1;
×
1472
    terrno = TSDB_CODE_INVALID_MSG;
×
1473
    goto _exit;
×
1474
  }
1475

1476
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(cRsp));
94,944,274✔
1477
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
94,951,366✔
1478
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
94,942,242✔
1479
  if (rsp.pArray == NULL || tbUids == NULL || tbNames == NULL) {
94,947,056✔
1480
    rcode = -1;
×
1481
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1482
    goto _exit;
×
1483
  }
1484

1485
  // loop to create table
1486
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
207,346,082✔
1487
    pCreateReq = req.pReqs + iReq;
112,386,886✔
1488
    memset(&cRsp, 0, sizeof(cRsp));
112,372,626✔
1489

1490
    if (tsEnableAudit && tsEnableAuditCreateTable && tsAuditLevel >= AUDIT_LEVEL_CHILDTABLE) {
112,372,626✔
1491
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
308✔
1492
      if (str == NULL) {
308✔
1493
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1494
        rcode = -1;
×
1495
        goto _exit;
×
1496
      }
1497
      tstrncpy(str, pCreateReq->name, TSDB_TABLE_FNAME_LEN);
308✔
1498
      if (taosArrayPush(tbNames, &str) == NULL) {
308✔
1499
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1500
        rcode = -1;
×
1501
        goto _exit;
×
1502
      }
1503
    }
1504

1505
    // validate hash
1506
    (void)tsnprintf(tbName, TSDB_TABLE_FNAME_LEN, "%s.%s", pVnode->config.dbname, pCreateReq->name);
112,372,626✔
1507
    if (vnodeValidateTableHash(pVnode, tbName) < 0) {
112,388,964✔
1508
      cRsp.code = TSDB_CODE_VND_HASH_MISMATCH;
×
1509
      if (taosArrayPush(rsp.pArray, &cRsp) == NULL) {
×
1510
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1511
        rcode = -1;
×
1512
        goto _exit;
×
1513
      }
1514
      vError("vgId:%d create-table:%s failed due to hash value mismatch", TD_VID(pVnode), tbName);
×
1515
      continue;
×
1516
    }
1517

1518
    // do create table
1519
    if (metaCreateTable2(pVnode->pMeta, ver, pCreateReq, &cRsp.pMeta) < 0) {
112,354,122✔
1520
      if (pCreateReq->flags & TD_CREATE_IF_NOT_EXISTS && terrno == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
35,286✔
1521
        cRsp.code = TSDB_CODE_SUCCESS;
11,250✔
1522
      } else {
1523
        cRsp.code = terrno;
24,036✔
1524
      }
1525
    } else {
1526
      cRsp.code = TSDB_CODE_SUCCESS;
112,355,878✔
1527
      if (taosArrayPush(tbUids, &pCreateReq->uid) == NULL) {
224,712,546✔
1528
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1529
        rcode = -1;
×
1530
        goto _exit;
×
1531
      }
1532
      vnodeUpdateMetaRsp(pVnode, cRsp.pMeta);
112,356,668✔
1533
    }
1534

1535
    if (taosArrayPush(rsp.pArray, &cRsp) == NULL) {
224,782,978✔
1536
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1537
      rcode = -1;
×
1538
      goto _exit;
×
1539
    }
1540
  }
1541

1542
  vTrace("vgId:%d, add %d new created tables into query table list", TD_VID(pVnode), (int32_t)taosArrayGetSize(tbUids));
94,959,196✔
1543
  if (tqAddTbUidList(pVnode->pTq, tbUids) < 0) {
94,959,196✔
1544
    vError("vgId:%d, failed to add tbUid list since %s", TD_VID(pVnode), tstrerror(terrno));
×
1545
  }
1546

1547
  // prepare rsp
1548
  int32_t ret = 0;
94,959,134✔
1549
  tEncodeSize(tEncodeSVCreateTbBatchRsp, &rsp, pRsp->contLen, ret);
94,959,134✔
1550
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
94,950,608✔
1551
  if (pRsp->pCont == NULL) {
94,943,860✔
1552
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1553
    rcode = -1;
×
1554
    goto _exit;
×
1555
  }
1556
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
94,945,406✔
1557
  if (tEncodeSVCreateTbBatchRsp(&encoder, &rsp) != 0) {
94,945,182✔
1558
    vError("vgId:%d, failed to encode create table batch response", TD_VID(pVnode));
×
1559
  }
1560

1561
  if (tsEnableAudit && tsEnableAuditCreateTable && tsAuditLevel >= AUDIT_LEVEL_CHILDTABLE) {
94,947,750✔
1562
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
308✔
1563

1564
    SName name = {0};
308✔
1565
    if (tNameFromString(&name, pVnode->config.dbname, T_NAME_ACCT | T_NAME_DB) < 0) {
308✔
1566
      vError("vgId:%d, failed to get name from string", TD_VID(pVnode));
×
1567
    }
1568

1569
    SStringBuilder sb = {0};
308✔
1570
    for (int32_t i = 0; i < tbNames->size; i++) {
616✔
1571
      char **key = (char **)taosArrayGet(tbNames, i);
308✔
1572
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
308✔
1573
      if (i < tbNames->size - 1) {
308✔
1574
        taosStringBuilderAppendChar(&sb, ',');
×
1575
      }
1576
      // taosMemoryFreeClear(*key);
1577
    }
1578

1579
    size_t len = 0;
308✔
1580
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
308✔
1581

1582
    if (strlen(RPC_MSG_USER(pOriginRpc)) > 0) {
308✔
1583
      int64_t tse = taosGetTimestampMs();
308✔
1584
      double  duration = (double)(tse - tss);
308✔
1585
      duration = duration / 1000;
308✔
1586
      auditAddRecord(pOriginRpc, clusterId, "createTable", name.dbname, "", keyJoined, len, duration, 0);
308✔
1587
    }
1588

1589
    taosStringBuilderDestroy(&sb);
308✔
1590
  }
1591

1592
_exit:
94,947,750✔
1593
  tDeleteSVCreateTbBatchReq(&req);
94,944,282✔
1594
  taosArrayDestroyEx(rsp.pArray, tFreeSVCreateTbRsp);
94,949,610✔
1595
  taosArrayDestroy(tbUids);
94,921,144✔
1596
  tDecoderClear(&decoder);
94,912,566✔
1597
  tEncoderClear(&encoder);
94,952,062✔
1598
  taosArrayDestroyP(tbNames, NULL);
94,948,712✔
1599
  return rcode;
94,936,132✔
1600
}
1601

1602
static int32_t vnodeProcessAlterStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
15,537,440✔
1603
  int32_t        code = 0;
15,537,440✔
1604
  SVCreateStbReq req = {0};
15,537,440✔
1605
  SDecoder       dc = {0};
15,537,440✔
1606

1607
  pRsp->msgType = TDMT_VND_ALTER_STB_RSP;
15,537,440✔
1608
  pRsp->code = TSDB_CODE_SUCCESS;
15,537,440✔
1609
  pRsp->pCont = NULL;
15,537,440✔
1610
  pRsp->contLen = 0;
15,537,440✔
1611

1612
  tDecoderInit(&dc, pReq, len);
15,537,440✔
1613

1614
  // decode req
1615
  code = tDecodeSVCreateStbReq(&dc, &req);
15,537,440✔
1616
  if (code) {
15,516,858✔
1617
    tDecoderClear(&dc);
×
1618
    return code;
×
1619
  }
1620

1621
  code = metaAlterSuperTable(pVnode->pMeta, ver, &req);
15,516,858✔
1622
  if (code) {
15,537,440✔
1623
    pRsp->code = code;
×
1624
    tDecoderClear(&dc);
×
1625
    return code;
×
1626
  }
1627

1628
  tDecoderClear(&dc);
15,537,440✔
1629

1630
  return 0;
15,537,440✔
1631
}
1632

1633
static int32_t vnodeProcessDropStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp, SRpcMsg *pOriginRpc) {
1,894,180✔
1634
  SVDropStbReq req = {0};
1,894,180✔
1635
  int32_t      rcode = TSDB_CODE_SUCCESS;
1,894,180✔
1636
  SDecoder     decoder = {0};
1,894,180✔
1637
  SArray      *tbUidList = NULL;
1,894,180✔
1638

1639
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
1,894,180✔
1640
  pRsp->pCont = NULL;
1,894,180✔
1641
  pRsp->contLen = 0;
1,894,180✔
1642

1643
  // decode request
1644
  tDecoderInit(&decoder, pReq, len);
1,894,180✔
1645
  if (tDecodeSVDropStbReq(&decoder, &req) < 0) {
1,894,180✔
1646
    rcode = TSDB_CODE_INVALID_MSG;
×
1647
    goto _exit;
×
1648
  }
1649

1650
  STraceId* trace = &(pOriginRpc->info.traceId);
1,892,978✔
1651

1652
  vInfo("vgId:%d, start to process vnode-drop-stb, QID:0x%" PRIx64 ":0x%" PRIx64 ", drop stb:%s", TD_VID(pVnode), trace ? trace->rootId : 0, 
1,892,978✔
1653
              trace ? trace->msgId : 0, req.name);
1654

1655
  // process request
1656
  tbUidList = taosArrayInit(8, sizeof(int64_t));
1,894,180✔
1657
  if (tbUidList == NULL) goto _exit;
1,894,180✔
1658
  if (metaDropSuperTable(pVnode->pMeta, ver, &req) < 0) {
1,894,180✔
1659
    rcode = terrno;
3,108✔
1660
    goto _exit;
3,108✔
1661
  }
1662

1663
  if (tqDeleteTbUidList(pVnode->pTq, tbUidList) < 0) {
1,891,072✔
1664
    rcode = terrno;
×
1665
    goto _exit;
×
1666
  }
1667

1668
  // return rsp
1669
_exit:
1,894,180✔
1670
  vInfo("vgId:%d, finished to process vnode-drop-stb, QID:0x%" PRIx64 ":0x%" PRIx64 ", drop stb:%s", TD_VID(pVnode), trace ? trace->rootId : 0, 
1,894,180✔
1671
              trace ? trace->msgId : 0, req.name);
1672
  if (tbUidList) taosArrayDestroy(tbUidList);
1,894,180✔
1673
  pRsp->code = rcode;
1,894,180✔
1674
  tDecoderClear(&decoder);
1,894,180✔
1675
  return 0;
1,894,180✔
1676
}
1677

1678
static void alterTagForTmq(SVnode *pVnode, SVAlterTbReq *vAlterTbReq) {
15,661,224✔
1679
  int32_t       code = 0;
15,661,224✔
1680
  int32_t       lino = 0;
15,661,224✔
1681
  SArray* tbUids = NULL;
15,661,224✔
1682
  SArray* cidList = NULL;
15,661,224✔
1683

1684
  int64_t uid = metaGetTableEntryUidByName(pVnode->pMeta, vAlterTbReq->tbName);
15,661,224✔
1685
  QUERY_CHECK_CONDITION(uid != 0, code, lino, end, TSDB_CODE_TDB_TABLE_NOT_EXIST);
15,661,224✔
1686
  
1687
  tbUids = taosArrayInit(4, sizeof(int64_t));
15,661,224✔
1688
  QUERY_CHECK_NULL(tbUids, code, lino, end, terrno);
15,661,224✔
1689

1690
  QUERY_CHECK_CONDITION(taosArrayPush(tbUids, &uid) != NULL, code, lino, end, terrno);
15,661,224✔
1691

1692
  cidList = taosArrayInit(4, sizeof(col_id_t));
15,661,224✔
1693
  QUERY_CHECK_NULL(cidList, code, lino, end, terrno);
15,661,224✔
1694

1695
  if (vAlterTbReq->action == TSDB_ALTER_TABLE_UPDATE_TAG_VAL){
15,661,224✔
1696
    col_id_t cid = vAlterTbReq->colId;
15,654,954✔
1697
    QUERY_CHECK_CONDITION(taosArrayPush(cidList, &cid) != NULL, code, lino, end, terrno);
15,654,954✔
1698
  } else {
1699
    for (int32_t i = 0; i < taosArrayGetSize(vAlterTbReq->pMultiTag); i++) {
39,540✔
1700
      SMultiTagUpateVal *pTagVal = taosArrayGet(vAlterTbReq->pMultiTag, i);
33,270✔
1701
      QUERY_CHECK_NULL(pTagVal, code, lino, end, terrno);
33,270✔
1702
      col_id_t cid = pTagVal->colId;
33,270✔
1703
      QUERY_CHECK_CONDITION(taosArrayPush(cidList, &cid) != NULL, code, lino, end, terrno);
33,270✔
1704
    }
1705
  }
1706
  
1707
  vDebug("vgId:%d, try to add table:%s in query table list, cidList size:%"PRIzu, TD_VID(pVnode), vAlterTbReq->tbName, taosArrayGetSize(cidList));
15,661,224✔
1708
  code = tqUpdateTbUidList(pVnode->pTq, tbUids, cidList);
15,661,224✔
1709
  QUERY_CHECK_CODE(code, lino, end);
15,661,224✔
1710

1711
end:
15,661,224✔
1712
  if (code != 0) {
15,661,224✔
1713
    qError("vgId:%d, failed to alter table:%s since %s", TD_VID(pVnode), vAlterTbReq->tbName, tstrerror(code));
×
1714
  }
1715
  taosArrayDestroy(tbUids);
15,661,224✔
1716
  taosArrayDestroy(cidList);
15,661,224✔
1717
}
15,661,224✔
1718

1719
static int32_t vnodeProcessAlterTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
24,874,264✔
1720
  SVAlterTbReq  vAlterTbReq = {0};
24,874,264✔
1721
  SVAlterTbRsp  vAlterTbRsp = {0};
24,874,264✔
1722
  SDecoder      dc = {0};
24,874,264✔
1723
  int32_t       code = 0;
24,874,264✔
1724
  int32_t       lino = 0;
24,874,264✔
1725
  int32_t       ret;
1726
  SEncoder      ec = {0};
24,874,264✔
1727
  STableMetaRsp vMetaRsp = {0};
24,874,264✔
1728

1729
  pRsp->msgType = TDMT_VND_ALTER_TABLE_RSP;
24,874,264✔
1730
  pRsp->pCont = NULL;
24,874,264✔
1731
  pRsp->contLen = 0;
24,874,264✔
1732
  pRsp->code = TSDB_CODE_SUCCESS;
24,874,264✔
1733

1734
  tDecoderInit(&dc, pReq, len);
24,874,264✔
1735

1736
  // decode
1737
  if (tDecodeSVAlterTbReq(&dc, &vAlterTbReq) < 0) {
24,874,264✔
1738
    vAlterTbRsp.code = TSDB_CODE_INVALID_MSG;
×
1739
    tDecoderClear(&dc);
×
1740
    goto _exit;
×
1741
  }
1742

1743
  // process
1744
  if (metaAlterTable(pVnode->pMeta, ver, &vAlterTbReq, &vMetaRsp) < 0) {
24,874,264✔
1745
    vAlterTbRsp.code = terrno;
1,449,696✔
1746
    tDecoderClear(&dc);
1,449,696✔
1747
    goto _exit;
1,449,696✔
1748
  }
1749
  tDecoderClear(&dc);
23,424,568✔
1750

1751
  if (NULL != vMetaRsp.pSchemas) {
23,424,568✔
1752
    vnodeUpdateMetaRsp(pVnode, &vMetaRsp);
7,698,700✔
1753
    vAlterTbRsp.pMeta = &vMetaRsp;
7,698,700✔
1754
  }
1755

1756
  if (vAlterTbReq.action == TSDB_ALTER_TABLE_UPDATE_TAG_VAL || vAlterTbReq.action == TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL) {
23,424,568✔
1757
    alterTagForTmq(pVnode, &vAlterTbReq);
15,661,224✔
1758
  }
1759

1760
_exit:
24,874,156✔
1761
  taosArrayDestroy(vAlterTbReq.pMultiTag);
24,874,264✔
1762
  tEncodeSize(tEncodeSVAlterTbRsp, &vAlterTbRsp, pRsp->contLen, ret);
24,874,264✔
1763
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
24,874,264✔
1764
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
24,874,264✔
1765
  if (tEncodeSVAlterTbRsp(&ec, &vAlterTbRsp) != 0) {
24,874,264✔
1766
    vError("vgId:%d, failed to encode alter table response", TD_VID(pVnode));
×
1767
  }
1768

1769
  tEncoderClear(&ec);
24,874,264✔
1770
  if (vMetaRsp.pSchemas) {
24,874,264✔
1771
    taosMemoryFree(vMetaRsp.pSchemas);
7,698,700✔
1772
    taosMemoryFree(vMetaRsp.pSchemaExt);
7,698,700✔
1773
  }
1774
  if (vMetaRsp.pColRefs) {
24,874,264✔
1775
    taosMemoryFree(vMetaRsp.pColRefs);
565,196✔
1776
  }
1777
  return 0;
24,874,264✔
1778
}
1779

1780
static int32_t vnodeProcessDropTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
2,966,218✔
1781
                                     SRpcMsg *pOriginRpc) {
1782
  SVDropTbBatchReq req = {0};
2,966,218✔
1783
  SVDropTbBatchRsp rsp = {0};
2,966,218✔
1784
  SDecoder         decoder = {0};
2,966,218✔
1785
  SEncoder         encoder = {0};
2,966,218✔
1786
  int32_t          ret;
1787
  SArray          *tbUids = NULL;
2,966,218✔
1788
  SArray          *tbNames = NULL;
2,966,218✔
1789
  int64_t          tss = taosGetTimestampMs();
2,966,218✔
1790

1791
  pRsp->msgType = ((SRpcMsg *)pReq)->msgType + 1;
2,966,218✔
1792
  pRsp->pCont = NULL;
2,966,218✔
1793
  pRsp->contLen = 0;
2,966,218✔
1794
  pRsp->code = TSDB_CODE_SUCCESS;
2,966,218✔
1795

1796
  // decode req
1797
  tDecoderInit(&decoder, pReq, len);
2,966,218✔
1798
  ret = tDecodeSVDropTbBatchReq(&decoder, &req);
2,966,218✔
1799
  if (ret < 0) {
2,966,218✔
1800
    terrno = TSDB_CODE_INVALID_MSG;
×
1801
    pRsp->code = terrno;
×
1802
    goto _exit;
×
1803
  }
1804

1805
  // process req
1806
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
2,966,218✔
1807
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(SVDropTbRsp));
2,966,218✔
1808
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
2,966,218✔
1809
  if (tbUids == NULL || rsp.pArray == NULL || tbNames == NULL) goto _exit;
2,966,218✔
1810

1811
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
6,040,816✔
1812
    SVDropTbReq *pDropTbReq = req.pReqs + iReq;
3,074,598✔
1813
    SVDropTbRsp  dropTbRsp = {0};
3,074,598✔
1814
    tb_uid_t     tbUid = 0;
3,074,598✔
1815

1816
    /* code */
1817
    ret = metaDropTable2(pVnode->pMeta, ver, pDropTbReq);
3,074,598✔
1818
    if (ret < 0) {
3,074,598✔
1819
      if (pDropTbReq->igNotExists && terrno == TSDB_CODE_TDB_TABLE_NOT_EXIST) {
×
1820
        dropTbRsp.code = TSDB_CODE_SUCCESS;
×
1821
      } else {
1822
        dropTbRsp.code = terrno;
×
1823
      }
1824
    } else {
1825
      dropTbRsp.code = TSDB_CODE_SUCCESS;
3,074,598✔
1826
    }
1827

1828
    if (taosArrayPush(rsp.pArray, &dropTbRsp) == NULL) {
6,149,196✔
1829
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1830
      pRsp->code = terrno;
×
1831
      goto _exit;
×
1832
    }
1833

1834
    if (tsEnableAudit && tsEnableAuditCreateTable && tsAuditLevel >= AUDIT_LEVEL_CHILDTABLE) {
3,074,598✔
1835
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
×
1836
      if (str == NULL) {
×
1837
        pRsp->code = terrno;
×
1838
        goto _exit;
×
1839
      }
1840
      tstrncpy(str, pDropTbReq->name, TSDB_TABLE_FNAME_LEN);
×
1841
      if (taosArrayPush(tbNames, &str) == NULL) {
×
1842
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1843
        pRsp->code = terrno;
×
1844
        goto _exit;
×
1845
      }
1846
    }
1847
  }
1848

1849
  if (tqDeleteTbUidList(pVnode->pTq, tbUids) < 0) {
2,966,218✔
1850
    vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(terrno));
×
1851
  }
1852

1853
  if (tsEnableAudit && tsEnableAuditCreateTable && tsAuditLevel >= AUDIT_LEVEL_CHILDTABLE) {
2,966,218✔
1854
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
×
1855

1856
    SName name = {0};
×
1857
    if (tNameFromString(&name, pVnode->config.dbname, T_NAME_ACCT | T_NAME_DB) != 0) {
×
1858
      vError("vgId:%d, failed to get name from string", TD_VID(pVnode));
×
1859
    }
1860

1861
    SStringBuilder sb = {0};
×
1862
    for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
×
1863
      char **key = (char **)taosArrayGet(tbNames, iReq);
×
1864
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
×
1865
      if (iReq < req.nReqs - 1) {
×
1866
        taosStringBuilderAppendChar(&sb, ',');
×
1867
      }
1868
      taosMemoryFreeClear(*key);
×
1869
    }
1870

1871
    size_t len = 0;
×
1872
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
×
1873

1874
    if (strlen(RPC_MSG_USER(pOriginRpc)) > 0) {
×
1875
      int64_t tse = taosGetTimestampMs();
×
1876
      double  duration = (double)(tse - tss);
×
1877
      duration = duration / 1000;
×
1878
      auditAddRecord(pOriginRpc, clusterId, "dropTable", name.dbname, "", keyJoined, len, duration, 0);
×
1879
    }
1880

1881
    taosStringBuilderDestroy(&sb);
×
1882
  }
1883

1884
_exit:
2,966,218✔
1885
  taosArrayDestroy(tbUids);
2,966,218✔
1886
  tDecoderClear(&decoder);
2,966,218✔
1887
  tEncodeSize(tEncodeSVDropTbBatchRsp, &rsp, pRsp->contLen, ret);
2,966,218✔
1888
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
2,966,218✔
1889
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
2,966,218✔
1890
  if (tEncodeSVDropTbBatchRsp(&encoder, &rsp) != 0) {
2,966,218✔
1891
    vError("vgId:%d, failed to encode drop table batch response", TD_VID(pVnode));
×
1892
  }
1893
  tEncoderClear(&encoder);
2,966,218✔
1894
  taosArrayDestroy(rsp.pArray);
2,966,218✔
1895
  taosArrayDestroy(tbNames);
2,966,218✔
1896
  return 0;
2,966,218✔
1897
}
1898

1899
static int32_t vnodeProcessCreateRsmaReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
79,844✔
1900
  int32_t         code = 0, lino = 0;
79,844✔
1901
  SVCreateRsmaReq req = {0};
79,844✔
1902
  TAOS_CHECK_EXIT(tDeserializeSVCreateRsmaReq(pReq, len, &req));
79,844✔
1903
  TAOS_CHECK_EXIT(metaCreateRsma(pVnode->pMeta, ver, &req));
79,844✔
1904
_exit:
79,844✔
1905
  pRsp->msgType = TDMT_VND_CREATE_RSMA_RSP;
79,844✔
1906
  pRsp->pCont = NULL;
79,844✔
1907
  pRsp->code = code;
78,342✔
1908
  pRsp->contLen = 0;
79,844✔
1909
  tFreeSVCreateRsmaReq(&req);
78,342✔
1910
  return code;
79,844✔
1911
}
1912

1913
static int32_t vnodeProcessDropRsmaReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
40,212✔
1914
  int32_t       code = 0, lino = 0;
40,212✔
1915
  SVDropRsmaReq req = {0};
40,212✔
1916
  TAOS_CHECK_EXIT(tDeserializeSVDropRsmaReq(pReq, len, &req));
40,212✔
1917
  TAOS_CHECK_EXIT(metaDropRsma(pVnode->pMeta, ver, &req));
40,212✔
1918
_exit:
40,212✔
1919
  pRsp->msgType = TDMT_VND_DROP_RSMA_RSP;
40,212✔
1920
  pRsp->pCont = NULL;
40,212✔
1921
  pRsp->code = code;
40,212✔
1922
  pRsp->contLen = 0;
40,212✔
1923
  return 0;
40,212✔
1924
}
1925

1926
static int32_t vnodeProcessAlterRsmaReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
24,612✔
1927
  int32_t         code = 0, lino = 0;
24,612✔
1928
  SVAlterRsmaReq req = {0};
24,612✔
1929
  TAOS_CHECK_EXIT(tDeserializeSVAlterRsmaReq(pReq, len, &req));
24,612✔
1930
  TAOS_CHECK_EXIT(metaAlterRsma(pVnode->pMeta, ver, &req));
24,612✔
1931
_exit:
24,612✔
1932
  pRsp->msgType = TDMT_VND_ALTER_RSMA_RSP;
24,612✔
1933
  pRsp->pCont = NULL;
24,612✔
1934
  pRsp->code = code;
24,612✔
1935
  pRsp->contLen = 0;
24,612✔
1936
  tFreeSVAlterRsmaReq(&req);
24,612✔
1937
  return code;
24,612✔
1938
}
1939

1940
#ifdef BUILD_NO_CALL
1941
static int32_t vnodeDebugPrintSingleSubmitMsg(SMeta *pMeta, SSubmitBlk *pBlock, SSubmitMsgIter *msgIter,
1942
                                              const char *tags) {
1943
  SSubmitBlkIter blkIter = {0};
1944
  STSchema      *pSchema = NULL;
1945
  tb_uid_t       suid = 0;
1946
  STSRow        *row = NULL;
1947
  int32_t        rv = -1;
1948

1949
  tInitSubmitBlkIter(msgIter, pBlock, &blkIter);
1950
  if (blkIter.row == NULL) return 0;
1951

1952
  int32_t code = metaGetTbTSchemaNotNull(pMeta, msgIter->suid, TD_ROW_SVER(blkIter.row), 1,
1953
                                         &pSchema);  // TODO: use the real schema
1954
  if (TSDB_CODE_SUCCESS != code) {
1955
    printf("%s:%d no valid schema\n", tags, __LINE__);
1956
    return code;
1957
  }
1958

1959
  suid = msgIter->suid;
1960
  rv = TD_ROW_SVER(blkIter.row);
1961

1962
  char __tags[128] = {0};
1963
  snprintf(__tags, 128, "%s: uid %" PRIi64 " ", tags, msgIter->uid);
1964
  while ((row = tGetSubmitBlkNext(&blkIter))) {
1965
    tdSRowPrint(row, pSchema, __tags);
1966
  }
1967

1968
  taosMemoryFreeClear(pSchema);
1969

1970
  return TSDB_CODE_SUCCESS;
1971
}
1972
#endif
1973
typedef struct SSubmitReqConvertCxt {
1974
  SSubmitMsgIter msgIter;
1975
  SSubmitBlk    *pBlock;
1976
  SSubmitBlkIter blkIter;
1977
  STSRow        *pRow;
1978
  STSRowIter     rowIter;
1979
  SSubmitTbData *pTbData;
1980
  STSchema      *pTbSchema;
1981
  SArray        *pColValues;
1982
} SSubmitReqConvertCxt;
1983

1984
static int32_t vnodeResetTableCxt(SMeta *pMeta, SSubmitReqConvertCxt *pCxt) {
×
1985
  taosMemoryFreeClear(pCxt->pTbSchema);
×
1986
  int32_t code = metaGetTbTSchemaNotNull(pMeta, pCxt->msgIter.suid > 0 ? pCxt->msgIter.suid : pCxt->msgIter.uid,
×
1987
                                         pCxt->msgIter.sversion, 1, &pCxt->pTbSchema);
1988
  if (TSDB_CODE_SUCCESS != code) {
×
1989
    return code;
×
1990
  }
1991
  tdSTSRowIterInit(&pCxt->rowIter, pCxt->pTbSchema);
×
1992

1993
  tDestroySubmitTbData(pCxt->pTbData, TSDB_MSG_FLG_ENCODE);
×
1994
  if (NULL == pCxt->pTbData) {
×
1995
    pCxt->pTbData = taosMemoryCalloc(1, sizeof(SSubmitTbData));
×
1996
    if (NULL == pCxt->pTbData) {
×
1997
      return terrno;
×
1998
    }
1999
  }
2000
  pCxt->pTbData->flags = 0;
×
2001
  pCxt->pTbData->suid = pCxt->msgIter.suid;
×
2002
  pCxt->pTbData->uid = pCxt->msgIter.uid;
×
2003
  pCxt->pTbData->sver = pCxt->msgIter.sversion;
×
2004
  pCxt->pTbData->pCreateTbReq = NULL;
×
2005
  pCxt->pTbData->aRowP = taosArrayInit(128, POINTER_BYTES);
×
2006
  if (NULL == pCxt->pTbData->aRowP) {
×
2007
    return terrno;
×
2008
  }
2009

2010
  taosArrayDestroy(pCxt->pColValues);
×
2011
  pCxt->pColValues = taosArrayInit(pCxt->pTbSchema->numOfCols, sizeof(SColVal));
×
2012
  if (NULL == pCxt->pColValues) {
×
2013
    return terrno;
×
2014
  }
2015
  for (int32_t i = 0; i < pCxt->pTbSchema->numOfCols; ++i) {
×
2016
    SColVal val = COL_VAL_NONE(pCxt->pTbSchema->columns[i].colId, pCxt->pTbSchema->columns[i].type);
×
2017
    if (taosArrayPush(pCxt->pColValues, &val) == NULL) {
×
2018
      return terrno;
×
2019
    }
2020
  }
2021

2022
  return TSDB_CODE_SUCCESS;
×
2023
}
2024

2025
static void vnodeDestroySubmitReqConvertCxt(SSubmitReqConvertCxt *pCxt) {
×
2026
  taosMemoryFreeClear(pCxt->pTbSchema);
×
2027
  tDestroySubmitTbData(pCxt->pTbData, TSDB_MSG_FLG_ENCODE);
×
2028
  taosMemoryFreeClear(pCxt->pTbData);
×
2029
  taosArrayDestroy(pCxt->pColValues);
×
2030
}
×
2031

2032
static int32_t vnodeCellValConvertToColVal(STColumn *pCol, SCellVal *pCellVal, SColVal *pColVal) {
×
2033
  if (tdValTypeIsNone(pCellVal->valType)) {
×
2034
    pColVal->flag = CV_FLAG_NONE;
×
2035
    return TSDB_CODE_SUCCESS;
×
2036
  }
2037

2038
  if (tdValTypeIsNull(pCellVal->valType)) {
×
2039
    pColVal->flag = CV_FLAG_NULL;
×
2040
    return TSDB_CODE_SUCCESS;
×
2041
  }
2042

2043
  if (IS_VAR_DATA_TYPE(pCol->type)) {
×
2044
    if (IS_STR_DATA_BLOB(pCol->type)) {
×
2045
      pColVal->value.nData = blobDataLen(pCellVal->val);
×
2046
      pColVal->value.pData = (uint8_t *)blobDataVal(pCellVal->val);
×
2047

2048
    } else {
2049
      pColVal->value.nData = varDataLen(pCellVal->val);
×
2050
      pColVal->value.pData = (uint8_t *)varDataVal(pCellVal->val);
×
2051
    }
2052
  } else if (TSDB_DATA_TYPE_FLOAT == pCol->type) {
×
2053
    float f = GET_FLOAT_VAL(pCellVal->val);
×
2054
    valueSetDatum(&pColVal->value, pCol->type, &f, sizeof(f));
×
2055
  } else if (TSDB_DATA_TYPE_DOUBLE == pCol->type) {
×
2056
    taosSetPInt64Aligned(&pColVal->value.val, (int64_t *)pCellVal->val);
×
2057
  } else {
2058
    valueSetDatum(&pColVal->value, pCol->type, pCellVal->val, tDataTypes[pCol->type].bytes);
×
2059
  }
2060

2061
  pColVal->flag = CV_FLAG_VALUE;
×
2062
  return TSDB_CODE_SUCCESS;
×
2063
}
2064

2065
static int32_t vnodeTSRowConvertToColValArray(SSubmitReqConvertCxt *pCxt) {
×
2066
  int32_t code = TSDB_CODE_SUCCESS;
×
2067
  tdSTSRowIterReset(&pCxt->rowIter, pCxt->pRow);
×
2068
  for (int32_t i = 0; TSDB_CODE_SUCCESS == code && i < pCxt->pTbSchema->numOfCols; ++i) {
×
2069
    STColumn *pCol = pCxt->pTbSchema->columns + i;
×
2070
    SCellVal  cellVal = {0};
×
2071
    if (!tdSTSRowIterFetch(&pCxt->rowIter, pCol->colId, pCol->type, &cellVal)) {
×
2072
      break;
×
2073
    }
2074
    code = vnodeCellValConvertToColVal(pCol, &cellVal, (SColVal *)taosArrayGet(pCxt->pColValues, i));
×
2075
  }
2076
  return code;
×
2077
}
2078

2079
static int32_t vnodeDecodeCreateTbReq(SSubmitReqConvertCxt *pCxt) {
×
2080
  if (pCxt->msgIter.schemaLen <= 0) {
×
2081
    return TSDB_CODE_SUCCESS;
×
2082
  }
2083

2084
  pCxt->pTbData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
×
2085
  if (NULL == pCxt->pTbData->pCreateTbReq) {
×
2086
    return terrno;
×
2087
  }
2088

2089
  SDecoder decoder = {0};
×
2090
  tDecoderInit(&decoder, (uint8_t *)pCxt->pBlock->data, pCxt->msgIter.schemaLen);
×
2091
  int32_t code = tDecodeSVCreateTbReq(&decoder, pCxt->pTbData->pCreateTbReq);
×
2092
  tDecoderClear(&decoder);
×
2093

2094
  return code;
×
2095
}
2096

2097
static int32_t vnodeSubmitReqConvertToSubmitReq2(SVnode *pVnode, SSubmitReq *pReq, SSubmitReq2 *pReq2) {
×
2098
  pReq2->aSubmitTbData = taosArrayInit(128, sizeof(SSubmitTbData));
×
2099
  if (NULL == pReq2->aSubmitTbData) {
×
2100
    return terrno;
×
2101
  }
2102

2103
  SSubmitReqConvertCxt cxt = {0};
×
2104

2105
  int32_t code = tInitSubmitMsgIter(pReq, &cxt.msgIter);
×
2106
  while (TSDB_CODE_SUCCESS == code) {
×
2107
    code = tGetSubmitMsgNext(&cxt.msgIter, &cxt.pBlock);
×
2108
    if (TSDB_CODE_SUCCESS == code) {
×
2109
      if (NULL == cxt.pBlock) {
×
2110
        break;
×
2111
      }
2112
      code = vnodeResetTableCxt(pVnode->pMeta, &cxt);
×
2113
    }
2114
    if (TSDB_CODE_SUCCESS == code) {
×
2115
      code = tInitSubmitBlkIter(&cxt.msgIter, cxt.pBlock, &cxt.blkIter);
×
2116
    }
2117
    if (TSDB_CODE_SUCCESS == code) {
×
2118
      code = vnodeDecodeCreateTbReq(&cxt);
×
2119
    }
2120
    while (TSDB_CODE_SUCCESS == code && (cxt.pRow = tGetSubmitBlkNext(&cxt.blkIter)) != NULL) {
×
2121
      code = vnodeTSRowConvertToColValArray(&cxt);
×
2122
      if (TSDB_CODE_SUCCESS == code) {
×
2123
        SRow **pNewRow = taosArrayReserve(cxt.pTbData->aRowP, 1);
×
2124

2125
        SRowBuildScanInfo sinfo = {0};
×
2126
        code = tRowBuild(cxt.pColValues, cxt.pTbSchema, pNewRow, &sinfo);
×
2127
      }
2128
    }
2129
    if (TSDB_CODE_SUCCESS == code) {
×
2130
      code = (NULL == taosArrayPush(pReq2->aSubmitTbData, cxt.pTbData) ? terrno : TSDB_CODE_SUCCESS);
×
2131
    }
2132
    if (TSDB_CODE_SUCCESS == code) {
×
2133
      taosMemoryFreeClear(cxt.pTbData);
×
2134
    }
2135
  }
2136

2137
  vnodeDestroySubmitReqConvertCxt(&cxt);
×
2138
  return code;
×
2139
}
2140

2141
static int32_t vnodeRebuildSubmitReqMsg(SSubmitReq2 *pSubmitReq, void **ppMsg) {
×
2142
  int32_t  code = TSDB_CODE_SUCCESS;
×
2143
  char    *pMsg = NULL;
×
2144
  uint32_t msglen = 0;
×
2145
  tEncodeSize(tEncodeSubmitReq, pSubmitReq, msglen, code);
×
2146
  if (TSDB_CODE_SUCCESS == code) {
×
2147
    pMsg = taosMemoryMalloc(msglen);
×
2148
    if (NULL == pMsg) {
×
2149
      code = terrno;
×
2150
    }
2151
  }
2152
  if (TSDB_CODE_SUCCESS == code) {
×
2153
    SEncoder encoder;
×
2154
    tEncoderInit(&encoder, (uint8_t *)pMsg, msglen);
×
2155
    code = tEncodeSubmitReq(&encoder, pSubmitReq);
×
2156
    tEncoderClear(&encoder);
×
2157
  }
2158
  if (TSDB_CODE_SUCCESS == code) {
×
2159
    *ppMsg = pMsg;
×
2160
  }
2161
  return code;
×
2162
}
2163

2164
static int32_t buildExistSubTalbeRsp(SVnode *pVnode, SSubmitTbData *pSubmitTbData, STableMetaRsp **ppRsp) {
121,476✔
2165
  int32_t code = 0;
121,476✔
2166
  int32_t lino = 0;
121,476✔
2167

2168
  SMetaEntry *pEntry = NULL;
121,476✔
2169
  code = metaFetchEntryByUid(pVnode->pMeta, pSubmitTbData->suid, &pEntry);
121,476✔
2170
  if (code) {
121,476✔
2171
    vError("vgId:%d, table uid:%" PRId64 " not exists, line:%d", TD_VID(pVnode), pSubmitTbData->uid, __LINE__);
×
2172
    TSDB_CHECK_CODE(code, lino, _exit);
×
2173
  }
2174
  if (pEntry->type != TSDB_SUPER_TABLE) {
121,476✔
2175
    vError("vgId:%d, table uid:%" PRId64 " exists, but is not super table, line:%d", TD_VID(pVnode), pSubmitTbData->uid,
×
2176
           __LINE__);
2177
    code = TSDB_CODE_STREAM_INSERT_SCHEMA_NOT_MATCH;
×
2178
    TSDB_CHECK_CODE(code, lino, _exit);
×
2179
  }
2180

2181
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
121,476✔
2182
  if (NULL == *ppRsp) {
121,476✔
2183
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2184
  }
2185
  (*ppRsp)->suid = pSubmitTbData->suid;
121,476✔
2186
  (*ppRsp)->tuid = pSubmitTbData->uid;
121,476✔
2187
  (*ppRsp)->sversion = pEntry->stbEntry.schemaRow.version;
121,476✔
2188
  (*ppRsp)->vgId = pVnode->config.vgId;
121,476✔
2189
  (*ppRsp)->numOfColumns = pEntry->stbEntry.schemaRow.nCols;
121,476✔
2190
  (*ppRsp)->numOfTags = pEntry->stbEntry.schemaTag.nCols;
121,476✔
2191
  (*ppRsp)->pSchemas =
121,476✔
2192
      taosMemoryCalloc(pEntry->stbEntry.schemaRow.nCols + pEntry->stbEntry.schemaTag.nCols, sizeof(SSchema));
121,476✔
2193
  if (NULL == (*ppRsp)->pSchemas) {
121,476✔
2194
    taosMemoryFree(*ppRsp);
×
2195
    *ppRsp = NULL;
×
2196
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2197
  }
2198
  memcpy((*ppRsp)->pSchemas, pEntry->stbEntry.schemaRow.pSchema, pEntry->stbEntry.schemaRow.nCols * sizeof(SSchema));
121,476✔
2199
  memcpy((*ppRsp)->pSchemas + pEntry->stbEntry.schemaRow.nCols, pEntry->stbEntry.schemaTag.pSchema,
121,476✔
2200
         pEntry->stbEntry.schemaTag.nCols * sizeof(SSchema));
121,476✔
2201
  if (pEntry->pExtSchemas != NULL) {
121,476✔
2202
    (*ppRsp)->pSchemaExt = taosMemoryCalloc(pEntry->colCmpr.nCols, sizeof(SExtSchema));
×
2203
    if (NULL == (*ppRsp)->pSchemaExt) {
×
2204
      taosMemoryFree((*ppRsp)->pSchemas);
×
2205
      taosMemoryFree(*ppRsp);
×
2206
      *ppRsp = NULL;
×
2207
      TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2208
    }
2209
    memcpy((*ppRsp)->pSchemaExt, pEntry->pExtSchemas, pEntry->colCmpr.nCols * sizeof(SExtSchema));
×
2210
  }
2211

2212
  if (pEntry->stbEntry.schemaRow.version != pSubmitTbData->sver) {
121,476✔
2213
    TSDB_CHECK_CODE(code = TSDB_CODE_TDB_TABLE_ALREADY_EXIST, lino, _exit);
952✔
2214
  }
2215
_exit:
121,476✔
2216
  metaFetchEntryFree(&pEntry);
121,476✔
2217
  if (code != TSDB_CODE_SUCCESS) {
121,476✔
2218
    vError("vgId:%d, failed to build exist sub table response, code:0x%0x, line:%d", TD_VID(pVnode), code, lino);
952✔
2219
  }
2220
  return code;
121,476✔
2221
}
2222

2223
static int32_t buildExistNormalTalbeRsp(SVnode *pVnode, int64_t uid, STableMetaRsp **ppRsp) {
57,382✔
2224
  int32_t code = 0;
57,382✔
2225
  int32_t lino = 0;
57,382✔
2226

2227
  SMetaEntry *pEntry = NULL;
57,382✔
2228
  code = metaFetchEntryByUid(pVnode->pMeta, uid, &pEntry);
57,382✔
2229
  if (code) {
57,382✔
2230
    vError("vgId:%d, table uid:%" PRId64 " not exists, line:%d", TD_VID(pVnode), uid, __LINE__);
×
2231
    TSDB_CHECK_CODE(code, lino, _exit);
×
2232
  }
2233
  if (pEntry->type != TSDB_NORMAL_TABLE) {
57,382✔
2234
    vError("vgId:%d, table uid:%" PRId64 " exists, but is not normal table, line:%d", TD_VID(pVnode), uid, __LINE__);
×
2235
    code = TSDB_CODE_STREAM_INSERT_SCHEMA_NOT_MATCH;
×
2236
    TSDB_CHECK_CODE(code, lino, _exit);
×
2237
  }
2238

2239
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
57,382✔
2240
  if (NULL == *ppRsp) {
57,382✔
2241
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2242
  }
2243

2244
  (*ppRsp)->tuid = pEntry->uid;
57,382✔
2245
  (*ppRsp)->vgId = pVnode->config.vgId;
57,382✔
2246
  (*ppRsp)->sversion = pEntry->ntbEntry.schemaRow.version;
57,382✔
2247
  (*ppRsp)->numOfColumns = pEntry->ntbEntry.schemaRow.nCols;
57,382✔
2248
  (*ppRsp)->pSchemas = taosMemoryCalloc(pEntry->ntbEntry.schemaRow.nCols, sizeof(SSchema));
57,382✔
2249
  if (NULL == (*ppRsp)->pSchemas) {
57,382✔
2250
    taosMemoryFree(*ppRsp);
×
2251
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2252
  }
2253
  memcpy((*ppRsp)->pSchemas, pEntry->ntbEntry.schemaRow.pSchema, pEntry->ntbEntry.schemaRow.nCols * sizeof(SSchema));
57,382✔
2254
  if (pEntry->pExtSchemas != NULL) {
57,382✔
2255
    (*ppRsp)->pSchemaExt = taosMemoryCalloc(pEntry->ntbEntry.schemaRow.nCols, sizeof(SSchemaExt));
×
2256
    if (NULL == (*ppRsp)->pSchemaExt) {
×
2257
      taosMemoryFree((*ppRsp)->pSchemas);
×
2258
      taosMemoryFree(*ppRsp);
×
2259
      TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2260
    }
2261
    memcpy((*ppRsp)->pSchemaExt, pEntry->pExtSchemas, pEntry->ntbEntry.schemaRow.nCols * sizeof(SSchemaExt));
×
2262
  }
2263

2264
_exit:
57,382✔
2265
  metaFetchEntryFree(&pEntry);
57,382✔
2266
  if (code != TSDB_CODE_SUCCESS) {
57,382✔
2267
    vError("vgId:%d, failed to build exist normal table response, code:%d, line:%d", TD_VID(pVnode), code, lino);
×
2268
  }
2269
  return code;
57,382✔
2270
}
2271

2272
static int32_t buildExistTableInStreamRsp(SVnode *pVnode, SSubmitTbData *pSubmitTbData, STableMetaRsp **ppRsp) {
606,486✔
2273
  if (pSubmitTbData->pCreateTbReq->flags & TD_CREATE_NORMAL_TB_IN_STREAM) {
606,486✔
2274
    int32_t code = buildExistNormalTalbeRsp(pVnode, pSubmitTbData->uid, ppRsp);
57,382✔
2275
    if (code) {
57,382✔
2276
      vError("vgId:%d, table uid:%" PRId64 " not exists, line:%d", TD_VID(pVnode), pSubmitTbData->uid, __LINE__);
×
2277
      return code;
×
2278
    }
2279
    return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
57,382✔
2280
  } else if (pSubmitTbData->pCreateTbReq->flags & TD_CREATE_SUB_TB_IN_STREAM) {
549,010✔
2281
    return buildExistSubTalbeRsp(pVnode, pSubmitTbData, ppRsp);
121,476✔
2282
  }
2283
  return TSDB_CODE_SUCCESS;
427,628✔
2284
}
2285

2286
static int32_t vnodeHandleAutoCreateTable(SVnode      *pVnode,    // vnode
1,303,460,234✔
2287
                                          int64_t      version,   // version
2288
                                          SSubmitReq2 *pRequest,  // request
2289
                                          SSubmitRsp2 *pResponse  // response
2290
) {
2291
  int32_t code = TSDB_CODE_SUCCESS;
1,303,460,234✔
2292
  int32_t numTbData = taosArrayGetSize(pRequest->aSubmitTbData);
1,303,460,234✔
2293
  SArray *newTbUids = NULL;
1,303,460,140✔
2294

2295
  for (int32_t i = 0; i < numTbData; ++i) {
2,147,483,647✔
2296
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
1,355,668,678✔
2297

2298
    if (pTbData->pCreateTbReq == NULL) {
1,355,670,588✔
2299
      continue;
1,323,955,890✔
2300
    }
2301

2302
    pTbData->uid = pTbData->pCreateTbReq->uid;
31,714,698✔
2303

2304
    // Alloc necessary resources
2305
    if (pResponse->aCreateTbRsp == NULL) {
31,714,886✔
2306
      pResponse->aCreateTbRsp = taosArrayInit(numTbData, sizeof(SVCreateTbRsp));
25,906,310✔
2307
      if (pResponse->aCreateTbRsp == NULL) {
25,906,310✔
2308
        code = terrno;
×
2309
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2310
               tstrerror(code), version);
2311
        taosArrayDestroy(newTbUids);
×
2312
        return code;
×
2313
      }
2314
    }
2315

2316
    // Do create table
2317
    vDebug("vgId:%d start to handle auto create table, version:%" PRId64, TD_VID(pVnode), version);
31,714,886✔
2318

2319
    SVCreateTbRsp *pCreateTbRsp = taosArrayReserve(pResponse->aCreateTbRsp, 1);
31,714,980✔
2320
    code = metaCreateTable2(pVnode->pMeta, version, pTbData->pCreateTbReq, &pCreateTbRsp->pMeta);
31,714,886✔
2321
    if (code == TSDB_CODE_SUCCESS) {
31,714,698✔
2322
      // Allocate necessary resources
2323
      if (newTbUids == NULL) {
30,984,828✔
2324
        newTbUids = taosArrayInit(numTbData, sizeof(int64_t));
25,300,000✔
2325
        if (newTbUids == NULL) {
25,300,000✔
2326
          code = terrno;
×
2327
          vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2328
                 tstrerror(code), version);
2329
          return code;
×
2330
        }
2331
      }
2332

2333
      if (taosArrayPush(newTbUids, &pTbData->uid) == NULL) {
61,969,656✔
2334
        code = terrno;
×
2335
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2336
               tstrerror(code), version);
2337
        taosArrayDestroy(newTbUids);
×
2338
        return code;
×
2339
      }
2340

2341
      if (pCreateTbRsp->pMeta) {
30,984,828✔
2342
        vnodeUpdateMetaRsp(pVnode, pCreateTbRsp->pMeta);
30,984,828✔
2343
      }
2344
    } else if (code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
729,870✔
2345
      code = terrno = 0;
727,742✔
2346
      pTbData->uid = pTbData->pCreateTbReq->uid;  // update uid if table exist for using below
727,930✔
2347

2348
      // stream: get sver from meta, write to pCreateTbRsp, and need to check crateTbReq is same as meta.
2349
      if (i == 0) {
727,742✔
2350
        // In the streaming scenario, multiple grouped req requests will only operate on the same write table, and
2351
        // only the first one needs to be processed.
2352
        code = buildExistTableInStreamRsp(pVnode, pTbData, &pCreateTbRsp->pMeta);
606,392✔
2353
        if (code) {
606,580✔
2354
          vInfo("vgId:%d failed to create table in stream:%s, code(0x%0x):%s", TD_VID(pVnode),
58,334✔
2355
                pTbData->pCreateTbReq->name, code, tstrerror(code));
2356
          taosArrayDestroy(newTbUids);
58,334✔
2357
          return code;
58,334✔
2358
        }
2359
      }
2360
    } else {
2361
      code = terrno;
2,128✔
2362
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
2,128✔
2363
             tstrerror(code), version);
2364
      taosArrayDestroy(newTbUids);
2,128✔
2365
      return code;
2,128✔
2366
    }
2367
  }
2368

2369
  // Update the affected table uid list
2370
  if (taosArrayGetSize(newTbUids) > 0) {
1,303,399,772✔
2371
    vDebug("vgId:%d, add %d table into query table list in handling submit", TD_VID(pVnode),
25,300,000✔
2372
           (int32_t)taosArrayGetSize(newTbUids));
2373
    if (tqAddTbUidList(pVnode->pTq, newTbUids) != 0) {
25,300,000✔
2374
      vError("vgId:%d, failed to update tbUid list", TD_VID(pVnode));
×
2375
    }
2376
  }
2377

2378
  vDebug("vgId:%d, handle auto create table done, version:%" PRId64, TD_VID(pVnode), version);
1,303,399,608✔
2379

2380
  taosArrayDestroy(newTbUids);
1,303,399,772✔
2381
  return code;
1,303,401,588✔
2382
}
2383

2384
static void addExistTableInfoIntoRes(SVnode *pVnode, SSubmitReq2 *pRequest, SSubmitRsp2 *pResponse,
952✔
2385
                                     SSubmitTbData *pTbData, int32_t numTbData) {
2386
  int32_t code = 0;
952✔
2387
  int32_t lino = 0;
952✔
2388
  if ((pTbData->flags & SUBMIT_REQ_SCHEMA_RES) == 0) {
952✔
2389
    return;
×
2390
  }
2391
  if (pResponse->aCreateTbRsp) {  // If aSubmitTbData is not NULL, it means that the request is a create table request,
952✔
2392
                                  // so table info has exitst and we do not need to add again.
2393
    return;
952✔
2394
  }
2395
  pResponse->aCreateTbRsp = taosArrayInit(numTbData, sizeof(SVCreateTbRsp));
×
2396
  if (pResponse->aCreateTbRsp == NULL) {
×
2397
    code = terrno;
×
2398
    TSDB_CHECK_CODE(code, lino, _exit);
×
2399
  }
2400
  SVCreateTbRsp *pCreateTbRsp = taosArrayReserve(pResponse->aCreateTbRsp, 1);
×
2401
  if (pCreateTbRsp == NULL) {
×
2402
    code = terrno;
×
2403
    TSDB_CHECK_CODE(code, lino, _exit);
×
2404
  }
2405
  if (pTbData->suid == 0) {
×
2406
    code = buildExistNormalTalbeRsp(pVnode, pTbData->uid, &pCreateTbRsp->pMeta);
×
2407
    if (code) {
×
2408
      vError("vgId:%d, table uid:%" PRId64 " not exists, line:%d", TD_VID(pVnode), pTbData->uid, __LINE__);
×
2409
    }
2410
  } else {
2411
    code = buildExistSubTalbeRsp(pVnode, pTbData, &pCreateTbRsp->pMeta);
×
2412
  }
2413

2414
  TSDB_CHECK_CODE(code, lino, _exit);
×
2415
_exit:
×
2416
  if (code != TSDB_CODE_SUCCESS) {
×
2417
    vError("vgId:%d, failed to add exist table info into response, code:0x%0x, line:%d", TD_VID(pVnode), code, lino);
×
2418
  }
2419
  return;
×
2420
}
2421

2422
static int32_t vnodeHandleDataWrite(SVnode *pVnode, int64_t version, SSubmitReq2 *pRequest, SSubmitRsp2 *pResponse) {
1,303,398,634✔
2423
  int32_t code = TSDB_CODE_SUCCESS;
1,303,398,634✔
2424
  int32_t numTbData = taosArrayGetSize(pRequest->aSubmitTbData);
1,303,398,634✔
2425
  int8_t  hasBlob = 0;
1,303,398,888✔
2426

2427
  // Scan submit data
2428
  for (int32_t i = 0; i < numTbData; ++i) {
2,147,483,647✔
2429
    SMetaInfo      info = {0};
1,355,607,438✔
2430
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
1,355,608,402✔
2431

2432
    if (pTbData->flags & SUBMIT_REQ_WITH_BLOB) {
1,355,607,774✔
2433
      hasBlob = 1;
64,300✔
2434
    }
2435
    if (pTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
1,355,607,774✔
2436
      continue;  // skip column data format
13,737,354✔
2437
    }
2438
    if (pTbData->flags & SUBMIT_REQ_ONLY_CREATE_TABLE) {
1,341,869,988✔
2439
      continue;  // skip only crate table request
5,185,768✔
2440
    }
2441

2442
    code = metaGetInfo(pVnode->pMeta, pTbData->uid, &info, NULL);
1,336,684,124✔
2443
    if (code) {
1,336,685,342✔
2444
      code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
1,368✔
2445
      vWarn("vgId:%d, error occurred at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __FILE__,
1,368✔
2446
            __LINE__, tstrerror(code), version, pTbData->uid);
2447
      return code;
1,368✔
2448
    }
2449

2450
    if (info.suid != pTbData->suid) {
1,336,683,974✔
2451
      code = TSDB_CODE_INVALID_MSG;
×
2452
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64 " suid:%" PRId64
×
2453
             " info.suid:%" PRId64,
2454
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), version, pTbData->uid, pTbData->suid,
2455
             info.suid);
2456
      return code;
×
2457
    }
2458

2459
    if (info.suid) {
1,336,680,212✔
2460
      code = metaGetInfo(pVnode->pMeta, info.suid, &info, NULL);
1,040,183,090✔
2461
      if (code) {
1,040,188,146✔
2462
        code = TSDB_CODE_INTERNAL_ERROR;
×
2463
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " suid:%" PRId64, TD_VID(pVnode), __func__,
×
2464
               __FILE__, __LINE__, tstrerror(code), version, info.suid);
2465
        return code;
×
2466
      }
2467
    }
2468

2469
    if (pTbData->sver != info.skmVer) {
1,336,685,268✔
2470
      code = TSDB_CODE_TDB_INVALID_TABLE_SCHEMA_VER;
952✔
2471
      addExistTableInfoIntoRes(pVnode, pRequest, pResponse, pTbData, numTbData);
952✔
2472
      vDebug("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64
952✔
2473
             " sver:%d"
2474
             " info.skmVer:%d",
2475
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), version, pTbData->uid, pTbData->sver,
2476
             info.skmVer);
2477
      return code;
952✔
2478
    }
2479
  }
2480

2481
  // Do write data
2482
  vDebug("vgId:%d start to handle data write, version:%" PRId64, TD_VID(pVnode), version);
1,303,394,092✔
2483

2484
  for (int32_t i = 0; i < numTbData; ++i) {
2,147,483,647✔
2485
    int32_t        affectedRows = 0;
1,355,601,392✔
2486
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
1,355,602,584✔
2487

2488
    if (pTbData->flags & SUBMIT_REQ_ONLY_CREATE_TABLE) {
1,355,605,558✔
2489
      continue;
5,185,768✔
2490
    }
2491

2492
    if (hasBlob) {
1,350,419,550✔
2493
      code = vnodeSubmitBlobData(pVnode, pTbData);
64,300✔
2494
      if (code) {
64,300✔
2495
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2496
               __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2497
        return code;
×
2498
      }
2499
    }
2500

2501
    code = tsdbInsertTableData(pVnode->pTsdb, version, pTbData, &affectedRows);
1,350,419,550✔
2502
    if (code) {
1,350,375,200✔
2503
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2504
             __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2505
      return code;
×
2506
    }
2507

2508
    code = metaUpdateChangeTimeWithLock(pVnode->pMeta, pTbData->uid, pTbData->ctimeMs);
1,350,375,200✔
2509
    if (code) {
1,350,363,338✔
2510
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2511
             __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2512
      return code;
×
2513
    }
2514
    pResponse->affectedRows += affectedRows;
1,350,363,338✔
2515
  }
2516

2517
  vDebug("vgId:%d, handle data write done, version:%" PRId64 ", affectedRows:%d", TD_VID(pVnode), version,
1,303,379,298✔
2518
         pResponse->affectedRows);
2519
  return code;
1,303,398,702✔
2520
}
2521

2522
static int32_t vnodeScanColumnData(SVnode *pVnode, SSubmitTbData *pTbData, TSKEY minKey, TSKEY maxKey) {
13,735,320✔
2523
  int32_t code = 0;
13,735,320✔
2524

2525
  int32_t   numCols = taosArrayGetSize(pTbData->aCol);
13,735,320✔
2526
  SColData *aColData = (SColData *)TARRAY_DATA(pTbData->aCol);
13,736,684✔
2527

2528
  if (numCols <= 0) {
13,733,968✔
2529
    code = TSDB_CODE_INVALID_MSG;
×
2530
    vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " numCols:%d", TD_VID(pVnode), __func__,
×
2531
           __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, numCols);
2532
    return code;
×
2533
  }
2534

2535
  if (aColData[0].cid != PRIMARYKEY_TIMESTAMP_COL_ID || aColData[0].type != TSDB_DATA_TYPE_TIMESTAMP ||
13,733,968✔
2536
      aColData[0].nVal <= 0) {
13,733,298✔
2537
    code = TSDB_CODE_INVALID_MSG;
3,350✔
2538
    vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64
3,350✔
2539
           " first column is not primary key timestamp, cid:%d type:%d nVal:%d",
2540
           TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, aColData[0].cid,
2541
           aColData[0].type, aColData[0].nVal);
2542
    return code;
×
2543
  }
2544

2545
  for (int32_t i = 1; i < numCols; ++i) {
42,271,526✔
2546
    if (aColData[i].nVal != aColData[0].nVal) {
28,539,556✔
2547
      code = TSDB_CODE_INVALID_MSG;
×
2548
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64
×
2549
             " column cid:%d type:%d nVal:%d is not equal to primary key timestamp nVal:%d",
2550
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid,
2551
             aColData[i].cid, aColData[i].type, aColData[i].nVal, aColData[0].nVal);
2552
      return code;
×
2553
    }
2554
  }
2555

2556
  SRowKey *pLastKey = NULL;
13,731,970✔
2557
  SRowKey  lastKey = {0};
13,731,970✔
2558
  for (int32_t i = 0; i < aColData[0].nVal; ++i) {
2,147,483,647✔
2559
    SRowKey key = {0};
2,147,483,647✔
2560

2561
    tColDataArrGetRowKey(aColData, numCols, i, &key);
2,147,483,647✔
2562

2563
    if (key.ts < minKey || key.ts > maxKey) {
2,147,483,647✔
2564
      code = TSDB_CODE_INVALID_MSG;
×
2565
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] key:%" PRId64
×
2566
             " is out of range [%" PRId64 ", %" PRId64 "]",
2567
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, i, key.ts,
2568
             minKey, maxKey);
2569
      return code;
×
2570
    }
2571

2572
    if (pLastKey && tRowKeyCompare(pLastKey, &key) >= 0) {
2,147,483,647✔
2573
      code = TSDB_CODE_INVALID_MSG;
×
2574
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] key:%" PRId64
×
2575
             " is not in order, lastKey:%" PRId64,
2576
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, i, key.ts,
2577
             pLastKey->ts);
2578
      return code;
×
2579
    } else if (pLastKey == NULL) {
2,147,483,647✔
2580
      pLastKey = &lastKey;
13,731,958✔
2581
    }
2582

2583
    *pLastKey = key;
2,147,483,647✔
2584
  }
2585

2586
  return code;
13,737,354✔
2587
}
2588

2589
static int32_t vnodeScanSubmitRowData(SVnode *pVnode, SSubmitTbData *pTbData, TSKEY minKey, TSKEY maxKey) {
1,341,877,436✔
2590
  int32_t code = 0;
1,341,877,436✔
2591

2592
  int32_t numRows = taosArrayGetSize(pTbData->aRowP);
1,341,877,436✔
2593
  SRow  **aRow = (SRow **)TARRAY_DATA(pTbData->aRowP);
1,341,883,388✔
2594

2595
  if (numRows <= 0 && (pTbData->flags & SUBMIT_REQ_ONLY_CREATE_TABLE) == 0) {
1,341,882,410✔
2596
    code = TSDB_CODE_INVALID_MSG;
×
2597
    vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " numRows:%d", TD_VID(pVnode), __func__,
×
2598
           __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, numRows);
2599
    return code;
×
2600
  }
2601

2602
  SRowKey *pLastKey = NULL;
1,341,882,410✔
2603
  SRowKey  lastKey = {0};
1,341,882,410✔
2604
  for (int32_t i = 0; i < numRows; ++i) {
2,147,483,647✔
2605
    SRow *pRow = aRow[i];
2,147,483,647✔
2606
    if (pRow->sver != pTbData->sver) {
2,147,483,647✔
2607
      code = TSDB_CODE_INVALID_MSG;
×
2608
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] sver:%d pTbData->sver:%d",
×
2609
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, i, pRow->sver,
2610
             pTbData->sver);
2611
      return code;
×
2612
    }
2613

2614
    SRowKey key = {0};
2,147,483,647✔
2615
    tRowGetKey(pRow, &key);
2,147,483,647✔
2616
    if (key.ts < minKey || key.ts > maxKey) {
2,147,483,647✔
2617
      code = TSDB_CODE_INVALID_MSG;
×
2618
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] key:%" PRId64
×
2619
             " is out of range [%" PRId64 ", %" PRId64 "]",
2620
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, i, key.ts,
2621
             minKey, maxKey);
2622
      return code;
×
2623
    }
2624

2625
    if (pLastKey && tRowKeyCompare(pLastKey, &key) >= 0) {
2,147,483,647✔
2626
      code = TSDB_CODE_INVALID_MSG;
×
2627
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] key:%" PRId64
×
2628
             " is not in order, lastKey:%" PRId64,
2629
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, i, key.ts,
2630
             pLastKey->ts);
2631
      return code;
×
2632
    } else if (pLastKey == NULL) {
2,147,483,647✔
2633
      pLastKey = &lastKey;
1,336,671,026✔
2634
    }
2635

2636
    *pLastKey = key;
2,147,483,647✔
2637
  }
2638

2639
  return code;
1,334,947,662✔
2640
}
2641

2642
static int32_t vnodeScanSubmitReq(SVnode *pVnode, int64_t version, SSubmitReq2 *pRequest, SSubmitRsp2 *pResponse) {
1,303,422,244✔
2643
  int32_t code = TSDB_CODE_SUCCESS;
1,303,422,244✔
2644
  int32_t numTbData = taosArrayGetSize(pRequest->aSubmitTbData);
1,303,422,244✔
2645

2646
  TSKEY now = taosGetTimestamp(pVnode->config.tsdbCfg.precision);
1,303,432,506✔
2647
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * pVnode->config.tsdbCfg.keep2;
1,303,422,864✔
2648
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
1,303,438,808✔
2649
  for (int32_t i = 0; i < numTbData; i++) {
2,147,483,647✔
2650
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
1,355,627,162✔
2651

2652
    if (pTbData->pCreateTbReq && pTbData->pCreateTbReq->uid == 0) {
1,355,639,136✔
2653
      code = TSDB_CODE_INVALID_MSG;
×
2654
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2655
             tstrerror(code), version);
2656
      return code;
×
2657
    }
2658

2659
    if (pTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
1,355,643,576✔
2660
      code = vnodeScanColumnData(pVnode, pTbData, minKey, maxKey);
13,735,990✔
2661
      if (code) {
13,737,354✔
2662
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2663
               __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2664
        return code;
×
2665
      }
2666
    } else {
2667
      code = vnodeScanSubmitRowData(pVnode, pTbData, minKey, maxKey);
1,341,878,996✔
2668
      if (code) {
1,341,884,464✔
2669
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
16,796✔
2670
               __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2671
        return code;
×
2672
      }
2673
    }
2674
  }
2675

2676
  return code;
1,303,399,080✔
2677
}
2678

2679
static int32_t vnodeDecodeAuditRecord(const SJson *pJson, void *pObj) {
1,650✔
2680
  SAuditRecord *record = (SAuditRecord *)pObj;
1,650✔
2681
  int32_t       code = 0;
1,650✔
2682

2683
  tjsonGetNumberValue(pJson, "timestamp", record->curTime, code);
1,650✔
2684
  if (code < 0) return TSDB_CODE_INVALID_JSON_FORMAT;
1,650✔
2685
  code = tjsonGetStringValue2(pJson, "cluster_id", record->strClusterId, TSDB_CLUSTER_ID_LEN);
1,650✔
2686
  if (code < 0) return TSDB_CODE_INVALID_JSON_FORMAT;
1,650✔
2687
  code = tjsonGetStringValue2(pJson, "client_add", record->clientAddress, AUDIT_CLIENT_ADD_LEN);
1,650✔
2688
  if (code < 0) return TSDB_CODE_INVALID_JSON_FORMAT;
1,650✔
2689
  code = tjsonGetStringValue2(pJson, "user", record->user, TSDB_USER_LEN);
1,650✔
2690
  if (code < 0) return TSDB_CODE_INVALID_JSON_FORMAT;
1,650✔
2691
  code = tjsonGetStringValue2(pJson, "operation", record->operation, AUDIT_OPERATION_LEN);
1,650✔
2692
  if (code < 0) return TSDB_CODE_INVALID_JSON_FORMAT;
1,650✔
2693
  code = tjsonGetStringValue2(pJson, "db", record->target1, TSDB_DB_NAME_LEN);
1,650✔
2694
  if (code < 0) return TSDB_CODE_INVALID_JSON_FORMAT;
1,650✔
2695
  code = tjsonGetStringValue2(pJson, "resource", record->target2, TSDB_STREAM_NAME_LEN);
1,650✔
2696
  if (code < 0) return TSDB_CODE_INVALID_JSON_FORMAT;
1,650✔
2697
  record->detail = (char *)tjsonGetStringPointer(pJson, "details");
1,650✔
2698
  if (record->detail == NULL) return TSDB_CODE_INVALID_JSON_FORMAT;
1,650✔
2699
  code = tjsonGetDoubleValue(pJson, "duration", &record->duration);
1,650✔
2700
  if (code < 0) return TSDB_CODE_INVALID_JSON_FORMAT;
1,650✔
2701
  tjsonGetNumberValue(pJson, "affected_rows", record->affectedRows, code);
1,650✔
2702
  if (code < 0) return TSDB_CODE_INVALID_JSON_FORMAT;
1,650✔
2703

2704
  return 0;
1,650✔
2705
}
2706

2707
static int32_t vnodeBuildCreateTbReq(SVCreateTbReq *pTbReq, const char *tname, STag *pTag, int64_t suid,
150✔
2708
                                     const char *sname, SArray *tagName, uint8_t tagNum, int32_t ttl) {
2709
  int32_t code = TSDB_CODE_SUCCESS;
150✔
2710
  int32_t lino = 0;
150✔
2711

2712
  if (tname == NULL || sname == NULL) {
150✔
2713
    return TSDB_CODE_INVALID_PARA;
×
2714
  }
2715

2716
  pTbReq->name = taosStrdup(tname);
150✔
2717
  if (!pTbReq->name) {
150✔
2718
    code = terrno;
×
2719
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
2720
  }
2721

2722
  pTbReq->ctb.stbName = taosStrdup(sname);
150✔
2723
  if (!pTbReq->ctb.stbName) {
150✔
2724
    code = terrno;
×
2725
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
2726
  }
2727

2728
  pTbReq->type = TD_CHILD_TABLE;
150✔
2729
  pTbReq->ctb.suid = suid;
150✔
2730
  pTbReq->ctb.tagNum = tagNum;
150✔
2731
  pTbReq->ttl = ttl;
150✔
2732
  pTbReq->commentLen = -1;
150✔
2733
  pTbReq->ctb.tagName = tagName;
150✔
2734
  pTbReq->ctb.pTag = (uint8_t *)pTag;
150✔
2735

2736
  return code;
150✔
2737
_exit:
×
2738
  if (code != 0) {
×
2739
    if (pTbReq->name != NULL) {
×
2740
      taosMemoryFreeClear(pTbReq->name);
×
2741
    }
2742
    if (pTbReq->ctb.stbName != NULL) {
×
2743
      taosMemoryFreeClear(pTbReq->ctb.stbName);
×
2744
    }
2745
    vError("failed to build create tb at %d since %s", lino, tstrerror(code));
×
2746
  }
2747
  return code;
×
2748
}
2749

2750
static int32_t vnodePrepareCreateTb(SVCreateTbReq *pTbReq, char *tbName, int64_t suid, SSchemaWrapper *pTagSchema,
150✔
2751
                                    SAuditRecord *record) {
2752
  int32_t code = 0;
150✔
2753
  int32_t lino = 0;
150✔
2754

2755
  SArray *TagNames = NULL;
150✔
2756
  SArray *pTagVals = NULL;
150✔
2757
  STag   *pTag = NULL;
150✔
2758

2759
  SSchema *tSchema = &pTagSchema->pSchema[0];
150✔
2760
  vTrace("schema name:%s", tSchema->name);
150✔
2761

2762
  TagNames = taosArrayInit(1, TSDB_COL_NAME_LEN);
150✔
2763
  if (!TagNames) {
150✔
2764
    code = terrno;
×
2765
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
2766
  }
2767

2768
  if (NULL == taosArrayPush(TagNames, tSchema->name)) {
300✔
2769
    code = terrno;
×
2770
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
2771
  }
2772

2773
  STagVal tv = (STagVal){.cid = tSchema->colId,
150✔
2774
                         .type = tSchema->type,
150✔
2775
                         .nData = strlen(record->strClusterId),
150✔
2776
                         .pData = record->strClusterId};  // address copy, no value
150✔
2777
  if ((pTagVals = taosArrayInit(1, sizeof(STagVal))) == NULL) {
150✔
2778
    code = terrno;
×
2779
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
2780
  }
2781
  if (NULL == taosArrayPush(pTagVals, &tv)) {
150✔
2782
    code = terrno;
×
2783
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
2784
  }
2785

2786
  // version hardcode to 1 according to other module
2787
  code = tTagNew(pTagVals, 1, false, &pTag);
150✔
2788
  if (code != TSDB_CODE_SUCCESS) {
150✔
2789
    vError("failed to create tag, error:%s", tstrerror(code));
×
2790
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
2791
  }
2792

2793
  code = vnodeBuildCreateTbReq(pTbReq, tbName, pTag, suid, AUDIT_STABLE_NAME, TagNames, pTagSchema->nCols,
150✔
2794
                               TSDB_DEFAULT_TABLE_TTL);
2795
_exit:
150✔
2796
  if (code != 0) {
150✔
2797
    vError("failed to prepare create tb at %d since %s", lino, tstrerror(code));
×
2798
    if (TagNames != NULL) {
×
2799
      taosArrayDestroy(TagNames);
×
2800
      TagNames = NULL;
×
2801
      pTbReq->ctb.tagName = NULL;
×
2802
    }
2803
    if (pTag != NULL) {
×
2804
      tTagFree(pTag);
×
2805
      pTag = NULL;
×
2806
      pTbReq->ctb.pTag = NULL;
×
2807
    }
2808
  }
2809

2810
  if (pTagVals != NULL) {
150✔
2811
    taosArrayDestroy(pTagVals);
150✔
2812
    pTagVals = NULL;
150✔
2813
  }
2814

2815
  return code;
150✔
2816
}
2817

2818
static SArray *vnodePrepareRow(SVnode *pVnode, STSchema *pSchema, SAuditRecord *record) {
1,650✔
2819
  int32_t code = 0;
1,650✔
2820
  int32_t lino = 0;
1,650✔
2821

2822
  SArray *aRows = NULL;
1,650✔
2823
  SArray *pVals = NULL;
1,650✔
2824
  SRow   *pRow = NULL;
1,650✔
2825

2826
  if (!(aRows = taosArrayInit(1, sizeof(SRow *)))) {
1,650✔
2827
    code = terrno;
×
2828
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
2829
  }
2830

2831
  if ((pVals = taosArrayInit(1, sizeof(SColVal))) == NULL) {
1,650✔
2832
    code = terrno;
×
2833
    goto _exit;
×
2834
  }
2835

2836
  for (int32_t k = 0; k < pSchema->numOfCols; ++k) {
16,500✔
2837
    const STColumn *pCol = &pSchema->columns[k];
14,850✔
2838
    vTrace("vgId:%d, schema column id:%d, type:%d", TD_VID(pVnode), pCol->colId, pCol->type);
14,850✔
2839

2840
    // colId is consistent with audit_columns in mndStb.c
2841
    void *data = NULL;
14,850✔
2842
    if (pCol->colId == 1) {
14,850✔
2843
      data = &record->curTime;
1,650✔
2844
    } else if (pCol->colId == 2) {
13,200✔
2845
      data = record->detail;
1,650✔
2846
    } else if (pCol->colId == 3) {
11,550✔
2847
      data = record->user;
1,650✔
2848
    } else if (pCol->colId == 4) {
9,900✔
2849
      data = record->operation;
1,650✔
2850
    } else if (pCol->colId == 5) {
8,250✔
2851
      data = record->target1;
1,650✔
2852
    } else if (pCol->colId == 6) {
6,600✔
2853
      data = record->target2;
1,650✔
2854
    } else if (pCol->colId == 7) {
4,950✔
2855
      data = record->clientAddress;
1,650✔
2856
    } else if (pCol->colId == 8) {
3,300✔
2857
      data = &record->duration;
1,650✔
2858
    } else if (pCol->colId == 9) {
1,650✔
2859
      data = &record->affectedRows;
1,650✔
2860
    } else {
2861
      vError("the column id %" PRIi16 " is not defined in audit record table", pCol->colId);
×
2862
      code = TSDB_CODE_APP_ERROR;
×
2863
      TAOS_CHECK_GOTO(code, &lino, _exit);
×
2864
    }
2865

2866
    switch (pCol->type) {
14,850✔
2867
      case TSDB_DATA_TYPE_NCHAR:
9,900✔
2868
      case TSDB_DATA_TYPE_VARBINARY:
2869
      case TSDB_DATA_TYPE_VARCHAR: {
2870
        // if (colDataIsNull_s(pColInfoData, j)) {
2871
        //   SColVal cv = COL_VAL_NULL(pCol->colId, pCol->type);
2872
        //   if (NULL == taosArrayPush(pVals, &cv)) {
2873
        //     goto _end;
2874
        //   }
2875
        // } else {
2876
        char   *str = data;
9,900✔
2877
        int32_t len = strlen(str);
9,900✔
2878
        vTrace("set svalue id:%d %s, len:%d, bytes:%d", pCol->colId, str, len, pCol->bytes);
9,900✔
2879
        if (len > pCol->bytes - VARSTR_HEADER_SIZE) {
9,900✔
2880
          vWarn("vgId:%d, audit record string field with colId %d is too long, truncated from %d to %d bytes",
×
2881
                TD_VID(pVnode), pCol->colId, len, (int32_t)(pCol->bytes - VARSTR_HEADER_SIZE));
2882
          len = pCol->bytes - VARSTR_HEADER_SIZE;
×
2883
        }
2884
        SValue sv = {0};
9,900✔
2885
        sv.type = pCol->type;
9,900✔
2886
        sv.nData = len;
9,900✔
2887
        sv.pData = str;
9,900✔
2888
        SColVal cv = COL_VAL_VALUE(pCol->colId, sv);
9,900✔
2889
        if (NULL == taosArrayPush(pVals, &cv)) {
9,900✔
2890
          code = terrno;
×
2891
          TAOS_CHECK_GOTO(code, &lino, _exit);
×
2892
        }
2893
        //}
2894
        break;
9,900✔
2895
      }
2896
      case TSDB_DATA_TYPE_BLOB:
×
2897
      case TSDB_DATA_TYPE_MEDIUMBLOB:
2898
      case TSDB_DATA_TYPE_JSON: {
2899
        vError("the column type %" PRIi16 " is defined but not implemented yet", pCol->type);
×
2900
        code = TSDB_CODE_APP_ERROR;
×
2901
        TAOS_CHECK_GOTO(code, &lino, _exit);
×
2902
        break;
×
2903
      }
2904
      default: {
4,950✔
2905
        /*
2906
          if (colDataIsNull_s(pColInfoData, j)) {
2907
            if (PRIMARYKEY_TIMESTAMP_COL_ID == pCol->colId) {
2908
              qError("Primary timestamp column should not be null");
2909
              terrno = TSDB_CODE_PAR_INCORRECT_TIMESTAMP_VAL;
2910
              goto _end;
2911
            }
2912

2913
            SColVal cv = COL_VAL_NULL(pCol->colId, pCol->type);  // should use pCol->type
2914
            if (NULL == taosArrayPush(pVals, &cv)) {
2915
              goto _end;
2916
            }
2917
          } else {
2918
           */
2919
        SValue sv = {0};
4,950✔
2920
        sv.type = pCol->type;
4,950✔
2921
        valueSetDatum(&sv, sv.type, data, pCol->bytes);
4,950✔
2922
        SColVal cv = COL_VAL_VALUE(pCol->colId, sv);
4,950✔
2923
        if (NULL == taosArrayPush(pVals, &cv)) {
4,950✔
2924
          code = terrno;
×
2925
          TAOS_CHECK_GOTO(code, &lino, _exit);
×
2926
        }
2927
        //}
2928
        break;
4,950✔
2929
      }
2930
    }
2931
  }
2932

2933
  SRowBuildScanInfo sinfo = {0};
1,650✔
2934
  if ((code = tRowBuild(pVals, pSchema, &pRow, &sinfo)) < 0) {
1,650✔
2935
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
2936
  }
2937
  if (NULL == taosArrayPush(aRows, &pRow)) {
1,650✔
2938
    code = terrno;
×
2939
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
2940
  }
2941
  taosArrayDestroy(pVals);
1,650✔
2942
  pVals = NULL;
1,650✔
2943

2944
_exit:
1,650✔
2945
  if (code != 0) {
1,650✔
2946
    vError("vgId:%d, failed to prepare row at:%d since %s", TD_VID(pVnode), lino, tstrerror(code));
×
2947
    taosArrayDestroy(pVals);
×
2948
    tRowDestroy(pRow);
×
2949
    taosArrayDestroy(aRows);
×
2950
    terrno = code;
×
2951
    aRows = NULL;
×
2952
  }
2953
  return aRows;
1,650✔
2954
}
2955

2956
static SArray *vnodePrepareSubmitTb(SVnode *pVnode, SAuditRecord *record, STSchema *pSchema, SSchemaWrapper *pTagSchema,
1,650✔
2957
                                    int64_t suid) {
2958
  int32_t code = 0;
1,650✔
2959
  int32_t lino = 0;
1,650✔
2960

2961
  SArray *aSubmitTbData = NULL;
1,650✔
2962
  SSubmitTbData tbData = {0};
1,650✔
2963

2964
  if (!(aSubmitTbData = taosArrayInit(1, sizeof(SSubmitTbData)))) {
1,650✔
2965
    code = terrno;
×
2966
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
2967
  }
2968

2969
  char tbName[TSDB_TABLE_NAME_LEN] = {0};
1,650✔
2970
  (void)tsnprintf(tbName, TSDB_TABLE_NAME_LEN, "t_operations_%s", record->strClusterId);
1,650✔
2971

2972
  SMetaReader merTb = {0};
1,650✔
2973
  metaReaderDoInit(&merTb, pVnode->pMeta, META_READER_LOCK);
1,650✔
2974
  if ((code = metaGetTableEntryByName(&merTb, tbName)) == 0) {
1,650✔
2975
    vTrace("vgId:%d, get table entry, table:%s uid:%" PRId64 ", suid:% " PRId64 ", version:%" PRId64 ", api:%p",
1,500✔
2976
           TD_VID(pVnode), tbName, merTb.me.uid, merTb.me.ctbEntry.suid, merTb.me.version, merTb.pAPI);
2977
    tbData.uid = merTb.me.uid;
1,500✔
2978
    metaReaderClear(&merTb);
1,500✔
2979
  } else if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST) {
150✔
2980
    vTrace("vgId:%d, %s table not exist", TD_VID(pVnode), tbName);
150✔
2981

2982
    tbData.uid = tGenIdPI64();
150✔
2983
    tbData.flags |= SUBMIT_REQ_AUTO_CREATE_TABLE;
150✔
2984

2985
    tbData.pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
150✔
2986

2987
    if (tbData.pCreateTbReq == NULL) {
150✔
2988
      code = terrno;
×
2989
      TAOS_CHECK_GOTO(code, &lino, _exit);
×
2990
    }
2991

2992
    tbData.pCreateTbReq->uid = tbData.uid;
150✔
2993
    tbData.pCreateTbReq->btime = taosGetTimestampMs();
300✔
2994

2995
    code = vnodePrepareCreateTb(tbData.pCreateTbReq, tbName, suid, pTagSchema, record);
150✔
2996

2997
    metaReaderClear(&merTb);
150✔
2998
    TAOS_CHECK_GOTO(code, &lino, _exit);
150✔
2999
  } else {
3000
    metaReaderClear(&merTb);
×
3001
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
3002
  }
3003

3004
  tbData.suid = suid;
1,650✔
3005
  tbData.sver = pSchema->version;
1,650✔
3006

3007
  tbData.aRowP = vnodePrepareRow(pVnode, pSchema, record);
1,650✔
3008
  if (tbData.aRowP == NULL) {
1,650✔
3009
    code = terrno;
×
3010
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
3011
  }
3012

3013
  if (NULL == taosArrayPush(aSubmitTbData, &tbData)) {
1,650✔
3014
    code = terrno;
×
3015
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
3016
  }
3017

3018
_exit:
1,650✔
3019
  if (code != 0) {
1,650✔
3020
    vError("vgId:%d, failed to prepare submitTb at:%d since %s", TD_VID(pVnode), lino, tstrerror(code));
×
3021
    tDestroySubmitTbData(&tbData, TSDB_MSG_FLG_ENCODE);
×
3022
    if (aSubmitTbData != NULL) {
×
3023
      taosArrayDestroy(aSubmitTbData);
×
3024
    }
3025
    terrno = code;
×
3026
    aSubmitTbData = NULL;
×
3027
  }
3028
  return aSubmitTbData;
1,650✔
3029
}
3030

3031
static int32_t vnodeSaveOneAuditRecord(SVnode *pVnode, int64_t ver, SJson *pJson, SSchemaWrapper *pTagSchema,
1,650✔
3032
                                       int64_t suid, STSchema *pSchema, SRpcMsg *pOriginalMsg) {
3033
  int32_t code = 0;
1,650✔
3034
  int32_t lino = 0;
1,650✔
3035
  terrno = 0;
1,650✔
3036

3037
  SAuditRecord record = {0};
1,650✔
3038
  SSubmitReq2 *pSubmitReq = NULL;
1,650✔
3039
  SSubmitRsp2 *pSubmitRsp = &(SSubmitRsp2){0};
1,650✔
3040

3041
  TAOS_CHECK_GOTO(vnodeDecodeAuditRecord(pJson, &record), &lino, _exit);
1,650✔
3042

3043
  vTrace("vgId:%d, start to audit operation:%s", TD_VID(pVnode), record.operation);
1,650✔
3044

3045
  if (!(pSubmitReq = taosMemoryCalloc(1, sizeof(SSubmitReq2)))) {
1,650✔
3046
    code = terrno;
×
3047
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
3048
  }
3049

3050
  pSubmitReq->aSubmitTbData = vnodePrepareSubmitTb(pVnode, &record, pSchema, pTagSchema, suid);
1,650✔
3051
  if (pSubmitReq->aSubmitTbData == NULL) {
1,650✔
3052
    code = terrno;
×
3053
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
3054
  }
3055

3056
  vTrace("vgId:%d, going to create table", TD_VID(pVnode));
1,650✔
3057
  code = vnodeHandleAutoCreateTable(pVnode, ver, pSubmitReq, pSubmitRsp);
1,650✔
3058
  if (code) {
1,650✔
3059
    vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
3060
           tstrerror(code), ver);
3061
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
3062
  }
3063

3064
  vTrace("vgId:%d, going to data write", TD_VID(pVnode));
1,650✔
3065
  code = vnodeHandleDataWrite(pVnode, ver, pSubmitReq, pSubmitRsp);
1,650✔
3066
  if (code) {
1,650✔
3067
    vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
3068
           tstrerror(code), ver);
3069
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
3070
  }
3071

3072
  vTrace("vgId:%d, affectedRow:%d", TD_VID(pVnode), pSubmitRsp->affectedRows);
1,650✔
3073

3074
_exit:
1,650✔
3075
  if (code != 0)
1,650✔
3076
    vError("vgId:%d, failed to save one AuditRecord at line:%d, since %s", TD_VID(pVnode), lino, tstrerror(code));
×
3077

3078
  // update statistics
3079
  (void)atomic_add_fetch_64(&pVnode->statis.nInsert, pSubmitRsp->affectedRows);
1,650✔
3080
  (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsert, 1);
1,650✔
3081
  if (code == 0) {
1,650✔
3082
    (void)atomic_add_fetch_64(&pVnode->statis.nInsertSuccess, pSubmitRsp->affectedRows);
1,650✔
3083
    (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
1,650✔
3084
  }
3085

3086
  // update metrics
3087
  METRICS_UPDATE(pVnode->writeMetrics.total_requests, METRIC_LEVEL_LOW, 1);
1,650✔
3088
  METRICS_UPDATE(pVnode->writeMetrics.total_rows, METRIC_LEVEL_HIGH, pSubmitRsp->affectedRows);
1,650✔
3089
  // METRICS_UPDATE(pVnode->writeMetrics.total_bytes, METRIC_LEVEL_LOW, pMsg->header.contLen);
3090

3091
  if (code == 0 && tsEnableMonitor && tsMonitorFqdn[0] != 0 && tsMonitorPort != 0 && pSubmitRsp->affectedRows > 0 &&
1,650✔
3092
      strlen(RPC_MSG_USER(pOriginalMsg)) > 0 && tsInsertCounter != NULL) {
×
3093
    const char *sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT_AFFECTED_ROWS,
×
3094
                                   pVnode->monitor.strClusterId,
×
3095
                                   pVnode->monitor.strDnodeId,
×
3096
                                   tsLocalEp,
3097
                                   pVnode->monitor.strVgId,
×
3098
                                   RPC_MSG_USER(pOriginalMsg),
×
3099
                                   "Success"};
3100
    int         tv = taos_counter_add(tsInsertCounter, pSubmitRsp->affectedRows, sample_labels);
×
3101
    if (tv != 0) vError("vgId:%d, failed to taos counter add since return is %d", TD_VID(pVnode), tv);
×
3102
  }
3103

3104
  // clear
3105
  if (pSubmitReq != NULL) {
1,650✔
3106
    tDestroySubmitReq(pSubmitReq, TSDB_MSG_FLG_ENCODE);
1,650✔
3107
    taosMemoryFree(pSubmitReq);
1,650✔
3108
  }
3109
  if (pSubmitRsp != NULL) {
1,650✔
3110
    tDestroySSubmitRsp2(pSubmitRsp, TSDB_MSG_FLG_ENCODE);
1,650✔
3111
  }
3112

3113
  return code;
1,650✔
3114
}
3115

3116
static int32_t vnodeProcessAuditRecordReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pOriginalMsg) {
600✔
3117
  int32_t code = 0;
600✔
3118
  int32_t lino = 0;
600✔
3119
  terrno = 0;
600✔
3120

3121
  SVAuditRecordReq req = {0};
600✔
3122
  if (tDeserializeSVAuditRecordReq(pReq, len, &req) != 0) {
600✔
3123
    vError("vgId:%d, failed to deserialize SVAuditRecordReq", TD_VID(pVnode));
×
3124
    code = TSDB_CODE_INVALID_MSG;
×
3125
    return code;
×
3126
  }
3127

3128
  vTrace("vgId:%d, start to process AuditRecord Req", TD_VID(pVnode));
600✔
3129

3130
  SJson          *pJson = NULL;
600✔
3131
  STSchema       *pSchema = NULL;
600✔
3132
  SSchemaWrapper *pTagSchema = NULL;
600✔
3133
  int64_t         suid = 0;
600✔
3134

3135
  pJson = tjsonParse(req.data);
600✔
3136
  if (pJson == NULL) {
600✔
3137
    code = TSDB_CODE_INVALID_JSON_FORMAT;
×
3138
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
3139
  }
3140

3141
  SMetaReader merStb = {0};
600✔
3142
  metaReaderDoInit(&merStb, pVnode->pMeta, META_READER_LOCK);
600✔
3143
  code = metaGetTableEntryByName(&merStb, AUDIT_STABLE_NAME);
600✔
3144
  if (code != 0) {
600✔
3145
    metaReaderClear(&merStb);
×
3146
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
3147
  }
3148
  vTrace("vgId:%d, get audit stable entry, uid:%" PRId64 ", suid:% " PRId64 ", version:%" PRId64 ", api:%p",
600✔
3149
         TD_VID(pVnode), merStb.me.uid, merStb.me.ctbEntry.suid, merStb.me.version, merStb.pAPI);
3150

3151
  code = vnodeGetTableSchema(pVnode, merStb.me.uid, &pSchema, &suid, &pTagSchema);
600✔
3152
  if (code != 0) {
600✔
3153
    metaReaderClear(&merStb);
×
3154
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
3155
  }
3156
  vTrace("vgId:%d, get audit stable schema, version:%d, suid:%" PRId64, TD_VID(pVnode), pSchema->version, suid);
600✔
3157
  metaReaderClear(&merStb);
600✔
3158
  // pSchema pTagSchema can be cached in the future
3159

3160
  SJson *pRecords = tjsonGetObjectItem(pJson, "records");
600✔
3161
  if (pRecords == NULL) {
600✔
3162
    TAOS_CHECK_GOTO(vnodeSaveOneAuditRecord(pVnode, ver, pJson, pTagSchema, suid, pSchema, pOriginalMsg), &lino, _exit);
300✔
3163
  } else {
3164
    int32_t size = tjsonGetArraySize(pRecords);
300✔
3165
    vTrace("%d items in records", size);
300✔
3166
    for (int32_t i = 0; i < size; ++i) {
1,650✔
3167
      SJson *pRecord = tjsonGetArrayItem(pRecords, i);
1,350✔
3168
      if (pRecord == NULL) {
1,350✔
3169
        vError("vgId:%d, failed to get array item %d", TD_VID(pVnode), i);
×
3170
        code = TSDB_CODE_INVALID_JSON_FORMAT;
×
3171
        TAOS_CHECK_GOTO(code, &lino, _exit);
×
3172
      }
3173
      TAOS_CHECK_GOTO(vnodeSaveOneAuditRecord(pVnode, ver, pRecord, pTagSchema, suid, pSchema, pOriginalMsg), &lino,
1,350✔
3174
                      _exit);
3175
    }
3176
  }
3177

3178
_exit:
600✔
3179
  if (code != 0)
600✔
3180
    vError("vgId:%d, failed to process AuditRecordReq at line:%d, since %s", TD_VID(pVnode), lino, tstrerror(code));
×
3181

3182
  // clear
3183
  if (pSchema) taosMemoryFree(pSchema);
600✔
3184
  if (pTagSchema) {
600✔
3185
    taosMemoryFreeClear(pTagSchema->pSchema);
600✔
3186
    taosMemoryFree(pTagSchema);
600✔
3187
  }
3188
  if (pJson != NULL) {
600✔
3189
    cJSON_Delete(pJson);
600✔
3190
    pJson = NULL;
600✔
3191
  }
3192
  tFreeSVAuditRecordReq(&req);
600✔
3193

3194
  return code;
600✔
3195
}
3196

3197
static int32_t vnodeProcessSubmitReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
1,303,454,180✔
3198
                                     SRpcMsg *pOriginalMsg) {
3199
  int32_t code = 0;
1,303,454,180✔
3200
  int32_t lino = 0;
1,303,454,180✔
3201
  terrno = 0;
1,303,454,180✔
3202

3203
  SSubmitReq2 *pSubmitReq = &(SSubmitReq2){0};
1,303,459,172✔
3204
  SSubmitRsp2 *pSubmitRsp = &(SSubmitRsp2){0};
1,303,459,172✔
3205
  int32_t      ret;
3206
  SEncoder     ec = {0};
1,303,453,670✔
3207

3208
  pRsp->code = TSDB_CODE_SUCCESS;
1,303,456,630✔
3209

3210
  void           *pAllocMsg = NULL;
1,303,455,150✔
3211
  SSubmitReq2Msg *pMsg = (SSubmitReq2Msg *)pReq;
1,303,455,150✔
3212
  SDecoder        dc = {0};
1,303,455,150✔
3213
  if (0 == taosHton64(pMsg->version)) {
1,303,454,732✔
3214
    code = vnodeSubmitReqConvertToSubmitReq2(pVnode, (SSubmitReq *)pMsg, pSubmitReq);
×
3215
    if (TSDB_CODE_SUCCESS == code) {
×
3216
      code = vnodeRebuildSubmitReqMsg(pSubmitReq, &pReq);
×
3217
    }
3218
    if (TSDB_CODE_SUCCESS == code) {
×
3219
      pAllocMsg = pReq;
×
3220
    }
3221
    TSDB_CHECK_CODE(code, lino, _exit);
×
3222
  } else {
3223
    // decode
3224
    pReq = POINTER_SHIFT(pReq, sizeof(SSubmitReq2Msg));
1,303,456,042✔
3225
    len -= sizeof(SSubmitReq2Msg);
1,303,456,042✔
3226

3227
    tDecoderInit(&dc, pReq, len);
1,303,456,042✔
3228
    if (tDecodeSubmitReq(&dc, pSubmitReq, NULL) < 0) {
1,303,458,584✔
3229
      code = TSDB_CODE_INVALID_MSG;
×
3230
      TSDB_CHECK_CODE(code, lino, _exit);
×
3231
    }
3232
  }
3233

3234
  // Scan the request
3235
  code = vnodeScanSubmitReq(pVnode, ver, pSubmitReq, pSubmitRsp);
1,303,430,350✔
3236
  if (code) {
1,303,399,200✔
3237
    vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
3238
           tstrerror(code), ver);
3239
    TSDB_CHECK_CODE(code, lino, _exit);
×
3240
  }
3241

3242
  vGDebug(pOriginalMsg ? &pOriginalMsg->info.traceId : NULL, "vgId:%d, index:%" PRId64 ", submit block, rows:%d",
1,303,399,200✔
3243
          TD_VID(pVnode), ver, (int32_t)taosArrayGetSize(pSubmitReq->aSubmitTbData));
3244

3245
  // Handle auto create table
3246
  code = vnodeHandleAutoCreateTable(pVnode, ver, pSubmitReq, pSubmitRsp);
1,303,448,922✔
3247
  if (code) {
1,303,458,674✔
3248
    vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
60,462✔
3249
           tstrerror(code), ver);
3250
    TSDB_CHECK_CODE(code, lino, _exit);
60,462✔
3251
  }
3252

3253
  // Handle data write
3254
  code = vnodeHandleDataWrite(pVnode, ver, pSubmitReq, pSubmitRsp);
1,303,398,212✔
3255
  if (code) {
1,303,399,844✔
3256
    vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
2,320✔
3257
           tstrerror(code), ver);
3258
    TSDB_CHECK_CODE(code, lino, _exit);
2,320✔
3259
  }
3260

3261
_exit:
1,303,459,910✔
3262
  // message
3263
  pRsp->code = code;
1,303,460,064✔
3264
  tEncodeSize(tEncodeSSubmitRsp2, pSubmitRsp, pRsp->contLen, ret);
1,303,460,064✔
3265
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
1,303,459,730✔
3266
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
1,303,458,054✔
3267
  if (tEncodeSSubmitRsp2(&ec, pSubmitRsp) < 0) {
1,303,458,668✔
3268
    vError("vgId:%d, failed to encode submit response", TD_VID(pVnode));
×
3269
  }
3270
  tEncoderClear(&ec);
1,303,453,652✔
3271

3272
  // update statistics
3273
  (void)atomic_add_fetch_64(&pVnode->statis.nInsert, pSubmitRsp->affectedRows);
1,303,451,750✔
3274
  (void)atomic_add_fetch_64(&pVnode->statis.nInsertSuccess, pSubmitRsp->affectedRows);
1,303,459,244✔
3275
  (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsert, 1);
1,303,458,230✔
3276

3277
  // update metrics
3278
  METRICS_UPDATE(pVnode->writeMetrics.total_requests, METRIC_LEVEL_LOW, 1);
1,303,459,150✔
3279
  METRICS_UPDATE(pVnode->writeMetrics.total_rows, METRIC_LEVEL_HIGH, pSubmitRsp->affectedRows);
1,303,445,412✔
3280
  METRICS_UPDATE(pVnode->writeMetrics.total_bytes, METRIC_LEVEL_LOW, pMsg->header.contLen);
1,303,444,766✔
3281

3282
  if (tsEnableMonitor && tsMonitorFqdn[0] != 0 && tsMonitorPort != 0 && pSubmitRsp->affectedRows > 0 &&
1,303,437,744✔
3283
      strlen(RPC_MSG_USER(pOriginalMsg)) > 0 && tsInsertCounter != NULL) {
×
3284
    const char *sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT_AFFECTED_ROWS,
×
3285
                                   pVnode->monitor.strClusterId,
×
3286
                                   pVnode->monitor.strDnodeId,
×
3287
                                   tsLocalEp,
3288
                                   pVnode->monitor.strVgId,
×
3289
                                   RPC_MSG_USER(pOriginalMsg),
×
3290
                                   "Success"};
3291
    int         tv = taos_counter_add(tsInsertCounter, pSubmitRsp->affectedRows, sample_labels);
×
3292
    if (tv != 0) vError("vgId:%d, failed to taos counter add since return is %d", TD_VID(pVnode), tv);
×
3293
  }
3294

3295
  if (code == 0) {
1,303,437,744✔
3296
    (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
1,303,380,734✔
3297
  }
3298

3299
  // clear
3300
  tDestroySubmitReq(pSubmitReq, 0 == taosHton64(pMsg->version) ? TSDB_MSG_FLG_CMPT : TSDB_MSG_FLG_DECODE);
1,303,449,322✔
3301
  tDestroySSubmitRsp2(pSubmitRsp, TSDB_MSG_FLG_ENCODE);
1,303,424,966✔
3302

3303
  if (code) {
1,303,421,134✔
3304
    terrno = code;
62,782✔
3305
    if (code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
62,782✔
3306
      vInfo("vgId:%d, failed to process submit request since %s, lino:%d, version:%" PRId64, TD_VID(pVnode),
58,334✔
3307
            tstrerror(code), lino, ver);
3308

3309
    } else {
3310
      vError("vgId:%d, failed to process submit request since %s, lino:%d, version:%" PRId64, TD_VID(pVnode),
4,448✔
3311
             tstrerror(code), lino, ver);
3312
    }
3313
  }
3314

3315
  taosMemoryFree(pAllocMsg);
1,303,406,302✔
3316
  tDecoderClear(&dc);
1,303,416,498✔
3317

3318
  return code;
1,303,457,112✔
3319
}
3320

3321
static int32_t vnodeConsolidateAlterHashRange(SVnode *pVnode, int64_t ver) {
51,724✔
3322
  int32_t code = TSDB_CODE_SUCCESS;
51,724✔
3323

3324
  vInfo("vgId:%d, trim meta of tables per hash range [%" PRIu32 ", %" PRIu32 "]. apply-index:%" PRId64, TD_VID(pVnode),
51,724✔
3325
        pVnode->config.hashBegin, pVnode->config.hashEnd, ver);
3326

3327
  // TODO: trim meta of tables from TDB per hash range [pVnode->config.hashBegin, pVnode->config.hashEnd]
3328
  code = metaTrimTables(pVnode->pMeta, ver);
51,724✔
3329

3330
  return code;
51,724✔
3331
}
3332

3333
static int32_t vnodeProcessAlterConfirmReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
8,979,308✔
3334
  vInfo("vgId:%d, vnode handle msgType:alter-confirm, alter confirm msg is processed", TD_VID(pVnode));
8,979,308✔
3335
  int32_t code = TSDB_CODE_SUCCESS;
8,979,308✔
3336
  if (!pVnode->config.hashChange) {
8,979,308✔
3337
    goto _exit;
8,927,584✔
3338
  }
3339

3340
  code = vnodeConsolidateAlterHashRange(pVnode, ver);
51,724✔
3341
  if (code < 0) {
51,724✔
3342
    vError("vgId:%d, failed to consolidate alter hashrange since %s. version:%" PRId64, TD_VID(pVnode), terrstr(), ver);
×
3343
    goto _exit;
×
3344
  }
3345
  pVnode->config.hashChange = false;
51,724✔
3346

3347
_exit:
8,979,308✔
3348
  pRsp->msgType = TDMT_VND_ALTER_CONFIRM_RSP;
8,979,308✔
3349
  pRsp->code = code;
8,979,308✔
3350
  pRsp->pCont = NULL;
8,979,308✔
3351
  pRsp->contLen = 0;
8,979,308✔
3352

3353
  return code;
8,979,308✔
3354
}
3355

3356
extern int32_t tsdbDisableAndCancelAllBgTask(STsdb *pTsdb);
3357
extern void    tsdbEnableBgTask(STsdb *pTsdb);
3358

3359
static int32_t vnodeProcessAlterConfigReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
664,962✔
3360
  bool walChanged = false;
664,962✔
3361
  bool tsdbChanged = false;
664,962✔
3362

3363
  SAlterVnodeConfigReq req = {0};
664,962✔
3364
  if (tDeserializeSAlterVnodeConfigReq(pReq, len, &req) != 0) {
664,962✔
3365
    terrno = TSDB_CODE_INVALID_MSG;
×
3366
    return TSDB_CODE_INVALID_MSG;
×
3367
  }
3368

3369
  vInfo("vgId:%d, start to alter vnode config, page:%d pageSize:%d buffer:%d szPage:%d szBuf:%" PRIu64
664,962✔
3370
        " cacheLast:%d cacheLastSize:%d days:%d keep0:%d keep1:%d keep2:%d keepTimeOffset:%d ssKeepLocal:%d "
3371
        "ssCompact:%d allowDrop:%d fsync:%d level:%d "
3372
        "walRetentionPeriod:%d walRetentionSize:%d",
3373
        TD_VID(pVnode), req.pages, req.pageSize, req.buffer, req.pageSize * 1024, (uint64_t)req.buffer * 1024 * 1024,
3374
        req.cacheLast, req.cacheLastSize, req.daysPerFile, req.daysToKeep0, req.daysToKeep1, req.daysToKeep2,
3375
        req.keepTimeOffset, req.ssKeepLocal, req.ssCompact, req.allowDrop, req.walFsyncPeriod, req.walLevel,
3376
        req.walRetentionPeriod, req.walRetentionSize);
3377

3378
  if (pVnode->config.cacheLastSize != req.cacheLastSize) {
664,962✔
3379
    pVnode->config.cacheLastSize = req.cacheLastSize;
7,444✔
3380
    tsdbCacheSetCapacity(pVnode, (size_t)pVnode->config.cacheLastSize * 1024 * 1024);
7,444✔
3381
  }
3382

3383
  if (pVnode->config.szBuf != req.buffer * 1024LL * 1024LL) {
664,962✔
3384
    vInfo("vgId:%d, vnode buffer is changed from %" PRId64 " to %" PRId64, TD_VID(pVnode), pVnode->config.szBuf,
19,208✔
3385
          (uint64_t)(req.buffer * 1024LL * 1024LL));
3386
    pVnode->config.szBuf = req.buffer * 1024LL * 1024LL;
19,208✔
3387
  }
3388

3389
  if (pVnode->config.szCache != req.pages) {
664,962✔
3390
    if ((terrno = metaAlterCache(pVnode->pMeta, req.pages)) < 0) {
9,336✔
3391
      vError("vgId:%d, failed to change vnode pages from %d to %d failed since %s", TD_VID(pVnode),
×
3392
             pVnode->config.szCache, req.pages, tstrerror(terrno));
3393
      return terrno;
×
3394
    } else {
3395
      vInfo("vgId:%d, vnode pages is changed from %d to %d", TD_VID(pVnode), pVnode->config.szCache, req.pages);
9,336✔
3396
      pVnode->config.szCache = req.pages;
9,336✔
3397
    }
3398
  }
3399

3400
  if (pVnode->config.cacheLast != req.cacheLast) {
664,962✔
3401
    pVnode->config.cacheLast = req.cacheLast;
178,374✔
3402
  }
3403

3404
  if (pVnode->config.walCfg.fsyncPeriod != req.walFsyncPeriod) {
664,962✔
3405
    pVnode->config.walCfg.fsyncPeriod = req.walFsyncPeriod;
108,096✔
3406
    walChanged = true;
108,096✔
3407
  }
3408

3409
  if (pVnode->config.walCfg.level != req.walLevel) {
664,962✔
3410
    if (pVnode->config.walCfg.level == 0) {
79,580✔
3411
      pVnode->config.walCfg.clearFiles = 1;
4,508✔
3412
    }
3413
    pVnode->config.walCfg.level = req.walLevel;
79,580✔
3414
    walChanged = true;
79,580✔
3415
  }
3416

3417
  if (pVnode->config.walCfg.retentionPeriod != req.walRetentionPeriod) {
664,962✔
3418
    pVnode->config.walCfg.retentionPeriod = req.walRetentionPeriod;
101,076✔
3419
    walChanged = true;
101,076✔
3420
  }
3421

3422
  if (pVnode->config.walCfg.retentionSize != req.walRetentionSize) {
664,962✔
3423
    pVnode->config.walCfg.retentionSize = req.walRetentionSize;
3,000✔
3424
    walChanged = true;
3,000✔
3425
  }
3426

3427
  if (pVnode->config.tsdbCfg.keep0 != req.daysToKeep0) {
664,962✔
3428
    pVnode->config.tsdbCfg.keep0 = req.daysToKeep0;
89,936✔
3429
    tsdbChanged = true;
89,936✔
3430
  }
3431

3432
  if (pVnode->config.tsdbCfg.keep1 != req.daysToKeep1) {
664,962✔
3433
    pVnode->config.tsdbCfg.keep1 = req.daysToKeep1;
86,932✔
3434
    tsdbChanged = true;
86,932✔
3435
  }
3436

3437
  if (pVnode->config.tsdbCfg.keep2 != req.daysToKeep2) {
664,962✔
3438
    pVnode->config.tsdbCfg.keep2 = req.daysToKeep2;
75,124✔
3439
    tsdbChanged = true;
75,124✔
3440
  }
3441

3442
  if (pVnode->config.tsdbCfg.keepTimeOffset != req.keepTimeOffset) {
664,962✔
3443
    pVnode->config.tsdbCfg.keepTimeOffset = req.keepTimeOffset;
15,116✔
3444
    tsdbChanged = true;
15,116✔
3445
  }
3446

3447
  if (req.sttTrigger != -1 && req.sttTrigger != pVnode->config.sttTrigger) {
664,962✔
3448
    if (req.sttTrigger > 1 && pVnode->config.sttTrigger > 1) {
5,224✔
3449
      pVnode->config.sttTrigger = req.sttTrigger;
3,000✔
3450
    } else {
3451
      vnodeAWait(&pVnode->commitTask);
2,224✔
3452

3453
      int32_t ret = tsdbDisableAndCancelAllBgTask(pVnode->pTsdb);
2,224✔
3454
      if (ret != 0) {
2,224✔
3455
        vError("vgId:%d, failed to disable bg task since %s", TD_VID(pVnode), tstrerror(ERRNO));
×
3456
      }
3457

3458
      pVnode->config.sttTrigger = req.sttTrigger;
2,224✔
3459
      tsdbEnableBgTask(pVnode->pTsdb);
2,224✔
3460
    }
3461
  }
3462

3463
  if (req.minRows != -1 && req.minRows != pVnode->config.tsdbCfg.minRows) {
664,962✔
3464
    pVnode->config.tsdbCfg.minRows = req.minRows;
3,000✔
3465
  }
3466

3467
  if (req.ssKeepLocal != -1 && req.ssKeepLocal != pVnode->config.ssKeepLocal) {
664,962✔
3468
    pVnode->config.ssKeepLocal = req.ssKeepLocal;
3,000✔
3469
  }
3470
  if (req.ssCompact != -1 && req.ssCompact != pVnode->config.ssCompact) {
664,962✔
3471
    pVnode->config.ssCompact = req.ssCompact;
3,000✔
3472
  }
3473
  if (req.allowDrop != pVnode->config.allowDrop) {
664,962✔
3474
    pVnode->config.allowDrop = req.allowDrop;
×
3475
  }
3476
  if (req.secureDelete != pVnode->config.secureDelete) {
664,962✔
3477
    pVnode->config.secureDelete = req.secureDelete;
2,352✔
3478
  }
3479

3480
  if (walChanged) {
664,962✔
3481
    if (walAlter(pVnode->pWal, &pVnode->config.walCfg) != 0) {
285,728✔
UNCOV
3482
      vError("vgId:%d, failed to alter wal config since %s", TD_VID(pVnode), tstrerror(ERRNO));
×
3483
    }
3484
  }
3485

3486
  if (tsdbChanged) {
664,962✔
3487
    tsdbSetKeepCfg(pVnode->pTsdb, &pVnode->config.tsdbCfg);
120,076✔
3488
  }
3489

3490
  return 0;
664,962✔
3491
}
3492

UNCOV
3493
static int32_t vnodeProcessBatchDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
UNCOV
3494
  SBatchDeleteReq deleteReq;
×
UNCOV
3495
  SDecoder        decoder;
×
3496
  tDecoderInit(&decoder, pReq, len);
×
3497
  if (tDecodeSBatchDeleteReq(&decoder, &deleteReq) < 0) {
×
3498
    tDecoderClear(&decoder);
×
3499
    return terrno = TSDB_CODE_INVALID_MSG;
×
3500
  }
3501

3502
  SMetaReader mr = {0};
×
UNCOV
3503
  metaReaderDoInit(&mr, pVnode->pMeta, META_READER_NOLOCK);
×
UNCOV
3504
  STsdb *pTsdb = pVnode->pTsdb;
×
3505

3506
  int32_t sz = taosArrayGetSize(deleteReq.deleteReqs);
×
3507
  for (int32_t i = 0; i < sz; i++) {
×
UNCOV
3508
    SSingleDeleteReq *pOneReq = taosArrayGet(deleteReq.deleteReqs, i);
×
3509
    char             *name = pOneReq->tbname;
×
3510
    if (metaGetTableEntryByName(&mr, name) < 0) {
×
3511
      vDebug("vgId:%d, stream delete msg, skip since no table: %s", pVnode->config.vgId, name);
×
3512
      continue;
×
3513
    }
3514

3515
    int64_t uid = mr.me.uid;
×
3516

NEW
3517
    int32_t code = tsdbDeleteTableData(pTsdb, ver, deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs, 0);
×
3518
    if (code < 0) {
×
UNCOV
3519
      terrno = code;
×
3520
      vError("vgId:%d, delete error since %s, suid:%" PRId64 ", uid:%" PRId64 ", start ts:%" PRId64 ", end ts:%" PRId64,
×
3521
             TD_VID(pVnode), terrstr(), deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
3522
    }
3523

UNCOV
3524
    if (deleteReq.level == 0) {
×
UNCOV
3525
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, deleteReq.ctimeMs);
×
UNCOV
3526
      if (code < 0) {
×
3527
        terrno = code;
×
3528
        vError("vgId:%d, update change time error since %s, suid:%" PRId64 ", uid:%" PRId64 ", start ts:%" PRId64
×
3529
               ", end ts:%" PRId64,
3530
               TD_VID(pVnode), terrstr(), deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
3531
      }
3532
    }
UNCOV
3533
    tDecoderClear(&mr.coder);
×
3534
  }
UNCOV
3535
  metaReaderClear(&mr);
×
3536
  taosArrayDestroy(deleteReq.deleteReqs);
×
UNCOV
3537
  return 0;
×
3538
}
3539

3540
static int32_t vnodeProcessDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
4,379,850✔
3541
                                     SRpcMsg *pOriginalMsg) {
3542
  int32_t     code = 0;
4,379,850✔
3543
  SDecoder   *pCoder = &(SDecoder){0};
4,379,850✔
3544
  SDeleteRes *pRes = &(SDeleteRes){0};
4,379,850✔
3545

3546
  pRsp->msgType = TDMT_VND_DELETE_RSP;
4,379,850✔
3547
  pRsp->pCont = NULL;
4,379,850✔
3548
  pRsp->contLen = 0;
4,379,850✔
3549
  pRsp->code = TSDB_CODE_SUCCESS;
4,379,850✔
3550

3551
  pRes->uidList = taosArrayInit(0, sizeof(tb_uid_t));
4,379,850✔
3552
  if (pRes->uidList == NULL) {
4,379,850✔
UNCOV
3553
    code = terrno;
×
UNCOV
3554
    goto _err;
×
3555
  }
3556

3557
  tDecoderInit(pCoder, pReq, len);
4,379,850✔
3558
  code = tDecodeDeleteRes(pCoder, pRes);
4,379,850✔
3559
  if (code) goto _err;
4,379,850✔
3560

3561
  if (pRes->affectedRows > 0) {
4,379,850✔
3562
    for (int32_t iUid = 0; iUid < taosArrayGetSize(pRes->uidList); iUid++) {
7,809,680✔
3563
      uint64_t uid = *(uint64_t *)taosArrayGet(pRes->uidList, iUid);
4,448,698✔
3564
      // Merge runtime vnode(db-level) secureDelete to avoid stale client meta after ALTER DATABASE.
3565
      int8_t secureDelete = pRes->secureDelete | pVnode->config.secureDelete;
4,448,698✔
3566
      code = tsdbDeleteTableData(pVnode->pTsdb, ver, pRes->suid, uid, pRes->skey, pRes->ekey, secureDelete);
4,448,698✔
3567
      if (code) goto _err;
4,448,698✔
3568
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, pRes->ctimeMs);
4,448,698✔
3569
      if (code) goto _err;
4,448,698✔
3570
    }
3571
  }
3572

3573
  tDecoderClear(pCoder);
4,379,850✔
3574
  taosArrayDestroy(pRes->uidList);
4,379,850✔
3575

3576
  SVDeleteRsp rsp = {.affectedRows = pRes->affectedRows};
4,379,850✔
3577
  int32_t     ret = 0;
4,379,850✔
3578
  tEncodeSize(tEncodeSVDeleteRsp, &rsp, pRsp->contLen, ret);
4,379,850✔
3579
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
4,379,850✔
3580
  SEncoder ec = {0};
4,379,850✔
3581
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
4,379,850✔
3582
  code = tEncodeSVDeleteRsp(&ec, &rsp);
4,379,850✔
3583
  if (code) goto _err;
4,379,850✔
3584
  tEncoderClear(&ec);
4,379,850✔
3585
  return code;
4,379,850✔
3586

UNCOV
3587
_err:
×
3588
  /*
3589
  if(code == TSDB_CODE_SUCCESS){
3590
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_DELETE, pVnode->monitor.strClusterId,
3591
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
3592
                                        RPC_MSG_USER(pOriginalMsg), "Success"};
3593
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
3594
  }
3595
  else{
3596
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_DELETE, pVnode->monitor.strClusterId,
3597
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
3598
                                        RPC_MSG_USER(pOriginalMsg), "Failed"};
3599
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
3600
  }
3601
  */
3602

UNCOV
3603
  return code;
×
3604
}
3605
static int32_t vnodeProcessCreateIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
37,334✔
3606
  SVCreateStbReq req = {0};
37,334✔
3607
  SDecoder       dc = {0};
37,334✔
3608
  int32_t        code = 0;
37,334✔
3609

3610
  pRsp->msgType = TDMT_VND_CREATE_INDEX_RSP;
37,334✔
3611
  pRsp->code = TSDB_CODE_SUCCESS;
37,334✔
3612
  pRsp->pCont = NULL;
37,334✔
3613
  pRsp->contLen = 0;
37,334✔
3614

3615
  tDecoderInit(&dc, pReq, len);
37,334✔
3616
  // decode req
3617
  if (tDecodeSVCreateStbReq(&dc, &req) < 0) {
37,334✔
UNCOV
3618
    tDecoderClear(&dc);
×
UNCOV
3619
    return terrno = TSDB_CODE_INVALID_MSG;
×
3620
  }
3621

3622
  code = metaAddIndexToSuperTable(pVnode->pMeta, ver, &req);
37,334✔
3623
  if (code) {
37,334✔
3624
    pRsp->code = code;
×
UNCOV
3625
    goto _err;
×
3626
  }
3627
  tDecoderClear(&dc);
37,334✔
3628
  return 0;
37,334✔
3629

3630
_err:
×
UNCOV
3631
  tDecoderClear(&dc);
×
UNCOV
3632
  return code;
×
3633
}
3634
static int32_t vnodeProcessDropIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
30,396✔
3635
  SDropIndexReq req = {0};
30,396✔
3636
  int32_t       code = 0;
30,396✔
3637
  pRsp->msgType = TDMT_VND_DROP_INDEX_RSP;
30,396✔
3638
  pRsp->code = TSDB_CODE_SUCCESS;
30,396✔
3639
  pRsp->pCont = NULL;
30,396✔
3640
  pRsp->contLen = 0;
30,396✔
3641

3642
  if ((code = tDeserializeSDropIdxReq(pReq, len, &req))) {
30,396✔
UNCOV
3643
    pRsp->code = code;
×
UNCOV
3644
    return code;
×
3645
  }
3646

3647
  code = metaDropIndexFromSuperTable(pVnode->pMeta, ver, &req);
30,396✔
3648
  if (code) {
30,396✔
3649
    pRsp->code = code;
×
UNCOV
3650
    return code;
×
3651
  }
3652
  return TSDB_CODE_SUCCESS;
30,396✔
3653
}
3654

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

3657
static int32_t vnodeProcessCompactVnodeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
142,454✔
3658
  if (!pVnode->restored) {
142,454✔
3659
    vInfo("vgId:%d, ignore compact req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
6,776✔
3660
    return 0;
6,776✔
3661
  }
3662
  return vnodeAsyncCompact(pVnode, ver, pReq, len, pRsp);
135,678✔
3663
}
3664

UNCOV
3665
static int32_t vnodeProcessConfigChangeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
UNCOV
3666
  if (syncCheckMember(pVnode->sync) != 0) {
×
UNCOV
3667
    vError("vgId:%d, failed to check member", TD_VID(pVnode));
×
3668
  }
3669

3670
  pRsp->msgType = TDMT_SYNC_CONFIG_CHANGE_RSP;
×
3671
  pRsp->code = TSDB_CODE_SUCCESS;
×
3672
  pRsp->pCont = NULL;
×
UNCOV
3673
  pRsp->contLen = 0;
×
3674

3675
  return 0;
×
3676
}
3677

3678
static int32_t vnodeCheckState(SVnode *pVnode) {
108,276✔
3679
  SSyncState syncState = syncGetState(pVnode->sync);
108,276✔
3680
  if (syncState.state != TAOS_SYNC_STATE_LEADER) {
108,276✔
3681
    return terrno = TSDB_CODE_SYN_NOT_LEADER;
34,244✔
3682
  }
3683
  return 0;
74,032✔
3684
}
3685

3686
static int32_t vnodeCheckToken(SVnode *pVnode, char *member0Token, char *member1Token) {
74,032✔
3687
  char token[TSDB_ARB_TOKEN_SIZE] = {0};
74,032✔
3688
  if (vnodeGetArbToken(pVnode, token) != 0) {
74,032✔
UNCOV
3689
    return terrno = TSDB_CODE_NOT_FOUND;
×
3690
  }
3691

3692
  if (strncmp(token, member0Token, TSDB_ARB_TOKEN_SIZE) != 0 &&
74,032✔
3693
      strncmp(token, member1Token, TSDB_ARB_TOKEN_SIZE) != 0) {
68,948✔
3694
    return terrno = TSDB_CODE_MND_ARB_TOKEN_MISMATCH;
67,532✔
3695
  }
3696

3697
  terrno = TSDB_CODE_SUCCESS;
6,500✔
3698
  return 0;
6,500✔
3699
}
3700

3701
static int32_t vnodeCheckSyncd(SVnode *pVnode, char *member0Token, char *member1Token) {
3,250✔
3702
  int32_t code = vnodeCheckToken(pVnode, member0Token, member1Token);
3,250✔
3703
  if (code != 0) {
3,250✔
UNCOV
3704
    return code;
×
3705
  }
3706

3707
  return syncCheckSynced(pVnode->sync);
3,250✔
3708
}
3709

3710
static int32_t vnodeProcessArbCheckSyncReq(SVnode *pVnode, void *pReq, int32_t len, SRpcMsg *pRsp) {
6,494✔
3711
  int32_t code = 0;
6,494✔
3712

3713
  if ((code = vnodeCheckState(pVnode)) != 0) {
6,494✔
3714
    vDebug("vgId:%d, failed to preprocess vnode-arb-check-sync request since %s", TD_VID(pVnode), tstrerror(code));
3,244✔
3715
    return 0;
3,244✔
3716
  }
3717

3718
  SVArbCheckSyncReq syncReq = {0};
3,250✔
3719

3720
  code = tDeserializeSVArbCheckSyncReq(pReq, len, &syncReq);
3,250✔
3721
  if (code) {
3,250✔
UNCOV
3722
    return code;
×
3723
  }
3724

3725
  vInfo("vgId:%d, start to process vnode-arb-check-sync req QID:0x%" PRIx64 ":0x%" PRIx64 ", seqNum:%" PRIx64
3,250✔
3726
        ", arbToken:%s, member0Token:%s, member1Token:%s, "
3727
        "arbTerm:%" PRId64,
3728
        TD_VID(pVnode), pRsp->info.traceId.rootId, pRsp->info.traceId.msgId, pRsp->info.seqNum, syncReq.arbToken,
3729
        syncReq.member0Token, syncReq.member1Token, syncReq.arbTerm);
3730

3731
  pRsp->msgType = TDMT_VND_ARB_CHECK_SYNC_RSP;
3,250✔
3732
  pRsp->code = TSDB_CODE_SUCCESS;
3,250✔
3733
  pRsp->pCont = NULL;
3,250✔
3734
  pRsp->contLen = 0;
3,250✔
3735

3736
  SVArbCheckSyncRsp syncRsp = {0};
3,250✔
3737
  syncRsp.arbToken = syncReq.arbToken;
3,250✔
3738
  syncRsp.member0Token = syncReq.member0Token;
3,250✔
3739
  syncRsp.member1Token = syncReq.member1Token;
3,250✔
3740
  syncRsp.vgId = TD_VID(pVnode);
3,250✔
3741

3742
  if ((syncRsp.errCode = vnodeCheckSyncd(pVnode, syncReq.member0Token, syncReq.member1Token)) != 0) {
3,250✔
UNCOV
3743
    vError("vgId:%d, failed to check assigned log syncd since %s", TD_VID(pVnode), tstrerror(syncRsp.errCode));
×
3744
  }
3745

3746
  if ((code = vnodeUpdateArbTerm(pVnode, syncReq.arbTerm)) != 0) {
3,250✔
UNCOV
3747
    vError("vgId:%d, failed to update arb term since %s", TD_VID(pVnode), tstrerror(code));
×
3748
    goto _OVER;
×
3749
  }
3750

3751
  int32_t contLen = tSerializeSVArbCheckSyncRsp(NULL, 0, &syncRsp);
3,250✔
3752
  if (contLen <= 0) {
3,250✔
3753
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
3754
    code = -1;
×
UNCOV
3755
    goto _OVER;
×
3756
  }
3757
  void *pHead = rpcMallocCont(contLen);
3,250✔
3758
  if (!pHead) {
3,250✔
3759
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
3760
    code = -1;
×
UNCOV
3761
    goto _OVER;
×
3762
  }
3763

3764
  if (tSerializeSVArbCheckSyncRsp(pHead, contLen, &syncRsp) <= 0) {
3,250✔
3765
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
3766
    rpcFreeCont(pHead);
×
UNCOV
3767
    code = -1;
×
UNCOV
3768
    goto _OVER;
×
3769
  }
3770

3771
  pRsp->pCont = pHead;
3,250✔
3772
  pRsp->contLen = contLen;
3,250✔
3773

3774
  vInfo(
3,250✔
3775
      "vgId:%d, suceed to process vnode-arb-check-sync req rsp.code:%s, arbToken:%s, member0Token:%s, "
3776
      "member1Token:%s",
3777
      TD_VID(pVnode), tstrerror(syncRsp.errCode), syncRsp.arbToken, syncRsp.member0Token, syncRsp.member1Token);
3778

3779
  code = TSDB_CODE_SUCCESS;
3,250✔
3780

3781
_OVER:
3,250✔
3782
  if (code != 0) {
3,250✔
UNCOV
3783
    vError("vgId:%d, failed to process arb check req rsp.code:%s since %s", TD_VID(pVnode), tstrerror(syncRsp.errCode),
×
3784
           tstrerror(code));
3785
  }
3786
  tFreeSVArbCheckSyncReq(&syncReq);
3,250✔
3787
  return code;
3,250✔
3788
}
3789

3790
#ifndef TD_ENTERPRISE
3791
int32_t vnodeAsyncCompact(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) { return 0; }
3792
int32_t tsdbAsyncCompact(STsdb *tsdb, const STimeWindow *tw, ETsdbOpType type, bool force) { return 0; }
3793
#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