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

taosdata / TDengine / #4541

19 Jul 2025 01:13AM UTC coverage: 56.753% (-1.6%) from 58.31%
#4541

push

travis-ci

web-flow
fix: subquery memleak (#32024)

124299 of 282344 branches covered (44.02%)

Branch coverage included in aggregate %.

181106 of 255787 relevant lines covered (70.8%)

24937406.43 hits per line

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

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

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

32
extern taos_counter_t *tsInsertCounter;
33

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

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

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

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

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

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

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

91
  // uid
92
  int64_t uid = metaGetTableEntryUidByName(pVnode->pMeta, name);
198,497✔
93
  if (uid == 0) {
198,728✔
94
    uid = tGenIdPI64();
191,844✔
95
  }
96
  taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), uid);
198,616✔
97

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

101
  tEndDecode(pCoder);
198,616✔
102

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

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

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

126
  if (tDecodeI32v(&dc, &nReqs) < 0) {
83,282!
127
    code = TSDB_CODE_INVALID_MSG;
×
128
    TSDB_CHECK_CODE(code, lino, _exit);
×
129
  }
130
  for (int32_t iReq = 0; iReq < nReqs; iReq++) {
201,910✔
131
    code = vnodePreprocessCreateTableReq(pVnode, &dc, btime, NULL);
118,663✔
132
    TSDB_CHECK_CODE(code, lino, _exit);
118,628!
133
  }
134

135
  tEndDecode(&dc);
83,247✔
136

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

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

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

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

163
  code = 0;
10,564✔
164

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

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

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

183
  SArray *tbUids = NULL;
115,005✔
184
  int64_t timestampMs = 0;
115,005✔
185

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

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

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

206
    ttlReq.nUids = taosArrayGetSize(tbUids);
115,011✔
207
    ttlReq.pTbUids = tbUids;
114,660✔
208
  }
209

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

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

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

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

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

236
  code = 0;
30✔
237

238
_exit:
114,706✔
239
  taosArrayDestroy(tbUids);
114,706✔
240

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

248
  return code;
114,939✔
249
}
250

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

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

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

270
  int64_t uid;
271
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
19,666,691✔
272
    code = vnodePreprocessCreateTableReq(pVnode, pCoder, btimeMs, &uid);
79,784✔
273
    TSDB_CHECK_CODE(code, lino, _exit);
79,951!
274
  }
275

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

282
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
19,666,611✔
283
    taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), uid);
79,944✔
284
    pCoder->pos += sizeof(int64_t);
79,944✔
285
  } else {
286
    if (tDecodeI64(pCoder, &submitTbData.uid) < 0) {
19,586,671!
287
      code = TSDB_CODE_INVALID_MSG;
×
288
      TSDB_CHECK_CODE(code, lino, _exit);
×
289
    }
290
  }
291

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

297
  // scan and check
298
  TSKEY now = btimeMs;
19,666,527✔
299
  if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_MICRO) {
19,666,527✔
300
    now *= 1000;
23,956✔
301
  } else if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_NANO) {
19,642,571✔
302
    now *= 1000000;
4,631✔
303
  }
304

305
  int32_t keep = pVnode->config.tsdbCfg.keep2;
19,666,527✔
306
  /*
307
  int32_t nlevel = tfsGetLevel(pVnode->pTfs);
308
  if (nlevel > 1 && tsSsEnabled) {
309
    if (nlevel == 3) {
310
      keep = pVnode->config.tsdbCfg.keep1;
311
    } else if (nlevel == 2) {
312
      keep = pVnode->config.tsdbCfg.keep0;
313
    }
314
  }
315
  */
316

317
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * keep;
19,666,527✔
318
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
19,666,527✔
319
  if (submitTbData.flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
19,666,527✔
320
    uint64_t nColData;
321
    if (tDecodeU64v(pCoder, &nColData) < 0) {
10,130!
322
      code = TSDB_CODE_INVALID_MSG;
×
323
      TSDB_CHECK_CODE(code, lino, _exit);
1!
324
    }
325

326
    SColData colData = {0};
10,130✔
327
    code = tDecodeColData(version, pCoder, &colData);
10,130✔
328
    if (code) {
10,131!
329
      code = TSDB_CODE_INVALID_MSG;
×
330
      TSDB_CHECK_CODE(code, lino, _exit);
×
331
    }
332

333
    if (colData.flag != HAS_VALUE) {
10,131!
334
      code = TSDB_CODE_INVALID_MSG;
×
335
      TSDB_CHECK_CODE(code, lino, _exit);
×
336
    }
337

338
    for (int32_t iRow = 0; iRow < colData.nVal; iRow++) {
9,971,307✔
339
      if (((TSKEY *)colData.pData)[iRow] < minKey || ((TSKEY *)colData.pData)[iRow] > maxKey) {
9,961,177!
340
        code = TSDB_CODE_TDB_TIMESTAMP_OUT_OF_RANGE;
×
341
        TSDB_CHECK_CODE(code, lino, _exit);
×
342
      }
343
    }
344

345
    for (uint64_t i = 1; i < nColData; i++) {
30,596✔
346
      code = tDecodeColData(version, pCoder, &colData);
20,468✔
347
      if (code) {
20,466!
348
        code = TSDB_CODE_INVALID_MSG;
×
349
        TSDB_CHECK_CODE(code, lino, _exit);
×
350
      }
351
    }
352
  } else {
353
    uint64_t nRow;
354
    if (tDecodeU64v(pCoder, &nRow) < 0) {
19,656,358!
355
      code = TSDB_CODE_INVALID_MSG;
×
356
      TSDB_CHECK_CODE(code, lino, _exit);
977!
357
    }
358

359
    for (int32_t iRow = 0; iRow < nRow; ++iRow) {
1,520,943,622✔
360
      SRow *pRow = (SRow *)(pCoder->data + pCoder->pos);
1,501,288,241✔
361
      pCoder->pos += pRow->len;
1,501,288,241✔
362
#ifndef NO_UNALIGNED_ACCESS
363
      if (pRow->ts < minKey || pRow->ts > maxKey) {
1,501,288,241✔
364
#else
365
      TSKEY ts = taosGetInt64Aligned(&pRow->ts);
366
      if (ts < minKey || ts > maxKey) {
367
#endif
368
        code = TSDB_CODE_TDB_TIMESTAMP_OUT_OF_RANGE;
9,388✔
369
        TSDB_CHECK_CODE(code, lino, _exit);
9,388✔
370
      }
371

372
      // Check pRow->sver
373
      if (pRow->sver != submitTbData.sver) {
1,501,287,264!
374
        code = TSDB_CODE_INVALID_DATA_FMT;
×
375
        TSDB_CHECK_CODE(code, lino, _exit);
×
376
      }
377
    }
378
  }
379

380
  if (!tDecodeIsEnd(pCoder)) {
19,665,509!
381
    taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), ctimeMs);
19,665,668✔
382
    pCoder->pos += sizeof(int64_t);
19,665,668✔
383
  }
384

385
  tEndDecode(pCoder);
19,665,509✔
386

387
_exit:
19,665,842✔
388
  if (code) {
19,665,842✔
389
    vError("vgId:%d, %s:%d failed to vnodePreProcessSubmitTbData submit request since %s", TD_VID(pVnode), __func__,
978!
390
           lino, tstrerror(code));
391
  }
392
  return code;
19,665,845✔
393
}
394
static int32_t vnodePreProcessSubmitMsg(SVnode *pVnode, SRpcMsg *pMsg) {
18,941,853✔
395
  int32_t code = 0;
18,941,853✔
396
  int32_t lino = 0;
18,941,853✔
397

398
  if (tsBypassFlag & TSDB_BYPASS_RA_RPC_RECV_SUBMIT) {
18,941,853!
399
    return TSDB_CODE_MSG_PREPROCESSED;
×
400
  }
401

402
  SDecoder *pCoder = &(SDecoder){0};
18,941,853✔
403

404
  if (taosHton64(((SSubmitReq2Msg *)pMsg->pCont)->version) != 1) {
18,941,853!
405
    code = TSDB_CODE_INVALID_MSG;
×
406
    TSDB_CHECK_CODE(code, lino, _exit);
×
407
  }
408

409
  tDecoderInit(pCoder, (uint8_t *)pMsg->pCont + sizeof(SSubmitReq2Msg), pMsg->contLen - sizeof(SSubmitReq2Msg));
18,941,896✔
410

411
  if (tStartDecode(pCoder) < 0) {
18,941,831!
412
    code = TSDB_CODE_INVALID_MSG;
×
413
    TSDB_CHECK_CODE(code, lino, _exit);
×
414
  }
415

416
  uint64_t nSubmitTbData;
417
  if (tDecodeU64v(pCoder, &nSubmitTbData) < 0) {
18,941,869!
418
    code = TSDB_CODE_INVALID_MSG;
×
419
    TSDB_CHECK_CODE(code, lino, _exit);
×
420
  }
421

422
  int64_t btimeMs = taosGetTimestampMs();
18,941,937✔
423
  int64_t ctimeMs = btimeMs;
18,941,937✔
424
  for (int32_t i = 0; i < nSubmitTbData; i++) {
38,606,805✔
425
    code = vnodePreProcessSubmitTbData(pVnode, pCoder, btimeMs, ctimeMs);
19,665,970✔
426
    TSDB_CHECK_CODE(code, lino, _exit);
19,665,849✔
427
  }
428

429
  tEndDecode(pCoder);
18,940,835✔
430

431
_exit:
18,941,862✔
432
  tDecoderClear(pCoder);
18,941,862✔
433
  if (code) {
18,941,871✔
434
    vError("vgId:%d, %s:%d failed to preprocess submit request since %s, msg type:%s", TD_VID(pVnode), __func__, lino,
981!
435
           tstrerror(code), TMSG_INFO(pMsg->msgType));
436
  }
437
  return code;
18,941,859✔
438
}
439

440
static int32_t vnodePreProcessDeleteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
116,815✔
441
  int32_t code = 0;
116,815✔
442

443
  int32_t    size;
444
  int32_t    ret;
445
  uint8_t   *pCont;
446
  SEncoder  *pCoder = &(SEncoder){0};
116,815✔
447
  SDeleteRes res = {0};
116,815✔
448

449
  SReadHandle handle = {0};
116,815✔
450
  handle.vnode = pVnode;
116,815✔
451
  handle.pMsgCb = &pVnode->msgCb;
116,815✔
452
  handle.skipRollup = 1;
116,815✔
453
  initStorageAPI(&handle.api);
116,815✔
454

455
  code = qWorkerProcessDeleteMsg(&handle, pVnode->pQuery, pMsg, &res);
116,822✔
456
  if (code) goto _exit;
116,821!
457

458
  res.ctimeMs = taosGetTimestampMs();
116,823✔
459
  // malloc and encode
460
  tEncodeSize(tEncodeDeleteRes, &res, size, ret);
116,823!
461
  pCont = rpcMallocCont(size + sizeof(SMsgHead));
116,819✔
462

463
  ((SMsgHead *)pCont)->contLen = size + sizeof(SMsgHead);
116,823✔
464
  ((SMsgHead *)pCont)->vgId = TD_VID(pVnode);
116,823✔
465

466
  tEncoderInit(pCoder, pCont + sizeof(SMsgHead), size);
116,823✔
467
  if (tEncodeDeleteRes(pCoder, &res) != 0) {
116,825!
468
    vError("vgId:%d %s failed to encode delete response", TD_VID(pVnode), __func__);
×
469
  }
470
  tEncoderClear(pCoder);
116,824✔
471

472
  rpcFreeCont(pMsg->pCont);
116,824✔
473
  pMsg->pCont = pCont;
116,825✔
474
  pMsg->contLen = size + sizeof(SMsgHead);
116,825✔
475

476
  taosArrayDestroy(res.uidList);
116,825✔
477

478
_exit:
116,824✔
479
  return code;
116,824✔
480
}
481

482
static int32_t vnodePreProcessBatchDeleteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
×
483
  int32_t code = 0;
×
484
  int32_t lino = 0;
×
485

486
  int64_t         ctimeMs = taosGetTimestampMs();
×
487
  SBatchDeleteReq pReq = {0};
×
488
  SDecoder       *pCoder = &(SDecoder){0};
×
489

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

492
  if (tDecodeSBatchDeleteReqSetCtime(pCoder, &pReq, ctimeMs) < 0) {
×
493
    code = TSDB_CODE_INVALID_MSG;
×
494
  }
495

496
  tDecoderClear(pCoder);
×
497
  taosArrayDestroy(pReq.deleteReqs);
×
498

499
  if (code) {
×
500
    vError("vgId:%d %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
501
  } else {
502
    vTrace("vgId:%d %s done, ctimeMs generated:%" PRId64, TD_VID(pVnode), __func__, ctimeMs);
×
503
  }
504
  return code;
×
505
}
506

507
static int32_t vnodePreProcessArbCheckSyncMsg(SVnode *pVnode, SRpcMsg *pMsg) {
40✔
508
  SVArbCheckSyncReq syncReq = {0};
40✔
509

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

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

520
  int32_t code = terrno;
40✔
521
  tFreeSVArbCheckSyncReq(&syncReq);
40✔
522

523
  return code;
40✔
524
}
525

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

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

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

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

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

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

581

582
int32_t vnodePreProcessSsMigrateReq(SVnode* pVnode, SRpcMsg* pMsg) {
×
583
  int32_t          code = TSDB_CODE_SUCCESS;
×
584
  int32_t          lino = 0;
×
585
  SSsMigrateVgroupReq req = {0};
×
586

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

593
  req.nodeId = vnodeNodeId(pVnode);
×
594
  tSerializeSSsMigrateVgroupReq(POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), pMsg->contLen - sizeof(SMsgHead), &req);
×
595

596
_exit:
×
597
  return code;
×
598
}
599

600

601
int32_t vnodePreProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
19,371,948✔
602
  int32_t code = 0;
19,371,948✔
603

604
  if (pVnode->mounted) {
19,371,948!
605
    vError("vgId:%d, mountVgId:%d, skip write msg:%s", TD_VID(pVnode), pVnode->config.mountVgId,
×
606
           TMSG_INFO(pMsg->msgType));
607
    return TSDB_CODE_OPS_NOT_SUPPORT;
×
608
  }
609

610
  switch (pMsg->msgType) {
19,371,948!
611
    case TDMT_VND_CREATE_TABLE: {
83,296✔
612
      code = vnodePreProcessCreateTableMsg(pVnode, pMsg);
83,296✔
613
    } break;
83,282✔
614
    case TDMT_VND_ALTER_TABLE: {
10,564✔
615
      code = vnodePreProcessAlterTableMsg(pVnode, pMsg);
10,564✔
616
    } break;
10,564✔
617
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
115,201✔
618
    case TDMT_VND_DROP_TTL_TABLE: {
619
      code = vnodePreProcessDropTtlMsg(pVnode, pMsg);
115,201✔
620
    } break;
114,848✔
621
    case TDMT_VND_SUBMIT: {
18,941,908✔
622
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.preprocess_time, METRIC_LEVEL_LOW,
18,941,908!
623
                           { code = vnodePreProcessSubmitMsg(pVnode, pMsg); });
624
    } break;
18,941,856✔
625
    case TDMT_VND_DELETE: {
116,811✔
626
      code = vnodePreProcessDeleteMsg(pVnode, pMsg);
116,811✔
627
    } break;
116,821✔
628
    case TDMT_VND_BATCH_DEL: {
×
629
      code = vnodePreProcessBatchDeleteMsg(pVnode, pMsg);
×
630
    } break;
×
631
    case TDMT_VND_ARB_CHECK_SYNC: {
40✔
632
      code = vnodePreProcessArbCheckSyncMsg(pVnode, pMsg);
40✔
633
    } break;
40✔
634
    case TDMT_VND_DROP_TABLE: {
15,795✔
635
      code = vnodePreProcessDropTbMsg(pVnode, pMsg);
15,795✔
636
    } break;
15,795✔
637
    case TDMT_VND_SSMIGRATE: {
×
638
      code = vnodePreProcessSsMigrateReq(pVnode, pMsg);
×
639
    } break;
×
640
    default:
88,333✔
641
      break;
88,333✔
642
  }
643

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

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

655
  int64_t    st = taosGetTimestampUs();
×
656
  SBlobRow2 *pBlobRow = pSubmitTbData->pBlobRow;
×
657
  int32_t    sz = taosArrayGetSize(pBlobRow->pSeqTable);
×
658

659
  SBseBatch *pBatch = NULL;
×
660

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

664
  SRow  **pRow = (SRow **)TARRAY_DATA(pSubmitTbData->aRowP);
×
665
  int32_t rowIdx = -1;
