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

taosdata / TDengine / #5009

29 Mar 2026 04:32AM UTC coverage: 72.26% (+0.02%) from 72.241%
#5009

push

travis-ci

web-flow
refactor: do some internal refactor for TDgpt. (#34955)

253662 of 351039 relevant lines covered (72.26%)

131649114.75 hits per line

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

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

16
#include <stdint.h>
17
#include "audit.h"
18
#include "cos.h"
19
#include "libs/new-stream/stream.h"
20
#include "monitor.h"
21
#include "taoserror.h"
22
#include "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) {
64,661,756✔
91
  int32_t code = 0;
64,661,756✔
92
  int32_t lino = 0;
64,661,756✔
93

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

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

105
  // name
106
  char *name = NULL;
64,664,732✔
107
  if (tDecodeCStr(pCoder, &name) < 0) {
64,665,935✔
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);
64,665,935✔
114
  if (uid == 0) {
64,657,117✔
115
    uid = tGenIdPI64();
64,287,230✔
116
  }
117
  taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), uid);
64,651,647✔
118

119
  // btime
120
  taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos + 8), btime);
64,652,003✔
121

122
  tEndDecode(pCoder);
64,660,683✔
123

124
_exit:
64,652,800✔
125
  if (code) {
64,653,764✔
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);
64,653,764✔
129
    if (pUid) *pUid = uid;
64,650,754✔
130
  }
131
  return code;
64,650,367✔
132
}
133
static int32_t vnodePreProcessCreateTableMsg(SVnode *pVnode, SRpcMsg *pMsg) {
43,245,508✔
134
  int32_t code = 0;
43,245,508✔
135
  int32_t lino = 0;
43,245,508✔
136

137
  int64_t  btime = taosGetTimestampMs();
43,245,508✔
138
  SDecoder dc = {0};
43,245,508✔
139
  int32_t  nReqs;
43,242,992✔
140

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

147
  if (tDecodeI32v(&dc, &nReqs) < 0) {
43,245,170✔
148
    code = TSDB_CODE_INVALID_MSG;
×
149
    TSDB_CHECK_CODE(code, lino, _exit);
×
150
  }
151
  for (int32_t iReq = 0; iReq < nReqs; iReq++) {
92,027,957✔
152
    code = vnodePreprocessCreateTableReq(pVnode, &dc, btime, NULL);
48,786,617✔
153
    TSDB_CHECK_CODE(code, lino, _exit);
48,782,787✔
154
  }
155

156
  tEndDecode(&dc);
43,241,340✔
157

158
_exit:
43,244,763✔
159
  tDecoderClear(&dc);
43,244,763✔
160
  if (code) {
43,244,653✔
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;
43,244,231✔
165
}
166

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

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

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

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

182
  destroyAlterTbReq(&vAlterTbReq);
12,559,807✔
183

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

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

199
  SMsgHead *pContOld = pMsg->pCont;
11,421,444✔
200
  int32_t   reqLenOld = pMsg->contLen - sizeof(SMsgHead);
11,421,866✔
201

202
  SArray *tbUids = NULL;
11,419,841✔
203
  int64_t timestampMs = 0;
11,419,841✔
204

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

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

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

225
    ttlReq.nUids = taosArrayGetSize(tbUids);
11,410,277✔
226
    ttlReq.pTbUids = tbUids;
11,410,399✔
227
  }
228

229
  if (ttlReq.nUids == 0) {
11,410,399✔
230
    code = TSDB_CODE_MSG_PREPROCESSED;
11,400,295✔
231
    TSDB_CHECK_CODE(code, lino, _exit);
11,400,295✔
232
  }
233

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

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

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

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

255
  code = 0;
12,120✔
256

257
_exit:
11,411,133✔
258
  taosArrayDestroy(tbUids);
11,414,825✔
259

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

267
  return code;
11,406,033✔
268
}
269

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

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

280
  SSubmitTbData submitTbData;
515,196,857✔
281
  uint8_t       version;
282
  if (tDecodeI32v(pCoder, &submitTbData.flags) < 0) {
515,199,531✔
283
    code = TSDB_CODE_INVALID_MSG;
×
284
    TSDB_CHECK_CODE(code, lino, _exit);
×
285
  }
286
  version = (submitTbData.flags >> 8) & 0xff;
515,199,531✔
287
  submitTbData.flags = submitTbData.flags & 0xff;
515,199,531✔
288

289
  int64_t uid;
515,194,993✔
290
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
515,197,854✔
291
    code = vnodePreprocessCreateTableReq(pVnode, pCoder, btimeMs, &uid);
15,875,783✔
292
    TSDB_CHECK_CODE(code, lino, _exit);
15,872,946✔
293
  }
294

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

301
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
515,200,901✔
302
    taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), uid);
15,874,389✔
303
    pCoder->pos += sizeof(int64_t);
15,874,646✔
304
  } else {
305
    if (tDecodeI64(pCoder, &submitTbData.uid) < 0) {
499,322,269✔
306
      code = TSDB_CODE_INVALID_MSG;
×
307
      TSDB_CHECK_CODE(code, lino, _exit);
×
308
    }
309
  }
310

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

316
  // scan and check
317
  TSKEY now = btimeMs;
515,195,509✔
318
  if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_MICRO) {
515,195,509✔
319
    now *= 1000;
2,616,538✔
320
  } else if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_NANO) {
512,580,369✔
321
    now *= 1000000;
1,528,333✔
322
  }
323

324
  int32_t keep = pVnode->config.tsdbCfg.keep2;
515,197,351✔
325

326
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * keep;
515,196,553✔
327
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
515,197,132✔
328
  if (submitTbData.flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
515,188,545✔
329
    uint64_t nColData;
6,897,100✔
330
    if (tDecodeU64v(pCoder, &nColData) < 0) {
6,899,207✔
331
      code = TSDB_CODE_INVALID_MSG;
×
332
      TSDB_CHECK_CODE(code, lino, _exit);
×
333
    }
334

335
    SColData colData = {0};
6,899,207✔
336
    code = tDecodeColData(version, pCoder, &colData, false);
6,898,529✔
337
    if (code) {
6,899,199✔
338
      code = TSDB_CODE_INVALID_MSG;
×
339
      TSDB_CHECK_CODE(code, lino, _exit);
×
340
    }
341

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

354
    for (uint64_t i = 1; i < nColData; i++) {
21,098,220✔
355
      code = tDecodeColData(version, pCoder, &colData, true);
14,266,475✔
356
      if (code) {
14,267,490✔
357
        code = TSDB_CODE_INVALID_MSG;
×
358
        TSDB_CHECK_CODE(code, lino, _exit);
×
359
      }
360
    }
361
  } else {
362
    uint64_t nRow;
508,286,907✔
363
    if (tDecodeU64v(pCoder, &nRow) < 0) {
508,295,632✔
364
      code = TSDB_CODE_INVALID_MSG;
×
365
      TSDB_CHECK_CODE(code, lino, _exit);
×
366
    }
367

368
    for (uint32_t iRow = 0; iRow < nRow; ++iRow) {
2,147,483,647✔
369
      SRow *pRow = (SRow *)(pCoder->data + pCoder->pos);
2,147,483,647✔
370
      pCoder->pos += pRow->len;
2,147,483,647✔
371
#ifndef NO_UNALIGNED_ACCESS
372
      if (pRow->ts < minKey || pRow->ts > maxKey) {
2,147,483,647✔
373
#else
374
      TSKEY ts = taosGetInt64Aligned(&pRow->ts);
375
      if (ts < minKey || ts > maxKey) {
376
#endif
377
        code = TSDB_CODE_TDB_TIMESTAMP_OUT_OF_RANGE;
69,341✔
378
        TSDB_CHECK_CODE(code, lino, _exit);
69,341✔
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)) {
515,127,530✔
390
    taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), ctimeMs);
515,126,776✔
391
    pCoder->pos += sizeof(int64_t);
515,122,176✔
392
  }
393

394
  tEndDecode(pCoder);
515,123,032✔
395

396
_exit:
515,199,252✔
397
  if (code) {
515,194,667✔
398
    vError("vgId:%d, %s:%d failed to vnodePreProcessSubmitTbData submit request since %s", TD_VID(pVnode), __func__,
69,950✔
399
           lino, tstrerror(code));
400
  }
401
  return code;
515,194,667✔
402
}
403
static int32_t vnodePreProcessSubmitMsg(SVnode *pVnode, SRpcMsg *pMsg) {
489,623,393✔
404
  int32_t code = 0;
489,623,393✔
405
  int32_t lino = 0;
489,623,393✔
406

407
  if (tsBypassFlag & TSDB_BYPASS_RA_RPC_RECV_SUBMIT) {
489,623,393✔
408
    return TSDB_CODE_MSG_PREPROCESSED;
71✔
409
  }
410

411
  SDecoder *pCoder = &(SDecoder){0};
489,623,322✔
412

413
  if (taosHton64(((SSubmitReq2Msg *)pMsg->pCont)->version) != 1) {
489,624,258✔
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));
489,624,392✔
419

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

425
  uint64_t nSubmitTbData;
489,619,227✔
426
  if (tDecodeU64v(pCoder, &nSubmitTbData) < 0) {
489,621,730✔
427
    code = TSDB_CODE_INVALID_MSG;
×
428
    TSDB_CHECK_CODE(code, lino, _exit);
×
429
  }
430

431
  int64_t btimeMs = taosGetTimestampMs();
489,622,938✔
432
  int64_t ctimeMs = btimeMs;
489,622,938✔
433
  for (uint64_t i = 0; i < nSubmitTbData; i++) {
1,004,745,608✔
434
    code = vnodePreProcessSubmitTbData(pVnode, pCoder, btimeMs, ctimeMs);
515,197,463✔
435
    TSDB_CHECK_CODE(code, lino, _exit);
515,192,620✔
436
  }
437

438
  tEndDecode(pCoder);
489,548,145✔
439

440
_exit:
489,614,429✔
441
  tDecoderClear(pCoder);
489,622,462✔
442
  if (code) {
489,620,935✔
443
    vError("vgId:%d, %s:%d failed to preprocess submit request since %s, msg type:%s", TD_VID(pVnode), __func__, lino,
69,950✔
444
           tstrerror(code), TMSG_INFO(pMsg->msgType));
445
  }
446
  return code;
489,617,273✔
447
}
448

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

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

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

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

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

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

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

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

485
  taosArrayDestroy(res.uidList);
1,791,210✔
486

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

523
  SVArbCheckSyncReq syncReq = {0};
34,149✔
524

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

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

535
  int32_t code = terrno;
34,149✔
536
  tFreeSVArbCheckSyncReq(&syncReq);
34,149✔
537

538
  return code;
34,149✔
539
}
540

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

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

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

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

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

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

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

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

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

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

619
int32_t vnodePreProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
583,672,921✔
620
  int32_t code = 0;
583,672,921✔
621

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

628
  switch (pMsg->msgType) {
583,671,709✔
629
    case TDMT_VND_CREATE_TABLE: {
43,245,508✔
630
      code = vnodePreProcessCreateTableMsg(pVnode, pMsg);
43,245,508✔
631
    } break;
43,244,105✔
632
    case TDMT_VND_ALTER_TABLE: {
12,559,807✔
633
      code = vnodePreProcessAlterTableMsg(pVnode, pMsg);
12,559,807✔
634
    } break;
12,559,807✔
635
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
11,422,619✔
636
    case TDMT_VND_DROP_TTL_TABLE: {
637
      code = vnodePreProcessDropTtlMsg(pVnode, pMsg);
11,422,619✔
638
    } break;
11,414,539✔
639
    case TDMT_VND_SUBMIT: {
489,622,845✔
640
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.preprocess_time, METRIC_LEVEL_LOW,
489,622,845✔
641
                           { code = vnodePreProcessSubmitMsg(pVnode, pMsg); });
642
    } break;
489,617,416✔
643
    case TDMT_VND_DELETE: {
1,791,210✔
644
      code = vnodePreProcessDeleteMsg(pVnode, pMsg);
1,791,210✔
645
    } break;
1,791,210✔
646
    case TDMT_VND_BATCH_DEL: {
×
647
      code = vnodePreProcessBatchDeleteMsg(pVnode, pMsg);
×
648
    } break;
×
649
    case TDMT_VND_ARB_CHECK_SYNC: {
49,532✔
650
      code = vnodePreProcessArbCheckSyncMsg(pVnode, pMsg);
49,532✔
651
    } break;
49,532✔
652
    case TDMT_VND_DROP_TABLE: {
1,620,880✔
653
      code = vnodePreProcessDropTbMsg(pVnode, pMsg);
1,620,880✔
654
    } break;
1,620,880✔
655
#ifdef TD_ENTERPRISE
656
    case TDMT_VND_SSMIGRATE_FILESET: {
3,770✔
657
      code = vnodePreProcessSsMigrateFileSetReq(pVnode, pMsg);
3,770✔
658
    } break;
3,770✔
659
#endif
660
    case TDMT_VND_AUDIT_RECORD: {
979✔
661
      vTrace("vgId:%d, pre process TDMT_VND_AUDIT_RECORD", TD_VID(pVnode));
979✔
662
    } break;
244✔
663
    default:
23,354,270✔
664
      break;
23,354,270✔
665
  }
666

667
  if (code && code != TSDB_CODE_MSG_PREPROCESSED) {
583,655,773✔
668
    vError("vgId:%d, failed to preprocess write request since %s, msg type:%s", TD_VID(pVnode), tstrerror(code),
102,197✔
669
           TMSG_INFO(pMsg->msgType));
670
  }
671
  return code;
583,651,595✔
672
}
673

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

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

682
  SBseBatch *pBatch = NULL;
45,817✔
683

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

687
  SRow  **pRow = (SRow **)TARRAY_DATA(pSubmitTbData->aRowP);
45,817✔
688
  int32_t rowIdx = -1;
45,817✔
689
  for (int32_t i = 0; i < sz; i++) {
25,652,668✔
690
    int64_t     seq = 0;
25,606,851✔
691
    SBlobValue *p = taosArrayGet(pBlobSet->pSeqTable, i);
25,606,851✔
692
    if (p->type == TSDB_DATA_BLOB_EMPTY_VALUE || p->type == TSDB_DATA_BLOB_NULL_VALUE) {
25,606,851✔
693
      // skip empty or null blob
694
      continue;
2,543,694✔
695
    }
696

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

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

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

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

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

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

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

738
  SBseBatch *pBatch = NULL;
84✔
739

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

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

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

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

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

796
  return code;
45,901✔
797
}
798

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

806
  (void)taosThreadMutexLock(&pVnode->mutex);
697,534,116✔
807
  if (pVnode->disableWrite) {
697,600,266✔
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);
697,532,147✔
813

814
  if (ver <= pVnode->state.applied) {
697,602,488✔
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,
697,532,654✔
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)) {
697,585,497✔
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)) {
697,612,178✔
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);
697,613,885✔
839
  atomic_store_64(&pVnode->state.applyTerm, pMsg->info.conn.applyTerm);
697,614,158✔
840

841
  if (!syncUtilUserCommit(pMsg->msgType)) goto _exit;
697,614,804✔
842

843
  // skip header
844
  pReq = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
686,768,857✔
845
  len = pMsg->contLen - sizeof(SMsgHead);
686,769,558✔
846
  bool needCommit = false;
686,770,300✔
847
  bool forceTrimWal = false;
686,770,300✔
848

