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

taosdata / TDengine / #4995

18 Mar 2026 06:26AM UTC coverage: 71.996% (-0.2%) from 72.244%
#4995

push

travis-ci

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

2 of 4 new or added lines in 2 files covered. (50.0%)

5312 existing lines in 167 files now uncovered.

244665 of 339830 relevant lines covered (72.0%)

135013613.72 hits per line

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

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

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

32
extern taos_counter_t *tsInsertCounter;
33

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

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

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

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

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

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

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

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

89
  if (tStartDecode(pCoder) < 0) {
62,975,601✔
90
    code = TSDB_CODE_INVALID_MSG;
×
91
    TSDB_CHECK_CODE(code, lino, _exit);
×
92
  }
93

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

100
  // name
101
  char *name = NULL;
62,976,719✔
102
  if (tDecodeCStr(pCoder, &name) < 0) {
62,978,434✔
103
    code = TSDB_CODE_INVALID_MSG;
×
104
    TSDB_CHECK_CODE(code, lino, _exit);
×
105
  }
106

107
  // uid
108
  int64_t uid = metaGetTableEntryUidByName(pVnode->pMeta, name);
62,978,434✔
109
  if (uid == 0) {
62,975,053✔
110
    uid = tGenIdPI64();
62,605,589✔
111
  }
112
  taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), uid);
62,972,122✔
113

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

117
  tEndDecode(pCoder);
62,969,135✔
118

119
_exit:
62,977,617✔
120
  if (code) {
62,975,039✔
121
    vError("vgId:%d %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
122
  } else {
123
    vTrace("vgId:%d %s done, table:%s uid generated:%" PRId64, TD_VID(pVnode), __func__, name, uid);
62,975,039✔
124
    if (pUid) *pUid = uid;
62,973,048✔
125
  }
126
  return code;
62,972,758✔
127
}
128
static int32_t vnodePreProcessCreateTableMsg(SVnode *pVnode, SRpcMsg *pMsg) {
42,306,541✔
129
  int32_t code = 0;
42,306,541✔
130
  int32_t lino = 0;
42,306,541✔
131

132
  int64_t  btime = taosGetTimestampMs();
42,307,268✔
133
  SDecoder dc = {0};
42,307,268✔
134
  int32_t  nReqs;
42,304,870✔
135

136
  tDecoderInit(&dc, (uint8_t *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead));
42,307,268✔
137
  if (tStartDecode(&dc) < 0) {
42,306,541✔
138
    code = TSDB_CODE_INVALID_MSG;
×
139
    return code;
×
140
  }
141

142
  if (tDecodeI32v(&dc, &nReqs) < 0) {
42,307,268✔
143
    code = TSDB_CODE_INVALID_MSG;
×
144
    TSDB_CHECK_CODE(code, lino, _exit);
×
145
  }
146
  for (int32_t iReq = 0; iReq < nReqs; iReq++) {
89,907,200✔
147
    code = vnodePreprocessCreateTableReq(pVnode, &dc, btime, NULL);
47,603,883✔
148
    TSDB_CHECK_CODE(code, lino, _exit);
47,599,932✔
149
  }
150

151
  tEndDecode(&dc);
42,303,317✔
152

153
_exit:
42,306,438✔
154
  tDecoderClear(&dc);
42,304,617✔
155
  if (code) {
42,305,219✔
156
    vError("vgId:%d, %s:%d failed to preprocess submit request since %s, msg type:%s", TD_VID(pVnode), __func__, lino,
×
157
           tstrerror(code), TMSG_INFO(pMsg->msgType));
158
  }
159
  return code;
42,304,169✔
160
}
161

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

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

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

173
  SVAlterTbReq vAlterTbReq = {0};
12,293,712✔
174
  int64_t      ctimeMs = taosGetTimestampMs();
12,293,712✔
175
  code = tDecodeSVAlterTbReqSetCtime(&dc, &vAlterTbReq, ctimeMs);
12,293,712✔
176

177
  taosArrayDestroy(vAlterTbReq.pMultiTag);
12,293,712✔
178
  vAlterTbReq.pMultiTag = NULL;
12,293,712✔
179

180
  for (int32_t i = 0; i < taosArrayGetSize(vAlterTbReq.tables); i++) {
20,195,204✔
181
    SUpdateTableTagVal* pTable = taosArrayGet(vAlterTbReq.tables, i);
7,901,492✔
182
    taosArrayDestroy(pTable->tags);
7,901,492✔
183
  }
184
  taosArrayDestroy(vAlterTbReq.tables);
12,293,712✔
185
  vAlterTbReq.tables = NULL;
12,293,712✔
186

187

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

199
static int32_t vnodePreProcessDropTtlMsg(SVnode *pVnode, SRpcMsg *pMsg) {
11,062,650✔
200
  int32_t code = TSDB_CODE_INVALID_MSG;
11,062,650✔
201
  int32_t lino = 0;
11,062,650✔
202

203
  SMsgHead *pContOld = pMsg->pCont;
11,062,650✔
204
  int32_t   reqLenOld = pMsg->contLen - sizeof(SMsgHead);
11,063,958✔
205

206
  SArray *tbUids = NULL;
11,063,613✔
207
  int64_t timestampMs = 0;
11,063,613✔
208

209
  SVDropTtlTableReq ttlReq = {0};
11,063,613✔
210
  if (tDeserializeSVDropTtlTableReq((char *)pContOld + sizeof(SMsgHead), reqLenOld, &ttlReq) != 0) {
11,065,010✔
UNCOV
211
    code = TSDB_CODE_INVALID_MSG;
×
UNCOV
212
    TSDB_CHECK_CODE(code, lino, _exit);
×
213
  }
214

215
  {  // find expired uids
216
    tbUids = taosArrayInit(8, sizeof(tb_uid_t));
11,060,993✔
217
    if (tbUids == NULL) {
11,063,367✔
UNCOV
218
      code = terrno;
×
UNCOV
219
      TSDB_CHECK_CODE(code, lino, _exit);
×
220
    }
221

222
    timestampMs = (int64_t)ttlReq.timestampSec * 1000;
11,063,367✔
223
    code = metaTtlFindExpired(pVnode->pMeta, timestampMs, tbUids, ttlReq.ttlDropMaxCount);
11,063,367✔
224
    if (code != 0) {
11,055,498✔
UNCOV
225
      code = TSDB_CODE_INVALID_MSG;
×
UNCOV
226
      TSDB_CHECK_CODE(code, lino, _exit);
×
227
    }
228

229
    ttlReq.nUids = taosArrayGetSize(tbUids);
11,055,498✔
230
    ttlReq.pTbUids = tbUids;
11,058,673✔
231
  }
232

233
  if (ttlReq.nUids == 0) {
11,058,673✔
234
    code = TSDB_CODE_MSG_PREPROCESSED;
11,041,860✔
235
    TSDB_CHECK_CODE(code, lino, _exit);
11,041,860✔
236
  }
237

238
  {  // prepare new content
239
    int32_t reqLenNew = tSerializeSVDropTtlTableReq(NULL, 0, &ttlReq);
12,011✔
240
    int32_t contLenNew = reqLenNew + sizeof(SMsgHead);
11,781✔
241

242
    SMsgHead *pContNew = rpcMallocCont(contLenNew);
11,781✔
243
    if (pContNew == NULL) {
11,781✔
UNCOV
244
      code = terrno;
×
UNCOV
245
      TSDB_CHECK_CODE(code, lino, _exit);
×
246
    }
247

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

254
    rpcFreeCont(pContOld);
11,781✔
255
    pMsg->pCont = pContNew;
11,781✔
256
    pMsg->contLen = contLenNew;
11,781✔
257
  }
258

259
  code = 0;
11,781✔
260

261
_exit:
11,058,443✔
262
  taosArrayDestroy(tbUids);
11,060,000✔
263

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

271
  return code;
11,045,659✔
272
}
273

274
extern int64_t tsMaxKeyByPrecision[];
275
static int32_t vnodePreProcessSubmitTbData(SVnode *pVnode, SDecoder *pCoder, int64_t btimeMs, int64_t ctimeMs) {
548,487,186✔
276
  int32_t code = 0;
548,487,186✔
277
  int32_t lino = 0;
548,487,186✔
278

279
  if (tStartDecode(pCoder) < 0) {
548,487,186✔
UNCOV
280
    code = TSDB_CODE_INVALID_MSG;
×
UNCOV
281
    TSDB_CHECK_CODE(code, lino, _exit);
×
282
  }
283

284
  SSubmitTbData submitTbData;
548,482,169✔
285
  uint8_t       version;
286
  if (tDecodeI32v(pCoder, &submitTbData.flags) < 0) {
548,487,478✔
UNCOV
287
    code = TSDB_CODE_INVALID_MSG;
×
UNCOV
288
    TSDB_CHECK_CODE(code, lino, _exit);
×
289
  }
290
  version = (submitTbData.flags >> 8) & 0xff;
548,487,478✔
291
  submitTbData.flags = submitTbData.flags & 0xff;
548,487,478✔
292

293
  int64_t uid;
548,483,411✔
294
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
548,487,207✔
295
    code = vnodePreprocessCreateTableReq(pVnode, pCoder, btimeMs, &uid);
15,372,333✔
296
    TSDB_CHECK_CODE(code, lino, _exit);
15,372,633✔
297
  }
298

299
  // submit data
300
  if (tDecodeI64(pCoder, &submitTbData.suid) < 0) {
548,481,521✔
UNCOV
301
    code = TSDB_CODE_INVALID_MSG;
×
UNCOV
302
    TSDB_CHECK_CODE(code, lino, _exit);
×
303
  }
304

305
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
548,481,521✔
306
    taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), uid);
15,372,386✔
307
    pCoder->pos += sizeof(int64_t);
15,372,060✔
308
  } else {
309
    if (tDecodeI64(pCoder, &submitTbData.uid) < 0) {
533,114,794✔
UNCOV
310
      code = TSDB_CODE_INVALID_MSG;
×
UNCOV
311
      TSDB_CHECK_CODE(code, lino, _exit);
×
312
    }
313
  }
314

315
  if (tDecodeI32v(pCoder, &submitTbData.sver) < 0) {
548,487,166✔
UNCOV
316
    code = TSDB_CODE_INVALID_MSG;
×
UNCOV
317
    TSDB_CHECK_CODE(code, lino, _exit);
×
318
  }
319

320
  // scan and check
321
  TSKEY now = btimeMs;
548,487,166✔
322
  if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_MICRO) {
548,487,166✔
323
    now *= 1000;
2,512,117✔
324
  } else if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_NANO) {
545,975,074✔
325
    now *= 1000000;
1,462,572✔
326
  }
327

328
  int32_t keep = pVnode->config.tsdbCfg.keep2;
548,486,613✔
329

330
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * keep;
548,483,287✔
331
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
548,486,848✔
332
  if (submitTbData.flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
548,483,843✔
333
    uint64_t nColData;
6,672,402✔
334
    if (tDecodeU64v(pCoder, &nColData) < 0) {
6,673,145✔
UNCOV
335
      code = TSDB_CODE_INVALID_MSG;
×
UNCOV
336
      TSDB_CHECK_CODE(code, lino, _exit);
×
337
    }
338

339
    SColData colData = {0};
6,673,145✔
340
    code = tDecodeColData(version, pCoder, &colData, false);
6,672,818✔
341
    if (code) {
6,672,810✔
UNCOV
342
      code = TSDB_CODE_INVALID_MSG;
×
UNCOV
343
      TSDB_CHECK_CODE(code, lino, _exit);
×
344
    }
345

346
    if (colData.flag != HAS_VALUE) {
6,672,810✔
UNCOV
347
      code = TSDB_CODE_INVALID_MSG;
×
UNCOV
348
      TSDB_CHECK_CODE(code, lino, _exit);
×
349
    }
350

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

358
    for (uint64_t i = 1; i < nColData; i++) {
20,357,784✔
359
      code = tDecodeColData(version, pCoder, &colData, true);
13,796,812✔
360
      if (code) {
13,797,140✔
UNCOV
361
        code = TSDB_CODE_INVALID_MSG;
×
UNCOV
362
        TSDB_CHECK_CODE(code, lino, _exit);
×
363
      }
364
    }
365
  } else {
366
    uint64_t nRow;
541,807,374✔
367
    if (tDecodeU64v(pCoder, &nRow) < 0) {
541,815,352✔
UNCOV
368
      code = TSDB_CODE_INVALID_MSG;
×
UNCOV
369
      TSDB_CHECK_CODE(code, lino, _exit);
×
370
    }
371

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

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

393
  if (!tDecodeIsEnd(pCoder)) {
548,415,048✔
394
    taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), ctimeMs);
548,414,644✔
395
    pCoder->pos += sizeof(int64_t);
548,408,375✔
396
  }
397

398
  tEndDecode(pCoder);
548,413,949✔
399

400
_exit:
548,482,557✔
401
  if (code) {
548,483,714✔
402
    vError("vgId:%d, %s:%d failed to vnodePreProcessSubmitTbData submit request since %s", TD_VID(pVnode), __func__,
68,207✔
403
           lino, tstrerror(code));
404
  }
405
  return code;
