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

taosdata / TDengine / #3528

13 Nov 2024 02:14AM UTC coverage: 60.905% (+0.09%) from 60.819%
#3528

push

travis-ci

web-flow
Merge pull request #28748 from taosdata/test/chr-3.0-TD14758

test:add docs ci in jenkinsfile2

118800 of 249004 branches covered (47.71%)

Branch coverage included in aggregate %.

199361 of 273386 relevant lines covered (72.92%)

14738389.65 hits per line

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

60.87
/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 "audit.h"
17
#include "cos.h"
18
#include "monitor.h"
19
#include "tencode.h"
20
#include "tglobal.h"
21
#include "tmsg.h"
22
#include "tstrbuild.h"
23
#include "vnd.h"
24
#include "vnode.h"
25
#include "vnodeInt.h"
26

27
extern taos_counter_t *tsInsertCounter;
28

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

56
static int32_t vnodePreCheckAssignedLogSyncd(SVnode *pVnode, char *member0Token, char *member1Token);
57
static int32_t vnodeCheckAssignedLogSyncd(SVnode *pVnode, char *member0Token, char *member1Token);
58
static int32_t vnodeProcessFetchTtlExpiredTbs(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
59

60
extern int32_t vnodeProcessKillCompactReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
61
extern int32_t vnodeQueryCompactProgress(SVnode *pVnode, SRpcMsg *pMsg);
62

63
static int32_t vnodePreprocessCreateTableReq(SVnode *pVnode, SDecoder *pCoder, int64_t btime, int64_t *pUid) {
166,894✔
64
  int32_t code = 0;
166,894✔
65
  int32_t lino = 0;
166,894✔
66

67
  if (tStartDecode(pCoder) < 0) {
166,894!
68
    code = TSDB_CODE_INVALID_MSG;
×
69
    TSDB_CHECK_CODE(code, lino, _exit);
×
70
  }
71

72
  // flags
73
  if (tDecodeI32v(pCoder, NULL) < 0) {
166,948!
74
    code = TSDB_CODE_INVALID_MSG;
×
75
    TSDB_CHECK_CODE(code, lino, _exit);
×
76
  }
77

78
  // name
79
  char *name = NULL;
166,948✔
80
  if (tDecodeCStr(pCoder, &name) < 0) {
166,921!
81
    code = TSDB_CODE_INVALID_MSG;
×
82
    TSDB_CHECK_CODE(code, lino, _exit);
×
83
  }
84

85
  // uid
86
  int64_t uid = metaGetTableEntryUidByName(pVnode->pMeta, name);
166,921✔
87
  if (uid == 0) {
166,908✔
88
    uid = tGenIdPI64();
146,228✔
89
  }
90
  *(int64_t *)(pCoder->data + pCoder->pos) = uid;
166,907✔
91

92
  // btime
93
  *(int64_t *)(pCoder->data + pCoder->pos + 8) = btime;
166,907✔
94

95
  tEndDecode(pCoder);
166,907✔
96

97
_exit:
166,893✔
98
  if (code) {
166,893!
99
    vError("vgId:%d %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
100
  } else {
101
    vTrace("vgId:%d %s done, table:%s uid generated:%" PRId64, TD_VID(pVnode), __func__, name, uid);
166,893✔
102
    if (pUid) *pUid = uid;
166,893✔
103
  }
104
  return code;
166,896✔
105
}
106
static int32_t vnodePreProcessCreateTableMsg(SVnode *pVnode, SRpcMsg *pMsg) {
76,921✔
107
  int32_t code = 0;
76,921✔
108
  int32_t lino = 0;
76,921✔
109

110
  int64_t  btime = taosGetTimestampMs();
76,930✔
111
  SDecoder dc = {0};
76,930✔
112
  int32_t  nReqs;
113

114
  tDecoderInit(&dc, (uint8_t *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead));
76,930✔
115
  if (tStartDecode(&dc) < 0) {
76,917!
116
    code = TSDB_CODE_INVALID_MSG;
×
117
    return code;
×
118
  }
119

120
  if (tDecodeI32v(&dc, &nReqs) < 0) {
76,938!
121
    code = TSDB_CODE_INVALID_MSG;
×
122
    TSDB_CHECK_CODE(code, lino, _exit);
×
123
  }
124
  for (int32_t iReq = 0; iReq < nReqs; iReq++) {
178,934✔
125
    code = vnodePreprocessCreateTableReq(pVnode, &dc, btime, NULL);
102,019✔
126
    TSDB_CHECK_CODE(code, lino, _exit);
101,996!
127
  }
128

129
  tEndDecode(&dc);
76,915✔
130

131
_exit:
76,930✔
132
  tDecoderClear(&dc);
76,930✔
133
  if (code) {
76,933!
134
    vError("vgId:%d, %s:%d failed to preprocess submit request since %s, msg type:%s", TD_VID(pVnode), __func__, lino,
×
135
           tstrerror(code), TMSG_INFO(pMsg->msgType));
136
  }
137
  return code;
76,927✔
138
}
139

140
static int32_t vnodePreProcessAlterTableMsg(SVnode *pVnode, SRpcMsg *pMsg) {
5,198✔
141
  int32_t code = TSDB_CODE_INVALID_MSG;
5,198✔
142
  int32_t lino = 0;
5,198✔
143

144
  SDecoder dc = {0};
5,198✔
145
  tDecoderInit(&dc, (uint8_t *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead));
5,198✔
146

147
  SVAlterTbReq vAlterTbReq = {0};
5,198✔
148
  int64_t      ctimeMs = taosGetTimestampMs();
5,198✔
149
  if (tDecodeSVAlterTbReqSetCtime(&dc, &vAlterTbReq, ctimeMs) < 0) {
5,198!
150
    goto _exit;
×
151
  }
152

153
  code = 0;
5,198✔
154

155
_exit:
5,198✔
156
  tDecoderClear(&dc);
5,198✔
157
  if (code) {
5,198!
158
    vError("vgId:%d %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
159
  } else {
160
    vTrace("vgId:%d %s done, table:%s ctimeMs generated:%" PRId64, TD_VID(pVnode), __func__, vAlterTbReq.tbName,
5,198✔
161
           ctimeMs);
162
  }
163
  return code;
5,198✔
164
}
165

166
static int32_t vnodePreProcessDropTtlMsg(SVnode *pVnode, SRpcMsg *pMsg) {
96,117✔
167
  int32_t code = TSDB_CODE_INVALID_MSG;
96,117✔
168
  int32_t lino = 0;
96,117✔
169

170
  SMsgHead *pContOld = pMsg->pCont;
96,117✔
171
  int32_t   reqLenOld = pMsg->contLen - sizeof(SMsgHead);
96,117✔
172

173
  SArray *tbUids = NULL;
96,117✔
174
  int64_t timestampMs = 0;
96,117✔
175

176
  SVDropTtlTableReq ttlReq = {0};
96,117✔
177
  if (tDeserializeSVDropTtlTableReq((char *)pContOld + sizeof(SMsgHead), reqLenOld, &ttlReq) != 0) {
96,117!
178
    code = TSDB_CODE_INVALID_MSG;
×
179
    TSDB_CHECK_CODE(code, lino, _exit);
×
180
  }
181

182
  {  // find expired uids
183
    tbUids = taosArrayInit(8, sizeof(tb_uid_t));
96,329✔
184
    if (tbUids == NULL) {
96,280!
185
      code = terrno;
×
186
      TSDB_CHECK_CODE(code, lino, _exit);
×
187
    }
188

189
    timestampMs = (int64_t)ttlReq.timestampSec * 1000;
96,280✔
190
    code = metaTtlFindExpired(pVnode->pMeta, timestampMs, tbUids, ttlReq.ttlDropMaxCount);
96,280✔
191
    if (code != 0) {
96,201!
192
      code = TSDB_CODE_INVALID_MSG;
×
193
      TSDB_CHECK_CODE(code, lino, _exit);
×
194
    }
195

196
    ttlReq.nUids = taosArrayGetSize(tbUids);
96,201✔
197
    ttlReq.pTbUids = tbUids;
96,077✔
198
  }
199

200
  if (ttlReq.nUids == 0) {
96,077✔
201
    code = TSDB_CODE_MSG_PREPROCESSED;
96,034✔
202
    TSDB_CHECK_CODE(code, lino, _exit);
96,034!
203
  }
204

205
  {  // prepare new content
206
    int32_t reqLenNew = tSerializeSVDropTtlTableReq(NULL, 0, &ttlReq);
×
207
    int32_t contLenNew = reqLenNew + sizeof(SMsgHead);
38✔
208

209
    SMsgHead *pContNew = rpcMallocCont(contLenNew);
38✔
210
    if (pContNew == NULL) {
38!
211
      code = terrno;
×
212
      TSDB_CHECK_CODE(code, lino, _exit);
×
213
    }
214

215
    if (tSerializeSVDropTtlTableReq((char *)pContNew + sizeof(SMsgHead), reqLenNew, &ttlReq) != 0) {
38!
216
      vError("vgId:%d %s:%d failed to serialize drop ttl request", TD_VID(pVnode), __func__, lino);
38!
217
    }
218
    pContNew->contLen = htonl(reqLenNew);
38✔
219
    pContNew->vgId = pContOld->vgId;
38✔
220

221
    rpcFreeCont(pContOld);
38✔
222
    pMsg->pCont = pContNew;
38✔
223
    pMsg->contLen = contLenNew;
38✔
224
  }
225

226
  code = 0;
38✔
227

228
_exit:
96,117✔
229
  taosArrayDestroy(tbUids);
96,117✔
230

231
  if (code && code != TSDB_CODE_MSG_PREPROCESSED) {
96,194!
232
    vError("vgId:%d, %s:%d failed to preprocess drop ttl request since %s, msg type:%s", TD_VID(pVnode), __func__, lino,
×
233
           tstrerror(code), TMSG_INFO(pMsg->msgType));
234
  } else {
235
    vTrace("vgId:%d, %s done, timestampSec:%d, nUids:%d", TD_VID(pVnode), __func__, ttlReq.timestampSec, ttlReq.nUids);
96,194✔
236
  }
237

238
  return code;
96,047✔
239
}
240

241
extern int64_t tsMaxKeyByPrecision[];
242
static int32_t vnodePreProcessSubmitTbData(SVnode *pVnode, SDecoder *pCoder, int64_t btimeMs, int64_t ctimeMs) {
4,639,474✔
243
  int32_t code = 0;
4,639,474✔
244
  int32_t lino = 0;
4,639,474✔
245

246
  if (tStartDecode(pCoder) < 0) {
4,639,474!
247
    code = TSDB_CODE_INVALID_MSG;
×
248
    TSDB_CHECK_CODE(code, lino, _exit);
×
249
  }
250

251
  SSubmitTbData submitTbData;
252
  uint8_t       version;
253
  if (tDecodeI32v(pCoder, &submitTbData.flags) < 0) {
4,666,747!
254
    code = TSDB_CODE_INVALID_MSG;
×
255
    TSDB_CHECK_CODE(code, lino, _exit);
×
256
  }
257
  version = (submitTbData.flags >> 8) & 0xff;
4,666,747✔
258
  submitTbData.flags = submitTbData.flags & 0xff;
4,666,747✔
259

260
  int64_t uid;
261
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
4,666,747✔
262
    code = vnodePreprocessCreateTableReq(pVnode, pCoder, btimeMs, &uid);
64,890✔
263
    TSDB_CHECK_CODE(code, lino, _exit);
64,890!
264
  }
265

266
  // submit data
267
  if (tDecodeI64(pCoder, &submitTbData.suid) < 0) {
4,660,198!
268
    code = TSDB_CODE_INVALID_MSG;
×
269
    TSDB_CHECK_CODE(code, lino, _exit);
×
270
  }
271

272
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
4,660,198✔
273
    *(int64_t *)(pCoder->data + pCoder->pos) = uid;
64,890✔
274
    pCoder->pos += sizeof(int64_t);
64,890✔
275
  } else {
276
    if (tDecodeI64(pCoder, &submitTbData.uid) < 0) {
4,592,039!
277
      code = TSDB_CODE_INVALID_MSG;
×
278
      TSDB_CHECK_CODE(code, lino, _exit);
×
279
    }
280
  }
281

282
  if (tDecodeI32v(pCoder, &submitTbData.sver) < 0) {
4,639,419!
283
    code = TSDB_CODE_INVALID_MSG;
×
284
    TSDB_CHECK_CODE(code, lino, _exit);
×
285
  }
286

287
  // scan and check
288
  TSKEY now = btimeMs;
4,639,419✔
289
  if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_MICRO) {
4,639,419✔
290
    now *= 1000;
16,153✔
291
  } else if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_NANO) {
4,623,266✔
292
    now *= 1000000;
3,739✔
293
  }
294

295
  int32_t keep = pVnode->config.tsdbCfg.keep2;
4,639,419✔
296
  /*
297
  int32_t nlevel = tfsGetLevel(pVnode->pTfs);
298
  if (nlevel > 1 && tsS3Enabled) {
299
    if (nlevel == 3) {
300
      keep = pVnode->config.tsdbCfg.keep1;
301
    } else if (nlevel == 2) {
302
      keep = pVnode->config.tsdbCfg.keep0;
303
    }
304
  }
305
  */
306

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

316
    SColData colData = {0};
150✔
317
    pCoder->pos += tGetColData(version, pCoder->data + pCoder->pos, &colData);
150✔
318
    if (colData.flag != HAS_VALUE) {
150!
319
      code = TSDB_CODE_INVALID_MSG;
×
320
      goto _exit;
×
321
    }
322

323
    for (int32_t iRow = 0; iRow < colData.nVal; iRow++) {
404✔
324
      if (((TSKEY *)colData.pData)[iRow] < minKey || ((TSKEY *)colData.pData)[iRow] > maxKey) {
255!
325
        code = TSDB_CODE_TDB_TIMESTAMP_OUT_OF_RANGE;
1✔
326
        goto _exit;
1✔
327
      }
328
    }
329

330
    for (uint64_t i = 1; i < nColData; i++) {
667✔
331
      pCoder->pos += tGetColData(version, pCoder->data + pCoder->pos, &colData);
518✔
332
    }
333
  } else {
334
    uint64_t nRow;
335
    if (tDecodeU64v(pCoder, &nRow) < 0) {
4,637,067!
336
      code = TSDB_CODE_INVALID_MSG;
×
337
      goto _exit;
95✔
338
    }
339

340
    for (int32_t iRow = 0; iRow < nRow; ++iRow) {
290,146,802✔
341
      SRow *pRow = (SRow *)(pCoder->data + pCoder->pos);
285,509,830✔
342
      pCoder->pos += pRow->len;
285,509,830✔
343

344
      if (pRow->ts < minKey || pRow->ts > maxKey) {
285,509,830!
345
        code = TSDB_CODE_TDB_TIMESTAMP_OUT_OF_RANGE;
95✔
346
        goto _exit;
95✔
347
      }
348
    }
349
  }
350

351
  if (!tDecodeIsEnd(pCoder)) {
4,637,121!
352
    *(int64_t *)(pCoder->data + pCoder->pos) = ctimeMs;
4,641,029✔
353
    pCoder->pos += sizeof(int64_t);
4,641,029✔
354
  }
355

356
  tEndDecode(pCoder);
4,637,121✔
357

358
_exit:
4,639,657✔
359
  return code;
