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

taosdata / TDengine / #4984

13 Mar 2026 03:38AM UTC coverage: 68.643% (-0.01%) from 68.653%
#4984

push

travis-ci

web-flow
feat/6641435300-save-audit-in-self (#34738)

434 of 584 new or added lines in 10 files covered. (74.32%)

3048 existing lines in 150 files now uncovered.

212713 of 309883 relevant lines covered (68.64%)

135561814.23 hits per line

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

69.63
/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 "tencode.h"
23
#include "tglobal.h"
24
#include "tmsg.h"
25
#include "tmsgcb.h"
26
#include "tstrbuild.h"
27
#include "tutil.h"
28
#include "vnd.h"
29
#include "vnode.h"
30
#include "vnodeInt.h"
31

32
extern taos_counter_t *tsInsertCounter;
33

34
extern int32_t vnodeProcessScanVnodeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
35
extern int32_t vnodeQueryScanProgress(SVnode *pVnode, SRpcMsg *pMsg);
36

37
static int32_t vnodeProcessCreateStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
38
static int32_t vnodeProcessAlterStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
39
static int32_t vnodeProcessDropStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp, SRpcMsg *pOriginRpc);
40
static int32_t vnodeProcessCreateTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
41
                                       SRpcMsg *pOriginRpc);
42
static int32_t vnodeProcessAlterTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
43
static int32_t vnodeProcessDropTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
44
                                     SRpcMsg *pOriginRpc);
45
static int32_t vnodeProcessSubmitReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
46
                                     SRpcMsg *pOriginalMsg);
47
static int32_t vnodeProcessAuditRecordReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pOriginalMsg);
48
static int32_t vnodeProcessAlterConfirmReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
49
static int32_t vnodeProcessAlterConfigReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
50
static int32_t vnodeProcessDropTtlTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
51
static int32_t vnodeProcessTrimReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
52
static int32_t vnodeProcessDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
53
                                     SRpcMsg *pOriginalMsg);
54
static int32_t vnodeProcessBatchDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
55
static int32_t vnodeProcessCreateIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
56
static int32_t vnodeProcessDropIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
57
static int32_t vnodeProcessCompactVnodeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
58
static int32_t vnodeProcessConfigChangeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
59
static int32_t vnodeProcessArbCheckSyncReq(SVnode *pVnode, void *pReq, int32_t len, SRpcMsg *pRsp);
60
static int32_t vnodeProcessDropTSmaCtbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
61
                                          SRpcMsg *pOriginRpc);
62
static int32_t vnodeProcessCreateRsmaReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
63
static int32_t vnodeProcessDropRsmaReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
64
static int32_t vnodeProcessAlterRsmaReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
65

66
static int32_t vnodeCheckState(SVnode *pVnode);
67
static int32_t vnodeCheckToken(SVnode *pVnode, char *member0Token, char *member1Token);
68
static int32_t vnodeCheckSyncd(SVnode *pVnode, char *member0Token, char *member1Token);
69
static int32_t vnodeProcessFetchTtlExpiredTbs(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
70

71
extern int32_t vnodeProcessKillCompactReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
72
extern int32_t vnodeQueryCompactProgress(SVnode *pVnode, SRpcMsg *pMsg);
73

74
extern int32_t vnodeProcessKillRetentionReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
75
extern int32_t vnodeQueryRetentionProgress(SVnode *pVnode, SRpcMsg *pMsg);
76

77
extern int32_t vnodeProcessKillScanReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
78

79
extern int32_t vnodeListSsMigrateFileSets(SVnode *pVnode, SRpcMsg *pMsg);
80
static int32_t vnodeProcessSsMigrateFileSetReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
81
extern int32_t vnodeQuerySsMigrateProgress(SVnode *pVnode, SRpcMsg *pMsg);
82
static int32_t vnodeProcessFollowerSsMigrateReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
83
static int32_t vnodeProcessKillSsMigrateReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
84

85
static int32_t vnodePreprocessCreateTableReq(SVnode *pVnode, SDecoder *pCoder, int64_t btime, int64_t *pUid) {
77,950,887✔
86
  int32_t code = 0;
77,950,887✔
87
  int32_t lino = 0;
77,950,887✔
88

89
  if (tStartDecode(pCoder) < 0) {
77,950,887✔
UNCOV
90
    code = TSDB_CODE_INVALID_MSG;
×
91
    TSDB_CHECK_CODE(code, lino, _exit);
×
92
  }
93

94
  // flags
95
  if (tDecodeI32v(pCoder, NULL) < 0) {
77,951,647✔
UNCOV
96
    code = TSDB_CODE_INVALID_MSG;
×
97
    TSDB_CHECK_CODE(code, lino, _exit);
×
98
  }
99

100
  // name
101
  char *name = NULL;
77,951,647✔
102
  if (tDecodeCStr(pCoder, &name) < 0) {
77,952,907✔
UNCOV
103
    code = TSDB_CODE_INVALID_MSG;
×
104
    TSDB_CHECK_CODE(code, lino, _exit);
×
105
  }
106

107
  // uid
108
  int64_t uid = metaGetTableEntryUidByName(pVnode->pMeta, name);
77,952,907✔
109
  if (uid == 0) {
77,942,396✔
110
    uid = tGenIdPI64();
77,577,623✔
111
  }
112
  taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), uid);
77,938,619✔
113

114
  // btime
115
  taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos + 8), btime);
77,940,115✔
116

117
  tEndDecode(pCoder);
77,942,949✔
118

119
_exit:
77,942,730✔
120
  if (code) {
77,943,768✔
UNCOV
121
    vError("vgId:%d %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
122
  } else {
123
    vTrace("vgId:%d %s done, table:%s uid generated:%" PRId64, TD_VID(pVnode), __func__, name, uid);
77,943,768✔
124
    if (pUid) *pUid = uid;
77,938,054✔
125
  }
126
  return code;
77,938,376✔
127
}
128
static int32_t vnodePreProcessCreateTableMsg(SVnode *pVnode, SRpcMsg *pMsg) {
57,581,409✔
129
  int32_t code = 0;
57,581,409✔
130
  int32_t lino = 0;
57,581,409✔
131

132
  int64_t  btime = taosGetTimestampMs();
57,581,409✔
133
  SDecoder dc = {0};
57,581,409✔
134
  int32_t  nReqs;
57,579,895✔
135

136
  tDecoderInit(&dc, (uint8_t *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead));
57,581,088✔
137
  if (tStartDecode(&dc) < 0) {
57,581,409✔
UNCOV
138
    code = TSDB_CODE_INVALID_MSG;
×
139
    return code;
×
140
  }
141

142
  if (tDecodeI32v(&dc, &nReqs) < 0) {
57,581,261✔
UNCOV
143
    code = TSDB_CODE_INVALID_MSG;
×
144
    TSDB_CHECK_CODE(code, lino, _exit);
×
145
  }
146
  for (int32_t iReq = 0; iReq < nReqs; iReq++) {
120,444,581✔
147
    code = vnodePreprocessCreateTableReq(pVnode, &dc, btime, NULL);
62,875,371✔
148
    TSDB_CHECK_CODE(code, lino, _exit);
62,863,320✔
149
  }
150

151
  tEndDecode(&dc);
57,569,210✔
152

153
_exit:
57,579,636✔
154
  tDecoderClear(&dc);
57,579,636✔
155
  if (code) {
57,576,566✔
UNCOV
156
    vError("vgId:%d, %s:%d failed to preprocess submit request since %s, msg type:%s", TD_VID(pVnode), __func__, lino,
×
157
           tstrerror(code), TMSG_INFO(pMsg->msgType));
158
  }
159
  return code;
57,574,884✔
160
}
161

162
static int32_t vnodePreProcessAlterTableMsg(SVnode *pVnode, SRpcMsg *pMsg) {
12,102,300✔
163
  int32_t code = TSDB_CODE_INVALID_MSG;
12,102,300✔
164
  int32_t lino = 0;
12,102,300✔
165

166
  if (pVnode->config.isAudit) {
12,102,300✔
UNCOV
167
    return TSDB_CODE_PAR_PERMISSION_DENIED;
×
168
  }
169

170
  SDecoder dc = {0};
12,102,300✔
171
  tDecoderInit(&dc, (uint8_t *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead));
12,102,300✔
172

173
  SVAlterTbReq vAlterTbReq = {0};
12,102,300✔
174
  int64_t      ctimeMs = taosGetTimestampMs();
12,102,300✔
175
  if (tDecodeSVAlterTbReqSetCtime(&dc, &vAlterTbReq, ctimeMs) < 0) {
12,102,300✔
UNCOV
176
    taosArrayDestroy(vAlterTbReq.pMultiTag);
×
177
    vAlterTbReq.pMultiTag = NULL;
×
178
    goto _exit;
×
179
  }
180
  taosArrayDestroy(vAlterTbReq.pMultiTag);
12,102,300✔
181
  vAlterTbReq.pMultiTag = NULL;
12,102,300✔
182

183
  code = 0;
12,102,300✔
184

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

196
static int32_t vnodePreProcessDropTtlMsg(SVnode *pVnode, SRpcMsg *pMsg) {
10,996,320✔
197
  int32_t code = TSDB_CODE_INVALID_MSG;
10,996,320✔
198
  int32_t lino = 0;
10,996,320✔
199

200
  SMsgHead *pContOld = pMsg->pCont;
10,996,320✔
201
  int32_t   reqLenOld = pMsg->contLen - sizeof(SMsgHead);
10,997,574✔
202

203
  SArray *tbUids = NULL;
10,998,759✔
204
  int64_t timestampMs = 0;
10,998,759✔
205

206
  SVDropTtlTableReq ttlReq = {0};
10,998,759✔
207
  if (tDeserializeSVDropTtlTableReq((char *)pContOld + sizeof(SMsgHead), reqLenOld, &ttlReq) != 0) {
10,998,543✔
UNCOV
208
    code = TSDB_CODE_INVALID_MSG;
×
209
    TSDB_CHECK_CODE(code, lino, _exit);
×
210
  }
211

212
  {  // find expired uids
213
    tbUids = taosArrayInit(8, sizeof(tb_uid_t));
10,999,074✔
214
    if (tbUids == NULL) {
11,000,738✔
UNCOV
215
      code = terrno;
×
216
      TSDB_CHECK_CODE(code, lino, _exit);
×
217
    }
218

219
    timestampMs = (int64_t)ttlReq.timestampSec * 1000;
11,000,738✔
220
    code = metaTtlFindExpired(pVnode->pMeta, timestampMs, tbUids, ttlReq.ttlDropMaxCount);
11,000,738✔
221
    if (code != 0) {
10,992,841✔
UNCOV
222
      code = TSDB_CODE_INVALID_MSG;
×
223
      TSDB_CHECK_CODE(code, lino, _exit);
×
224
    }
225

226
    ttlReq.nUids = taosArrayGetSize(tbUids);
10,992,841✔
227
    ttlReq.pTbUids = tbUids;
10,990,545✔
228
  }
229

230
  if (ttlReq.nUids == 0) {
10,990,545✔
231
    code = TSDB_CODE_MSG_PREPROCESSED;
10,975,045✔
232
    TSDB_CHECK_CODE(code, lino, _exit);
10,975,045✔
233
  }
234

235
  {  // prepare new content
236
    int32_t reqLenNew = tSerializeSVDropTtlTableReq(NULL, 0, &ttlReq);
7,088✔
237
    int32_t contLenNew = reqLenNew + sizeof(SMsgHead);
11,670✔
238

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

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

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

256
  code = 0;
11,670✔
257

258
_exit:
10,995,229✔
259
  taosArrayDestroy(tbUids);
10,995,091✔
260

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

268
  return code;
10,972,394✔
269
}
270

271
extern int64_t tsMaxKeyByPrecision[];
272
static int32_t vnodePreProcessSubmitTbData(SVnode *pVnode, SDecoder *pCoder, int64_t btimeMs, int64_t ctimeMs) {
615,173,725✔
273
  int32_t code = 0;
615,173,725✔
274
  int32_t lino = 0;
615,173,725✔
275

276
  if (tStartDecode(pCoder) < 0) {
615,173,725✔
UNCOV
277
    code = TSDB_CODE_INVALID_MSG;
×
278
    TSDB_CHECK_CODE(code, lino, _exit);
×
279
  }
280

281
  SSubmitTbData submitTbData;
615,172,549✔
282
  uint8_t       version;
283
  if (tDecodeI32v(pCoder, &submitTbData.flags) < 0) {
615,172,120✔
UNCOV
284
    code = TSDB_CODE_INVALID_MSG;
×
285
    TSDB_CHECK_CODE(code, lino, _exit);
×
286
  }
287
  version = (submitTbData.flags >> 8) & 0xff;
615,172,120✔
288
  submitTbData.flags = submitTbData.flags & 0xff;
615,172,120✔
289

290
  int64_t uid;
615,169,317✔
291
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
615,169,936✔
292
    code = vnodePreprocessCreateTableReq(pVnode, pCoder, btimeMs, &uid);
15,073,393✔
293
    TSDB_CHECK_CODE(code, lino, _exit);
15,070,121✔
294
  }
295

296
  // submit data
297
  if (tDecodeI64(pCoder, &submitTbData.suid) < 0) {
615,171,808✔
UNCOV
298
    code = TSDB_CODE_INVALID_MSG;
×
299
    TSDB_CHECK_CODE(code, lino, _exit);
×
300
  }
301

302
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
615,171,808✔
303
    taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), uid);
15,073,057✔
304
    pCoder->pos += sizeof(int64_t);
15,070,450✔
305
  } else {
306
    if (tDecodeI64(pCoder, &submitTbData.uid) < 0) {
600,098,131✔
UNCOV
307
      code = TSDB_CODE_INVALID_MSG;
×
308
      TSDB_CHECK_CODE(code, lino, _exit);
×
309
    }
310
  }
311

312
  if (tDecodeI32v(pCoder, &submitTbData.sver) < 0) {
615,168,964✔
UNCOV
313
    code = TSDB_CODE_INVALID_MSG;
×
314
    TSDB_CHECK_CODE(code, lino, _exit);
×
315
  }
316

317
  // scan and check
318
  TSKEY now = btimeMs;
615,168,964✔
319
  if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_MICRO) {
615,168,964✔
320
    now *= 1000;
2,504,992✔
321
  } else if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_NANO) {
612,667,639✔
322
    now *= 1000000;
1,460,727✔
323
  }
324

325
  int32_t keep = pVnode->config.tsdbCfg.keep2;
615,173,089✔
326

327
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * keep;
615,168,877✔
328
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
615,171,305✔
329
  if (submitTbData.flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
615,165,451✔
330
    uint64_t nColData;
6,595,145✔
331
    if (tDecodeU64v(pCoder, &nColData) < 0) {
6,595,554✔
UNCOV
332
      code = TSDB_CODE_INVALID_MSG;
×
333
      TSDB_CHECK_CODE(code, lino, _exit);
×
334
    }
335

336
    SColData colData = {0};
6,595,554✔
337
    code = tDecodeColData(version, pCoder, &colData, false);
6,595,561✔
338
    if (code) {
6,596,205✔
UNCOV
339
      code = TSDB_CODE_INVALID_MSG;
×
340
      TSDB_CHECK_CODE(code, lino, _exit);
×
341
    }
342

343
    if (colData.flag != HAS_VALUE) {
6,596,205✔
UNCOV
344
      code = TSDB_CODE_INVALID_MSG;
×
345
      TSDB_CHECK_CODE(code, lino, _exit);
×
346
    }
347

348
    for (uint32_t iRow = 0; iRow < colData.nVal; iRow++) {
2,147,483,647✔
349
      if (((TSKEY *)colData.pData)[iRow] < minKey || ((TSKEY *)colData.pData)[iRow] > maxKey) {
2,147,483,647✔
350
        code = TSDB_CODE_TDB_TIMESTAMP_OUT_OF_RANGE;
587✔
351
        TSDB_CHECK_CODE(code, lino, _exit);
587✔
352
      }
353
    }
354

355
    for (uint64_t i = 1; i < nColData; i++) {
20,122,024✔
356
      code = tDecodeColData(version, pCoder, &colData, true);
13,679,167✔
357
      if (code) {
13,680,133✔
UNCOV
358
        code = TSDB_CODE_INVALID_MSG;
×
359
        TSDB_CHECK_CODE(code, lino, _exit);
×
360
      }
361
    }
362
  } else {
363
    uint64_t nRow;
608,567,503✔
364
    if (tDecodeU64v(pCoder, &nRow) < 0) {
608,573,343✔
UNCOV
365
      code = TSDB_CODE_INVALID_MSG;
×
366
      TSDB_CHECK_CODE(code, lino, _exit);
×
367
    }
368

369
    for (uint32_t iRow = 0; iRow < nRow; ++iRow) {
2,147,483,647✔
370
      SRow *pRow = (SRow *)(pCoder->data + pCoder->pos);
2,147,483,647✔
371
      pCoder->pos += pRow->len;
2,147,483,647✔
372
#ifndef NO_UNALIGNED_ACCESS
373
      if (pRow->ts < minKey || pRow->ts > maxKey) {
2,147,483,647✔
374
#else
375
      TSKEY ts = taosGetInt64Aligned(&pRow->ts);
376
      if (ts < minKey || ts > maxKey) {
377
#endif
378
        code = TSDB_CODE_TDB_TIMESTAMP_OUT_OF_RANGE;
67,090✔
379
        TSDB_CHECK_CODE(code, lino, _exit);
67,090✔
380
      }
381

382
      // Check pRow->sver
383
      if (pRow->sver != submitTbData.sver) {
2,147,483,647✔
UNCOV
384
        code = TSDB_CODE_INVALID_DATA_FMT;
×
385
        TSDB_CHECK_CODE(code, lino, _exit);
×
386
      }
387
    }
388
  }
389

390
  if (!tDecodeIsEnd(pCoder)) {
615,100,111✔
391
    taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), ctimeMs);
615,103,525✔
392
    pCoder->pos += sizeof(int64_t);
615,096,831✔
393
  }
394

395
  tEndDecode(pCoder);
615,100,303✔
396

397
_exit:
615,172,293✔
398
  if (code) {
615,173,965✔
399
    vError("vgId:%d, %s:%d failed to vnodePreProcessSubmitTbData submit request since %s", TD_VID(pVnode), __func__,
67,677✔
400
           lino, tstrerror(code));
401
  }
402
  return code;
615,173,965✔
403
}
404
static int32_t vnodePreProcessSubmitMsg(SVnode *pVnode, SRpcMsg *pMsg) {
586,517,427✔
405
  int32_t code = 0;
586,517,427✔
406
  int32_t lino = 0;
586,517,427✔
407

408
  if (tsBypassFlag & TSDB_BYPASS_RA_RPC_RECV_SUBMIT) {
586,517,427✔
409
    return TSDB_CODE_MSG_PREPROCESSED;
67✔
410
  }
411

412
  SDecoder *pCoder = &(SDecoder){0};
586,517,360✔
413

414
  if (taosHton64(((SSubmitReq2Msg *)pMsg->pCont)->version) != 1) {
586,516,903✔
UNCOV
415
    code = TSDB_CODE_INVALID_MSG;
×
416
    TSDB_CHECK_CODE(code, lino, _exit);
×
417
  }
418

419
  tDecoderInit(pCoder, (uint8_t *)pMsg->pCont + sizeof(SSubmitReq2Msg), pMsg->contLen - sizeof(SSubmitReq2Msg));
586,518,263✔
420

421
  if (tStartDecode(pCoder) < 0) {
586,517,376✔
UNCOV
422
    code = TSDB_CODE_INVALID_MSG;
×
423
    TSDB_CHECK_CODE(code, lino, _exit);
×
424
  }
425

426
  uint64_t nSubmitTbData;
586,513,639✔
427
  if (tDecodeU64v(pCoder, &nSubmitTbData) < 0) {
586,516,835✔
UNCOV
428
    code = TSDB_CODE_INVALID_MSG;
×
429
    TSDB_CHECK_CODE(code, lino, _exit);
×
430
  }
431

432
  int64_t btimeMs = taosGetTimestampMs();
586,516,975✔
433
  int64_t ctimeMs = btimeMs;
586,516,975✔
434
  for (uint64_t i = 0; i < nSubmitTbData; i++) {
1,201,618,435✔
435
    code = vnodePreProcessSubmitTbData(pVnode, pCoder, btimeMs, ctimeMs);
615,173,712✔
436
    TSDB_CHECK_CODE(code, lino, _exit);
615,169,137✔
437
  }
438

439
  tEndDecode(pCoder);
586,444,723✔
440

441
_exit:
586,518,115✔
442
  tDecoderClear(pCoder);
586,517,602✔
443
  if (code) {
586,516,332✔
444
    vError("vgId:%d, %s:%d failed to preprocess submit request since %s, msg type:%s", TD_VID(pVnode), __func__, lino,
67,677✔
445
           tstrerror(code), TMSG_INFO(pMsg->msgType));
446
  }
447
  return code;
586,509,972✔
448
}
449

450
static int32_t vnodePreProcessDeleteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
1,720,373✔
451
  int32_t code = 0;
1,720,373✔
452

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

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

465
  code = qWorkerProcessDeleteMsg(&handle, pVnode->pQuery, pMsg, &res);
1,720,373✔
466
  if (code) goto _exit;
1,720,373✔
467

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

473
  ((SMsgHead *)pCont)->contLen = size + sizeof(SMsgHead);
1,720,373✔
474
  ((SMsgHead *)pCont)->vgId = TD_VID(pVnode);
1,720,373✔
475

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

482
  rpcFreeCont(pMsg->pCont);
1,720,373✔
483
  pMsg->pCont = pCont;
1,720,373✔
484
  pMsg->contLen = size + sizeof(SMsgHead);
1,720,373✔
485

486
  taosArrayDestroy(res.uidList);
1,720,373✔
487

488
_exit:
1,720,373✔
489
  return code;
1,720,373✔
490
}
491

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

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

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

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

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

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

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

524
  SVArbCheckSyncReq syncReq = {0};
33,364✔
525

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

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

536
  int32_t code = terrno;
33,364✔
537
  tFreeSVArbCheckSyncReq(&syncReq);
33,364✔
538

539
  return code;
33,364✔
540
}
541

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

551
  if (pVnode->config.isAudit) {
1,450,789✔
UNCOV
552
    return TSDB_CODE_PAR_PERMISSION_DENIED;
×
553
  }
554

555
  tDecoderInit(&dc, POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), pMsg->contLen - sizeof(SMsgHead));
1,450,789✔
556

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

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

584
  tEncodeSize(tEncodeSVDropTbBatchReq, &sentBatchReqs, size, code);
1,450,789✔
585
  tEncoderInit(&ec, POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), size);
1,450,789✔
586
  code = tEncodeSVDropTbBatchReq(&ec, &sentBatchReqs);
1,450,789✔
587
  tEncoderClear(&ec);
1,450,789✔
588
  if (code != TSDB_CODE_SUCCESS) {
1,450,789✔
UNCOV
589
    vError("vgId:%d %s failed to encode drop tb batch req: %s", TD_VID(pVnode), __func__, tstrerror(code));
×
590
    TSDB_CHECK_CODE(code, lino, _exit);
×
591
  }
592

593
_exit:
1,450,789✔
594
  tDecoderClear(&dc);
1,450,789✔
595
  if (sentBatchReqs.pArray) {
1,450,789✔
596
    taosArrayDestroy(sentBatchReqs.pArray);
1,450,789✔
597
  }
598
  return code;
1,450,789✔
599
}
600

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

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

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

616
_exit:
3,620✔
617
  return code;
3,620✔
618
}
619

620
int32_t vnodePreProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
690,993,644✔
621
  int32_t code = 0;
690,993,644✔
622

623
  if (pVnode->mounted) {
690,993,644✔
624
    vError("vgId:%d, mountVgId:%d, skip write msg:%s", TD_VID(pVnode), pVnode->config.mountVgId,
1,290✔
625
           TMSG_INFO(pMsg->msgType));
626
    return TSDB_CODE_OPS_NOT_SUPPORT;
1,290✔
627
  }
628

629
  switch (pMsg->msgType) {
690,993,273✔
630
    case TDMT_VND_CREATE_TABLE: {
57,581,409✔
631
      code = vnodePreProcessCreateTableMsg(pVnode, pMsg);
57,581,409✔
632
    } break;
57,575,217✔
633
    case TDMT_VND_ALTER_TABLE: {
12,102,300✔
634
      code = vnodePreProcessAlterTableMsg(pVnode, pMsg);
12,102,300✔
635
    } break;
12,102,300✔
636
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
11,002,097✔
637
    case TDMT_VND_DROP_TTL_TABLE: {
638
      code = vnodePreProcessDropTtlMsg(pVnode, pMsg);
11,002,097✔
639
    } break;
10,981,093✔
640
    case TDMT_VND_SUBMIT: {
586,516,207✔
641
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.preprocess_time, METRIC_LEVEL_LOW,
586,516,207✔
642
                           { code = vnodePreProcessSubmitMsg(pVnode, pMsg); });
643
    } break;