548,483,714✔
406
}
407
static int32_t vnodePreProcessSubmitMsg(SVnode *pVnode, SRpcMsg *pMsg) {
523,200,919✔
408
  int32_t code = 0;
523,200,919✔
409
  int32_t lino = 0;
523,200,919✔
410

411
  if (tsBypassFlag & TSDB_BYPASS_RA_RPC_RECV_SUBMIT) {
523,200,919✔
412
    return TSDB_CODE_MSG_PREPROCESSED;
65✔
413
  }
414

415
  SDecoder *pCoder = &(SDecoder){0};
523,200,854✔
416

417
  if (taosHton64(((SSubmitReq2Msg *)pMsg->pCont)->version) != 1) {
523,202,467✔
UNCOV
418
    code = TSDB_CODE_INVALID_MSG;
×
UNCOV
419
    TSDB_CHECK_CODE(code, lino, _exit);
×
420
  }
421

422
  tDecoderInit(pCoder, (uint8_t *)pMsg->pCont + sizeof(SSubmitReq2Msg), pMsg->contLen - sizeof(SSubmitReq2Msg));
523,201,899✔
423

424
  if (tStartDecode(pCoder) < 0) {
523,201,413✔
UNCOV
425
    code = TSDB_CODE_INVALID_MSG;
×
UNCOV
426
    TSDB_CHECK_CODE(code, lino, _exit);
×
427
  }
428

429
  uint64_t nSubmitTbData;
523,197,673✔
430
  if (tDecodeU64v(pCoder, &nSubmitTbData) < 0) {
523,203,353✔
UNCOV
431
    code = TSDB_CODE_INVALID_MSG;
×
UNCOV
432
    TSDB_CHECK_CODE(code, lino, _exit);
×
433
  }
434

435
  int64_t btimeMs = taosGetTimestampMs();
523,201,899✔
436
  int64_t ctimeMs = btimeMs;
523,201,899✔
437
  for (uint64_t i = 0; i < nSubmitTbData; i++) {
1,071,616,710✔
438
    code = vnodePreProcessSubmitTbData(pVnode, pCoder, btimeMs, ctimeMs);
548,485,247✔
439
    TSDB_CHECK_CODE(code, lino, _exit);
548,483,018✔
440
  }
441

442
  tEndDecode(pCoder);
523,131,463✔
443

444
_exit:
523,201,328✔
445
  tDecoderClear(pCoder);
523,201,328✔
446
  if (code) {
523,198,409✔
447
    vError("vgId:%d, %s:%d failed to preprocess submit request since %s, msg type:%s", TD_VID(pVnode), __func__, lino,
68,207✔
448
           tstrerror(code), TMSG_INFO(pMsg->msgType));
449
  }
450
  return code;
523,196,820✔
451
}
452

453
static int32_t vnodePreProcessDeleteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
1,750,580✔
454
  int32_t code = 0;
1,750,580✔
455

456
  int32_t    size;
457
  int32_t    ret;
458
  uint8_t   *pCont;
459
  SEncoder  *pCoder = &(SEncoder){0};
1,750,580✔
460
  SDeleteRes res = {0};
1,750,580✔
461

462
  SReadHandle handle = {0};
1,750,580✔
463
  handle.vnode = pVnode;
1,750,580✔
464
  handle.pMsgCb = &pVnode->msgCb;
1,750,580✔
465
  handle.skipRollup = 1;
1,750,580✔
466
  initStorageAPI(&handle.api);
1,750,580✔
467

468
  code = qWorkerProcessDeleteMsg(&handle, pVnode->pQuery, pMsg, &res);
1,750,018✔
469
  if (code) goto _exit;
1,750,580✔
470

471
  res.ctimeMs = taosGetTimestampMs();
1,750,580✔
472
  // malloc and encode
473
  tEncodeSize(tEncodeDeleteRes, &res, size, ret);
1,750,580✔
474
  pCont = rpcMallocCont(size + sizeof(SMsgHead));
1,750,018✔
475

476
  ((SMsgHead *)pCont)->contLen = size + sizeof(SMsgHead);
1,750,580✔
477
  ((SMsgHead *)pCont)->vgId = TD_VID(pVnode);
1,750,580✔
478

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

485
  rpcFreeCont(pMsg->pCont);
1,750,018✔
486
  pMsg->pCont = pCont;
1,750,018✔
487
  pMsg->contLen = size + sizeof(SMsgHead);
1,750,018✔
488

489
  taosArrayDestroy(res.uidList);
1,750,018✔
490

491
_exit:
1,750,018✔
492
  return code;
1,750,018✔
493
}
494

UNCOV
495
static int32_t vnodePreProcessBatchDeleteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
×
496
  int32_t code = 0;
×
497
  int32_t lino = 0;
×
498

UNCOV
499
  int64_t         ctimeMs = taosGetTimestampMs();
×
500
  SBatchDeleteReq pReq = {0};
×
UNCOV
501
  SDecoder       *pCoder = &(SDecoder){0};
×
502

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

UNCOV
505
  if (tDecodeSBatchDeleteReqSetCtime(pCoder, &pReq, ctimeMs) < 0) {
×
506
    code = TSDB_CODE_INVALID_MSG;
×
507
  }
508

509
  tDecoderClear(pCoder);
×
510
  taosArrayDestroy(pReq.deleteReqs);
×
511

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

520
static int32_t vnodePreProcessArbCheckSyncMsg(SVnode *pVnode, SRpcMsg *pMsg) {
47,698✔
521
  int32_t ret = 0;
47,698✔
522
  if ((ret = vnodeCheckState(pVnode)) != 0) {
47,698✔
523
    vDebug("vgId:%d, failed to preprocess vnode-arb-check-sync request since %s", TD_VID(pVnode), tstrerror(ret));
11,817✔
524
    return 0;
11,817✔
525
  }
526

527
  SVArbCheckSyncReq syncReq = {0};
35,881✔
528

529
  if (tDeserializeSVArbCheckSyncReq((char *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead),
35,881✔
530
                                    &syncReq) != 0) {
UNCOV
531
    return TSDB_CODE_INVALID_MSG;
×
532
  }
533

534
  ret = vnodeCheckToken(pVnode, syncReq.member0Token, syncReq.member1Token);
35,881✔
535
  if (ret != 0) {
35,881✔
536
    vError("vgId:%d, failed to preprocess vnode-arb-check-sync request since %s", TD_VID(pVnode), tstrerror(ret));
34,937✔
537
  }
538

539
  int32_t code = terrno;
35,881✔
540
  tFreeSVArbCheckSyncReq(&syncReq);
35,881✔
541

542
  return code;
35,881✔
543
}
544

545
int32_t vnodePreProcessDropTbMsg(SVnode *pVnode, SRpcMsg *pMsg) {
1,531,701✔
546
  int32_t          code = TSDB_CODE_SUCCESS;
1,531,701✔
547
  int32_t          lino = 0;
1,531,701✔
548
  int32_t          size = 0;
1,531,701✔
549
  SDecoder         dc = {0};
1,531,701✔
550
  SEncoder         ec = {0};
1,531,701✔
551
  SVDropTbBatchReq receivedBatchReqs = {0};
1,531,701✔
552
  SVDropTbBatchReq sentBatchReqs = {0};
1,531,701✔
553

554
  if (pVnode->config.isAudit) {
1,531,701✔
UNCOV
555
    return TSDB_CODE_PAR_PERMISSION_DENIED;
×
556
  }
557

558
  tDecoderInit(&dc, POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), pMsg->contLen - sizeof(SMsgHead));
1,531,701✔
559

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

571
  for (int32_t i = 0; i < receivedBatchReqs.nReqs; ++i) {
3,110,192✔
572
    SVDropTbReq *pReq = receivedBatchReqs.pReqs + i;
1,578,491✔
573
    tb_uid_t     uid = metaGetTableEntryUidByName(pVnode->pMeta, pReq->name);
1,578,491✔
574
    if (uid == 0) {
1,578,491✔
UNCOV
575
      vWarn("vgId:%d, preprocess drop ctb: %s not found", TD_VID(pVnode), pReq->name);
×
UNCOV
576
      continue;
×
577
    }
578
    pReq->uid = uid;
1,578,491✔
579
    vDebug("vgId:%d %s for: %s, uid: %" PRId64, TD_VID(pVnode), __func__, pReq->name, pReq->uid);
1,578,491✔
580
    if (taosArrayPush(sentBatchReqs.pArray, pReq) == NULL) {
3,156,982✔
UNCOV
581
      code = terrno;
×
UNCOV
582
      goto _exit;
×
583
    }
584
  }
585
  sentBatchReqs.nReqs = sentBatchReqs.pArray->size;
1,531,701✔
586

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

596
_exit:
1,531,701✔
597
  tDecoderClear(&dc);
1,531,701✔
598
  if (sentBatchReqs.pArray) {
1,531,701✔
599
    taosArrayDestroy(sentBatchReqs.pArray);
1,531,701✔
600
  }
601
  return code;
1,531,701✔
602
}
603

604
int32_t vnodePreProcessSsMigrateFileSetReq(SVnode *pVnode, SRpcMsg *pMsg) {
3,740✔
605
  int32_t              code = TSDB_CODE_SUCCESS, lino = 0;
3,740✔
606
  SSsMigrateFileSetReq req = {0};
3,740✔
607

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

615
  req.nodeId = vnodeNodeId(pVnode);
3,740✔
616
  TAOS_UNUSED(tSerializeSSsMigrateFileSetReq(POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)),
3,740✔
617
                                             pMsg->contLen - sizeof(SMsgHead), &req));
618

619
_exit:
3,740✔
620
  return code;
3,740✔
621
}
622

623
int32_t vnodePreProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
612,658,961✔
624
  int32_t code = 0;
612,658,961✔
625

626
  if (pVnode->mounted) {
612,658,961✔
627
    vError("vgId:%d, mountVgId:%d, skip write msg:%s", TD_VID(pVnode), pVnode->config.mountVgId,
1,310✔
628
           TMSG_INFO(pMsg->msgType));
629
    return TSDB_CODE_OPS_NOT_SUPPORT;
1,310✔
630
  }
631

632
  switch (pMsg->msgType) {
612,660,291✔
633
    case TDMT_VND_CREATE_TABLE: {
42,307,268✔
634
      code = vnodePreProcessCreateTableMsg(pVnode, pMsg);
42,307,268✔
635
    } break;
42,304,134✔
636
    case TDMT_VND_ALTER_TABLE: {
12,293,712✔
637
      code = vnodePreProcessAlterTableMsg(pVnode, pMsg);
12,293,712✔
638
    } break;
12,293,712✔
639
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
11,063,613✔
640
    case TDMT_VND_DROP_TTL_TABLE: {
641
      code = vnodePreProcessDropTtlMsg(pVnode, pMsg);
11,063,613✔
642
    } break;
11,052,282✔
643
    case TDMT_VND_SUBMIT: {
523,202,652✔
644
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.preprocess_time, METRIC_LEVEL_LOW,
523,202,652✔
645
                           { code = vnodePreProcessSubmitMsg(pVnode, pMsg); });
646
    } break;
523,200,956✔
647
    case TDMT_VND_DELETE: {
1,750,580✔
648
      code = vnodePreProcessDeleteMsg(pVnode, pMsg);
1,750,580✔
649
    } break;
1,750,018✔
UNCOV
650
    case TDMT_VND_BATCH_DEL: {
×
UNCOV
651
      code = vnodePreProcessBatchDeleteMsg(pVnode, pMsg);
×
UNCOV
652
    } break;
×
653
    case TDMT_VND_ARB_CHECK_SYNC: {
47,698✔
654
      code = vnodePreProcessArbCheckSyncMsg(pVnode, pMsg);
47,698✔
655
    } break;
47,698✔
656
    case TDMT_VND_DROP_TABLE: {
1,531,701✔
657
      code = vnodePreProcessDropTbMsg(pVnode, pMsg);
1,531,701✔
658
    } break;
1,531,701✔
659
#ifdef TD_ENTERPRISE
660
    case TDMT_VND_SSMIGRATE_FILESET: {
3,740✔
661
      code = vnodePreProcessSsMigrateFileSetReq(pVnode, pMsg);
3,740✔
662
    } break;
3,740✔
663
#endif
664
    case TDMT_VND_AUDIT_RECORD: {
251✔
665
      vTrace("vgId:%d, pre process TDMT_VND_AUDIT_RECORD", TD_VID(pVnode));
251✔
666
    } break;
212✔
667
    default:
20,459,546✔
668
      break;
20,459,546✔
669
  }
670

671
  if (code && code != TSDB_CODE_MSG_PREPROCESSED) {
612,643,999✔
672
    vError("vgId:%d, failed to preprocess write request since %s, msg type:%s", TD_VID(pVnode), tstrerror(code),
103,144✔
673
           TMSG_INFO(pMsg->msgType));
674
  }
675
  return code;
612,643,732✔
676
}
677

678
static int32_t inline vnodeSubmitSubRowBlobData(SVnode *pVnode, SSubmitTbData *pSubmitTbData) {
31,562✔
679
  int32_t code = 0;
31,562✔
680
  int32_t lino = 0;
31,562✔
681

682
  int64_t   st = taosGetTimestampUs();
31,562✔
683
  SBlobSet *pBlobSet = pSubmitTbData->pBlobSet;
31,562✔
684
  int32_t   sz = taosArrayGetSize(pBlobSet->pSeqTable);
31,562✔
685

686
  SBseBatch *pBatch = NULL;
31,562✔
687

688
  code = bseBatchInit(pVnode->pBse, &pBatch, sz);
31,562✔
689
  TSDB_CHECK_CODE(code, lino, _exit);
31,562✔
690

691
  SRow  **pRow = (SRow **)TARRAY_DATA(pSubmitTbData->aRowP);
31,562✔
692
  int32_t rowIdx = -1;
31,562✔
693
  for (int32_t i = 0; i < sz; i++) {
24,647,050✔
694
    int64_t     seq = 0;
24,615,488✔
695
    SBlobValue *p = taosArrayGet(pBlobSet->pSeqTable, i);
24,615,488✔
696
    if (p->type == TSDB_DATA_BLOB_EMPTY_VALUE || p->type == TSDB_DATA_BLOB_NULL_VALUE) {
24,615,488✔
697
      // skip empty or null blob
698
      continue;
2,444,456✔
699
    }
700

701
    code = bseBatchPut(pBatch, &seq, pBlobSet->data + p->offset, p->len);
22,171,032✔
702
    TSDB_CHECK_CODE(code, lino, _exit);
22,171,032✔
703

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

711
    if (tPutU64(row->data + p->dataOffset, seq) < 0) {
44,342,064✔
UNCOV
712
      code = TSDB_CODE_INVALID_MSG;
×
UNCOV
713
      TSDB_CHECK_CODE(code, lino, _exit);
×
714
    }
715
  }
716

717
  code = bseCommitBatch(pVnode->pBse, pBatch);
31,562✔
718
  TSDB_CHECK_CODE(code, lino, _exit);
31,562✔
719

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

732
static int32_t inline vnodeSubmitSubColBlobData(SVnode *pVnode, SSubmitTbData *pSubmitTbData) {
83✔
733
  int32_t code = 0;
83✔
734
  int32_t lino = 0;
83✔
735

736
  int32_t   blobColIdx = 0;
83✔
737
  SColData *pBlobCol = NULL;
83✔
738
  int64_t   st = taosGetTimestampUs();
83✔
739
  SBlobSet *pBlobSet = pSubmitTbData->pBlobSet;
83✔
740
  int32_t   sz = taosArrayGetSize(pBlobSet->pSeqTable);
83✔
741

742
  SBseBatch *pBatch = NULL;
83✔
743

744
  code = bseBatchInit(pVnode->pBse, &pBatch, sz);
83✔
745
  TSDB_CHECK_CODE(code, lino, _exit);
83✔
746

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

760
  int32_t offset = 0;
83✔
761
  // int32_t   rowIdx = -1;
762
  for (int32_t i = 0; i < sz; i++) {
176✔
763
    int64_t     seq = 0;
93✔
764
    SBlobValue *p = taosArrayGet(pBlobSet->pSeqTable, i);
93✔
765
    if (p->type == TSDB_DATA_BLOB_EMPTY_VALUE || p->type == TSDB_DATA_BLOB_NULL_VALUE) {
93✔
766
      // skip empty or null blob
767
      continue;
52✔
768
    }
769
    code = bseBatchPut(pBatch, &seq, pBlobSet->data + p->offset, p->len);
41✔
770
    TSDB_CHECK_CODE(code, lino, _exit);
41✔
771

772
    memcpy(pBlobCol->pData + offset, (void *)&seq, BSE_SEQUECE_SIZE);
41✔
773
    offset += BSE_SEQUECE_SIZE;
41✔
774
  }
775

776
  code = bseCommitBatch(pVnode->pBse, pBatch);
83✔
777
  TSDB_CHECK_CODE(code, lino, _exit);
83✔
778

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

800
  return code;
31,645✔
801
}
802

803
int32_t vnodeProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg, int64_t ver, SRpcMsg *pRsp) {
732,105,112✔
804
  int32_t code = 0;
732,105,112✔
805
  int32_t lino = 0;
732,105,112✔
806
  void   *ptr = NULL;
732,105,112✔
807
  void   *pReq;
808
  int32_t len;
809

810
  (void)taosThreadMutexLock(&pVnode->mutex);
732,105,112✔
811
  if (pVnode->disableWrite) {
732,158,904✔
UNCOV
812
    (void)taosThreadMutexUnlock(&pVnode->mutex);
×
UNCOV
813
    vError("vgId:%d write is disabled for snapshot, version:%" PRId64, TD_VID(pVnode), ver);
×
UNCOV
814
    return TSDB_CODE_VND_WRITE_DISABLED;
×
815
  }
816
  (void)taosThreadMutexUnlock(&pVnode->mutex);
732,131,543✔
817

818
  if (ver <= pVnode->state.applied) {
732,110,265✔
UNCOV
819
    vError("vgId:%d, duplicate write request. ver: %" PRId64 ", applied: %" PRId64, TD_VID(pVnode), ver,
×
820
           pVnode->state.applied);
UNCOV
821
    return terrno = TSDB_CODE_VND_DUP_REQUEST;
×
822
  }
823

824
  vGDebug(&pMsg->info.traceId,
732,118,567✔
825
          "vgId:%d, index:%" PRId64 ", process write request:%s, applied:%" PRId64 ", state.applyTerm:%" PRId64
826
          ", conn.applyTerm:%" PRId64 ", contLen:%d",
827
          TD_VID(pVnode), ver, TMSG_INFO(pMsg->msgType), pVnode->state.applied, pVnode->state.applyTerm,
828
          pMsg->info.conn.applyTerm, pMsg->contLen);
829

830
  if (!(pVnode->state.applyTerm <= pMsg->info.conn.applyTerm)) {
732,158,786✔
UNCOV
831
    vError("vgId:%d, applyTerm mismatch, expected: %" PRId64 ", received: %" PRId64, TD_VID(pVnode),
×
832
           pVnode->state.applyTerm, pMsg->info.conn.applyTerm);
UNCOV
833
    return terrno = TSDB_CODE_INTERNAL_ERROR;
×
834
  }
835

836
  if (!(pVnode->state.applied + 1 == ver)) {
732,166,578✔
UNCOV
837
    vError("vgId:%d, mountVgId:%d, ver mismatch, expected: %" PRId64 ", received: %" PRId64, TD_VID(pVnode),
×
838
           pVnode->config.mountVgId, pVnode->state.applied + 1, ver);
UNCOV
839
    return terrno = TSDB_CODE_INTERNAL_ERROR;
×
840
  }
841

842
  atomic_store_64(&pVnode->state.applied, ver);
732,166,726✔
843
  atomic_store_64(&pVnode->state.applyTerm, pMsg->info.conn.applyTerm);
732,172,167✔
844

845
  if (!syncUtilUserCommit(pMsg->msgType)) goto _exit;
732,170,770✔
846

847
  // skip header
848
  pReq = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
721,394,734✔
849
  len = pMsg->contLen - sizeof(SMsgHead);
721,395,194✔
850
  bool needCommit = false;
721,394,261✔
851
  bool forceTrimWal = false;
721,394,261✔
852

853
  switch (pMsg->msgType) {
721,394,261✔
854
    /* META */
855
    case TDMT_VND_CREATE_STB:
5,172,013✔
856
      code = vnodeProcessCreateStbReq(pVnode, ver, pReq, len, pRsp);
5,172,013✔
857
      TSDB_CHECK_CODE(code, lino, _err);
5,170,945✔
858
      break;
5,167,937✔
859
    case TDMT_VND_ALTER_STB:
7,548,571✔
860
      code = vnodeProcessAlterStbReq(pVnode, ver, pReq, len, pRsp);
7,548,571✔
861
      TSDB_CHECK_CODE(code, lino, _err);
7,548,571✔
862
      break;
7,548,571✔
863
    case TDMT_VND_DROP_STB:
903,205✔
864
      code = vnodeProcessDropStbReq(pVnode, ver, pReq, len, pRsp, pMsg);
903,205✔
865
      TSDB_CHECK_CODE(code, lino, _err);
903,205✔
866
      break;
903,205✔
867
    case TDMT_VND_CREATE_TABLE:
44,891,823✔
868
      code = vnodeProcessCreateTbReq(pVnode, ver, pReq, len, pRsp, pMsg);
44,891,823✔
869
      TSDB_CHECK_CODE(code, lino, _err);
44,885,336✔
870
      break;
44,885,336✔
871
    case TDMT_VND_ALTER_TABLE:
12,294,092✔
872
      code = vnodeProcessAlterTbReq(pVnode, ver, pReq, len, pRsp);
12,294,092✔
873
      TSDB_CHECK_CODE(code, lino, _err);
12,294,092✔
874
      break;
12,294,092✔
875
    case TDMT_VND_DROP_TABLE:
1,531,766✔
876
    case TDMT_VND_SNODE_DROP_TABLE:
877
      code = vnodeProcessDropTbReq(pVnode, ver, pReq, len, pRsp, pMsg);
1,531,766✔
878
      TSDB_CHECK_CODE(code, lino, _err);
1,531,766✔
879
      break;
1,531,766✔
880
    case TDMT_VND_CREATE_RSMA:
39,694✔
881
      code = vnodeProcessCreateRsmaReq(pVnode, ver, pReq, len, pRsp);
39,694✔
882
      TSDB_CHECK_CODE(code, lino, _err);
39,694✔
883
      break;
39,694✔
884
    case TDMT_VND_DROP_RSMA:
19,980✔
885
      code = vnodeProcessDropRsmaReq(pVnode, ver, pReq, len, pRsp);
19,980✔
886
      TSDB_CHECK_CODE(code, lino, _err);
19,980✔
887
      break;
19,980✔
888
    case TDMT_VND_ALTER_RSMA:
12,234✔
889
      code = vnodeProcessAlterRsmaReq(pVnode, ver, pReq, len, pRsp);
12,234✔
890
      TSDB_CHECK_CODE(code, lino, _err);
12,234✔
891
      break;
12,234✔
892
    case TDMT_VND_DROP_TTL_TABLE:
×
UNCOV
893
      code = vnodeProcessDropTtlTbReq(pVnode, ver, pReq, len, pRsp);
×
UNCOV
894
      TSDB_CHECK_CODE(code, lino, _err);
×
UNCOV
895
      break;
×
896
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
11,781✔
897
      code = vnodeProcessFetchTtlExpiredTbs(pVnode, ver, pReq, len, pRsp);
11,781✔
898
      TSDB_CHECK_CODE(code, lino, _err);
11,781✔
899
      break;
11,781✔
900
    case TDMT_VND_TRIM:
74,962✔
901
      code = vnodeProcessTrimReq(pVnode, ver, pReq, len, pRsp);
74,962✔
902
      TSDB_CHECK_CODE(code, lino, _err);
74,962✔
903
      break;
74,962✔
904
      case TDMT_VND_TRIM_WAL:
6,660✔
905
        needCommit = true;
6,660✔
906
        forceTrimWal = true;
6,660✔
907
        break;
6,660✔
908
#ifdef TD_ENTERPRISE
909
    case TDMT_VND_SSMIGRATE_FILESET:
3,740✔
910
      if (vnodeProcessSsMigrateFileSetReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
3,740✔
911
      break;
3,740✔
912
    case TDMT_VND_FOLLOWER_SSMIGRATE:
6,732✔
913
      if (vnodeProcessFollowerSsMigrateReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
6,732✔
914
      break;
6,732✔
UNCOV
915
    case TDMT_VND_KILL_SSMIGRATE:
×
UNCOV
916
      if (vnodeProcessKillSsMigrateReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
UNCOV
917
      break;
×
918
#endif
919

920
    /* TSDB */
921
    case TDMT_VND_SUBMIT: {
631,180,884✔
922
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.apply_time, METRIC_LEVEL_LOW, {
631,180,884✔
923
        if (vnodeProcessSubmitReq(pVnode, ver, pMsg->pCont, pMsg->contLen, pRsp, pMsg) < 0) goto _err;
924
      });
925
      METRICS_UPDATE(pVnode->writeMetrics.apply_bytes, METRIC_LEVEL_LOW, (int64_t)pMsg->contLen);
631,131,985✔
926
      break;
631,124,795✔
927
    }
928
    case TDMT_VND_DELETE:
2,134,224✔
929
      code = vnodeProcessDeleteReq(pVnode, ver, pReq, len, pRsp, pMsg);
2,134,224✔
930
      TSDB_CHECK_CODE(code, lino, _err);
2,134,224✔
931
      break;
2,134,224✔
932
    case TDMT_VND_BATCH_DEL:
×
UNCOV
933
      code = vnodeProcessBatchDeleteReq(pVnode, ver, pReq, len, pRsp);
×
UNCOV
934
      TSDB_CHECK_CODE(code, lino, _err);
×
UNCOV
935
      break;
×
936
      /* TQ */
937
#if defined(USE_TQ) || defined(USE_STREAM)
938
    case TDMT_VND_TMQ_SUBSCRIBE:
1,218,332✔
939
      code = tqProcessSubscribeReq(pVnode->pTq, ver, pReq, len);
1,218,332✔
940
      TSDB_CHECK_CODE(code, lino, _err);
1,216,024✔
941
      break;
1,216,024✔
942
    case TDMT_VND_TMQ_DELETE_SUB:
267,765✔
943
      code = tqProcessDeleteSubReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen);
267,765✔
944
      TSDB_CHECK_CODE(code, lino, _err);
267,166✔
945
      break;
267,166✔
946
    case TDMT_VND_TMQ_COMMIT_OFFSET:
4,336,248✔
947
      code = tqProcessOffsetCommitReq(pVnode->pTq, ver, pReq, len);
4,336,248✔
948
      TSDB_CHECK_CODE(code, lino, _err);
4,335,672✔
949
      break;
4,335,672✔
950
#endif
951
    case TDMT_VND_ALTER_CONFIRM:
4,199,548✔
952
      needCommit = pVnode->config.hashChange;
4,199,548✔
953
      code = vnodeProcessAlterConfirmReq(pVnode, ver, pReq, len, pRsp);
4,199,548✔
954
      TSDB_CHECK_CODE(code, lino, _err);
4,199,548✔
955
      break;
4,199,548✔
956
    case TDMT_VND_ALTER_CONFIG:
322,498✔
957
      vnodeProcessAlterConfigReq(pVnode, ver, pReq, len, pRsp);
322,498✔
958
      break;
323,188✔
959
    case TDMT_VND_COMMIT:
5,112,720✔
960
      needCommit = true;
5,112,720✔
961
      break;
5,112,720✔
962
    case TDMT_VND_CREATE_INDEX:
17,787✔
963
      vnodeProcessCreateIndexReq(pVnode, ver, pReq, len, pRsp);
17,787✔
964
      break;
17,787✔
965
    case TDMT_VND_DROP_INDEX:
14,466✔
966
      vnodeProcessDropIndexReq(pVnode, ver, pReq, len, pRsp);
14,466✔
967
      break;
14,466✔
968
    case TDMT_VND_COMPACT:
68,608✔
969
      vnodeProcessCompactVnodeReq(pVnode, ver, pReq, len, pRsp);
68,608✔
970
      goto _exit;
68,608✔
971
    case TDMT_VND_SCAN:
481✔
972
      vnodeProcessScanVnodeReq(pVnode, ver, pReq, len, pRsp);
481✔
973
      goto _exit;
481✔
UNCOV
974
    case TDMT_SYNC_CONFIG_CHANGE:
×
UNCOV
975
      vnodeProcessConfigChangeReq(pVnode, ver, pReq, len, pRsp);
×
UNCOV
976
      break;
×
977
#ifdef TD_ENTERPRISE
978
    case TDMT_VND_KILL_COMPACT:
1,504✔
979
      vnodeProcessKillCompactReq(pVnode, ver, pReq, len, pRsp);
1,504✔
980
      break;
1,504✔
981
#endif
982
    case TDMT_VND_KILL_SCAN:
86✔
983
      vnodeProcessKillScanReq(pVnode, ver, pReq, len, pRsp);
86✔
984
      break;
86✔
UNCOV
985
    case TDMT_VND_KILL_TRIM:
×
UNCOV
986
      vnodeProcessKillRetentionReq(pVnode, ver, pReq, len, pRsp);
×
UNCOV
987
      break;
×
988
    /* ARB */
989
    case TDMT_VND_ARB_CHECK_SYNC:
1,890✔
990
      vnodeProcessArbCheckSyncReq(pVnode, pReq, len, pRsp);
1,890✔
991
      break;
1,890✔
992
    case TDMT_VND_AUDIT_RECORD:
212✔
993
      vTrace("vgId:%d, processed audit msg", TD_VID(pVnode));
212✔
994
      code = vnodeProcessAuditRecordReq(pVnode, ver, pReq, len, pMsg);
212✔
995
      pRsp->code = code;
212✔
996
      pRsp->msgType = TDMT_VND_AUDIT_RECORD_RSP;
212✔
997
      pRsp->pCont = NULL;
212✔
998
      pRsp->contLen = 0;
212✔
999
      if (code) {
212✔
1000
        goto _err;
×
1001
      }
1002
      break;
212✔
UNCOV
1003
    default:
×
UNCOV
1004
      vError("vgId:%d, unprocessed msg, %d", TD_VID(pVnode), pMsg->msgType);
×
UNCOV
1005
      return TSDB_CODE_INVALID_MSG;
×
1006
  }
1007

1008
  vGDebug(&pMsg->info.traceId, "vgId:%d, index:%" PRId64 ", msg processed, code:0x%x", TD_VID(pVnode), ver, pRsp->code);
721,255,972✔
1009

1010
  walApplyVer(pVnode->pWal, ver);
721,269,208✔
1011

1012
  if (pVnode->pTq) {
721,279,278✔
1013
    code = tqPushMsg(pVnode->pTq, pMsg->msgType);
721,279,278✔
1014
    if (code) {
721,280,719✔
1015
      vError("vgId:%d, failed to push msg to TQ since %s", TD_VID(pVnode), tstrerror(terrno));
288✔
1016
      return code;
288✔
1017
    }
1018
  }
1019

1020
  // commit if need
1021
  if (needCommit) {
721,280,431✔
1022
    vInfo("vgId:%d, commit at version %" PRId64, TD_VID(pVnode), ver);
5,143,992✔
1023
    code = vnodeAsyncCommit(pVnode, forceTrimWal);
5,143,992✔
1024
    if (code) {
5,143,992✔
UNCOV
1025
      vError("vgId:%d, failed to vnode async commit since %s.", TD_VID(pVnode), tstrerror(terrno));
×
UNCOV
1026
      goto _err;
×
1027
    }
1028

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

1039
_exit:
721,278,447✔
1040
  return 0;
732,121,510✔
1041

1042
_err:
48,694✔
1043
  if (code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
48,694✔
1044
    vInfo("vgId:%d, process %s request failed since %s, lino 1:%d ver:%" PRId64, TD_VID(pVnode),
3,008✔
1045
          TMSG_INFO(pMsg->msgType), tstrerror(code), lino, ver);
1046
  } else if (code) {
45,686✔
UNCOV
1047
    vError("vgId:%d, process %s request failed since %s, lino 2:%d ver:%" PRId64, TD_VID(pVnode),
×
1048
           TMSG_INFO(pMsg->msgType), tstrerror(code), lino, ver);
1049
  }
1050

1051
  return code;
48,694✔
1052
}
1053

1054
int32_t vnodePreprocessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg) {
413,428,620✔
1055
  if (TDMT_SCH_QUERY != pMsg->msgType && TDMT_SCH_MERGE_QUERY != pMsg->msgType) {
413,428,620✔
1056
    return 0;
81,966,852✔
1057
  }
1058

1059
  int32_t qType = 0;
331,484,152✔
1060
  return qWorkerPreprocessQueryMsg(pVnode->pQuery, pMsg, (TDMT_SCH_QUERY == pMsg->msgType), &qType);
331,491,378✔
1061
}
1062

1063
int32_t vnodeProcessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
413,466,995✔
1064
  const STraceId *trace = &pMsg->info.traceId;
413,466,995✔
1065
  vDebug("vgId:%d, msg:%s, %p in vnode query queue is processing, %" PRIx64 ":%" PRIx64, 
413,469,929✔
1066
    pVnode->config.vgId, TMSG_INFO(pMsg->msgType), pMsg, TRACE_GET_ROOTID(trace), TRACE_GET_MSGID(trace));
1067

1068
  if (pMsg->msgType == TDMT_VND_TMQ_CONSUME && !syncIsReadyForRead(pVnode->sync)) {
413,477,498✔
1069
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
393,770✔
1070
    return 0;
393,770✔
1071
  }
1072

1073
  if (pMsg->msgType == TDMT_VND_TMQ_CONSUME && !pVnode->restored) {
413,082,895✔
UNCOV
1074
    vnodeRedirectRpcMsg(pVnode, pMsg, TSDB_CODE_SYN_RESTORING);
×
UNCOV
1075
    return 0;
×
1076
  }
1077

1078
  SReadHandle handle = {0};
413,080,961✔
1079
  handle.vnode = pVnode;
413,079,474✔
1080
  handle.pMsgCb = &pVnode->msgCb;
413,079,474✔
1081
  handle.pWorkerCb = pInfo->workerCb;
413,067,569✔
1082
  initStorageAPI(&handle.api);
413,068,112✔
1083
  int32_t code = TSDB_CODE_SUCCESS;
413,065,622✔
1084
  bool    redirected = false;
413,065,622✔
1085

1086
  switch (pMsg->msgType) {
413,065,622✔
1087
    case TDMT_SCH_QUERY:
271,702,218✔
1088
      if (!syncIsReadyForRead(pVnode->sync)) {
271,702,218✔
1089
        pMsg->code = (terrno) ? terrno : TSDB_CODE_SYN_NOT_LEADER;
1,396,230✔
1090
        redirected = true;
1,396,230✔
1091
      }
1092
      code = qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
271,699,560✔
1093
      if (redirected) {
271,673,109✔
1094
        vnodeRedirectRpcMsg(pVnode, pMsg, pMsg->code);
1,396,230✔
1095
        return 0;
1,396,230✔
1096
      }
1097

1098
      return code;
270,276,879✔
1099
    case TDMT_SCH_MERGE_QUERY:
59,797,597✔
1100
      return qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
59,797,597✔
1101
    case TDMT_SCH_QUERY_CONTINUE:
40,661,855✔
1102
      return qWorkerProcessCQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
40,661,855✔
1103
    case TDMT_VND_TMQ_CONSUME:
30,733,475✔
1104
      return tqProcessPollReq(pVnode->pTq, pMsg);
30,733,475✔
1105
    case TDMT_VND_TMQ_CONSUME_PUSH:
10,172,775✔
1106
      return tqProcessPollPush(pVnode->pTq);
10,172,775✔
UNCOV
1107
    default:
×
UNCOV
1108
      vError("unknown msg type:%d in query queue", pMsg->msgType);
×
UNCOV
1109
      return TSDB_CODE_APP_ERROR;
×
1110
  }
1111
}
1112

