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

taosdata / TDengine / #3522

07 Nov 2024 05:59AM UTC coverage: 58.216% (+1.3%) from 56.943%
#3522

push

travis-ci

web-flow
Merge pull request #28663 from taosdata/fix/3_liaohj

fix(stream): stop the underlying scan operations for stream

111884 of 248391 branches covered (45.04%)

Branch coverage included in aggregate %.

3 of 4 new or added lines in 1 file covered. (75.0%)

1164 existing lines in 134 files now uncovered.

191720 of 273118 relevant lines covered (70.2%)

13088725.13 hits per line

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

54.55
/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

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

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

61
static int32_t vnodePreprocessCreateTableReq(SVnode *pVnode, SDecoder *pCoder, int64_t btime, int64_t *pUid) {
85,543✔
62
  int32_t code = 0;
85,543✔
63
  int32_t lino = 0;
85,543✔
64

65
  if (tStartDecode(pCoder) < 0) {
85,543!
66
    code = TSDB_CODE_INVALID_MSG;
×
67
    TSDB_CHECK_CODE(code, lino, _exit);
×
68
  }
69

70
  // flags
71
  if (tDecodeI32v(pCoder, NULL) < 0) {
85,545!
72
    code = TSDB_CODE_INVALID_MSG;
×
73
    TSDB_CHECK_CODE(code, lino, _exit);
×
74
  }
75

76
  // name
77
  char *name = NULL;
85,545✔
78
  if (tDecodeCStr(pCoder, &name) < 0) {
85,540!
79
    code = TSDB_CODE_INVALID_MSG;
×
80
    TSDB_CHECK_CODE(code, lino, _exit);
×
81
  }
82

83
  // uid
84
  int64_t uid = metaGetTableEntryUidByName(pVnode->pMeta, name);
85,540✔
85
  if (uid == 0) {
85,542✔
86
    uid = tGenIdPI64();
69,714✔
87
  }
88
  *(int64_t *)(pCoder->data + pCoder->pos) = uid;
85,546✔
89

90
  // btime
91
  *(int64_t *)(pCoder->data + pCoder->pos + 8) = btime;
85,546✔
92

93
  tEndDecode(pCoder);
85,546✔
94

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

108
  int64_t  btime = taosGetTimestampMs();
24,074✔
109
  SDecoder dc = {0};
24,074✔
110
  int32_t  nReqs;
111

112
  tDecoderInit(&dc, (uint8_t *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead));
24,074✔
113
  if (tStartDecode(&dc) < 0) {
24,072!
114
    code = TSDB_CODE_INVALID_MSG;
×
115
    return code;
×
116
  }
117

118
  if (tDecodeI32v(&dc, &nReqs) < 0) {
24,075!
119
    code = TSDB_CODE_INVALID_MSG;
×
120
    TSDB_CHECK_CODE(code, lino, _exit);
×
121
  }
122
  for (int32_t iReq = 0; iReq < nReqs; iReq++) {
63,014✔
123
    code = vnodePreprocessCreateTableReq(pVnode, &dc, btime, NULL);
38,941✔
124
    TSDB_CHECK_CODE(code, lino, _exit);
38,939!
125
  }
126

127
  tEndDecode(&dc);
24,073✔
128

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

138
static int32_t vnodePreProcessAlterTableMsg(SVnode *pVnode, SRpcMsg *pMsg) {
671✔
139
  int32_t code = TSDB_CODE_INVALID_MSG;
671✔
140
  int32_t lino = 0;
671✔
141

142
  SDecoder dc = {0};
671✔
143
  tDecoderInit(&dc, (uint8_t *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead));
671✔
144

145
  SVAlterTbReq vAlterTbReq = {0};
671✔
146
  int64_t      ctimeMs = taosGetTimestampMs();
671✔
147
  if (tDecodeSVAlterTbReqSetCtime(&dc, &vAlterTbReq, ctimeMs) < 0) {
671!
148
    goto _exit;
×
149
  }
150

151
  code = 0;
671✔
152

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

164
static int32_t vnodePreProcessDropTtlMsg(SVnode *pVnode, SRpcMsg *pMsg) {
81,678✔
165
  int32_t code = TSDB_CODE_INVALID_MSG;
81,678✔
166
  int32_t lino = 0;
81,678✔
167

168
  SMsgHead *pContOld = pMsg->pCont;
81,678✔
169
  int32_t   reqLenOld = pMsg->contLen - sizeof(SMsgHead);
81,678✔
170

171
  SArray *tbUids = NULL;
81,678✔
172
  int64_t timestampMs = 0;
81,678✔
173

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

180
  {  // find expired uids
181
    tbUids = taosArrayInit(8, sizeof(tb_uid_t));
81,894✔
182
    if (tbUids == NULL) {
81,877!
183
      code = terrno;
×
184
      TSDB_CHECK_CODE(code, lino, _exit);
×
185
    }
186

187
    timestampMs = (int64_t)ttlReq.timestampSec * 1000;
81,877✔
188
    code = metaTtlFindExpired(pVnode->pMeta, timestampMs, tbUids, ttlReq.ttlDropMaxCount);
81,877✔
189
    if (code != 0) {
81,744!
190
      code = TSDB_CODE_INVALID_MSG;
×
191
      TSDB_CHECK_CODE(code, lino, _exit);
×
192
    }
193

194
    ttlReq.nUids = taosArrayGetSize(tbUids);
81,744✔
195
    ttlReq.pTbUids = tbUids;
81,691✔
196
  }
197

198
  if (ttlReq.nUids == 0) {
81,691!
199
    code = TSDB_CODE_MSG_PREPROCESSED;
81,761✔
200
    TSDB_CHECK_CODE(code, lino, _exit);
81,761!
201
  }
202

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

207
    SMsgHead *pContNew = rpcMallocCont(contLenNew);
×
208
    if (pContNew == NULL) {
×
209
      code = terrno;
×
210
      TSDB_CHECK_CODE(code, lino, _exit);
×
211
    }
212

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

219
    rpcFreeCont(pContOld);
×
220
    pMsg->pCont = pContNew;
×
221
    pMsg->contLen = contLenNew;
×
222
  }
223

224
  code = 0;
×
225

226
_exit:
81,795✔
227
  taosArrayDestroy(tbUids);
81,795✔
228

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

236
  return code;
81,757✔
237
}
238

239
extern int64_t tsMaxKeyByPrecision[];
240
static int32_t vnodePreProcessSubmitTbData(SVnode *pVnode, SDecoder *pCoder, int64_t btimeMs, int64_t ctimeMs) {
8,997,122✔
241
  int32_t code = 0;
8,997,122✔
242
  int32_t lino = 0;
8,997,122✔
243

244
  if (tStartDecode(pCoder) < 0) {
8,997,122!
245
    code = TSDB_CODE_INVALID_MSG;
×
246
    TSDB_CHECK_CODE(code, lino, _exit);
×
247
  }
248

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

258
  int64_t uid;
259
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
8,997,227✔
260
    code = vnodePreprocessCreateTableReq(pVnode, pCoder, btimeMs, &uid);
46,604✔
261
    TSDB_CHECK_CODE(code, lino, _exit);
46,604!
262
  }
263

264
  // submit data
265
  if (tDecodeI64(pCoder, &submitTbData.suid) < 0) {
8,997,180!
266
    code = TSDB_CODE_INVALID_MSG;
×
267
    TSDB_CHECK_CODE(code, lino, _exit);
×
268
  }
269

270
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
8,997,180✔
271
    *(int64_t *)(pCoder->data + pCoder->pos) = uid;
46,604✔
272
    pCoder->pos += sizeof(int64_t);
46,604✔
273
  } else {
274
    if (tDecodeI64(pCoder, &submitTbData.uid) < 0) {
8,950,582!
275
      code = TSDB_CODE_INVALID_MSG;
×
276
      TSDB_CHECK_CODE(code, lino, _exit);
×
277
    }
278
  }
279

280
  if (tDecodeI32v(pCoder, &submitTbData.sver) < 0) {
8,997,166!
281
    code = TSDB_CODE_INVALID_MSG;
×
282
    TSDB_CHECK_CODE(code, lino, _exit);
×
283
  }
284

285
  // scan and check
286
  TSKEY now = btimeMs;
8,997,166✔
287
  if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_MICRO) {
8,997,166✔
288
    now *= 1000;
100✔
289
  } else if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_NANO) {
8,997,066✔
290
    now *= 1000000;
837✔
291
  }
292

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

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

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

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

328
    for (uint64_t i = 1; i < nColData; i++) {
4✔
329
      pCoder->pos += tGetColData(version, pCoder->data + pCoder->pos, &colData);
3✔
330
    }
331
  } else {
332
    uint64_t nRow;
333
    if (tDecodeU64v(pCoder, &nRow) < 0) {
8,997,161!
334
      code = TSDB_CODE_INVALID_MSG;
×
335
      goto _exit;
86✔
336
    }
337

338
    for (int32_t iRow = 0; iRow < nRow; ++iRow) {
250,522,058✔
339
      SRow *pRow = (SRow *)(pCoder->data + pCoder->pos);
241,524,983✔
340
      pCoder->pos += pRow->len;
241,524,983✔
341

342
      if (pRow->ts < minKey || pRow->ts > maxKey) {
241,524,983!
343
        code = TSDB_CODE_TDB_TIMESTAMP_OUT_OF_RANGE;
86✔
344
        goto _exit;
86✔
345
      }
346
    }
347
  }
348

349
  if (!tDecodeIsEnd(pCoder)) {
8,997,076!
350
    *(int64_t *)(pCoder->data + pCoder->pos) = ctimeMs;
8,997,139✔
351
    pCoder->pos += sizeof(int64_t);
8,997,139✔
352
  }
353

354
  tEndDecode(pCoder);
8,997,076✔
355

356
_exit:
8,997,159✔
357
  return code;
8,997,159✔
358
}
359
static int32_t vnodePreProcessSubmitMsg(SVnode *pVnode, SRpcMsg *pMsg) {
8,896,187✔
360
  int32_t code = 0;
8,896,187✔
361
  int32_t lino = 0;
8,896,187✔
362

363
  SDecoder *pCoder = &(SDecoder){0};
8,896,187✔
364

365
  if (taosHton64(((SSubmitReq2Msg *)pMsg->pCont)->version) != 1) {
8,896,187!
366
    code = TSDB_CODE_INVALID_MSG;
×
367
    TSDB_CHECK_CODE(code, lino, _exit);
×
368
  }
369

370
  tDecoderInit(pCoder, (uint8_t *)pMsg->pCont + sizeof(SSubmitReq2Msg), pMsg->contLen - sizeof(SSubmitReq2Msg));
8,896,207✔
371

372
  if (tStartDecode(pCoder) < 0) {
8,896,150!
373
    code = TSDB_CODE_INVALID_MSG;
×
374
    TSDB_CHECK_CODE(code, lino, _exit);
×
375
  }
376

377
  uint64_t nSubmitTbData;
378
  if (tDecodeU64v(pCoder, &nSubmitTbData) < 0) {
8,896,258!
379
    code = TSDB_CODE_INVALID_MSG;
×
380
    TSDB_CHECK_CODE(code, lino, _exit);
×
381
  }
382

383
  int64_t btimeMs = taosGetTimestampMs();
8,896,253✔
384
  int64_t ctimeMs = btimeMs;
8,896,253✔
385
  for (int32_t i = 0; i < nSubmitTbData; i++) {
17,893,334✔
386
    code = vnodePreProcessSubmitTbData(pVnode, pCoder, btimeMs, ctimeMs);
8,997,201✔
387
    TSDB_CHECK_CODE(code, lino, _exit);
8,997,167✔
388
  }
389

390
  tEndDecode(pCoder);
8,896,133✔
391

392
_exit:
8,896,217✔
393
  tDecoderClear(pCoder);
8,896,217✔
394
  if (code) {
8,896,169✔
395
    vError("vgId:%d, %s:%d failed to preprocess submit request since %s, msg type:%s", TD_VID(pVnode), __func__, lino,
86!
396
           tstrerror(code), TMSG_INFO(pMsg->msgType));
397
  }
398
  return code;
8,896,164✔
399
}
400

401
static int32_t vnodePreProcessDeleteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
53,123✔
402
  int32_t code = 0;
53,123✔
403

404
  int32_t    size;
405
  int32_t    ret;
406
  uint8_t   *pCont;
407
  SEncoder  *pCoder = &(SEncoder){0};
53,123✔
408
  SDeleteRes res = {0};
53,123✔
409

410
  SReadHandle handle = {.vnode = pVnode, .pMsgCb = &pVnode->msgCb, .skipRollup = 1};
53,123✔
411
  initStorageAPI(&handle.api);
53,123✔
412

413
  code = qWorkerProcessDeleteMsg(&handle, pVnode->pQuery, pMsg, &res);
53,124✔
414
  if (code) goto _exit;
53,127!
415

416
  res.ctimeMs = taosGetTimestampMs();
53,127✔
417
  // malloc and encode
418
  tEncodeSize(tEncodeDeleteRes, &res, size, ret);
53,127!
419
  pCont = rpcMallocCont(size + sizeof(SMsgHead));
53,126✔
420

421
  ((SMsgHead *)pCont)->contLen = size + sizeof(SMsgHead);
53,127✔
422
  ((SMsgHead *)pCont)->vgId = TD_VID(pVnode);
53,127✔
423

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

430
  rpcFreeCont(pMsg->pCont);
53,127✔
431
  pMsg->pCont = pCont;
53,127✔
432
  pMsg->contLen = size + sizeof(SMsgHead);
53,127✔
433

434
  taosArrayDestroy(res.uidList);
53,127✔
435

436
_exit:
53,127✔
437
  return code;
53,127✔
438
}
439

440
static int32_t vnodePreProcessBatchDeleteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
2,106✔
441
  int32_t code = 0;
2,106✔
442
  int32_t lino = 0;
2,106✔
443

444
  int64_t         ctimeMs = taosGetTimestampMs();
2,109✔
445
  SBatchDeleteReq pReq = {0};
2,109✔
446
  SDecoder       *pCoder = &(SDecoder){0};
2,109✔
447

448
  tDecoderInit(pCoder, (uint8_t *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead));
2,109✔
449

450
  if (tDecodeSBatchDeleteReqSetCtime(pCoder, &pReq, ctimeMs) < 0) {
2,107!
451
    code = TSDB_CODE_INVALID_MSG;
×
452
  }
453

454
  tDecoderClear(pCoder);
2,108✔
455
  taosArrayDestroy(pReq.deleteReqs);
2,106✔
456

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

465
static int32_t vnodePreProcessArbCheckSyncMsg(SVnode *pVnode, SRpcMsg *pMsg) {
×
466
  SVArbCheckSyncReq syncReq = {0};
×
467

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

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

478
  int32_t code = terrno;
×
479
  tFreeSVArbCheckSyncReq(&syncReq);
×
480

481
  return code;
×
482
}
483

