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

taosdata / TDengine / #3533

20 Nov 2024 07:11AM UTC coverage: 58.848% (-1.9%) from 60.78%
#3533

push

travis-ci

web-flow
Merge pull request #28823 from taosdata/fix/3.0/TD-32587

fix:[TD-32587]fix stmt segmentation fault

115578 of 252434 branches covered (45.79%)

Branch coverage included in aggregate %.

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

8038 existing lines in 233 files now uncovered.

194926 of 275199 relevant lines covered (70.83%)

1494459.59 hits per line

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

58.08
/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) {
40,168✔
64
  int32_t code = 0;
40,168✔
65
  int32_t lino = 0;
40,168✔
66

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

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

78
  // name
79
  char *name = NULL;
40,169✔
80
  if (tDecodeCStr(pCoder, &name) < 0) {
40,169!
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);
40,169✔
87
  if (uid == 0) {
40,168✔
88
    uid = tGenIdPI64();
22,759✔
89
  }
90
  *(int64_t *)(pCoder->data + pCoder->pos) = uid;
40,166✔
91

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

95
  tEndDecode(pCoder);
40,166✔
96

97
_exit:
40,166✔
98
  if (code) {
40,166!
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);
40,166✔
102
    if (pUid) *pUid = uid;
40,166✔
103
  }
104
  return code;
40,166✔
105
}
106
static int32_t vnodePreProcessCreateTableMsg(SVnode *pVnode, SRpcMsg *pMsg) {
15,320✔
107
  int32_t code = 0;
15,320✔
108
  int32_t lino = 0;
15,320✔
109

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

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

120
  if (tDecodeI32v(&dc, &nReqs) < 0) {
15,320!
121
    code = TSDB_CODE_INVALID_MSG;
×
122
    TSDB_CHECK_CODE(code, lino, _exit);
×
123
  }
124
  for (int32_t iReq = 0; iReq < nReqs; iReq++) {
34,474✔
125
    code = vnodePreprocessCreateTableReq(pVnode, &dc, btime, NULL);
19,154✔
126
    TSDB_CHECK_CODE(code, lino, _exit);
19,154!
127
  }
128

129
  tEndDecode(&dc);
15,320✔
130

131
_exit:
15,320✔
132
  tDecoderClear(&dc);
15,320✔
133
  if (code) {
15,320!
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;
15,320✔
138
}
139

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

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

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

153
  code = 0;
903✔
154

155
_exit:
903✔
156
  tDecoderClear(&dc);
903✔
157
  if (code) {
903!
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,
903✔
161
           ctimeMs);
162
  }
163
  return code;
903✔
164
}
165

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

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

173
  SArray *tbUids = NULL;
8,583✔
174
  int64_t timestampMs = 0;
8,583✔
175

176
  SVDropTtlTableReq ttlReq = {0};
8,583✔
177
  if (tDeserializeSVDropTtlTableReq((char *)pContOld + sizeof(SMsgHead), reqLenOld, &ttlReq) != 0) {
8,583!
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));
8,592✔
184
    if (tbUids == NULL) {
8,601!
185
      code = terrno;
×
186
      TSDB_CHECK_CODE(code, lino, _exit);
×
187
    }
188

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

196
    ttlReq.nUids = taosArrayGetSize(tbUids);
8,599✔
197
    ttlReq.pTbUids = tbUids;
8,572✔
198
  }
199

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

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

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

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

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

226
  code = 0;
18✔
227

228
_exit:
8,586✔
229
  taosArrayDestroy(tbUids);
8,586✔
230

231
  if (code && code != TSDB_CODE_MSG_PREPROCESSED) {
8,601!
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);
8,601✔
236
  }
237

238
  return code;
8,577✔
239
}
240

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

246
  if (tStartDecode(pCoder) < 0) {
810,315!
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) {
810,352!
254
    code = TSDB_CODE_INVALID_MSG;
×
255
    TSDB_CHECK_CODE(code, lino, _exit);
×
256
  }
257
  version = (submitTbData.flags >> 8) & 0xff;
810,352✔
258
  submitTbData.flags = submitTbData.flags & 0xff;
810,352✔
259

260
  int64_t uid;
261
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
810,352✔
262
    code = vnodePreprocessCreateTableReq(pVnode, pCoder, btimeMs, &uid);
21,013✔
263
    TSDB_CHECK_CODE(code, lino, _exit);
21,012!
264
  }
265

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

272
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
810,350✔
273
    *(int64_t *)(pCoder->data + pCoder->pos) = uid;
21,013✔
274
    pCoder->pos += sizeof(int64_t);
21,013✔
275
  } else {
276
    if (tDecodeI64(pCoder, &submitTbData.uid) < 0) {
789,336!
277
      code = TSDB_CODE_INVALID_MSG;
×
278
      TSDB_CHECK_CODE(code, lino, _exit);
×
279
    }
280
  }
281

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

287
  // scan and check
288
  TSKEY now = btimeMs;
810,344✔
289
  if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_MICRO) {
810,344✔
290
    now *= 1000;
3,624✔
291
  } else if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_NANO) {
806,720✔
292
    now *= 1000000;
858✔
293
  }
294

295
  int32_t keep = pVnode->config.tsdbCfg.keep2;
810,344✔
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;
810,344✔
308
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
810,344✔
309
  if (submitTbData.flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
810,344✔
310
    uint64_t nColData;
311
    if (tDecodeU64v(pCoder, &nColData) < 0) {
130!
312
      code = TSDB_CODE_INVALID_MSG;
×
313
      goto _exit;
1✔
314
    }
315

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

323
    for (int32_t iRow = 0; iRow < colData.nVal; iRow++) {
343✔
324
      if (((TSKEY *)colData.pData)[iRow] < minKey || ((TSKEY *)colData.pData)[iRow] > maxKey) {
214!
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++) {
587✔
331
      pCoder->pos += tGetColData(version, pCoder->data + pCoder->pos, &colData);
458✔
332
    }
333
  } else {
334
    uint64_t nRow;
335
    if (tDecodeU64v(pCoder, &nRow) < 0) {
810,187!
336
      code = TSDB_CODE_INVALID_MSG;
×
337
      goto _exit;
5✔
338
    }
339

340
    for (int32_t iRow = 0; iRow < nRow; ++iRow) {
28,166,406✔
341
      SRow *pRow = (SRow *)(pCoder->data + pCoder->pos);
27,356,224✔
342
      pCoder->pos += pRow->len;
27,356,224✔
343

344
      if (pRow->ts < minKey || pRow->ts > maxKey) {
27,356,224!
345
        code = TSDB_CODE_TDB_TIMESTAMP_OUT_OF_RANGE;
5✔
346
        goto _exit;
5✔
347
      }
348
    }
349
  }
350

351
  if (!tDecodeIsEnd(pCoder)) {
810,311!
352
    *(int64_t *)(pCoder->data + pCoder->pos) = ctimeMs;
810,344✔
353
    pCoder->pos += sizeof(int64_t);
810,344✔
354
  }
355

356
  tEndDecode(pCoder);
810,311✔
357

358
_exit:
810,345✔
359
  return code;
810,345✔
360
}
361
static int32_t vnodePreProcessSubmitMsg(SVnode *pVnode, SRpcMsg *pMsg) {
774,610✔
362
  int32_t code = 0;
774,610✔
363
  int32_t lino = 0;
774,610✔
364

365
  if (tsBypassFlag & TSDB_BYPASS_RA_RPC_RECV_SUBMIT) {
774,610!
366
    return TSDB_CODE_MSG_PREPROCESSED;
×
367
  }
368

369
  SDecoder *pCoder = &(SDecoder){0};
774,610✔
370

371
  if (taosHton64(((SSubmitReq2Msg *)pMsg->pCont)->version) != 1) {
774,610!
372
    code = TSDB_CODE_INVALID_MSG;
×
373
    TSDB_CHECK_CODE(code, lino, _exit);
×
374
  }
375

376
  tDecoderInit(pCoder, (uint8_t *)pMsg->pCont + sizeof(SSubmitReq2Msg), pMsg->contLen - sizeof(SSubmitReq2Msg));
774,630✔
377

378
  if (tStartDecode(pCoder) < 0) {
774,608!
379
    code = TSDB_CODE_INVALID_MSG;
×
380
    TSDB_CHECK_CODE(code, lino, _exit);
×
381
  }
382

383
  uint64_t nSubmitTbData;
384
  if (tDecodeU64v(pCoder, &nSubmitTbData) < 0) {
774,635!
385
    code = TSDB_CODE_INVALID_MSG;
×
386
    TSDB_CHECK_CODE(code, lino, _exit);
×
387
  }
388

389
  int64_t btimeMs = taosGetTimestampMs();
774,619✔
390
  int64_t ctimeMs = btimeMs;
774,619✔
391
  for (int32_t i = 0; i < nSubmitTbData; i++) {
1,584,959✔
392
    code = vnodePreProcessSubmitTbData(pVnode, pCoder, btimeMs, ctimeMs);
810,335✔
393
    TSDB_CHECK_CODE(code, lino, _exit);
810,346✔
394
  }
395

396
  tEndDecode(pCoder);
774,624✔
397

398
_exit:
774,638✔
399
  tDecoderClear(pCoder);
774,638✔
400
  if (code) {
774,623✔
401
    vError("vgId:%d, %s:%d failed to preprocess submit request since %s, msg type:%s", TD_VID(pVnode), __func__, lino,
6!
402
           tstrerror(code), TMSG_INFO(pMsg->msgType));
403
  }
404
  return code;
774,623✔
405
}
406

407
static int32_t vnodePreProcessDeleteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
1,193✔
408
  int32_t code = 0;
1,193✔
409

410
  int32_t    size;
411
  int32_t    ret;
412
  uint8_t   *pCont;
413
  SEncoder  *pCoder = &(SEncoder){0};
1,193✔
414
  SDeleteRes res = {0};
1,193✔
415

416
  SReadHandle handle = {.vnode = pVnode, .pMsgCb = &pVnode->msgCb, .skipRollup = 1};
1,193✔
417
  initStorageAPI(&handle.api);
1,193✔
418

419
  code = qWorkerProcessDeleteMsg(&handle, pVnode->pQuery, pMsg, &res);
1,195✔
420
  if (code) goto _exit;
1,195!
421

422
  res.ctimeMs = taosGetTimestampMs();
1,195✔
423
  // malloc and encode
424
  tEncodeSize(tEncodeDeleteRes, &res, size, ret);
1,195!
425
  pCont = rpcMallocCont(size + sizeof(SMsgHead));
1,195✔
426

427
  ((SMsgHead *)pCont)->contLen = size + sizeof(SMsgHead);
1,195✔
428
  ((SMsgHead *)pCont)->vgId = TD_VID(pVnode);
1,195✔
429

430
  tEncoderInit(pCoder, pCont + sizeof(SMsgHead), size);
1,195✔
431
  if (tEncodeDeleteRes(pCoder, &res) != 0) {
1,195!
432
    vError("vgId:%d %s failed to encode delete response", TD_VID(pVnode), __func__);
×
433
  }
434
  tEncoderClear(pCoder);
1,195✔
435

436
  rpcFreeCont(pMsg->pCont);
1,195✔
437
  pMsg->pCont = pCont;
1,195✔
438
  pMsg->contLen = size + sizeof(SMsgHead);
1,195✔
439

440
  taosArrayDestroy(res.uidList);
1,195✔
441

442
_exit:
1,195✔
443
  return code;
1,195✔
444
}
445

446
static int32_t vnodePreProcessBatchDeleteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
589✔
447
  int32_t code = 0;
589✔
448
  int32_t lino = 0;
589✔
449

450
  int64_t         ctimeMs = taosGetTimestampMs();
