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

taosdata / TDengine / #4727

08 Sep 2025 08:43AM UTC coverage: 59.125% (+0.01%) from 59.112%
#4727

push

travis-ci

web-flow
Merge pull request #32881 from taosdata/enh/add-new-windows-ci

fix(ci): update workflow reference to use new Windows CI YAML

135884 of 292179 branches covered (46.51%)

Branch coverage included in aggregate %.

204671 of 283811 relevant lines covered (72.12%)

29258962.23 hits per line

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

55.02
/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 vnodeProcessDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
49
                                     SRpcMsg *pOriginalMsg);
50
static int32_t vnodeProcessBatchDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
51
static int32_t vnodeProcessCreateIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
52
static int32_t vnodeProcessDropIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
53
static int32_t vnodeProcessCompactVnodeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
54
static int32_t vnodeProcessConfigChangeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
55
static int32_t vnodeProcessArbCheckSyncReq(SVnode *pVnode, void *pReq, int32_t len, SRpcMsg *pRsp);
56
static int32_t vnodeProcessDropTSmaCtbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
57
                                          SRpcMsg *pOriginRpc);
58

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

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

67
extern int32_t vnodeListSsMigrateFileSets(SVnode *pVnode, SRpcMsg *pMsg);
68
static int32_t vnodeProcessSsMigrateFileSetReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
69
extern int32_t vnodeQuerySsMigrateProgress(SVnode *pVnode, SRpcMsg *pMsg);
70
static int32_t vnodeProcessFollowerSsMigrateReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
71

72
static int32_t vnodePreprocessCreateTableReq(SVnode *pVnode, SDecoder *pCoder, int64_t btime, int64_t *pUid) {
200,087✔
73
  int32_t code = 0;
200,087✔
74
  int32_t lino = 0;
200,087✔
75

76
  if (tStartDecode(pCoder) < 0) {
200,087!
77
    code = TSDB_CODE_INVALID_MSG;
×
78
    TSDB_CHECK_CODE(code, lino, _exit);
×
79
  }
80

81
  // flags
82
  if (tDecodeI32v(pCoder, NULL) < 0) {
200,153!
83
    code = TSDB_CODE_INVALID_MSG;
×
84
    TSDB_CHECK_CODE(code, lino, _exit);
×
85
  }
86

87
  // name
88
  char *name = NULL;
200,153✔
89
  if (tDecodeCStr(pCoder, &name) < 0) {
200,002!
90
    code = TSDB_CODE_INVALID_MSG;
×
91
    TSDB_CHECK_CODE(code, lino, _exit);
×
92
  }
93

94
  // uid
95
  int64_t uid = metaGetTableEntryUidByName(pVnode->pMeta, name);
200,002✔
96
  if (uid == 0) {
200,254✔
97
    uid = tGenIdPI64();
190,194✔
98
  }
99
  taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), uid);
200,180✔
100

101
  // btime
102
  taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos + 8), btime);
200,180✔
103

104
  tEndDecode(pCoder);
200,180✔
105

106
_exit:
200,123✔
107
  if (code) {
200,123!
108
    vError("vgId:%d %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
109
  } else {
110
    vTrace("vgId:%d %s done, table:%s uid generated:%" PRId64, TD_VID(pVnode), __func__, name, uid);
200,123✔
111
    if (pUid) *pUid = uid;
200,123✔
112
  }
113
  return code;
200,121✔
114
}
115
static int32_t vnodePreProcessCreateTableMsg(SVnode *pVnode, SRpcMsg *pMsg) {
94,163✔
116
  int32_t code = 0;
94,163✔
117
  int32_t lino = 0;
94,163✔
118

119
  int64_t  btime = taosGetTimestampMs();
94,166✔
120
  SDecoder dc = {0};
94,166✔
121
  int32_t  nReqs;
122

123
  tDecoderInit(&dc, (uint8_t *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead));
94,166✔
124
  if (tStartDecode(&dc) < 0) {
94,161!
125
    code = TSDB_CODE_INVALID_MSG;
×
126
    return code;
×
127
  }
128

129
  if (tDecodeI32v(&dc, &nReqs) < 0) {
94,161!
130
    code = TSDB_CODE_INVALID_MSG;
×
131
    TSDB_CHECK_CODE(code, lino, _exit);
×
132
  }
133
  for (int32_t iReq = 0; iReq < nReqs; iReq++) {
236,456✔
134
    code = vnodePreprocessCreateTableReq(pVnode, &dc, btime, NULL);
142,306✔
135
    TSDB_CHECK_CODE(code, lino, _exit);
142,295!
136
  }
137

138
  tEndDecode(&dc);
94,150✔
139

140
_exit:
94,157✔
141
  tDecoderClear(&dc);
94,157✔
142
  if (code) {
94,162!
143
    vError("vgId:%d, %s:%d failed to preprocess submit request since %s, msg type:%s", TD_VID(pVnode), __func__, lino,
×
144
           tstrerror(code), TMSG_INFO(pMsg->msgType));
145
  }
146
  return code;
94,160✔
147
}
148

149
static int32_t vnodePreProcessAlterTableMsg(SVnode *pVnode, SRpcMsg *pMsg) {
19,761✔
150
  int32_t code = TSDB_CODE_INVALID_MSG;
19,761✔
151
  int32_t lino = 0;
19,761✔
152

153
  SDecoder dc = {0};
19,761✔
154
  tDecoderInit(&dc, (uint8_t *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead));
19,761✔
155

156
  SVAlterTbReq vAlterTbReq = {0};
19,761✔
157
  int64_t      ctimeMs = taosGetTimestampMs();
19,761✔
158
  if (tDecodeSVAlterTbReqSetCtime(&dc, &vAlterTbReq, ctimeMs) < 0) {
19,761!
159
    taosArrayDestroy(vAlterTbReq.pMultiTag);
×
160
    vAlterTbReq.pMultiTag = NULL;
×
161
    goto _exit;
×
162
  }
163
  taosArrayDestroy(vAlterTbReq.pMultiTag);
19,761✔
164
  vAlterTbReq.pMultiTag = NULL;
19,761✔
165

166
  code = 0;
19,761✔
167

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

179
static int32_t vnodePreProcessDropTtlMsg(SVnode *pVnode, SRpcMsg *pMsg) {
159,567✔
180
  int32_t code = TSDB_CODE_INVALID_MSG;
159,567✔
181
  int32_t lino = 0;
159,567✔
182

183
  SMsgHead *pContOld = pMsg->pCont;
159,567✔
184
  int32_t   reqLenOld = pMsg->contLen - sizeof(SMsgHead);
159,567✔
185

186
  SArray *tbUids = NULL;
159,567✔
187
  int64_t timestampMs = 0;
159,567✔
188

189
  SVDropTtlTableReq ttlReq = {0};
159,567✔
190
  if (tDeserializeSVDropTtlTableReq((char *)pContOld + sizeof(SMsgHead), reqLenOld, &ttlReq) != 0) {
159,567!
191
    code = TSDB_CODE_INVALID_MSG;
×
192
    TSDB_CHECK_CODE(code, lino, _exit);
×
193
  }
194

195
  {  // find expired uids
196
    tbUids = taosArrayInit(8, sizeof(tb_uid_t));
159,982✔
197
    if (tbUids == NULL) {
159,995!
198
      code = terrno;
×
199
      TSDB_CHECK_CODE(code, lino, _exit);
×
200
    }
201

202
    timestampMs = (int64_t)ttlReq.timestampSec * 1000;
159,995✔
203
    code = metaTtlFindExpired(pVnode->pMeta, timestampMs, tbUids, ttlReq.ttlDropMaxCount);
159,995✔
204
    if (code != 0) {
159,844!
205
      code = TSDB_CODE_INVALID_MSG;
×
206
      TSDB_CHECK_CODE(code, lino, _exit);
×
207
    }
208

209
    ttlReq.nUids = taosArrayGetSize(tbUids);
159,844✔
210
    ttlReq.pTbUids = tbUids;
158,962✔
211
  }
212

213
  if (ttlReq.nUids == 0) {
158,962✔
214
    code = TSDB_CODE_MSG_PREPROCESSED;
158,936✔
215
    TSDB_CHECK_CODE(code, lino, _exit);
158,936!
216
  }
217

218
  {  // prepare new content
219
    int32_t reqLenNew = tSerializeSVDropTtlTableReq(NULL, 0, &ttlReq);
×
220
    int32_t contLenNew = reqLenNew + sizeof(SMsgHead);
31✔
221

222
    SMsgHead *pContNew = rpcMallocCont(contLenNew);
31✔
223
    if (pContNew == NULL) {
31!
224
      code = terrno;
×
225
      TSDB_CHECK_CODE(code, lino, _exit);
×
226
    }
227

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

234
    rpcFreeCont(pContOld);
31✔
235
    pMsg->pCont = pContNew;
31✔
236
    pMsg->contLen = contLenNew;
31✔
237
  }
238

239
  code = 0;
31✔
240

241
_exit:
159,004✔
242
  taosArrayDestroy(tbUids);
159,004✔
243

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

251
  return code;
159,531✔
252
}
253

254
extern int64_t tsMaxKeyByPrecision[];
255
static int32_t vnodePreProcessSubmitTbData(SVnode *pVnode, SDecoder *pCoder, int64_t btimeMs, int64_t ctimeMs) {
18,966,439✔
256
  int32_t code = 0;
18,966,439✔
257
  int32_t lino = 0;
18,966,439✔
258

259
  if (tStartDecode(pCoder) < 0) {
18,966,439!
260
    code = TSDB_CODE_INVALID_MSG;
×
261
    TSDB_CHECK_CODE(code, lino, _exit);
×
262
  }
263

264
  SSubmitTbData submitTbData;
265
  uint8_t       version;
266
  if (tDecodeI32v(pCoder, &submitTbData.flags) < 0) {
18,967,095!
267
    code = TSDB_CODE_INVALID_MSG;
×
268
    TSDB_CHECK_CODE(code, lino, _exit);
×
269
  }
270
  version = (submitTbData.flags >> 8) & 0xff;
18,967,095✔
271
  submitTbData.flags = submitTbData.flags & 0xff;
18,967,095✔
272

273
  int64_t uid;
274
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
18,967,095✔
275
    code = vnodePreprocessCreateTableReq(pVnode, pCoder, btimeMs, &uid);
57,786✔
276
    TSDB_CHECK_CODE(code, lino, _exit);
57,824!
277
  }
278

279
  // submit data
280
  if (tDecodeI64(pCoder, &submitTbData.suid) < 0) {
18,967,060!
281
    code = TSDB_CODE_INVALID_MSG;
×
282
    TSDB_CHECK_CODE(code, lino, _exit);
×
283
  }
284

285
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
18,967,060✔
286
    taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), uid);
57,814✔
287
    pCoder->pos += sizeof(int64_t);
57,814✔
288
  } else {
289
    if (tDecodeI64(pCoder, &submitTbData.uid) < 0) {
18,909,207!
290
      code = TSDB_CODE_INVALID_MSG;
×
291
      TSDB_CHECK_CODE(code, lino, _exit);
×
292
    }
293
  }
294

295
  if (tDecodeI32v(pCoder, &submitTbData.sver) < 0) {
18,966,888!
296
    code = TSDB_CODE_INVALID_MSG;
×
297
    TSDB_CHECK_CODE(code, lino, _exit);
×
298
  }
299

300
  // scan and check
301
  TSKEY now = btimeMs;
18,966,888✔
302
  if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_MICRO) {
18,966,888✔
303
    now *= 1000;
24,451✔
304
  } else if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_NANO) {
18,942,437✔
305
    now *= 1000000;
2,934✔
306
  }
307

308
  int32_t keep = pVnode->config.tsdbCfg.keep2;
18,966,888✔
309

310
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * keep;
18,966,888✔
311
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
18,966,888✔
312
  if (submitTbData.flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
18,966,888✔
313
    uint64_t nColData;
314
    if (tDecodeU64v(pCoder, &nColData) < 0) {
1,147!
315
      code = TSDB_CODE_INVALID_MSG;
×
316
      TSDB_CHECK_CODE(code, lino, _exit);
1!
317
    }
318

319
    SColData colData = {0};
1,147✔
320
    code = tDecodeColData(version, pCoder, &colData);
1,147✔
321
    if (code) {
1,147!
322
      code = TSDB_CODE_INVALID_MSG;
×
323
      TSDB_CHECK_CODE(code, lino, _exit);
×
324
    }
325

326
    if (colData.flag != HAS_VALUE) {
1,147!
327
      code = TSDB_CODE_INVALID_MSG;
×
328
      TSDB_CHECK_CODE(code, lino, _exit);
×
329
    }
330

331
    for (int32_t iRow = 0; iRow < colData.nVal; iRow++) {
51,489✔
332
      if (((TSKEY *)colData.pData)[iRow] < minKey || ((TSKEY *)colData.pData)[iRow] > maxKey) {
50,343!
333
        code = TSDB_CODE_TDB_TIMESTAMP_OUT_OF_RANGE;
1✔
334
        TSDB_CHECK_CODE(code, lino, _exit);
1!
335
      }
336
    }
337

338
    for (uint64_t i = 1; i < nColData; i++) {
7,238✔
339
      code = tDecodeColData(version, pCoder, &colData);
6,092✔
340
      if (code) {
6,092!
341
        code = TSDB_CODE_INVALID_MSG;
×
342
        TSDB_CHECK_CODE(code, lino, _exit);
×
343
      }
344
    }
345
  } else {
346
    uint64_t nRow;
347
    if (tDecodeU64v(pCoder, &nRow) < 0) {
18,965,618!
348
      code = TSDB_CODE_INVALID_MSG;
×
349
      TSDB_CHECK_CODE(code, lino, _exit);
1,300!
350
    }
351

352
    for (int32_t iRow = 0; iRow < nRow; ++iRow) {
1,743,469,058✔
353
      SRow *pRow = (SRow *)(pCoder->data + pCoder->pos);
1,724,504,740✔
354
      pCoder->pos += pRow->len;
1,724,504,740✔
355
#ifndef NO_UNALIGNED_ACCESS
356
      if (pRow->ts < minKey || pRow->ts > maxKey) {
1,724,504,740✔
357
#else
358
      TSKEY ts = taosGetInt64Aligned(&pRow->ts);
359
      if (ts < minKey || ts > maxKey) {
360
#endif
361
        code = TSDB_CODE_TDB_TIMESTAMP_OUT_OF_RANGE;
2,523✔
362
        TSDB_CHECK_CODE(code, lino, _exit);
2,523✔
363
      }
364

365
      // Check pRow->sver
366
      if (pRow->sver != submitTbData.sver) {
1,724,503,440!
367
        code = TSDB_CODE_INVALID_DATA_FMT;
×
368
        TSDB_CHECK_CODE(code, lino, _exit);
×
369
      }
370
    }
371
  }
372

373
  if (!tDecodeIsEnd(pCoder)) {
18,965,464!
374
    taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), ctimeMs);
18,965,685✔
375
    pCoder->pos += sizeof(int64_t);
18,965,685✔
376
  }
377

378
  tEndDecode(pCoder);
18,965,464✔
379

380
_exit:
18,966,407✔
381
  if (code) {
18,966,407✔
382
    vError("vgId:%d, %s:%d failed to vnodePreProcessSubmitTbData submit request since %s", TD_VID(pVnode), __func__,
1,301!
383
           lino, tstrerror(code));
384
  }
385
  return code;
18,966,396✔
386
}
387
static int32_t vnodePreProcessSubmitMsg(SVnode *pVnode, SRpcMsg *pMsg) {
18,890,477✔
388
  int32_t code = 0;
18,890,477✔
389
  int32_t lino = 0;
18,890,477✔
390

391
  if (tsBypassFlag & TSDB_BYPASS_RA_RPC_RECV_SUBMIT) {
18,890,477✔
392
    return TSDB_CODE_MSG_PREPROCESSED;
1✔
393
  }
394

395
  SDecoder *pCoder = &(SDecoder){0};
18,890,476✔
396

397
  if (taosHton64(((SSubmitReq2Msg *)pMsg->pCont)->version) != 1) {
18,890,476!
398
    code = TSDB_CODE_INVALID_MSG;
×
399
    TSDB_CHECK_CODE(code, lino, _exit);
×
400
  }
401

402
  tDecoderInit(pCoder, (uint8_t *)pMsg->pCont + sizeof(SSubmitReq2Msg), pMsg->contLen - sizeof(SSubmitReq2Msg));
18,890,576✔
403

404
  if (tStartDecode(pCoder) < 0) {
18,890,436!
405
    code = TSDB_CODE_INVALID_MSG;
×
406
    TSDB_CHECK_CODE(code, lino, _exit);
×
407
  }
408

409
  uint64_t nSubmitTbData;
410
  if (tDecodeU64v(pCoder, &nSubmitTbData) < 0) {
18,890,511!
411
    code = TSDB_CODE_INVALID_MSG;
×
412
    TSDB_CHECK_CODE(code, lino, _exit);
×
413
  }
414

415
  int64_t btimeMs = taosGetTimestampMs();
18,890,604✔
416
  int64_t ctimeMs = btimeMs;
18,890,604✔
417
  for (int32_t i = 0; i < nSubmitTbData; i++) {
37,855,728✔
418
    code = vnodePreProcessSubmitTbData(pVnode, pCoder, btimeMs, ctimeMs);
18,966,549✔
419
    TSDB_CHECK_CODE(code, lino, _exit);
18,966,426✔
420
  }
421

422
  tEndDecode(pCoder);
18,889,179✔
423

424
_exit:
18,890,460✔
425
  tDecoderClear(pCoder);
18,890,460✔
426
  if (code) {
18,890,545✔
427
    vError("vgId:%d, %s:%d failed to preprocess submit request since %s, msg type:%s", TD_VID(pVnode), __func__, lino,
1,302!
428
           tstrerror(code), TMSG_INFO(pMsg->msgType));
429
  }
430
  return code;
18,890,526✔
431
}
432

433
static int32_t vnodePreProcessDeleteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
140,378✔
434
  int32_t code = 0;
140,378✔
435

436
  int32_t    size;
437
  int32_t    ret;
438
  uint8_t   *pCont;
439
  SEncoder  *pCoder = &(SEncoder){0};
140,378✔
440
  SDeleteRes res = {0};
140,378✔
441

442
  SReadHandle handle = {0};
140,378✔
443
  handle.vnode = pVnode;
140,378✔
444
  handle.pMsgCb = &pVnode->msgCb;
140,378✔
445
  handle.skipRollup = 1;
140,378✔
446
  initStorageAPI(&handle.api);
140,378✔
447

448
  code = qWorkerProcessDeleteMsg(&handle, pVnode->pQuery, pMsg, &res);
140,388✔
449
  if (code) goto _exit;
140,382!
450

451
  res.ctimeMs = taosGetTimestampMs();
140,387✔
452
  // malloc and encode
453
  tEncodeSize(tEncodeDeleteRes, &res, size, ret);
140,387!
454
  pCont = rpcMallocCont(size + sizeof(SMsgHead));
140,377✔
455

456
  ((SMsgHead *)pCont)->contLen = size + sizeof(SMsgHead);
140,385✔
457
  ((SMsgHead *)pCont)->vgId = TD_VID(pVnode);
140,385✔
458

459
  tEncoderInit(pCoder, pCont + sizeof(SMsgHead), size);
140,385✔
460
  if (tEncodeDeleteRes(pCoder, &res) != 0) {
140,388!
461
    vError("vgId:%d %s failed to encode delete response", TD_VID(pVnode), __func__);
×
462
  }
463
  tEncoderClear(pCoder);
140,385✔
464

465
  rpcFreeCont(pMsg->pCont);
140,385✔
466
  pMsg->pCont = pCont;
140,388✔
467
  pMsg->contLen = size + sizeof(SMsgHead);
140,388✔
468

469
  taosArrayDestroy(res.uidList);
140,388✔
470

471
_exit:
140,390✔
472
  return code;
140,390✔
473
}
474

475
static int32_t vnodePreProcessBatchDeleteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
×
476
  int32_t code = 0;
×
477
  int32_t lino = 0;
×
478

479
  int64_t         ctimeMs = taosGetTimestampMs();