4,639,657✔
360
}
361
static int32_t vnodePreProcessSubmitMsg(SVnode *pVnode, SRpcMsg *pMsg) {
3,982,993✔
362
  int32_t code = 0;
3,982,993✔
363
  int32_t lino = 0;
3,982,993✔
364

365
  SDecoder *pCoder = &(SDecoder){0};
3,982,993✔
366

367
  if (taosHton64(((SSubmitReq2Msg *)pMsg->pCont)->version) != 1) {
3,982,993!
368
    code = TSDB_CODE_INVALID_MSG;
×
369
    TSDB_CHECK_CODE(code, lino, _exit);
×
370
  }
371

372
  tDecoderInit(pCoder, (uint8_t *)pMsg->pCont + sizeof(SSubmitReq2Msg), pMsg->contLen - sizeof(SSubmitReq2Msg));
3,983,273✔
373

374
  if (tStartDecode(pCoder) < 0) {
3,983,309!
375
    code = TSDB_CODE_INVALID_MSG;
×
376
    TSDB_CHECK_CODE(code, lino, _exit);
×
377
  }
378

379
  uint64_t nSubmitTbData;
380
  if (tDecodeU64v(pCoder, &nSubmitTbData) < 0) {
3,983,489!
381
    code = TSDB_CODE_INVALID_MSG;
×
382
    TSDB_CHECK_CODE(code, lino, _exit);
×
383
  }
384

385
  int64_t btimeMs = taosGetTimestampMs();
3,983,511✔
386
  int64_t ctimeMs = btimeMs;
3,983,511✔
387
  for (int32_t i = 0; i < nSubmitTbData; i++) {
8,623,117✔
388
    code = vnodePreProcessSubmitTbData(pVnode, pCoder, btimeMs, ctimeMs);
4,639,915✔
389
    TSDB_CHECK_CODE(code, lino, _exit);
4,639,702✔
390
  }
391

392
  tEndDecode(pCoder);
3,983,202✔
393

394
_exit:
3,983,501✔
395
  tDecoderClear(pCoder);
3,983,501✔
396
  if (code) {
3,983,467✔
397
    vError("vgId:%d, %s:%d failed to preprocess submit request since %s, msg type:%s", TD_VID(pVnode), __func__, lino,
96!
398
           tstrerror(code), TMSG_INFO(pMsg->msgType));
399
  }
400
  return code;
3,983,439✔
401
}
402

403
static int32_t vnodePreProcessDeleteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
57,625✔
404
  int32_t code = 0;
57,625✔
405

406
  int32_t    size;
407
  int32_t    ret;
408
  uint8_t   *pCont;
409
  SEncoder  *pCoder = &(SEncoder){0};
57,625✔
410
  SDeleteRes res = {0};
57,625✔
411

412
  SReadHandle handle = {.vnode = pVnode, .pMsgCb = &pVnode->msgCb, .skipRollup = 1};
57,625✔
413
  initStorageAPI(&handle.api);
57,625✔
414

415
  code = qWorkerProcessDeleteMsg(&handle, pVnode->pQuery, pMsg, &res);
57,621✔
416
  if (code) goto _exit;
57,624!
417

418
  res.ctimeMs = taosGetTimestampMs();
57,625✔
419
  // malloc and encode
420
  tEncodeSize(tEncodeDeleteRes, &res, size, ret);
57,625!
421
  pCont = rpcMallocCont(size + sizeof(SMsgHead));
57,624✔
422

423
  ((SMsgHead *)pCont)->contLen = size + sizeof(SMsgHead);
57,625✔
424
  ((SMsgHead *)pCont)->vgId = TD_VID(pVnode);
57,625✔
425

426
  tEncoderInit(pCoder, pCont + sizeof(SMsgHead), size);
57,625✔
427
  if (tEncodeDeleteRes(pCoder, &res) != 0) {
57,625!
428
    vError("vgId:%d %s failed to encode delete response", TD_VID(pVnode), __func__);
×
429
  }
430
  tEncoderClear(pCoder);
57,625✔
431

432
  rpcFreeCont(pMsg->pCont);
57,625✔
433
  pMsg->pCont = pCont;
57,625✔
434
  pMsg->contLen = size + sizeof(SMsgHead);
57,625✔
435

436
  taosArrayDestroy(res.uidList);
57,625✔
437

438
_exit:
57,625✔
439
  return code;
57,625✔
440
}
441

442
static int32_t vnodePreProcessBatchDeleteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
2,883✔
443
  int32_t code = 0;
2,883✔
444
  int32_t lino = 0;
2,883✔
445

446
  int64_t         ctimeMs = taosGetTimestampMs();
2,884✔
447
  SBatchDeleteReq pReq = {0};
2,884✔
448
  SDecoder       *pCoder = &(SDecoder){0};
2,884✔
449

450
  tDecoderInit(pCoder, (uint8_t *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead));
2,884✔
451

452
  if (tDecodeSBatchDeleteReqSetCtime(pCoder, &pReq, ctimeMs) < 0) {
2,881!
453
    code = TSDB_CODE_INVALID_MSG;
×
454
  }
455

456
  tDecoderClear(pCoder);
2,886✔
457
  taosArrayDestroy(pReq.deleteReqs);
2,886✔
458

459
  if (code) {
2,888!
460
    vError("vgId:%d %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
461
  } else {
462
    vTrace("vgId:%d %s done, ctimeMs generated:%" PRId64, TD_VID(pVnode), __func__, ctimeMs);
2,888✔
463
  }
464
  return code;
2,887✔
465
}
466

467
static int32_t vnodePreProcessArbCheckSyncMsg(SVnode *pVnode, SRpcMsg *pMsg) {
3✔
468
  SVArbCheckSyncReq syncReq = {0};
3✔
469

470
  if (tDeserializeSVArbCheckSyncReq((char *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead),
3!
471
                                    &syncReq) != 0) {
472
    return TSDB_CODE_INVALID_MSG;
×
473
  }
474

475
  int32_t ret = vnodePreCheckAssignedLogSyncd(pVnode, syncReq.member0Token, syncReq.member1Token);
3✔
476
  if (ret != 0) {
3!
477
    vError("vgId:%d, failed to preprocess arb check sync request since %s", TD_VID(pVnode), tstrerror(ret));
3!
478
  }
479

480
  int32_t code = terrno;
3✔
481
  tFreeSVArbCheckSyncReq(&syncReq);
3✔
482

483
  return code;
3✔
484
}
485

486
int32_t vnodePreProcessDropTbMsg(SVnode* pVnode, SRpcMsg* pMsg) {
16,260✔
487
  int32_t          code = TSDB_CODE_SUCCESS;
16,260✔
488
  int32_t          lino = 0;
16,260✔
489
  int32_t          size = 0;
16,260✔
490
  SDecoder         dc = {0};
16,260✔
491
  SEncoder         ec = {0};
16,260✔
492
  SVDropTbBatchReq receivedBatchReqs = {0};
16,260✔
493
  SVDropTbBatchReq sentBatchReqs = {0};
16,260✔
494

495
  tDecoderInit(&dc, POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), pMsg->contLen - sizeof(SMsgHead));
16,260✔
496

497
  code = tDecodeSVDropTbBatchReq(&dc, &receivedBatchReqs);
16,260✔
498
  if (code < 0) {
16,260!
499
    terrno = code;
×
500
    TSDB_CHECK_CODE(code, lino, _exit);
×
501
  }
502
  sentBatchReqs.pArray = taosArrayInit(receivedBatchReqs.nReqs, sizeof(SVDropTbReq));
16,260✔
503
  if (!sentBatchReqs.pArray) {
16,260!
504
    code = terrno;
×
505
    goto _exit;
×
506
  }
507

508
  for (int32_t i = 0; i < receivedBatchReqs.nReqs; ++i) {
32,545✔
509
    SVDropTbReq* pReq = receivedBatchReqs.pReqs + i;
16,285✔
510
    tb_uid_t uid = metaGetTableEntryUidByName(pVnode->pMeta, pReq->name);
16,285✔
511
    if (uid == 0) {
16,285!
512
      vWarn("vgId:%d, preprocess drop ctb: %s not found", TD_VID(pVnode), pReq->name);
×
513
      continue;
×
514
    }
515
    pReq->uid = uid;
16,285✔
516
    vDebug("vgId:%d %s for: %s, uid: %"PRId64, TD_VID(pVnode), __func__, pReq->name, pReq->uid);
16,285✔
517
    if (taosArrayPush(sentBatchReqs.pArray, pReq) == NULL) {
32,570!
518
      code = terrno;
×
519
      goto _exit;
×
520
    }
521
  }
522
  sentBatchReqs.nReqs = sentBatchReqs.pArray->size;
16,260✔
523

524
  tEncodeSize(tEncodeSVDropTbBatchReq, &sentBatchReqs, size, code);
16,260!
525
  tEncoderInit(&ec, POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), size);
16,260✔
526
  code = tEncodeSVDropTbBatchReq(&ec, &sentBatchReqs);
16,260✔
527
  tEncoderClear(&ec);
16,260✔
528
  if (code != TSDB_CODE_SUCCESS) {
16,260!
529
    vError("vgId:%d %s failed to encode drop tb batch req: %s", TD_VID(pVnode), __func__, tstrerror(code));
×
530
    TSDB_CHECK_CODE(code, lino, _exit);
×
531
  }
532

533
_exit:
16,260✔
534
  tDecoderClear(&dc);
16,260✔
535
  if (sentBatchReqs.pArray) {
16,260!
536
    taosArrayDestroy(sentBatchReqs.pArray);
16,260✔
537
  }
538
  return code;
16,260✔
539
}
540

541
int32_t vnodePreProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
4,346,879✔
542
  int32_t code = 0;
4,346,879✔
543

544
  switch (pMsg->msgType) {
4,346,879✔
545
    case TDMT_VND_CREATE_TABLE: {
76,932✔
546
      code = vnodePreProcessCreateTableMsg(pVnode, pMsg);
76,932✔
547
    } break;
76,925✔
548
    case TDMT_VND_ALTER_TABLE: {
5,198✔
549
      code = vnodePreProcessAlterTableMsg(pVnode, pMsg);
5,198✔
550
    } break;
5,198✔
551
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
96,337✔
552
    case TDMT_VND_DROP_TTL_TABLE: {
553
      code = vnodePreProcessDropTtlMsg(pVnode, pMsg);
96,337✔
554
    } break;
96,068✔
555
    case TDMT_VND_SUBMIT: {
3,983,178✔
556
      code = vnodePreProcessSubmitMsg(pVnode, pMsg);
3,983,178✔
557
    } break;
3,983,426✔
558
    case TDMT_VND_DELETE: {
57,625✔
559
      code = vnodePreProcessDeleteMsg(pVnode, pMsg);
57,625✔
560
    } break;
57,625✔
561
    case TDMT_VND_BATCH_DEL: {
2,885✔
562
      code = vnodePreProcessBatchDeleteMsg(pVnode, pMsg);
2,885✔
563
    } break;
2,887✔
564
    case TDMT_VND_ARB_CHECK_SYNC: {
3✔
565
      code = vnodePreProcessArbCheckSyncMsg(pVnode, pMsg);
3✔
566
    } break;
3✔
567
    case TDMT_VND_DROP_TABLE: {
16,260✔
568
      code = vnodePreProcessDropTbMsg(pVnode, pMsg);
16,260✔
569
    } break;
16,260✔
570
    default:
108,461✔
571
      break;
108,461✔
572
  }
573

574
  if (code && code != TSDB_CODE_MSG_PREPROCESSED) {
4,346,853✔
575
    vError("vgId:%d, failed to preprocess write request since %s, msg type:%s", TD_VID(pVnode), tstrerror(code),
99!
576
           TMSG_INFO(pMsg->msgType));
577
  }
578
  return code;
4,346,817✔
579
}
580

