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

taosdata / TDengine / #4996

19 Mar 2026 02:16AM UTC coverage: 72.069% (+0.07%) from 71.996%
#4996

push

travis-ci

web-flow
feat: SQL firewall black/white list (#34798)

461 of 618 new or added lines in 4 files covered. (74.6%)

380 existing lines in 128 files now uncovered.

245359 of 340448 relevant lines covered (72.07%)

135732617.17 hits per line

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

69.8
/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) {
63,312,400✔
86
  int32_t code = 0;
63,312,400✔
87
  int32_t lino = 0;
63,312,400✔
88

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

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

100
  // name
101
  char *name = NULL;
63,318,154✔
102
  if (tDecodeCStr(pCoder, &name) < 0) {
63,316,994✔
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);
63,316,994✔
109
  if (uid == 0) {
63,311,959✔
110
    uid = tGenIdPI64();
62,943,836✔
111
  }
112
  taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), uid);
63,305,934✔
113

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

117
  tEndDecode(pCoder);
63,302,600✔
118

119
_exit:
63,312,685✔
120
  if (code) {
63,309,997✔
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);
63,309,997✔
124
    if (pUid) *pUid = uid;
63,304,254✔
125
  }
126
  return code;
63,304,585✔
127
}
128
static int32_t vnodePreProcessCreateTableMsg(SVnode *pVnode, SRpcMsg *pMsg) {
42,438,381✔
129
  int32_t code = 0;
42,438,381✔
130
  int32_t lino = 0;
42,438,381✔
131

132
  int64_t  btime = taosGetTimestampMs();
42,438,703✔
133
  SDecoder dc = {0};
42,438,703✔
134
  int32_t  nReqs;
42,436,287✔
135

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

142
  if (tDecodeI32v(&dc, &nReqs) < 0) {
42,439,433✔
143
    code = TSDB_CODE_INVALID_MSG;
×
144
    TSDB_CHECK_CODE(code, lino, _exit);
×
145
  }
146
  for (int32_t iReq = 0; iReq < nReqs; iReq++) {
90,271,630✔
147
    code = vnodePreprocessCreateTableReq(pVnode, &dc, btime, NULL);
47,845,782✔
148
    TSDB_CHECK_CODE(code, lino, _exit);
47,832,197✔
149
  }
150

151
  tEndDecode(&dc);
42,425,848✔
152

153
_exit:
42,438,614✔
154
  tDecoderClear(&dc);
42,438,264✔
155
  if (code) {
42,437,995✔
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,437,497✔
160
}
161

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

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

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

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

177
  taosArrayDestroy(vAlterTbReq.pMultiTag);
12,369,576✔
178
  vAlterTbReq.pMultiTag = NULL;
12,369,576✔
179

180
  for (int32_t i = 0; i < taosArrayGetSize(vAlterTbReq.tables); i++) {
20,284,644✔
181
    SUpdateTableTagVal* pTable = taosArrayGet(vAlterTbReq.tables, i);
7,915,068✔
182
    taosArrayDestroy(pTable->tags);
7,915,068✔
183
  }
184
  taosArrayDestroy(vAlterTbReq.tables);
12,369,576✔
185
  vAlterTbReq.tables = NULL;
12,369,576✔
186

187

188
_exit:
12,369,576✔
189
  tDecoderClear(&dc);
12,369,576✔
190
  if (code) {
12,369,576✔
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,369,576✔
194
           ctimeMs);
195
  }
196
  return code;
12,369,576✔
197
}
198

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

203
  SMsgHead *pContOld = pMsg->pCont;
11,144,819✔
204
  int32_t   reqLenOld = pMsg->contLen - sizeof(SMsgHead);
11,146,121✔
205

206
  SArray *tbUids = NULL;
11,147,946✔
207
  int64_t timestampMs = 0;
11,147,946✔
208

209
  SVDropTtlTableReq ttlReq = {0};
11,147,946✔
210
  if (tDeserializeSVDropTtlTableReq((char *)pContOld + sizeof(SMsgHead), reqLenOld, &ttlReq) != 0) {
11,149,906✔
211
    code = TSDB_CODE_INVALID_MSG;
×
212
    TSDB_CHECK_CODE(code, lino, _exit);
×
213
  }
214

215
  {  // find expired uids
216
    tbUids = taosArrayInit(8, sizeof(tb_uid_t));
11,147,893✔
217
    if (tbUids == NULL) {
11,145,770✔
218
      code = terrno;
×
219
      TSDB_CHECK_CODE(code, lino, _exit);
×
220
    }
221

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

229
    ttlReq.nUids = taosArrayGetSize(tbUids);
11,138,915✔
230
    ttlReq.pTbUids = tbUids;
11,142,551✔
231
  }
232

233
  if (ttlReq.nUids == 0) {
11,142,551✔
234
    code = TSDB_CODE_MSG_PREPROCESSED;
11,122,174✔
235
    TSDB_CHECK_CODE(code, lino, _exit);
11,122,174✔
236
  }
237

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

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

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

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

259
  code = 0;
11,874✔
260

261
_exit:
11,142,012✔
262
  taosArrayDestroy(tbUids);
11,143,370✔
263

264
  if (code && code != TSDB_CODE_MSG_PREPROCESSED) {
11,119,441✔
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,119,441✔
269
  }
270

271
  return code;
11,119,441✔
272
}
273

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

279
  if (tStartDecode(pCoder) < 0) {
542,729,705✔
280
    code = TSDB_CODE_INVALID_MSG;
×
281
    TSDB_CHECK_CODE(code, lino, _exit);
×
282
  }
283

284
  SSubmitTbData submitTbData;
542,723,955✔
285
  uint8_t       version;
286
  if (tDecodeI32v(pCoder, &submitTbData.flags) < 0) {
542,722,361✔
287
    code = TSDB_CODE_INVALID_MSG;
×
288
    TSDB_CHECK_CODE(code, lino, _exit);
×
289
  }
290
  version = (submitTbData.flags >> 8) & 0xff;
542,722,361✔
291
  submitTbData.flags = submitTbData.flags & 0xff;
542,722,361✔
292

293
  int64_t uid;
542,717,990✔
294
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
542,723,272✔
295
    code = vnodePreprocessCreateTableReq(pVnode, pCoder, btimeMs, &uid);
15,466,111✔
296
    TSDB_CHECK_CODE(code, lino, _exit);
15,463,792✔
297
  }
298

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

305
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
542,727,102✔
306
    taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), uid);
15,464,783✔
307
    pCoder->pos += sizeof(int64_t);
15,464,707✔
308
  } else {
309
    if (tDecodeI64(pCoder, &submitTbData.uid) < 0) {
527,255,260✔
310
      code = TSDB_CODE_INVALID_MSG;
×
311
      TSDB_CHECK_CODE(code, lino, _exit);
×
312
    }
313
  }
314

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

320
  // scan and check
321
  TSKEY now = btimeMs;
542,719,646✔
322
  if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_MICRO) {
542,719,646✔
323
    now *= 1000;
2,551,759✔
324
  } else if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_NANO) {
540,168,023✔
325
    now *= 1000000;
1,490,065✔
326
  }
327

328
  int32_t keep = pVnode->config.tsdbCfg.keep2;
542,722,047✔
329

330
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * keep;
542,721,157✔
331
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
542,720,840✔
332
  if (submitTbData.flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
542,717,605✔
333
    uint64_t nColData;
6,739,323✔
334
    if (tDecodeU64v(pCoder, &nColData) < 0) {
6,738,750✔
335
      code = TSDB_CODE_INVALID_MSG;
×
336
      TSDB_CHECK_CODE(code, lino, _exit);
×
337
    }
338

339
    SColData colData = {0};
6,738,750✔
340
    code = tDecodeColData(version, pCoder, &colData, false);
6,739,412✔
341
    if (code) {
6,739,081✔
342
      code = TSDB_CODE_INVALID_MSG;
×
343
      TSDB_CHECK_CODE(code, lino, _exit);
×
344
    }
345

346
    if (colData.flag != HAS_VALUE) {
6,739,081✔
347
      code = TSDB_CODE_INVALID_MSG;
×
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;
594✔
354
        TSDB_CHECK_CODE(code, lino, _exit);
594✔
355
      }
356
    }
357

358
    for (uint64_t i = 1; i < nColData; i++) {
20,502,887✔
359
      code = tDecodeColData(version, pCoder, &colData, true);
13,946,791✔
360
      if (code) {
13,946,791✔
361
        code = TSDB_CODE_INVALID_MSG;
×
362
        TSDB_CHECK_CODE(code, lino, _exit);
×
363
      }
364
    }
365
  } else {
366
    uint64_t nRow;
535,973,911✔
367
    if (tDecodeU64v(pCoder, &nRow) < 0) {
535,987,300✔
368
      code = TSDB_CODE_INVALID_MSG;
×
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;
68,164✔
382
        TSDB_CHECK_CODE(code, lino, _exit);
68,164✔
383
      }
384

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

393
  if (!tDecodeIsEnd(pCoder)) {
542,648,494✔
394
    taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), ctimeMs);
542,650,807✔
395
    pCoder->pos += sizeof(int64_t);
542,647,805✔
396
  }
397

398
  tEndDecode(pCoder);
542,649,716✔
399

400
_exit:
542,724,211✔
401
  if (code) {
542,724,183✔
402
    vError("vgId:%d, %s:%d failed to vnodePreProcessSubmitTbData submit request since %s", TD_VID(pVnode), __func__,
68,758✔
403
           lino, tstrerror(code));
404
  }
405
  return code;
542,724,183✔
406
}
407
static int32_t vnodePreProcessSubmitMsg(SVnode *pVnode, SRpcMsg *pMsg) {
517,328,953✔
408
  int32_t code = 0;
517,328,953✔
409
  int32_t lino = 0;
517,328,953✔
410

411
  if (tsBypassFlag & TSDB_BYPASS_RA_RPC_RECV_SUBMIT) {
517,328,953✔
412
    return TSDB_CODE_MSG_PREPROCESSED;
68✔
413
  }
414

415
  SDecoder *pCoder = &(SDecoder){0};
517,328,885✔
416

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

422
  tDecoderInit(pCoder, (uint8_t *)pMsg->pCont + sizeof(SSubmitReq2Msg), pMsg->contLen - sizeof(SSubmitReq2Msg));
517,331,207✔
423

424
  if (tStartDecode(pCoder) < 0) {
517,330,797✔
425
    code = TSDB_CODE_INVALID_MSG;
×
426
    TSDB_CHECK_CODE(code, lino, _exit);
×
427
  }
428

429
  uint64_t nSubmitTbData;
517,328,172✔
430
  if (tDecodeU64v(pCoder, &nSubmitTbData) < 0) {
517,332,930✔
431
    code = TSDB_CODE_INVALID_MSG;
×
432
    TSDB_CHECK_CODE(code, lino, _exit);
×
433
  }
434

435
  int64_t btimeMs = taosGetTimestampMs();
517,333,444✔
436
  int64_t ctimeMs = btimeMs;
517,333,444✔
437
  for (uint64_t i = 0; i < nSubmitTbData; i++) {
1,059,984,625✔
438
    code = vnodePreProcessSubmitTbData(pVnode, pCoder, btimeMs, ctimeMs);
542,724,489✔
439
    TSDB_CHECK_CODE(code, lino, _exit);
542,719,939✔
440
  }
441

442
  tEndDecode(pCoder);
517,260,136✔
443

444
_exit:
517,331,536✔
445
  tDecoderClear(pCoder);
517,331,536✔
446
  if (code) {
517,331,409✔
447
    vError("vgId:%d, %s:%d failed to preprocess submit request since %s, msg type:%s", TD_VID(pVnode), __func__, lino,
68,758✔
448
           tstrerror(code), TMSG_INFO(pMsg->msgType));
449
  }
450
  return code;
517,326,541✔
451
}
452

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

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

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

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

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

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

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

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

489
  taosArrayDestroy(res.uidList);
1,752,237✔
490

491
_exit:
1,752,237✔
492
  return code;
1,752,237✔
493
}
494

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

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

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

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) {
×
513
    vError("vgId:%d %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
514
  } else {
515
    vTrace("vgId:%d %s done, ctimeMs generated:%" PRId64, TD_VID(pVnode), __func__, ctimeMs);
×
516
  }
517
  return code;
×
518
}
519

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

527
  SVArbCheckSyncReq syncReq = {0};
30,383✔
528

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

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

539
  int32_t code = terrno;
30,383✔
540
  tFreeSVArbCheckSyncReq(&syncReq);
30,383✔
541

542
  return code;
30,383✔
543
}
544

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

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

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

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

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

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

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

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

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

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

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

623
int32_t vnodePreProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
607,256,271✔
624
  int32_t code = 0;
607,256,271✔
625

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

632
  switch (pMsg->msgType) {
607,255,374✔
633
    case TDMT_VND_CREATE_TABLE: {
42,439,433✔
634
      code = vnodePreProcessCreateTableMsg(pVnode, pMsg);
42,439,433✔
635
    } break;
42,437,815✔
636
    case TDMT_VND_ALTER_TABLE: {
12,369,576✔
637
      code = vnodePreProcessAlterTableMsg(pVnode, pMsg);
12,369,576✔
638
    } break;
12,369,576✔
639
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
11,148,305✔
640
    case TDMT_VND_DROP_TTL_TABLE: {
641
      code = vnodePreProcessDropTtlMsg(pVnode, pMsg);
11,148,305✔
642
    } break;
11,131,696✔
643
    case TDMT_VND_SUBMIT: {
517,333,027✔
644
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.preprocess_time, METRIC_LEVEL_LOW,
517,333,027✔
645
                           { code = vnodePreProcessSubmitMsg(pVnode, pMsg); });
646
    } break;
517,329,481✔
647
    case TDMT_VND_DELETE: {
1,752,237✔
648
      code = vnodePreProcessDeleteMsg(pVnode, pMsg);
1,752,237✔
649
    } break;
1,752,237✔
650
    case TDMT_VND_BATCH_DEL: {
×
651
      code = vnodePreProcessBatchDeleteMsg(pVnode, pMsg);
×
652
    } break;
×
653
    case TDMT_VND_ARB_CHECK_SYNC: {
42,925✔
654
      code = vnodePreProcessArbCheckSyncMsg(pVnode, pMsg);
42,925✔
655
    } break;
42,925✔
656
    case TDMT_VND_DROP_TABLE: {
1,508,577✔
657
      code = vnodePreProcessDropTbMsg(pVnode, pMsg);
1,508,577✔
658
    } break;
1,508,577✔
659
#ifdef TD_ENTERPRISE
660
    case TDMT_VND_SSMIGRATE_FILESET: {
3,705✔
661
      code = vnodePreProcessSsMigrateFileSetReq(pVnode, pMsg);
3,705✔
662
    } break;
3,705✔
663
#endif
664
    case TDMT_VND_AUDIT_RECORD: {
3,608✔
665
      vTrace("vgId:%d, pre process TDMT_VND_AUDIT_RECORD", TD_VID(pVnode));
3,608✔
666
    } break;
264✔
667
    default:
20,653,484✔
668
      break;
20,653,484✔
669
  }
670

671
  if (code && code != TSDB_CODE_MSG_PREPROCESSED) {
607,229,760✔
672
    vError("vgId:%d, failed to preprocess write request since %s, msg type:%s", TD_VID(pVnode), tstrerror(code),
98,158✔
673
           TMSG_INFO(pMsg->msgType));
674
  }
675
  return code;
607,237,627✔
676
}
677

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

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

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

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

691
  SRow  **pRow = (SRow **)TARRAY_DATA(pSubmitTbData->aRowP);
31,586✔
692
  int32_t rowIdx = -1;
