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

taosdata / TDengine / #3580

14 Jan 2025 02:54AM UTC coverage: 63.556% (+0.6%) from 62.976%
#3580

push

travis-ci

web-flow
Merge pull request #29504 from taosdata/chore/codeowners

chore: add codeowners

140725 of 284535 branches covered (49.46%)

Branch coverage included in aggregate %.

219148 of 281694 relevant lines covered (77.8%)

19019641.27 hits per line

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

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

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

27
extern taos_counter_t *tsInsertCounter;
28

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

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

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

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

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

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

78
  // name
79
  char *name = NULL;
153,091✔
80
  if (tDecodeCStr(pCoder, &name) < 0) {
153,065!
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);
153,065✔
87
  if (uid == 0) {
153,069✔
88
    uid = tGenIdPI64();
126,599✔
89
  }
90
  *(int64_t *)(pCoder->data + pCoder->pos) = uid;
153,089✔
91

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

95
  tEndDecode(pCoder);
153,089✔
96

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

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

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

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

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

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

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

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

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

157
  code = 0;
950✔
158

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

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

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

177
  SArray *tbUids = NULL;
105,509✔
178
  int64_t timestampMs = 0;
105,509✔
179

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

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

200
    ttlReq.nUids = taosArrayGetSize(tbUids);
105,590✔
201
    ttlReq.pTbUids = tbUids;
105,481✔
202
  }
203

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

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

242
  return code;
105,538✔
243
}
244

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

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

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

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

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

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

299
  int32_t keep = pVnode->config.tsdbCfg.keep2;
10,714,099✔
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,714,099✔
312
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
10,714,099✔
313
  if (submitTbData.flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
10,714,099✔
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++) {
582✔
340
      code = tDecodeColData(version, pCoder, &colData);
448✔
341
      if (code) {
448!
342
        code = TSDB_CODE_INVALID_MSG;
×
343
        goto _exit;
×
344
      }
345
    }
346
  } else {
347
    uint64_t nRow;
348
    if (tDecodeU64v(pCoder, &nRow) < 0) {
10,719,136!
349
      code = TSDB_CODE_INVALID_MSG;
×
350
      goto _exit;
1,697✔
351
    }
352

353
    for (int32_t iRow = 0; iRow < nRow; ++iRow) {
680,352,315✔
354
      SRow *pRow = (SRow *)(pCoder->data + pCoder->pos);
669,634,876✔
355
      pCoder->pos += pRow->len;
669,634,876✔
356

357
      if (pRow->ts < minKey || pRow->ts > maxKey) {
669,634,876!
358
        code = TSDB_CODE_TDB_TIMESTAMP_OUT_OF_RANGE;
1,697✔
359
        goto _exit;
1,697✔
360
      }
361
    }
362
  }
363

364
  if (!tDecodeIsEnd(pCoder)) {
10,717,573!
365
    *(int64_t *)(pCoder->data + pCoder->pos) = ctimeMs;
10,719,489✔
366
    pCoder->pos += sizeof(int64_t);
10,719,489✔
367
  }
368

369
  tEndDecode(pCoder);
10,717,573✔
370

371
_exit:
10,719,152✔
372
  return code;
10,719,152✔
373
}
374
static int32_t vnodePreProcessSubmitMsg(SVnode *pVnode, SRpcMsg *pMsg) {
10,043,193✔
375
  int32_t code = 0;
10,043,193✔
376
  int32_t lino = 0;
10,043,193✔
377

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

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

384
  if (taosHton64(((SSubmitReq2Msg *)pMsg->pCont)->version) != 1) {
10,043,193!
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,043,203✔
390

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

402
  int64_t btimeMs = taosGetTimestampMs();
10,043,193✔
403
  int64_t ctimeMs = btimeMs;
10,043,193✔
404
  for (int32_t i = 0; i < nSubmitTbData; i++) {
20,760,576✔
405
    code = vnodePreProcessSubmitTbData(pVnode, pCoder, btimeMs, ctimeMs);
10,719,807✔
406
    TSDB_CHECK_CODE(code, lino, _exit);
10,719,081✔
407
  }
408

409
  tEndDecode(pCoder);
10,040,769✔
410

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

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

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

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

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

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

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

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

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

453
  taosArrayDestroy(res.uidList);
58,931✔
454

455
_exit:
58,931✔
456
  return code;
58,931✔
457
}
458

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

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

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

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

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

476
  if (code) {
2,616!
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,616✔
480
  }
481
  return code;
2,616✔
482
}
483

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

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

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

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