586,512,999✔
644
    case TDMT_VND_DELETE: {
1,720,373✔
645
      code = vnodePreProcessDeleteMsg(pVnode, pMsg);
1,720,373✔
646
    } break;
1,720,373✔
UNCOV
647
    case TDMT_VND_BATCH_DEL: {
×
648
      code = vnodePreProcessBatchDeleteMsg(pVnode, pMsg);
×
649
    } break;
×
650
    case TDMT_VND_ARB_CHECK_SYNC: {
49,710✔
651
      code = vnodePreProcessArbCheckSyncMsg(pVnode, pMsg);
49,710✔
652
    } break;
49,710✔
653
    case TDMT_VND_DROP_TABLE: {
1,450,789✔
654
      code = vnodePreProcessDropTbMsg(pVnode, pMsg);
1,450,789✔
655
    } break;
1,450,789✔
656
#ifdef TD_ENTERPRISE
657
    case TDMT_VND_SSMIGRATE_FILESET: {
3,620✔
658
      code = vnodePreProcessSsMigrateFileSetReq(pVnode, pMsg);
3,620✔
659
    } break;
3,620✔
660
#endif
661
    case TDMT_VND_AUDIT_RECORD: {
2,844✔
662
      vTrace("vgId:%d, pre process TDMT_VND_AUDIT_RECORD", TD_VID(pVnode));
2,844✔
663
    } break;
268✔
664
    default:
20,566,712✔
665
      break;
20,566,712✔
666
  }
667

668
  if (code && code != TSDB_CODE_MSG_PREPROCESSED) {
690,963,081✔
669
    vError("vgId:%d, failed to preprocess write request since %s, msg type:%s", TD_VID(pVnode), tstrerror(code),
99,507✔
670
           TMSG_INFO(pMsg->msgType));
671
  }
672
  return code;
690,967,888✔
673
}
674

675
static int32_t inline vnodeSubmitSubRowBlobData(SVnode *pVnode, SSubmitTbData *pSubmitTbData) {
31,060✔
676
  int32_t code = 0;
31,060✔
677
  int32_t lino = 0;
31,060✔
678

679
  int64_t   st = taosGetTimestampUs();
31,060✔
680
  SBlobSet *pBlobSet = pSubmitTbData->pBlobSet;
31,060✔
681
  int32_t   sz = taosArrayGetSize(pBlobSet->pSeqTable);
31,060✔
682

683
  SBseBatch *pBatch = NULL;
31,060✔
684

685
  code = bseBatchInit(pVnode->pBse, &pBatch, sz);
31,060✔
686
  TSDB_CHECK_CODE(code, lino, _exit);
31,060✔
687

688
  SRow  **pRow = (SRow **)TARRAY_DATA(pSubmitTbData->aRowP);
31,060✔
689
  int32_t rowIdx = -1;
31,060✔
690
  for (int32_t i = 0; i < sz; i++) {
24,769,167✔
691
    int64_t     seq = 0;
24,738,107✔
692
    SBlobValue *p = taosArrayGet(pBlobSet->pSeqTable, i);
24,738,107✔
693
    if (p->type == TSDB_DATA_BLOB_EMPTY_VALUE || p->type == TSDB_DATA_BLOB_NULL_VALUE) {
24,738,107✔
694
      // skip empty or null blob
695
      continue;
2,456,790✔
696
    }
697

698
    code = bseBatchPut(pBatch, &seq, pBlobSet->data + p->offset, p->len);
22,281,317✔
699
    TSDB_CHECK_CODE(code, lino, _exit);
22,281,317✔
700

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

708
    if (tPutU64(row->data + p->dataOffset, seq) < 0) {
44,562,634✔
709
      code = TSDB_CODE_INVALID_MSG;
×
UNCOV
710
      TSDB_CHECK_CODE(code, lino, _exit);
×
711
    }
712
  }
713

714
  code = bseCommitBatch(pVnode->pBse, pBatch);
31,060✔
715
  TSDB_CHECK_CODE(code, lino, _exit);
31,060✔
716

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

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

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

739
  SBseBatch *pBatch = NULL;
67✔
740

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

744
  SColData *p = (SColData *)TARRAY_DATA(pSubmitTbData->aCol);
67✔
745
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitTbData->aCol); i++) {
899✔
746
    if (IS_STR_DATA_BLOB(p[i].type)) {
899✔
747
      pBlobCol = &p[i];
67✔
748
      break;
67✔
749
    }
750
  }
751
  if (pBlobCol == NULL) {
67✔
UNCOV
752
    vError("vgId:%d %s failed to find blob column in submit data", TD_VID(pVnode), __func__);
×
UNCOV
753
    code = TSDB_CODE_INVALID_MSG;
×
UNCOV
754
    goto _exit;
×
755
  }
756

757
  int32_t offset = 0;
67✔
758
  // int32_t   rowIdx = -1;
759
  for (int32_t i = 0; i < sz; i++) {
142✔
760
    int64_t     seq = 0;
75✔
761
    SBlobValue *p = taosArrayGet(pBlobSet->pSeqTable, i);
75✔
762
    if (p->type == TSDB_DATA_BLOB_EMPTY_VALUE || p->type == TSDB_DATA_BLOB_NULL_VALUE) {
75✔
763
      // skip empty or null blob
764
      continue;
42✔
765
    }
766
    code = bseBatchPut(pBatch, &seq, pBlobSet->data + p->offset, p->len);
33✔
767
    TSDB_CHECK_CODE(code, lino, _exit);
33✔
768

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

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

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

797
  return code;
31,127✔
798
}
799

800
int32_t vnodeProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg, int64_t ver, SRpcMsg *pRsp) {
820,850,524✔
801
  int32_t code = 0;
820,850,524✔
802
  int32_t lino = 0;
820,850,524✔
803
  void   *ptr = NULL;
820,850,524✔
804
  void   *pReq;
805
  int32_t len;
806

807
  (void)taosThreadMutexLock(&pVnode->mutex);
820,850,524✔
808
  if (pVnode->disableWrite) {
820,915,573✔
UNCOV
809
    (void)taosThreadMutexUnlock(&pVnode->mutex);
×
UNCOV
810
    vError("vgId:%d write is disabled for snapshot, version:%" PRId64, TD_VID(pVnode), ver);
×
UNCOV
811
    return TSDB_CODE_VND_WRITE_DISABLED;
×
812
  }
813
  (void)taosThreadMutexUnlock(&pVnode->mutex);
820,875,115✔
814

815
  if (ver <= pVnode->state.applied) {
820,884,503✔
UNCOV
816
    vError("vgId:%d, duplicate write request. ver: %" PRId64 ", applied: %" PRId64, TD_VID(pVnode), ver,
×
817
           pVnode->state.applied);
UNCOV
818
    return terrno = TSDB_CODE_VND_DUP_REQUEST;
×
819
  }
820

821
  vGDebug(&pMsg->info.traceId,
820,892,597✔
822
          "vgId:%d, index:%" PRId64 ", process write request:%s, applied:%" PRId64 ", state.applyTerm:%" PRId64
823
          ", conn.applyTerm:%" PRId64 ", contLen:%d",
824
          TD_VID(pVnode), ver, TMSG_INFO(pMsg->msgType), pVnode->state.applied, pVnode->state.applyTerm,
825
          pMsg->info.conn.applyTerm, pMsg->contLen);
826

827
  if (!(pVnode->state.applyTerm <= pMsg->info.conn.applyTerm)) {
820,940,469✔
UNCOV
828
    vError("vgId:%d, applyTerm mismatch, expected: %" PRId64 ", received: %" PRId64, TD_VID(pVnode),
×
829
           pVnode->state.applyTerm, pMsg->info.conn.applyTerm);
UNCOV
830
    return terrno = TSDB_CODE_INTERNAL_ERROR;
×
831
  }
832

833
  if (!(pVnode->state.applied + 1 == ver)) {
820,935,124✔
834
    vError("vgId:%d, mountVgId:%d, ver mismatch, expected: %" PRId64 ", received: %" PRId64, TD_VID(pVnode),
×
835
           pVnode->config.mountVgId, pVnode->state.applied + 1, ver);
UNCOV
836
    return terrno = TSDB_CODE_INTERNAL_ERROR;
×
837
  }
838

839
  atomic_store_64(&pVnode->state.applied, ver);
820,936,305✔
840
  atomic_store_64(&pVnode->state.applyTerm, pMsg->info.conn.applyTerm);
820,940,056✔
841

842
  if (!syncUtilUserCommit(pMsg->msgType)) goto _exit;
820,939,967✔
843

844
  // skip header
845
  pReq = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
810,743,406✔
846
  len = pMsg->contLen - sizeof(SMsgHead);
810,743,174✔
847
  bool needCommit = false;
810,743,494✔
848
  bool forceTrimWal = false;
810,743,494✔
849

850
  switch (pMsg->msgType) {
810,743,494✔
851
    /* META */
852
    case TDMT_VND_CREATE_STB:
5,188,338✔
853
      code = vnodeProcessCreateStbReq(pVnode, ver, pReq, len, pRsp);
5,188,338✔
854
      TSDB_CHECK_CODE(code, lino, _err);
5,188,923✔
855
      break;
5,186,691✔
856
    case TDMT_VND_ALTER_STB:
7,479,701✔
857
      code = vnodeProcessAlterStbReq(pVnode, ver, pReq, len, pRsp);
7,479,701✔
858
      TSDB_CHECK_CODE(code, lino, _err);
7,479,701✔
859
      break;
7,479,701✔
860
    case TDMT_VND_DROP_STB:
909,610✔
861
      code = vnodeProcessDropStbReq(pVnode, ver, pReq, len, pRsp, pMsg);
909,610✔
862
      TSDB_CHECK_CODE(code, lino, _err);
909,610✔
863
      break;
909,610✔
864
    case TDMT_VND_CREATE_TABLE:
60,528,321✔
865
      code = vnodeProcessCreateTbReq(pVnode, ver, pReq, len, pRsp, pMsg);
60,528,321✔
866
      TSDB_CHECK_CODE(code, lino, _err);
60,514,849✔
867
      break;
60,514,849✔
868
    case TDMT_VND_ALTER_TABLE:
12,103,226✔
869
      code = vnodeProcessAlterTbReq(pVnode, ver, pReq, len, pRsp);
12,103,226✔
870
      TSDB_CHECK_CODE(code, lino, _err);
12,103,226✔
871
      break;
12,103,226✔
872
    case TDMT_VND_DROP_TABLE:
1,451,113✔
873
    case TDMT_VND_SNODE_DROP_TABLE:
874
      code = vnodeProcessDropTbReq(pVnode, ver, pReq, len, pRsp, pMsg);
1,451,113✔
875
      TSDB_CHECK_CODE(code, lino, _err);
1,451,113✔
876
      break;
1,451,113✔
877
    case TDMT_VND_CREATE_RSMA:
38,452✔
878
      code = vnodeProcessCreateRsmaReq(pVnode, ver, pReq, len, pRsp);
38,452✔
879
      TSDB_CHECK_CODE(code, lino, _err);
38,452✔
880
      break;
38,452✔
881
    case TDMT_VND_DROP_RSMA:
19,360✔
882
      code = vnodeProcessDropRsmaReq(pVnode, ver, pReq, len, pRsp);
19,360✔
883
      TSDB_CHECK_CODE(code, lino, _err);
19,360✔
884
      break;
19,360✔
885
    case TDMT_VND_ALTER_RSMA:
11,852✔
886
      code = vnodeProcessAlterRsmaReq(pVnode, ver, pReq, len, pRsp);
11,852✔
887
      TSDB_CHECK_CODE(code, lino, _err);
11,852✔
888
      break;
11,852✔
UNCOV
889
    case TDMT_VND_DROP_TTL_TABLE:
×
UNCOV
890
      code = vnodeProcessDropTtlTbReq(pVnode, ver, pReq, len, pRsp);
×
UNCOV
891
      TSDB_CHECK_CODE(code, lino, _err);
×
UNCOV
892
      break;
×
893
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
11,670✔
894
      code = vnodeProcessFetchTtlExpiredTbs(pVnode, ver, pReq, len, pRsp);
11,670✔
895
      TSDB_CHECK_CODE(code, lino, _err);
11,670✔
896
      break;
11,670✔
897
    case TDMT_VND_TRIM:
64,038✔
898
      code = vnodeProcessTrimReq(pVnode, ver, pReq, len, pRsp);
64,038✔
899
      TSDB_CHECK_CODE(code, lino, _err);
64,038✔
900
      break;
64,038✔
901
      case TDMT_VND_TRIM_WAL:
6,696✔
902
        needCommit = true;
6,696✔
903
        forceTrimWal = true;
6,696✔
904
        break;
6,696✔
905
#ifdef TD_ENTERPRISE
906
    case TDMT_VND_SSMIGRATE_FILESET:
3,620✔
907
      if (vnodeProcessSsMigrateFileSetReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
3,620✔
908
      break;
3,620✔
909
    case TDMT_VND_FOLLOWER_SSMIGRATE:
6,516✔
910
      if (vnodeProcessFollowerSsMigrateReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
6,516✔
911
      break;
6,516✔
UNCOV
912
    case TDMT_VND_KILL_SSMIGRATE:
×
UNCOV
913
      if (vnodeProcessKillSsMigrateReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
UNCOV
914
      break;
×
915
#endif
916

917
    /* TSDB */
918
    case TDMT_VND_SUBMIT: {
705,013,564✔
919
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.apply_time, METRIC_LEVEL_LOW, {
705,013,564✔
920
        if (vnodeProcessSubmitReq(pVnode, ver, pMsg->pCont, pMsg->contLen, pRsp, pMsg) < 0) goto _err;
921
      });
922
      METRICS_UPDATE(pVnode->writeMetrics.apply_bytes, METRIC_LEVEL_LOW, (int64_t)pMsg->contLen);
704,970,736✔
923
      break;
704,966,983✔
924
    }
925
    case TDMT_VND_DELETE:
2,095,243✔
926
      code = vnodeProcessDeleteReq(pVnode, ver, pReq, len, pRsp, pMsg);
2,095,243✔
927
      TSDB_CHECK_CODE(code, lino, _err);
2,094,696✔
928
      break;
2,094,696✔
UNCOV
929
    case TDMT_VND_BATCH_DEL:
×
UNCOV
930
      code = vnodeProcessBatchDeleteReq(pVnode, ver, pReq, len, pRsp);
×
UNCOV
931
      TSDB_CHECK_CODE(code, lino, _err);
×
UNCOV
932
      break;
×
933
      /* TQ */
934
#if defined(USE_TQ) || defined(USE_STREAM)
935
    case TDMT_VND_TMQ_SUBSCRIBE:
1,373,568✔
936
      code = tqProcessSubscribeReq(pVnode->pTq, ver, pReq, len);
1,373,568✔
937
      TSDB_CHECK_CODE(code, lino, _err);
1,370,343✔
938
      break;
1,370,343✔
939
    case TDMT_VND_TMQ_DELETE_SUB:
290,678✔
940
      code = tqProcessDeleteSubReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen);
290,678✔
941
      TSDB_CHECK_CODE(code, lino, _err);
290,066✔
942
      break;
290,066✔
943
    case TDMT_VND_TMQ_COMMIT_OFFSET:
4,777,989✔
944
      code = tqProcessOffsetCommitReq(pVnode->pTq, ver, pReq, len);
4,777,989✔
945
      TSDB_CHECK_CODE(code, lino, _err);
4,778,260✔
946
      break;
4,778,260✔
947
#endif
948
    case TDMT_VND_ALTER_CONFIRM:
3,859,510✔
949
      needCommit = pVnode->config.hashChange;
3,859,510✔
950
      code = vnodeProcessAlterConfirmReq(pVnode, ver, pReq, len, pRsp);
3,859,510✔
951
      TSDB_CHECK_CODE(code, lino, _err);
3,859,510✔
952
      break;
3,859,510✔
953
    case TDMT_VND_ALTER_CONFIG:
321,183✔
954
      vnodeProcessAlterConfigReq(pVnode, ver, pReq, len, pRsp);
321,183✔
955
      break;
321,183✔
956
    case TDMT_VND_COMMIT:
5,082,476✔
957
      needCommit = true;
5,082,476✔
958
      break;
5,082,476✔
959
    case TDMT_VND_CREATE_INDEX:
17,783✔
960
      vnodeProcessCreateIndexReq(pVnode, ver, pReq, len, pRsp);
17,783✔
961
      break;
17,783✔
962
    case TDMT_VND_DROP_INDEX:
14,476✔
963
      vnodeProcessDropIndexReq(pVnode, ver, pReq, len, pRsp);
14,476✔
964
      break;
14,476✔
965
    case TDMT_VND_COMPACT:
65,394✔
966
      vnodeProcessCompactVnodeReq(pVnode, ver, pReq, len, pRsp);
65,394✔
967
      goto _exit;
65,394✔
968
    case TDMT_VND_SCAN:
493✔
969
      vnodeProcessScanVnodeReq(pVnode, ver, pReq, len, pRsp);
493✔
970
      goto _exit;
493✔
UNCOV
971
    case TDMT_SYNC_CONFIG_CHANGE:
×
UNCOV
972
      vnodeProcessConfigChangeReq(pVnode, ver, pReq, len, pRsp);
×
UNCOV
973
      break;
×
974
#ifdef TD_ENTERPRISE
975
    case TDMT_VND_KILL_COMPACT:
1,488✔
976
      vnodeProcessKillCompactReq(pVnode, ver, pReq, len, pRsp);
1,488✔
977
      break;
1,488✔
978
#endif
979
    case TDMT_VND_KILL_SCAN:
90✔
980
      vnodeProcessKillScanReq(pVnode, ver, pReq, len, pRsp);
90✔
981
      break;
90✔
UNCOV
982
    case TDMT_VND_KILL_TRIM:
×
UNCOV
983
      vnodeProcessKillRetentionReq(pVnode, ver, pReq, len, pRsp);
×
UNCOV
984
      break;
×
985
    /* ARB */
986
    case TDMT_VND_ARB_CHECK_SYNC:
3,062✔
987
      vnodeProcessArbCheckSyncReq(pVnode, pReq, len, pRsp);
3,062✔
988
      break;
3,062✔
989
    case TDMT_VND_AUDIT_RECORD:
268✔
990
      vTrace("vgId:%d, processed audit msg", TD_VID(pVnode));
268✔
991
      code = vnodeProcessAuditRecordReq(pVnode, ver, pReq, len, pMsg);
268✔
992
      pRsp->code = code;
268✔
993
      pRsp->msgType = TDMT_VND_AUDIT_RECORD_RSP;
268✔
994
      pRsp->pCont = NULL;
268✔
995
      pRsp->contLen = 0;
268✔
996
      if (code) {
268✔
NEW
997
        goto _err;
×
998
      }
999
      break;
268✔
UNCOV
1000
    default:
×
UNCOV
1001
      vError("vgId:%d, unprocessed msg, %d", TD_VID(pVnode), pMsg->msgType);
×
UNCOV
1002
      return TSDB_CODE_INVALID_MSG;
×
1003
  }
1004

1005
  vGDebug(&pMsg->info.traceId, "vgId:%d, index:%" PRId64 ", msg processed, code:0x%x", TD_VID(pVnode), ver, pRsp->code);
810,608,078✔
1006

1007
  walApplyVer(pVnode->pWal, ver);
810,624,498✔
1008

1009
  if (pVnode->pTq) {
810,638,227✔
1010
    code = tqPushMsg(pVnode->pTq, pMsg->msgType);
810,639,166✔
1011
    if (code) {
810,639,660✔
1012
      vError("vgId:%d, failed to push msg to TQ since %s", TD_VID(pVnode), tstrerror(terrno));
×
UNCOV
1013
      return code;
×
1014
    }
1015
  }
1016

1017
  // commit if need
1018
  if (needCommit) {
810,639,660✔
1019
    vInfo("vgId:%d, commit at version %" PRId64, TD_VID(pVnode), ver);
5,113,086✔
1020
    code = vnodeAsyncCommit(pVnode, forceTrimWal);
5,114,300✔
1021
    if (code) {
5,114,523✔
UNCOV
1022
      vError("vgId:%d, failed to vnode async commit since %s.", TD_VID(pVnode), tstrerror(terrno));
×
UNCOV
1023
      goto _err;
×
1024
    }
1025

1026
    // start a new one
1027
    METRICS_TIMING_BLOCK(pVnode->writeMetrics.memtable_wait_time, METRIC_LEVEL_LOW, {
5,114,523✔
1028
      code = vnodeBegin(pVnode);
1029
      if (code) {
1030
        vError("vgId:%d, failed to begin vnode since %s.", TD_VID(pVnode), tstrerror(terrno));
1031
        goto _err;
1032
      }
1033
    });
1034
  }
1035

1036
_exit:
810,639,088✔
1037
  return 0;
820,894,505✔
1038

1039
_err:
38,930✔
1040
  if (code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
38,930✔
1041
    vInfo("vgId:%d, process %s request failed since %s, lino 1:%d ver:%" PRId64, TD_VID(pVnode),
2,232✔
1042
          TMSG_INFO(pMsg->msgType), tstrerror(code), lino, ver);
1043
  } else if (code) {
36,698✔
UNCOV
1044
    vError("vgId:%d, process %s request failed since %s, lino 2:%d ver:%" PRId64, TD_VID(pVnode),
×
1045
           TMSG_INFO(pMsg->msgType), tstrerror(code), lino, ver);
1046
  }
1047

1048
  return code;
38,930✔
1049
}
1050

1051
int32_t vnodePreprocessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg) {
414,118,975✔
1052
  if (TDMT_SCH_QUERY != pMsg->msgType && TDMT_SCH_MERGE_QUERY != pMsg->msgType) {
414,118,975✔
1053
    return 0;
85,747,696✔
1054
  }
1055

1056
  int32_t qType = 0;
328,400,847✔
1057
  return qWorkerPreprocessQueryMsg(pVnode->pQuery, pMsg, (TDMT_SCH_QUERY == pMsg->msgType), &qType);
328,410,467✔
1058
}
1059

1060
int32_t vnodeProcessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
414,178,327✔
1061
  const STraceId *trace = &pMsg->info.traceId;
414,178,327✔
1062
  vDebug("vgId:%d, msg:%s, %p in vnode query queue is processing, %" PRIx64 ":%" PRIx64, 
414,180,025✔
1063
    pVnode->config.vgId, TMSG_INFO(pMsg->msgType), pMsg, TRACE_GET_ROOTID(trace), TRACE_GET_MSGID(trace));
1064

1065
  if (pMsg->msgType == TDMT_VND_TMQ_CONSUME && !syncIsReadyForRead(pVnode->sync)) {
414,193,555✔
1066
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
469,295✔
1067
    return 0;
469,295✔
1068
  }
1069

1070
  if (pMsg->msgType == TDMT_VND_TMQ_CONSUME && !pVnode->restored) {
413,730,034✔
UNCOV
1071
    vnodeRedirectRpcMsg(pVnode, pMsg, TSDB_CODE_SYN_RESTORING);
×
UNCOV
1072
    return 0;
×
1073
  }
1074

1075
  SReadHandle handle = {0};
413,730,220✔
1076
  handle.vnode = pVnode;
413,730,286✔
1077
  handle.pMsgCb = &pVnode->msgCb;
413,730,286✔
1078
  handle.pWorkerCb = pInfo->workerCb;
413,710,658✔
1079
  initStorageAPI(&handle.api);
413,711,072✔
1080
  int32_t code = TSDB_CODE_SUCCESS;
413,614,711✔
1081
  bool    redirected = false;
413,614,711✔
1082

1083
  switch (pMsg->msgType) {
413,614,711✔
1084
    case TDMT_SCH_QUERY:
270,446,145✔
1085
      if (!syncIsReadyForRead(pVnode->sync)) {
270,446,145✔
1086
        pMsg->code = (terrno) ? terrno : TSDB_CODE_SYN_NOT_LEADER;
1,342,251✔
1087
        redirected = true;
1,342,251✔
1088
      }
1089
      code = qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
270,467,890✔
1090
      if (redirected) {
270,445,286✔
1091
        vnodeRedirectRpcMsg(pVnode, pMsg, pMsg->code);
1,342,251✔
1092
        return 0;
1,342,251✔
1093
      }
1094

1095
      return code;
269,103,035✔
1096
    case TDMT_SCH_MERGE_QUERY:
57,960,025✔
1097
      return qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
57,960,025✔
1098
    case TDMT_SCH_QUERY_CONTINUE:
43,040,808✔
1099
      return qWorkerProcessCQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
43,040,808✔
1100
    case TDMT_VND_TMQ_CONSUME:
31,509,162✔
1101
      return tqProcessPollReq(pVnode->pTq, pMsg);
31,509,162✔
1102
    case TDMT_VND_TMQ_CONSUME_PUSH:
10,716,749✔
1103
      return tqProcessPollPush(pVnode->pTq);
10,716,749✔
UNCOV
1104
    default:
×
UNCOV
1105
      vError("unknown msg type:%d in query queue", pMsg->msgType);
×
UNCOV
1106
      return TSDB_CODE_APP_ERROR;
×
1107
  }