31,586✔
693
  for (int32_t i = 0; i < sz; i++) {
25,071,921✔
694
    int64_t     seq = 0;
25,040,335✔
695
    SBlobValue *p = taosArrayGet(pBlobSet->pSeqTable, i);
25,040,335✔
696
    if (p->type == TSDB_DATA_BLOB_EMPTY_VALUE || p->type == TSDB_DATA_BLOB_NULL_VALUE) {
25,040,335✔
697
      // skip empty or null blob
698
      continue;
2,486,460✔
699
    }
700

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

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

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

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

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

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

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

742
  SBseBatch *pBatch = NULL;
85✔
743

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

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

760
  int32_t offset = 0;
85✔
761
  // int32_t   rowIdx = -1;
762
  for (int32_t i = 0; i < sz; i++) {
178✔
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;
54✔
768
    }
769
    code = bseBatchPut(pBatch, &seq, pBlobSet->data + p->offset, p->len);
39✔
770
    TSDB_CHECK_CODE(code, lino, _exit);
39✔
771

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

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

779
  int64_t cost = taosGetTimestampUs() - st;
85✔
780
  if (cost >= 500) {
85✔
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:
85✔
785
  if (code != 0) {
85✔
786
    vError("vgId:%d %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
787
  }
788
  return code;
85✔
789
}
790
static int32_t inline vnodeSubmitBlobData(SVnode *pVnode, SSubmitTbData *pData) {
31,671✔
791
  int32_t code = 0;
31,671✔
792
  if (pData->flags & SUBMIT_REQ_WITH_BLOB) {
31,671✔
793
    if (pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
31,671✔
794
      code = vnodeSubmitSubColBlobData(pVnode, pData);
85✔
795
    } else {
796
      code = vnodeSubmitSubRowBlobData(pVnode, pData);
31,586✔
797
    }
798
  }
799

800
  return code;
31,671✔
801
}
802

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

810
  (void)taosThreadMutexLock(&pVnode->mutex);
736,120,590✔
811
  if (pVnode->disableWrite) {
736,184,999✔
812
    (void)taosThreadMutexUnlock(&pVnode->mutex);
×
813
    vError("vgId:%d write is disabled for snapshot, version:%" PRId64, TD_VID(pVnode), ver);
×
814
    return TSDB_CODE_VND_WRITE_DISABLED;
×
815
  }
816
  (void)taosThreadMutexUnlock(&pVnode->mutex);
736,147,654✔
817

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

824
  vGDebug(&pMsg->info.traceId,
736,139,154✔
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)) {
736,191,637✔
831
    vError("vgId:%d, applyTerm mismatch, expected: %" PRId64 ", received: %" PRId64, TD_VID(pVnode),
×
832
           pVnode->state.applyTerm, pMsg->info.conn.applyTerm);
833
    return terrno = TSDB_CODE_INTERNAL_ERROR;
×
834
  }
835

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

842
  atomic_store_64(&pVnode->state.applied, ver);
736,197,227✔
843
  atomic_store_64(&pVnode->state.applyTerm, pMsg->info.conn.applyTerm);
736,198,638✔
844

845
  if (!syncUtilUserCommit(pMsg->msgType)) goto _exit;
736,197,172✔
846

847
  // skip header
848
  pReq = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
725,411,961✔
849
  len = pMsg->contLen - sizeof(SMsgHead);
725,413,261✔
850
  bool needCommit = false;
725,411,927✔
851
  bool forceTrimWal = false;
725,411,927✔
852

853
  switch (pMsg->msgType) {
725,411,927✔
854
    /* META */
855
    case TDMT_VND_CREATE_STB:
5,381,547✔
856
      code = vnodeProcessCreateStbReq(pVnode, ver, pReq, len, pRsp);
5,381,547✔
857
      TSDB_CHECK_CODE(code, lino, _err);
5,381,329✔
858
      break;
5,379,067✔
859
    case TDMT_VND_ALTER_STB:
7,635,133✔
860
      code = vnodeProcessAlterStbReq(pVnode, ver, pReq, len, pRsp);
7,635,133✔
861
      TSDB_CHECK_CODE(code, lino, _err);
7,634,482✔
862
      break;
7,634,482✔
863
    case TDMT_VND_DROP_STB:
912,933✔
864
      code = vnodeProcessDropStbReq(pVnode, ver, pReq, len, pRsp, pMsg);
912,933✔
865
      TSDB_CHECK_CODE(code, lino, _err);
912,933✔
866
      break;
912,933✔
867
    case TDMT_VND_CREATE_TABLE:
45,389,571✔
868
      code = vnodeProcessCreateTbReq(pVnode, ver, pReq, len, pRsp, pMsg);
45,389,571✔
869
      TSDB_CHECK_CODE(code, lino, _err);
45,378,137✔
870
      break;
45,378,137✔
871
    case TDMT_VND_ALTER_TABLE:
12,370,528✔
872
      code = vnodeProcessAlterTbReq(pVnode, ver, pReq, len, pRsp);
12,370,528✔
873
      TSDB_CHECK_CODE(code, lino, _err);
12,370,528✔
874
      break;
12,370,528✔
875
    case TDMT_VND_DROP_TABLE:
1,508,645✔
876
    case TDMT_VND_SNODE_DROP_TABLE:
877
      code = vnodeProcessDropTbReq(pVnode, ver, pReq, len, pRsp, pMsg);
1,508,645✔
878
      TSDB_CHECK_CODE(code, lino, _err);
1,508,645✔
879
      break;
1,508,645✔
880
    case TDMT_VND_CREATE_RSMA:
39,336✔
881
      code = vnodeProcessCreateRsmaReq(pVnode, ver, pReq, len, pRsp);
39,336✔
882
      TSDB_CHECK_CODE(code, lino, _err);
38,595✔
883
      break;
38,595✔
884
    case TDMT_VND_DROP_RSMA:
19,802✔
885
      code = vnodeProcessDropRsmaReq(pVnode, ver, pReq, len, pRsp);
19,802✔
886
      TSDB_CHECK_CODE(code, lino, _err);
19,802✔
887
      break;
19,802✔
888
    case TDMT_VND_ALTER_RSMA:
12,124✔
889
      code = vnodeProcessAlterRsmaReq(pVnode, ver, pReq, len, pRsp);
12,124✔
890
      TSDB_CHECK_CODE(code, lino, _err);
12,124✔
891
      break;
12,124✔
892
    case TDMT_VND_DROP_TTL_TABLE:
×
893
      code = vnodeProcessDropTtlTbReq(pVnode, ver, pReq, len, pRsp);
×
894
      TSDB_CHECK_CODE(code, lino, _err);
×
895
      break;
×
896
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
11,874✔
897
      code = vnodeProcessFetchTtlExpiredTbs(pVnode, ver, pReq, len, pRsp);
11,874✔
898
      TSDB_CHECK_CODE(code, lino, _err);
11,874✔
899
      break;
11,874✔
900
    case TDMT_VND_TRIM:
85,107✔
901
      code = vnodeProcessTrimReq(pVnode, ver, pReq, len, pRsp);
85,107✔
902
      TSDB_CHECK_CODE(code, lino, _err);
85,833✔
903
      break;
85,833✔
904
      case TDMT_VND_TRIM_WAL:
6,784✔
905
        needCommit = true;
6,784✔
906
        forceTrimWal = true;
6,784✔
907
        break;
6,784✔
908
#ifdef TD_ENTERPRISE
909
    case TDMT_VND_SSMIGRATE_FILESET:
3,705✔
910
      if (vnodeProcessSsMigrateFileSetReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
3,705✔
911
      break;
3,705✔
912
    case TDMT_VND_FOLLOWER_SSMIGRATE:
6,669✔
913
      if (vnodeProcessFollowerSsMigrateReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
6,669✔
914
      break;
6,669✔
915
    case TDMT_VND_KILL_SSMIGRATE:
×
916
      if (vnodeProcessKillSsMigrateReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
917
      break;
×
918
#endif
919

920
    /* TSDB */
921
    case TDMT_VND_SUBMIT: {
634,899,860✔
922
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.apply_time, METRIC_LEVEL_LOW, {
634,899,860✔
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);
634,856,911✔
926
      break;
634,848,758✔
927
    }
928
    case TDMT_VND_DELETE:
2,134,615✔
929
      code = vnodeProcessDeleteReq(pVnode, ver, pReq, len, pRsp, pMsg);
2,134,615✔
930
      TSDB_CHECK_CODE(code, lino, _err);
2,134,615✔
931
      break;
2,134,615✔
932
    case TDMT_VND_BATCH_DEL:
×
933
      code = vnodeProcessBatchDeleteReq(pVnode, ver, pReq, len, pRsp);
×
934
      TSDB_CHECK_CODE(code, lino, _err);
×
935
      break;
×
936
      /* TQ */
937
#if defined(USE_TQ) || defined(USE_STREAM)
938
    case TDMT_VND_TMQ_SUBSCRIBE:
1,252,457✔
939
      code = tqProcessSubscribeReq(pVnode->pTq, ver, pReq, len);
1,252,457✔
940
      TSDB_CHECK_CODE(code, lino, _err);
1,249,732✔
941
      break;
1,249,732✔
942
    case TDMT_VND_TMQ_DELETE_SUB:
269,864✔
943
      code = tqProcessDeleteSubReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen);
269,864✔
944
      TSDB_CHECK_CODE(code, lino, _err);
268,585✔
945
      break;
268,585✔
946
    case TDMT_VND_TMQ_COMMIT_OFFSET:
3,709,468✔
947
      code = tqProcessOffsetCommitReq(pVnode->pTq, ver, pReq, len);
3,709,468✔
948
      TSDB_CHECK_CODE(code, lino, _err);
3,709,111✔
949
      break;
3,709,111✔
950
#endif
951
    case TDMT_VND_ALTER_CONFIRM:
4,179,201✔
952
      needCommit = pVnode->config.hashChange;
4,179,201✔
953
      code = vnodeProcessAlterConfirmReq(pVnode, ver, pReq, len, pRsp);
4,179,201✔
954
      TSDB_CHECK_CODE(code, lino, _err);
4,179,201✔
955
      break;
4,179,201✔
956
    case TDMT_VND_ALTER_CONFIG:
329,898✔
957
      vnodeProcessAlterConfigReq(pVnode, ver, pReq, len, pRsp);
329,898✔
958
      break;
329,898✔
959
    case TDMT_VND_COMMIT:
5,143,604✔
960
      needCommit = true;
5,143,604✔
961
      break;
5,143,604✔
962
    case TDMT_VND_CREATE_INDEX:
18,117✔
963
      vnodeProcessCreateIndexReq(pVnode, ver, pReq, len, pRsp);
18,117✔
964
      break;
18,117✔
965
    case TDMT_VND_DROP_INDEX:
14,738✔
966
      vnodeProcessDropIndexReq(pVnode, ver, pReq, len, pRsp);
14,738✔
967
      break;
14,738✔
968
    case TDMT_VND_COMPACT:
69,473✔
969
      vnodeProcessCompactVnodeReq(pVnode, ver, pReq, len, pRsp);
69,473✔
970
      goto _exit;
69,473✔
971
    case TDMT_VND_SCAN:
498✔
972
      vnodeProcessScanVnodeReq(pVnode, ver, pReq, len, pRsp);
498✔
973
      goto _exit;
498✔
974
    case TDMT_SYNC_CONFIG_CHANGE:
×
975
      vnodeProcessConfigChangeReq(pVnode, ver, pReq, len, pRsp);
×
976
      break;
×
977
#ifdef TD_ENTERPRISE
978
    case TDMT_VND_KILL_COMPACT:
1,508✔
979
      vnodeProcessKillCompactReq(pVnode, ver, pReq, len, pRsp);
1,508✔
980
      break;
1,508✔
981
#endif
982
    case TDMT_VND_KILL_SCAN:
92✔
983
      vnodeProcessKillScanReq(pVnode, ver, pReq, len, pRsp);
92✔
984
      break;
92✔
985
    case TDMT_VND_KILL_TRIM:
×
986
      vnodeProcessKillRetentionReq(pVnode, ver, pReq, len, pRsp);
×
987
      break;
×
988
    /* ARB */
989
    case TDMT_VND_ARB_CHECK_SYNC:
1,962✔
990
      vnodeProcessArbCheckSyncReq(pVnode, pReq, len, pRsp);
1,962✔
991
      break;
1,962✔
992
    case TDMT_VND_AUDIT_RECORD:
264✔
993
      vTrace("vgId:%d, processed audit msg", TD_VID(pVnode));
264✔
994
      code = vnodeProcessAuditRecordReq(pVnode, ver, pReq, len, pMsg);
264✔
995
      pRsp->code = code;
264✔
996
      pRsp->msgType = TDMT_VND_AUDIT_RECORD_RSP;
264✔
997
      pRsp->pCont = NULL;
264✔
998
      pRsp->contLen = 0;
264✔
999
      if (code) {
264✔
1000
        goto _err;
×
1001
      }
1002
      break;
264✔
1003
    default:
×
1004
      vError("vgId:%d, unprocessed msg, %d", TD_VID(pVnode), pMsg->msgType);
×
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);
725,269,363✔
1009

1010
  walApplyVer(pVnode->pWal, ver);
725,293,893✔
1011

1012
  if (pVnode->pTq) {
725,304,516✔
1013
    code = tqPushMsg(pVnode->pTq, pMsg->msgType);
725,305,246✔
1014
    if (code) {
725,304,356✔
UNCOV
1015
      vError("vgId:%d, failed to push msg to TQ since %s", TD_VID(pVnode), tstrerror(terrno));
×
UNCOV
1016
      return code;
×
1017
    }
1018
  }
1019

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

1029
    // start a new one
1030
    METRICS_TIMING_BLOCK(pVnode->writeMetrics.memtable_wait_time, METRIC_LEVEL_LOW, {
5,175,078✔
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:
725,302,703✔
1040
  return 0;
736,157,346✔
1041

1042
_err:
38,212✔
1043
  if (code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
38,212✔
1044
    vInfo("vgId:%d, process %s request failed since %s, lino 1:%d ver:%" PRId64, TD_VID(pVnode),
2,262✔
1045
          TMSG_INFO(pMsg->msgType), tstrerror(code), lino, ver);
1046
  } else if (code) {
35,950✔
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;
38,212✔
1052
}
1053

1054
int32_t vnodePreprocessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg) {
421,039,440✔
1055
  if (TDMT_SCH_QUERY != pMsg->msgType && TDMT_SCH_MERGE_QUERY != pMsg->msgType) {
421,039,440✔
1056
    return 0;
82,803,179✔
1057
  }
1058

1059
  int32_t qType = 0;
338,270,621✔
1060
  return qWorkerPreprocessQueryMsg(pVnode->pQuery, pMsg, (TDMT_SCH_QUERY == pMsg->msgType), &qType);
338,273,906✔
1061
}
1062

1063
int32_t vnodeProcessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
421,099,087✔
1064
  const STraceId *trace = &pMsg->info.traceId;
421,099,087✔
1065
  vDebug("vgId:%d, msg:%s, %p in vnode query queue is processing, %" PRIx64 ":%" PRIx64, 
421,101,711✔
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)) {
421,112,103✔
1069
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
431,049✔
1070
    return 0;
431,049✔
1071
  }
1072

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

1078
  SReadHandle handle = {0};
420,674,875✔
1079
  handle.vnode = pVnode;
420,672,655✔
1080
  handle.pMsgCb = &pVnode->msgCb;
420,672,655✔
1081
  handle.pWorkerCb = pInfo->workerCb;
420,659,175✔
1082
  initStorageAPI(&handle.api);
420,658,729✔
1083
  int32_t code = TSDB_CODE_SUCCESS;
420,654,740✔
1084
  bool    redirected = false;
420,654,740✔
1085

1086
  switch (pMsg->msgType) {
420,654,740✔
1087
    case TDMT_SCH_QUERY:
279,479,475✔
1088
      if (!syncIsReadyForRead(pVnode->sync)) {
279,479,475✔
1089
        pMsg->code = (terrno) ? terrno : TSDB_CODE_SYN_NOT_LEADER;
2,743,419✔
1090
        redirected = true;
2,743,419✔
1091
      }
1092
      code = qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
279,482,701✔
1093
      if (redirected) {
279,445,604✔
1094
        vnodeRedirectRpcMsg(pVnode, pMsg, pMsg->code);
2,743,419✔
1095
        return 0;
2,743,419✔
1096
      }
1097

1098
      return code;
276,702,185✔
1099
    case TDMT_SCH_MERGE_QUERY:
58,812,426✔
1100
      return qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
58,812,426✔
1101
    case TDMT_SCH_QUERY_CONTINUE:
44,843,221✔
1102
      return qWorkerProcessCQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
44,843,221✔
1103
    case TDMT_VND_TMQ_CONSUME:
28,137,583✔
1104
      return tqProcessPollReq(pVnode->pTq, pMsg);
28,137,583✔
1105
    case TDMT_VND_TMQ_CONSUME_PUSH:
9,386,734✔
1106
      return tqProcessPollPush(pVnode->pTq);
9,386,734✔
1107
    default:
×
1108
      vError("unknown msg type:%d in query queue", pMsg->msgType);
×
1109
      return TSDB_CODE_APP_ERROR;
×
1110
  }
1111
}
1112

1113
int32_t vnodeProcessFetchMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
1,015,092,651✔
1114
  int32_t code = TSDB_CODE_SUCCESS;
1,015,092,651✔
1115
  const STraceId *trace = &pMsg->info.traceId;
1,015,092,651✔
1116
  vDebug("vgId:%d, msg:%s, %p in fetch queue is processing, %" PRIx64 ":%" PRIx64, 
1,015,132,077✔
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 ||
1,015,224,832✔
1119
       pMsg->msgType == TDMT_VND_BATCH_META || pMsg->msgType == TDMT_VND_TABLE_NAME ||
694,000,519✔
1120
       pMsg->msgType == TDMT_VND_VSUBTABLES_META || pMsg->msgType == TDMT_VND_VSTB_REF_DBS) &&
664,434,972✔
1121
      !syncIsReadyForRead(pVnode->sync)) {
350,809,022✔
1122
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
610,071✔
1123
    return 0;
610,071✔
1124
  }
1125

1126
  switch (pMsg->msgType) {
1,014,666,630✔
1127
    case TDMT_SCH_FETCH:
474,261,133✔
1128
    case TDMT_SCH_MERGE_FETCH:
1129
      return qWorkerProcessFetchMsg(pVnode, pVnode->pQuery, pMsg, 0);
474,261,133✔
1130
    case TDMT_SCH_FETCH_RSP:
×
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:
338,016,509✔
1135
      return qWorkerProcessDropMsg(pVnode, pVnode->pQuery, pMsg, 0);
338,016,509✔
1136
    case TDMT_SCH_TASK_NOTIFY:
11,538✔
1137
      return qWorkerProcessNotifyMsg(pVnode, pVnode->pQuery, pMsg, 0);
11,538✔
1138
    case TDMT_SCH_QUERY_HEARTBEAT:
172,221,561✔
1139
      return qWorkerProcessHbMsg(pVnode, pVnode->pQuery, pMsg, 0);
172,221,561✔
1140
    case TDMT_VND_TABLE_META:
892,423✔
1141
    case TDMT_VND_TABLE_NAME:
1142
      return vnodeGetTableMeta(pVnode, pMsg, true);
892,423✔
1143
    case TDMT_VND_TABLE_CFG:
×
1144
      return vnodeGetTableCfg(pVnode, pMsg, true);
×
1145
    case TDMT_VND_BATCH_META: {
28,961,510✔
1146
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.fetch_batch_meta_time, METRIC_LEVEL_LOW,
28,961,510✔
1147
                           { code = vnodeGetBatchMeta(pVnode, pMsg); });
1148
      METRICS_UPDATE(pVnode->writeMetrics.fetch_batch_meta_count, METRIC_LEVEL_LOW, 1);
28,959,460✔
1149
      return code;
28,954,629✔
1150
    }
1151
    case TDMT_VND_VSUBTABLES_META:
×
1152
      return vnodeGetVSubtablesMeta(pVnode, pMsg);
×
1153
    case TDMT_VND_VSTB_REF_DBS:
×
1154
      return vnodeGetVStbRefDbs(pVnode, pMsg);
×
1155
    case TDMT_VND_QUERY_SCAN_PROGRESS:
368✔
1156
      return vnodeQueryScanProgress(pVnode, pMsg);
368✔
1157
#ifdef TD_ENTERPRISE
1158
    case TDMT_VND_QUERY_COMPACT_PROGRESS:
101,343✔
1159
      return vnodeQueryCompactProgress(pVnode, pMsg);
101,343✔
1160

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

1164
    case TDMT_VND_QUERY_SSMIGRATE_PROGRESS:
6,669✔
1165
      return vnodeQuerySsMigrateProgress(pVnode, pMsg);
6,669✔
1166
#endif
1167
    case TDMT_VND_QUERY_TRIM_PROGRESS:
154,552✔
1168
      return vnodeQueryRetentionProgress(pVnode, pMsg);
154,552✔
1169
      //    case TDMT_VND_TMQ_CONSUME:
1170
      //      return tqProcessPollReq(pVnode->pTq, pMsg);
1171
#ifdef USE_TQ
1172
    case TDMT_VND_TMQ_VG_WALINFO:
632✔
1173
      return tqProcessVgWalInfoReq(pVnode->pTq, pMsg);
632✔
1174
    case TDMT_VND_TMQ_VG_COMMITTEDINFO:
2,633✔
1175
      return tqProcessVgCommittedInfoReq(pVnode->pTq, pMsg);
2,633✔
1176
    case TDMT_VND_TMQ_SEEK:
1,264✔
1177
      return tqProcessSeekReq(pVnode->pTq, pMsg);
1,264✔
1178
#endif
1179
    default:
21,998✔
1180
      vError("unknown msg type:%d in fetch queue", pMsg->msgType);
21,998✔
1181
      return TSDB_CODE_APP_ERROR;
×
1182
  }
1183
}
1184

1185
void vnodeUpdateMetaRsp(SVnode *pVnode, STableMetaRsp *pMetaRsp) {
72,468,625✔
1186
  if (NULL == pMetaRsp) {
72,468,625✔
1187
    return;
×
1188
  }
1189

1190
  tstrncpy(pMetaRsp->dbFName, pVnode->config.dbname, TSDB_DB_FNAME_LEN);
72,468,625✔
1191
  pMetaRsp->dbId = pVnode->config.dbId;
72,469,668✔
1192
  pMetaRsp->vgId = TD_VID(pVnode);
72,471,615✔
1193
  pMetaRsp->precision = pVnode->config.tsdbCfg.precision;
72,468,795✔
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) {
85,107✔
1199
  if (!pVnode->restored) {
85,107✔
1200
    vInfo("vgId:%d, ignore trim req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
2,312✔
1201
    return 0;
2,312✔
1202
  }
1203

1204
  int32_t     code = 0;
82,795✔
1205
  SVTrimDbReq trimReq = {0};
82,795✔
1206

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

1213
  vInfo("vgId:%d, process trim vnode request, time:%" PRIi64 ",%" PRIi64 ", optr:%d, trigger:%d", pVnode->config.vgId,
83,521✔
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);
83,521✔
1217

1218
_exit:
83,521✔
1219
  return code;
83,521✔
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,705✔
1227
  int32_t code = 0;
3,705✔
1228

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

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

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

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

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

1265
_exit:
3,705✔
1266
  pRsp->code = code;
3,705✔
1267
  return code;
3,705✔
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,669✔
1273
  int32_t            code = 0;
6,669✔
1274
  SSsMigrateProgress req = {0};
6,669✔
1275

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

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

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

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

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

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

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

1303
_exit:
×
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};
×
1311
  if (tDeserializeSVDropTtlTableReq(pReq, len, &ttlReq) != 0) {
×
1312
    ret = TSDB_CODE_INVALID_MSG;
×
1313
    goto end;
×
1314
  }
1315

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

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

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

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

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

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

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

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

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

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

1388
    expiredTb.name = p;
48,153✔
1389
    if (mr.me.type == TSDB_CHILD_TABLE) {
48,153✔
1390
      expiredTb.suid = mr.me.ctbEntry.suid;
31,039✔
1391
    }
1392

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

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

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

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

1425
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
5,382,155✔
1426
  pRsp->code = TSDB_CODE_SUCCESS;
5,382,155✔
1427
  pRsp->pCont = NULL;
5,381,547✔
1428
  pRsp->contLen = 0;
5,381,547✔
1429

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

1433
  code = tDecodeSVCreateStbReq(&coder, &req);
5,381,377✔
1434
  if (code) {
5,367,203✔
1435
    pRsp->code = code;
×
1436
    goto _err;
×
1437
  }
1438

1439
  code = metaCreateSuperTable(pVnode->pMeta, ver, &req);
5,367,203✔
1440
  if (code) {
5,382,524✔
1441
    pRsp->code = code;
2,262✔
1442
    goto _err;
2,262✔
1443
  }
1444

1445
  tDecoderClear(&coder);
5,380,262✔
1446
  return 0;
5,379,090✔
1447

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

1453
static int32_t vnodeProcessCreateTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
45,390,301✔
1454
                                       SRpcMsg *pOriginRpc) {
1455
  SDecoder           decoder = {0};
45,390,301✔
1456
  SEncoder           encoder = {0};
45,390,301✔
1457
  int32_t            rcode = 0;
45,388,841✔
1458
  SVCreateTbBatchReq req = {0};
45,388,841✔
1459
  SVCreateTbReq     *pCreateReq;
1460
  SVCreateTbBatchRsp rsp = {0};
45,388,519✔
1461
  SVCreateTbRsp      cRsp = {0};
45,389,243✔
1462
  char               tbName[TSDB_TABLE_FNAME_LEN];
45,386,797✔
1463
  SArray            *tbUids = NULL;
45,389,571✔
1464
  SArray            *tbNames = NULL;
45,389,571✔
1465
  int64_t            tss = taosGetTimestampMs();
45,389,571✔
1466
  pRsp->msgType = TDMT_VND_CREATE_TABLE_RSP;
45,389,571✔
1467
  pRsp->code = TSDB_CODE_SUCCESS;
45,390,301✔
1468
  pRsp->pCont = NULL;
45,389,243✔
1469
  pRsp->contLen = 0;
45,388,921✔
1470

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

1479
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(cRsp));
45,386,674✔
1480
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
45,385,637✔
1481
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
45,381,173✔
1482
  if (rsp.pArray == NULL || tbUids == NULL || tbNames == NULL) {
45,384,310✔
1483
    rcode = -1;
78✔
1484
    terrno = TSDB_CODE_OUT_OF_MEMORY;
78✔
1485
    goto _exit;
×
1486
  }
1487

1488
  // loop to create table
1489
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
98,933,592✔
1490
    pCreateReq = req.pReqs + iReq;
53,546,277✔
1491
    memset(&cRsp, 0, sizeof(cRsp));
53,541,088✔
1492

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

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

1521
    // do create table
1522
    if (metaCreateTable2(pVnode->pMeta, ver, pCreateReq, &cRsp.pMeta) < 0) {
53,532,307✔
1523
      if (pCreateReq->flags & TD_CREATE_IF_NOT_EXISTS && terrno == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
15,949✔
1524
        cRsp.code = TSDB_CODE_SUCCESS;
4,188✔
1525
      } else {
1526
        cRsp.code = terrno;
11,761✔
1527
      }
1528
    } else {
1529
      cRsp.code = TSDB_CODE_SUCCESS;
53,533,048✔
1530
      if (taosArrayPush(tbUids, &pCreateReq->uid) == NULL) {
107,066,872✔
1531
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1532
        rcode = -1;
×
1533
        goto _exit;
×
1534
      }
1535
      vnodeUpdateMetaRsp(pVnode, cRsp.pMeta);
53,533,824✔
1536
    }
1537

