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

taosdata / TDengine / #5051

13 May 2026 12:00PM UTC coverage: 73.358% (-0.04%) from 73.398%
#5051

push

travis-ci

web-flow
feat: taosdump support stream backup/restore (#35326)

139 of 170 new or added lines in 3 files covered. (81.76%)

714 existing lines in 146 files now uncovered.

281543 of 383795 relevant lines covered (73.36%)

135448694.71 hits per line

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

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

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

37
extern taos_counter_t *tsInsertCounter;
38

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

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

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

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

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

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

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

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

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

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

105
  // name
106
  char *name = NULL;
73,834,167✔
107
  if (tDecodeCStr(pCoder, &name) < 0) {
73,833,714✔
108
    code = TSDB_CODE_INVALID_MSG;
×
109
    TSDB_CHECK_CODE(code, lino, _exit);
×
110
  }
111

112
  // uid
113
  int64_t uid = metaGetTableEntryUidByName(pVnode->pMeta, name);
73,833,714✔
114
  if (uid == 0) {
73,827,925✔
115
    uid = tGenIdPI64();
73,350,095✔
116
  }
117
  taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), uid);
73,811,586✔
118

119
  // btime
120
  taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos + 8), btime);
73,801,797✔
121

122
  tEndDecode(pCoder);
73,806,923✔
123

124
_exit:
73,821,345✔
125
  if (code) {
73,822,024✔
126
    vError("vgId:%d %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
127
  } else {
128
    vTrace("vgId:%d %s done, table:%s uid generated:%" PRId64, TD_VID(pVnode), __func__, name, uid);
73,822,024✔
129
    if (pUid) *pUid = uid;
73,801,332✔
130
  }
131
  return code;
73,801,228✔
132
}
133
static int32_t vnodePreProcessCreateTableMsg(SVnode *pVnode, SRpcMsg *pMsg) {
49,222,687✔
134
  int32_t code = 0;
49,222,687✔
135
  int32_t lino = 0;
49,222,687✔
136

137
  int64_t  btime = taosGetTimestampMs();
49,223,875✔
138
  SDecoder dc = {0};
49,223,875✔
139
  int32_t  nReqs;
49,221,190✔
140

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

147
  if (tDecodeI32v(&dc, &nReqs) < 0) {
49,223,091✔
148
    code = TSDB_CODE_INVALID_MSG;
×
149
    TSDB_CHECK_CODE(code, lino, _exit);
×
150
  }
151
  for (int32_t iReq = 0; iReq < nReqs; iReq++) {
104,845,030✔
152
    code = vnodePreprocessCreateTableReq(pVnode, &dc, btime, NULL);
55,642,228✔
153
    TSDB_CHECK_CODE(code, lino, _exit);
55,621,939✔
154
  }
155

156
  tEndDecode(&dc);
49,202,802✔
157

158
_exit:
49,220,200✔
159
  tDecoderClear(&dc);
49,220,200✔
160
  if (code) {
49,220,360✔
161
    vError("vgId:%d, %s:%d failed to preprocess submit request since %s, msg type:%s", TD_VID(pVnode), __func__, lino,
×
162
           tstrerror(code), TMSG_INFO(pMsg->msgType));
163
  }
164
  return code;
49,217,964✔
165
}
166

167
static int32_t vnodePreProcessAlterTableMsg(SVnode *pVnode, SRpcMsg *pMsg) {
13,825,889✔
168
  int32_t code = TSDB_CODE_INVALID_MSG;
13,825,889✔
169
  int32_t lino = 0;
13,825,889✔
170

171
  if (pVnode->config.isAudit) {
13,825,889✔
172
    return TSDB_CODE_PAR_PERMISSION_DENIED;
×
173
  }
174

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

178
  SVAlterTbReq vAlterTbReq = {0};
13,825,889✔
179
  int64_t      ctimeMs = taosGetTimestampMs();
13,825,889✔
180
  code = tDecodeSVAlterTbReqSetCtime(&dc, &vAlterTbReq, ctimeMs);
13,825,889✔
181

182
  destroyAlterTbReq(&vAlterTbReq);
13,825,889✔
183

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

195
static int32_t vnodePreProcessDropTtlMsg(SVnode *pVnode, SRpcMsg *pMsg) {
14,940,823✔
196
  int32_t code = TSDB_CODE_INVALID_MSG;
14,940,823✔
197
  int32_t lino = 0;
14,940,823✔
198

199
  SMsgHead *pContOld = pMsg->pCont;
14,940,823✔
200
  int32_t   reqLenOld = pMsg->contLen - sizeof(SMsgHead);
14,946,108✔
201

202
  SArray *tbUids = NULL;
14,947,411✔
203
  int64_t timestampMs = 0;
14,947,411✔
204

205
  SVDropTtlTableReq ttlReq = {0};
14,947,411✔
206
  if (tDeserializeSVDropTtlTableReq((char *)pContOld + sizeof(SMsgHead), reqLenOld, &ttlReq) != 0) {
14,946,939✔
207
    code = TSDB_CODE_INVALID_MSG;
×
208
    TSDB_CHECK_CODE(code, lino, _exit);
×
209
  }
210

211
  {  // find expired uids
212
    tbUids = taosArrayInit(8, sizeof(tb_uid_t));
14,945,943✔
213
    if (tbUids == NULL) {
14,943,866✔
214
      code = terrno;
×
215
      TSDB_CHECK_CODE(code, lino, _exit);
×
216
    }
217

218
    timestampMs = (int64_t)ttlReq.timestampSec * 1000;
14,943,866✔
219
    code = metaTtlFindExpired(pVnode->pMeta, timestampMs, tbUids, ttlReq.ttlDropMaxCount);
14,943,866✔
220
    if (code != 0) {
14,933,399✔
221
      code = TSDB_CODE_INVALID_MSG;
×
222
      TSDB_CHECK_CODE(code, lino, _exit);
×
223
    }
224

225
    ttlReq.nUids = taosArrayGetSize(tbUids);
14,933,399✔
226
    ttlReq.pTbUids = tbUids;
14,936,446✔
227
  }
228

229
  if (ttlReq.nUids == 0) {
14,936,446✔
230
    code = TSDB_CODE_MSG_PREPROCESSED;
14,915,843✔
231
    TSDB_CHECK_CODE(code, lino, _exit);
14,915,843✔
232
  }
233

234
  {  // prepare new content
235
    int32_t reqLenNew = tSerializeSVDropTtlTableReq(NULL, 0, &ttlReq);
20,769✔
236
    int32_t contLenNew = reqLenNew + sizeof(SMsgHead);
13,386✔
237

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

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

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

255
  code = 0;
13,386✔
256

257
_exit:
14,929,063✔
258
  taosArrayDestroy(tbUids);
14,937,415✔
259

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

267
  return code;
14,923,878✔
268
}
269

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

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

280
  SSubmitTbData submitTbData;
830,910,531✔
281
  uint8_t       version;
282
  if (tDecodeI32v(pCoder, &submitTbData.flags) < 0) {
830,911,814✔
283
    code = TSDB_CODE_INVALID_MSG;
×
284
    TSDB_CHECK_CODE(code, lino, _exit);
×
285
  }
286
  version = (submitTbData.flags >> 8) & 0xff;
830,911,814✔
287
  submitTbData.flags = submitTbData.flags & 0xff;
830,911,814✔
288

289
  int64_t uid;
830,907,039✔
290
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
830,912,352✔
291
    code = vnodePreprocessCreateTableReq(pVnode, pCoder, btimeMs, &uid);
18,186,301✔
292
    TSDB_CHECK_CODE(code, lino, _exit);
18,182,742✔
293
  }
294

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

301
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
830,916,339✔
302
    taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), uid);
18,185,169✔
303
    pCoder->pos += sizeof(int64_t);
18,181,335✔
304
  } else {
305
    if (tDecodeI64(pCoder, &submitTbData.uid) < 0) {
812,727,429✔
306
      code = TSDB_CODE_INVALID_MSG;
×
307
      TSDB_CHECK_CODE(code, lino, _exit);
×
308
    }
309
  }
310

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

316
  // scan and check
317
  TSKEY now = btimeMs;
830,902,836✔
318
  if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_MICRO) {
830,902,836✔
319
    now *= 1000;
2,936,170✔
320
  } else if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_NANO) {
827,969,606✔
321
    now *= 1000000;
1,722,239✔
322
  }
323

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

326
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * keep;
830,898,783✔
327
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
830,907,882✔
328
  if (submitTbData.flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
830,900,790✔
329
    uint64_t nColData;
7,557,428✔
330
    if (tDecodeU64v(pCoder, &nColData) < 0) {
7,561,189✔
331
      code = TSDB_CODE_INVALID_MSG;
×
332
      TSDB_CHECK_CODE(code, lino, _exit);
×
333
    }
334

335
    SColData colData = {0};
7,561,189✔
336
    code = tDecodeColData(version, pCoder, &colData, false);
7,559,656✔
337
    if (code) {
7,558,601✔
338
      code = TSDB_CODE_INVALID_MSG;
×
339
      TSDB_CHECK_CODE(code, lino, _exit);
×
340
    }
341

342
    if (colData.flag != HAS_VALUE) {
7,558,601✔
343
      code = TSDB_CODE_INVALID_MSG;
×
344
      TSDB_CHECK_CODE(code, lino, _exit);
×
345
    }
346

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

354
    for (uint64_t i = 1; i < nColData; i++) {
23,058,358✔
355
      code = tDecodeColData(version, pCoder, &colData, true);
15,935,937✔
356
      if (code) {
15,937,423✔
357
        code = TSDB_CODE_INVALID_MSG;
×
358
        TSDB_CHECK_CODE(code, lino, _exit);
×
359
      }
360
    }
361
  } else {
362
    uint64_t nRow;
823,338,587✔
363
    if (tDecodeU64v(pCoder, &nRow) < 0) {
823,340,078✔
364
      code = TSDB_CODE_INVALID_MSG;
×
365
      TSDB_CHECK_CODE(code, lino, _exit);
×
366
    }
367

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

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

389
  if (!tDecodeIsEnd(pCoder)) {
830,825,066✔
390
    taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), ctimeMs);
830,822,747✔
391
    pCoder->pos += sizeof(int64_t);
830,810,265✔
392
  }
393

394
  tEndDecode(pCoder);
830,821,166✔
395

396
_exit:
830,911,938✔
397
  if (code) {
830,897,313✔
398
    vError("vgId:%d, %s:%d failed to vnodePreProcessSubmitTbData submit request since %s", TD_VID(pVnode), __func__,
76,164✔
399
           lino, tstrerror(code));
400
  }
401
  return code;
830,897,313✔
402
}
403
static int32_t vnodePreProcessSubmitMsg(SVnode *pVnode, SRpcMsg *pMsg) {
753,542,393✔
404
  int32_t code = 0;
753,542,393✔
405
  int32_t lino = 0;
753,542,393✔
406

407
  if (tsBypassFlag & TSDB_BYPASS_RA_RPC_RECV_SUBMIT) {
753,542,393✔
408
    return TSDB_CODE_MSG_PREPROCESSED;
101✔
409
  }
410

411
  SDecoder *pCoder = &(SDecoder){0};
753,542,292✔
412

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

418
  tDecoderInit(pCoder, (uint8_t *)pMsg->pCont + sizeof(SSubmitReq2Msg), pMsg->contLen - sizeof(SSubmitReq2Msg));
753,545,374✔
419

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

425
  uint64_t nSubmitTbData;
753,540,096✔
426
  if (tDecodeU64v(pCoder, &nSubmitTbData) < 0) {
753,542,408✔
427
    code = TSDB_CODE_INVALID_MSG;
×
428
    TSDB_CHECK_CODE(code, lino, _exit);
×
429
  }
430

431
  int64_t btimeMs = taosGetTimestampMs();
753,544,674✔
432
  int64_t ctimeMs = btimeMs;
753,544,674✔
433
  for (uint64_t i = 0; i < nSubmitTbData; i++) {
1,584,363,361✔
434
    code = vnodePreProcessSubmitTbData(pVnode, pCoder, btimeMs, ctimeMs);
830,908,784✔
435
    TSDB_CHECK_CODE(code, lino, _exit);
830,894,851✔
436
  }
437

438
  tEndDecode(pCoder);
753,454,577✔
439

440
_exit:
753,531,639✔
441
  tDecoderClear(pCoder);
753,543,041✔
442
  if (code) {
753,536,941✔
443
    vError("vgId:%d, %s:%d failed to preprocess submit request since %s, msg type:%s", TD_VID(pVnode), __func__, lino,
76,164✔
444
           tstrerror(code), TMSG_INFO(pMsg->msgType));
445
  }
446
  return code;
753,528,959✔
447
}
448

449
static int32_t vnodePreProcessDeleteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
2,024,083✔
450
  int32_t code = 0;
2,024,083✔
451

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

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

464
  code = qWorkerProcessDeleteMsg(&handle, pVnode->pQuery, pMsg, &res);
2,024,083✔
465
  if (code) goto _exit;
2,024,083✔
466

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

472
  ((SMsgHead *)pCont)->contLen = size + sizeof(SMsgHead);
2,022,805✔
473
  ((SMsgHead *)pCont)->vgId = TD_VID(pVnode);
2,022,805✔
474

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

481
  rpcFreeCont(pMsg->pCont);
2,023,444✔
482
  pMsg->pCont = pCont;
2,023,444✔
483
  pMsg->contLen = size + sizeof(SMsgHead);
2,023,444✔
484

485
  taosArrayDestroy(res.uidList);
2,022,805✔
486

487
_exit:
2,024,083✔
488
  return code;
2,022,805✔
489
}
490

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

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

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

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

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

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

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

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

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

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

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

538
  return code;
32,843✔
539
}
540

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

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

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

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

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

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

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

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

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

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

615
_exit:
4,130✔
616
  return code;
4,130✔
617
}
618

619
int32_t vnodePreProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
862,952,807✔
620
  int32_t code = 0;
862,952,807✔
621

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

628
  switch (pMsg->msgType) {
862,950,412✔
629
    case TDMT_VND_CREATE_TABLE: {
49,223,875✔
630
      code = vnodePreProcessCreateTableMsg(pVnode, pMsg);
49,223,875✔
631
    } break;
49,220,550✔
632
    case TDMT_VND_ALTER_TABLE: {
13,825,889✔
633
      code = vnodePreProcessAlterTableMsg(pVnode, pMsg);
13,825,889✔
634
    } break;
13,825,889✔
635
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
14,946,816✔
636
    case TDMT_VND_DROP_TTL_TABLE: {
637
      code = vnodePreProcessDropTtlMsg(pVnode, pMsg);
14,946,816✔
638
    } break;
14,928,424✔
639
    case TDMT_VND_SUBMIT: {
753,544,660✔
640
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.preprocess_time, METRIC_LEVEL_LOW,
753,544,660✔
641
                           { code = vnodePreProcessSubmitMsg(pVnode, pMsg); });
642
    } break;
753,535,063✔
643
    case TDMT_VND_DELETE: {
2,024,083✔
644
      code = vnodePreProcessDeleteMsg(pVnode, pMsg);
2,024,083✔
645
    } break;
2,022,805✔
646
    case TDMT_VND_BATCH_DEL: {
×
647
      code = vnodePreProcessBatchDeleteMsg(pVnode, pMsg);
×
648
    } break;
×
649
    case TDMT_VND_ARB_CHECK_SYNC: {
64,441✔
650
      code = vnodePreProcessArbCheckSyncMsg(pVnode, pMsg);
64,441✔
651
    } break;
64,441✔
652
    case TDMT_VND_DROP_TABLE: {
1,898,282✔
653
      code = vnodePreProcessDropTbMsg(pVnode, pMsg);
1,898,282✔
654
    } break;
1,898,282✔
655
#ifdef TD_ENTERPRISE
656
    case TDMT_VND_SSMIGRATE_FILESET: {
4,130✔
657
      code = vnodePreProcessSsMigrateFileSetReq(pVnode, pMsg);
4,130✔
658
    } break;
4,130✔
659
#endif
660
    case TDMT_VND_AUDIT_RECORD: {
514✔
661
      vTrace("vgId:%d, pre process TDMT_VND_AUDIT_RECORD", TD_VID(pVnode));
514✔
662
    } break;
768✔
663
    default:
27,421,326✔
664
      break;
27,421,326✔
665
  }
666

667
  if (code && code != TSDB_CODE_MSG_PREPROCESSED) {
862,921,678✔
668
    vError("vgId:%d, failed to preprocess write request since %s, msg type:%s", TD_VID(pVnode), tstrerror(code),
108,054✔
669
           TMSG_INFO(pMsg->msgType));
670
  }
671
  return code;
862,918,928✔
672
}
673

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

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

682
  SBseBatch *pBatch = NULL;
51,046✔
683

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

687
  SRow  **pRow = (SRow **)TARRAY_DATA(pSubmitTbData->aRowP);
51,046✔
688
  int32_t rowIdx = -1;
51,046✔
689
  for (int32_t i = 0; i < sz; i++) {
28,481,862✔
690
    int64_t     seq = 0;
28,430,816✔
691
    SBlobValue *p = taosArrayGet(pBlobSet->pSeqTable, i);
28,430,816✔
692
    if (p->type == TSDB_DATA_BLOB_EMPTY_VALUE || p->type == TSDB_DATA_BLOB_NULL_VALUE) {
28,430,816✔
693
      // skip empty or null blob
694
      continue;
2,822,813✔
695
    }
696

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

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

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

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

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

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

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

738
  SBseBatch *pBatch = NULL;
412✔
739

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

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

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

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

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

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

796
  return code;
51,458✔
797
}
798

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

806
  (void)taosThreadMutexLock(&pVnode->mutex);
1,398,349,880✔
807
  if (pVnode->disableWrite) {
1,398,411,821✔
808
    (void)taosThreadMutexUnlock(&pVnode->mutex);
×
809
    vError("vgId:%d write is disabled for snapshot, version:%" PRId64, TD_VID(pVnode), ver);
×
810
    return TSDB_CODE_VND_WRITE_DISABLED;
×
811
  }
812
  (void)taosThreadMutexUnlock(&pVnode->mutex);
1,398,313,425✔
813

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

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

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

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

838
  atomic_store_64(&pVnode->state.applied, ver);
1,398,432,827✔
839
  atomic_store_64(&pVnode->state.applyTerm, pMsg->info.conn.applyTerm);
1,398,434,342✔
840

841
  if (!syncUtilUserCommit(pMsg->msgType)) goto _exit;
1,398,433,318✔
842

843
  // skip header
844
  pReq = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
1,386,582,186✔
845
  len = pMsg->contLen - sizeof(SMsgHead);
1,386,583,550✔
846
  bool needCommit = false;
1,386,581,264✔
847
  bool forceTrimWal = false;
1,386,581,264✔
848

849
  switch (pMsg->msgType) {
1,386,581,264✔
850
    /* META */
851
    case TDMT_VND_CREATE_STB:
6,645,030✔
852
      code = vnodeProcessCreateStbReq(pVnode, ver, pReq, len, pRsp);
6,645,030✔
853
      TSDB_CHECK_CODE(code, lino, _err);
6,645,677✔
854
      break;
6,643,118✔
855
    case TDMT_VND_ALTER_STB:
8,757,488✔
856
      code = vnodeProcessAlterStbReq(pVnode, ver, pReq, len, pRsp);
8,757,488✔
857
      TSDB_CHECK_CODE(code, lino, _err);
8,757,488✔
858
      break;
8,757,488✔
859
    case TDMT_VND_DROP_STB:
1,075,544✔
860
      code = vnodeProcessDropStbReq(pVnode, ver, pReq, len, pRsp, pMsg);
1,075,544✔
861
      TSDB_CHECK_CODE(code, lino, _err);
1,075,544✔
862
      break;
1,075,544✔
863
    case TDMT_VND_CREATE_TABLE:
52,015,036✔
864
      code = vnodeProcessCreateTbReq(pVnode, ver, pReq, len, pRsp, pMsg);
52,015,036✔
865
      TSDB_CHECK_CODE(code, lino, _err);
52,008,683✔
866
      break;
52,008,683✔
867
    case TDMT_VND_ALTER_TABLE:
13,827,154✔
868
      code = vnodeProcessAlterTbReq(pVnode, ver, pReq, len, pRsp);
13,827,154✔
869
      TSDB_CHECK_CODE(code, lino, _err);
13,827,154✔
870
      break;
13,827,154✔
871
    case TDMT_VND_DROP_TABLE:
1,898,383✔
872
    case TDMT_VND_SNODE_DROP_TABLE:
873
      code = vnodeProcessDropTbReq(pVnode, ver, pReq, len, pRsp, pMsg);
1,898,383✔
874
      TSDB_CHECK_CODE(code, lino, _err);
1,898,383✔
875
      break;
1,898,383✔
876
    case TDMT_VND_CREATE_RSMA:
44,426✔
877
      code = vnodeProcessCreateRsmaReq(pVnode, ver, pReq, len, pRsp);
44,426✔
878
      TSDB_CHECK_CODE(code, lino, _err);
44,426✔
879
      break;
44,426✔
880
    case TDMT_VND_DROP_RSMA:
22,208✔
881
      code = vnodeProcessDropRsmaReq(pVnode, ver, pReq, len, pRsp);
22,208✔
882
      TSDB_CHECK_CODE(code, lino, _err);
22,208✔
883
      break;
22,208✔
884
    case TDMT_VND_ALTER_RSMA:
13,582✔
885
      code = vnodeProcessAlterRsmaReq(pVnode, ver, pReq, len, pRsp);
13,582✔
886
      TSDB_CHECK_CODE(code, lino, _err);
13,582✔
887
      break;
13,582✔
888
    case TDMT_VND_DROP_TTL_TABLE:
×
889
      code = vnodeProcessDropTtlTbReq(pVnode, ver, pReq, len, pRsp);
×
890
      TSDB_CHECK_CODE(code, lino, _err);
×
891
      break;
×
892
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
13,386✔
893
      code = vnodeProcessFetchTtlExpiredTbs(pVnode, ver, pReq, len, pRsp);
13,386✔
894
      TSDB_CHECK_CODE(code, lino, _err);
13,386✔
895
      break;
13,386✔
896
    case TDMT_VND_TRIM:
91,203✔
897
      code = vnodeProcessTrimReq(pVnode, ver, pReq, len, pRsp);
91,203✔
898
      TSDB_CHECK_CODE(code, lino, _err);
91,203✔
899
      break;
91,203✔
900
      case TDMT_VND_TRIM_WAL:
7,668✔
901
        needCommit = true;
7,668✔
902
        forceTrimWal = true;
7,668✔
903
        break;
7,668✔
904
#ifdef TD_ENTERPRISE
905
    case TDMT_VND_SSMIGRATE_FILESET:
4,130✔
906
      if (vnodeProcessSsMigrateFileSetReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
4,130✔
907
      break;
4,130✔
908
    case TDMT_VND_FOLLOWER_SSMIGRATE:
7,434✔
909
      if (vnodeProcessFollowerSsMigrateReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
7,434✔
910
      break;
7,434✔
911
    case TDMT_VND_KILL_SSMIGRATE:
×
912
      if (vnodeProcessKillSsMigrateReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
913
      break;
×
914
#endif
915

916
    /* TSDB */
917
    case TDMT_VND_SUBMIT: {
1,273,439,276✔
918
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.apply_time, METRIC_LEVEL_LOW, {
1,273,439,276✔
919
        if (vnodeProcessSubmitReq(pVnode, ver, pMsg->pCont, pMsg->contLen, pRsp, pMsg) < 0) goto _err;
920
      });
921
      METRICS_UPDATE(pVnode->writeMetrics.apply_bytes, METRIC_LEVEL_LOW, (int64_t)pMsg->contLen);
1,273,409,259✔
922
      break;
1,273,406,946✔
923
    }
924
    case TDMT_VND_DELETE:
2,481,893✔
925
      code = vnodeProcessDeleteReq(pVnode, ver, pReq, len, pRsp, pMsg);
2,481,893✔
926
      TSDB_CHECK_CODE(code, lino, _err);
2,481,893✔
927
      break;
2,481,893✔
928
    case TDMT_VND_BATCH_DEL:
×
929
      code = vnodeProcessBatchDeleteReq(pVnode, ver, pReq, len, pRsp);
×
930
      TSDB_CHECK_CODE(code, lino, _err);
×
931
      break;
×
932
      /* TQ */
933
#if defined(USE_TQ) || defined(USE_STREAM)
934
    case TDMT_VND_TMQ_SUBSCRIBE:
1,490,356✔
935
      code = tqProcessSubscribeReq(pVnode->pTq, ver, pReq, len);
1,490,356✔
936
      TSDB_CHECK_CODE(code, lino, _err);
1,482,736✔
937
      break;
1,482,736✔
938
    case TDMT_VND_TMQ_DELETE_SUB:
349,651✔
939
      code = tqProcessDeleteSubReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen);
349,651✔
940
      TSDB_CHECK_CODE(code, lino, _err);
348,822✔
941
      break;
348,822✔
942
    case TDMT_VND_TMQ_COMMIT_OFFSET:
13,563,535✔
943
      code = tqProcessOffsetCommitReq(pVnode->pTq, ver, pReq, len);
13,563,535✔
944
      TSDB_CHECK_CODE(code, lino, _err);
13,562,367✔
945
      break;
13,562,367✔
946
#endif
947
    case TDMT_VND_ALTER_CONFIRM:
4,344,707✔
948
      needCommit = pVnode->config.hashChange;
4,344,707✔
949
      code = vnodeProcessAlterConfirmReq(pVnode, ver, pReq, len, pRsp);
4,344,707✔
950
      TSDB_CHECK_CODE(code, lino, _err);
4,344,707✔
951
      break;
4,344,707✔
952
    case TDMT_VND_ALTER_CONFIG:
375,266✔
953
      vnodeProcessAlterConfigReq(pVnode, ver, pReq, len, pRsp);
375,266✔
954
      break;
375,266✔
955
    case TDMT_VND_COMMIT:
5,988,739✔
956
      needCommit = true;
5,988,739✔
957
      break;
5,988,739✔
958
    case TDMT_VND_CREATE_INDEX:
21,174✔
959
      vnodeProcessCreateIndexReq(pVnode, ver, pReq, len, pRsp);
21,174✔
960
      break;
21,174✔
961
    case TDMT_VND_DROP_INDEX:
17,228✔
962
      vnodeProcessDropIndexReq(pVnode, ver, pReq, len, pRsp);
17,228✔
963
      break;
17,228✔
964
    case TDMT_VND_COMPACT:
82,445✔
965
      vnodeProcessCompactVnodeReq(pVnode, ver, pReq, len, pRsp);
82,445✔
966
      goto _exit;
82,445✔
967
    case TDMT_VND_SCAN:
796✔
968
      vnodeProcessScanVnodeReq(pVnode, ver, pReq, len, pRsp);
796✔
969
      goto _exit;
796✔
970
    case TDMT_SYNC_CONFIG_CHANGE:
×
971
      vnodeProcessConfigChangeReq(pVnode, ver, pReq, len, pRsp);
×
972
      break;
×
973
#ifdef TD_ENTERPRISE
974
    case TDMT_VND_KILL_COMPACT:
1,706✔
975
      vnodeProcessKillCompactReq(pVnode, ver, pReq, len, pRsp);
1,706✔
976
      break;
1,706✔
977
#endif
978
    case TDMT_VND_KILL_SCAN:
168✔
979
      vnodeProcessKillScanReq(pVnode, ver, pReq, len, pRsp);
168✔
980
      break;
168✔
981
    case TDMT_VND_KILL_TRIM:
×
982
      vnodeProcessKillRetentionReq(pVnode, ver, pReq, len, pRsp);
×
983
      break;
×
984
    /* ARB */
985
    case TDMT_VND_ARB_CHECK_SYNC:
1,961✔
986
      vnodeProcessArbCheckSyncReq(pVnode, pReq, len, pRsp);
1,961✔
987
      break;
1,961✔
988
    case TDMT_VND_AUDIT_RECORD:
768✔
989
      vTrace("vgId:%d, processed audit msg", TD_VID(pVnode));
768✔
990
      code = vnodeProcessAuditRecordReq(pVnode, ver, pReq, len, pMsg);
768✔
991
      pRsp->code = code;
768✔
992
      pRsp->msgType = TDMT_VND_AUDIT_RECORD_RSP;
768✔
993
      pRsp->pCont = NULL;
768✔
994
      pRsp->contLen = 0;
768✔
995
      if (code) {
768✔
996
        goto _err;
×
997
      }
998
      break;
768✔
999
    default:
×
1000
      vError("vgId:%d, unprocessed msg, %d", TD_VID(pVnode), pMsg->msgType);
×
1001
      return TSDB_CODE_INVALID_MSG;
×
1002
  }
1003

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

1006
  walApplyVer(pVnode->pWal, ver);
1,386,477,896✔
1007

1008
  // commit if need
1009
  if (needCommit) {
1,386,493,615✔
1010
    vInfo("vgId:%d, commit at version %" PRId64, TD_VID(pVnode), ver);
6,024,408✔
1011
    code = vnodeAsyncCommit(pVnode, forceTrimWal);
6,025,462✔
1012
    if (code) {
6,025,182✔
1013
      vError("vgId:%d, failed to vnode async commit since %s.", TD_VID(pVnode), tstrerror(terrno));
×
1014
      goto _err;
×
1015
    }
1016

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

1027
_exit:
1,386,491,272✔
1028
  return 0;
1,398,425,638✔
1029

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

1039
  return code;
6,404✔
1040
}
1041

1042
int32_t vnodePreprocessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg) {
800,010,781✔
1043
  if (TDMT_SCH_QUERY != pMsg->msgType && TDMT_SCH_MERGE_QUERY != pMsg->msgType) {
800,010,781✔
1044
    return 0;
392,091,487✔
1045
  }
1046

1047
  int32_t qType = 0;
407,984,314✔
1048
  return qWorkerPreprocessQueryMsg(pVnode->pQuery, pMsg, (TDMT_SCH_QUERY == pMsg->msgType), &qType);
407,984,552✔
1049
}
1050

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

