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

taosdata / TDengine / #5011

03 Apr 2026 03:59PM UTC coverage: 72.3% (+0.008%) from 72.292%
#5011

push

travis-ci

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

4053 of 5985 new or added lines in 68 files covered. (67.72%)

732 existing lines in 143 files now uncovered.

257430 of 356056 relevant lines covered (72.3%)

131834103.52 hits per line

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

70.0
/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) {
65,946,371✔
91
  int32_t code = 0;
65,946,371✔
92
  int32_t lino = 0;
65,946,371✔
93

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

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

105
  // name
106
  char *name = NULL;
65,950,634✔
107
  if (tDecodeCStr(pCoder, &name) < 0) {
65,948,973✔
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);
65,948,973✔
114
  if (uid == 0) {
65,939,684✔
115
    uid = tGenIdPI64();
65,346,654✔
116
  }
117
  taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), uid);
65,930,572✔
118

119
  // btime
120
  taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos + 8), btime);
65,931,791✔
121

122
  tEndDecode(pCoder);
65,934,828✔
123

124
_exit:
65,946,738✔
125
  if (code) {
65,947,598✔
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);
65,947,598✔
129
    if (pUid) *pUid = uid;
65,938,787✔
130
  }
131
  return code;
65,937,824✔
132
}
133
static int32_t vnodePreProcessCreateTableMsg(SVnode *pVnode, SRpcMsg *pMsg) {
44,643,700✔
134
  int32_t code = 0;
44,643,700✔
135
  int32_t lino = 0;
44,643,700✔
136

137
  int64_t  btime = taosGetTimestampMs();
44,644,466✔
138
  SDecoder dc = {0};
44,644,466✔
139
  int32_t  nReqs;
44,641,626✔
140

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

147
  if (tDecodeI32v(&dc, &nReqs) < 0) {
44,643,071✔
148
    code = TSDB_CODE_INVALID_MSG;
×
149
    TSDB_CHECK_CODE(code, lino, _exit);
×
150
  }
151
  for (int32_t iReq = 0; iReq < nReqs; iReq++) {
94,777,981✔
152
    code = vnodePreprocessCreateTableReq(pVnode, &dc, btime, NULL);
50,142,262✔
153
    TSDB_CHECK_CODE(code, lino, _exit);
50,134,910✔
154
  }
155

156
  tEndDecode(&dc);
44,635,719✔
157

158
_exit:
44,644,172✔
159
  tDecoderClear(&dc);
44,644,172✔
160
  if (code) {
44,643,483✔
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;
44,643,101✔
165
}
166

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

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

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

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

182
  destroyAlterTbReq(&vAlterTbReq);
12,677,224✔
183

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

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

199
  SMsgHead *pContOld = pMsg->pCont;
12,833,040✔
200
  int32_t   reqLenOld = pMsg->contLen - sizeof(SMsgHead);
12,833,274✔
201

202
  SArray *tbUids = NULL;
12,834,145✔
203
  int64_t timestampMs = 0;
12,834,145✔
204

205
  SVDropTtlTableReq ttlReq = {0};
12,834,145✔
206
  if (tDeserializeSVDropTtlTableReq((char *)pContOld + sizeof(SMsgHead), reqLenOld, &ttlReq) != 0) {
12,833,876✔
207
    code = TSDB_CODE_INVALID_MSG;
×
208
    TSDB_CHECK_CODE(code, lino, _exit);
×
209
  }
210

211
  {  // find expired uids
212
    tbUids = taosArrayInit(8, sizeof(tb_uid_t));
12,840,124✔
213
    if (tbUids == NULL) {
12,837,768✔
214
      code = terrno;
×
215
      TSDB_CHECK_CODE(code, lino, _exit);
×
216
    }
217

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

225
    ttlReq.nUids = taosArrayGetSize(tbUids);
12,827,688✔
226
    ttlReq.pTbUids = tbUids;
12,830,054✔
227
  }
228

229
  if (ttlReq.nUids == 0) {
12,830,054✔
230
    code = TSDB_CODE_MSG_PREPROCESSED;
12,815,343✔
231
    TSDB_CHECK_CODE(code, lino, _exit);
12,815,343✔
232
  }
233

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

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

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

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

255
  code = 0;
12,195✔
256

257
_exit:
12,831,133✔
258
  taosArrayDestroy(tbUids);
12,832,673✔
259

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

267
  return code;
12,810,676✔
268
}
269

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

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

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

289
  int64_t uid;
521,228,971✔
290
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
521,525,549✔
291
    code = vnodePreprocessCreateTableReq(pVnode, pCoder, btimeMs, &uid);
15,804,766✔
292
    TSDB_CHECK_CODE(code, lino, _exit);
15,801,640✔
293
  }
294

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

301
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
521,532,368✔
302
    taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), uid);
15,802,432✔
303
    pCoder->pos += sizeof(int64_t);
15,799,795✔
304
  } else {
305
    if (tDecodeI64(pCoder, &submitTbData.uid) < 0) {
505,720,938✔
306
      code = TSDB_CODE_INVALID_MSG;
×
307
      TSDB_CHECK_CODE(code, lino, _exit);
×
308
    }
309
  }
310

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

316
  // scan and check
317
  TSKEY now = btimeMs;
521,526,228✔
318
  if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_MICRO) {
521,526,228✔
319
    now *= 1000;
2,611,000✔
320
  } else if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_NANO) {
518,912,195✔
321
    now *= 1000000;
1,520,704✔
322
  }
323

324
  int32_t keep = pVnode->config.tsdbCfg.keep2;
521,523,802✔
325

326
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * keep;
521,521,184✔
327
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
521,524,935✔
328
  if (submitTbData.flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
521,521,034✔
329
    uint64_t nColData;
6,671,251✔
330
    if (tDecodeU64v(pCoder, &nColData) < 0) {
6,673,648✔
331
      code = TSDB_CODE_INVALID_MSG;
×
332
      TSDB_CHECK_CODE(code, lino, _exit);
×
333
    }
334

335
    SColData colData = {0};
6,673,648✔
336
    code = tDecodeColData(version, pCoder, &colData, false);
6,670,025✔
337
    if (code) {
6,673,612✔
338
      code = TSDB_CODE_INVALID_MSG;
×
339
      TSDB_CHECK_CODE(code, lino, _exit);
×
340
    }
341

342
    if (colData.flag != HAS_VALUE) {
6,673,612✔
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++) {
20,388,802✔
355
      code = tDecodeColData(version, pCoder, &colData, true);
13,821,977✔
356
      if (code) {
13,822,302✔
357
        code = TSDB_CODE_INVALID_MSG;
×
358
        TSDB_CHECK_CODE(code, lino, _exit);
×
359
      }
360
    }
361
  } else {
362
    uint64_t nRow;
514,551,779✔
363
    if (tDecodeU64v(pCoder, &nRow) < 0) {
514,852,397✔
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,798✔
378
        TSDB_CHECK_CODE(code, lino, _exit);
69,798✔
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)) {
521,455,363✔
390
    taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), ctimeMs);
521,454,007✔
391
    pCoder->pos += sizeof(int64_t);
521,442,858✔
392
  }
393

394
  tEndDecode(pCoder);
521,443,328✔
395

396
_exit:
521,527,398✔
397
  if (code) {
521,526,109✔
398
    vError("vgId:%d, %s:%d failed to vnodePreProcessSubmitTbData submit request since %s", TD_VID(pVnode), __func__,
70,407✔
399
           lino, tstrerror(code));
400
  }
401
  return code;
521,526,109✔
402
}
403
static int32_t vnodePreProcessSubmitMsg(SVnode *pVnode, SRpcMsg *pMsg) {
495,654,943✔
404
  int32_t code = 0;
495,654,943✔
405
  int32_t lino = 0;
495,654,943✔
406

407
  if (tsBypassFlag & TSDB_BYPASS_RA_RPC_RECV_SUBMIT) {
495,654,943✔
408
    return TSDB_CODE_MSG_PREPROCESSED;
67✔
409
  }
410

411
  SDecoder *pCoder = &(SDecoder){0};
495,654,876✔
412

413
  if (taosHton64(((SSubmitReq2Msg *)pMsg->pCont)->version) != 1) {
495,656,109✔
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));
495,656,722✔
419

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

425
  uint64_t nSubmitTbData;
495,358,975✔
426
  if (tDecodeU64v(pCoder, &nSubmitTbData) < 0) {
495,655,367✔
427
    code = TSDB_CODE_INVALID_MSG;
×
428
    TSDB_CHECK_CODE(code, lino, _exit);
×
429
  }
430

431
  int64_t btimeMs = taosGetTimestampMs();
495,656,979✔
432
  int64_t ctimeMs = btimeMs;
495,656,979✔
433
  for (uint64_t i = 0; i < nSubmitTbData; i++) {
1,017,112,232✔
434
    code = vnodePreProcessSubmitTbData(pVnode, pCoder, btimeMs, ctimeMs);
521,529,463✔
435
    TSDB_CHECK_CODE(code, lino, _exit);
521,525,660✔
436
  }
437

438
  tEndDecode(pCoder);
495,582,769✔
439

440
_exit:
495,652,084✔
441
  tDecoderClear(pCoder);
495,656,911✔
442
  if (code) {
495,654,446✔
443
    vError("vgId:%d, %s:%d failed to preprocess submit request since %s, msg type:%s", TD_VID(pVnode), __func__, lino,
70,407✔
444
           tstrerror(code), TMSG_INFO(pMsg->msgType));
445
  }
446
  return code;
495,649,465✔
447
}
448

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

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

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

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

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

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

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

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

485
  taosArrayDestroy(res.uidList);
1,794,389✔
486

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

523
  SVArbCheckSyncReq syncReq = {0};
32,262✔
524

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

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

535
  int32_t code = terrno;
32,262✔
536
  tFreeSVArbCheckSyncReq(&syncReq);
32,262✔
537

538
  return code;
32,262✔
539
}
540

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

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

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

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

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

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

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

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

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

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

619
int32_t vnodePreProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
592,218,667✔
620
  int32_t code = 0;
592,218,667✔
621

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

628
  switch (pMsg->msgType) {
592,220,089✔
629
    case TDMT_VND_CREATE_TABLE: {
44,644,466✔
630
      code = vnodePreProcessCreateTableMsg(pVnode, pMsg);
44,644,466✔
631
    } break;
44,642,335✔
632
    case TDMT_VND_ALTER_TABLE: {
12,677,224✔
633
      code = vnodePreProcessAlterTableMsg(pVnode, pMsg);
12,677,224✔
634
    } break;
12,677,224✔
635
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
12,837,879✔
636
    case TDMT_VND_DROP_TTL_TABLE: {
637
      code = vnodePreProcessDropTtlMsg(pVnode, pMsg);
12,837,879✔
638
    } break;
12,827,404✔
639
    case TDMT_VND_SUBMIT: {
495,655,457✔
640
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.preprocess_time, METRIC_LEVEL_LOW,
495,655,457✔
641
                           { code = vnodePreProcessSubmitMsg(pVnode, pMsg); });
642
    } break;
