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

taosdata / TDengine / #4998

21 Mar 2026 01:22PM UTC coverage: 72.335% (+0.6%) from 71.739%
#4998

push

travis-ci

web-flow
enh:register add secondEp (#34867)

61 of 69 new or added lines in 1 file covered. (88.41%)

8670 existing lines in 142 files now uncovered.

253516 of 350475 relevant lines covered (72.33%)

133451446.75 hits per line

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

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

16
#include <stdint.h>
17
#include "audit.h"
18
#include "cos.h"
19
#include "libs/new-stream/stream.h"
20
#include "monitor.h"
21
#include "taoserror.h"
22
#include "tarray.h"
23
#include "tencode.h"
24
#include "tglobal.h"
25
#include "thash.h"
26
#include "tlrucache.h"
27
#include "tmsg.h"
28
#include "tmsgcb.h"
29
#include "tstrbuild.h"
30
#include "tutil.h"
31
#include "tsdb.h"
32
#include "ttypes.h"
33
#include "vnd.h"
34
#include "vnode.h"
35
#include "vnodeInt.h"
36

37
extern taos_counter_t *tsInsertCounter;
38

39
extern int32_t vnodeProcessScanVnodeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
40
extern int32_t vnodeQueryScanProgress(SVnode *pVnode, SRpcMsg *pMsg);
41

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

71
static int32_t vnodeCheckState(SVnode *pVnode);
72
static int32_t vnodeCheckToken(SVnode *pVnode, char *member0Token, char *member1Token);
73
static int32_t vnodeCheckSyncd(SVnode *pVnode, char *member0Token, char *member1Token);
74
static int32_t vnodeProcessFetchTtlExpiredTbs(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
75

76
extern int32_t vnodeProcessKillCompactReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
77
extern int32_t vnodeQueryCompactProgress(SVnode *pVnode, SRpcMsg *pMsg);
78

79
extern int32_t vnodeProcessKillRetentionReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
80
extern int32_t vnodeQueryRetentionProgress(SVnode *pVnode, SRpcMsg *pMsg);
81

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

84
extern int32_t vnodeListSsMigrateFileSets(SVnode *pVnode, SRpcMsg *pMsg);
85
static int32_t vnodeProcessSsMigrateFileSetReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
86
extern int32_t vnodeQuerySsMigrateProgress(SVnode *pVnode, SRpcMsg *pMsg);
87
static int32_t vnodeProcessFollowerSsMigrateReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
88
static int32_t vnodeProcessKillSsMigrateReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
89

90
static int32_t vnodePreprocessCreateTableReq(SVnode *pVnode, SDecoder *pCoder, int64_t btime, int64_t *pUid) {
64,501,726✔
91
  int32_t code = 0;
64,501,726✔
92
  int32_t lino = 0;
64,501,726✔
93

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

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

105
  // name
106
  char *name = NULL;
64,505,274✔
107
  if (tDecodeCStr(pCoder, &name) < 0) {
64,505,375✔
UNCOV
108
    code = TSDB_CODE_INVALID_MSG;
×
UNCOV
109
    TSDB_CHECK_CODE(code, lino, _exit);
×
110
  }
111

112
  // uid
113
  int64_t uid = metaGetTableEntryUidByName(pVnode->pMeta, name);
64,505,375✔
114
  if (uid == 0) {
64,507,806✔
115
    uid = tGenIdPI64();
64,156,518✔
116
  }
117
  taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), uid);
64,499,568✔
118

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

122
  tEndDecode(pCoder);
64,488,595✔
123

124
_exit:
64,493,021✔
125
  if (code) {
64,490,746✔
UNCOV
126
    vError("vgId:%d %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
127
  } else {
128
    vTrace("vgId:%d %s done, table:%s uid generated:%" PRId64, TD_VID(pVnode), __func__, name, uid);
64,490,746✔
129
    if (pUid) *pUid = uid;
64,483,207✔
130
  }
131
  return code;
64,483,512✔
132
}
133
static int32_t vnodePreProcessCreateTableMsg(SVnode *pVnode, SRpcMsg *pMsg) {
43,253,876✔
134
  int32_t code = 0;
43,253,876✔
135
  int32_t lino = 0;
43,253,876✔
136

137
  int64_t  btime = taosGetTimestampMs();
43,253,911✔
138
  SDecoder dc = {0};
43,253,911✔
139
  int32_t  nReqs;
43,251,503✔
140

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

147
  if (tDecodeI32v(&dc, &nReqs) < 0) {
43,253,876✔
UNCOV
148
    code = TSDB_CODE_INVALID_MSG;
×
UNCOV
149
    TSDB_CHECK_CODE(code, lino, _exit);
×
150
  }
151
  for (int32_t iReq = 0; iReq < nReqs; iReq++) {
91,894,364✔
152
    code = vnodePreprocessCreateTableReq(pVnode, &dc, btime, NULL);
48,666,810✔
153
    TSDB_CHECK_CODE(code, lino, _exit);
48,640,488✔
154
  }
155

156
  tEndDecode(&dc);
43,227,554✔
157

158
_exit:
43,249,513✔
159
  tDecoderClear(&dc);
43,250,638✔
160
  if (code) {
43,251,538✔
UNCOV
161
    vError("vgId:%d, %s:%d failed to preprocess submit request since %s, msg type:%s", TD_VID(pVnode), __func__, lino,
×
162
           tstrerror(code), TMSG_INFO(pMsg->msgType));
163
  }
164
  return code;
43,247,808✔
165
}
166

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

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

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

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

182
  destroyAlterTbReq(&vAlterTbReq);
12,393,531✔
183

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

195
static int32_t vnodePreProcessDropTtlMsg(SVnode *pVnode, SRpcMsg *pMsg) {
9,770,695✔
196
  int32_t code = TSDB_CODE_INVALID_MSG;
9,770,695✔
197
  int32_t lino = 0;
9,770,695✔
198

199
  SMsgHead *pContOld = pMsg->pCont;
9,770,695✔
200
  int32_t   reqLenOld = pMsg->contLen - sizeof(SMsgHead);
9,771,711✔
201

202
  SArray *tbUids = NULL;
9,774,494✔
203
  int64_t timestampMs = 0;
9,774,494✔
204

205
  SVDropTtlTableReq ttlReq = {0};
9,774,494✔
206
  if (tDeserializeSVDropTtlTableReq((char *)pContOld + sizeof(SMsgHead), reqLenOld, &ttlReq) != 0) {
9,770,954✔
UNCOV
207
    code = TSDB_CODE_INVALID_MSG;
×
UNCOV
208
    TSDB_CHECK_CODE(code, lino, _exit);
×
209
  }
210

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

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

225
    ttlReq.nUids = taosArrayGetSize(tbUids);
9,741,497✔
226
    ttlReq.pTbUids = tbUids;
9,755,917✔
227
  }
228

229
  if (ttlReq.nUids == 0) {
9,755,917✔
230
    code = TSDB_CODE_MSG_PREPROCESSED;
9,722,958✔
231
    TSDB_CHECK_CODE(code, lino, _exit);
9,722,958✔
232
  }
233

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

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

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

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

255
  code = 0;
11,934✔
256

257
_exit:
9,762,637✔
258
  taosArrayDestroy(tbUids);
9,762,590✔
259

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

267
  return code;
9,731,410✔
268
}
269

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

275
  if (tStartDecode(pCoder) < 0) {
508,155,307✔
UNCOV
276
    code = TSDB_CODE_INVALID_MSG;
×
UNCOV
277
    TSDB_CHECK_CODE(code, lino, _exit);
×
278
  }
279

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

289
  int64_t uid;
508,145,882✔
290
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
508,148,993✔
291
    code = vnodePreprocessCreateTableReq(pVnode, pCoder, btimeMs, &uid);
15,832,560✔
292
    TSDB_CHECK_CODE(code, lino, _exit);
15,832,434✔
293
  }
294

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

301
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
508,154,440✔
302
    taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), uid);
15,832,926✔
303
    pCoder->pos += sizeof(int64_t);
15,826,836✔
304
  } else {
305
    if (tDecodeI64(pCoder, &submitTbData.uid) < 0) {
492,316,266✔
UNCOV
306
      code = TSDB_CODE_INVALID_MSG;
×
UNCOV
307
      TSDB_CHECK_CODE(code, lino, _exit);
×
308
    }
309
  }
310

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

316
  // scan and check
317
  TSKEY now = btimeMs;
508,139,939✔
318
  if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_MICRO) {
508,139,939✔
319
    now *= 1000;
2,562,402✔
320
  } else if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_NANO) {
505,582,071✔
321
    now *= 1000000;
1,495,445✔
322
  }
323

324
  int32_t keep = pVnode->config.tsdbCfg.keep2;
508,143,617✔
325

326
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * keep;
508,135,696✔
327
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
508,154,036✔
328
  if (submitTbData.flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
508,152,478✔
329
    uint64_t nColData;
6,836,208✔
330
    if (tDecodeU64v(pCoder, &nColData) < 0) {
6,836,623✔
UNCOV
331
      code = TSDB_CODE_INVALID_MSG;
×
UNCOV
332
      TSDB_CHECK_CODE(code, lino, _exit);
×
333
    }
334

335
    SColData colData = {0};
6,836,623✔
336
    code = tDecodeColData(version, pCoder, &colData, false);
6,835,619✔
337
    if (code) {
6,837,271✔
338
      code = TSDB_CODE_INVALID_MSG;
×
339
      TSDB_CHECK_CODE(code, lino, _exit);
×
340
    }
341

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

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

354
    for (uint64_t i = 1; i < nColData; i++) {
20,939,849✔
355
      code = tDecodeColData(version, pCoder, &colData, true);
14,129,559✔
356
      if (code) {
14,129,848✔
UNCOV
357
        code = TSDB_CODE_INVALID_MSG;
×
UNCOV
358
        TSDB_CHECK_CODE(code, lino, _exit);
×
359
      }
360
    }
361
  } else {
362
    uint64_t nRow;
501,312,152✔
363
    if (tDecodeU64v(pCoder, &nRow) < 0) {
501,309,827✔
364
      code = TSDB_CODE_INVALID_MSG;
×
365
      TSDB_CHECK_CODE(code, lino, _exit);
×
366
    }
367

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

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

389
  if (!tDecodeIsEnd(pCoder)) {
508,069,829✔
390
    taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), ctimeMs);
508,075,747✔
391
    pCoder->pos += sizeof(int64_t);
508,043,402✔
392
  }
393

394
  tEndDecode(pCoder);
508,040,901✔
395

396
_exit:
508,130,640✔
397
  if (code) {
508,132,783✔
398
    vError("vgId:%d, %s:%d failed to vnodePreProcessSubmitTbData submit request since %s", TD_VID(pVnode), __func__,
69,946✔
399
           lino, tstrerror(code));
400
  }
401
  return code;
508,132,783✔
402
}
403
static int32_t vnodePreProcessSubmitMsg(SVnode *pVnode, SRpcMsg *pMsg) {
482,908,547✔
404
  int32_t code = 0;
482,908,547✔
405
  int32_t lino = 0;
482,908,547✔
406

407
  if (tsBypassFlag & TSDB_BYPASS_RA_RPC_RECV_SUBMIT) {
482,908,547✔
408
    return TSDB_CODE_MSG_PREPROCESSED;
57✔
409
  }
410

411
  SDecoder *pCoder = &(SDecoder){0};
482,908,490✔
412

413
  if (taosHton64(((SSubmitReq2Msg *)pMsg->pCont)->version) != 1) {
482,909,303✔
UNCOV
414
    code = TSDB_CODE_INVALID_MSG;
×
UNCOV
415
    TSDB_CHECK_CODE(code, lino, _exit);
×
416
  }
417

418
  tDecoderInit(pCoder, (uint8_t *)pMsg->pCont + sizeof(SSubmitReq2Msg), pMsg->contLen - sizeof(SSubmitReq2Msg));
482,910,079✔
419

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

425
  uint64_t nSubmitTbData;
482,905,961✔
426
  if (tDecodeU64v(pCoder, &nSubmitTbData) < 0) {
482,908,698✔
UNCOV
427
    code = TSDB_CODE_INVALID_MSG;
×
428
    TSDB_CHECK_CODE(code, lino, _exit);
×
429
  }
430

431
  int64_t btimeMs = taosGetTimestampMs();
482,908,459✔
432
  int64_t ctimeMs = btimeMs;
482,908,459✔
433
  for (uint64_t i = 0; i < nSubmitTbData; i++) {
990,964,332✔
434
    code = vnodePreProcessSubmitTbData(pVnode, pCoder, btimeMs, ctimeMs);
508,135,918✔
435
    TSDB_CHECK_CODE(code, lino, _exit);
508,125,819✔
436
  }
437

438
  tEndDecode(pCoder);
482,828,414✔
439

440
_exit:
482,902,775✔
441
  tDecoderClear(pCoder);
482,903,340✔
442
  if (code) {
482,904,219✔
443
    vError("vgId:%d, %s:%d failed to preprocess submit request since %s, msg type:%s", TD_VID(pVnode), __func__, lino,
69,946✔
444
           tstrerror(code), TMSG_INFO(pMsg->msgType));
445
  }
446
  return code;
482,892,360✔
447
}
448

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

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

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

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

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

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

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

481
  rpcFreeCont(pMsg->pCont);
1,768,492✔
482
  pMsg->pCont = pCont;
1,769,628✔
483
  pMsg->contLen = size + sizeof(SMsgHead);
1,769,628✔
484

485
  taosArrayDestroy(res.uidList);
1,770,196✔
486

487
_exit:
1,770,196✔
488
  return code;
1,770,196✔
489
}
490

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

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

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

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

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

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

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

523
  SVArbCheckSyncReq syncReq = {0};
16,494✔
524

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

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

535
  int32_t code = terrno;
16,494✔
536
  tFreeSVArbCheckSyncReq(&syncReq);
16,494✔
537

538
  return code;
16,494✔
539
}
540

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

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

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

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

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

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

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

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

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

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

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