1056
  if (pMsg->msgType == TDMT_VND_TMQ_CONSUME && !syncIsReadyForRead(pVnode->sync)) {
800,157,281✔
1057
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
719,784✔
1058
    return 0;
719,784✔
1059
  }
1060

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

1066
  SReadHandle handle = {0};
799,439,859✔
1067
  handle.vnode = pVnode;
799,441,062✔
1068
  handle.pMsgCb = &pVnode->msgCb;
799,441,062✔
1069
  handle.pWorkerCb = pInfo->workerCb;
799,405,142✔
1070
  initStorageAPI(&handle.api);
799,402,166✔
1071
  int32_t code = TSDB_CODE_SUCCESS;
799,323,582✔
1072
  bool    redirected = false;
799,323,582✔
1073

1074
  switch (pMsg->msgType) {
799,323,582✔
1075
    case TDMT_SCH_QUERY:
339,032,767✔
1076
      if (!syncIsReadyForRead(pVnode->sync)) {
339,032,767✔
1077
        pMsg->code = (terrno) ? terrno : TSDB_CODE_SYN_NOT_LEADER;
2,583,627✔
1078
        redirected = true;
2,583,627✔
1079
      }
1080
      code = qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
339,061,503✔
1081
      if (redirected) {
339,035,480✔
1082
        vnodeRedirectRpcMsg(pVnode, pMsg, pMsg->code);
2,583,627✔
1083
        return 0;
2,583,627✔
1084
      }
1085

1086
      return code;
336,451,853✔
1087
    case TDMT_SCH_MERGE_QUERY:
68,958,593✔
1088
      return qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
68,958,593✔
1089
    case TDMT_SCH_QUERY_CONTINUE:
57,539,487✔
1090
      return qWorkerProcessCQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
57,539,487✔
1091
    case TDMT_VND_TMQ_CONSUME:
333,832,775✔
1092
      return tqProcessPollReq(pVnode->pTq, pMsg);
333,832,775✔
1093
    default:
×
1094
      vError("unknown msg type:%d in query queue", pMsg->msgType);
×
1095
      return TSDB_CODE_APP_ERROR;
×
1096
  }
1097
}
1098

1099
int32_t vnodeProcessFetchMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
1,216,491,255✔
1100
  int32_t code = TSDB_CODE_SUCCESS;
1,216,491,255✔
1101
  const STraceId *trace = &pMsg->info.traceId;
1,216,491,255✔
1102
  vDebug("vgId:%d, msg:%s, %p in fetch queue is processing, %" PRIx64 ":%" PRIx64, 
1,216,547,002✔
1103
    pVnode->config.vgId, TMSG_INFO(pMsg->msgType), pMsg, TRACE_GET_ROOTID(trace), TRACE_GET_MSGID(trace));
1104
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
1,216,662,628✔
1105
       pMsg->msgType == TDMT_VND_BATCH_META || pMsg->msgType == TDMT_VND_TABLE_NAME ||
836,884,432✔
1106
       pMsg->msgType == TDMT_VND_VSUBTABLES_META || pMsg->msgType == TDMT_VND_VSTB_REF_DBS) &&
804,662,763✔
1107
      !syncIsReadyForRead(pVnode->sync)) {
412,001,160✔
1108
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
349,396✔
1109
    return 0;
349,396✔
1110
  }
1111

1112
  switch (pMsg->msgType) {
1,216,349,373✔
1113
    case TDMT_SCH_FETCH:
562,728,994✔
1114
    case TDMT_SCH_MERGE_FETCH:
1115
      return qWorkerProcessFetchMsg(pVnode, pVnode->pQuery, pMsg, 0);
562,728,994✔
1116
    case TDMT_SCH_FETCH_RSP:
×
1117
      return qWorkerProcessRspMsg(pVnode, pVnode->pQuery, pMsg, 0);
×
1118
    // case TDMT_SCH_CANCEL_TASK:
1119
    //   return qWorkerProcessCancelMsg(pVnode, pVnode->pQuery, pMsg, 0);
1120
    case TDMT_SCH_DROP_TASK:
407,702,558✔
1121
      return qWorkerProcessDropMsg(pVnode, pVnode->pQuery, pMsg, 0);
407,702,558✔
1122
    case TDMT_SCH_TASK_NOTIFY:
39,496✔
1123
      return qWorkerProcessNotifyMsg(pVnode, pVnode->pQuery, pMsg, 0);
39,496✔
1124
    case TDMT_SCH_QUERY_HEARTBEAT:
212,347,405✔
1125
      return qWorkerProcessHbMsg(pVnode, pVnode->pQuery, pMsg, 0);
212,347,405✔
1126
    case TDMT_VND_TABLE_META:
1,465,068✔
1127
    case TDMT_VND_TABLE_NAME:
1128
      return vnodeGetTableMeta(pVnode, pMsg, true);
1,465,068✔
1129
    case TDMT_VND_TABLE_CFG:
×
1130
      return vnodeGetTableCfg(pVnode, pMsg, true);
×
1131
    case TDMT_VND_BATCH_META: {
31,869,300✔
1132
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.fetch_batch_meta_time, METRIC_LEVEL_LOW,
31,869,300✔
1133
                           { code = vnodeGetBatchMeta(pVnode, pMsg); });
1134
      METRICS_UPDATE(pVnode->writeMetrics.fetch_batch_meta_count, METRIC_LEVEL_LOW, 1);
31,874,698✔
1135
      return code;
31,868,021✔
1136
    }
1137
    case TDMT_VND_VSUBTABLES_META:
×
1138
      return vnodeGetVSubtablesMeta(pVnode, pMsg);
×
1139
    case TDMT_VND_VSTB_REF_DBS:
×
1140
      return vnodeGetVStbRefDbs(pVnode, pMsg);
×
1141
    case TDMT_VND_QUERY_SCAN_PROGRESS:
672✔
1142
      return vnodeQueryScanProgress(pVnode, pMsg);
672✔
1143
#ifdef TD_ENTERPRISE
1144
    case TDMT_VND_QUERY_COMPACT_PROGRESS:
×
1145
      return vnodeQueryCompactProgress(pVnode, pMsg);
×
1146

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

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

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

1174
  tstrncpy(pMetaRsp->dbFName, pVnode->config.dbname, TSDB_DB_FNAME_LEN);
83,071,798✔
1175
  pMetaRsp->dbId = pVnode->config.dbId;
83,072,849✔
1176
  pMetaRsp->vgId = TD_VID(pVnode);
83,071,454✔
1177
  pMetaRsp->precision = pVnode->config.tsdbCfg.precision;
83,073,408✔
1178
}
1179

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

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

1188
  int32_t     code = 0;
87,176✔
1189
  SVTrimDbReq trimReq = {0};
87,176✔
1190

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

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

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

1202
_exit:
87,176✔
1203
  return code;
87,176✔
1204
}
1205

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1372
    expiredTb.name = p;
55,022✔
1373
    if (mr.me.type == TSDB_CHILD_TABLE) {
55,022✔
1374
      expiredTb.suid = mr.me.ctbEntry.suid;
35,752✔
1375
    }
1376

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

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

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

1404
static int32_t vnodeProcessCreateStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
6,639,511✔
1405
  int32_t        code = 0;
6,639,511✔
1406
  SVCreateStbReq req = {0};
6,639,511✔
1407
  SDecoder       coder;
6,638,804✔
1408

1409
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
6,645,845✔
1410
  pRsp->code = TSDB_CODE_SUCCESS;
6,645,845✔
1411
  pRsp->pCont = NULL;
6,645,845✔
1412
  pRsp->contLen = 0;
6,644,327✔
1413

1414
  // decode and process req
1415
  tDecoderInit(&coder, pReq, len);
6,644,327✔
1416

1417
  code = tDecodeSVCreateStbReq(&coder, &req);
6,644,826✔
1418
  if (code) {
6,628,284✔
1419
    pRsp->code = code;
×
1420
    goto _err;
×
1421
  }
1422

1423
  code = metaCreateSuperTable(pVnode->pMeta, ver, &req);
6,628,284✔
1424
  if (code) {
6,645,845✔
1425
    pRsp->code = code;
2,559✔
1426
    goto _err;
2,559✔
1427
  }
1428

1429
  tDecoderClear(&coder);
6,643,286✔
1430
  return 0;
6,643,118✔
1431

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

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

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

1463
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(cRsp));
52,011,360✔
1464
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
52,001,463✔
1465
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
52,008,489✔
1466
  if (rsp.pArray == NULL || tbUids == NULL || tbNames == NULL) {
52,006,358✔
1467
    rcode = -1;
35✔
1468
    terrno = TSDB_CODE_OUT_OF_MEMORY;
35✔
1469
    goto _exit;
×
1470
  }