581
int32_t vnodeProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg, int64_t ver, SRpcMsg *pRsp) {
5,659,469✔
582
  int32_t code = 0;
5,659,469✔
583
  void   *ptr = NULL;
5,659,469✔
584
  void   *pReq;
585
  int32_t len;
586

587
  (void)taosThreadMutexLock(&pVnode->mutex);
5,659,469✔
588
  if (pVnode->disableWrite) {
5,659,906!
589
    (void)taosThreadMutexUnlock(&pVnode->mutex);
×
590
    vError("vgId:%d write is disabled for snapshot, version:%" PRId64, TD_VID(pVnode), ver);
×
591
    return TSDB_CODE_VND_WRITE_DISABLED;
×
592
  }
593
  (void)taosThreadMutexUnlock(&pVnode->mutex);
5,659,906✔
594

595
  if (ver <= pVnode->state.applied) {
5,659,875!
596
    vError("vgId:%d, duplicate write request. ver: %" PRId64 ", applied: %" PRId64 "", TD_VID(pVnode), ver,
×
597
           pVnode->state.applied);
598
    return terrno = TSDB_CODE_VND_DUP_REQUEST;
×
599
  }
600

601
  vDebug("vgId:%d, start to process write request %s, index:%" PRId64 ", applied:%" PRId64 ", state.applyTerm:%" PRId64
5,659,875!
602
         ", conn.applyTerm:%" PRId64,
603
         TD_VID(pVnode), TMSG_INFO(pMsg->msgType), ver, pVnode->state.applied, pVnode->state.applyTerm,
604
         pMsg->info.conn.applyTerm);
605

606
  if (!(pVnode->state.applyTerm <= pMsg->info.conn.applyTerm)) {
5,659,602!
607
    return terrno = TSDB_CODE_INTERNAL_ERROR;
×
608
  }
609

610
  if (!(pVnode->state.applied + 1 == ver)) {
5,659,602!
611
    return terrno = TSDB_CODE_INTERNAL_ERROR;
×
612
  }
613

614
  atomic_store_64(&pVnode->state.applied, ver);
5,659,602✔
615
  atomic_store_64(&pVnode->state.applyTerm, pMsg->info.conn.applyTerm);
5,659,962✔
616

617
  if (!syncUtilUserCommit(pMsg->msgType)) goto _exit;
5,659,967✔
618

619
  // skip header
620
  pReq = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
5,632,737✔
621
  len = pMsg->contLen - sizeof(SMsgHead);
5,632,737✔
622
  bool needCommit = false;
5,632,737✔
623

624
  switch (pMsg->msgType) {
5,632,737!
625
    /* META */
626
    case TDMT_VND_CREATE_STB:
29,758✔
627
      if (vnodeProcessCreateStbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
29,758✔
628
      break;
29,757✔
629
    case TDMT_VND_ALTER_STB:
4,853✔
630
      if (vnodeProcessAlterStbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
4,853!
631
      break;
4,870✔
632
    case TDMT_VND_DROP_STB:
2,336✔
633
      if (vnodeProcessDropStbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
2,336!
634
      break;
2,347✔
635
    case TDMT_VND_CREATE_TABLE:
84,872✔
636
      if (vnodeProcessCreateTbReq(pVnode, ver, pReq, len, pRsp, pMsg) < 0) goto _err;
84,872!
637
      break;
84,895✔
638
    case TDMT_VND_ALTER_TABLE:
5,200✔
639
      if (vnodeProcessAlterTbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
5,200!
640
      break;
5,200✔
641
    case TDMT_VND_DROP_TABLE:
16,302✔
642
      if (vnodeProcessDropTbReq(pVnode, ver, pReq, len, pRsp, pMsg) < 0) goto _err;
16,302!
643
      break;
16,302✔
644
    case TDMT_VND_DROP_TTL_TABLE:
×
645
      if (vnodeProcessDropTtlTbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
646
      break;
×
647
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
38✔
648
      if (vnodeProcessFetchTtlExpiredTbs(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
38!
649
      break;
38✔
650
    case TDMT_VND_TRIM:
242✔
651
      if (vnodeProcessTrimReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
242!
652
      break;
242✔
653
    case TDMT_VND_S3MIGRATE:
×
654
      if (vnodeProcessS3MigrateReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
655
      break;
×
656
    case TDMT_VND_CREATE_SMA:
33✔
657
      if (vnodeProcessCreateTSmaReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
33!
658
      break;
33✔
659
    /* TSDB */
660
    case TDMT_VND_SUBMIT:
5,330,628✔
661
      if (vnodeProcessSubmitReq(pVnode, ver, pMsg->pCont, pMsg->contLen, pRsp, pMsg) < 0) goto _err;
5,330,628✔
662
      break;
5,330,598✔
663
    case TDMT_VND_DELETE:
60,987✔
664
      if (vnodeProcessDeleteReq(pVnode, ver, pReq, len, pRsp, pMsg) < 0) goto _err;
60,987!
665
      break;
60,986✔
666
    case TDMT_VND_BATCH_DEL:
3,262✔
667
      if (vnodeProcessBatchDeleteReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
3,262!
668
      break;
3,266✔
669
    /* TQ */
670
    case TDMT_VND_TMQ_SUBSCRIBE:
4,241✔
671
      if (tqProcessSubscribeReq(pVnode->pTq, ver, pReq, len) < 0) {
4,241✔
672
        goto _err;
1✔
673
      }
674
      break;
4,244✔
675
    case TDMT_VND_TMQ_DELETE_SUB:
878✔
676
      if (tqProcessDeleteSubReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen) < 0) {
878!
677
        goto _err;
×
678
      }
679
      break;
879✔
680
    case TDMT_VND_TMQ_COMMIT_OFFSET:
9,674✔
681
      if (tqProcessOffsetCommitReq(pVnode->pTq, ver, pReq, len) < 0) {
9,674!
682
        goto _err;
×
683
      }
684
      break;
9,681✔
685
    case TDMT_VND_TMQ_ADD_CHECKINFO:
130✔
686
      if (tqProcessAddCheckInfoReq(pVnode->pTq, ver, pReq, len) < 0) {
130!
687
        goto _err;
×
688
      }
689
      break;
130✔
690
    case TDMT_VND_TMQ_DEL_CHECKINFO:
24✔
691
      if (tqProcessDelCheckInfoReq(pVnode->pTq, ver, pReq, len) < 0) {
24✔
692
        goto _err;
2✔
693
      }
694
      break;
22✔
695
    case TDMT_STREAM_TASK_DEPLOY: {
13,606✔
696
      if ((code = tqProcessTaskDeployReq(pVnode->pTq, ver, pReq, len)) != TSDB_CODE_SUCCESS) {
13,606!
697
        goto _err;
×
698
      }
699
    } break;
13,625✔
700
    case TDMT_STREAM_TASK_DROP: {
6,925✔
701
      if ((code = tqProcessTaskDropReq(pVnode->pTq, pMsg->pCont, pMsg->contLen)) < 0) {
6,925!
702
        goto _err;
×
703
      }
704
    } break;
6,943✔
705
    case TDMT_STREAM_TASK_UPDATE_CHKPT: {
6,916✔
706
      if ((code = tqProcessTaskUpdateCheckpointReq(pVnode->pTq, pMsg->pCont, pMsg->contLen)) < 0) {
6,916!
707
        goto _err;
×
708
      }
709
    } break;
6,918✔
710
    case TDMT_STREAM_CONSEN_CHKPT: {
202✔
711
      if (pVnode->restored && (code = tqProcessTaskConsenChkptIdReq(pVnode->pTq, pMsg)) < 0) {
202!
712
        goto _err;
×
713
      }
714

715
    } break;
202✔
716
    case TDMT_STREAM_TASK_PAUSE: {
1,894✔
717
      if (pVnode->restored && vnodeIsLeader(pVnode) &&
3,196!
718
          (code = tqProcessTaskPauseReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen)) < 0) {
1,301✔
719
        goto _err;
×
720
      }
721
    } break;
1,896✔
722
    case TDMT_STREAM_TASK_RESUME: {
1,889✔
723
      if (pVnode->restored && vnodeIsLeader(pVnode) &&
3,184!
724
          (code = tqProcessTaskResumeReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen)) < 0) {
1,294✔
725
        goto _err;
1,259✔
726
      }
727
    } break;
630✔
728
    case TDMT_VND_STREAM_TASK_RESET: {
×
729
      if (pVnode->restored && vnodeIsLeader(pVnode) &&
×
730
           (code = tqProcessTaskResetReq(pVnode->pTq, pMsg)) < 0) {
×
731
          goto _err;
×
732
        }
733

734
    } break;
×
735
    case TDMT_VND_ALTER_CONFIRM:
8,146✔
736
      needCommit = pVnode->config.hashChange;
8,146✔
737
      if (vnodeProcessAlterConfirmReq(pVnode, ver, pReq, len, pRsp) < 0) {
8,146!
738
        goto _err;
×
739
      }
740
      break;
8,146✔
741
    case TDMT_VND_ALTER_CONFIG:
869✔
742
      vnodeProcessAlterConfigReq(pVnode, ver, pReq, len, pRsp);
869✔
743
      break;
874✔
744
    case TDMT_VND_COMMIT:
31,264✔
745
      needCommit = true;
31,264✔
746
      break;
31,264✔
747
    case TDMT_VND_CREATE_INDEX:
991✔
748
      vnodeProcessCreateIndexReq(pVnode, ver, pReq, len, pRsp);
991✔
749
      break;
991✔
750
    case TDMT_VND_DROP_INDEX:
2,158✔
751
      vnodeProcessDropIndexReq(pVnode, ver, pReq, len, pRsp);
2,158✔
752
      break;
2,158✔
753
    case TDMT_VND_STREAM_CHECK_POINT_SOURCE: // always return true
4,284✔
754
      tqProcessTaskCheckPointSourceReq(pVnode->pTq, pMsg, pRsp);
4,284✔
755
      break;
4,292✔
756
    case TDMT_VND_STREAM_TASK_UPDATE:  // always return true
82✔
757
      tqProcessTaskUpdateReq(pVnode->pTq, pMsg);
82✔
758
      break;
82✔
759
    case TDMT_VND_COMPACT:
53✔
760
      vnodeProcessCompactVnodeReq(pVnode, ver, pReq, len, pRsp);
53✔
761
      goto _exit;
53✔
762
    case TDMT_SYNC_CONFIG_CHANGE:
×
763
      vnodeProcessConfigChangeReq(pVnode, ver, pReq, len, pRsp);
×
764
      break;
×
765
#ifdef TD_ENTERPRISE
766
    case TDMT_VND_KILL_COMPACT:
×
767
      vnodeProcessKillCompactReq(pVnode, ver, pReq, len, pRsp);
×
768
      break;
×
769
#endif
770
    /* ARB */
771
    case TDMT_VND_ARB_CHECK_SYNC:
×
772
      vnodeProcessArbCheckSyncReq(pVnode, pReq, len, pRsp);
×
773
      break;
×
774
    default:
×
775
      vError("vgId:%d, unprocessed msg, %d", TD_VID(pVnode), pMsg->msgType);
×
776
      return TSDB_CODE_INVALID_MSG;
×
777
  }
778

779
  vTrace("vgId:%d, process %s request, code:0x%x index:%" PRId64, TD_VID(pVnode), TMSG_INFO(pMsg->msgType), pRsp->code,
5,631,511!
780
         ver);
781

782
  walApplyVer(pVnode->pWal, ver);
5,631,511✔
783

784
  code = tqPushMsg(pVnode->pTq, pMsg->msgType);
5,631,477✔
785
  if (code) {
5,631,549✔
786
    vError("vgId:%d, failed to push msg to TQ since %s", TD_VID(pVnode), tstrerror(terrno));
538!
787
    return code;
538✔
788
  }
789

790
  // commit if need
791
  if (needCommit) {
5,631,011✔
792
    vInfo("vgId:%d, commit at version %" PRId64, TD_VID(pVnode), ver);
30,829!
793
    code = vnodeAsyncCommit(pVnode);
30,843✔
794
    if (code) {
30,843!
795
      vError("vgId:%d, failed to vnode async commit since %s.", TD_VID(pVnode), tstrerror(terrno));
×
796
      goto _err;
×
797
    }
798

799
    // start a new one
800
    code = vnodeBegin(pVnode);
30,843✔
801
    if (code) {
30,836✔
802
      vError("vgId:%d, failed to begin vnode since %s.", TD_VID(pVnode), tstrerror(terrno));
35!
803
      goto _err;
×
804
    }
805
  }
806

807
_exit:
5,630,983✔
808
  return 0;
5,657,728✔
809

810
_err:
1,285✔
811
  vError("vgId:%d, process %s request failed since %s, ver:%" PRId64, TD_VID(pVnode), TMSG_INFO(pMsg->msgType),
1,285!
812
         tstrerror(code), ver);
813
  return code;
1,286✔
814
}
815

816
int32_t vnodePreprocessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg) {
7,239,250✔
817
  if (TDMT_SCH_QUERY != pMsg->msgType && TDMT_SCH_MERGE_QUERY != pMsg->msgType) {
7,239,250✔
818
    return 0;
363,956✔
819
  }
820

821
  return qWorkerPreprocessQueryMsg(pVnode->pQuery, pMsg, TDMT_SCH_QUERY == pMsg->msgType);
6,875,294✔
822
}
823

824
int32_t vnodeProcessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
7,249,491✔
825
  vTrace("message in vnode query queue is processing");
7,249,491✔
826
  if ((pMsg->msgType == TDMT_SCH_QUERY || pMsg->msgType == TDMT_VND_TMQ_CONSUME) && !syncIsReadyForRead(pVnode->sync)) {
7,249,491✔
827
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
1,608✔
828
    return 0;
1,608✔
829
  }
830

831
  if (pMsg->msgType == TDMT_VND_TMQ_CONSUME && !pVnode->restored) {
7,247,462!
832
    vnodeRedirectRpcMsg(pVnode, pMsg, TSDB_CODE_SYN_RESTORING);
×
833
    return 0;
×
834
  }
835

836
  SReadHandle handle = {.vnode = pVnode, .pMsgCb = &pVnode->msgCb, .pWorkerCb = pInfo->workerCb};
7,247,462✔
837
  initStorageAPI(&handle.api);
7,247,462✔
838

839
  switch (pMsg->msgType) {
7,244,300!
840
    case TDMT_SCH_QUERY:
6,882,071✔
841
    case TDMT_SCH_MERGE_QUERY:
842
      return qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
6,882,071✔
843
    case TDMT_SCH_QUERY_CONTINUE:
255,275✔
844
      return qWorkerProcessCQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
255,275✔
845
    case TDMT_VND_TMQ_CONSUME:
78,513✔
846
      return tqProcessPollReq(pVnode->pTq, pMsg);
78,513✔
847
    case TDMT_VND_TMQ_CONSUME_PUSH:
28,850✔
848
      return tqProcessPollPush(pVnode->pTq, pMsg);
28,850✔
849
    default:
×
850
      vError("unknown msg type:%d in query queue", pMsg->msgType);
×
851
      return TSDB_CODE_APP_ERROR;
×
852
  }
853
}
854

855
int32_t vnodeProcessFetchMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
18,650,607✔
856
  vTrace("vgId:%d, msg:%p in fetch queue is processing", pVnode->config.vgId, pMsg);
18,650,607✔
857
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
18,650,607✔
858
       pMsg->msgType == TDMT_VND_BATCH_META || pMsg->msgType == TDMT_VND_TABLE_NAME) &&
13,261,791✔
859
      !syncIsReadyForRead(pVnode->sync)) {
6,486,877✔
860
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
1,863✔
861
    return 0;
1,863✔
862
  }
863

864
  switch (pMsg->msgType) {
18,651,055!
865
    case TDMT_SCH_FETCH:
9,036,904✔
866
    case TDMT_SCH_MERGE_FETCH:
867
      return qWorkerProcessFetchMsg(pVnode, pVnode->pQuery, pMsg, 0);
9,036,904✔
868
    case TDMT_SCH_FETCH_RSP:
×
869
      return qWorkerProcessRspMsg(pVnode, pVnode->pQuery, pMsg, 0);
×
870
    // case TDMT_SCH_CANCEL_TASK:
871
    //   return qWorkerProcessCancelMsg(pVnode, pVnode->pQuery, pMsg, 0);
872
    case TDMT_SCH_DROP_TASK:
6,881,899✔
873
      return qWorkerProcessDropMsg(pVnode, pVnode->pQuery, pMsg, 0);
6,881,899✔
874
    case TDMT_SCH_TASK_NOTIFY:
159✔
875
      return qWorkerProcessNotifyMsg(pVnode, pVnode->pQuery, pMsg, 0);
159✔
876
    case TDMT_SCH_QUERY_HEARTBEAT:
1,638,984✔
877
      return qWorkerProcessHbMsg(pVnode, pVnode->pQuery, pMsg, 0);
1,638,984✔
878
    case TDMT_VND_TABLE_META:
1,765✔
879
    case TDMT_VND_TABLE_NAME:
880
      return vnodeGetTableMeta(pVnode, pMsg, true);
1,765✔
881
    case TDMT_VND_TABLE_CFG:
×
882
      return vnodeGetTableCfg(pVnode, pMsg, true);
×
883
    case TDMT_VND_BATCH_META:
1,091,261✔
884
      return vnodeGetBatchMeta(pVnode, pMsg);
1,091,261✔
885
#ifdef TD_ENTERPRISE
886
    case TDMT_VND_QUERY_COMPACT_PROGRESS:
45✔
887
      return vnodeQueryCompactProgress(pVnode, pMsg);
45✔
888
#endif
889
      //    case TDMT_VND_TMQ_CONSUME:
890
      //      return tqProcessPollReq(pVnode->pTq, pMsg);
891
    case TDMT_VND_TMQ_VG_WALINFO:
13✔
892
      return tqProcessVgWalInfoReq(pVnode->pTq, pMsg);
13✔
893
    case TDMT_VND_TMQ_VG_COMMITTEDINFO:
1✔
894
      return tqProcessVgCommittedInfoReq(pVnode->pTq, pMsg);
1✔
895
    case TDMT_VND_TMQ_SEEK:
24✔
896
      return tqProcessSeekReq(pVnode->pTq, pMsg);
24✔
897

898
    default:
×
899
      vError("unknown msg type:%d in fetch queue", pMsg->msgType);
×
900
      return TSDB_CODE_APP_ERROR;
×
901
  }
902
}
903

904
int32_t vnodeProcessStreamMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
355,657✔
905
  vTrace("vgId:%d, msg:%p in fetch queue is processing", pVnode->config.vgId, pMsg);
355,657✔
906
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
355,657!
907
       pMsg->msgType == TDMT_VND_BATCH_META) &&
355,682!
908
      !syncIsReadyForRead(pVnode->sync)) {
×
909
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
×
910
    return 0;
×
911
  }
912

913
  switch (pMsg->msgType) {
355,688!
914
    case TDMT_STREAM_TASK_RUN:
194,727✔
915
      return tqProcessTaskRunReq(pVnode->pTq, pMsg);
194,727✔
916
    case TDMT_STREAM_TASK_DISPATCH:
32,852✔
917
      return tqProcessTaskDispatchReq(pVnode->pTq, pMsg);
32,852✔
918
    case TDMT_STREAM_TASK_DISPATCH_RSP:
33,035✔
919
      return tqProcessTaskDispatchRsp(pVnode->pTq, pMsg);
33,035✔
920
    case TDMT_VND_STREAM_TASK_CHECK:
20,894✔
921
      return tqProcessTaskCheckReq(pVnode->pTq, pMsg);
20,894✔
922
    case TDMT_VND_STREAM_TASK_CHECK_RSP:
20,954✔
923
      return tqProcessTaskCheckRsp(pVnode->pTq, pMsg);
20,954✔
924
    case TDMT_STREAM_RETRIEVE:
542✔
925
      return tqProcessTaskRetrieveReq(pVnode->pTq, pMsg);
542✔
926
    case TDMT_STREAM_RETRIEVE_RSP:
436✔
927
      return tqProcessTaskRetrieveRsp(pVnode->pTq, pMsg);
436✔
928
    case TDMT_VND_STREAM_SCAN_HISTORY:
2,552✔
929
      return tqProcessTaskScanHistory(pVnode->pTq, pMsg);
2,552✔
930
    case TDMT_STREAM_TASK_CHECKPOINT_READY:
8,174✔
931
      return tqProcessTaskCheckpointReadyMsg(pVnode->pTq, pMsg);
8,174✔
932
    case TDMT_STREAM_TASK_CHECKPOINT_READY_RSP:
8,159✔
933
      return tqProcessTaskCheckpointReadyRsp(pVnode->pTq, pMsg);
8,159✔
934
    case TDMT_STREAM_RETRIEVE_TRIGGER:
×
935
      return tqProcessTaskRetrieveTriggerReq(pVnode->pTq, pMsg);
×
936
    case TDMT_STREAM_RETRIEVE_TRIGGER_RSP:
×
937
      return tqProcessTaskRetrieveTriggerRsp(pVnode->pTq, pMsg);
×
938
    case TDMT_MND_STREAM_HEARTBEAT_RSP:
22,415✔
939
      return tqProcessStreamHbRsp(pVnode->pTq, pMsg);
22,415✔
940
    case TDMT_MND_STREAM_REQ_CHKPT_RSP:
4,692✔
941
      return tqProcessStreamReqCheckpointRsp(pVnode->pTq, pMsg);
4,692✔
942
    case TDMT_VND_GET_STREAM_PROGRESS:
×
943
      return tqStreamProgressRetrieveReq(pVnode->pTq, pMsg);
×
944
    case TDMT_MND_STREAM_CHKPT_REPORT_RSP:
6,256✔
945
      return tqProcessTaskChkptReportRsp(pVnode->pTq, pMsg);
6,256✔
946
    default:
×
947
      vError("unknown msg type:%d in stream queue", pMsg->msgType);
×
948
      return TSDB_CODE_APP_ERROR;
×
949
  }
950
}
951