619
int32_t vnodePreProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
575,362,319✔
620
  int32_t code = 0;
575,362,319✔
621

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

628
  switch (pMsg->msgType) {
575,361,647✔
629
    case TDMT_VND_CREATE_TABLE: {
43,253,911✔
630
      code = vnodePreProcessCreateTableMsg(pVnode, pMsg);
43,253,911✔
631
    } break;
43,250,253✔
632
    case TDMT_VND_ALTER_TABLE: {
12,393,531✔
633
      code = vnodePreProcessAlterTableMsg(pVnode, pMsg);
12,393,531✔
634
    } break;
12,392,918✔
635
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
9,779,127✔
636
    case TDMT_VND_DROP_TTL_TABLE: {
637
      code = vnodePreProcessDropTtlMsg(pVnode, pMsg);
9,779,127✔
638
    } break;
9,739,848✔
639
    case TDMT_VND_SUBMIT: {
482,909,395✔
640
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.preprocess_time, METRIC_LEVEL_LOW,
482,909,395✔
641
                           { code = vnodePreProcessSubmitMsg(pVnode, pMsg); });
642
    } break;
482,900,320✔
643
    case TDMT_VND_DELETE: {
1,770,196✔
644
      code = vnodePreProcessDeleteMsg(pVnode, pMsg);
1,770,196✔
645
    } break;
1,769,628✔
UNCOV
646
    case TDMT_VND_BATCH_DEL: {
×
UNCOV
647
      code = vnodePreProcessBatchDeleteMsg(pVnode, pMsg);
×
UNCOV
648
    } break;
×
649
    case TDMT_VND_ARB_CHECK_SYNC: {
19,791✔
650
      code = vnodePreProcessArbCheckSyncMsg(pVnode, pMsg);
19,791✔
651
    } break;
19,791✔
652
    case TDMT_VND_DROP_TABLE: {
1,736,223✔
653
      code = vnodePreProcessDropTbMsg(pVnode, pMsg);
1,736,223✔
654
    } break;
1,736,223✔
655
#ifdef TD_ENTERPRISE
656
    case TDMT_VND_SSMIGRATE_FILESET: {
3,810✔
657
      code = vnodePreProcessSsMigrateFileSetReq(pVnode, pMsg);
3,810✔
658
    } break;
3,810✔
659
#endif
660
    case TDMT_VND_AUDIT_RECORD: {
2,754✔
661
      vTrace("vgId:%d, pre process TDMT_VND_AUDIT_RECORD", TD_VID(pVnode));
2,754✔
662
    } break;
228✔
663
    default:
23,495,141✔
664
      break;
23,495,141✔
665
  }
666

667
  if (code && code != TSDB_CODE_MSG_PREPROCESSED) {
575,308,160✔
668
    vError("vgId:%d, failed to preprocess write request since %s, msg type:%s", TD_VID(pVnode), tstrerror(code),
84,630✔
669
           TMSG_INFO(pMsg->msgType));
670
  }
671
  return code;
575,322,209✔
672
}
673

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

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

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

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

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

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

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

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

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

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

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

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

738
  SBseBatch *pBatch = NULL;
97✔
739

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

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

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

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

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

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

796
  return code;
45,726✔
797
}
798

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

806
  (void)taosThreadMutexLock(&pVnode->mutex);
708,412,902✔
807
  if (pVnode->disableWrite) {
708,517,911✔
UNCOV
808
    (void)taosThreadMutexUnlock(&pVnode->mutex);
×
UNCOV
809
    vError("vgId:%d write is disabled for snapshot, version:%" PRId64, TD_VID(pVnode), ver);
×
UNCOV
810
    return TSDB_CODE_VND_WRITE_DISABLED;
×
811
  }
812
  (void)taosThreadMutexUnlock(&pVnode->mutex);
708,380,578✔
813

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

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

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

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

838
  atomic_store_64(&pVnode->state.applied, ver);
708,539,543✔
839
  atomic_store_64(&pVnode->state.applyTerm, pMsg->info.conn.applyTerm);
708,545,758✔
840

841
  if (!syncUtilUserCommit(pMsg->msgType)) goto _exit;
708,543,081✔
842

843
  // skip header
844
  pReq = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
697,545,560✔
845
  len = pMsg->contLen - sizeof(SMsgHead);
697,545,489✔
846
  bool needCommit = false;
697,542,154✔
847
  bool forceTrimWal = false;
697,542,154✔
848

849
  switch (pMsg->msgType) {
697,542,154✔
850
    /* META */
851
    case TDMT_VND_CREATE_STB:
5,278,936✔
852
      code = vnodeProcessCreateStbReq(pVnode, ver, pReq, len, pRsp);
5,278,936✔
853
      TSDB_CHECK_CODE(code, lino, _err);
5,279,328✔
854
      break;
5,276,320✔
855
    case TDMT_VND_ALTER_STB:
7,899,547✔
856
      code = vnodeProcessAlterStbReq(pVnode, ver, pReq, len, pRsp);
7,899,547✔
857
      TSDB_CHECK_CODE(code, lino, _err);
7,898,448✔
858
      break;
7,898,448✔
859
    case TDMT_VND_DROP_STB:
920,264✔
860
      code = vnodeProcessDropStbReq(pVnode, ver, pReq, len, pRsp, pMsg);
920,264✔
861
      TSDB_CHECK_CODE(code, lino, _err);
920,264✔
862
      break;
920,264✔
863
    case TDMT_VND_CREATE_TABLE:
46,072,181✔
864
      code = vnodeProcessCreateTbReq(pVnode, ver, pReq, len, pRsp, pMsg);
46,072,181✔
865
      TSDB_CHECK_CODE(code, lino, _err);
46,067,460✔
866
      break;
46,067,460✔
867
    case TDMT_VND_ALTER_TABLE:
12,393,897✔
868
      code = vnodeProcessAlterTbReq(pVnode, ver, pReq, len, pRsp);
12,393,897✔
869
      TSDB_CHECK_CODE(code, lino, _err);
12,393,897✔
870
      break;
12,393,897✔
871
    case TDMT_VND_DROP_TABLE:
1,736,280✔
872
    case TDMT_VND_SNODE_DROP_TABLE:
873
      code = vnodeProcessDropTbReq(pVnode, ver, pReq, len, pRsp, pMsg);
1,736,280✔
874
      TSDB_CHECK_CODE(code, lino, _err);
1,736,280✔
875
      break;
1,736,280✔
876
    case TDMT_VND_CREATE_RSMA:
40,452✔
877
      code = vnodeProcessCreateRsmaReq(pVnode, ver, pReq, len, pRsp);
40,452✔
878
      TSDB_CHECK_CODE(code, lino, _err);
40,452✔
879
      break;
40,452✔
880
    case TDMT_VND_DROP_RSMA:
20,364✔
881
      code = vnodeProcessDropRsmaReq(pVnode, ver, pReq, len, pRsp);
20,364✔
882
      TSDB_CHECK_CODE(code, lino, _err);
20,364✔
883
      break;
20,364✔
884
    case TDMT_VND_ALTER_RSMA:
12,468✔
885
      code = vnodeProcessAlterRsmaReq(pVnode, ver, pReq, len, pRsp);
12,468✔
886
      TSDB_CHECK_CODE(code, lino, _err);
12,468✔
887
      break;
12,468✔
UNCOV
888
    case TDMT_VND_DROP_TTL_TABLE:
×
UNCOV
889
      code = vnodeProcessDropTtlTbReq(pVnode, ver, pReq, len, pRsp);
×
UNCOV
890
      TSDB_CHECK_CODE(code, lino, _err);
×
UNCOV
891
      break;
×
892
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
11,934✔
893
      code = vnodeProcessFetchTtlExpiredTbs(pVnode, ver, pReq, len, pRsp);
11,934✔
894
      TSDB_CHECK_CODE(code, lino, _err);
11,934✔
895
      break;
11,934✔
896
    case TDMT_VND_TRIM:
79,830✔
897
      code = vnodeProcessTrimReq(pVnode, ver, pReq, len, pRsp);
79,830✔
898
      TSDB_CHECK_CODE(code, lino, _err);
79,994✔
899
      break;
79,994✔
900
      case TDMT_VND_TRIM_WAL:
6,840✔
901
        needCommit = true;
6,840✔
902
        forceTrimWal = true;
6,840✔
903
        break;
6,840✔
904
#ifdef TD_ENTERPRISE
905
    case TDMT_VND_SSMIGRATE_FILESET:
3,810✔
906
      if (vnodeProcessSsMigrateFileSetReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
3,810✔
907
      break;
3,810✔
908
    case TDMT_VND_FOLLOWER_SSMIGRATE:
7,620✔
909
      if (vnodeProcessFollowerSsMigrateReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
7,620✔
910
      break;
7,620✔
UNCOV
911
    case TDMT_VND_KILL_SSMIGRATE:
×
UNCOV
912
      if (vnodeProcessKillSsMigrateReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
UNCOV
913
      break;
×
914
#endif
915

916
    /* TSDB */
917
    case TDMT_VND_SUBMIT: {
600,264,916✔
918
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.apply_time, METRIC_LEVEL_LOW, {
600,264,916✔
919
        if (vnodeProcessSubmitReq(pVnode, ver, pMsg->pCont, pMsg->contLen, pRsp, pMsg) < 0) goto _err;
920
      });
921
      METRICS_UPDATE(pVnode->writeMetrics.apply_bytes, METRIC_LEVEL_LOW, (int64_t)pMsg->contLen);
600,216,605✔
922
      break;
600,190,928✔
923
    }
924
    case TDMT_VND_DELETE:
2,157,816✔
925
      code = vnodeProcessDeleteReq(pVnode, ver, pReq, len, pRsp, pMsg);
2,157,816✔
926
      TSDB_CHECK_CODE(code, lino, _err);
2,157,816✔
927
      break;
2,157,816✔
UNCOV
928
    case TDMT_VND_BATCH_DEL:
×
UNCOV
929
      code = vnodeProcessBatchDeleteReq(pVnode, ver, pReq, len, pRsp);
×
UNCOV
930
      TSDB_CHECK_CODE(code, lino, _err);
×
UNCOV
931
      break;
×
932
      /* TQ */
933
#if defined(USE_TQ) || defined(USE_STREAM)
934
    case TDMT_VND_TMQ_SUBSCRIBE:
1,205,307✔
935
      code = tqProcessSubscribeReq(pVnode->pTq, ver, pReq, len);
1,205,307✔
936
      TSDB_CHECK_CODE(code, lino, _err);
1,199,501✔
937
      break;
1,199,501✔
938
    case TDMT_VND_TMQ_DELETE_SUB:
271,024✔
939
      code = tqProcessDeleteSubReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen);
271,024✔
940
      TSDB_CHECK_CODE(code, lino, _err);
269,048✔
941
      break;
269,048✔
942
    case TDMT_VND_TMQ_COMMIT_OFFSET:
9,180,753✔
943
      code = tqProcessOffsetCommitReq(pVnode->pTq, ver, pReq, len);
9,180,753✔
944
      TSDB_CHECK_CODE(code, lino, _err);
9,177,232✔
945
      break;
9,177,232✔
946
#endif
947
    case TDMT_VND_ALTER_CONFIRM:
4,367,131✔
948
      needCommit = pVnode->config.hashChange;
4,367,131✔
949
      code = vnodeProcessAlterConfirmReq(pVnode, ver, pReq, len, pRsp);
4,367,131✔
950
      TSDB_CHECK_CODE(code, lino, _err);
4,367,131✔
951
      break;
4,367,131✔
952
    case TDMT_VND_ALTER_CONFIG:
330,981✔
953
      vnodeProcessAlterConfigReq(pVnode, ver, pReq, len, pRsp);
330,981✔
954
      break;
330,981✔
955
    case TDMT_VND_COMMIT:
5,153,498✔
956
      needCommit = true;
5,153,498✔
957
      break;
5,153,498✔
958
    case TDMT_VND_CREATE_INDEX:
17,958✔
959
      vnodeProcessCreateIndexReq(pVnode, ver, pReq, len, pRsp);
17,958✔
960
      break;
17,958✔
961
    case TDMT_VND_DROP_INDEX:
14,588✔
962
      vnodeProcessDropIndexReq(pVnode, ver, pReq, len, pRsp);
14,588✔
963
      break;
14,588✔
964
    case TDMT_VND_COMPACT:
69,679✔
965
      vnodeProcessCompactVnodeReq(pVnode, ver, pReq, len, pRsp);
69,679✔
966
      goto _exit;
69,679✔
967
    case TDMT_VND_SCAN:
506✔
968
      vnodeProcessScanVnodeReq(pVnode, ver, pReq, len, pRsp);
506✔
969
      goto _exit;
506✔
UNCOV
970
    case TDMT_SYNC_CONFIG_CHANGE:
×
UNCOV
971
      vnodeProcessConfigChangeReq(pVnode, ver, pReq, len, pRsp);
×
UNCOV
972
      break;
×
973
#ifdef TD_ENTERPRISE
974
    case TDMT_VND_KILL_COMPACT:
1,503✔
975
      vnodeProcessKillCompactReq(pVnode, ver, pReq, len, pRsp);
1,503✔
976
      break;
1,503✔
977
#endif
978
    case TDMT_VND_KILL_SCAN:
92✔
979
      vnodeProcessKillScanReq(pVnode, ver, pReq, len, pRsp);
92✔
980
      break;
92✔
UNCOV
981
    case TDMT_VND_KILL_TRIM:
×
UNCOV
982
      vnodeProcessKillRetentionReq(pVnode, ver, pReq, len, pRsp);
×
UNCOV
983
      break;
×
984
    /* ARB */
985
    case TDMT_VND_ARB_CHECK_SYNC:
23,267✔
986
      vnodeProcessArbCheckSyncReq(pVnode, pReq, len, pRsp);
23,267✔
987
      break;
23,267✔
988
    case TDMT_VND_AUDIT_RECORD:
228✔
989
      vTrace("vgId:%d, processed audit msg", TD_VID(pVnode));
228✔
990
      code = vnodeProcessAuditRecordReq(pVnode, ver, pReq, len, pMsg);
228✔
991
      pRsp->code = code;
228✔
992
      pRsp->msgType = TDMT_VND_AUDIT_RECORD_RSP;
228✔
993
      pRsp->pCont = NULL;
228✔
994
      pRsp->contLen = 0;
228✔
995
      if (code) {
228✔
UNCOV
996
        goto _err;
×
997
      }
998
      break;
228✔
UNCOV
999
    default:
×
UNCOV
1000
      vError("vgId:%d, unprocessed msg, %d", TD_VID(pVnode), pMsg->msgType);
×
UNCOV
1001
      return TSDB_CODE_INVALID_MSG;
×
1002
  }
1003

1004
  vGDebug(&pMsg->info.traceId, "vgId:%d, index:%" PRId64 ", msg processed, code:0x%x", TD_VID(pVnode), ver, pRsp->code);
697,379,922✔
1005

1006
  walApplyVer(pVnode->pWal, ver);
697,417,966✔
1007

1008
  // commit if need
1009
  if (needCommit) {
697,440,735✔
1010
    vInfo("vgId:%d, commit at version %" PRId64, TD_VID(pVnode), ver);
5,186,145✔
1011
    code = vnodeAsyncCommit(pVnode, forceTrimWal);
5,186,145✔
1012
    if (code) {
5,186,145✔
UNCOV
1013
      vError("vgId:%d, failed to vnode async commit since %s.", TD_VID(pVnode), tstrerror(terrno));
×
UNCOV
1014
      goto _err;
×
1015
    }
1016

1017
    // start a new one
1018
    METRICS_TIMING_BLOCK(pVnode->writeMetrics.memtable_wait_time, METRIC_LEVEL_LOW, {
5,186,145✔
1019
      code = vnodeBegin(pVnode);
1020
      if (code) {
1021
        vError("vgId:%d, failed to begin vnode since %s.", TD_VID(pVnode), tstrerror(terrno));
1022
        goto _err;
1023
      }
1024
    });
1025
  }
1026

1027
_exit:
697,434,339✔
1028
  return 0;
708,484,963✔
1029

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

1039
  return code;
34,028✔
1040
}
1041

