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

taosdata / TDengine / #5052

13 May 2026 12:00PM UTC coverage: 73.338% (-0.02%) from 73.358%
#5052

push

travis-ci

web-flow
feat: taosdump support stream backup/restore (#35326)

139 of 170 new or added lines in 3 files covered. (81.76%)

761 existing lines in 163 files now uncovered.

281469 of 383795 relevant lines covered (73.34%)

134502812.98 hits per line

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

70.17
/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) {
76,614,700✔
91
  int32_t code = 0;
76,614,700✔
92
  int32_t lino = 0;
76,614,700✔
93

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

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

105
  // name
106
  char *name = NULL;
76,616,903✔
107
  if (tDecodeCStr(pCoder, &name) < 0) {
76,615,167✔
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);
76,615,167✔
114
  if (uid == 0) {
76,609,044✔
115
    uid = tGenIdPI64();
76,143,575✔
116
  }
117
  taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), uid);
76,603,020✔
118

119
  // btime
120
  taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos + 8), btime);
76,596,525✔
121

122
  tEndDecode(pCoder);
76,602,673✔
123

124
_exit:
76,606,818✔
125
  if (code) {
76,606,344✔
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);
76,606,344✔
129
    if (pUid) *pUid = uid;
76,597,322✔
130
  }
131
  return code;
76,596,370✔
132
}
133
static int32_t vnodePreProcessCreateTableMsg(SVnode *pVnode, SRpcMsg *pMsg) {
52,711,011✔
134
  int32_t code = 0;
52,711,011✔
135
  int32_t lino = 0;
52,711,011✔
136

137
  int64_t  btime = taosGetTimestampMs();
52,711,376✔
138
  SDecoder dc = {0};
52,711,376✔
139
  int32_t  nReqs;
52,708,392✔
140

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

147
  if (tDecodeI32v(&dc, &nReqs) < 0) {
52,710,944✔
148
    code = TSDB_CODE_INVALID_MSG;
×
149
    TSDB_CHECK_CODE(code, lino, _exit);
×
150
  }
151
  for (int32_t iReq = 0; iReq < nReqs; iReq++) {
111,632,498✔
152
    code = vnodePreprocessCreateTableReq(pVnode, &dc, btime, NULL);
58,928,255✔
153
    TSDB_CHECK_CODE(code, lino, _exit);
58,921,554✔
154
  }
155

156
  tEndDecode(&dc);
52,704,243✔
157

158
_exit:
52,710,143✔
159
  tDecoderClear(&dc);
52,710,143✔
160
  if (code) {
52,708,650✔
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;
52,707,389✔
165
}
166

167
static int32_t vnodePreProcessAlterTableMsg(SVnode *pVnode, SRpcMsg *pMsg) {
13,698,953✔
168
  int32_t code = TSDB_CODE_INVALID_MSG;
13,698,953✔
169
  int32_t lino = 0;
13,698,953✔
170

171
  if (pVnode->config.isAudit) {
13,698,953✔
172
    return TSDB_CODE_PAR_PERMISSION_DENIED;
×
173
  }
174

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

178
  SVAlterTbReq vAlterTbReq = {0};
13,698,953✔
179
  int64_t      ctimeMs = taosGetTimestampMs();
13,698,953✔
180
  code = tDecodeSVAlterTbReqSetCtime(&dc, &vAlterTbReq, ctimeMs);
13,698,953✔
181

182
  destroyAlterTbReq(&vAlterTbReq);
13,698,953✔
183

184
_exit:
13,698,953✔
185
  tDecoderClear(&dc);
13,698,953✔
186
  if (code) {
13,698,953✔
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,
13,698,953✔
190
           ctimeMs);
191
  }
192
  return code;
13,698,953✔
193
}
194

195
static int32_t vnodePreProcessDropTtlMsg(SVnode *pVnode, SRpcMsg *pMsg) {
14,221,780✔
196
  int32_t code = TSDB_CODE_INVALID_MSG;
14,221,780✔
197
  int32_t lino = 0;
14,221,780✔
198

199
  SMsgHead *pContOld = pMsg->pCont;
14,221,780✔
200
  int32_t   reqLenOld = pMsg->contLen - sizeof(SMsgHead);
14,222,525✔
201

202
  SArray *tbUids = NULL;
14,224,997✔
203
  int64_t timestampMs = 0;
14,224,997✔
204

205
  SVDropTtlTableReq ttlReq = {0};
14,224,997✔
206
  if (tDeserializeSVDropTtlTableReq((char *)pContOld + sizeof(SMsgHead), reqLenOld, &ttlReq) != 0) {
14,220,380✔
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));
14,218,754✔
213
    if (tbUids == NULL) {
14,222,654✔
214
      code = terrno;
×
215
      TSDB_CHECK_CODE(code, lino, _exit);
×
216
    }
217

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

225
    ttlReq.nUids = taosArrayGetSize(tbUids);
14,213,491✔
226
    ttlReq.pTbUids = tbUids;
14,213,424✔
227
  }
228

229
  if (ttlReq.nUids == 0) {
14,213,424✔
230
    code = TSDB_CODE_MSG_PREPROCESSED;
14,195,185✔
231
    TSDB_CHECK_CODE(code, lino, _exit);
14,195,185✔
232
  }
233

234
  {  // prepare new content
235
    int32_t reqLenNew = tSerializeSVDropTtlTableReq(NULL, 0, &ttlReq);
10,271✔
236
    int32_t contLenNew = reqLenNew + sizeof(SMsgHead);
13,206✔
237

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

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

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

255
  code = 0;
13,206✔
256

257
_exit:
14,216,429✔
258
  taosArrayDestroy(tbUids);
14,216,840✔
259

260
  if (code && code != TSDB_CODE_MSG_PREPROCESSED) {
14,196,368✔
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);
14,196,368✔
265
  }
266

267
  return code;
14,196,354✔
268
}
269

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

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

280
  SSubmitTbData submitTbData;
803,434,264✔
281
  uint8_t       version;
282
  if (tDecodeI32v(pCoder, &submitTbData.flags) < 0) {
803,505,526✔
283
    code = TSDB_CODE_INVALID_MSG;
×
284
    TSDB_CHECK_CODE(code, lino, _exit);
×
285
  }
286
  version = (submitTbData.flags >> 8) & 0xff;
803,505,526✔
287
  submitTbData.flags = submitTbData.flags & 0xff;
803,505,526✔
288

289
  int64_t uid;
803,500,958✔
290
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
803,504,586✔
291
    code = vnodePreprocessCreateTableReq(pVnode, pCoder, btimeMs, &uid);
17,679,921✔
292
    TSDB_CHECK_CODE(code, lino, _exit);
17,679,376✔
293
  }
294

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

301
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
803,509,407✔
302
    taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), uid);
17,678,847✔
303
    pCoder->pos += sizeof(int64_t);
17,679,853✔
304
  } else {
305
    if (tDecodeI64(pCoder, &submitTbData.uid) < 0) {
785,822,884✔
306
      code = TSDB_CODE_INVALID_MSG;
×
307
      TSDB_CHECK_CODE(code, lino, _exit);
×
308
    }
309
  }
310

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

316
  // scan and check
317
  TSKEY now = btimeMs;
803,511,000✔
318
  if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_MICRO) {
803,511,000✔
319
    now *= 1000;
2,861,208✔
320
  } else if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_NANO) {
800,648,985✔
321
    now *= 1000000;
1,654,530✔
322
  }
323

324
  int32_t keep = pVnode->config.tsdbCfg.keep2;
803,509,083✔
325

326
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * keep;
803,509,390✔
327
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
803,510,846✔
328
  if (submitTbData.flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
803,507,667✔
329
    uint64_t nColData;
7,413,774✔
330
    if (tDecodeU64v(pCoder, &nColData) < 0) {
7,416,340✔
331
      code = TSDB_CODE_INVALID_MSG;
×
332
      TSDB_CHECK_CODE(code, lino, _exit);
×
333
    }
334

335
    SColData colData = {0};
7,416,340✔
336
    code = tDecodeColData(version, pCoder, &colData, false);
7,415,983✔
337
    if (code) {
7,415,269✔
338
      code = TSDB_CODE_INVALID_MSG;
×
339
      TSDB_CHECK_CODE(code, lino, _exit);
×
340
    }
341

342
    if (colData.flag != HAS_VALUE) {
7,415,269✔
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;
663✔
350
        TSDB_CHECK_CODE(code, lino, _exit);
663✔
351
      }
352
    }
353

354
    for (uint64_t i = 1; i < nColData; i++) {
22,848,956✔
355
      code = tDecodeColData(version, pCoder, &colData, true);
15,541,983✔
356
      if (code) {
15,543,794✔
357
        code = TSDB_CODE_INVALID_MSG;
×
358
        TSDB_CHECK_CODE(code, lino, _exit);
×
359
      }
360
    }
361
  } else {
362
    uint64_t nRow;
796,089,325✔
363
    if (tDecodeU64v(pCoder, &nRow) < 0) {
796,100,826✔
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;
75,247✔
378
        TSDB_CHECK_CODE(code, lino, _exit);
75,247✔
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)) {
803,433,311✔
390
    taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), ctimeMs);
803,433,755✔
391
    pCoder->pos += sizeof(int64_t);
803,334,404✔
392
  }
393

394
  tEndDecode(pCoder);
803,342,546✔
395

396
_exit:
803,485,493✔
397
  if (code) {
803,485,995✔
398
    vError("vgId:%d, %s:%d failed to vnodePreProcessSubmitTbData submit request since %s", TD_VID(pVnode), __func__,
75,910✔
399
           lino, tstrerror(code));
400
  }
401
  return code;
803,485,995✔
402
}
403
static int32_t vnodePreProcessSubmitMsg(SVnode *pVnode, SRpcMsg *pMsg) {
729,422,701✔
404
  int32_t code = 0;
729,422,701✔
405
  int32_t lino = 0;
729,422,701✔
406

407
  if (tsBypassFlag & TSDB_BYPASS_RA_RPC_RECV_SUBMIT) {
729,422,701✔
408
    return TSDB_CODE_MSG_PREPROCESSED;
87✔
409
  }
410

411
  SDecoder *pCoder = &(SDecoder){0};
729,422,614✔
412

413
  if (taosHton64(((SSubmitReq2Msg *)pMsg->pCont)->version) != 1) {
729,422,614✔
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));
729,421,810✔
419

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

425
  uint64_t nSubmitTbData;
729,417,986✔
426
  if (tDecodeU64v(pCoder, &nSubmitTbData) < 0) {
729,421,478✔
427
    code = TSDB_CODE_INVALID_MSG;
×
428
    TSDB_CHECK_CODE(code, lino, _exit);
×
429
  }
430

431
  int64_t btimeMs = taosGetTimestampMs();
729,421,869✔
432
  int64_t ctimeMs = btimeMs;
729,421,869✔
433
  for (uint64_t i = 0; i < nSubmitTbData; i++) {
1,532,826,780✔
434
    code = vnodePreProcessSubmitTbData(pVnode, pCoder, btimeMs, ctimeMs);
803,490,847✔
435
    TSDB_CHECK_CODE(code, lino, _exit);
803,480,821✔
436
  }
437

438
  tEndDecode(pCoder);
729,335,933✔
439

440
_exit:
729,412,927✔
441
  tDecoderClear(pCoder);
729,420,316✔
442
  if (code) {
729,415,678✔
443
    vError("vgId:%d, %s:%d failed to preprocess submit request since %s, msg type:%s", TD_VID(pVnode), __func__, lino,
75,910✔
444
           tstrerror(code), TMSG_INFO(pMsg->msgType));
445
  }
446
  return code;
729,410,457✔
447
}
448

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

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

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

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

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

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

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

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

485
  taosArrayDestroy(res.uidList);
1,977,174✔
486

487
_exit:
1,977,174✔
488
  return code;
1,977,812✔
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) {
24,286✔
517
  int32_t ret = 0;
24,286✔
518
  if ((ret = vnodeCheckState(pVnode)) != 0) {
24,286✔
519
    vDebug("vgId:%d, failed to preprocess vnode-arb-check-sync request since %s", TD_VID(pVnode), tstrerror(ret));
3,230✔
520
    return 0;
3,230✔
521
  }
522

523
  SVArbCheckSyncReq syncReq = {0};
21,056✔
524

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

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

535
  int32_t code = terrno;
21,056✔
536
  tFreeSVArbCheckSyncReq(&syncReq);
21,056✔
537

538
  return code;
21,056✔
539
}
540

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

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

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

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

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

583
  tEncodeSize(tEncodeSVDropTbBatchReq, &sentBatchReqs, size, code);
1,796,698✔
584
  tEncoderInit(&ec, POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), size);
1,796,698✔
585
  code = tEncodeSVDropTbBatchReq(&ec, &sentBatchReqs);
1,796,698✔
586
  tEncoderClear(&ec);
1,796,698✔
587
  if (code != TSDB_CODE_SUCCESS) {
1,796,698✔
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,796,698✔
593
  tDecoderClear(&dc);
1,796,698✔
594
  if (sentBatchReqs.pArray) {
1,796,698✔
595
    taosArrayDestroy(sentBatchReqs.pArray);
1,796,698✔
596
  }
597
  return code;
1,796,698✔
598
}
599

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

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

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

615
_exit:
4,120✔
616
  return code;
4,120✔
617
}
618

619
int32_t vnodePreProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
840,745,678✔
620
  int32_t code = 0;
840,745,678✔
621

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

628
  switch (pMsg->msgType) {
840,749,518✔
629
    case TDMT_VND_CREATE_TABLE: {
52,711,011✔
630
      code = vnodePreProcessCreateTableMsg(pVnode, pMsg);
52,711,011✔
631
    } break;
52,708,221✔
632
    case TDMT_VND_ALTER_TABLE: {
13,698,953✔
633
      code = vnodePreProcessAlterTableMsg(pVnode, pMsg);
13,698,953✔
634
    } break;
13,698,953✔
635
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
14,226,385✔
636
    case TDMT_VND_DROP_TTL_TABLE: {
637
      code = vnodePreProcessDropTtlMsg(pVnode, pMsg);
14,226,385✔
638
    } break;
14,211,178✔
639
    case TDMT_VND_SUBMIT: {
729,421,748✔
640
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.preprocess_time, METRIC_LEVEL_LOW,
729,421,748✔
641
                           { code = vnodePreProcessSubmitMsg(pVnode, pMsg); });
642
    } break;
729,414,520✔
643
    case TDMT_VND_DELETE: {
1,977,812✔
644
      code = vnodePreProcessDeleteMsg(pVnode, pMsg);
1,977,812✔
645
    } break;
1,977,174✔
646
    case TDMT_VND_BATCH_DEL: {
×
647
      code = vnodePreProcessBatchDeleteMsg(pVnode, pMsg);
×
648
    } break;
×
649
    case TDMT_VND_ARB_CHECK_SYNC: {
24,286✔
650
      code = vnodePreProcessArbCheckSyncMsg(pVnode, pMsg);
24,286✔
651
    } break;
24,286✔
652
    case TDMT_VND_DROP_TABLE: {
1,796,698✔
653
      code = vnodePreProcessDropTbMsg(pVnode, pMsg);
1,796,698✔
654
    } break;
1,796,698✔
655
#ifdef TD_ENTERPRISE
656
    case TDMT_VND_SSMIGRATE_FILESET: {
4,120✔
657
      code = vnodePreProcessSsMigrateFileSetReq(pVnode, pMsg);
4,120✔
658
    } break;
4,120✔
659
#endif
660
    case TDMT_VND_AUDIT_RECORD: {
778✔
661
      vTrace("vgId:%d, pre process TDMT_VND_AUDIT_RECORD", TD_VID(pVnode));
778✔
662
    } break;
656✔
663
    default:
26,891,101✔
664
      break;
26,891,101✔
665
  }
666

667
  if (code && code != TSDB_CODE_MSG_PREPROCESSED) {
840,726,907✔
668
    vError("vgId:%d, failed to preprocess write request since %s, msg type:%s", TD_VID(pVnode), tstrerror(code),
94,153✔
669
           TMSG_INFO(pMsg->msgType));
670
  }
671
  return code;
840,721,793✔
672
}
673

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

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

682
  SBseBatch *pBatch = NULL;
50,356✔
683

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

687
  SRow  **pRow = (SRow **)TARRAY_DATA(pSubmitTbData->aRowP);
50,356✔
688
  int32_t rowIdx = -1;
50,356✔
689
  for (int32_t i = 0; i < sz; i++) {
28,092,111✔
690
    int64_t     seq = 0;
28,041,755✔
691
    SBlobValue *p = taosArrayGet(pBlobSet->pSeqTable, i);
28,041,755✔
692
    if (p->type == TSDB_DATA_BLOB_EMPTY_VALUE || p->type == TSDB_DATA_BLOB_NULL_VALUE) {
28,041,755✔
693
      // skip empty or null blob
694
      continue;
2,784,851✔
695
    }
696

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

700
    SRow *row = taosArrayGetP(pSubmitTbData->aRowP, i);
25,256,904✔
701
    if (row == NULL) {
25,256,904✔
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) {
50,513,808✔
708
      code = TSDB_CODE_INVALID_MSG;
×
709
      TSDB_CHECK_CODE(code, lino, _exit);
×
710
    }
711
  }
712

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

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

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

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

738
  SBseBatch *pBatch = NULL;
325✔
739

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

743
  SColData *p = (SColData *)TARRAY_DATA(pSubmitTbData->aCol);
325✔
744
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitTbData->aCol); i++) {
3,695✔
745
    if (IS_STR_DATA_BLOB(p[i].type)) {
3,695✔
746
      pBlobCol = &p[i];
325✔
747
      break;
325✔
748
    }
749
  }
750
  if (pBlobCol == NULL) {
325✔
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;
325✔
757
  // int32_t   rowIdx = -1;
758
  for (int32_t i = 0; i < sz; i++) {
760✔
759
    int64_t     seq = 0;
435✔
760
    SBlobValue *p = taosArrayGet(pBlobSet->pSeqTable, i);
435✔
761
    if (p->type == TSDB_DATA_BLOB_EMPTY_VALUE || p->type == TSDB_DATA_BLOB_NULL_VALUE) {
435✔
762
      // skip empty or null blob
763
      continue;
280✔
764
    }
765
    code = bseBatchPut(pBatch, &seq, pBlobSet->data + p->offset, p->len);
155✔
766
    TSDB_CHECK_CODE(code, lino, _exit);
155✔
767

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

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

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

796
  return code;
50,681✔
797
}
798

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

806
  (void)taosThreadMutexLock(&pVnode->mutex);
1,346,907,611✔
807
  if (pVnode->disableWrite) {
1,346,970,225✔
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);
1,346,886,475✔
813

814
  if (ver <= pVnode->state.applied) {
1,346,982,771✔
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,
1,346,866,493✔
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)) {
1,346,912,254✔
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)) {
1,346,985,529✔
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);
1,346,986,862✔
839
  atomic_store_64(&pVnode->state.applyTerm, pMsg->info.conn.applyTerm);
1,346,989,270✔
840

841
  if (!syncUtilUserCommit(pMsg->msgType)) goto _exit;
1,346,988,138✔
842

843
  // skip header
844
  pReq = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
1,335,240,251✔
845
  len = pMsg->contLen - sizeof(SMsgHead);
1,335,239,554✔
846
  bool needCommit = false;
1,335,235,351✔
847
  bool forceTrimWal = false;
1,335,235,351✔
848

