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

taosdata / TDengine / #5050

12 May 2026 05:36AM UTC coverage: 73.398% (+0.09%) from 73.313%
#5050

push

travis-ci

web-flow
merge: from main to 3.0 branch #35319

90 of 101 new or added lines in 2 files covered. (89.11%)

489 existing lines in 125 files now uncovered.

281602 of 383662 relevant lines covered (73.4%)

138099127.08 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) {
74,288,519✔
91
  int32_t code = 0;
74,288,519✔
92
  int32_t lino = 0;
74,288,519✔
93

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

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

105
  // name
106
  char *name = NULL;
74,349,168✔
107
  if (tDecodeCStr(pCoder, &name) < 0) {
74,350,708✔
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);
74,350,708✔
114
  if (uid == 0) {
74,359,383✔
115
    uid = tGenIdPI64();
73,895,649✔
116
  }
117
  taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), uid);
74,338,824✔
118

119
  // btime
120
  taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos + 8), btime);
74,331,636✔
121

122
  tEndDecode(pCoder);
74,338,218✔
123

124
_exit:
74,339,496✔
125
  if (code) {
74,338,694✔
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);
74,338,694✔
129
    if (pUid) *pUid = uid;
74,325,894✔
130
  }
131
  return code;
74,326,603✔
132
}
133
static int32_t vnodePreProcessCreateTableMsg(SVnode *pVnode, SRpcMsg *pMsg) {
49,902,773✔
134
  int32_t code = 0;
49,902,773✔
135
  int32_t lino = 0;
49,902,773✔
136

137
  int64_t  btime = taosGetTimestampMs();
49,903,260✔
138
  SDecoder dc = {0};
49,903,260✔
139
  int32_t  nReqs;
49,900,552✔
140

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

147
  if (tDecodeI32v(&dc, &nReqs) < 0) {
49,902,785✔
148
    code = TSDB_CODE_INVALID_MSG;
×
149
    TSDB_CHECK_CODE(code, lino, _exit);
×
150
  }
151
  for (int32_t iReq = 0; iReq < nReqs; iReq++) {
106,025,482✔
152
    code = vnodePreprocessCreateTableReq(pVnode, &dc, btime, NULL);
56,145,548✔
153
    TSDB_CHECK_CODE(code, lino, _exit);
56,122,697✔
154
  }
155

156
  tEndDecode(&dc);
49,879,934✔
157

158
_exit:
49,900,688✔
159
  tDecoderClear(&dc);
49,900,745✔
160
  if (code) {
49,900,343✔
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;
49,897,079✔
165
}
166

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

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

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

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

182
  destroyAlterTbReq(&vAlterTbReq);
13,660,427✔
183

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

195
static int32_t vnodePreProcessDropTtlMsg(SVnode *pVnode, SRpcMsg *pMsg) {
12,210,786✔
196
  int32_t code = TSDB_CODE_INVALID_MSG;
12,210,786✔
197
  int32_t lino = 0;
12,210,786✔
198

199
  SMsgHead *pContOld = pMsg->pCont;
12,210,786✔
200
  int32_t   reqLenOld = pMsg->contLen - sizeof(SMsgHead);
12,225,232✔
201

202
  SArray *tbUids = NULL;
12,225,625✔
203
  int64_t timestampMs = 0;
12,225,625✔
204

205
  SVDropTtlTableReq ttlReq = {0};
12,225,625✔
206
  if (tDeserializeSVDropTtlTableReq((char *)pContOld + sizeof(SMsgHead), reqLenOld, &ttlReq) != 0) {
12,221,348✔
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));
12,221,413✔
213
    if (tbUids == NULL) {
12,220,281✔
214
      code = terrno;
×
215
      TSDB_CHECK_CODE(code, lino, _exit);
×
216
    }
217

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

225
    ttlReq.nUids = taosArrayGetSize(tbUids);
12,198,285✔
226
    ttlReq.pTbUids = tbUids;
12,205,148✔
227
  }
228

229
  if (ttlReq.nUids == 0) {
12,205,148✔
230
    code = TSDB_CODE_MSG_PREPROCESSED;
12,183,791✔
231
    TSDB_CHECK_CODE(code, lino, _exit);
12,183,791✔
232
  }
233

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

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

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

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

255
  code = 0;
13,185✔
256

257
_exit:
12,202,869✔
258
  taosArrayDestroy(tbUids);
12,207,312✔
259

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

267
  return code;
12,172,585✔
268
}
269

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

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

280
  SSubmitTbData submitTbData;
869,719,351✔
281
  uint8_t       version;
282
  if (tDecodeI32v(pCoder, &submitTbData.flags) < 0) {
869,655,804✔
283
    code = TSDB_CODE_INVALID_MSG;
×
284
    TSDB_CHECK_CODE(code, lino, _exit);
×
285
  }
286
  version = (submitTbData.flags >> 8) & 0xff;
869,655,804✔
287
  submitTbData.flags = submitTbData.flags & 0xff;
869,655,804✔
288

289
  int64_t uid;
869,651,345✔
290
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
869,654,252✔
291
    code = vnodePreprocessCreateTableReq(pVnode, pCoder, btimeMs, &uid);
18,164,281✔
292
    TSDB_CHECK_CODE(code, lino, _exit);
18,199,603✔
293
  }
294

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

301
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
869,699,828✔
302
    taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), uid);
18,188,020✔
303
    pCoder->pos += sizeof(int64_t);
18,180,551✔
304
  } else {
305
    if (tDecodeI64(pCoder, &submitTbData.uid) < 0) {
851,527,366✔
306
      code = TSDB_CODE_INVALID_MSG;
×
307
      TSDB_CHECK_CODE(code, lino, _exit);
×
308
    }
309
  }
310

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

316
  // scan and check
317
  TSKEY now = btimeMs;
869,706,048✔
318
  if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_MICRO) {
869,706,048✔
319
    now *= 1000;
2,888,509✔
320
  } else if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_NANO) {
866,822,681✔
321
    now *= 1000000;
1,689,986✔
322
  }
323

324
  int32_t keep = pVnode->config.tsdbCfg.keep2;
869,709,364✔
325

326
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * keep;
869,721,150✔
327
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
869,737,859✔
328
  if (submitTbData.flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
869,730,037✔
329
    uint64_t nColData;
7,714,592✔
330
    if (tDecodeU64v(pCoder, &nColData) < 0) {
7,715,014✔
331
      code = TSDB_CODE_INVALID_MSG;
×
332
      TSDB_CHECK_CODE(code, lino, _exit);
×
333
    }
334

335
    SColData colData = {0};
7,715,014✔
336
    code = tDecodeColData(version, pCoder, &colData, false);
7,714,265✔
337
    if (code) {
7,715,011✔
338
      code = TSDB_CODE_INVALID_MSG;
×
339
      TSDB_CHECK_CODE(code, lino, _exit);
×
340
    }
341

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

354
    for (uint64_t i = 1; i < nColData; i++) {
23,709,756✔
355
      code = tDecodeColData(version, pCoder, &colData, true);
16,117,407✔
356
      if (code) {
16,118,129✔
357
        code = TSDB_CODE_INVALID_MSG;
×
358
        TSDB_CHECK_CODE(code, lino, _exit);
×
359
      }
360
    }
361
  } else {
362
    uint64_t nRow;
862,010,986✔
363
    if (tDecodeU64v(pCoder, &nRow) < 0) {
862,009,076✔
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;
74,998✔
378
        TSDB_CHECK_CODE(code, lino, _exit);
74,998✔
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)) {
869,670,632✔
390
    taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), ctimeMs);
869,662,826✔
391
    pCoder->pos += sizeof(int64_t);
869,624,389✔
392
  }
393

394
  tEndDecode(pCoder);
869,645,909✔
395

396
_exit:
869,712,959✔
397
  if (code) {
869,693,008✔
398
    vError("vgId:%d, %s:%d failed to vnodePreProcessSubmitTbData submit request since %s", TD_VID(pVnode), __func__,
75,655✔
399
           lino, tstrerror(code));
400
  }
401
  return code;
869,693,008✔
402
}
403
static int32_t vnodePreProcessSubmitMsg(SVnode *pVnode, SRpcMsg *pMsg) {
782,903,767✔
404
  int32_t code = 0;
782,903,767✔
405
  int32_t lino = 0;
782,903,767✔
406

407
  if (tsBypassFlag & TSDB_BYPASS_RA_RPC_RECV_SUBMIT) {
782,903,767✔
408
    return TSDB_CODE_MSG_PREPROCESSED;
83✔
409
  }
410

411
  SDecoder *pCoder = &(SDecoder){0};
782,903,684✔
412

413
  if (taosHton64(((SSubmitReq2Msg *)pMsg->pCont)->version) != 1) {
782,903,545✔
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));
782,903,447✔
419

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

425
  uint64_t nSubmitTbData;
782,899,736✔
426
  if (tDecodeU64v(pCoder, &nSubmitTbData) < 0) {
782,900,438✔
427
    code = TSDB_CODE_INVALID_MSG;
×
428
    TSDB_CHECK_CODE(code, lino, _exit);
×
429
  }
430

431
  int64_t btimeMs = taosGetTimestampMs();
782,903,427✔
432
  int64_t ctimeMs = btimeMs;
782,903,427✔
433
  for (uint64_t i = 0; i < nSubmitTbData; i++) {
1,652,527,346✔
434
    code = vnodePreProcessSubmitTbData(pVnode, pCoder, btimeMs, ctimeMs);
869,718,318✔
435
    TSDB_CHECK_CODE(code, lino, _exit);
869,699,574✔
436
  }
437

438
  tEndDecode(pCoder);
782,809,028✔
439

440
_exit:
782,887,822✔
441
  tDecoderClear(pCoder);
782,901,383✔
442
  if (code) {
782,899,590✔
443
    vError("vgId:%d, %s:%d failed to preprocess submit request since %s, msg type:%s", TD_VID(pVnode), __func__, lino,
75,655✔
444
           tstrerror(code), TMSG_INFO(pMsg->msgType));
445
  }
446
  return code;
782,886,026✔
447
}
448

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

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

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

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

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

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

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

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

485
  taosArrayDestroy(res.uidList);
1,988,799✔
486

487
_exit:
1,989,430✔
488
  return code;
1,989,430✔
489
}
490

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

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

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

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

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

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

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

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

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

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

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

538
  return code;
38,899✔
539
}
540

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

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

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

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

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

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

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

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

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

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

619
int32_t vnodePreProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
889,419,059✔
620
  int32_t code = 0;
889,419,059✔
621

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