1108
}
1109

1110
int32_t vnodeProcessFetchMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
996,892,037✔
1111
  int32_t code = TSDB_CODE_SUCCESS;
996,892,037✔
1112
  const STraceId *trace = &pMsg->info.traceId;
996,892,037✔
1113
  vDebug("vgId:%d, msg:%s, %p in fetch queue is processing, %" PRIx64 ":%" PRIx64, 
996,938,513✔
1114
    pVnode->config.vgId, TMSG_INFO(pMsg->msgType), pMsg, TRACE_GET_ROOTID(trace), TRACE_GET_MSGID(trace));
1115
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
997,009,915✔
1116
       pMsg->msgType == TDMT_VND_BATCH_META || pMsg->msgType == TDMT_VND_TABLE_NAME ||
695,464,167✔
1117
       pMsg->msgType == TDMT_VND_VSUBTABLES_META || pMsg->msgType == TDMT_VND_VSTB_REF_DBS) &&
667,160,256✔
1118
      !syncIsReadyForRead(pVnode->sync)) {
329,849,053✔
1119
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
481,201✔
1120
    return 0;
481,201✔
1121
  }
1122

1123
  switch (pMsg->msgType) {
996,533,658✔
1124
    case TDMT_SCH_FETCH:
473,337,275✔
1125
    case TDMT_SCH_MERGE_FETCH:
1126
      return qWorkerProcessFetchMsg(pVnode, pVnode->pQuery, pMsg, 0);
473,337,275✔
UNCOV
1127
    case TDMT_SCH_FETCH_RSP:
×
UNCOV
1128
      return qWorkerProcessRspMsg(pVnode, pVnode->pQuery, pMsg, 0);
×
1129
    // case TDMT_SCH_CANCEL_TASK:
1130
    //   return qWorkerProcessCancelMsg(pVnode, pVnode->pQuery, pMsg, 0);
1131
    case TDMT_SCH_DROP_TASK:
328,197,143✔
1132
      return qWorkerProcessDropMsg(pVnode, pVnode->pQuery, pMsg, 0);
328,197,143✔
1133
    case TDMT_SCH_TASK_NOTIFY:
14,423✔
1134
      return qWorkerProcessNotifyMsg(pVnode, pVnode->pQuery, pMsg, 0);
14,423✔
1135
    case TDMT_SCH_QUERY_HEARTBEAT:
166,006,863✔
1136
      return qWorkerProcessHbMsg(pVnode, pVnode->pQuery, pMsg, 0);
166,006,863✔
1137
    case TDMT_VND_TABLE_META:
919,233✔
1138
    case TDMT_VND_TABLE_NAME:
1139
      return vnodeGetTableMeta(pVnode, pMsg, true);
919,233✔
UNCOV
1140
    case TDMT_VND_TABLE_CFG:
×
UNCOV
1141
      return vnodeGetTableCfg(pVnode, pMsg, true);
×
1142
    case TDMT_VND_BATCH_META: {
27,821,030✔
1143
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.fetch_batch_meta_time, METRIC_LEVEL_LOW,
27,821,030✔
1144
                           { code = vnodeGetBatchMeta(pVnode, pMsg); });
1145
      METRICS_UPDATE(pVnode->writeMetrics.fetch_batch_meta_count, METRIC_LEVEL_LOW, 1);
27,823,852✔
1146
      return code;
27,816,345✔
1147
    }
UNCOV
1148
    case TDMT_VND_VSUBTABLES_META:
×
UNCOV
1149
      return vnodeGetVSubtablesMeta(pVnode, pMsg);
×
UNCOV
1150
    case TDMT_VND_VSTB_REF_DBS:
×
UNCOV
1151
      return vnodeGetVStbRefDbs(pVnode, pMsg);
×
1152
    case TDMT_VND_QUERY_SCAN_PROGRESS:
315✔
1153
      return vnodeQueryScanProgress(pVnode, pMsg);
315✔
1154
#ifdef TD_ENTERPRISE
1155
    case TDMT_VND_QUERY_COMPACT_PROGRESS:
101,376✔
1156
      return vnodeQueryCompactProgress(pVnode, pMsg);
101,376✔
1157

1158
    case TDMT_VND_LIST_SSMIGRATE_FILESETS:
4,344✔
1159
      return vnodeListSsMigrateFileSets(pVnode, pMsg);
4,344✔
1160

1161
    case TDMT_VND_QUERY_SSMIGRATE_PROGRESS:
6,516✔
1162
      return vnodeQuerySsMigrateProgress(pVnode, pMsg);
6,516✔
1163
#endif
1164
    case TDMT_VND_QUERY_TRIM_PROGRESS:
107,025✔
1165
      return vnodeQueryRetentionProgress(pVnode, pMsg);
107,025✔
1166
      //    case TDMT_VND_TMQ_CONSUME:
1167
      //      return tqProcessPollReq(pVnode->pTq, pMsg);
1168
#ifdef USE_TQ
1169
    case TDMT_VND_TMQ_VG_WALINFO:
606✔
1170
      return tqProcessVgWalInfoReq(pVnode->pTq, pMsg);
606✔
1171
    case TDMT_VND_TMQ_VG_COMMITTEDINFO:
303✔
1172
      return tqProcessVgCommittedInfoReq(pVnode->pTq, pMsg);
303✔
1173
    case TDMT_VND_TMQ_SEEK:
1,212✔
1174
      return tqProcessSeekReq(pVnode->pTq, pMsg);
1,212✔
1175
#endif
1176
    default:
9,178✔
1177
      vError("unknown msg type:%d in fetch queue", pMsg->msgType);
9,178✔
UNCOV
1178
      return TSDB_CODE_APP_ERROR;
×
1179
  }
1180
}
1181

1182
void vnodeUpdateMetaRsp(SVnode *pVnode, STableMetaRsp *pMetaRsp) {
87,139,725✔
1183
  if (NULL == pMetaRsp) {
87,139,725✔
UNCOV
1184
    return;
×
1185
  }
1186

1187
  tstrncpy(pMetaRsp->dbFName, pVnode->config.dbname, TSDB_DB_FNAME_LEN);
87,139,725✔
1188
  pMetaRsp->dbId = pVnode->config.dbId;
87,140,715✔
1189
  pMetaRsp->vgId = TD_VID(pVnode);
87,141,096✔
1190
  pMetaRsp->precision = pVnode->config.tsdbCfg.precision;
87,141,099✔
1191
}
1192

1193
extern int32_t vnodeAsyncRetention(SVnode *pVnode, STimeWindow tw, int8_t optrType, int8_t triggerType);
1194

1195
static int32_t vnodeProcessTrimReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
64,038✔
1196
  if (!pVnode->restored) {
64,038✔
1197
    vInfo("vgId:%d, ignore trim req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
1,837✔
1198
    return 0;
1,837✔
1199
  }
1200

1201
  int32_t     code = 0;
62,201✔
1202
  SVTrimDbReq trimReq = {0};
62,201✔
1203

1204
  // decode
1205
  if (tDeserializeSVTrimDbReq(pReq, len, &trimReq) != 0) {
62,201✔
UNCOV
1206
    code = TSDB_CODE_INVALID_MSG;
×
UNCOV
1207
    goto _exit;
×
1208
  }
1209

1210
  vInfo("vgId:%d, process trim vnode request, time:%" PRIi64 ",%" PRIi64 ", optr:%d, trigger:%d", pVnode->config.vgId,
62,201✔
1211
        trimReq.tw.skey, trimReq.tw.ekey, (int32_t)trimReq.optrType, (int32_t)trimReq.triggerType);
1212

1213
  code = vnodeAsyncRetention(pVnode, trimReq.tw, (int8_t)trimReq.optrType, (int8_t)trimReq.triggerType);
62,201✔
1214

1215
_exit:
62,201✔
1216
  return code;
62,201✔
1217
}
1218

1219
extern int32_t vnodeAsyncSsMigrateFileSet(SVnode *pVnode, SSsMigrateFileSetReq *pReq);
1220

1221
extern int32_t vnodeAsyncS3Migrate(SVnode *pVnode, int64_t now);
1222

1223
static int32_t vnodeProcessSsMigrateFileSetReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
3,620✔
1224
  int32_t code = 0;
3,620✔
1225

1226
  SSsMigrateFileSetReq req = {0};
3,620✔
1227
  SSsMigrateFileSetRsp rsp = {0};
3,620✔
1228
  pRsp->msgType = TDMT_VND_SSMIGRATE_FILESET_RSP;
3,620✔
1229
  pRsp->code = 0;
3,620✔
1230
  pRsp->pCont = NULL;
3,620✔
1231
  pRsp->contLen = 0;
3,620✔
1232

1233
  if (tDeserializeSSsMigrateFileSetReq(pReq, len, &req) != 0) {
3,620✔
UNCOV
1234
    code = TSDB_CODE_INVALID_MSG;
×
UNCOV
1235
    goto _exit;
×
1236
  }
1237

1238
  vInfo("vgId:%d, ssmigrate:%d, fid:%d, process ssmigrate fileset request, time:%" PRId64, pVnode->config.vgId,
3,620✔
1239
        req.ssMigrateId, req.fid, req.startTimeSec);
1240
  rsp.ssMigrateId = req.ssMigrateId;
3,620✔
1241
  rsp.vgId = TD_VID(pVnode);
3,620✔
1242
  rsp.nodeId = req.nodeId;
3,620✔
1243
  rsp.fid = req.fid;
3,620✔
1244

1245
  code = vnodeAsyncSsMigrateFileSet(pVnode, &req);
3,620✔
1246
  if (code != TSDB_CODE_SUCCESS) {
3,620✔
UNCOV
1247
    vError("vgId:%d, failed to async ssmigrate since %s", TD_VID(pVnode), tstrerror(code));
×
UNCOV
1248
    pRsp->code = code;
×
1249
    goto _exit;
×
1250
  }
1251

1252
  pRsp->code = TSDB_CODE_SUCCESS;
3,620✔
1253
  pRsp->contLen = tSerializeSSsMigrateFileSetRsp(NULL, 0, &rsp);
3,620✔
1254
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
3,620✔
1255
  if (pRsp->pCont == NULL) {
3,620✔
UNCOV
1256
    vError("vgId:%d, failed to allocate memory for ssmigrate fileset response", TD_VID(pVnode));
×
UNCOV
1257
    code = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
1258
    goto _exit;
×
1259
  }
1260
  TAOS_UNUSED(tSerializeSSsMigrateFileSetRsp(pRsp->pCont, pRsp->contLen, &rsp));
3,620✔
1261

1262
_exit:
3,620✔
1263
  pRsp->code = code;
3,620✔
1264
  return code;
3,620✔
1265
}
1266

1267
extern int32_t vnodeFollowerSsMigrate(SVnode *pVnode, SSsMigrateProgress *pReq);
1268

1269
static int32_t vnodeProcessFollowerSsMigrateReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
6,516✔
1270
  int32_t            code = 0;
6,516✔
1271
  SSsMigrateProgress req = {0};
6,516✔
1272

1273
  // decode
1274
  if (tDeserializeSSsMigrateProgress(pReq, len, &req) != 0) {
6,516✔
UNCOV
1275
    code = TSDB_CODE_INVALID_MSG;
×
UNCOV
1276
    goto _exit;
×
1277
  }
1278

1279
  code = vnodeFollowerSsMigrate(pVnode, &req);
6,516✔
1280

1281
_exit:
6,516✔
1282
  pRsp->code = code;
6,516✔
1283
  return code;
6,516✔
1284
}
1285

1286
extern int32_t vnodeKillSsMigrate(SVnode *pVnode, SVnodeKillSsMigrateReq *pReq);
1287

UNCOV
1288
static int32_t vnodeProcessKillSsMigrateReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
UNCOV
1289
  int32_t          code = 0;
×
1290
  SVnodeKillSsMigrateReq req = {0};
×
1291

1292
  // decode
UNCOV
1293
  if (tDeserializeSVnodeKillSsMigrateReq(pReq, len, &req) != 0) {
×
UNCOV
1294
    code = TSDB_CODE_INVALID_MSG;
×
UNCOV
1295
    goto _exit;
×
1296
  }
1297

UNCOV
1298
  code = vnodeKillSsMigrate(pVnode, &req);
×
1299

UNCOV
1300
_exit:
×
UNCOV
1301
  pRsp->code = code;
×
UNCOV
1302
  return code;
×
1303
}
1304

1305
static int32_t vnodeProcessDropTtlTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
UNCOV
1306
  int               ret = 0;
×
UNCOV
1307
  SVDropTtlTableReq ttlReq = {0};
×
1308
  if (tDeserializeSVDropTtlTableReq(pReq, len, &ttlReq) != 0) {
×
1309
    ret = TSDB_CODE_INVALID_MSG;
×
1310
    goto end;
×
1311
  }
1312

1313
  if (ttlReq.nUids != taosArrayGetSize(ttlReq.pTbUids)) {
×
UNCOV
1314
    ret = TSDB_CODE_INVALID_MSG;
×
1315
    goto end;
×
1316
  }
1317

UNCOV
1318
  if (ttlReq.nUids != 0) {
×
UNCOV
1319
    vInfo("vgId:%d, process drop ttl table request, time:%d, ntbUids:%d", pVnode->config.vgId, ttlReq.timestampSec,
×
1320
          ttlReq.nUids);
1321
  }
1322

1323
  if (ttlReq.nUids > 0) {
×
1324
    int32_t code = metaDropMultipleTables(pVnode->pMeta, ver, ttlReq.pTbUids);
×
1325
    if (code) return code;
×
1326

UNCOV
1327
    code = tqDeleteTbUidList(pVnode->pTq, ttlReq.pTbUids);
×
1328
    if (code) {
×
1329
      vError("vgId:%d, failed to delete tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1330
    }
1331
  }
1332

1333
end:
×
1334
  taosArrayDestroy(ttlReq.pTbUids);
×
UNCOV
1335
  return ret;
×
1336
}
1337

1338
static int32_t vnodeProcessFetchTtlExpiredTbs(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
11,670✔
1339
  int32_t                 code = -1;
11,670✔
1340
  SMetaReader             mr = {0};
11,670✔
1341
  SVDropTtlTableReq       ttlReq = {0};
11,670✔
1342
  SVFetchTtlExpiredTbsRsp rsp = {0};
11,670✔
1343
  SEncoder                encoder = {0};
11,670✔
1344
  SArray                 *pNames = NULL;
11,670✔
1345
  pRsp->msgType = TDMT_VND_FETCH_TTL_EXPIRED_TBS_RSP;
11,670✔
1346
  pRsp->code = TSDB_CODE_SUCCESS;
11,670✔
1347
  pRsp->pCont = NULL;
11,670✔
1348
  pRsp->contLen = 0;
11,670✔
1349

1350
  if (tDeserializeSVDropTtlTableReq(pReq, len, &ttlReq) != 0) {
11,670✔
UNCOV
1351
    terrno = TSDB_CODE_INVALID_MSG;
×
UNCOV
1352
    goto _end;
×
1353
  }
1354

1355
  if (!(ttlReq.nUids == taosArrayGetSize(ttlReq.pTbUids))) {
11,670✔
UNCOV
1356
    terrno = TSDB_CODE_INVALID_MSG;
×
UNCOV
1357
    goto _end;
×
1358
  }
1359

1360
  tb_uid_t    suid;
1361
  char        ctbName[TSDB_TABLE_NAME_LEN];
1362
  SVDropTbReq expiredTb = {.igNotExists = true};
11,670✔
1363
  metaReaderDoInit(&mr, pVnode->pMeta, 0);
11,670✔
1364
  rsp.vgId = TD_VID(pVnode);
11,670✔
1365
  rsp.pExpiredTbs = taosArrayInit(ttlReq.nUids, sizeof(SVDropTbReq));
11,670✔
1366
  if (!rsp.pExpiredTbs) goto _end;
11,670✔
1367

1368
  pNames = taosArrayInit(ttlReq.nUids, TSDB_TABLE_NAME_LEN);
11,670✔
1369
  if (!pNames) {
11,670✔
UNCOV
1370
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1371
    goto _end;
×
1372
  }
1373
  char buf[TSDB_TABLE_NAME_LEN];
11,670✔
1374
  for (int32_t i = 0; i < ttlReq.nUids; ++i) {
58,995✔
1375
    tb_uid_t *uid = taosArrayGet(ttlReq.pTbUids, i);
47,325✔
1376
    expiredTb.suid = *uid;
47,325✔
1377
    terrno = metaReaderGetTableEntryByUid(&mr, *uid);
47,325✔
1378
    if (terrno < 0) goto _end;
47,325✔
1379
    tstrncpy(buf, mr.me.name, TSDB_TABLE_NAME_LEN);
47,325✔
1380
    void *p = taosArrayPush(pNames, buf);
47,325✔
1381
    if (p == NULL) {
47,325✔
UNCOV
1382
      goto _end;
×
1383
    }
1384

1385
    expiredTb.name = p;
47,325✔
1386
    if (mr.me.type == TSDB_CHILD_TABLE) {
47,325✔
1387
      expiredTb.suid = mr.me.ctbEntry.suid;
30,515✔
1388
    }
1389

1390
    if (taosArrayPush(rsp.pExpiredTbs, &expiredTb) == NULL) {
94,650✔
UNCOV
1391
      goto _end;
×
1392
    }
1393
  }
1394

1395
  int32_t ret = 0;
11,670✔
1396
  tEncodeSize(tEncodeVFetchTtlExpiredTbsRsp, &rsp, pRsp->contLen, ret);
11,670✔
1397
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
11,670✔
1398
  if (pRsp->pCont == NULL) {
11,670✔
UNCOV
1399
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
1400
    code = -1;
×
UNCOV
1401
    goto _end;
×
1402
  }
1403
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
11,670✔
1404
  terrno = tEncodeVFetchTtlExpiredTbsRsp(&encoder, &rsp);
11,670✔
1405
  tEncoderClear(&encoder);
11,670✔
1406

1407
  if (terrno == 0) code = 0;
11,670✔
1408
_end:
11,670✔
1409
  metaReaderClear(&mr);
11,670✔
1410
  tFreeFetchTtlExpiredTbsRsp(&rsp);
11,670✔
1411
  taosArrayDestroy(ttlReq.pTbUids);
11,670✔
1412
  if (pNames) taosArrayDestroy(pNames);
11,670✔
1413
  pRsp->code = terrno;
11,670✔
1414
  return code;
11,670✔
1415
}
1416

1417
static int32_t vnodeProcessCreateStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
5,179,317✔
1418
  int32_t        code = 0;
5,179,317✔
1419
  SVCreateStbReq req = {0};
5,179,317✔
1420
  SDecoder       coder;
5,188,102✔
1421

1422
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
5,188,464✔
1423
  pRsp->code = TSDB_CODE_SUCCESS;
5,188,522✔
1424
  pRsp->pCont = NULL;
5,189,029✔
1425
  pRsp->contLen = 0;
5,188,464✔
1426

1427
  // decode and process req
1428
  tDecoderInit(&coder, pReq, len);
5,189,029✔
1429

1430
  code = tDecodeSVCreateStbReq(&coder, &req);
5,188,404✔
1431
  if (code) {
5,175,692✔
UNCOV
1432
    pRsp->code = code;
×
UNCOV
1433
    goto _err;
×
1434
  }
1435

1436
  code = metaCreateSuperTable(pVnode->pMeta, ver, &req);
5,175,692✔
1437
  if (code) {
5,189,386✔
1438
    pRsp->code = code;
2,232✔
1439
    goto _err;
2,232✔
1440
  }
1441

1442
  tDecoderClear(&coder);
5,187,154✔
1443
  return 0;
5,186,002✔
1444

1445
_err:
2,232✔
1446
  tDecoderClear(&coder);
2,232✔
1447
  return code;
2,232✔
1448
}
1449

1450
static int32_t vnodeProcessCreateTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
60,527,529✔
1451
                                       SRpcMsg *pOriginRpc) {
1452
  SDecoder           decoder = {0};
60,527,529✔
1453
  SEncoder           encoder = {0};
60,528,321✔
1454
  int32_t            rcode = 0;
60,528,321✔
1455
  SVCreateTbBatchReq req = {0};
60,528,321✔
1456
  SVCreateTbReq     *pCreateReq;
1457
  SVCreateTbBatchRsp rsp = {0};
60,528,022✔
1458
  SVCreateTbRsp      cRsp = {0};
60,528,321✔
1459
  char               tbName[TSDB_TABLE_FNAME_LEN];
60,527,499✔
1460
  SArray            *tbUids = NULL;
60,528,321✔
1461
  SArray            *tbNames = NULL;
60,528,321✔
1462
  int64_t            tss = taosGetTimestampMs();
60,528,321✔
1463
  pRsp->msgType = TDMT_VND_CREATE_TABLE_RSP;
60,528,321✔
1464
  pRsp->code = TSDB_CODE_SUCCESS;
60,528,321✔
1465
  pRsp->pCont = NULL;
60,527,925✔
1466
  pRsp->contLen = 0;
60,528,321✔
1467

1468
  // decode
1469
  tDecoderInit(&decoder, pReq, len);
60,528,321✔
1470
  if (tDecodeSVCreateTbBatchReq(&decoder, &req) < 0) {
60,527,595✔
UNCOV
1471
    rcode = -1;
×
UNCOV
1472
    terrno = TSDB_CODE_INVALID_MSG;
×
UNCOV
1473
    goto _exit;
×
1474
  }
1475

1476
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(cRsp));
60,517,646✔
1477
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
60,522,607✔
1478
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
60,515,401✔
1479
  if (rsp.pArray == NULL || tbUids == NULL || tbNames == NULL) {
60,515,491✔
1480
    rcode = -1;
1,605✔
1481
    terrno = TSDB_CODE_OUT_OF_MEMORY;
1,605✔
UNCOV
1482
    goto _exit;
×
1483
  }
1484

1485
  // loop to create table
1486
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
129,170,047✔
1487
    pCreateReq = req.pReqs + iReq;
68,647,416✔
1488
    memset(&cRsp, 0, sizeof(cRsp));
68,645,458✔
1489

1490
    if (tsEnableAudit && tsEnableAuditCreateTable && tsAuditLevel >= AUDIT_LEVEL_CHILDTABLE) {
68,645,458✔
1491
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
134✔
1492
      if (str == NULL) {
134✔
UNCOV
1493
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
1494
        rcode = -1;
×
UNCOV
1495
        goto _exit;
×
1496
      }
1497
      tstrncpy(str, pCreateReq->name, TSDB_TABLE_FNAME_LEN);
134✔
1498
      if (taosArrayPush(tbNames, &str) == NULL) {
134✔
UNCOV
1499
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
1500
        rcode = -1;
×
UNCOV
1501
        goto _exit;
×
1502
      }
1503
    }
1504

1505
    // validate hash
1506
    (void)tsnprintf(tbName, TSDB_TABLE_FNAME_LEN, "%s.%s", pVnode->config.dbname, pCreateReq->name);
68,645,458✔
1507
    if (vnodeValidateTableHash(pVnode, tbName) < 0) {
68,652,123✔
1508
      cRsp.code = TSDB_CODE_VND_HASH_MISMATCH;
×
1509
      if (taosArrayPush(rsp.pArray, &cRsp) == NULL) {
×
1510
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
1511
        rcode = -1;
×
UNCOV
1512
        goto _exit;
×
1513
      }
1514
      vError("vgId:%d create-table:%s failed due to hash value mismatch", TD_VID(pVnode), tbName);
×
1515
      continue;
×
1516
    }
1517

1518
    // do create table
1519
    if (metaCreateTable2(pVnode->pMeta, ver, pCreateReq, &cRsp.pMeta) < 0) {
68,631,409✔
1520
      if (pCreateReq->flags & TD_CREATE_IF_NOT_EXISTS && terrno == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
16,055✔
1521
        cRsp.code = TSDB_CODE_SUCCESS;
4,334✔
1522
      } else {
1523
        cRsp.code = terrno;
11,721✔
1524
      }
1525
    } else {
1526
      cRsp.code = TSDB_CODE_SUCCESS;
68,639,206✔
1527
      if (taosArrayPush(tbUids, &pCreateReq->uid) == NULL) {
137,279,619✔
UNCOV
1528
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1529
        rcode = -1;
×
1530
        goto _exit;
×
1531
      }
1532
      vnodeUpdateMetaRsp(pVnode, cRsp.pMeta);
68,640,413✔
1533
    }
1534

1535
    if (taosArrayPush(rsp.pArray, &cRsp) == NULL) {
137,311,744✔
UNCOV
1536
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
1537
      rcode = -1;
×
UNCOV
1538
      goto _exit;
×
1539
    }
1540
  }
1541

1542
  vTrace("vgId:%d, add %d new created tables into query table list", TD_VID(pVnode), (int32_t)taosArrayGetSize(tbUids));
60,522,631✔
1543
  if (tqAddTbUidList(pVnode->pTq, tbUids) < 0) {
60,522,631✔
1544
    vError("vgId:%d, failed to add tbUid list since %s", TD_VID(pVnode), tstrerror(terrno));
×
1545
  }
1546

1547
  // prepare rsp
1548
  int32_t ret = 0;
60,527,483✔
1549
  tEncodeSize(tEncodeSVCreateTbBatchRsp, &rsp, pRsp->contLen, ret);
60,527,483✔
1550
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
60,523,650✔
1551
  if (pRsp->pCont == NULL) {
60,519,222✔
1552
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1553
    rcode = -1;
×
UNCOV
1554
    goto _exit;
×
1555
  }
1556
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
60,515,762✔
1557
  if (tEncodeSVCreateTbBatchRsp(&encoder, &rsp) != 0) {
60,511,725✔
UNCOV
1558
    vError("vgId:%d, failed to encode create table batch response", TD_VID(pVnode));
×
1559
  }
1560

1561
  if (tsEnableAudit && tsEnableAuditCreateTable && tsAuditLevel >= AUDIT_LEVEL_CHILDTABLE) {
60,519,364✔
1562
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
134✔
1563

1564
    SName name = {0};
134✔
1565
    if (tNameFromString(&name, pVnode->config.dbname, T_NAME_ACCT | T_NAME_DB) < 0) {
134✔
UNCOV
1566
      vError("vgId:%d, failed to get name from string", TD_VID(pVnode));
×
1567
    }
1568

1569
    SStringBuilder sb = {0};
134✔
1570
    for (int32_t i = 0; i < tbNames->size; i++) {
268✔
1571
      char **key = (char **)taosArrayGet(tbNames, i);
134✔
1572
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
134✔
1573
      if (i < tbNames->size - 1) {
134✔
UNCOV
1574
        taosStringBuilderAppendChar(&sb, ',');
×
1575
      }
1576
      // taosMemoryFreeClear(*key);
1577
    }
1578

1579
    size_t len = 0;
134✔
1580
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
134✔
1581

1582
    if (strlen(RPC_MSG_USER(pOriginRpc)) > 0) {
134✔
1583
      int64_t tse = taosGetTimestampMs();
134✔
1584
      double  duration = (double)(tse - tss);
134✔
1585
      duration = duration / 1000;
134✔
1586
      auditAddRecord(pOriginRpc, clusterId, "createTable", name.dbname, "", keyJoined, len, duration, 0);
134✔
1587
    }
1588

1589
    taosStringBuilderDestroy(&sb);
134✔
1590
  }
1591

1592
_exit:
60,519,364✔
1593
  tDeleteSVCreateTbBatchReq(&req);
60,516,873✔
1594
  taosArrayDestroyEx(rsp.pArray, tFreeSVCreateTbRsp);
60,516,316✔
1595
  taosArrayDestroy(tbUids);
60,502,713✔
1596
  tDecoderClear(&decoder);
60,499,971✔
1597
  tEncoderClear(&encoder);
60,521,186✔
1598
  taosArrayDestroyP(tbNames, NULL);
60,515,494✔
1599
  return rcode;
60,519,829✔
1600
}
1601

1602
static int32_t vnodeProcessAlterStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
7,479,701✔
1603
  int32_t        code = 0;
7,479,701✔
1604
  SVCreateStbReq req = {0};
7,479,701✔
1605
  SDecoder       dc = {0};
7,479,701✔
1606

1607
  pRsp->msgType = TDMT_VND_ALTER_STB_RSP;
7,479,701✔
1608
  pRsp->code = TSDB_CODE_SUCCESS;
7,479,701✔
1609
  pRsp->pCont = NULL;
7,479,701✔
1610
  pRsp->contLen = 0;
7,479,701✔
1611

1612
  tDecoderInit(&dc, pReq, len);
7,479,701✔
1613

1614
  // decode req
1615
  code = tDecodeSVCreateStbReq(&dc, &req);
7,479,701✔
1616
  if (code) {
7,473,227✔
UNCOV
1617
    tDecoderClear(&dc);
×
UNCOV
1618
    return code;
×
1619
  }
1620

1621
  code = metaAlterSuperTable(pVnode->pMeta, ver, &req);
7,473,227✔
1622
  if (code) {
7,479,701✔
UNCOV
1623
    pRsp->code = code;
×
UNCOV
1624
    tDecoderClear(&dc);
×
UNCOV
1625
    return code;
×
1626
  }
1627

1628
  tDecoderClear(&dc);
7,479,701✔
1629

1630
  return 0;
7,479,701✔
1631
}
1632

1633
static int32_t vnodeProcessDropStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp, SRpcMsg *pOriginRpc) {
909,610✔
1634
  SVDropStbReq req = {0};
909,610✔
1635
  int32_t      rcode = TSDB_CODE_SUCCESS;
909,610✔
1636
  SDecoder     decoder = {0};
909,610✔
1637
  SArray      *tbUidList = NULL;
909,610✔
1638

1639
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
909,610✔
1640
  pRsp->pCont = NULL;
909,610✔
1641
  pRsp->contLen = 0;
909,610✔
1642

1643
  // decode request
1644
  tDecoderInit(&decoder, pReq, len);
909,610✔
1645
  if (tDecodeSVDropStbReq(&decoder, &req) < 0) {
909,610✔
UNCOV
1646
    rcode = TSDB_CODE_INVALID_MSG;
×
UNCOV
1647
    goto _exit;
×
1648
  }
1649

1650
  STraceId* trace = &(pOriginRpc->info.traceId);
909,610✔
1651

1652
  vInfo("vgId:%d, start to process vnode-drop-stb, QID:0x%" PRIx64 ":0x%" PRIx64 ", drop stb:%s", TD_VID(pVnode), trace ? trace->rootId : 0, 
909,610✔
1653
              trace ? trace->msgId : 0, req.name);
1654

1655
  // process request
1656
  tbUidList = taosArrayInit(8, sizeof(int64_t));
909,804✔
1657
  if (tbUidList == NULL) goto _exit;
909,610✔
1658
  if (metaDropSuperTable(pVnode->pMeta, ver, &req) < 0) {
909,610✔
1659
    rcode = terrno;
1,488✔
1660
    goto _exit;
1,488✔
1661
  }
1662

1663
  if (tqDeleteTbUidList(pVnode->pTq, tbUidList) < 0) {
908,122✔
UNCOV
1664
    rcode = terrno;
×
UNCOV
1665
    goto _exit;
×
1666
  }
1667

1668
  // return rsp
1669
_exit:
909,610✔
1670
  vInfo("vgId:%d, finished to process vnode-drop-stb, QID:0x%" PRIx64 ":0x%" PRIx64 ", drop stb:%s", TD_VID(pVnode), trace ? trace->rootId : 0, 
909,610✔
1671
              trace ? trace->msgId : 0, req.name);
1672
  if (tbUidList) taosArrayDestroy(tbUidList);
909,610✔
1673
  pRsp->code = rcode;
909,610✔
1674
  tDecoderClear(&decoder);
909,610✔
1675
  return 0;
909,610✔
1676
}
1677

1678
static void alterTagForTmq(SVnode *pVnode, SVAlterTbReq *vAlterTbReq) {
7,605,004✔
1679
  int32_t       code = 0;
7,605,004✔
1680
  int32_t       lino = 0;
7,605,004✔
1681
  SArray* tbUids = NULL;
7,605,004✔
1682
  SArray* cidList = NULL;
7,605,004✔
1683

1684
  int64_t uid = metaGetTableEntryUidByName(pVnode->pMeta, vAlterTbReq->tbName);
7,605,004✔
1685
  QUERY_CHECK_CONDITION(uid != 0, code, lino, end, TSDB_CODE_TDB_TABLE_NOT_EXIST);
7,605,004✔
1686
  
1687
  tbUids = taosArrayInit(4, sizeof(int64_t));
7,605,004✔
1688
  QUERY_CHECK_NULL(tbUids, code, lino, end, terrno);
7,605,004✔
1689

1690
  QUERY_CHECK_CONDITION(taosArrayPush(tbUids, &uid) != NULL, code, lino, end, terrno);
7,605,004✔
1691

1692
  cidList = taosArrayInit(4, sizeof(col_id_t));
7,605,004✔
1693
  QUERY_CHECK_NULL(cidList, code, lino, end, terrno);
7,605,004✔
1694

1695
  if (vAlterTbReq->action == TSDB_ALTER_TABLE_UPDATE_TAG_VAL){
7,605,004✔
1696
    col_id_t cid = vAlterTbReq->colId;
7,602,003✔
1697
    QUERY_CHECK_CONDITION(taosArrayPush(cidList, &cid) != NULL, code, lino, end, terrno);
7,602,003✔
1698
  } else {
1699
    for (int32_t i = 0; i < taosArrayGetSize(vAlterTbReq->pMultiTag); i++) {
18,948✔
1700
      SMultiTagUpateVal *pTagVal = taosArrayGet(vAlterTbReq->pMultiTag, i);
15,947✔
1701
      QUERY_CHECK_NULL(pTagVal, code, lino, end, terrno);
15,947✔
1702
      col_id_t cid = pTagVal->colId;
15,947✔
1703
      QUERY_CHECK_CONDITION(taosArrayPush(cidList, &cid) != NULL, code, lino, end, terrno);
15,947✔
1704
    }
1705
  }
1706
  
1707
  vDebug("vgId:%d, try to add table:%s in query table list, cidList size:%"PRIzu, TD_VID(pVnode), vAlterTbReq->tbName, taosArrayGetSize(cidList));
7,605,004✔
1708
  code = tqUpdateTbUidList(pVnode->pTq, tbUids, cidList);
7,605,004✔
1709
  QUERY_CHECK_CODE(code, lino, end);
7,605,004✔
1710

1711
end:
7,605,004✔
1712
  if (code != 0) {
7,605,004✔
UNCOV
1713
    qError("vgId:%d, failed to alter table:%s since %s", TD_VID(pVnode), vAlterTbReq->tbName, tstrerror(code));
×
1714
  }
1715
  taosArrayDestroy(tbUids);
7,605,004✔
1716
  taosArrayDestroy(cidList);
7,605,004✔
1717
}
7,605,004✔
1718

1719
static int32_t vnodeProcessAlterTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
12,103,226✔
1720
  SVAlterTbReq  vAlterTbReq = {0};
12,103,226✔
1721
  SVAlterTbRsp  vAlterTbRsp = {0};
12,103,226✔
1722
  SDecoder      dc = {0};
12,103,226✔
1723
  int32_t       code = 0;
12,103,226✔
1724
  int32_t       lino = 0;
12,103,226✔
1725
  int32_t       ret;
1726
  SEncoder      ec = {0};
12,103,226✔
1727
  STableMetaRsp vMetaRsp = {0};
12,103,226✔
1728

1729
  pRsp->msgType = TDMT_VND_ALTER_TABLE_RSP;
12,103,226✔
1730
  pRsp->pCont = NULL;
12,103,226✔
1731
  pRsp->contLen = 0;
12,103,226✔
1732
  pRsp->code = TSDB_CODE_SUCCESS;
12,103,226✔
1733

1734
  tDecoderInit(&dc, pReq, len);
12,103,226✔
1735

1736
  // decode
1737
  if (tDecodeSVAlterTbReq(&dc, &vAlterTbReq) < 0) {
12,103,226✔
UNCOV
1738
    vAlterTbRsp.code = TSDB_CODE_INVALID_MSG;
×
UNCOV
1739
    tDecoderClear(&dc);
×
UNCOV
1740
    goto _exit;
×
1741
  }
1742

1743
  // process
1744
  if (metaAlterTable(pVnode->pMeta, ver, &vAlterTbReq, &vMetaRsp) < 0) {
12,103,226✔
1745
    vAlterTbRsp.code = terrno;
705,019✔
1746
    tDecoderClear(&dc);
705,019✔
1747
    goto _exit;
705,019✔
1748
  }
1749
  tDecoderClear(&dc);
11,398,207✔
1750

1751
  if (NULL != vMetaRsp.pSchemas) {
11,398,207✔
1752
    vnodeUpdateMetaRsp(pVnode, &vMetaRsp);
3,762,072✔
1753
    vAlterTbRsp.pMeta = &vMetaRsp;
3,762,072✔
1754
  }
1755

1756
  if (vAlterTbReq.action == TSDB_ALTER_TABLE_UPDATE_TAG_VAL || vAlterTbReq.action == TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL) {
11,398,207✔
1757
    alterTagForTmq(pVnode, &vAlterTbReq);
7,605,004✔
1758
  }
1759

1760
_exit:
12,103,226✔
1761
  taosArrayDestroy(vAlterTbReq.pMultiTag);
12,103,226✔
1762
  tEncodeSize(tEncodeSVAlterTbRsp, &vAlterTbRsp, pRsp->contLen, ret);
12,103,226✔
1763
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
12,103,226✔
1764
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
12,103,226✔
1765
  if (tEncodeSVAlterTbRsp(&ec, &vAlterTbRsp) != 0) {
12,103,226✔
UNCOV
1766
    vError("vgId:%d, failed to encode alter table response", TD_VID(pVnode));
×
1767
  }
1768

1769
  tEncoderClear(&ec);
12,103,226✔
1770
  if (vMetaRsp.pSchemas) {
12,103,226✔
1771
    taosMemoryFree(vMetaRsp.pSchemas);
3,762,072✔
1772
    taosMemoryFree(vMetaRsp.pSchemaExt);
3,762,072✔
1773
  }
1774
  if (vMetaRsp.pColRefs) {
12,103,226✔
1775
    taosMemoryFree(vMetaRsp.pColRefs);
273,489✔
1776
  }
1777
  return 0;
12,103,226✔
1778
}
1779

1780
static int32_t vnodeProcessDropTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
1,451,113✔
1781
                                     SRpcMsg *pOriginRpc) {
1782
  SVDropTbBatchReq req = {0};
1,451,113✔
1783
  SVDropTbBatchRsp rsp = {0};
1,451,113✔
1784
  SDecoder         decoder = {0};
1,451,113✔
1785
  SEncoder         encoder = {0};
1,451,113✔
1786
  int32_t          ret;
1787
  SArray          *tbUids = NULL;
1,451,113✔
1788
  SArray          *tbNames = NULL;
1,451,113✔
1789
  int64_t          tss = taosGetTimestampMs();
1,451,113✔
1790

1791
  pRsp->msgType = ((SRpcMsg *)pReq)->msgType + 1;
1,451,113✔
1792
  pRsp->pCont = NULL;
1,451,113✔
1793
  pRsp->contLen = 0;
1,451,113✔
1794
  pRsp->code = TSDB_CODE_SUCCESS;
1,451,113✔
1795

1796
  // decode req
1797
  tDecoderInit(&decoder, pReq, len);
1,451,113✔
1798
  ret = tDecodeSVDropTbBatchReq(&decoder, &req);
1,451,113✔
1799
  if (ret < 0) {
1,451,113✔
UNCOV
1800
    terrno = TSDB_CODE_INVALID_MSG;
×
UNCOV
1801
    pRsp->code = terrno;
×
UNCOV
1802
    goto _exit;
×
1803
  }
1804

1805
  // process req
1806
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
1,451,113✔
1807
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(SVDropTbRsp));
1,451,113✔
1808
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
1,451,113✔
1809
  if (tbUids == NULL || rsp.pArray == NULL || tbNames == NULL) goto _exit;
1,451,113✔
1810

1811
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
2,949,594✔
1812
    SVDropTbReq *pDropTbReq = req.pReqs + iReq;
1,498,481✔
1813
    SVDropTbRsp  dropTbRsp = {0};
1,498,481✔
1814
    tb_uid_t     tbUid = 0;
1,498,481✔
1815

1816
    /* code */
1817
    ret = metaDropTable2(pVnode->pMeta, ver, pDropTbReq);
1,498,481✔
1818
    if (ret < 0) {
1,498,481✔
UNCOV
1819
      if (pDropTbReq->igNotExists && terrno == TSDB_CODE_TDB_TABLE_NOT_EXIST) {
×
UNCOV
1820
        dropTbRsp.code = TSDB_CODE_SUCCESS;
×
1821
      } else {
UNCOV
1822
        dropTbRsp.code = terrno;
×
1823
      }
1824
    } else {
1825
      dropTbRsp.code = TSDB_CODE_SUCCESS;
1,498,481✔
1826
    }
1827

1828
    if (taosArrayPush(rsp.pArray, &dropTbRsp) == NULL) {
2,996,962✔
UNCOV
1829
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
1830
      pRsp->code = terrno;
×
UNCOV
1831
      goto _exit;
×
1832
    }
1833

1834
    if (tsEnableAudit && tsEnableAuditCreateTable && tsAuditLevel >= AUDIT_LEVEL_CHILDTABLE) {
1,498,481✔
1835
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
×
UNCOV
1836
      if (str == NULL) {
×
1837
        pRsp->code = terrno;
×
UNCOV
1838
        goto _exit;
×
1839
      }
UNCOV
1840
      tstrncpy(str, pDropTbReq->name, TSDB_TABLE_FNAME_LEN);
×
UNCOV
1841
      if (taosArrayPush(tbNames, &str) == NULL) {
×
UNCOV
1842
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
1843
        pRsp->code = terrno;
×
1844
        goto _exit;
×
1845
      }
1846
    }
1847
  }
1848

1849
  if (tqDeleteTbUidList(pVnode->pTq, tbUids) < 0) {
1,451,113✔
1850
    vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(terrno));
×
1851
  }
1852

1853
  if (tsEnableAudit && tsEnableAuditCreateTable && tsAuditLevel >= AUDIT_LEVEL_CHILDTABLE) {
1,451,113✔
UNCOV
1854
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
×
1855

1856
    SName name = {0};
×
1857
    if (tNameFromString(&name, pVnode->config.dbname, T_NAME_ACCT | T_NAME_DB) != 0) {
×
1858
      vError("vgId:%d, failed to get name from string", TD_VID(pVnode));
×
1859
    }
1860

UNCOV
1861
    SStringBuilder sb = {0};
×
UNCOV
1862
    for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
×
UNCOV
1863
      char **key = (char **)taosArrayGet(tbNames, iReq);
×
UNCOV
1864
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
×
1865
      if (iReq < req.nReqs - 1) {
×
UNCOV
1866
        taosStringBuilderAppendChar(&sb, ',');
×
1867
      }
UNCOV
1868
      taosMemoryFreeClear(*key);
×
1869
    }
1870

1871
    size_t len = 0;
×
1872
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
×
1873

UNCOV
1874
    if (strlen(RPC_MSG_USER(pOriginRpc)) > 0) {
×
UNCOV
1875
      int64_t tse = taosGetTimestampMs();
×
1876
      double  duration = (double)(tse - tss);
×
1877
      duration = duration / 1000;
×
1878
      auditAddRecord(pOriginRpc, clusterId, "dropTable", name.dbname, "", keyJoined, len, duration, 0);
×
1879
    }
1880

1881
    taosStringBuilderDestroy(&sb);
×
1882
  }
1883

1884
_exit:
1,451,113✔
1885
  taosArrayDestroy(tbUids);
1,451,113✔
1886
  tDecoderClear(&decoder);
1,451,113✔
1887
  tEncodeSize(tEncodeSVDropTbBatchRsp, &rsp, pRsp->contLen, ret);
1,451,113✔
1888
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
1,451,113✔
1889
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
1,451,113✔
1890
  if (tEncodeSVDropTbBatchRsp(&encoder, &rsp) != 0) {
1,451,113✔
1891
    vError("vgId:%d, failed to encode drop table batch response", TD_VID(pVnode));
×
1892
  }
1893
  tEncoderClear(&encoder);
1,451,113✔
1894
  taosArrayDestroy(rsp.pArray);
1,451,113✔
1895
  taosArrayDestroy(tbNames);
1,451,113✔
1896
  return 0;
1,451,113✔
1897
}
1898

1899
static int32_t vnodeProcessCreateRsmaReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
37,728✔
1900
  int32_t         code = 0, lino = 0;
37,728✔
1901
  SVCreateRsmaReq req = {0};
37,728✔
1902
  TAOS_CHECK_EXIT(tDeserializeSVCreateRsmaReq(pReq, len, &req));
38,452✔
1903
  TAOS_CHECK_EXIT(metaCreateRsma(pVnode->pMeta, ver, &req));
38,452✔
1904
_exit:
38,452✔
1905
  pRsp->msgType = TDMT_VND_CREATE_RSMA_RSP;
38,452✔
1906
  pRsp->pCont = NULL;
38,452✔
1907
  pRsp->code = code;
38,452✔
1908
  pRsp->contLen = 0;
38,452✔
1909
  tFreeSVCreateRsmaReq(&req);
38,452✔
1910
  return code;
38,452✔
1911
}
1912

1913
static int32_t vnodeProcessDropRsmaReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
19,360✔
1914
  int32_t       code = 0, lino = 0;
19,360✔
1915
  SVDropRsmaReq req = {0};
19,360✔
1916
  TAOS_CHECK_EXIT(tDeserializeSVDropRsmaReq(pReq, len, &req));
19,360✔
1917
  TAOS_CHECK_EXIT(metaDropRsma(pVnode->pMeta, ver, &req));
19,360✔
1918
_exit:
19,360✔
1919
  pRsp->msgType = TDMT_VND_DROP_RSMA_RSP;
19,360✔
1920
  pRsp->pCont = NULL;
19,360✔
1921
  pRsp->code = code;
19,360✔
1922
  pRsp->contLen = 0;
19,360✔
1923
  return 0;
19,360✔
1924
}
1925

1926
static int32_t vnodeProcessAlterRsmaReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
11,852✔
1927
  int32_t         code = 0, lino = 0;
11,852✔
1928
  SVAlterRsmaReq req = {0};
11,852✔
1929
  TAOS_CHECK_EXIT(tDeserializeSVAlterRsmaReq(pReq, len, &req));
11,852✔
1930
  TAOS_CHECK_EXIT(metaAlterRsma(pVnode->pMeta, ver, &req));
11,852✔
1931
_exit:
11,852✔
1932
  pRsp->msgType = TDMT_VND_ALTER_RSMA_RSP;
11,852✔
1933
  pRsp->pCont = NULL;
11,852✔
1934
  pRsp->code = code;
11,852✔
1935
  pRsp->contLen = 0;
11,852✔
1936
  tFreeSVAlterRsmaReq(&req);
11,852✔
1937
  return code;
11,852✔
1938
}
1939

1940
#ifdef BUILD_NO_CALL
1941
static int32_t vnodeDebugPrintSingleSubmitMsg(SMeta *pMeta, SSubmitBlk *pBlock, SSubmitMsgIter *msgIter,
1942
                                              const char *tags) {
1943
  SSubmitBlkIter blkIter = {0};
1944
  STSchema      *pSchema = NULL;
1945
  tb_uid_t       suid = 0;
1946
  STSRow        *row = NULL;
1947
  int32_t        rv = -1;
1948

1949
  tInitSubmitBlkIter(msgIter, pBlock, &blkIter);
1950
  if (blkIter.row == NULL) return 0;
1951

1952
  int32_t code = metaGetTbTSchemaNotNull(pMeta, msgIter->suid, TD_ROW_SVER(blkIter.row), 1,
1953
                                         &pSchema);  // TODO: use the real schema
1954
  if (TSDB_CODE_SUCCESS != code) {
1955
    printf("%s:%d no valid schema\n", tags, __LINE__);
1956
    return code;
1957
  }
1958

1959
  suid = msgIter->suid;
1960
  rv = TD_ROW_SVER(blkIter.row);
1961

1962
  char __tags[128] = {0};
1963
  snprintf(__tags, 128, "%s: uid %" PRIi64 " ", tags, msgIter->uid);
1964
  while ((row = tGetSubmitBlkNext(&blkIter))) {
1965
    tdSRowPrint(row, pSchema, __tags);
1966
  }
1967

1968
  taosMemoryFreeClear(pSchema);
1969

1970
  return TSDB_CODE_SUCCESS;
1971
}
1972
#endif
1973
typedef struct SSubmitReqConvertCxt {
1974
  SSubmitMsgIter msgIter;
1975
  SSubmitBlk    *pBlock;
1976
  SSubmitBlkIter blkIter;
1977
  STSRow        *pRow;
1978
  STSRowIter     rowIter;
1979
  SSubmitTbData *pTbData;
1980
  STSchema      *pTbSchema;
1981
  SArray        *pColValues;
1982
} SSubmitReqConvertCxt;
1983