500
  return code;
×
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,492✔
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,387,021✔
559
  int32_t code = 0;
10,387,021✔
560

561
  switch (pMsg->msgType) {
10,387,021!
562
    case TDMT_VND_CREATE_TABLE: {
66,441✔
563
      code = vnodePreProcessCreateTableMsg(pVnode, pMsg);
66,441✔
564
    } break;
66,445✔
565
    case TDMT_VND_ALTER_TABLE: {
950✔
566
      code = vnodePreProcessAlterTableMsg(pVnode, pMsg);
950✔
567
    } break;
950✔
568
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
105,740✔
569
    case TDMT_VND_DROP_TTL_TABLE: {
570
      code = vnodePreProcessDropTtlMsg(pVnode, pMsg);
105,740✔
571
    } break;
105,503✔
572
    case TDMT_VND_SUBMIT: {
10,043,243✔
573
      code = vnodePreProcessSubmitMsg(pVnode, pMsg);
10,043,243✔
574
    } break;
10,043,190✔
575
    case TDMT_VND_DELETE: {
58,928✔
576
      code = vnodePreProcessDeleteMsg(pVnode, pMsg);
58,928✔
577
    } break;
58,930✔
578
    case TDMT_VND_BATCH_DEL: {
2,617✔
579
      code = vnodePreProcessBatchDeleteMsg(pVnode, pMsg);
2,617✔
580
    } break;
2,616✔
581
    case TDMT_VND_ARB_CHECK_SYNC: {
×
582
      code = vnodePreProcessArbCheckSyncMsg(pVnode, pMsg);
×
583
    } break;
×
584
    case TDMT_VND_DROP_TABLE: {
2,492✔
585
      code = vnodePreProcessDropTbMsg(pVnode, pMsg);
2,492✔
586
    } break;
2,492✔
587
    default:
106,610✔
588
      break;
106,610✔
589
  }
590

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

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

604
  (void)taosThreadMutexLock(&pVnode->mutex);