484
int32_t vnodePreProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
9,099,058✔
485
  int32_t code = 0;
9,099,058✔
486

487
  switch (pMsg->msgType) {
9,099,058!
488
    case TDMT_VND_CREATE_TABLE: {
24,076✔
489
      code = vnodePreProcessCreateTableMsg(pVnode, pMsg);
24,076✔
490
    } break;
24,075✔
491
    case TDMT_VND_ALTER_TABLE: {
671✔
492
      code = vnodePreProcessAlterTableMsg(pVnode, pMsg);
671✔
493
    } break;
671✔
494
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
81,896✔
495
    case TDMT_VND_DROP_TTL_TABLE: {
496
      code = vnodePreProcessDropTtlMsg(pVnode, pMsg);
81,896✔
497
    } break;
81,723✔
498
    case TDMT_VND_SUBMIT: {
8,896,233✔
499
      code = vnodePreProcessSubmitMsg(pVnode, pMsg);
8,896,233✔
500
    } break;
8,896,159✔
501
    case TDMT_VND_DELETE: {
53,125✔
502
      code = vnodePreProcessDeleteMsg(pVnode, pMsg);
53,125✔
503
    } break;
53,127✔
504
    case TDMT_VND_BATCH_DEL: {
2,108✔
505
      code = vnodePreProcessBatchDeleteMsg(pVnode, pMsg);
2,108✔
506
    } break;
2,106✔
507
    case TDMT_VND_ARB_CHECK_SYNC: {
×
508
      code = vnodePreProcessArbCheckSyncMsg(pVnode, pMsg);
×
509
    } break;
×
510
    default:
40,949✔
511
      break;
40,949✔
512
  }
513

514
  if (code && code != TSDB_CODE_MSG_PREPROCESSED) {
9,098,810✔
515
    vError("vgId:%d, failed to preprocess write request since %s, msg type:%s", TD_VID(pVnode), tstrerror(code),
86!
516
           TMSG_INFO(pMsg->msgType));
517
  }
518
  return code;
9,098,722✔
519
}
520

521
int32_t vnodeProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg, int64_t ver, SRpcMsg *pRsp) {
11,783,652✔
522
  int32_t code = 0;
11,783,652✔
523
  void   *ptr = NULL;
11,783,652✔
524
  void   *pReq;
525
  int32_t len;
526

527
  (void)taosThreadMutexLock(&pVnode->mutex);
11,783,652✔
528
  if (pVnode->disableWrite) {
11,783,918!
529
    (void)taosThreadMutexUnlock(&pVnode->mutex);
×
530
    vError("vgId:%d write is disabled for snapshot, version:%" PRId64, TD_VID(pVnode), ver);
×
531
    return TSDB_CODE_VND_WRITE_DISABLED;
×
532
  }
533
  (void)taosThreadMutexUnlock(&pVnode->mutex);
11,783,918✔
534

535
  if (ver <= pVnode->state.applied) {
11,783,848!
536
    vError("vgId:%d, duplicate write request. ver: %" PRId64 ", applied: %" PRId64 "", TD_VID(pVnode), ver,
×
537
           pVnode->state.applied);
538
    return terrno = TSDB_CODE_VND_DUP_REQUEST;
×
539
  }
540

541
  vDebug("vgId:%d, start to process write request %s, index:%" PRId64 ", applied:%" PRId64 ", state.applyTerm:%" PRId64
11,783,848!
542
         ", conn.applyTerm:%" PRId64,
543
         TD_VID(pVnode), TMSG_INFO(pMsg->msgType), ver, pVnode->state.applied, pVnode->state.applyTerm,
544
         pMsg->info.conn.applyTerm);
545

546
  if (!(pVnode->state.applyTerm <= pMsg->info.conn.applyTerm)) {
11,783,662!
547
    return terrno = TSDB_CODE_INTERNAL_ERROR;
×
548
  }
549

550
  if (!(pVnode->state.applied + 1 == ver)) {
11,783,662!
551
    return terrno = TSDB_CODE_INTERNAL_ERROR;
×
552
  }
553

554
  atomic_store_64(&pVnode->state.applied, ver);
11,783,662✔
555
  atomic_store_64(&pVnode->state.applyTerm, pMsg->info.conn.applyTerm);
11,783,939✔
556

557
  if (!syncUtilUserCommit(pMsg->msgType)) goto _exit;
11,783,947✔
558

559
  // skip header
560
  pReq = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
11,766,759✔
561
  len = pMsg->contLen - sizeof(SMsgHead);
11,766,759✔
562
  bool needCommit = false;
11,766,759✔
563

564
  switch (pMsg->msgType) {
11,766,759!
565
    /* META */
566
    case TDMT_VND_CREATE_STB:
9,379✔
567
      if (vnodeProcessCreateStbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
9,379✔
568
      break;
9,369✔
569
    case TDMT_VND_ALTER_STB:
656✔
570
      if (vnodeProcessAlterStbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
656!
571
      break;
658✔
572
    case TDMT_VND_DROP_STB:
156✔
573
      if (vnodeProcessDropStbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
156!
574
      break;
156✔
575
    case TDMT_VND_CREATE_TABLE:
28,798✔
576
      if (vnodeProcessCreateTbReq(pVnode, ver, pReq, len, pRsp, pMsg) < 0) goto _err;
28,798!
577
      break;
28,805✔
578
    case TDMT_VND_ALTER_TABLE:
671✔
579
      if (vnodeProcessAlterTbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
671!
580
      break;
671✔
581
    case TDMT_VND_DROP_TABLE:
203✔
582
      if (vnodeProcessDropTbReq(pVnode, ver, pReq, len, pRsp, pMsg) < 0) goto _err;
203!
583
      break;
203✔
584
    case TDMT_VND_DROP_TTL_TABLE:
×
585
      if (vnodeProcessDropTtlTbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
586
      break;
×
587
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
×
588
      if (vnodeProcessFetchTtlExpiredTbs(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
589
      break;
×
590
    case TDMT_VND_TRIM:
229✔
591
      if (vnodeProcessTrimReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
229!
592
      break;
229✔
593
    case TDMT_VND_S3MIGRATE:
×
594
      if (vnodeProcessS3MigrateReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
595
      break;
×
596
    case TDMT_VND_CREATE_SMA:
24✔
597
      if (vnodeProcessCreateTSmaReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
24!
598
      break;
24✔
599
    /* TSDB */
600
    case TDMT_VND_SUBMIT:
11,628,438✔
601
      if (vnodeProcessSubmitReq(pVnode, ver, pMsg->pCont, pMsg->contLen, pRsp, pMsg) < 0) goto _err;
11,628,438!
602
      break;
11,628,456✔
603
    case TDMT_VND_DELETE:
53,235✔
604
      if (vnodeProcessDeleteReq(pVnode, ver, pReq, len, pRsp, pMsg) < 0) goto _err;
53,235!
605
      break;
53,235✔
606
    case TDMT_VND_BATCH_DEL:
2,458✔
607
      if (vnodeProcessBatchDeleteReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
2,458!
608
      break;
2,467✔
609
    /* TQ */
610
    case TDMT_VND_TMQ_SUBSCRIBE:
535✔
611
      if (tqProcessSubscribeReq(pVnode->pTq, ver, pReq, len) < 0) {
535!
612
        goto _err;
×
613
      }
614
      break;
535✔
615
    case TDMT_VND_TMQ_DELETE_SUB:
12✔
616
      if (tqProcessDeleteSubReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen) < 0) {
12!
617
        goto _err;
×
618
      }
619
      break;
12✔
620
    case TDMT_VND_TMQ_COMMIT_OFFSET:
670✔
621
      if (tqProcessOffsetCommitReq(pVnode->pTq, ver, pReq, len) < 0) {
670!
622
        goto _err;
×
623
      }
624
      break;
671✔
625
    case TDMT_VND_TMQ_ADD_CHECKINFO:
75✔
626
      if (tqProcessAddCheckInfoReq(pVnode->pTq, ver, pReq, len) < 0) {
75!
627
        goto _err;
×
628
      }
629
      break;
75✔
630
    case TDMT_VND_TMQ_DEL_CHECKINFO:
3✔
631
      if (tqProcessDelCheckInfoReq(pVnode->pTq, ver, pReq, len) < 0) {
3✔
632
        goto _err;
1✔
633
      }
634
      break;
2✔
635
    case TDMT_STREAM_TASK_DEPLOY: {
6,845✔
636
      if ((code = tqProcessTaskDeployReq(pVnode->pTq, ver, pReq, len)) != TSDB_CODE_SUCCESS) {
6,845!
637
        goto _err;
×
638
      }
639
    } break;
6,862✔
640
    case TDMT_STREAM_TASK_DROP: {
3,631✔
641
      if ((code = tqProcessTaskDropReq(pVnode->pTq, pMsg->pCont, pMsg->contLen)) < 0) {
3,631!
642
        goto _err;
×
643
      }
644
    } break;
3,650✔
645
    case TDMT_STREAM_TASK_UPDATE_CHKPT: {
4,474✔
646
      if ((code = tqProcessTaskUpdateCheckpointReq(pVnode->pTq, pMsg->pCont, pMsg->contLen)) < 0) {
4,474!
647
        goto _err;
×
648
      }
649
    } break;
4,486✔
650
    case TDMT_STREAM_CONSEN_CHKPT: {
116✔
651
      if (pVnode->restored && (code = tqProcessTaskConsenChkptIdReq(pVnode->pTq, pMsg)) < 0) {
116!
652
        goto _err;
×
653
      }
654

655
    } break;
116✔
656
    case TDMT_STREAM_TASK_PAUSE: {
1,216✔
657
      if (pVnode->restored && vnodeIsLeader(pVnode) &&
1,847!
658
          (code = tqProcessTaskPauseReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen)) < 0) {
630✔
659
        goto _err;
×
660
      }
661
    } break;
1,225✔
662
    case TDMT_STREAM_TASK_RESUME: {
1,211✔
663
      if (pVnode->restored && vnodeIsLeader(pVnode) &&
1,833!
664
          (code = tqProcessTaskResumeReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen)) < 0) {
621✔
665
        goto _err;
551✔
666
      }
667
    } break;
665✔
668
    case TDMT_VND_STREAM_TASK_RESET: {
×
669
      if (pVnode->restored && vnodeIsLeader(pVnode) &&
×
670
           (code = tqProcessTaskResetReq(pVnode->pTq, pMsg)) < 0) {
×
671
          goto _err;
×
672
        }
673

674
    } break;
×
675
    case TDMT_VND_ALTER_CONFIRM:
6,977✔
676
      needCommit = pVnode->config.hashChange;
6,977✔
677
      if (vnodeProcessAlterConfirmReq(pVnode, ver, pReq, len, pRsp) < 0) {
6,977!
678
        goto _err;
×
679
      }
680
      break;
6,977✔
681
    case TDMT_VND_ALTER_CONFIG:
491✔
682
      vnodeProcessAlterConfigReq(pVnode, ver, pReq, len, pRsp);
491✔
683
      break;
495✔
684
    case TDMT_VND_COMMIT:
10,237✔
685
      needCommit = true;
10,237✔
686
      break;
10,237✔
687
    case TDMT_VND_CREATE_INDEX:
951✔
688
      vnodeProcessCreateIndexReq(pVnode, ver, pReq, len, pRsp);
951✔
689
      break;
951✔
690
    case TDMT_VND_DROP_INDEX:
2,132✔
691
      vnodeProcessDropIndexReq(pVnode, ver, pReq, len, pRsp);
2,132✔
692
      break;
2,132✔
693
    case TDMT_VND_STREAM_CHECK_POINT_SOURCE: // always return true
2,853✔
694
      tqProcessTaskCheckPointSourceReq(pVnode->pTq, pMsg, pRsp);
2,853✔
695
      break;
2,862✔
696
    case TDMT_VND_STREAM_TASK_UPDATE:  // always return true
70✔
697
      tqProcessTaskUpdateReq(pVnode->pTq, pMsg);
70✔
698
      break;
70✔
699
    case TDMT_VND_COMPACT:
13✔
700
      vnodeProcessCompactVnodeReq(pVnode, ver, pReq, len, pRsp);
13✔
701
      goto _exit;
13✔
702
    case TDMT_SYNC_CONFIG_CHANGE:
×
703
      vnodeProcessConfigChangeReq(pVnode, ver, pReq, len, pRsp);
×
704
      break;
×
705
#ifdef TD_ENTERPRISE
706
    case TDMT_VND_KILL_COMPACT:
×
707
      vnodeProcessKillCompactReq(pVnode, ver, pReq, len, pRsp);
×
708
      break;
×
709
#endif
710
    /* ARB */
711
    case TDMT_VND_ARB_CHECK_SYNC:
×
712
      vnodeProcessArbCheckSyncReq(pVnode, pReq, len, pRsp);
×
713
      break;
×
714
    default:
×
715
      vError("vgId:%d, unprocessed msg, %d", TD_VID(pVnode), pMsg->msgType);
×
716
      return TSDB_CODE_INVALID_MSG;
×
717
  }
718

719
  vTrace("vgId:%d, process %s request, code:0x%x index:%" PRId64, TD_VID(pVnode), TMSG_INFO(pMsg->msgType), pRsp->code,
11,766,296!
720
         ver);
721

722
  walApplyVer(pVnode->pWal, ver);
11,766,296✔
723

724
  code = tqPushMsg(pVnode->pTq, pMsg->msgType);
11,766,250✔
725
  if (code) {
11,766,354✔
726
    vError("vgId:%d, failed to push msg to TQ since %s", TD_VID(pVnode), tstrerror(terrno));
436!
727
    return code;
436✔
728
  }
729

730
  // commit if need
731
  if (needCommit) {
11,765,918✔
732
    vInfo("vgId:%d, commit at version %" PRId64, TD_VID(pVnode), ver);
9,834!
733
    code = vnodeAsyncCommit(pVnode);
9,838✔
734
    if (code) {
9,839!
735
      vError("vgId:%d, failed to vnode async commit since %s.", TD_VID(pVnode), tstrerror(terrno));
×
736
      goto _err;
×
737
    }
738

739
    // start a new one
740
    code = vnodeBegin(pVnode);
9,839✔
741
    if (code) {
9,838✔
742
      vError("vgId:%d, failed to begin vnode since %s.", TD_VID(pVnode), tstrerror(terrno));
51!
743
      goto _err;
×
744
    }
745
  }
746

747
_exit:
11,765,871✔
748
  return 0;
11,782,684✔
749

750
_err:
556✔
751
  vError("vgId:%d, process %s request failed since %s, ver:%" PRId64, TD_VID(pVnode), TMSG_INFO(pMsg->msgType),
556!
752
         tstrerror(code), ver);
753
  return code;
557✔
754
}
755

756
int32_t vnodePreprocessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg) {
5,458,466✔
757
  if (TDMT_SCH_QUERY != pMsg->msgType && TDMT_SCH_MERGE_QUERY != pMsg->msgType) {
5,458,466✔
758
    return 0;
255,519✔
759
  }
760

761
  return qWorkerPreprocessQueryMsg(pVnode->pQuery, pMsg, TDMT_SCH_QUERY == pMsg->msgType);
5,202,947✔
762
}
763

764
int32_t vnodeProcessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
5,467,121✔
765
  vTrace("message in vnode query queue is processing");
5,467,121!
766
  if ((pMsg->msgType == TDMT_SCH_QUERY || pMsg->msgType == TDMT_VND_TMQ_CONSUME) && !syncIsReadyForRead(pVnode->sync)) {
5,467,121✔
767
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
219✔
768
    return 0;
219✔
769
  }
770

771
  if (pMsg->msgType == TDMT_VND_TMQ_CONSUME && !pVnode->restored) {
5,466,701!
772
    vnodeRedirectRpcMsg(pVnode, pMsg, TSDB_CODE_SYN_RESTORING);
×
773
    return 0;
×
774
  }
775

776
  SReadHandle handle = {.vnode = pVnode, .pMsgCb = &pVnode->msgCb, .pWorkerCb = pInfo->workerCb};
5,466,701✔
777
  initStorageAPI(&handle.api);
5,466,701✔
778

779
  switch (pMsg->msgType) {
5,465,122!
780
    case TDMT_SCH_QUERY:
5,209,802✔
781
    case TDMT_SCH_MERGE_QUERY:
782
      return qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
5,209,802✔
783
    case TDMT_SCH_QUERY_CONTINUE:
251,031✔
784
      return qWorkerProcessCQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
251,031✔
785
    case TDMT_VND_TMQ_CONSUME:
3,265✔
786
      return tqProcessPollReq(pVnode->pTq, pMsg);
3,265✔
787
    case TDMT_VND_TMQ_CONSUME_PUSH:
1,082✔
788
      return tqProcessPollPush(pVnode->pTq, pMsg);
1,082✔
789
    default:
×
790
      vError("unknown msg type:%d in query queue", pMsg->msgType);
×
791
      return TSDB_CODE_APP_ERROR;
×
792
  }
793
}
794

795
int32_t vnodeProcessFetchMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
13,962,156✔
796
  vTrace("vgId:%d, msg:%p in fetch queue is processing", pVnode->config.vgId, pMsg);
13,962,156!
797
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
13,962,156!
798
       pMsg->msgType == TDMT_VND_BATCH_META || pMsg->msgType == TDMT_VND_TABLE_NAME) &&
10,099,185✔
799
      !syncIsReadyForRead(pVnode->sync)) {
4,951,808✔
800
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
3,654✔
801
    return 0;
3,654✔
802
  }
803

804
  switch (pMsg->msgType) {
13,958,841!
805
    case TDMT_SCH_FETCH:
7,040,479✔
806
    case TDMT_SCH_MERGE_FETCH:
807
      return qWorkerProcessFetchMsg(pVnode, pVnode->pQuery, pMsg, 0);
7,040,479✔
808
    case TDMT_SCH_FETCH_RSP:
×
809
      return qWorkerProcessRspMsg(pVnode, pVnode->pQuery, pMsg, 0);
×
810
    // case TDMT_SCH_CANCEL_TASK:
811
    //   return qWorkerProcessCancelMsg(pVnode, pVnode->pQuery, pMsg, 0);
812
    case TDMT_SCH_DROP_TASK:
5,208,832✔
813
      return qWorkerProcessDropMsg(pVnode, pVnode->pQuery, pMsg, 0);
5,208,832✔
814
    case TDMT_SCH_TASK_NOTIFY:
125✔
815
      return qWorkerProcessNotifyMsg(pVnode, pVnode->pQuery, pMsg, 0);
125✔
816
    case TDMT_SCH_QUERY_HEARTBEAT:
628,978✔
817
      return qWorkerProcessHbMsg(pVnode, pVnode->pQuery, pMsg, 0);
628,978✔
818
    case TDMT_VND_TABLE_META:
3✔
819
    case TDMT_VND_TABLE_NAME:
820
      return vnodeGetTableMeta(pVnode, pMsg, true);
3✔
821
    case TDMT_VND_TABLE_CFG:
×
822
      return vnodeGetTableCfg(pVnode, pMsg, true);
×
823
    case TDMT_VND_BATCH_META:
1,080,414✔
824
      return vnodeGetBatchMeta(pVnode, pMsg);
1,080,414✔
825
#ifdef TD_ENTERPRISE
826
    case TDMT_VND_QUERY_COMPACT_PROGRESS:
10✔
827
      return vnodeQueryCompactProgress(pVnode, pMsg);
10✔
828
#endif
829
      //    case TDMT_VND_TMQ_CONSUME:
830
      //      return tqProcessPollReq(pVnode->pTq, pMsg);
831
    case TDMT_VND_TMQ_VG_WALINFO:
×
832
      return tqProcessVgWalInfoReq(pVnode->pTq, pMsg);
×
833
    case TDMT_VND_TMQ_VG_COMMITTEDINFO:
×
834
      return tqProcessVgCommittedInfoReq(pVnode->pTq, pMsg);
×
835
    case TDMT_VND_TMQ_SEEK:
×
836
      return tqProcessSeekReq(pVnode->pTq, pMsg);
×
837

838
    default:
×
839
      vError("unknown msg type:%d in fetch queue", pMsg->msgType);
×
840
      return TSDB_CODE_APP_ERROR;
×
841
  }
842
}
843

844
int32_t vnodeProcessStreamMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
255,261✔
845
  vTrace("vgId:%d, msg:%p in fetch queue is processing", pVnode->config.vgId, pMsg);
255,261✔
846
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
255,261!
847
       pMsg->msgType == TDMT_VND_BATCH_META) &&
255,289!
848
      !syncIsReadyForRead(pVnode->sync)) {
×
849
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
×
850
    return 0;
×
851
  }
852

853
  switch (pMsg->msgType) {
255,303!
854
    case TDMT_STREAM_TASK_RUN:
131,742✔
855
      return tqProcessTaskRunReq(pVnode->pTq, pMsg);
131,742✔
856
    case TDMT_STREAM_TASK_DISPATCH:
33,917✔
857
      return tqProcessTaskDispatchReq(pVnode->pTq, pMsg);
33,917✔
858
    case TDMT_STREAM_TASK_DISPATCH_RSP:
34,114✔
859
      return tqProcessTaskDispatchRsp(pVnode->pTq, pMsg);
34,114✔
860
    case TDMT_VND_STREAM_TASK_CHECK:
10,314✔
861
      return tqProcessTaskCheckReq(pVnode->pTq, pMsg);
10,314✔
862
    case TDMT_VND_STREAM_TASK_CHECK_RSP:
10,317✔
863
      return tqProcessTaskCheckRsp(pVnode->pTq, pMsg);
10,317✔
864
    case TDMT_STREAM_RETRIEVE:
439✔
865
      return tqProcessTaskRetrieveReq(pVnode->pTq, pMsg);
439✔
866
    case TDMT_STREAM_RETRIEVE_RSP:
324✔
867
      return tqProcessTaskRetrieveRsp(pVnode->pTq, pMsg);
324✔
868
    case TDMT_VND_STREAM_SCAN_HISTORY:
1,027✔
869
      return tqProcessTaskScanHistory(pVnode->pTq, pMsg);
1,027✔
870
    case TDMT_STREAM_TASK_CHECKPOINT_READY:
3,741✔
871
      return tqProcessTaskCheckpointReadyMsg(pVnode->pTq, pMsg);
3,741✔
872
    case TDMT_STREAM_TASK_CHECKPOINT_READY_RSP:
3,745✔
873
      return tqProcessTaskCheckpointReadyRsp(pVnode->pTq, pMsg);
3,745✔
874
    case TDMT_STREAM_RETRIEVE_TRIGGER:
×
875
      return tqProcessTaskRetrieveTriggerReq(pVnode->pTq, pMsg);
×
876
    case TDMT_STREAM_RETRIEVE_TRIGGER_RSP:
×
877
      return tqProcessTaskRetrieveTriggerRsp(pVnode->pTq, pMsg);
×
878
    case TDMT_MND_STREAM_HEARTBEAT_RSP:
20,138✔
879
      return tqProcessStreamHbRsp(pVnode->pTq, pMsg);
20,138✔
880
    case TDMT_MND_STREAM_REQ_CHKPT_RSP:
1,911✔
881
      return tqProcessStreamReqCheckpointRsp(pVnode->pTq, pMsg);
1,911✔
882
    case TDMT_VND_GET_STREAM_PROGRESS:
×
883
      return tqStreamProgressRetrieveReq(pVnode->pTq, pMsg);
×
884
    case TDMT_MND_STREAM_CHKPT_REPORT_RSP:
3,574✔
885
      return tqProcessTaskChkptReportRsp(pVnode->pTq, pMsg);
3,574✔
886
    default:
×
887
      vError("unknown msg type:%d in stream queue", pMsg->msgType);
×
888
      return TSDB_CODE_APP_ERROR;
×
889
  }
890
}
891

892
void smaHandleRes(void *pVnode, int64_t smaId, const SArray *data) {
2✔
893
  int32_t code = tdProcessTSmaInsert(((SVnode *)pVnode)->pSma, smaId, (const char *)data);
2✔
894
  if (code) {
2!
895
    vError("failed to process sma result since %s", tstrerror(code));
×
896
  }
897
}
2✔
898

899
void vnodeUpdateMetaRsp(SVnode *pVnode, STableMetaRsp *pMetaRsp) {
87,402✔
900
  if (NULL == pMetaRsp) {
87,402!
901
    return;
×
902
  }
903

904
  strcpy(pMetaRsp->dbFName, pVnode->config.dbname);
87,402✔
905
  pMetaRsp->dbId = pVnode->config.dbId;
87,402✔
906
  pMetaRsp->vgId = TD_VID(pVnode);
87,402✔
907
  pMetaRsp->precision = pVnode->config.tsdbCfg.precision;
87,402✔
908
}
909

910
extern int32_t vnodeAsyncRetention(SVnode *pVnode, int64_t now);
911

912
static int32_t vnodeProcessTrimReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
229✔
913
  int32_t     code = 0;
229✔
914
  SVTrimDbReq trimReq = {0};
229✔
915

916
  // decode
917
  if (tDeserializeSVTrimDbReq(pReq, len, &trimReq) != 0) {
229!
918
    code = TSDB_CODE_INVALID_MSG;
×
919
    goto _exit;
×
920
  }
921

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

924
  code = vnodeAsyncRetention(pVnode, trimReq.timestamp);
229✔
925

926
_exit:
229✔
927
  return code;
229✔
928
}
929

930
extern int32_t vnodeAsyncS3Migrate(SVnode *pVnode, int64_t now);
931

932
static int32_t vnodeProcessS3MigrateReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
933
  int32_t          code = 0;
×
934
  SVS3MigrateDbReq s3migrateReq = {0};
×
935

936
  // decode
937
  if (tDeserializeSVS3MigrateDbReq(pReq, len, &s3migrateReq) != 0) {
×
938
    code = TSDB_CODE_INVALID_MSG;
×
939
    goto _exit;
×
940
  }
941

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

944
  code = vnodeAsyncS3Migrate(pVnode, s3migrateReq.timestamp);
×
945

946
_exit:
×
947
  return code;
×
948
}
949

950
static int32_t vnodeProcessDropTtlTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
951
  int               ret = 0;
×
952
  SVDropTtlTableReq ttlReq = {0};
×
953
  if (tDeserializeSVDropTtlTableReq(pReq, len, &ttlReq) != 0) {
×
954
    ret = TSDB_CODE_INVALID_MSG;
×
955
    goto end;
×
956
  }
957

958
  if (ttlReq.nUids != taosArrayGetSize(ttlReq.pTbUids)) {
×
959
    ret = TSDB_CODE_INVALID_MSG;
×
960
    goto end;
×
961
  }
962

963
  if (ttlReq.nUids != 0) {
×
964
    vInfo("vgId:%d, drop ttl table req will be processed, time:%d, ntbUids:%d", pVnode->config.vgId,
×
965
          ttlReq.timestampSec, ttlReq.nUids);
966
  }
967

968
  if (ttlReq.nUids > 0) {
×
969
    int32_t code = metaDropTables(pVnode->pMeta, ttlReq.pTbUids);
×
970
    if (code) return code;
×
971

972
    code = tqUpdateTbUidList(pVnode->pTq, ttlReq.pTbUids, false);
×
973
    if (code) {
×
974
      vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
975
    }
976
  }
977

978
end:
×
979
  taosArrayDestroy(ttlReq.pTbUids);
×
980
  return ret;
×
981
}
982

983
static int32_t vnodeProcessFetchTtlExpiredTbs(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
984
  int32_t                 code = -1;
×
985
  SMetaReader             mr = {0};
×
986
  SVDropTtlTableReq       ttlReq = {0};
×
987
  SVFetchTtlExpiredTbsRsp rsp = {0};
×
988
  SEncoder                encoder = {0};
×
989
  SArray                 *pNames = NULL;
×
990
  pRsp->msgType = TDMT_VND_FETCH_TTL_EXPIRED_TBS_RSP;
×
991
  pRsp->code = TSDB_CODE_SUCCESS;
×
992
  pRsp->pCont = NULL;
×
993
  pRsp->contLen = 0;
×
994

995
  if (tDeserializeSVDropTtlTableReq(pReq, len, &ttlReq) != 0) {
×
996
    terrno = TSDB_CODE_INVALID_MSG;
×
997
    goto _end;
×
998
  }
999

1000
  if (!(ttlReq.nUids == taosArrayGetSize(ttlReq.pTbUids))) {
×
1001
    terrno = TSDB_CODE_INVALID_MSG;
×
1002
    goto _end;
×
1003
  }
1004

1005
  tb_uid_t    suid;
1006
  char        ctbName[TSDB_TABLE_NAME_LEN];
1007
  SVDropTbReq expiredTb = {.igNotExists = true};
×
1008
  metaReaderDoInit(&mr, pVnode->pMeta, 0);
×
1009
  rsp.vgId = TD_VID(pVnode);
×
1010
  rsp.pExpiredTbs = taosArrayInit(ttlReq.nUids, sizeof(SVDropTbReq));
×
1011
  if (!rsp.pExpiredTbs) goto _end;
×
1012

1013
  pNames = taosArrayInit(ttlReq.nUids, TSDB_TABLE_NAME_LEN);
×
1014
  if (!pNames) {
×
1015
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1016
    goto _end;
×
1017
  }
1018
  char buf[TSDB_TABLE_NAME_LEN];
1019
  for (int32_t i = 0; i < ttlReq.nUids; ++i) {
×
1020
    tb_uid_t *uid = taosArrayGet(ttlReq.pTbUids, i);
×
1021
    expiredTb.suid = *uid;
×
1022
    terrno = metaReaderGetTableEntryByUid(&mr, *uid);
×
1023
    if (terrno < 0) goto _end;
×
1024
    strncpy(buf, mr.me.name, TSDB_TABLE_NAME_LEN);
×
1025
    void *p = taosArrayPush(pNames, buf);
×
1026
    if (p == NULL) {
×
1027
      goto _end;
×
1028
    }
1029

1030
    expiredTb.name = p;
×
1031
    if (mr.me.type == TSDB_CHILD_TABLE) {
×
1032
      expiredTb.suid = mr.me.ctbEntry.suid;
×
1033
    }
1034

1035
    if (taosArrayPush(rsp.pExpiredTbs, &expiredTb) == NULL) {
×
1036
      goto _end;
×
1037
    }
1038
  }
1039

1040
  int32_t ret = 0;
×
1041
  tEncodeSize(tEncodeVFetchTtlExpiredTbsRsp, &rsp, pRsp->contLen, ret);
×
1042
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
×
1043
  if (pRsp->pCont == NULL) {
×
1044
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1045
    code = -1;
×
1046
    goto _end;
×
1047
  }
1048
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
×
1049
  terrno = tEncodeVFetchTtlExpiredTbsRsp(&encoder, &rsp);
×
1050
  tEncoderClear(&encoder);
×
1051

1052
  if (terrno == 0) code = 0;
×
1053
_end:
×
1054
  metaReaderClear(&mr);
×
1055
  tFreeFetchTtlExpiredTbsRsp(&rsp);
×
1056
  taosArrayDestroy(ttlReq.pTbUids);
×
1057
  if (pNames) taosArrayDestroy(pNames);
×
1058
  pRsp->code = terrno;
×
1059
  return code;
×
1060
}
1061

1062
static int32_t vnodeProcessCreateStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
9,363✔
1063
  int32_t        code = 0;
9,363✔
1064
  SVCreateStbReq req = {0};
9,363✔
1065
  SDecoder       coder;
1066

1067
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
9,363✔
1068
  pRsp->code = TSDB_CODE_SUCCESS;
9,363✔
1069
  pRsp->pCont = NULL;
9,363✔
1070
  pRsp->contLen = 0;
9,363✔
1071

1072
  // decode and process req
1073
  tDecoderInit(&coder, pReq, len);
9,363✔
1074

1075
  code = tDecodeSVCreateStbReq(&coder, &req);
9,374✔
1076
  if (code) {
9,391!
1077
    pRsp->code = code;
×
1078
    goto _err;
×
1079
  }
1080

1081
  code = metaCreateSTable(pVnode->pMeta, ver, &req);
9,391✔
1082
  if (code) {
9,366✔
1083
    pRsp->code = code;
4✔
1084
    goto _err;
4✔
1085
  }
1086

1087
  if ((code = tdProcessRSmaCreate(pVnode->pSma, &req)) < 0) {
9,362!
1088
    pRsp->code = code;
×
1089
    goto _err;
×
1090
  }
1091

1092
  tDecoderClear(&coder);
9,404✔
1093
  return 0;
9,405✔
1094

1095
_err:
4✔
1096
  tDecoderClear(&coder);
4✔
1097
  return code;
4✔
1098
}
1099

1100
static int32_t vnodeProcessCreateTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
28,798✔
1101
                                       SRpcMsg *pOriginRpc) {
1102
  SDecoder           decoder = {0};
28,798✔
1103
  SEncoder           encoder = {0};
28,798✔
1104
  int32_t            rcode = 0;
28,798✔
1105
  SVCreateTbBatchReq req = {0};
28,798✔
1106
  SVCreateTbReq     *pCreateReq;
1107
  SVCreateTbBatchRsp rsp = {0};
28,798✔
1108
  SVCreateTbRsp      cRsp = {0};
28,798✔
1109
  char               tbName[TSDB_TABLE_FNAME_LEN];
1110
  STbUidStore       *pStore = NULL;
28,798✔
1111
  SArray            *tbUids = NULL;
28,798✔
1112
  SArray            *tbNames = NULL;
28,798✔
1113

1114
  pRsp->msgType = TDMT_VND_CREATE_TABLE_RSP;
28,798✔
1115
  pRsp->code = TSDB_CODE_SUCCESS;
28,798✔
1116
  pRsp->pCont = NULL;
28,798✔
1117
  pRsp->contLen = 0;
28,798✔
1118

1119
  // decode
1120
  tDecoderInit(&decoder, pReq, len);
28,798✔
1121
  if (tDecodeSVCreateTbBatchReq(&decoder, &req) < 0) {
28,797!
1122
    rcode = -1;
×
1123
    terrno = TSDB_CODE_INVALID_MSG;
×
1124
    goto _exit;
×
1125
  }
1126

1127
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(cRsp));
28,801✔
1128
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
28,804✔
1129
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
28,805✔
1130
  if (rsp.pArray == NULL || tbUids == NULL || tbNames == NULL) {
28,805!
1131
    rcode = -1;
1✔
1132
    terrno = TSDB_CODE_OUT_OF_MEMORY;
1✔
1133
    goto _exit;
×
1134
  }
1135

1136
  // loop to create table
1137
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
79,943✔
1138
    pCreateReq = req.pReqs + iReq;
51,142✔
1139
    memset(&cRsp, 0, sizeof(cRsp));
51,142✔
1140

1141
    if (tsEnableAudit && tsEnableAuditCreateTable) {
51,142!
1142
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
51,142✔
1143
      if (str == NULL) {
51,144!
1144
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1145
        rcode = -1;
×
1146
        goto _exit;
×
1147
      }
1148
      strcpy(str, pCreateReq->name);
51,144✔
1149
      if (taosArrayPush(tbNames, &str) == NULL) {
51,144!
1150
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1151
        rcode = -1;
×
1152
        goto _exit;
×
1153
      }
1154
    }
1155

1156
    // validate hash
1157
    sprintf(tbName, "%s.%s", pVnode->config.dbname, pCreateReq->name);
51,144✔
1158
    if (vnodeValidateTableHash(pVnode, tbName) < 0) {
51,144✔
1159
      cRsp.code = TSDB_CODE_VND_HASH_MISMATCH;
21✔
1160
      if (taosArrayPush(rsp.pArray, &cRsp) == NULL) {
42!
1161
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1162
        rcode = -1;
×
1163
        goto _exit;
×
1164
      }
1165
      vError("vgId:%d create-table:%s failed due to hash value mismatch", TD_VID(pVnode), tbName);
21!
1166
      continue;
21✔
1167
    }
1168

1169
    // do create table
1170
    if (metaCreateTable(pVnode->pMeta, ver, pCreateReq, &cRsp.pMeta) < 0) {
51,132✔
1171
      if (pCreateReq->flags & TD_CREATE_IF_NOT_EXISTS && terrno == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
757!
1172
        cRsp.code = TSDB_CODE_SUCCESS;
3✔
1173
      } else {
1174
        cRsp.code = terrno;
754✔
1175
      }
1176
    } else {
1177
      cRsp.code = TSDB_CODE_SUCCESS;
50,369✔
1178
      if (tdFetchTbUidList(pVnode->pSma, &pStore, pCreateReq->ctb.suid, pCreateReq->uid) < 0) {
50,369!
1179
        vError("vgId:%d, failed to fetch tbUid list", TD_VID(pVnode));
×
1180
      }
1181
      if (taosArrayPush(tbUids, &pCreateReq->uid) == NULL) {
100,738!
1182
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1183
        rcode = -1;
×
1184
        goto _exit;
×
1185
      }
1186
      vnodeUpdateMetaRsp(pVnode, cRsp.pMeta);
50,366✔
1187
    }
1188

1189
    if (taosArrayPush(rsp.pArray, &cRsp) == NULL) {
102,240!
1190
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1191
      rcode = -1;
×
1192
      goto _exit;
×
1193
    }
1194
  }
1195

1196
  vDebug("vgId:%d, add %d new created tables into query table list", TD_VID(pVnode), (int32_t)taosArrayGetSize(tbUids));
28,801✔
1197
  if (tqUpdateTbUidList(pVnode->pTq, tbUids, true) < 0) {
28,801!
1198
    vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(terrno));
×
1199
  }
1200
  if (tdUpdateTbUidList(pVnode->pSma, pStore, true) < 0) {
28,805!
1201
    goto _exit;
×
1202
  }
1203
  pStore = tdUidStoreFree(pStore);
28,805✔
1204

1205
  // prepare rsp
1206
  int32_t ret = 0;
28,805✔
1207
  tEncodeSize(tEncodeSVCreateTbBatchRsp, &rsp, pRsp->contLen, ret);
28,805!
1208
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
28,805✔
1209
  if (pRsp->pCont == NULL) {
28,804!
1210
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1211
    rcode = -1;
×
1212
    goto _exit;
×
1213
  }
1214
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
28,804✔
1215
  if (tEncodeSVCreateTbBatchRsp(&encoder, &rsp) != 0) {
28,804!
1216
    vError("vgId:%d, failed to encode create table batch response", TD_VID(pVnode));
×
1217
  }
1218

1219
  if (tsEnableAudit && tsEnableAuditCreateTable) {
28,803!
1220
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
28,801✔
1221

1222
    SName name = {0};
28,801✔
1223
    if (tNameFromString(&name, pVnode->config.dbname, T_NAME_ACCT | T_NAME_DB) < 0) {
28,801!
1224
      vError("vgId:%d, failed to get name from string", TD_VID(pVnode));
×
1225
    }
1226

1227
    SStringBuilder sb = {0};
28,803✔
1228
    for (int32_t i = 0; i < tbNames->size; i++) {
79,950✔
1229
      char **key = (char **)taosArrayGet(tbNames, i);
51,150✔
1230
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
51,150✔
1231
      if (i < tbNames->size - 1) {
51,149✔
1232
        taosStringBuilderAppendChar(&sb, ',');
22,349✔
1233
      }
1234
      // taosMemoryFreeClear(*key);
1235
    }
1236

1237
    size_t len = 0;
28,800✔
1238
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
28,800✔
1239

1240
    if (pOriginRpc->info.conn.user != NULL && strlen(pOriginRpc->info.conn.user) > 0) {
28,798!
1241
      auditAddRecord(pOriginRpc, clusterId, "createTable", name.dbname, "", keyJoined, len);
20,753✔
1242
    }
1243

1244
    taosStringBuilderDestroy(&sb);
28,798✔
1245
  }
1246

1247
_exit:
2✔
1248
  tDeleteSVCreateTbBatchReq(&req);
28,802✔
1249
  taosArrayDestroyEx(rsp.pArray, tFreeSVCreateTbRsp);
28,804✔
1250
  taosArrayDestroy(tbUids);
28,802✔
1251
  tDecoderClear(&decoder);
28,804✔
1252
  tEncoderClear(&encoder);
28,804✔
1253
  taosArrayDestroyP(tbNames, taosMemoryFree);
28,804✔
1254
  return rcode;
28,805✔
1255
}
1256

1257
static int32_t vnodeProcessAlterStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
656✔
1258
  int32_t        code = 0;
656✔
1259
  SVCreateStbReq req = {0};
656✔
1260
  SDecoder       dc = {0};
656✔
1261

1262
  pRsp->msgType = TDMT_VND_ALTER_STB_RSP;
656✔
1263
  pRsp->code = TSDB_CODE_SUCCESS;
656✔
1264
  pRsp->pCont = NULL;
656✔
1265
  pRsp->contLen = 0;
656✔
1266

1267
  tDecoderInit(&dc, pReq, len);
656✔
1268

1269
  // decode req
1270
  code = tDecodeSVCreateStbReq(&dc, &req);
657✔
1271
  if (code) {
657!
1272
    tDecoderClear(&dc);
×
1273
    return code;
×
1274
  }
1275

1276
  code = metaAlterSTable(pVnode->pMeta, ver, &req);
657✔
1277
  if (code) {
658!
1278
    pRsp->code = code;
×
1279
    tDecoderClear(&dc);
×
1280
    return code;
×
1281
  }
1282

1283
  tDecoderClear(&dc);
658✔
1284

1285
  return 0;
658✔
1286
}
1287

1288
static int32_t vnodeProcessDropStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
156✔
1289
  SVDropStbReq req = {0};
156✔
1290
  int32_t      rcode = TSDB_CODE_SUCCESS;
156✔
1291
  SDecoder     decoder = {0};
156✔
1292
  SArray      *tbUidList = NULL;
156✔
1293

1294
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
156✔
1295
  pRsp->pCont = NULL;
156✔
1296
  pRsp->contLen = 0;
156✔
1297

1298
  // decode request
1299
  tDecoderInit(&decoder, pReq, len);
156✔
1300
  if (tDecodeSVDropStbReq(&decoder, &req) < 0) {
156!
1301
    rcode = TSDB_CODE_INVALID_MSG;
×
1302
    goto _exit;
×
1303
  }
1304

1305
  // process request
1306
  tbUidList = taosArrayInit(8, sizeof(int64_t));
156✔
1307
  if (tbUidList == NULL) goto _exit;
156!
1308
  if (metaDropSTable(pVnode->pMeta, ver, &req, tbUidList) < 0) {
156✔
1309
    rcode = terrno;
9✔
1310
    goto _exit;
9✔
1311
  }
1312

1313
  if (tqUpdateTbUidList(pVnode->pTq, tbUidList, false) < 0) {
147!
1314
    rcode = terrno;
×
1315
    goto _exit;
×
1316
  }
1317

1318
  if (tdProcessRSmaDrop(pVnode->pSma, &req) < 0) {
147!
1319
    rcode = terrno;
×
1320
    goto _exit;
×
1321
  }
1322

1323
  // return rsp
1324
_exit:
147✔
1325
  if (tbUidList) taosArrayDestroy(tbUidList);
156!
1326
  pRsp->code = rcode;
156✔
1327
  tDecoderClear(&decoder);
156✔
1328
  return 0;
156✔
1329
}
1330

1331
static int32_t vnodeProcessAlterTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
671✔
1332
  SVAlterTbReq  vAlterTbReq = {0};
671✔
1333
  SVAlterTbRsp  vAlterTbRsp = {0};
671✔
1334
  SDecoder      dc = {0};
671✔
1335
  int32_t       rcode = 0;
671✔
1336
  int32_t       ret;
1337
  SEncoder      ec = {0};
671✔
1338
  STableMetaRsp vMetaRsp = {0};
671✔
1339

1340
  pRsp->msgType = TDMT_VND_ALTER_TABLE_RSP;
671✔
1341
  pRsp->pCont = NULL;
671✔
1342
  pRsp->contLen = 0;
671✔
1343
  pRsp->code = TSDB_CODE_SUCCESS;
671✔
1344

1345
  tDecoderInit(&dc, pReq, len);
671✔
1346

1347
  // decode
1348
  if (tDecodeSVAlterTbReq(&dc, &vAlterTbReq) < 0) {
671!
1349
    vAlterTbRsp.code = TSDB_CODE_INVALID_MSG;
×
1350
    tDecoderClear(&dc);
×
1351
    rcode = -1;
×
1352
    goto _exit;
×
1353
  }
1354

1355
  // process
1356
  if (metaAlterTable(pVnode->pMeta, ver, &vAlterTbReq, &vMetaRsp) < 0) {
671✔
1357
    vAlterTbRsp.code = terrno;
3✔
1358
    tDecoderClear(&dc);
3✔
1359
    rcode = -1;
3✔
1360
    goto _exit;
3✔
1361
  }
1362
  tDecoderClear(&dc);
668✔
1363

1364
  if (NULL != vMetaRsp.pSchemas) {
668✔
1365
    vnodeUpdateMetaRsp(pVnode, &vMetaRsp);
74✔
1366
    vAlterTbRsp.pMeta = &vMetaRsp;
74✔
1367
  }
1368

1369
_exit:
594✔
1370
  tEncodeSize(tEncodeSVAlterTbRsp, &vAlterTbRsp, pRsp->contLen, ret);
671!
1371
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
671✔
1372
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
671✔
1373
  if (tEncodeSVAlterTbRsp(&ec, &vAlterTbRsp) != 0) {
671!
1374
    vError("vgId:%d, failed to encode alter table response", TD_VID(pVnode));
×
1375
  }
1376
  tEncoderClear(&ec);
671✔
1377
  if (vMetaRsp.pSchemas) {
671✔
1378
    taosMemoryFree(vMetaRsp.pSchemas);
74✔
1379
    taosMemoryFree(vMetaRsp.pSchemaExt);
74✔
1380
  }
1381
  return 0;
671✔
1382
}
1383

1384
static int32_t vnodeProcessDropTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
203✔
1385
                                     SRpcMsg *pOriginRpc) {
1386
  SVDropTbBatchReq req = {0};
203✔
1387
  SVDropTbBatchRsp rsp = {0};
203✔
1388
  SDecoder         decoder = {0};
203✔
1389
  SEncoder         encoder = {0};
203✔
1390
  int32_t          ret;
1391
  SArray          *tbUids = NULL;
203✔
1392
  STbUidStore     *pStore = NULL;
203✔
1393
  SArray          *tbNames = NULL;
203✔
1394

1395
  pRsp->msgType = TDMT_VND_DROP_TABLE_RSP;
203✔
1396
  pRsp->pCont = NULL;
203✔
1397
  pRsp->contLen = 0;
203✔
1398
  pRsp->code = TSDB_CODE_SUCCESS;
203✔
1399

1400
  // decode req
1401
  tDecoderInit(&decoder, pReq, len);
203✔
1402
  ret = tDecodeSVDropTbBatchReq(&decoder, &req);
203✔
1403
  if (ret < 0) {
203!
1404
    terrno = TSDB_CODE_INVALID_MSG;
×
1405
    pRsp->code = terrno;
×
1406
    goto _exit;
×
1407
  }
1408

1409
  // process req
1410
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
203✔
1411
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(SVDropTbRsp));
203✔
1412
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
203✔
1413
  if (tbUids == NULL || rsp.pArray == NULL || tbNames == NULL) goto _exit;
203!
1414

1415
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
406✔
1416
    SVDropTbReq *pDropTbReq = req.pReqs + iReq;
203✔
1417
    SVDropTbRsp  dropTbRsp = {0};
203✔
1418
    tb_uid_t     tbUid = 0;
203✔
1419

1420
    /* code */
1421
    ret = metaDropTable(pVnode->pMeta, ver, pDropTbReq, tbUids, &tbUid);
203✔
1422
    if (ret < 0) {
203!
1423
      if (pDropTbReq->igNotExists && terrno == TSDB_CODE_TDB_TABLE_NOT_EXIST) {
×
1424
        dropTbRsp.code = TSDB_CODE_SUCCESS;
×
1425
      } else {
1426
        dropTbRsp.code = terrno;
×
1427
      }
1428
    } else {
1429
      dropTbRsp.code = TSDB_CODE_SUCCESS;
203✔
1430
      if (tbUid > 0) {
203✔
1431
        if (tdFetchTbUidList(pVnode->pSma, &pStore, pDropTbReq->suid, tbUid) < 0) {
83!
1432
          vError("vgId:%d, failed to fetch tbUid list", TD_VID(pVnode));
×
1433
        }
1434
      }
1435
    }
1436

1437
    if (taosArrayPush(rsp.pArray, &dropTbRsp) == NULL) {
406!
1438
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1439
      pRsp->code = terrno;
×
1440
      goto _exit;
×
1441
    }
1442

1443
    if (tsEnableAuditCreateTable) {
203!
1444
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
203✔
1445
      if (str == NULL) {
203!
1446
        pRsp->code = terrno;
×
1447
        goto _exit;
×
1448
      }
1449
      strcpy(str, pDropTbReq->name);
203✔
1450
      if (taosArrayPush(tbNames, &str) == NULL) {
203!
1451
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1452
        pRsp->code = terrno;
×
1453
        goto _exit;
×
1454
      }
1455
    }
1456
  }
1457

1458
  if (tqUpdateTbUidList(pVnode->pTq, tbUids, false) < 0) {
203!
1459
    vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(terrno));
×
1460
  }
1461

1462
  if (tdUpdateTbUidList(pVnode->pSma, pStore, false) < 0) {
203!
1463
    goto _exit;
×
1464
  }
1465

1466
  if (tsEnableAuditCreateTable) {
203!
1467
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
203✔
1468

1469
    SName name = {0};
203✔
1470
    if (tNameFromString(&name, pVnode->config.dbname, T_NAME_ACCT | T_NAME_DB) != 0) {
203!
1471
      vError("vgId:%d, failed to get name from string", TD_VID(pVnode));
×
1472
    }
1473

1474
    SStringBuilder sb = {0};
203✔
1475
    for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
406✔
1476
      char **key = (char **)taosArrayGet(tbNames, iReq);
203✔
1477
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
203✔
1478
      if (iReq < req.nReqs - 1) {
203!
1479
        taosStringBuilderAppendChar(&sb, ',');
×
1480
      }
1481
      taosMemoryFreeClear(*key);
203!
1482
    }
1483

1484
    size_t len = 0;
203✔
1485
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
203✔
1486

1487
    if (pOriginRpc->info.conn.user != NULL && strlen(pOriginRpc->info.conn.user) > 0) {
203!
1488
      auditAddRecord(pOriginRpc, clusterId, "dropTable", name.dbname, "", keyJoined, len);
203✔
1489
    }
1490

1491
    taosStringBuilderDestroy(&sb);
203✔
1492
  }
1493

1494
_exit:
×
1495
  taosArrayDestroy(tbUids);
203✔
1496
  pStore = tdUidStoreFree(pStore);
203✔
1497
  tDecoderClear(&decoder);
203✔
1498
  tEncodeSize(tEncodeSVDropTbBatchRsp, &rsp, pRsp->contLen, ret);
203!
1499
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
203✔
1500
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
203✔
1501
  if (tEncodeSVDropTbBatchRsp(&encoder, &rsp) != 0) {
203!
1502
    vError("vgId:%d, failed to encode drop table batch response", TD_VID(pVnode));
×
1503
  }
1504
  tEncoderClear(&encoder);
203✔
1505
  taosArrayDestroy(rsp.pArray);
203✔
1506
  taosArrayDestroy(tbNames);
203✔
1507
  return 0;
203✔
1508
}
1509

1510
#ifdef BUILD_NO_CALL
1511
static int32_t vnodeDebugPrintSingleSubmitMsg(SMeta *pMeta, SSubmitBlk *pBlock, SSubmitMsgIter *msgIter,
1512
                                              const char *tags) {
1513
  SSubmitBlkIter blkIter = {0};
1514
  STSchema      *pSchema = NULL;
1515
  tb_uid_t       suid = 0;
1516
  STSRow        *row = NULL;
1517
  int32_t        rv = -1;
1518

1519
  tInitSubmitBlkIter(msgIter, pBlock, &blkIter);
1520
  if (blkIter.row == NULL) return 0;
1521

1522
  int32_t code = metaGetTbTSchemaNotNull(pMeta, msgIter->suid, TD_ROW_SVER(blkIter.row), 1,
1523
                                         &pSchema);  // TODO: use the real schema
1524
  if (TSDB_CODE_SUCCESS != code) {
1525
    printf("%s:%d no valid schema\n", tags, __LINE__);
1526
    return code;
1527
  }
1528

1529
  suid = msgIter->suid;
1530
  rv = TD_ROW_SVER(blkIter.row);
1531

1532
  char __tags[128] = {0};
1533
  snprintf(__tags, 128, "%s: uid %" PRIi64 " ", tags, msgIter->uid);
1534
  while ((row = tGetSubmitBlkNext(&blkIter))) {
1535
    tdSRowPrint(row, pSchema, __tags);
1536
  }
1537

1538
  taosMemoryFreeClear(pSchema);
1539

1540
  return TSDB_CODE_SUCCESS;
1541
}
1542
#endif
1543
typedef struct SSubmitReqConvertCxt {
1544
  SSubmitMsgIter msgIter;
1545
  SSubmitBlk    *pBlock;
1546
  SSubmitBlkIter blkIter;
1547
  STSRow        *pRow;
1548
  STSRowIter     rowIter;
1549
  SSubmitTbData *pTbData;
1550
  STSchema      *pTbSchema;
1551
  SArray        *pColValues;
1552
} SSubmitReqConvertCxt;
1553

1554
static int32_t vnodeResetTableCxt(SMeta *pMeta, SSubmitReqConvertCxt *pCxt) {
×
1555
  taosMemoryFreeClear(pCxt->pTbSchema);
×
1556
  int32_t code = metaGetTbTSchemaNotNull(pMeta, pCxt->msgIter.suid > 0 ? pCxt->msgIter.suid : pCxt->msgIter.uid,
×
1557
                                         pCxt->msgIter.sversion, 1, &pCxt->pTbSchema);
1558
  if (TSDB_CODE_SUCCESS != code) {
×
1559
    return code;
×
1560
  }
1561
  tdSTSRowIterInit(&pCxt->rowIter, pCxt->pTbSchema);
×
1562

1563
  tDestroySubmitTbData(pCxt->pTbData, TSDB_MSG_FLG_ENCODE);
×
1564
  if (NULL == pCxt->pTbData) {
×
1565
    pCxt->pTbData = taosMemoryCalloc(1, sizeof(SSubmitTbData));
×
1566
    if (NULL == pCxt->pTbData) {
×
1567
      return terrno;
×
1568
    }
1569
  }
1570
  pCxt->pTbData->flags = 0;
×
1571
  pCxt->pTbData->suid = pCxt->msgIter.suid;
×
1572
  pCxt->pTbData->uid = pCxt->msgIter.uid;
×
1573
  pCxt->pTbData->sver = pCxt->msgIter.sversion;
×
1574
  pCxt->pTbData->pCreateTbReq = NULL;
×
1575
  pCxt->pTbData->aRowP = taosArrayInit(128, POINTER_BYTES);
×
1576
  if (NULL == pCxt->pTbData->aRowP) {
×
1577
    return terrno;
×
1578
  }
1579

1580
  taosArrayDestroy(pCxt->pColValues);
×
1581
  pCxt->pColValues = taosArrayInit(pCxt->pTbSchema->numOfCols, sizeof(SColVal));
×
1582
  if (NULL == pCxt->pColValues) {
×
1583
    return terrno;
×
1584
  }
1585
  for (int32_t i = 0; i < pCxt->pTbSchema->numOfCols; ++i) {
×
1586
    SColVal val = COL_VAL_NONE(pCxt->pTbSchema->columns[i].colId, pCxt->pTbSchema->columns[i].type);
×
1587
    if (taosArrayPush(pCxt->pColValues, &val) == NULL) {
×
1588
      return terrno;
×
1589
    }
1590
  }
1591

1592
  return TSDB_CODE_SUCCESS;
×
1593
}
1594

1595
static void vnodeDestroySubmitReqConvertCxt(SSubmitReqConvertCxt *pCxt) {
×
1596
  taosMemoryFreeClear(pCxt->pTbSchema);
×
1597
  tDestroySubmitTbData(pCxt->pTbData, TSDB_MSG_FLG_ENCODE);
×
1598
  taosMemoryFreeClear(pCxt->pTbData);
×
1599
  taosArrayDestroy(pCxt->pColValues);
×
1600
}
×
1601

1602
static int32_t vnodeCellValConvertToColVal(STColumn *pCol, SCellVal *pCellVal, SColVal *pColVal) {
×
1603
  if (tdValTypeIsNone(pCellVal->valType)) {
×
1604
    pColVal->flag = CV_FLAG_NONE;
×
1605
    return TSDB_CODE_SUCCESS;
×
1606
  }
1607

1608
  if (tdValTypeIsNull(pCellVal->valType)) {
×
1609
    pColVal->flag = CV_FLAG_NULL;
×
1610
    return TSDB_CODE_SUCCESS;
×
1611
  }
1612

1613
  if (IS_VAR_DATA_TYPE(pCol->type)) {
×
1614
    pColVal->value.nData = varDataLen(pCellVal->val);
×
1615
    pColVal->value.pData = (uint8_t *)varDataVal(pCellVal->val);
×
1616
  } else if (TSDB_DATA_TYPE_FLOAT == pCol->type) {
×
1617
    float f = GET_FLOAT_VAL(pCellVal->val);
×
1618
    memcpy(&pColVal->value.val, &f, sizeof(f));
×
1619
  } else if (TSDB_DATA_TYPE_DOUBLE == pCol->type) {
×
1620
    pColVal->value.val = *(int64_t *)pCellVal->val;
×
1621
  } else {
1622
    GET_TYPED_DATA(pColVal->value.val, int64_t, pCol->type, pCellVal->val);
×
1623
  }
1624

1625
  pColVal->flag = CV_FLAG_VALUE;
×
1626
  return TSDB_CODE_SUCCESS;
×
1627
}
1628

1629
static int32_t vnodeTSRowConvertToColValArray(SSubmitReqConvertCxt *pCxt) {
×
1630
  int32_t code = TSDB_CODE_SUCCESS;
×
1631
  tdSTSRowIterReset(&pCxt->rowIter, pCxt->pRow);
×
1632
  for (int32_t i = 0; TSDB_CODE_SUCCESS == code && i < pCxt->pTbSchema->numOfCols; ++i) {
×
1633
    STColumn *pCol = pCxt->pTbSchema->columns + i;
×
1634
    SCellVal  cellVal = {0};
×
1635
    if (!tdSTSRowIterFetch(&pCxt->rowIter, pCol->colId, pCol->type, &cellVal)) {
×
1636
      break;
×
1637
    }
1638
    code = vnodeCellValConvertToColVal(pCol, &cellVal, (SColVal *)taosArrayGet(pCxt->pColValues, i));
×
1639
  }
1640
  return code;
×
1641
}
1642

1643
static int32_t vnodeDecodeCreateTbReq(SSubmitReqConvertCxt *pCxt) {
×
1644
  if (pCxt->msgIter.schemaLen <= 0) {
×
1645
    return TSDB_CODE_SUCCESS;
×
1646
  }
1647

1648
  pCxt->pTbData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
×
1649
  if (NULL == pCxt->pTbData->pCreateTbReq) {
×
1650
    return terrno;
×
1651
  }
1652

1653
  SDecoder decoder = {0};
×
1654
  tDecoderInit(&decoder, (uint8_t *)pCxt->pBlock->data, pCxt->msgIter.schemaLen);
×
1655
  int32_t code = tDecodeSVCreateTbReq(&decoder, pCxt->pTbData->pCreateTbReq);
×
1656
  tDecoderClear(&decoder);
×
1657

1658
  return code;
×
1659
}
1660

1661
static int32_t vnodeSubmitReqConvertToSubmitReq2(SVnode *pVnode, SSubmitReq *pReq, SSubmitReq2 *pReq2) {
×
1662
  pReq2->aSubmitTbData = taosArrayInit(128, sizeof(SSubmitTbData));
×
1663
  if (NULL == pReq2->aSubmitTbData) {
×
1664
    return terrno;
×
1665
  }
1666

1667
  SSubmitReqConvertCxt cxt = {0};
×
1668

1669
  int32_t code = tInitSubmitMsgIter(pReq, &cxt.msgIter);
×
1670
  while (TSDB_CODE_SUCCESS == code) {
×
1671
    code = tGetSubmitMsgNext(&cxt.msgIter, &cxt.pBlock);
×
1672
    if (TSDB_CODE_SUCCESS == code) {
×
1673
      if (NULL == cxt.pBlock) {
×
1674
        break;
×
1675
      }
1676
      code = vnodeResetTableCxt(pVnode->pMeta, &cxt);
×
1677
    }
1678
    if (TSDB_CODE_SUCCESS == code) {
×
1679
      code = tInitSubmitBlkIter(&cxt.msgIter, cxt.pBlock, &cxt.blkIter);
×
1680
    }
1681
    if (TSDB_CODE_SUCCESS == code) {
×
1682
      code = vnodeDecodeCreateTbReq(&cxt);
×
1683
    }
1684
    while (TSDB_CODE_SUCCESS == code && (cxt.pRow = tGetSubmitBlkNext(&cxt.blkIter)) != NULL) {
×
1685
      code = vnodeTSRowConvertToColValArray(&cxt);
×
1686
      if (TSDB_CODE_SUCCESS == code) {
×
1687
        SRow **pNewRow = taosArrayReserve(cxt.pTbData->aRowP, 1);
×
1688
        code = tRowBuild(cxt.pColValues, cxt.pTbSchema, pNewRow);
×
1689
      }
1690
    }
1691
    if (TSDB_CODE_SUCCESS == code) {
×
1692
      code = (NULL == taosArrayPush(pReq2->aSubmitTbData, cxt.pTbData) ? terrno : TSDB_CODE_SUCCESS);
×
1693
    }
1694
    if (TSDB_CODE_SUCCESS == code) {
×
1695
      taosMemoryFreeClear(cxt.pTbData);
×
1696
    }
1697
  }
1698

1699
  vnodeDestroySubmitReqConvertCxt(&cxt);
×
1700
  return code;
×
1701
}
1702

1703
static int32_t vnodeRebuildSubmitReqMsg(SSubmitReq2 *pSubmitReq, void **ppMsg) {
×
1704
  int32_t  code = TSDB_CODE_SUCCESS;
×
1705
  char    *pMsg = NULL;
×
1706
  uint32_t msglen = 0;
×
1707
  tEncodeSize(tEncodeSubmitReq, pSubmitReq, msglen, code);
×
1708
  if (TSDB_CODE_SUCCESS == code) {
×
1709
    pMsg = taosMemoryMalloc(msglen);
×
1710
    if (NULL == pMsg) {
×
1711
      code = terrno;
×
1712
    }
1713
  }
1714
  if (TSDB_CODE_SUCCESS == code) {
×
1715
    SEncoder encoder;
1716
    tEncoderInit(&encoder, (uint8_t *)pMsg, msglen);
×
1717
    code = tEncodeSubmitReq(&encoder, pSubmitReq);
×
1718
    tEncoderClear(&encoder);
×
1719
  }
1720
  if (TSDB_CODE_SUCCESS == code) {
×
1721
    *ppMsg = pMsg;
×
1722
  }
1723
  return code;
×
1724
}
1725

1726
static int32_t vnodeProcessSubmitReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
11,628,439✔
1727
                                     SRpcMsg *pOriginalMsg) {
1728
  int32_t code = 0;
11,628,439✔
1729
  terrno = 0;
11,628,439✔
1730

1731
  SSubmitReq2 *pSubmitReq = &(SSubmitReq2){0};
11,628,558✔
1732
  SSubmitRsp2 *pSubmitRsp = &(SSubmitRsp2){0};
11,628,558✔
1733
  SArray      *newTbUids = NULL;
11,628,558✔
1734
  int32_t      ret;
1735
  SEncoder     ec = {0};
11,628,558✔
1736

1737
  pRsp->code = TSDB_CODE_SUCCESS;
11,628,558✔
1738

1739
  void           *pAllocMsg = NULL;
11,628,558✔
1740
  SSubmitReq2Msg *pMsg = (SSubmitReq2Msg *)pReq;
11,628,558✔
1741
  if (0 == pMsg->version) {
11,628,558!
1742
    code = vnodeSubmitReqConvertToSubmitReq2(pVnode, (SSubmitReq *)pMsg, pSubmitReq);
×
1743
    if (TSDB_CODE_SUCCESS == code) {
×
1744
      code = vnodeRebuildSubmitReqMsg(pSubmitReq, &pReq);
×
1745
    }
UNCOV
1746
    if (TSDB_CODE_SUCCESS == code) {
×
1747
      pAllocMsg = pReq;
×
1748
    }
UNCOV
1749
    if (TSDB_CODE_SUCCESS != code) {
×
1750
      goto _exit;
×
1751
    }
1752
  } else {
1753
    // decode
1754
    pReq = POINTER_SHIFT(pReq, sizeof(SSubmitReq2Msg));
11,628,558✔
1755
    len -= sizeof(SSubmitReq2Msg);
11,628,558✔
1756
    SDecoder dc = {0};
11,628,558✔
1757
    tDecoderInit(&dc, pReq, len);
11,628,558✔
1758
    if (tDecodeSubmitReq(&dc, pSubmitReq) < 0) {
11,628,442!
1759
      code = TSDB_CODE_INVALID_MSG;
×
1760
      goto _exit;
×
1761
    }
1762
    tDecoderClear(&dc);
11,628,518✔
1763
  }
1764

1765
  // scan
1766
  TSKEY now = taosGetTimestamp(pVnode->config.tsdbCfg.precision);
11,628,586✔
1767
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * pVnode->config.tsdbCfg.keep2;
11,628,460✔
1768
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
11,628,460✔
1769
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
23,371,072✔
1770
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
11,741,816✔
1771

1772
    if (pSubmitTbData->pCreateTbReq && pSubmitTbData->pCreateTbReq->uid == 0) {
11,741,851!
1773
      code = TSDB_CODE_INVALID_MSG;
×
1774
      goto _exit;
×
1775
    }
1776

1777
    if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
11,741,851✔
1778
      if (TARRAY_SIZE(pSubmitTbData->aCol) <= 0) {
1!
1779
        code = TSDB_CODE_INVALID_MSG;
×
1780
        goto _exit;
×
1781
      }
1782

1783
      SColData *colDataArr = TARRAY_DATA(pSubmitTbData->aCol);
1✔
1784
      SRowKey   lastKey;
1785
      tColDataArrGetRowKey(colDataArr, TARRAY_SIZE(pSubmitTbData->aCol), 0, &lastKey);
1✔
1786
      for (int32_t iRow = 1; iRow < colDataArr[0].nVal; iRow++) {
1!
1787
        SRowKey key;
1788
        tColDataArrGetRowKey(TARRAY_DATA(pSubmitTbData->aCol), TARRAY_SIZE(pSubmitTbData->aCol), iRow, &key);
×
1789
        if (tRowKeyCompare(&lastKey, &key) >= 0) {
×
1790
          code = TSDB_CODE_INVALID_MSG;
×
1791
          vError("vgId:%d %s failed 1 since %s, version:%" PRId64, TD_VID(pVnode), __func__, tstrerror(terrno), ver);
×
1792
          goto _exit;
×
1793
        }
1794
      }
1795
    } else {
1796
      int32_t nRow = TARRAY_SIZE(pSubmitTbData->aRowP);
11,741,850✔
1797
      SRow  **aRow = (SRow **)TARRAY_DATA(pSubmitTbData->aRowP);
11,741,850✔
1798
      SRowKey lastRowKey;
1799
      for (int32_t iRow = 0; iRow < nRow; ++iRow) {
279,231,190✔
1800
        if (aRow[iRow]->ts < minKey || aRow[iRow]->ts > maxKey) {
267,488,579✔
1801
          code = TSDB_CODE_INVALID_MSG;
17,173✔
1802
          vError("vgId:%d %s failed 2 since %s, version:%" PRId64, TD_VID(pVnode), __func__, tstrerror(code), ver);
17,173!
1803
          goto _exit;
×
1804
        }
1805
        if (iRow == 0) {
267,471,406✔
1806
          tRowGetKey(aRow[iRow], &lastRowKey);
11,741,827✔
1807
        } else {
1808
          SRowKey rowKey;
1809
          tRowGetKey(aRow[iRow], &rowKey);
255,729,579✔
1810

1811
          if (tRowKeyCompare(&lastRowKey, &rowKey) >= 0) {
255,718,074!
1812
            code = TSDB_CODE_INVALID_MSG;
×
1813
            vError("vgId:%d %s failed 3 since %s, version:%" PRId64, TD_VID(pVnode), __func__, tstrerror(code), ver);
×
1814
            goto _exit;
×
1815
          }
1816
          lastRowKey = rowKey;
255,744,906✔
1817
        }
1818
      }
1819
    }
1820
  }
1821

1822
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
23,371,255✔
1823
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
11,741,803✔
1824

1825
    if (pSubmitTbData->pCreateTbReq) {
11,741,785✔
1826
      pSubmitTbData->uid = pSubmitTbData->pCreateTbReq->uid;
52,065✔
1827
    } else {
1828
      SMetaInfo info = {0};
11,689,720✔
1829

1830
      code = metaGetInfo(pVnode->pMeta, pSubmitTbData->uid, &info, NULL);
11,689,720✔
1831
      if (code) {
11,689,850✔
1832
        code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
12✔
1833
        vWarn("vgId:%d, table uid:%" PRId64 " not exists", TD_VID(pVnode), pSubmitTbData->uid);
12!
1834
        goto _exit;
×
1835
      }
1836

1837
      if (info.suid != pSubmitTbData->suid) {
11,689,838!
1838
        code = TSDB_CODE_INVALID_MSG;
×
1839
        goto _exit;
×
1840
      }
1841

1842
      if (info.suid) {
11,689,838✔
1843
        if (metaGetInfo(pVnode->pMeta, info.suid, &info, NULL) != 0) {
11,026,067!
1844
          vWarn("vgId:%d, table uid:%" PRId64 " not exists", TD_VID(pVnode), info.suid);
×
1845
        }
1846
      }
1847

1848
      if (pSubmitTbData->sver != info.skmVer) {
11,689,934!
1849
        code = TSDB_CODE_TDB_INVALID_TABLE_SCHEMA_VER;
×
1850
        goto _exit;
×
1851
      }
1852
    }
1853

1854
    if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
11,741,999✔
1855
      int32_t   nColData = TARRAY_SIZE(pSubmitTbData->aCol);
1✔
1856
      SColData *aColData = (SColData *)TARRAY_DATA(pSubmitTbData->aCol);
1✔
1857

1858
      if (nColData <= 0) {
1!
1859
        code = TSDB_CODE_INVALID_MSG;
×
1860
        goto _exit;
×
1861
      }
1862

1863
      if (aColData[0].cid != PRIMARYKEY_TIMESTAMP_COL_ID || aColData[0].type != TSDB_DATA_TYPE_TIMESTAMP ||
1!
1864
          aColData[0].nVal <= 0) {
1!
1865
        code = TSDB_CODE_INVALID_MSG;
×
1866
        goto _exit;
×
1867
      }
1868

1869
      for (int32_t j = 1; j < nColData; j++) {
4✔
1870
        if (aColData[j].nVal != aColData[0].nVal) {
3!
1871
          code = TSDB_CODE_INVALID_MSG;
×
1872
          goto _exit;
×
1873
        }
1874
      }
1875
    }
1876
  }
1877

1878
  vDebug("vgId:%d, submit block size %d", TD_VID(pVnode), (int32_t)taosArrayGetSize(pSubmitReq->aSubmitTbData));
11,629,452✔
1879

1880
  // loop to handle
1881
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
23,370,565✔
1882
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
11,741,910✔
1883

1884
    // create table
1885
    if (pSubmitTbData->pCreateTbReq) {
11,741,929✔
1886
      // alloc if need
1887
      if (pSubmitRsp->aCreateTbRsp == NULL &&
52,065✔
1888
          (pSubmitRsp->aCreateTbRsp = taosArrayInit(TARRAY_SIZE(pSubmitReq->aSubmitTbData), sizeof(SVCreateTbRsp))) ==
36,787!
1889
              NULL) {
1890
        code = terrno;
×
1891
        goto _exit;
×
1892
      }
1893

1894
      SVCreateTbRsp *pCreateTbRsp = taosArrayReserve(pSubmitRsp->aCreateTbRsp, 1);
52,065✔
1895

1896
      // create table
1897
      if (metaCreateTable(pVnode->pMeta, ver, pSubmitTbData->pCreateTbReq, &pCreateTbRsp->pMeta) == 0) {
52,065✔
1898
        // create table success
1899

1900
        if (newTbUids == NULL &&
58,652!
1901
            (newTbUids = taosArrayInit(TARRAY_SIZE(pSubmitReq->aSubmitTbData), sizeof(int64_t))) == NULL) {
21,688✔
1902
          code = terrno;
×
1903
          goto _exit;
×
1904
        }
1905

1906
        if (taosArrayPush(newTbUids, &pSubmitTbData->uid) == NULL) {
73,928!
1907
          code = terrno;
×
1908
          goto _exit;
×
1909
        }
1910

1911
        if (pCreateTbRsp->pMeta) {
36,964!
1912
          vnodeUpdateMetaRsp(pVnode, pCreateTbRsp->pMeta);
36,964✔
1913
        }
1914
      } else {  // create table failed
1915
        if (terrno != TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
15,101!
1916
          code = terrno;
×
1917
          vError("vgId:%d failed to create table:%s, code:%s", TD_VID(pVnode), pSubmitTbData->pCreateTbReq->name,
×
1918
                 tstrerror(terrno));
1919
          goto _exit;
×
1920
        }
1921
        terrno = 0;
15,101✔
1922
        pSubmitTbData->uid = pSubmitTbData->pCreateTbReq->uid;  // update uid if table exist for using below
15,101✔
1923
      }
1924
    }
1925

1926
    // insert data
1927
    int32_t affectedRows;
1928
    code = tsdbInsertTableData(pVnode->pTsdb, ver, pSubmitTbData, &affectedRows);
11,741,929✔
1929
    if (code) goto _exit;
11,741,963!
1930

1931
    code = metaUpdateChangeTimeWithLock(pVnode->pMeta, pSubmitTbData->uid, pSubmitTbData->ctimeMs);
11,741,963✔
1932
    if (code) goto _exit;
11,741,956!
1933

1934
    pSubmitRsp->affectedRows += affectedRows;
11,741,956✔
1935
  }
1936

1937
  // update the affected table uid list
1938
  if (taosArrayGetSize(newTbUids) > 0) {
11,628,655✔
1939
    vDebug("vgId:%d, add %d table into query table list in handling submit", TD_VID(pVnode),
21,688✔
1940
           (int32_t)taosArrayGetSize(newTbUids));
1941
    if (tqUpdateTbUidList(pVnode->pTq, newTbUids, true) != 0) {
21,688!
1942
      vError("vgId:%d, failed to update tbUid list", TD_VID(pVnode));
×
1943
    }
1944
  }
1945

1946
_exit:
11,628,566✔
1947
  // message
1948
  pRsp->code = code;
11,628,566✔
1949
  tEncodeSize(tEncodeSSubmitRsp2, pSubmitRsp, pRsp->contLen, ret);
11,628,566!
1950
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
11,628,526✔
1951
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
11,628,541✔
1952
  if (tEncodeSSubmitRsp2(&ec, pSubmitRsp) < 0) {
11,628,552!
1953
    vError("vgId:%d, failed to encode submit response", TD_VID(pVnode));
×
1954
  }
1955
  tEncoderClear(&ec);
11,628,548✔
1956

1957
  // update statistics
1958
  (void)atomic_add_fetch_64(&pVnode->statis.nInsert, pSubmitRsp->affectedRows);
11,628,553✔
1959
  (void)atomic_add_fetch_64(&pVnode->statis.nInsertSuccess, pSubmitRsp->affectedRows);
11,628,622✔
1960
  (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsert, 1);
11,628,619✔
1961

1962
  if (tsEnableMonitor && tsMonitorFqdn[0] != 0 && tsMonitorPort != 0 && pSubmitRsp->affectedRows > 0 &&
11,628,620!
1963
      strlen(pOriginalMsg->info.conn.user) > 0 && tsInsertCounter != NULL) {
×
1964
    const char *sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT_AFFECTED_ROWS,
×
1965
                                   pVnode->monitor.strClusterId,
×
1966
                                   pVnode->monitor.strDnodeId,
×
1967
                                   tsLocalEp,
1968
                                   pVnode->monitor.strVgId,
×
1969
                                   pOriginalMsg->info.conn.user,
×
1970
                                   "Success"};
1971
    int         tv = taos_counter_add(tsInsertCounter, pSubmitRsp->affectedRows, sample_labels);
×
1972
  }
1973

1974
  if (code == 0) {
11,628,620✔
1975
    (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
11,628,602✔
1976
    code = tdProcessRSmaSubmit(pVnode->pSma, ver, pSubmitReq, pReq, len);
11,628,621✔
1977
  }
1978
  /*
1979
  if (code == 0) {
1980
    atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
1981
    code = tdProcessRSmaSubmit(pVnode->pSma, ver, pSubmitReq, pReq, len);
1982

1983
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT, pVnode->monitor.strClusterId,
1984
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
1985
                                          pOriginalMsg->info.conn.user, "Success"};
1986
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
1987
  }
1988
  else{
1989
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT, pVnode->monitor.strClusterId,
1990
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
1991
                                        pOriginalMsg->info.conn.user, "Failed"};
1992
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
1993
  }
1994
  */
1995

1996
  // clear
1997
  taosArrayDestroy(newTbUids);
11,628,577✔
1998
  tDestroySubmitReq(pSubmitReq, 0 == pMsg->version ? TSDB_MSG_FLG_CMPT : TSDB_MSG_FLG_DECODE);
11,628,527!
1999
  tDestroySSubmitRsp2(pSubmitRsp, TSDB_MSG_FLG_ENCODE);
11,628,566✔
2000

2001
  if (code) terrno = code;
11,628,489!
2002

2003
  taosMemoryFree(pAllocMsg);
11,628,489✔
2004

2005
  return code;
11,628,504✔
2006
}
2007

2008
static int32_t vnodeProcessCreateTSmaReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
24✔
2009
  SVCreateTSmaReq req = {0};
24✔
2010
  SDecoder        coder = {0};
24✔
2011

2012
  if (pRsp) {
24!
2013
    pRsp->msgType = TDMT_VND_CREATE_SMA_RSP;
24✔
2014
    pRsp->code = TSDB_CODE_SUCCESS;
24✔
2015
    pRsp->pCont = NULL;
24✔
2016
    pRsp->contLen = 0;
24✔
2017
  }
2018

2019
  // decode and process req
2020
  tDecoderInit(&coder, pReq, len);
24✔
2021

2022
  if (tDecodeSVCreateTSmaReq(&coder, &req) < 0) {
24!
2023
    terrno = TSDB_CODE_MSG_DECODE_ERROR;
×
2024
    if (pRsp) pRsp->code = terrno;
×
2025
    goto _err;
×
2026
  }
2027

2028
  if (tdProcessTSmaCreate(pVnode->pSma, ver, (const char *)&req) < 0) {
24!
2029
    if (pRsp) pRsp->code = terrno;
×
2030
    goto _err;
×
2031
  }
2032

2033
  tDecoderClear(&coder);
24✔
2034
  vDebug("vgId:%d, success to create tsma %s:%" PRIi64 " version %" PRIi64 " for table %" PRIi64, TD_VID(pVnode),
24!
2035
         req.indexName, req.indexUid, ver, req.tableUid);
2036
  return 0;
24✔
2037

2038
_err:
×
2039
  tDecoderClear(&coder);
×
2040
  vError("vgId:%d, failed to create tsma %s:%" PRIi64 " version %" PRIi64 "for table %" PRIi64 " since %s",
×
2041
         TD_VID(pVnode), req.indexName, req.indexUid, ver, req.tableUid, terrstr());
2042
  return terrno;
×
2043
}
2044

2045
/**
2046
 * @brief specific for smaDstVnode
2047
 *
2048
 * @param pVnode
2049
 * @param pCont
2050
 * @param contLen
2051
 * @return int32_t
2052
 */
2053
int32_t vnodeProcessCreateTSma(SVnode *pVnode, void *pCont, uint32_t contLen) {
×
2054
  return vnodeProcessCreateTSmaReq(pVnode, 1, pCont, contLen, NULL);
×
2055
}
2056

2057
static int32_t vnodeConsolidateAlterHashRange(SVnode *pVnode, int64_t ver) {
16✔
2058
  int32_t code = TSDB_CODE_SUCCESS;
16✔
2059

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

2063
  // TODO: trim meta of tables from TDB per hash range [pVnode->config.hashBegin, pVnode->config.hashEnd]
2064
  code = metaTrimTables(pVnode->pMeta);
16✔
2065

2066
  return code;
16✔
2067
}
2068

2069
static int32_t vnodeProcessAlterConfirmReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
6,977✔
2070
  vInfo("vgId:%d, vnode handle msgType:alter-confirm, alter confirm msg is processed", TD_VID(pVnode));
6,977!
2071
  int32_t code = TSDB_CODE_SUCCESS;
6,977✔
2072
  if (!pVnode->config.hashChange) {
6,977✔
2073
    goto _exit;
6,961✔
2074
  }
2075

2076
  code = vnodeConsolidateAlterHashRange(pVnode, ver);
16✔
2077
  if (code < 0) {
16!
2078
    vError("vgId:%d, failed to consolidate alter hashrange since %s. version:%" PRId64, TD_VID(pVnode), terrstr(), ver);
×
2079
    goto _exit;
×
2080
  }
2081
  pVnode->config.hashChange = false;
16✔
2082

2083
_exit:
6,977✔
2084
  pRsp->msgType = TDMT_VND_ALTER_CONFIRM_RSP;
6,977✔
2085
  pRsp->code = code;
6,977✔
2086
  pRsp->pCont = NULL;
6,977✔
2087
  pRsp->contLen = 0;
6,977✔
2088

2089
  return code;
6,977✔
2090
}
2091

2092
extern int32_t tsdbDisableAndCancelAllBgTask(STsdb *pTsdb);
2093
extern void    tsdbEnableBgTask(STsdb *pTsdb);
2094

2095
static int32_t vnodeProcessAlterConfigReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
490✔
2096
  bool walChanged = false;
490✔
2097
  bool tsdbChanged = false;
490✔
2098

2099
  SAlterVnodeConfigReq req = {0};
490✔
2100
  if (tDeserializeSAlterVnodeConfigReq(pReq, len, &req) != 0) {
490!
2101
    terrno = TSDB_CODE_INVALID_MSG;
×
2102
    return TSDB_CODE_INVALID_MSG;
×
2103
  }
2104

2105
  vInfo("vgId:%d, start to alter vnode config, page:%d pageSize:%d buffer:%d szPage:%d szBuf:%" PRIu64
493✔
2106
        " cacheLast:%d cacheLastSize:%d days:%d keep0:%d keep1:%d keep2:%d keepTimeOffset:%d s3KeepLocal:%d "
2107
        "s3Compact:%d fsync:%d level:%d "
2108
        "walRetentionPeriod:%d walRetentionSize:%d",
2109
        TD_VID(pVnode), req.pages, req.pageSize, req.buffer, req.pageSize * 1024, (uint64_t)req.buffer * 1024 * 1024,
2110
        req.cacheLast, req.cacheLastSize, req.daysPerFile, req.daysToKeep0, req.daysToKeep1, req.daysToKeep2,
2111
        req.keepTimeOffset, req.s3KeepLocal, req.s3Compact, req.walFsyncPeriod, req.walLevel, req.walRetentionPeriod,
2112
        req.walRetentionSize);
2113

2114
  if (pVnode->config.cacheLastSize != req.cacheLastSize) {
497✔
2115
    pVnode->config.cacheLastSize = req.cacheLastSize;
242✔
2116
    tsdbCacheSetCapacity(pVnode, (size_t)pVnode->config.cacheLastSize * 1024 * 1024);
242✔
2117
  }
2118

2119
  if (pVnode->config.szBuf != req.buffer * 1024LL * 1024LL) {
497!
2120
    vInfo("vgId:%d, vnode buffer is changed from %" PRId64 " to %" PRId64, TD_VID(pVnode), pVnode->config.szBuf,
×
2121
          (uint64_t)(req.buffer * 1024LL * 1024LL));
2122
    pVnode->config.szBuf = req.buffer * 1024LL * 1024LL;
×
2123
  }
2124

2125
  if (pVnode->config.szCache != req.pages) {
497!
2126
    if (metaAlterCache(pVnode->pMeta, req.pages) < 0) {
×
2127
      vError("vgId:%d, failed to change vnode pages from %d to %d failed since %s", TD_VID(pVnode),
×
2128
             pVnode->config.szCache, req.pages, tstrerror(errno));
2129
      return errno;
×
2130
    } else {
2131
      vInfo("vgId:%d, vnode pages is changed from %d to %d", TD_VID(pVnode), pVnode->config.szCache, req.pages);
×
2132
      pVnode->config.szCache = req.pages;
×
2133
    }
2134
  }
2135

2136
  if (pVnode->config.cacheLast != req.cacheLast) {
497✔
2137
    pVnode->config.cacheLast = req.cacheLast;
331✔
2138
  }
2139

2140
  if (pVnode->config.walCfg.fsyncPeriod != req.walFsyncPeriod) {
497✔
2141
    pVnode->config.walCfg.fsyncPeriod = req.walFsyncPeriod;
62✔
2142
    walChanged = true;
62✔
2143
  }
2144

2145
  if (pVnode->config.walCfg.level != req.walLevel) {
497✔
2146
    if (pVnode->config.walCfg.level == 0) {
42!
2147
      pVnode->config.walCfg.clearFiles = 1;
×
2148
    }
2149
    pVnode->config.walCfg.level = req.walLevel;
42✔
2150
    walChanged = true;
42✔
2151
  }
2152

2153
  if (pVnode->config.walCfg.retentionPeriod != req.walRetentionPeriod) {
497✔
2154
    pVnode->config.walCfg.retentionPeriod = req.walRetentionPeriod;
4✔
2155
    walChanged = true;
4✔
2156
  }
2157

2158
  if (pVnode->config.walCfg.retentionSize != req.walRetentionSize) {
497!
2159
    pVnode->config.walCfg.retentionSize = req.walRetentionSize;
×
2160
    walChanged = true;
×
2161
  }
2162

2163
  if (pVnode->config.tsdbCfg.keep0 != req.daysToKeep0) {
497✔
2164
    pVnode->config.tsdbCfg.keep0 = req.daysToKeep0;
54✔
2165
    if (!VND_IS_RSMA(pVnode)) {
54!
2166
      tsdbChanged = true;
54✔
2167
    }
2168
  }
2169

2170
  if (pVnode->config.tsdbCfg.keep1 != req.daysToKeep1) {
497✔
2171
    pVnode->config.tsdbCfg.keep1 = req.daysToKeep1;
54✔
2172
    if (!VND_IS_RSMA(pVnode)) {
54!
2173
      tsdbChanged = true;
54✔
2174
    }
2175
  }
2176

2177
  if (pVnode->config.tsdbCfg.keep2 != req.daysToKeep2) {
497✔
2178
    pVnode->config.tsdbCfg.keep2 = req.daysToKeep2;
54✔
2179
    if (!VND_IS_RSMA(pVnode)) {
54!
2180
      tsdbChanged = true;
54✔
2181
    }
2182
  }
2183

2184
  if (pVnode->config.tsdbCfg.keepTimeOffset != req.keepTimeOffset) {
497!
2185
    pVnode->config.tsdbCfg.keepTimeOffset = req.keepTimeOffset;
×
2186
    if (!VND_IS_RSMA(pVnode)) {
×
2187
      tsdbChanged = true;
×
2188
    }
2189
  }
2190

2191
  if (req.sttTrigger != -1 && req.sttTrigger != pVnode->config.sttTrigger) {
497!
2192
    if (req.sttTrigger > 1 && pVnode->config.sttTrigger > 1) {
×
2193
      pVnode->config.sttTrigger = req.sttTrigger;
×
2194
    } else {
2195
      vnodeAWait(&pVnode->commitTask);
×
2196

2197
      int32_t ret = tsdbDisableAndCancelAllBgTask(pVnode->pTsdb);
×
2198
      if (ret != 0) {
×
2199
        vError("vgId:%d, failed to disable bg task since %s", TD_VID(pVnode), tstrerror(errno));
×
2200
      }
2201

2202
      pVnode->config.sttTrigger = req.sttTrigger;
×
2203
      tsdbEnableBgTask(pVnode->pTsdb);
×
2204
    }
2205
  }
2206

2207
  if (req.minRows != -1 && req.minRows != pVnode->config.tsdbCfg.minRows) {
497!
2208
    pVnode->config.tsdbCfg.minRows = req.minRows;
×
2209
  }
2210

2211
  if (req.s3KeepLocal != -1 && req.s3KeepLocal != pVnode->config.s3KeepLocal) {
497!
2212
    pVnode->config.s3KeepLocal = req.s3KeepLocal;
×
2213
  }
2214
  if (req.s3Compact != -1 && req.s3Compact != pVnode->config.s3Compact) {
497!
2215
    pVnode->config.s3Compact = req.s3Compact;
×
2216
  }
2217

2218
  if (walChanged) {
497✔
2219
    if (walAlter(pVnode->pWal, &pVnode->config.walCfg) != 0) {
106!
2220
      vError("vgId:%d, failed to alter wal config since %s", TD_VID(pVnode), tstrerror(errno));
×
2221
    }
2222
  }
2223

2224
  if (tsdbChanged) {
495✔
2225
    tsdbSetKeepCfg(pVnode->pTsdb, &pVnode->config.tsdbCfg);
56✔
2226
  }
2227

2228
  return 0;
495✔
2229
}
2230

2231
static int32_t vnodeProcessBatchDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
2,461✔
2232
  SBatchDeleteReq deleteReq;
2233
  SDecoder        decoder;
2234
  tDecoderInit(&decoder, pReq, len);
2,461✔
2235
  if (tDecodeSBatchDeleteReq(&decoder, &deleteReq) < 0) {
2,462!
2236
    tDecoderClear(&decoder);
×
2237
    return terrno = TSDB_CODE_INVALID_MSG;
×
2238
  }
2239

2240
  SMetaReader mr = {0};
2,467✔
2241
  metaReaderDoInit(&mr, pVnode->pMeta, META_READER_NOLOCK);
2,467✔
2242
  STsdb *pTsdb = pVnode->pTsdb;
2,464✔
2243

2244
  if (deleteReq.level) {
2,464✔
2245
    pTsdb = deleteReq.level == 1 ? VND_RSMA1(pVnode) : VND_RSMA2(pVnode);
16✔
2246
  }
2247

2248
  int32_t sz = taosArrayGetSize(deleteReq.deleteReqs);
2,464✔
2249
  for (int32_t i = 0; i < sz; i++) {
5,785✔
2250
    SSingleDeleteReq *pOneReq = taosArrayGet(deleteReq.deleteReqs, i);
3,321✔
2251
    char             *name = pOneReq->tbname;
3,320✔
2252
    if (metaGetTableEntryByName(&mr, name) < 0) {
3,320✔
2253
      vDebug("vgId:%d, stream delete msg, skip since no table: %s", pVnode->config.vgId, name);
1,741!
2254
      continue;
1,741✔
2255
    }
2256

2257
    int64_t uid = mr.me.uid;
1,584✔
2258

2259
    int32_t code = tsdbDeleteTableData(pTsdb, ver, deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
1,584✔
2260
    if (code < 0) {
1,584!
2261
      terrno = code;
×
2262
      vError("vgId:%d, delete error since %s, suid:%" PRId64 ", uid:%" PRId64 ", start ts:%" PRId64 ", end ts:%" PRId64,
×
2263
             TD_VID(pVnode), terrstr(), deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
2264
    }
2265

2266
    if (deleteReq.level == 0) {
1,584✔
2267
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, deleteReq.ctimeMs);
968✔
2268
      if (code < 0) {
968!
2269
        terrno = code;
×
2270
        vError("vgId:%d, update change time error since %s, suid:%" PRId64 ", uid:%" PRId64 ", start ts:%" PRId64
×
2271
               ", end ts:%" PRId64,
2272
               TD_VID(pVnode), terrstr(), deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
2273
      }
2274
    }
2275
    tDecoderClear(&mr.coder);
1,584✔
2276
  }
2277
  metaReaderClear(&mr);
2,464✔
2278
  taosArrayDestroy(deleteReq.deleteReqs);
2,465✔
2279
  return 0;
2,467✔
2280
}
2281

2282
static int32_t vnodeProcessDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
53,235✔
2283
                                     SRpcMsg *pOriginalMsg) {
2284
  int32_t     code = 0;
53,235✔
2285
  SDecoder   *pCoder = &(SDecoder){0};
53,235✔
2286
  SDeleteRes *pRes = &(SDeleteRes){0};
53,235✔
2287

2288
  pRsp->msgType = TDMT_VND_DELETE_RSP;
53,235✔
2289
  pRsp->pCont = NULL;
53,235✔
2290
  pRsp->contLen = 0;
53,235✔
2291
  pRsp->code = TSDB_CODE_SUCCESS;
53,235✔
2292

2293
  pRes->uidList = taosArrayInit(0, sizeof(tb_uid_t));
53,235✔
2294
  if (pRes->uidList == NULL) {
53,236!
2295
    code = terrno;
×
2296
    goto _err;
×
2297
  }
2298

2299
  tDecoderInit(pCoder, pReq, len);
53,236✔
2300
  code = tDecodeDeleteRes(pCoder, pRes);
53,236✔
2301
  if (code) goto _err;
53,237!
2302

2303
  if (pRes->affectedRows > 0) {
53,237✔
2304
    for (int32_t iUid = 0; iUid < taosArrayGetSize(pRes->uidList); iUid++) {
100,368✔
2305
      uint64_t uid = *(uint64_t *)taosArrayGet(pRes->uidList, iUid);
50,794✔
2306
      code = tsdbDeleteTableData(pVnode->pTsdb, ver, pRes->suid, uid, pRes->skey, pRes->ekey);
50,794✔
2307
      if (code) goto _err;
50,793!
2308
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, pRes->ctimeMs);
50,793✔
2309
      if (code) goto _err;
50,793!
2310
    }
2311
  }
2312

2313
  code = tdProcessRSmaDelete(pVnode->pSma, ver, pRes, pReq, len);
53,237✔
2314

2315
  tDecoderClear(pCoder);
53,234✔
2316
  taosArrayDestroy(pRes->uidList);
53,234✔
2317

2318
  SVDeleteRsp rsp = {.affectedRows = pRes->affectedRows};
53,239✔
2319
  int32_t     ret = 0;
53,239✔
2320
  tEncodeSize(tEncodeSVDeleteRsp, &rsp, pRsp->contLen, ret);
53,239!
2321
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
53,236✔
2322
  SEncoder ec = {0};
53,236✔
2323
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
53,236✔
2324
  code = tEncodeSVDeleteRsp(&ec, &rsp);
53,238✔
2325
  if (code) goto _err;
53,234!
2326
  tEncoderClear(&ec);
53,234✔
2327
  return code;
53,239✔
2328

2329
_err:
×
2330
  /*
2331
  if(code == TSDB_CODE_SUCCESS){
2332
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_DELETE, pVnode->monitor.strClusterId,
2333
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2334
                                        pOriginalMsg->info.conn.user, "Success"};
2335
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2336
  }
2337
  else{
2338
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_DELETE, pVnode->monitor.strClusterId,
2339
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2340
                                        pOriginalMsg->info.conn.user, "Failed"};
2341
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2342
  }
2343
  */
2344

2345
  return code;
×
2346
}
2347
static int32_t vnodeProcessCreateIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
951✔
2348
  SVCreateStbReq req = {0};
951✔
2349
  SDecoder       dc = {0};
951✔
2350
  int32_t        code = 0;
951✔
2351

2352
  pRsp->msgType = TDMT_VND_CREATE_INDEX_RSP;
951✔
2353
  pRsp->code = TSDB_CODE_SUCCESS;
951✔
2354
  pRsp->pCont = NULL;
951✔
2355
  pRsp->contLen = 0;
951✔
2356

2357
  tDecoderInit(&dc, pReq, len);
951✔
2358
  // decode req
2359
  if (tDecodeSVCreateStbReq(&dc, &req) < 0) {
951!
2360
    tDecoderClear(&dc);
×
2361
    return terrno = TSDB_CODE_INVALID_MSG;
×
2362
  }
2363

2364
  code = metaAddIndexToSTable(pVnode->pMeta, ver, &req);
951✔
2365
  if (code) {
951!
2366
    pRsp->code = code;
×
2367
    goto _err;
×
2368
  }
2369
  tDecoderClear(&dc);
951✔
2370
  return 0;
951✔
2371

2372
_err:
×
2373
  tDecoderClear(&dc);
×
2374
  return code;
×
2375
}
2376
static int32_t vnodeProcessDropIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
2,132✔
2377
  SDropIndexReq req = {0};
2,132✔
2378
  int32_t       code = 0;
2,132✔
2379
  pRsp->msgType = TDMT_VND_DROP_INDEX_RSP;
2,132✔
2380
  pRsp->code = TSDB_CODE_SUCCESS;
2,132✔
2381
  pRsp->pCont = NULL;
2,132✔
2382
  pRsp->contLen = 0;
2,132✔
2383

2384
  if ((code = tDeserializeSDropIdxReq(pReq, len, &req))) {
2,132!
2385
    pRsp->code = code;
×
2386
    return code;
×
2387
  }
2388

2389
  code = metaDropIndexFromSTable(pVnode->pMeta, ver, &req);
2,132✔
2390
  if (code) {
2,132!
2391
    pRsp->code = code;
×
2392
    return code;
×
2393
  }
2394
  return TSDB_CODE_SUCCESS;
2,132✔
2395
}
2396

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

2399
static int32_t vnodeProcessCompactVnodeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
13✔
2400
  if (!pVnode->restored) {
13✔
2401
    vInfo("vgId:%d, ignore compact req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
1!
2402
    return 0;
1✔
2403
  }
2404
  return vnodeAsyncCompact(pVnode, ver, pReq, len, pRsp);
12✔
2405
}
2406

2407
static int32_t vnodeProcessConfigChangeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
2408
  if (syncCheckMember(pVnode->sync) != 0) {
×
2409
    vError("vgId:%d, failed to check member", TD_VID(pVnode));
×
2410
  }
2411

2412
  pRsp->msgType = TDMT_SYNC_CONFIG_CHANGE_RSP;
×
2413
  pRsp->code = TSDB_CODE_SUCCESS;
×
2414
  pRsp->pCont = NULL;
×
2415
  pRsp->contLen = 0;
×
2416

2417
  return 0;
×
2418
}
2419

2420
static int32_t vnodePreCheckAssignedLogSyncd(SVnode *pVnode, char *member0Token, char *member1Token) {
×
2421
  SSyncState syncState = syncGetState(pVnode->sync);
×
2422
  if (syncState.state != TAOS_SYNC_STATE_LEADER) {
×
2423
    return terrno = TSDB_CODE_SYN_NOT_LEADER;
×
2424
  }
2425

2426
  char token[TSDB_ARB_TOKEN_SIZE] = {0};
×
2427
  if (vnodeGetArbToken(pVnode, token) != 0) {
×
2428
    return terrno = TSDB_CODE_NOT_FOUND;
×
2429
  }
2430

2431
  if (strncmp(token, member0Token, TSDB_ARB_TOKEN_SIZE) != 0 &&
×
2432
      strncmp(token, member1Token, TSDB_ARB_TOKEN_SIZE) != 0) {
×
2433
    return terrno = TSDB_CODE_MND_ARB_TOKEN_MISMATCH;
×
2434
  }
2435

2436
  terrno = TSDB_CODE_SUCCESS;
×
2437
  return 0;
×
2438
}
2439

2440
static int32_t vnodeCheckAssignedLogSyncd(SVnode *pVnode, char *member0Token, char *member1Token) {
×
2441
  int32_t code = vnodePreCheckAssignedLogSyncd(pVnode, member0Token, member1Token);
×
2442
  if (code != 0) {
×
2443
    return code;
×
2444
  }
2445

2446
  return syncGetAssignedLogSynced(pVnode->sync);
×
2447
}
2448

2449
static int32_t vnodeProcessArbCheckSyncReq(SVnode *pVnode, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
2450
  int32_t code = 0;
×
2451

2452
  SVArbCheckSyncReq syncReq = {0};
×
2453

2454
  code = tDeserializeSVArbCheckSyncReq(pReq, len, &syncReq);
×
2455
  if (code) {
×
2456
    return terrno = code;
×
2457
  }
2458

2459
  pRsp->msgType = TDMT_VND_ARB_CHECK_SYNC_RSP;
×
2460
  pRsp->code = TSDB_CODE_SUCCESS;
×
2461
  pRsp->pCont = NULL;
×
2462
  pRsp->contLen = 0;
×
2463

2464
  SVArbCheckSyncRsp syncRsp = {0};
×
2465
  syncRsp.arbToken = syncReq.arbToken;
×
2466
  syncRsp.member0Token = syncReq.member0Token;
×
2467
  syncRsp.member1Token = syncReq.member1Token;
×
2468
  syncRsp.vgId = TD_VID(pVnode);
×
2469

2470
  if (vnodeCheckAssignedLogSyncd(pVnode, syncReq.member0Token, syncReq.member1Token) != 0) {
×
2471
    vError("vgId:%d, failed to check assigned log syncd", TD_VID(pVnode));
×
2472
  }
2473
  syncRsp.errCode = terrno;
×
2474

2475
  if (vnodeUpdateArbTerm(pVnode, syncReq.arbTerm) != 0) {
×
2476
    vError("vgId:%d, failed to update arb term", TD_VID(pVnode));
×
2477
    code = -1;
×
2478
    goto _OVER;
×
2479
  }
2480

2481
  int32_t contLen = tSerializeSVArbCheckSyncRsp(NULL, 0, &syncRsp);
×
2482
  if (contLen <= 0) {
×
2483
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2484
    code = -1;
×
2485
    goto _OVER;
×
2486
  }
2487
  void *pHead = rpcMallocCont(contLen);
×
2488
  if (!pHead) {
×
2489
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2490
    code = -1;
×
2491
    goto _OVER;
×
2492
  }
2493

2494
  if (tSerializeSVArbCheckSyncRsp(pHead, contLen, &syncRsp) <= 0) {
×
2495
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2496
    rpcFreeCont(pHead);
×
2497
    code = -1;
×
2498
    goto _OVER;
×
2499
  }
2500

2501
  pRsp->pCont = pHead;
×
2502
  pRsp->contLen = contLen;
×
2503

2504
  terrno = TSDB_CODE_SUCCESS;
×
2505

2506
_OVER:
×
2507
  tFreeSVArbCheckSyncReq(&syncReq);
×
2508
  return code;
×
2509
}
2510

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

© 2026 Coveralls, Inc