1042
int32_t vnodePreprocessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg) {
428,477,026✔
1043
  if (TDMT_SCH_QUERY != pMsg->msgType && TDMT_SCH_MERGE_QUERY != pMsg->msgType) {
428,477,026✔
1044
    return 0;
66,036,805✔
1045
  }
1046

1047
  int32_t qType = 0;
362,497,391✔
1048
  return qWorkerPreprocessQueryMsg(pVnode->pQuery, pMsg, (TDMT_SCH_QUERY == pMsg->msgType), &qType);
362,506,522✔
1049
}
1050

1051
int32_t vnodeProcessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
428,539,713✔
1052
  const STraceId *trace = &pMsg->info.traceId;
428,539,713✔
1053
  vDebug("vgId:%d, msg:%s, %p in vnode query queue is processing, %" PRIx64 ":%" PRIx64, 
428,549,015✔
1054
    pVnode->config.vgId, TMSG_INFO(pMsg->msgType), pMsg, TRACE_GET_ROOTID(trace), TRACE_GET_MSGID(trace));
1055

1056
  if (pMsg->msgType == TDMT_VND_TMQ_CONSUME && !syncIsReadyForRead(pVnode->sync)) {
428,624,829✔
1057
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
428,891✔
1058
    return 0;
428,891✔
1059
  }
1060

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

1066
  SReadHandle handle = {0};
428,210,395✔
1067
  handle.vnode = pVnode;
428,211,793✔
1068
  handle.pMsgCb = &pVnode->msgCb;
428,211,793✔
1069
  handle.pWorkerCb = pInfo->workerCb;
428,156,306✔
1070
  initStorageAPI(&handle.api);
428,158,412✔
1071
  int32_t code = TSDB_CODE_SUCCESS;
427,902,638✔
1072
  bool    redirected = false;
427,902,638✔
1073

1074
  switch (pMsg->msgType) {
427,902,638✔
1075
    case TDMT_SCH_QUERY:
301,157,355✔
1076
      if (!syncIsReadyForRead(pVnode->sync)) {
301,157,355✔
1077
        pMsg->code = (terrno) ? terrno : TSDB_CODE_SYN_NOT_LEADER;
1,484,551✔
1078
        redirected = true;
1,484,551✔
1079
      }
1080
      code = qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
301,225,362✔
1081
      if (redirected) {
301,198,027✔
1082
        vnodeRedirectRpcMsg(pVnode, pMsg, pMsg->code);
1,484,551✔
1083
        return 0;
1,484,551✔
1084
      }
1085

1086
      return code;
299,713,476✔
1087
    case TDMT_SCH_MERGE_QUERY:
61,308,995✔
1088
      return qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
61,308,995✔
1089
    case TDMT_SCH_QUERY_CONTINUE:
51,811,155✔
1090
      return qWorkerProcessCQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
51,811,155✔
1091
    case TDMT_VND_TMQ_CONSUME:
13,789,058✔
1092
      return tqProcessPollReq(pVnode->pTq, pMsg);
13,789,058✔
UNCOV
1093
    default:
×
UNCOV
1094
      vError("unknown msg type:%d in query queue", pMsg->msgType);
×
UNCOV
1095
      return TSDB_CODE_APP_ERROR;
×
1096
  }
1097
}
1098

1099
int32_t vnodeProcessFetchMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
1,098,724,231✔
1100
  int32_t code = TSDB_CODE_SUCCESS;
1,098,724,231✔
1101
  const STraceId *trace = &pMsg->info.traceId;
1,098,724,231✔
1102
  vDebug("vgId:%d, msg:%s, %p in fetch queue is processing, %" PRIx64 ":%" PRIx64, 
1,098,795,515✔
1103
    pVnode->config.vgId, TMSG_INFO(pMsg->msgType), pMsg, TRACE_GET_ROOTID(trace), TRACE_GET_MSGID(trace));
1104
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
1,098,985,351✔
1105
       pMsg->msgType == TDMT_VND_BATCH_META || pMsg->msgType == TDMT_VND_TABLE_NAME ||
760,312,348✔
1106
       pMsg->msgType == TDMT_VND_VSUBTABLES_META || pMsg->msgType == TDMT_VND_VSTB_REF_DBS) &&
730,104,624✔
1107
      !syncIsReadyForRead(pVnode->sync)) {
368,854,039✔
1108
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
501,020✔
1109
    return 0;
501,020✔
1110
  }
1111

1112
  switch (pMsg->msgType) {
1,098,474,173✔
1113
    case TDMT_SCH_FETCH:
516,439,121✔
1114
    case TDMT_SCH_MERGE_FETCH:
1115
      return qWorkerProcessFetchMsg(pVnode, pVnode->pQuery, pMsg, 0);
516,439,121✔
UNCOV
1116
    case TDMT_SCH_FETCH_RSP:
×
UNCOV
1117
      return qWorkerProcessRspMsg(pVnode, pVnode->pQuery, pMsg, 0);
×
1118
    // case TDMT_SCH_CANCEL_TASK:
1119
    //   return qWorkerProcessCancelMsg(pVnode, pVnode->pQuery, pMsg, 0);
1120
    case TDMT_SCH_DROP_TASK:
362,349,350✔
1121
      return qWorkerProcessDropMsg(pVnode, pVnode->pQuery, pMsg, 0);
362,349,350✔
1122
    case TDMT_SCH_TASK_NOTIFY:
29,393✔
1123
      return qWorkerProcessNotifyMsg(pVnode, pVnode->pQuery, pMsg, 0);
29,393✔
1124
    case TDMT_SCH_QUERY_HEARTBEAT:
188,425,028✔
1125
      return qWorkerProcessHbMsg(pVnode, pVnode->pQuery, pMsg, 0);
188,425,028✔
1126
    case TDMT_VND_TABLE_META:
1,249,359✔
1127
    case TDMT_VND_TABLE_NAME:
1128
      return vnodeGetTableMeta(pVnode, pMsg, true);
1,249,359✔
UNCOV
1129
    case TDMT_VND_TABLE_CFG:
×
UNCOV
1130
      return vnodeGetTableCfg(pVnode, pMsg, true);
×
1131
    case TDMT_VND_BATCH_META: {
29,698,541✔
1132
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.fetch_batch_meta_time, METRIC_LEVEL_LOW,
29,698,541✔
1133
                           { code = vnodeGetBatchMeta(pVnode, pMsg); });
1134
      METRICS_UPDATE(pVnode->writeMetrics.fetch_batch_meta_count, METRIC_LEVEL_LOW, 1);
29,713,214✔
1135
      return code;
29,702,091✔
1136
    }
1137
    case TDMT_VND_VSUBTABLES_META:
×
UNCOV
1138
      return vnodeGetVSubtablesMeta(pVnode, pMsg);
×
UNCOV
1139
    case TDMT_VND_VSTB_REF_DBS:
×
UNCOV
1140
      return vnodeGetVStbRefDbs(pVnode, pMsg);
×
1141
    case TDMT_VND_QUERY_SCAN_PROGRESS:
414✔
1142
      return vnodeQueryScanProgress(pVnode, pMsg);
414✔
1143
#ifdef TD_ENTERPRISE
1144
    case TDMT_VND_QUERY_COMPACT_PROGRESS:
117,683✔
1145
      return vnodeQueryCompactProgress(pVnode, pMsg);
117,683✔
1146

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

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

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

1176
  tstrncpy(pMetaRsp->dbFName, pVnode->config.dbname, TSDB_DB_FNAME_LEN);
73,661,629✔
1177
  pMetaRsp->dbId = pVnode->config.dbId;
73,662,953✔
1178
  pMetaRsp->vgId = TD_VID(pVnode);
73,663,118✔
1179
  pMetaRsp->precision = pVnode->config.tsdbCfg.precision;
73,663,947✔
1180
}
1181

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

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

1190
  int32_t     code = 0;
77,086✔
1191
  SVTrimDbReq trimReq = {0};
77,086✔
1192

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

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

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

1204
_exit:
77,250✔
1205
  return code;
77,250✔
1206
}
1207

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1411
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
5,277,936✔
1412
  pRsp->code = TSDB_CODE_SUCCESS;
5,278,677✔
1413
  pRsp->pCont = NULL;
5,278,139✔
1414
  pRsp->contLen = 0;
5,278,936✔
1415

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

1419
  code = tDecodeSVCreateStbReq(&coder, &req);
5,278,421✔
1420
  if (code) {
5,256,260✔
UNCOV
1421
    pRsp->code = code;
×
UNCOV
1422
    goto _err;
×
1423
  }
1424

1425
  code = metaCreateSuperTable(pVnode->pMeta, ver, &req);
5,256,260✔
1426
  if (code) {
5,280,072✔
1427
    pRsp->code = code;
3,008✔
1428
    goto _err;
3,008✔
1429
  }
1430

1431
  tDecoderClear(&coder);
5,277,064✔
1432
  return 0;
5,276,672✔
1433

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

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

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

1465
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(cRsp));
46,063,766✔
1466
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
46,066,205✔
1467
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
46,060,626✔
1468
  if (rsp.pArray == NULL || tbUids == NULL || tbNames == NULL) {
46,060,657✔
1469
    rcode = -1;
3,627✔
1470
    terrno = TSDB_CODE_OUT_OF_MEMORY;
3,627✔
UNCOV
1471
    goto _exit;
×
1472
  }
1473

1474
  // loop to create table
1475
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
100,415,196✔
1476
    pCreateReq = req.pReqs + iReq;
54,347,203✔
1477
    memset(&cRsp, 0, sizeof(cRsp));
54,345,056✔
1478

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

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

1507
    // do create table
1508
    if (metaCreateTable2(pVnode->pMeta, ver, pCreateReq, &cRsp.pMeta) < 0) {
54,328,532✔
1509
      if (pCreateReq->flags & TD_CREATE_IF_NOT_EXISTS && terrno == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
15,969✔
1510
        cRsp.code = TSDB_CODE_SUCCESS;
4,064✔
1511
      } else {
1512
        cRsp.code = terrno;
11,905✔
1513
      }
1514
    } else {
1515
      cRsp.code = TSDB_CODE_SUCCESS;
54,341,870✔
1516
      if (taosArrayPush(tbUids, &pCreateReq->uid) == NULL) {
108,683,246✔
UNCOV
1517
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
1518
        rcode = -1;
×
UNCOV
1519
        goto _exit;
×
1520
      }
1521
      vnodeUpdateMetaRsp(pVnode, cRsp.pMeta);
54,341,376✔
1522
    }
1523

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

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

1536
  // prepare rsp
1537
  int32_t ret = 0;
46,072,379✔
1538
  tEncodeSize(tEncodeSVCreateTbBatchRsp, &rsp, pRsp->contLen, ret);
46,072,379✔
1539
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
46,066,685✔
1540
  if (pRsp->pCont == NULL) {
46,061,746✔
UNCOV
1541
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
1542
    rcode = -1;
×
UNCOV
1543
    goto _exit;
×
1544
  }
1545
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
46,063,836✔
1546
  if (tEncodeSVCreateTbBatchRsp(&encoder, &rsp) != 0) {
46,058,461✔
UNCOV
1547
    vError("vgId:%d, failed to encode create table batch response", TD_VID(pVnode));
×
1548
  }
1549

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

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

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

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

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

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

1581
_exit:
46,058,503✔
1582
  tDeleteSVCreateTbBatchReq(&req);
46,059,204✔
1583
  taosArrayDestroyEx(rsp.pArray, tFreeSVCreateTbRsp);
46,062,282✔
1584
  taosArrayDestroy(tbUids);
46,049,961✔
1585
  tDecoderClear(&decoder);
46,048,764✔
1586
  tEncoderClear(&encoder);
46,068,080✔
1587
  taosArrayDestroyP(tbNames, NULL);
46,065,346✔
1588
  return rcode;
46,053,728✔
1589
}
1590

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

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

1601
  tDecoderInit(&dc, pReq, len);