×
666
  for (int32_t i = 0; i < sz; i++) {
×
667
    int64_t     seq = 0;
×
668
    SBlobValue *p = taosArrayGet(pBlobRow->pSeqTable, i);
×
669
    code = bseBatchPut(pBatch, &seq, pBlobRow->data + p->offset, p->len);
×
670
    TSDB_CHECK_CODE(code, lino, _exit);
×
671

672
    if (p->nextRow == 1) {
×
673
      rowIdx++;
×
674
    }
675
    if (p->len == 0) {
×
676
      uWarn("received invalid row");
×
677
      continue;
×
678
    }
679
    SRow *row = taosArrayGetP(pSubmitTbData->aRowP, rowIdx);
×
680
    if (row == NULL) {
×
681
      int32_t tlen = taosArrayGetSize(pBlobRow->pSeqTable);
×
682
      uTrace("blob invalid row index:%d, sz:%d, pBlobRow size:%d", rowIdx, sz, tlen);
×
683
      break;
×
684
    }
685
    // tPutU64(row->data+p->pdataO, uint64_t v)
686
    tPutU64(row->data + p->dataOffset, seq);
×
687
    // memcpy(row->data + p->dataOffset, (void *)&seq, sizeof(uint64_t));
688
  }
689

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

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

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

709
  int32_t    blobColIdx = 0;
×
710
  SColData  *pBlobCol = NULL;
×
711
  int64_t    st = taosGetTimestampUs();
×
712
  SBlobRow2 *pBlobRow = pSubmitTbData->pBlobRow;
×
713
  int32_t    sz = taosArrayGetSize(pBlobRow->pSeqTable);
×
714

715
  SBseBatch *pBatch = NULL;
×
716

717
  code = bseBatchInit(pVnode->pBse, &pBatch, sz);
×
718
  TSDB_CHECK_CODE(code, lino, _exit);
×
719

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

733
  int32_t offset = 0;
×
734
  // int32_t   rowIdx = -1;
735
  for (int32_t i = 0; i < sz; i++) {
×
736
    int64_t     seq = 0;
×
737
    SBlobValue *p = taosArrayGet(pBlobRow->pSeqTable, i);
×
738
    code = bseBatchPut(pBatch, &seq, pBlobRow->data + p->offset, p->len);
×
739
    TSDB_CHECK_CODE(code, lino, _exit);
×
740

741
    memcpy(pBlobCol->pData + offset, (void *)&seq, BSE_SEQUECE_SIZE);
×
742
    offset += BSE_SEQUECE_SIZE;
×
743
  }
744

745
  code = bseCommitBatch(pVnode->pBse, pBatch);
×
746
  TSDB_CHECK_CODE(code, lino, _exit);
×
747

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

769
  return code;
×
770
}
771

772
int32_t vnodeProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg, int64_t ver, SRpcMsg *pRsp) {
26,921,750✔
773
  int32_t code = 0;
26,921,750✔
774
  int32_t lino = 0;
26,921,750✔
775
  void   *ptr = NULL;
26,921,750✔
776
  void   *pReq;
777
  int32_t len;
778

779
  (void)taosThreadMutexLock(&pVnode->mutex);
26,921,750✔
780
  if (pVnode->disableWrite) {
26,922,248!
781
    (void)taosThreadMutexUnlock(&pVnode->mutex);
×
782
    vError("vgId:%d write is disabled for snapshot, version:%" PRId64, TD_VID(pVnode), ver);
×
783
    return TSDB_CODE_VND_WRITE_DISABLED;
×
784
  }
785
  (void)taosThreadMutexUnlock(&pVnode->mutex);
26,922,248✔
786

787
  if (ver <= pVnode->state.applied) {
26,922,246!
788
    vError("vgId:%d, duplicate write request. ver: %" PRId64 ", applied: %" PRId64, TD_VID(pVnode), ver,
×
789
           pVnode->state.applied);
790
    return terrno = TSDB_CODE_VND_DUP_REQUEST;
×
791
  }
792

793
  vGDebug(&pMsg->info.traceId,
26,922,246!
794
          "vgId:%d, index:%" PRId64 ", process write request:%s, applied:%" PRId64 ", state.applyTerm:%" PRId64
795
          ", conn.applyTerm:%" PRId64 ", contLen:%d",
796
          TD_VID(pVnode), ver, TMSG_INFO(pMsg->msgType), pVnode->state.applied, pVnode->state.applyTerm,
797
          pMsg->info.conn.applyTerm, pMsg->contLen);
798

799
  if (!(pVnode->state.applyTerm <= pMsg->info.conn.applyTerm)) {
26,921,851!
800
    vError("vgId:%d, applyTerm mismatch, expected: %" PRId64 ", received: %" PRId64, TD_VID(pVnode),
×
801
           pVnode->state.applyTerm, pMsg->info.conn.applyTerm);
802
    return terrno = TSDB_CODE_INTERNAL_ERROR;
×
803
  }
804

805
  if (!(pVnode->state.applied + 1 == ver)) {
26,921,851!
806
    vError("vgId:%d, mountVgId:%d, ver mismatch, expected: %" PRId64 ", received: %" PRId64, TD_VID(pVnode), pVnode->config.mountVgId,
×
807
           pVnode->state.applied + 1, ver);
808
    return terrno = TSDB_CODE_INTERNAL_ERROR;
×
809
  }
810

811
  atomic_store_64(&pVnode->state.applied, ver);
26,921,851✔
812
  atomic_store_64(&pVnode->state.applyTerm, pMsg->info.conn.applyTerm);
26,922,198✔
813

814
  if (!syncUtilUserCommit(pMsg->msgType)) goto _exit;
26,922,228✔
815

816
  // skip header
817
  pReq = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
26,881,841✔
818
  len = pMsg->contLen - sizeof(SMsgHead);
26,881,841✔
819
  bool needCommit = false;
26,881,841✔
820

821
  switch (pMsg->msgType) {
26,881,841!
822
    /* META */
823
    case TDMT_VND_CREATE_STB:
25,277✔
824
      code = vnodeProcessCreateStbReq(pVnode, ver, pReq, len, pRsp);
25,277✔
825
      TSDB_CHECK_CODE(code, lino, _err);
25,394✔
826
      break;
25,386✔
827
    case TDMT_VND_ALTER_STB:
11,453✔
828
      code = vnodeProcessAlterStbReq(pVnode, ver, pReq, len, pRsp);
11,453✔
829
      TSDB_CHECK_CODE(code, lino, _err);
11,461!
830
      break;
11,461✔
831
    case TDMT_VND_DROP_STB:
3,622✔
832
      code = vnodeProcessDropStbReq(pVnode, ver, pReq, len, pRsp);
3,622✔
833
      TSDB_CHECK_CODE(code, lino, _err);
3,635!
834
      break;
3,635✔
835
    case TDMT_VND_CREATE_TABLE:
95,016✔
836
      code = vnodeProcessCreateTbReq(pVnode, ver, pReq, len, pRsp, pMsg);
95,016✔
837
      TSDB_CHECK_CODE(code, lino, _err);
95,055!
838
      break;
95,055✔
839
    case TDMT_VND_ALTER_TABLE:
10,570✔
840
      code = vnodeProcessAlterTbReq(pVnode, ver, pReq, len, pRsp);
10,570✔
841
      TSDB_CHECK_CODE(code, lino, _err);
10,570!
842
      break;
10,570✔
843
    case TDMT_VND_DROP_TABLE:
15,837✔
844
      code = vnodeProcessDropTbReq(pVnode, ver, pReq, len, pRsp, pMsg);
15,837✔
845
      TSDB_CHECK_CODE(code, lino, _err);
15,837!
846
      break;
15,837✔
847
    case TDMT_VND_DROP_TTL_TABLE:
×
848
      code = vnodeProcessDropTtlTbReq(pVnode, ver, pReq, len, pRsp);
×
849
      TSDB_CHECK_CODE(code, lino, _err);
×
850
      break;
×
851
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
30✔
852
      code = vnodeProcessFetchTtlExpiredTbs(pVnode, ver, pReq, len, pRsp);
30✔
853
      TSDB_CHECK_CODE(code, lino, _err);
30!
854
      break;
30✔
855
    case TDMT_VND_TRIM:
233✔
856
      code = vnodeProcessTrimReq(pVnode, ver, pReq, len, pRsp);
233✔
857
      TSDB_CHECK_CODE(code, lino, _err);
234!
858
      break;
234✔
859
    case TDMT_VND_SSMIGRATE:
×
860
      if (vnodeProcessSsMigrateReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
861
      break;
×
862
    case TDMT_VND_FOLLOWER_SSMIGRATE:
×
863
      if (vnodeProcessFollowerSsMigrateReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
864
      break;
×
865
      break;
866
    /* TSDB */
867
    case TDMT_VND_SUBMIT: {
26,527,638✔
868
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.apply_time, METRIC_LEVEL_LOW, {
26,527,638!
869
        if (vnodeProcessSubmitReq(pVnode, ver, pMsg->pCont, pMsg->contLen, pRsp, pMsg) < 0) goto _err;
870
      });
871
      METRICS_UPDATE(pVnode->writeMetrics.apply_bytes, METRIC_LEVEL_LOW, (int64_t)pMsg->contLen);
26,527,700!
872
      break;
26,527,623✔
873
    }
874
    case TDMT_VND_DELETE:
120,264✔
875
      code = vnodeProcessDeleteReq(pVnode, ver, pReq, len, pRsp, pMsg);
120,264✔
876
      TSDB_CHECK_CODE(code, lino, _err);
120,264!
877
      break;
120,264✔
878
    case TDMT_VND_BATCH_DEL:
×
879
      code = vnodeProcessBatchDeleteReq(pVnode, ver, pReq, len, pRsp);
×
880
      TSDB_CHECK_CODE(code, lino, _err);
×
881
      break;
×
882
      /* TQ */
883
#if defined(USE_TQ) || defined(USE_STREAM)
884
    case TDMT_VND_TMQ_SUBSCRIBE:
3,283✔
885
      code = tqProcessSubscribeReq(pVnode->pTq, ver, pReq, len);
3,283✔
886
      TSDB_CHECK_CODE(code, lino, _err);
3,295!
887
      break;
3,295✔
888
    case TDMT_VND_TMQ_DELETE_SUB:
947✔
889
      code = tqProcessDeleteSubReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen);
947✔
890
      TSDB_CHECK_CODE(code, lino, _err);
948!
891
      break;
948✔
892
    case TDMT_VND_TMQ_COMMIT_OFFSET:
10,325✔
893
      code = tqProcessOffsetCommitReq(pVnode->pTq, ver, pReq, len);
10,325✔
894
      TSDB_CHECK_CODE(code, lino, _err);
10,337!
895
      break;
10,337✔
896
    case TDMT_VND_TMQ_ADD_CHECKINFO:
123✔
897
      code = tqProcessAddCheckInfoReq(pVnode->pTq, ver, pReq, len);
123✔
898
      TSDB_CHECK_CODE(code, lino, _err);
123!
899
      break;
123✔
900
    case TDMT_VND_TMQ_DEL_CHECKINFO:
11✔
901
      code = tqProcessDelCheckInfoReq(pVnode->pTq, ver, pReq, len);
11✔
902
      TSDB_CHECK_CODE(code, lino, _err);
11!
903
      break;
11✔
904
#endif
905
    case TDMT_VND_ALTER_CONFIRM:
15,099✔
906
      needCommit = pVnode->config.hashChange;
15,099✔
907
      code = vnodeProcessAlterConfirmReq(pVnode, ver, pReq, len, pRsp);
15,099✔
908
      TSDB_CHECK_CODE(code, lino, _err);
15,099!
909
      break;
15,099✔
910
    case TDMT_VND_ALTER_CONFIG:
1,297✔
911
      vnodeProcessAlterConfigReq(pVnode, ver, pReq, len, pRsp);
1,297✔
912
      break;
1,298✔
913
    case TDMT_VND_COMMIT:
34,468✔
914
      needCommit = true;
34,468✔
915
      break;
34,468✔
916
    case TDMT_VND_CREATE_INDEX:
1,892✔
917
      vnodeProcessCreateIndexReq(pVnode, ver, pReq, len, pRsp);
1,892✔
918
      break;
1,892✔
919
    case TDMT_VND_DROP_INDEX:
4,234✔
920
      vnodeProcessDropIndexReq(pVnode, ver, pReq, len, pRsp);
4,234✔
921
      break;
4,234✔
922
    case TDMT_VND_COMPACT:
214✔
923
      vnodeProcessCompactVnodeReq(pVnode, ver, pReq, len, pRsp);
214✔
924
      goto _exit;
214✔
925
    case TDMT_SYNC_CONFIG_CHANGE:
×
926
      vnodeProcessConfigChangeReq(pVnode, ver, pReq, len, pRsp);
×
927
      break;
×
928
#ifdef TD_ENTERPRISE
929
    case TDMT_VND_KILL_COMPACT:
×
930
      vnodeProcessKillCompactReq(pVnode, ver, pReq, len, pRsp);
×
931
      break;
×
932
#endif
933
    /* ARB */
934
    case TDMT_VND_ARB_CHECK_SYNC:
8✔
935
      vnodeProcessArbCheckSyncReq(pVnode, pReq, len, pRsp);
8✔
936
      break;
8✔
937
    default:
×
938
      vError("vgId:%d, unprocessed msg, %d", TD_VID(pVnode), pMsg->msgType);
×
939
      return TSDB_CODE_INVALID_MSG;
×
940
  }
941

942
  vGDebug(&pMsg->info.traceId, "vgId:%d, index:%" PRId64 ", msg processed, code:0x%x", TD_VID(pVnode), ver, pRsp->code);
26,881,808!
943

944
  walApplyVer(pVnode->pWal, ver);
26,881,860✔
945

946
  if (pVnode->pTq) {
26,881,810!
947
    code = tqPushMsg(pVnode->pTq, pMsg->msgType);
26,881,878✔
948
    if (code) {
26,881,878!
949
      vError("vgId:%d, failed to push msg to TQ since %s", TD_VID(pVnode), tstrerror(terrno));
×
950
      return code;
×
951
    }
952
  }
953

954
  // commit if need
955
  if (needCommit) {
26,881,827✔
956
    vInfo("vgId:%d, commit at version %" PRId64, TD_VID(pVnode), ver);
34,521✔
957
    code = vnodeAsyncCommit(pVnode);
34,608✔
958
    if (code) {
34,605!
959
      vError("vgId:%d, failed to vnode async commit since %s.", TD_VID(pVnode), tstrerror(terrno));
×
960
      goto _err;
×
961
    }
962

963
    // start a new one
964
    METRICS_TIMING_BLOCK(pVnode->writeMetrics.memtable_wait_time, METRIC_LEVEL_LOW, {
34,605!
965
      code = vnodeBegin(pVnode);
966
      if (code) {
967
        vError("vgId:%d, failed to begin vnode since %s.", TD_VID(pVnode), tstrerror(terrno));
968
        goto _err;
969
      }
970
    });
971
  }
972

973
_exit:
26,881,904✔
974
  return 0;
26,921,918✔
975

976
_err:
16✔
977
  if (code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
16✔
978
    vInfo("vgId:%d, process %s request failed since %s, lino 1:%d ver:%" PRId64, TD_VID(pVnode),
8!
979
          TMSG_INFO(pMsg->msgType), tstrerror(code), lino, ver);
980
  } else if (code) {
8!
981
    vError("vgId:%d, process %s request failed since %s, lino 2:%d ver:%" PRId64, TD_VID(pVnode),
×
982
           TMSG_INFO(pMsg->msgType), tstrerror(code), lino, ver);
983
  }
984

985
  return code;
16✔
986
}
987

988
int32_t vnodePreprocessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg) {
14,573,896✔
989
  if (TDMT_SCH_QUERY != pMsg->msgType && TDMT_SCH_MERGE_QUERY != pMsg->msgType) {
14,573,896✔
990
    return 0;
836,173✔
991
  }
992

993
  return qWorkerPreprocessQueryMsg(pVnode->pQuery, pMsg, TDMT_SCH_QUERY == pMsg->msgType);
13,737,723✔
994
}
995

996
int32_t vnodeProcessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
14,587,744✔
997
  vTrace("message in vnode query queue is processing");
14,587,744✔
998
  if (pMsg->msgType == TDMT_VND_TMQ_CONSUME && !syncIsReadyForRead(pVnode->sync)) {
14,587,744✔
999
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
1,378✔
1000
    return 0;
1,378✔
1001
  }
1002

1003
  if (pMsg->msgType == TDMT_VND_TMQ_CONSUME && !pVnode->restored) {
14,586,351!
1004
    vnodeRedirectRpcMsg(pVnode, pMsg, TSDB_CODE_SYN_RESTORING);
×
1005
    return 0;
×
1006
  }
1007

1008
  SReadHandle handle = {0};
14,586,351✔
1009
  handle.vnode = pVnode;
14,586,351✔
1010
  handle.pMsgCb = &pVnode->msgCb;
14,586,351✔
1011
  handle.pWorkerCb = pInfo->workerCb;
14,586,351✔
1012
  initStorageAPI(&handle.api);
14,586,351✔
1013
  int32_t code = TSDB_CODE_SUCCESS;
14,584,081✔
1014
  bool    redirected = false;
14,584,081✔
1015