1471

1472
  // loop to create table
1473
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
113,063,356✔
1474
    pCreateReq = req.pReqs + iReq;
61,052,628✔
1475
    memset(&cRsp, 0, sizeof(cRsp));
61,048,312✔
1476

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

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

1505
    // do create table
1506
    if (metaCreateTable2(pVnode->pMeta, ver, pCreateReq, &cRsp.pMeta) < 0) {
61,034,451✔
1507
      if (pCreateReq->flags & TD_CREATE_IF_NOT_EXISTS && terrno == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
20,343✔
1508
        cRsp.code = TSDB_CODE_SUCCESS;
6,667✔
1509
      } else {
1510
        cRsp.code = terrno;
13,676✔
1511
      }
1512
    } else {
1513
      cRsp.code = TSDB_CODE_SUCCESS;
61,034,638✔
1514
      if (taosArrayPush(tbUids, &pCreateReq->uid) == NULL) {
122,069,463✔
1515
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1516
        rcode = -1;
×
1517
        goto _exit;
×
1518
      }
1519
      vnodeUpdateMetaRsp(pVnode, cRsp.pMeta);
61,034,825✔
1520
    }
1521

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

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

1534
  // prepare rsp
1535
  int32_t ret = 0;
52,015,245✔
1536
  tEncodeSize(tEncodeSVCreateTbBatchRsp, &rsp, pRsp->contLen, ret);
52,015,245✔
1537
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
52,011,441✔
1538
  if (pRsp->pCont == NULL) {
52,002,089✔
1539
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1540
    rcode = -1;
×
1541
    goto _exit;
×
1542
  }
1543
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
52,000,955✔
1544
  if (tEncodeSVCreateTbBatchRsp(&encoder, &rsp) != 0) {
52,005,172✔
1545
    vError("vgId:%d, failed to encode create table batch response", TD_VID(pVnode));
×
1546
  }
1547

1548
  if (tsEnableAudit && tsEnableAuditCreateTable && tsAuditLevel >= AUDIT_LEVEL_CHILDTABLE) {
51,991,686✔
1549
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
391✔
1550

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

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

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

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

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

1579
_exit:
51,991,686✔
1580
  tDeleteSVCreateTbBatchReq(&req);
52,004,514✔
1581
  taosArrayDestroyEx(rsp.pArray, tFreeSVCreateTbRsp);
52,002,971✔
1582
  taosArrayDestroy(tbUids);
51,988,228✔
1583
  tDecoderClear(&decoder);
51,992,880✔
1584
  tEncoderClear(&encoder);
52,011,654✔
1585
  taosArrayDestroyP(tbNames, NULL);
52,009,341✔
1586
  return rcode;
52,002,619✔
1587
}
1588

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

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

1599
  tDecoderInit(&dc, pReq, len);
8,757,488✔
1600

1601
  // decode req
1602
  code = tDecodeSVCreateStbReq(&dc, &req);
8,757,488✔
1603
  if (code) {
8,752,409✔
1604
    tDecoderClear(&dc);
×
1605
    return code;
×
1606
  }
1607

1608
  code = metaAlterSuperTable(pVnode->pMeta, ver, &req);
8,752,409✔
1609
  if (code) {
8,757,488✔
1610
    pRsp->code = code;
×
1611
    tDecoderClear(&dc);
×
1612
    return code;
×
1613
  }
1614

1615
  tDecoderClear(&dc);
8,757,488✔
1616

1617
  return 0;
8,757,488✔
1618
}
1619

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

1626
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
1,075,544✔
1627
  pRsp->pCont = NULL;
1,075,544✔
1628
  pRsp->contLen = 0;
1,075,544✔
1629

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

1637
  STraceId* trace = &(pOriginRpc->info.traceId);
1,074,876✔
1638

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

1642
  // process request
1643
  tbUidList = taosArrayInit(8, sizeof(int64_t));
1,075,544✔
1644
  if (tbUidList == NULL) goto _exit;
1,074,810✔
1645
  if (vnodeGetCtbIdList(pVnode, req.suid, tbUidList) != 0){
1,074,810✔
1646
    rcode = terrno;
×
1647
    goto _exit;
×
1648
  }
1649
  if (metaDropSuperTable(pVnode->pMeta, ver, &req) < 0) {
1,073,471✔
1650
    rcode = terrno;
1,706✔
1651
    goto _exit;
1,706✔
1652
  }
1653
  if (tqDeleteTbUidList(pVnode->pTq, tbUidList) < 0) {
1,073,838✔
1654
    rcode = terrno;
×
1655
    goto _exit;
×
1656
  }
1657

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

1668
static SArray* getCidList(const SArray* tags) {
8,689,343✔
1669
  int32_t       code = 0;
8,689,343✔
1670
  int32_t       lino = 0;
8,689,343✔
1671
  SArray* cidList = taosArrayInit(taosArrayGetSize(tags), sizeof(col_id_t));
8,689,343✔
1672
  QUERY_CHECK_NULL(cidList, code, lino, end, terrno);
8,689,343✔
1673
  for (int32_t i = 0; i < taosArrayGetSize(tags); i++) {
17,447,252✔
1674
    SUpdatedTagVal *pTagVal = taosArrayGet(tags, i);
8,757,909✔
1675
    QUERY_CHECK_NULL(pTagVal, code, lino, end, terrno);
8,757,909✔
1676
    col_id_t cid = pTagVal->colId;
8,757,909✔
1677
    QUERY_CHECK_NULL(taosArrayPush(cidList, &cid), code, lino, end, terrno);
8,757,909✔
1678
  }
1679

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

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

1693
  if (tags != NULL){
8,673,551✔
1694
    *cidList = getCidList(tags);
28,262✔
1695
    QUERY_CHECK_NULL(*cidList, code, lino, end, terrno);
28,262✔
1696
  }
1697

1698
  if (tagsArray != NULL) {
8,673,551✔
1699
    *cidListArray = taosArrayInit(taosArrayGetSize(tagsArray), sizeof(SArray*));
8,645,289✔
1700
    QUERY_CHECK_NULL(*cidListArray, code, lino, end, terrno);
8,645,289✔
1701

1702
    for (int32_t i = 0; i < taosArrayGetSize(tagsArray); i++) {
17,306,370✔
1703
      SArray   *obj = taosArrayGetP(tagsArray, i);
8,661,081✔
1704
      cids  = getCidList(obj);
8,661,081✔
1705
      QUERY_CHECK_NULL(cids, code, lino, end, terrno);
8,661,081✔
1706
      QUERY_CHECK_NULL(taosArrayPush(*cidListArray, &cids), code, lino, end, terrno);
17,322,162✔
1707
      cids = NULL;
8,661,081✔
1708
    }
1709
  }
1710

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

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

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

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

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

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

1750
  pRsp->msgType = TDMT_VND_ALTER_TABLE_RSP;
13,827,154✔
1751
  pRsp->pCont = NULL;
13,827,154✔
1752
  pRsp->contLen = 0;
13,827,154✔
1753
  pRsp->code = TSDB_CODE_SUCCESS;
13,827,154✔
1754

1755
  tDecoderInit(&dc, pReq, len);
13,827,154✔
1756

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

1764
  // process
1765
  if (metaAlterTable(pVnode->pMeta, ver, &vAlterTbReq, &vMetaRsp) < 0) {
13,827,154✔
1766
    vAlterTbRsp.code = terrno;
641,970✔
1767
    tDecoderClear(&dc);
641,970✔
1768
    goto _exit;
641,970✔
1769
  }
1770
  tDecoderClear(&dc);
13,185,184✔
1771

1772
  if (NULL != vMetaRsp.pSchemas) {
13,185,184✔
1773
    vnodeUpdateMetaRsp(pVnode, &vMetaRsp);
4,288,086✔
1774
    vAlterTbRsp.pMeta = &vMetaRsp;
4,288,086✔
1775
  }
1776

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

1786
  tEncoderClear(&ec);
13,826,960✔
1787
  if (vMetaRsp.pSchemas) {
13,827,154✔
1788
    taosMemoryFree(vMetaRsp.pSchemas);
4,288,086✔
1789
    taosMemoryFree(vMetaRsp.pSchemaExt);
4,288,086✔
1790
  }
1791
  if (vMetaRsp.pColRefs) {
13,827,154✔
1792
    taosMemoryFree(vMetaRsp.pColRefs);
316,857✔
1793
  }
1794
  return 0;
13,827,154✔
1795
}
1796

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1904
    taosStringBuilderDestroy(&sb);
×
1905
  }
1906

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

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

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

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

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

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

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

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

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

1991
  taosMemoryFreeClear(pSchema);
1992

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

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

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

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

2045
  return TSDB_CODE_SUCCESS;
×
2046
}
2047

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

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

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

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

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

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

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

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

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

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

2117
  return code;
×
2118
}
2119

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

2126
  SSubmitReqConvertCxt cxt = {0};
×
2127

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

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

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

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

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

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

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

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

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

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

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

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

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

2295
static int32_t buildExistTableInStreamRsp(SVnode *pVnode, SSubmitTbData *pSubmitTbData, SVCreateTbRsp *pCreateTbRsp) {
458,093✔
2296
  int32_t code = TSDB_CODE_SUCCESS;
458,093✔
2297
  if (pSubmitTbData->pCreateTbReq->flags & TD_CREATE_NORMAL_TB_IN_STREAM) {
458,093✔
2298
    code = buildExistNormalTalbeRsp(pVnode, pSubmitTbData->uid, &pCreateTbRsp->pMeta);
50,810✔
2299
    if (code) {
50,810✔
2300
      vError("vgId:%d, table uid:%" PRId64 " not exists, line:%d", TD_VID(pVnode), pSubmitTbData->uid, __LINE__);
×
2301
      return code;
×
2302
    }
2303
    pCreateTbRsp->code = TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
50,810✔
2304
    return TSDB_CODE_SUCCESS;
50,810✔
2305
  } else if (pSubmitTbData->pCreateTbReq->flags & TD_CREATE_SUB_TB_IN_STREAM) {
407,283✔
2306
    return buildExistSubTalbeRsp(pVnode, pSubmitTbData, &pCreateTbRsp->pMeta);
82,955✔
2307
  }
2308
  return TSDB_CODE_SUCCESS;
324,328✔
2309
}
2310

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

