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

taosdata / TDengine / #3583

17 Jan 2025 07:28AM UTC coverage: 63.876% (+0.07%) from 63.803%
#3583

push

travis-ci

web-flow
Merge pull request #29594 from taosdata/fix/insert-when-2-replicas

fix/insert-when-2-replicas

141608 of 284535 branches covered (49.77%)

Branch coverage included in aggregate %.

0 of 2 new or added lines in 1 file covered. (0.0%)

396 existing lines in 105 files now uncovered.

220075 of 281695 relevant lines covered (78.13%)

19864675.58 hits per line

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

61.68
/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) {
157,357✔
64
  int32_t code = 0;
157,357✔
65
  int32_t lino = 0;
157,357✔
66

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

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

78
  // name
79
  char *name = NULL;
157,368✔
80
  if (tDecodeCStr(pCoder, &name) < 0) {
157,354!
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);
157,354✔
87
  if (uid == 0) {
157,364✔
88
    uid = tGenIdPI64();
130,812✔
89
  }
90
  *(int64_t *)(pCoder->data + pCoder->pos) = uid;
157,362✔
91

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

95
  tEndDecode(pCoder);
157,362✔
96

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

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

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

120
  if (tDecodeI32v(&dc, &nReqs) < 0) {
69,644!
121
    code = TSDB_CODE_INVALID_MSG;
×
122
    TSDB_CHECK_CODE(code, lino, _exit);
×
123
  }
124
  for (int32_t iReq = 0; iReq < nReqs; iReq++) {
164,494✔
125
    code = vnodePreprocessCreateTableReq(pVnode, &dc, btime, NULL);
94,867✔
126
    TSDB_CHECK_CODE(code, lino, _exit);
94,850!
127
  }
128

129
  tEndDecode(&dc);
69,627✔
130

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

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

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

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

157
  code = 0;
950✔
158

159
_exit:
950✔
160
  tDecoderClear(&dc);
950✔
161
  if (code) {
950!
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,
950✔
165
           ctimeMs);
166
  }
167
  return code;
950✔
168
}
169

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

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

177
  SArray *tbUids = NULL;
100,512✔
178
  int64_t timestampMs = 0;
100,512✔
179

180
  SVDropTtlTableReq ttlReq = {0};
100,512✔
181
  if (tDeserializeSVDropTtlTableReq((char *)pContOld + sizeof(SMsgHead), reqLenOld, &ttlReq) != 0) {
100,512!
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));
100,792✔
188
    if (tbUids == NULL) {
100,839!
189
      code = terrno;
×
190
      TSDB_CHECK_CODE(code, lino, _exit);
×
191
    }
192

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

200
    ttlReq.nUids = taosArrayGetSize(tbUids);
100,547✔
201
    ttlReq.pTbUids = tbUids;
100,450✔
202
  }
203

204
  if (ttlReq.nUids == 0) {
100,450!
205
    code = TSDB_CODE_MSG_PREPROCESSED;
100,485✔
206
    TSDB_CHECK_CODE(code, lino, _exit);
100,485!
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:
100,540✔
233
  taosArrayDestroy(tbUids);
100,540✔
234

235
  if (code && code != TSDB_CODE_MSG_PREPROCESSED) {
100,718!
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);
100,718✔
240
  }
241

242
  return code;
100,525✔
243
}
244

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

250
  if (tStartDecode(pCoder) < 0) {
10,823,009!
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,827,760!
258
    code = TSDB_CODE_INVALID_MSG;
×
259
    TSDB_CHECK_CODE(code, lino, _exit);
×
260
  }
261
  version = (submitTbData.flags >> 8) & 0xff;
10,827,760✔
262
  submitTbData.flags = submitTbData.flags & 0xff;
10,827,760✔
263

264
  int64_t uid;
265
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
10,827,760✔
266
    code = vnodePreprocessCreateTableReq(pVnode, pCoder, btimeMs, &uid);
62,501✔
267
    TSDB_CHECK_CODE(code, lino, _exit);
62,491!
268
  }
269

270
  // submit data
271
  if (tDecodeI64(pCoder, &submitTbData.suid) < 0) {
10,824,874!
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,824,874✔
277
    *(int64_t *)(pCoder->data + pCoder->pos) = uid;
62,487✔
278
    pCoder->pos += sizeof(int64_t);
62,487✔
279
  } else {
280
    if (tDecodeI64(pCoder, &submitTbData.uid) < 0) {
10,759,537!
281
      code = TSDB_CODE_INVALID_MSG;
×
282
      TSDB_CHECK_CODE(code, lino, _exit);
×
283
    }
284
  }
285

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

291
  // scan and check
292
  TSKEY now = btimeMs;
10,820,212✔
293
  if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_MICRO) {
10,820,212✔
294
    now *= 1000;
22,717✔
295
  } else if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_NANO) {
10,797,495✔
296
    now *= 1000000;
3,709✔
297
  }
298

299
  int32_t keep = pVnode->config.tsdbCfg.keep2;
10,820,212✔
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,820,212✔
312
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
10,820,212✔
313
  if (submitTbData.flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
10,820,212✔
314
    uint64_t nColData;
315
    if (tDecodeU64v(pCoder, &nColData) < 0) {
136!
316
      code = TSDB_CODE_INVALID_MSG;
×
317
      goto _exit;
1✔
318
    }
319

320
    SColData colData = {0};
136✔
321
    code = tDecodeColData(version, pCoder, &colData);
136✔
322
    if (code) {
136!
323
      code = TSDB_CODE_INVALID_MSG;
×
324
      goto _exit;
×
325
    }
326

327
    if (colData.flag != HAS_VALUE) {
136!
328
      code = TSDB_CODE_INVALID_MSG;
×
329
      goto _exit;
×
330
    }
331

332
    for (int32_t iRow = 0; iRow < colData.nVal; iRow++) {
370✔
333
      if (((TSKEY *)colData.pData)[iRow] < minKey || ((TSKEY *)colData.pData)[iRow] > maxKey) {
235!
334
        code = TSDB_CODE_TDB_TIMESTAMP_OUT_OF_RANGE;
1✔
335
        goto _exit;
1✔
336
      }
337
    }
338

339
    for (uint64_t i = 1; i < nColData; i++) {
587✔
340
      code = tDecodeColData(version, pCoder, &colData);
452✔
341
      if (code) {
452!
342
        code = TSDB_CODE_INVALID_MSG;
×
343
        goto _exit;
×
344
      }
345
    }
346
  } else {
347
    uint64_t nRow;
348
    if (tDecodeU64v(pCoder, &nRow) < 0) {
10,823,073!
349
      code = TSDB_CODE_INVALID_MSG;
×
350
      goto _exit;
1,696✔
351
    }
352

353
    for (int32_t iRow = 0; iRow < nRow; ++iRow) {
773,566,007✔
354
      SRow *pRow = (SRow *)(pCoder->data + pCoder->pos);
762,744,630✔
355
      pCoder->pos += pRow->len;
762,744,630✔
356

357
      if (pRow->ts < minKey || pRow->ts > maxKey) {
762,744,630!
358
        code = TSDB_CODE_TDB_TIMESTAMP_OUT_OF_RANGE;
1,696✔
359
        goto _exit;
1,696✔
360
      }
361
    }
362
  }
363

364
  if (!tDecodeIsEnd(pCoder)) {
10,821,512!
365
    *(int64_t *)(pCoder->data + pCoder->pos) = ctimeMs;
10,823,161✔
366
    pCoder->pos += sizeof(int64_t);
10,823,161✔
367
  }
368

369
  tEndDecode(pCoder);
10,821,512✔
370

371
_exit:
10,823,188✔
372
  return code;
10,823,188✔
373
}
374
static int32_t vnodePreProcessSubmitMsg(SVnode *pVnode, SRpcMsg *pMsg) {
10,093,615✔
375
  int32_t code = 0;
10,093,615✔
376
  int32_t lino = 0;
10,093,615✔
377

378
  if (tsBypassFlag & TSDB_BYPASS_RA_RPC_RECV_SUBMIT) {
10,093,615!
379
    return TSDB_CODE_MSG_PREPROCESSED;
×
380
  }
381

382
  SDecoder *pCoder = &(SDecoder){0};
10,093,615✔
383

384
  if (taosHton64(((SSubmitReq2Msg *)pMsg->pCont)->version) != 1) {
10,093,615!
385
    code = TSDB_CODE_INVALID_MSG;
×
386
    TSDB_CHECK_CODE(code, lino, _exit);
×
387
  }
388

389
  tDecoderInit(pCoder, (uint8_t *)pMsg->pCont + sizeof(SSubmitReq2Msg), pMsg->contLen - sizeof(SSubmitReq2Msg));
10,093,630✔
390

391
  if (tStartDecode(pCoder) < 0) {
10,093,574!
392
    code = TSDB_CODE_INVALID_MSG;
×
393
    TSDB_CHECK_CODE(code, lino, _exit);
×
394
  }
395

396
  uint64_t nSubmitTbData;
397
  if (tDecodeU64v(pCoder, &nSubmitTbData) < 0) {
10,093,676!
398
    code = TSDB_CODE_INVALID_MSG;
×
399
    TSDB_CHECK_CODE(code, lino, _exit);
×
400
  }
401

402
  int64_t btimeMs = taosGetTimestampMs();
10,093,613✔
403
  int64_t ctimeMs = btimeMs;
10,093,613✔
404
  for (int32_t i = 0; i < nSubmitTbData; i++) {
20,915,042✔
405
    code = vnodePreProcessSubmitTbData(pVnode, pCoder, btimeMs, ctimeMs);
10,823,432✔
406
    TSDB_CHECK_CODE(code, lino, _exit);
10,823,126✔
407
  }
408

409
  tEndDecode(pCoder);
10,091,610✔
410

411
_exit:
10,093,641✔
412
  tDecoderClear(pCoder);
10,093,641✔
413
  if (code) {
10,093,614✔
414
    vError("vgId:%d, %s:%d failed to preprocess submit request since %s, msg type:%s", TD_VID(pVnode), __func__, lino,
1,696!
415
           tstrerror(code), TMSG_INFO(pMsg->msgType));
416
  }
417
  return code;
10,093,607✔
418
}
419

420
static int32_t vnodePreProcessDeleteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
59,013✔
421
  int32_t code = 0;
59,013✔
422

423
  int32_t    size;
424
  int32_t    ret;
425
  uint8_t   *pCont;
426
  SEncoder  *pCoder = &(SEncoder){0};
59,013✔
427
  SDeleteRes res = {0};
59,013✔
428

429
  SReadHandle handle = {.vnode = pVnode, .pMsgCb = &pVnode->msgCb, .skipRollup = 1};
59,013✔
430
  initStorageAPI(&handle.api);
59,013✔
431

432
  code = qWorkerProcessDeleteMsg(&handle, pVnode->pQuery, pMsg, &res);
59,014✔
433
  if (code) goto _exit;
59,014!
434

435
  res.ctimeMs = taosGetTimestampMs();
59,014✔
436
  // malloc and encode
437
  tEncodeSize(tEncodeDeleteRes, &res, size, ret);
59,014!
438
  pCont = rpcMallocCont(size + sizeof(SMsgHead));
59,011✔
439

440
  ((SMsgHead *)pCont)->contLen = size + sizeof(SMsgHead);
59,015✔
441
  ((SMsgHead *)pCont)->vgId = TD_VID(pVnode);
59,015✔
442

443
  tEncoderInit(pCoder, pCont + sizeof(SMsgHead), size);
59,015✔
444
  if (tEncodeDeleteRes(pCoder, &res) != 0) {
59,015!
445
    vError("vgId:%d %s failed to encode delete response", TD_VID(pVnode), __func__);
×
446
  }
447
  tEncoderClear(pCoder);
59,015✔
448

449
  rpcFreeCont(pMsg->pCont);
59,014✔
450
  pMsg->pCont = pCont;
59,015✔
451
  pMsg->contLen = size + sizeof(SMsgHead);
59,015✔
452

453
  taosArrayDestroy(res.uidList);
59,015✔
454

455
_exit:
59,015✔
456
  return code;
59,015✔
457
}
458

459
static int32_t vnodePreProcessBatchDeleteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
2,657✔
460
  int32_t code = 0;
2,657✔
461
  int32_t lino = 0;
2,657✔
462

463
  int64_t         ctimeMs = taosGetTimestampMs();
2,657✔
464
  SBatchDeleteReq pReq = {0};
2,657✔
465
  SDecoder       *pCoder = &(SDecoder){0};
2,657✔
466

467
  tDecoderInit(pCoder, (uint8_t *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead));
2,657✔
468

469
  if (tDecodeSBatchDeleteReqSetCtime(pCoder, &pReq, ctimeMs) < 0) {
2,656!
470
    code = TSDB_CODE_INVALID_MSG;
×
471
  }
472

473
  tDecoderClear(pCoder);
2,656✔
474
  taosArrayDestroy(pReq.deleteReqs);
2,656✔
475

476
  if (code) {
2,656!
477
    vError("vgId:%d %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
478
  } else {
479
    vTrace("vgId:%d %s done, ctimeMs generated:%" PRId64, TD_VID(pVnode), __func__, ctimeMs);
2,656✔
480
  }
481
  return code;
2,656✔
482
}
483

484
static int32_t vnodePreProcessArbCheckSyncMsg(SVnode *pVnode, SRpcMsg *pMsg) {
5✔
485
  SVArbCheckSyncReq syncReq = {0};
5✔
486

487
  if (tDeserializeSVArbCheckSyncReq((char *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead),
5!
488
                                    &syncReq) != 0) {
489
    return TSDB_CODE_INVALID_MSG;
×
490
  }
491

492
  int32_t ret = vnodePreCheckAssignedLogSyncd(pVnode, syncReq.member0Token, syncReq.member1Token);
5✔
493
  if (ret != 0) {
5✔
494
    vError("vgId:%d, failed to preprocess arb check sync request since %s", TD_VID(pVnode), tstrerror(ret));
4!
495
  }
496

497
  int32_t code = terrno;
5✔
498
  tFreeSVArbCheckSyncReq(&syncReq);
5✔
499

500
  return code;
5✔
501
}
502

503
int32_t vnodePreProcessDropTbMsg(SVnode *pVnode, SRpcMsg *pMsg) {
2,557✔
504
  int32_t          code = TSDB_CODE_SUCCESS;
2,557✔
505
  int32_t          lino = 0;
2,557✔
506
  int32_t          size = 0;
2,557✔
507
  SDecoder         dc = {0};
2,557✔
508
  SEncoder         ec = {0};
2,557✔
509
  SVDropTbBatchReq receivedBatchReqs = {0};
2,557✔
510
  SVDropTbBatchReq sentBatchReqs = {0};
2,557✔
511

512
  tDecoderInit(&dc, POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), pMsg->contLen - sizeof(SMsgHead));
2,557✔
513

514
  code = tDecodeSVDropTbBatchReq(&dc, &receivedBatchReqs);
2,557✔
515
  if (code < 0) {
2,557!
516
    terrno = code;
×
517
    TSDB_CHECK_CODE(code, lino, _exit);
×
518
  }
519
  sentBatchReqs.pArray = taosArrayInit(receivedBatchReqs.nReqs, sizeof(SVDropTbReq));
2,557✔
520
  if (!sentBatchReqs.pArray) {
2,557!
521
    code = terrno;
×
522
    goto _exit;
×
523
  }
524

525
  for (int32_t i = 0; i < receivedBatchReqs.nReqs; ++i) {
5,139✔
526
    SVDropTbReq *pReq = receivedBatchReqs.pReqs + i;
2,582✔
527
    tb_uid_t     uid = metaGetTableEntryUidByName(pVnode->pMeta, pReq->name);
2,582✔
528
    if (uid == 0) {
2,582!
529
      vWarn("vgId:%d, preprocess drop ctb: %s not found", TD_VID(pVnode), pReq->name);
×
530
      continue;
×
531
    }
532
    pReq->uid = uid;
2,582✔
533
    vDebug("vgId:%d %s for: %s, uid: %" PRId64, TD_VID(pVnode), __func__, pReq->name, pReq->uid);
2,582✔
534
    if (taosArrayPush(sentBatchReqs.pArray, pReq) == NULL) {
5,164!
535
      code = terrno;
×
536
      goto _exit;
×
537
    }
538
  }
539
  sentBatchReqs.nReqs = sentBatchReqs.pArray->size;
2,557✔
540

541
  tEncodeSize(tEncodeSVDropTbBatchReq, &sentBatchReqs, size, code);
2,557!
542
  tEncoderInit(&ec, POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), size);
2,557✔
543
  code = tEncodeSVDropTbBatchReq(&ec, &sentBatchReqs);
2,557✔
544
  tEncoderClear(&ec);
2,557✔
545
  if (code != TSDB_CODE_SUCCESS) {
2,557!
546
    vError("vgId:%d %s failed to encode drop tb batch req: %s", TD_VID(pVnode), __func__, tstrerror(code));
×
547
    TSDB_CHECK_CODE(code, lino, _exit);
×
548
  }
549

550
_exit:
2,557✔
551
  tDecoderClear(&dc);
2,557✔
552
  if (sentBatchReqs.pArray) {
2,557!
553
    taosArrayDestroy(sentBatchReqs.pArray);
2,557✔
554
  }
555
  return code;
2,557✔
556
}
557

558
int32_t vnodePreProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
10,436,052✔
559
  int32_t code = 0;
10,436,052✔
560

561
  switch (pMsg->msgType) {
10,436,052✔
562
    case TDMT_VND_CREATE_TABLE: {
69,644✔
563
      code = vnodePreProcessCreateTableMsg(pVnode, pMsg);
69,644✔
564
    } break;
69,641✔
565
    case TDMT_VND_ALTER_TABLE: {
950✔
566
      code = vnodePreProcessAlterTableMsg(pVnode, pMsg);
950✔
567
    } break;
950✔
568
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
100,778✔
569
    case TDMT_VND_DROP_TTL_TABLE: {
570
      code = vnodePreProcessDropTtlMsg(pVnode, pMsg);
100,778✔
571
    } break;
100,466✔
572
    case TDMT_VND_SUBMIT: {
10,093,678✔
573
      code = vnodePreProcessSubmitMsg(pVnode, pMsg);
10,093,678✔
574
    } break;
10,093,602✔
575
    case TDMT_VND_DELETE: {
59,014✔
576
      code = vnodePreProcessDeleteMsg(pVnode, pMsg);
59,014✔
577
    } break;
59,013✔
578
    case TDMT_VND_BATCH_DEL: {
2,657✔
579
      code = vnodePreProcessBatchDeleteMsg(pVnode, pMsg);
2,657✔
580
    } break;
2,656✔
581
    case TDMT_VND_ARB_CHECK_SYNC: {
5✔
582
      code = vnodePreProcessArbCheckSyncMsg(pVnode, pMsg);
5✔
583
    } break;
5✔
584
    case TDMT_VND_DROP_TABLE: {
2,557✔
585
      code = vnodePreProcessDropTbMsg(pVnode, pMsg);
2,557✔
586
    } break;
2,557✔
587
    default:
106,769✔
588
      break;
106,769✔
589
  }
590

591
  if (code && code != TSDB_CODE_MSG_PREPROCESSED) {
10,435,659✔
592
    vError("vgId:%d, failed to preprocess write request since %s, msg type:%s", TD_VID(pVnode), tstrerror(code),
1,703!
593
           TMSG_INFO(pMsg->msgType));
594
  }
595
  return code;
10,435,591✔
596
}
597

598
int32_t vnodeProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg, int64_t ver, SRpcMsg *pRsp) {
13,373,135✔
599
  int32_t code = 0;
13,373,135✔
600
  void   *ptr = NULL;
13,373,135✔
601
  void   *pReq;
602
  int32_t len;
603

604
  (void)taosThreadMutexLock(&pVnode->mutex);
13,373,135✔
605
  if (pVnode->disableWrite) {
13,373,524!
606
    (void)taosThreadMutexUnlock(&pVnode->mutex);
×
607
    vError("vgId:%d write is disabled for snapshot, version:%" PRId64, TD_VID(pVnode), ver);
×
608
    return TSDB_CODE_VND_WRITE_DISABLED;
×
609
  }
610
  (void)taosThreadMutexUnlock(&pVnode->mutex);
13,373,524✔
611

612
  if (ver <= pVnode->state.applied) {
13,373,473!
613
    vError("vgId:%d, duplicate write request. ver: %" PRId64 ", applied: %" PRId64 "", TD_VID(pVnode), ver,
×
614
           pVnode->state.applied);
615
    return terrno = TSDB_CODE_VND_DUP_REQUEST;
×
616
  }
617

618
  vDebug("vgId:%d, start to process write request %s, index:%" PRId64 ", applied:%" PRId64 ", state.applyTerm:%" PRId64
13,373,473!
619
         ", conn.applyTerm:%" PRId64 ", contLen:%d",
620
         TD_VID(pVnode), TMSG_INFO(pMsg->msgType), ver, pVnode->state.applied, pVnode->state.applyTerm,
621
         pMsg->info.conn.applyTerm, pMsg->contLen);
622

623
  if (!(pVnode->state.applyTerm <= pMsg->info.conn.applyTerm)) {
13,373,201!
624
    return terrno = TSDB_CODE_INTERNAL_ERROR;
×
625
  }
626

627
  if (!(pVnode->state.applied + 1 == ver)) {
13,373,201!
628
    return terrno = TSDB_CODE_INTERNAL_ERROR;
×
629
  }
630

631
  atomic_store_64(&pVnode->state.applied, ver);
13,373,201✔
632
  atomic_store_64(&pVnode->state.applyTerm, pMsg->info.conn.applyTerm);
13,373,525✔
633

634
  if (!syncUtilUserCommit(pMsg->msgType)) goto _exit;
13,373,492✔
635

636
  // skip header
637
  pReq = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
13,347,472✔
638
  len = pMsg->contLen - sizeof(SMsgHead);
13,347,472✔
639
  bool needCommit = false;
13,347,472✔
640

641
  switch (pMsg->msgType) {
13,347,472!
642
    /* META */
643
    case TDMT_VND_CREATE_STB:
28,887✔
644
      if (vnodeProcessCreateStbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
28,887✔
645
      break;
28,954✔
646
    case TDMT_VND_ALTER_STB:
6,411✔
647
      if (vnodeProcessAlterStbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
6,411!
648
      break;
6,422✔
649
    case TDMT_VND_DROP_STB:
2,335✔
650
      if (vnodeProcessDropStbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
2,335!
651
      break;
2,341✔
652
    case TDMT_VND_CREATE_TABLE:
78,356✔
653
      if (vnodeProcessCreateTbReq(pVnode, ver, pReq, len, pRsp, pMsg) < 0) goto _err;
78,356!
654
      break;
78,380✔
655
    case TDMT_VND_ALTER_TABLE:
957✔
656
      if (vnodeProcessAlterTbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
957!
657
      break;
957✔
658
    case TDMT_VND_DROP_TABLE:
2,599✔
659
      if (vnodeProcessDropTbReq(pVnode, ver, pReq, len, pRsp, pMsg) < 0) goto _err;
2,599!
660
      break;
2,599✔
661
    case TDMT_VND_DROP_TTL_TABLE:
×
662
      if (vnodeProcessDropTtlTbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
663
      break;
×
664
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
5✔
665
      if (vnodeProcessFetchTtlExpiredTbs(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
5!
666
      break;
5✔
667
    case TDMT_VND_TRIM:
232✔
668
      if (vnodeProcessTrimReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
232!
669
      break;
232✔
670
    case TDMT_VND_S3MIGRATE:
×
671
      if (vnodeProcessS3MigrateReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
672
      break;
×
673
    case TDMT_VND_CREATE_SMA:
31✔
674
      if (vnodeProcessCreateTSmaReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
31!
675
      break;
31✔
676
    /* TSDB */
677
    case TDMT_VND_SUBMIT:
13,072,734✔
678
      if (vnodeProcessSubmitReq(pVnode, ver, pMsg->pCont, pMsg->contLen, pRsp, pMsg) < 0) goto _err;
13,072,734✔
679
      break;
13,072,619✔
680
    case TDMT_VND_DELETE:
62,376✔
681
      if (vnodeProcessDeleteReq(pVnode, ver, pReq, len, pRsp, pMsg) < 0) goto _err;
62,376!
682
      break;
62,377✔
683
    case TDMT_VND_BATCH_DEL:
2,935✔
684
      if (vnodeProcessBatchDeleteReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
2,935!
685
      break;
2,934✔
686
    /* TQ */
687
    case TDMT_VND_TMQ_SUBSCRIBE:
3,166✔
688
      if (tqProcessSubscribeReq(pVnode->pTq, ver, pReq, len) < 0) {
3,166✔
689
        goto _err;
1✔
690
      }
691
      break;
3,166✔
692
    case TDMT_VND_TMQ_DELETE_SUB:
885✔
693
      if (tqProcessDeleteSubReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen) < 0) {
885!
694
        goto _err;
×
695
      }
696
      break;
885✔
697
    case TDMT_VND_TMQ_COMMIT_OFFSET:
6,796✔
698
      if (tqProcessOffsetCommitReq(pVnode->pTq, ver, pReq, len) < 0) {
6,796!
699
        goto _err;
×
700
      }
701
      break;
6,804✔
702
    case TDMT_VND_TMQ_ADD_CHECKINFO:
123✔
703
      if (tqProcessAddCheckInfoReq(pVnode->pTq, ver, pReq, len) < 0) {
123!
704
        goto _err;
×
705
      }
706
      break;
124✔
707
    case TDMT_VND_TMQ_DEL_CHECKINFO:
14✔
708
      if (tqProcessDelCheckInfoReq(pVnode->pTq, ver, pReq, len) < 0) {
14✔
709
        goto _err;
2✔
710
      }
711
      break;
12✔
712
    case TDMT_STREAM_TASK_DEPLOY: {
14,245✔
713
      if ((code = tqProcessTaskDeployReq(pVnode->pTq, ver, pReq, len)) != TSDB_CODE_SUCCESS) {
14,245!
714
        goto _err;
×
715
      }
716
    } break;
14,265✔
717
    case TDMT_STREAM_TASK_DROP: {
6,962✔
718
      if ((code = tqProcessTaskDropReq(pVnode->pTq, pMsg->pCont, pMsg->contLen)) < 0) {
6,962!
719
        goto _err;
×
720
      }
721
    } break;
6,986✔
722
    case TDMT_STREAM_TASK_UPDATE_CHKPT: {
5,412✔
723
      if ((code = tqProcessTaskUpdateCheckpointReq(pVnode->pTq, pMsg->pCont, pMsg->contLen)) < 0) {
5,412!
724
        goto _err;
×
725
      }
726
    } break;
5,422✔
727
    case TDMT_STREAM_CONSEN_CHKPT: {
187✔
728
      if (pVnode->restored && (code = tqProcessTaskConsenChkptIdReq(pVnode->pTq, pMsg)) < 0) {
187!
729
        goto _err;
×
730
      }
731

732
    } break;
187✔
733
    case TDMT_STREAM_TASK_PAUSE: {
2,067✔
734
      if (pVnode->restored && vnodeIsLeader(pVnode) &&
3,466!
735
          (code = tqProcessTaskPauseReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen)) < 0) {
1,395✔
736
        goto _err;
×
737
      }
738
    } break;
2,071✔
739
    case TDMT_STREAM_TASK_RESUME: {
3,694✔
740
      if (pVnode->restored && vnodeIsLeader(pVnode) &&
6,261!
741
          (code = tqProcessTaskResumeReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen)) < 0) {
2,564✔
742
        goto _err;
×
743
      }
744
    } break;
3,701✔
745
    case TDMT_VND_STREAM_TASK_RESET: {
×
746
      if (pVnode->restored && vnodeIsLeader(pVnode) && (code = tqProcessTaskResetReq(pVnode->pTq, pMsg)) < 0) {
×
747
        goto _err;
×
748
      }
749

750
    } break;
×
751
    case TDMT_VND_ALTER_CONFIRM:
8,093✔
752
      needCommit = pVnode->config.hashChange;
8,093✔
753
      if (vnodeProcessAlterConfirmReq(pVnode, ver, pReq, len, pRsp) < 0) {
8,093!
754
        goto _err;
×
755
      }
756
      break;
8,093✔
757
    case TDMT_VND_ALTER_CONFIG:
760✔
758
      vnodeProcessAlterConfigReq(pVnode, ver, pReq, len, pRsp);
760✔
759
      break;
762✔
760
    case TDMT_VND_COMMIT:
30,545✔
761
      needCommit = true;
30,545✔
762
      break;
30,545✔
763
    case TDMT_VND_CREATE_INDEX:
967✔
764
      vnodeProcessCreateIndexReq(pVnode, ver, pReq, len, pRsp);
967✔
765
      break;
967✔
766
    case TDMT_VND_DROP_INDEX:
2,134✔
767
      vnodeProcessDropIndexReq(pVnode, ver, pReq, len, pRsp);
2,134✔
768
      break;
2,134✔
769
    case TDMT_VND_STREAM_CHECK_POINT_SOURCE:  // always return true
3,258✔
770
      tqProcessTaskCheckPointSourceReq(pVnode->pTq, pMsg, pRsp);
3,258✔
771
      break;
3,261✔
772
    case TDMT_VND_STREAM_TASK_UPDATE:  // always return true
100✔
773
      tqProcessTaskUpdateReq(pVnode->pTq, pMsg);
100✔
774
      break;
100✔
775
    case TDMT_VND_COMPACT:
203✔
776
      vnodeProcessCompactVnodeReq(pVnode, ver, pReq, len, pRsp);
203✔
777
      goto _exit;
202✔
778
    case TDMT_SYNC_CONFIG_CHANGE:
×
779
      vnodeProcessConfigChangeReq(pVnode, ver, pReq, len, pRsp);
×
780
      break;
×
781
#ifdef TD_ENTERPRISE
782
    case TDMT_VND_KILL_COMPACT:
×
783
      vnodeProcessKillCompactReq(pVnode, ver, pReq, len, pRsp);
×
784
      break;
×
785
#endif
786
    /* ARB */
787
    case TDMT_VND_ARB_CHECK_SYNC:
3✔
788
      vnodeProcessArbCheckSyncReq(pVnode, pReq, len, pRsp);
3✔
789
      break;
3✔
790
    default:
×
791
      vError("vgId:%d, unprocessed msg, %d", TD_VID(pVnode), pMsg->msgType);
×
792
      return TSDB_CODE_INVALID_MSG;
×
793
  }
794

795
  vTrace("vgId:%d, process %s request, code:0x%x index:%" PRId64, TD_VID(pVnode), TMSG_INFO(pMsg->msgType), pRsp->code,
13,347,339!
796
         ver);
797

798
  walApplyVer(pVnode->pWal, ver);
13,347,339✔
799

800
  code = tqPushMsg(pVnode->pTq, pMsg->msgType);
13,347,319✔
801
  if (code) {
13,347,431✔
802
    vError("vgId:%d, failed to push msg to TQ since %s", TD_VID(pVnode), tstrerror(terrno));
524!
803
    return code;
524✔
804
  }
805

806
  // commit if need
807
  if (needCommit) {
13,346,907✔
808
    vInfo("vgId:%d, commit at version %" PRId64, TD_VID(pVnode), ver);
30,102!
809
    code = vnodeAsyncCommit(pVnode);
30,110✔
810
    if (code) {
30,116!
811
      vError("vgId:%d, failed to vnode async commit since %s.", TD_VID(pVnode), tstrerror(terrno));
×
812
      goto _err;
×
813
    }
814

815
    // start a new one
816
    code = vnodeBegin(pVnode);
30,116✔
817
    if (code) {
30,108✔
818
      vError("vgId:%d, failed to begin vnode since %s.", TD_VID(pVnode), tstrerror(terrno));
43!
819
      goto _err;
×
820
    }
821
  }
822

823
_exit:
13,346,870✔
824
  return 0;
13,372,662✔
825

826
_err:
21✔
827
  vError("vgId:%d, process %s request failed since %s, ver:%" PRId64, TD_VID(pVnode), TMSG_INFO(pMsg->msgType),
21!
828
         tstrerror(terrno), ver);
829
  return code;
21✔
830
}
831

832
int32_t vnodePreprocessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg) {
8,275,201✔
833
  if (TDMT_SCH_QUERY != pMsg->msgType && TDMT_SCH_MERGE_QUERY != pMsg->msgType) {
8,275,201✔
834
    return 0;
627,608✔
835
  }
836

837
  return qWorkerPreprocessQueryMsg(pVnode->pQuery, pMsg, TDMT_SCH_QUERY == pMsg->msgType);
7,647,593✔
838
}
839

840
int32_t vnodeProcessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
8,283,483✔
841
  vTrace("message in vnode query queue is processing");
8,283,483✔
842
  if (pMsg->msgType == TDMT_VND_TMQ_CONSUME && !syncIsReadyForRead(pVnode->sync)) {
8,283,483✔
843
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
1,181✔
844
    return 0;
1,181✔
845
  }
846

847
  if (pMsg->msgType == TDMT_VND_TMQ_CONSUME && !pVnode->restored) {
8,282,310!
848
    vnodeRedirectRpcMsg(pVnode, pMsg, TSDB_CODE_SYN_RESTORING);
×
849
    return 0;
×
850
  }
851

852
  SReadHandle handle = {.vnode = pVnode, .pMsgCb = &pVnode->msgCb, .pWorkerCb = pInfo->workerCb};
8,282,310✔
853
  initStorageAPI(&handle.api);
8,282,310✔
854
  int32_t code = TSDB_CODE_SUCCESS;
8,282,230✔
855
  bool    redirected = false;
8,282,230✔
856

857
  switch (pMsg->msgType) {
8,282,230!
858
    case TDMT_SCH_QUERY:
5,897,390✔
859
      if (!syncIsReadyForRead(pVnode->sync)) {
5,897,390✔
860
        pMsg->code = (terrno) ? terrno : TSDB_CODE_SYN_NOT_LEADER;
2,206!
861
        redirected = true;
2,206✔
862
      }
863
      code = qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
5,895,450✔
864
      if (redirected) {
5,896,334✔
865
        vnodeRedirectRpcMsg(pVnode, pMsg, pMsg->code);
1,809✔
866
        return 0;
2,206✔
867
      }
868
      return code;
5,894,525✔
869
    case TDMT_SCH_MERGE_QUERY:
1,758,831✔
870
      return qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
1,758,831✔
871
    case TDMT_SCH_QUERY_CONTINUE:
544,706✔
872
      return qWorkerProcessCQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
544,706✔
873
    case TDMT_VND_TMQ_CONSUME:
60,747✔
874
      return tqProcessPollReq(pVnode->pTq, pMsg);
60,747✔
875
    case TDMT_VND_TMQ_CONSUME_PUSH:
20,938✔
876
      return tqProcessPollPush(pVnode->pTq, pMsg);
20,938✔
877
    default:
×
878
      vError("unknown msg type:%d in query queue", pMsg->msgType);
×
879
      return TSDB_CODE_APP_ERROR;
×
880
  }
881
}
882

883
int32_t vnodeProcessFetchMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
21,110,153✔
884
  vTrace("vgId:%d, msg:%p in fetch queue is processing", pVnode->config.vgId, pMsg);
21,110,153✔
885
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
21,110,154✔
886
       pMsg->msgType == TDMT_VND_BATCH_META || pMsg->msgType == TDMT_VND_TABLE_NAME) &&
14,654,215✔
887
      !syncIsReadyForRead(pVnode->sync)) {
7,557,618✔
888
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
4,007✔
889
    return 0;
4,007✔
890
  }
891

892
  switch (pMsg->msgType) {
21,109,238!
893
    case TDMT_SCH_FETCH:
10,404,995✔
894
    case TDMT_SCH_MERGE_FETCH:
895
      return qWorkerProcessFetchMsg(pVnode, pVnode->pQuery, pMsg, 0);
10,404,995✔
896
    case TDMT_SCH_FETCH_RSP:
×
897
      return qWorkerProcessRspMsg(pVnode, pVnode->pQuery, pMsg, 0);
×
898
    // case TDMT_SCH_CANCEL_TASK:
899
    //   return qWorkerProcessCancelMsg(pVnode, pVnode->pQuery, pMsg, 0);
900
    case TDMT_SCH_DROP_TASK:
7,647,702✔
901
      return qWorkerProcessDropMsg(pVnode, pVnode->pQuery, pMsg, 0);
7,647,702✔
902
    case TDMT_SCH_TASK_NOTIFY:
41✔
903
      return qWorkerProcessNotifyMsg(pVnode, pVnode->pQuery, pMsg, 0);
41✔
904
    case TDMT_SCH_QUERY_HEARTBEAT:
1,960,706✔
905
      return qWorkerProcessHbMsg(pVnode, pVnode->pQuery, pMsg, 0);
1,960,706✔
906
    case TDMT_VND_TABLE_META:
2,069✔
907
    case TDMT_VND_TABLE_NAME:
908
      return vnodeGetTableMeta(pVnode, pMsg, true);
2,069✔
909
    case TDMT_VND_TABLE_CFG:
×
910
      return vnodeGetTableCfg(pVnode, pMsg, true);
×
911
    case TDMT_VND_BATCH_META:
1,093,638✔
912
      return vnodeGetBatchMeta(pVnode, pMsg);
1,093,638✔
913
#ifdef TD_ENTERPRISE
914
    case TDMT_VND_QUERY_COMPACT_PROGRESS:
48✔
915
      return vnodeQueryCompactProgress(pVnode, pMsg);
48✔
916
#endif
917
      //    case TDMT_VND_TMQ_CONSUME:
918
      //      return tqProcessPollReq(pVnode->pTq, pMsg);
919
    case TDMT_VND_TMQ_VG_WALINFO:
13✔
920
      return tqProcessVgWalInfoReq(pVnode->pTq, pMsg);
13✔
921
    case TDMT_VND_TMQ_VG_COMMITTEDINFO:
2✔
922
      return tqProcessVgCommittedInfoReq(pVnode->pTq, pMsg);
2✔
923
    case TDMT_VND_TMQ_SEEK:
24✔
924
      return tqProcessSeekReq(pVnode->pTq, pMsg);
24✔
925

926
    default:
×
927
      vError("unknown msg type:%d in fetch queue", pMsg->msgType);
×
928
      return TSDB_CODE_APP_ERROR;
×
929
  }
930
}
931

932
int32_t vnodeProcessStreamMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
367,145✔
933
  vTrace("vgId:%d, msg:%p in fetch queue is processing", pVnode->config.vgId, pMsg);
367,145✔
934
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
367,145!
935
       pMsg->msgType == TDMT_VND_BATCH_META) &&
367,162✔
936
      !syncIsReadyForRead(pVnode->sync)) {
×
937
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
×
938
    return 0;
×
939
  }
940

941
  switch (pMsg->msgType) {
367,156!
942
    case TDMT_STREAM_TASK_RUN:
169,785✔
943
      return tqProcessTaskRunReq(pVnode->pTq, pMsg);
169,785✔
944
    case TDMT_STREAM_TASK_DISPATCH:
49,282✔
945
      return tqProcessTaskDispatchReq(pVnode->pTq, pMsg);
49,282✔
946
    case TDMT_STREAM_TASK_DISPATCH_RSP:
49,266✔
947
      return tqProcessTaskDispatchRsp(pVnode->pTq, pMsg);
49,266✔
948
    case TDMT_VND_STREAM_TASK_CHECK:
21,743✔
949
      return tqProcessTaskCheckReq(pVnode->pTq, pMsg);
21,743✔
950
    case TDMT_VND_STREAM_TASK_CHECK_RSP:
21,994✔
951
      return tqProcessTaskCheckRsp(pVnode->pTq, pMsg);
21,994✔
952
    case TDMT_STREAM_RETRIEVE:
565✔
953
      return tqProcessTaskRetrieveReq(pVnode->pTq, pMsg);
565✔
954
    case TDMT_STREAM_RETRIEVE_RSP:
442✔
955
      return tqProcessTaskRetrieveRsp(pVnode->pTq, pMsg);
442✔
956
    case TDMT_VND_STREAM_SCAN_HISTORY:
2,641✔
957
      return tqProcessTaskScanHistory(pVnode->pTq, pMsg);
2,641✔
958
    case TDMT_STREAM_TASK_CHECKPOINT_READY:
7,716✔
959
      return tqProcessTaskCheckpointReadyMsg(pVnode->pTq, pMsg);
7,716✔
960
    case TDMT_STREAM_TASK_CHECKPOINT_READY_RSP:
7,713✔
961
      return tqProcessTaskCheckpointReadyRsp(pVnode->pTq, pMsg);
7,713✔
962
    case TDMT_STREAM_RETRIEVE_TRIGGER:
×
963
      return tqProcessTaskRetrieveTriggerReq(pVnode->pTq, pMsg);
×
964
    case TDMT_STREAM_RETRIEVE_TRIGGER_RSP:
×
965
      return tqProcessTaskRetrieveTriggerRsp(pVnode->pTq, pMsg);
×
966
    case TDMT_MND_STREAM_HEARTBEAT_RSP:
25,562✔
967
      return tqProcessStreamHbRsp(pVnode->pTq, pMsg);
25,562✔
968
    case TDMT_MND_STREAM_REQ_CHKPT_RSP:
4,432✔
969
      return tqProcessStreamReqCheckpointRsp(pVnode->pTq, pMsg);
4,432✔
970
    case TDMT_VND_GET_STREAM_PROGRESS:
×
971
      return tqStreamProgressRetrieveReq(pVnode->pTq, pMsg);
×
972
    case TDMT_MND_STREAM_CHKPT_REPORT_RSP:
6,015✔
973
      return tqProcessTaskChkptReportRsp(pVnode->pTq, pMsg);
6,015✔
974
    default:
×
975
      vError("unknown msg type:%d in stream queue", pMsg->msgType);
×
976
      return TSDB_CODE_APP_ERROR;
×
977
  }
978
}
979

980
void smaHandleRes(void *pVnode, int64_t smaId, const SArray *data) {
1✔
981
  int32_t code = tdProcessTSmaInsert(((SVnode *)pVnode)->pSma, smaId, (const char *)data);
1✔
982
  if (code) {
1!
983
    vError("failed to process sma result since %s", tstrerror(code));
×
984
  }
985
}
1✔
986

987
void vnodeUpdateMetaRsp(SVnode *pVnode, STableMetaRsp *pMetaRsp) {
155,619✔
988
  if (NULL == pMetaRsp) {
155,619!
989
    return;
×
990
  }
991

992
  tstrncpy(pMetaRsp->dbFName, pVnode->config.dbname, TSDB_DB_FNAME_LEN);
155,619✔
993
  pMetaRsp->dbId = pVnode->config.dbId;
155,619✔
994
  pMetaRsp->vgId = TD_VID(pVnode);
155,619✔
995
  pMetaRsp->precision = pVnode->config.tsdbCfg.precision;
155,619✔
996
}
997

998
extern int32_t vnodeAsyncRetention(SVnode *pVnode, int64_t now);
999

1000
static int32_t vnodeProcessTrimReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
232✔
1001
  if (!pVnode->restored) {
232✔
1002
    vInfo("vgId:%d, ignore trim req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
7!
1003
    return 0;
7✔
1004
  }
1005

1006
  int32_t     code = 0;
225✔
1007
  SVTrimDbReq trimReq = {0};
225✔
1008

1009
  // decode
1010
  if (tDeserializeSVTrimDbReq(pReq, len, &trimReq) != 0) {
225!
1011
    code = TSDB_CODE_INVALID_MSG;
×
1012
    goto _exit;
×
1013
  }
1014

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

1017
  code = vnodeAsyncRetention(pVnode, trimReq.timestamp);
225✔
1018

1019
_exit:
225✔
1020
  return code;
225✔
1021
}
1022

1023
extern int32_t vnodeAsyncS3Migrate(SVnode *pVnode, int64_t now);
1024

1025
static int32_t vnodeProcessS3MigrateReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1026
  int32_t          code = 0;
×
1027
  SVS3MigrateDbReq s3migrateReq = {0};
×
1028

1029
  // decode
1030
  if (tDeserializeSVS3MigrateDbReq(pReq, len, &s3migrateReq) != 0) {
×
1031
    code = TSDB_CODE_INVALID_MSG;
×
1032
    goto _exit;
×
1033
  }
1034

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

1037
  code = vnodeAsyncS3Migrate(pVnode, s3migrateReq.timestamp);
×
1038

1039
_exit:
×
1040
  return code;
×
1041
}
1042

1043
static int32_t vnodeProcessDropTtlTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1044
  int               ret = 0;
×
1045
  SVDropTtlTableReq ttlReq = {0};
×
1046
  if (tDeserializeSVDropTtlTableReq(pReq, len, &ttlReq) != 0) {
×
1047
    ret = TSDB_CODE_INVALID_MSG;
×
1048
    goto end;
×
1049
  }
1050

1051
  if (ttlReq.nUids != taosArrayGetSize(ttlReq.pTbUids)) {
×
1052
    ret = TSDB_CODE_INVALID_MSG;
×
1053
    goto end;
×
1054
  }
1055

1056
  if (ttlReq.nUids != 0) {
×
1057
    vInfo("vgId:%d, drop ttl table req will be processed, time:%d, ntbUids:%d", pVnode->config.vgId,
×
1058
          ttlReq.timestampSec, ttlReq.nUids);
1059
  }
1060

1061
  if (ttlReq.nUids > 0) {
×
1062
    int32_t code = metaDropMultipleTables(pVnode->pMeta, ver, ttlReq.pTbUids);
×
1063
    if (code) return code;
×
1064

1065
    code = tqUpdateTbUidList(pVnode->pTq, ttlReq.pTbUids, false);
×
1066
    if (code) {
×
1067
      vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1068
    }
1069
  }
1070

1071
end:
×
1072
  taosArrayDestroy(ttlReq.pTbUids);
×
1073
  return ret;
×
1074
}
1075

1076
static int32_t vnodeProcessFetchTtlExpiredTbs(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
5✔
1077
  int32_t                 code = -1;
5✔
1078
  SMetaReader             mr = {0};
5✔
1079
  SVDropTtlTableReq       ttlReq = {0};
5✔
1080
  SVFetchTtlExpiredTbsRsp rsp = {0};
5✔
1081
  SEncoder                encoder = {0};
5✔
1082
  SArray                 *pNames = NULL;
5✔
1083
  pRsp->msgType = TDMT_VND_FETCH_TTL_EXPIRED_TBS_RSP;
5✔
1084
  pRsp->code = TSDB_CODE_SUCCESS;
5✔
1085
  pRsp->pCont = NULL;
5✔
1086
  pRsp->contLen = 0;
5✔
1087

1088
  if (tDeserializeSVDropTtlTableReq(pReq, len, &ttlReq) != 0) {
5!
1089
    terrno = TSDB_CODE_INVALID_MSG;
×
1090
    goto _end;
×
1091
  }
1092

1093
  if (!(ttlReq.nUids == taosArrayGetSize(ttlReq.pTbUids))) {
5!
1094
    terrno = TSDB_CODE_INVALID_MSG;
×
1095
    goto _end;
×
1096
  }
1097

1098
  tb_uid_t    suid;
1099
  char        ctbName[TSDB_TABLE_NAME_LEN];
1100
  SVDropTbReq expiredTb = {.igNotExists = true};
5✔
1101
  metaReaderDoInit(&mr, pVnode->pMeta, 0);
5✔
1102
  rsp.vgId = TD_VID(pVnode);
5✔
1103
  rsp.pExpiredTbs = taosArrayInit(ttlReq.nUids, sizeof(SVDropTbReq));
5✔
1104
  if (!rsp.pExpiredTbs) goto _end;
5!
1105

1106
  pNames = taosArrayInit(ttlReq.nUids, TSDB_TABLE_NAME_LEN);
5✔
1107
  if (!pNames) {
5!
1108
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1109
    goto _end;
×
1110
  }
1111
  char buf[TSDB_TABLE_NAME_LEN];
1112
  for (int32_t i = 0; i < ttlReq.nUids; ++i) {
10✔
1113
    tb_uid_t *uid = taosArrayGet(ttlReq.pTbUids, i);
5✔
1114
    expiredTb.suid = *uid;
5✔
1115
    terrno = metaReaderGetTableEntryByUid(&mr, *uid);
5✔
1116
    if (terrno < 0) goto _end;
5!
1117
    tstrncpy(buf, mr.me.name, TSDB_TABLE_NAME_LEN);
5✔
1118
    void *p = taosArrayPush(pNames, buf);
5✔
1119
    if (p == NULL) {
5!
1120
      goto _end;
×
1121
    }
1122

1123
    expiredTb.name = p;
5✔
1124
    if (mr.me.type == TSDB_CHILD_TABLE) {
5!
1125
      expiredTb.suid = mr.me.ctbEntry.suid;
5✔
1126
    }
1127

1128
    if (taosArrayPush(rsp.pExpiredTbs, &expiredTb) == NULL) {
10!
1129
      goto _end;
×
1130
    }
1131
  }
1132

1133
  int32_t ret = 0;
5✔
1134
  tEncodeSize(tEncodeVFetchTtlExpiredTbsRsp, &rsp, pRsp->contLen, ret);
5!
1135
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
5✔
1136
  if (pRsp->pCont == NULL) {
5!
1137
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1138
    code = -1;
×
1139
    goto _end;
×
1140
  }
1141
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
5✔
1142
  terrno = tEncodeVFetchTtlExpiredTbsRsp(&encoder, &rsp);
5✔
1143
  tEncoderClear(&encoder);
5✔
1144

1145
  if (terrno == 0) code = 0;
5!
1146
_end:
×
1147
  metaReaderClear(&mr);
5✔
1148
  tFreeFetchTtlExpiredTbsRsp(&rsp);
5✔
1149
  taosArrayDestroy(ttlReq.pTbUids);
5✔
1150
  if (pNames) taosArrayDestroy(pNames);
5!
1151
  pRsp->code = terrno;
5✔
1152
  return code;
5✔
1153
}
1154

1155
static int32_t vnodeProcessCreateStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
28,876✔
1156
  int32_t        code = 0;
28,876✔
1157
  SVCreateStbReq req = {0};
28,876✔
1158
  SDecoder       coder;
1159

1160
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
28,876✔
1161
  pRsp->code = TSDB_CODE_SUCCESS;
28,876✔
1162
  pRsp->pCont = NULL;
28,876✔
1163
  pRsp->contLen = 0;
28,876✔
1164

1165
  // decode and process req
1166
  tDecoderInit(&coder, pReq, len);
28,876✔
1167

1168
  code = tDecodeSVCreateStbReq(&coder, &req);
28,865✔
1169
  if (code) {
28,908!
1170
    pRsp->code = code;
×
1171
    goto _err;
×
1172
  }
1173

1174
  code = metaCreateSuperTable(pVnode->pMeta, ver, &req);
28,908✔
1175
  if (code) {
28,979✔
1176
    pRsp->code = code;
3✔
1177
    goto _err;
3✔
1178
  }
1179

1180
  if ((code = tdProcessRSmaCreate(pVnode->pSma, &req)) < 0) {
28,976!
1181
    pRsp->code = code;
×
1182
    goto _err;
×
1183
  }
1184

1185
  tDecoderClear(&coder);
28,964✔
1186
  return 0;
28,952✔
1187

1188
_err:
3✔
1189
  tDecoderClear(&coder);
3✔
1190
  return code;
3✔
1191
}
1192

1193
static int32_t vnodeProcessCreateTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
78,357✔
1194
                                       SRpcMsg *pOriginRpc) {
1195
  SDecoder           decoder = {0};
78,357✔
1196
  SEncoder           encoder = {0};
78,357✔
1197
  int32_t            rcode = 0;
78,357✔
1198
  SVCreateTbBatchReq req = {0};
78,357✔
1199
  SVCreateTbReq     *pCreateReq;
1200
  SVCreateTbBatchRsp rsp = {0};
78,357✔
1201
  SVCreateTbRsp      cRsp = {0};
78,357✔
1202
  char               tbName[TSDB_TABLE_FNAME_LEN];
1203
  STbUidStore       *pStore = NULL;
78,357✔
1204
  SArray            *tbUids = NULL;
78,357✔
1205
  SArray            *tbNames = NULL;
78,357✔
1206
  pRsp->msgType = TDMT_VND_CREATE_TABLE_RSP;
78,357✔
1207
  pRsp->code = TSDB_CODE_SUCCESS;
78,357✔
1208
  pRsp->pCont = NULL;
78,357✔
1209
  pRsp->contLen = 0;
78,357✔
1210

1211
  // decode
1212
  tDecoderInit(&decoder, pReq, len);
78,357✔
1213
  if (tDecodeSVCreateTbBatchReq(&decoder, &req) < 0) {
78,352!
1214
    rcode = -1;
×
1215
    terrno = TSDB_CODE_INVALID_MSG;
×
1216
    goto _exit;
×
1217
  }
1218

1219
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(cRsp));
78,372✔
1220
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
78,381✔
1221
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
78,379✔
1222
  if (rsp.pArray == NULL || tbUids == NULL || tbNames == NULL) {
78,380!
UNCOV
1223
    rcode = -1;
×
UNCOV
1224
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1225
    goto _exit;
×
1226
  }
1227

1228
  // loop to create table
1229
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
197,843✔
1230
    pCreateReq = req.pReqs + iReq;
119,459✔
1231
    memset(&cRsp, 0, sizeof(cRsp));
119,459✔
1232

1233
    if (tsEnableAudit && tsEnableAuditCreateTable) {
119,459!
1234
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
119,456!
1235
      if (str == NULL) {
119,451!
1236
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1237
        rcode = -1;
×
1238
        goto _exit;
×
1239
      }
1240
      tstrncpy(str, pCreateReq->name, TSDB_TABLE_FNAME_LEN);
119,451✔
1241
      if (taosArrayPush(tbNames, &str) == NULL) {
119,453!
1242
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1243
        rcode = -1;
×
1244
        goto _exit;
×
1245
      }
1246
    }
1247

1248
    // validate hash
1249
    (void)tsnprintf(tbName, TSDB_TABLE_FNAME_LEN, "%s.%s", pVnode->config.dbname, pCreateReq->name);
119,456✔
1250
    if (vnodeValidateTableHash(pVnode, tbName) < 0) {
119,457✔
1251
      cRsp.code = TSDB_CODE_VND_HASH_MISMATCH;
27✔
1252
      if (taosArrayPush(rsp.pArray, &cRsp) == NULL) {
54!
1253
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1254
        rcode = -1;
×
1255
        goto _exit;
×
1256
      }
1257
      vError("vgId:%d create-table:%s failed due to hash value mismatch", TD_VID(pVnode), tbName);
27!
1258
      continue;
27✔
1259
    }
1260

1261
    // do create table
1262
    if (metaCreateTable2(pVnode->pMeta, ver, pCreateReq, &cRsp.pMeta) < 0) {
119,423✔
1263
      if (pCreateReq->flags & TD_CREATE_IF_NOT_EXISTS && terrno == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
1,415!
1264
        cRsp.code = TSDB_CODE_SUCCESS;
4✔
1265
      } else {
1266
        cRsp.code = terrno;
1,411✔
1267
      }
1268
    } else {
1269
      cRsp.code = TSDB_CODE_SUCCESS;
118,020✔
1270
      if (tdFetchTbUidList(pVnode->pSma, &pStore, pCreateReq->ctb.suid, pCreateReq->uid) < 0) {
118,020!
1271
        vError("vgId:%d, failed to fetch tbUid list", TD_VID(pVnode));
×
1272
      }
1273
      if (taosArrayPush(tbUids, &pCreateReq->uid) == NULL) {
236,043!
1274
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1275
        rcode = -1;
×
1276
        goto _exit;
×
1277
      }
1278
      vnodeUpdateMetaRsp(pVnode, cRsp.pMeta);
118,022✔
1279
    }
1280

1281
    if (taosArrayPush(rsp.pArray, &cRsp) == NULL) {
238,873!
1282
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1283
      rcode = -1;
×
1284
      goto _exit;
×
1285
    }
1286
  }
1287

1288
  vDebug("vgId:%d, add %d new created tables into query table list", TD_VID(pVnode), (int32_t)taosArrayGetSize(tbUids));
78,384✔
1289
  if (tqUpdateTbUidList(pVnode->pTq, tbUids, true) < 0) {
78,384!
1290
    vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(terrno));
×
1291
  }
1292
  if (tdUpdateTbUidList(pVnode->pSma, pStore, true) < 0) {
78,382!
1293
    goto _exit;
×
1294
  }
1295
  pStore = tdUidStoreFree(pStore);
78,382✔
1296

1297
  // prepare rsp
1298
  int32_t ret = 0;
78,381✔
1299
  tEncodeSize(tEncodeSVCreateTbBatchRsp, &rsp, pRsp->contLen, ret);
78,381!
1300
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
78,379✔
1301
  if (pRsp->pCont == NULL) {
78,382!
1302
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1303
    rcode = -1;
×
1304
    goto _exit;
×
1305
  }
1306
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
78,382✔
1307
  if (tEncodeSVCreateTbBatchRsp(&encoder, &rsp) != 0) {
78,381!
1308
    vError("vgId:%d, failed to encode create table batch response", TD_VID(pVnode));
×
1309
  }
1310

1311
  if (tsEnableAudit && tsEnableAuditCreateTable) {
78,378!
1312
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
78,377✔
1313

1314
    SName name = {0};
78,377✔
1315
    if (tNameFromString(&name, pVnode->config.dbname, T_NAME_ACCT | T_NAME_DB) < 0) {
78,377!
1316
      vError("vgId:%d, failed to get name from string", TD_VID(pVnode));
×
1317
    }
1318

1319
    SStringBuilder sb = {0};
78,376✔
1320
    for (int32_t i = 0; i < tbNames->size; i++) {
197,833✔
1321
      char **key = (char **)taosArrayGet(tbNames, i);
119,454✔
1322
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
119,453✔
1323
      if (i < tbNames->size - 1) {
119,458✔
1324
        taosStringBuilderAppendChar(&sb, ',');
41,080✔
1325
      }
1326
      // taosMemoryFreeClear(*key);
1327
    }
1328

1329
    size_t len = 0;
78,379✔
1330
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
78,379✔
1331

1332
    if (pOriginRpc->info.conn.user != NULL && strlen(pOriginRpc->info.conn.user) > 0) {
78,378!
1333
      auditAddRecord(pOriginRpc, clusterId, "createTable", name.dbname, "", keyJoined, len);
62,826✔
1334
    }
1335

1336
    taosStringBuilderDestroy(&sb);
78,378✔
1337
  }
1338

1339
_exit:
1✔
1340
  tDeleteSVCreateTbBatchReq(&req);
78,377✔
1341
  taosArrayDestroyEx(rsp.pArray, tFreeSVCreateTbRsp);
78,383✔
1342
  taosArrayDestroy(tbUids);
78,381✔
1343
  tDecoderClear(&decoder);
78,380✔
1344
  tEncoderClear(&encoder);
78,382✔
1345
  taosArrayDestroyP(tbNames, NULL);
78,381✔
1346
  return rcode;
78,382✔
1347
}
1348

1349
static int32_t vnodeProcessAlterStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
6,411✔
1350
  int32_t        code = 0;
6,411✔
1351
  SVCreateStbReq req = {0};
6,411✔
1352
  SDecoder       dc = {0};
6,411✔
1353

1354
  pRsp->msgType = TDMT_VND_ALTER_STB_RSP;
6,411✔
1355
  pRsp->code = TSDB_CODE_SUCCESS;
6,411✔
1356
  pRsp->pCont = NULL;
6,411✔
1357
  pRsp->contLen = 0;
6,411✔
1358

1359
  tDecoderInit(&dc, pReq, len);
6,411✔
1360

1361
  // decode req
1362
  code = tDecodeSVCreateStbReq(&dc, &req);
6,411✔
1363
  if (code) {
6,416!
1364
    tDecoderClear(&dc);
×
1365
    return code;
×
1366
  }
1367

1368
  code = metaAlterSuperTable(pVnode->pMeta, ver, &req);
6,416✔
1369
  if (code) {
6,422!
1370
    pRsp->code = code;
×
1371
    tDecoderClear(&dc);
×
1372
    return code;
×
1373
  }
1374

1375
  tDecoderClear(&dc);
6,422✔
1376

1377
  return 0;
6,422✔
1378
}
1379

1380
static int32_t vnodeProcessDropStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
2,335✔
1381
  SVDropStbReq req = {0};
2,335✔
1382
  int32_t      rcode = TSDB_CODE_SUCCESS;
2,335✔
1383
  SDecoder     decoder = {0};
2,335✔
1384
  SArray      *tbUidList = NULL;
2,335✔
1385

1386
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
2,335✔
1387
  pRsp->pCont = NULL;
2,335✔
1388
  pRsp->contLen = 0;
2,335✔
1389

1390
  // decode request
1391
  tDecoderInit(&decoder, pReq, len);
2,335✔
1392
  if (tDecodeSVDropStbReq(&decoder, &req) < 0) {
2,336!
1393
    rcode = TSDB_CODE_INVALID_MSG;
×
1394
    goto _exit;
×
1395
  }
1396

1397
  // process request
1398
  tbUidList = taosArrayInit(8, sizeof(int64_t));
2,339✔
1399
  if (tbUidList == NULL) goto _exit;
2,339!
1400
  if (metaDropSuperTable(pVnode->pMeta, ver, &req) < 0) {
2,339✔
1401
    rcode = terrno;
2✔
1402
    goto _exit;
2✔
1403
  }
1404

1405
  if (tqUpdateTbUidList(pVnode->pTq, tbUidList, false) < 0) {
2,339!
1406
    rcode = terrno;
×
1407
    goto _exit;
×
1408
  }
1409

1410
  if (tdProcessRSmaDrop(pVnode->pSma, &req) < 0) {
2,339!
1411
    rcode = terrno;
×
1412
    goto _exit;
×
1413
  }
1414

1415
  // return rsp
1416
_exit:
2,339✔
1417
  if (tbUidList) taosArrayDestroy(tbUidList);
2,341!
1418
  pRsp->code = rcode;
2,341✔
1419
  tDecoderClear(&decoder);
2,341✔
1420
  return 0;
2,341✔
1421
}
1422

1423
static int32_t vnodeProcessAlterTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
957✔
1424
  SVAlterTbReq  vAlterTbReq = {0};
957✔
1425
  SVAlterTbRsp  vAlterTbRsp = {0};
957✔
1426
  SDecoder      dc = {0};
957✔
1427
  int32_t       code = 0;
957✔
1428
  int32_t       lino = 0;
957✔
1429
  int32_t       ret;
1430
  SEncoder      ec = {0};
957✔
1431
  STableMetaRsp vMetaRsp = {0};
957✔
1432

1433
  pRsp->msgType = TDMT_VND_ALTER_TABLE_RSP;
957✔
1434
  pRsp->pCont = NULL;
957✔
1435
  pRsp->contLen = 0;
957✔
1436
  pRsp->code = TSDB_CODE_SUCCESS;
957✔
1437

1438
  tDecoderInit(&dc, pReq, len);
957✔
1439

1440
  // decode
1441
  if (tDecodeSVAlterTbReq(&dc, &vAlterTbReq) < 0) {
957!
1442
    vAlterTbRsp.code = TSDB_CODE_INVALID_MSG;
×
1443
    tDecoderClear(&dc);
×
1444
    goto _exit;
×
1445
  }
1446

1447
  // process
1448
  if (metaAlterTable(pVnode->pMeta, ver, &vAlterTbReq, &vMetaRsp) < 0) {
957✔
1449
    vAlterTbRsp.code = terrno;
38✔
1450
    tDecoderClear(&dc);
38✔
1451
    goto _exit;
38✔
1452
  }
1453
  tDecoderClear(&dc);
919✔
1454

1455
  if (NULL != vMetaRsp.pSchemas) {
919✔
1456
    vnodeUpdateMetaRsp(pVnode, &vMetaRsp);
183✔
1457
    vAlterTbRsp.pMeta = &vMetaRsp;
183✔
1458
  }
1459

1460
  if (vAlterTbReq.action == TSDB_ALTER_TABLE_UPDATE_TAG_VAL || vAlterTbReq.action == TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL) {
919✔
1461
    int64_t uid = metaGetTableEntryUidByName(pVnode->pMeta, vAlterTbReq.tbName);
654✔
1462
    if (uid == 0) {
654!
1463
      vError("vgId:%d, %s failed at %s:%d since table %s not found", TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
1464
             vAlterTbReq.tbName);
1465
      goto _exit;
×
1466
    }
1467

1468
    SArray* tbUids = taosArrayInit(4, sizeof(int64_t));
654✔
1469
    void* p = taosArrayPush(tbUids, &uid);
654✔
1470
    TSDB_CHECK_NULL(p, code, lino, _exit, terrno);
654!
1471

1472
    vDebug("vgId:%d, remove tags value altered table:%s from query table list", TD_VID(pVnode), vAlterTbReq.tbName);
654✔
1473
    if ((code = tqUpdateTbUidList(pVnode->pTq, tbUids, false)) < 0) {
654!
1474
      vError("vgId:%d, failed to remove tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1475
    }
1476

1477
    vDebug("vgId:%d, try to add table:%s in query table list", TD_VID(pVnode), vAlterTbReq.tbName);
654✔
1478
    if ((code = tqUpdateTbUidList(pVnode->pTq, tbUids, true)) < 0) {
654!
1479
      vError("vgId:%d, failed to add tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1480
    }
1481

1482
    taosArrayDestroy(tbUids);
654✔
1483
  }
1484

1485
_exit:
265✔
1486
  taosArrayDestroy(vAlterTbReq.pMultiTag);
957✔
1487
  tEncodeSize(tEncodeSVAlterTbRsp, &vAlterTbRsp, pRsp->contLen, ret);
957!
1488
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
957✔
1489
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
957✔
1490
  if (tEncodeSVAlterTbRsp(&ec, &vAlterTbRsp) != 0) {
957!
1491
    vError("vgId:%d, failed to encode alter table response", TD_VID(pVnode));
×
1492
  }
1493

1494
  tEncoderClear(&ec);
957✔
1495
  if (vMetaRsp.pSchemas) {
957✔
1496
    taosMemoryFree(vMetaRsp.pSchemas);
183!
1497
    taosMemoryFree(vMetaRsp.pSchemaExt);
183!
1498
  }
1499
  return 0;
957✔
1500
}
1501

1502
static int32_t vnodeProcessDropTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
2,599✔
1503
                                     SRpcMsg *pOriginRpc) {
1504
  SVDropTbBatchReq req = {0};
2,599✔
1505
  SVDropTbBatchRsp rsp = {0};
2,599✔
1506
  SDecoder         decoder = {0};
2,599✔
1507
  SEncoder         encoder = {0};
2,599✔
1508
  int32_t          ret;
1509
  SArray          *tbUids = NULL;
2,599✔
1510
  STbUidStore     *pStore = NULL;
2,599✔
1511
  SArray          *tbNames = NULL;
2,599✔
1512

1513
  pRsp->msgType = TDMT_VND_DROP_TABLE_RSP;
2,599✔
1514
  pRsp->pCont = NULL;
2,599✔
1515
  pRsp->contLen = 0;
2,599✔
1516
  pRsp->code = TSDB_CODE_SUCCESS;
2,599✔
1517

1518
  // decode req
1519
  tDecoderInit(&decoder, pReq, len);
2,599✔
1520
  ret = tDecodeSVDropTbBatchReq(&decoder, &req);
2,599✔
1521
  if (ret < 0) {
2,599!
1522
    terrno = TSDB_CODE_INVALID_MSG;
×
1523
    pRsp->code = terrno;
×
1524
    goto _exit;
×
1525
  }
1526

1527
  // process req
1528
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
2,599✔
1529
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(SVDropTbRsp));
2,599✔
1530
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
2,599✔
1531
  if (tbUids == NULL || rsp.pArray == NULL || tbNames == NULL) goto _exit;
2,599!
1532

1533
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
5,223✔
1534
    SVDropTbReq *pDropTbReq = req.pReqs + iReq;
2,624✔
1535
    SVDropTbRsp  dropTbRsp = {0};
2,624✔
1536
    tb_uid_t     tbUid = 0;
2,624✔
1537

1538
    /* code */
1539
    ret = metaDropTable2(pVnode->pMeta, ver, pDropTbReq);
2,624✔
1540
    if (ret < 0) {
2,624!
1541
      if (pDropTbReq->igNotExists && terrno == TSDB_CODE_TDB_TABLE_NOT_EXIST) {
×
1542
        dropTbRsp.code = TSDB_CODE_SUCCESS;
×
1543
      } else {
1544
        dropTbRsp.code = terrno;
×
1545
      }
1546
    } else {
1547
      dropTbRsp.code = TSDB_CODE_SUCCESS;
2,624✔
1548
      if (tbUid > 0) {
2,624!
1549
        if (tdFetchTbUidList(pVnode->pSma, &pStore, pDropTbReq->suid, tbUid) < 0) {
×
1550
          vError("vgId:%d, failed to fetch tbUid list", TD_VID(pVnode));
×
1551
        }
1552
      }
1553
    }
1554

1555
    if (taosArrayPush(rsp.pArray, &dropTbRsp) == NULL) {
5,248!
1556
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1557
      pRsp->code = terrno;
×
1558
      goto _exit;
×
1559
    }
1560

1561
    if (tsEnableAuditCreateTable) {
2,624!
1562
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
2,624!
1563
      if (str == NULL) {
2,624!
1564
        pRsp->code = terrno;
×
1565
        goto _exit;
×
1566
      }
1567
      tstrncpy(str, pDropTbReq->name, TSDB_TABLE_FNAME_LEN);
2,624✔
1568
      if (taosArrayPush(tbNames, &str) == NULL) {
2,624!
1569
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1570
        pRsp->code = terrno;
×
1571
        goto _exit;
×
1572
      }
1573
    }
1574
  }
1575

1576
  if (tqUpdateTbUidList(pVnode->pTq, tbUids, false) < 0) {
2,599!
1577
    vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(terrno));
×
1578
  }
1579

1580
  if (tdUpdateTbUidList(pVnode->pSma, pStore, false) < 0) {
2,599!
1581
    goto _exit;
×
1582
  }
1583

1584
  if (tsEnableAuditCreateTable) {
2,599!
1585
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
2,599✔
1586

1587
    SName name = {0};
2,599✔
1588
    if (tNameFromString(&name, pVnode->config.dbname, T_NAME_ACCT | T_NAME_DB) != 0) {
2,599!
1589
      vError("vgId:%d, failed to get name from string", TD_VID(pVnode));
×
1590
    }
1591

1592
    SStringBuilder sb = {0};
2,599✔
1593
    for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
5,223✔
1594
      char **key = (char **)taosArrayGet(tbNames, iReq);
2,624✔
1595
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
2,624✔
1596
      if (iReq < req.nReqs - 1) {
2,624✔
1597
        taosStringBuilderAppendChar(&sb, ',');
25✔
1598
      }
1599
      taosMemoryFreeClear(*key);
2,624!
1600
    }
1601

1602
    size_t len = 0;
2,599✔
1603
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
2,599✔
1604

1605
    if (pOriginRpc->info.conn.user != NULL && strlen(pOriginRpc->info.conn.user) > 0) {
2,599!
1606
      auditAddRecord(pOriginRpc, clusterId, "dropTable", name.dbname, "", keyJoined, len);
2,502✔
1607
    }
1608

1609
    taosStringBuilderDestroy(&sb);
2,599✔
1610
  }
1611

1612
_exit:
×
1613
  taosArrayDestroy(tbUids);
2,599✔
1614
  pStore = tdUidStoreFree(pStore);
2,599✔
1615
  tDecoderClear(&decoder);
2,599✔
1616
  tEncodeSize(tEncodeSVDropTbBatchRsp, &rsp, pRsp->contLen, ret);
2,599!
1617
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
2,599✔
1618
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
2,599✔
1619
  if (tEncodeSVDropTbBatchRsp(&encoder, &rsp) != 0) {
2,599!
1620
    vError("vgId:%d, failed to encode drop table batch response", TD_VID(pVnode));
×
1621
  }
1622
  tEncoderClear(&encoder);
2,599✔
1623
  taosArrayDestroy(rsp.pArray);
2,599✔
1624
  taosArrayDestroy(tbNames);
2,599✔
1625
  return 0;
2,599✔
1626
}
1627

1628
#ifdef BUILD_NO_CALL
1629
static int32_t vnodeDebugPrintSingleSubmitMsg(SMeta *pMeta, SSubmitBlk *pBlock, SSubmitMsgIter *msgIter,
1630
                                              const char *tags) {
1631
  SSubmitBlkIter blkIter = {0};
1632
  STSchema      *pSchema = NULL;
1633
  tb_uid_t       suid = 0;
1634
  STSRow        *row = NULL;
1635
  int32_t        rv = -1;
1636

1637
  tInitSubmitBlkIter(msgIter, pBlock, &blkIter);
1638
  if (blkIter.row == NULL) return 0;
1639

1640
  int32_t code = metaGetTbTSchemaNotNull(pMeta, msgIter->suid, TD_ROW_SVER(blkIter.row), 1,
1641
                                         &pSchema);  // TODO: use the real schema
1642
  if (TSDB_CODE_SUCCESS != code) {
1643
    printf("%s:%d no valid schema\n", tags, __LINE__);
1644
    return code;
1645
  }
1646

1647
  suid = msgIter->suid;
1648
  rv = TD_ROW_SVER(blkIter.row);
1649

1650
  char __tags[128] = {0};
1651
  snprintf(__tags, 128, "%s: uid %" PRIi64 " ", tags, msgIter->uid);
1652
  while ((row = tGetSubmitBlkNext(&blkIter))) {
1653
    tdSRowPrint(row, pSchema, __tags);
1654
  }
1655

1656
  taosMemoryFreeClear(pSchema);
1657

1658
  return TSDB_CODE_SUCCESS;
1659
}
1660
#endif
1661
typedef struct SSubmitReqConvertCxt {
1662
  SSubmitMsgIter msgIter;
1663
  SSubmitBlk    *pBlock;
1664
  SSubmitBlkIter blkIter;
1665
  STSRow        *pRow;
1666
  STSRowIter     rowIter;
1667
  SSubmitTbData *pTbData;
1668
  STSchema      *pTbSchema;
1669
  SArray        *pColValues;
1670
} SSubmitReqConvertCxt;
1671

1672
static int32_t vnodeResetTableCxt(SMeta *pMeta, SSubmitReqConvertCxt *pCxt) {
×
1673
  taosMemoryFreeClear(pCxt->pTbSchema);
×
1674
  int32_t code = metaGetTbTSchemaNotNull(pMeta, pCxt->msgIter.suid > 0 ? pCxt->msgIter.suid : pCxt->msgIter.uid,
×
1675
                                         pCxt->msgIter.sversion, 1, &pCxt->pTbSchema);
1676
  if (TSDB_CODE_SUCCESS != code) {
×
1677
    return code;
×
1678
  }
1679
  tdSTSRowIterInit(&pCxt->rowIter, pCxt->pTbSchema);
×
1680

1681
  tDestroySubmitTbData(pCxt->pTbData, TSDB_MSG_FLG_ENCODE);
×
1682
  if (NULL == pCxt->pTbData) {
×
1683
    pCxt->pTbData = taosMemoryCalloc(1, sizeof(SSubmitTbData));
×
1684
    if (NULL == pCxt->pTbData) {
×
1685
      return terrno;
×
1686
    }
1687
  }
1688
  pCxt->pTbData->flags = 0;
×
1689
  pCxt->pTbData->suid = pCxt->msgIter.suid;
×
1690
  pCxt->pTbData->uid = pCxt->msgIter.uid;
×
1691
  pCxt->pTbData->sver = pCxt->msgIter.sversion;
×
1692
  pCxt->pTbData->pCreateTbReq = NULL;
×
1693
  pCxt->pTbData->aRowP = taosArrayInit(128, POINTER_BYTES);
×
1694
  if (NULL == pCxt->pTbData->aRowP) {
×
1695
    return terrno;
×
1696
  }
1697

1698
  taosArrayDestroy(pCxt->pColValues);
×
1699
  pCxt->pColValues = taosArrayInit(pCxt->pTbSchema->numOfCols, sizeof(SColVal));
×
1700
  if (NULL == pCxt->pColValues) {
×
1701
    return terrno;
×
1702
  }
1703
  for (int32_t i = 0; i < pCxt->pTbSchema->numOfCols; ++i) {
×
1704
    SColVal val = COL_VAL_NONE(pCxt->pTbSchema->columns[i].colId, pCxt->pTbSchema->columns[i].type);
×
1705
    if (taosArrayPush(pCxt->pColValues, &val) == NULL) {
×
1706
      return terrno;
×
1707
    }
1708
  }
1709

1710
  return TSDB_CODE_SUCCESS;
×
1711
}
1712

1713
static void vnodeDestroySubmitReqConvertCxt(SSubmitReqConvertCxt *pCxt) {
×
1714
  taosMemoryFreeClear(pCxt->pTbSchema);
×
1715
  tDestroySubmitTbData(pCxt->pTbData, TSDB_MSG_FLG_ENCODE);
×
1716
  taosMemoryFreeClear(pCxt->pTbData);
×
1717
  taosArrayDestroy(pCxt->pColValues);
×
1718
}
×
1719

1720
static int32_t vnodeCellValConvertToColVal(STColumn *pCol, SCellVal *pCellVal, SColVal *pColVal) {
×
1721
  if (tdValTypeIsNone(pCellVal->valType)) {
×
1722
    pColVal->flag = CV_FLAG_NONE;
×
1723
    return TSDB_CODE_SUCCESS;
×
1724
  }
1725

1726
  if (tdValTypeIsNull(pCellVal->valType)) {
×
1727
    pColVal->flag = CV_FLAG_NULL;
×
1728
    return TSDB_CODE_SUCCESS;
×
1729
  }
1730

1731
  if (IS_VAR_DATA_TYPE(pCol->type)) {
×
1732
    pColVal->value.nData = varDataLen(pCellVal->val);
×
1733
    pColVal->value.pData = (uint8_t *)varDataVal(pCellVal->val);
×
1734
  } else if (TSDB_DATA_TYPE_FLOAT == pCol->type) {
×
1735
    float f = GET_FLOAT_VAL(pCellVal->val);
×
1736
    memcpy(&pColVal->value.val, &f, sizeof(f));
×
1737
  } else if (TSDB_DATA_TYPE_DOUBLE == pCol->type) {
×
1738
    pColVal->value.val = *(int64_t *)pCellVal->val;
×
1739
  } else {
1740
    GET_TYPED_DATA(pColVal->value.val, int64_t, pCol->type, pCellVal->val);
×
1741
  }
1742

1743
  pColVal->flag = CV_FLAG_VALUE;
×
1744
  return TSDB_CODE_SUCCESS;
×
1745
}
1746

1747
static int32_t vnodeTSRowConvertToColValArray(SSubmitReqConvertCxt *pCxt) {
×
1748
  int32_t code = TSDB_CODE_SUCCESS;
×
1749
  tdSTSRowIterReset(&pCxt->rowIter, pCxt->pRow);
×
1750
  for (int32_t i = 0; TSDB_CODE_SUCCESS == code && i < pCxt->pTbSchema->numOfCols; ++i) {
×
1751
    STColumn *pCol = pCxt->pTbSchema->columns + i;
×
1752
    SCellVal  cellVal = {0};
×
1753
    if (!tdSTSRowIterFetch(&pCxt->rowIter, pCol->colId, pCol->type, &cellVal)) {
×
1754
      break;
×
1755
    }
1756
    code = vnodeCellValConvertToColVal(pCol, &cellVal, (SColVal *)taosArrayGet(pCxt->pColValues, i));
×
1757
  }
1758
  return code;
×
1759
}
1760

1761
static int32_t vnodeDecodeCreateTbReq(SSubmitReqConvertCxt *pCxt) {
×
1762
  if (pCxt->msgIter.schemaLen <= 0) {
×
1763
    return TSDB_CODE_SUCCESS;
×
1764
  }
1765

1766
  pCxt->pTbData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
×
1767
  if (NULL == pCxt->pTbData->pCreateTbReq) {
×
1768
    return terrno;
×
1769
  }
1770

1771
  SDecoder decoder = {0};
×
1772
  tDecoderInit(&decoder, (uint8_t *)pCxt->pBlock->data, pCxt->msgIter.schemaLen);
×
1773
  int32_t code = tDecodeSVCreateTbReq(&decoder, pCxt->pTbData->pCreateTbReq);
×
1774
  tDecoderClear(&decoder);
×
1775

1776
  return code;
×
1777
}
1778

1779
static int32_t vnodeSubmitReqConvertToSubmitReq2(SVnode *pVnode, SSubmitReq *pReq, SSubmitReq2 *pReq2) {
×
1780
  pReq2->aSubmitTbData = taosArrayInit(128, sizeof(SSubmitTbData));
×
1781
  if (NULL == pReq2->aSubmitTbData) {
×
1782
    return terrno;
×
1783
  }
1784

1785
  SSubmitReqConvertCxt cxt = {0};
×
1786

1787
  int32_t code = tInitSubmitMsgIter(pReq, &cxt.msgIter);
×
1788
  while (TSDB_CODE_SUCCESS == code) {
×
1789
    code = tGetSubmitMsgNext(&cxt.msgIter, &cxt.pBlock);
×
1790
    if (TSDB_CODE_SUCCESS == code) {
×
1791
      if (NULL == cxt.pBlock) {
×
1792
        break;
×
1793
      }
1794
      code = vnodeResetTableCxt(pVnode->pMeta, &cxt);
×
1795
    }
1796
    if (TSDB_CODE_SUCCESS == code) {
×
1797
      code = tInitSubmitBlkIter(&cxt.msgIter, cxt.pBlock, &cxt.blkIter);
×
1798
    }
1799
    if (TSDB_CODE_SUCCESS == code) {
×
1800
      code = vnodeDecodeCreateTbReq(&cxt);
×
1801
    }
1802
    while (TSDB_CODE_SUCCESS == code && (cxt.pRow = tGetSubmitBlkNext(&cxt.blkIter)) != NULL) {
×
1803
      code = vnodeTSRowConvertToColValArray(&cxt);
×
1804
      if (TSDB_CODE_SUCCESS == code) {
×
1805
        SRow **pNewRow = taosArrayReserve(cxt.pTbData->aRowP, 1);
×
1806
        code = tRowBuild(cxt.pColValues, cxt.pTbSchema, pNewRow);
×
1807
      }
1808
    }
1809
    if (TSDB_CODE_SUCCESS == code) {
×
1810
      code = (NULL == taosArrayPush(pReq2->aSubmitTbData, cxt.pTbData) ? terrno : TSDB_CODE_SUCCESS);
×
1811
    }
1812
    if (TSDB_CODE_SUCCESS == code) {
×
1813
      taosMemoryFreeClear(cxt.pTbData);
×
1814
    }
1815
  }
1816

1817
  vnodeDestroySubmitReqConvertCxt(&cxt);
×
1818
  return code;
×
1819
}
1820

1821
static int32_t vnodeRebuildSubmitReqMsg(SSubmitReq2 *pSubmitReq, void **ppMsg) {
×
1822
  int32_t  code = TSDB_CODE_SUCCESS;
×
1823
  char    *pMsg = NULL;
×
1824
  uint32_t msglen = 0;
×
1825
  tEncodeSize(tEncodeSubmitReq, pSubmitReq, msglen, code);
×
1826
  if (TSDB_CODE_SUCCESS == code) {
×
1827
    pMsg = taosMemoryMalloc(msglen);
×
1828
    if (NULL == pMsg) {
×
1829
      code = terrno;
×
1830
    }
1831
  }
1832
  if (TSDB_CODE_SUCCESS == code) {
×
1833
    SEncoder encoder;
1834
    tEncoderInit(&encoder, (uint8_t *)pMsg, msglen);
×
1835
    code = tEncodeSubmitReq(&encoder, pSubmitReq);
×
1836
    tEncoderClear(&encoder);
×
1837
  }
1838
  if (TSDB_CODE_SUCCESS == code) {
×
1839
    *ppMsg = pMsg;
×
1840
  }
1841
  return code;
×
1842
}
1843

1844
static int32_t vnodeProcessSubmitReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
13,072,715✔
1845
                                     SRpcMsg *pOriginalMsg) {
1846
  int32_t code = 0;
13,072,715✔
1847
  terrno = 0;
13,072,715✔
1848

1849
  SSubmitReq2 *pSubmitReq = &(SSubmitReq2){0};
13,072,842✔
1850
  SSubmitRsp2 *pSubmitRsp = &(SSubmitRsp2){0};
13,072,842✔
1851
  SArray      *newTbUids = NULL;
13,072,842✔
1852
  int32_t      ret;
1853
  SEncoder     ec = {0};
13,072,842✔
1854

1855
  pRsp->code = TSDB_CODE_SUCCESS;
13,072,842✔
1856

1857
  void           *pAllocMsg = NULL;
13,072,842✔
1858
  SSubmitReq2Msg *pMsg = (SSubmitReq2Msg *)pReq;
13,072,842✔
1859
  if (0 == pMsg->version) {
13,072,842!
1860
    code = vnodeSubmitReqConvertToSubmitReq2(pVnode, (SSubmitReq *)pMsg, pSubmitReq);
×
1861
    if (TSDB_CODE_SUCCESS == code) {
×
1862
      code = vnodeRebuildSubmitReqMsg(pSubmitReq, &pReq);
×
1863
    }
UNCOV
1864
    if (TSDB_CODE_SUCCESS == code) {
×
1865
      pAllocMsg = pReq;
×
1866
    }
UNCOV
1867
    if (TSDB_CODE_SUCCESS != code) {
×
1868
      goto _exit;
×
1869
    }
1870
  } else {
1871
    // decode
1872
    pReq = POINTER_SHIFT(pReq, sizeof(SSubmitReq2Msg));
13,072,842✔
1873
    len -= sizeof(SSubmitReq2Msg);
13,072,842✔
1874
    SDecoder dc = {0};
13,072,842✔
1875
    tDecoderInit(&dc, pReq, len);
13,072,842✔
1876
    if (tDecodeSubmitReq(&dc, pSubmitReq) < 0) {
13,072,681!
1877
      code = TSDB_CODE_INVALID_MSG;
×
1878
      goto _exit;
×
1879
    }
1880
    tDecoderClear(&dc);
13,072,713✔
1881
  }
1882

1883
  // scan
1884
  TSKEY now = taosGetTimestamp(pVnode->config.tsdbCfg.precision);
13,072,850✔
1885
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * pVnode->config.tsdbCfg.keep2;
13,072,705✔
1886
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
13,072,705✔
1887
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
26,890,900✔
1888
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
13,817,396✔
1889

1890
    if (pSubmitTbData->pCreateTbReq && pSubmitTbData->pCreateTbReq->uid == 0) {
13,817,310!
1891
      code = TSDB_CODE_INVALID_MSG;
×
1892
      goto _exit;
×
1893
    }
1894

1895
    if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
13,817,310✔
1896
      if (TARRAY_SIZE(pSubmitTbData->aCol) <= 0) {
135!
1897
        code = TSDB_CODE_INVALID_MSG;
×
1898
        goto _exit;
×
1899
      }
1900

1901
      SColData *colDataArr = TARRAY_DATA(pSubmitTbData->aCol);
135✔
1902
      SRowKey   lastKey;
1903
      tColDataArrGetRowKey(colDataArr, TARRAY_SIZE(pSubmitTbData->aCol), 0, &lastKey);
135✔
1904
      for (int32_t iRow = 1; iRow < colDataArr[0].nVal; iRow++) {
234✔
1905
        SRowKey key;
1906
        tColDataArrGetRowKey(TARRAY_DATA(pSubmitTbData->aCol), TARRAY_SIZE(pSubmitTbData->aCol), iRow, &key);
99✔
1907
        if (tRowKeyCompare(&lastKey, &key) >= 0) {
99!
1908
          code = TSDB_CODE_INVALID_MSG;
×
1909
          vError("vgId:%d %s failed 1 since %s, version:%" PRId64, TD_VID(pVnode), __func__, tstrerror(terrno), ver);
×
1910
          goto _exit;
×
1911
        }
1912
      }
1913
    } else {
1914
      int32_t nRow = TARRAY_SIZE(pSubmitTbData->aRowP);
13,817,175✔
1915
      SRow  **aRow = (SRow **)TARRAY_DATA(pSubmitTbData->aRowP);
13,817,175✔
1916
      SRowKey lastRowKey;
1917
      for (int32_t iRow = 0; iRow < nRow; ++iRow) {
840,573,650✔
1918
        if (aRow[iRow]->ts < minKey || aRow[iRow]->ts > maxKey) {
826,755,590✔
1919
          code = TSDB_CODE_INVALID_MSG;
42,115✔
1920
          vError("vgId:%d %s failed 2 since %s, version:%" PRId64, TD_VID(pVnode), __func__, tstrerror(code), ver);
42,115!
1921
          goto _exit;
×
1922
        }
1923
        if (iRow == 0) {
826,713,475✔
1924
          tRowGetKey(aRow[iRow], &lastRowKey);
13,817,274✔
1925
        } else {
1926
          SRowKey rowKey;
1927
          tRowGetKey(aRow[iRow], &rowKey);
812,896,201✔
1928

1929
          if (tRowKeyCompare(&lastRowKey, &rowKey) >= 0) {
812,886,984!
1930
            code = TSDB_CODE_INVALID_MSG;
×
1931
            vError("vgId:%d %s failed 3 since %s, version:%" PRId64, TD_VID(pVnode), __func__, tstrerror(code), ver);
×
1932
            goto _exit;
×
1933
          }
1934
          lastRowKey = rowKey;
812,938,615✔
1935
        }
1936
      }
1937
    }
1938
  }
1939

1940
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
26,892,604✔
1941
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
13,818,792✔
1942

1943
    if (pSubmitTbData->pCreateTbReq) {
13,818,500✔
1944
      pSubmitTbData->uid = pSubmitTbData->pCreateTbReq->uid;
63,181✔
1945
    } else {
1946
      SMetaInfo info = {0};
13,755,319✔
1947

1948
      code = metaGetInfo(pVnode->pMeta, pSubmitTbData->uid, &info, NULL);
13,755,319✔
1949
      if (code) {
13,755,675!
1950
        code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
1951
        vWarn("vgId:%d, table uid:%" PRId64 " not exists", TD_VID(pVnode), pSubmitTbData->uid);
×
1952
        goto _exit;
7✔
1953
      }
1954

1955
      if (info.suid != pSubmitTbData->suid) {
13,755,705!
1956
        code = TSDB_CODE_INVALID_MSG;
×
1957
        goto _exit;
×
1958
      }
1959

1960
      if (info.suid) {
13,755,705✔
1961
        if (metaGetInfo(pVnode->pMeta, info.suid, &info, NULL) != 0) {
12,765,176!
1962
          vWarn("vgId:%d, table uid:%" PRId64 " not exists", TD_VID(pVnode), info.suid);
×
1963
        }
1964
      }
1965

1966
      if (pSubmitTbData->sver != info.skmVer) {
13,755,919!
1967
        code = TSDB_CODE_TDB_INVALID_TABLE_SCHEMA_VER;
×
1968
        goto _exit;
×
1969
      }
1970
    }
1971

1972
    if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
13,819,100✔
1973
      int32_t   nColData = TARRAY_SIZE(pSubmitTbData->aCol);
135✔
1974
      SColData *aColData = (SColData *)TARRAY_DATA(pSubmitTbData->aCol);
135✔
1975

1976
      if (nColData <= 0) {
135!
1977
        code = TSDB_CODE_INVALID_MSG;
×
1978
        goto _exit;
×
1979
      }
1980

1981
      if (aColData[0].cid != PRIMARYKEY_TIMESTAMP_COL_ID || aColData[0].type != TSDB_DATA_TYPE_TIMESTAMP ||
135!
1982
          aColData[0].nVal <= 0) {
135!
1983
        code = TSDB_CODE_INVALID_MSG;
×
1984
        goto _exit;
×
1985
      }
1986

1987
      for (int32_t j = 1; j < nColData; j++) {
587✔
1988
        if (aColData[j].nVal != aColData[0].nVal) {
452!
1989
          code = TSDB_CODE_INVALID_MSG;
×
1990
          goto _exit;
×
1991
        }
1992
      }
1993
    }
1994
  }
1995

1996
  vDebug("vgId:%d, submit block size %d", TD_VID(pVnode), (int32_t)taosArrayGetSize(pSubmitReq->aSubmitTbData));
13,073,812✔
1997

1998
  // loop to handle
1999
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
26,889,247✔
2000
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
13,816,411✔
2001

2002
    // create table
2003
    if (pSubmitTbData->pCreateTbReq) {
13,816,165✔
2004
      // alloc if need
2005
      if (pSubmitRsp->aCreateTbRsp == NULL &&
63,204✔
2006
          (pSubmitRsp->aCreateTbRsp = taosArrayInit(TARRAY_SIZE(pSubmitReq->aSubmitTbData), sizeof(SVCreateTbRsp))) ==
46,169!
2007
              NULL) {
2008
        code = terrno;
×
2009
        goto _exit;
8✔
2010
      }
2011

2012
      SVCreateTbRsp *pCreateTbRsp = taosArrayReserve(pSubmitRsp->aCreateTbRsp, 1);
63,205✔
2013

2014
      // create table
2015
      if (metaCreateTable2(pVnode->pMeta, ver, pSubmitTbData->pCreateTbReq, &pCreateTbRsp->pMeta) == 0) {
63,203✔
2016
        // create table success
2017

2018
        if (newTbUids == NULL &&
63,631!
2019
            (newTbUids = taosArrayInit(TARRAY_SIZE(pSubmitReq->aSubmitTbData), sizeof(int64_t))) == NULL) {
26,216✔
2020
          code = terrno;
×
2021
          goto _exit;
×
2022
        }
2023

2024
        if (taosArrayPush(newTbUids, &pSubmitTbData->uid) == NULL) {
74,830!
2025
          code = terrno;
×
2026
          goto _exit;
×
2027
        }
2028

2029
        if (pCreateTbRsp->pMeta) {
37,415!
2030
          vnodeUpdateMetaRsp(pVnode, pCreateTbRsp->pMeta);
37,415✔
2031
        }
2032
      } else {  // create table failed
2033
        if (terrno != TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
25,788✔
2034
          code = terrno;
8✔
2035
          vError("vgId:%d failed to create table:%s, code:%s", TD_VID(pVnode), pSubmitTbData->pCreateTbReq->name,
8!
2036
                 tstrerror(terrno));
2037
          goto _exit;
8✔
2038
        }
2039
        terrno = 0;
25,779✔
2040
        pSubmitTbData->uid = pSubmitTbData->pCreateTbReq->uid;  // update uid if table exist for using below
25,779✔
2041
      }
2042
    }
2043

2044
    // insert data
2045
    int32_t affectedRows;
2046
    code = tsdbInsertTableData(pVnode->pTsdb, ver, pSubmitTbData, &affectedRows);
13,816,155✔
2047
    if (code) goto _exit;
13,816,737!
2048

2049
    code = metaUpdateChangeTimeWithLock(pVnode->pMeta, pSubmitTbData->uid, pSubmitTbData->ctimeMs);
13,816,737✔
2050
    if (code) goto _exit;
13,816,465!
2051

2052
    pSubmitRsp->affectedRows += affectedRows;
13,816,465✔
2053
  }
2054

2055
  // update the affected table uid list
2056
  if (taosArrayGetSize(newTbUids) > 0) {
13,072,836✔
2057
    vDebug("vgId:%d, add %d table into query table list in handling submit", TD_VID(pVnode),
26,216✔
2058
           (int32_t)taosArrayGetSize(newTbUids));
2059
    if (tqUpdateTbUidList(pVnode->pTq, newTbUids, true) != 0) {
26,216!
2060
      vError("vgId:%d, failed to update tbUid list", TD_VID(pVnode));
×
2061
    }
2062
  }
2063

2064
_exit:
13,072,841✔
2065
  // message
2066
  pRsp->code = code;
13,072,856✔
2067
  tEncodeSize(tEncodeSSubmitRsp2, pSubmitRsp, pRsp->contLen, ret);
13,072,856!
2068
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
13,072,710✔
2069
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
13,072,885✔
2070
  if (tEncodeSSubmitRsp2(&ec, pSubmitRsp) < 0) {
13,072,889!
2071
    vError("vgId:%d, failed to encode submit response", TD_VID(pVnode));
×
2072
  }
2073
  tEncoderClear(&ec);
13,072,756✔
2074

2075
  // update statistics
2076
  (void)atomic_add_fetch_64(&pVnode->statis.nInsert, pSubmitRsp->affectedRows);
13,072,827✔
2077
  (void)atomic_add_fetch_64(&pVnode->statis.nInsertSuccess, pSubmitRsp->affectedRows);
13,072,891✔
2078
  (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsert, 1);
13,072,881✔
2079

2080
  if (tsEnableMonitor && tsMonitorFqdn[0] != 0 && tsMonitorPort != 0 && pSubmitRsp->affectedRows > 0 &&
13,072,895!
2081
      strlen(pOriginalMsg->info.conn.user) > 0 && tsInsertCounter != NULL) {
×
2082
    const char *sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT_AFFECTED_ROWS,
×
2083
                                   pVnode->monitor.strClusterId,
×
2084
                                   pVnode->monitor.strDnodeId,
×
2085
                                   tsLocalEp,
2086
                                   pVnode->monitor.strVgId,
×
2087
                                   pOriginalMsg->info.conn.user,
×
2088
                                   "Success"};
2089
    int         tv = taos_counter_add(tsInsertCounter, pSubmitRsp->affectedRows, sample_labels);
×
2090
  }
2091

2092
  if (code == 0) {
13,072,895✔
2093
    (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
13,072,862✔
2094
    code = tdProcessRSmaSubmit(pVnode->pSma, ver, pSubmitReq, pReq, len);
13,072,908✔
2095
  }
2096
  /*
2097
  if (code == 0) {
2098
    atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
2099
    code = tdProcessRSmaSubmit(pVnode->pSma, ver, pSubmitReq, pReq, len);
2100

2101
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT, pVnode->monitor.strClusterId,
2102
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2103
                                          pOriginalMsg->info.conn.user, "Success"};
2104
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2105
  }
2106
  else{
2107
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT, pVnode->monitor.strClusterId,
2108
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2109
                                        pOriginalMsg->info.conn.user, "Failed"};
2110
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2111
  }
2112
  */
2113

2114
  // clear
2115
  taosArrayDestroy(newTbUids);
13,072,865✔
2116
  tDestroySubmitReq(pSubmitReq, 0 == pMsg->version ? TSDB_MSG_FLG_CMPT : TSDB_MSG_FLG_DECODE);
13,072,767!
2117
  tDestroySSubmitRsp2(pSubmitRsp, TSDB_MSG_FLG_ENCODE);
13,072,814✔
2118

2119
  if (code) terrno = code;
13,072,726✔
2120

2121
  taosMemoryFree(pAllocMsg);
13,072,726!
2122

2123
  return code;
13,072,673✔
2124
}
2125

2126
static int32_t vnodeProcessCreateTSmaReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
31✔
2127
  SVCreateTSmaReq req = {0};
31✔
2128
  SDecoder        coder = {0};
31✔
2129

2130
  if (pRsp) {
31!
2131
    pRsp->msgType = TDMT_VND_CREATE_SMA_RSP;
31✔
2132
    pRsp->code = TSDB_CODE_SUCCESS;
31✔
2133
    pRsp->pCont = NULL;
31✔
2134
    pRsp->contLen = 0;
31✔
2135
  }
2136

2137
  // decode and process req
2138
  tDecoderInit(&coder, pReq, len);
31✔
2139

2140
  if (tDecodeSVCreateTSmaReq(&coder, &req) < 0) {
31!
2141
    terrno = TSDB_CODE_MSG_DECODE_ERROR;
×
2142
    if (pRsp) pRsp->code = terrno;
×
2143
    goto _err;
×
2144
  }
2145

2146
  if (tdProcessTSmaCreate(pVnode->pSma, ver, (const char *)&req) < 0) {
31!
2147
    if (pRsp) pRsp->code = terrno;
×
2148
    goto _err;
×
2149
  }
2150

2151
  tDecoderClear(&coder);
31✔
2152
  vDebug("vgId:%d, success to create tsma %s:%" PRIi64 " version %" PRIi64 " for table %" PRIi64, TD_VID(pVnode),
31!
2153
         req.indexName, req.indexUid, ver, req.tableUid);
2154
  return 0;
31✔
2155

2156
_err:
×
2157
  tDecoderClear(&coder);
×
2158
  vError("vgId:%d, failed to create tsma %s:%" PRIi64 " version %" PRIi64 "for table %" PRIi64 " since %s",
×
2159
         TD_VID(pVnode), req.indexName, req.indexUid, ver, req.tableUid, terrstr());
2160
  return terrno;
×
2161
}
2162

2163
/**
2164
 * @brief specific for smaDstVnode
2165
 *
2166
 * @param pVnode
2167
 * @param pCont
2168
 * @param contLen
2169
 * @return int32_t
2170
 */
2171
int32_t vnodeProcessCreateTSma(SVnode *pVnode, void *pCont, uint32_t contLen) {
×
2172
  return vnodeProcessCreateTSmaReq(pVnode, 1, pCont, contLen, NULL);
×
2173
}
2174

2175
static int32_t vnodeConsolidateAlterHashRange(SVnode *pVnode, int64_t ver) {
42✔
2176
  int32_t code = TSDB_CODE_SUCCESS;
42✔
2177

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

2181
  // TODO: trim meta of tables from TDB per hash range [pVnode->config.hashBegin, pVnode->config.hashEnd]
2182
  code = metaTrimTables(pVnode->pMeta, ver);
42✔
2183

2184
  return code;
42✔
2185
}
2186

2187
static int32_t vnodeProcessAlterConfirmReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
8,093✔
2188
  vInfo("vgId:%d, vnode handle msgType:alter-confirm, alter confirm msg is processed", TD_VID(pVnode));
8,093!
2189
  int32_t code = TSDB_CODE_SUCCESS;
8,093✔
2190
  if (!pVnode->config.hashChange) {
8,093✔
2191
    goto _exit;
8,051✔
2192
  }
2193

2194
  code = vnodeConsolidateAlterHashRange(pVnode, ver);
42✔
2195
  if (code < 0) {
42!
2196
    vError("vgId:%d, failed to consolidate alter hashrange since %s. version:%" PRId64, TD_VID(pVnode), terrstr(), ver);
×
2197
    goto _exit;
×
2198
  }
2199
  pVnode->config.hashChange = false;
42✔
2200

2201
_exit:
8,093✔
2202
  pRsp->msgType = TDMT_VND_ALTER_CONFIRM_RSP;
8,093✔
2203
  pRsp->code = code;
8,093✔
2204
  pRsp->pCont = NULL;
8,093✔
2205
  pRsp->contLen = 0;
8,093✔
2206

2207
  return code;
8,093✔
2208
}
2209

2210
extern int32_t tsdbDisableAndCancelAllBgTask(STsdb *pTsdb);
2211
extern void    tsdbEnableBgTask(STsdb *pTsdb);
2212

2213
static int32_t vnodeProcessAlterConfigReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
760✔
2214
  bool walChanged = false;
760✔
2215
  bool tsdbChanged = false;
760✔
2216

2217
  SAlterVnodeConfigReq req = {0};
760✔
2218
  if (tDeserializeSAlterVnodeConfigReq(pReq, len, &req) != 0) {
760!
2219
    terrno = TSDB_CODE_INVALID_MSG;
×
2220
    return TSDB_CODE_INVALID_MSG;
×
2221
  }
2222

2223
  vInfo("vgId:%d, start to alter vnode config, page:%d pageSize:%d buffer:%d szPage:%d szBuf:%" PRIu64
762✔
2224
        " cacheLast:%d cacheLastSize:%d days:%d keep0:%d keep1:%d keep2:%d keepTimeOffset:%d s3KeepLocal:%d "
2225
        "s3Compact:%d fsync:%d level:%d "
2226
        "walRetentionPeriod:%d walRetentionSize:%d",
2227
        TD_VID(pVnode), req.pages, req.pageSize, req.buffer, req.pageSize * 1024, (uint64_t)req.buffer * 1024 * 1024,
2228
        req.cacheLast, req.cacheLastSize, req.daysPerFile, req.daysToKeep0, req.daysToKeep1, req.daysToKeep2,
2229
        req.keepTimeOffset, req.s3KeepLocal, req.s3Compact, req.walFsyncPeriod, req.walLevel, req.walRetentionPeriod,
2230
        req.walRetentionSize);
2231

2232
  if (pVnode->config.cacheLastSize != req.cacheLastSize) {
764✔
2233
    pVnode->config.cacheLastSize = req.cacheLastSize;
367✔
2234
    tsdbCacheSetCapacity(pVnode, (size_t)pVnode->config.cacheLastSize * 1024 * 1024);
367✔
2235
  }
2236

2237
  if (pVnode->config.szBuf != req.buffer * 1024LL * 1024LL) {
764!
2238
    vInfo("vgId:%d, vnode buffer is changed from %" PRId64 " to %" PRId64, TD_VID(pVnode), pVnode->config.szBuf,
×
2239
          (uint64_t)(req.buffer * 1024LL * 1024LL));
2240
    pVnode->config.szBuf = req.buffer * 1024LL * 1024LL;
×
2241
  }
2242

2243
  if (pVnode->config.szCache != req.pages) {
764!
2244
    if (metaAlterCache(pVnode->pMeta, req.pages) < 0) {
×
2245
      vError("vgId:%d, failed to change vnode pages from %d to %d failed since %s", TD_VID(pVnode),
×
2246
             pVnode->config.szCache, req.pages, tstrerror(errno));
2247
      return errno;
×
2248
    } else {
2249
      vInfo("vgId:%d, vnode pages is changed from %d to %d", TD_VID(pVnode), pVnode->config.szCache, req.pages);
×
2250
      pVnode->config.szCache = req.pages;
×
2251
    }
2252
  }
2253

2254
  if (pVnode->config.cacheLast != req.cacheLast) {
764✔
2255
    pVnode->config.cacheLast = req.cacheLast;
480✔
2256
  }
2257

2258
  if (pVnode->config.walCfg.fsyncPeriod != req.walFsyncPeriod) {
764✔
2259
    pVnode->config.walCfg.fsyncPeriod = req.walFsyncPeriod;
54✔
2260
    walChanged = true;
54✔
2261
  }
2262

2263
  if (pVnode->config.walCfg.level != req.walLevel) {
764✔
2264
    if (pVnode->config.walCfg.level == 0) {
36!
2265
      pVnode->config.walCfg.clearFiles = 1;
×
2266
    }
2267
    pVnode->config.walCfg.level = req.walLevel;
36✔
2268
    walChanged = true;
36✔
2269
  }
2270

2271
  if (pVnode->config.walCfg.retentionPeriod != req.walRetentionPeriod) {
764✔
2272
    pVnode->config.walCfg.retentionPeriod = req.walRetentionPeriod;
132✔
2273
    walChanged = true;
132✔
2274
  }
2275

2276
  if (pVnode->config.walCfg.retentionSize != req.walRetentionSize) {
764!
2277
    pVnode->config.walCfg.retentionSize = req.walRetentionSize;
×
2278
    walChanged = true;
×
2279
  }
2280

2281
  if (pVnode->config.tsdbCfg.keep0 != req.daysToKeep0) {
764✔
2282
    pVnode->config.tsdbCfg.keep0 = req.daysToKeep0;
54✔
2283
    if (!VND_IS_RSMA(pVnode)) {
54!
2284
      tsdbChanged = true;
54✔
2285
    }
2286
  }
2287

2288
  if (pVnode->config.tsdbCfg.keep1 != req.daysToKeep1) {
764✔
2289
    pVnode->config.tsdbCfg.keep1 = req.daysToKeep1;
54✔
2290
    if (!VND_IS_RSMA(pVnode)) {
54!
2291
      tsdbChanged = true;
54✔
2292
    }
2293
  }
2294

2295
  if (pVnode->config.tsdbCfg.keep2 != req.daysToKeep2) {
764✔
2296
    pVnode->config.tsdbCfg.keep2 = req.daysToKeep2;
56✔
2297
    if (!VND_IS_RSMA(pVnode)) {
56!
2298
      tsdbChanged = true;
56✔
2299
    }
2300
  }
2301

2302
  if (pVnode->config.tsdbCfg.keepTimeOffset != req.keepTimeOffset) {
764✔
2303
    pVnode->config.tsdbCfg.keepTimeOffset = req.keepTimeOffset;
2✔
2304
    if (!VND_IS_RSMA(pVnode)) {
2!
2305
      tsdbChanged = true;
2✔
2306
    }
2307
  }
2308

2309
  if (req.sttTrigger != -1 && req.sttTrigger != pVnode->config.sttTrigger) {
764!
2310
    if (req.sttTrigger > 1 && pVnode->config.sttTrigger > 1) {
×
2311
      pVnode->config.sttTrigger = req.sttTrigger;
×
2312
    } else {
2313
      vnodeAWait(&pVnode->commitTask);
×
2314

2315
      int32_t ret = tsdbDisableAndCancelAllBgTask(pVnode->pTsdb);
×
2316
      if (ret != 0) {
×
2317
        vError("vgId:%d, failed to disable bg task since %s", TD_VID(pVnode), tstrerror(errno));
×
2318
      }
2319

2320
      pVnode->config.sttTrigger = req.sttTrigger;
×
2321
      tsdbEnableBgTask(pVnode->pTsdb);
×
2322
    }
2323
  }
2324

2325
  if (req.minRows != -1 && req.minRows != pVnode->config.tsdbCfg.minRows) {
764!
2326
    pVnode->config.tsdbCfg.minRows = req.minRows;
×
2327
  }
2328

2329
  if (req.s3KeepLocal != -1 && req.s3KeepLocal != pVnode->config.s3KeepLocal) {
764!
2330
    pVnode->config.s3KeepLocal = req.s3KeepLocal;
×
2331
  }
2332
  if (req.s3Compact != -1 && req.s3Compact != pVnode->config.s3Compact) {
764!
2333
    pVnode->config.s3Compact = req.s3Compact;
×
2334
  }
2335

2336
  if (walChanged) {
764✔
2337
    if (walAlter(pVnode->pWal, &pVnode->config.walCfg) != 0) {
222!
2338
      vError("vgId:%d, failed to alter wal config since %s", TD_VID(pVnode), tstrerror(errno));
×
2339
    }
2340
  }
2341

2342
  if (tsdbChanged) {
762✔
2343
    tsdbSetKeepCfg(pVnode->pTsdb, &pVnode->config.tsdbCfg);
58✔
2344
  }
2345

2346
  return 0;
762✔
2347
}
2348

2349
static int32_t vnodeProcessBatchDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
2,934✔
2350
  SBatchDeleteReq deleteReq;
2351
  SDecoder        decoder;
2352
  tDecoderInit(&decoder, pReq, len);
2,934✔
2353
  if (tDecodeSBatchDeleteReq(&decoder, &deleteReq) < 0) {
2,934!
2354
    tDecoderClear(&decoder);
×
2355
    return terrno = TSDB_CODE_INVALID_MSG;
×
2356
  }
2357

2358
  SMetaReader mr = {0};
2,934✔
2359
  metaReaderDoInit(&mr, pVnode->pMeta, META_READER_NOLOCK);
2,934✔
2360
  STsdb *pTsdb = pVnode->pTsdb;
2,935✔
2361

2362
  if (deleteReq.level) {
2,935✔
2363
    pTsdb = deleteReq.level == 1 ? VND_RSMA1(pVnode) : VND_RSMA2(pVnode);
16✔
2364
  }
2365

2366
  int32_t sz = taosArrayGetSize(deleteReq.deleteReqs);
2,935✔
2367
  for (int32_t i = 0; i < sz; i++) {
6,764✔
2368
    SSingleDeleteReq *pOneReq = taosArrayGet(deleteReq.deleteReqs, i);
3,829✔
2369
    char             *name = pOneReq->tbname;
3,829✔
2370
    if (metaGetTableEntryByName(&mr, name) < 0) {
3,829✔
2371
      vDebug("vgId:%d, stream delete msg, skip since no table: %s", pVnode->config.vgId, name);
1,950✔
2372
      continue;
1,950✔
2373
    }
2374

2375
    int64_t uid = mr.me.uid;
1,879✔
2376

2377
    int32_t code = tsdbDeleteTableData(pTsdb, ver, deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
1,879✔
2378
    if (code < 0) {
1,879!
2379
      terrno = code;
×
2380
      vError("vgId:%d, delete error since %s, suid:%" PRId64 ", uid:%" PRId64 ", start ts:%" PRId64 ", end ts:%" PRId64,
×
2381
             TD_VID(pVnode), terrstr(), deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
2382
    }
2383

2384
    if (deleteReq.level == 0) {
1,879✔
2385
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, deleteReq.ctimeMs);
1,263✔
2386
      if (code < 0) {
1,263!
2387
        terrno = code;
×
2388
        vError("vgId:%d, update change time error since %s, suid:%" PRId64 ", uid:%" PRId64 ", start ts:%" PRId64
×
2389
               ", end ts:%" PRId64,
2390
               TD_VID(pVnode), terrstr(), deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
2391
      }
2392
    }
2393
    tDecoderClear(&mr.coder);
1,879✔
2394
  }
2395
  metaReaderClear(&mr);
2,935✔
2396
  taosArrayDestroy(deleteReq.deleteReqs);
2,933✔
2397
  return 0;
2,934✔
2398
}
2399

2400
static int32_t vnodeProcessDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
62,375✔
2401
                                     SRpcMsg *pOriginalMsg) {
2402
  int32_t     code = 0;
62,375✔
2403
  SDecoder   *pCoder = &(SDecoder){0};
62,375✔
2404
  SDeleteRes *pRes = &(SDeleteRes){0};
62,375✔
2405

2406
  pRsp->msgType = TDMT_VND_DELETE_RSP;
62,375✔
2407
  pRsp->pCont = NULL;
62,375✔
2408
  pRsp->contLen = 0;
62,375✔
2409
  pRsp->code = TSDB_CODE_SUCCESS;
62,375✔
2410

2411
  pRes->uidList = taosArrayInit(0, sizeof(tb_uid_t));
62,375✔
2412
  if (pRes->uidList == NULL) {
62,377!
2413
    code = terrno;
×
2414
    goto _err;
×
2415
  }
2416

2417
  tDecoderInit(pCoder, pReq, len);
62,377✔
2418
  code = tDecodeDeleteRes(pCoder, pRes);
62,375✔
2419
  if (code) goto _err;
62,378!
2420

2421
  if (pRes->affectedRows > 0) {
62,378✔
2422
    for (int32_t iUid = 0; iUid < taosArrayGetSize(pRes->uidList); iUid++) {
117,126✔
2423
      uint64_t uid = *(uint64_t *)taosArrayGet(pRes->uidList, iUid);
59,384✔
2424
      code = tsdbDeleteTableData(pVnode->pTsdb, ver, pRes->suid, uid, pRes->skey, pRes->ekey);
59,384✔
2425
      if (code) goto _err;
59,384!
2426
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, pRes->ctimeMs);
59,384✔
2427
      if (code) goto _err;
59,383!
2428
    }
2429
  }
2430

2431
  code = tdProcessRSmaDelete(pVnode->pSma, ver, pRes, pReq, len);
62,378✔
2432

2433
  tDecoderClear(pCoder);
62,377✔
2434
  taosArrayDestroy(pRes->uidList);
62,378✔
2435

2436
  SVDeleteRsp rsp = {.affectedRows = pRes->affectedRows};
62,379✔
2437
  int32_t     ret = 0;
62,379✔
2438
  tEncodeSize(tEncodeSVDeleteRsp, &rsp, pRsp->contLen, ret);
62,379!
2439
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
62,375✔
2440
  SEncoder ec = {0};
62,377✔
2441
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
62,377✔
2442
  code = tEncodeSVDeleteRsp(&ec, &rsp);
62,377✔
2443
  if (code) goto _err;
62,378!
2444
  tEncoderClear(&ec);
62,378✔
2445
  return code;
62,379✔
2446

2447
_err:
×
2448
  /*
2449
  if(code == TSDB_CODE_SUCCESS){
2450
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_DELETE, pVnode->monitor.strClusterId,
2451
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2452
                                        pOriginalMsg->info.conn.user, "Success"};
2453
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2454
  }
2455
  else{
2456
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_DELETE, pVnode->monitor.strClusterId,
2457
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2458
                                        pOriginalMsg->info.conn.user, "Failed"};
2459
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2460
  }
2461
  */
2462

2463
  return code;
×
2464
}
2465
static int32_t vnodeProcessCreateIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
967✔
2466
  SVCreateStbReq req = {0};
967✔
2467
  SDecoder       dc = {0};
967✔
2468
  int32_t        code = 0;
967✔
2469

2470
  pRsp->msgType = TDMT_VND_CREATE_INDEX_RSP;
967✔
2471
  pRsp->code = TSDB_CODE_SUCCESS;
967✔
2472
  pRsp->pCont = NULL;
967✔
2473
  pRsp->contLen = 0;
967✔
2474

2475
  tDecoderInit(&dc, pReq, len);
967✔
2476
  // decode req
2477
  if (tDecodeSVCreateStbReq(&dc, &req) < 0) {
967!
2478
    tDecoderClear(&dc);
×
2479
    return terrno = TSDB_CODE_INVALID_MSG;
×
2480
  }
2481

2482
  code = metaAddIndexToSuperTable(pVnode->pMeta, ver, &req);
967✔
2483
  if (code) {
967!
2484
    pRsp->code = code;
×
2485
    goto _err;
×
2486
  }
2487
  tDecoderClear(&dc);
967✔
2488
  return 0;
967✔
2489

2490
_err:
×
2491
  tDecoderClear(&dc);
×
2492
  return code;
×
2493
}
2494
static int32_t vnodeProcessDropIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
2,134✔
2495
  SDropIndexReq req = {0};
2,134✔
2496
  int32_t       code = 0;
2,134✔
2497
  pRsp->msgType = TDMT_VND_DROP_INDEX_RSP;
2,134✔
2498
  pRsp->code = TSDB_CODE_SUCCESS;
2,134✔
2499
  pRsp->pCont = NULL;
2,134✔
2500
  pRsp->contLen = 0;
2,134✔
2501

2502
  if ((code = tDeserializeSDropIdxReq(pReq, len, &req))) {
2,134!
2503
    pRsp->code = code;
×
2504
    return code;
×
2505
  }
2506

2507
  code = metaDropIndexFromSuperTable(pVnode->pMeta, ver, &req);
2,134✔
2508
  if (code) {
2,134!
2509
    pRsp->code = code;
×
2510
    return code;
×
2511
  }
2512
  return TSDB_CODE_SUCCESS;
2,134✔
2513
}
2514

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

2517
static int32_t vnodeProcessCompactVnodeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
203✔
2518
  if (!pVnode->restored) {
203✔
2519
    vInfo("vgId:%d, ignore compact req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
173!
2520
    return 0;
173✔
2521
  }
2522
  return vnodeAsyncCompact(pVnode, ver, pReq, len, pRsp);
30✔
2523
}
2524

2525
static int32_t vnodeProcessConfigChangeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
2526
  if (syncCheckMember(pVnode->sync) != 0) {
×
2527
    vError("vgId:%d, failed to check member", TD_VID(pVnode));
×
2528
  }
2529

2530
  pRsp->msgType = TDMT_SYNC_CONFIG_CHANGE_RSP;
×
2531
  pRsp->code = TSDB_CODE_SUCCESS;
×
2532
  pRsp->pCont = NULL;
×
2533
  pRsp->contLen = 0;
×
2534

2535
  return 0;
×
2536
}
2537

2538
static int32_t vnodePreCheckAssignedLogSyncd(SVnode *pVnode, char *member0Token, char *member1Token) {
8✔
2539
  SSyncState syncState = syncGetState(pVnode->sync);
8✔
2540
  if (syncState.state != TAOS_SYNC_STATE_LEADER) {
8✔
2541
    return terrno = TSDB_CODE_SYN_NOT_LEADER;
3✔
2542
  }
2543

2544
  char token[TSDB_ARB_TOKEN_SIZE] = {0};
5✔
2545
  if (vnodeGetArbToken(pVnode, token) != 0) {
5!
2546
    return terrno = TSDB_CODE_NOT_FOUND;
×
2547
  }
2548

2549
  if (strncmp(token, member0Token, TSDB_ARB_TOKEN_SIZE) != 0 &&
5!
2550
      strncmp(token, member1Token, TSDB_ARB_TOKEN_SIZE) != 0) {
5✔
2551
    return terrno = TSDB_CODE_MND_ARB_TOKEN_MISMATCH;
3✔
2552
  }
2553

2554
  terrno = TSDB_CODE_SUCCESS;
2✔
2555
  return 0;
2✔
2556
}
2557

2558
static int32_t vnodeCheckAssignedLogSyncd(SVnode *pVnode, char *member0Token, char *member1Token) {
3✔
2559
  int32_t code = vnodePreCheckAssignedLogSyncd(pVnode, member0Token, member1Token);
3✔
2560
  if (code != 0) {
3✔
2561
    return code;
2✔
2562
  }
2563

2564
  return syncGetAssignedLogSynced(pVnode->sync);
1✔
2565
}
2566

2567
static int32_t vnodeProcessArbCheckSyncReq(SVnode *pVnode, void *pReq, int32_t len, SRpcMsg *pRsp) {
3✔
2568
  int32_t code = 0;
3✔
2569

2570
  SVArbCheckSyncReq syncReq = {0};
3✔
2571

2572
  code = tDeserializeSVArbCheckSyncReq(pReq, len, &syncReq);
3✔
2573
  if (code) {
3!
2574
    return terrno = code;
×
2575
  }
2576

2577
  pRsp->msgType = TDMT_VND_ARB_CHECK_SYNC_RSP;
3✔
2578
  pRsp->code = TSDB_CODE_SUCCESS;
3✔
2579
  pRsp->pCont = NULL;
3✔
2580
  pRsp->contLen = 0;
3✔
2581

2582
  SVArbCheckSyncRsp syncRsp = {0};
3✔
2583
  syncRsp.arbToken = syncReq.arbToken;
3✔
2584
  syncRsp.member0Token = syncReq.member0Token;
3✔
2585
  syncRsp.member1Token = syncReq.member1Token;
3✔
2586
  syncRsp.vgId = TD_VID(pVnode);
3✔
2587

2588
  if (vnodeCheckAssignedLogSyncd(pVnode, syncReq.member0Token, syncReq.member1Token) != 0) {
3✔
2589
    vError("vgId:%d, failed to check assigned log syncd", TD_VID(pVnode));
2!
2590
  }
2591
  syncRsp.errCode = terrno;
3✔
2592

2593
  if (vnodeUpdateArbTerm(pVnode, syncReq.arbTerm) != 0) {
3!
2594
    vError("vgId:%d, failed to update arb term", TD_VID(pVnode));
×
2595
    code = -1;
×
2596
    goto _OVER;
×
2597
  }
2598

2599
  int32_t contLen = tSerializeSVArbCheckSyncRsp(NULL, 0, &syncRsp);
3✔
2600
  if (contLen <= 0) {
3!
2601
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2602
    code = -1;
×
2603
    goto _OVER;
×
2604
  }
2605
  void *pHead = rpcMallocCont(contLen);
3✔
2606
  if (!pHead) {
3!
2607
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2608
    code = -1;
×
2609
    goto _OVER;
×
2610
  }
2611

2612
  if (tSerializeSVArbCheckSyncRsp(pHead, contLen, &syncRsp) <= 0) {
3!
2613
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2614
    rpcFreeCont(pHead);
×
2615
    code = -1;
×
2616
    goto _OVER;
×
2617
  }
2618

2619
  pRsp->pCont = pHead;
3✔
2620
  pRsp->contLen = contLen;
3✔
2621

2622
  terrno = TSDB_CODE_SUCCESS;
3✔
2623

2624
_OVER:
3✔
2625
  tFreeSVArbCheckSyncReq(&syncReq);
3✔
2626
  return code;
3✔
2627
}
2628

2629
#ifndef TD_ENTERPRISE
2630
int32_t vnodeAsyncCompact(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) { return 0; }
2631
int32_t tsdbAsyncCompact(STsdb *tsdb, const STimeWindow *tw, bool sync) { return 0; }
2632
#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