×
480
  SBatchDeleteReq pReq = {0};
×
481
  SDecoder       *pCoder = &(SDecoder){0};
×
482

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

485
  if (tDecodeSBatchDeleteReqSetCtime(pCoder, &pReq, ctimeMs) < 0) {
×
486
    code = TSDB_CODE_INVALID_MSG;
×
487
  }
488

489
  tDecoderClear(pCoder);
×
490
  taosArrayDestroy(pReq.deleteReqs);
×
491

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

500
static int32_t vnodePreProcessArbCheckSyncMsg(SVnode *pVnode, SRpcMsg *pMsg) {
48✔
501
  int32_t ret = 0;
48✔
502
  if ((ret = vnodeCheckState(pVnode)) != 0) {
48✔
503
    vDebug("vgId:%d, failed to preprocess vnode-arb-check-sync request since %s", TD_VID(pVnode), tstrerror(ret));
26!
504
    return 0;
26✔
505
  }
506

507
  SVArbCheckSyncReq syncReq = {0};
22✔
508

509
  if (tDeserializeSVArbCheckSyncReq((char *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead),
22!
510
                                    &syncReq) != 0) {
511
    return TSDB_CODE_INVALID_MSG;
×
512
  }
513

514
  ret = vnodeCheckToken(pVnode, syncReq.member0Token, syncReq.member1Token);
22✔
515
  if (ret != 0) {
22✔
516
    vError("vgId:%d, failed to preprocess vnode-arb-check-sync request since %s", TD_VID(pVnode), tstrerror(ret));
15!
517
  }
518

519
  int32_t code = terrno;
22✔
520
  tFreeSVArbCheckSyncReq(&syncReq);
22✔
521

522
  return code;
22✔
523
}
524

525
int32_t vnodePreProcessDropTbMsg(SVnode *pVnode, SRpcMsg *pMsg) {
15,723✔
526
  int32_t          code = TSDB_CODE_SUCCESS;
15,723✔
527
  int32_t          lino = 0;
15,723✔
528
  int32_t          size = 0;
15,723✔
529
  SDecoder         dc = {0};
15,723✔
530
  SEncoder         ec = {0};
15,723✔
531
  SVDropTbBatchReq receivedBatchReqs = {0};
15,723✔
532
  SVDropTbBatchReq sentBatchReqs = {0};
15,723✔
533

534
  tDecoderInit(&dc, POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), pMsg->contLen - sizeof(SMsgHead));
15,723✔
535

536
  code = tDecodeSVDropTbBatchReq(&dc, &receivedBatchReqs);
15,723✔
537
  if (code < 0) {
15,723!
538
    terrno = code;
×
539
    TSDB_CHECK_CODE(code, lino, _exit);
×
540
  }
541
  sentBatchReqs.pArray = taosArrayInit(receivedBatchReqs.nReqs, sizeof(SVDropTbReq));
15,723✔
542
  if (!sentBatchReqs.pArray) {
15,723!
543
    code = terrno;
×
544
    goto _exit;
×
545
  }
546

547
  for (int32_t i = 0; i < receivedBatchReqs.nReqs; ++i) {
31,849✔
548
    SVDropTbReq *pReq = receivedBatchReqs.pReqs + i;
16,126✔
549
    tb_uid_t     uid = metaGetTableEntryUidByName(pVnode->pMeta, pReq->name);
16,126✔
550
    if (uid == 0) {
16,126!
551
      vWarn("vgId:%d, preprocess drop ctb: %s not found", TD_VID(pVnode), pReq->name);
×
552
      continue;
×
553
    }
554
    pReq->uid = uid;
16,126✔
555
    vDebug("vgId:%d %s for: %s, uid: %" PRId64, TD_VID(pVnode), __func__, pReq->name, pReq->uid);
16,126✔
556
    if (taosArrayPush(sentBatchReqs.pArray, pReq) == NULL) {
32,252!
557
      code = terrno;
×
558
      goto _exit;
×
559
    }
560
  }
561
  sentBatchReqs.nReqs = sentBatchReqs.pArray->size;
15,723✔
562

563
  tEncodeSize(tEncodeSVDropTbBatchReq, &sentBatchReqs, size, code);
15,723!
564
  tEncoderInit(&ec, POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), size);
15,723✔
565
  code = tEncodeSVDropTbBatchReq(&ec, &sentBatchReqs);
15,723✔
566
  tEncoderClear(&ec);
15,723✔
567
  if (code != TSDB_CODE_SUCCESS) {
15,723!
568
    vError("vgId:%d %s failed to encode drop tb batch req: %s", TD_VID(pVnode), __func__, tstrerror(code));
×
569
    TSDB_CHECK_CODE(code, lino, _exit);
×
570
  }
571

572
_exit:
15,723✔
573
  tDecoderClear(&dc);
15,723✔
574
  if (sentBatchReqs.pArray) {
15,723!
575
    taosArrayDestroy(sentBatchReqs.pArray);
15,723✔
576
  }
577
  return code;
15,723✔
578
}
579

580

581
int32_t vnodePreProcessSsMigrateFileSetReq(SVnode* pVnode, SRpcMsg* pMsg) {
×
582
  int32_t          code = TSDB_CODE_SUCCESS, lino = 0;
×
583
  SSsMigrateFileSetReq req = {0};
×
584

585
  code = tDeserializeSSsMigrateFileSetReq(POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), pMsg->contLen - sizeof(SMsgHead), &req);
×
586
  if (code < 0) {
×
587
    terrno = code;
×
588
    TSDB_CHECK_CODE(code, lino, _exit);
×
589
  }
590

591
  req.nodeId = vnodeNodeId(pVnode);
×
592
  TAOS_UNUSED(tSerializeSSsMigrateFileSetReq(POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), pMsg->contLen - sizeof(SMsgHead), &req));
×
593

594
_exit:
×
595
  return code;
×
596
}
597

598
int32_t vnodePreProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
19,423,371✔
599
  int32_t code = 0;
19,423,371✔
600

601
  if (pVnode->mounted) {
19,423,371✔
602
    vError("vgId:%d, mountVgId:%d, skip write msg:%s", TD_VID(pVnode), pVnode->config.mountVgId,
10!
603
           TMSG_INFO(pMsg->msgType));
604
    return TSDB_CODE_OPS_NOT_SUPPORT;
10✔
605
  }
606

607
  switch (pMsg->msgType) {
19,423,361!
608
    case TDMT_VND_CREATE_TABLE: {
94,165✔
609
      code = vnodePreProcessCreateTableMsg(pVnode, pMsg);
94,165✔
610
    } break;
94,159✔
611
    case TDMT_VND_ALTER_TABLE: {
19,761✔
612
      code = vnodePreProcessAlterTableMsg(pVnode, pMsg);
19,761✔
613
    } break;
19,761✔
614
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
159,854✔
615
    case TDMT_VND_DROP_TTL_TABLE: {
616
      code = vnodePreProcessDropTtlMsg(pVnode, pMsg);
159,854✔
617
    } break;
159,452✔
618
    case TDMT_VND_SUBMIT: {
18,890,525✔
619
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.preprocess_time, METRIC_LEVEL_LOW,
18,890,525!
620
                           { code = vnodePreProcessSubmitMsg(pVnode, pMsg); });
621
    } break;
18,890,497✔
622
    case TDMT_VND_DELETE: {
140,379✔
623
      code = vnodePreProcessDeleteMsg(pVnode, pMsg);
140,379✔
624
    } break;
140,380✔
625
    case TDMT_VND_BATCH_DEL: {
×
626
      code = vnodePreProcessBatchDeleteMsg(pVnode, pMsg);
×
627
    } break;
×
628
    case TDMT_VND_ARB_CHECK_SYNC: {
48✔
629
      code = vnodePreProcessArbCheckSyncMsg(pVnode, pMsg);
48✔
630
    } break;
48✔
631
    case TDMT_VND_DROP_TABLE: {
15,723✔
632
      code = vnodePreProcessDropTbMsg(pVnode, pMsg);
15,723✔
633
    } break;
15,723✔
634
#ifdef TD_ENTERPRISE
635
    case TDMT_VND_SSMIGRATE_FILESET: {
×
636
      code = vnodePreProcessSsMigrateFileSetReq(pVnode, pMsg);
×
637
    } break;
×
638
#endif
639
    default:
102,906✔
640
      break;
102,906✔
641
  }
642

643
  if (code && code != TSDB_CODE_MSG_PREPROCESSED) {
19,422,926✔
644
    vError("vgId:%d, failed to preprocess write request since %s, msg type:%s", TD_VID(pVnode), tstrerror(code),
1,317!
645
           TMSG_INFO(pMsg->msgType));
646
  }
647
  return code;
19,422,589✔
648
}
649

650
static int32_t inline vnodeSubmitSubRowBlobData(SVnode *pVnode, SSubmitTbData *pSubmitTbData) {
×
651
  int32_t code = 0;
×
652
  int32_t lino = 0;
×
653

654
  int64_t   st = taosGetTimestampUs();
×
655
  SBlobSet *pBlobSet = pSubmitTbData->pBlobSet;
×
656
  int32_t   sz = taosArrayGetSize(pBlobSet->pSeqTable);
×
657

658
  SBseBatch *pBatch = NULL;
×
659

660
  code = bseBatchInit(pVnode->pBse, &pBatch, sz);
×
661
  TSDB_CHECK_CODE(code, lino, _exit);
×
662

663
  SRow  **pRow = (SRow **)TARRAY_DATA(pSubmitTbData->aRowP);
×
664
  int32_t rowIdx = -1;
×
665
  for (int32_t i = 0; i < sz; i++) {
×
666
    int64_t     seq = 0;
×
667
    SBlobValue *p = taosArrayGet(pBlobSet->pSeqTable, i);
×
668
    if (p->type == TSDB_DATA_BLOB_EMPTY_VALUE || p->type == TSDB_DATA_BLOB_NULL_VALUE) {
×
669
      // skip empty or null blob
670
      continue;
×
671
    }
672

673
    code = bseBatchPut(pBatch, &seq, pBlobSet->data + p->offset, p->len);
×
674
    TSDB_CHECK_CODE(code, lino, _exit);
×
675

676
    SRow *row = taosArrayGetP(pSubmitTbData->aRowP, i);
×
677
    if (row == NULL) {
×
678
      int32_t tlen = taosArrayGetSize(pBlobSet->pSeqTable);
×
679
      uTrace("blob invalid row index:%d, sz:%d, pBlobSet size:%d", rowIdx, sz, tlen);
×
680
      break;
×
681
    }
682

683
    if (tPutU64(row->data + p->dataOffset, seq) < 0) {
×
684
      code = TSDB_CODE_INVALID_MSG;
×
685
      TSDB_CHECK_CODE(code, lino, _exit);
×
686
    }
687
  }
688

689
  code = bseCommitBatch(pVnode->pBse, pBatch);
×
690
  TSDB_CHECK_CODE(code, lino, _exit);
×
691

692
  int64_t cost = taosGetTimestampUs() - st;
×
693
  if (cost >= 500) {
×
694
    vDebug("vgId:%d, %s, cost:%" PRId64 "us, rows:%d, size:%" PRId64 "", TD_VID(pVnode), __func__, cost, sz,
×
695
           pBlobSet->len);
696
  }
697
_exit:
×
698
  if (code != 0) {
×
699
    vError("vgId:%d %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
700
  }
701
  return code;
×
702
}
703

704
static int32_t inline vnodeSubmitSubColBlobData(SVnode *pVnode, SSubmitTbData *pSubmitTbData) {
1✔
705
  int32_t code = 0;
1✔
706
  int32_t lino = 0;
1✔
707

708
  int32_t   blobColIdx = 0;
1✔
709
  SColData *pBlobCol = NULL;
1✔
710
  int64_t   st = taosGetTimestampUs();
1✔
711
  SBlobSet *pBlobSet = pSubmitTbData->pBlobSet;
1✔
712
  int32_t   sz = taosArrayGetSize(pBlobSet->pSeqTable);
1✔
713

714
  SBseBatch *pBatch = NULL;
1✔
715

716
  code = bseBatchInit(pVnode->pBse, &pBatch, sz);
1✔
717
  TSDB_CHECK_CODE(code, lino, _exit);
1!
718

719
  SColData *p = (SColData *)TARRAY_DATA(pSubmitTbData->aCol);
1✔
720
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitTbData->aCol); i++) {
2!
721
    if (IS_STR_DATA_BLOB(p[i].type)) {
2!
722
      pBlobCol = &p[i];
1✔
723
      break;
1✔
724
    }
725
  }
726
  if (pBlobCol == NULL) {
1!
727
    vError("vgId:%d %s failed to find blob column in submit data", TD_VID(pVnode), __func__);
×
728
    code = TSDB_CODE_INVALID_MSG;
×
729
    goto _exit;
×
730
  }
731

732
  int32_t offset = 0;
1✔
733
  // int32_t   rowIdx = -1;
734
  for (int32_t i = 0; i < sz; i++) {
11✔
735
    int64_t     seq = 0;
10✔
736
    SBlobValue *p = taosArrayGet(pBlobSet->pSeqTable, i);
10✔
737
    if (p->type == TSDB_DATA_BLOB_EMPTY_VALUE || p->type == TSDB_DATA_BLOB_NULL_VALUE) {
10!
738
      // skip empty or null blob
739
      continue;
×
740
    }
741
    code = bseBatchPut(pBatch, &seq, pBlobSet->data + p->offset, p->len);
10✔
742
    TSDB_CHECK_CODE(code, lino, _exit);
10!
743

744
    memcpy(pBlobCol->pData + offset, (void *)&seq, BSE_SEQUECE_SIZE);
10✔
745
    offset += BSE_SEQUECE_SIZE;
10✔
746
  }
747

748
  code = bseCommitBatch(pVnode->pBse, pBatch);
1✔
749
  TSDB_CHECK_CODE(code, lino, _exit);
1!
750

751
  int64_t cost = taosGetTimestampUs() - st;
1✔
752
  if (cost >= 500) {
1!
753
    vDebug("vgId:%d, %s, cost:%" PRId64 "us, rows:%d, size:%" PRId64 "", TD_VID(pVnode), __func__, cost, sz,
×
754
           pBlobSet->len);
755
  }
756
_exit:
1✔
757
  if (code != 0) {
1!
758
    vError("vgId:%d %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
759
  }
760
  return code;
1✔
761
}
762
static int32_t inline vnodeSubmitBlobData(SVnode *pVnode, SSubmitTbData *pData) {
1✔
763
  int32_t code = 0;
1✔
764
  if (pData->flags & SUBMIT_REQ_WITH_BLOB) {
1!
765
    if (pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
1!
766
      code = vnodeSubmitSubColBlobData(pVnode, pData);
1✔
767
    } else {
768
      code = vnodeSubmitSubRowBlobData(pVnode, pData);
×
769
    }
770
  }
771

772
  return code;
1✔
773
}
774