2320
  for (int32_t i = 0; i < numTbData; ++i) {
2,147,483,647✔
2321
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
1,451,759,139✔
2322

2323
    if (pTbData->pCreateTbReq == NULL) {
1,451,759,224✔
2324
      continue;
1,433,549,070✔
2325
    }
2326

2327
    pTbData->uid = pTbData->pCreateTbReq->uid;
18,210,154✔
2328

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

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

2344
    SVCreateTbRsp *pCreateTbRsp = taosArrayReserve(pResponse->aCreateTbRsp, 1);
18,210,282✔
2345
    pCreateTbRsp->index = i;
18,210,154✔
2346
    code = metaCreateTable2(pVnode->pMeta, version, pTbData->pCreateTbReq, &pCreateTbRsp->pMeta);
18,210,154✔
2347
    if (code == TSDB_CODE_SUCCESS) {
18,209,789✔
2348
      // Allocate necessary resources
2349
      if (newTbUids == NULL) {
17,750,438✔
2350
        newTbUids = taosArrayInit(numTbData, sizeof(int64_t));
14,561,291✔
2351
        if (newTbUids == NULL) {
14,560,926✔
2352
          code = terrno;
×
2353
          vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2354
                 tstrerror(code), version);
2355
          return code;
×
2356
        }
2357
      }
2358

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

2367
      if (pCreateTbRsp->pMeta) {
17,750,803✔
2368
        vnodeUpdateMetaRsp(pVnode, pCreateTbRsp->pMeta);
17,750,803✔
2369
      }
2370
    } else if (code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
459,351✔
2371
      code = terrno = 0;
458,157✔
2372
      pTbData->uid = pTbData->pCreateTbReq->uid;  // update uid if table exist for using below
458,157✔
2373

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

2391
  // Update the affected table uid list
2392
  if (taosArrayGetSize(newTbUids) > 0) {
1,273,439,378✔
2393
    vDebug("vgId:%d, add %d table into query table list in handling submit", TD_VID(pVnode),
14,560,561✔
2394
           (int32_t)taosArrayGetSize(newTbUids));
2395
    if (tqAddTbUidListForQuerySub(pVnode->pTq, newTbUids) != 0) {
14,561,291✔
2396
      vError("vgId:%d, failed to update tbUid list", TD_VID(pVnode));
×
2397
    }
2398
  }
2399

2400
  vDebug("vgId:%d, handle auto create table done, version:%" PRId64, TD_VID(pVnode), version);
1,273,439,322✔
2401

2402
  taosArrayDestroy(newTbUids);
1,273,440,236✔
2403
  return code;
1,273,440,122✔
2404
}
2405

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

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

2443
static int32_t vnodeHandleDataWrite(SVnode *pVnode, int64_t version, SSubmitReq2 *pRequest, SSubmitRsp2 *pResponse) {
1,273,439,421✔
2444
  int32_t code = TSDB_CODE_SUCCESS;
1,273,439,421✔
2445
  int32_t numTbData = taosArrayGetSize(pRequest->aSubmitTbData);
1,273,439,421✔
2446
  int8_t  hasBlob = 0;
1,273,439,701✔
2447

2448
  // Scan submit data
2449
  for (int32_t i = 0; i < numTbData; ++i) {
2,147,483,647✔
2450
    SMetaInfo      info = {0};
1,451,758,268✔
2451
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
1,451,758,065✔
2452

2453
    if (pTbData->flags & SUBMIT_REQ_WITH_BLOB) {
1,451,756,021✔
2454
      hasBlob = 1;
51,458✔
2455
    }
2456
    if (pTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
1,451,756,084✔
2457
      continue;  // skip column data format
7,563,044✔
2458
    }
2459
    if (pTbData->flags & SUBMIT_REQ_ONLY_CREATE_TABLE) {
1,444,194,521✔
2460
      continue;  // skip only crate table request
2,941,565✔
2461
    }
2462

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

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

2480
    if (info.suid) {
1,441,247,420✔
2481
      code = metaGetInfo(pVnode->pMeta, info.suid, &info, NULL);
1,271,653,878✔
2482
      if (code) {
1,271,656,778✔
2483
        code = TSDB_CODE_INTERNAL_ERROR;
×
2484
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " suid:%" PRId64, TD_VID(pVnode), __func__,
×
2485
               __FILE__, __LINE__, tstrerror(code), version, info.suid);
2486
        return code;
×
2487
      }
2488
    }
2489

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

2502
  // Do write data
2503
  vDebug("vgId:%d start to handle data write, version:%" PRId64, TD_VID(pVnode), version);
1,273,436,347✔
2504

2505
  for (int32_t i = 0; i < numTbData; ++i) {
2,147,483,647✔
2506
    int32_t        affectedRows = 0;
1,451,753,073✔
2507
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
1,451,753,440✔
2508

2509
    if (pTbData->flags & SUBMIT_REQ_ONLY_CREATE_TABLE) {
1,451,752,347✔
2510
      continue;
2,941,565✔
2511
    }
2512

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

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

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

2538
  vDebug("vgId:%d, handle data write done, version:%" PRId64 ", affectedRows:%d", TD_VID(pVnode), version,
1,273,412,122✔
2539
         pResponse->affectedRows);
2540
  return code;
1,273,438,421✔
2541
}
2542

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

2546
  int32_t   numCols = taosArrayGetSize(pTbData->aCol);
7,561,245✔
2547
  SColData *aColData = (SColData *)TARRAY_DATA(pTbData->aCol);
7,561,948✔
2548

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

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

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

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

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

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

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

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

2607
  return code;
7,562,711✔
2608
}
2609

2610
static int32_t vnodeScanSubmitRowData(SVnode *pVnode, SSubmitTbData *pTbData, TSKEY minKey, TSKEY maxKey) {
1,444,171,858✔
2611
  int32_t code = 0;
1,444,171,858✔
2612

2613
  int32_t numRows = taosArrayGetSize(pTbData->aRowP);
1,444,171,858✔
2614
  SRow  **aRow = (SRow **)TARRAY_DATA(pTbData->aRowP);
1,444,160,594✔
2615

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

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

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

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

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

2660
  return code;
1,437,414,756✔
2661
}
2662

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

2667
  TSKEY now = taosGetTimestamp(pVnode->config.tsdbCfg.precision);
1,273,417,555✔
2668
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * pVnode->config.tsdbCfg.keep2;
1,273,405,606✔
2669
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
1,273,420,943✔
2670
  for (int32_t i = 0; i < numTbData; i++) {
2,147,483,647✔
2671
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
1,451,732,159✔
2672

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

2680
    if (pTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
1,451,743,316✔
2681
      code = vnodeScanColumnData(pVnode, pTbData, minKey, maxKey);
7,561,222✔
2682
      if (code) {
7,562,711✔
2683
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2684
               __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2685
        return code;
×
2686
      }
2687
    } else {
2688
      code = vnodeScanSubmitRowData(pVnode, pTbData, minKey, maxKey);
1,444,155,056✔
2689
      if (code) {
1,444,168,681✔
2690
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
25,719✔
2691
               __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2692
        return code;
×
2693
      }
2694
    }
2695
  }
2696

2697
  return code;
1,273,385,877✔
2698
}
2699

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

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

2725
  return 0;
2,112✔
2726
}
2727

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2836
  return code;
192✔
2837
}
2838

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

2843
  SArray *aRows = NULL;
2,112✔
2844
  SArray *pVals = NULL;
2,112✔
2845
  SRow   *pRow = NULL;
2,112✔
2846

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

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

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

2861
    // colId is consistent with audit_columns in mndStb.c
2862
    void *data = NULL;
19,008✔
2863
    if (pCol->colId == 1) {
19,008✔
2864
      data = &record->curTime;
2,112✔
2865
    } else if (pCol->colId == 2) {
16,896✔
2866
      data = record->detail;
2,112✔
2867
    } else if (pCol->colId == 3) {
14,784✔
2868
      data = record->user;
2,112✔
2869
    } else if (pCol->colId == 4) {
12,672✔
2870
      data = record->operation;
2,112✔
2871
    } else if (pCol->colId == 5) {
10,560✔
2872
      data = record->target1;
2,112✔
2873
    } else if (pCol->colId == 6) {
8,448✔
2874
      data = record->target2;
2,112✔
2875
    } else if (pCol->colId == 7) {
6,336✔
2876
      data = record->clientAddress;
2,112✔
2877
    } else if (pCol->colId == 8) {
4,224✔
2878
      data = &record->duration;
2,112✔
2879
    } else if (pCol->colId == 9) {
2,112✔
2880
      data = &record->affectedRows;
2,112✔
2881
    } else {
2882
      vError("the column id %" PRIi16 " is not defined in audit record table", pCol->colId);
×
2883
      code = TSDB_CODE_APP_ERROR;
×
2884
      TAOS_CHECK_GOTO(code, &lino, _exit);
×
2885
    }
2886

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

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

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

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

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

2982
  SArray *aSubmitTbData = NULL;
2,112✔
2983
  SSubmitTbData tbData = {0};
2,112✔
2984

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

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

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

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

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

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

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

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

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

3025
  tbData.suid = suid;