13,384,549✔
605
  if (pVnode->disableWrite) {
13,384,909!
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,384,909✔
611

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

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

631
  atomic_store_64(&pVnode->state.applied, ver);
13,384,640✔
632
  atomic_store_64(&pVnode->state.applyTerm, pMsg->info.conn.applyTerm);
13,384,939✔
633

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

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

641
  switch (pMsg->msgType) {
13,359,568!
642
    /* META */
643
    case TDMT_VND_CREATE_STB:
27,287✔
644
      if (vnodeProcessCreateStbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
27,287✔
645
      break;
27,387✔
646
    case TDMT_VND_ALTER_STB:
6,278✔
647
      if (vnodeProcessAlterStbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
6,278!
648
      break;
6,286✔
649
    case TDMT_VND_DROP_STB:
2,280✔
650
      if (vnodeProcessDropStbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
2,280!
651
      break;
2,315✔
652
    case TDMT_VND_CREATE_TABLE:
75,217✔
653
      if (vnodeProcessCreateTbReq(pVnode, ver, pReq, len, pRsp, pMsg) < 0) goto _err;
75,217!
654
      break;
75,271✔
655
    case TDMT_VND_ALTER_TABLE:
955✔
656
      if (vnodeProcessAlterTbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
955!
657
      break;
955✔
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:
229✔
668
      if (vnodeProcessTrimReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
229!
669
      break;
229✔
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,083,996✔
678
      if (vnodeProcessSubmitReq(pVnode, ver, pMsg->pCont, pMsg->contLen, pRsp, pMsg) < 0) goto _err;
13,083,996✔
679
      break;
13,083,964✔
680
    case TDMT_VND_DELETE:
62,265✔
681
      if (vnodeProcessDeleteReq(pVnode, ver, pReq, len, pRsp, pMsg) < 0) goto _err;
62,265!
682
      break;
62,265✔
683
    case TDMT_VND_BATCH_DEL:
2,927✔
684
      if (vnodeProcessBatchDeleteReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
2,927!
685
      break;
2,932✔
686
    /* TQ */
687
    case TDMT_VND_TMQ_SUBSCRIBE:
3,103✔
688
      if (tqProcessSubscribeReq(pVnode->pTq, ver, pReq, len) < 0) {
3,103✔
689
        goto _err;
1✔
690
      }
691
      break;
3,107✔
692
    case TDMT_VND_TMQ_DELETE_SUB:
868✔
693
      if (tqProcessDeleteSubReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen) < 0) {
868!
694
        goto _err;
×
695
      }
696
      break;
869✔
697
    case TDMT_VND_TMQ_COMMIT_OFFSET:
13,631✔
698
      if (tqProcessOffsetCommitReq(pVnode->pTq, ver, pReq, len) < 0) {
13,631!
699
        goto _err;
×
700
      }
701
      break;
13,637✔
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,250✔
713
      if ((code = tqProcessTaskDeployReq(pVnode->pTq, ver, pReq, len)) != TSDB_CODE_SUCCESS) {
14,250!
714
        goto _err;
×
715
      }
716
    } break;
14,262✔
717
    case TDMT_STREAM_TASK_DROP: {
6,961✔
718
      if ((code = tqProcessTaskDropReq(pVnode->pTq, pMsg->pCont, pMsg->contLen)) < 0) {
6,961!
719
        goto _err;
×
720
      }
721
    } break;
6,986✔
722
    case TDMT_STREAM_TASK_UPDATE_CHKPT: {
5,333✔
723
      if ((code = tqProcessTaskUpdateCheckpointReq(pVnode->pTq, pMsg->pCont, pMsg->contLen)) < 0) {
5,333!
724
        goto _err;
×
725
      }
726
    } break;
5,339✔
727
    case TDMT_STREAM_CONSEN_CHKPT: {
277✔
728
      if (pVnode->restored && (code = tqProcessTaskConsenChkptIdReq(pVnode->pTq, pMsg)) < 0) {
277!
729
        goto _err;
×
730
      }
731

732
    } break;
277✔
733
    case TDMT_STREAM_TASK_PAUSE: {
2,048✔
734
      if (pVnode->restored && vnodeIsLeader(pVnode) &&
3,427!
735
          (code = tqProcessTaskPauseReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen)) < 0) {
1,375✔
736
        goto _err;
×
737
      }
738
    } break;
2,051✔
739
    case TDMT_STREAM_TASK_RESUME: {
3,678✔
740
      if (pVnode->restored && vnodeIsLeader(pVnode) &&
6,226!
741
          (code = tqProcessTaskResumeReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen)) < 0) {
2,546✔
742
        goto _err;
×
743
      }
744
    } break;
3,682✔
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,084✔
752
      needCommit = pVnode->config.hashChange;
8,084✔
753
      if (vnodeProcessAlterConfirmReq(pVnode, ver, pReq, len, pRsp) < 0) {
8,084!
754
        goto _err;
×
755
      }
756
      break;
8,084✔
757
    case TDMT_VND_ALTER_CONFIG:
631✔
758
      vnodeProcessAlterConfigReq(pVnode, ver, pReq, len, pRsp);
631✔
759
      break;
633✔
760
    case TDMT_VND_COMMIT:
29,929✔
761
      needCommit = true;
29,929✔
762
      break;
29,929✔
763
    case TDMT_VND_CREATE_INDEX:
959✔
764
      vnodeProcessCreateIndexReq(pVnode, ver, pReq, len, pRsp);
959✔
765
      break;
959✔
766
    case TDMT_VND_DROP_INDEX:
2,134✔
767
      vnodeProcessDropIndexReq(pVnode, ver, pReq, len, pRsp);
2,134✔
768
      break;
2,134✔
769
    case TDMT_VND_STREAM_CHECK_POINT_SOURCE:  // always return true
3,222✔
770
      tqProcessTaskCheckPointSourceReq(pVnode->pTq, pMsg, pRsp);
3,222✔
771
      break;
3,226✔
772
    case TDMT_VND_STREAM_TASK_UPDATE:  // always return true
118✔
773
      tqProcessTaskUpdateReq(pVnode->pTq, pMsg);
118✔
774
      break;
118✔
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:
×
788
      vnodeProcessArbCheckSyncReq(pVnode, pReq, len, pRsp);
×
789
      break;
×
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,359,600!
796
         ver);
797

798
  walApplyVer(pVnode->pWal, ver);
13,359,600✔
799

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

806
  // commit if need
807
  if (needCommit) {
13,359,155✔
808
    vInfo("vgId:%d, commit at version %" PRId64, TD_VID(pVnode), ver);
29,455!
809
    code = vnodeAsyncCommit(pVnode);
29,466✔
810
    if (code) {
29,468!
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);
29,468✔
817
    if (code) {
29,464✔
818
      vError("vgId:%d, failed to begin vnode since %s.", TD_VID(pVnode), tstrerror(terrno));
31!
819
      goto _err;
×
820
    }
821
  }
822

823
_exit:
13,359,133✔
824
  return 0;
13,384,216✔
825

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

832
int32_t vnodePreprocessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg) {
8,027,796✔
833
  if (TDMT_SCH_QUERY != pMsg->msgType && TDMT_SCH_MERGE_QUERY != pMsg->msgType) {
8,027,796✔
834
    return 0;
535,453✔
835
  }
836

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

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

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

857
  switch (pMsg->msgType) {
8,034,531!
858
    case TDMT_SCH_QUERY:
5,743,575✔
859
      if (!syncIsReadyForRead(pVnode->sync)) {
5,743,575✔
860
        pMsg->code = (terrno) ? terrno : TSDB_CODE_SYN_NOT_LEADER;
1,888!
861
        redirected = true;
1,888✔
862
      }
863
      code = qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
5,742,183✔
864
      if (redirected) {
5,742,111✔
865
        vnodeRedirectRpcMsg(pVnode, pMsg, pMsg->code);
1,621✔
866
        return 0;
1,888✔
867
      }
868
      return code;
5,740,490✔
869
    case TDMT_SCH_MERGE_QUERY:
1,757,450✔
870
      return qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
1,757,450✔
871
    case TDMT_SCH_QUERY_CONTINUE:
451,739✔
872
      return qWorkerProcessCQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
451,739✔
873
    case TDMT_VND_TMQ_CONSUME:
63,901✔
874
      return tqProcessPollReq(pVnode->pTq, pMsg);
63,901✔
875
    case TDMT_VND_TMQ_CONSUME_PUSH:
18,432✔
876
      return tqProcessPollPush(pVnode->pTq, pMsg);
18,432✔
877
    default:
×
878
      vError("unknown msg type:%d in query queue", pMsg->msgType);
×
879
      return TSDB_CODE_APP_ERROR;
×
880
  }
881
}
882

883
int32_t vnodeProcessFetchMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
20,313,442✔
884
  vTrace("vgId:%d, msg:%p in fetch queue is processing", pVnode->config.vgId, pMsg);
20,313,442✔
885
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
20,313,443✔
886
       pMsg->msgType == TDMT_VND_BATCH_META || pMsg->msgType == TDMT_VND_TABLE_NAME) &&
14,105,807✔
887
      !syncIsReadyForRead(pVnode->sync)) {
7,292,921✔
888
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
5,673✔
889
    return 0;
5,673✔
890
  }