628
  switch (pMsg->msgType) {
889,428,598✔
629
    case TDMT_VND_CREATE_TABLE: {
49,903,260✔
630
      code = vnodePreProcessCreateTableMsg(pVnode, pMsg);
49,903,260✔
631
    } break;
49,896,923✔
632
    case TDMT_VND_ALTER_TABLE: {
13,662,605✔
633
      code = vnodePreProcessAlterTableMsg(pVnode, pMsg);
13,662,605✔
634
    } break;
13,661,099✔
635
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
12,219,236✔
636
    case TDMT_VND_DROP_TTL_TABLE: {
637
      code = vnodePreProcessDropTtlMsg(pVnode, pMsg);
12,219,236✔
638
    } break;
12,205,707✔
639
    case TDMT_VND_SUBMIT: {
782,903,984✔
640
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.preprocess_time, METRIC_LEVEL_LOW,
782,903,984✔
641
                           { code = vnodePreProcessSubmitMsg(pVnode, pMsg); });
642
    } break;
782,891,166✔
643
    case TDMT_VND_DELETE: {
1,990,061✔
644
      code = vnodePreProcessDeleteMsg(pVnode, pMsg);
1,990,061✔
645
    } break;
1,989,430✔
646
    case TDMT_VND_BATCH_DEL: {
×
647
      code = vnodePreProcessBatchDeleteMsg(pVnode, pMsg);
×
648
    } break;
×
649
    case TDMT_VND_ARB_CHECK_SYNC: {
67,851✔
650
      code = vnodePreProcessArbCheckSyncMsg(pVnode, pMsg);
67,851✔
651
    } break;
67,851✔
652
    case TDMT_VND_DROP_TABLE: {
1,831,818✔
653
      code = vnodePreProcessDropTbMsg(pVnode, pMsg);
1,831,818✔
654
    } break;
1,831,818✔
655
#ifdef TD_ENTERPRISE
656
    case TDMT_VND_SSMIGRATE_FILESET: {
4,140✔
657
      code = vnodePreProcessSsMigrateFileSetReq(pVnode, pMsg);
4,140✔
658
    } break;
4,140✔
659
#endif
660
    case TDMT_VND_AUDIT_RECORD: {
6,819✔
661
      vTrace("vgId:%d, pre process TDMT_VND_AUDIT_RECORD", TD_VID(pVnode));
6,819✔
662
    } break;
664✔
663
    default:
26,840,855✔
664
      break;
26,840,855✔
665
  }
666

667
  if (code && code != TSDB_CODE_MSG_PREPROCESSED) {
889,389,653✔
668
    vError("vgId:%d, failed to preprocess write request since %s, msg type:%s", TD_VID(pVnode), tstrerror(code),
110,631✔
669
           TMSG_INFO(pMsg->msgType));
670
  }
671
  return code;
889,384,725✔
672
}
673

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

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

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

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

687
  SRow  **pRow = (SRow **)TARRAY_DATA(pSubmitTbData->aRowP);
50,451✔
688
  int32_t rowIdx = -1;
50,451✔
689
  for (int32_t i = 0; i < sz; i++) {
27,880,932✔
690
    int64_t     seq = 0;
27,830,481✔
691
    SBlobValue *p = taosArrayGet(pBlobSet->pSeqTable, i);
27,830,481✔
692
    if (p->type == TSDB_DATA_BLOB_EMPTY_VALUE || p->type == TSDB_DATA_BLOB_NULL_VALUE) {
27,830,481✔
693
      // skip empty or null blob
694
      continue;
2,763,912✔
695
    }
696

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

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

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

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

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

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

738
  SBseBatch *pBatch = NULL;
336✔
739

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

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

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

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

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

796
  return code;
50,787✔
797
}
798

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

806
  (void)taosThreadMutexLock(&pVnode->mutex);