7,899,547✔
1602

1603
  // decode req
1604
  code = tDecodeSVCreateStbReq(&dc, &req);
7,898,989✔
1605
  if (code) {
7,889,761✔
UNCOV
1606
    tDecoderClear(&dc);
×
UNCOV
1607
    return code;
×
1608
  }
1609

1610
  code = metaAlterSuperTable(pVnode->pMeta, ver, &req);
7,889,761✔
1611
  if (code) {
7,897,832✔
UNCOV
1612
    pRsp->code = code;
×
1613
    tDecoderClear(&dc);
×
1614
    return code;
×
1615
  }
1616

1617
  tDecoderClear(&dc);
7,897,832✔
1618

1619
  return 0;
7,899,547✔
1620
}
1621

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

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

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

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

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

1644
  // process request
1645
  tbUidList = taosArrayInit(8, sizeof(int64_t));
920,264✔
1646
  if (tbUidList == NULL) goto _exit;
919,677✔
1647
  if (metaDropSuperTable(pVnode->pMeta, ver, &req) < 0) {
919,677✔
1648
    rcode = terrno;
1,504✔
1649
    goto _exit;
1,504✔
1650
  }
1651

1652
  if (tqDeleteTbUidList(pVnode->pTq, tbUidList) < 0) {
918,760✔
UNCOV
1653
    rcode = terrno;
×
UNCOV
1654
    goto _exit;
×
1655
  }
1656

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

1667
SArray* getCidList(const SArray* tags) {
7,812,327✔
1668
  int32_t       code = 0;
7,812,327✔
1669
  int32_t       lino = 0;
7,812,327✔
1670
  SArray* cidList = taosArrayInit(taosArrayGetSize(tags), sizeof(col_id_t));
7,812,327✔
1671
  QUERY_CHECK_NULL(cidList, code, lino, end, terrno);
7,812,327✔
1672
  for (int32_t i = 0; i < taosArrayGetSize(tags); i++) {
15,683,938✔
1673
    SUpdatedTagVal *pTagVal = taosArrayGet(tags, i);
7,871,611✔
1674
    QUERY_CHECK_NULL(pTagVal, code, lino, end, terrno);
7,871,611✔
1675
    col_id_t cid = pTagVal->colId;
7,871,611✔
1676
    QUERY_CHECK_NULL(taosArrayPush(cidList, &cid), code, lino, end, terrno);
7,871,611✔
1677
  }
1678

1679
end:
7,812,327✔
1680
  if (code != 0) {
7,812,327✔
UNCOV
1681
    taosArrayDestroy(cidList);
×
UNCOV
1682
    return NULL;
×
1683
  }
1684
  return cidList;
7,812,327✔
1685
}
1686

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

1699
  if (tagsArray != NULL) {
7,801,234✔
1700
    cidListArray = taosArrayInit(taosArrayGetSize(tagsArray), sizeof(SArray*));
7,779,126✔
1701
    QUERY_CHECK_NULL(cidListArray, code, lino, end, terrno);
7,779,126✔
1702

1703
    for (int32_t i = 0; i < taosArrayGetSize(tagsArray); i++) {
15,569,345✔
1704
      SArray   *obj = taosArrayGetP(tagsArray, i);
7,790,219✔
1705
      cids  = getCidList(obj);
7,790,219✔
1706
      QUERY_CHECK_NULL(cids, code, lino, end, terrno);
7,790,219✔
1707
      QUERY_CHECK_NULL(taosArrayPush(cidListArray, &cids), code, lino, end, terrno);
7,790,219✔
1708
      cids = NULL;
7,790,219✔
1709
    }
1710
  }
1711
  vDebug("vgId:%d, try to add %d tables in query table list, cidList size:%"PRIzu,
7,801,234✔
1712
         TD_VID(pVnode), (int32_t)taosArrayGetSize(tbUidList), taosArrayGetSize(cidList));
1713
  code = tqUpdateTbUidList(pVnode->pTq, tbUidList, cidList, cidListArray);
7,801,234✔
1714
  QUERY_CHECK_CODE(code, lino, end);
7,801,234✔
1715

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

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

1736
  pRsp->msgType = TDMT_VND_ALTER_TABLE_RSP;
12,393,897✔
1737
  pRsp->pCont = NULL;
12,393,897✔
1738
  pRsp->contLen = 0;
12,393,897✔
1739
  pRsp->code = TSDB_CODE_SUCCESS;
12,393,897✔
1740

1741
  tDecoderInit(&dc, pReq, len);
12,393,897✔
1742

1743
  // decode
1744
  if (tDecodeSVAlterTbReq(&dc, &vAlterTbReq) < 0) {
12,393,897✔
UNCOV
1745
    vAlterTbRsp.code = TSDB_CODE_INVALID_MSG;
×
UNCOV
1746
    tDecoderClear(&dc);
×
UNCOV
1747
    goto _exit;
×
1748
  }
1749

1750
  // process
1751
  if (metaAlterTable(pVnode->pMeta, ver, &vAlterTbReq, &vMetaRsp) < 0) {
12,393,897✔
1752
    vAlterTbRsp.code = terrno;
584,356✔
1753
    tDecoderClear(&dc);
584,356✔
1754
    goto _exit;
584,356✔
1755
  }
1756
  tDecoderClear(&dc);
11,809,541✔
1757

1758
  if (NULL != vMetaRsp.pSchemas) {
11,809,541✔
1759
    vnodeUpdateMetaRsp(pVnode, &vMetaRsp);
3,802,600✔
1760
    vAlterTbRsp.pMeta = &vMetaRsp;
3,802,600✔
1761
  }
1762

1763
_exit:
12,393,681✔
1764
  destroyAlterTbReq(&vAlterTbReq);
12,393,897✔
1765
  tEncodeSize(tEncodeSVAlterTbRsp, &vAlterTbRsp, pRsp->contLen, ret);
12,393,284✔
1766
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
12,393,897✔
1767
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
12,393,897✔
1768
  if (tEncodeSVAlterTbRsp(&ec, &vAlterTbRsp) != 0) {
12,393,897✔
UNCOV
1769
    vError("vgId:%d, failed to encode alter table response", TD_VID(pVnode));
×
1770
  }
1771

1772
  tEncoderClear(&ec);
12,393,897✔
1773
  if (vMetaRsp.pSchemas) {
12,393,897✔
1774
    taosMemoryFree(vMetaRsp.pSchemas);
3,802,600✔
1775
    taosMemoryFree(vMetaRsp.pSchemaExt);
3,802,600✔
1776
  }
1777
  if (vMetaRsp.pColRefs) {
12,393,897✔
1778
    taosMemoryFree(vMetaRsp.pColRefs);
276,092✔
1779
  }
1780
  return 0;
12,393,897✔
1781
}
1782

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

1794
  pRsp->msgType = ((SRpcMsg *)pReq)->msgType + 1;
1,736,280✔
1795
  pRsp->pCont = NULL;
1,736,280✔
1796
  pRsp->contLen = 0;
1,736,280✔
1797
  pRsp->code = TSDB_CODE_SUCCESS;
1,736,280✔
1798

1799
  // decode req
1800
  tDecoderInit(&decoder, pReq, len);
1,736,280✔
1801
  ret = tDecodeSVDropTbBatchReq(&decoder, &req);
1,736,280✔
1802
  if (ret < 0) {
1,736,280✔
UNCOV
1803
    terrno = TSDB_CODE_INVALID_MSG;
×
UNCOV
1804
    pRsp->code = terrno;
×
UNCOV
1805
    goto _exit;
×
1806
  }
1807

1808
  // process req
1809
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
1,736,280✔
1810
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(SVDropTbRsp));
1,736,280✔
1811
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
1,736,280✔
1812
  if (tbUids == NULL || rsp.pArray == NULL || tbNames == NULL) goto _exit;
1,736,280✔
1813

1814
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
3,519,386✔
1815
    SVDropTbReq *pDropTbReq = req.pReqs + iReq;
1,783,106✔
1816
    SVDropTbRsp  dropTbRsp = {0};
1,783,106✔
1817
    tb_uid_t     tbUid = 0;
1,783,106✔
1818

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

1831
    if (taosArrayPush(rsp.pArray, &dropTbRsp) == NULL) {
3,566,212✔
UNCOV
1832
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1833
      pRsp->code = terrno;
×
1834
      goto _exit;
×
1835
    }
1836

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

1852
  if (tqDeleteTbUidList(pVnode->pTq, tbUids) < 0) {
1,736,280✔
UNCOV
1853
    vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(terrno));
×
1854
  }
1855

1856
  if (tsEnableAudit && tsEnableAuditCreateTable && tsAuditLevel >= AUDIT_LEVEL_CHILDTABLE) {
1,736,280✔
1857
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
×
1858

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

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

1874
    size_t len = 0;
×
UNCOV
1875
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
×
1876

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

1884
    taosStringBuilderDestroy(&sb);
×
1885
  }
1886

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

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

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

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

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

1952
  tInitSubmitBlkIter(msgIter, pBlock, &blkIter);
1953
  if (blkIter.row == NULL) return 0;
1954

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

1962
  suid = msgIter->suid;
1963
  rv = TD_ROW_SVER(blkIter.row);
1964

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

1971
  taosMemoryFreeClear(pSchema);
1972

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

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

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

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

2025
  return TSDB_CODE_SUCCESS;
×
2026
}
2027

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

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

UNCOV
2041
  if (tdValTypeIsNull(pCellVal->valType)) {
×
2042
    pColVal->flag = CV_FLAG_NULL;
×
2043
    return TSDB_CODE_SUCCESS;
×
2044
  }
2045

2046
  if (IS_VAR_DATA_TYPE(pCol->type)) {
×
2047
    if (IS_STR_DATA_BLOB(pCol->type)) {
×
2048
      pColVal->value.nData = blobDataLen(pCellVal->val);
×
2049
      pColVal->value.pData = (uint8_t *)blobDataVal(pCellVal->val);
×
2050

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

2064
  pColVal->flag = CV_FLAG_VALUE;
×
2065
  return TSDB_CODE_SUCCESS;
×
2066
}
2067

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

2082
static int32_t vnodeDecodeCreateTbReq(SSubmitReqConvertCxt *pCxt) {
×
2083
  if (pCxt->msgIter.schemaLen <= 0) {
×
2084
    return TSDB_CODE_SUCCESS;
×
2085
  }
2086

2087
  pCxt->pTbData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
×
UNCOV
2088
  if (NULL == pCxt->pTbData->pCreateTbReq) {
×
UNCOV
2089
    return terrno;
×
2090
  }
2091

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

UNCOV
2097
  return code;
×
2098
}
2099

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

UNCOV
2106
  SSubmitReqConvertCxt cxt = {0};
×
2107

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

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

2140
  vnodeDestroySubmitReqConvertCxt(&cxt);
×
2141
  return code;
×
2142
}
2143

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

2167
static int32_t buildExistSubTalbeRsp(SVnode *pVnode, SSubmitTbData *pSubmitTbData, STableMetaRsp **ppRsp) {
62,960✔
2168
  int32_t code = 0;
62,960✔
2169
  int32_t lino = 0;
62,960✔
2170

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

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

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

2226
static int32_t buildExistNormalTalbeRsp(SVnode *pVnode, int64_t uid, STableMetaRsp **ppRsp) {
27,740✔
2227
  int32_t code = 0;
27,740✔
2228
  int32_t lino = 0;
27,740✔
2229

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

2242
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
27,740✔
2243
  if (NULL == *ppRsp) {
27,740✔
2244
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2245
  }
2246

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

2267
_exit:
27,740✔
2268
  metaFetchEntryFree(&pEntry);
27,740✔
2269
  if (code != TSDB_CODE_SUCCESS) {
27,740✔
2270
    vError("vgId:%d, failed to build exist normal table response, code:%d, line:%d", TD_VID(pVnode), code, lino);
×
2271
  }
2272
  return code;
27,740✔
2273
}
2274

2275
static int32_t buildExistTableInStreamRsp(SVnode *pVnode, SSubmitTbData *pSubmitTbData, STableMetaRsp **ppRsp) {
288,907✔
2276
  if (pSubmitTbData->pCreateTbReq->flags & TD_CREATE_NORMAL_TB_IN_STREAM) {
288,907✔
2277
    int32_t code = buildExistNormalTalbeRsp(pVnode, pSubmitTbData->uid, ppRsp);
27,740✔
2278
    if (code) {
27,740✔
UNCOV
2279
      vError("vgId:%d, table uid:%" PRId64 " not exists, line:%d", TD_VID(pVnode), pSubmitTbData->uid, __LINE__);
×
UNCOV
2280
      return code;
×
2281
    }
2282
    return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
27,740✔
2283
  } else if (pSubmitTbData->pCreateTbReq->flags & TD_CREATE_SUB_TB_IN_STREAM) {
261,167✔
2284
    return buildExistSubTalbeRsp(pVnode, pSubmitTbData, ppRsp);
62,960✔
2285
  }
2286
  return TSDB_CODE_SUCCESS;
198,207✔
2287
}
2288

2289
static int32_t vnodeHandleAutoCreateTable(SVnode      *pVnode,    // vnode
600,265,959✔
2290
                                          int64_t      version,   // version
2291
                                          SSubmitReq2 *pRequest,  // request
2292
                                          SSubmitRsp2 *pResponse  // response
2293
) {
2294
  int32_t code = TSDB_CODE_SUCCESS;
600,265,959✔
2295
  int32_t numTbData = taosArrayGetSize(pRequest->aSubmitTbData);
600,265,959✔
2296
  SArray *newTbUids = NULL;
600,266,701✔
2297

2298
  for (int32_t i = 0; i < numTbData; ++i) {
1,225,763,820✔
2299
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
625,529,629✔
2300

2301
    if (pTbData->pCreateTbReq == NULL) {
625,530,594✔
2302
      continue;
609,672,835✔
2303
    }
2304

2305
    pTbData->uid = pTbData->pCreateTbReq->uid;
15,856,787✔
2306

2307
    // Alloc necessary resources
2308
    if (pResponse->aCreateTbRsp == NULL) {
15,856,081✔
2309
      pResponse->aCreateTbRsp = taosArrayInit(numTbData, sizeof(SVCreateTbRsp));
12,995,311✔
2310
      if (pResponse->aCreateTbRsp == NULL) {
12,995,542✔
UNCOV
2311
        code = terrno;
×
UNCOV
2312
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2313
               tstrerror(code), version);
UNCOV
2314
        taosArrayDestroy(newTbUids);
×
UNCOV
2315
        return code;
×
2316
      }
2317
    }
2318

2319
    // Do create table
2320
    vDebug("vgId:%d start to handle auto create table, version:%" PRId64, TD_VID(pVnode), version);
15,856,787✔
2321

2322
    SVCreateTbRsp *pCreateTbRsp = taosArrayReserve(pResponse->aCreateTbRsp, 1);
15,856,892✔
2323
    code = metaCreateTable2(pVnode->pMeta, version, pTbData->pCreateTbReq, &pCreateTbRsp->pMeta);
15,856,682✔
2324
    if (code == TSDB_CODE_SUCCESS) {
15,856,913✔
2325
      // Allocate necessary resources
2326
      if (newTbUids == NULL) {
15,520,306✔
2327
        newTbUids = taosArrayInit(numTbData, sizeof(int64_t));
12,706,715✔
2328
        if (newTbUids == NULL) {
12,705,705✔
UNCOV
2329
          code = terrno;
×
2330
          vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2331
                 tstrerror(code), version);
UNCOV
2332
          return code;
×
2333
        }
2334
      }
2335

2336
      if (taosArrayPush(newTbUids, &pTbData->uid) == NULL) {
31,039,602✔
UNCOV
2337
        code = terrno;
×
UNCOV
2338
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2339
               tstrerror(code), version);
UNCOV
2340
        taosArrayDestroy(newTbUids);
×
UNCOV
2341
        return code;
×
2342
      }
2343

2344
      if (pCreateTbRsp->pMeta) {
15,520,306✔
2345
        vnodeUpdateMetaRsp(pVnode, pCreateTbRsp->pMeta);
15,519,971✔
2346
      }
2347
    } else if (code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
336,607✔
2348
      code = terrno = 0;
335,551✔
2349
      pTbData->uid = pTbData->pCreateTbReq->uid;  // update uid if table exist for using below
335,656✔
2350

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

2372
  // Update the affected table uid list
2373
  if (taosArrayGetSize(newTbUids) > 0) {
600,234,191✔
2374
    vDebug("vgId:%d, add %d table into query table list in handling submit", TD_VID(pVnode),
12,705,704✔
2375
           (int32_t)taosArrayGetSize(newTbUids));
2376
    if (tqAddTbUidList(pVnode->pTq, newTbUids) != 0) {
12,706,039✔
UNCOV
2377
      vError("vgId:%d, failed to update tbUid list", TD_VID(pVnode));
×
2378
    }
2379
  }
2380

2381
  vDebug("vgId:%d, handle auto create table done, version:%" PRId64, TD_VID(pVnode), version);
600,235,789✔
2382

2383
  taosArrayDestroy(newTbUids);
600,236,417✔
2384
  return code;
600,234,904✔
2385
}
2386

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

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

