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

taosdata / TDengine / #4585

25 Jul 2025 01:12AM UTC coverage: 59.161% (+0.08%) from 59.08%
#4585

push

travis-ci

web-flow
enh: refactor some tsdb log info (#32159)

145856 of 316723 branches covered (46.05%)

Branch coverage included in aggregate %.

0 of 13 new or added lines in 2 files covered. (0.0%)

254 existing lines in 53 files now uncovered.

220043 of 301752 relevant lines covered (72.92%)

18243809.17 hits per line

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

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

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

32
extern taos_counter_t *tsInsertCounter;
33

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

61
static int32_t vnodeCheckToken(SVnode *pVnode, char *member0Token, char *member1Token);
62
static int32_t vnodeCheckSyncd(SVnode *pVnode, char *member0Token, char *member1Token);
63
static int32_t vnodeProcessFetchTtlExpiredTbs(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
64

65
extern int32_t vnodeProcessKillCompactReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
66
extern int32_t vnodeQueryCompactProgress(SVnode *pVnode, SRpcMsg *pMsg);
67
extern int32_t vnodeQuerySsMigrateProgress(SVnode *pVnode, SRpcMsg *pMsg);
68

69
static int32_t vnodePreprocessCreateTableReq(SVnode *pVnode, SDecoder *pCoder, int64_t btime, int64_t *pUid) {
195,273✔
70
  int32_t code = 0;
195,273✔
71
  int32_t lino = 0;
195,273✔
72

73
  if (tStartDecode(pCoder) < 0) {
195,273!
74
    code = TSDB_CODE_INVALID_MSG;
×
75
    TSDB_CHECK_CODE(code, lino, _exit);
×
76
  }
77

78
  // flags
79
  if (tDecodeI32v(pCoder, NULL) < 0) {
195,403!
80
    code = TSDB_CODE_INVALID_MSG;
×
81
    TSDB_CHECK_CODE(code, lino, _exit);
×
82
  }
83

84
  // name
85
  char *name = NULL;
195,403✔
86
  if (tDecodeCStr(pCoder, &name) < 0) {
195,284!
87
    code = TSDB_CODE_INVALID_MSG;
×
88
    TSDB_CHECK_CODE(code, lino, _exit);
×
89
  }
90

91
  // uid
92
  int64_t uid = metaGetTableEntryUidByName(pVnode->pMeta, name);
195,284✔
93
  if (uid == 0) {
195,479✔
94
    uid = tGenIdPI64();
185,865✔
95
  }
96
  taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), uid);
195,468✔
97

98
  // btime
99
  taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos + 8), btime);
195,468✔
100

101
  tEndDecode(pCoder);
195,468✔
102

103
_exit:
195,424✔
104
  if (code) {
195,424!
105
    vError("vgId:%d %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
106
  } else {
107
    vTrace("vgId:%d %s done, table:%s uid generated:%" PRId64, TD_VID(pVnode), __func__, name, uid);
195,424✔
108
    if (pUid) *pUid = uid;
195,424✔
109
  }
110
  return code;
195,423✔
111
}
112
static int32_t vnodePreProcessCreateTableMsg(SVnode *pVnode, SRpcMsg *pMsg) {
89,091✔
113
  int32_t code = 0;
89,091✔
114
  int32_t lino = 0;
89,091✔
115

116
  int64_t  btime = taosGetTimestampMs();
89,095✔
117
  SDecoder dc = {0};
89,095✔
118
  int32_t  nReqs;
119

120
  tDecoderInit(&dc, (uint8_t *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead));
89,095✔
121
  if (tStartDecode(&dc) < 0) {
89,091!
122
    code = TSDB_CODE_INVALID_MSG;
×
123
    return code;
×
124
  }
125

126
  if (tDecodeI32v(&dc, &nReqs) < 0) {
89,098!
127
    code = TSDB_CODE_INVALID_MSG;
×
128
    TSDB_CHECK_CODE(code, lino, _exit);
×
129
  }
130
  for (int32_t iReq = 0; iReq < nReqs; iReq++) {
207,647✔
131
    code = vnodePreprocessCreateTableReq(pVnode, &dc, btime, NULL);
118,577✔
132
    TSDB_CHECK_CODE(code, lino, _exit);
118,549!
133
  }
134

135
  tEndDecode(&dc);
89,070✔
136

137
_exit:
89,079✔
138
  tDecoderClear(&dc);
89,079✔
139
  if (code) {
89,095!
140
    vError("vgId:%d, %s:%d failed to preprocess submit request since %s, msg type:%s", TD_VID(pVnode), __func__, lino,
×
141
           tstrerror(code), TMSG_INFO(pMsg->msgType));
142
  }
143
  return code;
89,090✔
144
}
145

146
static int32_t vnodePreProcessAlterTableMsg(SVnode *pVnode, SRpcMsg *pMsg) {
20,331✔
147
  int32_t code = TSDB_CODE_INVALID_MSG;
20,331✔
148
  int32_t lino = 0;
20,331✔
149

150
  SDecoder dc = {0};
20,331✔
151
  tDecoderInit(&dc, (uint8_t *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead));
20,331✔
152

153
  SVAlterTbReq vAlterTbReq = {0};
20,331✔
154
  int64_t      ctimeMs = taosGetTimestampMs();
20,331✔
155
  if (tDecodeSVAlterTbReqSetCtime(&dc, &vAlterTbReq, ctimeMs) < 0) {
20,331!
156
    taosArrayDestroy(vAlterTbReq.pMultiTag);
×
157
    vAlterTbReq.pMultiTag = NULL;
×
158
    goto _exit;
×
159
  }
160
  taosArrayDestroy(vAlterTbReq.pMultiTag);
20,331✔
161
  vAlterTbReq.pMultiTag = NULL;
20,331✔
162

163
  code = 0;
20,331✔
164

165
_exit:
20,331✔
166
  tDecoderClear(&dc);
20,331✔
167
  if (code) {
20,331!
168
    vError("vgId:%d %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
169
  } else {
170
    vTrace("vgId:%d %s done, table:%s ctimeMs generated:%" PRId64, TD_VID(pVnode), __func__, vAlterTbReq.tbName,
20,331!
171
           ctimeMs);
172
  }
173
  return code;
20,331✔
174
}
175

176
static int32_t vnodePreProcessDropTtlMsg(SVnode *pVnode, SRpcMsg *pMsg) {
71,875✔
177
  int32_t code = TSDB_CODE_INVALID_MSG;
71,875✔
178
  int32_t lino = 0;
71,875✔
179

180
  SMsgHead *pContOld = pMsg->pCont;
71,875✔
181
  int32_t   reqLenOld = pMsg->contLen - sizeof(SMsgHead);
71,875✔
182

183
  SArray *tbUids = NULL;
71,875✔
184
  int64_t timestampMs = 0;
71,875✔
185

186
  SVDropTtlTableReq ttlReq = {0};
71,875✔
187
  if (tDeserializeSVDropTtlTableReq((char *)pContOld + sizeof(SMsgHead), reqLenOld, &ttlReq) != 0) {
71,875!
188
    code = TSDB_CODE_INVALID_MSG;
×
189
    TSDB_CHECK_CODE(code, lino, _exit);
×
190
  }
191

192
  {  // find expired uids
193
    tbUids = taosArrayInit(8, sizeof(tb_uid_t));
72,076✔
194
    if (tbUids == NULL) {
72,121!
195
      code = terrno;
×
196
      TSDB_CHECK_CODE(code, lino, _exit);
×
197
    }
198

199
    timestampMs = (int64_t)ttlReq.timestampSec * 1000;
72,121✔
200
    code = metaTtlFindExpired(pVnode->pMeta, timestampMs, tbUids, ttlReq.ttlDropMaxCount);
72,121✔
201
    if (code != 0) {
71,885!
202
      code = TSDB_CODE_INVALID_MSG;
×
203
      TSDB_CHECK_CODE(code, lino, _exit);
×
204
    }
205

206
    ttlReq.nUids = taosArrayGetSize(tbUids);
71,885✔
207
    ttlReq.pTbUids = tbUids;
71,737✔
208
  }
209

210
  if (ttlReq.nUids == 0) {
71,737!
211
    code = TSDB_CODE_MSG_PREPROCESSED;
71,788✔
212
    TSDB_CHECK_CODE(code, lino, _exit);
71,788!
213
  }
214

215
  {  // prepare new content
UNCOV
216
    int32_t reqLenNew = tSerializeSVDropTtlTableReq(NULL, 0, &ttlReq);
×
217
    int32_t contLenNew = reqLenNew + sizeof(SMsgHead);
16✔
218

219
    SMsgHead *pContNew = rpcMallocCont(contLenNew);
16✔
220
    if (pContNew == NULL) {
16!
221
      code = terrno;
×
222
      TSDB_CHECK_CODE(code, lino, _exit);
×
223
    }
224

225
    if (tSerializeSVDropTtlTableReq((char *)pContNew + sizeof(SMsgHead), reqLenNew, &ttlReq) != 0) {
16!
226
      vError("vgId:%d %s:%d failed to serialize drop ttl request", TD_VID(pVnode), __func__, lino);
16!
227
    }
228
    pContNew->contLen = htonl(reqLenNew);
16✔
229
    pContNew->vgId = pContOld->vgId;
16✔
230

231
    rpcFreeCont(pContOld);
16✔
232
    pMsg->pCont = pContNew;
16✔
233
    pMsg->contLen = contLenNew;
16✔
234
  }
235

236
  code = 0;
16✔
237

238
_exit:
71,825✔
239
  taosArrayDestroy(tbUids);
71,825✔
240

241
  if (code && code != TSDB_CODE_MSG_PREPROCESSED) {
71,830!
242
    vError("vgId:%d, %s:%d failed to preprocess drop ttl request since %s, msg type:%s", TD_VID(pVnode), __func__, lino,
×
243
           tstrerror(code), TMSG_INFO(pMsg->msgType));
244
  } else {
245
    vTrace("vgId:%d, %s done, timestampSec:%d, nUids:%d", TD_VID(pVnode), __func__, ttlReq.timestampSec, ttlReq.nUids);
71,830!
246
  }
247

248
  return code;
71,874✔
249
}
250

251
extern int64_t tsMaxKeyByPrecision[];
252
static int32_t vnodePreProcessSubmitTbData(SVnode *pVnode, SDecoder *pCoder, int64_t btimeMs, int64_t ctimeMs) {
10,802,321✔
253
  int32_t code = 0;
10,802,321✔
254
  int32_t lino = 0;
10,802,321✔
255

256
  if (tStartDecode(pCoder) < 0) {
10,802,321!
257
    code = TSDB_CODE_INVALID_MSG;
×
258
    TSDB_CHECK_CODE(code, lino, _exit);
×
259
  }
260

261
  SSubmitTbData submitTbData;
262
  uint8_t       version;
263
  if (tDecodeI32v(pCoder, &submitTbData.flags) < 0) {
10,802,942!
264
    code = TSDB_CODE_INVALID_MSG;
×
265
    TSDB_CHECK_CODE(code, lino, _exit);
×
266
  }
267
  version = (submitTbData.flags >> 8) & 0xff;
10,802,942✔
268
  submitTbData.flags = submitTbData.flags & 0xff;
10,802,942✔
269

270
  int64_t uid;
271
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
10,802,942✔
272
    code = vnodePreprocessCreateTableReq(pVnode, pCoder, btimeMs, &uid);
76,708✔
273
    TSDB_CHECK_CODE(code, lino, _exit);
76,873!
274
  }
275

276
  // submit data
277
  if (tDecodeI64(pCoder, &submitTbData.suid) < 0) {
10,803,058!
278
    code = TSDB_CODE_INVALID_MSG;
×
279
    TSDB_CHECK_CODE(code, lino, _exit);
×
280
  }
281

282
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
10,803,058✔
283
    taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), uid);
76,867✔
284
    pCoder->pos += sizeof(int64_t);
76,867✔
285
  } else {
286
    if (tDecodeI64(pCoder, &submitTbData.uid) < 0) {
10,726,160!
287
      code = TSDB_CODE_INVALID_MSG;
×
288
      TSDB_CHECK_CODE(code, lino, _exit);
×
289
    }
290
  }
291

292
  if (tDecodeI32v(pCoder, &submitTbData.sver) < 0) {
10,802,870!
293
    code = TSDB_CODE_INVALID_MSG;
×
294
    TSDB_CHECK_CODE(code, lino, _exit);
×
295
  }
296

297
  // scan and check
298
  TSKEY now = btimeMs;
10,802,870✔
299
  if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_MICRO) {
10,802,870✔
300
    now *= 1000;
22,805✔
301
  } else if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_NANO) {
10,780,065✔
302
    now *= 1000000;
3,353✔
303
  }
304

305
  int32_t keep = pVnode->config.tsdbCfg.keep2;
10,802,870✔
306

307
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * keep;
10,802,870✔
308
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
10,802,870✔
309
  if (submitTbData.flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
10,802,870✔
310
    uint64_t nColData;
311
    if (tDecodeU64v(pCoder, &nColData) < 0) {
567!
312
      code = TSDB_CODE_INVALID_MSG;
×
313
      TSDB_CHECK_CODE(code, lino, _exit);
1!
314
    }
315

316
    SColData colData = {0};
567✔
317
    code = tDecodeColData(version, pCoder, &colData);
567✔
318
    if (code) {
567!
319
      code = TSDB_CODE_INVALID_MSG;
×
320
      TSDB_CHECK_CODE(code, lino, _exit);
×
321
    }
322

323
    if (colData.flag != HAS_VALUE) {
567!
324
      code = TSDB_CODE_INVALID_MSG;
×
325
      TSDB_CHECK_CODE(code, lino, _exit);
×
326
    }
327

328
    for (int32_t iRow = 0; iRow < colData.nVal; iRow++) {
43,939✔
329
      if (((TSKEY *)colData.pData)[iRow] < minKey || ((TSKEY *)colData.pData)[iRow] > maxKey) {
43,373!
330
        code = TSDB_CODE_TDB_TIMESTAMP_OUT_OF_RANGE;
1✔
331
        TSDB_CHECK_CODE(code, lino, _exit);
1!
332
      }
333
    }
334

335
    for (uint64_t i = 1; i < nColData; i++) {
3,903✔
336
      code = tDecodeColData(version, pCoder, &colData);
3,337✔
337
      if (code) {
3,337!
338
        code = TSDB_CODE_INVALID_MSG;
×
339
        TSDB_CHECK_CODE(code, lino, _exit);
×
340
      }
341
    }
342
  } else {
343
    uint64_t nRow;
344
    if (tDecodeU64v(pCoder, &nRow) < 0) {
10,802,240!
345
      code = TSDB_CODE_INVALID_MSG;
×
346
      TSDB_CHECK_CODE(code, lino, _exit);
984!
347
    }
348

349
    for (int32_t iRow = 0; iRow < nRow; ++iRow) {
1,011,808,817✔
350
      SRow *pRow = (SRow *)(pCoder->data + pCoder->pos);
1,001,007,561✔
351
      pCoder->pos += pRow->len;
1,001,007,561✔
352
#ifndef NO_UNALIGNED_ACCESS
353
      if (pRow->ts < minKey || pRow->ts > maxKey) {
1,001,007,561!
354
#else
355
      TSKEY ts = taosGetInt64Aligned(&pRow->ts);
356
      if (ts < minKey || ts > maxKey) {
357
#endif
358
        code = TSDB_CODE_TDB_TIMESTAMP_OUT_OF_RANGE;
×
359
        TSDB_CHECK_CODE(code, lino, _exit);
×
360
      }
361

362
      // Check pRow->sver
363
      if (pRow->sver != submitTbData.sver) {
1,001,006,577!
364
        code = TSDB_CODE_INVALID_DATA_FMT;
×
365
        TSDB_CHECK_CODE(code, lino, _exit);
×
366
      }
367
    }
368
  }
369

370
  if (!tDecodeIsEnd(pCoder)) {
10,801,822!
371
    taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), ctimeMs);
10,801,946✔
372
    pCoder->pos += sizeof(int64_t);
10,801,946✔
373
  }
374

375
  tEndDecode(pCoder);
10,801,822✔
376

377
_exit:
10,802,301✔
378
  if (code) {
10,802,301✔
379
    vError("vgId:%d, %s:%d failed to vnodePreProcessSubmitTbData submit request since %s", TD_VID(pVnode), __func__,
985!
380
           lino, tstrerror(code));
381
  }
382
  return code;
10,802,312✔
383
}
384
static int32_t vnodePreProcessSubmitMsg(SVnode *pVnode, SRpcMsg *pMsg) {
10,681,505✔
385
  int32_t code = 0;
10,681,505✔
386
  int32_t lino = 0;
10,681,505✔
387

388
  if (tsBypassFlag & TSDB_BYPASS_RA_RPC_RECV_SUBMIT) {
10,681,505✔
389
    return TSDB_CODE_MSG_PREPROCESSED;
1✔
390
  }
391

392
  SDecoder *pCoder = &(SDecoder){0};
10,681,504✔
393

394
  if (taosHton64(((SSubmitReq2Msg *)pMsg->pCont)->version) != 1) {
10,681,504!
395
    code = TSDB_CODE_INVALID_MSG;
×
396
    TSDB_CHECK_CODE(code, lino, _exit);
×
397
  }
398

399
  tDecoderInit(pCoder, (uint8_t *)pMsg->pCont + sizeof(SSubmitReq2Msg), pMsg->contLen - sizeof(SSubmitReq2Msg));
10,681,556✔
400

401
  if (tStartDecode(pCoder) < 0) {
10,681,448!
402
    code = TSDB_CODE_INVALID_MSG;
×
403
    TSDB_CHECK_CODE(code, lino, _exit);
×
404
  }
405

406
  uint64_t nSubmitTbData;
407
  if (tDecodeU64v(pCoder, &nSubmitTbData) < 0) {
10,681,576!
408
    code = TSDB_CODE_INVALID_MSG;
×
409
    TSDB_CHECK_CODE(code, lino, _exit);
×
410
  }
411

412
  int64_t btimeMs = taosGetTimestampMs();
10,681,501✔
413
  int64_t ctimeMs = btimeMs;
10,681,501✔
414
  for (int32_t i = 0; i < nSubmitTbData; i++) {
21,482,834✔
415
    code = vnodePreProcessSubmitTbData(pVnode, pCoder, btimeMs, ctimeMs);
10,802,401✔
416
    TSDB_CHECK_CODE(code, lino, _exit);
10,802,318✔
417
  }
418

419
  tEndDecode(pCoder);
10,680,433✔
420

421
_exit:
10,681,509✔
422
  tDecoderClear(pCoder);
10,681,509✔
423
  if (code) {
10,681,502✔
424
    vError("vgId:%d, %s:%d failed to preprocess submit request since %s, msg type:%s", TD_VID(pVnode), __func__, lino,
985!
425
           tstrerror(code), TMSG_INFO(pMsg->msgType));
426
  }
427
  return code;
10,681,462✔
428
}
429

430
static int32_t vnodePreProcessDeleteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
63,407✔
431
  int32_t code = 0;
63,407✔
432

433
  int32_t    size;
434
  int32_t    ret;
435
  uint8_t   *pCont;
436
  SEncoder  *pCoder = &(SEncoder){0};
63,407✔
437
  SDeleteRes res = {0};
63,407✔
438

439
  SReadHandle handle = {0};
63,407✔
440
  handle.vnode = pVnode;
63,407✔
441
  handle.pMsgCb = &pVnode->msgCb;
63,407✔
442
  handle.skipRollup = 1;
63,407✔
443
  initStorageAPI(&handle.api);
63,407✔
444

445
  code = qWorkerProcessDeleteMsg(&handle, pVnode->pQuery, pMsg, &res);
63,408✔
446
  if (code) goto _exit;
63,406!
447

448
  res.ctimeMs = taosGetTimestampMs();
63,406✔
449
  // malloc and encode
