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

taosdata / TDengine / #4999

22 Mar 2026 10:21AM UTC coverage: 72.31% (-0.03%) from 72.335%
#4999

push

travis-ci

web-flow
feat(subq/some): some/any/exists for stream subq (#34860)

50 of 68 new or added lines in 1 file covered. (73.53%)

701 existing lines in 132 files now uncovered.

253472 of 350536 relevant lines covered (72.31%)

131775367.32 hits per line

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

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

16
#include <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 "tarray.h"
23
#include "tencode.h"
24
#include "tglobal.h"
25
#include "thash.h"
26
#include "tlrucache.h"
27
#include "tmsg.h"
28
#include "tmsgcb.h"
29
#include "tstrbuild.h"
30
#include "tutil.h"
31
#include "tsdb.h"
32
#include "ttypes.h"
33
#include "vnd.h"
34
#include "vnode.h"
35
#include "vnodeInt.h"
36

37
extern taos_counter_t *tsInsertCounter;
38

39
extern int32_t vnodeProcessScanVnodeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
40
extern int32_t vnodeQueryScanProgress(SVnode *pVnode, SRpcMsg *pMsg);
41

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

71
static int32_t vnodeCheckState(SVnode *pVnode);
72
static int32_t vnodeCheckToken(SVnode *pVnode, char *member0Token, char *member1Token);
73
static int32_t vnodeCheckSyncd(SVnode *pVnode, char *member0Token, char *member1Token);
74
static int32_t vnodeProcessFetchTtlExpiredTbs(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
75

76
extern int32_t vnodeProcessKillCompactReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
77
extern int32_t vnodeQueryCompactProgress(SVnode *pVnode, SRpcMsg *pMsg);
78

79
extern int32_t vnodeProcessKillRetentionReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
80
extern int32_t vnodeQueryRetentionProgress(SVnode *pVnode, SRpcMsg *pMsg);
81

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

84
extern int32_t vnodeListSsMigrateFileSets(SVnode *pVnode, SRpcMsg *pMsg);
85
static int32_t vnodeProcessSsMigrateFileSetReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
86
extern int32_t vnodeQuerySsMigrateProgress(SVnode *pVnode, SRpcMsg *pMsg);
87
static int32_t vnodeProcessFollowerSsMigrateReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
88
static int32_t vnodeProcessKillSsMigrateReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
89

90
static int32_t vnodePreprocessCreateTableReq(SVnode *pVnode, SDecoder *pCoder, int64_t btime, int64_t *pUid) {
67,076,719✔
91
  int32_t code = 0;
67,076,719✔
92
  int32_t lino = 0;
67,076,719✔
93

94
  if (tStartDecode(pCoder) < 0) {
67,076,719✔
95
    code = TSDB_CODE_INVALID_MSG;
×
96
    TSDB_CHECK_CODE(code, lino, _exit);
×
97
  }
98

99
  // flags
100
  if (tDecodeI32v(pCoder, NULL) < 0) {
67,078,703✔
101
    code = TSDB_CODE_INVALID_MSG;
×
102
    TSDB_CHECK_CODE(code, lino, _exit);
×
103
  }
104

105
  // name
106
  char *name = NULL;
67,078,703✔
107
  if (tDecodeCStr(pCoder, &name) < 0) {
67,080,104✔
108
    code = TSDB_CODE_INVALID_MSG;
×
109
    TSDB_CHECK_CODE(code, lino, _exit);
×
110
  }
111

112
  // uid
113
  int64_t uid = metaGetTableEntryUidByName(pVnode->pMeta, name);
67,080,104✔
114
  if (uid == 0) {
67,075,227✔
115
    uid = tGenIdPI64();
66,683,836✔
116
  }
117
  taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), uid);
67,059,266✔
118

119
  // btime
120
  taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos + 8), btime);
67,059,467✔
121

122
  tEndDecode(pCoder);
67,060,196✔
123

124
_exit:
67,069,842✔
125
  if (code) {
67,070,651✔
126
    vError("vgId:%d %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
127
  } else {
128
    vTrace("vgId:%d %s done, table:%s uid generated:%" PRId64, TD_VID(pVnode), __func__, name, uid);
67,070,651✔
129
    if (pUid) *pUid = uid;
67,050,089✔
130
  }
131
  return code;
67,050,475✔
132
}
133
static int32_t vnodePreProcessCreateTableMsg(SVnode *pVnode, SRpcMsg *pMsg) {
45,268,566✔
134
  int32_t code = 0;
45,268,566✔
135
  int32_t lino = 0;
45,268,566✔
136

137
  int64_t  btime = taosGetTimestampMs();
45,269,241✔
138
  SDecoder dc = {0};
45,269,241✔
139
  int32_t  nReqs;
45,266,769✔
140

141
  tDecoderInit(&dc, (uint8_t *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead));
45,269,241✔
142
  if (tStartDecode(&dc) < 0) {
45,269,241✔
143
    code = TSDB_CODE_INVALID_MSG;
×
144
    return code;
×
145
  }
146

147
  if (tDecodeI32v(&dc, &nReqs) < 0) {
45,269,279✔
148
    code = TSDB_CODE_INVALID_MSG;
×
149
    TSDB_CHECK_CODE(code, lino, _exit);
×
150
  }
151
  for (int32_t iReq = 0; iReq < nReqs; iReq++) {
96,179,581✔
152
    code = vnodePreprocessCreateTableReq(pVnode, &dc, btime, NULL);
50,924,279✔
153
    TSDB_CHECK_CODE(code, lino, _exit);
50,910,302✔
154
  }
155

156
  tEndDecode(&dc);
45,255,302✔
157

158
_exit:
45,268,717✔
159
  tDecoderClear(&dc);
45,267,852✔
160
  if (code) {
45,267,840✔
161
    vError("vgId:%d, %s:%d failed to preprocess submit request since %s, msg type:%s", TD_VID(pVnode), __func__, lino,
×
162
           tstrerror(code), TMSG_INFO(pMsg->msgType));
163
  }
164
  return code;
45,266,411✔
165
}
166

167
static int32_t vnodePreProcessAlterTableMsg(SVnode *pVnode, SRpcMsg *pMsg) {
12,649,804✔
168
  int32_t code = TSDB_CODE_INVALID_MSG;
12,649,804✔
169
  int32_t lino = 0;
12,649,804✔
170

171
  if (pVnode->config.isAudit) {
12,649,804✔
172
    return TSDB_CODE_PAR_PERMISSION_DENIED;
×
173
  }
174

175
  SDecoder dc = {0};
12,649,804✔
176
  tDecoderInit(&dc, (uint8_t *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead));
12,649,804✔
177

178
  SVAlterTbReq vAlterTbReq = {0};
12,649,804✔
179
  int64_t      ctimeMs = taosGetTimestampMs();
12,649,804✔
180
  code = tDecodeSVAlterTbReqSetCtime(&dc, &vAlterTbReq, ctimeMs);
12,649,804✔
181

182
  destroyAlterTbReq(&vAlterTbReq);
12,649,804✔
183

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

195
static int32_t vnodePreProcessDropTtlMsg(SVnode *pVnode, SRpcMsg *pMsg) {
11,439,556✔
196
  int32_t code = TSDB_CODE_INVALID_MSG;
11,439,556✔
197
  int32_t lino = 0;
11,439,556✔
198

199
  SMsgHead *pContOld = pMsg->pCont;
11,439,556✔
200
  int32_t   reqLenOld = pMsg->contLen - sizeof(SMsgHead);
11,441,716✔
201

202
  SArray *tbUids = NULL;
11,439,845✔
203
  int64_t timestampMs = 0;
11,439,845✔
204

205
  SVDropTtlTableReq ttlReq = {0};
11,439,845✔
206
  if (tDeserializeSVDropTtlTableReq((char *)pContOld + sizeof(SMsgHead), reqLenOld, &ttlReq) != 0) {
11,442,010✔
207
    code = TSDB_CODE_INVALID_MSG;
×
208
    TSDB_CHECK_CODE(code, lino, _exit);
×
209
  }
210

211
  {  // find expired uids
212
    tbUids = taosArrayInit(8, sizeof(tb_uid_t));
11,442,945✔
213
    if (tbUids == NULL) {
11,444,541✔
214
      code = terrno;
×
215
      TSDB_CHECK_CODE(code, lino, _exit);
×
216
    }
217

218
    timestampMs = (int64_t)ttlReq.timestampSec * 1000;
11,444,541✔
219
    code = metaTtlFindExpired(pVnode->pMeta, timestampMs, tbUids, ttlReq.ttlDropMaxCount);
11,444,541✔
220
    if (code != 0) {
11,430,610✔
221
      code = TSDB_CODE_INVALID_MSG;
×
222
      TSDB_CHECK_CODE(code, lino, _exit);
×
223
    }
224

225
    ttlReq.nUids = taosArrayGetSize(tbUids);
11,430,610✔
226
    ttlReq.pTbUids = tbUids;
11,434,241✔
227
  }
228

229
  if (ttlReq.nUids == 0) {
11,434,241✔
230
    code = TSDB_CODE_MSG_PREPROCESSED;
11,408,675✔
231
    TSDB_CHECK_CODE(code, lino, _exit);
11,408,675✔
232
  }
233

234
  {  // prepare new content
235
    int32_t reqLenNew = tSerializeSVDropTtlTableReq(NULL, 0, &ttlReq);
8,510✔
236
    int32_t contLenNew = reqLenNew + sizeof(SMsgHead);
12,183✔
237

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

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

250
    rpcFreeCont(pContOld);
12,183✔
251
    pMsg->pCont = pContNew;
12,183✔
252
    pMsg->contLen = contLenNew;
12,183✔
253
  }
254

255
  code = 0;
12,183✔
256

257
_exit:
11,437,914✔
258
  taosArrayDestroy(tbUids);
11,436,047✔
259

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

267
  return code;
11,416,054✔
268
}
269

270
extern int64_t tsMaxKeyByPrecision[];
271
static int32_t vnodePreProcessSubmitTbData(SVnode *pVnode, SDecoder *pCoder, int64_t btimeMs, int64_t ctimeMs) {
522,926,425✔
272
  int32_t code = 0;
522,926,425✔
273
  int32_t lino = 0;
522,926,425✔
274

275
  if (tStartDecode(pCoder) < 0) {
522,926,425✔
276
    code = TSDB_CODE_INVALID_MSG;
×
277
    TSDB_CHECK_CODE(code, lino, _exit);
×
278
  }
279

280
  SSubmitTbData submitTbData;
522,922,704✔
281
  uint8_t       version;
282
  if (tDecodeI32v(pCoder, &submitTbData.flags) < 0) {
522,925,939✔
283
    code = TSDB_CODE_INVALID_MSG;
×
284
    TSDB_CHECK_CODE(code, lino, _exit);
×
285
  }
286
  version = (submitTbData.flags >> 8) & 0xff;
522,925,939✔
287
  submitTbData.flags = submitTbData.flags & 0xff;
522,925,939✔
288

289
  int64_t uid;
522,921,317✔
290
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
522,926,066✔
291
    code = vnodePreprocessCreateTableReq(pVnode, pCoder, btimeMs, &uid);
16,144,536✔
292
    TSDB_CHECK_CODE(code, lino, _exit);
16,142,151✔
293
  }
294

295
  // submit data
296
  if (tDecodeI64(pCoder, &submitTbData.suid) < 0) {
522,928,883✔
297
    code = TSDB_CODE_INVALID_MSG;
×
298
    TSDB_CHECK_CODE(code, lino, _exit);
×
299
  }
300

301
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
522,928,883✔
302
    taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), uid);
16,144,244✔
303
    pCoder->pos += sizeof(int64_t);
16,142,647✔
304
  } else {
305
    if (tDecodeI64(pCoder, &submitTbData.uid) < 0) {
506,782,686✔
306
      code = TSDB_CODE_INVALID_MSG;
×
307
      TSDB_CHECK_CODE(code, lino, _exit);
×
308
    }
309
  }
310

311
  if (tDecodeI32v(pCoder, &submitTbData.sver) < 0) {
522,925,860✔
312
    code = TSDB_CODE_INVALID_MSG;
×
313
    TSDB_CHECK_CODE(code, lino, _exit);
×
314
  }
315

316
  // scan and check
317
  TSKEY now = btimeMs;
522,925,860✔
318
  if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_MICRO) {
522,925,860✔
319
    now *= 1000;
2,626,866✔
320
  } else if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_NANO) {
520,299,897✔
321
    now *= 1000000;
1,542,950✔
322
  }
323

324
  int32_t keep = pVnode->config.tsdbCfg.keep2;
522,926,675✔
325

326
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * keep;
522,922,584✔
327
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
522,925,516✔
328
  if (submitTbData.flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
522,919,914✔
329
    uint64_t nColData;
6,922,094✔
330
    if (tDecodeU64v(pCoder, &nColData) < 0) {
6,921,180✔
331
      code = TSDB_CODE_INVALID_MSG;
×
332
      TSDB_CHECK_CODE(code, lino, _exit);
×
333
    }
334

335
    SColData colData = {0};
6,921,180✔
336
    code = tDecodeColData(version, pCoder, &colData, false);
6,920,841✔
337
    if (code) {
6,921,864✔
338
      code = TSDB_CODE_INVALID_MSG;
×
339
      TSDB_CHECK_CODE(code, lino, _exit);
×
340
    }
341

342
    if (colData.flag != HAS_VALUE) {
6,921,864✔
343
      code = TSDB_CODE_INVALID_MSG;
×
344
      TSDB_CHECK_CODE(code, lino, _exit);
×
345
    }
346

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

354
    for (uint64_t i = 1; i < nColData; i++) {
21,334,512✔
355
      code = tDecodeColData(version, pCoder, &colData, true);
14,365,065✔
356
      if (code) {
14,364,726✔
357
        code = TSDB_CODE_INVALID_MSG;
×
358
        TSDB_CHECK_CODE(code, lino, _exit);
×
359
      }
360
    }
361
  } else {
362
    uint64_t nRow;
515,993,198✔
363
    if (tDecodeU64v(pCoder, &nRow) < 0) {
515,997,424✔
364
      code = TSDB_CODE_INVALID_MSG;
×
365
      TSDB_CHECK_CODE(code, lino, _exit);
×
366
    }
367

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

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

389
  if (!tDecodeIsEnd(pCoder)) {
522,850,534✔
390
    taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), ctimeMs);
522,853,997✔
391
    pCoder->pos += sizeof(int64_t);
522,847,264✔
392
  }
393

394
  tEndDecode(pCoder);
522,849,234✔
395

396
_exit:
522,921,942✔
397
  if (code) {
522,922,996✔
398
    vError("vgId:%d, %s:%d failed to vnodePreProcessSubmitTbData submit request since %s", TD_VID(pVnode), __func__,
70,087✔
399
           lino, tstrerror(code));
400
  }
401
  return code;
522,922,996✔
402
}
403
static int32_t vnodePreProcessSubmitMsg(SVnode *pVnode, SRpcMsg *pMsg) {
496,650,200✔
404
  int32_t code = 0;
496,650,200✔
405
  int32_t lino = 0;
496,650,200✔
406

407
  if (tsBypassFlag & TSDB_BYPASS_RA_RPC_RECV_SUBMIT) {
496,650,200✔
408
    return TSDB_CODE_MSG_PREPROCESSED;
84✔
409
  }
410

411
  SDecoder *pCoder = &(SDecoder){0};
496,650,116✔
412

413
  if (taosHton64(((SSubmitReq2Msg *)pMsg->pCont)->version) != 1) {
496,651,573✔
414
    code = TSDB_CODE_INVALID_MSG;
×
415
    TSDB_CHECK_CODE(code, lino, _exit);
×
416
  }
417

418
  tDecoderInit(pCoder, (uint8_t *)pMsg->pCont + sizeof(SSubmitReq2Msg), pMsg->contLen - sizeof(SSubmitReq2Msg));
496,653,287✔
419

420
  if (tStartDecode(pCoder) < 0) {
496,652,365✔
421
    code = TSDB_CODE_INVALID_MSG;
×
422
    TSDB_CHECK_CODE(code, lino, _exit);
×
423
  }
424

425
  uint64_t nSubmitTbData;
496,647,076✔
426
  if (tDecodeU64v(pCoder, &nSubmitTbData) < 0) {
496,650,691✔
427
    code = TSDB_CODE_INVALID_MSG;
×
428
    TSDB_CHECK_CODE(code, lino, _exit);
×
429
  }
430

431
  int64_t btimeMs = taosGetTimestampMs();
496,652,668✔
432
  int64_t ctimeMs = btimeMs;
496,652,668✔
433
  for (uint64_t i = 0; i < nSubmitTbData; i++) {
1,019,501,290✔
434
    code = vnodePreProcessSubmitTbData(pVnode, pCoder, btimeMs, ctimeMs);
522,925,864✔
435
    TSDB_CHECK_CODE(code, lino, _exit);
522,918,709✔
436
  }
437

438
  tEndDecode(pCoder);
496,575,426✔
439

440
_exit:
496,649,008✔
441
  tDecoderClear(pCoder);
496,650,094✔
442
  if (code) {
496,647,741✔
443
    vError("vgId:%d, %s:%d failed to preprocess submit request since %s, msg type:%s", TD_VID(pVnode), __func__, lino,
70,087✔
444
           tstrerror(code), TMSG_INFO(pMsg->msgType));
445
  }
446
  return code;
496,637,685✔
447
}
448

449
static int32_t vnodePreProcessDeleteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
1,799,033✔
450
  int32_t code = 0;
1,799,033✔
451

452
  int32_t    size;
453
  int32_t    ret;
454
  uint8_t   *pCont;
455
  SEncoder  *pCoder = &(SEncoder){0};
1,799,033✔
456
  SDeleteRes res = {0};
1,799,033✔
457

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

464
  code = qWorkerProcessDeleteMsg(&handle, pVnode->pQuery, pMsg, &res);
1,799,033✔
465
  if (code) goto _exit;
1,799,033✔
466

467
  res.ctimeMs = taosGetTimestampMs();
1,799,033✔
468
  // malloc and encode
469
  tEncodeSize(tEncodeDeleteRes, &res, size, ret);
1,799,033✔
470
  pCont = rpcMallocCont(size + sizeof(SMsgHead));
1,799,033✔
471

472
  ((SMsgHead *)pCont)->contLen = size + sizeof(SMsgHead);
1,798,332✔
473
  ((SMsgHead *)pCont)->vgId = TD_VID(pVnode);
1,798,332✔
474

475
  tEncoderInit(pCoder, pCont + sizeof(SMsgHead), size);
1,799,033✔
476
  if (tEncodeDeleteRes(pCoder, &res) != 0) {
1,799,033✔
477
    vError("vgId:%d %s failed to encode delete response", TD_VID(pVnode), __func__);
×
478
  }
479
  tEncoderClear(pCoder);
1,799,033✔
480

481
  rpcFreeCont(pMsg->pCont);
1,799,033✔
482
  pMsg->pCont = pCont;
1,799,033✔
483
  pMsg->contLen = size + sizeof(SMsgHead);
1,799,033✔
484

485
  taosArrayDestroy(res.uidList);
1,798,332✔
486

487
_exit:
1,799,033✔
488
  return code;
1,798,332✔
489
}
490

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

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

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

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

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

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

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

523
  SVArbCheckSyncReq syncReq = {0};
38,070✔
524

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

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

535
  int32_t code = terrno;
38,070✔
536
  tFreeSVArbCheckSyncReq(&syncReq);
38,070✔
537

538
  return code;
38,070✔
539
}
540

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

550
  if (pVnode->config.isAudit) {
1,699,572✔
551
    return TSDB_CODE_PAR_PERMISSION_DENIED;
×
552
  }
553

554
  tDecoderInit(&dc, POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), pMsg->contLen - sizeof(SMsgHead));
1,699,572✔
555

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

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

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

592
_exit:
1,699,572✔
593
  tDecoderClear(&dc);
1,699,572✔
594
  if (sentBatchReqs.pArray) {
1,699,572✔
595
    taosArrayDestroy(sentBatchReqs.pArray);
1,699,572✔
596
  }
597
  return code;
1,699,572✔
598
}
599

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

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

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

615
_exit:
3,810✔
616
  return code;
3,810✔
617
}
618

619
int32_t vnodePreProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
593,580,926✔
620
  int32_t code = 0;
593,580,926✔
621

622
  if (pVnode->mounted) {
593,580,926✔
623
    vError("vgId:%d, mountVgId:%d, skip write msg:%s", TD_VID(pVnode), pVnode->config.mountVgId,
1,560✔
624
           TMSG_INFO(pMsg->msgType));
625
    return TSDB_CODE_OPS_NOT_SUPPORT;
1,560✔
626
  }
627

628
  switch (pMsg->msgType) {
593,579,346✔
629
    case TDMT_VND_CREATE_TABLE: {
45,269,578✔
630
      code = vnodePreProcessCreateTableMsg(pVnode, pMsg);
45,269,578✔
631
    } break;
45,267,152✔
632
    case TDMT_VND_ALTER_TABLE: {
12,649,804✔
633
      code = vnodePreProcessAlterTableMsg(pVnode, pMsg);
12,649,804✔
634
    } break;
12,649,804✔
635
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
11,443,728✔
636
    case TDMT_VND_DROP_TTL_TABLE: {
637
      code = vnodePreProcessDropTtlMsg(pVnode, pMsg);
11,443,728✔
638
    } break;
11,423,979✔
639
    case TDMT_VND_SUBMIT: {
496,651,121✔
640
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.preprocess_time, METRIC_LEVEL_LOW,
496,651,121✔
641
                           { code = vnodePreProcessSubmitMsg(pVnode, pMsg); });
642
    } break;
496,641,527✔
643
    case TDMT_VND_DELETE: {
1,799,033✔
644
      code = vnodePreProcessDeleteMsg(pVnode, pMsg);
1,799,033✔
645
    } break;
1,798,332✔
646
    case TDMT_VND_BATCH_DEL: {
×
647
      code = vnodePreProcessBatchDeleteMsg(pVnode, pMsg);
×
648
    } break;
×
649
    case TDMT_VND_ARB_CHECK_SYNC: {
67,568✔
650
      code = vnodePreProcessArbCheckSyncMsg(pVnode, pMsg);
67,568✔
651
    } break;
67,568✔
652
    case TDMT_VND_DROP_TABLE: {
1,699,572✔
653
      code = vnodePreProcessDropTbMsg(pVnode, pMsg);
1,699,572✔
654
    } break;
1,699,572✔
655
#ifdef TD_ENTERPRISE
656
    case TDMT_VND_SSMIGRATE_FILESET: {
3,810✔
657
      code = vnodePreProcessSsMigrateFileSetReq(pVnode, pMsg);
3,810✔
658
    } break;
3,810✔
659
#endif
660
    case TDMT_VND_AUDIT_RECORD: {
548✔
661
      vTrace("vgId:%d, pre process TDMT_VND_AUDIT_RECORD", TD_VID(pVnode));
548✔
662
    } break;
292✔
663
    default:
23,993,689✔
664
      break;
23,993,689✔
665
  }
666

667
  if (code && code != TSDB_CODE_MSG_PREPROCESSED) {
593,545,725✔
668
    vError("vgId:%d, failed to preprocess write request since %s, msg type:%s", TD_VID(pVnode), tstrerror(code),
105,191✔
669
           TMSG_INFO(pMsg->msgType));
670
  }
671
  return code;
593,555,973✔
672
}
673

674
static int32_t inline vnodeSubmitSubRowBlobData(SVnode *pVnode, SSubmitTbData *pSubmitTbData) {
46,518✔
675
  int32_t code = 0;
46,518✔
676
  int32_t lino = 0;
46,518✔
677

678
  int64_t   st = taosGetTimestampUs();
46,518✔
679
  SBlobSet *pBlobSet = pSubmitTbData->pBlobSet;
46,518✔
680
  int32_t   sz = taosArrayGetSize(pBlobSet->pSeqTable);
46,518✔
681

682
  SBseBatch *pBatch = NULL;
46,518✔
683

684
  code = bseBatchInit(pVnode->pBse, &pBatch, sz);
46,518✔
685
  TSDB_CHECK_CODE(code, lino, _exit);
46,518✔
686

687
  SRow  **pRow = (SRow **)TARRAY_DATA(pSubmitTbData->aRowP);
46,518✔
688
  int32_t rowIdx = -1;
46,518✔
689
  for (int32_t i = 0; i < sz; i++) {
25,997,649✔
690
    int64_t     seq = 0;
25,951,131✔
691
    SBlobValue *p = taosArrayGet(pBlobSet->pSeqTable, i);
25,951,131✔
692
    if (p->type == TSDB_DATA_BLOB_EMPTY_VALUE || p->type == TSDB_DATA_BLOB_NULL_VALUE) {
25,951,131✔
693
      // skip empty or null blob
694
      continue;
2,577,429✔
695
    }
696

697
    code = bseBatchPut(pBatch, &seq, pBlobSet->data + p->offset, p->len);
23,373,702✔
698
    TSDB_CHECK_CODE(code, lino, _exit);
23,373,702✔
699

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

707
    if (tPutU64(row->data + p->dataOffset, seq) < 0) {
46,747,404✔
708
      code = TSDB_CODE_INVALID_MSG;
×
709
      TSDB_CHECK_CODE(code, lino, _exit);
×
710
    }
711
  }
712

713
  code = bseCommitBatch(pVnode->pBse, pBatch);
46,518✔
714
  TSDB_CHECK_CODE(code, lino, _exit);
46,518✔
715

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

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

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

738
  SBseBatch *pBatch = NULL;
155✔
739

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

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

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

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

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

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

796
  return code;
46,673✔
797
}
798

799
int32_t vnodeProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg, int64_t ver, SRpcMsg *pRsp) {
726,049,486✔
800
  int32_t code = 0;
726,049,486✔
801
  int32_t lino = 0;
726,049,486✔
802
  void   *ptr = NULL;
726,049,486✔
803
  void   *pReq;
804
  int32_t len;
805

806
  (void)taosThreadMutexLock(&pVnode->mutex);
726,049,486✔
807
  if (pVnode->disableWrite) {
726,119,513✔
808
    (void)taosThreadMutexUnlock(&pVnode->mutex);
×
809
    vError("vgId:%d write is disabled for snapshot, version:%" PRId64, TD_VID(pVnode), ver);
×
810
    return TSDB_CODE_VND_WRITE_DISABLED;
×
811
  }
812
  (void)taosThreadMutexUnlock(&pVnode->mutex);
726,051,923✔
813

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

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

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

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

838
  atomic_store_64(&pVnode->state.applied, ver);
726,138,413✔
839
  atomic_store_64(&pVnode->state.applyTerm, pMsg->info.conn.applyTerm);
726,140,543✔
840

841
  if (!syncUtilUserCommit(pMsg->msgType)) goto _exit;
726,141,465✔
842

843
  // skip header
844
  pReq = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
715,193,779✔
845
  len = pMsg->contLen - sizeof(SMsgHead);
715,193,779✔
846
  bool needCommit = false;
715,192,174✔
847
  bool forceTrimWal = false;
715,192,174✔
848

849
  switch (pMsg->msgType) {
715,192,174✔
850
    /* META */
851
    case TDMT_VND_CREATE_STB:
5,593,148✔
852
      code = vnodeProcessCreateStbReq(pVnode, ver, pReq, len, pRsp);
5,593,148✔
853
      TSDB_CHECK_CODE(code, lino, _err);
5,593,232✔
854
      break;
5,590,080✔
855
    case TDMT_VND_ALTER_STB:
8,056,354✔
856
      code = vnodeProcessAlterStbReq(pVnode, ver, pReq, len, pRsp);
8,056,354✔
857
      TSDB_CHECK_CODE(code, lino, _err);
8,055,987✔
858
      break;
8,055,987✔
859
    case TDMT_VND_DROP_STB:
955,049✔
860
      code = vnodeProcessDropStbReq(pVnode, ver, pReq, len, pRsp, pMsg);
955,049✔
861
      TSDB_CHECK_CODE(code, lino, _err);
955,049✔
862
      break;
955,049✔
863
    case TDMT_VND_CREATE_TABLE:
48,259,653✔
864
      code = vnodeProcessCreateTbReq(pVnode, ver, pReq, len, pRsp, pMsg);
48,259,653✔
865
      TSDB_CHECK_CODE(code, lino, _err);
48,252,533✔
866
      break;
48,252,533✔
867
    case TDMT_VND_ALTER_TABLE:
12,650,451✔
868
      code = vnodeProcessAlterTbReq(pVnode, ver, pReq, len, pRsp);
12,650,451✔
869
      TSDB_CHECK_CODE(code, lino, _err);
12,650,451✔
870
      break;
12,650,451✔
871
    case TDMT_VND_DROP_TABLE:
1,699,656✔
872
    case TDMT_VND_SNODE_DROP_TABLE:
873
      code = vnodeProcessDropTbReq(pVnode, ver, pReq, len, pRsp, pMsg);
1,699,656✔
874
      TSDB_CHECK_CODE(code, lino, _err);
1,699,656✔
875
      break;
1,699,656✔
876
    case TDMT_VND_CREATE_RSMA:
40,506✔
877
      code = vnodeProcessCreateRsmaReq(pVnode, ver, pReq, len, pRsp);
40,506✔
878
      TSDB_CHECK_CODE(code, lino, _err);
40,506✔
879
      break;
40,506✔
880
    case TDMT_VND_DROP_RSMA:
20,400✔
881
      code = vnodeProcessDropRsmaReq(pVnode, ver, pReq, len, pRsp);
20,400✔
882
      TSDB_CHECK_CODE(code, lino, _err);
20,400✔
883
      break;
20,400✔
884
    case TDMT_VND_ALTER_RSMA:
12,486✔
885
      code = vnodeProcessAlterRsmaReq(pVnode, ver, pReq, len, pRsp);
12,486✔
886
      TSDB_CHECK_CODE(code, lino, _err);
12,486✔
887
      break;
12,486✔
888
    case TDMT_VND_DROP_TTL_TABLE:
×
889
      code = vnodeProcessDropTtlTbReq(pVnode, ver, pReq, len, pRsp);
×
890
      TSDB_CHECK_CODE(code, lino, _err);
×
891
      break;
×
892
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
12,183✔
893
      code = vnodeProcessFetchTtlExpiredTbs(pVnode, ver, pReq, len, pRsp);
12,183✔
894
      TSDB_CHECK_CODE(code, lino, _err);
12,183✔
895
      break;
12,183✔
896
    case TDMT_VND_TRIM:
68,803✔
897
      code = vnodeProcessTrimReq(pVnode, ver, pReq, len, pRsp);
68,803✔
898
      TSDB_CHECK_CODE(code, lino, _err);
68,803✔
899
      break;
68,803✔
900
      case TDMT_VND_TRIM_WAL:
6,888✔
901
        needCommit = true;
6,888✔
902
        forceTrimWal = true;
6,888✔
903
        break;
6,888✔
904
#ifdef TD_ENTERPRISE
905
    case TDMT_VND_SSMIGRATE_FILESET:
3,810✔
906
      if (vnodeProcessSsMigrateFileSetReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
3,810✔
907
      break;
3,810✔
908
    case TDMT_VND_FOLLOWER_SSMIGRATE:
6,858✔
909
      if (vnodeProcessFollowerSsMigrateReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
6,858✔
910
      break;
6,858✔
911
    case TDMT_VND_KILL_SSMIGRATE:
×
912
      if (vnodeProcessKillSsMigrateReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
913
      break;
×
914
#endif
915

916
    /* TSDB */
917
    case TDMT_VND_SUBMIT: {
614,516,426✔
918
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.apply_time, METRIC_LEVEL_LOW, {
614,516,426✔
919
        if (vnodeProcessSubmitReq(pVnode, ver, pMsg->pCont, pMsg->contLen, pRsp, pMsg) < 0) goto _err;
920
      });
921
      METRICS_UPDATE(pVnode->writeMetrics.apply_bytes, METRIC_LEVEL_LOW, (int64_t)pMsg->contLen);
614,464,114✔
922
      break;
614,453,094✔
923
    }
924
    case TDMT_VND_DELETE:
2,203,545✔
925
      code = vnodeProcessDeleteReq(pVnode, ver, pReq, len, pRsp, pMsg);
2,203,545✔
926
      TSDB_CHECK_CODE(code, lino, _err);
2,203,545✔
927
      break;
2,203,545✔
928
    case TDMT_VND_BATCH_DEL:
×
929
      code = vnodeProcessBatchDeleteReq(pVnode, ver, pReq, len, pRsp);
×
930
      TSDB_CHECK_CODE(code, lino, _err);
×
931
      break;
×
932
      /* TQ */
933
#if defined(USE_TQ) || defined(USE_STREAM)
934
    case TDMT_VND_TMQ_SUBSCRIBE:
1,254,834✔
935
      code = tqProcessSubscribeReq(pVnode->pTq, ver, pReq, len);
1,254,834✔
936
      TSDB_CHECK_CODE(code, lino, _err);
1,252,364✔
937
      break;
1,252,364✔
938
    case TDMT_VND_TMQ_DELETE_SUB:
286,373✔
939
      code = tqProcessDeleteSubReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen);
286,373✔
940
      TSDB_CHECK_CODE(code, lino, _err);
285,655✔
941
      break;
285,655✔
942
    case TDMT_VND_TMQ_COMMIT_OFFSET:
9,689,859✔
943
      code = tqProcessOffsetCommitReq(pVnode->pTq, ver, pReq, len);
9,689,859✔
944
      TSDB_CHECK_CODE(code, lino, _err);
9,688,165✔
945
      break;
9,688,165✔
946
#endif
947
    case TDMT_VND_ALTER_CONFIRM:
4,145,917✔
948
      needCommit = pVnode->config.hashChange;
4,145,917✔
949
      code = vnodeProcessAlterConfirmReq(pVnode, ver, pReq, len, pRsp);
4,145,917✔
950
      TSDB_CHECK_CODE(code, lino, _err);
4,145,917✔
951
      break;
4,145,917✔
952
    case TDMT_VND_ALTER_CONFIG:
330,958✔
953
      vnodeProcessAlterConfigReq(pVnode, ver, pReq, len, pRsp);
330,958✔
954
      break;
330,958✔
955
    case TDMT_VND_COMMIT:
5,260,802✔
956
      needCommit = true;
5,260,802✔
957
      break;
5,260,802✔
958
    case TDMT_VND_CREATE_INDEX:
18,899✔
959
      vnodeProcessCreateIndexReq(pVnode, ver, pReq, len, pRsp);
18,899✔
960
      break;
18,899✔
961
    case TDMT_VND_DROP_INDEX:
15,388✔
962
      vnodeProcessDropIndexReq(pVnode, ver, pReq, len, pRsp);
15,388✔
963
      break;
15,388✔
964
    case TDMT_VND_COMPACT:
74,135✔
965
      vnodeProcessCompactVnodeReq(pVnode, ver, pReq, len, pRsp);
74,135✔
966
      goto _exit;
74,135✔
967
    case TDMT_VND_SCAN:
589✔
968
      vnodeProcessScanVnodeReq(pVnode, ver, pReq, len, pRsp);
589✔
969
      goto _exit;
589✔
970
    case TDMT_SYNC_CONFIG_CHANGE:
×
971
      vnodeProcessConfigChangeReq(pVnode, ver, pReq, len, pRsp);
×
972
      break;
×
973
#ifdef TD_ENTERPRISE
974
    case TDMT_VND_KILL_COMPACT:
1,576✔
975
      vnodeProcessKillCompactReq(pVnode, ver, pReq, len, pRsp);
1,576✔
976
      break;
1,576✔
977
#endif
978
    case TDMT_VND_KILL_SCAN:
118✔
979
      vnodeProcessKillScanReq(pVnode, ver, pReq, len, pRsp);
118✔
980
      break;
118✔
981
    case TDMT_VND_KILL_TRIM:
×
982
      vnodeProcessKillRetentionReq(pVnode, ver, pReq, len, pRsp);
×
983
      break;
×
984
    /* ARB */
985
    case TDMT_VND_ARB_CHECK_SYNC:
5,936✔
986
      vnodeProcessArbCheckSyncReq(pVnode, pReq, len, pRsp);
5,936✔
987
      break;
5,936✔
988
    case TDMT_VND_AUDIT_RECORD:
292✔
989
      vTrace("vgId:%d, processed audit msg", TD_VID(pVnode));
292✔
990
      code = vnodeProcessAuditRecordReq(pVnode, ver, pReq, len, pMsg);
292✔
991
      pRsp->code = code;
292✔
992
      pRsp->msgType = TDMT_VND_AUDIT_RECORD_RSP;
292✔
993
      pRsp->pCont = NULL;
292✔
994
      pRsp->contLen = 0;
292✔
995
      if (code) {
292✔
996
        goto _err;
×
997
      }
998
      break;
292✔
999
    default:
×
1000
      vError("vgId:%d, unprocessed msg, %d", TD_VID(pVnode), pMsg->msgType);
×
1001
      return TSDB_CODE_INVALID_MSG;
×
1002
  }
1003

1004
  vGDebug(&pMsg->info.traceId, "vgId:%d, index:%" PRId64 ", msg processed, code:0x%x", TD_VID(pVnode), ver, pRsp->code);
715,038,399✔
1005

1006
  walApplyVer(pVnode->pWal, ver);
715,056,489✔
1007

1008
  // commit if need
1009
  if (needCommit) {
715,070,626✔
1010
    vInfo("vgId:%d, commit at version %" PRId64, TD_VID(pVnode), ver);
5,293,903✔
1011
    code = vnodeAsyncCommit(pVnode, forceTrimWal);
5,294,247✔
1012
    if (code) {
5,294,247✔
1013
      vError("vgId:%d, failed to vnode async commit since %s.", TD_VID(pVnode), tstrerror(terrno));
×
1014
      goto _err;
×
1015
    }
1016

1017
    // start a new one
1018
    METRICS_TIMING_BLOCK(pVnode->writeMetrics.memtable_wait_time, METRIC_LEVEL_LOW, {
5,294,247✔
1019
      code = vnodeBegin(pVnode);
1020
      if (code) {
1021
        vError("vgId:%d, failed to begin vnode since %s.", TD_VID(pVnode), tstrerror(terrno));
1022
        goto _err;
1023
      }
1024
    });
1025
  }
1026

1027
_exit:
715,069,835✔
1028
  return 0;
726,084,901✔
1029

1030
_err:
48,663✔
1031
  if (code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
48,663✔
1032
    vInfo("vgId:%d, process %s request failed since %s, lino 1:%d ver:%" PRId64, TD_VID(pVnode),
3,152✔
1033
          TMSG_INFO(pMsg->msgType), tstrerror(code), lino, ver);
1034
  } else if (code) {
45,511✔
1035
    vError("vgId:%d, process %s request failed since %s, lino 2:%d ver:%" PRId64, TD_VID(pVnode),
×
1036
           TMSG_INFO(pMsg->msgType), tstrerror(code), lino, ver);
1037
  }
1038

1039
  return code;
48,663✔
1040
}
1041

1042
int32_t vnodePreprocessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg) {
402,320,021✔
1043
  if (TDMT_SCH_QUERY != pMsg->msgType && TDMT_SCH_MERGE_QUERY != pMsg->msgType) {
402,320,021✔
1044
    return 0;
57,066,788✔
1045
  }
1046

1047
  int32_t qType = 0;
345,279,230✔
1048
  return qWorkerPreprocessQueryMsg(pVnode->pQuery, pMsg, (TDMT_SCH_QUERY == pMsg->msgType), &qType);
345,294,178✔
1049
}
1050