849
  switch (pMsg->msgType) {
686,770,300✔
850
    /* META */
851
    case TDMT_VND_CREATE_STB:
5,342,438✔
852
      code = vnodeProcessCreateStbReq(pVnode, ver, pReq, len, pRsp);
5,342,438✔
853
      TSDB_CHECK_CODE(code, lino, _err);
5,343,180✔
854
      break;
5,340,116✔
855
    case TDMT_VND_ALTER_STB:
7,710,956✔
856
      code = vnodeProcessAlterStbReq(pVnode, ver, pReq, len, pRsp);
7,710,956✔
857
      TSDB_CHECK_CODE(code, lino, _err);
7,710,956✔
858
      break;
7,710,956✔
859
    case TDMT_VND_DROP_STB:
930,120✔
860
      code = vnodeProcessDropStbReq(pVnode, ver, pReq, len, pRsp, pMsg);
930,120✔
861
      TSDB_CHECK_CODE(code, lino, _err);
930,120✔
862
      break;
930,120✔
863
    case TDMT_VND_CREATE_TABLE:
46,022,962✔
864
      code = vnodeProcessCreateTbReq(pVnode, ver, pReq, len, pRsp, pMsg);
46,022,962✔
865
      TSDB_CHECK_CODE(code, lino, _err);
46,013,366✔
866
      break;
46,013,366✔
867
    case TDMT_VND_ALTER_TABLE:
12,560,785✔
868
      code = vnodeProcessAlterTbReq(pVnode, ver, pReq, len, pRsp);
12,560,785✔
869
      TSDB_CHECK_CODE(code, lino, _err);
12,560,785✔
870
      break;
12,560,785✔
871
    case TDMT_VND_DROP_TABLE:
1,620,951✔
872
    case TDMT_VND_SNODE_DROP_TABLE:
873
      code = vnodeProcessDropTbReq(pVnode, ver, pReq, len, pRsp, pMsg);
1,620,951✔
874
      TSDB_CHECK_CODE(code, lino, _err);
1,620,951✔
875
      break;
1,620,951✔
876
    case TDMT_VND_CREATE_RSMA:
40,012✔
877
      code = vnodeProcessCreateRsmaReq(pVnode, ver, pReq, len, pRsp);
40,012✔
878
      TSDB_CHECK_CODE(code, lino, _err);
40,012✔
879
      break;
40,012✔
880
    case TDMT_VND_DROP_RSMA:
20,140✔
881
      code = vnodeProcessDropRsmaReq(pVnode, ver, pReq, len, pRsp);
20,140✔
882
      TSDB_CHECK_CODE(code, lino, _err);
20,140✔
883
      break;
20,140✔
884
    case TDMT_VND_ALTER_RSMA:
12,332✔
885
      code = vnodeProcessAlterRsmaReq(pVnode, ver, pReq, len, pRsp);
12,332✔
886
      TSDB_CHECK_CODE(code, lino, _err);
12,332✔
887
      break;
12,332✔
888
    case TDMT_VND_DROP_TTL_TABLE:
×
889
      code = vnodeProcessDropTtlTbReq(pVnode, ver, pReq, len, pRsp);
×
890
      TSDB_CHECK_CODE(code, lino, _err);
×
891
      break;
×
892
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
12,120✔
893
      code = vnodeProcessFetchTtlExpiredTbs(pVnode, ver, pReq, len, pRsp);
12,120✔
894
      TSDB_CHECK_CODE(code, lino, _err);
12,120✔
895
      break;
12,120✔
896
    case TDMT_VND_TRIM:
94,826✔
897
      code = vnodeProcessTrimReq(pVnode, ver, pReq, len, pRsp);
94,826✔
898
      TSDB_CHECK_CODE(code, lino, _err);
94,826✔
899
      break;
94,826✔
900
      case TDMT_VND_TRIM_WAL:
6,900✔
901
        needCommit = true;
6,900✔
902
        forceTrimWal = true;
6,900✔
903
        break;
6,900✔
904
#ifdef TD_ENTERPRISE
905
    case TDMT_VND_SSMIGRATE_FILESET:
3,770✔
906
      if (vnodeProcessSsMigrateFileSetReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
3,770✔
907
      break;
3,770✔
908
    case TDMT_VND_FOLLOWER_SSMIGRATE:
6,786✔
909
      if (vnodeProcessFollowerSsMigrateReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
6,786✔
910
      break;
6,786✔
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: {
589,161,629✔
918
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.apply_time, METRIC_LEVEL_LOW, {
589,161,629✔
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);
589,112,116✔
922
      break;
589,112,412✔
923
    }
924
    case TDMT_VND_DELETE:
2,187,140✔
925
      code = vnodeProcessDeleteReq(pVnode, ver, pReq, len, pRsp, pMsg);
2,187,140✔
926
      TSDB_CHECK_CODE(code, lino, _err);
2,187,140✔
927
      break;
2,187,140✔
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,220,955✔
935
      code = tqProcessSubscribeReq(pVnode->pTq, ver, pReq, len);
1,220,955✔
936
      TSDB_CHECK_CODE(code, lino, _err);
1,219,294✔
937
      break;
1,219,294✔
938
    case TDMT_VND_TMQ_DELETE_SUB:
284,264✔
939
      code = tqProcessDeleteSubReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen);
284,264✔
940
      TSDB_CHECK_CODE(code, lino, _err);
284,025✔
941
      break;
284,025✔
942
    case TDMT_VND_TMQ_COMMIT_OFFSET:
9,656,426✔
943
      code = tqProcessOffsetCommitReq(pVnode->pTq, ver, pReq, len);
9,656,426✔
944
      TSDB_CHECK_CODE(code, lino, _err);
9,655,066✔
945
      break;
9,655,066✔
946
#endif
947
    case TDMT_VND_ALTER_CONFIRM:
4,223,500✔
948
      needCommit = pVnode->config.hashChange;
4,223,500✔
949
      code = vnodeProcessAlterConfirmReq(pVnode, ver, pReq, len, pRsp);
4,223,500✔
950
      TSDB_CHECK_CODE(code, lino, _err);
4,223,500✔
951
      break;
4,223,500✔
952
    case TDMT_VND_ALTER_CONFIG:
336,131✔
953
      vnodeProcessAlterConfigReq(pVnode, ver, pReq, len, pRsp);
336,131✔
954
      break;
336,131✔
955
    case TDMT_VND_COMMIT:
5,204,142✔
956
      needCommit = true;
5,204,142✔
957
      break;
5,204,142✔
958
    case TDMT_VND_CREATE_INDEX:
18,514✔
959
      vnodeProcessCreateIndexReq(pVnode, ver, pReq, len, pRsp);
18,514✔
960
      break;
18,514✔
961
    case TDMT_VND_DROP_INDEX:
15,072✔
962
      vnodeProcessDropIndexReq(pVnode, ver, pReq, len, pRsp);
15,072✔
963
      break;
15,072✔
964
    case TDMT_VND_COMPACT:
70,165✔
965
      vnodeProcessCompactVnodeReq(pVnode, ver, pReq, len, pRsp);
70,165✔
966
      goto _exit;
70,165✔
967
    case TDMT_VND_SCAN:
503✔
968
      vnodeProcessScanVnodeReq(pVnode, ver, pReq, len, pRsp);
503✔
969
      goto _exit;
503✔
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,532✔
975
      vnodeProcessKillCompactReq(pVnode, ver, pReq, len, pRsp);
1,532✔
976
      break;
1,532✔
977
#endif
978
    case TDMT_VND_KILL_SCAN:
94✔
979
      vnodeProcessKillScanReq(pVnode, ver, pReq, len, pRsp);
94✔
980
      break;
94✔
981
    case TDMT_VND_KILL_TRIM:
×
982
      vnodeProcessKillRetentionReq(pVnode, ver, pReq, len, pRsp);
×
983
      break;
×
984
    /* ARB */
985
    case TDMT_VND_ARB_CHECK_SYNC:
3,816✔
986
      vnodeProcessArbCheckSyncReq(pVnode, pReq, len, pRsp);
3,816✔
987
      break;
3,816✔
988
    case TDMT_VND_AUDIT_RECORD:
244✔
989
      vTrace("vgId:%d, processed audit msg", TD_VID(pVnode));
244✔
990
      code = vnodeProcessAuditRecordReq(pVnode, ver, pReq, len, pMsg);
244✔
991
      pRsp->code = code;
244✔
992
      pRsp->msgType = TDMT_VND_AUDIT_RECORD_RSP;
244✔
993
      pRsp->pCont = NULL;
244✔
994
      pRsp->contLen = 0;
244✔
995
      if (code) {
244✔
996
        goto _err;
×
997
      }
998
      break;
244✔
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);
686,634,162✔
1005

1006
  walApplyVer(pVnode->pWal, ver);
686,653,482✔
1007

1008
  // commit if need
1009
  if (needCommit) {
686,661,833✔
1010
    vInfo("vgId:%d, commit at version %" PRId64, TD_VID(pVnode), ver);
5,236,299✔
1011
    code = vnodeAsyncCommit(pVnode, forceTrimWal);
5,236,299✔
1012
    if (code) {
5,236,299✔
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,236,299✔
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:
686,661,242✔
1028
  return 0;
697,577,386✔
1029

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

1042
int32_t vnodePreprocessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg) {
398,854,118✔
1043
  if (TDMT_SCH_QUERY != pMsg->msgType && TDMT_SCH_MERGE_QUERY != pMsg->msgType) {
398,854,118✔
1044
    return 0;
52,048,618✔
1045
  }
1046

1047
  int32_t qType = 0;
346,825,611✔
1048
  return qWorkerPreprocessQueryMsg(pVnode->pQuery, pMsg, (TDMT_SCH_QUERY == pMsg->msgType), &qType);
346,820,245✔
1049
}
1050

1051
int32_t vnodeProcessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
398,888,288✔
1052
  const STraceId *trace = &pMsg->info.traceId;
398,888,288✔
1053
  vDebug("vgId:%d, msg:%s, %p in vnode query queue is processing, %" PRIx64 ":%" PRIx64, 
398,891,079✔
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)) {
398,904,670✔
1057
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
432,024✔
1058
    return 0;
432,024✔
1059
  }
1060

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

1066
  SReadHandle handle = {0};
398,474,238✔
1067
  handle.vnode = pVnode;
398,472,993✔
1068
  handle.pMsgCb = &pVnode->msgCb;
398,472,993✔
1069
  handle.pWorkerCb = pInfo->workerCb;
398,457,717✔
1070
  initStorageAPI(&handle.api);
398,457,863✔
1071
  int32_t code = TSDB_CODE_SUCCESS;
398,456,302✔
1072
  bool    redirected = false;
398,456,302✔
1073

1074
  switch (pMsg->msgType) {
398,456,302✔
1075
    case TDMT_SCH_QUERY:
285,937,844✔
1076
      if (!syncIsReadyForRead(pVnode->sync)) {
285,937,844✔
1077
        pMsg->code = (terrno) ? terrno : TSDB_CODE_SYN_NOT_LEADER;
2,424,663✔
1078
        redirected = true;
2,424,663✔
1079
      }
1080
      code = qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
285,941,225✔
1081
      if (redirected) {
285,917,033✔
1082
        vnodeRedirectRpcMsg(pVnode, pMsg, pMsg->code);
2,424,663✔
1083
        return 0;
2,424,663✔
1084
      }
1085

1086
      return code;
283,492,370✔
1087
    case TDMT_SCH_MERGE_QUERY:
60,903,838✔
1088
      return qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
60,903,838✔
1089
    case TDMT_SCH_QUERY_CONTINUE:
36,738,406✔
1090
      return qWorkerProcessCQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
36,738,406✔
1091
    case TDMT_VND_TMQ_CONSUME:
14,876,561✔
1092
      return tqProcessPollReq(pVnode->pTq, pMsg);
14,876,561✔
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,027,832,349✔
1100
  int32_t code = TSDB_CODE_SUCCESS;
1,027,832,349✔
1101
  const STraceId *trace = &pMsg->info.traceId;
1,027,832,349✔
1102
  vDebug("vgId:%d, msg:%s, %p in fetch queue is processing, %" PRIx64 ":%" PRIx64, 
1,027,888,798✔
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,027,982,388✔
1105
       pMsg->msgType == TDMT_VND_BATCH_META || pMsg->msgType == TDMT_VND_TABLE_NAME ||
716,563,852✔
1106
       pMsg->msgType == TDMT_VND_VSUBTABLES_META || pMsg->msgType == TDMT_VND_VSTB_REF_DBS) &&
688,456,991✔
1107
      !syncIsReadyForRead(pVnode->sync)) {
339,547,813✔
1108
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
466,100✔
1109
    return 0;
466,100✔
1110
  }
1111

1112
  switch (pMsg->msgType) {
1,027,545,907✔
1113
    case TDMT_SCH_FETCH:
472,014,886✔
1114
    case TDMT_SCH_MERGE_FETCH:
1115
      return qWorkerProcessFetchMsg(pVnode, pVnode->pQuery, pMsg, 0);
472,014,886✔
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:
346,564,195✔
1121
      return qWorkerProcessDropMsg(pVnode, pVnode->pQuery, pMsg, 0);
346,564,195✔
1122
    case TDMT_SCH_TASK_NOTIFY:
36,645✔
1123
      return qWorkerProcessNotifyMsg(pVnode, pVnode->pQuery, pMsg, 0);
36,645✔
1124
    case TDMT_SCH_QUERY_HEARTBEAT:
179,703,788✔
1125
      return qWorkerProcessHbMsg(pVnode, pVnode->pQuery, pMsg, 0);
179,703,788✔
1126
    case TDMT_VND_TABLE_META:
1,280,951✔
1127
    case TDMT_VND_TABLE_NAME:
1128
      return vnodeGetTableMeta(pVnode, pMsg, true);
1,280,951✔
1129
    case TDMT_VND_TABLE_CFG:
×
1130
      return vnodeGetTableCfg(pVnode, pMsg, true);
×
1131
    case TDMT_VND_BATCH_META: {
27,639,184✔
1132
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.fetch_batch_meta_time, METRIC_LEVEL_LOW,
27,639,184✔
1133
                           { code = vnodeGetBatchMeta(pVnode, pMsg); });
1134
      METRICS_UPDATE(pVnode->writeMetrics.fetch_batch_meta_count, METRIC_LEVEL_LOW, 1);
27,640,343✔
1135
      return code;
27,638,535✔
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:
470✔
1142
      return vnodeQueryScanProgress(pVnode, pMsg);
470✔
1143
#ifdef TD_ENTERPRISE
1144
    case TDMT_VND_QUERY_COMPACT_PROGRESS:
117,502✔
1145
      return vnodeQueryCompactProgress(pVnode, pMsg);
117,502✔
1146

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

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

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

1176
  tstrncpy(pMetaRsp->dbFName, pVnode->config.dbname, TSDB_DB_FNAME_LEN);
73,199,947✔
1177
  pMetaRsp->dbId = pVnode->config.dbId;
73,202,888✔
1178
  pMetaRsp->vgId = TD_VID(pVnode);
73,206,006✔
1179
  pMetaRsp->precision = pVnode->config.tsdbCfg.precision;
73,202,166✔
1180
}
1181

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

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

1190
  int32_t     code = 0;
93,257✔
1191
  SVTrimDbReq trimReq = {0};
93,257✔
1192

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

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

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

1204
_exit:
93,257✔
1205
  return code;
93,257✔
1206
}
1207

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1374
    expiredTb.name = p;
49,151✔
1375
    if (mr.me.type == TSDB_CHILD_TABLE) {
49,151✔
1376
      expiredTb.suid = mr.me.ctbEntry.suid;
31,677✔
1377
    }
1378

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

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

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

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

1411
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
5,342,295✔
1412
  pRsp->code = TSDB_CODE_SUCCESS;
5,342,295✔
1413
  pRsp->pCont = NULL;
5,343,037✔
1414
  pRsp->contLen = 0;
5,343,037✔
1415

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

1419
  code = tDecodeSVCreateStbReq(&coder, &req);
5,342,099✔
1420
  if (code) {
5,333,216✔
1421
    pRsp->code = code;
×
1422
    goto _err;
×
1423
  }
1424

1425
  code = metaCreateSuperTable(pVnode->pMeta, ver, &req);
5,333,216✔
1426
  if (code) {
5,343,180✔
1427
    pRsp->code = code;
3,064✔
1428
    goto _err;
3,064✔
1429
  }
1430

1431
  tDecoderClear(&coder);
5,340,116✔
1432
  return 0;
5,340,116✔
1433

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

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

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

1465
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(cRsp));
46,020,037✔
1466
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
46,020,105✔
1467
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
46,017,014✔
1468
  if (rsp.pArray == NULL || tbUids == NULL || tbNames == NULL) {
46,018,281✔
1469
    rcode = -1;
×
1470
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1471
    goto _exit;
×
1472
  }
1473

1474
  // loop to create table
1475
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
99,848,759✔
1476
    pCreateReq = req.pReqs + iReq;
53,828,275✔
1477
    memset(&cRsp, 0, sizeof(cRsp));
53,824,903✔
1478

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

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

1507
    // do create table
1508
    if (metaCreateTable2(pVnode->pMeta, ver, pCreateReq, &cRsp.pMeta) < 0) {
53,815,646✔
1509
      if (pCreateReq->flags & TD_CREATE_IF_NOT_EXISTS && terrno == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
16,325✔
1510
        cRsp.code = TSDB_CODE_SUCCESS;
4,360✔
1511
      } else {
1512
        cRsp.code = terrno;
11,965✔
1513
      }
1514
    } else {
1515
      cRsp.code = TSDB_CODE_SUCCESS;
53,805,949✔
1516
      if (taosArrayPush(tbUids, &pCreateReq->uid) == NULL) {
107,618,958✔
1517
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1518
        rcode = -1;
×
1519
        goto _exit;
×
1520
      }
1521
      vnodeUpdateMetaRsp(pVnode, cRsp.pMeta);
53,813,009✔
1522
    }
1523

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

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

1536
  // prepare rsp
1537
  int32_t ret = 0;
46,022,962✔
1538
  tEncodeSize(tEncodeSVCreateTbBatchRsp, &rsp, pRsp->contLen, ret);
46,022,962✔
1539
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
46,018,180✔
1540
  if (pRsp->pCont == NULL) {
46,017,082✔
1541
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1542
    rcode = -1;
×
1543
    goto _exit;
×
1544
  }
1545
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
46,014,792✔
1546
  if (tEncodeSVCreateTbBatchRsp(&encoder, &rsp) != 0) {
46,018,516✔
1547
    vError("vgId:%d, failed to encode create table batch response", TD_VID(pVnode));
×
1548
  }
1549

1550
  if (tsEnableAudit && tsEnableAuditCreateTable && tsAuditLevel >= AUDIT_LEVEL_CHILDTABLE) {
46,018,172✔
1551
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
125✔
1552

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

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

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

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

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

1581
_exit:
46,018,172✔
1582
  tDeleteSVCreateTbBatchReq(&req);
46,018,276✔
1583
  taosArrayDestroyEx(rsp.pArray, tFreeSVCreateTbRsp);
46,013,060✔
1584
  taosArrayDestroy(tbUids);
46,016,192✔
1585
  tDecoderClear(&decoder);
46,006,702✔
1586
  tEncoderClear(&encoder);
46,019,665✔
1587
  taosArrayDestroyP(tbNames, NULL);
46,018,394✔
1588
  return rcode;
46,018,438✔
1589
}
1590

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

1596
  pRsp->msgType = TDMT_VND_ALTER_STB_RSP;
7,710,285✔
1597
  pRsp->code = TSDB_CODE_SUCCESS;
7,710,956✔
1598
  pRsp->pCont = NULL;
7,710,956✔
1599
  pRsp->contLen = 0;
7,710,956✔
1600

1601
  tDecoderInit(&dc, pReq, len);
7,710,285✔
1602

1603
  // decode req
1604
  code = tDecodeSVCreateStbReq(&dc, &req);
7,710,285✔
1605
  if (code) {
7,704,053✔
1606
    tDecoderClear(&dc);
×
1607
    return code;
×
1608
  }
1609

1610
  code = metaAlterSuperTable(pVnode->pMeta, ver, &req);
7,704,053✔
1611
  if (code) {
7,710,956✔
1612
    pRsp->code = code;
×
1613
    tDecoderClear(&dc);
×
1614
    return code;
×
1615
  }
1616

1617
  tDecoderClear(&dc);
7,710,956✔
1618

1619
  return 0;
7,710,956✔
1620
}
1621

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

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

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

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

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

1644
  // process request
1645
  tbUidList = taosArrayInit(8, sizeof(int64_t));
930,120✔
1646
  if (tbUidList == NULL) goto _exit;
930,120✔
1647
  if (vnodeGetCtbIdList(pVnode, req.suid, tbUidList) != 0){
930,120✔
1648
    rcode = terrno;
×
1649
    goto _exit;
×
1650
  }