775
int32_t vnodeProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg, int64_t ver, SRpcMsg *pRsp) {
25,444,441✔
776
  int32_t code = 0;
25,444,441✔
777
  int32_t lino = 0;
25,444,441✔
778
  void   *ptr = NULL;
25,444,441✔
779
  void   *pReq;
780
  int32_t len;
781

782
  (void)taosThreadMutexLock(&pVnode->mutex);
25,444,441✔
783
  if (pVnode->disableWrite) {
25,444,918!
784
    (void)taosThreadMutexUnlock(&pVnode->mutex);
×
785
    vError("vgId:%d write is disabled for snapshot, version:%" PRId64, TD_VID(pVnode), ver);
×
786
    return TSDB_CODE_VND_WRITE_DISABLED;
×
787
  }
788
  (void)taosThreadMutexUnlock(&pVnode->mutex);
25,444,918✔
789

790
  if (ver <= pVnode->state.applied) {
25,444,557!
791
    vError("vgId:%d, duplicate write request. ver: %" PRId64 ", applied: %" PRId64, TD_VID(pVnode), ver,
×
792
           pVnode->state.applied);
793
    return terrno = TSDB_CODE_VND_DUP_REQUEST;
×
794
  }
795

796
  vGDebug(&pMsg->info.traceId,
25,444,557!
797
          "vgId:%d, index:%" PRId64 ", process write request:%s, applied:%" PRId64 ", state.applyTerm:%" PRId64
798
          ", conn.applyTerm:%" PRId64 ", contLen:%d",
799
          TD_VID(pVnode), ver, TMSG_INFO(pMsg->msgType), pVnode->state.applied, pVnode->state.applyTerm,
800
          pMsg->info.conn.applyTerm, pMsg->contLen);
801

802
  if (!(pVnode->state.applyTerm <= pMsg->info.conn.applyTerm)) {
25,444,568!
803
    vError("vgId:%d, applyTerm mismatch, expected: %" PRId64 ", received: %" PRId64, TD_VID(pVnode),
×
804
           pVnode->state.applyTerm, pMsg->info.conn.applyTerm);
805
    return terrno = TSDB_CODE_INTERNAL_ERROR;
×
806
  }
807

808
  if (!(pVnode->state.applied + 1 == ver)) {
25,444,568!
809
    vError("vgId:%d, mountVgId:%d, ver mismatch, expected: %" PRId64 ", received: %" PRId64, TD_VID(pVnode),
×
810
           pVnode->config.mountVgId, pVnode->state.applied + 1, ver);
811
    return terrno = TSDB_CODE_INTERNAL_ERROR;
×
812
  }
813

814
  atomic_store_64(&pVnode->state.applied, ver);
25,444,568✔
815
  atomic_store_64(&pVnode->state.applyTerm, pMsg->info.conn.applyTerm);
25,444,923✔
816

817
  if (!syncUtilUserCommit(pMsg->msgType)) goto _exit;
25,444,911✔
818

819
  // skip header
820
  pReq = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
25,415,845✔
821
  len = pMsg->contLen - sizeof(SMsgHead);
25,415,845✔
822
  bool needCommit = false;
25,415,845✔
823

824
  switch (pMsg->msgType) {
25,415,845!
825
    /* META */
826
    case TDMT_VND_CREATE_STB:
34,002✔
827
      code = vnodeProcessCreateStbReq(pVnode, ver, pReq, len, pRsp);
34,002✔
828
      TSDB_CHECK_CODE(code, lino, _err);
33,996✔
829
      break;
33,993✔
830
    case TDMT_VND_ALTER_STB:
16,350✔
831
      code = vnodeProcessAlterStbReq(pVnode, ver, pReq, len, pRsp);
16,350✔
832
      TSDB_CHECK_CODE(code, lino, _err);
16,350!
833
      break;
16,350✔
834
    case TDMT_VND_DROP_STB:
5,104✔
835
      code = vnodeProcessDropStbReq(pVnode, ver, pReq, len, pRsp);
5,104✔
836
      TSDB_CHECK_CODE(code, lino, _err);
5,110!
837
      break;
5,110✔
838
    case TDMT_VND_CREATE_TABLE:
102,813✔
839
      code = vnodeProcessCreateTbReq(pVnode, ver, pReq, len, pRsp, pMsg);
102,813✔
840
      TSDB_CHECK_CODE(code, lino, _err);
102,824!
841
      break;
102,824✔
842
    case TDMT_VND_ALTER_TABLE:
19,767✔
843
      code = vnodeProcessAlterTbReq(pVnode, ver, pReq, len, pRsp);
19,767✔
844
      TSDB_CHECK_CODE(code, lino, _err);
19,767!
845
      break;
19,767✔
846
    case TDMT_VND_DROP_TABLE:
15,724✔
847
      code = vnodeProcessDropTbReq(pVnode, ver, pReq, len, pRsp, pMsg);
15,724✔
848
      TSDB_CHECK_CODE(code, lino, _err);
15,724!
849
      break;
15,724✔
850
    case TDMT_VND_DROP_TTL_TABLE:
×
851
      code = vnodeProcessDropTtlTbReq(pVnode, ver, pReq, len, pRsp);
×
852
      TSDB_CHECK_CODE(code, lino, _err);
×
853
      break;
×
854
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
31✔
855
      code = vnodeProcessFetchTtlExpiredTbs(pVnode, ver, pReq, len, pRsp);
31✔
856
      TSDB_CHECK_CODE(code, lino, _err);
31!
857
      break;
31✔
858
    case TDMT_VND_TRIM:
389✔
859
      code = vnodeProcessTrimReq(pVnode, ver, pReq, len, pRsp);
389✔
860
      TSDB_CHECK_CODE(code, lino, _err);
389!
861
      break;
389✔
862
#ifdef TD_ENTERPRISE
863
    case TDMT_VND_SSMIGRATE_FILESET:
×
864
      if (vnodeProcessSsMigrateFileSetReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
865
      break;
×
866
    case TDMT_VND_FOLLOWER_SSMIGRATE:
×
867
      if (vnodeProcessFollowerSsMigrateReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
868
      break;
×
869
    case TDMT_VND_KILL_SSMIGRATE:
×
870
      //if (vnodeProcessKillSsMigrateReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
871
      break;
×
872
#endif
873
    /* TSDB */
874
    case TDMT_VND_SUBMIT: {
25,005,075✔
875
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.apply_time, METRIC_LEVEL_LOW, {
25,005,075!
876
        if (vnodeProcessSubmitReq(pVnode, ver, pMsg->pCont, pMsg->contLen, pRsp, pMsg) < 0) goto _err;
877
      });
878
      METRICS_UPDATE(pVnode->writeMetrics.apply_bytes, METRIC_LEVEL_LOW, (int64_t)pMsg->contLen);
25,005,069!
879
      break;
25,005,044✔
880
    }
881
    case TDMT_VND_DELETE:
143,701✔
882
      code = vnodeProcessDeleteReq(pVnode, ver, pReq, len, pRsp, pMsg);
143,701✔
883
      TSDB_CHECK_CODE(code, lino, _err);
143,697!
884
      break;
143,697✔
885
    case TDMT_VND_BATCH_DEL:
×
886
      code = vnodeProcessBatchDeleteReq(pVnode, ver, pReq, len, pRsp);
×
887
      TSDB_CHECK_CODE(code, lino, _err);
×
888
      break;
×
889
      /* TQ */
890
#if defined(USE_TQ) || defined(USE_STREAM)
891
    case TDMT_VND_TMQ_SUBSCRIBE:
2,659✔
892
      code = tqProcessSubscribeReq(pVnode->pTq, ver, pReq, len);
2,659✔
893
      TSDB_CHECK_CODE(code, lino, _err);
2,658!
894
      break;
2,658✔
895
    case TDMT_VND_TMQ_DELETE_SUB:
869✔
896
      code = tqProcessDeleteSubReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen);
869✔
897
      TSDB_CHECK_CODE(code, lino, _err);
869!
898
      break;
869✔
899
    case TDMT_VND_TMQ_COMMIT_OFFSET:
10,858✔
900
      code = tqProcessOffsetCommitReq(pVnode->pTq, ver, pReq, len);
10,858✔
901
      TSDB_CHECK_CODE(code, lino, _err);
10,858!
902
      break;
10,858✔
903
    case TDMT_VND_TMQ_ADD_CHECKINFO:
48✔
904
      code = tqProcessAddCheckInfoReq(pVnode->pTq, ver, pReq, len);
48✔
905
      TSDB_CHECK_CODE(code, lino, _err);
48!
906
      break;
48✔
907
    case TDMT_VND_TMQ_DEL_CHECKINFO:
10✔
908
      code = tqProcessDelCheckInfoReq(pVnode->pTq, ver, pReq, len);
10✔
909
      TSDB_CHECK_CODE(code, lino, _err);
10!
910
      break;
10✔
911
#endif
912
    case TDMT_VND_ALTER_CONFIRM:
8,436✔
913
      needCommit = pVnode->config.hashChange;
8,436✔
914
      code = vnodeProcessAlterConfirmReq(pVnode, ver, pReq, len, pRsp);
8,436✔
915
      TSDB_CHECK_CODE(code, lino, _err);
8,436!
916
      break;
8,436✔
917
    case TDMT_VND_ALTER_CONFIG:
1,369✔
918
      vnodeProcessAlterConfigReq(pVnode, ver, pReq, len, pRsp);
1,369✔
919
      break;
1,371✔
920
    case TDMT_VND_COMMIT:
39,220✔
921
      needCommit = true;
39,220✔
922
      break;
39,220✔
923
    case TDMT_VND_CREATE_INDEX:
2,839✔
924
      vnodeProcessCreateIndexReq(pVnode, ver, pReq, len, pRsp);
2,839✔
925
      break;
2,839✔
926
    case TDMT_VND_DROP_INDEX:
6,398✔
927
      vnodeProcessDropIndexReq(pVnode, ver, pReq, len, pRsp);
6,398✔
928
      break;
6,398✔
929
    case TDMT_VND_COMPACT:
160✔
930
      vnodeProcessCompactVnodeReq(pVnode, ver, pReq, len, pRsp);
160✔
931
      goto _exit;
160✔
932
    case TDMT_SYNC_CONFIG_CHANGE:
×
933
      vnodeProcessConfigChangeReq(pVnode, ver, pReq, len, pRsp);
×
934
      break;
×
935
#ifdef TD_ENTERPRISE
936
    case TDMT_VND_KILL_COMPACT:
2✔
937
      vnodeProcessKillCompactReq(pVnode, ver, pReq, len, pRsp);
2✔
938
      break;
2✔
939
#endif
940
    /* ARB */
941
    case TDMT_VND_ARB_CHECK_SYNC:
21✔
942
      vnodeProcessArbCheckSyncReq(pVnode, pReq, len, pRsp);
21✔
943
      break;
21✔
944
    default:
×
945
      vError("vgId:%d, unprocessed msg, %d", TD_VID(pVnode), pMsg->msgType);
×
946
      return TSDB_CODE_INVALID_MSG;
×
947
  }
948

949
  vGDebug(&pMsg->info.traceId, "vgId:%d, index:%" PRId64 ", msg processed, code:0x%x", TD_VID(pVnode), ver, pRsp->code);
25,415,659!
950

951
  walApplyVer(pVnode->pWal, ver);
25,415,690✔
952

953
  if (pVnode->pTq) {
25,415,572!
954
    code = tqPushMsg(pVnode->pTq, pMsg->msgType);
25,415,694✔
955
    if (code) {
25,415,776!
956
      vError("vgId:%d, failed to push msg to TQ since %s", TD_VID(pVnode), tstrerror(terrno));
×
957
      return code;
×
958
    }
959
  }
960

961
  // commit if need
962
  if (needCommit) {
25,415,721✔
963
    vInfo("vgId:%d, commit at version %" PRId64, TD_VID(pVnode), ver);
39,311!
964
    code = vnodeAsyncCommit(pVnode);
39,338✔
965
    if (code) {
39,343!
966
      vError("vgId:%d, failed to vnode async commit since %s.", TD_VID(pVnode), tstrerror(terrno));
×
967
      goto _err;
×
968
    }
969

970
    // start a new one
971
    METRICS_TIMING_BLOCK(pVnode->writeMetrics.memtable_wait_time, METRIC_LEVEL_LOW, {
39,343!
972
      code = vnodeBegin(pVnode);
973
      if (code) {
974
        vError("vgId:%d, failed to begin vnode since %s.", TD_VID(pVnode), tstrerror(terrno));
975
        goto _err;
976
      }
977
    });
978
  }
979

980
_exit:
25,415,748✔
981
  return 0;
25,444,360✔
982

983
_err:
148✔
984
  if (code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
148✔
985
    vInfo("vgId:%d, process %s request failed since %s, lino 1:%d ver:%" PRId64, TD_VID(pVnode),
3!
986
          TMSG_INFO(pMsg->msgType), tstrerror(code), lino, ver);
987
  } else if (code) {
145!
988
    vError("vgId:%d, process %s request failed since %s, lino 2:%d ver:%" PRId64, TD_VID(pVnode),
×
989
           TMSG_INFO(pMsg->msgType), tstrerror(code), lino, ver);
990
  }
991

992
  return code;
148✔
993
}
994

995
int32_t vnodePreprocessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg) {
20,950,437✔
996
  if (TDMT_SCH_QUERY != pMsg->msgType && TDMT_SCH_MERGE_QUERY != pMsg->msgType) {
20,950,437✔
997
    return 0;
1,019,700✔
998
  }
999

1000
  return qWorkerPreprocessQueryMsg(pVnode->pQuery, pMsg, TDMT_SCH_QUERY == pMsg->msgType);
19,930,737✔
1001
}
1002

1003
int32_t vnodeProcessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
20,967,758✔
1004
  vTrace("message in vnode query queue is processing");
20,967,758!
1005
  if (pMsg->msgType == TDMT_VND_TMQ_CONSUME && !syncIsReadyForRead(pVnode->sync)) {
20,967,758✔
1006
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
1,203✔
1007
    return 0;
1,203✔
1008
  }
1009

1010
  if (pMsg->msgType == TDMT_VND_TMQ_CONSUME && !pVnode->restored) {
20,966,548!
1011
    vnodeRedirectRpcMsg(pVnode, pMsg, TSDB_CODE_SYN_RESTORING);
×
1012
    return 0;
×
1013
  }
1014

1015
  SReadHandle handle = {0};
20,966,548✔
1016
  handle.vnode = pVnode;
20,966,548✔
1017
  handle.pMsgCb = &pVnode->msgCb;
20,966,548✔
1018
  handle.pWorkerCb = pInfo->workerCb;
20,966,548✔
1019
  initStorageAPI(&handle.api);
20,966,548✔
1020
  int32_t code = TSDB_CODE_SUCCESS;
20,963,879✔
1021
  bool    redirected = false;
20,963,879✔
1022

1023
  switch (pMsg->msgType) {
20,963,879!
1024
    case TDMT_SCH_QUERY:
14,943,445✔
1025
      if (!syncIsReadyForRead(pVnode->sync)) {
14,943,445✔
1026
        pMsg->code = (terrno) ? terrno : TSDB_CODE_SYN_NOT_LEADER;
150!
1027
        redirected = true;
150✔
1028
      }
1029
      code = qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
14,939,543✔
1030
      if (redirected) {
14,939,396✔
1031
        vnodeRedirectRpcMsg(pVnode, pMsg, pMsg->code);
147✔
1032
        return 0;
150✔
1033
      }
1034

1035
      return code;
14,939,249✔
1036
    case TDMT_SCH_MERGE_QUERY:
5,003,369✔
1037
      return qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
5,003,369✔
1038
    case TDMT_SCH_QUERY_CONTINUE:
965,308✔
1039
      return qWorkerProcessCQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
965,308✔
1040
    case TDMT_VND_TMQ_CONSUME:
42,149✔
1041
      return tqProcessPollReq(pVnode->pTq, pMsg);
42,149✔
1042
    case TDMT_VND_TMQ_CONSUME_PUSH:
11,033✔
1043
      return tqProcessPollPush(pVnode->pTq);
11,033✔
1044
    default:
×
1045
      vError("unknown msg type:%d in query queue", pMsg->msgType);
×
1046
      return TSDB_CODE_APP_ERROR;
×
1047
  }
1048
}
1049

1050
int32_t vnodeProcessFetchMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
54,123,144✔
1051
  int32_t code = TSDB_CODE_SUCCESS;
54,123,144✔
1052
  vTrace("vgId:%d, msg:%p in fetch queue is processing", pVnode->config.vgId, pMsg);
54,123,144!
1053
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
54,123,144✔
1054
       pMsg->msgType == TDMT_VND_BATCH_META || pMsg->msgType == TDMT_VND_TABLE_NAME ||
38,106,697!
1055
       pMsg->msgType == TDMT_VND_VSUBTABLES_META || pMsg->msgType == TDMT_VND_VSTB_REF_DBS) &&
34,744,512✔
1056
      !syncIsReadyForRead(pVnode->sync)) {
19,390,654✔
1057
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
11,861✔
1058
    return 0;
11,861✔
1059
  }
1060

1061
  switch (pMsg->msgType) {
54,128,469!
1062
    case TDMT_SCH_FETCH:
26,117,044✔
1063
    case TDMT_SCH_MERGE_FETCH:
1064
      return qWorkerProcessFetchMsg(pVnode, pVnode->pQuery, pMsg, 0);
26,117,044✔
1065
    case TDMT_SCH_FETCH_RSP:
×
1066
      return qWorkerProcessRspMsg(pVnode, pVnode->pQuery, pMsg, 0);
×
1067
    // case TDMT_SCH_CANCEL_TASK:
1068
    //   return qWorkerProcessCancelMsg(pVnode, pVnode->pQuery, pMsg, 0);
1069
    case TDMT_SCH_DROP_TASK:
19,909,750✔
1070
      return qWorkerProcessDropMsg(pVnode, pVnode->pQuery, pMsg, 0);
19,909,750✔
1071
    case TDMT_SCH_TASK_NOTIFY:
126✔
1072
      return qWorkerProcessNotifyMsg(pVnode, pVnode->pQuery, pMsg, 0);
126✔
1073
    case TDMT_SCH_QUERY_HEARTBEAT:
4,743,099✔
1074
      return qWorkerProcessHbMsg(pVnode, pVnode->pQuery, pMsg, 0);
4,743,099✔
1075
    case TDMT_VND_TABLE_META:
6,415✔
1076
    case TDMT_VND_TABLE_NAME:
1077
      return vnodeGetTableMeta(pVnode, pMsg, true);
6,415✔
1078
    case TDMT_VND_TABLE_CFG:
×
1079
      return vnodeGetTableCfg(pVnode, pMsg, true);
×
1080
    case TDMT_VND_BATCH_META: {
3,351,812✔
1081
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.fetch_batch_meta_time, METRIC_LEVEL_LOW,
3,351,812!
1082
                           { code = vnodeGetBatchMeta(pVnode, pMsg); });
1083
      METRICS_UPDATE(pVnode->writeMetrics.fetch_batch_meta_count, METRIC_LEVEL_LOW, 1);
3,354,173!
1084
      return code;
3,354,206✔
1085
    }
1086
    case TDMT_VND_VSUBTABLES_META:
×
1087
      return vnodeGetVSubtablesMeta(pVnode, pMsg);
×
1088
    case TDMT_VND_VSTB_REF_DBS:
×
1089
      return vnodeGetVStbRefDbs(pVnode, pMsg);
×
1090
#ifdef TD_ENTERPRISE
1091
    case TDMT_VND_QUERY_COMPACT_PROGRESS:
209✔
1092
      return vnodeQueryCompactProgress(pVnode, pMsg);
209✔
1093

1094
    case TDMT_VND_LIST_SSMIGRATE_FILESETS:
×
1095
      return vnodeListSsMigrateFileSets(pVnode, pMsg);
×
1096

1097
    case TDMT_VND_QUERY_SSMIGRATE_PROGRESS:
×
1098
      return vnodeQuerySsMigrateProgress(pVnode, pMsg);
×
1099
#endif
1100
      //    case TDMT_VND_TMQ_CONSUME:
1101
      //      return tqProcessPollReq(pVnode->pTq, pMsg);
1102
#ifdef USE_TQ
1103
    case TDMT_VND_TMQ_VG_WALINFO:
3✔
1104
      return tqProcessVgWalInfoReq(pVnode->pTq, pMsg);
3✔
1105
    case TDMT_VND_TMQ_VG_COMMITTEDINFO:
3✔
1106
      return tqProcessVgCommittedInfoReq(pVnode->pTq, pMsg);
3✔
1107
    case TDMT_VND_TMQ_SEEK:
8✔
1108
      return tqProcessSeekReq(pVnode->pTq, pMsg);
8✔
1109
#endif
1110
    default:
×
1111
      vError("unknown msg type:%d in fetch queue", pMsg->msgType);
×
1112
      return TSDB_CODE_APP_ERROR;
×
1113
  }
1114
}
1115

1116
void smaHandleRes(void *pVnode, int64_t smaId, const SArray *data) {
×
1117
  int32_t code = tdProcessTSmaInsert(((SVnode *)pVnode)->pSma, smaId, (const char *)data);
×
1118
  if (code) {
×
1119
    vError("failed to process sma result since %s", tstrerror(code));
×
1120
  }
1121
}
×
1122

1123
void vnodeUpdateMetaRsp(SVnode *pVnode, STableMetaRsp *pMetaRsp) {
222,871✔
1124
  if (NULL == pMetaRsp) {
222,871!
1125
    return;
×
1126
  }
1127

1128
  tstrncpy(pMetaRsp->dbFName, pVnode->config.dbname, TSDB_DB_FNAME_LEN);
222,871✔
1129
  pMetaRsp->dbId = pVnode->config.dbId;
222,871✔
1130
  pMetaRsp->vgId = TD_VID(pVnode);
222,871✔
1131
  pMetaRsp->precision = pVnode->config.tsdbCfg.precision;
222,871✔
1132
}
1133

1134
extern int32_t vnodeAsyncRetention(SVnode *pVnode, int64_t now);
1135

1136
static int32_t vnodeProcessTrimReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
389✔
1137
  if (!pVnode->restored) {
389✔
1138
    vInfo("vgId:%d, ignore trim req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
28!
1139
    return 0;
28✔
1140
  }
1141

1142
  int32_t     code = 0;
361✔
1143
  SVTrimDbReq trimReq = {0};
361✔
1144

1145
  // decode
1146
  if (tDeserializeSVTrimDbReq(pReq, len, &trimReq) != 0) {
361!
1147
    code = TSDB_CODE_INVALID_MSG;
×
1148
    goto _exit;
×
1149
  }
1150

1151
  vInfo("vgId:%d, process trim vnode request, time:%d", pVnode->config.vgId, trimReq.timestamp);
361!
1152

1153
  code = vnodeAsyncRetention(pVnode, trimReq.timestamp);
361✔
1154

1155
_exit:
361✔
1156
  return code;
361✔
1157
}
1158

1159
extern int32_t vnodeAsyncSsMigrateFileSet(SVnode *pVnode, SSsMigrateFileSetReq *pReq);
1160

1161
static int32_t vnodeProcessSsMigrateFileSetReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1162
  int32_t          code = 0;
×
1163

1164
  SSsMigrateFileSetReq req = {0};
×
1165
  SSsMigrateFileSetRsp rsp = {0};
×
1166
  pRsp->msgType = TDMT_VND_SSMIGRATE_FILESET_RSP;
×
1167
  pRsp->code = 0;
×
1168
  pRsp->pCont = NULL;
×
1169
  pRsp->contLen = 0;
×
1170

1171
  if (tDeserializeSSsMigrateFileSetReq(pReq, len, &req) != 0) {
×
1172
    code = TSDB_CODE_INVALID_MSG;
×
1173
    goto _exit;
×
1174
  }
1175

1176
  vInfo("vgId:%d, ssmigrate:%d, fid:%d, process ssmigrate fileset request, time:%" PRId64, pVnode->config.vgId, req.ssMigrateId, req.fid, req.startTimeSec);
×
1177
  rsp.ssMigrateId = req.ssMigrateId;
×
1178
  rsp.vgId = TD_VID(pVnode);
×
1179
  rsp.nodeId = req.nodeId;
×
1180
  rsp.fid = req.fid;
×
1181

1182
  code = vnodeAsyncSsMigrateFileSet(pVnode, &req);
×
1183
  if (code != TSDB_CODE_SUCCESS) {
×
1184
    vError("vgId:%d, failed to async ssmigrate since %s", TD_VID(pVnode), tstrerror(code));
×
1185
    pRsp->code = code;
×
1186
    goto _exit;
×
1187
  }
1188

1189
  pRsp->code = TSDB_CODE_SUCCESS;
×
1190
  pRsp->contLen = tSerializeSSsMigrateFileSetRsp(NULL, 0, &rsp);
×
1191
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
×
1192
  if (pRsp->pCont == NULL) {
×
1193
    vError("vgId:%d, failed to allocate memory for ssmigrate fileset response", TD_VID(pVnode));
×
1194
    code = TSDB_CODE_OUT_OF_MEMORY;
×
1195
    goto _exit;
×
1196
  }
1197
  TAOS_UNUSED(tSerializeSSsMigrateFileSetRsp(pRsp->pCont, pRsp->contLen, &rsp));
×
1198

1199
_exit:
×
1200
  if (code != TSDB_CODE_SUCCESS) {
×
1201
    pRsp->code = code;
×
1202
  }