1016
  switch (pMsg->msgType) {
14,584,081!
1017
    case TDMT_SCH_QUERY:
10,421,229✔
1018
      if (!syncIsReadyForRead(pVnode->sync)) {
10,421,229✔
1019
        pMsg->code = (terrno) ? terrno : TSDB_CODE_SYN_NOT_LEADER;
3,867!
1020
        redirected = true;
3,867✔
1021
      }
1022
      code = qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
10,422,231✔
1023
      if (redirected) {
10,418,986✔
1024
        vnodeRedirectRpcMsg(pVnode, pMsg, pMsg->code);
1,871✔
1025
        return 0;
3,867✔
1026
      }
1027

1028
      return code;
10,417,115✔
1029
    case TDMT_SCH_MERGE_QUERY:
3,328,095✔
1030
      return qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
3,328,095✔
1031
    case TDMT_SCH_QUERY_CONTINUE:
766,391✔
1032
      return qWorkerProcessCQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
766,391✔
1033
    case TDMT_VND_TMQ_CONSUME:
53,207✔
1034
      return tqProcessPollReq(pVnode->pTq, pMsg);
53,207✔
1035
    case TDMT_VND_TMQ_CONSUME_PUSH:
15,174✔
1036
      return tqProcessPollPush(pVnode->pTq);
15,174✔
1037
    default:
×
1038
      vError("unknown msg type:%d in query queue", pMsg->msgType);
×
1039
      return TSDB_CODE_APP_ERROR;
×
1040
  }
1041
}
1042

1043
int32_t vnodeProcessFetchMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
37,976,614✔
1044
  int32_t code = TSDB_CODE_SUCCESS;
37,976,614✔
1045
  vTrace("vgId:%d, msg:%p in fetch queue is processing", pVnode->config.vgId, pMsg);
37,976,614✔
1046
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
37,976,615!
1047
       pMsg->msgType == TDMT_VND_BATCH_META || pMsg->msgType == TDMT_VND_TABLE_NAME ||
26,642,681✔
1048
       pMsg->msgType == TDMT_VND_VSUBTABLES_META || pMsg->msgType == TDMT_VND_VSTB_REF_DBS) &&
24,432,096!
1049
      !syncIsReadyForRead(pVnode->sync)) {
13,549,288✔
1050
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
5,070✔
1051
    return 0;
5,070✔
1052
  }
1053

1054
  switch (pMsg->msgType) {
37,983,386!
1055
    case TDMT_SCH_FETCH:
18,423,638✔
1056
    case TDMT_SCH_MERGE_FETCH:
1057
      return qWorkerProcessFetchMsg(pVnode, pVnode->pQuery, pMsg, 0);
18,423,638✔
1058
    case TDMT_SCH_FETCH_RSP:
×
1059
      return qWorkerProcessRspMsg(pVnode, pVnode->pQuery, pMsg, 0);
×
1060
    // case TDMT_SCH_CANCEL_TASK:
1061
    //   return qWorkerProcessCancelMsg(pVnode, pVnode->pQuery, pMsg, 0);
1062
    case TDMT_SCH_DROP_TASK:
13,725,634✔
1063
      return qWorkerProcessDropMsg(pVnode, pVnode->pQuery, pMsg, 0);
13,725,634✔
1064
    case TDMT_SCH_TASK_NOTIFY:
165✔
1065
      return qWorkerProcessNotifyMsg(pVnode, pVnode->pQuery, pMsg, 0);
165✔
1066
    case TDMT_SCH_QUERY_HEARTBEAT:
3,625,700✔
1067
      return qWorkerProcessHbMsg(pVnode, pVnode->pQuery, pMsg, 0);
3,625,700✔
1068
    case TDMT_VND_TABLE_META:
4,493✔
1069
    case TDMT_VND_TABLE_NAME:
1070
      return vnodeGetTableMeta(pVnode, pMsg, true);
4,493✔
1071
    case TDMT_VND_TABLE_CFG:
×
1072
      return vnodeGetTableCfg(pVnode, pMsg, true);
×
1073
    case TDMT_VND_BATCH_META: {
2,203,660✔
1074
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.fetch_batch_meta_time, METRIC_LEVEL_LOW,
2,203,660!
1075
                           { code = vnodeGetBatchMeta(pVnode, pMsg); });
1076
      METRICS_UPDATE(pVnode->writeMetrics.fetch_batch_meta_count, METRIC_LEVEL_LOW, 1);
2,204,698!
1077
      return code;
2,204,751✔
1078
    }
1079
    case TDMT_VND_VSUBTABLES_META:
×
1080
      return vnodeGetVSubtablesMeta(pVnode, pMsg);
×
1081
    case TDMT_VND_VSTB_REF_DBS:
×
1082
      return vnodeGetVStbRefDbs(pVnode, pMsg);
×
1083
#ifdef TD_ENTERPRISE
1084
    case TDMT_VND_QUERY_COMPACT_PROGRESS:
56✔
1085
      return vnodeQueryCompactProgress(pVnode, pMsg);
56✔
1086
#endif
1087
    case TDMT_VND_QUERY_SSMIGRATE_PROGRESS:
×
1088
      return vnodeQuerySsMigrateProgress(pVnode, pMsg);
×
1089
      //    case TDMT_VND_TMQ_CONSUME:
1090
      //      return tqProcessPollReq(pVnode->pTq, pMsg);
1091
#ifdef USE_TQ
1092
    case TDMT_VND_TMQ_VG_WALINFO:
11✔
1093
      return tqProcessVgWalInfoReq(pVnode->pTq, pMsg);
11✔
1094
    case TDMT_VND_TMQ_VG_COMMITTEDINFO:
3✔
1095
      return tqProcessVgCommittedInfoReq(pVnode->pTq, pMsg);
3✔
1096
    case TDMT_VND_TMQ_SEEK:
26✔
1097
      return tqProcessSeekReq(pVnode->pTq, pMsg);
26✔
1098
#endif
1099
    default:
×
1100
      vError("unknown msg type:%d in fetch queue", pMsg->msgType);
×
1101
      return TSDB_CODE_APP_ERROR;
×
1102
  }
1103
}
1104

1105
void smaHandleRes(void *pVnode, int64_t smaId, const SArray *data) {
×
1106
  int32_t code = tdProcessTSmaInsert(((SVnode *)pVnode)->pSma, smaId, (const char *)data);
×
1107
  if (code) {
×
1108
    vError("failed to process sma result since %s", tstrerror(code));
×
1109
  }
1110
}
×
1111

1112
void vnodeUpdateMetaRsp(SVnode *pVnode, STableMetaRsp *pMetaRsp) {
227,894✔
1113
  if (NULL == pMetaRsp) {
227,894!
1114
    return;
×
1115
  }
1116

1117
  tstrncpy(pMetaRsp->dbFName, pVnode->config.dbname, TSDB_DB_FNAME_LEN);
227,894✔
1118
  pMetaRsp->dbId = pVnode->config.dbId;
227,894✔
1119
  pMetaRsp->vgId = TD_VID(pVnode);
227,894✔
1120
  pMetaRsp->precision = pVnode->config.tsdbCfg.precision;
227,894✔
1121
}
1122

1123
extern int32_t vnodeAsyncRetention(SVnode *pVnode, int64_t now);
1124

1125
static int32_t vnodeProcessTrimReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
234✔
1126
  if (!pVnode->restored) {
234✔
1127
    vInfo("vgId:%d, ignore trim req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
6!
1128
    return 0;
6✔
1129
  }
1130

1131
  int32_t     code = 0;
228✔
1132
  SVTrimDbReq trimReq = {0};
228✔
1133

1134
  // decode
1135
  if (tDeserializeSVTrimDbReq(pReq, len, &trimReq) != 0) {
228!
1136
    code = TSDB_CODE_INVALID_MSG;
×
1137
    goto _exit;
×
1138
  }
1139

1140
  vInfo("vgId:%d, process trim vnode request, time:%d", pVnode->config.vgId, trimReq.timestamp);
228!
1141

1142
  code = vnodeAsyncRetention(pVnode, trimReq.timestamp);
228✔
1143

1144
_exit:
228✔
1145
  return code;
228✔
1146
}
1147

1148
extern int32_t vnodeAsyncSsMigrate(SVnode *pVnode, SSsMigrateVgroupReq *pReq);
1149

1150
static int32_t vnodeProcessSsMigrateReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1151
  int32_t          code = 0;
×
1152
  SSsMigrateVgroupReq req = {0};
×
1153
  SSsMigrateVgroupRsp rsp = {0};
×
1154
  pRsp->msgType = TDMT_VND_SSMIGRATE_RSP;
×
1155
  pRsp->code = 0;
×
1156
  pRsp->pCont = NULL;
×
1157
  pRsp->contLen = 0;
×
1158

1159
  if (tDeserializeSSsMigrateVgroupReq(pReq, len, &req) != 0) {
×
1160
    code = TSDB_CODE_INVALID_MSG;
×
1161
    goto _exit;
×
1162
  }
1163

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

1166
  code = vnodeAsyncSsMigrate(pVnode, &req);
×
1167
  if (code != TSDB_CODE_SUCCESS) {
×
1168
    vError("vgId:%d, failed to async ssmigrate since %s", TD_VID(pVnode), tstrerror(code));
×
1169
    pRsp->code = code;
×
1170
    goto _exit;
×
1171
  }
1172

1173
  rsp.ssMigrateId = req.ssMigrateId;
×
1174
  rsp.vgId = TD_VID(pVnode);
×
1175
  rsp.nodeId = req.nodeId;
×
1176

1177
  pRsp->code = TSDB_CODE_SUCCESS;
×
1178
  pRsp->contLen = tSerializeSSsMigrateVgroupRsp(NULL, 0, &rsp);
×
1179
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
×
1180
  if (pRsp->pCont == NULL) {
×
1181
    vError("vgId:%d, failed to allocate memory for ssmigrate response", TD_VID(pVnode));
×
1182
    code = TSDB_CODE_OUT_OF_MEMORY;
×
1183
    goto _exit;
×
1184
  }
1185
  tSerializeSSsMigrateVgroupRsp(pRsp->pCont, pRsp->contLen, &rsp);
×
1186

1187
_exit:
×
1188
  if (code != TSDB_CODE_SUCCESS) {
×
1189
    pRsp->code = code;
×
1190
  }
1191
  return code;
×
1192
}
1193

1194

1195
extern int32_t vnodeFollowerSsMigrate(SVnode *pVnode, SFollowerSsMigrateReq *pReq);
1196

1197
static int32_t vnodeProcessFollowerSsMigrateReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1198
  int32_t          code = 0;
×
1199
  SFollowerSsMigrateReq req = {0};
×
1200

1201
  // decode
1202
  if (tDeserializeSFollowerSsMigrateReq(pReq, len, &req) != 0) {
×
1203
    code = TSDB_CODE_INVALID_MSG;
×
1204
    goto _exit;
×
1205
  }
1206

1207
  code = vnodeFollowerSsMigrate(pVnode, &req);
×
1208

1209
_exit:
×
1210
  tFreeSFollowerSsMigrateReq(&req);
×
1211
  return code;
×
1212
}
1213

1214
static int32_t vnodeProcessDropTtlTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1215
  int               ret = 0;
×
1216
  SVDropTtlTableReq ttlReq = {0};
×
1217
  if (tDeserializeSVDropTtlTableReq(pReq, len, &ttlReq) != 0) {
×
1218
    ret = TSDB_CODE_INVALID_MSG;
×
1219
    goto end;
×
1220
  }
1221

1222
  if (ttlReq.nUids != taosArrayGetSize(ttlReq.pTbUids)) {
×
1223
    ret = TSDB_CODE_INVALID_MSG;
×
1224
    goto end;
×
1225
  }
1226

1227
  if (ttlReq.nUids != 0) {
×
1228
    vInfo("vgId:%d, process drop ttl table request, time:%d, ntbUids:%d", pVnode->config.vgId, ttlReq.timestampSec,
×
1229
          ttlReq.nUids);
1230
  }
1231

1232
  if (ttlReq.nUids > 0) {
×
1233
    int32_t code = metaDropMultipleTables(pVnode->pMeta, ver, ttlReq.pTbUids);
×
1234
    if (code) return code;
×
1235

1236
    code = tqUpdateTbUidList(pVnode->pTq, ttlReq.pTbUids, false);
×
1237
    if (code) {
×
1238
      vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1239
    }
1240
  }
1241

1242
end:
×
1243
  taosArrayDestroy(ttlReq.pTbUids);
×
1244
  return ret;
×
1245
}
1246

1247
static int32_t vnodeProcessFetchTtlExpiredTbs(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
30✔
1248
  int32_t                 code = -1;
30✔
1249
  SMetaReader             mr = {0};
30✔
1250
  SVDropTtlTableReq       ttlReq = {0};
30✔
1251
  SVFetchTtlExpiredTbsRsp rsp = {0};
30✔
1252
  SEncoder                encoder = {0};
30✔
1253
  SArray                 *pNames = NULL;
30✔
1254
  pRsp->msgType = TDMT_VND_FETCH_TTL_EXPIRED_TBS_RSP;
30✔
1255
  pRsp->code = TSDB_CODE_SUCCESS;
30✔
1256
  pRsp->pCont = NULL;
30✔
1257
  pRsp->contLen = 0;
30✔
1258

1259
  if (tDeserializeSVDropTtlTableReq(pReq, len, &ttlReq) != 0) {
30!
1260
    terrno = TSDB_CODE_INVALID_MSG;
×
1261
    goto _end;
×
1262
  }
1263

1264
  if (!(ttlReq.nUids == taosArrayGetSize(ttlReq.pTbUids))) {
30!
1265
    terrno = TSDB_CODE_INVALID_MSG;
×
1266
    goto _end;
×
1267
  }
1268

1269
  tb_uid_t    suid;
1270
  char        ctbName[TSDB_TABLE_NAME_LEN];
1271
  SVDropTbReq expiredTb = {.igNotExists = true};
30✔
1272
  metaReaderDoInit(&mr, pVnode->pMeta, 0);
30✔
1273
  rsp.vgId = TD_VID(pVnode);
30✔
1274
  rsp.pExpiredTbs = taosArrayInit(ttlReq.nUids, sizeof(SVDropTbReq));
30✔
1275
  if (!rsp.pExpiredTbs) goto _end;
30!
1276

1277
  pNames = taosArrayInit(ttlReq.nUids, TSDB_TABLE_NAME_LEN);
30✔
1278
  if (!pNames) {
30!
1279
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1280
    goto _end;
×
1281
  }
1282
  char buf[TSDB_TABLE_NAME_LEN];
1283
  for (int32_t i = 0; i < ttlReq.nUids; ++i) {
151✔
1284
    tb_uid_t *uid = taosArrayGet(ttlReq.pTbUids, i);
121✔
1285
    expiredTb.suid = *uid;
121✔
1286
    terrno = metaReaderGetTableEntryByUid(&mr, *uid);
121✔
1287
    if (terrno < 0) goto _end;
121!
1288
    tstrncpy(buf, mr.me.name, TSDB_TABLE_NAME_LEN);
121✔
1289
    void *p = taosArrayPush(pNames, buf);
121✔
1290
    if (p == NULL) {
121!
1291
      goto _end;
×
1292
    }
1293

1294
    expiredTb.name = p;
121✔
1295
    if (mr.me.type == TSDB_CHILD_TABLE) {
121✔
1296
      expiredTb.suid = mr.me.ctbEntry.suid;
76✔
1297
    }
1298

1299
    if (taosArrayPush(rsp.pExpiredTbs, &expiredTb) == NULL) {
242!
1300
      goto _end;
×
1301
    }
1302
  }
1303

1304
  int32_t ret = 0;
30✔
1305
  tEncodeSize(tEncodeVFetchTtlExpiredTbsRsp, &rsp, pRsp->contLen, ret);
30!
1306
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
30✔
1307
  if (pRsp->pCont == NULL) {
30!
1308
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1309
    code = -1;
×
1310
    goto _end;
×
1311
  }
1312
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
30✔
1313
  terrno = tEncodeVFetchTtlExpiredTbsRsp(&encoder, &rsp);
30✔
1314
  tEncoderClear(&encoder);
30✔
1315

1316
  if (terrno == 0) code = 0;
30!
1317
_end:
×
1318
  metaReaderClear(&mr);
30✔
1319
  tFreeFetchTtlExpiredTbsRsp(&rsp);
30✔
1320
  taosArrayDestroy(ttlReq.pTbUids);
30✔
1321
  if (pNames) taosArrayDestroy(pNames);
30!
1322
  pRsp->code = terrno;
30✔
1323
  return code;
30✔
1324
}
1325

1326
static int32_t vnodeProcessCreateStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
25,161✔
1327
  int32_t        code = 0;
25,161✔
1328
  SVCreateStbReq req = {0};
25,161✔
1329
  SDecoder       coder;
1330

1331
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
25,161✔
1332
  pRsp->code = TSDB_CODE_SUCCESS;
25,161✔
1333
  pRsp->pCont = NULL;
25,161✔
1334
  pRsp->contLen = 0;
25,161✔
1335

1336
  // decode and process req
1337
  tDecoderInit(&coder, pReq, len);
25,161✔
1338

1339
  code = tDecodeSVCreateStbReq(&coder, &req);