1113
int32_t vnodeProcessFetchMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
984,867,391✔
1114
  int32_t code = TSDB_CODE_SUCCESS;
984,867,391✔
1115
  const STraceId *trace = &pMsg->info.traceId;
984,867,391✔
1116
  vDebug("vgId:%d, msg:%s, %p in fetch queue is processing, %" PRIx64 ":%" PRIx64, 
984,910,428✔
1117
    pVnode->config.vgId, TMSG_INFO(pMsg->msgType), pMsg, TRACE_GET_ROOTID(trace), TRACE_GET_MSGID(trace));
1118
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
984,998,059✔
1119
       pMsg->msgType == TDMT_VND_BATCH_META || pMsg->msgType == TDMT_VND_TABLE_NAME ||
684,163,041✔
1120
       pMsg->msgType == TDMT_VND_VSUBTABLES_META || pMsg->msgType == TDMT_VND_VSTB_REF_DBS) &&
655,413,711✔
1121
      !syncIsReadyForRead(pVnode->sync)) {
329,595,804✔
1122
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
522,536✔
1123
    return 0;
522,536✔
1124
  }
1125

1126
  switch (pMsg->msgType) {
984,508,940✔
1127
    case TDMT_SCH_FETCH:
457,642,359✔
1128
    case TDMT_SCH_MERGE_FETCH:
1129
      return qWorkerProcessFetchMsg(pVnode, pVnode->pQuery, pMsg, 0);
457,642,359✔
UNCOV
1130
    case TDMT_SCH_FETCH_RSP:
×
UNCOV
1131
      return qWorkerProcessRspMsg(pVnode, pVnode->pQuery, pMsg, 0);
×
1132
    // case TDMT_SCH_CANCEL_TASK:
1133
    //   return qWorkerProcessCancelMsg(pVnode, pVnode->pQuery, pMsg, 0);
1134
    case TDMT_SCH_DROP_TASK:
331,276,453✔
1135
      return qWorkerProcessDropMsg(pVnode, pVnode->pQuery, pMsg, 0);
331,276,453✔
1136
    case TDMT_SCH_TASK_NOTIFY:
12,922✔
1137
      return qWorkerProcessNotifyMsg(pVnode, pVnode->pQuery, pMsg, 0);
12,922✔
1138
    case TDMT_SCH_QUERY_HEARTBEAT:
166,191,659✔
1139
      return qWorkerProcessHbMsg(pVnode, pVnode->pQuery, pMsg, 0);
166,191,659✔
1140
    case TDMT_VND_TABLE_META:
886,754✔
1141
    case TDMT_VND_TABLE_NAME:
1142
      return vnodeGetTableMeta(pVnode, pMsg, true);
886,754✔
UNCOV
1143
    case TDMT_VND_TABLE_CFG:
×
UNCOV
1144
      return vnodeGetTableCfg(pVnode, pMsg, true);
×
1145
    case TDMT_VND_BATCH_META: {
28,228,928✔
1146
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.fetch_batch_meta_time, METRIC_LEVEL_LOW,
28,228,928✔
1147
                           { code = vnodeGetBatchMeta(pVnode, pMsg); });
1148
      METRICS_UPDATE(pVnode->writeMetrics.fetch_batch_meta_count, METRIC_LEVEL_LOW, 1);
28,230,095✔
1149
      return code;
28,225,424✔
1150
    }
1151
    case TDMT_VND_VSUBTABLES_META:
×
UNCOV
1152
      return vnodeGetVSubtablesMeta(pVnode, pMsg);
×
UNCOV
1153
    case TDMT_VND_VSTB_REF_DBS:
×
UNCOV
1154
      return vnodeGetVStbRefDbs(pVnode, pMsg);
×
1155
    case TDMT_VND_QUERY_SCAN_PROGRESS:
430✔
1156
      return vnodeQueryScanProgress(pVnode, pMsg);
430✔
1157
#ifdef TD_ENTERPRISE
1158
    case TDMT_VND_QUERY_COMPACT_PROGRESS:
103,880✔
1159
      return vnodeQueryCompactProgress(pVnode, pMsg);
103,880✔
1160

1161
    case TDMT_VND_LIST_SSMIGRATE_FILESETS:
4,488✔
1162
      return vnodeListSsMigrateFileSets(pVnode, pMsg);
4,488✔
1163

1164
    case TDMT_VND_QUERY_SSMIGRATE_PROGRESS:
6,732✔
1165
      return vnodeQuerySsMigrateProgress(pVnode, pMsg);
6,732✔
1166
#endif
1167
    case TDMT_VND_QUERY_TRIM_PROGRESS:
114,267✔
1168
      return vnodeQueryRetentionProgress(pVnode, pMsg);
114,267✔
1169
      //    case TDMT_VND_TMQ_CONSUME:
1170
      //      return tqProcessPollReq(pVnode->pTq, pMsg);
1171
#ifdef USE_TQ
1172
    case TDMT_VND_TMQ_VG_WALINFO:
612✔
1173
      return tqProcessVgWalInfoReq(pVnode->pTq, pMsg);
612✔
1174
    case TDMT_VND_TMQ_VG_COMMITTEDINFO:
306✔
1175
      return tqProcessVgCommittedInfoReq(pVnode->pTq, pMsg);
306✔
1176
    case TDMT_VND_TMQ_SEEK:
1,224✔
1177
      return tqProcessSeekReq(pVnode->pTq, pMsg);
1,224✔
1178
#endif
1179
    default:
27,927✔
1180
      vError("unknown msg type:%d in fetch queue", pMsg->msgType);
27,927✔
UNCOV
1181
      return TSDB_CODE_APP_ERROR;
×
1182
  }
1183
}
1184

1185
void vnodeUpdateMetaRsp(SVnode *pVnode, STableMetaRsp *pMetaRsp) {
71,151,031✔
1186
  if (NULL == pMetaRsp) {
71,151,031✔
UNCOV
1187
    return;
×
1188
  }
1189

1190
  tstrncpy(pMetaRsp->dbFName, pVnode->config.dbname, TSDB_DB_FNAME_LEN);
71,151,031✔
1191
  pMetaRsp->dbId = pVnode->config.dbId;
71,151,571✔
1192
  pMetaRsp->vgId = TD_VID(pVnode);
71,152,944✔
1193
  pMetaRsp->precision = pVnode->config.tsdbCfg.precision;
71,150,917✔
1194
}
1195

1196
extern int32_t vnodeAsyncRetention(SVnode *pVnode, STimeWindow tw, int8_t optrType, int8_t triggerType);
1197

1198
static int32_t vnodeProcessTrimReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
74,962✔
1199
  if (!pVnode->restored) {
74,962✔
1200
    vInfo("vgId:%d, ignore trim req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
2,906✔
1201
    return 0;
2,906✔
1202
  }
1203

1204
  int32_t     code = 0;
72,056✔
1205
  SVTrimDbReq trimReq = {0};
72,056✔
1206

1207
  // decode
1208
  if (tDeserializeSVTrimDbReq(pReq, len, &trimReq) != 0) {
72,056✔
UNCOV
1209
    code = TSDB_CODE_INVALID_MSG;
×
UNCOV
1210
    goto _exit;
×
1211
  }
1212

1213
  vInfo("vgId:%d, process trim vnode request, time:%" PRIi64 ",%" PRIi64 ", optr:%d, trigger:%d", pVnode->config.vgId,
72,056✔
1214
        trimReq.tw.skey, trimReq.tw.ekey, (int32_t)trimReq.optrType, (int32_t)trimReq.triggerType);
1215

1216
  code = vnodeAsyncRetention(pVnode, trimReq.tw, (int8_t)trimReq.optrType, (int8_t)trimReq.triggerType);
72,056✔
1217

1218
_exit:
72,056✔
1219
  return code;
72,056✔
1220
}
1221

1222
extern int32_t vnodeAsyncSsMigrateFileSet(SVnode *pVnode, SSsMigrateFileSetReq *pReq);
1223

1224
extern int32_t vnodeAsyncS3Migrate(SVnode *pVnode, int64_t now);
1225

1226
static int32_t vnodeProcessSsMigrateFileSetReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
3,740✔
1227
  int32_t code = 0;
3,740✔
1228

1229
  SSsMigrateFileSetReq req = {0};
3,740✔
1230
  SSsMigrateFileSetRsp rsp = {0};
3,740✔
1231
  pRsp->msgType = TDMT_VND_SSMIGRATE_FILESET_RSP;
3,740✔
1232
  pRsp->code = 0;
3,740✔
1233
  pRsp->pCont = NULL;
3,740✔
1234
  pRsp->contLen = 0;
3,740✔
1235

1236
  if (tDeserializeSSsMigrateFileSetReq(pReq, len, &req) != 0) {
3,740✔
UNCOV
1237
    code = TSDB_CODE_INVALID_MSG;
×
UNCOV
1238
    goto _exit;
×
1239
  }
1240

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

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

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

1265
_exit:
3,740✔
1266
  pRsp->code = code;
3,740✔
1267
  return code;
3,740✔
1268
}
1269

1270
extern int32_t vnodeFollowerSsMigrate(SVnode *pVnode, SSsMigrateProgress *pReq);
1271

1272
static int32_t vnodeProcessFollowerSsMigrateReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
6,732✔
1273
  int32_t            code = 0;
6,732✔
1274
  SSsMigrateProgress req = {0};
6,732✔
1275

1276
  // decode
1277
  if (tDeserializeSSsMigrateProgress(pReq, len, &req) != 0) {
6,732✔
UNCOV
1278
    code = TSDB_CODE_INVALID_MSG;
×
UNCOV
1279
    goto _exit;
×
1280
  }
1281

1282
  code = vnodeFollowerSsMigrate(pVnode, &req);
6,732✔
1283

1284
_exit:
6,732✔
1285
  pRsp->code = code;
6,732✔
1286
  return code;
6,732✔
1287
}
1288

1289
extern int32_t vnodeKillSsMigrate(SVnode *pVnode, SVnodeKillSsMigrateReq *pReq);
1290

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

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

1301
  code = vnodeKillSsMigrate(pVnode, &req);
×
1302

UNCOV
1303
_exit:
×
UNCOV
1304
  pRsp->code = code;
×
1305
  return code;
×
1306
}
1307

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

UNCOV
1316
  if (ttlReq.nUids != taosArrayGetSize(ttlReq.pTbUids)) {
×
UNCOV
1317
    ret = TSDB_CODE_INVALID_MSG;
×
1318
    goto end;
×
1319
  }
1320

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

UNCOV
1326
  if (ttlReq.nUids > 0) {
×
1327
    int32_t code = metaDropMultipleTables(pVnode->pMeta, ver, ttlReq.pTbUids);
×
1328
    if (code) return code;
×
1329

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

UNCOV
1336
end:
×
UNCOV
1337
  taosArrayDestroy(ttlReq.pTbUids);
×
UNCOV
1338
  return ret;
×
1339
}
1340

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

1353
  if (tDeserializeSVDropTtlTableReq(pReq, len, &ttlReq) != 0) {
11,781✔
UNCOV
1354
    terrno = TSDB_CODE_INVALID_MSG;
×
UNCOV
1355
    goto _end;
×
1356
  }
1357

1358
  if (!(ttlReq.nUids == taosArrayGetSize(ttlReq.pTbUids))) {
11,781✔
UNCOV
1359
    terrno = TSDB_CODE_INVALID_MSG;
×
UNCOV
1360
    goto _end;
×
1361
  }
1362

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

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

1388
    expiredTb.name = p;
47,776✔
1389
    if (mr.me.type == TSDB_CHILD_TABLE) {
47,776✔
1390
      expiredTb.suid = mr.me.ctbEntry.suid;
30,794✔
1391
    }
1392

1393
    if (taosArrayPush(rsp.pExpiredTbs, &expiredTb) == NULL) {
95,552✔
UNCOV
1394
      goto _end;
×
1395
    }
1396
  }
1397

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

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

1420
static int32_t vnodeProcessCreateStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
5,168,977✔
1421
  int32_t        code = 0;
5,168,977✔
1422
  SVCreateStbReq req = {0};
5,168,977✔
1423
  SDecoder       coder;
5,165,565✔
1424

1425
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
5,172,013✔
1426
  pRsp->code = TSDB_CODE_SUCCESS;
5,172,013✔
1427
  pRsp->pCont = NULL;
5,172,620✔
1428
  pRsp->contLen = 0;
5,172,620✔
1429

1430
  // decode and process req
1431
  tDecoderInit(&coder, pReq, len);
5,171,912✔
1432

1433
  code = tDecodeSVCreateStbReq(&coder, &req);
5,172,620✔
1434
  if (code) {
5,162,130✔
UNCOV
1435
    pRsp->code = code;
×
UNCOV
1436
    goto _err;
×
1437
  }
1438

1439
  code = metaCreateSuperTable(pVnode->pMeta, ver, &req);
5,162,130✔
1440
  if (code) {
5,172,620✔
1441
    pRsp->code = code;
3,008✔
1442
    goto _err;
3,008✔
1443
  }
1444

1445
  tDecoderClear(&coder);
5,169,612✔
1446
  return 0;
5,168,144✔
1447

1448
_err:
3,008✔
1449
  tDecoderClear(&coder);
3,008✔
1450
  return code;
3,008✔
1451
}
1452

1453
static int32_t vnodeProcessCreateTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
44,891,823✔
1454
                                       SRpcMsg *pOriginRpc) {
1455
  SDecoder           decoder = {0};
44,891,823✔
1456
  SEncoder           encoder = {0};
44,891,823✔
1457
  int32_t            rcode = 0;
44,891,096✔
1458
  SVCreateTbBatchReq req = {0};
44,891,096✔
1459
  SVCreateTbReq     *pCreateReq;
1460
  SVCreateTbBatchRsp rsp = {0};
44,891,096✔
1461
  SVCreateTbRsp      cRsp = {0};
44,891,823✔
1462
  char               tbName[TSDB_TABLE_FNAME_LEN];
44,888,664✔
1463
  SArray            *tbUids = NULL;
44,891,823✔
1464
  SArray            *tbNames = NULL;
44,891,823✔
1465
  int64_t            tss = taosGetTimestampMs();
44,891,823✔
1466
  pRsp->msgType = TDMT_VND_CREATE_TABLE_RSP;
44,891,823✔
1467
  pRsp->code = TSDB_CODE_SUCCESS;
44,892,550✔
1468
  pRsp->pCont = NULL;
44,891,823✔
1469
  pRsp->contLen = 0;
44,891,096✔
1470

1471
  // decode
1472
  tDecoderInit(&decoder, pReq, len);
44,891,096✔
1473
  if (tDecodeSVCreateTbBatchReq(&decoder, &req) < 0) {
44,891,823✔
UNCOV
1474
    rcode = -1;
×
UNCOV
1475
    terrno = TSDB_CODE_INVALID_MSG;
×
UNCOV
1476
    goto _exit;
×
1477
  }
1478

1479
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(cRsp));
44,891,302✔
1480
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
44,890,958✔
1481
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
44,888,643✔
1482
  if (rsp.pArray == NULL || tbUids == NULL || tbNames == NULL) {
44,888,363✔
1483
    rcode = -1;
431✔
1484
    terrno = TSDB_CODE_OUT_OF_MEMORY;
431✔
UNCOV
1485
    goto _exit;
×
1486
  }
1487

1488
  // loop to create table
1489
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
97,268,962✔
1490
    pCreateReq = req.pReqs + iReq;
52,377,411✔
1491
    memset(&cRsp, 0, sizeof(cRsp));
52,375,751✔
1492

1493
    if (tsEnableAudit && tsEnableAuditCreateTable && tsAuditLevel >= AUDIT_LEVEL_CHILDTABLE) {
52,375,751✔
1494
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
110✔
1495
      if (str == NULL) {
110✔
UNCOV
1496
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
1497
        rcode = -1;
×
UNCOV
1498
        goto _exit;
×
1499
      }
1500
      tstrncpy(str, pCreateReq->name, TSDB_TABLE_FNAME_LEN);
110✔
1501
      if (taosArrayPush(tbNames, &str) == NULL) {
110✔
UNCOV
1502
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
1503
        rcode = -1;
×
UNCOV
1504
        goto _exit;
×
1505
      }
1506
    }
1507

1508
    // validate hash
1509
    (void)snprintf(tbName, TSDB_TABLE_FNAME_LEN, "%s.%s", pVnode->config.dbname, pCreateReq->name);
52,375,751✔
1510
    if (vnodeValidateTableHash(pVnode, tbName) < 0) {
52,376,718✔
1511
      cRsp.code = TSDB_CODE_VND_HASH_MISMATCH;
×
1512
      if (taosArrayPush(rsp.pArray, &cRsp) == NULL) {
×
UNCOV
1513
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1514
        rcode = -1;
×
1515
        goto _exit;
×
1516
      }
UNCOV
1517
      vError("vgId:%d create-table:%s failed due to hash value mismatch", TD_VID(pVnode), tbName);
×
UNCOV
1518
      continue;
×
1519
    }
1520

1521
    // do create table
1522
    if (metaCreateTable2(pVnode->pMeta, ver, pCreateReq, &cRsp.pMeta) < 0) {
52,372,632✔
1523
      if (pCreateReq->flags & TD_CREATE_IF_NOT_EXISTS && terrno == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
15,311✔
1524
        cRsp.code = TSDB_CODE_SUCCESS;
3,662✔
1525
      } else {
1526
        cRsp.code = terrno;
11,649✔
1527
      }
1528
    } else {
1529
      cRsp.code = TSDB_CODE_SUCCESS;
52,365,998✔
1530
      if (taosArrayPush(tbUids, &pCreateReq->uid) == NULL) {
104,732,351✔
UNCOV
1531
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
1532
        rcode = -1;
×
UNCOV
1533
        goto _exit;
×
1534
      }
1535
      vnodeUpdateMetaRsp(pVnode, cRsp.pMeta);
52,366,353✔
1536
    }
1537

1538
    if (taosArrayPush(rsp.pArray, &cRsp) == NULL) {
104,761,950✔
UNCOV
1539
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
1540
      rcode = -1;
×
UNCOV
1541
      goto _exit;
×
1542
    }
1543
  }
1544

1545
  vTrace("vgId:%d, add %d new created tables into query table list", TD_VID(pVnode), (int32_t)taosArrayGetSize(tbUids));
44,891,551✔
1546
  if (tqAddTbUidList(pVnode->pTq, tbUids) < 0) {
44,891,551✔
UNCOV
1547
    vError("vgId:%d, failed to add tbUid list since %s", TD_VID(pVnode), tstrerror(terrno));
×
1548
  }
1549

1550
  // prepare rsp
1551
  int32_t ret = 0;
44,893,277✔
1552
  tEncodeSize(tEncodeSVCreateTbBatchRsp, &rsp, pRsp->contLen, ret);
44,893,277✔
1553
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
44,887,635✔
1554
  if (pRsp->pCont == NULL) {
44,890,478✔
UNCOV
1555
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
1556
    rcode = -1;
×
UNCOV
1557
    goto _exit;
×
1558
  }
1559
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
44,887,628✔
1560
  if (tEncodeSVCreateTbBatchRsp(&encoder, &rsp) != 0) {
44,889,229✔
UNCOV
1561
    vError("vgId:%d, failed to encode create table batch response", TD_VID(pVnode));
×
1562
  }
1563

1564
  if (tsEnableAudit && tsEnableAuditCreateTable && tsAuditLevel >= AUDIT_LEVEL_CHILDTABLE) {
44,887,216✔
1565
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
110✔
1566

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

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

1582
    size_t len = 0;
110✔
1583
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
110✔
1584

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

1592
    taosStringBuilderDestroy(&sb);
110✔
1593
  }
1594

1595
_exit:
44,887,216✔
1596
  tDeleteSVCreateTbBatchReq(&req);
44,887,303✔
1597
  taosArrayDestroyEx(rsp.pArray, tFreeSVCreateTbRsp);
44,888,300✔
1598
  taosArrayDestroy(tbUids);
44,882,628✔
1599
  tDecoderClear(&decoder);
44,882,796✔
1600
  tEncoderClear(&encoder);
44,888,831✔
1601
  taosArrayDestroyP(tbNames, NULL);
44,888,759✔
1602
  return rcode;
44,890,457✔
1603
}
1604

1605
static int32_t vnodeProcessAlterStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
7,548,571✔
1606
  int32_t        code = 0;
7,548,571✔
1607
  SVCreateStbReq req = {0};
7,548,571✔
1608
  SDecoder       dc = {0};
7,548,571✔
1609

1610
  pRsp->msgType = TDMT_VND_ALTER_STB_RSP;
7,548,571✔
1611
  pRsp->code = TSDB_CODE_SUCCESS;
7,548,571✔
1612
  pRsp->pCont = NULL;
7,548,571✔
1613
  pRsp->contLen = 0;
7,548,571✔
1614

1615
  tDecoderInit(&dc, pReq, len);
7,548,571✔
1616

1617
  // decode req
1618
  code = tDecodeSVCreateStbReq(&dc, &req);
7,548,571✔
1619
  if (code) {
7,543,732✔
UNCOV
1620
    tDecoderClear(&dc);
×
UNCOV
1621
    return code;
×
1622
  }
1623

1624
  code = metaAlterSuperTable(pVnode->pMeta, ver, &req);
7,543,732✔
1625
  if (code) {
7,548,571✔
UNCOV
1626
    pRsp->code = code;
×
UNCOV
1627
    tDecoderClear(&dc);
×
UNCOV
1628
    return code;
×
1629
  }
1630

1631
  tDecoderClear(&dc);
7,548,571✔
1632

1633
  return 0;
7,548,571✔
1634
}
1635

1636
static int32_t vnodeProcessDropStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp, SRpcMsg *pOriginRpc) {
902,779✔
1637
  SVDropStbReq req = {0};
902,779✔
1638
  int32_t      rcode = TSDB_CODE_SUCCESS;
903,205✔
1639
  SDecoder     decoder = {0};
903,205✔
1640
  SArray      *tbUidList = NULL;
903,205✔
1641

1642
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
903,205✔
1643
  pRsp->pCont = NULL;
903,205✔
1644
  pRsp->contLen = 0;
903,205✔
1645

1646
  // decode request
1647
  tDecoderInit(&decoder, pReq, len);
903,205✔
1648
  if (tDecodeSVDropStbReq(&decoder, &req) < 0) {
903,205✔
UNCOV
1649
    rcode = TSDB_CODE_INVALID_MSG;
×
UNCOV
1650
    goto _exit;
×
1651
  }
1652

1653
  STraceId* trace = &(pOriginRpc->info.traceId);
903,205✔
1654

1655
  vInfo("vgId:%d, start to process vnode-drop-stb, QID:0x%" PRIx64 ":0x%" PRIx64 ", drop stb:%s", TD_VID(pVnode), trace ? trace->rootId : 0, 
903,205✔
1656
              trace ? trace->msgId : 0, req.name);
1657

1658
  // process request
1659
  tbUidList = taosArrayInit(8, sizeof(int64_t));
903,787✔
1660
  if (tbUidList == NULL) goto _exit;
903,205✔
1661
  if (metaDropSuperTable(pVnode->pMeta, ver, &req) < 0) {
903,205✔
1662
    rcode = terrno;
1,504✔
1663
    goto _exit;
1,504✔
1664
  }
1665

1666
  if (tqDeleteTbUidList(pVnode->pTq, tbUidList) < 0) {
901,701✔
UNCOV
1667
    rcode = terrno;
×
UNCOV
1668
    goto _exit;
×
1669
  }
1670

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

1681
void vnodeAlterTagForTmq(SVnode *pVnode, const char* tbName, const SArray* tags) {
8,031,603✔
1682
  int32_t       code = 0;
8,031,603✔
1683
  int32_t       lino = 0;
8,031,603✔
1684
  SArray* tbUids = NULL;
8,031,603✔
1685
  SArray* cidList = NULL;
8,031,603✔
1686

1687
  int64_t uid = metaGetTableEntryUidByName(pVnode->pMeta, tbName);
8,031,603✔
1688
  QUERY_CHECK_CONDITION(uid != 0, code, lino, end, TSDB_CODE_TDB_TABLE_NOT_EXIST);
8,031,603✔
1689
  
1690
  tbUids = taosArrayInit(4, sizeof(int64_t));
8,031,603✔
1691
  QUERY_CHECK_NULL(tbUids, code, lino, end, terrno);
8,031,603✔
1692

1693
  QUERY_CHECK_CONDITION(taosArrayPush(tbUids, &uid) != NULL, code, lino, end, terrno);
8,031,603✔
1694

1695
  cidList = taosArrayInit(taosArrayGetSize(tags), sizeof(col_id_t));
8,031,603✔
1696
  QUERY_CHECK_NULL(cidList, code, lino, end, terrno);
8,031,603✔
1697

1698
  for (int32_t i = 0; i < taosArrayGetSize(tags); i++) {
16,119,768✔
1699
    SUpdatedTagVal *pTagVal = taosArrayGet(tags, i);
8,088,165✔
1700
    QUERY_CHECK_NULL(pTagVal, code, lino, end, terrno);
8,088,165✔
1701
    col_id_t cid = pTagVal->colId;
8,088,165✔
1702
    QUERY_CHECK_CONDITION(taosArrayPush(cidList, &cid) != NULL, code, lino, end, terrno);
8,088,165✔
1703
  }
1704
  
1705
  vDebug("vgId:%d, try to add table:%s in query table list, cidList size:%"PRIzu, TD_VID(pVnode), tbName, taosArrayGetSize(cidList));
8,031,603✔
1706
  code = tqUpdateTbUidList(pVnode->pTq, tbUids, cidList);
8,031,603✔
1707
  QUERY_CHECK_CODE(code, lino, end);
8,031,603✔
1708

1709
end:
8,031,603✔
1710
  if (code != 0) {
8,031,603✔
UNCOV
1711
    qError("vgId:%d, failed to alter table:%s since %s", TD_VID(pVnode), tbName, tstrerror(code));
×
1712
  }
1713
  taosArrayDestroy(tbUids);
8,031,603✔
1714
  taosArrayDestroy(cidList);
8,031,603✔
1715
}
8,031,603✔
1716

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

1727
  pRsp->msgType = TDMT_VND_ALTER_TABLE_RSP;
12,294,092✔
1728
  pRsp->pCont = NULL;
12,294,092✔
1729
  pRsp->contLen = 0;
12,294,092✔
1730
  pRsp->code = TSDB_CODE_SUCCESS;
12,294,092✔
1731

1732
  tDecoderInit(&dc, pReq, len);
12,294,092✔
1733

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

1741
  // process
1742
  if (metaAlterTable(pVnode->pMeta, ver, &vAlterTbReq, &vMetaRsp) < 0) {
12,294,092✔
1743
    vAlterTbRsp.code = terrno;
569,034✔
1744
    tDecoderClear(&dc);
569,034✔
1745
    goto _exit;
569,034✔
1746
  }
1747
  tDecoderClear(&dc);
11,725,058✔
1748

1749
  if (NULL != vMetaRsp.pSchemas) {
11,725,058✔
1750
    vnodeUpdateMetaRsp(pVnode, &vMetaRsp);
3,751,705✔
1751
    vAlterTbRsp.pMeta = &vMetaRsp;
3,751,705✔
1752
  }
1753

1754
_exit:
12,293,876✔
1755
  taosArrayDestroy(vAlterTbReq.pMultiTag);
12,294,092✔
1756
  for (int32_t i = 0; i < taosArrayGetSize(vAlterTbReq.tables); i++) {
20,195,834✔
1757
    SUpdateTableTagVal* pTable = taosArrayGet(vAlterTbReq.tables, i);
7,901,742✔
1758
    taosArrayDestroy(pTable->tags);
7,901,742✔
1759
  }
1760
  taosArrayDestroy(vAlterTbReq.tables);
12,294,092✔
1761

1762
  tEncodeSize(tEncodeSVAlterTbRsp, &vAlterTbRsp, pRsp->contLen, ret);
12,294,092✔
1763
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
12,294,092✔
1764
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
12,294,092✔
1765
  if (tEncodeSVAlterTbRsp(&ec, &vAlterTbRsp) != 0) {
12,294,092✔
1766
    vError("vgId:%d, failed to encode alter table response", TD_VID(pVnode));
×
1767
  }
1768

1769
  tEncoderClear(&ec);
12,294,092✔
1770
  if (vMetaRsp.pSchemas) {
12,294,092✔
1771
    taosMemoryFree(vMetaRsp.pSchemas);
3,751,705✔
1772
    taosMemoryFree(vMetaRsp.pSchemaExt);
3,751,705✔
1773
  }
1774
  if (vMetaRsp.pColRefs) {
12,294,092✔
1775
    taosMemoryFree(vMetaRsp.pColRefs);
274,160✔
1776
  }
1777
  return 0;
12,294,092✔
1778
}
1779

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

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

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

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

1811
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
3,110,322✔
1812
    SVDropTbReq *pDropTbReq = req.pReqs + iReq;
1,578,556✔
1813
    SVDropTbRsp  dropTbRsp = {0};
1,578,556✔
1814
    tb_uid_t     tbUid = 0;
1,578,556✔
1815

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

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

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

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

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

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

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

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

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

1881
    taosStringBuilderDestroy(&sb);
×
1882
  }
1883

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

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

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

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

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

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

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

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

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

1968
  taosMemoryFreeClear(pSchema);
1969

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

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

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

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

2022
  return TSDB_CODE_SUCCESS;
×
2023
}
2024

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

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

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

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

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

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

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

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

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

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

2094
  return code;
×
2095
}
2096

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

2103
  SSubmitReqConvertCxt cxt = {0};
×
2104

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

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

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

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

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

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

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

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

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

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

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

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

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

2272
static int32_t buildExistTableInStreamRsp(SVnode *pVnode, SSubmitTbData *pSubmitTbData, STableMetaRsp **ppRsp) {
305,275✔
2273
  if (pSubmitTbData->pCreateTbReq->flags & TD_CREATE_NORMAL_TB_IN_STREAM) {
305,275✔
2274
    int32_t code = buildExistNormalTalbeRsp(pVnode, pSubmitTbData->uid, ppRsp);
42,474✔
2275
    if (code) {
42,474✔
2276
      vError("vgId:%d, table uid:%" PRId64 " not exists, line:%d", TD_VID(pVnode), pSubmitTbData->uid, __LINE__);
×
2277
      return code;
×
2278
    }
2279
    return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
42,474✔
2280
  } else if (pSubmitTbData->pCreateTbReq->flags & TD_CREATE_SUB_TB_IN_STREAM) {
262,801✔
2281
    return buildExistSubTalbeRsp(pVnode, pSubmitTbData, ppRsp);
62,458✔
2282
  }
2283
  return TSDB_CODE_SUCCESS;
200,343✔
2284
}
2285

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

2295
  for (int32_t i = 0; i < numTbData; ++i) {
1,287,607,395✔
2296
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
656,469,111✔
2297

2298
    if (pTbData->pCreateTbReq == NULL) {
656,469,111✔
2299
      continue;
641,078,638✔
2300
    }
2301

2302
    pTbData->uid = pTbData->pCreateTbReq->uid;
15,390,473✔
2303

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

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

2319
    SVCreateTbRsp *pCreateTbRsp = taosArrayReserve(pResponse->aCreateTbRsp, 1);
15,390,473✔
2320
    code = metaCreateTable2(pVnode->pMeta, version, pTbData->pCreateTbReq, &pCreateTbRsp->pMeta);
15,390,473✔
2321
    if (code == TSDB_CODE_SUCCESS) {
15,390,473✔
2322
      // Allocate necessary resources
2323
      if (newTbUids == NULL) {
15,034,886✔
2324
        newTbUids = taosArrayInit(numTbData, sizeof(int64_t));
12,248,957✔
2325
        if (newTbUids == NULL) {
12,248,630✔
2326
          code = terrno;
×
2327
          vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2328
                 tstrerror(code), version);
2329
          return code;
×
2330
        }
2331
      }
2332

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

2341
      if (pCreateTbRsp->pMeta) {
15,034,886✔
2342
        vnodeUpdateMetaRsp(pVnode, pCreateTbRsp->pMeta);
15,034,886✔
2343
      }
2344
    } else if (code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
355,587✔
2345
      code = terrno = 0;
354,535✔
2346
      pTbData->uid = pTbData->pCreateTbReq->uid;  // update uid if table exist for using below
354,498✔
2347

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

2369
  // Update the affected table uid list
2370
  if (taosArrayGetSize(newTbUids) > 0) {
631,138,284✔
2371
    vDebug("vgId:%d, add %d table into query table list in handling submit", TD_VID(pVnode),
12,248,957✔
2372
           (int32_t)taosArrayGetSize(newTbUids));
2373
    if (tqAddTbUidList(pVnode->pTq, newTbUids) != 0) {
12,248,957✔
2374
      vError("vgId:%d, failed to update tbUid list", TD_VID(pVnode));
×
2375
    }
2376
  }
2377

2378
  vDebug("vgId:%d, handle auto create table done, version:%" PRId64, TD_VID(pVnode), version);
631,139,338✔
2379

2380
  taosArrayDestroy(newTbUids);
631,139,011✔
2381
  return code;
631,139,338✔
2382
}
2383

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

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

2422
static int32_t vnodeHandleDataWrite(SVnode *pVnode, int64_t version, SSubmitReq2 *pRequest, SSubmitRsp2 *pResponse) {
631,137,157✔
2423
  int32_t code = TSDB_CODE_SUCCESS;
631,137,157✔
2424
  int32_t numTbData = taosArrayGetSize(pRequest->aSubmitTbData);
631,137,157✔
2425
  int8_t  hasBlob = 0;
631,138,544✔
2426

2427
  // Scan submit data
2428
  for (int32_t i = 0; i < numTbData; ++i) {
1,287,559,787✔
2429
    SMetaInfo      info = {0};
656,424,848✔
2430
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
656,424,991✔
2431

2432
    if (pTbData->flags & SUBMIT_REQ_WITH_BLOB) {
656,424,515✔
2433
      hasBlob = 1;
31,645✔
2434
    }
2435
    if (pTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
656,424,515✔
2436
      continue;  // skip column data format
6,672,885✔
2437
    }
2438
    if (pTbData->flags & SUBMIT_REQ_ONLY_CREATE_TABLE) {
649,751,110✔
2439
      continue;  // skip only crate table request
2,090,091✔
2440
    }
2441

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

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

2459
    if (info.suid) {
647,659,739✔
2460
      code = metaGetInfo(pVnode->pMeta, info.suid, &info, NULL);
502,505,764✔
2461
      if (code) {
502,506,955✔
2462
        code = TSDB_CODE_INTERNAL_ERROR;
×
2463
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " suid:%" PRId64, TD_VID(pVnode), __func__,
×
2464
               __FILE__, __LINE__, tstrerror(code), version, info.suid);
2465
        return code;
×
2466
      }
2467
    }
2468

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

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

2484
  for (int32_t i = 0; i < numTbData; ++i) {
1,287,548,486✔
2485
    int32_t        affectedRows = 0;
656,422,294✔
2486
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
656,422,639✔
2487

2488
    if (pTbData->flags & SUBMIT_REQ_ONLY_CREATE_TABLE) {
656,422,834✔
2489
      continue;
2,090,091✔
2490
    }
2491

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

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

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

2517
  vDebug("vgId:%d, handle data write done, version:%" PRId64 ", affectedRows:%d", TD_VID(pVnode), version,
631,126,192✔
2518
         pResponse->affectedRows);
2519
  return code;
631,137,622✔
2520
}
2521

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

2525
  int32_t   numCols = taosArrayGetSize(pTbData->aCol);
6,671,903✔
2526
  SColData *aColData = (SColData *)TARRAY_DATA(pTbData->aCol);
6,672,543✔
2527

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

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

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

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

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

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

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

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

2586
  return code;
6,672,885✔
2587
}
2588

2589
static int32_t vnodeScanSubmitRowData(SVnode *pVnode, SSubmitTbData *pTbData, TSKEY minKey, TSKEY maxKey) {
649,785,792✔
2590
  int32_t code = 0;
649,785,792✔
2591

2592
  int32_t numRows = taosArrayGetSize(pTbData->aRowP);
649,785,792✔
2593
  SRow  **aRow = (SRow **)TARRAY_DATA(pTbData->aRowP);
649,780,349✔
2594

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

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

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

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

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

2639
  return code;
630,841,028✔
2640
}
2641

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

2646
  TSKEY now = taosGetTimestamp(pVnode->config.tsdbCfg.precision);
631,173,147✔
2647
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * pVnode->config.tsdbCfg.keep2;
631,173,230✔
2648
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
631,175,163✔
2649
  for (int32_t i = 0; i < numTbData; i++) {
1,287,626,621✔
2650
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
656,457,873✔
2651

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

2659
    if (pTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
656,457,559✔
2660
      code = vnodeScanColumnData(pVnode, pTbData, minKey, maxKey);
6,672,230✔
2661
      if (code) {
6,672,885✔
2662
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2663
               __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2664
        return code;
×
2665
      }
2666
    } else {
2667
      code = vnodeScanSubmitRowData(pVnode, pTbData, minKey, maxKey);
649,785,623✔
2668
      if (code) {
649,783,625✔
2669
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
5,373✔
2670
               __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2671
        return code;
×
2672
      }
2673
    }
2674
  }
2675

2676
  return code;
631,168,748✔
2677
}
2678

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

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

2704
  return 0;
583✔
2705
}
2706

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2815
  return code;
53✔
2816
}
2817

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

3113
  return code;
583✔
3114
}
3115

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

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

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

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

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

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

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

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

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

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

3194
  return code;
212✔
3195
}
3196

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

3203
  SSubmitReq2 *pSubmitReq = &(SSubmitReq2){0};
631,181,998✔
3204
  SSubmitRsp2 *pSubmitRsp = &(SSubmitRsp2){0};
631,181,998✔
3205
  int32_t      ret;
3206
  SEncoder     ec = {0};
631,179,430✔
3207

3208
  pRsp->code = TSDB_CODE_SUCCESS;
631,180,157✔
3209

3210
  void           *pAllocMsg = NULL;
631,181,043✔
3211
  SSubmitReq2Msg *pMsg = (SSubmitReq2Msg *)pReq;
631,181,043✔
3212
  SDecoder        dc = {0};
631,181,043✔
3213
  if (0 == taosHton64(pMsg->version)) {
631,181,770✔
3214
    code = vnodeSubmitReqConvertToSubmitReq2(pVnode, (SSubmitReq *)pMsg, pSubmitReq);
×
3215
    if (TSDB_CODE_SUCCESS == code) {
×
3216
      code = vnodeRebuildSubmitReqMsg(pSubmitReq, &pReq);
×
3217
    }
3218
    if (TSDB_CODE_SUCCESS == code) {
×
3219
      pAllocMsg = pReq;
×
3220
    }
3221
    TSDB_CHECK_CODE(code, lino, _exit);
×
3222
  } else {
3223
    // decode
3224
    pReq = POINTER_SHIFT(pReq, sizeof(SSubmitReq2Msg));
631,181,174✔
3225
    len -= sizeof(SSubmitReq2Msg);
631,181,626✔
3226

3227
    tDecoderInit(&dc, pReq, len);
631,181,626✔
3228
    if (tDecodeSubmitReq(&dc, pSubmitReq, NULL) < 0) {
631,181,839✔
3229
      code = TSDB_CODE_INVALID_MSG;
×
3230
      TSDB_CHECK_CODE(code, lino, _exit);
×
3231
    }
3232
  }
3233

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

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

3245
  // Handle auto create table
3246
  code = vnodeHandleAutoCreateTable(pVnode, ver, pSubmitReq, pSubmitRsp);
631,182,764✔
3247
  if (code) {
631,182,665✔
3248
    vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
43,970✔
3249
           tstrerror(code), ver);
3250
    TSDB_CHECK_CODE(code, lino, _exit);
43,970✔
3251
  }
3252

3253
  // Handle data write
3254
  code = vnodeHandleDataWrite(pVnode, ver, pSubmitReq, pSubmitRsp);
631,138,695✔
3255
  if (code) {
631,138,338✔
3256
    vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
1,716✔
3257
           tstrerror(code), ver);
3258
    TSDB_CHECK_CODE(code, lino, _exit);
1,716✔
3259
  }
3260

3261
_exit:
631,182,164✔
3262
  // message
3263
  pRsp->code = code;
631,182,725✔
3264
  tEncodeSize(tEncodeSSubmitRsp2, pSubmitRsp, pRsp->contLen, ret);
631,182,725✔
3265
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
631,180,597✔
3266
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
631,181,471✔
3267
  if (tEncodeSSubmitRsp2(&ec, pSubmitRsp) < 0) {
631,182,343✔
3268
    vError("vgId:%d, failed to encode submit response", TD_VID(pVnode));
×
3269
  }
3270
  tEncoderClear(&ec);
631,181,166✔
3271

3272
  // update statistics
3273
  (void)atomic_add_fetch_64(&pVnode->statis.nInsert, pSubmitRsp->affectedRows);
631,177,849✔
3274
  (void)atomic_add_fetch_64(&pVnode->statis.nInsertSuccess, pSubmitRsp->affectedRows);
631,180,234✔
3275
  (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsert, 1);
631,180,622✔
3276

3277
  // update metrics
3278
  METRICS_UPDATE(pVnode->writeMetrics.total_requests, METRIC_LEVEL_LOW, 1);
631,178,199✔
3279
  METRICS_UPDATE(pVnode->writeMetrics.total_rows, METRIC_LEVEL_HIGH, pSubmitRsp->affectedRows);
631,177,603✔
3280
  METRICS_UPDATE(pVnode->writeMetrics.total_bytes, METRIC_LEVEL_LOW, pMsg->header.contLen);
631,177,492✔
3281

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

3295
  if (code == 0) {
631,176,942✔
3296
    (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
631,130,631✔
3297
  }
3298

3299
  // clear
3300
  tDestroySubmitReq(pSubmitReq, 0 == taosHton64(pMsg->version) ? TSDB_MSG_FLG_CMPT : TSDB_MSG_FLG_DECODE);
631,180,922✔
3301
  tDestroySSubmitRsp2(pSubmitRsp, TSDB_MSG_FLG_ENCODE);
631,173,706✔
3302

3303
  if (code) {
631,170,427✔
3304
    terrno = code;
45,686✔
3305
    if (code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
45,686✔
3306
      vInfo("vgId:%d, failed to process submit request since %s, lino:%d, version:%" PRId64, TD_VID(pVnode),
42,918✔
3307
            tstrerror(code), lino, ver);
3308

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

3315
  taosMemoryFree(pAllocMsg);
631,171,442✔
3316
  tDecoderClear(&dc);
631,169,302✔
3317

3318
  return code;
631,181,066✔
3319
}
3320

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

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

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

3330
  return code;
24,612✔
3331
}
3332

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

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

3347
_exit:
4,199,548✔
3348
  pRsp->msgType = TDMT_VND_ALTER_CONFIRM_RSP;
4,199,548✔
3349
  pRsp->code = code;
4,199,548✔
3350
  pRsp->pCont = NULL;
4,199,548✔
3351
  pRsp->contLen = 0;
4,199,548✔
3352

3353
  return code;
4,199,548✔
3354
}
3355

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

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

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

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

3378
  if (pVnode->config.cacheLastSize != req.cacheLastSize) {
323,188✔
3379
    pVnode->config.cacheLastSize = req.cacheLastSize;
3,588✔
3380
    tsdbCacheSetCapacity(pVnode, (size_t)pVnode->config.cacheLastSize * 1024 * 1024);
3,588✔
3381
  }
3382

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

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

3400
  if (pVnode->config.cacheLast != req.cacheLast) {
323,188✔
3401
    pVnode->config.cacheLast = req.cacheLast;
86,528✔
3402
  }
3403

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

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

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

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

3427
  if (pVnode->config.tsdbCfg.keep0 != req.daysToKeep0) {
323,188✔
3428
    pVnode->config.tsdbCfg.keep0 = req.daysToKeep0;
44,194✔
3429
    tsdbChanged = true;
44,194✔
3430
  }
3431

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

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

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

