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

taosdata / TDengine / #3626

28 Feb 2025 03:34AM UTC coverage: 63.764% (+0.1%) from 63.633%
#3626

push

travis-ci

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

merge: from main to 3.0 branch

149233 of 299935 branches covered (49.76%)

Branch coverage included in aggregate %.

53 of 91 new or added lines in 8 files covered. (58.24%)

3267 existing lines in 138 files now uncovered.

233601 of 300457 relevant lines covered (77.75%)

17374158.38 hits per line

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

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

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

27
extern taos_counter_t *tsInsertCounter;
28

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

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

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

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

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

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

78
  // name
79
  char *name = NULL;
157,929✔
80
  if (tDecodeCStr(pCoder, &name) < 0) {
157,894!
81
    code = TSDB_CODE_INVALID_MSG;
×
82
    TSDB_CHECK_CODE(code, lino, _exit);
×
83
  }
84

85
  // uid
86
  int64_t uid = metaGetTableEntryUidByName(pVnode->pMeta, name);
157,894✔
87
  if (uid == 0) {
157,885✔
88
    uid = tGenIdPI64();
131,288✔
89
  }
90
  *(int64_t *)(pCoder->data + pCoder->pos) = uid;
157,926✔
91

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

95
  tEndDecode(pCoder);
157,926✔
96

97
_exit:
157,876✔
98
  if (code) {
157,876!
99
    vError("vgId:%d %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
100
  } else {
101
    vTrace("vgId:%d %s done, table:%s uid generated:%" PRId64, TD_VID(pVnode), __func__, name, uid);
157,876✔
102
    if (pUid) *pUid = uid;
157,876✔
103
  }
104
  return code;
157,869✔
105
}
106
static int32_t vnodePreProcessCreateTableMsg(SVnode *pVnode, SRpcMsg *pMsg) {
71,254✔
107
  int32_t code = 0;
71,254✔
108
  int32_t lino = 0;
71,254✔
109

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

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

120
  if (tDecodeI32v(&dc, &nReqs) < 0) {
71,265!
121
    code = TSDB_CODE_INVALID_MSG;
×
122
    TSDB_CHECK_CODE(code, lino, _exit);
×
123
  }
124
  for (int32_t iReq = 0; iReq < nReqs; iReq++) {
166,499✔
125
    code = vnodePreprocessCreateTableReq(pVnode, &dc, btime, NULL);
95,270✔
126
    TSDB_CHECK_CODE(code, lino, _exit);
95,234!
127
  }
128

129
  tEndDecode(&dc);
71,229✔
130

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

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

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

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

157
  code = 0;
975✔
158

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

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

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

177
  SArray *tbUids = NULL;
94,122✔
178
  int64_t timestampMs = 0;
94,122✔
179

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

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

200
    ttlReq.nUids = taosArrayGetSize(tbUids);
94,164✔
201
    ttlReq.pTbUids = tbUids;
93,993✔
202
  }
203

204
  if (ttlReq.nUids == 0) {
93,993!
205
    code = TSDB_CODE_MSG_PREPROCESSED;
94,052✔
206
    TSDB_CHECK_CODE(code, lino, _exit);
94,052!
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:
94,084✔
233
  taosArrayDestroy(tbUids);
94,084✔
234

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

242
  return code;
94,141✔
243
}
244

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

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

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

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

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

291
  // scan and check
292
  TSKEY now = btimeMs;
10,682,694✔
293
  if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_MICRO) {
10,682,694✔
294
    now *= 1000;
22,729✔
295
  } else if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_NANO) {
10,659,965✔
296
    now *= 1000000;
3,711✔
297
  }
298

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

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

327
    if (colData.flag != HAS_VALUE) {
131!
328
      code = TSDB_CODE_INVALID_MSG;
×
329
      TSDB_CHECK_CODE(code, lino, _exit);
×
330
    }
331

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

339
    for (uint64_t i = 1; i < nColData; i++) {
569✔
340
      code = tDecodeColData(version, pCoder, &colData);
438✔
341
      if (code) {
439!
342
        code = TSDB_CODE_INVALID_MSG;
×
343
        TSDB_CHECK_CODE(code, lino, _exit);
×
344
      }
345
    }
346
  } else {
347
    uint64_t nRow;
348
    if (tDecodeU64v(pCoder, &nRow) < 0) {
10,685,630!
349
      code = TSDB_CODE_INVALID_MSG;
×
350
      TSDB_CHECK_CODE(code, lino, _exit);
497!
351
    }
352

353
    for (int32_t iRow = 0; iRow < nRow; ++iRow) {
660,241,886✔
354
      SRow *pRow = (SRow *)(pCoder->data + pCoder->pos);
649,556,753✔
355
      pCoder->pos += pRow->len;
649,556,753✔
356

357
      if (pRow->ts < minKey || pRow->ts > maxKey) {
649,556,753✔
358
        code = TSDB_CODE_TDB_TIMESTAMP_OUT_OF_RANGE;
6,324✔
359
        TSDB_CHECK_CODE(code, lino, _exit);
6,324✔
360
      }
361
    }
362
  }
363

364
  if (!tDecodeIsEnd(pCoder)) {
10,685,264!
365
    *(int64_t *)(pCoder->data + pCoder->pos) = ctimeMs;
10,686,154✔
366
    pCoder->pos += sizeof(int64_t);
10,686,154✔
367
  }
368

369
  tEndDecode(pCoder);
10,685,264✔
370

371
_exit:
10,683,986✔
372
  if (code) {
10,683,986✔
373
    vError("vgId:%d, %s:%d failed to vnodePreProcessSubmitTbData submit request since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
498!
374
  }
375
  return code;
10,684,389✔
376
}
377
static int32_t vnodePreProcessSubmitMsg(SVnode *pVnode, SRpcMsg *pMsg) {
10,018,450✔
378
  int32_t code = 0;
10,018,450✔
379
  int32_t lino = 0;
10,018,450✔
380

381
  if (tsBypassFlag & TSDB_BYPASS_RA_RPC_RECV_SUBMIT) {
10,018,450!
382
    return TSDB_CODE_MSG_PREPROCESSED;
×
383
  }
384

385
  SDecoder *pCoder = &(SDecoder){0};
10,018,450✔
386

387
  if (taosHton64(((SSubmitReq2Msg *)pMsg->pCont)->version) != 1) {
10,018,450!
388
    code = TSDB_CODE_INVALID_MSG;
×
389
    TSDB_CHECK_CODE(code, lino, _exit);
×
390
  }
391

392
  tDecoderInit(pCoder, (uint8_t *)pMsg->pCont + sizeof(SSubmitReq2Msg), pMsg->contLen - sizeof(SSubmitReq2Msg));
10,018,438✔
393

394
  if (tStartDecode(pCoder) < 0) {
10,018,428!
395
    code = TSDB_CODE_INVALID_MSG;
×
396
    TSDB_CHECK_CODE(code, lino, _exit);
×
397
  }
398

399
  uint64_t nSubmitTbData;
400
  if (tDecodeU64v(pCoder, &nSubmitTbData) < 0) {
10,018,510!
401
    code = TSDB_CODE_INVALID_MSG;
×
402
    TSDB_CHECK_CODE(code, lino, _exit);
×
403
  }
404

405
  int64_t btimeMs = taosGetTimestampMs();
10,018,444✔
406
  int64_t ctimeMs = btimeMs;
10,018,444✔
407
  for (int32_t i = 0; i < nSubmitTbData; i++) {
20,702,213✔
408
    code = vnodePreProcessSubmitTbData(pVnode, pCoder, btimeMs, ctimeMs);
10,685,146✔
409
    TSDB_CHECK_CODE(code, lino, _exit);
10,684,267✔
410
  }
411

412
  tEndDecode(pCoder);
10,017,067✔
413

414
_exit:
10,018,472✔
415
  tDecoderClear(pCoder);
10,018,472✔
416
  if (code) {
10,018,479✔
417
    vError("vgId:%d, %s:%d failed to preprocess submit request since %s, msg type:%s", TD_VID(pVnode), __func__, lino,
498!
418
           tstrerror(code), TMSG_INFO(pMsg->msgType));
419
  }
420
  return code;
10,018,453✔
421
}
422

423
static int32_t vnodePreProcessDeleteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
67,219✔
424
  int32_t code = 0;
67,219✔
425

426
  int32_t    size;
427
  int32_t    ret;
428
  uint8_t   *pCont;
429
  SEncoder  *pCoder = &(SEncoder){0};
67,219✔
430
  SDeleteRes res = {0};
67,219✔
431

432
  SReadHandle handle = {.vnode = pVnode, .pMsgCb = &pVnode->msgCb, .skipRollup = 1};
67,219✔
433
  initStorageAPI(&handle.api);
67,219✔
434

435
  code = qWorkerProcessDeleteMsg(&handle, pVnode->pQuery, pMsg, &res);
67,220✔
436
  if (code) goto _exit;
67,219!
437

438
  res.ctimeMs = taosGetTimestampMs();
67,219✔
439
  // malloc and encode
440
  tEncodeSize(tEncodeDeleteRes, &res, size, ret);
67,219!
441
  pCont = rpcMallocCont(size + sizeof(SMsgHead));
67,219✔
442

443
  ((SMsgHead *)pCont)->contLen = size + sizeof(SMsgHead);
67,220✔
444
  ((SMsgHead *)pCont)->vgId = TD_VID(pVnode);
67,220✔
445

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

452
  rpcFreeCont(pMsg->pCont);
67,218✔
453
  pMsg->pCont = pCont;
67,220✔
454
  pMsg->contLen = size + sizeof(SMsgHead);
67,220✔
455

456
  taosArrayDestroy(res.uidList);
67,220✔
457

458
_exit:
67,220✔
459
  return code;
67,220✔
460
}
461

462
static int32_t vnodePreProcessBatchDeleteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
2,729✔
463
  int32_t code = 0;
2,729✔
464
  int32_t lino = 0;
2,729✔
465

466
  int64_t         ctimeMs = taosGetTimestampMs();
