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

taosdata / TDengine / #3613

14 Feb 2025 09:14AM UTC coverage: 63.499% (-0.01%) from 63.513%
#3613

push

travis-ci

web-flow
Merge pull request #29781 from taosdata/doc/internal

docs: minor changes

141396 of 286269 branches covered (49.39%)

Branch coverage included in aggregate %.

220278 of 283307 relevant lines covered (77.75%)

19138445.45 hits per line

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

61.64
/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 vnodeCheckToken(SVnode *pVnode, char *member0Token, char *member1Token);
57
static int32_t vnodeCheckSyncd(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) {
152,481✔
64
  int32_t code = 0;
152,481✔
65
  int32_t lino = 0;
152,481✔
66

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

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

78
  // name
79
  char *name = NULL;
152,516✔
80
  if (tDecodeCStr(pCoder, &name) < 0) {
152,485!
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);
152,485✔
87
  if (uid == 0) {
152,496✔
88
    uid = tGenIdPI64();
126,001✔
89
  }
90
  *(int64_t *)(pCoder->data + pCoder->pos) = uid;
152,513✔
91

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

95
  tEndDecode(pCoder);
152,513✔
96

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

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

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

120
  if (tDecodeI32v(&dc, &nReqs) < 0) {
66,574!
121
    code = TSDB_CODE_INVALID_MSG;
×
122
    TSDB_CHECK_CODE(code, lino, _exit);
×
123
  }
124
  for (int32_t iReq = 0; iReq < nReqs; iReq++) {
157,156✔
125
    code = vnodePreprocessCreateTableReq(pVnode, &dc, btime, NULL);
90,594✔
126
    TSDB_CHECK_CODE(code, lino, _exit);
90,582!
127
  }
128

129
  tEndDecode(&dc);
66,562✔
130

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

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

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

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

157
  code = 0;
979✔
158

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

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

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

177
  SArray *tbUids = NULL;
91,203✔
178
  int64_t timestampMs = 0;
91,203✔
179

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

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

200
    ttlReq.nUids = taosArrayGetSize(tbUids);
91,383✔
201
    ttlReq.pTbUids = tbUids;
91,295✔
202
  }
203

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

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

242
  return code;
91,384✔
243
}
244

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

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

264
  int64_t uid;
265
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
10,743,637✔
266
    code = vnodePreprocessCreateTableReq(pVnode, pCoder, btimeMs, &uid);
61,898✔
267
    TSDB_CHECK_CODE(code, lino, _exit);
61,894!
268
  }
269

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

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

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

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

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

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

332
    for (int32_t iRow = 0; iRow < colData.nVal; iRow++) {
359✔
333
      if (((TSKEY *)colData.pData)[iRow] < minKey || ((TSKEY *)colData.pData)[iRow] > maxKey) {
225!
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++) {
583✔
340
      code = tDecodeColData(version, pCoder, &colData);
449✔
341
      if (code) {
449!
342
        code = TSDB_CODE_INVALID_MSG;
×
343
        goto _exit;
×
344
      }
345
    }
346
  } else {
347
    uint64_t nRow;
348
    if (tDecodeU64v(pCoder, &nRow) < 0) {
10,723,446!
349
      code = TSDB_CODE_INVALID_MSG;
×
350
      goto _exit;
496✔
351
    }
352

353
    for (int32_t iRow = 0; iRow < nRow; ++iRow) {
689,978,921✔
354
      SRow *pRow = (SRow *)(pCoder->data + pCoder->pos);
679,255,971✔
355
      pCoder->pos += pRow->len;
679,255,971✔
356

357
      if (pRow->ts < minKey || pRow->ts > maxKey) {
679,255,971!
358
        code = TSDB_CODE_TDB_TIMESTAMP_OUT_OF_RANGE;
496✔
359
        goto _exit;
496✔
360
      }
361
    }
362
  }
363

364
  if (!tDecodeIsEnd(pCoder)) {
10,723,084!
365
    *(int64_t *)(pCoder->data + pCoder->pos) = ctimeMs;
10,729,522✔
366
    pCoder->pos += sizeof(int64_t);
10,729,522✔
367
  }
368

369
  tEndDecode(pCoder);
10,723,084✔
370

371
_exit:
10,726,070✔
372
  return code;