1651
  if (metaDropSuperTable(pVnode->pMeta, ver, &req) < 0) {
928,930✔
1652
    rcode = terrno;
1,532✔
1653
    goto _exit;
1,532✔
1654
  }
1655
  if (tqDeleteTbUidList(pVnode->pTq, tbUidList) < 0) {
928,588✔
1656
    rcode = terrno;
×
1657
    goto _exit;
×
1658
  }
1659

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

1670
SArray* getCidList(const SArray* tags) {
7,922,093✔
1671
  int32_t       code = 0;
7,922,093✔
1672
  int32_t       lino = 0;
7,922,093✔
1673
  SArray* cidList = taosArrayInit(taosArrayGetSize(tags), sizeof(col_id_t));
7,922,093✔
1674
  QUERY_CHECK_NULL(cidList, code, lino, end, terrno);
7,922,093✔
1675
  for (int32_t i = 0; i < taosArrayGetSize(tags); i++) {
15,904,611✔
1676
    SUpdatedTagVal *pTagVal = taosArrayGet(tags, i);
7,982,518✔
1677
    QUERY_CHECK_NULL(pTagVal, code, lino, end, terrno);
7,982,518✔
1678
    col_id_t cid = pTagVal->colId;
7,982,518✔
1679
    QUERY_CHECK_NULL(taosArrayPush(cidList, &cid), code, lino, end, terrno);
7,982,518✔
1680
  }
1681

1682
end:
7,922,093✔
1683
  if (code != 0) {
7,922,093✔
1684
    taosArrayDestroy(cidList);
×
1685
    return NULL;
×
1686
  }
1687
  return cidList;
7,922,093✔
1688
}
1689

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

1702
  if (tagsArray != NULL) {
7,910,768✔
1703
    cidListArray = taosArrayInit(taosArrayGetSize(tagsArray), sizeof(SArray*));
7,888,208✔
1704
    QUERY_CHECK_NULL(cidListArray, code, lino, end, terrno);
7,888,208✔
1705

1706
    for (int32_t i = 0; i < taosArrayGetSize(tagsArray); i++) {
15,787,741✔
1707
      SArray   *obj = taosArrayGetP(tagsArray, i);
7,899,533✔
1708
      cids  = getCidList(obj);
7,899,533✔
1709
      QUERY_CHECK_NULL(cids, code, lino, end, terrno);
7,899,533✔
1710
      QUERY_CHECK_NULL(taosArrayPush(cidListArray, &cids), code, lino, end, terrno);
7,899,533✔
1711
      cids = NULL;
7,899,533✔
1712
    }
1713
  }
1714
  vDebug("vgId:%d, try to add %d tables in query table list, cidList size:%"PRIzu,
7,910,768✔
1715
         TD_VID(pVnode), (int32_t)taosArrayGetSize(tbUidList), taosArrayGetSize(cidList));
1716
  code = tqUpdateTbUidList(pVnode->pTq, tbUidList, cidList, cidListArray);
7,910,768✔
1717
  QUERY_CHECK_CODE(code, lino, end);
7,910,768✔
1718

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

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

1739
  pRsp->msgType = TDMT_VND_ALTER_TABLE_RSP;
12,560,785✔
1740
  pRsp->pCont = NULL;
12,560,785✔
1741
  pRsp->contLen = 0;
12,560,785✔
1742
  pRsp->code = TSDB_CODE_SUCCESS;
12,560,785✔
1743

1744
  tDecoderInit(&dc, pReq, len);
12,560,785✔
1745

1746
  // decode
1747
  if (tDecodeSVAlterTbReq(&dc, &vAlterTbReq) < 0) {
12,560,785✔
1748
    vAlterTbRsp.code = TSDB_CODE_INVALID_MSG;
×
1749
    tDecoderClear(&dc);
×
1750
    goto _exit;
×
1751
  }
1752

1753
  // process
1754
  if (metaAlterTable(pVnode->pMeta, ver, &vAlterTbReq, &vMetaRsp) < 0) {
12,560,785✔
1755
    vAlterTbRsp.code = terrno;
587,670✔
1756
    tDecoderClear(&dc);
587,670✔
1757
    goto _exit;
587,670✔
1758
  }
1759
  tDecoderClear(&dc);
11,973,115✔
1760

1761
  if (NULL != vMetaRsp.pSchemas) {
11,973,115✔
1762
    vnodeUpdateMetaRsp(pVnode, &vMetaRsp);
3,853,849✔
1763
    vAlterTbRsp.pMeta = &vMetaRsp;
3,853,849✔
1764
  }
1765

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

1775
  tEncoderClear(&ec);
12,560,785✔
1776
  if (vMetaRsp.pSchemas) {
12,560,785✔
1777
    taosMemoryFree(vMetaRsp.pSchemas);
3,853,849✔
1778
    taosMemoryFree(vMetaRsp.pSchemaExt);
3,853,849✔
1779
  }
1780
  if (vMetaRsp.pColRefs) {
12,560,785✔
1781
    taosMemoryFree(vMetaRsp.pColRefs);
277,537✔
1782
  }
1783
  return 0;
12,560,785✔
1784
}
1785

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

1797
  pRsp->msgType = ((SRpcMsg *)pReq)->msgType + 1;
1,620,951✔
1798
  pRsp->pCont = NULL;
1,620,951✔
1799
  pRsp->contLen = 0;
1,620,951✔
1800
  pRsp->code = TSDB_CODE_SUCCESS;
1,620,951✔
1801

1802
  // decode req
1803
  tDecoderInit(&decoder, pReq, len);
1,620,951✔
1804
  ret = tDecodeSVDropTbBatchReq(&decoder, &req);
1,620,951✔
1805
  if (ret < 0) {
1,620,951✔
1806
    terrno = TSDB_CODE_INVALID_MSG;
×
1807
    pRsp->code = terrno;
×
1808
    goto _exit;
×
1809
  }
1810

1811
  // process req
1812
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
1,620,951✔
1813
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(SVDropTbRsp));
1,620,951✔
1814
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
1,620,951✔
1815
  if (tbUids == NULL || rsp.pArray == NULL || tbNames == NULL) goto _exit;
1,620,951✔
1816

1817
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
3,291,179✔
1818
    SVDropTbReq *pDropTbReq = req.pReqs + iReq;
1,670,228✔
1819
    SVDropTbRsp  dropTbRsp = {0};
1,670,228✔
1820
    tb_uid_t     tbUid = 0;
1,670,228✔
1821

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

1834
    if (taosArrayPush(rsp.pArray, &dropTbRsp) == NULL) {
3,340,456✔
1835
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1836
      pRsp->code = terrno;
×
1837
      goto _exit;
×
1838
    }
1839

1840
    if (taosArrayPush(tbUids, &pDropTbReq->uid) == NULL) {
3,340,456✔
1841
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1842
      pRsp->code = terrno;
×
1843
      goto _exit;
×
1844
    }
1845

1846
    if (tsEnableAudit && tsEnableAuditCreateTable && tsAuditLevel >= AUDIT_LEVEL_CHILDTABLE) {
1,670,228✔
1847
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
×
1848
      if (str == NULL) {
×
1849
        pRsp->code = terrno;
×
1850
        goto _exit;
×
1851
      }
1852
      tstrncpy(str, pDropTbReq->name, TSDB_TABLE_FNAME_LEN);
×
1853
      if (taosArrayPush(tbNames, &str) == NULL) {
×
1854
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1855
        pRsp->code = terrno;
×
1856
        goto _exit;
×
1857
      }
1858
    }
1859
  }
1860

1861
  if (tqDeleteTbUidList(pVnode->pTq, tbUids) < 0) {
1,620,951✔
1862
    vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(terrno));
×
1863
  }
1864

1865
  if (tsEnableAudit && tsEnableAuditCreateTable && tsAuditLevel >= AUDIT_LEVEL_CHILDTABLE) {
1,620,951✔
1866
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
×
1867

1868
    SName name = {0};
×
1869
    if (tNameFromString(&name, pVnode->config.dbname, T_NAME_ACCT | T_NAME_DB) != 0) {
×
1870
      vError("vgId:%d, failed to get name from string", TD_VID(pVnode));
×
1871
    }
1872

1873
    SStringBuilder sb = {0};
×
1874
    for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
×
1875
      char **key = (char **)taosArrayGet(tbNames, iReq);
×
1876
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
×
1877
      if (iReq < req.nReqs - 1) {
×
1878
        taosStringBuilderAppendChar(&sb, ',');
×
1879
      }
1880
      taosMemoryFreeClear(*key);
×
1881
    }
1882

1883
    size_t len = 0;
×
1884
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
×
1885

1886
    if (strlen(RPC_MSG_USER(pOriginRpc)) > 0) {
×
1887
      int64_t tse = taosGetTimestampMs();
×
1888
      double  duration = (double)(tse - tss);
×
1889
      duration = duration / 1000;
×
1890
      auditAddRecord(pOriginRpc, clusterId, "dropTable", name.dbname, "", keyJoined, len, duration, 0);
×
1891
    }
1892

1893
    taosStringBuilderDestroy(&sb);
×
1894
  }
1895

1896
_exit:
1,620,951✔
1897
  taosArrayDestroy(tbUids);
1,620,951✔
1898
  tDecoderClear(&decoder);
1,620,951✔
1899
  tEncodeSize(tEncodeSVDropTbBatchRsp, &rsp, pRsp->contLen, ret);
1,620,951✔
1900
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
1,620,951✔
1901
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
1,620,951✔
1902
  if (tEncodeSVDropTbBatchRsp(&encoder, &rsp) != 0) {
1,620,951✔
1903
    vError("vgId:%d, failed to encode drop table batch response", TD_VID(pVnode));
×
1904
  }
1905
  tEncoderClear(&encoder);
1,620,951✔
1906
  taosArrayDestroy(rsp.pArray);
1,620,951✔
1907
  taosArrayDestroy(tbNames);
1,620,951✔
1908
  return 0;
1,620,951✔
1909
}
1910

1911
static int32_t vnodeProcessCreateRsmaReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
40,012✔
1912
  int32_t         code = 0, lino = 0;
40,012✔
1913
  SVCreateRsmaReq req = {0};
40,012✔
1914
  TAOS_CHECK_EXIT(tDeserializeSVCreateRsmaReq(pReq, len, &req));
40,012✔
1915
  TAOS_CHECK_EXIT(metaCreateRsma(pVnode->pMeta, ver, &req));
40,012✔
1916
_exit:
40,012✔
1917
  pRsp->msgType = TDMT_VND_CREATE_RSMA_RSP;
40,012✔
1918
  pRsp->pCont = NULL;
40,012✔
1919
  pRsp->code = code;
40,012✔
1920
  pRsp->contLen = 0;
40,012✔
1921
  tFreeSVCreateRsmaReq(&req);
40,012✔
1922
  return code;
40,012✔
1923
}
1924

1925
static int32_t vnodeProcessDropRsmaReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
20,140✔
1926
  int32_t       code = 0, lino = 0;
20,140✔
1927
  SVDropRsmaReq req = {0};
20,140✔
1928
  TAOS_CHECK_EXIT(tDeserializeSVDropRsmaReq(pReq, len, &req));
20,140✔
1929
  TAOS_CHECK_EXIT(metaDropRsma(pVnode->pMeta, ver, &req));
20,140✔
1930
_exit:
20,140✔
1931
  pRsp->msgType = TDMT_VND_DROP_RSMA_RSP;
20,140✔
1932
  pRsp->pCont = NULL;
20,140✔
1933
  pRsp->code = code;
20,140✔
1934
  pRsp->contLen = 0;
20,140✔
1935
  return 0;
20,140✔
1936
}
1937

1938
static int32_t vnodeProcessAlterRsmaReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
12,332✔
1939
  int32_t         code = 0, lino = 0;
12,332✔
1940
  SVAlterRsmaReq req = {0};
12,332✔
1941
  TAOS_CHECK_EXIT(tDeserializeSVAlterRsmaReq(pReq, len, &req));
12,332✔
1942
  TAOS_CHECK_EXIT(metaAlterRsma(pVnode->pMeta, ver, &req));
12,332✔
1943
_exit:
12,332✔
1944
  pRsp->msgType = TDMT_VND_ALTER_RSMA_RSP;
12,332✔
1945
  pRsp->pCont = NULL;
12,332✔
1946
  pRsp->code = code;
12,332✔
1947
  pRsp->contLen = 0;
12,332✔
1948
  tFreeSVAlterRsmaReq(&req);
12,332✔
1949
  return code;
12,332✔
1950
}
1951

1952
#ifdef BUILD_NO_CALL
1953
static int32_t vnodeDebugPrintSingleSubmitMsg(SMeta *pMeta, SSubmitBlk *pBlock, SSubmitMsgIter *msgIter,
1954
                                              const char *tags) {
1955
  SSubmitBlkIter blkIter = {0};
1956
  STSchema      *pSchema = NULL;
1957
  tb_uid_t       suid = 0;
1958
  STSRow        *row = NULL;
1959
  int32_t        rv = -1;
1960

1961
  tInitSubmitBlkIter(msgIter, pBlock, &blkIter);
1962
  if (blkIter.row == NULL) return 0;
1963

1964
  int32_t code = metaGetTbTSchemaNotNull(pMeta, msgIter->suid, TD_ROW_SVER(blkIter.row), 1,
1965
                                         &pSchema);  // TODO: use the real schema
1966
  if (TSDB_CODE_SUCCESS != code) {
1967
    printf("%s:%d no valid schema\n", tags, __LINE__);
1968
    return code;
1969
  }
1970

1971
  suid = msgIter->suid;
1972
  rv = TD_ROW_SVER(blkIter.row);
1973

1974
  char __tags[128] = {0};
1975
  snprintf(__tags, 128, "%s: uid %" PRIi64 " ", tags, msgIter->uid);
1976
  while ((row = tGetSubmitBlkNext(&blkIter))) {
1977
    tdSRowPrint(row, pSchema, __tags);
1978
  }
1979

1980
  taosMemoryFreeClear(pSchema);
1981

1982
  return TSDB_CODE_SUCCESS;
1983
}
1984
#endif
1985
typedef struct SSubmitReqConvertCxt {
1986
  SSubmitMsgIter msgIter;
1987
  SSubmitBlk    *pBlock;
1988
  SSubmitBlkIter blkIter;
1989
  STSRow        *pRow;
1990
  STSRowIter     rowIter;
1991
  SSubmitTbData *pTbData;
1992
  STSchema      *pTbSchema;
1993
  SArray        *pColValues;
1994
} SSubmitReqConvertCxt;
1995

1996
static int32_t vnodeResetTableCxt(SMeta *pMeta, SSubmitReqConvertCxt *pCxt) {
×
1997
  taosMemoryFreeClear(pCxt->pTbSchema);
×
1998
  int32_t code = metaGetTbTSchemaNotNull(pMeta, pCxt->msgIter.suid > 0 ? pCxt->msgIter.suid : pCxt->msgIter.uid,
×
1999
                                         pCxt->msgIter.sversion, 1, &pCxt->pTbSchema);
2000
  if (TSDB_CODE_SUCCESS != code) {
×
2001
    return code;
×
2002
  }
2003
  tdSTSRowIterInit(&pCxt->rowIter, pCxt->pTbSchema);
×
2004

2005
  tDestroySubmitTbData(pCxt->pTbData, TSDB_MSG_FLG_ENCODE);
×
2006
  if (NULL == pCxt->pTbData) {
×
2007
    pCxt->pTbData = taosMemoryCalloc(1, sizeof(SSubmitTbData));
×
2008
    if (NULL == pCxt->pTbData) {
×
2009
      return terrno;
×
2010
    }
2011
  }
2012
  pCxt->pTbData->flags = 0;
×
2013
  pCxt->pTbData->suid = pCxt->msgIter.suid;
×
2014
  pCxt->pTbData->uid = pCxt->msgIter.uid;
×
2015
  pCxt->pTbData->sver = pCxt->msgIter.sversion;
×
2016
  pCxt->pTbData->pCreateTbReq = NULL;
×
2017
  pCxt->pTbData->aRowP = taosArrayInit(128, POINTER_BYTES);
×
2018
  if (NULL == pCxt->pTbData->aRowP) {
×
2019
    return terrno;
×
2020
  }
2021

2022
  taosArrayDestroy(pCxt->pColValues);
×
2023
  pCxt->pColValues = taosArrayInit(pCxt->pTbSchema->numOfCols, sizeof(SColVal));
×
2024
  if (NULL == pCxt->pColValues) {
×
2025
    return terrno;
×
2026
  }
2027
  for (int32_t i = 0; i < pCxt->pTbSchema->numOfCols; ++i) {
×
2028
    SColVal val = COL_VAL_NONE(pCxt->pTbSchema->columns[i].colId, pCxt->pTbSchema->columns[i].type);
×
2029
    if (taosArrayPush(pCxt->pColValues, &val) == NULL) {
×
2030
      return terrno;
×
2031
    }
2032
  }
2033

2034
  return TSDB_CODE_SUCCESS;
×
2035
}
2036

2037
static void vnodeDestroySubmitReqConvertCxt(SSubmitReqConvertCxt *pCxt) {
×
2038
  taosMemoryFreeClear(pCxt->pTbSchema);
×
2039
  tDestroySubmitTbData(pCxt->pTbData, TSDB_MSG_FLG_ENCODE);
×
2040
  taosMemoryFreeClear(pCxt->pTbData);
×
2041
  taosArrayDestroy(pCxt->pColValues);
×
2042
}
×
2043