891

892
  switch (pMsg->msgType) {
20,308,820!
893
    case TDMT_SCH_FETCH:
9,880,861✔
894
    case TDMT_SCH_MERGE_FETCH:
895
      return qWorkerProcessFetchMsg(pVnode, pVnode->pQuery, pMsg, 0);
9,880,861✔
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,493,107✔
901
      return qWorkerProcessDropMsg(pVnode, pVnode->pQuery, pMsg, 0);
7,493,107✔
902
    case TDMT_SCH_TASK_NOTIFY:
231✔
903
      return qWorkerProcessNotifyMsg(pVnode, pVnode->pQuery, pMsg, 0);
231✔
904
    case TDMT_SCH_QUERY_HEARTBEAT:
1,858,473✔
905
      return qWorkerProcessHbMsg(pVnode, pVnode->pQuery, pMsg, 0);
1,858,473✔
906
    case TDMT_VND_TABLE_META:
1,779✔
907
    case TDMT_VND_TABLE_NAME:
908
      return vnodeGetTableMeta(pVnode, pMsg, true);
1,779✔
909
    case TDMT_VND_TABLE_CFG:
×
910
      return vnodeGetTableCfg(pVnode, pMsg, true);
×
911
    case TDMT_VND_BATCH_META:
1,074,278✔
912
      return vnodeGetBatchMeta(pVnode, pMsg);
1,074,278✔
913
#ifdef TD_ENTERPRISE
914
    case TDMT_VND_QUERY_COMPACT_PROGRESS:
48✔
915
      return vnodeQueryCompactProgress(pVnode, pMsg);
48✔
916
#endif
917
      //    case TDMT_VND_TMQ_CONSUME:
918
      //      return tqProcessPollReq(pVnode->pTq, pMsg);
919
    case TDMT_VND_TMQ_VG_WALINFO:
13✔
920
      return tqProcessVgWalInfoReq(pVnode->pTq, pMsg);
13✔
921
    case TDMT_VND_TMQ_VG_COMMITTEDINFO:
2✔
922
      return tqProcessVgCommittedInfoReq(pVnode->pTq, pMsg);
2✔
923
    case TDMT_VND_TMQ_SEEK:
28✔
924
      return tqProcessSeekReq(pVnode->pTq, pMsg);
28✔
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) {
384,198✔
933
  vTrace("vgId:%d, msg:%p in fetch queue is processing", pVnode->config.vgId, pMsg);
384,198✔
934
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
384,198!
935
       pMsg->msgType == TDMT_VND_BATCH_META) &&
