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

taosdata / TDengine / #5028

20 Apr 2026 09:07AM UTC coverage: 72.986% (-0.01%) from 72.996%
#5028

push

travis-ci

web-flow
perf: optimize compact progress query from O(m*n) to O(n+m) (#35115)

104 of 141 new or added lines in 4 files covered. (73.76%)

5170 existing lines in 133 files now uncovered.

273777 of 375111 relevant lines covered (72.99%)

130458474.51 hits per line

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

70.02
/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) {
66,620,518✔
91
  int32_t code = 0;
66,620,518✔
92
  int32_t lino = 0;
66,620,518✔
93

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

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

105
  // name
106
  char *name = NULL;
66,620,760✔
107
  if (tDecodeCStr(pCoder, &name) < 0) {
66,619,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);
66,619,973✔
114
  if (uid == 0) {
66,616,105✔
115
    uid = tGenIdPI64();
66,230,957✔
116
  }
117
  taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), uid);
66,616,786✔
118

119
  // btime
120
  taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos + 8), btime);
66,616,471✔
121

122
  tEndDecode(pCoder);
66,611,156✔
123

124
_exit:
66,612,890✔
125
  if (code) {
66,612,048✔
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);
66,612,048✔
129
    if (pUid) *pUid = uid;
66,613,435✔
130
  }
131
  return code;
66,613,142✔
132
}
133
static int32_t vnodePreProcessCreateTableMsg(SVnode *pVnode, SRpcMsg *pMsg) {
44,865,900✔
134
  int32_t code = 0;
44,865,900✔
135
  int32_t lino = 0;
44,865,900✔
136

137
  int64_t  btime = taosGetTimestampMs();
44,865,900✔
138
  SDecoder dc = {0};
44,865,900✔
139
  int32_t  nReqs;
44,863,289✔
140

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

147
  if (tDecodeI32v(&dc, &nReqs) < 0) {
44,865,900✔
148
    code = TSDB_CODE_INVALID_MSG;
×
149
    TSDB_CHECK_CODE(code, lino, _exit);
×
150
  }
151
  for (int32_t iReq = 0; iReq < nReqs; iReq++) {
95,290,771✔
152
    code = vnodePreprocessCreateTableReq(pVnode, &dc, btime, NULL);
50,431,425✔
153
    TSDB_CHECK_CODE(code, lino, _exit);
50,424,871✔
154
  }
155

156
  tEndDecode(&dc);
44,859,346✔
157

158
_exit:
44,865,140✔
159
  tDecoderClear(&dc);
44,864,808✔
160
  if (code) {
44,865,040✔
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,864,806✔
165
}
166

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

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

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

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

182
  destroyAlterTbReq(&vAlterTbReq);
12,923,600✔
183

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

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

199
  SMsgHead *pContOld = pMsg->pCont;
12,857,846✔
200
  int32_t   reqLenOld = pMsg->contLen - sizeof(SMsgHead);
12,858,815✔
201

202
  SArray *tbUids = NULL;
12,859,660✔
203
  int64_t timestampMs = 0;
12,859,660✔
204

205
  SVDropTtlTableReq ttlReq = {0};
12,859,660✔
206
  if (tDeserializeSVDropTtlTableReq((char *)pContOld + sizeof(SMsgHead), reqLenOld, &ttlReq) != 0) {
12,859,599✔
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,862,854✔
213
    if (tbUids == NULL) {
12,861,612✔
214
      code = terrno;
×
215
      TSDB_CHECK_CODE(code, lino, _exit);
×
216
    }
217

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

225
    ttlReq.nUids = taosArrayGetSize(tbUids);
12,857,895✔
226
    ttlReq.pTbUids = tbUids;
12,856,994✔
227
  }
228

229
  if (ttlReq.nUids == 0) {
12,856,994✔
230
    code = TSDB_CODE_MSG_PREPROCESSED;
12,839,990✔
231
    TSDB_CHECK_CODE(code, lino, _exit);
12,839,990✔
232
  }
233

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

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

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

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

255
  code = 0;
11,645✔
256

257
_exit:
12,856,883✔
258
  taosArrayDestroy(tbUids);
12,857,274✔
259

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

267
  return code;
12,841,990✔
268
}
269

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

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

280
  SSubmitTbData submitTbData;
532,156,452✔
281
  uint8_t       version;
282
  if (tDecodeI32v(pCoder, &submitTbData.flags) < 0) {
532,160,352✔
283
    code = TSDB_CODE_INVALID_MSG;
×
284
    TSDB_CHECK_CODE(code, lino, _exit);
×
285
  }
286
  version = (submitTbData.flags >> 8) & 0xff;
532,160,352✔
287
  submitTbData.flags = submitTbData.flags & 0xff;
532,160,352✔
288

289
  int64_t uid;
532,156,080✔
290
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
532,158,945✔
291
    code = vnodePreprocessCreateTableReq(pVnode, pCoder, btimeMs, &uid);
16,185,797✔
292
    TSDB_CHECK_CODE(code, lino, _exit);
16,184,621✔
293
  }
294

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

301
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
532,158,633✔
302
    taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), uid);
16,184,993✔
303
    pCoder->pos += sizeof(int64_t);
16,184,099✔
304
  } else {
305
    if (tDecodeI64(pCoder, &submitTbData.uid) < 0) {
515,973,609✔
306
      code = TSDB_CODE_INVALID_MSG;
×
307
      TSDB_CHECK_CODE(code, lino, _exit);
×
308
    }
309
  }
310

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

316
  // scan and check
317
  TSKEY now = btimeMs;
532,158,232✔
318
  if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_MICRO) {
532,158,232✔
319
    now *= 1000;
2,651,235✔
320
  } else if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_NANO) {
529,506,967✔
321
    now *= 1000000;
1,536,940✔
322
  }
323

324
  int32_t keep = pVnode->config.tsdbCfg.keep2;
532,158,305✔
325

326
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * keep;
532,155,240✔
327
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
532,153,887✔
328
  if (submitTbData.flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
532,155,811✔
329
    uint64_t nColData;
7,031,390✔
330
    if (tDecodeU64v(pCoder, &nColData) < 0) {
7,031,113✔
331
      code = TSDB_CODE_INVALID_MSG;
×
332
      TSDB_CHECK_CODE(code, lino, _exit);
×
333
    }
334

335
    SColData colData = {0};
7,031,113✔
336
    code = tDecodeColData(version, pCoder, &colData, false);
7,032,149✔
337
    if (code) {
7,032,156✔
338
      code = TSDB_CODE_INVALID_MSG;
×
339
      TSDB_CHECK_CODE(code, lino, _exit);
×
340
    }
341

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

354
    for (uint64_t i = 1; i < nColData; i++) {
21,827,917✔
355
      code = tDecodeColData(version, pCoder, &colData, true);
14,559,832✔
356
      if (code) {
14,559,832✔
357
        code = TSDB_CODE_INVALID_MSG;
×
358
        TSDB_CHECK_CODE(code, lino, _exit);
×
359
      }
360
    }
361
  } else {
362
    uint64_t nRow;
525,120,149✔
363
    if (tDecodeU64v(pCoder, &nRow) < 0) {
525,126,982✔
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;
70,735✔
378
        TSDB_CHECK_CODE(code, lino, _exit);
70,735✔
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)) {
532,085,425✔
390
    taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), ctimeMs);
532,086,605✔
391
    pCoder->pos += sizeof(int64_t);
532,077,704✔
392
  }
393

394
  tEndDecode(pCoder);
532,086,458✔
395

396
_exit:
532,156,942✔
397
  if (code) {
532,159,630✔
398
    vError("vgId:%d, %s:%d failed to vnodePreProcessSubmitTbData submit request since %s", TD_VID(pVnode), __func__,
71,350✔
399
           lino, tstrerror(code));
400
  }
401
  return code;
532,159,630✔
402
}
403
static int32_t vnodePreProcessSubmitMsg(SVnode *pVnode, SRpcMsg *pMsg) {
505,547,982✔
404
  int32_t code = 0;
505,547,982✔
405
  int32_t lino = 0;
505,547,982✔
406

407
  if (tsBypassFlag & TSDB_BYPASS_RA_RPC_RECV_SUBMIT) {
505,547,982✔
408
    return TSDB_CODE_MSG_PREPROCESSED;
65✔
409
  }
410

411
  SDecoder *pCoder = &(SDecoder){0};
505,547,917✔
412

413
  if (taosHton64(((SSubmitReq2Msg *)pMsg->pCont)->version) != 1) {
505,550,357✔
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));
505,550,689✔
419

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

425
  uint64_t nSubmitTbData;
505,546,251✔
426
  if (tDecodeU64v(pCoder, &nSubmitTbData) < 0) {
505,549,571✔
427
    code = TSDB_CODE_INVALID_MSG;
×
428
    TSDB_CHECK_CODE(code, lino, _exit);
×
429
  }
430

431
  int64_t btimeMs = taosGetTimestampMs();
505,549,015✔
432
  int64_t ctimeMs = btimeMs;
505,549,015✔
433
  for (uint64_t i = 0; i < nSubmitTbData; i++) {
1,037,633,316✔
434
    code = vnodePreProcessSubmitTbData(pVnode, pCoder, btimeMs, ctimeMs);
532,159,298✔
435
    TSDB_CHECK_CODE(code, lino, _exit);
532,155,651✔
436
  }
437

438
  tEndDecode(pCoder);
505,474,018✔
439

440
_exit:
505,550,095✔
441
  tDecoderClear(pCoder);
505,550,095✔
442
  if (code) {
505,549,288✔
443
    vError("vgId:%d, %s:%d failed to preprocess submit request since %s, msg type:%s", TD_VID(pVnode), __func__, lino,
71,350✔
444
           tstrerror(code), TMSG_INFO(pMsg->msgType));
445
  }
446
  return code;
505,543,892✔
447
}
448

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

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

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

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

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

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

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

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

485
  taosArrayDestroy(res.uidList);
1,837,142✔
486

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

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

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

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

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

538
  return code;
32,293✔
539
}
540

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

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

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

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

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

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

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

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

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

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

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

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

628
  switch (pMsg->msgType) {
605,673,401✔
629
    case TDMT_VND_CREATE_TABLE: {
44,865,900✔
630
      code = vnodePreProcessCreateTableMsg(pVnode, pMsg);
44,865,900✔
631
    } break;
44,864,771✔
632
    case TDMT_VND_ALTER_TABLE: {
12,923,600✔
633
      code = vnodePreProcessAlterTableMsg(pVnode, pMsg);
12,923,600✔
634
    } break;
12,923,600✔
635
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
12,865,814✔
636
    case TDMT_VND_DROP_TTL_TABLE: {
637
      code = vnodePreProcessDropTtlMsg(pVnode, pMsg);
12,865,814✔
638
    } break;
12,844,224✔
639
    case TDMT_VND_SUBMIT: {
505,551,678✔
640
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.preprocess_time, METRIC_LEVEL_LOW,
505,551,678✔
641
                           { code = vnodePreProcessSubmitMsg(pVnode, pMsg); });
642
    } break;
505,545,836✔
643
    case TDMT_VND_DELETE: {
1,837,142✔
644
      code = vnodePreProcessDeleteMsg(pVnode, pMsg);
1,837,142✔
645
    } break;
1,837,142✔
646
    case TDMT_VND_BATCH_DEL: {
×
647
      code = vnodePreProcessBatchDeleteMsg(pVnode, pMsg);
×
648
    } break;
×
649
    case TDMT_VND_ARB_CHECK_SYNC: {
44,019✔
650
      code = vnodePreProcessArbCheckSyncMsg(pVnode, pMsg);
44,019✔
651
    } break;
44,019✔
652
    case TDMT_VND_DROP_TABLE: {
1,723,114✔
653
      code = vnodePreProcessDropTbMsg(pVnode, pMsg);
1,723,114✔
654
    } break;
1,723,114✔
655
#ifdef TD_ENTERPRISE
656
    case TDMT_VND_SSMIGRATE_FILESET: {
3,900✔
657
      code = vnodePreProcessSsMigrateFileSetReq(pVnode, pMsg);
3,900✔
658
    } break;
3,900✔
659
#endif
660
    case TDMT_VND_AUDIT_RECORD: {
604✔
661
      vTrace("vgId:%d, pre process TDMT_VND_AUDIT_RECORD", TD_VID(pVnode));
604✔
662
    } break;
348✔
663
    default:
25,858,557✔
664
      break;
25,858,557✔
665
  }
666

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

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

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

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

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

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

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

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

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

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

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

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

738
  SBseBatch *pBatch = NULL;
79✔
739

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

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

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

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

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

796
  return code;
46,982✔
797
}
798

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

806
  (void)taosThreadMutexLock(&pVnode->mutex);
723,073,983✔
807
  if (pVnode->disableWrite) {
723,131,707✔
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);
723,086,663✔
813

814
  if (ver <= pVnode->state.applied) {
723,099,098✔
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,
723,075,127✔
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)) {
723,110,466✔
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)) {
723,137,745✔
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);
723,137,920✔
839
  atomic_store_64(&pVnode->state.applyTerm, pMsg->info.conn.applyTerm);
723,138,195✔
840

841
  if (!syncUtilUserCommit(pMsg->msgType)) goto _exit;
723,139,239✔
842

843
  // skip header
844
  pReq = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
712,564,174✔
845
  len = pMsg->contLen - sizeof(SMsgHead);
712,564,220✔
846
  bool needCommit = false;
712,564,793✔
847
  bool forceTrimWal = false;
712,564,793✔
848