495,652,826✔
643
    case TDMT_VND_DELETE: {
1,794,389✔
644
      code = vnodePreProcessDeleteMsg(pVnode, pMsg);
1,794,389✔
645
    } break;
1,794,389✔
646
    case TDMT_VND_BATCH_DEL: {
×
647
      code = vnodePreProcessBatchDeleteMsg(pVnode, pMsg);
×
648
    } break;
×
649
    case TDMT_VND_ARB_CHECK_SYNC: {
42,924✔
650
      code = vnodePreProcessArbCheckSyncMsg(pVnode, pMsg);
42,924✔
651
    } break;
42,924✔
652
    case TDMT_VND_DROP_TABLE: {
1,529,905✔
653
      code = vnodePreProcessDropTbMsg(pVnode, pMsg);
1,529,905✔
654
    } break;
1,529,905✔
655
#ifdef TD_ENTERPRISE
656
    case TDMT_VND_SSMIGRATE_FILESET: {
3,805✔
657
      code = vnodePreProcessSsMigrateFileSetReq(pVnode, pMsg);
3,805✔
658
    } break;
3,805✔
659
#endif
660
    case TDMT_VND_AUDIT_RECORD: {
7,552✔
661
      vTrace("vgId:%d, pre process TDMT_VND_AUDIT_RECORD", TD_VID(pVnode));
7,552✔
662
    } break;
264✔
663
    default:
23,029,026✔
664
      break;
23,029,026✔
665
  }
666

667
  if (code && code != TSDB_CODE_MSG_PREPROCESSED) {
592,200,102✔
668
    vError("vgId:%d, failed to preprocess write request since %s, msg type:%s", TD_VID(pVnode), tstrerror(code),
101,555✔
669
           TMSG_INFO(pMsg->msgType));
670
  }
671
  return code;
592,205,728✔
672
}
673

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

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

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

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

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

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

700
    SRow *row = taosArrayGetP(pSubmitTbData->aRowP, i);
23,103,271✔
701
    if (row == NULL) {
23,103,271✔
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,206,542✔
708
      code = TSDB_CODE_INVALID_MSG;
×
709
      TSDB_CHECK_CODE(code, lino, _exit);
×
710
    }
711
  }
712

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

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

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

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

738
  SBseBatch *pBatch = NULL;
70✔
739

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

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

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

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

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

796
  return code;
46,211✔
797
}
798

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

806
  (void)taosThreadMutexLock(&pVnode->mutex);
705,321,538✔
807
  if (pVnode->disableWrite) {
705,388,441✔
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);
705,305,312✔
813

814
  if (ver <= pVnode->state.applied) {
705,392,138✔
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,
705,338,232✔
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)) {
705,383,201✔
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)) {
705,397,459✔
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);
705,398,542✔
839
  atomic_store_64(&pVnode->state.applyTerm, pMsg->info.conn.applyTerm);
705,399,584✔
840

841
  if (!syncUtilUserCommit(pMsg->msgType)) goto _exit;
705,400,231✔
842

843
  // skip header
844
  pReq = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
693,975,446✔
845
  len = pMsg->contLen - sizeof(SMsgHead);
693,975,316✔
846
  bool needCommit = false;
693,976,304✔
847
  bool forceTrimWal = false;
693,976,304✔
848

849
  switch (pMsg->msgType) {
693,976,304✔
850
    /* META */
851
    case TDMT_VND_CREATE_STB:
5,491,735✔
852
      code = vnodeProcessCreateStbReq(pVnode, ver, pReq, len, pRsp);
5,491,735✔
853
      TSDB_CHECK_CODE(code, lino, _err);
5,490,118✔
854
      break;
5,487,802✔
855
    case TDMT_VND_ALTER_STB:
7,774,162✔
856
      code = vnodeProcessAlterStbReq(pVnode, ver, pReq, len, pRsp);
7,774,162✔
857
      TSDB_CHECK_CODE(code, lino, _err);
7,773,604✔
858
      break;
7,773,604✔
859
    case TDMT_VND_DROP_STB:
941,033✔
860
      code = vnodeProcessDropStbReq(pVnode, ver, pReq, len, pRsp, pMsg);
941,033✔
861
      TSDB_CHECK_CODE(code, lino, _err);
940,427✔
862
      break;
940,427✔
863
    case TDMT_VND_CREATE_TABLE:
47,543,080✔
864
      code = vnodeProcessCreateTbReq(pVnode, ver, pReq, len, pRsp, pMsg);
47,543,080✔
865
      TSDB_CHECK_CODE(code, lino, _err);
47,534,432✔
866
      break;
47,534,432✔
867
    case TDMT_VND_ALTER_TABLE:
12,677,902✔
868
      code = vnodeProcessAlterTbReq(pVnode, ver, pReq, len, pRsp);
12,677,902✔
869
      TSDB_CHECK_CODE(code, lino, _err);
12,677,902✔
870
      break;
12,677,902✔
871
    case TDMT_VND_DROP_TABLE:
1,529,972✔
872
    case TDMT_VND_SNODE_DROP_TABLE:
873
      code = vnodeProcessDropTbReq(pVnode, ver, pReq, len, pRsp, pMsg);
1,529,972✔
874
      TSDB_CHECK_CODE(code, lino, _err);
1,529,972✔
875
      break;
1,529,972✔
876
    case TDMT_VND_CREATE_RSMA:
40,352✔
877
      code = vnodeProcessCreateRsmaReq(pVnode, ver, pReq, len, pRsp);
40,352✔
878
      TSDB_CHECK_CODE(code, lino, _err);
40,352✔
879
      break;
40,352✔
880
    case TDMT_VND_DROP_RSMA:
20,306✔
881
      code = vnodeProcessDropRsmaReq(pVnode, ver, pReq, len, pRsp);
20,306✔
882
      TSDB_CHECK_CODE(code, lino, _err);
20,306✔
883
      break;
20,306✔
884
    case TDMT_VND_ALTER_RSMA:
12,436✔
885
      code = vnodeProcessAlterRsmaReq(pVnode, ver, pReq, len, pRsp);
12,436✔
886
      TSDB_CHECK_CODE(code, lino, _err);
12,436✔
887
      break;
12,436✔
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,195✔
893
      code = vnodeProcessFetchTtlExpiredTbs(pVnode, ver, pReq, len, pRsp);
12,195✔
894
      TSDB_CHECK_CODE(code, lino, _err);
12,195✔
895
      break;
12,195✔
896
    case TDMT_VND_TRIM:
79,618✔
897
      code = vnodeProcessTrimReq(pVnode, ver, pReq, len, pRsp);
79,618✔
898
      TSDB_CHECK_CODE(code, lino, _err);
79,618✔
899
      break;
79,618✔
900
      case TDMT_VND_TRIM_WAL:
6,552✔
901
        needCommit = true;
6,552✔
902
        forceTrimWal = true;
6,552✔
903
        break;
6,552✔
904
#ifdef TD_ENTERPRISE
905
    case TDMT_VND_SSMIGRATE_FILESET:
3,805✔
906
      if (vnodeProcessSsMigrateFileSetReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
3,805✔
907
      break;
3,805✔
908
    case TDMT_VND_FOLLOWER_SSMIGRATE:
7,610✔
909
      if (vnodeProcessFollowerSsMigrateReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
7,610✔
910
      break;
7,610✔
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: {
595,861,092✔
918
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.apply_time, METRIC_LEVEL_LOW, {
595,861,092✔
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);
595,585,647✔
922
      break;
595,585,462✔
923
    }
924
    case TDMT_VND_DELETE:
2,198,495✔
925
      code = vnodeProcessDeleteReq(pVnode, ver, pReq, len, pRsp, pMsg);
2,198,495✔
926
      TSDB_CHECK_CODE(code, lino, _err);
2,198,495✔
927
      break;
2,198,495✔
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,250,918✔
935
      code = tqProcessSubscribeReq(pVnode->pTq, ver, pReq, len);
1,250,918✔
936
      TSDB_CHECK_CODE(code, lino, _err);
1,248,351✔
937
      break;
1,248,351✔
938
    case TDMT_VND_TMQ_DELETE_SUB:
285,375✔
939
      code = tqProcessDeleteSubReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen);
285,375✔
940
      TSDB_CHECK_CODE(code, lino, _err);
283,986✔
941
      break;
283,986✔
942
    case TDMT_VND_TMQ_COMMIT_OFFSET:
7,861,296✔
943
      code = tqProcessOffsetCommitReq(pVnode->pTq, ver, pReq, len);
7,861,296✔
944
      TSDB_CHECK_CODE(code, lino, _err);
7,859,701✔
945
      break;
7,859,701✔
946
#endif
947
    case TDMT_VND_ALTER_CONFIRM:
4,516,552✔
948
      needCommit = pVnode->config.hashChange;
4,516,552✔
949
      code = vnodeProcessAlterConfirmReq(pVnode, ver, pReq, len, pRsp);
4,516,552✔
950
      TSDB_CHECK_CODE(code, lino, _err);
4,516,552✔
951
      break;
4,516,552✔
952
    case TDMT_VND_ALTER_CONFIG:
334,366✔
953
      vnodeProcessAlterConfigReq(pVnode, ver, pReq, len, pRsp);
334,366✔
954
      break;
334,366✔
955
    case TDMT_VND_COMMIT:
5,404,882✔
956
      needCommit = true;
5,404,882✔
957
      break;
5,404,882✔
958
    case TDMT_VND_CREATE_INDEX:
18,589✔
959
      vnodeProcessCreateIndexReq(pVnode, ver, pReq, len, pRsp);
18,589✔
960
      break;
18,589✔
961
    case TDMT_VND_DROP_INDEX:
15,162✔
962
      vnodeProcessDropIndexReq(pVnode, ver, pReq, len, pRsp);
15,162✔
963
      break;
15,162✔
964
    case TDMT_VND_COMPACT:
82,634✔
965
      vnodeProcessCompactVnodeReq(pVnode, ver, pReq, len, pRsp);
82,634✔
966
      goto _exit;
82,634✔
967
    case TDMT_VND_SCAN:
480✔
968
      vnodeProcessScanVnodeReq(pVnode, ver, pReq, len, pRsp);
480✔
969
      goto _exit;
480✔
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,544✔
975
      vnodeProcessKillCompactReq(pVnode, ver, pReq, len, pRsp);
1,544✔
976
      break;
1,544✔
977
#endif
978
    case TDMT_VND_KILL_SCAN:
88✔
979
      vnodeProcessKillScanReq(pVnode, ver, pReq, len, pRsp);
88✔
980
      break;
88✔
981
    case TDMT_VND_KILL_TRIM:
×
982
      vnodeProcessKillRetentionReq(pVnode, ver, pReq, len, pRsp);
×
983
      break;
×
984
    /* ARB */
985
    case TDMT_VND_ARB_CHECK_SYNC:
2,226✔
986
      vnodeProcessArbCheckSyncReq(pVnode, pReq, len, pRsp);
2,226✔
987
      break;
2,226✔
988
    case TDMT_VND_AUDIT_RECORD:
264✔
989
      vTrace("vgId:%d, processed audit msg", TD_VID(pVnode));
264✔
990
      code = vnodeProcessAuditRecordReq(pVnode, ver, pReq, len, pMsg);
264✔
991
      pRsp->code = code;
264✔
992
      pRsp->msgType = TDMT_VND_AUDIT_RECORD_RSP;
264✔
993
      pRsp->pCont = NULL;
264✔
994
      pRsp->contLen = 0;
264✔
995
      if (code) {
264✔
996
        goto _err;
×
997
      }
998
      break;
264✔
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);
693,596,681✔
1005

1006
  walApplyVer(pVnode->pWal, ver);
693,616,678✔
1007

1008
  // commit if need
1009
  if (needCommit) {
693,624,272✔
1010
    vInfo("vgId:%d, commit at version %" PRId64, TD_VID(pVnode), ver);
5,444,268✔
1011
    code = vnodeAsyncCommit(pVnode, forceTrimWal);
5,444,268✔
1012
    if (code) {
5,444,268✔
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,444,268✔
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:
693,621,522✔
1028
  return 0;
705,125,745✔
1029

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

1042
int32_t vnodePreprocessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg) {
432,947,388✔
1043
  if (TDMT_SCH_QUERY != pMsg->msgType && TDMT_SCH_MERGE_QUERY != pMsg->msgType) {
432,947,388✔
1044
    return 0;
70,547,270✔
1045
  }
1046

1047
  int32_t qType = 0;
362,413,297✔
1048
  return qWorkerPreprocessQueryMsg(pVnode->pQuery, pMsg, (TDMT_SCH_QUERY == pMsg->msgType), &qType);
362,406,113✔
1049
}
1050

1051
int32_t vnodeProcessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
432,980,021✔
1052
  const STraceId *trace = &pMsg->info.traceId;
432,980,021✔
1053
  vDebug("vgId:%d, msg:%s, %p in vnode query queue is processing, %" PRIx64 ":%" PRIx64, 
432,984,294✔
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)) {
433,000,837✔
1057
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
440,254✔
1058
    return 0;
440,254✔
1059
  }
1060

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

1066
  SReadHandle handle = {0};
432,566,480✔
1067
  handle.vnode = pVnode;
432,565,311✔
1068
  handle.pMsgCb = &pVnode->msgCb;
432,565,311✔
1069
  handle.pWorkerCb = pInfo->workerCb;
432,551,378✔
1070
  initStorageAPI(&handle.api);
432,552,128✔
1071
  int32_t code = TSDB_CODE_SUCCESS;
432,543,477✔
1072
  bool    redirected = false;
432,543,477✔
1073

1074
  switch (pMsg->msgType) {
432,543,477✔
1075
    case TDMT_SCH_QUERY:
300,881,417✔
1076
      if (!syncIsReadyForRead(pVnode->sync)) {
300,881,417✔
1077
        pMsg->code = (terrno) ? terrno : TSDB_CODE_SYN_NOT_LEADER;
2,032,477✔
1078
        redirected = true;
2,032,477✔
1079
      }
1080
      code = qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
300,915,456✔
1081
      if (redirected) {
300,885,905✔
1082
        vnodeRedirectRpcMsg(pVnode, pMsg, pMsg->code);
2,032,477✔
1083
        return 0;
2,032,477✔
1084
      }
1085

1086
      return code;
298,853,428✔
1087
    case TDMT_SCH_MERGE_QUERY:
61,526,142✔
1088
      return qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
61,526,142✔
1089
    case TDMT_SCH_QUERY_CONTINUE:
55,153,841✔
1090
      return qWorkerProcessCQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
55,153,841✔
1091
    case TDMT_VND_TMQ_CONSUME:
14,952,601✔
1092
      return tqProcessPollReq(pVnode->pTq, pMsg);
14,952,601✔
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,095,205,461✔
1100
  int32_t code = TSDB_CODE_SUCCESS;
1,095,205,461✔
1101
  const STraceId *trace = &pMsg->info.traceId;
1,095,205,461✔
1102
  vDebug("vgId:%d, msg:%s, %p in fetch queue is processing, %" PRIx64 ":%" PRIx64, 
1,095,264,281✔
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,095,377,471✔
1105
       pMsg->msgType == TDMT_VND_BATCH_META || pMsg->msgType == TDMT_VND_TABLE_NAME ||
755,564,494✔
1106
       pMsg->msgType == TDMT_VND_VSUBTABLES_META || pMsg->msgType == TDMT_VND_VSTB_REF_DBS) &&
727,001,530✔
1107
      !syncIsReadyForRead(pVnode->sync)) {
368,379,243✔
1108
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
460,799✔
1109
    return 0;
460,799✔
1110
  }
1111

1112
  switch (pMsg->msgType) {
1,094,947,179✔
1113
    case TDMT_SCH_FETCH:
517,945,384✔
1114
    case TDMT_SCH_MERGE_FETCH:
1115
      return qWorkerProcessFetchMsg(pVnode, pVnode->pQuery, pMsg, 0);
517,945,384✔
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:
362,171,753✔
1121
      return qWorkerProcessDropMsg(pVnode, pVnode->pQuery, pMsg, 0);
362,171,753✔
1122
    case TDMT_SCH_TASK_NOTIFY:
31,048✔
1123
      return qWorkerProcessNotifyMsg(pVnode, pVnode->pQuery, pMsg, 0);
31,048✔
1124
    case TDMT_SCH_QUERY_HEARTBEAT:
185,120,468✔
1125
      return qWorkerProcessHbMsg(pVnode, pVnode->pQuery, pMsg, 0);
185,120,468✔
1126
    case TDMT_VND_TABLE_META:
1,278,473✔
1127
    case TDMT_VND_TABLE_NAME:
1128
      return vnodeGetTableMeta(pVnode, pMsg, true);
1,278,473✔
1129
    case TDMT_VND_TABLE_CFG:
×
1130
      return vnodeGetTableCfg(pVnode, pMsg, true);
×
1131
    case TDMT_VND_BATCH_META: {
28,104,521✔
1132
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.fetch_batch_meta_time, METRIC_LEVEL_LOW,
28,104,521✔
1133
                           { code = vnodeGetBatchMeta(pVnode, pMsg); });
1134
      METRICS_UPDATE(pVnode->writeMetrics.fetch_batch_meta_count, METRIC_LEVEL_LOW, 1);
28,104,778✔
1135
      return code;
28,101,472✔
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:
440✔
1142
      return vnodeQueryScanProgress(pVnode, pMsg);
440✔
1143
#ifdef TD_ENTERPRISE
1144
    case TDMT_VND_QUERY_COMPACT_PROGRESS:
122,612✔
1145
      return vnodeQueryCompactProgress(pVnode, pMsg);
122,612✔
1146

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

1150
    case TDMT_VND_QUERY_SSMIGRATE_PROGRESS:
7,610✔
1151
      return vnodeQuerySsMigrateProgress(pVnode, pMsg);
7,610✔
1152
#endif
1153
    case TDMT_VND_QUERY_TRIM_PROGRESS:
132,094✔
1154
      return vnodeQueryRetentionProgress(pVnode, pMsg);
132,094✔
1155
      //    case TDMT_VND_TMQ_CONSUME:
1156
      //      return tqProcessPollReq(pVnode->pTq, pMsg);
1157
#ifdef USE_TQ
1158
    case TDMT_VND_TMQ_VG_WALINFO:
644✔
1159
      return tqProcessVgWalInfoReq(pVnode->pTq, pMsg);
644✔
1160
    case TDMT_VND_TMQ_VG_COMMITTEDINFO:
974✔
1161
      return tqProcessVgCommittedInfoReq(pVnode->pTq, pMsg);
974✔
1162
    case TDMT_VND_TMQ_SEEK:
1,940✔
1163
      return tqProcessSeekReq(pVnode->pTq, pMsg);
1,940✔
1164
#endif
1165
    default:
19,122✔
1166
      vError("unknown msg type:%d in fetch queue", pMsg->msgType);
19,122✔
1167
      return TSDB_CODE_APP_ERROR;
×
1168
  }
1169
}
1170

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

1176
  tstrncpy(pMetaRsp->dbFName, pVnode->config.dbname, TSDB_DB_FNAME_LEN);
74,552,550✔
1177
  pMetaRsp->dbId = pVnode->config.dbId;
74,553,715✔
1178
  pMetaRsp->vgId = TD_VID(pVnode);
74,557,049✔
1179
  pMetaRsp->precision = pVnode->config.tsdbCfg.precision;