25,296✔
1340
  if (code) {
25,258!
1341
    pRsp->code = code;
×
1342
    goto _err;
×
1343
  }
1344

1345
  code = metaCreateSuperTable(pVnode->pMeta, ver, &req);
25,258✔
1346
  if (code) {
25,420✔
1347
    pRsp->code = code;
8✔
1348
    goto _err;
8✔
1349
  }
1350

1351
  if ((code = tdProcessRSmaCreate(pVnode->pSma, &req)) < 0) {
25,412!
1352
    pRsp->code = code;
×
1353
    goto _err;
×
1354
  }
1355

1356
  tDecoderClear(&coder);
25,401✔
1357
  return 0;
25,379✔
1358

1359
_err:
8✔
1360
  tDecoderClear(&coder);
8✔
1361
  return code;
8✔
1362
}
1363

1364
static int32_t vnodeProcessCreateTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
95,007✔
1365
                                       SRpcMsg *pOriginRpc) {
1366
  SDecoder           decoder = {0};
95,007✔
1367
  SEncoder           encoder = {0};
95,007✔
1368
  int32_t            rcode = 0;
95,007✔
1369
  SVCreateTbBatchReq req = {0};
95,007✔
1370
  SVCreateTbReq     *pCreateReq;
1371
  SVCreateTbBatchRsp rsp = {0};
95,007✔
1372
  SVCreateTbRsp      cRsp = {0};
95,007✔
1373
  char               tbName[TSDB_TABLE_FNAME_LEN];
1374
  STbUidStore       *pStore = NULL;
95,007✔
1375
  SArray            *tbUids = NULL;
95,007✔
1376
  SArray            *tbNames = NULL;
95,007✔
1377
  pRsp->msgType = TDMT_VND_CREATE_TABLE_RSP;
95,007✔
1378
  pRsp->code = TSDB_CODE_SUCCESS;
95,007✔
1379
  pRsp->pCont = NULL;
95,007✔
1380
  pRsp->contLen = 0;
95,007✔
1381

1382
  // decode
1383
  tDecoderInit(&decoder, pReq, len);
95,007✔
1384
  if (tDecodeSVCreateTbBatchReq(&decoder, &req) < 0) {
95,010!
1385
    rcode = -1;
×
1386
    terrno = TSDB_CODE_INVALID_MSG;
×
1387
    goto _exit;
×
1388
  }
1389

1390
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(cRsp));
95,036✔
1391
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
95,054✔
1392
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
95,051✔
1393
  if (rsp.pArray == NULL || tbUids == NULL || tbNames == NULL) {
95,053!
1394
    rcode = -1;
4✔
1395
    terrno = TSDB_CODE_OUT_OF_MEMORY;
4✔
1396
    goto _exit;
×
1397
  }
1398

1399
  // loop to create table
1400
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
243,895✔
1401
    pCreateReq = req.pReqs + iReq;
148,836✔
1402
    memset(&cRsp, 0, sizeof(cRsp));
148,836✔
1403

1404
    if (tsEnableAudit && tsEnableAuditCreateTable) {
148,836!
1405
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
148,836!
1406
      if (str == NULL) {
148,834!
1407
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1408
        rcode = -1;
×
1409
        goto _exit;
×
1410
      }
1411
      tstrncpy(str, pCreateReq->name, TSDB_TABLE_FNAME_LEN);
148,834✔
1412
      if (taosArrayPush(tbNames, &str) == NULL) {
148,836!
1413
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1414
        rcode = -1;
×
1415
        goto _exit;
×
1416
      }
1417
    }
1418

1419
    // validate hash
1420
    (void)tsnprintf(tbName, TSDB_TABLE_FNAME_LEN, "%s.%s", pVnode->config.dbname, pCreateReq->name);
148,836✔
1421
    if (vnodeValidateTableHash(pVnode, tbName) < 0) {
148,843!
1422
      cRsp.code = TSDB_CODE_VND_HASH_MISMATCH;
×
1423
      if (taosArrayPush(rsp.pArray, &cRsp) == NULL) {
×
1424
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1425
        rcode = -1;
×
1426
        goto _exit;
×
1427
      }
1428
      vError("vgId:%d create-table:%s failed due to hash value mismatch", TD_VID(pVnode), tbName);
×
1429
      continue;
×
1430
    }
1431

1432
    // do create table
1433
    if (metaCreateTable2(pVnode->pMeta, ver, pCreateReq, &cRsp.pMeta) < 0) {
148,824✔
1434
      if (pCreateReq->flags & TD_CREATE_IF_NOT_EXISTS && terrno == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
38!
1435
        cRsp.code = TSDB_CODE_SUCCESS;
6✔
1436
      } else {
1437
        cRsp.code = terrno;
32✔
1438
      }
1439
    } else {
1440
      cRsp.code = TSDB_CODE_SUCCESS;
148,811✔
1441
      if (tdFetchTbUidList(pVnode->pSma, &pStore, pCreateReq->ctb.suid, pCreateReq->uid) < 0) {
148,811!
1442
        vError("vgId:%d, failed to fetch tbUid list", TD_VID(pVnode));
×
1443
      }
1444
      if (taosArrayPush(tbUids, &pCreateReq->uid) == NULL) {
297,620!
1445
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1446
        rcode = -1;
×
1447
        goto _exit;
×
1448
      }
1449
      vnodeUpdateMetaRsp(pVnode, cRsp.pMeta);
148,810✔
1450
    }
1451

1452
    if (taosArrayPush(rsp.pArray, &cRsp) == NULL) {
297,694!
1453
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1454
      rcode = -1;
×
1455
      goto _exit;
×
1456
    }
1457
  }
1458

1459
  vTrace("vgId:%d, add %d new created tables into query table list", TD_VID(pVnode), (int32_t)taosArrayGetSize(tbUids));
95,059✔
1460
  if (tqUpdateTbUidList(pVnode->pTq, tbUids, true) < 0) {
95,059!
1461
    vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(terrno));
×
1462
  }
1463
  if (tdUpdateTbUidList(pVnode->pSma, pStore, true) < 0) {
95,055!
1464
    goto _exit;
×
1465
  }
1466
  pStore = tdUidStoreFree(pStore);
95,055✔
1467

1468
  // prepare rsp
1469
  int32_t ret = 0;
95,055✔
1470
  tEncodeSize(tEncodeSVCreateTbBatchRsp, &rsp, pRsp->contLen, ret);
95,055!
1471
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
95,051✔
1472
  if (pRsp->pCont == NULL) {
95,053!
1473
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1474
    rcode = -1;
×
1475
    goto _exit;
×
1476
  }
1477
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
95,053✔
1478
  if (tEncodeSVCreateTbBatchRsp(&encoder, &rsp) != 0) {
95,052!
1479
    vError("vgId:%d, failed to encode create table batch response", TD_VID(pVnode));
×
1480
  }
1481

1482
  if (tsEnableAudit && tsEnableAuditCreateTable) {
95,052!
1483
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
95,050✔
1484

1485
    SName name = {0};
95,050✔
1486
    if (tNameFromString(&name, pVnode->config.dbname, T_NAME_ACCT | T_NAME_DB) < 0) {
95,050!
1487
      vError("vgId:%d, failed to get name from string", TD_VID(pVnode));
×
1488
    }
1489

1490
    SStringBuilder sb = {0};
95,051✔
1491
    for (int32_t i = 0; i < tbNames->size; i++) {
243,893✔
1492
      char **key = (char **)taosArrayGet(tbNames, i);
148,845✔
1493
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
148,845✔
1494
      if (i < tbNames->size - 1) {
148,841✔
1495
        taosStringBuilderAppendChar(&sb, ',');
53,795✔
1496
      }
1497
      // taosMemoryFreeClear(*key);
1498
    }
1499

1500
    size_t len = 0;
95,048✔
1501
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
95,048✔
1502

1503
    if (pOriginRpc->info.conn.user != NULL && strlen(pOriginRpc->info.conn.user) > 0) {
95,048!
1504
      auditAddRecord(pOriginRpc, clusterId, "createTable", name.dbname, "", keyJoined, len);
83,279✔
1505
    }
1506

1507
    taosStringBuilderDestroy(&sb);
95,049✔
1508
  }
1509

1510
_exit:
2✔
1511
  tDeleteSVCreateTbBatchReq(&req);
95,050✔
1512
  taosArrayDestroyEx(rsp.pArray, tFreeSVCreateTbRsp);
95,054✔
1513
  taosArrayDestroy(tbUids);
95,055✔
1514
  tDecoderClear(&decoder);
95,055✔
1515
  tEncoderClear(&encoder);
95,053✔
1516
  taosArrayDestroyP(tbNames, NULL);
95,054✔
1517
  return rcode;
95,055✔
1518
}
1519

1520
static int32_t vnodeProcessAlterStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
11,453✔
1521
  int32_t        code = 0;
11,453✔
1522
  SVCreateStbReq req = {0};
11,453✔
1523
  SDecoder       dc = {0};
11,453✔
1524

1525
  pRsp->msgType = TDMT_VND_ALTER_STB_RSP;
11,453✔
1526
  pRsp->code = TSDB_CODE_SUCCESS;
11,453✔
1527
  pRsp->pCont = NULL;
11,453✔
1528
  pRsp->contLen = 0;
11,453✔
1529

1530
  tDecoderInit(&dc, pReq, len);
11,453✔
1531

1532
  // decode req
1533
  code = tDecodeSVCreateStbReq(&dc, &req);
11,453✔
1534
  if (code) {
11,452!
1535
    tDecoderClear(&dc);
×
1536
    return code;
×
1537
  }
1538

1539
  code = metaAlterSuperTable(pVnode->pMeta, ver, &req);
11,452✔
1540
  if (code) {
11,461!
1541
    pRsp->code = code;
×
1542
    tDecoderClear(&dc);
×
1543
    return code;
×
1544
  }
1545

1546
  tDecoderClear(&dc);
11,461✔
1547

1548
  return 0;
11,461✔
1549
}
1550

1551
static int32_t vnodeProcessDropStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
3,622✔
1552
  SVDropStbReq req = {0};
3,622✔
1553
  int32_t      rcode = TSDB_CODE_SUCCESS;
3,622✔
1554
  SDecoder     decoder = {0};
3,622✔
1555
  SArray      *tbUidList = NULL;
3,622✔
1556

1557
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
3,622✔
1558
  pRsp->pCont = NULL;
3,622✔
1559
  pRsp->contLen = 0;
3,622✔
1560

1561
  // decode request
1562
  tDecoderInit(&decoder, pReq, len);
3,622✔
1563
  if (tDecodeSVDropStbReq(&decoder, &req) < 0) {
3,623!
1564
    rcode = TSDB_CODE_INVALID_MSG;
×
1565
    goto _exit;
×
1566
  }
1567

1568
  // process request
1569
  tbUidList = taosArrayInit(8, sizeof(int64_t));
3,626✔
1570
  if (tbUidList == NULL) goto _exit;
3,632!
1571
  if (metaDropSuperTable(pVnode->pMeta, ver, &req) < 0) {
3,632✔
1572
    rcode = terrno;
22✔
1573
    goto _exit;
22✔
1574
  }
1575

1576
  if (tqUpdateTbUidList(pVnode->pTq, tbUidList, false) < 0) {
3,613!
1577
    rcode = terrno;
×
1578
    goto _exit;
×
1579
  }
1580

1581
  if (tdProcessRSmaDrop(pVnode->pSma, &req) < 0) {
3,613!
1582
    rcode = terrno;
×
1583
    goto _exit;
×
1584
  }
1585

1586
  // return rsp
1587
_exit:
3,613✔
1588
  if (tbUidList) taosArrayDestroy(tbUidList);
3,635!
1589
  pRsp->code = rcode;
3,634✔
1590
  tDecoderClear(&decoder);
3,634✔
1591
  return 0;
3,635✔
1592
}
1593

1594
static int32_t vnodeProcessAlterTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
10,570✔
1595
  SVAlterTbReq  vAlterTbReq = {0};
10,570✔
1596
  SVAlterTbRsp  vAlterTbRsp = {0};
10,570✔
1597
  SDecoder      dc = {0};
10,570✔
1598
  int32_t       code = 0;
10,570✔
1599
  int32_t       lino = 0;
10,570✔
1600
  int32_t       ret;
1601
  SEncoder      ec = {0};
10,570✔
1602
  STableMetaRsp vMetaRsp = {0};
10,570✔
1603

1604
  pRsp->msgType = TDMT_VND_ALTER_TABLE_RSP;
10,570✔
1605
  pRsp->pCont = NULL;
10,570✔
1606
  pRsp->contLen = 0;
10,570✔
1607
  pRsp->code = TSDB_CODE_SUCCESS;
10,570✔
1608

1609
  tDecoderInit(&dc, pReq, len);
10,570✔
1610

1611
  // decode
1612
  if (tDecodeSVAlterTbReq(&dc, &vAlterTbReq) < 0) {
10,570!
1613
    vAlterTbRsp.code = TSDB_CODE_INVALID_MSG;
×
1614
    tDecoderClear(&dc);
×
1615
    goto _exit;
×
1616
  }
1617

1618
  // process
1619
  if (metaAlterTable(pVnode->pMeta, ver, &vAlterTbReq, &vMetaRsp) < 0) {
10,570✔
1620
    vAlterTbRsp.code = terrno;
911✔
1621
    tDecoderClear(&dc);
911✔
1622
    goto _exit;
911✔
1623
  }
1624
  tDecoderClear(&dc);
9,659✔
1625

1626
  if (NULL != vMetaRsp.pSchemas) {
9,659✔
1627
    vnodeUpdateMetaRsp(pVnode, &vMetaRsp);
5,790✔
1628
    vAlterTbRsp.pMeta = &vMetaRsp;
5,790✔
1629
  }
1630

1631
  if (vAlterTbReq.action == TSDB_ALTER_TABLE_UPDATE_TAG_VAL ||
9,659✔
1632
      vAlterTbReq.action == TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL) {
5,920✔
1633
    int64_t uid = metaGetTableEntryUidByName(pVnode->pMeta, vAlterTbReq.tbName);
3,749✔
1634
    if (uid == 0) {
3,749!
1635
      vError("vgId:%d, %s failed at %s:%d since table %s not found", TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
1636
             vAlterTbReq.tbName);
1637
      goto _exit;
×
1638
    }
1639

1640
    SArray *tbUids = taosArrayInit(4, sizeof(int64_t));
3,749✔
1641
    void   *p = taosArrayPush(tbUids, &uid);
3,749✔
1642
    TSDB_CHECK_NULL(p, code, lino, _exit, terrno);
3,749!
1643

1644
    vDebug("vgId:%d, remove tags value altered table:%s from query table list", TD_VID(pVnode), vAlterTbReq.tbName);
3,749✔
1645
    if ((code = tqUpdateTbUidList(pVnode->pTq, tbUids, false)) < 0) {
3,749!
1646
      vError("vgId:%d, failed to remove tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1647
    }
1648

1649
    vDebug("vgId:%d, try to add table:%s in query table list", TD_VID(pVnode), vAlterTbReq.tbName);
3,749✔
1650
    if ((code = tqUpdateTbUidList(pVnode->pTq, tbUids, true)) < 0) {
3,749!
1651
      vError("vgId:%d, failed to add tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1652
    }
1653

1654
    taosArrayDestroy(tbUids);
3,749✔
1655
  }
1656

1657
_exit:
5,910✔
1658
  taosArrayDestroy(vAlterTbReq.pMultiTag);
10,570✔
1659
  tEncodeSize(tEncodeSVAlterTbRsp, &vAlterTbRsp, pRsp->contLen, ret);
10,570!
1660
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
10,570✔
1661
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
10,570✔
1662
  if (tEncodeSVAlterTbRsp(&ec, &vAlterTbRsp) != 0) {
10,570!
1663
    vError("vgId:%d, failed to encode alter table response", TD_VID(pVnode));
×
1664
  }
1665

1666
  tEncoderClear(&ec);
10,570✔
1667
  if (vMetaRsp.pSchemas) {
10,570✔
1668
    taosMemoryFree(vMetaRsp.pSchemas);
5,790!
1669
    taosMemoryFree(vMetaRsp.pSchemaExt);
5,790!
1670
  }
1671
  if (vMetaRsp.pColRefs) {
10,570✔
1672
    taosMemoryFree(vMetaRsp.pColRefs);
329!
1673
  }
1674
  return 0;
10,570✔
1675
}
1676

1677
static int32_t vnodeProcessDropTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
15,837✔
1678
                                     SRpcMsg *pOriginRpc) {
1679
  SVDropTbBatchReq req = {0};
15,837✔
1680
  SVDropTbBatchRsp rsp = {0};
15,837✔
1681
  SDecoder         decoder = {0};
15,837✔
1682
  SEncoder         encoder = {0};
15,837✔
1683
  int32_t          ret;
1684
  SArray          *tbUids = NULL;
15,837✔
1685
  STbUidStore     *pStore = NULL;
15,837✔
1686
  SArray          *tbNames = NULL;
15,837✔
1687

1688
  pRsp->msgType = TDMT_VND_DROP_TABLE_RSP;
15,837✔
1689
  pRsp->pCont = NULL;
15,837✔
1690
  pRsp->contLen = 0;
15,837✔
1691
  pRsp->code = TSDB_CODE_SUCCESS;
15,837✔
1692

1693
  // decode req
1694
  tDecoderInit(&decoder, pReq, len);
15,837✔
1695
  ret = tDecodeSVDropTbBatchReq(&decoder, &req);
15,837✔
1696
  if (ret < 0) {
15,837!
1697
    terrno = TSDB_CODE_INVALID_MSG;
×
1698
    pRsp->code = terrno;
×
1699
    goto _exit;
×
1700
  }
1701

1702
  // process req
1703
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
15,837✔
1704
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(SVDropTbRsp));
15,837✔
1705
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
15,837✔
1706
  if (tbUids == NULL || rsp.pArray == NULL || tbNames == NULL) goto _exit;