UNCOV
1984
static int32_t vnodeResetTableCxt(SMeta *pMeta, SSubmitReqConvertCxt *pCxt) {
×
UNCOV
1985
  taosMemoryFreeClear(pCxt->pTbSchema);
×
UNCOV
1986
  int32_t code = metaGetTbTSchemaNotNull(pMeta, pCxt->msgIter.suid > 0 ? pCxt->msgIter.suid : pCxt->msgIter.uid,
×
1987
                                         pCxt->msgIter.sversion, 1, &pCxt->pTbSchema);
UNCOV
1988
  if (TSDB_CODE_SUCCESS != code) {
×
UNCOV
1989
    return code;
×
1990
  }
UNCOV
1991
  tdSTSRowIterInit(&pCxt->rowIter, pCxt->pTbSchema);
×
1992

UNCOV
1993
  tDestroySubmitTbData(pCxt->pTbData, TSDB_MSG_FLG_ENCODE);
×
UNCOV
1994
  if (NULL == pCxt->pTbData) {
×
UNCOV
1995
    pCxt->pTbData = taosMemoryCalloc(1, sizeof(SSubmitTbData));
×
UNCOV
1996
    if (NULL == pCxt->pTbData) {
×
UNCOV
1997
      return terrno;
×
1998
    }
1999
  }
2000
  pCxt->pTbData->flags = 0;
×
2001
  pCxt->pTbData->suid = pCxt->msgIter.suid;
×
UNCOV
2002
  pCxt->pTbData->uid = pCxt->msgIter.uid;
×
2003
  pCxt->pTbData->sver = pCxt->msgIter.sversion;
×
2004
  pCxt->pTbData->pCreateTbReq = NULL;
×
UNCOV
2005
  pCxt->pTbData->aRowP = taosArrayInit(128, POINTER_BYTES);
×
2006
  if (NULL == pCxt->pTbData->aRowP) {
×
UNCOV
2007
    return terrno;
×
2008
  }
2009

2010
  taosArrayDestroy(pCxt->pColValues);
×
2011
  pCxt->pColValues = taosArrayInit(pCxt->pTbSchema->numOfCols, sizeof(SColVal));
×
2012
  if (NULL == pCxt->pColValues) {
×
UNCOV
2013
    return terrno;
×
2014
  }
2015
  for (int32_t i = 0; i < pCxt->pTbSchema->numOfCols; ++i) {
×
2016
    SColVal val = COL_VAL_NONE(pCxt->pTbSchema->columns[i].colId, pCxt->pTbSchema->columns[i].type);
×
2017
    if (taosArrayPush(pCxt->pColValues, &val) == NULL) {
×
2018
      return terrno;
×
2019
    }
2020
  }
2021

2022
  return TSDB_CODE_SUCCESS;
×
2023
}
2024

2025
static void vnodeDestroySubmitReqConvertCxt(SSubmitReqConvertCxt *pCxt) {
×
2026
  taosMemoryFreeClear(pCxt->pTbSchema);
×
2027
  tDestroySubmitTbData(pCxt->pTbData, TSDB_MSG_FLG_ENCODE);
×
2028
  taosMemoryFreeClear(pCxt->pTbData);
×
UNCOV
2029
  taosArrayDestroy(pCxt->pColValues);
×
2030
}
×
2031

2032
static int32_t vnodeCellValConvertToColVal(STColumn *pCol, SCellVal *pCellVal, SColVal *pColVal) {
×
2033
  if (tdValTypeIsNone(pCellVal->valType)) {
×
UNCOV
2034
    pColVal->flag = CV_FLAG_NONE;
×
UNCOV
2035
    return TSDB_CODE_SUCCESS;
×
2036
  }
2037

UNCOV
2038
  if (tdValTypeIsNull(pCellVal->valType)) {
×
UNCOV
2039
    pColVal->flag = CV_FLAG_NULL;
×
2040
    return TSDB_CODE_SUCCESS;
×
2041
  }
2042

2043
  if (IS_VAR_DATA_TYPE(pCol->type)) {
×
2044
    if (IS_STR_DATA_BLOB(pCol->type)) {
×
2045
      pColVal->value.nData = blobDataLen(pCellVal->val);
×
UNCOV
2046
      pColVal->value.pData = (uint8_t *)blobDataVal(pCellVal->val);
×
2047

2048
    } else {
2049
      pColVal->value.nData = varDataLen(pCellVal->val);
×
2050
      pColVal->value.pData = (uint8_t *)varDataVal(pCellVal->val);
×
2051
    }
UNCOV
2052
  } else if (TSDB_DATA_TYPE_FLOAT == pCol->type) {
×
2053
    float f = GET_FLOAT_VAL(pCellVal->val);
×
2054
    valueSetDatum(&pColVal->value, pCol->type, &f, sizeof(f));
×
2055
  } else if (TSDB_DATA_TYPE_DOUBLE == pCol->type) {
×
UNCOV
2056
    taosSetPInt64Aligned(&pColVal->value.val, (int64_t *)pCellVal->val);
×
2057
  } else {
2058
    valueSetDatum(&pColVal->value, pCol->type, pCellVal->val, tDataTypes[pCol->type].bytes);
×
2059
  }
2060

2061
  pColVal->flag = CV_FLAG_VALUE;
×
UNCOV
2062
  return TSDB_CODE_SUCCESS;
×
2063
}
2064

2065
static int32_t vnodeTSRowConvertToColValArray(SSubmitReqConvertCxt *pCxt) {
×
UNCOV
2066
  int32_t code = TSDB_CODE_SUCCESS;
×
2067
  tdSTSRowIterReset(&pCxt->rowIter, pCxt->pRow);
×
2068
  for (int32_t i = 0; TSDB_CODE_SUCCESS == code && i < pCxt->pTbSchema->numOfCols; ++i) {
×
2069
    STColumn *pCol = pCxt->pTbSchema->columns + i;
×
2070
    SCellVal  cellVal = {0};
×
2071
    if (!tdSTSRowIterFetch(&pCxt->rowIter, pCol->colId, pCol->type, &cellVal)) {
×
UNCOV
2072
      break;
×
2073
    }
UNCOV
2074
    code = vnodeCellValConvertToColVal(pCol, &cellVal, (SColVal *)taosArrayGet(pCxt->pColValues, i));
×
2075
  }
2076
  return code;
×
2077
}
2078

UNCOV
2079
static int32_t vnodeDecodeCreateTbReq(SSubmitReqConvertCxt *pCxt) {
×
2080
  if (pCxt->msgIter.schemaLen <= 0) {
×
2081
    return TSDB_CODE_SUCCESS;
×
2082
  }
2083

2084
  pCxt->pTbData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
×
2085
  if (NULL == pCxt->pTbData->pCreateTbReq) {
×
2086
    return terrno;
×
2087
  }
2088

2089
  SDecoder decoder = {0};
×
UNCOV
2090
  tDecoderInit(&decoder, (uint8_t *)pCxt->pBlock->data, pCxt->msgIter.schemaLen);
×
2091
  int32_t code = tDecodeSVCreateTbReq(&decoder, pCxt->pTbData->pCreateTbReq);
×
UNCOV
2092
  tDecoderClear(&decoder);
×
2093

2094
  return code;
×
2095
}
2096

UNCOV
2097
static int32_t vnodeSubmitReqConvertToSubmitReq2(SVnode *pVnode, SSubmitReq *pReq, SSubmitReq2 *pReq2) {
×
UNCOV
2098
  pReq2->aSubmitTbData = taosArrayInit(128, sizeof(SSubmitTbData));
×
2099
  if (NULL == pReq2->aSubmitTbData) {
×
2100
    return terrno;
×
2101
  }
2102

UNCOV
2103
  SSubmitReqConvertCxt cxt = {0};
×
2104

2105
  int32_t code = tInitSubmitMsgIter(pReq, &cxt.msgIter);
×
2106
  while (TSDB_CODE_SUCCESS == code) {
×
2107
    code = tGetSubmitMsgNext(&cxt.msgIter, &cxt.pBlock);
×
UNCOV
2108
    if (TSDB_CODE_SUCCESS == code) {
×
2109
      if (NULL == cxt.pBlock) {
×
UNCOV
2110
        break;
×
2111
      }
2112
      code = vnodeResetTableCxt(pVnode->pMeta, &cxt);
×
2113
    }
2114
    if (TSDB_CODE_SUCCESS == code) {
×
2115
      code = tInitSubmitBlkIter(&cxt.msgIter, cxt.pBlock, &cxt.blkIter);
×
2116
    }
UNCOV
2117
    if (TSDB_CODE_SUCCESS == code) {
×
2118
      code = vnodeDecodeCreateTbReq(&cxt);
×
2119
    }
2120
    while (TSDB_CODE_SUCCESS == code && (cxt.pRow = tGetSubmitBlkNext(&cxt.blkIter)) != NULL) {
×
2121
      code = vnodeTSRowConvertToColValArray(&cxt);
×
2122
      if (TSDB_CODE_SUCCESS == code) {
×
2123
        SRow **pNewRow = taosArrayReserve(cxt.pTbData->aRowP, 1);
×
2124

2125
        SRowBuildScanInfo sinfo = {0};
×
UNCOV
2126
        code = tRowBuild(cxt.pColValues, cxt.pTbSchema, pNewRow, &sinfo);
×
2127
      }
2128
    }
2129
    if (TSDB_CODE_SUCCESS == code) {
×
2130
      code = (NULL == taosArrayPush(pReq2->aSubmitTbData, cxt.pTbData) ? terrno : TSDB_CODE_SUCCESS);
×
2131
    }
2132
    if (TSDB_CODE_SUCCESS == code) {
×
2133
      taosMemoryFreeClear(cxt.pTbData);
×
2134
    }
2135
  }
2136

2137
  vnodeDestroySubmitReqConvertCxt(&cxt);
×
2138
  return code;
×
2139
}
2140

2141
static int32_t vnodeRebuildSubmitReqMsg(SSubmitReq2 *pSubmitReq, void **ppMsg) {
×
UNCOV
2142
  int32_t  code = TSDB_CODE_SUCCESS;
×
UNCOV
2143
  char    *pMsg = NULL;
×
2144
  uint32_t msglen = 0;
×
2145
  tEncodeSize(tEncodeSubmitReq, pSubmitReq, msglen, code);
×
UNCOV
2146
  if (TSDB_CODE_SUCCESS == code) {
×
2147
    pMsg = taosMemoryMalloc(msglen);
×
2148
    if (NULL == pMsg) {
×
UNCOV
2149
      code = terrno;
×
2150
    }
2151
  }
2152
  if (TSDB_CODE_SUCCESS == code) {
×
2153
    SEncoder encoder;
×
UNCOV
2154
    tEncoderInit(&encoder, (uint8_t *)pMsg, msglen);
×
UNCOV
2155
    code = tEncodeSubmitReq(&encoder, pSubmitReq);
×
2156
    tEncoderClear(&encoder);
×
2157
  }
2158
  if (TSDB_CODE_SUCCESS == code) {
×
2159
    *ppMsg = pMsg;
×
2160
  }
2161
  return code;
×
2162
}
2163

2164
static int32_t buildExistSubTalbeRsp(SVnode *pVnode, SSubmitTbData *pSubmitTbData, STableMetaRsp **ppRsp) {
64,923✔
2165
  int32_t code = 0;
64,923✔
2166
  int32_t lino = 0;
64,923✔
2167

2168
  SMetaEntry *pEntry = NULL;
64,923✔
2169
  code = metaFetchEntryByUid(pVnode->pMeta, pSubmitTbData->suid, &pEntry);
64,923✔
2170
  if (code) {
64,923✔
2171
    vError("vgId:%d, table uid:%" PRId64 " not exists, line:%d", TD_VID(pVnode), pSubmitTbData->uid, __LINE__);
×
UNCOV
2172
    TSDB_CHECK_CODE(code, lino, _exit);
×
2173
  }
2174
  if (pEntry->type != TSDB_SUPER_TABLE) {
64,923✔
UNCOV
2175
    vError("vgId:%d, table uid:%" PRId64 " exists, but is not super table, line:%d", TD_VID(pVnode), pSubmitTbData->uid,
×
2176
           __LINE__);
UNCOV
2177
    code = TSDB_CODE_STREAM_INSERT_SCHEMA_NOT_MATCH;
×
UNCOV
2178
    TSDB_CHECK_CODE(code, lino, _exit);
×
2179
  }
2180

2181
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
64,923✔
2182
  if (NULL == *ppRsp) {
64,923✔
UNCOV
2183
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2184
  }
2185
  (*ppRsp)->suid = pSubmitTbData->suid;
64,923✔
2186
  (*ppRsp)->tuid = pSubmitTbData->uid;
64,923✔
2187
  (*ppRsp)->sversion = pEntry->stbEntry.schemaRow.version;
64,923✔
2188
  (*ppRsp)->vgId = pVnode->config.vgId;
64,923✔
2189
  (*ppRsp)->numOfColumns = pEntry->stbEntry.schemaRow.nCols;
64,923✔
2190
  (*ppRsp)->numOfTags = pEntry->stbEntry.schemaTag.nCols;
64,923✔
2191
  (*ppRsp)->pSchemas =
64,923✔
2192
      taosMemoryCalloc(pEntry->stbEntry.schemaRow.nCols + pEntry->stbEntry.schemaTag.nCols, sizeof(SSchema));
64,923✔
2193
  if (NULL == (*ppRsp)->pSchemas) {
64,923✔
UNCOV
2194
    taosMemoryFree(*ppRsp);
×
UNCOV
2195
    *ppRsp = NULL;
×
UNCOV
2196
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2197
  }
2198
  memcpy((*ppRsp)->pSchemas, pEntry->stbEntry.schemaRow.pSchema, pEntry->stbEntry.schemaRow.nCols * sizeof(SSchema));
64,923✔
2199
  memcpy((*ppRsp)->pSchemas + pEntry->stbEntry.schemaRow.nCols, pEntry->stbEntry.schemaTag.pSchema,
64,923✔
2200
         pEntry->stbEntry.schemaTag.nCols * sizeof(SSchema));
64,923✔
2201
  if (pEntry->pExtSchemas != NULL) {
64,923✔
UNCOV
2202
    (*ppRsp)->pSchemaExt = taosMemoryCalloc(pEntry->colCmpr.nCols, sizeof(SExtSchema));
×
UNCOV
2203
    if (NULL == (*ppRsp)->pSchemaExt) {
×
UNCOV
2204
      taosMemoryFree((*ppRsp)->pSchemas);
×
UNCOV
2205
      taosMemoryFree(*ppRsp);
×
UNCOV
2206
      *ppRsp = NULL;
×
UNCOV
2207
      TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2208
    }
2209
    memcpy((*ppRsp)->pSchemaExt, pEntry->pExtSchemas, pEntry->colCmpr.nCols * sizeof(SExtSchema));
×
2210
  }
2211

2212
  if (pEntry->stbEntry.schemaRow.version != pSubmitTbData->sver) {
64,923✔
2213
    TSDB_CHECK_CODE(code = TSDB_CODE_TDB_TABLE_ALREADY_EXIST, lino, _exit);
448✔
2214
  }
2215
_exit:
64,923✔
2216
  metaFetchEntryFree(&pEntry);
64,923✔
2217
  if (code != TSDB_CODE_SUCCESS) {
64,923✔
2218
    vError("vgId:%d, failed to build exist sub table response, code:0x%0x, line:%d", TD_VID(pVnode), code, lino);
448✔
2219
  }
2220
  return code;
64,923✔
2221
}
2222

2223
static int32_t buildExistNormalTalbeRsp(SVnode *pVnode, int64_t uid, STableMetaRsp **ppRsp) {
32,876✔
2224
  int32_t code = 0;
32,876✔
2225
  int32_t lino = 0;
32,876✔
2226

2227
  SMetaEntry *pEntry = NULL;
32,876✔
2228
  code = metaFetchEntryByUid(pVnode->pMeta, uid, &pEntry);
32,876✔
2229
  if (code) {
32,876✔
UNCOV
2230
    vError("vgId:%d, table uid:%" PRId64 " not exists, line:%d", TD_VID(pVnode), uid, __LINE__);
×
UNCOV
2231
    TSDB_CHECK_CODE(code, lino, _exit);
×
2232
  }
2233
  if (pEntry->type != TSDB_NORMAL_TABLE) {
32,876✔
UNCOV
2234
    vError("vgId:%d, table uid:%" PRId64 " exists, but is not normal table, line:%d", TD_VID(pVnode), uid, __LINE__);
×
UNCOV
2235
    code = TSDB_CODE_STREAM_INSERT_SCHEMA_NOT_MATCH;
×
UNCOV
2236
    TSDB_CHECK_CODE(code, lino, _exit);
×
2237
  }
2238

2239
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
32,876✔
2240
  if (NULL == *ppRsp) {
32,876✔
UNCOV
2241
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2242
  }
2243

2244
  (*ppRsp)->tuid = pEntry->uid;
32,876✔
2245
  (*ppRsp)->vgId = pVnode->config.vgId;
32,876✔
2246
  (*ppRsp)->sversion = pEntry->ntbEntry.schemaRow.version;
32,876✔
2247
  (*ppRsp)->numOfColumns = pEntry->ntbEntry.schemaRow.nCols;
32,876✔
2248
  (*ppRsp)->pSchemas = taosMemoryCalloc(pEntry->ntbEntry.schemaRow.nCols, sizeof(SSchema));
32,876✔
2249
  if (NULL == (*ppRsp)->pSchemas) {
32,876✔
2250
    taosMemoryFree(*ppRsp);
×
2251
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2252
  }
2253
  memcpy((*ppRsp)->pSchemas, pEntry->ntbEntry.schemaRow.pSchema, pEntry->ntbEntry.schemaRow.nCols * sizeof(SSchema));
32,876✔
2254
  if (pEntry->pExtSchemas != NULL) {
32,876✔
UNCOV
2255
    (*ppRsp)->pSchemaExt = taosMemoryCalloc(pEntry->ntbEntry.schemaRow.nCols, sizeof(SSchemaExt));
×
2256
    if (NULL == (*ppRsp)->pSchemaExt) {
×
UNCOV
2257
      taosMemoryFree((*ppRsp)->pSchemas);
×
UNCOV
2258
      taosMemoryFree(*ppRsp);
×
UNCOV
2259
      TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2260
    }
UNCOV
2261
    memcpy((*ppRsp)->pSchemaExt, pEntry->pExtSchemas, pEntry->ntbEntry.schemaRow.nCols * sizeof(SSchemaExt));
×
2262
  }
2263

2264
_exit:
32,876✔
2265
  metaFetchEntryFree(&pEntry);
32,876✔
2266
  if (code != TSDB_CODE_SUCCESS) {
32,876✔
UNCOV
2267
    vError("vgId:%d, failed to build exist normal table response, code:%d, line:%d", TD_VID(pVnode), code, lino);
×
2268
  }
2269
  return code;
32,876✔
2270
}
2271

2272
static int32_t buildExistTableInStreamRsp(SVnode *pVnode, SSubmitTbData *pSubmitTbData, STableMetaRsp **ppRsp) {
298,836✔
2273
  if (pSubmitTbData->pCreateTbReq->flags & TD_CREATE_NORMAL_TB_IN_STREAM) {
298,836✔
2274
    int32_t code = buildExistNormalTalbeRsp(pVnode, pSubmitTbData->uid, ppRsp);
32,876✔
2275
    if (code) {
32,876✔
2276
      vError("vgId:%d, table uid:%" PRId64 " not exists, line:%d", TD_VID(pVnode), pSubmitTbData->uid, __LINE__);
×
UNCOV
2277
      return code;
×
2278
    }
2279
    return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
32,876✔
2280
  } else if (pSubmitTbData->pCreateTbReq->flags & TD_CREATE_SUB_TB_IN_STREAM) {
265,960✔
2281
    return buildExistSubTalbeRsp(pVnode, pSubmitTbData, ppRsp);
64,923✔
2282
  }
2283
  return TSDB_CODE_SUCCESS;
201,037✔
2284
}
2285

2286
static int32_t vnodeHandleAutoCreateTable(SVnode      *pVnode,    // vnode
705,016,574✔
2287
                                          int64_t      version,   // version
2288
                                          SSubmitReq2 *pRequest,  // request
2289
                                          SSubmitRsp2 *pResponse  // response
2290
) {
2291
  int32_t code = TSDB_CODE_SUCCESS;
705,016,574✔
2292
  int32_t numTbData = taosArrayGetSize(pRequest->aSubmitTbData);
705,016,574✔
2293
  SArray *newTbUids = NULL;
705,017,564✔
2294

2295
  for (int32_t i = 0; i < numTbData; ++i) {
1,438,656,109✔
2296
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
733,673,548✔
2297

2298
    if (pTbData->pCreateTbReq == NULL) {
733,674,893✔
2299
      continue;
718,584,886✔
2300
    }
2301

2302
    pTbData->uid = pTbData->pCreateTbReq->uid;
15,089,859✔
2303

2304
    // Alloc necessary resources
2305
    if (pResponse->aCreateTbRsp == NULL) {
15,089,859✔
2306
      pResponse->aCreateTbRsp = taosArrayInit(numTbData, sizeof(SVCreateTbRsp));
12,244,605✔
2307
      if (pResponse->aCreateTbRsp == NULL) {
12,244,871✔
UNCOV
2308
        code = terrno;
×
UNCOV
2309
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2310
               tstrerror(code), version);
UNCOV
2311
        taosArrayDestroy(newTbUids);
×
UNCOV
2312
        return code;
×
2313
      }
2314
    }
2315

2316
    // Do create table
2317
    vDebug("vgId:%d start to handle auto create table, version:%" PRId64, TD_VID(pVnode), version);
15,090,315✔
2318

2319
    SVCreateTbRsp *pCreateTbRsp = taosArrayReserve(pResponse->aCreateTbRsp, 1);
15,090,429✔
2320
    code = metaCreateTable2(pVnode->pMeta, version, pTbData->pCreateTbReq, &pCreateTbRsp->pMeta);
15,090,125✔
2321
    if (code == TSDB_CODE_SUCCESS) {
15,090,049✔
2322
      // Allocate necessary resources
2323
      if (newTbUids == NULL) {
14,739,814✔
2324
        newTbUids = taosArrayInit(numTbData, sizeof(int64_t));
11,946,063✔
2325
        if (newTbUids == NULL) {
11,946,063✔
2326
          code = terrno;
×
2327
          vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2328
                 tstrerror(code), version);
UNCOV
2329
          return code;
×
2330
        }
2331
      }
2332

2333
      if (taosArrayPush(newTbUids, &pTbData->uid) == NULL) {
29,478,984✔
UNCOV
2334
        code = terrno;
×
UNCOV
2335
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2336
               tstrerror(code), version);
UNCOV
2337
        taosArrayDestroy(newTbUids);
×
UNCOV
2338
        return code;
×
2339
      }
2340

2341
      if (pCreateTbRsp->pMeta) {
14,739,170✔
2342
        vnodeUpdateMetaRsp(pVnode, pCreateTbRsp->pMeta);
14,739,170✔
2343
      }
2344
    } else if (code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
350,235✔
2345
      code = terrno = 0;
349,189✔
2346
      pTbData->uid = pTbData->pCreateTbReq->uid;  // update uid if table exist for using below
349,189✔
2347

2348
      // stream: get sver from meta, write to pCreateTbRsp, and need to check crateTbReq is same as meta.
2349
      if (i == 0) {
349,189✔
2350
        // In the streaming scenario, multiple grouped req requests will only operate on the same write table, and
2351
        // only the first one needs to be processed.
2352
        code = buildExistTableInStreamRsp(pVnode, pTbData, &pCreateTbRsp->pMeta);
298,836✔
2353
        if (code) {
298,836✔
2354
          vInfo("vgId:%d failed to create table in stream:%s, code(0x%0x):%s", TD_VID(pVnode),
33,324✔
2355
                pTbData->pCreateTbReq->name, code, tstrerror(code));
2356
          taosArrayDestroy(newTbUids);
33,324✔
2357
          return code;
33,324✔
2358
        }
2359
      }
2360
    } else {
2361
      code = terrno;
1,046✔
2362
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
1,046✔
2363
             tstrerror(code), version);
2364
      taosArrayDestroy(newTbUids);
1,046✔
2365
      return code;
1,046✔
2366
    }
2367
  }
2368

2369
  // Update the affected table uid list
2370
  if (taosArrayGetSize(newTbUids) > 0) {
704,982,561✔
2371
    vDebug("vgId:%d, add %d table into query table list in handling submit", TD_VID(pVnode),
11,946,063✔
2372
           (int32_t)taosArrayGetSize(newTbUids));
2373
    if (tqAddTbUidList(pVnode->pTq, newTbUids) != 0) {
11,946,385✔
UNCOV
2374
      vError("vgId:%d, failed to update tbUid list", TD_VID(pVnode));
×
2375
    }
2376
  }
2377

2378
  vDebug("vgId:%d, handle auto create table done, version:%" PRId64, TD_VID(pVnode), version);
704,983,183✔
2379