952
void smaHandleRes(void *pVnode, int64_t smaId, const SArray *data) {
3✔
953
  int32_t code = tdProcessTSmaInsert(((SVnode *)pVnode)->pSma, smaId, (const char *)data);
3✔
954
  if (code) {
3!
955
    vError("failed to process sma result since %s", tstrerror(code));
×
956
  }
957
}
3✔
958

959
void vnodeUpdateMetaRsp(SVnode *pVnode, STableMetaRsp *pMetaRsp) {
171,741✔
960
  if (NULL == pMetaRsp) {
171,741!
961
    return;
×
962
  }
963

964
  strcpy(pMetaRsp->dbFName, pVnode->config.dbname);
171,741✔
965
  pMetaRsp->dbId = pVnode->config.dbId;
171,741✔
966
  pMetaRsp->vgId = TD_VID(pVnode);
171,741✔
967
  pMetaRsp->precision = pVnode->config.tsdbCfg.precision;
171,741✔
968
}
969

970
extern int32_t vnodeAsyncRetention(SVnode *pVnode, int64_t now);
971

972
static int32_t vnodeProcessTrimReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
242✔
973
  int32_t     code = 0;
242✔
974
  SVTrimDbReq trimReq = {0};
242✔
975

976
  // decode
977
  if (tDeserializeSVTrimDbReq(pReq, len, &trimReq) != 0) {
242!
978
    code = TSDB_CODE_INVALID_MSG;
×
979
    goto _exit;
×
980
  }
981

982
  vInfo("vgId:%d, trim vnode request will be processed, time:%d", pVnode->config.vgId, trimReq.timestamp);
242!
983

984
  code = vnodeAsyncRetention(pVnode, trimReq.timestamp);
242✔
985

986
_exit:
242✔
987
  return code;
242✔
988
}
989

990
extern int32_t vnodeAsyncS3Migrate(SVnode *pVnode, int64_t now);
991

992
static int32_t vnodeProcessS3MigrateReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
993
  int32_t          code = 0;
×
994
  SVS3MigrateDbReq s3migrateReq = {0};
×
995

996
  // decode
997
  if (tDeserializeSVS3MigrateDbReq(pReq, len, &s3migrateReq) != 0) {
×
998
    code = TSDB_CODE_INVALID_MSG;
×
999
    goto _exit;
×
1000
  }
1001

1002
  vInfo("vgId:%d, s3migrate vnode request will be processed, time:%d", pVnode->config.vgId, s3migrateReq.timestamp);
×
1003

1004
  code = vnodeAsyncS3Migrate(pVnode, s3migrateReq.timestamp);
×
1005

1006
_exit:
×
1007
  return code;
×
1008
}
1009

1010
static int32_t vnodeProcessDropTtlTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1011
  int               ret = 0;
×
1012
  SVDropTtlTableReq ttlReq = {0};
×
1013
  if (tDeserializeSVDropTtlTableReq(pReq, len, &ttlReq) != 0) {
×
1014
    ret = TSDB_CODE_INVALID_MSG;
×
1015
    goto end;
×
1016
  }
1017

1018
  if (ttlReq.nUids != taosArrayGetSize(ttlReq.pTbUids)) {
×
1019
    ret = TSDB_CODE_INVALID_MSG;
×
1020
    goto end;
×
1021
  }
1022

1023
  if (ttlReq.nUids != 0) {
×
1024
    vInfo("vgId:%d, drop ttl table req will be processed, time:%d, ntbUids:%d", pVnode->config.vgId,
×
1025
          ttlReq.timestampSec, ttlReq.nUids);
1026
  }
1027

1028
  if (ttlReq.nUids > 0) {
×
1029
    int32_t code = metaDropTables(pVnode->pMeta, ttlReq.pTbUids);
×
1030
    if (code) return code;
×
1031

1032
    code = tqUpdateTbUidList(pVnode->pTq, ttlReq.pTbUids, false);
×
1033
    if (code) {
×
1034
      vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1035
    }
1036
  }
1037

1038
end:
×
1039
  taosArrayDestroy(ttlReq.pTbUids);
×
1040
  return ret;
×
1041
}
1042

1043
static int32_t vnodeProcessFetchTtlExpiredTbs(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
38✔
1044
  int32_t                 code = -1;
38✔
1045
  SMetaReader             mr = {0};
38✔
1046
  SVDropTtlTableReq       ttlReq = {0};
38✔
1047
  SVFetchTtlExpiredTbsRsp rsp = {0};
38✔
1048
  SEncoder                encoder = {0};
38✔
1049
  SArray                 *pNames = NULL;
38✔
1050
  pRsp->msgType = TDMT_VND_FETCH_TTL_EXPIRED_TBS_RSP;
38✔
1051
  pRsp->code = TSDB_CODE_SUCCESS;
38✔
1052
  pRsp->pCont = NULL;
38✔
1053
  pRsp->contLen = 0;
38✔
1054

1055
  if (tDeserializeSVDropTtlTableReq(pReq, len, &ttlReq) != 0) {
38!
1056
    terrno = TSDB_CODE_INVALID_MSG;
×
1057
    goto _end;
×
1058
  }
1059

1060
  if (!(ttlReq.nUids == taosArrayGetSize(ttlReq.pTbUids))) {
38!
1061
    terrno = TSDB_CODE_INVALID_MSG;
×
1062
    goto _end;
×
1063
  }
1064

1065
  tb_uid_t    suid;
1066
  char        ctbName[TSDB_TABLE_NAME_LEN];
1067
  SVDropTbReq expiredTb = {.igNotExists = true};
38✔
1068
  metaReaderDoInit(&mr, pVnode->pMeta, 0);
38✔
1069
  rsp.vgId = TD_VID(pVnode);
38✔
1070
  rsp.pExpiredTbs = taosArrayInit(ttlReq.nUids, sizeof(SVDropTbReq));
38✔
1071
  if (!rsp.pExpiredTbs) goto _end;
38!
1072

1073
  pNames = taosArrayInit(ttlReq.nUids, TSDB_TABLE_NAME_LEN);
38✔
1074
  if (!pNames) {
38!
1075
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1076
    goto _end;
×
1077
  }
1078
  char buf[TSDB_TABLE_NAME_LEN];
1079
  for (int32_t i = 0; i < ttlReq.nUids; ++i) {
179✔
1080
    tb_uid_t *uid = taosArrayGet(ttlReq.pTbUids, i);
141✔
1081
    expiredTb.suid = *uid;
141✔
1082
    terrno = metaReaderGetTableEntryByUid(&mr, *uid);
141✔
1083
    if (terrno < 0) goto _end;
140!
1084
    strncpy(buf, mr.me.name, TSDB_TABLE_NAME_LEN);
140✔
1085
    void *p = taosArrayPush(pNames, buf);
140✔
1086
    if (p == NULL) {
140!
1087
      goto _end;
×
1088
    }
1089

1090
    expiredTb.name = p;
140✔
1091
    if (mr.me.type == TSDB_CHILD_TABLE) {
140✔
1092
      expiredTb.suid = mr.me.ctbEntry.suid;
91✔
1093
    }
1094

1095
    if (taosArrayPush(rsp.pExpiredTbs, &expiredTb) == NULL) {
281!
1096
      goto _end;
×
1097
    }
1098
  }
1099

1100
  int32_t ret = 0;
38✔
1101
  tEncodeSize(tEncodeVFetchTtlExpiredTbsRsp, &rsp, pRsp->contLen, ret);
38!
1102
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
38✔
1103
  if (pRsp->pCont == NULL) {
38!
1104
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1105
    code = -1;
×
1106
    goto _end;
×
1107
  }
1108
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
38✔
1109
  terrno = tEncodeVFetchTtlExpiredTbsRsp(&encoder, &rsp);
38✔
1110
  tEncoderClear(&encoder);
38✔
1111

1112
  if (terrno == 0) code = 0;
38!
1113
_end:
×
1114
  metaReaderClear(&mr);
38✔
1115
  tFreeFetchTtlExpiredTbsRsp(&rsp);
38✔
1116
  taosArrayDestroy(ttlReq.pTbUids);
38✔
1117
  if (pNames) taosArrayDestroy(pNames);
38!
1118
  pRsp->code = terrno;
38✔
1119
  return code;
38✔
1120
}
1121

1122
static int32_t vnodeProcessCreateStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
29,750✔
1123
  int32_t        code = 0;
29,750✔
1124
  SVCreateStbReq req = {0};
29,750✔
1125
  SDecoder       coder;
1126

1127
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
29,750✔
1128
  pRsp->code = TSDB_CODE_SUCCESS;
29,750✔
1129
  pRsp->pCont = NULL;
29,750✔
1130
  pRsp->contLen = 0;
29,750✔
1131

1132
  // decode and process req
1133
  tDecoderInit(&coder, pReq, len);
29,750✔
1134

1135
  code = tDecodeSVCreateStbReq(&coder, &req);
29,752✔
1136
  if (code) {
29,773!
1137
    pRsp->code = code;
×
1138
    goto _err;
×
1139
  }
1140

1141
  code = metaCreateSTable(pVnode->pMeta, ver, &req);
29,773✔
1142
  if (code) {
29,751✔
1143
    pRsp->code = code;
4✔
1144
    goto _err;
4✔
1145
  }
1146

1147
  if ((code = tdProcessRSmaCreate(pVnode->pSma, &req)) < 0) {
29,747!
1148
    pRsp->code = code;
×
1149
    goto _err;
×
1150
  }
1151

1152
  tDecoderClear(&coder);
29,829✔
1153
  return 0;
29,829✔
1154

1155
_err:
4✔
1156
  tDecoderClear(&coder);
4✔
1157
  return code;
4✔
1158
}
1159

1160
static int32_t vnodeProcessCreateTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
84,865✔
1161
                                       SRpcMsg *pOriginRpc) {
1162
  SDecoder           decoder = {0};
84,865✔
1163
  SEncoder           encoder = {0};
84,865✔
1164
  int32_t            rcode = 0;
84,865✔
1165
  SVCreateTbBatchReq req = {0};
84,865✔
1166
  SVCreateTbReq     *pCreateReq;
1167
  SVCreateTbBatchRsp rsp = {0};
84,865✔
1168
  SVCreateTbRsp      cRsp = {0};
84,865✔
1169
  char               tbName[TSDB_TABLE_FNAME_LEN];
1170
  STbUidStore       *pStore = NULL;
84,865✔
1171
  SArray            *tbUids = NULL;
84,865✔
1172
  SArray            *tbNames = NULL;
84,865✔
1173
  pRsp->msgType = TDMT_VND_CREATE_TABLE_RSP;
84,865✔
1174
  pRsp->code = TSDB_CODE_SUCCESS;
84,865✔
1175
  pRsp->pCont = NULL;
84,865✔
1176
  pRsp->contLen = 0;
84,865✔
1177

1178
  // decode
1179
  tDecoderInit(&decoder, pReq, len);
84,865✔
1180
  if (tDecodeSVCreateTbBatchReq(&decoder, &req) < 0) {
84,863!
1181
    rcode = -1;
×
1182
    terrno = TSDB_CODE_INVALID_MSG;
×
1183
    goto _exit;
×
1184
  }
1185

1186
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(cRsp));
84,887✔
1187
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
84,896✔
1188
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
84,893✔
1189
  if (rsp.pArray == NULL || tbUids == NULL || tbNames == NULL) {
84,893!
1190
    rcode = -1;
1✔
1191
    terrno = TSDB_CODE_OUT_OF_MEMORY;
1✔
1192
    goto _exit;
×
1193
  }
1194

1195
  // loop to create table
1196
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
210,044✔
1197
    pCreateReq = req.pReqs + iReq;
125,166✔
1198
    memset(&cRsp, 0, sizeof(cRsp));
125,166✔
1199

1200
    if (tsEnableAudit && tsEnableAuditCreateTable) {
125,166!
1201
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
125,167✔
1202
      if (str == NULL) {
125,184!
1203
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1204
        rcode = -1;
×
1205
        goto _exit;
×
1206
      }
1207
      strcpy(str, pCreateReq->name);
125,184✔
1208
      if (taosArrayPush(tbNames, &str) == NULL) {
125,180!
1209
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1210
        rcode = -1;
×
1211
        goto _exit;
×
1212
      }
1213
    }
1214

1215
    // validate hash
1216
    sprintf(tbName, "%s.%s", pVnode->config.dbname, pCreateReq->name);
125,179✔
1217
    if (vnodeValidateTableHash(pVnode, tbName) < 0) {
125,179✔
1218
      cRsp.code = TSDB_CODE_VND_HASH_MISMATCH;
25✔
1219
      if (taosArrayPush(rsp.pArray, &cRsp) == NULL) {
50!
1220
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1221
        rcode = -1;
×
1222
        goto _exit;
×
1223
      }
1224
      vError("vgId:%d create-table:%s failed due to hash value mismatch", TD_VID(pVnode), tbName);
25!
1225
      continue;
25✔
1226
    }
1227

1228
    // do create table
1229
    if (metaCreateTable(pVnode->pMeta, ver, pCreateReq, &cRsp.pMeta) < 0) {
125,151✔
1230
      if (pCreateReq->flags & TD_CREATE_IF_NOT_EXISTS && terrno == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
1,324!
1231
        cRsp.code = TSDB_CODE_SUCCESS;
3✔
1232
      } else {
1233
        cRsp.code = terrno;
1,321✔
1234
      }
1235
    } else {
1236
      cRsp.code = TSDB_CODE_SUCCESS;
123,825✔
1237
      if (tdFetchTbUidList(pVnode->pSma, &pStore, pCreateReq->ctb.suid, pCreateReq->uid) < 0) {
123,825!
1238
        vError("vgId:%d, failed to fetch tbUid list", TD_VID(pVnode));
×
1239
      }
1240
      if (taosArrayPush(tbUids, &pCreateReq->uid) == NULL) {
247,634!
1241
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1242
        rcode = -1;
×
1243
        goto _exit;
×
1244
      }
1245
      vnodeUpdateMetaRsp(pVnode, cRsp.pMeta);
123,814✔
1246
    }
1247

1248
    if (taosArrayPush(rsp.pArray, &cRsp) == NULL) {
250,263!
1249
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1250
      rcode = -1;
×
1251
      goto _exit;
×
1252
    }
1253
  }
1254

1255
  vDebug("vgId:%d, add %d new created tables into query table list", TD_VID(pVnode), (int32_t)taosArrayGetSize(tbUids));
84,878✔
1256
  if (tqUpdateTbUidList(pVnode->pTq, tbUids, true) < 0) {
84,878!
1257
    vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(terrno));
×
1258
  }
1259
  if (tdUpdateTbUidList(pVnode->pSma, pStore, true) < 0) {
84,892!
1260
    goto _exit;
×
1261
  }
1262
  pStore = tdUidStoreFree(pStore);
84,894✔
1263

1264
  // prepare rsp
1265
  int32_t ret = 0;
84,892✔
1266
  tEncodeSize(tEncodeSVCreateTbBatchRsp, &rsp, pRsp->contLen, ret);
84,892!
1267
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
84,893✔
1268
  if (pRsp->pCont == NULL) {
84,896!
1269
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1270
    rcode = -1;
×
1271
    goto _exit;
×
1272
  }
1273
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
84,896✔
1274
  if (tEncodeSVCreateTbBatchRsp(&encoder, &rsp) != 0) {
84,893!
1275
    vError("vgId:%d, failed to encode create table batch response", TD_VID(pVnode));
×
1276
  }
1277

1278
  if (tsEnableAudit && tsEnableAuditCreateTable) {
84,891!
1279
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
84,891✔
1280

1281
    SName name = {0};
84,891✔
1282
    if (tNameFromString(&name, pVnode->config.dbname, T_NAME_ACCT | T_NAME_DB) < 0) {
84,891!
1283
      vError("vgId:%d, failed to get name from string", TD_VID(pVnode));
×
1284
    }
1285

1286
    SStringBuilder sb = {0};
84,893✔
1287
    for (int32_t i = 0; i < tbNames->size; i++) {
210,030✔
1288
      char **key = (char **)taosArrayGet(tbNames, i);
125,151✔
1289
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
125,151✔
1290
      if (i < tbNames->size - 1) {
125,139✔
1291
        taosStringBuilderAppendChar(&sb, ',');
40,267✔
1292
      }
1293
      // taosMemoryFreeClear(*key);
1294
    }
1295

1296
    size_t len = 0;
84,879✔
1297
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
84,879✔
1298

1299
    if (pOriginRpc->info.conn.user != NULL && strlen(pOriginRpc->info.conn.user) > 0) {
84,886!
1300
      auditAddRecord(pOriginRpc, clusterId, "createTable", name.dbname, "", keyJoined, len);
70,142✔
1301
    }
1302

1303
    taosStringBuilderDestroy(&sb);
84,885✔
1304
  }
1305

1306
_exit:
×
1307
  tDeleteSVCreateTbBatchReq(&req);
84,885✔
1308
  taosArrayDestroyEx(rsp.pArray, tFreeSVCreateTbRsp);
84,892✔
1309
  taosArrayDestroy(tbUids);
84,896✔
1310
  tDecoderClear(&decoder);
84,894✔
1311
  tEncoderClear(&encoder);
84,892✔
1312
  taosArrayDestroyP(tbNames, taosMemoryFree);
84,893✔
1313
  return rcode;
84,895✔
1314
}
1315

1316
static int32_t vnodeProcessAlterStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
4,853✔
1317
  int32_t        code = 0;
4,853✔
1318
  SVCreateStbReq req = {0};
4,853✔
1319
  SDecoder       dc = {0};
4,853✔
1320

1321
  pRsp->msgType = TDMT_VND_ALTER_STB_RSP;
4,853✔
1322
  pRsp->code = TSDB_CODE_SUCCESS;
4,853✔
1323
  pRsp->pCont = NULL;
4,853✔
1324
  pRsp->contLen = 0;
4,853✔
1325

1326
  tDecoderInit(&dc, pReq, len);
4,853✔
1327

1328
  // decode req
1329
  code = tDecodeSVCreateStbReq(&dc, &req);
4,854✔
1330
  if (code) {
4,877!
1331
    tDecoderClear(&dc);
×
1332
    return code;
×
1333
  }
1334

1335
  code = metaAlterSTable(pVnode->pMeta, ver, &req);
4,877✔
1336
  if (code) {
4,871!
1337
    pRsp->code = code;
×
1338
    tDecoderClear(&dc);
×
1339
    return code;
×
1340
  }
1341

1342
  tDecoderClear(&dc);
4,871✔
1343

1344
  return 0;
4,874✔
1345
}
1346

1347
static int32_t vnodeProcessDropStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
2,335✔
1348
  SVDropStbReq req = {0};
2,335✔
1349
  int32_t      rcode = TSDB_CODE_SUCCESS;
2,335✔
1350
  SDecoder     decoder = {0};
2,335✔
1351
  SArray      *tbUidList = NULL;
2,335✔
1352

1353
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
2,335✔
1354
  pRsp->pCont = NULL;
2,335✔
1355
  pRsp->contLen = 0;
2,335✔
1356

1357
  // decode request
1358
  tDecoderInit(&decoder, pReq, len);
2,335✔
1359
  if (tDecodeSVDropStbReq(&decoder, &req) < 0) {
2,335!
1360
    rcode = TSDB_CODE_INVALID_MSG;
×
1361
    goto _exit;
×
1362
  }
1363

1364
  // process request
1365
  tbUidList = taosArrayInit(8, sizeof(int64_t));
2,344✔
1366
  if (tbUidList == NULL) goto _exit;
2,347!
1367
  if (metaDropSTable(pVnode->pMeta, ver, &req, tbUidList) < 0) {
2,347✔
1368
    rcode = terrno;
8✔
1369
    goto _exit;
8✔
1370
  }
1371

1372
  if (tqUpdateTbUidList(pVnode->pTq, tbUidList, false) < 0) {
2,339!
1373
    rcode = terrno;
×
1374
    goto _exit;
×
1375
  }
1376

1377
  if (tdProcessRSmaDrop(pVnode->pSma, &req) < 0) {
2,339!
1378
    rcode = terrno;
×
1379
    goto _exit;
×
1380
  }
1381

1382
  // return rsp
1383
_exit:
2,339✔
1384
  if (tbUidList) taosArrayDestroy(tbUidList);
2,347!
1385
  pRsp->code = rcode;
2,347✔
1386
  tDecoderClear(&decoder);
2,347✔
1387
  return 0;
2,347✔
1388
}
1389

1390
static int32_t vnodeProcessAlterTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
5,200✔
1391
  SVAlterTbReq  vAlterTbReq = {0};
5,200✔
1392
  SVAlterTbRsp  vAlterTbRsp = {0};
5,200✔
1393
  SDecoder      dc = {0};
5,200✔
1394
  int32_t       rcode = 0;
5,200✔
1395
  int32_t       ret;
1396
  SEncoder      ec = {0};
5,200✔
1397
  STableMetaRsp vMetaRsp = {0};
5,200✔
1398

1399
  pRsp->msgType = TDMT_VND_ALTER_TABLE_RSP;
5,200✔
1400
  pRsp->pCont = NULL;
5,200✔
1401
  pRsp->contLen = 0;
5,200✔
1402
  pRsp->code = TSDB_CODE_SUCCESS;
5,200✔
1403

1404
  tDecoderInit(&dc, pReq, len);
5,200✔
1405

1406
  // decode
1407
  if (tDecodeSVAlterTbReq(&dc, &vAlterTbReq) < 0) {
5,200!
1408
    vAlterTbRsp.code = TSDB_CODE_INVALID_MSG;
×
1409
    tDecoderClear(&dc);
×
1410
    rcode = -1;
×
1411
    goto _exit;
×
1412
  }
1413

1414
  // process
1415
  if (metaAlterTable(pVnode->pMeta, ver, &vAlterTbReq, &vMetaRsp) < 0) {
5,200✔
1416
    vAlterTbRsp.code = terrno;
942✔
1417
    tDecoderClear(&dc);
942✔
1418
    rcode = -1;
942✔
1419
    goto _exit;
942✔
1420
  }
1421
  tDecoderClear(&dc);
4,258✔
1422

1423
  if (NULL != vMetaRsp.pSchemas) {
4,258✔
1424
    vnodeUpdateMetaRsp(pVnode, &vMetaRsp);
1,042✔
1425
    vAlterTbRsp.pMeta = &vMetaRsp;
1,042✔
1426
  }
1427

1428
_exit:
3,216✔
1429
  tEncodeSize(tEncodeSVAlterTbRsp, &vAlterTbRsp, pRsp->contLen, ret);
5,200!
1430
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
5,200✔
1431
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
5,200✔
1432
  if (tEncodeSVAlterTbRsp(&ec, &vAlterTbRsp) != 0) {
5,200!
1433
    vError("vgId:%d, failed to encode alter table response", TD_VID(pVnode));
×
1434
  }
1435
  tEncoderClear(&ec);
5,200✔
1436
  if (vMetaRsp.pSchemas) {
5,200✔
1437
    taosMemoryFree(vMetaRsp.pSchemas);
1,042✔
1438
    taosMemoryFree(vMetaRsp.pSchemaExt);
1,042✔
1439
  }
1440
  return 0;
5,200✔
1441
}
1442

1443
static int32_t vnodeProcessDropTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
16,302✔
1444
                                     SRpcMsg *pOriginRpc) {
1445
  SVDropTbBatchReq req = {0};
16,302✔
1446
  SVDropTbBatchRsp rsp = {0};
16,302✔
1447
  SDecoder         decoder = {0};
16,302✔
1448
  SEncoder         encoder = {0};
16,302✔
1449
  int32_t          ret;
1450
  SArray          *tbUids = NULL;
16,302✔
1451
  STbUidStore     *pStore = NULL;
16,302✔
1452
  SArray          *tbNames = NULL;
16,302✔
1453

1454
  pRsp->msgType = TDMT_VND_DROP_TABLE_RSP;
16,302✔
1455
  pRsp->pCont = NULL;
16,302✔
1456
  pRsp->contLen = 0;
16,302✔
1457
  pRsp->code = TSDB_CODE_SUCCESS;
16,302✔
1458

1459
  // decode req
1460
  tDecoderInit(&decoder, pReq, len);
16,302✔
1461
  ret = tDecodeSVDropTbBatchReq(&decoder, &req);
16,302✔
1462
  if (ret < 0) {
16,302!
1463
    terrno = TSDB_CODE_INVALID_MSG;
×
1464
    pRsp->code = terrno;
×
1465
    goto _exit;
×
1466
  }
1467

1468
  // process req
1469
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
16,302✔
1470
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(SVDropTbRsp));
16,302✔
1471
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
16,302✔
1472
  if (tbUids == NULL || rsp.pArray == NULL || tbNames == NULL) goto _exit;
16,302!
1473

1474
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
32,629✔
1475
    SVDropTbReq *pDropTbReq = req.pReqs + iReq;
16,327✔
1476
    SVDropTbRsp  dropTbRsp = {0};
16,327✔
1477
    tb_uid_t     tbUid = 0;
16,327✔
1478

1479
    /* code */
1480
    ret = metaDropTable(pVnode->pMeta, ver, pDropTbReq, tbUids, &tbUid);
16,327✔
1481
    if (ret < 0) {
16,327!
1482
      if (pDropTbReq->igNotExists && terrno == TSDB_CODE_TDB_TABLE_NOT_EXIST) {
×
1483
        dropTbRsp.code = TSDB_CODE_SUCCESS;
×
1484
      } else {
1485
        dropTbRsp.code = terrno;
×
1486
      }
1487
    } else {
1488
      dropTbRsp.code = TSDB_CODE_SUCCESS;
16,327✔
1489
      if (tbUid > 0) {
16,327✔
1490
        if (tdFetchTbUidList(pVnode->pSma, &pStore, pDropTbReq->suid, tbUid) < 0) {
13,964!
1491
          vError("vgId:%d, failed to fetch tbUid list", TD_VID(pVnode));
×
1492
        }
1493
      }
1494
    }
1495

1496
    if (taosArrayPush(rsp.pArray, &dropTbRsp) == NULL) {
32,654!
1497
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1498
      pRsp->code = terrno;
×
1499
      goto _exit;
×
1500
    }
1501

1502
    if (tsEnableAuditCreateTable) {
16,327!
1503
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
16,327✔
1504
      if (str == NULL) {
16,327!
1505
        pRsp->code = terrno;
×
1506
        goto _exit;
×
1507
      }
1508
      strcpy(str, pDropTbReq->name);
16,327✔
1509
      if (taosArrayPush(tbNames, &str) == NULL) {
16,327!
1510
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1511
        pRsp->code = terrno;
×
1512
        goto _exit;
×
1513
      }
1514
    }
1515
  }
1516

1517
  if (tqUpdateTbUidList(pVnode->pTq, tbUids, false) < 0) {
16,302!
1518
    vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(terrno));
×
1519
  }
1520

1521
  if (tdUpdateTbUidList(pVnode->pSma, pStore, false) < 0) {
16,302!
1522
    goto _exit;
×
1523
  }
1524

1525
  if (tsEnableAuditCreateTable) {
16,302!
1526
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
16,302✔
1527

1528
    SName name = {0};
16,302✔
1529
    if (tNameFromString(&name, pVnode->config.dbname, T_NAME_ACCT | T_NAME_DB) != 0) {
16,302!
1530
      vError("vgId:%d, failed to get name from string", TD_VID(pVnode));
×
1531
    }
1532

1533
    SStringBuilder sb = {0};
16,302✔
1534
    for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
32,629✔
1535
      char **key = (char **)taosArrayGet(tbNames, iReq);
16,327✔
1536
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
16,327✔
1537
      if (iReq < req.nReqs - 1) {
16,327✔
1538
        taosStringBuilderAppendChar(&sb, ',');
25✔
1539
      }
1540
      taosMemoryFreeClear(*key);
16,327!
1541
    }
1542

1543
    size_t len = 0;
16,302✔
1544
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
16,302✔
1545

1546
    if (pOriginRpc->info.conn.user != NULL && strlen(pOriginRpc->info.conn.user) > 0) {
16,302!
1547
      auditAddRecord(pOriginRpc, clusterId, "dropTable", name.dbname, "", keyJoined, len);
16,205✔
1548
    }
1549

1550
    taosStringBuilderDestroy(&sb);
16,302✔
1551
  }
1552

1553
_exit:
×
1554
  taosArrayDestroy(tbUids);
16,302✔
1555
  pStore = tdUidStoreFree(pStore);
16,302✔
1556
  tDecoderClear(&decoder);
16,302✔
1557
  tEncodeSize(tEncodeSVDropTbBatchRsp, &rsp, pRsp->contLen, ret);
16,302!
1558
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
16,302✔
1559
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
16,302✔
1560
  if (tEncodeSVDropTbBatchRsp(&encoder, &rsp) != 0) {
16,302!
1561
    vError("vgId:%d, failed to encode drop table batch response", TD_VID(pVnode));
×
1562
  }
1563
  tEncoderClear(&encoder);
16,302✔
1564
  taosArrayDestroy(rsp.pArray);
16,302✔
1565
  taosArrayDestroy(tbNames);
16,302✔
1566
  return 0;
16,302✔
1567
}
1568

1569
#ifdef BUILD_NO_CALL
1570
static int32_t vnodeDebugPrintSingleSubmitMsg(SMeta *pMeta, SSubmitBlk *pBlock, SSubmitMsgIter *msgIter,
1571
                                              const char *tags) {
1572
  SSubmitBlkIter blkIter = {0};
1573
  STSchema      *pSchema = NULL;
1574
  tb_uid_t       suid = 0;
1575
  STSRow        *row = NULL;
1576
  int32_t        rv = -1;
1577

1578
  tInitSubmitBlkIter(msgIter, pBlock, &blkIter);
1579
  if (blkIter.row == NULL) return 0;
1580

1581
  int32_t code = metaGetTbTSchemaNotNull(pMeta, msgIter->suid, TD_ROW_SVER(blkIter.row), 1,
1582
                                         &pSchema);  // TODO: use the real schema
1583
  if (TSDB_CODE_SUCCESS != code) {
1584
    printf("%s:%d no valid schema\n", tags, __LINE__);
1585
    return code;
1586
  }
1587

1588
  suid = msgIter->suid;
1589
  rv = TD_ROW_SVER(blkIter.row);
1590

1591
  char __tags[128] = {0};
1592
  snprintf(__tags, 128, "%s: uid %" PRIi64 " ", tags, msgIter->uid);
1593
  while ((row = tGetSubmitBlkNext(&blkIter))) {
1594
    tdSRowPrint(row, pSchema, __tags);
1595
  }
1596

1597
  taosMemoryFreeClear(pSchema);
1598

1599
  return TSDB_CODE_SUCCESS;
1600
}
1601
#endif
1602
typedef struct SSubmitReqConvertCxt {
1603
  SSubmitMsgIter msgIter;
1604
  SSubmitBlk    *pBlock;
1605
  SSubmitBlkIter blkIter;
1606
  STSRow        *pRow;
1607
  STSRowIter     rowIter;
1608
  SSubmitTbData *pTbData;
1609
  STSchema      *pTbSchema;
1610
  SArray        *pColValues;
1611
} SSubmitReqConvertCxt;
1612

1613
static int32_t vnodeResetTableCxt(SMeta *pMeta, SSubmitReqConvertCxt *pCxt) {
×
1614
  taosMemoryFreeClear(pCxt->pTbSchema);
×
1615
  int32_t code = metaGetTbTSchemaNotNull(pMeta, pCxt->msgIter.suid > 0 ? pCxt->msgIter.suid : pCxt->msgIter.uid,
×
1616
                                         pCxt->msgIter.sversion, 1, &pCxt->pTbSchema);
1617
  if (TSDB_CODE_SUCCESS != code) {
×
1618
    return code;
×
1619
  }
1620
  tdSTSRowIterInit(&pCxt->rowIter, pCxt->pTbSchema);
×
1621

1622
  tDestroySubmitTbData(pCxt->pTbData, TSDB_MSG_FLG_ENCODE);
×
1623
  if (NULL == pCxt->pTbData) {
×
1624
    pCxt->pTbData = taosMemoryCalloc(1, sizeof(SSubmitTbData));
×
1625
    if (NULL == pCxt->pTbData) {
×
1626
      return terrno;
×
1627
    }
1628
  }
1629
  pCxt->pTbData->flags = 0;
×
1630
  pCxt->pTbData->suid = pCxt->msgIter.suid;
×
1631
  pCxt->pTbData->uid = pCxt->msgIter.uid;
×
1632
  pCxt->pTbData->sver = pCxt->msgIter.sversion;
×
1633
  pCxt->pTbData->pCreateTbReq = NULL;
×
1634
  pCxt->pTbData->aRowP = taosArrayInit(128, POINTER_BYTES);
×
1635
  if (NULL == pCxt->pTbData->aRowP) {
×
1636
    return terrno;
×
1637
  }
1638

1639
  taosArrayDestroy(pCxt->pColValues);
×
1640
  pCxt->pColValues = taosArrayInit(pCxt->pTbSchema->numOfCols, sizeof(SColVal));
×
1641
  if (NULL == pCxt->pColValues) {
×
1642
    return terrno;
×
1643
  }
1644
  for (int32_t i = 0; i < pCxt->pTbSchema->numOfCols; ++i) {
×
1645
    SColVal val = COL_VAL_NONE(pCxt->pTbSchema->columns[i].colId, pCxt->pTbSchema->columns[i].type);
×
1646
    if (taosArrayPush(pCxt->pColValues, &val) == NULL) {
×
1647
      return terrno;
×
1648
    }
1649
  }
1650

1651
  return TSDB_CODE_SUCCESS;
×
1652
}
1653