1,494,386,545✔
807
  if (pVnode->disableWrite) {
1,494,505,832✔
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,494,376,464✔
813

814
  if (ver <= pVnode->state.applied) {
1,494,515,783✔
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,494,373,454✔
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,494,451,096✔
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,494,536,525✔
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,494,537,998✔
839
  atomic_store_64(&pVnode->state.applyTerm, pMsg->info.conn.applyTerm);
1,494,538,579✔
840

841
  if (!syncUtilUserCommit(pMsg->msgType)) goto _exit;
1,494,538,579✔
842

843
  // skip header
844
  pReq = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
1,479,973,424✔
845
  len = pMsg->contLen - sizeof(SMsgHead);
1,479,972,999✔
846
  bool needCommit = false;
1,479,971,997✔
847
  bool forceTrimWal = false;
1,479,971,997✔
848

849
  switch (pMsg->msgType) {
1,479,971,997✔
850
    /* META */
851
    case TDMT_VND_CREATE_STB:
6,585,128✔
852
      code = vnodeProcessCreateStbReq(pVnode, ver, pReq, len, pRsp);
6,585,128✔
853
      TSDB_CHECK_CODE(code, lino, _err);
6,584,826✔
854
      break;
6,582,360✔
855
    case TDMT_VND_ALTER_STB:
8,975,080✔
856
      code = vnodeProcessAlterStbReq(pVnode, ver, pReq, len, pRsp);
8,975,080✔
857
      TSDB_CHECK_CODE(code, lino, _err);
8,973,020✔
858
      break;
8,973,020✔
859
    case TDMT_VND_DROP_STB:
1,048,581✔
860
      code = vnodeProcessDropStbReq(pVnode, ver, pReq, len, pRsp, pMsg);
1,048,581✔
861
      TSDB_CHECK_CODE(code, lino, _err);
1,048,581✔
862
      break;
1,048,581✔
863
    case TDMT_VND_CREATE_TABLE:
53,382,560✔
864
      code = vnodeProcessCreateTbReq(pVnode, ver, pReq, len, pRsp, pMsg);
53,382,560✔
865
      TSDB_CHECK_CODE(code, lino, _err);
53,373,098✔
866
      break;
53,373,098✔
867
    case TDMT_VND_ALTER_TABLE:
13,663,757✔
868
      code = vnodeProcessAlterTbReq(pVnode, ver, pReq, len, pRsp);
13,663,757✔
869
      TSDB_CHECK_CODE(code, lino, _err);
13,663,757✔
870
      break;
13,663,757✔
871
    case TDMT_VND_DROP_TABLE:
1,831,901✔
872
    case TDMT_VND_SNODE_DROP_TABLE:
873
      code = vnodeProcessDropTbReq(pVnode, ver, pReq, len, pRsp, pMsg);
1,831,901✔
874
      TSDB_CHECK_CODE(code, lino, _err);
1,831,901✔
875
      break;
1,831,901✔
876
    case TDMT_VND_CREATE_RSMA:
44,420✔
877
      code = vnodeProcessCreateRsmaReq(pVnode, ver, pReq, len, pRsp);
44,420✔
878
      TSDB_CHECK_CODE(code, lino, _err);
44,420✔
879
      break;
44,420✔
880
    case TDMT_VND_DROP_RSMA:
22,204✔
881
      code = vnodeProcessDropRsmaReq(pVnode, ver, pReq, len, pRsp);
22,204✔
882
      TSDB_CHECK_CODE(code, lino, _err);
22,204✔
883
      break;
22,204✔
884
    case TDMT_VND_ALTER_RSMA:
13,586✔
885
      code = vnodeProcessAlterRsmaReq(pVnode, ver, pReq, len, pRsp);
13,586✔
886
      TSDB_CHECK_CODE(code, lino, _err);
13,586✔
887
      break;
13,586✔
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,185✔
893
      code = vnodeProcessFetchTtlExpiredTbs(pVnode, ver, pReq, len, pRsp);
13,185✔
894
      TSDB_CHECK_CODE(code, lino, _err);
13,185✔
895
      break;
13,185✔
896
    case TDMT_VND_TRIM:
80,005✔
897
      code = vnodeProcessTrimReq(pVnode, ver, pReq, len, pRsp);
80,005✔
898
      TSDB_CHECK_CODE(code, lino, _err);
80,203✔
899
      break;
80,203✔
900
      case TDMT_VND_TRIM_WAL:
7,616✔
901
        needCommit = true;
7,616✔
902
        forceTrimWal = true;
7,616✔
903
        break;
7,616✔
904
#ifdef TD_ENTERPRISE
905
    case TDMT_VND_SSMIGRATE_FILESET:
4,140✔
906
      if (vnodeProcessSsMigrateFileSetReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
4,140✔
907
      break;
4,140✔
908
    case TDMT_VND_FOLLOWER_SSMIGRATE:
6,624✔
909
      if (vnodeProcessFollowerSsMigrateReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
6,624✔
910
      break;
6,624✔
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,366,079,965✔
918
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.apply_time, METRIC_LEVEL_LOW, {
1,366,079,965✔
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,366,029,400✔
922
      break;
1,366,011,932✔
923
    }
924
    case TDMT_VND_DELETE:
2,435,855✔
925
      code = vnodeProcessDeleteReq(pVnode, ver, pReq, len, pRsp, pMsg);
2,435,855✔
926
      TSDB_CHECK_CODE(code, lino, _err);
2,435,855✔
927
      break;
2,435,855✔
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,433,685✔
935
      code = tqProcessSubscribeReq(pVnode->pTq, ver, pReq, len);
1,433,685✔
936
      TSDB_CHECK_CODE(code, lino, _err);
1,427,848✔
937
      break;
1,427,848✔
938
    case TDMT_VND_TMQ_DELETE_SUB:
340,764✔
939
      code = tqProcessDeleteSubReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen);
340,764✔
940
      TSDB_CHECK_CODE(code, lino, _err);
338,066✔
941
      break;
338,066✔
942
    case TDMT_VND_TMQ_COMMIT_OFFSET:
11,651,866✔
943
      code = tqProcessOffsetCommitReq(pVnode->pTq, ver, pReq, len);
11,651,866✔
944
      TSDB_CHECK_CODE(code, lino, _err);
11,650,744✔
945
      break;
11,650,744✔
946
#endif
947
    case TDMT_VND_ALTER_CONFIRM:
6,010,948✔
948
      needCommit = pVnode->config.hashChange;
6,010,948✔
949
      code = vnodeProcessAlterConfirmReq(pVnode, ver, pReq, len, pRsp);
6,010,948✔
950
      TSDB_CHECK_CODE(code, lino, _err);
6,010,948✔
951
      break;
6,010,948✔
952
    case TDMT_VND_ALTER_CONFIG:
357,444✔
953
      vnodeProcessAlterConfigReq(pVnode, ver, pReq, len, pRsp);
357,444✔
954
      break;
357,444✔
955
    case TDMT_VND_COMMIT:
5,828,548✔
956
      needCommit = true;
5,828,548✔
957
      break;
5,828,548✔
958
    case TDMT_VND_CREATE_INDEX:
20,489✔
959
      vnodeProcessCreateIndexReq(pVnode, ver, pReq, len, pRsp);
20,489✔
960
      break;
20,489✔
961
    case TDMT_VND_DROP_INDEX:
16,636✔
962
      vnodeProcessDropIndexReq(pVnode, ver, pReq, len, pRsp);
16,636✔
963
      break;
16,636✔
964
    case TDMT_VND_COMPACT:
81,222✔
965
      vnodeProcessCompactVnodeReq(pVnode, ver, pReq, len, pRsp);
81,222✔
966
      goto _exit;
81,222✔
967
    case TDMT_VND_SCAN:
670✔
968
      vnodeProcessScanVnodeReq(pVnode, ver, pReq, len, pRsp);
670✔
969
      goto _exit;
670✔
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,642✔
975
      vnodeProcessKillCompactReq(pVnode, ver, pReq, len, pRsp);
1,642✔
976
      break;
1,642✔
977
#endif
978
    case TDMT_VND_KILL_SCAN:
128✔
979
      vnodeProcessKillScanReq(pVnode, ver, pReq, len, pRsp);
128✔
980
      break;
128✔
981
    case TDMT_VND_KILL_TRIM:
×
982
      vnodeProcessKillRetentionReq(pVnode, ver, pReq, len, pRsp);
×
983
      break;
×
984
    /* ARB */
985
    case TDMT_VND_ARB_CHECK_SYNC:
32,332✔
986
      vnodeProcessArbCheckSyncReq(pVnode, pReq, len, pRsp);
32,332✔
987
      break;
32,332✔
988
    case TDMT_VND_AUDIT_RECORD:
664✔
989
      vTrace("vgId:%d, processed audit msg", TD_VID(pVnode));
664✔
990
      code = vnodeProcessAuditRecordReq(pVnode, ver, pReq, len, pMsg);
664✔
991
      pRsp->code = code;
664✔
992
      pRsp->msgType = TDMT_VND_AUDIT_RECORD_RSP;
664✔
993
      pRsp->pCont = NULL;
664✔
994
      pRsp->contLen = 0;
664✔
995
      if (code) {
664✔
996
        goto _err;
×
997
      }
998
      break;
664✔
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,479,797,971✔
1005

1006
  walApplyVer(pVnode->pWal, ver);
1,479,846,761✔
1007

1008
  // commit if need
1009
  if (needCommit) {
1,479,885,902✔
1010
    vInfo("vgId:%d, commit at version %" PRId64, TD_VID(pVnode), ver);
5,863,805✔
1011
    code = vnodeAsyncCommit(pVnode, forceTrimWal);
5,864,464✔
1012
    if (code) {
5,865,128✔
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,865,128✔
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,479,875,797✔
1028
  return 0;
1,494,522,527✔
1029

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

1042
int32_t vnodePreprocessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg) {
805,175,228✔
1043
  if (TDMT_SCH_QUERY != pMsg->msgType && TDMT_SCH_MERGE_QUERY != pMsg->msgType) {
805,175,228✔
1044
    return 0;
397,516,970✔
1045
  }
1046

1047
  int32_t qType = 0;
407,774,538✔
1048
  return qWorkerPreprocessQueryMsg(pVnode->pQuery, pMsg, (TDMT_SCH_QUERY == pMsg->msgType), &qType);
407,761,285✔
1049
}
1050

1051
int32_t vnodeProcessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
805,289,911✔
1052
  const STraceId *trace = &pMsg->info.traceId;
805,289,911✔
1053
  vDebug("vgId:%d, msg:%s, %p in vnode query queue is processing, %" PRIx64 ":%" PRIx64, 
805,308,477✔
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)) {
805,435,782✔
1057
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
752,009✔
1058
    return 0;
752,009✔
1059
  }
1060

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

1066
  SReadHandle handle = {0};
804,692,954✔
1067
  handle.vnode = pVnode;
804,694,044✔
1068
  handle.pMsgCb = &pVnode->msgCb;
804,694,044✔
1069
  handle.pWorkerCb = pInfo->workerCb;
804,599,579✔
1070
  initStorageAPI(&handle.api);
804,597,337✔
1071
  int32_t code = TSDB_CODE_SUCCESS;
804,365,244✔
1072
  bool    redirected = false;
804,365,244✔
1073

1074
  switch (pMsg->msgType) {
804,365,244✔
1075
    case TDMT_SCH_QUERY:
338,156,151✔
1076
      if (!syncIsReadyForRead(pVnode->sync)) {
338,156,151✔
1077
        pMsg->code = (terrno) ? terrno : TSDB_CODE_SYN_NOT_LEADER;
2,301,980✔
1078
        redirected = true;
2,301,980✔
1079
      }
1080
      code = qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
338,241,944✔
1081
      if (redirected) {
338,223,686✔
1082
        vnodeRedirectRpcMsg(pVnode, pMsg, pMsg->code);
2,301,980✔
1083
        return 0;
2,301,980✔
1084
      }
1085

1086
      return code;
335,921,706✔
1087
    case TDMT_SCH_MERGE_QUERY:
69,576,678✔
1088
      return qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
69,576,678✔
1089
    case TDMT_SCH_QUERY_CONTINUE:
76,295,423✔
1090
      return qWorkerProcessCQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
76,295,423✔
1091
    case TDMT_VND_TMQ_CONSUME:
320,453,148✔
1092
      return tqProcessPollReq(pVnode->pTq, pMsg);
320,453,148✔
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,243,816,928✔
1100
  int32_t code = TSDB_CODE_SUCCESS;
1,243,816,928✔
1101
  const STraceId *trace = &pMsg->info.traceId;
1,243,816,928✔
1102
  vDebug("vgId:%d, msg:%s, %p in fetch queue is processing, %" PRIx64 ":%" PRIx64, 
1,243,895,502✔
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,244,115,736✔
1105
       pMsg->msgType == TDMT_VND_BATCH_META || pMsg->msgType == TDMT_VND_TABLE_NAME ||
851,174,824✔
1106
       pMsg->msgType == TDMT_VND_VSUBTABLES_META || pMsg->msgType == TDMT_VND_VSTB_REF_DBS) &&
816,961,959✔
1107
      !syncIsReadyForRead(pVnode->sync)) {
427,121,322✔
1108
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
412,552✔
1109
    return 0;
412,552✔
1110
  }
1111

1112
  switch (pMsg->msgType) {
1,243,714,880✔
1113
    case TDMT_SCH_FETCH:
596,122,710✔
1114
    case TDMT_SCH_MERGE_FETCH:
1115
      return qWorkerProcessFetchMsg(pVnode, pVnode->pQuery, pMsg, 0);
596,122,710✔
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:
407,583,500✔
1121
      return qWorkerProcessDropMsg(pVnode, pVnode->pQuery, pMsg, 0);
407,583,500✔
1122
    case TDMT_SCH_TASK_NOTIFY:
32,327✔
1123
      return qWorkerProcessNotifyMsg(pVnode, pVnode->pQuery, pMsg, 0);
32,327✔
1124
    case TDMT_SCH_QUERY_HEARTBEAT:
204,572,681✔
1125
      return qWorkerProcessHbMsg(pVnode, pVnode->pQuery, pMsg, 0);
204,572,681✔
1126
    case TDMT_VND_TABLE_META:
1,414,613✔
1127
    case TDMT_VND_TABLE_NAME:
1128
      return vnodeGetTableMeta(pVnode, pMsg, true);
1,414,613✔
1129
    case TDMT_VND_TABLE_CFG:
×
1130
      return vnodeGetTableCfg(pVnode, pMsg, true);
×
1131
    case TDMT_VND_BATCH_META: {
33,783,295✔
1132
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.fetch_batch_meta_time, METRIC_LEVEL_LOW,
33,783,295✔
1133
                           { code = vnodeGetBatchMeta(pVnode, pMsg); });
1134
      METRICS_UPDATE(pVnode->writeMetrics.fetch_batch_meta_count, METRIC_LEVEL_LOW, 1);
33,793,687✔
1135
      return code;
33,782,575✔
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:
640✔
1142
      return vnodeQueryScanProgress(pVnode, pMsg);
640✔
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,968✔
1148
      return vnodeListSsMigrateFileSets(pVnode, pMsg);
4,968✔
1149

1150
    case TDMT_VND_QUERY_SSMIGRATE_PROGRESS:
6,624✔
1151
      return vnodeQuerySsMigrateProgress(pVnode, pMsg);
6,624✔
1152
#endif
1153
    case TDMT_VND_QUERY_TRIM_PROGRESS:
112,515✔
1154
      return vnodeQueryRetentionProgress(pVnode, pMsg);
112,515✔
1155
      //    case TDMT_VND_TMQ_CONSUME:
1156
      //      return tqProcessPollReq(pVnode->pTq, pMsg);
1157
#ifdef USE_TQ
1158
    case TDMT_VND_TMQ_VG_WALINFO:
712✔
1159
      return tqProcessVgWalInfoReq(pVnode->pTq, pMsg);
712✔
1160
    case TDMT_VND_TMQ_VG_COMMITTEDINFO:
356✔
1161
      return tqProcessVgCommittedInfoReq(pVnode->pTq, pMsg);
356✔
1162
#endif
1163
    default:
51,612✔
1164
      vError("unknown msg type:%d in fetch queue", pMsg->msgType);
51,612✔
1165
      return TSDB_CODE_APP_ERROR;
×
1166
  }
1167
}
1168

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

1174
  tstrncpy(pMetaRsp->dbFName, pVnode->config.dbname, TSDB_DB_FNAME_LEN);
84,480,346✔
1175
  pMetaRsp->dbId = pVnode->config.dbId;
84,480,760✔
1176
  pMetaRsp->vgId = TD_VID(pVnode);
84,479,975✔
1177
  pMetaRsp->precision = pVnode->config.tsdbCfg.precision;
84,481,101✔
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) {
80,005✔
1183
  if (!pVnode->restored) {
80,005✔
1184
    vInfo("vgId:%d, ignore trim req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
3,576✔
1185
    return 0;
3,576✔
1186
  }
1187

1188
  int32_t     code = 0;
76,429✔
1189
  SVTrimDbReq trimReq = {0};
76,429✔
1190

1191
  // decode
1192
  if (tDeserializeSVTrimDbReq(pReq, len, &trimReq) != 0) {
76,429✔
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,
76,429✔
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);
76,627✔
1201

1202
_exit:
76,627✔
1203
  return code;
76,627✔
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,140✔
1211
  int32_t code = 0;
4,140✔
1212

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

1220
  if (tDeserializeSSsMigrateFileSetReq(pReq, len, &req) != 0) {
4,140✔
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,140✔
1226
        req.ssMigrateId, req.fid, req.startTimeSec);
1227
  rsp.ssMigrateId = req.ssMigrateId;
4,140✔
1228
  rsp.vgId = TD_VID(pVnode);
4,140✔
1229
  rsp.nodeId = req.nodeId;
4,140✔
1230
  rsp.fid = req.fid;
4,140✔
1231

1232
  code = vnodeAsyncSsMigrateFileSet(pVnode, &req);
4,140✔
1233
  if (code != TSDB_CODE_SUCCESS) {
4,140✔
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,140✔
1240
  pRsp->contLen = tSerializeSSsMigrateFileSetRsp(NULL, 0, &rsp);
4,140✔
1241
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
4,140✔
1242
  if (pRsp->pCont == NULL) {
4,140✔
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,140✔
1248

1249
_exit:
4,140✔
1250
  pRsp->code = code;
4,140✔
1251
  return code;
4,140✔
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) {
6,624✔
1257
  int32_t            code = 0;
6,624✔
1258
  SSsMigrateProgress req = {0};
6,624✔
1259

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

1266
  code = vnodeFollowerSsMigrate(pVnode, &req);
6,624✔
1267

1268
_exit:
6,624✔
1269
  pRsp->code = code;
6,624✔
1270
  return code;
6,624✔
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,185✔
1326
  int32_t                 code = -1;
13,185✔
1327
  SMetaReader             mr = {0};
13,185✔
1328
  SVDropTtlTableReq       ttlReq = {0};
13,185✔
1329
  SVFetchTtlExpiredTbsRsp rsp = {0};
13,185✔
1330
  SEncoder                encoder = {0};
13,185✔
1331
  SArray                 *pNames = NULL;
13,185✔
1332
  pRsp->msgType = TDMT_VND_FETCH_TTL_EXPIRED_TBS_RSP;
13,185✔
1333
  pRsp->code = TSDB_CODE_SUCCESS;
13,185✔
1334
  pRsp->pCont = NULL;
13,185✔
1335
  pRsp->contLen = 0;
13,185✔
1336

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

1342
  if (!(ttlReq.nUids == taosArrayGetSize(ttlReq.pTbUids))) {
13,185✔
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,185✔
1350
  metaReaderDoInit(&mr, pVnode->pMeta, 0);
13,185✔
1351
  rsp.vgId = TD_VID(pVnode);
13,185✔
1352
  rsp.pExpiredTbs = taosArrayInit(ttlReq.nUids, sizeof(SVDropTbReq));
13,185✔
1353
  if (!rsp.pExpiredTbs) goto _end;
13,185✔
1354

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

1372
    expiredTb.name = p;
54,202✔
1373
    if (mr.me.type == TSDB_CHILD_TABLE) {
54,202✔
1374
      expiredTb.suid = mr.me.ctbEntry.suid;
35,178✔
1375
    }
1376

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

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

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

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

1409
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
6,584,718✔
1410
  pRsp->code = TSDB_CODE_SUCCESS;
6,584,594✔
1411
  pRsp->pCont = NULL;
6,584,594✔
1412
  pRsp->contLen = 0;
6,582,231✔
1413

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

1417
  code = tDecodeSVCreateStbReq(&coder, &req);
6,582,752✔
1418
  if (code) {
6,550,871✔
1419
    pRsp->code = code;
×
1420
    goto _err;
×
1421
  }
1422

1423
  code = metaCreateSuperTable(pVnode->pMeta, ver, &req);
6,550,871✔
1424
  if (code) {
6,585,711✔
1425
    pRsp->code = code;
2,466✔
1426
    goto _err;
2,466✔
1427
  }
1428

1429
  tDecoderClear(&coder);
6,583,245✔
1430
  return 0;
6,582,814✔
1431

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

1437
static int32_t vnodeProcessCreateTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
53,381,989✔
1438
                                       SRpcMsg *pOriginRpc) {
1439
  SDecoder           decoder = {0};
53,381,989✔
1440
  SEncoder           encoder = {0};
53,382,935✔
1441
  int32_t            rcode = 0;
53,382,560✔
1442
  SVCreateTbBatchReq req = {0};
53,382,560✔
1443
  SVCreateTbReq     *pCreateReq;
1444
  SVCreateTbBatchRsp rsp = {0};
53,382,564✔
1445
  SVCreateTbRsp      cRsp = {0};
53,382,878✔
1446
  char               tbName[TSDB_TABLE_FNAME_LEN];
53,379,737✔
1447
  SArray            *tbUids = NULL;
53,382,935✔
1448
  SArray            *tbNames = NULL;
53,382,935✔
1449
  int64_t            tss = taosGetTimestampMs();
53,382,507✔
1450
  pRsp->msgType = TDMT_VND_CREATE_TABLE_RSP;
53,382,507✔
1451
  pRsp->code = TSDB_CODE_SUCCESS;
53,382,507✔
1452
  pRsp->pCont = NULL;
53,382,089✔
1453
  pRsp->contLen = 0;
53,382,564✔
1454

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

1463
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(cRsp));
53,374,591✔
1464
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
53,366,053✔
1465
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
53,374,763✔
1466
  if (rsp.pArray == NULL || tbUids == NULL || tbNames == NULL) {
53,371,655✔
1467
    rcode = -1;
2,620✔
1468
    terrno = TSDB_CODE_OUT_OF_MEMORY;
2,620✔
1469
    goto _exit;
×
1470
  }
1471

1472
  // loop to create table
1473
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
115,869,610✔
1474
    pCreateReq = req.pReqs + iReq;
62,490,737✔
1475
    memset(&cRsp, 0, sizeof(cRsp));
62,483,616✔
1476

1477
    if (tsEnableAudit && tsEnableAuditCreateTable && tsAuditLevel >= AUDIT_LEVEL_CHILDTABLE) {
62,483,616✔
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);
62,483,616✔
1494
    if (vnodeValidateTableHash(pVnode, tbName) < 0) {
62,489,244✔
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) {
62,476,166✔
1507
      if (pCreateReq->flags & TD_CREATE_IF_NOT_EXISTS && terrno == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
18,626✔
1508
        cRsp.code = TSDB_CODE_SUCCESS;
5,218✔
1509
      } else {
1510
        cRsp.code = terrno;
13,408✔
1511
      }
1512
    } else {
1513
      cRsp.code = TSDB_CODE_SUCCESS;
62,480,405✔
1514
      if (taosArrayPush(tbUids, &pCreateReq->uid) == NULL) {
124,960,383✔
1515
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1516
        rcode = -1;
×
1517
        goto _exit;
×
1518
      }
1519
      vnodeUpdateMetaRsp(pVnode, cRsp.pMeta);
62,479,978✔
1520
    }
1521

1522
    if (taosArrayPush(rsp.pArray, &cRsp) == NULL) {
124,997,675✔
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));
53,378,873✔
1530
  if (tqAddTbUidListForQuerySub(pVnode->pTq, tbUids) < 0) {
53,378,873✔
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;
53,382,935✔
1536
  tEncodeSize(tEncodeSVCreateTbBatchRsp, &rsp, pRsp->contLen, ret);
53,382,935✔
1537
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
53,377,312✔
1538
  if (pRsp->pCont == NULL) {
53,373,508✔
1539
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1540
    rcode = -1;
×
1541
    goto _exit;
×
1542
  }
1543
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
53,370,062✔
1544
  if (tEncodeSVCreateTbBatchRsp(&encoder, &rsp) != 0) {
53,368,797✔
1545
    vError("vgId:%d, failed to encode create table batch response", TD_VID(pVnode));
×
1546
  }
1547

1548
  if (tsEnableAudit && tsEnableAuditCreateTable && tsAuditLevel >= AUDIT_LEVEL_CHILDTABLE) {
53,364,688✔
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:
53,364,688✔
1580
  tDeleteSVCreateTbBatchReq(&req);
53,369,929✔
1581
  taosArrayDestroyEx(rsp.pArray, tFreeSVCreateTbRsp);
53,374,291✔
1582
  taosArrayDestroy(tbUids);
53,360,723✔
1583
  tDecoderClear(&decoder);
53,360,590✔
1584
  tEncoderClear(&encoder);
53,379,534✔
1585
  taosArrayDestroyP(tbNames, NULL);
53,375,168✔
1586
  return rcode;
53,370,007✔
1587
}
1588

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

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

1599
  tDecoderInit(&dc, pReq, len);
8,975,080✔
1600

1601
  // decode req
1602
  code = tDecodeSVCreateStbReq(&dc, &req);
8,974,349✔
1603
  if (code) {
8,957,044✔
1604
    tDecoderClear(&dc);
×
1605
    return code;
×
1606
  }
1607

1608
  code = metaAlterSuperTable(pVnode->pMeta, ver, &req);
8,957,044✔
1609
  if (code) {
8,974,142✔
1610
    pRsp->code = code;
×
1611
    tDecoderClear(&dc);
×
1612
    return code;
×
1613
  }
1614

1615
  tDecoderClear(&dc);
8,974,142✔
1616

1617
  return 0;
8,974,428✔
1618
}
1619

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

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

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

1637
  STraceId* trace = &(pOriginRpc->info.traceId);
1,048,083✔
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,048,083✔
1640
              trace ? trace->msgId : 0, req.name);
1641

1642
  // process request
1643
  tbUidList = taosArrayInit(8, sizeof(int64_t));
1,048,581✔
1644
  if (tbUidList == NULL) goto _exit;
1,047,926✔
1645
  if (vnodeGetCtbIdList(pVnode, req.suid, tbUidList) != 0){
1,047,926✔
1646
    rcode = terrno;
×
1647
    goto _exit;
×
1648
  }
1649
  if (metaDropSuperTable(pVnode->pMeta, ver, &req) < 0) {
1,045,432✔
1650
    rcode = terrno;
2,092✔
1651
    goto _exit;
2,092✔
1652
  }
1653
  if (tqDeleteTbUidList(pVnode->pTq, tbUidList) < 0) {
1,045,664✔
1654
    rcode = terrno;
×
1655
    goto _exit;
×
1656
  }
1657

1658
  // return rsp
1659
_exit:
1,048,581✔
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,048,581✔
1661
              trace ? trace->msgId : 0, req.name);
1662
  if (tbUidList) taosArrayDestroy(tbUidList);
1,048,581✔
1663
  pRsp->code = rcode;
1,048,581✔
1664
  tDecoderClear(&decoder);
1,048,581✔
1665
  return 0;
1,048,581✔
1666
}
1667

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

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

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

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

1698
  if (tagsArray != NULL) {
8,572,655✔
1699
    *cidListArray = taosArrayInit(taosArrayGetSize(tagsArray), sizeof(SArray*));
8,545,021✔
1700
    QUERY_CHECK_NULL(*cidListArray, code, lino, end, terrno);
8,545,021✔
1701

1702
    for (int32_t i = 0; i < taosArrayGetSize(tagsArray); i++) {
17,105,542✔
1703
      SArray   *obj = taosArrayGetP(tagsArray, i);
8,560,521✔
1704
      cids  = getCidList(obj);
8,560,521✔
1705
      QUERY_CHECK_NULL(cids, code, lino, end, terrno);
8,560,521✔
1706
      QUERY_CHECK_NULL(taosArrayPush(*cidListArray, &cids), code, lino, end, terrno);
17,121,042✔
1707
      cids = NULL;
8,560,521✔
1708
    }
1709
  }
1710

1711
end:
8,572,655✔
1712
  taosArrayDestroy(cids);
8,572,655✔
1713
  return code;
8,572,655✔
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,565,865✔
1718
  int32_t       code = 0;
8,565,865✔
1719
  int32_t       lino = 0;
8,565,865✔
1720
  SArray*       cidList = NULL;
8,565,865✔
1721
  SArray*       cidListArray = NULL;
8,565,865✔
1722

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

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

1731
end:
8,565,865✔
1732
  if (code != 0) {
8,565,865✔
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,565,865✔
1737
  taosArrayDestroyP(cidListArray, (FDelete)taosArrayDestroy);
8,565,865✔
1738
}
8,565,865✔
1739

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

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

1755
  tDecoderInit(&dc, pReq, len);
13,663,757✔
1756

1757
  // decode
1758
  if (tDecodeSVAlterTbReq(&dc, &vAlterTbReq) < 0) {
13,663,757✔
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,663,757✔
1766
    vAlterTbRsp.code = terrno;
651,430✔
1767
    tDecoderClear(&dc);
651,430✔
1768
    goto _exit;
651,430✔
1769
  }
1770
  tDecoderClear(&dc);
13,012,327✔
1771

1772
  if (NULL != vMetaRsp.pSchemas) {
13,012,327✔
1773
    vnodeUpdateMetaRsp(pVnode, &vMetaRsp);
4,218,434✔
1774
    vAlterTbRsp.pMeta = &vMetaRsp;
4,218,434✔
1775
  }
1776

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

1786
  tEncoderClear(&ec);
13,663,757✔
1787
  if (vMetaRsp.pSchemas) {
13,663,757✔
1788
    taosMemoryFree(vMetaRsp.pSchemas);
4,218,434✔
1789
    taosMemoryFree(vMetaRsp.pSchemaExt);
4,218,434✔
1790
  }
1791
  if (vMetaRsp.pColRefs) {
13,663,757✔
1792
    taosMemoryFree(vMetaRsp.pColRefs);
310,990✔
1793
  }
1794
  return 0;
13,663,757✔
1795
}
1796

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

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

1813
  // decode req
1814
  tDecoderInit(&decoder, pReq, len);
1,831,901✔
1815
  ret = tDecodeSVDropTbBatchReq(&decoder, &req);
1,831,901✔
1816
  if (ret < 0) {
1,831,901✔
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,831,901✔
1824
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(SVDropTbRsp));
1,831,901✔
1825
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
1,831,901✔
1826
  if (tbUids == NULL || rsp.pArray == NULL || tbNames == NULL) goto _exit;
1,831,901✔
1827

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

1833
    /* code */
1834
    ret = metaDropTable2(pVnode->pMeta, ver, pDropTbReq);
1,892,310✔
1835
    if (ret < 0) {
1,892,310✔
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,892,310✔
1843
    }
1844

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

1851
    if (taosArrayPush(tbUids, &pDropTbReq->uid) == NULL) {
3,784,620✔
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,892,310✔
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,831,901✔
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,831,901✔
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,831,901✔
1908
  taosArrayDestroy(tbUids);
1,831,901✔
1909
  tDecoderClear(&decoder);
1,831,901✔
1910
  tEncodeSize(tEncodeSVDropTbBatchRsp, &rsp, pRsp->contLen, ret);
1,831,901✔
1911
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
1,831,901✔
1912
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
1,831,901✔
1913
  if (tEncodeSVDropTbBatchRsp(&encoder, &rsp) != 0) {
1,831,901✔
1914
    vError("vgId:%d, failed to encode drop table batch response", TD_VID(pVnode));
×
1915
  }
1916
  tEncoderClear(&encoder);
1,831,901✔
1917
  taosArrayDestroy(rsp.pArray);
1,831,901✔
1918
  taosArrayDestroy(tbNames);
1,831,901✔
1919
  return 0;
1,831,901✔
1920
}
1921

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

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

1949
static int32_t vnodeProcessAlterRsmaReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
13,586✔
1950
  int32_t         code = 0, lino = 0;
13,586✔
1951
  SVAlterRsmaReq req = {0};
13,586✔
1952
  TAOS_CHECK_EXIT(tDeserializeSVAlterRsmaReq(pReq, len, &req));
13,586✔
1953
  TAOS_CHECK_EXIT(metaAlterRsma(pVnode->pMeta, ver, &req));
13,586✔
1954
_exit:
13,586✔
1955
  pRsp->msgType = TDMT_VND_ALTER_RSMA_RSP;
13,586✔
1956
  pRsp->pCont = NULL;
13,586✔
1957
  pRsp->code = code;
13,586✔
1958
  pRsp->contLen = 0;
13,586✔
1959
  tFreeSVAlterRsmaReq(&req);
13,586✔
1960
  return code;
13,586✔
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) {
86,169✔
2188
  int32_t code = 0;
86,169✔
2189
  int32_t lino = 0;
86,169✔
2190

2191
  SMetaEntry *pEntry = NULL;
86,169✔
2192
  code = metaFetchEntryByUid(pVnode->pMeta, pSubmitTbData->suid, &pEntry);
86,169✔
2193
  if (code) {
86,169✔
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) {
86,169✔
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));
86,169✔
2205
  if (NULL == *ppRsp) {
86,169✔
2206
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2207
  }
2208
  (*ppRsp)->suid = pSubmitTbData->suid;
86,169✔
2209
  (*ppRsp)->tuid = pSubmitTbData->uid;
86,169✔
2210
  (*ppRsp)->sversion = pEntry->stbEntry.schemaRow.version;
86,169✔
2211
  (*ppRsp)->vgId = pVnode->config.vgId;
86,169✔
2212
  (*ppRsp)->numOfColumns = pEntry->stbEntry.schemaRow.nCols;
86,169✔
2213
  (*ppRsp)->numOfTags = pEntry->stbEntry.schemaTag.nCols;
86,169✔
2214
  (*ppRsp)->pSchemas =
86,169✔
2215
      taosMemoryCalloc(pEntry->stbEntry.schemaRow.nCols + pEntry->stbEntry.schemaTag.nCols, sizeof(SSchema));
86,169✔
2216
  if (NULL == (*ppRsp)->pSchemas) {
86,169✔
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));
86,169✔
2222
  memcpy((*ppRsp)->pSchemas + pEntry->stbEntry.schemaRow.nCols, pEntry->stbEntry.schemaTag.pSchema,
86,169✔
2223
         pEntry->stbEntry.schemaTag.nCols * sizeof(SSchema));
86,169✔
2224
  if (pEntry->pExtSchemas != NULL) {
86,169✔
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) {
86,169✔
2236
    TSDB_CHECK_CODE(code = TSDB_CODE_TDB_TABLE_ALREADY_EXIST, lino, _exit);
1,064✔
2237
  }