15,837!
1707

1708
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
31,694✔
1709
    SVDropTbReq *pDropTbReq = req.pReqs + iReq;
15,857✔
1710
    SVDropTbRsp  dropTbRsp = {0};
15,857✔
1711
    tb_uid_t     tbUid = 0;
15,857✔
1712

1713
    /* code */
1714
    ret = metaDropTable2(pVnode->pMeta, ver, pDropTbReq);
15,857✔
1715
    if (ret < 0) {
15,857!
1716
      if (pDropTbReq->igNotExists && terrno == TSDB_CODE_TDB_TABLE_NOT_EXIST) {
×
1717
        dropTbRsp.code = TSDB_CODE_SUCCESS;
×
1718
      } else {
1719
        dropTbRsp.code = terrno;
×
1720
      }
1721
    } else {
1722
      dropTbRsp.code = TSDB_CODE_SUCCESS;
15,857✔
1723
      if (tbUid > 0) {
15,857!
1724
        if (tdFetchTbUidList(pVnode->pSma, &pStore, pDropTbReq->suid, tbUid) < 0) {
×
1725
          vError("vgId:%d, failed to fetch tbUid list", TD_VID(pVnode));
×
1726
        }
1727
      }
1728
    }
1729

1730
    if (taosArrayPush(rsp.pArray, &dropTbRsp) == NULL) {
31,714!
1731
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1732
      pRsp->code = terrno;
×
1733
      goto _exit;
×
1734
    }
1735

1736
    if (tsEnableAuditCreateTable) {
15,857!
1737
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
15,857!
1738
      if (str == NULL) {
15,857!
1739
        pRsp->code = terrno;
×
1740
        goto _exit;
×
1741
      }
1742
      tstrncpy(str, pDropTbReq->name, TSDB_TABLE_FNAME_LEN);
15,857✔
1743
      if (taosArrayPush(tbNames, &str) == NULL) {
15,857!
1744
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1745
        pRsp->code = terrno;
×
1746
        goto _exit;
×
1747
      }
1748
    }
1749
  }
1750

1751
  if (tqUpdateTbUidList(pVnode->pTq, tbUids, false) < 0) {
15,837!
1752
    vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(terrno));
×
1753
  }
1754

1755
  if (tdUpdateTbUidList(pVnode->pSma, pStore, false) < 0) {
15,837!
1756
    goto _exit;
×
1757
  }
1758

1759
  if (tsEnableAuditCreateTable) {
15,837!
1760
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
15,837✔
1761

1762
    SName name = {0};
15,837✔
1763
    if (tNameFromString(&name, pVnode->config.dbname, T_NAME_ACCT | T_NAME_DB) != 0) {
15,837!
1764
      vError("vgId:%d, failed to get name from string", TD_VID(pVnode));
×
1765
    }
1766

1767
    SStringBuilder sb = {0};
15,837✔
1768
    for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
31,694✔
1769
      char **key = (char **)taosArrayGet(tbNames, iReq);
15,857✔
1770
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
15,857✔
1771
      if (iReq < req.nReqs - 1) {
15,857✔
1772
        taosStringBuilderAppendChar(&sb, ',');
20✔
1773
      }
1774
      taosMemoryFreeClear(*key);
15,857!
1775
    }
1776

1777
    size_t len = 0;
15,837✔
1778
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
15,837✔
1779

1780
    if (pOriginRpc->info.conn.user != NULL && strlen(pOriginRpc->info.conn.user) > 0) {
15,837!
1781
      auditAddRecord(pOriginRpc, clusterId, "dropTable", name.dbname, "", keyJoined, len);
15,795✔
1782
    }
1783

1784
    taosStringBuilderDestroy(&sb);
15,837✔
1785
  }
1786

1787
_exit:
×
1788
  taosArrayDestroy(tbUids);
15,837✔
1789
  pStore = tdUidStoreFree(pStore);
15,837✔
1790
  tDecoderClear(&decoder);
15,837✔
1791
  tEncodeSize(tEncodeSVDropTbBatchRsp, &rsp, pRsp->contLen, ret);
15,837!
1792
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
15,837✔
1793
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
15,837✔
1794
  if (tEncodeSVDropTbBatchRsp(&encoder, &rsp) != 0) {
15,837!
1795
    vError("vgId:%d, failed to encode drop table batch response", TD_VID(pVnode));
×
1796
  }
1797
  tEncoderClear(&encoder);
15,837✔
1798
  taosArrayDestroy(rsp.pArray);
15,837✔
1799
  taosArrayDestroy(tbNames);
15,837✔
1800
  return 0;
15,837✔
1801
}
1802

1803
#ifdef BUILD_NO_CALL
1804
static int32_t vnodeDebugPrintSingleSubmitMsg(SMeta *pMeta, SSubmitBlk *pBlock, SSubmitMsgIter *msgIter,
1805
                                              const char *tags) {
1806
  SSubmitBlkIter blkIter = {0};
1807
  STSchema      *pSchema = NULL;
1808
  tb_uid_t       suid = 0;
1809
  STSRow        *row = NULL;
1810
  int32_t        rv = -1;
1811

1812
  tInitSubmitBlkIter(msgIter, pBlock, &blkIter);
1813
  if (blkIter.row == NULL) return 0;
1814

1815
  int32_t code = metaGetTbTSchemaNotNull(pMeta, msgIter->suid, TD_ROW_SVER(blkIter.row), 1,
1816
                                         &pSchema);  // TODO: use the real schema
1817
  if (TSDB_CODE_SUCCESS != code) {
1818
    printf("%s:%d no valid schema\n", tags, __LINE__);
1819
    return code;
1820
  }
1821

1822
  suid = msgIter->suid;
1823
  rv = TD_ROW_SVER(blkIter.row);
1824

1825
  char __tags[128] = {0};
1826
  snprintf(__tags, 128, "%s: uid %" PRIi64 " ", tags, msgIter->uid);
1827
  while ((row = tGetSubmitBlkNext(&blkIter))) {
1828
    tdSRowPrint(row, pSchema, __tags);
1829
  }
1830

1831
  taosMemoryFreeClear(pSchema);
1832

1833
  return TSDB_CODE_SUCCESS;
1834
}
1835
#endif
1836
typedef struct SSubmitReqConvertCxt {
1837
  SSubmitMsgIter msgIter;
1838
  SSubmitBlk    *pBlock;
1839
  SSubmitBlkIter blkIter;
1840
  STSRow        *pRow;
1841
  STSRowIter     rowIter;
1842
  SSubmitTbData *pTbData;
1843
  STSchema      *pTbSchema;
1844
  SArray        *pColValues;
1845
} SSubmitReqConvertCxt;
1846

1847
static int32_t vnodeResetTableCxt(SMeta *pMeta, SSubmitReqConvertCxt *pCxt) {
×
1848
  taosMemoryFreeClear(pCxt->pTbSchema);
×
1849
  int32_t code = metaGetTbTSchemaNotNull(pMeta, pCxt->msgIter.suid > 0 ? pCxt->msgIter.suid : pCxt->msgIter.uid,
×
1850
                                         pCxt->msgIter.sversion, 1, &pCxt->pTbSchema);
1851
  if (TSDB_CODE_SUCCESS != code) {
×
1852
    return code;
×
1853
  }
1854
  tdSTSRowIterInit(&pCxt->rowIter, pCxt->pTbSchema);
×
1855

1856
  tDestroySubmitTbData(pCxt->pTbData, TSDB_MSG_FLG_ENCODE);
×
1857
  if (NULL == pCxt->pTbData) {
×
1858
    pCxt->pTbData = taosMemoryCalloc(1, sizeof(SSubmitTbData));
×
1859
    if (NULL == pCxt->pTbData) {
×
1860
      return terrno;
×
1861
    }
1862
  }
1863
  pCxt->pTbData->flags = 0;
×
1864
  pCxt->pTbData->suid = pCxt->msgIter.suid;
×
1865
  pCxt->pTbData->uid = pCxt->msgIter.uid;
×
1866
  pCxt->pTbData->sver = pCxt->msgIter.sversion;
×
1867
  pCxt->pTbData->pCreateTbReq = NULL;
×
1868
  pCxt->pTbData->aRowP = taosArrayInit(128, POINTER_BYTES);
×
1869
  if (NULL == pCxt->pTbData->aRowP) {
×
1870
    return terrno;
×
1871
  }
1872

1873
  taosArrayDestroy(pCxt->pColValues);
×
1874
  pCxt->pColValues = taosArrayInit(pCxt->pTbSchema->numOfCols, sizeof(SColVal));
×
1875
  if (NULL == pCxt->pColValues) {
×
1876
    return terrno;
×
1877
  }
1878
  for (int32_t i = 0; i < pCxt->pTbSchema->numOfCols; ++i) {
×
1879
    SColVal val = COL_VAL_NONE(pCxt->pTbSchema->columns[i].colId, pCxt->pTbSchema->columns[i].type);
×
1880
    if (taosArrayPush(pCxt->pColValues, &val) == NULL) {
×
1881
      return terrno;
×
1882
    }
1883
  }
1884

1885
  return TSDB_CODE_SUCCESS;
×
1886
}
1887

1888
static void vnodeDestroySubmitReqConvertCxt(SSubmitReqConvertCxt *pCxt) {
×
1889
  taosMemoryFreeClear(pCxt->pTbSchema);
×
1890
  tDestroySubmitTbData(pCxt->pTbData, TSDB_MSG_FLG_ENCODE);
×
1891
  taosMemoryFreeClear(pCxt->pTbData);
×
1892
  taosArrayDestroy(pCxt->pColValues);
×
1893
}
×
1894

1895
static int32_t vnodeCellValConvertToColVal(STColumn *pCol, SCellVal *pCellVal, SColVal *pColVal) {
×
1896
  if (tdValTypeIsNone(pCellVal->valType)) {
×
1897
    pColVal->flag = CV_FLAG_NONE;
×
1898
    return TSDB_CODE_SUCCESS;
×
1899
  }
1900

1901
  if (tdValTypeIsNull(pCellVal->valType)) {
×
1902
    pColVal->flag = CV_FLAG_NULL;
×
1903
    return TSDB_CODE_SUCCESS;
×
1904
  }
1905

1906
  if (IS_VAR_DATA_TYPE(pCol->type)) {
×
1907
    if (IS_STR_DATA_BLOB(pCol->type)) {
×
1908
      pColVal->value.nData = blobDataLen(pCellVal->val);
×
1909
      pColVal->value.pData = (uint8_t *)blobDataVal(pCellVal->val);
×
1910

1911
    } else {
1912
      pColVal->value.nData = varDataLen(pCellVal->val);
×
1913
      pColVal->value.pData = (uint8_t *)varDataVal(pCellVal->val);
×
1914
    }
1915
  } else if (TSDB_DATA_TYPE_FLOAT == pCol->type) {
×
1916
    float f = GET_FLOAT_VAL(pCellVal->val);
×
1917
    valueSetDatum(&pColVal->value, pCol->type, &f, sizeof(f));
×
1918
  } else if (TSDB_DATA_TYPE_DOUBLE == pCol->type) {
×
1919
    taosSetPInt64Aligned(&pColVal->value.val, (int64_t *)pCellVal->val);
×
1920
  } else {
1921
    valueSetDatum(&pColVal->value, pCol->type, pCellVal->val, tDataTypes[pCol->type].bytes);
×
1922
  }
1923

1924
  pColVal->flag = CV_FLAG_VALUE;
×
1925
  return TSDB_CODE_SUCCESS;
×
1926
}
1927

1928
static int32_t vnodeTSRowConvertToColValArray(SSubmitReqConvertCxt *pCxt) {
×
1929
  int32_t code = TSDB_CODE_SUCCESS;
×
1930
  tdSTSRowIterReset(&pCxt->rowIter, pCxt->pRow);
×
1931
  for (int32_t i = 0; TSDB_CODE_SUCCESS == code && i < pCxt->pTbSchema->numOfCols; ++i) {
×
1932
    STColumn *pCol = pCxt->pTbSchema->columns + i;
×
1933
    SCellVal  cellVal = {0};
×
1934
    if (!tdSTSRowIterFetch(&pCxt->rowIter, pCol->colId, pCol->type, &cellVal)) {
×
1935
      break;
×
1936
    }
1937
    code = vnodeCellValConvertToColVal(pCol, &cellVal, (SColVal *)taosArrayGet(pCxt->pColValues, i));
×
1938
  }
1939
  return code;
×
1940
}
1941

1942
static int32_t vnodeDecodeCreateTbReq(SSubmitReqConvertCxt *pCxt) {
×
1943
  if (pCxt->msgIter.schemaLen <= 0) {
×
1944
    return TSDB_CODE_SUCCESS;
×
1945
  }
1946

1947
  pCxt->pTbData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
×
1948
  if (NULL == pCxt->pTbData->pCreateTbReq) {
×
1949
    return terrno;
×
1950
  }
1951

1952
  SDecoder decoder = {0};
×
1953
  tDecoderInit(&decoder, (uint8_t *)pCxt->pBlock->data, pCxt->msgIter.schemaLen);
×
1954
  int32_t code = tDecodeSVCreateTbReq(&decoder, pCxt->pTbData->pCreateTbReq);
×
1955
  tDecoderClear(&decoder);
×
1956

1957
  return code;
×
1958
}
1959

1960
static int32_t vnodeSubmitReqConvertToSubmitReq2(SVnode *pVnode, SSubmitReq *pReq, SSubmitReq2 *pReq2) {
×
1961
  pReq2->aSubmitTbData = taosArrayInit(128, sizeof(SSubmitTbData));
×
1962
  if (NULL == pReq2->aSubmitTbData) {
×
1963
    return terrno;
×
1964
  }
1965

1966
  SSubmitReqConvertCxt cxt = {0};
×
1967

1968
  int32_t code = tInitSubmitMsgIter(pReq, &cxt.msgIter);
×
1969
  while (TSDB_CODE_SUCCESS == code) {
×
1970
    code = tGetSubmitMsgNext(&cxt.msgIter, &cxt.pBlock);
×
1971
    if (TSDB_CODE_SUCCESS == code) {
×
1972
      if (NULL == cxt.pBlock) {
×
1973
        break;
×
1974
      }
1975
      code = vnodeResetTableCxt(pVnode->pMeta, &cxt);
×
1976
    }
1977
    if (TSDB_CODE_SUCCESS == code) {
×
1978
      code = tInitSubmitBlkIter(&cxt.msgIter, cxt.pBlock, &cxt.blkIter);
×
1979
    }
1980
    if (TSDB_CODE_SUCCESS == code) {
×
1981
      code = vnodeDecodeCreateTbReq(&cxt);
×
1982
    }
1983
    while (TSDB_CODE_SUCCESS == code && (cxt.pRow = tGetSubmitBlkNext(&cxt.blkIter)) != NULL) {
×
1984
      code = vnodeTSRowConvertToColValArray(&cxt);
×
1985
      if (TSDB_CODE_SUCCESS == code) {
×
1986
        SRow            **pNewRow = taosArrayReserve(cxt.pTbData->aRowP, 1);
×
1987

1988
        SRowBuildScanInfo sinfo = {0};
×
1989
        code = tRowBuild(cxt.pColValues, cxt.pTbSchema, pNewRow, &sinfo);
×
1990
      }
1991
    }
1992
    if (TSDB_CODE_SUCCESS == code) {
×
1993
      code = (NULL == taosArrayPush(pReq2->aSubmitTbData, cxt.pTbData) ? terrno : TSDB_CODE_SUCCESS);
×
1994
    }
1995
    if (TSDB_CODE_SUCCESS == code) {
×
1996
      taosMemoryFreeClear(cxt.pTbData);
×
1997
    }
1998
  }
1999

2000
  vnodeDestroySubmitReqConvertCxt(&cxt);
×
2001
  return code;
×
2002
}
2003

2004
static int32_t vnodeRebuildSubmitReqMsg(SSubmitReq2 *pSubmitReq, void **ppMsg) {
×
2005
  int32_t  code = TSDB_CODE_SUCCESS;
×
2006
  char    *pMsg = NULL;
×
2007
  uint32_t msglen = 0;
×
2008
  tEncodeSize(tEncodeSubmitReq, pSubmitReq, msglen, code);
×
2009
  if (TSDB_CODE_SUCCESS == code) {
×
2010
    pMsg = taosMemoryMalloc(msglen);
×
2011
    if (NULL == pMsg) {
×
2012
      code = terrno;
×
2013
    }
2014
  }
2015
  if (TSDB_CODE_SUCCESS == code) {
×
2016
    SEncoder encoder;
2017
    tEncoderInit(&encoder, (uint8_t *)pMsg, msglen);
×
2018
    code = tEncodeSubmitReq(&encoder, pSubmitReq);
×
2019
    tEncoderClear(&encoder);
×
2020
  }
2021
  if (TSDB_CODE_SUCCESS == code) {
×
2022
    *ppMsg = pMsg;
×
2023
  }
2024
  return code;
×
2025
}
2026

2027
static int32_t buildExistSubTalbeRsp(SVnode *pVnode, SSubmitTbData *pSubmitTbData, STableMetaRsp **ppRsp) {
×
2028
  int32_t code = 0;
×
2029
  int32_t lino = 0;
×
2030

2031
  SMetaEntry *pEntry = NULL;
×
2032
  code = metaFetchEntryByUid(pVnode->pMeta, pSubmitTbData->suid, &pEntry);
×
2033
  if (code) {
×
2034
    vError("vgId:%d, table uid:%" PRId64 " not exists, line:%d", TD_VID(pVnode), pSubmitTbData->uid, __LINE__);
×
2035
    TSDB_CHECK_CODE(code, lino, _exit);
×
2036
  }
2037
    if (pEntry->type != TSDB_SUPER_TABLE) {
×
2038
    vError("vgId:%d, table uid:%" PRId64 " exists, but is not super table, line:%d", TD_VID(pVnode), pSubmitTbData->uid, __LINE__);
×
2039
    code = TSDB_CODE_STREAM_INSERT_SCHEMA_NOT_MATCH;
×
2040
    TSDB_CHECK_CODE(code, lino, _exit);
×
2041
  }
2042

2043
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
×
2044
  if (NULL == *ppRsp) {
×
2045
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2046
  }
2047
  (*ppRsp)->suid = pSubmitTbData->suid;
×
2048
  (*ppRsp)->tuid = pSubmitTbData->uid;
×
2049
  (*ppRsp)->sversion = pEntry->stbEntry.schemaRow.version;
×
2050
  (*ppRsp)->vgId = pVnode->config.vgId;
×
2051
  (*ppRsp)->numOfColumns = pEntry->stbEntry.schemaRow.nCols;
×
2052
  (*ppRsp)->numOfTags = pEntry->stbEntry.schemaTag.nCols;
×
2053
  (*ppRsp)->pSchemas =
×
2054
      taosMemoryCalloc(pEntry->stbEntry.schemaRow.nCols + pEntry->stbEntry.schemaTag.nCols, sizeof(SSchema));
×
2055
  if (NULL == (*ppRsp)->pSchemas) {
×
2056
    taosMemoryFree(*ppRsp);
×
2057
    *ppRsp = NULL;
×
2058
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2059
  }
2060
  memcpy((*ppRsp)->pSchemas, pEntry->stbEntry.schemaRow.pSchema, pEntry->stbEntry.schemaRow.nCols * sizeof(SSchema));
×
2061
  memcpy((*ppRsp)->pSchemas + pEntry->stbEntry.schemaRow.nCols, pEntry->stbEntry.schemaTag.pSchema,
×
2062
         pEntry->stbEntry.schemaTag.nCols * sizeof(SSchema));
×
2063
  if (pEntry->pExtSchemas != NULL) {
×
2064
    (*ppRsp)->pSchemaExt = taosMemoryCalloc(pEntry->stbEntry.schemaRow.nCols, sizeof(SSchemaExt));
×
2065
    if (NULL == (*ppRsp)->pSchemaExt) {
×
2066
      taosMemoryFree((*ppRsp)->pSchemas);
×
2067
      taosMemoryFree(*ppRsp);
×
2068
      *ppRsp = NULL;
×
2069
      TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2070
    }
2071
    memcpy((*ppRsp)->pSchemaExt, pEntry->pExtSchemas, pEntry->stbEntry.schemaRow.nCols * sizeof(SSchemaExt));
×
2072
  }
2073

2074
  if (pEntry->stbEntry.schemaRow.version != pSubmitTbData->sver) {
×
2075
    TSDB_CHECK_CODE(code = TSDB_CODE_TDB_TABLE_ALREADY_EXIST, lino, _exit);
×
2076
  }
2077
_exit:
×
2078
  metaFetchEntryFree(&pEntry);
×
2079
  if (code != TSDB_CODE_SUCCESS) {
×
2080
    vError("vgId:%d, failed to build exist sub table response, code:%d, line:%d", TD_VID(pVnode), code, lino);
×
2081
  }
2082
  return code;
×
2083
}
2084

2085
static int32_t buildExistNormalTalbeRsp(SVnode *pVnode, SSubmitTbData *pSubmitTbData, STableMetaRsp **ppRsp) {
×
2086
  int32_t code = 0;
×
2087
  int32_t lino = 0;
×
2088

2089
  SMetaEntry *pEntry = NULL;
×
2090
  code = metaFetchEntryByUid(pVnode->pMeta, pSubmitTbData->uid, &pEntry);
×
2091
  if (code) {
×
2092
    vError("vgId:%d, table uid:%" PRId64 " not exists, line:%d", TD_VID(pVnode), pSubmitTbData->uid, __LINE__);
×
2093
    TSDB_CHECK_CODE(code, lino, _exit);
×
2094
  }
2095
  if (pEntry->type != TSDB_NORMAL_TABLE) {
×
2096
    vError("vgId:%d, table uid:%" PRId64 " exists, but is not normal table, line:%d", TD_VID(pVnode), pSubmitTbData->uid, __LINE__);
×
2097
    code = TSDB_CODE_STREAM_INSERT_SCHEMA_NOT_MATCH;
×
2098
    TSDB_CHECK_CODE(code, lino, _exit);
×
2099
  }
2100

2101
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
×
2102
  if (NULL == *ppRsp) {
×
2103
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2104
  }
2105

2106
  (*ppRsp)->tuid = pEntry->uid;
×
2107
  (*ppRsp)->vgId = pVnode->config.vgId;
×
2108
  (*ppRsp)->sversion = pEntry->ntbEntry.schemaRow.version;
×
2109
  (*ppRsp)->numOfColumns = pEntry->ntbEntry.schemaRow.nCols;
×
2110
  (*ppRsp)->pSchemas = taosMemoryCalloc(pEntry->ntbEntry.schemaRow.nCols, sizeof(SSchema));
×
2111
  if (NULL == (*ppRsp)->pSchemas) {
×
2112
    taosMemoryFree(*ppRsp);
×
2113
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2114
  }
2115
  memcpy((*ppRsp)->pSchemas, pEntry->ntbEntry.schemaRow.pSchema, pEntry->ntbEntry.schemaRow.nCols * sizeof(SSchema));
×
2116
  if (pEntry->pExtSchemas != NULL) {
×
2117
    (*ppRsp)->pSchemaExt = taosMemoryCalloc(pEntry->ntbEntry.schemaRow.nCols, sizeof(SSchemaExt));
×
2118
    if (NULL == (*ppRsp)->pSchemaExt) {
×
2119
      taosMemoryFree((*ppRsp)->pSchemas);
×
2120
      taosMemoryFree(*ppRsp);
×
2121
      TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2122
    }
2123
    memcpy((*ppRsp)->pSchemaExt, pEntry->pExtSchemas, pEntry->ntbEntry.schemaRow.nCols * sizeof(SSchemaExt));
×
2124
  }
2125

2126
_exit:
×
2127
  metaFetchEntryFree(&pEntry);
×
2128
  if (code != TSDB_CODE_SUCCESS) {
×
2129
    vError("vgId:%d, failed to build exist normal table response, code:%d, line:%d", TD_VID(pVnode), code, lino);
×
2130
  }
2131
  return code;
×
2132
}
2133

2134
static int32_t buildExistTalbeInStreamRsp(SVnode *pVnode, SSubmitTbData *pSubmitTbData, STableMetaRsp **ppRsp) {
1,021✔
2135
  if (pSubmitTbData->pCreateTbReq->flags & TD_CREATE_NORMAL_TB_IN_STREAM) {
1,021!
2136
    int32_t code = buildExistNormalTalbeRsp(pVnode, pSubmitTbData, ppRsp);
×
2137
    if (code) {
×
2138
      vError("vgId:%d, table uid:%" PRId64 " not exists, line:%d", TD_VID(pVnode), pSubmitTbData->uid, __LINE__);
×
2139
      return code;
×
2140
    }
2141
    return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
×
2142
  } else if (pSubmitTbData->pCreateTbReq->flags & TD_CREATE_SUB_TB_IN_STREAM) {
1,021!
2143
    return buildExistSubTalbeRsp(pVnode, pSubmitTbData, ppRsp);
×
2144
  }
2145
  return TSDB_CODE_SUCCESS;
1,021✔
2146
}
2147

2148
static int32_t vnodeProcessSubmitReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
26,527,596✔
2149
                                     SRpcMsg *pOriginalMsg) {
2150
  int32_t code = 0;
26,527,596✔
2151
  int32_t lino = 0;
26,527,596✔
2152
  terrno = 0;
26,527,596✔
2153
  uint8_t hasBlob = 0;
26,527,728✔
2154

2155
  SSubmitReq2 *pSubmitReq = &(SSubmitReq2){0};
26,527,728✔
2156
  SSubmitRsp2 *pSubmitRsp = &(SSubmitRsp2){0};
26,527,728✔
2157
  SArray      *newTbUids = NULL;
26,527,728✔
2158
  int32_t      ret;
2159
  SEncoder     ec = {0};
26,527,728✔
2160

2161
  pRsp->code = TSDB_CODE_SUCCESS;
26,527,728✔
2162

2163
  void           *pAllocMsg = NULL;
26,527,728✔
2164
  SSubmitReq2Msg *pMsg = (SSubmitReq2Msg *)pReq;
26,527,728✔
2165
  SDecoder dc = {0};
26,527,728✔
2166
  if (0 == taosHton64(pMsg->version)) {
26,527,728!
2167
    code = vnodeSubmitReqConvertToSubmitReq2(pVnode, (SSubmitReq *)pMsg, pSubmitReq);
×
2168
    if (TSDB_CODE_SUCCESS == code) {
×
2169
      code = vnodeRebuildSubmitReqMsg(pSubmitReq, &pReq);
×
2170
    }
2171
    if (TSDB_CODE_SUCCESS == code) {
29!
2172
      pAllocMsg = pReq;
×
2173
    }
2174
    TSDB_CHECK_CODE(code, lino, _exit);
29!
2175
  } else {
2176
    // decode
2177
    pReq = POINTER_SHIFT(pReq, sizeof(SSubmitReq2Msg));
26,527,682✔
2178
    len -= sizeof(SSubmitReq2Msg);
26,527,682✔
2179

2180
    tDecoderInit(&dc, pReq, len);
26,527,682✔
2181
    if (tDecodeSubmitReq(&dc, pSubmitReq, NULL) < 0) {
26,527,584!
2182
      code = TSDB_CODE_INVALID_MSG;
×
2183
      TSDB_CHECK_CODE(code, lino, _exit);
×
2184
    }
2185
  }
2186

2187
  // scan
2188
  TSKEY now = taosGetTimestamp(pVnode->config.tsdbCfg.precision);
26,527,727✔
2189
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * pVnode->config.tsdbCfg.keep2;
26,527,585✔
2190
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
26,527,585✔
2191
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
53,778,792✔
2192
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
27,252,476✔
2193

2194
    if (pSubmitTbData->pCreateTbReq && pSubmitTbData->pCreateTbReq->uid == 0) {
27,252,487!
2195
      code = TSDB_CODE_INVALID_MSG;
×
2196
      TSDB_CHECK_CODE(code, lino, _exit);
×
2197
    }
2198

2199
    if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
27,252,487✔
2200
      if (TARRAY_SIZE(pSubmitTbData->aCol) <= 0) {
10,134!
2201
        code = TSDB_CODE_INVALID_MSG;
×
2202
        TSDB_CHECK_CODE(code, lino, _exit);
×
2203
      }
2204

2205
      SColData *colDataArr = TARRAY_DATA(pSubmitTbData->aCol);
10,134✔
2206
      SRowKey   lastKey;
2207
      tColDataArrGetRowKey(colDataArr, TARRAY_SIZE(pSubmitTbData->aCol), 0, &lastKey);
10,134✔
2208
      for (int32_t iRow = 1; iRow < colDataArr[0].nVal; iRow++) {
9,637,524✔
2209
        SRowKey key;
2210
        tColDataArrGetRowKey(TARRAY_DATA(pSubmitTbData->aCol), TARRAY_SIZE(pSubmitTbData->aCol), iRow, &key);
9,629,036✔
2211
        if (tRowKeyCompare(&lastKey, &key) >= 0) {
9,627,139!
2212
          code = TSDB_CODE_INVALID_MSG;
×
2213
          vError("vgId:%d %s failed 1 since %s, version:%" PRId64, TD_VID(pVnode), __func__, tstrerror(terrno), ver);
×
2214
          TSDB_CHECK_CODE(code, lino, _exit);
×
2215
        }
2216
      }
2217
    } else {
2218
      int32_t nRow = TARRAY_SIZE(pSubmitTbData->aRowP);
27,242,353✔
2219
      SRow  **aRow = (SRow **)TARRAY_DATA(pSubmitTbData->aRowP);
27,242,353✔
2220
      SRowKey lastRowKey;
2221
      for (int32_t iRow = 0; iRow < nRow; ++iRow) {
1,613,528,313✔
2222
#ifndef NO_UNALIGNED_ACCESS
2223
        if (aRow[iRow]->ts < minKey || aRow[iRow]->ts > maxKey) {
1,586,285,594!
2224
#else
2225
        TSKEY ts = taosGetInt64Aligned(&(aRow[iRow]->ts));
2226
        if (ts < minKey || ts > maxKey) {
2227
#endif
2228
          code = TSDB_CODE_INVALID_MSG;
29,953✔
2229
          vError("vgId:%d %s failed 2 since %s, version:%" PRId64, TD_VID(pVnode), __func__, tstrerror(code), ver);
29,953!
2230
          TSDB_CHECK_CODE(code, lino, _exit);
×
2231
        }
2232
        if (iRow == 0) {
1,586,254,581✔
2233
          tRowGetKey(aRow[iRow], &lastRowKey);
54,484,624✔
2234
        } else {
2235
          SRowKey rowKey;
2236
          tRowGetKey(aRow[iRow], &rowKey);
2,147,483,647✔
2237

2238
          if (tRowKeyCompare(&lastRowKey, &rowKey) >= 0) {
1,559,008,415!
2239
            code = TSDB_CODE_INVALID_MSG;
×
2240
            vError("vgId:%d %s failed 3 since %s, version:%" PRId64, TD_VID(pVnode), __func__, tstrerror(code), ver);
×
2241
            TSDB_CHECK_CODE(code, lino, _exit);
17,040!
2242
          }
2243
          lastRowKey = rowKey;
1,559,036,899✔
2244
        }
2245
      }
2246
    }
2247
  }
2248

2249
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
53,779,067✔
2250
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
27,252,483✔
2251

2252
    if (pSubmitTbData->pCreateTbReq) {
27,252,528✔
2253
      pSubmitTbData->uid = pSubmitTbData->pCreateTbReq->uid;
80,155✔
2254
    } else {
2255
      SMetaInfo info = {0};
27,172,373✔
2256

2257
      code = metaGetInfo(pVnode->pMeta, pSubmitTbData->uid, &info, NULL);
27,172,373✔
2258
      if (code) {
27,172,495!
2259
        code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
2260
        vWarn("vgId:%d, table uid:%" PRId64 " not exists", TD_VID(pVnode), pSubmitTbData->uid);
×
2261
        TSDB_CHECK_CODE(code, lino, _exit);
×
2262
      }
2263

2264
      if (info.suid != pSubmitTbData->suid) {
27,172,495!
2265
        vError("vgId:%d, submit uid:%" PRId64 " submit suid:%" PRId64 " info suid:%" PRId64 " not match, line:%d",
×
2266
               TD_VID(pVnode), pSubmitTbData->uid, pSubmitTbData->suid, info.suid, __LINE__);
2267
        code = TSDB_CODE_INVALID_MSG;
×
2268
        TSDB_CHECK_CODE(code, lino, _exit);
×
2269
      }
2270

2271
      if (info.suid) {
27,172,495✔
2272
        if (metaGetInfo(pVnode->pMeta, info.suid, &info, NULL) != 0) {
25,705,446!
2273
          vWarn("vgId:%d, table uid:%" PRId64 " not exists", TD_VID(pVnode), info.suid);
×
2274
        }
2275
      }
2276

2277
      if (pSubmitTbData->sver != info.skmVer) {
27,172,596!
2278
        code = TSDB_CODE_TDB_INVALID_TABLE_SCHEMA_VER;
×
2279
        TSDB_CHECK_CODE(code, lino, _exit);
×
2280
      }
2281
    }