384,221!
936
      !syncIsReadyForRead(pVnode->sync)) {
×
937
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
×
938
    return 0;
×
939
  }
940

941
  switch (pMsg->msgType) {
384,236!
942
    case TDMT_STREAM_TASK_RUN:
168,989✔
943
      return tqProcessTaskRunReq(pVnode->pTq, pMsg);
168,989✔
944
    case TDMT_STREAM_TASK_DISPATCH:
58,504✔
945
      return tqProcessTaskDispatchReq(pVnode->pTq, pMsg);
58,504✔
946
    case TDMT_STREAM_TASK_DISPATCH_RSP:
58,473✔
947
      return tqProcessTaskDispatchRsp(pVnode->pTq, pMsg);
58,473✔
948
    case TDMT_VND_STREAM_TASK_CHECK:
21,748✔
949
      return tqProcessTaskCheckReq(pVnode->pTq, pMsg);
21,748✔
950
    case TDMT_VND_STREAM_TASK_CHECK_RSP:
22,286✔
951
      return tqProcessTaskCheckRsp(pVnode->pTq, pMsg);
22,286✔
952
    case TDMT_STREAM_RETRIEVE:
579✔
953
      return tqProcessTaskRetrieveReq(pVnode->pTq, pMsg);
579✔
954
    case TDMT_STREAM_RETRIEVE_RSP:
462✔
955
      return tqProcessTaskRetrieveRsp(pVnode->pTq, pMsg);
462✔
956
    case TDMT_VND_STREAM_SCAN_HISTORY:
2,603✔
957
      return tqProcessTaskScanHistory(pVnode->pTq, pMsg);
2,603✔
958
    case TDMT_STREAM_TASK_CHECKPOINT_READY:
7,710✔
959
      return tqProcessTaskCheckpointReadyMsg(pVnode->pTq, pMsg);
7,710✔
960
    case TDMT_STREAM_TASK_CHECKPOINT_READY_RSP:
7,708✔
961
      return tqProcessTaskCheckpointReadyRsp(pVnode->pTq, pMsg);
7,708✔
962
    case TDMT_STREAM_RETRIEVE_TRIGGER:
×
963
      return tqProcessTaskRetrieveTriggerReq(pVnode->pTq, pMsg);
×
964
    case TDMT_STREAM_RETRIEVE_TRIGGER_RSP:
×
965
      return tqProcessTaskRetrieveTriggerRsp(pVnode->pTq, pMsg);
×
966
    case TDMT_MND_STREAM_HEARTBEAT_RSP:
24,766✔
967
      return tqProcessStreamHbRsp(pVnode->pTq, pMsg);
24,766✔
968
    case TDMT_MND_STREAM_REQ_CHKPT_RSP:
4,411✔
969
      return tqProcessStreamReqCheckpointRsp(pVnode->pTq, pMsg);
4,411✔
970
    case TDMT_VND_GET_STREAM_PROGRESS:
×
971
      return tqStreamProgressRetrieveReq(pVnode->pTq, pMsg);
×
972
    case TDMT_MND_STREAM_CHKPT_REPORT_RSP:
5,997✔
973
      return tqProcessTaskChkptReportRsp(pVnode->pTq, pMsg);
5,997✔
974
    default:
×
975
      vError("unknown msg type:%d in stream queue", pMsg->msgType);
×
976
      return TSDB_CODE_APP_ERROR;
×
977
  }
978
}
979

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1185
  tDecoderClear(&coder);
27,393✔
1186
  return 0;