1538
    if (taosArrayPush(rsp.pArray, &cRsp) == NULL) {
107,096,447✔
1539
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1540
      rcode = -1;
×
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));
45,387,315✔
1546
  if (tqAddTbUidList(pVnode->pTq, tbUids) < 0) {
45,387,315✔
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;
45,390,261✔
1552
  tEncodeSize(tEncodeSVCreateTbBatchRsp, &rsp, pRsp->contLen, ret);
45,390,261✔
1553
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
45,384,145✔
1554
  if (pRsp->pCont == NULL) {
45,383,029✔
1555
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1556
    rcode = -1;
×
1557
    goto _exit;
×
1558
  }
1559
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
45,382,686✔
1560
  if (tEncodeSVCreateTbBatchRsp(&encoder, &rsp) != 0) {
45,383,036✔
1561
    vError("vgId:%d, failed to encode create table batch response", TD_VID(pVnode));
×
1562
  }
1563

1564
  if (tsEnableAudit && tsEnableAuditCreateTable && tsAuditLevel >= AUDIT_LEVEL_CHILDTABLE) {
45,382,267✔
1565
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
133✔
1566

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

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

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

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

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

1595
_exit:
45,382,267✔
1596
  tDeleteSVCreateTbBatchReq(&req);
45,384,413✔
1597
  taosArrayDestroyEx(rsp.pArray, tFreeSVCreateTbRsp);
45,381,754✔
1598
  taosArrayDestroy(tbUids);
45,370,019✔
1599
  tDecoderClear(&decoder);
45,367,262✔
1600
  tEncoderClear(&encoder);
45,385,520✔
1601
  taosArrayDestroyP(tbNames, NULL);
45,382,682✔
1602
  return rcode;
45,380,364✔
1603
}
1604

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

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

1615
  tDecoderInit(&dc, pReq, len);
7,635,133✔
1616

1617
  // decode req
1618
  code = tDecodeSVCreateStbReq(&dc, &req);
7,635,133✔
1619
  if (code) {
7,625,944✔
1620
    tDecoderClear(&dc);
×
1621
    return code;
×
1622
  }
1623

1624
  code = metaAlterSuperTable(pVnode->pMeta, ver, &req);
7,625,944✔
1625
  if (code) {
7,634,131✔
1626
    pRsp->code = code;
×
1627
    tDecoderClear(&dc);
×
1628
    return code;
×
1629
  }
1630

1631
  tDecoderClear(&dc);
7,634,131✔
1632

1633
  return 0;
7,634,482✔
1634
}
1635

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

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

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

1653
  STraceId* trace = &(pOriginRpc->info.traceId);
912,933✔
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, 
912,933✔
1656
              trace ? trace->msgId : 0, req.name);
1657

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

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

1671
  // return rsp
1672
_exit:
912,933✔
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, 
912,933✔
1674
              trace ? trace->msgId : 0, req.name);
1675
  if (tbUidList) taosArrayDestroy(tbUidList);
912,933✔
1676
  pRsp->code = rcode;
912,933✔
1677
  tDecoderClear(&decoder);
912,933✔
1678
  return 0;
912,933✔
1679
}
1680

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

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

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

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

1698
  for (int32_t i = 0; i < taosArrayGetSize(tags); i++) {
16,153,150✔
1699
    SUpdatedTagVal *pTagVal = taosArrayGet(tags, i);
8,105,131✔
1700
    QUERY_CHECK_NULL(pTagVal, code, lino, end, terrno);
8,105,131✔
1701
    col_id_t cid = pTagVal->colId;
8,105,131✔
1702
    QUERY_CHECK_CONDITION(taosArrayPush(cidList, &cid) != NULL, code, lino, end, terrno);
8,105,131✔
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,048,019✔
1706
  code = tqUpdateTbUidList(pVnode->pTq, tbUids, cidList);
8,048,019✔
1707
  QUERY_CHECK_CODE(code, lino, end);
8,048,019✔
1708

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

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

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

1732
  tDecoderInit(&dc, pReq, len);
12,370,528✔
1733

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

1741
  // process
1742
  if (metaAlterTable(pVnode->pMeta, ver, &vAlterTbReq, &vMetaRsp) < 0) {
12,370,528✔
1743
    vAlterTbRsp.code = terrno;
575,135✔
1744
    tDecoderClear(&dc);
575,135✔
1745
    goto _exit;
575,135✔
1746
  }
1747
  tDecoderClear(&dc);
11,795,393✔
1748

1749
  if (NULL != vMetaRsp.pSchemas) {
11,795,393✔
1750
    vnodeUpdateMetaRsp(pVnode, &vMetaRsp);
3,807,877✔
1751
    vAlterTbRsp.pMeta = &vMetaRsp;
3,807,877✔
1752
  }
1753

1754
_exit:
12,370,312✔
1755
  taosArrayDestroy(vAlterTbReq.pMultiTag);
12,370,528✔
1756
  for (int32_t i = 0; i < taosArrayGetSize(vAlterTbReq.tables); i++) {
20,285,854✔
1757
    SUpdateTableTagVal* pTable = taosArrayGet(vAlterTbReq.tables, i);
7,915,326✔
1758
    taosArrayDestroy(pTable->tags);
7,915,326✔
1759
  }
1760
  taosArrayDestroy(vAlterTbReq.tables);
12,370,528✔
1761

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

1769
  tEncoderClear(&ec);
12,370,528✔
1770
  if (vMetaRsp.pSchemas) {
12,370,528✔
1771
    taosMemoryFree(vMetaRsp.pSchemas);
3,807,877✔
1772
    taosMemoryFree(vMetaRsp.pSchemaExt);
3,807,877✔
1773
  }
1774
  if (vMetaRsp.pColRefs) {
12,370,528✔
1775
    taosMemoryFree(vMetaRsp.pColRefs);
274,990✔
1776
  }
1777
  return 0;
12,370,528✔
1778
}
1779

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

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

1796
  // decode req
1797
  tDecoderInit(&decoder, pReq, len);
1,508,645✔
1798
  ret = tDecodeSVDropTbBatchReq(&decoder, &req);
1,508,645✔
1799
  if (ret < 0) {
1,508,645✔
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,508,645✔
1807
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(SVDropTbRsp));
1,508,645✔
1808
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
1,508,645✔
1809
  if (tbUids == NULL || rsp.pArray == NULL || tbNames == NULL) goto _exit;
1,508,645✔
1810

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

1816
    /* code */
1817
    ret = metaDropTable2(pVnode->pMeta, ver, pDropTbReq);
1,557,895✔
1818
    if (ret < 0) {
1,557,895✔
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,557,895✔
1826
    }
1827

1828
    if (taosArrayPush(rsp.pArray, &dropTbRsp) == NULL) {
3,115,790✔
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,557,895✔
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,508,645✔
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,508,645✔
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,508,645✔
1885
  taosArrayDestroy(tbUids);
1,508,645✔
1886
  tDecoderClear(&decoder);
1,508,645✔
1887
  tEncodeSize(tEncodeSVDropTbBatchRsp, &rsp, pRsp->contLen, ret);
1,508,645✔
1888
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
1,508,645✔
1889
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
1,508,645✔
1890
  if (tEncodeSVDropTbBatchRsp(&encoder, &rsp) != 0) {
1,508,645✔
1891
    vError("vgId:%d, failed to encode drop table batch response", TD_VID(pVnode));
×
1892
  }
1893
  tEncoderClear(&encoder);
1,508,645✔
1894
  taosArrayDestroy(rsp.pArray);
1,508,645✔
1895
  taosArrayDestroy(tbNames);
1,508,645✔
1896
  return 0;
1,508,645✔
1897
}
1898

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

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

1926
static int32_t vnodeProcessAlterRsmaReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
12,124✔
1927
  int32_t         code = 0, lino = 0;
12,124✔
1928
  SVAlterRsmaReq req = {0};
12,124✔
1929
  TAOS_CHECK_EXIT(tDeserializeSVAlterRsmaReq(pReq, len, &req));
12,124✔
1930
  TAOS_CHECK_EXIT(metaAlterRsma(pVnode->pMeta, ver, &req));
12,124✔
1931
_exit:
12,124✔
1932
  pRsp->msgType = TDMT_VND_ALTER_RSMA_RSP;
12,124✔
1933
  pRsp->pCont = NULL;
12,124✔
1934
  pRsp->code = code;
12,124✔
1935
  pRsp->contLen = 0;
12,124✔
1936
  tFreeSVAlterRsmaReq(&req);
12,124✔
1937
  return code;
12,124✔
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) {
65,203✔
2165
  int32_t code = 0;
65,203✔
2166
  int32_t lino = 0;
65,203✔
2167

2168
  SMetaEntry *pEntry = NULL;
65,203✔
2169
  code = metaFetchEntryByUid(pVnode->pMeta, pSubmitTbData->suid, &pEntry);
65,203✔
2170
  if (code) {
65,203✔
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) {
65,203✔
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));
65,203✔
2182
  if (NULL == *ppRsp) {
65,203✔
2183
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2184
  }
2185
  (*ppRsp)->suid = pSubmitTbData->suid;
65,203✔
2186
  (*ppRsp)->tuid = pSubmitTbData->uid;
65,203✔
2187
  (*ppRsp)->sversion = pEntry->stbEntry.schemaRow.version;
65,203✔
2188
  (*ppRsp)->vgId = pVnode->config.vgId;
65,203✔
2189
  (*ppRsp)->numOfColumns = pEntry->stbEntry.schemaRow.nCols;
65,203✔
2190
  (*ppRsp)->numOfTags = pEntry->stbEntry.schemaTag.nCols;
65,203✔
2191
  (*ppRsp)->pSchemas =
65,203✔
2192
      taosMemoryCalloc(pEntry->stbEntry.schemaRow.nCols + pEntry->stbEntry.schemaTag.nCols, sizeof(SSchema));
65,203✔
2193
  if (NULL == (*ppRsp)->pSchemas) {
65,203✔
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));
65,203✔
2199
  memcpy((*ppRsp)->pSchemas + pEntry->stbEntry.schemaRow.nCols, pEntry->stbEntry.schemaTag.pSchema,
65,203✔
2200
         pEntry->stbEntry.schemaTag.nCols * sizeof(SSchema));
65,203✔
2201
  if (pEntry->pExtSchemas != NULL) {
65,203✔
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) {
65,203✔
2213
    TSDB_CHECK_CODE(code = TSDB_CODE_TDB_TABLE_ALREADY_EXIST, lino, _exit);
460✔
2214
  }
2215
_exit:
65,203✔
2216
  metaFetchEntryFree(&pEntry);
65,203✔
2217
  if (code != TSDB_CODE_SUCCESS) {
65,203✔
2218
    vError("vgId:%d, failed to build exist sub table response, code:0x%0x, line:%d", TD_VID(pVnode), code, lino);
460✔
2219
  }
2220
  return code;
65,203✔
2221
}
2222

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

2227
  SMetaEntry *pEntry = NULL;
32,674✔
2228
  code = metaFetchEntryByUid(pVnode->pMeta, uid, &pEntry);
32,674✔
2229
  if (code) {
32,674✔
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) {
32,674✔
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));
32,674✔
2240
  if (NULL == *ppRsp) {
32,674✔
2241
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2242
  }
2243

2244
  (*ppRsp)->tuid = pEntry->uid;
32,674✔
2245
  (*ppRsp)->vgId = pVnode->config.vgId;
32,674✔
2246
  (*ppRsp)->sversion = pEntry->ntbEntry.schemaRow.version;
32,674✔
2247
  (*ppRsp)->numOfColumns = pEntry->ntbEntry.schemaRow.nCols;
32,674✔
2248
  (*ppRsp)->pSchemas = taosMemoryCalloc(pEntry->ntbEntry.schemaRow.nCols, sizeof(SSchema));
32,674✔
2249
  if (NULL == (*ppRsp)->pSchemas) {
32,674✔
2250
    taosMemoryFree(*ppRsp);
×
2251
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2252
  }
2253
  memcpy((*ppRsp)->pSchemas, pEntry->ntbEntry.schemaRow.pSchema, pEntry->ntbEntry.schemaRow.nCols * sizeof(SSchema));
32,674✔
2254
  if (pEntry->pExtSchemas != NULL) {
32,674✔
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:
32,674✔
2265
  metaFetchEntryFree(&pEntry);
32,674✔
2266
  if (code != TSDB_CODE_SUCCESS) {
32,674✔
2267
    vError("vgId:%d, failed to build exist normal table response, code:%d, line:%d", TD_VID(pVnode), code, lino);
×
2268
  }
2269
  return code;
32,674✔
2270
}
2271