2425
static int32_t vnodeHandleDataWrite(SVnode *pVnode, int64_t version, SSubmitReq2 *pRequest, SSubmitRsp2 *pResponse) {
600,236,351✔
2426
  int32_t code = TSDB_CODE_SUCCESS;
600,236,351✔
2427
  int32_t numTbData = taosArrayGetSize(pRequest->aSubmitTbData);
600,236,351✔
2428
  int8_t  hasBlob = 0;
600,239,829✔
2429

2430
  // Scan submit data
2431
  for (int32_t i = 0; i < numTbData; ++i) {
1,225,713,237✔
2432
    SMetaInfo      info = {0};
625,485,888✔
2433
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
625,483,321✔
2434

2435
    if (pTbData->flags & SUBMIT_REQ_WITH_BLOB) {
625,482,210✔
2436
      hasBlob = 1;
45,726✔
2437
    }
2438
    if (pTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
625,484,692✔
2439
      continue;  // skip column data format
6,837,038✔
2440
    }
2441
    if (pTbData->flags & SUBMIT_REQ_ONLY_CREATE_TABLE) {
618,648,760✔
2442
      continue;  // skip only crate table request
2,119,141✔
2443
    }
2444

2445
    code = metaGetInfo(pVnode->pMeta, pTbData->uid, &info, NULL);
616,526,730✔
2446
    if (code) {
616,523,948✔
2447
      code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
1,304✔
2448
      vWarn("vgId:%d, error occurred at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __FILE__,
1,304✔
2449
            __LINE__, tstrerror(code), version, pTbData->uid);
2450
      return code;
1,304✔
2451
    }
2452

2453
    if (info.suid != pTbData->suid) {
616,522,644✔
UNCOV
2454
      code = TSDB_CODE_INVALID_MSG;
×
2455
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64 " suid:%" PRId64
×
2456
             " info.suid:%" PRId64,
2457
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), version, pTbData->uid, pTbData->suid,
2458
             info.suid);
UNCOV
2459
      return code;
×
2460
    }
2461

2462
    if (info.suid) {
616,521,974✔
2463
      code = metaGetInfo(pVnode->pMeta, info.suid, &info, NULL);
470,080,905✔
2464
      if (code) {
470,092,489✔
UNCOV
2465
        code = TSDB_CODE_INTERNAL_ERROR;
×
UNCOV
2466
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " suid:%" PRId64, TD_VID(pVnode), __func__,
×
2467
               __FILE__, __LINE__, tstrerror(code), version, info.suid);
UNCOV
2468
        return code;
×
2469
      }
2470
    }
2471

2472
    if (pTbData->sver != info.skmVer) {
616,533,558✔
2473
      code = TSDB_CODE_TDB_INVALID_TABLE_SCHEMA_VER;
460✔
2474
      addExistTableInfoIntoRes(pVnode, pRequest, pResponse, pTbData, numTbData);
460✔
2475
      vDebug("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64
460✔
2476
             " sver:%d"
2477
             " info.skmVer:%d",
2478
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), version, pTbData->uid, pTbData->sver,
2479
             info.skmVer);
2480
      return code;
460✔
2481
    }
2482
  }
2483

2484
  // Do write data
2485
  vDebug("vgId:%d start to handle data write, version:%" PRId64, TD_VID(pVnode), version);
600,227,349✔
2486

2487
  for (int32_t i = 0; i < numTbData; ++i) {
1,225,651,738✔
2488
    int32_t        affectedRows = 0;
625,494,717✔
2489
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
625,491,625✔
2490

2491
    if (pTbData->flags & SUBMIT_REQ_ONLY_CREATE_TABLE) {
625,491,402✔
2492
      continue;
2,119,141✔
2493
    }
2494

2495
    if (hasBlob) {
623,371,438✔
2496
      code = vnodeSubmitBlobData(pVnode, pTbData);
45,726✔
2497
      if (code) {
45,726✔
2498
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2499
               __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
UNCOV
2500
        return code;
×
2501
      }
2502
    }
2503

2504
    code = tsdbInsertTableData(pVnode->pTsdb, version, pTbData, &affectedRows);
623,371,438✔
2505
    if (code) {
623,332,397✔
UNCOV
2506
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2507
             __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
UNCOV
2508
      return code;
×
2509
    }
2510

2511
    code = metaUpdateChangeTimeWithLock(pVnode->pMeta, pTbData->uid, pTbData->ctimeMs);
623,332,397✔
2512
    if (code) {
623,278,164✔
UNCOV
2513
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2514
             __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
UNCOV
2515
      return code;
×
2516
    }
2517
    pResponse->affectedRows += affectedRows;
623,278,164✔
2518
  }
2519

2520
  vDebug("vgId:%d, handle data write done, version:%" PRId64 ", affectedRows:%d", TD_VID(pVnode), version,
600,157,021✔
2521
         pResponse->affectedRows);
2522
  return code;
600,236,683✔
2523
}
2524

2525
static int32_t vnodeScanColumnData(SVnode *pVnode, SSubmitTbData *pTbData, TSKEY minKey, TSKEY maxKey) {
6,835,004✔
2526
  int32_t code = 0;
6,835,004✔
2527

2528
  int32_t   numCols = taosArrayGetSize(pTbData->aCol);
6,835,004✔
2529
  SColData *aColData = (SColData *)TARRAY_DATA(pTbData->aCol);
6,835,337✔
2530

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

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

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

2559
  SRowKey *pLastKey = NULL;
6,836,259✔
2560
  SRowKey  lastKey = {0};
6,836,259✔
2561
  for (int32_t i = 0; i < aColData[0].nVal; ++i) {
2,147,483,647✔
2562
    SRowKey key = {0};
2,147,483,647✔
2563

2564
    tColDataArrGetRowKey(aColData, numCols, i, &key);
2,147,483,647✔
2565

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

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

2586
    *pLastKey = key;
2,147,483,647✔
2587
  }
2588

2589
  return code;
6,837,038✔
2590
}
2591

2592
static int32_t vnodeScanSubmitRowData(SVnode *pVnode, SSubmitTbData *pTbData, TSKEY minKey, TSKEY maxKey) {
618,653,240✔
2593
  int32_t code = 0;
618,653,240✔
2594

2595
  int32_t numRows = taosArrayGetSize(pTbData->aRowP);
618,653,240✔
2596
  SRow  **aRow = (SRow **)TARRAY_DATA(pTbData->aRowP);
618,626,595✔
2597

2598
  if (numRows <= 0 && (pTbData->flags & SUBMIT_REQ_ONLY_CREATE_TABLE) == 0) {
618,643,166✔
UNCOV
2599
    code = TSDB_CODE_INVALID_MSG;
×
2600
    vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " numRows:%d", TD_VID(pVnode), __func__,
×
2601
           __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, numRows);
UNCOV
2602
    return code;
×
2603
  }
2604

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

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

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

2639
    *pLastKey = key;
2,147,483,647✔
2640
  }
2641

2642
  return code;
611,126,155✔
2643
}
2644

2645
static int32_t vnodeScanSubmitReq(SVnode *pVnode, int64_t version, SSubmitReq2 *pRequest, SSubmitRsp2 *pResponse) {
600,225,173✔
2646
  int32_t code = TSDB_CODE_SUCCESS;
600,225,173✔
2647
  int32_t numTbData = taosArrayGetSize(pRequest->aSubmitTbData);
600,225,173✔
2648

2649
  TSKEY now = taosGetTimestamp(pVnode->config.tsdbCfg.precision);
600,229,089✔
2650
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * pVnode->config.tsdbCfg.keep2;
600,235,984✔
2651
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
600,228,408✔
2652
  for (int32_t i = 0; i < numTbData; i++) {
1,225,699,116✔
2653
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
625,487,940✔
2654

2655
    if (pTbData->pCreateTbReq && pTbData->pCreateTbReq->uid == 0) {
625,492,265✔
UNCOV
2656
      code = TSDB_CODE_INVALID_MSG;
×
2657
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2658
             tstrerror(code), version);
UNCOV
2659
      return code;
×
2660
    }
2661

2662
    if (pTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
625,494,062✔
2663
      code = vnodeScanColumnData(pVnode, pTbData, minKey, maxKey);
6,835,022✔
2664
      if (code) {
6,837,038✔
UNCOV
2665
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2666
               __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
UNCOV
2667
        return code;
×
2668
      }
2669
    } else {
2670
      code = vnodeScanSubmitRowData(pVnode, pTbData, minKey, maxKey);
618,646,759✔
2671
      if (code) {
618,621,258✔
2672
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
5✔
2673
               __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
UNCOV
2674
        return code;
×
2675
      }
2676
    }
2677
  }
2678

2679
  return code;
600,211,176✔
2680
}
2681

2682
static int32_t vnodeDecodeAuditRecord(const SJson *pJson, void *pObj) {
627✔
2683
  SAuditRecord *record = (SAuditRecord *)pObj;
627✔
2684
  int32_t       code = 0;
627✔
2685

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

2707
  return 0;
627✔
2708
}
2709

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

2715
  if (tname == NULL || sname == NULL) {
57✔
UNCOV
2716
    return TSDB_CODE_INVALID_PARA;
×
2717
  }
2718

2719
  pTbReq->name = taosStrdup(tname);
57✔
2720
  if (!pTbReq->name) {
57✔
UNCOV
2721
    code = terrno;
×
UNCOV
2722
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
2723
  }
2724

2725
  pTbReq->ctb.stbName = taosStrdup(sname);
57✔
2726
  if (!pTbReq->ctb.stbName) {
57✔
UNCOV
2727
    code = terrno;
×
UNCOV
2728
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
2729
  }
2730

2731
  pTbReq->type = TD_CHILD_TABLE;
57✔
2732
  pTbReq->ctb.suid = suid;
57✔
2733
  pTbReq->ctb.tagNum = tagNum;
57✔
2734
  pTbReq->ttl = ttl;
57✔
2735
  pTbReq->commentLen = -1;
57✔
2736
  pTbReq->ctb.tagName = tagName;
57✔
2737
  pTbReq->ctb.pTag = (uint8_t *)pTag;
57✔
2738

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

2753
static int32_t vnodePrepareCreateTb(SVCreateTbReq *pTbReq, char *tbName, int64_t suid, SSchemaWrapper *pTagSchema,
57✔
2754
                                    SAuditRecord *record) {
2755
  int32_t code = 0;
57✔
2756
  int32_t lino = 0;
57✔
2757

2758
  SArray *TagNames = NULL;
57✔
2759
  SArray *pTagVals = NULL;
57✔
2760
  STag   *pTag = NULL;
57✔
2761

2762
  SSchema *tSchema = &pTagSchema->pSchema[0];
57✔
2763
  vTrace("schema name:%s", tSchema->name);
57✔
2764

2765
  TagNames = taosArrayInit(1, TSDB_COL_NAME_LEN);
57✔
2766
  if (!TagNames) {
57✔
UNCOV
2767
    code = terrno;
×
UNCOV
2768
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
2769
  }
2770

2771
  if (NULL == taosArrayPush(TagNames, tSchema->name)) {
114✔
2772
    code = terrno;
×
UNCOV
2773
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
2774
  }
2775

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

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

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

2813
  if (pTagVals != NULL) {
57✔
2814
    taosArrayDestroy(pTagVals);
57✔
2815
    pTagVals = NULL;
57✔
2816
  }
2817

2818
  return code;
57✔
2819
}
2820

2821
static SArray *vnodePrepareRow(SVnode *pVnode, STSchema *pSchema, SAuditRecord *record) {
627✔
2822
  int32_t code = 0;
627✔
2823
  int32_t lino = 0;
627✔
2824

2825
  SArray *aRows = NULL;
627✔
2826
  SArray *pVals = NULL;
627✔
2827
  SRow   *pRow = NULL;
627✔
2828

2829
  if (!(aRows = taosArrayInit(1, sizeof(SRow *)))) {
627✔
UNCOV
2830
    code = terrno;
×
UNCOV
2831
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
2832
  }
2833

2834
  if ((pVals = taosArrayInit(1, sizeof(SColVal))) == NULL) {
627✔
UNCOV
2835
    code = terrno;
×
UNCOV
2836
    goto _exit;
×
2837
  }
2838

2839
  for (int32_t k = 0; k < pSchema->numOfCols; ++k) {
6,270✔
2840
    const STColumn *pCol = &pSchema->columns[k];
5,643✔
2841
    vTrace("vgId:%d, schema column id:%d, type:%d", TD_VID(pVnode), pCol->colId, pCol->type);
5,643✔
2842

2843
    // colId is consistent with audit_columns in mndStb.c
2844
    void *data = NULL;
5,643✔
2845
    if (pCol->colId == 1) {
5,643✔
2846
      data = &record->curTime;
627✔
2847
    } else if (pCol->colId == 2) {
5,016✔
2848
      data = record->detail;
627✔
2849
    } else if (pCol->colId == 3) {
4,389✔
2850
      data = record->user;
627✔
2851
    } else if (pCol->colId == 4) {
3,762✔
2852
      data = record->operation;
627✔
2853
    } else if (pCol->colId == 5) {
3,135✔
2854
      data = record->target1;
627✔
2855
    } else if (pCol->colId == 6) {
2,508✔
2856
      data = record->target2;
627✔
2857
    } else if (pCol->colId == 7) {
1,881✔
2858
      data = record->clientAddress;
627✔
2859
    } else if (pCol->colId == 8) {
1,254✔
2860
      data = &record->duration;
627✔
2861
    } else if (pCol->colId == 9) {
627✔
2862
      data = &record->affectedRows;
627✔
2863
    } else {
UNCOV
2864
      vError("the column id %" PRIi16 " is not defined in audit record table", pCol->colId);
×
UNCOV
2865
      code = TSDB_CODE_APP_ERROR;
×
UNCOV
2866
      TAOS_CHECK_GOTO(code, &lino, _exit);
×
2867
    }
2868

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

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

2936
  SRowBuildScanInfo sinfo = {0};
627✔
2937
  if ((code = tRowBuild(pVals, pSchema, &pRow, &sinfo)) < 0) {
627✔
UNCOV
2938
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
2939
  }
2940
  if (NULL == taosArrayPush(aRows, &pRow)) {
627✔
2941
    code = terrno;
×
2942
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
2943
  }
2944
  taosArrayDestroy(pVals);
627✔
2945
  pVals = NULL;
627✔
2946

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

2959
static SArray *vnodePrepareSubmitTb(SVnode *pVnode, SAuditRecord *record, STSchema *pSchema, SSchemaWrapper *pTagSchema,
627✔
2960
                                    int64_t suid) {
2961
  int32_t code = 0;
627✔
2962
  int32_t lino = 0;
627✔
2963

2964
  SArray *aSubmitTbData = NULL;
627✔
2965
  SSubmitTbData tbData = {0};
627✔
2966

2967
  if (!(aSubmitTbData = taosArrayInit(1, sizeof(SSubmitTbData)))) {
627✔
UNCOV
2968
    code = terrno;
×
UNCOV
2969
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
2970
  }
2971

2972
  char tbName[TSDB_TABLE_NAME_LEN] = {0};
627✔
2973
  (void)tsnprintf(tbName, TSDB_TABLE_NAME_LEN, "t_operations_%s", record->strClusterId);
627✔
2974

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

2985
    tbData.uid = tGenIdPI64();
57✔
2986
    tbData.flags |= SUBMIT_REQ_AUTO_CREATE_TABLE;
57✔
2987

2988
    tbData.pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
57✔
2989

2990
    if (tbData.pCreateTbReq == NULL) {
57✔
UNCOV
2991
      code = terrno;
×
UNCOV
2992
      TAOS_CHECK_GOTO(code, &lino, _exit);
×
2993
    }
2994

2995
    tbData.pCreateTbReq->uid = tbData.uid;
57✔
2996
    tbData.pCreateTbReq->btime = taosGetTimestampMs();
114✔
2997

2998
    code = vnodePrepareCreateTb(tbData.pCreateTbReq, tbName, suid, pTagSchema, record);
57✔
2999

3000
    metaReaderClear(&merTb);
57✔
3001
    TAOS_CHECK_GOTO(code, &lino, _exit);
57✔
3002
  } else {
3003
    metaReaderClear(&merTb);
×
UNCOV
3004
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
3005
  }
3006

3007
  tbData.suid = suid;
627✔
3008
  tbData.sver = pSchema->version;
627✔
3009

3010
  tbData.aRowP = vnodePrepareRow(pVnode, pSchema, record);
627✔
3011
  if (tbData.aRowP == NULL) {
627✔
UNCOV
3012
    code = terrno;
×
3013
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
3014
  }
3015

3016
  if (NULL == taosArrayPush(aSubmitTbData, &tbData)) {
627✔
UNCOV
3017
    code = terrno;
×
3018
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
3019
  }
3020

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

3034
static int32_t vnodeSaveOneAuditRecord(SVnode *pVnode, int64_t ver, SJson *pJson, SSchemaWrapper *pTagSchema,
627✔
3035
                                       int64_t suid, STSchema *pSchema, SRpcMsg *pOriginalMsg) {
3036
  int32_t code = 0;
627✔
3037
  int32_t lino = 0;
627✔
3038
  terrno = 0;
627✔
3039

3040
  SAuditRecord record = {0};
627✔
3041
  SSubmitReq2 *pSubmitReq = NULL;
627✔
3042
  SSubmitRsp2 *pSubmitRsp = &(SSubmitRsp2){0};
627✔
3043

3044
  TAOS_CHECK_GOTO(vnodeDecodeAuditRecord(pJson, &record), &lino, _exit);
627✔
3045

3046
  vTrace("vgId:%d, start to audit operation:%s", TD_VID(pVnode), record.operation);
627✔
3047

3048
  if (!(pSubmitReq = taosMemoryCalloc(1, sizeof(SSubmitReq2)))) {
627✔
UNCOV
3049
    code = terrno;
×
UNCOV
3050
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
3051
  }
3052

3053
  pSubmitReq->aSubmitTbData = vnodePrepareSubmitTb(pVnode, &record, pSchema, pTagSchema, suid);
627✔
3054
  if (pSubmitReq->aSubmitTbData == NULL) {
627✔
UNCOV
3055
    code = terrno;
×
UNCOV
3056
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
3057
  }
3058

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

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

3075
  vTrace("vgId:%d, affectedRow:%d", TD_VID(pVnode), pSubmitRsp->affectedRows);
627✔
3076

3077
_exit:
627✔
3078
  if (code != 0)
627✔
UNCOV
3079
    vError("vgId:%d, failed to save one AuditRecord at line:%d, since %s", TD_VID(pVnode), lino, tstrerror(code));
×
3080

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

3089
  // update metrics
3090
  METRICS_UPDATE(pVnode->writeMetrics.total_requests, METRIC_LEVEL_LOW, 1);
627✔
3091
  METRICS_UPDATE(pVnode->writeMetrics.total_rows, METRIC_LEVEL_HIGH, pSubmitRsp->affectedRows);
627✔
3092
  // METRICS_UPDATE(pVnode->writeMetrics.total_bytes, METRIC_LEVEL_LOW, pMsg->header.contLen);
3093

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

3107
  // clear
3108
  if (pSubmitReq != NULL) {
627✔
3109
    tDestroySubmitReq(pSubmitReq, TSDB_MSG_FLG_ENCODE);
627✔
3110
    taosMemoryFree(pSubmitReq);
627✔
3111
  }
3112
  if (pSubmitRsp != NULL) {
627✔
3113
    tDestroySSubmitRsp2(pSubmitRsp, TSDB_MSG_FLG_ENCODE);
627✔
3114
  }
3115

3116
  return code;
627✔
3117
}
3118

3119
static int32_t vnodeProcessAuditRecordReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pOriginalMsg) {
228✔
3120
  int32_t code = 0;
228✔
3121
  int32_t lino = 0;
228✔
3122
  terrno = 0;
228✔
3123

3124
  SVAuditRecordReq req = {0};
228✔
3125
  if (tDeserializeSVAuditRecordReq(pReq, len, &req) != 0) {
228✔
UNCOV
3126
    vError("vgId:%d, failed to deserialize SVAuditRecordReq", TD_VID(pVnode));
×
UNCOV
3127
    code = TSDB_CODE_INVALID_MSG;
×
UNCOV
3128
    return code;
×
3129
  }
3130

3131
  vTrace("vgId:%d, start to process AuditRecord Req", TD_VID(pVnode));
228✔
3132

3133
  SJson          *pJson = NULL;
228✔
3134
  STSchema       *pSchema = NULL;
228✔
3135
  SSchemaWrapper *pTagSchema = NULL;
228✔
3136
  int64_t         suid = 0;
228✔
3137

3138
  pJson = tjsonParse(req.data);
228✔
3139
  if (pJson == NULL) {
228✔
UNCOV
3140
    code = TSDB_CODE_INVALID_JSON_FORMAT;
×
UNCOV
3141
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
3142
  }
3143

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

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

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

3181
_exit:
228✔
3182
  if (code != 0)
228✔
UNCOV
3183
    vError("vgId:%d, failed to process AuditRecordReq at line:%d, since %s", TD_VID(pVnode), lino, tstrerror(code));
×
3184

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

3197
  return code;
228✔
3198
}
3199

3200
static int32_t vnodeProcessSubmitReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
600,262,913✔
3201
                                     SRpcMsg *pOriginalMsg) {
3202
  int32_t code = 0;
600,262,913✔
3203
  int32_t lino = 0;
600,262,913✔
3204
  terrno = 0;
600,262,913✔
3205

3206
  SSubmitReq2 *pSubmitReq = &(SSubmitReq2){0};
600,266,528✔
3207
  SSubmitRsp2 *pSubmitRsp = &(SSubmitRsp2){0};
600,266,528✔
3208
  int32_t      ret;
3209
  SEncoder     ec = {0};
600,265,824✔
3210

3211
  pRsp->code = TSDB_CODE_SUCCESS;
600,266,565✔
3212

3213
  void           *pAllocMsg = NULL;
600,263,847✔
3214
  SSubmitReq2Msg *pMsg = (SSubmitReq2Msg *)pReq;
600,263,847✔
3215
  SDecoder        dc = {0};
600,263,847✔
3216
  if (0 == taosHton64(pMsg->version)) {
600,265,620✔
UNCOV
3217
    code = vnodeSubmitReqConvertToSubmitReq2(pVnode, (SSubmitReq *)pMsg, pSubmitReq);
×
UNCOV
3218
    if (TSDB_CODE_SUCCESS == code) {
×
UNCOV
3219
      code = vnodeRebuildSubmitReqMsg(pSubmitReq, &pReq);
×
3220
    }
UNCOV
3221
    if (TSDB_CODE_SUCCESS == code) {
×
3222
      pAllocMsg = pReq;
×
3223
    }
UNCOV
3224
    TSDB_CHECK_CODE(code, lino, _exit);
×
3225
  } else {
3226
    // decode
3227
    pReq = POINTER_SHIFT(pReq, sizeof(SSubmitReq2Msg));
600,264,916✔
3228
    len -= sizeof(SSubmitReq2Msg);
600,265,083✔
3229

3230
    tDecoderInit(&dc, pReq, len);
600,265,083✔
3231
    if (tDecodeSubmitReq(&dc, pSubmitReq, NULL) < 0) {
600,265,824✔
3232
      code = TSDB_CODE_INVALID_MSG;
×
UNCOV
3233
      TSDB_CHECK_CODE(code, lino, _exit);
×
3234
    }
3235
  }
3236

3237
  // Scan the request
3238
  code = vnodeScanSubmitReq(pVnode, ver, pSubmitReq, pSubmitRsp);
600,242,087✔
3239
  if (code) {
600,198,274✔
UNCOV
3240
    vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
3241
           tstrerror(code), ver);
UNCOV
3242
    TSDB_CHECK_CODE(code, lino, _exit);
×
3243
  }
3244

3245
  vGDebug(pOriginalMsg ? &pOriginalMsg->info.traceId : NULL, "vgId:%d, index:%" PRId64 ", submit block, rows:%d",
600,198,274✔
3246
          TD_VID(pVnode), ver, (int32_t)taosArrayGetSize(pSubmitReq->aSubmitTbData));
3247

3248
  // Handle auto create table
3249
  code = vnodeHandleAutoCreateTable(pVnode, ver, pSubmitReq, pSubmitRsp);
600,244,777✔
3250
  if (code) {
600,263,533✔
3251
    vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
29,256✔
3252
           tstrerror(code), ver);
3253
    TSDB_CHECK_CODE(code, lino, _exit);
29,256✔
3254
  }
3255

3256
  // Handle data write
3257
  code = vnodeHandleDataWrite(pVnode, ver, pSubmitReq, pSubmitRsp);