450
  tEncodeSize(tEncodeDeleteRes, &res, size, ret);
63,406!
451
  pCont = rpcMallocCont(size + sizeof(SMsgHead));
63,407✔
452

453
  ((SMsgHead *)pCont)->contLen = size + sizeof(SMsgHead);
63,409✔
454
  ((SMsgHead *)pCont)->vgId = TD_VID(pVnode);
63,409✔
455

456
  tEncoderInit(pCoder, pCont + sizeof(SMsgHead), size);
63,409✔
457
  if (tEncodeDeleteRes(pCoder, &res) != 0) {
63,410!
458
    vError("vgId:%d %s failed to encode delete response", TD_VID(pVnode), __func__);
×
459
  }
460
  tEncoderClear(pCoder);
63,410✔
461

462
  rpcFreeCont(pMsg->pCont);
63,408✔
463
  pMsg->pCont = pCont;
63,410✔
464
  pMsg->contLen = size + sizeof(SMsgHead);
63,410✔
465

466
  taosArrayDestroy(res.uidList);
63,410✔
467

468
_exit:
63,410✔
469
  return code;
63,410✔
470
}
471

472
static int32_t vnodePreProcessBatchDeleteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
×
473
  int32_t code = 0;
×
474
  int32_t lino = 0;
×
475

476
  int64_t         ctimeMs = taosGetTimestampMs();
×
477
  SBatchDeleteReq pReq = {0};
×
478
  SDecoder       *pCoder = &(SDecoder){0};
×
479

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

482
  if (tDecodeSBatchDeleteReqSetCtime(pCoder, &pReq, ctimeMs) < 0) {
×
483
    code = TSDB_CODE_INVALID_MSG;
×
484
  }
485

486
  tDecoderClear(pCoder);
×
487
  taosArrayDestroy(pReq.deleteReqs);
×
488

489
  if (code) {
×
490
    vError("vgId:%d %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
491
  } else {
492
    vTrace("vgId:%d %s done, ctimeMs generated:%" PRId64, TD_VID(pVnode), __func__, ctimeMs);
×
493
  }
494
  return code;
×
495
}
496

497
static int32_t vnodePreProcessArbCheckSyncMsg(SVnode *pVnode, SRpcMsg *pMsg) {
168✔
498
  SVArbCheckSyncReq syncReq = {0};
168✔
499

500
  if (tDeserializeSVArbCheckSyncReq((char *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead),
168!
501
                                    &syncReq) != 0) {
502
    return TSDB_CODE_INVALID_MSG;
×
503
  }
504

505
  int32_t ret = vnodeCheckToken(pVnode, syncReq.member0Token, syncReq.member1Token);
168✔
506
  if (ret != 0) {
168✔
507
    vError("vgId:%d, failed to preprocess arb check sync request since %s", TD_VID(pVnode), tstrerror(ret));
162!
508
  }
509

510
  int32_t code = terrno;
168✔
511
  tFreeSVArbCheckSyncReq(&syncReq);
168✔
512

513
  return code;
168✔
514
}
515

516
int32_t vnodePreProcessDropTbMsg(SVnode *pVnode, SRpcMsg *pMsg) {
15,851✔
517
  int32_t          code = TSDB_CODE_SUCCESS;
15,851✔
518
  int32_t          lino = 0;
15,851✔
519
  int32_t          size = 0;
15,851✔
520
  SDecoder         dc = {0};
15,851✔
521
  SEncoder         ec = {0};
15,851✔
522
  SVDropTbBatchReq receivedBatchReqs = {0};
15,851✔
523
  SVDropTbBatchReq sentBatchReqs = {0};
15,851✔
524

525
  tDecoderInit(&dc, POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), pMsg->contLen - sizeof(SMsgHead));
15,851✔
526

527
  code = tDecodeSVDropTbBatchReq(&dc, &receivedBatchReqs);
15,851✔
528
  if (code < 0) {
15,851!
529
    terrno = code;
×
530
    TSDB_CHECK_CODE(code, lino, _exit);
×
531
  }
532
  sentBatchReqs.pArray = taosArrayInit(receivedBatchReqs.nReqs, sizeof(SVDropTbReq));
15,851✔
533
  if (!sentBatchReqs.pArray) {
15,851!
534
    code = terrno;
×
535
    goto _exit;
×
536
  }
537

538
  for (int32_t i = 0; i < receivedBatchReqs.nReqs; ++i) {
32,103✔
539
    SVDropTbReq *pReq = receivedBatchReqs.pReqs + i;
16,252✔
540
    tb_uid_t     uid = metaGetTableEntryUidByName(pVnode->pMeta, pReq->name);
16,252✔
541
    if (uid == 0) {
16,252!
542
      vWarn("vgId:%d, preprocess drop ctb: %s not found", TD_VID(pVnode), pReq->name);
×
543
      continue;
×
544
    }
545
    pReq->uid = uid;
16,252✔
546
    vDebug("vgId:%d %s for: %s, uid: %" PRId64, TD_VID(pVnode), __func__, pReq->name, pReq->uid);
16,252✔
547
    if (taosArrayPush(sentBatchReqs.pArray, pReq) == NULL) {
32,504!
548
      code = terrno;
×
549
      goto _exit;
×
550
    }
551
  }
552
  sentBatchReqs.nReqs = sentBatchReqs.pArray->size;
15,851✔
553

554
  tEncodeSize(tEncodeSVDropTbBatchReq, &sentBatchReqs, size, code);
15,851!
555
  tEncoderInit(&ec, POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), size);
15,851✔
556
  code = tEncodeSVDropTbBatchReq(&ec, &sentBatchReqs);
15,851✔
557
  tEncoderClear(&ec);
15,851✔
558
  if (code != TSDB_CODE_SUCCESS) {
15,851!
559
    vError("vgId:%d %s failed to encode drop tb batch req: %s", TD_VID(pVnode), __func__, tstrerror(code));
×
560
    TSDB_CHECK_CODE(code, lino, _exit);
×
561
  }
562

563
_exit:
15,851✔
564
  tDecoderClear(&dc);
15,851✔
565
  if (sentBatchReqs.pArray) {
15,851!
566
    taosArrayDestroy(sentBatchReqs.pArray);
15,851✔
567
  }
568
  return code;
15,851✔
569
}
570

571
int32_t vnodePreProcessSsMigrateReq(SVnode *pVnode, SRpcMsg *pMsg) {
×
572
  int32_t             code = TSDB_CODE_SUCCESS;
×
573
  int32_t             lino = 0;
×
574
  SSsMigrateVgroupReq req = {0};
×
575

576
  code = tDeserializeSSsMigrateVgroupReq(POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), pMsg->contLen - sizeof(SMsgHead),
×
577
                                         &req);
578
  if (code < 0) {
×
579
    terrno = code;
×
580
    TSDB_CHECK_CODE(code, lino, _exit);
×
581
  }
582

583
  req.nodeId = vnodeNodeId(pVnode);
×
584
  tSerializeSSsMigrateVgroupReq(POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), pMsg->contLen - sizeof(SMsgHead), &req);
×
585

586
_exit:
×
587
  return code;
×
588
}
589

590
int32_t vnodePreProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
11,030,704✔
591
  int32_t code = 0;
11,030,704✔
592

593
  if (pVnode->mounted) {
11,030,704✔
594
    vError("vgId:%d, mountVgId:%d, skip write msg:%s", TD_VID(pVnode), pVnode->config.mountVgId,
10!
595
           TMSG_INFO(pMsg->msgType));
596
    return TSDB_CODE_OPS_NOT_SUPPORT;
10✔
597
  }
598

599
  switch (pMsg->msgType) {
11,030,694!
600
    case TDMT_VND_CREATE_TABLE: {
89,092✔
601
      code = vnodePreProcessCreateTableMsg(pVnode, pMsg);
89,092✔
602
    } break;
89,090✔
603
    case TDMT_VND_ALTER_TABLE: {
20,331✔
604
      code = vnodePreProcessAlterTableMsg(pVnode, pMsg);
20,331✔
605
    } break;
20,331✔
606
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
71,970✔
607
    case TDMT_VND_DROP_TTL_TABLE: {
608
      code = vnodePreProcessDropTtlMsg(pVnode, pMsg);
71,970✔
609
    } break;
71,817✔
610
    case TDMT_VND_SUBMIT: {
10,681,522✔
611
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.preprocess_time, METRIC_LEVEL_LOW,
10,681,522!
612
                           { code = vnodePreProcessSubmitMsg(pVnode, pMsg); });
613
    } break;
10,681,449✔
614
    case TDMT_VND_DELETE: {
63,404✔
615
      code = vnodePreProcessDeleteMsg(pVnode, pMsg);
63,404✔
616
    } break;
63,405✔
617
    case TDMT_VND_BATCH_DEL: {
×
618
      code = vnodePreProcessBatchDeleteMsg(pVnode, pMsg);
×
619
    } break;
×
620
    case TDMT_VND_ARB_CHECK_SYNC: {
168✔
621
      code = vnodePreProcessArbCheckSyncMsg(pVnode, pMsg);
168✔
622
    } break;
168✔
623
    case TDMT_VND_DROP_TABLE: {
15,851✔
624
      code = vnodePreProcessDropTbMsg(pVnode, pMsg);
15,851✔
625
    } break;
15,851✔
626
    case TDMT_VND_SSMIGRATE: {
×
627
      code = vnodePreProcessSsMigrateReq(pVnode, pMsg);
×
628
    } break;
×
629
    default:
88,356✔
630
      break;
88,356✔
631
  }
632

633
  if (code && code != TSDB_CODE_MSG_PREPROCESSED) {
11,030,467✔
634
    vError("vgId:%d, failed to preprocess write request since %s, msg type:%s", TD_VID(pVnode), tstrerror(code),
1,147!
635
           TMSG_INFO(pMsg->msgType));
636
  }
637
  return code;
11,030,308✔
638
}
639

640
static int32_t inline vnodeSubmitSubRowBlobData(SVnode *pVnode, SSubmitTbData *pSubmitTbData) {
×
641
  int32_t code = 0;
×
642
  int32_t lino = 0;
×
643

644
  int64_t   st = taosGetTimestampUs();
×
645
  SBlobSet *pBlobSet = pSubmitTbData->pBlobSet;
×
646
  int32_t   sz = taosArrayGetSize(pBlobSet->pSeqTable);
×
647

648
  SBseBatch *pBatch = NULL;
×
649

650
  code = bseBatchInit(pVnode->pBse, &pBatch, sz);
×
651
  TSDB_CHECK_CODE(code, lino, _exit);
×
652

653
  SRow  **pRow = (SRow **)TARRAY_DATA(pSubmitTbData->aRowP);
×
654
  int32_t rowIdx = -1;
×
655
  for (int32_t i = 0; i < sz; i++) {
×
656
    int64_t     seq = 0;
×
657
    SBlobValue *p = taosArrayGet(pBlobSet->pSeqTable, i);
×
658
    if (p->type == TSDB_DATA_BLOB_EMPTY_VALUE || p->type == TSDB_DATA_BLOB_NULL_VALUE) {
×
659
      // skip empty or null blob
660
      continue;
×
661
    }
662

663
    code = bseBatchPut(pBatch, &seq, pBlobSet->data + p->offset, p->len);
×
664
    TSDB_CHECK_CODE(code, lino, _exit);
×
665

666
    SRow *row = taosArrayGetP(pSubmitTbData->aRowP, i);
×
667
    if (row == NULL) {
×
668
      int32_t tlen = taosArrayGetSize(pBlobSet->pSeqTable);
×
669
      uTrace("blob invalid row index:%d, sz:%d, pBlobSet size:%d", rowIdx, sz, tlen);
×
670
      break;
×
671
    }
672

673
    tPutU64(row->data + p->dataOffset, seq);
×
674
  }
675

676
  code = bseCommitBatch(pVnode->pBse, pBatch);
×
677
  TSDB_CHECK_CODE(code, lino, _exit);
×
678

679
  int64_t cost = taosGetTimestampUs() - st;
×
680
  if (cost >= 500) {
×
681
    vDebug("vgId:%d, %s, cost:%" PRId64 "us, rows:%d, size:%" PRId64 "", TD_VID(pVnode), __func__, cost, sz,
×
682
           pBlobSet->len);
683
  }
684
_exit:
×
685
  if (code != 0) {
×
686
    vError("vgId:%d %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
687
  }
688
  return code;
×
689
}
690

691
static int32_t inline vnodeSubmitSubColBlobData(SVnode *pVnode, SSubmitTbData *pSubmitTbData) {
×
692
  int32_t code = 0;
×
693
  int32_t lino = 0;
×
694

695
  int32_t   blobColIdx = 0;
×
696
  SColData *pBlobCol = NULL;
×
697
  int64_t   st = taosGetTimestampUs();
×
698
  SBlobSet *pBlobSet = pSubmitTbData->pBlobSet;
×
699
  int32_t   sz = taosArrayGetSize(pBlobSet->pSeqTable);
×
700

701
  SBseBatch *pBatch = NULL;
×
702

703
  code = bseBatchInit(pVnode->pBse, &pBatch, sz);
×
704
  TSDB_CHECK_CODE(code, lino, _exit);
×
705

706
  SColData *p = (SColData *)TARRAY_DATA(pSubmitTbData->aCol);
×
707
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitTbData->aCol); i++) {
×
708
    if (IS_STR_DATA_BLOB(p[i].type)) {
×
709
      pBlobCol = &p[i];
×
710
      break;
×
711
    }
712
  }
713
  if (pBlobCol == NULL) {
×
714
    vError("vgId:%d %s failed to find blob column in submit data", TD_VID(pVnode), __func__);
×
715
    code = TSDB_CODE_INVALID_MSG;
×
716
    goto _exit;
×
717
  }
718

719
  int32_t offset = 0;
×
720
  // int32_t   rowIdx = -1;
721
  for (int32_t i = 0; i < sz; i++) {
×
722
    int64_t     seq = 0;
×
723
    SBlobValue *p = taosArrayGet(pBlobSet->pSeqTable, i);
×
724
    if (p->type == TSDB_DATA_BLOB_EMPTY_VALUE || p->type == TSDB_DATA_BLOB_NULL_VALUE) {
×
725
      // skip empty or null blob
726
      continue;
×
727
    }
728
    code = bseBatchPut(pBatch, &seq, pBlobSet->data + p->offset, p->len);
×
729
    TSDB_CHECK_CODE(code, lino, _exit);
×
730

731
    memcpy(pBlobCol->pData + offset, (void *)&seq, BSE_SEQUECE_SIZE);
×
732
    offset += BSE_SEQUECE_SIZE;
×
733
  }
734

735
  code = bseCommitBatch(pVnode->pBse, pBatch);
×
736
  TSDB_CHECK_CODE(code, lino, _exit);
×
737

738
  int64_t cost = taosGetTimestampUs() - st;
×
739
  if (cost >= 500) {
×
740
    vDebug("vgId:%d, %s, cost:%" PRId64 "us, rows:%d, size:%" PRId64 "", TD_VID(pVnode), __func__, cost, sz,
×
741
           pBlobSet->len);
742
  }
743
_exit:
×
744
  if (code != 0) {
×
745
    vError("vgId:%d %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
746
  }
747
  return code;
×
748
}
749
static int32_t inline vnodeSubmitBlobData(SVnode *pVnode, SSubmitTbData *pData) {
×
750
  int32_t code = 0;
×
751
  if (pData->flags & SUBMIT_REQ_WITH_BLOB) {
×
752
    if (pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
×
753
      code = vnodeSubmitSubColBlobData(pVnode, pData);
×
754
    } else {
755
      code = vnodeSubmitSubRowBlobData(pVnode, pData);
×
756
    }
757
  }
758

759
  return code;
×
760
}
761