1654
static void vnodeDestroySubmitReqConvertCxt(SSubmitReqConvertCxt *pCxt) {
×
1655
  taosMemoryFreeClear(pCxt->pTbSchema);
×
1656
  tDestroySubmitTbData(pCxt->pTbData, TSDB_MSG_FLG_ENCODE);
×
1657
  taosMemoryFreeClear(pCxt->pTbData);
×
1658
  taosArrayDestroy(pCxt->pColValues);
×
1659
}
×
1660

1661
static int32_t vnodeCellValConvertToColVal(STColumn *pCol, SCellVal *pCellVal, SColVal *pColVal) {
×
1662
  if (tdValTypeIsNone(pCellVal->valType)) {
×
1663
    pColVal->flag = CV_FLAG_NONE;
×
1664
    return TSDB_CODE_SUCCESS;
×
1665
  }
1666

1667
  if (tdValTypeIsNull(pCellVal->valType)) {
×
1668
    pColVal->flag = CV_FLAG_NULL;
×
1669
    return TSDB_CODE_SUCCESS;
×
1670
  }
1671

1672
  if (IS_VAR_DATA_TYPE(pCol->type)) {
×
1673
    pColVal->value.nData = varDataLen(pCellVal->val);
×
1674
    pColVal->value.pData = (uint8_t *)varDataVal(pCellVal->val);
×
1675
  } else if (TSDB_DATA_TYPE_FLOAT == pCol->type) {
×
1676
    float f = GET_FLOAT_VAL(pCellVal->val);
×
1677
    memcpy(&pColVal->value.val, &f, sizeof(f));
×
1678
  } else if (TSDB_DATA_TYPE_DOUBLE == pCol->type) {
×
1679
    pColVal->value.val = *(int64_t *)pCellVal->val;
×
1680
  } else {
1681
    GET_TYPED_DATA(pColVal->value.val, int64_t, pCol->type, pCellVal->val);
×
1682
  }
1683

1684
  pColVal->flag = CV_FLAG_VALUE;
×
1685
  return TSDB_CODE_SUCCESS;
×
1686
}
1687

1688
static int32_t vnodeTSRowConvertToColValArray(SSubmitReqConvertCxt *pCxt) {
×
1689
  int32_t code = TSDB_CODE_SUCCESS;
×
1690
  tdSTSRowIterReset(&pCxt->rowIter, pCxt->pRow);
×
1691
  for (int32_t i = 0; TSDB_CODE_SUCCESS == code && i < pCxt->pTbSchema->numOfCols; ++i) {
×
1692
    STColumn *pCol = pCxt->pTbSchema->columns + i;
×
1693
    SCellVal  cellVal = {0};
×
1694
    if (!tdSTSRowIterFetch(&pCxt->rowIter, pCol->colId, pCol->type, &cellVal)) {
×
1695
      break;
×
1696
    }
1697
    code = vnodeCellValConvertToColVal(pCol, &cellVal, (SColVal *)taosArrayGet(pCxt->pColValues, i));
×
1698
  }
1699
  return code;
×
1700
}
1701

1702
static int32_t vnodeDecodeCreateTbReq(SSubmitReqConvertCxt *pCxt) {
×
1703
  if (pCxt->msgIter.schemaLen <= 0) {
×
1704
    return TSDB_CODE_SUCCESS;
×
1705
  }
1706

1707
  pCxt->pTbData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
×
1708
  if (NULL == pCxt->pTbData->pCreateTbReq) {
×
1709
    return terrno;
×
1710
  }
1711

1712
  SDecoder decoder = {0};
×
1713
  tDecoderInit(&decoder, (uint8_t *)pCxt->pBlock->data, pCxt->msgIter.schemaLen);
×
1714
  int32_t code = tDecodeSVCreateTbReq(&decoder, pCxt->pTbData->pCreateTbReq);
×
1715
  tDecoderClear(&decoder);
×
1716

1717
  return code;
×
1718
}
1719

1720
static int32_t vnodeSubmitReqConvertToSubmitReq2(SVnode *pVnode, SSubmitReq *pReq, SSubmitReq2 *pReq2) {
×
1721
  pReq2->aSubmitTbData = taosArrayInit(128, sizeof(SSubmitTbData));
×
1722
  if (NULL == pReq2->aSubmitTbData) {
×
1723
    return terrno;
×
1724
  }
1725

1726
  SSubmitReqConvertCxt cxt = {0};
×
1727

1728
  int32_t code = tInitSubmitMsgIter(pReq, &cxt.msgIter);
×
1729
  while (TSDB_CODE_SUCCESS == code) {
×
1730
    code = tGetSubmitMsgNext(&cxt.msgIter, &cxt.pBlock);
×
1731
    if (TSDB_CODE_SUCCESS == code) {
×
1732
      if (NULL == cxt.pBlock) {
×
1733
        break;
×
1734
      }
1735
      code = vnodeResetTableCxt(pVnode->pMeta, &cxt);
×
1736
    }
1737
    if (TSDB_CODE_SUCCESS == code) {
×
1738
      code = tInitSubmitBlkIter(&cxt.msgIter, cxt.pBlock, &cxt.blkIter);
×
1739
    }
1740
    if (TSDB_CODE_SUCCESS == code) {
×
1741
      code = vnodeDecodeCreateTbReq(&cxt);
×
1742
    }
1743
    while (TSDB_CODE_SUCCESS == code && (cxt.pRow = tGetSubmitBlkNext(&cxt.blkIter)) != NULL) {
×
1744
      code = vnodeTSRowConvertToColValArray(&cxt);
×
1745
      if (TSDB_CODE_SUCCESS == code) {
×
1746
        SRow **pNewRow = taosArrayReserve(cxt.pTbData->aRowP, 1);
×
1747
        code = tRowBuild(cxt.pColValues, cxt.pTbSchema, pNewRow);
×
1748
      }
1749
    }
1750
    if (TSDB_CODE_SUCCESS == code) {
×
1751
      code = (NULL == taosArrayPush(pReq2->aSubmitTbData, cxt.pTbData) ? terrno : TSDB_CODE_SUCCESS);
×
1752
    }
1753
    if (TSDB_CODE_SUCCESS == code) {
×
1754
      taosMemoryFreeClear(cxt.pTbData);
×
1755
    }
1756
  }
1757

1758
  vnodeDestroySubmitReqConvertCxt(&cxt);
×
1759
  return code;
×
1760
}
1761

1762
static int32_t vnodeRebuildSubmitReqMsg(SSubmitReq2 *pSubmitReq, void **ppMsg) {
×
1763
  int32_t  code = TSDB_CODE_SUCCESS;
×
1764
  char    *pMsg = NULL;
×
1765
  uint32_t msglen = 0;
×
1766
  tEncodeSize(tEncodeSubmitReq, pSubmitReq, msglen, code);
×
1767
  if (TSDB_CODE_SUCCESS == code) {
×
1768
    pMsg = taosMemoryMalloc(msglen);
×
1769
    if (NULL == pMsg) {
×
1770
      code = terrno;
×
1771
    }
1772
  }
1773
  if (TSDB_CODE_SUCCESS == code) {
×
1774
    SEncoder encoder;
1775
    tEncoderInit(&encoder, (uint8_t *)pMsg, msglen);
×
1776
    code = tEncodeSubmitReq(&encoder, pSubmitReq);
×
1777
    tEncoderClear(&encoder);
×
1778
  }
1779
  if (TSDB_CODE_SUCCESS == code) {
×
1780
    *ppMsg = pMsg;
×
1781
  }
1782
  return code;
×
1783
}
1784

1785
static int32_t vnodeProcessSubmitReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
5,330,598✔
1786
                                     SRpcMsg *pOriginalMsg) {
1787
  int32_t code = 0;
5,330,598✔
1788
  terrno = 0;
5,330,598✔
1789

1790
  SSubmitReq2 *pSubmitReq = &(SSubmitReq2){0};
5,330,680✔
1791
  SSubmitRsp2 *pSubmitRsp = &(SSubmitRsp2){0};
5,330,680✔
1792
  SArray      *newTbUids = NULL;
5,330,680✔
1793
  int32_t      ret;
1794
  SEncoder     ec = {0};
5,330,680✔
1795

1796
  pRsp->code = TSDB_CODE_SUCCESS;
5,330,680✔
1797

1798
  void           *pAllocMsg = NULL;
5,330,680✔
1799
  SSubmitReq2Msg *pMsg = (SSubmitReq2Msg *)pReq;
5,330,680✔
1800
  if (0 == pMsg->version) {
5,330,680!
1801
    code = vnodeSubmitReqConvertToSubmitReq2(pVnode, (SSubmitReq *)pMsg, pSubmitReq);
×
1802
    if (TSDB_CODE_SUCCESS == code) {
×
1803
      code = vnodeRebuildSubmitReqMsg(pSubmitReq, &pReq);
×
1804
    }
1805
    if (TSDB_CODE_SUCCESS == code) {
×
1806
      pAllocMsg = pReq;
×
1807
    }
1808
    if (TSDB_CODE_SUCCESS != code) {
×
1809
      goto _exit;
×
1810
    }
1811
  } else {
1812
    // decode
1813
    pReq = POINTER_SHIFT(pReq, sizeof(SSubmitReq2Msg));
5,330,680✔
1814
    len -= sizeof(SSubmitReq2Msg);
5,330,680✔
1815
    SDecoder dc = {0};
5,330,680✔
1816
    tDecoderInit(&dc, pReq, len);
5,330,680✔
1817
    if (tDecodeSubmitReq(&dc, pSubmitReq) < 0) {
5,330,591!
1818
      code = TSDB_CODE_INVALID_MSG;
×
1819
      goto _exit;
×
1820
    }
1821
    tDecoderClear(&dc);
5,330,638✔
1822
  }
1823

1824
  // scan
1825
  TSKEY now = taosGetTimestamp(pVnode->config.tsdbCfg.precision);
5,330,731✔
1826
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * pVnode->config.tsdbCfg.keep2;
5,330,612✔
1827
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
5,330,612✔
1828
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
11,354,002✔
1829
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
6,020,209✔
1830

1831
    if (pSubmitTbData->pCreateTbReq && pSubmitTbData->pCreateTbReq->uid == 0) {
6,020,227!
1832
      code = TSDB_CODE_INVALID_MSG;
×
1833
      goto _exit;
×
1834
    }
1835

1836
    if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
6,020,227✔
1837
      if (TARRAY_SIZE(pSubmitTbData->aCol) <= 0) {
149!
1838
        code = TSDB_CODE_INVALID_MSG;
×
1839
        goto _exit;
×
1840
      }
1841

1842
      SColData *colDataArr = TARRAY_DATA(pSubmitTbData->aCol);
149✔
1843
      SRowKey   lastKey;
1844
      tColDataArrGetRowKey(colDataArr, TARRAY_SIZE(pSubmitTbData->aCol), 0, &lastKey);
149✔
1845
      for (int32_t iRow = 1; iRow < colDataArr[0].nVal; iRow++) {
254✔
1846
        SRowKey key;
1847
        tColDataArrGetRowKey(TARRAY_DATA(pSubmitTbData->aCol), TARRAY_SIZE(pSubmitTbData->aCol), iRow, &key);
105✔
1848
        if (tRowKeyCompare(&lastKey, &key) >= 0) {
105!
1849
          code = TSDB_CODE_INVALID_MSG;
×
1850
          vError("vgId:%d %s failed 1 since %s, version:%" PRId64, TD_VID(pVnode), __func__, tstrerror(terrno), ver);
×
1851
          goto _exit;
×
1852
        }
1853
      }
1854
    } else {
1855
      int32_t nRow = TARRAY_SIZE(pSubmitTbData->aRowP);
6,020,078✔
1856
      SRow  **aRow = (SRow **)TARRAY_DATA(pSubmitTbData->aRowP);
6,020,078✔
1857
      SRowKey lastRowKey;
1858
      for (int32_t iRow = 0; iRow < nRow; ++iRow) {
341,628,489✔
1859
        if (aRow[iRow]->ts < minKey || aRow[iRow]->ts > maxKey) {
335,605,248✔
1860
          code = TSDB_CODE_INVALID_MSG;
44,550✔
1861
          vError("vgId:%d %s failed 2 since %s, version:%" PRId64, TD_VID(pVnode), __func__, tstrerror(code), ver);
44,550!
1862
          goto _exit;
×
1863
        }
1864
        if (iRow == 0) {
335,560,698✔
1865
          tRowGetKey(aRow[iRow], &lastRowKey);
6,019,999✔
1866
        } else {
1867
          SRowKey rowKey;
1868
          tRowGetKey(aRow[iRow], &rowKey);
329,540,699✔
1869

1870
          if (tRowKeyCompare(&lastRowKey, &rowKey) >= 0) {
329,538,217!
1871
            code = TSDB_CODE_INVALID_MSG;
×
1872
            vError("vgId:%d %s failed 3 since %s, version:%" PRId64, TD_VID(pVnode), __func__, tstrerror(code), ver);
×
1873
            goto _exit;
×
1874
          }
1875
          lastRowKey = rowKey;
329,587,303✔
1876
        }
1877
      }
1878
    }
1879
  }
1880

1881
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
11,357,753✔
1882
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
6,023,634✔
1883

1884
    if (pSubmitTbData->pCreateTbReq) {
6,023,379✔
1885
      pSubmitTbData->uid = pSubmitTbData->pCreateTbReq->uid;
66,920✔
1886
    } else {
1887
      SMetaInfo info = {0};
5,956,459✔
1888

1889
      code = metaGetInfo(pVnode->pMeta, pSubmitTbData->uid, &info, NULL);
5,956,459✔
1890
      if (code) {
5,956,656!
1891
        code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
1892
        vWarn("vgId:%d, table uid:%" PRId64 " not exists", TD_VID(pVnode), pSubmitTbData->uid);
×
1893
        goto _exit;
11✔
1894
      }
1895

1896
      if (info.suid != pSubmitTbData->suid) {
5,956,698!
1897
        code = TSDB_CODE_INVALID_MSG;
×
1898
        goto _exit;
×
1899
      }
1900

1901
      if (info.suid) {
5,956,698✔
1902
        if (metaGetInfo(pVnode->pMeta, info.suid, &info, NULL) != 0) {
5,022,017!
1903
          vWarn("vgId:%d, table uid:%" PRId64 " not exists", TD_VID(pVnode), info.suid);
×
1904
        }
1905
      }
1906

1907
      if (pSubmitTbData->sver != info.skmVer) {
5,957,040!
1908
        code = TSDB_CODE_TDB_INVALID_TABLE_SCHEMA_VER;
×
1909
        goto _exit;
×
1910
      }
1911
    }
1912

1913
    if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
6,023,960✔
1914
      int32_t   nColData = TARRAY_SIZE(pSubmitTbData->aCol);
149✔
1915
      SColData *aColData = (SColData *)TARRAY_DATA(pSubmitTbData->aCol);
149✔
1916

1917
      if (nColData <= 0) {
149!
1918
        code = TSDB_CODE_INVALID_MSG;
×
1919
        goto _exit;
×
1920
      }
1921

1922
      if (aColData[0].cid != PRIMARYKEY_TIMESTAMP_COL_ID || aColData[0].type != TSDB_DATA_TYPE_TIMESTAMP ||
149!
1923
          aColData[0].nVal <= 0) {
149!
1924
        code = TSDB_CODE_INVALID_MSG;
×
1925
        goto _exit;
×
1926
      }