600,234,277✔
3258
  if (code) {
600,237,820✔
3259
    vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
1,764✔
3260
           tstrerror(code), ver);
3261
    TSDB_CHECK_CODE(code, lino, _exit);
1,764✔
3262
  }
3263

3264
_exit:
600,266,926✔
3265
  // message
3266
  pRsp->code = code;
600,267,076✔
3267
  tEncodeSize(tEncodeSSubmitRsp2, pSubmitRsp, pRsp->contLen, ret);
600,267,076✔
3268
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
600,265,002✔
3269
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
600,264,477✔
3270
  if (tEncodeSSubmitRsp2(&ec, pSubmitRsp) < 0) {
600,265,468✔
UNCOV
3271
    vError("vgId:%d, failed to encode submit response", TD_VID(pVnode));
×
3272
  }
3273
  tEncoderClear(&ec);
600,260,148✔
3274

3275
  // update statistics
3276
  (void)atomic_add_fetch_64(&pVnode->statis.nInsert, pSubmitRsp->affectedRows);
600,260,797✔
3277
  (void)atomic_add_fetch_64(&pVnode->statis.nInsertSuccess, pSubmitRsp->affectedRows);
600,263,909✔
3278
  (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsert, 1);
600,264,953✔
3279

3280
  // update metrics
3281
  METRICS_UPDATE(pVnode->writeMetrics.total_requests, METRIC_LEVEL_LOW, 1);
600,264,465✔
3282
  METRICS_UPDATE(pVnode->writeMetrics.total_rows, METRIC_LEVEL_HIGH, pSubmitRsp->affectedRows);
600,250,052✔
3283
  METRICS_UPDATE(pVnode->writeMetrics.total_bytes, METRIC_LEVEL_LOW, pMsg->header.contLen);
600,248,865✔
3284

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

3298
  if (code == 0) {
600,247,155✔
3299
    (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
600,216,160✔
3300
  }
3301

3302
  // clear
3303
  tDestroySubmitReq(pSubmitReq, 0 == taosHton64(pMsg->version) ? TSDB_MSG_FLG_CMPT : TSDB_MSG_FLG_DECODE);
600,255,905✔
3304
  tDestroySSubmitRsp2(pSubmitRsp, TSDB_MSG_FLG_ENCODE);
600,225,651✔
3305

3306
  if (code) {
600,204,067✔
3307
    terrno = code;
31,020✔
3308
    if (code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
31,020✔
3309
      vInfo("vgId:%d, failed to process submit request since %s, lino:%d, version:%" PRId64, TD_VID(pVnode),
28,200✔
3310
            tstrerror(code), lino, ver);
3311

3312
    } else {
3313
      vError("vgId:%d, failed to process submit request since %s, lino:%d, version:%" PRId64, TD_VID(pVnode),
2,820✔
3314
             tstrerror(code), lino, ver);
3315
    }
3316
  }
3317

3318
  taosMemoryFree(pAllocMsg);
600,212,803✔
3319
  tDecoderClear(&dc);
600,204,172✔
3320

3321
  return code;
600,261,720✔
3322
}
3323

3324
static int32_t vnodeConsolidateAlterHashRange(SVnode *pVnode, int64_t ver) {
25,127✔
3325
  int32_t code = TSDB_CODE_SUCCESS;
25,127✔
3326

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

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

3333
  return code;
25,127✔
3334
}
3335

3336
static int32_t vnodeProcessAlterConfirmReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
4,367,131✔
3337
  vInfo("vgId:%d, vnode handle msgType:alter-confirm, alter confirm msg is processed", TD_VID(pVnode));
4,367,131✔
3338
  int32_t code = TSDB_CODE_SUCCESS;
4,367,131✔
3339
  if (!pVnode->config.hashChange) {
4,367,131✔
3340
    goto _exit;
4,342,004✔
3341
  }
3342

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

3350
_exit:
4,367,131✔
3351
  pRsp->msgType = TDMT_VND_ALTER_CONFIRM_RSP;
4,367,131✔
3352
  pRsp->code = code;
4,367,131✔
3353
  pRsp->pCont = NULL;
4,367,131✔
3354
  pRsp->contLen = 0;
4,367,131✔
3355

3356
  return code;
4,367,131✔
3357
}
3358

3359
extern int32_t tsdbDisableAndCancelAllBgTask(STsdb *pTsdb);
3360
extern void    tsdbEnableBgTask(STsdb *pTsdb);
3361

3362
static int32_t vnodeProcessAlterConfigReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
330,290✔
3363
  bool walChanged = false;
330,290✔
3364
  bool tsdbChanged = false;
330,290✔
3365

3366
  SAlterVnodeConfigReq req = {0};
330,290✔
3367
  if (tDeserializeSAlterVnodeConfigReq(pReq, len, &req) != 0) {
330,981✔
UNCOV
3368
    terrno = TSDB_CODE_INVALID_MSG;
×
UNCOV
3369
    return TSDB_CODE_INVALID_MSG;
×
3370
  }
3371

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

3381
  if (pVnode->config.cacheLastSize != req.cacheLastSize) {
330,981✔
3382
    pVnode->config.cacheLastSize = req.cacheLastSize;
14,481✔
3383
    tsdbCacheSetCapacity(pVnode, (size_t)pVnode->config.cacheLastSize * 1024 * 1024);
14,481✔
3384
  }
3385

3386
  if (pVnode->config.cacheLastShardBits != req.cacheLastShardBits) {
330,981✔
3387
    vInfo("vgId:%d, cacheLastShardBits change from %d to %d, rebuilding last cache",
1,480✔
3388
          TD_VID(pVnode), pVnode->config.cacheLastShardBits, req.cacheLastShardBits);
3389

3390
    // Rebuild only the lruCache under mutex to protect concurrent readers/writers
3391
    (void)taosThreadMutexLock(&pVnode->pTsdb->lruMutex);
1,480✔
3392
    int32_t code = tsdbRebuildLastCache(pVnode->pTsdb, req.cacheLastShardBits);
1,480✔
3393
    (void)taosThreadMutexUnlock(&pVnode->pTsdb->lruMutex);
1,480✔
3394

3395
    if (code) {
1,480✔
UNCOV
3396
      vError("vgId:%d, failed to rebuild last cache since %s", TD_VID(pVnode), tstrerror(code));
×
UNCOV
3397
      return code;
×
3398
    }
3399

3400
    pVnode->config.cacheLastShardBits = req.cacheLastShardBits;
1,480✔
3401

3402
    vInfo("vgId:%d, last cache rebuilt successfully with %d shards",
1,480✔
3403
          TD_VID(pVnode), taosLRUCacheGetNumShards(pVnode->pTsdb->lruCache));
3404
  }
3405

3406
  if (pVnode->config.szBuf != req.buffer * 1024LL * 1024LL) {
330,981✔
3407
    vInfo("vgId:%d, vnode buffer is changed from %" PRId64 " to %" PRId64, TD_VID(pVnode), pVnode->config.szBuf,
1,776✔
3408
          (uint64_t)(req.buffer * 1024LL * 1024LL));
3409
    pVnode->config.szBuf = req.buffer * 1024LL * 1024LL;
1,776✔
3410
  }
3411

3412
  if (pVnode->config.szCache != req.pages) {
330,981✔
3413
    if ((terrno = metaAlterCache(pVnode->pMeta, req.pages)) < 0) {
4,704✔
UNCOV
3414
      vError("vgId:%d, failed to change vnode pages from %d to %d failed since %s", TD_VID(pVnode),
×
3415
             pVnode->config.szCache, req.pages, tstrerror(terrno));
UNCOV
3416
      return terrno;
×
3417
    } else {
3418
      vInfo("vgId:%d, vnode pages is changed from %d to %d", TD_VID(pVnode), pVnode->config.szCache, req.pages);
4,704✔
3419
      pVnode->config.szCache = req.pages;
4,704✔
3420
    }
3421
  }
3422

3423
  if (pVnode->config.cacheLast != req.cacheLast) {
330,981✔
3424
    pVnode->config.cacheLast = req.cacheLast;
98,251✔
3425
  }
3426

3427
  if (pVnode->config.walCfg.fsyncPeriod != req.walFsyncPeriod) {
330,981✔
3428
    pVnode->config.walCfg.fsyncPeriod = req.walFsyncPeriod;
53,366✔
3429
    walChanged = true;
53,366✔
3430
  }
3431

3432
  if (pVnode->config.walCfg.level != req.walLevel) {
330,981✔
3433
    if (pVnode->config.walCfg.level == 0) {
39,355✔
3434
      pVnode->config.walCfg.clearFiles = 1;
2,269✔
3435
    }
3436
    pVnode->config.walCfg.level = req.walLevel;
39,355✔
3437
    walChanged = true;
39,355✔
3438
  }
3439

3440
  if (pVnode->config.walCfg.retentionPeriod != req.walRetentionPeriod) {
330,981✔
3441
    pVnode->config.walCfg.retentionPeriod = req.walRetentionPeriod;
48,619✔
3442
    walChanged = true;
48,619✔
3443
  }
3444

3445
  if (pVnode->config.walCfg.retentionSize != req.walRetentionSize) {
330,981✔
3446
    pVnode->config.walCfg.retentionSize = req.walRetentionSize;
1,470✔
3447
    walChanged = true;
1,470✔
3448
  }
3449

3450
  if (pVnode->config.tsdbCfg.keep0 != req.daysToKeep0) {
330,981✔
3451
    pVnode->config.tsdbCfg.keep0 = req.daysToKeep0;
44,530✔
3452
    tsdbChanged = true;
44,530✔
3453
  }
3454

3455
  if (pVnode->config.tsdbCfg.keep1 != req.daysToKeep1) {
330,981✔
3456
    pVnode->config.tsdbCfg.keep1 = req.daysToKeep1;
43,006✔
3457
    tsdbChanged = true;
43,006✔
3458
  }
3459

3460
  if (pVnode->config.tsdbCfg.keep2 != req.daysToKeep2) {
330,981✔
3461
    pVnode->config.tsdbCfg.keep2 = req.daysToKeep2;
37,008✔
3462
    tsdbChanged = true;
37,008✔
3463
  }
3464

3465
  if (pVnode->config.tsdbCfg.keepTimeOffset != req.keepTimeOffset) {
330,981✔
3466
    pVnode->config.tsdbCfg.keepTimeOffset = req.keepTimeOffset;
7,490✔
3467
    tsdbChanged = true;
7,490✔
3468
  }
3469

3470
  if (req.sttTrigger != -1 && req.sttTrigger != pVnode->config.sttTrigger) {
330,981✔
3471
    if (req.sttTrigger > 1 && pVnode->config.sttTrigger > 1) {
2,578✔
3472
      pVnode->config.sttTrigger = req.sttTrigger;
1,470✔
3473
    } else {
3474
      vnodeAWait(&pVnode->commitTask);
1,108✔
3475

3476
      int32_t ret = tsdbDisableAndCancelAllBgTask(pVnode->pTsdb);
1,108✔
3477
      if (ret != 0) {
1,108✔
UNCOV
3478
        vError("vgId:%d, failed to disable bg task since %s", TD_VID(pVnode), tstrerror(ERRNO));
×
3479
      }
3480

3481
      pVnode->config.sttTrigger = req.sttTrigger;
1,108✔
3482
      tsdbEnableBgTask(pVnode->pTsdb);
1,108✔
3483
    }
3484
  }
3485

3486
  if (req.minRows != -1 && req.minRows != pVnode->config.tsdbCfg.minRows) {
330,981✔
3487
    pVnode->config.tsdbCfg.minRows = req.minRows;
1,470✔
3488
  }
3489

3490
  if (req.ssKeepLocal != -1 && req.ssKeepLocal != pVnode->config.ssKeepLocal) {
330,981✔
3491
    pVnode->config.ssKeepLocal = req.ssKeepLocal;
1,470✔
3492
  }
3493
  if (req.ssCompact != -1 && req.ssCompact != pVnode->config.ssCompact) {
330,981✔
3494
    pVnode->config.ssCompact = req.ssCompact;
1,470✔
3495
  }
3496
  if (req.allowDrop != pVnode->config.allowDrop) {
330,981✔
UNCOV
3497
    pVnode->config.allowDrop = req.allowDrop;
×
3498
  }
3499
  if (req.secureDelete != pVnode->config.secureDelete) {
330,981✔
3500
    pVnode->config.secureDelete = req.secureDelete;
1,142✔
3501
  }
3502

3503
  if (walChanged) {
330,981✔
3504
    if (walAlter(pVnode->pWal, &pVnode->config.walCfg) != 0) {
139,754✔
UNCOV
3505
      vError("vgId:%d, failed to alter wal config since %s", TD_VID(pVnode), tstrerror(ERRNO));
×
3506
    }
3507
  }
3508

3509
  if (tsdbChanged) {
330,981✔
3510
    tsdbSetKeepCfg(pVnode->pTsdb, &pVnode->config.tsdbCfg);
59,596✔
3511
  }
3512

3513
  return 0;
330,981✔
3514
}
3515

3516
static int32_t vnodeProcessBatchDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
3517
  SBatchDeleteReq deleteReq;
×
UNCOV
3518
  SDecoder        decoder;
×
3519
  tDecoderInit(&decoder, pReq, len);
×
3520
  if (tDecodeSBatchDeleteReq(&decoder, &deleteReq) < 0) {
×
3521
    tDecoderClear(&decoder);
×
3522
    return terrno = TSDB_CODE_INVALID_MSG;
×
3523
  }
3524

3525
  SMetaReader mr = {0};
×
UNCOV
3526
  metaReaderDoInit(&mr, pVnode->pMeta, META_READER_NOLOCK);
×
UNCOV
3527
  STsdb *pTsdb = pVnode->pTsdb;
×
3528

UNCOV
3529
  int32_t sz = taosArrayGetSize(deleteReq.deleteReqs);
×
3530
  for (int32_t i = 0; i < sz; i++) {
×
3531
    SSingleDeleteReq *pOneReq = taosArrayGet(deleteReq.deleteReqs, i);
×
3532
    char             *name = pOneReq->tbname;
×
3533
    if (metaGetTableEntryByName(&mr, name) < 0) {
×
UNCOV
3534
      vDebug("vgId:%d, stream delete msg, skip since no table: %s", pVnode->config.vgId, name);
×
UNCOV
3535
      continue;
×
3536
    }
3537

3538
    int64_t uid = mr.me.uid;
×
3539

3540
    int32_t code = tsdbDeleteTableData(pTsdb, ver, deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs, 0);
×
3541
    if (code < 0) {
×
UNCOV
3542
      terrno = code;
×
UNCOV
3543
      vError("vgId:%d, delete error since %s, suid:%" PRId64 ", uid:%" PRId64 ", start ts:%" PRId64 ", end ts:%" PRId64,
×
3544
             TD_VID(pVnode), terrstr(), deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
3545
    }
3546

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

3563
static int32_t vnodeProcessDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
2,157,816✔
3564
                                     SRpcMsg *pOriginalMsg) {
3565
  int32_t     code = 0;
2,157,816✔
3566
  SDecoder   *pCoder = &(SDecoder){0};
2,157,816✔
3567
  SDeleteRes *pRes = &(SDeleteRes){0};
2,157,816✔
3568

3569
  pRsp->msgType = TDMT_VND_DELETE_RSP;
2,157,816✔
3570
  pRsp->pCont = NULL;
2,157,816✔
3571
  pRsp->contLen = 0;
2,157,816✔
3572
  pRsp->code = TSDB_CODE_SUCCESS;
2,157,816✔
3573

3574
  pRes->uidList = taosArrayInit(0, sizeof(tb_uid_t));
2,157,816✔
3575
  if (pRes->uidList == NULL) {
2,157,816✔
UNCOV
3576
    code = terrno;
×
UNCOV
3577
    goto _err;
×
3578
  }
3579

3580
  tDecoderInit(pCoder, pReq, len);
2,157,816✔
3581
  code = tDecodeDeleteRes(pCoder, pRes);
2,157,816✔
3582
  if (code) goto _err;
2,157,816✔
3583

3584
  if (pRes->affectedRows > 0) {
2,157,816✔
3585
    for (int32_t iUid = 0; iUid < taosArrayGetSize(pRes->uidList); iUid++) {
3,842,196✔
3586
      uint64_t uid = *(uint64_t *)taosArrayGet(pRes->uidList, iUid);
2,191,538✔
3587
      // Merge runtime vnode(db-level) secureDelete to avoid stale client meta after ALTER DATABASE.
3588
      int8_t secureDelete = pRes->secureDelete | pVnode->config.secureDelete;
2,191,538✔
3589
      code = tsdbDeleteTableData(pVnode->pTsdb, ver, pRes->suid, uid, pRes->skey, pRes->ekey, secureDelete);
2,191,538✔
3590
      if (code) goto _err;
2,191,538✔
3591
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, pRes->ctimeMs);
2,191,538✔
3592
      if (code) goto _err;
2,191,538✔
3593
    }
3594
  }
3595

3596
  tDecoderClear(pCoder);
2,157,816✔
3597
  taosArrayDestroy(pRes->uidList);
2,157,816✔
3598

3599
  SVDeleteRsp rsp = {.affectedRows = pRes->affectedRows};
2,157,816✔
3600
  int32_t     ret = 0;
2,157,816✔
3601
  tEncodeSize(tEncodeSVDeleteRsp, &rsp, pRsp->contLen, ret);
2,157,816✔
3602
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
2,157,816✔
3603
  SEncoder ec = {0};
2,157,816✔
3604
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
2,157,816✔
3605
  code = tEncodeSVDeleteRsp(&ec, &rsp);
2,157,816✔
3606
  if (code) goto _err;
2,157,816✔
3607
  tEncoderClear(&ec);
2,157,816✔
3608
  return code;
2,157,816✔
3609

UNCOV
3610
_err:
×
3611
  /*
3612
  if(code == TSDB_CODE_SUCCESS){
3613
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_DELETE, pVnode->monitor.strClusterId,
3614
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
3615
                                        RPC_MSG_USER(pOriginalMsg), "Success"};
3616
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
3617
  }
3618
  else{
3619
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_DELETE, pVnode->monitor.strClusterId,
3620
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
3621
                                        RPC_MSG_USER(pOriginalMsg), "Failed"};
3622
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
3623
  }
3624
  */
3625

UNCOV
3626
  return code;
×
3627
}
3628
static int32_t vnodeProcessCreateIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
17,958✔
3629
  SVCreateStbReq req = {0};
17,958✔
3630
  SDecoder       dc = {0};
17,958✔
3631
  int32_t        code = 0;
17,958✔
3632

3633
  pRsp->msgType = TDMT_VND_CREATE_INDEX_RSP;
17,958✔
3634
  pRsp->code = TSDB_CODE_SUCCESS;
17,958✔
3635
  pRsp->pCont = NULL;
17,958✔
3636
  pRsp->contLen = 0;
17,958✔
3637

3638
  tDecoderInit(&dc, pReq, len);
17,958✔
3639
  // decode req
3640
  if (tDecodeSVCreateStbReq(&dc, &req) < 0) {
17,958✔
UNCOV
3641
    tDecoderClear(&dc);
×
UNCOV
3642
    return terrno = TSDB_CODE_INVALID_MSG;
×
3643
  }
3644

3645
  code = metaAddIndexToSuperTable(pVnode->pMeta, ver, &req);
17,958✔
3646
  if (code) {
17,958✔
UNCOV
3647
    pRsp->code = code;
×
UNCOV
3648
    goto _err;
×
3649
  }
3650
  tDecoderClear(&dc);
17,958✔
3651
  return 0;
17,958✔
3652

UNCOV
3653
_err:
×
UNCOV
3654
  tDecoderClear(&dc);
×
UNCOV
3655
  return code;
×
3656
}
3657
static int32_t vnodeProcessDropIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
14,588✔
3658
  SDropIndexReq req = {0};
14,588✔
3659
  int32_t       code = 0;
14,588✔
3660
  pRsp->msgType = TDMT_VND_DROP_INDEX_RSP;
14,588✔
3661
  pRsp->code = TSDB_CODE_SUCCESS;
14,588✔
3662
  pRsp->pCont = NULL;
14,588✔
3663
  pRsp->contLen = 0;
14,588✔
3664

3665
  if ((code = tDeserializeSDropIdxReq(pReq, len, &req))) {
14,588✔
UNCOV
3666
    pRsp->code = code;
×
UNCOV
3667
    return code;
×
3668
  }
3669

3670
  code = metaDropIndexFromSuperTable(pVnode->pMeta, ver, &req);
14,588✔
3671
  if (code) {
14,588✔
UNCOV
3672
    pRsp->code = code;
×
UNCOV
3673
    return code;
×
3674
  }
3675
  return TSDB_CODE_SUCCESS;
14,588✔
3676
}
3677

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

3680
static int32_t vnodeProcessCompactVnodeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
69,679✔
3681
  if (!pVnode->restored) {
69,679✔
3682
    vInfo("vgId:%d, ignore compact req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
2,206✔
3683
    return 0;
2,206✔
3684
  }
3685
  return vnodeAsyncCompact(pVnode, ver, pReq, len, pRsp);
67,473✔
3686
}
3687

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

UNCOV
3693
  pRsp->msgType = TDMT_SYNC_CONFIG_CHANGE_RSP;
×
UNCOV
3694
  pRsp->code = TSDB_CODE_SUCCESS;
×
UNCOV
3695
  pRsp->pCont = NULL;
×
UNCOV
3696
  pRsp->contLen = 0;
×
3697

UNCOV
3698
  return 0;
×
3699
}
3700

3701
static int32_t vnodeCheckState(SVnode *pVnode) {
43,058✔
3702
  SSyncState syncState = syncGetState(pVnode->sync);
43,058✔
3703
  if (syncState.state != TAOS_SYNC_STATE_LEADER) {
43,058✔
3704
    return terrno = TSDB_CODE_SYN_NOT_LEADER;
17,464✔
3705
  }
3706
  return 0;
25,594✔
3707
}
3708

3709
static int32_t vnodeCheckToken(SVnode *pVnode, char *member0Token, char *member1Token) {
25,594✔
3710
  char token[TSDB_ARB_TOKEN_SIZE] = {0};
25,594✔
3711
  if (vnodeGetArbToken(pVnode, token) != 0) {
25,594✔
UNCOV
3712
    return terrno = TSDB_CODE_NOT_FOUND;
×
3713
  }
3714

3715
  if (strncmp(token, member0Token, TSDB_ARB_TOKEN_SIZE) != 0 &&
25,594✔
3716
      strncmp(token, member1Token, TSDB_ARB_TOKEN_SIZE) != 0) {
24,426✔
3717
    return terrno = TSDB_CODE_MND_ARB_TOKEN_MISMATCH;
21,974✔
3718
  }
3719

3720
  terrno = TSDB_CODE_SUCCESS;
3,620✔
3721
  return 0;
3,620✔
3722
}
3723

3724
static int32_t vnodeCheckSyncd(SVnode *pVnode, char *member0Token, char *member1Token) {
9,100✔
3725
  int32_t code = vnodeCheckToken(pVnode, member0Token, member1Token);
9,100✔
3726
  if (code != 0) {
9,100✔
3727
    return code;
7,290✔
3728
  }
3729

3730
  return syncCheckSynced(pVnode->sync);
1,810✔
3731
}
3732

3733
static int32_t vnodeProcessArbCheckSyncReq(SVnode *pVnode, void *pReq, int32_t len, SRpcMsg *pRsp) {
23,267✔
3734
  int32_t code = 0;
23,267✔
3735

3736
  if ((code = vnodeCheckState(pVnode)) != 0) {
23,267✔
3737
    vDebug("vgId:%d, failed to preprocess vnode-arb-check-sync request since %s", TD_VID(pVnode), tstrerror(code));
14,167✔
3738
    return 0;
14,167✔
3739
  }
3740

3741
  SVArbCheckSyncReq syncReq = {0};
9,100✔
3742

3743
  code = tDeserializeSVArbCheckSyncReq(pReq, len, &syncReq);
9,100✔
3744
  if (code) {
9,100✔
UNCOV
3745
    return code;
×
3746
  }
3747

3748
  vInfo("vgId:%d, start to process vnode-arb-check-sync req QID:0x%" PRIx64 ":0x%" PRIx64 ", seqNum:%" PRIx64
9,100✔
3749
        ", arbToken:%s, member0Token:%s, member1Token:%s, "
3750
        "arbTerm:%" PRId64,
3751
        TD_VID(pVnode), pRsp->info.traceId.rootId, pRsp->info.traceId.msgId, pRsp->info.seqNum, syncReq.arbToken,
3752
        syncReq.member0Token, syncReq.member1Token, syncReq.arbTerm);
3753

3754
  pRsp->msgType = TDMT_VND_ARB_CHECK_SYNC_RSP;
9,100✔
3755
  pRsp->code = TSDB_CODE_SUCCESS;
9,100✔
3756
  pRsp->pCont = NULL;
9,100✔
3757
  pRsp->contLen = 0;
9,100✔
3758

3759
  SVArbCheckSyncRsp syncRsp = {0};
9,100✔
3760
  syncRsp.arbToken = syncReq.arbToken;
9,100✔
3761
  syncRsp.member0Token = syncReq.member0Token;
9,100✔
3762
  syncRsp.member1Token = syncReq.member1Token;
9,100✔
3763
  syncRsp.vgId = TD_VID(pVnode);
9,100✔
3764

3765
  if ((syncRsp.errCode = vnodeCheckSyncd(pVnode, syncReq.member0Token, syncReq.member1Token)) != 0) {
9,100✔
3766
    vError("vgId:%d, failed to check assigned log syncd since %s", TD_VID(pVnode), tstrerror(syncRsp.errCode));
7,290✔
3767
  }
3768

3769
  if ((code = vnodeUpdateArbTerm(pVnode, syncReq.arbTerm)) != 0) {
9,100✔
UNCOV
3770
    vError("vgId:%d, failed to update arb term since %s", TD_VID(pVnode), tstrerror(code));
×
UNCOV
3771
    goto _OVER;
×
3772
  }
3773

3774
  int32_t contLen = tSerializeSVArbCheckSyncRsp(NULL, 0, &syncRsp);
9,100✔
3775
  if (contLen <= 0) {
9,100✔
UNCOV
3776
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
3777
    code = -1;
×
3778
    goto _OVER;
×
3779
  }
3780
  void *pHead = rpcMallocCont(contLen);
9,100✔
3781
  if (!pHead) {
9,100✔
UNCOV
3782
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
3783
    code = -1;
×
UNCOV
3784
    goto _OVER;
×
3785
  }
3786

3787
  if (tSerializeSVArbCheckSyncRsp(pHead, contLen, &syncRsp) <= 0) {
9,100✔
UNCOV
3788
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
3789
    rpcFreeCont(pHead);
×
UNCOV
3790
    code = -1;
×
UNCOV
3791
    goto _OVER;
×
3792
  }
3793

3794
  pRsp->pCont = pHead;
9,100✔
3795
  pRsp->contLen = contLen;
9,100✔
3796

3797
  vInfo(
9,100✔
3798
      "vgId:%d, suceed to process vnode-arb-check-sync req rsp.code:%s, arbToken:%s, member0Token:%s, "
3799
      "member1Token:%s",
3800
      TD_VID(pVnode), tstrerror(syncRsp.errCode), syncRsp.arbToken, syncRsp.member0Token, syncRsp.member1Token);
3801

3802
  code = TSDB_CODE_SUCCESS;
9,100✔
3803

3804
_OVER:
9,100✔
3805
  if (code != 0) {
9,100✔
UNCOV
3806
    vError("vgId:%d, failed to process arb check req rsp.code:%s since %s", TD_VID(pVnode), tstrerror(syncRsp.errCode),
×
3807
           tstrerror(code));
3808
  }
3809
  tFreeSVArbCheckSyncReq(&syncReq);
9,100✔
3810
  return code;
9,100✔
3811
}
3812

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