762
int32_t vnodeProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg, int64_t ver, SRpcMsg *pRsp) {
14,943,592✔
763
  int32_t code = 0;
14,943,592✔
764
  int32_t lino = 0;
14,943,592✔
765
  void   *ptr = NULL;
14,943,592✔
766
  void   *pReq;
767
  int32_t len;
768

769
  (void)taosThreadMutexLock(&pVnode->mutex);
14,943,592✔
770
  if (pVnode->disableWrite) {
14,943,935!
771
    (void)taosThreadMutexUnlock(&pVnode->mutex);
×
772
    vError("vgId:%d write is disabled for snapshot, version:%" PRId64, TD_VID(pVnode), ver);
×
773
    return TSDB_CODE_VND_WRITE_DISABLED;
×
774
  }
775
  (void)taosThreadMutexUnlock(&pVnode->mutex);
14,943,935✔
776

777
  if (ver <= pVnode->state.applied) {
14,943,912!
778
    vError("vgId:%d, duplicate write request. ver: %" PRId64 ", applied: %" PRId64, TD_VID(pVnode), ver,
×
779
           pVnode->state.applied);
780
    return terrno = TSDB_CODE_VND_DUP_REQUEST;
×
781
  }
782

783
  vGDebug(&pMsg->info.traceId,
14,943,912!
784
          "vgId:%d, index:%" PRId64 ", process write request:%s, applied:%" PRId64 ", state.applyTerm:%" PRId64
785
          ", conn.applyTerm:%" PRId64 ", contLen:%d",
786
          TD_VID(pVnode), ver, TMSG_INFO(pMsg->msgType), pVnode->state.applied, pVnode->state.applyTerm,
787
          pMsg->info.conn.applyTerm, pMsg->contLen);
788

789
  if (!(pVnode->state.applyTerm <= pMsg->info.conn.applyTerm)) {
14,943,684!
790
    vError("vgId:%d, applyTerm mismatch, expected: %" PRId64 ", received: %" PRId64, TD_VID(pVnode),
×
791
           pVnode->state.applyTerm, pMsg->info.conn.applyTerm);
792
    return terrno = TSDB_CODE_INTERNAL_ERROR;
×
793
  }
794

795
  if (!(pVnode->state.applied + 1 == ver)) {
14,943,684!
796
    vError("vgId:%d, mountVgId:%d, ver mismatch, expected: %" PRId64 ", received: %" PRId64, TD_VID(pVnode),
×
797
           pVnode->config.mountVgId, pVnode->state.applied + 1, ver);
798
    return terrno = TSDB_CODE_INTERNAL_ERROR;
×
799
  }
800

801
  atomic_store_64(&pVnode->state.applied, ver);
14,943,684✔
802
  atomic_store_64(&pVnode->state.applyTerm, pMsg->info.conn.applyTerm);
14,943,952✔
803

804
  if (!syncUtilUserCommit(pMsg->msgType)) goto _exit;
14,943,962✔
805

806
  // skip header
807
  pReq = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
14,902,827✔
808
  len = pMsg->contLen - sizeof(SMsgHead);
14,902,827✔
809
  bool needCommit = false;
14,902,827✔
810

811
  switch (pMsg->msgType) {
14,902,827!
812
    /* META */
813
    case TDMT_VND_CREATE_STB:
32,519✔
814
      code = vnodeProcessCreateStbReq(pVnode, ver, pReq, len, pRsp);
32,519✔
815
      TSDB_CHECK_CODE(code, lino, _err);
32,484✔
816
      break;
32,477✔
817
    case TDMT_VND_ALTER_STB:
16,840✔
818
      code = vnodeProcessAlterStbReq(pVnode, ver, pReq, len, pRsp);
16,840✔
819
      TSDB_CHECK_CODE(code, lino, _err);
16,840!
820
      break;
16,840✔
821
    case TDMT_VND_DROP_STB:
4,384✔
822
      code = vnodeProcessDropStbReq(pVnode, ver, pReq, len, pRsp);
4,384✔
823
      TSDB_CHECK_CODE(code, lino, _err);
4,393!
824
      break;
4,393✔
825
    case TDMT_VND_CREATE_TABLE:
100,281✔
826
      code = vnodeProcessCreateTbReq(pVnode, ver, pReq, len, pRsp, pMsg);
100,281✔
827
      TSDB_CHECK_CODE(code, lino, _err);
100,285!
828
      break;
100,285✔
829
    case TDMT_VND_ALTER_TABLE:
20,340✔
830
      code = vnodeProcessAlterTbReq(pVnode, ver, pReq, len, pRsp);
20,340✔
831
      TSDB_CHECK_CODE(code, lino, _err);
20,340!
832
      break;
20,340✔
833
    case TDMT_VND_DROP_TABLE:
15,894✔
834
      code = vnodeProcessDropTbReq(pVnode, ver, pReq, len, pRsp, pMsg);
15,894✔
835
      TSDB_CHECK_CODE(code, lino, _err);
15,894!
836
      break;
15,894✔
837
    case TDMT_VND_DROP_TTL_TABLE:
×
838
      code = vnodeProcessDropTtlTbReq(pVnode, ver, pReq, len, pRsp);
×
839
      TSDB_CHECK_CODE(code, lino, _err);
×
840
      break;
×
841
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
16✔
842
      code = vnodeProcessFetchTtlExpiredTbs(pVnode, ver, pReq, len, pRsp);
16✔
843
      TSDB_CHECK_CODE(code, lino, _err);
16!
844
      break;
16✔
845
    case TDMT_VND_TRIM:
180✔
846
      code = vnodeProcessTrimReq(pVnode, ver, pReq, len, pRsp);
180✔
847
      TSDB_CHECK_CODE(code, lino, _err);
180!
848
      break;
180✔
849
    case TDMT_VND_SSMIGRATE:
×
850
      if (vnodeProcessSsMigrateReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
851
      break;
×
852
    case TDMT_VND_FOLLOWER_SSMIGRATE:
×
853
      if (vnodeProcessFollowerSsMigrateReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
854
      break;
×
855
      break;
856
    /* TSDB */
857
    case TDMT_VND_SUBMIT: {
14,580,538✔
858
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.apply_time, METRIC_LEVEL_LOW, {
14,580,538!
859
        if (vnodeProcessSubmitReq(pVnode, ver, pMsg->pCont, pMsg->contLen, pRsp, pMsg) < 0) goto _err;
860
      });
861
      METRICS_UPDATE(pVnode->writeMetrics.apply_bytes, METRIC_LEVEL_LOW, (int64_t)pMsg->contLen);
14,580,508!
862
      break;
14,580,526✔
863
    }
864
    case TDMT_VND_DELETE:
65,874✔
865
      code = vnodeProcessDeleteReq(pVnode, ver, pReq, len, pRsp, pMsg);
65,874✔
866
      TSDB_CHECK_CODE(code, lino, _err);
65,871!
867
      break;
65,871✔
868
    case TDMT_VND_BATCH_DEL:
×
869
      code = vnodeProcessBatchDeleteReq(pVnode, ver, pReq, len, pRsp);
×
870
      TSDB_CHECK_CODE(code, lino, _err);
×
871
      break;
×
872
      /* TQ */
873
#if defined(USE_TQ) || defined(USE_STREAM)
874
    case TDMT_VND_TMQ_SUBSCRIBE:
2,977✔
875
      code = tqProcessSubscribeReq(pVnode->pTq, ver, pReq, len);
2,977✔
876
      TSDB_CHECK_CODE(code, lino, _err);
2,976!
877
      break;
2,976✔
878
    case TDMT_VND_TMQ_DELETE_SUB:
802✔
879
      code = tqProcessDeleteSubReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen);
802✔
880
      TSDB_CHECK_CODE(code, lino, _err);
802!
881
      break;
802✔
882
    case TDMT_VND_TMQ_COMMIT_OFFSET:
10,309✔
883
      code = tqProcessOffsetCommitReq(pVnode->pTq, ver, pReq, len);
10,309✔
884
      TSDB_CHECK_CODE(code, lino, _err);
10,309!
885
      break;
10,309✔
886
    case TDMT_VND_TMQ_ADD_CHECKINFO:
118✔
887
      code = tqProcessAddCheckInfoReq(pVnode->pTq, ver, pReq, len);
118✔
888
      TSDB_CHECK_CODE(code, lino, _err);
118!
889
      break;
118✔
890
    case TDMT_VND_TMQ_DEL_CHECKINFO:
11✔
891
      code = tqProcessDelCheckInfoReq(pVnode->pTq, ver, pReq, len);
11✔
892
      TSDB_CHECK_CODE(code, lino, _err);
11!
893
      break;
11✔
894
#endif
895
    case TDMT_VND_ALTER_CONFIRM:
15,227✔
896
      needCommit = pVnode->config.hashChange;
15,227✔
897
      code = vnodeProcessAlterConfirmReq(pVnode, ver, pReq, len, pRsp);
15,227✔
898
      TSDB_CHECK_CODE(code, lino, _err);
15,227!
899
      break;
15,227✔
900
    case TDMT_VND_ALTER_CONFIG:
1,198✔
901
      vnodeProcessAlterConfigReq(pVnode, ver, pReq, len, pRsp);
1,198✔
902
      break;
1,198✔
903
    case TDMT_VND_COMMIT:
30,843✔
904
      needCommit = true;
30,843✔
905
      break;
30,843✔
906
    case TDMT_VND_CREATE_INDEX:
1,300✔
907
      vnodeProcessCreateIndexReq(pVnode, ver, pReq, len, pRsp);
1,300✔
908
      break;
1,300✔
909
    case TDMT_VND_DROP_INDEX:
2,986✔
910
      vnodeProcessDropIndexReq(pVnode, ver, pReq, len, pRsp);
2,986✔
911
      break;
2,986✔
912
    case TDMT_VND_COMPACT:
174✔
913
      vnodeProcessCompactVnodeReq(pVnode, ver, pReq, len, pRsp);
174✔
914
      goto _exit;
174✔
915
    case TDMT_SYNC_CONFIG_CHANGE:
×
916
      vnodeProcessConfigChangeReq(pVnode, ver, pReq, len, pRsp);
×
917
      break;
×
918
#ifdef TD_ENTERPRISE
919
    case TDMT_VND_KILL_COMPACT:
2✔
920
      vnodeProcessKillCompactReq(pVnode, ver, pReq, len, pRsp);
2✔
921
      break;
2✔
922
#endif
923
    /* ARB */
924
    case TDMT_VND_ARB_CHECK_SYNC:
14✔
925
      vnodeProcessArbCheckSyncReq(pVnode, pReq, len, pRsp);
14✔
926
      break;
14✔
927
    default:
×
928
      vError("vgId:%d, unprocessed msg, %d", TD_VID(pVnode), pMsg->msgType);
×
929
      return TSDB_CODE_INVALID_MSG;
×
930
  }
931

932
  vGDebug(&pMsg->info.traceId, "vgId:%d, index:%" PRId64 ", msg processed, code:0x%x", TD_VID(pVnode), ver, pRsp->code);
14,902,608!
933

934
  walApplyVer(pVnode->pWal, ver);
14,902,653✔
935

936
  if (pVnode->pTq) {
14,902,563!
937
    code = tqPushMsg(pVnode->pTq, pMsg->msgType);
14,902,636✔
938
    if (code) {
14,902,716!
939
      vError("vgId:%d, failed to push msg to TQ since %s", TD_VID(pVnode), tstrerror(terrno));
×
940
      return code;
×
941
    }
942
  }
943

944
  // commit if need
945
  if (needCommit) {
14,902,689✔
946
    vInfo("vgId:%d, commit at version %" PRId64, TD_VID(pVnode), ver);
30,918✔
947
    code = vnodeAsyncCommit(pVnode);
30,936✔
948
    if (code) {
30,937!
949
      vError("vgId:%d, failed to vnode async commit since %s.", TD_VID(pVnode), tstrerror(terrno));
×
950
      goto _err;
×
951
    }
952

953
    // start a new one
954
    METRICS_TIMING_BLOCK(pVnode->writeMetrics.memtable_wait_time, METRIC_LEVEL_LOW, {
30,937!
955
      code = vnodeBegin(pVnode);
956
      if (code) {
957
        vError("vgId:%d, failed to begin vnode since %s.", TD_VID(pVnode), tstrerror(terrno));
958
        goto _err;
959
      }
960
    });
961
  }
962

963
_exit:
14,902,707✔
964
  return 0;
14,943,649✔
965

966
_err:
59✔
967
  if (code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
59✔
968
    vInfo("vgId:%d, process %s request failed since %s, lino 1:%d ver:%" PRId64, TD_VID(pVnode),
7!
969
          TMSG_INFO(pMsg->msgType), tstrerror(code), lino, ver);
970
  } else if (code) {
52!
971
    vError("vgId:%d, process %s request failed since %s, lino 2:%d ver:%" PRId64, TD_VID(pVnode),
×
972
           TMSG_INFO(pMsg->msgType), tstrerror(code), lino, ver);
973
  }
974

975
  return code;
59✔
976
}
977

978
int32_t vnodePreprocessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg) {
9,813,218✔
979
  if (TDMT_SCH_QUERY != pMsg->msgType && TDMT_SCH_MERGE_QUERY != pMsg->msgType) {
9,813,218✔
980
    return 0;
432,852✔
981
  }
982

983
  return qWorkerPreprocessQueryMsg(pVnode->pQuery, pMsg, TDMT_SCH_QUERY == pMsg->msgType);
9,380,366✔
984
}
985

986
int32_t vnodeProcessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
9,820,888✔
987
  vTrace("message in vnode query queue is processing");
9,820,888!
988
  if (pMsg->msgType == TDMT_VND_TMQ_CONSUME && !syncIsReadyForRead(pVnode->sync)) {
9,820,888✔
989
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
1,326✔
990
    return 0;
1,326✔
991
  }
992

993
  if (pMsg->msgType == TDMT_VND_TMQ_CONSUME && !pVnode->restored) {
9,819,550!
994
    vnodeRedirectRpcMsg(pVnode, pMsg, TSDB_CODE_SYN_RESTORING);
×
995
    return 0;
×
996
  }
997

998
  SReadHandle handle = {0};
9,819,550✔
999
  handle.vnode = pVnode;
9,819,550✔
1000
  handle.pMsgCb = &pVnode->msgCb;
9,819,550✔
1001
  handle.pWorkerCb = pInfo->workerCb;
9,819,550✔
1002
  initStorageAPI(&handle.api);
9,819,550✔
1003
  int32_t code = TSDB_CODE_SUCCESS;
9,818,629✔
1004
  bool    redirected = false;
9,818,629✔
1005

1006
  switch (pMsg->msgType) {
9,818,629✔
1007
    case TDMT_SCH_QUERY:
7,236,993✔
1008
      if (!syncIsReadyForRead(pVnode->sync)) {
7,236,993✔
1009
        pMsg->code = (terrno) ? terrno : TSDB_CODE_SYN_NOT_LEADER;
4,456!
1010
        redirected = true;
4,456✔
1011
      }
1012
      code = qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
7,237,301✔
1013
      if (redirected) {
7,235,961✔
1014
        vnodeRedirectRpcMsg(pVnode, pMsg, pMsg->code);
3,632✔
1015
        return 0;
4,456✔
1016
      }
1017

1018
      return code;
7,232,329✔
1019
    case TDMT_SCH_MERGE_QUERY:
2,149,652✔
1020
      return qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
2,149,652✔
1021
    case TDMT_SCH_QUERY_CONTINUE:
372,192✔
1022
      return qWorkerProcessCQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
372,192✔
1023
    case TDMT_VND_TMQ_CONSUME:
46,142✔
1024
      return tqProcessPollReq(pVnode->pTq, pMsg);
46,142✔
1025
    case TDMT_VND_TMQ_CONSUME_PUSH:
13,180✔
1026
      return tqProcessPollPush(pVnode->pTq);
13,180✔
1027
    default:
470✔
1028
      vError("unknown msg type:%d in query queue", pMsg->msgType);
470!
1029
      return TSDB_CODE_APP_ERROR;
×
1030
  }
1031
}
1032

1033
int32_t vnodeProcessFetchMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
24,897,492✔
1034
  int32_t code = TSDB_CODE_SUCCESS;
24,897,492✔
1035
  vTrace("vgId:%d, msg:%p in fetch queue is processing", pVnode->config.vgId, pMsg);
24,897,492!
1036
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
24,897,492!
1037
       pMsg->msgType == TDMT_VND_BATCH_META || pMsg->msgType == TDMT_VND_TABLE_NAME ||
17,297,478!
1038
       pMsg->msgType == TDMT_VND_VSUBTABLES_META || pMsg->msgType == TDMT_VND_VSTB_REF_DBS) &&
15,939,585✔
1039
      !syncIsReadyForRead(pVnode->sync)) {
8,960,863✔
1040
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
3,608✔
1041
    return 0;
3,608✔
1042
  }
1043

1044
  switch (pMsg->msgType) {
24,901,477!
1045
    case TDMT_SCH_FETCH:
11,450,121✔
1046
    case TDMT_SCH_MERGE_FETCH:
1047
      return qWorkerProcessFetchMsg(pVnode, pVnode->pQuery, pMsg, 0);
11,450,121✔
1048
    case TDMT_SCH_FETCH_RSP:
×
1049
      return qWorkerProcessRspMsg(pVnode, pVnode->pQuery, pMsg, 0);
×
1050
    // case TDMT_SCH_CANCEL_TASK:
1051
    //   return qWorkerProcessCancelMsg(pVnode, pVnode->pQuery, pMsg, 0);
1052
    case TDMT_SCH_DROP_TASK:
9,371,497✔
1053
      return qWorkerProcessDropMsg(pVnode, pVnode->pQuery, pMsg, 0);
9,371,497✔
1054
    case TDMT_SCH_TASK_NOTIFY:
95✔
1055
      return qWorkerProcessNotifyMsg(pVnode, pVnode->pQuery, pMsg, 0);
95✔
1056
    case TDMT_SCH_QUERY_HEARTBEAT:
2,719,537✔
1057
      return qWorkerProcessHbMsg(pVnode, pVnode->pQuery, pMsg, 0);
2,719,537✔
1058
    case TDMT_VND_TABLE_META:
5,179✔
1059
    case TDMT_VND_TABLE_NAME:
1060
      return vnodeGetTableMeta(pVnode, pMsg, true);
5,179✔
1061
    case TDMT_VND_TABLE_CFG:
×
1062
      return vnodeGetTableCfg(pVnode, pMsg, true);
×
1063
    case TDMT_VND_BATCH_META: {
1,354,851✔
1064
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.fetch_batch_meta_time, METRIC_LEVEL_LOW,
1,354,851!
1065
                           { code = vnodeGetBatchMeta(pVnode, pMsg); });
1066
      METRICS_UPDATE(pVnode->writeMetrics.fetch_batch_meta_count, METRIC_LEVEL_LOW, 1);
1,355,559!
1067
      return code;
1,355,585✔
1068
    }
1069
    case TDMT_VND_VSUBTABLES_META:
×
1070
      return vnodeGetVSubtablesMeta(pVnode, pMsg);
×
1071
    case TDMT_VND_VSTB_REF_DBS:
×
1072
      return vnodeGetVStbRefDbs(pVnode, pMsg);
×
1073
#ifdef TD_ENTERPRISE
1074
    case TDMT_VND_QUERY_COMPACT_PROGRESS:
187✔
1075
      return vnodeQueryCompactProgress(pVnode, pMsg);
187✔
1076
#endif
1077
    case TDMT_VND_QUERY_SSMIGRATE_PROGRESS:
×
1078
      return vnodeQuerySsMigrateProgress(pVnode, pMsg);
×
1079
      //    case TDMT_VND_TMQ_CONSUME:
1080
      //      return tqProcessPollReq(pVnode->pTq, pMsg);
1081
#ifdef USE_TQ
1082
    case TDMT_VND_TMQ_VG_WALINFO:
3✔
1083
      return tqProcessVgWalInfoReq(pVnode->pTq, pMsg);
3✔
1084
    case TDMT_VND_TMQ_VG_COMMITTEDINFO:
1✔
1085
      return tqProcessVgCommittedInfoReq(pVnode->pTq, pMsg);
1✔
1086
    case TDMT_VND_TMQ_SEEK:
6✔
1087
      return tqProcessSeekReq(pVnode->pTq, pMsg);
6✔
1088
#endif
1089
    default:
×
1090
      vError("unknown msg type:%d in fetch queue", pMsg->msgType);
×
1091
      return TSDB_CODE_APP_ERROR;
×
1092
  }
1093
}
1094

1095
void smaHandleRes(void *pVnode, int64_t smaId, const SArray *data) {
×
1096
  int32_t code = tdProcessTSmaInsert(((SVnode *)pVnode)->pSma, smaId, (const char *)data);
×
1097
  if (code) {
×
1098
    vError("failed to process sma result since %s", tstrerror(code));
×
1099
  }
1100
}
×
1101

1102
void vnodeUpdateMetaRsp(SVnode *pVnode, STableMetaRsp *pMetaRsp) {
221,126✔
1103
  if (NULL == pMetaRsp) {
221,126!
1104
    return;
×
1105
  }
1106

1107
  tstrncpy(pMetaRsp->dbFName, pVnode->config.dbname, TSDB_DB_FNAME_LEN);
221,126✔
1108
  pMetaRsp->dbId = pVnode->config.dbId;
221,126✔
1109
  pMetaRsp->vgId = TD_VID(pVnode);
221,126✔
1110
  pMetaRsp->precision = pVnode->config.tsdbCfg.precision;
221,126✔
1111
}
1112

1113
extern int32_t vnodeAsyncRetention(SVnode *pVnode, int64_t now);
1114

1115
static int32_t vnodeProcessTrimReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
180✔
1116
  if (!pVnode->restored) {
180✔
1117
    vInfo("vgId:%d, ignore trim req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
30!
1118
    return 0;
30✔
1119
  }
1120

1121
  int32_t     code = 0;
150✔
1122
  SVTrimDbReq trimReq = {0};
150✔
1123

1124
  // decode
1125
  if (tDeserializeSVTrimDbReq(pReq, len, &trimReq) != 0) {
150!
1126
    code = TSDB_CODE_INVALID_MSG;
×
1127
    goto _exit;
×
1128
  }
1129

1130
  vInfo("vgId:%d, process trim vnode request, time:%d", pVnode->config.vgId, trimReq.timestamp);
150!
1131

1132
  code = vnodeAsyncRetention(pVnode, trimReq.timestamp);
150✔
1133

1134
_exit:
150✔
1135
  return code;
150✔
1136
}
1137

1138
extern int32_t vnodeAsyncSsMigrate(SVnode *pVnode, SSsMigrateVgroupReq *pReq);
1139

1140
static int32_t vnodeProcessSsMigrateReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1141
  int32_t             code = 0;
×
1142
  SSsMigrateVgroupReq req = {0};
×
1143
  SSsMigrateVgroupRsp rsp = {0};
×
1144
  pRsp->msgType = TDMT_VND_SSMIGRATE_RSP;
×
1145
  pRsp->code = 0;
×
1146
  pRsp->pCont = NULL;
×
1147
  pRsp->contLen = 0;
×
1148

1149
  if (tDeserializeSSsMigrateVgroupReq(pReq, len, &req) != 0) {
×
1150
    code = TSDB_CODE_INVALID_MSG;
×
1151
    goto _exit;
×
1152
  }
1153

1154
  vInfo("vgId:%d, process ssmigrate vnode request, time:%" PRId64, pVnode->config.vgId, req.timestamp);
×
1155

1156
  code = vnodeAsyncSsMigrate(pVnode, &req);
×
1157
  if (code != TSDB_CODE_SUCCESS) {
×
1158
    vError("vgId:%d, failed to async ssmigrate since %s", TD_VID(pVnode), tstrerror(code));
×
1159
    pRsp->code = code;
×
1160
    goto _exit;
×
1161
  }
1162

1163
  rsp.ssMigrateId = req.ssMigrateId;
×
1164
  rsp.vgId = TD_VID(pVnode);
×
1165
  rsp.nodeId = req.nodeId;
×
1166

1167
  pRsp->code = TSDB_CODE_SUCCESS;
×
1168
  pRsp->contLen = tSerializeSSsMigrateVgroupRsp(NULL, 0, &rsp);
×
1169
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
×
1170
  if (pRsp->pCont == NULL) {
×
1171
    vError("vgId:%d, failed to allocate memory for ssmigrate response", TD_VID(pVnode));
×
1172
    code = TSDB_CODE_OUT_OF_MEMORY;
×
1173
    goto _exit;
×
1174
  }
1175
  tSerializeSSsMigrateVgroupRsp(pRsp->pCont, pRsp->contLen, &rsp);
×
1176

1177
_exit:
×
1178
  if (code != TSDB_CODE_SUCCESS) {
×
1179
    pRsp->code = code;
×
1180
  }
1181
  return code;
×
1182
}
1183

1184
extern int32_t vnodeFollowerSsMigrate(SVnode *pVnode, SFollowerSsMigrateReq *pReq);
1185

1186
static int32_t vnodeProcessFollowerSsMigrateReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1187
  int32_t               code = 0;
×
1188
  SFollowerSsMigrateReq req = {0};
×
1189

1190
  // decode
1191
  if (tDeserializeSFollowerSsMigrateReq(pReq, len, &req) != 0) {
×
1192
    code = TSDB_CODE_INVALID_MSG;
×
1193
    goto _exit;
×
1194
  }
1195

1196
  code = vnodeFollowerSsMigrate(pVnode, &req);
×
1197

1198
_exit:
×
1199
  tFreeSFollowerSsMigrateReq(&req);
×
1200
  return code;
×
1201
}
1202

1203
static int32_t vnodeProcessDropTtlTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1204
  int               ret = 0;
×
1205
  SVDropTtlTableReq ttlReq = {0};
×
1206
  if (tDeserializeSVDropTtlTableReq(pReq, len, &ttlReq) != 0) {
×
1207
    ret = TSDB_CODE_INVALID_MSG;
×
1208
    goto end;
×
1209
  }
1210

1211
  if (ttlReq.nUids != taosArrayGetSize(ttlReq.pTbUids)) {
×
1212
    ret = TSDB_CODE_INVALID_MSG;
×
1213
    goto end;
×
1214
  }
1215

1216
  if (ttlReq.nUids != 0) {
×
1217
    vInfo("vgId:%d, process drop ttl table request, time:%d, ntbUids:%d", pVnode->config.vgId, ttlReq.timestampSec,
×
1218
          ttlReq.nUids);
1219
  }
1220

1221
  if (ttlReq.nUids > 0) {
×
1222
    int32_t code = metaDropMultipleTables(pVnode->pMeta, ver, ttlReq.pTbUids);
×
1223
    if (code) return code;
×
1224

1225
    code = tqUpdateTbUidList(pVnode->pTq, ttlReq.pTbUids, false);
×
1226
    if (code) {
×
1227
      vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1228
    }
1229
  }
1230

1231
end:
×
1232
  taosArrayDestroy(ttlReq.pTbUids);
×
1233
  return ret;
×
1234
}
1235

1236
static int32_t vnodeProcessFetchTtlExpiredTbs(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
16✔
1237
  int32_t                 code = -1;
16✔
1238
  SMetaReader             mr = {0};
16✔
1239
  SVDropTtlTableReq       ttlReq = {0};
16✔
1240
  SVFetchTtlExpiredTbsRsp rsp = {0};
16✔
1241
  SEncoder                encoder = {0};
16✔
1242
  SArray                 *pNames = NULL;
16✔
1243
  pRsp->msgType = TDMT_VND_FETCH_TTL_EXPIRED_TBS_RSP;
16✔
1244
  pRsp->code = TSDB_CODE_SUCCESS;
16✔
1245
  pRsp->pCont = NULL;
16✔
1246
  pRsp->contLen = 0;
16✔
1247

1248
  if (tDeserializeSVDropTtlTableReq(pReq, len, &ttlReq) != 0) {
16!
1249
    terrno = TSDB_CODE_INVALID_MSG;
×
1250
    goto _end;
×
1251
  }
1252

1253
  if (!(ttlReq.nUids == taosArrayGetSize(ttlReq.pTbUids))) {
16!
1254
    terrno = TSDB_CODE_INVALID_MSG;
×
1255
    goto _end;
×
1256
  }
1257

1258
  tb_uid_t    suid;
1259
  char        ctbName[TSDB_TABLE_NAME_LEN];
1260
  SVDropTbReq expiredTb = {.igNotExists = true};
16✔
1261
  metaReaderDoInit(&mr, pVnode->pMeta, 0);
16✔
1262
  rsp.vgId = TD_VID(pVnode);
16✔
1263
  rsp.pExpiredTbs = taosArrayInit(ttlReq.nUids, sizeof(SVDropTbReq));
16✔
1264
  if (!rsp.pExpiredTbs) goto _end;
16!
1265

1266
  pNames = taosArrayInit(ttlReq.nUids, TSDB_TABLE_NAME_LEN);
16✔
1267
  if (!pNames) {
16!
1268
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1269
    goto _end;
×
1270
  }
1271
  char buf[TSDB_TABLE_NAME_LEN];
1272
  for (int32_t i = 0; i < ttlReq.nUids; ++i) {
79✔
1273
    tb_uid_t *uid = taosArrayGet(ttlReq.pTbUids, i);
63✔
1274
    expiredTb.suid = *uid;
63✔
1275
    terrno = metaReaderGetTableEntryByUid(&mr, *uid);
63✔
1276
    if (terrno < 0) goto _end;
63!
1277
    tstrncpy(buf, mr.me.name, TSDB_TABLE_NAME_LEN);
63✔
1278
    void *p = taosArrayPush(pNames, buf);
63✔
1279
    if (p == NULL) {
63!
1280
      goto _end;
×
1281
    }
1282

1283
    expiredTb.name = p;
63✔
1284
    if (mr.me.type == TSDB_CHILD_TABLE) {
63✔
1285
      expiredTb.suid = mr.me.ctbEntry.suid;
38✔
1286
    }
1287

1288
    if (taosArrayPush(rsp.pExpiredTbs, &expiredTb) == NULL) {
126!
1289
      goto _end;
×
1290
    }
1291
  }
1292

1293
  int32_t ret = 0;
16✔
1294
  tEncodeSize(tEncodeVFetchTtlExpiredTbsRsp, &rsp, pRsp->contLen, ret);
16!
1295
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
16✔
1296
  if (pRsp->pCont == NULL) {
16!
1297
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1298
    code = -1;
×
1299
    goto _end;
×
1300
  }
1301
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
16✔
1302
  terrno = tEncodeVFetchTtlExpiredTbsRsp(&encoder, &rsp);
16✔
1303
  tEncoderClear(&encoder);
16✔
1304

1305
  if (terrno == 0) code = 0;
16!
1306
_end:
×
1307
  metaReaderClear(&mr);
16✔
1308
  tFreeFetchTtlExpiredTbsRsp(&rsp);
16✔
1309
  taosArrayDestroy(ttlReq.pTbUids);
16✔
1310
  if (pNames) taosArrayDestroy(pNames);
16!
1311
  pRsp->code = terrno;
16✔
1312
  return code;
16✔
1313
}
1314

1315
static int32_t vnodeProcessCreateStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
32,473✔
1316
  int32_t        code = 0;
32,473✔
1317
  SVCreateStbReq req = {0};
32,473✔
1318
  SDecoder       coder;
1319

1320
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
32,473✔
1321
  pRsp->code = TSDB_CODE_SUCCESS;
32,473✔
1322
  pRsp->pCont = NULL;
32,473✔
1323
  pRsp->contLen = 0;
32,473✔
1324

1325
  // decode and process req
1326
  tDecoderInit(&coder, pReq, len);
32,473✔
1327

1328
  code = tDecodeSVCreateStbReq(&coder, &req);
32,514✔
1329
  if (code) {
32,456!
1330
    pRsp->code = code;
×
1331
    goto _err;
×
1332
  }
1333

1334
  code = metaCreateSuperTable(pVnode->pMeta, ver, &req);
32,456✔
1335
  if (code) {
32,539✔
1336
    pRsp->code = code;
7✔
1337
    goto _err;
7✔
1338
  }
1339

1340
  if ((code = tdProcessRSmaCreate(pVnode->pSma, &req)) < 0) {
32,532!
1341
    pRsp->code = code;
×
1342
    goto _err;
×
1343
  }
1344

1345
  tDecoderClear(&coder);
32,513✔
1346
  return 0;
32,451✔
1347

1348
_err:
7✔
1349
  tDecoderClear(&coder);
7✔
1350
  return code;
7✔
1351
}
1352

1353
static int32_t vnodeProcessCreateTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
100,272✔
1354
                                       SRpcMsg *pOriginRpc) {
1355
  SDecoder           decoder = {0};
100,272✔
1356
  SEncoder           encoder = {0};
100,272✔
1357
  int32_t            rcode = 0;
100,272✔
1358
  SVCreateTbBatchReq req = {0};
100,272✔
1359
  SVCreateTbReq     *pCreateReq;
1360
  SVCreateTbBatchRsp rsp = {0};
100,272✔
1361
  SVCreateTbRsp      cRsp = {0};
100,272✔
1362
  char               tbName[TSDB_TABLE_FNAME_LEN];
1363
  STbUidStore       *pStore = NULL;
100,272✔
1364
  SArray            *tbUids = NULL;
100,272✔
1365
  SArray            *tbNames = NULL;
100,272✔
1366
  pRsp->msgType = TDMT_VND_CREATE_TABLE_RSP;
100,272✔
1367
  pRsp->code = TSDB_CODE_SUCCESS;
100,272✔
1368
  pRsp->pCont = NULL;
100,272✔
1369
  pRsp->contLen = 0;
100,272✔
1370

1371
  // decode
1372
  tDecoderInit(&decoder, pReq, len);
100,272✔
1373
  if (tDecodeSVCreateTbBatchReq(&decoder, &req) < 0) {
100,267!
1374
    rcode = -1;
×
1375
    terrno = TSDB_CODE_INVALID_MSG;
×
1376
    goto _exit;
×
1377
  }
1378

1379
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(cRsp));
100,274✔
1380
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
100,288✔
1381
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
100,289✔
1382
  if (rsp.pArray == NULL || tbUids == NULL || tbNames == NULL) {
100,288!
1383
    rcode = -1;
1✔
1384
    terrno = TSDB_CODE_OUT_OF_MEMORY;
1✔
1385
    goto _exit;
×
1386
  }
1387

1388
  // loop to create table
1389
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
248,478✔
1390
    pCreateReq = req.pReqs + iReq;
148,183✔
1391
    memset(&cRsp, 0, sizeof(cRsp));
148,183✔
1392

1393
    if (tsEnableAudit && tsEnableAuditCreateTable) {
148,183!
1394
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
148,187!
1395
      if (str == NULL) {
148,176!
1396
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1397
        rcode = -1;
×
1398
        goto _exit;
×
1399
      }
1400
      tstrncpy(str, pCreateReq->name, TSDB_TABLE_FNAME_LEN);
148,176✔
1401
      if (taosArrayPush(tbNames, &str) == NULL) {
148,188!
1402
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1403
        rcode = -1;
×
1404
        goto _exit;
×
1405
      }
1406
    }
1407

1408
    // validate hash
1409
    (void)tsnprintf(tbName, TSDB_TABLE_FNAME_LEN, "%s.%s", pVnode->config.dbname, pCreateReq->name);
148,184✔
1410
    if (vnodeValidateTableHash(pVnode, tbName) < 0) {
148,185!
1411
      cRsp.code = TSDB_CODE_VND_HASH_MISMATCH;
×
1412
      if (taosArrayPush(rsp.pArray, &cRsp) == NULL) {
×
1413
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1414
        rcode = -1;
×
1415
        goto _exit;
×
1416
      }
1417
      vError("vgId:%d create-table:%s failed due to hash value mismatch", TD_VID(pVnode), tbName);
×
1418
      continue;
×
1419
    }
1420

1421
    // do create table
1422
    if (metaCreateTable2(pVnode->pMeta, ver, pCreateReq, &cRsp.pMeta) < 0) {
148,172✔
1423
      if (pCreateReq->flags & TD_CREATE_IF_NOT_EXISTS && terrno == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
144!
1424
        cRsp.code = TSDB_CODE_SUCCESS;
112✔
1425
      } else {
1426
        cRsp.code = terrno;
32✔
1427
      }
1428
    } else {
1429
      cRsp.code = TSDB_CODE_SUCCESS;
148,052✔
1430
      if (tdFetchTbUidList(pVnode->pSma, &pStore, pCreateReq->ctb.suid, pCreateReq->uid) < 0) {
148,052!
1431
        vError("vgId:%d, failed to fetch tbUid list", TD_VID(pVnode));
×
1432
      }
1433
      if (taosArrayPush(tbUids, &pCreateReq->uid) == NULL) {
296,105!
1434
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1435
        rcode = -1;
×
1436
        goto _exit;
×
1437
      }
1438
      vnodeUpdateMetaRsp(pVnode, cRsp.pMeta);
148,052✔
1439
    }
1440

1441
    if (taosArrayPush(rsp.pArray, &cRsp) == NULL) {
296,384!
1442
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1443
      rcode = -1;
×
1444
      goto _exit;
×
1445
    }
1446
  }
1447

1448
  vTrace("vgId:%d, add %d new created tables into query table list", TD_VID(pVnode), (int32_t)taosArrayGetSize(tbUids));
100,295✔
1449
  if (tqUpdateTbUidList(pVnode->pTq, tbUids, true) < 0) {
100,295!
1450
    vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(terrno));
×
1451
  }
1452
  if (tdUpdateTbUidList(pVnode->pSma, pStore, true) < 0) {
100,291!
1453
    goto _exit;
×
1454
  }
1455
  pStore = tdUidStoreFree(pStore);
100,291✔
1456

1457
  // prepare rsp
1458
  int32_t ret = 0;
100,290✔
1459
  tEncodeSize(tEncodeSVCreateTbBatchRsp, &rsp, pRsp->contLen, ret);
100,290!
1460
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
100,287✔
1461
  if (pRsp->pCont == NULL) {
100,287!
1462
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1463
    rcode = -1;
×
1464
    goto _exit;
×
1465
  }
1466
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
100,287✔
1467
  if (tEncodeSVCreateTbBatchRsp(&encoder, &rsp) != 0) {
100,287!
1468
    vError("vgId:%d, failed to encode create table batch response", TD_VID(pVnode));
×
1469
  }
1470

1471
  if (tsEnableAudit && tsEnableAuditCreateTable) {
100,283!
1472
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
100,284✔
1473

1474
    SName name = {0};
100,284✔
1475
    if (tNameFromString(&name, pVnode->config.dbname, T_NAME_ACCT | T_NAME_DB) < 0) {
100,284!
1476
      vError("vgId:%d, failed to get name from string", TD_VID(pVnode));
×
1477
    }
1478

1479
    SStringBuilder sb = {0};
100,283✔
1480
    for (int32_t i = 0; i < tbNames->size; i++) {
248,470✔
1481
      char **key = (char **)taosArrayGet(tbNames, i);
148,184✔
1482
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
148,182✔
1483
      if (i < tbNames->size - 1) {
148,185✔
1484
        taosStringBuilderAppendChar(&sb, ',');
47,902✔
1485
      }
1486
      // taosMemoryFreeClear(*key);
1487
    }
1488

1489
    size_t len = 0;
100,286✔
1490
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
100,286✔
1491

1492
    if (pOriginRpc->info.conn.user != NULL && strlen(pOriginRpc->info.conn.user) > 0) {
100,283!
1493
      auditAddRecord(pOriginRpc, clusterId, "createTable", name.dbname, "", keyJoined, len);
89,083✔
1494
    }
1495

1496
    taosStringBuilderDestroy(&sb);
100,283✔
1497
  }
1498

1499
_exit:
×
1500
  tDeleteSVCreateTbBatchReq(&req);
100,278✔
1501
  taosArrayDestroyEx(rsp.pArray, tFreeSVCreateTbRsp);
100,289✔
1502
  taosArrayDestroy(tbUids);
100,291✔
1503
  tDecoderClear(&decoder);
100,292✔
1504
  tEncoderClear(&encoder);
100,291✔
1505
  taosArrayDestroyP(tbNames, NULL);
100,290✔
1506
  return rcode;
100,292✔
1507
}
1508

1509
static int32_t vnodeProcessAlterStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
16,840✔
1510
  int32_t        code = 0;
16,840✔
1511
  SVCreateStbReq req = {0};
16,840✔
1512
  SDecoder       dc = {0};
16,840✔
1513

1514
  pRsp->msgType = TDMT_VND_ALTER_STB_RSP;
16,840✔
1515
  pRsp->code = TSDB_CODE_SUCCESS;
16,840✔
1516
  pRsp->pCont = NULL;
16,840✔
1517
  pRsp->contLen = 0;
16,840✔
1518

1519
  tDecoderInit(&dc, pReq, len);
16,840✔
1520

1521
  // decode req
1522
  code = tDecodeSVCreateStbReq(&dc, &req);
16,840✔
1523
  if (code) {
16,839!
1524
    tDecoderClear(&dc);
×
1525
    return code;
×
1526
  }
1527

1528
  code = metaAlterSuperTable(pVnode->pMeta, ver, &req);
16,839✔
1529
  if (code) {
16,840!
1530
    pRsp->code = code;
×
1531
    tDecoderClear(&dc);
×
1532
    return code;
×
1533
  }
1534

1535
  tDecoderClear(&dc);
16,840✔
1536

1537
  return 0;
16,840✔
1538
}
1539

1540
static int32_t vnodeProcessDropStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
4,384✔
1541
  SVDropStbReq req = {0};
4,384✔
1542
  int32_t      rcode = TSDB_CODE_SUCCESS;
4,384✔
1543
  SDecoder     decoder = {0};
4,384✔
1544
  SArray      *tbUidList = NULL;
4,384✔
1545

1546
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
4,384✔
1547
  pRsp->pCont = NULL;
4,384✔
1548
  pRsp->contLen = 0;
4,384✔
1549

1550
  // decode request
1551
  tDecoderInit(&decoder, pReq, len);
4,384✔
1552
  if (tDecodeSVDropStbReq(&decoder, &req) < 0) {
4,383!
1553
    rcode = TSDB_CODE_INVALID_MSG;
×
1554
    goto _exit;
×
1555
  }
1556

1557
  // process request
1558
  tbUidList = taosArrayInit(8, sizeof(int64_t));
4,388✔
1559
  if (tbUidList == NULL) goto _exit;
4,392!
1560
  if (metaDropSuperTable(pVnode->pMeta, ver, &req) < 0) {
4,392✔
1561
    rcode = terrno;
4✔
1562
    goto _exit;
4✔
1563
  }
1564

1565
  if (tqUpdateTbUidList(pVnode->pTq, tbUidList, false) < 0) {
4,389!
1566
    rcode = terrno;
×
1567
    goto _exit;
×
1568
  }
1569

1570
  if (tdProcessRSmaDrop(pVnode->pSma, &req) < 0) {
4,389!
1571
    rcode = terrno;
×
1572
    goto _exit;
×
1573
  }
1574

1575
  // return rsp
1576
_exit:
4,388✔
1577
  if (tbUidList) taosArrayDestroy(tbUidList);
4,392!
1578
  pRsp->code = rcode;
4,393✔
1579
  tDecoderClear(&decoder);
4,393✔
1580
  return 0;
4,393✔
1581
}
1582

1583
static int32_t vnodeProcessAlterTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
20,340✔
1584
  SVAlterTbReq  vAlterTbReq = {0};
20,340✔
1585
  SVAlterTbRsp  vAlterTbRsp = {0};
20,340✔
1586
  SDecoder      dc = {0};
20,340✔
1587
  int32_t       code = 0;
20,340✔
1588
  int32_t       lino = 0;
20,340✔
1589
  int32_t       ret;
1590
  SEncoder      ec = {0};
20,340✔
1591
  STableMetaRsp vMetaRsp = {0};
20,340✔
1592

1593
  pRsp->msgType = TDMT_VND_ALTER_TABLE_RSP;
20,340✔
1594
  pRsp->pCont = NULL;
20,340✔
1595
  pRsp->contLen = 0;
20,340✔
1596
  pRsp->code = TSDB_CODE_SUCCESS;
20,340✔
1597

1598
  tDecoderInit(&dc, pReq, len);
20,340✔
1599

1600
  // decode
1601
  if (tDecodeSVAlterTbReq(&dc, &vAlterTbReq) < 0) {
20,340!
1602
    vAlterTbRsp.code = TSDB_CODE_INVALID_MSG;
×
1603
    tDecoderClear(&dc);
×
1604
    goto _exit;
×
1605
  }
1606

1607
  // process
1608
  if (metaAlterTable(pVnode->pMeta, ver, &vAlterTbReq, &vMetaRsp) < 0) {
20,340✔
1609
    vAlterTbRsp.code = terrno;
915✔
1610
    tDecoderClear(&dc);
915✔
1611
    goto _exit;
915✔
1612
  }
1613
  tDecoderClear(&dc);
19,425✔
1614

1615
  if (NULL != vMetaRsp.pSchemas) {
19,425✔
1616
    vnodeUpdateMetaRsp(pVnode, &vMetaRsp);
5,574✔
1617
    vAlterTbRsp.pMeta = &vMetaRsp;
5,574✔
1618
  }
1619

1620
  if (vAlterTbReq.action == TSDB_ALTER_TABLE_UPDATE_TAG_VAL ||
19,425✔
1621
      vAlterTbReq.action == TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL) {
5,684✔
1622
    int64_t uid = metaGetTableEntryUidByName(pVnode->pMeta, vAlterTbReq.tbName);
13,751✔
1623
    if (uid == 0) {
13,751!
1624
      vError("vgId:%d, %s failed at %s:%d since table %s not found", TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
1625
             vAlterTbReq.tbName);
1626
      goto _exit;
×
1627
    }
1628

1629
    SArray *tbUids = taosArrayInit(4, sizeof(int64_t));
13,751✔
1630
    void   *p = taosArrayPush(tbUids, &uid);
13,751✔
1631
    TSDB_CHECK_NULL(p, code, lino, _exit, terrno);
13,751!
1632

1633
    vDebug("vgId:%d, remove tags value altered table:%s from query table list", TD_VID(pVnode), vAlterTbReq.tbName);
13,751✔
1634
    if ((code = tqUpdateTbUidList(pVnode->pTq, tbUids, false)) < 0) {
13,751!
1635
      vError("vgId:%d, failed to remove tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1636
    }
1637

1638
    vDebug("vgId:%d, try to add table:%s in query table list", TD_VID(pVnode), vAlterTbReq.tbName);
13,751✔
1639
    if ((code = tqUpdateTbUidList(pVnode->pTq, tbUids, true)) < 0) {
13,751!
1640
      vError("vgId:%d, failed to add tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1641
    }
1642

1643
    taosArrayDestroy(tbUids);
13,751✔
1644
  }
1645

1646
_exit:
5,674✔
1647
  taosArrayDestroy(vAlterTbReq.pMultiTag);
20,340✔
1648
  tEncodeSize(tEncodeSVAlterTbRsp, &vAlterTbRsp, pRsp->contLen, ret);
20,340!
1649
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
20,340✔
1650
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
20,340✔
1651
  if (tEncodeSVAlterTbRsp(&ec, &vAlterTbRsp) != 0) {
20,340!
1652
    vError("vgId:%d, failed to encode alter table response", TD_VID(pVnode));
×
1653
  }
1654

1655
  tEncoderClear(&ec);
20,340✔
1656
  if (vMetaRsp.pSchemas) {
20,340✔
1657
    taosMemoryFree(vMetaRsp.pSchemas);
5,574!
1658
    taosMemoryFree(vMetaRsp.pSchemaExt);
5,574!
1659
  }
1660
  if (vMetaRsp.pColRefs) {
20,340✔
1661
    taosMemoryFree(vMetaRsp.pColRefs);
339!
1662
  }
1663
  return 0;
20,340✔
1664
}
1665

1666
static int32_t vnodeProcessDropTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
15,894✔
1667
                                     SRpcMsg *pOriginRpc) {
1668
  SVDropTbBatchReq req = {0};
15,894✔
1669
  SVDropTbBatchRsp rsp = {0};
15,894✔
1670
  SDecoder         decoder = {0};
15,894✔
1671
  SEncoder         encoder = {0};
15,894✔
1672
  int32_t          ret;
1673
  SArray          *tbUids = NULL;
15,894✔
1674
  STbUidStore     *pStore = NULL;
15,894✔
1675
  SArray          *tbNames = NULL;
15,894✔
1676

1677
  pRsp->msgType = TDMT_VND_DROP_TABLE_RSP;
15,894✔
1678
  pRsp->pCont = NULL;
15,894✔
1679
  pRsp->contLen = 0;
15,894✔
1680
  pRsp->code = TSDB_CODE_SUCCESS;
15,894✔
1681

1682
  // decode req
1683
  tDecoderInit(&decoder, pReq, len);
15,894✔
1684
  ret = tDecodeSVDropTbBatchReq(&decoder, &req);
15,894✔
1685
  if (ret < 0) {
15,894!
1686
    terrno = TSDB_CODE_INVALID_MSG;
×
1687
    pRsp->code = terrno;
×
1688
    goto _exit;
×
1689
  }
1690

1691
  // process req
1692
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
15,894✔
1693
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(SVDropTbRsp));
15,894✔
1694
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
15,894✔
1695
  if (tbUids == NULL || rsp.pArray == NULL || tbNames == NULL) goto _exit;
15,894!
1696

1697
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
32,189✔
1698
    SVDropTbReq *pDropTbReq = req.pReqs + iReq;
16,295✔
1699
    SVDropTbRsp  dropTbRsp = {0};
16,295✔
1700
    tb_uid_t     tbUid = 0;
16,295✔
1701

1702
    /* code */
1703
    ret = metaDropTable2(pVnode->pMeta, ver, pDropTbReq);
16,295✔
1704
    if (ret < 0) {
16,295!
1705
      if (pDropTbReq->igNotExists && terrno == TSDB_CODE_TDB_TABLE_NOT_EXIST) {
×
1706
        dropTbRsp.code = TSDB_CODE_SUCCESS;
×
1707
      } else {
1708
        dropTbRsp.code = terrno;
×
1709
      }
1710
    } else {
1711
      dropTbRsp.code = TSDB_CODE_SUCCESS;
16,295✔
1712
      if (tbUid > 0) {
16,295!
1713
        if (tdFetchTbUidList(pVnode->pSma, &pStore, pDropTbReq->suid, tbUid) < 0) {
×
1714
          vError("vgId:%d, failed to fetch tbUid list", TD_VID(pVnode));
×
1715
        }
1716
      }
1717
    }
1718

1719
    if (taosArrayPush(rsp.pArray, &dropTbRsp) == NULL) {
32,590!
1720
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1721
      pRsp->code = terrno;
×
1722
      goto _exit;
×
1723
    }
1724

1725
    if (tsEnableAuditCreateTable) {
16,295!
1726
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
16,295!
1727
      if (str == NULL) {
16,295!
1728
        pRsp->code = terrno;
×
1729
        goto _exit;
×
1730
      }
1731
      tstrncpy(str, pDropTbReq->name, TSDB_TABLE_FNAME_LEN);
16,295✔
1732
      if (taosArrayPush(tbNames, &str) == NULL) {
16,295!
1733
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1734
        pRsp->code = terrno;
×
1735
        goto _exit;
×
1736
      }
1737
    }
1738
  }
1739

1740
  if (tqUpdateTbUidList(pVnode->pTq, tbUids, false) < 0) {
15,894!
1741
    vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(terrno));
×
1742
  }
1743

1744
  if (tdUpdateTbUidList(pVnode->pSma, pStore, false) < 0) {
15,894!
1745
    goto _exit;
×
1746
  }
1747

1748
  if (tsEnableAuditCreateTable) {
15,894!
1749
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
15,894✔
1750

1751
    SName name = {0};
15,894✔
1752
    if (tNameFromString(&name, pVnode->config.dbname, T_NAME_ACCT | T_NAME_DB) != 0) {
15,894!
1753
      vError("vgId:%d, failed to get name from string", TD_VID(pVnode));
×
1754
    }
1755

1756
    SStringBuilder sb = {0};
15,894✔
1757
    for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
32,189✔
1758
      char **key = (char **)taosArrayGet(tbNames, iReq);
16,295✔
1759
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
16,295✔
1760
      if (iReq < req.nReqs - 1) {
16,295✔
1761
        taosStringBuilderAppendChar(&sb, ',');
401✔
1762
      }
1763
      taosMemoryFreeClear(*key);
16,295!
1764
    }
1765

1766
    size_t len = 0;
15,894✔
1767
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
15,894✔
1768

1769
    if (pOriginRpc->info.conn.user != NULL && strlen(pOriginRpc->info.conn.user) > 0) {
15,894!
1770
      auditAddRecord(pOriginRpc, clusterId, "dropTable", name.dbname, "", keyJoined, len);
15,851✔
1771
    }
1772

1773
    taosStringBuilderDestroy(&sb);
15,894✔
1774
  }
1775

1776
_exit:
×
1777
  taosArrayDestroy(tbUids);
15,894✔
1778
  pStore = tdUidStoreFree(pStore);
15,894✔
1779
  tDecoderClear(&decoder);
15,894✔
1780
  tEncodeSize(tEncodeSVDropTbBatchRsp, &rsp, pRsp->contLen, ret);
15,894!
1781
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
15,894✔
1782
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
15,894✔
1783
  if (tEncodeSVDropTbBatchRsp(&encoder, &rsp) != 0) {
15,894!
1784
    vError("vgId:%d, failed to encode drop table batch response", TD_VID(pVnode));
×
1785
  }
1786
  tEncoderClear(&encoder);
15,894✔
1787
  taosArrayDestroy(rsp.pArray);
15,894✔
1788
  taosArrayDestroy(tbNames);
15,894✔
1789
  return 0;
15,894✔
1790
}
1791

1792
#ifdef BUILD_NO_CALL
1793
static int32_t vnodeDebugPrintSingleSubmitMsg(SMeta *pMeta, SSubmitBlk *pBlock, SSubmitMsgIter *msgIter,
1794
                                              const char *tags) {
1795
  SSubmitBlkIter blkIter = {0};
1796
  STSchema      *pSchema = NULL;
1797
  tb_uid_t       suid = 0;
1798
  STSRow        *row = NULL;
1799
  int32_t        rv = -1;
1800

1801
  tInitSubmitBlkIter(msgIter, pBlock, &blkIter);
1802
  if (blkIter.row == NULL) return 0;
1803

1804
  int32_t code = metaGetTbTSchemaNotNull(pMeta, msgIter->suid, TD_ROW_SVER(blkIter.row), 1,
1805
                                         &pSchema);  // TODO: use the real schema
1806
  if (TSDB_CODE_SUCCESS != code) {
1807
    printf("%s:%d no valid schema\n", tags, __LINE__);
1808
    return code;
1809
  }
1810

1811
  suid = msgIter->suid;
1812
  rv = TD_ROW_SVER(blkIter.row);
1813

1814
  char __tags[128] = {0};
1815
  snprintf(__tags, 128, "%s: uid %" PRIi64 " ", tags, msgIter->uid);
1816
  while ((row = tGetSubmitBlkNext(&blkIter))) {
1817
    tdSRowPrint(row, pSchema, __tags);
1818
  }
1819

1820
  taosMemoryFreeClear(pSchema);
1821

1822
  return TSDB_CODE_SUCCESS;
1823
}
1824
#endif
1825
typedef struct SSubmitReqConvertCxt {
1826
  SSubmitMsgIter msgIter;
1827
  SSubmitBlk    *pBlock;
1828
  SSubmitBlkIter blkIter;
1829
  STSRow        *pRow;
1830
  STSRowIter     rowIter;
1831
  SSubmitTbData *pTbData;
1832
  STSchema      *pTbSchema;
1833
  SArray        *pColValues;
1834
} SSubmitReqConvertCxt;
1835

1836
static int32_t vnodeResetTableCxt(SMeta *pMeta, SSubmitReqConvertCxt *pCxt) {
×
1837
  taosMemoryFreeClear(pCxt->pTbSchema);
×
1838
  int32_t code = metaGetTbTSchemaNotNull(pMeta, pCxt->msgIter.suid > 0 ? pCxt->msgIter.suid : pCxt->msgIter.uid,
×
1839
                                         pCxt->msgIter.sversion, 1, &pCxt->pTbSchema);
1840
  if (TSDB_CODE_SUCCESS != code) {
×
1841
    return code;
×
1842
  }
1843
  tdSTSRowIterInit(&pCxt->rowIter, pCxt->pTbSchema);
×
1844

1845
  tDestroySubmitTbData(pCxt->pTbData, TSDB_MSG_FLG_ENCODE);
×
1846
  if (NULL == pCxt->pTbData) {
×
1847
    pCxt->pTbData = taosMemoryCalloc(1, sizeof(SSubmitTbData));
×
1848
    if (NULL == pCxt->pTbData) {
×
1849
      return terrno;
×
1850
    }
1851
  }
1852
  pCxt->pTbData->flags = 0;
×
1853
  pCxt->pTbData->suid = pCxt->msgIter.suid;
×
1854
  pCxt->pTbData->uid = pCxt->msgIter.uid;
×
1855
  pCxt->pTbData->sver = pCxt->msgIter.sversion;
×
1856
  pCxt->pTbData->pCreateTbReq = NULL;
×
1857
  pCxt->pTbData->aRowP = taosArrayInit(128, POINTER_BYTES);
×
1858
  if (NULL == pCxt->pTbData->aRowP) {
×
1859
    return terrno;
×
1860
  }
1861

1862
  taosArrayDestroy(pCxt->pColValues);
×
1863
  pCxt->pColValues = taosArrayInit(pCxt->pTbSchema->numOfCols, sizeof(SColVal));
×
1864
  if (NULL == pCxt->pColValues) {
×
1865
    return terrno;
×
1866
  }
1867
  for (int32_t i = 0; i < pCxt->pTbSchema->numOfCols; ++i) {
×
1868
    SColVal val = COL_VAL_NONE(pCxt->pTbSchema->columns[i].colId, pCxt->pTbSchema->columns[i].type);
×
1869
    if (taosArrayPush(pCxt->pColValues, &val) == NULL) {
×
1870
      return terrno;
×
1871
    }
1872
  }
1873

1874
  return TSDB_CODE_SUCCESS;
×
1875
}
1876

1877
static void vnodeDestroySubmitReqConvertCxt(SSubmitReqConvertCxt *pCxt) {
×
1878
  taosMemoryFreeClear(pCxt->pTbSchema);
×
1879
  tDestroySubmitTbData(pCxt->pTbData, TSDB_MSG_FLG_ENCODE);
×
1880
  taosMemoryFreeClear(pCxt->pTbData);
×
1881
  taosArrayDestroy(pCxt->pColValues);
×
1882
}
×
1883

1884
static int32_t vnodeCellValConvertToColVal(STColumn *pCol, SCellVal *pCellVal, SColVal *pColVal) {
×
1885
  if (tdValTypeIsNone(pCellVal->valType)) {
×
1886
    pColVal->flag = CV_FLAG_NONE;
×
1887
    return TSDB_CODE_SUCCESS;
×
1888
  }
1889

1890
  if (tdValTypeIsNull(pCellVal->valType)) {
×
1891
    pColVal->flag = CV_FLAG_NULL;
×
1892
    return TSDB_CODE_SUCCESS;
×
1893
  }
1894

1895
  if (IS_VAR_DATA_TYPE(pCol->type)) {
×
1896
    if (IS_STR_DATA_BLOB(pCol->type)) {
×
1897
      pColVal->value.nData = blobDataLen(pCellVal->val);
×
1898
      pColVal->value.pData = (uint8_t *)blobDataVal(pCellVal->val);
×
1899

1900
    } else {
1901
      pColVal->value.nData = varDataLen(pCellVal->val);
×
1902
      pColVal->value.pData = (uint8_t *)varDataVal(pCellVal->val);
×
1903
    }
1904
  } else if (TSDB_DATA_TYPE_FLOAT == pCol->type) {
×
1905
    float f = GET_FLOAT_VAL(pCellVal->val);
×
1906
    valueSetDatum(&pColVal->value, pCol->type, &f, sizeof(f));
×
1907
  } else if (TSDB_DATA_TYPE_DOUBLE == pCol->type) {
×
1908
    taosSetPInt64Aligned(&pColVal->value.val, (int64_t *)pCellVal->val);
×
1909
  } else {
1910
    valueSetDatum(&pColVal->value, pCol->type, pCellVal->val, tDataTypes[pCol->type].bytes);
×
1911
  }
1912

1913
  pColVal->flag = CV_FLAG_VALUE;
×
1914
  return TSDB_CODE_SUCCESS;
×
1915
}
1916

1917
static int32_t vnodeTSRowConvertToColValArray(SSubmitReqConvertCxt *pCxt) {
×
1918
  int32_t code = TSDB_CODE_SUCCESS;
×
1919
  tdSTSRowIterReset(&pCxt->rowIter, pCxt->pRow);
×
1920
  for (int32_t i = 0; TSDB_CODE_SUCCESS == code && i < pCxt->pTbSchema->numOfCols; ++i) {
×
1921
    STColumn *pCol = pCxt->pTbSchema->columns + i;
×
1922
    SCellVal  cellVal = {0};
×
1923
    if (!tdSTSRowIterFetch(&pCxt->rowIter, pCol->colId, pCol->type, &cellVal)) {
×
1924
      break;
×
1925
    }
1926
    code = vnodeCellValConvertToColVal(pCol, &cellVal, (SColVal *)taosArrayGet(pCxt->pColValues, i));
×
1927
  }
1928
  return code;
×
1929
}
1930

1931
static int32_t vnodeDecodeCreateTbReq(SSubmitReqConvertCxt *pCxt) {
×
1932
  if (pCxt->msgIter.schemaLen <= 0) {
×
1933
    return TSDB_CODE_SUCCESS;
×
1934
  }
1935

1936
  pCxt->pTbData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
×
1937
  if (NULL == pCxt->pTbData->pCreateTbReq) {
×
1938
    return terrno;
×
1939
  }
1940

1941
  SDecoder decoder = {0};
×
1942
  tDecoderInit(&decoder, (uint8_t *)pCxt->pBlock->data, pCxt->msgIter.schemaLen);
×
1943
  int32_t code = tDecodeSVCreateTbReq(&decoder, pCxt->pTbData->pCreateTbReq);
×
1944
  tDecoderClear(&decoder);
×
1945

1946
  return code;
×
1947
}
1948

1949
static int32_t vnodeSubmitReqConvertToSubmitReq2(SVnode *pVnode, SSubmitReq *pReq, SSubmitReq2 *pReq2) {
×
1950
  pReq2->aSubmitTbData = taosArrayInit(128, sizeof(SSubmitTbData));
×
1951
  if (NULL == pReq2->aSubmitTbData) {
×
1952
    return terrno;
×
1953
  }
1954

1955
  SSubmitReqConvertCxt cxt = {0};
×
1956

1957
  int32_t code = tInitSubmitMsgIter(pReq, &cxt.msgIter);
×
1958
  while (TSDB_CODE_SUCCESS == code) {
×
1959
    code = tGetSubmitMsgNext(&cxt.msgIter, &cxt.pBlock);
×
1960
    if (TSDB_CODE_SUCCESS == code) {
×
1961
      if (NULL == cxt.pBlock) {
×
1962
        break;
×
1963
      }
1964
      code = vnodeResetTableCxt(pVnode->pMeta, &cxt);
×
1965
    }
1966
    if (TSDB_CODE_SUCCESS == code) {
×
1967
      code = tInitSubmitBlkIter(&cxt.msgIter, cxt.pBlock, &cxt.blkIter);
×
1968
    }
1969
    if (TSDB_CODE_SUCCESS == code) {
×
1970
      code = vnodeDecodeCreateTbReq(&cxt);
×
1971
    }
1972
    while (TSDB_CODE_SUCCESS == code && (cxt.pRow = tGetSubmitBlkNext(&cxt.blkIter)) != NULL) {
×
1973
      code = vnodeTSRowConvertToColValArray(&cxt);
×
1974
      if (TSDB_CODE_SUCCESS == code) {
×
1975
        SRow **pNewRow = taosArrayReserve(cxt.pTbData->aRowP, 1);
×
1976

1977
        SRowBuildScanInfo sinfo = {0};
×
1978
        code = tRowBuild(cxt.pColValues, cxt.pTbSchema, pNewRow, &sinfo);
×
1979
      }
1980
    }
1981
    if (TSDB_CODE_SUCCESS == code) {
×
1982
      code = (NULL == taosArrayPush(pReq2->aSubmitTbData, cxt.pTbData) ? terrno : TSDB_CODE_SUCCESS);
×
1983
    }
1984
    if (TSDB_CODE_SUCCESS == code) {
×
1985
      taosMemoryFreeClear(cxt.pTbData);
×
1986
    }
1987
  }
1988

1989
  vnodeDestroySubmitReqConvertCxt(&cxt);
×
1990
  return code;
×
1991
}
1992

1993
static int32_t vnodeRebuildSubmitReqMsg(SSubmitReq2 *pSubmitReq, void **ppMsg) {
×
1994
  int32_t  code = TSDB_CODE_SUCCESS;
×
1995
  char    *pMsg = NULL;
×
1996
  uint32_t msglen = 0;
×
1997
  tEncodeSize(tEncodeSubmitReq, pSubmitReq, msglen, code);
×
1998
  if (TSDB_CODE_SUCCESS == code) {
×
1999
    pMsg = taosMemoryMalloc(msglen);
×
2000
    if (NULL == pMsg) {
×
2001
      code = terrno;
×
2002
    }
2003
  }
2004
  if (TSDB_CODE_SUCCESS == code) {
×
2005
    SEncoder encoder;
2006
    tEncoderInit(&encoder, (uint8_t *)pMsg, msglen);
×
2007
    code = tEncodeSubmitReq(&encoder, pSubmitReq);
×
2008
    tEncoderClear(&encoder);
×
2009
  }
2010
  if (TSDB_CODE_SUCCESS == code) {
×
2011
    *ppMsg = pMsg;
×
2012
  }
2013
  return code;
×
2014
}
2015

2016
static int32_t buildExistSubTalbeRsp(SVnode *pVnode, SSubmitTbData *pSubmitTbData, STableMetaRsp **ppRsp) {
73✔
2017
  int32_t code = 0;
73✔
2018
  int32_t lino = 0;
73✔
2019

2020
  SMetaEntry *pEntry = NULL;
73✔
2021
  code = metaFetchEntryByUid(pVnode->pMeta, pSubmitTbData->suid, &pEntry);
73✔
2022
  if (code) {
73!
2023
    vError("vgId:%d, table uid:%" PRId64 " not exists, line:%d", TD_VID(pVnode), pSubmitTbData->uid, __LINE__);
×
2024
    TSDB_CHECK_CODE(code, lino, _exit);
×
2025
  }
2026
  if (pEntry->type != TSDB_SUPER_TABLE) {
73!
2027
    vError("vgId:%d, table uid:%" PRId64 " exists, but is not super table, line:%d", TD_VID(pVnode), pSubmitTbData->uid,
×
2028
           __LINE__);
2029
    code = TSDB_CODE_STREAM_INSERT_SCHEMA_NOT_MATCH;
×
2030
    TSDB_CHECK_CODE(code, lino, _exit);
×
2031
  }
2032

2033
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
73!
2034
  if (NULL == *ppRsp) {
73!
2035
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2036
  }
2037
  (*ppRsp)->suid = pSubmitTbData->suid;
73✔
2038
  (*ppRsp)->tuid = pSubmitTbData->uid;
73✔
2039
  (*ppRsp)->sversion = pEntry->stbEntry.schemaRow.version;
73✔
2040
  (*ppRsp)->vgId = pVnode->config.vgId;
73✔
2041
  (*ppRsp)->numOfColumns = pEntry->stbEntry.schemaRow.nCols;
73✔
2042
  (*ppRsp)->numOfTags = pEntry->stbEntry.schemaTag.nCols;
73✔
2043
  (*ppRsp)->pSchemas =
146✔
2044
      taosMemoryCalloc(pEntry->stbEntry.schemaRow.nCols + pEntry->stbEntry.schemaTag.nCols, sizeof(SSchema));
73!
2045
  if (NULL == (*ppRsp)->pSchemas) {
73!
2046
    taosMemoryFree(*ppRsp);
×
2047
    *ppRsp = NULL;
×
2048
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2049
  }
2050
  memcpy((*ppRsp)->pSchemas, pEntry->stbEntry.schemaRow.pSchema, pEntry->stbEntry.schemaRow.nCols * sizeof(SSchema));
73✔
2051
  memcpy((*ppRsp)->pSchemas + pEntry->stbEntry.schemaRow.nCols, pEntry->stbEntry.schemaTag.pSchema,
73✔
2052
         pEntry->stbEntry.schemaTag.nCols * sizeof(SSchema));
73✔
2053
  if (pEntry->pExtSchemas != NULL) {
73!
2054
    (*ppRsp)->pSchemaExt = taosMemoryCalloc(pEntry->colCmpr.nCols, sizeof(SExtSchema));
×
2055
    if (NULL == (*ppRsp)->pSchemaExt) {
×
2056
      taosMemoryFree((*ppRsp)->pSchemas);
×
2057
      taosMemoryFree(*ppRsp);
×
2058
      *ppRsp = NULL;
×
2059
      TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2060
    }
2061
    memcpy((*ppRsp)->pSchemaExt, pEntry->pExtSchemas, pEntry->colCmpr.nCols * sizeof(SExtSchema));
×
2062
  }
2063

2064
  if (pEntry->stbEntry.schemaRow.version != pSubmitTbData->sver) {
73!
2065
    TSDB_CHECK_CODE(code = TSDB_CODE_TDB_TABLE_ALREADY_EXIST, lino, _exit);
×
2066
  }
2067
_exit:
73✔
2068
  metaFetchEntryFree(&pEntry);
73✔
2069
  if (code != TSDB_CODE_SUCCESS) {
73!
2070
    vError("vgId:%d, failed to build exist sub table response, code:0x%0x, line:%d", TD_VID(pVnode), code, lino);
×
2071
  }
2072
  return code;
73✔
2073
}
2074

2075
static int32_t buildExistNormalTalbeRsp(SVnode *pVnode, SSubmitTbData *pSubmitTbData, STableMetaRsp **ppRsp) {
41✔
2076
  int32_t code = 0;
41✔
2077
  int32_t lino = 0;
41✔
2078

2079
  SMetaEntry *pEntry = NULL;
41✔
2080
  code = metaFetchEntryByUid(pVnode->pMeta, pSubmitTbData->uid, &pEntry);
41✔
2081
  if (code) {
41!
2082
    vError("vgId:%d, table uid:%" PRId64 " not exists, line:%d", TD_VID(pVnode), pSubmitTbData->uid, __LINE__);
×
2083
    TSDB_CHECK_CODE(code, lino, _exit);
×
2084
  }
2085
  if (pEntry->type != TSDB_NORMAL_TABLE) {
41!
2086
    vError("vgId:%d, table uid:%" PRId64 " exists, but is not normal table, line:%d", TD_VID(pVnode),
×
2087
           pSubmitTbData->uid, __LINE__);
2088
    code = TSDB_CODE_STREAM_INSERT_SCHEMA_NOT_MATCH;
×
2089
    TSDB_CHECK_CODE(code, lino, _exit);
×
2090
  }
2091

2092
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
41!
2093
  if (NULL == *ppRsp) {
41!
2094
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2095
  }
2096

2097
  (*ppRsp)->tuid = pEntry->uid;
41✔
2098
  (*ppRsp)->vgId = pVnode->config.vgId;
41✔
2099
  (*ppRsp)->sversion = pEntry->ntbEntry.schemaRow.version;
41✔
2100
  (*ppRsp)->numOfColumns = pEntry->ntbEntry.schemaRow.nCols;
41✔
2101
  (*ppRsp)->pSchemas = taosMemoryCalloc(pEntry->ntbEntry.schemaRow.nCols, sizeof(SSchema));
41!
2102
  if (NULL == (*ppRsp)->pSchemas) {
41!
2103
    taosMemoryFree(*ppRsp);
×
2104
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2105
  }
2106
  memcpy((*ppRsp)->pSchemas, pEntry->ntbEntry.schemaRow.pSchema, pEntry->ntbEntry.schemaRow.nCols * sizeof(SSchema));
41✔
2107
  if (pEntry->pExtSchemas != NULL) {
41!
2108
    (*ppRsp)->pSchemaExt = taosMemoryCalloc(pEntry->ntbEntry.schemaRow.nCols, sizeof(SSchemaExt));
×
2109
    if (NULL == (*ppRsp)->pSchemaExt) {
×
2110
      taosMemoryFree((*ppRsp)->pSchemas);
×
2111
      taosMemoryFree(*ppRsp);
×
2112
      TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2113
    }
2114
    memcpy((*ppRsp)->pSchemaExt, pEntry->pExtSchemas, pEntry->ntbEntry.schemaRow.nCols * sizeof(SSchemaExt));
×
2115
  }
2116

2117
_exit:
41✔
2118
  metaFetchEntryFree(&pEntry);
41✔
2119
  if (code != TSDB_CODE_SUCCESS) {
41!
2120
    vError("vgId:%d, failed to build exist normal table response, code:%d, line:%d", TD_VID(pVnode), code, lino);
×
2121
  }
2122
  return code;
41✔
2123
}
2124

2125
static int32_t buildExistTalbeInStreamRsp(SVnode *pVnode, SSubmitTbData *pSubmitTbData, STableMetaRsp **ppRsp) {
2,524✔
2126
  if (pSubmitTbData->pCreateTbReq->flags & TD_CREATE_NORMAL_TB_IN_STREAM) {
2,524✔
2127
    int32_t code = buildExistNormalTalbeRsp(pVnode, pSubmitTbData, ppRsp);
41✔
2128
    if (code) {
41!
2129
      vError("vgId:%d, table uid:%" PRId64 " not exists, line:%d", TD_VID(pVnode), pSubmitTbData->uid, __LINE__);
×
2130
      return code;
×
2131
    }
2132
    return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
41✔
2133
  } else if (pSubmitTbData->pCreateTbReq->flags & TD_CREATE_SUB_TB_IN_STREAM) {
2,483✔
2134
    return buildExistSubTalbeRsp(pVnode, pSubmitTbData, ppRsp);
73✔
2135
  }
2136
  return TSDB_CODE_SUCCESS;
2,410✔
2137
}
2138

2139
static int32_t vnodeHandleAutoCreateTable(SVnode      *pVnode,    // vnode
14,580,438✔
2140
                                          int64_t      version,   // version
2141
                                          SSubmitReq2 *pRequest,  // request
2142
                                          SSubmitRsp2 *pResponse  // response
2143
) {
2144
  int32_t code = TSDB_CODE_SUCCESS;
14,580,438✔
2145
  int32_t numTbData = taosArrayGetSize(pRequest->aSubmitTbData);
14,580,438✔
2146
  SArray *newTbUids = NULL;
14,580,579✔
2147

2148
  for (int32_t i = 0; i < numTbData; ++i) {
29,282,600✔
2149
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
14,701,989✔
2150

2151
    if (pTbData->pCreateTbReq == NULL) {
14,702,123✔
2152
      continue;
14,625,176✔
2153
    }
2154

2155
    pTbData->uid = pTbData->pCreateTbReq->uid;
76,947✔
2156

2157
    // Alloc necessary resources
2158
    if (pResponse->aCreateTbRsp == NULL) {
76,947✔
2159
      pResponse->aCreateTbRsp = taosArrayInit(numTbData, sizeof(SVCreateTbRsp));
58,362✔
2160
      if (pResponse->aCreateTbRsp == NULL) {
58,362!
2161
        code = terrno;
×
2162
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2163
               tstrerror(code), version);
2164
        taosArrayDestroy(newTbUids);
×
2165
        return code;
×
2166
      }
2167
    }
2168

2169
    // Do create table
2170
    vDebug("vgId:%d start to handle auto create table, version:%" PRId64, TD_VID(pVnode), version);
76,947✔
2171

2172
    SVCreateTbRsp *pCreateTbRsp = taosArrayReserve(pResponse->aCreateTbRsp, 1);
76,948✔
2173
    code = metaCreateTable2(pVnode->pMeta, version, pTbData->pCreateTbReq, &pCreateTbRsp->pMeta);
76,971✔
2174
    if (code == TSDB_CODE_SUCCESS) {
76,974✔
2175
      // Allocate necessary resources
2176
      if (newTbUids == NULL) {
67,500✔
2177
        newTbUids = taosArrayInit(numTbData, sizeof(int64_t));
55,836✔
2178
        if (newTbUids == NULL) {
55,836!
2179
          code = terrno;
×
2180
          vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2181
                 tstrerror(code), version);
2182
          return code;
×
2183
        }
2184
      }
2185

2186
      if (taosArrayPush(newTbUids, &pTbData->uid) == NULL) {
135,000!
2187
        code = terrno;
×
2188
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2189
               tstrerror(code), version);
2190
        taosArrayDestroy(newTbUids);
×
2191
        return code;
×
2192
      }
2193

2194
      if (pCreateTbRsp->pMeta) {
67,500!
2195
        vnodeUpdateMetaRsp(pVnode, pCreateTbRsp->pMeta);
67,500✔
2196
      }
2197
    } else if (code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
9,474✔
2198
      code = terrno = 0;
9,466✔
2199
      pTbData->uid = pTbData->pCreateTbReq->uid;  // update uid if table exist for using below
9,466✔
2200

2201
      // stream: get sver from meta, write to pCreateTbRsp, and need to check crateTbReq is same as meta.
2202
      if (i == 0) {
9,466✔
2203
        // In the streaming scenario, multiple grouped req requests will only operate on the same write table, and
2204
        // only the first one needs to be processed.
2205
        code = buildExistTalbeInStreamRsp(pVnode, pTbData, &pCreateTbRsp->pMeta);
2,524✔
2206
        if (code) {
2,524✔
2207
          vInfo("vgId:%d failed to create table in stream:%s, code(0x%0x):%s", TD_VID(pVnode),
41!
2208
                pTbData->pCreateTbReq->name, code, tstrerror(code));
2209
          taosArrayDestroy(newTbUids);
41✔
2210
          return code;
41✔
2211
        }
2212
      }
2213
    } else {
2214
      code = terrno;
8✔
2215
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
8!
2216
             tstrerror(code), version);
2217
      taosArrayDestroy(newTbUids);
8✔
2218
      return code;
8✔
2219
    }
2220
  }
2221

2222
  // Update the affected table uid list
2223
  if (taosArrayGetSize(newTbUids) > 0) {
14,580,611✔
2224
    vDebug("vgId:%d, add %d table into query table list in handling submit", TD_VID(pVnode),
55,836✔
2225
           (int32_t)taosArrayGetSize(newTbUids));
2226
    if (tqUpdateTbUidList(pVnode->pTq, newTbUids, true) != 0) {
55,836!
2227
      vError("vgId:%d, failed to update tbUid list", TD_VID(pVnode));
×
2228
    }
2229
  }
2230

2231
  vDebug("vgId:%d, handle auto create table done, version:%" PRId64, TD_VID(pVnode), version);
14,580,496✔
2232

2233
  taosArrayDestroy(newTbUids);
14,580,503✔
2234
  return code;
14,580,376✔
2235
}
2236

2237
static int32_t vnodeHandleDataWrite(SVnode *pVnode, int64_t version, SSubmitReq2 *pRequest, SSubmitRsp2 *pResponse) {
14,580,328✔
2238
  int32_t code = TSDB_CODE_SUCCESS;
14,580,328✔
2239
  int32_t numTbData = taosArrayGetSize(pRequest->aSubmitTbData);
14,580,328✔
2240
  int8_t  hasBlob = 0;
14,580,455✔
2241

2242
  // Scan submit data
2243
  for (int32_t i = 0; i < numTbData; ++i) {
29,282,686✔
2244
    SMetaInfo      info = {0};
14,701,918✔
2245
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
14,701,918✔
2246

2247
    code = metaGetInfo(pVnode->pMeta, pTbData->uid, &info, NULL);
14,701,993✔
2248
    if (code) {
14,702,118!
2249
      code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
2250
      vWarn("vgId:%d, error occurred at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __FILE__,
×
2251
            __LINE__, tstrerror(code), version, pTbData->uid);
2252
      return code;
2✔
2253
    }
2254

2255
    if (info.suid != pTbData->suid) {
14,702,118!
2256
      code = TSDB_CODE_INVALID_MSG;
×
2257
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64 " suid:%" PRId64
×
2258
             " info.suid:%" PRId64,
2259
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), version, pTbData->uid, pTbData->suid,
2260
             info.suid);
2261
      return code;
×
2262
    }
2263

2264
    if (info.suid) {
14,702,118✔
2265
      code = metaGetInfo(pVnode->pMeta, info.suid, &info, NULL);
13,565,695✔
2266
      if (code) {
13,565,765!
2267
        code = TSDB_CODE_INTERNAL_ERROR;
×
2268
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " suid:%" PRId64, TD_VID(pVnode), __func__,
×
2269
               __FILE__, __LINE__, tstrerror(code), version, info.suid);
2270
        return code;
×
2271
      }
2272
    }
2273

2274
    if (pTbData->sver != info.skmVer) {
14,702,233✔
2275
      code = TSDB_CODE_TDB_INVALID_TABLE_SCHEMA_VER;
2✔
2276
      vDebug("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64
2!
2277
             " sver:%d"
2278
             " info.skmVer:%d",
2279
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), version, pTbData->uid, pTbData->sver,
2280
             info.skmVer);
2281
      return code;
2✔
2282
    }
2283

2284
    if (pTbData->flags & SUBMIT_REQ_WITH_BLOB) {
14,702,231!
2285
      hasBlob = 1;
×
2286
    }
2287
  }
2288

2289
  // Do write data
2290
  vDebug("vgId:%d start to handle data write, version:%" PRId64, TD_VID(pVnode), version);
14,580,768✔
2291

2292
  for (int32_t i = 0; i < numTbData; ++i) {
29,282,901✔
2293
    int32_t        affectedRows = 0;
14,702,164✔
2294
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
14,702,164✔
2295

2296
    if (hasBlob) {
14,702,148!
2297
      code = vnodeSubmitBlobData(pVnode, pTbData);
×
2298
      if (code) {
×
2299
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2300
               __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2301
        return code;
×
2302
      }
2303
    }
2304

2305
    code = tsdbInsertTableData(pVnode->pTsdb, version, pTbData, &affectedRows);
14,702,148✔
2306
    if (code) {
14,702,246!
2307
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2308
             __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2309
      return code;
×
2310
    }
2311

2312
    code = metaUpdateChangeTimeWithLock(pVnode->pMeta, pTbData->uid, pTbData->ctimeMs);
14,702,246✔
2313
    if (code) {
14,702,195!
2314
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2315
             __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2316
      return code;
×
2317
    }
2318
    pResponse->affectedRows += affectedRows;
14,702,219✔
2319
  }