1927

1928
      for (int32_t j = 1; j < nColData; j++) {
667✔
1929
        if (aColData[j].nVal != aColData[0].nVal) {
518!
1930
          code = TSDB_CODE_INVALID_MSG;
×
1931
          goto _exit;
×
1932
        }
1933
      }
1934
    }
1935
  }
1936

1937
  vDebug("vgId:%d, submit block size %d", TD_VID(pVnode), (int32_t)taosArrayGetSize(pSubmitReq->aSubmitTbData));
5,334,119✔
1938

1939
  // loop to handle
1940
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
11,351,201✔
1941
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
6,020,507✔
1942

1943
    // create table
1944
    if (pSubmitTbData->pCreateTbReq) {
6,020,433✔
1945
      // alloc if need
1946
      if (pSubmitRsp->aCreateTbRsp == NULL &&
66,917✔
1947
          (pSubmitRsp->aCreateTbRsp = taosArrayInit(TARRAY_SIZE(pSubmitReq->aSubmitTbData), sizeof(SVCreateTbRsp))) ==
54,878!
1948
              NULL) {
1949
        code = terrno;
×
1950
        goto _exit;
8✔
1951
      }
1952

1953
      SVCreateTbRsp *pCreateTbRsp = taosArrayReserve(pSubmitRsp->aCreateTbRsp, 1);
66,917✔
1954

1955
      // create table
1956
      if (metaCreateTable(pVnode->pMeta, ver, pSubmitTbData->pCreateTbReq, &pCreateTbRsp->pMeta) == 0) {
66,917✔
1957
        // create table success
1958

1959
        if (newTbUids == NULL &&
81,807!
1960
            (newTbUids = taosArrayInit(TARRAY_SIZE(pSubmitReq->aSubmitTbData), sizeof(int64_t))) == NULL) {
34,915✔
1961
          code = terrno;
×
1962
          goto _exit;
×
1963
        }
1964

1965
        if (taosArrayPush(newTbUids, &pSubmitTbData->uid) == NULL) {
93,784!
1966
          code = terrno;
×
1967
          goto _exit;
×
1968
        }
1969

1970
        if (pCreateTbRsp->pMeta) {
46,892!
1971
          vnodeUpdateMetaRsp(pVnode, pCreateTbRsp->pMeta);
46,892✔
1972
        }
1973
      } else {  // create table failed
1974
        if (terrno != TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
20,023✔
1975
          code = terrno;
8✔
1976
          vError("vgId:%d failed to create table:%s, code:%s", TD_VID(pVnode), pSubmitTbData->pCreateTbReq->name,
8!
1977
                 tstrerror(terrno));
1978
          goto _exit;
8✔
1979
        }
1980
        terrno = 0;
20,015✔
1981
        pSubmitTbData->uid = pSubmitTbData->pCreateTbReq->uid;  // update uid if table exist for using below
20,015✔
1982
      }
1983
    }
1984

1985
    // insert data
1986
    int32_t affectedRows;
1987
    code = tsdbInsertTableData(pVnode->pTsdb, ver, pSubmitTbData, &affectedRows);
6,020,423✔
1988
    if (code) goto _exit;
6,021,059!
1989

1990
    code = metaUpdateChangeTimeWithLock(pVnode->pMeta, pSubmitTbData->uid, pSubmitTbData->ctimeMs);
6,021,059✔
1991
    if (code) goto _exit;
6,020,448!
1992

1993
    pSubmitRsp->affectedRows += affectedRows;
6,020,448✔
1994
  }
1995

1996
  // update the affected table uid list
1997
  if (taosArrayGetSize(newTbUids) > 0) {
5,330,694✔
1998
    vDebug("vgId:%d, add %d table into query table list in handling submit", TD_VID(pVnode),
34,915✔
1999
           (int32_t)taosArrayGetSize(newTbUids));
2000
    if (tqUpdateTbUidList(pVnode->pTq, newTbUids, true) != 0) {
34,915!
2001
      vError("vgId:%d, failed to update tbUid list", TD_VID(pVnode));
×
2002
    }
2003
  }
2004

2005
_exit:
5,330,714✔
2006
  // message
2007
  pRsp->code = code;
5,330,733✔
2008
  tEncodeSize(tEncodeSSubmitRsp2, pSubmitRsp, pRsp->contLen, ret);
5,330,733!
2009
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
5,330,645✔
2010
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
5,330,774✔
2011
  if (tEncodeSSubmitRsp2(&ec, pSubmitRsp) < 0) {
5,330,767!
2012
    vError("vgId:%d, failed to encode submit response", TD_VID(pVnode));
×
2013
  }
2014
  tEncoderClear(&ec);
5,330,690✔
2015

2016
  // update statistics
2017
  (void)atomic_add_fetch_64(&pVnode->statis.nInsert, pSubmitRsp->affectedRows);
5,330,760✔
2018
  (void)atomic_add_fetch_64(&pVnode->statis.nInsertSuccess, pSubmitRsp->affectedRows);
5,330,819✔
2019
  (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsert, 1);
5,330,825✔
2020

2021
  if (tsEnableMonitor && tsMonitorFqdn[0] != 0 && tsMonitorPort != 0 && pSubmitRsp->affectedRows > 0 &&
5,330,830!
2022
      strlen(pOriginalMsg->info.conn.user) > 0 && tsInsertCounter != NULL) {
×
2023
    const char *sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT_AFFECTED_ROWS,
×
2024
                                   pVnode->monitor.strClusterId,
×
2025
                                   pVnode->monitor.strDnodeId,
×
2026
                                   tsLocalEp,
2027
                                   pVnode->monitor.strVgId,
×
2028
                                   pOriginalMsg->info.conn.user,
×
2029
                                   "Success"};
2030
    int         tv = taos_counter_add(tsInsertCounter, pSubmitRsp->affectedRows, sample_labels);
×
2031
  }
2032

2033
  if (code == 0) {
5,330,830✔
2034
    (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
5,330,801✔
2035
    code = tdProcessRSmaSubmit(pVnode->pSma, ver, pSubmitReq, pReq, len);
5,330,811✔
2036
  }
2037
  /*
2038
  if (code == 0) {
2039
    atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
2040
    code = tdProcessRSmaSubmit(pVnode->pSma, ver, pSubmitReq, pReq, len);
2041

2042
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT, pVnode->monitor.strClusterId,
2043
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2044
                                          pOriginalMsg->info.conn.user, "Success"};
2045
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2046
  }
2047
  else{
2048
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT, pVnode->monitor.strClusterId,
2049
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2050
                                        pOriginalMsg->info.conn.user, "Failed"};
2051
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2052
  }
2053
  */
2054

2055
  // clear
2056
  taosArrayDestroy(newTbUids);
5,330,777✔
2057
  tDestroySubmitReq(pSubmitReq, 0 == pMsg->version ? TSDB_MSG_FLG_CMPT : TSDB_MSG_FLG_DECODE);
5,330,731!
2058
  tDestroySSubmitRsp2(pSubmitRsp, TSDB_MSG_FLG_ENCODE);
5,330,767✔
2059

2060
  if (code) terrno = code;
5,330,696✔
2061

2062
  taosMemoryFree(pAllocMsg);
5,330,696✔
2063

2064
  return code;
5,330,681✔
2065
}
2066

2067
static int32_t vnodeProcessCreateTSmaReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
33✔
2068
  SVCreateTSmaReq req = {0};
33✔
2069
  SDecoder        coder = {0};
33✔
2070

2071
  if (pRsp) {
33!
2072
    pRsp->msgType = TDMT_VND_CREATE_SMA_RSP;
33✔
2073
    pRsp->code = TSDB_CODE_SUCCESS;
33✔
2074
    pRsp->pCont = NULL;
33✔
2075
    pRsp->contLen = 0;
33✔
2076
  }
2077

2078
  // decode and process req
2079
  tDecoderInit(&coder, pReq, len);
33✔
2080

2081
  if (tDecodeSVCreateTSmaReq(&coder, &req) < 0) {
33!
2082
    terrno = TSDB_CODE_MSG_DECODE_ERROR;
×
2083
    if (pRsp) pRsp->code = terrno;
×
2084
    goto _err;
×
2085
  }
2086

2087
  if (tdProcessTSmaCreate(pVnode->pSma, ver, (const char *)&req) < 0) {
33!
2088
    if (pRsp) pRsp->code = terrno;
×
2089
    goto _err;
×
2090
  }
2091

2092
  tDecoderClear(&coder);
33✔
2093
  vDebug("vgId:%d, success to create tsma %s:%" PRIi64 " version %" PRIi64 " for table %" PRIi64, TD_VID(pVnode),
33✔
2094
         req.indexName, req.indexUid, ver, req.tableUid);
2095
  return 0;
33✔
2096

2097
_err:
×
2098
  tDecoderClear(&coder);
×
2099
  vError("vgId:%d, failed to create tsma %s:%" PRIi64 " version %" PRIi64 "for table %" PRIi64 " since %s",
×
2100
         TD_VID(pVnode), req.indexName, req.indexUid, ver, req.tableUid, terrstr());
2101
  return terrno;
×
2102
}
2103

2104
/**
2105
 * @brief specific for smaDstVnode
2106
 *
2107
 * @param pVnode
2108
 * @param pCont
2109
 * @param contLen
2110
 * @return int32_t
2111
 */
2112
int32_t vnodeProcessCreateTSma(SVnode *pVnode, void *pCont, uint32_t contLen) {
×
2113
  return vnodeProcessCreateTSmaReq(pVnode, 1, pCont, contLen, NULL);
×
2114
}
2115

2116
static int32_t vnodeConsolidateAlterHashRange(SVnode *pVnode, int64_t ver) {
76✔
2117
  int32_t code = TSDB_CODE_SUCCESS;
76✔
2118

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

2122
  // TODO: trim meta of tables from TDB per hash range [pVnode->config.hashBegin, pVnode->config.hashEnd]
2123
  code = metaTrimTables(pVnode->pMeta);
76✔
2124

2125
  return code;
76✔
2126
}
2127

2128
static int32_t vnodeProcessAlterConfirmReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
8,146✔
2129
  vInfo("vgId:%d, vnode handle msgType:alter-confirm, alter confirm msg is processed", TD_VID(pVnode));
8,146!
2130
  int32_t code = TSDB_CODE_SUCCESS;
8,146✔
2131
  if (!pVnode->config.hashChange) {
8,146✔
2132
    goto _exit;
8,070✔
2133
  }
2134

2135
  code = vnodeConsolidateAlterHashRange(pVnode, ver);
76✔
2136
  if (code < 0) {
76!
2137
    vError("vgId:%d, failed to consolidate alter hashrange since %s. version:%" PRId64, TD_VID(pVnode), terrstr(), ver);
×
2138
    goto _exit;
×
2139
  }
2140
  pVnode->config.hashChange = false;
76✔
2141

2142
_exit:
8,146✔
2143
  pRsp->msgType = TDMT_VND_ALTER_CONFIRM_RSP;
8,146✔
2144
  pRsp->code = code;
8,146✔
2145
  pRsp->pCont = NULL;
8,146✔
2146
  pRsp->contLen = 0;
8,146✔
2147

2148
  return code;
8,146✔
2149
}
2150

2151
extern int32_t tsdbDisableAndCancelAllBgTask(STsdb *pTsdb);
2152
extern void    tsdbEnableBgTask(STsdb *pTsdb);
2153

2154
static int32_t vnodeProcessAlterConfigReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
866✔
2155
  bool walChanged = false;
866✔
2156
  bool tsdbChanged = false;
866✔
2157

2158
  SAlterVnodeConfigReq req = {0};
866✔
2159
  if (tDeserializeSAlterVnodeConfigReq(pReq, len, &req) != 0) {
866!
2160
    terrno = TSDB_CODE_INVALID_MSG;
×
2161
    return TSDB_CODE_INVALID_MSG;
×
2162
  }
2163

2164
  vInfo("vgId:%d, start to alter vnode config, page:%d pageSize:%d buffer:%d szPage:%d szBuf:%" PRIu64
873✔
2165
        " cacheLast:%d cacheLastSize:%d days:%d keep0:%d keep1:%d keep2:%d keepTimeOffset:%d s3KeepLocal:%d "
2166
        "s3Compact:%d fsync:%d level:%d "
2167
        "walRetentionPeriod:%d walRetentionSize:%d",
2168
        TD_VID(pVnode), req.pages, req.pageSize, req.buffer, req.pageSize * 1024, (uint64_t)req.buffer * 1024 * 1024,
2169
        req.cacheLast, req.cacheLastSize, req.daysPerFile, req.daysToKeep0, req.daysToKeep1, req.daysToKeep2,
2170
        req.keepTimeOffset, req.s3KeepLocal, req.s3Compact, req.walFsyncPeriod, req.walLevel, req.walRetentionPeriod,
2171
        req.walRetentionSize);
2172

2173
  if (pVnode->config.cacheLastSize != req.cacheLastSize) {
878✔
2174
    pVnode->config.cacheLastSize = req.cacheLastSize;
370✔
2175
    tsdbCacheSetCapacity(pVnode, (size_t)pVnode->config.cacheLastSize * 1024 * 1024);
370✔
2176
  }
2177

2178
  if (pVnode->config.szBuf != req.buffer * 1024LL * 1024LL) {
878✔
2179
    vInfo("vgId:%d, vnode buffer is changed from %" PRId64 " to %" PRId64, TD_VID(pVnode), pVnode->config.szBuf,
10!
2180
          (uint64_t)(req.buffer * 1024LL * 1024LL));
2181
    pVnode->config.szBuf = req.buffer * 1024LL * 1024LL;
10✔
2182
  }
2183

2184
  if (pVnode->config.szCache != req.pages) {
878✔
2185
    if (metaAlterCache(pVnode->pMeta, req.pages) < 0) {
6!
2186
      vError("vgId:%d, failed to change vnode pages from %d to %d failed since %s", TD_VID(pVnode),
×
2187
             pVnode->config.szCache, req.pages, tstrerror(errno));
2188
      return errno;
×
2189
    } else {
2190
      vInfo("vgId:%d, vnode pages is changed from %d to %d", TD_VID(pVnode), pVnode->config.szCache, req.pages);
6!
2191
      pVnode->config.szCache = req.pages;
6✔
2192
    }
2193
  }
2194

2195
  if (pVnode->config.cacheLast != req.cacheLast) {
878✔
2196
    pVnode->config.cacheLast = req.cacheLast;
550✔
2197
  }
2198

2199
  if (pVnode->config.walCfg.fsyncPeriod != req.walFsyncPeriod) {
878✔
2200
    pVnode->config.walCfg.fsyncPeriod = req.walFsyncPeriod;
70✔
2201
    walChanged = true;
70✔
2202
  }
2203

2204
  if (pVnode->config.walCfg.level != req.walLevel) {
878✔
2205
    if (pVnode->config.walCfg.level == 0) {
48!
2206
      pVnode->config.walCfg.clearFiles = 1;
×
2207
    }
2208
    pVnode->config.walCfg.level = req.walLevel;
48✔
2209
    walChanged = true;
48✔
2210
  }
2211

2212
  if (pVnode->config.walCfg.retentionPeriod != req.walRetentionPeriod) {
878✔
2213
    pVnode->config.walCfg.retentionPeriod = req.walRetentionPeriod;
132✔
2214
    walChanged = true;
132✔
2215
  }
2216

2217
  if (pVnode->config.walCfg.retentionSize != req.walRetentionSize) {
878!
2218
    pVnode->config.walCfg.retentionSize = req.walRetentionSize;
×
2219
    walChanged = true;
×
2220
  }
2221

2222
  if (pVnode->config.tsdbCfg.keep0 != req.daysToKeep0) {
878✔
2223
    pVnode->config.tsdbCfg.keep0 = req.daysToKeep0;
56✔
2224
    if (!VND_IS_RSMA(pVnode)) {
56!
2225
      tsdbChanged = true;
56✔
2226
    }
2227
  }
2228

2229
  if (pVnode->config.tsdbCfg.keep1 != req.daysToKeep1) {
878✔
2230
    pVnode->config.tsdbCfg.keep1 = req.daysToKeep1;
56✔
2231
    if (!VND_IS_RSMA(pVnode)) {
56!
2232
      tsdbChanged = true;
56✔
2233
    }
2234
  }