1203
  return code;
×
1204
}
1205

1206
extern int32_t vnodeFollowerSsMigrate(SVnode *pVnode, SSsMigrateProgress *pReq);
1207

1208
static int32_t vnodeProcessFollowerSsMigrateReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1209
  int32_t          code = 0;
×
1210
  SSsMigrateProgress req = {0};
×
1211

1212
  // decode
1213
  if (tDeserializeSSsMigrateProgress(pReq, len, &req) != 0) {
×
1214
    code = TSDB_CODE_INVALID_MSG;
×
1215
    goto _exit;
×
1216
  }
1217

1218
  code = vnodeFollowerSsMigrate(pVnode, &req);
×
1219

1220
_exit:
×
1221
  pRsp->code = code;
×
1222
  return code;
×
1223
}
1224

1225
static int32_t vnodeProcessDropTtlTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1226
  int               ret = 0;
×
1227
  SVDropTtlTableReq ttlReq = {0};
×
1228
  if (tDeserializeSVDropTtlTableReq(pReq, len, &ttlReq) != 0) {
×
1229
    ret = TSDB_CODE_INVALID_MSG;
×
1230
    goto end;
×
1231
  }
1232

1233
  if (ttlReq.nUids != taosArrayGetSize(ttlReq.pTbUids)) {
×
1234
    ret = TSDB_CODE_INVALID_MSG;
×
1235
    goto end;
×
1236
  }
1237

1238
  if (ttlReq.nUids != 0) {
×
1239
    vInfo("vgId:%d, process drop ttl table request, time:%d, ntbUids:%d", pVnode->config.vgId, ttlReq.timestampSec,
×
1240
          ttlReq.nUids);
1241
  }
1242

1243
  if (ttlReq.nUids > 0) {
×
1244
    int32_t code = metaDropMultipleTables(pVnode->pMeta, ver, ttlReq.pTbUids);
×
1245
    if (code) return code;
×
1246

1247
    code = tqUpdateTbUidList(pVnode->pTq, ttlReq.pTbUids, false);
×
1248
    if (code) {
×
1249
      vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1250
    }
1251
  }
1252

1253
end:
×
1254
  taosArrayDestroy(ttlReq.pTbUids);
×
1255
  return ret;
×
1256
}
1257

1258
static int32_t vnodeProcessFetchTtlExpiredTbs(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
31✔
1259
  int32_t                 code = -1;
31✔
1260
  SMetaReader             mr = {0};
31✔
1261
  SVDropTtlTableReq       ttlReq = {0};
31✔
1262
  SVFetchTtlExpiredTbsRsp rsp = {0};
31✔
1263
  SEncoder                encoder = {0};
31✔
1264
  SArray                 *pNames = NULL;
31✔
1265
  pRsp->msgType = TDMT_VND_FETCH_TTL_EXPIRED_TBS_RSP;
31✔
1266
  pRsp->code = TSDB_CODE_SUCCESS;
31✔
1267
  pRsp->pCont = NULL;
31✔
1268
  pRsp->contLen = 0;
31✔
1269

1270
  if (tDeserializeSVDropTtlTableReq(pReq, len, &ttlReq) != 0) {
31!
1271
    terrno = TSDB_CODE_INVALID_MSG;
×
1272
    goto _end;
×
1273
  }
1274

1275
  if (!(ttlReq.nUids == taosArrayGetSize(ttlReq.pTbUids))) {
31!
1276
    terrno = TSDB_CODE_INVALID_MSG;
×
1277
    goto _end;
×
1278
  }
1279

1280
  tb_uid_t    suid;
1281
  char        ctbName[TSDB_TABLE_NAME_LEN];
1282
  SVDropTbReq expiredTb = {.igNotExists = true};
31✔
1283
  metaReaderDoInit(&mr, pVnode->pMeta, 0);
31✔
1284
  rsp.vgId = TD_VID(pVnode);
31✔
1285
  rsp.pExpiredTbs = taosArrayInit(ttlReq.nUids, sizeof(SVDropTbReq));
31✔
1286
  if (!rsp.pExpiredTbs) goto _end;
31!
1287

1288
  pNames = taosArrayInit(ttlReq.nUids, TSDB_TABLE_NAME_LEN);
31✔
1289
  if (!pNames) {
31!
1290
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1291
    goto _end;
×
1292
  }
1293
  char buf[TSDB_TABLE_NAME_LEN];
1294
  for (int32_t i = 0; i < ttlReq.nUids; ++i) {
156✔
1295
    tb_uid_t *uid = taosArrayGet(ttlReq.pTbUids, i);
125✔
1296
    expiredTb.suid = *uid;
125✔
1297
    terrno = metaReaderGetTableEntryByUid(&mr, *uid);
125✔
1298
    if (terrno < 0) goto _end;
124!
1299
    tstrncpy(buf, mr.me.name, TSDB_TABLE_NAME_LEN);
124✔
1300
    void *p = taosArrayPush(pNames, buf);
125✔
1301
    if (p == NULL) {
125!
1302
      goto _end;
×
1303
    }
1304

1305
    expiredTb.name = p;
125✔
1306
    if (mr.me.type == TSDB_CHILD_TABLE) {
125✔
1307
      expiredTb.suid = mr.me.ctbEntry.suid;
76✔
1308
    }
1309

1310
    if (taosArrayPush(rsp.pExpiredTbs, &expiredTb) == NULL) {
250!
1311
      goto _end;
×
1312
    }
1313
  }
1314

1315
  int32_t ret = 0;
31✔
1316
  tEncodeSize(tEncodeVFetchTtlExpiredTbsRsp, &rsp, pRsp->contLen, ret);
31!
1317
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
31✔
1318
  if (pRsp->pCont == NULL) {
31!
1319
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1320
    code = -1;
×
1321
    goto _end;
×
1322
  }
1323
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
31✔
1324
  terrno = tEncodeVFetchTtlExpiredTbsRsp(&encoder, &rsp);
31✔
1325
  tEncoderClear(&encoder);
31✔
1326

1327
  if (terrno == 0) code = 0;
31!
1328
_end:
×
1329
  metaReaderClear(&mr);
31✔
1330
  tFreeFetchTtlExpiredTbsRsp(&rsp);
31✔
1331
  taosArrayDestroy(ttlReq.pTbUids);
31✔
1332
  if (pNames) taosArrayDestroy(pNames);
31!
1333
  pRsp->code = terrno;
31✔
1334
  return code;
31✔
1335
}
1336

1337
static int32_t vnodeProcessCreateStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
34,000✔
1338
  int32_t        code = 0;
34,000✔
1339
  SVCreateStbReq req = {0};
34,000✔
1340
  SDecoder       coder;
1341

1342
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
34,000✔
1343
  pRsp->code = TSDB_CODE_SUCCESS;
34,000✔
1344
  pRsp->pCont = NULL;
34,000✔
1345
  pRsp->contLen = 0;
34,000✔
1346

1347
  // decode and process req
1348
  tDecoderInit(&coder, pReq, len);
34,000✔
1349

1350
  code = tDecodeSVCreateStbReq(&coder, &req);
33,996✔
1351
  if (code) {
33,993!
1352
    pRsp->code = code;
×
1353
    goto _err;
×
1354
  }
1355

1356
  code = metaCreateSuperTable(pVnode->pMeta, ver, &req);
33,993✔
1357
  if (code) {
34,033✔
1358
    pRsp->code = code;
3✔
1359
    goto _err;
3✔
1360
  }
1361

1362
  if ((code = tdProcessRSmaCreate(pVnode->pSma, &req)) < 0) {
34,030!
1363
    pRsp->code = code;
×
1364
    goto _err;
×
1365
  }
1366

1367
  tDecoderClear(&coder);
34,013✔
1368
  return 0;
33,981✔
1369

1370
_err:
3✔
1371
  tDecoderClear(&coder);
3✔
1372
  return code;
3✔
1373
}
1374

1375
static int32_t vnodeProcessCreateTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
102,813✔
1376
                                       SRpcMsg *pOriginRpc) {
1377
  SDecoder           decoder = {0};
102,813✔
1378
  SEncoder           encoder = {0};
102,813✔
1379
  int32_t            rcode = 0;
102,813✔
1380
  SVCreateTbBatchReq req = {0};
102,813✔
1381
  SVCreateTbReq     *pCreateReq;
1382
  SVCreateTbBatchRsp rsp = {0};
102,813✔
1383
  SVCreateTbRsp      cRsp = {0};
102,813✔
1384
  char               tbName[TSDB_TABLE_FNAME_LEN];
1385
  STbUidStore       *pStore = NULL;
102,813✔
1386
  SArray            *tbUids = NULL;
102,813✔
1387
  SArray            *tbNames = NULL;
102,813✔
1388
  pRsp->msgType = TDMT_VND_CREATE_TABLE_RSP;
102,813✔
1389
  pRsp->code = TSDB_CODE_SUCCESS;
102,813✔
1390
  pRsp->pCont = NULL;
102,813✔
1391
  pRsp->contLen = 0;
102,813✔
1392

1393
  // decode
1394
  tDecoderInit(&decoder, pReq, len);
102,813✔
1395
  if (tDecodeSVCreateTbBatchReq(&decoder, &req) < 0) {
102,812!
1396
    rcode = -1;
×
1397
    terrno = TSDB_CODE_INVALID_MSG;
×
1398
    goto _exit;
×
1399
  }
1400

1401
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(cRsp));
102,809✔
1402
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
102,830✔
1403
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
102,828✔
1404
  if (rsp.pArray == NULL || tbUids == NULL || tbNames == NULL) {
102,829!
1405
    rcode = -1;
2✔
1406
    terrno = TSDB_CODE_OUT_OF_MEMORY;
2✔
1407
    goto _exit;
×
1408
  }
1409

1410
  // loop to create table
1411
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
272,131✔
1412
    pCreateReq = req.pReqs + iReq;
169,295✔
1413
    memset(&cRsp, 0, sizeof(cRsp));
169,295✔
1414

1415
    if (tsEnableAudit && tsEnableAuditCreateTable) {
169,295!
1416
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
169,295!
1417
      if (str == NULL) {
169,294!
1418
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1419
        rcode = -1;
×
1420
        goto _exit;
×
1421
      }
1422
      tstrncpy(str, pCreateReq->name, TSDB_TABLE_FNAME_LEN);
169,294✔
1423
      if (taosArrayPush(tbNames, &str) == NULL) {
169,296!
1424
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1425
        rcode = -1;
×
1426
        goto _exit;
×
1427
      }
1428
    }
1429

1430
    // validate hash
1431
    (void)tsnprintf(tbName, TSDB_TABLE_FNAME_LEN, "%s.%s", pVnode->config.dbname, pCreateReq->name);
169,296✔
1432
    if (vnodeValidateTableHash(pVnode, tbName) < 0) {
169,304!
1433
      cRsp.code = TSDB_CODE_VND_HASH_MISMATCH;
×
1434
      if (taosArrayPush(rsp.pArray, &cRsp) == NULL) {
×
1435
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1436
        rcode = -1;
×
1437
        goto _exit;
×
1438
      }
1439
      vError("vgId:%d create-table:%s failed due to hash value mismatch", TD_VID(pVnode), tbName);
×
1440
      continue;
×
1441
    }
1442

1443
    // do create table
1444
    if (metaCreateTable2(pVnode->pMeta, ver, pCreateReq, &cRsp.pMeta) < 0) {
169,282✔
1445
      if (pCreateReq->flags & TD_CREATE_IF_NOT_EXISTS && terrno == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
125!
1446
        cRsp.code = TSDB_CODE_SUCCESS;
109✔
1447
      } else {
1448
        cRsp.code = terrno;
16✔
1449
      }
1450
    } else {
1451
      cRsp.code = TSDB_CODE_SUCCESS;
169,184✔
1452
      if (tdFetchTbUidList(pVnode->pSma, &pStore, pCreateReq->ctb.suid, pCreateReq->uid) < 0) {
169,184!
1453
        vError("vgId:%d, failed to fetch tbUid list", TD_VID(pVnode));
×
1454
      }
1455
      if (taosArrayPush(tbUids, &pCreateReq->uid) == NULL) {
338,369!
1456
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1457
        rcode = -1;
×
1458
        goto _exit;
×
1459
      }
1460
      vnodeUpdateMetaRsp(pVnode, cRsp.pMeta);
169,184✔
1461
    }
1462

1463
    if (taosArrayPush(rsp.pArray, &cRsp) == NULL) {
338,608!
1464
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1465
      rcode = -1;
×
1466
      goto _exit;
×
1467
    }
1468
  }
1469

1470
  vTrace("vgId:%d, add %d new created tables into query table list", TD_VID(pVnode), (int32_t)taosArrayGetSize(tbUids));
102,836✔
1471
  if (tqUpdateTbUidList(pVnode->pTq, tbUids, true) < 0) {
102,836!
1472
    vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(terrno));
×
1473
  }
1474
  if (tdUpdateTbUidList(pVnode->pSma, pStore, true) < 0) {
102,833!
1475
    goto _exit;
×
1476
  }
1477
  pStore = tdUidStoreFree(pStore);
102,833✔
1478

1479
  // prepare rsp
1480
  int32_t ret = 0;
102,832✔
1481
  tEncodeSize(tEncodeSVCreateTbBatchRsp, &rsp, pRsp->contLen, ret);
102,832!
1482
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
102,830✔
1483
  if (pRsp->pCont == NULL) {
102,832!
1484
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1485
    rcode = -1;
×
1486
    goto _exit;
×
1487
  }
1488
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
102,832✔
1489
  if (tEncodeSVCreateTbBatchRsp(&encoder, &rsp) != 0) {
102,832!
1490
    vError("vgId:%d, failed to encode create table batch response", TD_VID(pVnode));
×
1491
  }
1492

1493
  if (tsEnableAudit && tsEnableAuditCreateTable) {
102,827!
1494
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
102,827✔
1495

1496
    SName name = {0};
102,827✔
1497
    if (tNameFromString(&name, pVnode->config.dbname, T_NAME_ACCT | T_NAME_DB) < 0) {
102,827!
1498
      vError("vgId:%d, failed to get name from string", TD_VID(pVnode));
×
1499
    }
1500

1501
    SStringBuilder sb = {0};
102,829✔
1502
    for (int32_t i = 0; i < tbNames->size; i++) {
272,098✔
1503
      char **key = (char **)taosArrayGet(tbNames, i);
169,275✔
1504
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
169,273✔
1505
      if (i < tbNames->size - 1) {
169,278✔
1506
        taosStringBuilderAppendChar(&sb, ',');
66,456✔
1507
      }
1508
      // taosMemoryFreeClear(*key);
1509
    }
1510

1511
    size_t len = 0;
102,823✔
1512
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
102,823✔
1513

1514
    if (pOriginRpc->info.conn.user != NULL && strlen(pOriginRpc->info.conn.user) > 0) {
102,825!
1515
      auditAddRecord(pOriginRpc, clusterId, "createTable", name.dbname, "", keyJoined, len);
94,162✔
1516
    }
1517

1518
    taosStringBuilderDestroy(&sb);
102,825✔
1519
  }
1520

1521
_exit:
×
1522
  tDeleteSVCreateTbBatchReq(&req);
102,824✔
1523
  taosArrayDestroyEx(rsp.pArray, tFreeSVCreateTbRsp);
102,827✔
1524
  taosArrayDestroy(tbUids);
102,831✔
1525
  tDecoderClear(&decoder);
102,831✔
1526
  tEncoderClear(&encoder);
102,831✔
1527
  taosArrayDestroyP(tbNames, NULL);
102,831✔
1528
  return rcode;
102,829✔
1529
}
1530

1531
static int32_t vnodeProcessAlterStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
16,350✔
1532
  int32_t        code = 0;
16,350✔
1533
  SVCreateStbReq req = {0};
16,350✔
1534
  SDecoder       dc = {0};
16,350✔
1535

1536
  pRsp->msgType = TDMT_VND_ALTER_STB_RSP;
16,350✔
1537
  pRsp->code = TSDB_CODE_SUCCESS;
16,350✔
1538
  pRsp->pCont = NULL;
16,350✔
1539
  pRsp->contLen = 0;
16,350✔
1540

1541
  tDecoderInit(&dc, pReq, len);
16,350✔
1542

1543
  // decode req
1544
  code = tDecodeSVCreateStbReq(&dc, &req);
16,350✔
1545
  if (code) {
16,348!
1546
    tDecoderClear(&dc);
×
1547
    return code;
×
1548
  }
1549

1550
  code = metaAlterSuperTable(pVnode->pMeta, ver, &req);
16,348✔
1551
  if (code) {
16,350!
1552
    pRsp->code = code;
×
1553
    tDecoderClear(&dc);
×
1554
    return code;
×
1555
  }
1556

1557
  tDecoderClear(&dc);
16,350✔
1558

1559
  return 0;
16,350✔
1560
}
1561

1562
static int32_t vnodeProcessDropStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
5,104✔
1563
  SVDropStbReq req = {0};
5,104✔
1564
  int32_t      rcode = TSDB_CODE_SUCCESS;
5,104✔
1565
  SDecoder     decoder = {0};
5,104✔
1566
  SArray      *tbUidList = NULL;
5,104✔
1567

1568
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
5,104✔
1569
  pRsp->pCont = NULL;
5,104✔
1570
  pRsp->contLen = 0;
5,104✔
1571

1572
  // decode request
1573
  tDecoderInit(&decoder, pReq, len);
5,104✔
1574
  if (tDecodeSVDropStbReq(&decoder, &req) < 0) {
5,104!
1575
    rcode = TSDB_CODE_INVALID_MSG;
×
1576
    goto _exit;
×
1577
  }
1578

1579
  // process request
1580
  tbUidList = taosArrayInit(8, sizeof(int64_t));
5,104✔
1581
  if (tbUidList == NULL) goto _exit;
5,110!
1582
  if (metaDropSuperTable(pVnode->pMeta, ver, &req) < 0) {
5,110✔
1583
    rcode = terrno;
2✔
1584
    goto _exit;
2✔
1585
  }
1586

1587
  if (tqUpdateTbUidList(pVnode->pTq, tbUidList, false) < 0) {
5,108!
1588
    rcode = terrno;
×
1589
    goto _exit;
×
1590
  }
1591

1592
  if (tdProcessRSmaDrop(pVnode->pSma, &req) < 0) {
5,108!
1593
    rcode = terrno;
×
1594
    goto _exit;
×
1595
  }
1596

1597
  // return rsp
1598
_exit:
5,108✔
1599
  if (tbUidList) taosArrayDestroy(tbUidList);
5,110!
1600
  pRsp->code = rcode;
5,110✔
1601
  tDecoderClear(&decoder);
5,110✔
1602
  return 0;
5,110✔
1603
}
1604

1605
static int32_t vnodeProcessAlterTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
19,767✔
1606
  SVAlterTbReq  vAlterTbReq = {0};
19,767✔
1607
  SVAlterTbRsp  vAlterTbRsp = {0};
19,767✔
1608
  SDecoder      dc = {0};
19,767✔
1609
  int32_t       code = 0;
19,767✔
1610
  int32_t       lino = 0;
19,767✔
1611
  int32_t       ret;
1612
  SEncoder      ec = {0};
19,767✔
1613
  STableMetaRsp vMetaRsp = {0};
19,767✔
1614

1615
  pRsp->msgType = TDMT_VND_ALTER_TABLE_RSP;
19,767✔
1616
  pRsp->pCont = NULL;
19,767✔
1617
  pRsp->contLen = 0;
19,767✔
1618
  pRsp->code = TSDB_CODE_SUCCESS;
19,767✔
1619

1620
  tDecoderInit(&dc, pReq, len);
19,767✔
1621

1622
  // decode
1623
  if (tDecodeSVAlterTbReq(&dc, &vAlterTbReq) < 0) {
19,767!
1624
    vAlterTbRsp.code = TSDB_CODE_INVALID_MSG;
×
1625
    tDecoderClear(&dc);
×
1626
    goto _exit;
×
1627
  }
1628

1629
  // process