2044
static int32_t vnodeCellValConvertToColVal(STColumn *pCol, SCellVal *pCellVal, SColVal *pColVal) {
×
2045
  if (tdValTypeIsNone(pCellVal->valType)) {
×
2046
    pColVal->flag = CV_FLAG_NONE;
×
2047
    return TSDB_CODE_SUCCESS;
×
2048
  }
2049

2050
  if (tdValTypeIsNull(pCellVal->valType)) {
×
2051
    pColVal->flag = CV_FLAG_NULL;
×
2052
    return TSDB_CODE_SUCCESS;
×
2053
  }
2054

2055
  if (IS_VAR_DATA_TYPE(pCol->type)) {
×
2056
    if (IS_STR_DATA_BLOB(pCol->type)) {
×
2057
      pColVal->value.nData = blobDataLen(pCellVal->val);
×
2058
      pColVal->value.pData = (uint8_t *)blobDataVal(pCellVal->val);
×
2059

2060
    } else {
2061
      pColVal->value.nData = varDataLen(pCellVal->val);
×
2062
      pColVal->value.pData = (uint8_t *)varDataVal(pCellVal->val);
×
2063
    }
2064
  } else if (TSDB_DATA_TYPE_FLOAT == pCol->type) {
×
2065
    float f = GET_FLOAT_VAL(pCellVal->val);
×
2066
    valueSetDatum(&pColVal->value, pCol->type, &f, sizeof(f));
×
2067
  } else if (TSDB_DATA_TYPE_DOUBLE == pCol->type) {
×
2068
    taosSetPInt64Aligned(&pColVal->value.val, (int64_t *)pCellVal->val);
×
2069
  } else {
2070
    valueSetDatum(&pColVal->value, pCol->type, pCellVal->val, tDataTypes[pCol->type].bytes);
×
2071
  }
2072

2073
  pColVal->flag = CV_FLAG_VALUE;
×
2074
  return TSDB_CODE_SUCCESS;
×
2075
}
2076

2077
static int32_t vnodeTSRowConvertToColValArray(SSubmitReqConvertCxt *pCxt) {
×
2078
  int32_t code = TSDB_CODE_SUCCESS;
×
2079
  tdSTSRowIterReset(&pCxt->rowIter, pCxt->pRow);
×
2080
  for (int32_t i = 0; TSDB_CODE_SUCCESS == code && i < pCxt->pTbSchema->numOfCols; ++i) {
×
2081
    STColumn *pCol = pCxt->pTbSchema->columns + i;
×
2082
    SCellVal  cellVal = {0};
×
2083
    if (!tdSTSRowIterFetch(&pCxt->rowIter, pCol->colId, pCol->type, &cellVal)) {
×
2084
      break;
×
2085
    }
2086
    code = vnodeCellValConvertToColVal(pCol, &cellVal, (SColVal *)taosArrayGet(pCxt->pColValues, i));
×
2087
  }
2088
  return code;
×
2089
}
2090

2091
static int32_t vnodeDecodeCreateTbReq(SSubmitReqConvertCxt *pCxt) {
×
2092
  if (pCxt->msgIter.schemaLen <= 0) {
×
2093
    return TSDB_CODE_SUCCESS;
×
2094
  }
2095

2096
  pCxt->pTbData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
×
2097
  if (NULL == pCxt->pTbData->pCreateTbReq) {
×
2098
    return terrno;
×
2099
  }
2100

2101
  SDecoder decoder = {0};
×
2102
  tDecoderInit(&decoder, (uint8_t *)pCxt->pBlock->data, pCxt->msgIter.schemaLen);
×
2103
  int32_t code = tDecodeSVCreateTbReq(&decoder, pCxt->pTbData->pCreateTbReq);
×
2104
  tDecoderClear(&decoder);
×
2105

2106
  return code;
×
2107
}
2108

2109
static int32_t vnodeSubmitReqConvertToSubmitReq2(SVnode *pVnode, SSubmitReq *pReq, SSubmitReq2 *pReq2) {
×
2110
  pReq2->aSubmitTbData = taosArrayInit(128, sizeof(SSubmitTbData));
×
2111
  if (NULL == pReq2->aSubmitTbData) {
×
2112
    return terrno;
×
2113
  }
2114

2115
  SSubmitReqConvertCxt cxt = {0};
×
2116

2117
  int32_t code = tInitSubmitMsgIter(pReq, &cxt.msgIter);
×
2118
  while (TSDB_CODE_SUCCESS == code) {
×
2119
    code = tGetSubmitMsgNext(&cxt.msgIter, &cxt.pBlock);
×
2120
    if (TSDB_CODE_SUCCESS == code) {
×
2121
      if (NULL == cxt.pBlock) {
×
2122
        break;
×
2123
      }
2124
      code = vnodeResetTableCxt(pVnode->pMeta, &cxt);
×
2125
    }
2126
    if (TSDB_CODE_SUCCESS == code) {
×
2127
      code = tInitSubmitBlkIter(&cxt.msgIter, cxt.pBlock, &cxt.blkIter);
×
2128
    }
2129
    if (TSDB_CODE_SUCCESS == code) {
×
2130
      code = vnodeDecodeCreateTbReq(&cxt);
×
2131
    }
2132
    while (TSDB_CODE_SUCCESS == code && (cxt.pRow = tGetSubmitBlkNext(&cxt.blkIter)) != NULL) {
×
2133
      code = vnodeTSRowConvertToColValArray(&cxt);
×
2134
      if (TSDB_CODE_SUCCESS == code) {
×
2135
        SRow **pNewRow = taosArrayReserve(cxt.pTbData->aRowP, 1);
×
2136

2137
        SRowBuildScanInfo sinfo = {0};
×
2138
        code = tRowBuild(cxt.pColValues, cxt.pTbSchema, pNewRow, &sinfo);
×
2139
      }
2140
    }
2141
    if (TSDB_CODE_SUCCESS == code) {
×
2142
      code = (NULL == taosArrayPush(pReq2->aSubmitTbData, cxt.pTbData) ? terrno : TSDB_CODE_SUCCESS);
×
2143
    }
2144
    if (TSDB_CODE_SUCCESS == code) {
×
2145
      taosMemoryFreeClear(cxt.pTbData);
×
2146
    }
2147
  }
2148

2149
  vnodeDestroySubmitReqConvertCxt(&cxt);
×
2150
  return code;
×
2151
}
2152

2153
static int32_t vnodeRebuildSubmitReqMsg(SSubmitReq2 *pSubmitReq, void **ppMsg) {
×
2154
  int32_t  code = TSDB_CODE_SUCCESS;
×
2155
  char    *pMsg = NULL;
×
2156
  uint32_t msglen = 0;
×
2157
  tEncodeSize(tEncodeSubmitReq, pSubmitReq, msglen, code);
×
2158
  if (TSDB_CODE_SUCCESS == code) {
×
2159
    pMsg = taosMemoryMalloc(msglen);
×
2160
    if (NULL == pMsg) {
×
2161
      code = terrno;
×
2162
    }
2163
  }
2164
  if (TSDB_CODE_SUCCESS == code) {
×
2165
    SEncoder encoder;
×
2166
    tEncoderInit(&encoder, (uint8_t *)pMsg, msglen);
×
2167
    code = tEncodeSubmitReq(&encoder, pSubmitReq);
×
2168
    tEncoderClear(&encoder);
×
2169
  }
2170
  if (TSDB_CODE_SUCCESS == code) {
×
2171
    *ppMsg = pMsg;
×
2172
  }
2173
  return code;
×
2174
}
2175

2176
static int32_t buildExistSubTalbeRsp(SVnode *pVnode, SSubmitTbData *pSubmitTbData, STableMetaRsp **ppRsp) {
62,776✔
2177
  int32_t code = 0;
62,776✔
2178
  int32_t lino = 0;
62,776✔
2179

2180
  SMetaEntry *pEntry = NULL;
62,776✔
2181
  code = metaFetchEntryByUid(pVnode->pMeta, pSubmitTbData->suid, &pEntry);
62,776✔
2182
  if (code) {
62,776✔
2183
    vError("vgId:%d, table uid:%" PRId64 " not exists, line:%d", TD_VID(pVnode), pSubmitTbData->uid, __LINE__);
×
2184
    TSDB_CHECK_CODE(code, lino, _exit);
×
2185
  }
2186
  if (pEntry->type != TSDB_SUPER_TABLE) {
62,776✔
2187
    vError("vgId:%d, table uid:%" PRId64 " exists, but is not super table, line:%d", TD_VID(pVnode), pSubmitTbData->uid,
×
2188
           __LINE__);
2189
    code = TSDB_CODE_STREAM_INSERT_SCHEMA_NOT_MATCH;
×
2190
    TSDB_CHECK_CODE(code, lino, _exit);
×
2191
  }
2192

2193
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
62,776✔
2194
  if (NULL == *ppRsp) {
62,776✔
2195
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2196
  }
2197
  (*ppRsp)->suid = pSubmitTbData->suid;
62,776✔
2198
  (*ppRsp)->tuid = pSubmitTbData->uid;
62,776✔
2199
  (*ppRsp)->sversion = pEntry->stbEntry.schemaRow.version;
62,776✔
2200
  (*ppRsp)->vgId = pVnode->config.vgId;
62,776✔
2201
  (*ppRsp)->numOfColumns = pEntry->stbEntry.schemaRow.nCols;
62,776✔
2202
  (*ppRsp)->numOfTags = pEntry->stbEntry.schemaTag.nCols;
62,776✔
2203
  (*ppRsp)->pSchemas =
62,776✔
2204
      taosMemoryCalloc(pEntry->stbEntry.schemaRow.nCols + pEntry->stbEntry.schemaTag.nCols, sizeof(SSchema));
62,776✔
2205
  if (NULL == (*ppRsp)->pSchemas) {
62,776✔
2206
    taosMemoryFree(*ppRsp);
×
2207
    *ppRsp = NULL;
×
2208
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2209
  }
2210
  memcpy((*ppRsp)->pSchemas, pEntry->stbEntry.schemaRow.pSchema, pEntry->stbEntry.schemaRow.nCols * sizeof(SSchema));
62,776✔
2211
  memcpy((*ppRsp)->pSchemas + pEntry->stbEntry.schemaRow.nCols, pEntry->stbEntry.schemaTag.pSchema,
62,776✔
2212
         pEntry->stbEntry.schemaTag.nCols * sizeof(SSchema));
62,776✔
2213
  if (pEntry->pExtSchemas != NULL) {
62,776✔
2214
    (*ppRsp)->pSchemaExt = taosMemoryCalloc(pEntry->colCmpr.nCols, sizeof(SExtSchema));
×
2215
    if (NULL == (*ppRsp)->pSchemaExt) {
×
2216
      taosMemoryFree((*ppRsp)->pSchemas);
×
2217
      taosMemoryFree(*ppRsp);
×
2218
      *ppRsp = NULL;
×
2219
      TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2220
    }
2221
    memcpy((*ppRsp)->pSchemaExt, pEntry->pExtSchemas, pEntry->colCmpr.nCols * sizeof(SExtSchema));
×
2222
  }
2223

2224
  if (pEntry->stbEntry.schemaRow.version != pSubmitTbData->sver) {
62,776✔
2225
    TSDB_CHECK_CODE(code = TSDB_CODE_TDB_TABLE_ALREADY_EXIST, lino, _exit);
470✔
2226
  }
2227
_exit:
62,776✔
2228
  metaFetchEntryFree(&pEntry);
62,776✔
2229
  if (code != TSDB_CODE_SUCCESS) {
62,776✔
2230
    vError("vgId:%d, failed to build exist sub table response, code:0x%0x, line:%d", TD_VID(pVnode), code, lino);
470✔
2231
  }
2232
  return code;
62,776✔
2233
}
2234

2235
static int32_t buildExistNormalTalbeRsp(SVnode *pVnode, int64_t uid, STableMetaRsp **ppRsp) {
30,460✔
2236
  int32_t code = 0;
30,460✔
2237
  int32_t lino = 0;
30,460✔
2238

2239
  SMetaEntry *pEntry = NULL;
30,460✔
2240
  code = metaFetchEntryByUid(pVnode->pMeta, uid, &pEntry);
30,460✔
2241
  if (code) {
30,460✔
2242
    vError("vgId:%d, table uid:%" PRId64 " not exists, line:%d", TD_VID(pVnode), uid, __LINE__);
×
2243
    TSDB_CHECK_CODE(code, lino, _exit);
×
2244
  }
2245
  if (pEntry->type != TSDB_NORMAL_TABLE) {
30,460✔
2246
    vError("vgId:%d, table uid:%" PRId64 " exists, but is not normal table, line:%d", TD_VID(pVnode), uid, __LINE__);
×
2247
    code = TSDB_CODE_STREAM_INSERT_SCHEMA_NOT_MATCH;
×
2248
    TSDB_CHECK_CODE(code, lino, _exit);
×
2249
  }
2250

2251
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
30,460✔
2252
  if (NULL == *ppRsp) {
30,460✔
2253
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2254
  }
2255

2256
  (*ppRsp)->tuid = pEntry->uid;
30,460✔
2257
  (*ppRsp)->vgId = pVnode->config.vgId;
30,460✔
2258
  (*ppRsp)->sversion = pEntry->ntbEntry.schemaRow.version;
30,460✔
2259
  (*ppRsp)->numOfColumns = pEntry->ntbEntry.schemaRow.nCols;
30,460✔
2260
  (*ppRsp)->pSchemas = taosMemoryCalloc(pEntry->ntbEntry.schemaRow.nCols, sizeof(SSchema));
30,460✔
2261
  if (NULL == (*ppRsp)->pSchemas) {
30,460✔
2262
    taosMemoryFree(*ppRsp);
×
2263
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2264
  }
2265
  memcpy((*ppRsp)->pSchemas, pEntry->ntbEntry.schemaRow.pSchema, pEntry->ntbEntry.schemaRow.nCols * sizeof(SSchema));
30,460✔
2266
  if (pEntry->pExtSchemas != NULL) {
30,460✔
2267
    (*ppRsp)->pSchemaExt = taosMemoryCalloc(pEntry->ntbEntry.schemaRow.nCols, sizeof(SSchemaExt));
×
2268
    if (NULL == (*ppRsp)->pSchemaExt) {
×
2269
      taosMemoryFree((*ppRsp)->pSchemas);
×
2270
      taosMemoryFree(*ppRsp);
×
2271
      TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2272
    }
2273
    memcpy((*ppRsp)->pSchemaExt, pEntry->pExtSchemas, pEntry->ntbEntry.schemaRow.nCols * sizeof(SSchemaExt));
×
2274
  }
2275

2276
_exit:
30,460✔
2277
  metaFetchEntryFree(&pEntry);
30,460✔
2278
  if (code != TSDB_CODE_SUCCESS) {
30,460✔
2279
    vError("vgId:%d, failed to build exist normal table response, code:%d, line:%d", TD_VID(pVnode), code, lino);
×
2280
  }
2281
  return code;
30,460✔
2282
}
2283

2284
static int32_t buildExistTableInStreamRsp(SVnode *pVnode, SSubmitTbData *pSubmitTbData, STableMetaRsp **ppRsp) {
300,386✔
2285
  if (pSubmitTbData->pCreateTbReq->flags & TD_CREATE_NORMAL_TB_IN_STREAM) {
300,386✔
2286
    int32_t code = buildExistNormalTalbeRsp(pVnode, pSubmitTbData->uid, ppRsp);
30,460✔
2287
    if (code) {
30,460✔
2288
      vError("vgId:%d, table uid:%" PRId64 " not exists, line:%d", TD_VID(pVnode), pSubmitTbData->uid, __LINE__);
×
2289
      return code;
×
2290
    }
2291
    return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
30,460✔
2292
  } else if (pSubmitTbData->pCreateTbReq->flags & TD_CREATE_SUB_TB_IN_STREAM) {
269,926✔
2293
    return buildExistSubTalbeRsp(pVnode, pSubmitTbData, ppRsp);
62,776✔
2294
  }
2295
  return TSDB_CODE_SUCCESS;
207,150✔
2296
}
2297

2298
static int32_t vnodeHandleAutoCreateTable(SVnode      *pVnode,    // vnode
589,163,132✔
2299
                                          int64_t      version,   // version
2300
                                          SSubmitReq2 *pRequest,  // request
2301
                                          SSubmitRsp2 *pResponse  // response
2302
) {
2303
  int32_t code = TSDB_CODE_SUCCESS;
589,163,132✔
2304
  int32_t numTbData = taosArrayGetSize(pRequest->aSubmitTbData);
589,163,132✔
2305
  SArray *newTbUids = NULL;
589,163,810✔
2306

2307
  for (int32_t i = 0; i < numTbData; ++i) {
1,203,871,993✔
2308
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
614,741,205✔
2309

2310
    if (pTbData->pCreateTbReq == NULL) {
614,741,145✔
2311
      continue;
598,847,623✔
2312
    }
2313

2314
    pTbData->uid = pTbData->pCreateTbReq->uid;
15,893,582✔
2315

2316
    // Alloc necessary resources
2317
    if (pResponse->aCreateTbRsp == NULL) {
15,893,582✔
2318
      pResponse->aCreateTbRsp = taosArrayInit(numTbData, sizeof(SVCreateTbRsp));
12,972,974✔
2319
      if (pResponse->aCreateTbRsp == NULL) {
12,972,974✔
2320
        code = terrno;
×
2321
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2322
               tstrerror(code), version);
2323
        taosArrayDestroy(newTbUids);
×
2324
        return code;
×
2325
      }
2326
    }
2327

2328
    // Do create table
2329
    vDebug("vgId:%d start to handle auto create table, version:%" PRId64, TD_VID(pVnode), version);
15,893,582✔
2330

2331
    SVCreateTbRsp *pCreateTbRsp = taosArrayReserve(pResponse->aCreateTbRsp, 1);
15,893,582✔
2332
    code = metaCreateTable2(pVnode->pMeta, version, pTbData->pCreateTbReq, &pCreateTbRsp->pMeta);
15,893,582✔
2333
    if (code == TSDB_CODE_SUCCESS) {
15,893,541✔
2334
      // Allocate necessary resources
2335
      if (newTbUids == NULL) {
15,538,537✔
2336
        newTbUids = taosArrayInit(numTbData, sizeof(int64_t));
12,672,698✔
2337
        if (newTbUids == NULL) {
12,672,698✔
2338
          code = terrno;
×
2339
          vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2340
                 tstrerror(code), version);
2341
          return code;
×
2342
        }
2343
      }
2344

2345
      if (taosArrayPush(newTbUids, &pTbData->uid) == NULL) {
31,076,421✔
2346
        code = terrno;
×
2347
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2348
               tstrerror(code), version);
2349
        taosArrayDestroy(newTbUids);
×
2350
        return code;
×
2351
      }
2352

2353
      if (pCreateTbRsp->pMeta) {
15,537,884✔
2354
        vnodeUpdateMetaRsp(pVnode, pCreateTbRsp->pMeta);
15,538,537✔
2355
      }