589✔
451
  SBatchDeleteReq pReq = {0};
589✔
452
  SDecoder       *pCoder = &(SDecoder){0};
589✔
453

454
  tDecoderInit(pCoder, (uint8_t *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead));
589✔
455

456
  if (tDecodeSBatchDeleteReqSetCtime(pCoder, &pReq, ctimeMs) < 0) {
589!
457
    code = TSDB_CODE_INVALID_MSG;
×
458
  }
459

460
  tDecoderClear(pCoder);
589✔
461
  taosArrayDestroy(pReq.deleteReqs);
589✔
462

463
  if (code) {
589!
464
    vError("vgId:%d %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
465
  } else {
466
    vTrace("vgId:%d %s done, ctimeMs generated:%" PRId64, TD_VID(pVnode), __func__, ctimeMs);
589✔
467
  }
468
  return code;
589✔
469
}
470

471
static int32_t vnodePreProcessArbCheckSyncMsg(SVnode *pVnode, SRpcMsg *pMsg) {
×
472
  SVArbCheckSyncReq syncReq = {0};
×
473

474
  if (tDeserializeSVArbCheckSyncReq((char *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead),
×
475
                                    &syncReq) != 0) {
476
    return TSDB_CODE_INVALID_MSG;
×
477
  }
478

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

484
  int32_t code = terrno;
×
485
  tFreeSVArbCheckSyncReq(&syncReq);
×
486

487
  return code;
×
488
}
489

490
int32_t vnodePreProcessDropTbMsg(SVnode* pVnode, SRpcMsg* pMsg) {
1,007✔
491
  int32_t          code = TSDB_CODE_SUCCESS;
1,007✔
492
  int32_t          lino = 0;
1,007✔
493
  int32_t          size = 0;
1,007✔
494
  SDecoder         dc = {0};
1,007✔
495
  SEncoder         ec = {0};
1,007✔
496
  SVDropTbBatchReq receivedBatchReqs = {0};
1,007✔
497
  SVDropTbBatchReq sentBatchReqs = {0};
1,007✔
498

499
  tDecoderInit(&dc, POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), pMsg->contLen - sizeof(SMsgHead));
1,007✔
500

501
  code = tDecodeSVDropTbBatchReq(&dc, &receivedBatchReqs);
1,007✔
502
  if (code < 0) {
1,007!
503
    terrno = code;
×
504
    TSDB_CHECK_CODE(code, lino, _exit);
×
505
  }
506
  sentBatchReqs.pArray = taosArrayInit(receivedBatchReqs.nReqs, sizeof(SVDropTbReq));
1,007✔
507
  if (!sentBatchReqs.pArray) {
1,007!
508
    code = terrno;
×
509
    goto _exit;
×
510
  }
511

512
  for (int32_t i = 0; i < receivedBatchReqs.nReqs; ++i) {
2,025✔
513
    SVDropTbReq* pReq = receivedBatchReqs.pReqs + i;
1,018✔
514
    tb_uid_t uid = metaGetTableEntryUidByName(pVnode->pMeta, pReq->name);
1,018✔
515
    if (uid == 0) {
1,018!
516
      vWarn("vgId:%d, preprocess drop ctb: %s not found", TD_VID(pVnode), pReq->name);
×
517
      continue;
×
518
    }
519
    pReq->uid = uid;
1,018✔
520
    vDebug("vgId:%d %s for: %s, uid: %"PRId64, TD_VID(pVnode), __func__, pReq->name, pReq->uid);
1,018✔
521
    if (taosArrayPush(sentBatchReqs.pArray, pReq) == NULL) {
2,036!
522
      code = terrno;
×
523
      goto _exit;
×
524
    }
525
  }
526
  sentBatchReqs.nReqs = sentBatchReqs.pArray->size;
1,007✔
527

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

537
_exit:
1,007✔
538
  tDecoderClear(&dc);
1,007✔
539
  if (sentBatchReqs.pArray) {
1,007!
540
    taosArrayDestroy(sentBatchReqs.pArray);
1,007✔
541
  }
542
  return code;
1,007✔
543
}
544

545
int32_t vnodePreProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
833,435✔
546
  int32_t code = 0;
833,435✔
547

548
  switch (pMsg->msgType) {
833,435!
549
    case TDMT_VND_CREATE_TABLE: {
15,319✔
550
      code = vnodePreProcessCreateTableMsg(pVnode, pMsg);
15,319✔
551
    } break;
15,320✔
552
    case TDMT_VND_ALTER_TABLE: {
903✔
553
      code = vnodePreProcessAlterTableMsg(pVnode, pMsg);
903✔
554
    } break;
903✔
555
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
8,591✔
556
    case TDMT_VND_DROP_TTL_TABLE: {
557
      code = vnodePreProcessDropTtlMsg(pVnode, pMsg);
8,591✔
558
    } break;
8,578✔
559
    case TDMT_VND_SUBMIT: {
774,630✔
560
      code = vnodePreProcessSubmitMsg(pVnode, pMsg);
774,630✔
561
    } break;
774,622✔
562
    case TDMT_VND_DELETE: {
1,193✔
563
      code = vnodePreProcessDeleteMsg(pVnode, pMsg);
1,193✔
564
    } break;
1,195✔
565
    case TDMT_VND_BATCH_DEL: {
589✔
566
      code = vnodePreProcessBatchDeleteMsg(pVnode, pMsg);
589✔
567
    } break;
589✔
568
    case TDMT_VND_ARB_CHECK_SYNC: {
×
569
      code = vnodePreProcessArbCheckSyncMsg(pVnode, pMsg);
×
570
    } break;
×
571
    case TDMT_VND_DROP_TABLE: {
1,007✔
572
      code = vnodePreProcessDropTbMsg(pVnode, pMsg);
1,007✔
573
    } break;
1,007✔
574
    default:
31,203✔
575
      break;
31,203✔
576
  }
577

578
  if (code && code != TSDB_CODE_MSG_PREPROCESSED) {
833,417✔
579
    vError("vgId:%d, failed to preprocess write request since %s, msg type:%s", TD_VID(pVnode), tstrerror(code),
6!
580
           TMSG_INFO(pMsg->msgType));
581
  }
582
  return code;
833,395✔
583
}
584

585
int32_t vnodeProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg, int64_t ver, SRpcMsg *pRsp) {
1,102,296✔
586
  int32_t code = 0;
1,102,296✔
587
  void   *ptr = NULL;
1,102,296✔
588
  void   *pReq;
589
  int32_t len;
590

591
  (void)taosThreadMutexLock(&pVnode->mutex);
1,102,296✔
592
  if (pVnode->disableWrite) {
1,102,443!
593
    (void)taosThreadMutexUnlock(&pVnode->mutex);
×
594
    vError("vgId:%d write is disabled for snapshot, version:%" PRId64, TD_VID(pVnode), ver);
×
595
    return TSDB_CODE_VND_WRITE_DISABLED;
×
596
  }
597
  (void)taosThreadMutexUnlock(&pVnode->mutex);
1,102,443✔
598

599
  if (ver <= pVnode->state.applied) {
1,102,441!
600
    vError("vgId:%d, duplicate write request. ver: %" PRId64 ", applied: %" PRId64 "", TD_VID(pVnode), ver,
×
601
           pVnode->state.applied);
602
    return terrno = TSDB_CODE_VND_DUP_REQUEST;
×
603
  }
604

605
  vDebug("vgId:%d, start to process write request %s, index:%" PRId64 ", applied:%" PRId64 ", state.applyTerm:%" PRId64
1,102,441!
606
         ", conn.applyTerm:%" PRId64,
607
         TD_VID(pVnode), TMSG_INFO(pMsg->msgType), ver, pVnode->state.applied, pVnode->state.applyTerm,
608
         pMsg->info.conn.applyTerm);
609

610
  if (!(pVnode->state.applyTerm <= pMsg->info.conn.applyTerm)) {
1,102,375!
611
    return terrno = TSDB_CODE_INTERNAL_ERROR;
×
612
  }
613

614
  if (!(pVnode->state.applied + 1 == ver)) {
1,102,375!
615
    return terrno = TSDB_CODE_INTERNAL_ERROR;
×
616
  }
617

618
  atomic_store_64(&pVnode->state.applied, ver);
1,102,375✔
619
  atomic_store_64(&pVnode->state.applyTerm, pMsg->info.conn.applyTerm);
1,102,457✔
620

621
  if (!syncUtilUserCommit(pMsg->msgType)) goto _exit;
1,102,458✔
622

623
  // skip header
624
  pReq = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
1,092,996✔
625
  len = pMsg->contLen - sizeof(SMsgHead);
1,092,996✔
626
  bool needCommit = false;
1,092,996✔
627

628
  switch (pMsg->msgType) {
1,092,996!
629
    /* META */
630
    case TDMT_VND_CREATE_STB:
11,966✔
631
      if (vnodeProcessCreateStbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
11,966✔
632
      break;
11,971✔
633
    case TDMT_VND_ALTER_STB:
1,256✔
634
      if (vnodeProcessAlterStbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
1,256!
635
      break;
1,256✔
636
    case TDMT_VND_DROP_STB:
307✔
637
      if (vnodeProcessDropStbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
307!
638
      break;
309✔
639
    case TDMT_VND_CREATE_TABLE:
17,766✔
640
      if (vnodeProcessCreateTbReq(pVnode, ver, pReq, len, pRsp, pMsg) < 0) goto _err;
17,766!
641
      break;
17,768✔
642
    case TDMT_VND_ALTER_TABLE:
903✔
643
      if (vnodeProcessAlterTbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
903!
644
      break;
903✔
645
    case TDMT_VND_DROP_TABLE:
1,007✔
646
      if (vnodeProcessDropTbReq(pVnode, ver, pReq, len, pRsp, pMsg) < 0) goto _err;
1,007!
647
      break;
1,007✔
648
    case TDMT_VND_DROP_TTL_TABLE:
×
649
      if (vnodeProcessDropTtlTbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
650
      break;
×
651
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
18✔
652
      if (vnodeProcessFetchTtlExpiredTbs(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
18!
653
      break;
18✔
UNCOV
654
    case TDMT_VND_TRIM:
×
UNCOV
655
      if (vnodeProcessTrimReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
UNCOV
656
      break;
×
657
    case TDMT_VND_S3MIGRATE:
×
658
      if (vnodeProcessS3MigrateReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
659
      break;
×
660
    case TDMT_VND_CREATE_SMA:
17✔
661
      if (vnodeProcessCreateTSmaReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
17!
662
      break;
17✔
663
    /* TSDB */
664
    case TDMT_VND_SUBMIT:
1,029,565✔
665
      if (vnodeProcessSubmitReq(pVnode, ver, pMsg->pCont, pMsg->contLen, pRsp, pMsg) < 0) goto _err;
1,029,565✔
666
      break;
1,029,379✔
667
    case TDMT_VND_DELETE:
1,193✔
668
      if (vnodeProcessDeleteReq(pVnode, ver, pReq, len, pRsp, pMsg) < 0) goto _err;
1,193!
669
      break;
1,194✔
670
    case TDMT_VND_BATCH_DEL:
589✔
671
      if (vnodeProcessBatchDeleteReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
589!
672
      break;
589✔
673
    /* TQ */
674
    case TDMT_VND_TMQ_SUBSCRIBE:
2,423✔
675
      if (tqProcessSubscribeReq(pVnode->pTq, ver, pReq, len) < 0) {
2,423!
UNCOV
676
        goto _err;
×
677
      }
678
      break;
2,424✔
679
    case TDMT_VND_TMQ_DELETE_SUB:
378✔
680
      if (tqProcessDeleteSubReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen) < 0) {
378!
681
        goto _err;
×
682
      }
683
      break;
378✔
684
    case TDMT_VND_TMQ_COMMIT_OFFSET:
10,500✔
685
      if (tqProcessOffsetCommitReq(pVnode->pTq, ver, pReq, len) < 0) {
10,500!
686
        goto _err;
×
687
      }
688
      break;
10,502✔
689
    case TDMT_VND_TMQ_ADD_CHECKINFO:
68✔
690
      if (tqProcessAddCheckInfoReq(pVnode->pTq, ver, pReq, len) < 0) {
68!
691
        goto _err;
×
692
      }
693
      break;
69✔
694
    case TDMT_VND_TMQ_DEL_CHECKINFO:
6✔
695
      if (tqProcessDelCheckInfoReq(pVnode->pTq, ver, pReq, len) < 0) {
6✔
696
        goto _err;
2✔
697
      }
698
      break;
4✔
699
    case TDMT_STREAM_TASK_DEPLOY: {
3,220✔
700
      if ((code = tqProcessTaskDeployReq(pVnode->pTq, ver, pReq, len)) != TSDB_CODE_SUCCESS) {
3,220!
701
        goto _err;
×
702
      }
703
    } break;
3,220✔
704
    case TDMT_STREAM_TASK_DROP: {
1,286✔
705
      if ((code = tqProcessTaskDropReq(pVnode->pTq, pMsg->pCont, pMsg->contLen)) < 0) {
1,286!
706
        goto _err;
×
707
      }
708
    } break;
1,288✔
709
    case TDMT_STREAM_TASK_UPDATE_CHKPT: {
430✔
710
      if ((code = tqProcessTaskUpdateCheckpointReq(pVnode->pTq, pMsg->pCont, pMsg->contLen)) < 0) {
430!
711
        goto _err;
×
712
      }
713
    } break;
430✔
714
    case TDMT_STREAM_CONSEN_CHKPT: {
30✔
715
      if (pVnode->restored && (code = tqProcessTaskConsenChkptIdReq(pVnode->pTq, pMsg)) < 0) {
30!
716
        goto _err;
×
717
      }
718

719
    } break;
30✔
720
    case TDMT_STREAM_TASK_PAUSE: {
37✔
721
      if (pVnode->restored && vnodeIsLeader(pVnode) &&
74!
722
          (code = tqProcessTaskPauseReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen)) < 0) {
37✔
723
        goto _err;
×
724
      }
725
    } break;
37✔
726
    case TDMT_STREAM_TASK_RESUME: {
30✔
727
      if (pVnode->restored && vnodeIsLeader(pVnode) &&
60!
728
          (code = tqProcessTaskResumeReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen)) < 0) {
30✔
729
        goto _err;
×
730
      }
731
    } break;
30✔
732
    case TDMT_VND_STREAM_TASK_RESET: {
×
733
      if (pVnode->restored && vnodeIsLeader(pVnode) &&
×
734
           (code = tqProcessTaskResetReq(pVnode->pTq, pMsg)) < 0) {
×
735
          goto _err;
×
736
        }
737

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

783
  vTrace("vgId:%d, process %s request, code:0x%x index:%" PRId64, TD_VID(pVnode), TMSG_INFO(pMsg->msgType), pRsp->code,
1,092,817!
784
         ver);
785

786
  walApplyVer(pVnode->pWal, ver);
1,092,817✔
787

788
  code = tqPushMsg(pVnode->pTq, pMsg->msgType);
1,092,767✔
789
  if (code) {
1,092,876✔
790
    vError("vgId:%d, failed to push msg to TQ since %s", TD_VID(pVnode), tstrerror(terrno));
395!
791
    return code;
395✔
792
  }
793

794
  // commit if need
795
  if (needCommit) {
1,092,481✔
796
    vInfo("vgId:%d, commit at version %" PRId64, TD_VID(pVnode), ver);
7,330!
797
    code = vnodeAsyncCommit(pVnode);
7,337✔
798
    if (code) {
7,340!
799
      vError("vgId:%d, failed to vnode async commit since %s.", TD_VID(pVnode), tstrerror(terrno));
×
800
      goto _err;
×
801
    }
802

803
    // start a new one
804
    code = vnodeBegin(pVnode);
7,340✔
805
    if (code) {
7,338✔
806
      vError("vgId:%d, failed to begin vnode since %s.", TD_VID(pVnode), tstrerror(terrno));
15!
807
      goto _err;
×
808
    }
809
  }
810

811
_exit:
1,092,474✔
812
  return 0;
1,101,812✔
813

814
_err:
13✔
815
  vError("vgId:%d, process %s request failed since %s, ver:%" PRId64, TD_VID(pVnode), TMSG_INFO(pMsg->msgType),
13!
816
         tstrerror(code), ver);
817
  return code;
13✔
818
}
819

820
int32_t vnodePreprocessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg) {
432,093✔
821
  if (TDMT_SCH_QUERY != pMsg->msgType && TDMT_SCH_MERGE_QUERY != pMsg->msgType) {
432,093✔
822
    return 0;
81,182✔
823
  }
824

825
  return qWorkerPreprocessQueryMsg(pVnode->pQuery, pMsg, TDMT_SCH_QUERY == pMsg->msgType);
350,911✔
826
}
827

828
int32_t vnodeProcessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
431,848✔
829
  vTrace("message in vnode query queue is processing");
431,848✔
830
  if ((pMsg->msgType == TDMT_SCH_QUERY || pMsg->msgType == TDMT_VND_TMQ_CONSUME) && !syncIsReadyForRead(pVnode->sync)) {
431,848✔
831
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
482✔
832
    return 0;
482✔
833
  }
834

835
  if (pMsg->msgType == TDMT_VND_TMQ_CONSUME && !pVnode->restored) {
431,365!
836
    vnodeRedirectRpcMsg(pVnode, pMsg, TSDB_CODE_SYN_RESTORING);
×
837
    return 0;
×
838
  }
839

840
  SReadHandle handle = {.vnode = pVnode, .pMsgCb = &pVnode->msgCb, .pWorkerCb = pInfo->workerCb};
431,365✔
841
  initStorageAPI(&handle.api);
431,365✔
842

843
  switch (pMsg->msgType) {
431,305!
844
    case TDMT_SCH_QUERY:
350,374✔
845
    case TDMT_SCH_MERGE_QUERY:
846
      return qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
350,374✔
847
    case TDMT_SCH_QUERY_CONTINUE:
1,849✔
848
      return qWorkerProcessCQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
1,849✔
849
    case TDMT_VND_TMQ_CONSUME:
57,735✔
850
      return tqProcessPollReq(pVnode->pTq, pMsg);
57,735✔
851
    case TDMT_VND_TMQ_CONSUME_PUSH:
21,369✔
852
      return tqProcessPollPush(pVnode->pTq, pMsg);
21,369✔
853
    default:
×
854
      vError("unknown msg type:%d in query queue", pMsg->msgType);
×
855
      return TSDB_CODE_APP_ERROR;
×
856
  }
857
}
858

859
int32_t vnodeProcessFetchMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
973,930✔
860
  vTrace("vgId:%d, msg:%p in fetch queue is processing", pVnode->config.vgId, pMsg);
973,930✔
861
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
973,933✔
862
       pMsg->msgType == TDMT_VND_BATCH_META || pMsg->msgType == TDMT_VND_TABLE_NAME) &&
679,779✔
863
      !syncIsReadyForRead(pVnode->sync)) {
309,477✔
864
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
700✔
865
    return 0;
700✔
866
  }
867

868
  switch (pMsg->msgType) {
973,170!
869
    case TDMT_SCH_FETCH:
335,407✔
870
    case TDMT_SCH_MERGE_FETCH:
871
      return qWorkerProcessFetchMsg(pVnode, pVnode->pQuery, pMsg, 0);
335,407✔
872
    case TDMT_SCH_FETCH_RSP:
×
873
      return qWorkerProcessRspMsg(pVnode, pVnode->pQuery, pMsg, 0);
×
874
    // case TDMT_SCH_CANCEL_TASK:
875
    //   return qWorkerProcessCancelMsg(pVnode, pVnode->pQuery, pMsg, 0);
876
    case TDMT_SCH_DROP_TASK:
350,809✔
877
      return qWorkerProcessDropMsg(pVnode, pVnode->pQuery, pMsg, 0);
350,809✔
878
    case TDMT_SCH_TASK_NOTIFY:
1✔
879
      return qWorkerProcessNotifyMsg(pVnode, pVnode->pQuery, pMsg, 0);
1✔
880
    case TDMT_SCH_QUERY_HEARTBEAT:
272,204✔
881
      return qWorkerProcessHbMsg(pVnode, pVnode->pQuery, pMsg, 0);
272,204✔
882
    case TDMT_VND_TABLE_META:
327✔
883
    case TDMT_VND_TABLE_NAME:
884
      return vnodeGetTableMeta(pVnode, pMsg, true);
327✔
885
    case TDMT_VND_TABLE_CFG:
×
886
      return vnodeGetTableCfg(pVnode, pMsg, true);
×
887
    case TDMT_VND_BATCH_META:
14,381✔
888
      return vnodeGetBatchMeta(pVnode, pMsg);
14,381✔
889
#ifdef TD_ENTERPRISE
890
    case TDMT_VND_QUERY_COMPACT_PROGRESS:
12✔
891
      return vnodeQueryCompactProgress(pVnode, pMsg);
12✔
892
#endif
893
      //    case TDMT_VND_TMQ_CONSUME:
894
      //      return tqProcessPollReq(pVnode->pTq, pMsg);
895
    case TDMT_VND_TMQ_VG_WALINFO:
9✔
896
      return tqProcessVgWalInfoReq(pVnode->pTq, pMsg);
9✔
UNCOV
897
    case TDMT_VND_TMQ_VG_COMMITTEDINFO:
×
UNCOV
898
      return tqProcessVgCommittedInfoReq(pVnode->pTq, pMsg);
×
899
    case TDMT_VND_TMQ_SEEK:
20✔
900
      return tqProcessSeekReq(pVnode->pTq, pMsg);
20✔
901

902
    default:
×
903
      vError("unknown msg type:%d in fetch queue", pMsg->msgType);
×
904
      return TSDB_CODE_APP_ERROR;
×
905
  }
906
}
907

908
int32_t vnodeProcessStreamMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
67,719✔
909
  vTrace("vgId:%d, msg:%p in fetch queue is processing", pVnode->config.vgId, pMsg);
67,719✔
910
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
67,720!
911
       pMsg->msgType == TDMT_VND_BATCH_META) &&
67,749!
912
      !syncIsReadyForRead(pVnode->sync)) {
×
913
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
×
914
    return 0;
×
915
  }
916

917
  switch (pMsg->msgType) {
67,750!
918
    case TDMT_STREAM_TASK_RUN:
42,105✔
919
      return tqProcessTaskRunReq(pVnode->pTq, pMsg);
42,105✔
920
    case TDMT_STREAM_TASK_DISPATCH:
5,578✔
921
      return tqProcessTaskDispatchReq(pVnode->pTq, pMsg);
5,578✔
922
    case TDMT_STREAM_TASK_DISPATCH_RSP:
5,651✔
923
      return tqProcessTaskDispatchRsp(pVnode->pTq, pMsg);
5,651✔
924
    case TDMT_VND_STREAM_TASK_CHECK:
3,873✔
925
      return tqProcessTaskCheckReq(pVnode->pTq, pMsg);
3,873✔
926
    case TDMT_VND_STREAM_TASK_CHECK_RSP:
3,870✔
927
      return tqProcessTaskCheckRsp(pVnode->pTq, pMsg);
3,870✔
928
    case TDMT_STREAM_RETRIEVE:
300✔
929
      return tqProcessTaskRetrieveReq(pVnode->pTq, pMsg);
300✔
930
    case TDMT_STREAM_RETRIEVE_RSP:
294✔
931
      return tqProcessTaskRetrieveRsp(pVnode->pTq, pMsg);
294✔
932
    case TDMT_VND_STREAM_SCAN_HISTORY:
427✔
933
      return tqProcessTaskScanHistory(pVnode->pTq, pMsg);
427✔
934
    case TDMT_STREAM_TASK_CHECKPOINT_READY:
814✔
935
      return tqProcessTaskCheckpointReadyMsg(pVnode->pTq, pMsg);
814✔
936
    case TDMT_STREAM_TASK_CHECKPOINT_READY_RSP:
810✔
937
      return tqProcessTaskCheckpointReadyRsp(pVnode->pTq, pMsg);
810✔
938
    case TDMT_STREAM_RETRIEVE_TRIGGER:
×
939
      return tqProcessTaskRetrieveTriggerReq(pVnode->pTq, pMsg);
×
940
    case TDMT_STREAM_RETRIEVE_TRIGGER_RSP:
×
941
      return tqProcessTaskRetrieveTriggerRsp(pVnode->pTq, pMsg);
×
942
    case TDMT_MND_STREAM_HEARTBEAT_RSP:
2,345✔
943
      return tqProcessStreamHbRsp(pVnode->pTq, pMsg);
2,345✔
944
    case TDMT_MND_STREAM_REQ_CHKPT_RSP:
854✔
945
      return tqProcessStreamReqCheckpointRsp(pVnode->pTq, pMsg);
854✔
946
    case TDMT_VND_GET_STREAM_PROGRESS:
×
947
      return tqStreamProgressRetrieveReq(pVnode->pTq, pMsg);
×
948
    case TDMT_MND_STREAM_CHKPT_REPORT_RSP:
829✔
949
      return tqProcessTaskChkptReportRsp(pVnode->pTq, pMsg);
829✔
950
    default:
×
951
      vError("unknown msg type:%d in stream queue", pMsg->msgType);
×
952
      return TSDB_CODE_APP_ERROR;
×
953
  }
954
}
955

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

963
void vnodeUpdateMetaRsp(SVnode *pVnode, STableMetaRsp *pMetaRsp) {
29,838✔
964
  if (NULL == pMetaRsp) {
29,838!
965
    return;
×
966
  }
967

968
  strcpy(pMetaRsp->dbFName, pVnode->config.dbname);
29,838✔
969
  pMetaRsp->dbId = pVnode->config.dbId;
29,838✔
970
  pMetaRsp->vgId = TD_VID(pVnode);
29,838✔
971
  pMetaRsp->precision = pVnode->config.tsdbCfg.precision;
29,838✔
972
}
973

974
extern int32_t vnodeAsyncRetention(SVnode *pVnode, int64_t now);
975

UNCOV
976
static int32_t vnodeProcessTrimReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
UNCOV
977
  int32_t     code = 0;
×
UNCOV
978
  SVTrimDbReq trimReq = {0};
×
979

980
  // decode
UNCOV
981
  if (tDeserializeSVTrimDbReq(pReq, len, &trimReq) != 0) {
×
982
    code = TSDB_CODE_INVALID_MSG;
×
983
    goto _exit;
×
984
  }
985

UNCOV
986
  vInfo("vgId:%d, trim vnode request will be processed, time:%d", pVnode->config.vgId, trimReq.timestamp);
×
987

UNCOV
988
  code = vnodeAsyncRetention(pVnode, trimReq.timestamp);
×
989

UNCOV
990
_exit:
×
UNCOV
991
  return code;
×
992
}
993

994
extern int32_t vnodeAsyncS3Migrate(SVnode *pVnode, int64_t now);
995

996
static int32_t vnodeProcessS3MigrateReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
997
  int32_t          code = 0;
×
998
  SVS3MigrateDbReq s3migrateReq = {0};
×
999

1000
  // decode
1001
  if (tDeserializeSVS3MigrateDbReq(pReq, len, &s3migrateReq) != 0) {
×
1002
    code = TSDB_CODE_INVALID_MSG;
×
1003
    goto _exit;
×
1004
  }
1005

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

1008
  code = vnodeAsyncS3Migrate(pVnode, s3migrateReq.timestamp);
×
1009

1010
_exit:
×
1011
  return code;
×
1012
}
1013

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

1022
  if (ttlReq.nUids != taosArrayGetSize(ttlReq.pTbUids)) {
×
1023
    ret = TSDB_CODE_INVALID_MSG;
×
1024
    goto end;
×
1025
  }
1026

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

1032
  if (ttlReq.nUids > 0) {
×
1033
    int32_t code = metaDropTables(pVnode->pMeta, ttlReq.pTbUids);
×
1034
    if (code) return code;
×
1035

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

1042
end:
×
1043
  taosArrayDestroy(ttlReq.pTbUids);
×
1044
  return ret;
×
1045
}
1046

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

1059
  if (tDeserializeSVDropTtlTableReq(pReq, len, &ttlReq) != 0) {
18!
1060
    terrno = TSDB_CODE_INVALID_MSG;
×
1061
    goto _end;
×
1062
  }
1063

1064
  if (!(ttlReq.nUids == taosArrayGetSize(ttlReq.pTbUids))) {
18!
1065
    terrno = TSDB_CODE_INVALID_MSG;
×
1066
    goto _end;
×
1067
  }
1068

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

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

1094
    expiredTb.name = p;
74✔
1095
    if (mr.me.type == TSDB_CHILD_TABLE) {
74✔
1096
      expiredTb.suid = mr.me.ctbEntry.suid;
48✔
1097
    }
1098

1099
    if (taosArrayPush(rsp.pExpiredTbs, &expiredTb) == NULL) {
148!
1100
      goto _end;
×
1101
    }
1102
  }
1103

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

1116
  if (terrno == 0) code = 0;
18!
1117
_end:
×
1118
  metaReaderClear(&mr);
18✔
1119
  tFreeFetchTtlExpiredTbsRsp(&rsp);
18✔
1120
  taosArrayDestroy(ttlReq.pTbUids);
18✔
1121
  if (pNames) taosArrayDestroy(pNames);
18!
1122
  pRsp->code = terrno;
18✔
1123
  return code;
18✔
1124
}
1125

1126
static int32_t vnodeProcessCreateStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
11,963✔
1127
  int32_t        code = 0;
11,963✔
1128
  SVCreateStbReq req = {0};
11,963✔
1129
  SDecoder       coder;
1130

1131
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
11,963✔
1132
  pRsp->code = TSDB_CODE_SUCCESS;
11,963✔
1133
  pRsp->pCont = NULL;
11,963✔
1134
  pRsp->contLen = 0;
11,963✔
1135

1136
  // decode and process req
1137
  tDecoderInit(&coder, pReq, len);
11,963✔
1138

1139
  code = tDecodeSVCreateStbReq(&coder, &req);
11,972✔
1140
  if (code) {
11,961!
1141
    pRsp->code = code;
×
1142
    goto _err;
×
1143
  }
1144

1145
  code = metaCreateSTable(pVnode->pMeta, ver, &req);
11,961✔
1146
  if (code) {
11,967✔
1147
    pRsp->code = code;
3✔
1148
    goto _err;
3✔
1149
  }
1150

1151
  if ((code = tdProcessRSmaCreate(pVnode->pSma, &req)) < 0) {
11,964!
1152
    pRsp->code = code;
×
1153
    goto _err;
×
1154
  }
1155

1156
  tDecoderClear(&coder);
11,986✔
1157
  return 0;
11,980✔
1158

1159
_err:
3✔
1160
  tDecoderClear(&coder);
3✔
1161
  return code;
3✔
1162
}
1163

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

1182
  // decode
1183
  tDecoderInit(&decoder, pReq, len);
17,766✔
1184
  if (tDecodeSVCreateTbBatchReq(&decoder, &req) < 0) {
17,766!
1185
    rcode = -1;
×
1186
    terrno = TSDB_CODE_INVALID_MSG;
×
1187
    goto _exit;
×
1188
  }
1189

1190
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(cRsp));
17,768✔
1191
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
17,768✔
1192
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
17,768✔
1193
  if (rsp.pArray == NULL || tbUids == NULL || tbNames == NULL) {
17,768!
UNCOV
1194
    rcode = -1;
×
UNCOV
1195
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1196
    goto _exit;
×
1197
  }
1198

1199
  // loop to create table
1200
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
43,808✔
1201
    pCreateReq = req.pReqs + iReq;
26,047✔
1202
    memset(&cRsp, 0, sizeof(cRsp));
26,047✔
1203

1204
    if (tsEnableAudit && tsEnableAuditCreateTable) {
26,047!
1205
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
26,046✔
1206
      if (str == NULL) {
26,061!
1207
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1208
        rcode = -1;
×
1209
        goto _exit;
×
1210
      }
1211
      strcpy(str, pCreateReq->name);
26,061✔
1212
      if (taosArrayPush(tbNames, &str) == NULL) {
26,059!
1213
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1214
        rcode = -1;
×
1215
        goto _exit;
×
1216
      }
1217
    }
1218

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

1232
    // do create table
1233
    if (metaCreateTable(pVnode->pMeta, ver, pCreateReq, &cRsp.pMeta) < 0) {
26,058✔
1234
      if (pCreateReq->flags & TD_CREATE_IF_NOT_EXISTS && terrno == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
517!
1235
        cRsp.code = TSDB_CODE_SUCCESS;
3✔
1236
      } else {
1237
        cRsp.code = terrno;
514✔
1238
      }
1239
    } else {
1240
      cRsp.code = TSDB_CODE_SUCCESS;
25,536✔
1241
      if (tdFetchTbUidList(pVnode->pSma, &pStore, pCreateReq->ctb.suid, pCreateReq->uid) < 0) {
25,536!
1242
        vError("vgId:%d, failed to fetch tbUid list", TD_VID(pVnode));
×
1243
      }
1244
      if (taosArrayPush(tbUids, &pCreateReq->uid) == NULL) {
51,067!
1245
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1246
        rcode = -1;
×
1247
        goto _exit;
×
1248
      }
1249
      vnodeUpdateMetaRsp(pVnode, cRsp.pMeta);
25,531✔
1250
    }
1251

1252
    if (taosArrayPush(rsp.pArray, &cRsp) == NULL) {
52,087!
1253
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1254
      rcode = -1;
×
1255
      goto _exit;
×
1256
    }
1257
  }
1258

1259
  vDebug("vgId:%d, add %d new created tables into query table list", TD_VID(pVnode), (int32_t)taosArrayGetSize(tbUids));
17,761✔
1260
  if (tqUpdateTbUidList(pVnode->pTq, tbUids, true) < 0) {
17,761!
1261
    vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(terrno));
×
1262
  }
1263
  if (tdUpdateTbUidList(pVnode->pSma, pStore, true) < 0) {
17,768!
1264
    goto _exit;
×
1265
  }
1266
  pStore = tdUidStoreFree(pStore);
17,767✔
1267

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

1282
  if (tsEnableAudit && tsEnableAuditCreateTable) {
17,768!
1283
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
17,767✔
1284

1285
    SName name = {0};
17,767✔
1286
    if (tNameFromString(&name, pVnode->config.dbname, T_NAME_ACCT | T_NAME_DB) < 0) {
17,767!
1287
      vError("vgId:%d, failed to get name from string", TD_VID(pVnode));
×
1288
    }
1289

1290
    SStringBuilder sb = {0};
17,766✔
1291
    for (int32_t i = 0; i < tbNames->size; i++) {
43,823✔
1292
      char **key = (char **)taosArrayGet(tbNames, i);
26,059✔
1293
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
26,059✔
1294
      if (i < tbNames->size - 1) {
26,059✔
1295
        taosStringBuilderAppendChar(&sb, ',');
8,293✔
1296
      }
1297
      // taosMemoryFreeClear(*key);
1298
    }
1299

1300
    size_t len = 0;
17,764✔
1301
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
17,764✔
1302

1303
    if (pOriginRpc->info.conn.user != NULL && strlen(pOriginRpc->info.conn.user) > 0) {
17,766!
1304
      auditAddRecord(pOriginRpc, clusterId, "createTable", name.dbname, "", keyJoined, len);
14,344✔
1305
    }
1306

1307
    taosStringBuilderDestroy(&sb);
17,766✔
1308
  }
1309

1310
_exit:
1✔
1311
  tDeleteSVCreateTbBatchReq(&req);
17,768✔
1312
  taosArrayDestroyEx(rsp.pArray, tFreeSVCreateTbRsp);
17,768✔
1313
  taosArrayDestroy(tbUids);
17,767✔
1314
  tDecoderClear(&decoder);
17,768✔
1315
  tEncoderClear(&encoder);
17,768✔
1316
  taosArrayDestroyP(tbNames, taosMemoryFree);
17,768✔
1317
  return rcode;
17,768✔
1318
}
1319

1320
static int32_t vnodeProcessAlterStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
1,255✔
1321
  int32_t        code = 0;
1,255✔
1322
  SVCreateStbReq req = {0};
1,255✔
1323
  SDecoder       dc = {0};
1,255✔
1324

1325
  pRsp->msgType = TDMT_VND_ALTER_STB_RSP;
1,255✔
1326
  pRsp->code = TSDB_CODE_SUCCESS;
1,255✔
1327
  pRsp->pCont = NULL;
1,255✔
1328
  pRsp->contLen = 0;
1,255✔
1329

1330
  tDecoderInit(&dc, pReq, len);
1,255✔
1331

1332
  // decode req
1333
  code = tDecodeSVCreateStbReq(&dc, &req);
1,255✔
1334
  if (code) {
1,258!
1335
    tDecoderClear(&dc);
×
1336
    return code;
×
1337
  }
1338

1339
  code = metaAlterSTable(pVnode->pMeta, ver, &req);
1,258✔
1340
  if (code) {
1,256!
1341
    pRsp->code = code;
×
1342
    tDecoderClear(&dc);
×
1343
    return code;
×
1344
  }
1345

1346
  tDecoderClear(&dc);
1,256✔
1347

1348
  return 0;
1,259✔
1349
}
1350

1351
static int32_t vnodeProcessDropStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
307✔
1352
  SVDropStbReq req = {0};
307✔
1353
  int32_t      rcode = TSDB_CODE_SUCCESS;
307✔
1354
  SDecoder     decoder = {0};
307✔
1355
  SArray      *tbUidList = NULL;
307✔
1356

1357
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
307✔
1358
  pRsp->pCont = NULL;
307✔
1359
  pRsp->contLen = 0;
307✔
1360

1361
  // decode request
1362
  tDecoderInit(&decoder, pReq, len);
307✔
1363
  if (tDecodeSVDropStbReq(&decoder, &req) < 0) {
306!
1364
    rcode = TSDB_CODE_INVALID_MSG;
×
1365
    goto _exit;
×
1366
  }
1367

1368
  // process request
1369
  tbUidList = taosArrayInit(8, sizeof(int64_t));
310✔
1370
  if (tbUidList == NULL) goto _exit;
310!
1371
  if (metaDropSTable(pVnode->pMeta, ver, &req, tbUidList) < 0) {
310✔
1372
    rcode = terrno;
13✔
1373
    goto _exit;
13✔
1374
  }
1375

1376
  if (tqUpdateTbUidList(pVnode->pTq, tbUidList, false) < 0) {
297!
1377
    rcode = terrno;
×
1378
    goto _exit;
×
1379
  }
1380

1381
  if (tdProcessRSmaDrop(pVnode->pSma, &req) < 0) {
296!
1382
    rcode = terrno;
×
1383
    goto _exit;
×
1384
  }
1385

1386
  // return rsp
1387
_exit:
297✔
1388
  if (tbUidList) taosArrayDestroy(tbUidList);
310!
1389
  pRsp->code = rcode;
309✔
1390
  tDecoderClear(&decoder);
309✔
1391
  return 0;
310✔
1392
}
1393

1394
static int32_t vnodeProcessAlterTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
903✔
1395
  SVAlterTbReq  vAlterTbReq = {0};
903✔
1396
  SVAlterTbRsp  vAlterTbRsp = {0};
903✔
1397
  SDecoder      dc = {0};
903✔
1398
  int32_t       rcode = 0;
903✔
1399
  int32_t       ret;
1400
  SEncoder      ec = {0};
903✔
1401
  STableMetaRsp vMetaRsp = {0};
903✔
1402

1403
  pRsp->msgType = TDMT_VND_ALTER_TABLE_RSP;
903✔
1404
  pRsp->pCont = NULL;
903✔
1405
  pRsp->contLen = 0;
903✔
1406
  pRsp->code = TSDB_CODE_SUCCESS;
903✔
1407

1408
  tDecoderInit(&dc, pReq, len);
903✔
1409

1410
  // decode
1411
  if (tDecodeSVAlterTbReq(&dc, &vAlterTbReq) < 0) {
903!
1412
    vAlterTbRsp.code = TSDB_CODE_INVALID_MSG;
×
1413
    tDecoderClear(&dc);
×
1414
    rcode = -1;
×
1415
    goto _exit;
×
1416
  }
1417

1418
  // process
1419
  if (metaAlterTable(pVnode->pMeta, ver, &vAlterTbReq, &vMetaRsp) < 0) {
903✔
1420
    vAlterTbRsp.code = terrno;
44✔
1421
    tDecoderClear(&dc);
44✔
1422
    rcode = -1;
44✔
1423
    goto _exit;
44✔
1424
  }
1425
  tDecoderClear(&dc);
859✔
1426

1427
  if (NULL != vMetaRsp.pSchemas) {
859✔
1428
    vnodeUpdateMetaRsp(pVnode, &vMetaRsp);
187✔
1429
    vAlterTbRsp.pMeta = &vMetaRsp;
187✔
1430
  }
1431

1432
_exit:
672✔
1433
  tEncodeSize(tEncodeSVAlterTbRsp, &vAlterTbRsp, pRsp->contLen, ret);
903!
1434
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
903✔
1435
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
903✔
1436
  if (tEncodeSVAlterTbRsp(&ec, &vAlterTbRsp) != 0) {
903!
1437
    vError("vgId:%d, failed to encode alter table response", TD_VID(pVnode));
×
1438
  }
1439
  tEncoderClear(&ec);
903✔
1440
  if (vMetaRsp.pSchemas) {
903✔
1441
    taosMemoryFree(vMetaRsp.pSchemas);
187✔
1442
    taosMemoryFree(vMetaRsp.pSchemaExt);
187✔
1443
  }
1444
  return 0;
903✔
1445
}
1446

1447
static int32_t vnodeProcessDropTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
1,007✔
1448
                                     SRpcMsg *pOriginRpc) {
1449
  SVDropTbBatchReq req = {0};
1,007✔
1450
  SVDropTbBatchRsp rsp = {0};
1,007✔
1451
  SDecoder         decoder = {0};
1,007✔
1452
  SEncoder         encoder = {0};
1,007✔
1453
  int32_t          ret;
1454
  SArray          *tbUids = NULL;
1,007✔
1455
  STbUidStore     *pStore = NULL;
1,007✔
1456
  SArray          *tbNames = NULL;
1,007✔
1457

1458
  pRsp->msgType = TDMT_VND_DROP_TABLE_RSP;
1,007✔
1459
  pRsp->pCont = NULL;
1,007✔
1460
  pRsp->contLen = 0;
1,007✔
1461
  pRsp->code = TSDB_CODE_SUCCESS;
1,007✔
1462

1463
  // decode req
1464
  tDecoderInit(&decoder, pReq, len);
1,007✔
1465
  ret = tDecodeSVDropTbBatchReq(&decoder, &req);
1,007✔
1466
  if (ret < 0) {
1,007!
1467
    terrno = TSDB_CODE_INVALID_MSG;
×
1468
    pRsp->code = terrno;
×
1469
    goto _exit;
×
1470
  }
1471

1472
  // process req
1473
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
1,007✔
1474
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(SVDropTbRsp));
1,007✔
1475
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
1,007✔
1476
  if (tbUids == NULL || rsp.pArray == NULL || tbNames == NULL) goto _exit;
1,007!
1477

1478
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
2,025✔
1479
    SVDropTbReq *pDropTbReq = req.pReqs + iReq;
1,018✔
1480
    SVDropTbRsp  dropTbRsp = {0};
1,018✔
1481
    tb_uid_t     tbUid = 0;
1,018✔
1482

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

1500
    if (taosArrayPush(rsp.pArray, &dropTbRsp) == NULL) {
2,036!
1501
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1502
      pRsp->code = terrno;
×
1503
      goto _exit;
×
1504
    }
1505

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

1521
  if (tqUpdateTbUidList(pVnode->pTq, tbUids, false) < 0) {
1,007!
1522
    vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(terrno));
×
1523
  }
1524

1525
  if (tdUpdateTbUidList(pVnode->pSma, pStore, false) < 0) {
1,007!
1526
    goto _exit;
×
1527
  }
1528

1529
  if (tsEnableAuditCreateTable) {
1,007!
1530
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
1,007✔
1531

1532
    SName name = {0};
1,007✔
1533
    if (tNameFromString(&name, pVnode->config.dbname, T_NAME_ACCT | T_NAME_DB) != 0) {
1,007!
1534
      vError("vgId:%d, failed to get name from string", TD_VID(pVnode));
×
1535
    }
1536

1537
    SStringBuilder sb = {0};
1,007✔
1538
    for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
2,025✔
1539
      char **key = (char **)taosArrayGet(tbNames, iReq);
1,018✔
1540
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
1,018✔
1541
      if (iReq < req.nReqs - 1) {
1,018✔
1542
        taosStringBuilderAppendChar(&sb, ',');
11✔
1543
      }
1544
      taosMemoryFreeClear(*key);
1,018!
1545
    }
1546

1547
    size_t len = 0;
1,007✔
1548
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
1,007✔
1549

1550
    if (pOriginRpc->info.conn.user != NULL && strlen(pOriginRpc->info.conn.user) > 0) {
1,007!
1551
      auditAddRecord(pOriginRpc, clusterId, "dropTable", name.dbname, "", keyJoined, len);
1,007✔
1552
    }
1553

1554
    taosStringBuilderDestroy(&sb);
1,007✔
1555
  }
1556

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

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

1582
  tInitSubmitBlkIter(msgIter, pBlock, &blkIter);
1583
  if (blkIter.row == NULL) return 0;
1584

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

1592
  suid = msgIter->suid;
1593
  rv = TD_ROW_SVER(blkIter.row);
1594

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

1601
  taosMemoryFreeClear(pSchema);
1602

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

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

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

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

1655
  return TSDB_CODE_SUCCESS;
×
1656
}
1657

1658
static void vnodeDestroySubmitReqConvertCxt(SSubmitReqConvertCxt *pCxt) {
×
1659
  taosMemoryFreeClear(pCxt->pTbSchema);
×
1660
  tDestroySubmitTbData(pCxt->pTbData, TSDB_MSG_FLG_ENCODE);
×
1661
  taosMemoryFreeClear(pCxt->pTbData);
×
1662
  taosArrayDestroy(pCxt->pColValues);
×
1663
}
×
1664

1665
static int32_t vnodeCellValConvertToColVal(STColumn *pCol, SCellVal *pCellVal, SColVal *pColVal) {
×
1666
  if (tdValTypeIsNone(pCellVal->valType)) {
×
1667
    pColVal->flag = CV_FLAG_NONE;
×
1668
    return TSDB_CODE_SUCCESS;
×
1669
  }
1670

1671
  if (tdValTypeIsNull(pCellVal->valType)) {
×
1672
    pColVal->flag = CV_FLAG_NULL;
×
1673
    return TSDB_CODE_SUCCESS;
×
1674
  }
1675

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

1688
  pColVal->flag = CV_FLAG_VALUE;
×
1689
  return TSDB_CODE_SUCCESS;
×
1690
}
1691

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

1706
static int32_t vnodeDecodeCreateTbReq(SSubmitReqConvertCxt *pCxt) {
×
1707
  if (pCxt->msgIter.schemaLen <= 0) {
×
1708
    return TSDB_CODE_SUCCESS;
×
1709
  }
1710

1711
  pCxt->pTbData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
×
1712
  if (NULL == pCxt->pTbData->pCreateTbReq) {
×
1713
    return terrno;
×
1714
  }
1715

1716
  SDecoder decoder = {0};
×
1717
  tDecoderInit(&decoder, (uint8_t *)pCxt->pBlock->data, pCxt->msgIter.schemaLen);
×
1718
  int32_t code = tDecodeSVCreateTbReq(&decoder, pCxt->pTbData->pCreateTbReq);
×
1719
  tDecoderClear(&decoder);
×
1720

1721
  return code;
×
1722
}
1723

1724
static int32_t vnodeSubmitReqConvertToSubmitReq2(SVnode *pVnode, SSubmitReq *pReq, SSubmitReq2 *pReq2) {
×
1725
  pReq2->aSubmitTbData = taosArrayInit(128, sizeof(SSubmitTbData));
×
1726
  if (NULL == pReq2->aSubmitTbData) {
×
1727
    return terrno;
×
1728
  }
1729

1730
  SSubmitReqConvertCxt cxt = {0};
×
1731

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

1762
  vnodeDestroySubmitReqConvertCxt(&cxt);
×
1763
  return code;
×
1764
}
1765

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

1789
static int32_t vnodeProcessSubmitReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
1,029,513✔
1790
                                     SRpcMsg *pOriginalMsg) {
1791
  int32_t code = 0;
1,029,513✔
1792
  terrno = 0;
1,029,513✔
1793

1794
  SSubmitReq2 *pSubmitReq = &(SSubmitReq2){0};
1,029,544✔
1795
  SSubmitRsp2 *pSubmitRsp = &(SSubmitRsp2){0};
1,029,544✔
1796
  SArray      *newTbUids = NULL;
1,029,544✔
1797
  int32_t      ret;
1798
  SEncoder     ec = {0};
1,029,544✔
1799

1800
  pRsp->code = TSDB_CODE_SUCCESS;
1,029,544✔
1801

1802
  void           *pAllocMsg = NULL;
1,029,544✔
1803
  SSubmitReq2Msg *pMsg = (SSubmitReq2Msg *)pReq;
1,029,544✔
1804
  if (0 == pMsg->version) {
1,029,544!
1805
    code = vnodeSubmitReqConvertToSubmitReq2(pVnode, (SSubmitReq *)pMsg, pSubmitReq);
×
1806
    if (TSDB_CODE_SUCCESS == code) {
×
1807
      code = vnodeRebuildSubmitReqMsg(pSubmitReq, &pReq);
×
1808
    }
1809
    if (TSDB_CODE_SUCCESS == code) {
×
1810
      pAllocMsg = pReq;
×
1811
    }
1812
    if (TSDB_CODE_SUCCESS != code) {
×
1813
      goto _exit;
×
1814
    }
1815
  } else {
1816
    // decode
1817
    pReq = POINTER_SHIFT(pReq, sizeof(SSubmitReq2Msg));
1,029,544✔
1818
    len -= sizeof(SSubmitReq2Msg);
1,029,544✔
1819
    SDecoder dc = {0};
1,029,544✔
1820
    tDecoderInit(&dc, pReq, len);
1,029,544✔
1821
    if (tDecodeSubmitReq(&dc, pSubmitReq) < 0) {
1,029,453!
1822
      code = TSDB_CODE_INVALID_MSG;
×
1823
      goto _exit;
×
1824
    }
1825
    tDecoderClear(&dc);
1,029,299✔
1826
  }
1827

1828
  // scan
1829
  TSKEY now = taosGetTimestamp(pVnode->config.tsdbCfg.precision);
1,029,512✔
1830
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * pVnode->config.tsdbCfg.keep2;
1,029,443✔
1831
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
1,029,443✔
1832
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
2,094,946✔
1833
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
1,065,118✔
1834

1835
    if (pSubmitTbData->pCreateTbReq && pSubmitTbData->pCreateTbReq->uid == 0) {
1,065,116!
1836
      code = TSDB_CODE_INVALID_MSG;
×
1837
      goto _exit;
×
1838
    }
1839

1840
    if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
1,065,116✔
1841
      if (TARRAY_SIZE(pSubmitTbData->aCol) <= 0) {
129!
1842
        code = TSDB_CODE_INVALID_MSG;
×
1843
        goto _exit;
×
1844
      }
1845

1846
      SColData *colDataArr = TARRAY_DATA(pSubmitTbData->aCol);
129✔
1847
      SRowKey   lastKey;
1848
      tColDataArrGetRowKey(colDataArr, TARRAY_SIZE(pSubmitTbData->aCol), 0, &lastKey);
129✔
1849
      for (int32_t iRow = 1; iRow < colDataArr[0].nVal; iRow++) {
213✔
1850
        SRowKey key;
1851
        tColDataArrGetRowKey(TARRAY_DATA(pSubmitTbData->aCol), TARRAY_SIZE(pSubmitTbData->aCol), iRow, &key);
84✔
1852
        if (tRowKeyCompare(&lastKey, &key) >= 0) {
84!
1853
          code = TSDB_CODE_INVALID_MSG;
×
1854
          vError("vgId:%d %s failed 1 since %s, version:%" PRId64, TD_VID(pVnode), __func__, tstrerror(terrno), ver);
×
1855
          goto _exit;
×
1856
        }
1857
      }
1858
    } else {
1859
      int32_t nRow = TARRAY_SIZE(pSubmitTbData->aRowP);
1,064,987✔
1860
      SRow  **aRow = (SRow **)TARRAY_DATA(pSubmitTbData->aRowP);
1,064,987✔
1861
      SRowKey lastRowKey;
1862
      for (int32_t iRow = 0; iRow < nRow; ++iRow) {
39,186,424✔
1863
        if (aRow[iRow]->ts < minKey || aRow[iRow]->ts > maxKey) {
38,121,050✔
1864
          code = TSDB_CODE_INVALID_MSG;
6,981✔
1865
          vError("vgId:%d %s failed 2 since %s, version:%" PRId64, TD_VID(pVnode), __func__, tstrerror(code), ver);
6,981!
1866
          goto _exit;
×
1867
        }
1868
        if (iRow == 0) {
38,114,069✔
1869
          tRowGetKey(aRow[iRow], &lastRowKey);
1,064,954✔
1870
        } else {
1871
          SRowKey rowKey;
1872
          tRowGetKey(aRow[iRow], &rowKey);
37,049,115✔
1873

1874
          if (tRowKeyCompare(&lastRowKey, &rowKey) >= 0) {
37,049,120!
1875
            code = TSDB_CODE_INVALID_MSG;
×
1876
            vError("vgId:%d %s failed 3 since %s, version:%" PRId64, TD_VID(pVnode), __func__, tstrerror(code), ver);
×
1877
            goto _exit;
×
1878
          }
1879
          lastRowKey = rowKey;
37,056,454✔
1880
        }
1881
      }
1882
    }
1883
  }
1884

1885
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
2,095,054✔
1886
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
1,065,077✔
1887

1888
    if (pSubmitTbData->pCreateTbReq) {
1,065,042✔
1889
      pSubmitTbData->uid = pSubmitTbData->pCreateTbReq->uid;
21,014✔
1890
    } else {
1891
      SMetaInfo info = {0};
1,044,028✔
1892

1893
      code = metaGetInfo(pVnode->pMeta, pSubmitTbData->uid, &info, NULL);
1,044,028✔
1894
      if (code) {
1,044,171!
1895
        code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
1896
        vWarn("vgId:%d, table uid:%" PRId64 " not exists", TD_VID(pVnode), pSubmitTbData->uid);
×
UNCOV
1897
        goto _exit;
×
1898
      }
1899

1900
      if (info.suid != pSubmitTbData->suid) {
1,044,178!
1901
        code = TSDB_CODE_INVALID_MSG;
×
1902
        goto _exit;
×
1903
      }
1904

1905
      if (info.suid) {
1,044,178✔
1906
        if (metaGetInfo(pVnode->pMeta, info.suid, &info, NULL) != 0) {
686,691!
1907
          vWarn("vgId:%d, table uid:%" PRId64 " not exists", TD_VID(pVnode), info.suid);
×
1908
        }
1909
      }
1910

1911
      if (pSubmitTbData->sver != info.skmVer) {
1,044,212!
1912
        code = TSDB_CODE_TDB_INVALID_TABLE_SCHEMA_VER;
×
1913
        goto _exit;
×
1914
      }
1915
    }
1916

1917
    if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
1,065,226✔
1918
      int32_t   nColData = TARRAY_SIZE(pSubmitTbData->aCol);
129✔
1919
      SColData *aColData = (SColData *)TARRAY_DATA(pSubmitTbData->aCol);
129✔
1920

1921
      if (nColData <= 0) {
129!
1922
        code = TSDB_CODE_INVALID_MSG;
×
1923
        goto _exit;
×
1924
      }
1925

1926
      if (aColData[0].cid != PRIMARYKEY_TIMESTAMP_COL_ID || aColData[0].type != TSDB_DATA_TYPE_TIMESTAMP ||
129!
1927
          aColData[0].nVal <= 0) {
129!
1928
        code = TSDB_CODE_INVALID_MSG;
×
1929
        goto _exit;
×
1930
      }
1931

1932
      for (int32_t j = 1; j < nColData; j++) {
587✔
1933
        if (aColData[j].nVal != aColData[0].nVal) {
458!
1934
          code = TSDB_CODE_INVALID_MSG;
×
1935
          goto _exit;
×
1936
        }
1937
      }
1938
    }
1939
  }
1940

1941
  vDebug("vgId:%d, submit block size %d", TD_VID(pVnode), (int32_t)taosArrayGetSize(pSubmitReq->aSubmitTbData));
1,029,977✔
1942

1943
  // loop to handle
1944
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
2,094,736✔
1945
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
1,065,142✔
1946

1947
    // create table
1948
    if (pSubmitTbData->pCreateTbReq) {
1,065,123✔
1949
      // alloc if need
1950
      if (pSubmitRsp->aCreateTbRsp == NULL &&
21,010✔
1951
          (pSubmitRsp->aCreateTbRsp = taosArrayInit(TARRAY_SIZE(pSubmitReq->aSubmitTbData), sizeof(SVCreateTbRsp))) ==
20,552!
1952
              NULL) {
1953
        code = terrno;
×
1954
        goto _exit;
8✔
1955
      }
1956

1957
      SVCreateTbRsp *pCreateTbRsp = taosArrayReserve(pSubmitRsp->aCreateTbRsp, 1);
21,010✔
1958

1959
      // create table
1960
      if (metaCreateTable(pVnode->pMeta, ver, pSubmitTbData->pCreateTbReq, &pCreateTbRsp->pMeta) == 0) {
21,010✔
1961
        // create table success
1962

1963
        if (newTbUids == NULL &&
7,835!
1964
            (newTbUids = taosArrayInit(TARRAY_SIZE(pSubmitReq->aSubmitTbData), sizeof(int64_t))) == NULL) {
3,713✔
1965
          code = terrno;
×
1966
          goto _exit;
×
1967
        }
1968

1969
        if (taosArrayPush(newTbUids, &pSubmitTbData->uid) == NULL) {
8,244!
1970
          code = terrno;
×
1971
          goto _exit;
×
1972
        }
1973

1974
        if (pCreateTbRsp->pMeta) {
4,122!
1975
          vnodeUpdateMetaRsp(pVnode, pCreateTbRsp->pMeta);
4,122✔
1976
        }
1977
      } else {  // create table failed
1978
        if (terrno != TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
16,888✔
1979
          code = terrno;
8✔
1980
          vError("vgId:%d failed to create table:%s, code:%s", TD_VID(pVnode), pSubmitTbData->pCreateTbReq->name,
8!
1981
                 tstrerror(terrno));
1982
          goto _exit;
8✔
1983
        }
1984
        terrno = 0;
16,880✔
1985
        pSubmitTbData->uid = pSubmitTbData->pCreateTbReq->uid;  // update uid if table exist for using below
16,880✔
1986
      }
1987
    }
1988

1989
    // insert data
1990
    int32_t affectedRows;
1991
    code = tsdbInsertTableData(pVnode->pTsdb, ver, pSubmitTbData, &affectedRows);
1,065,115✔
1992
    if (code) goto _exit;
1,065,242!
1993

1994
    code = metaUpdateChangeTimeWithLock(pVnode->pMeta, pSubmitTbData->uid, pSubmitTbData->ctimeMs);
1,065,242✔
1995
    if (code) goto _exit;
1,065,230!
1996

1997
    pSubmitRsp->affectedRows += affectedRows;
1,065,230✔
1998
  }
1999

2000
  // update the affected table uid list
2001
  if (taosArrayGetSize(newTbUids) > 0) {
1,029,594✔
2002
    vDebug("vgId:%d, add %d table into query table list in handling submit", TD_VID(pVnode),
3,713✔
2003
           (int32_t)taosArrayGetSize(newTbUids));
2004
    if (tqUpdateTbUidList(pVnode->pTq, newTbUids, true) != 0) {
3,713!
2005
      vError("vgId:%d, failed to update tbUid list", TD_VID(pVnode));
×
2006
    }
2007
  }
2008

2009
_exit:
1,029,530✔
2010
  // message
2011
  pRsp->code = code;
1,029,538✔
2012
  tEncodeSize(tEncodeSSubmitRsp2, pSubmitRsp, pRsp->contLen, ret);
1,029,538!
2013
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
1,029,375✔
2014
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
1,029,564✔
2015
  if (tEncodeSSubmitRsp2(&ec, pSubmitRsp) < 0) {
1,029,531!
2016
    vError("vgId:%d, failed to encode submit response", TD_VID(pVnode));
×
2017
  }
2018
  tEncoderClear(&ec);
1,029,471✔
2019

2020
  // update statistics
2021
  (void)atomic_add_fetch_64(&pVnode->statis.nInsert, pSubmitRsp->affectedRows);
1,029,524✔
2022
  (void)atomic_add_fetch_64(&pVnode->statis.nInsertSuccess, pSubmitRsp->affectedRows);
1,029,606✔
2023
  (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsert, 1);
1,029,602✔
2024

2025
  if (tsEnableMonitor && tsMonitorFqdn[0] != 0 && tsMonitorPort != 0 && pSubmitRsp->affectedRows > 0 &&
1,029,598!
2026
      strlen(pOriginalMsg->info.conn.user) > 0 && tsInsertCounter != NULL) {
×
2027
    const char *sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT_AFFECTED_ROWS,
×
2028
                                   pVnode->monitor.strClusterId,
×
2029
                                   pVnode->monitor.strDnodeId,
×
2030
                                   tsLocalEp,
2031
                                   pVnode->monitor.strVgId,
×
2032
                                   pOriginalMsg->info.conn.user,
×
2033
                                   "Success"};
2034
    int         tv = taos_counter_add(tsInsertCounter, pSubmitRsp->affectedRows, sample_labels);
×
2035
  }
2036

2037
  if (code == 0) {
1,029,598✔
2038
    (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
1,029,591✔
2039
    code = tdProcessRSmaSubmit(pVnode->pSma, ver, pSubmitReq, pReq, len);
1,029,570✔
2040
  }
2041
  /*
2042
  if (code == 0) {
2043
    atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
2044
    code = tdProcessRSmaSubmit(pVnode->pSma, ver, pSubmitReq, pReq, len);
2045

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

2059
  // clear
2060
  taosArrayDestroy(newTbUids);
1,029,453✔
2061
  tDestroySubmitReq(pSubmitReq, 0 == pMsg->version ? TSDB_MSG_FLG_CMPT : TSDB_MSG_FLG_DECODE);
1,029,410!
2062
  tDestroySSubmitRsp2(pSubmitRsp, TSDB_MSG_FLG_ENCODE);
1,029,523✔
2063

2064
  if (code) terrno = code;
1,029,453✔
2065

2066
  taosMemoryFree(pAllocMsg);
1,029,453✔
2067

2068
  return code;
1,029,415✔
2069
}
2070

2071
static int32_t vnodeProcessCreateTSmaReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
17✔
2072
  SVCreateTSmaReq req = {0};
17✔
2073
  SDecoder        coder = {0};
17✔
2074

2075
  if (pRsp) {
17!
2076
    pRsp->msgType = TDMT_VND_CREATE_SMA_RSP;
17✔
2077
    pRsp->code = TSDB_CODE_SUCCESS;
17✔
2078
    pRsp->pCont = NULL;
17✔
2079
    pRsp->contLen = 0;
17✔
2080
  }
2081

2082
  // decode and process req
2083
  tDecoderInit(&coder, pReq, len);
17✔
2084

2085
  if (tDecodeSVCreateTSmaReq(&coder, &req) < 0) {
17!
2086
    terrno = TSDB_CODE_MSG_DECODE_ERROR;
×
2087
    if (pRsp) pRsp->code = terrno;
×
2088
    goto _err;
×
2089
  }
2090

2091
  if (tdProcessTSmaCreate(pVnode->pSma, ver, (const char *)&req) < 0) {
17!
2092
    if (pRsp) pRsp->code = terrno;
×
2093
    goto _err;
×
2094
  }
2095

2096
  tDecoderClear(&coder);
17✔
2097
  vDebug("vgId:%d, success to create tsma %s:%" PRIi64 " version %" PRIi64 " for table %" PRIi64, TD_VID(pVnode),
17!
2098
         req.indexName, req.indexUid, ver, req.tableUid);
2099
  return 0;
17✔
2100

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

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

2120
static int32_t vnodeConsolidateAlterHashRange(SVnode *pVnode, int64_t ver) {
12✔
2121
  int32_t code = TSDB_CODE_SUCCESS;
12✔
2122

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

2126
  // TODO: trim meta of tables from TDB per hash range [pVnode->config.hashBegin, pVnode->config.hashEnd]
2127
  code = metaTrimTables(pVnode->pMeta);
12✔
2128

2129
  return code;
12✔
2130
}
2131

2132
static int32_t vnodeProcessAlterConfirmReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
1,457✔
2133
  vInfo("vgId:%d, vnode handle msgType:alter-confirm, alter confirm msg is processed", TD_VID(pVnode));
1,457!
2134
  int32_t code = TSDB_CODE_SUCCESS;
1,457✔
2135
  if (!pVnode->config.hashChange) {
1,457✔
2136
    goto _exit;
1,445✔
2137
  }
2138

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

2146
_exit:
1,457✔
2147
  pRsp->msgType = TDMT_VND_ALTER_CONFIRM_RSP;
1,457✔
2148
  pRsp->code = code;
1,457✔
2149
  pRsp->pCont = NULL;
1,457✔
2150
  pRsp->contLen = 0;
1,457✔
2151

2152
  return code;
1,457✔
2153
}
2154

2155
extern int32_t tsdbDisableAndCancelAllBgTask(STsdb *pTsdb);
2156
extern void    tsdbEnableBgTask(STsdb *pTsdb);
2157

2158
static int32_t vnodeProcessAlterConfigReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
294✔
2159
  bool walChanged = false;
294✔
2160
  bool tsdbChanged = false;
294✔
2161

2162
  SAlterVnodeConfigReq req = {0};
294✔
2163
  if (tDeserializeSAlterVnodeConfigReq(pReq, len, &req) != 0) {
294!
2164
    terrno = TSDB_CODE_INVALID_MSG;
×
2165
    return TSDB_CODE_INVALID_MSG;
×
2166
  }
2167

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

2177
  if (pVnode->config.cacheLastSize != req.cacheLastSize) {
295✔
2178
    pVnode->config.cacheLastSize = req.cacheLastSize;
19✔
2179
    tsdbCacheSetCapacity(pVnode, (size_t)pVnode->config.cacheLastSize * 1024 * 1024);
19✔
2180
  }
2181

2182
  if (pVnode->config.szBuf != req.buffer * 1024LL * 1024LL) {
295✔
2183
    vInfo("vgId:%d, vnode buffer is changed from %" PRId64 " to %" PRId64, TD_VID(pVnode), pVnode->config.szBuf,
10!
2184
          (uint64_t)(req.buffer * 1024LL * 1024LL));
2185
    pVnode->config.szBuf = req.buffer * 1024LL * 1024LL;
10✔
2186
  }
2187

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

2199
  if (pVnode->config.cacheLast != req.cacheLast) {
295✔
2200
    pVnode->config.cacheLast = req.cacheLast;
108✔
2201
  }
2202

2203
  if (pVnode->config.walCfg.fsyncPeriod != req.walFsyncPeriod) {
295✔
2204
    pVnode->config.walCfg.fsyncPeriod = req.walFsyncPeriod;
70✔
2205
    walChanged = true;
70✔
2206
  }
2207

2208
  if (pVnode->config.walCfg.level != req.walLevel) {
295✔
2209
    if (pVnode->config.walCfg.level == 0) {
51✔
2210
      pVnode->config.walCfg.clearFiles = 1;
3✔
2211
    }
2212
    pVnode->config.walCfg.level = req.walLevel;
51✔
2213
    walChanged = true;
51✔
2214
  }
2215

2216
  if (pVnode->config.walCfg.retentionPeriod != req.walRetentionPeriod) {
295✔
2217
    pVnode->config.walCfg.retentionPeriod = req.walRetentionPeriod;
16✔
2218
    walChanged = true;
16✔
2219
  }
2220

2221
  if (pVnode->config.walCfg.retentionSize != req.walRetentionSize) {
295!
2222
    pVnode->config.walCfg.retentionSize = req.walRetentionSize;
×
2223
    walChanged = true;
×
2224
  }
2225

2226
  if (pVnode->config.tsdbCfg.keep0 != req.daysToKeep0) {
295✔
2227
    pVnode->config.tsdbCfg.keep0 = req.daysToKeep0;
32✔
2228
    if (!VND_IS_RSMA(pVnode)) {
32!
2229
      tsdbChanged = true;
32✔
2230
    }
2231
  }
2232

2233
  if (pVnode->config.tsdbCfg.keep1 != req.daysToKeep1) {
295✔
2234
    pVnode->config.tsdbCfg.keep1 = req.daysToKeep1;
32✔
2235
    if (!VND_IS_RSMA(pVnode)) {
32!
2236
      tsdbChanged = true;
32✔
2237
    }
2238
  }
2239

2240
  if (pVnode->config.tsdbCfg.keep2 != req.daysToKeep2) {
295✔
2241
    pVnode->config.tsdbCfg.keep2 = req.daysToKeep2;
34✔
2242
    if (!VND_IS_RSMA(pVnode)) {
34!
2243
      tsdbChanged = true;
34✔
2244
    }
2245
  }
2246

2247
  if (pVnode->config.tsdbCfg.keepTimeOffset != req.keepTimeOffset) {
295✔
2248
    pVnode->config.tsdbCfg.keepTimeOffset = req.keepTimeOffset;
2✔
2249
    if (!VND_IS_RSMA(pVnode)) {
2!
2250
      tsdbChanged = true;
2✔
2251
    }
2252
  }
2253

2254
  if (req.sttTrigger != -1 && req.sttTrigger != pVnode->config.sttTrigger) {
295!
2255
    if (req.sttTrigger > 1 && pVnode->config.sttTrigger > 1) {
×
2256
      pVnode->config.sttTrigger = req.sttTrigger;
×
2257
    } else {
2258
      vnodeAWait(&pVnode->commitTask);
×
2259

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

2265
      pVnode->config.sttTrigger = req.sttTrigger;
×
2266
      tsdbEnableBgTask(pVnode->pTsdb);
×
2267
    }
2268
  }
2269

2270
  if (req.minRows != -1 && req.minRows != pVnode->config.tsdbCfg.minRows) {
295!
2271
    pVnode->config.tsdbCfg.minRows = req.minRows;
×
2272
  }
2273

2274
  if (req.s3KeepLocal != -1 && req.s3KeepLocal != pVnode->config.s3KeepLocal) {
295!
2275
    pVnode->config.s3KeepLocal = req.s3KeepLocal;
×
2276
  }
2277
  if (req.s3Compact != -1 && req.s3Compact != pVnode->config.s3Compact) {
295!
2278
    pVnode->config.s3Compact = req.s3Compact;
×
2279
  }
2280

2281
  if (walChanged) {
295✔
2282
    if (walAlter(pVnode->pWal, &pVnode->config.walCfg) != 0) {
133!
2283
      vError("vgId:%d, failed to alter wal config since %s", TD_VID(pVnode), tstrerror(errno));
×
2284
    }
2285
  }
2286

2287
  if (tsdbChanged) {
295✔
2288
    tsdbSetKeepCfg(pVnode->pTsdb, &pVnode->config.tsdbCfg);
36✔
2289
  }
2290

2291
  return 0;
295✔
2292
}
2293

2294
static int32_t vnodeProcessBatchDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
589✔
2295
  SBatchDeleteReq deleteReq;
2296
  SDecoder        decoder;
2297
  tDecoderInit(&decoder, pReq, len);
589✔
2298
  if (tDecodeSBatchDeleteReq(&decoder, &deleteReq) < 0) {
589!
2299
    tDecoderClear(&decoder);
×
2300
    return terrno = TSDB_CODE_INVALID_MSG;
×
2301
  }
2302

2303
  SMetaReader mr = {0};
589✔
2304
  metaReaderDoInit(&mr, pVnode->pMeta, META_READER_NOLOCK);
589✔
2305
  STsdb *pTsdb = pVnode->pTsdb;
589✔
2306

2307
  if (deleteReq.level) {
589✔
2308
    pTsdb = deleteReq.level == 1 ? VND_RSMA1(pVnode) : VND_RSMA2(pVnode);
14✔
2309
  }
2310

2311
  int32_t sz = taosArrayGetSize(deleteReq.deleteReqs);
589✔
2312
  for (int32_t i = 0; i < sz; i++) {
1,851✔
2313
    SSingleDeleteReq *pOneReq = taosArrayGet(deleteReq.deleteReqs, i);
1,262✔
2314
    char             *name = pOneReq->tbname;
1,262✔
2315
    if (metaGetTableEntryByName(&mr, name) < 0) {
1,262✔
2316
      vDebug("vgId:%d, stream delete msg, skip since no table: %s", pVnode->config.vgId, name);
316✔
2317
      continue;
316✔
2318
    }
2319

2320
    int64_t uid = mr.me.uid;
946✔
2321

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

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

2345
static int32_t vnodeProcessDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
1,193✔
2346
                                     SRpcMsg *pOriginalMsg) {
2347
  int32_t     code = 0;
1,193✔
2348
  SDecoder   *pCoder = &(SDecoder){0};
1,193✔
2349
  SDeleteRes *pRes = &(SDeleteRes){0};
1,193✔
2350

2351
  pRsp->msgType = TDMT_VND_DELETE_RSP;
1,193✔
2352
  pRsp->pCont = NULL;
1,193✔
2353
  pRsp->contLen = 0;
1,193✔
2354
  pRsp->code = TSDB_CODE_SUCCESS;
1,193✔
2355

2356
  pRes->uidList = taosArrayInit(0, sizeof(tb_uid_t));
1,193✔
2357
  if (pRes->uidList == NULL) {
1,194!
2358
    code = terrno;
×
2359
    goto _err;
×
2360
  }
2361

2362
  tDecoderInit(pCoder, pReq, len);
1,194✔
2363
  code = tDecodeDeleteRes(pCoder, pRes);
1,194✔
2364
  if (code) goto _err;
1,195!
2365

2366
  if (pRes->affectedRows > 0) {
1,195✔
2367
    for (int32_t iUid = 0; iUid < taosArrayGetSize(pRes->uidList); iUid++) {
1,079✔
2368
      uint64_t uid = *(uint64_t *)taosArrayGet(pRes->uidList, iUid);
559✔
2369
      code = tsdbDeleteTableData(pVnode->pTsdb, ver, pRes->suid, uid, pRes->skey, pRes->ekey);
559✔
2370
      if (code) goto _err;
560!
2371
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, pRes->ctimeMs);
560✔
2372
      if (code) goto _err;
560!
2373
    }
2374
  }
2375

2376
  code = tdProcessRSmaDelete(pVnode->pSma, ver, pRes, pReq, len);
1,195✔
2377

2378
  tDecoderClear(pCoder);
1,195✔
2379
  taosArrayDestroy(pRes->uidList);
1,195✔
2380

2381
  SVDeleteRsp rsp = {.affectedRows = pRes->affectedRows};
1,195✔
2382
  int32_t     ret = 0;
1,195✔
2383
  tEncodeSize(tEncodeSVDeleteRsp, &rsp, pRsp->contLen, ret);
1,195!
2384
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
1,195✔
2385
  SEncoder ec = {0};
1,195✔
2386
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
1,195✔
2387
  code = tEncodeSVDeleteRsp(&ec, &rsp);
1,195✔
2388
  if (code) goto _err;
1,194!
2389
  tEncoderClear(&ec);
1,194✔
2390
  return code;
1,195✔
2391

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

2408
  return code;
×
2409
}
2410
static int32_t vnodeProcessCreateIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
49✔
2411
  SVCreateStbReq req = {0};
49✔
2412
  SDecoder       dc = {0};
49✔
2413
  int32_t        code = 0;
49✔
2414

2415
  pRsp->msgType = TDMT_VND_CREATE_INDEX_RSP;
49✔
2416
  pRsp->code = TSDB_CODE_SUCCESS;
49✔
2417
  pRsp->pCont = NULL;
49✔
2418
  pRsp->contLen = 0;
49✔
2419

2420
  tDecoderInit(&dc, pReq, len);
49✔
2421
  // decode req
2422
  if (tDecodeSVCreateStbReq(&dc, &req) < 0) {
49!
2423
    tDecoderClear(&dc);
×
2424
    return terrno = TSDB_CODE_INVALID_MSG;
×
2425
  }
2426

2427
  code = metaAddIndexToSTable(pVnode->pMeta, ver, &req);
49✔
2428
  if (code) {
49!
2429
    pRsp->code = code;
×
2430
    goto _err;
×
2431
  }
2432
  tDecoderClear(&dc);
49✔
2433
  return 0;
49✔
2434

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

2447
  if ((code = tDeserializeSDropIdxReq(pReq, len, &req))) {
32!
2448
    pRsp->code = code;
×
2449
    return code;
×
2450
  }
2451

2452
  code = metaDropIndexFromSTable(pVnode->pMeta, ver, &req);
32✔
2453
  if (code) {
32!
2454
    pRsp->code = code;
×
2455
    return code;
×
2456
  }
2457
  return TSDB_CODE_SUCCESS;
32✔
2458
}
2459

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

2462
static int32_t vnodeProcessCompactVnodeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
8✔
2463
  if (!pVnode->restored) {
8!
UNCOV
2464
    vInfo("vgId:%d, ignore compact req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
×
UNCOV
2465
    return 0;
×
2466
  }
2467
  return vnodeAsyncCompact(pVnode, ver, pReq, len, pRsp);
8✔
2468
}
2469

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

2475
  pRsp->msgType = TDMT_SYNC_CONFIG_CHANGE_RSP;
×
2476
  pRsp->code = TSDB_CODE_SUCCESS;
×
2477
  pRsp->pCont = NULL;
×
2478
  pRsp->contLen = 0;
×
2479

2480
  return 0;
×
2481
}
2482

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

2489
  char token[TSDB_ARB_TOKEN_SIZE] = {0};
×
2490
  if (vnodeGetArbToken(pVnode, token) != 0) {
×
2491
    return terrno = TSDB_CODE_NOT_FOUND;
×
2492
  }
2493

2494
  if (strncmp(token, member0Token, TSDB_ARB_TOKEN_SIZE) != 0 &&
×
2495
      strncmp(token, member1Token, TSDB_ARB_TOKEN_SIZE) != 0) {
×
2496
    return terrno = TSDB_CODE_MND_ARB_TOKEN_MISMATCH;
×
2497
  }
2498

2499
  terrno = TSDB_CODE_SUCCESS;
×
2500
  return 0;
×
2501
}
2502

2503
static int32_t vnodeCheckAssignedLogSyncd(SVnode *pVnode, char *member0Token, char *member1Token) {
×
2504
  int32_t code = vnodePreCheckAssignedLogSyncd(pVnode, member0Token, member1Token);
×
2505
  if (code != 0) {
×
2506
    return code;
×
2507
  }
2508

2509
  return syncGetAssignedLogSynced(pVnode->sync);
×
2510
}
2511

2512
static int32_t vnodeProcessArbCheckSyncReq(SVnode *pVnode, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
2513
  int32_t code = 0;
×
2514

2515
  SVArbCheckSyncReq syncReq = {0};
×
2516

2517
  code = tDeserializeSVArbCheckSyncReq(pReq, len, &syncReq);
×
2518
  if (code) {
×
2519
    return terrno = code;
×
2520
  }
2521

2522
  pRsp->msgType = TDMT_VND_ARB_CHECK_SYNC_RSP;
×
2523
  pRsp->code = TSDB_CODE_SUCCESS;
×
2524
  pRsp->pCont = NULL;
×
2525
  pRsp->contLen = 0;
×
2526

2527
  SVArbCheckSyncRsp syncRsp = {0};
×
2528
  syncRsp.arbToken = syncReq.arbToken;
×
2529
  syncRsp.member0Token = syncReq.member0Token;
×
2530
  syncRsp.member1Token = syncReq.member1Token;
×
2531
  syncRsp.vgId = TD_VID(pVnode);
×
2532

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

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

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

2557
  if (tSerializeSVArbCheckSyncRsp(pHead, contLen, &syncRsp) <= 0) {
×
2558
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2559
    rpcFreeCont(pHead);
×
2560
    code = -1;
×
2561
    goto _OVER;
×
2562
  }
2563

2564
  pRsp->pCont = pHead;
×
2565
  pRsp->contLen = contLen;
×
2566

2567
  terrno = TSDB_CODE_SUCCESS;
×
2568

2569
_OVER:
×
2570
  tFreeSVArbCheckSyncReq(&syncReq);
×
2571
  return code;
×
2572
}
2573

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

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