2272
static int32_t buildExistTableInStreamRsp(SVnode *pVnode, SSubmitTbData *pSubmitTbData, STableMetaRsp **ppRsp) {
299,712✔
2273
  if (pSubmitTbData->pCreateTbReq->flags & TD_CREATE_NORMAL_TB_IN_STREAM) {
299,712✔
2274
    int32_t code = buildExistNormalTalbeRsp(pVnode, pSubmitTbData->uid, ppRsp);
32,444✔
2275
    if (code) {
32,674✔
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;
32,674✔
2280
  } else if (pSubmitTbData->pCreateTbReq->flags & TD_CREATE_SUB_TB_IN_STREAM) {
267,038✔
2281
    return buildExistSubTalbeRsp(pVnode, pSubmitTbData, ppRsp);
65,203✔
2282
  }
2283
  return TSDB_CODE_SUCCESS;
201,835✔
2284
}
2285

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

2295
  for (int32_t i = 0; i < numTbData; ++i) {
1,295,165,789✔
2296
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
660,299,612✔
2297

2298
    if (pTbData->pCreateTbReq == NULL) {
660,300,300✔
2299
      continue;
644,815,239✔
2300
    }
2301

2302
    pTbData->uid = pTbData->pCreateTbReq->uid;
15,484,747✔
2303

2304
    // Alloc necessary resources
2305
    if (pResponse->aCreateTbRsp == NULL) {
15,484,747✔
2306
      pResponse->aCreateTbRsp = taosArrayInit(numTbData, sizeof(SVCreateTbRsp));
12,616,294✔
2307
      if (pResponse->aCreateTbRsp == NULL) {
12,616,294✔
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,484,747✔
2318

2319
    SVCreateTbRsp *pCreateTbRsp = taosArrayReserve(pResponse->aCreateTbRsp, 1);
15,484,787✔
2320
    code = metaCreateTable2(pVnode->pMeta, version, pTbData->pCreateTbReq, &pCreateTbRsp->pMeta);
15,484,747✔
2321
    if (code == TSDB_CODE_SUCCESS) {
15,484,219✔
2322
      // Allocate necessary resources
2323
      if (newTbUids == NULL) {
15,130,372✔
2324
        newTbUids = taosArrayInit(numTbData, sizeof(int64_t));
12,316,699✔
2325
        if (newTbUids == NULL) {
12,316,699✔
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,261,232✔
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,130,860✔
2342
        vnodeUpdateMetaRsp(pVnode, pCreateTbRsp->pMeta);
15,130,198✔
2343
      }
2344
    } else if (code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
353,847✔
2345
      code = terrno = 0;
352,789✔
2346
      pTbData->uid = pTbData->pCreateTbReq->uid;  // update uid if table exist for using below
352,789✔
2347

2348
      // stream: get sver from meta, write to pCreateTbRsp, and need to check crateTbReq is same as meta.
2349
      if (i == 0) {
352,559✔
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);
299,712✔
2353
        if (code) {
299,712✔
2354
          vInfo("vgId:%d failed to create table in stream:%s, code(0x%0x):%s", TD_VID(pVnode),
33,134✔
2355
                pTbData->pCreateTbReq->name, code, tstrerror(code));
2356
          taosArrayDestroy(newTbUids);
33,134✔
2357
          return code;
33,134✔
2358
        }
2359
      }
2360
    } else {
2361
      code = terrno;
1,058✔
2362
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
1,058✔
2363
             tstrerror(code), version);
2364
      taosArrayDestroy(newTbUids);
1,058✔
2365
      return code;
1,058✔
2366
    }
2367
  }
2368

2369
  // Update the affected table uid list
2370
  if (taosArrayGetSize(newTbUids) > 0) {
634,866,177✔
2371
    vDebug("vgId:%d, add %d table into query table list in handling submit", TD_VID(pVnode),
12,315,706✔
2372
           (int32_t)taosArrayGetSize(newTbUids));
2373
    if (tqAddTbUidList(pVnode->pTq, newTbUids) != 0) {
12,316,368✔
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);
634,868,054✔
2379

2380
  taosArrayDestroy(newTbUids);
634,868,054✔
2381
  return code;
634,867,824✔
2382
}
2383

2384
static void addExistTableInfoIntoRes(SVnode *pVnode, SSubmitReq2 *pRequest, SSubmitRsp2 *pResponse,
460✔
2385
                                     SSubmitTbData *pTbData, int32_t numTbData) {
2386
  int32_t code = 0;
460✔
2387
  int32_t lino = 0;
460✔
2388
  if ((pTbData->flags & SUBMIT_REQ_SCHEMA_RES) == 0) {
460✔
2389
    return;
×
2390
  }
2391
  if (pResponse->aCreateTbRsp) {  // If aSubmitTbData is not NULL, it means that the request is a create table request,
460✔
2392
                                  // so table info has exitst and we do not need to add again.
2393
    return;
460✔
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) {
634,868,026✔
2423
  int32_t code = TSDB_CODE_SUCCESS;
634,868,026✔
2424
  int32_t numTbData = taosArrayGetSize(pRequest->aSubmitTbData);
634,868,026✔
2425
  int8_t  hasBlob = 0;
634,867,171✔
2426

2427
  // Scan submit data
2428
  for (int32_t i = 0; i < numTbData; ++i) {
1,295,129,692✔
2429
    SMetaInfo      info = {0};
660,265,348✔
2430
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
660,265,508✔
2431

2432
    if (pTbData->flags & SUBMIT_REQ_WITH_BLOB) {
660,264,298✔
2433
      hasBlob = 1;
31,671✔
2434
    }
2435
    if (pTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
660,264,860✔
2436
      continue;  // skip column data format
6,739,811✔
2437
    }
2438
    if (pTbData->flags & SUBMIT_REQ_ONLY_CREATE_TABLE) {
653,525,049✔
2439
      continue;  // skip only crate table request
2,378,643✔
2440
    }
2441

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

2450
    if (info.suid != pTbData->suid) {
651,144,002✔
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) {
651,142,728✔
2460
      code = metaGetInfo(pVnode->pMeta, info.suid, &info, NULL);
505,823,333✔
2461
      if (code) {
505,826,158✔
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) {
651,145,553✔
2470
      code = TSDB_CODE_TDB_INVALID_TABLE_SCHEMA_VER;
460✔
2471
      addExistTableInfoIntoRes(pVnode, pRequest, pResponse, pTbData, numTbData);
460✔
2472
      vDebug("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64
460✔
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;
460✔
2478
    }
2479
  }
2480

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

2484
  for (int32_t i = 0; i < numTbData; ++i) {
1,295,120,808✔
2485
    int32_t        affectedRows = 0;
660,263,146✔
2486
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
660,262,945✔
2487

2488
    if (pTbData->flags & SUBMIT_REQ_ONLY_CREATE_TABLE) {
660,263,954✔
2489
      continue;
2,378,643✔
2490
    }
2491

2492
    if (hasBlob) {
657,884,581✔
2493
      code = vnodeSubmitBlobData(pVnode, pTbData);
31,671✔
2494
      if (code) {
31,671✔
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);
657,884,581✔
2502
    if (code) {
657,401,828✔
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);
657,401,828✔
2509
    if (code) {
657,871,236✔
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;
657,871,236✔
2515
  }
2516

2517
  vDebug("vgId:%d, handle data write done, version:%" PRId64 ", affectedRows:%d", TD_VID(pVnode), version,
634,857,662✔
2518
         pResponse->affectedRows);
2519
  return code;
634,866,588✔
2520
}
2521

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

2525
  int32_t   numCols = taosArrayGetSize(pTbData->aCol);
6,737,163✔
2526
  SColData *aColData = (SColData *)TARRAY_DATA(pTbData->aCol);
6,738,156✔
2527

2528
  if (numCols <= 0) {
6,737,494✔
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,737,494✔
2536
      aColData[0].nVal <= 0) {
6,737,825✔
2537
    code = TSDB_CODE_INVALID_MSG;
×
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,676,995✔
2546
    if (aColData[i].nVal != aColData[0].nVal) {
13,940,829✔
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,736,166✔
2557
  SRowKey  lastKey = {0};
6,736,166✔
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,736,497✔
2581
    }
2582

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

2586
  return code;
6,739,480✔
2587
}
2588

2589
static int32_t vnodeScanSubmitRowData(SVnode *pVnode, SSubmitTbData *pTbData, TSKEY minKey, TSKEY maxKey) {
653,544,011✔
2590
  int32_t code = 0;
653,544,011✔
2591

2592
  int32_t numRows = taosArrayGetSize(pTbData->aRowP);
653,544,011✔
2593
  SRow  **aRow = (SRow **)TARRAY_DATA(pTbData->aRowP);
653,535,771✔
2594

2595
  if (numRows <= 0 && (pTbData->flags & SUBMIT_REQ_ONLY_CREATE_TABLE) == 0) {
653,536,180✔
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;
653,536,180✔
2603
  SRowKey  lastKey = {0};
653,536,180✔
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;
330,202✔
2618
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] key:%" PRId64
330,202✔
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;
651,159,605✔
2634
    }
2635

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

2639
  return code;
643,440,608✔
2640
}
2641

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

2646
  TSKEY now = taosGetTimestamp(pVnode->config.tsdbCfg.precision);
634,887,604✔
2647
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * pVnode->config.tsdbCfg.keep2;
634,886,071✔
2648
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
634,892,149✔
2649
  for (int32_t i = 0; i < numTbData; i++) {
1,295,165,180✔
2650
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
660,282,345✔
2651

2652
    if (pTbData->pCreateTbReq && pTbData->pCreateTbReq->uid == 0) {
660,287,306✔
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) {
660,284,561✔
2660
      code = vnodeScanColumnData(pVnode, pTbData, minKey, maxKey);
6,737,825✔
2661
      if (code) {
6,739,480✔
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);
653,543,476✔
2668
      if (code) {
653,538,617✔
2669
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
3,746✔
2670
               __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2671
        return code;
×
2672
      }
2673
    }
2674
  }
2675

2676
  return code;
634,882,835✔
2677
}
2678

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

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

2704
  return 0;
726✔
2705
}
2706

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

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

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

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

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

2736
  return code;
66✔
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,
66✔
2751
                                    SAuditRecord *record) {
2752
  int32_t code = 0;
66✔
2753
  int32_t lino = 0;
66✔
2754

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

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

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

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

2773
  STagVal tv = (STagVal){.cid = tSchema->colId,
66✔
2774
                         .type = tSchema->type,
66✔
2775
                         .nData = strlen(record->strClusterId),
66✔
2776
                         .pData = record->strClusterId};  // address copy, no value
66✔
2777
  if ((pTagVals = taosArrayInit(1, sizeof(STagVal))) == NULL) {
66✔
2778
    code = terrno;
×
2779
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
2780
  }
2781
  if (NULL == taosArrayPush(pTagVals, &tv)) {
66✔
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);
66✔
2788
  if (code != TSDB_CODE_SUCCESS) {
66✔
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,
66✔
2794
                               TSDB_DEFAULT_TABLE_TTL);
2795
_exit:
66✔
2796
  if (code != 0) {
66✔
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) {
66✔
2811
    taosArrayDestroy(pTagVals);
66✔
2812
    pTagVals = NULL;
66✔
2813
  }
2814

2815
  return code;
66✔
2816
}
2817

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

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

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

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

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

2840
    // colId is consistent with audit_columns in mndStb.c
2841
    void *data = NULL;
6,534✔
2842
    if (pCol->colId == 1) {
6,534✔
2843
      data = &record->curTime;
726✔
2844
    } else if (pCol->colId == 2) {
5,808✔
2845
      data = record->detail;
726✔
2846
    } else if (pCol->colId == 3) {
5,082✔
2847
      data = record->user;
726✔
2848
    } else if (pCol->colId == 4) {
4,356✔
2849
      data = record->operation;
726✔
2850
    } else if (pCol->colId == 5) {
3,630✔
2851
      data = record->target1;
726✔
2852
    } else if (pCol->colId == 6) {
2,904✔
2853
      data = record->target2;
726✔
2854
    } else if (pCol->colId == 7) {
2,178✔
2855
      data = record->clientAddress;
726✔
2856
    } else if (pCol->colId == 8) {
1,452✔
2857
      data = &record->duration;
726✔
2858
    } else if (pCol->colId == 9) {
726✔
2859
      data = &record->affectedRows;
726✔
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) {
6,534✔
2867
      case TSDB_DATA_TYPE_NCHAR:
4,356✔
2868
      case TSDB_DATA_TYPE_VARBINARY:
2869
      case TSDB_DATA_TYPE_VARCHAR: {
2870
        // if (colDataIsNull_s(pColInfoData, j)) {
2871
        //   SColVal cv = COL_VAL_NULL(pCol->colId, pCol->type);
2872
        //   if (NULL == taosArrayPush(pVals, &cv)) {
2873
        //     goto _end;
2874
        //   }
2875
        // } else {
2876
        char   *str = data;
4,356✔
2877
        int32_t len = strlen(str);
4,356✔
2878
        vTrace("set svalue id:%d %s, len:%d, bytes:%d", pCol->colId, str, len, pCol->bytes);
4,356✔
2879
        if (len > pCol->bytes - VARSTR_HEADER_SIZE) {
4,356✔
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};
4,356✔
2885
        sv.type = pCol->type;
4,356✔
2886
        sv.nData = len;
4,356✔
2887
        sv.pData = str;
4,356✔
2888
        SColVal cv = COL_VAL_VALUE(pCol->colId, sv);
4,356✔
2889
        if (NULL == taosArrayPush(pVals, &cv)) {
4,356✔
2890
          code = terrno;
×
2891
          TAOS_CHECK_GOTO(code, &lino, _exit);
×
2892
        }
2893
        //}
2894
        break;
4,356✔
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: {
2,178✔
2905
        /*
2906
          if (colDataIsNull_s(pColInfoData, j)) {
2907
            if (PRIMARYKEY_TIMESTAMP_COL_ID == pCol->colId) {
2908
              qError("Primary timestamp column should not be null");
2909
              terrno = TSDB_CODE_PAR_INCORRECT_TIMESTAMP_VAL;
2910
              goto _end;
2911
            }
2912

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

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

2944
_exit:
726✔
2945
  if (code != 0) {
726✔
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;
726✔
2954
}
2955

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

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

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

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

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

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

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

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

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

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

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

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

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

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

3018
_exit:
726✔
3019
  if (code != 0) {
726✔
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;
726✔
3029
}
3030

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

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

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

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

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

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

3056
  vTrace("vgId:%d, going to create table", TD_VID(pVnode));
726✔
3057
  code = vnodeHandleAutoCreateTable(pVnode, ver, pSubmitReq, pSubmitRsp);
726✔
3058
  if (code) {
726✔
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));
726✔
3065
  code = vnodeHandleDataWrite(pVnode, ver, pSubmitReq, pSubmitRsp);
726✔
3066
  if (code) {
726✔
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);
726✔
3073

3074
_exit:
726✔
3075
  if (code != 0)
726✔
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);
726✔
3080
  (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsert, 1);
726✔
3081
  if (code == 0) {
726✔
3082
    (void)atomic_add_fetch_64(&pVnode->statis.nInsertSuccess, pSubmitRsp->affectedRows);
726✔
3083
    (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
726✔
3084
  }
3085

3086
  // update metrics
3087
  METRICS_UPDATE(pVnode->writeMetrics.total_requests, METRIC_LEVEL_LOW, 1);
726✔
3088
  METRICS_UPDATE(pVnode->writeMetrics.total_rows, METRIC_LEVEL_HIGH, pSubmitRsp->affectedRows);
726✔
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 &&
726✔
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) {
726✔
3106
    tDestroySubmitReq(pSubmitReq, TSDB_MSG_FLG_ENCODE);
726✔
3107
    taosMemoryFree(pSubmitReq);
726✔
3108
  }
3109
  if (pSubmitRsp != NULL) {
726✔
3110
    tDestroySSubmitRsp2(pSubmitRsp, TSDB_MSG_FLG_ENCODE);
726✔
3111
  }
3112

3113
  return code;
726✔
3114
}
3115

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

3121
  SVAuditRecordReq req = {0};
264✔
3122
  if (tDeserializeSVAuditRecordReq(pReq, len, &req) != 0) {
264✔
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));
264✔
3129

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

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

3141
  SMetaReader merStb = {0};
264✔
3142
  metaReaderDoInit(&merStb, pVnode->pMeta, META_READER_LOCK);
264✔
3143
  code = metaGetTableEntryByName(&merStb, AUDIT_STABLE_NAME);
264✔
3144
  if (code != 0) {
264✔
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",
264✔
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);
264✔
3152
  if (code != 0) {
264✔
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);
264✔
3157
  metaReaderClear(&merStb);
264✔
3158
  // pSchema pTagSchema can be cached in the future
3159

3160
  SJson *pRecords = tjsonGetObjectItem(pJson, "records");
264✔
3161
  if (pRecords == NULL) {
264✔
3162
    TAOS_CHECK_GOTO(vnodeSaveOneAuditRecord(pVnode, ver, pJson, pTagSchema, suid, pSchema, pOriginalMsg), &lino, _exit);
132✔
3163
  } else {
3164
    int32_t size = tjsonGetArraySize(pRecords);
132✔
3165
    vTrace("%d items in records", size);
132✔
3166
    for (int32_t i = 0; i < size; ++i) {
726✔
3167
      SJson *pRecord = tjsonGetArrayItem(pRecords, i);
594✔
3168
      if (pRecord == NULL) {
594✔
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,
594✔
3174
                      _exit);
3175
    }
3176
  }
3177

3178
_exit:
264✔
3179
  if (code != 0)
264✔
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);
264✔
3184
  if (pTagSchema) {
264✔
3185
    taosMemoryFreeClear(pTagSchema->pSchema);
264✔
3186
    taosMemoryFree(pTagSchema);
264✔
3187
  }
3188
  if (pJson != NULL) {
264✔
3189
    cJSON_Delete(pJson);
264✔
3190
    pJson = NULL;
264✔
3191
  }
3192
  tFreeSVAuditRecordReq(&req);
264✔
3193

3194
  return code;
264✔
3195
}
3196

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

3203
  SSubmitReq2 *pSubmitReq = &(SSubmitReq2){0};
634,901,840✔
3204
  SSubmitRsp2 *pSubmitRsp = &(SSubmitRsp2){0};
634,901,840✔
3205
  int32_t      ret;
3206
  SEncoder     ec = {0};
634,901,110✔
3207

3208
  pRsp->code = TSDB_CODE_SUCCESS;
634,900,380✔
3209

3210
  void           *pAllocMsg = NULL;
634,900,312✔
3211
  SSubmitReq2Msg *pMsg = (SSubmitReq2Msg *)pReq;
634,900,312✔
3212
  SDecoder        dc = {0};
634,900,312✔
3213
  if (0 == taosHton64(pMsg->version)) {
634,898,980✔
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));
634,900,453✔
3225
    len -= sizeof(SSubmitReq2Msg);
634,900,625✔
3226

3227
    tDecoderInit(&dc, pReq, len);
634,900,625✔
3228
    if (tDecodeSubmitReq(&dc, pSubmitReq, NULL) < 0) {
634,901,042✔
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);
634,897,990✔
3236
  if (code) {
634,881,108✔
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",
634,881,108✔
3243
          TD_VID(pVnode), ver, (int32_t)taosArrayGetSize(pSubmitReq->aSubmitTbData));
3244

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

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

3261
_exit:
634,901,625✔
3262
  // message
3263
  pRsp->code = code;
634,901,840✔
3264
  tEncodeSize(tEncodeSSubmitRsp2, pSubmitRsp, pRsp->contLen, ret);
634,901,840✔
3265
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
634,900,101✔
3266
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
634,899,862✔
3267
  if (tEncodeSSubmitRsp2(&ec, pSubmitRsp) < 0) {
634,900,408✔
3268
    vError("vgId:%d, failed to encode submit response", TD_VID(pVnode));
×
3269
  }
3270
  tEncoderClear(&ec);
634,900,907✔
3271

3272
  // update statistics
3273
  (void)atomic_add_fetch_64(&pVnode->statis.nInsert, pSubmitRsp->affectedRows);
634,896,255✔
3274
  (void)atomic_add_fetch_64(&pVnode->statis.nInsertSuccess, pSubmitRsp->affectedRows);
634,897,832✔
3275
  (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsert, 1);
634,899,823✔
3276

3277
  // update metrics
3278
  METRICS_UPDATE(pVnode->writeMetrics.total_requests, METRIC_LEVEL_LOW, 1);
634,893,889✔
3279
  METRICS_UPDATE(pVnode->writeMetrics.total_rows, METRIC_LEVEL_HIGH, pSubmitRsp->affectedRows);
634,892,935✔
3280
  METRICS_UPDATE(pVnode->writeMetrics.total_bytes, METRIC_LEVEL_LOW, pMsg->header.contLen);
634,892,416✔
3281

3282
  if (tsEnableMonitor && tsMonitorFqdn[0] != 0 && tsMonitorPort != 0 && pSubmitRsp->affectedRows > 0 &&
634,892,950✔
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) {
634,892,950✔
3296
    (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
634,857,471✔
3297
  }
3298

3299
  // clear
3300
  tDestroySubmitReq(pSubmitReq, 0 == taosHton64(pMsg->version) ? TSDB_MSG_FLG_CMPT : TSDB_MSG_FLG_DECODE);
634,896,591✔
3301
  tDestroySSubmitRsp2(pSubmitRsp, TSDB_MSG_FLG_ENCODE);
634,889,638✔
3302

3303
  if (code) {
634,884,146✔
3304
    terrno = code;
35,950✔
3305
    if (code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
35,950✔
3306
      vInfo("vgId:%d, failed to process submit request since %s, lino:%d, version:%" PRId64, TD_VID(pVnode),
33,134✔
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,816✔
3311
             tstrerror(code), lino, ver);
3312
    }
3313
  }
3314

3315
  taosMemoryFree(pAllocMsg);
634,884,442✔
3316
  tDecoderClear(&dc);
634,887,675✔
3317

3318
  return code;
634,899,225✔
3319
}
3320

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

3324
  vInfo("vgId:%d, trim meta of tables per hash range [%" PRIu32 ", %" PRIu32 "]. apply-index:%" PRId64, TD_VID(pVnode),
24,690✔
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,690✔
3329

3330
  return code;
24,690✔
3331
}
3332

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

3340
  code = vnodeConsolidateAlterHashRange(pVnode, ver);
24,690✔
3341
  if (code < 0) {
24,690✔
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,690✔
3346

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

3353
  return code;
4,179,201✔
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) {
329,898✔
3360
  bool walChanged = false;
329,898✔
3361
  bool tsdbChanged = false;
329,898✔
3362

3363
  SAlterVnodeConfigReq req = {0};
329,898✔
3364
  if (tDeserializeSAlterVnodeConfigReq(pReq, len, &req) != 0) {
329,898✔
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
329,898✔
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) {
329,898✔
3379
    pVnode->config.cacheLastSize = req.cacheLastSize;
7,264✔
3380
    tsdbCacheSetCapacity(pVnode, (size_t)pVnode->config.cacheLastSize * 1024 * 1024);
7,264✔
3381
  }
3382

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

3389
  if (pVnode->config.szCache != req.pages) {
329,898✔
3390
    if ((terrno = metaAlterCache(pVnode->pMeta, req.pages)) < 0) {
4,668✔
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,668✔
3396
      pVnode->config.szCache = req.pages;
4,668✔
3397
    }
3398
  }
3399

3400
  if (pVnode->config.cacheLast != req.cacheLast) {
329,898✔
3401
    pVnode->config.cacheLast = req.cacheLast;
91,097✔
3402
  }
3403

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

3409
  if (pVnode->config.walCfg.level != req.walLevel) {
329,898✔
3410
    if (pVnode->config.walCfg.level == 0) {
39,306✔
3411
      pVnode->config.walCfg.clearFiles = 1;
2,224✔
3412
    }
3413
    pVnode->config.walCfg.level = req.walLevel;
39,306✔
3414
    walChanged = true;
39,306✔
3415
  }
3416

3417
  if (pVnode->config.walCfg.retentionPeriod != req.walRetentionPeriod) {
329,898✔
3418
    pVnode->config.walCfg.retentionPeriod = req.walRetentionPeriod;
48,455✔
3419
    walChanged = true;
48,455✔
3420
  }
3421

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

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

3432
  if (pVnode->config.tsdbCfg.keep1 != req.daysToKeep1) {
329,898✔
3433
    pVnode->config.tsdbCfg.keep1 = req.daysToKeep1;
43,022✔
3434
    tsdbChanged = true;
43,022✔
3435
  }
3436

3437
  if (pVnode->config.tsdbCfg.keep2 != req.daysToKeep2) {
329,898✔
3438
    pVnode->config.tsdbCfg.keep2 = req.daysToKeep2;
37,196✔
3439
    tsdbChanged = true;
37,196✔
3440
  }
3441

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

3447
  if (req.sttTrigger != -1 && req.sttTrigger != pVnode->config.sttTrigger) {
329,898✔
3448
    if (req.sttTrigger > 1 && pVnode->config.sttTrigger > 1) {
2,570✔
3449
      pVnode->config.sttTrigger = req.sttTrigger;
1,478✔
3450
    } else {
3451
      vnodeAWait(&pVnode->commitTask);
1,092✔
3452

3453
      int32_t ret = tsdbDisableAndCancelAllBgTask(pVnode->pTsdb);
1,092✔
3454
      if (ret != 0) {
1,092✔
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,092✔
3459
      tsdbEnableBgTask(pVnode->pTsdb);
1,092✔
3460
    }
3461
  }
3462

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

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

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

3486
  if (tsdbChanged) {
329,898✔
3487
    tsdbSetKeepCfg(pVnode->pTsdb, &pVnode->config.tsdbCfg);
59,434✔
3488
  }
3489

3490
  return 0;
329,898✔
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,134,615✔
3541
                                     SRpcMsg *pOriginalMsg) {
3542
  int32_t     code = 0;
2,134,615✔
3543
  SDecoder   *pCoder = &(SDecoder){0};
2,134,615✔
3544
  SDeleteRes *pRes = &(SDeleteRes){0};
2,134,615✔
3545

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

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

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

3561
  if (pRes->affectedRows > 0) {
2,134,615✔
3562
    for (int32_t iUid = 0; iUid < taosArrayGetSize(pRes->uidList); iUid++) {
3,800,756✔
3563
      uint64_t uid = *(uint64_t *)taosArrayGet(pRes->uidList, iUid);
2,168,947✔
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,947✔
3566
      code = tsdbDeleteTableData(pVnode->pTsdb, ver, pRes->suid, uid, pRes->skey, pRes->ekey, secureDelete);
2,168,947✔
3567
      if (code) goto _err;
2,168,947✔
3568
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, pRes->ctimeMs);
2,168,947✔
3569
      if (code) goto _err;
2,168,947✔
3570
    }
3571
  }
3572

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

3576
  SVDeleteRsp rsp = {.affectedRows = pRes->affectedRows};
2,134,615✔
3577
  int32_t     ret = 0;
2,134,615✔
3578
  tEncodeSize(tEncodeSVDeleteRsp, &rsp, pRsp->contLen, ret);
2,134,615✔
3579
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
2,134,615✔
3580
  SEncoder ec = {0};
2,134,615✔
3581
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
2,134,615✔
3582
  code = tEncodeSVDeleteRsp(&ec, &rsp);
2,134,615✔
3583
  if (code) goto _err;
2,134,615✔
3584
  tEncoderClear(&ec);
2,134,615✔
3585
  return code;
2,134,615✔
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) {
18,117✔
3606
  SVCreateStbReq req = {0};
18,117✔
3607
  SDecoder       dc = {0};
18,117✔
3608
  int32_t        code = 0;
18,117✔
3609

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

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

3622
  code = metaAddIndexToSuperTable(pVnode->pMeta, ver, &req);
18,117✔
3623
  if (code) {
18,117✔
3624
    pRsp->code = code;
×
3625
    goto _err;
×
3626
  }
3627
  tDecoderClear(&dc);
18,117✔
3628
  return 0;
18,117✔
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,738✔
3635
  SDropIndexReq req = {0};
14,738✔
3636
  int32_t       code = 0;
14,738✔
3637
  pRsp->msgType = TDMT_VND_DROP_INDEX_RSP;
14,738✔
3638
  pRsp->code = TSDB_CODE_SUCCESS;
14,738✔
3639
  pRsp->pCont = NULL;
14,738✔
3640
  pRsp->contLen = 0;
14,738✔
3641

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

3647
  code = metaDropIndexFromSuperTable(pVnode->pMeta, ver, &req);
14,738✔
3648
  if (code) {
14,738✔
3649
    pRsp->code = code;
×
3650
    return code;
×
3651
  }
3652
  return TSDB_CODE_SUCCESS;
14,738✔
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) {
69,473✔
3658
  if (!pVnode->restored) {
69,473✔
3659
    vInfo("vgId:%d, ignore compact req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
3,221✔
3660
    return 0;
3,221✔
3661
  }
3662
  return vnodeAsyncCompact(pVnode, ver, pReq, len, pRsp);
66,252✔
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) {
44,887✔
3679
  SSyncState syncState = syncGetState(pVnode->sync);
44,887✔
3680
  if (syncState.state != TAOS_SYNC_STATE_LEADER) {
44,887✔
3681
    return terrno = TSDB_CODE_SYN_NOT_LEADER;
13,521✔
3682
  }
3683
  return 0;
31,366✔
3684
}
3685

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

3692
  if (strncmp(token, member0Token, TSDB_ARB_TOKEN_SIZE) != 0 &&
31,366✔
3693
      strncmp(token, member1Token, TSDB_ARB_TOKEN_SIZE) != 0) {
30,780✔
3694
    return terrno = TSDB_CODE_MND_ARB_TOKEN_MISMATCH;
29,400✔
3695
  }
3696

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

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

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

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

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

3718
  SVArbCheckSyncReq syncReq = {0};
983✔
3719

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

3725
  vInfo("vgId:%d, start to process vnode-arb-check-sync req QID:0x%" PRIx64 ":0x%" PRIx64 ", seqNum:%" PRIx64
983✔
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;
983✔
3732
  pRsp->code = TSDB_CODE_SUCCESS;
983✔
3733
  pRsp->pCont = NULL;
983✔
3734
  pRsp->contLen = 0;
983✔
3735

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

3742
  if ((syncRsp.errCode = vnodeCheckSyncd(pVnode, syncReq.member0Token, syncReq.member1Token)) != 0) {
983✔
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) {
983✔
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);
983✔
3752
  if (contLen <= 0) {
983✔
3753
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
3754
    code = -1;
×
3755
    goto _OVER;
×
3756
  }
3757
  void *pHead = rpcMallocCont(contLen);
983✔
3758
  if (!pHead) {
983✔
3759
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
3760
    code = -1;
×
3761
    goto _OVER;
×
3762
  }
3763

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

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

3774
  vInfo(
983✔
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;
983✔
3780

3781
_OVER:
983✔
3782
  if (code != 0) {
983✔
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);
983✔
3787
  return code;
983✔
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