2,729✔
467
  SBatchDeleteReq pReq = {0};
2,729✔
468
  SDecoder       *pCoder = &(SDecoder){0};
2,729✔
469

470
  tDecoderInit(pCoder, (uint8_t *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead));
2,729✔
471

472
  if (tDecodeSBatchDeleteReqSetCtime(pCoder, &pReq, ctimeMs) < 0) {
2,729!
473
    code = TSDB_CODE_INVALID_MSG;
×
474
  }
475

476
  tDecoderClear(pCoder);
2,727✔
477
  taosArrayDestroy(pReq.deleteReqs);
2,727✔
478

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

487
static int32_t vnodePreProcessArbCheckSyncMsg(SVnode *pVnode, SRpcMsg *pMsg) {
5✔
488
  SVArbCheckSyncReq syncReq = {0};
5✔
489

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

495
  int32_t ret = vnodeCheckToken(pVnode, syncReq.member0Token, syncReq.member1Token);
5✔
496
  if (ret != 0) {
5✔
497
    vError("vgId:%d, failed to preprocess arb check sync request since %s", TD_VID(pVnode), tstrerror(ret));
2!
498
  }
499

500
  int32_t code = terrno;
5✔
501
  tFreeSVArbCheckSyncReq(&syncReq);
5✔
502

503
  return code;
5✔
504
}
505

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

515
  tDecoderInit(&dc, POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), pMsg->contLen - sizeof(SMsgHead));
2,590✔
516

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

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

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

553
_exit:
2,590✔
554
  tDecoderClear(&dc);
2,590✔
555
  if (sentBatchReqs.pArray) {
2,590!
556
    taosArrayDestroy(sentBatchReqs.pArray);
2,590✔
557
  }
558
  return code;
2,590✔
559
}
560

561
int32_t vnodePreProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
10,364,305✔
562
  int32_t code = 0;
10,364,305✔
563

564
  switch (pMsg->msgType) {
10,364,305✔
565
    case TDMT_VND_CREATE_TABLE: {
71,266✔
566
      code = vnodePreProcessCreateTableMsg(pVnode, pMsg);
71,266✔
567
    } break;
71,258✔
568
    case TDMT_VND_ALTER_TABLE: {
975✔
569
      code = vnodePreProcessAlterTableMsg(pVnode, pMsg);
975✔
570
    } break;
975✔
571
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
94,307✔
572
    case TDMT_VND_DROP_TTL_TABLE: {
573
      code = vnodePreProcessDropTtlMsg(pVnode, pMsg);
94,307✔
574
    } break;
94,074✔
575
    case TDMT_VND_SUBMIT: {
10,018,518✔
576
      code = vnodePreProcessSubmitMsg(pVnode, pMsg);
10,018,518✔
577
    } break;
10,018,461✔
578
    case TDMT_VND_DELETE: {
67,219✔
579
      code = vnodePreProcessDeleteMsg(pVnode, pMsg);
67,219✔
580
    } break;
67,220✔
581
    case TDMT_VND_BATCH_DEL: {
2,729✔
582
      code = vnodePreProcessBatchDeleteMsg(pVnode, pMsg);
2,729✔
583
    } break;
2,727✔
584
    case TDMT_VND_ARB_CHECK_SYNC: {
5✔
585
      code = vnodePreProcessArbCheckSyncMsg(pVnode, pMsg);
5✔
586
    } break;
5✔
587
    case TDMT_VND_DROP_TABLE: {
2,590✔
588
      code = vnodePreProcessDropTbMsg(pVnode, pMsg);
2,590✔
589
    } break;
2,590✔
590
    default:
106,696✔
591
      break;
106,696✔
592
  }
593

594
  if (code && code != TSDB_CODE_MSG_PREPROCESSED) {
10,364,006✔
595
    vError("vgId:%d, failed to preprocess write request since %s, msg type:%s", TD_VID(pVnode), tstrerror(code),
500!
596
           TMSG_INFO(pMsg->msgType));
597
  }
598
  return code;
10,363,976✔
599
}
600

601
int32_t vnodeProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg, int64_t ver, SRpcMsg *pRsp) {
11,691,646✔
602
  int32_t code = 0;
11,691,646✔
603
  void   *ptr = NULL;
11,691,646✔
604
  void   *pReq;
605
  int32_t len;
606

607
  (void)taosThreadMutexLock(&pVnode->mutex);
11,691,646✔
608
  if (pVnode->disableWrite) {
11,692,110!
609
    (void)taosThreadMutexUnlock(&pVnode->mutex);
×
610
    vError("vgId:%d write is disabled for snapshot, version:%" PRId64, TD_VID(pVnode), ver);
×
611
    return TSDB_CODE_VND_WRITE_DISABLED;
×
612
  }
613
  (void)taosThreadMutexUnlock(&pVnode->mutex);
11,692,110✔
614

615
  if (ver <= pVnode->state.applied) {
11,692,025!
616
    vError("vgId:%d, duplicate write request. ver: %" PRId64 ", applied: %" PRId64 "", TD_VID(pVnode), ver,
×
617
           pVnode->state.applied);
618
    return terrno = TSDB_CODE_VND_DUP_REQUEST;
×
619
  }
620

621
  vDebug("vgId:%d, start to process write request %s, index:%" PRId64 ", applied:%" PRId64 ", state.applyTerm:%" PRId64
11,692,025!
622
         ", conn.applyTerm:%" PRId64 ", contLen:%d",
623
         TD_VID(pVnode), TMSG_INFO(pMsg->msgType), ver, pVnode->state.applied, pVnode->state.applyTerm,
624
         pMsg->info.conn.applyTerm, pMsg->contLen);
625

626
  if (!(pVnode->state.applyTerm <= pMsg->info.conn.applyTerm)) {
11,691,750!
627
    return terrno = TSDB_CODE_INTERNAL_ERROR;
×
628
  }
629

630
  if (!(pVnode->state.applied + 1 == ver)) {
11,691,750!
631
    return terrno = TSDB_CODE_INTERNAL_ERROR;
×
632
  }
633

634
  atomic_store_64(&pVnode->state.applied, ver);
11,691,750✔
635
  atomic_store_64(&pVnode->state.applyTerm, pMsg->info.conn.applyTerm);
11,692,144✔
636

637
  if (!syncUtilUserCommit(pMsg->msgType)) goto _exit;
11,692,149✔
638

639
  // skip header
640
  pReq = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
11,665,622✔
641
  len = pMsg->contLen - sizeof(SMsgHead);
11,665,622✔
642
  bool needCommit = false;
11,665,622✔
643

644
  switch (pMsg->msgType) {
11,665,622!
645
    /* META */
646
    case TDMT_VND_CREATE_STB:
28,144✔
647
      if (vnodeProcessCreateStbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
28,144✔
648
      break;
28,213✔
649
    case TDMT_VND_ALTER_STB:
6,552✔
650
      if (vnodeProcessAlterStbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
6,552!
651
      break;
6,596✔
652
    case TDMT_VND_DROP_STB:
2,432✔
653
      if (vnodeProcessDropStbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
2,432!
654
      break;
2,438✔
655
    case TDMT_VND_CREATE_TABLE:
80,026✔
656
      if (vnodeProcessCreateTbReq(pVnode, ver, pReq, len, pRsp, pMsg) < 0) goto _err;
80,026!
657
      break;
80,051✔
658
    case TDMT_VND_ALTER_TABLE:
970✔
659
      if (vnodeProcessAlterTbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
970!
660
      break;
970✔
661
    case TDMT_VND_DROP_TABLE:
2,632✔
662
      if (vnodeProcessDropTbReq(pVnode, ver, pReq, len, pRsp, pMsg) < 0) goto _err;
2,632!
663
      break;
2,632✔
664
    case TDMT_VND_DROP_TTL_TABLE:
×
665
      if (vnodeProcessDropTtlTbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
666
      break;
×
667
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
5✔
668
      if (vnodeProcessFetchTtlExpiredTbs(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
5!
669
      break;
5✔
670
    case TDMT_VND_TRIM:
209✔
671
      if (vnodeProcessTrimReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
209!
672
      break;
208✔
673
    case TDMT_VND_S3MIGRATE:
×
674
      if (vnodeProcessS3MigrateReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
675
      break;
×
676
    case TDMT_VND_CREATE_SMA:
31✔
677
      if (vnodeProcessCreateTSmaReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
31!
678
      break;
31✔
679
    /* TSDB */
680
    case TDMT_VND_SUBMIT:
11,381,725✔
681
      if (vnodeProcessSubmitReq(pVnode, ver, pMsg->pCont, pMsg->contLen, pRsp, pMsg) < 0) goto _err;
11,381,725✔
682
      break;
11,381,647✔
683
    case TDMT_VND_DELETE:
68,526✔
684
      if (vnodeProcessDeleteReq(pVnode, ver, pReq, len, pRsp, pMsg) < 0) goto _err;
68,526!
685
      break;
68,523✔
686
    case TDMT_VND_BATCH_DEL:
3,003✔
687
      if (vnodeProcessBatchDeleteReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
3,003!
688
      break;
3,003✔
689
    /* TQ */
690
    case TDMT_VND_TMQ_SUBSCRIBE:
3,103✔
691
      if (tqProcessSubscribeReq(pVnode->pTq, ver, pReq, len) < 0) {
3,103✔
692
        goto _err;
1✔
693
      }
694
      break;
3,106✔
695
    case TDMT_VND_TMQ_DELETE_SUB:
859✔
696
      if (tqProcessDeleteSubReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen) < 0) {
859!
697
        goto _err;
×
698
      }
699
      break;
861✔
700
    case TDMT_VND_TMQ_COMMIT_OFFSET:
10,884✔
701
      if (tqProcessOffsetCommitReq(pVnode->pTq, ver, pReq, len) < 0) {
10,884!
702
        goto _err;
×
703
      }
704
      break;
10,894✔
705
    case TDMT_VND_TMQ_ADD_CHECKINFO:
115✔
706
      if (tqProcessAddCheckInfoReq(pVnode->pTq, ver, pReq, len) < 0) {
115!
707
        goto _err;
×
708
      }
709
      break;
116✔
710
    case TDMT_VND_TMQ_DEL_CHECKINFO:
10✔
711
      if (tqProcessDelCheckInfoReq(pVnode->pTq, ver, pReq, len) < 0) {
10!
712
        goto _err;
×
713
      }
714
      break;
10✔
715
    case TDMT_STREAM_TASK_DEPLOY: {
14,366✔
716
      if ((code = tqProcessTaskDeployReq(pVnode->pTq, ver, pReq, len)) != TSDB_CODE_SUCCESS) {
14,366!
717
        goto _err;
×
718
      }
719
    } break;
14,383✔
720
    case TDMT_STREAM_TASK_DROP: {
6,959✔
721
      if ((code = tqProcessTaskDropReq(pVnode->pTq, pMsg->pCont, pMsg->contLen)) < 0) {
6,959!
722
        goto _err;
×
723
      }
724
    } break;
6,987✔
725
    case TDMT_STREAM_TASK_UPDATE_CHKPT: {
5,300✔
726
      if ((code = tqProcessTaskUpdateCheckpointReq(pVnode->pTq, pMsg->pCont, pMsg->contLen)) < 0) {
5,300!
727
        goto _err;
×
728
      }
729
    } break;
5,308✔
730
    case TDMT_STREAM_CONSEN_CHKPT: {
223✔
731
      if (pVnode->restored && (code = tqProcessTaskConsenChkptIdReq(pVnode->pTq, pMsg)) < 0) {
223!
732
        goto _err;
×
733
      }
734

735
    } break;
223✔
736
    case TDMT_STREAM_TASK_PAUSE: {
2,137✔
737
      if (pVnode->restored && vnodeIsLeader(pVnode) &&
3,536!
738
          (code = tqProcessTaskPauseReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen)) < 0) {
1,391✔
739
        goto _err;
×
740
      }
741
    } break;
2,141✔
742
    case TDMT_STREAM_TASK_RESUME: {
3,800✔
743
      if (pVnode->restored && vnodeIsLeader(pVnode) &&
6,367!
744
          (code = tqProcessTaskResumeReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen)) < 0) {
2,565✔
745
        goto _err;
×
746
      }
747
    } break;
3,810✔
748
    case TDMT_VND_STREAM_TASK_RESET: {
10✔
749
      if (pVnode->restored && vnodeIsLeader(pVnode) && (code = tqProcessTaskResetReq(pVnode->pTq, pMsg)) < 0) {
10!
750
        goto _err;
×
751
      }
752

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

798
  vTrace("vgId:%d, process %s request, code:0x%x index:%" PRId64, TD_VID(pVnode), TMSG_INFO(pMsg->msgType), pRsp->code,
11,665,580!
799
         ver);
800

801
  walApplyVer(pVnode->pWal, ver);
11,665,580✔
802

803
  code = tqPushMsg(pVnode->pTq, pMsg->msgType);
11,665,560✔
804
  if (code) {
11,665,708✔
805
    vError("vgId:%d, failed to push msg to TQ since %s", TD_VID(pVnode), tstrerror(terrno));
584!
806
    return code;
584✔
807
  }
808

809
  // commit if need
810
  if (needCommit) {
11,665,124✔
811
    vInfo("vgId:%d, commit at version %" PRId64, TD_VID(pVnode), ver);
27,773!
812
    code = vnodeAsyncCommit(pVnode);
27,792✔
813
    if (code) {
27,796!
814
      vError("vgId:%d, failed to vnode async commit since %s.", TD_VID(pVnode), tstrerror(terrno));
×
815
      goto _err;
×
816
    }
817

818
    // start a new one
819
    code = vnodeBegin(pVnode);
27,796✔
820
    if (code) {
27,788✔
821
      vError("vgId:%d, failed to begin vnode since %s.", TD_VID(pVnode), tstrerror(terrno));
46!
822
      goto _err;
×
823
    }
824
  }
825

826
_exit:
11,665,093✔
827
  return 0;
11,691,222✔
828

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

835
int32_t vnodePreprocessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg) {
8,109,215✔
836
  if (TDMT_SCH_QUERY != pMsg->msgType && TDMT_SCH_MERGE_QUERY != pMsg->msgType) {
8,109,215✔
837
    return 0;
527,578✔
838
  }
839

840
  return qWorkerPreprocessQueryMsg(pVnode->pQuery, pMsg, TDMT_SCH_QUERY == pMsg->msgType);
7,581,637✔
841
}
842

843
int32_t vnodeProcessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
8,119,422✔
844
  vTrace("message in vnode query queue is processing");
8,119,422✔
845
  if (pMsg->msgType == TDMT_VND_TMQ_CONSUME && !syncIsReadyForRead(pVnode->sync)) {
8,119,422✔
846
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
1,260✔
847
    return 0;
1,260✔
848
  }
849

850
  if (pMsg->msgType == TDMT_VND_TMQ_CONSUME && !pVnode->restored) {
8,118,161!
851
    vnodeRedirectRpcMsg(pVnode, pMsg, TSDB_CODE_SYN_RESTORING);
×
852
    return 0;
×
853
  }
854

855
  SReadHandle handle = {.vnode = pVnode, .pMsgCb = &pVnode->msgCb, .pWorkerCb = pInfo->workerCb};
8,118,161✔
856
  initStorageAPI(&handle.api);
8,118,161✔
857
  int32_t code = TSDB_CODE_SUCCESS;
8,117,795✔
858
  bool    redirected = false;
8,117,795✔
859

860
  switch (pMsg->msgType) {
8,117,795!
861
    case TDMT_SCH_QUERY:
5,853,635✔
862
      if (!syncIsReadyForRead(pVnode->sync)) {
5,853,635✔
863
        pMsg->code = (terrno) ? terrno : TSDB_CODE_SYN_NOT_LEADER;
2,575!
864
        redirected = true;
2,575✔
865
      }
866
      code = qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
5,852,335✔
867
      if (redirected) {
5,852,246✔
868
        vnodeRedirectRpcMsg(pVnode, pMsg, pMsg->code);
2,690✔
869
        return 0;
2,575✔
870
      }
871
      return code;
5,849,556✔
872
    case TDMT_SCH_MERGE_QUERY:
1,738,036✔
873
      return qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
1,738,036✔
874
    case TDMT_SCH_QUERY_CONTINUE:
441,744✔
875
      return qWorkerProcessCQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
441,744✔
876
    case TDMT_VND_TMQ_CONSUME:
63,257✔
877
      return tqProcessPollReq(pVnode->pTq, pMsg);
63,257✔
878
    case TDMT_VND_TMQ_CONSUME_PUSH:
21,321✔
879
      return tqProcessPollPush(pVnode->pTq, pMsg);
21,321✔
880
    default:
×
881
      vError("unknown msg type:%d in query queue", pMsg->msgType);
×
882
      return TSDB_CODE_APP_ERROR;
×
883
  }
884
}
885

886
int32_t vnodeProcessFetchMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
20,460,932✔
887
  vTrace("vgId:%d, msg:%p in fetch queue is processing", pVnode->config.vgId, pMsg);
20,460,932✔
888
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
20,460,934✔
889
       pMsg->msgType == TDMT_VND_BATCH_META || pMsg->msgType == TDMT_VND_TABLE_NAME) &&
14,192,270✔
890
      !syncIsReadyForRead(pVnode->sync)) {
7,383,550✔
891
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
2,166✔
892
    return 0;
2,166✔
893
  }
894

895
  switch (pMsg->msgType) {
20,459,916!
896
    case TDMT_SCH_FETCH:
9,789,094✔
897
    case TDMT_SCH_MERGE_FETCH:
898
      return qWorkerProcessFetchMsg(pVnode, pVnode->pQuery, pMsg, 0);
9,789,094✔
899
    case TDMT_SCH_FETCH_RSP:
×
900
      return qWorkerProcessRspMsg(pVnode, pVnode->pQuery, pMsg, 0);
×
901
    // case TDMT_SCH_CANCEL_TASK:
902
    //   return qWorkerProcessCancelMsg(pVnode, pVnode->pQuery, pMsg, 0);
903
    case TDMT_SCH_DROP_TASK:
7,582,410✔
904
      return qWorkerProcessDropMsg(pVnode, pVnode->pQuery, pMsg, 0);
7,582,410✔
905
    case TDMT_SCH_TASK_NOTIFY:
44✔
906
      return qWorkerProcessNotifyMsg(pVnode, pVnode->pQuery, pMsg, 0);
44✔
907
    case TDMT_SCH_QUERY_HEARTBEAT:
1,980,411✔
908
      return qWorkerProcessHbMsg(pVnode, pVnode->pQuery, pMsg, 0);
1,980,411✔
909
    case TDMT_VND_TABLE_META:
2,077✔
910
    case TDMT_VND_TABLE_NAME:
911
      return vnodeGetTableMeta(pVnode, pMsg, true);
2,077✔
912
    case TDMT_VND_TABLE_CFG:
×
913
      return vnodeGetTableCfg(pVnode, pMsg, true);
×
914
    case TDMT_VND_BATCH_META:
1,105,793✔
915
      return vnodeGetBatchMeta(pVnode, pMsg);
1,105,793✔
916
#ifdef TD_ENTERPRISE
917
    case TDMT_VND_QUERY_COMPACT_PROGRESS:
48✔
918
      return vnodeQueryCompactProgress(pVnode, pMsg);
48✔
919
#endif
920
      //    case TDMT_VND_TMQ_CONSUME:
921
      //      return tqProcessPollReq(pVnode->pTq, pMsg);
922
    case TDMT_VND_TMQ_VG_WALINFO:
13✔
923
      return tqProcessVgWalInfoReq(pVnode->pTq, pMsg);
13✔
924
    case TDMT_VND_TMQ_VG_COMMITTEDINFO:
2✔
925
      return tqProcessVgCommittedInfoReq(pVnode->pTq, pMsg);
2✔
926
    case TDMT_VND_TMQ_SEEK:
24✔
927
      return tqProcessSeekReq(pVnode->pTq, pMsg);
24✔
928

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

935
int32_t vnodeProcessStreamMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
170,655✔
936
  vTrace("vgId:%d, msg:%p in stream queue is processing", pVnode->config.vgId, pMsg);
170,655✔
937
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
170,655!
938
       pMsg->msgType == TDMT_VND_BATCH_META) &&
170,697✔
939
      !syncIsReadyForRead(pVnode->sync)) {
×
940
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
×
941
    return 0;
×
942
  }
943

944
  switch (pMsg->msgType) {
170,694!
945
    case TDMT_STREAM_TASK_RUN:
167,103✔
946
      return tqProcessTaskRunReq(pVnode->pTq, pMsg);
167,103✔
947
    case TDMT_STREAM_RETRIEVE:
562✔
948
      return tqProcessTaskRetrieveReq(pVnode->pTq, pMsg);
562✔
949
    case TDMT_STREAM_RETRIEVE_RSP:
448✔
950
      return tqProcessTaskRetrieveRsp(pVnode->pTq, pMsg);
448✔
951
    case TDMT_VND_STREAM_SCAN_HISTORY:
2,581✔
952
      return tqProcessTaskScanHistory(pVnode->pTq, pMsg);
2,581✔
953
    case TDMT_VND_GET_STREAM_PROGRESS:
×
954
      return tqStreamProgressRetrieveReq(pVnode->pTq, pMsg);
×
955
    default:
×
956
      vError("unknown msg type:%d in stream queue", pMsg->msgType);
×
957
      return TSDB_CODE_APP_ERROR;
×
958
  }
959
}
960

961
int32_t vnodeProcessStreamCtrlMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
210,438✔
962
  vTrace("vgId:%d, msg:%p in stream ctrl queue is processing", pVnode->config.vgId, pMsg);
210,438✔
963
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
210,438!
964
       pMsg->msgType == TDMT_VND_BATCH_META) &&
210,438!
965
      !syncIsReadyForRead(pVnode->sync)) {
×
966
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
×
967
    return 0;
×
968
  }
969

970
  switch (pMsg->msgType) {
210,438!
971
    case TDMT_MND_STREAM_HEARTBEAT_RSP:
22,997✔
972
      return tqProcessStreamHbRsp(pVnode->pTq, pMsg);
22,997✔
973
    case TDMT_STREAM_TASK_DISPATCH:
58,510✔
974
      return tqProcessTaskDispatchReq(pVnode->pTq, pMsg);
58,510✔
975
    case TDMT_STREAM_TASK_DISPATCH_RSP:
58,470✔
976
      return tqProcessTaskDispatchRsp(pVnode->pTq, pMsg);
58,470✔
977
    case TDMT_VND_STREAM_TASK_CHECK:
22,284✔
978
      return tqProcessTaskCheckReq(pVnode->pTq, pMsg);
22,284✔
979
    case TDMT_VND_STREAM_TASK_CHECK_RSP:
22,583✔
980
      return tqProcessTaskCheckRsp(pVnode->pTq, pMsg);
22,583✔
981
    case TDMT_STREAM_TASK_CHECKPOINT_READY:
7,656✔
982
      return tqProcessTaskCheckpointReadyMsg(pVnode->pTq, pMsg);
7,656✔
983
    case TDMT_STREAM_TASK_CHECKPOINT_READY_RSP:
7,653✔
984
      return tqProcessTaskCheckpointReadyRsp(pVnode->pTq, pMsg);
7,653✔
985
    case TDMT_STREAM_RETRIEVE_TRIGGER:
2✔
986
      return tqProcessTaskRetrieveTriggerReq(pVnode->pTq, pMsg);
2✔
987
    case TDMT_STREAM_RETRIEVE_TRIGGER_RSP:
2✔
988
      return tqProcessTaskRetrieveTriggerRsp(pVnode->pTq, pMsg);
2✔
989
    case TDMT_MND_STREAM_REQ_CHKPT_RSP:
4,419✔
990
      return tqProcessStreamReqCheckpointRsp(pVnode->pTq, pMsg);
4,419✔
991
    case TDMT_MND_STREAM_CHKPT_REPORT_RSP:
5,862✔
992
      return tqProcessTaskChkptReportRsp(pVnode->pTq, pMsg);
5,862✔
993
    default:
×
994
      vError("unknown msg type:%d in stream ctrl queue", pMsg->msgType);
×
995
      return TSDB_CODE_APP_ERROR;
×
996
  }
997
}
998

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

1006
void vnodeUpdateMetaRsp(SVnode *pVnode, STableMetaRsp *pMetaRsp) {
155,750✔
1007
  if (NULL == pMetaRsp) {
155,750!
1008
    return;
×
1009
  }
1010

1011
  tstrncpy(pMetaRsp->dbFName, pVnode->config.dbname, TSDB_DB_FNAME_LEN);
155,750✔
1012
  pMetaRsp->dbId = pVnode->config.dbId;
155,750✔
1013
  pMetaRsp->vgId = TD_VID(pVnode);
155,750✔
1014
  pMetaRsp->precision = pVnode->config.tsdbCfg.precision;
155,750✔
1015
}
1016

1017
extern int32_t vnodeAsyncRetention(SVnode *pVnode, int64_t now);
1018

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

1025
  int32_t     code = 0;
202✔
1026
  SVTrimDbReq trimReq = {0};
202✔
1027

1028
  // decode
1029
  if (tDeserializeSVTrimDbReq(pReq, len, &trimReq) != 0) {
202!
1030
    code = TSDB_CODE_INVALID_MSG;
×
1031
    goto _exit;
×
1032
  }
1033

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

1036
  code = vnodeAsyncRetention(pVnode, trimReq.timestamp);
202✔
1037

1038
_exit:
201✔
1039
  return code;
201✔
1040
}
1041

1042
extern int32_t vnodeAsyncS3Migrate(SVnode *pVnode, int64_t now);
1043

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

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

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

1056
  code = vnodeAsyncS3Migrate(pVnode, s3migrateReq.timestamp);
×
1057

1058
_exit:
×
1059
  return code;
×
1060
}
1061

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

1070
  if (ttlReq.nUids != taosArrayGetSize(ttlReq.pTbUids)) {
×
1071
    ret = TSDB_CODE_INVALID_MSG;
×
1072
    goto end;
×
1073
  }
1074

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

1080
  if (ttlReq.nUids > 0) {
×
1081
    int32_t code = metaDropMultipleTables(pVnode->pMeta, ver, ttlReq.pTbUids);
×
1082
    if (code) return code;
×
1083

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

1090
end:
×
1091
  taosArrayDestroy(ttlReq.pTbUids);
×
1092
  return ret;
×
1093
}
1094

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

1107
  if (tDeserializeSVDropTtlTableReq(pReq, len, &ttlReq) != 0) {
5!
1108
    terrno = TSDB_CODE_INVALID_MSG;
×
1109
    goto _end;
×
1110
  }
1111

1112
  if (!(ttlReq.nUids == taosArrayGetSize(ttlReq.pTbUids))) {
5!
1113
    terrno = TSDB_CODE_INVALID_MSG;
×
1114
    goto _end;
×
1115
  }
1116

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

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

1142
    expiredTb.name = p;
5✔
1143
    if (mr.me.type == TSDB_CHILD_TABLE) {
5!
1144
      expiredTb.suid = mr.me.ctbEntry.suid;
5✔
1145
    }
1146

1147
    if (taosArrayPush(rsp.pExpiredTbs, &expiredTb) == NULL) {
10!
1148
      goto _end;
×
1149
    }
1150
  }
1151

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

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

1174
static int32_t vnodeProcessCreateStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
28,137✔
1175
  int32_t        code = 0;
28,137✔
1176
  SVCreateStbReq req = {0};
28,137✔
1177
  SDecoder       coder;
1178

1179
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
28,137✔
1180
  pRsp->code = TSDB_CODE_SUCCESS;
28,137✔
1181
  pRsp->pCont = NULL;
28,137✔
1182
  pRsp->contLen = 0;
28,137✔
1183

1184
  // decode and process req
1185
  tDecoderInit(&coder, pReq, len);
28,137✔
1186

1187
  code = tDecodeSVCreateStbReq(&coder, &req);
28,145✔
1188
  if (code) {
28,086!
1189
    pRsp->code = code;
×
1190
    goto _err;
×
1191
  }
1192

1193
  code = metaCreateSuperTable(pVnode->pMeta, ver, &req);
28,086✔
1194
  if (code) {
28,255✔
1195
    pRsp->code = code;
3✔
1196
    goto _err;
3✔
1197
  }
1198

1199
  if ((code = tdProcessRSmaCreate(pVnode->pSma, &req)) < 0) {
28,252!
1200
    pRsp->code = code;
×
1201
    goto _err;
×
1202
  }
1203

1204
  tDecoderClear(&coder);
28,244✔
1205
  return 0;
28,199✔
1206

1207
_err:
3✔
1208
  tDecoderClear(&coder);
3✔
1209
  return code;
3✔
1210
}
1211

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

1230
  // decode
1231
  tDecoderInit(&decoder, pReq, len);
80,020✔
1232
  if (tDecodeSVCreateTbBatchReq(&decoder, &req) < 0) {
80,018!
1233
    rcode = -1;
×
1234
    terrno = TSDB_CODE_INVALID_MSG;
×
1235
    goto _exit;
×
1236
  }
1237

1238
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(cRsp));
80,041✔
1239
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
80,049✔
1240
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
80,055✔
1241
  if (rsp.pArray == NULL || tbUids == NULL || tbNames == NULL) {
80,056!
UNCOV
1242
    rcode = -1;
×
UNCOV
1243
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1244
    goto _exit;
×
1245
  }
1246

1247
  // loop to create table
1248
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
199,557✔
1249
    pCreateReq = req.pReqs + iReq;
119,490✔
1250
    memset(&cRsp, 0, sizeof(cRsp));
119,490✔
1251

1252
    if (tsEnableAudit && tsEnableAuditCreateTable) {
119,490!
1253
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
119,484!
1254
      if (str == NULL) {
119,483!
1255
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1256
        rcode = -1;
×
1257
        goto _exit;
×
1258
      }
1259
      tstrncpy(str, pCreateReq->name, TSDB_TABLE_FNAME_LEN);
119,483✔
1260
      if (taosArrayPush(tbNames, &str) == NULL) {
119,489!
1261
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1262
        rcode = -1;
×
1263
        goto _exit;
×
1264
      }
1265
    }
1266

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

1280
    // do create table
1281
    if (metaCreateTable2(pVnode->pMeta, ver, pCreateReq, &cRsp.pMeta) < 0) {
119,471✔
1282
      if (pCreateReq->flags & TD_CREATE_IF_NOT_EXISTS && terrno == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
1,396!
1283
        cRsp.code = TSDB_CODE_SUCCESS;
4✔
1284
      } else {
1285
        cRsp.code = terrno;
1,392✔
1286
      }
1287
    } else {
1288
      cRsp.code = TSDB_CODE_SUCCESS;
118,080✔
1289
      if (tdFetchTbUidList(pVnode->pSma, &pStore, pCreateReq->ctb.suid, pCreateReq->uid) < 0) {
118,080!
1290
        vError("vgId:%d, failed to fetch tbUid list", TD_VID(pVnode));
×
1291
      }
1292
      if (taosArrayPush(tbUids, &pCreateReq->uid) == NULL) {
236,159!
1293
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1294
        rcode = -1;
×
1295
        goto _exit;
×
1296
      }
1297
      vnodeUpdateMetaRsp(pVnode, cRsp.pMeta);
118,079✔
1298
    }
1299

1300
    if (taosArrayPush(rsp.pArray, &cRsp) == NULL) {
238,952!
1301
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1302
      rcode = -1;
×
1303
      goto _exit;
×
1304
    }
1305
  }
1306

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

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

1330
  if (tsEnableAudit && tsEnableAuditCreateTable) {
80,052!
1331
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
80,051✔
1332

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

1338
    SStringBuilder sb = {0};
80,049✔
1339
    for (int32_t i = 0; i < tbNames->size; i++) {
199,514✔
1340
      char **key = (char **)taosArrayGet(tbNames, i);
119,468✔
1341
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
119,468✔
1342
      if (i < tbNames->size - 1) {
119,465✔
1343
        taosStringBuilderAppendChar(&sb, ',');
39,421✔
1344
      }
1345
      // taosMemoryFreeClear(*key);
1346
    }
1347

1348
    size_t len = 0;
80,046✔
1349
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
80,046✔
1350

1351
    if (pOriginRpc->info.conn.user != NULL && strlen(pOriginRpc->info.conn.user) > 0) {
80,047!
1352
      auditAddRecord(pOriginRpc, clusterId, "createTable", name.dbname, "", keyJoined, len);
64,424✔
1353
    }
1354

1355
    taosStringBuilderDestroy(&sb);
80,047✔
1356
  }
1357

1358
_exit:
1✔
1359
  tDeleteSVCreateTbBatchReq(&req);
80,048✔
1360
  taosArrayDestroyEx(rsp.pArray, tFreeSVCreateTbRsp);
80,053✔
1361
  taosArrayDestroy(tbUids);
80,055✔
1362
  tDecoderClear(&decoder);
80,051✔
1363
  tEncoderClear(&encoder);
80,053✔
1364
  taosArrayDestroyP(tbNames, NULL);
80,055✔
1365
  return rcode;
80,053✔
1366
}
1367

1368
static int32_t vnodeProcessAlterStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
6,544✔
1369
  int32_t        code = 0;
6,544✔
1370
  SVCreateStbReq req = {0};
6,544✔
1371
  SDecoder       dc = {0};
6,544✔
1372

1373
  pRsp->msgType = TDMT_VND_ALTER_STB_RSP;
6,544✔
1374
  pRsp->code = TSDB_CODE_SUCCESS;
6,544✔
1375
  pRsp->pCont = NULL;
6,544✔
1376
  pRsp->contLen = 0;
6,544✔
1377

1378
  tDecoderInit(&dc, pReq, len);
6,544✔
1379

1380
  // decode req
1381
  code = tDecodeSVCreateStbReq(&dc, &req);
6,548✔
1382
  if (code) {
6,588!
1383
    tDecoderClear(&dc);
×
1384
    return code;
×
1385
  }
1386

1387
  code = metaAlterSuperTable(pVnode->pMeta, ver, &req);
6,588✔
1388
  if (code) {
6,596!
1389
    pRsp->code = code;
×
1390
    tDecoderClear(&dc);
×
1391
    return code;
×
1392
  }
1393

1394
  tDecoderClear(&dc);
6,596✔
1395

1396
  return 0;
6,596✔
1397
}
1398

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

1405
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
2,430✔
1406
  pRsp->pCont = NULL;
2,430✔
1407
  pRsp->contLen = 0;
2,430✔
1408

1409
  // decode request
1410
  tDecoderInit(&decoder, pReq, len);
2,430✔
1411
  if (tDecodeSVDropStbReq(&decoder, &req) < 0) {
2,432!
1412
    rcode = TSDB_CODE_INVALID_MSG;
×
1413
    goto _exit;
×
1414
  }
1415

1416
  // process request
1417
  tbUidList = taosArrayInit(8, sizeof(int64_t));
2,435✔
1418
  if (tbUidList == NULL) goto _exit;
2,436!
1419
  if (metaDropSuperTable(pVnode->pMeta, ver, &req) < 0) {
2,436✔
1420
    rcode = terrno;
25✔
1421
    goto _exit;
25✔
1422
  }
1423

1424
  if (tqUpdateTbUidList(pVnode->pTq, tbUidList, false) < 0) {
2,413!
1425
    rcode = terrno;
×
1426
    goto _exit;
×
1427
  }
1428

1429
  if (tdProcessRSmaDrop(pVnode->pSma, &req) < 0) {
2,413!
1430
    rcode = terrno;
×
1431
    goto _exit;
×
1432
  }
1433

1434
  // return rsp
1435
_exit:
2,413✔
1436
  if (tbUidList) taosArrayDestroy(tbUidList);
2,438!
1437
  pRsp->code = rcode;
2,438✔
1438
  tDecoderClear(&decoder);
2,438✔
1439
  return 0;
2,438✔
1440
}
1441

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

1452
  pRsp->msgType = TDMT_VND_ALTER_TABLE_RSP;
970✔
1453
  pRsp->pCont = NULL;
970✔
1454
  pRsp->contLen = 0;
970✔
1455
  pRsp->code = TSDB_CODE_SUCCESS;
970✔
1456

1457
  tDecoderInit(&dc, pReq, len);
970✔
1458

1459
  // decode
1460
  if (tDecodeSVAlterTbReq(&dc, &vAlterTbReq) < 0) {
970!
1461
    vAlterTbRsp.code = TSDB_CODE_INVALID_MSG;
×
1462
    tDecoderClear(&dc);
×
1463
    goto _exit;
×
1464
  }
1465

1466
  // process
1467
  if (metaAlterTable(pVnode->pMeta, ver, &vAlterTbReq, &vMetaRsp) < 0) {
970✔
1468
    vAlterTbRsp.code = terrno;
38✔
1469
    tDecoderClear(&dc);
38✔
1470
    goto _exit;
38✔
1471
  }
1472
  tDecoderClear(&dc);
932✔
1473

1474
  if (NULL != vMetaRsp.pSchemas) {
932✔
1475
    vnodeUpdateMetaRsp(pVnode, &vMetaRsp);
191✔
1476
    vAlterTbRsp.pMeta = &vMetaRsp;
191✔
1477
  }
1478

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

1487
    SArray* tbUids = taosArrayInit(4, sizeof(int64_t));
658✔
1488
    void* p = taosArrayPush(tbUids, &uid);
658✔
1489
    TSDB_CHECK_NULL(p, code, lino, _exit, terrno);
658!
1490

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

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

1501
    taosArrayDestroy(tbUids);
658✔
1502
  }
1503

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

1513
  tEncoderClear(&ec);
970✔
1514
  if (vMetaRsp.pSchemas) {
970✔
1515
    taosMemoryFree(vMetaRsp.pSchemas);
191!
1516
    taosMemoryFree(vMetaRsp.pSchemaExt);
191!
1517
  }
1518
  return 0;
970✔
1519
}
1520

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

1532
  pRsp->msgType = TDMT_VND_DROP_TABLE_RSP;
2,632✔
1533
  pRsp->pCont = NULL;
2,632✔
1534
  pRsp->contLen = 0;
2,632✔
1535
  pRsp->code = TSDB_CODE_SUCCESS;
2,632✔
1536

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

1546
  // process req
1547
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
2,632✔
1548
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(SVDropTbRsp));
2,632✔
1549
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
2,632✔
1550
  if (tbUids == NULL || rsp.pArray == NULL || tbNames == NULL) goto _exit;
2,632!
1551

1552
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
5,289✔
1553
    SVDropTbReq *pDropTbReq = req.pReqs + iReq;
2,657✔
1554
    SVDropTbRsp  dropTbRsp = {0};
2,657✔
1555
    tb_uid_t     tbUid = 0;
2,657✔
1556

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

1574
    if (taosArrayPush(rsp.pArray, &dropTbRsp) == NULL) {
5,314!
1575
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1576
      pRsp->code = terrno;
×
1577
      goto _exit;
×
1578
    }
1579

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

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

1599
  if (tdUpdateTbUidList(pVnode->pSma, pStore, false) < 0) {
2,632!
1600
    goto _exit;
×
1601
  }
1602

1603
  if (tsEnableAuditCreateTable) {
2,632!
1604
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
2,632✔
1605

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

1611
    SStringBuilder sb = {0};
2,632✔
1612
    for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
5,289✔
1613
      char **key = (char **)taosArrayGet(tbNames, iReq);
2,657✔
1614
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
2,657✔
1615
      if (iReq < req.nReqs - 1) {
2,657✔
1616
        taosStringBuilderAppendChar(&sb, ',');
25✔
1617
      }
1618
      taosMemoryFreeClear(*key);
2,657!
1619
    }
1620

1621
    size_t len = 0;
2,632✔
1622
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
2,632✔
1623

1624
    if (pOriginRpc->info.conn.user != NULL && strlen(pOriginRpc->info.conn.user) > 0) {
2,632!
1625
      auditAddRecord(pOriginRpc, clusterId, "dropTable", name.dbname, "", keyJoined, len);
2,535✔
1626
    }
1627

1628
    taosStringBuilderDestroy(&sb);
2,632✔
1629
  }
1630

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

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

1656
  tInitSubmitBlkIter(msgIter, pBlock, &blkIter);
1657
  if (blkIter.row == NULL) return 0;
1658

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

1666
  suid = msgIter->suid;
1667
  rv = TD_ROW_SVER(blkIter.row);
1668

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

1675
  taosMemoryFreeClear(pSchema);
1676

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

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

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

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

1729
  return TSDB_CODE_SUCCESS;
×
1730
}
1731

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

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

1745
  if (tdValTypeIsNull(pCellVal->valType)) {
×
1746
    pColVal->flag = CV_FLAG_NULL;
×
1747
    return TSDB_CODE_SUCCESS;
×
1748
  }
1749

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

1762
  pColVal->flag = CV_FLAG_VALUE;
×
1763
  return TSDB_CODE_SUCCESS;
×
1764
}
1765

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

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

1785
  pCxt->pTbData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
×
1786
  if (NULL == pCxt->pTbData->pCreateTbReq) {
×
1787
    return terrno;
×
1788
  }
1789

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

1795
  return code;
×
1796
}
1797

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

1804
  SSubmitReqConvertCxt cxt = {0};
×
1805

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

1836
  vnodeDestroySubmitReqConvertCxt(&cxt);
×
1837
  return code;
×
1838
}
1839

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

1863
static int32_t vnodeProcessSubmitReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
11,381,711✔
1864
                                     SRpcMsg *pOriginalMsg) {
1865
  int32_t code = 0;
11,381,711✔
1866
  terrno = 0;
11,381,711✔
1867

1868
  SSubmitReq2 *pSubmitReq = &(SSubmitReq2){0};
11,381,820✔
1869
  SSubmitRsp2 *pSubmitRsp = &(SSubmitRsp2){0};
11,381,820✔
1870
  SArray      *newTbUids = NULL;
11,381,820✔
1871
  int32_t      ret;
1872
  SEncoder     ec = {0};
11,381,820✔
1873

1874
  pRsp->code = TSDB_CODE_SUCCESS;
11,381,820✔
1875

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

1902
  // scan
1903
  TSKEY now = taosGetTimestamp(pVnode->config.tsdbCfg.precision);
11,381,757✔
1904
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * pVnode->config.tsdbCfg.keep2;
11,381,803✔
1905
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
11,381,803✔
1906
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
23,459,710✔
1907
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
12,078,643✔
1908

1909
    if (pSubmitTbData->pCreateTbReq && pSubmitTbData->pCreateTbReq->uid == 0) {
12,078,662!
1910
      code = TSDB_CODE_INVALID_MSG;
×
1911
      goto _exit;
×
1912
    }
1913

1914
    if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
12,078,662✔
1915
      if (TARRAY_SIZE(pSubmitTbData->aCol) <= 0) {
135!
1916
        code = TSDB_CODE_INVALID_MSG;
×
1917
        goto _exit;
×
1918
      }
1919

1920
      SColData *colDataArr = TARRAY_DATA(pSubmitTbData->aCol);
135✔
1921
      SRowKey   lastKey;
1922
      tColDataArrGetRowKey(colDataArr, TARRAY_SIZE(pSubmitTbData->aCol), 0, &lastKey);
135✔
1923
      for (int32_t iRow = 1; iRow < colDataArr[0].nVal; iRow++) {
252✔
1924
        SRowKey key;
1925
        tColDataArrGetRowKey(TARRAY_DATA(pSubmitTbData->aCol), TARRAY_SIZE(pSubmitTbData->aCol), iRow, &key);
117✔
1926
        if (tRowKeyCompare(&lastKey, &key) >= 0) {
117!
1927
          code = TSDB_CODE_INVALID_MSG;
×
1928
          vError("vgId:%d %s failed 1 since %s, version:%" PRId64, TD_VID(pVnode), __func__, tstrerror(terrno), ver);
×
1929
          goto _exit;
×
1930
        }
1931
      }
1932
    } else {
1933
      int32_t nRow = TARRAY_SIZE(pSubmitTbData->aRowP);
12,078,527✔
1934
      SRow  **aRow = (SRow **)TARRAY_DATA(pSubmitTbData->aRowP);
12,078,527✔
1935
      SRowKey lastRowKey;
1936
      for (int32_t iRow = 0; iRow < nRow; ++iRow) {
709,321,959✔
1937
        if (aRow[iRow]->ts < minKey || aRow[iRow]->ts > maxKey) {
697,244,187✔
1938
          code = TSDB_CODE_INVALID_MSG;
23,738✔
1939
          vError("vgId:%d %s failed 2 since %s, version:%" PRId64, TD_VID(pVnode), __func__, tstrerror(code), ver);
23,738!
1940
          goto _exit;
×
1941
        }
1942
        if (iRow == 0) {
697,220,449✔
1943
          tRowGetKey(aRow[iRow], &lastRowKey);
12,078,464✔
1944
        } else {
1945
          SRowKey rowKey;
1946
          tRowGetKey(aRow[iRow], &rowKey);
685,141,985✔
1947

1948
          if (tRowKeyCompare(&lastRowKey, &rowKey) >= 0) {
685,142,254!
1949
            code = TSDB_CODE_INVALID_MSG;
×
1950
            vError("vgId:%d %s failed 3 since %s, version:%" PRId64, TD_VID(pVnode), __func__, tstrerror(code), ver);
×
1951
            goto _exit;
×
1952
          }
1953
          lastRowKey = rowKey;
685,154,536✔
1954
        }
1955
      }
1956
    }
1957
  }
1958

1959
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
23,459,296✔
1960
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
12,077,790✔
1961

1962
    if (pSubmitTbData->pCreateTbReq) {
12,077,617✔
1963
      pSubmitTbData->uid = pSubmitTbData->pCreateTbReq->uid;
63,305✔
1964
    } else {
1965
      SMetaInfo info = {0};
12,014,312✔
1966

1967
      code = metaGetInfo(pVnode->pMeta, pSubmitTbData->uid, &info, NULL);
12,014,312✔
1968
      if (code) {
12,014,906✔
1969
        code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
9✔
1970
        vWarn("vgId:%d, table uid:%" PRId64 " not exists", TD_VID(pVnode), pSubmitTbData->uid);
9!
UNCOV
1971
        goto _exit;
×
1972
      }
1973

1974
      if (info.suid != pSubmitTbData->suid) {
12,014,897!
1975
        code = TSDB_CODE_INVALID_MSG;
×
1976
        goto _exit;
×
1977
      }
1978

1979
      if (info.suid) {
12,014,897✔
1980
        if (metaGetInfo(pVnode->pMeta, info.suid, &info, NULL) != 0) {
11,140,138!
1981
          vWarn("vgId:%d, table uid:%" PRId64 " not exists", TD_VID(pVnode), info.suid);
×
1982
        }
1983
      }
1984

1985
      if (pSubmitTbData->sver != info.skmVer) {
12,014,924!
UNCOV
1986
        code = TSDB_CODE_TDB_INVALID_TABLE_SCHEMA_VER;
×
UNCOV
1987
        goto _exit;
×
1988
      }
1989
    }
1990

1991
    if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
12,078,229✔
1992
      int32_t   nColData = TARRAY_SIZE(pSubmitTbData->aCol);
135✔
1993
      SColData *aColData = (SColData *)TARRAY_DATA(pSubmitTbData->aCol);
135✔
1994

1995
      if (nColData <= 0) {
135!
1996
        code = TSDB_CODE_INVALID_MSG;
×
1997
        goto _exit;
×
1998
      }
1999

2000
      if (aColData[0].cid != PRIMARYKEY_TIMESTAMP_COL_ID || aColData[0].type != TSDB_DATA_TYPE_TIMESTAMP ||
135!
2001
          aColData[0].nVal <= 0) {
135!
2002
        code = TSDB_CODE_INVALID_MSG;
×
2003
        goto _exit;
×
2004
      }
2005

2006
      for (int32_t j = 1; j < nColData; j++) {
579✔
2007
        if (aColData[j].nVal != aColData[0].nVal) {
444!
2008
          code = TSDB_CODE_INVALID_MSG;
×
2009
          goto _exit;
×
2010
        }
2011
      }
2012
    }
2013
  }
2014

2015
  vDebug("vgId:%d, submit block size %d", TD_VID(pVnode), (int32_t)taosArrayGetSize(pSubmitReq->aSubmitTbData));
11,381,506✔
2016

2017
  // loop to handle
2018
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
23,458,072✔
2019
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
12,075,769✔
2020

2021
    // create table
2022
    if (pSubmitTbData->pCreateTbReq) {
12,075,379✔
2023
      // alloc if need
2024
      if (pSubmitRsp->aCreateTbRsp == NULL &&
63,336✔
2025
          (pSubmitRsp->aCreateTbRsp = taosArrayInit(TARRAY_SIZE(pSubmitReq->aSubmitTbData), sizeof(SVCreateTbRsp))) ==
46,485!
2026
              NULL) {
2027
        code = terrno;
×
2028
        goto _exit;
8✔
2029
      }
2030

2031
      SVCreateTbRsp *pCreateTbRsp = taosArrayReserve(pSubmitRsp->aCreateTbRsp, 1);
63,337✔
2032

2033
      // create table
2034
      if (metaCreateTable2(pVnode->pMeta, ver, pSubmitTbData->pCreateTbReq, &pCreateTbRsp->pMeta) == 0) {
63,333✔
2035
        // create table success
2036

2037
        if (newTbUids == NULL &&
63,944!
2038
            (newTbUids = taosArrayInit(TARRAY_SIZE(pSubmitReq->aSubmitTbData), sizeof(int64_t))) == NULL) {
26,464✔
2039
          code = terrno;
×
2040
          goto _exit;
×
2041
        }
2042

2043
        if (taosArrayPush(newTbUids, &pSubmitTbData->uid) == NULL) {
74,960!
2044
          code = terrno;
×
2045
          goto _exit;
×
2046
        }
2047

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

2063
    // insert data
2064
    int32_t affectedRows;
2065
    code = tsdbInsertTableData(pVnode->pTsdb, ver, pSubmitTbData, &affectedRows);
12,075,375✔
2066
    if (code) goto _exit;
12,077,687!
2067

2068
    code = metaUpdateChangeTimeWithLock(pVnode->pMeta, pSubmitTbData->uid, pSubmitTbData->ctimeMs);
12,077,687✔
2069
    if (code) goto _exit;
12,076,214!
2070

2071
    pSubmitRsp->affectedRows += affectedRows;
12,076,214✔
2072
  }
2073

2074
  // update the affected table uid list
2075
  if (taosArrayGetSize(newTbUids) > 0) {
11,382,303✔
2076
    vDebug("vgId:%d, add %d table into query table list in handling submit", TD_VID(pVnode),
26,464✔
2077
           (int32_t)taosArrayGetSize(newTbUids));
2078
    if (tqUpdateTbUidList(pVnode->pTq, newTbUids, true) != 0) {
26,464!
2079
      vError("vgId:%d, failed to update tbUid list", TD_VID(pVnode));
×
2080
    }
2081
  }
2082

2083
_exit:
11,381,746✔
2084
  // message
2085
  pRsp->code = code;
11,381,754✔
2086
  tEncodeSize(tEncodeSSubmitRsp2, pSubmitRsp, pRsp->contLen, ret);
11,381,754!
2087
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
11,381,640✔
2088
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
11,381,813✔
2089
  if (tEncodeSSubmitRsp2(&ec, pSubmitRsp) < 0) {
11,381,816!
2090
    vError("vgId:%d, failed to encode submit response", TD_VID(pVnode));
×
2091
  }
2092
  tEncoderClear(&ec);
11,381,777✔
2093

2094
  // update statistics
2095
  (void)atomic_add_fetch_64(&pVnode->statis.nInsert, pSubmitRsp->affectedRows);
11,381,846✔
2096
  (void)atomic_add_fetch_64(&pVnode->statis.nInsertSuccess, pSubmitRsp->affectedRows);
11,381,881✔
2097
  (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsert, 1);
11,381,971✔
2098

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

2111
  if (code == 0) {
11,381,942✔
2112
    (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
11,381,900✔
2113
    code = tdProcessRSmaSubmit(pVnode->pSma, ver, pSubmitReq, pReq, len);
11,381,918✔
2114
  }
2115
  /*
2116
  if (code == 0) {
2117
    atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
2118
    code = tdProcessRSmaSubmit(pVnode->pSma, ver, pSubmitReq, pReq, len);
2119

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

2133
  // clear
2134
  taosArrayDestroy(newTbUids);
11,381,863✔
2135
  tDestroySubmitReq(pSubmitReq, 0 == pMsg->version ? TSDB_MSG_FLG_CMPT : TSDB_MSG_FLG_DECODE);
11,381,714!
2136
  tDestroySSubmitRsp2(pSubmitRsp, TSDB_MSG_FLG_ENCODE);
11,381,849✔
2137

2138
  if (code) terrno = code;
11,381,758✔
2139

2140
  taosMemoryFree(pAllocMsg);
11,381,758!
2141

2142
  return code;
11,381,750✔
2143
}
2144

2145
static int32_t vnodeProcessCreateTSmaReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
31✔
2146
  SVCreateTSmaReq req = {0};
31✔
2147
  SDecoder        coder = {0};
31✔
2148

2149
  if (pRsp) {
31!
2150
    pRsp->msgType = TDMT_VND_CREATE_SMA_RSP;
31✔
2151
    pRsp->code = TSDB_CODE_SUCCESS;
31✔
2152
    pRsp->pCont = NULL;
31✔
2153
    pRsp->contLen = 0;
31✔
2154
  }
2155

2156
  // decode and process req
2157
  tDecoderInit(&coder, pReq, len);
31✔
2158

2159
  if (tDecodeSVCreateTSmaReq(&coder, &req) < 0) {
31!
2160
    terrno = TSDB_CODE_MSG_DECODE_ERROR;
×
2161
    if (pRsp) pRsp->code = terrno;
×
2162
    goto _err;
×
2163
  }
2164

2165
  if (tdProcessTSmaCreate(pVnode->pSma, ver, (const char *)&req) < 0) {
31!
2166
    if (pRsp) pRsp->code = terrno;
×
2167
    goto _err;
×
2168
  }
2169

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

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

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

2194
static int32_t vnodeConsolidateAlterHashRange(SVnode *pVnode, int64_t ver) {
32✔
2195
  int32_t code = TSDB_CODE_SUCCESS;
32✔
2196

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

2200
  // TODO: trim meta of tables from TDB per hash range [pVnode->config.hashBegin, pVnode->config.hashEnd]
2201
  code = metaTrimTables(pVnode->pMeta, ver);
32✔
2202

2203
  return code;
32✔
2204
}
2205

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

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

2220
_exit:
7,986✔
2221
  pRsp->msgType = TDMT_VND_ALTER_CONFIRM_RSP;
7,986✔
2222
  pRsp->code = code;
7,986✔
2223
  pRsp->pCont = NULL;
7,986✔
2224
  pRsp->contLen = 0;
7,986✔
2225

2226
  return code;
7,986✔
2227
}
2228

2229
extern int32_t tsdbDisableAndCancelAllBgTask(STsdb *pTsdb);
2230
extern void    tsdbEnableBgTask(STsdb *pTsdb);
2231

2232
static int32_t vnodeProcessAlterConfigReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
825✔
2233
  bool walChanged = false;
825✔
2234
  bool tsdbChanged = false;
825✔
2235

2236
  SAlterVnodeConfigReq req = {0};
825✔
2237
  if (tDeserializeSAlterVnodeConfigReq(pReq, len, &req) != 0) {
825!
2238
    terrno = TSDB_CODE_INVALID_MSG;
×
2239
    return TSDB_CODE_INVALID_MSG;
×
2240
  }
2241

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

2251
  if (pVnode->config.cacheLastSize != req.cacheLastSize) {
827✔
2252
    pVnode->config.cacheLastSize = req.cacheLastSize;
366✔
2253
    tsdbCacheSetCapacity(pVnode, (size_t)pVnode->config.cacheLastSize * 1024 * 1024);
366✔
2254
  }
2255

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

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

2273
  if (pVnode->config.cacheLast != req.cacheLast) {
827✔
2274
    pVnode->config.cacheLast = req.cacheLast;
547✔
2275
  }
2276

2277
  if (pVnode->config.walCfg.fsyncPeriod != req.walFsyncPeriod) {
827✔
2278
    pVnode->config.walCfg.fsyncPeriod = req.walFsyncPeriod;
54✔
2279
    walChanged = true;
54✔
2280
  }
2281

2282
  if (pVnode->config.walCfg.level != req.walLevel) {
827✔
2283
    if (pVnode->config.walCfg.level == 0) {
36!
2284
      pVnode->config.walCfg.clearFiles = 1;
×
2285
    }
2286
    pVnode->config.walCfg.level = req.walLevel;
36✔
2287
    walChanged = true;
36✔
2288
  }
2289

2290
  if (pVnode->config.walCfg.retentionPeriod != req.walRetentionPeriod) {
827✔
2291
    pVnode->config.walCfg.retentionPeriod = req.walRetentionPeriod;
132✔
2292
    walChanged = true;
132✔
2293
  }
2294

2295
  if (pVnode->config.walCfg.retentionSize != req.walRetentionSize) {
827!
2296
    pVnode->config.walCfg.retentionSize = req.walRetentionSize;
×
2297
    walChanged = true;
×
2298
  }
2299

2300
  if (pVnode->config.tsdbCfg.keep0 != req.daysToKeep0) {
827✔
2301
    pVnode->config.tsdbCfg.keep0 = req.daysToKeep0;
54✔
2302
    if (!VND_IS_RSMA(pVnode)) {
54!
2303
      tsdbChanged = true;
54✔
2304
    }
2305
  }
2306

2307
  if (pVnode->config.tsdbCfg.keep1 != req.daysToKeep1) {
827✔
2308
    pVnode->config.tsdbCfg.keep1 = req.daysToKeep1;
54✔
2309
    if (!VND_IS_RSMA(pVnode)) {
54!
2310
      tsdbChanged = true;
54✔
2311
    }
2312
  }
2313

2314
  if (pVnode->config.tsdbCfg.keep2 != req.daysToKeep2) {
827✔
2315
    pVnode->config.tsdbCfg.keep2 = req.daysToKeep2;
56✔
2316
    if (!VND_IS_RSMA(pVnode)) {
56!
2317
      tsdbChanged = true;
56✔
2318
    }
2319
  }
2320

2321
  if (pVnode->config.tsdbCfg.keepTimeOffset != req.keepTimeOffset) {
827✔
2322
    pVnode->config.tsdbCfg.keepTimeOffset = req.keepTimeOffset;
2✔
2323
    if (!VND_IS_RSMA(pVnode)) {
2!
2324
      tsdbChanged = true;
2✔
2325
    }
2326
  }
2327

2328
  if (req.sttTrigger != -1 && req.sttTrigger != pVnode->config.sttTrigger) {
827!
2329
    if (req.sttTrigger > 1 && pVnode->config.sttTrigger > 1) {
×
2330
      pVnode->config.sttTrigger = req.sttTrigger;
×
2331
    } else {
2332
      vnodeAWait(&pVnode->commitTask);
×
2333

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

2339
      pVnode->config.sttTrigger = req.sttTrigger;
×
2340
      tsdbEnableBgTask(pVnode->pTsdb);
×
2341
    }
2342
  }
2343

2344
  if (req.minRows != -1 && req.minRows != pVnode->config.tsdbCfg.minRows) {
827!
2345
    pVnode->config.tsdbCfg.minRows = req.minRows;
×
2346
  }
2347

2348
  if (req.s3KeepLocal != -1 && req.s3KeepLocal != pVnode->config.s3KeepLocal) {
827!
2349
    pVnode->config.s3KeepLocal = req.s3KeepLocal;
×
2350
  }
2351
  if (req.s3Compact != -1 && req.s3Compact != pVnode->config.s3Compact) {
827!
2352
    pVnode->config.s3Compact = req.s3Compact;
×
2353
  }
2354

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

2361
  if (tsdbChanged) {
829✔
2362
    tsdbSetKeepCfg(pVnode->pTsdb, &pVnode->config.tsdbCfg);
58✔
2363
  }
2364

2365
  return 0;
829✔
2366
}
2367

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

2377
  SMetaReader mr = {0};
3,003✔
2378
  metaReaderDoInit(&mr, pVnode->pMeta, META_READER_NOLOCK);
3,003✔
2379
  STsdb *pTsdb = pVnode->pTsdb;
3,002✔
2380

2381
  if (deleteReq.level) {
3,002✔
2382
    pTsdb = deleteReq.level == 1 ? VND_RSMA1(pVnode) : VND_RSMA2(pVnode);
16✔
2383
  }
2384

2385
  int32_t sz = taosArrayGetSize(deleteReq.deleteReqs);
3,002✔
2386
  for (int32_t i = 0; i < sz; i++) {
6,986✔
2387
    SSingleDeleteReq *pOneReq = taosArrayGet(deleteReq.deleteReqs, i);
3,981✔
2388
    char             *name = pOneReq->tbname;
3,982✔
2389
    if (metaGetTableEntryByName(&mr, name) < 0) {
3,982✔
2390
      vDebug("vgId:%d, stream delete msg, skip since no table: %s", pVnode->config.vgId, name);
2,053✔
2391
      continue;
2,053✔
2392
    }
2393

2394
    int64_t uid = mr.me.uid;
1,932✔
2395

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

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

2419
static int32_t vnodeProcessDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
68,526✔
2420
                                     SRpcMsg *pOriginalMsg) {
2421
  int32_t     code = 0;
68,526✔
2422
  SDecoder   *pCoder = &(SDecoder){0};
68,526✔
2423
  SDeleteRes *pRes = &(SDeleteRes){0};
68,526✔
2424

2425
  pRsp->msgType = TDMT_VND_DELETE_RSP;
68,526✔
2426
  pRsp->pCont = NULL;
68,526✔
2427
  pRsp->contLen = 0;
68,526✔
2428
  pRsp->code = TSDB_CODE_SUCCESS;
68,526✔
2429

2430
  pRes->uidList = taosArrayInit(0, sizeof(tb_uid_t));
68,526✔
2431
  if (pRes->uidList == NULL) {
68,529!
2432
    code = terrno;
×
2433
    goto _err;
×
2434
  }
2435

2436
  tDecoderInit(pCoder, pReq, len);
68,529✔
2437
  code = tDecodeDeleteRes(pCoder, pRes);
68,526✔
2438
  if (code) goto _err;
68,532!
2439

2440
  if (pRes->affectedRows > 0) {
68,532✔
2441
    for (int32_t iUid = 0; iUid < taosArrayGetSize(pRes->uidList); iUid++) {
129,188✔
2442
      uint64_t uid = *(uint64_t *)taosArrayGet(pRes->uidList, iUid);
65,483✔
2443
      code = tsdbDeleteTableData(pVnode->pTsdb, ver, pRes->suid, uid, pRes->skey, pRes->ekey);
65,483✔
2444
      if (code) goto _err;
65,484!
2445
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, pRes->ctimeMs);
65,484✔
2446
      if (code) goto _err;
65,485!
2447
    }
2448
  }
2449

2450
  code = tdProcessRSmaDelete(pVnode->pSma, ver, pRes, pReq, len);
68,531✔
2451

2452
  tDecoderClear(pCoder);
68,527✔
2453
  taosArrayDestroy(pRes->uidList);
68,529✔
2454

2455
  SVDeleteRsp rsp = {.affectedRows = pRes->affectedRows};
68,531✔
2456
  int32_t     ret = 0;
68,531✔
2457
  tEncodeSize(tEncodeSVDeleteRsp, &rsp, pRsp->contLen, ret);
68,531!
2458
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
68,524✔
2459
  SEncoder ec = {0};
68,530✔
2460
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
68,530✔
2461
  code = tEncodeSVDeleteRsp(&ec, &rsp);
68,532✔
2462
  if (code) goto _err;
68,531!
2463
  tEncoderClear(&ec);
68,531✔
2464
  return code;
68,532✔
2465

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

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

2489
  pRsp->msgType = TDMT_VND_CREATE_INDEX_RSP;
953✔
2490
  pRsp->code = TSDB_CODE_SUCCESS;
953✔
2491
  pRsp->pCont = NULL;
953✔
2492
  pRsp->contLen = 0;
953✔
2493

2494
  tDecoderInit(&dc, pReq, len);
953✔
2495
  // decode req
2496
  if (tDecodeSVCreateStbReq(&dc, &req) < 0) {
953!
2497
    tDecoderClear(&dc);
×
2498
    return terrno = TSDB_CODE_INVALID_MSG;
×
2499
  }
2500

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

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

2521
  if ((code = tDeserializeSDropIdxReq(pReq, len, &req))) {
2,128!
2522
    pRsp->code = code;
×
2523
    return code;
×
2524
  }
2525

2526
  code = metaDropIndexFromSuperTable(pVnode->pMeta, ver, &req);
2,128✔
2527
  if (code) {
2,128!
2528
    pRsp->code = code;
×
2529
    return code;
×
2530
  }
2531
  return TSDB_CODE_SUCCESS;
2,128✔
2532
}
2533

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

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

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

2549
  pRsp->msgType = TDMT_SYNC_CONFIG_CHANGE_RSP;
×
2550
  pRsp->code = TSDB_CODE_SUCCESS;
×
2551
  pRsp->pCont = NULL;
×
2552
  pRsp->contLen = 0;
×
2553

2554
  return 0;
×
2555
}
2556

2557
static int32_t vnodeCheckToken(SVnode *pVnode, char *member0Token, char *member1Token) {
13✔
2558
  SSyncState syncState = syncGetState(pVnode->sync);
13✔
2559
  if (syncState.state != TAOS_SYNC_STATE_LEADER) {
13✔
2560
    return terrno = TSDB_CODE_SYN_NOT_LEADER;
5✔
2561
  }
2562

2563
  char token[TSDB_ARB_TOKEN_SIZE] = {0};
8✔
2564
  if (vnodeGetArbToken(pVnode, token) != 0) {
8!
2565
    return terrno = TSDB_CODE_NOT_FOUND;
×
2566
  }
2567

2568
  if (strncmp(token, member0Token, TSDB_ARB_TOKEN_SIZE) != 0 &&
8!
2569
      strncmp(token, member1Token, TSDB_ARB_TOKEN_SIZE) != 0) {
8✔
2570
    return terrno = TSDB_CODE_MND_ARB_TOKEN_MISMATCH;
2✔
2571
  }
2572

2573
  terrno = TSDB_CODE_SUCCESS;
6✔
2574
  return 0;
6✔
2575
}
2576

2577
static int32_t vnodeCheckSyncd(SVnode *pVnode, char *member0Token, char *member1Token) {
8✔
2578
  int32_t code = vnodeCheckToken(pVnode, member0Token, member1Token);
8✔
2579
  if (code != 0) {
8✔
2580
    return code;
5✔
2581
  }
2582

2583
  return syncCheckSynced(pVnode->sync);
3✔
2584
}
2585

2586
static int32_t vnodeProcessArbCheckSyncReq(SVnode *pVnode, void *pReq, int32_t len, SRpcMsg *pRsp) {
8✔
2587
  int32_t code = 0;
8✔
2588

2589
  SVArbCheckSyncReq syncReq = {0};
8✔
2590

2591
  code = tDeserializeSVArbCheckSyncReq(pReq, len, &syncReq);
8✔
2592
  if (code) {
8!
2593
    return terrno = code;
×
2594
  }
2595

2596
  pRsp->msgType = TDMT_VND_ARB_CHECK_SYNC_RSP;
8✔
2597
  pRsp->code = TSDB_CODE_SUCCESS;
8✔
2598
  pRsp->pCont = NULL;
8✔
2599
  pRsp->contLen = 0;
8✔
2600

2601
  SVArbCheckSyncRsp syncRsp = {0};
8✔
2602
  syncRsp.arbToken = syncReq.arbToken;
8✔
2603
  syncRsp.member0Token = syncReq.member0Token;
8✔
2604
  syncRsp.member1Token = syncReq.member1Token;
8✔
2605
  syncRsp.vgId = TD_VID(pVnode);
8✔
2606

2607
  if (vnodeCheckSyncd(pVnode, syncReq.member0Token, syncReq.member1Token) != 0) {
8✔
2608
    vError("vgId:%d, failed to check assigned log syncd", TD_VID(pVnode));
5!
2609
  }
2610
  syncRsp.errCode = terrno;
8✔
2611

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

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

2631
  if (tSerializeSVArbCheckSyncRsp(pHead, contLen, &syncRsp) <= 0) {
8!
2632
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2633
    rpcFreeCont(pHead);
×
2634
    code = -1;
×
2635
    goto _OVER;
×
2636
  }
2637

2638
  pRsp->pCont = pHead;
8✔
2639
  pRsp->contLen = contLen;
8✔
2640

2641
  terrno = TSDB_CODE_SUCCESS;
8✔
2642

2643
_OVER:
8✔
2644
  tFreeSVArbCheckSyncReq(&syncReq);
8✔
2645
  return code;
8✔
2646
}
2647

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