2356
    } else if (code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
355,004✔
2357
      code = terrno = 0;
353,928✔
2358
      pTbData->uid = pTbData->pCreateTbReq->uid;  // update uid if table exist for using below
353,928✔
2359

2360
      // stream: get sver from meta, write to pCreateTbRsp, and need to check crateTbReq is same as meta.
2361
      if (i == 0) {
353,928✔
2362
        // In the streaming scenario, multiple grouped req requests will only operate on the same write table, and
2363
        // only the first one needs to be processed.
2364
        code = buildExistTableInStreamRsp(pVnode, pTbData, &pCreateTbRsp->pMeta);
300,386✔
2365
        if (code) {
300,386✔
2366
          vInfo("vgId:%d failed to create table in stream:%s, code(0x%0x):%s", TD_VID(pVnode),
30,930✔
2367
                pTbData->pCreateTbReq->name, code, tstrerror(code));
2368
          taosArrayDestroy(newTbUids);
30,930✔
2369
          return code;
30,930✔
2370
        }
2371
      }
2372
    } else {
2373
      code = terrno;
1,076✔
2374
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
1,076✔
2375
             tstrerror(code), version);
2376
      taosArrayDestroy(newTbUids);
1,076✔
2377
      return code;
1,076✔
2378
    }
2379
  }
2380

2381
  // Update the affected table uid list
2382
  if (taosArrayGetSize(newTbUids) > 0) {
589,130,788✔
2383
    vDebug("vgId:%d, add %d table into query table list in handling submit", TD_VID(pVnode),
12,672,021✔
2384
           (int32_t)taosArrayGetSize(newTbUids));
2385
    if (tqAddTbUidList(pVnode->pTq, newTbUids) != 0) {
12,673,376✔
2386
      vError("vgId:%d, failed to update tbUid list", TD_VID(pVnode));
×
2387
    }
2388
  }
2389

2390
  vDebug("vgId:%d, handle auto create table done, version:%" PRId64, TD_VID(pVnode), version);
589,130,937✔
2391

2392
  taosArrayDestroy(newTbUids);
589,131,126✔
2393
  return code;
589,131,126✔
2394
}
2395

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

2426
  TSDB_CHECK_CODE(code, lino, _exit);
×
2427
_exit:
×
2428
  if (code != TSDB_CODE_SUCCESS) {
×
2429
    vError("vgId:%d, failed to add exist table info into response, code:0x%0x, line:%d", TD_VID(pVnode), code, lino);
×
2430
  }
2431
  return;
×
2432
}
2433

2434
static int32_t vnodeHandleDataWrite(SVnode *pVnode, int64_t version, SSubmitReq2 *pRequest, SSubmitRsp2 *pResponse) {
589,131,096✔
2435
  int32_t code = TSDB_CODE_SUCCESS;
589,131,096✔
2436
  int32_t numTbData = taosArrayGetSize(pRequest->aSubmitTbData);
589,131,096✔
2437
  int8_t  hasBlob = 0;
589,131,066✔
2438

2439
  // Scan submit data
2440
  for (int32_t i = 0; i < numTbData; ++i) {
1,203,837,882✔
2441
    SMetaInfo      info = {0};
614,709,127✔
2442
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
614,708,629✔
2443

2444
    if (pTbData->flags & SUBMIT_REQ_WITH_BLOB) {
614,709,157✔
2445
      hasBlob = 1;
45,901✔
2446
    }
2447
    if (pTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
614,709,157✔
2448
      continue;  // skip column data format
6,899,950✔
2449
    }
2450
    if (pTbData->flags & SUBMIT_REQ_ONLY_CREATE_TABLE) {
607,809,177✔
2451
      continue;  // skip only crate table request
2,417,020✔
2452
    }
2453

2454
    code = metaGetInfo(pVnode->pMeta, pTbData->uid, &info, NULL);
605,391,570✔
2455
    if (code) {
605,391,443✔
2456
      code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
1,811✔
2457
      vWarn("vgId:%d, error occurred at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __FILE__,
1,811✔
2458
            __LINE__, tstrerror(code), version, pTbData->uid);
2459
      return code;
1,811✔
2460
    }
2461

2462
    if (info.suid != pTbData->suid) {
605,389,632✔
2463
      code = TSDB_CODE_INVALID_MSG;
×
2464
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64 " suid:%" PRId64
×
2465
             " info.suid:%" PRId64,
2466
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), version, pTbData->uid, pTbData->suid,
2467
             info.suid);
2468
      return code;
×
2469
    }
2470

2471
    if (info.suid) {
605,389,066✔
2472
      code = metaGetInfo(pVnode->pMeta, info.suid, &info, NULL);
458,207,827✔
2473
      if (code) {
458,209,397✔
2474
        code = TSDB_CODE_INTERNAL_ERROR;
×
2475
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " suid:%" PRId64, TD_VID(pVnode), __func__,
×
2476
               __FILE__, __LINE__, tstrerror(code), version, info.suid);
2477
        return code;
×
2478
      }
2479
    }
2480

2481
    if (pTbData->sver != info.skmVer) {
605,390,636✔
2482
      code = TSDB_CODE_TDB_INVALID_TABLE_SCHEMA_VER;
470✔
2483
      addExistTableInfoIntoRes(pVnode, pRequest, pResponse, pTbData, numTbData);
470✔
2484
      vDebug("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64
470✔
2485
             " sver:%d"
2486
             " info.skmVer:%d",
2487
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), version, pTbData->uid, pTbData->sver,
2488
             info.skmVer);
2489
      return code;
470✔
2490
    }
2491
  }
2492

2493
  // Do write data
2494
  vDebug("vgId:%d start to handle data write, version:%" PRId64, TD_VID(pVnode), version);
589,128,755✔
2495

2496
  for (int32_t i = 0; i < numTbData; ++i) {
1,203,822,720✔
2497
    int32_t        affectedRows = 0;
614,704,600✔
2498
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
614,704,922✔
2499

2500
    if (pTbData->flags & SUBMIT_REQ_ONLY_CREATE_TABLE) {
614,704,797✔
2501
      continue;
2,417,020✔
2502
    }
2503

2504
    if (hasBlob) {
612,287,818✔
2505
      code = vnodeSubmitBlobData(pVnode, pTbData);
45,901✔
2506
      if (code) {
45,901✔
2507
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2508
               __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2509
        return code;
×
2510
      }
2511
    }
2512

2513
    code = tsdbInsertTableData(pVnode->pTsdb, version, pTbData, &affectedRows);
612,287,818✔
2514
    if (code) {
612,267,802✔
2515
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2516
             __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2517
      return code;
×
2518
    }
2519

2520
    code = metaUpdateChangeTimeWithLock(pVnode->pMeta, pTbData->uid, pTbData->ctimeMs);
612,267,802✔
2521
    if (code) {
612,261,221✔
2522
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2523
             __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2524
      return code;
×
2525
    }
2526
    pResponse->affectedRows += affectedRows;
612,261,221✔
2527
  }
2528

2529
  vDebug("vgId:%d, handle data write done, version:%" PRId64 ", affectedRows:%d", TD_VID(pVnode), version,
589,118,120✔
2530
         pResponse->affectedRows);
2531
  return code;
589,128,845✔
2532
}
2533

2534
static int32_t vnodeScanColumnData(SVnode *pVnode, SSubmitTbData *pTbData, TSKEY minKey, TSKEY maxKey) {
6,898,258✔
2535
  int32_t code = 0;
6,898,258✔
2536

2537
  int32_t   numCols = taosArrayGetSize(pTbData->aCol);
6,898,258✔
2538
  SColData *aColData = (SColData *)TARRAY_DATA(pTbData->aCol);
6,897,582✔
2539

2540
  if (numCols <= 0) {
6,895,552✔
2541
    code = TSDB_CODE_INVALID_MSG;
×
2542
    vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " numCols:%d", TD_VID(pVnode), __func__,
×
2543
           __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, numCols);
2544
    return code;
×
2545
  }
2546

2547
  if (aColData[0].cid != PRIMARYKEY_TIMESTAMP_COL_ID || aColData[0].type != TSDB_DATA_TYPE_TIMESTAMP ||
6,895,552✔
2548
      aColData[0].nVal <= 0) {
6,898,934✔
2549
    code = TSDB_CODE_INVALID_MSG;
×
2550
    vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64
×
2551
           " first column is not primary key timestamp, cid:%d type:%d nVal:%d",
2552
           TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, aColData[0].cid,
2553
           aColData[0].type, aColData[0].nVal);
2554
    return code;
×
2555
  }
2556

2557
  for (int32_t i = 1; i < numCols; ++i) {
21,163,044✔
2558
    if (aColData[i].nVal != aColData[0].nVal) {
14,265,122✔
2559
      code = TSDB_CODE_INVALID_MSG;
×
2560
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64
×
2561
             " column cid:%d type:%d nVal:%d is not equal to primary key timestamp nVal:%d",
2562
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid,
2563
             aColData[i].cid, aColData[i].type, aColData[i].nVal, aColData[0].nVal);
2564
      return code;
×
2565
    }
2566
  }
2567

2568
  SRowKey *pLastKey = NULL;
6,897,922✔
2569
  SRowKey  lastKey = {0};
6,897,922✔
2570
  for (int32_t i = 0; i < aColData[0].nVal; ++i) {
2,147,483,647✔
2571
    SRowKey key = {0};
2,147,483,647✔
2572

2573
    tColDataArrGetRowKey(aColData, numCols, i, &key);
2,147,483,647✔
2574

2575
    if (key.ts < minKey || key.ts > maxKey) {
2,147,483,647✔
2576
      code = TSDB_CODE_INVALID_MSG;
×
2577
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] key:%" PRId64
×
2578
             " is out of range [%" PRId64 ", %" PRId64 "]",
2579
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, i, key.ts,
2580
             minKey, maxKey);
2581
      return code;
×
2582
    }
2583

2584
    if (pLastKey && tRowKeyCompare(pLastKey, &key) >= 0) {
2,147,483,647✔
2585
      code = TSDB_CODE_INVALID_MSG;
×
2586
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] key:%" PRId64
×
2587
             " is not in order, lastKey:%" PRId64,
2588
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, i, key.ts,
2589
             pLastKey->ts);
2590
      return code;
×
2591
    } else if (pLastKey == NULL) {
2,147,483,647✔
2592
      pLastKey = &lastKey;
6,895,887✔
2593
    }
2594

2595
    *pLastKey = key;
2,147,483,647✔
2596
  }
2597

2598
  return code;
6,899,950✔
2599
}
2600

2601
static int32_t vnodeScanSubmitRowData(SVnode *pVnode, SSubmitTbData *pTbData, TSKEY minKey, TSKEY maxKey) {
607,826,978✔
2602
  int32_t code = 0;
607,826,978✔
2603

2604
  int32_t numRows = taosArrayGetSize(pTbData->aRowP);
607,826,978✔
2605
  SRow  **aRow = (SRow **)TARRAY_DATA(pTbData->aRowP);
607,827,315✔
2606

2607
  if (numRows <= 0 && (pTbData->flags & SUBMIT_REQ_ONLY_CREATE_TABLE) == 0) {
607,826,625✔
2608
    code = TSDB_CODE_INVALID_MSG;
×
2609
    vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " numRows:%d", TD_VID(pVnode), __func__,
×
2610
           __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, numRows);
2611
    return code;
×
2612
  }
2613

2614
  SRowKey *pLastKey = NULL;
607,826,625✔
2615
  SRowKey  lastKey = {0};
607,826,625✔
2616
  for (int32_t i = 0; i < numRows; ++i) {
2,147,483,647✔
2617
    SRow *pRow = aRow[i];
2,147,483,647✔
2618
    if (pRow->sver != pTbData->sver) {
2,147,483,647✔
2619
      code = TSDB_CODE_INVALID_MSG;
×
2620
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] sver:%d pTbData->sver:%d",
×
2621
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, i, pRow->sver,
2622
             pTbData->sver);
2623
      return code;
×
2624
    }
2625

2626
    SRowKey key = {0};
2,147,483,647✔
2627
    tRowGetKey(pRow, &key);
2,147,483,647✔
2628
    if (key.ts < minKey || key.ts > maxKey) {
2,147,483,647✔
2629
      code = TSDB_CODE_INVALID_MSG;
498,416✔
2630
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] key:%" PRId64
498,416✔
2631
             " is out of range [%" PRId64 ", %" PRId64 "]",
2632
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, i, key.ts,
2633
             minKey, maxKey);
2634
      return code;
×
2635
    }
2636

2637
    if (pLastKey && tRowKeyCompare(pLastKey, &key) >= 0) {
2,147,483,647✔
2638
      code = TSDB_CODE_INVALID_MSG;
×
2639
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] key:%" PRId64
×
2640
             " is not in order, lastKey:%" PRId64,
2641
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, i, key.ts,
2642
             pLastKey->ts);
2643
      return code;
×
2644
    } else if (pLastKey == NULL) {
2,147,483,647✔
2645
      pLastKey = &lastKey;
605,396,630✔
2646
    }
2647

2648
    *pLastKey = key;
2,147,483,647✔
2649
  }
2650

2651
  return code;
605,967,469✔
2652
}
2653

2654
static int32_t vnodeScanSubmitReq(SVnode *pVnode, int64_t version, SSubmitReq2 *pRequest, SSubmitRsp2 *pResponse) {
589,149,196✔
2655
  int32_t code = TSDB_CODE_SUCCESS;
589,149,196✔
2656
  int32_t numTbData = taosArrayGetSize(pRequest->aSubmitTbData);
589,149,196✔
2657

2658
  TSKEY now = taosGetTimestamp(pVnode->config.tsdbCfg.precision);
589,154,585✔
2659
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * pVnode->config.tsdbCfg.keep2;
589,152,679✔
2660
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
589,154,024✔
2661
  for (int32_t i = 0; i < numTbData; i++) {
1,203,874,820✔
2662
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
614,727,318✔
2663

2664
    if (pTbData->pCreateTbReq && pTbData->pCreateTbReq->uid == 0) {
614,732,811✔
2665
      code = TSDB_CODE_INVALID_MSG;
×
2666
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2667
             tstrerror(code), version);
2668
      return code;
×
2669
    }
2670

2671
    if (pTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
614,723,274✔
2672
      code = vnodeScanColumnData(pVnode, pTbData, minKey, maxKey);
6,898,935✔
2673
      if (code) {
6,899,272✔
2674
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2675
               __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2676
        return code;
×
2677
      }
2678
    } else {
2679
      code = vnodeScanSubmitRowData(pVnode, pTbData, minKey, maxKey);
607,825,742✔
2680
      if (code) {
607,823,030✔
2681
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2682
               __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2683
        return code;
×
2684
      }
2685
    }
2686
  }
2687

2688
  return code;
589,147,502✔
2689
}
2690

2691
static int32_t vnodeDecodeAuditRecord(const SJson *pJson, void *pObj) {
671✔
2692
  SAuditRecord *record = (SAuditRecord *)pObj;
671✔
2693
  int32_t       code = 0;
671✔
2694

2695
  tjsonGetNumberValue(pJson, "timestamp", record->curTime, code);
671✔
2696
  if (code < 0) return TSDB_CODE_INVALID_JSON_FORMAT;
671✔
2697
  code = tjsonGetStringValue2(pJson, "cluster_id", record->strClusterId, TSDB_CLUSTER_ID_LEN);
671✔
2698
  if (code < 0) return TSDB_CODE_INVALID_JSON_FORMAT;
671✔
2699
  code = tjsonGetStringValue2(pJson, "client_add", record->clientAddress, AUDIT_CLIENT_ADD_LEN);
671✔
2700
  if (code < 0) return TSDB_CODE_INVALID_JSON_FORMAT;
671✔
2701
  code = tjsonGetStringValue2(pJson, "user", record->user, TSDB_USER_LEN);
671✔
2702
  if (code < 0) return TSDB_CODE_INVALID_JSON_FORMAT;
671✔
2703
  code = tjsonGetStringValue2(pJson, "operation", record->operation, AUDIT_OPERATION_LEN);
671✔
2704
  if (code < 0) return TSDB_CODE_INVALID_JSON_FORMAT;
671✔
2705
  code = tjsonGetStringValue2(pJson, "db", record->target1, TSDB_DB_NAME_LEN);
671✔
2706
  if (code < 0) return TSDB_CODE_INVALID_JSON_FORMAT;
671✔
2707
  code = tjsonGetStringValue2(pJson, "resource", record->target2, TSDB_STREAM_NAME_LEN);
671✔
2708
  if (code < 0) return TSDB_CODE_INVALID_JSON_FORMAT;
671✔
2709
  record->detail = (char *)tjsonGetStringPointer(pJson, "details");
671✔
2710
  if (record->detail == NULL) return TSDB_CODE_INVALID_JSON_FORMAT;
671✔
2711
  code = tjsonGetDoubleValue(pJson, "duration", &record->duration);
671✔
2712
  if (code < 0) return TSDB_CODE_INVALID_JSON_FORMAT;
671✔
2713
  tjsonGetNumberValue(pJson, "affected_rows", record->affectedRows, code);
671✔
2714
  if (code < 0) return TSDB_CODE_INVALID_JSON_FORMAT;
671✔
2715

2716
  return 0;
671✔
2717
}
2718