849
  switch (pMsg->msgType) {
712,564,793✔
850
    /* META */
851
    case TDMT_VND_CREATE_STB:
5,547,548✔
852
      code = vnodeProcessCreateStbReq(pVnode, ver, pReq, len, pRsp);
5,547,548✔
853
      TSDB_CHECK_CODE(code, lino, _err);
5,547,495✔
854
      break;
5,545,143✔
855
    case TDMT_VND_ALTER_STB:
8,203,676✔
856
      code = vnodeProcessAlterStbReq(pVnode, ver, pReq, len, pRsp);
8,203,676✔
857
      TSDB_CHECK_CODE(code, lino, _err);
8,203,321✔
858
      break;
8,203,321✔
859
    case TDMT_VND_DROP_STB:
964,466✔
860
      code = vnodeProcessDropStbReq(pVnode, ver, pReq, len, pRsp, pMsg);
964,466✔
861
      TSDB_CHECK_CODE(code, lino, _err);
964,466✔
862
      break;
964,466✔
863
    case TDMT_VND_CREATE_TABLE:
47,611,260✔
864
      code = vnodeProcessCreateTbReq(pVnode, ver, pReq, len, pRsp, pMsg);
47,611,260✔
865
      TSDB_CHECK_CODE(code, lino, _err);
47,607,128✔
866
      break;
47,607,128✔
867
    case TDMT_VND_ALTER_TABLE:
12,924,274✔
868
      code = vnodeProcessAlterTbReq(pVnode, ver, pReq, len, pRsp);
12,924,274✔
869
      TSDB_CHECK_CODE(code, lino, _err);
12,924,274✔
870
      break;
12,924,274✔
871
    case TDMT_VND_DROP_TABLE:
1,723,179✔
872
    case TDMT_VND_SNODE_DROP_TABLE:
873
      code = vnodeProcessDropTbReq(pVnode, ver, pReq, len, pRsp, pMsg);
1,723,179✔
874
      TSDB_CHECK_CODE(code, lino, _err);
1,723,179✔
875
      break;
1,723,179✔
876
    case TDMT_VND_CREATE_RSMA:
41,406✔
877
      code = vnodeProcessCreateRsmaReq(pVnode, ver, pReq, len, pRsp);
41,406✔
878
      TSDB_CHECK_CODE(code, lino, _err);
41,406✔
879
      break;
41,406✔
880
    case TDMT_VND_DROP_RSMA:
20,844✔
881
      code = vnodeProcessDropRsmaReq(pVnode, ver, pReq, len, pRsp);
20,844✔
882
      TSDB_CHECK_CODE(code, lino, _err);
20,844✔
883
      break;
20,844✔
884
    case TDMT_VND_ALTER_RSMA:
12,762✔
885
      code = vnodeProcessAlterRsmaReq(pVnode, ver, pReq, len, pRsp);
12,762✔
886
      TSDB_CHECK_CODE(code, lino, _err);
12,762✔
887
      break;
12,762✔
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:
11,645✔
893
      code = vnodeProcessFetchTtlExpiredTbs(pVnode, ver, pReq, len, pRsp);
11,645✔
894
      TSDB_CHECK_CODE(code, lino, _err);
11,645✔
895
      break;
11,645✔
896
    case TDMT_VND_TRIM:
73,861✔
897
      code = vnodeProcessTrimReq(pVnode, ver, pReq, len, pRsp);
73,861✔
898
      TSDB_CHECK_CODE(code, lino, _err);
73,861✔
899
      break;
73,861✔
900
      case TDMT_VND_TRIM_WAL:
6,960✔
901
        needCommit = true;
6,960✔
902
        forceTrimWal = true;
6,960✔
903
        break;
6,960✔
904
#ifdef TD_ENTERPRISE
905
    case TDMT_VND_SSMIGRATE_FILESET:
3,900✔
906
      if (vnodeProcessSsMigrateFileSetReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
3,900✔
907
      break;
3,900✔
908
    case TDMT_VND_FOLLOWER_SSMIGRATE:
7,020✔
909
      if (vnodeProcessFollowerSsMigrateReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
7,020✔
910
      break;
7,020✔
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: {
607,187,563✔
918
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.apply_time, METRIC_LEVEL_LOW, {
607,187,563✔
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);
607,179,434✔
922
      break;
607,176,260✔
923
    }
924
    case TDMT_VND_DELETE:
2,239,506✔
925
      code = vnodeProcessDeleteReq(pVnode, ver, pReq, len, pRsp, pMsg);
2,239,506✔
926
      TSDB_CHECK_CODE(code, lino, _err);
2,239,506✔
927
      break;
2,239,506✔
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,327,367✔
935
      code = tqProcessSubscribeReq(pVnode->pTq, ver, pReq, len);
1,327,367✔
936
      TSDB_CHECK_CODE(code, lino, _err);
1,326,027✔
937
      break;
1,325,738✔
938
    case TDMT_VND_TMQ_DELETE_SUB:
312,754✔
939
      code = tqProcessDeleteSubReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen);
312,754✔
940
      TSDB_CHECK_CODE(code, lino, _err);
311,745✔
941
      break;
311,745✔
942
    case TDMT_VND_TMQ_COMMIT_OFFSET:
14,570,352✔
943
      code = tqProcessOffsetCommitReq(pVnode->pTq, ver, pReq, len);
14,570,352✔
944
      TSDB_CHECK_CODE(code, lino, _err);
14,568,399✔
945
      break;
14,568,399✔
946
#endif
947
    case TDMT_VND_ALTER_CONFIRM:
3,900,024✔
948
      needCommit = pVnode->config.hashChange;
3,900,024✔
949
      code = vnodeProcessAlterConfirmReq(pVnode, ver, pReq, len, pRsp);
3,900,024✔
950
      TSDB_CHECK_CODE(code, lino, _err);
3,900,024✔
951
      break;
3,900,024✔
952
    case TDMT_VND_ALTER_CONFIG:
333,021✔
953
      vnodeProcessAlterConfigReq(pVnode, ver, pReq, len, pRsp);
333,021✔
954
      break;
333,021✔
955
    case TDMT_VND_COMMIT:
5,428,354✔
956
      needCommit = true;
5,428,354✔
957
      break;
5,428,354✔
958
    case TDMT_VND_CREATE_INDEX:
18,506✔
959
      vnodeProcessCreateIndexReq(pVnode, ver, pReq, len, pRsp);
18,506✔
960
      break;
18,506✔
961
    case TDMT_VND_DROP_INDEX:
15,052✔
962
      vnodeProcessDropIndexReq(pVnode, ver, pReq, len, pRsp);
15,052✔
963
      break;
15,052✔
964
    case TDMT_VND_COMPACT:
72,847✔
965
      vnodeProcessCompactVnodeReq(pVnode, ver, pReq, len, pRsp);
72,847✔
966
      goto _exit;
72,847✔
967
    case TDMT_VND_SCAN:
507✔
968
      vnodeProcessScanVnodeReq(pVnode, ver, pReq, len, pRsp);
507✔
969
      goto _exit;
507✔
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,568✔
975
      vnodeProcessKillCompactReq(pVnode, ver, pReq, len, pRsp);
1,568✔
976
      break;
1,568✔
977
#endif
978
    case TDMT_VND_KILL_SCAN:
90✔
979
      vnodeProcessKillScanReq(pVnode, ver, pReq, len, pRsp);
90✔
980
      break;
90✔
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,073✔
986
      vnodeProcessArbCheckSyncReq(pVnode, pReq, len, pRsp);
2,073✔
987
      break;
2,073✔
988
    case TDMT_VND_AUDIT_RECORD:
348✔
989
      vTrace("vgId:%d, processed audit msg", TD_VID(pVnode));
348✔
990
      code = vnodeProcessAuditRecordReq(pVnode, ver, pReq, len, pMsg);
348✔
991
      pRsp->code = code;
348✔
992
      pRsp->msgType = TDMT_VND_AUDIT_RECORD_RSP;
348✔
993
      pRsp->pCont = NULL;
348✔
994
      pRsp->contLen = 0;
348✔
995
      if (code) {
348✔
996
        goto _err;
×
997
      }
998
      break;
348✔
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);
712,466,593✔
1005

1006
  walApplyVer(pVnode->pWal, ver);
712,481,021✔
1007

1008
  // commit if need
1009
  if (needCommit) {
712,485,422✔
1010
    vInfo("vgId:%d, commit at version %" PRId64, TD_VID(pVnode), ver);
5,461,282✔
1011
    code = vnodeAsyncCommit(pVnode, forceTrimWal);
5,461,282✔
1012
    if (code) {
5,461,914✔
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,461,914✔
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:
712,485,668✔
1028
  return 0;
723,127,977✔
1029

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

1039
  return code;
6,013✔
1040
}
1041

1042
int32_t vnodePreprocessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg) {
725,400,652✔
1043
  if (TDMT_SCH_QUERY != pMsg->msgType && TDMT_SCH_MERGE_QUERY != pMsg->msgType) {
725,400,652✔
1044
    return 0;
371,541,098✔
1045
  }
1046

1047
  int32_t qType = 0;
353,901,452✔
1048
  return qWorkerPreprocessQueryMsg(pVnode->pQuery, pMsg, (TDMT_SCH_QUERY == pMsg->msgType), &qType);
353,905,968✔
1049
}
1050

1051
int32_t vnodeProcessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
725,449,101✔
1052
  const STraceId *trace = &pMsg->info.traceId;
725,449,101✔
1053
  vDebug("vgId:%d, msg:%s, %p in vnode query queue is processing, %" PRIx64 ":%" PRIx64, 
725,452,629✔
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)) {
725,477,322✔
1057
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
610,415✔
1058
    return 0;
610,415✔
1059
  }
1060

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

1066
  SReadHandle handle = {0};
724,866,544✔
1067
  handle.vnode = pVnode;
724,868,045✔
1068
  handle.pMsgCb = &pVnode->msgCb;
724,868,045✔
1069
  handle.pWorkerCb = pInfo->workerCb;
724,844,854✔
1070
  initStorageAPI(&handle.api);
724,847,532✔
1071
  int32_t code = TSDB_CODE_SUCCESS;
724,822,458✔
1072
  bool    redirected = false;
724,822,458✔
1073

1074
  switch (pMsg->msgType) {
724,822,458✔
1075
    case TDMT_SCH_QUERY:
292,610,148✔
1076
      if (!syncIsReadyForRead(pVnode->sync)) {
292,610,148✔
1077
        pMsg->code = (terrno) ? terrno : TSDB_CODE_SYN_NOT_LEADER;
1,732,363✔
1078
        redirected = true;
1,732,363✔
1079
      }
1080
      code = qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
292,618,006✔
1081
      if (redirected) {
292,598,000✔
1082
        vnodeRedirectRpcMsg(pVnode, pMsg, pMsg->code);
1,732,363✔
1083
        return 0;
1,732,363✔
1084
      }
1085

1086
      return code;
290,865,637✔
1087
    case TDMT_SCH_MERGE_QUERY:
61,307,424✔
1088
      return qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
61,307,424✔
1089
    case TDMT_SCH_QUERY_CONTINUE:
51,765,626✔
1090
      return qWorkerProcessCQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
51,765,626✔
1091
    case TDMT_VND_TMQ_CONSUME:
319,163,171✔
1092
      return tqProcessPollReq(pVnode->pTq, pMsg);
319,163,171✔
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,051,444,583✔
1100
  int32_t code = TSDB_CODE_SUCCESS;
1,051,444,583✔
1101
  const STraceId *trace = &pMsg->info.traceId;
1,051,444,583✔
1102
  vDebug("vgId:%d, msg:%s, %p in fetch queue is processing, %" PRIx64 ":%" PRIx64, 
1,051,479,253✔
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,051,567,536✔
1105
       pMsg->msgType == TDMT_VND_BATCH_META || pMsg->msgType == TDMT_VND_TABLE_NAME ||
720,097,626✔
1106
       pMsg->msgType == TDMT_VND_VSUBTABLES_META || pMsg->msgType == TDMT_VND_VSTB_REF_DBS) &&
690,896,598✔
1107
      !syncIsReadyForRead(pVnode->sync)) {
360,666,811✔
1108
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
399,822✔
1109
    return 0;
399,822✔
1110
  }
1111

1112
  switch (pMsg->msgType) {
1,051,183,711✔
1113
    case TDMT_SCH_FETCH:
489,436,629✔
1114
    case TDMT_SCH_MERGE_FETCH:
1115
      return qWorkerProcessFetchMsg(pVnode, pVnode->pQuery, pMsg, 0);
489,436,629✔
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:
353,656,139✔
1121
      return qWorkerProcessDropMsg(pVnode, pVnode->pQuery, pMsg, 0);
353,656,139✔
1122
    case TDMT_SCH_TASK_NOTIFY:
33,055✔
1123
      return qWorkerProcessNotifyMsg(pVnode, pVnode->pQuery, pMsg, 0);
33,055✔
1124
    case TDMT_SCH_QUERY_HEARTBEAT:
177,777,457✔
1125
      return qWorkerProcessHbMsg(pVnode, pVnode->pQuery, pMsg, 0);
177,777,457✔
1126
    case TDMT_VND_TABLE_META:
1,315,818✔
1127
    case TDMT_VND_TABLE_NAME:
1128
      return vnodeGetTableMeta(pVnode, pMsg, true);
1,315,818✔
1129
    case TDMT_VND_TABLE_CFG:
×
1130
      return vnodeGetTableCfg(pVnode, pMsg, true);
×
1131
    case TDMT_VND_BATCH_META: {
28,802,536✔
1132
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.fetch_batch_meta_time, METRIC_LEVEL_LOW,
28,802,536✔
1133
                           { code = vnodeGetBatchMeta(pVnode, pMsg); });
1134
      METRICS_UPDATE(pVnode->writeMetrics.fetch_batch_meta_count, METRIC_LEVEL_LOW, 1);
28,804,104✔
1135
      return code;
28,802,189✔
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:
450✔
1142
      return vnodeQueryScanProgress(pVnode, pMsg);
450✔
1143
#ifdef TD_ENTERPRISE
UNCOV
1144
    case TDMT_VND_QUERY_COMPACT_PROGRESS:
×
UNCOV
1145
      return vnodeQueryCompactProgress(pVnode, pMsg);
×
1146

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

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

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

1174
  tstrncpy(pMetaRsp->dbFName, pVnode->config.dbname, TSDB_DB_FNAME_LEN);
75,358,855✔
1175
  pMetaRsp->dbId = pVnode->config.dbId;
75,359,942✔
1176
  pMetaRsp->vgId = TD_VID(pVnode);
75,360,617✔
1177
  pMetaRsp->precision = pVnode->config.tsdbCfg.precision;
75,358,687✔
1178
}
1179

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

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

1188
  int32_t     code = 0;
66,874✔
1189
  SVTrimDbReq trimReq = {0};
66,874✔
1190

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

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

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

1202
_exit:
66,874✔
1203
  return code;
66,874✔
1204
}
1205

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1372
    expiredTb.name = p;
47,262✔
1373
    if (mr.me.type == TSDB_CHILD_TABLE) {
47,262✔
1374
      expiredTb.suid = mr.me.ctbEntry.suid;
32,224✔
1375
    }
1376

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

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

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

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

1409
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
5,548,308✔
1410
  pRsp->code = TSDB_CODE_SUCCESS;
5,547,928✔
1411
  pRsp->pCont = NULL;
5,548,308✔
1412
  pRsp->contLen = 0;
5,547,928✔
1413

1414
  // decode and process req
1415
  tDecoderInit(&coder, pReq, len);
5,547,548✔
1416

1417
  code = tDecodeSVCreateStbReq(&coder, &req);
5,547,164✔
1418
  if (code) {
5,542,937✔
1419
    pRsp->code = code;
×
1420
    goto _err;
×
1421
  }
1422

1423
  code = metaCreateSuperTable(pVnode->pMeta, ver, &req);
5,542,937✔
1424
  if (code) {
5,548,308✔
1425
    pRsp->code = code;
2,352✔
1426
    goto _err;
2,352✔
1427
  }
1428

1429
  tDecoderClear(&coder);
5,545,956✔
1430
  return 0;
5,545,956✔
1431

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

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

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

1463
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(cRsp));
47,609,239✔
1464
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
47,605,178✔
1465
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
47,607,663✔
1466
  if (rsp.pArray == NULL || tbUids == NULL || tbNames == NULL) {
47,607,250✔
1467
    rcode = -1;
330✔
1468
    terrno = TSDB_CODE_OUT_OF_MEMORY;
330✔
1469
    goto _exit;
×
1470
  }
1471

1472
  // loop to create table
1473
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
103,157,292✔
1474
    pCreateReq = req.pReqs + iReq;
55,548,003✔
1475
    memset(&cRsp, 0, sizeof(cRsp));
55,547,009✔
1476

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

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

1505
    // do create table
1506
    if (metaCreateTable2(pVnode->pMeta, ver, pCreateReq, &cRsp.pMeta) < 0) {
55,541,083✔
1507
      if (pCreateReq->flags & TD_CREATE_IF_NOT_EXISTS && terrno == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
15,979✔
1508
        cRsp.code = TSDB_CODE_SUCCESS;
3,905✔
1509
      } else {
1510
        cRsp.code = terrno;
12,074✔
1511
      }
1512
    } else {
1513
      cRsp.code = TSDB_CODE_SUCCESS;
55,533,861✔
1514
      if (taosArrayPush(tbUids, &pCreateReq->uid) == NULL) {
111,068,054✔
1515
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1516
        rcode = -1;
×
1517
        goto _exit;
×
1518
      }
1519
      vnodeUpdateMetaRsp(pVnode, cRsp.pMeta);
55,534,193✔
1520
    }
1521

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

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

1534
  // prepare rsp
1535
  int32_t ret = 0;
47,610,963✔
1536
  tEncodeSize(tEncodeSVCreateTbBatchRsp, &rsp, pRsp->contLen, ret);
47,610,963✔
1537
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
47,608,690✔
1538
  if (pRsp->pCont == NULL) {
47,609,596✔
1539
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1540
    rcode = -1;
×
1541
    goto _exit;
×
1542
  }
1543
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
47,608,702✔
1544
  if (tEncodeSVCreateTbBatchRsp(&encoder, &rsp) != 0) {
47,610,051✔
1545
    vError("vgId:%d, failed to encode create table batch response", TD_VID(pVnode));
×
1546
  }
1547

1548
  if (tsEnableAudit && tsEnableAuditCreateTable && tsAuditLevel >= AUDIT_LEVEL_CHILDTABLE) {
47,608,337✔
1549
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
200✔
1550

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

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

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

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

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

1579
_exit:
47,608,337✔
1580
  tDeleteSVCreateTbBatchReq(&req);
47,605,987✔
1581
  taosArrayDestroyEx(rsp.pArray, tFreeSVCreateTbRsp);
47,607,859✔
1582
  taosArrayDestroy(tbUids);
47,605,234✔
1583
  tDecoderClear(&decoder);
47,599,896✔
1584
  tEncoderClear(&encoder);
47,610,452✔
1585
  taosArrayDestroyP(tbNames, NULL);
47,607,504✔
1586
  return rcode;
47,605,599✔
1587
}
1588

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

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

1599
  tDecoderInit(&dc, pReq, len);
8,203,676✔
1600

1601
  // decode req
1602
  code = tDecodeSVCreateStbReq(&dc, &req);
8,203,676✔
1603
  if (code) {
8,193,452✔
1604
    tDecoderClear(&dc);
×
1605
    return code;
×
1606
  }
1607

1608
  code = metaAlterSuperTable(pVnode->pMeta, ver, &req);
8,193,452✔
1609
  if (code) {
8,202,075✔
1610
    pRsp->code = code;
×
1611
    tDecoderClear(&dc);
×
1612
    return code;
×
1613
  }
1614

1615
  tDecoderClear(&dc);
8,202,075✔
1616

1617
  return 0;
8,203,321✔
1618
}
1619

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

1626
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
964,466✔
1627
  pRsp->pCont = NULL;
964,466✔
1628
  pRsp->contLen = 0;
964,466✔
1629

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

1637
  STraceId* trace = &(pOriginRpc->info.traceId);
964,466✔
1638

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

1642
  // process request
1643
  tbUidList = taosArrayInit(8, sizeof(int64_t));
964,466✔
1644
  if (tbUidList == NULL) goto _exit;
964,466✔
1645
  if (vnodeGetCtbIdList(pVnode, req.suid, tbUidList) != 0){
964,466✔
1646
    rcode = terrno;
×
1647
    goto _exit;
×
1648
  }
1649
  if (metaDropSuperTable(pVnode->pMeta, ver, &req) < 0) {
963,228✔
1650
    rcode = terrno;
2,213✔
1651
    goto _exit;
2,213✔
1652
  }
1653
  if (tqDeleteTbUidList(pVnode->pTq, tbUidList) < 0) {
962,253✔
1654
    rcode = terrno;
×
1655
    goto _exit;
×
1656
  }
1657

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

1668
static SArray* getCidList(const SArray* tags) {
8,159,250✔
1669
  int32_t       code = 0;
8,159,250✔
1670
  int32_t       lino = 0;
8,159,250✔
1671
  SArray* cidList = taosArrayInit(taosArrayGetSize(tags), sizeof(col_id_t));
8,159,250✔
1672
  QUERY_CHECK_NULL(cidList, code, lino, end, terrno);
8,159,250✔
1673
  for (int32_t i = 0; i < taosArrayGetSize(tags); i++) {
16,381,296✔
1674
    SUpdatedTagVal *pTagVal = taosArrayGet(tags, i);
8,222,046✔
1675
    QUERY_CHECK_NULL(pTagVal, code, lino, end, terrno);
8,222,046✔
1676
    col_id_t cid = pTagVal->colId;
8,222,046✔
1677
    QUERY_CHECK_NULL(taosArrayPush(cidList, &cid), code, lino, end, terrno);
8,222,046✔
1678
  }
1679

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

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

1693
  if (tags != NULL){
8,144,819✔
1694
    *cidList = getCidList(tags);
25,798✔
1695
    QUERY_CHECK_NULL(*cidList, code, lino, end, terrno);
25,798✔
1696
  }
1697

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

1702
    for (int32_t i = 0; i < taosArrayGetSize(tagsArray); i++) {
16,252,473✔
1703
      SArray   *obj = taosArrayGetP(tagsArray, i);
8,133,452✔
1704
      cids  = getCidList(obj);
8,133,452✔
1705
      QUERY_CHECK_NULL(cids, code, lino, end, terrno);
8,133,452✔
1706
      QUERY_CHECK_NULL(taosArrayPush(*cidListArray, &cids), code, lino, end, terrno);
16,266,904✔
1707
      cids = NULL;
8,133,452✔
1708
    }
1709
  }
1710

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

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

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

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

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

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

1750
  pRsp->msgType = TDMT_VND_ALTER_TABLE_RSP;
12,924,274✔
1751
  pRsp->pCont = NULL;
12,924,274✔
1752
  pRsp->contLen = 0;
12,924,274✔
1753
  pRsp->code = TSDB_CODE_SUCCESS;
12,924,274✔
1754

1755
  tDecoderInit(&dc, pReq, len);
12,924,274✔
1756

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

1764
  // process
1765
  if (metaAlterTable(pVnode->pMeta, ver, &vAlterTbReq, &vMetaRsp) < 0) {
12,924,274✔
1766
    vAlterTbRsp.code = terrno;
578,949✔
1767
    tDecoderClear(&dc);
578,949✔
1768
    goto _exit;
578,949✔
1769
  }
1770
  tDecoderClear(&dc);
12,345,325✔
1771

1772
  if (NULL != vMetaRsp.pSchemas) {
12,345,325✔
1773
    vnodeUpdateMetaRsp(pVnode, &vMetaRsp);
3,990,893✔
1774
    vAlterTbRsp.pMeta = &vMetaRsp;
3,990,893✔
1775
  }
1776

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

1786
  tEncoderClear(&ec);
12,924,274✔
1787
  if (vMetaRsp.pSchemas) {
12,924,274✔
1788
    taosMemoryFree(vMetaRsp.pSchemas);
3,990,893✔
1789
    taosMemoryFree(vMetaRsp.pSchemaExt);
3,990,893✔
1790
  }
1791
  if (vMetaRsp.pColRefs) {
12,924,274✔
1792
    taosMemoryFree(vMetaRsp.pColRefs);
291,421✔
1793
  }
1794
  return 0;
12,924,274✔
1795
}
1796

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1904
    taosStringBuilderDestroy(&sb);
×
1905
  }
1906

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

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

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

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

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

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

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

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

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

1991
  taosMemoryFreeClear(pSchema);
1992

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

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

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

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

2045
  return TSDB_CODE_SUCCESS;
×
2046
}
2047

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

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

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

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

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

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

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

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

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

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

2117
  return code;
×
2118
}
2119

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

2126
  SSubmitReqConvertCxt cxt = {0};
×
2127

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

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

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

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

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

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

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

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

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

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

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

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

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

2295
static int32_t buildExistTableInStreamRsp(SVnode *pVnode, SSubmitTbData *pSubmitTbData, SVCreateTbRsp *pCreateTbRsp) {
368,942✔
2296
  int32_t code = TSDB_CODE_SUCCESS;
368,942✔
2297
  if (pSubmitTbData->pCreateTbReq->flags & TD_CREATE_NORMAL_TB_IN_STREAM) {
368,942✔
2298
    code = buildExistNormalTalbeRsp(pVnode, pSubmitTbData->uid, &pCreateTbRsp->pMeta);
41,079✔
2299
    if (code) {
41,079✔
2300
      vError("vgId:%d, table uid:%" PRId64 " not exists, line:%d", TD_VID(pVnode), pSubmitTbData->uid, __LINE__);
×
2301
      return code;
×
2302
    }
2303
    pCreateTbRsp->code = TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
41,079✔
2304
    return TSDB_CODE_SUCCESS;
41,079✔
2305
  } else if (pSubmitTbData->pCreateTbReq->flags & TD_CREATE_SUB_TB_IN_STREAM) {
327,826✔
2306
    return buildExistSubTalbeRsp(pVnode, pSubmitTbData, &pCreateTbRsp->pMeta);
68,741✔
2307
  }
2308
  return TSDB_CODE_SUCCESS;
259,159✔
2309
}
2310

2311
static int32_t vnodeHandleAutoCreateTable(SVnode      *pVnode,    // vnode
607,189,612✔
2312
                                          int64_t      version,   // version
2313
                                          SSubmitReq2 *pRequest,  // request
2314
                                          SSubmitRsp2 *pResponse  // response
2315
) {
2316
  int32_t code = TSDB_CODE_SUCCESS;
607,189,612✔
2317
  int32_t numTbData = taosArrayGetSize(pRequest->aSubmitTbData);
607,189,612✔
2318
  SArray *newTbUids = NULL;
607,190,842✔
2319

2320
  for (int32_t i = 0; i < numTbData; ++i) {
1,240,989,644✔
2321
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
633,800,680✔
2322

2323
    if (pTbData->pCreateTbReq == NULL) {
633,800,881✔
2324
      continue;
617,595,103✔
2325
    }
2326

2327
    pTbData->uid = pTbData->pCreateTbReq->uid;
16,205,612✔
2328

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

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

2344
    SVCreateTbRsp *pCreateTbRsp = taosArrayReserve(pResponse->aCreateTbRsp, 1);
16,205,612✔
2345
    pCreateTbRsp->index = i;
16,205,612✔
2346
    code = metaCreateTable2(pVnode->pMeta, version, pTbData->pCreateTbReq, &pCreateTbRsp->pMeta);
16,205,612✔
2347
    if (code == TSDB_CODE_SUCCESS) {
16,205,269✔
2348
      // Allocate necessary resources
2349
      if (newTbUids == NULL) {
15,835,188✔
2350
        newTbUids = taosArrayInit(numTbData, sizeof(int64_t));
12,903,540✔
2351
        if (newTbUids == NULL) {
12,903,540✔
2352
          code = terrno;
×
2353
          vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2354
                 tstrerror(code), version);
2355
          return code;
×
2356
        }
2357
      }
2358

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

2367
      if (pCreateTbRsp->pMeta) {
15,835,531✔
2368
        vnodeUpdateMetaRsp(pVnode, pCreateTbRsp->pMeta);
15,835,188✔
2369
      }
2370
    } else if (code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
370,081✔
2371
      code = terrno = 0;
368,979✔
2372
      pTbData->uid = pTbData->pCreateTbReq->uid;  // update uid if table exist for using below
368,942✔
2373

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

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

2400
  vDebug("vgId:%d, handle auto create table done, version:%" PRId64, TD_VID(pVnode), version);
607,188,798✔
2401

2402
  taosArrayDestroy(newTbUids);
607,188,767✔
2403
  return code;
607,188,798✔
2404
}
2405

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

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

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

2448
  // Scan submit data
2449
  for (int32_t i = 0; i < numTbData; ++i) {
1,240,984,623✔
2450
    SMetaInfo      info = {0};
633,798,102✔
2451
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
633,798,010✔
2452

2453
    if (pTbData->flags & SUBMIT_REQ_WITH_BLOB) {
633,798,556✔
2454
      hasBlob = 1;
46,982✔
2455
    }
2456
    if (pTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
633,798,556✔
2457
      continue;  // skip column data format
7,031,884✔
2458
    }
2459
    if (pTbData->flags & SUBMIT_REQ_ONLY_CREATE_TABLE) {
626,766,749✔
2460
      continue;  // skip only crate table request
2,450,770✔
2461
    }
2462

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

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

2480
    if (info.suid) {
624,314,438✔
2481
      code = metaGetInfo(pVnode->pMeta, info.suid, &info, NULL);
470,729,820✔
2482
      if (code) {
470,731,170✔
2483
        code = TSDB_CODE_INTERNAL_ERROR;
×
2484
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " suid:%" PRId64, TD_VID(pVnode), __func__,
×
2485
               __FILE__, __LINE__, tstrerror(code), version, info.suid);
2486
        return code;
×
2487
      }
2488
    }
2489

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

2502
  // Do write data
2503
  vDebug("vgId:%d start to handle data write, version:%" PRId64, TD_VID(pVnode), version);
607,186,521✔
2504

2505
  for (int32_t i = 0; i < numTbData; ++i) {
1,240,976,324✔
2506
    int32_t        affectedRows = 0;
633,797,372✔
2507
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
633,796,749✔
2508

2509
    if (pTbData->flags & SUBMIT_REQ_ONLY_CREATE_TABLE) {
633,796,790✔
2510
      continue;
2,450,770✔
2511
    }
2512

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

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

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

2538
  vDebug("vgId:%d, handle data write done, version:%" PRId64 ", affectedRows:%d", TD_VID(pVnode), version,
607,178,952✔
2539
         pResponse->affectedRows);
2540
  return code;
607,186,996✔
2541
}
2542

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

2546
  int32_t   numCols = taosArrayGetSize(pTbData->aCol);
7,030,898✔
2547
  SColData *aColData = (SColData *)TARRAY_DATA(pTbData->aCol);
7,031,222✔
2548

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

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

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

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

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

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

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

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

2607
  return code;
7,031,884✔
2608
}
2609

2610
static int32_t vnodeScanSubmitRowData(SVnode *pVnode, SSubmitTbData *pTbData, TSKEY minKey, TSKEY maxKey) {
626,756,482✔
2611
  int32_t code = 0;
626,756,482✔
2612

2613
  int32_t numRows = taosArrayGetSize(pTbData->aRowP);
626,756,482✔
2614
  SRow  **aRow = (SRow **)TARRAY_DATA(pTbData->aRowP);
626,750,298✔
2615

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

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

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

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

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

2660
  return code;
625,180,190✔
2661
}
2662

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

2667
  TSKEY now = taosGetTimestamp(pVnode->config.tsdbCfg.precision);
607,175,593✔
2668
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * pVnode->config.tsdbCfg.keep2;
607,173,738✔
2669
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
607,177,686✔
2670
  for (int32_t i = 0; i < numTbData; i++) {
1,240,958,770✔
2671
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
633,786,723✔
2672

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

2680
    if (pTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
633,791,824✔
2681
      code = vnodeScanColumnData(pVnode, pTbData, minKey, maxKey);
7,030,536✔
2682
      if (code) {
7,031,884✔
2683
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2684
               __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2685
        return code;
×
2686
      }
2687
    } else {
2688
      code = vnodeScanSubmitRowData(pVnode, pTbData, minKey, maxKey);
626,757,444✔
2689
      if (code) {
626,753,658✔
2690
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
5,392✔
2691
               __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2692
        return code;
×
2693
      }
2694
    }
2695
  }
2696

2697
  return code;
607,172,047✔
2698
}
2699

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

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

2725
  return 0;
957✔
2726
}
2727

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2836
  return code;
87✔
2837
}
2838

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

2843
  SArray *aRows = NULL;
957✔
2844
  SArray *pVals = NULL;
957✔
2845
  SRow   *pRow = NULL;
957✔
2846

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

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

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

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

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

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

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

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

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

2982
  SArray *aSubmitTbData = NULL;
957✔
2983
  SSubmitTbData tbData = {0};
957✔
2984

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

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

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

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

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

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

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

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

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

3025
  tbData.suid = suid;
957✔
3026
  tbData.sver = pSchema->version;
957✔
3027

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

3134
  return code;
957✔
3135
}
3136

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

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

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

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

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

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

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

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

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

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

3215
  return code;
348✔
3216
}
3217

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

3224
  SSubmitReq2 *pSubmitReq = &(SSubmitReq2){0};
607,188,258✔
3225
  SSubmitRsp2 *pSubmitRsp = &(SSubmitRsp2){0};
607,188,258✔
3226
  int32_t      ret;
3227
  SEncoder     ec = {0};
607,187,106✔
3228

3229
  pRsp->code = TSDB_CODE_SUCCESS;
607,188,061✔
3230

3231
  void           *pAllocMsg = NULL;
607,188,030✔
3232
  SSubmitReq2Msg *pMsg = (SSubmitReq2Msg *)pReq;
607,188,030✔
3233
  SDecoder        dc = {0};
607,188,030✔
3234
  if (0 == taosHton64(pMsg->version)) {
607,187,864✔
3235
    code = vnodeSubmitReqConvertToSubmitReq2(pVnode, (SSubmitReq *)pMsg, pSubmitReq);
×
3236
    if (TSDB_CODE_SUCCESS == code) {
×
3237
      code = vnodeRebuildSubmitReqMsg(pSubmitReq, &pReq);
×
3238
    }
3239
    if (TSDB_CODE_SUCCESS == code) {
×
3240
      pAllocMsg = pReq;
×
3241
    }
3242
    TSDB_CHECK_CODE(code, lino, _exit);
×
3243
  } else {
3244
    // decode
3245
    pReq = POINTER_SHIFT(pReq, sizeof(SSubmitReq2Msg));
607,187,895✔
3246
    len -= sizeof(SSubmitReq2Msg);
607,188,653✔
3247

3248
    tDecoderInit(&dc, pReq, len);
607,188,653✔
3249
    if (tDecodeSubmitReq(&dc, pSubmitReq, NULL) < 0) {
607,188,321✔
3250
      code = TSDB_CODE_INVALID_MSG;
×
3251
      TSDB_CHECK_CODE(code, lino, _exit);
×
3252
    }
3253
  }
3254

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

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

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

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

3282
_exit:
607,189,411✔
3283
  // message
3284
  pRsp->code = code;
607,189,411✔
3285
  tEncodeSize(tEncodeSSubmitRsp2, pSubmitRsp, pRsp->contLen, ret);
607,189,411✔
3286
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
607,189,547✔
3287
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
607,189,204✔
3288
  if (tEncodeSSubmitRsp2(&ec, pSubmitRsp) < 0) {
607,189,197✔
3289
    vError("vgId:%d, failed to encode submit response", TD_VID(pVnode));
×
3290
  }
3291
  tEncoderClear(&ec);
607,188,419✔
3292

3293
  // update statistics
3294
  (void)atomic_add_fetch_64(&pVnode->statis.nInsert, pSubmitRsp->affectedRows);
607,187,334✔
3295
  (void)atomic_add_fetch_64(&pVnode->statis.nInsertSuccess, pSubmitRsp->affectedRows);
607,189,577✔
3296
  (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsert, 1);
607,187,741✔
3297

3298
  // update metrics
3299
  METRICS_UPDATE(pVnode->writeMetrics.total_requests, METRIC_LEVEL_LOW, 1);
607,185,646✔
3300
  METRICS_UPDATE(pVnode->writeMetrics.total_rows, METRIC_LEVEL_HIGH, pSubmitRsp->affectedRows);
607,185,587✔
3301
  METRICS_UPDATE(pVnode->writeMetrics.total_bytes, METRIC_LEVEL_LOW, pMsg->header.contLen);
607,184,674✔
3302

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

3316
  if (code == 0) {
607,184,081✔
3317
    (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
607,180,709✔
3318
  }
3319

3320
  // clear
3321
  tDestroySubmitReq(pSubmitReq, 0 == taosHton64(pMsg->version) ? TSDB_MSG_FLG_CMPT : TSDB_MSG_FLG_DECODE);
607,187,031✔
3322
  tDestroySSubmitRsp2(pSubmitRsp, TSDB_MSG_FLG_ENCODE);
607,178,823✔
3323

3324
  if (code) {
607,176,121✔
3325
    terrno = code;
3,372✔
3326
    if (code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
3,372✔
3327
      vInfo("vgId:%d, failed to process submit request since %s, lino:%d, version:%" PRId64, TD_VID(pVnode),
468✔
3328
            tstrerror(code), lino, ver);
3329

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

3336
  taosMemoryFree(pAllocMsg);
607,179,759✔
3337
  tDecoderClear(&dc);
607,178,445✔
3338

3339
  return code;
607,186,847✔
3340
}
3341

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

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

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

3351
  return code;
25,762✔
3352
}
3353

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

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

3368
_exit:
3,900,024✔
3369
  pRsp->msgType = TDMT_VND_ALTER_CONFIRM_RSP;
3,900,024✔
3370
  pRsp->code = code;
3,900,024✔
3371
  pRsp->pCont = NULL;
3,900,024✔
3372
  pRsp->contLen = 0;
3,900,024✔
3373

3374
  return code;
3,900,024✔
3375
}
3376

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

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

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

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

3399
  if (pVnode->config.cacheLastSize != req.cacheLastSize) {
333,021✔
3400
    pVnode->config.cacheLastSize = req.cacheLastSize;
5,070✔
3401
    tsdbCacheSetCapacity(pVnode, (size_t)pVnode->config.cacheLastSize * 1024 * 1024);
5,070✔
3402
  }
3403

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

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

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

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

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

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

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

3441
  if (pVnode->config.cacheLast != req.cacheLast) {
333,021✔
3442
    pVnode->config.cacheLast = req.cacheLast;
91,973✔
3443
  }
3444

3445
  if (pVnode->config.walCfg.fsyncPeriod != req.walFsyncPeriod) {
333,021✔
3446
    pVnode->config.walCfg.fsyncPeriod = req.walFsyncPeriod;
55,208✔
3447
    walChanged = true;
55,208✔
3448
  }
3449

3450
  if (pVnode->config.walCfg.level != req.walLevel) {
333,021✔
3451
    if (pVnode->config.walCfg.level == 0) {
40,655✔
3452
      pVnode->config.walCfg.clearFiles = 1;
2,301✔
3453
    }
3454
    pVnode->config.walCfg.level = req.walLevel;
40,655✔
3455
    walChanged = true;
40,655✔
3456
  }
3457

3458
  if (pVnode->config.walCfg.retentionPeriod != req.walRetentionPeriod) {
333,021✔
3459
    pVnode->config.walCfg.retentionPeriod = req.walRetentionPeriod;
50,343✔
3460
    walChanged = true;
50,343✔
3461
  }
3462

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

3468
  if (pVnode->config.tsdbCfg.keep0 != req.daysToKeep0) {
333,021✔
3469
    pVnode->config.tsdbCfg.keep0 = req.daysToKeep0;
46,194✔
3470
    tsdbChanged = true;
46,194✔
3471
  }
3472

3473
  if (pVnode->config.tsdbCfg.keep1 != req.daysToKeep1) {
333,021✔
3474
    pVnode->config.tsdbCfg.keep1 = req.daysToKeep1;
44,634✔
3475
    tsdbChanged = true;
44,634✔
3476
  }
3477

3478
  if (pVnode->config.tsdbCfg.keep2 != req.daysToKeep2) {
333,021✔
3479
    pVnode->config.tsdbCfg.keep2 = req.daysToKeep2;
38,502✔
3480
    tsdbChanged = true;
38,502✔
3481
  }
3482

3483
  if (pVnode->config.tsdbCfg.keepTimeOffset != req.keepTimeOffset) {
333,021✔
3484
    pVnode->config.tsdbCfg.keepTimeOffset = req.keepTimeOffset;
7,770✔
3485
    tsdbChanged = true;
7,770✔
3486
  }
3487

3488
  if (req.sttTrigger != -1 && req.sttTrigger != pVnode->config.sttTrigger) {
333,021✔
3489
    if (req.sttTrigger > 1 && pVnode->config.sttTrigger > 1) {
2,666✔
3490
      pVnode->config.sttTrigger = req.sttTrigger;
1,526✔
3491
    } else {
3492
      vnodeAWait(&pVnode->commitTask);
1,140✔
3493

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

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

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

3508
  if (req.ssKeepLocal != -1 && req.ssKeepLocal != pVnode->config.ssKeepLocal) {
333,021✔
3509
    pVnode->config.ssKeepLocal = req.ssKeepLocal;
1,526✔
3510
  }
3511
  if (req.ssCompact != -1 && req.ssCompact != pVnode->config.ssCompact) {
333,021✔
3512
    pVnode->config.ssCompact = req.ssCompact;
1,526✔
3513
  }
3514
  if (req.allowDrop != pVnode->config.allowDrop) {
333,021✔
3515
    pVnode->config.allowDrop = req.allowDrop;
×
3516
  }
3517
  if (req.secureDelete != pVnode->config.secureDelete) {
333,021✔
3518
    pVnode->config.secureDelete = req.secureDelete;
1,190✔
3519
  }
3520

3521
  if (walChanged) {
333,021✔
3522
    if (walAlter(pVnode->pWal, &pVnode->config.walCfg) != 0) {
144,612✔
3523
      vError("vgId:%d, failed to alter wal config since %s", TD_VID(pVnode), tstrerror(ERRNO));
×
3524
    }
3525
  }
3526

3527
  if (tsdbChanged) {
333,021✔
3528
    tsdbSetKeepCfg(pVnode->pTsdb, &pVnode->config.tsdbCfg);
61,756✔
3529
  }
3530

3531
  return 0;
333,021✔
3532
}
3533

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

3543
  SMetaReader mr = {0};
×
3544
  metaReaderDoInit(&mr, pVnode->pMeta, META_READER_NOLOCK);
×
3545
  STsdb *pTsdb = pVnode->pTsdb;
×
3546

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

3556
    int64_t uid = mr.me.uid;
×
3557

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

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

3581
static int32_t vnodeProcessDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
2,239,506✔
3582
                                     SRpcMsg *pOriginalMsg) {
3583
  int32_t     code = 0;
2,239,506✔
3584
  SDecoder   *pCoder = &(SDecoder){0};
2,239,506✔
3585
  SDeleteRes *pRes = &(SDeleteRes){0};
2,239,506✔
3586

3587
  pRsp->msgType = TDMT_VND_DELETE_RSP;
2,239,506✔
3588
  pRsp->pCont = NULL;
2,239,506✔
3589
  pRsp->contLen = 0;
2,239,506✔
3590
  pRsp->code = TSDB_CODE_SUCCESS;
2,239,506✔
3591

3592
  pRes->uidList = taosArrayInit(0, sizeof(tb_uid_t));
2,239,506✔
3593
  if (pRes->uidList == NULL) {
2,239,506✔
3594
    code = terrno;
×
3595
    goto _err;
×
3596
  }
3597

3598
  tDecoderInit(pCoder, pReq, len);
2,239,506✔
3599
  code = tDecodeDeleteRes(pCoder, pRes);
2,239,506✔
3600
  if (code) goto _err;
2,239,506✔
3601

3602
  if (pRes->affectedRows > 0) {
2,239,506✔
3603
    for (int32_t iUid = 0; iUid < taosArrayGetSize(pRes->uidList); iUid++) {
3,988,895✔
3604
      uint64_t uid = *(uint64_t *)taosArrayGet(pRes->uidList, iUid);
2,275,401✔
3605
      // Merge runtime vnode(db-level) secureDelete to avoid stale client meta after ALTER DATABASE.
3606
      int8_t secureDelete = pRes->secureDelete | pVnode->config.secureDelete;
2,275,401✔
3607
      code = tsdbDeleteTableData(pVnode->pTsdb, ver, pRes->suid, uid, pRes->skey, pRes->ekey, secureDelete);
2,275,401✔
3608
      if (code) goto _err;
2,275,401✔
3609
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, pRes->ctimeMs);
2,275,401✔
3610
      if (code) goto _err;
2,275,401✔
3611
    }
3612
  }
3613

3614
  tDecoderClear(pCoder);
2,239,506✔
3615
  taosArrayDestroy(pRes->uidList);
2,239,506✔
3616

3617
  SVDeleteRsp rsp = {.affectedRows = pRes->affectedRows};
2,239,185✔
3618
  int32_t     ret = 0;
2,239,185✔
3619
  tEncodeSize(tEncodeSVDeleteRsp, &rsp, pRsp->contLen, ret);
2,239,185✔
3620
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
2,239,185✔
3621
  SEncoder ec = {0};
2,239,506✔
3622
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
2,239,506✔
3623
  code = tEncodeSVDeleteRsp(&ec, &rsp);
2,239,506✔
3624
  if (code) goto _err;
2,239,506✔
3625
  tEncoderClear(&ec);
2,239,506✔
3626
  return code;
2,239,506✔
3627

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

3644
  return code;
×
3645
}
3646
static int32_t vnodeProcessCreateIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
18,506✔
3647
  SVCreateStbReq req = {0};
18,506✔
3648
  SDecoder       dc = {0};
18,506✔
3649
  int32_t        code = 0;
18,506✔
3650

3651
  pRsp->msgType = TDMT_VND_CREATE_INDEX_RSP;
18,506✔
3652
  pRsp->code = TSDB_CODE_SUCCESS;
18,506✔
3653
  pRsp->pCont = NULL;
18,506✔
3654
  pRsp->contLen = 0;
18,506✔
3655

3656
  tDecoderInit(&dc, pReq, len);
18,506✔
3657
  // decode req
3658
  if (tDecodeSVCreateStbReq(&dc, &req) < 0) {
18,506✔
3659
    tDecoderClear(&dc);
×
3660
    return terrno = TSDB_CODE_INVALID_MSG;
×
3661
  }
3662

3663
  code = metaAddIndexToSuperTable(pVnode->pMeta, ver, &req);
18,506✔
3664
  if (code) {
18,506✔
3665
    pRsp->code = code;
×
3666
    goto _err;
×
3667
  }
3668
  tDecoderClear(&dc);
18,506✔
3669
  return 0;
18,506✔
3670

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

3683
  if ((code = tDeserializeSDropIdxReq(pReq, len, &req))) {
15,052✔
3684
    pRsp->code = code;
×
3685
    return code;
×
3686
  }
3687

3688
  code = metaDropIndexFromSuperTable(pVnode->pMeta, ver, &req);
15,052✔
3689
  if (code) {
15,052✔
3690
    pRsp->code = code;
×
3691
    return code;
×
3692
  }
3693
  return TSDB_CODE_SUCCESS;
15,052✔
3694
}
3695

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

3698
static int32_t vnodeProcessCompactVnodeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
72,847✔
3699
  if (!pVnode->restored) {
72,847✔
3700
    vInfo("vgId:%d, ignore compact req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
2,975✔
3701
    return 0;
2,975✔
3702
  }
3703
  return vnodeAsyncCompact(pVnode, ver, pReq, len, pRsp);
69,872✔
3704
}
3705

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

3711
  pRsp->msgType = TDMT_SYNC_CONFIG_CHANGE_RSP;
×
3712
  pRsp->code = TSDB_CODE_SUCCESS;
×
3713
  pRsp->pCont = NULL;
×
3714
  pRsp->contLen = 0;
×
3715

3716
  return 0;
×
3717
}
3718

3719
static int32_t vnodeCheckState(SVnode *pVnode) {
46,092✔
3720
  SSyncState syncState = syncGetState(pVnode->sync);
46,092✔
3721
  if (syncState.state != TAOS_SYNC_STATE_LEADER) {
46,092✔
3722
    return terrno = TSDB_CODE_SYN_NOT_LEADER;
12,780✔
3723
  }
3724
  return 0;
33,312✔
3725
}
3726

3727
static int32_t vnodeCheckToken(SVnode *pVnode, char *member0Token, char *member1Token) {
33,312✔
3728
  char token[TSDB_ARB_TOKEN_SIZE] = {0};
33,312✔
3729
  if (vnodeGetArbToken(pVnode, token) != 0) {
33,312✔
3730
    return terrno = TSDB_CODE_NOT_FOUND;
×
3731
  }
3732

3733
  if (strncmp(token, member0Token, TSDB_ARB_TOKEN_SIZE) != 0 &&
33,312✔
3734
      strncmp(token, member1Token, TSDB_ARB_TOKEN_SIZE) != 0) {
31,904✔
3735
    return terrno = TSDB_CODE_MND_ARB_TOKEN_MISMATCH;
31,274✔
3736
  }
3737

3738
  terrno = TSDB_CODE_SUCCESS;
2,038✔
3739
  return 0;
2,038✔
3740
}
3741

3742
static int32_t vnodeCheckSyncd(SVnode *pVnode, char *member0Token, char *member1Token) {
1,019✔
3743
  int32_t code = vnodeCheckToken(pVnode, member0Token, member1Token);
1,019✔
3744
  if (code != 0) {
1,019✔
UNCOV
3745
    return code;
×
3746
  }
3747

3748
  return syncCheckSynced(pVnode->sync);
1,019✔
3749
}
3750

3751
static int32_t vnodeProcessArbCheckSyncReq(SVnode *pVnode, void *pReq, int32_t len, SRpcMsg *pRsp) {
2,073✔
3752
  int32_t code = 0;
2,073✔
3753

3754
  if ((code = vnodeCheckState(pVnode)) != 0) {
2,073✔
3755
    vDebug("vgId:%d, failed to preprocess vnode-arb-check-sync request since %s", TD_VID(pVnode), tstrerror(code));
1,054✔
3756
    return 0;
1,054✔
3757
  }
3758

3759
  SVArbCheckSyncReq syncReq = {0};
1,019✔
3760

3761
  code = tDeserializeSVArbCheckSyncReq(pReq, len, &syncReq);
1,019✔
3762
  if (code) {
1,019✔
3763
    return code;
×
3764
  }
3765

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

3772
  pRsp->msgType = TDMT_VND_ARB_CHECK_SYNC_RSP;
1,019✔
3773
  pRsp->code = TSDB_CODE_SUCCESS;
1,019✔
3774
  pRsp->pCont = NULL;
1,019✔
3775
  pRsp->contLen = 0;
1,019✔
3776

3777
  SVArbCheckSyncRsp syncRsp = {0};
1,019✔
3778
  syncRsp.arbToken = syncReq.arbToken;
1,019✔
3779
  syncRsp.member0Token = syncReq.member0Token;
1,019✔
3780
  syncRsp.member1Token = syncReq.member1Token;
1,019✔
3781
  syncRsp.vgId = TD_VID(pVnode);
1,019✔
3782

3783
  if ((syncRsp.errCode = vnodeCheckSyncd(pVnode, syncReq.member0Token, syncReq.member1Token)) != 0) {
1,019✔
UNCOV
3784
    vError("vgId:%d, failed to check assigned log syncd since %s", TD_VID(pVnode), tstrerror(syncRsp.errCode));
×
3785
  }
3786

3787
  if ((code = vnodeUpdateArbTerm(pVnode, syncReq.arbTerm)) != 0) {
1,019✔
3788
    vError("vgId:%d, failed to update arb term since %s", TD_VID(pVnode), tstrerror(code));
×
3789
    goto _OVER;
×
3790
  }
3791

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

3805
  if (tSerializeSVArbCheckSyncRsp(pHead, contLen, &syncRsp) <= 0) {
1,019✔
3806
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
3807
    rpcFreeCont(pHead);
×
3808
    code = -1;
×
3809
    goto _OVER;
×
3810
  }
3811

3812
  pRsp->pCont = pHead;
1,019✔
3813
  pRsp->contLen = contLen;
1,019✔
3814

3815
  vInfo(
1,019✔
3816
      "vgId:%d, suceed to process vnode-arb-check-sync req rsp.code:%s, arbToken:%s, member0Token:%s, "
3817
      "member1Token:%s",
3818
      TD_VID(pVnode), tstrerror(syncRsp.errCode), syncRsp.arbToken, syncRsp.member0Token, syncRsp.member1Token);
3819

3820
  code = TSDB_CODE_SUCCESS;
1,019✔
3821

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

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