2380
  taosArrayDestroy(newTbUids);
704,983,213✔
2381
  return code;
704,981,883✔
2382
}
2383

2384
static void addExistTableInfoIntoRes(SVnode *pVnode, SSubmitReq2 *pRequest, SSubmitRsp2 *pResponse,
448✔
2385
                                     SSubmitTbData *pTbData, int32_t numTbData) {
2386
  int32_t code = 0;
448✔
2387
  int32_t lino = 0;
448✔
2388
  if ((pTbData->flags & SUBMIT_REQ_SCHEMA_RES) == 0) {
448✔
2389
    return;
×
2390
  }
2391
  if (pResponse->aCreateTbRsp) {  // If aSubmitTbData is not NULL, it means that the request is a create table request,
448✔
2392
                                  // so table info has exitst and we do not need to add again.
2393
    return;
448✔
2394
  }
UNCOV
2395
  pResponse->aCreateTbRsp = taosArrayInit(numTbData, sizeof(SVCreateTbRsp));
×
UNCOV
2396
  if (pResponse->aCreateTbRsp == NULL) {
×
UNCOV
2397
    code = terrno;
×
UNCOV
2398
    TSDB_CHECK_CODE(code, lino, _exit);
×
2399
  }
UNCOV
2400
  SVCreateTbRsp *pCreateTbRsp = taosArrayReserve(pResponse->aCreateTbRsp, 1);
×
UNCOV
2401
  if (pCreateTbRsp == NULL) {
×
UNCOV
2402
    code = terrno;
×
UNCOV
2403
    TSDB_CHECK_CODE(code, lino, _exit);
×
2404
  }
UNCOV
2405
  if (pTbData->suid == 0) {
×
UNCOV
2406
    code = buildExistNormalTalbeRsp(pVnode, pTbData->uid, &pCreateTbRsp->pMeta);
×
UNCOV
2407
    if (code) {
×
UNCOV
2408
      vError("vgId:%d, table uid:%" PRId64 " not exists, line:%d", TD_VID(pVnode), pTbData->uid, __LINE__);
×
2409
    }
2410
  } else {
2411
    code = buildExistSubTalbeRsp(pVnode, pTbData, &pCreateTbRsp->pMeta);
×
2412
  }
2413

UNCOV
2414
  TSDB_CHECK_CODE(code, lino, _exit);
×
2415
_exit:
×
2416
  if (code != TSDB_CODE_SUCCESS) {
×
2417
    vError("vgId:%d, failed to add exist table info into response, code:0x%0x, line:%d", TD_VID(pVnode), code, lino);
×
2418
  }
UNCOV
2419
  return;
×
2420
}
2421

2422
static int32_t vnodeHandleDataWrite(SVnode *pVnode, int64_t version, SSubmitReq2 *pRequest, SSubmitRsp2 *pResponse) {
704,983,065✔
2423
  int32_t code = TSDB_CODE_SUCCESS;
704,983,065✔
2424
  int32_t numTbData = taosArrayGetSize(pRequest->aSubmitTbData);
704,983,065✔
2425
  int8_t  hasBlob = 0;
704,983,989✔
2426

2427
  // Scan submit data
2428
  for (int32_t i = 0; i < numTbData; ++i) {
1,438,620,264✔
2429
    SMetaInfo      info = {0};
733,639,678✔
2430
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
733,639,787✔
2431

2432
    if (pTbData->flags & SUBMIT_REQ_WITH_BLOB) {
733,639,461✔
2433
      hasBlob = 1;
31,127✔
2434
    }
2435
    if (pTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
733,639,461✔
2436
      continue;  // skip column data format
6,597,228✔
2437
    }
2438
    if (pTbData->flags & SUBMIT_REQ_ONLY_CREATE_TABLE) {
727,042,180✔
2439
      continue;  // skip only crate table request
2,111,318✔
2440
    }
2441

2442
    code = metaGetInfo(pVnode->pMeta, pTbData->uid, &info, NULL);
724,931,289✔
2443
    if (code) {
724,930,165✔
2444
      code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
1,880✔
2445
      vWarn("vgId:%d, error occurred at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __FILE__,
1,880✔
2446
            __LINE__, tstrerror(code), version, pTbData->uid);
2447
      return code;
1,880✔
2448
    }
2449

2450
    if (info.suid != pTbData->suid) {
724,928,285✔
UNCOV
2451
      code = TSDB_CODE_INVALID_MSG;
×
UNCOV
2452
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64 " suid:%" PRId64
×
2453
             " info.suid:%" PRId64,
2454
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), version, pTbData->uid, pTbData->suid,
2455
             info.suid);
UNCOV
2456
      return code;
×
2457
    }
2458

2459
    if (info.suid) {
724,924,052✔
2460
      code = metaGetInfo(pVnode->pMeta, info.suid, &info, NULL);
535,649,819✔
2461
      if (code) {
535,652,287✔
UNCOV
2462
        code = TSDB_CODE_INTERNAL_ERROR;
×
UNCOV
2463
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " suid:%" PRId64, TD_VID(pVnode), __func__,
×
2464
               __FILE__, __LINE__, tstrerror(code), version, info.suid);
UNCOV
2465
        return code;
×
2466
      }
2467
    }
2468

2469
    if (pTbData->sver != info.skmVer) {
724,926,520✔
2470
      code = TSDB_CODE_TDB_INVALID_TABLE_SCHEMA_VER;
448✔
2471
      addExistTableInfoIntoRes(pVnode, pRequest, pResponse, pTbData, numTbData);
448✔
2472
      vDebug("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64
448✔
2473
             " sver:%d"
2474
             " info.skmVer:%d",
2475
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), version, pTbData->uid, pTbData->sver,
2476
             info.skmVer);
2477
      return code;
448✔
2478
    }
2479
  }
2480

2481
  // Do write data
2482
  vDebug("vgId:%d start to handle data write, version:%" PRId64, TD_VID(pVnode), version);
704,980,586✔
2483

2484
  for (int32_t i = 0; i < numTbData; ++i) {
1,438,599,556✔
2485
    int32_t        affectedRows = 0;
733,635,670✔
2486
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
733,634,192✔
2487

2488
    if (pTbData->flags & SUBMIT_REQ_ONLY_CREATE_TABLE) {
733,632,877✔
2489
      continue;
2,111,318✔
2490
    }
2491

2492
    if (hasBlob) {
731,520,993✔
2493
      code = vnodeSubmitBlobData(pVnode, pTbData);
31,127✔
2494
      if (code) {
31,127✔
UNCOV
2495
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2496
               __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
UNCOV
2497
        return code;
×
2498
      }
2499
    }
2500

2501
    code = tsdbInsertTableData(pVnode->pTsdb, version, pTbData, &affectedRows);
731,520,993✔
2502
    if (code) {
731,497,305✔
UNCOV
2503
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2504
             __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
UNCOV
2505
      return code;
×
2506
    }
2507

2508
    code = metaUpdateChangeTimeWithLock(pVnode->pMeta, pTbData->uid, pTbData->ctimeMs);
731,497,305✔
2509
    if (code) {
731,494,674✔
2510
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2511
             __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2512
      return code;
×
2513
    }
2514
    pResponse->affectedRows += affectedRows;
731,494,674✔
2515
  }
2516

2517
  vDebug("vgId:%d, handle data write done, version:%" PRId64 ", affectedRows:%d", TD_VID(pVnode), version,
704,963,886✔
2518
         pResponse->affectedRows);
2519
  return code;
704,981,549✔
2520
}
2521

2522
static int32_t vnodeScanColumnData(SVnode *pVnode, SSubmitTbData *pTbData, TSKEY minKey, TSKEY maxKey) {
6,595,604✔
2523
  int32_t code = 0;
6,595,604✔
2524

2525
  int32_t   numCols = taosArrayGetSize(pTbData->aCol);
6,595,604✔
2526
  SColData *aColData = (SColData *)TARRAY_DATA(pTbData->aCol);
6,594,638✔
2527

2528
  if (numCols <= 0) {
6,595,282✔
UNCOV
2529
    code = TSDB_CODE_INVALID_MSG;
×
UNCOV
2530
    vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " numCols:%d", TD_VID(pVnode), __func__,
×
2531
           __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, numCols);
UNCOV
2532
    return code;
×
2533
  }
2534

2535
  if (aColData[0].cid != PRIMARYKEY_TIMESTAMP_COL_ID || aColData[0].type != TSDB_DATA_TYPE_TIMESTAMP ||
6,595,282✔
2536
      aColData[0].nVal <= 0) {
6,594,953✔
UNCOV
2537
    code = TSDB_CODE_INVALID_MSG;
×
UNCOV
2538
    vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64
×
2539
           " first column is not primary key timestamp, cid:%d type:%d nVal:%d",
2540
           TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, aColData[0].cid,
2541
           aColData[0].type, aColData[0].nVal);
UNCOV
2542
    return code;
×
2543
  }
2544

2545
  for (int32_t i = 1; i < numCols; ++i) {
20,268,924✔
2546
    if (aColData[i].nVal != aColData[0].nVal) {
13,674,302✔
2547
      code = TSDB_CODE_INVALID_MSG;
×
UNCOV
2548
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64
×
2549
             " column cid:%d type:%d nVal:%d is not equal to primary key timestamp nVal:%d",
2550
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid,
2551
             aColData[i].cid, aColData[i].type, aColData[i].nVal, aColData[0].nVal);
2552
      return code;
×
2553
    }
2554
  }
2555

2556
  SRowKey *pLastKey = NULL;
6,594,622✔
2557
  SRowKey  lastKey = {0};
6,594,622✔
2558
  for (int32_t i = 0; i < aColData[0].nVal; ++i) {
2,147,483,647✔
2559
    SRowKey key = {0};
2,147,483,647✔
2560

2561
    tColDataArrGetRowKey(aColData, numCols, i, &key);
2,147,483,647✔
2562

2563
    if (key.ts < minKey || key.ts > maxKey) {
2,147,483,647✔
UNCOV
2564
      code = TSDB_CODE_INVALID_MSG;
×
UNCOV
2565
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] key:%" PRId64
×
2566
             " is out of range [%" PRId64 ", %" PRId64 "]",
2567
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, i, key.ts,
2568
             minKey, maxKey);
UNCOV
2569
      return code;
×
2570
    }
2571

2572
    if (pLastKey && tRowKeyCompare(pLastKey, &key) >= 0) {
2,147,483,647✔
UNCOV
2573
      code = TSDB_CODE_INVALID_MSG;
×
UNCOV
2574
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] key:%" PRId64
×
2575
             " is not in order, lastKey:%" PRId64,
2576
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, i, key.ts,
2577
             pLastKey->ts);
UNCOV
2578
      return code;
×
2579
    } else if (pLastKey == NULL) {
2,147,483,647✔
2580
      pLastKey = &lastKey;
6,591,733✔
2581
    }
2582

2583
    *pLastKey = key;
2,147,483,647✔
2584
  }
2585

2586
  return code;
6,597,228✔
2587
}
2588

2589
static int32_t vnodeScanSubmitRowData(SVnode *pVnode, SSubmitTbData *pTbData, TSKEY minKey, TSKEY maxKey) {
727,056,812✔
2590
  int32_t code = 0;
727,056,812✔
2591

2592
  int32_t numRows = taosArrayGetSize(pTbData->aRowP);
727,056,812✔
2593
  SRow  **aRow = (SRow **)TARRAY_DATA(pTbData->aRowP);
727,061,214✔
2594

2595
  if (numRows <= 0 && (pTbData->flags & SUBMIT_REQ_ONLY_CREATE_TABLE) == 0) {
727,054,640✔
UNCOV
2596
    code = TSDB_CODE_INVALID_MSG;
×
UNCOV
2597
    vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " numRows:%d", TD_VID(pVnode), __func__,
×
2598
           __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, numRows);
UNCOV
2599
    return code;
×
2600
  }
2601

2602
  SRowKey *pLastKey = NULL;
727,054,640✔
2603
  SRowKey  lastKey = {0};
727,054,640✔
2604
  for (int32_t i = 0; i < numRows; ++i) {
2,147,483,647✔
2605
    SRow *pRow = aRow[i];
2,147,483,647✔
2606
    if (pRow->sver != pTbData->sver) {
2,147,483,647✔
UNCOV
2607
      code = TSDB_CODE_INVALID_MSG;
×
UNCOV
2608
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] sver:%d pTbData->sver:%d",
×
2609
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, i, pRow->sver,
2610
             pTbData->sver);
2611
      return code;
×
2612
    }
2613

2614
    SRowKey key = {0};
2,147,483,647✔
2615
    tRowGetKey(pRow, &key);
2,147,483,647✔
2616
    if (key.ts < minKey || key.ts > maxKey) {
2,147,483,647✔
2617
      code = TSDB_CODE_INVALID_MSG;
5,971,906✔
2618
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] key:%" PRId64
5,971,906✔
2619
             " is out of range [%" PRId64 ", %" PRId64 "]",
2620
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, i, key.ts,
2621
             minKey, maxKey);
2622
      return code;
×
2623
    }
2624

2625
    if (pLastKey && tRowKeyCompare(pLastKey, &key) >= 0) {
2,147,483,647✔
2626
      code = TSDB_CODE_INVALID_MSG;
×
UNCOV
2627
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] key:%" PRId64
×
2628
             " is not in order, lastKey:%" PRId64,
2629
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, i, key.ts,
2630
             pLastKey->ts);
UNCOV
2631
      return code;
×
2632
    } else if (pLastKey == NULL) {
2,147,483,647✔
2633
      pLastKey = &lastKey;
724,933,987✔
2634
    }
2635

2636
    *pLastKey = key;
2,147,483,647✔
2637
  }
2638

2639
  return code;
726,100,391✔
2640
}
2641

2642
static int32_t vnodeScanSubmitReq(SVnode *pVnode, int64_t version, SSubmitReq2 *pRequest, SSubmitRsp2 *pResponse) {
705,000,099✔
2643
  int32_t code = TSDB_CODE_SUCCESS;
705,000,099✔
2644
  int32_t numTbData = taosArrayGetSize(pRequest->aSubmitTbData);
705,000,099✔
2645

2646
  TSKEY now = taosGetTimestamp(pVnode->config.tsdbCfg.precision);
705,004,521✔
2647
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * pVnode->config.tsdbCfg.keep2;
705,013,625✔
2648
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
705,010,816✔
2649
  for (int32_t i = 0; i < numTbData; i++) {
1,438,652,723✔
2650
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
733,655,559✔
2651

2652
    if (pTbData->pCreateTbReq && pTbData->pCreateTbReq->uid == 0) {
733,661,007✔
UNCOV
2653
      code = TSDB_CODE_INVALID_MSG;
×
UNCOV
2654
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2655
             tstrerror(code), version);
UNCOV
2656
      return code;
×
2657
    }
2658

2659
    if (pTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
733,652,955✔
2660
      code = vnodeScanColumnData(pVnode, pTbData, minKey, maxKey);
6,595,282✔
2661
      if (code) {
6,597,228✔
UNCOV
2662
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2663
               __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
UNCOV
2664
        return code;
×
2665
      }
2666
    } else {
2667
      code = vnodeScanSubmitRowData(pVnode, pTbData, minKey, maxKey);
727,056,223✔
2668
      if (code) {
727,055,081✔
2669
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
410✔
2670
               __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2671
        return code;
×
2672
      }
2673
    }
2674
  }
2675

2676
  return code;
704,997,164✔
2677
}
2678

2679
static int32_t vnodeDecodeAuditRecord(const SJson *pJson, void *pObj) {
737✔
2680
  SAuditRecord *record = (SAuditRecord *)pObj;
737✔
2681
  int32_t       code = 0;
737✔
2682

2683
  tjsonGetNumberValue(pJson, "timestamp", record->curTime, code);
737✔
2684
  if (code < 0) return TSDB_CODE_INVALID_JSON_FORMAT;
737✔
2685
  code = tjsonGetStringValue2(pJson, "cluster_id", record->strClusterId, TSDB_CLUSTER_ID_LEN);
737✔
2686
  if (code < 0) return TSDB_CODE_INVALID_JSON_FORMAT;
737✔
2687
  code = tjsonGetStringValue2(pJson, "client_add", record->clientAddress, AUDIT_CLIENT_ADD_LEN);
737✔
2688
  if (code < 0) return TSDB_CODE_INVALID_JSON_FORMAT;
737✔
2689
  code = tjsonGetStringValue2(pJson, "user", record->user, TSDB_USER_LEN);
737✔
2690
  if (code < 0) return TSDB_CODE_INVALID_JSON_FORMAT;
737✔
2691
  code = tjsonGetStringValue2(pJson, "operation", record->operation, AUDIT_OPERATION_LEN);
737✔
2692
  if (code < 0) return TSDB_CODE_INVALID_JSON_FORMAT;
737✔
2693
  code = tjsonGetStringValue2(pJson, "db", record->target1, TSDB_DB_NAME_LEN);
737✔
2694
  if (code < 0) return TSDB_CODE_INVALID_JSON_FORMAT;
737✔
2695
  code = tjsonGetStringValue2(pJson, "resource", record->target2, TSDB_STREAM_NAME_LEN);
737✔
2696
  if (code < 0) return TSDB_CODE_INVALID_JSON_FORMAT;
737✔
2697
  record->detail = (char *)tjsonGetStringPointer(pJson, "details");
737✔
2698
  if (record->detail == NULL) return TSDB_CODE_INVALID_JSON_FORMAT;
737✔
2699
  code = tjsonGetDoubleValue(pJson, "duration", &record->duration);
737✔
2700
  if (code < 0) return TSDB_CODE_INVALID_JSON_FORMAT;
737✔
2701
  tjsonGetNumberValue(pJson, "affected_rows", record->affectedRows, code);
737✔
2702
  if (code < 0) return TSDB_CODE_INVALID_JSON_FORMAT;
737✔
2703

2704
  return 0;
737✔
2705
}
2706

2707
static int32_t vnodeBuildCreateTbReq(SVCreateTbReq *pTbReq, const char *tname, STag *pTag, int64_t suid,
67✔
2708
                                     const char *sname, SArray *tagName, uint8_t tagNum, int32_t ttl) {
2709
  int32_t code = TSDB_CODE_SUCCESS;
67✔
2710
  int32_t lino = 0;
67✔
2711

2712
  if (tname == NULL || sname == NULL) {
67✔
NEW
2713
    return TSDB_CODE_INVALID_PARA;
×
2714
  }
2715

2716
  pTbReq->name = taosStrdup(tname);
67✔
2717
  if (!pTbReq->name) {
67✔
NEW
2718
    code = terrno;
×
NEW
2719
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
2720
  }
2721

2722
  pTbReq->ctb.stbName = taosStrdup(sname);
67✔
2723
  if (!pTbReq->ctb.stbName) {
67✔
NEW
2724
    code = terrno;
×
NEW
2725
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
2726
  }
2727

2728
  pTbReq->type = TD_CHILD_TABLE;
67✔
2729
  pTbReq->ctb.suid = suid;
67✔
2730
  pTbReq->ctb.tagNum = tagNum;
67✔
2731
  pTbReq->ttl = ttl;
67✔
2732
  pTbReq->commentLen = -1;
67✔
2733
  pTbReq->ctb.tagName = tagName;
67✔
2734
  pTbReq->ctb.pTag = (uint8_t *)pTag;
67✔
2735

2736
  return code;
67✔
NEW
2737
_exit:
×
NEW
2738
  if (code != 0) {
×
NEW
2739
    if (pTbReq->name != NULL) {
×
NEW
2740
      taosMemoryFreeClear(pTbReq->name);
×
2741
    }
NEW
2742
    if (pTbReq->ctb.stbName != NULL) {
×
NEW
2743
      taosMemoryFreeClear(pTbReq->ctb.stbName);
×
2744
    }
NEW
2745
    vError("failed to build create tb at %d since %s", lino, tstrerror(code));
×
2746
  }
NEW
2747
  return code;
×
2748
}
2749

2750
static int32_t vnodePrepareCreateTb(SVCreateTbReq *pTbReq, char *tbName, int64_t suid, SSchemaWrapper *pTagSchema,
67✔
2751
                                    SAuditRecord *record) {
2752
  int32_t code = 0;
67✔
2753
  int32_t lino = 0;
67✔
2754

2755
  SArray *TagNames = NULL;
67✔
2756
  SArray *pTagVals = NULL;
67✔
2757
  STag   *pTag = NULL;
67✔
2758

2759
  SSchema *tSchema = &pTagSchema->pSchema[0];
67✔
2760
  vTrace("schema name:%s", tSchema->name);
67✔
2761

2762
  TagNames = taosArrayInit(1, TSDB_COL_NAME_LEN);
67✔
2763
  if (!TagNames) {
67✔
NEW
2764
    code = terrno;
×
NEW
2765
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
2766
  }
2767

2768
  if (NULL == taosArrayPush(TagNames, tSchema->name)) {
134✔
NEW
2769
    code = terrno;
×
NEW
2770
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
2771
  }
2772

2773
  STagVal tv = (STagVal){.cid = tSchema->colId,
67✔
2774
                         .type = tSchema->type,
67✔
2775
                         .nData = strlen(record->strClusterId),
67✔
2776
                         .pData = record->strClusterId};  // address copy, no value
67✔
2777
  if ((pTagVals = taosArrayInit(1, sizeof(STagVal))) == NULL) {
67✔
NEW
2778
    code = terrno;
×
NEW
2779
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
2780
  }
2781
  if (NULL == taosArrayPush(pTagVals, &tv)) {
67✔
NEW
2782
    code = terrno;
×
NEW
2783
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
2784
  }
2785

2786
  // version hardcode to 1 according to other module
2787
  code = tTagNew(pTagVals, 1, false, &pTag);
67✔
2788
  if (code != TSDB_CODE_SUCCESS) {
67✔
NEW
2789
    vError("failed to create tag, error:%s", tstrerror(code));
×
NEW
2790
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
2791
  }
2792

2793
  code = vnodeBuildCreateTbReq(pTbReq, tbName, pTag, suid, AUDIT_STABLE_NAME, TagNames, pTagSchema->nCols,
67✔
2794
                               TSDB_DEFAULT_TABLE_TTL);
2795
_exit:
67✔
2796
  if (code != 0) {
67✔
NEW
2797
    vError("failed to prepare create tb at %d since %s", lino, tstrerror(code));
×
NEW
2798
    if (TagNames != NULL) {
×
NEW
2799
      taosArrayDestroy(TagNames);
×
NEW
2800
      TagNames = NULL;
×
NEW
2801
      pTbReq->ctb.tagName = NULL;
×
2802
    }
NEW
2803
    if (pTag != NULL) {
×
NEW
2804
      tTagFree(pTag);
×
NEW
2805
      pTag = NULL;
×
NEW
2806
      pTbReq->ctb.pTag = NULL;
×
2807
    }
2808
  }
2809

2810
  if (pTagVals != NULL) {
67✔
2811
    taosArrayDestroy(pTagVals);
67✔
2812
    pTagVals = NULL;
67✔
2813
  }
2814

2815
  return code;
67✔
2816
}
2817

2818
static SArray *vnodePrepareRow(SVnode *pVnode, STSchema *pSchema, SAuditRecord *record) {
737✔
2819
  int32_t code = 0;
737✔
2820
  int32_t lino = 0;
737✔
2821

2822
  SArray *aRows = NULL;
737✔
2823
  SArray *pVals = NULL;
737✔
2824
  SRow   *pRow = NULL;
737✔
2825

2826
  if (!(aRows = taosArrayInit(1, sizeof(SRow *)))) {
737✔
NEW
2827
    code = terrno;
×
NEW
2828
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
2829
  }
2830

2831
  if ((pVals = taosArrayInit(1, sizeof(SColVal))) == NULL) {
737✔
NEW
2832
    code = terrno;
×
NEW
2833
    goto _exit;
×
2834
  }
2835

2836
  for (int32_t k = 0; k < pSchema->numOfCols; ++k) {
7,370✔
2837
    const STColumn *pCol = &pSchema->columns[k];
6,633✔
2838
    vTrace("vgId:%d, schema column id:%d, type:%d", TD_VID(pVnode), pCol->colId, pCol->type);
6,633✔
2839

2840
    // colId is consistent with audit_columns in mndStb.c
2841
    void *data = NULL;
6,633✔
2842
    if (pCol->colId == 1) {
6,633✔
2843
      data = &record->curTime;
737✔
2844
    } else if (pCol->colId == 2) {
5,896✔
2845
      data = record->detail;
737✔
2846
    } else if (pCol->colId == 3) {
5,159✔
2847
      data = record->user;
737✔
2848
    } else if (pCol->colId == 4) {
4,422✔
2849
      data = record->operation;
737✔
2850
    } else if (pCol->colId == 5) {
3,685✔
2851
      data = record->target1;
737✔
2852
    } else if (pCol->colId == 6) {
2,948✔
2853
      data = record->target2;
737✔
2854
    } else if (pCol->colId == 7) {
2,211✔
2855
      data = record->clientAddress;
737✔
2856
    } else if (pCol->colId == 8) {
1,474✔
2857
      data = &record->duration;
737✔
2858
    } else if (pCol->colId == 9) {
737✔
2859
      data = &record->affectedRows;
737✔
2860
    } else {
NEW
2861
      vError("the column id %" PRIi16 " is not defined in audit record table", pCol->colId);
×
NEW
2862
      code = TSDB_CODE_APP_ERROR;
×
NEW
2863
      TAOS_CHECK_GOTO(code, &lino, _exit);
×
2864
    }
2865

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

2913
            SColVal cv = COL_VAL_NULL(pCol->colId, pCol->type);  // should use pCol->type
2914
            if (NULL == taosArrayPush(pVals, &cv)) {
2915
              goto _end;
2916
            }
2917
          } else {
2918
           */
2919
        SValue sv = {0};
2,211✔
2920
        sv.type = pCol->type;
2,211✔
2921
        valueSetDatum(&sv, sv.type, data, pCol->bytes);
2,211✔
2922
        SColVal cv = COL_VAL_VALUE(pCol->colId, sv);
2,211✔
2923
        if (NULL == taosArrayPush(pVals, &cv)) {
2,211✔
NEW
2924
          code = terrno;
×
NEW
2925
          TAOS_CHECK_GOTO(code, &lino, _exit);
×
2926
        }
2927
        //}
2928
        break;
2,211✔
2929
      }
2930
    }