2238
_exit:
86,169✔
2239
  metaFetchEntryFree(&pEntry);
86,169✔
2240
  if (code != TSDB_CODE_SUCCESS) {
86,169✔
2241
    vError("vgId:%d, failed to build exist sub table response, code:0x%0x, line:%d", TD_VID(pVnode), code, lino);
1,064✔
2242
  }
2243
  return code;
86,169✔
2244
}
2245

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

2250
  SMetaEntry *pEntry = NULL;
53,230✔
2251
  code = metaFetchEntryByUid(pVnode->pMeta, uid, &pEntry);
53,230✔
2252
  if (code) {
53,230✔
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) {
53,230✔
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));
53,230✔
2263
  if (NULL == *ppRsp) {
53,230✔
2264
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2265
  }
2266

2267
  (*ppRsp)->tuid = pEntry->uid;
53,230✔
2268
  (*ppRsp)->vgId = pVnode->config.vgId;
53,230✔
2269
  (*ppRsp)->sversion = pEntry->ntbEntry.schemaRow.version;
53,230✔
2270
  (*ppRsp)->numOfColumns = pEntry->ntbEntry.schemaRow.nCols;
53,230✔
2271
  (*ppRsp)->pSchemas = taosMemoryCalloc(pEntry->ntbEntry.schemaRow.nCols, sizeof(SSchema));
53,230✔
2272
  if (NULL == (*ppRsp)->pSchemas) {
53,230✔
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));
53,230✔
2277
  if (pEntry->pExtSchemas != NULL) {
53,230✔
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:
53,230✔
2288
  metaFetchEntryFree(&pEntry);
53,230✔
2289
  if (code != TSDB_CODE_SUCCESS) {
53,230✔
2290
    vError("vgId:%d, failed to build exist normal table response, code:%d, line:%d", TD_VID(pVnode), code, lino);
×
2291
  }
2292
  return code;
53,230✔
2293
}
2294

2295
static int32_t buildExistTableInStreamRsp(SVnode *pVnode, SSubmitTbData *pSubmitTbData, SVCreateTbRsp *pCreateTbRsp) {
447,311✔
2296
  int32_t code = TSDB_CODE_SUCCESS;
447,311✔
2297
  if (pSubmitTbData->pCreateTbReq->flags & TD_CREATE_NORMAL_TB_IN_STREAM) {
447,311✔
2298
    code = buildExistNormalTalbeRsp(pVnode, pSubmitTbData->uid, &pCreateTbRsp->pMeta);
53,230✔
2299
    if (code) {
53,230✔
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;
53,230✔
2304
    return TSDB_CODE_SUCCESS;
53,230✔
2305
  } else if (pSubmitTbData->pCreateTbReq->flags & TD_CREATE_SUB_TB_IN_STREAM) {
394,140✔
2306
    return buildExistSubTalbeRsp(pVnode, pSubmitTbData, &pCreateTbRsp->pMeta);
85,637✔
2307
  }
2308
  return TSDB_CODE_SUCCESS;
308,444✔
2309
}
2310

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

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

2323
    if (pTbData->pCreateTbReq == NULL) {
1,571,359,501✔
2324
      continue;
1,553,127,832✔
2325
    }
2326

2327
    pTbData->uid = pTbData->pCreateTbReq->uid;
18,231,492✔
2328

2329
    // Alloc necessary resources
2330
    if (pResponse->aCreateTbRsp == NULL) {
18,231,343✔
2331
      pResponse->aCreateTbRsp = taosArrayInit(numTbData, sizeof(SVCreateTbRsp));
14,995,894✔
2332
      if (pResponse->aCreateTbRsp == NULL) {
14,995,953✔
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);
18,231,610✔
2343

2344
    SVCreateTbRsp *pCreateTbRsp = taosArrayReserve(pResponse->aCreateTbRsp, 1);
18,232,109✔
2345
    pCreateTbRsp->index = i;
18,231,669✔
2346
    code = metaCreateTable2(pVnode->pMeta, version, pTbData->pCreateTbReq, &pCreateTbRsp->pMeta);
18,231,669✔
2347
    if (code == TSDB_CODE_SUCCESS) {
18,231,551✔
2348
      // Allocate necessary resources
2349
      if (newTbUids == NULL) {
17,783,058✔
2350
        newTbUids = taosArrayInit(numTbData, sizeof(int64_t));
14,623,872✔
2351
        if (newTbUids == NULL) {
14,623,872✔
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) {
35,566,116✔
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,783,058✔
2368
        vnodeUpdateMetaRsp(pVnode, pCreateTbRsp->pMeta);
17,782,684✔
2369
      }
2370
    } else if (code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
448,493✔
2371
      code = terrno = 0;
447,311✔
2372
      pTbData->uid = pTbData->pCreateTbReq->uid;  // update uid if table exist for using below
447,370✔
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);
447,370✔
2376
      if (code) {
447,193✔
2377
        vInfo("vgId:%d failed to create table in stream:%s, code(0x%0x):%s", TD_VID(pVnode),
532✔
2378
              pTbData->pCreateTbReq->name, code, tstrerror(code));
2379
        taosArrayDestroy(newTbUids);
532✔
2380
        return code;
532✔
2381
      }
2382
    } else {
2383
      code = terrno;
1,182✔
2384
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
1,182✔
2385
             tstrerror(code), version);
2386
      taosArrayDestroy(newTbUids);
1,182✔
2387
      return code;
1,182✔
2388
    }
2389
  }
2390

2391
  // Update the affected table uid list
2392
  if (taosArrayGetSize(newTbUids) > 0) {
1,366,080,972✔
2393
    vDebug("vgId:%d, add %d table into query table list in handling submit", TD_VID(pVnode),
14,623,872✔
2394
           (int32_t)taosArrayGetSize(newTbUids));
2395
    if (tqAddTbUidListForQuerySub(pVnode->pTq, newTbUids) != 0) {
14,624,246✔
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,366,079,880✔
2401

2402
  taosArrayDestroy(newTbUids);
1,366,081,604✔
2403
  return code;
1,366,081,604✔
2404
}
2405

2406
static void addExistTableInfoIntoRes(SVnode *pVnode, SSubmitReq2 *pRequest, SSubmitRsp2 *pResponse,
532✔
2407
                                     SSubmitTbData *pTbData, int32_t index) {
2408
  int32_t code = 0;
532✔
2409
  int32_t lino = 0;
532✔
2410
  if ((pTbData->flags & SUBMIT_REQ_SCHEMA_RES) == 0) {
532✔
2411
    return;
×
2412
  }
2413
  if (!pResponse->aCreateTbRsp) {
532✔
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);
532✔
2421
  pCreateTbRsp->index = index;
532✔
2422
  if (pCreateTbRsp == NULL) {
532✔
2423
    code = terrno;
×
2424
    TSDB_CHECK_CODE(code, lino, _exit);
×
2425
  }
2426
  if (pTbData->suid == 0) {
532✔
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);
532✔
2433
  }
2434

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

2443
static int32_t vnodeHandleDataWrite(SVnode *pVnode, int64_t version, SSubmitReq2 *pRequest, SSubmitRsp2 *pResponse) {
1,366,080,930✔
2444
  int32_t code = TSDB_CODE_SUCCESS;
1,366,080,930✔
2445
  int32_t numTbData = taosArrayGetSize(pRequest->aSubmitTbData);
1,366,080,930✔
2446
  int8_t  hasBlob = 0;
1,366,082,195✔
2447

2448
  // Scan submit data
2449
  for (int32_t i = 0; i < numTbData; ++i) {
2,147,483,647✔
2450
    SMetaInfo      info = {0};
1,571,355,223✔
2451
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
1,571,354,668✔
2452

2453
    if (pTbData->flags & SUBMIT_REQ_WITH_BLOB) {
1,571,353,774✔
2454
      hasBlob = 1;
50,787✔
2455
    }
2456
    if (pTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
1,571,353,774✔
2457
      continue;  // skip column data format
7,714,734✔
2458
    }
2459
    if (pTbData->flags & SUBMIT_REQ_ONLY_CREATE_TABLE) {
1,563,638,932✔
2460
      continue;  // skip only crate table request
2,816,679✔
2461
    }
2462

2463
    code = metaGetInfo(pVnode->pMeta, pTbData->uid, &info, NULL);
1,560,821,622✔
2464
    if (code) {
1,560,817,592✔
2465
      code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
771✔
2466
      vWarn("vgId:%d, error occurred at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __FILE__,
771✔
2467
            __LINE__, tstrerror(code), version, pTbData->uid);
2468
      return code;
771✔
2469
    }
2470

2471
    if (info.suid != pTbData->suid) {
1,560,816,821✔
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,560,811,774✔
2481
      code = metaGetInfo(pVnode->pMeta, info.suid, &info, NULL);
1,393,205,440✔
2482
      if (code) {
1,393,216,884✔
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,560,823,218✔
2491
      code = TSDB_CODE_TDB_INVALID_TABLE_SCHEMA_VER;
532✔
2492
      addExistTableInfoIntoRes(pVnode, pRequest, pResponse, pTbData, i);
532✔
2493
      vDebug("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64
532✔
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;
532✔
2499
    }
2500
  }
2501

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

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

2509
    if (pTbData->flags & SUBMIT_REQ_ONLY_CREATE_TABLE) {
1,571,338,037✔
2510
      continue;
2,816,679✔
2511
    }
2512

2513
    if (hasBlob) {
1,568,520,664✔
2514
      code = vnodeSubmitBlobData(pVnode, pTbData);
50,787✔
2515
      if (code) {
50,787✔
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,568,520,664✔
2523
    if (code) {
1,568,417,942✔
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,568,417,942✔
2530
    if (code) {
1,568,369,147✔
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,568,369,147✔
2536
  }
2537

2538
  vDebug("vgId:%d, handle data write done, version:%" PRId64 ", affectedRows:%d", TD_VID(pVnode), version,
1,365,986,294✔
2539
         pResponse->affectedRows);
2540
  return code;
1,366,080,260✔
2541
}
2542

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

2546
  int32_t   numCols = taosArrayGetSize(pTbData->aCol);
7,712,490✔
2547
  SColData *aColData = (SColData *)TARRAY_DATA(pTbData->aCol);
7,713,185✔
2548

2549
  if (numCols <= 0) {
7,712,076✔
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,712,076✔
2557
      aColData[0].nVal <= 0) {
7,712,856✔
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) {
23,825,763✔
2567
    if (aColData[i].nVal != aColData[0].nVal) {
16,113,397✔
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,712,366✔
2578
  SRowKey  lastKey = {0};
7,712,366✔
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✔
UNCOV
2585
      code = TSDB_CODE_INVALID_MSG;
×
UNCOV
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,711,696✔
2602
    }
2603

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

2607
  return code;
7,714,734✔
2608
}
2609

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

2613
  int32_t numRows = taosArrayGetSize(pTbData->aRowP);
1,563,603,440✔
2614
  SRow  **aRow = (SRow **)TARRAY_DATA(pTbData->aRowP);
1,563,556,363✔
2615

2616
  if (numRows <= 0 && (pTbData->flags & SUBMIT_REQ_ONLY_CREATE_TABLE) == 0) {
1,563,548,435✔
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,563,548,435✔
2624
  SRowKey  lastKey = {0};
1,563,548,435✔
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;
23✔
2639
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] key:%" PRId64
23✔
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,560,689,142✔
2655
    }
2656

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

2660
  return code;
1,558,743,339✔
2661
}
2662

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

2667
  TSKEY now = taosGetTimestamp(pVnode->config.tsdbCfg.precision);
1,366,021,249✔
2668
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * pVnode->config.tsdbCfg.keep2;
1,366,007,555✔
2669
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
1,366,041,380✔
2670
  for (int32_t i = 0; i < numTbData; i++) {
2,147,483,647✔
2671
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
1,571,310,299✔
2672

2673
    if (pTbData->pCreateTbReq && pTbData->pCreateTbReq->uid == 0) {
1,571,325,521✔
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,571,318,931✔
2681
      code = vnodeScanColumnData(pVnode, pTbData, minKey, maxKey);
7,712,834✔
2682
      if (code) {
7,714,734✔
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,563,560,539✔
2689
      if (code) {
1,563,594,536✔
2690
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
72,950✔
2691
               __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2692
        return code;
×
2693
      }
2694
    }
2695
  }
2696

2697
  return code;
1,365,950,170✔
2698
}
2699

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

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

2725
  return 0;
1,826✔
2726
}
2727

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

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

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

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

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

2757
  return code;
166✔
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,
166✔
2772
                                    SAuditRecord *record) {
2773
  int32_t code = 0;
166✔
2774
  int32_t lino = 0;
166✔
2775

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

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

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

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

2794
  STagVal tv = (STagVal){.cid = tSchema->colId,
166✔
2795
                         .type = tSchema->type,
166✔
2796
                         .nData = strlen(record->strClusterId),
166✔
2797
                         .pData = record->strClusterId};  // address copy, no value
166✔
2798
  if ((pTagVals = taosArrayInit(1, sizeof(STagVal))) == NULL) {
166✔
2799
    code = terrno;
×
2800
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
2801
  }
2802
  if (NULL == taosArrayPush(pTagVals, &tv)) {
166✔
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);
166✔
2809
  if (code != TSDB_CODE_SUCCESS) {
166✔
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,
166✔
2815
                               TSDB_DEFAULT_TABLE_TTL);
2816
_exit:
166✔
2817
  if (code != 0) {
166✔
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) {
166✔
2832
    taosArrayDestroy(pTagVals);
166✔
2833
    pTagVals = NULL;
166✔
2834
  }
2835

2836
  return code;
166✔
2837
}
2838

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

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

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

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

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

2861
    // colId is consistent with audit_columns in mndStb.c
2862
    void *data = NULL;
16,434✔
2863
    if (pCol->colId == 1) {
16,434✔
2864
      data = &record->curTime;
1,826✔
2865
    } else if (pCol->colId == 2) {
14,608✔
2866
      data = record->detail;
1,826✔
2867
    } else if (pCol->colId == 3) {
12,782✔
2868
      data = record->user;
1,826✔
2869
    } else if (pCol->colId == 4) {
10,956✔
2870
      data = record->operation;
1,826✔
2871
    } else if (pCol->colId == 5) {
9,130✔
2872
      data = record->target1;
1,826✔
2873
    } else if (pCol->colId == 6) {
7,304✔
2874
      data = record->target2;
1,826✔
2875
    } else if (pCol->colId == 7) {
5,478✔
2876
      data = record->clientAddress;
1,826✔
2877
    } else if (pCol->colId == 8) {
3,652✔
2878
      data = &record->duration;
1,826✔
2879
    } else if (pCol->colId == 9) {
1,826✔
2880
      data = &record->affectedRows;
1,826✔
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,434✔
2888
      case TSDB_DATA_TYPE_NCHAR:
10,956✔
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,956✔
2898
        int32_t len = strlen(str);
10,956✔
2899
        vTrace("set svalue id:%d %s, len:%d, bytes:%d", pCol->colId, str, len, pCol->bytes);
10,956✔
2900
        if (len > pCol->bytes - VARSTR_HEADER_SIZE) {
10,956✔
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,956✔
2906
        sv.type = pCol->type;
10,956✔
2907
        sv.nData = len;
10,956✔
2908
        sv.pData = str;
10,956✔
2909
        SColVal cv = COL_VAL_VALUE(pCol->colId, sv);
10,956✔
2910
        if (NULL == taosArrayPush(pVals, &cv)) {
10,956✔
2911
          code = terrno;
×
2912
          TAOS_CHECK_GOTO(code, &lino, _exit);
×
2913
        }
2914
        //}
2915
        break;
10,956✔
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,478✔
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,478✔
2941
        sv.type = pCol->type;
5,478✔
2942
        valueSetDatum(&sv, sv.type, data, pCol->bytes);
5,478✔
2943
        SColVal cv = COL_VAL_VALUE(pCol->colId, sv);
5,478✔
2944
        if (NULL == taosArrayPush(pVals, &cv)) {
5,478✔
2945
          code = terrno;
×
2946
          TAOS_CHECK_GOTO(code, &lino, _exit);
×
2947
        }
2948
        //}
2949
        break;
5,478✔
2950
      }
2951
    }
2952
  }
2953

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

2965
_exit:
1,826✔
2966
  if (code != 0) {
1,826✔
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,826✔
2975
}
2976

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

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

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

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

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

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

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

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

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

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

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

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

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

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

3039
_exit:
1,826✔
3040
  if (code != 0) {
1,826✔
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,826✔
3050
}
3051

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

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

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

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

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

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

3077
  vTrace("vgId:%d, going to create table", TD_VID(pVnode));
1,826✔
3078
  code = vnodeHandleAutoCreateTable(pVnode, ver, pSubmitReq, pSubmitRsp);
1,826✔
3079
  if (code) {
1,826✔
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,826✔
3086
  code = vnodeHandleDataWrite(pVnode, ver, pSubmitReq, pSubmitRsp);
1,826✔
3087
  if (code) {
1,826✔
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,826✔
3094

3095
_exit:
1,826✔
3096
  if (code != 0)
1,826✔
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,826✔
3101
  (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsert, 1);
1,826✔
3102
  if (code == 0) {
1,826✔
3103
    (void)atomic_add_fetch_64(&pVnode->statis.nInsertSuccess, pSubmitRsp->affectedRows);
1,826✔
3104
    (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
1,826✔
3105
  }
3106

3107
  // update metrics
3108
  METRICS_UPDATE(pVnode->writeMetrics.total_requests, METRIC_LEVEL_LOW, 1);
1,826✔
3109
  METRICS_UPDATE(pVnode->writeMetrics.total_rows, METRIC_LEVEL_HIGH, pSubmitRsp->affectedRows);
1,826✔
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,826✔
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,826✔
3127
    tDestroySubmitReq(pSubmitReq, TSDB_MSG_FLG_ENCODE);
1,826✔
3128
    taosMemoryFree(pSubmitReq);
1,826✔
3129
  }
3130
  if (pSubmitRsp != NULL) {
1,826✔
3131
    tDestroySSubmitRsp2(pSubmitRsp, TSDB_MSG_FLG_ENCODE);
1,826✔
3132
  }
3133

3134
  return code;
1,826✔
3135
}
3136

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

3142
  SVAuditRecordReq req = {0};
664✔
3143
  if (tDeserializeSVAuditRecordReq(pReq, len, &req) != 0) {
664✔
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));
664✔
3150

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

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

3162
  SMetaReader merStb = {0};
664✔
3163
  metaReaderDoInit(&merStb, pVnode->pMeta, META_READER_LOCK);
664✔
3164
  code = metaGetTableEntryByName(&merStb, AUDIT_STABLE_NAME);
664✔
3165
  if (code != 0) {
664✔
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",
664✔
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);
664✔
3173
  if (code != 0) {
664✔
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);
664✔
3178
  metaReaderClear(&merStb);
664✔
3179
  // pSchema pTagSchema can be cached in the future
3180

3181
  SJson *pRecords = tjsonGetObjectItem(pJson, "records");
664✔
3182
  if (pRecords == NULL) {
664✔
3183
    TAOS_CHECK_GOTO(vnodeSaveOneAuditRecord(pVnode, ver, pJson, pTagSchema, suid, pSchema, pOriginalMsg), &lino, _exit);
332✔
3184
  } else {
3185
    int32_t size = tjsonGetArraySize(pRecords);
332✔
3186
    vTrace("%d items in records", size);
332✔
3187
    for (int32_t i = 0; i < size; ++i) {
1,826✔
3188
      SJson *pRecord = tjsonGetArrayItem(pRecords, i);
1,494✔
3189
      if (pRecord == NULL) {
1,494✔
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,494✔
3195
                      _exit);
3196
    }
3197
  }
3198

3199
_exit:
664✔
3200
  if (code != 0)
664✔
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);
664✔
3205
  if (pTagSchema) {
664✔
3206
    taosMemoryFreeClear(pTagSchema->pSchema);
664✔
3207
    taosMemoryFree(pTagSchema);
664✔
3208
  }
3209
  if (pJson != NULL) {
664✔
3210
    cJSON_Delete(pJson);
664✔
3211
    pJson = NULL;
664✔
3212
  }
3213
  tFreeSVAuditRecordReq(&req);
664✔
3214

3215
  return code;
664✔
3216
}
3217

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

3224
  SSubmitReq2 *pSubmitReq = &(SSubmitReq2){0};
1,366,081,492✔
3225
  SSubmitRsp2 *pSubmitRsp = &(SSubmitRsp2){0};
1,366,079,722✔
3226
  int32_t      ret;
3227
  SEncoder     ec = {0};
1,366,078,860✔
3228

3229
  pRsp->code = TSDB_CODE_SUCCESS;
1,366,078,860✔
3230

3231
  void           *pAllocMsg = NULL;
1,366,078,661✔
3232
  SSubmitReq2Msg *pMsg = (SSubmitReq2Msg *)pReq;
1,366,078,661✔
3233
  SDecoder        dc = {0};
1,366,078,661✔
3234
  if (0 == taosHton64(pMsg->version)) {
1,366,079,604✔
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,366,079,060✔
3246
    len -= sizeof(SSubmitReq2Msg);
1,366,079,675✔
3247

3248
    tDecoderInit(&dc, pReq, len);
1,366,079,675✔
3249
    if (tDecodeSubmitReq(&dc, pSubmitReq, NULL) < 0) {
1,366,079,887✔
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,366,046,431✔
3257
  if (code) {
1,365,940,714✔
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,365,940,714✔
3264
          TD_VID(pVnode), ver, (int32_t)taosArrayGetSize(pSubmitReq->aSubmitTbData));
3265

3266
  // Handle auto create table
3267
  code = vnodeHandleAutoCreateTable(pVnode, ver, pSubmitReq, pSubmitRsp);
1,365,979,869✔
3268
  if (code) {
1,366,081,492✔
3269
    vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
1,714✔
3270
           tstrerror(code), ver);
3271
    TSDB_CHECK_CODE(code, lino, _exit);
1,714✔
3272
  }
3273

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

3282
_exit:
1,366,081,492✔
3283
  // message
3284
  pRsp->code = code;
1,366,081,492✔
3285
  tEncodeSize(tEncodeSSubmitRsp2, pSubmitRsp, pRsp->contLen, ret);
1,366,081,441✔
3286
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
1,366,079,001✔
3287
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
1,366,078,800✔
3288
  if (tEncodeSSubmitRsp2(&ec, pSubmitRsp) < 0) {
1,366,080,950✔
3289
    vError("vgId:%d, failed to encode submit response", TD_VID(pVnode));
×
3290
  }
3291
  tEncoderClear(&ec);
1,366,067,799✔
3292

3293
  // update statistics
3294
  (void)atomic_add_fetch_64(&pVnode->statis.nInsert, pSubmitRsp->affectedRows);
1,366,059,091✔
3295
  (void)atomic_add_fetch_64(&pVnode->statis.nInsertSuccess, pSubmitRsp->affectedRows);
1,366,076,639✔
3296
  (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsert, 1);
1,366,067,777✔
3297

3298
  // update metrics
3299
  METRICS_UPDATE(pVnode->writeMetrics.total_requests, METRIC_LEVEL_LOW, 1);
1,366,064,415✔
3300
  METRICS_UPDATE(pVnode->writeMetrics.total_rows, METRIC_LEVEL_HIGH, pSubmitRsp->affectedRows);
1,366,044,655✔
3301
  METRICS_UPDATE(pVnode->writeMetrics.total_bytes, METRIC_LEVEL_LOW, pMsg->header.contLen);
1,366,050,585✔
3302

3303
  if (tsEnableMonitor && tsMonitorFqdn[0] != 0 && tsMonitorPort != 0 && pSubmitRsp->affectedRows > 0 &&
1,366,049,483✔
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,366,049,483✔
3317
    (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
1,366,050,013✔
3318
  }
3319

3320
  // clear
3321
  tDestroySubmitReq(pSubmitReq, 0 == taosHton64(pMsg->version) ? TSDB_MSG_FLG_CMPT : TSDB_MSG_FLG_DECODE);
1,366,073,132✔
3322
  tDestroySSubmitRsp2(pSubmitRsp, TSDB_MSG_FLG_ENCODE);
1,366,015,953✔
3323

3324
  if (code) {
1,365,986,976✔
3325
    terrno = code;
3,017✔
3326
    if (code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
3,017✔
3327
      vInfo("vgId:%d, failed to process submit request since %s, lino:%d, version:%" PRId64, TD_VID(pVnode),
532✔
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),
2,485✔
3332
             tstrerror(code), lino, ver);
3333
    }
3334
  }
3335

3336
  taosMemoryFree(pAllocMsg);
1,366,008,405✔
3337
  tDecoderClear(&dc);
1,365,985,289✔
3338

3339
  return code;
1,366,064,946✔
3340
}
3341

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