10,726,070✔
373
}
374
static int32_t vnodePreProcessSubmitMsg(SVnode *pVnode, SRpcMsg *pMsg) {
10,052,961✔
375
  int32_t code = 0;
10,052,961✔
376
  int32_t lino = 0;
10,052,961✔
377

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

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

384
  if (taosHton64(((SSubmitReq2Msg *)pMsg->pCont)->version) != 1) {
10,052,961!
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,053,014✔
390

391
  if (tStartDecode(pCoder) < 0) {
10,052,957!
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,053,006!
398
    code = TSDB_CODE_INVALID_MSG;
×
399
    TSDB_CHECK_CODE(code, lino, _exit);
×
400
  }
401

402
  int64_t btimeMs = taosGetTimestampMs();
10,053,074✔
403
  int64_t ctimeMs = btimeMs;
10,053,074✔
404
  for (int32_t i = 0; i < nSubmitTbData; i++) {
20,778,490✔
405
    code = vnodePreProcessSubmitTbData(pVnode, pCoder, btimeMs, ctimeMs);
10,726,574✔
406
    TSDB_CHECK_CODE(code, lino, _exit);
10,725,913✔
407
  }
408

409
  tEndDecode(pCoder);
10,051,916✔
410

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

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

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

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

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

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

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

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

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

453
  taosArrayDestroy(res.uidList);
57,275✔
454

455
_exit:
57,275✔
456
  return code;
57,275✔
457
}
458

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

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

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

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

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

476
  if (code) {
2,702!
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,702✔
480
  }
481
  return code;
2,702✔
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 = vnodeCheckToken(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,492✔
504
  int32_t          code = TSDB_CODE_SUCCESS;
2,492✔
505
  int32_t          lino = 0;
2,492✔
506
  int32_t          size = 0;
2,492✔
507
  SDecoder         dc = {0};
2,492✔
508
  SEncoder         ec = {0};
2,492✔
509
  SVDropTbBatchReq receivedBatchReqs = {0};
2,492✔
510
  SVDropTbBatchReq sentBatchReqs = {0};
2,492✔
511

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

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

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

541
  tEncodeSize(tEncodeSVDropTbBatchReq, &sentBatchReqs, size, code);
2,492!
542
  tEncoderInit(&ec, POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), size);
2,492✔
543
  code = tEncodeSVDropTbBatchReq(&ec, &sentBatchReqs);
2,492✔
544
  tEncoderClear(&ec);
2,492✔
545
  if (code != TSDB_CODE_SUCCESS) {
2,492!
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,492✔
551
  tDecoderClear(&dc);
2,492✔
552
  if (sentBatchReqs.pArray) {
2,492!
553
    taosArrayDestroy(sentBatchReqs.pArray);
2,492✔
554
  }
555
  return code;
2,492✔
556
}
557

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

561
  switch (pMsg->msgType) {
10,377,885✔
562
    case TDMT_VND_CREATE_TABLE: {
66,583✔
563
      code = vnodePreProcessCreateTableMsg(pVnode, pMsg);
66,583✔
564
    } break;
66,572✔
565
    case TDMT_VND_ALTER_TABLE: {
979✔
566
      code = vnodePreProcessAlterTableMsg(pVnode, pMsg);
979✔
567
    } break;
979✔
568
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
91,587✔
569
    case TDMT_VND_DROP_TTL_TABLE: {
570
      code = vnodePreProcessDropTtlMsg(pVnode, pMsg);
91,587✔
571
    } break;
91,328✔
572
    case TDMT_VND_SUBMIT: {
10,053,034✔
573
      code = vnodePreProcessSubmitMsg(pVnode, pMsg);
10,053,034✔
574
    } break;
10,052,963✔
575
    case TDMT_VND_DELETE: {
57,275✔
576
      code = vnodePreProcessDeleteMsg(pVnode, pMsg);
57,275✔
577
    } break;
57,273✔
578
    case TDMT_VND_BATCH_DEL: {
2,702✔
579
      code = vnodePreProcessBatchDeleteMsg(pVnode, pMsg);
2,702✔
580
    } break;
2,702✔
581
    case TDMT_VND_ARB_CHECK_SYNC: {
5✔
582
      code = vnodePreProcessArbCheckSyncMsg(pVnode, pMsg);
5✔
583
    } break;
5✔
584
    case TDMT_VND_DROP_TABLE: {
2,492✔
585
      code = vnodePreProcessDropTbMsg(pVnode, pMsg);
2,492✔
586
    } break;
2,492✔
587
    default:
103,228✔
588
      break;
103,228✔
589
  }
590

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

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

604
  (void)taosThreadMutexLock(&pVnode->mutex);
13,375,682✔
605
  if (pVnode->disableWrite) {
13,376,104!
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,376,104✔
611

612
  if (ver <= pVnode->state.applied) {
13,376,109!
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,376,109!
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,375,828!
624
    return terrno = TSDB_CODE_INTERNAL_ERROR;
×
625
  }
626

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

631
  atomic_store_64(&pVnode->state.applied, ver);
13,375,828✔
632
  atomic_store_64(&pVnode->state.applyTerm, pMsg->info.conn.applyTerm);
13,376,132✔
633

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

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

641
  switch (pMsg->msgType) {
13,350,549!
642
    /* META */
643
    case TDMT_VND_CREATE_STB:
25,837✔
644
      if (vnodeProcessCreateStbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
25,837✔
645
      break;
25,931✔
646
    case TDMT_VND_ALTER_STB:
6,421✔
647
      if (vnodeProcessAlterStbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
6,421!
648
      break;
6,424✔
649
    case TDMT_VND_DROP_STB:
2,302✔
650
      if (vnodeProcessDropStbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
2,302!
651
      break;
2,325✔
652
    case TDMT_VND_CREATE_TABLE:
75,580✔
653
      if (vnodeProcessCreateTbReq(pVnode, ver, pReq, len, pRsp, pMsg) < 0) goto _err;
75,580!
654
      break;
75,605✔
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,534✔
659
      if (vnodeProcessDropTbReq(pVnode, ver, pReq, len, pRsp, pMsg) < 0) goto _err;
2,534!
660
      break;
2,534✔
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:
201✔
668
      if (vnodeProcessTrimReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
201!
669
      break;
201✔
670
    case TDMT_VND_S3MIGRATE:
×
671
      if (vnodeProcessS3MigrateReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
672
      break;
×
673
    case TDMT_VND_CREATE_SMA:
28✔
674
      if (vnodeProcessCreateTSmaReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
28!
675
      break;
28✔
676
    /* TSDB */
677
    case TDMT_VND_SUBMIT:
13,085,500✔
678
      if (vnodeProcessSubmitReq(pVnode, ver, pMsg->pCont, pMsg->contLen, pRsp, pMsg) < 0) goto _err;
13,085,500✔
679
      break;
13,085,455✔
680
    case TDMT_VND_DELETE:
58,582✔
681
      if (vnodeProcessDeleteReq(pVnode, ver, pReq, len, pRsp, pMsg) < 0) goto _err;
58,582!
682
      break;
58,585✔
683
    case TDMT_VND_BATCH_DEL:
3,012✔
684
      if (vnodeProcessBatchDeleteReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
3,012!
685
      break;
3,013✔
686
    /* TQ */
687
    case TDMT_VND_TMQ_SUBSCRIBE:
3,116✔
688
      if (tqProcessSubscribeReq(pVnode->pTq, ver, pReq, len) < 0) {
3,116✔
689
        goto _err;
1✔
690
      }
691
      break;
3,121✔
692
    case TDMT_VND_TMQ_DELETE_SUB:
861✔
693
      if (tqProcessDeleteSubReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen) < 0) {
861!
694
        goto _err;
×
695
      }
696
      break;
864✔
697
    case TDMT_VND_TMQ_COMMIT_OFFSET:
9,710✔
698
      if (tqProcessOffsetCommitReq(pVnode->pTq, ver, pReq, len) < 0) {
9,710!
699
        goto _err;
×
700
      }
701
      break;
9,721✔
702
    case TDMT_VND_TMQ_ADD_CHECKINFO:
124✔
703
      if (tqProcessAddCheckInfoReq(pVnode->pTq, ver, pReq, len) < 0) {
124!
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,246✔
713
      if ((code = tqProcessTaskDeployReq(pVnode->pTq, ver, pReq, len)) != TSDB_CODE_SUCCESS) {
14,246!
714
        goto _err;
×
715
      }
716
    } break;
14,270✔
717
    case TDMT_STREAM_TASK_DROP: {
6,956✔
718
      if ((code = tqProcessTaskDropReq(pVnode->pTq, pMsg->pCont, pMsg->contLen)) < 0) {
6,956!
719
        goto _err;
×
720
      }
721
    } break;
6,986✔
722
    case TDMT_STREAM_TASK_UPDATE_CHKPT: {
5,171✔
723
      if ((code = tqProcessTaskUpdateCheckpointReq(pVnode->pTq, pMsg->pCont, pMsg->contLen)) < 0) {
5,171!
724
        goto _err;
×
725
      }
726
    } break;
5,181✔
727
    case TDMT_STREAM_CONSEN_CHKPT: {
221✔
728
      if (pVnode->restored && (code = tqProcessTaskConsenChkptIdReq(pVnode->pTq, pMsg)) < 0) {
221!
729
        goto _err;
×
730
      }
731

732
    } break;
221✔
733
    case TDMT_STREAM_TASK_PAUSE: {
2,092✔
734
      if (pVnode->restored && vnodeIsLeader(pVnode) &&
3,481!
735
          (code = tqProcessTaskPauseReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen)) < 0) {
1,387✔
736
        goto _err;
×
737
      }
738
    } break;
2,093✔
739
    case TDMT_STREAM_TASK_RESUME: {
3,766✔
740
      if (pVnode->restored && vnodeIsLeader(pVnode) &&
6,322!
741
          (code = tqProcessTaskResumeReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen)) < 0) {
2,558✔
742
        goto _err;
×
743
      }
744
    } break;
3,764✔
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:
631✔
758
      vnodeProcessAlterConfigReq(pVnode, ver, pReq, len, pRsp);
631✔
759
      break;
633✔
760
    case TDMT_VND_COMMIT:
28,014✔
761
      needCommit = true;
28,014✔
762
      break;
28,014✔
763
    case TDMT_VND_CREATE_INDEX:
993✔
764
      vnodeProcessCreateIndexReq(pVnode, ver, pReq, len, pRsp);
993✔
765
      break;
993✔
766
    case TDMT_VND_DROP_INDEX:
2,168✔
767
      vnodeProcessDropIndexReq(pVnode, ver, pReq, len, pRsp);
2,168✔
768
      break;
2,168✔
769
    case TDMT_VND_STREAM_CHECK_POINT_SOURCE:  // always return true
3,139✔
770
      tqProcessTaskCheckPointSourceReq(pVnode->pTq, pMsg, pRsp);
3,139✔
771
      break;
3,143✔
772
    case TDMT_VND_STREAM_TASK_UPDATE:  // always return true
68✔
773
      tqProcessTaskUpdateReq(pVnode->pTq, pMsg);
68✔
774
      break;
68✔
775
    case TDMT_VND_COMPACT:
203✔
776
      vnodeProcessCompactVnodeReq(pVnode, ver, pReq, len, pRsp);
203✔
777
      goto _exit;
203✔
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:
4✔
788
      vnodeProcessArbCheckSyncReq(pVnode, pReq, len, pRsp);
4✔
789
      break;
4✔
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,350,536!
796
         ver);
797

798
  walApplyVer(pVnode->pWal, ver);
13,350,537✔
799

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

806
  // commit if need
807
  if (needCommit) {
13,350,063✔
808
    vInfo("vgId:%d, commit at version %" PRId64, TD_VID(pVnode), ver);
27,503✔
809
    code = vnodeAsyncCommit(pVnode);
27,522✔
810
    if (code) {
27,523!
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);
27,523✔
817
    if (code) {
27,519✔
818
      vError("vgId:%d, failed to begin vnode since %s.", TD_VID(pVnode), tstrerror(terrno));
37!
819
      goto _err;
×
820
    }
821
  }
822

823
_exit:
13,350,042✔
824
  return 0;
13,375,224✔
825

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

832
int32_t vnodePreprocessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg) {
8,137,353✔
833
  if (TDMT_SCH_QUERY != pMsg->msgType && TDMT_SCH_MERGE_QUERY != pMsg->msgType) {
8,137,353✔
834
    return 0;
525,360✔
835
  }
836

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

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

847
  if (pMsg->msgType == TDMT_VND_TMQ_CONSUME && !pVnode->restored) {
8,145,294!
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,145,294✔
853
  initStorageAPI(&handle.api);
8,145,294✔
854
  int32_t code = TSDB_CODE_SUCCESS;
8,145,436✔
855
  bool    redirected = false;
8,145,436✔
856

857
  switch (pMsg->msgType) {
8,145,436✔
858
    case TDMT_SCH_QUERY:
5,818,610✔
859
      if (!syncIsReadyForRead(pVnode->sync)) {
5,818,610✔
860
        pMsg->code = (terrno) ? terrno : TSDB_CODE_SYN_NOT_LEADER;
2,768!
861
        redirected = true;
2,768✔
862
      }
863
      code = qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
5,818,393✔
864
      if (redirected) {
5,816,772✔
865
        vnodeRedirectRpcMsg(pVnode, pMsg, pMsg->code);
1,552✔
866
        return 0;
2,768✔
867
      }
868
      return code;
5,815,220✔
869
    case TDMT_SCH_MERGE_QUERY:
1,802,514✔
870
      return qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
1,802,514✔
871
    case TDMT_SCH_QUERY_CONTINUE:
441,704✔
872
      return qWorkerProcessCQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
441,704✔
873
    case TDMT_VND_TMQ_CONSUME:
62,404✔
874
      return tqProcessPollReq(pVnode->pTq, pMsg);
62,404✔
875
    case TDMT_VND_TMQ_CONSUME_PUSH:
19,806✔
876
      return tqProcessPollPush(pVnode->pTq, pMsg);
19,806✔
877
    default:
398✔
878
      vError("unknown msg type:%d in query queue", pMsg->msgType);
398!
879
      return TSDB_CODE_APP_ERROR;
×
880
  }
881
}
882

883
int32_t vnodeProcessFetchMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
20,054,147✔
884
  vTrace("vgId:%d, msg:%p in fetch queue is processing", pVnode->config.vgId, pMsg);
20,054,147✔
885
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
20,054,148!
886
       pMsg->msgType == TDMT_VND_BATCH_META || pMsg->msgType == TDMT_VND_TABLE_NAME) &&
13,873,445✔
887
      !syncIsReadyForRead(pVnode->sync)) {
7,330,276✔
888
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
5,826✔
889
    return 0;
5,826✔
890
  }
891

892
  switch (pMsg->msgType) {
20,051,256!
893
    case TDMT_SCH_FETCH:
9,502,015✔
894
    case TDMT_SCH_MERGE_FETCH:
895
      return qWorkerProcessFetchMsg(pVnode, pVnode->pQuery, pMsg, 0);
9,502,015✔
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,613,009✔
901
      return qWorkerProcessDropMsg(pVnode, pVnode->pQuery, pMsg, 0);
7,613,009✔
902
    case TDMT_SCH_TASK_NOTIFY:
49✔
903
      return qWorkerProcessNotifyMsg(pVnode, pVnode->pQuery, pMsg, 0);
49✔
904
    case TDMT_SCH_QUERY_HEARTBEAT:
1,794,156✔
905
      return qWorkerProcessHbMsg(pVnode, pVnode->pQuery, pMsg, 0);
1,794,156✔
906
    case TDMT_VND_TABLE_META:
1,781✔
907
    case TDMT_VND_TABLE_NAME:
908
      return vnodeGetTableMeta(pVnode, pMsg, true);
1,781✔
909
    case TDMT_VND_TABLE_CFG:
×
910
      return vnodeGetTableCfg(pVnode, pMsg, true);
×
911
    case TDMT_VND_BATCH_META:
1,140,155✔
912
      return vnodeGetBatchMeta(pVnode, pMsg);
1,140,155✔
913
#ifdef TD_ENTERPRISE
914
    case TDMT_VND_QUERY_COMPACT_PROGRESS:
46✔
915
      return vnodeQueryCompactProgress(pVnode, pMsg);
46✔
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:
6✔
922
      return tqProcessVgCommittedInfoReq(pVnode->pTq, pMsg);
6✔
923
    case TDMT_VND_TMQ_SEEK:
26✔
924
      return tqProcessSeekReq(pVnode->pTq, pMsg);
26✔
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) {
197,890✔
933
  vTrace("vgId:%d, msg:%p in stream queue is processing", pVnode->config.vgId, pMsg);
197,890✔
934
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
197,890!
935
       pMsg->msgType == TDMT_VND_BATCH_META) &&
197,943!
936
      !syncIsReadyForRead(pVnode->sync)) {
×
937
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
×
938
    return 0;
×
939
  }
940

941
  switch (pMsg->msgType) {
197,952!
942
    case TDMT_STREAM_TASK_RUN:
169,030✔
943
      return tqProcessTaskRunReq(pVnode->pTq, pMsg);
169,030✔
944
    case TDMT_STREAM_RETRIEVE:
573✔
945
      return tqProcessTaskRetrieveReq(pVnode->pTq, pMsg);
573✔
946
    case TDMT_STREAM_RETRIEVE_RSP:
450✔
947
      return tqProcessTaskRetrieveRsp(pVnode->pTq, pMsg);
450✔
948
    case TDMT_VND_STREAM_SCAN_HISTORY:
2,602✔
949
      return tqProcessTaskScanHistory(pVnode->pTq, pMsg);
2,602✔
950
    case TDMT_STREAM_TASK_CHECKPOINT_READY:
7,552✔
951
      return tqProcessTaskCheckpointReadyMsg(pVnode->pTq, pMsg);
7,552✔
952
    case TDMT_STREAM_TASK_CHECKPOINT_READY_RSP:
7,550✔
953
      return tqProcessTaskCheckpointReadyRsp(pVnode->pTq, pMsg);
7,550✔
954
    case TDMT_STREAM_RETRIEVE_TRIGGER:
×
955
      return tqProcessTaskRetrieveTriggerReq(pVnode->pTq, pMsg);
×
956
    case TDMT_STREAM_RETRIEVE_TRIGGER_RSP:
×
957
      return tqProcessTaskRetrieveTriggerRsp(pVnode->pTq, pMsg);
×
958
    case TDMT_MND_STREAM_REQ_CHKPT_RSP:
4,354✔
959
      return tqProcessStreamReqCheckpointRsp(pVnode->pTq, pMsg);
4,354✔
960
    case TDMT_VND_GET_STREAM_PROGRESS:
×
961
      return tqStreamProgressRetrieveReq(pVnode->pTq, pMsg);
×
962
    case TDMT_MND_STREAM_CHKPT_REPORT_RSP:
5,841✔
963
      return tqProcessTaskChkptReportRsp(pVnode->pTq, pMsg);
5,841✔
964
    default:
×
965
      vError("unknown msg type:%d in stream queue", pMsg->msgType);
×
966
      return TSDB_CODE_APP_ERROR;
×
967
  }
968
}
969

970
int32_t vnodeProcessStreamCtrlMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
183,281✔
971
  vTrace("vgId:%d, msg:%p in stream ctrl queue is processing", pVnode->config.vgId, pMsg);
183,281✔
972
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
183,281!
973
       pMsg->msgType == TDMT_VND_BATCH_META) &&
183,281!
974
      !syncIsReadyForRead(pVnode->sync)) {
×
975
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
×
976
    return 0;
×
977
  }
978

979
  switch (pMsg->msgType) {
183,281!
980
    case TDMT_MND_STREAM_HEARTBEAT_RSP:
23,456✔
981
      return tqProcessStreamHbRsp(pVnode->pTq, pMsg);
23,456✔
982
    case TDMT_STREAM_TASK_DISPATCH:
57,321✔
983
      return tqProcessTaskDispatchReq(pVnode->pTq, pMsg);
57,321✔
984
    case TDMT_STREAM_TASK_DISPATCH_RSP:
57,287✔
985
      return tqProcessTaskDispatchRsp(pVnode->pTq, pMsg);
57,287✔
986
    case TDMT_VND_STREAM_TASK_CHECK:
22,344✔
987
      return tqProcessTaskCheckReq(pVnode->pTq, pMsg);
22,344✔
988
    case TDMT_VND_STREAM_TASK_CHECK_RSP:
22,873✔
989
      return tqProcessTaskCheckRsp(pVnode->pTq, pMsg);
22,873✔
990
    default:
×
991
      vError("unknown msg type:%d in stream ctrl queue", pMsg->msgType);
×
992
      return TSDB_CODE_APP_ERROR;
×
993
  }
994
}
995

996
void smaHandleRes(void *pVnode, int64_t smaId, const SArray *data) {
1✔
997
  int32_t code = tdProcessTSmaInsert(((SVnode *)pVnode)->pSma, smaId, (const char *)data);
1✔
998
  if (code) {
1!
999
    vError("failed to process sma result since %s", tstrerror(code));
×
1000
  }
1001
}
1✔
1002

1003
void vnodeUpdateMetaRsp(SVnode *pVnode, STableMetaRsp *pMetaRsp) {
157,203✔
1004
  if (NULL == pMetaRsp) {
157,203!
1005
    return;
×
1006
  }
1007

1008
  tstrncpy(pMetaRsp->dbFName, pVnode->config.dbname, TSDB_DB_FNAME_LEN);
157,203✔
1009
  pMetaRsp->dbId = pVnode->config.dbId;
157,203✔
1010
  pMetaRsp->vgId = TD_VID(pVnode);
157,203✔
1011
  pMetaRsp->precision = pVnode->config.tsdbCfg.precision;
157,203✔
1012
}
1013

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

1016
static int32_t vnodeProcessTrimReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
201✔
1017
  if (!pVnode->restored) {
201✔
1018
    vInfo("vgId:%d, ignore trim req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
7!
1019
    return 0;
7✔
1020
  }
1021

1022
  int32_t     code = 0;
194✔
1023
  SVTrimDbReq trimReq = {0};
194✔
1024

1025
  // decode
1026
  if (tDeserializeSVTrimDbReq(pReq, len, &trimReq) != 0) {
194!
1027
    code = TSDB_CODE_INVALID_MSG;
×
1028
    goto _exit;
×
1029
  }
1030

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

1033
  code = vnodeAsyncRetention(pVnode, trimReq.timestamp);
194✔
1034

1035
_exit:
194✔
1036
  return code;
194✔
1037
}
1038

1039
extern int32_t vnodeAsyncS3Migrate(SVnode *pVnode, int64_t now);
1040

1041
static int32_t vnodeProcessS3MigrateReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1042
  int32_t          code = 0;