2,112✔
3026
  tbData.sver = pSchema->version;
2,112✔
3027

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

3134
  return code;
2,112✔
3135
}
3136

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

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

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

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

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

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

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

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

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

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

3215
  return code;
768✔
3216
}
3217

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

3224
  SSubmitReq2 *pSubmitReq = &(SSubmitReq2){0};
1,273,440,154✔
3225
  SSubmitRsp2 *pSubmitRsp = &(SSubmitRsp2){0};
1,273,439,494✔
3226
  int32_t      ret;
3227
  SEncoder     ec = {0};
1,273,439,881✔
3228

3229
  pRsp->code = TSDB_CODE_SUCCESS;
1,273,439,285✔
3230

3231
  void           *pAllocMsg = NULL;
1,273,439,881✔
3232
  SSubmitReq2Msg *pMsg = (SSubmitReq2Msg *)pReq;
1,273,439,881✔
3233
  SDecoder        dc = {0};
1,273,439,881✔
3234
  if (0 == taosHton64(pMsg->version)) {
1,273,439,275✔
3235
    code = vnodeSubmitReqConvertToSubmitReq2(pVnode, (SSubmitReq *)pMsg, pSubmitReq);
×
3236
    if (TSDB_CODE_SUCCESS == code) {
×
3237
      code = vnodeRebuildSubmitReqMsg(pSubmitReq, &pReq);
×
3238
    }
3239
    if (TSDB_CODE_SUCCESS == code) {
×
3240
      pAllocMsg = pReq;
×
3241
    }
3242
    TSDB_CHECK_CODE(code, lino, _exit);
×
3243
  } else {
3244
    // decode
3245
    pReq = POINTER_SHIFT(pReq, sizeof(SSubmitReq2Msg));
1,273,437,873✔
3246
    len -= sizeof(SSubmitReq2Msg);
1,273,437,709✔
3247

3248
    tDecoderInit(&dc, pReq, len);
1,273,437,709✔
3249
    if (tDecodeSubmitReq(&dc, pSubmitReq, NULL) < 0) {
1,273,438,858✔
3250
      code = TSDB_CODE_INVALID_MSG;
×
3251
      TSDB_CHECK_CODE(code, lino, _exit);
×
3252
    }
3253
  }
3254

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

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

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

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

3282
_exit:
1,273,440,154✔
3283
  // message
3284
  pRsp->code = code;
1,273,440,154✔
3285
  tEncodeSize(tEncodeSSubmitRsp2, pSubmitRsp, pRsp->contLen, ret);
1,273,439,339✔
3286
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
1,273,438,995✔
3287
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
1,273,437,372✔
3288
  if (tEncodeSSubmitRsp2(&ec, pSubmitRsp) < 0) {
1,273,438,312✔
3289
    vError("vgId:%d, failed to encode submit response", TD_VID(pVnode));
×
3290
  }
3291
  tEncoderClear(&ec);
1,273,438,150✔
3292

3293
  // update statistics
3294
  (void)atomic_add_fetch_64(&pVnode->statis.nInsert, pSubmitRsp->affectedRows);
1,273,431,881✔
3295
  (void)atomic_add_fetch_64(&pVnode->statis.nInsertSuccess, pSubmitRsp->affectedRows);
1,273,437,904✔
3296
  (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsert, 1);
1,273,435,224✔
3297

3298
  // update metrics
3299
  METRICS_UPDATE(pVnode->writeMetrics.total_requests, METRIC_LEVEL_LOW, 1);
1,273,434,630✔
3300
  METRICS_UPDATE(pVnode->writeMetrics.total_rows, METRIC_LEVEL_HIGH, pSubmitRsp->affectedRows);
1,273,426,186✔
3301
  METRICS_UPDATE(pVnode->writeMetrics.total_bytes, METRIC_LEVEL_LOW, pMsg->header.contLen);
1,273,426,276✔
3302

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

3316
  if (code == 0) {
1,273,425,288✔
3317
    (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
1,273,421,329✔
3318
  }
3319

3320
  // clear
3321
  tDestroySubmitReq(pSubmitReq, 0 == taosHton64(pMsg->version) ? TSDB_MSG_FLG_CMPT : TSDB_MSG_FLG_DECODE);
1,273,439,039✔
3322
  tDestroySSubmitRsp2(pSubmitRsp, TSDB_MSG_FLG_ENCODE);
1,273,415,897✔
3323

3324
  if (code) {
1,273,407,867✔
3325
    terrno = code;
3,845✔
3326
    if (code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
3,845✔
3327
      vInfo("vgId:%d, failed to process submit request since %s, lino:%d, version:%" PRId64, TD_VID(pVnode),
556✔
3328
            tstrerror(code), lino, ver);
3329

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

3336
  taosMemoryFree(pAllocMsg);
1,273,411,170✔
3337
  tDecoderClear(&dc);
1,273,411,141✔
3338

3339
  return code;
1,273,436,606✔
3340
}
3341

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

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

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

3351
  return code;
28,775✔
3352
}
3353

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

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

3368
_exit:
4,344,707✔
3369
  pRsp->msgType = TDMT_VND_ALTER_CONFIRM_RSP;
4,344,707✔
3370
  pRsp->code = code;
4,344,707✔
3371
  pRsp->pCont = NULL;
4,344,707✔
3372
  pRsp->contLen = 0;
4,344,707✔
3373

3374
  return code;
4,344,707✔
3375
}
3376

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

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

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

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

3399
  if (pVnode->config.cacheLastSize != req.cacheLastSize) {
375,266✔
3400
    pVnode->config.cacheLastSize = req.cacheLastSize;
15,414✔
3401
    tsdbCacheSetCapacity(pVnode, (size_t)pVnode->config.cacheLastSize * 1024 * 1024);
15,414✔
3402
  }
3403

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

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

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

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

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

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

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

3441
  if (pVnode->config.cacheLast != req.cacheLast) {
375,266✔
3442
    pVnode->config.cacheLast = req.cacheLast;
109,158✔
3443
  }
3444

3445
  if (pVnode->config.walCfg.fsyncPeriod != req.walFsyncPeriod) {
375,266✔
3446
    pVnode->config.walCfg.fsyncPeriod = req.walFsyncPeriod;
59,488✔
3447
    walChanged = true;
59,488✔
3448
  }
3449

3450
  if (pVnode->config.walCfg.level != req.walLevel) {
375,266✔
3451
    if (pVnode->config.walCfg.level == 0) {
43,792✔
3452
      pVnode->config.walCfg.clearFiles = 1;
2,480✔
3453
    }
3454
    pVnode->config.walCfg.level = req.walLevel;
43,792✔
3455
    walChanged = true;
43,792✔
3456
  }
3457

3458
  if (pVnode->config.walCfg.retentionPeriod != req.walRetentionPeriod) {
375,266✔
3459
    pVnode->config.walCfg.retentionPeriod = req.walRetentionPeriod;
56,776✔
3460
    walChanged = true;
56,776✔
3461
  }
3462

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

3468
  if (pVnode->config.tsdbCfg.keep0 != req.daysToKeep0) {
375,266✔
3469
    pVnode->config.tsdbCfg.keep0 = req.daysToKeep0;
49,644✔
3470
    tsdbChanged = true;
49,644✔
3471
  }
3472

3473
  if (pVnode->config.tsdbCfg.keep1 != req.daysToKeep1) {
375,266✔
3474
    pVnode->config.tsdbCfg.keep1 = req.daysToKeep1;
47,992✔
3475
    tsdbChanged = true;
47,992✔
3476
  }
3477

3478
  if (pVnode->config.tsdbCfg.keep2 != req.daysToKeep2) {
375,266✔
3479
    pVnode->config.tsdbCfg.keep2 = req.daysToKeep2;
41,486✔
3480
    tsdbChanged = true;
41,486✔
3481
  }
3482

3483
  if (pVnode->config.tsdbCfg.keepTimeOffset != req.keepTimeOffset) {
375,266✔
3484
    pVnode->config.tsdbCfg.keepTimeOffset = req.keepTimeOffset;
8,312✔
3485
    tsdbChanged = true;
8,312✔
3486
  }
3487

3488
  if (req.sttTrigger != -1 && req.sttTrigger != pVnode->config.sttTrigger) {
375,266✔
3489
    if (req.sttTrigger > 1 && pVnode->config.sttTrigger > 1) {
2,902✔
3490
      pVnode->config.sttTrigger = req.sttTrigger;
1,652✔
3491
    } else {
3492
      vnodeAWait(&pVnode->commitTask);
1,250✔
3493

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

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

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

3508
  if (req.ssKeepLocal != -1 && req.ssKeepLocal != pVnode->config.ssKeepLocal) {
375,266✔
3509
    pVnode->config.ssKeepLocal = req.ssKeepLocal;
1,652✔
3510
  }
3511
  if (req.ssCompact != -1 && req.ssCompact != pVnode->config.ssCompact) {
375,266✔
3512
    pVnode->config.ssCompact = req.ssCompact;
1,652✔
3513
  }
3514
  if (req.allowDrop != pVnode->config.allowDrop) {
375,266✔
3515
    pVnode->config.allowDrop = req.allowDrop;
×
3516
  }
3517
  if (req.securityLevel != pVnode->config.securityLevel) {
375,266✔
3518
    pVnode->config.securityLevel = req.securityLevel;
3,880✔
3519
  }
3520
  if (req.secureDelete != pVnode->config.secureDelete) {
375,266✔
3521
    pVnode->config.secureDelete = req.secureDelete;
1,312✔
3522
  }
3523

3524
  if (walChanged) {
375,266✔
3525
    if (walAlter(pVnode->pWal, &pVnode->config.walCfg) != 0) {
158,400✔
3526
      vError("vgId:%d, failed to alter wal config since %s", TD_VID(pVnode), tstrerror(ERRNO));
×
3527
    }
3528
  }
3529

3530
  if (tsdbChanged) {
375,266✔
3531
    tsdbSetKeepCfg(pVnode->pTsdb, &pVnode->config.tsdbCfg);
66,220✔
3532
  }
3533

3534
  return 0;
375,266✔
3535
}
3536

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

3546
  SMetaReader mr = {0};
×
3547
  metaReaderDoInit(&mr, pVnode->pMeta, META_READER_NOLOCK);
×
3548
  STsdb *pTsdb = pVnode->pTsdb;
×
3549

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

3559
    int64_t uid = mr.me.uid;
×
3560

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

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

3584
static int32_t vnodeProcessDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
2,481,893✔
3585
                                     SRpcMsg *pOriginalMsg) {
3586
  int32_t     code = 0;
2,481,893✔
3587
  SDecoder   *pCoder = &(SDecoder){0};
2,481,893✔
3588
  SDeleteRes *pRes = &(SDeleteRes){0};
2,481,893✔
3589

3590
  pRsp->msgType = TDMT_VND_DELETE_RSP;
2,481,893✔
3591
  pRsp->pCont = NULL;
2,481,893✔
3592
  pRsp->contLen = 0;
2,481,893✔
3593
  pRsp->code = TSDB_CODE_SUCCESS;
2,481,893✔
3594

3595
  pRes->uidList = taosArrayInit(0, sizeof(tb_uid_t));
2,481,893✔
3596
  if (pRes->uidList == NULL) {
2,481,893✔
3597
    code = terrno;
×
3598
    goto _err;
×
3599
  }
3600

3601
  tDecoderInit(pCoder, pReq, len);
2,481,893✔
3602
  code = tDecodeDeleteRes(pCoder, pRes);
2,481,893✔
3603
  if (code) goto _err;
2,481,893✔
3604

3605
  if (pRes->affectedRows > 0) {
2,481,893✔
3606
    for (int32_t iUid = 0; iUid < taosArrayGetSize(pRes->uidList); iUid++) {
4,422,673✔
3607
      uint64_t uid = *(uint64_t *)taosArrayGet(pRes->uidList, iUid);
2,519,001✔
3608
      // Merge runtime vnode(db-level) secureDelete to avoid stale client meta after ALTER DATABASE.
3609
      int8_t secureDelete = pRes->secureDelete | pVnode->config.secureDelete;
2,519,001✔
3610
      code = tsdbDeleteTableData(pVnode->pTsdb, ver, pRes->suid, uid, pRes->skey, pRes->ekey, secureDelete);
2,519,001✔
3611
      if (code) goto _err;
2,519,001✔
3612
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, pRes->ctimeMs);
2,519,001✔
3613
      if (code) goto _err;
2,519,001✔
3614
    }
3615
  }
3616

3617
  tDecoderClear(pCoder);
2,481,893✔
3618
  taosArrayDestroy(pRes->uidList);
2,481,893✔
3619

3620
  SVDeleteRsp rsp = {.affectedRows = pRes->affectedRows};
2,481,893✔
3621
  int32_t     ret = 0;
2,481,893✔
3622
  tEncodeSize(tEncodeSVDeleteRsp, &rsp, pRsp->contLen, ret);
2,481,893✔
3623
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
2,481,893✔
3624
  SEncoder ec = {0};
2,481,893✔
3625
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
2,481,893✔
3626
  code = tEncodeSVDeleteRsp(&ec, &rsp);
2,481,893✔
3627
  if (code) goto _err;
2,481,893✔
3628
  tEncoderClear(&ec);
2,481,893✔
3629
  return code;
2,481,893✔
3630

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

3647
  return code;
×
3648
}
3649
static int32_t vnodeProcessCreateIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
21,174✔
3650
  SVCreateStbReq req = {0};
21,174✔
3651
  SDecoder       dc = {0};
21,174✔
3652
  int32_t        code = 0;
21,174✔
3653

3654
  pRsp->msgType = TDMT_VND_CREATE_INDEX_RSP;
21,174✔
3655
  pRsp->code = TSDB_CODE_SUCCESS;
21,174✔
3656
  pRsp->pCont = NULL;
21,174✔
3657
  pRsp->contLen = 0;
21,174✔
3658

3659
  tDecoderInit(&dc, pReq, len);
21,174✔
3660
  // decode req
3661
  if (tDecodeSVCreateStbReq(&dc, &req) < 0) {
21,174✔
3662
    tDecoderClear(&dc);
×
3663
    return terrno = TSDB_CODE_INVALID_MSG;
×
3664
  }
3665

3666
  code = metaAddIndexToSuperTable(pVnode->pMeta, ver, &req);
21,174✔
3667
  if (code) {
21,174✔
3668
    pRsp->code = code;
×
3669
    goto _err;
×
3670
  }
3671
  tDecoderClear(&dc);
21,174✔
3672
  return 0;
21,174✔
3673

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

3686
  if ((code = tDeserializeSDropIdxReq(pReq, len, &req))) {
17,228✔
3687
    pRsp->code = code;
×
3688
    return code;
×
3689
  }
3690

3691
  code = metaDropIndexFromSuperTable(pVnode->pMeta, ver, &req);
17,228✔
3692
  if (code) {
17,228✔
3693
    pRsp->code = code;
×
3694
    return code;
×
3695
  }
3696
  return TSDB_CODE_SUCCESS;
17,228✔
3697
}
3698

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

3701
static int32_t vnodeProcessCompactVnodeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
82,445✔
3702
  if (!pVnode->restored) {
82,445✔
3703
    vInfo("vgId:%d, ignore compact req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
5,040✔
3704
    return 0;
5,040✔
3705
  }
3706
  return vnodeAsyncCompact(pVnode, ver, pReq, len, pRsp);
77,405✔
3707
}
3708

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

3714
  pRsp->msgType = TDMT_SYNC_CONFIG_CHANGE_RSP;
×
3715
  pRsp->code = TSDB_CODE_SUCCESS;
×
3716
  pRsp->pCont = NULL;
×
3717
  pRsp->contLen = 0;
×
3718

3719
  return 0;
×
3720
}
3721

3722
static int32_t vnodeCheckState(SVnode *pVnode) {
66,402✔
3723
  SSyncState syncState = syncGetState(pVnode->sync);
66,402✔
3724
  if (syncState.state != TAOS_SYNC_STATE_LEADER) {
66,402✔
3725
    return terrno = TSDB_CODE_SYN_NOT_LEADER;
32,606✔
3726
  }
3727
  return 0;
33,796✔
3728
}
3729

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

3736
  if (strncmp(token, member0Token, TSDB_ARB_TOKEN_SIZE) != 0 &&
33,796✔
3737
      strncmp(token, member1Token, TSDB_ARB_TOKEN_SIZE) != 0) {
33,044✔
3738
    return terrno = TSDB_CODE_MND_ARB_TOKEN_MISMATCH;
31,890✔
3739
  }
3740

3741
  terrno = TSDB_CODE_SUCCESS;
1,906✔
3742
  return 0;
1,906✔
3743
}
3744

3745
static int32_t vnodeCheckSyncd(SVnode *pVnode, char *member0Token, char *member1Token) {
953✔
3746
  int32_t code = vnodeCheckToken(pVnode, member0Token, member1Token);
953✔
3747
  if (code != 0) {
953✔
UNCOV
3748
    return code;
×
3749
  }
3750

3751
  return syncCheckSynced(pVnode->sync);
953✔
3752
}
3753

3754
static int32_t vnodeProcessArbCheckSyncReq(SVnode *pVnode, void *pReq, int32_t len, SRpcMsg *pRsp) {
1,961✔
3755
  int32_t code = 0;
1,961✔
3756

3757
  if ((code = vnodeCheckState(pVnode)) != 0) {
1,961✔
3758
    vDebug("vgId:%d, failed to preprocess vnode-arb-check-sync request since %s", TD_VID(pVnode), tstrerror(code));
1,008✔
3759
    return 0;
1,008✔
3760
  }
3761

3762
  SVArbCheckSyncReq syncReq = {0};
953✔
3763

3764
  code = tDeserializeSVArbCheckSyncReq(pReq, len, &syncReq);
953✔
3765
  if (code) {
953✔
3766
    return code;
×
3767
  }
3768

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

3775
  pRsp->msgType = TDMT_VND_ARB_CHECK_SYNC_RSP;
953✔
3776
  pRsp->code = TSDB_CODE_SUCCESS;
953✔
3777
  pRsp->pCont = NULL;
953✔
3778
  pRsp->contLen = 0;
953✔
3779

3780
  SVArbCheckSyncRsp syncRsp = {0};
953✔
3781
  syncRsp.arbToken = syncReq.arbToken;
953✔
3782
  syncRsp.member0Token = syncReq.member0Token;
953✔
3783
  syncRsp.member1Token = syncReq.member1Token;
953✔
3784
  syncRsp.vgId = TD_VID(pVnode);
953✔
3785

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

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

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

3808
  if (tSerializeSVArbCheckSyncRsp(pHead, contLen, &syncRsp) <= 0) {
953✔
3809
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
3810
    rpcFreeCont(pHead);
×
3811
    code = -1;
×
3812
    goto _OVER;
×
3813
  }
3814

3815
  pRsp->pCont = pHead;
953✔
3816
  pRsp->contLen = contLen;
953✔
3817

3818
  vInfo(
953✔
3819
      "vgId:%d, suceed to process vnode-arb-check-sync req rsp.code:%s, arbToken:%s, member0Token:%s, "
3820
      "member1Token:%s",
3821
      TD_VID(pVnode), tstrerror(syncRsp.errCode), syncRsp.arbToken, syncRsp.member0Token, syncRsp.member1Token);
3822

3823
  code = TSDB_CODE_SUCCESS;
953✔
3824

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

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