1051
int32_t vnodeProcessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
402,377,687✔
1052
  const STraceId *trace = &pMsg->info.traceId;
402,377,687✔
1053
  vDebug("vgId:%d, msg:%s, %p in vnode query queue is processing, %" PRIx64 ":%" PRIx64, 
402,378,723✔
1054
    pVnode->config.vgId, TMSG_INFO(pMsg->msgType), pMsg, TRACE_GET_ROOTID(trace), TRACE_GET_MSGID(trace));
1055

1056
  if (pMsg->msgType == TDMT_VND_TMQ_CONSUME && !syncIsReadyForRead(pVnode->sync)) {
402,386,513✔
1057
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
400,729✔
1058
    return 0;
400,729✔
1059
  }
1060

1061
  if (pMsg->msgType == TDMT_VND_TMQ_CONSUME && !pVnode->restored) {
401,989,212✔
1062
    vnodeRedirectRpcMsg(pVnode, pMsg, TSDB_CODE_SYN_RESTORING);
×
1063
    return 0;
×
1064
  }
1065

1066
  SReadHandle handle = {0};
401,987,832✔
1067
  handle.vnode = pVnode;
401,989,680✔
1068
  handle.pMsgCb = &pVnode->msgCb;
401,989,680✔
1069
  handle.pWorkerCb = pInfo->workerCb;
401,969,351✔
1070
  initStorageAPI(&handle.api);
401,971,995✔
1071
  int32_t code = TSDB_CODE_SUCCESS;
401,878,205✔
1072
  bool    redirected = false;
401,878,205✔
1073

1074
  switch (pMsg->msgType) {
401,878,205✔
1075
    case TDMT_SCH_QUERY:
284,007,519✔
1076
      if (!syncIsReadyForRead(pVnode->sync)) {
284,007,519✔
1077
        pMsg->code = (terrno) ? terrno : TSDB_CODE_SYN_NOT_LEADER;
1,998,436✔
1078
        redirected = true;
1,998,436✔
1079
      }
1080
      code = qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
284,042,196✔
1081
      if (redirected) {
284,012,963✔
1082
        vnodeRedirectRpcMsg(pVnode, pMsg, pMsg->code);
1,998,436✔
1083
        return 0;
1,998,436✔
1084
      }
1085

1086
      return code;
282,014,527✔
1087
    case TDMT_SCH_MERGE_QUERY:
61,266,205✔
1088
      return qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
61,266,205✔
1089
    case TDMT_SCH_QUERY_CONTINUE:
41,109,455✔
1090
      return qWorkerProcessCQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
41,109,455✔
1091
    case TDMT_VND_TMQ_CONSUME:
15,557,200✔
1092
      return tqProcessPollReq(pVnode->pTq, pMsg);
15,557,200✔
1093
    default:
×
1094
      vError("unknown msg type:%d in query queue", pMsg->msgType);
×
1095
      return TSDB_CODE_APP_ERROR;
×
1096
  }
1097
}
1098

1099
int32_t vnodeProcessFetchMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
1,044,570,839✔
1100
  int32_t code = TSDB_CODE_SUCCESS;
1,044,570,839✔
1101
  const STraceId *trace = &pMsg->info.traceId;
1,044,570,839✔
1102
  vDebug("vgId:%d, msg:%s, %p in fetch queue is processing, %" PRIx64 ":%" PRIx64, 
1,044,616,235✔
1103
    pVnode->config.vgId, TMSG_INFO(pMsg->msgType), pMsg, TRACE_GET_ROOTID(trace), TRACE_GET_MSGID(trace));
1104
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
1,044,735,947✔
1105
       pMsg->msgType == TDMT_VND_BATCH_META || pMsg->msgType == TDMT_VND_TABLE_NAME ||
732,013,696✔
1106
       pMsg->msgType == TDMT_VND_VSUBTABLES_META || pMsg->msgType == TDMT_VND_VSTB_REF_DBS) &&
702,709,974✔
1107
      !syncIsReadyForRead(pVnode->sync)) {
342,019,378✔
1108
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
392,065✔
1109
    return 0;
392,065✔
1110
  }
1111

1112
  switch (pMsg->msgType) {
1,044,351,859✔
1113
    case TDMT_SCH_FETCH:
494,023,884✔
1114
    case TDMT_SCH_MERGE_FETCH:
1115
      return qWorkerProcessFetchMsg(pVnode, pVnode->pQuery, pMsg, 0);
494,023,884✔
1116
    case TDMT_SCH_FETCH_RSP:
×
1117
      return qWorkerProcessRspMsg(pVnode, pVnode->pQuery, pMsg, 0);
×
1118
    // case TDMT_SCH_CANCEL_TASK:
1119
    //   return qWorkerProcessCancelMsg(pVnode, pVnode->pQuery, pMsg, 0);
1120
    case TDMT_SCH_DROP_TASK:
345,036,895✔
1121
      return qWorkerProcessDropMsg(pVnode, pVnode->pQuery, pMsg, 0);
345,036,895✔
1122
    case TDMT_SCH_TASK_NOTIFY:
30,983✔
1123
      return qWorkerProcessNotifyMsg(pVnode, pVnode->pQuery, pMsg, 0);
30,983✔
1124
    case TDMT_SCH_QUERY_HEARTBEAT:
174,806,869✔
1125
      return qWorkerProcessHbMsg(pVnode, pVnode->pQuery, pMsg, 0);
174,806,869✔
1126
    case TDMT_VND_TABLE_META:
1,288,986✔
1127
    case TDMT_VND_TABLE_NAME:
1128
      return vnodeGetTableMeta(pVnode, pMsg, true);
1,288,986✔
1129
    case TDMT_VND_TABLE_CFG:
×
1130
      return vnodeGetTableCfg(pVnode, pMsg, true);
×
1131
    case TDMT_VND_BATCH_META: {
28,911,382✔
1132
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.fetch_batch_meta_time, METRIC_LEVEL_LOW,
28,911,382✔
1133
                           { code = vnodeGetBatchMeta(pVnode, pMsg); });
1134
      METRICS_UPDATE(pVnode->writeMetrics.fetch_batch_meta_count, METRIC_LEVEL_LOW, 1);
28,916,382✔
1135
      return code;
28,914,767✔
1136
    }
1137
    case TDMT_VND_VSUBTABLES_META:
×
1138
      return vnodeGetVSubtablesMeta(pVnode, pMsg);
×
1139
    case TDMT_VND_VSTB_REF_DBS:
×
1140
      return vnodeGetVStbRefDbs(pVnode, pMsg);
×
1141
    case TDMT_VND_QUERY_SCAN_PROGRESS:
766✔
1142
      return vnodeQueryScanProgress(pVnode, pMsg);
766✔
1143
#ifdef TD_ENTERPRISE
1144
    case TDMT_VND_QUERY_COMPACT_PROGRESS:
108,292✔
1145
      return vnodeQueryCompactProgress(pVnode, pMsg);
108,292✔
1146

1147
    case TDMT_VND_LIST_SSMIGRATE_FILESETS:
4,572✔
1148
      return vnodeListSsMigrateFileSets(pVnode, pMsg);
4,572✔
1149

1150
    case TDMT_VND_QUERY_SSMIGRATE_PROGRESS:
6,858✔
1151
      return vnodeQuerySsMigrateProgress(pVnode, pMsg);
6,858✔
1152
#endif
1153
    case TDMT_VND_QUERY_TRIM_PROGRESS:
98,848✔
1154
      return vnodeQueryRetentionProgress(pVnode, pMsg);
98,848✔
1155
      //    case TDMT_VND_TMQ_CONSUME:
1156
      //      return tqProcessPollReq(pVnode->pTq, pMsg);
1157
#ifdef USE_TQ
1158
    case TDMT_VND_TMQ_VG_WALINFO:
664✔
1159
      return tqProcessVgWalInfoReq(pVnode->pTq, pMsg);
664✔
1160
    case TDMT_VND_TMQ_VG_COMMITTEDINFO:
332✔
1161
      return tqProcessVgCommittedInfoReq(pVnode->pTq, pMsg);
332✔
1162
    case TDMT_VND_TMQ_SEEK:
1,328✔
1163
      return tqProcessSeekReq(pVnode->pTq, pMsg);
1,328✔
1164
#endif
1165
    default:
17,999✔
1166
      vError("unknown msg type:%d in fetch queue", pMsg->msgType);
17,999✔
1167
      return TSDB_CODE_APP_ERROR;
×
1168
  }
1169
}
1170

1171
void vnodeUpdateMetaRsp(SVnode *pVnode, STableMetaRsp *pMetaRsp) {
76,613,226✔
1172
  if (NULL == pMetaRsp) {
76,613,226✔
1173
    return;
×
1174
  }
1175

1176
  tstrncpy(pMetaRsp->dbFName, pVnode->config.dbname, TSDB_DB_FNAME_LEN);
76,613,226✔
1177
  pMetaRsp->dbId = pVnode->config.dbId;
76,616,043✔
1178
  pMetaRsp->vgId = TD_VID(pVnode);
76,613,712✔
1179
  pMetaRsp->precision = pVnode->config.tsdbCfg.precision;
76,615,898✔
1180
}
1181

1182
extern int32_t vnodeAsyncRetention(SVnode *pVnode, STimeWindow tw, int8_t optrType, int8_t triggerType);
1183

1184
static int32_t vnodeProcessTrimReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
68,803✔
1185
  if (!pVnode->restored) {
68,803✔
1186
    vInfo("vgId:%d, ignore trim req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
3,173✔
1187
    return 0;
3,173✔
1188
  }
1189

1190
  int32_t     code = 0;
65,630✔
1191
  SVTrimDbReq trimReq = {0};
65,630✔
1192

1193
  // decode
1194
  if (tDeserializeSVTrimDbReq(pReq, len, &trimReq) != 0) {
65,630✔
1195
    code = TSDB_CODE_INVALID_MSG;
×
1196
    goto _exit;
×
1197
  }
1198

1199
  vInfo("vgId:%d, process trim vnode request, time:%" PRIi64 ",%" PRIi64 ", optr:%d, trigger:%d", pVnode->config.vgId,
65,630✔
1200
        trimReq.tw.skey, trimReq.tw.ekey, (int32_t)trimReq.optrType, (int32_t)trimReq.triggerType);
1201

1202
  code = vnodeAsyncRetention(pVnode, trimReq.tw, (int8_t)trimReq.optrType, (int8_t)trimReq.triggerType);
65,630✔
1203

1204
_exit:
65,630✔
1205
  return code;
65,630✔
1206
}
1207

1208
extern int32_t vnodeAsyncSsMigrateFileSet(SVnode *pVnode, SSsMigrateFileSetReq *pReq);
1209

1210
extern int32_t vnodeAsyncS3Migrate(SVnode *pVnode, int64_t now);
1211

1212
static int32_t vnodeProcessSsMigrateFileSetReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
3,810✔
1213
  int32_t code = 0;
3,810✔
1214

1215
  SSsMigrateFileSetReq req = {0};
3,810✔
1216
  SSsMigrateFileSetRsp rsp = {0};
3,810✔
1217
  pRsp->msgType = TDMT_VND_SSMIGRATE_FILESET_RSP;
3,810✔
1218
  pRsp->code = 0;
3,810✔
1219
  pRsp->pCont = NULL;
3,810✔
1220
  pRsp->contLen = 0;
3,810✔
1221

1222
  if (tDeserializeSSsMigrateFileSetReq(pReq, len, &req) != 0) {
3,810✔
1223
    code = TSDB_CODE_INVALID_MSG;
×
1224
    goto _exit;
×
1225
  }
1226

1227
  vInfo("vgId:%d, ssmigrate:%d, fid:%d, process ssmigrate fileset request, time:%" PRId64, pVnode->config.vgId,
3,810✔
1228
        req.ssMigrateId, req.fid, req.startTimeSec);
1229
  rsp.ssMigrateId = req.ssMigrateId;
3,810✔
1230
  rsp.vgId = TD_VID(pVnode);
3,810✔
1231
  rsp.nodeId = req.nodeId;
3,810✔
1232
  rsp.fid = req.fid;
3,810✔
1233

1234
  code = vnodeAsyncSsMigrateFileSet(pVnode, &req);
3,810✔
1235
  if (code != TSDB_CODE_SUCCESS) {
3,810✔
1236
    vError("vgId:%d, failed to async ssmigrate since %s", TD_VID(pVnode), tstrerror(code));
×
1237
    pRsp->code = code;
×
1238
    goto _exit;
×
1239
  }
1240

1241
  pRsp->code = TSDB_CODE_SUCCESS;
3,810✔
1242
  pRsp->contLen = tSerializeSSsMigrateFileSetRsp(NULL, 0, &rsp);
3,810✔
1243
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
3,810✔
1244
  if (pRsp->pCont == NULL) {
3,810✔
1245
    vError("vgId:%d, failed to allocate memory for ssmigrate fileset response", TD_VID(pVnode));
×
1246
    code = TSDB_CODE_OUT_OF_MEMORY;
×
1247
    goto _exit;
×
1248
  }
1249
  TAOS_UNUSED(tSerializeSSsMigrateFileSetRsp(pRsp->pCont, pRsp->contLen, &rsp));
3,810✔
1250

1251
_exit:
3,810✔
1252
  pRsp->code = code;
3,810✔
1253
  return code;
3,810✔
1254
}
1255

1256
extern int32_t vnodeFollowerSsMigrate(SVnode *pVnode, SSsMigrateProgress *pReq);
1257

1258
static int32_t vnodeProcessFollowerSsMigrateReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
6,858✔
1259
  int32_t            code = 0;
6,858✔
1260
  SSsMigrateProgress req = {0};
6,858✔
1261

1262
  // decode
1263
  if (tDeserializeSSsMigrateProgress(pReq, len, &req) != 0) {
6,858✔
1264
    code = TSDB_CODE_INVALID_MSG;
×
1265
    goto _exit;
×
1266
  }
1267

1268
  code = vnodeFollowerSsMigrate(pVnode, &req);
6,858✔
1269

1270
_exit:
6,858✔
1271
  pRsp->code = code;
6,858✔
1272
  return code;
6,858✔
1273
}
1274

1275
extern int32_t vnodeKillSsMigrate(SVnode *pVnode, SVnodeKillSsMigrateReq *pReq);
1276

1277
static int32_t vnodeProcessKillSsMigrateReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1278
  int32_t          code = 0;
×
1279
  SVnodeKillSsMigrateReq req = {0};
×
1280

1281
  // decode
1282
  if (tDeserializeSVnodeKillSsMigrateReq(pReq, len, &req) != 0) {
×
1283
    code = TSDB_CODE_INVALID_MSG;
×
1284
    goto _exit;
×
1285
  }
1286

1287
  code = vnodeKillSsMigrate(pVnode, &req);
×
1288

1289
_exit:
×
1290
  pRsp->code = code;
×
1291
  return code;
×
1292
}
1293

1294
static int32_t vnodeProcessDropTtlTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1295
  int               ret = 0;
×
1296
  SVDropTtlTableReq ttlReq = {0};
×
1297
  if (tDeserializeSVDropTtlTableReq(pReq, len, &ttlReq) != 0) {
×
1298
    ret = TSDB_CODE_INVALID_MSG;
×
1299
    goto end;
×
1300
  }
1301

1302
  if (ttlReq.nUids != taosArrayGetSize(ttlReq.pTbUids)) {
×
1303
    ret = TSDB_CODE_INVALID_MSG;
×
1304
    goto end;
×
1305
  }
1306

1307
  if (ttlReq.nUids != 0) {
×
1308
    vInfo("vgId:%d, process drop ttl table request, time:%d, ntbUids:%d", pVnode->config.vgId, ttlReq.timestampSec,
×
1309
          ttlReq.nUids);
1310
  }
1311

1312
  if (ttlReq.nUids > 0) {
×
1313
    int32_t code = metaDropMultipleTables(pVnode->pMeta, ver, ttlReq.pTbUids);
×
1314
    if (code) return code;
×
1315

1316
    code = tqDeleteTbUidList(pVnode->pTq, ttlReq.pTbUids);
×
1317
    if (code) {
×
1318
      vError("vgId:%d, failed to delete tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1319
    }
1320
  }
1321

1322
end:
×
1323
  taosArrayDestroy(ttlReq.pTbUids);
×
1324
  return ret;
×
1325
}
1326

1327
static int32_t vnodeProcessFetchTtlExpiredTbs(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
12,183✔
1328
  int32_t                 code = -1;
12,183✔
1329
  SMetaReader             mr = {0};
12,183✔
1330
  SVDropTtlTableReq       ttlReq = {0};
12,183✔
1331
  SVFetchTtlExpiredTbsRsp rsp = {0};
12,183✔
1332
  SEncoder                encoder = {0};
12,183✔
1333
  SArray                 *pNames = NULL;
12,183✔
1334
  pRsp->msgType = TDMT_VND_FETCH_TTL_EXPIRED_TBS_RSP;
12,183✔
1335
  pRsp->code = TSDB_CODE_SUCCESS;
12,183✔
1336
  pRsp->pCont = NULL;
12,183✔
1337
  pRsp->contLen = 0;
12,183✔
1338

1339
  if (tDeserializeSVDropTtlTableReq(pReq, len, &ttlReq) != 0) {
12,183✔
1340
    terrno = TSDB_CODE_INVALID_MSG;
×
1341
    goto _end;
×
1342
  }
1343

1344
  if (!(ttlReq.nUids == taosArrayGetSize(ttlReq.pTbUids))) {
12,183✔
1345
    terrno = TSDB_CODE_INVALID_MSG;
×
1346
    goto _end;
×
1347
  }
1348

1349
  tb_uid_t    suid;
1350
  char        ctbName[TSDB_TABLE_NAME_LEN];
1351
  SVDropTbReq expiredTb = {.igNotExists = true};
12,183✔
1352
  metaReaderDoInit(&mr, pVnode->pMeta, 0);
12,183✔
1353
  rsp.vgId = TD_VID(pVnode);
12,183✔
1354
  rsp.pExpiredTbs = taosArrayInit(ttlReq.nUids, sizeof(SVDropTbReq));
12,183✔
1355
  if (!rsp.pExpiredTbs) goto _end;
12,183✔
1356

1357
  pNames = taosArrayInit(ttlReq.nUids, TSDB_TABLE_NAME_LEN);
12,183✔
1358
  if (!pNames) {
12,183✔
1359
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1360
    goto _end;
×
1361
  }
1362
  char buf[TSDB_TABLE_NAME_LEN];
12,183✔
1363
  for (int32_t i = 0; i < ttlReq.nUids; ++i) {
62,259✔
1364
    tb_uid_t *uid = taosArrayGet(ttlReq.pTbUids, i);
50,076✔
1365
    expiredTb.suid = *uid;
50,076✔
1366
    terrno = metaReaderGetTableEntryByUid(&mr, *uid);
50,076✔
1367
    if (terrno < 0) goto _end;
50,076✔
1368
    tstrncpy(buf, mr.me.name, TSDB_TABLE_NAME_LEN);
50,076✔
1369
    void *p = taosArrayPush(pNames, buf);
50,076✔
1370
    if (p == NULL) {
50,076✔
1371
      goto _end;
×
1372
    }
1373

1374
    expiredTb.name = p;
50,076✔
1375
    if (mr.me.type == TSDB_CHILD_TABLE) {
50,076✔
1376
      expiredTb.suid = mr.me.ctbEntry.suid;
32,546✔
1377
    }
1378

1379
    if (taosArrayPush(rsp.pExpiredTbs, &expiredTb) == NULL) {
100,152✔
1380
      goto _end;
×
1381
    }
1382
  }
1383

1384
  int32_t ret = 0;
12,183✔
1385
  tEncodeSize(tEncodeVFetchTtlExpiredTbsRsp, &rsp, pRsp->contLen, ret);
12,183✔
1386
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
12,183✔
1387
  if (pRsp->pCont == NULL) {
12,183✔
1388
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1389
    code = -1;
×
1390
    goto _end;
×
1391
  }
1392
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
12,183✔
1393
  terrno = tEncodeVFetchTtlExpiredTbsRsp(&encoder, &rsp);
12,183✔
1394
  tEncoderClear(&encoder);
12,183✔
1395

1396
  if (terrno == 0) code = 0;
12,183✔
1397
_end:
12,183✔
1398
  metaReaderClear(&mr);
12,183✔
1399
  tFreeFetchTtlExpiredTbsRsp(&rsp);
12,183✔
1400
  taosArrayDestroy(ttlReq.pTbUids);
12,183✔
1401
  if (pNames) taosArrayDestroy(pNames);
12,183✔
1402
  pRsp->code = terrno;
12,183✔
1403
  return code;
12,183✔
1404
}
1405

1406
static int32_t vnodeProcessCreateStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
5,586,592✔
1407
  int32_t        code = 0;
5,586,592✔
1408
  SVCreateStbReq req = {0};
5,586,592✔
1409
  SDecoder       coder;
5,587,765✔
1410

1411
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
5,593,674✔
1412
  pRsp->code = TSDB_CODE_SUCCESS;
5,593,057✔
1413
  pRsp->pCont = NULL;
5,593,148✔
1414
  pRsp->contLen = 0;
5,593,148✔
1415

1416
  // decode and process req
1417
  tDecoderInit(&coder, pReq, len);
5,593,786✔
1418

1419
  code = tDecodeSVCreateStbReq(&coder, &req);
5,593,145✔
1420
  if (code) {
5,582,691✔
1421
    pRsp->code = code;
×
1422
    goto _err;
×
1423
  }
1424

1425
  code = metaCreateSuperTable(pVnode->pMeta, ver, &req);
5,582,691✔
1426
  if (code) {
5,594,312✔
1427
    pRsp->code = code;
3,152✔
1428
    goto _err;
3,152✔
1429
  }
1430

1431
  tDecoderClear(&coder);
5,591,160✔
1432
  return 0;
5,590,786✔
1433

1434
_err:
3,152✔
1435
  tDecoderClear(&coder);
3,152✔
1436
  return code;
3,152✔
1437
}
1438

1439
static int32_t vnodeProcessCreateTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
48,259,204✔
1440
                                       SRpcMsg *pOriginRpc) {
1441
  SDecoder           decoder = {0};
48,259,204✔
1442
  SEncoder           encoder = {0};
48,259,653✔
1443
  int32_t            rcode = 0;
48,259,316✔
1444
  SVCreateTbBatchReq req = {0};
48,259,316✔
1445
  SVCreateTbReq     *pCreateReq;
1446
  SVCreateTbBatchRsp rsp = {0};
48,259,316✔
1447
  SVCreateTbRsp      cRsp = {0};
48,259,316✔
1448
  char               tbName[TSDB_TABLE_FNAME_LEN];
48,257,145✔
1449
  SArray            *tbUids = NULL;
48,259,316✔
1450
  SArray            *tbNames = NULL;
48,259,316✔
1451
  int64_t            tss = taosGetTimestampMs();
48,259,653✔
1452
  pRsp->msgType = TDMT_VND_CREATE_TABLE_RSP;
48,259,653✔
1453
  pRsp->code = TSDB_CODE_SUCCESS;
48,259,653✔
1454
  pRsp->pCont = NULL;
48,259,224✔
1455
  pRsp->contLen = 0;
48,259,653✔
1456

1457
  // decode
1458
  tDecoderInit(&decoder, pReq, len);
48,259,653✔
1459
  if (tDecodeSVCreateTbBatchReq(&decoder, &req) < 0) {
48,259,653✔
1460
    rcode = -1;
×
1461
    terrno = TSDB_CODE_INVALID_MSG;
×
1462
    goto _exit;
×
1463
  }
1464

1465
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(cRsp));
48,254,788✔
1466
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
48,253,861✔
1467
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
48,251,035✔
1468
  if (rsp.pArray == NULL || tbUids == NULL || tbNames == NULL) {
48,246,611✔
1469
    rcode = -1;
20✔
1470
    terrno = TSDB_CODE_OUT_OF_MEMORY;
20✔
1471
    goto _exit;
×
1472
  }
1473

1474
  // loop to create table
1475
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
105,178,545✔
1476
    pCreateReq = req.pReqs + iReq;
56,923,828✔
1477
    memset(&cRsp, 0, sizeof(cRsp));
56,925,287✔
1478

1479
    if (tsEnableAudit && tsEnableAuditCreateTable && tsAuditLevel >= AUDIT_LEVEL_CHILDTABLE) {
56,925,287✔
1480
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
149✔
1481
      if (str == NULL) {
149✔
1482
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1483
        rcode = -1;
×
1484
        goto _exit;
×
1485
      }
1486
      tstrncpy(str, pCreateReq->name, TSDB_TABLE_FNAME_LEN);
149✔
1487
      if (taosArrayPush(tbNames, &str) == NULL) {
149✔
1488
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1489
        rcode = -1;
×
1490
        goto _exit;
×
1491
      }
1492
    }
1493

1494
    // validate hash
1495
    (void)snprintf(tbName, TSDB_TABLE_FNAME_LEN, "%s.%s", pVnode->config.dbname, pCreateReq->name);
56,925,287✔
1496
    if (vnodeValidateTableHash(pVnode, tbName) < 0) {
56,923,836✔
1497
      cRsp.code = TSDB_CODE_VND_HASH_MISMATCH;
×
1498
      if (taosArrayPush(rsp.pArray, &cRsp) == NULL) {
×
1499
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1500
        rcode = -1;
×
1501
        goto _exit;
×
1502
      }
1503
      vError("vgId:%d create-table:%s failed due to hash value mismatch", TD_VID(pVnode), tbName);
×
1504
      continue;
×
1505
    }
1506

1507
    // do create table
1508
    if (metaCreateTable2(pVnode->pMeta, ver, pCreateReq, &cRsp.pMeta) < 0) {
56,908,119✔
1509
      if (pCreateReq->flags & TD_CREATE_IF_NOT_EXISTS && terrno == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
18,067✔
1510
        cRsp.code = TSDB_CODE_SUCCESS;
5,870✔
1511
      } else {
1512
        cRsp.code = terrno;
12,197✔
1513
      }
1514
    } else {
1515
      cRsp.code = TSDB_CODE_SUCCESS;
56,913,266✔
1516
      if (taosArrayPush(tbUids, &pCreateReq->uid) == NULL) {
113,826,722✔
1517
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1518
        rcode = -1;
×
1519
        goto _exit;
×
1520
      }
1521
      vnodeUpdateMetaRsp(pVnode, cRsp.pMeta);
56,913,456✔
1522
    }
1523

1524
    if (taosArrayPush(rsp.pArray, &cRsp) == NULL) {
113,861,018✔
1525
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1526
      rcode = -1;
×
1527
      goto _exit;
×
1528
    }
1529
  }
1530

1531
  vTrace("vgId:%d, add %d new created tables into query table list", TD_VID(pVnode), (int32_t)taosArrayGetSize(tbUids));
48,254,717✔
1532
  if (tqAddTbUidList(pVnode->pTq, tbUids) < 0) {
48,254,717✔
1533
    vError("vgId:%d, failed to add tbUid list since %s", TD_VID(pVnode), tstrerror(terrno));
×
1534
  }
1535

1536
  // prepare rsp
1537
  int32_t ret = 0;
48,259,606✔
1538
  tEncodeSize(tEncodeSVCreateTbBatchRsp, &rsp, pRsp->contLen, ret);
48,259,606✔
1539
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
48,254,399✔
1540
  if (pRsp->pCont == NULL) {
48,252,773✔
1541
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1542
    rcode = -1;
×
1543
    goto _exit;
×
1544
  }
1545
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
48,251,719✔
1546
  if (tEncodeSVCreateTbBatchRsp(&encoder, &rsp) != 0) {
48,253,320✔
1547
    vError("vgId:%d, failed to encode create table batch response", TD_VID(pVnode));
×
1548
  }
1549

1550
  if (tsEnableAudit && tsEnableAuditCreateTable && tsAuditLevel >= AUDIT_LEVEL_CHILDTABLE) {
48,248,484✔
1551
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
149✔
1552

1553
    SName name = {0};
149✔
1554
    if (tNameFromString(&name, pVnode->config.dbname, T_NAME_ACCT | T_NAME_DB) < 0) {
149✔
1555
      vError("vgId:%d, failed to get name from string", TD_VID(pVnode));
×
1556
    }
1557

1558
    SStringBuilder sb = {0};
149✔
1559
    for (int32_t i = 0; i < tbNames->size; i++) {
298✔
1560
      char **key = (char **)taosArrayGet(tbNames, i);
149✔
1561
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
149✔
1562
      if (i < tbNames->size - 1) {
149✔
1563
        taosStringBuilderAppendChar(&sb, ',');
×
1564
      }
1565
      // taosMemoryFreeClear(*key);
1566
    }
1567

1568
    size_t len = 0;
149✔
1569
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
149✔
1570

1571
    if (strlen(RPC_MSG_USER(pOriginRpc)) > 0) {
149✔
1572
      int64_t tse = taosGetTimestampMs();
149✔
1573
      double  duration = (double)(tse - tss);
149✔
1574
      duration = duration / 1000;
149✔
1575
      auditAddRecord(pOriginRpc, clusterId, "createTable", name.dbname, "", keyJoined, len, duration, 0);
149✔
1576
    }
1577

1578
    taosStringBuilderDestroy(&sb);
149✔
1579
  }
1580

1581
_exit:
48,248,484✔
1582
  tDeleteSVCreateTbBatchReq(&req);
48,251,797✔
1583
  taosArrayDestroyEx(rsp.pArray, tFreeSVCreateTbRsp);
48,251,761✔
1584
  taosArrayDestroy(tbUids);
48,246,682✔
1585
  tDecoderClear(&decoder);
48,242,503✔
1586
  tEncoderClear(&encoder);
48,256,020✔
1587
  taosArrayDestroyP(tbNames, NULL);
48,258,363✔
1588
  return rcode;
48,253,832✔
1589
}
1590

1591
static int32_t vnodeProcessAlterStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
8,056,354✔
1592
  int32_t        code = 0;
8,056,354✔
1593
  SVCreateStbReq req = {0};
8,056,354✔
1594
  SDecoder       dc = {0};
8,056,354✔
1595

1596
  pRsp->msgType = TDMT_VND_ALTER_STB_RSP;
8,056,354✔
1597
  pRsp->code = TSDB_CODE_SUCCESS;
8,056,354✔
1598
  pRsp->pCont = NULL;
8,056,354✔
1599
  pRsp->contLen = 0;
8,055,759✔
1600

1601
  tDecoderInit(&dc, pReq, len);
8,055,759✔
1602

1603
  // decode req
1604
  code = tDecodeSVCreateStbReq(&dc, &req);
8,056,354✔
1605
  if (code) {
8,044,284✔
1606
    tDecoderClear(&dc);
×
1607
    return code;
×
1608
  }
1609

1610
  code = metaAlterSuperTable(pVnode->pMeta, ver, &req);
8,044,284✔
1611
  if (code) {
8,055,987✔
1612
    pRsp->code = code;
×
1613
    tDecoderClear(&dc);
×
1614
    return code;
×
1615
  }
1616

1617
  tDecoderClear(&dc);
8,055,987✔
1618

1619
  return 0;
8,056,354✔
1620
}
1621

1622
static int32_t vnodeProcessDropStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp, SRpcMsg *pOriginRpc) {
954,612✔
1623
  SVDropStbReq req = {0};
954,612✔
1624
  int32_t      rcode = TSDB_CODE_SUCCESS;
955,049✔
1625
  SDecoder     decoder = {0};
955,049✔
1626
  SArray      *tbUidList = NULL;
955,049✔
1627

1628
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
955,049✔
1629
  pRsp->pCont = NULL;
955,049✔
1630
  pRsp->contLen = 0;
955,049✔
1631

1632
  // decode request
1633
  tDecoderInit(&decoder, pReq, len);
955,049✔
1634
  if (tDecodeSVDropStbReq(&decoder, &req) < 0) {
955,049✔
1635
    rcode = TSDB_CODE_INVALID_MSG;
×
1636
    goto _exit;
×
1637
  }
1638

1639
  STraceId* trace = &(pOriginRpc->info.traceId);
954,261✔
1640

1641
  vInfo("vgId:%d, start to process vnode-drop-stb, QID:0x%" PRIx64 ":0x%" PRIx64 ", drop stb:%s", TD_VID(pVnode), trace ? trace->rootId : 0, 
954,261✔
1642
              trace ? trace->msgId : 0, req.name);
1643

1644
  // process request
1645
  tbUidList = taosArrayInit(8, sizeof(int64_t));
955,755✔
1646
  if (tbUidList == NULL) goto _exit;
955,049✔
1647
  if (metaDropSuperTable(pVnode->pMeta, ver, &req) < 0) {
955,049✔
1648
    rcode = terrno;
3,234✔
1649
    goto _exit;
3,234✔
1650
  }
1651

1652
  if (tqDeleteTbUidList(pVnode->pTq, tbUidList) < 0) {
951,815✔
1653
    rcode = terrno;
×
1654
    goto _exit;
×
1655
  }
1656

1657
  // return rsp
1658
_exit:
955,049✔
1659
  vInfo("vgId:%d, finished to process vnode-drop-stb, QID:0x%" PRIx64 ":0x%" PRIx64 ", drop stb:%s", TD_VID(pVnode), trace ? trace->rootId : 0, 
955,049✔
1660
              trace ? trace->msgId : 0, req.name);
1661
  if (tbUidList) taosArrayDestroy(tbUidList);
955,049✔
1662
  pRsp->code = rcode;
955,049✔
1663
  tDecoderClear(&decoder);
955,049✔
1664
  return 0;
955,049✔
1665
}
1666

1667
SArray* getCidList(const SArray* tags) {
7,966,687✔
1668
  int32_t       code = 0;
7,966,687✔
1669
  int32_t       lino = 0;
7,966,687✔
1670
  SArray* cidList = taosArrayInit(taosArrayGetSize(tags), sizeof(col_id_t));
7,966,687✔
1671
  QUERY_CHECK_NULL(cidList, code, lino, end, terrno);
7,966,687✔
1672
  for (int32_t i = 0; i < taosArrayGetSize(tags); i++) {
15,994,311✔
1673
    SUpdatedTagVal *pTagVal = taosArrayGet(tags, i);
8,027,624✔
1674
    QUERY_CHECK_NULL(pTagVal, code, lino, end, terrno);
8,027,624✔
1675
    col_id_t cid = pTagVal->colId;
8,027,624✔
1676
    QUERY_CHECK_NULL(taosArrayPush(cidList, &cid), code, lino, end, terrno);
8,027,624✔
1677
  }
1678

1679
end:
7,966,687✔
1680
  if (code != 0) {
7,966,687✔
1681
    taosArrayDestroy(cidList);
×
1682
    return NULL;
×
1683
  }
1684
  return cidList;
7,966,687✔
1685
}
1686

1687
// the elements in tbUidList and tagsArray are one-to-one correspondence
1688
void vnodeAlterTagForTmq(SVnode *pVnode, const SArray* tbUidList, const SArray* tags, const SArray* tagsArray) {
7,955,299✔
1689
  int32_t       code = 0;
7,955,299✔
1690
  int32_t       lino = 0;
7,955,299✔
1691
  SArray*       cidList = NULL;
7,955,299✔
1692
  SArray*       cids  = NULL;
7,955,299✔
1693
  SArray*       cidListArray = NULL;
7,955,299✔
1694
  if (tags != NULL){
7,955,299✔
1695
    cidList = getCidList(tags);
22,676✔
1696
    QUERY_CHECK_NULL(cidList, code, lino, end, terrno);
22,676✔
1697
  }
1698

1699
  if (tagsArray != NULL) {
7,955,299✔
1700
    cidListArray = taosArrayInit(taosArrayGetSize(tagsArray), sizeof(SArray*));
7,932,623✔
1701
    QUERY_CHECK_NULL(cidListArray, code, lino, end, terrno);
7,932,623✔
1702

1703
    for (int32_t i = 0; i < taosArrayGetSize(tagsArray); i++) {
15,876,634✔
1704
      SArray   *obj = taosArrayGetP(tagsArray, i);
7,944,011✔
1705
      cids  = getCidList(obj);
7,944,011✔
1706
      QUERY_CHECK_NULL(cids, code, lino, end, terrno);
7,944,011✔
1707
      QUERY_CHECK_NULL(taosArrayPush(cidListArray, &cids), code, lino, end, terrno);
7,944,011✔
1708
      cids = NULL;
7,944,011✔
1709
    }
1710
  }
1711
  vDebug("vgId:%d, try to add %d tables in query table list, cidList size:%"PRIzu,
7,955,299✔
1712
         TD_VID(pVnode), (int32_t)taosArrayGetSize(tbUidList), taosArrayGetSize(cidList));
1713
  code = tqUpdateTbUidList(pVnode->pTq, tbUidList, cidList, cidListArray);
7,955,299✔
1714
  QUERY_CHECK_CODE(code, lino, end);
7,955,299✔
1715

1716
end:
7,955,299✔
1717
  if (code != 0) {
7,955,299✔
1718
    qError("vgId:%d, failed to alter tags for %d tables since %s",
×
1719
           TD_VID(pVnode), (int32_t)taosArrayGetSize(tbUidList), tstrerror(code));
1720
  }
1721
  taosArrayDestroy(cidList);
7,955,299✔
1722
  taosArrayDestroy(cids);
7,955,299✔
1723
  taosArrayDestroyP(cidListArray, (FDelete)taosArrayDestroy);
7,955,299✔
1724
}
7,955,299✔
1725

1726
static int32_t vnodeProcessAlterTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
12,650,451✔
1727
  SVAlterTbReq  vAlterTbReq = {0};
12,650,451✔
1728
  SVAlterTbRsp  vAlterTbRsp = {0};
12,650,451✔
1729
  SDecoder      dc = {0};
12,650,451✔
1730
  int32_t       code = 0;
12,650,451✔
1731
  int32_t       lino = 0;
12,650,451✔
1732
  int32_t       ret;
1733
  SEncoder      ec = {0};
12,650,451✔
1734
  STableMetaRsp vMetaRsp = {0};
12,650,451✔
1735

1736
  pRsp->msgType = TDMT_VND_ALTER_TABLE_RSP;
12,650,451✔
1737
  pRsp->pCont = NULL;
12,650,451✔
1738
  pRsp->contLen = 0;
12,650,451✔
1739
  pRsp->code = TSDB_CODE_SUCCESS;
12,650,451✔
1740

1741
  tDecoderInit(&dc, pReq, len);
12,650,451✔
1742

1743
  // decode
1744
  if (tDecodeSVAlterTbReq(&dc, &vAlterTbReq) < 0) {
12,650,451✔
1745
    vAlterTbRsp.code = TSDB_CODE_INVALID_MSG;
×
1746
    tDecoderClear(&dc);
×
1747
    goto _exit;
×
1748
  }
1749

1750
  // process
1751
  if (metaAlterTable(pVnode->pMeta, ver, &vAlterTbReq, &vMetaRsp) < 0) {
12,650,451✔
1752
    vAlterTbRsp.code = terrno;
572,690✔
1753
    tDecoderClear(&dc);
572,690✔
1754
    goto _exit;
572,690✔
1755
  }
1756
  tDecoderClear(&dc);
12,077,761✔
1757

1758
  if (NULL != vMetaRsp.pSchemas) {
12,077,761✔
1759
    vnodeUpdateMetaRsp(pVnode, &vMetaRsp);
3,913,849✔
1760
    vAlterTbRsp.pMeta = &vMetaRsp;
3,913,849✔
1761
  }
1762

1763
_exit:
12,650,235✔
1764
  destroyAlterTbReq(&vAlterTbReq);
12,650,451✔
1765
  tEncodeSize(tEncodeSVAlterTbRsp, &vAlterTbRsp, pRsp->contLen, ret);
12,650,451✔
1766
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
12,650,451✔
1767
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
12,650,451✔
1768
  if (tEncodeSVAlterTbRsp(&ec, &vAlterTbRsp) != 0) {
12,650,451✔
1769
    vError("vgId:%d, failed to encode alter table response", TD_VID(pVnode));
×
1770
  }
1771

1772
  tEncoderClear(&ec);
12,650,451✔
1773
  if (vMetaRsp.pSchemas) {
12,650,451✔
1774
    taosMemoryFree(vMetaRsp.pSchemas);
3,913,849✔
1775
    taosMemoryFree(vMetaRsp.pSchemaExt);
3,913,849✔
1776
  }
1777
  if (vMetaRsp.pColRefs) {
12,650,451✔
1778
    taosMemoryFree(vMetaRsp.pColRefs);
285,524✔
1779
  }
1780
  return 0;
12,650,451✔
1781
}
1782

1783
static int32_t vnodeProcessDropTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
1,699,656✔
1784
                                     SRpcMsg *pOriginRpc) {
1785
  SVDropTbBatchReq req = {0};
1,699,656✔
1786
  SVDropTbBatchRsp rsp = {0};
1,699,656✔
1787
  SDecoder         decoder = {0};
1,699,656✔
1788
  SEncoder         encoder = {0};
1,699,656✔
1789
  int32_t          ret;
1790
  SArray          *tbUids = NULL;
1,699,656✔
1791
  SArray          *tbNames = NULL;
1,699,656✔
1792
  int64_t          tss = taosGetTimestampMs();
1,699,656✔
1793

1794
  pRsp->msgType = ((SRpcMsg *)pReq)->msgType + 1;
1,699,656✔
1795
  pRsp->pCont = NULL;
1,699,656✔
1796
  pRsp->contLen = 0;
1,699,656✔
1797
  pRsp->code = TSDB_CODE_SUCCESS;
1,699,656✔
1798

1799
  // decode req
1800
  tDecoderInit(&decoder, pReq, len);
1,699,656✔
1801
  ret = tDecodeSVDropTbBatchReq(&decoder, &req);
1,699,656✔
1802
  if (ret < 0) {
1,699,656✔
1803
    terrno = TSDB_CODE_INVALID_MSG;
×
1804
    pRsp->code = terrno;
×
1805
    goto _exit;
×
1806
  }
1807

1808
  // process req
1809
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
1,699,656✔
1810
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(SVDropTbRsp));
1,699,656✔
1811
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
1,699,656✔
1812
  if (tbUids == NULL || rsp.pArray == NULL || tbNames == NULL) goto _exit;
1,699,656✔
1813

1814
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
3,453,748✔
1815
    SVDropTbReq *pDropTbReq = req.pReqs + iReq;
1,754,092✔
1816
    SVDropTbRsp  dropTbRsp = {0};
1,754,092✔
1817
    tb_uid_t     tbUid = 0;
1,754,092✔
1818

1819
    /* code */
1820
    ret = metaDropTable2(pVnode->pMeta, ver, pDropTbReq);
1,754,092✔
1821
    if (ret < 0) {
1,754,092✔
1822
      if (pDropTbReq->igNotExists && terrno == TSDB_CODE_TDB_TABLE_NOT_EXIST) {
×
1823
        dropTbRsp.code = TSDB_CODE_SUCCESS;
×
1824
      } else {
1825
        dropTbRsp.code = terrno;
×
1826
      }
1827
    } else {
1828
      dropTbRsp.code = TSDB_CODE_SUCCESS;
1,754,092✔
1829
    }
1830

1831
    if (taosArrayPush(rsp.pArray, &dropTbRsp) == NULL) {
3,508,184✔
1832
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1833
      pRsp->code = terrno;
×
1834
      goto _exit;
×
1835
    }
1836

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

1852
  if (tqDeleteTbUidList(pVnode->pTq, tbUids) < 0) {
1,699,656✔
1853
    vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(terrno));
×
1854
  }
1855

1856
  if (tsEnableAudit && tsEnableAuditCreateTable && tsAuditLevel >= AUDIT_LEVEL_CHILDTABLE) {
1,699,656✔
1857
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
×
1858

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

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

1874
    size_t len = 0;
×
1875
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
×
1876

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

1884
    taosStringBuilderDestroy(&sb);
×
1885
  }
1886

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

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

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

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

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

1952
  tInitSubmitBlkIter(msgIter, pBlock, &blkIter);
1953
  if (blkIter.row == NULL) return 0;
1954

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

1962
  suid = msgIter->suid;
1963
  rv = TD_ROW_SVER(blkIter.row);
1964

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

1971
  taosMemoryFreeClear(pSchema);
1972

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

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

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

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

2025
  return TSDB_CODE_SUCCESS;
×
2026
}
2027

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

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

2041
  if (tdValTypeIsNull(pCellVal->valType)) {
×
2042
    pColVal->flag = CV_FLAG_NULL;
×
2043
    return TSDB_CODE_SUCCESS;
×
2044
  }
2045

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

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

2064
  pColVal->flag = CV_FLAG_VALUE;
×
2065
  return TSDB_CODE_SUCCESS;
×
2066
}
2067

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

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

2087
  pCxt->pTbData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
×
2088
  if (NULL == pCxt->pTbData->pCreateTbReq) {
×
2089
    return terrno;
×
2090
  }
2091

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

2097
  return code;
×
2098
}
2099

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

2106
  SSubmitReqConvertCxt cxt = {0};
×
2107

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

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

2140
  vnodeDestroySubmitReqConvertCxt(&cxt);
×
2141
  return code;
×
2142
}
2143

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

2167
static int32_t buildExistSubTalbeRsp(SVnode *pVnode, SSubmitTbData *pSubmitTbData, STableMetaRsp **ppRsp) {
65,648✔
2168
  int32_t code = 0;
65,648✔
2169
  int32_t lino = 0;
65,648✔
2170

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

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

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

2226
static int32_t buildExistNormalTalbeRsp(SVnode *pVnode, int64_t uid, STableMetaRsp **ppRsp) {
40,125✔
2227
  int32_t code = 0;
40,125✔
2228
  int32_t lino = 0;
40,125✔
2229

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

2242
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
40,125✔
2243
  if (NULL == *ppRsp) {
40,125✔
2244
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2245
  }
2246

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

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

2275
static int32_t buildExistTableInStreamRsp(SVnode *pVnode, SSubmitTbData *pSubmitTbData, STableMetaRsp **ppRsp) {
316,325✔
2276
  if (pSubmitTbData->pCreateTbReq->flags & TD_CREATE_NORMAL_TB_IN_STREAM) {
316,325✔
2277
    int32_t code = buildExistNormalTalbeRsp(pVnode, pSubmitTbData->uid, ppRsp);
40,125✔
2278
    if (code) {
40,125✔
2279
      vError("vgId:%d, table uid:%" PRId64 " not exists, line:%d", TD_VID(pVnode), pSubmitTbData->uid, __LINE__);
×
2280
      return code;
×
2281
    }
2282
    return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
40,125✔
2283
  } else if (pSubmitTbData->pCreateTbReq->flags & TD_CREATE_SUB_TB_IN_STREAM) {
276,200✔
2284
    return buildExistSubTalbeRsp(pVnode, pSubmitTbData, ppRsp);
65,648✔
2285
  }
2286
  return TSDB_CODE_SUCCESS;
210,552✔
2287
}
2288

2289
static int32_t vnodeHandleAutoCreateTable(SVnode      *pVnode,    // vnode
614,517,549✔
2290
                                          int64_t      version,   // version
2291
                                          SSubmitReq2 *pRequest,  // request
2292
                                          SSubmitRsp2 *pResponse  // response
2293
) {
2294
  int32_t code = TSDB_CODE_SUCCESS;
614,517,549✔
2295
  int32_t numTbData = taosArrayGetSize(pRequest->aSubmitTbData);
614,517,549✔
2296
  SArray *newTbUids = NULL;
614,518,644✔
2297

2298
  for (int32_t i = 0; i < numTbData; ++i) {
1,255,268,659✔
2299
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
640,794,040✔
2300

2301
    if (pTbData->pCreateTbReq == NULL) {
640,794,790✔
2302
      continue;
624,626,926✔
2303
    }
2304

2305
    pTbData->uid = pTbData->pCreateTbReq->uid;
16,167,208✔
2306

2307
    // Alloc necessary resources
2308
    if (pResponse->aCreateTbRsp == NULL) {
16,167,208✔
2309
      pResponse->aCreateTbRsp = taosArrayInit(numTbData, sizeof(SVCreateTbRsp));
13,240,460✔
2310
      if (pResponse->aCreateTbRsp == NULL) {
13,240,460✔
2311
        code = terrno;
×
2312
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2313
               tstrerror(code), version);
2314
        taosArrayDestroy(newTbUids);
×
2315
        return code;
×
2316
      }
2317
    }
2318

2319
    // Do create table
2320
    vDebug("vgId:%d start to handle auto create table, version:%" PRId64, TD_VID(pVnode), version);
16,167,255✔
2321

2322
    SVCreateTbRsp *pCreateTbRsp = taosArrayReserve(pResponse->aCreateTbRsp, 1);
16,167,255✔
2323
    code = metaCreateTable2(pVnode->pMeta, version, pTbData->pCreateTbReq, &pCreateTbRsp->pMeta);
16,167,255✔
2324
    if (code == TSDB_CODE_SUCCESS) {
16,167,255✔
2325
      // Allocate necessary resources
2326
      if (newTbUids == NULL) {
15,789,122✔
2327
        newTbUids = taosArrayInit(numTbData, sizeof(int64_t));
12,923,927✔
2328
        if (newTbUids == NULL) {
12,924,266✔
2329
          code = terrno;
×
2330
          vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2331
                 tstrerror(code), version);
2332
          return code;
×
2333
        }
2334
      }
2335

2336
      if (taosArrayPush(newTbUids, &pTbData->uid) == NULL) {
31,578,583✔
2337
        code = terrno;
×
2338
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2339
               tstrerror(code), version);
2340
        taosArrayDestroy(newTbUids);
×
2341
        return code;
×
2342
      }
2343

2344
      if (pCreateTbRsp->pMeta) {
15,789,122✔
2345
        vnodeUpdateMetaRsp(pVnode, pCreateTbRsp->pMeta);
15,788,444✔
2346
      }
2347
    } else if (code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
378,133✔
2348
      code = terrno = 0;
377,059✔
2349
      pTbData->uid = pTbData->pCreateTbReq->uid;  // update uid if table exist for using below
377,059✔
2350

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

2372
  // Update the affected table uid list
2373
  if (taosArrayGetSize(newTbUids) > 0) {
614,474,619✔
2374
    vDebug("vgId:%d, add %d table into query table list in handling submit", TD_VID(pVnode),
12,923,249✔
2375
           (int32_t)taosArrayGetSize(newTbUids));
2376
    if (tqAddTbUidList(pVnode->pTq, newTbUids) != 0) {
12,924,605✔
2377
      vError("vgId:%d, failed to update tbUid list", TD_VID(pVnode));
×
2378
    }
2379
  }
2380

2381
  vDebug("vgId:%d, handle auto create table done, version:%" PRId64, TD_VID(pVnode), version);
614,475,324✔
2382

2383
  taosArrayDestroy(newTbUids);
614,475,975✔
2384
  return code;
614,475,975✔
2385
}
2386

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

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

2425
static int32_t vnodeHandleDataWrite(SVnode *pVnode, int64_t version, SSubmitReq2 *pRequest, SSubmitRsp2 *pResponse) {
614,475,975✔
2426
  int32_t code = TSDB_CODE_SUCCESS;
614,475,975✔
2427
  int32_t numTbData = taosArrayGetSize(pRequest->aSubmitTbData);
614,475,975✔
2428
  int8_t  hasBlob = 0;
614,475,875✔
2429

2430
  // Scan submit data
2431
  for (int32_t i = 0; i < numTbData; ++i) {
1,255,223,442✔
2432
    SMetaInfo      info = {0};
640,752,292✔
2433
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
640,752,195✔
2434

2435
    if (pTbData->flags & SUBMIT_REQ_WITH_BLOB) {
640,753,092✔
2436
      hasBlob = 1;
46,673✔
2437
    }
2438
    if (pTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
640,753,092✔
2439
      continue;  // skip column data format
6,924,648✔
2440
    }
2441
    if (pTbData->flags & SUBMIT_REQ_ONLY_CREATE_TABLE) {
633,827,694✔
2442
      continue;  // skip only crate table request
2,675,483✔
2443
    }
2444

2445
    code = metaGetInfo(pVnode->pMeta, pTbData->uid, &info, NULL);
631,151,679✔
2446
    if (code) {
631,152,210✔
2447
      code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
3,332✔
2448
      vWarn("vgId:%d, error occurred at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __FILE__,
3,332✔
2449
            __LINE__, tstrerror(code), version, pTbData->uid);
2450
      return code;
3,332✔
2451
    }
2452

2453
    if (info.suid != pTbData->suid) {
631,148,878✔
2454
      code = TSDB_CODE_INVALID_MSG;
×
2455
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64 " suid:%" PRId64
×
2456
             " info.suid:%" PRId64,
2457
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), version, pTbData->uid, pTbData->suid,
2458
             info.suid);
2459
      return code;
×
2460
    }
2461

2462
    if (info.suid) {
631,148,522✔
2463
      code = metaGetInfo(pVnode->pMeta, info.suid, &info, NULL);
481,681,487✔
2464
      if (code) {
481,683,677✔
2465
        code = TSDB_CODE_INTERNAL_ERROR;
×
2466
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " suid:%" PRId64, TD_VID(pVnode), __func__,
×
2467
               __FILE__, __LINE__, tstrerror(code), version, info.suid);
2468
        return code;
×
2469
      }
2470
    }
2471

2472
    if (pTbData->sver != info.skmVer) {
631,150,712✔
2473
      code = TSDB_CODE_TDB_INVALID_TABLE_SCHEMA_VER;
490✔
2474
      addExistTableInfoIntoRes(pVnode, pRequest, pResponse, pTbData, numTbData);
490✔
2475
      vDebug("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64
490✔
2476
             " sver:%d"
2477
             " info.skmVer:%d",
2478
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), version, pTbData->uid, pTbData->sver,
2479
             info.skmVer);
2480
      return code;
490✔
2481
    }
2482
  }
2483

2484
  // Do write data
2485
  vDebug("vgId:%d start to handle data write, version:%" PRId64, TD_VID(pVnode), version);
614,471,150✔
2486

2487
  for (int32_t i = 0; i < numTbData; ++i) {
1,255,201,204✔
2488
    int32_t        affectedRows = 0;
640,747,170✔
2489
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
640,748,523✔
2490

2491
    if (pTbData->flags & SUBMIT_REQ_ONLY_CREATE_TABLE) {
640,749,126✔
2492
      continue;
2,675,483✔
2493
    }
2494

2495
    if (hasBlob) {
638,072,940✔
2496
      code = vnodeSubmitBlobData(pVnode, pTbData);
46,673✔
2497
      if (code) {
46,673✔
2498
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2499
               __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2500
        return code;
×
2501
      }
2502
    }
2503

2504
    code = tsdbInsertTableData(pVnode->pTsdb, version, pTbData, &affectedRows);
638,072,940✔
2505
    if (code) {
638,056,379✔
2506
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2507
             __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2508
      return code;
×
2509
    }
2510

2511
    code = metaUpdateChangeTimeWithLock(pVnode->pMeta, pTbData->uid, pTbData->ctimeMs);
638,056,379✔
2512
    if (code) {
638,035,798✔
2513
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2514
             __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2515
      return code;
×
2516
    }
2517
    pResponse->affectedRows += affectedRows;
638,035,798✔
2518
  }
2519

2520
  vDebug("vgId:%d, handle data write done, version:%" PRId64 ", affectedRows:%d", TD_VID(pVnode), version,
614,454,034✔
2521
         pResponse->affectedRows);
2522
  return code;
614,472,731✔
2523
}
2524

2525
static int32_t vnodeScanColumnData(SVnode *pVnode, SSubmitTbData *pTbData, TSKEY minKey, TSKEY maxKey) {
6,922,593✔
2526
  int32_t code = 0;
6,922,593✔
2527

2528
  int32_t   numCols = taosArrayGetSize(pTbData->aCol);
6,922,593✔
2529
  SColData *aColData = (SColData *)TARRAY_DATA(pTbData->aCol);
6,922,263✔
2530

2531
  if (numCols <= 0) {
6,919,881✔
2532
    code = TSDB_CODE_INVALID_MSG;
×
2533
    vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " numCols:%d", TD_VID(pVnode), __func__,
×
2534
           __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, numCols);
2535
    return code;
×
2536
  }
2537

2538
  if (aColData[0].cid != PRIMARYKEY_TIMESTAMP_COL_ID || aColData[0].type != TSDB_DATA_TYPE_TIMESTAMP ||
6,919,881✔
2539
      aColData[0].nVal <= 0) {
6,922,614✔
2540
    code = TSDB_CODE_INVALID_MSG;
×
2541
    vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64
×
2542
           " first column is not primary key timestamp, cid:%d type:%d nVal:%d",
2543
           TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, aColData[0].cid,
2544
           aColData[0].type, aColData[0].nVal);
2545
    return code;
×
2546
  }
2547

2548
  for (int32_t i = 1; i < numCols; ++i) {
21,279,843✔
2549
    if (aColData[i].nVal != aColData[0].nVal) {
14,358,594✔
2550
      code = TSDB_CODE_INVALID_MSG;
×
2551
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64
×
2552
             " column cid:%d type:%d nVal:%d is not equal to primary key timestamp nVal:%d",
2553
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid,
2554
             aColData[i].cid, aColData[i].type, aColData[i].nVal, aColData[0].nVal);
2555
      return code;
×
2556
    }
2557
  }
2558

2559
  SRowKey *pLastKey = NULL;
6,921,249✔
2560
  SRowKey  lastKey = {0};
6,921,249✔
2561
  for (int32_t i = 0; i < aColData[0].nVal; ++i) {
2,147,483,647✔
2562
    SRowKey key = {0};
2,147,483,647✔
2563

2564
    tColDataArrGetRowKey(aColData, numCols, i, &key);
2,147,483,647✔
2565

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

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

2586
    *pLastKey = key;
2,147,483,647✔
2587
  }
2588

2589
  return code;
6,924,648✔
2590
}
2591

2592
static int32_t vnodeScanSubmitRowData(SVnode *pVnode, SSubmitTbData *pTbData, TSKEY minKey, TSKEY maxKey) {
633,839,548✔
2593
  int32_t code = 0;
633,839,548✔
2594

2595
  int32_t numRows = taosArrayGetSize(pTbData->aRowP);
633,839,548✔
2596
  SRow  **aRow = (SRow **)TARRAY_DATA(pTbData->aRowP);
633,848,263✔
2597

2598
  if (numRows <= 0 && (pTbData->flags & SUBMIT_REQ_ONLY_CREATE_TABLE) == 0) {
633,843,997✔
2599
    code = TSDB_CODE_INVALID_MSG;
×
2600
    vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " numRows:%d", TD_VID(pVnode), __func__,
×
2601
           __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, numRows);
2602
    return code;
×
2603
  }
2604

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

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

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

2639
    *pLastKey = key;
2,147,483,647✔
2640
  }
2641

2642
  return code;
621,163,714✔
2643
}
2644

2645
static int32_t vnodeScanSubmitReq(SVnode *pVnode, int64_t version, SSubmitReq2 *pRequest, SSubmitRsp2 *pResponse) {
614,498,816✔
2646
  int32_t code = TSDB_CODE_SUCCESS;
614,498,816✔
2647
  int32_t numTbData = taosArrayGetSize(pRequest->aSubmitTbData);
614,498,816✔
2648

2649
  TSKEY now = taosGetTimestamp(pVnode->config.tsdbCfg.precision);
614,502,833✔
2650
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * pVnode->config.tsdbCfg.keep2;
614,500,872✔
2651
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
614,501,534✔
2652
  for (int32_t i = 0; i < numTbData; i++) {
1,255,265,216✔
2653
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
640,774,931✔
2654

2655
    if (pTbData->pCreateTbReq && pTbData->pCreateTbReq->uid == 0) {
640,778,413✔
2656
      code = TSDB_CODE_INVALID_MSG;
×
2657
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2658
             tstrerror(code), version);
2659
      return code;
×
2660
    }
2661

2662
    if (pTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
640,778,651✔
2663
      code = vnodeScanColumnData(pVnode, pTbData, minKey, maxKey);
6,923,280✔
2664
      if (code) {
6,924,648✔
2665
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2666
               __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2667
        return code;
×
2668
      }
2669
    } else {
2670
      code = vnodeScanSubmitRowData(pVnode, pTbData, minKey, maxKey);
633,849,352✔
2671
      if (code) {
633,835,601✔
2672
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
303✔
2673
               __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2674
        return code;
×
2675
      }
2676
    }
2677
  }
2678

2679
  return code;
614,490,285✔
2680
}
2681

2682
static int32_t vnodeDecodeAuditRecord(const SJson *pJson, void *pObj) {
803✔
2683
  SAuditRecord *record = (SAuditRecord *)pObj;
803✔
2684
  int32_t       code = 0;
803✔
2685

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

2707
  return 0;
803✔
2708
}
2709

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

2715
  if (tname == NULL || sname == NULL) {
73✔
2716
    return TSDB_CODE_INVALID_PARA;
×
2717
  }
2718

2719
  pTbReq->name = taosStrdup(tname);
73✔
2720
  if (!pTbReq->name) {
73✔
2721
    code = terrno;
×
2722
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
2723
  }
2724

2725
  pTbReq->ctb.stbName = taosStrdup(sname);
73✔
2726
  if (!pTbReq->ctb.stbName) {
73✔
2727
    code = terrno;
×
2728
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
2729
  }
2730

2731
  pTbReq->type = TD_CHILD_TABLE;
73✔
2732
  pTbReq->ctb.suid = suid;
73✔
2733
  pTbReq->ctb.tagNum = tagNum;
73✔
2734
  pTbReq->ttl = ttl;
73✔
2735
  pTbReq->commentLen = -1;
73✔
2736
  pTbReq->ctb.tagName = tagName;
73✔
2737
  pTbReq->ctb.pTag = (uint8_t *)pTag;
73✔
2738

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

2753
static int32_t vnodePrepareCreateTb(SVCreateTbReq *pTbReq, char *tbName, int64_t suid, SSchemaWrapper *pTagSchema,
73✔
2754
                                    SAuditRecord *record) {
2755
  int32_t code = 0;
73✔
2756
  int32_t lino = 0;
73✔
2757

2758
  SArray *TagNames = NULL;
73✔
2759
  SArray *pTagVals = NULL;
73✔
2760
  STag   *pTag = NULL;
73✔
2761

2762
  SSchema *tSchema = &pTagSchema->pSchema[0];
73✔
2763
  vTrace("schema name:%s", tSchema->name);
73✔
2764

2765
  TagNames = taosArrayInit(1, TSDB_COL_NAME_LEN);
73✔
2766
  if (!TagNames) {
73✔
2767
    code = terrno;
×
2768
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
2769
  }
2770

2771
  if (NULL == taosArrayPush(TagNames, tSchema->name)) {
146✔
2772
    code = terrno;
×
2773
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
2774
  }
2775

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

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

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

2813
  if (pTagVals != NULL) {
73✔
2814
    taosArrayDestroy(pTagVals);
73✔
2815
    pTagVals = NULL;
73✔
2816
  }
2817

2818
  return code;
73✔
2819
}
2820

2821
static SArray *vnodePrepareRow(SVnode *pVnode, STSchema *pSchema, SAuditRecord *record) {
803✔
2822
  int32_t code = 0;
803✔
2823
  int32_t lino = 0;
803✔
2824

2825
  SArray *aRows = NULL;
803✔
2826
  SArray *pVals = NULL;
803✔
2827
  SRow   *pRow = NULL;
803✔
2828

2829
  if (!(aRows = taosArrayInit(1, sizeof(SRow *)))) {
803✔
2830
    code = terrno;
×
2831
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
2832
  }
2833

2834
  if ((pVals = taosArrayInit(1, sizeof(SColVal))) == NULL) {
803✔
2835
    code = terrno;
×
2836
    goto _exit;
×
2837
  }
2838

2839
  for (int32_t k = 0; k < pSchema->numOfCols; ++k) {
8,030✔
2840
    const STColumn *pCol = &pSchema->columns[k];
7,227✔
2841
    vTrace("vgId:%d, schema column id:%d, type:%d", TD_VID(pVnode), pCol->colId, pCol->type);
7,227✔
2842

2843
    // colId is consistent with audit_columns in mndStb.c
2844
    void *data = NULL;
7,227✔
2845
    if (pCol->colId == 1) {
7,227✔
2846
      data = &record->curTime;
803✔
2847
    } else if (pCol->colId == 2) {
6,424✔
2848
      data = record->detail;
803✔
2849
    } else if (pCol->colId == 3) {
5,621✔
2850
      data = record->user;
803✔
2851
    } else if (pCol->colId == 4) {
4,818✔
2852
      data = record->operation;
803✔
2853
    } else if (pCol->colId == 5) {
4,015✔
2854
      data = record->target1;
803✔
2855
    } else if (pCol->colId == 6) {
3,212✔
2856
      data = record->target2;
803✔
2857
    } else if (pCol->colId == 7) {
2,409✔
2858
      data = record->clientAddress;
803✔
2859
    } else if (pCol->colId == 8) {
1,606✔
2860
      data = &record->duration;
803✔
2861
    } else if (pCol->colId == 9) {
803✔
2862
      data = &record->affectedRows;
803✔
2863
    } else {
2864
      vError("the column id %" PRIi16 " is not defined in audit record table", pCol->colId);
×
2865
      code = TSDB_CODE_APP_ERROR;
×
2866
      TAOS_CHECK_GOTO(code, &lino, _exit);
×
2867
    }
2868

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

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

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

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

2959
static SArray *vnodePrepareSubmitTb(SVnode *pVnode, SAuditRecord *record, STSchema *pSchema, SSchemaWrapper *pTagSchema,
803✔
2960
                                    int64_t suid) {
2961
  int32_t code = 0;
803✔
2962
  int32_t lino = 0;
803✔
2963

2964
  SArray *aSubmitTbData = NULL;
803✔
2965
  SSubmitTbData tbData = {0};
803✔
2966

2967
  if (!(aSubmitTbData = taosArrayInit(1, sizeof(SSubmitTbData)))) {
803✔
2968
    code = terrno;
×
2969
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
2970
  }
2971

2972
  char tbName[TSDB_TABLE_NAME_LEN] = {0};
803✔
2973
  (void)tsnprintf(tbName, TSDB_TABLE_NAME_LEN, "t_operations_%s", record->strClusterId);
803✔
2974

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

2985
    tbData.uid = tGenIdPI64();
73✔
2986
    tbData.flags |= SUBMIT_REQ_AUTO_CREATE_TABLE;
73✔
2987

2988
    tbData.pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
73✔
2989

2990
    if (tbData.pCreateTbReq == NULL) {
73✔
2991
      code = terrno;
×
2992
      TAOS_CHECK_GOTO(code, &lino, _exit);
×
2993
    }
2994

2995
    tbData.pCreateTbReq->uid = tbData.uid;
73✔
2996
    tbData.pCreateTbReq->btime = taosGetTimestampMs();
146✔
2997

2998
    code = vnodePrepareCreateTb(tbData.pCreateTbReq, tbName, suid, pTagSchema, record);
73✔
2999

3000
    metaReaderClear(&merTb);
73✔
3001
    TAOS_CHECK_GOTO(code, &lino, _exit);
73✔
3002
  } else {
3003
    metaReaderClear(&merTb);
×
3004
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
3005
  }
3006

3007
  tbData.suid = suid;
803✔
3008
  tbData.sver = pSchema->version;
803✔
3009

3010
  tbData.aRowP = vnodePrepareRow(pVnode, pSchema, record);
803✔
3011
  if (tbData.aRowP == NULL) {
803✔
3012
    code = terrno;
×
3013
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
3014
  }
3015

3016
  if (NULL == taosArrayPush(aSubmitTbData, &tbData)) {
803✔
3017
    code = terrno;
×
3018
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
3019
  }
3020

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

3034
static int32_t vnodeSaveOneAuditRecord(SVnode *pVnode, int64_t ver, SJson *pJson, SSchemaWrapper *pTagSchema,
803✔
3035
                                       int64_t suid, STSchema *pSchema, SRpcMsg *pOriginalMsg) {
3036
  int32_t code = 0;
803✔
3037
  int32_t lino = 0;
803✔
3038
  terrno = 0;
803✔
3039

3040
  SAuditRecord record = {0};
803✔
3041
  SSubmitReq2 *pSubmitReq = NULL;
803✔
3042
  SSubmitRsp2 *pSubmitRsp = &(SSubmitRsp2){0};
803✔
3043

3044
  TAOS_CHECK_GOTO(vnodeDecodeAuditRecord(pJson, &record), &lino, _exit);
803✔
3045

3046
  vTrace("vgId:%d, start to audit operation:%s", TD_VID(pVnode), record.operation);
803✔
3047

3048
  if (!(pSubmitReq = taosMemoryCalloc(1, sizeof(SSubmitReq2)))) {
803✔
3049
    code = terrno;
×
3050
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
3051
  }
3052

3053
  pSubmitReq->aSubmitTbData = vnodePrepareSubmitTb(pVnode, &record, pSchema, pTagSchema, suid);
803✔
3054
  if (pSubmitReq->aSubmitTbData == NULL) {
803✔
3055
    code = terrno;
×
3056
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
3057
  }
3058

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

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

3075
  vTrace("vgId:%d, affectedRow:%d", TD_VID(pVnode), pSubmitRsp->affectedRows);
803✔
3076

3077
_exit:
803✔
3078
  if (code != 0)
803✔
3079
    vError("vgId:%d, failed to save one AuditRecord at line:%d, since %s", TD_VID(pVnode), lino, tstrerror(code));
×
3080

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

3089
  // update metrics
3090
  METRICS_UPDATE(pVnode->writeMetrics.total_requests, METRIC_LEVEL_LOW, 1);
803✔
3091
  METRICS_UPDATE(pVnode->writeMetrics.total_rows, METRIC_LEVEL_HIGH, pSubmitRsp->affectedRows);
803✔
3092
  // METRICS_UPDATE(pVnode->writeMetrics.total_bytes, METRIC_LEVEL_LOW, pMsg->header.contLen);
3093

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

3107
  // clear
3108
  if (pSubmitReq != NULL) {
803✔
3109
    tDestroySubmitReq(pSubmitReq, TSDB_MSG_FLG_ENCODE);
803✔
3110
    taosMemoryFree(pSubmitReq);
803✔
3111
  }
3112
  if (pSubmitRsp != NULL) {
803✔
3113
    tDestroySSubmitRsp2(pSubmitRsp, TSDB_MSG_FLG_ENCODE);
803✔
3114
  }
3115

3116
  return code;
803✔
3117
}
3118

3119
static int32_t vnodeProcessAuditRecordReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pOriginalMsg) {
292✔
3120
  int32_t code = 0;
292✔
3121
  int32_t lino = 0;
292✔
3122
  terrno = 0;
292✔
3123

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

3131
  vTrace("vgId:%d, start to process AuditRecord Req", TD_VID(pVnode));
292✔
3132

3133
  SJson          *pJson = NULL;
292✔
3134
  STSchema       *pSchema = NULL;
292✔
3135
  SSchemaWrapper *pTagSchema = NULL;
292✔
3136
  int64_t         suid = 0;
292✔
3137

3138
  pJson = tjsonParse(req.data);
292✔
3139
  if (pJson == NULL) {
292✔
3140
    code = TSDB_CODE_INVALID_JSON_FORMAT;
×
3141
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
3142
  }
3143

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

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

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

3181
_exit:
292✔
3182
  if (code != 0)
292✔
3183
    vError("vgId:%d, failed to process AuditRecordReq at line:%d, since %s", TD_VID(pVnode), lino, tstrerror(code));
×
3184

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

3197
  return code;
292✔
3198
}
3199

3200
static int32_t vnodeProcessSubmitReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
614,515,510✔
3201
                                     SRpcMsg *pOriginalMsg) {
3202
  int32_t code = 0;
614,515,510✔
3203
  int32_t lino = 0;
614,515,510✔
3204
  terrno = 0;
614,515,510✔
3205

3206
  SSubmitReq2 *pSubmitReq = &(SSubmitReq2){0};
614,517,439✔
3207
  SSubmitRsp2 *pSubmitRsp = &(SSubmitRsp2){0};
614,517,439✔
3208
  int32_t      ret;
3209
  SEncoder     ec = {0};
614,515,017✔
3210

3211
  pRsp->code = TSDB_CODE_SUCCESS;
614,516,475✔
3212

3213
  void           *pAllocMsg = NULL;
614,516,819✔
3214
  SSubmitReq2Msg *pMsg = (SSubmitReq2Msg *)pReq;
614,516,819✔
3215
  SDecoder        dc = {0};
614,516,819✔
3216
  if (0 == taosHton64(pMsg->version)) {
614,515,675✔
3217
    code = vnodeSubmitReqConvertToSubmitReq2(pVnode, (SSubmitReq *)pMsg, pSubmitReq);
×
3218
    if (TSDB_CODE_SUCCESS == code) {
×
3219
      code = vnodeRebuildSubmitReqMsg(pSubmitReq, &pReq);
×
3220
    }
3221
    if (TSDB_CODE_SUCCESS == code) {
×
3222
      pAllocMsg = pReq;
×
3223
    }
3224
    TSDB_CHECK_CODE(code, lino, _exit);
×
3225
  } else {
3226
    // decode
3227
    pReq = POINTER_SHIFT(pReq, sizeof(SSubmitReq2Msg));
614,515,147✔
3228
    len -= sizeof(SSubmitReq2Msg);
614,515,361✔
3229

3230
    tDecoderInit(&dc, pReq, len);
614,515,361✔
3231
    if (tDecodeSubmitReq(&dc, pSubmitReq, NULL) < 0) {
614,516,376✔
3232
      code = TSDB_CODE_INVALID_MSG;
×
3233
      TSDB_CHECK_CODE(code, lino, _exit);
×
3234
    }
3235
  }
3236

3237
  // Scan the request
3238
  code = vnodeScanSubmitReq(pVnode, ver, pSubmitReq, pSubmitRsp);
614,504,066✔
3239
  if (code) {
614,490,031✔
3240
    vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
3241
           tstrerror(code), ver);
3242
    TSDB_CHECK_CODE(code, lino, _exit);
×
3243
  }
3244

3245
  vGDebug(pOriginalMsg ? &pOriginalMsg->info.traceId : NULL, "vgId:%d, index:%" PRId64 ", submit block, rows:%d",
614,490,031✔
3246
          TD_VID(pVnode), ver, (int32_t)taosArrayGetSize(pSubmitReq->aSubmitTbData));
3247

3248
  // Handle auto create table
3249
  code = vnodeHandleAutoCreateTable(pVnode, ver, pSubmitReq, pSubmitRsp);
614,514,504✔
3250
  if (code) {
614,516,572✔
3251
    vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
41,689✔
3252
           tstrerror(code), ver);
3253
    TSDB_CHECK_CODE(code, lino, _exit);
41,689✔
3254
  }
3255

3256
  // Handle data write
3257
  code = vnodeHandleDataWrite(pVnode, ver, pSubmitReq, pSubmitRsp);
614,474,883✔
3258
  if (code) {
614,475,830✔
3259
    vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
3,822✔
3260
           tstrerror(code), ver);
3261
    TSDB_CHECK_CODE(code, lino, _exit);
3,822✔
3262
  }
3263

3264
_exit:
614,517,321✔
3265
  // message
3266
  pRsp->code = code;
614,517,611✔
3267
  tEncodeSize(tEncodeSSubmitRsp2, pSubmitRsp, pRsp->contLen, ret);
614,517,611✔
3268
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
614,516,422✔
3269
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
614,516,345✔
3270
  if (tEncodeSSubmitRsp2(&ec, pSubmitRsp) < 0) {
614,516,519✔
3271
    vError("vgId:%d, failed to encode submit response", TD_VID(pVnode));
×
3272
  }
3273
  tEncoderClear(&ec);
614,515,511✔
3274

3275
  // update statistics
3276
  (void)atomic_add_fetch_64(&pVnode->statis.nInsert, pSubmitRsp->affectedRows);
614,515,506✔
3277
  (void)atomic_add_fetch_64(&pVnode->statis.nInsertSuccess, pSubmitRsp->affectedRows);
614,515,156✔
3278
  (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsert, 1);
614,516,453✔
3279

3280
  // update metrics
3281
  METRICS_UPDATE(pVnode->writeMetrics.total_requests, METRIC_LEVEL_LOW, 1);
614,516,288✔
3282
  METRICS_UPDATE(pVnode->writeMetrics.total_rows, METRIC_LEVEL_HIGH, pSubmitRsp->affectedRows);
614,507,229✔
3283
  METRICS_UPDATE(pVnode->writeMetrics.total_bytes, METRIC_LEVEL_LOW, pMsg->header.contLen);
614,508,239✔
3284

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

3298
  if (code == 0) {
614,507,732✔
3299
    (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
614,459,044✔
3300
  }
3301

3302
  // clear
3303
  tDestroySubmitReq(pSubmitReq, 0 == taosHton64(pMsg->version) ? TSDB_MSG_FLG_CMPT : TSDB_MSG_FLG_DECODE);
614,515,778✔
3304
  tDestroySSubmitRsp2(pSubmitRsp, TSDB_MSG_FLG_ENCODE);
614,505,061✔
3305

3306
  if (code) {
614,492,386✔
3307
    terrno = code;
45,511✔
3308
    if (code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
45,511✔
3309
      vInfo("vgId:%d, failed to process submit request since %s, lino:%d, version:%" PRId64, TD_VID(pVnode),
40,615✔
3310
            tstrerror(code), lino, ver);
3311

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

3318
  taosMemoryFree(pAllocMsg);
614,503,431✔
3319
  tDecoderClear(&dc);
614,502,968✔
3320

3321
  return code;
614,512,588✔
3322
}
3323

3324
static int32_t vnodeConsolidateAlterHashRange(SVnode *pVnode, int64_t ver) {
25,701✔
3325
  int32_t code = TSDB_CODE_SUCCESS;
25,701✔
3326

3327
  vInfo("vgId:%d, trim meta of tables per hash range [%" PRIu32 ", %" PRIu32 "]. apply-index:%" PRId64, TD_VID(pVnode),
25,701✔
3328
        pVnode->config.hashBegin, pVnode->config.hashEnd, ver);
3329

3330
  // TODO: trim meta of tables from TDB per hash range [pVnode->config.hashBegin, pVnode->config.hashEnd]
3331
  code = metaTrimTables(pVnode->pMeta, ver);
25,701✔
3332

3333
  return code;
25,701✔
3334
}
3335

3336
static int32_t vnodeProcessAlterConfirmReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
4,145,917✔
3337
  vInfo("vgId:%d, vnode handle msgType:alter-confirm, alter confirm msg is processed", TD_VID(pVnode));
4,145,917✔
3338
  int32_t code = TSDB_CODE_SUCCESS;
4,145,917✔
3339
  if (!pVnode->config.hashChange) {
4,145,917✔
3340
    goto _exit;
4,120,216✔
3341
  }
3342

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

3350
_exit:
4,145,917✔
3351
  pRsp->msgType = TDMT_VND_ALTER_CONFIRM_RSP;
4,145,917✔
3352
  pRsp->code = code;
4,145,917✔
3353
  pRsp->pCont = NULL;
4,145,917✔
3354
  pRsp->contLen = 0;
4,145,917✔
3355

3356
  return code;
4,145,917✔
3357
}
3358

3359
extern int32_t tsdbDisableAndCancelAllBgTask(STsdb *pTsdb);
3360
extern void    tsdbEnableBgTask(STsdb *pTsdb);
3361

3362
static int32_t vnodeProcessAlterConfigReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
330,958✔
3363
  bool walChanged = false;
330,958✔
3364
  bool tsdbChanged = false;
330,958✔
3365

3366
  SAlterVnodeConfigReq req = {0};
330,958✔
3367
  if (tDeserializeSAlterVnodeConfigReq(pReq, len, &req) != 0) {
330,958✔
3368
    terrno = TSDB_CODE_INVALID_MSG;
×
3369
    return TSDB_CODE_INVALID_MSG;
×
3370
  }
3371

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

3381
  if (pVnode->config.cacheLastSize != req.cacheLastSize) {
330,958✔
3382
    pVnode->config.cacheLastSize = req.cacheLastSize;
4,412✔
3383
    tsdbCacheSetCapacity(pVnode, (size_t)pVnode->config.cacheLastSize * 1024 * 1024);
4,412✔
3384
  }
3385

3386
  if (pVnode->config.cacheLastShardBits != req.cacheLastShardBits) {
330,958✔
3387
    vInfo("vgId:%d, cacheLastShardBits change from %d to %d, rebuilding last cache",
1,526✔
3388
          TD_VID(pVnode), pVnode->config.cacheLastShardBits, req.cacheLastShardBits);
3389

3390
    // Rebuild only the lruCache under mutex to protect concurrent readers/writers
3391
    (void)taosThreadMutexLock(&pVnode->pTsdb->lruMutex);
1,526✔
3392
    int32_t code = tsdbRebuildLastCache(pVnode->pTsdb, req.cacheLastShardBits);
1,526✔
3393
    (void)taosThreadMutexUnlock(&pVnode->pTsdb->lruMutex);
1,526✔
3394

3395
    if (code) {
1,526✔
3396
      vError("vgId:%d, failed to rebuild last cache since %s", TD_VID(pVnode), tstrerror(code));
×
3397
      return code;
×
3398
    }
3399

3400
    pVnode->config.cacheLastShardBits = req.cacheLastShardBits;
1,526✔
3401

3402
    vInfo("vgId:%d, last cache rebuilt successfully with %d shards",
1,526✔
3403
          TD_VID(pVnode), taosLRUCacheGetNumShards(pVnode->pTsdb->lruCache));
3404
  }
3405

3406
  if (pVnode->config.szBuf != req.buffer * 1024LL * 1024LL) {
330,958✔
3407
    vInfo("vgId:%d, vnode buffer is changed from %" PRId64 " to %" PRId64, TD_VID(pVnode), pVnode->config.szBuf,
1,850✔
3408
          (uint64_t)(req.buffer * 1024LL * 1024LL));
3409
    pVnode->config.szBuf = req.buffer * 1024LL * 1024LL;
1,850✔
3410
  }
3411

3412
  if (pVnode->config.szCache != req.pages) {
330,958✔
3413
    if ((terrno = metaAlterCache(pVnode->pMeta, req.pages)) < 0) {
4,728✔
3414
      vError("vgId:%d, failed to change vnode pages from %d to %d failed since %s", TD_VID(pVnode),
×
3415
             pVnode->config.szCache, req.pages, tstrerror(terrno));
3416
      return terrno;
×
3417
    } else {
3418
      vInfo("vgId:%d, vnode pages is changed from %d to %d", TD_VID(pVnode), pVnode->config.szCache, req.pages);
4,728✔
3419
      pVnode->config.szCache = req.pages;
4,728✔
3420
    }
3421
  }
3422

3423
  if (pVnode->config.cacheLast != req.cacheLast) {
330,958✔
3424
    pVnode->config.cacheLast = req.cacheLast;
91,001✔
3425
  }
3426

3427
  if (pVnode->config.walCfg.fsyncPeriod != req.walFsyncPeriod) {
330,958✔
3428
    pVnode->config.walCfg.fsyncPeriod = req.walFsyncPeriod;
54,776✔
3429
    walChanged = true;
54,776✔
3430
  }
3431

3432
  if (pVnode->config.walCfg.level != req.walLevel) {
330,958✔
3433
    if (pVnode->config.walCfg.level == 0) {
40,326✔
3434
      pVnode->config.walCfg.clearFiles = 1;
2,284✔
3435
    }
3436
    pVnode->config.walCfg.level = req.walLevel;
40,326✔
3437
    walChanged = true;
40,326✔
3438
  }
3439

3440
  if (pVnode->config.walCfg.retentionPeriod != req.walRetentionPeriod) {
330,958✔
3441
    pVnode->config.walCfg.retentionPeriod = req.walRetentionPeriod;
50,971✔
3442
    walChanged = true;
50,971✔
3443
  }
3444

3445
  if (pVnode->config.walCfg.retentionSize != req.walRetentionSize) {
330,958✔
3446
    pVnode->config.walCfg.retentionSize = req.walRetentionSize;
1,520✔
3447
    walChanged = true;
1,520✔
3448
  }
3449

3450
  if (pVnode->config.tsdbCfg.keep0 != req.daysToKeep0) {
330,958✔
3451
    pVnode->config.tsdbCfg.keep0 = req.daysToKeep0;
45,592✔
3452
    tsdbChanged = true;
45,592✔
3453
  }
3454

3455
  if (pVnode->config.tsdbCfg.keep1 != req.daysToKeep1) {
330,958✔
3456
    pVnode->config.tsdbCfg.keep1 = req.daysToKeep1;
44,068✔
3457
    tsdbChanged = true;
44,068✔
3458
  }
3459

3460
  if (pVnode->config.tsdbCfg.keep2 != req.daysToKeep2) {
330,958✔
3461
    pVnode->config.tsdbCfg.keep2 = req.daysToKeep2;
38,078✔
3462
    tsdbChanged = true;
38,078✔
3463
  }
3464

3465
  if (pVnode->config.tsdbCfg.keepTimeOffset != req.keepTimeOffset) {
330,958✔
3466
    pVnode->config.tsdbCfg.keepTimeOffset = req.keepTimeOffset;
7,658✔
3467
    tsdbChanged = true;
7,658✔
3468
  }
3469

3470
  if (req.sttTrigger != -1 && req.sttTrigger != pVnode->config.sttTrigger) {
330,958✔
3471
    if (req.sttTrigger > 1 && pVnode->config.sttTrigger > 1) {
2,638✔
3472
      pVnode->config.sttTrigger = req.sttTrigger;
1,520✔
3473
    } else {
3474
      vnodeAWait(&pVnode->commitTask);
1,118✔
3475

3476
      int32_t ret = tsdbDisableAndCancelAllBgTask(pVnode->pTsdb);
1,118✔
3477
      if (ret != 0) {
1,118✔
3478
        vError("vgId:%d, failed to disable bg task since %s", TD_VID(pVnode), tstrerror(ERRNO));
×
3479
      }
3480

3481
      pVnode->config.sttTrigger = req.sttTrigger;
1,118✔
3482
      tsdbEnableBgTask(pVnode->pTsdb);
1,118✔
3483
    }
3484
  }
3485

3486
  if (req.minRows != -1 && req.minRows != pVnode->config.tsdbCfg.minRows) {
330,958✔
3487
    pVnode->config.tsdbCfg.minRows = req.minRows;
1,520✔
3488
  }
3489

3490
  if (req.ssKeepLocal != -1 && req.ssKeepLocal != pVnode->config.ssKeepLocal) {
330,958✔
3491
    pVnode->config.ssKeepLocal = req.ssKeepLocal;
1,520✔
3492
  }
3493
  if (req.ssCompact != -1 && req.ssCompact != pVnode->config.ssCompact) {
330,958✔
3494
    pVnode->config.ssCompact = req.ssCompact;
1,520✔
3495
  }
3496
  if (req.allowDrop != pVnode->config.allowDrop) {
330,958✔
3497
    pVnode->config.allowDrop = req.allowDrop;
×
3498
  }
3499
  if (req.secureDelete != pVnode->config.secureDelete) {
330,958✔
3500
    pVnode->config.secureDelete = req.secureDelete;
1,182✔
3501
  }
3502

3503
  if (walChanged) {
330,958✔
3504
    if (walAlter(pVnode->pWal, &pVnode->config.walCfg) != 0) {
144,539✔
3505
      vError("vgId:%d, failed to alter wal config since %s", TD_VID(pVnode), tstrerror(ERRNO));
×
3506
    }
3507
  }
3508

3509
  if (tsdbChanged) {
330,958✔
3510
    tsdbSetKeepCfg(pVnode->pTsdb, &pVnode->config.tsdbCfg);
60,872✔
3511
  }
3512

3513
  return 0;
330,958✔
3514
}
3515

3516
static int32_t vnodeProcessBatchDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
3517
  SBatchDeleteReq deleteReq;
×
3518
  SDecoder        decoder;
×
3519
  tDecoderInit(&decoder, pReq, len);
×
3520
  if (tDecodeSBatchDeleteReq(&decoder, &deleteReq) < 0) {
×
3521
    tDecoderClear(&decoder);
×
3522
    return terrno = TSDB_CODE_INVALID_MSG;
×
3523
  }
3524

3525
  SMetaReader mr = {0};
×
3526
  metaReaderDoInit(&mr, pVnode->pMeta, META_READER_NOLOCK);
×
3527
  STsdb *pTsdb = pVnode->pTsdb;
×
3528

3529
  int32_t sz = taosArrayGetSize(deleteReq.deleteReqs);
×
3530
  for (int32_t i = 0; i < sz; i++) {
×
3531
    SSingleDeleteReq *pOneReq = taosArrayGet(deleteReq.deleteReqs, i);
×
3532
    char             *name = pOneReq->tbname;
×
3533
    if (metaGetTableEntryByName(&mr, name) < 0) {
×
3534
      vDebug("vgId:%d, stream delete msg, skip since no table: %s", pVnode->config.vgId, name);
×
3535
      continue;
×
3536
    }
3537

3538
    int64_t uid = mr.me.uid;
×
3539

3540
    int32_t code = tsdbDeleteTableData(pTsdb, ver, deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs, 0);
×
3541
    if (code < 0) {
×
3542
      terrno = code;
×
3543
      vError("vgId:%d, delete error since %s, suid:%" PRId64 ", uid:%" PRId64 ", start ts:%" PRId64 ", end ts:%" PRId64,
×
3544
             TD_VID(pVnode), terrstr(), deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
3545
    }
3546

3547
    if (deleteReq.level == 0) {
×
3548
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, deleteReq.ctimeMs);
×
3549
      if (code < 0) {
×
3550
        terrno = code;
×
3551
        vError("vgId:%d, update change time error since %s, suid:%" PRId64 ", uid:%" PRId64 ", start ts:%" PRId64
×
3552
               ", end ts:%" PRId64,
3553
               TD_VID(pVnode), terrstr(), deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
3554
      }
3555
    }
3556
    tDecoderClear(&mr.coder);
×
3557
  }
3558
  metaReaderClear(&mr);
×
3559
  taosArrayDestroy(deleteReq.deleteReqs);
×
3560
  return 0;
×
3561
}
3562

3563
static int32_t vnodeProcessDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
2,203,545✔
3564
                                     SRpcMsg *pOriginalMsg) {
3565
  int32_t     code = 0;
2,203,545✔
3566
  SDecoder   *pCoder = &(SDecoder){0};
2,203,545✔
3567
  SDeleteRes *pRes = &(SDeleteRes){0};
2,203,545✔
3568

3569
  pRsp->msgType = TDMT_VND_DELETE_RSP;
2,203,545✔
3570
  pRsp->pCont = NULL;
2,203,545✔
3571
  pRsp->contLen = 0;
2,203,545✔
3572
  pRsp->code = TSDB_CODE_SUCCESS;
2,203,545✔
3573

3574
  pRes->uidList = taosArrayInit(0, sizeof(tb_uid_t));
2,203,545✔
3575
  if (pRes->uidList == NULL) {
2,203,545✔
3576
    code = terrno;
×
3577
    goto _err;
×
3578
  }
3579

3580
  tDecoderInit(pCoder, pReq, len);
2,203,545✔
3581
  code = tDecodeDeleteRes(pCoder, pRes);
2,203,545✔
3582
  if (code) goto _err;
2,203,545✔
3583

3584
  if (pRes->affectedRows > 0) {
2,203,545✔
3585
    for (int32_t iUid = 0; iUid < taosArrayGetSize(pRes->uidList); iUid++) {
3,927,011✔
3586
      uint64_t uid = *(uint64_t *)taosArrayGet(pRes->uidList, iUid);
2,238,516✔
3587
      // Merge runtime vnode(db-level) secureDelete to avoid stale client meta after ALTER DATABASE.
3588
      int8_t secureDelete = pRes->secureDelete | pVnode->config.secureDelete;
2,238,516✔
3589
      code = tsdbDeleteTableData(pVnode->pTsdb, ver, pRes->suid, uid, pRes->skey, pRes->ekey, secureDelete);
2,238,516✔
3590
      if (code) goto _err;
2,238,516✔
3591
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, pRes->ctimeMs);
2,238,516✔
3592
      if (code) goto _err;
2,238,516✔
3593
    }
3594
  }
3595

3596
  tDecoderClear(pCoder);
2,203,545✔
3597
  taosArrayDestroy(pRes->uidList);
2,203,545✔
3598

3599
  SVDeleteRsp rsp = {.affectedRows = pRes->affectedRows};
2,203,545✔
3600
  int32_t     ret = 0;
2,203,545✔
3601
  tEncodeSize(tEncodeSVDeleteRsp, &rsp, pRsp->contLen, ret);
2,203,545✔
3602
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
2,203,545✔
3603
  SEncoder ec = {0};
2,203,545✔
3604
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
2,203,545✔
3605
  code = tEncodeSVDeleteRsp(&ec, &rsp);
2,203,545✔
3606
  if (code) goto _err;
2,203,545✔
3607
  tEncoderClear(&ec);
2,203,545✔
3608
  return code;
2,203,545✔
3609

3610
_err:
×
3611
  /*
3612
  if(code == TSDB_CODE_SUCCESS){
3613
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_DELETE, pVnode->monitor.strClusterId,
3614
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
3615
                                        RPC_MSG_USER(pOriginalMsg), "Success"};
3616
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
3617
  }
3618
  else{
3619
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_DELETE, pVnode->monitor.strClusterId,
3620
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
3621
                                        RPC_MSG_USER(pOriginalMsg), "Failed"};
3622
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
3623
  }
3624
  */
3625

3626
  return code;
×
3627
}
3628
static int32_t vnodeProcessCreateIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
18,899✔
3629
  SVCreateStbReq req = {0};
18,899✔
3630
  SDecoder       dc = {0};
18,899✔
3631
  int32_t        code = 0;
18,899✔
3632

3633
  pRsp->msgType = TDMT_VND_CREATE_INDEX_RSP;
18,899✔
3634
  pRsp->code = TSDB_CODE_SUCCESS;
18,899✔
3635
  pRsp->pCont = NULL;
18,899✔
3636
  pRsp->contLen = 0;
18,899✔
3637

3638
  tDecoderInit(&dc, pReq, len);
18,899✔
3639
  // decode req
3640
  if (tDecodeSVCreateStbReq(&dc, &req) < 0) {
18,899✔
3641
    tDecoderClear(&dc);
×
3642
    return terrno = TSDB_CODE_INVALID_MSG;
×
3643
  }
3644

3645
  code = metaAddIndexToSuperTable(pVnode->pMeta, ver, &req);
18,899✔
3646
  if (code) {
18,899✔
3647
    pRsp->code = code;
×
3648
    goto _err;
×
3649
  }
3650
  tDecoderClear(&dc);
18,899✔
3651
  return 0;
18,899✔
3652

3653
_err:
×
3654
  tDecoderClear(&dc);
×
3655
  return code;
×
3656
}
3657
static int32_t vnodeProcessDropIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
15,388✔
3658
  SDropIndexReq req = {0};
15,388✔
3659
  int32_t       code = 0;
15,388✔
3660
  pRsp->msgType = TDMT_VND_DROP_INDEX_RSP;
15,388✔
3661
  pRsp->code = TSDB_CODE_SUCCESS;
15,388✔
3662
  pRsp->pCont = NULL;
15,388✔
3663
  pRsp->contLen = 0;
15,388✔
3664

3665
  if ((code = tDeserializeSDropIdxReq(pReq, len, &req))) {
15,388✔
3666
    pRsp->code = code;
×
3667
    return code;
×
3668
  }
3669

3670
  code = metaDropIndexFromSuperTable(pVnode->pMeta, ver, &req);
15,388✔
3671
  if (code) {
15,388✔
3672
    pRsp->code = code;
×
3673
    return code;
×
3674
  }
3675
  return TSDB_CODE_SUCCESS;
15,388✔
3676
}
3677

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

3680
static int32_t vnodeProcessCompactVnodeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
74,135✔
3681
  if (!pVnode->restored) {
74,135✔
3682
    vInfo("vgId:%d, ignore compact req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
5,111✔
3683
    return 0;
5,111✔
3684
  }
3685
  return vnodeAsyncCompact(pVnode, ver, pReq, len, pRsp);
69,024✔
3686
}
3687

3688
static int32_t vnodeProcessConfigChangeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
3689
  if (syncCheckMember(pVnode->sync) != 0) {
×
3690
    vError("vgId:%d, failed to check member", TD_VID(pVnode));
×
3691
  }
3692

3693
  pRsp->msgType = TDMT_SYNC_CONFIG_CHANGE_RSP;
×
3694
  pRsp->code = TSDB_CODE_SUCCESS;
×
3695
  pRsp->pCont = NULL;
×
3696
  pRsp->contLen = 0;
×
3697

3698
  return 0;
×
3699
}
3700

3701
static int32_t vnodeCheckState(SVnode *pVnode) {
73,504✔
3702
  SSyncState syncState = syncGetState(pVnode->sync);
73,504✔
3703
  if (syncState.state != TAOS_SYNC_STATE_LEADER) {
73,504✔
3704
    return terrno = TSDB_CODE_SYN_NOT_LEADER;
32,468✔
3705
  }
3706
  return 0;
41,036✔
3707
}
3708

3709
static int32_t vnodeCheckToken(SVnode *pVnode, char *member0Token, char *member1Token) {
41,036✔
3710
  char token[TSDB_ARB_TOKEN_SIZE] = {0};
41,036✔
3711
  if (vnodeGetArbToken(pVnode, token) != 0) {
41,036✔
3712
    return terrno = TSDB_CODE_NOT_FOUND;
×
3713
  }
3714

3715
  if (strncmp(token, member0Token, TSDB_ARB_TOKEN_SIZE) != 0 &&
41,036✔
3716
      strncmp(token, member1Token, TSDB_ARB_TOKEN_SIZE) != 0) {
39,914✔
3717
    return terrno = TSDB_CODE_MND_ARB_TOKEN_MISMATCH;
35,104✔
3718
  }
3719

3720
  terrno = TSDB_CODE_SUCCESS;
5,932✔
3721
  return 0;
5,932✔
3722
}
3723

3724
static int32_t vnodeCheckSyncd(SVnode *pVnode, char *member0Token, char *member1Token) {
2,966✔
3725
  int32_t code = vnodeCheckToken(pVnode, member0Token, member1Token);
2,966✔
3726
  if (code != 0) {
2,966✔
UNCOV
3727
    return code;
×
3728
  }
3729

3730
  return syncCheckSynced(pVnode->sync);
2,966✔
3731
}
3732

3733
static int32_t vnodeProcessArbCheckSyncReq(SVnode *pVnode, void *pReq, int32_t len, SRpcMsg *pRsp) {
5,936✔
3734
  int32_t code = 0;
5,936✔
3735

3736
  if ((code = vnodeCheckState(pVnode)) != 0) {
5,936✔
3737
    vDebug("vgId:%d, failed to preprocess vnode-arb-check-sync request since %s", TD_VID(pVnode), tstrerror(code));
2,970✔
3738
    return 0;
2,970✔
3739
  }
3740

3741
  SVArbCheckSyncReq syncReq = {0};
2,966✔
3742

3743
  code = tDeserializeSVArbCheckSyncReq(pReq, len, &syncReq);
2,966✔
3744
  if (code) {
2,966✔
3745
    return code;
×
3746
  }
3747

3748
  vInfo("vgId:%d, start to process vnode-arb-check-sync req QID:0x%" PRIx64 ":0x%" PRIx64 ", seqNum:%" PRIx64
2,966✔
3749
        ", arbToken:%s, member0Token:%s, member1Token:%s, "
3750
        "arbTerm:%" PRId64,
3751
        TD_VID(pVnode), pRsp->info.traceId.rootId, pRsp->info.traceId.msgId, pRsp->info.seqNum, syncReq.arbToken,
3752
        syncReq.member0Token, syncReq.member1Token, syncReq.arbTerm);
3753

3754
  pRsp->msgType = TDMT_VND_ARB_CHECK_SYNC_RSP;
2,966✔
3755
  pRsp->code = TSDB_CODE_SUCCESS;
2,966✔
3756
  pRsp->pCont = NULL;
2,966✔
3757
  pRsp->contLen = 0;
2,966✔
3758

3759
  SVArbCheckSyncRsp syncRsp = {0};
2,966✔
3760
  syncRsp.arbToken = syncReq.arbToken;
2,966✔
3761
  syncRsp.member0Token = syncReq.member0Token;
2,966✔
3762
  syncRsp.member1Token = syncReq.member1Token;
2,966✔
3763
  syncRsp.vgId = TD_VID(pVnode);
2,966✔
3764

3765
  if ((syncRsp.errCode = vnodeCheckSyncd(pVnode, syncReq.member0Token, syncReq.member1Token)) != 0) {
2,966✔
UNCOV
3766
    vError("vgId:%d, failed to check assigned log syncd since %s", TD_VID(pVnode), tstrerror(syncRsp.errCode));
×
3767
  }
3768

3769
  if ((code = vnodeUpdateArbTerm(pVnode, syncReq.arbTerm)) != 0) {
2,966✔
3770
    vError("vgId:%d, failed to update arb term since %s", TD_VID(pVnode), tstrerror(code));
×
3771
    goto _OVER;
×
3772
  }
3773

3774
  int32_t contLen = tSerializeSVArbCheckSyncRsp(NULL, 0, &syncRsp);
2,966✔
3775
  if (contLen <= 0) {
2,966✔
3776
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
3777
    code = -1;
×
3778
    goto _OVER;
×
3779
  }
3780
  void *pHead = rpcMallocCont(contLen);
2,966✔
3781
  if (!pHead) {
2,966✔
3782
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
3783
    code = -1;
×
3784
    goto _OVER;
×
3785
  }
3786

3787
  if (tSerializeSVArbCheckSyncRsp(pHead, contLen, &syncRsp) <= 0) {
2,966✔
3788
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
3789
    rpcFreeCont(pHead);
×
3790
    code = -1;
×
3791
    goto _OVER;
×
3792
  }
3793

3794
  pRsp->pCont = pHead;
2,966✔
3795
  pRsp->contLen = contLen;
2,966✔
3796

3797
  vInfo(
2,966✔
3798
      "vgId:%d, suceed to process vnode-arb-check-sync req rsp.code:%s, arbToken:%s, member0Token:%s, "
3799
      "member1Token:%s",
3800
      TD_VID(pVnode), tstrerror(syncRsp.errCode), syncRsp.arbToken, syncRsp.member0Token, syncRsp.member1Token);
3801

3802
  code = TSDB_CODE_SUCCESS;
2,966✔
3803

3804
_OVER:
2,966✔
3805
  if (code != 0) {
2,966✔
3806
    vError("vgId:%d, failed to process arb check req rsp.code:%s since %s", TD_VID(pVnode), tstrerror(syncRsp.errCode),
×
3807
           tstrerror(code));
3808
  }
3809
  tFreeSVArbCheckSyncReq(&syncReq);
2,966✔
3810
  return code;
2,966✔
3811
}
3812

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