74,555,039✔
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) {
79,618✔
1185
  if (!pVnode->restored) {
79,618✔
1186
    vInfo("vgId:%d, ignore trim req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
3,518✔
1187
    return 0;
3,518✔
1188
  }
1189

1190
  int32_t     code = 0;
76,100✔
1191
  SVTrimDbReq trimReq = {0};
76,100✔
1192

1193
  // decode
1194
  if (tDeserializeSVTrimDbReq(pReq, len, &trimReq) != 0) {
76,100✔
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,
76,100✔
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);
76,100✔
1203

1204
_exit:
76,100✔
1205
  return code;
76,100✔
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,805✔
1213
  int32_t code = 0;
3,805✔
1214

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

1222
  if (tDeserializeSSsMigrateFileSetReq(pReq, len, &req) != 0) {
3,805✔
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,805✔
1228
        req.ssMigrateId, req.fid, req.startTimeSec);
1229
  rsp.ssMigrateId = req.ssMigrateId;
3,805✔
1230
  rsp.vgId = TD_VID(pVnode);
3,805✔
1231
  rsp.nodeId = req.nodeId;
3,805✔
1232
  rsp.fid = req.fid;
3,805✔
1233

1234
  code = vnodeAsyncSsMigrateFileSet(pVnode, &req);
3,805✔
1235
  if (code != TSDB_CODE_SUCCESS) {
3,805✔
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,805✔
1242
  pRsp->contLen = tSerializeSSsMigrateFileSetRsp(NULL, 0, &rsp);
3,805✔
1243
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
3,805✔
1244
  if (pRsp->pCont == NULL) {
3,805✔
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,805✔
1250

1251
_exit:
3,805✔
1252
  pRsp->code = code;
3,805✔
1253
  return code;
3,805✔
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) {
7,610✔
1259
  int32_t            code = 0;
7,610✔
1260
  SSsMigrateProgress req = {0};
7,610✔
1261

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

1268
  code = vnodeFollowerSsMigrate(pVnode, &req);
7,610✔
1269

1270
_exit:
7,610✔
1271
  pRsp->code = code;
7,610✔
1272
  return code;
7,610✔
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,195✔
1328
  int32_t                 code = -1;
12,195✔
1329
  SMetaReader             mr = {0};
12,195✔
1330
  SVDropTtlTableReq       ttlReq = {0};
12,195✔
1331
  SVFetchTtlExpiredTbsRsp rsp = {0};
12,195✔
1332
  SEncoder                encoder = {0};
12,195✔
1333
  SArray                 *pNames = NULL;
12,195✔
1334
  pRsp->msgType = TDMT_VND_FETCH_TTL_EXPIRED_TBS_RSP;
12,195✔
1335
  pRsp->code = TSDB_CODE_SUCCESS;
12,195✔
1336
  pRsp->pCont = NULL;
12,195✔
1337
  pRsp->contLen = 0;
12,195✔
1338

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

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

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

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

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

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

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

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

1411
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
5,492,023✔
1412
  pRsp->code = TSDB_CODE_SUCCESS;
5,492,023✔
1413
  pRsp->pCont = NULL;
5,492,228✔
1414
  pRsp->contLen = 0;
5,491,735✔
1415

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

1419
  code = tDecodeSVCreateStbReq(&coder, &req);
5,491,924✔
1420
  if (code) {
5,483,842✔
1421
    pRsp->code = code;
×
1422
    goto _err;
×
1423
  }
1424

1425
  code = metaCreateSuperTable(pVnode->pMeta, ver, &req);
5,483,842✔
1426
  if (code) {
5,492,324✔
1427
    pRsp->code = code;
2,316✔
1428
    goto _err;
2,316✔
1429
  }
1430

1431
  tDecoderClear(&coder);
5,490,008✔
1432
  return 0;
5,489,548✔
1433

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

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

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

1465
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(cRsp));
47,541,531✔
1466
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
47,531,740✔
1467
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
47,536,598✔
1468
  if (rsp.pArray == NULL || tbUids == NULL || tbNames == NULL) {
47,535,965✔
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++) {
102,953,250✔
1476
    pCreateReq = req.pReqs + iReq;
55,410,908✔
1477
    memset(&cRsp, 0, sizeof(cRsp));
55,406,196✔
1478

1479
    if (tsEnableAudit && tsEnableAuditCreateTable && tsAuditLevel >= AUDIT_LEVEL_CHILDTABLE) {
55,406,196✔
1480
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
133✔
1481
      if (str == NULL) {
133✔
1482
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1483
        rcode = -1;
×
1484
        goto _exit;
×
1485
      }
1486
      tstrncpy(str, pCreateReq->name, TSDB_TABLE_FNAME_LEN);
133✔
1487
      if (taosArrayPush(tbNames, &str) == NULL) {
133✔
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);
55,406,196✔
1496
    if (vnodeValidateTableHash(pVnode, tbName) < 0) {
55,407,494✔
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) {
55,397,700✔
1509
      if (pCreateReq->flags & TD_CREATE_IF_NOT_EXISTS && terrno == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
16,563✔
1510
        cRsp.code = TSDB_CODE_SUCCESS;
4,404✔
1511
      } else {
1512
        cRsp.code = terrno;
12,159✔
1513
      }
1514
    } else {
1515
      cRsp.code = TSDB_CODE_SUCCESS;
55,394,718✔
1516
      if (taosArrayPush(tbUids, &pCreateReq->uid) == NULL) {
110,793,068✔
1517
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1518
        rcode = -1;
×
1519
        goto _exit;
×
1520
      }
1521
      vnodeUpdateMetaRsp(pVnode, cRsp.pMeta);
55,398,350✔
1522
    }
1523

1524
    if (taosArrayPush(rsp.pArray, &cRsp) == NULL) {
110,826,491✔
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));
47,542,342✔
1532
  if (tqAddTbUidList(pVnode->pTq, tbUids) < 0) {
47,542,342✔
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;
47,543,296✔
1538
  tEncodeSize(tEncodeSVCreateTbBatchRsp, &rsp, pRsp->contLen, ret);
47,543,296✔
1539
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
47,535,819✔
1540
  if (pRsp->pCont == NULL) {
47,539,804✔
1541
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1542
    rcode = -1;
×
1543
    goto _exit;
×
1544
  }
1545
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
47,539,007✔
1546
  if (tEncodeSVCreateTbBatchRsp(&encoder, &rsp) != 0) {
47,537,996✔
1547
    vError("vgId:%d, failed to encode create table batch response", TD_VID(pVnode));
×
1548
  }
1549

1550
  if (tsEnableAudit && tsEnableAuditCreateTable && tsAuditLevel >= AUDIT_LEVEL_CHILDTABLE) {
47,537,017✔
1551
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
133✔
1552

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

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

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

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

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

1581
_exit:
47,537,017✔
1582
  tDeleteSVCreateTbBatchReq(&req);
47,537,446✔
1583
  taosArrayDestroyEx(rsp.pArray, tFreeSVCreateTbRsp);
47,540,012✔
1584
  taosArrayDestroy(tbUids);
47,528,609✔
1585
  tDecoderClear(&decoder);
47,525,810✔
1586
  tEncoderClear(&encoder);
47,541,306✔
1587
  taosArrayDestroyP(tbNames, NULL);
47,539,462✔
1588
  return rcode;
47,538,077✔
1589
}
1590

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

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

1601
  tDecoderInit(&dc, pReq, len);
7,774,162✔
1602

1603
  // decode req
1604
  code = tDecodeSVCreateStbReq(&dc, &req);
7,774,162✔
1605
  if (code) {
7,760,591✔
1606
    tDecoderClear(&dc);
×
1607
    return code;
×
1608
  }
1609

1610
  code = metaAlterSuperTable(pVnode->pMeta, ver, &req);
7,760,591✔
1611
  if (code) {
7,773,046✔
1612
    pRsp->code = code;
×
1613
    tDecoderClear(&dc);
×
1614
    return code;
×
1615
  }
1616

1617
  tDecoderClear(&dc);
7,773,046✔
1618

1619
  return 0;
7,773,604✔
1620
}
1621

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

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

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

1639
  STraceId* trace = &(pOriginRpc->info.traceId);
941,033✔
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, 
941,033✔
1642
              trace ? trace->msgId : 0, req.name);
1643

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

1660
  // return rsp
1661
_exit:
941,033✔
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, 
941,033✔
1663
              trace ? trace->msgId : 0, req.name);
1664
  if (tbUidList) taosArrayDestroy(tbUidList);
941,033✔
1665
  pRsp->code = rcode;
941,033✔
1666
  tDecoderClear(&decoder);
941,033✔
1667
  return 0;
941,033✔
1668
}
1669

1670
SArray* getCidList(const SArray* tags) {
7,977,181✔
1671
  int32_t       code = 0;
7,977,181✔
1672
  int32_t       lino = 0;
7,977,181✔
1673
  SArray* cidList = taosArrayInit(taosArrayGetSize(tags), sizeof(col_id_t));
7,977,181✔
1674
  QUERY_CHECK_NULL(cidList, code, lino, end, terrno);
7,977,181✔
1675
  for (int32_t i = 0; i < taosArrayGetSize(tags); i++) {
16,015,162✔
1676
    SUpdatedTagVal *pTagVal = taosArrayGet(tags, i);
8,037,981✔
1677
    QUERY_CHECK_NULL(pTagVal, code, lino, end, terrno);
8,037,981✔
1678
    col_id_t cid = pTagVal->colId;
8,037,981✔
1679
    QUERY_CHECK_NULL(taosArrayPush(cidList, &cid), code, lino, end, terrno);
8,037,981✔
1680
  }
1681

1682
end:
7,977,181✔
1683
  if (code != 0) {
7,977,181✔
1684
    taosArrayDestroy(cidList);
×
1685
    return NULL;
×
1686
  }
1687
  return cidList;
7,977,181✔
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,965,806✔
1692
  int32_t       code = 0;
7,965,806✔
1693
  int32_t       lino = 0;
7,965,806✔
1694
  SArray*       cidList = NULL;
7,965,806✔
1695
  SArray*       cids  = NULL;
7,965,806✔
1696
  SArray*       cidListArray = NULL;
7,965,806✔
1697
  if (tags != NULL){
7,965,806✔
1698
    cidList = getCidList(tags);
22,684✔
1699
    QUERY_CHECK_NULL(cidList, code, lino, end, terrno);
22,684✔
1700
  }
1701

1702
  if (tagsArray != NULL) {
7,965,806✔
1703
    cidListArray = taosArrayInit(taosArrayGetSize(tagsArray), sizeof(SArray*));
7,943,122✔
1704
    QUERY_CHECK_NULL(cidListArray, code, lino, end, terrno);
7,943,122✔
1705

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

1719
end:
7,965,806✔
1720
  if (code != 0) {
7,965,806✔
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,965,806✔
1725
  taosArrayDestroy(cids);
7,965,806✔
1726
  taosArrayDestroyP(cidListArray, (FDelete)taosArrayDestroy);
7,965,806✔
1727
}
7,965,806✔
1728

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

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

1744
  tDecoderInit(&dc, pReq, len);
12,677,902✔
1745

1746
  // decode
1747
  if (tDecodeSVAlterTbReq(&dc, &vAlterTbReq) < 0) {
12,677,902✔
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,677,902✔
1755
    vAlterTbRsp.code = terrno;
588,043✔
1756
    tDecoderClear(&dc);
588,043✔
1757
    goto _exit;
588,043✔
1758
  }
1759
  tDecoderClear(&dc);
12,089,859✔
1760

1761
  if (NULL != vMetaRsp.pSchemas) {
12,089,859✔
1762
    vnodeUpdateMetaRsp(pVnode, &vMetaRsp);
3,914,751✔
1763
    vAlterTbRsp.pMeta = &vMetaRsp;
3,914,751✔
1764
  }
1765

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

1775
  tEncoderClear(&ec);
12,677,902✔
1776
  if (vMetaRsp.pSchemas) {
12,677,902✔
1777
    taosMemoryFree(vMetaRsp.pSchemas);
3,914,751✔
1778
    taosMemoryFree(vMetaRsp.pSchemaExt);
3,914,751✔
1779
  }
1780
  if (vMetaRsp.pColRefs) {
12,677,902✔
1781
    taosMemoryFree(vMetaRsp.pColRefs);
286,286✔
1782
  }
1783
  return 0;
12,677,902✔
1784
}
1785

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

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

1802
  // decode req
1803
  tDecoderInit(&decoder, pReq, len);
1,529,972✔
1804
  ret = tDecodeSVDropTbBatchReq(&decoder, &req);
1,529,972✔
1805
  if (ret < 0) {
1,529,972✔
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,529,972✔
1813
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(SVDropTbRsp));
1,529,972✔
1814
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
1,529,972✔
1815
  if (tbUids == NULL || rsp.pArray == NULL || tbNames == NULL) goto _exit;
1,529,972✔
1816

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

1822
    /* code */
1823
    ret = metaDropTable2(pVnode->pMeta, ver, pDropTbReq);
1,578,465✔
1824
    if (ret < 0) {
1,578,465✔
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,578,465✔
1832
    }
1833

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

1840
    if (taosArrayPush(tbUids, &pDropTbReq->uid) == NULL) {
3,156,930✔
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,578,465✔
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,529,972✔
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,529,972✔
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,529,972✔
1897
  taosArrayDestroy(tbUids);
1,529,972✔
1898
  tDecoderClear(&decoder);
1,529,972✔
1899
  tEncodeSize(tEncodeSVDropTbBatchRsp, &rsp, pRsp->contLen, ret);
1,529,972✔
1900
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
1,529,972✔
1901
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
1,529,972✔
1902
  if (tEncodeSVDropTbBatchRsp(&encoder, &rsp) != 0) {
1,529,972✔
1903
    vError("vgId:%d, failed to encode drop table batch response", TD_VID(pVnode));
×
1904
  }
1905
  tEncoderClear(&encoder);
1,529,972✔
1906
  taosArrayDestroy(rsp.pArray);
1,529,972✔
1907
  taosArrayDestroy(tbNames);
1,529,972✔
1908
  return 0;
1,529,972✔
1909
}
1910

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

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

1938
static int32_t vnodeProcessAlterRsmaReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
12,436✔
1939
  int32_t         code = 0, lino = 0;
12,436✔
1940
  SVAlterRsmaReq req = {0};
12,436✔
1941
  TAOS_CHECK_EXIT(tDeserializeSVAlterRsmaReq(pReq, len, &req));
12,436✔
1942
  TAOS_CHECK_EXIT(metaAlterRsma(pVnode->pMeta, ver, &req));
12,436✔
1943
_exit:
12,436✔
1944
  pRsp->msgType = TDMT_VND_ALTER_RSMA_RSP;
12,436✔
1945
  pRsp->pCont = NULL;
12,436✔
1946
  pRsp->code = code;
12,436✔
1947
  pRsp->contLen = 0;
12,436✔
1948
  tFreeSVAlterRsmaReq(&req);
12,436✔
1949
  return code;
12,436✔
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) {
64,356✔
2177
  int32_t code = 0;
64,356✔
2178
  int32_t lino = 0;
64,356✔
2179

2180
  SMetaEntry *pEntry = NULL;
64,356✔
2181
  code = metaFetchEntryByUid(pVnode->pMeta, pSubmitTbData->suid, &pEntry);
64,356✔
2182
  if (code) {
64,356✔
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) {
64,356✔
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));
64,356✔
2194
  if (NULL == *ppRsp) {
64,356✔
2195
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2196
  }
2197
  (*ppRsp)->suid = pSubmitTbData->suid;
64,356✔
2198
  (*ppRsp)->tuid = pSubmitTbData->uid;
64,356✔
2199
  (*ppRsp)->sversion = pEntry->stbEntry.schemaRow.version;
64,356✔
2200
  (*ppRsp)->vgId = pVnode->config.vgId;
64,356✔
2201
  (*ppRsp)->numOfColumns = pEntry->stbEntry.schemaRow.nCols;
64,356✔
2202
  (*ppRsp)->numOfTags = pEntry->stbEntry.schemaTag.nCols;
64,356✔
2203
  (*ppRsp)->pSchemas =
64,356✔
2204
      taosMemoryCalloc(pEntry->stbEntry.schemaRow.nCols + pEntry->stbEntry.schemaTag.nCols, sizeof(SSchema));
64,356✔
2205
  if (NULL == (*ppRsp)->pSchemas) {
64,356✔
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));
64,356✔
2211
  memcpy((*ppRsp)->pSchemas + pEntry->stbEntry.schemaRow.nCols, pEntry->stbEntry.schemaTag.pSchema,
64,356✔
2212
         pEntry->stbEntry.schemaTag.nCols * sizeof(SSchema));
64,356✔
2213
  if (pEntry->pExtSchemas != NULL) {
64,356✔
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) {
64,356✔
2225
    TSDB_CHECK_CODE(code = TSDB_CODE_TDB_TABLE_ALREADY_EXIST, lino, _exit);
936✔
2226
  }
2227
_exit:
64,356✔
2228
  metaFetchEntryFree(&pEntry);
64,356✔
2229
  if (code != TSDB_CODE_SUCCESS) {
64,356✔
2230
    vError("vgId:%d, failed to build exist sub table response, code:0x%0x, line:%d", TD_VID(pVnode), code, lino);
936✔
2231
  }
2232
  return code;
64,356✔
2233
}
2234

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

2239
  SMetaEntry *pEntry = NULL;
300,966✔
2240
  code = metaFetchEntryByUid(pVnode->pMeta, uid, &pEntry);
300,966✔
2241
  if (code) {
300,966✔
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) {
300,966✔
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));
300,966✔
2252
  if (NULL == *ppRsp) {
300,966✔
2253
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2254
  }
2255

2256
  (*ppRsp)->tuid = pEntry->uid;
300,966✔
2257
  (*ppRsp)->vgId = pVnode->config.vgId;
300,966✔
2258
  (*ppRsp)->sversion = pEntry->ntbEntry.schemaRow.version;
300,966✔
2259
  (*ppRsp)->numOfColumns = pEntry->ntbEntry.schemaRow.nCols;
300,966✔
2260
  (*ppRsp)->pSchemas = taosMemoryCalloc(pEntry->ntbEntry.schemaRow.nCols, sizeof(SSchema));
300,966✔
2261
  if (NULL == (*ppRsp)->pSchemas) {
300,966✔
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));
300,966✔
2266
  if (pEntry->pExtSchemas != NULL) {
300,966✔
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:
300,966✔
2277
  metaFetchEntryFree(&pEntry);
300,966✔
2278
  if (code != TSDB_CODE_SUCCESS) {
300,966✔
2279
    vError("vgId:%d, failed to build exist normal table response, code:%d, line:%d", TD_VID(pVnode), code, lino);
×
2280
  }
2281
  return code;
300,966✔
2282
}
2283

2284
static int32_t buildExistTableInStreamRsp(SVnode *pVnode, SSubmitTbData *pSubmitTbData, SVCreateTbRsp *pCreateTbRsp) {
624,061✔
2285
  int32_t code = TSDB_CODE_SUCCESS;
624,061✔
2286
  if (pSubmitTbData->pCreateTbReq->flags & TD_CREATE_NORMAL_TB_IN_STREAM) {
624,061✔
2287
    code = buildExistNormalTalbeRsp(pVnode, pSubmitTbData->uid, &pCreateTbRsp->pMeta);
300,966✔
2288
    if (code) {
300,966✔
2289
      vError("vgId:%d, table uid:%" PRId64 " not exists, line:%d", TD_VID(pVnode), pSubmitTbData->uid, __LINE__);
×
2290
      return code;
×
2291
    }
2292
    pCreateTbRsp->code = TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
300,966✔
2293
    return TSDB_CODE_SUCCESS;
300,966✔
2294
  } else if (pSubmitTbData->pCreateTbReq->flags & TD_CREATE_SUB_TB_IN_STREAM) {
323,056✔
2295
    return buildExistSubTalbeRsp(pVnode, pSubmitTbData, &pCreateTbRsp->pMeta);
63,888✔
2296
  }
2297
  return TSDB_CODE_SUCCESS;
259,207✔
2298
}
2299

2300
static int32_t vnodeHandleAutoCreateTable(SVnode      *pVnode,    // vnode
595,862,363✔
2301
                                          int64_t      version,   // version
2302
                                          SSubmitReq2 *pRequest,  // request
2303
                                          SSubmitRsp2 *pResponse  // response
2304
) {
2305
  int32_t code = TSDB_CODE_SUCCESS;
595,862,363✔
2306
  int32_t numTbData = taosArrayGetSize(pRequest->aSubmitTbData);
595,862,363✔
2307
  SArray *newTbUids = NULL;
595,862,144✔
2308

2309
  for (int32_t i = 0; i < numTbData; ++i) {
1,217,600,263✔
2310
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
621,738,926✔
2311

2312
    if (pTbData->pCreateTbReq == NULL) {
621,739,751✔
2313
      continue;
605,870,941✔
2314
    }
2315

2316
    pTbData->uid = pTbData->pCreateTbReq->uid;
15,868,810✔
2317

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

2330
    // Do create table
2331
    vDebug("vgId:%d start to handle auto create table, version:%" PRId64, TD_VID(pVnode), version);
15,868,849✔
2332

2333
    SVCreateTbRsp *pCreateTbRsp = taosArrayReserve(pResponse->aCreateTbRsp, 1);
15,868,966✔
2334
    pCreateTbRsp->index = i;
15,868,849✔
2335
    code = metaCreateTable2(pVnode->pMeta, version, pTbData->pCreateTbReq, &pCreateTbRsp->pMeta);
15,868,849✔
2336
    if (code == TSDB_CODE_SUCCESS) {
15,868,771✔
2337
      // Allocate necessary resources
2338
      if (newTbUids == NULL) {
15,243,624✔
2339
        newTbUids = taosArrayInit(numTbData, sizeof(int64_t));
12,350,704✔
2340
        if (newTbUids == NULL) {
12,350,704✔
2341
          code = terrno;
×
2342
          vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2343
                 tstrerror(code), version);
2344
          return code;
×
2345
        }
2346
      }
2347

2348
      if (taosArrayPush(newTbUids, &pTbData->uid) == NULL) {
30,487,248✔
2349
        code = terrno;
×
2350
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2351
               tstrerror(code), version);
2352
        taosArrayDestroy(newTbUids);
×
2353
        return code;
×
2354
      }
2355

2356
      if (pCreateTbRsp->pMeta) {
15,243,624✔
2357
        vnodeUpdateMetaRsp(pVnode, pCreateTbRsp->pMeta);
15,243,624✔
2358
      }
2359
    } else if (code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
625,147✔
2360
      code = terrno = 0;
624,061✔
2361
      pTbData->uid = pTbData->pCreateTbReq->uid;  // update uid if table exist for using below
624,139✔
2362

2363
      // stream: get sver from meta, write to pCreateTbRsp, and need to check crateTbReq is same as meta.
2364
      code = buildExistTableInStreamRsp(pVnode, pTbData, pCreateTbRsp);
624,139✔
2365
      if (code) {
624,061✔
2366
        vInfo("vgId:%d failed to create table in stream:%s, code(0x%0x):%s", TD_VID(pVnode),
468✔
2367
              pTbData->pCreateTbReq->name, code, tstrerror(code));
2368
        taosArrayDestroy(newTbUids);
468✔
2369
        return code;
468✔
2370
      }
2371
    } else {
2372
      code = terrno;
1,086✔
2373
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
1,086✔
2374
             tstrerror(code), version);
2375
      taosArrayDestroy(newTbUids);
1,086✔
2376
      return code;
1,086✔
2377
    }
2378
  }
2379

2380
  // Update the affected table uid list
2381
  if (taosArrayGetSize(newTbUids) > 0) {
595,861,337✔
2382
    vDebug("vgId:%d, add %d table into query table list in handling submit", TD_VID(pVnode),
12,350,704✔
2383
           (int32_t)taosArrayGetSize(newTbUids));
2384
    if (tqAddTbUidList(pVnode->pTq, newTbUids) != 0) {
12,350,704✔
2385
      vError("vgId:%d, failed to update tbUid list", TD_VID(pVnode));
×
2386
    }
2387
  }
2388

2389
  vDebug("vgId:%d, handle auto create table done, version:%" PRId64, TD_VID(pVnode), version);
595,860,029✔
2390

2391
  taosArrayDestroy(newTbUids);
595,860,590✔
2392
  return code;
595,861,337✔
2393
}
2394

2395
static void addExistTableInfoIntoRes(SVnode *pVnode, SSubmitReq2 *pRequest, SSubmitRsp2 *pResponse,
468✔
2396
                                     SSubmitTbData *pTbData, int32_t index) {
2397
  int32_t code = 0;
468✔
2398
  int32_t lino = 0;
468✔
2399
  if ((pTbData->flags & SUBMIT_REQ_SCHEMA_RES) == 0) {
468✔
2400
    return;
×
2401
  }
2402
  if (!pResponse->aCreateTbRsp) {
468✔
NEW
2403
    pResponse->aCreateTbRsp = taosArrayInit(index, sizeof(SVCreateTbRsp));
×
NEW
2404
    if (pResponse->aCreateTbRsp == NULL) {
×
NEW
2405
      code = terrno;
×
NEW
2406
      TSDB_CHECK_CODE(code, lino, _exit);
×
2407
    }
2408
  }
2409
  SVCreateTbRsp *pCreateTbRsp = taosArrayReserve(pResponse->aCreateTbRsp, 1);
468✔
2410
  pCreateTbRsp->index = index;
468✔
2411
  if (pCreateTbRsp == NULL) {
468✔
2412
    code = terrno;
×
2413
    TSDB_CHECK_CODE(code, lino, _exit);
×
2414
  }
2415
  if (pTbData->suid == 0) {
468✔
2416
    code = buildExistNormalTalbeRsp(pVnode, pTbData->uid, &pCreateTbRsp->pMeta);
×
2417
    if (code) {
×
2418
      vError("vgId:%d, table uid:%" PRId64 " not exists, line:%d", TD_VID(pVnode), pTbData->uid, __LINE__);
×
2419
    }
2420
  } else {
2421
    code = buildExistSubTalbeRsp(pVnode, pTbData, &pCreateTbRsp->pMeta);
468✔
2422
  }
2423

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

2432
static int32_t vnodeHandleDataWrite(SVnode *pVnode, int64_t version, SSubmitReq2 *pRequest, SSubmitRsp2 *pResponse) {
595,860,590✔
2433
  int32_t code = TSDB_CODE_SUCCESS;
595,860,590✔
2434
  int32_t numTbData = taosArrayGetSize(pRequest->aSubmitTbData);
595,860,590✔
2435
  int8_t  hasBlob = 0;
595,861,352✔
2436

2437
  // Scan submit data
2438
  for (int32_t i = 0; i < numTbData; ++i) {
1,217,333,185✔
2439
    SMetaInfo      info = {0};
621,739,179✔
2440
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
621,739,179✔
2441

2442
    if (pTbData->flags & SUBMIT_REQ_WITH_BLOB) {
621,737,754✔
2443
      hasBlob = 1;
46,211✔
2444
    }
2445
    if (pTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
621,737,754✔
2446
      continue;  // skip column data format
6,675,322✔
2447
    }
2448
    if (pTbData->flags & SUBMIT_REQ_ONLY_CREATE_TABLE) {
615,063,158✔
2449
      continue;  // skip only crate table request
2,197,415✔
2450
    }
2451

2452
    code = metaGetInfo(pVnode->pMeta, pTbData->uid, &info, NULL);
612,864,458✔
2453
    if (code) {
612,864,119✔
2454
      code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
265,329✔
2455
      vWarn("vgId:%d, error occurred at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __FILE__,
265,329✔
2456
            __LINE__, tstrerror(code), version, pTbData->uid);
2457
      return code;
265,329✔
2458
    }
2459

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

2469
    if (info.suid) {
612,598,289✔
2470
      code = metaGetInfo(pVnode->pMeta, info.suid, &info, NULL);
463,304,696✔
2471
      if (code) {
463,306,518✔
2472
        code = TSDB_CODE_INTERNAL_ERROR;
×
2473
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " suid:%" PRId64, TD_VID(pVnode), __func__,
×
2474
               __FILE__, __LINE__, tstrerror(code), version, info.suid);
2475
        return code;
×
2476
      }
2477
    }
2478

2479
    if (pTbData->sver != info.skmVer) {
612,600,111✔
2480
      code = TSDB_CODE_TDB_INVALID_TABLE_SCHEMA_VER;
468✔
2481
      addExistTableInfoIntoRes(pVnode, pRequest, pResponse, pTbData, i);
468✔
2482
      vDebug("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64
468✔
2483
             " sver:%d"
2484
             " info.skmVer:%d",
2485
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), version, pTbData->uid, pTbData->sver,
2486
             info.skmVer);
2487
      return code;
468✔
2488
    }
2489
  }
2490

2491
  // Do write data
2492
  vDebug("vgId:%d start to handle data write, version:%" PRId64, TD_VID(pVnode), version);
595,594,006✔
2493

2494
  for (int32_t i = 0; i < numTbData; ++i) {
1,217,049,961✔
2495
    int32_t        affectedRows = 0;
621,461,945✔
2496
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
621,466,633✔
2497

2498
    if (pTbData->flags & SUBMIT_REQ_ONLY_CREATE_TABLE) {
621,467,641✔
2499
      continue;
2,197,415✔
2500
    }
2501

2502
    if (hasBlob) {
619,270,189✔
2503
      code = vnodeSubmitBlobData(pVnode, pTbData);
46,211✔
2504
      if (code) {
46,211✔
2505
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2506
               __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2507
        return code;
×
2508
      }
2509
    }
2510

2511
    code = tsdbInsertTableData(pVnode->pTsdb, version, pTbData, &affectedRows);
619,270,189✔
2512
    if (code) {
619,251,754✔
2513
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2514
             __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2515
      return code;
×
2516
    }
2517

2518
    code = metaUpdateChangeTimeWithLock(pVnode->pMeta, pTbData->uid, pTbData->ctimeMs);
619,251,754✔
2519
    if (code) {
619,249,495✔
2520
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2521
             __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2522
      return code;
×
2523
    }
2524
    pResponse->affectedRows += affectedRows;
619,249,495✔
2525
  }
2526

2527
  vDebug("vgId:%d, handle data write done, version:%" PRId64 ", affectedRows:%d", TD_VID(pVnode), version,
595,588,016✔
2528
         pResponse->affectedRows);
2529
  return code;
595,595,462✔
2530
}
2531

2532
static int32_t vnodeScanColumnData(SVnode *pVnode, SSubmitTbData *pTbData, TSKEY minKey, TSKEY maxKey) {
6,673,354✔
2533
  int32_t code = 0;
6,673,354✔
2534

2535
  int32_t   numCols = taosArrayGetSize(pTbData->aCol);
6,673,354✔
2536
  SColData *aColData = (SColData *)TARRAY_DATA(pTbData->aCol);
6,673,028✔
2537

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

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

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

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

2571
    tColDataArrGetRowKey(aColData, numCols, i, &key);
2,147,483,647✔
2572

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

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

2593
    *pLastKey = key;
2,147,483,647✔
2594
  }
2595

2596
  return code;
6,675,322✔
2597
}
2598

2599
static int32_t vnodeScanSubmitRowData(SVnode *pVnode, SSubmitTbData *pTbData, TSKEY minKey, TSKEY maxKey) {
615,019,547✔
2600
  int32_t code = 0;
615,019,547✔
2601

2602
  int32_t numRows = taosArrayGetSize(pTbData->aRowP);
615,019,547✔
2603
  SRow  **aRow = (SRow **)TARRAY_DATA(pTbData->aRowP);
615,017,204✔
2604

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

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

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

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

2646
    *pLastKey = key;
2,147,483,647✔
2647
  }
2648

2649
  return code;
608,604,939✔
2650
}
2651

2652
static int32_t vnodeScanSubmitReq(SVnode *pVnode, int64_t version, SSubmitReq2 *pRequest, SSubmitRsp2 *pResponse) {
595,848,313✔
2653
  int32_t code = TSDB_CODE_SUCCESS;
595,848,313✔
2654
  int32_t numTbData = taosArrayGetSize(pRequest->aSubmitTbData);
595,848,313✔
2655

2656
  TSKEY now = taosGetTimestamp(pVnode->config.tsdbCfg.precision);
595,851,380✔
2657
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * pVnode->config.tsdbCfg.keep2;
595,849,648✔
2658
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
595,852,651✔
2659
  for (int32_t i = 0; i < numTbData; i++) {
1,217,556,247✔
2660
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
621,705,685✔
2661

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

2669
    if (pTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
621,698,149✔
2670
      code = vnodeScanColumnData(pVnode, pTbData, minKey, maxKey);
6,673,981✔
2671
      if (code) {
6,675,322✔
2672
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2673
               __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2674
        return code;
×
2675
      }
2676
    } else {
2677
      code = vnodeScanSubmitRowData(pVnode, pTbData, minKey, maxKey);
615,033,318✔
2678
      if (code) {
615,032,358✔
2679
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
84✔
2680
               __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2681
        return code;
×
2682
      }
2683
    }
2684
  }
2685

2686
  return code;
595,850,562✔
2687
}
2688

2689
static int32_t vnodeDecodeAuditRecord(const SJson *pJson, void *pObj) {
726✔
2690
  SAuditRecord *record = (SAuditRecord *)pObj;
726✔
2691
  int32_t       code = 0;
726✔
2692

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

2714
  return 0;
726✔
2715
}
2716

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

2722
  if (tname == NULL || sname == NULL) {
66✔
2723
    return TSDB_CODE_INVALID_PARA;
×
2724
  }
2725

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

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

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

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

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

2765
  SArray *TagNames = NULL;
66✔
2766
  SArray *pTagVals = NULL;
66✔
2767
  STag   *pTag = NULL;
66✔
2768

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

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

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

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

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

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

2820
  if (pTagVals != NULL) {
66✔
2821
    taosArrayDestroy(pTagVals);
66✔
2822
    pTagVals = NULL;
66✔
2823
  }
2824

2825
  return code;
66✔
2826
}
2827

2828
static SArray *vnodePrepareRow(SVnode *pVnode, STSchema *pSchema, SAuditRecord *record) {
726✔
2829
  int32_t code = 0;
726✔
2830
  int32_t lino = 0;
726✔
2831

2832
  SArray *aRows = NULL;
726✔
2833
  SArray *pVals = NULL;
726✔
2834
  SRow   *pRow = NULL;
726✔
2835

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

2841
  if ((pVals = taosArrayInit(1, sizeof(SColVal))) == NULL) {
726✔
2842
    code = terrno;
×
2843
    goto _exit;
×
2844
  }
2845

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

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

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

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

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

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

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

2971
  SArray *aSubmitTbData = NULL;
726✔
2972
  SSubmitTbData tbData = {0};
726✔
2973

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

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

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

2992
    tbData.uid = tGenIdPI64();
66✔
2993
    tbData.flags |= SUBMIT_REQ_AUTO_CREATE_TABLE;
66✔
2994

2995
    tbData.pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
66✔
2996

2997
    if (tbData.pCreateTbReq == NULL) {
66✔
2998
      code = terrno;
×
2999
      TAOS_CHECK_GOTO(code, &lino, _exit);
×
3000
    }
3001

3002
    tbData.pCreateTbReq->uid = tbData.uid;
66✔
3003
    tbData.pCreateTbReq->btime = taosGetTimestampMs();
132✔
3004

3005
    code = vnodePrepareCreateTb(tbData.pCreateTbReq, tbName, suid, pTagSchema, record);
66✔
3006

3007
    metaReaderClear(&merTb);
66✔
3008
    TAOS_CHECK_GOTO(code, &lino, _exit);
66✔
3009
  } else {
3010
    metaReaderClear(&merTb);
×
3011
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
3012
  }
3013

3014
  tbData.suid = suid;
726✔
3015
  tbData.sver = pSchema->version;
726✔
3016

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

3023
  if (NULL == taosArrayPush(aSubmitTbData, &tbData)) {
726✔
3024
    code = terrno;
×
3025
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
3026
  }
3027

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

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

3047
  SAuditRecord record = {0};
726✔
3048
  SSubmitReq2 *pSubmitReq = NULL;
726✔
3049
  SSubmitRsp2 *pSubmitRsp = &(SSubmitRsp2){0};
726✔
3050

3051
  TAOS_CHECK_GOTO(vnodeDecodeAuditRecord(pJson, &record), &lino, _exit);
726✔
3052

3053
  vTrace("vgId:%d, start to audit operation:%s", TD_VID(pVnode), record.operation);
726✔
3054

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

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

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

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

3082
  vTrace("vgId:%d, affectedRow:%d", TD_VID(pVnode), pSubmitRsp->affectedRows);
726✔
3083

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

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

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

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

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

3123
  return code;
726✔
3124
}
3125

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

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

3138
  vTrace("vgId:%d, start to process AuditRecord Req", TD_VID(pVnode));
264✔
3139

3140
  SJson          *pJson = NULL;
264✔
3141
  STSchema       *pSchema = NULL;
264✔
3142
  SSchemaWrapper *pTagSchema = NULL;
264✔
3143
  int64_t         suid = 0;
264✔
3144

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

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

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

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

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

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

3204
  return code;
264✔
3205
}
3206

3207
static int32_t vnodeProcessSubmitReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
595,859,764✔
3208
                                     SRpcMsg *pOriginalMsg) {
3209
  int32_t code = 0;
595,859,764✔
3210
  int32_t lino = 0;
595,859,764✔
3211
  terrno = 0;
595,859,764✔
3212

3213
  SSubmitReq2 *pSubmitReq = &(SSubmitReq2){0};
595,862,140✔
3214
  SSubmitRsp2 *pSubmitRsp = &(SSubmitRsp2){0};
595,862,165✔
3215
  int32_t      ret;
3216
  SEncoder     ec = {0};
595,862,165✔
3217

3218
  pRsp->code = TSDB_CODE_SUCCESS;
595,862,137✔
3219

3220
  void           *pAllocMsg = NULL;
595,862,137✔
3221
  SSubmitReq2Msg *pMsg = (SSubmitReq2Msg *)pReq;
595,862,137✔
3222
  SDecoder        dc = {0};
595,862,137✔
3223
  if (0 == taosHton64(pMsg->version)) {
595,861,814✔
3224
    code = vnodeSubmitReqConvertToSubmitReq2(pVnode, (SSubmitReq *)pMsg, pSubmitReq);
×
3225
    if (TSDB_CODE_SUCCESS == code) {
×
3226
      code = vnodeRebuildSubmitReqMsg(pSubmitReq, &pReq);
×
3227
    }
3228
    if (TSDB_CODE_SUCCESS == code) {
×
3229
      pAllocMsg = pReq;
×
3230
    }
3231
    TSDB_CHECK_CODE(code, lino, _exit);
×
3232
  } else {
3233
    // decode
3234
    pReq = POINTER_SHIFT(pReq, sizeof(SSubmitReq2Msg));
595,862,109✔
3235
    len -= sizeof(SSubmitReq2Msg);
595,862,165✔
3236

3237
    tDecoderInit(&dc, pReq, len);
595,862,165✔
3238
    if (tDecodeSubmitReq(&dc, pSubmitReq, NULL) < 0) {
595,860,548✔
3239
      code = TSDB_CODE_INVALID_MSG;
×
3240
      TSDB_CHECK_CODE(code, lino, _exit);
×
3241
    }
3242
  }
3243

3244
  // Scan the request
3245
  code = vnodeScanSubmitReq(pVnode, ver, pSubmitReq, pSubmitRsp);
595,849,643✔
3246
  if (code) {
595,851,758✔
3247
    vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
3248
           tstrerror(code), ver);
3249
    TSDB_CHECK_CODE(code, lino, _exit);
×
3250
  }
3251

3252
  vGDebug(pOriginalMsg ? &pOriginalMsg->info.traceId : NULL, "vgId:%d, index:%" PRId64 ", submit block, rows:%d",
595,851,758✔
3253
          TD_VID(pVnode), ver, (int32_t)taosArrayGetSize(pSubmitReq->aSubmitTbData));
3254

3255
  // Handle auto create table
3256
  code = vnodeHandleAutoCreateTable(pVnode, ver, pSubmitReq, pSubmitRsp);
595,860,668✔
3257
  if (code) {
595,862,165✔
3258
    vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
1,554✔
3259
           tstrerror(code), ver);
3260
    TSDB_CHECK_CODE(code, lino, _exit);
1,554✔
3261
  }
3262

3263
  // Handle data write
3264
  code = vnodeHandleDataWrite(pVnode, ver, pSubmitReq, pSubmitRsp);
595,860,611✔
3265
  if (code) {
595,860,572✔
3266
    vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
265,797✔
3267
           tstrerror(code), ver);
3268
    TSDB_CHECK_CODE(code, lino, _exit);
265,797✔
3269
  }
3270

3271
_exit:
595,714,914✔
3272
  // message
3273
  pRsp->code = code;
595,862,126✔
3274
  tEncodeSize(tEncodeSSubmitRsp2, pSubmitRsp, pRsp->contLen, ret);
595,862,126✔
3275
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
595,860,894✔
3276
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
595,860,906✔
3277
  if (tEncodeSSubmitRsp2(&ec, pSubmitRsp) < 0) {
595,861,447✔
3278
    vError("vgId:%d, failed to encode submit response", TD_VID(pVnode));
×
3279
  }
3280
  tEncoderClear(&ec);
595,859,422✔
3281

3282
  // update statistics
3283
  (void)atomic_add_fetch_64(&pVnode->statis.nInsert, pSubmitRsp->affectedRows);
595,858,434✔
3284
  (void)atomic_add_fetch_64(&pVnode->statis.nInsertSuccess, pSubmitRsp->affectedRows);
595,860,834✔
3285
  (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsert, 1);
595,860,673✔
3286

3287
  // update metrics
3288
  METRICS_UPDATE(pVnode->writeMetrics.total_requests, METRIC_LEVEL_LOW, 1);
595,860,098✔
3289
  METRICS_UPDATE(pVnode->writeMetrics.total_rows, METRIC_LEVEL_HIGH, pSubmitRsp->affectedRows);
595,858,407✔
3290
  METRICS_UPDATE(pVnode->writeMetrics.total_bytes, METRIC_LEVEL_LOW, pMsg->header.contLen);
595,857,278✔
3291

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

3305
  if (code == 0) {
595,857,873✔
3306
    (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
595,590,496✔
3307
  }
3308

3309
  // clear
3310
  tDestroySubmitReq(pSubmitReq, 0 == taosHton64(pMsg->version) ? TSDB_MSG_FLG_CMPT : TSDB_MSG_FLG_DECODE);
595,860,353✔
3311
  tDestroySSubmitRsp2(pSubmitRsp, TSDB_MSG_FLG_ENCODE);
595,845,918✔
3312

3313
  if (code) {
595,842,277✔
3314
    terrno = code;
267,351✔
3315
    if (code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
267,351✔
3316
      vInfo("vgId:%d, failed to process submit request since %s, lino:%d, version:%" PRId64, TD_VID(pVnode),
468✔
3317
            tstrerror(code), lino, ver);
3318

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

3325
  taosMemoryFree(pAllocMsg);
595,852,223✔
3326
  tDecoderClear(&dc);
595,851,579✔
3327

3328
  return code;
595,861,170✔
3329
}
3330

3331
static int32_t vnodeConsolidateAlterHashRange(SVnode *pVnode, int64_t ver) {
32,834✔
3332
  int32_t code = TSDB_CODE_SUCCESS;
32,834✔
3333

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

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

3340
  return code;
32,834✔
3341
}
3342

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

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

3357
_exit:
4,516,552✔
3358
  pRsp->msgType = TDMT_VND_ALTER_CONFIRM_RSP;
4,516,552✔
3359
  pRsp->code = code;
4,516,552✔
3360
  pRsp->pCont = NULL;
4,516,552✔
3361
  pRsp->contLen = 0;
4,516,552✔
3362

3363
  return code;
4,516,552✔
3364
}
3365

3366
extern int32_t tsdbDisableAndCancelAllBgTask(STsdb *pTsdb);
3367
extern void    tsdbEnableBgTask(STsdb *pTsdb);
3368

3369
static int32_t vnodeProcessAlterConfigReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
334,366✔
3370
  bool walChanged = false;
334,366✔
3371
  bool tsdbChanged = false;
334,366✔
3372

3373
  SAlterVnodeConfigReq req = {0};
334,366✔
3374
  if (tDeserializeSAlterVnodeConfigReq(pReq, len, &req) != 0) {
334,366✔
3375
    terrno = TSDB_CODE_INVALID_MSG;
×
3376
    return TSDB_CODE_INVALID_MSG;
×
3377
  }
3378

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

3388
  if (pVnode->config.cacheLastSize != req.cacheLastSize) {
334,366✔
3389
    pVnode->config.cacheLastSize = req.cacheLastSize;
10,728✔
3390
    tsdbCacheSetCapacity(pVnode, (size_t)pVnode->config.cacheLastSize * 1024 * 1024);
10,728✔
3391
  }
3392

3393
  if (pVnode->config.cacheLastShardBits != req.cacheLastShardBits) {
334,366✔
3394
    vInfo("vgId:%d, cacheLastShardBits change from %d to %d, rebuilding last cache",
1,536✔
3395
          TD_VID(pVnode), pVnode->config.cacheLastShardBits, req.cacheLastShardBits);
3396

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

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

3407
    pVnode->config.cacheLastShardBits = req.cacheLastShardBits;
1,536✔
3408

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

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

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

3430
  if (pVnode->config.cacheLast != req.cacheLast) {
334,366✔
3431
    pVnode->config.cacheLast = req.cacheLast;
96,465✔
3432
  }
3433

3434
  if (pVnode->config.walCfg.fsyncPeriod != req.walFsyncPeriod) {
334,366✔
3435
    pVnode->config.walCfg.fsyncPeriod = req.walFsyncPeriod;
54,576✔
3436
    walChanged = true;
54,576✔
3437
  }
3438

3439
  if (pVnode->config.walCfg.level != req.walLevel) {
334,366✔
3440
    if (pVnode->config.walCfg.level == 0) {
40,190✔
3441
      pVnode->config.walCfg.clearFiles = 1;
2,278✔
3442
    }
3443
    pVnode->config.walCfg.level = req.walLevel;
40,190✔
3444
    walChanged = true;
40,190✔
3445
  }
3446

3447
  if (pVnode->config.walCfg.retentionPeriod != req.walRetentionPeriod) {
334,366✔
3448
    pVnode->config.walCfg.retentionPeriod = req.walRetentionPeriod;
49,633✔
3449
    walChanged = true;
49,633✔
3450
  }
3451

3452
  if (pVnode->config.walCfg.retentionSize != req.walRetentionSize) {
334,366✔
3453
    pVnode->config.walCfg.retentionSize = req.walRetentionSize;
1,510✔
3454
    walChanged = true;
1,510✔
3455
  }
3456

3457
  if (pVnode->config.tsdbCfg.keep0 != req.daysToKeep0) {
334,366✔
3458
    pVnode->config.tsdbCfg.keep0 = req.daysToKeep0;
45,532✔
3459
    tsdbChanged = true;
45,532✔
3460
  }
3461

3462
  if (pVnode->config.tsdbCfg.keep1 != req.daysToKeep1) {
334,366✔
3463
    pVnode->config.tsdbCfg.keep1 = req.daysToKeep1;
44,010✔
3464
    tsdbChanged = true;
44,010✔
3465
  }
3466

3467
  if (pVnode->config.tsdbCfg.keep2 != req.daysToKeep2) {
334,366✔
3468
    pVnode->config.tsdbCfg.keep2 = req.daysToKeep2;
38,036✔
3469
    tsdbChanged = true;
38,036✔
3470
  }
3471

3472
  if (pVnode->config.tsdbCfg.keepTimeOffset != req.keepTimeOffset) {
334,366✔
3473
    pVnode->config.tsdbCfg.keepTimeOffset = req.keepTimeOffset;
7,676✔
3474
    tsdbChanged = true;
7,676✔
3475
  }
3476

3477
  if (req.sttTrigger != -1 && req.sttTrigger != pVnode->config.sttTrigger) {
334,366✔
3478
    if (req.sttTrigger > 1 && pVnode->config.sttTrigger > 1) {
2,632✔
3479
      pVnode->config.sttTrigger = req.sttTrigger;
1,510✔
3480
    } else {
3481
      vnodeAWait(&pVnode->commitTask);
1,122✔
3482

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

3488
      pVnode->config.sttTrigger = req.sttTrigger;
1,122✔
3489
      tsdbEnableBgTask(pVnode->pTsdb);
1,122✔
3490
    }
3491
  }
3492

3493
  if (req.minRows != -1 && req.minRows != pVnode->config.tsdbCfg.minRows) {
334,366✔
3494
    pVnode->config.tsdbCfg.minRows = req.minRows;
1,510✔
3495
  }
3496

3497
  if (req.ssKeepLocal != -1 && req.ssKeepLocal != pVnode->config.ssKeepLocal) {
334,366✔
3498
    pVnode->config.ssKeepLocal = req.ssKeepLocal;
1,510✔
3499
  }
3500
  if (req.ssCompact != -1 && req.ssCompact != pVnode->config.ssCompact) {
334,366✔
3501
    pVnode->config.ssCompact = req.ssCompact;
1,510✔
3502
  }
3503
  if (req.allowDrop != pVnode->config.allowDrop) {
334,366✔
3504
    pVnode->config.allowDrop = req.allowDrop;
×
3505
  }
3506
  if (req.secureDelete != pVnode->config.secureDelete) {
334,366✔
3507
    pVnode->config.secureDelete = req.secureDelete;
1,176✔
3508
  }
3509

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

3516
  if (tsdbChanged) {
334,366✔
3517
    tsdbSetKeepCfg(pVnode->pTsdb, &pVnode->config.tsdbCfg);
60,832✔
3518
  }
3519

3520
  return 0;
334,366✔
3521
}
3522

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

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

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

3545
    int64_t uid = mr.me.uid;
×
3546

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

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

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

3576
  pRsp->msgType = TDMT_VND_DELETE_RSP;
2,198,495✔
3577
  pRsp->pCont = NULL;
2,198,495✔
3578
  pRsp->contLen = 0;
2,198,495✔
3579
  pRsp->code = TSDB_CODE_SUCCESS;
2,198,495✔
3580

3581
  pRes->uidList = taosArrayInit(0, sizeof(tb_uid_t));
2,198,495✔
3582
  if (pRes->uidList == NULL) {
2,198,495✔
3583
    code = terrno;
×
3584
    goto _err;
×
3585
  }
3586

3587
  tDecoderInit(pCoder, pReq, len);
2,198,495✔
3588
  code = tDecodeDeleteRes(pCoder, pRes);
2,198,495✔
3589
  if (code) goto _err;
2,198,495✔
3590

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

3603
  tDecoderClear(pCoder);
2,198,495✔
3604
  taosArrayDestroy(pRes->uidList);
2,198,495✔
3605

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

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

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

3640
  pRsp->msgType = TDMT_VND_CREATE_INDEX_RSP;
18,589✔
3641
  pRsp->code = TSDB_CODE_SUCCESS;
18,589✔
3642
  pRsp->pCont = NULL;
18,589✔
3643
  pRsp->contLen = 0;
18,589✔
3644

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

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

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

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

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

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

3687
static int32_t vnodeProcessCompactVnodeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
82,634✔
3688
  if (!pVnode->restored) {
82,634✔
3689
    vInfo("vgId:%d, ignore compact req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
4,240✔
3690
    return 0;
4,240✔
3691
  }
3692
  return vnodeAsyncCompact(pVnode, ver, pReq, len, pRsp);
78,394✔
3693
}
3694

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

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

3705
  return 0;
×
3706
}
3707

3708
static int32_t vnodeCheckState(SVnode *pVnode) {
45,150✔
3709
  SSyncState syncState = syncGetState(pVnode->sync);
45,150✔
3710
  if (syncState.state != TAOS_SYNC_STATE_LEADER) {
45,150✔
3711
    return terrno = TSDB_CODE_SYN_NOT_LEADER;
11,774✔
3712
  }
3713
  return 0;
33,376✔
3714
}
3715

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

3722
  if (strncmp(token, member0Token, TSDB_ARB_TOKEN_SIZE) != 0 &&
33,376✔
3723
      strncmp(token, member1Token, TSDB_ARB_TOKEN_SIZE) != 0) {
32,172✔
3724
    return terrno = TSDB_CODE_MND_ARB_TOKEN_MISMATCH;
31,148✔
3725
  }
3726

3727
  terrno = TSDB_CODE_SUCCESS;
2,228✔
3728
  return 0;
2,228✔
3729
}
3730

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

3737
  return syncCheckSynced(pVnode->sync);
1,114✔
3738
}
3739

3740
static int32_t vnodeProcessArbCheckSyncReq(SVnode *pVnode, void *pReq, int32_t len, SRpcMsg *pRsp) {
2,226✔
3741
  int32_t code = 0;
2,226✔
3742

3743
  if ((code = vnodeCheckState(pVnode)) != 0) {
2,226✔
3744
    vDebug("vgId:%d, failed to preprocess vnode-arb-check-sync request since %s", TD_VID(pVnode), tstrerror(code));
1,112✔
3745
    return 0;
1,112✔
3746
  }
3747

3748
  SVArbCheckSyncReq syncReq = {0};
1,114✔
3749

3750
  code = tDeserializeSVArbCheckSyncReq(pReq, len, &syncReq);
1,114✔
3751
  if (code) {
1,114✔
3752
    return code;
×
3753
  }
3754

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

3761
  pRsp->msgType = TDMT_VND_ARB_CHECK_SYNC_RSP;
1,114✔
3762
  pRsp->code = TSDB_CODE_SUCCESS;
1,114✔
3763
  pRsp->pCont = NULL;
1,114✔
3764
  pRsp->contLen = 0;
1,114✔
3765

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

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

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

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

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

3801
  pRsp->pCont = pHead;
1,114✔
3802
  pRsp->contLen = contLen;
1,114✔
3803

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

3809
  code = TSDB_CODE_SUCCESS;
1,114✔
3810

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

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