×
1043
  SVS3MigrateDbReq s3migrateReq = {0};
×
1044

1045
  // decode
1046
  if (tDeserializeSVS3MigrateDbReq(pReq, len, &s3migrateReq) != 0) {
×
1047
    code = TSDB_CODE_INVALID_MSG;
×
1048
    goto _exit;
×
1049
  }
1050

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

1053
  code = vnodeAsyncS3Migrate(pVnode, s3migrateReq.timestamp);
×
1054

1055
_exit:
×
1056
  return code;
×
1057
}
1058

1059
static int32_t vnodeProcessDropTtlTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1060
  int               ret = 0;
×
1061
  SVDropTtlTableReq ttlReq = {0};
×
1062
  if (tDeserializeSVDropTtlTableReq(pReq, len, &ttlReq) != 0) {
×
1063
    ret = TSDB_CODE_INVALID_MSG;
×
1064
    goto end;
×
1065
  }
1066

1067
  if (ttlReq.nUids != taosArrayGetSize(ttlReq.pTbUids)) {
×
1068
    ret = TSDB_CODE_INVALID_MSG;
×
1069
    goto end;
×
1070
  }
1071

1072
  if (ttlReq.nUids != 0) {
×
1073
    vInfo("vgId:%d, drop ttl table req will be processed, time:%d, ntbUids:%d", pVnode->config.vgId,
×
1074
          ttlReq.timestampSec, ttlReq.nUids);
1075
  }
1076

1077
  if (ttlReq.nUids > 0) {
×
1078
    int32_t code = metaDropMultipleTables(pVnode->pMeta, ver, ttlReq.pTbUids);
×
1079
    if (code) return code;
×
1080

1081
    code = tqUpdateTbUidList(pVnode->pTq, ttlReq.pTbUids, false);
×
1082
    if (code) {
×
1083
      vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1084
    }
1085
  }
1086

1087
end:
×
1088
  taosArrayDestroy(ttlReq.pTbUids);
×
1089
  return ret;
×
1090
}
1091

1092
static int32_t vnodeProcessFetchTtlExpiredTbs(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
5✔
1093
  int32_t                 code = -1;
5✔
1094
  SMetaReader             mr = {0};
5✔
1095
  SVDropTtlTableReq       ttlReq = {0};
5✔
1096
  SVFetchTtlExpiredTbsRsp rsp = {0};
5✔
1097
  SEncoder                encoder = {0};
5✔
1098
  SArray                 *pNames = NULL;
5✔
1099
  pRsp->msgType = TDMT_VND_FETCH_TTL_EXPIRED_TBS_RSP;
5✔
1100
  pRsp->code = TSDB_CODE_SUCCESS;
5✔
1101
  pRsp->pCont = NULL;
5✔
1102
  pRsp->contLen = 0;
5✔
1103

1104
  if (tDeserializeSVDropTtlTableReq(pReq, len, &ttlReq) != 0) {
5!
1105
    terrno = TSDB_CODE_INVALID_MSG;
×
1106
    goto _end;
×
1107
  }
1108

1109
  if (!(ttlReq.nUids == taosArrayGetSize(ttlReq.pTbUids))) {
5!
1110
    terrno = TSDB_CODE_INVALID_MSG;
×
1111
    goto _end;
×
1112
  }
1113

1114
  tb_uid_t    suid;
1115
  char        ctbName[TSDB_TABLE_NAME_LEN];
1116
  SVDropTbReq expiredTb = {.igNotExists = true};
5✔
1117
  metaReaderDoInit(&mr, pVnode->pMeta, 0);
5✔
1118
  rsp.vgId = TD_VID(pVnode);
5✔
1119
  rsp.pExpiredTbs = taosArrayInit(ttlReq.nUids, sizeof(SVDropTbReq));
5✔
1120
  if (!rsp.pExpiredTbs) goto _end;
5!
1121

1122
  pNames = taosArrayInit(ttlReq.nUids, TSDB_TABLE_NAME_LEN);
5✔
1123
  if (!pNames) {
5!
1124
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1125
    goto _end;
×
1126
  }
1127
  char buf[TSDB_TABLE_NAME_LEN];
1128
  for (int32_t i = 0; i < ttlReq.nUids; ++i) {
10✔
1129
    tb_uid_t *uid = taosArrayGet(ttlReq.pTbUids, i);
5✔
1130
    expiredTb.suid = *uid;
5✔
1131
    terrno = metaReaderGetTableEntryByUid(&mr, *uid);
5✔
1132
    if (terrno < 0) goto _end;
5!
1133
    tstrncpy(buf, mr.me.name, TSDB_TABLE_NAME_LEN);
5✔
1134
    void *p = taosArrayPush(pNames, buf);
5✔
1135
    if (p == NULL) {
5!
1136
      goto _end;
×
1137
    }
1138

1139
    expiredTb.name = p;
5✔
1140
    if (mr.me.type == TSDB_CHILD_TABLE) {
5!
1141
      expiredTb.suid = mr.me.ctbEntry.suid;
5✔
1142
    }
1143

1144
    if (taosArrayPush(rsp.pExpiredTbs, &expiredTb) == NULL) {
10!
1145
      goto _end;
×
1146
    }
1147
  }
1148

1149
  int32_t ret = 0;
5✔
1150
  tEncodeSize(tEncodeVFetchTtlExpiredTbsRsp, &rsp, pRsp->contLen, ret);
5!
1151
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
5✔
1152
  if (pRsp->pCont == NULL) {
5!
1153
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1154
    code = -1;
×
1155
    goto _end;
×
1156
  }
1157
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
5✔
1158
  terrno = tEncodeVFetchTtlExpiredTbsRsp(&encoder, &rsp);
5✔
1159
  tEncoderClear(&encoder);
5✔
1160

1161
  if (terrno == 0) code = 0;
5!
1162
_end:
×
1163
  metaReaderClear(&mr);
5✔
1164
  tFreeFetchTtlExpiredTbsRsp(&rsp);
5✔
1165
  taosArrayDestroy(ttlReq.pTbUids);
5✔
1166
  if (pNames) taosArrayDestroy(pNames);
5!
1167
  pRsp->code = terrno;
5✔
1168
  return code;
5✔
1169
}
1170

1171
static int32_t vnodeProcessCreateStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
25,840✔
1172
  int32_t        code = 0;
25,840✔
1173
  SVCreateStbReq req = {0};
25,840✔
1174
  SDecoder       coder;
1175

1176
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
25,840✔
1177
  pRsp->code = TSDB_CODE_SUCCESS;
25,840✔
1178
  pRsp->pCont = NULL;
25,840✔
1179
  pRsp->contLen = 0;
25,840✔
1180

1181
  // decode and process req
1182
  tDecoderInit(&coder, pReq, len);
25,840✔
1183

1184
  code = tDecodeSVCreateStbReq(&coder, &req);
25,820✔
1185
  if (code) {
25,869!
1186
    pRsp->code = code;
×
1187
    goto _err;
×
1188
  }
1189

1190
  code = metaCreateSuperTable(pVnode->pMeta, ver, &req);
25,869✔
1191
  if (code) {
25,956✔
1192
    pRsp->code = code;
4✔
1193
    goto _err;
4✔
1194
  }
1195

1196
  if ((code = tdProcessRSmaCreate(pVnode->pSma, &req)) < 0) {
25,952!
1197
    pRsp->code = code;
×
1198
    goto _err;
×
1199
  }
1200

1201
  tDecoderClear(&coder);
25,942✔
1202
  return 0;
25,912✔
1203

1204
_err:
4✔
1205
  tDecoderClear(&coder);
4✔
1206
  return code;
4✔
1207
}
1208

1209
static int32_t vnodeProcessCreateTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
75,578✔
1210
                                       SRpcMsg *pOriginRpc) {
1211
  SDecoder           decoder = {0};
75,578✔
1212
  SEncoder           encoder = {0};
75,578✔
1213
  int32_t            rcode = 0;
75,578✔
1214
  SVCreateTbBatchReq req = {0};
75,578✔
1215
  SVCreateTbReq     *pCreateReq;
1216
  SVCreateTbBatchRsp rsp = {0};
75,578✔
1217
  SVCreateTbRsp      cRsp = {0};
75,578✔
1218
  char               tbName[TSDB_TABLE_FNAME_LEN];
1219
  STbUidStore       *pStore = NULL;
75,578✔
1220
  SArray            *tbUids = NULL;
75,578✔
1221
  SArray            *tbNames = NULL;
75,578✔
1222
  pRsp->msgType = TDMT_VND_CREATE_TABLE_RSP;
75,578✔
1223
  pRsp->code = TSDB_CODE_SUCCESS;
75,578✔
1224
  pRsp->pCont = NULL;
75,578✔
1225
  pRsp->contLen = 0;
75,578✔
1226

1227
  // decode
1228
  tDecoderInit(&decoder, pReq, len);
75,578✔
1229
  if (tDecodeSVCreateTbBatchReq(&decoder, &req) < 0) {
75,576!
1230
    rcode = -1;
×
1231
    terrno = TSDB_CODE_INVALID_MSG;
×
1232
    goto _exit;
×
1233
  }
1234

1235
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(cRsp));
75,589✔
1236
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
75,602✔
1237
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
75,603✔
1238
  if (rsp.pArray == NULL || tbUids == NULL || tbNames == NULL) {
75,602!
1239
    rcode = -1;
×
1240
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1241
    goto _exit;
×
1242
  }
1243

1244
  // loop to create table
1245
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
191,699✔
1246
    pCreateReq = req.pReqs + iReq;
116,088✔
1247
    memset(&cRsp, 0, sizeof(cRsp));
116,088✔
1248

1249
    if (tsEnableAudit && tsEnableAuditCreateTable) {
116,088!
1250
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
116,082!
1251
      if (str == NULL) {
116,083!
1252
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1253
        rcode = -1;
×
1254
        goto _exit;
×
1255
      }
1256
      tstrncpy(str, pCreateReq->name, TSDB_TABLE_FNAME_LEN);
116,083✔
1257
      if (taosArrayPush(tbNames, &str) == NULL) {
116,088!
1258
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1259
        rcode = -1;
×
1260
        goto _exit;
×
1261
      }
1262
    }
1263

1264
    // validate hash
1265
    (void)tsnprintf(tbName, TSDB_TABLE_FNAME_LEN, "%s.%s", pVnode->config.dbname, pCreateReq->name);
116,094✔
1266
    if (vnodeValidateTableHash(pVnode, tbName) < 0) {
116,094✔
1267
      cRsp.code = TSDB_CODE_VND_HASH_MISMATCH;
25✔
1268
      if (taosArrayPush(rsp.pArray, &cRsp) == NULL) {
50!
1269
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1270
        rcode = -1;
×
1271
        goto _exit;
×
1272
      }
1273
      vError("vgId:%d create-table:%s failed due to hash value mismatch", TD_VID(pVnode), tbName);
25!
1274
      continue;
25✔
1275
    }
1276

1277
    // do create table
1278
    if (metaCreateTable2(pVnode->pMeta, ver, pCreateReq, &cRsp.pMeta) < 0) {
116,058✔
1279
      if (pCreateReq->flags & TD_CREATE_IF_NOT_EXISTS && terrno == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
1,403!
1280
        cRsp.code = TSDB_CODE_SUCCESS;
4✔
1281
      } else {
1282
        cRsp.code = terrno;
1,399✔
1283
      }
1284
    } else {
1285
      cRsp.code = TSDB_CODE_SUCCESS;
114,673✔
1286
      if (tdFetchTbUidList(pVnode->pSma, &pStore, pCreateReq->ctb.suid, pCreateReq->uid) < 0) {
114,673!
1287
        vError("vgId:%d, failed to fetch tbUid list", TD_VID(pVnode));
×
1288
      }
1289
      if (taosArrayPush(tbUids, &pCreateReq->uid) == NULL) {
229,345!
1290
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1291
        rcode = -1;
×
1292
        goto _exit;
×
1293
      }
1294
      vnodeUpdateMetaRsp(pVnode, cRsp.pMeta);
114,673✔
1295
    }
1296

1297
    if (taosArrayPush(rsp.pArray, &cRsp) == NULL) {
232,145!
1298
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1299
      rcode = -1;
×
1300
      goto _exit;
×
1301
    }
1302
  }
1303

1304
  vDebug("vgId:%d, add %d new created tables into query table list", TD_VID(pVnode), (int32_t)taosArrayGetSize(tbUids));
75,611✔
1305
  if (tqUpdateTbUidList(pVnode->pTq, tbUids, true) < 0) {
75,611!
1306
    vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(terrno));
×
1307
  }
1308
  if (tdUpdateTbUidList(pVnode->pSma, pStore, true) < 0) {
75,605!
1309
    goto _exit;
×
1310
  }
1311
  pStore = tdUidStoreFree(pStore);
75,605✔
1312

1313
  // prepare rsp
1314
  int32_t ret = 0;
75,604✔
1315
  tEncodeSize(tEncodeSVCreateTbBatchRsp, &rsp, pRsp->contLen, ret);
75,604!
1316
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
75,602✔
1317
  if (pRsp->pCont == NULL) {
75,603!
1318
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1319
    rcode = -1;
×
1320
    goto _exit;
×
1321
  }
1322
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
75,603✔
1323
  if (tEncodeSVCreateTbBatchRsp(&encoder, &rsp) != 0) {
75,603!
1324
    vError("vgId:%d, failed to encode create table batch response", TD_VID(pVnode));
×
1325
  }
1326

1327
  if (tsEnableAudit && tsEnableAuditCreateTable) {
75,604!
1328
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
75,602✔
1329

1330
    SName name = {0};
75,602✔
1331
    if (tNameFromString(&name, pVnode->config.dbname, T_NAME_ACCT | T_NAME_DB) < 0) {
75,602!
1332
      vError("vgId:%d, failed to get name from string", TD_VID(pVnode));
×
1333
    }
1334

1335
    SStringBuilder sb = {0};
75,603✔
1336
    for (int32_t i = 0; i < tbNames->size; i++) {
191,659✔
1337
      char **key = (char **)taosArrayGet(tbNames, i);
116,061✔
1338
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
116,061✔
1339
      if (i < tbNames->size - 1) {
116,057✔
1340
        taosStringBuilderAppendChar(&sb, ',');
40,461✔
1341
      }
1342
      // taosMemoryFreeClear(*key);
1343
    }
1344

1345
    size_t len = 0;
75,598✔
1346
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
75,598✔
1347

1348
    if (pOriginRpc->info.conn.user != NULL && strlen(pOriginRpc->info.conn.user) > 0) {
75,596!
1349
      auditAddRecord(pOriginRpc, clusterId, "createTable", name.dbname, "", keyJoined, len);
59,719✔
1350
    }
1351

1352
    taosStringBuilderDestroy(&sb);
75,593✔
1353
  }
1354

1355
_exit:
2✔
1356
  tDeleteSVCreateTbBatchReq(&req);
75,598✔
1357
  taosArrayDestroyEx(rsp.pArray, tFreeSVCreateTbRsp);
75,601✔
1358
  taosArrayDestroy(tbUids);
75,604✔
1359
  tDecoderClear(&decoder);
75,600✔
1360
  tEncoderClear(&encoder);
75,604✔
1361
  taosArrayDestroyP(tbNames, NULL);
75,605✔
1362
  return rcode;
75,605✔
1363
}
1364

1365
static int32_t vnodeProcessAlterStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
6,421✔
1366
  int32_t        code = 0;
6,421✔
1367
  SVCreateStbReq req = {0};
6,421✔
1368
  SDecoder       dc = {0};
6,421✔
1369

1370
  pRsp->msgType = TDMT_VND_ALTER_STB_RSP;
6,421✔
1371
  pRsp->code = TSDB_CODE_SUCCESS;
6,421✔
1372
  pRsp->pCont = NULL;
6,421✔
1373
  pRsp->contLen = 0;
6,421✔
1374

1375
  tDecoderInit(&dc, pReq, len);
6,421✔
1376

1377
  // decode req
1378
  code = tDecodeSVCreateStbReq(&dc, &req);
6,421✔
1379
  if (code) {
6,423!
1380
    tDecoderClear(&dc);
×
1381
    return code;
×
1382
  }
1383

1384
  code = metaAlterSuperTable(pVnode->pMeta, ver, &req);
6,423✔
1385
  if (code) {
6,424!
1386
    pRsp->code = code;
×
1387
    tDecoderClear(&dc);
×
1388
    return code;
×
1389
  }
1390

1391
  tDecoderClear(&dc);
6,424✔
1392

1393
  return 0;
6,424✔
1394
}
1395

1396
static int32_t vnodeProcessDropStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
2,302✔
1397
  SVDropStbReq req = {0};
2,302✔
1398
  int32_t      rcode = TSDB_CODE_SUCCESS;
2,302✔
1399
  SDecoder     decoder = {0};
2,302✔
1400
  SArray      *tbUidList = NULL;
2,302✔
1401

1402
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
2,302✔
1403
  pRsp->pCont = NULL;
2,302✔
1404
  pRsp->contLen = 0;
2,302✔
1405

1406
  // decode request
1407
  tDecoderInit(&decoder, pReq, len);
2,302✔
1408
  if (tDecodeSVDropStbReq(&decoder, &req) < 0) {
2,302!
1409
    rcode = TSDB_CODE_INVALID_MSG;
×
1410
    goto _exit;
×
1411
  }
1412

1413
  // process request
1414
  tbUidList = taosArrayInit(8, sizeof(int64_t));
2,316✔
1415
  if (tbUidList == NULL) goto _exit;
2,323!
1416
  if (metaDropSuperTable(pVnode->pMeta, ver, &req) < 0) {
2,323✔
1417
    rcode = terrno;
8✔
1418
    goto _exit;
8✔
1419
  }
1420

1421
  if (tqUpdateTbUidList(pVnode->pTq, tbUidList, false) < 0) {
2,317!
1422
    rcode = terrno;
×
1423
    goto _exit;
×
1424
  }
1425

1426
  if (tdProcessRSmaDrop(pVnode->pSma, &req) < 0) {
2,317!
1427
    rcode = terrno;
×
1428
    goto _exit;
×
1429
  }
1430

1431
  // return rsp
1432
_exit:
2,317✔
1433
  if (tbUidList) taosArrayDestroy(tbUidList);
2,325!
1434
  pRsp->code = rcode;
2,325✔
1435
  tDecoderClear(&decoder);
2,325✔
1436
  return 0;
2,325✔
1437
}
1438

1439
static int32_t vnodeProcessAlterTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
957✔
1440
  SVAlterTbReq  vAlterTbReq = {0};
957✔
1441
  SVAlterTbRsp  vAlterTbRsp = {0};
957✔
1442
  SDecoder      dc = {0};
957✔
1443
  int32_t       code = 0;
957✔
1444
  int32_t       lino = 0;
957✔
1445
  int32_t       ret;
1446
  SEncoder      ec = {0};
957✔
1447
  STableMetaRsp vMetaRsp = {0};
957✔
1448

1449
  pRsp->msgType = TDMT_VND_ALTER_TABLE_RSP;
957✔
1450
  pRsp->pCont = NULL;
957✔
1451
  pRsp->contLen = 0;
957✔
1452
  pRsp->code = TSDB_CODE_SUCCESS;
957✔
1453

1454
  tDecoderInit(&dc, pReq, len);
957✔
1455

1456
  // decode
1457
  if (tDecodeSVAlterTbReq(&dc, &vAlterTbReq) < 0) {
957!
1458
    vAlterTbRsp.code = TSDB_CODE_INVALID_MSG;
×
1459
    tDecoderClear(&dc);
×
1460
    goto _exit;
×
1461
  }
1462

1463
  // process
1464
  if (metaAlterTable(pVnode->pMeta, ver, &vAlterTbReq, &vMetaRsp) < 0) {
957✔
1465
    vAlterTbRsp.code = terrno;
38✔
1466
    tDecoderClear(&dc);
38✔
1467
    goto _exit;
38✔
1468
  }
1469
  tDecoderClear(&dc);
919✔
1470

1471
  if (NULL != vMetaRsp.pSchemas) {
919✔
1472
    vnodeUpdateMetaRsp(pVnode, &vMetaRsp);
182✔
1473
    vAlterTbRsp.pMeta = &vMetaRsp;
182✔
1474
  }
1475

1476
  if (vAlterTbReq.action == TSDB_ALTER_TABLE_UPDATE_TAG_VAL || vAlterTbReq.action == TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL) {
919✔
1477
    int64_t uid = metaGetTableEntryUidByName(pVnode->pMeta, vAlterTbReq.tbName);
655✔
1478
    if (uid == 0) {
655!
1479
      vError("vgId:%d, %s failed at %s:%d since table %s not found", TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
1480
             vAlterTbReq.tbName);
1481
      goto _exit;
×
1482
    }
1483

1484
    SArray* tbUids = taosArrayInit(4, sizeof(int64_t));
655✔
1485
    void* p = taosArrayPush(tbUids, &uid);
655✔
1486
    TSDB_CHECK_NULL(p, code, lino, _exit, terrno);
655!
1487

1488
    vDebug("vgId:%d, remove tags value altered table:%s from query table list", TD_VID(pVnode), vAlterTbReq.tbName);
655✔
1489
    if ((code = tqUpdateTbUidList(pVnode->pTq, tbUids, false)) < 0) {
655!
1490
      vError("vgId:%d, failed to remove tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1491
    }
1492

1493
    vDebug("vgId:%d, try to add table:%s in query table list", TD_VID(pVnode), vAlterTbReq.tbName);
655✔
1494
    if ((code = tqUpdateTbUidList(pVnode->pTq, tbUids, true)) < 0) {
655!
1495
      vError("vgId:%d, failed to add tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1496
    }
1497

1498
    taosArrayDestroy(tbUids);
655✔
1499
  }
1500

1501
_exit:
264✔
1502
  taosArrayDestroy(vAlterTbReq.pMultiTag);
957✔
1503
  tEncodeSize(tEncodeSVAlterTbRsp, &vAlterTbRsp, pRsp->contLen, ret);
957!
1504
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
957✔
1505
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
957✔
1506
  if (tEncodeSVAlterTbRsp(&ec, &vAlterTbRsp) != 0) {
957!
1507
    vError("vgId:%d, failed to encode alter table response", TD_VID(pVnode));
×
1508
  }
1509

1510
  tEncoderClear(&ec);
957✔
1511
  if (vMetaRsp.pSchemas) {
957✔
1512
    taosMemoryFree(vMetaRsp.pSchemas);
182!
1513
    taosMemoryFree(vMetaRsp.pSchemaExt);
182!
1514
  }
1515
  return 0;
957✔
1516
}
1517

1518
static int32_t vnodeProcessDropTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
2,534✔
1519
                                     SRpcMsg *pOriginRpc) {
1520
  SVDropTbBatchReq req = {0};
2,534✔
1521
  SVDropTbBatchRsp rsp = {0};
2,534✔
1522
  SDecoder         decoder = {0};
2,534✔
1523
  SEncoder         encoder = {0};
2,534✔
1524
  int32_t          ret;
1525
  SArray          *tbUids = NULL;
2,534✔
1526
  STbUidStore     *pStore = NULL;
2,534✔
1527
  SArray          *tbNames = NULL;
2,534✔
1528

1529
  pRsp->msgType = TDMT_VND_DROP_TABLE_RSP;
2,534✔
1530
  pRsp->pCont = NULL;
2,534✔
1531
  pRsp->contLen = 0;
2,534✔
1532
  pRsp->code = TSDB_CODE_SUCCESS;
2,534✔
1533

1534
  // decode req
1535
  tDecoderInit(&decoder, pReq, len);
2,534✔
1536
  ret = tDecodeSVDropTbBatchReq(&decoder, &req);
2,534✔
1537
  if (ret < 0) {
2,534!
1538
    terrno = TSDB_CODE_INVALID_MSG;
×
1539
    pRsp->code = terrno;
×
1540
    goto _exit;
×
1541
  }
1542

1543
  // process req
1544
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
2,534✔
1545
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(SVDropTbRsp));
2,534✔
1546
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
2,534✔
1547
  if (tbUids == NULL || rsp.pArray == NULL || tbNames == NULL) goto _exit;
2,534!
1548

1549
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
5,093✔
1550
    SVDropTbReq *pDropTbReq = req.pReqs + iReq;
2,559✔
1551
    SVDropTbRsp  dropTbRsp = {0};
2,559✔
1552
    tb_uid_t     tbUid = 0;
2,559✔
1553

1554
    /* code */
1555
    ret = metaDropTable2(pVnode->pMeta, ver, pDropTbReq);
2,559✔
1556
    if (ret < 0) {
2,559!
1557
      if (pDropTbReq->igNotExists && terrno == TSDB_CODE_TDB_TABLE_NOT_EXIST) {
×
1558
        dropTbRsp.code = TSDB_CODE_SUCCESS;
×
1559
      } else {
1560
        dropTbRsp.code = terrno;
×
1561
      }
1562
    } else {
1563
      dropTbRsp.code = TSDB_CODE_SUCCESS;
2,559✔
1564
      if (tbUid > 0) {
2,559!
1565
        if (tdFetchTbUidList(pVnode->pSma, &pStore, pDropTbReq->suid, tbUid) < 0) {
×
1566
          vError("vgId:%d, failed to fetch tbUid list", TD_VID(pVnode));
×
1567
        }
1568
      }
1569
    }
1570

1571
    if (taosArrayPush(rsp.pArray, &dropTbRsp) == NULL) {
5,118!
1572
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1573
      pRsp->code = terrno;
×
1574
      goto _exit;
×
1575
    }
1576

1577
    if (tsEnableAuditCreateTable) {
2,559!
1578
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
2,559!
1579
      if (str == NULL) {
2,559!
1580
        pRsp->code = terrno;
×
1581
        goto _exit;
×
1582
      }
1583
      tstrncpy(str, pDropTbReq->name, TSDB_TABLE_FNAME_LEN);
2,559✔
1584
      if (taosArrayPush(tbNames, &str) == NULL) {
2,559!
1585
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1586
        pRsp->code = terrno;
×
1587
        goto _exit;
×
1588
      }
1589
    }
1590
  }
1591

1592
  if (tqUpdateTbUidList(pVnode->pTq, tbUids, false) < 0) {
2,534!
1593
    vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(terrno));
×
1594
  }
1595

1596
  if (tdUpdateTbUidList(pVnode->pSma, pStore, false) < 0) {
2,534!
1597
    goto _exit;
×
1598
  }
1599

1600
  if (tsEnableAuditCreateTable) {
2,534!
1601
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
2,534✔
1602

1603
    SName name = {0};
2,534✔
1604
    if (tNameFromString(&name, pVnode->config.dbname, T_NAME_ACCT | T_NAME_DB) != 0) {
2,534!
1605
      vError("vgId:%d, failed to get name from string", TD_VID(pVnode));
×
1606
    }
1607

1608
    SStringBuilder sb = {0};
2,534✔
1609
    for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
5,093✔
1610
      char **key = (char **)taosArrayGet(tbNames, iReq);
2,559✔
1611
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
2,559✔
1612
      if (iReq < req.nReqs - 1) {
2,559✔
1613
        taosStringBuilderAppendChar(&sb, ',');
25✔
1614
      }
1615
      taosMemoryFreeClear(*key);
2,559!
1616
    }