3447
  if (req.sttTrigger != -1 && req.sttTrigger != pVnode->config.sttTrigger) {
323,188✔
3448
    if (req.sttTrigger > 1 && pVnode->config.sttTrigger > 1) {
2,544✔
3449
      pVnode->config.sttTrigger = req.sttTrigger;
1,458✔
3450
    } else {
3451
      vnodeAWait(&pVnode->commitTask);
1,086✔
3452

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

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

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

3467
  if (req.ssKeepLocal != -1 && req.ssKeepLocal != pVnode->config.ssKeepLocal) {
323,188✔
3468
    pVnode->config.ssKeepLocal = req.ssKeepLocal;
1,458✔
3469
  }
3470
  if (req.ssCompact != -1 && req.ssCompact != pVnode->config.ssCompact) {
323,188✔
3471
    pVnode->config.ssCompact = req.ssCompact;
1,458✔
3472
  }
3473
  if (req.allowDrop != pVnode->config.allowDrop) {
323,188✔
3474
    pVnode->config.allowDrop = req.allowDrop;
×
3475
  }
3476
  if (req.secureDelete != pVnode->config.secureDelete) {
323,188✔
3477
    pVnode->config.secureDelete = req.secureDelete;
1,130✔
3478
  }
3479

3480
  if (walChanged) {
323,188✔
3481
    if (walAlter(pVnode->pWal, &pVnode->config.walCfg) != 0) {
138,084✔
3482
      vError("vgId:%d, failed to alter wal config since %s", TD_VID(pVnode), tstrerror(ERRNO));
×
3483
    }
3484
  }
3485

3486
  if (tsdbChanged) {
323,188✔
3487
    tsdbSetKeepCfg(pVnode->pTsdb, &pVnode->config.tsdbCfg);
59,106✔
3488
  }
3489

3490
  return 0;
323,188✔
3491
}
3492

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

3502
  SMetaReader mr = {0};
×
3503
  metaReaderDoInit(&mr, pVnode->pMeta, META_READER_NOLOCK);
×
3504
  STsdb *pTsdb = pVnode->pTsdb;
×
3505

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

3515
    int64_t uid = mr.me.uid;
×
3516

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

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

3540
static int32_t vnodeProcessDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
2,133,662✔
3541
                                     SRpcMsg *pOriginalMsg) {
3542
  int32_t     code = 0;
2,133,662✔
3543
  SDecoder   *pCoder = &(SDecoder){0};
2,133,662✔
3544
  SDeleteRes *pRes = &(SDeleteRes){0};
2,134,224✔
3545

3546
  pRsp->msgType = TDMT_VND_DELETE_RSP;
2,134,224✔
3547
  pRsp->pCont = NULL;
2,134,224✔
3548
  pRsp->contLen = 0;
2,134,224✔
3549
  pRsp->code = TSDB_CODE_SUCCESS;
2,134,224✔
3550

3551
  pRes->uidList = taosArrayInit(0, sizeof(tb_uid_t));
2,134,224✔
3552
  if (pRes->uidList == NULL) {
2,134,224✔
3553
    code = terrno;
×
3554
    goto _err;
×
3555
  }
3556

3557
  tDecoderInit(pCoder, pReq, len);
2,134,224✔
3558
  code = tDecodeDeleteRes(pCoder, pRes);
2,134,224✔
3559
  if (code) goto _err;
2,134,224✔
3560

3561
  if (pRes->affectedRows > 0) {
2,134,224✔
3562
    for (int32_t iUid = 0; iUid < taosArrayGetSize(pRes->uidList); iUid++) {
3,799,855✔
3563
      uint64_t uid = *(uint64_t *)taosArrayGet(pRes->uidList, iUid);
2,168,694✔
3564
      // Merge runtime vnode(db-level) secureDelete to avoid stale client meta after ALTER DATABASE.
3565
      int8_t secureDelete = pRes->secureDelete | pVnode->config.secureDelete;
2,168,694✔
3566
      code = tsdbDeleteTableData(pVnode->pTsdb, ver, pRes->suid, uid, pRes->skey, pRes->ekey, secureDelete);
2,168,694✔
3567
      if (code) goto _err;
2,168,694✔
3568
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, pRes->ctimeMs);
2,168,694✔
3569
      if (code) goto _err;
2,168,694✔
3570
    }
3571
  }
3572

3573
  tDecoderClear(pCoder);
2,134,224✔
3574
  taosArrayDestroy(pRes->uidList);
2,134,224✔
3575

3576
  SVDeleteRsp rsp = {.affectedRows = pRes->affectedRows};
2,134,224✔
3577
  int32_t     ret = 0;
2,134,224✔
3578
  tEncodeSize(tEncodeSVDeleteRsp, &rsp, pRsp->contLen, ret);
2,134,224✔
3579
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
2,134,224✔
3580
  SEncoder ec = {0};
2,134,224✔
3581
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
2,134,224✔
3582
  code = tEncodeSVDeleteRsp(&ec, &rsp);
2,134,224✔
3583
  if (code) goto _err;
2,134,224✔
3584
  tEncoderClear(&ec);
2,134,224✔
3585
  return code;
2,134,224✔
3586

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

3603
  return code;
×
3604
}
3605
static int32_t vnodeProcessCreateIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
17,787✔
3606
  SVCreateStbReq req = {0};
17,787✔
3607
  SDecoder       dc = {0};
17,787✔
3608
  int32_t        code = 0;
17,787✔
3609

3610
  pRsp->msgType = TDMT_VND_CREATE_INDEX_RSP;
17,787✔
3611
  pRsp->code = TSDB_CODE_SUCCESS;
17,787✔
3612
  pRsp->pCont = NULL;
17,787✔
3613
  pRsp->contLen = 0;
17,787✔
3614

3615
  tDecoderInit(&dc, pReq, len);
17,787✔
3616
  // decode req
3617
  if (tDecodeSVCreateStbReq(&dc, &req) < 0) {
17,787✔
3618
    tDecoderClear(&dc);
×
3619
    return terrno = TSDB_CODE_INVALID_MSG;
×
3620
  }
3621

3622
  code = metaAddIndexToSuperTable(pVnode->pMeta, ver, &req);
17,787✔
3623
  if (code) {
17,787✔
3624
    pRsp->code = code;
×
3625
    goto _err;
×
3626
  }
3627
  tDecoderClear(&dc);
17,787✔
3628
  return 0;
17,787✔
3629

3630
_err:
×
3631
  tDecoderClear(&dc);
×
3632
  return code;
×
3633
}
3634
static int32_t vnodeProcessDropIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
14,466✔
3635
  SDropIndexReq req = {0};
14,466✔
3636
  int32_t       code = 0;
14,466✔
3637
  pRsp->msgType = TDMT_VND_DROP_INDEX_RSP;
14,466✔
3638
  pRsp->code = TSDB_CODE_SUCCESS;
14,466✔
3639
  pRsp->pCont = NULL;
14,466✔
3640
  pRsp->contLen = 0;
14,466✔
3641

3642
  if ((code = tDeserializeSDropIdxReq(pReq, len, &req))) {
14,466✔
3643
    pRsp->code = code;
×
3644
    return code;
×
3645
  }
3646

3647
  code = metaDropIndexFromSuperTable(pVnode->pMeta, ver, &req);
14,466✔
3648
  if (code) {
14,466✔
3649
    pRsp->code = code;
×
3650
    return code;
×
3651
  }
3652
  return TSDB_CODE_SUCCESS;
14,466✔
3653
}
3654

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

3657
static int32_t vnodeProcessCompactVnodeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
68,608✔
3658
  if (!pVnode->restored) {
68,608✔
3659
    vInfo("vgId:%d, ignore compact req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
2,924✔
3660
    return 0;
2,924✔
3661
  }
3662
  return vnodeAsyncCompact(pVnode, ver, pReq, len, pRsp);
65,684✔
3663
}
3664

3665
static int32_t vnodeProcessConfigChangeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
3666
  if (syncCheckMember(pVnode->sync) != 0) {
×
3667
    vError("vgId:%d, failed to check member", TD_VID(pVnode));
×
3668
  }
3669

3670
  pRsp->msgType = TDMT_SYNC_CONFIG_CHANGE_RSP;
×
3671
  pRsp->code = TSDB_CODE_SUCCESS;
×
3672
  pRsp->pCont = NULL;
×
3673
  pRsp->contLen = 0;
×
3674

3675
  return 0;
×
3676
}
3677

3678
static int32_t vnodeCheckState(SVnode *pVnode) {
49,588✔
3679
  SSyncState syncState = syncGetState(pVnode->sync);
49,588✔
3680
  if (syncState.state != TAOS_SYNC_STATE_LEADER) {
49,588✔
3681
    return terrno = TSDB_CODE_SYN_NOT_LEADER;
12,763✔
3682
  }
3683
  return 0;
36,825✔
3684
}
3685

3686
static int32_t vnodeCheckToken(SVnode *pVnode, char *member0Token, char *member1Token) {
36,825✔
3687
  char token[TSDB_ARB_TOKEN_SIZE] = {0};
36,825✔
3688
  if (vnodeGetArbToken(pVnode, token) != 0) {
36,825✔
3689
    return terrno = TSDB_CODE_NOT_FOUND;
×
3690
  }
3691

3692
  if (strncmp(token, member0Token, TSDB_ARB_TOKEN_SIZE) != 0 &&
36,825✔
3693
      strncmp(token, member1Token, TSDB_ARB_TOKEN_SIZE) != 0) {
35,495✔
3694
    return terrno = TSDB_CODE_MND_ARB_TOKEN_MISMATCH;
34,937✔
3695
  }
3696

3697
  terrno = TSDB_CODE_SUCCESS;
1,888✔
3698
  return 0;
1,888✔
3699
}
3700

3701
static int32_t vnodeCheckSyncd(SVnode *pVnode, char *member0Token, char *member1Token) {
944✔
3702
  int32_t code = vnodeCheckToken(pVnode, member0Token, member1Token);
944✔
3703
  if (code != 0) {
944✔
UNCOV
3704
    return code;
×
3705
  }
3706

3707
  return syncCheckSynced(pVnode->sync);
944✔
3708
}
3709

3710
static int32_t vnodeProcessArbCheckSyncReq(SVnode *pVnode, void *pReq, int32_t len, SRpcMsg *pRsp) {
1,890✔
3711
  int32_t code = 0;
1,890✔
3712

3713
  if ((code = vnodeCheckState(pVnode)) != 0) {
1,890✔
3714
    vDebug("vgId:%d, failed to preprocess vnode-arb-check-sync request since %s", TD_VID(pVnode), tstrerror(code));
946✔
3715
    return 0;
946✔
3716
  }
3717

3718
  SVArbCheckSyncReq syncReq = {0};
944✔
3719

3720
  code = tDeserializeSVArbCheckSyncReq(pReq, len, &syncReq);
944✔
3721
  if (code) {
944✔
3722
    return code;
×
3723
  }
3724

3725
  vInfo("vgId:%d, start to process vnode-arb-check-sync req QID:0x%" PRIx64 ":0x%" PRIx64 ", seqNum:%" PRIx64
944✔
3726
        ", arbToken:%s, member0Token:%s, member1Token:%s, "
3727
        "arbTerm:%" PRId64,
3728
        TD_VID(pVnode), pRsp->info.traceId.rootId, pRsp->info.traceId.msgId, pRsp->info.seqNum, syncReq.arbToken,
3729
        syncReq.member0Token, syncReq.member1Token, syncReq.arbTerm);
3730

3731
  pRsp->msgType = TDMT_VND_ARB_CHECK_SYNC_RSP;
944✔
3732
  pRsp->code = TSDB_CODE_SUCCESS;
944✔
3733
  pRsp->pCont = NULL;
944✔
3734
  pRsp->contLen = 0;
944✔
3735

3736
  SVArbCheckSyncRsp syncRsp = {0};
944✔
3737
  syncRsp.arbToken = syncReq.arbToken;
944✔
3738
  syncRsp.member0Token = syncReq.member0Token;
944✔
3739
  syncRsp.member1Token = syncReq.member1Token;
944✔
3740
  syncRsp.vgId = TD_VID(pVnode);
944✔
3741

3742
  if ((syncRsp.errCode = vnodeCheckSyncd(pVnode, syncReq.member0Token, syncReq.member1Token)) != 0) {
944✔
UNCOV
3743
    vError("vgId:%d, failed to check assigned log syncd since %s", TD_VID(pVnode), tstrerror(syncRsp.errCode));
×
3744
  }
3745

3746
  if ((code = vnodeUpdateArbTerm(pVnode, syncReq.arbTerm)) != 0) {
944✔
3747
    vError("vgId:%d, failed to update arb term since %s", TD_VID(pVnode), tstrerror(code));
×
3748
    goto _OVER;
×
3749
  }
3750

3751
  int32_t contLen = tSerializeSVArbCheckSyncRsp(NULL, 0, &syncRsp);
944✔
3752
  if (contLen <= 0) {
944✔
3753
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
3754
    code = -1;
×
3755
    goto _OVER;
×
3756
  }
3757
  void *pHead = rpcMallocCont(contLen);
944✔
3758
  if (!pHead) {
944✔
3759
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
3760
    code = -1;
×
3761
    goto _OVER;
×
3762
  }
3763

3764
  if (tSerializeSVArbCheckSyncRsp(pHead, contLen, &syncRsp) <= 0) {
944✔
3765
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
3766
    rpcFreeCont(pHead);
×
3767
    code = -1;
×
3768
    goto _OVER;
×
3769
  }
3770

3771
  pRsp->pCont = pHead;
944✔
3772
  pRsp->contLen = contLen;
944✔
3773

3774
  vInfo(
944✔
3775
      "vgId:%d, suceed to process vnode-arb-check-sync req rsp.code:%s, arbToken:%s, member0Token:%s, "
3776
      "member1Token:%s",
3777
      TD_VID(pVnode), tstrerror(syncRsp.errCode), syncRsp.arbToken, syncRsp.member0Token, syncRsp.member1Token);
3778

3779
  code = TSDB_CODE_SUCCESS;
944✔
3780

3781
_OVER:
944✔
3782
  if (code != 0) {
944✔
3783
    vError("vgId:%d, failed to process arb check req rsp.code:%s since %s", TD_VID(pVnode), tstrerror(syncRsp.errCode),
×
3784
           tstrerror(code));
3785
  }
3786
  tFreeSVArbCheckSyncReq(&syncReq);
944✔
3787
  return code;
944✔
3788
}
3789

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