849
  switch (pMsg->msgType) {
1,335,235,351✔
850
    /* META */
851
    case TDMT_VND_CREATE_STB:
6,338,682✔
852
      code = vnodeProcessCreateStbReq(pVnode, ver, pReq, len, pRsp);
6,338,682✔
853
      TSDB_CHECK_CODE(code, lino, _err);
6,338,491✔
854
      break;
6,335,171✔
855
    case TDMT_VND_ALTER_STB:
8,662,754✔
856
      code = vnodeProcessAlterStbReq(pVnode, ver, pReq, len, pRsp);
8,662,754✔
857
      TSDB_CHECK_CODE(code, lino, _err);
8,662,754✔
858
      break;
8,662,754✔
859
    case TDMT_VND_DROP_STB:
1,043,320✔
860
      code = vnodeProcessDropStbReq(pVnode, ver, pReq, len, pRsp, pMsg);
1,043,320✔
861
      TSDB_CHECK_CODE(code, lino, _err);
1,043,320✔
862
      break;
1,043,320✔
863
    case TDMT_VND_CREATE_TABLE:
55,832,878✔
864
      code = vnodeProcessCreateTbReq(pVnode, ver, pReq, len, pRsp, pMsg);
55,832,878✔
865
      TSDB_CHECK_CODE(code, lino, _err);
55,824,722✔
866
      break;
55,824,722✔
867
    case TDMT_VND_ALTER_TABLE:
13,700,126✔
868
      code = vnodeProcessAlterTbReq(pVnode, ver, pReq, len, pRsp);
13,700,126✔
869
      TSDB_CHECK_CODE(code, lino, _err);
13,700,126✔
870
      break;
13,700,126✔
871
    case TDMT_VND_DROP_TABLE:
1,796,785✔
872
    case TDMT_VND_SNODE_DROP_TABLE:
873
      code = vnodeProcessDropTbReq(pVnode, ver, pReq, len, pRsp, pMsg);
1,796,785✔
874
      TSDB_CHECK_CODE(code, lino, _err);
1,796,785✔
875
      break;
1,796,785✔
876
    case TDMT_VND_CREATE_RSMA:
44,206✔
877
      code = vnodeProcessCreateRsmaReq(pVnode, ver, pReq, len, pRsp);
44,206✔
878
      TSDB_CHECK_CODE(code, lino, _err);
44,206✔
879
      break;
44,206✔
880
    case TDMT_VND_DROP_RSMA:
22,096✔
881
      code = vnodeProcessDropRsmaReq(pVnode, ver, pReq, len, pRsp);
22,096✔
882
      TSDB_CHECK_CODE(code, lino, _err);
22,096✔
883
      break;
22,096✔
884
    case TDMT_VND_ALTER_RSMA:
13,520✔
885
      code = vnodeProcessAlterRsmaReq(pVnode, ver, pReq, len, pRsp);
13,520✔
886
      TSDB_CHECK_CODE(code, lino, _err);
13,520✔
887
      break;
13,520✔
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:
13,206✔
893
      code = vnodeProcessFetchTtlExpiredTbs(pVnode, ver, pReq, len, pRsp);
13,206✔
894
      TSDB_CHECK_CODE(code, lino, _err);
13,206✔
895
      break;
13,206✔
896
    case TDMT_VND_TRIM:
104,295✔
897
      code = vnodeProcessTrimReq(pVnode, ver, pReq, len, pRsp);
104,295✔
898
      TSDB_CHECK_CODE(code, lino, _err);
104,295✔
899
      break;
104,295✔
900
      case TDMT_VND_TRIM_WAL:
7,632✔
901
        needCommit = true;
7,632✔
902
        forceTrimWal = true;
7,632✔
903
        break;
7,632✔
904
#ifdef TD_ENTERPRISE
905
    case TDMT_VND_SSMIGRATE_FILESET:
4,120✔
906
      if (vnodeProcessSsMigrateFileSetReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
4,120✔
907
      break;
4,120✔
908
    case TDMT_VND_FOLLOWER_SSMIGRATE:
7,416✔
909
      if (vnodeProcessFollowerSsMigrateReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
7,416✔
910
      break;
7,416✔
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: {
1,219,798,039✔
918
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.apply_time, METRIC_LEVEL_LOW, {
1,219,798,039✔
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);
1,219,770,577✔
922
      break;
1,219,766,512✔
923
    }
924
    case TDMT_VND_DELETE:
2,422,801✔
925
      code = vnodeProcessDeleteReq(pVnode, ver, pReq, len, pRsp, pMsg);
2,422,801✔
926
      TSDB_CHECK_CODE(code, lino, _err);
2,422,801✔
927
      break;
2,422,801✔
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,449,529✔
935
      code = tqProcessSubscribeReq(pVnode->pTq, ver, pReq, len);
1,449,529✔
936
      TSDB_CHECK_CODE(code, lino, _err);
1,445,948✔
937
      break;
1,445,948✔
938
    case TDMT_VND_TMQ_DELETE_SUB:
345,341✔
939
      code = tqProcessDeleteSubReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen);
345,341✔
940
      TSDB_CHECK_CODE(code, lino, _err);
344,534✔
941
      break;
344,534✔
942
    case TDMT_VND_TMQ_COMMIT_OFFSET:
12,878,201✔
943
      code = tqProcessOffsetCommitReq(pVnode->pTq, ver, pReq, len);
12,878,201✔
944
      TSDB_CHECK_CODE(code, lino, _err);
12,877,873✔
945
      break;
12,877,873✔
946
#endif
947
    case TDMT_VND_ALTER_CONFIRM:
4,363,845✔
948
      needCommit = pVnode->config.hashChange;
4,363,845✔
949
      code = vnodeProcessAlterConfirmReq(pVnode, ver, pReq, len, pRsp);
4,363,845✔
950
      TSDB_CHECK_CODE(code, lino, _err);
4,363,845✔
951
      break;
4,363,845✔
952
    case TDMT_VND_ALTER_CONFIG:
365,925✔
953
      vnodeProcessAlterConfigReq(pVnode, ver, pReq, len, pRsp);
365,925✔
954
      break;
365,925✔
955
    case TDMT_VND_COMMIT:
5,850,538✔
956
      needCommit = true;
5,850,538✔
957
      break;
5,850,538✔
958
    case TDMT_VND_CREATE_INDEX:
20,476✔
959
      vnodeProcessCreateIndexReq(pVnode, ver, pReq, len, pRsp);
20,476✔
960
      break;
20,476✔
961
    case TDMT_VND_DROP_INDEX:
16,640✔
962
      vnodeProcessDropIndexReq(pVnode, ver, pReq, len, pRsp);
16,640✔
963
      break;
16,640✔
964
    case TDMT_VND_COMPACT:
78,365✔
965
      vnodeProcessCompactVnodeReq(pVnode, ver, pReq, len, pRsp);
78,365✔
966
      goto _exit;
78,365✔
967
    case TDMT_VND_SCAN:
690✔
968
      vnodeProcessScanVnodeReq(pVnode, ver, pReq, len, pRsp);
690✔
969
      goto _exit;
690✔
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,660✔
975
      vnodeProcessKillCompactReq(pVnode, ver, pReq, len, pRsp);
1,660✔
976
      break;
1,660✔
977
#endif
978
    case TDMT_VND_KILL_SCAN:
136✔
979
      vnodeProcessKillScanReq(pVnode, ver, pReq, len, pRsp);
136✔
980
      break;
136✔
981
    case TDMT_VND_KILL_TRIM:
×
982
      vnodeProcessKillRetentionReq(pVnode, ver, pReq, len, pRsp);
×
983
      break;
×
984
    /* ARB */
985
    case TDMT_VND_ARB_CHECK_SYNC:
54,145✔
986
      vnodeProcessArbCheckSyncReq(pVnode, pReq, len, pRsp);
54,145✔
987
      break;
54,145✔
988
    case TDMT_VND_AUDIT_RECORD:
656✔
989
      vTrace("vgId:%d, processed audit msg", TD_VID(pVnode));
656✔
990
      code = vnodeProcessAuditRecordReq(pVnode, ver, pReq, len, pMsg);
656✔
991
      pRsp->code = code;
656✔
992
      pRsp->msgType = TDMT_VND_AUDIT_RECORD_RSP;
656✔
993
      pRsp->pCont = NULL;
656✔
994
      pRsp->contLen = 0;
656✔
995
      if (code) {
656✔
996
        goto _err;
×
997
      }
998
      break;
656✔
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);
1,335,111,058✔
1005

1006
  walApplyVer(pVnode->pWal, ver);
1,335,139,371✔
1007

1008
  // commit if need
1009
  if (needCommit) {
1,335,152,725✔
1010
    vInfo("vgId:%d, commit at version %" PRId64, TD_VID(pVnode), ver);
5,885,359✔
1011
    code = vnodeAsyncCommit(pVnode, forceTrimWal);
5,886,030✔
1012
    if (code) {
5,886,030✔
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,886,030✔
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:
1,335,150,324✔
1028
  return 0;
1,346,977,183✔
1029

1030
_err:
7,800✔
1031
  if (code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
7,800✔
1032
    vInfo("vgId:%d, process %s request failed since %s, lino 1:%d ver:%" PRId64, TD_VID(pVnode),
3,320✔
1033
          TMSG_INFO(pMsg->msgType), tstrerror(code), lino, ver);
1034
  } else if (code) {
4,480✔
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;
7,800✔
1040
}
1041

1042
int32_t vnodePreprocessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg) {
765,957,749✔
1043
  if (TDMT_SCH_QUERY != pMsg->msgType && TDMT_SCH_MERGE_QUERY != pMsg->msgType) {
765,957,749✔
1044
    return 0;
374,153,875✔
1045
  }
1046

1047
  int32_t qType = 0;
391,859,546✔
1048
  return qWorkerPreprocessQueryMsg(pVnode->pQuery, pMsg, (TDMT_SCH_QUERY == pMsg->msgType), &qType);
391,850,302✔
1049
}
1050

1051
int32_t vnodeProcessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
766,039,539✔
1052
  const STraceId *trace = &pMsg->info.traceId;
766,039,539✔
1053
  vDebug("vgId:%d, msg:%s, %p in vnode query queue is processing, %" PRIx64 ":%" PRIx64, 
766,047,980✔
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)) {
766,079,186✔
1057
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
786,005✔
1058
    return 0;
786,005✔
1059
  }
1060

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

1066
  SReadHandle handle = {0};
765,292,608✔
1067
  handle.vnode = pVnode;
765,292,462✔
1068
  handle.pMsgCb = &pVnode->msgCb;
765,292,462✔
1069
  handle.pWorkerCb = pInfo->workerCb;
765,253,617✔
1070
  initStorageAPI(&handle.api);
765,257,577✔
1071
  int32_t code = TSDB_CODE_SUCCESS;
765,187,042✔
1072
  bool    redirected = false;
765,187,042✔
1073

1074
  switch (pMsg->msgType) {
765,187,042✔
1075
    case TDMT_SCH_QUERY:
324,434,914✔
1076
      if (!syncIsReadyForRead(pVnode->sync)) {
324,434,914✔
1077
        pMsg->code = (terrno) ? terrno : TSDB_CODE_SYN_NOT_LEADER;
2,843,084✔
1078
        redirected = true;
2,843,084✔
1079
      }
1080
      code = qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
324,455,979✔
1081
      if (redirected) {
324,439,452✔
1082
        vnodeRedirectRpcMsg(pVnode, pMsg, pMsg->code);
2,843,084✔
1083
        return 0;
2,843,084✔
1084
      }
1085

1086
      return code;
321,596,368✔
1087
    case TDMT_SCH_MERGE_QUERY:
67,422,406✔
1088
      return qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
67,422,406✔
1089
    case TDMT_SCH_QUERY_CONTINUE:
57,217,302✔
1090
      return qWorkerProcessCQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
57,217,302✔
1091
    case TDMT_VND_TMQ_CONSUME:
316,146,938✔
1092
      return tqProcessPollReq(pVnode->pTq, pMsg);
316,146,938✔
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,166,951,002✔
1100
  int32_t code = TSDB_CODE_SUCCESS;
1,166,951,002✔
1101
  const STraceId *trace = &pMsg->info.traceId;
1,166,951,002✔
1102
  vDebug("vgId:%d, msg:%s, %p in fetch queue is processing, %" PRIx64 ":%" PRIx64, 
1,167,004,999✔
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,167,117,651✔
1105
       pMsg->msgType == TDMT_VND_BATCH_META || pMsg->msgType == TDMT_VND_TABLE_NAME ||
802,319,630✔
1106
       pMsg->msgType == TDMT_VND_VSUBTABLES_META || pMsg->msgType == TDMT_VND_VSTB_REF_DBS) &&
770,902,822✔
1107
      !syncIsReadyForRead(pVnode->sync)) {
396,236,803✔
1108
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
302,374✔
1109
    return 0;
302,374✔
1110
  }
1111

1112
  switch (pMsg->msgType) {
1,166,839,437✔
1113
    case TDMT_SCH_FETCH:
541,847,607✔
1114
    case TDMT_SCH_MERGE_FETCH:
1115
      return qWorkerProcessFetchMsg(pVnode, pVnode->pQuery, pMsg, 0);
541,847,607✔
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:
391,561,671✔
1121
      return qWorkerProcessDropMsg(pVnode, pVnode->pQuery, pMsg, 0);
391,561,671✔
1122
    case TDMT_SCH_TASK_NOTIFY:
42,072✔
1123
      return qWorkerProcessNotifyMsg(pVnode, pVnode->pQuery, pMsg, 0);
42,072✔
1124
    case TDMT_SCH_QUERY_HEARTBEAT:
200,628,963✔
1125
      return qWorkerProcessHbMsg(pVnode, pVnode->pQuery, pMsg, 0);
200,628,963✔
1126
    case TDMT_VND_TABLE_META:
1,434,805✔
1127
    case TDMT_VND_TABLE_NAME:
1128
      return vnodeGetTableMeta(pVnode, pMsg, true);
1,434,805✔
1129
    case TDMT_VND_TABLE_CFG:
×
1130
      return vnodeGetTableCfg(pVnode, pMsg, true);
×
1131
    case TDMT_VND_BATCH_META: {
31,115,350✔
1132
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.fetch_batch_meta_time, METRIC_LEVEL_LOW,
31,115,350✔
1133
                           { code = vnodeGetBatchMeta(pVnode, pMsg); });
1134
      METRICS_UPDATE(pVnode->writeMetrics.fetch_batch_meta_count, METRIC_LEVEL_LOW, 1);
31,120,120✔
1135
      return code;
31,113,661✔
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:
680✔
1142
      return vnodeQueryScanProgress(pVnode, pMsg);
680✔
1143
#ifdef TD_ENTERPRISE
1144
    case TDMT_VND_QUERY_COMPACT_PROGRESS:
×
1145
      return vnodeQueryCompactProgress(pVnode, pMsg);
×
1146

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

1150
    case TDMT_VND_QUERY_SSMIGRATE_PROGRESS:
7,416✔
1151
      return vnodeQuerySsMigrateProgress(pVnode, pMsg);
7,416✔
1152
#endif
1153
    case TDMT_VND_QUERY_TRIM_PROGRESS:
163,315✔
1154
      return vnodeQueryRetentionProgress(pVnode, pMsg);
163,315✔
1155
      //    case TDMT_VND_TMQ_CONSUME:
1156
      //      return tqProcessPollReq(pVnode->pTq, pMsg);
1157
#ifdef USE_TQ
1158
    case TDMT_VND_TMQ_VG_WALINFO:
734✔
1159
      return tqProcessVgWalInfoReq(pVnode->pTq, pMsg);
734✔
1160
    case TDMT_VND_TMQ_VG_COMMITTEDINFO:
1,795✔
1161
      return tqProcessVgCommittedInfoReq(pVnode->pTq, pMsg);
1,795✔
1162
#endif
1163
    default:
15,953✔
1164
      vError("unknown msg type:%d in fetch queue", pMsg->msgType);
15,953✔
1165
      return TSDB_CODE_APP_ERROR;
×
1166
  }
1167
}
1168

1169
void vnodeUpdateMetaRsp(SVnode *pVnode, STableMetaRsp *pMetaRsp) {
86,154,705✔
1170
  if (NULL == pMetaRsp) {
86,154,705✔
1171
    return;
×
1172
  }
1173

1174
  tstrncpy(pMetaRsp->dbFName, pVnode->config.dbname, TSDB_DB_FNAME_LEN);
86,154,705✔
1175
  pMetaRsp->dbId = pVnode->config.dbId;
86,156,503✔
1176
  pMetaRsp->vgId = TD_VID(pVnode);
86,155,562✔
1177
  pMetaRsp->precision = pVnode->config.tsdbCfg.precision;
86,156,493✔
1178
}
1179

1180
extern int32_t vnodeAsyncRetention(SVnode *pVnode, STimeWindow tw, int8_t optrType, int8_t triggerType);
1181

1182
static int32_t vnodeProcessTrimReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
104,295✔
1183
  if (!pVnode->restored) {
104,295✔
1184
    vInfo("vgId:%d, ignore trim req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
2,355✔
1185
    return 0;
2,355✔
1186
  }
1187

1188
  int32_t     code = 0;
101,940✔
1189
  SVTrimDbReq trimReq = {0};
101,940✔
1190

1191
  // decode
1192
  if (tDeserializeSVTrimDbReq(pReq, len, &trimReq) != 0) {
101,940✔
1193
    code = TSDB_CODE_INVALID_MSG;
×
1194
    goto _exit;
×
1195
  }
1196

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

1200
  code = vnodeAsyncRetention(pVnode, trimReq.tw, (int8_t)trimReq.optrType, (int8_t)trimReq.triggerType);
101,940✔
1201

1202
_exit:
101,940✔
1203
  return code;
101,940✔
1204
}
1205

1206
extern int32_t vnodeAsyncSsMigrateFileSet(SVnode *pVnode, SSsMigrateFileSetReq *pReq);
1207

1208
extern int32_t vnodeAsyncS3Migrate(SVnode *pVnode, int64_t now);
1209

1210
static int32_t vnodeProcessSsMigrateFileSetReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
4,120✔
1211
  int32_t code = 0;
4,120✔
1212

1213
  SSsMigrateFileSetReq req = {0};
4,120✔
1214
  SSsMigrateFileSetRsp rsp = {0};
4,120✔
1215
  pRsp->msgType = TDMT_VND_SSMIGRATE_FILESET_RSP;
4,120✔
1216
  pRsp->code = 0;
4,120✔
1217
  pRsp->pCont = NULL;
4,120✔
1218
  pRsp->contLen = 0;
4,120✔
1219

1220
  if (tDeserializeSSsMigrateFileSetReq(pReq, len, &req) != 0) {
4,120✔
1221
    code = TSDB_CODE_INVALID_MSG;
×
1222
    goto _exit;
×
1223
  }
1224

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

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

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

1249
_exit:
4,120✔
1250
  pRsp->code = code;
4,120✔
1251
  return code;
4,120✔
1252
}
1253

1254
extern int32_t vnodeFollowerSsMigrate(SVnode *pVnode, SSsMigrateProgress *pReq);
1255

1256
static int32_t vnodeProcessFollowerSsMigrateReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
7,416✔
1257
  int32_t            code = 0;
7,416✔
1258
  SSsMigrateProgress req = {0};
7,416✔
1259

1260
  // decode
1261
  if (tDeserializeSSsMigrateProgress(pReq, len, &req) != 0) {
7,416✔
1262
    code = TSDB_CODE_INVALID_MSG;
×
1263
    goto _exit;
×
1264
  }
1265

1266
  code = vnodeFollowerSsMigrate(pVnode, &req);
7,416✔
1267

1268
_exit:
7,416✔
1269
  pRsp->code = code;
7,416✔
1270
  return code;
7,416✔
1271
}
1272

1273
extern int32_t vnodeKillSsMigrate(SVnode *pVnode, SVnodeKillSsMigrateReq *pReq);
1274

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

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

1285
  code = vnodeKillSsMigrate(pVnode, &req);
×
1286

1287
_exit:
×
1288
  pRsp->code = code;
×
1289
  return code;
×
1290
}
1291

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

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

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

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

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

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

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

1337
  if (tDeserializeSVDropTtlTableReq(pReq, len, &ttlReq) != 0) {
13,206✔
1338
    terrno = TSDB_CODE_INVALID_MSG;
×
1339
    goto _end;
×
1340
  }
1341

1342
  if (!(ttlReq.nUids == taosArrayGetSize(ttlReq.pTbUids))) {
13,206✔
1343
    terrno = TSDB_CODE_INVALID_MSG;
×
1344
    goto _end;
×
1345
  }
1346

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

1355
  pNames = taosArrayInit(ttlReq.nUids, TSDB_TABLE_NAME_LEN);
13,206✔
1356
  if (!pNames) {
13,206✔
1357
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1358
    goto _end;
×
1359
  }
1360
  char buf[TSDB_TABLE_NAME_LEN];
13,206✔
1361
  for (int32_t i = 0; i < ttlReq.nUids; ++i) {
66,761✔
1362
    tb_uid_t *uid = taosArrayGet(ttlReq.pTbUids, i);
53,555✔
1363
    expiredTb.suid = *uid;
53,555✔
1364
    terrno = metaReaderGetTableEntryByUid(&mr, *uid);
53,555✔
1365
    if (terrno < 0) goto _end;
53,555✔
1366
    tstrncpy(buf, mr.me.name, TSDB_TABLE_NAME_LEN);
53,555✔
1367
    void *p = taosArrayPush(pNames, buf);
53,555✔
1368
    if (p == NULL) {
53,555✔
1369
      goto _end;
×
1370
    }
1371

1372
    expiredTb.name = p;
53,555✔
1373
    if (mr.me.type == TSDB_CHILD_TABLE) {
53,555✔
1374
      expiredTb.suid = mr.me.ctbEntry.suid;
34,517✔
1375
    }
1376

1377
    if (taosArrayPush(rsp.pExpiredTbs, &expiredTb) == NULL) {
107,110✔
1378
      goto _end;
×
1379
    }
1380
  }
1381

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

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

1404
static int32_t vnodeProcessCreateStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
6,333,480✔
1405
  int32_t        code = 0;
6,333,480✔
1406
  SVCreateStbReq req = {0};
6,333,480✔
1407
  SDecoder       coder;
6,331,665✔
1408

1409
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
6,338,636✔
1410
  pRsp->code = TSDB_CODE_SUCCESS;
6,338,682✔
1411
  pRsp->pCont = NULL;
6,338,636✔
1412
  pRsp->contLen = 0;
6,338,636✔
1413

1414
  // decode and process req
1415
  tDecoderInit(&coder, pReq, len);
6,337,987✔
1416

1417
  code = tDecodeSVCreateStbReq(&coder, &req);
6,338,051✔
1418
  if (code) {
6,331,377✔
1419
    pRsp->code = code;
×
1420
    goto _err;
×
1421
  }
1422

1423
  code = metaCreateSuperTable(pVnode->pMeta, ver, &req);
6,331,377✔
1424
  if (code) {
6,338,682✔
1425
    pRsp->code = code;
3,320✔
1426
    goto _err;
3,320✔
1427
  }
1428

1429
  tDecoderClear(&coder);
6,335,362✔
1430
  return 0;
6,333,829✔
1431

1432
_err:
3,320✔
1433
  tDecoderClear(&coder);
3,320✔
1434
  return code;
3,320✔
1435
}
1436

1437
static int32_t vnodeProcessCreateTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
55,832,942✔
1438
                                       SRpcMsg *pOriginRpc) {
1439
  SDecoder           decoder = {0};
55,832,942✔
1440
  SEncoder           encoder = {0};
55,833,717✔
1441
  int32_t            rcode = 0;
55,833,251✔
1442
  SVCreateTbBatchReq req = {0};
55,833,251✔
1443
  SVCreateTbReq     *pCreateReq;
1444
  SVCreateTbBatchRsp rsp = {0};
55,833,249✔
1445
  SVCreateTbRsp      cRsp = {0};
55,833,342✔
1446
  char               tbName[TSDB_TABLE_FNAME_LEN];
55,830,649✔
1447
  SArray            *tbUids = NULL;
55,833,342✔
1448
  SArray            *tbNames = NULL;
55,833,342✔
1449
  int64_t            tss = taosGetTimestampMs();
55,833,249✔
1450
  pRsp->msgType = TDMT_VND_CREATE_TABLE_RSP;
55,833,249✔
1451
  pRsp->code = TSDB_CODE_SUCCESS;
55,832,876✔
1452
  pRsp->pCont = NULL;
55,832,876✔
1453
  pRsp->contLen = 0;
55,833,249✔
1454

1455
  // decode
1456
  tDecoderInit(&decoder, pReq, len);
55,832,876✔
1457
  if (tDecodeSVCreateTbBatchReq(&decoder, &req) < 0) {
55,833,249✔
1458
    rcode = -1;
×
1459
    terrno = TSDB_CODE_INVALID_MSG;
×
1460
    goto _exit;
×
1461
  }
1462

1463
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(cRsp));
55,826,131✔
1464
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
55,822,258✔
1465
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
55,821,911✔
1466
  if (rsp.pArray == NULL || tbUids == NULL || tbNames == NULL) {
55,822,076✔
1467
    rcode = -1;
3,516✔
1468
    terrno = TSDB_CODE_OUT_OF_MEMORY;
3,516✔
1469
    goto _exit;
×
1470
  }
1471

1472
  // loop to create table
1473
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
120,481,711✔
1474
    pCreateReq = req.pReqs + iReq;
64,654,762✔
1475
    memset(&cRsp, 0, sizeof(cRsp));
64,653,972✔
1476

1477
    if (tsEnableAudit && tsEnableAuditCreateTable && tsAuditLevel >= AUDIT_LEVEL_CHILDTABLE) {
64,653,972✔
1478
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
332✔
1479
      if (str == NULL) {
332✔
1480
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1481
        rcode = -1;
×
1482
        goto _exit;
×
1483
      }
1484
      tstrncpy(str, pCreateReq->name, TSDB_TABLE_FNAME_LEN);
332✔
1485
      if (taosArrayPush(tbNames, &str) == NULL) {
332✔
1486
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1487
        rcode = -1;
×
1488
        goto _exit;
×
1489
      }
1490
    }
1491

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

1505
    // do create table
1506
    if (metaCreateTable2(pVnode->pMeta, ver, pCreateReq, &cRsp.pMeta) < 0) {
64,636,436✔
1507
      if (pCreateReq->flags & TD_CREATE_IF_NOT_EXISTS && terrno == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
19,074✔
1508
        cRsp.code = TSDB_CODE_SUCCESS;
5,544✔
1509
      } else {
1510
        cRsp.code = terrno;
13,530✔
1511
      }
1512
    } else {
1513
      cRsp.code = TSDB_CODE_SUCCESS;
64,644,807✔
1514
      if (taosArrayPush(tbUids, &pCreateReq->uid) == NULL) {
129,288,321✔
1515
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1516
        rcode = -1;
×
1517
        goto _exit;
×
1518
      }
1519
      vnodeUpdateMetaRsp(pVnode, cRsp.pMeta);
64,643,514✔
1520
    }
1521

1522
    if (taosArrayPush(rsp.pArray, &cRsp) == NULL) {
129,324,935✔
1523
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1524
      rcode = -1;
×
1525
      goto _exit;
×
1526
    }
1527
  }
1528

1529
  vTrace("vgId:%d, add %d new created tables into query table list", TD_VID(pVnode), (int32_t)taosArrayGetSize(tbUids));
55,826,949✔
1530
  if (tqAddTbUidListForQuerySub(pVnode->pTq, tbUids) < 0) {
55,826,949✔
1531
    vError("vgId:%d, failed to add tbUid list since %s", TD_VID(pVnode), tstrerror(terrno));
×
1532
  }
1533

1534
  // prepare rsp
1535
  int32_t ret = 0;
55,833,717✔
1536
  tEncodeSize(tEncodeSVCreateTbBatchRsp, &rsp, pRsp->contLen, ret);
55,833,717✔
1537
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
55,830,555✔
1538
  if (pRsp->pCont == NULL) {
55,822,857✔
1539
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1540
    rcode = -1;
×
1541
    goto _exit;
×
1542
  }
1543
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
55,824,672✔
1544
  if (tEncodeSVCreateTbBatchRsp(&encoder, &rsp) != 0) {
55,820,385✔
1545
    vError("vgId:%d, failed to encode create table batch response", TD_VID(pVnode));
×
1546
  }
1547

1548
  if (tsEnableAudit && tsEnableAuditCreateTable && tsAuditLevel >= AUDIT_LEVEL_CHILDTABLE) {
55,817,917✔
1549
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
332✔
1550

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

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

1566
    size_t len = 0;
332✔
1567
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
332✔
1568

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

1576
    taosStringBuilderDestroy(&sb);
332✔
1577
  }
1578

1579
_exit:
55,817,917✔
1580
  tDeleteSVCreateTbBatchReq(&req);
55,821,091✔
1581
  taosArrayDestroyEx(rsp.pArray, tFreeSVCreateTbRsp);
55,824,129✔
1582
  taosArrayDestroy(tbUids);
55,802,988✔
1583
  tDecoderClear(&decoder);
55,811,414✔
1584
  tEncoderClear(&encoder);
55,831,096✔
1585
  taosArrayDestroyP(tbNames, NULL);
55,826,411✔
1586
  return rcode;
55,825,181✔
1587
}
1588

1589
static int32_t vnodeProcessAlterStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
8,662,754✔
1590
  int32_t        code = 0;
8,662,754✔
1591
  SVCreateStbReq req = {0};
8,662,754✔
1592
  SDecoder       dc = {0};
8,662,754✔
1593

1594
  pRsp->msgType = TDMT_VND_ALTER_STB_RSP;
8,662,754✔
1595
  pRsp->code = TSDB_CODE_SUCCESS;
8,662,024✔
1596
  pRsp->pCont = NULL;
8,662,754✔
1597
  pRsp->contLen = 0;
8,662,024✔
1598

1599
  tDecoderInit(&dc, pReq, len);
8,662,754✔
1600

1601
  // decode req
1602
  code = tDecodeSVCreateStbReq(&dc, &req);
8,662,024✔
1603
  if (code) {
8,656,111✔
1604
    tDecoderClear(&dc);
×
1605
    return code;
×
1606
  }
1607

1608
  code = metaAlterSuperTable(pVnode->pMeta, ver, &req);
8,656,111✔
1609
  if (code) {
8,662,754✔
1610
    pRsp->code = code;
×
1611
    tDecoderClear(&dc);
×
1612
    return code;
×
1613
  }
1614

1615
  tDecoderClear(&dc);
8,662,754✔
1616

1617
  return 0;
8,662,754✔
1618
}
1619

1620
static int32_t vnodeProcessDropStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp, SRpcMsg *pOriginRpc) {
1,043,320✔
1621
  SVDropStbReq req = {0};
1,043,320✔
1622
  int32_t      rcode = TSDB_CODE_SUCCESS;
1,043,320✔
1623
  SDecoder     decoder = {0};
1,043,320✔
1624
  SArray      *tbUidList = NULL;
1,043,320✔
1625

1626
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
1,043,320✔
1627
  pRsp->pCont = NULL;
1,043,320✔
1628
  pRsp->contLen = 0;
1,043,320✔
1629

1630
  // decode request
1631
  tDecoderInit(&decoder, pReq, len);
1,043,320✔
1632
  if (tDecodeSVDropStbReq(&decoder, &req) < 0) {
1,043,320✔
1633
    rcode = TSDB_CODE_INVALID_MSG;
×
1634
    goto _exit;
×
1635
  }
1636

1637
  STraceId* trace = &(pOriginRpc->info.traceId);
1,043,320✔
1638

1639
  vInfo("vgId:%d, start to process vnode-drop-stb, QID:0x%" PRIx64 ":0x%" PRIx64 ", drop stb:%s", TD_VID(pVnode), trace ? trace->rootId : 0, 
1,043,320✔
1640
              trace ? trace->msgId : 0, req.name);
1641

1642
  // process request
1643
  tbUidList = taosArrayInit(8, sizeof(int64_t));
1,044,084✔
1644
  if (tbUidList == NULL) goto _exit;
1,043,320✔
1645
  if (vnodeGetCtbIdList(pVnode, req.suid, tbUidList) != 0){
1,043,320✔
1646
    rcode = terrno;
×
1647
    goto _exit;
×
1648
  }
1649
  if (metaDropSuperTable(pVnode->pMeta, ver, &req) < 0) {
1,041,894✔
1650
    rcode = terrno;
1,660✔
1651
    goto _exit;
1,660✔
1652
  }
1653
  if (tqDeleteTbUidList(pVnode->pTq, tbUidList) < 0) {
1,041,660✔
1654
    rcode = terrno;
×
1655
    goto _exit;
×
1656
  }
1657

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

1668
static SArray* getCidList(const SArray* tags) {
8,612,744✔
1669
  int32_t       code = 0;
8,612,744✔
1670
  int32_t       lino = 0;
8,612,744✔
1671
  SArray* cidList = taosArrayInit(taosArrayGetSize(tags), sizeof(col_id_t));
8,612,744✔
1672
  QUERY_CHECK_NULL(cidList, code, lino, end, terrno);
8,612,744✔
1673
  for (int32_t i = 0; i < taosArrayGetSize(tags); i++) {
17,292,937✔
1674
    SUpdatedTagVal *pTagVal = taosArrayGet(tags, i);
8,680,193✔
1675
    QUERY_CHECK_NULL(pTagVal, code, lino, end, terrno);
8,680,193✔
1676
    col_id_t cid = pTagVal->colId;
8,680,193✔
1677
    QUERY_CHECK_NULL(taosArrayPush(cidList, &cid), code, lino, end, terrno);
8,680,193✔
1678
  }
1679

1680
end:
8,612,744✔
1681
  if (code != 0) {
8,612,744✔
1682
    taosArrayDestroy(cidList);
×
1683
    return NULL;
×
1684
  }
1685
  return cidList;
8,612,744✔
1686
}
1687

1688
int32_t getCidInfo(const SArray* tags, const SArray* tagsArray, SArray** cidList, SArray** cidListArray) {
8,597,138✔
1689
  int32_t       code = 0;
8,597,138✔
1690
  int32_t       lino = 0;
8,597,138✔
1691
  SArray*       cids  = NULL;
8,597,138✔
1692

1693
  if (tags != NULL){
8,597,138✔
1694
    *cidList = getCidList(tags);
27,854✔
1695
    QUERY_CHECK_NULL(*cidList, code, lino, end, terrno);
27,854✔
1696
  }
1697

1698
  if (tagsArray != NULL) {
8,597,138✔
1699
    *cidListArray = taosArrayInit(taosArrayGetSize(tagsArray), sizeof(SArray*));
8,569,284✔
1700
    QUERY_CHECK_NULL(*cidListArray, code, lino, end, terrno);
8,569,284✔
1701

1702
    for (int32_t i = 0; i < taosArrayGetSize(tagsArray); i++) {
17,154,174✔
1703
      SArray   *obj = taosArrayGetP(tagsArray, i);
8,584,890✔
1704
      cids  = getCidList(obj);
8,584,890✔
1705
      QUERY_CHECK_NULL(cids, code, lino, end, terrno);
8,584,890✔
1706
      QUERY_CHECK_NULL(taosArrayPush(*cidListArray, &cids), code, lino, end, terrno);
17,169,780✔
1707
      cids = NULL;
8,584,890✔
1708
    }
1709
  }
1710

1711
end:
8,597,138✔
1712
  taosArrayDestroy(cids);
8,597,138✔
1713
  return code;
8,597,138✔
1714
}
1715

1716
// the elements in tbUidList and tagsArray are one-to-one correspondence
1717
void vnodeAlterTagForQuerySub(SVnode *pVnode, const SArray* tbUidList, const SArray* tags, const SArray* tagsArray) {
8,590,360✔
1718
  int32_t       code = 0;
8,590,360✔
1719
  int32_t       lino = 0;
8,590,360✔
1720
  SArray*       cidList = NULL;
8,590,360✔
1721
  SArray*       cidListArray = NULL;
8,590,360✔
1722

1723
  code = getCidInfo(tags, tagsArray, &cidList, &cidListArray);
8,590,360✔
1724
  QUERY_CHECK_CODE(code, lino, end);
8,590,360✔
1725

1726
  vDebug("vgId:%d, try to add %d tables in query table list, cidList size:%"PRIzu,
8,590,360✔
1727
         TD_VID(pVnode), (int32_t)taosArrayGetSize(tbUidList), taosArrayGetSize(cidList));
1728
  code = tqUpdateTbUidListForQuerySub(pVnode->pTq, tbUidList, cidList, cidListArray);
8,590,360✔
1729
  QUERY_CHECK_CODE(code, lino, end);
8,590,360✔
1730

1731
end:
8,590,360✔
1732
  if (code != 0) {
8,590,360✔
1733
    qError("vgId:%d, failed to alter tags for %d tables since %s",
×
1734
           TD_VID(pVnode), (int32_t)taosArrayGetSize(tbUidList), tstrerror(code));
1735
  }
1736
  taosArrayDestroy(cidList);
8,590,360✔
1737
  taosArrayDestroyP(cidListArray, (FDelete)taosArrayDestroy);
8,590,360✔
1738
}
8,590,360✔
1739

1740
static int32_t vnodeProcessAlterTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
13,700,126✔
1741
  SVAlterTbReq  vAlterTbReq = {0};
13,700,126✔
1742
  SVAlterTbRsp  vAlterTbRsp = {0};
13,700,126✔
1743
  SDecoder      dc = {0};
13,700,126✔
1744
  int32_t       code = 0;
13,700,126✔
1745
  int32_t       lino = 0;
13,700,126✔
1746
  int32_t       ret;
1747
  SEncoder      ec = {0};
13,700,126✔
1748
  STableMetaRsp vMetaRsp = {0};
13,700,126✔
1749

1750
  pRsp->msgType = TDMT_VND_ALTER_TABLE_RSP;
13,700,126✔
1751
  pRsp->pCont = NULL;
13,700,126✔
1752
  pRsp->contLen = 0;
13,700,126✔
1753
  pRsp->code = TSDB_CODE_SUCCESS;
13,700,126✔
1754

1755
  tDecoderInit(&dc, pReq, len);
13,700,126✔
1756

1757
  // decode
1758
  if (tDecodeSVAlterTbReq(&dc, &vAlterTbReq) < 0) {
13,700,126✔
1759
    vAlterTbRsp.code = TSDB_CODE_INVALID_MSG;
×
1760
    tDecoderClear(&dc);
×
1761
    goto _exit;
×
1762
  }
1763

1764
  // process
1765
  if (metaAlterTable(pVnode->pMeta, ver, &vAlterTbReq, &vMetaRsp) < 0) {
13,700,126✔
1766
    vAlterTbRsp.code = terrno;
627,209✔
1767
    tDecoderClear(&dc);
627,209✔
1768
    goto _exit;
627,209✔
1769
  }
1770
  tDecoderClear(&dc);
13,072,917✔
1771

1772
  if (NULL != vMetaRsp.pSchemas) {
13,072,917✔
1773
    vnodeUpdateMetaRsp(pVnode, &vMetaRsp);
4,254,138✔
1774
    vAlterTbRsp.pMeta = &vMetaRsp;
4,254,138✔
1775
  }
1776

1777
_exit:
13,699,898✔
1778
  destroyAlterTbReq(&vAlterTbReq);
13,700,126✔
1779
  tEncodeSize(tEncodeSVAlterTbRsp, &vAlterTbRsp, pRsp->contLen, ret);
13,700,126✔
1780
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
13,699,369✔
1781
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
13,700,126✔
1782
  if (tEncodeSVAlterTbRsp(&ec, &vAlterTbRsp) != 0) {
13,700,126✔
1783
    vError("vgId:%d, failed to encode alter table response", TD_VID(pVnode));
×
1784
  }
1785

1786
  tEncoderClear(&ec);
13,700,126✔
1787
  if (vMetaRsp.pSchemas) {
13,700,126✔
1788
    taosMemoryFree(vMetaRsp.pSchemas);
4,254,138✔
1789
    taosMemoryFree(vMetaRsp.pSchemaExt);
4,254,138✔
1790
  }
1791
  if (vMetaRsp.pColRefs) {
13,700,126✔
1792
    taosMemoryFree(vMetaRsp.pColRefs);
314,493✔
1793
  }
1794
  return 0;
13,700,126✔
1795
}
1796

1797
static int32_t vnodeProcessDropTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
1,796,785✔
1798
                                     SRpcMsg *pOriginRpc) {
1799
  SVDropTbBatchReq req = {0};
1,796,785✔
1800
  SVDropTbBatchRsp rsp = {0};
1,796,785✔
1801
  SDecoder         decoder = {0};
1,796,785✔
1802
  SEncoder         encoder = {0};
1,796,785✔
1803
  int32_t          ret;
1804
  SArray          *tbUids = NULL;
1,796,785✔
1805
  SArray          *tbNames = NULL;
1,796,785✔
1806
  int64_t          tss = taosGetTimestampMs();
1,796,785✔
1807

1808
  pRsp->msgType = ((SRpcMsg *)pReq)->msgType + 1;
1,796,785✔
1809
  pRsp->pCont = NULL;
1,796,785✔
1810
  pRsp->contLen = 0;
1,796,785✔
1811
  pRsp->code = TSDB_CODE_SUCCESS;
1,796,785✔
1812

1813
  // decode req
1814
  tDecoderInit(&decoder, pReq, len);
1,796,785✔
1815
  ret = tDecodeSVDropTbBatchReq(&decoder, &req);
1,796,785✔
1816
  if (ret < 0) {
1,796,785✔
1817
    terrno = TSDB_CODE_INVALID_MSG;
×
1818
    pRsp->code = terrno;
×
1819
    goto _exit;
×
1820
  }
1821

1822
  // process req
1823
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
1,796,785✔
1824
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(SVDropTbRsp));
1,796,785✔
1825
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
1,796,785✔
1826
  if (tbUids == NULL || rsp.pArray == NULL || tbNames == NULL) goto _exit;
1,796,785✔
1827

1828
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
3,654,084✔
1829
    SVDropTbReq *pDropTbReq = req.pReqs + iReq;
1,857,299✔
1830
    SVDropTbRsp  dropTbRsp = {0};
1,857,299✔
1831
    tb_uid_t     tbUid = 0;
1,857,299✔
1832

1833
    /* code */
1834
    ret = metaDropTable2(pVnode->pMeta, ver, pDropTbReq);
1,857,299✔
1835
    if (ret < 0) {
1,857,299✔
1836
      if (pDropTbReq->igNotExists && terrno == TSDB_CODE_TDB_TABLE_NOT_EXIST) {
×
1837
        dropTbRsp.code = TSDB_CODE_SUCCESS;
×
1838
      } else {
1839
        dropTbRsp.code = terrno;
×
1840
      }
1841
    } else {
1842
      dropTbRsp.code = TSDB_CODE_SUCCESS;
1,857,299✔
1843
    }
1844

1845
    if (taosArrayPush(rsp.pArray, &dropTbRsp) == NULL) {
3,714,598✔
1846
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1847
      pRsp->code = terrno;
×
1848
      goto _exit;
×
1849
    }
1850

1851
    if (taosArrayPush(tbUids, &pDropTbReq->uid) == NULL) {
3,714,598✔
1852
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1853
      pRsp->code = terrno;
×
1854
      goto _exit;
×
1855
    }
1856

1857
    if (tsEnableAudit && tsEnableAuditCreateTable && tsAuditLevel >= AUDIT_LEVEL_CHILDTABLE) {
1,857,299✔
1858
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
×
1859
      if (str == NULL) {
×
1860
        pRsp->code = terrno;
×
1861
        goto _exit;
×
1862
      }
1863
      tstrncpy(str, pDropTbReq->name, TSDB_TABLE_FNAME_LEN);
×
1864
      if (taosArrayPush(tbNames, &str) == NULL) {
×
1865
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1866
        pRsp->code = terrno;
×
1867
        goto _exit;
×
1868
      }
1869
    }
1870
  }
1871

1872
  if (tqDeleteTbUidList(pVnode->pTq, tbUids) < 0) {
1,796,785✔
1873
    vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(terrno));
×
1874
  }
1875

1876
  if (tsEnableAudit && tsEnableAuditCreateTable && tsAuditLevel >= AUDIT_LEVEL_CHILDTABLE) {
1,796,785✔
1877
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
×
1878

1879
    SName name = {0};
×
1880
    if (tNameFromString(&name, pVnode->config.dbname, T_NAME_ACCT | T_NAME_DB) != 0) {
×
1881
      vError("vgId:%d, failed to get name from string", TD_VID(pVnode));
×
1882
    }
1883

1884
    SStringBuilder sb = {0};
×
1885
    for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
×
1886
      char **key = (char **)taosArrayGet(tbNames, iReq);
×
1887
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
×
1888
      if (iReq < req.nReqs - 1) {
×
1889
        taosStringBuilderAppendChar(&sb, ',');
×
1890
      }
1891
      taosMemoryFreeClear(*key);
×
1892
    }