1617

1618
    size_t len = 0;
2,534✔
1619
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
2,534✔
1620

1621
    if (pOriginRpc->info.conn.user != NULL && strlen(pOriginRpc->info.conn.user) > 0) {
2,534!
1622
      auditAddRecord(pOriginRpc, clusterId, "dropTable", name.dbname, "", keyJoined, len);
2,437✔
1623
    }
1624

1625
    taosStringBuilderDestroy(&sb);
2,534✔
1626
  }
1627

1628
_exit:
×
1629
  taosArrayDestroy(tbUids);
2,534✔
1630
  pStore = tdUidStoreFree(pStore);
2,534✔
1631
  tDecoderClear(&decoder);
2,534✔
1632
  tEncodeSize(tEncodeSVDropTbBatchRsp, &rsp, pRsp->contLen, ret);
2,534!
1633
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
2,534✔
1634
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
2,534✔
1635
  if (tEncodeSVDropTbBatchRsp(&encoder, &rsp) != 0) {
2,534!
1636
    vError("vgId:%d, failed to encode drop table batch response", TD_VID(pVnode));
×
1637
  }
1638
  tEncoderClear(&encoder);
2,534✔
1639
  taosArrayDestroy(rsp.pArray);
2,534✔
1640
  taosArrayDestroy(tbNames);
2,534✔
1641
  return 0;
2,534✔
1642
}
1643

1644
#ifdef BUILD_NO_CALL
1645
static int32_t vnodeDebugPrintSingleSubmitMsg(SMeta *pMeta, SSubmitBlk *pBlock, SSubmitMsgIter *msgIter,
1646
                                              const char *tags) {
1647
  SSubmitBlkIter blkIter = {0};
1648
  STSchema      *pSchema = NULL;
1649
  tb_uid_t       suid = 0;
1650
  STSRow        *row = NULL;
1651
  int32_t        rv = -1;
1652

1653
  tInitSubmitBlkIter(msgIter, pBlock, &blkIter);
1654
  if (blkIter.row == NULL) return 0;
1655

1656
  int32_t code = metaGetTbTSchemaNotNull(pMeta, msgIter->suid, TD_ROW_SVER(blkIter.row), 1,
1657
                                         &pSchema);  // TODO: use the real schema
1658
  if (TSDB_CODE_SUCCESS != code) {
1659
    printf("%s:%d no valid schema\n", tags, __LINE__);
1660
    return code;
1661
  }
1662

1663
  suid = msgIter->suid;
1664
  rv = TD_ROW_SVER(blkIter.row);
1665

1666
  char __tags[128] = {0};
1667
  snprintf(__tags, 128, "%s: uid %" PRIi64 " ", tags, msgIter->uid);
1668
  while ((row = tGetSubmitBlkNext(&blkIter))) {
1669
    tdSRowPrint(row, pSchema, __tags);
1670
  }
1671

1672
  taosMemoryFreeClear(pSchema);
1673

1674
  return TSDB_CODE_SUCCESS;
1675
}
1676
#endif
1677
typedef struct SSubmitReqConvertCxt {
1678
  SSubmitMsgIter msgIter;
1679
  SSubmitBlk    *pBlock;
1680
  SSubmitBlkIter blkIter;
1681
  STSRow        *pRow;
1682
  STSRowIter     rowIter;
1683
  SSubmitTbData *pTbData;
1684
  STSchema      *pTbSchema;
1685
  SArray        *pColValues;
1686
} SSubmitReqConvertCxt;
1687

1688
static int32_t vnodeResetTableCxt(SMeta *pMeta, SSubmitReqConvertCxt *pCxt) {
×
1689
  taosMemoryFreeClear(pCxt->pTbSchema);
×
1690
  int32_t code = metaGetTbTSchemaNotNull(pMeta, pCxt->msgIter.suid > 0 ? pCxt->msgIter.suid : pCxt->msgIter.uid,
×
1691
                                         pCxt->msgIter.sversion, 1, &pCxt->pTbSchema);
1692
  if (TSDB_CODE_SUCCESS != code) {
×
1693
    return code;
×
1694
  }
1695
  tdSTSRowIterInit(&pCxt->rowIter, pCxt->pTbSchema);
×
1696

1697
  tDestroySubmitTbData(pCxt->pTbData, TSDB_MSG_FLG_ENCODE);
×
1698
  if (NULL == pCxt->pTbData) {
×
1699
    pCxt->pTbData = taosMemoryCalloc(1, sizeof(SSubmitTbData));
×
1700
    if (NULL == pCxt->pTbData) {
×
1701
      return terrno;
×
1702
    }
1703
  }
1704
  pCxt->pTbData->flags = 0;
×
1705
  pCxt->pTbData->suid = pCxt->msgIter.suid;
×
1706
  pCxt->pTbData->uid = pCxt->msgIter.uid;
×
1707
  pCxt->pTbData->sver = pCxt->msgIter.sversion;
×
1708
  pCxt->pTbData->pCreateTbReq = NULL;
×
1709
  pCxt->pTbData->aRowP = taosArrayInit(128, POINTER_BYTES);
×
1710
  if (NULL == pCxt->pTbData->aRowP) {
×
1711
    return terrno;
×
1712
  }
1713

1714
  taosArrayDestroy(pCxt->pColValues);
×
1715
  pCxt->pColValues = taosArrayInit(pCxt->pTbSchema->numOfCols, sizeof(SColVal));
×
1716
  if (NULL == pCxt->pColValues) {
×
1717
    return terrno;
×
1718
  }
1719
  for (int32_t i = 0; i < pCxt->pTbSchema->numOfCols; ++i) {
×
1720
    SColVal val = COL_VAL_NONE(pCxt->pTbSchema->columns[i].colId, pCxt->pTbSchema->columns[i].type);
×
1721
    if (taosArrayPush(pCxt->pColValues, &val) == NULL) {
×
1722
      return terrno;
×
1723
    }
1724
  }
1725

1726
  return TSDB_CODE_SUCCESS;
×
1727
}
1728

1729
static void vnodeDestroySubmitReqConvertCxt(SSubmitReqConvertCxt *pCxt) {
×
1730
  taosMemoryFreeClear(pCxt->pTbSchema);
×
1731
  tDestroySubmitTbData(pCxt->pTbData, TSDB_MSG_FLG_ENCODE);
×
1732
  taosMemoryFreeClear(pCxt->pTbData);
×
1733
  taosArrayDestroy(pCxt->pColValues);
×
1734
}
×
1735

1736
static int32_t vnodeCellValConvertToColVal(STColumn *pCol, SCellVal *pCellVal, SColVal *pColVal) {
×
1737
  if (tdValTypeIsNone(pCellVal->valType)) {
×
1738
    pColVal->flag = CV_FLAG_NONE;
×
1739
    return TSDB_CODE_SUCCESS;
×
1740
  }
1741

1742
  if (tdValTypeIsNull(pCellVal->valType)) {
×
1743
    pColVal->flag = CV_FLAG_NULL;
×
1744
    return TSDB_CODE_SUCCESS;
×
1745
  }
1746

1747
  if (IS_VAR_DATA_TYPE(pCol->type)) {
×
1748
    pColVal->value.nData = varDataLen(pCellVal->val);
×
1749
    pColVal->value.pData = (uint8_t *)varDataVal(pCellVal->val);
×
1750
  } else if (TSDB_DATA_TYPE_FLOAT == pCol->type) {
×
1751
    float f = GET_FLOAT_VAL(pCellVal->val);
×
1752
    memcpy(&pColVal->value.val, &f, sizeof(f));
×
1753
  } else if (TSDB_DATA_TYPE_DOUBLE == pCol->type) {
×
1754
    pColVal->value.val = *(int64_t *)pCellVal->val;
×
1755
  } else {
1756
    GET_TYPED_DATA(pColVal->value.val, int64_t, pCol->type, pCellVal->val);
×
1757
  }
1758

1759
  pColVal->flag = CV_FLAG_VALUE;
×
1760
  return TSDB_CODE_SUCCESS;
×
1761
}
1762

1763
static int32_t vnodeTSRowConvertToColValArray(SSubmitReqConvertCxt *pCxt) {
×
1764
  int32_t code = TSDB_CODE_SUCCESS;
×
1765
  tdSTSRowIterReset(&pCxt->rowIter, pCxt->pRow);
×
1766
  for (int32_t i = 0; TSDB_CODE_SUCCESS == code && i < pCxt->pTbSchema->numOfCols; ++i) {
×
1767
    STColumn *pCol = pCxt->pTbSchema->columns + i;
×
1768
    SCellVal  cellVal = {0};
×
1769
    if (!tdSTSRowIterFetch(&pCxt->rowIter, pCol->colId, pCol->type, &cellVal)) {
×
1770
      break;
×
1771
    }
1772
    code = vnodeCellValConvertToColVal(pCol, &cellVal, (SColVal *)taosArrayGet(pCxt->pColValues, i));
×
1773
  }
1774
  return code;
×
1775
}
1776

1777
static int32_t vnodeDecodeCreateTbReq(SSubmitReqConvertCxt *pCxt) {
×
1778
  if (pCxt->msgIter.schemaLen <= 0) {
×
1779
    return TSDB_CODE_SUCCESS;
×
1780
  }
1781

1782
  pCxt->pTbData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
×
1783
  if (NULL == pCxt->pTbData->pCreateTbReq) {
×
1784
    return terrno;
×
1785
  }
1786

1787
  SDecoder decoder = {0};
×
1788
  tDecoderInit(&decoder, (uint8_t *)pCxt->pBlock->data, pCxt->msgIter.schemaLen);
×
1789
  int32_t code = tDecodeSVCreateTbReq(&decoder, pCxt->pTbData->pCreateTbReq);
×
1790
  tDecoderClear(&decoder);
×
1791

1792
  return code;
×
1793
}
1794

1795
static int32_t vnodeSubmitReqConvertToSubmitReq2(SVnode *pVnode, SSubmitReq *pReq, SSubmitReq2 *pReq2) {
×
1796
  pReq2->aSubmitTbData = taosArrayInit(128, sizeof(SSubmitTbData));
×
1797
  if (NULL == pReq2->aSubmitTbData) {
×
1798
    return terrno;
×
1799
  }
1800

1801
  SSubmitReqConvertCxt cxt = {0};
×
1802

1803
  int32_t code = tInitSubmitMsgIter(pReq, &cxt.msgIter);
×
1804
  while (TSDB_CODE_SUCCESS == code) {
×
1805
    code = tGetSubmitMsgNext(&cxt.msgIter, &cxt.pBlock);
×
1806
    if (TSDB_CODE_SUCCESS == code) {
×
1807
      if (NULL == cxt.pBlock) {
×
1808
        break;
×
1809
      }
1810
      code = vnodeResetTableCxt(pVnode->pMeta, &cxt);
×
1811
    }
1812
    if (TSDB_CODE_SUCCESS == code) {
×
1813
      code = tInitSubmitBlkIter(&cxt.msgIter, cxt.pBlock, &cxt.blkIter);
×
1814
    }
1815
    if (TSDB_CODE_SUCCESS == code) {
×
1816
      code = vnodeDecodeCreateTbReq(&cxt);
×
1817
    }
1818
    while (TSDB_CODE_SUCCESS == code && (cxt.pRow = tGetSubmitBlkNext(&cxt.blkIter)) != NULL) {
×
1819
      code = vnodeTSRowConvertToColValArray(&cxt);
×
1820
      if (TSDB_CODE_SUCCESS == code) {
×
1821
        SRow **pNewRow = taosArrayReserve(cxt.pTbData->aRowP, 1);
×
1822
        code = tRowBuild(cxt.pColValues, cxt.pTbSchema, pNewRow);
×
1823
      }
1824
    }
1825
    if (TSDB_CODE_SUCCESS == code) {
×
1826
      code = (NULL == taosArrayPush(pReq2->aSubmitTbData, cxt.pTbData) ? terrno : TSDB_CODE_SUCCESS);
×
1827
    }
1828
    if (TSDB_CODE_SUCCESS == code) {
×
1829
      taosMemoryFreeClear(cxt.pTbData);
×
1830
    }
1831
  }
1832

1833
  vnodeDestroySubmitReqConvertCxt(&cxt);
×
1834
  return code;
×
1835
}
1836

1837
static int32_t vnodeRebuildSubmitReqMsg(SSubmitReq2 *pSubmitReq, void **ppMsg) {
×
1838
  int32_t  code = TSDB_CODE_SUCCESS;
×
1839
  char    *pMsg = NULL;
×
1840
  uint32_t msglen = 0;
×
1841
  tEncodeSize(tEncodeSubmitReq, pSubmitReq, msglen, code);
×
1842
  if (TSDB_CODE_SUCCESS == code) {
×
1843
    pMsg = taosMemoryMalloc(msglen);
×
1844
    if (NULL == pMsg) {
×
1845
      code = terrno;
×
1846
    }
1847
  }
1848
  if (TSDB_CODE_SUCCESS == code) {
×
1849
    SEncoder encoder;
1850
    tEncoderInit(&encoder, (uint8_t *)pMsg, msglen);
×
1851
    code = tEncodeSubmitReq(&encoder, pSubmitReq);
×
1852
    tEncoderClear(&encoder);
×
1853
  }
1854
  if (TSDB_CODE_SUCCESS == code) {
×
1855
    *ppMsg = pMsg;
×
1856
  }
1857
  return code;
×
1858
}
1859

1860
static int32_t vnodeProcessSubmitReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
13,085,488✔
1861
                                     SRpcMsg *pOriginalMsg) {
1862
  int32_t code = 0;
13,085,488✔
1863
  terrno = 0;
13,085,488✔
1864

1865
  SSubmitReq2 *pSubmitReq = &(SSubmitReq2){0};
13,085,724✔
1866
  SSubmitRsp2 *pSubmitRsp = &(SSubmitRsp2){0};
13,085,724✔
1867
  SArray      *newTbUids = NULL;
13,085,724✔
1868
  int32_t      ret;
1869
  SEncoder     ec = {0};
13,085,724✔
1870

1871
  pRsp->code = TSDB_CODE_SUCCESS;
13,085,724✔
1872

1873
  void           *pAllocMsg = NULL;
13,085,724✔
1874
  SSubmitReq2Msg *pMsg = (SSubmitReq2Msg *)pReq;
13,085,724✔
1875
  if (0 == pMsg->version) {
13,085,724!
1876
    code = vnodeSubmitReqConvertToSubmitReq2(pVnode, (SSubmitReq *)pMsg, pSubmitReq);
×
1877
    if (TSDB_CODE_SUCCESS == code) {
×
1878
      code = vnodeRebuildSubmitReqMsg(pSubmitReq, &pReq);
×
1879
    }
1880
    if (TSDB_CODE_SUCCESS == code) {
×
1881
      pAllocMsg = pReq;
×
1882
    }
1883
    if (TSDB_CODE_SUCCESS != code) {
×
1884
      goto _exit;
×
1885
    }
1886
  } else {
1887
    // decode
1888
    pReq = POINTER_SHIFT(pReq, sizeof(SSubmitReq2Msg));
13,085,724✔
1889
    len -= sizeof(SSubmitReq2Msg);
13,085,724✔
1890
    SDecoder dc = {0};
13,085,724✔
1891
    tDecoderInit(&dc, pReq, len);
13,085,724✔
1892
    if (tDecodeSubmitReq(&dc, pSubmitReq) < 0) {
13,085,540!
1893
      code = TSDB_CODE_INVALID_MSG;
×
1894
      goto _exit;
×
1895
    }
1896
    tDecoderClear(&dc);
13,085,615✔
1897
  }
1898

1899
  // scan
1900
  TSKEY now = taosGetTimestamp(pVnode->config.tsdbCfg.precision);
13,085,725✔
1901
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * pVnode->config.tsdbCfg.keep2;
13,085,514✔
1902
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
13,085,514✔
1903
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
26,879,710✔
1904
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
13,793,184✔
1905

1906
    if (pSubmitTbData->pCreateTbReq && pSubmitTbData->pCreateTbReq->uid == 0) {
13,793,246!
1907
      code = TSDB_CODE_INVALID_MSG;
×
1908
      goto _exit;
×
1909
    }
1910

1911
    if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
13,793,246✔
1912
      if (TARRAY_SIZE(pSubmitTbData->aCol) <= 0) {
134!
1913
        code = TSDB_CODE_INVALID_MSG;
×
1914
        goto _exit;
×
1915
      }
1916

1917
      SColData *colDataArr = TARRAY_DATA(pSubmitTbData->aCol);
134✔
1918
      SRowKey   lastKey;
1919
      tColDataArrGetRowKey(colDataArr, TARRAY_SIZE(pSubmitTbData->aCol), 0, &lastKey);
134✔
1920
      for (int32_t iRow = 1; iRow < colDataArr[0].nVal; iRow++) {
224✔
1921
        SRowKey key;
1922
        tColDataArrGetRowKey(TARRAY_DATA(pSubmitTbData->aCol), TARRAY_SIZE(pSubmitTbData->aCol), iRow, &key);
90✔
1923
        if (tRowKeyCompare(&lastKey, &key) >= 0) {
90!
1924
          code = TSDB_CODE_INVALID_MSG;
×
1925
          vError("vgId:%d %s failed 1 since %s, version:%" PRId64, TD_VID(pVnode), __func__, tstrerror(terrno), ver);
×
1926
          goto _exit;
×
1927
        }
1928
      }
1929
    } else {
1930
      int32_t nRow = TARRAY_SIZE(pSubmitTbData->aRowP);
13,793,112✔
1931
      SRow  **aRow = (SRow **)TARRAY_DATA(pSubmitTbData->aRowP);
13,793,112✔
1932
      SRowKey lastRowKey;
1933
      for (int32_t iRow = 0; iRow < nRow; ++iRow) {
739,966,728✔
1934
        if (aRow[iRow]->ts < minKey || aRow[iRow]->ts > maxKey) {
726,172,666✔
1935
          code = TSDB_CODE_INVALID_MSG;
28,058✔
1936
          vError("vgId:%d %s failed 2 since %s, version:%" PRId64, TD_VID(pVnode), __func__, tstrerror(code), ver);
28,058✔
1937
          goto _exit;
2✔
1938
        }
1939
        if (iRow == 0) {
726,144,608✔
1940
          tRowGetKey(aRow[iRow], &lastRowKey);
13,793,093✔
1941
        } else {
1942
          SRowKey rowKey;
1943
          tRowGetKey(aRow[iRow], &rowKey);
712,351,515✔
1944

1945
          if (tRowKeyCompare(&lastRowKey, &rowKey) >= 0) {
712,346,033!
1946
            code = TSDB_CODE_INVALID_MSG;
×
1947
            vError("vgId:%d %s failed 3 since %s, version:%" PRId64, TD_VID(pVnode), __func__, tstrerror(code), ver);
×
1948
            goto _exit;
×
1949
          }
1950
          lastRowKey = rowKey;
712,377,136✔
1951
        }
1952
      }
1953
    }
1954
  }
1955

1956
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
26,881,985✔
1957
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
13,795,020✔
1958

1959
    if (pSubmitTbData->pCreateTbReq) {
13,794,669✔
1960
      pSubmitTbData->uid = pSubmitTbData->pCreateTbReq->uid;
68,110✔
1961
    } else {
1962
      SMetaInfo info = {0};
13,726,559✔
1963

1964
      code = metaGetInfo(pVnode->pMeta, pSubmitTbData->uid, &info, NULL);
13,726,559✔
1965
      if (code) {
13,726,887!
1966
        code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
1967
        vWarn("vgId:%d, table uid:%" PRId64 " not exists", TD_VID(pVnode), pSubmitTbData->uid);
×
1968
        goto _exit;
7✔
1969
      }
1970

1971
      if (info.suid != pSubmitTbData->suid) {
13,726,890!
1972
        code = TSDB_CODE_INVALID_MSG;
×
1973
        goto _exit;
×
1974
      }
1975

1976
      if (info.suid) {
13,726,890✔
1977
        if (metaGetInfo(pVnode->pMeta, info.suid, &info, NULL) != 0) {
12,721,901!
1978
          vWarn("vgId:%d, table uid:%" PRId64 " not exists", TD_VID(pVnode), info.suid);
×
1979
        }
1980
      }
1981

1982
      if (pSubmitTbData->sver != info.skmVer) {
13,727,349!
1983
        code = TSDB_CODE_TDB_INVALID_TABLE_SCHEMA_VER;
×
1984
        goto _exit;
×
1985
      }
1986
    }
1987

1988
    if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
13,795,459✔
1989
      int32_t   nColData = TARRAY_SIZE(pSubmitTbData->aCol);
134✔
1990
      SColData *aColData = (SColData *)TARRAY_DATA(pSubmitTbData->aCol);
134✔
1991

1992
      if (nColData <= 0) {
134!
1993
        code = TSDB_CODE_INVALID_MSG;
×
1994
        goto _exit;
×
1995
      }
1996

1997
      if (aColData[0].cid != PRIMARYKEY_TIMESTAMP_COL_ID || aColData[0].type != TSDB_DATA_TYPE_TIMESTAMP ||
134!
1998
          aColData[0].nVal <= 0) {
134!
1999
        code = TSDB_CODE_INVALID_MSG;
×
2000
        goto _exit;
×
2001
      }
2002

2003
      for (int32_t j = 1; j < nColData; j++) {
583✔
2004
        if (aColData[j].nVal != aColData[0].nVal) {
449!
2005
          code = TSDB_CODE_INVALID_MSG;
×
2006
          goto _exit;
×
2007
        }
2008
      }
2009
    }
2010
  }
2011

2012
  vDebug("vgId:%d, submit block size %d", TD_VID(pVnode), (int32_t)taosArrayGetSize(pSubmitReq->aSubmitTbData));
13,086,965✔
2013

2014
  // loop to handle
2015
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
26,879,195✔
2016
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
13,793,314✔
2017

2018
    // create table
2019
    if (pSubmitTbData->pCreateTbReq) {
13,793,074✔
2020
      // alloc if need
2021
      if (pSubmitRsp->aCreateTbRsp == NULL &&
68,103✔
2022
          (pSubmitRsp->aCreateTbRsp = taosArrayInit(TARRAY_SIZE(pSubmitReq->aSubmitTbData), sizeof(SVCreateTbRsp))) ==
46,054!
2023
              NULL) {
2024
        code = terrno;
×
2025
        goto _exit;
8✔
2026
      }
2027

2028
      SVCreateTbRsp *pCreateTbRsp = taosArrayReserve(pSubmitRsp->aCreateTbRsp, 1);
68,103✔
2029

2030
      // create table
2031
      if (metaCreateTable2(pVnode->pMeta, ver, pSubmitTbData->pCreateTbReq, &pCreateTbRsp->pMeta) == 0) {
68,101✔
2032
        // create table success
2033

2034
        if (newTbUids == NULL &&
68,478!
2035
            (newTbUids = taosArrayInit(TARRAY_SIZE(pSubmitReq->aSubmitTbData), sizeof(int64_t))) == NULL) {
26,130✔
2036
          code = terrno;
×
2037
          goto _exit;
×
2038
        }
2039

2040
        if (taosArrayPush(newTbUids, &pSubmitTbData->uid) == NULL) {
84,696!
2041
          code = terrno;
×
2042
          goto _exit;
×
2043
        }
2044

2045
        if (pCreateTbRsp->pMeta) {
42,348!
2046
          vnodeUpdateMetaRsp(pVnode, pCreateTbRsp->pMeta);
42,348✔
2047
        }
2048
      } else {  // create table failed
2049
        if (terrno != TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
25,752✔
2050
          code = terrno;
8✔
2051
          vError("vgId:%d failed to create table:%s, code:%s", TD_VID(pVnode), pSubmitTbData->pCreateTbReq->name,
8!
2052
                 tstrerror(terrno));
2053
          goto _exit;
8✔
2054
        }
2055
        terrno = 0;
25,742✔
2056
        pSubmitTbData->uid = pSubmitTbData->pCreateTbReq->uid;  // update uid if table exist for using below
25,742✔
2057
      }
2058
    }
2059

2060
    // insert data
2061
    int32_t affectedRows;
2062
    code = tsdbInsertTableData(pVnode->pTsdb, ver, pSubmitTbData, &affectedRows);
13,793,061✔
2063
    if (code) goto _exit;
13,794,467!
2064

2065
    code = metaUpdateChangeTimeWithLock(pVnode->pMeta, pSubmitTbData->uid, pSubmitTbData->ctimeMs);
13,794,467✔
2066
    if (code) goto _exit;
13,793,451!
2067

2068
    pSubmitRsp->affectedRows += affectedRows;
13,793,451✔
2069
  }
2070

2071
  // update the affected table uid list
2072
  if (taosArrayGetSize(newTbUids) > 0) {
13,085,881✔
2073
    vDebug("vgId:%d, add %d table into query table list in handling submit", TD_VID(pVnode),
26,130✔
2074
           (int32_t)taosArrayGetSize(newTbUids));
2075
    if (tqUpdateTbUidList(pVnode->pTq, newTbUids, true) != 0) {
26,130!
2076
      vError("vgId:%d, failed to update tbUid list", TD_VID(pVnode));
×
2077
    }
2078
  }
2079

2080
_exit:
13,085,680✔
2081
  // message
2082
  pRsp->code = code;
13,085,697✔
2083
  tEncodeSize(tEncodeSSubmitRsp2, pSubmitRsp, pRsp->contLen, ret);
13,085,697!
2084
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
13,085,511✔
2085
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
13,085,745✔
2086
  if (tEncodeSSubmitRsp2(&ec, pSubmitRsp) < 0) {
13,085,743!
2087
    vError("vgId:%d, failed to encode submit response", TD_VID(pVnode));
×
2088
  }
2089
  tEncoderClear(&ec);
13,085,714✔
2090

2091
  // update statistics
2092
  (void)atomic_add_fetch_64(&pVnode->statis.nInsert, pSubmitRsp->affectedRows);
13,085,581✔
2093
  (void)atomic_add_fetch_64(&pVnode->statis.nInsertSuccess, pSubmitRsp->affectedRows);
13,085,762✔
2094
  (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsert, 1);
13,085,751✔
2095

2096
  if (tsEnableMonitor && tsMonitorFqdn[0] != 0 && tsMonitorPort != 0 && pSubmitRsp->affectedRows > 0 &&
13,085,743!
2097
      strlen(pOriginalMsg->info.conn.user) > 0 && tsInsertCounter != NULL) {
×
2098
    const char *sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT_AFFECTED_ROWS,
×
2099
                                   pVnode->monitor.strClusterId,
×
2100
                                   pVnode->monitor.strDnodeId,
×
2101
                                   tsLocalEp,
2102
                                   pVnode->monitor.strVgId,
×
2103
                                   pOriginalMsg->info.conn.user,
×
2104
                                   "Success"};
2105
    int         tv = taos_counter_add(tsInsertCounter, pSubmitRsp->affectedRows, sample_labels);
×
2106
  }
2107

2108
  if (code == 0) {
13,085,743✔
2109
    (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
13,085,694✔
2110
    code = tdProcessRSmaSubmit(pVnode->pSma, ver, pSubmitReq, pReq, len);
13,085,754✔
2111
  }
2112
  /*
2113
  if (code == 0) {
2114
    atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
2115
    code = tdProcessRSmaSubmit(pVnode->pSma, ver, pSubmitReq, pReq, len);
2116

2117
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT, pVnode->monitor.strClusterId,
2118
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2119
                                          pOriginalMsg->info.conn.user, "Success"};
2120
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2121
  }
2122
  else{
2123
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT, pVnode->monitor.strClusterId,
2124
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2125
                                        pOriginalMsg->info.conn.user, "Failed"};
2126
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2127
  }
2128
  */
2129

2130
  // clear
2131
  taosArrayDestroy(newTbUids);
13,085,620✔
2132
  tDestroySubmitReq(pSubmitReq, 0 == pMsg->version ? TSDB_MSG_FLG_CMPT : TSDB_MSG_FLG_DECODE);
13,085,503!
2133
  tDestroySSubmitRsp2(pSubmitRsp, TSDB_MSG_FLG_ENCODE);
13,085,739✔
2134

2135
  if (code) terrno = code;
13,085,624✔
2136

2137
  taosMemoryFree(pAllocMsg);
13,085,624!
2138

2139
  return code;
13,085,549✔
2140
}
2141

2142
static int32_t vnodeProcessCreateTSmaReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
28✔
2143
  SVCreateTSmaReq req = {0};
28✔
2144
  SDecoder        coder = {0};
28✔
2145

2146
  if (pRsp) {
28!
2147
    pRsp->msgType = TDMT_VND_CREATE_SMA_RSP;
28✔
2148
    pRsp->code = TSDB_CODE_SUCCESS;
28✔
2149
    pRsp->pCont = NULL;
28✔
2150
    pRsp->contLen = 0;
28✔
2151
  }
2152

2153
  // decode and process req
2154
  tDecoderInit(&coder, pReq, len);
28✔
2155

2156
  if (tDecodeSVCreateTSmaReq(&coder, &req) < 0) {
28!
2157
    terrno = TSDB_CODE_MSG_DECODE_ERROR;
×
2158
    if (pRsp) pRsp->code = terrno;
×
2159
    goto _err;
×
2160
  }
2161

2162
  if (tdProcessTSmaCreate(pVnode->pSma, ver, (const char *)&req) < 0) {
28!
2163
    if (pRsp) pRsp->code = terrno;
×
2164
    goto _err;
×
2165
  }
2166

2167
  tDecoderClear(&coder);
28✔
2168
  vDebug("vgId:%d, success to create tsma %s:%" PRIi64 " version %" PRIi64 " for table %" PRIi64, TD_VID(pVnode),
28!
2169
         req.indexName, req.indexUid, ver, req.tableUid);
2170
  return 0;
28✔
2171

2172
_err:
×
2173
  tDecoderClear(&coder);
×
2174
  vError("vgId:%d, failed to create tsma %s:%" PRIi64 " version %" PRIi64 "for table %" PRIi64 " since %s",
×
2175
         TD_VID(pVnode), req.indexName, req.indexUid, ver, req.tableUid, terrstr());
2176
  return terrno;
×
2177
}
2178

2179
/**
2180
 * @brief specific for smaDstVnode
2181
 *
2182
 * @param pVnode
2183
 * @param pCont
2184
 * @param contLen
2185
 * @return int32_t
2186
 */
2187
int32_t vnodeProcessCreateTSma(SVnode *pVnode, void *pCont, uint32_t contLen) {
×
2188
  return vnodeProcessCreateTSmaReq(pVnode, 1, pCont, contLen, NULL);
×
2189
}
2190

2191
static int32_t vnodeConsolidateAlterHashRange(SVnode *pVnode, int64_t ver) {
42✔
2192
  int32_t code = TSDB_CODE_SUCCESS;
42✔
2193

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

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

2200
  return code;
42✔
2201
}
2202

2203
static int32_t vnodeProcessAlterConfirmReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
8,093✔
2204
  vInfo("vgId:%d, vnode handle msgType:alter-confirm, alter confirm msg is processed", TD_VID(pVnode));
8,093!
2205
  int32_t code = TSDB_CODE_SUCCESS;
8,093✔
2206
  if (!pVnode->config.hashChange) {
8,093✔
2207
    goto _exit;
8,051✔
2208
  }
2209

2210
  code = vnodeConsolidateAlterHashRange(pVnode, ver);
42✔
2211
  if (code < 0) {
42!
2212
    vError("vgId:%d, failed to consolidate alter hashrange since %s. version:%" PRId64, TD_VID(pVnode), terrstr(), ver);
×
2213
    goto _exit;
×
2214
  }
2215
  pVnode->config.hashChange = false;
42✔
2216

2217
_exit:
8,093✔
2218
  pRsp->msgType = TDMT_VND_ALTER_CONFIRM_RSP;
8,093✔
2219
  pRsp->code = code;
8,093✔
2220
  pRsp->pCont = NULL;
8,093✔
2221
  pRsp->contLen = 0;
8,093✔
2222

2223
  return code;
8,093✔
2224
}
2225

2226
extern int32_t tsdbDisableAndCancelAllBgTask(STsdb *pTsdb);
2227
extern void    tsdbEnableBgTask(STsdb *pTsdb);
2228

2229
static int32_t vnodeProcessAlterConfigReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
629✔
2230
  bool walChanged = false;
629✔
2231
  bool tsdbChanged = false;
629✔
2232

2233
  SAlterVnodeConfigReq req = {0};
629✔
2234
  if (tDeserializeSAlterVnodeConfigReq(pReq, len, &req) != 0) {
629!
2235
    terrno = TSDB_CODE_INVALID_MSG;
×
2236
    return TSDB_CODE_INVALID_MSG;
×
2237
  }
2238

2239
  vInfo("vgId:%d, start to alter vnode config, page:%d pageSize:%d buffer:%d szPage:%d szBuf:%" PRIu64
633✔
2240
        " cacheLast:%d cacheLastSize:%d days:%d keep0:%d keep1:%d keep2:%d keepTimeOffset:%d s3KeepLocal:%d "
2241
        "s3Compact:%d fsync:%d level:%d "
2242
        "walRetentionPeriod:%d walRetentionSize:%d",
2243
        TD_VID(pVnode), req.pages, req.pageSize, req.buffer, req.pageSize * 1024, (uint64_t)req.buffer * 1024 * 1024,
2244
        req.cacheLast, req.cacheLastSize, req.daysPerFile, req.daysToKeep0, req.daysToKeep1, req.daysToKeep2,
2245
        req.keepTimeOffset, req.s3KeepLocal, req.s3Compact, req.walFsyncPeriod, req.walLevel, req.walRetentionPeriod,
2246
        req.walRetentionSize);
2247

2248
  if (pVnode->config.cacheLastSize != req.cacheLastSize) {
634✔
2249
    pVnode->config.cacheLastSize = req.cacheLastSize;
238✔
2250
    tsdbCacheSetCapacity(pVnode, (size_t)pVnode->config.cacheLastSize * 1024 * 1024);
238✔
2251
  }
2252

2253
  if (pVnode->config.szBuf != req.buffer * 1024LL * 1024LL) {
634!
2254
    vInfo("vgId:%d, vnode buffer is changed from %" PRId64 " to %" PRId64, TD_VID(pVnode), pVnode->config.szBuf,
×
2255
          (uint64_t)(req.buffer * 1024LL * 1024LL));
2256
    pVnode->config.szBuf = req.buffer * 1024LL * 1024LL;
×
2257
  }
2258

2259
  if (pVnode->config.szCache != req.pages) {
634!
2260
    if (metaAlterCache(pVnode->pMeta, req.pages) < 0) {
×
2261
      vError("vgId:%d, failed to change vnode pages from %d to %d failed since %s", TD_VID(pVnode),
×
2262
             pVnode->config.szCache, req.pages, tstrerror(errno));
2263
      return errno;
×
2264
    } else {
2265
      vInfo("vgId:%d, vnode pages is changed from %d to %d", TD_VID(pVnode), pVnode->config.szCache, req.pages);
×
2266
      pVnode->config.szCache = req.pages;
×
2267
    }
2268
  }
2269

2270
  if (pVnode->config.cacheLast != req.cacheLast) {
634✔
2271
    pVnode->config.cacheLast = req.cacheLast;
351✔
2272
  }
2273

2274
  if (pVnode->config.walCfg.fsyncPeriod != req.walFsyncPeriod) {
634✔
2275
    pVnode->config.walCfg.fsyncPeriod = req.walFsyncPeriod;
54✔
2276
    walChanged = true;
54✔
2277
  }
2278

2279
  if (pVnode->config.walCfg.level != req.walLevel) {
634✔
2280
    if (pVnode->config.walCfg.level == 0) {
36!
2281
      pVnode->config.walCfg.clearFiles = 1;
×
2282
    }
2283
    pVnode->config.walCfg.level = req.walLevel;
36✔
2284
    walChanged = true;
36✔
2285
  }
2286

2287
  if (pVnode->config.walCfg.retentionPeriod != req.walRetentionPeriod) {
634✔
2288
    pVnode->config.walCfg.retentionPeriod = req.walRetentionPeriod;
132✔
2289
    walChanged = true;
132✔
2290
  }
2291

2292
  if (pVnode->config.walCfg.retentionSize != req.walRetentionSize) {
634!
2293
    pVnode->config.walCfg.retentionSize = req.walRetentionSize;
×
2294
    walChanged = true;
×
2295
  }
2296

2297
  if (pVnode->config.tsdbCfg.keep0 != req.daysToKeep0) {
634✔
2298
    pVnode->config.tsdbCfg.keep0 = req.daysToKeep0;
54✔
2299
    if (!VND_IS_RSMA(pVnode)) {
54!
2300
      tsdbChanged = true;
54✔
2301
    }
2302
  }
2303

2304
  if (pVnode->config.tsdbCfg.keep1 != req.daysToKeep1) {
634✔
2305
    pVnode->config.tsdbCfg.keep1 = req.daysToKeep1;
54✔
2306
    if (!VND_IS_RSMA(pVnode)) {
54!
2307
      tsdbChanged = true;
54✔
2308
    }
2309
  }
2310

2311
  if (pVnode->config.tsdbCfg.keep2 != req.daysToKeep2) {
634✔
2312
    pVnode->config.tsdbCfg.keep2 = req.daysToKeep2;
56✔
2313
    if (!VND_IS_RSMA(pVnode)) {
56!
2314
      tsdbChanged = true;
56✔
2315
    }
2316
  }
2317

2318
  if (pVnode->config.tsdbCfg.keepTimeOffset != req.keepTimeOffset) {
634✔
2319
    pVnode->config.tsdbCfg.keepTimeOffset = req.keepTimeOffset;
2✔
2320
    if (!VND_IS_RSMA(pVnode)) {
2!
2321
      tsdbChanged = true;
2✔
2322
    }
2323
  }
2324

2325
  if (req.sttTrigger != -1 && req.sttTrigger != pVnode->config.sttTrigger) {
634!
2326
    if (req.sttTrigger > 1 && pVnode->config.sttTrigger > 1) {
×
2327
      pVnode->config.sttTrigger = req.sttTrigger;
×
2328
    } else {
2329
      vnodeAWait(&pVnode->commitTask);
×
2330

2331
      int32_t ret = tsdbDisableAndCancelAllBgTask(pVnode->pTsdb);
×
2332
      if (ret != 0) {
×
2333
        vError("vgId:%d, failed to disable bg task since %s", TD_VID(pVnode), tstrerror(errno));
×
2334
      }
2335

2336
      pVnode->config.sttTrigger = req.sttTrigger;
×
2337
      tsdbEnableBgTask(pVnode->pTsdb);
×
2338
    }
2339
  }
2340

2341
  if (req.minRows != -1 && req.minRows != pVnode->config.tsdbCfg.minRows) {
634!
2342
    pVnode->config.tsdbCfg.minRows = req.minRows;
×
2343
  }
2344

2345
  if (req.s3KeepLocal != -1 && req.s3KeepLocal != pVnode->config.s3KeepLocal) {
634!
2346
    pVnode->config.s3KeepLocal = req.s3KeepLocal;
×
2347
  }
2348
  if (req.s3Compact != -1 && req.s3Compact != pVnode->config.s3Compact) {
634!
2349
    pVnode->config.s3Compact = req.s3Compact;
×
2350
  }
2351

2352
  if (walChanged) {
634✔
2353
    if (walAlter(pVnode->pWal, &pVnode->config.walCfg) != 0) {
222!
2354
      vError("vgId:%d, failed to alter wal config since %s", TD_VID(pVnode), tstrerror(errno));
×
2355
    }
2356
  }
2357

2358
  if (tsdbChanged) {
633✔
2359
    tsdbSetKeepCfg(pVnode->pTsdb, &pVnode->config.tsdbCfg);
58✔
2360
  }
2361

2362
  return 0;
633✔
2363
}
2364

2365
static int32_t vnodeProcessBatchDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
3,011✔
2366
  SBatchDeleteReq deleteReq;
2367
  SDecoder        decoder;
2368
  tDecoderInit(&decoder, pReq, len);
3,011✔
2369
  if (tDecodeSBatchDeleteReq(&decoder, &deleteReq) < 0) {
3,010!
2370
    tDecoderClear(&decoder);
×
2371
    return terrno = TSDB_CODE_INVALID_MSG;
×
2372
  }
2373

2374
  SMetaReader mr = {0};
3,012✔
2375
  metaReaderDoInit(&mr, pVnode->pMeta, META_READER_NOLOCK);
3,012✔
2376
  STsdb *pTsdb = pVnode->pTsdb;
3,013✔
2377

2378
  if (deleteReq.level) {
3,013✔
2379
    pTsdb = deleteReq.level == 1 ? VND_RSMA1(pVnode) : VND_RSMA2(pVnode);
16✔
2380
  }
2381

2382
  int32_t sz = taosArrayGetSize(deleteReq.deleteReqs);
3,013✔
2383
  for (int32_t i = 0; i < sz; i++) {
6,881✔
2384
    SSingleDeleteReq *pOneReq = taosArrayGet(deleteReq.deleteReqs, i);
3,867✔
2385
    char             *name = pOneReq->tbname;
3,867✔
2386
    if (metaGetTableEntryByName(&mr, name) < 0) {
3,867✔
2387
      vDebug("vgId:%d, stream delete msg, skip since no table: %s", pVnode->config.vgId, name);
1,964✔
2388
      continue;
1,963✔
2389
    }
2390

2391
    int64_t uid = mr.me.uid;
1,908✔
2392

2393
    int32_t code = tsdbDeleteTableData(pTsdb, ver, deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
1,908✔
2394
    if (code < 0) {
1,908!
2395
      terrno = code;
×
2396
      vError("vgId:%d, delete error since %s, suid:%" PRId64 ", uid:%" PRId64 ", start ts:%" PRId64 ", end ts:%" PRId64,
×
2397
             TD_VID(pVnode), terrstr(), deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
2398
    }
2399

2400
    if (deleteReq.level == 0) {
1,908✔
2401
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, deleteReq.ctimeMs);
1,292✔
2402
      if (code < 0) {
1,292!
2403
        terrno = code;
×
2404
        vError("vgId:%d, update change time error since %s, suid:%" PRId64 ", uid:%" PRId64 ", start ts:%" PRId64
×
2405
               ", end ts:%" PRId64,
2406
               TD_VID(pVnode), terrstr(), deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
2407
      }
2408
    }
2409
    tDecoderClear(&mr.coder);
1,908✔
2410
  }
2411
  metaReaderClear(&mr);
3,014✔
2412
  taosArrayDestroy(deleteReq.deleteReqs);
3,014✔
2413
  return 0;
3,013✔
2414
}
2415

2416
static int32_t vnodeProcessDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
58,584✔
2417
                                     SRpcMsg *pOriginalMsg) {
2418
  int32_t     code = 0;
58,584✔
2419
  SDecoder   *pCoder = &(SDecoder){0};
58,584✔
2420
  SDeleteRes *pRes = &(SDeleteRes){0};
58,584✔
2421

2422
  pRsp->msgType = TDMT_VND_DELETE_RSP;
58,584✔
2423
  pRsp->pCont = NULL;
58,584✔
2424
  pRsp->contLen = 0;
58,584✔
2425
  pRsp->code = TSDB_CODE_SUCCESS;
58,584✔
2426

2427
  pRes->uidList = taosArrayInit(0, sizeof(tb_uid_t));
58,584✔
2428
  if (pRes->uidList == NULL) {
58,584!
2429
    code = terrno;
×
2430
    goto _err;
×
2431
  }
2432

2433
  tDecoderInit(pCoder, pReq, len);
58,584✔
2434
  code = tDecodeDeleteRes(pCoder, pRes);
58,584✔
2435
  if (code) goto _err;
58,584!
2436

2437
  if (pRes->affectedRows > 0) {
58,584✔
2438
    for (int32_t iUid = 0; iUid < taosArrayGetSize(pRes->uidList); iUid++) {
111,662✔
2439
      uint64_t uid = *(uint64_t *)taosArrayGet(pRes->uidList, iUid);
57,982✔
2440
      code = tsdbDeleteTableData(pVnode->pTsdb, ver, pRes->suid, uid, pRes->skey, pRes->ekey);
57,983✔
2441
      if (code) goto _err;
57,982!
2442
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, pRes->ctimeMs);
57,982✔
2443
      if (code) goto _err;
57,982!
2444
    }
2445
  }
2446

2447
  code = tdProcessRSmaDelete(pVnode->pSma, ver, pRes, pReq, len);
58,584✔
2448

2449
  tDecoderClear(pCoder);
58,586✔
2450
  taosArrayDestroy(pRes->uidList);
58,586✔
2451

2452
  SVDeleteRsp rsp = {.affectedRows = pRes->affectedRows};
58,586✔
2453
  int32_t     ret = 0;
58,586✔
2454
  tEncodeSize(tEncodeSVDeleteRsp, &rsp, pRsp->contLen, ret);
58,586!
2455
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
58,585✔
2456
  SEncoder ec = {0};
58,585✔
2457
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
58,585✔
2458
  code = tEncodeSVDeleteRsp(&ec, &rsp);
58,586✔
2459
  if (code) goto _err;
58,585!
2460
  tEncoderClear(&ec);
58,585✔
2461
  return code;
58,587✔
2462

2463
_err:
×
2464
  /*
2465
  if(code == TSDB_CODE_SUCCESS){
2466
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_DELETE, pVnode->monitor.strClusterId,
2467
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2468
                                        pOriginalMsg->info.conn.user, "Success"};
2469
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2470
  }
2471
  else{
2472
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_DELETE, pVnode->monitor.strClusterId,
2473
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2474
                                        pOriginalMsg->info.conn.user, "Failed"};
2475
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2476
  }
2477
  */
2478

2479
  return code;
×
2480
}
2481
static int32_t vnodeProcessCreateIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
993✔
2482
  SVCreateStbReq req = {0};
993✔
2483
  SDecoder       dc = {0};
993✔
2484
  int32_t        code = 0;
993✔
2485

2486
  pRsp->msgType = TDMT_VND_CREATE_INDEX_RSP;
993✔
2487
  pRsp->code = TSDB_CODE_SUCCESS;
993✔
2488
  pRsp->pCont = NULL;
993✔
2489
  pRsp->contLen = 0;
993✔
2490

2491
  tDecoderInit(&dc, pReq, len);
993✔
2492
  // decode req
2493
  if (tDecodeSVCreateStbReq(&dc, &req) < 0) {
993!
2494
    tDecoderClear(&dc);
×
2495
    return terrno = TSDB_CODE_INVALID_MSG;
×
2496
  }
2497

2498
  code = metaAddIndexToSuperTable(pVnode->pMeta, ver, &req);
993✔
2499
  if (code) {
993!
2500
    pRsp->code = code;
×
2501
    goto _err;
×
2502
  }
2503
  tDecoderClear(&dc);
993✔
2504
  return 0;
993✔
2505

2506
_err:
×
2507
  tDecoderClear(&dc);
×
2508
  return code;
×
2509
}
2510
static int32_t vnodeProcessDropIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
2,168✔
2511
  SDropIndexReq req = {0};
2,168✔
2512
  int32_t       code = 0;
2,168✔
2513
  pRsp->msgType = TDMT_VND_DROP_INDEX_RSP;
2,168✔
2514
  pRsp->code = TSDB_CODE_SUCCESS;
2,168✔
2515
  pRsp->pCont = NULL;
2,168✔
2516
  pRsp->contLen = 0;
2,168✔
2517

2518
  if ((code = tDeserializeSDropIdxReq(pReq, len, &req))) {
2,168!
2519
    pRsp->code = code;
×
2520
    return code;
×
2521
  }
2522

2523
  code = metaDropIndexFromSuperTable(pVnode->pMeta, ver, &req);
2,168✔
2524
  if (code) {
2,168!
2525
    pRsp->code = code;
×
2526
    return code;
×
2527
  }
2528
  return TSDB_CODE_SUCCESS;
2,168✔
2529
}
2530

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

2533
static int32_t vnodeProcessCompactVnodeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
203✔
2534
  if (!pVnode->restored) {
203✔
2535
    vInfo("vgId:%d, ignore compact req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
173!
2536
    return 0;
173✔
2537
  }
2538
  return vnodeAsyncCompact(pVnode, ver, pReq, len, pRsp);
30✔
2539
}
2540

2541
static int32_t vnodeProcessConfigChangeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
2542
  if (syncCheckMember(pVnode->sync) != 0) {
×
2543
    vError("vgId:%d, failed to check member", TD_VID(pVnode));
×
2544
  }
2545

2546
  pRsp->msgType = TDMT_SYNC_CONFIG_CHANGE_RSP;
×
2547
  pRsp->code = TSDB_CODE_SUCCESS;
×
2548
  pRsp->pCont = NULL;
×
2549
  pRsp->contLen = 0;
×
2550

2551
  return 0;
×
2552
}
2553

2554
static int32_t vnodeCheckToken(SVnode *pVnode, char *member0Token, char *member1Token) {
9✔
2555
  SSyncState syncState = syncGetState(pVnode->sync);
9✔
2556
  if (syncState.state != TAOS_SYNC_STATE_LEADER) {
9✔
2557
    return terrno = TSDB_CODE_SYN_NOT_LEADER;
4✔
2558
  }
2559

2560
  char token[TSDB_ARB_TOKEN_SIZE] = {0};
5✔
2561
  if (vnodeGetArbToken(pVnode, token) != 0) {
5!
2562
    return terrno = TSDB_CODE_NOT_FOUND;
×
2563
  }
2564

2565
  if (strncmp(token, member0Token, TSDB_ARB_TOKEN_SIZE) != 0 &&
5!
2566
      strncmp(token, member1Token, TSDB_ARB_TOKEN_SIZE) != 0) {
5✔
2567
    return terrno = TSDB_CODE_MND_ARB_TOKEN_MISMATCH;
3✔
2568
  }
2569

2570
  terrno = TSDB_CODE_SUCCESS;
2✔
2571
  return 0;
2✔
2572
}
2573

2574
static int32_t vnodeCheckSyncd(SVnode *pVnode, char *member0Token, char *member1Token) {
4✔
2575
  int32_t code = vnodeCheckToken(pVnode, member0Token, member1Token);
4✔
2576
  if (code != 0) {
4✔
2577
    return code;
3✔
2578
  }
2579

2580
  return syncCheckSynced(pVnode->sync);
1✔
2581
}
2582

2583
static int32_t vnodeProcessArbCheckSyncReq(SVnode *pVnode, void *pReq, int32_t len, SRpcMsg *pRsp) {
4✔
2584
  int32_t code = 0;
4✔
2585

2586
  SVArbCheckSyncReq syncReq = {0};
4✔
2587

2588
  code = tDeserializeSVArbCheckSyncReq(pReq, len, &syncReq);
4✔
2589
  if (code) {
4!
2590
    return terrno = code;
×
2591
  }
2592

2593
  pRsp->msgType = TDMT_VND_ARB_CHECK_SYNC_RSP;
4✔
2594
  pRsp->code = TSDB_CODE_SUCCESS;
4✔
2595
  pRsp->pCont = NULL;
4✔
2596
  pRsp->contLen = 0;
4✔
2597

2598
  SVArbCheckSyncRsp syncRsp = {0};
4✔
2599
  syncRsp.arbToken = syncReq.arbToken;
4✔
2600
  syncRsp.member0Token = syncReq.member0Token;
4✔
2601
  syncRsp.member1Token = syncReq.member1Token;
4✔
2602
  syncRsp.vgId = TD_VID(pVnode);
4✔
2603

2604
  if (vnodeCheckSyncd(pVnode, syncReq.member0Token, syncReq.member1Token) != 0) {
4✔
2605
    vError("vgId:%d, failed to check assigned log syncd", TD_VID(pVnode));
3!
2606
  }
2607
  syncRsp.errCode = terrno;
4✔
2608

2609
  if (vnodeUpdateArbTerm(pVnode, syncReq.arbTerm) != 0) {
4!
2610
    vError("vgId:%d, failed to update arb term", TD_VID(pVnode));
×
2611
    code = -1;
×
2612
    goto _OVER;
×
2613
  }
2614

2615
  int32_t contLen = tSerializeSVArbCheckSyncRsp(NULL, 0, &syncRsp);
4✔
2616
  if (contLen <= 0) {
4!
2617
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2618
    code = -1;
×
2619
    goto _OVER;
×
2620
  }
2621
  void *pHead = rpcMallocCont(contLen);
4✔
2622
  if (!pHead) {
4!
2623
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2624
    code = -1;
×
2625
    goto _OVER;
×
2626
  }
2627

2628
  if (tSerializeSVArbCheckSyncRsp(pHead, contLen, &syncRsp) <= 0) {
4!
2629
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2630
    rpcFreeCont(pHead);
×
2631
    code = -1;
×
2632
    goto _OVER;
×
2633
  }
2634

2635
  pRsp->pCont = pHead;
4✔
2636
  pRsp->contLen = contLen;
4✔
2637

2638
  terrno = TSDB_CODE_SUCCESS;
4✔
2639

2640
_OVER:
4✔
2641
  tFreeSVArbCheckSyncReq(&syncReq);
4✔
2642
  return code;
4✔
2643
}
2644

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