2235

2236
  if (pVnode->config.tsdbCfg.keep2 != req.daysToKeep2) {
878✔
2237
    pVnode->config.tsdbCfg.keep2 = req.daysToKeep2;
56✔
2238
    if (!VND_IS_RSMA(pVnode)) {
56!
2239
      tsdbChanged = true;
56✔
2240
    }
2241
  }
2242

2243
  if (pVnode->config.tsdbCfg.keepTimeOffset != req.keepTimeOffset) {
878✔
2244
    pVnode->config.tsdbCfg.keepTimeOffset = req.keepTimeOffset;
2✔
2245
    if (!VND_IS_RSMA(pVnode)) {
2!
2246
      tsdbChanged = true;
2✔
2247
    }
2248
  }
2249

2250
  if (req.sttTrigger != -1 && req.sttTrigger != pVnode->config.sttTrigger) {
878!
2251
    if (req.sttTrigger > 1 && pVnode->config.sttTrigger > 1) {
×
2252
      pVnode->config.sttTrigger = req.sttTrigger;
×
2253
    } else {
2254
      vnodeAWait(&pVnode->commitTask);
×
2255

2256
      int32_t ret = tsdbDisableAndCancelAllBgTask(pVnode->pTsdb);
×
2257
      if (ret != 0) {
×
2258
        vError("vgId:%d, failed to disable bg task since %s", TD_VID(pVnode), tstrerror(errno));
×
2259
      }
2260

2261
      pVnode->config.sttTrigger = req.sttTrigger;
×
2262
      tsdbEnableBgTask(pVnode->pTsdb);
×
2263
    }
2264
  }
2265

2266
  if (req.minRows != -1 && req.minRows != pVnode->config.tsdbCfg.minRows) {
878!
2267
    pVnode->config.tsdbCfg.minRows = req.minRows;
×
2268
  }
2269

2270
  if (req.s3KeepLocal != -1 && req.s3KeepLocal != pVnode->config.s3KeepLocal) {
878!
2271
    pVnode->config.s3KeepLocal = req.s3KeepLocal;
×
2272
  }
2273
  if (req.s3Compact != -1 && req.s3Compact != pVnode->config.s3Compact) {
878!
2274
    pVnode->config.s3Compact = req.s3Compact;
×
2275
  }
2276

2277
  if (walChanged) {
878✔
2278
    if (walAlter(pVnode->pWal, &pVnode->config.walCfg) != 0) {
246!
2279
      vError("vgId:%d, failed to alter wal config since %s", TD_VID(pVnode), tstrerror(errno));
×
2280
    }
2281
  }
2282

2283
  if (tsdbChanged) {
874✔
2284
    tsdbSetKeepCfg(pVnode->pTsdb, &pVnode->config.tsdbCfg);
60✔
2285
  }
2286

2287
  return 0;
874✔
2288
}
2289

2290
static int32_t vnodeProcessBatchDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
3,261✔
2291
  SBatchDeleteReq deleteReq;
2292
  SDecoder        decoder;
2293
  tDecoderInit(&decoder, pReq, len);
3,261✔
2294
  if (tDecodeSBatchDeleteReq(&decoder, &deleteReq) < 0) {
3,259!
2295
    tDecoderClear(&decoder);
×
2296
    return terrno = TSDB_CODE_INVALID_MSG;
×
2297
  }
2298

2299
  SMetaReader mr = {0};
3,261✔
2300
  metaReaderDoInit(&mr, pVnode->pMeta, META_READER_NOLOCK);
3,261✔
2301
  STsdb *pTsdb = pVnode->pTsdb;
3,259✔
2302

2303
  if (deleteReq.level) {
3,259✔
2304
    pTsdb = deleteReq.level == 1 ? VND_RSMA1(pVnode) : VND_RSMA2(pVnode);
16✔
2305
  }
2306

2307
  int32_t sz = taosArrayGetSize(deleteReq.deleteReqs);
3,259✔
2308
  for (int32_t i = 0; i < sz; i++) {
7,384✔
2309
    SSingleDeleteReq *pOneReq = taosArrayGet(deleteReq.deleteReqs, i);
4,118✔
2310
    char             *name = pOneReq->tbname;
4,116✔
2311
    if (metaGetTableEntryByName(&mr, name) < 0) {
4,116✔
2312
      vDebug("vgId:%d, stream delete msg, skip since no table: %s", pVnode->config.vgId, name);
2,148✔
2313
      continue;
2,148✔
2314
    }
2315

2316
    int64_t uid = mr.me.uid;
1,977✔
2317

2318
    int32_t code = tsdbDeleteTableData(pTsdb, ver, deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
1,977✔
2319
    if (code < 0) {
1,977!
2320
      terrno = code;
×
2321
      vError("vgId:%d, delete error since %s, suid:%" PRId64 ", uid:%" PRId64 ", start ts:%" PRId64 ", end ts:%" PRId64,
×
2322
             TD_VID(pVnode), terrstr(), deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
2323
    }
2324

2325
    if (deleteReq.level == 0) {
1,977✔
2326
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, deleteReq.ctimeMs);
1,361✔
2327
      if (code < 0) {
1,361!
2328
        terrno = code;
×
2329
        vError("vgId:%d, update change time error since %s, suid:%" PRId64 ", uid:%" PRId64 ", start ts:%" PRId64
×
2330
               ", end ts:%" PRId64,
2331
               TD_VID(pVnode), terrstr(), deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
2332
      }
2333
    }
2334
    tDecoderClear(&mr.coder);
1,977✔
2335
  }
2336
  metaReaderClear(&mr);
3,266✔
2337
  taosArrayDestroy(deleteReq.deleteReqs);
3,266✔
2338
  return 0;
3,266✔
2339
}
2340

2341
static int32_t vnodeProcessDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
60,988✔
2342
                                     SRpcMsg *pOriginalMsg) {
2343
  int32_t     code = 0;
60,988✔
2344
  SDecoder   *pCoder = &(SDecoder){0};
60,988✔
2345
  SDeleteRes *pRes = &(SDeleteRes){0};
60,988✔
2346

2347
  pRsp->msgType = TDMT_VND_DELETE_RSP;
60,988✔
2348
  pRsp->pCont = NULL;
60,988✔
2349
  pRsp->contLen = 0;
60,988✔
2350
  pRsp->code = TSDB_CODE_SUCCESS;
60,988✔
2351

2352
  pRes->uidList = taosArrayInit(0, sizeof(tb_uid_t));
60,988✔
2353
  if (pRes->uidList == NULL) {
60,989!
2354
    code = terrno;
×
2355
    goto _err;
×
2356
  }
2357

2358
  tDecoderInit(pCoder, pReq, len);
60,989✔
2359
  code = tDecodeDeleteRes(pCoder, pRes);
60,989✔
2360
  if (code) goto _err;
60,990!
2361

2362
  if (pRes->affectedRows > 0) {
60,990✔
2363
    for (int32_t iUid = 0; iUid < taosArrayGetSize(pRes->uidList); iUid++) {
114,321✔
2364
      uint64_t uid = *(uint64_t *)taosArrayGet(pRes->uidList, iUid);
58,012✔
2365
      code = tsdbDeleteTableData(pVnode->pTsdb, ver, pRes->suid, uid, pRes->skey, pRes->ekey);
58,012✔
2366
      if (code) goto _err;
58,013!
2367
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, pRes->ctimeMs);
58,013✔
2368
      if (code) goto _err;
58,012!
2369
    }
2370
  }
2371

2372
  code = tdProcessRSmaDelete(pVnode->pSma, ver, pRes, pReq, len);
60,987✔
2373

2374
  tDecoderClear(pCoder);
60,991✔
2375
  taosArrayDestroy(pRes->uidList);
60,990✔
2376

2377
  SVDeleteRsp rsp = {.affectedRows = pRes->affectedRows};
60,990✔
2378
  int32_t     ret = 0;
60,990✔
2379
  tEncodeSize(tEncodeSVDeleteRsp, &rsp, pRsp->contLen, ret);
60,990!
2380
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
60,988✔
2381
  SEncoder ec = {0};
60,991✔
2382
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
60,991✔
2383
  code = tEncodeSVDeleteRsp(&ec, &rsp);
60,992✔
2384
  if (code) goto _err;
60,986!
2385
  tEncoderClear(&ec);
60,986✔
2386
  return code;
60,991✔
2387

2388
_err:
×
2389
  /*
2390
  if(code == TSDB_CODE_SUCCESS){
2391
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_DELETE, pVnode->monitor.strClusterId,
2392
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2393
                                        pOriginalMsg->info.conn.user, "Success"};
2394
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2395
  }
2396
  else{
2397
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_DELETE, pVnode->monitor.strClusterId,
2398
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2399
                                        pOriginalMsg->info.conn.user, "Failed"};
2400
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2401
  }
2402
  */
2403

2404
  return code;
×
2405
}
2406
static int32_t vnodeProcessCreateIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
991✔
2407
  SVCreateStbReq req = {0};
991✔
2408
  SDecoder       dc = {0};
991✔
2409
  int32_t        code = 0;
991✔
2410

2411
  pRsp->msgType = TDMT_VND_CREATE_INDEX_RSP;
991✔
2412
  pRsp->code = TSDB_CODE_SUCCESS;
991✔
2413
  pRsp->pCont = NULL;
991✔
2414
  pRsp->contLen = 0;
991✔
2415

2416
  tDecoderInit(&dc, pReq, len);
991✔
2417
  // decode req
2418
  if (tDecodeSVCreateStbReq(&dc, &req) < 0) {
991!
2419
    tDecoderClear(&dc);
×
2420
    return terrno = TSDB_CODE_INVALID_MSG;
×
2421
  }
2422

2423
  code = metaAddIndexToSTable(pVnode->pMeta, ver, &req);
991✔
2424
  if (code) {
991!
2425
    pRsp->code = code;
×
2426
    goto _err;
×
2427
  }
2428
  tDecoderClear(&dc);
991✔
2429
  return 0;
991✔
2430

2431
_err:
×
2432
  tDecoderClear(&dc);
×
2433
  return code;
×
2434
}
2435
static int32_t vnodeProcessDropIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
2,158✔
2436
  SDropIndexReq req = {0};
2,158✔
2437
  int32_t       code = 0;
2,158✔
2438
  pRsp->msgType = TDMT_VND_DROP_INDEX_RSP;
2,158✔
2439
  pRsp->code = TSDB_CODE_SUCCESS;
2,158✔
2440
  pRsp->pCont = NULL;
2,158✔
2441
  pRsp->contLen = 0;
2,158✔
2442

2443
  if ((code = tDeserializeSDropIdxReq(pReq, len, &req))) {
2,158!
2444
    pRsp->code = code;
×
2445
    return code;
×
2446
  }
2447

2448
  code = metaDropIndexFromSTable(pVnode->pMeta, ver, &req);
2,158✔
2449
  if (code) {
2,158!
2450
    pRsp->code = code;
×
2451
    return code;
×
2452
  }
2453
  return TSDB_CODE_SUCCESS;
2,158✔
2454
}
2455

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

2458
static int32_t vnodeProcessCompactVnodeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
53✔
2459
  if (!pVnode->restored) {
53✔
2460
    vInfo("vgId:%d, ignore compact req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
1!
2461
    return 0;
1✔
2462
  }
2463
  return vnodeAsyncCompact(pVnode, ver, pReq, len, pRsp);
52✔
2464
}
2465

2466
static int32_t vnodeProcessConfigChangeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
2467
  if (syncCheckMember(pVnode->sync) != 0) {
×
2468
    vError("vgId:%d, failed to check member", TD_VID(pVnode));
×
2469
  }
2470

2471
  pRsp->msgType = TDMT_SYNC_CONFIG_CHANGE_RSP;
×
2472
  pRsp->code = TSDB_CODE_SUCCESS;
×
2473
  pRsp->pCont = NULL;
×
2474
  pRsp->contLen = 0;
×
2475

2476
  return 0;
×
2477
}
2478

2479
static int32_t vnodePreCheckAssignedLogSyncd(SVnode *pVnode, char *member0Token, char *member1Token) {
3✔
2480
  SSyncState syncState = syncGetState(pVnode->sync);
3✔
2481
  if (syncState.state != TAOS_SYNC_STATE_LEADER) {
3✔
2482
    return terrno = TSDB_CODE_SYN_NOT_LEADER;
1✔
2483
  }
2484

2485
  char token[TSDB_ARB_TOKEN_SIZE] = {0};
2✔
2486
  if (vnodeGetArbToken(pVnode, token) != 0) {
2!
2487
    return terrno = TSDB_CODE_NOT_FOUND;
×
2488
  }
2489

2490
  if (strncmp(token, member0Token, TSDB_ARB_TOKEN_SIZE) != 0 &&
2!
2491
      strncmp(token, member1Token, TSDB_ARB_TOKEN_SIZE) != 0) {
2!
2492
    return terrno = TSDB_CODE_MND_ARB_TOKEN_MISMATCH;
2✔
2493
  }
2494

2495
  terrno = TSDB_CODE_SUCCESS;
×
2496
  return 0;
×
2497
}
2498

2499
static int32_t vnodeCheckAssignedLogSyncd(SVnode *pVnode, char *member0Token, char *member1Token) {
×
2500
  int32_t code = vnodePreCheckAssignedLogSyncd(pVnode, member0Token, member1Token);
×
2501
  if (code != 0) {
×
2502
    return code;
×
2503
  }
2504

2505
  return syncGetAssignedLogSynced(pVnode->sync);
×
2506
}
2507

2508
static int32_t vnodeProcessArbCheckSyncReq(SVnode *pVnode, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
2509
  int32_t code = 0;
×
2510

2511
  SVArbCheckSyncReq syncReq = {0};
×
2512

2513
  code = tDeserializeSVArbCheckSyncReq(pReq, len, &syncReq);
×
2514
  if (code) {
×
2515
    return terrno = code;
×
2516
  }
2517

2518
  pRsp->msgType = TDMT_VND_ARB_CHECK_SYNC_RSP;
×
2519
  pRsp->code = TSDB_CODE_SUCCESS;
×
2520
  pRsp->pCont = NULL;
×
2521
  pRsp->contLen = 0;
×
2522

2523
  SVArbCheckSyncRsp syncRsp = {0};
×
2524
  syncRsp.arbToken = syncReq.arbToken;
×
2525
  syncRsp.member0Token = syncReq.member0Token;
×
2526
  syncRsp.member1Token = syncReq.member1Token;
×
2527
  syncRsp.vgId = TD_VID(pVnode);
×
2528

2529
  if (vnodeCheckAssignedLogSyncd(pVnode, syncReq.member0Token, syncReq.member1Token) != 0) {
×
2530
    vError("vgId:%d, failed to check assigned log syncd", TD_VID(pVnode));
×
2531
  }
2532
  syncRsp.errCode = terrno;
×
2533

2534
  if (vnodeUpdateArbTerm(pVnode, syncReq.arbTerm) != 0) {
×
2535
    vError("vgId:%d, failed to update arb term", TD_VID(pVnode));
×
2536
    code = -1;
×
2537
    goto _OVER;
×
2538
  }
2539

2540
  int32_t contLen = tSerializeSVArbCheckSyncRsp(NULL, 0, &syncRsp);
×
2541
  if (contLen <= 0) {
×
2542
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2543
    code = -1;
×
2544
    goto _OVER;
×
2545
  }
2546
  void *pHead = rpcMallocCont(contLen);
×
2547
  if (!pHead) {
×
2548
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2549
    code = -1;
×
2550
    goto _OVER;
×
2551
  }
2552

2553
  if (tSerializeSVArbCheckSyncRsp(pHead, contLen, &syncRsp) <= 0) {
×
2554
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2555
    rpcFreeCont(pHead);
×
2556
    code = -1;
×
2557
    goto _OVER;
×
2558
  }
2559

2560
  pRsp->pCont = pHead;
×
2561
  pRsp->contLen = contLen;
×
2562

2563
  terrno = TSDB_CODE_SUCCESS;
×
2564

2565
_OVER:
×
2566
  tFreeSVArbCheckSyncReq(&syncReq);
×
2567
  return code;
×
2568
}
2569

2570
#ifndef TD_ENTERPRISE
2571
int32_t vnodeAsyncCompact(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) { return 0; }
2572
int32_t tsdbAsyncCompact(STsdb *tsdb, const STimeWindow *tw, bool sync) { return 0; }
2573
#endif
2574

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