2320

2321
  vDebug("vgId:%d, handle data write done, version:%" PRId64 ", affectedRows:%d", TD_VID(pVnode), version,
14,580,737✔
2322
         pResponse->affectedRows);
2323
  return code;
14,580,673✔
2324
}
2325

2326
static int32_t vnodeScanColumnData(SVnode *pVnode, SSubmitTbData *pTbData, TSKEY minKey, TSKEY maxKey) {
571✔
2327
  int32_t code = 0;
571✔
2328

2329
  int32_t   numCols = taosArrayGetSize(pTbData->aCol);
571✔
2330
  SColData *aColData = (SColData *)TARRAY_DATA(pTbData->aCol);
571✔
2331

2332
  if (numCols <= 0) {
571!
2333
    code = TSDB_CODE_INVALID_MSG;
×
2334
    vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " numCols:%d", TD_VID(pVnode), __func__,
×
2335
           __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, numCols);
2336
    return code;
×
2337
  }
2338

2339
  if (aColData[0].cid != PRIMARYKEY_TIMESTAMP_COL_ID || aColData[0].type != TSDB_DATA_TYPE_TIMESTAMP ||
571!
2340
      aColData[0].nVal <= 0) {
571!
2341
    code = TSDB_CODE_INVALID_MSG;
×
2342
    vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64
×
2343
           " first column is not primary key timestamp, cid:%d type:%d nVal:%d",
2344
           TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, aColData[0].cid,
2345
           aColData[0].type, aColData[0].nVal);
2346
    return code;
×
2347
  }
2348

2349
  for (int32_t i = 1; i < numCols; ++i) {
3,949✔
2350
    if (aColData[i].nVal != aColData[0].nVal) {
3,378!
2351
      code = TSDB_CODE_INVALID_MSG;
×
2352
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64
×
2353
             " column cid:%d type:%d nVal:%d is not equal to primary key timestamp nVal:%d",
2354
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid,
2355
             aColData[i].cid, aColData[i].type, aColData[i].nVal, aColData[0].nVal);
2356
      return code;
×
2357
    }
2358
  }
2359

2360
  SRowKey *pLastKey = NULL;
571✔
2361
  SRowKey  lastKey = {0};
571✔
2362
  for (int32_t i = 0; i < aColData[0].nVal; ++i) {
43,828✔
2363
    SRowKey key = {0};
43,256✔
2364

2365
    tColDataArrGetRowKey(aColData, numCols, i, &key);
43,256✔
2366

2367
    if (key.ts < minKey || key.ts > maxKey) {
43,248!
2368
      code = TSDB_CODE_INVALID_MSG;
×
2369
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] key:%" PRId64
×
2370
             " is out of range [%" PRId64 ", %" PRId64 "]",
2371
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, i, key.ts,
2372
             minKey, maxKey);
2373
      return code;
×
2374
    }
2375

2376
    if (pLastKey && tRowKeyCompare(pLastKey, &key) >= 0) {
43,248!
2377
      code = TSDB_CODE_INVALID_MSG;
×
2378
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] key:%" PRId64
×
2379
             " is not in order, lastKey:%" PRId64,
2380
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, i, key.ts,
2381
             pLastKey->ts);
2382
      return code;
×
2383
    } else if (pLastKey == NULL) {
43,257✔
2384
      pLastKey = &lastKey;
571✔
2385
    }
2386

2387
    *pLastKey = key;
43,257✔
2388
  }
2389

2390
  return code;
572✔
2391
}
2392

2393
static int32_t vnodeScanSubmitRowData(SVnode *pVnode, SSubmitTbData *pTbData, TSKEY minKey, TSKEY maxKey) {
14,701,345✔
2394
  int32_t code = 0;
14,701,345✔
2395

2396
  int32_t numRows = taosArrayGetSize(pTbData->aRowP);
14,701,345✔
2397
  SRow  **aRow = (SRow **)TARRAY_DATA(pTbData->aRowP);
14,701,490✔
2398

2399
  if (numRows <= 0) {
14,701,490!
2400
    code = TSDB_CODE_INVALID_MSG;
×
2401
    vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " numRows:%d", TD_VID(pVnode), __func__,
×
2402
           __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, numRows);
2403
    return code;
×
2404
  }
2405

2406
  SRowKey *pLastKey = NULL;
14,701,524✔
2407
  SRowKey  lastKey = {0};
14,701,524✔
2408
  for (int32_t i = 0; i < numRows; ++i) {
1,091,087,265✔
2409
    SRow *pRow = aRow[i];
1,076,350,337✔
2410
    if (pRow->sver != pTbData->sver) {
1,076,350,337!
2411
      code = TSDB_CODE_INVALID_MSG;
×
2412
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] sver:%d pTbData->sver:%d",
×
2413
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, i, pRow->sver,
2414
             pTbData->sver);
2415
      return code;
1✔
2416
    }
2417

2418
    SRowKey key = {0};
1,076,350,337✔
2419
    tRowGetKey(pRow, &key);
2,147,483,647✔
2420
    if (key.ts < minKey || key.ts > maxKey) {
1,076,349,058!
2421
      code = TSDB_CODE_INVALID_MSG;
×
2422
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] key:%" PRId64
×
2423
             " is out of range [%" PRId64 ", %" PRId64 "]",
2424
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, i, key.ts,
2425
             minKey, maxKey);
2426
      return code;
1✔
2427
    }
2428

2429
    if (pLastKey && tRowKeyCompare(pLastKey, &key) >= 0) {
1,076,393,498✔
2430
      code = TSDB_CODE_INVALID_MSG;
2,262✔
2431
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] key:%" PRId64
2,262!
2432
             " is not in order, lastKey:%" PRId64,
2433
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, i, key.ts,
2434
             pLastKey->ts);
2435
      return code;
×
2436
    } else if (pLastKey == NULL) {
1,076,385,741✔
2437
      pLastKey = &lastKey;
14,701,360✔
2438
    }
2439

2440
    *pLastKey = key;
1,076,385,741✔
2441
  }
2442

2443
  return code;
14,736,928✔
2444
}
2445

2446
static int32_t vnodeScanSubmitReq(SVnode *pVnode, int64_t version, SSubmitReq2 *pRequest, SSubmitRsp2 *pResponse) {
14,580,436✔
2447
  int32_t code = TSDB_CODE_SUCCESS;
14,580,436✔
2448
  int32_t numTbData = taosArrayGetSize(pRequest->aSubmitTbData);
14,580,436✔
2449

2450
  TSKEY now = taosGetTimestamp(pVnode->config.tsdbCfg.precision);
14,580,474✔
2451
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * pVnode->config.tsdbCfg.keep2;
14,580,617✔
2452
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
14,580,617✔
2453
  for (int32_t i = 0; i < numTbData; i++) {
29,282,651✔
2454
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
14,701,966✔
2455

2456
    if (pTbData->pCreateTbReq && pTbData->pCreateTbReq->uid == 0) {
14,701,968!
2457
      code = TSDB_CODE_INVALID_MSG;
×
2458
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2459
             tstrerror(code), version);
2460
      return code;
×
2461
    }
2462

2463
    if (pTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
14,701,968✔
2464
      code = vnodeScanColumnData(pVnode, pTbData, minKey, maxKey);
571✔
2465
      if (code) {
571!
2466
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2467
               __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2468
        return code;
×
2469
      }
2470
    } else {
2471
      code = vnodeScanSubmitRowData(pVnode, pTbData, minKey, maxKey);
14,701,397✔
2472
      if (code) {
14,701,457!
2473
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2474
               __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2475
        return code;
1✔
2476
      }
2477
    }
2478
  }
2479

2480
  return code;
14,580,685✔
2481
}
2482

2483
static int32_t vnodeProcessSubmitReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
14,580,504✔
2484
                                     SRpcMsg *pOriginalMsg) {
2485
  int32_t code = 0;
14,580,504✔
2486
  int32_t lino = 0;
14,580,504✔
2487
  terrno = 0;
14,580,504✔
2488

2489
  SSubmitReq2 *pSubmitReq = &(SSubmitReq2){0};
14,580,648✔
2490
  SSubmitRsp2 *pSubmitRsp = &(SSubmitRsp2){0};
14,580,648✔
2491
  int32_t      ret;
2492
  SEncoder     ec = {0};
14,580,648✔
2493

2494
  pRsp->code = TSDB_CODE_SUCCESS;
14,580,648✔
2495

2496
  void           *pAllocMsg = NULL;
14,580,648✔
2497
  SSubmitReq2Msg *pMsg = (SSubmitReq2Msg *)pReq;
14,580,648✔
2498
  SDecoder        dc = {0};
14,580,648✔
2499
  if (0 == taosHton64(pMsg->version)) {
14,580,648!
2500
    code = vnodeSubmitReqConvertToSubmitReq2(pVnode, (SSubmitReq *)pMsg, pSubmitReq);
×
2501
    if (TSDB_CODE_SUCCESS == code) {
×
2502
      code = vnodeRebuildSubmitReqMsg(pSubmitReq, &pReq);
×
2503
    }
2504
    if (TSDB_CODE_SUCCESS == code) {
×
2505
      pAllocMsg = pReq;
×
2506
    }
2507
    TSDB_CHECK_CODE(code, lino, _exit);
×
2508
  } else {
2509
    // decode
2510
    pReq = POINTER_SHIFT(pReq, sizeof(SSubmitReq2Msg));
14,580,485✔
2511
    len -= sizeof(SSubmitReq2Msg);
14,580,485✔
2512

2513
    tDecoderInit(&dc, pReq, len);
14,580,485✔
2514
    if (tDecodeSubmitReq(&dc, pSubmitReq, NULL) < 0) {
14,580,470!
2515
      code = TSDB_CODE_INVALID_MSG;
×
2516
      TSDB_CHECK_CODE(code, lino, _exit);
×
2517
    }
2518
  }
2519

2520
  // Scan the request
2521
  code = vnodeScanSubmitReq(pVnode, ver, pSubmitReq, pSubmitRsp);
14,580,627✔
2522
  if (code) {
14,580,478✔
2523
    vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
1!
2524
           tstrerror(code), ver);
2525
    TSDB_CHECK_CODE(code, lino, _exit);
1!
2526
  }
2527

2528
  vGDebug(pOriginalMsg ? &pOriginalMsg->info.traceId : NULL, "vgId:%d, index:%" PRId64 ", submit block, rows:%d",
14,580,477!
2529
          TD_VID(pVnode), ver, (int32_t)taosArrayGetSize(pSubmitReq->aSubmitTbData));
2530

2531
  // Handle auto create table
2532
  code = vnodeHandleAutoCreateTable(pVnode, ver, pSubmitReq, pSubmitRsp);
14,580,488✔
2533
  if (code) {
14,580,397✔
2534
    vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
49!
2535
           tstrerror(code), ver);
2536
    TSDB_CHECK_CODE(code, lino, _exit);
49!
2537
  }
2538

2539
  // Handle data write
2540
  code = vnodeHandleDataWrite(pVnode, ver, pSubmitReq, pSubmitRsp);
14,580,348✔
2541
  if (code) {
14,580,665✔
2542
    vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
2!
2543
           tstrerror(code), ver);
2544
    TSDB_CHECK_CODE(code, lino, _exit);
2!
2545
  }
2546

2547
_exit:
14,580,663✔
2548
  // message
2549
  pRsp->code = code;
14,580,715✔
2550
  tEncodeSize(tEncodeSSubmitRsp2, pSubmitRsp, pRsp->contLen, ret);
14,580,715!
2551
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
14,580,567✔
2552
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
14,580,720✔
2553
  if (tEncodeSSubmitRsp2(&ec, pSubmitRsp) < 0) {
14,580,719!
2554
    vError("vgId:%d, failed to encode submit response", TD_VID(pVnode));
×
2555
  }
2556
  tEncoderClear(&ec);
14,580,686✔
2557

2558
  // update statistics
2559
  (void)atomic_add_fetch_64(&pVnode->statis.nInsert, pSubmitRsp->affectedRows);
14,580,657✔
2560
  (void)atomic_add_fetch_64(&pVnode->statis.nInsertSuccess, pSubmitRsp->affectedRows);
14,580,780✔
2561
  (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsert, 1);
14,580,774✔
2562

2563
  // update metrics
2564
  METRICS_UPDATE(pVnode->writeMetrics.total_requests, METRIC_LEVEL_LOW, 1);
14,580,754!
2565
  METRICS_UPDATE(pVnode->writeMetrics.total_rows, METRIC_LEVEL_HIGH, pSubmitRsp->affectedRows);
14,580,754!
2566
  METRICS_UPDATE(pVnode->writeMetrics.total_bytes, METRIC_LEVEL_LOW, pMsg->header.contLen);
14,580,754!
2567

2568
  if (tsEnableMonitor && tsMonitorFqdn[0] != 0 && tsMonitorPort != 0 && pSubmitRsp->affectedRows > 0 &&
14,580,754!
2569
      strlen(pOriginalMsg->info.conn.user) > 0 && tsInsertCounter != NULL) {
×
2570
    const char *sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT_AFFECTED_ROWS,
×
2571
                                   pVnode->monitor.strClusterId,
×
2572
                                   pVnode->monitor.strDnodeId,
×
2573
                                   tsLocalEp,
2574
                                   pVnode->monitor.strVgId,
×
2575
                                   pOriginalMsg->info.conn.user,
×
2576
                                   "Success"};
2577
    int         tv = taos_counter_add(tsInsertCounter, pSubmitRsp->affectedRows, sample_labels);
×
2578
  }
2579

2580
  if (code == 0) {
14,580,754✔
2581
    (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
14,580,603✔
2582
    code = tdProcessRSmaSubmit(pVnode->pSma, ver, pSubmitReq, pReq, len);
14,580,719✔
2583
  }
2584

2585
  // clear
2586
  tDestroySubmitReq(pSubmitReq, 0 == taosHton64(pMsg->version) ? TSDB_MSG_FLG_CMPT : TSDB_MSG_FLG_DECODE);
14,580,789!
2587
  tDestroySSubmitRsp2(pSubmitRsp, TSDB_MSG_FLG_ENCODE);
14,580,709✔
2588

2589
  if (code) {
14,580,583✔
2590
    terrno = code;
52✔
2591
    if (code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
52✔
2592
      vInfo("vgId:%d, failed to process submit request since %s, lino:%d, version:%" PRId64, TD_VID(pVnode),
41!
2593
            tstrerror(code), lino, ver);
2594

2595
    } else {
2596
      vError("vgId:%d, failed to process submit request since %s, lino:%d, version:%" PRId64, TD_VID(pVnode),
11!
2597
             tstrerror(code), lino, ver);
2598
    }
2599
  }
2600

2601
  taosMemoryFree(pAllocMsg);
14,580,535!
2602
  tDecoderClear(&dc);
14,580,583✔
2603

2604
  return code;
14,580,747✔
2605
}
2606

2607
static int32_t vnodeConsolidateAlterHashRange(SVnode *pVnode, int64_t ver) {
78✔
2608
  int32_t code = TSDB_CODE_SUCCESS;
78✔
2609

2610
  vInfo("vgId:%d, trim meta of tables per hash range [%" PRIu32 ", %" PRIu32 "]. apply-index:%" PRId64, TD_VID(pVnode),
78!
2611
        pVnode->config.hashBegin, pVnode->config.hashEnd, ver);
2612

2613
  // TODO: trim meta of tables from TDB per hash range [pVnode->config.hashBegin, pVnode->config.hashEnd]
2614
  code = metaTrimTables(pVnode->pMeta, ver);
78✔
2615

2616
  return code;
78✔
2617
}
2618

2619
static int32_t vnodeProcessAlterConfirmReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
15,227✔
2620
  vInfo("vgId:%d, vnode handle msgType:alter-confirm, alter confirm msg is processed", TD_VID(pVnode));
15,227!
2621
  int32_t code = TSDB_CODE_SUCCESS;
15,227✔
2622
  if (!pVnode->config.hashChange) {
15,227✔
2623
    goto _exit;
15,149✔
2624
  }
2625

2626
  code = vnodeConsolidateAlterHashRange(pVnode, ver);
78✔
2627
  if (code < 0) {
78!
2628
    vError("vgId:%d, failed to consolidate alter hashrange since %s. version:%" PRId64, TD_VID(pVnode), terrstr(), ver);
×
2629
    goto _exit;
×
2630
  }
2631
  pVnode->config.hashChange = false;
78✔
2632

2633
_exit:
15,227✔
2634
  pRsp->msgType = TDMT_VND_ALTER_CONFIRM_RSP;
15,227✔
2635
  pRsp->code = code;
15,227✔
2636
  pRsp->pCont = NULL;
15,227✔
2637
  pRsp->contLen = 0;
15,227✔
2638

2639
  return code;
15,227✔
2640
}
2641

2642
extern int32_t tsdbDisableAndCancelAllBgTask(STsdb *pTsdb);
2643
extern void    tsdbEnableBgTask(STsdb *pTsdb);
2644

2645
static int32_t vnodeProcessAlterConfigReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
1,197✔
2646
  bool walChanged = false;
1,197✔
2647
  bool tsdbChanged = false;
1,197✔
2648

2649
  SAlterVnodeConfigReq req = {0};
1,197✔
2650
  if (tDeserializeSAlterVnodeConfigReq(pReq, len, &req) != 0) {
1,197!
2651
    terrno = TSDB_CODE_INVALID_MSG;
×
2652
    return TSDB_CODE_INVALID_MSG;
×
2653
  }
2654

2655
  vInfo("vgId:%d, start to alter vnode config, page:%d pageSize:%d buffer:%d szPage:%d szBuf:%" PRIu64
1,198✔
2656
        " cacheLast:%d cacheLastSize:%d days:%d keep0:%d keep1:%d keep2:%d keepTimeOffset:%d ssKeepLocal:%d "
2657
        "ssCompact:%d fsync:%d level:%d "
2658
        "walRetentionPeriod:%d walRetentionSize:%d",
2659
        TD_VID(pVnode), req.pages, req.pageSize, req.buffer, req.pageSize * 1024, (uint64_t)req.buffer * 1024 * 1024,
2660
        req.cacheLast, req.cacheLastSize, req.daysPerFile, req.daysToKeep0, req.daysToKeep1, req.daysToKeep2,
2661
        req.keepTimeOffset, req.ssKeepLocal, req.ssCompact, req.walFsyncPeriod, req.walLevel, req.walRetentionPeriod,
2662
        req.walRetentionSize);
2663

2664
  if (pVnode->config.cacheLastSize != req.cacheLastSize) {
1,199✔
2665
    pVnode->config.cacheLastSize = req.cacheLastSize;
424✔
2666
    tsdbCacheSetCapacity(pVnode, (size_t)pVnode->config.cacheLastSize * 1024 * 1024);
424✔
2667
  }
2668

2669
  if (pVnode->config.szBuf != req.buffer * 1024LL * 1024LL) {
1,199✔
2670
    vInfo("vgId:%d, vnode buffer is changed from %" PRId64 " to %" PRId64, TD_VID(pVnode), pVnode->config.szBuf,
14!
2671
          (uint64_t)(req.buffer * 1024LL * 1024LL));
2672
    pVnode->config.szBuf = req.buffer * 1024LL * 1024LL;
14✔
2673
  }
2674

2675
  if (pVnode->config.szCache != req.pages) {
1,199✔
2676
    if ((terrno = metaAlterCache(pVnode->pMeta, req.pages)) < 0) {
6!
2677
      vError("vgId:%d, failed to change vnode pages from %d to %d failed since %s", TD_VID(pVnode),
×
2678
             pVnode->config.szCache, req.pages, tstrerror(terrno));
2679
      return terrno;
×
2680
    } else {
2681
      vInfo("vgId:%d, vnode pages is changed from %d to %d", TD_VID(pVnode), pVnode->config.szCache, req.pages);
6!
2682
      pVnode->config.szCache = req.pages;
6✔
2683
    }
2684
  }
2685

2686
  if (pVnode->config.cacheLast != req.cacheLast) {
1,199✔
2687
    pVnode->config.cacheLast = req.cacheLast;
694✔
2688
  }
2689

2690
  if (pVnode->config.walCfg.fsyncPeriod != req.walFsyncPeriod) {
1,199✔
2691
    pVnode->config.walCfg.fsyncPeriod = req.walFsyncPeriod;
126✔
2692
    walChanged = true;
126✔
2693
  }
2694

2695
  if (pVnode->config.walCfg.level != req.walLevel) {
1,199✔
2696
    if (pVnode->config.walCfg.level == 0) {
86!
2697
      pVnode->config.walCfg.clearFiles = 1;
×
2698
    }
2699
    pVnode->config.walCfg.level = req.walLevel;
86✔
2700
    walChanged = true;
86✔
2701
  }
2702

2703
  if (pVnode->config.walCfg.retentionPeriod != req.walRetentionPeriod) {
1,199✔
2704
    pVnode->config.walCfg.retentionPeriod = req.walRetentionPeriod;
124✔
2705
    walChanged = true;
124✔
2706
  }
2707

2708
  if (pVnode->config.walCfg.retentionSize != req.walRetentionSize) {
1,199✔
2709
    pVnode->config.walCfg.retentionSize = req.walRetentionSize;
2✔
2710
    walChanged = true;
2✔
2711
  }
2712

2713
  if (pVnode->config.tsdbCfg.keep0 != req.daysToKeep0) {
1,199✔
2714
    pVnode->config.tsdbCfg.keep0 = req.daysToKeep0;
100✔
2715
    if (!VND_IS_RSMA(pVnode)) {
100!
2716
      tsdbChanged = true;
100✔
2717
    }
2718
  }
2719

2720
  if (pVnode->config.tsdbCfg.keep1 != req.daysToKeep1) {
1,199✔
2721
    pVnode->config.tsdbCfg.keep1 = req.daysToKeep1;
100✔
2722
    if (!VND_IS_RSMA(pVnode)) {
100!
2723
      tsdbChanged = true;
100✔
2724
    }
2725
  }
2726

2727
  if (pVnode->config.tsdbCfg.keep2 != req.daysToKeep2) {
1,199✔
2728
    pVnode->config.tsdbCfg.keep2 = req.daysToKeep2;
102✔
2729
    if (!VND_IS_RSMA(pVnode)) {
102!
2730
      tsdbChanged = true;
102✔
2731
    }
2732
  }
2733

2734
  if (pVnode->config.tsdbCfg.keepTimeOffset != req.keepTimeOffset) {
1,199✔
2735
    pVnode->config.tsdbCfg.keepTimeOffset = req.keepTimeOffset;
10✔
2736
    if (!VND_IS_RSMA(pVnode)) {
10!
2737
      tsdbChanged = true;
10✔
2738
    }
2739
  }
2740

2741
  if (req.sttTrigger != -1 && req.sttTrigger != pVnode->config.sttTrigger) {
1,199✔
2742
    if (req.sttTrigger > 1 && pVnode->config.sttTrigger > 1) {
4!
2743
      pVnode->config.sttTrigger = req.sttTrigger;
2✔
2744
    } else {
2745
      vnodeAWait(&pVnode->commitTask);
2✔
2746

2747
      int32_t ret = tsdbDisableAndCancelAllBgTask(pVnode->pTsdb);
2✔
2748
      if (ret != 0) {
2!
2749
        vError("vgId:%d, failed to disable bg task since %s", TD_VID(pVnode), tstrerror(ERRNO));
×
2750
      }
2751

2752
      pVnode->config.sttTrigger = req.sttTrigger;
2✔
2753
      tsdbEnableBgTask(pVnode->pTsdb);
2✔
2754
    }
2755
  }
2756

2757
  if (req.minRows != -1 && req.minRows != pVnode->config.tsdbCfg.minRows) {
1,199✔
2758
    pVnode->config.tsdbCfg.minRows = req.minRows;
2✔
2759
  }
2760

2761
  if (req.ssKeepLocal != -1 && req.ssKeepLocal != pVnode->config.ssKeepLocal) {
1,199✔
2762
    pVnode->config.ssKeepLocal = req.ssKeepLocal;
2✔
2763
  }
2764
  if (req.ssCompact != -1 && req.ssCompact != pVnode->config.ssCompact) {
1,199✔
2765
    pVnode->config.ssCompact = req.ssCompact;
2✔
2766
  }
2767

2768
  if (walChanged) {
1,199✔
2769
    if (walAlter(pVnode->pWal, &pVnode->config.walCfg) != 0) {
334!
2770
      vError("vgId:%d, failed to alter wal config since %s", TD_VID(pVnode), tstrerror(ERRNO));
×
2771
    }
2772
  }
2773

2774
  if (tsdbChanged) {
1,197✔
2775
    tsdbSetKeepCfg(pVnode->pTsdb, &pVnode->config.tsdbCfg);
114✔
2776
  }
2777

2778
  return 0;
1,198✔
2779
}
2780

2781
static int32_t vnodeProcessBatchDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
2782
  SBatchDeleteReq deleteReq;
2783
  SDecoder        decoder;
2784
  tDecoderInit(&decoder, pReq, len);
×
2785
  if (tDecodeSBatchDeleteReq(&decoder, &deleteReq) < 0) {
×
2786
    tDecoderClear(&decoder);
×
2787
    return terrno = TSDB_CODE_INVALID_MSG;
×
2788
  }
2789

2790
  SMetaReader mr = {0};
×
2791
  metaReaderDoInit(&mr, pVnode->pMeta, META_READER_NOLOCK);
×
2792
  STsdb *pTsdb = pVnode->pTsdb;
×
2793

2794
  if (deleteReq.level) {
×
2795
    pTsdb = deleteReq.level == 1 ? VND_RSMA1(pVnode) : VND_RSMA2(pVnode);
×
2796
  }
2797

2798
  int32_t sz = taosArrayGetSize(deleteReq.deleteReqs);
×
2799
  for (int32_t i = 0; i < sz; i++) {
×
2800
    SSingleDeleteReq *pOneReq = taosArrayGet(deleteReq.deleteReqs, i);
×
2801
    char             *name = pOneReq->tbname;
×
2802
    if (metaGetTableEntryByName(&mr, name) < 0) {
×
2803
      vDebug("vgId:%d, stream delete msg, skip since no table: %s", pVnode->config.vgId, name);
×
2804
      continue;
×
2805
    }
2806

2807
    int64_t uid = mr.me.uid;
×
2808

2809
    int32_t code = tsdbDeleteTableData(pTsdb, ver, deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
×
2810
    if (code < 0) {
×
2811
      terrno = code;
×
2812
      vError("vgId:%d, delete error since %s, suid:%" PRId64 ", uid:%" PRId64 ", start ts:%" PRId64 ", end ts:%" PRId64,
×
2813
             TD_VID(pVnode), terrstr(), deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
2814
    }
2815

2816
    if (deleteReq.level == 0) {
×
2817
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, deleteReq.ctimeMs);
×
2818
      if (code < 0) {
×
2819
        terrno = code;
×
2820
        vError("vgId:%d, update change time error since %s, suid:%" PRId64 ", uid:%" PRId64 ", start ts:%" PRId64
×
2821
               ", end ts:%" PRId64,
2822
               TD_VID(pVnode), terrstr(), deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
2823
      }
2824
    }
2825
    tDecoderClear(&mr.coder);
×
2826
  }
2827
  metaReaderClear(&mr);
×
2828
  taosArrayDestroy(deleteReq.deleteReqs);
×
2829
  return 0;
×
2830
}
2831

2832
static int32_t vnodeProcessDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
65,876✔
2833
                                     SRpcMsg *pOriginalMsg) {
2834
  int32_t     code = 0;
65,876✔
2835
  SDecoder   *pCoder = &(SDecoder){0};
65,876✔
2836
  SDeleteRes *pRes = &(SDeleteRes){0};
65,876✔
2837

2838
  pRsp->msgType = TDMT_VND_DELETE_RSP;
65,876✔
2839
  pRsp->pCont = NULL;
65,876✔
2840
  pRsp->contLen = 0;
65,876✔
2841
  pRsp->code = TSDB_CODE_SUCCESS;
65,876✔
2842

2843
  pRes->uidList = taosArrayInit(0, sizeof(tb_uid_t));
65,876✔
2844
  if (pRes->uidList == NULL) {
65,876!
2845
    code = terrno;
×
2846
    goto _err;
×
2847
  }
2848

2849
  tDecoderInit(pCoder, pReq, len);
65,876✔
2850
  code = tDecodeDeleteRes(pCoder, pRes);
65,874✔
2851
  if (code) goto _err;
65,875!
2852

2853
  if (pRes->affectedRows > 0) {
65,875✔
2854
    for (int32_t iUid = 0; iUid < taosArrayGetSize(pRes->uidList); iUid++) {
127,764✔
2855
      uint64_t uid = *(uint64_t *)taosArrayGet(pRes->uidList, iUid);
66,177✔
2856
      code = tsdbDeleteTableData(pVnode->pTsdb, ver, pRes->suid, uid, pRes->skey, pRes->ekey);
66,177✔
2857
      if (code) goto _err;
66,178!
2858
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, pRes->ctimeMs);
66,178✔
2859
      if (code) goto _err;
66,178!
2860
    }
2861
  }
2862

2863
  code = tdProcessRSmaDelete(pVnode->pSma, ver, pRes, pReq, len);
65,876✔
2864

2865
  tDecoderClear(pCoder);
65,878✔
2866
  taosArrayDestroy(pRes->uidList);
65,878✔
2867

2868
  SVDeleteRsp rsp = {.affectedRows = pRes->affectedRows};
65,875✔
2869
  int32_t     ret = 0;
65,875✔
2870
  tEncodeSize(tEncodeSVDeleteRsp, &rsp, pRsp->contLen, ret);
65,875!
2871
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
65,874✔
2872
  SEncoder ec = {0};
65,876✔
2873
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
65,876✔
2874
  code = tEncodeSVDeleteRsp(&ec, &rsp);
65,878✔
2875
  if (code) goto _err;
65,878!
2876
  tEncoderClear(&ec);
65,878✔
2877
  return code;
65,877✔
2878

2879
_err:
×
2880
  /*
2881
  if(code == TSDB_CODE_SUCCESS){
2882
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_DELETE, pVnode->monitor.strClusterId,
2883
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2884
                                        pOriginalMsg->info.conn.user, "Success"};
2885
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2886
  }
2887
  else{
2888
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_DELETE, pVnode->monitor.strClusterId,
2889
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2890
                                        pOriginalMsg->info.conn.user, "Failed"};
2891
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2892
  }
2893
  */
2894

2895
  return code;
×
2896
}
2897
static int32_t vnodeProcessCreateIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
1,300✔
2898
  SVCreateStbReq req = {0};
1,300✔
2899
  SDecoder       dc = {0};
1,300✔
2900
  int32_t        code = 0;
1,300✔
2901

2902
  pRsp->msgType = TDMT_VND_CREATE_INDEX_RSP;
1,300✔
2903
  pRsp->code = TSDB_CODE_SUCCESS;
1,300✔
2904
  pRsp->pCont = NULL;
1,300✔
2905
  pRsp->contLen = 0;
1,300✔
2906

2907
  tDecoderInit(&dc, pReq, len);
1,300✔
2908
  // decode req
2909
  if (tDecodeSVCreateStbReq(&dc, &req) < 0) {
1,300!
2910
    tDecoderClear(&dc);
×
2911
    return terrno = TSDB_CODE_INVALID_MSG;
×
2912
  }
2913

2914
  code = metaAddIndexToSuperTable(pVnode->pMeta, ver, &req);
1,300✔
2915
  if (code) {
1,300!
2916
    pRsp->code = code;
×
2917
    goto _err;
×
2918
  }
2919
  tDecoderClear(&dc);
1,300✔
2920
  return 0;
1,300✔
2921

2922
_err:
×
2923
  tDecoderClear(&dc);
×
2924
  return code;
×
2925
}
2926
static int32_t vnodeProcessDropIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
2,986✔
2927
  SDropIndexReq req = {0};
2,986✔
2928
  int32_t       code = 0;
2,986✔
2929
  pRsp->msgType = TDMT_VND_DROP_INDEX_RSP;
2,986✔
2930
  pRsp->code = TSDB_CODE_SUCCESS;
2,986✔
2931
  pRsp->pCont = NULL;
2,986✔
2932
  pRsp->contLen = 0;
2,986✔
2933

2934
  if ((code = tDeserializeSDropIdxReq(pReq, len, &req))) {
2,986!
2935
    pRsp->code = code;
×
2936
    return code;
×
2937
  }
2938

2939
  code = metaDropIndexFromSuperTable(pVnode->pMeta, ver, &req);
2,986✔
2940
  if (code) {
2,986!
2941
    pRsp->code = code;
×
2942
    return code;
×
2943
  }
2944
  return TSDB_CODE_SUCCESS;
2,986✔
2945
}
2946

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

2949
static int32_t vnodeProcessCompactVnodeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
173✔
2950
  if (!pVnode->restored) {
173✔
2951
    vInfo("vgId:%d, ignore compact req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
47!
2952
    return 0;
47✔
2953
  }
2954
  return vnodeAsyncCompact(pVnode, ver, pReq, len, pRsp);
126✔
2955
}
2956

2957
static int32_t vnodeProcessConfigChangeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
2958
  if (syncCheckMember(pVnode->sync) != 0) {
×
2959
    vError("vgId:%d, failed to check member", TD_VID(pVnode));
×
2960
  }
2961

2962
  pRsp->msgType = TDMT_SYNC_CONFIG_CHANGE_RSP;
×
2963
  pRsp->code = TSDB_CODE_SUCCESS;
×
2964
  pRsp->pCont = NULL;
×
2965
  pRsp->contLen = 0;
×
2966

2967
  return 0;
×
2968
}
2969

2970
static int32_t vnodeCheckToken(SVnode *pVnode, char *member0Token, char *member1Token) {
182✔
2971
  SSyncState syncState = syncGetState(pVnode->sync);
182✔
2972
  if (syncState.state != TAOS_SYNC_STATE_LEADER) {
182✔
2973
    return terrno = TSDB_CODE_SYN_NOT_LEADER;
139✔
2974
  }
2975

2976
  char token[TSDB_ARB_TOKEN_SIZE] = {0};
43✔
2977
  if (vnodeGetArbToken(pVnode, token) != 0) {
43!
2978
    return terrno = TSDB_CODE_NOT_FOUND;
×
2979
  }
2980

2981
  if (strncmp(token, member0Token, TSDB_ARB_TOKEN_SIZE) != 0 &&
43✔
2982
      strncmp(token, member1Token, TSDB_ARB_TOKEN_SIZE) != 0) {
37✔
2983
    return terrno = TSDB_CODE_MND_ARB_TOKEN_MISMATCH;
31✔
2984
  }
2985

2986
  terrno = TSDB_CODE_SUCCESS;
12✔
2987
  return 0;
12✔
2988
}
2989

2990
static int32_t vnodeCheckSyncd(SVnode *pVnode, char *member0Token, char *member1Token) {
14✔
2991
  int32_t code = vnodeCheckToken(pVnode, member0Token, member1Token);
14✔
2992
  if (code != 0) {
14✔
2993
    return code;
8✔
2994
  }
2995

2996
  return syncCheckSynced(pVnode->sync);
6✔
2997
}
2998

2999
static int32_t vnodeProcessArbCheckSyncReq(SVnode *pVnode, void *pReq, int32_t len, SRpcMsg *pRsp) {
14✔
3000
  int32_t code = 0;
14✔
3001

3002
  SVArbCheckSyncReq syncReq = {0};
14✔
3003

3004
  code = tDeserializeSVArbCheckSyncReq(pReq, len, &syncReq);
14✔
3005
  if (code) {
14!
3006
    return code;
×
3007
  }
3008

3009
  vInfo("vgId:%d, start to process vnode-arb-check-sync req QID:0x%" PRIx64 ":0x%" PRIx64 ", seqNum:%" PRIx64
14!
3010
        ", arbToken:%s, member0Token:%s, member1Token:%s, "
3011
        "arbTerm:%" PRId64,
3012
        TD_VID(pVnode), pRsp->info.traceId.rootId, pRsp->info.traceId.msgId, pRsp->info.seqNum, syncReq.arbToken,
3013
        syncReq.member0Token, syncReq.member1Token, syncReq.arbTerm);
3014

3015
  pRsp->msgType = TDMT_VND_ARB_CHECK_SYNC_RSP;
14✔
3016
  pRsp->code = TSDB_CODE_SUCCESS;
14✔
3017
  pRsp->pCont = NULL;
14✔
3018
  pRsp->contLen = 0;
14✔
3019

3020
  SVArbCheckSyncRsp syncRsp = {0};
14✔
3021
  syncRsp.arbToken = syncReq.arbToken;
14✔
3022
  syncRsp.member0Token = syncReq.member0Token;
14✔
3023
  syncRsp.member1Token = syncReq.member1Token;
14✔
3024
  syncRsp.vgId = TD_VID(pVnode);
14✔
3025

3026
  if ((syncRsp.errCode = vnodeCheckSyncd(pVnode, syncReq.member0Token, syncReq.member1Token)) != 0) {
14✔
3027
    vError("vgId:%d, failed to check assigned log syncd since %s", TD_VID(pVnode), tstrerror(syncRsp.errCode));
8!
3028
  }
3029

3030
  if ((code = vnodeUpdateArbTerm(pVnode, syncReq.arbTerm)) != 0) {
14!
3031
    vError("vgId:%d, failed to update arb term since %s", TD_VID(pVnode), tstrerror(code));
×
3032
    goto _OVER;
×
3033
  }
3034

3035
  int32_t contLen = tSerializeSVArbCheckSyncRsp(NULL, 0, &syncRsp);
14✔
3036
  if (contLen <= 0) {
14!
3037
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
3038
    code = -1;
×
3039
    goto _OVER;
×
3040
  }
3041
  void *pHead = rpcMallocCont(contLen);
14✔
3042
  if (!pHead) {
14!
3043
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
3044
    code = -1;
×
3045
    goto _OVER;
×
3046
  }
3047

3048
  if (tSerializeSVArbCheckSyncRsp(pHead, contLen, &syncRsp) <= 0) {
14!
3049
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
3050
    rpcFreeCont(pHead);
×
3051
    code = -1;
×
3052
    goto _OVER;
×
3053
  }
3054

3055
  pRsp->pCont = pHead;
14✔
3056
  pRsp->contLen = contLen;
14✔
3057

3058
  vInfo(
14!
3059
      "vgId:%d, suceed to process vnode-arb-check-sync req rsp.code:%s, arbToken:%s, member0Token:%s, "
3060
      "member1Token:%s",
3061
      TD_VID(pVnode), tstrerror(syncRsp.errCode), syncRsp.arbToken, syncRsp.member0Token, syncRsp.member1Token);
3062

3063
  code = TSDB_CODE_SUCCESS;
14✔
3064

3065
_OVER:
14✔
3066
  if (code != 0) {
14!
3067
    vError("vgId:%d, failed to process arb check req rsp.code:%s since %s", TD_VID(pVnode), tstrerror(syncRsp.errCode),
×
3068
           tstrerror(code));
3069
  }
3070
  tFreeSVArbCheckSyncReq(&syncReq);
14✔
3071
  return code;
14✔
3072
}
3073

3074
#ifndef TD_ENTERPRISE
3075
int32_t vnodeAsyncCompact(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) { return 0; }
3076
int32_t tsdbAsyncCompact(STsdb *tsdb, const STimeWindow *tw, bool sync) { return 0; }
3077
#endif
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc