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

taosdata / TDengine / #3524

08 Nov 2024 04:27AM UTC coverage: 60.898% (+5.0%) from 55.861%
#3524

push

travis-ci

web-flow
Merge pull request #28647 from taosdata/fix/3.0/TD-32519_drop_ctb

fix TD-32519 drop child table with tsma caused crash

118687 of 248552 branches covered (47.75%)

Branch coverage included in aggregate %.

286 of 337 new or added lines in 18 files covered. (84.87%)

9647 existing lines in 190 files now uncovered.

199106 of 273291 relevant lines covered (72.85%)

15236719.35 hits per line

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

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

16
#include "audit.h"
17
#include "cos.h"
18
#include "monitor.h"
19
#include "tencode.h"
20
#include "tglobal.h"
21
#include "tmsg.h"
22
#include "tstrbuild.h"
23
#include "vnd.h"
24
#include "vnode.h"
25
#include "vnodeInt.h"
26

27
extern taos_counter_t *tsInsertCounter;
28

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

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

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

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

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

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

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

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

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

95
  tEndDecode(pCoder);
166,860✔
96

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

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

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

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

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

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

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

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

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

153
  code = 0;
5,206✔
154

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

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

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

173
  SArray *tbUids = NULL;
105,973✔
174
  int64_t timestampMs = 0;
105,973✔
175

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

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

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

196
    ttlReq.nUids = taosArrayGetSize(tbUids);
106,127✔
197
    ttlReq.pTbUids = tbUids;
106,017✔
198
  }
199

200
  if (ttlReq.nUids == 0) {
106,017✔
201
    code = TSDB_CODE_MSG_PREPROCESSED;
105,965✔
202
    TSDB_CHECK_CODE(code, lino, _exit);
105,965!
203
  }
204

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

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

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

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

226
  code = 0;
38✔
227

228
_exit:
106,030✔
229
  taosArrayDestroy(tbUids);
106,030✔
230

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

238
  return code;
106,017✔
239
}
240

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

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

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

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

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

272
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
10,723,284✔
273
    *(int64_t *)(pCoder->data + pCoder->pos) = uid;
64,898✔
274
    pCoder->pos += sizeof(int64_t);
64,898✔
275
  } else {
276
    if (tDecodeI64(pCoder, &submitTbData.uid) < 0) {
10,654,334!
277
      code = TSDB_CODE_INVALID_MSG;
×
278
      TSDB_CHECK_CODE(code, lino, _exit);
×
279
    }
280
  }
281

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

287
  // scan and check
288
  TSKEY now = btimeMs;
10,704,770✔
289
  if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_MICRO) {
10,704,770✔
290
    now *= 1000;
16,155✔
291
  } else if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_NANO) {
10,688,615✔
292
    now *= 1000000;
3,737✔
293
  }
294

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

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

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

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

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

340
    for (int32_t iRow = 0; iRow < nRow; ++iRow) {
340,979,010✔
341
      SRow *pRow = (SRow *)(pCoder->data + pCoder->pos);
330,268,776✔
342
      pCoder->pos += pRow->len;
330,268,776✔
343

344
      if (pRow->ts < minKey || pRow->ts > maxKey) {
330,268,776!
345
        code = TSDB_CODE_TDB_TIMESTAMP_OUT_OF_RANGE;
96✔
346
        goto _exit;
96✔
347
      }
348
    }
349
  }
350

351
  if (!tDecodeIsEnd(pCoder)) {
10,710,383!
352
    *(int64_t *)(pCoder->data + pCoder->pos) = ctimeMs;
10,715,132✔
353
    pCoder->pos += sizeof(int64_t);
10,715,132✔
354
  }
355

356
  tEndDecode(pCoder);
10,710,383✔
357

358
_exit:
10,712,083✔
359
  return code;
10,712,083✔
360
}
361
static int32_t vnodePreProcessSubmitMsg(SVnode *pVnode, SRpcMsg *pMsg) {
10,018,531✔
362
  int32_t code = 0;
10,018,531✔
363
  int32_t lino = 0;
10,018,531✔
364

365
  SDecoder *pCoder = &(SDecoder){0};
10,018,531✔
366

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

372
  tDecoderInit(pCoder, (uint8_t *)pMsg->pCont + sizeof(SSubmitReq2Msg), pMsg->contLen - sizeof(SSubmitReq2Msg));
10,018,579✔
373

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

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

385
  int64_t btimeMs = taosGetTimestampMs();
10,018,648✔
386
  int64_t ctimeMs = btimeMs;
10,018,648✔
387
  for (int32_t i = 0; i < nSubmitTbData; i++) {
20,730,576✔
388
    code = vnodePreProcessSubmitTbData(pVnode, pCoder, btimeMs, ctimeMs);
10,712,387✔
389
    TSDB_CHECK_CODE(code, lino, _exit);
10,712,025✔
390
  }
391

392
  tEndDecode(pCoder);
10,018,189✔
393

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

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

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

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

415
  code = qWorkerProcessDeleteMsg(&handle, pVnode->pQuery, pMsg, &res);
60,089✔
416
  if (code) goto _exit;
60,090!
417

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

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

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

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

436
  taosArrayDestroy(res.uidList);
60,090✔
437

438
_exit:
60,090✔
439
  return code;
60,090✔
440
}
441

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

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

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

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

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

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

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

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

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

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

483
  return code;
×
484
}
485

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

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

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

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

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

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

541
int32_t vnodePreProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
10,396,456✔
542
  int32_t code = 0;
10,396,456✔
543

544
  switch (pMsg->msgType) {
10,396,456!
545
    case TDMT_VND_CREATE_TABLE: {
76,862✔
546
      code = vnodePreProcessCreateTableMsg(pVnode, pMsg);
76,862✔
547
    } break;
76,859✔
548
    case TDMT_VND_ALTER_TABLE: {
5,206✔
549
      code = vnodePreProcessAlterTableMsg(pVnode, pMsg);
5,206✔
550
    } break;
5,206✔
551
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
106,232✔
552
    case TDMT_VND_DROP_TTL_TABLE: {
553
      code = vnodePreProcessDropTtlMsg(pVnode, pMsg);
106,232✔
554
    } break;
106,013✔
555
    case TDMT_VND_SUBMIT: {
10,018,638✔
556
      code = vnodePreProcessSubmitMsg(pVnode, pMsg);
10,018,638✔
557
    } break;
10,018,511✔
558
    case TDMT_VND_DELETE: {
60,090✔
559
      code = vnodePreProcessDeleteMsg(pVnode, pMsg);
60,090✔
560
    } break;
60,089✔
561
    case TDMT_VND_BATCH_DEL: {
2,983✔
562
      code = vnodePreProcessBatchDeleteMsg(pVnode, pMsg);
2,983✔
563
    } break;
2,983✔
564
    case TDMT_VND_ARB_CHECK_SYNC: {
×
565
      code = vnodePreProcessArbCheckSyncMsg(pVnode, pMsg);
×
566
    } break;
×
567
    case TDMT_VND_DROP_TABLE: {
16,258✔
568
      code = vnodePreProcessDropTbMsg(pVnode, pMsg);
16,258✔
569
    } break;
16,258✔
570
    default:
110,187✔
571
      break;
110,187✔
572
  }
573

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

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

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

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

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

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

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

614
  atomic_store_64(&pVnode->state.applied, ver);
13,367,417✔
615
  atomic_store_64(&pVnode->state.applyTerm, pMsg->info.conn.applyTerm);
13,367,820✔
616

617
  if (!syncUtilUserCommit(pMsg->msgType)) goto _exit;
13,367,810✔
618

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

624
  switch (pMsg->msgType) {
13,340,392!
625
    /* META */
626
    case TDMT_VND_CREATE_STB:
29,752✔
627
      if (vnodeProcessCreateStbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
29,752✔
628
      break;
29,741✔
629
    case TDMT_VND_ALTER_STB:
4,890✔
630
      if (vnodeProcessAlterStbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
4,890!
631
      break;
4,882✔
632
    case TDMT_VND_DROP_STB:
2,327✔
633
      if (vnodeProcessDropStbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
2,327!
634
      break;
2,334✔
635
    case TDMT_VND_CREATE_TABLE:
85,114✔
636
      if (vnodeProcessCreateTbReq(pVnode, ver, pReq, len, pRsp, pMsg) < 0) goto _err;
85,114!
637
      break;
85,157✔
638
    case TDMT_VND_ALTER_TABLE:
5,201✔
639
      if (vnodeProcessAlterTbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
5,201!
640
      break;
5,201✔
641
    case TDMT_VND_DROP_TABLE:
16,300✔
642
      if (vnodeProcessDropTbReq(pVnode, ver, pReq, len, pRsp, pMsg) < 0) goto _err;
16,300!
643
      break;
16,300✔
644
    case TDMT_VND_DROP_TTL_TABLE:
×
645
      if (vnodeProcessDropTtlTbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
646
      break;
×
647
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
38✔
648
      if (vnodeProcessFetchTtlExpiredTbs(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
38!
649
      break;
38✔
650
    case TDMT_VND_TRIM:
262✔
651
      if (vnodeProcessTrimReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
262!
652
      break;
261✔
653
    case TDMT_VND_S3MIGRATE:
×
654
      if (vnodeProcessS3MigrateReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
655
      break;
×
656
    case TDMT_VND_CREATE_SMA:
33✔
657
      if (vnodeProcessCreateTSmaReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
33!
658
      break;
33✔
659
    /* TSDB */
660
    case TDMT_VND_SUBMIT:
13,030,594✔
661
      if (vnodeProcessSubmitReq(pVnode, ver, pMsg->pCont, pMsg->contLen, pRsp, pMsg) < 0) goto _err;
13,030,594✔
662
      break;
13,030,640✔
663
    case TDMT_VND_DELETE:
63,475✔
664
      if (vnodeProcessDeleteReq(pVnode, ver, pReq, len, pRsp, pMsg) < 0) goto _err;
63,475!
665
      break;
63,476✔
666
    case TDMT_VND_BATCH_DEL:
3,345✔
667
      if (vnodeProcessBatchDeleteReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
3,345!
668
      break;
3,351✔
669
    /* TQ */
670
    case TDMT_VND_TMQ_SUBSCRIBE:
4,224✔
671
      if (tqProcessSubscribeReq(pVnode->pTq, ver, pReq, len) < 0) {
4,224✔
672
        goto _err;
1✔
673
      }
674
      break;
4,226✔
675
    case TDMT_VND_TMQ_DELETE_SUB:
883✔
676
      if (tqProcessDeleteSubReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen) < 0) {
883!
677
        goto _err;
×
678
      }
679
      break;
884✔
680
    case TDMT_VND_TMQ_COMMIT_OFFSET:
14,041✔
681
      if (tqProcessOffsetCommitReq(pVnode->pTq, ver, pReq, len) < 0) {
14,041!
682
        goto _err;
×
683
      }
684
      break;
14,044✔
685
    case TDMT_VND_TMQ_ADD_CHECKINFO:
132✔
686
      if (tqProcessAddCheckInfoReq(pVnode->pTq, ver, pReq, len) < 0) {
132!
687
        goto _err;
×
688
      }
689
      break;
133✔
690
    case TDMT_VND_TMQ_DEL_CHECKINFO:
30✔
691
      if (tqProcessDelCheckInfoReq(pVnode->pTq, ver, pReq, len) < 0) {
30✔
692
        goto _err;
2✔
693
      }
694
      break;
28✔
695
    case TDMT_STREAM_TASK_DEPLOY: {
13,653✔
696
      if ((code = tqProcessTaskDeployReq(pVnode->pTq, ver, pReq, len)) != TSDB_CODE_SUCCESS) {
13,653!
697
        goto _err;
×
698
      }
699
    } break;
13,663✔
700
    case TDMT_STREAM_TASK_DROP: {
6,922✔
701
      if ((code = tqProcessTaskDropReq(pVnode->pTq, pMsg->pCont, pMsg->contLen)) < 0) {
6,922!
702
        goto _err;
×
703
      }
704
    } break;
6,947✔
705
    case TDMT_STREAM_TASK_UPDATE_CHKPT: {
6,932✔
706
      if ((code = tqProcessTaskUpdateCheckpointReq(pVnode->pTq, pMsg->pCont, pMsg->contLen)) < 0) {
6,932!
707
        goto _err;
×
708
      }
709
    } break;
6,874✔
710
    case TDMT_STREAM_CONSEN_CHKPT: {
146✔
711
      if (pVnode->restored && (code = tqProcessTaskConsenChkptIdReq(pVnode->pTq, pMsg)) < 0) {
146!
712
        goto _err;
×
713
      }
714

715
    } break;
146✔
716
    case TDMT_STREAM_TASK_PAUSE: {
1,891✔
717
      if (pVnode->restored && vnodeIsLeader(pVnode) &&
3,194!
718
          (code = tqProcessTaskPauseReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen)) < 0) {
1,301✔
719
        goto _err;
×
720
      }
721
    } break;
1,897✔
722
    case TDMT_STREAM_TASK_RESUME: {
1,890✔
723
      if (pVnode->restored && vnodeIsLeader(pVnode) &&
3,186!
724
          (code = tqProcessTaskResumeReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen)) < 0) {
1,296✔
725
        goto _err;
1,284✔
726
      }
727
    } break;
606✔
728
    case TDMT_VND_STREAM_TASK_RESET: {
×
729
      if (pVnode->restored && vnodeIsLeader(pVnode) &&
×
730
           (code = tqProcessTaskResetReq(pVnode->pTq, pMsg)) < 0) {
×
731
          goto _err;
×
732
        }
733

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

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

782
  walApplyVer(pVnode->pWal, ver);
13,339,125✔
783

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

790
  // commit if need
791
  if (needCommit) {
13,338,634✔
792
    vInfo("vgId:%d, commit at version %" PRId64, TD_VID(pVnode), ver);
31,277!
793
    code = vnodeAsyncCommit(pVnode);
31,284✔
794
    if (code) {
31,286!
795
      vError("vgId:%d, failed to vnode async commit since %s.", TD_VID(pVnode), tstrerror(terrno));
×
796
      goto _err;
×
797
    }
798

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

807
_exit:
13,338,595✔
808
  return 0;
13,365,481✔
809

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

816
int32_t vnodePreprocessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg) {
7,165,919✔
817
  if (TDMT_SCH_QUERY != pMsg->msgType && TDMT_SCH_MERGE_QUERY != pMsg->msgType) {
7,165,919✔
818
    return 0;
293,159✔
819
  }
820

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

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

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

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

839
  switch (pMsg->msgType) {
7,170,977!
840
    case TDMT_SCH_QUERY:
6,879,619✔
841
    case TDMT_SCH_MERGE_QUERY:
842
      return qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
6,879,619✔
843
    case TDMT_SCH_QUERY_CONTINUE:
179,395✔
844
      return qWorkerProcessCQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
179,395✔
845
    case TDMT_VND_TMQ_CONSUME:
82,475✔
846
      return tqProcessPollReq(pVnode->pTq, pMsg);
82,475✔
847
    case TDMT_VND_TMQ_CONSUME_PUSH:
29,694✔
848
      return tqProcessPollPush(pVnode->pTq, pMsg);
29,694✔
849
    default:
×
850
      vError("unknown msg type:%d in query queue", pMsg->msgType);
×
851
      return TSDB_CODE_APP_ERROR;
×
852
  }
853
}
854

855
int32_t vnodeProcessFetchMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
17,939,673✔
856
  vTrace("vgId:%d, msg:%p in fetch queue is processing", pVnode->config.vgId, pMsg);
17,939,673✔
857
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
17,939,675✔
858
       pMsg->msgType == TDMT_VND_BATCH_META || pMsg->msgType == TDMT_VND_TABLE_NAME) &&
12,723,552✔
859
      !syncIsReadyForRead(pVnode->sync)) {
6,348,284✔
860
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
5,749✔
861
    return 0;
5,749✔
862
  }
863

864
  switch (pMsg->msgType) {
17,936,208!
865
    case TDMT_SCH_FETCH:
8,332,060✔
866
    case TDMT_SCH_MERGE_FETCH:
867
      return qWorkerProcessFetchMsg(pVnode, pVnode->pQuery, pMsg, 0);
8,332,060✔
868
    case TDMT_SCH_FETCH_RSP:
×
869
      return qWorkerProcessRspMsg(pVnode, pVnode->pQuery, pMsg, 0);
×
870
    // case TDMT_SCH_CANCEL_TASK:
871
    //   return qWorkerProcessCancelMsg(pVnode, pVnode->pQuery, pMsg, 0);
872
    case TDMT_SCH_DROP_TASK:
6,878,256✔
873
      return qWorkerProcessDropMsg(pVnode, pVnode->pQuery, pMsg, 0);
6,878,256✔
874
    case TDMT_SCH_TASK_NOTIFY:
61✔
875
      return qWorkerProcessNotifyMsg(pVnode, pVnode->pQuery, pMsg, 0);
61✔
876
    case TDMT_SCH_QUERY_HEARTBEAT:
1,604,241✔
877
      return qWorkerProcessHbMsg(pVnode, pVnode->pQuery, pMsg, 0);
1,604,241✔
878
    case TDMT_VND_TABLE_META:
1,771✔
879
    case TDMT_VND_TABLE_NAME:
880
      return vnodeGetTableMeta(pVnode, pMsg, true);
1,771✔
881
    case TDMT_VND_TABLE_CFG:
×
882
      return vnodeGetTableCfg(pVnode, pMsg, true);
×
883
    case TDMT_VND_BATCH_META:
1,119,743✔
884
      return vnodeGetBatchMeta(pVnode, pMsg);
1,119,743✔
885
#ifdef TD_ENTERPRISE
886
    case TDMT_VND_QUERY_COMPACT_PROGRESS:
34✔
887
      return vnodeQueryCompactProgress(pVnode, pMsg);
34✔
888
#endif
889
      //    case TDMT_VND_TMQ_CONSUME:
890
      //      return tqProcessPollReq(pVnode->pTq, pMsg);
891
    case TDMT_VND_TMQ_VG_WALINFO:
13✔
892
      return tqProcessVgWalInfoReq(pVnode->pTq, pMsg);
13✔
893
    case TDMT_VND_TMQ_VG_COMMITTEDINFO:
3✔
894
      return tqProcessVgCommittedInfoReq(pVnode->pTq, pMsg);
3✔
895
    case TDMT_VND_TMQ_SEEK:
26✔
896
      return tqProcessSeekReq(pVnode->pTq, pMsg);
26✔
897

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

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

913
  switch (pMsg->msgType) {
438,646!
914
    case TDMT_STREAM_TASK_RUN:
216,465✔
915
      return tqProcessTaskRunReq(pVnode->pTq, pMsg);
216,465✔
916
    case TDMT_STREAM_TASK_DISPATCH:
63,581✔
917
      return tqProcessTaskDispatchReq(pVnode->pTq, pMsg);
63,581✔
918
    case TDMT_STREAM_TASK_DISPATCH_RSP:
63,748✔
919
      return tqProcessTaskDispatchRsp(pVnode->pTq, pMsg);
63,748✔
920
    case TDMT_VND_STREAM_TASK_CHECK:
20,805✔
921
      return tqProcessTaskCheckReq(pVnode->pTq, pMsg);
20,805✔
922
    case TDMT_VND_STREAM_TASK_CHECK_RSP:
20,864✔
923
      return tqProcessTaskCheckRsp(pVnode->pTq, pMsg);
20,864✔
924
    case TDMT_STREAM_RETRIEVE:
545✔
925
      return tqProcessTaskRetrieveReq(pVnode->pTq, pMsg);
545✔
926
    case TDMT_STREAM_RETRIEVE_RSP:
440✔
927
      return tqProcessTaskRetrieveRsp(pVnode->pTq, pMsg);
440✔
928
    case TDMT_VND_STREAM_SCAN_HISTORY:
2,551✔
929
      return tqProcessTaskScanHistory(pVnode->pTq, pMsg);
2,551✔
930
    case TDMT_STREAM_TASK_CHECKPOINT_READY:
8,215✔
931
      return tqProcessTaskCheckpointReadyMsg(pVnode->pTq, pMsg);
8,215✔
932
    case TDMT_STREAM_TASK_CHECKPOINT_READY_RSP:
8,203✔
933
      return tqProcessTaskCheckpointReadyRsp(pVnode->pTq, pMsg);
8,203✔
934
    case TDMT_STREAM_RETRIEVE_TRIGGER:
×
935
      return tqProcessTaskRetrieveTriggerReq(pVnode->pTq, pMsg);
×
936
    case TDMT_STREAM_RETRIEVE_TRIGGER_RSP:
×
937
      return tqProcessTaskRetrieveTriggerRsp(pVnode->pTq, pMsg);
×
938
    case TDMT_MND_STREAM_HEARTBEAT_RSP:
22,242✔
939
      return tqProcessStreamHbRsp(pVnode->pTq, pMsg);
22,242✔
940
    case TDMT_MND_STREAM_REQ_CHKPT_RSP:
4,705✔
941
      return tqProcessStreamReqCheckpointRsp(pVnode->pTq, pMsg);
4,705✔
942
    case TDMT_VND_GET_STREAM_PROGRESS:
×
943
      return tqStreamProgressRetrieveReq(pVnode->pTq, pMsg);
×
944
    case TDMT_MND_STREAM_CHKPT_REPORT_RSP:
6,282✔
945
      return tqProcessTaskChkptReportRsp(pVnode->pTq, pMsg);
6,282✔
946
    default:
×
947
      vError("unknown msg type:%d in stream queue", pMsg->msgType);
×
948
      return TSDB_CODE_APP_ERROR;
×
949
  }
950
}
951

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1286
    SStringBuilder sb = {0};
85,158✔
1287
    for (int32_t i = 0; i < tbNames->size; i++) {
211,513✔
1288
      char **key = (char **)taosArrayGet(tbNames, i);
126,355✔
1289
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
126,353✔
1290
      if (i < tbNames->size - 1) {
126,354✔
1291
        taosStringBuilderAppendChar(&sb, ',');
41,197✔
1292
      }
1293
      // taosMemoryFreeClear(*key);
1294
    }
1295

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

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

1303
    taosStringBuilderDestroy(&sb);
85,159✔
1304
  }
1305

1306
_exit:
1✔
1307
  tDeleteSVCreateTbBatchReq(&req);
85,161✔
1308
  taosArrayDestroyEx(rsp.pArray, tFreeSVCreateTbRsp);
85,163✔
1309
  taosArrayDestroy(tbUids);
85,162✔
1310
  tDecoderClear(&decoder);
85,162✔
1311
  tEncoderClear(&encoder);
85,162✔
1312
  taosArrayDestroyP(tbNames, taosMemoryFree);
85,163✔
1313
  return rcode;
85,164✔
1314
}
1315

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

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

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

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

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

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

1344
  return 0;
4,888✔
1345
}
1346

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1597
  taosMemoryFreeClear(pSchema);
1598

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

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

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

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

1651
  return TSDB_CODE_SUCCESS;
×
1652
}
1653

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

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

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

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

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

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

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

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

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

1717
  return code;
×
1718
}
1719

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

1726
  SSubmitReqConvertCxt cxt = {0};
×
1727

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

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

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

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

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

1796
  pRsp->code = TSDB_CODE_SUCCESS;
13,030,602✔
1797

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

1824
  // scan
1825
  TSKEY now = taosGetTimestamp(pVnode->config.tsdbCfg.precision);
13,030,778✔
1826
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * pVnode->config.tsdbCfg.keep2;
13,030,625✔
1827
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
13,030,625✔
1828
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
26,795,920✔
1829
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
13,763,949✔
1830

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

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

1842
      SColData *colDataArr = TARRAY_DATA(pSubmitTbData->aCol);
149✔
1843
      SRowKey   lastKey;
1844
      tColDataArrGetRowKey(colDataArr, TARRAY_SIZE(pSubmitTbData->aCol), 0, &lastKey);
149✔
1845
      for (int32_t iRow = 1; iRow < colDataArr[0].nVal; iRow++) {
254✔
1846
        SRowKey key;
1847
        tColDataArrGetRowKey(TARRAY_DATA(pSubmitTbData->aCol), TARRAY_SIZE(pSubmitTbData->aCol), iRow, &key);
105✔
1848
        if (tRowKeyCompare(&lastKey, &key) >= 0) {
105!
1849
          code = TSDB_CODE_INVALID_MSG;
×
1850
          vError("vgId:%d %s failed 1 since %s, version:%" PRId64, TD_VID(pVnode), __func__, tstrerror(terrno), ver);
×
1851
          goto _exit;
×
1852
        }
1853
      }
1854
    } else {
1855
      int32_t nRow = TARRAY_SIZE(pSubmitTbData->aRowP);
13,763,775✔
1856
      SRow  **aRow = (SRow **)TARRAY_DATA(pSubmitTbData->aRowP);
13,763,775✔
1857
      SRowKey lastRowKey;
1858
      for (int32_t iRow = 0; iRow < nRow; ++iRow) {
407,096,744✔
1859
        if (aRow[iRow]->ts < minKey || aRow[iRow]->ts > maxKey) {
393,331,598✔
1860
          code = TSDB_CODE_INVALID_MSG;
44,816✔
1861
          vError("vgId:%d %s failed 2 since %s, version:%" PRId64, TD_VID(pVnode), __func__, tstrerror(code), ver);
44,816!
1862
          goto _exit;
×
1863
        }
1864
        if (iRow == 0) {
393,286,782✔
1865
          tRowGetKey(aRow[iRow], &lastRowKey);
13,763,740✔
1866
        } else {
1867
          SRowKey rowKey;
1868
          tRowGetKey(aRow[iRow], &rowKey);
379,523,042✔
1869

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

1881
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
26,799,991✔
1882
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
13,767,661✔
1883

1884
    if (pSubmitTbData->pCreateTbReq) {
13,767,446✔
1885
      pSubmitTbData->uid = pSubmitTbData->pCreateTbReq->uid;
73,337✔
1886
    } else {
1887
      SMetaInfo info = {0};
13,694,109✔
1888

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

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

1901
      if (info.suid) {
13,694,645✔
1902
        if (metaGetInfo(pVnode->pMeta, info.suid, &info, NULL) != 0) {
12,786,503!
1903
          vWarn("vgId:%d, table uid:%" PRId64 " not exists", TD_VID(pVnode), info.suid);
×
1904
        }
1905
      }
1906

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

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

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

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

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

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

1939
  // loop to handle
1940
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
26,793,320✔
1941
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
13,762,460✔
1942

1943
    // create table
1944
    if (pSubmitTbData->pCreateTbReq) {
13,762,181✔
1945
      // alloc if need
1946
      if (pSubmitRsp->aCreateTbRsp == NULL &&
73,333✔
1947
          (pSubmitRsp->aCreateTbRsp = taosArrayInit(TARRAY_SIZE(pSubmitReq->aSubmitTbData), sizeof(SVCreateTbRsp))) ==
54,972!
1948
              NULL) {
1949
        code = terrno;
×
1950
        goto _exit;
8✔
1951
      }
1952

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

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

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

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

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

1985
    // insert data
1986
    int32_t affectedRows;
1987
    code = tsdbInsertTableData(pVnode->pTsdb, ver, pSubmitTbData, &affectedRows);
13,762,172✔
1988
    if (code) goto _exit;
13,764,242!
1989

1990
    code = metaUpdateChangeTimeWithLock(pVnode->pMeta, pSubmitTbData->uid, pSubmitTbData->ctimeMs);
13,764,242✔
1991
    if (code) goto _exit;
13,762,491!
1992

1993
    pSubmitRsp->affectedRows += affectedRows;
13,762,491✔
1994
  }
1995

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

2005
_exit:
13,030,756✔
2006
  // message
2007
  pRsp->code = code;
13,030,773✔
2008
  tEncodeSize(tEncodeSSubmitRsp2, pSubmitRsp, pRsp->contLen, ret);
13,030,773!
2009
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
13,030,703✔
2010
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
13,030,809✔
2011
  if (tEncodeSSubmitRsp2(&ec, pSubmitRsp) < 0) {
13,030,805!
2012
    vError("vgId:%d, failed to encode submit response", TD_VID(pVnode));
×
2013
  }
2014
  tEncoderClear(&ec);
13,030,746✔
2015

2016
  // update statistics
2017
  (void)atomic_add_fetch_64(&pVnode->statis.nInsert, pSubmitRsp->affectedRows);
13,030,778✔
2018
  (void)atomic_add_fetch_64(&pVnode->statis.nInsertSuccess, pSubmitRsp->affectedRows);
13,030,860✔
2019
  (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsert, 1);
13,030,872✔
2020

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

2033
  if (code == 0) {
13,030,864✔
2034
    (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
13,030,829✔
2035
    code = tdProcessRSmaSubmit(pVnode->pSma, ver, pSubmitReq, pReq, len);
13,030,832✔
2036
  }
2037
  /*
2038
  if (code == 0) {
2039
    atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
2040
    code = tdProcessRSmaSubmit(pVnode->pSma, ver, pSubmitReq, pReq, len);
2041

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

2055
  // clear
2056
  taosArrayDestroy(newTbUids);
13,030,798✔
2057
  tDestroySubmitReq(pSubmitReq, 0 == pMsg->version ? TSDB_MSG_FLG_CMPT : TSDB_MSG_FLG_DECODE);
13,030,740!
2058
  tDestroySSubmitRsp2(pSubmitRsp, TSDB_MSG_FLG_ENCODE);
13,030,814✔
2059

2060
  if (code) terrno = code;
13,030,738✔
2061

2062
  taosMemoryFree(pAllocMsg);
13,030,738✔
2063

2064
  return code;
13,030,757✔
2065
}
2066

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

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

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

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

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

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

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

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

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

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

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

2125
  return code;
86✔
2126
}
2127

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

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

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

2148
  return code;
8,146✔
2149
}
2150

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2287
  return 0;
889✔
2288
}
2289

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

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

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

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

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

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

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

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

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

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

2358
  tDecoderInit(pCoder, pReq, len);
63,478✔
2359
  code = tDecodeDeleteRes(pCoder, pRes);
63,476✔
2360
  if (code) goto _err;
63,476!
2361

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

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

2374
  tDecoderClear(pCoder);
63,478✔
2375
  taosArrayDestroy(pRes->uidList);
63,478✔
2376

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

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

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

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

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

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

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

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

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

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

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

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

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

2476
  return 0;
×
2477
}
2478

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

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

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

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

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

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

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

2511
  SVArbCheckSyncReq syncReq = {0};
×
2512

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

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

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

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

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

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

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

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

2563
  terrno = TSDB_CODE_SUCCESS;
×
2564

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

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

STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc