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

taosdata / TDengine / #3559

18 Dec 2024 12:59AM UTC coverage: 59.805% (+0.03%) from 59.778%
#3559

push

travis-ci

web-flow
Merge pull request #29187 from taosdata/merge/mainto3.0

merge: main to 3.0 branch

132705 of 287544 branches covered (46.15%)

Branch coverage included in aggregate %.

87 of 95 new or added lines in 19 files covered. (91.58%)

1132 existing lines in 133 files now uncovered.

209591 of 284807 relevant lines covered (73.59%)

8125235.78 hits per line

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

59.29
/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) {
137,747✔
64
  int32_t code = 0;
137,747✔
65
  int32_t lino = 0;
137,747✔
66

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

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

78
  // name
79
  char *name = NULL;
137,770✔
80
  if (tDecodeCStr(pCoder, &name) < 0) {
137,741!
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);
137,741✔
87
  if (uid == 0) {
137,752✔
88
    uid = tGenIdPI64();
111,784✔
89
  }
90
  *(int64_t *)(pCoder->data + pCoder->pos) = uid;
137,747✔
91

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

95
  tEndDecode(pCoder);
137,747✔
96

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

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

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

120
  if (tDecodeI32v(&dc, &nReqs) < 0) {
52,925!
121
    code = TSDB_CODE_INVALID_MSG;
×
122
    TSDB_CHECK_CODE(code, lino, _exit);
×
123
  }
124
  for (int32_t iReq = 0; iReq < nReqs; iReq++) {
129,766✔
125
    code = vnodePreprocessCreateTableReq(pVnode, &dc, btime, NULL);
76,860✔
126
    TSDB_CHECK_CODE(code, lino, _exit);
76,841!
127
  }
128

129
  tEndDecode(&dc);
52,906✔
130

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

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

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

147
  SVAlterTbReq vAlterTbReq = {0};
896✔
148
  int64_t      ctimeMs = taosGetTimestampMs();
896✔
149
  if (tDecodeSVAlterTbReqSetCtime(&dc, &vAlterTbReq, ctimeMs) < 0) {
896!
150
    taosArrayDestroy(vAlterTbReq.pMultiTag);
×
151
    vAlterTbReq.pMultiTag = NULL;
×
152
    goto _exit;
×
153
  }
154
  taosArrayDestroy(vAlterTbReq.pMultiTag);
896✔
155
  vAlterTbReq.pMultiTag = NULL;
896✔
156

157
  code = 0;
896✔
158

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

170
static int32_t vnodePreProcessDropTtlMsg(SVnode *pVnode, SRpcMsg *pMsg) {
16,554✔
171
  int32_t code = TSDB_CODE_INVALID_MSG;
16,554✔
172
  int32_t lino = 0;
16,554✔
173

174
  SMsgHead *pContOld = pMsg->pCont;
16,554✔
175
  int32_t   reqLenOld = pMsg->contLen - sizeof(SMsgHead);
16,554✔
176

177
  SArray *tbUids = NULL;
16,554✔
178
  int64_t timestampMs = 0;
16,554✔
179

180
  SVDropTtlTableReq ttlReq = {0};
16,554✔
181
  if (tDeserializeSVDropTtlTableReq((char *)pContOld + sizeof(SMsgHead), reqLenOld, &ttlReq) != 0) {
16,554!
182
    code = TSDB_CODE_INVALID_MSG;
×
183
    TSDB_CHECK_CODE(code, lino, _exit);
×
184
  }
185

186
  {  // find expired uids
187
    tbUids = taosArrayInit(8, sizeof(tb_uid_t));
16,563✔
188
    if (tbUids == NULL) {
16,586!
189
      code = terrno;
×
190
      TSDB_CHECK_CODE(code, lino, _exit);
×
191
    }
192

193
    timestampMs = (int64_t)ttlReq.timestampSec * 1000;
16,586✔
194
    code = metaTtlFindExpired(pVnode->pMeta, timestampMs, tbUids, ttlReq.ttlDropMaxCount);
16,586✔
195
    if (code != 0) {
16,544!
196
      code = TSDB_CODE_INVALID_MSG;
×
197
      TSDB_CHECK_CODE(code, lino, _exit);
×
198
    }
199

200
    ttlReq.nUids = taosArrayGetSize(tbUids);
16,544✔
201
    ttlReq.pTbUids = tbUids;
16,535✔
202
  }
203

204
  if (ttlReq.nUids == 0) {
16,535!
205
    code = TSDB_CODE_MSG_PREPROCESSED;
16,544✔
206
    TSDB_CHECK_CODE(code, lino, _exit);
16,544!
207
  }
208

209
  {  // prepare new content
210
    int32_t reqLenNew = tSerializeSVDropTtlTableReq(NULL, 0, &ttlReq);
×
211
    int32_t contLenNew = reqLenNew + sizeof(SMsgHead);
5✔
212

213
    SMsgHead *pContNew = rpcMallocCont(contLenNew);
5✔
214
    if (pContNew == NULL) {
5!
215
      code = terrno;
×
216
      TSDB_CHECK_CODE(code, lino, _exit);
×
217
    }
218

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

225
    rpcFreeCont(pContOld);
5✔
226
    pMsg->pCont = pContNew;
5✔
227
    pMsg->contLen = contLenNew;
5✔
228
  }
229

230
  code = 0;
5✔
231

232
_exit:
16,559✔
233
  taosArrayDestroy(tbUids);
16,559✔
234

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

242
  return code;
16,529✔
243
}
244

245
extern int64_t tsMaxKeyByPrecision[];
246
static int32_t vnodePreProcessSubmitTbData(SVnode *pVnode, SDecoder *pCoder, int64_t btimeMs, int64_t ctimeMs) {
10,141,057✔
247
  int32_t code = 0;
10,141,057✔
248
  int32_t lino = 0;
10,141,057✔
249

250
  if (tStartDecode(pCoder) < 0) {
10,141,057!
251
    code = TSDB_CODE_INVALID_MSG;
×
252
    TSDB_CHECK_CODE(code, lino, _exit);
×
253
  }
254

255
  SSubmitTbData submitTbData;
256
  uint8_t       version;
257
  if (tDecodeI32v(pCoder, &submitTbData.flags) < 0) {
10,159,354!
258
    code = TSDB_CODE_INVALID_MSG;
×
259
    TSDB_CHECK_CODE(code, lino, _exit);
×
260
  }
261
  version = (submitTbData.flags >> 8) & 0xff;
10,159,354✔
262
  submitTbData.flags = submitTbData.flags & 0xff;
10,159,354✔
263

264
  int64_t uid;
265
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
10,159,354✔
266
    code = vnodePreprocessCreateTableReq(pVnode, pCoder, btimeMs, &uid);
60,890✔
267
    TSDB_CHECK_CODE(code, lino, _exit);
60,892!
268
  }
269

270
  // submit data
271
  if (tDecodeI64(pCoder, &submitTbData.suid) < 0) {
10,147,393!
272
    code = TSDB_CODE_INVALID_MSG;
×
273
    TSDB_CHECK_CODE(code, lino, _exit);
×
274
  }
275

276
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
10,147,393✔
277
    *(int64_t *)(pCoder->data + pCoder->pos) = uid;
60,893✔
278
    pCoder->pos += sizeof(int64_t);
60,893✔
279
  } else {
280
    if (tDecodeI64(pCoder, &submitTbData.uid) < 0) {
10,079,926!
281
      code = TSDB_CODE_INVALID_MSG;
×
282
      TSDB_CHECK_CODE(code, lino, _exit);
×
283
    }
284
  }
285

286
  if (tDecodeI32v(pCoder, &submitTbData.sver) < 0) {
10,131,957!
287
    code = TSDB_CODE_INVALID_MSG;
×
288
    TSDB_CHECK_CODE(code, lino, _exit);
×
289
  }
290

291
  // scan and check
292
  TSKEY now = btimeMs;
10,131,957✔
293
  if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_MICRO) {
10,131,957✔
294
    now *= 1000;
22,336✔
295
  } else if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_NANO) {
10,109,621✔
296
    now *= 1000000;
3,534✔
297
  }
298

299
  int32_t keep = pVnode->config.tsdbCfg.keep2;
10,131,957✔
300
  /*
301
  int32_t nlevel = tfsGetLevel(pVnode->pTfs);
302
  if (nlevel > 1 && tsS3Enabled) {
303
    if (nlevel == 3) {
304
      keep = pVnode->config.tsdbCfg.keep1;
305
    } else if (nlevel == 2) {
306
      keep = pVnode->config.tsdbCfg.keep0;
307
    }
308
  }
309
  */
310

311
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * keep;
10,131,957✔
312
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
10,131,957✔
313
  if (submitTbData.flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
10,131,957✔
314
    uint64_t nColData;
315
    if (tDecodeU64v(pCoder, &nColData) < 0) {
131!
316
      code = TSDB_CODE_INVALID_MSG;
×
317
      goto _exit;
×
318
    }
319

320
    SColData colData = {0};
131✔
321
    pCoder->pos += tGetColData(version, pCoder->data + pCoder->pos, &colData);
131✔
322
    if (colData.flag != HAS_VALUE) {
132!
323
      code = TSDB_CODE_INVALID_MSG;
×
324
      goto _exit;
×
325
    }
326

327
    for (int32_t iRow = 0; iRow < colData.nVal; iRow++) {
354✔
328
      if (((TSKEY *)colData.pData)[iRow] < minKey || ((TSKEY *)colData.pData)[iRow] > maxKey) {
222!
329
        code = TSDB_CODE_TDB_TIMESTAMP_OUT_OF_RANGE;
×
330
        goto _exit;
×
331
      }
332
    }
333

334
    for (uint64_t i = 1; i < nColData; i++) {
562✔
335
      pCoder->pos += tGetColData(version, pCoder->data + pCoder->pos, &colData);
430✔
336
    }
337
  } else {
338
    uint64_t nRow;
339
    if (tDecodeU64v(pCoder, &nRow) < 0) {
10,138,436!
340
      code = TSDB_CODE_INVALID_MSG;
×
341
      goto _exit;
487✔
342
    }
343

344
    for (int32_t iRow = 0; iRow < nRow; ++iRow) {
261,831,324✔
345
      SRow *pRow = (SRow *)(pCoder->data + pCoder->pos);
251,693,375✔
346
      pCoder->pos += pRow->len;
251,693,375✔
347

348
      if (pRow->ts < minKey || pRow->ts > maxKey) {
251,693,375!
349
        code = TSDB_CODE_TDB_TIMESTAMP_OUT_OF_RANGE;
487✔
350
        goto _exit;
487✔
351
      }
352
    }
353
  }
354

355
  if (!tDecodeIsEnd(pCoder)) {
10,138,081!
356
    *(int64_t *)(pCoder->data + pCoder->pos) = ctimeMs;
10,144,610✔
357
    pCoder->pos += sizeof(int64_t);
10,144,610✔
358
  }
359

360
  tEndDecode(pCoder);
10,138,081✔
361

362
_exit:
10,140,704✔
363
  return code;
10,140,704✔
364
}
365
static int32_t vnodePreProcessSubmitMsg(SVnode *pVnode, SRpcMsg *pMsg) {
9,473,038✔
366
  int32_t code = 0;
9,473,038✔
367
  int32_t lino = 0;
9,473,038✔
368

369
  if (tsBypassFlag & TSDB_BYPASS_RA_RPC_RECV_SUBMIT) {
9,473,038!
370
    return TSDB_CODE_MSG_PREPROCESSED;
×
371
  }
372

373
  SDecoder *pCoder = &(SDecoder){0};
9,473,038✔
374

375
  if (taosHton64(((SSubmitReq2Msg *)pMsg->pCont)->version) != 1) {
9,473,038!
376
    code = TSDB_CODE_INVALID_MSG;
×
377
    TSDB_CHECK_CODE(code, lino, _exit);
×
378
  }
379

380
  tDecoderInit(pCoder, (uint8_t *)pMsg->pCont + sizeof(SSubmitReq2Msg), pMsg->contLen - sizeof(SSubmitReq2Msg));
9,473,060✔
381

382
  if (tStartDecode(pCoder) < 0) {
9,473,049!
383
    code = TSDB_CODE_INVALID_MSG;
×
384
    TSDB_CHECK_CODE(code, lino, _exit);
×
385
  }
386

387
  uint64_t nSubmitTbData;
388
  if (tDecodeU64v(pCoder, &nSubmitTbData) < 0) {
9,473,053!
389
    code = TSDB_CODE_INVALID_MSG;
×
390
    TSDB_CHECK_CODE(code, lino, _exit);
×
391
  }
392

393
  int64_t btimeMs = taosGetTimestampMs();
9,473,094✔
394
  int64_t ctimeMs = btimeMs;
9,473,094✔
395
  for (int32_t i = 0; i < nSubmitTbData; i++) {
19,613,149✔
396
    code = vnodePreProcessSubmitTbData(pVnode, pCoder, btimeMs, ctimeMs);
10,141,373✔
397
    TSDB_CHECK_CODE(code, lino, _exit);
10,140,542✔
398
  }
399

400
  tEndDecode(pCoder);
9,471,776✔
401

402
_exit:
9,473,083✔
403
  tDecoderClear(pCoder);
9,473,083✔
404
  if (code) {
9,473,071✔
405
    vError("vgId:%d, %s:%d failed to preprocess submit request since %s, msg type:%s", TD_VID(pVnode), __func__, lino,
487!
406
           tstrerror(code), TMSG_INFO(pMsg->msgType));
407
  }
408
  return code;
9,473,073✔
409
}
410

411
static int32_t vnodePreProcessDeleteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
18,033✔
412
  int32_t code = 0;
18,033✔
413

414
  int32_t    size;
415
  int32_t    ret;
416
  uint8_t   *pCont;
417
  SEncoder  *pCoder = &(SEncoder){0};
18,033✔
418
  SDeleteRes res = {0};
18,033✔
419

420
  SReadHandle handle = {.vnode = pVnode, .pMsgCb = &pVnode->msgCb, .skipRollup = 1};
18,033✔
421
  initStorageAPI(&handle.api);
18,033✔
422

423
  code = qWorkerProcessDeleteMsg(&handle, pVnode->pQuery, pMsg, &res);
18,034✔
424
  if (code) goto _exit;
18,034!
425

426
  res.ctimeMs = taosGetTimestampMs();
18,034✔
427
  // malloc and encode
428
  tEncodeSize(tEncodeDeleteRes, &res, size, ret);
18,034!
429
  pCont = rpcMallocCont(size + sizeof(SMsgHead));
18,034✔
430

431
  ((SMsgHead *)pCont)->contLen = size + sizeof(SMsgHead);
18,034✔
432
  ((SMsgHead *)pCont)->vgId = TD_VID(pVnode);
18,034✔
433

434
  tEncoderInit(pCoder, pCont + sizeof(SMsgHead), size);
18,034✔
435
  if (tEncodeDeleteRes(pCoder, &res) != 0) {
18,034!
436
    vError("vgId:%d %s failed to encode delete response", TD_VID(pVnode), __func__);
×
437
  }
438
  tEncoderClear(pCoder);
18,034✔
439

440
  rpcFreeCont(pMsg->pCont);
18,034✔
441
  pMsg->pCont = pCont;
18,034✔
442
  pMsg->contLen = size + sizeof(SMsgHead);
18,034✔
443

444
  taosArrayDestroy(res.uidList);
18,034✔
445

446
_exit:
18,034✔
447
  return code;
18,034✔
448
}
449

450
static int32_t vnodePreProcessBatchDeleteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
2,538✔
451
  int32_t code = 0;
2,538✔
452
  int32_t lino = 0;
2,538✔
453

454
  int64_t         ctimeMs = taosGetTimestampMs();
2,542✔
455
  SBatchDeleteReq pReq = {0};
2,542✔
456
  SDecoder       *pCoder = &(SDecoder){0};
2,542✔
457

458
  tDecoderInit(pCoder, (uint8_t *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead));
2,542✔
459

460
  if (tDecodeSBatchDeleteReqSetCtime(pCoder, &pReq, ctimeMs) < 0) {
2,539!
461
    code = TSDB_CODE_INVALID_MSG;
×
462
  }
463

464
  tDecoderClear(pCoder);
2,541✔
465
  taosArrayDestroy(pReq.deleteReqs);
2,538✔
466

467
  if (code) {
2,543!
468
    vError("vgId:%d %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
469
  } else {
470
    vTrace("vgId:%d %s done, ctimeMs generated:%" PRId64, TD_VID(pVnode), __func__, ctimeMs);
2,543✔
471
  }
472
  return code;
2,540✔
473
}
474

475
static int32_t vnodePreProcessArbCheckSyncMsg(SVnode *pVnode, SRpcMsg *pMsg) {
2✔
476
  SVArbCheckSyncReq syncReq = {0};
2✔
477

478
  if (tDeserializeSVArbCheckSyncReq((char *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead),
2!
479
                                    &syncReq) != 0) {
480
    return TSDB_CODE_INVALID_MSG;
×
481
  }
482

483
  int32_t ret = vnodePreCheckAssignedLogSyncd(pVnode, syncReq.member0Token, syncReq.member1Token);
2✔
484
  if (ret != 0) {
2!
485
    vError("vgId:%d, failed to preprocess arb check sync request since %s", TD_VID(pVnode), tstrerror(ret));
2!
486
  }
487

488
  int32_t code = terrno;
2✔
489
  tFreeSVArbCheckSyncReq(&syncReq);
2✔
490

491
  return code;
2✔
492
}
493

494
int32_t vnodePreProcessDropTbMsg(SVnode *pVnode, SRpcMsg *pMsg) {
1,869✔
495
  int32_t          code = TSDB_CODE_SUCCESS;
1,869✔
496
  int32_t          lino = 0;
1,869✔
497
  int32_t          size = 0;
1,869✔
498
  SDecoder         dc = {0};
1,869✔
499
  SEncoder         ec = {0};
1,869✔
500
  SVDropTbBatchReq receivedBatchReqs = {0};
1,869✔
501
  SVDropTbBatchReq sentBatchReqs = {0};
1,869✔
502

503
  tDecoderInit(&dc, POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), pMsg->contLen - sizeof(SMsgHead));
1,869✔
504

505
  code = tDecodeSVDropTbBatchReq(&dc, &receivedBatchReqs);
1,869✔
506
  if (code < 0) {
1,869!
507
    terrno = code;
×
508
    TSDB_CHECK_CODE(code, lino, _exit);
×
509
  }
510
  sentBatchReqs.pArray = taosArrayInit(receivedBatchReqs.nReqs, sizeof(SVDropTbReq));
1,869✔
511
  if (!sentBatchReqs.pArray) {
1,869!
512
    code = terrno;
×
513
    goto _exit;
×
514
  }
515

516
  for (int32_t i = 0; i < receivedBatchReqs.nReqs; ++i) {
3,745✔
517
    SVDropTbReq *pReq = receivedBatchReqs.pReqs + i;
1,876✔
518
    tb_uid_t     uid = metaGetTableEntryUidByName(pVnode->pMeta, pReq->name);
1,876✔
519
    if (uid == 0) {
1,876!
520
      vWarn("vgId:%d, preprocess drop ctb: %s not found", TD_VID(pVnode), pReq->name);
×
521
      continue;
×
522
    }
523
    pReq->uid = uid;
1,876✔
524
    vDebug("vgId:%d %s for: %s, uid: %" PRId64, TD_VID(pVnode), __func__, pReq->name, pReq->uid);
1,876✔
525
    if (taosArrayPush(sentBatchReqs.pArray, pReq) == NULL) {
3,752!
526
      code = terrno;
×
527
      goto _exit;
×
528
    }
529
  }
530
  sentBatchReqs.nReqs = sentBatchReqs.pArray->size;
1,869✔
531

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

541
_exit:
1,869✔
542
  tDecoderClear(&dc);
1,869✔
543
  if (sentBatchReqs.pArray) {
1,869!
544
    taosArrayDestroy(sentBatchReqs.pArray);
1,869✔
545
  }
546
  return code;
1,869✔
547
}
548

549
int32_t vnodePreProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
9,644,716✔
550
  int32_t code = 0;
9,644,716✔
551

552
  switch (pMsg->msgType) {
9,644,716✔
553
    case TDMT_VND_CREATE_TABLE: {
52,925✔
554
      code = vnodePreProcessCreateTableMsg(pVnode, pMsg);
52,925✔
555
    } break;
52,918✔
556
    case TDMT_VND_ALTER_TABLE: {
896✔
557
      code = vnodePreProcessAlterTableMsg(pVnode, pMsg);
896✔
558
    } break;
896✔
559
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
16,559✔
560
    case TDMT_VND_DROP_TTL_TABLE: {
561
      code = vnodePreProcessDropTtlMsg(pVnode, pMsg);
16,559✔
562
    } break;
16,525✔
563
    case TDMT_VND_SUBMIT: {
9,473,069✔
564
      code = vnodePreProcessSubmitMsg(pVnode, pMsg);
9,473,069✔
565
    } break;
9,473,071✔
566
    case TDMT_VND_DELETE: {
18,033✔
567
      code = vnodePreProcessDeleteMsg(pVnode, pMsg);
18,033✔
568
    } break;
18,034✔
569
    case TDMT_VND_BATCH_DEL: {
2,541✔
570
      code = vnodePreProcessBatchDeleteMsg(pVnode, pMsg);
2,541✔
571
    } break;
2,540✔
572
    case TDMT_VND_ARB_CHECK_SYNC: {
2✔
573
      code = vnodePreProcessArbCheckSyncMsg(pVnode, pMsg);
2✔
574
    } break;
2✔
575
    case TDMT_VND_DROP_TABLE: {
1,869✔
576
      code = vnodePreProcessDropTbMsg(pVnode, pMsg);
1,869✔
577
    } break;
1,869✔
578
    default:
78,822✔
579
      break;
78,822✔
580
  }
581

582
  if (code && code != TSDB_CODE_MSG_PREPROCESSED) {
9,644,677✔
583
    vError("vgId:%d, failed to preprocess write request since %s, msg type:%s", TD_VID(pVnode), tstrerror(code),
489!
584
           TMSG_INFO(pMsg->msgType));
585
  }
586
  return code;
9,644,662✔
587
}
588

589
int32_t vnodeProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg, int64_t ver, SRpcMsg *pRsp) {
12,642,022✔
590
  int32_t code = 0;
12,642,022✔
591
  void   *ptr = NULL;
12,642,022✔
592
  void   *pReq;
593
  int32_t len;
594

595
  (void)taosThreadMutexLock(&pVnode->mutex);
12,642,022✔
596
  if (pVnode->disableWrite) {
12,642,346!
597
    (void)taosThreadMutexUnlock(&pVnode->mutex);
×
598
    vError("vgId:%d write is disabled for snapshot, version:%" PRId64, TD_VID(pVnode), ver);
×
599
    return TSDB_CODE_VND_WRITE_DISABLED;
×
600
  }
601
  (void)taosThreadMutexUnlock(&pVnode->mutex);
12,642,346✔
602

603
  if (ver <= pVnode->state.applied) {
12,642,356!
604
    vError("vgId:%d, duplicate write request. ver: %" PRId64 ", applied: %" PRId64 "", TD_VID(pVnode), ver,
×
605
           pVnode->state.applied);
606
    return terrno = TSDB_CODE_VND_DUP_REQUEST;
×
607
  }
608

609
  vDebug("vgId:%d, start to process write request %s, index:%" PRId64 ", applied:%" PRId64 ", state.applyTerm:%" PRId64
12,642,356!
610
         ", conn.applyTerm:%" PRId64,
611
         TD_VID(pVnode), TMSG_INFO(pMsg->msgType), ver, pVnode->state.applied, pVnode->state.applyTerm,
612
         pMsg->info.conn.applyTerm);
613

614
  if (!(pVnode->state.applyTerm <= pMsg->info.conn.applyTerm)) {
12,642,194!
615
    return terrno = TSDB_CODE_INTERNAL_ERROR;
×
616
  }
617

618
  if (!(pVnode->state.applied + 1 == ver)) {
12,642,194!
619
    return terrno = TSDB_CODE_INTERNAL_ERROR;
×
620
  }
621

622
  atomic_store_64(&pVnode->state.applied, ver);
12,642,194✔
623
  atomic_store_64(&pVnode->state.applyTerm, pMsg->info.conn.applyTerm);
12,642,402✔
624

625
  if (!syncUtilUserCommit(pMsg->msgType)) goto _exit;
12,642,381✔
626

627
  // skip header
628
  pReq = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
12,618,985✔
629
  len = pMsg->contLen - sizeof(SMsgHead);
12,618,985✔
630
  bool needCommit = false;
12,618,985✔
631

632
  switch (pMsg->msgType) {
12,618,985!
633
    /* META */
634
    case TDMT_VND_CREATE_STB:
23,184✔
635
      if (vnodeProcessCreateStbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
23,184✔
636
      break;
23,290✔
637
    case TDMT_VND_ALTER_STB:
7,191✔
638
      if (vnodeProcessAlterStbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
7,191!
639
      break;
7,234✔
640
    case TDMT_VND_DROP_STB:
2,124✔
641
      if (vnodeProcessDropStbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
2,124!
642
      break;
2,139✔
643
    case TDMT_VND_CREATE_TABLE:
60,415✔
644
      if (vnodeProcessCreateTbReq(pVnode, ver, pReq, len, pRsp, pMsg) < 0) goto _err;
60,415!
645
      break;
60,438✔
646
    case TDMT_VND_ALTER_TABLE:
902✔
647
      if (vnodeProcessAlterTbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
902!
648
      break;
902✔
649
    case TDMT_VND_DROP_TABLE:
1,869✔
650
      if (vnodeProcessDropTbReq(pVnode, ver, pReq, len, pRsp, pMsg) < 0) goto _err;
1,869!
651
      break;
1,869✔
652
    case TDMT_VND_DROP_TTL_TABLE:
×
653
      if (vnodeProcessDropTtlTbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
654
      break;
×
655
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
5✔
656
      if (vnodeProcessFetchTtlExpiredTbs(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
5!
657
      break;
5✔
658
    case TDMT_VND_TRIM:
13✔
659
      if (vnodeProcessTrimReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
13!
660
      break;
13✔
661
    case TDMT_VND_S3MIGRATE:
×
662
      if (vnodeProcessS3MigrateReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
663
      break;
×
664
    case TDMT_VND_CREATE_SMA:
27✔
665
      if (vnodeProcessCreateTSmaReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
27!
666
      break;
27✔
667
    /* TSDB */
668
    case TDMT_VND_SUBMIT:
12,436,299✔
669
      if (vnodeProcessSubmitReq(pVnode, ver, pMsg->pCont, pMsg->contLen, pRsp, pMsg) < 0) goto _err;
12,436,299✔
670
      break;
12,436,174✔
671
    case TDMT_VND_DELETE:
20,141✔
672
      if (vnodeProcessDeleteReq(pVnode, ver, pReq, len, pRsp, pMsg) < 0) goto _err;
20,141!
673
      break;
20,141✔
674
    case TDMT_VND_BATCH_DEL:
2,857✔
675
      if (vnodeProcessBatchDeleteReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
2,857!
676
      break;
2,859✔
677
    /* TQ */
678
    case TDMT_VND_TMQ_SUBSCRIBE:
2,950✔
679
      if (tqProcessSubscribeReq(pVnode->pTq, ver, pReq, len) < 0) {
2,950✔
680
        goto _err;
1✔
681
      }
682
      break;
2,953✔
683
    case TDMT_VND_TMQ_DELETE_SUB:
816✔
684
      if (tqProcessDeleteSubReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen) < 0) {
816!
685
        goto _err;
×
686
      }
687
      break;
817✔
688
    case TDMT_VND_TMQ_COMMIT_OFFSET:
6,700✔
689
      if (tqProcessOffsetCommitReq(pVnode->pTq, ver, pReq, len) < 0) {
6,700!
690
        goto _err;
×
691
      }
692
      break;
6,715✔
693
    case TDMT_VND_TMQ_ADD_CHECKINFO:
117✔
694
      if (tqProcessAddCheckInfoReq(pVnode->pTq, ver, pReq, len) < 0) {
117!
695
        goto _err;
×
696
      }
697
      break;
117✔
698
    case TDMT_VND_TMQ_DEL_CHECKINFO:
12✔
699
      if (tqProcessDelCheckInfoReq(pVnode->pTq, ver, pReq, len) < 0) {
12✔
700
        goto _err;
2✔
701
      }
702
      break;
10✔
703
    case TDMT_STREAM_TASK_DEPLOY: {
13,604✔
704
      if ((code = tqProcessTaskDeployReq(pVnode->pTq, ver, pReq, len)) != TSDB_CODE_SUCCESS) {
13,604!
705
        goto _err;
×
706
      }
707
    } break;
13,629✔
708
    case TDMT_STREAM_TASK_DROP: {
6,859✔
709
      if ((code = tqProcessTaskDropReq(pVnode->pTq, pMsg->pCont, pMsg->contLen)) < 0) {
6,859!
710
        goto _err;
×
711
      }
712
    } break;
6,881✔
713
    case TDMT_STREAM_TASK_UPDATE_CHKPT: {
3,590✔
714
      if ((code = tqProcessTaskUpdateCheckpointReq(pVnode->pTq, pMsg->pCont, pMsg->contLen)) < 0) {
3,590!
715
        goto _err;
×
716
      }
717
    } break;
3,593✔
718
    case TDMT_STREAM_CONSEN_CHKPT: {
124✔
719
      if (pVnode->restored && (code = tqProcessTaskConsenChkptIdReq(pVnode->pTq, pMsg)) < 0) {
124!
720
        goto _err;
×
721
      }
722

723
    } break;
124✔
724
    case TDMT_STREAM_TASK_PAUSE: {
2,041✔
725
      if (pVnode->restored && vnodeIsLeader(pVnode) &&
3,434!
726
          (code = tqProcessTaskPauseReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen)) < 0) {
1,387✔
727
        goto _err;
×
728
      }
729
    } break;
2,048✔
730
    case TDMT_STREAM_TASK_RESUME: {
2,030✔
731
      if (pVnode->restored && vnodeIsLeader(pVnode) &&
3,415!
732
          (code = tqProcessTaskResumeReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen)) < 0) {
1,381✔
733
        goto _err;
×
734
      }
735
    } break;
2,039✔
736
    case TDMT_VND_STREAM_TASK_RESET: {
×
737
      if (pVnode->restored && vnodeIsLeader(pVnode) && (code = tqProcessTaskResetReq(pVnode->pTq, pMsg)) < 0) {
×
738
        goto _err;
×
739
      }
740

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

786
  vTrace("vgId:%d, process %s request, code:0x%x index:%" PRId64, TD_VID(pVnode), TMSG_INFO(pMsg->msgType), pRsp->code,
12,618,944!
787
         ver);
788

789
  walApplyVer(pVnode->pWal, ver);
12,618,944✔
790

791
  code = tqPushMsg(pVnode->pTq, pMsg->msgType);
12,618,904✔
792
  if (code) {
12,619,013✔
793
    vError("vgId:%d, failed to push msg to TQ since %s", TD_VID(pVnode), tstrerror(terrno));
420!
794
    return code;
420✔
795
  }
796

797
  // commit if need
798
  if (needCommit) {
12,618,593✔
799
    vInfo("vgId:%d, commit at version %" PRId64, TD_VID(pVnode), ver);
13,865!
800
    code = vnodeAsyncCommit(pVnode);
13,883✔
801
    if (code) {
13,890!
802
      vError("vgId:%d, failed to vnode async commit since %s.", TD_VID(pVnode), tstrerror(terrno));
×
803
      goto _err;
×
804
    }
805

806
    // start a new one
807
    code = vnodeBegin(pVnode);
13,890✔
808
    if (code) {
13,887✔
809
      vError("vgId:%d, failed to begin vnode since %s.", TD_VID(pVnode), tstrerror(terrno));
6!
810
      goto _err;
×
811
    }
812
  }
813

814
_exit:
12,618,609✔
815
  return 0;
12,641,793✔
816

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

823
int32_t vnodePreprocessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg) {
1,370,974✔
824
  if (TDMT_SCH_QUERY != pMsg->msgType && TDMT_SCH_MERGE_QUERY != pMsg->msgType) {
1,370,974✔
825
    return 0;
103,532✔
826
  }
827

828
  return qWorkerPreprocessQueryMsg(pVnode->pQuery, pMsg, TDMT_SCH_QUERY == pMsg->msgType);
1,267,442✔
829
}
830

831
int32_t vnodeProcessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
1,373,776✔
832
  vTrace("message in vnode query queue is processing");
1,373,776✔
833
  if (pMsg->msgType == TDMT_VND_TMQ_CONSUME && !syncIsReadyForRead(pVnode->sync)) {
1,373,776✔
834
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
1,388✔
835
    return 0;
1,388✔
836
  }
837

838
  if (pMsg->msgType == TDMT_VND_TMQ_CONSUME && !pVnode->restored) {
1,372,387!
839
    vnodeRedirectRpcMsg(pVnode, pMsg, TSDB_CODE_SYN_RESTORING);
×
840
    return 0;
×
841
  }
842

843
  SReadHandle handle = {.vnode = pVnode, .pMsgCb = &pVnode->msgCb, .pWorkerCb = pInfo->workerCb};
1,372,387✔
844
  initStorageAPI(&handle.api);
1,372,387✔
845
  int32_t code = TSDB_CODE_SUCCESS;
1,372,297✔
846
  bool redirected = false;
1,372,297✔
847

848
  switch (pMsg->msgType) {
1,372,297✔
849
    case TDMT_SCH_QUERY:
1,134,821✔
850
      if (!syncIsReadyForRead(pVnode->sync)) {
1,134,821✔
851
        pMsg->code = (terrno) ? terrno : TSDB_CODE_SYN_NOT_LEADER;
1,609!
852
        redirected = true;
1,609✔
853
      }
854
      code = qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
1,134,862✔
855
      if (redirected) {
1,134,297✔
856
        vnodeRedirectRpcMsg(pVnode, pMsg, pMsg->code);
1,373✔
857
        return 0;
1,609✔
858
      }
859
      return code;
1,132,924✔
860
    case TDMT_SCH_MERGE_QUERY:
135,316✔
861
      return qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
135,316✔
862
    case TDMT_SCH_QUERY_CONTINUE:
9,974✔
863
      return qWorkerProcessCQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
9,974✔
864
    case TDMT_VND_TMQ_CONSUME:
67,935✔
865
      return tqProcessPollReq(pVnode->pTq, pMsg);
67,935✔
866
    case TDMT_VND_TMQ_CONSUME_PUSH:
24,226✔
867
      return tqProcessPollPush(pVnode->pTq, pMsg);
24,226✔
868
    default:
25✔
869
      vError("unknown msg type:%d in query queue", pMsg->msgType);
25!
UNCOV
870
      return TSDB_CODE_APP_ERROR;
×
871
  }
872
}
873

874
int32_t vnodeProcessFetchMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
3,305,999✔
875
  vTrace("vgId:%d, msg:%p in fetch queue is processing", pVnode->config.vgId, pMsg);
3,305,999✔
876
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
3,306,000!
877
       pMsg->msgType == TDMT_VND_BATCH_META || pMsg->msgType == TDMT_VND_TABLE_NAME) &&
2,170,337✔
878
      !syncIsReadyForRead(pVnode->sync)) {
1,183,607✔
879
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
2,470✔
880
    return 0;
2,470✔
881
  }
882

883
  switch (pMsg->msgType) {
3,303,785!
884
    case TDMT_SCH_FETCH:
1,302,615✔
885
    case TDMT_SCH_MERGE_FETCH:
886
      return qWorkerProcessFetchMsg(pVnode, pVnode->pQuery, pMsg, 0);
1,302,615✔
UNCOV
887
    case TDMT_SCH_FETCH_RSP:
×
888
      return qWorkerProcessRspMsg(pVnode, pVnode->pQuery, pMsg, 0);
×
889
    // case TDMT_SCH_CANCEL_TASK:
890
    //   return qWorkerProcessCancelMsg(pVnode, pVnode->pQuery, pMsg, 0);
891
    case TDMT_SCH_DROP_TASK:
1,268,583✔
892
      return qWorkerProcessDropMsg(pVnode, pVnode->pQuery, pMsg, 0);
1,268,583✔
UNCOV
893
    case TDMT_SCH_TASK_NOTIFY:
×
UNCOV
894
      return qWorkerProcessNotifyMsg(pVnode, pVnode->pQuery, pMsg, 0);
×
895
    case TDMT_SCH_QUERY_HEARTBEAT:
686,558✔
896
      return qWorkerProcessHbMsg(pVnode, pVnode->pQuery, pMsg, 0);
686,558✔
897
    case TDMT_VND_TABLE_META:
1,774✔
898
    case TDMT_VND_TABLE_NAME:
899
      return vnodeGetTableMeta(pVnode, pMsg, true);
1,774✔
UNCOV
900
    case TDMT_VND_TABLE_CFG:
×
UNCOV
901
      return vnodeGetTableCfg(pVnode, pMsg, true);
×
902
    case TDMT_VND_BATCH_META:
44,176✔
903
      return vnodeGetBatchMeta(pVnode, pMsg);
44,176✔
904
#ifdef TD_ENTERPRISE
905
    case TDMT_VND_QUERY_COMPACT_PROGRESS:
36✔
906
      return vnodeQueryCompactProgress(pVnode, pMsg);
36✔
907
#endif
908
      //    case TDMT_VND_TMQ_CONSUME:
909
      //      return tqProcessPollReq(pVnode->pTq, pMsg);
910
    case TDMT_VND_TMQ_VG_WALINFO:
13✔
911
      return tqProcessVgWalInfoReq(pVnode->pTq, pMsg);
13✔
912
    case TDMT_VND_TMQ_VG_COMMITTEDINFO:
2✔
913
      return tqProcessVgCommittedInfoReq(pVnode->pTq, pMsg);
2✔
914
    case TDMT_VND_TMQ_SEEK:
28✔
915
      return tqProcessSeekReq(pVnode->pTq, pMsg);
28✔
916

917
    default:
×
UNCOV
918
      vError("unknown msg type:%d in fetch queue", pMsg->msgType);
×
UNCOV
919
      return TSDB_CODE_APP_ERROR;
×
920
  }
921
}
922

923
int32_t vnodeProcessStreamMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
326,389✔
924
  vTrace("vgId:%d, msg:%p in fetch queue is processing", pVnode->config.vgId, pMsg);
326,389✔
925
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
326,389!
926
       pMsg->msgType == TDMT_VND_BATCH_META) &&
326,417✔
UNCOV
927
      !syncIsReadyForRead(pVnode->sync)) {
×
UNCOV
928
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
×
UNCOV
929
    return 0;
×
930
  }
931

932
  switch (pMsg->msgType) {
326,404!
933
    case TDMT_STREAM_TASK_RUN:
140,149✔
934
      return tqProcessTaskRunReq(pVnode->pTq, pMsg);
140,149✔
935
    case TDMT_STREAM_TASK_DISPATCH:
56,483✔
936
      return tqProcessTaskDispatchReq(pVnode->pTq, pMsg);
56,483✔
937
    case TDMT_STREAM_TASK_DISPATCH_RSP:
56,499✔
938
      return tqProcessTaskDispatchRsp(pVnode->pTq, pMsg);
56,499✔
939
    case TDMT_VND_STREAM_TASK_CHECK:
20,811✔
940
      return tqProcessTaskCheckReq(pVnode->pTq, pMsg);
20,811✔
941
    case TDMT_VND_STREAM_TASK_CHECK_RSP:
20,885✔
942
      return tqProcessTaskCheckRsp(pVnode->pTq, pMsg);
20,885✔
943
    case TDMT_STREAM_RETRIEVE:
539✔
944
      return tqProcessTaskRetrieveReq(pVnode->pTq, pMsg);
539✔
945
    case TDMT_STREAM_RETRIEVE_RSP:
428✔
946
      return tqProcessTaskRetrieveRsp(pVnode->pTq, pMsg);
428✔
947
    case TDMT_VND_STREAM_SCAN_HISTORY:
2,564✔
948
      return tqProcessTaskScanHistory(pVnode->pTq, pMsg);
2,564✔
949
    case TDMT_STREAM_TASK_CHECKPOINT_READY:
6,622✔
950
      return tqProcessTaskCheckpointReadyMsg(pVnode->pTq, pMsg);
6,622✔
951
    case TDMT_STREAM_TASK_CHECKPOINT_READY_RSP:
6,618✔
952
      return tqProcessTaskCheckpointReadyRsp(pVnode->pTq, pMsg);
6,618✔
953
    case TDMT_STREAM_RETRIEVE_TRIGGER:
×
954
      return tqProcessTaskRetrieveTriggerReq(pVnode->pTq, pMsg);
×
955
    case TDMT_STREAM_RETRIEVE_TRIGGER_RSP:
×
UNCOV
956
      return tqProcessTaskRetrieveTriggerRsp(pVnode->pTq, pMsg);
×
957
    case TDMT_MND_STREAM_HEARTBEAT_RSP:
6,098✔
958
      return tqProcessStreamHbRsp(pVnode->pTq, pMsg);
6,098✔
959
    case TDMT_MND_STREAM_REQ_CHKPT_RSP:
4,445✔
960
      return tqProcessStreamReqCheckpointRsp(pVnode->pTq, pMsg);
4,445✔
UNCOV
961
    case TDMT_VND_GET_STREAM_PROGRESS:
×
962
      return tqStreamProgressRetrieveReq(pVnode->pTq, pMsg);
×
963
    case TDMT_MND_STREAM_CHKPT_REPORT_RSP:
4,263✔
964
      return tqProcessTaskChkptReportRsp(pVnode->pTq, pMsg);
4,263✔
UNCOV
965
    default:
×
UNCOV
966
      vError("unknown msg type:%d in stream queue", pMsg->msgType);
×
UNCOV
967
      return TSDB_CODE_APP_ERROR;
×
968
  }
969
}
970

971
void smaHandleRes(void *pVnode, int64_t smaId, const SArray *data) {
4✔
972
  int32_t code = tdProcessTSmaInsert(((SVnode *)pVnode)->pSma, smaId, (const char *)data);
4✔
973
  if (code) {
4!
UNCOV
974
    vError("failed to process sma result since %s", tstrerror(code));
×
975
  }
976
}
4✔
977

978
void vnodeUpdateMetaRsp(SVnode *pVnode, STableMetaRsp *pMetaRsp) {
136,936✔
979
  if (NULL == pMetaRsp) {
136,936!
UNCOV
980
    return;
×
981
  }
982

983
  tstrncpy(pMetaRsp->dbFName, pVnode->config.dbname, TSDB_DB_FNAME_LEN);
136,936✔
984
  pMetaRsp->dbId = pVnode->config.dbId;
136,936✔
985
  pMetaRsp->vgId = TD_VID(pVnode);
136,936✔
986
  pMetaRsp->precision = pVnode->config.tsdbCfg.precision;
136,936✔
987
}
988

989
extern int32_t vnodeAsyncRetention(SVnode *pVnode, int64_t now);
990

991
static int32_t vnodeProcessTrimReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
13✔
992
  if (!pVnode->restored) {
13✔
993
    vInfo("vgId:%d, ignore trim req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
7!
994
    return 0;
7✔
995
  }
996

997
  int32_t     code = 0;
6✔
998
  SVTrimDbReq trimReq = {0};
6✔
999

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

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

1008
  code = vnodeAsyncRetention(pVnode, trimReq.timestamp);
6✔
1009

1010
_exit:
6✔
1011
  return code;
6✔
1012
}
1013

1014
extern int32_t vnodeAsyncS3Migrate(SVnode *pVnode, int64_t now);
1015

1016
static int32_t vnodeProcessS3MigrateReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
UNCOV
1017
  int32_t          code = 0;
×
1018
  SVS3MigrateDbReq s3migrateReq = {0};
×
1019

1020
  // decode
UNCOV
1021
  if (tDeserializeSVS3MigrateDbReq(pReq, len, &s3migrateReq) != 0) {
×
1022
    code = TSDB_CODE_INVALID_MSG;
×
1023
    goto _exit;
×
1024
  }
1025

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

UNCOV
1028
  code = vnodeAsyncS3Migrate(pVnode, s3migrateReq.timestamp);
×
1029

1030
_exit:
×
1031
  return code;
×
1032
}
1033

UNCOV
1034
static int32_t vnodeProcessDropTtlTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1035
  int               ret = 0;
×
1036
  SVDropTtlTableReq ttlReq = {0};
×
UNCOV
1037
  if (tDeserializeSVDropTtlTableReq(pReq, len, &ttlReq) != 0) {
×
UNCOV
1038
    ret = TSDB_CODE_INVALID_MSG;
×
UNCOV
1039
    goto end;
×
1040
  }
1041

1042
  if (ttlReq.nUids != taosArrayGetSize(ttlReq.pTbUids)) {
×
UNCOV
1043
    ret = TSDB_CODE_INVALID_MSG;
×
1044
    goto end;
×
1045
  }
1046

UNCOV
1047
  if (ttlReq.nUids != 0) {
×
UNCOV
1048
    vInfo("vgId:%d, drop ttl table req will be processed, time:%d, ntbUids:%d", pVnode->config.vgId,
×
1049
          ttlReq.timestampSec, ttlReq.nUids);
1050
  }
1051

1052
  if (ttlReq.nUids > 0) {
×
UNCOV
1053
    int32_t code = metaDropMultipleTables(pVnode->pMeta, ver, ttlReq.pTbUids);
×
UNCOV
1054
    if (code) return code;
×
1055

UNCOV
1056
    code = tqUpdateTbUidList(pVnode->pTq, ttlReq.pTbUids, false);
×
UNCOV
1057
    if (code) {
×
UNCOV
1058
      vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1059
    }
1060
  }
1061

UNCOV
1062
end:
×
UNCOV
1063
  taosArrayDestroy(ttlReq.pTbUids);
×
UNCOV
1064
  return ret;
×
1065
}
1066

1067
static int32_t vnodeProcessFetchTtlExpiredTbs(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
5✔
1068
  int32_t                 code = -1;
5✔
1069
  SMetaReader             mr = {0};
5✔
1070
  SVDropTtlTableReq       ttlReq = {0};
5✔
1071
  SVFetchTtlExpiredTbsRsp rsp = {0};
5✔
1072
  SEncoder                encoder = {0};
5✔
1073
  SArray                 *pNames = NULL;
5✔
1074
  pRsp->msgType = TDMT_VND_FETCH_TTL_EXPIRED_TBS_RSP;
5✔
1075
  pRsp->code = TSDB_CODE_SUCCESS;
5✔
1076
  pRsp->pCont = NULL;
5✔
1077
  pRsp->contLen = 0;
5✔
1078

1079
  if (tDeserializeSVDropTtlTableReq(pReq, len, &ttlReq) != 0) {
5!
UNCOV
1080
    terrno = TSDB_CODE_INVALID_MSG;
×
UNCOV
1081
    goto _end;
×
1082
  }
1083

1084
  if (!(ttlReq.nUids == taosArrayGetSize(ttlReq.pTbUids))) {
5!
UNCOV
1085
    terrno = TSDB_CODE_INVALID_MSG;
×
UNCOV
1086
    goto _end;
×
1087
  }
1088

1089
  tb_uid_t    suid;
1090
  char        ctbName[TSDB_TABLE_NAME_LEN];
1091
  SVDropTbReq expiredTb = {.igNotExists = true};
5✔
1092
  metaReaderDoInit(&mr, pVnode->pMeta, 0);
5✔
1093
  rsp.vgId = TD_VID(pVnode);
5✔
1094
  rsp.pExpiredTbs = taosArrayInit(ttlReq.nUids, sizeof(SVDropTbReq));
5✔
1095
  if (!rsp.pExpiredTbs) goto _end;
5!
1096

1097
  pNames = taosArrayInit(ttlReq.nUids, TSDB_TABLE_NAME_LEN);
5✔
1098
  if (!pNames) {
5!
1099
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
1100
    goto _end;
×
1101
  }
1102
  char buf[TSDB_TABLE_NAME_LEN];
1103
  for (int32_t i = 0; i < ttlReq.nUids; ++i) {
10✔
1104
    tb_uid_t *uid = taosArrayGet(ttlReq.pTbUids, i);
5✔
1105
    expiredTb.suid = *uid;
5✔
1106
    terrno = metaReaderGetTableEntryByUid(&mr, *uid);
5✔
1107
    if (terrno < 0) goto _end;
5!
1108
    tstrncpy(buf, mr.me.name, TSDB_TABLE_NAME_LEN);
5✔
1109
    void *p = taosArrayPush(pNames, buf);
5✔
1110
    if (p == NULL) {
5!
UNCOV
1111
      goto _end;
×
1112
    }
1113

1114
    expiredTb.name = p;
5✔
1115
    if (mr.me.type == TSDB_CHILD_TABLE) {
5!
1116
      expiredTb.suid = mr.me.ctbEntry.suid;
5✔
1117
    }
1118

1119
    if (taosArrayPush(rsp.pExpiredTbs, &expiredTb) == NULL) {
10!
UNCOV
1120
      goto _end;
×
1121
    }
1122
  }
1123

1124
  int32_t ret = 0;
5✔
1125
  tEncodeSize(tEncodeVFetchTtlExpiredTbsRsp, &rsp, pRsp->contLen, ret);
5!
1126
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
5✔
1127
  if (pRsp->pCont == NULL) {
5!
UNCOV
1128
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
1129
    code = -1;
×
UNCOV
1130
    goto _end;
×
1131
  }
1132
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
5✔
1133
  terrno = tEncodeVFetchTtlExpiredTbsRsp(&encoder, &rsp);
5✔
1134
  tEncoderClear(&encoder);
5✔
1135

1136
  if (terrno == 0) code = 0;
5!
UNCOV
1137
_end:
×
1138
  metaReaderClear(&mr);
5✔
1139
  tFreeFetchTtlExpiredTbsRsp(&rsp);
5✔
1140
  taosArrayDestroy(ttlReq.pTbUids);
5✔
1141
  if (pNames) taosArrayDestroy(pNames);
5!
1142
  pRsp->code = terrno;
5✔
1143
  return code;
5✔
1144
}
1145

1146
static int32_t vnodeProcessCreateStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
23,176✔
1147
  int32_t        code = 0;
23,176✔
1148
  SVCreateStbReq req = {0};
23,176✔
1149
  SDecoder       coder;
1150

1151
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
23,176✔
1152
  pRsp->code = TSDB_CODE_SUCCESS;
23,176✔
1153
  pRsp->pCont = NULL;
23,176✔
1154
  pRsp->contLen = 0;
23,176✔
1155

1156
  // decode and process req
1157
  tDecoderInit(&coder, pReq, len);
23,176✔
1158

1159
  code = tDecodeSVCreateStbReq(&coder, &req);
23,185✔
1160
  if (code) {
23,173!
1161
    pRsp->code = code;
×
UNCOV
1162
    goto _err;
×
1163
  }
1164

1165
  code = metaCreateSuperTable(pVnode->pMeta, ver, &req);
23,173✔
1166
  if (code) {
23,312✔
1167
    pRsp->code = code;
4✔
1168
    goto _err;
4✔
1169
  }
1170

1171
  if ((code = tdProcessRSmaCreate(pVnode->pSma, &req)) < 0) {
23,308!
UNCOV
1172
    pRsp->code = code;
×
UNCOV
1173
    goto _err;
×
1174
  }
1175

1176
  tDecoderClear(&coder);
23,299✔
1177
  return 0;
23,272✔
1178

1179
_err:
4✔
1180
  tDecoderClear(&coder);
4✔
1181
  return code;
4✔
1182
}
1183

1184
static int32_t vnodeProcessCreateTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
60,411✔
1185
                                       SRpcMsg *pOriginRpc) {
1186
  SDecoder           decoder = {0};
60,411✔
1187
  SEncoder           encoder = {0};
60,411✔
1188
  int32_t            rcode = 0;
60,411✔
1189
  SVCreateTbBatchReq req = {0};
60,411✔
1190
  SVCreateTbReq     *pCreateReq;
1191
  SVCreateTbBatchRsp rsp = {0};
60,411✔
1192
  SVCreateTbRsp      cRsp = {0};
60,411✔
1193
  char               tbName[TSDB_TABLE_FNAME_LEN];
1194
  STbUidStore       *pStore = NULL;
60,411✔
1195
  SArray            *tbUids = NULL;
60,411✔
1196
  SArray            *tbNames = NULL;
60,411✔
1197
  pRsp->msgType = TDMT_VND_CREATE_TABLE_RSP;
60,411✔
1198
  pRsp->code = TSDB_CODE_SUCCESS;
60,411✔
1199
  pRsp->pCont = NULL;
60,411✔
1200
  pRsp->contLen = 0;
60,411✔
1201

1202
  // decode
1203
  tDecoderInit(&decoder, pReq, len);
60,411✔
1204
  if (tDecodeSVCreateTbBatchReq(&decoder, &req) < 0) {
60,413!
UNCOV
1205
    rcode = -1;
×
UNCOV
1206
    terrno = TSDB_CODE_INVALID_MSG;
×
UNCOV
1207
    goto _exit;
×
1208
  }
1209

1210
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(cRsp));
60,433✔
1211
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
60,438✔
1212
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
60,439✔
1213
  if (rsp.pArray == NULL || tbUids == NULL || tbNames == NULL) {
60,439!
UNCOV
1214
    rcode = -1;
×
1215
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1216
    goto _exit;
×
1217
  }
1218

1219
  // loop to create table
1220
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
160,089✔
1221
    pCreateReq = req.pReqs + iReq;
99,645✔
1222
    memset(&cRsp, 0, sizeof(cRsp));
99,645✔
1223

1224
    if (tsEnableAudit && tsEnableAuditCreateTable) {
99,645!
1225
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
99,643!
1226
      if (str == NULL) {
99,639!
UNCOV
1227
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
1228
        rcode = -1;
×
UNCOV
1229
        goto _exit;
×
1230
      }
1231
      tstrncpy(str, pCreateReq->name, TSDB_TABLE_FNAME_LEN);
99,639✔
1232
      if (taosArrayPush(tbNames, &str) == NULL) {
99,649!
1233
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1234
        rcode = -1;
×
UNCOV
1235
        goto _exit;
×
1236
      }
1237
    }
1238

1239
    // validate hash
1240
    (void)tsnprintf(tbName, TSDB_TABLE_FNAME_LEN, "%s.%s", pVnode->config.dbname, pCreateReq->name);
99,651✔
1241
    if (vnodeValidateTableHash(pVnode, tbName) < 0) {
99,648!
UNCOV
1242
      cRsp.code = TSDB_CODE_VND_HASH_MISMATCH;
×
UNCOV
1243
      if (taosArrayPush(rsp.pArray, &cRsp) == NULL) {
×
UNCOV
1244
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
1245
        rcode = -1;
×
UNCOV
1246
        goto _exit;
×
1247
      }
UNCOV
1248
      vError("vgId:%d create-table:%s failed due to hash value mismatch", TD_VID(pVnode), tbName);
×
UNCOV
1249
      continue;
×
1250
    }
1251

1252
    // do create table
1253
    if (metaCreateTable2(pVnode->pMeta, ver, pCreateReq, &cRsp.pMeta) < 0) {
99,640✔
1254
      if (pCreateReq->flags & TD_CREATE_IF_NOT_EXISTS && terrno == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
1,347!
1255
        cRsp.code = TSDB_CODE_SUCCESS;
3✔
1256
      } else {
1257
        cRsp.code = terrno;
1,344✔
1258
      }
1259
    } else {
1260
      cRsp.code = TSDB_CODE_SUCCESS;
98,304✔
1261
      if (tdFetchTbUidList(pVnode->pSma, &pStore, pCreateReq->ctb.suid, pCreateReq->uid) < 0) {
98,304!
1262
        vError("vgId:%d, failed to fetch tbUid list", TD_VID(pVnode));
×
1263
      }
1264
      if (taosArrayPush(tbUids, &pCreateReq->uid) == NULL) {
196,606!
UNCOV
1265
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
1266
        rcode = -1;
×
UNCOV
1267
        goto _exit;
×
1268
      }
1269
      vnodeUpdateMetaRsp(pVnode, cRsp.pMeta);
98,302✔
1270
    }
1271

1272
    if (taosArrayPush(rsp.pArray, &cRsp) == NULL) {
199,299!
UNCOV
1273
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
1274
      rcode = -1;
×
UNCOV
1275
      goto _exit;
×
1276
    }
1277
  }
1278

1279
  vDebug("vgId:%d, add %d new created tables into query table list", TD_VID(pVnode), (int32_t)taosArrayGetSize(tbUids));
60,444✔
1280
  if (tqUpdateTbUidList(pVnode->pTq, tbUids, true) < 0) {
60,444!
1281
    vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(terrno));
×
1282
  }
1283
  if (tdUpdateTbUidList(pVnode->pSma, pStore, true) < 0) {
60,442!
UNCOV
1284
    goto _exit;
×
1285
  }
1286
  pStore = tdUidStoreFree(pStore);
60,441✔
1287

1288
  // prepare rsp
1289
  int32_t ret = 0;
60,441✔
1290
  tEncodeSize(tEncodeSVCreateTbBatchRsp, &rsp, pRsp->contLen, ret);
60,441!
1291
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
60,441✔
1292
  if (pRsp->pCont == NULL) {
60,441!
UNCOV
1293
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
1294
    rcode = -1;
×
1295
    goto _exit;
×
1296
  }
1297
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
60,441✔
1298
  if (tEncodeSVCreateTbBatchRsp(&encoder, &rsp) != 0) {
60,441!
UNCOV
1299
    vError("vgId:%d, failed to encode create table batch response", TD_VID(pVnode));
×
1300
  }
1301

1302
  if (tsEnableAudit && tsEnableAuditCreateTable) {
60,439!
1303
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
60,439✔
1304

1305
    SName name = {0};
60,439✔
1306
    if (tNameFromString(&name, pVnode->config.dbname, T_NAME_ACCT | T_NAME_DB) < 0) {
60,439!
UNCOV
1307
      vError("vgId:%d, failed to get name from string", TD_VID(pVnode));
×
1308
    }
1309

1310
    SStringBuilder sb = {0};
60,436✔
1311
    for (int32_t i = 0; i < tbNames->size; i++) {
160,032✔
1312
      char **key = (char **)taosArrayGet(tbNames, i);
99,600✔
1313
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
99,599✔
1314
      if (i < tbNames->size - 1) {
99,597✔
1315
        taosStringBuilderAppendChar(&sb, ',');
39,163✔
1316
      }
1317
      // taosMemoryFreeClear(*key);
1318
    }
1319

1320
    size_t len = 0;
60,432✔
1321
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
60,432✔
1322

1323
    if (pOriginRpc->info.conn.user != NULL && strlen(pOriginRpc->info.conn.user) > 0) {
60,439!
1324
      auditAddRecord(pOriginRpc, clusterId, "createTable", name.dbname, "", keyJoined, len);
46,253✔
1325
    }
1326

1327
    taosStringBuilderDestroy(&sb);
60,437✔
1328
  }
1329

UNCOV
1330
_exit:
×
1331
  tDeleteSVCreateTbBatchReq(&req);
60,437✔
1332
  taosArrayDestroyEx(rsp.pArray, tFreeSVCreateTbRsp);
60,440✔
1333
  taosArrayDestroy(tbUids);
60,441✔
1334
  tDecoderClear(&decoder);
60,442✔
1335
  tEncoderClear(&encoder);
60,440✔
1336
  taosArrayDestroyP(tbNames, NULL);
60,441✔
1337
  return rcode;
60,441✔
1338
}
1339

1340
static int32_t vnodeProcessAlterStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
7,191✔
1341
  int32_t        code = 0;
7,191✔
1342
  SVCreateStbReq req = {0};
7,191✔
1343
  SDecoder       dc = {0};
7,191✔
1344

1345
  pRsp->msgType = TDMT_VND_ALTER_STB_RSP;
7,191✔
1346
  pRsp->code = TSDB_CODE_SUCCESS;
7,191✔
1347
  pRsp->pCont = NULL;
7,191✔
1348
  pRsp->contLen = 0;
7,191✔
1349

1350
  tDecoderInit(&dc, pReq, len);
7,191✔
1351

1352
  // decode req
1353
  code = tDecodeSVCreateStbReq(&dc, &req);
7,193✔
1354
  if (code) {
7,227!
UNCOV
1355
    tDecoderClear(&dc);
×
UNCOV
1356
    return code;
×
1357
  }
1358

1359
  code = metaAlterSuperTable(pVnode->pMeta, ver, &req);
7,227✔
1360
  if (code) {
7,234!
UNCOV
1361
    pRsp->code = code;
×
UNCOV
1362
    tDecoderClear(&dc);
×
UNCOV
1363
    return code;
×
1364
  }
1365

1366
  tDecoderClear(&dc);
7,234✔
1367

1368
  return 0;
7,234✔
1369
}
1370

1371
static int32_t vnodeProcessDropStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
2,123✔
1372
  SVDropStbReq req = {0};
2,123✔
1373
  int32_t      rcode = TSDB_CODE_SUCCESS;
2,123✔
1374
  SDecoder     decoder = {0};
2,123✔
1375
  SArray      *tbUidList = NULL;
2,123✔
1376

1377
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
2,123✔
1378
  pRsp->pCont = NULL;
2,123✔
1379
  pRsp->contLen = 0;
2,123✔
1380

1381
  // decode request
1382
  tDecoderInit(&decoder, pReq, len);
2,123✔
1383
  if (tDecodeSVDropStbReq(&decoder, &req) < 0) {
2,124!
UNCOV
1384
    rcode = TSDB_CODE_INVALID_MSG;
×
1385
    goto _exit;
×
1386
  }
1387

1388
  // process request
1389
  tbUidList = taosArrayInit(8, sizeof(int64_t));
2,122✔
1390
  if (tbUidList == NULL) goto _exit;
2,135!
1391
  if (metaDropSuperTable(pVnode->pMeta, ver, &req) < 0) {
2,135✔
1392
    rcode = terrno;
14✔
1393
    goto _exit;
14✔
1394
  }
1395

1396
  if (tqUpdateTbUidList(pVnode->pTq, tbUidList, false) < 0) {
2,125!
UNCOV
1397
    rcode = terrno;
×
UNCOV
1398
    goto _exit;
×
1399
  }
1400

1401
  if (tdProcessRSmaDrop(pVnode->pSma, &req) < 0) {
2,125!
UNCOV
1402
    rcode = terrno;
×
UNCOV
1403
    goto _exit;
×
1404
  }
1405

1406
  // return rsp
1407
_exit:
2,125✔
1408
  if (tbUidList) taosArrayDestroy(tbUidList);
2,139!
1409
  pRsp->code = rcode;
2,139✔
1410
  tDecoderClear(&decoder);
2,139✔
1411
  return 0;
2,139✔
1412
}
1413

1414
static int32_t vnodeProcessAlterTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
902✔
1415
  SVAlterTbReq  vAlterTbReq = {0};
902✔
1416
  SVAlterTbRsp  vAlterTbRsp = {0};
902✔
1417
  SDecoder      dc = {0};
902✔
1418
  int32_t       rcode = 0;
902✔
1419
  int32_t       ret;
1420
  SEncoder      ec = {0};
902✔
1421
  STableMetaRsp vMetaRsp = {0};
902✔
1422

1423
  pRsp->msgType = TDMT_VND_ALTER_TABLE_RSP;
902✔
1424
  pRsp->pCont = NULL;
902✔
1425
  pRsp->contLen = 0;
902✔
1426
  pRsp->code = TSDB_CODE_SUCCESS;
902✔
1427

1428
  tDecoderInit(&dc, pReq, len);
902✔
1429

1430
  // decode
1431
  if (tDecodeSVAlterTbReq(&dc, &vAlterTbReq) < 0) {
902!
UNCOV
1432
    vAlterTbRsp.code = TSDB_CODE_INVALID_MSG;
×
UNCOV
1433
    tDecoderClear(&dc);
×
UNCOV
1434
    rcode = -1;
×
UNCOV
1435
    goto _exit;
×
1436
  }
1437

1438
  // process
1439
  if (metaAlterTable(pVnode->pMeta, ver, &vAlterTbReq, &vMetaRsp) < 0) {
902✔
1440
    vAlterTbRsp.code = terrno;
38✔
1441
    tDecoderClear(&dc);
38✔
1442
    rcode = -1;
38✔
1443
    goto _exit;
38✔
1444
  }
1445
  tDecoderClear(&dc);
864✔
1446

1447
  if (NULL != vMetaRsp.pSchemas) {
864✔
1448
    vnodeUpdateMetaRsp(pVnode, &vMetaRsp);
160✔
1449
    vAlterTbRsp.pMeta = &vMetaRsp;
160✔
1450
  }
1451

1452
_exit:
704✔
1453
  taosArrayDestroy(vAlterTbReq.pMultiTag);
902✔
1454
  tEncodeSize(tEncodeSVAlterTbRsp, &vAlterTbRsp, pRsp->contLen, ret);
902!
1455
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
902✔
1456
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
902✔
1457
  if (tEncodeSVAlterTbRsp(&ec, &vAlterTbRsp) != 0) {
902!
UNCOV
1458
    vError("vgId:%d, failed to encode alter table response", TD_VID(pVnode));
×
1459
  }
1460
  tEncoderClear(&ec);
902✔
1461
  if (vMetaRsp.pSchemas) {
902✔
1462
    taosMemoryFree(vMetaRsp.pSchemas);
160!
1463
    taosMemoryFree(vMetaRsp.pSchemaExt);
160!
1464
  }
1465
  return 0;
902✔
1466
}
1467

1468
static int32_t vnodeProcessDropTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
1,869✔
1469
                                     SRpcMsg *pOriginRpc) {
1470
  SVDropTbBatchReq req = {0};
1,869✔
1471
  SVDropTbBatchRsp rsp = {0};
1,869✔
1472
  SDecoder         decoder = {0};
1,869✔
1473
  SEncoder         encoder = {0};
1,869✔
1474
  int32_t          ret;
1475
  SArray          *tbUids = NULL;
1,869✔
1476
  STbUidStore     *pStore = NULL;
1,869✔
1477
  SArray          *tbNames = NULL;
1,869✔
1478

1479
  pRsp->msgType = TDMT_VND_DROP_TABLE_RSP;
1,869✔
1480
  pRsp->pCont = NULL;
1,869✔
1481
  pRsp->contLen = 0;
1,869✔
1482
  pRsp->code = TSDB_CODE_SUCCESS;
1,869✔
1483

1484
  // decode req
1485
  tDecoderInit(&decoder, pReq, len);
1,869✔
1486
  ret = tDecodeSVDropTbBatchReq(&decoder, &req);
1,869✔
1487
  if (ret < 0) {
1,869!
UNCOV
1488
    terrno = TSDB_CODE_INVALID_MSG;
×
UNCOV
1489
    pRsp->code = terrno;
×
UNCOV
1490
    goto _exit;
×
1491
  }
1492

1493
  // process req
1494
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
1,869✔
1495
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(SVDropTbRsp));
1,869✔
1496
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
1,869✔
1497
  if (tbUids == NULL || rsp.pArray == NULL || tbNames == NULL) goto _exit;
1,869!
1498

1499
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
3,745✔
1500
    SVDropTbReq *pDropTbReq = req.pReqs + iReq;
1,876✔
1501
    SVDropTbRsp  dropTbRsp = {0};
1,876✔
1502
    tb_uid_t     tbUid = 0;
1,876✔
1503

1504
    /* code */
1505
    ret = metaDropTable2(pVnode->pMeta, ver, pDropTbReq);
1,876✔
1506
    if (ret < 0) {
1,876!
UNCOV
1507
      if (pDropTbReq->igNotExists && terrno == TSDB_CODE_TDB_TABLE_NOT_EXIST) {
×
UNCOV
1508
        dropTbRsp.code = TSDB_CODE_SUCCESS;
×
1509
      } else {
1510
        dropTbRsp.code = terrno;
×
1511
      }
1512
    } else {
1513
      dropTbRsp.code = TSDB_CODE_SUCCESS;
1,876✔
1514
      if (tbUid > 0) {
1,876!
UNCOV
1515
        if (tdFetchTbUidList(pVnode->pSma, &pStore, pDropTbReq->suid, tbUid) < 0) {
×
UNCOV
1516
          vError("vgId:%d, failed to fetch tbUid list", TD_VID(pVnode));
×
1517
        }
1518
      }
1519
    }
1520

1521
    if (taosArrayPush(rsp.pArray, &dropTbRsp) == NULL) {
3,752!
UNCOV
1522
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1523
      pRsp->code = terrno;
×
1524
      goto _exit;
×
1525
    }
1526

1527
    if (tsEnableAuditCreateTable) {
1,876!
1528
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
1,876!
1529
      if (str == NULL) {
1,876!
UNCOV
1530
        pRsp->code = terrno;
×
1531
        goto _exit;
×
1532
      }
1533
      tstrncpy(str, pDropTbReq->name, TSDB_TABLE_FNAME_LEN);
1,876✔
1534
      if (taosArrayPush(tbNames, &str) == NULL) {
1,876!
1535
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
1536
        pRsp->code = terrno;
×
UNCOV
1537
        goto _exit;
×
1538
      }
1539
    }
1540
  }
1541

1542
  if (tqUpdateTbUidList(pVnode->pTq, tbUids, false) < 0) {
1,869!
1543
    vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(terrno));
×
1544
  }
1545

1546
  if (tdUpdateTbUidList(pVnode->pSma, pStore, false) < 0) {
1,869!
UNCOV
1547
    goto _exit;
×
1548
  }
1549

1550
  if (tsEnableAuditCreateTable) {
1,869!
1551
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
1,869✔
1552

1553
    SName name = {0};
1,869✔
1554
    if (tNameFromString(&name, pVnode->config.dbname, T_NAME_ACCT | T_NAME_DB) != 0) {
1,869!
UNCOV
1555
      vError("vgId:%d, failed to get name from string", TD_VID(pVnode));
×
1556
    }
1557

1558
    SStringBuilder sb = {0};
1,869✔
1559
    for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
3,745✔
1560
      char **key = (char **)taosArrayGet(tbNames, iReq);
1,876✔
1561
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
1,876✔
1562
      if (iReq < req.nReqs - 1) {
1,876✔
1563
        taosStringBuilderAppendChar(&sb, ',');
7✔
1564
      }
1565
      taosMemoryFreeClear(*key);
1,876!
1566
    }
1567

1568
    size_t len = 0;
1,869✔
1569
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
1,869✔
1570

1571
    if (pOriginRpc->info.conn.user != NULL && strlen(pOriginRpc->info.conn.user) > 0) {
1,869!
1572
      auditAddRecord(pOriginRpc, clusterId, "dropTable", name.dbname, "", keyJoined, len);
1,814✔
1573
    }
1574

1575
    taosStringBuilderDestroy(&sb);
1,869✔
1576
  }
1577

UNCOV
1578
_exit:
×
1579
  taosArrayDestroy(tbUids);
1,869✔
1580
  pStore = tdUidStoreFree(pStore);
1,869✔
1581
  tDecoderClear(&decoder);
1,869✔
1582
  tEncodeSize(tEncodeSVDropTbBatchRsp, &rsp, pRsp->contLen, ret);
1,869!
1583
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
1,869✔
1584
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
1,869✔
1585
  if (tEncodeSVDropTbBatchRsp(&encoder, &rsp) != 0) {
1,869!
UNCOV
1586
    vError("vgId:%d, failed to encode drop table batch response", TD_VID(pVnode));
×
1587
  }
1588
  tEncoderClear(&encoder);
1,869✔
1589
  taosArrayDestroy(rsp.pArray);
1,869✔
1590
  taosArrayDestroy(tbNames);
1,869✔
1591
  return 0;
1,869✔
1592
}
1593

1594
#ifdef BUILD_NO_CALL
1595
static int32_t vnodeDebugPrintSingleSubmitMsg(SMeta *pMeta, SSubmitBlk *pBlock, SSubmitMsgIter *msgIter,
1596
                                              const char *tags) {
1597
  SSubmitBlkIter blkIter = {0};
1598
  STSchema      *pSchema = NULL;
1599
  tb_uid_t       suid = 0;
1600
  STSRow        *row = NULL;
1601
  int32_t        rv = -1;
1602

1603
  tInitSubmitBlkIter(msgIter, pBlock, &blkIter);
1604
  if (blkIter.row == NULL) return 0;
1605

1606
  int32_t code = metaGetTbTSchemaNotNull(pMeta, msgIter->suid, TD_ROW_SVER(blkIter.row), 1,
1607
                                         &pSchema);  // TODO: use the real schema
1608
  if (TSDB_CODE_SUCCESS != code) {
1609
    printf("%s:%d no valid schema\n", tags, __LINE__);
1610
    return code;
1611
  }
1612

1613
  suid = msgIter->suid;
1614
  rv = TD_ROW_SVER(blkIter.row);
1615

1616
  char __tags[128] = {0};
1617
  snprintf(__tags, 128, "%s: uid %" PRIi64 " ", tags, msgIter->uid);
1618
  while ((row = tGetSubmitBlkNext(&blkIter))) {
1619
    tdSRowPrint(row, pSchema, __tags);
1620
  }
1621

1622
  taosMemoryFreeClear(pSchema);
1623

1624
  return TSDB_CODE_SUCCESS;
1625
}
1626
#endif
1627
typedef struct SSubmitReqConvertCxt {
1628
  SSubmitMsgIter msgIter;
1629
  SSubmitBlk    *pBlock;
1630
  SSubmitBlkIter blkIter;
1631
  STSRow        *pRow;
1632
  STSRowIter     rowIter;
1633
  SSubmitTbData *pTbData;
1634
  STSchema      *pTbSchema;
1635
  SArray        *pColValues;
1636
} SSubmitReqConvertCxt;
1637

1638
static int32_t vnodeResetTableCxt(SMeta *pMeta, SSubmitReqConvertCxt *pCxt) {
×
1639
  taosMemoryFreeClear(pCxt->pTbSchema);
×
UNCOV
1640
  int32_t code = metaGetTbTSchemaNotNull(pMeta, pCxt->msgIter.suid > 0 ? pCxt->msgIter.suid : pCxt->msgIter.uid,
×
1641
                                         pCxt->msgIter.sversion, 1, &pCxt->pTbSchema);
1642
  if (TSDB_CODE_SUCCESS != code) {
×
1643
    return code;
×
1644
  }
1645
  tdSTSRowIterInit(&pCxt->rowIter, pCxt->pTbSchema);
×
1646

1647
  tDestroySubmitTbData(pCxt->pTbData, TSDB_MSG_FLG_ENCODE);
×
1648
  if (NULL == pCxt->pTbData) {
×
1649
    pCxt->pTbData = taosMemoryCalloc(1, sizeof(SSubmitTbData));
×
UNCOV
1650
    if (NULL == pCxt->pTbData) {
×
UNCOV
1651
      return terrno;
×
1652
    }
1653
  }
1654
  pCxt->pTbData->flags = 0;
×
1655
  pCxt->pTbData->suid = pCxt->msgIter.suid;
×
UNCOV
1656
  pCxt->pTbData->uid = pCxt->msgIter.uid;
×
1657
  pCxt->pTbData->sver = pCxt->msgIter.sversion;
×
1658
  pCxt->pTbData->pCreateTbReq = NULL;
×
1659
  pCxt->pTbData->aRowP = taosArrayInit(128, POINTER_BYTES);
×
1660
  if (NULL == pCxt->pTbData->aRowP) {
×
UNCOV
1661
    return terrno;
×
1662
  }
1663

1664
  taosArrayDestroy(pCxt->pColValues);
×
UNCOV
1665
  pCxt->pColValues = taosArrayInit(pCxt->pTbSchema->numOfCols, sizeof(SColVal));
×
UNCOV
1666
  if (NULL == pCxt->pColValues) {
×
1667
    return terrno;
×
1668
  }
1669
  for (int32_t i = 0; i < pCxt->pTbSchema->numOfCols; ++i) {
×
1670
    SColVal val = COL_VAL_NONE(pCxt->pTbSchema->columns[i].colId, pCxt->pTbSchema->columns[i].type);
×
1671
    if (taosArrayPush(pCxt->pColValues, &val) == NULL) {
×
1672
      return terrno;
×
1673
    }
1674
  }
1675

1676
  return TSDB_CODE_SUCCESS;
×
1677
}
1678

UNCOV
1679
static void vnodeDestroySubmitReqConvertCxt(SSubmitReqConvertCxt *pCxt) {
×
1680
  taosMemoryFreeClear(pCxt->pTbSchema);
×
1681
  tDestroySubmitTbData(pCxt->pTbData, TSDB_MSG_FLG_ENCODE);
×
1682
  taosMemoryFreeClear(pCxt->pTbData);
×
UNCOV
1683
  taosArrayDestroy(pCxt->pColValues);
×
UNCOV
1684
}
×
1685

1686
static int32_t vnodeCellValConvertToColVal(STColumn *pCol, SCellVal *pCellVal, SColVal *pColVal) {
×
1687
  if (tdValTypeIsNone(pCellVal->valType)) {
×
1688
    pColVal->flag = CV_FLAG_NONE;
×
1689
    return TSDB_CODE_SUCCESS;
×
1690
  }
1691

1692
  if (tdValTypeIsNull(pCellVal->valType)) {
×
UNCOV
1693
    pColVal->flag = CV_FLAG_NULL;
×
1694
    return TSDB_CODE_SUCCESS;
×
1695
  }
1696

1697
  if (IS_VAR_DATA_TYPE(pCol->type)) {
×
1698
    pColVal->value.nData = varDataLen(pCellVal->val);
×
UNCOV
1699
    pColVal->value.pData = (uint8_t *)varDataVal(pCellVal->val);
×
UNCOV
1700
  } else if (TSDB_DATA_TYPE_FLOAT == pCol->type) {
×
1701
    float f = GET_FLOAT_VAL(pCellVal->val);
×
1702
    memcpy(&pColVal->value.val, &f, sizeof(f));
×
1703
  } else if (TSDB_DATA_TYPE_DOUBLE == pCol->type) {
×
1704
    pColVal->value.val = *(int64_t *)pCellVal->val;
×
1705
  } else {
1706
    GET_TYPED_DATA(pColVal->value.val, int64_t, pCol->type, pCellVal->val);
×
1707
  }
1708

UNCOV
1709
  pColVal->flag = CV_FLAG_VALUE;
×
1710
  return TSDB_CODE_SUCCESS;
×
1711
}
1712

UNCOV
1713
static int32_t vnodeTSRowConvertToColValArray(SSubmitReqConvertCxt *pCxt) {
×
UNCOV
1714
  int32_t code = TSDB_CODE_SUCCESS;
×
1715
  tdSTSRowIterReset(&pCxt->rowIter, pCxt->pRow);
×
1716
  for (int32_t i = 0; TSDB_CODE_SUCCESS == code && i < pCxt->pTbSchema->numOfCols; ++i) {
×
1717
    STColumn *pCol = pCxt->pTbSchema->columns + i;
×
UNCOV
1718
    SCellVal  cellVal = {0};
×
UNCOV
1719
    if (!tdSTSRowIterFetch(&pCxt->rowIter, pCol->colId, pCol->type, &cellVal)) {
×
1720
      break;
×
1721
    }
1722
    code = vnodeCellValConvertToColVal(pCol, &cellVal, (SColVal *)taosArrayGet(pCxt->pColValues, i));
×
1723
  }
UNCOV
1724
  return code;
×
1725
}
1726

1727
static int32_t vnodeDecodeCreateTbReq(SSubmitReqConvertCxt *pCxt) {
×
1728
  if (pCxt->msgIter.schemaLen <= 0) {
×
UNCOV
1729
    return TSDB_CODE_SUCCESS;
×
1730
  }
1731

UNCOV
1732
  pCxt->pTbData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
×
1733
  if (NULL == pCxt->pTbData->pCreateTbReq) {
×
1734
    return terrno;
×
1735
  }
1736

UNCOV
1737
  SDecoder decoder = {0};
×
UNCOV
1738
  tDecoderInit(&decoder, (uint8_t *)pCxt->pBlock->data, pCxt->msgIter.schemaLen);
×
1739
  int32_t code = tDecodeSVCreateTbReq(&decoder, pCxt->pTbData->pCreateTbReq);
×
UNCOV
1740
  tDecoderClear(&decoder);
×
1741

1742
  return code;
×
1743
}
1744

1745
static int32_t vnodeSubmitReqConvertToSubmitReq2(SVnode *pVnode, SSubmitReq *pReq, SSubmitReq2 *pReq2) {
×
1746
  pReq2->aSubmitTbData = taosArrayInit(128, sizeof(SSubmitTbData));
×
UNCOV
1747
  if (NULL == pReq2->aSubmitTbData) {
×
1748
    return terrno;
×
1749
  }
1750

1751
  SSubmitReqConvertCxt cxt = {0};
×
1752

1753
  int32_t code = tInitSubmitMsgIter(pReq, &cxt.msgIter);
×
1754
  while (TSDB_CODE_SUCCESS == code) {
×
UNCOV
1755
    code = tGetSubmitMsgNext(&cxt.msgIter, &cxt.pBlock);
×
1756
    if (TSDB_CODE_SUCCESS == code) {
×
1757
      if (NULL == cxt.pBlock) {
×
1758
        break;
×
1759
      }
1760
      code = vnodeResetTableCxt(pVnode->pMeta, &cxt);
×
1761
    }
UNCOV
1762
    if (TSDB_CODE_SUCCESS == code) {
×
1763
      code = tInitSubmitBlkIter(&cxt.msgIter, cxt.pBlock, &cxt.blkIter);
×
1764
    }
UNCOV
1765
    if (TSDB_CODE_SUCCESS == code) {
×
1766
      code = vnodeDecodeCreateTbReq(&cxt);
×
1767
    }
UNCOV
1768
    while (TSDB_CODE_SUCCESS == code && (cxt.pRow = tGetSubmitBlkNext(&cxt.blkIter)) != NULL) {
×
UNCOV
1769
      code = vnodeTSRowConvertToColValArray(&cxt);
×
UNCOV
1770
      if (TSDB_CODE_SUCCESS == code) {
×
1771
        SRow **pNewRow = taosArrayReserve(cxt.pTbData->aRowP, 1);
×
1772
        code = tRowBuild(cxt.pColValues, cxt.pTbSchema, pNewRow);
×
1773
      }
1774
    }
1775
    if (TSDB_CODE_SUCCESS == code) {
×
1776
      code = (NULL == taosArrayPush(pReq2->aSubmitTbData, cxt.pTbData) ? terrno : TSDB_CODE_SUCCESS);
×
1777
    }
1778
    if (TSDB_CODE_SUCCESS == code) {
×
1779
      taosMemoryFreeClear(cxt.pTbData);
×
1780
    }
1781
  }
1782

1783
  vnodeDestroySubmitReqConvertCxt(&cxt);
×
UNCOV
1784
  return code;
×
1785
}
1786

UNCOV
1787
static int32_t vnodeRebuildSubmitReqMsg(SSubmitReq2 *pSubmitReq, void **ppMsg) {
×
1788
  int32_t  code = TSDB_CODE_SUCCESS;
×
1789
  char    *pMsg = NULL;
×
1790
  uint32_t msglen = 0;
×
UNCOV
1791
  tEncodeSize(tEncodeSubmitReq, pSubmitReq, msglen, code);
×
1792
  if (TSDB_CODE_SUCCESS == code) {
×
1793
    pMsg = taosMemoryMalloc(msglen);
×
UNCOV
1794
    if (NULL == pMsg) {
×
1795
      code = terrno;
×
1796
    }
1797
  }
UNCOV
1798
  if (TSDB_CODE_SUCCESS == code) {
×
1799
    SEncoder encoder;
UNCOV
1800
    tEncoderInit(&encoder, (uint8_t *)pMsg, msglen);
×
UNCOV
1801
    code = tEncodeSubmitReq(&encoder, pSubmitReq);
×
UNCOV
1802
    tEncoderClear(&encoder);
×
1803
  }
UNCOV
1804
  if (TSDB_CODE_SUCCESS == code) {
×
UNCOV
1805
    *ppMsg = pMsg;
×
1806
  }
UNCOV
1807
  return code;
×
1808
}
1809

1810
static int32_t vnodeProcessSubmitReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
12,436,266✔
1811
                                     SRpcMsg *pOriginalMsg) {
1812
  int32_t code = 0;
12,436,266✔
1813
  terrno = 0;
12,436,266✔
1814

1815
  SSubmitReq2 *pSubmitReq = &(SSubmitReq2){0};
12,436,319✔
1816
  SSubmitRsp2 *pSubmitRsp = &(SSubmitRsp2){0};
12,436,319✔
1817
  SArray      *newTbUids = NULL;
12,436,319✔
1818
  int32_t      ret;
1819
  SEncoder     ec = {0};
12,436,319✔
1820

1821
  pRsp->code = TSDB_CODE_SUCCESS;
12,436,319✔
1822

1823
  void           *pAllocMsg = NULL;
12,436,319✔
1824
  SSubmitReq2Msg *pMsg = (SSubmitReq2Msg *)pReq;
12,436,319✔
1825
  if (0 == pMsg->version) {
12,436,319!
UNCOV
1826
    code = vnodeSubmitReqConvertToSubmitReq2(pVnode, (SSubmitReq *)pMsg, pSubmitReq);
×
UNCOV
1827
    if (TSDB_CODE_SUCCESS == code) {
×
UNCOV
1828
      code = vnodeRebuildSubmitReqMsg(pSubmitReq, &pReq);
×
1829
    }
UNCOV
1830
    if (TSDB_CODE_SUCCESS == code) {
×
1831
      pAllocMsg = pReq;
×
1832
    }
UNCOV
1833
    if (TSDB_CODE_SUCCESS != code) {
×
UNCOV
1834
      goto _exit;
×
1835
    }
1836
  } else {
1837
    // decode
1838
    pReq = POINTER_SHIFT(pReq, sizeof(SSubmitReq2Msg));
12,436,319✔
1839
    len -= sizeof(SSubmitReq2Msg);
12,436,319✔
1840
    SDecoder dc = {0};
12,436,319✔
1841
    tDecoderInit(&dc, pReq, len);
12,436,319✔
1842
    if (tDecodeSubmitReq(&dc, pSubmitReq) < 0) {
12,436,123!
UNCOV
1843
      code = TSDB_CODE_INVALID_MSG;
×
UNCOV
1844
      goto _exit;
×
1845
    }
1846
    tDecoderClear(&dc);
12,436,113✔
1847
  }
1848

1849
  // scan
1850
  TSKEY now = taosGetTimestamp(pVnode->config.tsdbCfg.precision);
12,436,202✔
1851
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * pVnode->config.tsdbCfg.keep2;
12,436,228✔
1852
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
12,436,228✔
1853
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
25,571,580✔
1854
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
13,136,230✔
1855

1856
    if (pSubmitTbData->pCreateTbReq && pSubmitTbData->pCreateTbReq->uid == 0) {
13,136,220!
UNCOV
1857
      code = TSDB_CODE_INVALID_MSG;
×
UNCOV
1858
      goto _exit;
×
1859
    }
1860

1861
    if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
13,136,220✔
1862
      if (TARRAY_SIZE(pSubmitTbData->aCol) <= 0) {
131!
1863
        code = TSDB_CODE_INVALID_MSG;
×
1864
        goto _exit;
×
1865
      }
1866

1867
      SColData *colDataArr = TARRAY_DATA(pSubmitTbData->aCol);
131✔
1868
      SRowKey   lastKey;
1869
      tColDataArrGetRowKey(colDataArr, TARRAY_SIZE(pSubmitTbData->aCol), 0, &lastKey);
131✔
1870
      for (int32_t iRow = 1; iRow < colDataArr[0].nVal; iRow++) {
222✔
1871
        SRowKey key;
1872
        tColDataArrGetRowKey(TARRAY_DATA(pSubmitTbData->aCol), TARRAY_SIZE(pSubmitTbData->aCol), iRow, &key);
90✔
1873
        if (tRowKeyCompare(&lastKey, &key) >= 0) {
90!
UNCOV
1874
          code = TSDB_CODE_INVALID_MSG;
×
UNCOV
1875
          vError("vgId:%d %s failed 1 since %s, version:%" PRId64, TD_VID(pVnode), __func__, tstrerror(terrno), ver);
×
UNCOV
1876
          goto _exit;
×
1877
        }
1878
      }
1879
    } else {
1880
      int32_t nRow = TARRAY_SIZE(pSubmitTbData->aRowP);
13,136,089✔
1881
      SRow  **aRow = (SRow **)TARRAY_DATA(pSubmitTbData->aRowP);
13,136,089✔
1882
      SRowKey lastRowKey;
1883
      for (int32_t iRow = 0; iRow < nRow; ++iRow) {
310,249,017✔
1884
        if (aRow[iRow]->ts < minKey || aRow[iRow]->ts > maxKey) {
297,113,797!
1885
          code = TSDB_CODE_INVALID_MSG;
7,893✔
1886
          vError("vgId:%d %s failed 2 since %s, version:%" PRId64, TD_VID(pVnode), __func__, tstrerror(code), ver);
7,893✔
1887
          goto _exit;
1✔
1888
        }
1889
        if (iRow == 0) {
297,105,904✔
1890
          tRowGetKey(aRow[iRow], &lastRowKey);
13,136,105✔
1891
        } else {
1892
          SRowKey rowKey;
1893
          tRowGetKey(aRow[iRow], &rowKey);
283,969,799✔
1894

1895
          if (tRowKeyCompare(&lastRowKey, &rowKey) >= 0) {
283,969,260!
UNCOV
1896
            code = TSDB_CODE_INVALID_MSG;
×
UNCOV
1897
            vError("vgId:%d %s failed 3 since %s, version:%" PRId64, TD_VID(pVnode), __func__, tstrerror(code), ver);
×
UNCOV
1898
            goto _exit;
×
1899
          }
1900
          lastRowKey = rowKey;
283,967,309✔
1901
        }
1902
      }
1903
    }
1904
  }
1905

1906
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
25,572,974✔
1907
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
13,137,105✔
1908

1909
    if (pSubmitTbData->pCreateTbReq) {
13,136,850✔
1910
      pSubmitTbData->uid = pSubmitTbData->pCreateTbReq->uid;
63,123✔
1911
    } else {
1912
      SMetaInfo info = {0};
13,073,727✔
1913

1914
      code = metaGetInfo(pVnode->pMeta, pSubmitTbData->uid, &info, NULL);
13,073,727✔
1915
      if (code) {
13,073,378✔
1916
        code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
26✔
1917
        vWarn("vgId:%d, table uid:%" PRId64 " not exists", TD_VID(pVnode), pSubmitTbData->uid);
26✔
1918
        goto _exit;
4✔
1919
      }
1920

1921
      if (info.suid != pSubmitTbData->suid) {
13,073,352!
1922
        code = TSDB_CODE_INVALID_MSG;
×
UNCOV
1923
        goto _exit;
×
1924
      }
1925

1926
      if (info.suid) {
13,073,352✔
1927
        if (metaGetInfo(pVnode->pMeta, info.suid, &info, NULL) != 0) {
12,289,995!
UNCOV
1928
          vWarn("vgId:%d, table uid:%" PRId64 " not exists", TD_VID(pVnode), info.suid);
×
1929
        }
1930
      }
1931

1932
      if (pSubmitTbData->sver != info.skmVer) {
13,074,501!
UNCOV
1933
        code = TSDB_CODE_TDB_INVALID_TABLE_SCHEMA_VER;
×
UNCOV
1934
        goto _exit;
×
1935
      }
1936
    }
1937

1938
    if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
13,137,624✔
1939
      int32_t   nColData = TARRAY_SIZE(pSubmitTbData->aCol);
132✔
1940
      SColData *aColData = (SColData *)TARRAY_DATA(pSubmitTbData->aCol);
132✔
1941

1942
      if (nColData <= 0) {
132!
1943
        code = TSDB_CODE_INVALID_MSG;
×
1944
        goto _exit;
×
1945
      }
1946

1947
      if (aColData[0].cid != PRIMARYKEY_TIMESTAMP_COL_ID || aColData[0].type != TSDB_DATA_TYPE_TIMESTAMP ||
132!
1948
          aColData[0].nVal <= 0) {
132!
UNCOV
1949
        code = TSDB_CODE_INVALID_MSG;
×
UNCOV
1950
        goto _exit;
×
1951
      }
1952

1953
      for (int32_t j = 1; j < nColData; j++) {
562✔
1954
        if (aColData[j].nVal != aColData[0].nVal) {
430!
UNCOV
1955
          code = TSDB_CODE_INVALID_MSG;
×
UNCOV
1956
          goto _exit;
×
1957
        }
1958
      }
1959
    }
1960
  }
1961

1962
  vDebug("vgId:%d, submit block size %d", TD_VID(pVnode), (int32_t)taosArrayGetSize(pSubmitReq->aSubmitTbData));
12,435,869✔
1963

1964
  // loop to handle
1965
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
25,570,865✔
1966
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
13,134,475✔
1967

1968
    // create table
1969
    if (pSubmitTbData->pCreateTbReq) {
13,134,016✔
1970
      // alloc if need
1971
      if (pSubmitRsp->aCreateTbRsp == NULL &&
63,113✔
1972
          (pSubmitRsp->aCreateTbRsp = taosArrayInit(TARRAY_SIZE(pSubmitReq->aSubmitTbData), sizeof(SVCreateTbRsp))) ==
44,354!
1973
              NULL) {
1974
        code = terrno;
×
1975
        goto _exit;
6✔
1976
      }
1977

1978
      SVCreateTbRsp *pCreateTbRsp = taosArrayReserve(pSubmitRsp->aCreateTbRsp, 1);
63,113✔
1979

1980
      // create table
1981
      if (metaCreateTable2(pVnode->pMeta, ver, pSubmitTbData->pCreateTbReq, &pCreateTbRsp->pMeta) == 0) {
63,113✔
1982
        // create table success
1983

1984
        if (newTbUids == NULL &&
64,011!
1985
            (newTbUids = taosArrayInit(TARRAY_SIZE(pSubmitReq->aSubmitTbData), sizeof(int64_t))) == NULL) {
25,535✔
UNCOV
1986
          code = terrno;
×
UNCOV
1987
          goto _exit;
×
1988
        }
1989

1990
        if (taosArrayPush(newTbUids, &pSubmitTbData->uid) == NULL) {
76,952!
UNCOV
1991
          code = terrno;
×
UNCOV
1992
          goto _exit;
×
1993
        }
1994

1995
        if (pCreateTbRsp->pMeta) {
38,476!
1996
          vnodeUpdateMetaRsp(pVnode, pCreateTbRsp->pMeta);
38,476✔
1997
        }
1998
      } else {  // create table failed
1999
        if (terrno != TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
24,644✔
2000
          code = terrno;
6✔
2001
          vError("vgId:%d failed to create table:%s, code:%s", TD_VID(pVnode), pSubmitTbData->pCreateTbReq->name,
6!
2002
                 tstrerror(terrno));
2003
          goto _exit;
6✔
2004
        }
2005
        terrno = 0;
24,638✔
2006
        pSubmitTbData->uid = pSubmitTbData->pCreateTbReq->uid;  // update uid if table exist for using below
24,638✔
2007
      }
2008
    }
2009

2010
    // insert data
2011
    int32_t affectedRows;
2012
    code = tsdbInsertTableData(pVnode->pTsdb, ver, pSubmitTbData, &affectedRows);
13,134,017✔
2013
    if (code) goto _exit;
13,135,598!
2014

2015
    code = metaUpdateChangeTimeWithLock(pVnode->pMeta, pSubmitTbData->uid, pSubmitTbData->ctimeMs);
13,135,598✔
2016
    if (code) goto _exit;
13,134,546!
2017

2018
    pSubmitRsp->affectedRows += affectedRows;
13,134,546✔
2019
  }
2020

2021
  // update the affected table uid list
2022
  if (taosArrayGetSize(newTbUids) > 0) {
12,436,390✔
2023
    vDebug("vgId:%d, add %d table into query table list in handling submit", TD_VID(pVnode),
25,535✔
2024
           (int32_t)taosArrayGetSize(newTbUids));
2025
    if (tqUpdateTbUidList(pVnode->pTq, newTbUids, true) != 0) {
25,535!
UNCOV
2026
      vError("vgId:%d, failed to update tbUid list", TD_VID(pVnode));
×
2027
    }
2028
  }
2029

2030
_exit:
12,436,132✔
2031
  // message
2032
  pRsp->code = code;
12,436,143✔
2033
  tEncodeSize(tEncodeSSubmitRsp2, pSubmitRsp, pRsp->contLen, ret);
12,436,143!
2034
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
12,436,032✔
2035
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
12,436,228✔
2036
  if (tEncodeSSubmitRsp2(&ec, pSubmitRsp) < 0) {
12,436,236!
2037
    vError("vgId:%d, failed to encode submit response", TD_VID(pVnode));
×
2038
  }
2039
  tEncoderClear(&ec);
12,436,160✔
2040

2041
  // update statistics
2042
  (void)atomic_add_fetch_64(&pVnode->statis.nInsert, pSubmitRsp->affectedRows);
12,436,253✔
2043
  (void)atomic_add_fetch_64(&pVnode->statis.nInsertSuccess, pSubmitRsp->affectedRows);
12,436,291✔
2044
  (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsert, 1);
12,436,350✔
2045

2046
  if (tsEnableMonitor && tsMonitorFqdn[0] != 0 && tsMonitorPort != 0 && pSubmitRsp->affectedRows > 0 &&
12,436,221!
UNCOV
2047
      strlen(pOriginalMsg->info.conn.user) > 0 && tsInsertCounter != NULL) {
×
UNCOV
2048
    const char *sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT_AFFECTED_ROWS,
×
UNCOV
2049
                                   pVnode->monitor.strClusterId,
×
UNCOV
2050
                                   pVnode->monitor.strDnodeId,
×
2051
                                   tsLocalEp,
UNCOV
2052
                                   pVnode->monitor.strVgId,
×
UNCOV
2053
                                   pOriginalMsg->info.conn.user,
×
2054
                                   "Success"};
UNCOV
2055
    int         tv = taos_counter_add(tsInsertCounter, pSubmitRsp->affectedRows, sample_labels);
×
2056
  }
2057

2058
  if (code == 0) {
12,436,221✔
2059
    (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
12,436,212✔
2060
    code = tdProcessRSmaSubmit(pVnode->pSma, ver, pSubmitReq, pReq, len);
12,436,175✔
2061
  }
2062
  /*
2063
  if (code == 0) {
2064
    atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
2065
    code = tdProcessRSmaSubmit(pVnode->pSma, ver, pSubmitReq, pReq, len);
2066

2067
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT, pVnode->monitor.strClusterId,
2068
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2069
                                          pOriginalMsg->info.conn.user, "Success"};
2070
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2071
  }
2072
  else{
2073
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT, pVnode->monitor.strClusterId,
2074
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2075
                                        pOriginalMsg->info.conn.user, "Failed"};
2076
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2077
  }
2078
  */
2079

2080
  // clear
2081
  taosArrayDestroy(newTbUids);
12,436,093✔
2082
  tDestroySubmitReq(pSubmitReq, 0 == pMsg->version ? TSDB_MSG_FLG_CMPT : TSDB_MSG_FLG_DECODE);
12,436,050!
2083
  tDestroySSubmitRsp2(pSubmitRsp, TSDB_MSG_FLG_ENCODE);
12,436,280✔
2084

2085
  if (code) terrno = code;
12,436,218✔
2086

2087
  taosMemoryFree(pAllocMsg);
12,436,218!
2088

2089
  return code;
12,436,227✔
2090
}
2091

2092
static int32_t vnodeProcessCreateTSmaReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
27✔
2093
  SVCreateTSmaReq req = {0};
27✔
2094
  SDecoder        coder = {0};
27✔
2095

2096
  if (pRsp) {
27!
2097
    pRsp->msgType = TDMT_VND_CREATE_SMA_RSP;
27✔
2098
    pRsp->code = TSDB_CODE_SUCCESS;
27✔
2099
    pRsp->pCont = NULL;
27✔
2100
    pRsp->contLen = 0;
27✔
2101
  }
2102

2103
  // decode and process req
2104
  tDecoderInit(&coder, pReq, len);
27✔
2105

2106
  if (tDecodeSVCreateTSmaReq(&coder, &req) < 0) {
27!
UNCOV
2107
    terrno = TSDB_CODE_MSG_DECODE_ERROR;
×
UNCOV
2108
    if (pRsp) pRsp->code = terrno;
×
UNCOV
2109
    goto _err;
×
2110
  }
2111

2112
  if (tdProcessTSmaCreate(pVnode->pSma, ver, (const char *)&req) < 0) {
27!
UNCOV
2113
    if (pRsp) pRsp->code = terrno;
×
2114
    goto _err;
×
2115
  }
2116

2117
  tDecoderClear(&coder);
27✔
2118
  vDebug("vgId:%d, success to create tsma %s:%" PRIi64 " version %" PRIi64 " for table %" PRIi64, TD_VID(pVnode),
27!
2119
         req.indexName, req.indexUid, ver, req.tableUid);
2120
  return 0;
27✔
2121

UNCOV
2122
_err:
×
UNCOV
2123
  tDecoderClear(&coder);
×
UNCOV
2124
  vError("vgId:%d, failed to create tsma %s:%" PRIi64 " version %" PRIi64 "for table %" PRIi64 " since %s",
×
2125
         TD_VID(pVnode), req.indexName, req.indexUid, ver, req.tableUid, terrstr());
2126
  return terrno;
×
2127
}
2128

2129
/**
2130
 * @brief specific for smaDstVnode
2131
 *
2132
 * @param pVnode
2133
 * @param pCont
2134
 * @param contLen
2135
 * @return int32_t
2136
 */
UNCOV
2137
int32_t vnodeProcessCreateTSma(SVnode *pVnode, void *pCont, uint32_t contLen) {
×
UNCOV
2138
  return vnodeProcessCreateTSmaReq(pVnode, 1, pCont, contLen, NULL);
×
2139
}
2140

2141
static int32_t vnodeConsolidateAlterHashRange(SVnode *pVnode, int64_t ver) {
40✔
2142
  int32_t code = TSDB_CODE_SUCCESS;
40✔
2143

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

2147
  // TODO: trim meta of tables from TDB per hash range [pVnode->config.hashBegin, pVnode->config.hashEnd]
2148
  code = metaTrimTables(pVnode->pMeta);
40✔
2149

2150
  return code;
40✔
2151
}
2152

2153
static int32_t vnodeProcessAlterConfirmReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
8,017✔
2154
  vInfo("vgId:%d, vnode handle msgType:alter-confirm, alter confirm msg is processed", TD_VID(pVnode));
8,017!
2155
  int32_t code = TSDB_CODE_SUCCESS;
8,017✔
2156
  if (!pVnode->config.hashChange) {
8,017✔
2157
    goto _exit;
7,977✔
2158
  }
2159

2160
  code = vnodeConsolidateAlterHashRange(pVnode, ver);
40✔
2161
  if (code < 0) {
40!
UNCOV
2162
    vError("vgId:%d, failed to consolidate alter hashrange since %s. version:%" PRId64, TD_VID(pVnode), terrstr(), ver);
×
UNCOV
2163
    goto _exit;
×
2164
  }
2165
  pVnode->config.hashChange = false;
40✔
2166

2167
_exit:
8,017✔
2168
  pRsp->msgType = TDMT_VND_ALTER_CONFIRM_RSP;
8,017✔
2169
  pRsp->code = code;
8,017✔
2170
  pRsp->pCont = NULL;
8,017✔
2171
  pRsp->contLen = 0;
8,017✔
2172

2173
  return code;
8,017✔
2174
}
2175

2176
extern int32_t tsdbDisableAndCancelAllBgTask(STsdb *pTsdb);
2177
extern void    tsdbEnableBgTask(STsdb *pTsdb);
2178

2179
static int32_t vnodeProcessAlterConfigReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
316✔
2180
  bool walChanged = false;
316✔
2181
  bool tsdbChanged = false;
316✔
2182

2183
  SAlterVnodeConfigReq req = {0};
316✔
2184
  if (tDeserializeSAlterVnodeConfigReq(pReq, len, &req) != 0) {
316!
UNCOV
2185
    terrno = TSDB_CODE_INVALID_MSG;
×
UNCOV
2186
    return TSDB_CODE_INVALID_MSG;
×
2187
  }
2188

2189
  vInfo("vgId:%d, start to alter vnode config, page:%d pageSize:%d buffer:%d szPage:%d szBuf:%" PRIu64
317!
2190
        " cacheLast:%d cacheLastSize:%d days:%d keep0:%d keep1:%d keep2:%d keepTimeOffset:%d s3KeepLocal:%d "
2191
        "s3Compact:%d fsync:%d level:%d "
2192
        "walRetentionPeriod:%d walRetentionSize:%d",
2193
        TD_VID(pVnode), req.pages, req.pageSize, req.buffer, req.pageSize * 1024, (uint64_t)req.buffer * 1024 * 1024,
2194
        req.cacheLast, req.cacheLastSize, req.daysPerFile, req.daysToKeep0, req.daysToKeep1, req.daysToKeep2,
2195
        req.keepTimeOffset, req.s3KeepLocal, req.s3Compact, req.walFsyncPeriod, req.walLevel, req.walRetentionPeriod,
2196
        req.walRetentionSize);
2197

2198
  if (pVnode->config.cacheLastSize != req.cacheLastSize) {
318✔
2199
    pVnode->config.cacheLastSize = req.cacheLastSize;
2✔
2200
    tsdbCacheSetCapacity(pVnode, (size_t)pVnode->config.cacheLastSize * 1024 * 1024);
2✔
2201
  }
2202

2203
  if (pVnode->config.szBuf != req.buffer * 1024LL * 1024LL) {
318!
2204
    vInfo("vgId:%d, vnode buffer is changed from %" PRId64 " to %" PRId64, TD_VID(pVnode), pVnode->config.szBuf,
×
2205
          (uint64_t)(req.buffer * 1024LL * 1024LL));
UNCOV
2206
    pVnode->config.szBuf = req.buffer * 1024LL * 1024LL;
×
2207
  }
2208

2209
  if (pVnode->config.szCache != req.pages) {
318!
UNCOV
2210
    if (metaAlterCache(pVnode->pMeta, req.pages) < 0) {
×
UNCOV
2211
      vError("vgId:%d, failed to change vnode pages from %d to %d failed since %s", TD_VID(pVnode),
×
2212
             pVnode->config.szCache, req.pages, tstrerror(errno));
UNCOV
2213
      return errno;
×
2214
    } else {
UNCOV
2215
      vInfo("vgId:%d, vnode pages is changed from %d to %d", TD_VID(pVnode), pVnode->config.szCache, req.pages);
×
UNCOV
2216
      pVnode->config.szCache = req.pages;
×
2217
    }
2218
  }
2219

2220
  if (pVnode->config.cacheLast != req.cacheLast) {
318✔
2221
    pVnode->config.cacheLast = req.cacheLast;
98✔
2222
  }
2223

2224
  if (pVnode->config.walCfg.fsyncPeriod != req.walFsyncPeriod) {
318✔
2225
    pVnode->config.walCfg.fsyncPeriod = req.walFsyncPeriod;
54✔
2226
    walChanged = true;
54✔
2227
  }
2228

2229
  if (pVnode->config.walCfg.level != req.walLevel) {
318✔
2230
    if (pVnode->config.walCfg.level == 0) {
36!
2231
      pVnode->config.walCfg.clearFiles = 1;
×
2232
    }
2233
    pVnode->config.walCfg.level = req.walLevel;
36✔
2234
    walChanged = true;
36✔
2235
  }
2236

2237
  if (pVnode->config.walCfg.retentionPeriod != req.walRetentionPeriod) {
318✔
2238
    pVnode->config.walCfg.retentionPeriod = req.walRetentionPeriod;
84✔
2239
    walChanged = true;
84✔
2240
  }
2241

2242
  if (pVnode->config.walCfg.retentionSize != req.walRetentionSize) {
318!
UNCOV
2243
    pVnode->config.walCfg.retentionSize = req.walRetentionSize;
×
UNCOV
2244
    walChanged = true;
×
2245
  }
2246

2247
  if (pVnode->config.tsdbCfg.keep0 != req.daysToKeep0) {
318✔
2248
    pVnode->config.tsdbCfg.keep0 = req.daysToKeep0;
42✔
2249
    if (!VND_IS_RSMA(pVnode)) {
42!
2250
      tsdbChanged = true;
42✔
2251
    }
2252
  }
2253

2254
  if (pVnode->config.tsdbCfg.keep1 != req.daysToKeep1) {
318✔
2255
    pVnode->config.tsdbCfg.keep1 = req.daysToKeep1;
42✔
2256
    if (!VND_IS_RSMA(pVnode)) {
42!
2257
      tsdbChanged = true;
42✔
2258
    }
2259
  }
2260

2261
  if (pVnode->config.tsdbCfg.keep2 != req.daysToKeep2) {
318✔
2262
    pVnode->config.tsdbCfg.keep2 = req.daysToKeep2;
44✔
2263
    if (!VND_IS_RSMA(pVnode)) {
44!
2264
      tsdbChanged = true;
44✔
2265
    }
2266
  }
2267

2268
  if (pVnode->config.tsdbCfg.keepTimeOffset != req.keepTimeOffset) {
318✔
2269
    pVnode->config.tsdbCfg.keepTimeOffset = req.keepTimeOffset;
2✔
2270
    if (!VND_IS_RSMA(pVnode)) {
2!
2271
      tsdbChanged = true;
2✔
2272
    }
2273
  }
2274

2275
  if (req.sttTrigger != -1 && req.sttTrigger != pVnode->config.sttTrigger) {
318!
UNCOV
2276
    if (req.sttTrigger > 1 && pVnode->config.sttTrigger > 1) {
×
UNCOV
2277
      pVnode->config.sttTrigger = req.sttTrigger;
×
2278
    } else {
UNCOV
2279
      vnodeAWait(&pVnode->commitTask);
×
2280

UNCOV
2281
      int32_t ret = tsdbDisableAndCancelAllBgTask(pVnode->pTsdb);
×
UNCOV
2282
      if (ret != 0) {
×
UNCOV
2283
        vError("vgId:%d, failed to disable bg task since %s", TD_VID(pVnode), tstrerror(errno));
×
2284
      }
2285

UNCOV
2286
      pVnode->config.sttTrigger = req.sttTrigger;
×
2287
      tsdbEnableBgTask(pVnode->pTsdb);
×
2288
    }
2289
  }
2290

2291
  if (req.minRows != -1 && req.minRows != pVnode->config.tsdbCfg.minRows) {
318!
2292
    pVnode->config.tsdbCfg.minRows = req.minRows;
×
2293
  }
2294

2295
  if (req.s3KeepLocal != -1 && req.s3KeepLocal != pVnode->config.s3KeepLocal) {
318!
UNCOV
2296
    pVnode->config.s3KeepLocal = req.s3KeepLocal;
×
2297
  }
2298
  if (req.s3Compact != -1 && req.s3Compact != pVnode->config.s3Compact) {
318!
UNCOV
2299
    pVnode->config.s3Compact = req.s3Compact;
×
2300
  }
2301

2302
  if (walChanged) {
318✔
2303
    if (walAlter(pVnode->pWal, &pVnode->config.walCfg) != 0) {
174!
UNCOV
2304
      vError("vgId:%d, failed to alter wal config since %s", TD_VID(pVnode), tstrerror(errno));
×
2305
    }
2306
  }
2307

2308
  if (tsdbChanged) {
320✔
2309
    tsdbSetKeepCfg(pVnode->pTsdb, &pVnode->config.tsdbCfg);
46✔
2310
  }
2311

2312
  return 0;
320✔
2313
}
2314

2315
static int32_t vnodeProcessBatchDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
2,856✔
2316
  SBatchDeleteReq deleteReq;
2317
  SDecoder        decoder;
2318
  tDecoderInit(&decoder, pReq, len);
2,856✔
2319
  if (tDecodeSBatchDeleteReq(&decoder, &deleteReq) < 0) {
2,858!
UNCOV
2320
    tDecoderClear(&decoder);
×
UNCOV
2321
    return terrno = TSDB_CODE_INVALID_MSG;
×
2322
  }
2323

2324
  SMetaReader mr = {0};
2,857✔
2325
  metaReaderDoInit(&mr, pVnode->pMeta, META_READER_NOLOCK);
2,857✔
2326
  STsdb *pTsdb = pVnode->pTsdb;
2,859✔
2327

2328
  if (deleteReq.level) {
2,859✔
2329
    pTsdb = deleteReq.level == 1 ? VND_RSMA1(pVnode) : VND_RSMA2(pVnode);
16✔
2330
  }
2331

2332
  int32_t sz = taosArrayGetSize(deleteReq.deleteReqs);
2,859✔
2333
  for (int32_t i = 0; i < sz; i++) {
6,519✔
2334
    SSingleDeleteReq *pOneReq = taosArrayGet(deleteReq.deleteReqs, i);
3,657✔
2335
    char             *name = pOneReq->tbname;
3,656✔
2336
    if (metaGetTableEntryByName(&mr, name) < 0) {
3,656✔
2337
      vDebug("vgId:%d, stream delete msg, skip since no table: %s", pVnode->config.vgId, name);
1,845✔
2338
      continue;
1,845✔
2339
    }
2340

2341
    int64_t uid = mr.me.uid;
1,816✔
2342

2343
    int32_t code = tsdbDeleteTableData(pTsdb, ver, deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
1,816✔
2344
    if (code < 0) {
1,817!
UNCOV
2345
      terrno = code;
×
UNCOV
2346
      vError("vgId:%d, delete error since %s, suid:%" PRId64 ", uid:%" PRId64 ", start ts:%" PRId64 ", end ts:%" PRId64,
×
2347
             TD_VID(pVnode), terrstr(), deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
2348
    }
2349

2350
    if (deleteReq.level == 0) {
1,817✔
2351
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, deleteReq.ctimeMs);
1,201✔
2352
      if (code < 0) {
1,201!
UNCOV
2353
        terrno = code;
×
UNCOV
2354
        vError("vgId:%d, update change time error since %s, suid:%" PRId64 ", uid:%" PRId64 ", start ts:%" PRId64
×
2355
               ", end ts:%" PRId64,
2356
               TD_VID(pVnode), terrstr(), deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
2357
      }
2358
    }
2359
    tDecoderClear(&mr.coder);
1,817✔
2360
  }
2361
  metaReaderClear(&mr);
2,862✔
2362
  taosArrayDestroy(deleteReq.deleteReqs);
2,857✔
2363
  return 0;
2,861✔
2364
}
2365

2366
static int32_t vnodeProcessDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
20,141✔
2367
                                     SRpcMsg *pOriginalMsg) {
2368
  int32_t     code = 0;
20,141✔
2369
  SDecoder   *pCoder = &(SDecoder){0};
20,141✔
2370
  SDeleteRes *pRes = &(SDeleteRes){0};
20,141✔
2371

2372
  pRsp->msgType = TDMT_VND_DELETE_RSP;
20,141✔
2373
  pRsp->pCont = NULL;
20,141✔
2374
  pRsp->contLen = 0;
20,141✔
2375
  pRsp->code = TSDB_CODE_SUCCESS;
20,141✔
2376

2377
  pRes->uidList = taosArrayInit(0, sizeof(tb_uid_t));
20,141✔
2378
  if (pRes->uidList == NULL) {
20,141!
UNCOV
2379
    code = terrno;
×
UNCOV
2380
    goto _err;
×
2381
  }
2382

2383
  tDecoderInit(pCoder, pReq, len);
20,141✔
2384
  code = tDecodeDeleteRes(pCoder, pRes);
20,141✔
2385
  if (code) goto _err;
20,142!
2386

2387
  if (pRes->affectedRows > 0) {
20,142✔
2388
    for (int32_t iUid = 0; iUid < taosArrayGetSize(pRes->uidList); iUid++) {
37,350✔
2389
      uint64_t uid = *(uint64_t *)taosArrayGet(pRes->uidList, iUid);
19,086✔
2390
      code = tsdbDeleteTableData(pVnode->pTsdb, ver, pRes->suid, uid, pRes->skey, pRes->ekey);
19,086✔
2391
      if (code) goto _err;
19,087!
2392
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, pRes->ctimeMs);
19,087✔
2393
      if (code) goto _err;
19,087!
2394
    }
2395
  }
2396

2397
  code = tdProcessRSmaDelete(pVnode->pSma, ver, pRes, pReq, len);
20,142✔
2398

2399
  tDecoderClear(pCoder);
20,141✔
2400
  taosArrayDestroy(pRes->uidList);
20,141✔
2401

2402
  SVDeleteRsp rsp = {.affectedRows = pRes->affectedRows};
20,142✔
2403
  int32_t     ret = 0;
20,142✔
2404
  tEncodeSize(tEncodeSVDeleteRsp, &rsp, pRsp->contLen, ret);
20,142!
2405
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
20,141✔
2406
  SEncoder ec = {0};
20,142✔
2407
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
20,142✔
2408
  code = tEncodeSVDeleteRsp(&ec, &rsp);
20,142✔
2409
  if (code) goto _err;
20,141!
2410
  tEncoderClear(&ec);
20,141✔
2411
  return code;
20,142✔
2412

UNCOV
2413
_err:
×
2414
  /*
2415
  if(code == TSDB_CODE_SUCCESS){
2416
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_DELETE, pVnode->monitor.strClusterId,
2417
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2418
                                        pOriginalMsg->info.conn.user, "Success"};
2419
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2420
  }
2421
  else{
2422
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_DELETE, pVnode->monitor.strClusterId,
2423
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2424
                                        pOriginalMsg->info.conn.user, "Failed"};
2425
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2426
  }
2427
  */
2428

UNCOV
2429
  return code;
×
2430
}
2431
static int32_t vnodeProcessCreateIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
19✔
2432
  SVCreateStbReq req = {0};
19✔
2433
  SDecoder       dc = {0};
19✔
2434
  int32_t        code = 0;
19✔
2435

2436
  pRsp->msgType = TDMT_VND_CREATE_INDEX_RSP;
19✔
2437
  pRsp->code = TSDB_CODE_SUCCESS;
19✔
2438
  pRsp->pCont = NULL;
19✔
2439
  pRsp->contLen = 0;
19✔
2440

2441
  tDecoderInit(&dc, pReq, len);
19✔
2442
  // decode req
2443
  if (tDecodeSVCreateStbReq(&dc, &req) < 0) {
19!
2444
    tDecoderClear(&dc);
×
2445
    return terrno = TSDB_CODE_INVALID_MSG;
×
2446
  }
2447

2448
  code = metaAddIndexToSuperTable(pVnode->pMeta, ver, &req);
19✔
2449
  if (code) {
19!
UNCOV
2450
    pRsp->code = code;
×
UNCOV
2451
    goto _err;
×
2452
  }
2453
  tDecoderClear(&dc);
19✔
2454
  return 0;
19✔
2455

UNCOV
2456
_err:
×
2457
  tDecoderClear(&dc);
×
2458
  return code;
×
2459
}
2460
static int32_t vnodeProcessDropIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
8✔
2461
  SDropIndexReq req = {0};
8✔
2462
  int32_t       code = 0;
8✔
2463
  pRsp->msgType = TDMT_VND_DROP_INDEX_RSP;
8✔
2464
  pRsp->code = TSDB_CODE_SUCCESS;
8✔
2465
  pRsp->pCont = NULL;
8✔
2466
  pRsp->contLen = 0;
8✔
2467

2468
  if ((code = tDeserializeSDropIdxReq(pReq, len, &req))) {
8!
UNCOV
2469
    pRsp->code = code;
×
UNCOV
2470
    return code;
×
2471
  }
2472

2473
  code = metaDropIndexFromSuperTable(pVnode->pMeta, ver, &req);
8✔
2474
  if (code) {
8!
UNCOV
2475
    pRsp->code = code;
×
UNCOV
2476
    return code;
×
2477
  }
2478
  return TSDB_CODE_SUCCESS;
8✔
2479
}
2480

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

2483
static int32_t vnodeProcessCompactVnodeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
193✔
2484
  if (!pVnode->restored) {
193✔
2485
    vInfo("vgId:%d, ignore compact req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
173!
2486
    return 0;
173✔
2487
  }
2488
  return vnodeAsyncCompact(pVnode, ver, pReq, len, pRsp);
20✔
2489
}
2490

UNCOV
2491
static int32_t vnodeProcessConfigChangeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
UNCOV
2492
  if (syncCheckMember(pVnode->sync) != 0) {
×
UNCOV
2493
    vError("vgId:%d, failed to check member", TD_VID(pVnode));
×
2494
  }
2495

UNCOV
2496
  pRsp->msgType = TDMT_SYNC_CONFIG_CHANGE_RSP;
×
UNCOV
2497
  pRsp->code = TSDB_CODE_SUCCESS;
×
UNCOV
2498
  pRsp->pCont = NULL;
×
UNCOV
2499
  pRsp->contLen = 0;
×
2500

UNCOV
2501
  return 0;
×
2502
}
2503

2504
static int32_t vnodePreCheckAssignedLogSyncd(SVnode *pVnode, char *member0Token, char *member1Token) {
2✔
2505
  SSyncState syncState = syncGetState(pVnode->sync);
2✔
2506
  if (syncState.state != TAOS_SYNC_STATE_LEADER) {
2!
UNCOV
2507
    return terrno = TSDB_CODE_SYN_NOT_LEADER;
×
2508
  }
2509

2510
  char token[TSDB_ARB_TOKEN_SIZE] = {0};
2✔
2511
  if (vnodeGetArbToken(pVnode, token) != 0) {
2!
2512
    return terrno = TSDB_CODE_NOT_FOUND;
×
2513
  }
2514

2515
  if (strncmp(token, member0Token, TSDB_ARB_TOKEN_SIZE) != 0 &&
2!
2516
      strncmp(token, member1Token, TSDB_ARB_TOKEN_SIZE) != 0) {
2!
2517
    return terrno = TSDB_CODE_MND_ARB_TOKEN_MISMATCH;
2✔
2518
  }
2519

UNCOV
2520
  terrno = TSDB_CODE_SUCCESS;
×
2521
  return 0;
×
2522
}
2523

2524
static int32_t vnodeCheckAssignedLogSyncd(SVnode *pVnode, char *member0Token, char *member1Token) {
×
UNCOV
2525
  int32_t code = vnodePreCheckAssignedLogSyncd(pVnode, member0Token, member1Token);
×
2526
  if (code != 0) {
×
2527
    return code;
×
2528
  }
2529

UNCOV
2530
  return syncGetAssignedLogSynced(pVnode->sync);
×
2531
}
2532

2533
static int32_t vnodeProcessArbCheckSyncReq(SVnode *pVnode, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
2534
  int32_t code = 0;
×
2535

2536
  SVArbCheckSyncReq syncReq = {0};
×
2537

2538
  code = tDeserializeSVArbCheckSyncReq(pReq, len, &syncReq);
×
2539
  if (code) {
×
2540
    return terrno = code;
×
2541
  }
2542

2543
  pRsp->msgType = TDMT_VND_ARB_CHECK_SYNC_RSP;
×
UNCOV
2544
  pRsp->code = TSDB_CODE_SUCCESS;
×
2545
  pRsp->pCont = NULL;
×
UNCOV
2546
  pRsp->contLen = 0;
×
2547

2548
  SVArbCheckSyncRsp syncRsp = {0};
×
2549
  syncRsp.arbToken = syncReq.arbToken;
×
2550
  syncRsp.member0Token = syncReq.member0Token;
×
UNCOV
2551
  syncRsp.member1Token = syncReq.member1Token;
×
UNCOV
2552
  syncRsp.vgId = TD_VID(pVnode);
×
2553

2554
  if (vnodeCheckAssignedLogSyncd(pVnode, syncReq.member0Token, syncReq.member1Token) != 0) {
×
2555
    vError("vgId:%d, failed to check assigned log syncd", TD_VID(pVnode));
×
2556
  }
2557
  syncRsp.errCode = terrno;
×
2558

2559
  if (vnodeUpdateArbTerm(pVnode, syncReq.arbTerm) != 0) {
×
2560
    vError("vgId:%d, failed to update arb term", TD_VID(pVnode));
×
2561
    code = -1;
×
2562
    goto _OVER;
×
2563
  }
2564

UNCOV
2565
  int32_t contLen = tSerializeSVArbCheckSyncRsp(NULL, 0, &syncRsp);
×
2566
  if (contLen <= 0) {
×
2567
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2568
    code = -1;
×
2569
    goto _OVER;
×
2570
  }
UNCOV
2571
  void *pHead = rpcMallocCont(contLen);
×
UNCOV
2572
  if (!pHead) {
×
2573
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2574
    code = -1;
×
UNCOV
2575
    goto _OVER;
×
2576
  }
2577

2578
  if (tSerializeSVArbCheckSyncRsp(pHead, contLen, &syncRsp) <= 0) {
×
2579
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2580
    rpcFreeCont(pHead);
×
UNCOV
2581
    code = -1;
×
UNCOV
2582
    goto _OVER;
×
2583
  }
2584

UNCOV
2585
  pRsp->pCont = pHead;
×
UNCOV
2586
  pRsp->contLen = contLen;
×
2587

UNCOV
2588
  terrno = TSDB_CODE_SUCCESS;
×
2589

UNCOV
2590
_OVER:
×
UNCOV
2591
  tFreeSVArbCheckSyncReq(&syncReq);
×
UNCOV
2592
  return code;
×
2593
}
2594

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

© 2026 Coveralls, Inc