3345
  vInfo("vgId:%d, trim meta of tables per hash range [%" PRIu32 ", %" PRIu32 "]. apply-index:%" PRId64, TD_VID(pVnode),
28,305✔
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);
28,305✔
3350

3351
  return code;
28,305✔
3352
}
3353

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

3361
  code = vnodeConsolidateAlterHashRange(pVnode, ver);
28,305✔
3362
  if (code < 0) {
28,305✔
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;
28,305✔
3367

3368
_exit:
6,010,948✔
3369
  pRsp->msgType = TDMT_VND_ALTER_CONFIRM_RSP;
6,010,948✔
3370
  pRsp->code = code;
6,010,948✔
3371
  pRsp->pCont = NULL;
6,010,948✔
3372
  pRsp->contLen = 0;
6,010,948✔
3373

3374
  return code;
6,010,948✔
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) {
357,064✔
3381
  bool walChanged = false;
357,064✔
3382
  bool tsdbChanged = false;
357,064✔
3383

3384
  SAlterVnodeConfigReq req = {0};
357,064✔
3385
  if (tDeserializeSAlterVnodeConfigReq(pReq, len, &req) != 0) {
357,444✔
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
357,444✔
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) {
357,444✔
3400
    pVnode->config.cacheLastSize = req.cacheLastSize;
4,016✔
3401
    tsdbCacheSetCapacity(pVnode, (size_t)pVnode->config.cacheLastSize * 1024 * 1024);
4,016✔
3402
  }
3403

3404
  if (pVnode->config.cacheLastShardBits != req.cacheLastShardBits) {
357,444✔
3405
    vInfo("vgId:%d, cacheLastShardBits change from %d to %d, rebuilding last cache",
1,616✔
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,616✔
3410
    int32_t code = tsdbRebuildLastCache(pVnode->pTsdb, req.cacheLastShardBits);
1,616✔
3411
    (void)taosThreadMutexUnlock(&pVnode->pTsdb->lruMutex);
1,616✔
3412

3413
    if (code) {
1,616✔
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,616✔
3419

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

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

3430
  if (pVnode->config.szCache != req.pages) {
357,444✔
3431
    if ((terrno = metaAlterCache(pVnode->pMeta, req.pages)) < 0) {
5,112✔
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,112✔
3437
      pVnode->config.szCache = req.pages;
5,112✔
3438
    }
3439
  }
3440

3441
  if (pVnode->config.cacheLast != req.cacheLast) {
357,444✔
3442
    pVnode->config.cacheLast = req.cacheLast;
96,060✔
3443
  }
3444

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

3450
  if (pVnode->config.walCfg.level != req.walLevel) {
357,444✔
3451
    if (pVnode->config.walCfg.level == 0) {
42,964✔
3452
      pVnode->config.walCfg.clearFiles = 1;
2,480✔
3453
    }
3454
    pVnode->config.walCfg.level = req.walLevel;
42,964✔
3455
    walChanged = true;
42,964✔
3456
  }
3457

3458
  if (pVnode->config.walCfg.retentionPeriod != req.walRetentionPeriod) {
357,444✔
3459
    pVnode->config.walCfg.retentionPeriod = req.walRetentionPeriod;
56,582✔
3460
    walChanged = true;
56,582✔
3461
  }
3462

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

3468
  if (pVnode->config.tsdbCfg.keep0 != req.daysToKeep0) {
357,444✔
3469
    pVnode->config.tsdbCfg.keep0 = req.daysToKeep0;
48,666✔
3470
    tsdbChanged = true;
48,666✔
3471
  }
3472

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

3478
  if (pVnode->config.tsdbCfg.keep2 != req.daysToKeep2) {
357,444✔
3479
    pVnode->config.tsdbCfg.keep2 = req.daysToKeep2;
40,480✔
3480
    tsdbChanged = true;
40,480✔
3481
  }
3482

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

3488
  if (req.sttTrigger != -1 && req.sttTrigger != pVnode->config.sttTrigger) {
357,444✔
3489
    if (req.sttTrigger > 1 && pVnode->config.sttTrigger > 1) {
2,838✔
3490
      pVnode->config.sttTrigger = req.sttTrigger;
1,604✔
3491
    } else {
3492
      vnodeAWait(&pVnode->commitTask);
1,234✔
3493

3494
      int32_t ret = tsdbDisableAndCancelAllBgTask(pVnode->pTsdb);
1,234✔
3495
      if (ret != 0) {
1,234✔
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,234✔
3500
      tsdbEnableBgTask(pVnode->pTsdb);
1,234✔
3501
    }
3502
  }
3503

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

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

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

3530
  if (tsdbChanged) {
357,444✔
3531
    tsdbSetKeepCfg(pVnode->pTsdb, &pVnode->config.tsdbCfg);
65,070✔
3532
  }
3533

3534
  return 0;
357,444✔
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,435,855✔
3585
                                     SRpcMsg *pOriginalMsg) {
3586
  int32_t     code = 0;
2,435,855✔
3587
  SDecoder   *pCoder = &(SDecoder){0};
2,435,855✔
3588
  SDeleteRes *pRes = &(SDeleteRes){0};
2,435,855✔
3589

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

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

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

3605
  if (pRes->affectedRows > 0) {
2,435,855✔
3606
    for (int32_t iUid = 0; iUid < taosArrayGetSize(pRes->uidList); iUid++) {
4,338,912✔
3607
      uint64_t uid = *(uint64_t *)taosArrayGet(pRes->uidList, iUid);
2,471,404✔
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,471,404✔
3610
      code = tsdbDeleteTableData(pVnode->pTsdb, ver, pRes->suid, uid, pRes->skey, pRes->ekey, secureDelete);
2,471,404✔
3611
      if (code) goto _err;
2,471,404✔
3612
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, pRes->ctimeMs);
2,471,404✔
3613
      if (code) goto _err;
2,471,404✔
3614
    }
3615
  }
3616

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

3620
  SVDeleteRsp rsp = {.affectedRows = pRes->affectedRows};
2,435,855✔
3621
  int32_t     ret = 0;
2,435,855✔
3622
  tEncodeSize(tEncodeSVDeleteRsp, &rsp, pRsp->contLen, ret);
2,435,855✔
3623
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
2,435,224✔
3624
  SEncoder ec = {0};
2,435,855✔
3625
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
2,435,855✔
3626
  code = tEncodeSVDeleteRsp(&ec, &rsp);
2,435,224✔
3627
  if (code) goto _err;
2,435,855✔
3628
  tEncoderClear(&ec);
2,435,855✔
3629
  return code;
2,435,855✔
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,489✔
3650
  SVCreateStbReq req = {0};
20,489✔
3651
  SDecoder       dc = {0};
20,489✔
3652
  int32_t        code = 0;
20,489✔
3653

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

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

3666
  code = metaAddIndexToSuperTable(pVnode->pMeta, ver, &req);
20,489✔
3667
  if (code) {
20,489✔
3668
    pRsp->code = code;
×
3669
    goto _err;
×
3670
  }
3671
  tDecoderClear(&dc);
20,489✔
3672
  return 0;
20,489✔
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,636✔
3679
  SDropIndexReq req = {0};
16,636✔
3680
  int32_t       code = 0;
16,636✔
3681
  pRsp->msgType = TDMT_VND_DROP_INDEX_RSP;
16,636✔
3682
  pRsp->code = TSDB_CODE_SUCCESS;
16,636✔
3683
  pRsp->pCont = NULL;
16,636✔
3684
  pRsp->contLen = 0;
16,636✔
3685

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

3691
  code = metaDropIndexFromSuperTable(pVnode->pMeta, ver, &req);
16,636✔
3692
  if (code) {
16,636✔
3693
    pRsp->code = code;
×
3694
    return code;
×
3695
  }
3696
  return TSDB_CODE_SUCCESS;
16,636✔
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) {
81,222✔
3702
  if (!pVnode->restored) {
81,222✔
3703
    vInfo("vgId:%d, ignore compact req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
5,928✔
3704
    return 0;
5,928✔
3705
  }
3706
  return vnodeAsyncCompact(pVnode, ver, pReq, len, pRsp);
75,294✔
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) {
100,183✔
3723
  SSyncState syncState = syncGetState(pVnode->sync);
100,183✔
3724
  if (syncState.state != TAOS_SYNC_STATE_LEADER) {
100,183✔
3725
    return terrno = TSDB_CODE_SYN_NOT_LEADER;
47,809✔
3726
  }
3727
  return 0;
52,374✔
3728
}
3729

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

3736
  if (strncmp(token, member0Token, TSDB_ARB_TOKEN_SIZE) != 0 &&
52,374✔
3737
      strncmp(token, member1Token, TSDB_ARB_TOKEN_SIZE) != 0) {
48,446✔
3738
    return terrno = TSDB_CODE_MND_ARB_TOKEN_MISMATCH;
44,528✔
3739
  }
3740

3741
  terrno = TSDB_CODE_SUCCESS;
7,846✔
3742
  return 0;
7,846✔
3743
}
3744

3745
static int32_t vnodeCheckSyncd(SVnode *pVnode, char *member0Token, char *member1Token) {
13,475✔
3746
  int32_t code = vnodeCheckToken(pVnode, member0Token, member1Token);
13,475✔
3747
  if (code != 0) {
13,475✔
3748
    return code;
9,552✔
3749
  }
3750

3751
  return syncCheckSynced(pVnode->sync);
3,923✔
3752
}
3753

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

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

3762
  SVArbCheckSyncReq syncReq = {0};
13,475✔
3763

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

3769
  vInfo("vgId:%d, start to process vnode-arb-check-sync req QID:0x%" PRIx64 ":0x%" PRIx64 ", seqNum:%" PRIx64
13,475✔
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;
13,475✔
3776
  pRsp->code = TSDB_CODE_SUCCESS;
13,475✔
3777
  pRsp->pCont = NULL;
13,475✔
3778
  pRsp->contLen = 0;
13,475✔
3779

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

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

3790
  if ((code = vnodeUpdateArbTerm(pVnode, syncReq.arbTerm)) != 0) {
13,475✔
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);
13,475✔
3796
  if (contLen <= 0) {
13,475✔
3797
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
3798
    code = -1;
×
3799
    goto _OVER;
×
3800
  }
3801
  void *pHead = rpcMallocCont(contLen);
13,475✔
3802
  if (!pHead) {
13,475✔
3803
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
3804
    code = -1;
×
3805
    goto _OVER;
×
3806
  }
3807

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

3815
  pRsp->pCont = pHead;
13,475✔
3816
  pRsp->contLen = contLen;
13,475✔
3817

3818
  vInfo(
13,475✔
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;
13,475✔
3824

3825
_OVER:
13,475✔
3826
  if (code != 0) {
13,475✔
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);
13,475✔
3831
  return code;
13,475✔
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