2282
    if (pSubmitTbData->flags & SUBMIT_REQ_WITH_BLOB) hasBlob = 1;
27,252,751!
2283

2284
    if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
27,252,751✔
2285
      int32_t   nColData = TARRAY_SIZE(pSubmitTbData->aCol);
10,134✔
2286
      SColData *aColData = (SColData *)TARRAY_DATA(pSubmitTbData->aCol);
10,134✔
2287

2288
      if (nColData <= 0) {
10,134!
2289
        code = TSDB_CODE_INVALID_MSG;
×
2290
        TSDB_CHECK_CODE(code, lino, _exit);
×
2291
      }
2292

2293
      if (aColData[0].cid != PRIMARYKEY_TIMESTAMP_COL_ID || aColData[0].type != TSDB_DATA_TYPE_TIMESTAMP ||
10,134!
2294
          aColData[0].nVal <= 0) {
10,135!
2295
        code = TSDB_CODE_INVALID_MSG;
×
2296
        TSDB_CHECK_CODE(code, lino, _exit);
×
2297
      }
2298

2299
      for (int32_t j = 1; j < nColData; j++) {
30,641✔
2300
        if (aColData[j].nVal != aColData[0].nVal) {
20,507!
2301
          code = TSDB_CODE_INVALID_MSG;
×
2302
          TSDB_CHECK_CODE(code, lino, _exit);
×
2303
        }
2304
      }
2305
    }
2306
  }
2307

2308
  vGDebug(pOriginalMsg ? &pOriginalMsg->info.traceId : NULL, "vgId:%d, index:%" PRId64 ", submit block, rows:%d",
26,526,584!
2309
          TD_VID(pVnode), ver, (int32_t)taosArrayGetSize(pSubmitReq->aSubmitTbData));
2310

2311
  // loop to handle
2312
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
53,780,472✔
2313
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
27,252,654✔
2314

2315
    // create table
2316
    if (pSubmitTbData->pCreateTbReq) {
27,252,659✔
2317
      // alloc if need
2318
      if (pSubmitRsp->aCreateTbRsp == NULL &&
80,150✔
2319
          (pSubmitRsp->aCreateTbRsp = taosArrayInit(TARRAY_SIZE(pSubmitReq->aSubmitTbData), sizeof(SVCreateTbRsp))) ==
67,674!
2320
              NULL) {
2321
        code = terrno;
×
2322
        TSDB_CHECK_CODE(code, lino, _exit);
8!
2323
      }
2324

2325
      SVCreateTbRsp *pCreateTbRsp = taosArrayReserve(pSubmitRsp->aCreateTbRsp, 1);
80,150✔
2326

2327
      // create table
2328
      if (metaCreateTable2(pVnode->pMeta, ver, pSubmitTbData->pCreateTbReq, &pCreateTbRsp->pMeta) == 0) {
80,147✔
2329
        // create table success
2330

2331
        if (newTbUids == NULL &&
139,945!
2332
            (newTbUids = taosArrayInit(TARRAY_SIZE(pSubmitReq->aSubmitTbData), sizeof(int64_t))) == NULL) {
66,651✔
2333
          code = terrno;
×
2334
          TSDB_CHECK_CODE(code, lino, _exit);
×
2335
        }
2336

2337
        if (taosArrayPush(newTbUids, &pSubmitTbData->uid) == NULL) {
146,588!
2338
          code = terrno;
×
2339
          TSDB_CHECK_CODE(code, lino, _exit);
×
2340
        }
2341

2342
        if (pCreateTbRsp->pMeta) {
73,294!
2343
          vnodeUpdateMetaRsp(pVnode, pCreateTbRsp->pMeta);
73,294✔
2344
        }
2345
      } else {  // create table failed
2346
        if (terrno != TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
6,838✔
2347
          code = terrno;
8✔
2348
          vError("vgId:%d failed to create table:%s, code:%s", TD_VID(pVnode), pSubmitTbData->pCreateTbReq->name,
8!
2349
                 tstrerror(terrno));
2350
          TSDB_CHECK_CODE(code, lino, _exit);
8!
2351
        }
2352
        terrno = 0;
6,830✔
2353
        pSubmitTbData->uid = pSubmitTbData->pCreateTbReq->uid;  // update uid if table exist for using below
6,829✔
2354

2355
        // stream: get sver from meta, write to pCreateTbRsp, and need to check crateTbReq is same as meta.
2356
        if (i == 0) {
6,829✔
2357
          // In the streaming scenario, multiple grouped req requests will only operate on the same write table, and
2358
          // only the first one needs to be processed.
2359
          code = buildExistTalbeInStreamRsp(pVnode, pSubmitTbData, &pCreateTbRsp->pMeta);
1,021✔
2360
          if (code) {
1,021!
2361
            vInfo("vgId:%d failed to create table in stream:%s, code(0x%0x):%s", TD_VID(pVnode),
×
2362
                  pSubmitTbData->pCreateTbReq->name,  code, tstrerror(code));
2363
            TSDB_CHECK_CODE(code, lino, _exit);
×
2364
          }
2365
        }
2366
      }
2367
    }
2368

2369
    if (hasBlob) {
27,252,632!
2370
      code = vnodeSubmitBlobData(pVnode, pSubmitTbData);
×
2371
      if (code) goto _exit;
×
2372
    }
2373
    // insert data
2374
    int32_t affectedRows;
2375
    code = tsdbInsertTableData(pVnode->pTsdb, ver, pSubmitTbData, &affectedRows);
27,252,632✔
2376
    TSDB_CHECK_CODE(code, lino, _exit);
27,252,699!
2377

2378
    code = metaUpdateChangeTimeWithLock(pVnode->pMeta, pSubmitTbData->uid, pSubmitTbData->ctimeMs);
27,252,699✔
2379
    TSDB_CHECK_CODE(code, lino, _exit);
27,252,684!
2380

2381
    pSubmitRsp->affectedRows += affectedRows;
27,252,684✔
2382
  }
2383

2384
  // update the affected table uid list
2385
  if (taosArrayGetSize(newTbUids) > 0) {
26,527,818✔
2386
    vDebug("vgId:%d, add %d table into query table list in handling submit", TD_VID(pVnode),
66,651✔
2387
           (int32_t)taosArrayGetSize(newTbUids));
2388
    if (tqUpdateTbUidList(pVnode->pTq, newTbUids, true) != 0) {
66,651!
2389
      vError("vgId:%d, failed to update tbUid list", TD_VID(pVnode));
×
2390
    }
2391
  }
2392

2393
_exit:
26,527,788✔
2394
  // message
2395
  pRsp->code = code;
26,527,796✔
2396
  tEncodeSize(tEncodeSSubmitRsp2, pSubmitRsp, pRsp->contLen, ret);
26,527,796!
2397
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
26,527,683✔
2398
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
26,527,732✔
2399
  if (tEncodeSSubmitRsp2(&ec, pSubmitRsp) < 0) {
26,527,769!
2400
    vError("vgId:%d, failed to encode submit response", TD_VID(pVnode));
×
2401
  }
2402
  tEncoderClear(&ec);
26,527,788✔
2403

2404
  // update statistics
2405
  (void)atomic_add_fetch_64(&pVnode->statis.nInsert, pSubmitRsp->affectedRows);
26,527,840✔
2406
  (void)atomic_add_fetch_64(&pVnode->statis.nInsertSuccess, pSubmitRsp->affectedRows);
26,527,872✔
2407
  (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsert, 1);
26,527,874✔
2408

2409
  // update metrics
2410
  METRICS_UPDATE(pVnode->writeMetrics.total_requests, METRIC_LEVEL_LOW, 1);
26,527,877!
2411
  METRICS_UPDATE(pVnode->writeMetrics.total_rows, METRIC_LEVEL_HIGH, pSubmitRsp->affectedRows);
26,527,877!
2412
  METRICS_UPDATE(pVnode->writeMetrics.total_bytes, METRIC_LEVEL_LOW, pMsg->header.contLen);
26,527,877!
2413

2414
  if (tsEnableMonitor && tsMonitorFqdn[0] != 0 && tsMonitorPort != 0 && pSubmitRsp->affectedRows > 0 &&
26,527,877!
2415
      strlen(pOriginalMsg->info.conn.user) > 0 && tsInsertCounter != NULL) {
100!
2416
    const char *sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT_AFFECTED_ROWS,
100✔
2417
                                   pVnode->monitor.strClusterId,
100✔
2418
                                   pVnode->monitor.strDnodeId,
100✔
2419
                                   tsLocalEp,
2420
                                   pVnode->monitor.strVgId,
100✔
2421
                                   pOriginalMsg->info.conn.user,
100✔
2422
                                   "Success"};
2423
    int         tv = taos_counter_add(tsInsertCounter, pSubmitRsp->affectedRows, sample_labels);
100✔
2424
  }
2425

2426
  if (code == 0) {
26,527,877✔
2427
    (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
26,527,698✔
2428
    code = tdProcessRSmaSubmit(pVnode->pSma, ver, pSubmitReq, pReq, len);
26,527,871✔
2429
  }
2430
  /*
2431
  if (code == 0) {
2432
    atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
2433
    code = tdProcessRSmaSubmit(pVnode->pSma, ver, pSubmitReq, pReq, len);
2434

2435
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT, pVnode->monitor.strClusterId,
2436
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2437
                                          pOriginalMsg->info.conn.user, "Success"};
2438
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2439
  }
2440
  else{
2441
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT, pVnode->monitor.strClusterId,
2442
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2443
                                        pOriginalMsg->info.conn.user, "Failed"};
2444
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2445
  }
2446
  */
2447

2448
  // clear
2449
  taosArrayDestroy(newTbUids);
26,527,938✔
2450
  tDestroySubmitReq(pSubmitReq, 0 == taosHton64(pMsg->version) ? TSDB_MSG_FLG_CMPT : TSDB_MSG_FLG_DECODE);
26,527,740!
2451
  tDestroySSubmitRsp2(pSubmitRsp, TSDB_MSG_FLG_ENCODE);
26,527,779✔
2452

2453
  if (code) {
26,527,691✔
2454
    terrno = code;
8✔
2455
    if (code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
8!
2456
      vInfo("vgId:%d, failed to process submit request since %s, lino:%d, version:%" PRId64, TD_VID(pVnode),
×
2457
            tstrerror(code), lino, ver);
2458

2459
    } else {
2460
      vError("vgId:%d, failed to process submit request since %s, lino:%d, version:%" PRId64, TD_VID(pVnode),
8!
2461
             tstrerror(code), lino, ver);
2462
    }
2463
  }
2464

2465
  taosMemoryFree(pAllocMsg);
26,527,603!
2466
  tDecoderClear(&dc);
26,527,663✔
2467

2468
  return code;
26,527,843✔
2469
}
2470

2471
static int32_t vnodeConsolidateAlterHashRange(SVnode *pVnode, int64_t ver) {
56✔
2472
  int32_t code = TSDB_CODE_SUCCESS;
56✔
2473

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

2477
  // TODO: trim meta of tables from TDB per hash range [pVnode->config.hashBegin, pVnode->config.hashEnd]
2478
  code = metaTrimTables(pVnode->pMeta, ver);
56✔
2479

2480
  return code;
56✔
2481
}
2482

2483
static int32_t vnodeProcessAlterConfirmReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
15,099✔
2484
  vInfo("vgId:%d, vnode handle msgType:alter-confirm, alter confirm msg is processed", TD_VID(pVnode));
15,099!
2485
  int32_t code = TSDB_CODE_SUCCESS;
15,099✔
2486
  if (!pVnode->config.hashChange) {
15,099✔
2487
    goto _exit;
15,043✔
2488
  }
2489

2490
  code = vnodeConsolidateAlterHashRange(pVnode, ver);
56✔
2491
  if (code < 0) {
56!
2492
    vError("vgId:%d, failed to consolidate alter hashrange since %s. version:%" PRId64, TD_VID(pVnode), terrstr(), ver);
×
2493
    goto _exit;
×
2494
  }
2495
  pVnode->config.hashChange = false;
56✔
2496

2497
_exit:
15,099✔
2498
  pRsp->msgType = TDMT_VND_ALTER_CONFIRM_RSP;
15,099✔
2499
  pRsp->code = code;
15,099✔
2500
  pRsp->pCont = NULL;
15,099✔
2501
  pRsp->contLen = 0;
15,099✔
2502

2503
  return code;
15,099✔
2504
}
2505

2506
extern int32_t tsdbDisableAndCancelAllBgTask(STsdb *pTsdb);
2507
extern void    tsdbEnableBgTask(STsdb *pTsdb);
2508

2509
static int32_t vnodeProcessAlterConfigReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
1,297✔
2510
  bool walChanged = false;
1,297✔
2511
  bool tsdbChanged = false;
1,297✔
2512

2513
  SAlterVnodeConfigReq req = {0};
1,297✔
2514
  if (tDeserializeSAlterVnodeConfigReq(pReq, len, &req) != 0) {
1,297!
2515
    terrno = TSDB_CODE_INVALID_MSG;
×
2516
    return TSDB_CODE_INVALID_MSG;
×
2517
  }
2518

2519
  vInfo("vgId:%d, start to alter vnode config, page:%d pageSize:%d buffer:%d szPage:%d szBuf:%" PRIu64
1,298!
2520
        " cacheLast:%d cacheLastSize:%d days:%d keep0:%d keep1:%d keep2:%d keepTimeOffset:%d ssKeepLocal:%d "
2521
        "ssCompact:%d fsync:%d level:%d "
2522
        "walRetentionPeriod:%d walRetentionSize:%d",
2523
        TD_VID(pVnode), req.pages, req.pageSize, req.buffer, req.pageSize * 1024, (uint64_t)req.buffer * 1024 * 1024,
2524
        req.cacheLast, req.cacheLastSize, req.daysPerFile, req.daysToKeep0, req.daysToKeep1, req.daysToKeep2,
2525
        req.keepTimeOffset, req.ssKeepLocal, req.ssCompact, req.walFsyncPeriod, req.walLevel, req.walRetentionPeriod,
2526
        req.walRetentionSize);
2527

2528
  if (pVnode->config.cacheLastSize != req.cacheLastSize) {
1,298✔
2529
    pVnode->config.cacheLastSize = req.cacheLastSize;
590✔
2530
    tsdbCacheSetCapacity(pVnode, (size_t)pVnode->config.cacheLastSize * 1024 * 1024);
590✔
2531
  }
2532

2533
  if (pVnode->config.szBuf != req.buffer * 1024LL * 1024LL) {
1,298!
2534
    vInfo("vgId:%d, vnode buffer is changed from %" PRId64 " to %" PRId64, TD_VID(pVnode), pVnode->config.szBuf,
×
2535
          (uint64_t)(req.buffer * 1024LL * 1024LL));
2536
    pVnode->config.szBuf = req.buffer * 1024LL * 1024LL;
×
2537
  }
2538

2539
  if (pVnode->config.szCache != req.pages) {
1,298!
2540
    if ((terrno = metaAlterCache(pVnode->pMeta, req.pages)) < 0) {
×
2541
      vError("vgId:%d, failed to change vnode pages from %d to %d failed since %s", TD_VID(pVnode),
×
2542
             pVnode->config.szCache, req.pages, tstrerror(terrno));
2543
      return terrno;
×
2544
    } else {
2545
      vInfo("vgId:%d, vnode pages is changed from %d to %d", TD_VID(pVnode), pVnode->config.szCache, req.pages);
×
2546
      pVnode->config.szCache = req.pages;
×
2547
    }
2548
  }
2549

2550
  if (pVnode->config.cacheLast != req.cacheLast) {
1,298✔
2551
    pVnode->config.cacheLast = req.cacheLast;
866✔
2552
  }
2553

2554
  if (pVnode->config.walCfg.fsyncPeriod != req.walFsyncPeriod) {
1,298✔
2555
    pVnode->config.walCfg.fsyncPeriod = req.walFsyncPeriod;
108✔
2556
    walChanged = true;
108✔
2557
  }
2558

2559
  if (pVnode->config.walCfg.level != req.walLevel) {
1,298✔
2560
    if (pVnode->config.walCfg.level == 0) {
72!
2561
      pVnode->config.walCfg.clearFiles = 1;
×
2562
    }
2563
    pVnode->config.walCfg.level = req.walLevel;
72✔
2564
    walChanged = true;
72✔
2565
  }
2566

2567
  if (pVnode->config.walCfg.retentionPeriod != req.walRetentionPeriod) {
1,298✔
2568
    pVnode->config.walCfg.retentionPeriod = req.walRetentionPeriod;
132✔
2569
    walChanged = true;
132✔
2570
  }
2571

2572
  if (pVnode->config.walCfg.retentionSize != req.walRetentionSize) {
1,298!
2573
    pVnode->config.walCfg.retentionSize = req.walRetentionSize;
×
2574
    walChanged = true;
×
2575
  }
2576

2577
  if (pVnode->config.tsdbCfg.keep0 != req.daysToKeep0) {
1,298✔
2578
    pVnode->config.tsdbCfg.keep0 = req.daysToKeep0;
108✔
2579
    if (!VND_IS_RSMA(pVnode)) {
108!
2580
      tsdbChanged = true;
108✔
2581
    }
2582
  }
2583

2584
  if (pVnode->config.tsdbCfg.keep1 != req.daysToKeep1) {
1,298✔
2585
    pVnode->config.tsdbCfg.keep1 = req.daysToKeep1;
108✔
2586
    if (!VND_IS_RSMA(pVnode)) {
108!
2587
      tsdbChanged = true;
108✔
2588
    }
2589
  }
2590

2591
  if (pVnode->config.tsdbCfg.keep2 != req.daysToKeep2) {
1,298✔
2592
    pVnode->config.tsdbCfg.keep2 = req.daysToKeep2;
112✔
2593
    if (!VND_IS_RSMA(pVnode)) {
112!
2594
      tsdbChanged = true;
112✔
2595
    }
2596
  }
2597

2598
  if (pVnode->config.tsdbCfg.keepTimeOffset != req.keepTimeOffset) {
1,298✔
2599
    pVnode->config.tsdbCfg.keepTimeOffset = req.keepTimeOffset;
4✔
2600
    if (!VND_IS_RSMA(pVnode)) {
4!
2601
      tsdbChanged = true;
4✔
2602
    }
2603
  }
2604

2605
  if (req.sttTrigger != -1 && req.sttTrigger != pVnode->config.sttTrigger) {
1,298!
2606
    if (req.sttTrigger > 1 && pVnode->config.sttTrigger > 1) {
×
2607
      pVnode->config.sttTrigger = req.sttTrigger;
×
2608
    } else {
2609
      vnodeAWait(&pVnode->commitTask);
×
2610

2611
      int32_t ret = tsdbDisableAndCancelAllBgTask(pVnode->pTsdb);
×
2612
      if (ret != 0) {
×
2613
        vError("vgId:%d, failed to disable bg task since %s", TD_VID(pVnode), tstrerror(ERRNO));
×
2614
      }
2615

2616
      pVnode->config.sttTrigger = req.sttTrigger;
×
2617
      tsdbEnableBgTask(pVnode->pTsdb);
×
2618
    }
2619
  }
2620

2621
  if (req.minRows != -1 && req.minRows != pVnode->config.tsdbCfg.minRows) {
1,298!
2622
    pVnode->config.tsdbCfg.minRows = req.minRows;
×
2623
  }
2624

2625
  if (req.ssKeepLocal != -1 && req.ssKeepLocal != pVnode->config.ssKeepLocal) {
1,298!
2626
    pVnode->config.ssKeepLocal = req.ssKeepLocal;
×
2627
  }
2628
  if (req.ssCompact != -1 && req.ssCompact != pVnode->config.ssCompact) {
1,298!
2629
    pVnode->config.ssCompact = req.ssCompact;
×
2630
  }
2631

2632
  if (walChanged) {
1,298✔
2633
    if (walAlter(pVnode->pWal, &pVnode->config.walCfg) != 0) {
312!
2634
      vError("vgId:%d, failed to alter wal config since %s", TD_VID(pVnode), tstrerror(ERRNO));
×
2635
    }
2636
  }
2637

2638
  if (tsdbChanged) {
1,298✔
2639
    tsdbSetKeepCfg(pVnode->pTsdb, &pVnode->config.tsdbCfg);
116✔
2640
  }
2641

2642
  return 0;
1,298✔
2643
}
2644

2645
static int32_t vnodeProcessBatchDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
2646
  SBatchDeleteReq deleteReq;
2647
  SDecoder        decoder;
2648
  tDecoderInit(&decoder, pReq, len);
×
2649
  if (tDecodeSBatchDeleteReq(&decoder, &deleteReq) < 0) {
×
2650
    tDecoderClear(&decoder);
×
2651
    return terrno = TSDB_CODE_INVALID_MSG;
×
2652
  }
2653

2654
  SMetaReader mr = {0};
×
2655
  metaReaderDoInit(&mr, pVnode->pMeta, META_READER_NOLOCK);
×
2656
  STsdb *pTsdb = pVnode->pTsdb;
×
2657

2658
  if (deleteReq.level) {
×
2659
    pTsdb = deleteReq.level == 1 ? VND_RSMA1(pVnode) : VND_RSMA2(pVnode);
×
2660
  }
2661

2662
  int32_t sz = taosArrayGetSize(deleteReq.deleteReqs);
×
2663
  for (int32_t i = 0; i < sz; i++) {
×
2664
    SSingleDeleteReq *pOneReq = taosArrayGet(deleteReq.deleteReqs, i);
×
2665
    char             *name = pOneReq->tbname;
×
2666
    if (metaGetTableEntryByName(&mr, name) < 0) {
×
2667
      vDebug("vgId:%d, stream delete msg, skip since no table: %s", pVnode->config.vgId, name);
×
2668
      continue;
×
2669
    }
2670

2671
    int64_t uid = mr.me.uid;
×
2672

2673
    int32_t code = tsdbDeleteTableData(pTsdb, ver, deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
×
2674
    if (code < 0) {
×
2675
      terrno = code;
×
2676
      vError("vgId:%d, delete error since %s, suid:%" PRId64 ", uid:%" PRId64 ", start ts:%" PRId64 ", end ts:%" PRId64,
×
2677
             TD_VID(pVnode), terrstr(), deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
2678
    }
2679

2680
    if (deleteReq.level == 0) {
×
2681
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, deleteReq.ctimeMs);
×
2682
      if (code < 0) {
×
2683
        terrno = code;
×
2684
        vError("vgId:%d, update change time error since %s, suid:%" PRId64 ", uid:%" PRId64 ", start ts:%" PRId64
×
2685
               ", end ts:%" PRId64,
2686
               TD_VID(pVnode), terrstr(), deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
2687
      }
2688
    }
2689
    tDecoderClear(&mr.coder);
×
2690
  }
2691
  metaReaderClear(&mr);
×
2692
  taosArrayDestroy(deleteReq.deleteReqs);
×
2693
  return 0;
×
2694
}
2695

2696
static int32_t vnodeProcessDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
120,267✔
2697
                                     SRpcMsg *pOriginalMsg) {
2698
  int32_t     code = 0;
120,267✔
2699
  SDecoder   *pCoder = &(SDecoder){0};
120,267✔
2700
  SDeleteRes *pRes = &(SDeleteRes){0};
120,267✔
2701

2702
  pRsp->msgType = TDMT_VND_DELETE_RSP;
120,267✔
2703
  pRsp->pCont = NULL;
120,267✔
2704
  pRsp->contLen = 0;
120,267✔
2705
  pRsp->code = TSDB_CODE_SUCCESS;
120,267✔
2706

2707
  pRes->uidList = taosArrayInit(0, sizeof(tb_uid_t));
120,267✔
2708
  if (pRes->uidList == NULL) {
120,268!
2709
    code = terrno;
×
2710
    goto _err;
×
2711
  }
2712

2713
  tDecoderInit(pCoder, pReq, len);
120,268✔
2714
  code = tDecodeDeleteRes(pCoder, pRes);
120,267✔
2715
  if (code) goto _err;
120,273!
2716

2717
  if (pRes->affectedRows > 0) {
120,273✔
2718
    for (int32_t iUid = 0; iUid < taosArrayGetSize(pRes->uidList); iUid++) {
235,971✔
2719
      uint64_t uid = *(uint64_t *)taosArrayGet(pRes->uidList, iUid);
121,775✔
2720
      code = tsdbDeleteTableData(pVnode->pTsdb, ver, pRes->suid, uid, pRes->skey, pRes->ekey);
121,775✔
2721
      if (code) goto _err;
121,775!
2722
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, pRes->ctimeMs);
121,775✔
2723
      if (code) goto _err;
121,775!
2724
    }
2725
  }
2726

2727
  code = tdProcessRSmaDelete(pVnode->pSma, ver, pRes, pReq, len);
120,268✔
2728

2729
  tDecoderClear(pCoder);
120,274✔
2730
  taosArrayDestroy(pRes->uidList);
120,270✔
2731

2732
  SVDeleteRsp rsp = {.affectedRows = pRes->affectedRows};
120,278✔
2733
  int32_t     ret = 0;
120,278✔
2734
  tEncodeSize(tEncodeSVDeleteRsp, &rsp, pRsp->contLen, ret);
120,278!
2735
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
120,268✔
2736
  SEncoder ec = {0};
120,271✔
2737
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
120,271✔
2738
  code = tEncodeSVDeleteRsp(&ec, &rsp);
120,273✔
2739
  if (code) goto _err;
120,277!
2740
  tEncoderClear(&ec);
120,277✔
2741
  return code;
120,278✔
2742

2743
_err:
×
2744
  /*
2745
  if(code == TSDB_CODE_SUCCESS){
2746
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_DELETE, pVnode->monitor.strClusterId,
2747
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2748
                                        pOriginalMsg->info.conn.user, "Success"};
2749
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2750
  }
2751
  else{
2752
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_DELETE, pVnode->monitor.strClusterId,
2753
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2754
                                        pOriginalMsg->info.conn.user, "Failed"};
2755
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2756
  }
2757
  */
2758

2759
  return code;
×
2760
}
2761
static int32_t vnodeProcessCreateIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
1,892✔
2762
  SVCreateStbReq req = {0};
1,892✔
2763
  SDecoder       dc = {0};
1,892✔
2764
  int32_t        code = 0;
1,892✔
2765

2766
  pRsp->msgType = TDMT_VND_CREATE_INDEX_RSP;
1,892✔
2767
  pRsp->code = TSDB_CODE_SUCCESS;
1,892✔
2768
  pRsp->pCont = NULL;
1,892✔
2769
  pRsp->contLen = 0;
1,892✔
2770

2771
  tDecoderInit(&dc, pReq, len);
1,892✔
2772
  // decode req
2773
  if (tDecodeSVCreateStbReq(&dc, &req) < 0) {
1,892!
2774
    tDecoderClear(&dc);
×
2775
    return terrno = TSDB_CODE_INVALID_MSG;
×
2776
  }
2777

2778
  code = metaAddIndexToSuperTable(pVnode->pMeta, ver, &req);
1,892✔
2779
  if (code) {
1,892!
2780
    pRsp->code = code;
×
2781
    goto _err;
×
2782
  }
2783
  tDecoderClear(&dc);
1,892✔
2784
  return 0;
1,892✔
2785

2786
_err:
×
2787
  tDecoderClear(&dc);
×
2788
  return code;
×
2789
}
2790
static int32_t vnodeProcessDropIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
4,234✔
2791
  SDropIndexReq req = {0};
4,234✔
2792
  int32_t       code = 0;
4,234✔
2793
  pRsp->msgType = TDMT_VND_DROP_INDEX_RSP;
4,234✔
2794
  pRsp->code = TSDB_CODE_SUCCESS;
4,234✔
2795
  pRsp->pCont = NULL;
4,234✔
2796
  pRsp->contLen = 0;
4,234✔
2797

2798
  if ((code = tDeserializeSDropIdxReq(pReq, len, &req))) {
4,234!
2799
    pRsp->code = code;
×
2800
    return code;
×
2801
  }
2802

2803
  code = metaDropIndexFromSuperTable(pVnode->pMeta, ver, &req);
4,234✔
2804
  if (code) {
4,234!
2805
    pRsp->code = code;
×
2806
    return code;
×
2807
  }
2808
  return TSDB_CODE_SUCCESS;
4,234✔
2809
}
2810

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

2813
static int32_t vnodeProcessCompactVnodeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
214✔
2814
  if (!pVnode->restored) {
214✔
2815
    vInfo("vgId:%d, ignore compact req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
174!
2816
    return 0;
174✔
2817
  }
2818
  return vnodeAsyncCompact(pVnode, ver, pReq, len, pRsp);
40✔
2819
}
2820

2821
static int32_t vnodeProcessConfigChangeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
2822
  if (syncCheckMember(pVnode->sync) != 0) {
×
2823
    vError("vgId:%d, failed to check member", TD_VID(pVnode));
×
2824
  }
2825

2826
  pRsp->msgType = TDMT_SYNC_CONFIG_CHANGE_RSP;
×
2827
  pRsp->code = TSDB_CODE_SUCCESS;
×
2828
  pRsp->pCont = NULL;
×
2829
  pRsp->contLen = 0;
×
2830

2831
  return 0;
×
2832
}
2833

2834
static int32_t vnodeCheckToken(SVnode *pVnode, char *member0Token, char *member1Token) {
48✔
2835
  SSyncState syncState = syncGetState(pVnode->sync);
48✔
2836
  if (syncState.state != TAOS_SYNC_STATE_LEADER) {
48✔
2837
    return terrno = TSDB_CODE_SYN_NOT_LEADER;
10✔
2838
  }
2839

2840
  char token[TSDB_ARB_TOKEN_SIZE] = {0};
38✔
2841
  if (vnodeGetArbToken(pVnode, token) != 0) {
38!
2842
    return terrno = TSDB_CODE_NOT_FOUND;
×
2843
  }
2844

2845
  if (strncmp(token, member0Token, TSDB_ARB_TOKEN_SIZE) != 0 &&
38✔
2846
      strncmp(token, member1Token, TSDB_ARB_TOKEN_SIZE) != 0) {
32✔
2847
    return terrno = TSDB_CODE_MND_ARB_TOKEN_MISMATCH;
30✔
2848
  }
2849

2850
  terrno = TSDB_CODE_SUCCESS;
8✔
2851
  return 0;
8✔
2852
}
2853

2854
static int32_t vnodeCheckSyncd(SVnode *pVnode, char *member0Token, char *member1Token) {
8✔
2855
  int32_t code = vnodeCheckToken(pVnode, member0Token, member1Token);
8✔
2856
  if (code != 0) {
8✔
2857
    return code;
4✔
2858
  }
2859

2860
  return syncCheckSynced(pVnode->sync);
4✔
2861
}
2862

2863
static int32_t vnodeProcessArbCheckSyncReq(SVnode *pVnode, void *pReq, int32_t len, SRpcMsg *pRsp) {
8✔
2864
  int32_t code = 0;
8✔
2865

2866
  SVArbCheckSyncReq syncReq = {0};
8✔
2867

2868
  code = tDeserializeSVArbCheckSyncReq(pReq, len, &syncReq);
8✔
2869
  if (code) {
8!
2870
    return terrno = code;
×
2871
  }
2872

2873
  pRsp->msgType = TDMT_VND_ARB_CHECK_SYNC_RSP;
8✔
2874
  pRsp->code = TSDB_CODE_SUCCESS;
8✔
2875
  pRsp->pCont = NULL;
8✔
2876
  pRsp->contLen = 0;
8✔
2877

2878
  SVArbCheckSyncRsp syncRsp = {0};
8✔
2879
  syncRsp.arbToken = syncReq.arbToken;
8✔
2880
  syncRsp.member0Token = syncReq.member0Token;
8✔
2881
  syncRsp.member1Token = syncReq.member1Token;
8✔
2882
  syncRsp.vgId = TD_VID(pVnode);
8✔
2883

2884
  if (vnodeCheckSyncd(pVnode, syncReq.member0Token, syncReq.member1Token) != 0) {
8✔
2885
    vError("vgId:%d, failed to check assigned log syncd", TD_VID(pVnode));
4!
2886
  }
2887
  syncRsp.errCode = terrno;
8✔
2888

2889
  if (vnodeUpdateArbTerm(pVnode, syncReq.arbTerm) != 0) {
8!
2890
    vError("vgId:%d, failed to update arb term", TD_VID(pVnode));
×
2891
    code = -1;
×
2892
    goto _OVER;
×
2893
  }
2894

2895
  int32_t contLen = tSerializeSVArbCheckSyncRsp(NULL, 0, &syncRsp);
8✔
2896
  if (contLen <= 0) {
8!
2897
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2898
    code = -1;
×
2899
    goto _OVER;
×
2900
  }
2901
  void *pHead = rpcMallocCont(contLen);
8✔
2902
  if (!pHead) {
8!
2903
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2904
    code = -1;
×
2905
    goto _OVER;
×
2906
  }
2907

2908
  if (tSerializeSVArbCheckSyncRsp(pHead, contLen, &syncRsp) <= 0) {
8!
2909
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2910
    rpcFreeCont(pHead);
×
2911
    code = -1;
×
2912
    goto _OVER;
×
2913
  }
2914

2915
  pRsp->pCont = pHead;
8✔
2916
  pRsp->contLen = contLen;
8✔
2917

2918
  terrno = TSDB_CODE_SUCCESS;
8✔
2919

2920
_OVER:
8✔
2921
  tFreeSVArbCheckSyncReq(&syncReq);
8✔
2922
  return code;
8✔
2923
}
2924

2925
#ifndef TD_ENTERPRISE
2926
int32_t vnodeAsyncCompact(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) { return 0; }
2927
int32_t tsdbAsyncCompact(STsdb *tsdb, const STimeWindow *tw, bool sync) { return 0; }
2928
#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