2931
  }
2932

2933
  SRowBuildScanInfo sinfo = {0};
737✔
2934
  if ((code = tRowBuild(pVals, pSchema, &pRow, &sinfo)) < 0) {
737✔
NEW
2935
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
2936
  }
2937
  if (NULL == taosArrayPush(aRows, &pRow)) {
737✔
NEW
2938
    code = terrno;
×
NEW
2939
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
2940
  }
2941
  taosArrayDestroy(pVals);
737✔
2942
  pVals = NULL;
737✔
2943

2944
_exit:
737✔
2945
  if (code != 0) {
737✔
NEW
2946
    vError("vgId:%d, failed to prepare row at:%d since %s", TD_VID(pVnode), lino, tstrerror(code));
×
NEW
2947
    taosArrayDestroy(pVals);
×
NEW
2948
    tRowDestroy(pRow);
×
NEW
2949
    taosArrayDestroy(aRows);
×
NEW
2950
    terrno = code;
×
NEW
2951
    aRows = NULL;
×
2952
  }
2953
  return aRows;
737✔
2954
}
2955

2956
static SArray *vnodePrepareSubmitTb(SVnode *pVnode, SAuditRecord *record, STSchema *pSchema, SSchemaWrapper *pTagSchema,
737✔
2957
                                    int64_t suid) {
2958
  int32_t code = 0;
737✔
2959
  int32_t lino = 0;
737✔
2960

2961
  SArray *aSubmitTbData = NULL;
737✔
2962
  SSubmitTbData tbData = {0};
737✔
2963

2964
  if (!(aSubmitTbData = taosArrayInit(1, sizeof(SSubmitTbData)))) {
737✔
NEW
2965
    code = terrno;
×
NEW
2966
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
2967
  }
2968

2969
  char tbName[TSDB_TABLE_NAME_LEN] = {0};
737✔
2970
  (void)tsnprintf(tbName, TSDB_TABLE_NAME_LEN, "t_operations_%s", record->strClusterId);
737✔
2971

2972
  SMetaReader merTb = {0};
737✔
2973
  metaReaderDoInit(&merTb, pVnode->pMeta, META_READER_LOCK);
737✔
2974
  if ((code = metaGetTableEntryByName(&merTb, tbName)) == 0) {
737✔
2975
    vTrace("vgId:%d, get table entry, table:%s uid:%" PRId64 ", suid:% " PRId64 ", version:%" PRId64 ", api:%p",
670✔
2976
           TD_VID(pVnode), tbName, merTb.me.uid, merTb.me.ctbEntry.suid, merTb.me.version, merTb.pAPI);
2977
    tbData.uid = merTb.me.uid;
670✔
2978
    metaReaderClear(&merTb);
670✔
2979
  } else if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST) {
67✔
2980
    vTrace("vgId:%d, %s table not exist", TD_VID(pVnode), tbName);
67✔
2981

2982
    tbData.uid = tGenIdPI64();
67✔
2983
    tbData.flags |= SUBMIT_REQ_AUTO_CREATE_TABLE;
67✔
2984

2985
    tbData.pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
67✔
2986

2987
    if (tbData.pCreateTbReq == NULL) {
67✔
NEW
2988
      code = terrno;
×
NEW
2989
      TAOS_CHECK_GOTO(code, &lino, _exit);
×
2990
    }
2991

2992
    tbData.pCreateTbReq->uid = tbData.uid;
67✔
2993
    tbData.pCreateTbReq->btime = taosGetTimestampMs();
134✔
2994

2995
    code = vnodePrepareCreateTb(tbData.pCreateTbReq, tbName, suid, pTagSchema, record);
67✔
2996

2997
    metaReaderClear(&merTb);
67✔
2998
    TAOS_CHECK_GOTO(code, &lino, _exit);
67✔
2999
  } else {
NEW
3000
    metaReaderClear(&merTb);
×
NEW
3001
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
3002
  }
3003

3004
  tbData.suid = suid;
737✔
3005
  tbData.sver = pSchema->version;
737✔
3006

3007
  tbData.aRowP = vnodePrepareRow(pVnode, pSchema, record);
737✔
3008
  if (tbData.aRowP == NULL) {
737✔
NEW
3009
    code = terrno;
×
NEW
3010
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
3011
  }
3012

3013
  if (NULL == taosArrayPush(aSubmitTbData, &tbData)) {
737✔
NEW
3014
    code = terrno;
×
NEW
3015
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
3016
  }
3017

3018
_exit:
737✔
3019
  if (code != 0) {
737✔
NEW
3020
    vError("vgId:%d, failed to prepare submitTb at:%d since %s", TD_VID(pVnode), lino, tstrerror(code));
×
NEW
3021
    tDestroySubmitTbData(&tbData, TSDB_MSG_FLG_ENCODE);
×
NEW
3022
    if (aSubmitTbData != NULL) {
×
NEW
3023
      taosArrayDestroy(aSubmitTbData);
×
3024
    }
NEW
3025
    terrno = code;
×
NEW
3026
    aSubmitTbData = NULL;
×
3027
  }
3028
  return aSubmitTbData;
737✔
3029
}
3030

3031
static int32_t vnodeSaveOneAuditRecord(SVnode *pVnode, int64_t ver, SJson *pJson, SSchemaWrapper *pTagSchema,
737✔
3032
                                       int64_t suid, STSchema *pSchema, SRpcMsg *pOriginalMsg) {
3033
  int32_t code = 0;
737✔
3034
  int32_t lino = 0;
737✔
3035
  terrno = 0;
737✔
3036

3037
  SAuditRecord record = {0};
737✔
3038
  SSubmitReq2 *pSubmitReq = NULL;
737✔
3039
  SSubmitRsp2 *pSubmitRsp = &(SSubmitRsp2){0};
737✔
3040

3041
  TAOS_CHECK_GOTO(vnodeDecodeAuditRecord(pJson, &record), &lino, _exit);
737✔
3042

3043
  vTrace("vgId:%d, start to audit operation:%s", TD_VID(pVnode), record.operation);
737✔
3044

3045
  if (!(pSubmitReq = taosMemoryCalloc(1, sizeof(SSubmitReq2)))) {
737✔
NEW
3046
    code = terrno;
×
NEW
3047
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
3048
  }
3049

3050
  pSubmitReq->aSubmitTbData = vnodePrepareSubmitTb(pVnode, &record, pSchema, pTagSchema, suid);
737✔
3051
  if (pSubmitReq->aSubmitTbData == NULL) {
737✔
NEW
3052
    code = terrno;
×
NEW
3053
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
3054
  }
3055

3056
  vTrace("vgId:%d, going to create table", TD_VID(pVnode));
737✔
3057
  code = vnodeHandleAutoCreateTable(pVnode, ver, pSubmitReq, pSubmitRsp);
737✔
3058
  if (code) {
737✔
NEW
3059
    vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
3060
           tstrerror(code), ver);
NEW
3061
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
3062
  }
3063

3064
  vTrace("vgId:%d, going to data write", TD_VID(pVnode));
737✔
3065
  code = vnodeHandleDataWrite(pVnode, ver, pSubmitReq, pSubmitRsp);
737✔
3066
  if (code) {
737✔
NEW
3067
    vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
3068
           tstrerror(code), ver);
NEW
3069
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
3070
  }
3071

3072
  vTrace("vgId:%d, affectedRow:%d", TD_VID(pVnode), pSubmitRsp->affectedRows);
737✔
3073

3074
_exit:
737✔
3075
  if (code != 0)
737✔
NEW
3076
    vError("vgId:%d, failed to save one AuditRecord at line:%d, since %s", TD_VID(pVnode), lino, tstrerror(code));
×
3077

3078
  // update statistics
3079
  (void)atomic_add_fetch_64(&pVnode->statis.nInsert, pSubmitRsp->affectedRows);
737✔
3080
  (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsert, 1);
737✔
3081
  if (code == 0) {
737✔
3082
    (void)atomic_add_fetch_64(&pVnode->statis.nInsertSuccess, pSubmitRsp->affectedRows);
737✔
3083
    (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
737✔
3084
  }
3085

3086
  // update metrics
3087
  METRICS_UPDATE(pVnode->writeMetrics.total_requests, METRIC_LEVEL_LOW, 1);
737✔
3088
  METRICS_UPDATE(pVnode->writeMetrics.total_rows, METRIC_LEVEL_HIGH, pSubmitRsp->affectedRows);
737✔
3089
  // METRICS_UPDATE(pVnode->writeMetrics.total_bytes, METRIC_LEVEL_LOW, pMsg->header.contLen);
3090

3091
  if (code == 0 && tsEnableMonitor && tsMonitorFqdn[0] != 0 && tsMonitorPort != 0 && pSubmitRsp->affectedRows > 0 &&
737✔
NEW
3092
      strlen(RPC_MSG_USER(pOriginalMsg)) > 0 && tsInsertCounter != NULL) {
×
NEW
3093
    const char *sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT_AFFECTED_ROWS,
×
NEW
3094
                                   pVnode->monitor.strClusterId,
×
NEW
3095
                                   pVnode->monitor.strDnodeId,
×
3096
                                   tsLocalEp,
NEW
3097
                                   pVnode->monitor.strVgId,
×
NEW
3098
                                   RPC_MSG_USER(pOriginalMsg),
×
3099
                                   "Success"};
NEW
3100
    int         tv = taos_counter_add(tsInsertCounter, pSubmitRsp->affectedRows, sample_labels);
×
NEW
3101
    if (tv != 0) vError("vgId:%d, failed to taos counter add since return is %d", TD_VID(pVnode), tv);
×
3102
  }
3103

3104
  // clear
3105
  if (pSubmitReq != NULL) {
737✔
3106
    tDestroySubmitReq(pSubmitReq, TSDB_MSG_FLG_ENCODE);
737✔
3107
    taosMemoryFree(pSubmitReq);
737✔
3108
  }
3109
  if (pSubmitRsp != NULL) {
737✔
3110
    tDestroySSubmitRsp2(pSubmitRsp, TSDB_MSG_FLG_ENCODE);
737✔
3111
  }
3112

3113
  return code;
737✔
3114
}
3115

3116
static int32_t vnodeProcessAuditRecordReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pOriginalMsg) {
268✔
3117
  int32_t code = 0;
268✔
3118
  int32_t lino = 0;
268✔
3119
  terrno = 0;
268✔
3120

3121
  SVAuditRecordReq req = {0};
268✔
3122
  if (tDeserializeSVAuditRecordReq(pReq, len, &req) != 0) {
268✔
NEW
3123
    vError("vgId:%d, failed to deserialize SVAuditRecordReq", TD_VID(pVnode));
×
NEW
3124
    code = TSDB_CODE_INVALID_MSG;
×
NEW
3125
    return code;
×
3126
  }
3127

3128
  vTrace("vgId:%d, start to process AuditRecord Req", TD_VID(pVnode));
268✔
3129

3130
  SJson          *pJson = NULL;
268✔
3131
  STSchema       *pSchema = NULL;
268✔
3132
  SSchemaWrapper *pTagSchema = NULL;
268✔
3133
  int64_t         suid = 0;
268✔
3134

3135
  pJson = tjsonParse(req.data);
268✔
3136
  if (pJson == NULL) {
268✔
NEW
3137
    code = TSDB_CODE_INVALID_JSON_FORMAT;
×
NEW
3138
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
3139
  }
3140

3141
  SMetaReader merStb = {0};
268✔
3142
  metaReaderDoInit(&merStb, pVnode->pMeta, META_READER_LOCK);
268✔
3143
  code = metaGetTableEntryByName(&merStb, AUDIT_STABLE_NAME);
268✔
3144
  if (code != 0) {
268✔
NEW
3145
    metaReaderClear(&merStb);
×
NEW
3146
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
3147
  }
3148
  vTrace("vgId:%d, get audit stable entry, uid:%" PRId64 ", suid:% " PRId64 ", version:%" PRId64 ", api:%p",
268✔
3149
         TD_VID(pVnode), merStb.me.uid, merStb.me.ctbEntry.suid, merStb.me.version, merStb.pAPI);
3150

3151
  code = vnodeGetTableSchema(pVnode, merStb.me.uid, &pSchema, &suid, &pTagSchema);
268✔
3152
  if (code != 0) {
268✔
NEW
3153
    metaReaderClear(&merStb);
×
NEW
3154
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
3155
  }
3156
  vTrace("vgId:%d, get audit stable schema, version:%d, suid:%" PRId64, TD_VID(pVnode), pSchema->version, suid);
268✔
3157
  metaReaderClear(&merStb);
268✔
3158
  // pSchema pTagSchema can be cached in the future
3159

3160
  SJson *pRecords = tjsonGetObjectItem(pJson, "records");
268✔
3161
  if (pRecords == NULL) {
268✔
3162
    TAOS_CHECK_GOTO(vnodeSaveOneAuditRecord(pVnode, ver, pJson, pTagSchema, suid, pSchema, pOriginalMsg), &lino, _exit);
134✔
3163
  } else {
3164
    int32_t size = tjsonGetArraySize(pRecords);
134✔
3165
    vTrace("%d items in records", size);
134✔
3166
    for (int32_t i = 0; i < size; ++i) {
737✔
3167
      SJson *pRecord = tjsonGetArrayItem(pRecords, i);
603✔
3168
      if (pRecord == NULL) {
603✔
NEW
3169
        vError("vgId:%d, failed to get array item %d", TD_VID(pVnode), i);
×
NEW
3170
        code = TSDB_CODE_INVALID_JSON_FORMAT;
×
NEW
3171
        TAOS_CHECK_GOTO(code, &lino, _exit);
×
3172
      }
3173
      TAOS_CHECK_GOTO(vnodeSaveOneAuditRecord(pVnode, ver, pRecord, pTagSchema, suid, pSchema, pOriginalMsg), &lino,
603✔
3174
                      _exit);
3175
    }
3176
  }
3177

3178
_exit:
268✔
3179
  if (code != 0)
268✔
NEW
3180
    vError("vgId:%d, failed to process AuditRecordReq at line:%d, since %s", TD_VID(pVnode), lino, tstrerror(code));
×
3181

3182
  // clear
3183
  if (pSchema) taosMemoryFree(pSchema);
268✔
3184
  if (pTagSchema) {
268✔
3185
    taosMemoryFreeClear(pTagSchema->pSchema);
268✔
3186
    taosMemoryFree(pTagSchema);
268✔
3187
  }
3188
  if (pJson != NULL) {
268✔
3189
    cJSON_Delete(pJson);
268✔
3190
    pJson = NULL;
268✔
3191
  }
3192
  tFreeSVAuditRecordReq(&req);
268✔
3193

3194
  return code;
268✔
3195
}
3196

3197
static int32_t vnodeProcessSubmitReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
705,014,649✔
3198
                                     SRpcMsg *pOriginalMsg) {
3199
  int32_t code = 0;
705,014,649✔
3200
  int32_t lino = 0;
705,014,649✔
3201
  terrno = 0;
705,014,649✔
3202

3203
  SSubmitReq2 *pSubmitReq = &(SSubmitReq2){0};
705,016,846✔
3204
  SSubmitRsp2 *pSubmitRsp = &(SSubmitRsp2){0};
705,016,994✔
3205
  int32_t      ret;
3206
  SEncoder     ec = {0};
705,015,879✔
3207

3208
  pRsp->code = TSDB_CODE_SUCCESS;
705,015,140✔
3209

3210
  void           *pAllocMsg = NULL;
705,015,929✔
3211
  SSubmitReq2Msg *pMsg = (SSubmitReq2Msg *)pReq;
705,015,929✔
3212
  SDecoder        dc = {0};
705,015,929✔
3213
  if (0 == taosHton64(pMsg->version)) {
705,014,962✔
UNCOV
3214
    code = vnodeSubmitReqConvertToSubmitReq2(pVnode, (SSubmitReq *)pMsg, pSubmitReq);
×
UNCOV
3215
    if (TSDB_CODE_SUCCESS == code) {
×
UNCOV
3216
      code = vnodeRebuildSubmitReqMsg(pSubmitReq, &pReq);
×
3217
    }
UNCOV
3218
    if (TSDB_CODE_SUCCESS == code) {
×
UNCOV
3219
      pAllocMsg = pReq;
×
3220
    }
UNCOV
3221
    TSDB_CHECK_CODE(code, lino, _exit);
×
3222
  } else {
3223
    // decode
3224
    pReq = POINTER_SHIFT(pReq, sizeof(SSubmitReq2Msg));
705,016,051✔
3225
    len -= sizeof(SSubmitReq2Msg);
705,016,618✔
3226

3227
    tDecoderInit(&dc, pReq, len);
705,016,618✔
3228
    if (tDecodeSubmitReq(&dc, pSubmitReq, NULL) < 0) {
705,014,844✔
UNCOV
3229
      code = TSDB_CODE_INVALID_MSG;
×
UNCOV
3230
      TSDB_CHECK_CODE(code, lino, _exit);
×
3231
    }
3232
  }
3233

3234
  // Scan the request
3235
  code = vnodeScanSubmitReq(pVnode, ver, pSubmitReq, pSubmitRsp);
705,003,406✔
3236
  if (code) {
704,998,226✔
UNCOV
3237
    vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
3238
           tstrerror(code), ver);
UNCOV
3239
    TSDB_CHECK_CODE(code, lino, _exit);
×
3240
  }
3241

3242
  vGDebug(pOriginalMsg ? &pOriginalMsg->info.traceId : NULL, "vgId:%d, index:%" PRId64 ", submit block, rows:%d",
704,998,226✔
3243
          TD_VID(pVnode), ver, (int32_t)taosArrayGetSize(pSubmitReq->aSubmitTbData));
3244

3245
  // Handle auto create table
3246
  code = vnodeHandleAutoCreateTable(pVnode, ver, pSubmitReq, pSubmitRsp);
705,020,018✔
3247
  if (code) {
705,016,846✔
3248
    vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
34,370✔
3249
           tstrerror(code), ver);
3250
    TSDB_CHECK_CODE(code, lino, _exit);
34,370✔
3251
  }
3252

3253
  // Handle data write
3254
  code = vnodeHandleDataWrite(pVnode, ver, pSubmitReq, pSubmitRsp);
704,982,476✔
3255
  if (code) {
704,982,273✔
3256
    vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
2,328✔
3257
           tstrerror(code), ver);
3258
    TSDB_CHECK_CODE(code, lino, _exit);
2,328✔
3259
  }
3260

3261
_exit:
705,016,499✔
3262
  // message
3263
  pRsp->code = code;
705,017,510✔
3264
  tEncodeSize(tEncodeSSubmitRsp2, pSubmitRsp, pRsp->contLen, ret);
705,017,406✔
3265
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
705,014,556✔
3266
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
705,015,707✔
3267
  if (tEncodeSSubmitRsp2(&ec, pSubmitRsp) < 0) {
705,015,253✔
UNCOV
3268
    vError("vgId:%d, failed to encode submit response", TD_VID(pVnode));
×
3269
  }
3270
  tEncoderClear(&ec);
705,016,708✔
3271

3272
  // update statistics
3273
  (void)atomic_add_fetch_64(&pVnode->statis.nInsert, pSubmitRsp->affectedRows);
705,013,005✔
3274
  (void)atomic_add_fetch_64(&pVnode->statis.nInsertSuccess, pSubmitRsp->affectedRows);
705,016,964✔
3275
  (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsert, 1);
705,016,207✔
3276

3277
  // update metrics
3278
  METRICS_UPDATE(pVnode->writeMetrics.total_requests, METRIC_LEVEL_LOW, 1);
705,014,587✔
3279
  METRICS_UPDATE(pVnode->writeMetrics.total_rows, METRIC_LEVEL_HIGH, pSubmitRsp->affectedRows);
705,010,101✔
3280
  METRICS_UPDATE(pVnode->writeMetrics.total_bytes, METRIC_LEVEL_LOW, pMsg->header.contLen);
705,008,295✔
3281

3282
  if (tsEnableMonitor && tsMonitorFqdn[0] != 0 && tsMonitorPort != 0 && pSubmitRsp->affectedRows > 0 &&
705,009,562✔
UNCOV
3283
      strlen(RPC_MSG_USER(pOriginalMsg)) > 0 && tsInsertCounter != NULL) {
×
UNCOV
3284
    const char *sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT_AFFECTED_ROWS,
×
UNCOV
3285
                                   pVnode->monitor.strClusterId,
×
UNCOV
3286
                                   pVnode->monitor.strDnodeId,
×
3287
                                   tsLocalEp,
UNCOV
3288
                                   pVnode->monitor.strVgId,
×
UNCOV
3289
                                   RPC_MSG_USER(pOriginalMsg),
×
3290
                                   "Success"};
UNCOV
3291
    int         tv = taos_counter_add(tsInsertCounter, pSubmitRsp->affectedRows, sample_labels);
×
NEW
3292
    if (tv != 0) vError("vgId:%d, failed to taos counter add since return is %d", TD_VID(pVnode), tv);
×
3293
  }
3294

3295
  if (code == 0) {
705,009,562✔
3296
    (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
704,973,402✔
3297
  }
3298

3299
  // clear
3300
  tDestroySubmitReq(pSubmitReq, 0 == taosHton64(pMsg->version) ? TSDB_MSG_FLG_CMPT : TSDB_MSG_FLG_DECODE);
705,013,423✔
3301
  tDestroySSubmitRsp2(pSubmitRsp, TSDB_MSG_FLG_ENCODE);
705,008,754✔
3302

3303
  if (code) {
704,997,815✔
3304
    terrno = code;
36,698✔
3305
    if (code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
36,698✔
3306
      vInfo("vgId:%d, failed to process submit request since %s, lino:%d, version:%" PRId64, TD_VID(pVnode),
33,324✔
3307
            tstrerror(code), lino, ver);
3308

3309
    } else {
3310
      vError("vgId:%d, failed to process submit request since %s, lino:%d, version:%" PRId64, TD_VID(pVnode),
3,374✔
3311
             tstrerror(code), lino, ver);
3312
    }
3313
  }
3314

3315
  taosMemoryFree(pAllocMsg);
705,005,167✔
3316
  tDecoderClear(&dc);
705,003,137✔
3317

3318
  return code;
705,014,445✔
3319
}
3320

3321
static int32_t vnodeConsolidateAlterHashRange(SVnode *pVnode, int64_t ver) {
24,530✔
3322
  int32_t code = TSDB_CODE_SUCCESS;
24,530✔
3323

3324
  vInfo("vgId:%d, trim meta of tables per hash range [%" PRIu32 ", %" PRIu32 "]. apply-index:%" PRId64, TD_VID(pVnode),
24,530✔
3325
        pVnode->config.hashBegin, pVnode->config.hashEnd, ver);
3326

3327
  // TODO: trim meta of tables from TDB per hash range [pVnode->config.hashBegin, pVnode->config.hashEnd]
3328
  code = metaTrimTables(pVnode->pMeta, ver);
24,530✔
3329

3330
  return code;
24,530✔
3331
}
3332

3333
static int32_t vnodeProcessAlterConfirmReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
3,859,510✔
3334
  vInfo("vgId:%d, vnode handle msgType:alter-confirm, alter confirm msg is processed", TD_VID(pVnode));
3,859,510✔
3335
  int32_t code = TSDB_CODE_SUCCESS;
3,859,510✔
3336
  if (!pVnode->config.hashChange) {
3,859,510✔
3337
    goto _exit;
3,834,980✔
3338
  }
3339

3340
  code = vnodeConsolidateAlterHashRange(pVnode, ver);