27,376✔
1187

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

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

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

1219
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(cRsp));
75,251✔
1220
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
75,268✔
1221
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
75,270✔
1222
  if (rsp.pArray == NULL || tbUids == NULL || tbNames == NULL) {
75,267!
1223
    rcode = -1;
2✔
1224
    terrno = TSDB_CODE_OUT_OF_MEMORY;
2✔
1225
    goto _exit;
×
1226
  }
1227

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

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

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

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

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

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

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

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

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

1319
    SStringBuilder sb = {0};
75,270✔
1320
    for (int32_t i = 0; i < tbNames->size; i++) {
191,677✔
1321
      char **key = (char **)taosArrayGet(tbNames, i);
116,411✔
1322
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
116,411✔
1323
      if (i < tbNames->size - 1) {
116,406✔
1324
        taosStringBuilderAppendChar(&sb, ',');
41,144✔
1325
      }
1326
      // taosMemoryFreeClear(*key);
1327
    }
1328

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

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

1336
    taosStringBuilderDestroy(&sb);
75,267✔
1337
  }
1338

1339
_exit:
2✔
1340
  tDeleteSVCreateTbBatchReq(&req);
75,268✔
1341
  taosArrayDestroyEx(rsp.pArray, tFreeSVCreateTbRsp);
75,270✔
1342
  taosArrayDestroy(tbUids);
75,273✔
1343
  tDecoderClear(&decoder);
75,271✔
1344
  tEncoderClear(&encoder);
75,272✔
1345
  taosArrayDestroyP(tbNames, NULL);
75,272✔
1346
  return rcode;
75,273✔
1347
}
1348

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

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

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

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

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

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

1377
  return 0;
6,286✔
1378
}
1379

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1482
    taosArrayDestroy(tbUids);
653✔
1483
  }
1484

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1656
  taosMemoryFreeClear(pSchema);
1657

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

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

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

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

1710
  return TSDB_CODE_SUCCESS;
×
1711
}
1712

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

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

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

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

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

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

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

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

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

1776
  return code;
×
1777
}
1778

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

1785
  SSubmitReqConvertCxt cxt = {0};
×
1786

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

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

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

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

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

1855
  pRsp->code = TSDB_CODE_SUCCESS;
13,084,078✔
1856

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

1883
  // scan
1884
  TSKEY now = taosGetTimestamp(pVnode->config.tsdbCfg.precision);
13,084,111✔
1885
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * pVnode->config.tsdbCfg.keep2;
13,084,004✔
1886
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
13,084,004✔
1887
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
26,883,874✔
1888
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
13,799,248✔
1889

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

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

1901
      SColData *colDataArr = TARRAY_DATA(pSubmitTbData->aCol);
134✔
1902
      SRowKey   lastKey;
1903
      tColDataArrGetRowKey(colDataArr, TARRAY_SIZE(pSubmitTbData->aCol), 0, &lastKey);
134✔
1904
      for (int32_t iRow = 1; iRow < colDataArr[0].nVal; iRow++) {
224✔
1905
        SRowKey key;
1906
        tColDataArrGetRowKey(TARRAY_DATA(pSubmitTbData->aCol), TARRAY_SIZE(pSubmitTbData->aCol), iRow, &key);
90✔
1907
        if (tRowKeyCompare(&lastKey, &key) >= 0) {
90!
1908
          code = TSDB_CODE_INVALID_MSG;
×
1909
          vError("vgId:%d %s failed 1 since %s, version:%" PRId64, TD_VID(pVnode), __func__, tstrerror(terrno), ver);
×
1910
          goto _exit;
×
1911
        }
1912
      }
1913
    } else {
1914
      int32_t nRow = TARRAY_SIZE(pSubmitTbData->aRowP);
13,799,087✔
1915
      SRow  **aRow = (SRow **)TARRAY_DATA(pSubmitTbData->aRowP);
13,799,087✔
1916
      SRowKey lastRowKey;
1917
      for (int32_t iRow = 0; iRow < nRow; ++iRow) {
737,369,272✔
1918
        if (aRow[iRow]->ts < minKey || aRow[iRow]->ts > maxKey) {
723,569,536!
1919
          code = TSDB_CODE_INVALID_MSG;
27,133✔
1920
          vError("vgId:%d %s failed 2 since %s, version:%" PRId64, TD_VID(pVnode), __func__, tstrerror(code), ver);
27,133!
1921
          goto _exit;
×
1922
        }
1923
        if (iRow == 0) {
723,542,403✔
1924
          tRowGetKey(aRow[iRow], &lastRowKey);
13,799,097✔
1925
        } else {
1926
          SRowKey rowKey;
1927
          tRowGetKey(aRow[iRow], &rowKey);
709,743,306✔
1928

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

1940
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
26,886,460✔
1941
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
13,801,669✔
1942

1943
    if (pSubmitTbData->pCreateTbReq) {
13,801,461✔
1944
      pSubmitTbData->uid = pSubmitTbData->pCreateTbReq->uid;
62,686✔
1945
    } else {
1946
      SMetaInfo info = {0};
13,738,775✔
1947

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

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

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

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

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

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

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

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

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

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

2002
    // create table
2003
    if (pSubmitTbData->pCreateTbReq) {
13,798,967✔
2004
      // alloc if need
2005
      if (pSubmitRsp->aCreateTbRsp == NULL &&
62,679✔
2006
          (pSubmitRsp->aCreateTbRsp = taosArrayInit(TARRAY_SIZE(pSubmitReq->aSubmitTbData), sizeof(SVCreateTbRsp))) ==
45,932!
2007
              NULL) {
2008
        code = terrno;
×
2009
        goto _exit;
8✔
2010
      }
2011

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

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

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

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

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

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

2049
    code = metaUpdateChangeTimeWithLock(pVnode->pMeta, pSubmitTbData->uid, pSubmitTbData->ctimeMs);
13,800,417✔
2050
    if (code) goto _exit;
13,799,175!
2051

2052
    pSubmitRsp->affectedRows += affectedRows;
13,799,175✔
2053
  }
2054

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

2064
_exit:
13,084,054✔
2065
  // message
2066
  pRsp->code = code;
13,084,069✔
2067
  tEncodeSize(tEncodeSSubmitRsp2, pSubmitRsp, pRsp->contLen, ret);
13,084,069!
2068
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
13,083,987✔
2069
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
13,084,096✔
2070
  if (tEncodeSSubmitRsp2(&ec, pSubmitRsp) < 0) {
13,084,098!
2071
    vError("vgId:%d, failed to encode submit response", TD_VID(pVnode));
×
2072
  }
2073
  tEncoderClear(&ec);
13,084,093✔
2074

2075
  // update statistics
2076
  (void)atomic_add_fetch_64(&pVnode->statis.nInsert, pSubmitRsp->affectedRows);
13,084,048✔
2077
  (void)atomic_add_fetch_64(&pVnode->statis.nInsertSuccess, pSubmitRsp->affectedRows);
13,084,137✔
2078
  (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsert, 1);
13,084,134✔
2079

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

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

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

2114
  // clear
2115
  taosArrayDestroy(newTbUids);
13,084,076✔
2116
  tDestroySubmitReq(pSubmitReq, 0 == pMsg->version ? TSDB_MSG_FLG_CMPT : TSDB_MSG_FLG_DECODE);
13,084,003!
2117
  tDestroySSubmitRsp2(pSubmitRsp, TSDB_MSG_FLG_ENCODE);
13,084,102✔
2118

2119
  if (code) terrno = code;
13,084,037✔
2120

2121
  taosMemoryFree(pAllocMsg);
13,084,037!
2122

2123
  return code;
13,084,027✔
2124
}
2125

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

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

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

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

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

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

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

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

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

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

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

2184
  return code;
42✔
2185
}
2186

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

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

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

2207
  return code;
8,084✔
2208
}
2209

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2346
  return 0;
633✔
2347
}
2348

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

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

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

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

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

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

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

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

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

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

2417
  tDecoderInit(pCoder, pReq, len);
62,267✔
2418
  code = tDecodeDeleteRes(pCoder, pRes);
62,266✔
2419
  if (code) goto _err;
62,271!
2420

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2535
  return 0;
×
2536
}
2537

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

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

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

2554
  terrno = TSDB_CODE_SUCCESS;
×
2555
  return 0;
×
2556
}
2557

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

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

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

2570
  SVArbCheckSyncReq syncReq = {0};
×
2571

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

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

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

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

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

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

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

2619
  pRsp->pCont = pHead;
×
2620
  pRsp->contLen = contLen;
×
2621

2622
  terrno = TSDB_CODE_SUCCESS;
×
2623

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

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

© 2026 Coveralls, Inc