1630
  if (metaAlterTable(pVnode->pMeta, ver, &vAlterTbReq, &vMetaRsp) < 0) {
19,767✔
1631
    vAlterTbRsp.code = terrno;
922✔
1632
    tDecoderClear(&dc);
922✔
1633
    goto _exit;
922✔
1634
  }
1635
  tDecoderClear(&dc);
18,845✔
1636

1637
  if (NULL != vMetaRsp.pSchemas) {
18,845✔
1638
    vnodeUpdateMetaRsp(pVnode, &vMetaRsp);
5,574✔
1639
    vAlterTbRsp.pMeta = &vMetaRsp;
5,574✔
1640
  }
1641

1642
  if (vAlterTbReq.action == TSDB_ALTER_TABLE_UPDATE_TAG_VAL ||
18,845✔
1643
      vAlterTbReq.action == TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL) {
5,694✔
1644
    int64_t uid = metaGetTableEntryUidByName(pVnode->pMeta, vAlterTbReq.tbName);
13,156✔
1645
    if (uid == 0) {
13,156!
1646
      vError("vgId:%d, %s failed at %s:%d since table %s not found", TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
1647
             vAlterTbReq.tbName);
1648
      goto _exit;
×
1649
    }
1650

1651
    SArray *tbUids = taosArrayInit(4, sizeof(int64_t));
13,156✔
1652
    void   *p = taosArrayPush(tbUids, &uid);
13,156✔
1653
    TSDB_CHECK_NULL(p, code, lino, _exit, terrno);
13,156!
1654

1655
    vDebug("vgId:%d, remove tags value altered table:%s from query table list", TD_VID(pVnode), vAlterTbReq.tbName);
13,156✔
1656
    if ((code = tqUpdateTbUidList(pVnode->pTq, tbUids, false)) < 0) {
13,156!
1657
      vError("vgId:%d, failed to remove tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1658
    }
1659

1660
    vDebug("vgId:%d, try to add table:%s in query table list", TD_VID(pVnode), vAlterTbReq.tbName);
13,156✔
1661
    if ((code = tqUpdateTbUidList(pVnode->pTq, tbUids, true)) < 0) {
13,156!
1662
      vError("vgId:%d, failed to add tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1663
    }
1664

1665
    taosArrayDestroy(tbUids);
13,156✔
1666
  }
1667

1668
_exit:
5,689✔
1669
  taosArrayDestroy(vAlterTbReq.pMultiTag);
19,767✔
1670
  tEncodeSize(tEncodeSVAlterTbRsp, &vAlterTbRsp, pRsp->contLen, ret);
19,767!
1671
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
19,767✔
1672
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
19,767✔
1673
  if (tEncodeSVAlterTbRsp(&ec, &vAlterTbRsp) != 0) {
19,767!
1674
    vError("vgId:%d, failed to encode alter table response", TD_VID(pVnode));
×
1675
  }
1676

1677
  tEncoderClear(&ec);
19,767✔
1678
  if (vMetaRsp.pSchemas) {
19,767✔
1679
    taosMemoryFree(vMetaRsp.pSchemas);
5,574!
1680
    taosMemoryFree(vMetaRsp.pSchemaExt);
5,574!
1681
  }
1682
  if (vMetaRsp.pColRefs) {
19,767✔
1683
    taosMemoryFree(vMetaRsp.pColRefs);
342!
1684
  }
1685
  return 0;
19,767✔
1686
}
1687

1688
static int32_t vnodeProcessDropTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
15,724✔
1689
                                     SRpcMsg *pOriginRpc) {
1690
  SVDropTbBatchReq req = {0};
15,724✔
1691
  SVDropTbBatchRsp rsp = {0};
15,724✔
1692
  SDecoder         decoder = {0};
15,724✔
1693
  SEncoder         encoder = {0};
15,724✔
1694
  int32_t          ret;
1695
  SArray          *tbUids = NULL;
15,724✔
1696
  STbUidStore     *pStore = NULL;
15,724✔
1697
  SArray          *tbNames = NULL;
15,724✔
1698

1699
  pRsp->msgType = TDMT_VND_DROP_TABLE_RSP;
15,724✔
1700
  pRsp->pCont = NULL;
15,724✔
1701
  pRsp->contLen = 0;
15,724✔
1702
  pRsp->code = TSDB_CODE_SUCCESS;
15,724✔
1703

1704
  // decode req
1705
  tDecoderInit(&decoder, pReq, len);
15,724✔
1706
  ret = tDecodeSVDropTbBatchReq(&decoder, &req);
15,724✔
1707
  if (ret < 0) {
15,724!
1708
    terrno = TSDB_CODE_INVALID_MSG;
×
1709
    pRsp->code = terrno;
×
1710
    goto _exit;
×
1711
  }
1712

1713
  // process req
1714
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
15,724✔
1715
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(SVDropTbRsp));
15,724✔
1716
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
15,724✔
1717
  if (tbUids == NULL || rsp.pArray == NULL || tbNames == NULL) goto _exit;
15,724!
1718

1719
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
31,851✔
1720
    SVDropTbReq *pDropTbReq = req.pReqs + iReq;
16,127✔
1721
    SVDropTbRsp  dropTbRsp = {0};
16,127✔
1722
    tb_uid_t     tbUid = 0;
16,127✔
1723

1724
    /* code */
1725
    ret = metaDropTable2(pVnode->pMeta, ver, pDropTbReq);
16,127✔
1726
    if (ret < 0) {
16,127!
1727
      if (pDropTbReq->igNotExists && terrno == TSDB_CODE_TDB_TABLE_NOT_EXIST) {
×
1728
        dropTbRsp.code = TSDB_CODE_SUCCESS;
×
1729
      } else {
1730
        dropTbRsp.code = terrno;
×
1731
      }
1732
    } else {
1733
      dropTbRsp.code = TSDB_CODE_SUCCESS;
16,127✔
1734
      if (tbUid > 0) {
16,127!
1735
        if (tdFetchTbUidList(pVnode->pSma, &pStore, pDropTbReq->suid, tbUid) < 0) {
×
1736
          vError("vgId:%d, failed to fetch tbUid list", TD_VID(pVnode));
×
1737
        }
1738
      }
1739
    }
1740

1741
    if (taosArrayPush(rsp.pArray, &dropTbRsp) == NULL) {
32,254!
1742
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1743
      pRsp->code = terrno;
×
1744
      goto _exit;
×
1745
    }
1746

1747
    if (tsEnableAuditCreateTable) {
16,127!
1748
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
16,127!
1749
      if (str == NULL) {
16,127!
1750
        pRsp->code = terrno;
×
1751
        goto _exit;
×
1752
      }
1753
      tstrncpy(str, pDropTbReq->name, TSDB_TABLE_FNAME_LEN);
16,127✔
1754
      if (taosArrayPush(tbNames, &str) == NULL) {
16,127!
1755
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1756
        pRsp->code = terrno;
×
1757
        goto _exit;
×
1758
      }
1759
    }
1760
  }
1761

1762
  if (tqUpdateTbUidList(pVnode->pTq, tbUids, false) < 0) {
15,724!
1763
    vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(terrno));
×
1764
  }
1765

1766
  if (tdUpdateTbUidList(pVnode->pSma, pStore, false) < 0) {
15,724!
1767
    goto _exit;
×
1768
  }
1769

1770
  if (tsEnableAuditCreateTable) {
15,724!
1771
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
15,724✔
1772

1773
    SName name = {0};
15,724✔
1774
    if (tNameFromString(&name, pVnode->config.dbname, T_NAME_ACCT | T_NAME_DB) != 0) {
15,724!
1775
      vError("vgId:%d, failed to get name from string", TD_VID(pVnode));
×
1776
    }
1777

1778
    SStringBuilder sb = {0};
15,724✔
1779
    for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
31,851✔
1780
      char **key = (char **)taosArrayGet(tbNames, iReq);
16,127✔
1781
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
16,127✔
1782
      if (iReq < req.nReqs - 1) {
16,127✔
1783
        taosStringBuilderAppendChar(&sb, ',');
403✔
1784
      }
1785
      taosMemoryFreeClear(*key);
16,127!
1786
    }
1787

1788
    size_t len = 0;
15,724✔
1789
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
15,724✔
1790

1791
    if (pOriginRpc->info.conn.user != NULL && strlen(pOriginRpc->info.conn.user) > 0) {
15,724!
1792
      auditAddRecord(pOriginRpc, clusterId, "dropTable", name.dbname, "", keyJoined, len);
15,723✔
1793
    }
1794

1795
    taosStringBuilderDestroy(&sb);
15,724✔
1796
  }
1797

1798
_exit:
×
1799
  taosArrayDestroy(tbUids);
15,724✔
1800
  pStore = tdUidStoreFree(pStore);
15,724✔
1801
  tDecoderClear(&decoder);
15,724✔
1802
  tEncodeSize(tEncodeSVDropTbBatchRsp, &rsp, pRsp->contLen, ret);
15,724!
1803
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
15,724✔
1804
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
15,724✔
1805
  if (tEncodeSVDropTbBatchRsp(&encoder, &rsp) != 0) {
15,724!
1806
    vError("vgId:%d, failed to encode drop table batch response", TD_VID(pVnode));
×
1807
  }
1808
  tEncoderClear(&encoder);
15,724✔
1809
  taosArrayDestroy(rsp.pArray);
15,724✔
1810
  taosArrayDestroy(tbNames);
15,724✔
1811
  return 0;
15,724✔
1812
}
1813

1814
#ifdef BUILD_NO_CALL
1815
static int32_t vnodeDebugPrintSingleSubmitMsg(SMeta *pMeta, SSubmitBlk *pBlock, SSubmitMsgIter *msgIter,
1816
                                              const char *tags) {
1817
  SSubmitBlkIter blkIter = {0};
1818
  STSchema      *pSchema = NULL;
1819
  tb_uid_t       suid = 0;
1820
  STSRow        *row = NULL;
1821
  int32_t        rv = -1;
1822

1823
  tInitSubmitBlkIter(msgIter, pBlock, &blkIter);
1824
  if (blkIter.row == NULL) return 0;
1825

1826
  int32_t code = metaGetTbTSchemaNotNull(pMeta, msgIter->suid, TD_ROW_SVER(blkIter.row), 1,
1827
                                         &pSchema);  // TODO: use the real schema
1828
  if (TSDB_CODE_SUCCESS != code) {
1829
    printf("%s:%d no valid schema\n", tags, __LINE__);
1830
    return code;
1831
  }
1832

1833
  suid = msgIter->suid;
1834
  rv = TD_ROW_SVER(blkIter.row);
1835

1836
  char __tags[128] = {0};
1837
  snprintf(__tags, 128, "%s: uid %" PRIi64 " ", tags, msgIter->uid);
1838
  while ((row = tGetSubmitBlkNext(&blkIter))) {
1839
    tdSRowPrint(row, pSchema, __tags);
1840
  }
1841

1842
  taosMemoryFreeClear(pSchema);
1843

1844
  return TSDB_CODE_SUCCESS;
1845
}
1846
#endif
1847
typedef struct SSubmitReqConvertCxt {
1848
  SSubmitMsgIter msgIter;
1849
  SSubmitBlk    *pBlock;
1850
  SSubmitBlkIter blkIter;
1851
  STSRow        *pRow;
1852
  STSRowIter     rowIter;
1853
  SSubmitTbData *pTbData;
1854
  STSchema      *pTbSchema;
1855
  SArray        *pColValues;
1856
} SSubmitReqConvertCxt;
1857

1858
static int32_t vnodeResetTableCxt(SMeta *pMeta, SSubmitReqConvertCxt *pCxt) {
×
1859
  taosMemoryFreeClear(pCxt->pTbSchema);
×
1860
  int32_t code = metaGetTbTSchemaNotNull(pMeta, pCxt->msgIter.suid > 0 ? pCxt->msgIter.suid : pCxt->msgIter.uid,
×
1861
                                         pCxt->msgIter.sversion, 1, &pCxt->pTbSchema);
1862
  if (TSDB_CODE_SUCCESS != code) {
×
1863
    return code;
×
1864
  }
1865
  tdSTSRowIterInit(&pCxt->rowIter, pCxt->pTbSchema);
×
1866

1867
  tDestroySubmitTbData(pCxt->pTbData, TSDB_MSG_FLG_ENCODE);
×
1868
  if (NULL == pCxt->pTbData) {
×
1869
    pCxt->pTbData = taosMemoryCalloc(1, sizeof(SSubmitTbData));
×
1870
    if (NULL == pCxt->pTbData) {
×
1871
      return terrno;
×
1872
    }
1873
  }
1874
  pCxt->pTbData->flags = 0;
×
1875
  pCxt->pTbData->suid = pCxt->msgIter.suid;
×
1876
  pCxt->pTbData->uid = pCxt->msgIter.uid;
×
1877
  pCxt->pTbData->sver = pCxt->msgIter.sversion;
×
1878
  pCxt->pTbData->pCreateTbReq = NULL;
×
1879
  pCxt->pTbData->aRowP = taosArrayInit(128, POINTER_BYTES);
×
1880
  if (NULL == pCxt->pTbData->aRowP) {
×
1881
    return terrno;
×
1882
  }
1883

1884
  taosArrayDestroy(pCxt->pColValues);
×
1885
  pCxt->pColValues = taosArrayInit(pCxt->pTbSchema->numOfCols, sizeof(SColVal));
×
1886
  if (NULL == pCxt->pColValues) {
×
1887
    return terrno;
×
1888
  }
1889
  for (int32_t i = 0; i < pCxt->pTbSchema->numOfCols; ++i) {
×
1890
    SColVal val = COL_VAL_NONE(pCxt->pTbSchema->columns[i].colId, pCxt->pTbSchema->columns[i].type);
×
1891
    if (taosArrayPush(pCxt->pColValues, &val) == NULL) {
×
1892
      return terrno;
×
1893
    }
1894
  }
1895

1896
  return TSDB_CODE_SUCCESS;
×
1897
}
1898

1899
static void vnodeDestroySubmitReqConvertCxt(SSubmitReqConvertCxt *pCxt) {
×
1900
  taosMemoryFreeClear(pCxt->pTbSchema);
×
1901
  tDestroySubmitTbData(pCxt->pTbData, TSDB_MSG_FLG_ENCODE);
×
1902
  taosMemoryFreeClear(pCxt->pTbData);
×
1903
  taosArrayDestroy(pCxt->pColValues);
×
1904
}
×
1905

1906
static int32_t vnodeCellValConvertToColVal(STColumn *pCol, SCellVal *pCellVal, SColVal *pColVal) {
×
1907
  if (tdValTypeIsNone(pCellVal->valType)) {
×
1908
    pColVal->flag = CV_FLAG_NONE;
×
1909
    return TSDB_CODE_SUCCESS;
×
1910
  }
1911

1912
  if (tdValTypeIsNull(pCellVal->valType)) {
×
1913
    pColVal->flag = CV_FLAG_NULL;
×
1914
    return TSDB_CODE_SUCCESS;
×
1915
  }
1916

1917
  if (IS_VAR_DATA_TYPE(pCol->type)) {
×
1918
    if (IS_STR_DATA_BLOB(pCol->type)) {
×
1919
      pColVal->value.nData = blobDataLen(pCellVal->val);
×
1920
      pColVal->value.pData = (uint8_t *)blobDataVal(pCellVal->val);
×
1921

1922
    } else {
1923
      pColVal->value.nData = varDataLen(pCellVal->val);
×
1924
      pColVal->value.pData = (uint8_t *)varDataVal(pCellVal->val);
×
1925
    }
1926
  } else if (TSDB_DATA_TYPE_FLOAT == pCol->type) {
×
1927
    float f = GET_FLOAT_VAL(pCellVal->val);
×
1928
    valueSetDatum(&pColVal->value, pCol->type, &f, sizeof(f));
×
1929
  } else if (TSDB_DATA_TYPE_DOUBLE == pCol->type) {
×
1930
    taosSetPInt64Aligned(&pColVal->value.val, (int64_t *)pCellVal->val);
×
1931
  } else {
1932
    valueSetDatum(&pColVal->value, pCol->type, pCellVal->val, tDataTypes[pCol->type].bytes);
×
1933
  }
1934

1935
  pColVal->flag = CV_FLAG_VALUE;
×
1936
  return TSDB_CODE_SUCCESS;
×
1937
}
1938

1939
static int32_t vnodeTSRowConvertToColValArray(SSubmitReqConvertCxt *pCxt) {
×
1940
  int32_t code = TSDB_CODE_SUCCESS;
×
1941
  tdSTSRowIterReset(&pCxt->rowIter, pCxt->pRow);
×
1942
  for (int32_t i = 0; TSDB_CODE_SUCCESS == code && i < pCxt->pTbSchema->numOfCols; ++i) {
×
1943
    STColumn *pCol = pCxt->pTbSchema->columns + i;
×
1944
    SCellVal  cellVal = {0};
×
1945
    if (!tdSTSRowIterFetch(&pCxt->rowIter, pCol->colId, pCol->type, &cellVal)) {
×
1946
      break;
×
1947
    }
1948
    code = vnodeCellValConvertToColVal(pCol, &cellVal, (SColVal *)taosArrayGet(pCxt->pColValues, i));
×
1949
  }
1950
  return code;
×
1951
}
1952

1953
static int32_t vnodeDecodeCreateTbReq(SSubmitReqConvertCxt *pCxt) {
×
1954
  if (pCxt->msgIter.schemaLen <= 0) {
×
1955
    return TSDB_CODE_SUCCESS;
×
1956
  }
1957

1958
  pCxt->pTbData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
×
1959
  if (NULL == pCxt->pTbData->pCreateTbReq) {
×
1960
    return terrno;
×
1961
  }
1962

1963
  SDecoder decoder = {0};
×
1964
  tDecoderInit(&decoder, (uint8_t *)pCxt->pBlock->data, pCxt->msgIter.schemaLen);
×
1965
  int32_t code = tDecodeSVCreateTbReq(&decoder, pCxt->pTbData->pCreateTbReq);
×
1966
  tDecoderClear(&decoder);
×
1967

1968
  return code;
×
1969
}
1970

1971
static int32_t vnodeSubmitReqConvertToSubmitReq2(SVnode *pVnode, SSubmitReq *pReq, SSubmitReq2 *pReq2) {
×
1972
  pReq2->aSubmitTbData = taosArrayInit(128, sizeof(SSubmitTbData));
×
1973
  if (NULL == pReq2->aSubmitTbData) {
×
1974
    return terrno;
×
1975
  }
1976

1977
  SSubmitReqConvertCxt cxt = {0};
×
1978

1979
  int32_t code = tInitSubmitMsgIter(pReq, &cxt.msgIter);
×
1980
  while (TSDB_CODE_SUCCESS == code) {
×
1981
    code = tGetSubmitMsgNext(&cxt.msgIter, &cxt.pBlock);
×
1982
    if (TSDB_CODE_SUCCESS == code) {
×
1983
      if (NULL == cxt.pBlock) {
×
1984
        break;
×
1985
      }
1986
      code = vnodeResetTableCxt(pVnode->pMeta, &cxt);
×
1987
    }
1988
    if (TSDB_CODE_SUCCESS == code) {
×
1989
      code = tInitSubmitBlkIter(&cxt.msgIter, cxt.pBlock, &cxt.blkIter);
×
1990
    }
1991
    if (TSDB_CODE_SUCCESS == code) {
×
1992
      code = vnodeDecodeCreateTbReq(&cxt);
×
1993
    }
1994
    while (TSDB_CODE_SUCCESS == code && (cxt.pRow = tGetSubmitBlkNext(&cxt.blkIter)) != NULL) {
×
1995
      code = vnodeTSRowConvertToColValArray(&cxt);
×
1996
      if (TSDB_CODE_SUCCESS == code) {
×
1997
        SRow **pNewRow = taosArrayReserve(cxt.pTbData->aRowP, 1);
×
1998

1999
        SRowBuildScanInfo sinfo = {0};
×
2000
        code = tRowBuild(cxt.pColValues, cxt.pTbSchema, pNewRow, &sinfo);
×
2001
      }
2002
    }
2003
    if (TSDB_CODE_SUCCESS == code) {
×
2004
      code = (NULL == taosArrayPush(pReq2->aSubmitTbData, cxt.pTbData) ? terrno : TSDB_CODE_SUCCESS);
×
2005
    }
2006
    if (TSDB_CODE_SUCCESS == code) {
×
2007
      taosMemoryFreeClear(cxt.pTbData);
×
2008
    }
2009
  }
2010

2011
  vnodeDestroySubmitReqConvertCxt(&cxt);
×
2012
  return code;
×
2013
}
2014

2015
static int32_t vnodeRebuildSubmitReqMsg(SSubmitReq2 *pSubmitReq, void **ppMsg) {
×
2016
  int32_t  code = TSDB_CODE_SUCCESS;
×
2017
  char    *pMsg = NULL;
×
2018
  uint32_t msglen = 0;
×
2019
  tEncodeSize(tEncodeSubmitReq, pSubmitReq, msglen, code);
×
2020
  if (TSDB_CODE_SUCCESS == code) {
×
2021
    pMsg = taosMemoryMalloc(msglen);
×
2022
    if (NULL == pMsg) {
×
2023
      code = terrno;
×
2024
    }
2025
  }
2026
  if (TSDB_CODE_SUCCESS == code) {
×
2027
    SEncoder encoder;
2028
    tEncoderInit(&encoder, (uint8_t *)pMsg, msglen);
×
2029
    code = tEncodeSubmitReq(&encoder, pSubmitReq);
×
2030
    tEncoderClear(&encoder);
×
2031
  }
2032
  if (TSDB_CODE_SUCCESS == code) {
×
2033
    *ppMsg = pMsg;
×
2034
  }
2035
  return code;
×
2036
}
2037

2038
static int32_t buildExistSubTalbeRsp(SVnode *pVnode, SSubmitTbData *pSubmitTbData, STableMetaRsp **ppRsp) {
306✔
2039
  int32_t code = 0;
306✔
2040
  int32_t lino = 0;
306✔
2041

2042
  SMetaEntry *pEntry = NULL;
306✔
2043
  code = metaFetchEntryByUid(pVnode->pMeta, pSubmitTbData->suid, &pEntry);
306✔
2044
  if (code) {
306!
2045
    vError("vgId:%d, table uid:%" PRId64 " not exists, line:%d", TD_VID(pVnode), pSubmitTbData->uid, __LINE__);
×
2046
    TSDB_CHECK_CODE(code, lino, _exit);
×
2047
  }
2048
  if (pEntry->type != TSDB_SUPER_TABLE) {
306!
2049
    vError("vgId:%d, table uid:%" PRId64 " exists, but is not super table, line:%d", TD_VID(pVnode), pSubmitTbData->uid,
×
2050
           __LINE__);
2051
    code = TSDB_CODE_STREAM_INSERT_SCHEMA_NOT_MATCH;
×
2052
    TSDB_CHECK_CODE(code, lino, _exit);
×
2053
  }
2054

2055
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
306!
2056
  if (NULL == *ppRsp) {
306!
2057
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2058
  }
2059
  (*ppRsp)->suid = pSubmitTbData->suid;
306✔
2060
  (*ppRsp)->tuid = pSubmitTbData->uid;
306✔
2061
  (*ppRsp)->sversion = pEntry->stbEntry.schemaRow.version;
306✔
2062
  (*ppRsp)->vgId = pVnode->config.vgId;
306✔
2063
  (*ppRsp)->numOfColumns = pEntry->stbEntry.schemaRow.nCols;
306✔
2064
  (*ppRsp)->numOfTags = pEntry->stbEntry.schemaTag.nCols;
306✔
2065
  (*ppRsp)->pSchemas =
612✔
2066
      taosMemoryCalloc(pEntry->stbEntry.schemaRow.nCols + pEntry->stbEntry.schemaTag.nCols, sizeof(SSchema));
306!
2067
  if (NULL == (*ppRsp)->pSchemas) {
306!
2068
    taosMemoryFree(*ppRsp);
×
2069
    *ppRsp = NULL;
×
2070
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2071
  }
2072
  memcpy((*ppRsp)->pSchemas, pEntry->stbEntry.schemaRow.pSchema, pEntry->stbEntry.schemaRow.nCols * sizeof(SSchema));
306✔
2073
  memcpy((*ppRsp)->pSchemas + pEntry->stbEntry.schemaRow.nCols, pEntry->stbEntry.schemaTag.pSchema,
306✔
2074
         pEntry->stbEntry.schemaTag.nCols * sizeof(SSchema));
306✔
2075
  if (pEntry->pExtSchemas != NULL) {
306!
2076
    (*ppRsp)->pSchemaExt = taosMemoryCalloc(pEntry->colCmpr.nCols, sizeof(SExtSchema));
×
2077
    if (NULL == (*ppRsp)->pSchemaExt) {
×
2078
      taosMemoryFree((*ppRsp)->pSchemas);
×
2079
      taosMemoryFree(*ppRsp);
×
2080
      *ppRsp = NULL;
×
2081
      TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2082
    }
2083
    memcpy((*ppRsp)->pSchemaExt, pEntry->pExtSchemas, pEntry->colCmpr.nCols * sizeof(SExtSchema));
×
2084
  }
2085

2086
  if (pEntry->stbEntry.schemaRow.version != pSubmitTbData->sver) {
306✔
2087
    TSDB_CHECK_CODE(code = TSDB_CODE_TDB_TABLE_ALREADY_EXIST, lino, _exit);
2✔
2088
  }
2089
_exit:
304✔
2090
  metaFetchEntryFree(&pEntry);
306✔
2091
  if (code != TSDB_CODE_SUCCESS) {
306✔
2092
    vError("vgId:%d, failed to build exist sub table response, code:0x%0x, line:%d", TD_VID(pVnode), code, lino);
2!
2093
  }
2094
  return code;
306✔
2095
}
2096

2097
static int32_t buildExistNormalTalbeRsp(SVnode *pVnode, int64_t uid, STableMetaRsp **ppRsp) {
130✔
2098
  int32_t code = 0;
130✔
2099
  int32_t lino = 0;
130✔
2100

2101
  SMetaEntry *pEntry = NULL;
130✔
2102
  code = metaFetchEntryByUid(pVnode->pMeta, uid, &pEntry);
130✔
2103
  if (code) {
130!
2104
    vError("vgId:%d, table uid:%" PRId64 " not exists, line:%d", TD_VID(pVnode), uid, __LINE__);
×
2105
    TSDB_CHECK_CODE(code, lino, _exit);
×
2106
  }
2107
  if (pEntry->type != TSDB_NORMAL_TABLE) {
130!
2108
    vError("vgId:%d, table uid:%" PRId64 " exists, but is not normal table, line:%d", TD_VID(pVnode),
×
2109
           uid, __LINE__);
2110
    code = TSDB_CODE_STREAM_INSERT_SCHEMA_NOT_MATCH;
×
2111
    TSDB_CHECK_CODE(code, lino, _exit);
×
2112
  }
2113

2114
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
130!
2115
  if (NULL == *ppRsp) {
130!
2116
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2117
  }
2118

2119
  (*ppRsp)->tuid = pEntry->uid;
130✔
2120
  (*ppRsp)->vgId = pVnode->config.vgId;
130✔
2121
  (*ppRsp)->sversion = pEntry->ntbEntry.schemaRow.version;
130✔
2122
  (*ppRsp)->numOfColumns = pEntry->ntbEntry.schemaRow.nCols;
130✔
2123
  (*ppRsp)->pSchemas = taosMemoryCalloc(pEntry->ntbEntry.schemaRow.nCols, sizeof(SSchema));
130!
2124
  if (NULL == (*ppRsp)->pSchemas) {
130!
2125
    taosMemoryFree(*ppRsp);
×
2126
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2127
  }
2128
  memcpy((*ppRsp)->pSchemas, pEntry->ntbEntry.schemaRow.pSchema, pEntry->ntbEntry.schemaRow.nCols * sizeof(SSchema));
130✔
2129
  if (pEntry->pExtSchemas != NULL) {
130!
2130
    (*ppRsp)->pSchemaExt = taosMemoryCalloc(pEntry->ntbEntry.schemaRow.nCols, sizeof(SSchemaExt));
×
2131
    if (NULL == (*ppRsp)->pSchemaExt) {
×
2132
      taosMemoryFree((*ppRsp)->pSchemas);
×
2133
      taosMemoryFree(*ppRsp);
×
2134
      TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2135
    }
2136
    memcpy((*ppRsp)->pSchemaExt, pEntry->pExtSchemas, pEntry->ntbEntry.schemaRow.nCols * sizeof(SSchemaExt));
×
2137
  }
2138

2139
_exit:
130✔
2140
  metaFetchEntryFree(&pEntry);
130✔
2141
  if (code != TSDB_CODE_SUCCESS) {
130!
2142
    vError("vgId:%d, failed to build exist normal table response, code:%d, line:%d", TD_VID(pVnode), code, lino);
×
2143
  }
2144
  return code;
130✔
2145
}
2146

2147
static int32_t buildExistTableInStreamRsp(SVnode *pVnode, SSubmitTbData *pSubmitTbData, STableMetaRsp **ppRsp) {
2,971✔
2148
  if (pSubmitTbData->pCreateTbReq->flags & TD_CREATE_NORMAL_TB_IN_STREAM) {
2,971✔
2149
    int32_t code = buildExistNormalTalbeRsp(pVnode, pSubmitTbData->uid, ppRsp);
130✔
2150
    if (code) {
130!
2151
      vError("vgId:%d, table uid:%" PRId64 " not exists, line:%d", TD_VID(pVnode), pSubmitTbData->uid, __LINE__);
×
2152
      return code;
×
2153
    }
2154
    return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
130✔
2155
  } else if (pSubmitTbData->pCreateTbReq->flags & TD_CREATE_SUB_TB_IN_STREAM) {
2,841✔
2156
    return buildExistSubTalbeRsp(pVnode, pSubmitTbData, ppRsp);
306✔
2157
  }
2158
  return TSDB_CODE_SUCCESS;
2,535✔
2159
}
2160

2161
static int32_t vnodeHandleAutoCreateTable(SVnode      *pVnode,    // vnode
25,004,944✔
2162
                                          int64_t      version,   // version
2163
                                          SSubmitReq2 *pRequest,  // request
2164
                                          SSubmitRsp2 *pResponse  // response
2165
) {
2166
  int32_t code = TSDB_CODE_SUCCESS;
25,004,944✔
2167
  int32_t numTbData = taosArrayGetSize(pRequest->aSubmitTbData);
25,004,944✔
2168
  SArray *newTbUids = NULL;
25,005,150✔
2169

2170
  for (int32_t i = 0; i < numTbData; ++i) {
50,086,758✔
2171
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
25,081,600✔
2172

2173
    if (pTbData->pCreateTbReq == NULL) {
25,081,764✔
2174
      continue;
25,023,747✔
2175
    }
2176

2177
    pTbData->uid = pTbData->pCreateTbReq->uid;
58,017✔
2178

2179
    // Alloc necessary resources
2180
    if (pResponse->aCreateTbRsp == NULL) {
58,017✔
2181
      pResponse->aCreateTbRsp = taosArrayInit(numTbData, sizeof(SVCreateTbRsp));
38,885✔
2182
      if (pResponse->aCreateTbRsp == NULL) {
38,886!
2183
        code = terrno;
×
2184
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2185
               tstrerror(code), version);
2186
        taosArrayDestroy(newTbUids);
×
2187
        return code;
×
2188
      }
2189
    }
2190

2191
    // Do create table
2192
    vDebug("vgId:%d start to handle auto create table, version:%" PRId64, TD_VID(pVnode), version);
58,018✔
2193

2194
    SVCreateTbRsp *pCreateTbRsp = taosArrayReserve(pResponse->aCreateTbRsp, 1);
58,021✔
2195
    code = metaCreateTable2(pVnode->pMeta, version, pTbData->pCreateTbReq, &pCreateTbRsp->pMeta);
58,055✔
2196
    if (code == TSDB_CODE_SUCCESS) {
58,055✔
2197
      // Allocate necessary resources
2198
      if (newTbUids == NULL) {
48,113✔
2199
        newTbUids = taosArrayInit(numTbData, sizeof(int64_t));
35,913✔
2200
        if (newTbUids == NULL) {
35,913!
2201
          code = terrno;
×
2202
          vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2203
                 tstrerror(code), version);
2204
          return code;
×
2205
        }
2206
      }
2207

2208
      if (taosArrayPush(newTbUids, &pTbData->uid) == NULL) {
96,226!
2209
        code = terrno;
×
2210
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2211
               tstrerror(code), version);
2212
        taosArrayDestroy(newTbUids);
×
2213
        return code;
×
2214
      }
2215

2216
      if (pCreateTbRsp->pMeta) {
48,113!
2217
        vnodeUpdateMetaRsp(pVnode, pCreateTbRsp->pMeta);
48,113✔
2218
      }
2219
    } else if (code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
9,942✔
2220
      code = terrno = 0;
9,934✔
2221
      pTbData->uid = pTbData->pCreateTbReq->uid;  // update uid if table exist for using below
9,934✔
2222

2223
      // stream: get sver from meta, write to pCreateTbRsp, and need to check crateTbReq is same as meta.
2224
      if (i == 0) {
9,934✔
2225
        // In the streaming scenario, multiple grouped req requests will only operate on the same write table, and
2226
        // only the first one needs to be processed.
2227
        code = buildExistTableInStreamRsp(pVnode, pTbData, &pCreateTbRsp->pMeta);
2,971✔
2228
        if (code) {
2,971✔
2229
          vInfo("vgId:%d failed to create table in stream:%s, code(0x%0x):%s", TD_VID(pVnode),
132!
2230
                pTbData->pCreateTbReq->name, code, tstrerror(code));
2231
          taosArrayDestroy(newTbUids);
132✔
2232
          return code;
132✔
2233
        }
2234
      }
2235
    } else {
2236
      code = terrno;
8✔
2237
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
8!
2238
             tstrerror(code), version);
2239
      taosArrayDestroy(newTbUids);
8✔
2240
      return code;
8✔
2241
    }
2242
  }
2243

2244
  // Update the affected table uid list
2245
  if (taosArrayGetSize(newTbUids) > 0) {
25,005,158✔
2246
    vDebug("vgId:%d, add %d table into query table list in handling submit", TD_VID(pVnode),
35,913✔
2247
           (int32_t)taosArrayGetSize(newTbUids));
2248
    if (tqUpdateTbUidList(pVnode->pTq, newTbUids, true) != 0) {
35,913!
2249
      vError("vgId:%d, failed to update tbUid list", TD_VID(pVnode));
×
2250
    }
2251
  }
2252

2253
  vDebug("vgId:%d, handle auto create table done, version:%" PRId64, TD_VID(pVnode), version);
25,005,004✔
2254

2255
  taosArrayDestroy(newTbUids);
25,005,005✔
2256
  return code;
25,004,767✔
2257
}
2258

2259
static void addExistTableInfoIntoRes(SVnode *pVnode, SSubmitReq2 *pRequest, SSubmitRsp2 *pResponse, SSubmitTbData *pTbData,
2✔
2260
                                     int32_t numTbData) {
2261
  int32_t code = 0;
2✔
2262
  int32_t lino = 0;
2✔
2263
  if ((pTbData->flags & SUBMIT_REQ_SCHEMA_RES) == 0) {
2!
2264
    return;
×
2265
  }
2266
  if (pResponse->aCreateTbRsp) {  // If aSubmitTbData is not NULL, it means that the request is a create table request,
2!
2267
                                  // so table info has exitst and we do not need to add again.
2268
    return;
2✔
2269
  }
2270
  pResponse->aCreateTbRsp = taosArrayInit(numTbData, sizeof(SVCreateTbRsp));
×
2271
  if (pResponse->aCreateTbRsp == NULL) {
×
2272
    code = terrno;
×
2273
    TSDB_CHECK_CODE(code, lino, _exit);
×
2274
  }
2275
  SVCreateTbRsp *pCreateTbRsp = taosArrayReserve(pResponse->aCreateTbRsp, 1);
×
2276
  if (pCreateTbRsp == NULL) {
×
2277
    code = terrno;
×
2278
    TSDB_CHECK_CODE(code, lino, _exit);
×
2279
  }
2280
  if (pTbData->suid == 0) {
×
2281
    code = buildExistNormalTalbeRsp(pVnode, pTbData->uid, &pCreateTbRsp->pMeta);
×
2282
    if (code) {
×
2283
      vError("vgId:%d, table uid:%" PRId64 " not exists, line:%d", TD_VID(pVnode), pTbData->uid, __LINE__);
×
2284
    }
2285
  } else {
2286
    code = buildExistSubTalbeRsp(pVnode, pTbData, &pCreateTbRsp->pMeta);
×
2287
  }
2288

2289
  TSDB_CHECK_CODE(code, lino, _exit);
×
2290
_exit:
×
2291
  if (code != TSDB_CODE_SUCCESS) {
×
2292
    vError("vgId:%d, failed to add exist table info into response, code:0x%0x, line:%d", TD_VID(pVnode), code, lino);
×
2293
  }
2294
  return;
×
2295
}
2296

2297
static int32_t vnodeHandleDataWrite(SVnode *pVnode, int64_t version, SSubmitReq2 *pRequest, SSubmitRsp2 *pResponse) {
25,004,693✔
2298
  int32_t code = TSDB_CODE_SUCCESS;
25,004,693✔
2299
  int32_t numTbData = taosArrayGetSize(pRequest->aSubmitTbData);
25,004,693✔
2300
  int8_t  hasBlob = 0;
25,004,917✔
2301

2302
  // Scan submit data
2303
  for (int32_t i = 0; i < numTbData; ++i) {
50,086,833✔
2304
    SMetaInfo      info = {0};
25,081,402✔
2305
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
25,081,402✔
2306

2307
    if (pTbData->flags & SUBMIT_REQ_WITH_BLOB) {
25,081,597✔
2308
      hasBlob = 1;
1✔
2309
    }
2310
    if (pTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
25,081,597✔
2311
      continue;  // skip column data format
1,145✔
2312
    }
2313

2314
    code = metaGetInfo(pVnode->pMeta, pTbData->uid, &info, NULL);
25,080,452✔
2315
    if (code) {
25,080,586✔
2316
      code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
16✔
2317
      vWarn("vgId:%d, error occurred at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __FILE__,
16✔
2318
            __LINE__, tstrerror(code), version, pTbData->uid);
2319
      return code;
5✔
2320
    }
2321

2322
    if (info.suid != pTbData->suid) {
25,080,570!
2323
      code = TSDB_CODE_INVALID_MSG;
×
2324
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64 " suid:%" PRId64
×
2325
             " info.suid:%" PRId64,
2326
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), version, pTbData->uid, pTbData->suid,
2327
             info.suid);
2328
      return code;
×
2329
    }
2330

2331
    if (info.suid) {
25,080,570✔
2332
      code = metaGetInfo(pVnode->pMeta, info.suid, &info, NULL);
24,147,552✔
2333
      if (code) {
24,147,686!
2334
        code = TSDB_CODE_INTERNAL_ERROR;
×
2335
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " suid:%" PRId64, TD_VID(pVnode), __func__,
×
2336
               __FILE__, __LINE__, tstrerror(code), version, info.suid);
2337
        return code;
×
2338
      }
2339
    }
2340

2341
    if (pTbData->sver != info.skmVer) {
25,080,773✔
2342
      code = TSDB_CODE_TDB_INVALID_TABLE_SCHEMA_VER;
2✔
2343
      addExistTableInfoIntoRes(pVnode, pRequest, pResponse, pTbData, numTbData);
2✔
2344
      vDebug("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64
2!
2345
             " sver:%d"
2346
             " info.skmVer:%d",
2347
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), version, pTbData->uid, pTbData->sver,
2348
             info.skmVer);
2349
      return code;
2✔
2350
    }
2351
  }
2352

2353
  // Do write data
2354
  vDebug("vgId:%d start to handle data write, version:%" PRId64, TD_VID(pVnode), version);
25,005,431✔
2355

2356
  for (int32_t i = 0; i < numTbData; ++i) {
50,087,079✔
2357
    int32_t        affectedRows = 0;
25,081,734✔
2358
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
25,081,734✔
2359

2360
    if (hasBlob) {
25,081,699✔
2361
      code = vnodeSubmitBlobData(pVnode, pTbData);
1✔
2362
      if (code) {
1!
2363
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2364
               __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2365
        return code;
×
2366
      }
2367
    }
2368

2369
    code = tsdbInsertTableData(pVnode->pTsdb, version, pTbData, &affectedRows);
25,081,699✔
2370
    if (code) {
25,081,900!
2371
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2372
             __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2373
      return code;
×
2374
    }
2375

2376
    code = metaUpdateChangeTimeWithLock(pVnode->pMeta, pTbData->uid, pTbData->ctimeMs);
25,081,900✔
2377
    if (code) {
25,081,825!
2378
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2379
             __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2380
      return code;
×
2381
    }
2382
    pResponse->affectedRows += affectedRows;
25,081,842✔
2383
  }
2384

2385
  vDebug("vgId:%d, handle data write done, version:%" PRId64 ", affectedRows:%d", TD_VID(pVnode), version,
25,005,345✔
2386
         pResponse->affectedRows);
2387
  return code;
25,005,227✔
2388
}
2389

2390
static int32_t vnodeScanColumnData(SVnode *pVnode, SSubmitTbData *pTbData, TSKEY minKey, TSKEY maxKey) {
1,146✔
2391
  int32_t code = 0;
1,146✔
2392

2393
  int32_t   numCols = taosArrayGetSize(pTbData->aCol);
1,146✔
2394
  SColData *aColData = (SColData *)TARRAY_DATA(pTbData->aCol);
1,146✔
2395

2396
  if (numCols <= 0) {
1,146!
2397
    code = TSDB_CODE_INVALID_MSG;
×
2398
    vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " numCols:%d", TD_VID(pVnode), __func__,
×
2399
           __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, numCols);
2400
    return code;
×
2401
  }
2402

2403
  if (aColData[0].cid != PRIMARYKEY_TIMESTAMP_COL_ID || aColData[0].type != TSDB_DATA_TYPE_TIMESTAMP ||
1,146!
2404
      aColData[0].nVal <= 0) {
1,146!
2405
    code = TSDB_CODE_INVALID_MSG;
×
2406
    vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64
×
2407
           " first column is not primary key timestamp, cid:%d type:%d nVal:%d",
2408
           TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, aColData[0].cid,
2409
           aColData[0].type, aColData[0].nVal);
2410
    return code;
×
2411
  }
2412

2413
  for (int32_t i = 1; i < numCols; ++i) {
7,238✔
2414
    if (aColData[i].nVal != aColData[0].nVal) {
6,092!
2415
      code = TSDB_CODE_INVALID_MSG;
×
2416
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64
×
2417
             " column cid:%d type:%d nVal:%d is not equal to primary key timestamp nVal:%d",
2418
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid,
2419
             aColData[i].cid, aColData[i].type, aColData[i].nVal, aColData[0].nVal);
2420
      return code;
×
2421
    }
2422
  }
2423

2424
  SRowKey *pLastKey = NULL;
1,146✔
2425
  SRowKey  lastKey = {0};
1,146✔
2426
  for (int32_t i = 0; i < aColData[0].nVal; ++i) {
51,419✔
2427
    SRowKey key = {0};
50,277✔
2428

2429
    tColDataArrGetRowKey(aColData, numCols, i, &key);
50,277✔
2430

2431
    if (key.ts < minKey || key.ts > maxKey) {
50,277!
2432
      code = TSDB_CODE_INVALID_MSG;
×
2433
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] key:%" PRId64
×
2434
             " is out of range [%" PRId64 ", %" PRId64 "]",
2435
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, i, key.ts,
2436
             minKey, maxKey);
2437
      return code;
×
2438
    }
2439

2440
    if (pLastKey && tRowKeyCompare(pLastKey, &key) >= 0) {
50,277✔
2441
      code = TSDB_CODE_INVALID_MSG;
15✔
2442
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] key:%" PRId64
15!
2443
             " is not in order, lastKey:%" PRId64,
2444
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, i, key.ts,
2445
             pLastKey->ts);
2446
      return code;
×
2447
    } else if (pLastKey == NULL) {
50,273✔
2448
      pLastKey = &lastKey;
1,146✔
2449
    }
2450

2451
    *pLastKey = key;
50,273✔
2452
  }
2453

2454
  return code;
1,142✔
2455
}
2456

2457
static int32_t vnodeScanSubmitRowData(SVnode *pVnode, SSubmitTbData *pTbData, TSKEY minKey, TSKEY maxKey) {
25,080,389✔
2458
  int32_t code = 0;
25,080,389✔
2459

2460
  int32_t numRows = taosArrayGetSize(pTbData->aRowP);
25,080,389✔
2461
  SRow  **aRow = (SRow **)TARRAY_DATA(pTbData->aRowP);
25,080,597✔
2462

2463
  if (numRows <= 0) {
25,080,597!
2464
    code = TSDB_CODE_INVALID_MSG;
×
2465
    vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " numRows:%d", TD_VID(pVnode), __func__,
×
2466
           __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, numRows);
2467
    return code;
×
2468
  }
2469

2470
  SRowKey *pLastKey = NULL;
25,080,628✔
2471
  SRowKey  lastKey = {0};
25,080,628✔
2472
  for (int32_t i = 0; i < numRows; ++i) {
1,837,510,650✔
2473
    SRow *pRow = aRow[i];
1,812,409,726✔
2474
    if (pRow->sver != pTbData->sver) {
1,812,409,726!
2475
      code = TSDB_CODE_INVALID_MSG;
×
2476
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] sver:%d pTbData->sver:%d",
×
2477
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, i, pRow->sver,
2478
             pTbData->sver);
2479
      return code;
×
2480
    }
2481

2482
    SRowKey key = {0};
1,812,409,726✔
2483
    tRowGetKey(pRow, &key);
2,147,483,647✔
2484
    if (key.ts < minKey || key.ts > maxKey) {
1,812,411,531!
2485
      code = TSDB_CODE_INVALID_MSG;
×
2486
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] key:%" PRId64
×
2487
             " is out of range [%" PRId64 ", %" PRId64 "]",
2488
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, i, key.ts,
2489
             minKey, maxKey);
2490
      return code;
×
2491
    }
2492

2493
    if (pLastKey && tRowKeyCompare(pLastKey, &key) >= 0) {
1,812,471,361✔
2494
      code = TSDB_CODE_INVALID_MSG;
40,448✔
2495
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] key:%" PRId64
40,448!
2496
             " is not in order, lastKey:%" PRId64,
2497
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, i, key.ts,
2498
             pLastKey->ts);
2499
      return code;
×
2500
    } else if (pLastKey == NULL) {
1,812,430,022✔
2501
      pLastKey = &lastKey;
25,080,402✔
2502
    }
2503

2504
    *pLastKey = key;
1,812,430,022✔
2505
  }
2506

2507
  return code;
25,100,924✔
2508
}
2509

2510
static int32_t vnodeScanSubmitReq(SVnode *pVnode, int64_t version, SSubmitReq2 *pRequest, SSubmitRsp2 *pResponse) {
25,004,947✔
2511
  int32_t code = TSDB_CODE_SUCCESS;
25,004,947✔
2512
  int32_t numTbData = taosArrayGetSize(pRequest->aSubmitTbData);
25,004,947✔
2513

2514
  TSKEY now = taosGetTimestamp(pVnode->config.tsdbCfg.precision);
25,005,040✔
2515
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * pVnode->config.tsdbCfg.keep2;
25,005,212✔
2516
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
25,005,212✔
2517
  for (int32_t i = 0; i < numTbData; i++) {
50,086,914✔
2518
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
25,081,673✔
2519

2520
    if (pTbData->pCreateTbReq && pTbData->pCreateTbReq->uid == 0) {
25,081,657!
2521
      code = TSDB_CODE_INVALID_MSG;
×
2522
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2523
             tstrerror(code), version);
2524
      return code;
×
2525
    }
2526

2527
    if (pTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
25,081,657✔
2528
      code = vnodeScanColumnData(pVnode, pTbData, minKey, maxKey);
1,146✔
2529
      if (code) {
1,146!
2530
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2531
               __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2532
        return code;
×
2533
      }
2534
    } else {
2535
      code = vnodeScanSubmitRowData(pVnode, pTbData, minKey, maxKey);
25,080,511✔
2536
      if (code) {
25,080,551!
2537
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2538
               __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2539
        return code;
×
2540
      }
2541
    }
2542
  }
2543

2544
  return code;
25,005,241✔
2545
}
2546

2547
static int32_t vnodeProcessSubmitReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
25,005,008✔
2548
                                     SRpcMsg *pOriginalMsg) {
2549
  int32_t code = 0;
25,005,008✔
2550
  int32_t lino = 0;
25,005,008✔
2551
  terrno = 0;
25,005,008✔
2552

2553
  SSubmitReq2 *pSubmitReq = &(SSubmitReq2){0};
25,005,246✔
2554
  SSubmitRsp2 *pSubmitRsp = &(SSubmitRsp2){0};
25,005,246✔
2555
  int32_t      ret;
2556
  SEncoder     ec = {0};
25,005,246✔
2557

2558
  pRsp->code = TSDB_CODE_SUCCESS;
25,005,246✔
2559

2560
  void           *pAllocMsg = NULL;
25,005,246✔
2561
  SSubmitReq2Msg *pMsg = (SSubmitReq2Msg *)pReq;
25,005,246✔
2562
  SDecoder        dc = {0};
25,005,246✔
2563
  if (0 == taosHton64(pMsg->version)) {
25,005,246!
2564
    code = vnodeSubmitReqConvertToSubmitReq2(pVnode, (SSubmitReq *)pMsg, pSubmitReq);
×
2565
    if (TSDB_CODE_SUCCESS == code) {
×
2566
      code = vnodeRebuildSubmitReqMsg(pSubmitReq, &pReq);
×
2567
    }
2568
    if (TSDB_CODE_SUCCESS == code) {
×
2569
      pAllocMsg = pReq;
×
2570
    }
2571
    TSDB_CHECK_CODE(code, lino, _exit);
×
2572
  } else {
2573
    // decode
2574
    pReq = POINTER_SHIFT(pReq, sizeof(SSubmitReq2Msg));
25,005,010✔
2575
    len -= sizeof(SSubmitReq2Msg);
25,005,010✔
2576

2577
    tDecoderInit(&dc, pReq, len);
25,005,010✔
2578
    if (tDecodeSubmitReq(&dc, pSubmitReq, NULL) < 0) {
25,004,963!
2579
      code = TSDB_CODE_INVALID_MSG;
×
2580
      TSDB_CHECK_CODE(code, lino, _exit);
×
2581
    }
2582
  }
2583

2584
  // Scan the request
2585
  code = vnodeScanSubmitReq(pVnode, ver, pSubmitReq, pSubmitRsp);
25,005,203✔
2586
  if (code) {
25,005,056!
2587
    vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2588
           tstrerror(code), ver);
2589
    TSDB_CHECK_CODE(code, lino, _exit);
×
2590
  }
2591

2592
  vGDebug(pOriginalMsg ? &pOriginalMsg->info.traceId : NULL, "vgId:%d, index:%" PRId64 ", submit block, rows:%d",
25,005,056!
2593
          TD_VID(pVnode), ver, (int32_t)taosArrayGetSize(pSubmitReq->aSubmitTbData));
2594

2595
  // Handle auto create table
2596
  code = vnodeHandleAutoCreateTable(pVnode, ver, pSubmitReq, pSubmitRsp);
25,005,065✔
2597
  if (code) {
25,004,851✔
2598
    vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
140!
2599
           tstrerror(code), ver);
2600
    TSDB_CHECK_CODE(code, lino, _exit);
140!
2601
  }
2602

2603
  // Handle data write
2604
  code = vnodeHandleDataWrite(pVnode, ver, pSubmitReq, pSubmitRsp);
25,004,711✔
2605
  if (code) {
25,005,164✔
2606
    vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
5!
2607
           tstrerror(code), ver);
2608
    TSDB_CHECK_CODE(code, lino, _exit);
5!
2609
  }
2610

2611
_exit:
25,005,159✔
2612
  // message
2613
  pRsp->code = code;
25,005,304✔
2614
  tEncodeSize(tEncodeSSubmitRsp2, pSubmitRsp, pRsp->contLen, ret);
25,005,304!
2615
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
25,005,194✔
2616
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
25,005,358✔
2617
  if (tEncodeSSubmitRsp2(&ec, pSubmitRsp) < 0) {
25,005,385!
2618
    vError("vgId:%d, failed to encode submit response", TD_VID(pVnode));
×
2619
  }
2620
  tEncoderClear(&ec);
25,005,321✔
2621

2622
  // update statistics
2623
  (void)atomic_add_fetch_64(&pVnode->statis.nInsert, pSubmitRsp->affectedRows);
25,005,241✔
2624
  (void)atomic_add_fetch_64(&pVnode->statis.nInsertSuccess, pSubmitRsp->affectedRows);
25,005,415✔
2625
  (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsert, 1);
25,005,428✔
2626

2627
  // update metrics
2628
  METRICS_UPDATE(pVnode->writeMetrics.total_requests, METRIC_LEVEL_LOW, 1);
25,005,410!
2629
  METRICS_UPDATE(pVnode->writeMetrics.total_rows, METRIC_LEVEL_HIGH, pSubmitRsp->affectedRows);
25,005,410!
2630
  METRICS_UPDATE(pVnode->writeMetrics.total_bytes, METRIC_LEVEL_LOW, pMsg->header.contLen);
25,005,410!
2631

2632
  if (tsEnableMonitor && tsMonitorFqdn[0] != 0 && tsMonitorPort != 0 && pSubmitRsp->affectedRows > 0 &&
25,005,410!
2633
      strlen(pOriginalMsg->info.conn.user) > 0 && tsInsertCounter != NULL) {
×
2634
    const char *sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT_AFFECTED_ROWS,
×
2635
                                   pVnode->monitor.strClusterId,
×
2636
                                   pVnode->monitor.strDnodeId,
×
2637
                                   tsLocalEp,
2638
                                   pVnode->monitor.strVgId,
×
2639
                                   pOriginalMsg->info.conn.user,
×
2640
                                   "Success"};
2641
    int         tv = taos_counter_add(tsInsertCounter, pSubmitRsp->affectedRows, sample_labels);
×
2642
  }
2643

2644
  if (code == 0) {
25,005,410✔
2645
    (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
25,005,128✔
2646
    code = tdProcessRSmaSubmit(pVnode->pSma, ver, pSubmitReq, pReq, len);
25,005,229✔
2647
  }
2648

2649
  // clear
2650
  tDestroySubmitReq(pSubmitReq, 0 == taosHton64(pMsg->version) ? TSDB_MSG_FLG_CMPT : TSDB_MSG_FLG_DECODE);
25,005,496!
2651
  tDestroySSubmitRsp2(pSubmitRsp, TSDB_MSG_FLG_ENCODE);
25,005,376✔
2652

2653
  if (code) {
25,005,208✔
2654
    terrno = code;
145✔
2655
    if (code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
145✔
2656
      vInfo("vgId:%d, failed to process submit request since %s, lino:%d, version:%" PRId64, TD_VID(pVnode),
132!
2657
            tstrerror(code), lino, ver);
2658

2659
    } else {
2660
      vError("vgId:%d, failed to process submit request since %s, lino:%d, version:%" PRId64, TD_VID(pVnode),
13!
2661
             tstrerror(code), lino, ver);
2662
    }
2663
  }
2664

2665
  taosMemoryFree(pAllocMsg);
25,005,102!
2666
  tDecoderClear(&dc);
25,005,152✔
2667

2668
  return code;
25,005,390✔
2669
}
2670

2671
static int32_t vnodeConsolidateAlterHashRange(SVnode *pVnode, int64_t ver) {
86✔
2672
  int32_t code = TSDB_CODE_SUCCESS;
86✔
2673

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

2677
  // TODO: trim meta of tables from TDB per hash range [pVnode->config.hashBegin, pVnode->config.hashEnd]
2678
  code = metaTrimTables(pVnode->pMeta, ver);
86✔
2679

2680
  return code;
86✔
2681
}
2682

2683
static int32_t vnodeProcessAlterConfirmReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
8,436✔
2684
  vInfo("vgId:%d, vnode handle msgType:alter-confirm, alter confirm msg is processed", TD_VID(pVnode));
8,436!
2685
  int32_t code = TSDB_CODE_SUCCESS;
8,436✔
2686
  if (!pVnode->config.hashChange) {
8,436✔
2687
    goto _exit;
8,350✔
2688
  }
2689

2690
  code = vnodeConsolidateAlterHashRange(pVnode, ver);
86✔
2691
  if (code < 0) {
86!
2692
    vError("vgId:%d, failed to consolidate alter hashrange since %s. version:%" PRId64, TD_VID(pVnode), terrstr(), ver);
×
2693
    goto _exit;
×
2694
  }
2695
  pVnode->config.hashChange = false;
86✔
2696

2697
_exit:
8,436✔
2698
  pRsp->msgType = TDMT_VND_ALTER_CONFIRM_RSP;
8,436✔
2699
  pRsp->code = code;
8,436✔
2700
  pRsp->pCont = NULL;
8,436✔
2701
  pRsp->contLen = 0;
8,436✔
2702

2703
  return code;
8,436✔
2704
}
2705

2706
extern int32_t tsdbDisableAndCancelAllBgTask(STsdb *pTsdb);
2707
extern void    tsdbEnableBgTask(STsdb *pTsdb);
2708

2709
static int32_t vnodeProcessAlterConfigReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
1,369✔
2710
  bool walChanged = false;
1,369✔
2711
  bool tsdbChanged = false;
1,369✔
2712

2713
  SAlterVnodeConfigReq req = {0};
1,369✔
2714
  if (tDeserializeSAlterVnodeConfigReq(pReq, len, &req) != 0) {
1,369!
2715
    terrno = TSDB_CODE_INVALID_MSG;
×
2716
    return TSDB_CODE_INVALID_MSG;
×
2717
  }
2718

2719
  vInfo("vgId:%d, start to alter vnode config, page:%d pageSize:%d buffer:%d szPage:%d szBuf:%" PRIu64
1,371!
2720
        " cacheLast:%d cacheLastSize:%d days:%d keep0:%d keep1:%d keep2:%d keepTimeOffset:%d ssKeepLocal:%d "
2721
        "ssCompact:%d fsync:%d level:%d "
2722
        "walRetentionPeriod:%d walRetentionSize:%d",
2723
        TD_VID(pVnode), req.pages, req.pageSize, req.buffer, req.pageSize * 1024, (uint64_t)req.buffer * 1024 * 1024,
2724
        req.cacheLast, req.cacheLastSize, req.daysPerFile, req.daysToKeep0, req.daysToKeep1, req.daysToKeep2,
2725
        req.keepTimeOffset, req.ssKeepLocal, req.ssCompact, req.walFsyncPeriod, req.walLevel, req.walRetentionPeriod,
2726
        req.walRetentionSize);
2727

2728
  if (pVnode->config.cacheLastSize != req.cacheLastSize) {
1,371✔
2729
    pVnode->config.cacheLastSize = req.cacheLastSize;
812✔
2730
    tsdbCacheSetCapacity(pVnode, (size_t)pVnode->config.cacheLastSize * 1024 * 1024);
812✔
2731
  }
2732

2733
  if (pVnode->config.szBuf != req.buffer * 1024LL * 1024LL) {
1,371✔
2734
    vInfo("vgId:%d, vnode buffer is changed from %" PRId64 " to %" PRId64, TD_VID(pVnode), pVnode->config.szBuf,
14!
2735
          (uint64_t)(req.buffer * 1024LL * 1024LL));
2736
    pVnode->config.szBuf = req.buffer * 1024LL * 1024LL;
14✔
2737
  }
2738

2739
  if (pVnode->config.szCache != req.pages) {
1,371✔
2740
    if ((terrno = metaAlterCache(pVnode->pMeta, req.pages)) < 0) {
6!
2741
      vError("vgId:%d, failed to change vnode pages from %d to %d failed since %s", TD_VID(pVnode),
×
2742
             pVnode->config.szCache, req.pages, tstrerror(terrno));
2743
      return terrno;
×
2744
    } else {
2745
      vInfo("vgId:%d, vnode pages is changed from %d to %d", TD_VID(pVnode), pVnode->config.szCache, req.pages);
6!
2746
      pVnode->config.szCache = req.pages;
6✔
2747
    }
2748
  }