2719
static int32_t vnodeBuildCreateTbReq(SVCreateTbReq *pTbReq, const char *tname, STag *pTag, int64_t suid,
61✔
2720
                                     const char *sname, SArray *tagName, uint8_t tagNum, int32_t ttl) {
2721
  int32_t code = TSDB_CODE_SUCCESS;
61✔
2722
  int32_t lino = 0;
61✔
2723

2724
  if (tname == NULL || sname == NULL) {
61✔
2725
    return TSDB_CODE_INVALID_PARA;
×
2726
  }
2727

2728
  pTbReq->name = taosStrdup(tname);
61✔
2729
  if (!pTbReq->name) {
61✔
2730
    code = terrno;
×
2731
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
2732
  }
2733

2734
  pTbReq->ctb.stbName = taosStrdup(sname);
61✔
2735
  if (!pTbReq->ctb.stbName) {
61✔
2736
    code = terrno;
×
2737
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
2738
  }
2739

2740
  pTbReq->type = TD_CHILD_TABLE;
61✔
2741
  pTbReq->ctb.suid = suid;
61✔
2742
  pTbReq->ctb.tagNum = tagNum;
61✔
2743
  pTbReq->ttl = ttl;
61✔
2744
  pTbReq->commentLen = -1;
61✔
2745
  pTbReq->ctb.tagName = tagName;
61✔
2746
  pTbReq->ctb.pTag = (uint8_t *)pTag;
61✔
2747

2748
  return code;
61✔
2749
_exit:
×
2750
  if (code != 0) {
×
2751
    if (pTbReq->name != NULL) {
×
2752
      taosMemoryFreeClear(pTbReq->name);
×
2753
    }
2754
    if (pTbReq->ctb.stbName != NULL) {
×
2755
      taosMemoryFreeClear(pTbReq->ctb.stbName);
×
2756
    }
2757
    vError("failed to build create tb at %d since %s", lino, tstrerror(code));
×
2758
  }
2759
  return code;
×
2760
}
2761

2762
static int32_t vnodePrepareCreateTb(SVCreateTbReq *pTbReq, char *tbName, int64_t suid, SSchemaWrapper *pTagSchema,
61✔
2763
                                    SAuditRecord *record) {
2764
  int32_t code = 0;
61✔
2765
  int32_t lino = 0;
61✔
2766

2767
  SArray *TagNames = NULL;
61✔
2768
  SArray *pTagVals = NULL;
61✔
2769
  STag   *pTag = NULL;
61✔
2770

2771
  SSchema *tSchema = &pTagSchema->pSchema[0];
61✔
2772
  vTrace("schema name:%s", tSchema->name);
61✔
2773

2774
  TagNames = taosArrayInit(1, TSDB_COL_NAME_LEN);
61✔
2775
  if (!TagNames) {
61✔
2776
    code = terrno;
×
2777
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
2778
  }
2779

2780
  if (NULL == taosArrayPush(TagNames, tSchema->name)) {
122✔
2781
    code = terrno;
×
2782
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
2783
  }
2784

2785
  STagVal tv = (STagVal){.cid = tSchema->colId,
61✔
2786
                         .type = tSchema->type,
61✔
2787
                         .nData = strlen(record->strClusterId),
61✔
2788
                         .pData = record->strClusterId};  // address copy, no value
61✔
2789
  if ((pTagVals = taosArrayInit(1, sizeof(STagVal))) == NULL) {
61✔
2790
    code = terrno;
×
2791
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
2792
  }
2793
  if (NULL == taosArrayPush(pTagVals, &tv)) {
61✔
2794
    code = terrno;
×
2795
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
2796
  }
2797

2798
  // version hardcode to 1 according to other module
2799
  code = tTagNew(pTagVals, 1, false, &pTag);
61✔
2800
  if (code != TSDB_CODE_SUCCESS) {
61✔
2801
    vError("failed to create tag, error:%s", tstrerror(code));
×
2802
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
2803
  }
2804

2805
  code = vnodeBuildCreateTbReq(pTbReq, tbName, pTag, suid, AUDIT_STABLE_NAME, TagNames, pTagSchema->nCols,
61✔
2806
                               TSDB_DEFAULT_TABLE_TTL);
2807
_exit:
61✔
2808
  if (code != 0) {
61✔
2809
    vError("failed to prepare create tb at %d since %s", lino, tstrerror(code));
×
2810
    if (TagNames != NULL) {
×
2811
      taosArrayDestroy(TagNames);
×
2812
      TagNames = NULL;
×
2813
      pTbReq->ctb.tagName = NULL;
×
2814
    }
2815
    if (pTag != NULL) {
×
2816
      tTagFree(pTag);
×
2817
      pTag = NULL;
×
2818
      pTbReq->ctb.pTag = NULL;
×
2819
    }
2820
  }
2821

2822
  if (pTagVals != NULL) {
61✔
2823
    taosArrayDestroy(pTagVals);
61✔
2824
    pTagVals = NULL;
61✔
2825
  }
2826

2827
  return code;
61✔
2828
}
2829

2830
static SArray *vnodePrepareRow(SVnode *pVnode, STSchema *pSchema, SAuditRecord *record) {
671✔
2831
  int32_t code = 0;
671✔
2832
  int32_t lino = 0;
671✔
2833

2834
  SArray *aRows = NULL;
671✔
2835
  SArray *pVals = NULL;
671✔
2836
  SRow   *pRow = NULL;
671✔
2837

2838
  if (!(aRows = taosArrayInit(1, sizeof(SRow *)))) {
671✔
2839
    code = terrno;
×
2840
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
2841
  }
2842

2843
  if ((pVals = taosArrayInit(1, sizeof(SColVal))) == NULL) {
671✔
2844
    code = terrno;
×
2845
    goto _exit;
×
2846
  }
2847

2848
  for (int32_t k = 0; k < pSchema->numOfCols; ++k) {
6,710✔
2849
    const STColumn *pCol = &pSchema->columns[k];
6,039✔
2850
    vTrace("vgId:%d, schema column id:%d, type:%d", TD_VID(pVnode), pCol->colId, pCol->type);
6,039✔
2851

2852
    // colId is consistent with audit_columns in mndStb.c
2853
    void *data = NULL;
6,039✔
2854
    if (pCol->colId == 1) {
6,039✔
2855
      data = &record->curTime;
671✔
2856
    } else if (pCol->colId == 2) {
5,368✔
2857
      data = record->detail;
671✔
2858
    } else if (pCol->colId == 3) {
4,697✔
2859
      data = record->user;
671✔
2860
    } else if (pCol->colId == 4) {
4,026✔
2861
      data = record->operation;
671✔
2862
    } else if (pCol->colId == 5) {
3,355✔
2863
      data = record->target1;
671✔
2864
    } else if (pCol->colId == 6) {
2,684✔
2865
      data = record->target2;
671✔
2866
    } else if (pCol->colId == 7) {
2,013✔
2867
      data = record->clientAddress;
671✔
2868
    } else if (pCol->colId == 8) {
1,342✔
2869
      data = &record->duration;
671✔
2870
    } else if (pCol->colId == 9) {
671✔
2871
      data = &record->affectedRows;
671✔
2872
    } else {
2873
      vError("the column id %" PRIi16 " is not defined in audit record table", pCol->colId);
×
2874
      code = TSDB_CODE_APP_ERROR;
×
2875
      TAOS_CHECK_GOTO(code, &lino, _exit);
×
2876
    }
2877

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

2925
            SColVal cv = COL_VAL_NULL(pCol->colId, pCol->type);  // should use pCol->type
2926
            if (NULL == taosArrayPush(pVals, &cv)) {
2927
              goto _end;
2928
            }
2929
          } else {
2930
           */
2931
        SValue sv = {0};
2,013✔
2932
        sv.type = pCol->type;
2,013✔
2933
        valueSetDatum(&sv, sv.type, data, pCol->bytes);
2,013✔
2934
        SColVal cv = COL_VAL_VALUE(pCol->colId, sv);
2,013✔
2935
        if (NULL == taosArrayPush(pVals, &cv)) {
2,013✔
2936
          code = terrno;
×
2937
          TAOS_CHECK_GOTO(code, &lino, _exit);
×
2938
        }
2939
        //}
2940
        break;
2,013✔
2941
      }
2942
    }
2943
  }
2944

2945
  SRowBuildScanInfo sinfo = {0};
671✔
2946
  if ((code = tRowBuild(pVals, pSchema, &pRow, &sinfo)) < 0) {
671✔
2947
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
2948
  }
2949
  if (NULL == taosArrayPush(aRows, &pRow)) {
671✔
2950
    code = terrno;
×
2951
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
2952
  }
2953
  taosArrayDestroy(pVals);
671✔
2954
  pVals = NULL;
671✔
2955

2956
_exit:
671✔
2957
  if (code != 0) {
671✔
2958
    vError("vgId:%d, failed to prepare row at:%d since %s", TD_VID(pVnode), lino, tstrerror(code));
×
2959
    taosArrayDestroy(pVals);
×
2960
    tRowDestroy(pRow);
×
2961
    taosArrayDestroy(aRows);
×
2962
    terrno = code;
×
2963
    aRows = NULL;
×
2964
  }
2965
  return aRows;
671✔
2966
}
2967

2968
static SArray *vnodePrepareSubmitTb(SVnode *pVnode, SAuditRecord *record, STSchema *pSchema, SSchemaWrapper *pTagSchema,
671✔
2969
                                    int64_t suid) {
2970
  int32_t code = 0;
671✔
2971
  int32_t lino = 0;
671✔
2972

2973
  SArray *aSubmitTbData = NULL;
671✔
2974
  SSubmitTbData tbData = {0};
671✔
2975

2976
  if (!(aSubmitTbData = taosArrayInit(1, sizeof(SSubmitTbData)))) {
671✔
2977
    code = terrno;
×
2978
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
2979
  }
2980

2981
  char tbName[TSDB_TABLE_NAME_LEN] = {0};
671✔
2982
  (void)tsnprintf(tbName, TSDB_TABLE_NAME_LEN, "t_operations_%s", record->strClusterId);
671✔
2983

2984
  SMetaReader merTb = {0};
671✔
2985
  metaReaderDoInit(&merTb, pVnode->pMeta, META_READER_LOCK);
671✔
2986
  if ((code = metaGetTableEntryByName(&merTb, tbName)) == 0) {
671✔
2987
    vTrace("vgId:%d, get table entry, table:%s uid:%" PRId64 ", suid:% " PRId64 ", version:%" PRId64 ", api:%p",
610✔
2988
           TD_VID(pVnode), tbName, merTb.me.uid, merTb.me.ctbEntry.suid, merTb.me.version, merTb.pAPI);
2989
    tbData.uid = merTb.me.uid;
610✔
2990
    metaReaderClear(&merTb);
610✔
2991
  } else if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST) {
61✔
2992
    vTrace("vgId:%d, %s table not exist", TD_VID(pVnode), tbName);
61✔
2993

2994
    tbData.uid = tGenIdPI64();
61✔
2995
    tbData.flags |= SUBMIT_REQ_AUTO_CREATE_TABLE;
61✔
2996

2997
    tbData.pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
61✔
2998

2999
    if (tbData.pCreateTbReq == NULL) {
61✔
3000
      code = terrno;
×
3001
      TAOS_CHECK_GOTO(code, &lino, _exit);
×
3002
    }
3003

3004
    tbData.pCreateTbReq->uid = tbData.uid;
61✔
3005
    tbData.pCreateTbReq->btime = taosGetTimestampMs();
122✔
3006

3007
    code = vnodePrepareCreateTb(tbData.pCreateTbReq, tbName, suid, pTagSchema, record);
61✔
3008

3009
    metaReaderClear(&merTb);
61✔
3010
    TAOS_CHECK_GOTO(code, &lino, _exit);
61✔
3011
  } else {
3012
    metaReaderClear(&merTb);
×
3013
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
3014
  }
3015

3016
  tbData.suid = suid;
671✔
3017
  tbData.sver = pSchema->version;
671✔
3018

3019
  tbData.aRowP = vnodePrepareRow(pVnode, pSchema, record);
671✔
3020
  if (tbData.aRowP == NULL) {
671✔
3021
    code = terrno;
×
3022
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
3023
  }
3024

3025
  if (NULL == taosArrayPush(aSubmitTbData, &tbData)) {
671✔
3026
    code = terrno;
×
3027
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
3028
  }
3029

3030
_exit:
671✔
3031
  if (code != 0) {
671✔
3032
    vError("vgId:%d, failed to prepare submitTb at:%d since %s", TD_VID(pVnode), lino, tstrerror(code));
×
3033
    tDestroySubmitTbData(&tbData, TSDB_MSG_FLG_ENCODE);
×
3034
    if (aSubmitTbData != NULL) {
×
3035
      taosArrayDestroy(aSubmitTbData);
×
3036
    }
3037
    terrno = code;
×
3038
    aSubmitTbData = NULL;
×
3039
  }
3040
  return aSubmitTbData;
671✔
3041
}
3042

3043
static int32_t vnodeSaveOneAuditRecord(SVnode *pVnode, int64_t ver, SJson *pJson, SSchemaWrapper *pTagSchema,
671✔
3044
                                       int64_t suid, STSchema *pSchema, SRpcMsg *pOriginalMsg) {
3045
  int32_t code = 0;
671✔
3046
  int32_t lino = 0;
671✔
3047
  terrno = 0;
671✔
3048

3049
  SAuditRecord record = {0};
671✔
3050
  SSubmitReq2 *pSubmitReq = NULL;
671✔
3051
  SSubmitRsp2 *pSubmitRsp = &(SSubmitRsp2){0};
671✔
3052

3053
  TAOS_CHECK_GOTO(vnodeDecodeAuditRecord(pJson, &record), &lino, _exit);
671✔
3054

3055
  vTrace("vgId:%d, start to audit operation:%s", TD_VID(pVnode), record.operation);
671✔
3056

3057
  if (!(pSubmitReq = taosMemoryCalloc(1, sizeof(SSubmitReq2)))) {
671✔
3058
    code = terrno;
×
3059
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
3060
  }
3061

3062
  pSubmitReq->aSubmitTbData = vnodePrepareSubmitTb(pVnode, &record, pSchema, pTagSchema, suid);
671✔
3063
  if (pSubmitReq->aSubmitTbData == NULL) {
671✔
3064
    code = terrno;
×
3065
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
3066
  }
3067

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

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

3084
  vTrace("vgId:%d, affectedRow:%d", TD_VID(pVnode), pSubmitRsp->affectedRows);
671✔
3085

3086
_exit:
671✔
3087
  if (code != 0)
671✔
3088
    vError("vgId:%d, failed to save one AuditRecord at line:%d, since %s", TD_VID(pVnode), lino, tstrerror(code));
×
3089

3090
  // update statistics
3091
  (void)atomic_add_fetch_64(&pVnode->statis.nInsert, pSubmitRsp->affectedRows);
671✔
3092
  (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsert, 1);
671✔
3093
  if (code == 0) {
671✔
3094
    (void)atomic_add_fetch_64(&pVnode->statis.nInsertSuccess, pSubmitRsp->affectedRows);
671✔
3095
    (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
671✔
3096
  }
3097

3098
  // update metrics
3099
  METRICS_UPDATE(pVnode->writeMetrics.total_requests, METRIC_LEVEL_LOW, 1);
671✔
3100
  METRICS_UPDATE(pVnode->writeMetrics.total_rows, METRIC_LEVEL_HIGH, pSubmitRsp->affectedRows);
671✔
3101
  // METRICS_UPDATE(pVnode->writeMetrics.total_bytes, METRIC_LEVEL_LOW, pMsg->header.contLen);
3102

3103
  if (code == 0 && tsEnableMonitor && tsMonitorFqdn[0] != 0 && tsMonitorPort != 0 && pSubmitRsp->affectedRows > 0 &&
671✔
3104
      strlen(RPC_MSG_USER(pOriginalMsg)) > 0 && tsInsertCounter != NULL) {
×
3105
    const char *sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT_AFFECTED_ROWS,
×
3106
                                   pVnode->monitor.strClusterId,
×
3107
                                   pVnode->monitor.strDnodeId,
×
3108
                                   tsLocalEp,
3109
                                   pVnode->monitor.strVgId,
×
3110
                                   RPC_MSG_USER(pOriginalMsg),
×
3111
                                   "Success"};
3112
    int         tv = taos_counter_add(tsInsertCounter, pSubmitRsp->affectedRows, sample_labels);
×
3113
    if (tv != 0) vError("vgId:%d, failed to taos counter add since return is %d", TD_VID(pVnode), tv);
×
3114
  }
3115

3116
  // clear
3117
  if (pSubmitReq != NULL) {
671✔
3118
    tDestroySubmitReq(pSubmitReq, TSDB_MSG_FLG_ENCODE);
671✔
3119
    taosMemoryFree(pSubmitReq);
671✔
3120
  }
3121
  if (pSubmitRsp != NULL) {
671✔
3122
    tDestroySSubmitRsp2(pSubmitRsp, TSDB_MSG_FLG_ENCODE);
671✔
3123
  }
3124

3125
  return code;
671✔
3126
}
3127

3128
static int32_t vnodeProcessAuditRecordReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pOriginalMsg) {
244✔
3129
  int32_t code = 0;
244✔
3130
  int32_t lino = 0;
244✔
3131
  terrno = 0;
244✔
3132

3133
  SVAuditRecordReq req = {0};
244✔
3134
  if (tDeserializeSVAuditRecordReq(pReq, len, &req) != 0) {
244✔
3135
    vError("vgId:%d, failed to deserialize SVAuditRecordReq", TD_VID(pVnode));
×
3136
    code = TSDB_CODE_INVALID_MSG;
×
3137
    return code;
×
3138
  }
3139

3140
  vTrace("vgId:%d, start to process AuditRecord Req", TD_VID(pVnode));
244✔
3141

3142
  SJson          *pJson = NULL;
244✔
3143
  STSchema       *pSchema = NULL;
244✔
3144
  SSchemaWrapper *pTagSchema = NULL;
244✔
3145
  int64_t         suid = 0;
244✔
3146

3147
  pJson = tjsonParse(req.data);
244✔
3148
  if (pJson == NULL) {
244✔
3149
    code = TSDB_CODE_INVALID_JSON_FORMAT;
×
3150
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
3151
  }
3152

3153
  SMetaReader merStb = {0};
244✔
3154
  metaReaderDoInit(&merStb, pVnode->pMeta, META_READER_LOCK);
244✔
3155
  code = metaGetTableEntryByName(&merStb, AUDIT_STABLE_NAME);
244✔
3156
  if (code != 0) {
244✔
3157
    metaReaderClear(&merStb);
×
3158
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
3159
  }
3160
  vTrace("vgId:%d, get audit stable entry, uid:%" PRId64 ", suid:% " PRId64 ", version:%" PRId64 ", api:%p",
244✔
3161
         TD_VID(pVnode), merStb.me.uid, merStb.me.ctbEntry.suid, merStb.me.version, merStb.pAPI);
3162

3163
  code = vnodeGetTableSchema(pVnode, merStb.me.uid, &pSchema, &suid, &pTagSchema);
244✔
3164
  if (code != 0) {
244✔
3165
    metaReaderClear(&merStb);
×
3166
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
3167
  }
3168
  vTrace("vgId:%d, get audit stable schema, version:%d, suid:%" PRId64, TD_VID(pVnode), pSchema->version, suid);
244✔
3169
  metaReaderClear(&merStb);
244✔
3170
  // pSchema pTagSchema can be cached in the future
3171

3172
  SJson *pRecords = tjsonGetObjectItem(pJson, "records");
244✔
3173
  if (pRecords == NULL) {
244✔
3174
    TAOS_CHECK_GOTO(vnodeSaveOneAuditRecord(pVnode, ver, pJson, pTagSchema, suid, pSchema, pOriginalMsg), &lino, _exit);
122✔
3175
  } else {
3176
    int32_t size = tjsonGetArraySize(pRecords);
122✔
3177
    vTrace("%d items in records", size);
122✔
3178
    for (int32_t i = 0; i < size; ++i) {
671✔
3179
      SJson *pRecord = tjsonGetArrayItem(pRecords, i);
549✔
3180
      if (pRecord == NULL) {
549✔
3181
        vError("vgId:%d, failed to get array item %d", TD_VID(pVnode), i);
×
3182
        code = TSDB_CODE_INVALID_JSON_FORMAT;
×
3183
        TAOS_CHECK_GOTO(code, &lino, _exit);
×
3184
      }
3185
      TAOS_CHECK_GOTO(vnodeSaveOneAuditRecord(pVnode, ver, pRecord, pTagSchema, suid, pSchema, pOriginalMsg), &lino,
549✔
3186
                      _exit);
3187
    }
3188
  }
3189

3190
_exit:
244✔
3191
  if (code != 0)
244✔
3192
    vError("vgId:%d, failed to process AuditRecordReq at line:%d, since %s", TD_VID(pVnode), lino, tstrerror(code));
×
3193

3194
  // clear
3195
  if (pSchema) taosMemoryFree(pSchema);
244✔
3196
  if (pTagSchema) {
244✔
3197
    taosMemoryFreeClear(pTagSchema->pSchema);
244✔
3198
    taosMemoryFree(pTagSchema);
244✔
3199
  }
3200
  if (pJson != NULL) {
244✔
3201
    cJSON_Delete(pJson);
244✔
3202
    pJson = NULL;
244✔
3203
  }
3204
  tFreeSVAuditRecordReq(&req);
244✔
3205

3206
  return code;
244✔
3207
}
3208

3209
static int32_t vnodeProcessSubmitReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
589,160,953✔
3210
                                     SRpcMsg *pOriginalMsg) {
3211
  int32_t code = 0;
589,160,953✔
3212
  int32_t lino = 0;
589,160,953✔
3213
  terrno = 0;
589,160,953✔
3214

3215
  SSubmitReq2 *pSubmitReq = &(SSubmitReq2){0};
589,162,461✔
3216
  SSubmitRsp2 *pSubmitRsp = &(SSubmitRsp2){0};
589,162,461✔
3217
  int32_t      ret;
3218
  SEncoder     ec = {0};
589,161,163✔
3219

3220
  pRsp->code = TSDB_CODE_SUCCESS;
589,162,461✔
3221

3222
  void           *pAllocMsg = NULL;
589,161,719✔
3223
  SSubmitReq2Msg *pMsg = (SSubmitReq2Msg *)pReq;
589,161,719✔
3224
  SDecoder        dc = {0};
589,161,719✔
3225
  if (0 == taosHton64(pMsg->version)) {
589,162,461✔
3226
    code = vnodeSubmitReqConvertToSubmitReq2(pVnode, (SSubmitReq *)pMsg, pSubmitReq);
×
3227
    if (TSDB_CODE_SUCCESS == code) {
×
3228
      code = vnodeRebuildSubmitReqMsg(pSubmitReq, &pReq);
×
3229
    }
3230
    if (TSDB_CODE_SUCCESS == code) {
×
3231
      pAllocMsg = pReq;
×
3232
    }
3233
    TSDB_CHECK_CODE(code, lino, _exit);
×
3234
  } else {
3235
    // decode
3236
    pReq = POINTER_SHIFT(pReq, sizeof(SSubmitReq2Msg));
589,161,689✔
3237
    len -= sizeof(SSubmitReq2Msg);
589,161,719✔
3238

3239
    tDecoderInit(&dc, pReq, len);
589,161,719✔
3240
    if (tDecodeSubmitReq(&dc, pSubmitReq, NULL) < 0) {
589,161,677✔
3241
      code = TSDB_CODE_INVALID_MSG;
×
3242
      TSDB_CHECK_CODE(code, lino, _exit);
×
3243
    }
3244
  }
3245

3246
  // Scan the request
3247
  code = vnodeScanSubmitReq(pVnode, ver, pSubmitReq, pSubmitRsp);
589,153,861✔
3248
  if (code) {
589,147,532✔
3249
    vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
3250
           tstrerror(code), ver);
3251
    TSDB_CHECK_CODE(code, lino, _exit);
×
3252
  }
3253

3254
  vGDebug(pOriginalMsg ? &pOriginalMsg->info.traceId : NULL, "vgId:%d, index:%" PRId64 ", submit block, rows:%d",
589,147,532✔
3255
          TD_VID(pVnode), ver, (int32_t)taosArrayGetSize(pSubmitReq->aSubmitTbData));
3256

3257
  // Handle auto create table
3258
  code = vnodeHandleAutoCreateTable(pVnode, ver, pSubmitReq, pSubmitRsp);
589,161,084✔
3259
  if (code) {
589,162,191✔
3260
    vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
32,006✔
3261
           tstrerror(code), ver);
3262
    TSDB_CHECK_CODE(code, lino, _exit);
32,006✔
3263
  }
3264

3265
  // Handle data write
3266
  code = vnodeHandleDataWrite(pVnode, ver, pSubmitReq, pSubmitRsp);
589,130,185✔
3267
  if (code) {
589,130,117✔
3268
    vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
2,281✔
3269
           tstrerror(code), ver);
3270
    TSDB_CHECK_CODE(code, lino, _exit);
2,281✔
3271
  }
3272

3273
_exit:
589,161,949✔
3274
  // message
3275
  pRsp->code = code;
589,162,461✔
3276
  tEncodeSize(tEncodeSSubmitRsp2, pSubmitRsp, pRsp->contLen, ret);
589,162,461✔
3277
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
589,160,108✔
3278
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
589,159,449✔
3279
  if (tEncodeSSubmitRsp2(&ec, pSubmitRsp) < 0) {
589,160,027✔
3280
    vError("vgId:%d, failed to encode submit response", TD_VID(pVnode));
×
3281
  }
3282
  tEncoderClear(&ec);
589,159,527✔
3283

3284
  // update statistics
3285
  (void)atomic_add_fetch_64(&pVnode->statis.nInsert, pSubmitRsp->affectedRows);
589,155,041✔
3286
  (void)atomic_add_fetch_64(&pVnode->statis.nInsertSuccess, pSubmitRsp->affectedRows);
589,162,401✔
3287
  (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsert, 1);
589,161,754✔
3288

3289
  // update metrics
3290
  METRICS_UPDATE(pVnode->writeMetrics.total_requests, METRIC_LEVEL_LOW, 1);
589,162,063✔
3291
  METRICS_UPDATE(pVnode->writeMetrics.total_rows, METRIC_LEVEL_HIGH, pSubmitRsp->affectedRows);
589,155,209✔
3292
  METRICS_UPDATE(pVnode->writeMetrics.total_bytes, METRIC_LEVEL_LOW, pMsg->header.contLen);
589,154,800✔
3293

3294
  if (tsEnableMonitor && tsMonitorFqdn[0] != 0 && tsMonitorPort != 0 && pSubmitRsp->affectedRows > 0 &&
589,153,663✔
3295
      strlen(RPC_MSG_USER(pOriginalMsg)) > 0 && tsInsertCounter != NULL) {
×
3296
    const char *sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT_AFFECTED_ROWS,
×
3297
                                   pVnode->monitor.strClusterId,
×
3298
                                   pVnode->monitor.strDnodeId,
×
3299
                                   tsLocalEp,
3300
                                   pVnode->monitor.strVgId,
×
3301
                                   RPC_MSG_USER(pOriginalMsg),
×
3302
                                   "Success"};
3303
    int         tv = taos_counter_add(tsInsertCounter, pSubmitRsp->affectedRows, sample_labels);
×
3304
    if (tv != 0) vError("vgId:%d, failed to taos counter add since return is %d", TD_VID(pVnode), tv);
×
3305
  }
3306

3307
  if (code == 0) {
589,153,663✔
3308
    (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
589,119,316✔
3309
  }
3310

3311
  // clear
3312
  tDestroySubmitReq(pSubmitReq, 0 == taosHton64(pMsg->version) ? TSDB_MSG_FLG_CMPT : TSDB_MSG_FLG_DECODE);
589,159,593✔
3313
  tDestroySSubmitRsp2(pSubmitRsp, TSDB_MSG_FLG_ENCODE);
589,149,490✔
3314

3315
  if (code) {
589,141,552✔
3316
    terrno = code;
34,287✔
3317
    if (code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
34,287✔
3318
      vInfo("vgId:%d, failed to process submit request since %s, lino:%d, version:%" PRId64, TD_VID(pVnode),
30,930✔
3319
            tstrerror(code), lino, ver);
3320

3321
    } else {
3322
      vError("vgId:%d, failed to process submit request since %s, lino:%d, version:%" PRId64, TD_VID(pVnode),
3,357✔
3323
             tstrerror(code), lino, ver);
3324
    }
3325
  }
3326

3327
  taosMemoryFree(pAllocMsg);
589,145,794✔
3328
  tDecoderClear(&dc);
589,145,019✔
3329

3330
  return code;
589,161,561✔
3331
}
3332

3333
static int32_t vnodeConsolidateAlterHashRange(SVnode *pVnode, int64_t ver) {
25,257✔
3334
  int32_t code = TSDB_CODE_SUCCESS;
25,257✔
3335

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

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

3342
  return code;
25,257✔
3343
}
3344

3345
static int32_t vnodeProcessAlterConfirmReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
4,223,500✔
3346
  vInfo("vgId:%d, vnode handle msgType:alter-confirm, alter confirm msg is processed", TD_VID(pVnode));
4,223,500✔
3347
  int32_t code = TSDB_CODE_SUCCESS;
4,223,500✔
3348
  if (!pVnode->config.hashChange) {
4,223,500✔
3349
    goto _exit;
4,198,243✔
3350
  }
3351

3352
  code = vnodeConsolidateAlterHashRange(pVnode, ver);
25,257✔
3353
  if (code < 0) {
25,257✔
3354
    vError("vgId:%d, failed to consolidate alter hashrange since %s. version:%" PRId64, TD_VID(pVnode), terrstr(), ver);
×
3355
    goto _exit;
×
3356
  }
3357
  pVnode->config.hashChange = false;
25,257✔
3358

3359
_exit:
4,223,500✔
3360
  pRsp->msgType = TDMT_VND_ALTER_CONFIRM_RSP;
4,223,500✔
3361
  pRsp->code = code;
4,223,500✔
3362
  pRsp->pCont = NULL;
4,223,500✔
3363
  pRsp->contLen = 0;
4,223,500✔
3364

3365
  return code;
4,223,500✔
3366
}
3367

3368
extern int32_t tsdbDisableAndCancelAllBgTask(STsdb *pTsdb);
3369
extern void    tsdbEnableBgTask(STsdb *pTsdb);
3370

3371
static int32_t vnodeProcessAlterConfigReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
336,131✔
3372
  bool walChanged = false;
336,131✔
3373
  bool tsdbChanged = false;
336,131✔
3374

3375
  SAlterVnodeConfigReq req = {0};
336,131✔
3376
  if (tDeserializeSAlterVnodeConfigReq(pReq, len, &req) != 0) {
336,131✔
3377
    terrno = TSDB_CODE_INVALID_MSG;
×
3378
    return TSDB_CODE_INVALID_MSG;
×
3379
  }
3380

3381
  vInfo("vgId:%d, start to alter vnode config, page:%d pageSize:%d buffer:%d szPage:%d szBuf:%" PRIu64
336,131✔
3382
        " cacheLast:%d cacheLastSize:%d cacheLastShards:%d days:%d keep0:%d keep1:%d keep2:%d keepTimeOffset:%d ssKeepLocal:%d "
3383
        "ssCompact:%d allowDrop:%d fsync:%d level:%d "
3384
        "walRetentionPeriod:%d walRetentionSize:%d",
3385
        TD_VID(pVnode), req.pages, req.pageSize, req.buffer, req.pageSize * 1024, (uint64_t)req.buffer * 1024 * 1024,
3386
        req.cacheLast, req.cacheLastSize, req.cacheLastShardBits, req.daysPerFile, req.daysToKeep0, req.daysToKeep1, req.daysToKeep2,
3387
        req.keepTimeOffset, req.ssKeepLocal, req.ssCompact, req.allowDrop, req.walFsyncPeriod, req.walLevel,
3388
        req.walRetentionPeriod, req.walRetentionSize);
3389

3390
  if (pVnode->config.cacheLastSize != req.cacheLastSize) {
336,131✔
3391
    pVnode->config.cacheLastSize = req.cacheLastSize;
13,135✔
3392
    tsdbCacheSetCapacity(pVnode, (size_t)pVnode->config.cacheLastSize * 1024 * 1024);
13,135✔
3393
  }
3394

3395
  if (pVnode->config.cacheLastShardBits != req.cacheLastShardBits) {
336,131✔
3396
    vInfo("vgId:%d, cacheLastShardBits change from %d to %d, rebuilding last cache",
1,526✔
3397
          TD_VID(pVnode), pVnode->config.cacheLastShardBits, req.cacheLastShardBits);
3398

3399
    // Rebuild only the lruCache under mutex to protect concurrent readers/writers
3400
    (void)taosThreadMutexLock(&pVnode->pTsdb->lruMutex);
1,526✔
3401
    int32_t code = tsdbRebuildLastCache(pVnode->pTsdb, req.cacheLastShardBits);
1,526✔
3402
    (void)taosThreadMutexUnlock(&pVnode->pTsdb->lruMutex);
1,526✔
3403

3404
    if (code) {
1,526✔
3405
      vError("vgId:%d, failed to rebuild last cache since %s", TD_VID(pVnode), tstrerror(code));
×
3406
      return code;
×
3407
    }
3408

3409
    pVnode->config.cacheLastShardBits = req.cacheLastShardBits;
1,526✔
3410

3411
    vInfo("vgId:%d, last cache rebuilt successfully with %d shards",
1,526✔
3412
          TD_VID(pVnode), taosLRUCacheGetNumShards(pVnode->pTsdb->lruCache));
3413
  }
3414

3415
  if (pVnode->config.szBuf != req.buffer * 1024LL * 1024LL) {
336,131✔
3416
    vInfo("vgId:%d, vnode buffer is changed from %" PRId64 " to %" PRId64, TD_VID(pVnode), pVnode->config.szBuf,
1,822✔
3417
          (uint64_t)(req.buffer * 1024LL * 1024LL));
3418
    pVnode->config.szBuf = req.buffer * 1024LL * 1024LL;
1,822✔
3419
  }
3420

3421
  if (pVnode->config.szCache != req.pages) {
336,131✔
3422
    if ((terrno = metaAlterCache(pVnode->pMeta, req.pages)) < 0) {
4,740✔
3423
      vError("vgId:%d, failed to change vnode pages from %d to %d failed since %s", TD_VID(pVnode),
×
3424
             pVnode->config.szCache, req.pages, tstrerror(terrno));
3425
      return terrno;
×
3426
    } else {
3427
      vInfo("vgId:%d, vnode pages is changed from %d to %d", TD_VID(pVnode), pVnode->config.szCache, req.pages);
4,740✔
3428
      pVnode->config.szCache = req.pages;
4,740✔
3429
    }
3430
  }
3431

3432
  if (pVnode->config.cacheLast != req.cacheLast) {
336,131✔
3433
    pVnode->config.cacheLast = req.cacheLast;
98,686✔
3434
  }
3435

3436
  if (pVnode->config.walCfg.fsyncPeriod != req.walFsyncPeriod) {
336,131✔
3437
    pVnode->config.walCfg.fsyncPeriod = req.walFsyncPeriod;
54,432✔
3438
    walChanged = true;
54,432✔
3439
  }
3440

3441
  if (pVnode->config.walCfg.level != req.walLevel) {
336,131✔
3442
    if (pVnode->config.walCfg.level == 0) {
40,073✔
3443
      pVnode->config.walCfg.clearFiles = 1;
2,265✔
3444
    }
3445
    pVnode->config.walCfg.level = req.walLevel;
40,073✔
3446
    walChanged = true;
40,073✔
3447
  }
3448

3449
  if (pVnode->config.walCfg.retentionPeriod != req.walRetentionPeriod) {
336,131✔
3450
    pVnode->config.walCfg.retentionPeriod = req.walRetentionPeriod;
49,930✔
3451
    walChanged = true;
49,930✔
3452
  }
3453

3454
  if (pVnode->config.walCfg.retentionSize != req.walRetentionSize) {
336,131✔
3455
    pVnode->config.walCfg.retentionSize = req.walRetentionSize;
1,508✔
3456
    walChanged = true;
1,508✔
3457
  }
3458

3459
  if (pVnode->config.tsdbCfg.keep0 != req.daysToKeep0) {
336,131✔
3460
    pVnode->config.tsdbCfg.keep0 = req.daysToKeep0;
45,312✔
3461
    tsdbChanged = true;
45,312✔
3462
  }
3463

3464
  if (pVnode->config.tsdbCfg.keep1 != req.daysToKeep1) {
336,131✔
3465
    pVnode->config.tsdbCfg.keep1 = req.daysToKeep1;
43,804✔
3466
    tsdbChanged = true;
43,804✔
3467
  }
3468

3469
  if (pVnode->config.tsdbCfg.keep2 != req.daysToKeep2) {
336,131✔
3470
    pVnode->config.tsdbCfg.keep2 = req.daysToKeep2;
37,890✔
3471
    tsdbChanged = true;
37,890✔
3472
  }
3473

3474
  if (pVnode->config.tsdbCfg.keepTimeOffset != req.keepTimeOffset) {
336,131✔
3475
    pVnode->config.tsdbCfg.keepTimeOffset = req.keepTimeOffset;
7,634✔
3476
    tsdbChanged = true;
7,634✔
3477
  }
3478

3479
  if (req.sttTrigger != -1 && req.sttTrigger != pVnode->config.sttTrigger) {
336,131✔
3480
    if (req.sttTrigger > 1 && pVnode->config.sttTrigger > 1) {
2,622✔
3481
      pVnode->config.sttTrigger = req.sttTrigger;
1,508✔
3482
    } else {
3483
      vnodeAWait(&pVnode->commitTask);
1,114✔
3484

3485
      int32_t ret = tsdbDisableAndCancelAllBgTask(pVnode->pTsdb);
1,114✔
3486
      if (ret != 0) {
1,114✔
3487
        vError("vgId:%d, failed to disable bg task since %s", TD_VID(pVnode), tstrerror(ERRNO));
×
3488
      }
3489

3490
      pVnode->config.sttTrigger = req.sttTrigger;
1,114✔
3491
      tsdbEnableBgTask(pVnode->pTsdb);
1,114✔
3492
    }
3493
  }
3494

3495
  if (req.minRows != -1 && req.minRows != pVnode->config.tsdbCfg.minRows) {
336,131✔
3496
    pVnode->config.tsdbCfg.minRows = req.minRows;
1,508✔
3497
  }
3498

3499
  if (req.ssKeepLocal != -1 && req.ssKeepLocal != pVnode->config.ssKeepLocal) {
336,131✔
3500
    pVnode->config.ssKeepLocal = req.ssKeepLocal;
1,508✔
3501
  }
3502
  if (req.ssCompact != -1 && req.ssCompact != pVnode->config.ssCompact) {
336,131✔
3503
    pVnode->config.ssCompact = req.ssCompact;
1,508✔
3504
  }
3505
  if (req.allowDrop != pVnode->config.allowDrop) {
336,131✔
3506
    pVnode->config.allowDrop = req.allowDrop;
×
3507
  }
3508
  if (req.secureDelete != pVnode->config.secureDelete) {
336,131✔
3509
    pVnode->config.secureDelete = req.secureDelete;
1,160✔
3510
  }
3511

3512
  if (walChanged) {
336,131✔
3513
    if (walAlter(pVnode->pWal, &pVnode->config.walCfg) != 0) {
142,891✔
3514
      vError("vgId:%d, failed to alter wal config since %s", TD_VID(pVnode), tstrerror(ERRNO));
×
3515
    }
3516
  }
3517

3518
  if (tsdbChanged) {
336,131✔
3519
    tsdbSetKeepCfg(pVnode->pTsdb, &pVnode->config.tsdbCfg);
60,504✔
3520
  }
3521

3522
  return 0;
336,131✔
3523
}
3524

3525
static int32_t vnodeProcessBatchDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
3526
  SBatchDeleteReq deleteReq;
×
3527
  SDecoder        decoder;
×
3528
  tDecoderInit(&decoder, pReq, len);
×
3529
  if (tDecodeSBatchDeleteReq(&decoder, &deleteReq) < 0) {
×
3530
    tDecoderClear(&decoder);
×
3531
    return terrno = TSDB_CODE_INVALID_MSG;
×
3532
  }
3533

3534
  SMetaReader mr = {0};
×
3535
  metaReaderDoInit(&mr, pVnode->pMeta, META_READER_NOLOCK);
×
3536
  STsdb *pTsdb = pVnode->pTsdb;
×
3537

3538
  int32_t sz = taosArrayGetSize(deleteReq.deleteReqs);
×
3539
  for (int32_t i = 0; i < sz; i++) {
×
3540
    SSingleDeleteReq *pOneReq = taosArrayGet(deleteReq.deleteReqs, i);
×
3541
    char             *name = pOneReq->tbname;
×
3542
    if (metaGetTableEntryByName(&mr, name) < 0) {
×
3543
      vDebug("vgId:%d, stream delete msg, skip since no table: %s", pVnode->config.vgId, name);
×
3544
      continue;
×
3545
    }
3546

3547
    int64_t uid = mr.me.uid;
×
3548

3549
    int32_t code = tsdbDeleteTableData(pTsdb, ver, deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs, 0);
×
3550
    if (code < 0) {
×
3551
      terrno = code;
×
3552
      vError("vgId:%d, delete error since %s, suid:%" PRId64 ", uid:%" PRId64 ", start ts:%" PRId64 ", end ts:%" PRId64,
×
3553
             TD_VID(pVnode), terrstr(), deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
3554
    }
3555

3556
    if (deleteReq.level == 0) {
×
3557
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, deleteReq.ctimeMs);
×
3558
      if (code < 0) {
×
3559
        terrno = code;
×
3560
        vError("vgId:%d, update change time error since %s, suid:%" PRId64 ", uid:%" PRId64 ", start ts:%" PRId64
×
3561
               ", end ts:%" PRId64,
3562
               TD_VID(pVnode), terrstr(), deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
3563
      }
3564
    }
3565
    tDecoderClear(&mr.coder);
×
3566
  }
3567
  metaReaderClear(&mr);
×
3568
  taosArrayDestroy(deleteReq.deleteReqs);
×
3569
  return 0;
×
3570
}
3571

3572
static int32_t vnodeProcessDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
2,187,140✔
3573
                                     SRpcMsg *pOriginalMsg) {
3574
  int32_t     code = 0;
2,187,140✔
3575
  SDecoder   *pCoder = &(SDecoder){0};
2,187,140✔
3576
  SDeleteRes *pRes = &(SDeleteRes){0};
2,187,140✔
3577

3578
  pRsp->msgType = TDMT_VND_DELETE_RSP;
2,187,140✔
3579
  pRsp->pCont = NULL;
2,187,140✔
3580
  pRsp->contLen = 0;
2,187,140✔
3581
  pRsp->code = TSDB_CODE_SUCCESS;
2,187,140✔
3582

3583
  pRes->uidList = taosArrayInit(0, sizeof(tb_uid_t));
2,187,140✔
3584
  if (pRes->uidList == NULL) {
2,187,140✔
3585
    code = terrno;
×
3586
    goto _err;
×
3587
  }
3588

3589
  tDecoderInit(pCoder, pReq, len);
2,187,140✔
3590
  code = tDecodeDeleteRes(pCoder, pRes);
2,187,140✔
3591
  if (code) goto _err;
2,187,140✔
3592

3593
  if (pRes->affectedRows > 0) {
2,187,140✔
3594
    for (int32_t iUid = 0; iUid < taosArrayGetSize(pRes->uidList); iUid++) {
3,896,080✔
3595
      uint64_t uid = *(uint64_t *)taosArrayGet(pRes->uidList, iUid);
2,222,309✔
3596
      // Merge runtime vnode(db-level) secureDelete to avoid stale client meta after ALTER DATABASE.
3597
      int8_t secureDelete = pRes->secureDelete | pVnode->config.secureDelete;
2,222,309✔
3598
      code = tsdbDeleteTableData(pVnode->pTsdb, ver, pRes->suid, uid, pRes->skey, pRes->ekey, secureDelete);
2,222,309✔
3599
      if (code) goto _err;
2,222,309✔
3600
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, pRes->ctimeMs);
2,222,309✔
3601
      if (code) goto _err;
2,222,309✔
3602
    }
3603
  }
3604

3605
  tDecoderClear(pCoder);
2,187,140✔
3606
  taosArrayDestroy(pRes->uidList);
2,187,140✔
3607

3608
  SVDeleteRsp rsp = {.affectedRows = pRes->affectedRows};
2,187,140✔
3609
  int32_t     ret = 0;
2,187,140✔
3610
  tEncodeSize(tEncodeSVDeleteRsp, &rsp, pRsp->contLen, ret);
2,187,140✔
3611
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
2,187,140✔
3612
  SEncoder ec = {0};
2,187,140✔
3613
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
2,187,140✔
3614
  code = tEncodeSVDeleteRsp(&ec, &rsp);
2,187,140✔
3615
  if (code) goto _err;
2,187,140✔
3616
  tEncoderClear(&ec);
2,187,140✔
3617
  return code;
2,187,140✔
3618

3619
_err:
×
3620
  /*
3621
  if(code == TSDB_CODE_SUCCESS){
3622
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_DELETE, pVnode->monitor.strClusterId,
3623
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
3624
                                        RPC_MSG_USER(pOriginalMsg), "Success"};
3625
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
3626
  }
3627
  else{
3628
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_DELETE, pVnode->monitor.strClusterId,
3629
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
3630
                                        RPC_MSG_USER(pOriginalMsg), "Failed"};
3631
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
3632
  }
3633
  */
3634

3635
  return code;
×
3636
}
3637
static int32_t vnodeProcessCreateIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
18,514✔
3638
  SVCreateStbReq req = {0};
18,514✔
3639
  SDecoder       dc = {0};
18,514✔
3640
  int32_t        code = 0;
18,514✔
3641

3642
  pRsp->msgType = TDMT_VND_CREATE_INDEX_RSP;
18,514✔
3643
  pRsp->code = TSDB_CODE_SUCCESS;
18,514✔
3644
  pRsp->pCont = NULL;
18,514✔
3645
  pRsp->contLen = 0;
18,514✔
3646

3647
  tDecoderInit(&dc, pReq, len);
18,514✔
3648
  // decode req
3649
  if (tDecodeSVCreateStbReq(&dc, &req) < 0) {
18,514✔
3650
    tDecoderClear(&dc);
×
3651
    return terrno = TSDB_CODE_INVALID_MSG;
×
3652
  }
3653

3654
  code = metaAddIndexToSuperTable(pVnode->pMeta, ver, &req);
18,514✔
3655
  if (code) {
18,514✔
3656
    pRsp->code = code;
×
3657
    goto _err;
×
3658
  }
3659
  tDecoderClear(&dc);
18,514✔
3660
  return 0;
18,514✔
3661

3662
_err:
×
3663
  tDecoderClear(&dc);
×
3664
  return code;
×
3665
}
3666
static int32_t vnodeProcessDropIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
15,072✔
3667
  SDropIndexReq req = {0};
15,072✔
3668
  int32_t       code = 0;
15,072✔
3669
  pRsp->msgType = TDMT_VND_DROP_INDEX_RSP;
15,072✔
3670
  pRsp->code = TSDB_CODE_SUCCESS;
15,072✔
3671
  pRsp->pCont = NULL;
15,072✔
3672
  pRsp->contLen = 0;
15,072✔
3673

3674
  if ((code = tDeserializeSDropIdxReq(pReq, len, &req))) {
15,072✔
3675
    pRsp->code = code;
×
3676
    return code;
×
3677
  }
3678

3679
  code = metaDropIndexFromSuperTable(pVnode->pMeta, ver, &req);
15,072✔
3680
  if (code) {
15,072✔
3681
    pRsp->code = code;
×
3682
    return code;
×
3683
  }
3684
  return TSDB_CODE_SUCCESS;
15,072✔
3685
}
3686

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

3689
static int32_t vnodeProcessCompactVnodeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
70,165✔
3690
  if (!pVnode->restored) {
70,165✔
3691
    vInfo("vgId:%d, ignore compact req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
2,583✔
3692
    return 0;
2,583✔
3693
  }
3694
  return vnodeAsyncCompact(pVnode, ver, pReq, len, pRsp);
67,582✔
3695
}
3696

3697
static int32_t vnodeProcessConfigChangeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
3698
  if (syncCheckMember(pVnode->sync) != 0) {
×
3699
    vError("vgId:%d, failed to check member", TD_VID(pVnode));
×
3700
  }
3701

3702
  pRsp->msgType = TDMT_SYNC_CONFIG_CHANGE_RSP;
×
3703
  pRsp->code = TSDB_CODE_SUCCESS;
×
3704
  pRsp->pCont = NULL;
×
3705
  pRsp->contLen = 0;
×
3706

3707
  return 0;
×
3708
}
3709

3710
static int32_t vnodeCheckState(SVnode *pVnode) {
53,348✔
3711
  SSyncState syncState = syncGetState(pVnode->sync);
53,348✔
3712
  if (syncState.state != TAOS_SYNC_STATE_LEADER) {
53,348✔
3713
    return terrno = TSDB_CODE_SYN_NOT_LEADER;
17,297✔
3714
  }
3715
  return 0;
36,051✔
3716
}
3717

3718
static int32_t vnodeCheckToken(SVnode *pVnode, char *member0Token, char *member1Token) {
36,051✔
3719
  char token[TSDB_ARB_TOKEN_SIZE] = {0};
36,051✔
3720
  if (vnodeGetArbToken(pVnode, token) != 0) {
36,051✔
3721
    return terrno = TSDB_CODE_NOT_FOUND;
×
3722
  }
3723

3724
  if (strncmp(token, member0Token, TSDB_ARB_TOKEN_SIZE) != 0 &&
36,051✔
3725
      strncmp(token, member1Token, TSDB_ARB_TOKEN_SIZE) != 0) {
35,311✔
3726
    return terrno = TSDB_CODE_MND_ARB_TOKEN_MISMATCH;
32,247✔
3727
  }
3728

3729
  terrno = TSDB_CODE_SUCCESS;
3,804✔
3730
  return 0;
3,804✔
3731
}
3732

3733
static int32_t vnodeCheckSyncd(SVnode *pVnode, char *member0Token, char *member1Token) {
1,902✔
3734
  int32_t code = vnodeCheckToken(pVnode, member0Token, member1Token);
1,902✔
3735
  if (code != 0) {
1,902✔
3736
    return code;
×
3737
  }
3738

3739
  return syncCheckSynced(pVnode->sync);
1,902✔
3740
}
3741

3742
static int32_t vnodeProcessArbCheckSyncReq(SVnode *pVnode, void *pReq, int32_t len, SRpcMsg *pRsp) {
3,816✔
3743
  int32_t code = 0;
3,816✔
3744

3745
  if ((code = vnodeCheckState(pVnode)) != 0) {
3,816✔
3746
    vDebug("vgId:%d, failed to preprocess vnode-arb-check-sync request since %s", TD_VID(pVnode), tstrerror(code));
1,914✔
3747
    return 0;
1,914✔
3748
  }
3749

3750
  SVArbCheckSyncReq syncReq = {0};
1,902✔
3751

3752
  code = tDeserializeSVArbCheckSyncReq(pReq, len, &syncReq);
1,902✔
3753
  if (code) {
1,902✔
3754
    return code;
×
3755
  }
3756

3757
  vInfo("vgId:%d, start to process vnode-arb-check-sync req QID:0x%" PRIx64 ":0x%" PRIx64 ", seqNum:%" PRIx64
1,902✔
3758
        ", arbToken:%s, member0Token:%s, member1Token:%s, "
3759
        "arbTerm:%" PRId64,
3760
        TD_VID(pVnode), pRsp->info.traceId.rootId, pRsp->info.traceId.msgId, pRsp->info.seqNum, syncReq.arbToken,
3761
        syncReq.member0Token, syncReq.member1Token, syncReq.arbTerm);
3762

3763
  pRsp->msgType = TDMT_VND_ARB_CHECK_SYNC_RSP;
1,902✔
3764
  pRsp->code = TSDB_CODE_SUCCESS;
1,902✔
3765
  pRsp->pCont = NULL;
1,902✔
3766
  pRsp->contLen = 0;
1,902✔
3767

3768
  SVArbCheckSyncRsp syncRsp = {0};
1,902✔
3769
  syncRsp.arbToken = syncReq.arbToken;
1,902✔
3770
  syncRsp.member0Token = syncReq.member0Token;
1,902✔
3771
  syncRsp.member1Token = syncReq.member1Token;
1,902✔
3772
  syncRsp.vgId = TD_VID(pVnode);
1,902✔
3773

3774
  if ((syncRsp.errCode = vnodeCheckSyncd(pVnode, syncReq.member0Token, syncReq.member1Token)) != 0) {
1,902✔
3775
    vError("vgId:%d, failed to check assigned log syncd since %s", TD_VID(pVnode), tstrerror(syncRsp.errCode));
×
3776
  }
3777

3778
  if ((code = vnodeUpdateArbTerm(pVnode, syncReq.arbTerm)) != 0) {
1,902✔
3779
    vError("vgId:%d, failed to update arb term since %s", TD_VID(pVnode), tstrerror(code));
×
3780
    goto _OVER;
×
3781
  }
3782

3783
  int32_t contLen = tSerializeSVArbCheckSyncRsp(NULL, 0, &syncRsp);
1,902✔
3784
  if (contLen <= 0) {
1,902✔
3785
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
3786
    code = -1;
×
3787
    goto _OVER;
×
3788
  }
3789
  void *pHead = rpcMallocCont(contLen);
1,902✔
3790
  if (!pHead) {
1,902✔
3791
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
3792
    code = -1;
×
3793
    goto _OVER;
×
3794
  }
3795

3796
  if (tSerializeSVArbCheckSyncRsp(pHead, contLen, &syncRsp) <= 0) {
1,902✔
3797
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
3798
    rpcFreeCont(pHead);
×
3799
    code = -1;
×
3800
    goto _OVER;
×
3801
  }
3802

3803
  pRsp->pCont = pHead;
1,902✔
3804
  pRsp->contLen = contLen;
1,902✔
3805

3806
  vInfo(
1,902✔
3807
      "vgId:%d, suceed to process vnode-arb-check-sync req rsp.code:%s, arbToken:%s, member0Token:%s, "
3808
      "member1Token:%s",
3809
      TD_VID(pVnode), tstrerror(syncRsp.errCode), syncRsp.arbToken, syncRsp.member0Token, syncRsp.member1Token);
3810

3811
  code = TSDB_CODE_SUCCESS;
1,902✔
3812

3813
_OVER:
1,902✔
3814
  if (code != 0) {
1,902✔
3815
    vError("vgId:%d, failed to process arb check req rsp.code:%s since %s", TD_VID(pVnode), tstrerror(syncRsp.errCode),
×
3816
           tstrerror(code));
3817
  }
3818
  tFreeSVArbCheckSyncReq(&syncReq);
1,902✔
3819
  return code;
1,902✔
3820
}
3821

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