1893

1894
    size_t len = 0;
×
1895
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
×
1896

1897
    if (strlen(RPC_MSG_USER(pOriginRpc)) > 0) {
×
1898
      int64_t tse = taosGetTimestampMs();
×
1899
      double  duration = (double)(tse - tss);
×
1900
      duration = duration / 1000;
×
1901
      auditAddRecord(pOriginRpc, clusterId, "dropTable", name.dbname, "", keyJoined, len, duration, 0);
×
1902
    }
1903

1904
    taosStringBuilderDestroy(&sb);
×
1905
  }
1906

1907
_exit:
1,796,785✔
1908
  taosArrayDestroy(tbUids);
1,796,785✔
1909
  tDecoderClear(&decoder);
1,796,785✔
1910
  tEncodeSize(tEncodeSVDropTbBatchRsp, &rsp, pRsp->contLen, ret);
1,796,785✔
1911
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
1,796,785✔
1912
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
1,796,785✔
1913
  if (tEncodeSVDropTbBatchRsp(&encoder, &rsp) != 0) {
1,796,785✔
1914
    vError("vgId:%d, failed to encode drop table batch response", TD_VID(pVnode));
×
1915
  }
1916
  tEncoderClear(&encoder);
1,796,785✔
1917
  taosArrayDestroy(rsp.pArray);
1,796,785✔
1918
  taosArrayDestroy(tbNames);
1,796,785✔
1919
  return 0;
1,796,785✔
1920
}
1921

1922
static int32_t vnodeProcessCreateRsmaReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
44,206✔
1923
  int32_t         code = 0, lino = 0;
44,206✔
1924
  SVCreateRsmaReq req = {0};
44,206✔
1925
  TAOS_CHECK_EXIT(tDeserializeSVCreateRsmaReq(pReq, len, &req));
44,206✔
1926
  TAOS_CHECK_EXIT(metaCreateRsma(pVnode->pMeta, ver, &req));
44,206✔
1927
_exit:
44,206✔
1928
  pRsp->msgType = TDMT_VND_CREATE_RSMA_RSP;
44,206✔
1929
  pRsp->pCont = NULL;
44,206✔
1930
  pRsp->code = code;
44,206✔
1931
  pRsp->contLen = 0;
44,206✔
1932
  tFreeSVCreateRsmaReq(&req);
44,206✔
1933
  return code;
44,206✔
1934
}
1935

1936
static int32_t vnodeProcessDropRsmaReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
22,096✔
1937
  int32_t       code = 0, lino = 0;
22,096✔
1938
  SVDropRsmaReq req = {0};
22,096✔
1939
  TAOS_CHECK_EXIT(tDeserializeSVDropRsmaReq(pReq, len, &req));
22,096✔
1940
  TAOS_CHECK_EXIT(metaDropRsma(pVnode->pMeta, ver, &req));
22,096✔
1941
_exit:
22,096✔
1942
  pRsp->msgType = TDMT_VND_DROP_RSMA_RSP;
22,096✔
1943
  pRsp->pCont = NULL;
22,096✔
1944
  pRsp->code = code;
22,096✔
1945
  pRsp->contLen = 0;
22,096✔
1946
  return 0;
22,096✔
1947
}
1948

1949
static int32_t vnodeProcessAlterRsmaReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
13,520✔
1950
  int32_t         code = 0, lino = 0;
13,520✔
1951
  SVAlterRsmaReq req = {0};
13,520✔
1952
  TAOS_CHECK_EXIT(tDeserializeSVAlterRsmaReq(pReq, len, &req));
13,520✔
1953
  TAOS_CHECK_EXIT(metaAlterRsma(pVnode->pMeta, ver, &req));
13,520✔
1954
_exit:
13,520✔
1955
  pRsp->msgType = TDMT_VND_ALTER_RSMA_RSP;
13,520✔
1956
  pRsp->pCont = NULL;
13,520✔
1957
  pRsp->code = code;
13,520✔
1958
  pRsp->contLen = 0;
13,520✔
1959
  tFreeSVAlterRsmaReq(&req);
13,520✔
1960
  return code;
13,520✔
1961
}
1962

1963
#ifdef BUILD_NO_CALL
1964
static int32_t vnodeDebugPrintSingleSubmitMsg(SMeta *pMeta, SSubmitBlk *pBlock, SSubmitMsgIter *msgIter,
1965
                                              const char *tags) {
1966
  SSubmitBlkIter blkIter = {0};
1967
  STSchema      *pSchema = NULL;
1968
  tb_uid_t       suid = 0;
1969
  STSRow        *row = NULL;
1970
  int32_t        rv = -1;
1971

1972
  tInitSubmitBlkIter(msgIter, pBlock, &blkIter);
1973
  if (blkIter.row == NULL) return 0;
1974

1975
  int32_t code = metaGetTbTSchemaNotNull(pMeta, msgIter->suid, TD_ROW_SVER(blkIter.row), 1,
1976
                                         &pSchema);  // TODO: use the real schema
1977
  if (TSDB_CODE_SUCCESS != code) {
1978
    printf("%s:%d no valid schema\n", tags, __LINE__);
1979
    return code;
1980
  }
1981

1982
  suid = msgIter->suid;
1983
  rv = TD_ROW_SVER(blkIter.row);
1984

1985
  char __tags[128] = {0};
1986
  snprintf(__tags, 128, "%s: uid %" PRIi64 " ", tags, msgIter->uid);
1987
  while ((row = tGetSubmitBlkNext(&blkIter))) {
1988
    tdSRowPrint(row, pSchema, __tags);
1989
  }
1990

1991
  taosMemoryFreeClear(pSchema);
1992

1993
  return TSDB_CODE_SUCCESS;
1994
}
1995
#endif
1996
typedef struct SSubmitReqConvertCxt {
1997
  SSubmitMsgIter msgIter;
1998
  SSubmitBlk    *pBlock;
1999
  SSubmitBlkIter blkIter;
2000
  STSRow        *pRow;
2001
  STSRowIter     rowIter;
2002
  SSubmitTbData *pTbData;
2003
  STSchema      *pTbSchema;
2004
  SArray        *pColValues;
2005
} SSubmitReqConvertCxt;
2006

2007
static int32_t vnodeResetTableCxt(SMeta *pMeta, SSubmitReqConvertCxt *pCxt) {
×
2008
  taosMemoryFreeClear(pCxt->pTbSchema);
×
2009
  int32_t code = metaGetTbTSchemaNotNull(pMeta, pCxt->msgIter.suid > 0 ? pCxt->msgIter.suid : pCxt->msgIter.uid,
×
2010
                                         pCxt->msgIter.sversion, 1, &pCxt->pTbSchema);
2011
  if (TSDB_CODE_SUCCESS != code) {
×
2012
    return code;
×
2013
  }
2014
  tdSTSRowIterInit(&pCxt->rowIter, pCxt->pTbSchema);
×
2015

2016
  tDestroySubmitTbData(pCxt->pTbData, TSDB_MSG_FLG_ENCODE);
×
2017
  if (NULL == pCxt->pTbData) {
×
2018
    pCxt->pTbData = taosMemoryCalloc(1, sizeof(SSubmitTbData));
×
2019
    if (NULL == pCxt->pTbData) {
×
2020
      return terrno;
×
2021
    }
2022
  }
2023
  pCxt->pTbData->flags = 0;
×
2024
  pCxt->pTbData->suid = pCxt->msgIter.suid;
×
2025
  pCxt->pTbData->uid = pCxt->msgIter.uid;
×
2026
  pCxt->pTbData->sver = pCxt->msgIter.sversion;
×
2027
  pCxt->pTbData->pCreateTbReq = NULL;
×
2028
  pCxt->pTbData->aRowP = taosArrayInit(128, POINTER_BYTES);
×
2029
  if (NULL == pCxt->pTbData->aRowP) {
×
2030
    return terrno;
×
2031
  }
2032

2033
  taosArrayDestroy(pCxt->pColValues);
×
2034
  pCxt->pColValues = taosArrayInit(pCxt->pTbSchema->numOfCols, sizeof(SColVal));
×
2035
  if (NULL == pCxt->pColValues) {
×
2036
    return terrno;
×
2037
  }
2038
  for (int32_t i = 0; i < pCxt->pTbSchema->numOfCols; ++i) {
×
2039
    SColVal val = COL_VAL_NONE(pCxt->pTbSchema->columns[i].colId, pCxt->pTbSchema->columns[i].type);
×
2040
    if (taosArrayPush(pCxt->pColValues, &val) == NULL) {
×
2041
      return terrno;
×
2042
    }
2043
  }
2044

2045
  return TSDB_CODE_SUCCESS;
×
2046
}
2047

2048
static void vnodeDestroySubmitReqConvertCxt(SSubmitReqConvertCxt *pCxt) {
×
2049
  taosMemoryFreeClear(pCxt->pTbSchema);
×
2050
  tDestroySubmitTbData(pCxt->pTbData, TSDB_MSG_FLG_ENCODE);
×
2051
  taosMemoryFreeClear(pCxt->pTbData);
×
2052
  taosArrayDestroy(pCxt->pColValues);
×
2053
}
×
2054

2055
static int32_t vnodeCellValConvertToColVal(STColumn *pCol, SCellVal *pCellVal, SColVal *pColVal) {
×
2056
  if (tdValTypeIsNone(pCellVal->valType)) {
×
2057
    pColVal->flag = CV_FLAG_NONE;
×
2058
    return TSDB_CODE_SUCCESS;
×
2059
  }
2060

2061
  if (tdValTypeIsNull(pCellVal->valType)) {
×
2062
    pColVal->flag = CV_FLAG_NULL;
×
2063
    return TSDB_CODE_SUCCESS;
×
2064
  }
2065

2066
  if (IS_VAR_DATA_TYPE(pCol->type)) {
×
2067
    if (IS_STR_DATA_BLOB(pCol->type)) {
×
2068
      pColVal->value.nData = blobDataLen(pCellVal->val);
×
2069
      pColVal->value.pData = (uint8_t *)blobDataVal(pCellVal->val);
×
2070

2071
    } else {
2072
      pColVal->value.nData = varDataLen(pCellVal->val);
×
2073
      pColVal->value.pData = (uint8_t *)varDataVal(pCellVal->val);
×
2074
    }
2075
  } else if (TSDB_DATA_TYPE_FLOAT == pCol->type) {
×
2076
    float f = GET_FLOAT_VAL(pCellVal->val);
×
2077
    valueSetDatum(&pColVal->value, pCol->type, &f, sizeof(f));
×
2078
  } else if (TSDB_DATA_TYPE_DOUBLE == pCol->type) {
×
2079
    taosSetPInt64Aligned(&pColVal->value.val, (int64_t *)pCellVal->val);
×
2080
  } else {
2081
    valueSetDatum(&pColVal->value, pCol->type, pCellVal->val, tDataTypes[pCol->type].bytes);
×
2082
  }
2083

2084
  pColVal->flag = CV_FLAG_VALUE;
×
2085
  return TSDB_CODE_SUCCESS;
×
2086
}
2087

2088
static int32_t vnodeTSRowConvertToColValArray(SSubmitReqConvertCxt *pCxt) {
×
2089
  int32_t code = TSDB_CODE_SUCCESS;
×
2090
  tdSTSRowIterReset(&pCxt->rowIter, pCxt->pRow);
×
2091
  for (int32_t i = 0; TSDB_CODE_SUCCESS == code && i < pCxt->pTbSchema->numOfCols; ++i) {
×
2092
    STColumn *pCol = pCxt->pTbSchema->columns + i;
×
2093
    SCellVal  cellVal = {0};
×
2094
    if (!tdSTSRowIterFetch(&pCxt->rowIter, pCol->colId, pCol->type, &cellVal)) {
×
2095
      break;
×
2096
    }
2097
    code = vnodeCellValConvertToColVal(pCol, &cellVal, (SColVal *)taosArrayGet(pCxt->pColValues, i));
×
2098
  }
2099
  return code;
×
2100
}
2101

2102
static int32_t vnodeDecodeCreateTbReq(SSubmitReqConvertCxt *pCxt) {
×
2103
  if (pCxt->msgIter.schemaLen <= 0) {
×
2104
    return TSDB_CODE_SUCCESS;
×
2105
  }
2106

2107
  pCxt->pTbData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
×
2108
  if (NULL == pCxt->pTbData->pCreateTbReq) {
×
2109
    return terrno;
×
2110
  }
2111

2112
  SDecoder decoder = {0};
×
2113
  tDecoderInit(&decoder, (uint8_t *)pCxt->pBlock->data, pCxt->msgIter.schemaLen);
×
2114
  int32_t code = tDecodeSVCreateTbReq(&decoder, pCxt->pTbData->pCreateTbReq);
×
2115
  tDecoderClear(&decoder);
×
2116

2117
  return code;
×
2118
}
2119

2120
static int32_t vnodeSubmitReqConvertToSubmitReq2(SVnode *pVnode, SSubmitReq *pReq, SSubmitReq2 *pReq2) {
×
2121
  pReq2->aSubmitTbData = taosArrayInit(128, sizeof(SSubmitTbData));
×
2122
  if (NULL == pReq2->aSubmitTbData) {
×
2123
    return terrno;
×
2124
  }
2125

2126
  SSubmitReqConvertCxt cxt = {0};
×
2127

2128
  int32_t code = tInitSubmitMsgIter(pReq, &cxt.msgIter);
×
2129
  while (TSDB_CODE_SUCCESS == code) {
×
2130
    code = tGetSubmitMsgNext(&cxt.msgIter, &cxt.pBlock);
×
2131
    if (TSDB_CODE_SUCCESS == code) {
×
2132
      if (NULL == cxt.pBlock) {
×
2133
        break;
×
2134
      }
2135
      code = vnodeResetTableCxt(pVnode->pMeta, &cxt);
×
2136
    }
2137
    if (TSDB_CODE_SUCCESS == code) {
×
2138
      code = tInitSubmitBlkIter(&cxt.msgIter, cxt.pBlock, &cxt.blkIter);
×
2139
    }
2140
    if (TSDB_CODE_SUCCESS == code) {
×
2141
      code = vnodeDecodeCreateTbReq(&cxt);
×
2142
    }
2143
    while (TSDB_CODE_SUCCESS == code && (cxt.pRow = tGetSubmitBlkNext(&cxt.blkIter)) != NULL) {
×
2144
      code = vnodeTSRowConvertToColValArray(&cxt);
×
2145
      if (TSDB_CODE_SUCCESS == code) {
×
2146
        SRow **pNewRow = taosArrayReserve(cxt.pTbData->aRowP, 1);
×
2147

2148
        SRowBuildScanInfo sinfo = {0};
×
2149
        code = tRowBuild(cxt.pColValues, cxt.pTbSchema, pNewRow, &sinfo);
×
2150
      }
2151
    }
2152
    if (TSDB_CODE_SUCCESS == code) {
×
2153
      code = (NULL == taosArrayPush(pReq2->aSubmitTbData, cxt.pTbData) ? terrno : TSDB_CODE_SUCCESS);
×
2154
    }
2155
    if (TSDB_CODE_SUCCESS == code) {
×
2156
      taosMemoryFreeClear(cxt.pTbData);
×
2157
    }
2158
  }
2159

2160
  vnodeDestroySubmitReqConvertCxt(&cxt);
×
2161
  return code;
×
2162
}
2163

2164
static int32_t vnodeRebuildSubmitReqMsg(SSubmitReq2 *pSubmitReq, void **ppMsg) {
×
2165
  int32_t  code = TSDB_CODE_SUCCESS;
×
2166
  char    *pMsg = NULL;
×
2167
  uint32_t msglen = 0;
×
2168
  tEncodeSize(tEncodeSubmitReq, pSubmitReq, msglen, code);
×
2169
  if (TSDB_CODE_SUCCESS == code) {
×
2170
    pMsg = taosMemoryMalloc(msglen);
×
2171
    if (NULL == pMsg) {
×
2172
      code = terrno;
×
2173
    }
2174
  }
2175
  if (TSDB_CODE_SUCCESS == code) {
×
2176
    SEncoder encoder;
×
2177
    tEncoderInit(&encoder, (uint8_t *)pMsg, msglen);
×
2178
    code = tEncodeSubmitReq(&encoder, pSubmitReq);
×
2179
    tEncoderClear(&encoder);
×
2180
  }
2181
  if (TSDB_CODE_SUCCESS == code) {
×
2182
    *ppMsg = pMsg;
×
2183
  }
2184
  return code;
×
2185
}
2186

2187
static int32_t buildExistSubTalbeRsp(SVnode *pVnode, SSubmitTbData *pSubmitTbData, STableMetaRsp **ppRsp) {
78,661✔
2188
  int32_t code = 0;
78,661✔
2189
  int32_t lino = 0;
78,661✔
2190

2191
  SMetaEntry *pEntry = NULL;
78,661✔
2192
  code = metaFetchEntryByUid(pVnode->pMeta, pSubmitTbData->suid, &pEntry);
78,661✔
2193
  if (code) {
78,661✔
2194
    vError("vgId:%d, table uid:%" PRId64 " not exists, line:%d", TD_VID(pVnode), pSubmitTbData->uid, __LINE__);
×
2195
    TSDB_CHECK_CODE(code, lino, _exit);
×
2196
  }
2197
  if (pEntry->type != TSDB_SUPER_TABLE) {
78,661✔
2198
    vError("vgId:%d, table uid:%" PRId64 " exists, but is not super table, line:%d", TD_VID(pVnode), pSubmitTbData->uid,
×
2199
           __LINE__);
2200
    code = TSDB_CODE_STREAM_INSERT_SCHEMA_NOT_MATCH;
×
2201
    TSDB_CHECK_CODE(code, lino, _exit);
×
2202
  }
2203

2204
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
78,661✔
2205
  if (NULL == *ppRsp) {
78,661✔
2206
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2207
  }
2208
  (*ppRsp)->suid = pSubmitTbData->suid;
78,661✔
2209
  (*ppRsp)->tuid = pSubmitTbData->uid;
78,661✔
2210
  (*ppRsp)->sversion = pEntry->stbEntry.schemaRow.version;
78,661✔
2211
  (*ppRsp)->vgId = pVnode->config.vgId;
78,661✔
2212
  (*ppRsp)->numOfColumns = pEntry->stbEntry.schemaRow.nCols;
78,661✔
2213
  (*ppRsp)->numOfTags = pEntry->stbEntry.schemaTag.nCols;
78,661✔
2214
  (*ppRsp)->pSchemas =
78,661✔
2215
      taosMemoryCalloc(pEntry->stbEntry.schemaRow.nCols + pEntry->stbEntry.schemaTag.nCols, sizeof(SSchema));
78,661✔
2216
  if (NULL == (*ppRsp)->pSchemas) {
78,661✔
2217
    taosMemoryFree(*ppRsp);
×
2218
    *ppRsp = NULL;
×
2219
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2220
  }
2221
  memcpy((*ppRsp)->pSchemas, pEntry->stbEntry.schemaRow.pSchema, pEntry->stbEntry.schemaRow.nCols * sizeof(SSchema));
78,661✔
2222
  memcpy((*ppRsp)->pSchemas + pEntry->stbEntry.schemaRow.nCols, pEntry->stbEntry.schemaTag.pSchema,
78,661✔
2223
         pEntry->stbEntry.schemaTag.nCols * sizeof(SSchema));
78,661✔
2224
  if (pEntry->pExtSchemas != NULL) {
78,661✔
2225
    (*ppRsp)->pSchemaExt = taosMemoryCalloc(pEntry->colCmpr.nCols, sizeof(SExtSchema));
×
2226
    if (NULL == (*ppRsp)->pSchemaExt) {
×
2227
      taosMemoryFree((*ppRsp)->pSchemas);
×
2228
      taosMemoryFree(*ppRsp);
×
2229
      *ppRsp = NULL;
×
2230
      TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2231
    }
2232
    memcpy((*ppRsp)->pSchemaExt, pEntry->pExtSchemas, pEntry->colCmpr.nCols * sizeof(SExtSchema));
×
2233
  }
2234

2235
  if (pEntry->stbEntry.schemaRow.version != pSubmitTbData->sver) {
78,661✔
2236
    TSDB_CHECK_CODE(code = TSDB_CODE_TDB_TABLE_ALREADY_EXIST, lino, _exit);
1,084✔
2237
  }
2238
_exit:
78,661✔
2239
  metaFetchEntryFree(&pEntry);
78,661✔
2240
  if (code != TSDB_CODE_SUCCESS) {
78,661✔
2241
    vError("vgId:%d, failed to build exist sub table response, code:0x%0x, line:%d", TD_VID(pVnode), code, lino);
1,084✔
2242
  }
2243
  return code;
78,661✔
2244
}
2245

2246
static int32_t buildExistNormalTalbeRsp(SVnode *pVnode, int64_t uid, STableMetaRsp **ppRsp) {
51,062✔
2247
  int32_t code = 0;
51,062✔
2248
  int32_t lino = 0;
51,062✔
2249

2250
  SMetaEntry *pEntry = NULL;
51,062✔
2251
  code = metaFetchEntryByUid(pVnode->pMeta, uid, &pEntry);
51,062✔
2252
  if (code) {
51,062✔
2253
    vError("vgId:%d, table uid:%" PRId64 " not exists, line:%d", TD_VID(pVnode), uid, __LINE__);
×
2254
    TSDB_CHECK_CODE(code, lino, _exit);
×
2255
  }
2256
  if (pEntry->type != TSDB_NORMAL_TABLE) {
51,062✔
2257
    vError("vgId:%d, table uid:%" PRId64 " exists, but is not normal table, line:%d", TD_VID(pVnode), uid, __LINE__);
×
2258
    code = TSDB_CODE_STREAM_INSERT_SCHEMA_NOT_MATCH;
×
2259
    TSDB_CHECK_CODE(code, lino, _exit);
×
2260
  }
2261

2262
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
51,062✔
2263
  if (NULL == *ppRsp) {
51,062✔
2264
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2265
  }
2266

2267
  (*ppRsp)->tuid = pEntry->uid;
51,062✔
2268
  (*ppRsp)->vgId = pVnode->config.vgId;
51,062✔
2269
  (*ppRsp)->sversion = pEntry->ntbEntry.schemaRow.version;
51,062✔
2270
  (*ppRsp)->numOfColumns = pEntry->ntbEntry.schemaRow.nCols;
51,062✔
2271
  (*ppRsp)->pSchemas = taosMemoryCalloc(pEntry->ntbEntry.schemaRow.nCols, sizeof(SSchema));
51,062✔
2272
  if (NULL == (*ppRsp)->pSchemas) {
51,062✔
2273
    taosMemoryFree(*ppRsp);
×
2274
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2275
  }
2276
  memcpy((*ppRsp)->pSchemas, pEntry->ntbEntry.schemaRow.pSchema, pEntry->ntbEntry.schemaRow.nCols * sizeof(SSchema));
51,062✔
2277
  if (pEntry->pExtSchemas != NULL) {
51,062✔
2278
    (*ppRsp)->pSchemaExt = taosMemoryCalloc(pEntry->ntbEntry.schemaRow.nCols, sizeof(SSchemaExt));
×
2279
    if (NULL == (*ppRsp)->pSchemaExt) {
×
2280
      taosMemoryFree((*ppRsp)->pSchemas);
×
2281
      taosMemoryFree(*ppRsp);
×
2282
      TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2283
    }
2284
    memcpy((*ppRsp)->pSchemaExt, pEntry->pExtSchemas, pEntry->ntbEntry.schemaRow.nCols * sizeof(SSchemaExt));
×
2285
  }
2286

2287
_exit:
51,062✔
2288
  metaFetchEntryFree(&pEntry);
51,062✔
2289
  if (code != TSDB_CODE_SUCCESS) {
51,062✔
2290
    vError("vgId:%d, failed to build exist normal table response, code:%d, line:%d", TD_VID(pVnode), code, lino);
×
2291
  }
2292
  return code;
51,062✔
2293
}
2294

2295
static int32_t buildExistTableInStreamRsp(SVnode *pVnode, SSubmitTbData *pSubmitTbData, SVCreateTbRsp *pCreateTbRsp) {
445,639✔
2296
  int32_t code = TSDB_CODE_SUCCESS;
445,639✔
2297
  if (pSubmitTbData->pCreateTbReq->flags & TD_CREATE_NORMAL_TB_IN_STREAM) {
445,639✔
2298
    code = buildExistNormalTalbeRsp(pVnode, pSubmitTbData->uid, &pCreateTbRsp->pMeta);
51,062✔
2299
    if (code) {
51,062✔
2300
      vError("vgId:%d, table uid:%" PRId64 " not exists, line:%d", TD_VID(pVnode), pSubmitTbData->uid, __LINE__);
×
2301
      return code;
×
2302
    }
2303
    pCreateTbRsp->code = TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
51,062✔
2304
    return TSDB_CODE_SUCCESS;
51,062✔
2305
  } else if (pSubmitTbData->pCreateTbReq->flags & TD_CREATE_SUB_TB_IN_STREAM) {
394,577✔
2306
    return buildExistSubTalbeRsp(pVnode, pSubmitTbData, &pCreateTbRsp->pMeta);
78,119✔
2307
  }
2308
  return TSDB_CODE_SUCCESS;
316,458✔
2309
}
2310

2311
static int32_t vnodeHandleAutoCreateTable(SVnode      *pVnode,    // vnode
1,219,801,037✔
2312
                                          int64_t      version,   // version
2313
                                          SSubmitReq2 *pRequest,  // request
2314
                                          SSubmitRsp2 *pResponse  // response
2315
) {
2316
  int32_t code = TSDB_CODE_SUCCESS;
1,219,801,037✔
2317
  int32_t numTbData = taosArrayGetSize(pRequest->aSubmitTbData);
1,219,801,037✔
2318
  SArray *newTbUids = NULL;
1,219,800,442✔
2319

2320
  for (int32_t i = 0; i < numTbData; ++i) {
2,147,483,647✔
2321
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
1,389,477,141✔
2322

2323
    if (pTbData->pCreateTbReq == NULL) {
1,389,477,931✔
2324
      continue;
1,371,772,000✔
2325
    }
2326

2327
    pTbData->uid = pTbData->pCreateTbReq->uid;
17,705,736✔
2328

2329
    // Alloc necessary resources
2330
    if (pResponse->aCreateTbRsp == NULL) {
17,705,736✔
2331
      pResponse->aCreateTbRsp = taosArrayInit(numTbData, sizeof(SVCreateTbRsp));
14,448,973✔
2332
      if (pResponse->aCreateTbRsp == NULL) {
14,448,973✔
2333
        code = terrno;
×
2334
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2335
               tstrerror(code), version);
2336
        taosArrayDestroy(newTbUids);
×
2337
        return code;
×
2338
      }
2339
    }
2340

2341
    // Do create table
2342
    vDebug("vgId:%d start to handle auto create table, version:%" PRId64, TD_VID(pVnode), version);
17,705,676✔
2343

2344
    SVCreateTbRsp *pCreateTbRsp = taosArrayReserve(pResponse->aCreateTbRsp, 1);
17,705,676✔
2345
    pCreateTbRsp->index = i;
17,705,796✔
2346
    code = metaCreateTable2(pVnode->pMeta, version, pTbData->pCreateTbReq, &pCreateTbRsp->pMeta);
17,705,796✔
2347
    if (code == TSDB_CODE_SUCCESS) {
17,705,319✔
2348
      // Allocate necessary resources
2349
      if (newTbUids == NULL) {
17,258,548✔
2350
        newTbUids = taosArrayInit(numTbData, sizeof(int64_t));
14,078,474✔
2351
        if (newTbUids == NULL) {
14,078,474✔
2352
          code = terrno;
×
2353
          vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2354
                 tstrerror(code), version);
2355
          return code;
×
2356
        }
2357
      }
2358

2359
      if (taosArrayPush(newTbUids, &pTbData->uid) == NULL) {
34,516,735✔
2360
        code = terrno;
×
2361
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2362
               tstrerror(code), version);
2363
        taosArrayDestroy(newTbUids);
×
2364
        return code;
×
2365
      }
2366

2367
      if (pCreateTbRsp->pMeta) {
17,258,187✔
2368
        vnodeUpdateMetaRsp(pVnode, pCreateTbRsp->pMeta);
17,258,905✔
2369
      }
2370
    } else if (code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
446,771✔
2371
      code = terrno = 0;
445,579✔
2372
      pTbData->uid = pTbData->pCreateTbReq->uid;  // update uid if table exist for using below
445,639✔
2373

2374
      // stream: get sver from meta, write to pCreateTbRsp, and need to check crateTbReq is same as meta.
2375
      code = buildExistTableInStreamRsp(pVnode, pTbData, pCreateTbRsp);
445,579✔
2376
      if (code) {
445,639✔
2377
        vInfo("vgId:%d failed to create table in stream:%s, code(0x%0x):%s", TD_VID(pVnode),
542✔
2378
              pTbData->pCreateTbReq->name, code, tstrerror(code));
2379
        taosArrayDestroy(newTbUids);
542✔
2380
        return code;
542✔
2381
      }
2382
    } else {
2383
      code = terrno;
1,192✔
2384
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
1,192✔
2385
             tstrerror(code), version);
2386
      taosArrayDestroy(newTbUids);
1,192✔
2387
      return code;
1,192✔
2388
    }
2389
  }
2390

2391
  // Update the affected table uid list
2392
  if (taosArrayGetSize(newTbUids) > 0) {
1,219,798,589✔
2393
    vDebug("vgId:%d, add %d table into query table list in handling submit", TD_VID(pVnode),
14,077,760✔
2394
           (int32_t)taosArrayGetSize(newTbUids));
2395
    if (tqAddTbUidListForQuerySub(pVnode->pTq, newTbUids) != 0) {
14,077,760✔
2396
      vError("vgId:%d, failed to update tbUid list", TD_VID(pVnode));
×
2397
    }
2398
  }
2399

2400
  vDebug("vgId:%d, handle auto create table done, version:%" PRId64, TD_VID(pVnode), version);
1,219,799,048✔
2401

2402
  taosArrayDestroy(newTbUids);
1,219,799,108✔
2403
  return code;
1,219,800,107✔
2404
}
2405

2406
static void addExistTableInfoIntoRes(SVnode *pVnode, SSubmitReq2 *pRequest, SSubmitRsp2 *pResponse,
542✔
2407
                                     SSubmitTbData *pTbData, int32_t index) {
2408
  int32_t code = 0;
542✔
2409
  int32_t lino = 0;
542✔
2410
  if ((pTbData->flags & SUBMIT_REQ_SCHEMA_RES) == 0) {
542✔
2411
    return;
×
2412
  }
2413
  if (!pResponse->aCreateTbRsp) {
542✔
2414
    pResponse->aCreateTbRsp = taosArrayInit(index, sizeof(SVCreateTbRsp));
×
2415
    if (pResponse->aCreateTbRsp == NULL) {
×
2416
      code = terrno;
×
2417
      TSDB_CHECK_CODE(code, lino, _exit);
×
2418
    }
2419
  }
2420
  SVCreateTbRsp *pCreateTbRsp = taosArrayReserve(pResponse->aCreateTbRsp, 1);
542✔
2421
  pCreateTbRsp->index = index;
542✔
2422
  if (pCreateTbRsp == NULL) {
542✔
2423
    code = terrno;
×
2424
    TSDB_CHECK_CODE(code, lino, _exit);
×
2425
  }
2426
  if (pTbData->suid == 0) {
542✔
2427
    code = buildExistNormalTalbeRsp(pVnode, pTbData->uid, &pCreateTbRsp->pMeta);
×
2428
    if (code) {
×
2429
      vError("vgId:%d, table uid:%" PRId64 " not exists, line:%d", TD_VID(pVnode), pTbData->uid, __LINE__);
×
2430
    }
2431
  } else {
2432
    code = buildExistSubTalbeRsp(pVnode, pTbData, &pCreateTbRsp->pMeta);
542✔
2433
  }
2434

2435
  TSDB_CHECK_CODE(code, lino, _exit);
542✔
2436
_exit:
×
2437
  if (code != TSDB_CODE_SUCCESS) {
542✔
2438
    vError("vgId:%d, failed to add exist table info into response, code:0x%0x, line:%d", TD_VID(pVnode), code, lino);
542✔
2439
  }
2440
  return;
542✔
2441
}
2442

2443
static int32_t vnodeHandleDataWrite(SVnode *pVnode, int64_t version, SSubmitReq2 *pRequest, SSubmitRsp2 *pResponse) {
1,219,798,942✔
2444
  int32_t code = TSDB_CODE_SUCCESS;
1,219,798,942✔
2445
  int32_t numTbData = taosArrayGetSize(pRequest->aSubmitTbData);
1,219,798,942✔
2446
  int8_t  hasBlob = 0;
1,219,798,687✔
2447

2448
  // Scan submit data
2449
  for (int32_t i = 0; i < numTbData; ++i) {
2,147,483,647✔
2450
    SMetaInfo      info = {0};
1,389,474,506✔
2451
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
1,389,475,522✔
2452

2453
    if (pTbData->flags & SUBMIT_REQ_WITH_BLOB) {
1,389,477,098✔
2454
      hasBlob = 1;
50,681✔
2455
    }
2456
    if (pTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
1,389,477,098✔
2457
      continue;  // skip column data format
7,415,677✔
2458
    }
2459
    if (pTbData->flags & SUBMIT_REQ_ONLY_CREATE_TABLE) {
1,382,060,558✔
2460
      continue;  // skip only crate table request
2,929,608✔
2461
    }
2462

2463
    code = metaGetInfo(pVnode->pMeta, pTbData->uid, &info, NULL);
1,379,130,255✔
2464
    if (code) {
1,379,129,770✔
2465
      code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
2,204✔
2466
      vWarn("vgId:%d, error occurred at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __FILE__,
2,204✔
2467
            __LINE__, tstrerror(code), version, pTbData->uid);
2468
      return code;
2,204✔
2469
    }
2470

2471
    if (info.suid != pTbData->suid) {
1,379,127,566✔
2472
      code = TSDB_CODE_INVALID_MSG;
×
2473
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64 " suid:%" PRId64
×
2474
             " info.suid:%" PRId64,
2475
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), version, pTbData->uid, pTbData->suid,
2476
             info.suid);
2477
      return code;
×
2478
    }
2479

2480
    if (info.suid) {
1,379,125,158✔
2481
      code = metaGetInfo(pVnode->pMeta, info.suid, &info, NULL);
1,212,945,569✔
2482
      if (code) {
1,212,947,866✔
2483
        code = TSDB_CODE_INTERNAL_ERROR;
×
2484
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " suid:%" PRId64, TD_VID(pVnode), __func__,
×
2485
               __FILE__, __LINE__, tstrerror(code), version, info.suid);
2486
        return code;
×
2487
      }
2488
    }
2489

2490
    if (pTbData->sver != info.skmVer) {
1,379,127,455✔
2491
      code = TSDB_CODE_TDB_INVALID_TABLE_SCHEMA_VER;
542✔
2492
      addExistTableInfoIntoRes(pVnode, pRequest, pResponse, pTbData, i);
542✔
2493
      vDebug("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64
542✔
2494
             " sver:%d"
2495
             " info.skmVer:%d",
2496
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), version, pTbData->uid, pTbData->sver,
2497
             info.skmVer);
2498
      return code;
542✔
2499
    }
2500
  }
2501

2502
  // Do write data
2503
  vDebug("vgId:%d start to handle data write, version:%" PRId64, TD_VID(pVnode), version);
1,219,796,061✔
2504

2505
  for (int32_t i = 0; i < numTbData; ++i) {
2,147,483,647✔
2506
    int32_t        affectedRows = 0;
1,389,470,108✔
2507
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
1,389,470,129✔
2508

2509
    if (pTbData->flags & SUBMIT_REQ_ONLY_CREATE_TABLE) {
1,389,472,546✔
2510
      continue;
2,929,608✔
2511
    }
2512

2513
    if (hasBlob) {
1,386,542,075✔
2514
      code = vnodeSubmitBlobData(pVnode, pTbData);
50,681✔
2515
      if (code) {
50,681✔
2516
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2517
               __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2518
        return code;
×
2519
      }
2520
    }
2521

2522
    code = tsdbInsertTableData(pVnode->pTsdb, version, pTbData, &affectedRows);
1,386,542,075✔
2523
    if (code) {
1,386,495,961✔
2524
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2525
             __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2526
      return code;
×
2527
    }
2528

2529
    code = metaUpdateChangeTimeWithLock(pVnode->pMeta, pTbData->uid, pTbData->ctimeMs);
1,386,495,961✔
2530
    if (code) {
1,386,483,640✔
2531
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2532
             __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2533
      return code;
×
2534
    }
2535
    pResponse->affectedRows += affectedRows;
1,386,483,640✔
2536
  }
2537

2538
  vDebug("vgId:%d, handle data write done, version:%" PRId64 ", affectedRows:%d", TD_VID(pVnode), version,
1,219,765,317✔
2539
         pResponse->affectedRows);
2540
  return code;
1,219,797,556✔
2541
}
2542

2543
static int32_t vnodeScanColumnData(SVnode *pVnode, SSubmitTbData *pTbData, TSKEY minKey, TSKEY maxKey) {
7,413,918✔
2544
  int32_t code = 0;
7,413,918✔
2545

2546
  int32_t   numCols = taosArrayGetSize(pTbData->aCol);
7,413,918✔
2547
  SColData *aColData = (SColData *)TARRAY_DATA(pTbData->aCol);
7,414,265✔
2548

2549
  if (numCols <= 0) {
7,412,779✔
2550
    code = TSDB_CODE_INVALID_MSG;
×
2551
    vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " numCols:%d", TD_VID(pVnode), __func__,
×
2552
           __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, numCols);
2553
    return code;
×
2554
  }
2555

2556
  if (aColData[0].cid != PRIMARYKEY_TIMESTAMP_COL_ID || aColData[0].type != TSDB_DATA_TYPE_TIMESTAMP ||
7,412,779✔
2557
      aColData[0].nVal <= 0) {
7,414,548✔
UNCOV
2558
    code = TSDB_CODE_INVALID_MSG;
×
UNCOV
2559
    vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64
×
2560
           " first column is not primary key timestamp, cid:%d type:%d nVal:%d",
2561
           TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, aColData[0].cid,
2562
           aColData[0].type, aColData[0].nVal);
2563
    return code;
×
2564
  }
2565

2566
  for (int32_t i = 1; i < numCols; ++i) {
22,952,628✔
2567
    if (aColData[i].nVal != aColData[0].nVal) {
15,540,483✔
2568
      code = TSDB_CODE_INVALID_MSG;
×
2569
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64
×
2570
             " column cid:%d type:%d nVal:%d is not equal to primary key timestamp nVal:%d",
2571
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid,
2572
             aColData[i].cid, aColData[i].type, aColData[i].nVal, aColData[0].nVal);
2573
      return code;
×
2574
    }
2575
  }
2576

2577
  SRowKey *pLastKey = NULL;
7,412,145✔
2578
  SRowKey  lastKey = {0};
7,412,145✔
2579
  for (int32_t i = 0; i < aColData[0].nVal; ++i) {
2,147,483,647✔
2580
    SRowKey key = {0};
2,147,483,647✔
2581

2582
    tColDataArrGetRowKey(aColData, numCols, i, &key);
2,147,483,647✔
2583

2584
    if (key.ts < minKey || key.ts > maxKey) {
2,147,483,647✔
2585
      code = TSDB_CODE_INVALID_MSG;
×
2586
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] key:%" PRId64
×
2587
             " is out of range [%" PRId64 ", %" PRId64 "]",
2588
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, i, key.ts,
2589
             minKey, maxKey);
2590
      return code;
×
2591
    }
2592

2593
    if (pLastKey && tRowKeyCompare(pLastKey, &key) >= 0) {
2,147,483,647✔
2594
      code = TSDB_CODE_INVALID_MSG;
×
2595
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] key:%" PRId64
×
2596
             " is not in order, lastKey:%" PRId64,
2597
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, i, key.ts,
2598
             pLastKey->ts);
2599
      return code;
×
2600
    } else if (pLastKey == NULL) {
2,147,483,647✔
2601
      pLastKey = &lastKey;
7,411,764✔
2602
    }
2603

2604
    *pLastKey = key;
2,147,483,647✔
2605
  }
2606

2607
  return code;
7,415,677✔
2608
}
2609

2610
static int32_t vnodeScanSubmitRowData(SVnode *pVnode, SSubmitTbData *pTbData, TSKEY minKey, TSKEY maxKey) {
1,382,038,509✔
2611
  int32_t code = 0;
1,382,038,509✔
2612

2613
  int32_t numRows = taosArrayGetSize(pTbData->aRowP);
1,382,038,509✔
2614
  SRow  **aRow = (SRow **)TARRAY_DATA(pTbData->aRowP);
1,382,017,213✔
2615

2616
  if (numRows <= 0 && (pTbData->flags & SUBMIT_REQ_ONLY_CREATE_TABLE) == 0) {
1,382,009,444✔
2617
    code = TSDB_CODE_INVALID_MSG;
×
2618
    vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " numRows:%d", TD_VID(pVnode), __func__,
×
2619
           __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, numRows);
2620
    return code;
×
2621
  }
2622

2623
  SRowKey *pLastKey = NULL;
1,382,009,444✔
2624
  SRowKey  lastKey = {0};
1,382,009,444✔
2625
  for (int32_t i = 0; i < numRows; ++i) {
2,147,483,647✔
2626
    SRow *pRow = aRow[i];
2,147,483,647✔
2627
    if (pRow->sver != pTbData->sver) {
2,147,483,647✔
2628
      code = TSDB_CODE_INVALID_MSG;
×
2629
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] sver:%d pTbData->sver:%d",
×
2630
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, i, pRow->sver,
2631
             pTbData->sver);
2632
      return code;
×
2633
    }
2634

2635
    SRowKey key = {0};
2,147,483,647✔
2636
    tRowGetKey(pRow, &key);
2,147,483,647✔
2637
    if (key.ts < minKey || key.ts > maxKey) {
2,147,483,647✔
2638
      code = TSDB_CODE_INVALID_MSG;
11,224,086✔
2639
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] key:%" PRId64
11,224,086✔
2640
             " is out of range [%" PRId64 ", %" PRId64 "]",
2641
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, i, key.ts,
2642
             minKey, maxKey);
2643
      return code;
×
2644
    }
2645

2646
    if (pLastKey && tRowKeyCompare(pLastKey, &key) >= 0) {
2,147,483,647✔
2647
      code = TSDB_CODE_INVALID_MSG;
×
2648
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] key:%" PRId64
×
2649
             " is not in order, lastKey:%" PRId64,
2650
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, i, key.ts,
2651
             pLastKey->ts);
2652
      return code;
×
2653
    } else if (pLastKey == NULL) {
2,147,483,647✔
2654
      pLastKey = &lastKey;
1,379,070,532✔
2655
    }
2656

2657
    *pLastKey = key;
2,147,483,647✔
2658
  }
2659

2660
  return code;
1,375,413,094✔
2661
}
2662

2663
static int32_t vnodeScanSubmitReq(SVnode *pVnode, int64_t version, SSubmitReq2 *pRequest, SSubmitRsp2 *pResponse) {
1,219,775,637✔
2664
  int32_t code = TSDB_CODE_SUCCESS;
1,219,775,637✔
2665
  int32_t numTbData = taosArrayGetSize(pRequest->aSubmitTbData);
1,219,775,637✔
2666

2667
  TSKEY now = taosGetTimestamp(pVnode->config.tsdbCfg.precision);
1,219,774,273✔
2668
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * pVnode->config.tsdbCfg.keep2;
1,219,768,509✔
2669
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
1,219,780,015✔
2670
  for (int32_t i = 0; i < numTbData; i++) {
2,147,483,647✔
2671
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
1,389,450,386✔
2672

2673
    if (pTbData->pCreateTbReq && pTbData->pCreateTbReq->uid == 0) {
1,389,466,400✔
2674
      code = TSDB_CODE_INVALID_MSG;
×
2675
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2676
             tstrerror(code), version);
2677
      return code;
×
2678
    }
2679

2680
    if (pTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
1,389,453,023✔
2681
      code = vnodeScanColumnData(pVnode, pTbData, minKey, maxKey);
7,414,622✔
2682
      if (code) {
7,414,963✔
2683
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2684
               __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2685
        return code;
×
2686
      }
2687
    } else {
2688
      code = vnodeScanSubmitRowData(pVnode, pTbData, minKey, maxKey);
1,382,040,269✔
2689
      if (code) {
1,382,037,785✔
2690
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
2,266✔
2691
               __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2692
        return code;
×
2693
      }
2694
    }
2695
  }
2696

2697
  return code;
1,219,774,457✔
2698
}
2699

2700
static int32_t vnodeDecodeAuditRecord(const SJson *pJson, void *pObj) {
1,804✔
2701
  SAuditRecord *record = (SAuditRecord *)pObj;
1,804✔
2702
  int32_t       code = 0;
1,804✔
2703

2704
  tjsonGetNumberValue(pJson, "timestamp", record->curTime, code);
1,804✔
2705
  if (code < 0) return TSDB_CODE_INVALID_JSON_FORMAT;
1,804✔
2706
  code = tjsonGetStringValue2(pJson, "cluster_id", record->strClusterId, TSDB_CLUSTER_ID_LEN);
1,804✔
2707
  if (code < 0) return TSDB_CODE_INVALID_JSON_FORMAT;
1,804✔
2708
  code = tjsonGetStringValue2(pJson, "client_add", record->clientAddress, AUDIT_CLIENT_ADD_LEN);
1,804✔
2709
  if (code < 0) return TSDB_CODE_INVALID_JSON_FORMAT;
1,804✔
2710
  code = tjsonGetStringValue2(pJson, "user", record->user, TSDB_USER_LEN);
1,804✔
2711
  if (code < 0) return TSDB_CODE_INVALID_JSON_FORMAT;
1,804✔
2712
  code = tjsonGetStringValue2(pJson, "operation", record->operation, AUDIT_OPERATION_LEN);
1,804✔
2713
  if (code < 0) return TSDB_CODE_INVALID_JSON_FORMAT;
1,804✔
2714
  code = tjsonGetStringValue2(pJson, "db", record->target1, TSDB_DB_NAME_LEN);
1,804✔
2715
  if (code < 0) return TSDB_CODE_INVALID_JSON_FORMAT;
1,804✔
2716
  code = tjsonGetStringValue2(pJson, "resource", record->target2, TSDB_STREAM_NAME_LEN);
1,804✔
2717
  if (code < 0) return TSDB_CODE_INVALID_JSON_FORMAT;
1,804✔
2718
  record->detail = (char *)tjsonGetStringPointer(pJson, "details");
1,804✔
2719
  if (record->detail == NULL) return TSDB_CODE_INVALID_JSON_FORMAT;
1,804✔
2720
  code = tjsonGetDoubleValue(pJson, "duration", &record->duration);
1,804✔
2721
  if (code < 0) return TSDB_CODE_INVALID_JSON_FORMAT;
1,804✔
2722
  tjsonGetNumberValue(pJson, "affected_rows", record->affectedRows, code);
1,804✔
2723
  if (code < 0) return TSDB_CODE_INVALID_JSON_FORMAT;
1,804✔
2724

2725
  return 0;
1,804✔
2726
}
2727

2728
static int32_t vnodeBuildCreateTbReq(SVCreateTbReq *pTbReq, const char *tname, STag *pTag, int64_t suid,
164✔
2729
                                     const char *sname, SArray *tagName, uint8_t tagNum, int32_t ttl) {
2730
  int32_t code = TSDB_CODE_SUCCESS;
164✔
2731
  int32_t lino = 0;
164✔
2732

2733
  if (tname == NULL || sname == NULL) {
164✔
2734
    return TSDB_CODE_INVALID_PARA;
×
2735
  }
2736

2737
  pTbReq->name = taosStrdup(tname);
164✔
2738
  if (!pTbReq->name) {
164✔
2739
    code = terrno;
×
2740
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
2741
  }
2742

2743
  pTbReq->ctb.stbName = taosStrdup(sname);
164✔
2744
  if (!pTbReq->ctb.stbName) {
164✔
2745
    code = terrno;
×
2746
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
2747
  }
2748

2749
  pTbReq->type = TD_CHILD_TABLE;
164✔
2750
  pTbReq->ctb.suid = suid;
164✔
2751
  pTbReq->ctb.tagNum = tagNum;
164✔
2752
  pTbReq->ttl = ttl;
164✔
2753
  pTbReq->commentLen = -1;
164✔
2754
  pTbReq->ctb.tagName = tagName;
164✔
2755
  pTbReq->ctb.pTag = (uint8_t *)pTag;
164✔
2756

2757
  return code;
164✔
2758
_exit:
×
2759
  if (code != 0) {
×
2760
    if (pTbReq->name != NULL) {
×
2761
      taosMemoryFreeClear(pTbReq->name);
×
2762
    }
2763
    if (pTbReq->ctb.stbName != NULL) {
×
2764
      taosMemoryFreeClear(pTbReq->ctb.stbName);
×
2765
    }
2766
    vError("failed to build create tb at %d since %s", lino, tstrerror(code));
×
2767
  }
2768
  return code;
×
2769
}
2770

2771
static int32_t vnodePrepareCreateTb(SVCreateTbReq *pTbReq, char *tbName, int64_t suid, SSchemaWrapper *pTagSchema,
164✔
2772
                                    SAuditRecord *record) {
2773
  int32_t code = 0;
164✔
2774
  int32_t lino = 0;
164✔
2775

2776
  SArray *TagNames = NULL;
164✔
2777
  SArray *pTagVals = NULL;
164✔
2778
  STag   *pTag = NULL;
164✔
2779

2780
  SSchema *tSchema = &pTagSchema->pSchema[0];
164✔
2781
  vTrace("schema name:%s", tSchema->name);
164✔
2782

2783
  TagNames = taosArrayInit(1, TSDB_COL_NAME_LEN);
164✔
2784
  if (!TagNames) {
164✔
2785
    code = terrno;
×
2786
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
2787
  }
2788

2789
  if (NULL == taosArrayPush(TagNames, tSchema->name)) {
328✔
2790
    code = terrno;
×
2791
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
2792
  }
2793

2794
  STagVal tv = (STagVal){.cid = tSchema->colId,
164✔
2795
                         .type = tSchema->type,
164✔
2796
                         .nData = strlen(record->strClusterId),
164✔
2797
                         .pData = record->strClusterId};  // address copy, no value
164✔
2798
  if ((pTagVals = taosArrayInit(1, sizeof(STagVal))) == NULL) {
164✔
2799
    code = terrno;
×
2800
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
2801
  }
2802
  if (NULL == taosArrayPush(pTagVals, &tv)) {
164✔
2803
    code = terrno;
×
2804
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
2805
  }
2806

2807
  // version hardcode to 1 according to other module
2808
  code = tTagNew(pTagVals, 1, false, &pTag);
164✔
2809
  if (code != TSDB_CODE_SUCCESS) {
164✔
2810
    vError("failed to create tag, error:%s", tstrerror(code));
×
2811
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
2812
  }
2813

2814
  code = vnodeBuildCreateTbReq(pTbReq, tbName, pTag, suid, AUDIT_STABLE_NAME, TagNames, pTagSchema->nCols,
164✔
2815
                               TSDB_DEFAULT_TABLE_TTL);
2816
_exit:
164✔
2817
  if (code != 0) {
164✔
2818
    vError("failed to prepare create tb at %d since %s", lino, tstrerror(code));
×
2819
    if (TagNames != NULL) {
×
2820
      taosArrayDestroy(TagNames);
×
2821
      TagNames = NULL;
×
2822
      pTbReq->ctb.tagName = NULL;
×
2823
    }
2824
    if (pTag != NULL) {
×
2825
      tTagFree(pTag);
×
2826
      pTag = NULL;
×
2827
      pTbReq->ctb.pTag = NULL;
×
2828
    }
2829
  }
2830

2831
  if (pTagVals != NULL) {
164✔
2832
    taosArrayDestroy(pTagVals);
164✔
2833
    pTagVals = NULL;
164✔
2834
  }
2835

2836
  return code;
164✔
2837
}
2838

2839
static SArray *vnodePrepareRow(SVnode *pVnode, STSchema *pSchema, SAuditRecord *record) {
1,804✔
2840
  int32_t code = 0;
1,804✔
2841
  int32_t lino = 0;
1,804✔
2842

2843
  SArray *aRows = NULL;
1,804✔
2844
  SArray *pVals = NULL;
1,804✔
2845
  SRow   *pRow = NULL;
1,804✔
2846

2847
  if (!(aRows = taosArrayInit(1, sizeof(SRow *)))) {
1,804✔
2848
    code = terrno;
×
2849
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
2850
  }
2851

2852
  if ((pVals = taosArrayInit(1, sizeof(SColVal))) == NULL) {
1,804✔
2853
    code = terrno;
×
2854
    goto _exit;
×
2855
  }
2856

2857
  for (int32_t k = 0; k < pSchema->numOfCols; ++k) {
18,040✔
2858
    const STColumn *pCol = &pSchema->columns[k];
16,236✔
2859
    vTrace("vgId:%d, schema column id:%d, type:%d", TD_VID(pVnode), pCol->colId, pCol->type);
16,236✔
2860

2861
    // colId is consistent with audit_columns in mndStb.c
2862
    void *data = NULL;
16,236✔
2863
    if (pCol->colId == 1) {
16,236✔
2864
      data = &record->curTime;
1,804✔
2865
    } else if (pCol->colId == 2) {
14,432✔
2866
      data = record->detail;
1,804✔
2867
    } else if (pCol->colId == 3) {
12,628✔
2868
      data = record->user;
1,804✔
2869
    } else if (pCol->colId == 4) {
10,824✔
2870
      data = record->operation;
1,804✔
2871
    } else if (pCol->colId == 5) {
9,020✔
2872
      data = record->target1;
1,804✔
2873
    } else if (pCol->colId == 6) {
7,216✔
2874
      data = record->target2;
1,804✔
2875
    } else if (pCol->colId == 7) {
5,412✔
2876
      data = record->clientAddress;
1,804✔
2877
    } else if (pCol->colId == 8) {
3,608✔
2878
      data = &record->duration;
1,804✔
2879
    } else if (pCol->colId == 9) {
1,804✔
2880
      data = &record->affectedRows;
1,804✔
2881
    } else {
2882
      vError("the column id %" PRIi16 " is not defined in audit record table", pCol->colId);
×
2883
      code = TSDB_CODE_APP_ERROR;
×
2884
      TAOS_CHECK_GOTO(code, &lino, _exit);
×
2885
    }
2886

2887
    switch (pCol->type) {
16,236✔
2888
      case TSDB_DATA_TYPE_NCHAR:
10,824✔
2889
      case TSDB_DATA_TYPE_VARBINARY:
2890
      case TSDB_DATA_TYPE_VARCHAR: {
2891
        // if (colDataIsNull_s(pColInfoData, j)) {
2892
        //   SColVal cv = COL_VAL_NULL(pCol->colId, pCol->type);
2893
        //   if (NULL == taosArrayPush(pVals, &cv)) {
2894
        //     goto _end;
2895
        //   }
2896
        // } else {
2897
        char   *str = data;
10,824✔
2898
        int32_t len = strlen(str);
10,824✔
2899
        vTrace("set svalue id:%d %s, len:%d, bytes:%d", pCol->colId, str, len, pCol->bytes);
10,824✔
2900
        if (len > pCol->bytes - VARSTR_HEADER_SIZE) {
10,824✔
2901
          vWarn("vgId:%d, audit record string field with colId %d is too long, truncated from %d to %d bytes",
×
2902
                TD_VID(pVnode), pCol->colId, len, (int32_t)(pCol->bytes - VARSTR_HEADER_SIZE));
2903
          len = pCol->bytes - VARSTR_HEADER_SIZE;
×
2904
        }
2905
        SValue sv = {0};
10,824✔
2906
        sv.type = pCol->type;
10,824✔
2907
        sv.nData = len;
10,824✔
2908
        sv.pData = str;
10,824✔
2909
        SColVal cv = COL_VAL_VALUE(pCol->colId, sv);
10,824✔
2910
        if (NULL == taosArrayPush(pVals, &cv)) {
10,824✔
2911
          code = terrno;
×
2912
          TAOS_CHECK_GOTO(code, &lino, _exit);
×
2913
        }
2914
        //}
2915
        break;
10,824✔
2916
      }
2917
      case TSDB_DATA_TYPE_BLOB:
×
2918
      case TSDB_DATA_TYPE_MEDIUMBLOB:
2919
      case TSDB_DATA_TYPE_JSON: {
2920
        vError("the column type %" PRIi16 " is defined but not implemented yet", pCol->type);
×
2921
        code = TSDB_CODE_APP_ERROR;
×
2922
        TAOS_CHECK_GOTO(code, &lino, _exit);
×
2923
        break;
×
2924
      }
2925
      default: {
5,412✔
2926
        /*
2927
          if (colDataIsNull_s(pColInfoData, j)) {
2928
            if (PRIMARYKEY_TIMESTAMP_COL_ID == pCol->colId) {
2929
              qError("Primary timestamp column should not be null");
2930
              terrno = TSDB_CODE_PAR_INCORRECT_TIMESTAMP_VAL;
2931
              goto _end;
2932
            }
2933

2934
            SColVal cv = COL_VAL_NULL(pCol->colId, pCol->type);  // should use pCol->type
2935
            if (NULL == taosArrayPush(pVals, &cv)) {
2936
              goto _end;
2937
            }
2938
          } else {
2939
           */
2940
        SValue sv = {0};
5,412✔
2941
        sv.type = pCol->type;
5,412✔
2942
        valueSetDatum(&sv, sv.type, data, pCol->bytes);
5,412✔
2943
        SColVal cv = COL_VAL_VALUE(pCol->colId, sv);
5,412✔
2944
        if (NULL == taosArrayPush(pVals, &cv)) {
5,412✔
2945
          code = terrno;
×
2946
          TAOS_CHECK_GOTO(code, &lino, _exit);
×
2947
        }
2948
        //}
2949
        break;
5,412✔
2950
      }
2951
    }
2952
  }
2953

2954
  SRowBuildScanInfo sinfo = {0};
1,804✔
2955
  if ((code = tRowBuild(pVals, pSchema, &pRow, &sinfo)) < 0) {
1,804✔
2956
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
2957
  }
2958
  if (NULL == taosArrayPush(aRows, &pRow)) {
1,804✔
2959
    code = terrno;
×
2960
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
2961
  }
2962
  taosArrayDestroy(pVals);
1,804✔
2963
  pVals = NULL;
1,804✔
2964

2965
_exit:
1,804✔
2966
  if (code != 0) {
1,804✔
2967
    vError("vgId:%d, failed to prepare row at:%d since %s", TD_VID(pVnode), lino, tstrerror(code));
×
2968
    taosArrayDestroy(pVals);
×
2969
    tRowDestroy(pRow);
×
2970
    taosArrayDestroy(aRows);
×
2971
    terrno = code;
×
2972
    aRows = NULL;
×
2973
  }
2974
  return aRows;
1,804✔
2975
}
2976

2977
static SArray *vnodePrepareSubmitTb(SVnode *pVnode, SAuditRecord *record, STSchema *pSchema, SSchemaWrapper *pTagSchema,
1,804✔
2978
                                    int64_t suid) {
2979
  int32_t code = 0;
1,804✔
2980
  int32_t lino = 0;
1,804✔
2981

2982
  SArray *aSubmitTbData = NULL;
1,804✔
2983
  SSubmitTbData tbData = {0};
1,804✔
2984

2985
  if (!(aSubmitTbData = taosArrayInit(1, sizeof(SSubmitTbData)))) {
1,804✔
2986
    code = terrno;
×
2987
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
2988
  }
2989

2990
  char tbName[TSDB_TABLE_NAME_LEN] = {0};
1,804✔
2991
  (void)tsnprintf(tbName, TSDB_TABLE_NAME_LEN, "t_operations_%s", record->strClusterId);
1,804✔
2992

2993
  SMetaReader merTb = {0};
1,804✔
2994
  metaReaderDoInit(&merTb, pVnode->pMeta, META_READER_LOCK);
1,804✔
2995
  if ((code = metaGetTableEntryByName(&merTb, tbName)) == 0) {
1,804✔
2996
    vTrace("vgId:%d, get table entry, table:%s uid:%" PRId64 ", suid:% " PRId64 ", version:%" PRId64 ", api:%p",
1,640✔
2997
           TD_VID(pVnode), tbName, merTb.me.uid, merTb.me.ctbEntry.suid, merTb.me.version, merTb.pAPI);
2998
    tbData.uid = merTb.me.uid;
1,640✔
2999
    metaReaderClear(&merTb);
1,640✔
3000
  } else if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST) {
164✔
3001
    vTrace("vgId:%d, %s table not exist", TD_VID(pVnode), tbName);
164✔
3002

3003
    tbData.uid = tGenIdPI64();
164✔
3004
    tbData.flags |= SUBMIT_REQ_AUTO_CREATE_TABLE;
164✔
3005

3006
    tbData.pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
164✔
3007

3008
    if (tbData.pCreateTbReq == NULL) {
164✔
3009
      code = terrno;
×
3010
      TAOS_CHECK_GOTO(code, &lino, _exit);
×
3011
    }
3012

3013
    tbData.pCreateTbReq->uid = tbData.uid;
164✔
3014
    tbData.pCreateTbReq->btime = taosGetTimestampMs();
328✔
3015

3016
    code = vnodePrepareCreateTb(tbData.pCreateTbReq, tbName, suid, pTagSchema, record);
164✔
3017

3018
    metaReaderClear(&merTb);
164✔
3019
    TAOS_CHECK_GOTO(code, &lino, _exit);
164✔
3020
  } else {
3021
    metaReaderClear(&merTb);
×
3022
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
3023
  }
3024

3025
  tbData.suid = suid;
1,804✔
3026
  tbData.sver = pSchema->version;
1,804✔
3027

3028
  tbData.aRowP = vnodePrepareRow(pVnode, pSchema, record);
1,804✔
3029
  if (tbData.aRowP == NULL) {
1,804✔
3030
    code = terrno;
×
3031
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
3032
  }
3033

3034
  if (NULL == taosArrayPush(aSubmitTbData, &tbData)) {
1,804✔
3035
    code = terrno;
×
3036
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
3037
  }
3038

3039
_exit:
1,804✔
3040
  if (code != 0) {
1,804✔
3041
    vError("vgId:%d, failed to prepare submitTb at:%d since %s", TD_VID(pVnode), lino, tstrerror(code));
×
3042
    tDestroySubmitTbData(&tbData, TSDB_MSG_FLG_ENCODE);
×
3043
    if (aSubmitTbData != NULL) {
×
3044
      taosArrayDestroy(aSubmitTbData);
×
3045
    }
3046
    terrno = code;
×
3047
    aSubmitTbData = NULL;
×
3048
  }
3049
  return aSubmitTbData;
1,804✔
3050
}
3051

3052
static int32_t vnodeSaveOneAuditRecord(SVnode *pVnode, int64_t ver, SJson *pJson, SSchemaWrapper *pTagSchema,
1,804✔
3053
                                       int64_t suid, STSchema *pSchema, SRpcMsg *pOriginalMsg) {
3054
  int32_t code = 0;
1,804✔
3055
  int32_t lino = 0;
1,804✔
3056
  terrno = 0;
1,804✔
3057

3058
  SAuditRecord record = {0};
1,804✔
3059
  SSubmitReq2 *pSubmitReq = NULL;
1,804✔
3060
  SSubmitRsp2 *pSubmitRsp = &(SSubmitRsp2){0};
1,804✔
3061

3062
  TAOS_CHECK_GOTO(vnodeDecodeAuditRecord(pJson, &record), &lino, _exit);
1,804✔
3063

3064
  vTrace("vgId:%d, start to audit operation:%s", TD_VID(pVnode), record.operation);
1,804✔
3065

3066
  if (!(pSubmitReq = taosMemoryCalloc(1, sizeof(SSubmitReq2)))) {
1,804✔
3067
    code = terrno;
×
3068
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
3069
  }
3070

3071
  pSubmitReq->aSubmitTbData = vnodePrepareSubmitTb(pVnode, &record, pSchema, pTagSchema, suid);
1,804✔
3072
  if (pSubmitReq->aSubmitTbData == NULL) {
1,804✔
3073
    code = terrno;
×
3074
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
3075
  }
3076

3077
  vTrace("vgId:%d, going to create table", TD_VID(pVnode));
1,804✔
3078
  code = vnodeHandleAutoCreateTable(pVnode, ver, pSubmitReq, pSubmitRsp);
1,804✔
3079
  if (code) {
1,804✔
3080
    vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
3081
           tstrerror(code), ver);
3082
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
3083
  }
3084

3085
  vTrace("vgId:%d, going to data write", TD_VID(pVnode));
1,804✔
3086
  code = vnodeHandleDataWrite(pVnode, ver, pSubmitReq, pSubmitRsp);
1,804✔
3087
  if (code) {
1,804✔
3088
    vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
3089
           tstrerror(code), ver);
3090
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
3091
  }
3092

3093
  vTrace("vgId:%d, affectedRow:%d", TD_VID(pVnode), pSubmitRsp->affectedRows);
1,804✔
3094

3095
_exit:
1,804✔
3096
  if (code != 0)
1,804✔
3097
    vError("vgId:%d, failed to save one AuditRecord at line:%d, since %s", TD_VID(pVnode), lino, tstrerror(code));
×
3098

3099
  // update statistics
3100
  (void)atomic_add_fetch_64(&pVnode->statis.nInsert, pSubmitRsp->affectedRows);
1,804✔
3101
  (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsert, 1);
1,804✔
3102
  if (code == 0) {
1,804✔
3103
    (void)atomic_add_fetch_64(&pVnode->statis.nInsertSuccess, pSubmitRsp->affectedRows);
1,804✔
3104
    (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
1,804✔
3105
  }
3106

3107
  // update metrics
3108
  METRICS_UPDATE(pVnode->writeMetrics.total_requests, METRIC_LEVEL_LOW, 1);
1,804✔
3109
  METRICS_UPDATE(pVnode->writeMetrics.total_rows, METRIC_LEVEL_HIGH, pSubmitRsp->affectedRows);
1,804✔
3110
  // METRICS_UPDATE(pVnode->writeMetrics.total_bytes, METRIC_LEVEL_LOW, pMsg->header.contLen);
3111

3112
  if (code == 0 && tsEnableMonitor && tsMonitorFqdn[0] != 0 && tsMonitorPort != 0 && pSubmitRsp->affectedRows > 0 &&
1,804✔
3113
      strlen(RPC_MSG_USER(pOriginalMsg)) > 0 && tsInsertCounter != NULL) {
×
3114
    const char *sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT_AFFECTED_ROWS,
×
3115
                                   pVnode->monitor.strClusterId,
×
3116
                                   pVnode->monitor.strDnodeId,
×
3117
                                   tsLocalEp,
3118
                                   pVnode->monitor.strVgId,
×
3119
                                   RPC_MSG_USER(pOriginalMsg),
×
3120
                                   "Success"};
3121
    int         tv = taos_counter_add(tsInsertCounter, pSubmitRsp->affectedRows, sample_labels);
×
3122
    if (tv != 0) vError("vgId:%d, failed to taos counter add since return is %d", TD_VID(pVnode), tv);
×
3123
  }
3124

3125
  // clear
3126
  if (pSubmitReq != NULL) {
1,804✔
3127
    tDestroySubmitReq(pSubmitReq, TSDB_MSG_FLG_ENCODE);
1,804✔
3128
    taosMemoryFree(pSubmitReq);
1,804✔
3129
  }
3130
  if (pSubmitRsp != NULL) {
1,804✔
3131
    tDestroySSubmitRsp2(pSubmitRsp, TSDB_MSG_FLG_ENCODE);
1,804✔
3132
  }
3133

3134
  return code;
1,804✔
3135
}
3136

3137
static int32_t vnodeProcessAuditRecordReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pOriginalMsg) {
656✔
3138
  int32_t code = 0;
656✔
3139
  int32_t lino = 0;
656✔
3140
  terrno = 0;
656✔
3141

3142
  SVAuditRecordReq req = {0};
656✔
3143
  if (tDeserializeSVAuditRecordReq(pReq, len, &req) != 0) {
656✔
3144
    vError("vgId:%d, failed to deserialize SVAuditRecordReq", TD_VID(pVnode));
×
3145
    code = TSDB_CODE_INVALID_MSG;
×
3146
    return code;
×
3147
  }
3148

3149
  vTrace("vgId:%d, start to process AuditRecord Req", TD_VID(pVnode));
656✔
3150

3151
  SJson          *pJson = NULL;
656✔
3152
  STSchema       *pSchema = NULL;
656✔
3153
  SSchemaWrapper *pTagSchema = NULL;
656✔
3154
  int64_t         suid = 0;
656✔
3155

3156
  pJson = tjsonParse(req.data);
656✔
3157
  if (pJson == NULL) {
656✔
3158
    code = TSDB_CODE_INVALID_JSON_FORMAT;
×
3159
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
3160
  }
3161

3162
  SMetaReader merStb = {0};
656✔
3163
  metaReaderDoInit(&merStb, pVnode->pMeta, META_READER_LOCK);
656✔
3164
  code = metaGetTableEntryByName(&merStb, AUDIT_STABLE_NAME);
656✔
3165
  if (code != 0) {
656✔
3166
    metaReaderClear(&merStb);
×
3167
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
3168
  }
3169
  vTrace("vgId:%d, get audit stable entry, uid:%" PRId64 ", suid:% " PRId64 ", version:%" PRId64 ", api:%p",
656✔
3170
         TD_VID(pVnode), merStb.me.uid, merStb.me.ctbEntry.suid, merStb.me.version, merStb.pAPI);
3171

3172
  code = vnodeGetTableSchema(pVnode, merStb.me.uid, &pSchema, &suid, &pTagSchema);
656✔
3173
  if (code != 0) {
656✔
3174
    metaReaderClear(&merStb);
×
3175
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
3176
  }
3177
  vTrace("vgId:%d, get audit stable schema, version:%d, suid:%" PRId64, TD_VID(pVnode), pSchema->version, suid);
656✔
3178
  metaReaderClear(&merStb);
656✔
3179
  // pSchema pTagSchema can be cached in the future
3180

3181
  SJson *pRecords = tjsonGetObjectItem(pJson, "records");
656✔
3182
  if (pRecords == NULL) {
656✔
3183
    TAOS_CHECK_GOTO(vnodeSaveOneAuditRecord(pVnode, ver, pJson, pTagSchema, suid, pSchema, pOriginalMsg), &lino, _exit);
328✔
3184
  } else {
3185
    int32_t size = tjsonGetArraySize(pRecords);
328✔
3186
    vTrace("%d items in records", size);
328✔
3187
    for (int32_t i = 0; i < size; ++i) {
1,804✔
3188
      SJson *pRecord = tjsonGetArrayItem(pRecords, i);
1,476✔
3189
      if (pRecord == NULL) {
1,476✔
3190
        vError("vgId:%d, failed to get array item %d", TD_VID(pVnode), i);
×
3191
        code = TSDB_CODE_INVALID_JSON_FORMAT;
×
3192
        TAOS_CHECK_GOTO(code, &lino, _exit);
×
3193
      }
3194
      TAOS_CHECK_GOTO(vnodeSaveOneAuditRecord(pVnode, ver, pRecord, pTagSchema, suid, pSchema, pOriginalMsg), &lino,
1,476✔
3195
                      _exit);
3196
    }
3197
  }
3198

3199
_exit:
656✔
3200
  if (code != 0)
656✔
3201
    vError("vgId:%d, failed to process AuditRecordReq at line:%d, since %s", TD_VID(pVnode), lino, tstrerror(code));
×
3202

3203
  // clear
3204
  if (pSchema) taosMemoryFree(pSchema);
656✔
3205
  if (pTagSchema) {
656✔
3206
    taosMemoryFreeClear(pTagSchema->pSchema);
656✔
3207
    taosMemoryFree(pTagSchema);
656✔
3208
  }
3209
  if (pJson != NULL) {
656✔
3210
    cJSON_Delete(pJson);
656✔
3211
    pJson = NULL;
656✔
3212
  }
3213
  tFreeSVAuditRecordReq(&req);
656✔
3214

3215
  return code;
656✔
3216
}
3217

3218
static int32_t vnodeProcessSubmitReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
1,219,796,895✔
3219
                                     SRpcMsg *pOriginalMsg) {
3220
  int32_t code = 0;
1,219,796,895✔
3221
  int32_t lino = 0;
1,219,796,895✔
3222
  terrno = 0;
1,219,796,895✔
3223

3224
  SSubmitReq2 *pSubmitReq = &(SSubmitReq2){0};
1,219,800,232✔
3225
  SSubmitRsp2 *pSubmitRsp = &(SSubmitRsp2){0};
1,219,800,232✔
3226
  int32_t      ret;
3227
  SEncoder     ec = {0};
1,219,796,825✔
3228

3229
  pRsp->code = TSDB_CODE_SUCCESS;
1,219,798,429✔
3230

3231
  void           *pAllocMsg = NULL;
1,219,797,430✔
3232
  SSubmitReq2Msg *pMsg = (SSubmitReq2Msg *)pReq;
1,219,797,430✔
3233
  SDecoder        dc = {0};
1,219,797,430✔
3234
  if (0 == taosHton64(pMsg->version)) {
1,219,797,430✔
3235
    code = vnodeSubmitReqConvertToSubmitReq2(pVnode, (SSubmitReq *)pMsg, pSubmitReq);
×
3236
    if (TSDB_CODE_SUCCESS == code) {
×
3237
      code = vnodeRebuildSubmitReqMsg(pSubmitReq, &pReq);
×
3238
    }
3239
    if (TSDB_CODE_SUCCESS == code) {
×
3240
      pAllocMsg = pReq;
×
3241
    }
3242
    TSDB_CHECK_CODE(code, lino, _exit);
×
3243
  } else {
3244
    // decode
3245
    pReq = POINTER_SHIFT(pReq, sizeof(SSubmitReq2Msg));
1,219,798,234✔
3246
    len -= sizeof(SSubmitReq2Msg);
1,219,799,038✔
3247

3248
    tDecoderInit(&dc, pReq, len);
1,219,799,038✔
3249
    if (tDecodeSubmitReq(&dc, pSubmitReq, NULL) < 0) {
1,219,800,232✔
3250
      code = TSDB_CODE_INVALID_MSG;
×
3251
      TSDB_CHECK_CODE(code, lino, _exit);
×
3252
    }
3253
  }
3254

3255
  // Scan the request
3256
  code = vnodeScanSubmitReq(pVnode, ver, pSubmitReq, pSubmitRsp);
1,219,778,682✔
3257
  if (code) {
1,219,775,528✔
3258
    vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
3259
           tstrerror(code), ver);
3260
    TSDB_CHECK_CODE(code, lino, _exit);
×
3261
  }
3262

3263
  vGDebug(pOriginalMsg ? &pOriginalMsg->info.traceId : NULL, "vgId:%d, index:%" PRId64 ", submit block, rows:%d",
1,219,775,528✔
3264
          TD_VID(pVnode), ver, (int32_t)taosArrayGetSize(pSubmitReq->aSubmitTbData));
3265

3266
  // Handle auto create table
3267
  code = vnodeHandleAutoCreateTable(pVnode, ver, pSubmitReq, pSubmitRsp);
1,219,796,622✔
3268
  if (code) {
1,219,799,948✔
3269
    vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
1,734✔
3270
           tstrerror(code), ver);
3271
    TSDB_CHECK_CODE(code, lino, _exit);
1,734✔
3272
  }
3273

3274
  // Handle data write
3275
  code = vnodeHandleDataWrite(pVnode, ver, pSubmitReq, pSubmitRsp);
1,219,798,214✔
3276
  if (code) {
1,219,798,380✔
3277
    vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
2,746✔
3278
           tstrerror(code), ver);
3279
    TSDB_CHECK_CODE(code, lino, _exit);
2,746✔
3280
  }
3281

3282
_exit:
1,219,800,114✔
3283
  // message
3284
  pRsp->code = code;
1,219,800,232✔
3285
  tEncodeSize(tEncodeSSubmitRsp2, pSubmitRsp, pRsp->contLen, ret);
1,219,800,232✔
3286
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
1,219,798,872✔
3287
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
1,219,797,213✔
3288
  if (tEncodeSSubmitRsp2(&ec, pSubmitRsp) < 0) {
1,219,799,207✔
3289
    vError("vgId:%d, failed to encode submit response", TD_VID(pVnode));
×
3290
  }
3291
  tEncoderClear(&ec);
1,219,796,519✔
3292

3293
  // update statistics
3294
  (void)atomic_add_fetch_64(&pVnode->statis.nInsert, pSubmitRsp->affectedRows);
1,219,789,999✔
3295
  (void)atomic_add_fetch_64(&pVnode->statis.nInsertSuccess, pSubmitRsp->affectedRows);
1,219,798,755✔
3296
  (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsert, 1);
1,219,797,380✔
3297

3298
  // update metrics
3299
  METRICS_UPDATE(pVnode->writeMetrics.total_requests, METRIC_LEVEL_LOW, 1);
1,219,795,038✔
3300
  METRICS_UPDATE(pVnode->writeMetrics.total_rows, METRIC_LEVEL_HIGH, pSubmitRsp->affectedRows);
1,219,786,058✔
3301
  METRICS_UPDATE(pVnode->writeMetrics.total_bytes, METRIC_LEVEL_LOW, pMsg->header.contLen);
1,219,785,836✔
3302

3303
  if (tsEnableMonitor && tsMonitorFqdn[0] != 0 && tsMonitorPort != 0 && pSubmitRsp->affectedRows > 0 &&
1,219,785,289✔
3304
      strlen(RPC_MSG_USER(pOriginalMsg)) > 0 && tsInsertCounter != NULL) {
×
3305
    const char *sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT_AFFECTED_ROWS,
×
3306
                                   pVnode->monitor.strClusterId,
×
3307
                                   pVnode->monitor.strDnodeId,
×
3308
                                   tsLocalEp,
3309
                                   pVnode->monitor.strVgId,
×
3310
                                   RPC_MSG_USER(pOriginalMsg),
×
3311
                                   "Success"};
3312
    int         tv = taos_counter_add(tsInsertCounter, pSubmitRsp->affectedRows, sample_labels);
×
3313
    if (tv != 0) vError("vgId:%d, failed to taos counter add since return is %d", TD_VID(pVnode), tv);
×
3314
  }
3315

3316
  if (code == 0) {
1,219,785,289✔
3317
    (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
1,219,780,749✔
3318
  }
3319

3320
  // clear
3321
  tDestroySubmitReq(pSubmitReq, 0 == taosHton64(pMsg->version) ? TSDB_MSG_FLG_CMPT : TSDB_MSG_FLG_DECODE);
1,219,791,462✔
3322
  tDestroySSubmitRsp2(pSubmitRsp, TSDB_MSG_FLG_ENCODE);
1,219,773,508✔
3323

3324
  if (code) {
1,219,764,904✔
3325
    terrno = code;
4,480✔
3326
    if (code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
4,480✔
3327
      vInfo("vgId:%d, failed to process submit request since %s, lino:%d, version:%" PRId64, TD_VID(pVnode),
542✔
3328
            tstrerror(code), lino, ver);
3329

3330
    } else {
3331
      vError("vgId:%d, failed to process submit request since %s, lino:%d, version:%" PRId64, TD_VID(pVnode),
3,938✔
3332
             tstrerror(code), lino, ver);
3333
    }
3334
  }
3335

3336
  taosMemoryFree(pAllocMsg);
1,219,775,088✔
3337
  tDecoderClear(&dc);
1,219,772,719✔
3338

3339
  return code;
1,219,790,905✔
3340
}
3341

3342
static int32_t vnodeConsolidateAlterHashRange(SVnode *pVnode, int64_t ver) {
27,860✔
3343
  int32_t code = TSDB_CODE_SUCCESS;
27,860✔
3344

3345
  vInfo("vgId:%d, trim meta of tables per hash range [%" PRIu32 ", %" PRIu32 "]. apply-index:%" PRId64, TD_VID(pVnode),
27,860✔
3346
        pVnode->config.hashBegin, pVnode->config.hashEnd, ver);
3347

3348
  // TODO: trim meta of tables from TDB per hash range [pVnode->config.hashBegin, pVnode->config.hashEnd]
3349
  code = metaTrimTables(pVnode->pMeta, ver);
27,860✔
3350

3351
  return code;
27,860✔
3352
}
3353

3354
static int32_t vnodeProcessAlterConfirmReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
4,363,845✔
3355
  vInfo("vgId:%d, vnode handle msgType:alter-confirm, alter confirm msg is processed", TD_VID(pVnode));
4,363,845✔
3356
  int32_t code = TSDB_CODE_SUCCESS;
4,363,845✔
3357
  if (!pVnode->config.hashChange) {
4,363,845✔
3358
    goto _exit;
4,335,985✔
3359
  }
3360

3361
  code = vnodeConsolidateAlterHashRange(pVnode, ver);
27,860✔
3362
  if (code < 0) {
27,860✔
3363
    vError("vgId:%d, failed to consolidate alter hashrange since %s. version:%" PRId64, TD_VID(pVnode), terrstr(), ver);
×
3364
    goto _exit;
×
3365
  }
3366
  pVnode->config.hashChange = false;
27,860✔
3367

3368
_exit:
4,363,845✔
3369
  pRsp->msgType = TDMT_VND_ALTER_CONFIRM_RSP;
4,363,845✔
3370
  pRsp->code = code;
4,363,845✔
3371
  pRsp->pCont = NULL;
4,363,845✔
3372
  pRsp->contLen = 0;
4,363,845✔
3373

3374
  return code;
4,363,845✔
3375
}
3376

3377
extern int32_t tsdbDisableAndCancelAllBgTask(STsdb *pTsdb);
3378
extern void    tsdbEnableBgTask(STsdb *pTsdb);
3379

3380
static int32_t vnodeProcessAlterConfigReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
365,925✔
3381
  bool walChanged = false;
365,925✔
3382
  bool tsdbChanged = false;
365,925✔
3383

3384
  SAlterVnodeConfigReq req = {0};
365,925✔
3385
  if (tDeserializeSAlterVnodeConfigReq(pReq, len, &req) != 0) {
365,925✔
3386
    terrno = TSDB_CODE_INVALID_MSG;
×
3387
    return TSDB_CODE_INVALID_MSG;
×
3388
  }
3389

3390
  vInfo("vgId:%d, start to alter vnode config, page:%d pageSize:%d buffer:%d szPage:%d szBuf:%" PRIu64
365,925✔
3391
        " cacheLast:%d cacheLastSize:%d cacheLastShards:%d days:%d keep0:%d keep1:%d keep2:%d keepTimeOffset:%d ssKeepLocal:%d "
3392
        "ssCompact:%d allowDrop:%d fsync:%d level:%d "
3393
        "walRetentionPeriod:%d walRetentionSize:%d securityLevel:%d",
3394
        TD_VID(pVnode), req.pages, req.pageSize, req.buffer, req.pageSize * 1024, (uint64_t)req.buffer * 1024 * 1024,
3395
        req.cacheLast, req.cacheLastSize, req.cacheLastShardBits, req.daysPerFile, req.daysToKeep0, req.daysToKeep1, req.daysToKeep2,
3396
        req.keepTimeOffset, req.ssKeepLocal, req.ssCompact, req.allowDrop, req.walFsyncPeriod, req.walLevel,
3397
        req.walRetentionPeriod, req.walRetentionSize, req.securityLevel);
3398

3399
  if (pVnode->config.cacheLastSize != req.cacheLastSize) {
365,925✔
3400
    pVnode->config.cacheLastSize = req.cacheLastSize;
9,760✔
3401
    tsdbCacheSetCapacity(pVnode, (size_t)pVnode->config.cacheLastSize * 1024 * 1024);
9,760✔
3402
  }
3403

3404
  if (pVnode->config.cacheLastShardBits != req.cacheLastShardBits) {
365,925✔
3405
    vInfo("vgId:%d, cacheLastShardBits change from %d to %d, rebuilding last cache",
1,654✔
3406
          TD_VID(pVnode), pVnode->config.cacheLastShardBits, req.cacheLastShardBits);
3407

3408
    // Rebuild only the lruCache under mutex to protect concurrent readers/writers
3409
    (void)taosThreadMutexLock(&pVnode->pTsdb->lruMutex);
1,654✔
3410
    int32_t code = tsdbRebuildLastCache(pVnode->pTsdb, req.cacheLastShardBits);
1,654✔
3411
    (void)taosThreadMutexUnlock(&pVnode->pTsdb->lruMutex);
1,654✔
3412

3413
    if (code) {
1,654✔
3414
      vError("vgId:%d, failed to rebuild last cache since %s", TD_VID(pVnode), tstrerror(code));
×
3415
      return code;
×
3416
    }
3417

3418
    pVnode->config.cacheLastShardBits = req.cacheLastShardBits;
1,654✔
3419

3420
    vInfo("vgId:%d, last cache rebuilt successfully with %d shards",
1,654✔
3421
          TD_VID(pVnode), taosLRUCacheGetNumShards(pVnode->pTsdb->lruCache));
3422
  }
3423

3424
  if (pVnode->config.szBuf != req.buffer * 1024LL * 1024LL) {
365,925✔
3425
    vInfo("vgId:%d, vnode buffer is changed from %" PRId64 " to %" PRId64, TD_VID(pVnode), pVnode->config.szBuf,
2,016✔
3426
          (uint64_t)(req.buffer * 1024LL * 1024LL));
3427
    pVnode->config.szBuf = req.buffer * 1024LL * 1024LL;
2,016✔
3428
  }
3429

3430
  if (pVnode->config.szCache != req.pages) {
365,925✔
3431
    if ((terrno = metaAlterCache(pVnode->pMeta, req.pages)) < 0) {
5,118✔
3432
      vError("vgId:%d, failed to change vnode pages from %d to %d failed since %s", TD_VID(pVnode),
×
3433
             pVnode->config.szCache, req.pages, tstrerror(terrno));
3434
      return terrno;
×
3435
    } else {
3436
      vInfo("vgId:%d, vnode pages is changed from %d to %d", TD_VID(pVnode), pVnode->config.szCache, req.pages);
5,118✔
3437
      pVnode->config.szCache = req.pages;
5,118✔
3438
    }
3439
  }
3440

3441
  if (pVnode->config.cacheLast != req.cacheLast) {
365,925✔
3442
    pVnode->config.cacheLast = req.cacheLast;
103,003✔
3443
  }
3444

3445
  if (pVnode->config.walCfg.fsyncPeriod != req.walFsyncPeriod) {
365,925✔
3446
    pVnode->config.walCfg.fsyncPeriod = req.walFsyncPeriod;
58,966✔
3447
    walChanged = true;
58,966✔
3448
  }
3449

3450
  if (pVnode->config.walCfg.level != req.walLevel) {
365,925✔
3451
    if (pVnode->config.walCfg.level == 0) {
43,410✔
3452
      pVnode->config.walCfg.clearFiles = 1;
2,452✔
3453
    }
3454
    pVnode->config.walCfg.level = req.walLevel;
43,410✔
3455
    walChanged = true;
43,410✔
3456
  }
3457

3458
  if (pVnode->config.walCfg.retentionPeriod != req.walRetentionPeriod) {
365,925✔
3459
    pVnode->config.walCfg.retentionPeriod = req.walRetentionPeriod;
55,738✔
3460
    walChanged = true;
55,738✔
3461
  }
3462

3463
  if (pVnode->config.walCfg.retentionSize != req.walRetentionSize) {
365,925✔
3464
    pVnode->config.walCfg.retentionSize = req.walRetentionSize;
1,634✔
3465
    walChanged = true;
1,634✔
3466
  }
3467

3468
  if (pVnode->config.tsdbCfg.keep0 != req.daysToKeep0) {
365,925✔
3469
    pVnode->config.tsdbCfg.keep0 = req.daysToKeep0;
49,312✔
3470
    tsdbChanged = true;
49,312✔
3471
  }
3472

3473
  if (pVnode->config.tsdbCfg.keep1 != req.daysToKeep1) {
365,925✔
3474
    pVnode->config.tsdbCfg.keep1 = req.daysToKeep1;
47,664✔
3475
    tsdbChanged = true;
47,664✔
3476
  }
3477

3478
  if (pVnode->config.tsdbCfg.keep2 != req.daysToKeep2) {
365,925✔
3479
    pVnode->config.tsdbCfg.keep2 = req.daysToKeep2;
41,188✔
3480
    tsdbChanged = true;
41,188✔
3481
  }
3482

3483
  if (pVnode->config.tsdbCfg.keepTimeOffset != req.keepTimeOffset) {
365,925✔
3484
    pVnode->config.tsdbCfg.keepTimeOffset = req.keepTimeOffset;
8,270✔
3485
    tsdbChanged = true;
8,270✔
3486
  }
3487

3488
  if (req.sttTrigger != -1 && req.sttTrigger != pVnode->config.sttTrigger) {
365,925✔
3489
    if (req.sttTrigger > 1 && pVnode->config.sttTrigger > 1) {
2,862✔
3490
      pVnode->config.sttTrigger = req.sttTrigger;
1,634✔
3491
    } else {
3492
      vnodeAWait(&pVnode->commitTask);
1,228✔
3493

3494
      int32_t ret = tsdbDisableAndCancelAllBgTask(pVnode->pTsdb);
1,228✔
3495
      if (ret != 0) {
1,228✔
3496
        vError("vgId:%d, failed to disable bg task since %s", TD_VID(pVnode), tstrerror(ERRNO));
×
3497
      }
3498

3499
      pVnode->config.sttTrigger = req.sttTrigger;
1,228✔
3500
      tsdbEnableBgTask(pVnode->pTsdb);
1,228✔
3501
    }
3502
  }
3503

3504
  if (req.minRows != -1 && req.minRows != pVnode->config.tsdbCfg.minRows) {
365,925✔
3505
    pVnode->config.tsdbCfg.minRows = req.minRows;
1,634✔
3506
  }
3507

3508
  if (req.ssKeepLocal != -1 && req.ssKeepLocal != pVnode->config.ssKeepLocal) {
365,925✔
3509
    pVnode->config.ssKeepLocal = req.ssKeepLocal;
1,634✔
3510
  }
3511
  if (req.ssCompact != -1 && req.ssCompact != pVnode->config.ssCompact) {
365,925✔
3512
    pVnode->config.ssCompact = req.ssCompact;
1,634✔
3513
  }
3514
  if (req.allowDrop != pVnode->config.allowDrop) {
365,925✔
3515
    pVnode->config.allowDrop = req.allowDrop;
×
3516
  }
3517
  if (req.securityLevel != pVnode->config.securityLevel) {
365,925✔
3518
    pVnode->config.securityLevel = req.securityLevel;
3,660✔
3519
  }
3520
  if (req.secureDelete != pVnode->config.secureDelete) {
365,101✔
3521
    pVnode->config.secureDelete = req.secureDelete;
1,284✔
3522
  }
3523

3524
  if (walChanged) {
365,925✔
3525
    if (walAlter(pVnode->pWal, &pVnode->config.walCfg) != 0) {
156,440✔
3526
      vError("vgId:%d, failed to alter wal config since %s", TD_VID(pVnode), tstrerror(ERRNO));
×
3527
    }
3528
  }
3529

3530
  if (tsdbChanged) {
365,925✔
3531
    tsdbSetKeepCfg(pVnode->pTsdb, &pVnode->config.tsdbCfg);
65,828✔
3532
  }
3533

3534
  return 0;
365,925✔
3535
}
3536

3537
static int32_t vnodeProcessBatchDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
3538
  SBatchDeleteReq deleteReq;
×
3539
  SDecoder        decoder;
×
3540
  tDecoderInit(&decoder, pReq, len);
×
3541
  if (tDecodeSBatchDeleteReq(&decoder, &deleteReq) < 0) {
×
3542
    tDecoderClear(&decoder);
×
3543
    return terrno = TSDB_CODE_INVALID_MSG;
×
3544
  }
3545

3546
  SMetaReader mr = {0};
×
3547
  metaReaderDoInit(&mr, pVnode->pMeta, META_READER_NOLOCK);
×
3548
  STsdb *pTsdb = pVnode->pTsdb;
×
3549

3550
  int32_t sz = taosArrayGetSize(deleteReq.deleteReqs);
×
3551
  for (int32_t i = 0; i < sz; i++) {
×
3552
    SSingleDeleteReq *pOneReq = taosArrayGet(deleteReq.deleteReqs, i);
×
3553
    char             *name = pOneReq->tbname;
×
3554
    if (metaGetTableEntryByName(&mr, name) < 0) {
×
3555
      vDebug("vgId:%d, stream delete msg, skip since no table: %s", pVnode->config.vgId, name);
×
3556
      continue;
×
3557
    }
3558

3559
    int64_t uid = mr.me.uid;
×
3560

3561
    int32_t code = tsdbDeleteTableData(pTsdb, ver, deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs, 0);
×
3562
    if (code < 0) {
×
3563
      terrno = code;
×
3564
      vError("vgId:%d, delete error since %s, suid:%" PRId64 ", uid:%" PRId64 ", start ts:%" PRId64 ", end ts:%" PRId64,
×
3565
             TD_VID(pVnode), terrstr(), deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
3566
    }
3567

3568
    if (deleteReq.level == 0) {
×
3569
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, deleteReq.ctimeMs);
×
3570
      if (code < 0) {
×
3571
        terrno = code;
×
3572
        vError("vgId:%d, update change time error since %s, suid:%" PRId64 ", uid:%" PRId64 ", start ts:%" PRId64
×
3573
               ", end ts:%" PRId64,
3574
               TD_VID(pVnode), terrstr(), deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
3575
      }
3576
    }
3577
    tDecoderClear(&mr.coder);
×
3578
  }
3579
  metaReaderClear(&mr);
×
3580
  taosArrayDestroy(deleteReq.deleteReqs);
×
3581
  return 0;
×
3582
}
3583

3584
static int32_t vnodeProcessDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
2,422,801✔
3585
                                     SRpcMsg *pOriginalMsg) {
3586
  int32_t     code = 0;
2,422,801✔
3587
  SDecoder   *pCoder = &(SDecoder){0};
2,422,801✔
3588
  SDeleteRes *pRes = &(SDeleteRes){0};
2,422,801✔
3589

3590
  pRsp->msgType = TDMT_VND_DELETE_RSP;
2,422,801✔
3591
  pRsp->pCont = NULL;
2,422,801✔
3592
  pRsp->contLen = 0;
2,422,801✔
3593
  pRsp->code = TSDB_CODE_SUCCESS;
2,422,801✔
3594

3595
  pRes->uidList = taosArrayInit(0, sizeof(tb_uid_t));
2,422,801✔
3596
  if (pRes->uidList == NULL) {
2,422,801✔
3597
    code = terrno;
×
3598
    goto _err;
×
3599
  }
3600

3601
  tDecoderInit(pCoder, pReq, len);
2,422,801✔
3602
  code = tDecodeDeleteRes(pCoder, pRes);
2,422,801✔
3603
  if (code) goto _err;
2,422,801✔
3604

3605
  if (pRes->affectedRows > 0) {
2,422,801✔
3606
    for (int32_t iUid = 0; iUid < taosArrayGetSize(pRes->uidList); iUid++) {
4,316,084✔
3607
      uint64_t uid = *(uint64_t *)taosArrayGet(pRes->uidList, iUid);
2,459,311✔
3608
      // Merge runtime vnode(db-level) secureDelete to avoid stale client meta after ALTER DATABASE.
3609
      int8_t secureDelete = pRes->secureDelete | pVnode->config.secureDelete;
2,459,311✔
3610
      code = tsdbDeleteTableData(pVnode->pTsdb, ver, pRes->suid, uid, pRes->skey, pRes->ekey, secureDelete);
2,459,311✔
3611
      if (code) goto _err;
2,459,311✔
3612
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, pRes->ctimeMs);
2,459,311✔
3613
      if (code) goto _err;
2,459,311✔
3614
    }
3615
  }
3616

3617
  tDecoderClear(pCoder);
2,422,801✔
3618
  taosArrayDestroy(pRes->uidList);
2,422,801✔
3619

3620
  SVDeleteRsp rsp = {.affectedRows = pRes->affectedRows};
2,422,801✔
3621
  int32_t     ret = 0;
2,422,801✔
3622
  tEncodeSize(tEncodeSVDeleteRsp, &rsp, pRsp->contLen, ret);
2,422,801✔
3623
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
2,422,801✔
3624
  SEncoder ec = {0};
2,422,801✔
3625
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
2,422,801✔
3626
  code = tEncodeSVDeleteRsp(&ec, &rsp);
2,422,801✔
3627
  if (code) goto _err;
2,422,801✔
3628
  tEncoderClear(&ec);
2,422,801✔
3629
  return code;
2,422,801✔
3630

3631
_err:
×
3632
  /*
3633
  if(code == TSDB_CODE_SUCCESS){
3634
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_DELETE, pVnode->monitor.strClusterId,
3635
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
3636
                                        RPC_MSG_USER(pOriginalMsg), "Success"};
3637
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
3638
  }
3639
  else{
3640
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_DELETE, pVnode->monitor.strClusterId,
3641
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
3642
                                        RPC_MSG_USER(pOriginalMsg), "Failed"};
3643
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
3644
  }
3645
  */
3646

3647
  return code;
×
3648
}
3649
static int32_t vnodeProcessCreateIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
20,476✔
3650
  SVCreateStbReq req = {0};
20,476✔
3651
  SDecoder       dc = {0};
20,476✔
3652
  int32_t        code = 0;
20,476✔
3653

3654
  pRsp->msgType = TDMT_VND_CREATE_INDEX_RSP;
20,476✔
3655
  pRsp->code = TSDB_CODE_SUCCESS;
20,476✔
3656
  pRsp->pCont = NULL;
20,476✔
3657
  pRsp->contLen = 0;
20,476✔
3658

3659
  tDecoderInit(&dc, pReq, len);
20,476✔
3660
  // decode req
3661
  if (tDecodeSVCreateStbReq(&dc, &req) < 0) {
20,476✔
3662
    tDecoderClear(&dc);
×
3663
    return terrno = TSDB_CODE_INVALID_MSG;
×
3664
  }
3665

3666
  code = metaAddIndexToSuperTable(pVnode->pMeta, ver, &req);
20,476✔
3667
  if (code) {
20,476✔
3668
    pRsp->code = code;
×
3669
    goto _err;
×
3670
  }
3671
  tDecoderClear(&dc);
20,476✔
3672
  return 0;
20,476✔
3673

3674
_err:
×
3675
  tDecoderClear(&dc);
×
3676
  return code;
×
3677
}
3678
static int32_t vnodeProcessDropIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
16,640✔
3679
  SDropIndexReq req = {0};
16,640✔
3680
  int32_t       code = 0;
16,640✔
3681
  pRsp->msgType = TDMT_VND_DROP_INDEX_RSP;
16,640✔
3682
  pRsp->code = TSDB_CODE_SUCCESS;
16,640✔
3683
  pRsp->pCont = NULL;
16,640✔
3684
  pRsp->contLen = 0;
16,640✔
3685

3686
  if ((code = tDeserializeSDropIdxReq(pReq, len, &req))) {
16,640✔
3687
    pRsp->code = code;
×
3688
    return code;
×
3689
  }
3690

3691
  code = metaDropIndexFromSuperTable(pVnode->pMeta, ver, &req);
16,640✔
3692
  if (code) {
16,640✔
3693
    pRsp->code = code;
×
3694
    return code;
×
3695
  }
3696
  return TSDB_CODE_SUCCESS;
16,640✔
3697
}
3698

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

3701
static int32_t vnodeProcessCompactVnodeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
78,365✔
3702
  if (!pVnode->restored) {
78,365✔
3703
    vInfo("vgId:%d, ignore compact req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
4,374✔
3704
    return 0;
4,374✔
3705
  }
3706
  return vnodeAsyncCompact(pVnode, ver, pReq, len, pRsp);
73,991✔
3707
}
3708

3709
static int32_t vnodeProcessConfigChangeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
3710
  if (syncCheckMember(pVnode->sync) != 0) {
×
3711
    vError("vgId:%d, failed to check member", TD_VID(pVnode));
×
3712
  }
3713

3714
  pRsp->msgType = TDMT_SYNC_CONFIG_CHANGE_RSP;
×
3715
  pRsp->code = TSDB_CODE_SUCCESS;
×
3716
  pRsp->pCont = NULL;
×
3717
  pRsp->contLen = 0;
×
3718

3719
  return 0;
×
3720
}
3721

3722
static int32_t vnodeCheckState(SVnode *pVnode) {
78,431✔
3723
  SSyncState syncState = syncGetState(pVnode->sync);
78,431✔
3724
  if (syncState.state != TAOS_SYNC_STATE_LEADER) {
78,431✔
3725
    return terrno = TSDB_CODE_SYN_NOT_LEADER;
35,786✔
3726
  }
3727
  return 0;
42,645✔
3728
}
3729

3730
static int32_t vnodeCheckToken(SVnode *pVnode, char *member0Token, char *member1Token) {
42,645✔
3731
  char token[TSDB_ARB_TOKEN_SIZE] = {0};
42,645✔
3732
  if (vnodeGetArbToken(pVnode, token) != 0) {
42,645✔
3733
    return terrno = TSDB_CODE_NOT_FOUND;
×
3734
  }
3735

3736
  if (strncmp(token, member0Token, TSDB_ARB_TOKEN_SIZE) != 0 &&
42,645✔
3737
      strncmp(token, member1Token, TSDB_ARB_TOKEN_SIZE) != 0) {
38,857✔
3738
    return terrno = TSDB_CODE_MND_ARB_TOKEN_MISMATCH;
37,019✔
3739
  }
3740

3741
  terrno = TSDB_CODE_SUCCESS;
5,626✔
3742
  return 0;
5,626✔
3743
}
3744

3745
static int32_t vnodeCheckSyncd(SVnode *pVnode, char *member0Token, char *member1Token) {
21,589✔
3746
  int32_t code = vnodeCheckToken(pVnode, member0Token, member1Token);
21,589✔
3747
  if (code != 0) {
21,589✔
3748
    return code;
18,776✔
3749
  }
3750

3751
  return syncCheckSynced(pVnode->sync);
2,813✔
3752
}
3753

3754
static int32_t vnodeProcessArbCheckSyncReq(SVnode *pVnode, void *pReq, int32_t len, SRpcMsg *pRsp) {
54,145✔
3755
  int32_t code = 0;
54,145✔
3756

3757
  if ((code = vnodeCheckState(pVnode)) != 0) {
54,145✔
3758
    vDebug("vgId:%d, failed to preprocess vnode-arb-check-sync request since %s", TD_VID(pVnode), tstrerror(code));
32,556✔
3759
    return 0;
32,556✔
3760
  }
3761

3762
  SVArbCheckSyncReq syncReq = {0};
21,589✔
3763

3764
  code = tDeserializeSVArbCheckSyncReq(pReq, len, &syncReq);
21,589✔
3765
  if (code) {
21,589✔
3766
    return code;
×
3767
  }
3768

3769
  vInfo("vgId:%d, start to process vnode-arb-check-sync req QID:0x%" PRIx64 ":0x%" PRIx64 ", seqNum:%" PRIx64
21,589✔
3770
        ", arbToken:%s, member0Token:%s, member1Token:%s, "
3771
        "arbTerm:%" PRId64,
3772
        TD_VID(pVnode), pRsp->info.traceId.rootId, pRsp->info.traceId.msgId, pRsp->info.seqNum, syncReq.arbToken,
3773
        syncReq.member0Token, syncReq.member1Token, syncReq.arbTerm);
3774

3775
  pRsp->msgType = TDMT_VND_ARB_CHECK_SYNC_RSP;
21,589✔
3776
  pRsp->code = TSDB_CODE_SUCCESS;
21,589✔
3777
  pRsp->pCont = NULL;
21,589✔
3778
  pRsp->contLen = 0;
21,589✔
3779

3780
  SVArbCheckSyncRsp syncRsp = {0};
21,589✔
3781
  syncRsp.arbToken = syncReq.arbToken;
21,589✔
3782
  syncRsp.member0Token = syncReq.member0Token;
21,589✔
3783
  syncRsp.member1Token = syncReq.member1Token;
21,589✔
3784
  syncRsp.vgId = TD_VID(pVnode);
21,589✔
3785

3786
  if ((syncRsp.errCode = vnodeCheckSyncd(pVnode, syncReq.member0Token, syncReq.member1Token)) != 0) {
21,589✔
3787
    vError("vgId:%d, failed to check assigned log syncd since %s", TD_VID(pVnode), tstrerror(syncRsp.errCode));
18,776✔
3788
  }
3789

3790
  if ((code = vnodeUpdateArbTerm(pVnode, syncReq.arbTerm)) != 0) {
21,589✔
3791
    vError("vgId:%d, failed to update arb term since %s", TD_VID(pVnode), tstrerror(code));
×
3792
    goto _OVER;
×
3793
  }
3794

3795
  int32_t contLen = tSerializeSVArbCheckSyncRsp(NULL, 0, &syncRsp);
21,589✔
3796
  if (contLen <= 0) {
21,589✔
3797
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
3798
    code = -1;
×
3799
    goto _OVER;
×
3800
  }
3801
  void *pHead = rpcMallocCont(contLen);
21,589✔
3802
  if (!pHead) {
21,589✔
3803
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
3804
    code = -1;
×
3805
    goto _OVER;
×
3806
  }
3807

3808
  if (tSerializeSVArbCheckSyncRsp(pHead, contLen, &syncRsp) <= 0) {
21,589✔
3809
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
3810
    rpcFreeCont(pHead);
×
3811
    code = -1;
×
3812
    goto _OVER;
×
3813
  }
3814

3815
  pRsp->pCont = pHead;
21,589✔
3816
  pRsp->contLen = contLen;
21,589✔
3817

3818
  vInfo(
21,589✔
3819
      "vgId:%d, suceed to process vnode-arb-check-sync req rsp.code:%s, arbToken:%s, member0Token:%s, "
3820
      "member1Token:%s",
3821
      TD_VID(pVnode), tstrerror(syncRsp.errCode), syncRsp.arbToken, syncRsp.member0Token, syncRsp.member1Token);
3822

3823
  code = TSDB_CODE_SUCCESS;
21,589✔
3824

3825
_OVER:
21,589✔
3826
  if (code != 0) {
21,589✔
3827
    vError("vgId:%d, failed to process arb check req rsp.code:%s since %s", TD_VID(pVnode), tstrerror(syncRsp.errCode),
×
3828
           tstrerror(code));
3829
  }
3830
  tFreeSVArbCheckSyncReq(&syncReq);
21,589✔
3831
  return code;
21,589✔
3832
}
3833

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