24,530✔
3341
  if (code < 0) {
24,530✔
UNCOV
3342
    vError("vgId:%d, failed to consolidate alter hashrange since %s. version:%" PRId64, TD_VID(pVnode), terrstr(), ver);
×
UNCOV
3343
    goto _exit;
×
3344
  }
3345
  pVnode->config.hashChange = false;
24,530✔
3346

3347
_exit:
3,859,510✔
3348
  pRsp->msgType = TDMT_VND_ALTER_CONFIRM_RSP;
3,859,510✔
3349
  pRsp->code = code;
3,859,510✔
3350
  pRsp->pCont = NULL;
3,859,510✔
3351
  pRsp->contLen = 0;
3,859,510✔
3352

3353
  return code;
3,859,510✔
3354
}
3355

3356
extern int32_t tsdbDisableAndCancelAllBgTask(STsdb *pTsdb);
3357
extern void    tsdbEnableBgTask(STsdb *pTsdb);
3358

3359
static int32_t vnodeProcessAlterConfigReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
321,183✔
3360
  bool walChanged = false;
321,183✔
3361
  bool tsdbChanged = false;
321,183✔
3362

3363
  SAlterVnodeConfigReq req = {0};
321,183✔
3364
  if (tDeserializeSAlterVnodeConfigReq(pReq, len, &req) != 0) {
321,183✔
UNCOV
3365
    terrno = TSDB_CODE_INVALID_MSG;
×
3366
    return TSDB_CODE_INVALID_MSG;
×
3367
  }
3368

3369
  vInfo("vgId:%d, start to alter vnode config, page:%d pageSize:%d buffer:%d szPage:%d szBuf:%" PRIu64
321,183✔
3370
        " cacheLast:%d cacheLastSize:%d days:%d keep0:%d keep1:%d keep2:%d keepTimeOffset:%d ssKeepLocal:%d "
3371
        "ssCompact:%d allowDrop:%d fsync:%d level:%d "
3372
        "walRetentionPeriod:%d walRetentionSize:%d",
3373
        TD_VID(pVnode), req.pages, req.pageSize, req.buffer, req.pageSize * 1024, (uint64_t)req.buffer * 1024 * 1024,
3374
        req.cacheLast, req.cacheLastSize, req.daysPerFile, req.daysToKeep0, req.daysToKeep1, req.daysToKeep2,
3375
        req.keepTimeOffset, req.ssKeepLocal, req.ssCompact, req.allowDrop, req.walFsyncPeriod, req.walLevel,
3376
        req.walRetentionPeriod, req.walRetentionSize);
3377

3378
  if (pVnode->config.cacheLastSize != req.cacheLastSize) {
321,183✔
3379
    pVnode->config.cacheLastSize = req.cacheLastSize;
5,991✔
3380
    tsdbCacheSetCapacity(pVnode, (size_t)pVnode->config.cacheLastSize * 1024 * 1024);
5,991✔
3381
  }
3382

3383
  if (pVnode->config.szBuf != req.buffer * 1024LL * 1024LL) {
321,183✔
3384
    vInfo("vgId:%d, vnode buffer is changed from %" PRId64 " to %" PRId64, TD_VID(pVnode), pVnode->config.szBuf,
9,414✔
3385
          (uint64_t)(req.buffer * 1024LL * 1024LL));
3386
    pVnode->config.szBuf = req.buffer * 1024LL * 1024LL;
9,414✔
3387
  }
3388

3389
  if (pVnode->config.szCache != req.pages) {
321,183✔
3390
    if ((terrno = metaAlterCache(pVnode->pMeta, req.pages)) < 0) {
4,602✔
UNCOV
3391
      vError("vgId:%d, failed to change vnode pages from %d to %d failed since %s", TD_VID(pVnode),
×
3392
             pVnode->config.szCache, req.pages, tstrerror(terrno));
UNCOV
3393
      return terrno;
×
3394
    } else {
3395
      vInfo("vgId:%d, vnode pages is changed from %d to %d", TD_VID(pVnode), pVnode->config.szCache, req.pages);
4,602✔
3396
      pVnode->config.szCache = req.pages;
4,602✔
3397
    }
3398
  }
3399

3400
  if (pVnode->config.cacheLast != req.cacheLast) {
321,183✔
3401
    pVnode->config.cacheLast = req.cacheLast;
88,035✔
3402
  }
3403

3404
  if (pVnode->config.walCfg.fsyncPeriod != req.walFsyncPeriod) {
321,183✔
3405
    pVnode->config.walCfg.fsyncPeriod = req.walFsyncPeriod;
52,384✔
3406
    walChanged = true;
52,384✔
3407
  }
3408

3409
  if (pVnode->config.walCfg.level != req.walLevel) {
321,183✔
3410
    if (pVnode->config.walCfg.level == 0) {
38,595✔
3411
      pVnode->config.walCfg.clearFiles = 1;
2,203✔
3412
    }
3413
    pVnode->config.walCfg.level = req.walLevel;
38,595✔
3414
    walChanged = true;
38,595✔
3415
  }
3416

3417
  if (pVnode->config.walCfg.retentionPeriod != req.walRetentionPeriod) {
321,183✔
3418
    pVnode->config.walCfg.retentionPeriod = req.walRetentionPeriod;
47,403✔
3419
    walChanged = true;
47,403✔
3420
  }
3421

3422
  if (pVnode->config.walCfg.retentionSize != req.walRetentionSize) {
321,183✔
3423
    pVnode->config.walCfg.retentionSize = req.walRetentionSize;
1,448✔
3424
    walChanged = true;
1,448✔
3425
  }
3426

3427
  if (pVnode->config.tsdbCfg.keep0 != req.daysToKeep0) {
321,183✔
3428
    pVnode->config.tsdbCfg.keep0 = req.daysToKeep0;
43,680✔
3429
    tsdbChanged = true;
43,680✔
3430
  }
3431

3432
  if (pVnode->config.tsdbCfg.keep1 != req.daysToKeep1) {
321,183✔
3433
    pVnode->config.tsdbCfg.keep1 = req.daysToKeep1;
42,232✔
3434
    tsdbChanged = true;
42,232✔
3435
  }
3436

3437
  if (pVnode->config.tsdbCfg.keep2 != req.daysToKeep2) {
321,183✔
3438
    pVnode->config.tsdbCfg.keep2 = req.daysToKeep2;
36,552✔
3439
    tsdbChanged = true;
36,552✔
3440
  }
3441

3442
  if (pVnode->config.tsdbCfg.keepTimeOffset != req.keepTimeOffset) {
321,183✔
3443
    pVnode->config.tsdbCfg.keepTimeOffset = req.keepTimeOffset;
7,370✔
3444
    tsdbChanged = true;
7,370✔
3445
  }
3446

3447
  if (req.sttTrigger != -1 && req.sttTrigger != pVnode->config.sttTrigger) {
321,183✔
3448
    if (req.sttTrigger > 1 && pVnode->config.sttTrigger > 1) {
2,524✔
3449
      pVnode->config.sttTrigger = req.sttTrigger;
1,448✔
3450
    } else {
3451
      vnodeAWait(&pVnode->commitTask);
1,076✔
3452

3453
      int32_t ret = tsdbDisableAndCancelAllBgTask(pVnode->pTsdb);
1,076✔
3454
      if (ret != 0) {
1,076✔
UNCOV
3455
        vError("vgId:%d, failed to disable bg task since %s", TD_VID(pVnode), tstrerror(ERRNO));
×
3456
      }
3457

3458
      pVnode->config.sttTrigger = req.sttTrigger;
1,076✔
3459
      tsdbEnableBgTask(pVnode->pTsdb);
1,076✔
3460
    }
3461
  }
3462

3463
  if (req.minRows != -1 && req.minRows != pVnode->config.tsdbCfg.minRows) {
321,183✔
3464
    pVnode->config.tsdbCfg.minRows = req.minRows;
1,448✔
3465
  }
3466

3467
  if (req.ssKeepLocal != -1 && req.ssKeepLocal != pVnode->config.ssKeepLocal) {
321,183✔
3468
    pVnode->config.ssKeepLocal = req.ssKeepLocal;
1,448✔
3469
  }
3470
  if (req.ssCompact != -1 && req.ssCompact != pVnode->config.ssCompact) {
321,183✔
3471
    pVnode->config.ssCompact = req.ssCompact;
1,448✔
3472
  }
3473
  if (req.allowDrop != pVnode->config.allowDrop) {
321,183✔
UNCOV
3474
    pVnode->config.allowDrop = req.allowDrop;
×
3475
  }
3476

3477
  if (walChanged) {
321,183✔
3478
    if (walAlter(pVnode->pWal, &pVnode->config.walCfg) != 0) {
136,870✔
UNCOV
3479
      vError("vgId:%d, failed to alter wal config since %s", TD_VID(pVnode), tstrerror(ERRNO));
×
3480
    }
3481
  }
3482

3483
  if (tsdbChanged) {
321,183✔
3484
    tsdbSetKeepCfg(pVnode->pTsdb, &pVnode->config.tsdbCfg);
58,318✔
3485
  }
3486

3487
  return 0;
321,183✔
3488
}
3489

UNCOV
3490
static int32_t vnodeProcessBatchDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
UNCOV
3491
  SBatchDeleteReq deleteReq;
×
UNCOV
3492
  SDecoder        decoder;
×
UNCOV
3493
  tDecoderInit(&decoder, pReq, len);
×
UNCOV
3494
  if (tDecodeSBatchDeleteReq(&decoder, &deleteReq) < 0) {
×
UNCOV
3495
    tDecoderClear(&decoder);
×
UNCOV
3496
    return terrno = TSDB_CODE_INVALID_MSG;
×
3497
  }
3498

3499
  SMetaReader mr = {0};
×
3500
  metaReaderDoInit(&mr, pVnode->pMeta, META_READER_NOLOCK);
×
UNCOV
3501
  STsdb *pTsdb = pVnode->pTsdb;
×
3502

UNCOV
3503
  int32_t sz = taosArrayGetSize(deleteReq.deleteReqs);
×
UNCOV
3504
  for (int32_t i = 0; i < sz; i++) {
×
UNCOV
3505
    SSingleDeleteReq *pOneReq = taosArrayGet(deleteReq.deleteReqs, i);
×
UNCOV
3506
    char             *name = pOneReq->tbname;
×
UNCOV
3507
    if (metaGetTableEntryByName(&mr, name) < 0) {
×
UNCOV
3508
      vDebug("vgId:%d, stream delete msg, skip since no table: %s", pVnode->config.vgId, name);
×
UNCOV
3509
      continue;
×
3510
    }
3511

UNCOV
3512
    int64_t uid = mr.me.uid;
×
3513

UNCOV
3514
    int32_t code = tsdbDeleteTableData(pTsdb, ver, deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
×
UNCOV
3515
    if (code < 0) {
×
UNCOV
3516
      terrno = code;
×
UNCOV
3517
      vError("vgId:%d, delete error since %s, suid:%" PRId64 ", uid:%" PRId64 ", start ts:%" PRId64 ", end ts:%" PRId64,
×
3518
             TD_VID(pVnode), terrstr(), deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
3519
    }
3520

UNCOV
3521
    if (deleteReq.level == 0) {
×
3522
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, deleteReq.ctimeMs);
×
3523
      if (code < 0) {
×
UNCOV
3524
        terrno = code;
×
UNCOV
3525
        vError("vgId:%d, update change time error since %s, suid:%" PRId64 ", uid:%" PRId64 ", start ts:%" PRId64
×
3526
               ", end ts:%" PRId64,
3527
               TD_VID(pVnode), terrstr(), deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
3528
      }
3529
    }
UNCOV
3530
    tDecoderClear(&mr.coder);
×
3531
  }
UNCOV
3532
  metaReaderClear(&mr);
×
UNCOV
3533
  taosArrayDestroy(deleteReq.deleteReqs);
×
3534
  return 0;
×
3535
}
3536

3537
static int32_t vnodeProcessDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
2,095,243✔
3538
                                     SRpcMsg *pOriginalMsg) {
3539
  int32_t     code = 0;
2,095,243✔
3540
  SDecoder   *pCoder = &(SDecoder){0};
2,095,243✔
3541
  SDeleteRes *pRes = &(SDeleteRes){0};
2,095,243✔
3542

3543
  pRsp->msgType = TDMT_VND_DELETE_RSP;
2,095,243✔
3544
  pRsp->pCont = NULL;
2,095,243✔
3545
  pRsp->contLen = 0;
2,095,243✔
3546
  pRsp->code = TSDB_CODE_SUCCESS;
2,095,243✔
3547

3548
  pRes->uidList = taosArrayInit(0, sizeof(tb_uid_t));
2,095,243✔
3549
  if (pRes->uidList == NULL) {
2,095,243✔
UNCOV
3550
    code = terrno;
×
UNCOV
3551
    goto _err;
×
3552
  }
3553

3554
  tDecoderInit(pCoder, pReq, len);
2,095,243✔
3555
  code = tDecodeDeleteRes(pCoder, pRes);
2,095,243✔
3556
  if (code) goto _err;
2,095,243✔
3557

3558
  if (pRes->affectedRows > 0) {
2,095,243✔
3559
    for (int32_t iUid = 0; iUid < taosArrayGetSize(pRes->uidList); iUid++) {
3,731,996✔
3560
      uint64_t uid = *(uint64_t *)taosArrayGet(pRes->uidList, iUid);
2,129,813✔
3561
      code = tsdbDeleteTableData(pVnode->pTsdb, ver, pRes->suid, uid, pRes->skey, pRes->ekey);
2,129,813✔
3562
      if (code) goto _err;
2,129,813✔
3563
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, pRes->ctimeMs);
2,129,813✔
3564
      if (code) goto _err;
2,129,813✔
3565
    }
3566
  }
3567

3568
  tDecoderClear(pCoder);
2,095,243✔
3569
  taosArrayDestroy(pRes->uidList);
2,095,243✔
3570

3571
  SVDeleteRsp rsp = {.affectedRows = pRes->affectedRows};
2,095,243✔
3572
  int32_t     ret = 0;
2,095,243✔
3573
  tEncodeSize(tEncodeSVDeleteRsp, &rsp, pRsp->contLen, ret);
2,095,243✔
3574
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
2,095,243✔
3575
  SEncoder ec = {0};
2,095,243✔
3576
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
2,095,243✔
3577
  code = tEncodeSVDeleteRsp(&ec, &rsp);
2,094,696✔
3578
  if (code) goto _err;
2,095,243✔
3579
  tEncoderClear(&ec);
2,095,243✔
3580
  return code;
2,095,243✔
3581

UNCOV
3582
_err:
×
3583
  /*
3584
  if(code == TSDB_CODE_SUCCESS){
3585
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_DELETE, pVnode->monitor.strClusterId,
3586
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
3587
                                        RPC_MSG_USER(pOriginalMsg), "Success"};
3588
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
3589
  }
3590
  else{
3591
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_DELETE, pVnode->monitor.strClusterId,
3592
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
3593
                                        RPC_MSG_USER(pOriginalMsg), "Failed"};
3594
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
3595
  }
3596
  */
3597

UNCOV
3598
  return code;
×
3599
}
3600
static int32_t vnodeProcessCreateIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
17,783✔
3601
  SVCreateStbReq req = {0};
17,783✔
3602
  SDecoder       dc = {0};
17,783✔
3603
  int32_t        code = 0;
17,783✔
3604

3605
  pRsp->msgType = TDMT_VND_CREATE_INDEX_RSP;
17,783✔
3606
  pRsp->code = TSDB_CODE_SUCCESS;
17,783✔
3607
  pRsp->pCont = NULL;
17,783✔
3608
  pRsp->contLen = 0;
17,783✔
3609

3610
  tDecoderInit(&dc, pReq, len);
17,783✔
3611
  // decode req
3612
  if (tDecodeSVCreateStbReq(&dc, &req) < 0) {
17,783✔
UNCOV
3613
    tDecoderClear(&dc);
×
UNCOV
3614
    return terrno = TSDB_CODE_INVALID_MSG;
×
3615
  }
3616

3617
  code = metaAddIndexToSuperTable(pVnode->pMeta, ver, &req);
17,783✔
3618
  if (code) {
17,783✔
UNCOV
3619
    pRsp->code = code;
×
UNCOV
3620
    goto _err;
×
3621
  }
3622
  tDecoderClear(&dc);
17,783✔
3623
  return 0;
17,783✔
3624

UNCOV
3625
_err:
×
3626
  tDecoderClear(&dc);
×
3627
  return code;
×
3628
}
3629
static int32_t vnodeProcessDropIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
14,476✔
3630
  SDropIndexReq req = {0};
14,476✔
3631
  int32_t       code = 0;
14,476✔
3632
  pRsp->msgType = TDMT_VND_DROP_INDEX_RSP;
14,476✔
3633
  pRsp->code = TSDB_CODE_SUCCESS;
14,476✔
3634
  pRsp->pCont = NULL;
14,476✔
3635
  pRsp->contLen = 0;
14,476✔
3636

3637
  if ((code = tDeserializeSDropIdxReq(pReq, len, &req))) {
14,476✔
UNCOV
3638
    pRsp->code = code;
×
UNCOV
3639
    return code;
×
3640
  }
3641

3642
  code = metaDropIndexFromSuperTable(pVnode->pMeta, ver, &req);
14,476✔
3643
  if (code) {
14,476✔
UNCOV
3644
    pRsp->code = code;
×
UNCOV
3645
    return code;
×
3646
  }
3647
  return TSDB_CODE_SUCCESS;
14,476✔
3648
}
3649

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

3652
static int32_t vnodeProcessCompactVnodeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
65,394✔
3653
  if (!pVnode->restored) {
65,394✔
3654
    vInfo("vgId:%d, ignore compact req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
1,431✔
3655
    return 0;
1,431✔
3656
  }
3657
  return vnodeAsyncCompact(pVnode, ver, pReq, len, pRsp);
63,963✔
3658
}
3659

UNCOV
3660
static int32_t vnodeProcessConfigChangeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
UNCOV
3661
  if (syncCheckMember(pVnode->sync) != 0) {
×
UNCOV
3662
    vError("vgId:%d, failed to check member", TD_VID(pVnode));
×
3663
  }
3664

UNCOV
3665
  pRsp->msgType = TDMT_SYNC_CONFIG_CHANGE_RSP;
×
UNCOV
3666
  pRsp->code = TSDB_CODE_SUCCESS;
×
UNCOV
3667
  pRsp->pCont = NULL;
×
UNCOV
3668
  pRsp->contLen = 0;
×
3669

UNCOV
3670
  return 0;
×
3671
}
3672

3673
static int32_t vnodeCheckState(SVnode *pVnode) {
52,772✔
3674
  SSyncState syncState = syncGetState(pVnode->sync);
52,772✔
3675
  if (syncState.state != TAOS_SYNC_STATE_LEADER) {
52,772✔
3676
    return terrno = TSDB_CODE_SYN_NOT_LEADER;
17,874✔
3677
  }
3678
  return 0;
34,898✔
3679
}
3680

3681
static int32_t vnodeCheckToken(SVnode *pVnode, char *member0Token, char *member1Token) {
34,898✔
3682
  char token[TSDB_ARB_TOKEN_SIZE] = {0};
34,898✔
3683
  if (vnodeGetArbToken(pVnode, token) != 0) {
34,898✔
UNCOV
3684
    return terrno = TSDB_CODE_NOT_FOUND;
×
3685
  }
3686

3687
  if (strncmp(token, member0Token, TSDB_ARB_TOKEN_SIZE) != 0 &&
34,898✔
3688
      strncmp(token, member1Token, TSDB_ARB_TOKEN_SIZE) != 0) {
33,154✔
3689
    return terrno = TSDB_CODE_MND_ARB_TOKEN_MISMATCH;
31,830✔
3690
  }
3691

3692
  terrno = TSDB_CODE_SUCCESS;
3,068✔
3693
  return 0;
3,068✔
3694
}
3695

3696
static int32_t vnodeCheckSyncd(SVnode *pVnode, char *member0Token, char *member1Token) {
1,534✔
3697
  int32_t code = vnodeCheckToken(pVnode, member0Token, member1Token);
1,534✔
3698
  if (code != 0) {
1,534✔
UNCOV
3699
    return code;
×
3700
  }
3701

3702
  return syncCheckSynced(pVnode->sync);
1,534✔
3703
}
3704

3705
static int32_t vnodeProcessArbCheckSyncReq(SVnode *pVnode, void *pReq, int32_t len, SRpcMsg *pRsp) {
3,062✔
3706
  int32_t code = 0;
3,062✔
3707

3708
  if ((code = vnodeCheckState(pVnode)) != 0) {
3,062✔
3709
    vDebug("vgId:%d, failed to preprocess vnode-arb-check-sync request since %s", TD_VID(pVnode), tstrerror(code));
1,528✔
3710
    return 0;
1,528✔
3711
  }
3712

3713
  SVArbCheckSyncReq syncReq = {0};
1,534✔
3714

3715
  code = tDeserializeSVArbCheckSyncReq(pReq, len, &syncReq);
1,534✔
3716
  if (code) {
1,534✔
UNCOV
3717
    return code;
×
3718
  }
3719

3720
  vInfo("vgId:%d, start to process vnode-arb-check-sync req QID:0x%" PRIx64 ":0x%" PRIx64 ", seqNum:%" PRIx64
1,534✔
3721
        ", arbToken:%s, member0Token:%s, member1Token:%s, "
3722
        "arbTerm:%" PRId64,
3723
        TD_VID(pVnode), pRsp->info.traceId.rootId, pRsp->info.traceId.msgId, pRsp->info.seqNum, syncReq.arbToken,
3724
        syncReq.member0Token, syncReq.member1Token, syncReq.arbTerm);
3725

3726
  pRsp->msgType = TDMT_VND_ARB_CHECK_SYNC_RSP;
1,534✔
3727
  pRsp->code = TSDB_CODE_SUCCESS;
1,534✔
3728
  pRsp->pCont = NULL;
1,534✔
3729
  pRsp->contLen = 0;
1,534✔
3730

3731
  SVArbCheckSyncRsp syncRsp = {0};
1,534✔
3732
  syncRsp.arbToken = syncReq.arbToken;
1,534✔
3733
  syncRsp.member0Token = syncReq.member0Token;
1,534✔
3734
  syncRsp.member1Token = syncReq.member1Token;
1,534✔
3735
  syncRsp.vgId = TD_VID(pVnode);
1,534✔
3736

3737
  if ((syncRsp.errCode = vnodeCheckSyncd(pVnode, syncReq.member0Token, syncReq.member1Token)) != 0) {
1,534✔
UNCOV
3738
    vError("vgId:%d, failed to check assigned log syncd since %s", TD_VID(pVnode), tstrerror(syncRsp.errCode));
×
3739
  }
3740

3741
  if ((code = vnodeUpdateArbTerm(pVnode, syncReq.arbTerm)) != 0) {
1,534✔
UNCOV
3742
    vError("vgId:%d, failed to update arb term since %s", TD_VID(pVnode), tstrerror(code));
×
UNCOV
3743
    goto _OVER;
×
3744
  }
3745

3746
  int32_t contLen = tSerializeSVArbCheckSyncRsp(NULL, 0, &syncRsp);
1,534✔
3747
  if (contLen <= 0) {
1,534✔
3748
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
3749
    code = -1;
×
3750
    goto _OVER;
×
3751
  }
3752
  void *pHead = rpcMallocCont(contLen);
1,534✔
3753
  if (!pHead) {
1,534✔
UNCOV
3754
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
3755
    code = -1;
×
UNCOV
3756
    goto _OVER;
×
3757
  }
3758

3759
  if (tSerializeSVArbCheckSyncRsp(pHead, contLen, &syncRsp) <= 0) {
1,534✔
UNCOV
3760
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
3761
    rpcFreeCont(pHead);
×
UNCOV
3762
    code = -1;
×
3763
    goto _OVER;
×
3764
  }
3765

3766
  pRsp->pCont = pHead;
1,534✔
3767
  pRsp->contLen = contLen;
1,534✔
3768

3769
  vInfo(
1,534✔
3770
      "vgId:%d, suceed to process vnode-arb-check-sync req rsp.code:%s, arbToken:%s, member0Token:%s, "
3771
      "member1Token:%s",
3772
      TD_VID(pVnode), tstrerror(syncRsp.errCode), syncRsp.arbToken, syncRsp.member0Token, syncRsp.member1Token);
3773

3774
  code = TSDB_CODE_SUCCESS;
1,534✔
3775

3776
_OVER:
1,534✔
3777
  if (code != 0) {
1,534✔
UNCOV
3778
    vError("vgId:%d, failed to process arb check req rsp.code:%s since %s", TD_VID(pVnode), tstrerror(syncRsp.errCode),
×
3779
           tstrerror(code));
3780
  }
3781
  tFreeSVArbCheckSyncReq(&syncReq);
1,534✔
3782
  return code;
1,534✔
3783
}
3784

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