2749

2750
  if (pVnode->config.cacheLast != req.cacheLast) {
1,371✔
2751
    pVnode->config.cacheLast = req.cacheLast;
993✔
2752
  }
2753

2754
  if (pVnode->config.walCfg.fsyncPeriod != req.walFsyncPeriod) {
1,371✔
2755
    pVnode->config.walCfg.fsyncPeriod = req.walFsyncPeriod;
72✔
2756
    walChanged = true;
72✔
2757
  }
2758

2759
  if (pVnode->config.walCfg.level != req.walLevel) {
1,371✔
2760
    if (pVnode->config.walCfg.level == 0) {
50!
2761
      pVnode->config.walCfg.clearFiles = 1;
×
2762
    }
2763
    pVnode->config.walCfg.level = req.walLevel;
50✔
2764
    walChanged = true;
50✔
2765
  }
2766

2767
  if (pVnode->config.walCfg.retentionPeriod != req.walRetentionPeriod) {
1,371✔
2768
    pVnode->config.walCfg.retentionPeriod = req.walRetentionPeriod;
124✔
2769
    walChanged = true;
124✔
2770
  }
2771

2772
  if (pVnode->config.walCfg.retentionSize != req.walRetentionSize) {
1,371✔
2773
    pVnode->config.walCfg.retentionSize = req.walRetentionSize;
2✔
2774
    walChanged = true;
2✔
2775
  }
2776

2777
  if (pVnode->config.tsdbCfg.keep0 != req.daysToKeep0) {
1,371✔
2778
    pVnode->config.tsdbCfg.keep0 = req.daysToKeep0;
68✔
2779
    if (!VND_IS_RSMA(pVnode)) {
68!
2780
      tsdbChanged = true;
68✔
2781
    }
2782
  }
2783

2784
  if (pVnode->config.tsdbCfg.keep1 != req.daysToKeep1) {
1,371✔
2785
    pVnode->config.tsdbCfg.keep1 = req.daysToKeep1;
68✔
2786
    if (!VND_IS_RSMA(pVnode)) {
68!
2787
      tsdbChanged = true;
68✔
2788
    }
2789
  }
2790

2791
  if (pVnode->config.tsdbCfg.keep2 != req.daysToKeep2) {
1,371✔
2792
    pVnode->config.tsdbCfg.keep2 = req.daysToKeep2;
68✔
2793
    if (!VND_IS_RSMA(pVnode)) {
68!
2794
      tsdbChanged = true;
68✔
2795
    }
2796
  }
2797

2798
  if (pVnode->config.tsdbCfg.keepTimeOffset != req.keepTimeOffset) {
1,371✔
2799
    pVnode->config.tsdbCfg.keepTimeOffset = req.keepTimeOffset;
10✔
2800
    if (!VND_IS_RSMA(pVnode)) {
10!
2801
      tsdbChanged = true;
10✔
2802
    }
2803
  }
2804

2805
  if (req.sttTrigger != -1 && req.sttTrigger != pVnode->config.sttTrigger) {
1,371!
2806
    if (req.sttTrigger > 1 && pVnode->config.sttTrigger > 1) {
4!
2807
      pVnode->config.sttTrigger = req.sttTrigger;
2✔
2808
    } else {
2809
      vnodeAWait(&pVnode->commitTask);
2✔
2810

2811
      int32_t ret = tsdbDisableAndCancelAllBgTask(pVnode->pTsdb);
2✔
2812
      if (ret != 0) {
2!
2813
        vError("vgId:%d, failed to disable bg task since %s", TD_VID(pVnode), tstrerror(ERRNO));
×
2814
      }
2815

2816
      pVnode->config.sttTrigger = req.sttTrigger;
2✔
2817
      tsdbEnableBgTask(pVnode->pTsdb);
2✔
2818
    }
2819
  }
2820

2821
  if (req.minRows != -1 && req.minRows != pVnode->config.tsdbCfg.minRows) {
1,371!
2822
    pVnode->config.tsdbCfg.minRows = req.minRows;
2✔
2823
  }
2824

2825
  if (req.ssKeepLocal != -1 && req.ssKeepLocal != pVnode->config.ssKeepLocal) {
1,371!
2826
    pVnode->config.ssKeepLocal = req.ssKeepLocal;
2✔
2827
  }
2828
  if (req.ssCompact != -1 && req.ssCompact != pVnode->config.ssCompact) {
1,371!
2829
    pVnode->config.ssCompact = req.ssCompact;
2✔
2830
  }
2831

2832
  if (walChanged) {
1,371✔
2833
    if (walAlter(pVnode->pWal, &pVnode->config.walCfg) != 0) {
244!
2834
      vError("vgId:%d, failed to alter wal config since %s", TD_VID(pVnode), tstrerror(ERRNO));
×
2835
    }
2836
  }
2837

2838
  if (tsdbChanged) {
1,371✔
2839
    tsdbSetKeepCfg(pVnode->pTsdb, &pVnode->config.tsdbCfg);
80✔
2840
  }
2841

2842
  return 0;
1,371✔
2843
}
2844

2845
static int32_t vnodeProcessBatchDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
2846
  SBatchDeleteReq deleteReq;
2847
  SDecoder        decoder;
2848
  tDecoderInit(&decoder, pReq, len);
×
2849
  if (tDecodeSBatchDeleteReq(&decoder, &deleteReq) < 0) {
×
2850
    tDecoderClear(&decoder);
×
2851
    return terrno = TSDB_CODE_INVALID_MSG;
×
2852
  }
2853

2854
  SMetaReader mr = {0};
×
2855
  metaReaderDoInit(&mr, pVnode->pMeta, META_READER_NOLOCK);
×
2856
  STsdb *pTsdb = pVnode->pTsdb;
×
2857

2858
  if (deleteReq.level) {
×
2859
    pTsdb = deleteReq.level == 1 ? VND_RSMA1(pVnode) : VND_RSMA2(pVnode);
×
2860
  }
2861

2862
  int32_t sz = taosArrayGetSize(deleteReq.deleteReqs);
×
2863
  for (int32_t i = 0; i < sz; i++) {
×
2864
    SSingleDeleteReq *pOneReq = taosArrayGet(deleteReq.deleteReqs, i);
×
2865
    char             *name = pOneReq->tbname;
×
2866
    if (metaGetTableEntryByName(&mr, name) < 0) {
×
2867
      vDebug("vgId:%d, stream delete msg, skip since no table: %s", pVnode->config.vgId, name);
×
2868
      continue;
×
2869
    }
2870

2871
    int64_t uid = mr.me.uid;
×
2872

2873
    int32_t code = tsdbDeleteTableData(pTsdb, ver, deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
×
2874
    if (code < 0) {
×
2875
      terrno = code;
×
2876
      vError("vgId:%d, delete error since %s, suid:%" PRId64 ", uid:%" PRId64 ", start ts:%" PRId64 ", end ts:%" PRId64,
×
2877
             TD_VID(pVnode), terrstr(), deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
2878
    }
2879

2880
    if (deleteReq.level == 0) {
×
2881
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, deleteReq.ctimeMs);
×
2882
      if (code < 0) {
×
2883
        terrno = code;
×
2884
        vError("vgId:%d, update change time error since %s, suid:%" PRId64 ", uid:%" PRId64 ", start ts:%" PRId64
×
2885
               ", end ts:%" PRId64,
2886
               TD_VID(pVnode), terrstr(), deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
2887
      }
2888
    }
2889
    tDecoderClear(&mr.coder);
×
2890
  }
2891
  metaReaderClear(&mr);
×
2892
  taosArrayDestroy(deleteReq.deleteReqs);
×
2893
  return 0;
×
2894
}
2895

2896
static int32_t vnodeProcessDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
143,702✔
2897
                                     SRpcMsg *pOriginalMsg) {
2898
  int32_t     code = 0;
143,702✔
2899
  SDecoder   *pCoder = &(SDecoder){0};
143,702✔
2900
  SDeleteRes *pRes = &(SDeleteRes){0};
143,702✔
2901

2902
  pRsp->msgType = TDMT_VND_DELETE_RSP;
143,702✔
2903
  pRsp->pCont = NULL;
143,702✔
2904
  pRsp->contLen = 0;
143,702✔
2905
  pRsp->code = TSDB_CODE_SUCCESS;
143,702✔
2906

2907
  pRes->uidList = taosArrayInit(0, sizeof(tb_uid_t));
143,702✔
2908
  if (pRes->uidList == NULL) {
143,705!
2909
    code = terrno;
×
2910
    goto _err;
×
2911
  }
2912

2913
  tDecoderInit(pCoder, pReq, len);
143,705✔
2914
  code = tDecodeDeleteRes(pCoder, pRes);
143,701✔
2915
  if (code) goto _err;
143,716!
2916

2917
  if (pRes->affectedRows > 0) {
143,716✔
2918
    for (int32_t iUid = 0; iUid < taosArrayGetSize(pRes->uidList); iUid++) {
278,372✔
2919
      uint64_t uid = *(uint64_t *)taosArrayGet(pRes->uidList, iUid);
143,416✔
2920
      code = tsdbDeleteTableData(pVnode->pTsdb, ver, pRes->suid, uid, pRes->skey, pRes->ekey);
143,416✔
2921
      if (code) goto _err;
143,413!
2922
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, pRes->ctimeMs);
143,413✔
2923
      if (code) goto _err;
143,410!
2924
    }
2925
  }
2926

2927
  code = tdProcessRSmaDelete(pVnode->pSma, ver, pRes, pReq, len);
143,707✔
2928

2929
  tDecoderClear(pCoder);
143,713✔
2930
  taosArrayDestroy(pRes->uidList);
143,717✔
2931

2932
  SVDeleteRsp rsp = {.affectedRows = pRes->affectedRows};
143,714✔
2933
  int32_t     ret = 0;
143,714✔
2934
  tEncodeSize(tEncodeSVDeleteRsp, &rsp, pRsp->contLen, ret);
143,714!
2935
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
143,699✔
2936
  SEncoder ec = {0};
143,712✔
2937
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
143,712✔
2938
  code = tEncodeSVDeleteRsp(&ec, &rsp);
143,715✔
2939
  if (code) goto _err;
143,716!
2940
  tEncoderClear(&ec);
143,716✔
2941
  return code;
143,718✔
2942

2943
_err:
×
2944
  /*
2945
  if(code == TSDB_CODE_SUCCESS){
2946
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_DELETE, pVnode->monitor.strClusterId,
2947
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2948
                                        pOriginalMsg->info.conn.user, "Success"};
2949
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2950
  }
2951
  else{
2952
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_DELETE, pVnode->monitor.strClusterId,
2953
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2954
                                        pOriginalMsg->info.conn.user, "Failed"};
2955
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2956
  }
2957
  */
2958

2959
  return code;
×
2960
}
2961
static int32_t vnodeProcessCreateIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
2,839✔
2962
  SVCreateStbReq req = {0};
2,839✔
2963
  SDecoder       dc = {0};
2,839✔
2964
  int32_t        code = 0;
2,839✔
2965

2966
  pRsp->msgType = TDMT_VND_CREATE_INDEX_RSP;
2,839✔
2967
  pRsp->code = TSDB_CODE_SUCCESS;
2,839✔
2968
  pRsp->pCont = NULL;
2,839✔
2969
  pRsp->contLen = 0;
2,839✔
2970

2971
  tDecoderInit(&dc, pReq, len);
2,839✔
2972
  // decode req
2973
  if (tDecodeSVCreateStbReq(&dc, &req) < 0) {
2,839!
2974
    tDecoderClear(&dc);
×
2975
    return terrno = TSDB_CODE_INVALID_MSG;
×
2976
  }
2977

2978
  code = metaAddIndexToSuperTable(pVnode->pMeta, ver, &req);
2,839✔
2979
  if (code) {
2,839!
2980
    pRsp->code = code;
×
2981
    goto _err;
×
2982
  }
2983
  tDecoderClear(&dc);
2,839✔
2984
  return 0;
2,839✔
2985

2986
_err:
×
2987
  tDecoderClear(&dc);
×
2988
  return code;
×
2989
}
2990
static int32_t vnodeProcessDropIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
6,398✔
2991
  SDropIndexReq req = {0};
6,398✔
2992
  int32_t       code = 0;
6,398✔
2993
  pRsp->msgType = TDMT_VND_DROP_INDEX_RSP;
6,398✔
2994
  pRsp->code = TSDB_CODE_SUCCESS;
6,398✔
2995
  pRsp->pCont = NULL;
6,398✔
2996
  pRsp->contLen = 0;
6,398✔
2997

2998
  if ((code = tDeserializeSDropIdxReq(pReq, len, &req))) {
6,398!
2999
    pRsp->code = code;
×
3000
    return code;
×
3001
  }
3002

3003
  code = metaDropIndexFromSuperTable(pVnode->pMeta, ver, &req);
6,398✔
3004
  if (code) {
6,398!
3005
    pRsp->code = code;
×
3006
    return code;
×
3007
  }
3008
  return TSDB_CODE_SUCCESS;
6,398✔
3009
}
3010

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

3013
static int32_t vnodeProcessCompactVnodeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
160✔
3014
  if (!pVnode->restored) {
160✔
3015
    vInfo("vgId:%d, ignore compact req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
24!
3016
    return 0;
24✔
3017
  }
3018
  return vnodeAsyncCompact(pVnode, ver, pReq, len, pRsp);
136✔
3019
}
3020

3021
static int32_t vnodeProcessConfigChangeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
3022
  if (syncCheckMember(pVnode->sync) != 0) {
×
3023
    vError("vgId:%d, failed to check member", TD_VID(pVnode));
×
3024
  }
3025

3026
  pRsp->msgType = TDMT_SYNC_CONFIG_CHANGE_RSP;
×
3027
  pRsp->code = TSDB_CODE_SUCCESS;
×
3028
  pRsp->pCont = NULL;
×
3029
  pRsp->contLen = 0;
×
3030

3031
  return 0;
×
3032
}
3033

3034
static int32_t vnodeCheckState(SVnode *pVnode) {
69✔
3035
  SSyncState syncState = syncGetState(pVnode->sync);
69✔
3036
  if (syncState.state != TAOS_SYNC_STATE_LEADER) {
69✔
3037
    return terrno = TSDB_CODE_SYN_NOT_LEADER;
40✔
3038
  }
3039
  return 0;
29✔
3040
}
3041

3042
static int32_t vnodeCheckToken(SVnode *pVnode, char *member0Token, char *member1Token) {
29✔
3043
  char token[TSDB_ARB_TOKEN_SIZE] = {0};
29✔
3044
  if (vnodeGetArbToken(pVnode, token) != 0) {
29!
3045
    return terrno = TSDB_CODE_NOT_FOUND;
×
3046
  }
3047

3048
  if (strncmp(token, member0Token, TSDB_ARB_TOKEN_SIZE) != 0 &&
29✔
3049
      strncmp(token, member1Token, TSDB_ARB_TOKEN_SIZE) != 0) {
21✔
3050
    return terrno = TSDB_CODE_MND_ARB_TOKEN_MISMATCH;
15✔
3051
  }
3052

3053
  terrno = TSDB_CODE_SUCCESS;
14✔
3054
  return 0;
14✔
3055
}
3056

3057
static int32_t vnodeCheckSyncd(SVnode *pVnode, char *member0Token, char *member1Token) {
7✔
3058
  int32_t code = vnodeCheckToken(pVnode, member0Token, member1Token);
7✔
3059
  if (code != 0) {
7!
3060
    return code;
×
3061
  }
3062

3063
  return syncCheckSynced(pVnode->sync);
7✔
3064
}
3065

3066
static int32_t vnodeProcessArbCheckSyncReq(SVnode *pVnode, void *pReq, int32_t len, SRpcMsg *pRsp) {
21✔
3067
  int32_t code = 0;
21✔
3068

3069
  if ((code = vnodeCheckState(pVnode)) != 0) {
21✔
3070
    vDebug("vgId:%d, failed to preprocess vnode-arb-check-sync request since %s", TD_VID(pVnode), tstrerror(code));
14!
3071
    return 0;
14✔
3072
  }
3073

3074
  SVArbCheckSyncReq syncReq = {0};
7✔
3075

3076
  code = tDeserializeSVArbCheckSyncReq(pReq, len, &syncReq);
7✔
3077
  if (code) {
7!
3078
    return code;
×
3079
  }
3080

3081
  vInfo("vgId:%d, start to process vnode-arb-check-sync req QID:0x%" PRIx64 ":0x%" PRIx64 ", seqNum:%" PRIx64
7!
3082
        ", arbToken:%s, member0Token:%s, member1Token:%s, "
3083
        "arbTerm:%" PRId64,
3084
        TD_VID(pVnode), pRsp->info.traceId.rootId, pRsp->info.traceId.msgId, pRsp->info.seqNum, syncReq.arbToken,
3085
        syncReq.member0Token, syncReq.member1Token, syncReq.arbTerm);
3086

3087
  pRsp->msgType = TDMT_VND_ARB_CHECK_SYNC_RSP;
7✔
3088
  pRsp->code = TSDB_CODE_SUCCESS;
7✔
3089
  pRsp->pCont = NULL;
7✔
3090
  pRsp->contLen = 0;
7✔
3091

3092
  SVArbCheckSyncRsp syncRsp = {0};
7✔
3093
  syncRsp.arbToken = syncReq.arbToken;
7✔
3094
  syncRsp.member0Token = syncReq.member0Token;
7✔
3095
  syncRsp.member1Token = syncReq.member1Token;
7✔
3096
  syncRsp.vgId = TD_VID(pVnode);
7✔
3097

3098
  if ((syncRsp.errCode = vnodeCheckSyncd(pVnode, syncReq.member0Token, syncReq.member1Token)) != 0) {
7!
3099
    vError("vgId:%d, failed to check assigned log syncd since %s", TD_VID(pVnode), tstrerror(syncRsp.errCode));
×
3100
  }
3101

3102
  if ((code = vnodeUpdateArbTerm(pVnode, syncReq.arbTerm)) != 0) {
7!
3103
    vError("vgId:%d, failed to update arb term since %s", TD_VID(pVnode), tstrerror(code));
×
3104
    goto _OVER;
×
3105
  }
3106

3107
  int32_t contLen = tSerializeSVArbCheckSyncRsp(NULL, 0, &syncRsp);
7✔
3108
  if (contLen <= 0) {
7!
3109
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
3110
    code = -1;
×
3111
    goto _OVER;
×
3112
  }
3113
  void *pHead = rpcMallocCont(contLen);
7✔
3114
  if (!pHead) {
7!
3115
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
3116
    code = -1;
×
3117
    goto _OVER;
×
3118
  }
3119

3120
  if (tSerializeSVArbCheckSyncRsp(pHead, contLen, &syncRsp) <= 0) {
7!
3121
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
3122
    rpcFreeCont(pHead);
×
3123
    code = -1;
×
3124
    goto _OVER;
×
3125
  }
3126

3127
  pRsp->pCont = pHead;
7✔
3128
  pRsp->contLen = contLen;
7✔
3129

3130
  vInfo(
7!
3131
      "vgId:%d, suceed to process vnode-arb-check-sync req rsp.code:%s, arbToken:%s, member0Token:%s, "
3132
      "member1Token:%s",
3133
      TD_VID(pVnode), tstrerror(syncRsp.errCode), syncRsp.arbToken, syncRsp.member0Token, syncRsp.member1Token);
3134

3135
  code = TSDB_CODE_SUCCESS;
7✔
3136

3137
_OVER:
7✔
3138
  if (code != 0) {
7!
3139
    vError("vgId:%d, failed to process arb check req rsp.code:%s since %s", TD_VID(pVnode), tstrerror(syncRsp.errCode),
×
3140
           tstrerror(code));
3141
  }
3142
  tFreeSVArbCheckSyncReq(&syncReq);
7✔
3143
  return code;
7✔
3144
}
3145

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

© 2026 Coveralls, Inc